FD.io VPP  v19.01.3-6-g70449b9b9
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;
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 = &ptd->buffer_template;
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 (vm, ptd->buffers, n_buffers);
284  if (PREDICT_FALSE (n_alloc != n_buffers))
285  {
286  if (n_alloc)
287  vlib_buffer_free (vm, ptd->buffers, n_alloc);
288  vlib_error_count (vm, node->node_index,
289  MEMIF_INPUT_ERROR_BUFFER_ALLOC_FAIL, 1);
290  goto refill;
291  }
292 
293  /* copy data */
294  n_left = vec_len (ptd->copy_ops);
295  co = ptd->copy_ops;
296  while (n_left >= 8)
297  {
298  CLIB_PREFETCH (co[4].data, CLIB_CACHE_LINE_BYTES, LOAD);
299  CLIB_PREFETCH (co[5].data, CLIB_CACHE_LINE_BYTES, LOAD);
300  CLIB_PREFETCH (co[6].data, CLIB_CACHE_LINE_BYTES, LOAD);
301  CLIB_PREFETCH (co[7].data, CLIB_CACHE_LINE_BYTES, LOAD);
302 
303  b0 = vlib_get_buffer (vm, ptd->buffers[co[0].buffer_vec_index]);
304  b1 = vlib_get_buffer (vm, ptd->buffers[co[1].buffer_vec_index]);
305  b2 = vlib_get_buffer (vm, ptd->buffers[co[2].buffer_vec_index]);
306  b3 = vlib_get_buffer (vm, ptd->buffers[co[3].buffer_vec_index]);
307 
308  clib_memcpy_fast (b0->data + co[0].buffer_offset, co[0].data,
309  co[0].data_len);
310  clib_memcpy_fast (b1->data + co[1].buffer_offset, co[1].data,
311  co[1].data_len);
312  clib_memcpy_fast (b2->data + co[2].buffer_offset, co[2].data,
313  co[2].data_len);
314  clib_memcpy_fast (b3->data + co[3].buffer_offset, co[3].data,
315  co[3].data_len);
316 
317  co += 4;
318  n_left -= 4;
319  }
320  while (n_left)
321  {
322  b0 = vlib_get_buffer (vm, ptd->buffers[co[0].buffer_vec_index]);
323  clib_memcpy_fast (b0->data + co[0].buffer_offset, co[0].data,
324  co[0].data_len);
325  co += 1;
326  n_left -= 1;
327  }
328 
329  /* release slots from the ring */
330  if (type == MEMIF_RING_S2M)
331  {
333  ring->tail = mq->last_head = cur_slot;
334  }
335  else
336  {
337  mq->last_tail = cur_slot;
338  }
339 
340  /* prepare buffer template and next indices */
341  vnet_buffer (bt)->sw_if_index[VLIB_RX] = mif->sw_if_index;
342  vnet_buffer (bt)->feature_arc_index = 0;
343  bt->current_data = start_offset;
344  bt->current_config_index = 0;
345 
346  if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
347  {
349  if (mif->per_interface_next_index != ~0)
350  next_index = mif->per_interface_next_index;
351  else
353  bt);
354 
355  vlib_get_new_next_frame (vm, node, next_index, to_next_bufs,
356  n_left_to_next);
358  {
359  vlib_next_frame_t *nf;
360  vlib_frame_t *f;
362  nf = vlib_node_runtime_get_next_frame (vm, node, next_index);
363  f = vlib_get_frame (vm, nf->frame);
365 
366  ef = vlib_frame_scalar_args (f);
367  ef->sw_if_index = mif->sw_if_index;
368  ef->hw_if_index = mif->hw_if_index;
370  }
371  }
372 
373  /* process buffer metadata */
374  u32 n_from = n_rx_packets;
375  po = ptd->packet_ops;
376  bi = to_next_bufs;
377 
378  while (n_from >= 8)
379  {
380  b0 = vlib_get_buffer (vm, po[4].first_buffer_vec_index);
381  b1 = vlib_get_buffer (vm, po[5].first_buffer_vec_index);
382  b2 = vlib_get_buffer (vm, po[6].first_buffer_vec_index);
383  b3 = vlib_get_buffer (vm, po[7].first_buffer_vec_index);
384  vlib_prefetch_buffer_header (b0, STORE);
385  vlib_prefetch_buffer_header (b1, STORE);
386  vlib_prefetch_buffer_header (b2, STORE);
387  vlib_prefetch_buffer_header (b3, STORE);
388 
389  /* enqueue buffer */
390  u32 fbvi[4];
391  fbvi[0] = po[0].first_buffer_vec_index;
392  fbvi[1] = po[1].first_buffer_vec_index;
393  fbvi[2] = po[2].first_buffer_vec_index;
394  fbvi[3] = po[3].first_buffer_vec_index;
395 
396  bi[0] = ptd->buffers[fbvi[0]];
397  bi[1] = ptd->buffers[fbvi[1]];
398  bi[2] = ptd->buffers[fbvi[2]];
399  bi[3] = ptd->buffers[fbvi[3]];
400 
401  b0 = vlib_get_buffer (vm, bi[0]);
402  b1 = vlib_get_buffer (vm, bi[1]);
403  b2 = vlib_get_buffer (vm, bi[2]);
404  b3 = vlib_get_buffer (vm, bi[3]);
405 
406  clib_memcpy64_x4 (b0, b1, b2, b3, bt);
407 
408  b0->current_length = po[0].packet_len;
409  n_rx_bytes += b0->current_length;
410  b1->current_length = po[1].packet_len;
411  n_rx_bytes += b1->current_length;
412  b2->current_length = po[2].packet_len;
413  n_rx_bytes += b2->current_length;
414  b3->current_length = po[3].packet_len;
415  n_rx_bytes += b3->current_length;
416 
417  memif_add_to_chain (vm, b0, ptd->buffers + fbvi[0] + 1, buffer_size);
418  memif_add_to_chain (vm, b1, ptd->buffers + fbvi[1] + 1, buffer_size);
419  memif_add_to_chain (vm, b2, ptd->buffers + fbvi[2] + 1, buffer_size);
420  memif_add_to_chain (vm, b3, ptd->buffers + fbvi[3] + 1, buffer_size);
421 
422  if (mode == MEMIF_INTERFACE_MODE_IP)
423  {
424  next[0] = memif_next_from_ip_hdr (node, b0);
425  next[1] = memif_next_from_ip_hdr (node, b1);
426  next[2] = memif_next_from_ip_hdr (node, b2);
427  next[3] = memif_next_from_ip_hdr (node, b3);
428  }
429 
430  /* next */
431  n_from -= 4;
432  po += 4;
433  bi += 4;
434  next += 4;
435  }
436  while (n_from)
437  {
438  u32 fbvi[4];
439  /* enqueue buffer */
440  fbvi[0] = po[0].first_buffer_vec_index;
441  bi[0] = ptd->buffers[fbvi[0]];
442  b0 = vlib_get_buffer (vm, bi[0]);
443  clib_memcpy_fast (b0, bt, 64);
444  b0->current_length = po->packet_len;
445  n_rx_bytes += b0->current_length;
446 
447  memif_add_to_chain (vm, b0, ptd->buffers + fbvi[0] + 1, buffer_size);
448 
449  if (mode == MEMIF_INTERFACE_MODE_IP)
450  {
451  next[0] = memif_next_from_ip_hdr (node, b0);
452  }
453 
454  /* next */
455  n_from -= 1;
456  po += 1;
457  bi += 1;
458  next += 1;
459  }
460 
461  /* packet trace if enabled */
462  if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
463  {
464  u32 n_left = n_rx_packets;
465  bi = to_next_bufs;
466  next = nexts;
467  u32 ni = next_index;
468  while (n_trace && n_left)
469  {
470  vlib_buffer_t *b;
472  if (mode != MEMIF_INTERFACE_MODE_ETHERNET)
473  ni = next[0];
474  b = vlib_get_buffer (vm, bi[0]);
475  vlib_trace_buffer (vm, node, ni, b, /* follow_chain */ 0);
476  tr = vlib_add_trace (vm, node, b, sizeof (*tr));
477  tr->next_index = ni;
478  tr->hw_if_index = mif->hw_if_index;
479  tr->ring = qid;
480 
481  /* next */
482  n_trace--;
483  n_left--;
484  bi++;
485  next++;
486  }
487  vlib_set_trace_count (vm, node, n_trace);
488  }
489 
490  if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
491  {
492  n_left_to_next -= n_rx_packets;
493  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
494  }
495  else
496  vlib_buffer_enqueue_to_next (vm, node, to_next_bufs, nexts, n_rx_packets);
497 
499  + VNET_INTERFACE_COUNTER_RX, thread_index,
500  mif->hw_if_index, n_rx_packets,
501  n_rx_bytes);
502 
503  /* refill ring with empty buffers */
504 refill:
505  vec_reset_length (ptd->buffers);
506  vec_reset_length (ptd->copy_ops);
507 
508  if (type == MEMIF_RING_M2S)
509  {
510  u16 head = ring->head;
511  n_slots = ring_size - head + mq->last_tail;
512 
513  while (n_slots--)
514  {
515  u16 s = head++ & mask;
516  memif_desc_t *d = &ring->desc[s];
517  d->length = mif->run.buffer_size;
518  }
519 
521  ring->head = head;
522  }
523 
524  return n_rx_packets;
525 }
526 
529  vlib_frame_t * frame, memif_if_t * mif,
531 {
532  vnet_main_t *vnm = vnet_get_main ();
533  memif_main_t *mm = &memif_main;
534  memif_ring_t *ring;
535  memif_queue_t *mq;
536  u32 next_index;
537  uword n_trace = vlib_get_trace_count (vm, node);
538  u32 n_rx_packets = 0, n_rx_bytes = 0;
539  u32 *to_next = 0, *buffers;
540  u32 bi0, bi1, bi2, bi3;
541  u16 s0, s1, s2, s3;
542  memif_desc_t *d0, *d1, *d2, *d3;
543  vlib_buffer_t *b0, *b1, *b2, *b3;
544  u32 thread_index = vm->thread_index;
546  thread_index);
547  u16 cur_slot, last_slot, ring_size, n_slots, mask, head;
548  i16 start_offset;
549  u32 buffer_length;
550  u16 n_alloc, n_from;
551 
552  mq = vec_elt_at_index (mif->rx_queues, qid);
553  ring = mq->ring;
554  ring_size = 1 << mq->log2_ring_size;
555  mask = ring_size - 1;
556 
557  next_index = (mode == MEMIF_INTERFACE_MODE_IP) ?
559 
560  /* asume that somebody will want to add ethernet header on the packet
561  so start with IP header at offset 14 */
562  start_offset = (mode == MEMIF_INTERFACE_MODE_IP) ? 14 : 0;
563  buffer_length = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES - start_offset;
564 
565  cur_slot = mq->last_tail;
566  last_slot = ring->tail;
567  if (cur_slot == last_slot)
568  goto refill;
569  n_slots = last_slot - cur_slot;
570 
571  /* process ring slots */
574  while (n_slots && n_rx_packets < MEMIF_RX_VECTOR_SZ)
575  {
576  vlib_buffer_t *hb;
577 
578  s0 = cur_slot & mask;
579  bi0 = mq->buffers[s0];
580  ptd->buffers[n_rx_packets++] = bi0;
581 
582  CLIB_PREFETCH (&ring->desc[(cur_slot + 8) & mask],
583  CLIB_CACHE_LINE_BYTES, LOAD);
584  d0 = &ring->desc[s0];
585  hb = b0 = vlib_get_buffer (vm, bi0);
586  b0->current_data = start_offset;
587  b0->current_length = start_offset + d0->length;
588  n_rx_bytes += d0->length;
589 
590  if (0 && memif_desc_is_invalid (mif, d0, buffer_length))
591  return 0;
592 
593  cur_slot++;
594  n_slots--;
595  if (PREDICT_FALSE ((d0->flags & MEMIF_DESC_FLAG_NEXT) && n_slots))
596  {
597  hb->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
598  next_slot:
599  s0 = cur_slot & mask;
600  d0 = &ring->desc[s0];
601  bi0 = mq->buffers[s0];
602 
603  /* previous buffer */
604  b0->next_buffer = bi0;
605  b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
606 
607  /* current buffer */
608  b0 = vlib_get_buffer (vm, bi0);
609  b0->current_data = start_offset;
610  b0->current_length = start_offset + d0->length;
612  n_rx_bytes += d0->length;
613 
614  cur_slot++;
615  n_slots--;
616  if ((d0->flags & MEMIF_DESC_FLAG_NEXT) && n_slots)
617  goto next_slot;
618  }
619  }
620 
621  /* release slots from the ring */
622  mq->last_tail = cur_slot;
623 
624  n_from = n_rx_packets;
625  buffers = ptd->buffers;
626 
627  while (n_from)
628  {
629  u32 n_left_to_next;
630  u32 next0, next1, next2, next3;
631 
632  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
633  while (n_from >= 8 && n_left_to_next >= 4)
634  {
635  b0 = vlib_get_buffer (vm, buffers[4]);
636  b1 = vlib_get_buffer (vm, buffers[5]);
637  b2 = vlib_get_buffer (vm, buffers[6]);
638  b3 = vlib_get_buffer (vm, buffers[7]);
639  vlib_prefetch_buffer_header (b0, STORE);
640  vlib_prefetch_buffer_header (b1, STORE);
641  vlib_prefetch_buffer_header (b2, STORE);
642  vlib_prefetch_buffer_header (b3, STORE);
643 
644  /* enqueue buffer */
645  to_next[0] = bi0 = buffers[0];
646  to_next[1] = bi1 = buffers[1];
647  to_next[2] = bi2 = buffers[2];
648  to_next[3] = bi3 = buffers[3];
649  to_next += 4;
650  n_left_to_next -= 4;
651  buffers += 4;
652 
653  b0 = vlib_get_buffer (vm, bi0);
654  b1 = vlib_get_buffer (vm, bi1);
655  b2 = vlib_get_buffer (vm, bi2);
656  b3 = vlib_get_buffer (vm, bi3);
657 
658  vnet_buffer (b0)->sw_if_index[VLIB_RX] = mif->sw_if_index;
659  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
660  vnet_buffer (b1)->sw_if_index[VLIB_RX] = mif->sw_if_index;
661  vnet_buffer (b1)->sw_if_index[VLIB_TX] = ~0;
662  vnet_buffer (b2)->sw_if_index[VLIB_RX] = mif->sw_if_index;
663  vnet_buffer (b2)->sw_if_index[VLIB_TX] = ~0;
664  vnet_buffer (b3)->sw_if_index[VLIB_RX] = mif->sw_if_index;
665  vnet_buffer (b3)->sw_if_index[VLIB_TX] = ~0;
666 
667  if (mode == MEMIF_INTERFACE_MODE_IP)
668  {
669  next0 = memif_next_from_ip_hdr (node, b0);
670  next1 = memif_next_from_ip_hdr (node, b1);
671  next2 = memif_next_from_ip_hdr (node, b2);
672  next3 = memif_next_from_ip_hdr (node, b3);
673  }
674  else if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
675  {
676  if (PREDICT_FALSE (mif->per_interface_next_index != ~0))
677  {
678  next0 = mif->per_interface_next_index;
679  next1 = mif->per_interface_next_index;
680  next2 = mif->per_interface_next_index;
681  next3 = mif->per_interface_next_index;
682  }
683  else
684  {
685  next0 = next1 = next2 = next3 = next_index;
686  /* redirect if feature path enabled */
688  &next0, b0);
690  &next1, b1);
692  &next2, b2);
694  &next3, b3);
695  }
696  }
697 
698  /* trace */
699  if (PREDICT_FALSE (n_trace > 0))
700  {
701  memif_trace_buffer (vm, node, mif, b0, next0, qid, &n_trace);
702  if (PREDICT_FALSE (n_trace > 0))
703  memif_trace_buffer (vm, node, mif, b1, next1, qid, &n_trace);
704  if (PREDICT_FALSE (n_trace > 0))
705  memif_trace_buffer (vm, node, mif, b2, next2, qid, &n_trace);
706  if (PREDICT_FALSE (n_trace > 0))
707  memif_trace_buffer (vm, node, mif, b3, next3, qid, &n_trace);
708  }
709 
710  /* enqueue */
711  vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
712  n_left_to_next, bi0, bi1, bi2, bi3,
713  next0, next1, next2, next3);
714 
715  /* next */
716  n_from -= 4;
717  }
718  while (n_from && n_left_to_next)
719  {
720  /* enqueue buffer */
721  to_next[0] = bi0 = buffers[0];
722  to_next += 1;
723  n_left_to_next--;
724  buffers += 1;
725 
726  b0 = vlib_get_buffer (vm, bi0);
727  vnet_buffer (b0)->sw_if_index[VLIB_RX] = mif->sw_if_index;
728  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
729 
730  if (mode == MEMIF_INTERFACE_MODE_IP)
731  {
732  next0 = memif_next_from_ip_hdr (node, b0);
733  }
734  else if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
735  {
736  if (PREDICT_FALSE (mif->per_interface_next_index != ~0))
737  next0 = mif->per_interface_next_index;
738  else
739  {
740  next0 = next_index;
741  /* redirect if feature path enabled */
743  &next0, b0);
744  }
745  }
746 
747  /* trace */
748  if (PREDICT_FALSE (n_trace > 0))
749  memif_trace_buffer (vm, node, mif, b0, next0, qid, &n_trace);
750 
751  /* enqueue */
752  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
753  n_left_to_next, bi0, next0);
754 
755  /* next */
756  n_from--;
757  }
758  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
759  }
760 
762  + VNET_INTERFACE_COUNTER_RX, thread_index,
763  mif->hw_if_index, n_rx_packets,
764  n_rx_bytes);
765 
766  /* refill ring with empty buffers */
767 refill:
768  vec_reset_length (ptd->buffers);
769 
770  head = ring->head;
771  n_slots = ring_size - head + mq->last_tail;
772 
773  if (n_slots < 32)
774  goto done;
775 
776  memif_desc_t *dt = &ptd->desc_template;
777  clib_memset (dt, 0, sizeof (memif_desc_t));
778  dt->length = buffer_length;
779 
780  n_alloc = vlib_buffer_alloc_to_ring (vm, mq->buffers, head & mask,
781  ring_size, n_slots);
782 
783  if (PREDICT_FALSE (n_alloc != n_slots))
784  {
785  vlib_error_count (vm, node->node_index,
786  MEMIF_INPUT_ERROR_BUFFER_ALLOC_FAIL, 1);
787  }
788 
789  while (n_alloc >= 32)
790  {
791  bi0 = mq->buffers[(head + 4) & mask];
792  vlib_prefetch_buffer_with_index (vm, bi0, LOAD);
793  bi1 = mq->buffers[(head + 5) & mask];
794  vlib_prefetch_buffer_with_index (vm, bi1, LOAD);
795  bi2 = mq->buffers[(head + 6) & mask];
796  vlib_prefetch_buffer_with_index (vm, bi2, LOAD);
797  bi3 = mq->buffers[(head + 7) & mask];
798  vlib_prefetch_buffer_with_index (vm, bi3, LOAD);
799 
800  s0 = head++ & mask;
801  s1 = head++ & mask;
802  s2 = head++ & mask;
803  s3 = head++ & mask;
804 
805  d0 = &ring->desc[s0];
806  d1 = &ring->desc[s1];
807  d2 = &ring->desc[s2];
808  d3 = &ring->desc[s3];
809 
810  clib_memcpy_fast (d0, dt, sizeof (memif_desc_t));
811  clib_memcpy_fast (d1, dt, sizeof (memif_desc_t));
812  clib_memcpy_fast (d2, dt, sizeof (memif_desc_t));
813  clib_memcpy_fast (d3, dt, sizeof (memif_desc_t));
814 
815  b0 = vlib_get_buffer (vm, mq->buffers[s0]);
816  b1 = vlib_get_buffer (vm, mq->buffers[s1]);
817  b2 = vlib_get_buffer (vm, mq->buffers[s2]);
818  b3 = vlib_get_buffer (vm, mq->buffers[s3]);
819 
820  d0->region = b0->buffer_pool_index + 1;
821  d1->region = b1->buffer_pool_index + 1;
822  d2->region = b2->buffer_pool_index + 1;
823  d3->region = b3->buffer_pool_index + 1;
824 
825  d0->offset =
826  (void *) b0->data - mif->regions[d0->region].shm + start_offset;
827  d1->offset =
828  (void *) b1->data - mif->regions[d1->region].shm + start_offset;
829  d2->offset =
830  (void *) b2->data - mif->regions[d2->region].shm + start_offset;
831  d3->offset =
832  (void *) b3->data - mif->regions[d3->region].shm + start_offset;
833 
834  n_alloc -= 4;
835  }
836  while (n_alloc)
837  {
838  s0 = head++ & mask;
839  d0 = &ring->desc[s0];
840  clib_memcpy_fast (d0, dt, sizeof (memif_desc_t));
841  b0 = vlib_get_buffer (vm, mq->buffers[s0]);
842  d0->region = b0->buffer_pool_index + 1;
843  d0->offset =
844  (void *) b0->data - mif->regions[d0->region].shm + start_offset;
845 
846  n_alloc -= 1;
847  }
848 
850  ring->head = head;
851 
852 done:
853  return n_rx_packets;
854 }
855 
856 
858  vlib_node_runtime_t * node,
859  vlib_frame_t * frame)
860 {
861  u32 n_rx = 0;
862  memif_main_t *mm = &memif_main;
863  vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
867 
869  {
870  memif_if_t *mif;
871  mif = vec_elt_at_index (mm->interfaces, dq->dev_instance);
872  if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) &&
873  (mif->flags & MEMIF_IF_FLAG_CONNECTED))
874  {
875  if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
876  {
877  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
878  n_rx += memif_device_input_zc_inline (vm, node, frame, mif,
879  dq->queue_id, mode_ip);
880  else
881  n_rx += memif_device_input_zc_inline (vm, node, frame, mif,
882  dq->queue_id, mode_eth);
883  }
884  else if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
885  {
886  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
887  n_rx += memif_device_input_inline (vm, node, frame, mif,
888  MEMIF_RING_M2S, dq->queue_id,
889  mode_ip);
890  else
891  n_rx += memif_device_input_inline (vm, node, frame, mif,
892  MEMIF_RING_M2S, dq->queue_id,
893  mode_eth);
894  }
895  else
896  {
897  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
898  n_rx += memif_device_input_inline (vm, node, frame, mif,
899  MEMIF_RING_S2M, dq->queue_id,
900  mode_ip);
901  else
902  n_rx += memif_device_input_inline (vm, node, frame, mif,
903  MEMIF_RING_S2M, dq->queue_id,
904  mode_eth);
905  }
906  }
907  }
908 
909  return n_rx;
910 }
911 
912 /* *INDENT-OFF* */
914  .name = "memif-input",
915  .sibling_of = "device-input",
916  .format_trace = format_memif_input_trace,
917  .type = VLIB_NODE_TYPE_INPUT,
918  .state = VLIB_NODE_STATE_INTERRUPT,
919  .n_errors = MEMIF_INPUT_N_ERROR,
920  .error_strings = memif_input_error_strings,
921 };
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  */
memif_if_t * interfaces
Definition: private.h:240
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
struct memif_if_t::@498 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:529
u32 current_config_index
Used by feature subgraph arcs to visit enabled feature nodes.
Definition: buffer.h:136
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
#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
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
#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:576
u32 thread_index
Definition: main.h:179
memif_interface_mode_t
Definition: memif.h:53
static vlib_frame_t * vlib_get_frame(vlib_main_t *vm, vlib_frame_t *f)
Definition: node_funcs.h:216
u8 buffer_pool_index
index of buffer pool this buffer belongs.
Definition: buffer.h:144
#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
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:419
#define VLIB_NODE_FN(node)
Definition: node.h:201
u16 first_buffer_vec_index
Definition: private.h:204
#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
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:493
unsigned char u8
Definition: types.h:56
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
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
#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:341
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:51
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:839
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
u16 buffer_size
Definition: private.h:185
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:188
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:163
#define vlib_get_new_next_frame(vm, node, next_index, vectors, n_vectors_left)
Definition: node_funcs.h:343
#define VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES
Definition: buffer.h:443
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:264
memif_packet_op_t packet_ops[MEMIF_RX_VECTOR_SZ]
Definition: private.h:223
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:224
vlib_node_registration_t memif_input_node
(constructor) VLIB_REGISTER_NODE (memif_input_node)
Definition: node.c:913
memif_desc_t desc[0]
Definition: memif.h:173
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:114
uword dev_instance
Definition: private.h:160
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:214
void * data
Definition: private.h:210
#define PREDICT_FALSE(x)
Definition: clib.h:111
#define foreach_memif_input_error
Definition: node.c:33
u32 node_index
Node index.
Definition: node.h:518
#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:338
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:139
u8 len
Definition: ip_types.api:49
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:169
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:79
vlib_main_t * vm
Definition: buffer.c:301
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:213
static_always_inline void clib_memcpy64_x4(void *d0, void *d1, void *d2, void *d3, void *s)
Definition: string.h:224
static void * vlib_frame_scalar_args(vlib_frame_t *f)
Get pointer to frame scalar data.
Definition: node_funcs.h:258
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:452
static_always_inline u32 sat_sub(u32 x, u32 y)
Definition: node.c:145
i16 buffer_offset
Definition: private.h:212
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
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:528
memif_region_t * regions
Definition: private.h:171
signed int i32
Definition: types.h:77
memif_input_error_t
Definition: node.c:37
#define MEMIF_DESC_FLAG_NEXT
Definition: memif.h:150
vlib_frame_t * frame
Definition: node.h:429
u16 flags
Definition: node.h:411
u32 flags
Definition: private.h:156
memif_ring_t * ring
Definition: private.h:118
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:130
vlib_buffer_t buffer_template
Definition: private.h:228
u32 hw_if_index
Definition: private.h:158
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
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:156
#define foreach_device_and_queue(var, vec)
Definition: devices.h:161
Definition: defs.h:47
static u32 vlib_buffer_alloc_to_ring(vlib_main_t *vm, u32 *ring, u32 start, u32 ring_size, u32 n_buffers)
Allocate buffers into ring.
Definition: buffer_funcs.h:502
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:216
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:495
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:368
vl_api_gbp_vxlan_tunnel_mode_t mode
Definition: gbp.api:349
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:247
u8 data[0]
Packet data.
Definition: buffer.h:176
memif_ring_type_t
Definition: memif.h:47
volatile uint16_t head
Definition: memif.h:169
memif_queue_t * rx_queues
Definition: private.h:173
memif_desc_t desc_template
Definition: private.h:229
static void vlib_frame_no_append(vlib_frame_t *f)
Definition: node_funcs.h:224
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 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:117
memif_main_t memif_main
Definition: memif.c:43
static u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:485
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:62
u32 sw_if_index
Definition: private.h:159
volatile uint16_t tail
Definition: memif.h:171
memif_interface_mode_t mode
Definition: private.h:161
Definition: defs.h:46
memif_region_size_t region_size
Definition: private.h:103
signed short i16
Definition: types.h:46