FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
node.c
Go to the documentation of this file.
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2016 Cisco and/or its affiliates.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *------------------------------------------------------------------
16  */
17 
18 #define _GNU_SOURCE
19 #include <stdint.h>
20 #include <net/if.h>
21 #include <sys/ioctl.h>
22 #include <sys/uio.h>
23 
24 #include <vlib/vlib.h>
25 #include <vlib/unix/unix.h>
26 #include <vnet/ethernet/ethernet.h>
27 #include <vnet/devices/devices.h>
28 #include <vnet/feature/feature.h>
29 
30 #include <memif/memif.h>
31 #include <memif/private.h>
32 
33 #define foreach_memif_input_error \
34  _(BUFFER_ALLOC_FAIL, "buffer allocation failed") \
35  _(NOT_IP, "not ip packet")
36 
37 typedef enum
38 {
39 #define _(f,s) MEMIF_INPUT_ERROR_##f,
41 #undef _
44 
45 static __clib_unused char *memif_input_error_strings[] = {
46 #define _(n,s) s,
48 #undef _
49 };
50 
51 typedef struct
52 {
57 
58 static __clib_unused u8 *
59 format_memif_input_trace (u8 * s, va_list * args)
60 {
61  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
62  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
63  memif_input_trace_t *t = va_arg (*args, memif_input_trace_t *);
64  u32 indent = format_get_indent (s);
65 
66  s = format (s, "memif: hw_if_index %d next-index %d",
67  t->hw_if_index, t->next_index);
68  s = format (s, "\n%Uslot: ring %u", format_white_space, indent + 2,
69  t->ring);
70  return s;
71 }
72 
75 {
76  u8 *ptr = vlib_buffer_get_current (b);
77  u8 v = *ptr & 0xf0;
78 
79  if (PREDICT_TRUE (v == 0x40))
81  else if (PREDICT_TRUE (v == 0x60))
83 
84  b->error = node->errors[MEMIF_INPUT_ERROR_NOT_IP];
86 }
87 
90  memif_if_t * mif, vlib_buffer_t * b, u32 next, u16 qid,
91  uword * n_tracep)
92 {
94 
95  if (PREDICT_TRUE (b != 0))
96  {
98  vlib_trace_buffer (vm, node, next, b, /* follow_chain */ 0);
99  vlib_set_trace_count (vm, node, --(*n_tracep));
100  tr = vlib_add_trace (vm, node, b, sizeof (*tr));
101  tr->next_index = next;
102  tr->hw_if_index = mif->hw_if_index;
103  tr->ring = qid;
104  }
105 }
106 
109  u16 buffer_offset, u16 buffer_vec_index)
110 {
111  memif_copy_op_t *co;
113  co->data = data;
114  co->data_len = len;
115  co->buffer_offset = buffer_offset;
116  co->buffer_vec_index = buffer_vec_index;
117 }
118 
121  u32 buffer_size)
122 {
123  vlib_buffer_t *seg = b;
124  i32 bytes_left = b->current_length - buffer_size + b->current_data;
125 
126  if (PREDICT_TRUE (bytes_left <= 0))
127  return;
128 
129  b->current_length -= bytes_left;
131 
132  while (bytes_left)
133  {
134  seg->flags |= VLIB_BUFFER_NEXT_PRESENT;
135  seg->next_buffer = buffers[0];
136  seg = vlib_get_buffer (vm, buffers[0]);
137  buffers++;
138  seg->current_data = 0;
139  seg->current_length = clib_min (buffer_size, bytes_left);
140  bytes_left -= seg->current_length;
141  }
142 }
143 
146 {
147  u32 res = x - y;
148  res &= -(res <= x);
149  return res;
150 }
151 
152 /* branchless validation of the descriptor - uses saturated subtraction */
155 {
156  u32 rv;
157  u16 valid_flags = MEMIF_DESC_FLAG_NEXT;
158 
159  rv = d->flags & (~valid_flags);
160  rv |= sat_sub (d->region + 1, vec_len (mif->regions));
161  rv |= sat_sub (d->length, buffer_length);
162  rv |= sat_sub (d->offset + d->length, mif->regions[d->region].region_size);
163 
164  if (PREDICT_FALSE (rv))
165  {
166  mif->flags |= MEMIF_IF_FLAG_ERROR;
167  return 1;
168  }
169 
170  return 0;
171 }
172 
175  vlib_frame_t * frame, memif_if_t * mif,
178 {
179  vnet_main_t *vnm = vnet_get_main ();
180  memif_main_t *mm = &memif_main;
181  memif_ring_t *ring;
182  memif_queue_t *mq;
183  u16 buffer_size = vlib_buffer_get_default_data_size (vm);
184  uword n_trace = vlib_get_trace_count (vm, node);
185  u16 nexts[MEMIF_RX_VECTOR_SZ], *next = nexts;
186  u32 _to_next_bufs[MEMIF_RX_VECTOR_SZ], *to_next_bufs = _to_next_bufs, *bi;
187  u32 n_rx_packets = 0, n_rx_bytes = 0;
188  u32 n_left, n_left_to_next;
190  vlib_buffer_t *b0, *b1, *b2, *b3;
191  u32 thread_index = vm->thread_index;
193  thread_index);
194  vlib_buffer_t bt;
195  u16 cur_slot, last_slot, ring_size, n_slots, mask;
196  i16 start_offset;
197  u16 n_buffers = 0, n_alloc;
198  memif_copy_op_t *co;
199  memif_packet_op_t *po;
200  memif_region_index_t last_region = ~0;
201  void *last_region_shm = 0;
202 
203  mq = vec_elt_at_index (mif->rx_queues, qid);
204  ring = mq->ring;
205  ring_size = 1 << mq->log2_ring_size;
206  mask = ring_size - 1;
207 
208  /* asume that somebody will want to add ethernet header on the packet
209  so start with IP header at offset 14 */
210  start_offset = (mode == MEMIF_INTERFACE_MODE_IP) ? 14 : 0;
211 
212  /* for S2M rings, we are consumers of packet buffers, and for M2S rings we
213  are producers of empty buffers */
214  cur_slot = (type == MEMIF_RING_S2M) ? mq->last_head : mq->last_tail;
215  last_slot = (type == MEMIF_RING_S2M) ? ring->head : ring->tail;
216  if (cur_slot == last_slot)
217  goto refill;
218  n_slots = last_slot - cur_slot;
219 
220  /* construct copy and packet vector out of ring slots */
221  while (n_slots && n_rx_packets < MEMIF_RX_VECTOR_SZ)
222  {
223  u32 dst_off, src_off, n_bytes_left;
224  u16 s0;
225  memif_desc_t *d0;
226  void *mb0;
227  po = ptd->packet_ops + n_rx_packets;
228  n_rx_packets++;
229  po->first_buffer_vec_index = n_buffers++;
230  po->packet_len = 0;
231  src_off = 0;
232  dst_off = start_offset;
233 
234  next_slot:
235  CLIB_PREFETCH (&ring->desc[(cur_slot + 8) & mask],
236  CLIB_CACHE_LINE_BYTES, LOAD);
237  s0 = cur_slot & mask;
238  d0 = &ring->desc[s0];
239  n_bytes_left = d0->length;
240 
241  /* slave resets buffer length,
242  * so it can produce full size buffer for master
243  */
244  if (type == MEMIF_RING_M2S)
245  d0->length = mif->run.buffer_size;
246 
247  po->packet_len += n_bytes_left;
248  if (PREDICT_FALSE (last_region != d0->region))
249  {
250  last_region_shm = mif->regions[d0->region].shm;
251  last_region = d0->region;
252  }
253  mb0 = last_region_shm + d0->offset;
254 
255  do
256  {
257  u32 dst_free = buffer_size - dst_off;
258  if (dst_free == 0)
259  {
260  dst_off = 0;
261  dst_free = buffer_size;
262  n_buffers++;
263  }
264  u32 bytes_to_copy = clib_min (dst_free, n_bytes_left);
265  memif_add_copy_op (ptd, mb0 + src_off, bytes_to_copy, dst_off,
266  n_buffers - 1);
267  n_bytes_left -= bytes_to_copy;
268  src_off += bytes_to_copy;
269  dst_off += bytes_to_copy;
270  }
271  while (PREDICT_FALSE (n_bytes_left));
272 
273  cur_slot++;
274  n_slots--;
275  if ((d0->flags & MEMIF_DESC_FLAG_NEXT) && n_slots)
276  {
277  src_off = 0;
278  goto next_slot;
279  }
280  }
281 
282  /* allocate free buffers */
283  vec_validate_aligned (ptd->buffers, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
284  n_alloc = vlib_buffer_alloc_from_pool (vm, ptd->buffers, n_buffers,
285  mq->buffer_pool_index);
286  if (PREDICT_FALSE (n_alloc != n_buffers))
287  {
288  if (n_alloc)
289  vlib_buffer_free (vm, ptd->buffers, n_alloc);
290  vlib_error_count (vm, node->node_index,
291  MEMIF_INPUT_ERROR_BUFFER_ALLOC_FAIL, 1);
292  goto refill;
293  }
294 
295  /* copy data */
296  n_left = vec_len (ptd->copy_ops);
297  co = ptd->copy_ops;
298  while (n_left >= 8)
299  {
301  CLIB_PREFETCH (co[5].data, CLIB_CACHE_LINE_BYTES, LOAD);
302  CLIB_PREFETCH (co[6].data, CLIB_CACHE_LINE_BYTES, LOAD);
303  CLIB_PREFETCH (co[7].data, CLIB_CACHE_LINE_BYTES, LOAD);
304 
305  b0 = vlib_get_buffer (vm, ptd->buffers[co[0].buffer_vec_index]);
306  b1 = vlib_get_buffer (vm, ptd->buffers[co[1].buffer_vec_index]);
307  b2 = vlib_get_buffer (vm, ptd->buffers[co[2].buffer_vec_index]);
308  b3 = vlib_get_buffer (vm, ptd->buffers[co[3].buffer_vec_index]);
309 
310  clib_memcpy_fast (b0->data + co[0].buffer_offset, co[0].data,
311  co[0].data_len);
312  clib_memcpy_fast (b1->data + co[1].buffer_offset, co[1].data,
313  co[1].data_len);
314  clib_memcpy_fast (b2->data + co[2].buffer_offset, co[2].data,
315  co[2].data_len);
316  clib_memcpy_fast (b3->data + co[3].buffer_offset, co[3].data,
317  co[3].data_len);
318 
319  co += 4;
320  n_left -= 4;
321  }
322  while (n_left)
323  {
324  b0 = vlib_get_buffer (vm, ptd->buffers[co[0].buffer_vec_index]);
325  clib_memcpy_fast (b0->data + co[0].buffer_offset, co[0].data,
326  co[0].data_len);
327  co += 1;
328  n_left -= 1;
329  }
330 
331  /* release slots from the ring */
332  if (type == MEMIF_RING_S2M)
333  {
335  ring->tail = mq->last_head = cur_slot;
336  }
337  else
338  {
339  mq->last_tail = cur_slot;
340  }
341 
342  /* prepare buffer template and next indices */
343  vnet_buffer (&ptd->buffer_template)->sw_if_index[VLIB_RX] =
344  mif->sw_if_index;
345  vnet_buffer (&ptd->buffer_template)->feature_arc_index = 0;
346  ptd->buffer_template.current_data = start_offset;
349  ptd->buffer_template.ref_count = 1;
350 
351  if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
352  {
354  if (mif->per_interface_next_index != ~0)
355  next_index = mif->per_interface_next_index;
356  else
358  &ptd->buffer_template);
359 
360  vlib_get_new_next_frame (vm, node, next_index, to_next_bufs,
361  n_left_to_next);
363  {
364  vlib_next_frame_t *nf;
365  vlib_frame_t *f;
367  nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
368  f = vlib_get_frame (vm, nf->frame);
370 
371  ef = vlib_frame_scalar_args (f);
372  ef->sw_if_index = mif->sw_if_index;
373  ef->hw_if_index = mif->hw_if_index;
375  }
376  }
377 
378  /* process buffer metadata */
379  u32 n_from = n_rx_packets;
380  po = ptd->packet_ops;
381  bi = to_next_bufs;
382 
383  /* copy template into local variable - will save per packet load */
385 
386  while (n_from >= 8)
387  {
388  b0 = vlib_get_buffer (vm, ptd->buffers[po[0].first_buffer_vec_index]);
389  b1 = vlib_get_buffer (vm, ptd->buffers[po[1].first_buffer_vec_index]);
390  b2 = vlib_get_buffer (vm, ptd->buffers[po[2].first_buffer_vec_index]);
391  b3 = vlib_get_buffer (vm, ptd->buffers[po[3].first_buffer_vec_index]);
392 
393  vlib_prefetch_buffer_header (b0, STORE);
394  vlib_prefetch_buffer_header (b1, STORE);
395  vlib_prefetch_buffer_header (b2, STORE);
396  vlib_prefetch_buffer_header (b3, STORE);
397 
398  /* enqueue buffer */
399  u32 fbvi[4];
400  fbvi[0] = po[0].first_buffer_vec_index;
401  fbvi[1] = po[1].first_buffer_vec_index;
402  fbvi[2] = po[2].first_buffer_vec_index;
403  fbvi[3] = po[3].first_buffer_vec_index;
404 
405  bi[0] = ptd->buffers[fbvi[0]];
406  bi[1] = ptd->buffers[fbvi[1]];
407  bi[2] = ptd->buffers[fbvi[2]];
408  bi[3] = ptd->buffers[fbvi[3]];
409 
410  b0 = vlib_get_buffer (vm, bi[0]);
411  b1 = vlib_get_buffer (vm, bi[1]);
412  b2 = vlib_get_buffer (vm, bi[2]);
413  b3 = vlib_get_buffer (vm, bi[3]);
414 
415  vlib_buffer_copy_template (b0, &bt);
416  vlib_buffer_copy_template (b1, &bt);
417  vlib_buffer_copy_template (b2, &bt);
418  vlib_buffer_copy_template (b3, &bt);
419 
420  b0->current_length = po[0].packet_len;
421  n_rx_bytes += b0->current_length;
422  b1->current_length = po[1].packet_len;
423  n_rx_bytes += b1->current_length;
424  b2->current_length = po[2].packet_len;
425  n_rx_bytes += b2->current_length;
426  b3->current_length = po[3].packet_len;
427  n_rx_bytes += b3->current_length;
428 
429  memif_add_to_chain (vm, b0, ptd->buffers + fbvi[0] + 1, buffer_size);
430  memif_add_to_chain (vm, b1, ptd->buffers + fbvi[1] + 1, buffer_size);
431  memif_add_to_chain (vm, b2, ptd->buffers + fbvi[2] + 1, buffer_size);
432  memif_add_to_chain (vm, b3, ptd->buffers + fbvi[3] + 1, buffer_size);
433 
434  if (mode == MEMIF_INTERFACE_MODE_IP)
435  {
436  next[0] = memif_next_from_ip_hdr (node, b0);
437  next[1] = memif_next_from_ip_hdr (node, b1);
438  next[2] = memif_next_from_ip_hdr (node, b2);
439  next[3] = memif_next_from_ip_hdr (node, b3);
440  }
441 
442  /* next */
443  n_from -= 4;
444  po += 4;
445  bi += 4;
446  next += 4;
447  }
448  while (n_from)
449  {
450  u32 fbvi[4];
451  /* enqueue buffer */
452  fbvi[0] = po[0].first_buffer_vec_index;
453  bi[0] = ptd->buffers[fbvi[0]];
454  b0 = vlib_get_buffer (vm, bi[0]);
455  vlib_buffer_copy_template (b0, &bt);
456  b0->current_length = po->packet_len;
457  n_rx_bytes += b0->current_length;
458 
459  memif_add_to_chain (vm, b0, ptd->buffers + fbvi[0] + 1, buffer_size);
460 
461  if (mode == MEMIF_INTERFACE_MODE_IP)
462  {
463  next[0] = memif_next_from_ip_hdr (node, b0);
464  }
465 
466  /* next */
467  n_from -= 1;
468  po += 1;
469  bi += 1;
470  next += 1;
471  }
472 
473  /* packet trace if enabled */
474  if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
475  {
476  u32 n_left = n_rx_packets;
477  bi = to_next_bufs;
478  next = nexts;
479  u32 ni = next_index;
480  while (n_trace && n_left)
481  {
482  vlib_buffer_t *b;
484  if (mode != MEMIF_INTERFACE_MODE_ETHERNET)
485  ni = next[0];
486  b = vlib_get_buffer (vm, bi[0]);
487  vlib_trace_buffer (vm, node, ni, b, /* follow_chain */ 0);
488  tr = vlib_add_trace (vm, node, b, sizeof (*tr));
489  tr->next_index = ni;
490  tr->hw_if_index = mif->hw_if_index;
491  tr->ring = qid;
492 
493  /* next */
494  n_trace--;
495  n_left--;
496  bi++;
497  next++;
498  }
499  vlib_set_trace_count (vm, node, n_trace);
500  }
501 
502  if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
503  {
504  n_left_to_next -= n_rx_packets;
505  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
506  }
507  else
508  vlib_buffer_enqueue_to_next (vm, node, to_next_bufs, nexts, n_rx_packets);
509 
511  + VNET_INTERFACE_COUNTER_RX, thread_index,
512  mif->sw_if_index, n_rx_packets,
513  n_rx_bytes);
514 
515  /* refill ring with empty buffers */
516 refill:
517  vec_reset_length (ptd->buffers);
518  vec_reset_length (ptd->copy_ops);
519 
520  if (type == MEMIF_RING_M2S)
521  {
522  u16 head = ring->head;
523  n_slots = ring_size - head + mq->last_tail;
524 
525  while (n_slots--)
526  {
527  u16 s = head++ & mask;
528  memif_desc_t *d = &ring->desc[s];
529  d->length = mif->run.buffer_size;
530  }
531 
533  ring->head = head;
534  }
535 
536  return n_rx_packets;
537 }
538 
541  vlib_frame_t * frame, memif_if_t * mif,
543 {
544  vnet_main_t *vnm = vnet_get_main ();
545  memif_main_t *mm = &memif_main;
546  memif_ring_t *ring;
547  memif_queue_t *mq;
548  u32 next_index;
549  uword n_trace = vlib_get_trace_count (vm, node);
550  u32 n_rx_packets = 0, n_rx_bytes = 0;
551  u32 *to_next = 0, *buffers;
552  u32 bi0, bi1, bi2, bi3;
553  u16 s0, s1, s2, s3;
554  memif_desc_t *d0, *d1, *d2, *d3;
555  vlib_buffer_t *b0, *b1, *b2, *b3;
556  u32 thread_index = vm->thread_index;
558  thread_index);
559  u16 cur_slot, last_slot, ring_size, n_slots, mask, head;
560  i16 start_offset;
561  u32 buffer_length;
562  u16 n_alloc, n_from;
563 
564  mq = vec_elt_at_index (mif->rx_queues, qid);
565  ring = mq->ring;
566  ring_size = 1 << mq->log2_ring_size;
567  mask = ring_size - 1;
568 
569  next_index = (mode == MEMIF_INTERFACE_MODE_IP) ?
571 
572  /* asume that somebody will want to add ethernet header on the packet
573  so start with IP header at offset 14 */
574  start_offset = (mode == MEMIF_INTERFACE_MODE_IP) ? 14 : 0;
575  buffer_length = vlib_buffer_get_default_data_size (vm) - start_offset;
576 
577  cur_slot = mq->last_tail;
578  last_slot = ring->tail;
579  if (cur_slot == last_slot)
580  goto refill;
581  n_slots = last_slot - cur_slot;
582 
583  /* process ring slots */
586  while (n_slots && n_rx_packets < MEMIF_RX_VECTOR_SZ)
587  {
588  vlib_buffer_t *hb;
589 
590  s0 = cur_slot & mask;
591  bi0 = mq->buffers[s0];
592  ptd->buffers[n_rx_packets++] = bi0;
593 
594  CLIB_PREFETCH (&ring->desc[(cur_slot + 8) & mask],
595  CLIB_CACHE_LINE_BYTES, LOAD);
596  d0 = &ring->desc[s0];
597  hb = b0 = vlib_get_buffer (vm, bi0);
598  b0->current_data = start_offset;
599  b0->current_length = d0->length;
600  n_rx_bytes += d0->length;
601 
602  if (0 && memif_desc_is_invalid (mif, d0, buffer_length))
603  return 0;
604 
605  cur_slot++;
606  n_slots--;
607  if (PREDICT_FALSE ((d0->flags & MEMIF_DESC_FLAG_NEXT) && n_slots))
608  {
609  hb->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
610  next_slot:
611  s0 = cur_slot & mask;
612  d0 = &ring->desc[s0];
613  bi0 = mq->buffers[s0];
614 
615  /* previous buffer */
616  b0->next_buffer = bi0;
617  b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
618 
619  /* current buffer */
620  b0 = vlib_get_buffer (vm, bi0);
621  b0->current_data = start_offset;
622  b0->current_length = d0->length;
624  n_rx_bytes += d0->length;
625 
626  cur_slot++;
627  n_slots--;
628  if ((d0->flags & MEMIF_DESC_FLAG_NEXT) && n_slots)
629  goto next_slot;
630  }
631  }
632 
633  /* release slots from the ring */
634  mq->last_tail = cur_slot;
635 
636  n_from = n_rx_packets;
637  buffers = ptd->buffers;
638 
639  while (n_from)
640  {
641  u32 n_left_to_next;
642  u32 next0, next1, next2, next3;
643 
644  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
645  while (n_from >= 8 && n_left_to_next >= 4)
646  {
647  b0 = vlib_get_buffer (vm, buffers[4]);
648  b1 = vlib_get_buffer (vm, buffers[5]);
649  b2 = vlib_get_buffer (vm, buffers[6]);
650  b3 = vlib_get_buffer (vm, buffers[7]);
651  vlib_prefetch_buffer_header (b0, STORE);
652  vlib_prefetch_buffer_header (b1, STORE);
653  vlib_prefetch_buffer_header (b2, STORE);
654  vlib_prefetch_buffer_header (b3, STORE);
655 
656  /* enqueue buffer */
657  to_next[0] = bi0 = buffers[0];
658  to_next[1] = bi1 = buffers[1];
659  to_next[2] = bi2 = buffers[2];
660  to_next[3] = bi3 = buffers[3];
661  to_next += 4;
662  n_left_to_next -= 4;
663  buffers += 4;
664 
665  b0 = vlib_get_buffer (vm, bi0);
666  b1 = vlib_get_buffer (vm, bi1);
667  b2 = vlib_get_buffer (vm, bi2);
668  b3 = vlib_get_buffer (vm, bi3);
669 
670  vnet_buffer (b0)->sw_if_index[VLIB_RX] = mif->sw_if_index;
671  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
672  vnet_buffer (b1)->sw_if_index[VLIB_RX] = mif->sw_if_index;
673  vnet_buffer (b1)->sw_if_index[VLIB_TX] = ~0;
674  vnet_buffer (b2)->sw_if_index[VLIB_RX] = mif->sw_if_index;
675  vnet_buffer (b2)->sw_if_index[VLIB_TX] = ~0;
676  vnet_buffer (b3)->sw_if_index[VLIB_RX] = mif->sw_if_index;
677  vnet_buffer (b3)->sw_if_index[VLIB_TX] = ~0;
678 
679  if (mode == MEMIF_INTERFACE_MODE_IP)
680  {
681  next0 = memif_next_from_ip_hdr (node, b0);
682  next1 = memif_next_from_ip_hdr (node, b1);
683  next2 = memif_next_from_ip_hdr (node, b2);
684  next3 = memif_next_from_ip_hdr (node, b3);
685  }
686  else if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
687  {
688  if (PREDICT_FALSE (mif->per_interface_next_index != ~0))
689  {
690  next0 = mif->per_interface_next_index;
691  next1 = mif->per_interface_next_index;
692  next2 = mif->per_interface_next_index;
693  next3 = mif->per_interface_next_index;
694  }
695  else
696  {
697  next0 = next1 = next2 = next3 = next_index;
698  /* redirect if feature path enabled */
700  &next0, b0);
702  &next1, b1);
704  &next2, b2);
706  &next3, b3);
707  }
708  }
709 
710  /* trace */
711  if (PREDICT_FALSE (n_trace > 0))
712  {
713  memif_trace_buffer (vm, node, mif, b0, next0, qid, &n_trace);
714  if (PREDICT_FALSE (n_trace > 0))
715  memif_trace_buffer (vm, node, mif, b1, next1, qid, &n_trace);
716  if (PREDICT_FALSE (n_trace > 0))
717  memif_trace_buffer (vm, node, mif, b2, next2, qid, &n_trace);
718  if (PREDICT_FALSE (n_trace > 0))
719  memif_trace_buffer (vm, node, mif, b3, next3, qid, &n_trace);
720  }
721 
722  /* enqueue */
723  vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
724  n_left_to_next, bi0, bi1, bi2, bi3,
725  next0, next1, next2, next3);
726 
727  /* next */
728  n_from -= 4;
729  }
730  while (n_from && n_left_to_next)
731  {
732  /* enqueue buffer */
733  to_next[0] = bi0 = buffers[0];
734  to_next += 1;
735  n_left_to_next--;
736  buffers += 1;
737 
738  b0 = vlib_get_buffer (vm, bi0);
739  vnet_buffer (b0)->sw_if_index[VLIB_RX] = mif->sw_if_index;
740  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
741 
742  if (mode == MEMIF_INTERFACE_MODE_IP)
743  {
744  next0 = memif_next_from_ip_hdr (node, b0);
745  }
746  else if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
747  {
748  if (PREDICT_FALSE (mif->per_interface_next_index != ~0))
749  next0 = mif->per_interface_next_index;
750  else
751  {
752  next0 = next_index;
753  /* redirect if feature path enabled */
755  &next0, b0);
756  }
757  }
758 
759  /* trace */
760  if (PREDICT_FALSE (n_trace > 0))
761  memif_trace_buffer (vm, node, mif, b0, next0, qid, &n_trace);
762 
763  /* enqueue */
764  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
765  n_left_to_next, bi0, next0);
766 
767  /* next */
768  n_from--;
769  }
770  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
771  }
772 
774  + VNET_INTERFACE_COUNTER_RX, thread_index,
775  mif->sw_if_index, n_rx_packets,
776  n_rx_bytes);
777 
778  /* refill ring with empty buffers */
779 refill:
780  vec_reset_length (ptd->buffers);
781 
782  head = ring->head;
783  n_slots = ring_size - head + mq->last_tail;
784 
785  if (n_slots < 32)
786  goto done;
787 
788  memif_desc_t *dt = &ptd->desc_template;
789  clib_memset (dt, 0, sizeof (memif_desc_t));
790  dt->length = buffer_length;
791 
792  n_alloc = vlib_buffer_alloc_to_ring_from_pool (vm, mq->buffers, head & mask,
793  ring_size, n_slots,
794  mq->buffer_pool_index);
795 
796  if (PREDICT_FALSE (n_alloc != n_slots))
797  {
798  vlib_error_count (vm, node->node_index,
799  MEMIF_INPUT_ERROR_BUFFER_ALLOC_FAIL, 1);
800  }
801 
802  while (n_alloc >= 32)
803  {
804  bi0 = mq->buffers[(head + 4) & mask];
805  vlib_prefetch_buffer_with_index (vm, bi0, LOAD);
806  bi1 = mq->buffers[(head + 5) & mask];
807  vlib_prefetch_buffer_with_index (vm, bi1, LOAD);
808  bi2 = mq->buffers[(head + 6) & mask];
809  vlib_prefetch_buffer_with_index (vm, bi2, LOAD);
810  bi3 = mq->buffers[(head + 7) & mask];
811  vlib_prefetch_buffer_with_index (vm, bi3, LOAD);
812 
813  s0 = head++ & mask;
814  s1 = head++ & mask;
815  s2 = head++ & mask;
816  s3 = head++ & mask;
817 
818  d0 = &ring->desc[s0];
819  d1 = &ring->desc[s1];
820  d2 = &ring->desc[s2];
821  d3 = &ring->desc[s3];
822 
823  clib_memcpy_fast (d0, dt, sizeof (memif_desc_t));
824  clib_memcpy_fast (d1, dt, sizeof (memif_desc_t));
825  clib_memcpy_fast (d2, dt, sizeof (memif_desc_t));
826  clib_memcpy_fast (d3, dt, sizeof (memif_desc_t));
827 
828  b0 = vlib_get_buffer (vm, mq->buffers[s0]);
829  b1 = vlib_get_buffer (vm, mq->buffers[s1]);
830  b2 = vlib_get_buffer (vm, mq->buffers[s2]);
831  b3 = vlib_get_buffer (vm, mq->buffers[s3]);
832 
833  d0->region = b0->buffer_pool_index + 1;
834  d1->region = b1->buffer_pool_index + 1;
835  d2->region = b2->buffer_pool_index + 1;
836  d3->region = b3->buffer_pool_index + 1;
837 
838  d0->offset =
839  (void *) b0->data - mif->regions[d0->region].shm + start_offset;
840  d1->offset =
841  (void *) b1->data - mif->regions[d1->region].shm + start_offset;
842  d2->offset =
843  (void *) b2->data - mif->regions[d2->region].shm + start_offset;
844  d3->offset =
845  (void *) b3->data - mif->regions[d3->region].shm + start_offset;
846 
847  n_alloc -= 4;
848  }
849  while (n_alloc)
850  {
851  s0 = head++ & mask;
852  d0 = &ring->desc[s0];
853  clib_memcpy_fast (d0, dt, sizeof (memif_desc_t));
854  b0 = vlib_get_buffer (vm, mq->buffers[s0]);
855  d0->region = b0->buffer_pool_index + 1;
856  d0->offset =
857  (void *) b0->data - mif->regions[d0->region].shm + start_offset;
858 
859  n_alloc -= 1;
860  }
861 
863  ring->head = head;
864 
865 done:
866  return n_rx_packets;
867 }
868 
869 
873 {
874  u32 n_rx = 0;
875  memif_main_t *mm = &memif_main;
876  vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
880 
882  {
883  memif_if_t *mif;
884  mif = vec_elt_at_index (mm->interfaces, dq->dev_instance);
885  if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) &&
886  (mif->flags & MEMIF_IF_FLAG_CONNECTED))
887  {
888  if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
889  {
890  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
891  n_rx += memif_device_input_zc_inline (vm, node, frame, mif,
892  dq->queue_id, mode_ip);
893  else
894  n_rx += memif_device_input_zc_inline (vm, node, frame, mif,
895  dq->queue_id, mode_eth);
896  }
897  else if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
898  {
899  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
900  n_rx += memif_device_input_inline (vm, node, frame, mif,
901  MEMIF_RING_M2S, dq->queue_id,
902  mode_ip);
903  else
904  n_rx += memif_device_input_inline (vm, node, frame, mif,
905  MEMIF_RING_M2S, dq->queue_id,
906  mode_eth);
907  }
908  else
909  {
910  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
911  n_rx += memif_device_input_inline (vm, node, frame, mif,
912  MEMIF_RING_S2M, dq->queue_id,
913  mode_ip);
914  else
915  n_rx += memif_device_input_inline (vm, node, frame, mif,
916  MEMIF_RING_S2M, dq->queue_id,
917  mode_eth);
918  }
919  }
920  }
921 
922  return n_rx;
923 }
924 
925 /* *INDENT-OFF* */
927  .name = "memif-input",
929  .sibling_of = "device-input",
930  .format_trace = format_memif_input_trace,
931  .type = VLIB_NODE_TYPE_INPUT,
932  .state = VLIB_NODE_STATE_INTERRUPT,
933  .n_errors = MEMIF_INPUT_N_ERROR,
934  .error_strings = memif_input_error_strings,
935 };
936 
937 /* *INDENT-ON* */
938 
939 
940 /*
941  * fd.io coding-style-patch-verification: ON
942  *
943  * Local Variables:
944  * eval: (c-set-style "gnu")
945  * End:
946  */
memif_if_t * interfaces
Definition: private.h:241
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:124
static __clib_warn_unused_result u32 vlib_buffer_alloc_to_ring_from_pool(vlib_main_t *vm, u32 *ring, u32 start, u32 ring_size, u32 n_buffers, u8 buffer_pool_index)
Allocate buffers into ring from specific buffer pool.
Definition: buffer_funcs.h:722
vnet_device_and_queue_t * devices_and_queues
Definition: devices.h:69
#define clib_min(x, y)
Definition: clib.h:327
#define CLIB_UNUSED(x)
Definition: clib.h:87
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:193
struct memif_if_t::@674 run
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 thread_index, u32 index, u64 n_packets, u64 n_bytes)
Increment a combined counter.
Definition: counter.h:220
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:937
static __clib_unused u8 * format_memif_input_trace(u8 *s, va_list *args)
Definition: node.c:59
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
vnet_interface_main_t interface_main
Definition: vnet.h:59
#define PREDICT_TRUE(x)
Definition: clib.h:121
static_always_inline void memif_add_to_chain(vlib_main_t *vm, vlib_buffer_t *b, u32 *buffers, u32 buffer_size)
Definition: node.c:120
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
#define CLIB_MEMORY_STORE_BARRIER()
Definition: clib.h:135
static void vlib_error_count(vlib_main_t *vm, uword node_index, uword counter, uword increment)
Definition: error_funcs.h:57
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
#define VLIB_NODE_FLAG_TRACE_SUPPORTED
Definition: node.h:305
#define vec_add2_aligned(V, P, N, A)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:642
u32 thread_index
Definition: main.h:249
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
static_always_inline uword memif_device_input_zc_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, memif_if_t *mif, u16 qid, memif_interface_mode_t mode)
Definition: node.c:540
memif_interface_mode_t
Definition: memif.h:55
static vlib_frame_t * vlib_get_frame(vlib_main_t *vm, vlib_frame_t *f)
Definition: node_funcs.h:269
#define vlib_validate_buffer_enqueue_x4(vm, node, next_index, to_next, n_left_to_next, bi0, bi1, bi2, bi3, next0, next1, next2, next3)
Finish enqueueing four buffers forward in the graph.
Definition: buffer_node.h:140
uint16_t memif_region_index_t
Definition: memif.h:62
static u32 format_get_indent(u8 *s)
Definition: format.h:72
static __clib_unused char * memif_input_error_strings[]
Definition: node.c:45
vlib_main_t * vm
Definition: in2out_ed.c:1582
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
#define VLIB_NODE_FN(node)
Definition: node.h:202
u16 first_buffer_vec_index
Definition: private.h:205
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:520
u32 * buffers
Definition: private.h:125
u16 mask
Definition: flow_types.api:52
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:469
#define foreach_memif_input_error
Definition: node.c:33
unsigned char u8
Definition: types.h:56
u8 data[128]
Definition: ipsec_types.api:89
u8 buffer_pool_index
index of buffer pool this buffer belongs.
Definition: buffer.h:133
uint32_t length
Definition: memif.h:154
#define vec_reset_length(v)
Reset vector length to zero NULL-pointer tolerant.
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:136
#define static_always_inline
Definition: clib.h:108
#define vlib_prefetch_buffer_with_index(vm, bi, type)
Prefetch buffer metadata by buffer index The first 64 bytes of buffer contains most header informatio...
Definition: buffer_funcs.h:476
#define ETH_INPUT_FRAME_F_SINGLE_SW_IF_IDX
Definition: ethernet.h:52
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:881
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
u8 buffer_pool_index
Definition: private.h:126
u16 buffer_size
Definition: private.h:186
static_always_inline u32 memif_next_from_ip_hdr(vlib_node_runtime_t *node, vlib_buffer_t *b)
Definition: node.c:74
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:203
uint16_t flags
Definition: memif.h:151
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
u32 per_interface_next_index
Definition: private.h:164
#define vlib_get_new_next_frame(vm, node, next_index, vectors, n_vectors_left)
Definition: node_funcs.h:396
vlib_node_registration_t memif_input_node
(constructor) VLIB_REGISTER_NODE (memif_input_node)
Definition: node.c:926
unsigned int u32
Definition: types.h:88
void * shm
Definition: private.h:102
static vlib_next_frame_t * vlib_node_runtime_get_next_frame(vlib_main_t *vm, vlib_node_runtime_t *n, u32 next_index)
Definition: node_funcs.h:317
memif_packet_op_t packet_ops[MEMIF_RX_VECTOR_SZ]
Definition: private.h:224
vl_api_fib_path_type_t type
Definition: fib_types.api:123
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:136
memif_region_index_t region
Definition: memif.h:153
u16 last_head
Definition: private.h:123
memif_copy_op_t * copy_ops
Definition: private.h:225
memif_desc_t desc[0]
Definition: memif.h:175
static_always_inline void memif_trace_buffer(vlib_main_t *vm, vlib_node_runtime_t *node, memif_if_t *mif, vlib_buffer_t *b, u32 next, u16 qid, uword *n_tracep)
Definition: node.c:89
uword dev_instance
Definition: private.h:161
static_always_inline void vlib_buffer_copy_template(vlib_buffer_t *b, vlib_buffer_t *bt)
Definition: buffer_funcs.h:181
unsigned short u16
Definition: types.h:57
static_always_inline void memif_add_copy_op(memif_per_thread_data_t *ptd, void *data, u32 len, u16 buffer_offset, u16 buffer_vec_index)
Definition: node.c:108
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
void * data
Definition: private.h:211
static_always_inline uword memif_device_input_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, memif_if_t *mif, memif_ring_type_t type, u16 qid, memif_interface_mode_t mode)
Definition: node.c:174
#define PREDICT_FALSE(x)
Definition: clib.h:120
u32 node_index
Node index.
Definition: node.h:487
#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:224
#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:391
vl_api_tunnel_mode_t mode
Definition: gre.api:48
u8 len
Definition: ip_types.api:92
static_always_inline u32 vlib_buffer_get_default_data_size(vlib_main_t *vm)
Definition: buffer_funcs.h:96
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:80
static_always_inline void vlib_buffer_enqueue_to_next(vlib_main_t *vm, vlib_node_runtime_t *node, u32 *buffers, u16 *nexts, uword count)
Definition: buffer_node.h:339
u16 buffer_vec_index
Definition: private.h:214
u8 data[]
Packet data.
Definition: buffer.h:181
static void * vlib_frame_scalar_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:311
u32 current_config_index
Used by feature subgraph arcs to visit enabled feature nodes.
Definition: buffer.h:147
u16 last_tail
Definition: private.h:124
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:483
i16 buffer_offset
Definition: private.h:213
static_always_inline u32 memif_desc_is_invalid(memif_if_t *mif, memif_desc_t *d, u32 buffer_length)
Definition: node.c:154
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1582
memif_region_t * regions
Definition: private.h:172
signed int i32
Definition: types.h:77
#define MEMIF_DESC_FLAG_NEXT
Definition: memif.h:152
memif_input_error_t
Definition: node.c:37
vlib_frame_t * frame
Definition: node.h:405
u16 flags
Definition: node.h:387
u32 flags
Definition: private.h:157
memif_ring_t * ring
Definition: private.h:118
vlib_buffer_t buffer_template
Definition: private.h:229
u32 hw_if_index
Definition: private.h:159
memif_region_offset_t offset
Definition: memif.h:155
#define foreach_device_and_queue(var, vec)
Definition: devices.h:161
Definition: defs.h:47
#define MEMIF_RX_VECTOR_SZ
Definition: private.h:217
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:140
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1583
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:492
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
memif_log2_ring_size_t log2_ring_size
Definition: private.h:119
#define vnet_buffer(b)
Definition: buffer.h:417
static_always_inline void vnet_feature_start_device_input_x1(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:343
memif_per_thread_data_t * per_thread_data
Definition: private.h:248
memif_ring_type_t
Definition: memif.h:49
volatile uint16_t head
Definition: memif.h:171
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:577
memif_queue_t * rx_queues
Definition: private.h:174
memif_desc_t desc_template
Definition: private.h:230
static void vlib_frame_no_append(vlib_frame_t *f)
Definition: node_funcs.h:277
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:209
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:59
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:167
memif_main_t memif_main
Definition: memif.c:43
volatile u8 ref_count
Reference count for this buffer.
Definition: buffer.h:130
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:85
u32 sw_if_index
Definition: private.h:160
volatile uint16_t tail
Definition: memif.h:173
memif_interface_mode_t mode
Definition: private.h:162
Definition: defs.h:46
memif_region_size_t region_size
Definition: private.h:103
static __clib_warn_unused_result u32 vlib_buffer_alloc_from_pool(vlib_main_t *vm, u32 *buffers, u32 n_buffers, u8 buffer_pool_index)
Allocate buffers from specific pool into supplied array.
Definition: buffer_funcs.h:566
static_always_inline u32 sat_sub(u32 x, u32 y)
Definition: node.c:145
signed short i16
Definition: types.h:46