FD.io VPP  v18.07-rc0-415-g6c78436
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 static void
70  u32 next_index, u32 * to_next, u16 n_segs,
71  stream_session_t * s, u32 n_trace)
72 {
74  vlib_buffer_t *b;
75  int i;
76 
77  for (i = 0; i < clib_min (n_trace, n_segs); i++)
78  {
79  b = vlib_get_buffer (vm, to_next[i - n_segs]);
80  vlib_trace_buffer (vm, node, next_index, b, 1 /* follow_chain */ );
81  t = vlib_add_trace (vm, node, b, sizeof (*t));
82  t->session_index = s->session_index;
83  t->server_thread_index = s->thread_index;
84  }
85  vlib_set_trace_count (vm, node, n_trace - i);
86 }
87 
88 always_inline void
90  vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
91 {
93  vlib_buffer_t *chain_b, *prev_b;
94  u32 chain_bi0, to_deq, left_from_seg;
95  u16 len_to_deq, n_bytes_read;
96  u8 *data, j;
97 
98  b->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
100 
101  chain_b = b;
102  left_from_seg = clib_min (ctx->snd_mss - b->current_length,
103  ctx->left_to_snd);
104  to_deq = left_from_seg;
105  for (j = 1; j < ctx->n_bufs_per_seg; j++)
106  {
107  prev_b = chain_b;
108  len_to_deq = clib_min (to_deq, ctx->deq_per_buf);
109 
110  *n_bufs -= 1;
111  chain_bi0 = smm->tx_buffers[ctx->s->thread_index][*n_bufs];
112  _vec_len (smm->tx_buffers[ctx->s->thread_index]) = *n_bufs;
113 
114  chain_b = vlib_get_buffer (vm, chain_bi0);
115  chain_b->current_data = 0;
116  data = vlib_buffer_get_current (chain_b);
117  if (peek_data)
118  {
119  n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo,
120  ctx->tx_offset, len_to_deq, data);
121  ctx->tx_offset += n_bytes_read;
122  }
123  else
124  {
125  if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
126  {
127  svm_fifo_t *f = ctx->s->server_tx_fifo;
128  session_dgram_hdr_t *hdr = &ctx->hdr;
129  u16 deq_now;
130  deq_now = clib_min (hdr->data_length - hdr->data_offset,
131  len_to_deq);
132  n_bytes_read = svm_fifo_peek (f, hdr->data_offset, deq_now,
133  data);
134  ASSERT (n_bytes_read > 0);
135 
136  hdr->data_offset += n_bytes_read;
137  if (hdr->data_offset == hdr->data_length)
138  svm_fifo_dequeue_drop (f, hdr->data_length);
139  }
140  else
141  n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
142  len_to_deq, data);
143  }
144  ASSERT (n_bytes_read == len_to_deq);
145  chain_b->current_length = n_bytes_read;
147 
148  /* update previous buffer */
149  prev_b->next_buffer = chain_bi0;
150  prev_b->flags |= VLIB_BUFFER_NEXT_PRESENT;
151 
152  /* update current buffer */
153  chain_b->next_buffer = 0;
154 
155  to_deq -= n_bytes_read;
156  if (to_deq == 0)
157  break;
158  }
159  ASSERT (to_deq == 0
160  && b->total_length_not_including_first_buffer == left_from_seg);
161  ctx->left_to_snd -= left_from_seg;
162 }
163 
164 always_inline int
167  u32 thread_index, u16 * n_bufs, u32 wanted)
168 {
169  u32 n_alloc;
170  vec_validate_aligned (smm->tx_buffers[thread_index], wanted - 1,
172  n_alloc = vlib_buffer_alloc (vm, &smm->tx_buffers[thread_index][*n_bufs],
173  wanted - *n_bufs);
174  *n_bufs += n_alloc;
175  _vec_len (smm->tx_buffers[thread_index]) = *n_bufs;
176  return n_alloc;
177 }
178 
179 always_inline void
181  vlib_buffer_t * b, u16 * n_bufs, u8 peek_data)
182 {
183  u32 len_to_deq;
184  u8 *data0;
185  int n_bytes_read;
186 
187  /*
188  * Start with the first buffer in chain
189  */
190  b->error = 0;
191  b->flags = VNET_BUFFER_F_LOCALLY_ORIGINATED;
192  b->current_data = 0;
194 
196  len_to_deq = clib_min (ctx->left_to_snd, ctx->deq_per_first_buf);
197 
198  if (peek_data)
199  {
200  n_bytes_read = svm_fifo_peek (ctx->s->server_tx_fifo, ctx->tx_offset,
201  len_to_deq, data0);
202  ASSERT (n_bytes_read > 0);
203  /* Keep track of progress locally, transport is also supposed to
204  * increment it independently when pushing the header */
205  ctx->tx_offset += n_bytes_read;
206  }
207  else
208  {
209  if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
210  {
211  session_dgram_hdr_t *hdr = &ctx->hdr;
212  svm_fifo_t *f = ctx->s->server_tx_fifo;
213  u16 deq_now;
214  u32 offset;
215 
216  ASSERT (hdr->data_length > hdr->data_offset);
217  deq_now = clib_min (hdr->data_length - hdr->data_offset,
218  len_to_deq);
219  offset = hdr->data_offset + SESSION_CONN_HDR_LEN;
220  n_bytes_read = svm_fifo_peek (f, offset, deq_now, data0);
221  ASSERT (n_bytes_read > 0);
222 
223  if (ctx->s->session_state == SESSION_STATE_LISTENING)
224  {
225  ip_copy (&ctx->tc->rmt_ip, &hdr->rmt_ip, ctx->tc->is_ip4);
226  ctx->tc->rmt_port = hdr->rmt_port;
227  }
228  hdr->data_offset += n_bytes_read;
229  if (hdr->data_offset == hdr->data_length)
230  {
231  offset = hdr->data_length + SESSION_CONN_HDR_LEN;
232  svm_fifo_dequeue_drop (f, offset);
233  }
234  }
235  else
236  {
237  n_bytes_read = svm_fifo_dequeue_nowait (ctx->s->server_tx_fifo,
238  len_to_deq, data0);
239  ASSERT (n_bytes_read > 0);
240  }
241  }
242  b->current_length = n_bytes_read;
243  ctx->left_to_snd -= n_bytes_read;
244 
245  /*
246  * Fill in the remaining buffers in the chain, if any
247  */
248  if (PREDICT_FALSE (ctx->n_bufs_per_seg > 1 && ctx->left_to_snd))
249  session_tx_fifo_chain_tail (vm, ctx, b, n_bufs, peek_data);
250 
251  /* *INDENT-OFF* */
252  SESSION_EVT_DBG(SESSION_EVT_DEQ, ctx->s, ({
253  ed->data[0] = FIFO_EVENT_APP_TX;
254  ed->data[1] = ctx->max_dequeue;
255  ed->data[2] = len_to_deq;
256  ed->data[3] = ctx->left_to_snd;
257  }));
258  /* *INDENT-ON* */
259 }
260 
263 {
264  if (peek_data)
265  {
266  /* Can retransmit for closed sessions but can't send new data if
267  * session is not ready or closed */
268  if (s->session_state < SESSION_STATE_READY)
269  return 1;
270  if (s->session_state == SESSION_STATE_CLOSED)
271  return 2;
272  }
273  return 0;
274 }
275 
278 {
279  if (peek_data)
280  {
281  return ctx->transport_vft->get_connection (ctx->s->connection_index,
282  ctx->s->thread_index);
283  }
284  else
285  {
286  if (ctx->s->session_state == SESSION_STATE_LISTENING)
287  return ctx->transport_vft->get_listener (ctx->s->connection_index);
288  else
289  {
290  return ctx->transport_vft->get_connection (ctx->s->connection_index,
291  ctx->s->thread_index);
292  }
293  }
294 }
295 
296 always_inline void
298  u32 max_segs, u8 peek_data)
299 {
300  u32 n_bytes_per_buf, n_bytes_per_seg;
301  ctx->max_dequeue = svm_fifo_max_dequeue (ctx->s->server_tx_fifo);
302  if (peek_data)
303  {
304  /* Offset in rx fifo from where to peek data */
305  ctx->tx_offset = ctx->transport_vft->tx_fifo_offset (ctx->tc);
306  if (PREDICT_FALSE (ctx->tx_offset >= ctx->max_dequeue))
307  {
308  ctx->max_len_to_snd = 0;
309  return;
310  }
311  ctx->max_dequeue -= ctx->tx_offset;
312  }
313  else
314  {
315  if (ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
316  {
317  if (ctx->max_dequeue <= sizeof (ctx->hdr))
318  {
319  ctx->max_len_to_snd = 0;
320  return;
321  }
322  svm_fifo_peek (ctx->s->server_tx_fifo, 0, sizeof (ctx->hdr),
323  (u8 *) & ctx->hdr);
324  ASSERT (ctx->hdr.data_length > ctx->hdr.data_offset);
325  ctx->max_dequeue = ctx->hdr.data_length - ctx->hdr.data_offset;
326  }
327  }
328  ASSERT (ctx->max_dequeue > 0);
329 
330  /* Ensure we're not writing more than transport window allows */
331  if (ctx->max_dequeue < ctx->snd_space)
332  {
333  /* Constrained by tx queue. Try to send only fully formed segments */
334  ctx->max_len_to_snd =
335  (ctx->max_dequeue > ctx->snd_mss) ?
336  ctx->max_dequeue - ctx->max_dequeue % ctx->snd_mss : ctx->max_dequeue;
337  /* TODO Nagle ? */
338  }
339  else
340  {
341  /* Expectation is that snd_space0 is already a multiple of snd_mss */
342  ctx->max_len_to_snd = ctx->snd_space;
343  }
344 
345  /* Check if we're tx constrained by the node */
346  ctx->n_segs_per_evt = ceil ((f64) ctx->max_len_to_snd / ctx->snd_mss);
347  if (ctx->n_segs_per_evt > max_segs)
348  {
349  ctx->n_segs_per_evt = max_segs;
350  ctx->max_len_to_snd = max_segs * ctx->snd_mss;
351  }
352 
353  n_bytes_per_buf = vlib_buffer_free_list_buffer_size (vm,
355  ASSERT (n_bytes_per_buf > MAX_HDRS_LEN);
356  n_bytes_per_seg = MAX_HDRS_LEN + ctx->snd_mss;
357  ctx->n_bufs_per_seg = ceil ((f64) n_bytes_per_seg / n_bytes_per_buf);
358  ctx->deq_per_buf = clib_min (ctx->snd_mss, n_bytes_per_buf);
359  ctx->deq_per_first_buf = clib_min (ctx->snd_mss,
360  n_bytes_per_buf - MAX_HDRS_LEN);
361 }
362 
363 always_inline int
365  session_fifo_event_t * e,
366  stream_session_t * s, int *n_tx_packets,
367  u8 peek_data)
368 {
369  u32 next_index, next0, next1, *to_next, n_left_to_next;
370  u32 n_trace = vlib_get_trace_count (vm, node), n_bufs_needed = 0;
371  u32 thread_index = s->thread_index, n_left, pbi;
373  session_tx_context_t *ctx = &smm->ctx[thread_index];
375  vlib_buffer_t *pb;
376  u16 n_bufs, rv;
377 
378  if (PREDICT_FALSE ((rv = session_tx_not_ready (s, peek_data))))
379  {
380  if (rv < 2)
381  vec_add1 (smm->pending_event_vector[thread_index], *e);
382  return 0;
383  }
384 
385  next_index = smm->session_type_to_next[s->session_type];
386  next0 = next1 = next_index;
387 
389  ctx->s = s;
391  ctx->tc = session_tx_get_transport (ctx, peek_data);
392  ctx->snd_mss = ctx->transport_vft->send_mss (ctx->tc);
393  ctx->snd_space = ctx->transport_vft->send_space (ctx->tc);
394  if (ctx->snd_space == 0 || ctx->snd_mss == 0)
395  {
396  vec_add1 (smm->pending_event_vector[thread_index], *e);
397  return 0;
398  }
399 
400  /* Allow enqueuing of a new event */
401  svm_fifo_unset_event (s->server_tx_fifo);
402 
403  /* Check how much we can pull. */
404  session_tx_set_dequeue_params (vm, ctx, VLIB_FRAME_SIZE - *n_tx_packets,
405  peek_data);
406 
407  if (PREDICT_FALSE (!ctx->max_len_to_snd))
408  return 0;
409 
410  n_bufs = vec_len (smm->tx_buffers[thread_index]);
411  n_bufs_needed = ctx->n_segs_per_evt * ctx->n_bufs_per_seg;
412 
413  /*
414  * Make sure we have at least one full frame of buffers ready
415  */
416  if (n_bufs < n_bufs_needed)
417  {
418  session_output_try_get_buffers (vm, smm, thread_index, &n_bufs,
420  if (PREDICT_FALSE (n_bufs < n_bufs_needed))
421  {
422  vec_add1 (smm->pending_event_vector[thread_index], *e);
423  return -1;
424  }
425  }
426 
427  /*
428  * Write until we fill up a frame
429  */
430  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
431  if (PREDICT_FALSE (ctx->n_segs_per_evt > n_left_to_next))
432  {
433  ctx->n_segs_per_evt = n_left_to_next;
434  ctx->max_len_to_snd = ctx->snd_mss * n_left_to_next;
435  }
436  ctx->left_to_snd = ctx->max_len_to_snd;
437  n_left = ctx->n_segs_per_evt;
438 
439  while (n_left >= 4)
440  {
441  vlib_buffer_t *b0, *b1;
442  u32 bi0, bi1;
443 
444  pbi = smm->tx_buffers[thread_index][n_bufs - 3];
445  pb = vlib_get_buffer (vm, pbi);
446  vlib_prefetch_buffer_header (pb, STORE);
447  pbi = smm->tx_buffers[thread_index][n_bufs - 4];
448  pb = vlib_get_buffer (vm, pbi);
449  vlib_prefetch_buffer_header (pb, STORE);
450 
451  to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
452  to_next[1] = bi1 = smm->tx_buffers[thread_index][--n_bufs];
453 
454  b0 = vlib_get_buffer (vm, bi0);
455  b1 = vlib_get_buffer (vm, bi1);
456 
457  session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
458  session_tx_fill_buffer (vm, ctx, b1, &n_bufs, peek_data);
459 
460  ctx->transport_vft->push_header (ctx->tc, b0);
461  ctx->transport_vft->push_header (ctx->tc, b1);
462 
463  to_next += 2;
464  n_left_to_next -= 2;
465  n_left -= 2;
466 
469 
470  vlib_validate_buffer_enqueue_x2 (vm, node, next_index, to_next,
471  n_left_to_next, bi0, bi1, next0,
472  next1);
473  }
474  while (n_left)
475  {
476  vlib_buffer_t *b0;
477  u32 bi0;
478 
479  ASSERT (n_bufs >= 1);
480  to_next[0] = bi0 = smm->tx_buffers[thread_index][--n_bufs];
481  b0 = vlib_get_buffer (vm, bi0);
482  session_tx_fill_buffer (vm, ctx, b0, &n_bufs, peek_data);
483 
484  /* Ask transport to push header after current_length and
485  * total_length_not_including_first_buffer are updated */
486  ctx->transport_vft->push_header (ctx->tc, b0);
487 
488  to_next += 1;
489  n_left_to_next -= 1;
490  n_left -= 1;
491 
493 
494  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
495  n_left_to_next, bi0, next0);
496  }
497 
498  if (PREDICT_FALSE (n_trace > 0))
499  session_tx_trace_frame (vm, node, next_index, to_next,
500  ctx->n_segs_per_evt, s, n_trace);
501 
502  _vec_len (smm->tx_buffers[thread_index]) = n_bufs;
503  *n_tx_packets += ctx->n_segs_per_evt;
504  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
505 
506  /* If we couldn't dequeue all bytes mark as partially read */
507  ASSERT (ctx->left_to_snd == 0);
508  if (ctx->max_len_to_snd < ctx->max_dequeue)
509  if (svm_fifo_set_event (s->server_tx_fifo))
510  vec_add1 (smm->pending_event_vector[thread_index], *e);
511 
512  if (!peek_data && ctx->transport_vft->tx_type == TRANSPORT_TX_DGRAM)
513  {
514  /* Fix dgram pre header */
515  if (ctx->max_len_to_snd < ctx->max_dequeue)
516  svm_fifo_overwrite_head (s->server_tx_fifo, (u8 *) & ctx->hdr,
517  sizeof (session_dgram_pre_hdr_t));
518  /* More data needs to be read */
519  else if (svm_fifo_max_dequeue (s->server_tx_fifo) > 0)
520  if (svm_fifo_set_event (s->server_tx_fifo))
521  vec_add1 (smm->pending_event_vector[thread_index], *e);
522  }
523  return 0;
524 }
525 
526 int
528  session_fifo_event_t * e0,
529  stream_session_t * s0, int *n_tx_pkts)
530 {
531  return session_tx_fifo_read_and_snd_i (vm, node, e0, s0, n_tx_pkts, 1);
532 }
533 
534 int
536  session_fifo_event_t * e0,
537  stream_session_t * s0, int *n_tx_pkts)
538 {
539  return session_tx_fifo_read_and_snd_i (vm, node, e0, s0, n_tx_pkts, 0);
540 }
541 
542 int
544  vlib_node_runtime_t * node,
545  session_fifo_event_t * e0,
546  stream_session_t * s0, int *n_tx_pkts)
547 {
548  application_t *app;
549  app = application_get (s0->opaque);
550  svm_fifo_unset_event (s0->server_tx_fifo);
551  return app->cb_fns.builtin_app_tx_callback (s0);
552 }
553 
555 session_event_get_session (session_fifo_event_t * e, u8 thread_index)
556 {
557  return session_get_if_valid (e->fifo->master_session_index, thread_index);
558 }
559 
560 void
562 {
565  u32 my_thread_index = vm->thread_index;
566  session_fifo_event_t _e, *e = &_e;
567  stream_session_t *s0;
568  int i, index;
569  i8 *headp;
570 
571  svm_queue_t *q;
572  q = smm->vpp_event_queues[my_thread_index];
573 
574  index = q->head;
575 
576  for (i = 0; i < q->cursize; i++)
577  {
578  headp = (i8 *) (&q->data[0] + q->elsize * index);
579  clib_memcpy (e, headp, q->elsize);
580 
581  switch (e->event_type)
582  {
583  case FIFO_EVENT_APP_TX:
584  s0 = session_event_get_session (e, my_thread_index);
585  fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index);
586  break;
587 
589  s0 = session_get_from_handle (e->session_handle);
590  fformat (stdout, "[%04d] disconnect session %d\n", i,
591  s0->session_index);
592  break;
593 
595  s0 = session_event_get_session (e, my_thread_index);
596  fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index);
597  break;
598 
599  case FIFO_EVENT_RPC:
600  fformat (stdout, "[%04d] RPC call %llx with %llx\n",
601  i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg));
602  break;
603 
604  default:
605  fformat (stdout, "[%04d] unhandled event type %d\n",
606  i, e->event_type);
607  break;
608  }
609 
610  index++;
611 
612  if (index == q->maxsize)
613  index = 0;
614  }
615 }
616 
617 static u8
618 session_node_cmp_event (session_fifo_event_t * e, svm_fifo_t * f)
619 {
620  stream_session_t *s;
621  switch (e->event_type)
622  {
623  case FIFO_EVENT_APP_RX:
624  case FIFO_EVENT_APP_TX:
626  if (e->fifo == f)
627  return 1;
628  break;
630  break;
631  case FIFO_EVENT_RPC:
632  s = session_get_from_handle (e->session_handle);
633  if (!s)
634  {
635  clib_warning ("session has event but doesn't exist!");
636  break;
637  }
638  if (s->server_rx_fifo == f || s->server_tx_fifo == f)
639  return 1;
640  break;
641  default:
642  break;
643  }
644  return 0;
645 }
646 
647 u8
648 session_node_lookup_fifo_event (svm_fifo_t * f, session_fifo_event_t * e)
649 {
651  svm_queue_t *q;
652  session_fifo_event_t *pending_event_vector, *evt;
653  int i, index, found = 0;
654  i8 *headp;
655  u8 thread_index;
656 
657  ASSERT (e);
658  thread_index = f->master_thread_index;
659  /*
660  * Search evt queue
661  */
662  q = smm->vpp_event_queues[thread_index];
663  index = q->head;
664  for (i = 0; i < q->cursize; i++)
665  {
666  headp = (i8 *) (&q->data[0] + q->elsize * index);
667  clib_memcpy (e, headp, q->elsize);
668  found = session_node_cmp_event (e, f);
669  if (found)
670  return 1;
671  if (++index == q->maxsize)
672  index = 0;
673  }
674  /*
675  * Search pending events vector
676  */
677  pending_event_vector = smm->pending_event_vector[thread_index];
678  vec_foreach (evt, pending_event_vector)
679  {
680  found = session_node_cmp_event (evt, f);
681  if (found)
682  {
683  clib_memcpy (e, evt, sizeof (*evt));
684  break;
685  }
686  }
687  return found;
688 }
689 
690 static uword
692  vlib_frame_t * frame)
693 {
695  session_fifo_event_t *my_pending_event_vector, *e;
696  session_fifo_event_t *my_fifo_events;
697  u32 n_to_dequeue, n_events;
698  svm_queue_t *q;
699  application_t *app;
700  int n_tx_packets = 0;
701  u32 thread_index = vm->thread_index;
702  int i, rv;
703  f64 now = vlib_time_now (vm);
704  void (*fp) (void *);
705 
706  SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, thread_index);
707 
708  /*
709  * Update transport time
710  */
711  transport_update_time (now, thread_index);
712 
713  /*
714  * Get vpp queue events
715  */
716  q = smm->vpp_event_queues[thread_index];
717  if (PREDICT_FALSE (q == 0))
718  return 0;
719 
720  my_fifo_events = smm->free_event_vector[thread_index];
721 
722  /* min number of events we can dequeue without blocking */
723  n_to_dequeue = q->cursize;
724  my_pending_event_vector = smm->pending_event_vector[thread_index];
725 
726  if (!n_to_dequeue && !vec_len (my_pending_event_vector)
727  && !vec_len (smm->pending_disconnects[thread_index]))
728  return 0;
729 
730  SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0);
731 
732  /*
733  * If we didn't manage to process previous events try going
734  * over them again without dequeuing new ones.
735  */
736  /* XXX: Block senders to sessions that can't keep up */
737  if (0 && vec_len (my_pending_event_vector) >= 100)
738  {
739  clib_warning ("too many fifo events unsolved");
740  goto skip_dequeue;
741  }
742 
743  /* See you in the next life, don't be late */
744  if (pthread_mutex_trylock (&q->mutex))
745  return 0;
746 
747  for (i = 0; i < n_to_dequeue; i++)
748  {
749  vec_add2 (my_fifo_events, e, 1);
750  svm_queue_sub_raw (q, (u8 *) e);
751  }
752 
753  /* The other side of the connection is not polling */
754  if (q->cursize < (q->maxsize / 8))
755  (void) pthread_cond_broadcast (&q->condvar);
756  pthread_mutex_unlock (&q->mutex);
757 
758  vec_append (my_fifo_events, my_pending_event_vector);
759  vec_append (my_fifo_events, smm->pending_disconnects[thread_index]);
760 
761  _vec_len (my_pending_event_vector) = 0;
762  smm->pending_event_vector[thread_index] = my_pending_event_vector;
763  _vec_len (smm->pending_disconnects[thread_index]) = 0;
764 
765 skip_dequeue:
766  n_events = vec_len (my_fifo_events);
767  for (i = 0; i < n_events; i++)
768  {
769  stream_session_t *s0; /* $$$ prefetch 1 ahead maybe */
770  session_fifo_event_t *e0;
771 
772  e0 = &my_fifo_events[i];
773  switch (e0->event_type)
774  {
775  case FIFO_EVENT_APP_TX:
776  if (n_tx_packets == VLIB_FRAME_SIZE)
777  {
778  vec_add1 (smm->pending_event_vector[thread_index], *e0);
779  break;
780  }
781 
782  s0 = session_event_get_session (e0, thread_index);
783  if (PREDICT_FALSE (!s0))
784  {
785  clib_warning ("It's dead, Jim!");
786  continue;
787  }
788 
789  /* Spray packets in per session type frames, since they go to
790  * different nodes */
791  rv = (smm->session_tx_fns[s0->session_type]) (vm, node, e0, s0,
792  &n_tx_packets);
793  /* Out of buffers */
794  if (PREDICT_FALSE (rv < 0))
795  {
797  SESSION_QUEUE_ERROR_NO_BUFFER, 1);
798  continue;
799  }
800  break;
802  /* Make sure stream disconnects run after the pending list is
803  * drained */
804  s0 = session_get_from_handle (e0->session_handle);
805  if (!e0->postponed)
806  {
807  e0->postponed = 1;
808  vec_add1 (smm->pending_disconnects[thread_index], *e0);
809  continue;
810  }
811  /* If tx queue is still not empty, wait */
812  if (svm_fifo_max_dequeue (s0->server_tx_fifo))
813  {
814  vec_add1 (smm->pending_disconnects[thread_index], *e0);
815  continue;
816  }
817 
819  break;
821  s0 = session_event_get_session (e0, thread_index);
822  if (PREDICT_FALSE (!s0))
823  continue;
824  svm_fifo_unset_event (s0->server_rx_fifo);
825  app = application_get (s0->app_index);
826  app->cb_fns.builtin_app_rx_callback (s0);
827  break;
828  case FIFO_EVENT_RPC:
829  fp = e0->rpc_args.fp;
830  (*fp) (e0->rpc_args.arg);
831  break;
832 
833  default:
834  clib_warning ("unhandled event type %d", e0->event_type);
835  }
836  }
837 
838  _vec_len (my_fifo_events) = 0;
839  smm->free_event_vector[thread_index] = my_fifo_events;
840 
842  SESSION_QUEUE_ERROR_TX, n_tx_packets);
843 
844  SESSION_EVT_DBG (SESSION_EVT_DISPATCH_END, smm, thread_index);
845 
846  return n_tx_packets;
847 }
848 
849 /* *INDENT-OFF* */
851 {
852  .function = session_queue_node_fn,
853  .name = "session-queue",
854  .format_trace = format_session_queue_trace,
855  .type = VLIB_NODE_TYPE_INPUT,
857  .error_strings = session_queue_error_strings,
858  .state = VLIB_NODE_STATE_DISABLED,
859 };
860 /* *INDENT-ON* */
861 
862 static clib_error_t *
864 {
865  if (vec_len (vlib_mains) < 2)
866  return 0;
867 
868  /*
869  * Shut off (especially) worker-thread session nodes.
870  * Otherwise, vpp can crash as the main thread unmaps the
871  * API segment.
872  */
874  session_node_enable_disable (0 /* is_enable */ );
876  return 0;
877 }
878 
880 
881 static uword
883  vlib_frame_t * f)
884 {
885  f64 now, timeout = 1.0;
886  uword *event_data = 0;
887  uword event_type;
888 
889  while (1)
890  {
892  now = vlib_time_now (vm);
893  event_type = vlib_process_get_events (vm, (uword **) & event_data);
894 
895  switch (event_type)
896  {
898  /* Flush the frames by updating all transports times */
899  transport_update_time (now, 0);
900  break;
902  timeout = 100000.0;
903  break;
904  case ~0:
905  /* Timed out. Update time for all transports to trigger all
906  * outstanding retransmits. */
907  transport_update_time (now, 0);
908  break;
909  }
910  vec_reset_length (event_data);
911  }
912  return 0;
913 }
914 
915 /* *INDENT-OFF* */
917 {
918  .function = session_queue_process,
919  .type = VLIB_NODE_TYPE_PROCESS,
920  .name = "session-queue-process",
921  .state = VLIB_NODE_STATE_DISABLED,
922 };
923 /* *INDENT-ON* */
924 
925 
926 /*
927  * fd.io coding-style-patch-verification: ON
928  *
929  * Local Variables:
930  * eval: (c-set-style "gnu")
931  * End:
932  */
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:69
#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:165
unsigned long u64
Definition: types.h:89
int session_tx_fifo_dequeue_internal(vlib_main_t *vm, vlib_node_runtime_t *node, session_fifo_event_t *e0, stream_session_t *s0, int *n_tx_pkts)
Definition: session_node.c:543
session_manager_main_t session_manager_main
Definition: session.c:27
static f64 vlib_time_now(vlib_main_t *vm)
Definition: main.h:225
u32 thread_index
Definition: main.h:176
#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:863
static uword session_queue_node_fn(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: session_node.c:691
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:1405
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:180
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:561
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
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:184
int session_tx_fifo_dequeue_and_snd(vlib_main_t *vm, vlib_node_runtime_t *node, session_fifo_event_t *e0, stream_session_t *s0, int *n_tx_pkts)
Definition: session_node.c:535
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
int session_tx_fifo_peek_and_snd(vlib_main_t *vm, vlib_node_runtime_t *node, session_fifo_event_t *e0, stream_session_t *s0, int *n_tx_pkts)
Definition: session_node.c:527
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:882
#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:138
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:128
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:89
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
#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:618
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:555
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:297
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:262
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:277
transport_connection_t * tc
Definition: session.h:136
void vlib_worker_thread_barrier_release(vlib_main_t *vm)
Definition: threads.c:1508
#define vec_foreach(var, vec)
Vector iterator.
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:648
#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:916
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
void stream_session_disconnect_transport(stream_session_t *s)
Notify transport the session can be disconnected.
Definition: session.c:1107
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:364