TCP/session improvements
[vpp.git] / src / vnet / session / node.c
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15
16 #include <math.h>
17 #include <vlib/vlib.h>
18 #include <vnet/vnet.h>
19 #include <vnet/tcp/tcp.h>
20 #include <vppinfra/elog.h>
21 #include <vnet/session/application.h>
22 #include <vnet/session/session_debug.h>
23 #include <vlibmemory/unix_shared_memory_queue.h>
24
25 vlib_node_registration_t session_queue_node;
26
27 typedef struct
28 {
29   u32 session_index;
30   u32 server_thread_index;
31 } session_queue_trace_t;
32
33 /* packet trace format function */
34 static u8 *
35 format_session_queue_trace (u8 * s, va_list * args)
36 {
37   CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
38   CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
39   session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
40
41   s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
42               t->session_index, t->server_thread_index);
43   return s;
44 }
45
46 vlib_node_registration_t session_queue_node;
47
48 #define foreach_session_queue_error             \
49 _(TX, "Packets transmitted")                    \
50 _(TIMER, "Timer events")
51
52 typedef enum
53 {
54 #define _(sym,str) SESSION_QUEUE_ERROR_##sym,
55   foreach_session_queue_error
56 #undef _
57     SESSION_QUEUE_N_ERROR,
58 } session_queue_error_t;
59
60 static char *session_queue_error_strings[] = {
61 #define _(sym,string) string,
62   foreach_session_queue_error
63 #undef _
64 };
65
66 static u32 session_type_to_next[] = {
67   SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT,
68   SESSION_QUEUE_NEXT_IP4_LOOKUP,
69   SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT,
70   SESSION_QUEUE_NEXT_IP6_LOOKUP,
71 };
72
73 always_inline int
74 session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node,
75                                 session_manager_main_t * smm,
76                                 session_fifo_event_t * e0,
77                                 stream_session_t * s0, u32 thread_index,
78                                 int *n_tx_packets, u8 peek_data)
79 {
80   u32 n_trace = vlib_get_trace_count (vm, node);
81   u32 left_to_snd0, max_len_to_snd0, len_to_deq0, n_bufs, snd_space0;
82   u32 n_frame_bytes, n_frames_per_evt;
83   transport_connection_t *tc0;
84   transport_proto_vft_t *transport_vft;
85   u32 next_index, next0, *to_next, n_left_to_next, bi0;
86   vlib_buffer_t *b0;
87   u32 rx_offset = 0, max_dequeue0;
88   u16 snd_mss0;
89   u8 *data0;
90   int i, n_bytes_read;
91
92   next_index = next0 = session_type_to_next[s0->session_type];
93
94   transport_vft = session_get_transport_vft (s0->session_type);
95   tc0 = transport_vft->get_connection (s0->connection_index, thread_index);
96
97   /* Make sure we have space to send and there's something to dequeue */
98   snd_space0 = transport_vft->send_space (tc0);
99   snd_mss0 = transport_vft->send_mss (tc0);
100
101   /* Can't make any progress */
102   if (snd_space0 == 0 || snd_mss0 == 0)
103     {
104       vec_add1 (smm->evts_partially_read[thread_index], *e0);
105       return 0;
106     }
107
108   if (peek_data)
109     {
110       /* Offset in rx fifo from where to peek data  */
111       rx_offset = transport_vft->tx_fifo_offset (tc0);
112     }
113
114   /* Check how much we can pull. If buffering, subtract the offset */
115   max_dequeue0 = svm_fifo_max_dequeue (s0->server_tx_fifo) - rx_offset;
116
117   /* Allow enqueuing of a new event */
118   svm_fifo_unset_event (s0->server_tx_fifo);
119
120   /* Nothing to read return */
121   if (max_dequeue0 == 0)
122     {
123       return 0;
124     }
125
126   /* Ensure we're not writing more than transport window allows */
127   max_len_to_snd0 = clib_min (max_dequeue0, snd_space0);
128
129   /* TODO check if transport is willing to send len_to_snd0
130    * bytes (Nagle) */
131
132   n_frame_bytes = snd_mss0 * VLIB_FRAME_SIZE;
133   n_frames_per_evt = ceil ((double) max_len_to_snd0 / n_frame_bytes);
134
135   n_bufs = vec_len (smm->tx_buffers[thread_index]);
136   left_to_snd0 = max_len_to_snd0;
137   for (i = 0; i < n_frames_per_evt; i++)
138     {
139       /* Make sure we have at least one full frame of buffers ready */
140       if (PREDICT_FALSE (n_bufs < VLIB_FRAME_SIZE))
141         {
142           vec_validate (smm->tx_buffers[thread_index],
143                         n_bufs + VLIB_FRAME_SIZE - 1);
144           n_bufs +=
145             vlib_buffer_alloc (vm, &smm->tx_buffers[thread_index][n_bufs],
146                                VLIB_FRAME_SIZE);
147
148           /* buffer shortage
149            * XXX 0.9 because when debugging we might not get a full frame */
150           if (PREDICT_FALSE (n_bufs < 0.9 * VLIB_FRAME_SIZE))
151             {
152               if (svm_fifo_set_event (s0->server_tx_fifo))
153                 {
154                   vec_add1 (smm->evts_partially_read[thread_index], *e0);
155                 }
156               return -1;
157             }
158
159           _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
160         }
161
162       vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
163       while (left_to_snd0 && n_left_to_next)
164         {
165           /* Get free buffer */
166           n_bufs--;
167           bi0 = smm->tx_buffers[thread_index][n_bufs];
168           _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
169
170           b0 = vlib_get_buffer (vm, bi0);
171           b0->error = 0;
172           b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID
173             | VNET_BUFFER_LOCALLY_ORIGINATED;
174           b0->current_data = 0;
175
176           /* RX on the local interface. tx in default fib */
177           vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0;
178           vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0;
179
180           /* usual speculation, or the enqueue_x1 macro will barf */
181           to_next[0] = bi0;
182           to_next += 1;
183           n_left_to_next -= 1;
184
185           VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0);
186           if (PREDICT_FALSE (n_trace > 0))
187             {
188               session_queue_trace_t *t0;
189               vlib_trace_buffer (vm, node, next_index, b0,
190                                  1 /* follow_chain */ );
191               vlib_set_trace_count (vm, node, --n_trace);
192               t0 = vlib_add_trace (vm, node, b0, sizeof (*t0));
193               t0->session_index = s0->session_index;
194               t0->server_thread_index = s0->thread_index;
195             }
196
197           len_to_deq0 = (left_to_snd0 < snd_mss0) ? left_to_snd0 : snd_mss0;
198
199           /* *INDENT-OFF* */
200           SESSION_EVT_DBG(SESSION_EVT_DEQ, s0, ({
201               ed->data[0] = e0->event_id;
202               ed->data[1] = max_dequeue0;
203               ed->data[2] = len_to_deq0;
204               ed->data[3] = left_to_snd0;
205           }));
206           /* *INDENT-ON* */
207
208           /* Make room for headers */
209           data0 = vlib_buffer_make_headroom (b0, MAX_HDRS_LEN);
210
211           /* Dequeue the data
212            * TODO 1) peek instead of dequeue
213            *      2) buffer chains */
214           if (peek_data)
215             {
216               n_bytes_read = svm_fifo_peek (s0->server_tx_fifo, s0->pid,
217                                             rx_offset, len_to_deq0, data0);
218               if (n_bytes_read <= 0)
219                 goto dequeue_fail;
220
221               /* Keep track of progress locally, transport is also supposed to
222                * increment it independently when pushing the header */
223               rx_offset += n_bytes_read;
224             }
225           else
226             {
227               n_bytes_read = svm_fifo_dequeue_nowait (s0->server_tx_fifo,
228                                                       s0->pid, len_to_deq0,
229                                                       data0);
230               if (n_bytes_read <= 0)
231                 goto dequeue_fail;
232             }
233
234           b0->current_length = n_bytes_read;
235
236           /* Ask transport to push header */
237           transport_vft->push_header (tc0, b0);
238
239           left_to_snd0 -= n_bytes_read;
240           *n_tx_packets = *n_tx_packets + 1;
241
242           vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
243                                            to_next, n_left_to_next,
244                                            bi0, next0);
245         }
246       vlib_put_next_frame (vm, node, next_index, n_left_to_next);
247     }
248
249   /* If we couldn't dequeue all bytes mark as partially read */
250   if (max_len_to_snd0 < max_dequeue0)
251     {
252       /* If we don't already have new event */
253       if (svm_fifo_set_event (s0->server_tx_fifo))
254         {
255           vec_add1 (smm->evts_partially_read[thread_index], *e0);
256         }
257     }
258   return 0;
259
260 dequeue_fail:
261   /*
262    * Can't read from fifo. If we don't already have an event, save as partially
263    * read, return buff to free list and return
264    */
265   clib_warning ("dequeue fail");
266
267   if (svm_fifo_set_event (s0->server_tx_fifo))
268     {
269       vec_add1 (smm->evts_partially_read[thread_index], *e0);
270     }
271   vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1);
272   _vec_len (smm->tx_buffers[thread_index]) += 1;
273
274   return 0;
275 }
276
277 int
278 session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
279                               session_manager_main_t * smm,
280                               session_fifo_event_t * e0,
281                               stream_session_t * s0, u32 thread_index,
282                               int *n_tx_pkts)
283 {
284   return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index,
285                                          n_tx_pkts, 1);
286 }
287
288 int
289 session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node,
290                                  session_manager_main_t * smm,
291                                  session_fifo_event_t * e0,
292                                  stream_session_t * s0, u32 thread_index,
293                                  int *n_tx_pkts)
294 {
295   return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index,
296                                          n_tx_pkts, 0);
297 }
298
299 static uword
300 session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node,
301                        vlib_frame_t * frame)
302 {
303   session_manager_main_t *smm = vnet_get_session_manager_main ();
304   session_fifo_event_t *my_fifo_events, *e;
305   u32 n_to_dequeue, n_events;
306   unix_shared_memory_queue_t *q;
307   application_t *app;
308   int n_tx_packets = 0;
309   u32 my_thread_index = vm->cpu_index;
310   int i, rv;
311
312   /*
313    *  Update TCP time
314    */
315   tcp_update_time (vlib_time_now (vm), my_thread_index);
316
317   /*
318    * Get vpp queue events
319    */
320   q = smm->vpp_event_queues[my_thread_index];
321   if (PREDICT_FALSE (q == 0))
322     return 0;
323
324   /* min number of events we can dequeue without blocking */
325   n_to_dequeue = q->cursize;
326   my_fifo_events = smm->fifo_events[my_thread_index];
327
328   if (n_to_dequeue == 0 && vec_len (my_fifo_events) == 0)
329     return 0;
330
331   SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
332
333   /*
334    * If we didn't manage to process previous events try going
335    * over them again without dequeuing new ones.
336    */
337   /* XXX: Block senders to sessions that can't keep up */
338   if (vec_len (my_fifo_events) >= 100)
339     {
340       clib_warning ("too many fifo events unsolved");
341       goto skip_dequeue;
342     }
343
344   /* See you in the next life, don't be late */
345   if (pthread_mutex_trylock (&q->mutex))
346     return 0;
347
348   for (i = 0; i < n_to_dequeue; i++)
349     {
350       vec_add2 (my_fifo_events, e, 1);
351       unix_shared_memory_queue_sub_raw (q, (u8 *) e);
352     }
353
354   /* The other side of the connection is not polling */
355   if (q->cursize < (q->maxsize / 8))
356     (void) pthread_cond_broadcast (&q->condvar);
357   pthread_mutex_unlock (&q->mutex);
358
359   smm->fifo_events[my_thread_index] = my_fifo_events;
360
361 skip_dequeue:
362   n_events = vec_len (my_fifo_events);
363   for (i = 0; i < n_events; i++)
364     {
365       svm_fifo_t *f0;           /* $$$ prefetch 1 ahead maybe */
366       stream_session_t *s0;
367       u32 session_index0;
368       session_fifo_event_t *e0;
369
370       e0 = &my_fifo_events[i];
371       f0 = e0->fifo;
372       session_index0 = f0->server_session_index;
373
374       /* $$$ add multiple event queues, per vpp worker thread */
375       ASSERT (f0->server_thread_index == my_thread_index);
376
377       s0 = stream_session_get_if_valid (session_index0, my_thread_index);
378
379       if (CLIB_DEBUG && !s0)
380         {
381           clib_warning ("It's dead, Jim!");
382           continue;
383         }
384
385       if (PREDICT_FALSE (s0->session_state == SESSION_STATE_CLOSED))
386         continue;
387
388       ASSERT (s0->thread_index == my_thread_index);
389
390       switch (e0->event_type)
391         {
392         case FIFO_EVENT_SERVER_TX:
393           /* Spray packets in per session type frames, since they go to
394            * different nodes */
395           rv = (smm->session_tx_fns[s0->session_type]) (vm, node, smm, e0, s0,
396                                                         my_thread_index,
397                                                         &n_tx_packets);
398           /* Out of buffers */
399           if (rv < 0)
400             goto done;
401
402           break;
403         case FIFO_EVENT_SERVER_EXIT:
404           stream_session_disconnect (s0);
405           break;
406         case FIFO_EVENT_BUILTIN_RX:
407           svm_fifo_unset_event (s0->server_rx_fifo);
408           /* Get session's server */
409           app = application_get (s0->app_index);
410           app->cb_fns.builtin_server_rx_callback (s0);
411           break;
412         default:
413           clib_warning ("unhandled event type %d", e0->event_type);
414         }
415     }
416
417 done:
418
419   /* Couldn't process all events. Probably out of buffers */
420   if (PREDICT_FALSE (i < n_events))
421     {
422       session_fifo_event_t *partially_read =
423         smm->evts_partially_read[my_thread_index];
424       vec_add (partially_read, &my_fifo_events[i], n_events - i);
425       vec_free (my_fifo_events);
426       smm->fifo_events[my_thread_index] = partially_read;
427       smm->evts_partially_read[my_thread_index] = 0;
428     }
429   else
430     {
431       vec_free (smm->fifo_events[my_thread_index]);
432       smm->fifo_events[my_thread_index] =
433         smm->evts_partially_read[my_thread_index];
434       smm->evts_partially_read[my_thread_index] = 0;
435     }
436
437   vlib_node_increment_counter (vm, session_queue_node.index,
438                                SESSION_QUEUE_ERROR_TX, n_tx_packets);
439
440   SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 1);
441
442   return n_tx_packets;
443 }
444
445 /* *INDENT-OFF* */
446 VLIB_REGISTER_NODE (session_queue_node) =
447 {
448   .function = session_queue_node_fn,
449   .name = "session-queue",
450   .format_trace = format_session_queue_trace,
451   .type = VLIB_NODE_TYPE_INPUT,
452   .n_errors = ARRAY_LEN (session_queue_error_strings),
453   .error_strings = session_queue_error_strings,
454   .n_next_nodes = SESSION_QUEUE_N_NEXT,
455   .state = VLIB_NODE_STATE_DISABLED,
456   .next_nodes =
457   {
458       [SESSION_QUEUE_NEXT_DROP] = "error-drop",
459       [SESSION_QUEUE_NEXT_IP4_LOOKUP] = "ip4-lookup",
460       [SESSION_QUEUE_NEXT_IP6_LOOKUP] = "ip6-lookup",
461       [SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT] = "tcp4-output",
462       [SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT] = "tcp6-output",
463   },
464 };
465 /* *INDENT-ON* */
466
467 /*
468  * fd.io coding-style-patch-verification: ON
469  *
470  * Local Variables:
471  * eval: (c-set-style "gnu")
472  * End:
473  */