FD.io VPP  v19.04.1-1-ge4a0f9f
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,
176  memif_ring_type_t type, u16 qid,
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, next_index;
189  vlib_buffer_t *b0, *b1, *b2, *b3;
190  u32 thread_index = vm->thread_index;
192  thread_index);
193  vlib_buffer_t bt;
194  u16 cur_slot, last_slot, ring_size, n_slots, mask;
195  i16 start_offset;
196  u16 n_buffers = 0, n_alloc;
197  memif_copy_op_t *co;
198  memif_packet_op_t *po;
199  memif_region_index_t last_region = ~0;
200  void *last_region_shm = 0;
201 
202  mq = vec_elt_at_index (mif->rx_queues, qid);
203  ring = mq->ring;
204  ring_size = 1 << mq->log2_ring_size;
205  mask = ring_size - 1;
206 
207  /* asume that somebody will want to add ethernet header on the packet
208  so start with IP header at offset 14 */
209  start_offset = (mode == MEMIF_INTERFACE_MODE_IP) ? 14 : 0;
210 
211  /* for S2M rings, we are consumers of packet buffers, and for M2S rings we
212  are producers of empty buffers */
213  cur_slot = (type == MEMIF_RING_S2M) ? mq->last_head : mq->last_tail;
214  last_slot = (type == MEMIF_RING_S2M) ? ring->head : ring->tail;
215  if (cur_slot == last_slot)
216  goto refill;
217  n_slots = last_slot - cur_slot;
218 
219  /* construct copy and packet vector out of ring slots */
220  while (n_slots && n_rx_packets < MEMIF_RX_VECTOR_SZ)
221  {
222  u32 dst_off, src_off, n_bytes_left;
223  u16 s0;
224  memif_desc_t *d0;
225  void *mb0;
226  po = ptd->packet_ops + n_rx_packets;
227  n_rx_packets++;
228  po->first_buffer_vec_index = n_buffers++;
229  po->packet_len = 0;
230  src_off = 0;
231  dst_off = start_offset;
232 
233  next_slot:
234  CLIB_PREFETCH (&ring->desc[(cur_slot + 8) & mask],
235  CLIB_CACHE_LINE_BYTES, LOAD);
236  s0 = cur_slot & mask;
237  d0 = &ring->desc[s0];
238  n_bytes_left = d0->length;
239 
240  /* slave resets buffer length,
241  * so it can produce full size buffer for master
242  */
243  if (type == MEMIF_RING_M2S)
244  d0->length = mif->run.buffer_size;
245 
246  po->packet_len += n_bytes_left;
247  if (PREDICT_FALSE (last_region != d0->region))
248  {
249  last_region_shm = mif->regions[d0->region].shm;
250  last_region = d0->region;
251  }
252  mb0 = last_region_shm + d0->offset;
253 
254  do
255  {
256  u32 dst_free = buffer_size - dst_off;
257  if (dst_free == 0)
258  {
259  dst_off = 0;
260  dst_free = buffer_size;
261  n_buffers++;
262  }
263  u32 bytes_to_copy = clib_min (dst_free, n_bytes_left);
264  memif_add_copy_op (ptd, mb0 + src_off, bytes_to_copy, dst_off,
265  n_buffers - 1);
266  n_bytes_left -= bytes_to_copy;
267  src_off += bytes_to_copy;
268  dst_off += bytes_to_copy;
269  }
270  while (PREDICT_FALSE (n_bytes_left));
271 
272  cur_slot++;
273  n_slots--;
274  if ((d0->flags & MEMIF_DESC_FLAG_NEXT) && n_slots)
275  {
276  src_off = 0;
277  goto next_slot;
278  }
279  }
280 
281  /* allocate free buffers */
282  vec_validate_aligned (ptd->buffers, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
283  n_alloc = vlib_buffer_alloc_from_pool (vm, ptd->buffers, n_buffers,
284  mq->buffer_pool_index);
285  if (PREDICT_FALSE (n_alloc != n_buffers))
286  {
287  if (n_alloc)
288  vlib_buffer_free (vm, ptd->buffers, n_alloc);
289  vlib_error_count (vm, node->node_index,
290  MEMIF_INPUT_ERROR_BUFFER_ALLOC_FAIL, 1);
291  goto refill;
292  }
293 
294  /* copy data */
295  n_left = vec_len (ptd->copy_ops);
296  co = ptd->copy_ops;
297  while (n_left >= 8)
298  {
300  CLIB_PREFETCH (co[5].data, CLIB_CACHE_LINE_BYTES, LOAD);
301  CLIB_PREFETCH (co[6].data, CLIB_CACHE_LINE_BYTES, LOAD);
302  CLIB_PREFETCH (co[7].data, CLIB_CACHE_LINE_BYTES, LOAD);
303 
304  b0 = vlib_get_buffer (vm, ptd->buffers[co[0].buffer_vec_index]);
305  b1 = vlib_get_buffer (vm, ptd->buffers[co[1].buffer_vec_index]);
306  b2 = vlib_get_buffer (vm, ptd->buffers[co[2].buffer_vec_index]);
307  b3 = vlib_get_buffer (vm, ptd->buffers[co[3].buffer_vec_index]);
308 
309  clib_memcpy_fast (b0->data + co[0].buffer_offset, co[0].data,
310  co[0].data_len);
311  clib_memcpy_fast (b1->data + co[1].buffer_offset, co[1].data,
312  co[1].data_len);
313  clib_memcpy_fast (b2->data + co[2].buffer_offset, co[2].data,
314  co[2].data_len);
315  clib_memcpy_fast (b3->data + co[3].buffer_offset, co[3].data,
316  co[3].data_len);
317 
318  co += 4;
319  n_left -= 4;
320  }
321  while (n_left)
322  {
323  b0 = vlib_get_buffer (vm, ptd->buffers[co[0].buffer_vec_index]);
324  clib_memcpy_fast (b0->data + co[0].buffer_offset, co[0].data,
325  co[0].data_len);
326  co += 1;
327  n_left -= 1;
328  }
329 
330  /* release slots from the ring */
331  if (type == MEMIF_RING_S2M)
332  {
334  ring->tail = mq->last_head = cur_slot;
335  }
336  else
337  {
338  mq->last_tail = cur_slot;
339  }
340 
341  /* prepare buffer template and next indices */
342  vnet_buffer (&ptd->buffer_template)->sw_if_index[VLIB_RX] =
343  mif->sw_if_index;
344  vnet_buffer (&ptd->buffer_template)->feature_arc_index = 0;
345  ptd->buffer_template.current_data = start_offset;
348  ptd->buffer_template.ref_count = 1;
349 
350  if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
351  {
353  if (mif->per_interface_next_index != ~0)
354  next_index = mif->per_interface_next_index;
355  else
357  &ptd->buffer_template);
358 
359  vlib_get_new_next_frame (vm, node, next_index, to_next_bufs,
360  n_left_to_next);
362  {
363  vlib_next_frame_t *nf;
364  vlib_frame_t *f;
366  nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
367  f = vlib_get_frame (vm, nf->frame_index);
369 
370  ef = vlib_frame_scalar_args (f);
371  ef->sw_if_index = mif->sw_if_index;
372  ef->hw_if_index = mif->hw_if_index;
374  }
375  }
376 
377  /* process buffer metadata */
378  u32 n_from = n_rx_packets;
379  po = ptd->packet_ops;
380  bi = to_next_bufs;
381 
382  /* copy template into local variable - will save per packet load */
384 
385  while (n_from >= 8)
386  {
387  b0 = vlib_get_buffer (vm, ptd->buffers[po[0].first_buffer_vec_index]);
388  b1 = vlib_get_buffer (vm, ptd->buffers[po[1].first_buffer_vec_index]);
389  b2 = vlib_get_buffer (vm, ptd->buffers[po[2].first_buffer_vec_index]);
390  b3 = vlib_get_buffer (vm, ptd->buffers[po[3].first_buffer_vec_index]);
391 
392  vlib_prefetch_buffer_header (b0, STORE);
393  vlib_prefetch_buffer_header (b1, STORE);
394  vlib_prefetch_buffer_header (b2, STORE);
395  vlib_prefetch_buffer_header (b3, STORE);
396 
397  /* enqueue buffer */
398  u32 fbvi[4];
399  fbvi[0] = po[0].first_buffer_vec_index;
400  fbvi[1] = po[1].first_buffer_vec_index;
401  fbvi[2] = po[2].first_buffer_vec_index;
402  fbvi[3] = po[3].first_buffer_vec_index;
403 
404  bi[0] = ptd->buffers[fbvi[0]];
405  bi[1] = ptd->buffers[fbvi[1]];
406  bi[2] = ptd->buffers[fbvi[2]];
407  bi[3] = ptd->buffers[fbvi[3]];
408 
409  b0 = vlib_get_buffer (vm, bi[0]);
410  b1 = vlib_get_buffer (vm, bi[1]);
411  b2 = vlib_get_buffer (vm, bi[2]);
412  b3 = vlib_get_buffer (vm, bi[3]);
413 
414  vlib_buffer_copy_template (b0, &bt);
415  vlib_buffer_copy_template (b1, &bt);
416  vlib_buffer_copy_template (b2, &bt);
417  vlib_buffer_copy_template (b3, &bt);
418 
419  b0->current_length = po[0].packet_len;
420  n_rx_bytes += b0->current_length;
421  b1->current_length = po[1].packet_len;
422  n_rx_bytes += b1->current_length;
423  b2->current_length = po[2].packet_len;
424  n_rx_bytes += b2->current_length;
425  b3->current_length = po[3].packet_len;
426  n_rx_bytes += b3->current_length;
427 
428  memif_add_to_chain (vm, b0, ptd->buffers + fbvi[0] + 1, buffer_size);
429  memif_add_to_chain (vm, b1, ptd->buffers + fbvi[1] + 1, buffer_size);
430  memif_add_to_chain (vm, b2, ptd->buffers + fbvi[2] + 1, buffer_size);
431  memif_add_to_chain (vm, b3, ptd->buffers + fbvi[3] + 1, buffer_size);
432 
433  if (mode == MEMIF_INTERFACE_MODE_IP)
434  {
435  next[0] = memif_next_from_ip_hdr (node, b0);
436  next[1] = memif_next_from_ip_hdr (node, b1);
437  next[2] = memif_next_from_ip_hdr (node, b2);
438  next[3] = memif_next_from_ip_hdr (node, b3);
439  }
440 
441  /* next */
442  n_from -= 4;
443  po += 4;
444  bi += 4;
445  next += 4;
446  }
447  while (n_from)
448  {
449  u32 fbvi[4];
450  /* enqueue buffer */
451  fbvi[0] = po[0].first_buffer_vec_index;
452  bi[0] = ptd->buffers[fbvi[0]];
453  b0 = vlib_get_buffer (vm, bi[0]);
454  vlib_buffer_copy_template (b0, &bt);
455  b0->current_length = po->packet_len;
456  n_rx_bytes += b0->current_length;
457 
458  memif_add_to_chain (vm, b0, ptd->buffers + fbvi[0] + 1, buffer_size);
459 
460  if (mode == MEMIF_INTERFACE_MODE_IP)
461  {
462  next[0] = memif_next_from_ip_hdr (node, b0);
463  }
464 
465  /* next */
466  n_from -= 1;
467  po += 1;
468  bi += 1;
469  next += 1;
470  }
471 
472  /* packet trace if enabled */
473  if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
474  {
475  u32 n_left = n_rx_packets;
476  bi = to_next_bufs;
477  next = nexts;
478  u32 ni = next_index;
479  while (n_trace && n_left)
480  {
481  vlib_buffer_t *b;
483  if (mode != MEMIF_INTERFACE_MODE_ETHERNET)
484  ni = next[0];
485  b = vlib_get_buffer (vm, bi[0]);
486  vlib_trace_buffer (vm, node, ni, b, /* follow_chain */ 0);
487  tr = vlib_add_trace (vm, node, b, sizeof (*tr));
488  tr->next_index = ni;
489  tr->hw_if_index = mif->hw_if_index;
490  tr->ring = qid;
491 
492  /* next */
493  n_trace--;
494  n_left--;
495  bi++;
496  next++;
497  }
498  vlib_set_trace_count (vm, node, n_trace);
499  }
500 
501  if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
502  {
503  n_left_to_next -= n_rx_packets;
504  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
505  }
506  else
507  vlib_buffer_enqueue_to_next (vm, node, to_next_bufs, nexts, n_rx_packets);
508 
510  + VNET_INTERFACE_COUNTER_RX, thread_index,
511  mif->hw_if_index, n_rx_packets,
512  n_rx_bytes);
513 
514  /* refill ring with empty buffers */
515 refill:
516  vec_reset_length (ptd->buffers);
517  vec_reset_length (ptd->copy_ops);
518 
519  if (type == MEMIF_RING_M2S)
520  {
521  u16 head = ring->head;
522  n_slots = ring_size - head + mq->last_tail;
523 
524  while (n_slots--)
525  {
526  u16 s = head++ & mask;
527  memif_desc_t *d = &ring->desc[s];
528  d->length = mif->run.buffer_size;
529  }
530 
532  ring->head = head;
533  }
534 
535  return n_rx_packets;
536 }
537 
540  vlib_frame_t * frame, memif_if_t * mif,
542 {
543  vnet_main_t *vnm = vnet_get_main ();
544  memif_main_t *mm = &memif_main;
545  memif_ring_t *ring;
546  memif_queue_t *mq;
547  u32 next_index;
548  uword n_trace = vlib_get_trace_count (vm, node);
549  u32 n_rx_packets = 0, n_rx_bytes = 0;
550  u32 *to_next = 0, *buffers;
551  u32 bi0, bi1, bi2, bi3;
552  u16 s0, s1, s2, s3;
553  memif_desc_t *d0, *d1, *d2, *d3;
554  vlib_buffer_t *b0, *b1, *b2, *b3;
555  u32 thread_index = vm->thread_index;
557  thread_index);
558  u16 cur_slot, last_slot, ring_size, n_slots, mask, head;
559  i16 start_offset;
560  u32 buffer_length;
561  u16 n_alloc, n_from;
562 
563  mq = vec_elt_at_index (mif->rx_queues, qid);
564  ring = mq->ring;
565  ring_size = 1 << mq->log2_ring_size;
566  mask = ring_size - 1;
567 
568  next_index = (mode == MEMIF_INTERFACE_MODE_IP) ?
570 
571  /* asume that somebody will want to add ethernet header on the packet
572  so start with IP header at offset 14 */
573  start_offset = (mode == MEMIF_INTERFACE_MODE_IP) ? 14 : 0;
574  buffer_length = vlib_buffer_get_default_data_size (vm) - start_offset;
575 
576  cur_slot = mq->last_tail;
577  last_slot = ring->tail;
578  if (cur_slot == last_slot)
579  goto refill;
580  n_slots = last_slot - cur_slot;
581 
582  /* process ring slots */
585  while (n_slots && n_rx_packets < MEMIF_RX_VECTOR_SZ)
586  {
587  vlib_buffer_t *hb;
588 
589  s0 = cur_slot & mask;
590  bi0 = mq->buffers[s0];
591  ptd->buffers[n_rx_packets++] = bi0;
592 
593  CLIB_PREFETCH (&ring->desc[(cur_slot + 8) & mask],
594  CLIB_CACHE_LINE_BYTES, LOAD);
595  d0 = &ring->desc[s0];
596  hb = b0 = vlib_get_buffer (vm, bi0);
597  b0->current_data = start_offset;
598  b0->current_length = start_offset + d0->length;
599  n_rx_bytes += d0->length;
600 
601  if (0 && memif_desc_is_invalid (mif, d0, buffer_length))
602  return 0;
603 
604  cur_slot++;
605  n_slots--;
606  if (PREDICT_FALSE ((d0->flags & MEMIF_DESC_FLAG_NEXT) && n_slots))
607  {
608  hb->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
609  next_slot:
610  s0 = cur_slot & mask;
611  d0 = &ring->desc[s0];
612  bi0 = mq->buffers[s0];
613 
614  /* previous buffer */
615  b0->next_buffer = bi0;
616  b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
617 
618  /* current buffer */
619  b0 = vlib_get_buffer (vm, bi0);
620  b0->current_data = start_offset;
621  b0->current_length = start_offset + d0->length;
623  n_rx_bytes += d0->length;
624 
625  cur_slot++;
626  n_slots--;
627  if ((d0->flags & MEMIF_DESC_FLAG_NEXT) && n_slots)
628  goto next_slot;
629  }
630  }
631 
632  /* release slots from the ring */
633  mq->last_tail = cur_slot;
634 
635  n_from = n_rx_packets;
636  buffers = ptd->buffers;
637 
638  while (n_from)
639  {
640  u32 n_left_to_next;
641  u32 next0, next1, next2, next3;
642 
643  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
644  while (n_from >= 8 && n_left_to_next >= 4)
645  {
646  b0 = vlib_get_buffer (vm, buffers[4]);
647  b1 = vlib_get_buffer (vm, buffers[5]);
648  b2 = vlib_get_buffer (vm, buffers[6]);
649  b3 = vlib_get_buffer (vm, buffers[7]);
650  vlib_prefetch_buffer_header (b0, STORE);
651  vlib_prefetch_buffer_header (b1, STORE);
652  vlib_prefetch_buffer_header (b2, STORE);
653  vlib_prefetch_buffer_header (b3, STORE);
654 
655  /* enqueue buffer */
656  to_next[0] = bi0 = buffers[0];
657  to_next[1] = bi1 = buffers[1];
658  to_next[2] = bi2 = buffers[2];
659  to_next[3] = bi3 = buffers[3];
660  to_next += 4;
661  n_left_to_next -= 4;
662  buffers += 4;
663 
664  b0 = vlib_get_buffer (vm, bi0);
665  b1 = vlib_get_buffer (vm, bi1);
666  b2 = vlib_get_buffer (vm, bi2);
667  b3 = vlib_get_buffer (vm, bi3);
668 
669  vnet_buffer (b0)->sw_if_index[VLIB_RX] = mif->sw_if_index;
670  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
671  vnet_buffer (b1)->sw_if_index[VLIB_RX] = mif->sw_if_index;
672  vnet_buffer (b1)->sw_if_index[VLIB_TX] = ~0;
673  vnet_buffer (b2)->sw_if_index[VLIB_RX] = mif->sw_if_index;
674  vnet_buffer (b2)->sw_if_index[VLIB_TX] = ~0;
675  vnet_buffer (b3)->sw_if_index[VLIB_RX] = mif->sw_if_index;
676  vnet_buffer (b3)->sw_if_index[VLIB_TX] = ~0;
677 
678  if (mode == MEMIF_INTERFACE_MODE_IP)
679  {
680  next0 = memif_next_from_ip_hdr (node, b0);
681  next1 = memif_next_from_ip_hdr (node, b1);
682  next2 = memif_next_from_ip_hdr (node, b2);
683  next3 = memif_next_from_ip_hdr (node, b3);
684  }
685  else if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
686  {
687  if (PREDICT_FALSE (mif->per_interface_next_index != ~0))
688  {
689  next0 = mif->per_interface_next_index;
690  next1 = mif->per_interface_next_index;
691  next2 = mif->per_interface_next_index;
692  next3 = mif->per_interface_next_index;
693  }
694  else
695  {
696  next0 = next1 = next2 = next3 = next_index;
697  /* redirect if feature path enabled */
699  &next0, b0);
701  &next1, b1);
703  &next2, b2);
705  &next3, b3);
706  }
707  }
708 
709  /* trace */
710  if (PREDICT_FALSE (n_trace > 0))
711  {
712  memif_trace_buffer (vm, node, mif, b0, next0, qid, &n_trace);
713  if (PREDICT_FALSE (n_trace > 0))
714  memif_trace_buffer (vm, node, mif, b1, next1, qid, &n_trace);
715  if (PREDICT_FALSE (n_trace > 0))
716  memif_trace_buffer (vm, node, mif, b2, next2, qid, &n_trace);
717  if (PREDICT_FALSE (n_trace > 0))
718  memif_trace_buffer (vm, node, mif, b3, next3, qid, &n_trace);
719  }
720 
721  /* enqueue */
722  vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
723  n_left_to_next, bi0, bi1, bi2, bi3,
724  next0, next1, next2, next3);
725 
726  /* next */
727  n_from -= 4;
728  }
729  while (n_from && n_left_to_next)
730  {
731  /* enqueue buffer */
732  to_next[0] = bi0 = buffers[0];
733  to_next += 1;
734  n_left_to_next--;
735  buffers += 1;
736 
737  b0 = vlib_get_buffer (vm, bi0);
738  vnet_buffer (b0)->sw_if_index[VLIB_RX] = mif->sw_if_index;
739  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
740 
741  if (mode == MEMIF_INTERFACE_MODE_IP)
742  {
743  next0 = memif_next_from_ip_hdr (node, b0);
744  }
745  else if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
746  {
747  if (PREDICT_FALSE (mif->per_interface_next_index != ~0))
748  next0 = mif->per_interface_next_index;
749  else
750  {
751  next0 = next_index;
752  /* redirect if feature path enabled */
754  &next0, b0);
755  }
756  }
757 
758  /* trace */
759  if (PREDICT_FALSE (n_trace > 0))
760  memif_trace_buffer (vm, node, mif, b0, next0, qid, &n_trace);
761 
762  /* enqueue */
763  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
764  n_left_to_next, bi0, next0);
765 
766  /* next */
767  n_from--;
768  }
769  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
770  }
771 
773  + VNET_INTERFACE_COUNTER_RX, thread_index,
774  mif->hw_if_index, n_rx_packets,
775  n_rx_bytes);
776 
777  /* refill ring with empty buffers */
778 refill:
779  vec_reset_length (ptd->buffers);
780 
781  head = ring->head;
782  n_slots = ring_size - head + mq->last_tail;
783 
784  if (n_slots < 32)
785  goto done;
786 
787  memif_desc_t *dt = &ptd->desc_template;
788  clib_memset (dt, 0, sizeof (memif_desc_t));
789  dt->length = buffer_length;
790 
791  n_alloc = vlib_buffer_alloc_to_ring_from_pool (vm, mq->buffers, head & mask,
792  ring_size, n_slots,
793  mq->buffer_pool_index);
794 
795  if (PREDICT_FALSE (n_alloc != n_slots))
796  {
797  vlib_error_count (vm, node->node_index,
798  MEMIF_INPUT_ERROR_BUFFER_ALLOC_FAIL, 1);
799  }
800 
801  while (n_alloc >= 32)
802  {
803  bi0 = mq->buffers[(head + 4) & mask];
804  vlib_prefetch_buffer_with_index (vm, bi0, LOAD);
805  bi1 = mq->buffers[(head + 5) & mask];
806  vlib_prefetch_buffer_with_index (vm, bi1, LOAD);
807  bi2 = mq->buffers[(head + 6) & mask];
808  vlib_prefetch_buffer_with_index (vm, bi2, LOAD);
809  bi3 = mq->buffers[(head + 7) & mask];
810  vlib_prefetch_buffer_with_index (vm, bi3, LOAD);
811 
812  s0 = head++ & mask;
813  s1 = head++ & mask;
814  s2 = head++ & mask;
815  s3 = head++ & mask;
816 
817  d0 = &ring->desc[s0];
818  d1 = &ring->desc[s1];
819  d2 = &ring->desc[s2];
820  d3 = &ring->desc[s3];
821 
822  clib_memcpy_fast (d0, dt, sizeof (memif_desc_t));
823  clib_memcpy_fast (d1, dt, sizeof (memif_desc_t));
824  clib_memcpy_fast (d2, dt, sizeof (memif_desc_t));
825  clib_memcpy_fast (d3, dt, sizeof (memif_desc_t));
826 
827  b0 = vlib_get_buffer (vm, mq->buffers[s0]);
828  b1 = vlib_get_buffer (vm, mq->buffers[s1]);
829  b2 = vlib_get_buffer (vm, mq->buffers[s2]);
830  b3 = vlib_get_buffer (vm, mq->buffers[s3]);
831 
832  d0->region = b0->buffer_pool_index + 1;
833  d1->region = b1->buffer_pool_index + 1;
834  d2->region = b2->buffer_pool_index + 1;
835  d3->region = b3->buffer_pool_index + 1;
836 
837  d0->offset =
838  (void *) b0->data - mif->regions[d0->region].shm + start_offset;
839  d1->offset =
840  (void *) b1->data - mif->regions[d1->region].shm + start_offset;
841  d2->offset =
842  (void *) b2->data - mif->regions[d2->region].shm + start_offset;
843  d3->offset =
844  (void *) b3->data - mif->regions[d3->region].shm + start_offset;
845 
846  n_alloc -= 4;
847  }
848  while (n_alloc)
849  {
850  s0 = head++ & mask;
851  d0 = &ring->desc[s0];
852  clib_memcpy_fast (d0, dt, sizeof (memif_desc_t));
853  b0 = vlib_get_buffer (vm, mq->buffers[s0]);
854  d0->region = b0->buffer_pool_index + 1;
855  d0->offset =
856  (void *) b0->data - mif->regions[d0->region].shm + start_offset;
857 
858  n_alloc -= 1;
859  }
860 
862  ring->head = head;
863 
864 done:
865  return n_rx_packets;
866 }
867 
868 
870  vlib_node_runtime_t * node,
871  vlib_frame_t * frame)
872 {
873  u32 n_rx = 0;
874  memif_main_t *mm = &memif_main;
875  vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
879 
881  {
882  memif_if_t *mif;
883  mif = vec_elt_at_index (mm->interfaces, dq->dev_instance);
884  if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) &&
885  (mif->flags & MEMIF_IF_FLAG_CONNECTED))
886  {
887  if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
888  {
889  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
890  n_rx += memif_device_input_zc_inline (vm, node, frame, mif,
891  dq->queue_id, mode_ip);
892  else
893  n_rx += memif_device_input_zc_inline (vm, node, frame, mif,
894  dq->queue_id, mode_eth);
895  }
896  else if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
897  {
898  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
899  n_rx += memif_device_input_inline (vm, node, frame, mif,
900  MEMIF_RING_M2S, dq->queue_id,
901  mode_ip);
902  else
903  n_rx += memif_device_input_inline (vm, node, frame, mif,
904  MEMIF_RING_M2S, dq->queue_id,
905  mode_eth);
906  }
907  else
908  {
909  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
910  n_rx += memif_device_input_inline (vm, node, frame, mif,
911  MEMIF_RING_S2M, dq->queue_id,
912  mode_ip);
913  else
914  n_rx += memif_device_input_inline (vm, node, frame, mif,
915  MEMIF_RING_S2M, dq->queue_id,
916  mode_eth);
917  }
918  }
919  }
920 
921  return n_rx;
922 }
923 
924 /* *INDENT-OFF* */
926  .name = "memif-input",
927  .sibling_of = "device-input",
928  .format_trace = format_memif_input_trace,
929  .type = VLIB_NODE_TYPE_INPUT,
930  .state = VLIB_NODE_STATE_INTERRUPT,
931  .n_errors = MEMIF_INPUT_N_ERROR,
932  .error_strings = memif_input_error_strings,
933 };
934 
935 /* *INDENT-ON* */
936 
937 
938 /*
939  * fd.io coding-style-patch-verification: ON
940  *
941  * Local Variables:
942  * eval: (c-set-style "gnu")
943  * End:
944  */
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
vnet_device_and_queue_t * devices_and_queues
Definition: devices.h:69
#define clib_min(x, y)
Definition: clib.h:295
#define CLIB_UNUSED(x)
Definition: clib.h:82
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
static u32 vlib_get_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt)
Definition: trace_funcs.h:156
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:865
vnet_main_t * vnet_get_main(void)
Definition: misc.c:47
vnet_interface_main_t interface_main
Definition: vnet.h:56
#define PREDICT_TRUE(x)
Definition: clib.h:112
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:118
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
#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:572
u32 thread_index
Definition: main.h:197
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
memif_interface_mode_t
Definition: memif.h:53
u8 data[0]
Packet data.
Definition: buffer.h:181
#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:138
uint16_t memif_region_index_t
Definition: memif.h:60
static u32 format_get_indent(u8 *s)
Definition: format.h:72
clib_memset(h->entries, 0, sizeof(h->entries[0])*entries)
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
u8 data[128]
Definition: ipsec.api:248
#define VLIB_NODE_FN(node)
Definition: node.h:201
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:450
u32 * buffers
Definition: private.h:125
static 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:657
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:469
unsigned char u8
Definition: types.h:56
u8 buffer_pool_index
index of buffer pool this buffer belongs.
Definition: buffer.h:133
uint32_t length
Definition: memif.h:152
#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:114
#define static_always_inline
Definition: clib.h:99
#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:440
static_always_inline u32 memif_next_from_ip_hdr(vlib_node_runtime_t *node, vlib_buffer_t *b)
Definition: node.c:74
#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:824
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
u8 buffer_pool_index
Definition: private.h:126
u16 buffer_size
Definition: private.h:186
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:203
uint16_t flags
Definition: memif.h:149
#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:373
static __clib_unused char * memif_input_error_strings[]
Definition: node.c:45
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:294
memif_packet_op_t packet_ops[MEMIF_RX_VECTOR_SZ]
Definition: private.h:224
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:151
u16 last_head
Definition: private.h:123
memif_copy_op_t * copy_ops
Definition: private.h:225
vlib_node_registration_t memif_input_node
(constructor) VLIB_REGISTER_NODE (memif_input_node)
Definition: node.c:925
memif_desc_t desc[0]
Definition: memif.h:173
u32 frame_index
Definition: node.h:404
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:145
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:229
void * data
Definition: private.h:211
#define PREDICT_FALSE(x)
Definition: clib.h:111
#define foreach_memif_input_error
Definition: node.c:33
static vlib_frame_t * vlib_get_frame(vlib_main_t *vm, uword frame_index)
Definition: node_funcs.h:238
u32 node_index
Node index.
Definition: node.h:495
#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:368
u8 len
Definition: ip_types.api:49
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
vlib_main_t * vm
Definition: buffer.c:312
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:332
u16 buffer_vec_index
Definition: private.h:214
static void * vlib_frame_scalar_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:288
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:465
static_always_inline u32 sat_sub(u32 x, u32 y)
Definition: node.c:145
i16 buffer_offset
Definition: private.h:213
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
vl_api_vxlan_gbp_api_tunnel_mode_t mode
Definition: vxlan_gbp.api:44
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:539
memif_region_t * regions
Definition: private.h:172
signed int i32
Definition: types.h:77
static 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:523
memif_input_error_t
Definition: node.c:37
#define MEMIF_DESC_FLAG_NEXT
Definition: memif.h:150
struct memif_if_t::@508 run
u16 flags
Definition: node.h:386
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:153
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:57
#define foreach_device_and_queue(var, vec)
Definition: devices.h:161
Definition: defs.h:47
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
#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
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:451
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
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_always_inline u32 memif_desc_is_invalid(memif_if_t *mif, memif_desc_t *d, u32 buffer_length)
Definition: node.c:154
memif_log2_ring_size_t log2_ring_size
Definition: private.h:119
#define vnet_buffer(b)
Definition: buffer.h:369
static_always_inline void vnet_feature_start_device_input_x1(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:308
memif_per_thread_data_t * per_thread_data
Definition: private.h:248
memif_ring_type_t
Definition: memif.h:47
volatile uint16_t head
Definition: memif.h:169
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:246
static __clib_unused u8 * format_memif_input_trace(u8 *s, va_list *args)
Definition: node.c:59
static void vlib_set_trace_count(vlib_main_t *vm, vlib_node_runtime_t *rt, u32 count)
Definition: trace_funcs.h:172
#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:171
memif_interface_mode_t mode
Definition: private.h:162
Definition: defs.h:46
memif_region_size_t region_size
Definition: private.h:103
signed short i16
Definition: types.h:46