FD.io VPP  v18.07.1-19-g511ce25
Vector Packet Processing
session_node.c
Go to the documentation of this file.
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 <vppinfra/elog.h>
20 #include <vnet/session/transport.h>
21 #include <vnet/session/session.h>
24 #include <svm/queue.h>
25 
27 
28 typedef struct
29 {
33 
34 /* packet trace format function */
35 static u8 *
36 format_session_queue_trace (u8 * s, va_list * args)
37 {
38  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
39  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
40  session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *);
41 
42  s = format (s, "SESSION_QUEUE: session index %d, server thread index %d",
44  return s;
45 }
46 
48 
49 #define foreach_session_queue_error \
50 _(TX, "Packets transmitted") \
51 _(TIMER, "Timer events") \
52 _(NO_BUFFER, "Out of buffers")
53 
54 typedef enum
55 {
56 #define _(sym,str) SESSION_QUEUE_ERROR_##sym,
58 #undef _
61 
62 static char *session_queue_error_strings[] = {
63 #define _(sym,string) string,
65 #undef _
66 };
67 
68 enum
69 {
73 };
74 
75 
76 static void
78  u32 next_index, u32 * to_next, u16 n_segs,
79  stream_session_t * s, u32 n_trace)
80 {
82  vlib_buffer_t *b;
83  int i;
84 
85  for (i = 0; i < clib_min (n_trace, n_segs); i++)
86  {
87  b = vlib_get_buffer (vm, to_next[i - n_segs]);
88  vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
89  t = vlib_add_trace (vm, node, b, sizeof (*t));
90  t->session_index = s->session_index;
91  t->server_thread_index = s->thread_index;
92  }
93  vlib_set_trace_count (vm, node, n_trace - i);
94 }
95 
96 always_inline void
98  vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
99 {
101  vlib_buffer_t *chain_b, *prev_b;
102  u32 chain_bi0, to_deq, left_from_seg;
103  u16 len_to_deq, n_bytes_read;
104  u8 *data, j;
105 
106  b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
108 
109  chain_b = b;
110  left_from_seg = clib_min (ctx->snd_mss - b->current_length,
111  ctx->left_to_snd);
112  to_deq = left_from_seg;
113  for (j = 1; j < ctx->n_bufs_per_seg; j++)
114  {
115  prev_b = chain_b;
116  len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
117 
118  *n_bufs -= 1;
119  chain_bi0 = smm->tx_buffers[ctx->s->thread_index][*n_bufs];
120  _vec_len (smm->tx_buffers[ctx->s->thread_index]) = *n_bufs;
121 
122  chain_b = vlib_get_buffer (vm, chain_bi0);
123  chain_b->current_data = 0;
124  data = vlib_buffer_get_current (chain_b);
125  if (peek_data)
126  {
127  n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo,
128  ctx->tx_offset, len_to_deq, data);
129  ctx->tx_offset += n_bytes_read;
130  }
131  else
132  {
133  if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
134  {
135  svm_fifo_t *f = ctx->s->server_tx_fifo;
136  session_dgram_hdr_t *hdr = &ctx->hdr;
137  u16 deq_now;
138  deq_now = clib_min (hdr->data_length - hdr->data_offset,
139  len_to_deq);
140  n_bytes_read = svm_fifo_peek (f, hdr->data_offset, deq_now,
141  data);
142  ASSERT (n_bytes_read > 0);
143 
144  hdr->data_offset += n_bytes_read;
145  if (hdr->data_offset == hdr->data_length)
146  svm_fifo_dequeue_drop (f, hdr->data_length);
147  }
148  else
149  n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
150  len_to_deq, data);
151  }
152  ASSERT (n_bytes_read == len_to_deq);
153  chain_b->current_length = n_bytes_read;
155 
156  /* update previous buffer */
157  prev_b->next_buffer = chain_bi0;
158  prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
159 
160  /* update current buffer */
161  chain_b->next_buffer = 0;
162 
163  to_deq -= n_bytes_read;
164  if (to_deq == 0)
165  break;
166  }
167  ASSERT (to_deq == 0
168  && b->total_length_not_including_first_buffer == left_from_seg);
169  ctx->left_to_snd -= left_from_seg;
170 }
171 
172 always_inline int
175  u32 thread_index, u16 * n_bufs, u32 wanted)
176 {
177  u32 n_alloc;
178  vec_validate_aligned (smm->tx_buffers[thread_index], wanted - 1,
180  n_alloc = vlib_buffer_alloc (vm, &smm->tx_buffers[thread_index][*n_bufs],
181  wanted - *n_bufs);
182  *n_bufs += n_alloc;
183  _vec_len (smm->tx_buffers[thread_index]) = *n_bufs;
184  return n_alloc;
185 }
186 
187 always_inline void
189  vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
190 {
191  u32 len_to_deq;
192  u8 *data0;
193  int n_bytes_read;
194 
195  /*
196  * Start with the first buffer in chain
197  */
198  b->error = 0;
199  b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
200  b->current_data = 0;
201 
203  len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
204 
205  if (peek_data)
206  {
207  n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo, ctx->tx_offset,
208  len_to_deq, data0);
209  ASSERT (n_bytes_read > 0);
210  /* Keep track of progress locally, transport is also supposed to
211  * increment it independently when pushing the header */
212  ctx->tx_offset += n_bytes_read;
213  }
214  else
215  {
216  if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
217  {
218  session_dgram_hdr_t *hdr = &ctx->hdr;
219  svm_fifo_t *f = ctx->s->server_tx_fifo;
220  u16 deq_now;
221  u32 offset;
222 
223  ASSERT (hdr->data_length > hdr->data_offset);
224  deq_now = clib_min (hdr->data_length - hdr->data_offset,
225  len_to_deq);
226  offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
227  n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
228  ASSERT (n_bytes_read > 0);
229 
230  if (ctx->s->session_state == SESSION_STATE_LISTENING)
231  {
232  ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
233  ctx->tc->rmt_port = hdr->rmt_port;
234  }
235  hdr->data_offset += n_bytes_read;
236  if (hdr->data_offset == hdr->data_length)
237  {
238  offset = hdr->data_length + SESSION_CONN_HDR_LEN;
239  svm_fifo_dequeue_drop (f, offset);
240  }
241  }
242  else
243  {
244  n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
245  len_to_deq, data0);
246  ASSERT (n_bytes_read > 0);
247  }
248  }
249  b->current_length = n_bytes_read;
250  ctx->left_to_snd -= n_bytes_read;
251 
252  /*
253  * Fill in the remaining buffers in the chain, if any
254  */
255  if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
256  session_tx_fifo_chain_tail (vm, ctx, b, n_bufs, peek_data);
257 
258  /* *INDENT-OFF* */
259  SESSION_EVT_DBG(SESSION_EVT_DEQ, ctx->s, ({
260  ed->data[0] = FIFO_EVENT_APP_TX;
261  ed->data[1] = ctx->max_dequeue;
262  ed->data[2] = len_to_deq;
263  ed->data[3] = ctx->left_to_snd;
264  }));
265  /* *INDENT-ON* */
266 }
267 
270 {
271  if (peek_data)
272  {
273  /* Can retransmit for closed sessions but can't send new data if
274  * session is not ready or closed */
275  if (s->session_state < SESSION_STATE_READY)
276  return 1;
277  if (s->session_state == SESSION_STATE_CLOSED)
278  return 2;
279  }
280  return 0;
281 }
282 
285 {
286  if (peek_data)
287  {
288  return ctx->transport_vft->get_connection (ctx->s->connection_index,
289  ctx->s->thread_index);
290  }
291  else
292  {
293  if (ctx->s->session_state == SESSION_STATE_LISTENING)
294  return ctx->transport_vft->get_listener (ctx->s->connection_index);
295  else
296  {
297  return ctx->transport_vft->get_connection (ctx->s->connection_index,
298  ctx->s->thread_index);
299  }
300  }
301 }
302 
303 always_inline void
305  u32 max_segs, u8 peek_data)
306 {
307  u32 n_bytes_per_buf, n_bytes_per_seg;
308  ctx->max_dequeue = svm_fifo_max_dequeue (ctx->s->server_tx_fifo);
309  if (peek_data)
310  {
311  /* Offset in rx fifo from where to peek data */
312  ctx->tx_offset = ctx->transport_vft->tx_fifo_offset (ctx->tc);
313  if (PREDICT_FALSE (ctx->tx_offset >= ctx->max_dequeue))
314  {
315  ctx->max_len_to_snd = 0;
316  return;
317  }
318  ctx->max_dequeue -= ctx->tx_offset;
319  }
320  else
321  {
322  if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
323  {
324  if (ctx->max_dequeue <= sizeof (ctx->hdr))
325  {
326  ctx->max_len_to_snd = 0;
327  return;
328  }
329  svm_fifo_peek (ctx->s->server_tx_fifo, 0, sizeof (ctx->hdr),
330  (u8 *) & ctx->hdr);
331  ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
332  ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset;
333  }
334  }
335  ASSERT (ctx->max_dequeue > 0);
336 
337  /* Ensure we're not writing more than transport window allows */
338  if (ctx->max_dequeue < ctx->snd_space)
339  {
340  /* Constrained by tx queue. Try to send only fully formed segments */
341  ctx->max_len_to_snd =
342  (ctx->max_dequeue > ctx->snd_mss) ?
343  ctx->max_dequeue - ctx->max_dequeue % ctx->snd_mss : ctx->max_dequeue;
344  /* TODO Nagle ? */
345  }
346  else
347  {
348  /* Expectation is that snd_space0 is already a multiple of snd_mss */
349  ctx->max_len_to_snd = ctx->snd_space;
350  }
351 
352  /* Check if we're tx constrained by the node */
353  ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->snd_mss);
354  if (ctx->n_segs_per_evt > max_segs)
355  {
356  ctx->n_segs_per_evt = max_segs;
357  ctx->max_len_to_snd = max_segs * ctx->snd_mss;
358  }
359 
360  n_bytes_per_buf = vlib_buffer_free_list_buffer_size (vm,
362  ASSERT (n_bytes_per_buf > MAX_HDRS_LEN);
363  n_bytes_per_seg = MAX_HDRS_LEN + ctx->snd_mss;
364  ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
365  ctx->deq_per_buf = clib_min (ctx->snd_mss, n_bytes_per_buf);
366  ctx->deq_per_first_buf = clib_min (ctx->snd_mss,
367  n_bytes_per_buf - MAX_HDRS_LEN);
368 }
369 
370 always_inline int
372  session_fifo_event_t * e,
373  stream_session_t * s, int *n_tx_packets,
374  u8 peek_data)
375 {
376  u32 next_index, next0, next1, *to_next, n_left_to_next;
377  u32 n_trace = vlib_get_trace_count (vm, node), n_bufs_needed = 0;
378  u32 thread_index = s->thread_index, n_left, pbi;
380  session_tx_context_t *ctx = &smm->ctx[thread_index];
382  vlib_buffer_t *pb;
383  u16 n_bufs, rv;
384 
385  if (PREDICT_FALSE ((rv = session_tx_not_ready (s, peek_data))))
386  {
387  if (rv < 2)
388  vec_add1 (smm->pending_event_vector[thread_index], *e);
389  return SESSION_TX_NO_DATA;
390  }
391 
392  next_index = smm->session_type_to_next[s->session_type];
393  next0 = next1 = next_index;
394 
396  ctx->s = s;
398  ctx->tc = session_tx_get_transport (ctx, peek_data);
399  ctx->snd_mss = ctx->transport_vft->send_mss (ctx->tc);
400  ctx->snd_space = ctx->transport_vft->send_space (ctx->tc);
401  if (ctx->snd_space == 0 || ctx->snd_mss == 0)
402  {
403  vec_add1 (smm->pending_event_vector[thread_index], *e);
404  return SESSION_TX_NO_DATA;
405  }
406 
407  /* Allow enqueuing of a new event */
408  svm_fifo_unset_event (s->server_tx_fifo);
409 
410  /* Check how much we can pull. */
411  session_tx_set_dequeue_params (vm, ctx, VLIB_FRAME_SIZE - *n_tx_packets,
412  peek_data);
413 
414  if (PREDICT_FALSE (!ctx->max_len_to_snd))
415  return SESSION_TX_NO_DATA;
416 
417  n_bufs = vec_len (smm->tx_buffers[thread_index]);
418  n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg;
419 
420  /*
421  * Make sure we have at least one full frame of buffers ready
422  */
423  if (n_bufs < n_bufs_needed)
424  {
425  session_output_try_get_buffers (vm, smm, thread_index, &n_bufs,
427  if (PREDICT_FALSE (n_bufs < n_bufs_needed))
428  {
429  vec_add1 (smm->pending_event_vector[thread_index], *e);
430  return SESSION_TX_NO_BUFFERS;
431  }
432  }
433 
434  /*
435  * Write until we fill up a frame
436  */
437  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
438  if (PREDICT_FALSE (ctx->n_segs_per_evt > n_left_to_next))
439  {
440  ctx->n_segs_per_evt = n_left_to_next;
441  ctx->max_len_to_snd = ctx->snd_mss * n_left_to_next;
442  }
443  ctx->left_to_snd = ctx->max_len_to_snd;
444  n_left = ctx->n_segs_per_evt;
445 
446  while (n_left >= 4)
447  {
448  vlib_buffer_t *b0, *b1;
449  u32 bi0, bi1;
450 
451  pbi = smm->tx_buffers[thread_index][n_bufs - 3];
452  pb = vlib_get_buffer (vm, pbi);
453  vlib_prefetch_buffer_header (pb, STORE);
454  pbi = smm->tx_buffers[thread_index][n_bufs - 4];
455  pb = vlib_get_buffer (vm, pbi);
456  vlib_prefetch_buffer_header (pb, STORE);
457 
458  to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
459  to_next[1] = bi1 = smm->tx_buffers[thread_index][--n_bufs];
460 
461  b0 = vlib_get_buffer (vm, bi0);
462  b1 = vlib_get_buffer (vm, bi1);
463 
464  session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
465  session_tx_fill_buffer (vm, ctx, b1, &n_bufs, peek_data);
466 
467  ctx->transport_vft->push_header (ctx->tc, b0);
468  ctx->transport_vft->push_header (ctx->tc, b1);
469 
470  to_next += 2;
471  n_left_to_next -= 2;
472  n_left -= 2;
473 
476 
477  vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
478  n_left_to_next, bi0, bi1, next0,
479  next1);
480  }
481  while (n_left)
482  {
483  vlib_buffer_t *b0;
484  u32 bi0;
485 
486  if (n_left > 1)
487  {
488  pbi = smm->tx_buffers[thread_index][n_bufs - 2];
489  pb = vlib_get_buffer (vm, pbi);
490  vlib_prefetch_buffer_header (pb, STORE);
491  }
492 
493  to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
494  b0 = vlib_get_buffer (vm, bi0);
495  session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
496 
497  /* Ask transport to push header after current_length and
498  * total_length_not_including_first_buffer are updated */
499  ctx->transport_vft->push_header (ctx->tc, b0);
500 
501  to_next += 1;
502  n_left_to_next -= 1;
503  n_left -= 1;
504 
506 
507  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
508  n_left_to_next, bi0, next0);
509  }
510 
511  if (PREDICT_FALSE (n_trace > 0))
512  session_tx_trace_frame (vm, node, next_index, to_next,
513  ctx->n_segs_per_evt, s, n_trace);
514 
515  _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
516  *n_tx_packets += ctx->n_segs_per_evt;
517  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
518 
519  /* If we couldn't dequeue all bytes mark as partially read */
520  ASSERT (ctx->left_to_snd == 0);
521  if (ctx->max_len_to_snd < ctx->max_dequeue)
522  if (svm_fifo_set_event (s->server_tx_fifo))
523  vec_add1 (smm->pending_event_vector[thread_index], *e);
524 
525  if (!peek_data && ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
526  {
527  /* Fix dgram pre header */
528  if (ctx->max_len_to_snd < ctx->max_dequeue)
529  svm_fifo_overwrite_head (s->server_tx_fifo, (u8 *) & ctx->hdr,
530  sizeof (session_dgram_pre_hdr_t));
531  /* More data needs to be read */
532  else if (svm_fifo_max_dequeue (s->server_tx_fifo) > 0)
533  if (svm_fifo_set_event (s->server_tx_fifo))
534  vec_add1 (smm->pending_event_vector[thread_index], *e);
535  }
536  return SESSION_TX_OK;
537 }
538 
539 int
541  session_fifo_event_t * e,
542  stream_session_t * s, int *n_tx_pkts)
543 {
544  return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 1);
545 }
546 
547 int
549  session_fifo_event_t * e,
550  stream_session_t * s, int *n_tx_pkts)
551 {
552  return session_tx_fifo_read_and_snd_i (vm, node, e, s, n_tx_pkts, 0);
553 }
554 
555 int
557  vlib_node_runtime_t * node,
558  session_fifo_event_t * e,
559  stream_session_t * s, int *n_tx_pkts)
560 {
561  application_t *app;
562  app = application_get (s->opaque);
563  svm_fifo_unset_event (s->server_tx_fifo);
564  return app->cb_fns.builtin_app_tx_callback (s);
565 }
566 
568 session_event_get_session (session_fifo_event_t * e, u8 thread_index)
569 {
570  return session_get_if_valid (e->fifo->master_session_index, thread_index);
571 }
572 
573 static uword
575  vlib_frame_t * frame)
576 {
578  session_fifo_event_t *pending_events, *e;
579  session_fifo_event_t *fifo_events;
580  u32 n_to_dequeue, n_events;
581  svm_queue_t *q;
582  application_t *app;
583  int n_tx_packets = 0;
584  u32 thread_index = vm->thread_index;
585  int i, rv;
586  f64 now = vlib_time_now (vm);
587  void (*fp) (void *);
588 
589  SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, thread_index);
590 
591  /*
592  * Update transport time
593  */
594  transport_update_time (now, thread_index);
595 
596  /*
597  * Get vpp queue events
598  */
599  q = smm->vpp_event_queues[thread_index];
600  if (PREDICT_FALSE (q == 0))
601  return 0;
602 
603  fifo_events = smm->free_event_vector[thread_index];
604 
605  /* min number of events we can dequeue without blocking */
606  n_to_dequeue = q->cursize;
607  pending_events = smm->pending_event_vector[thread_index];
608 
609  if (!n_to_dequeue && !vec_len (pending_events)
610  && !vec_len (smm->pending_disconnects[thread_index]))
611  return 0;
612 
613  SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
614 
615  /*
616  * If we didn't manage to process previous events try going
617  * over them again without dequeuing new ones.
618  * XXX: Handle senders to sessions that can't keep up
619  */
620  if (0 && vec_len (pending_events) >= 100)
621  {
622  clib_warning ("too many fifo events unsolved");
623  goto skip_dequeue;
624  }
625 
626  /* See you in the next life, don't be late
627  * XXX: we may need priorities here
628  */
629  if (pthread_mutex_trylock (&q->mutex))
630  return 0;
631 
632  for (i = 0; i < n_to_dequeue; i++)
633  {
634  vec_add2 (fifo_events, e, 1);
635  svm_queue_sub_raw (q, (u8 *) e);
636  }
637 
638  /* The other side of the connection is not polling */
639  if (q->cursize < (q->maxsize / 8))
640  (void) pthread_cond_broadcast (&q->condvar);
641  pthread_mutex_unlock (&q->mutex);
642 
643  vec_append (fifo_events, pending_events);
644  vec_append (fifo_events, smm->pending_disconnects[thread_index]);
645 
646  _vec_len (pending_events) = 0;
647  smm->pending_event_vector[thread_index] = pending_events;
648  _vec_len (smm->pending_disconnects[thread_index]) = 0;
649 
650 skip_dequeue:
651  n_events = vec_len (fifo_events);
652  for (i = 0; i < n_events; i++)
653  {
654  stream_session_t *s; /* $$$ prefetch 1 ahead maybe */
655  session_fifo_event_t *e;
656  u32 to_dequeue;
657 
658  e = &fifo_events[i];
659  switch (e->event_type)
660  {
661  case FIFO_EVENT_APP_TX:
662  /* Don't try to send more that one frame per dispatch cycle */
663  if (n_tx_packets == VLIB_FRAME_SIZE)
664  {
665  vec_add1 (smm->pending_event_vector[thread_index], *e);
666  break;
667  }
668 
669  s = session_event_get_session (e, thread_index);
670  if (PREDICT_FALSE (!s))
671  {
672  clib_warning ("It's dead, Jim!");
673  continue;
674  }
675  to_dequeue = svm_fifo_max_dequeue (s->server_tx_fifo);
676 
677  /* Spray packets in per session type frames, since they go to
678  * different nodes */
679  rv = (smm->session_tx_fns[s->session_type]) (vm, node, e, s,
680  &n_tx_packets);
681  if (PREDICT_TRUE (rv == SESSION_TX_OK))
682  {
683  /* Notify app there's tx space if not polling */
684  if (PREDICT_FALSE (to_dequeue == s->server_tx_fifo->nitems
685  && !svm_fifo_has_event (s->server_tx_fifo)))
687  }
688  else if (PREDICT_FALSE (rv == SESSION_TX_NO_BUFFERS))
689  {
691  SESSION_QUEUE_ERROR_NO_BUFFER, 1);
692  continue;
693  }
694  break;
696  /* Make sure stream disconnects run after the pending list is
697  * drained */
698  s = session_get_from_handle (e->session_handle);
699  if (!e->postponed)
700  {
701  e->postponed = 1;
702  vec_add1 (smm->pending_disconnects[thread_index], *e);
703  continue;
704  }
705  /* If tx queue is still not empty, wait */
706  if (svm_fifo_max_dequeue (s->server_tx_fifo))
707  {
708  vec_add1 (smm->pending_disconnects[thread_index], *e);
709  continue;
710  }
711 
713  break;
715  s = session_event_get_session (e, thread_index);
716  if (PREDICT_FALSE (!s))
717  continue;
718  svm_fifo_unset_event (s->server_rx_fifo);
719  app = application_get (s->app_index);
720  app->cb_fns.builtin_app_rx_callback (s);
721  break;
722  case FIFO_EVENT_RPC:
723  fp = e->rpc_args.fp;
724  (*fp) (e->rpc_args.arg);
725  break;
726 
727  default:
728  clib_warning ("unhandled event type %d", e->event_type);
729  }
730  }
731 
732  _vec_len (fifo_events) = 0;
733  smm->free_event_vector[thread_index] = fifo_events;
734 
736  SESSION_QUEUE_ERROR_TX, n_tx_packets);
737 
738  SESSION_EVT_DBG (SESSION_EVT_DISPATCH_END, smm, thread_index);
739 
740  return n_tx_packets;
741 }
742 
743 /* *INDENT-OFF* */
745 {
746  .function = session_queue_node_fn,
747  .name = "session-queue",
748  .format_trace = format_session_queue_trace,
749  .type = VLIB_NODE_TYPE_INPUT,
751  .error_strings = session_queue_error_strings,
752  .state = VLIB_NODE_STATE_DISABLED,
753 };
754 /* *INDENT-ON* */
755 
756 void
758 {
761  u32 my_thread_index = vm->thread_index;
762  session_fifo_event_t _e, *e = &_e;
763  stream_session_t *s0;
764  int i, index;
765  i8 *headp;
766 
767  svm_queue_t *q;
768  q = smm->vpp_event_queues[my_thread_index];
769 
770  index = q->head;
771 
772  for (i = 0; i < q->cursize; i++)
773  {
774  headp = (i8 *) (&q->data[0] + q->elsize * index);
775  clib_memcpy (e, headp, q->elsize);
776 
777  switch (e->event_type)
778  {
779  case FIFO_EVENT_APP_TX:
780  s0 = session_event_get_session (e, my_thread_index);
781  fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
782  break;
783 
785  s0 = session_get_from_handle (e->session_handle);
786  fformat (stdout, "[%04d] disconnect session %d\n", i,
787  s0->session_index);
788  break;
789 
791  s0 = session_event_get_session (e, my_thread_index);
792  fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
793  break;
794 
795  case FIFO_EVENT_RPC:
796  fformat (stdout, "[%04d] RPC call %llx with %llx\n",
797  i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
798  break;
799 
800  default:
801  fformat (stdout, "[%04d] unhandled event type %d\n",
802  i, e->event_type);
803  break;
804  }
805 
806  index++;
807 
808  if (index == q->maxsize)
809  index = 0;
810  }
811 }
812 
813 static u8
814 session_node_cmp_event (session_fifo_event_t * e, svm_fifo_t * f)
815 {
816  stream_session_t *s;
817  switch (e->event_type)
818  {
819  case FIFO_EVENT_APP_RX:
820  case FIFO_EVENT_APP_TX:
822  if (e->fifo == f)
823  return 1;
824  break;
826  break;
827  case FIFO_EVENT_RPC:
828  s = session_get_from_handle (e->session_handle);
829  if (!s)
830  {
831  clib_warning ("session has event but doesn't exist!");
832  break;
833  }
834  if (s->server_rx_fifo == f || s->server_tx_fifo == f)
835  return 1;
836  break;
837  default:
838  break;
839  }
840  return 0;
841 }
842 
843 u8
844 session_node_lookup_fifo_event (svm_fifo_t * f, session_fifo_event_t * e)
845 {
847  svm_queue_t *q;
848  session_fifo_event_t *pending_event_vector, *evt;
849  int i, index, found = 0;
850  i8 *headp;
851  u8 thread_index;
852 
853  ASSERT (e);
854  thread_index = f->master_thread_index;
855  /*
856  * Search evt queue
857  */
858  q = smm->vpp_event_queues[thread_index];
859  index = q->head;
860  for (i = 0; i < q->cursize; i++)
861  {
862  headp = (i8 *) (&q->data[0] + q->elsize * index);
863  clib_memcpy (e, headp, q->elsize);
864  found = session_node_cmp_event (e, f);
865  if (found)
866  return 1;
867  if (++index == q->maxsize)
868  index = 0;
869  }
870  /*
871  * Search pending events vector
872  */
873  pending_event_vector = smm->pending_event_vector[thread_index];
874  vec_foreach (evt, pending_event_vector)
875  {
876  found = session_node_cmp_event (evt, f);
877  if (found)
878  {
879  clib_memcpy (e, evt, sizeof (*evt));
880  break;
881  }
882  }
883  return found;
884 }
885 
886 static clib_error_t *
888 {
889  if (vec_len (vlib_mains) < 2)
890  return 0;
891 
892  /*
893  * Shut off (especially) worker-thread session nodes.
894  * Otherwise, vpp can crash as the main thread unmaps the
895  * API segment.
896  */
898  session_node_enable_disable (0 /* is_enable */ );
900  return 0;
901 }
902 
904 
905 static uword
907  vlib_frame_t * f)
908 {
909  f64 now, timeout = 1.0;
910  uword *event_data = 0;
911  uword event_type;
912 
913  while (1)
914  {
916  now = vlib_time_now (vm);
917  event_type = vlib_process_get_events (vm, (uword **) & event_data);
918 
919  switch (event_type)
920  {
922  /* Flush the frames by updating all transports times */
923  transport_update_time (now, 0);
924  break;
926  timeout = 100000.0;
927  break;
928  case ~0:
929  /* Timed out. Update time for all transports to trigger all
930  * outstanding retransmits. */
931  transport_update_time (now, 0);
932  break;
933  }
934  vec_reset_length (event_data);
935  }
936  return 0;
937 }
938 
939 /* *INDENT-OFF* */
941 {
942  .function = session_queue_process,
943  .type = VLIB_NODE_TYPE_PROCESS,
944  .name = "session-queue-process",
945  .state = VLIB_NODE_STATE_DISABLED,
946 };
947 /* *INDENT-ON* */
948 
949 
950 /*
951  * fd.io coding-style-patch-verification: ON
952  *
953  * Local Variables:
954  * eval: (c-set-style "gnu")
955  * End:
956  */
vlib_main_t vlib_global_main
Definition: main.c:1644
static void session_tx_trace_frame(vlib_main_t *vm, vlib_node_runtime_t *node, u32 next_index, u32 *to_next, u16 n_segs, stream_session_t *s, u32 n_trace)
Definition: session_node.c:77
#define clib_min(x, y)
Definition: clib.h:289
#define CLIB_UNUSED(x)
Definition: clib.h:79
void ip_copy(ip46_address_t *dst, ip46_address_t *src, u8 is_ip4)
Definition: ip.c:81
#define SESSION_Q_PROCESS_FLUSH_FRAMES
Definition: session.h:259
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:143
static f64 vlib_process_wait_for_event_or_clock(vlib_main_t *vm, f64 dt)
Suspend a cooperative multi-tasking thread Waits for an event, or for the indicated number of seconds...
Definition: node_funcs.h:699
#define SESSION_CONN_HDR_LEN
Definition: session.h:126
struct _transport_connection transport_connection_t
static int session_output_try_get_buffers(vlib_main_t *vm, session_manager_main_t *smm, u32 thread_index, u16 *n_bufs, u32 wanted)
Definition: session_node.c:173
int session_tx_fifo_dequeue_internal(vlib_main_t *vm, vlib_node_runtime_t *node, session_fifo_event_t *e, stream_session_t *s, int *n_tx_pkts)
Definition: session_node.c:556
#define PREDICT_TRUE(x)
Definition: clib.h:106
unsigned long u64
Definition: types.h:89
session_manager_main_t session_manager_main
Definition: session.c:27
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:228
u32 thread_index
Definition: main.h:179
#define vec_add1(V, E)
Add 1 element to end of vector (unspecified alignment).
Definition: vec.h:523
void svm_fifo_overwrite_head(svm_fifo_t *f, u8 *data, u32 len)
Definition: svm_fifo.c:623
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:562
int i
static clib_error_t * session_queue_exit(vlib_main_t *vm)
Definition: session_node.c:887
static uword session_queue_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: session_node.c:574
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:448
static stream_session_t * session_get_from_handle(session_handle_t handle)
Definition: session.h:336
void session_node_enable_disable(u8 is_en)
Definition: session.c:1436
vlib_main_t ** vlib_mains
Definition: buffer.c:303
unsigned char u8
Definition: types.h:56
static void session_tx_fill_buffer(vlib_main_t *vm, session_tx_context_t *ctx, vlib_buffer_t *b, u16 *n_bufs, u8 peek_data)
Definition: session_node.c:188
void transport_update_time(f64 time_now, u8 thread_index)
Definition: transport.c:380
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
double f64
Definition: types.h:142
#define vlib_worker_thread_barrier_sync(X)
Definition: threads.h:212
struct _svm_fifo svm_fifo_t
static void vlib_trace_buffer(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, vlib_buffer_t *b, int follow_chain)
Definition: trace_funcs.h:104
void dump_thread_0_event_queue(void)
Definition: session_node.c:757
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:104
static uword vlib_process_get_events(vlib_main_t *vm, uword **data_vector)
Return the first event type which has occurred and a vector of per-event data of that type...
Definition: node_funcs.h:542
#define always_inline
Definition: clib.h:92
static u32 svm_fifo_max_dequeue(svm_fifo_t *f)
Definition: svm_fifo.h:105
int session_dequeue_notify(stream_session_t *s)
Definition: session.c:505
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:184
stream_session_t * s
Definition: session.h:134
unsigned int u32
Definition: types.h:88
struct _stream_session_t stream_session_t
#define VLIB_FRAME_SIZE
Definition: node.h:364
static char * session_queue_error_strings[]
Definition: session_node.c:62
transport_proto_vft_t * transport_protocol_get_vft(transport_proto_t transport_proto)
Get transport virtual function table.
Definition: transport.c:191
static transport_proto_t session_get_transport_proto(stream_session_t *s)
Definition: session.h:373
static session_manager_main_t * vnet_get_session_manager_main()
Definition: session.h:266
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:108
static void * vlib_buffer_make_headroom(vlib_buffer_t *b, u8 size)
Make head room, typically for packet headers.
Definition: buffer.h:314
#define MAX_HDRS_LEN
Definition: session.h:31
session_queue_error_t
Definition: session_node.c:54
transport_proto_vft_t * transport_vft
Definition: session.h:135
unsigned short u16
Definition: types.h:57
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:202
#define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX
Definition: buffer.h:439
static uword session_queue_process(vlib_main_t *vm, vlib_node_runtime_t *rt, vlib_frame_t *f)
Definition: session_node.c:906
#define PREDICT_FALSE(x)
Definition: clib.h:105
static void svm_fifo_unset_event(svm_fifo_t *f)
Unsets fifo event flag.
Definition: svm_fifo.h:144
signed char i8
Definition: types.h:45
static stream_session_t * session_get_if_valid(u64 si, u32 thread_index)
Definition: session.h:297
u32 node_index
Node index.
Definition: node.h:473
#define vlib_validate_buffer_enqueue_x2(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, next0, next1)
Finish enqueueing two buffers forward in the graph.
Definition: buffer_node.h:70
struct _session_manager_main session_manager_main_t
Definition: session.h:153
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:364
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:135
word fformat(FILE *f, char *fmt,...)
Definition: format.c:453
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1168
static u8 svm_fifo_set_event(svm_fifo_t *f)
Sets fifo event flag.
Definition: svm_fifo.h:134
The fine-grained event logger allows lightweight, thread-safe event logging at minimum cost...
#define SESSION_EVT_DBG(_evt, _args...)
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:153
static void session_tx_fifo_chain_tail(vlib_main_t *vm, session_tx_context_t *ctx, vlib_buffer_t *b, u16 *n_bufs, u8 peek_data)
Definition: session_node.c:97
vlib_main_t * vm
Definition: buffer.c:294
#define VLIB_MAIN_LOOP_EXIT_FUNCTION(x)
Definition: init.h:161
#define clib_warning(format, args...)
Definition: error.h:59
#define clib_memcpy(a, b, c)
Definition: string.h:75
struct _application application_t
#define ARRAY_LEN(x)
Definition: clib.h:59
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:454
#define SESSION_Q_PROCESS_STOP
Definition: session.h:260
int session_tx_fifo_dequeue_and_snd(vlib_main_t *vm, vlib_node_runtime_t *node, session_fifo_event_t *e, stream_session_t *s, int *n_tx_pkts)
Definition: session_node.c:548
#define foreach_session_queue_error
Definition: session_node.c:49
#define ASSERT(truth)
long ctx[MAX_CONNS]
Definition: main.c:126
static u8 session_node_cmp_event(session_fifo_event_t *e, svm_fifo_t *f)
Definition: session_node.c:814
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:126
#define vec_append(v1, v2)
Append v2 after v1.
Definition: vec.h:820
int svm_queue_sub_raw(svm_queue_t *q, u8 *elem)
Definition: queue.c:404
static stream_session_t * session_event_get_session(session_fifo_event_t *e, u8 thread_index)
Definition: session_node.c:568
static u8 * format_session_queue_trace(u8 *s, va_list *args)
Definition: session_node.c:36
session_dgram_hdr_t hdr
Definition: session.h:149
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:152
static void session_tx_set_dequeue_params(vlib_main_t *vm, session_tx_context_t *ctx, u32 max_segs, u8 peek_data)
Definition: session_node.c:304
struct _vlib_node_registration vlib_node_registration_t
int svm_fifo_dequeue_drop(svm_fifo_t *f, u32 max_bytes)
Definition: svm_fifo.c:792
static u8 session_tx_not_ready(stream_session_t *s, u8 peek_data)
Definition: session_node.c:269
enum _transport_proto transport_proto_t
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:546
application_t * application_get(u32 index)
Definition: application.c:386
u64 uword
Definition: types.h:112
static u32 vlib_buffer_free_list_buffer_size(vlib_main_t *vm, vlib_buffer_free_list_index_t index)
Definition: buffer_funcs.h:676
vlib_node_registration_t session_queue_node
(constructor) VLIB_REGISTER_NODE (session_queue_node)
Definition: session_node.c:26
struct _svm_queue svm_queue_t
struct clib_bihash_value offset
template key/value backing page structure
static transport_connection_t * session_tx_get_transport(session_tx_context_t *ctx, u8 peek_data)
Definition: session_node.c:284
transport_connection_t * tc
Definition: session.h:136
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1545
#define vec_foreach(var, vec)
Vector iterator.
static int svm_fifo_has_event(svm_fifo_t *f)
Definition: svm_fifo.h:117
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:159
u8 session_node_lookup_fifo_event(svm_fifo_t *f, session_fifo_event_t *e)
Definition: session_node.c:844
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:62
u32 flags
buffer flags: VLIB_BUFFER_FREE_LIST_INDEX_MASK: bits used to store free list index, VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:111
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:490
vlib_node_registration_t session_queue_process_node
(constructor) VLIB_REGISTER_NODE (session_queue_process_node)
Definition: session_node.c:940
int svm_fifo_peek(svm_fifo_t *f, u32 relative_offset, u32 max_bytes, u8 *copy_here)
Definition: svm_fifo.c:776
int svm_fifo_dequeue_nowait(svm_fifo_t *f, u32 max_bytes, u8 *copy_here)
Definition: svm_fifo.c:710
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:57
int session_tx_fifo_peek_and_snd(vlib_main_t *vm, vlib_node_runtime_t *node, session_fifo_event_t *e, stream_session_t *s, int *n_tx_pkts)
Definition: session_node.c:540
void stream_session_disconnect_transport(stream_session_t *s)
Notify transport the session can be disconnected.
Definition: session.c:1134
static int session_tx_fifo_read_and_snd_i(vlib_main_t *vm, vlib_node_runtime_t *node, session_fifo_event_t *e, stream_session_t *s, int *n_tx_packets, u8 peek_data)
Definition: session_node.c:371