FD.io VPP  v20.09-64-g4f7b92f0a
Vector Packet Processing
device.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 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 
22 #include <vlib/vlib.h>
23 #include <vlib/unix/unix.h>
24 #include <vnet/vnet.h>
25 #include <vnet/ethernet/ethernet.h>
26 #include <vnet/gso/gro_func.h>
28 #include <vnet/ip/ip4_packet.h>
29 #include <vnet/ip/ip6_packet.h>
30 #include <vnet/tcp/tcp_packet.h>
31 #include <vnet/udp/udp_packet.h>
33 
34 #define foreach_virtio_tx_func_error \
35 _(NO_FREE_SLOTS, "no free tx slots") \
36 _(TRUNC_PACKET, "packet > buffer size -- truncated in tx ring") \
37 _(PENDING_MSGS, "pending msgs in tx ring") \
38 _(INDIRECT_DESC_ALLOC_FAILED, "indirect descriptor allocation failed - packet drop") \
39 _(OUT_OF_ORDER, "out-of-order buffers in used ring") \
40 _(GSO_PACKET_DROP, "gso disabled on itf -- gso packet drop") \
41 _(CSUM_OFFLOAD_PACKET_DROP, "checksum offload disabled on itf -- csum offload packet drop")
42 
43 typedef enum
44 {
45 #define _(f,s) VIRTIO_TX_ERROR_##f,
47 #undef _
50 
51 static char *virtio_tx_func_error_strings[] = {
52 #define _(n,s) s,
54 #undef _
55 };
56 
57 static u8 *
58 format_virtio_device (u8 * s, va_list * args)
59 {
60  u32 dev_instance = va_arg (*args, u32);
61  int verbose = va_arg (*args, int);
62  u32 indent = format_get_indent (s);
63 
64  s = format (s, "VIRTIO interface");
65  if (verbose)
66  {
67  s = format (s, "\n%U instance %u", format_white_space, indent + 2,
68  dev_instance);
69  }
70  return s;
71 }
72 
73 typedef struct
74 {
80 
81 static u8 *
82 format_virtio_tx_trace (u8 * s, va_list * va)
83 {
84  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*va, vlib_main_t *);
85  CLIB_UNUSED (vlib_node_t * node) = va_arg (*va, vlib_node_t *);
86  virtio_tx_trace_t *t = va_arg (*va, virtio_tx_trace_t *);
87  u32 indent = format_get_indent (s);
88 
89  s = format (s, "%U ", format_generic_header_offset, &t->gho);
90  s = format (s, "%Ubuffer 0x%x: %U",
91  format_white_space, indent,
93 
94  s = format (s, "\n%U%U", format_white_space, indent,
96  sizeof (t->buffer.pre_data));
97  return s;
98 }
99 
102  u32 * buffers, u16 n,
104 {
105  vlib_error_count (vm, node_index, error, n);
106  vlib_buffer_free (vm, buffers, n);
107 }
108 
110 virtio_memset_ring_u32 (u32 * ring, u32 start, u32 ring_size, u32 n_buffers)
111 {
112  ASSERT (n_buffers <= ring_size);
113 
114  if (PREDICT_TRUE (start + n_buffers <= ring_size))
115  {
116  clib_memset_u32 (ring + start, ~0, n_buffers);
117  }
118  else
119  {
120  clib_memset_u32 (ring + start, ~0, ring_size - start);
121  clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start));
122  }
123 }
124 
127  uword node_index)
128 {
129  u16 used = vring->desc_in_use;
130  u16 sz = vring->size;
131  u16 mask = sz - 1;
132  u16 last = vring->last_used_idx;
133  u16 n_left = vring->used->idx - last;
134  u16 out_of_order_count = 0;
135 
136  if (n_left == 0)
137  return;
138 
139  while (n_left)
140  {
141  vring_used_elem_t *e = &vring->used->ring[last & mask];
142  u16 slot, n_buffers;
143  slot = n_buffers = e->id;
144 
145  while (e->id == (n_buffers & mask))
146  {
147  n_left--;
148  last++;
149  n_buffers++;
150  vring_desc_t *d = &vring->desc[e->id];
151  u16 next;
152  while (d->flags & VRING_DESC_F_NEXT)
153  {
154  n_buffers++;
155  next = d->next;
156  d = &vring->desc[next];
157  }
158  if (n_left == 0)
159  break;
160  e = &vring->used->ring[last & mask];
161  }
162  vlib_buffer_free_from_ring (vm, vring->buffers, slot,
163  sz, (n_buffers - slot));
164  virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot));
165  used -= (n_buffers - slot);
166 
167  if (n_left > 0)
168  {
169  vlib_buffer_free (vm, &vring->buffers[e->id], 1);
170  vring->buffers[e->id] = ~0;
171  used--;
172  last++;
173  n_left--;
174  out_of_order_count++;
175  vring->flags |= VRING_TX_OUT_OF_ORDER;
176  }
177  }
178 
179  /*
180  * Some vhost-backends give buffers back in out-of-order fashion in used ring.
181  * It impacts the overall virtio-performance.
182  */
183  if (out_of_order_count)
184  vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER,
185  out_of_order_count);
186 
187  vring->desc_in_use = used;
188  vring->last_used_idx = last;
189 }
190 
192 set_checksum_offsets (vlib_buffer_t * b, virtio_net_hdr_v1_t * hdr, int is_l2)
193 {
194  if (b->flags & VNET_BUFFER_F_IS_IP4)
195  {
196  ip4_header_t *ip4;
197  generic_header_offset_t gho = { 0 };
198  vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
199  0 /* ip6 */ );
200  hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
201  hdr->csum_start = gho.l4_hdr_offset; // 0x22;
202  if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
203  {
204  hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
205  }
206  else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
207  {
208  hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
209  }
210 
211  /*
212  * virtio devices do not support IP4 checksum offload. So driver takes care
213  * of it while doing tx.
214  */
215  ip4 =
217  if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
218  ip4->checksum = ip4_header_checksum (ip4);
219  }
220  else if (b->flags & VNET_BUFFER_F_IS_IP6)
221  {
222  generic_header_offset_t gho = { 0 };
223  vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
224  1 /* ip6 */ );
225  hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
226  hdr->csum_start = gho.l4_hdr_offset; // 0x36;
227  if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
228  {
229  hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
230  }
231  else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
232  {
233  hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
234  }
235  }
236 }
237 
239 set_gso_offsets (vlib_buffer_t * b, virtio_net_hdr_v1_t * hdr, int is_l2)
240 {
241  if (b->flags & VNET_BUFFER_F_IS_IP4)
242  {
243  ip4_header_t *ip4;
244  generic_header_offset_t gho = { 0 };
245  vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
246  0 /* ip6 */ );
247  hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
248  hdr->gso_size = vnet_buffer2 (b)->gso_size;
249  hdr->hdr_len = gho.hdr_sz;
250  hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
251  hdr->csum_start = gho.l4_hdr_offset; // 0x22;
252  hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
253  ip4 =
255  /*
256  * virtio devices do not support IP4 checksum offload. So driver takes care
257  * of it while doing tx.
258  */
259  if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
260  ip4->checksum = ip4_header_checksum (ip4);
261  }
262  else if (b->flags & VNET_BUFFER_F_IS_IP6)
263  {
264  generic_header_offset_t gho = { 0 };
265  vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
266  1 /* ip6 */ );
267  hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
268  hdr->gso_size = vnet_buffer2 (b)->gso_size;
269  hdr->hdr_len = gho.hdr_sz;
270  hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
271  hdr->csum_start = gho.l4_hdr_offset; // 0x36;
272  hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
273  }
274 }
275 
279  u32 bi, u16 free_desc_count,
280  u16 avail, u16 next, u16 mask, int do_gso,
281  int csum_offload, uword node_index)
282 {
283  u16 n_added = 0;
284  int hdr_sz = vif->virtio_net_hdr_sz;
285  vring_desc_t *d;
286  d = &vring->desc[next];
287  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
288  virtio_net_hdr_v1_t *hdr = vlib_buffer_get_current (b) - hdr_sz;
289  int is_l2 = (type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_PCI));
290 
291  clib_memset (hdr, 0, hdr_sz);
292 
293  if (b->flags & VNET_BUFFER_F_GSO)
294  {
295  if (do_gso)
296  set_gso_offsets (b, hdr, is_l2);
297  else
298  {
299  virtio_interface_drop_inline (vm, node_index, &bi, 1,
300  VIRTIO_TX_ERROR_GSO_PACKET_DROP);
301  return n_added;
302  }
303  }
304  else if (b->flags & (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
305  VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
306  {
307  if (csum_offload)
308  set_checksum_offsets (b, hdr, is_l2);
309  else
310  {
311  virtio_interface_drop_inline (vm, node_index, &bi, 1,
312  VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP);
313  return n_added;
314  }
315  }
316 
317  if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
318  {
319  d->addr =
320  ((type == VIRTIO_IF_TYPE_PCI) ? vlib_buffer_get_current_pa (vm,
321  b) :
323  d->len = b->current_length + hdr_sz;
324  d->flags = 0;
325  }
326  else if (vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC))
327  {
328  /*
329  * We are using single vlib_buffer_t for indirect descriptor(s)
330  * chain. Single descriptor is 16 bytes and vlib_buffer_t
331  * has 2048 bytes space. So maximum long chain can have 128
332  * (=2048/16) indirect descriptors.
333  * It can easily support 65535 bytes of Jumbo frames with
334  * each data buffer size of 512 bytes minimum.
335  */
336  u32 indirect_buffer = 0;
337  if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
338  {
339  virtio_interface_drop_inline (vm, node_index, &bi, 1,
340  VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED);
341  return n_added;
342  }
343 
344  vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
345  indirect_desc->current_data = 0;
346  indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
347  indirect_desc->next_buffer = bi;
348  bi = indirect_buffer;
349 
350  vring_desc_t *id =
351  (vring_desc_t *) vlib_buffer_get_current (indirect_desc);
352  u32 count = 1;
353  if (type == VIRTIO_IF_TYPE_PCI)
354  {
355  d->addr = vlib_physmem_get_pa (vm, id);
356  id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
357 
358  /*
359  * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
360  * should be presented in separate descriptor and data will start
361  * from next descriptor.
362  */
363  if (PREDICT_TRUE
364  (vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)))
365  id->len = b->current_length + hdr_sz;
366  else
367  {
368  id->len = hdr_sz;
369  id->flags = VRING_DESC_F_NEXT;
370  id->next = count;
371  count++;
372  id++;
373  id->addr = vlib_buffer_get_current_pa (vm, b);
374  id->len = b->current_length;
375  }
376  while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
377  {
378  id->flags = VRING_DESC_F_NEXT;
379  id->next = count;
380  count++;
381  id++;
382  b = vlib_get_buffer (vm, b->next_buffer);
383  id->addr = vlib_buffer_get_current_pa (vm, b);
384  id->len = b->current_length;
385  }
386  }
387  else /* VIRTIO_IF_TYPE_[TAP | TUN] */
388  {
389  d->addr = pointer_to_uword (id);
390  /* first buffer in chain */
391  id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
392  id->len = b->current_length + hdr_sz;
393 
394  while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
395  {
396  id->flags = VRING_DESC_F_NEXT;
397  id->next = count;
398  count++;
399  id++;
400  b = vlib_get_buffer (vm, b->next_buffer);
401  id->addr = pointer_to_uword (vlib_buffer_get_current (b));
402  id->len = b->current_length;
403  }
404  }
405  id->flags = 0;
406  id->next = 0;
407  d->len = count * sizeof (vring_desc_t);
409  }
410  else if (type == VIRTIO_IF_TYPE_PCI)
411  {
412  u16 count = next;
413  vlib_buffer_t *b_temp = b;
414  u16 n_buffers_in_chain = 1;
415 
416  /*
417  * Check the length of the chain for the required number of
418  * descriptors. Return from here, retry to get more descriptors,
419  * if chain length is greater than available descriptors.
420  */
421  while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT)
422  {
423  n_buffers_in_chain++;
424  b_temp = vlib_get_buffer (vm, b_temp->next_buffer);
425  }
426 
427  if (n_buffers_in_chain > free_desc_count)
428  return n_buffers_in_chain;
429 
430  d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
431  d->len = b->current_length + hdr_sz;
432 
433  while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
434  {
436  vring->buffers[count] = bi;
437  b->flags &=
438  ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID);
439  bi = b->next_buffer;
440  b->next_buffer = 0;
441  n_added++;
442  count = (count + 1) & mask;
443  d->next = count;
444  d = &vring->desc[count];
445  b = vlib_get_buffer (vm, bi);
446  d->addr = vlib_buffer_get_current_pa (vm, b);
447  d->len = b->current_length;
448  }
449  d->flags = 0;
450  vring->buffers[count] = bi;
451  vring->avail->ring[avail & mask] = next;
452  n_added++;
453  return n_added;
454  }
455  else
456  {
457  ASSERT (0);
458  }
459  vring->buffers[next] = bi;
460  vring->avail->ring[avail & mask] = next;
461  n_added++;
462  return n_added;
463 }
464 
467  u16 req, u16 next, u32 * first_free_desc_index,
468  u16 * free_desc_count)
469 {
470  u16 start = 0;
471  /* next is used as hint: from where to start looking */
472  for (u16 i = 0; i < size; i++, next++)
473  {
474  if (vring->buffers[next & mask] == ~0)
475  {
476  if (*first_free_desc_index == ~0)
477  {
478  *first_free_desc_index = (next & mask);
479  start = i;
480  (*free_desc_count)++;
481  req--;
482  if (req == 0)
483  break;
484  }
485  else
486  {
487  if (start + *free_desc_count == i)
488  {
489  (*free_desc_count)++;
490  req--;
491  if (req == 0)
492  break;
493  }
494  else
495  break;
496  }
497  }
498  }
499 }
500 
503  vlib_frame_t * frame, virtio_if_t * vif,
504  virtio_if_type_t type, int do_gso,
505  int csum_offload, int do_gro)
506 {
507  u16 n_left = frame->n_vectors;
508  virtio_vring_t *vring;
509  u16 qid = vm->thread_index % vif->num_txqs;
510  vring = vec_elt_at_index (vif->txq_vrings, qid);
511  u16 used, next, avail;
512  u16 sz = vring->size;
513  u16 mask = sz - 1;
514  u16 retry_count = 2;
515  u32 *buffers = vlib_frame_vector_args (frame);
516  u32 to[GRO_TO_VECTOR_SIZE (n_left)];
517 
519 
520  if ((vring->used->flags & VRING_USED_F_NO_NOTIFY) == 0 &&
521  (vring->last_kick_avail_idx != vring->avail->idx))
522  virtio_kick (vm, vring, vif);
523 
524  if (do_gro)
525  {
526  n_left = vnet_gro_inline (vm, vring->flow_table, buffers, n_left, to);
527  buffers = to;
528  }
529 
530 retry:
531  /* free consumed buffers */
532  virtio_free_used_device_desc (vm, vring, node->node_index);
533 
534  used = vring->desc_in_use;
535  next = vring->desc_next;
536  avail = vring->avail->idx;
537 
538  u16 free_desc_count = 0;
539 
541  {
542  u32 first_free_desc_index = ~0;
543 
544  virtio_find_free_desc (vring, sz, mask, n_left, next,
545  &first_free_desc_index, &free_desc_count);
546 
547  if (free_desc_count)
548  next = first_free_desc_index;
549  }
550  else
551  free_desc_count = sz - used;
552 
553  while (n_left && free_desc_count)
554  {
555  u16 n_added = 0;
557 
558  vlib_buffer_t *b0 = vlib_get_buffer (vm, buffers[0]);
559  if (b0->flags & VLIB_BUFFER_IS_TRACED)
560  {
561  t = vlib_add_trace (vm, node, b0, sizeof (t[0]));
562  t->sw_if_index = vnet_buffer (b0)->sw_if_index[VLIB_TX];
563  t->buffer_index = buffers[0];
564  if (type == VIRTIO_IF_TYPE_TUN)
565  {
566  int is_ip4 = 0, is_ip6 = 0;
567 
568  switch (((u8 *) vlib_buffer_get_current (b0))[0] & 0xf0)
569  {
570  case 0x40:
571  is_ip4 = 1;
572  break;
573  case 0x60:
574  is_ip6 = 1;
575  break;
576  default:
577  break;
578  }
579  vnet_generic_header_offset_parser (b0, &t->gho, 0, is_ip4,
580  is_ip6);
581  }
582  else
584  b0->flags &
585  VNET_BUFFER_F_IS_IP4,
586  b0->flags &
587  VNET_BUFFER_F_IS_IP6);
588 
589  clib_memcpy_fast (&t->buffer, b0,
590  sizeof (*b0) - sizeof (b0->pre_data));
592  sizeof (t->buffer.pre_data));
593  }
594  n_added =
595  add_buffer_to_slot (vm, vif, type, vring, buffers[0], free_desc_count,
596  avail, next, mask, do_gso, csum_offload,
597  node->node_index);
598 
599  if (PREDICT_FALSE (n_added == 0))
600  {
601  buffers++;
602  n_left--;
603  continue;
604  }
605  else if (PREDICT_FALSE (n_added > free_desc_count))
606  break;
607 
608  avail++;
609  next = (next + n_added) & mask;
610  used += n_added;
611  buffers++;
612  n_left--;
613  free_desc_count -= n_added;
614  }
615 
616  if (n_left != frame->n_vectors)
617  {
619  vring->avail->idx = avail;
620  vring->desc_next = next;
621  vring->desc_in_use = used;
622  if ((vring->used->flags & VRING_USED_F_NO_NOTIFY) == 0)
623  virtio_kick (vm, vring, vif);
624  }
625 
626  if (n_left)
627  {
628  if (retry_count--)
629  goto retry;
630 
632  buffers, n_left,
633  VIRTIO_TX_ERROR_NO_FREE_SLOTS);
634  }
635 
637 
638  return frame->n_vectors - n_left;
639 }
640 
643  vlib_frame_t * frame, virtio_if_t * vif,
645 {
646  vnet_main_t *vnm = vnet_get_main ();
648 
650  return virtio_interface_tx_gso_inline (vm, node, frame, vif, type,
651  1 /* do_gso */ ,
652  1 /* checksum offload */ ,
653  vif->packet_coalesce);
655  return virtio_interface_tx_gso_inline (vm, node, frame, vif, type,
656  0 /* no do_gso */ ,
657  1 /* checksum offload */ ,
658  0 /* do_gro */ );
659  else
660  return virtio_interface_tx_gso_inline (vm, node, frame, vif, type,
661  0 /* no do_gso */ ,
662  0 /* no checksum offload */ ,
663  0 /* do_gro */ );
664 }
665 
669 {
670  virtio_main_t *nm = &virtio_main;
671  vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
673 
674  if (vif->type == VIRTIO_IF_TYPE_TAP)
675  return virtio_interface_tx_inline (vm, node, frame, vif,
676  VIRTIO_IF_TYPE_TAP);
677  else if (vif->type == VIRTIO_IF_TYPE_PCI)
678  return virtio_interface_tx_inline (vm, node, frame, vif,
679  VIRTIO_IF_TYPE_PCI);
680  else if (vif->type == VIRTIO_IF_TYPE_TUN)
681  return virtio_interface_tx_inline (vm, node, frame, vif,
682  VIRTIO_IF_TYPE_TUN);
683  else
684  ASSERT (0);
685 
686  return 0;
687 }
688 
689 static void
691  u32 node_index)
692 {
693  virtio_main_t *apm = &virtio_main;
694  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
696 
697  /* Shut off redirection */
698  if (node_index == ~0)
699  {
700  vif->per_interface_next_index = node_index;
701  return;
702  }
703 
706  node_index);
707 }
708 
709 static void
711 {
712  /* Nothing for now */
713 }
714 
715 static clib_error_t *
718 {
719  virtio_main_t *mm = &virtio_main;
720  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
722  virtio_vring_t *rx_vring = vec_elt_at_index (vif->rxq_vrings, qid);
723  virtio_vring_t *tx_vring = 0;
724 
725  if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
726  {
727  rx_vring->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
728  return clib_error_return (0, "interrupt mode is not supported");
729  }
730 
732  {
733  vec_foreach (tx_vring, vif->txq_vrings)
734  {
735  /* only enable packet coalesce in poll mode */
737  }
738  rx_vring->avail->flags |= VRING_AVAIL_F_NO_INTERRUPT;
739  }
740  else
741  {
742  if (vif->packet_coalesce)
743  {
744  virtio_log_warning (vif,
745  "interface %U is in interrupt mode, disabling packet coalescing",
747  vif->sw_if_index);
748  vec_foreach (tx_vring, vif->txq_vrings)
749  {
751  }
752  }
753  rx_vring->avail->flags &= ~VRING_AVAIL_F_NO_INTERRUPT;
754  }
755 
756  return 0;
757 }
758 
759 static clib_error_t *
761 {
762  virtio_main_t *mm = &virtio_main;
763  vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
765 
767  {
768  vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
771  }
772  else
773  {
774  vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
776  }
777  return 0;
778 }
779 
780 static clib_error_t *
782  u32 hw_if_index,
783  struct vnet_sw_interface_t *st, int is_add)
784 {
785  /* Nothing for now */
786  return 0;
787 }
788 
789 /* *INDENT-OFF* */
791  .name = "virtio",
792  .format_device_name = format_virtio_device_name,
793  .format_device = format_virtio_device,
794  .format_tx_trace = format_virtio_tx_trace,
795  .tx_function_n_errors = VIRTIO_TX_N_ERROR,
796  .tx_function_error_strings = virtio_tx_func_error_strings,
797  .rx_redirect_to_node = virtio_set_interface_next_node,
798  .clear_counters = virtio_clear_hw_interface_counters,
799  .admin_up_down_function = virtio_interface_admin_up_down,
800  .subif_add_del_function = virtio_subif_add_del_function,
801  .rx_mode_change_function = virtio_interface_rx_mode_change,
802 };
803 
804 /* *INDENT-ON* */
805 
806 /*
807  * fd.io coding-style-patch-verification: ON
808  *
809  * Local Variables:
810  * eval: (c-set-style "gnu")
811  * End:
812  */
u32 per_interface_next_index
Definition: virtio.h:109
gro_flow_table_t * flow_table
Definition: virtio.h:86
vlib_buffer_t buffer
Definition: device.c:77
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
u8 count
Definition: dhcp.api:208
vlib_node_registration_t virtio_input_node
(constructor) VLIB_REGISTER_NODE (virtio_input_node)
Definition: node.c:490
static uword vlib_buffer_get_current_pa(vlib_main_t *vm, vlib_buffer_t *b)
Definition: buffer_funcs.h:463
VNET_DEVICE_CLASS_TX_FN() virtio_device_class(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: device.c:666
virtio_if_t * interfaces
Definition: virtio.h:192
#define CLIB_UNUSED(x)
Definition: clib.h:87
static clib_error_t * virtio_interface_admin_up_down(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
Definition: device.c:760
vl_api_wireguard_peer_flags_t flags
Definition: wireguard.api:103
static void vlib_buffer_free(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Free buffers Frees the entire buffer chain for each buffer.
Definition: buffer_funcs.h:937
#define VIRTIO_NET_HDR_F_NEEDS_CSUM
Definition: virtio_std.h:134
vnet_main_t * vnet_get_main(void)
Definition: misc.c:46
#define vnet_buffer2(b)
Definition: buffer.h:482
#define PREDICT_TRUE(x)
Definition: clib.h:121
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:110
#define CLIB_MEMORY_STORE_BARRIER()
Definition: clib.h:135
static void vlib_error_count(vlib_main_t *vm, uword node_index, uword counter, uword increment)
Definition: error_funcs.h:57
#define clib_memcpy_fast(a, b, c)
Definition: string.h:81
vring_used_elem_t ring[0]
Definition: virtio_std.h:113
clib_memset(h->entries, 0, sizeof(h->entries[0]) *entries)
static_always_inline void clib_spinlock_unlock_if_init(clib_spinlock_t *p)
Definition: lock.h:127
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
u32 thread_index
Definition: main.h:249
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:113
static heap_elt_t * last(heap_header_t *h)
Definition: heap.c:53
static u32 format_get_indent(u8 *s)
Definition: format.h:72
#define STRUCT_OFFSET_OF(t, f)
Definition: clib.h:70
vlib_main_t * vm
Definition: in2out_ed.c:1582
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:424
u16 mask
Definition: flow_types.api:52
struct _tcp_header tcp_header_t
format_function_t format_vnet_sw_if_index_name
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1173
unsigned char u8
Definition: types.h:56
#define VIRTIO_FEATURE(X)
Definition: virtio_std.h:69
vring_desc_t * desc
Definition: virtio.h:63
static_always_inline void set_gso_offsets(vlib_buffer_t *b, virtio_net_hdr_v1_t *hdr, int is_l2)
Definition: device.c:239
vnet_hw_interface_rx_mode
Definition: interface.h:53
#define VRING_AVAIL_F_NO_INTERRUPT
Definition: virtio_std.h:85
u64 features
Definition: virtio.h:107
#define static_always_inline
Definition: clib.h:108
VNET_DEVICE_CLASS(af_xdp_device_class)
u32 hw_if_index
Definition: virtio.h:128
static clib_error_t * virtio_subif_add_del_function(vnet_main_t *vnm, u32 hw_if_index, struct vnet_sw_interface_t *st, int is_add)
Definition: device.c:781
static_always_inline uword virtio_interface_tx_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, virtio_if_t *vif, virtio_if_type_t type)
Definition: device.c:642
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:129
u8 support_int_mode
Definition: virtio.h:180
vnet_hw_interface_flags_t flags
Definition: interface.h:537
#define vec_elt_at_index(v, i)
Get vector value at index i checking that i is in bounds.
clib_spinlock_t lockp
Definition: virtio.h:62
#define clib_error_return(e, args...)
Definition: error.h:99
#define VNET_DEVICE_CLASS_TX_FN(devclass)
Definition: interface.h:316
unsigned int u32
Definition: types.h:88
#define virtio_log_warning(vif, f,...)
Definition: virtio.h:251
#define foreach_virtio_tx_func_error
Definition: device.c:34
bool is_ip6
Definition: ip.api:43
u16 num_txqs
Definition: virtio.h:111
static void vlib_buffer_free_from_ring(vlib_main_t *vm, u32 *ring, u32 start, u32 ring_size, u32 n_buffers)
Free buffers from ring.
Definition: buffer_funcs.h:984
static_always_inline u16 add_buffer_to_slot(vlib_main_t *vm, virtio_if_t *vif, virtio_if_type_t type, virtio_vring_t *vring, u32 bi, u16 free_desc_count, u16 avail, u16 next, u16 mask, int do_gso, int csum_offload, uword node_index)
Definition: device.c:277
vl_api_fib_path_type_t type
Definition: fib_types.api:123
#define VRING_USED_F_NO_NOTIFY
Definition: virtio_std.h:84
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:534
format_function_t format_vnet_buffer
Definition: buffer.h:498
static_always_inline void virtio_find_free_desc(virtio_vring_t *vring, u16 size, u16 mask, u16 req, u16 next, u32 *first_free_desc_index, u16 *free_desc_count)
Definition: device.c:466
vring_avail_t * avail
Definition: virtio.h:65
static __clib_warn_unused_result u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: buffer_funcs.h:677
unsigned short u16
Definition: types.h:57
u32 size
Definition: vhost_user.h:106
static u8 * format_virtio_tx_trace(u8 *s, va_list *va)
Definition: device.c:82
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:229
static u8 * format_virtio_device(u8 *s, va_list *args)
Definition: device.c:58
#define PREDICT_FALSE(x)
Definition: clib.h:120
vl_api_ip4_address_t ip4
Definition: one.api:376
u32 node_index
Node index.
Definition: node.h:487
#define VIRTIO_NET_HDR_GSO_TCPV4
Definition: virtio_std.h:138
static_always_inline void set_checksum_offsets(vlib_buffer_t *b, virtio_net_hdr_v1_t *hdr, int is_l2)
Definition: device.c:192
vl_api_tunnel_mode_t mode
Definition: gre.api:48
int packet_coalesce
Definition: virtio.h:132
static void virtio_set_interface_next_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
Definition: device.c:690
static_always_inline void virtio_interface_drop_inline(vlib_main_t *vm, uword node_index, u32 *buffers, u16 n, virtio_tx_func_error_t error)
Definition: device.c:101
vring_used_t * used
Definition: virtio.h:64
u16 desc_next
Definition: virtio.h:67
u16 virtio_net_hdr_sz
Definition: virtio.h:125
u8 slot
Definition: pci_types.api:22
u8 * format_ethernet_header_with_length(u8 *s, va_list *args)
Definition: format.c:97
virtio_vring_t * rxq_vrings
Definition: virtio.h:112
format_function_t format_virtio_device_name
Definition: virtio.h:219
u16 last_used_idx
Definition: virtio.h:83
u16 n_vectors
Definition: node.h:396
sll srl srl sll sra u16x4 i
Definition: vector_sse42.h:317
u32 flags
Definition: virtio.h:108
u16 last_kick_avail_idx
Definition: virtio.h:84
vlib_main_t vlib_node_runtime_t * node
Definition: in2out_ed.c:1582
virtio_if_type_t type
Definition: virtio.h:126
#define ASSERT(truth)
static_always_inline u32 vnet_gro_inline(vlib_main_t *vm, gro_flow_table_t *flow_table, u32 *from, u16 n_left_from, u32 *to)
coalesce buffers with flow tables
Definition: gro_func.h:534
u8 pre_data[VLIB_BUFFER_PRE_DATA_SIZE]
Space for inserting data before buffer start.
Definition: buffer.h:178
u32 buffer_index
Definition: device.c:75
virtio_tx_func_error_t
Definition: device.c:43
static uword pointer_to_uword(const void *p)
Definition: types.h:131
#define VRING_DESC_F_NEXT
Definition: virtio_std.h:73
virtio_main_t virtio_main
Definition: virtio.c:35
static vlib_main_t * vlib_get_main(void)
Definition: global_funcs.h:23
#define VRING_TX_OUT_OF_ORDER
Definition: virtio.h:80
static_always_inline void vnet_generic_header_offset_parser(vlib_buffer_t *b0, generic_header_offset_t *gho, int is_l2, int is_ip4, int is_ip6)
u16 ring[0]
Definition: virtio_std.h:99
static u64 vlib_physmem_get_pa(vlib_main_t *vm, void *mem)
Definition: defs.h:47
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:140
u32 instance
Definition: gre.api:51
vlib_main_t vlib_node_runtime_t vlib_frame_t * frame
Definition: in2out_ed.c:1583
static void virtio_clear_hw_interface_counters(u32 instance)
Definition: device.c:710
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, vnet_hw_interface_flags_t flags)
Definition: interface.c:498
#define GRO_TO_VECTOR_SIZE(X)
Definition: gro.h:27
VLIB buffer representation.
Definition: buffer.h:102
u64 uword
Definition: types.h:112
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:297
static_always_inline u8 * format_generic_header_offset(u8 *s, va_list *args)
u32 * buffers
Definition: virtio.h:82
#define vnet_buffer(b)
Definition: buffer.h:417
u32 sw_if_index
Definition: virtio.h:129
generic_header_offset_t gho
Definition: device.c:78
#define vec_foreach(var, vec)
Vector iterator.
void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace.c:577
static_always_inline void virtio_free_used_device_desc(vlib_main_t *vm, virtio_vring_t *vring, uword node_index)
Definition: device.c:126
virtio_if_type_t
Definition: virtio.h:49
static clib_error_t * virtio_interface_rx_mode_change(vnet_main_t *vnm, u32 hw_if_index, u32 qid, vnet_hw_interface_rx_mode mode)
Definition: device.c:716
static_always_inline void gro_flow_table_set_is_enable(gro_flow_table_t *flow_table, u8 is_enable)
Definition: gro.h:164
static_always_inline void clib_spinlock_lock_if_init(clib_spinlock_t *p)
Definition: lock.h:104
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
static u16 ip4_header_checksum(ip4_header_t *i)
Definition: ip4_packet.h:314
static_always_inline void virtio_memset_ring_u32(u32 *ring, u32 start, u32 ring_size, u32 n_buffers)
Definition: device.c:110
u16 desc_in_use
Definition: virtio.h:66
static_always_inline uword virtio_interface_tx_gso_inline(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame, virtio_if_t *vif, virtio_if_type_t type, int do_gso, int csum_offload, int do_gro)
Definition: device.c:502
#define VRING_DESC_F_INDIRECT
Definition: virtio_std.h:75
static char * virtio_tx_func_error_strings[]
Definition: device.c:51
#define VIRTIO_NET_HDR_GSO_TCPV6
Definition: virtio_std.h:140
virtio_vring_t * txq_vrings
Definition: virtio.h:113
static_always_inline void virtio_kick(vlib_main_t *vm, virtio_vring_t *vring, virtio_if_t *vif)
Definition: virtio.h:223
static_always_inline void clib_memset_u32(void *p, u32 val, uword count)
Definition: string.h:332