FD.io VPP  v18.10-34-gcce845e
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  u32 n_rx_packets = 0, n_rx_bytes = 0;
186  u32 n_left;
187  vlib_buffer_t *b0, *b1, *b2, *b3;
188  u32 thread_index = vm->thread_index;
190  thread_index);
191  vlib_buffer_t *bt = &ptd->buffer_template;
192  u16 cur_slot, last_slot, ring_size, n_slots, mask;
193  i16 start_offset;
194  u16 n_buffers = 0, n_alloc;
195  memif_copy_op_t *co;
196  memif_packet_op_t *po;
197  memif_region_index_t last_region = ~0;
198  void *last_region_shm = 0;
199 
200  mq = vec_elt_at_index (mif->rx_queues, qid);
201  ring = mq->ring;
202  ring_size = 1 << mq->log2_ring_size;
203  mask = ring_size - 1;
204 
205  /* asume that somebody will want to add ethernet header on the packet
206  so start with IP header at offset 14 */
207  start_offset = (mode == MEMIF_INTERFACE_MODE_IP) ? 14 : 0;
208 
209  /* for S2M rings, we are consumers of packet buffers, and for M2S rings we
210  are producers of empty buffers */
211  cur_slot = (type == MEMIF_RING_S2M) ? mq->last_head : mq->last_tail;
212  last_slot = (type == MEMIF_RING_S2M) ? ring->head : ring->tail;
213  if (cur_slot == last_slot)
214  goto refill;
215  n_slots = last_slot - cur_slot;
216 
217  /* construct copy and packet vector out of ring slots */
218  while (n_slots && n_rx_packets < MEMIF_RX_VECTOR_SZ)
219  {
220  u32 dst_off, src_off, n_bytes_left;
221  u16 s0;
222  memif_desc_t *d0;
223  void *mb0;
224  po = ptd->packet_ops + n_rx_packets;
225  n_rx_packets++;
226  po->first_buffer_vec_index = n_buffers++;
227  po->packet_len = 0;
228  src_off = 0;
229  dst_off = start_offset;
230 
231  next_slot:
232  CLIB_PREFETCH (&ring->desc[(cur_slot + 8) & mask],
233  CLIB_CACHE_LINE_BYTES, LOAD);
234  s0 = cur_slot & mask;
235  d0 = &ring->desc[s0];
236  n_bytes_left = d0->length;
237 
238  /* slave resets buffer length,
239  * so it can produce full size buffer for master
240  */
241  if (type == MEMIF_RING_M2S)
242  d0->length = mif->run.buffer_size;
243 
244  po->packet_len += n_bytes_left;
245  if (PREDICT_FALSE (last_region != d0->region))
246  {
247  last_region_shm = mif->regions[d0->region].shm;
248  last_region = d0->region;
249  }
250  mb0 = last_region_shm + d0->offset;
251 
252  do
253  {
254  u32 dst_free = buffer_size - dst_off;
255  if (dst_free == 0)
256  {
257  dst_off = 0;
258  dst_free = buffer_size;
259  n_buffers++;
260  }
261  u32 bytes_to_copy = clib_min (dst_free, n_bytes_left);
262  memif_add_copy_op (ptd, mb0 + src_off, bytes_to_copy, dst_off,
263  n_buffers - 1);
264  n_bytes_left -= bytes_to_copy;
265  src_off += bytes_to_copy;
266  dst_off += bytes_to_copy;
267  }
268  while (PREDICT_FALSE (n_bytes_left));
269 
270  cur_slot++;
271  n_slots--;
272  if ((d0->flags & MEMIF_DESC_FLAG_NEXT) && n_slots)
273  {
274  src_off = 0;
275  goto next_slot;
276  }
277  }
278 
279  /* allocate free buffers */
280  vec_validate_aligned (ptd->buffers, n_buffers - 1, CLIB_CACHE_LINE_BYTES);
281  n_alloc = vlib_buffer_alloc (vm, ptd->buffers, n_buffers);
282  if (PREDICT_FALSE (n_alloc != n_buffers))
283  {
284  if (n_alloc)
285  vlib_buffer_free (vm, ptd->buffers, n_alloc);
286  vlib_error_count (vm, node->node_index,
287  MEMIF_INPUT_ERROR_BUFFER_ALLOC_FAIL, 1);
288  goto refill;
289  }
290 
291  /* copy data */
292  n_left = vec_len (ptd->copy_ops);
293  co = ptd->copy_ops;
294  while (n_left >= 8)
295  {
296  CLIB_PREFETCH (co[4].data, CLIB_CACHE_LINE_BYTES, LOAD);
297  CLIB_PREFETCH (co[5].data, CLIB_CACHE_LINE_BYTES, LOAD);
298  CLIB_PREFETCH (co[6].data, CLIB_CACHE_LINE_BYTES, LOAD);
299  CLIB_PREFETCH (co[7].data, CLIB_CACHE_LINE_BYTES, LOAD);
300 
301  b0 = vlib_get_buffer (vm, ptd->buffers[co[0].buffer_vec_index]);
302  b1 = vlib_get_buffer (vm, ptd->buffers[co[1].buffer_vec_index]);
303  b2 = vlib_get_buffer (vm, ptd->buffers[co[2].buffer_vec_index]);
304  b3 = vlib_get_buffer (vm, ptd->buffers[co[3].buffer_vec_index]);
305 
306  clib_memcpy (b0->data + co[0].buffer_offset, co[0].data,
307  co[0].data_len);
308  clib_memcpy (b1->data + co[1].buffer_offset, co[1].data,
309  co[1].data_len);
310  clib_memcpy (b2->data + co[2].buffer_offset, co[2].data,
311  co[2].data_len);
312  clib_memcpy (b3->data + co[3].buffer_offset, co[3].data,
313  co[3].data_len);
314 
315  co += 4;
316  n_left -= 4;
317  }
318  while (n_left)
319  {
320  b0 = vlib_get_buffer (vm, ptd->buffers[co[0].buffer_vec_index]);
321  clib_memcpy (b0->data + co[0].buffer_offset, co[0].data,
322  co[0].data_len);
323  co += 1;
324  n_left -= 1;
325  }
326 
327  /* release slots from the ring */
328  if (type == MEMIF_RING_S2M)
329  {
331  ring->tail = mq->last_head = cur_slot;
332  }
333  else
334  {
335  mq->last_tail = cur_slot;
336  }
337 
338  u16 nexts[MEMIF_RX_VECTOR_SZ], *next = nexts;
339  u32 to_next_buffers[MEMIF_RX_VECTOR_SZ], *bi = to_next_buffers;
340 
341  /* prepare buffer template and next indices */
342  vnet_buffer (bt)->sw_if_index[VLIB_RX] = mif->sw_if_index;
343  vnet_buffer (bt)->feature_arc_index = 0;
344  bt->current_data = start_offset;
345  bt->current_config_index = 0;
346 
347  if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
348  {
350  if (mif->per_interface_next_index != ~0)
351  next_index = mif->per_interface_next_index;
352  else
354  bt);
355  clib_memset_u16 (nexts, next_index, n_rx_packets);
356  }
357 
358  /* process buffer metadata */
359  u32 n_from = n_rx_packets;
360  po = ptd->packet_ops;
361 
362  while (n_from >= 8)
363  {
364  b0 = vlib_get_buffer (vm, po[4].first_buffer_vec_index);
365  b1 = vlib_get_buffer (vm, po[5].first_buffer_vec_index);
366  b2 = vlib_get_buffer (vm, po[6].first_buffer_vec_index);
367  b3 = vlib_get_buffer (vm, po[7].first_buffer_vec_index);
368  vlib_prefetch_buffer_header (b0, STORE);
369  vlib_prefetch_buffer_header (b1, STORE);
370  vlib_prefetch_buffer_header (b2, STORE);
371  vlib_prefetch_buffer_header (b3, STORE);
372 
373  /* enqueue buffer */
374  u32 fbvi[4];
375  fbvi[0] = po[0].first_buffer_vec_index;
376  fbvi[1] = po[1].first_buffer_vec_index;
377  fbvi[2] = po[2].first_buffer_vec_index;
378  fbvi[3] = po[3].first_buffer_vec_index;
379 
380  bi[0] = ptd->buffers[fbvi[0]];
381  bi[1] = ptd->buffers[fbvi[1]];
382  bi[2] = ptd->buffers[fbvi[2]];
383  bi[3] = ptd->buffers[fbvi[3]];
384 
385  b0 = vlib_get_buffer (vm, bi[0]);
386  b1 = vlib_get_buffer (vm, bi[1]);
387  b2 = vlib_get_buffer (vm, bi[2]);
388  b3 = vlib_get_buffer (vm, bi[3]);
389 
390  clib_memcpy64_x4 (b0, b1, b2, b3, bt);
391 
392  b0->current_length = po[0].packet_len;
393  n_rx_bytes += b0->current_length;
394  b1->current_length = po[1].packet_len;
395  n_rx_bytes += b1->current_length;
396  b2->current_length = po[2].packet_len;
397  n_rx_bytes += b2->current_length;
398  b3->current_length = po[3].packet_len;
399  n_rx_bytes += b3->current_length;
400 
401  memif_add_to_chain (vm, b0, ptd->buffers + fbvi[0] + 1, buffer_size);
402  memif_add_to_chain (vm, b1, ptd->buffers + fbvi[1] + 1, buffer_size);
403  memif_add_to_chain (vm, b2, ptd->buffers + fbvi[2] + 1, buffer_size);
404  memif_add_to_chain (vm, b3, ptd->buffers + fbvi[3] + 1, buffer_size);
405 
406  if (mode == MEMIF_INTERFACE_MODE_IP)
407  {
408  next[0] = memif_next_from_ip_hdr (node, b0);
409  next[1] = memif_next_from_ip_hdr (node, b1);
410  next[2] = memif_next_from_ip_hdr (node, b2);
411  next[3] = memif_next_from_ip_hdr (node, b3);
412  }
413 
414  /* next */
415  n_from -= 4;
416  po += 4;
417  bi += 4;
418  next += 4;
419  }
420  while (n_from)
421  {
422  u32 fbvi[4];
423  /* enqueue buffer */
424  fbvi[0] = po[0].first_buffer_vec_index;
425  bi[0] = ptd->buffers[fbvi[0]];
426  b0 = vlib_get_buffer (vm, bi[0]);
427  clib_memcpy (b0, bt, 64);
428  b0->current_length = po->packet_len;
429  n_rx_bytes += b0->current_length;
430 
431  memif_add_to_chain (vm, b0, ptd->buffers + fbvi[0] + 1, buffer_size);
432 
433  if (mode == MEMIF_INTERFACE_MODE_IP)
434  {
435  next[0] = memif_next_from_ip_hdr (node, b0);
436  }
437 
438  /* next */
439  n_from -= 1;
440  po += 1;
441  bi += 1;
442  next += 1;
443  }
444 
445  /* packet trace if enabled */
446  if (PREDICT_FALSE ((n_trace = vlib_get_trace_count (vm, node))))
447  {
448  u32 n_left = n_rx_packets;
449  bi = to_next_buffers;
450  next = nexts;
451  while (n_trace && n_left)
452  {
453  vlib_buffer_t *b;
455  b = vlib_get_buffer (vm, bi[0]);
456  vlib_trace_buffer (vm, node, next[0], b, /* follow_chain */ 0);
457  tr = vlib_add_trace (vm, node, b, sizeof (*tr));
458  tr->next_index = next[0];
459  tr->hw_if_index = mif->hw_if_index;
460  tr->ring = qid;
461 
462  /* next */
463  n_trace--;
464  n_left--;
465  bi++;
466  next++;
467  }
468  vlib_set_trace_count (vm, node, n_trace);
469  }
470 
471  vlib_buffer_enqueue_to_next (vm, node, to_next_buffers, nexts,
472  n_rx_packets);
473 
475  + VNET_INTERFACE_COUNTER_RX, thread_index,
476  mif->hw_if_index, n_rx_packets,
477  n_rx_bytes);
478 
479  /* refill ring with empty buffers */
480 refill:
481  vec_reset_length (ptd->buffers);
482  vec_reset_length (ptd->copy_ops);
483 
484  if (type == MEMIF_RING_M2S)
485  {
486  u16 head = ring->head;
487  n_slots = ring_size - head + mq->last_tail;
488 
489  while (n_slots--)
490  {
491  u16 s = head++ & mask;
492  memif_desc_t *d = &ring->desc[s];
493  d->length = mif->run.buffer_size;
494  }
495 
497  ring->head = head;
498  }
499 
500  return n_rx_packets;
501 }
502 
505  vlib_frame_t * frame, memif_if_t * mif,
506  u16 qid, memif_interface_mode_t mode)
507 {
508  vnet_main_t *vnm = vnet_get_main ();
509  memif_main_t *mm = &memif_main;
510  memif_ring_t *ring;
511  memif_queue_t *mq;
512  u32 next_index;
513  uword n_trace = vlib_get_trace_count (vm, node);
514  u32 n_rx_packets = 0, n_rx_bytes = 0;
515  u32 *to_next = 0, *buffers;
516  u32 bi0, bi1, bi2, bi3;
517  u16 s0, s1, s2, s3;
518  memif_desc_t *d0, *d1, *d2, *d3;
519  vlib_buffer_t *b0, *b1, *b2, *b3;
520  u32 thread_index = vm->thread_index;
522  thread_index);
523  u16 cur_slot, last_slot, ring_size, n_slots, mask, head;
524  i16 start_offset;
525  u32 buffer_length;
526  u16 n_alloc, n_from;
527 
528  mq = vec_elt_at_index (mif->rx_queues, qid);
529  ring = mq->ring;
530  ring_size = 1 << mq->log2_ring_size;
531  mask = ring_size - 1;
532 
533  next_index = (mode == MEMIF_INTERFACE_MODE_IP) ?
535 
536  /* asume that somebody will want to add ethernet header on the packet
537  so start with IP header at offset 14 */
538  start_offset = (mode == MEMIF_INTERFACE_MODE_IP) ? 14 : 0;
539  buffer_length = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES - start_offset;
540 
541  cur_slot = mq->last_tail;
542  last_slot = ring->tail;
543  if (cur_slot == last_slot)
544  goto refill;
545  n_slots = last_slot - cur_slot;
546 
547  /* process ring slots */
550  while (n_slots && n_rx_packets < MEMIF_RX_VECTOR_SZ)
551  {
552  vlib_buffer_t *hb;
553 
554  s0 = cur_slot & mask;
555  bi0 = mq->buffers[s0];
556  ptd->buffers[n_rx_packets++] = bi0;
557 
558  CLIB_PREFETCH (&ring->desc[(cur_slot + 8) & mask],
559  CLIB_CACHE_LINE_BYTES, LOAD);
560  d0 = &ring->desc[s0];
561  hb = b0 = vlib_get_buffer (vm, bi0);
562  b0->current_data = start_offset;
563  b0->current_length = start_offset + d0->length;
564  n_rx_bytes += d0->length;
565 
566  if (0 && memif_desc_is_invalid (mif, d0, buffer_length))
567  return 0;
568 
569  cur_slot++;
570  n_slots--;
571  if (PREDICT_FALSE ((d0->flags & MEMIF_DESC_FLAG_NEXT) && n_slots))
572  {
573  hb->flags |= VLIB_BUFFER_TOTAL_LENGTH_VALID;
574  next_slot:
575  s0 = cur_slot & mask;
576  d0 = &ring->desc[s0];
577  bi0 = mq->buffers[s0];
578 
579  /* previous buffer */
580  b0->next_buffer = bi0;
581  b0->flags |= VLIB_BUFFER_NEXT_PRESENT;
582 
583  /* current buffer */
584  b0 = vlib_get_buffer (vm, bi0);
585  b0->current_data = start_offset;
586  b0->current_length = start_offset + d0->length;
588  n_rx_bytes += d0->length;
589 
590  cur_slot++;
591  n_slots--;
592  if ((d0->flags & MEMIF_DESC_FLAG_NEXT) && n_slots)
593  goto next_slot;
594  }
595  }
596 
597  /* release slots from the ring */
598  mq->last_tail = cur_slot;
599 
600  n_from = n_rx_packets;
601  buffers = ptd->buffers;
602 
603  while (n_from)
604  {
605  u32 n_left_to_next;
606  u32 next0, next1, next2, next3;
607 
608  vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);
609  while (n_from >= 8 && n_left_to_next >= 4)
610  {
611  b0 = vlib_get_buffer (vm, buffers[4]);
612  b1 = vlib_get_buffer (vm, buffers[5]);
613  b2 = vlib_get_buffer (vm, buffers[6]);
614  b3 = vlib_get_buffer (vm, buffers[7]);
615  vlib_prefetch_buffer_header (b0, STORE);
616  vlib_prefetch_buffer_header (b1, STORE);
617  vlib_prefetch_buffer_header (b2, STORE);
618  vlib_prefetch_buffer_header (b3, STORE);
619 
620  /* enqueue buffer */
621  to_next[0] = bi0 = buffers[0];
622  to_next[1] = bi1 = buffers[1];
623  to_next[2] = bi2 = buffers[2];
624  to_next[3] = bi3 = buffers[3];
625  to_next += 4;
626  n_left_to_next -= 4;
627  buffers += 4;
628 
629  b0 = vlib_get_buffer (vm, bi0);
630  b1 = vlib_get_buffer (vm, bi1);
631  b2 = vlib_get_buffer (vm, bi2);
632  b3 = vlib_get_buffer (vm, bi3);
633 
634  vnet_buffer (b0)->sw_if_index[VLIB_RX] = mif->sw_if_index;
635  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
636  vnet_buffer (b1)->sw_if_index[VLIB_RX] = mif->sw_if_index;
637  vnet_buffer (b1)->sw_if_index[VLIB_TX] = ~0;
638  vnet_buffer (b2)->sw_if_index[VLIB_RX] = mif->sw_if_index;
639  vnet_buffer (b2)->sw_if_index[VLIB_TX] = ~0;
640  vnet_buffer (b3)->sw_if_index[VLIB_RX] = mif->sw_if_index;
641  vnet_buffer (b3)->sw_if_index[VLIB_TX] = ~0;
642 
643  if (mode == MEMIF_INTERFACE_MODE_IP)
644  {
645  next0 = memif_next_from_ip_hdr (node, b0);
646  next1 = memif_next_from_ip_hdr (node, b1);
647  next2 = memif_next_from_ip_hdr (node, b2);
648  next3 = memif_next_from_ip_hdr (node, b3);
649  }
650  else if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
651  {
652  if (PREDICT_FALSE (mif->per_interface_next_index != ~0))
653  {
654  next0 = mif->per_interface_next_index;
655  next1 = mif->per_interface_next_index;
656  next2 = mif->per_interface_next_index;
657  next3 = mif->per_interface_next_index;
658  }
659  else
660  {
661  next0 = next1 = next2 = next3 = next_index;
662  /* redirect if feature path enabled */
664  &next0, b0);
666  &next1, b1);
668  &next2, b2);
670  &next3, b3);
671  }
672  }
673 
674  /* trace */
675  if (PREDICT_FALSE (n_trace > 0))
676  {
677  memif_trace_buffer (vm, node, mif, b0, next0, qid, &n_trace);
678  if (PREDICT_FALSE (n_trace > 0))
679  memif_trace_buffer (vm, node, mif, b1, next1, qid, &n_trace);
680  if (PREDICT_FALSE (n_trace > 0))
681  memif_trace_buffer (vm, node, mif, b2, next2, qid, &n_trace);
682  if (PREDICT_FALSE (n_trace > 0))
683  memif_trace_buffer (vm, node, mif, b3, next3, qid, &n_trace);
684  }
685 
686  /* enqueue */
687  vlib_validate_buffer_enqueue_x4 (vm, node, next_index, to_next,
688  n_left_to_next, bi0, bi1, bi2, bi3,
689  next0, next1, next2, next3);
690 
691  /* next */
692  n_from -= 4;
693  }
694  while (n_from && n_left_to_next)
695  {
696  /* enqueue buffer */
697  to_next[0] = bi0 = buffers[0];
698  to_next += 1;
699  n_left_to_next--;
700  buffers += 1;
701 
702  b0 = vlib_get_buffer (vm, bi0);
703  vnet_buffer (b0)->sw_if_index[VLIB_RX] = mif->sw_if_index;
704  vnet_buffer (b0)->sw_if_index[VLIB_TX] = ~0;
705 
706  if (mode == MEMIF_INTERFACE_MODE_IP)
707  {
708  next0 = memif_next_from_ip_hdr (node, b0);
709  }
710  else if (mode == MEMIF_INTERFACE_MODE_ETHERNET)
711  {
712  if (PREDICT_FALSE (mif->per_interface_next_index != ~0))
713  next0 = mif->per_interface_next_index;
714  else
715  {
716  next0 = next_index;
717  /* redirect if feature path enabled */
719  &next0, b0);
720  }
721  }
722 
723  /* trace */
724  if (PREDICT_FALSE (n_trace > 0))
725  memif_trace_buffer (vm, node, mif, b0, next0, qid, &n_trace);
726 
727  /* enqueue */
728  vlib_validate_buffer_enqueue_x1 (vm, node, next_index, to_next,
729  n_left_to_next, bi0, next0);
730 
731  /* next */
732  n_from--;
733  }
734  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
735  }
736 
738  + VNET_INTERFACE_COUNTER_RX, thread_index,
739  mif->hw_if_index, n_rx_packets,
740  n_rx_bytes);
741 
742  /* refill ring with empty buffers */
743 refill:
744  vec_reset_length (ptd->buffers);
745 
746  head = ring->head;
747  n_slots = ring_size - head + mq->last_tail;
748 
749  if (n_slots < 32)
750  goto done;
751 
752  memif_desc_t *dt = &ptd->desc_template;
753  memset (dt, 0, sizeof (memif_desc_t));
754  dt->length = buffer_length;
755 
756  n_alloc = vlib_buffer_alloc_to_ring (vm, mq->buffers, head & mask,
757  ring_size, n_slots);
758 
759  if (PREDICT_FALSE (n_alloc != n_slots))
760  {
761  vlib_error_count (vm, node->node_index,
762  MEMIF_INPUT_ERROR_BUFFER_ALLOC_FAIL, 1);
763  }
764 
765  while (n_alloc >= 32)
766  {
767  bi0 = mq->buffers[(head + 4) & mask];
768  vlib_prefetch_buffer_with_index (vm, bi0, LOAD);
769  bi1 = mq->buffers[(head + 5) & mask];
770  vlib_prefetch_buffer_with_index (vm, bi1, LOAD);
771  bi2 = mq->buffers[(head + 6) & mask];
772  vlib_prefetch_buffer_with_index (vm, bi2, LOAD);
773  bi3 = mq->buffers[(head + 7) & mask];
774  vlib_prefetch_buffer_with_index (vm, bi3, LOAD);
775 
776  s0 = head++ & mask;
777  s1 = head++ & mask;
778  s2 = head++ & mask;
779  s3 = head++ & mask;
780 
781  d0 = &ring->desc[s0];
782  d1 = &ring->desc[s1];
783  d2 = &ring->desc[s2];
784  d3 = &ring->desc[s3];
785 
786  clib_memcpy (d0, dt, sizeof (memif_desc_t));
787  clib_memcpy (d1, dt, sizeof (memif_desc_t));
788  clib_memcpy (d2, dt, sizeof (memif_desc_t));
789  clib_memcpy (d3, dt, sizeof (memif_desc_t));
790 
791  b0 = vlib_get_buffer (vm, mq->buffers[s0]);
792  b1 = vlib_get_buffer (vm, mq->buffers[s1]);
793  b2 = vlib_get_buffer (vm, mq->buffers[s2]);
794  b3 = vlib_get_buffer (vm, mq->buffers[s3]);
795 
796  d0->region = b0->buffer_pool_index + 1;
797  d1->region = b1->buffer_pool_index + 1;
798  d2->region = b2->buffer_pool_index + 1;
799  d3->region = b3->buffer_pool_index + 1;
800 
801  d0->offset =
802  (void *) b0->data - mif->regions[d0->region].shm + start_offset;
803  d1->offset =
804  (void *) b1->data - mif->regions[d1->region].shm + start_offset;
805  d2->offset =
806  (void *) b2->data - mif->regions[d2->region].shm + start_offset;
807  d3->offset =
808  (void *) b3->data - mif->regions[d3->region].shm + start_offset;
809 
810  n_alloc -= 4;
811  }
812  while (n_alloc)
813  {
814  s0 = head++ & mask;
815  d0 = &ring->desc[s0];
816  clib_memcpy (d0, dt, sizeof (memif_desc_t));
817  b0 = vlib_get_buffer (vm, mq->buffers[s0]);
818  d0->region = b0->buffer_pool_index + 1;
819  d0->offset =
820  (void *) b0->data - mif->regions[d0->region].shm + start_offset;
821 
822  n_alloc -= 1;
823  }
824 
826  ring->head = head;
827 
828 done:
829  return n_rx_packets;
830 }
831 
832 
834  vlib_node_runtime_t * node,
835  vlib_frame_t * frame)
836 {
837  u32 n_rx = 0;
838  memif_main_t *mm = &memif_main;
839  vnet_device_input_runtime_t *rt = (void *) node->runtime_data;
843 
845  {
846  memif_if_t *mif;
847  mif = vec_elt_at_index (mm->interfaces, dq->dev_instance);
848  if ((mif->flags & MEMIF_IF_FLAG_ADMIN_UP) &&
849  (mif->flags & MEMIF_IF_FLAG_CONNECTED))
850  {
851  if (mif->flags & MEMIF_IF_FLAG_ZERO_COPY)
852  {
853  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
854  n_rx += memif_device_input_zc_inline (vm, node, frame, mif,
855  dq->queue_id, mode_ip);
856  else
857  n_rx += memif_device_input_zc_inline (vm, node, frame, mif,
858  dq->queue_id, mode_eth);
859  }
860  else if (mif->flags & MEMIF_IF_FLAG_IS_SLAVE)
861  {
862  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
863  n_rx += memif_device_input_inline (vm, node, frame, mif,
864  MEMIF_RING_M2S, dq->queue_id,
865  mode_ip);
866  else
867  n_rx += memif_device_input_inline (vm, node, frame, mif,
868  MEMIF_RING_M2S, dq->queue_id,
869  mode_eth);
870  }
871  else
872  {
873  if (mif->mode == MEMIF_INTERFACE_MODE_IP)
874  n_rx += memif_device_input_inline (vm, node, frame, mif,
875  MEMIF_RING_S2M, dq->queue_id,
876  mode_ip);
877  else
878  n_rx += memif_device_input_inline (vm, node, frame, mif,
879  MEMIF_RING_S2M, dq->queue_id,
880  mode_eth);
881  }
882  }
883  }
884 
885  return n_rx;
886 }
887 
888 /* *INDENT-OFF* */
890  .name = "memif-input",
891  .sibling_of = "device-input",
892  .format_trace = format_memif_input_trace,
893  .type = VLIB_NODE_TYPE_INPUT,
894  .state = VLIB_NODE_STATE_INTERRUPT,
895  .n_errors = MEMIF_INPUT_N_ERROR,
896  .error_strings = memif_input_error_strings,
897 };
898 
899 /* *INDENT-ON* */
900 
901 
902 /*
903  * fd.io coding-style-patch-verification: ON
904  *
905  * Local Variables:
906  * eval: (c-set-style "gnu")
907  * End:
908  */
memif_if_t * interfaces
Definition: private.h:238
vnet_device_and_queue_t * devices_and_queues
Definition: devices.h:69
#define clib_min(x, y)
Definition: clib.h:291
#define CLIB_UNUSED(x)
Definition: clib.h:81
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:204
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:547
u32 current_config_index
Used by feature subgraph arcs to visit enabled feature nodes.
Definition: buffer.h:135
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:108
#define CLIB_MEMORY_STORE_BARRIER()
Definition: clib.h:114
static void vlib_error_count(vlib_main_t *vm, uword node_index, uword counter, uword increment)
Definition: error_funcs.h:57
#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:574
u32 thread_index
Definition: main.h:179
memif_interface_mode_t
Definition: memif.h:53
u8 buffer_pool_index
index of buffer pool this buffer belongs.
Definition: buffer.h:143
#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:187
u16 first_buffer_vec_index
Definition: private.h:203
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
Definition: vec.h:448
u32 * buffers
Definition: private.h:124
vlib_error_t * errors
Vector of errors for this node.
Definition: node.h:472
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
memset(h->entries, 0, sizeof(h->entries[0])*entries)
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:109
#define static_always_inline
Definition: clib.h:95
#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:337
static_always_inline u32 memif_next_from_ip_hdr(vlib_node_runtime_t *node, vlib_buffer_t *b)
Definition: node.c:74
vlib_combined_counter_main_t * combined_sw_if_counters
Definition: interface.h:855
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
u16 buffer_size
Definition: private.h:184
#define vlib_prefetch_buffer_header(b, type)
Prefetch buffer metadata.
Definition: buffer.h:187
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:162
#define VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES
Definition: buffer.h:441
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
memif_packet_op_t packet_ops[MEMIF_RX_VECTOR_SZ]
Definition: private.h:221
memif_region_index_t region
Definition: memif.h:151
u16 last_head
Definition: private.h:122
memif_copy_op_t * copy_ops
Definition: private.h:222
vlib_node_registration_t memif_input_node
(constructor) VLIB_REGISTER_NODE (memif_input_node)
Definition: node.c:889
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:113
uword dev_instance
Definition: private.h:159
#define v
Definition: acl.c:496
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:205
void * data
Definition: private.h:208
#define PREDICT_FALSE(x)
Definition: clib.h:107
#define foreach_memif_input_error
Definition: node.c:33
u32 node_index
Node index.
Definition: node.h:494
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:218
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:364
vlib_error_t error
Error code for buffers to be enqueued to error handler.
Definition: buffer.h:138
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:155
#define CLIB_PREFETCH(addr, size, type)
Definition: cache.h:79
vlib_main_t * vm
Definition: buffer.c:294
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:211
static_always_inline void clib_memcpy64_x4(void *d0, void *d1, void *d2, void *d3, void *s)
Definition: string.h:89
#define clib_memcpy(a, b, c)
Definition: string.h:75
u16 last_tail
Definition: private.h:123
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:455
static_always_inline u32 sat_sub(u32 x, u32 y)
Definition: node.c:145
i16 buffer_offset
Definition: private.h:210
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:504
memif_region_t * regions
Definition: private.h:170
signed int i32
Definition: types.h:77
memif_input_error_t
Definition: node.c:37
#define MEMIF_DESC_FLAG_NEXT
Definition: memif.h:150
u32 flags
Definition: private.h:155
memif_ring_t * ring
Definition: private.h:117
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:129
vlib_buffer_t buffer_template
Definition: private.h:226
u32 hw_if_index
Definition: private.h:157
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
static_always_inline void clib_memset_u16(void *p, u16 val, uword count)
Definition: string.h:233
u32 total_length_not_including_first_buffer
Only valid for first buffer in chain.
Definition: buffer.h:155
#define foreach_device_and_queue(var, vec)
Definition: devices.h:156
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:520
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:214
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
#define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b)
Definition: buffer.h:547
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:118
#define vnet_buffer(b)
Definition: buffer.h:344
struct memif_if_t::@487 run
static_always_inline void vnet_feature_start_device_input_x1(u32 sw_if_index, u32 *next0, vlib_buffer_t *b0)
Definition: feature.h:259
memif_per_thread_data_t * per_thread_data
Definition: private.h:245
u8 data[0]
Packet data.
Definition: buffer.h:175
memif_ring_type_t
Definition: memif.h:47
volatile uint16_t head
Definition: memif.h:169
memif_queue_t * rx_queues
Definition: private.h:172
memif_desc_t desc_template
Definition: private.h:227
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:116
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:503
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:58
u32 sw_if_index
Definition: private.h:158
volatile uint16_t tail
Definition: memif.h:171
memif_interface_mode_t mode
Definition: private.h:160
Definition: defs.h:46
memif_region_size_t region_size
Definition: private.h:103
signed short i16
Definition: types.h:46