gso: add support for IP-IP
[vpp.git] / src / vnet / devices / virtio / device.c
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/hdr_offset_parser.h>
27 #include <vnet/ip/ip4_packet.h>
28 #include <vnet/ip/ip6_packet.h>
29 #include <vnet/tcp/tcp_packet.h>
30 #include <vnet/udp/udp_packet.h>
31 #include <vnet/devices/virtio/virtio.h>
32
33 #define foreach_virtio_tx_func_error           \
34 _(NO_FREE_SLOTS, "no free tx slots")           \
35 _(TRUNC_PACKET, "packet > buffer size -- truncated in tx ring") \
36 _(PENDING_MSGS, "pending msgs in tx ring") \
37 _(INDIRECT_DESC_ALLOC_FAILED, "indirect descriptor allocation failed - packet drop") \
38 _(OUT_OF_ORDER, "out-of-order buffers in used ring") \
39 _(GSO_PACKET_DROP, "gso disabled on itf  -- gso packet drop") \
40 _(CSUM_OFFLOAD_PACKET_DROP, "checksum offload disabled on itf -- csum offload packet drop")
41
42 typedef enum
43 {
44 #define _(f,s) VIRTIO_TX_ERROR_##f,
45   foreach_virtio_tx_func_error
46 #undef _
47     VIRTIO_TX_N_ERROR,
48 } virtio_tx_func_error_t;
49
50 static char *virtio_tx_func_error_strings[] = {
51 #define _(n,s) s,
52   foreach_virtio_tx_func_error
53 #undef _
54 };
55
56 static u8 *
57 format_virtio_device (u8 * s, va_list * args)
58 {
59   u32 dev_instance = va_arg (*args, u32);
60   int verbose = va_arg (*args, int);
61   u32 indent = format_get_indent (s);
62
63   s = format (s, "VIRTIO interface");
64   if (verbose)
65     {
66       s = format (s, "\n%U instance %u", format_white_space, indent + 2,
67                   dev_instance);
68     }
69   return s;
70 }
71
72 static u8 *
73 format_virtio_tx_trace (u8 * s, va_list * args)
74 {
75   s = format (s, "Unimplemented...");
76   return s;
77 }
78
79 static_always_inline void
80 virtio_interface_drop_inline (vlib_main_t * vm, uword node_index,
81                               u32 * buffers, u16 n,
82                               virtio_tx_func_error_t error)
83 {
84   vlib_error_count (vm, node_index, error, n);
85   vlib_buffer_free (vm, buffers, n);
86 }
87
88 static_always_inline void
89 virtio_memset_ring_u32 (u32 * ring, u32 start, u32 ring_size, u32 n_buffers)
90 {
91   ASSERT (n_buffers <= ring_size);
92
93   if (PREDICT_TRUE (start + n_buffers <= ring_size))
94     {
95       clib_memset_u32 (ring + start, ~0, n_buffers);
96     }
97   else
98     {
99       clib_memset_u32 (ring + start, ~0, ring_size - start);
100       clib_memset_u32 (ring, ~0, n_buffers - (ring_size - start));
101     }
102 }
103
104 static_always_inline void
105 virtio_free_used_device_desc (vlib_main_t * vm, virtio_vring_t * vring,
106                               uword node_index)
107 {
108   u16 used = vring->desc_in_use;
109   u16 sz = vring->size;
110   u16 mask = sz - 1;
111   u16 last = vring->last_used_idx;
112   u16 n_left = vring->used->idx - last;
113   u16 out_of_order_count = 0;
114
115   if (n_left == 0)
116     return;
117
118   while (n_left)
119     {
120       struct vring_used_elem *e = &vring->used->ring[last & mask];
121       u16 slot, n_buffers;
122       slot = n_buffers = e->id;
123
124       while (e->id == (n_buffers & mask))
125         {
126           n_left--;
127           last++;
128           n_buffers++;
129           struct vring_desc *d = &vring->desc[e->id];
130           u16 next;
131           while (d->flags & VRING_DESC_F_NEXT)
132             {
133               n_buffers++;
134               next = d->next;
135               d = &vring->desc[next];
136             }
137           if (n_left == 0)
138             break;
139           e = &vring->used->ring[last & mask];
140         }
141       vlib_buffer_free_from_ring (vm, vring->buffers, slot,
142                                   sz, (n_buffers - slot));
143       virtio_memset_ring_u32 (vring->buffers, slot, sz, (n_buffers - slot));
144       used -= (n_buffers - slot);
145
146       if (n_left > 0)
147         {
148           vlib_buffer_free (vm, &vring->buffers[e->id], 1);
149           vring->buffers[e->id] = ~0;
150           used--;
151           last++;
152           n_left--;
153           out_of_order_count++;
154           vring->flags |= VRING_TX_OUT_OF_ORDER;
155         }
156     }
157
158   /*
159    * Some vhost-backends give buffers back in out-of-order fashion in used ring.
160    * It impacts the overall virtio-performance.
161    */
162   if (out_of_order_count)
163     vlib_error_count (vm, node_index, VIRTIO_TX_ERROR_OUT_OF_ORDER,
164                       out_of_order_count);
165
166   vring->desc_in_use = used;
167   vring->last_used_idx = last;
168 }
169
170 static_always_inline void
171 set_checksum_offsets (vlib_buffer_t * b, struct virtio_net_hdr_v1 *hdr,
172                       int is_l2)
173 {
174   if (b->flags & VNET_BUFFER_F_IS_IP4)
175     {
176       ip4_header_t *ip4;
177       generic_header_offset_t gho = { 0 };
178       vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
179                                          0 /* ip6 */ );
180       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
181       hdr->csum_start = gho.l4_hdr_offset;      // 0x22;
182       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
183         {
184           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
185         }
186       else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
187         {
188           hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
189         }
190
191       /*
192        * virtio devices do not support IP4 checksum offload. So driver takes care
193        * of it while doing tx.
194        */
195       ip4 =
196         (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
197       if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
198         ip4->checksum = ip4_header_checksum (ip4);
199     }
200   else if (b->flags & VNET_BUFFER_F_IS_IP6)
201     {
202       generic_header_offset_t gho = { 0 };
203       vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
204                                          1 /* ip6 */ );
205       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
206       hdr->csum_start = gho.l4_hdr_offset;      // 0x36;
207       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
208         {
209           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
210         }
211       else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
212         {
213           hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
214         }
215     }
216 }
217
218 static_always_inline void
219 set_gso_offsets (vlib_buffer_t * b, struct virtio_net_hdr_v1 *hdr, int is_l2)
220 {
221   if (b->flags & VNET_BUFFER_F_IS_IP4)
222     {
223       ip4_header_t *ip4;
224       generic_header_offset_t gho = { 0 };
225       vnet_generic_header_offset_parser (b, &gho, is_l2, 1 /* ip4 */ ,
226                                          0 /* ip6 */ );
227       hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
228       hdr->gso_size = vnet_buffer2 (b)->gso_size;
229       hdr->hdr_len = gho.hdr_sz;
230       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
231       hdr->csum_start = gho.l4_hdr_offset;      // 0x22;
232       hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
233       ip4 =
234         (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
235       /*
236        * virtio devices do not support IP4 checksum offload. So driver takes care
237        * of it while doing tx.
238        */
239       if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
240         ip4->checksum = ip4_header_checksum (ip4);
241     }
242   else if (b->flags & VNET_BUFFER_F_IS_IP6)
243     {
244       generic_header_offset_t gho = { 0 };
245       vnet_generic_header_offset_parser (b, &gho, is_l2, 0 /* ip4 */ ,
246                                          1 /* ip6 */ );
247       hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
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;      // 0x36;
252       hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
253     }
254 }
255
256 static_always_inline u16
257 add_buffer_to_slot (vlib_main_t * vm, virtio_if_t * vif,
258                     virtio_vring_t * vring, u32 bi, u16 free_desc_count,
259                     u16 avail, u16 next, u16 mask, int do_gso,
260                     int csum_offload, uword node_index)
261 {
262   u16 n_added = 0;
263   int hdr_sz = vif->virtio_net_hdr_sz;
264   struct vring_desc *d;
265   d = &vring->desc[next];
266   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
267   struct virtio_net_hdr_v1 *hdr = vlib_buffer_get_current (b) - hdr_sz;
268   int is_l2 = (vif->type & (VIRTIO_IF_TYPE_TAP | VIRTIO_IF_TYPE_PCI));
269
270   clib_memset (hdr, 0, hdr_sz);
271
272   if (b->flags & VNET_BUFFER_F_GSO)
273     {
274       if (do_gso)
275         set_gso_offsets (b, hdr, is_l2);
276       else
277         {
278           virtio_interface_drop_inline (vm, node_index, &bi, 1,
279                                         VIRTIO_TX_ERROR_GSO_PACKET_DROP);
280           return n_added;
281         }
282     }
283   else if (b->flags & (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
284                        VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
285     {
286       if (csum_offload)
287         set_checksum_offsets (b, hdr, is_l2);
288       else
289         {
290           virtio_interface_drop_inline (vm, node_index, &bi, 1,
291                                         VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP);
292           return n_added;
293         }
294     }
295
296   if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
297     {
298       d->addr =
299         ((vif->type == VIRTIO_IF_TYPE_PCI) ? vlib_buffer_get_current_pa (vm,
300                                                                          b) :
301          pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
302       d->len = b->current_length + hdr_sz;
303       d->flags = 0;
304     }
305   else if (vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC))
306     {
307       /*
308        * We are using single vlib_buffer_t for indirect descriptor(s)
309        * chain. Single descriptor is 16 bytes and vlib_buffer_t
310        * has 2048 bytes space. So maximum long chain can have 128
311        * (=2048/16) indirect descriptors.
312        * It can easily support 65535 bytes of Jumbo frames with
313        * each data buffer size of 512 bytes minimum.
314        */
315       u32 indirect_buffer = 0;
316       if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
317         {
318           virtio_interface_drop_inline (vm, node_index, &bi, 1,
319                                         VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED);
320           return n_added;
321         }
322
323       vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
324       indirect_desc->current_data = 0;
325       indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
326       indirect_desc->next_buffer = bi;
327       bi = indirect_buffer;
328
329       struct vring_desc *id =
330         (struct vring_desc *) vlib_buffer_get_current (indirect_desc);
331       u32 count = 1;
332       if (vif->type == VIRTIO_IF_TYPE_PCI)
333         {
334           d->addr = vlib_physmem_get_pa (vm, id);
335           id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
336
337           /*
338            * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
339            * should be presented in separate descriptor and data will start
340            * from next descriptor.
341            */
342           if (PREDICT_TRUE
343               (vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)))
344             id->len = b->current_length + hdr_sz;
345           else
346             {
347               id->len = hdr_sz;
348               id->flags = VRING_DESC_F_NEXT;
349               id->next = count;
350               count++;
351               id++;
352               id->addr = vlib_buffer_get_current_pa (vm, b);
353               id->len = b->current_length;
354             }
355           while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
356             {
357               id->flags = VRING_DESC_F_NEXT;
358               id->next = count;
359               count++;
360               id++;
361               b = vlib_get_buffer (vm, b->next_buffer);
362               id->addr = vlib_buffer_get_current_pa (vm, b);
363               id->len = b->current_length;
364             }
365         }
366       else                      /* VIRTIO_IF_TYPE_[TAP | TUN] */
367         {
368           d->addr = pointer_to_uword (id);
369           /* first buffer in chain */
370           id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
371           id->len = b->current_length + hdr_sz;
372
373           while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
374             {
375               id->flags = VRING_DESC_F_NEXT;
376               id->next = count;
377               count++;
378               id++;
379               b = vlib_get_buffer (vm, b->next_buffer);
380               id->addr = pointer_to_uword (vlib_buffer_get_current (b));
381               id->len = b->current_length;
382             }
383         }
384       id->flags = 0;
385       id->next = 0;
386       d->len = count * sizeof (struct vring_desc);
387       d->flags = VRING_DESC_F_INDIRECT;
388     }
389   else if (vif->type == VIRTIO_IF_TYPE_PCI)
390     {
391       u16 count = next;
392       vlib_buffer_t *b_temp = b;
393       u16 n_buffers_in_chain = 1;
394
395       /*
396        * Check the length of the chain for the required number of
397        * descriptors. Return from here, retry to get more descriptors,
398        * if chain length is greater than available descriptors.
399        */
400       while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT)
401         {
402           n_buffers_in_chain++;
403           b_temp = vlib_get_buffer (vm, b_temp->next_buffer);
404         }
405
406       if (n_buffers_in_chain > free_desc_count)
407         return n_buffers_in_chain;
408
409       d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
410       d->len = b->current_length + hdr_sz;
411
412       while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
413         {
414           d->flags = VRING_DESC_F_NEXT;
415           vring->buffers[count] = bi;
416           b->flags &=
417             ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID);
418           bi = b->next_buffer;
419           b->next_buffer = 0;
420           n_added++;
421           count = (count + 1) & mask;
422           d->next = count;
423           d = &vring->desc[count];
424           b = vlib_get_buffer (vm, bi);
425           d->addr = vlib_buffer_get_current_pa (vm, b);
426           d->len = b->current_length;
427         }
428       d->flags = 0;
429       vring->buffers[count] = bi;
430       vring->avail->ring[avail & mask] = next;
431       n_added++;
432       return n_added;
433     }
434   else
435     {
436       ASSERT (0);
437     }
438   vring->buffers[next] = bi;
439   vring->avail->ring[avail & mask] = next;
440   n_added++;
441   return n_added;
442 }
443
444 static_always_inline void
445 virtio_find_free_desc (virtio_vring_t * vring, u16 size, u16 mask,
446                        u16 req, u16 next, u32 * first_free_desc_index,
447                        u16 * free_desc_count)
448 {
449   u16 start = 0;
450   /* next is used as hint: from where to start looking */
451   for (u16 i = 0; i < size; i++, next++)
452     {
453       if (vring->buffers[next & mask] == ~0)
454         {
455           if (*first_free_desc_index == ~0)
456             {
457               *first_free_desc_index = (next & mask);
458               start = i;
459               (*free_desc_count)++;
460               req--;
461               if (req == 0)
462                 break;
463             }
464           else
465             {
466               if (start + *free_desc_count == i)
467                 {
468                   (*free_desc_count)++;
469                   req--;
470                   if (req == 0)
471                     break;
472                 }
473               else
474                 break;
475             }
476         }
477     }
478 }
479
480 static_always_inline uword
481 virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
482                             vlib_frame_t * frame, virtio_if_t * vif,
483                             int do_gso, int csum_offload)
484 {
485   u16 n_left = frame->n_vectors;
486   virtio_vring_t *vring;
487   u16 qid = vm->thread_index % vif->num_txqs;
488   vring = vec_elt_at_index (vif->txq_vrings, qid);
489   u16 used, next, avail;
490   u16 sz = vring->size;
491   u16 mask = sz - 1;
492   u16 retry_count = 2;
493   u32 *buffers = vlib_frame_vector_args (frame);
494
495   clib_spinlock_lock_if_init (&vring->lockp);
496
497   if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0 &&
498       (vring->last_kick_avail_idx != vring->avail->idx))
499     virtio_kick (vm, vring, vif);
500
501 retry:
502   /* free consumed buffers */
503   virtio_free_used_device_desc (vm, vring, node->node_index);
504
505   used = vring->desc_in_use;
506   next = vring->desc_next;
507   avail = vring->avail->idx;
508
509   u16 free_desc_count = 0;
510
511   if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
512     {
513       u32 first_free_desc_index = ~0;
514
515       virtio_find_free_desc (vring, sz, mask, n_left, next,
516                              &first_free_desc_index, &free_desc_count);
517
518       if (free_desc_count)
519         next = first_free_desc_index;
520     }
521   else
522     free_desc_count = sz - used;
523
524   while (n_left && free_desc_count)
525     {
526       u16 n_added = 0;
527       n_added =
528         add_buffer_to_slot (vm, vif, vring, buffers[0], free_desc_count,
529                             avail, next, mask, do_gso, csum_offload,
530                             node->node_index);
531
532       if (PREDICT_FALSE (n_added == 0))
533         {
534           buffers++;
535           n_left--;
536           continue;
537         }
538       else if (PREDICT_FALSE (n_added > free_desc_count))
539         break;
540
541       avail++;
542       next = (next + n_added) & mask;
543       used += n_added;
544       buffers++;
545       n_left--;
546       free_desc_count -= n_added;
547     }
548
549   if (n_left != frame->n_vectors)
550     {
551       CLIB_MEMORY_STORE_BARRIER ();
552       vring->avail->idx = avail;
553       vring->desc_next = next;
554       vring->desc_in_use = used;
555       if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
556         virtio_kick (vm, vring, vif);
557     }
558
559   if (n_left)
560     {
561       if (retry_count--)
562         goto retry;
563
564       virtio_interface_drop_inline (vm, node->node_index, buffers, n_left,
565                                     VIRTIO_TX_ERROR_NO_FREE_SLOTS);
566     }
567
568   clib_spinlock_unlock_if_init (&vring->lockp);
569
570   return frame->n_vectors - n_left;
571 }
572
573 VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
574                                                vlib_node_runtime_t * node,
575                                                vlib_frame_t * frame)
576 {
577   vnet_main_t *vnm = vnet_get_main ();
578   virtio_main_t *nm = &virtio_main;
579   vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
580   virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
581   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
582
583   if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
584     return virtio_interface_tx_inline (vm, node, frame, vif, 1 /* do_gso */ ,
585                                        1);
586   else if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
587     return virtio_interface_tx_inline (vm, node, frame, vif,
588                                        0 /* no do_gso */ , 1);
589   else
590     return virtio_interface_tx_inline (vm, node, frame, vif,
591                                        0 /* no do_gso */ , 0);
592 }
593
594 static void
595 virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
596                                 u32 node_index)
597 {
598   virtio_main_t *apm = &virtio_main;
599   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
600   virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
601
602   /* Shut off redirection */
603   if (node_index == ~0)
604     {
605       vif->per_interface_next_index = node_index;
606       return;
607     }
608
609   vif->per_interface_next_index =
610     vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
611                         node_index);
612 }
613
614 static void
615 virtio_clear_hw_interface_counters (u32 instance)
616 {
617   /* Nothing for now */
618 }
619
620 static clib_error_t *
621 virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
622                                  vnet_hw_interface_rx_mode mode)
623 {
624   virtio_main_t *mm = &virtio_main;
625   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
626   virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
627   virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid);
628
629   if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
630     {
631       vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
632       return clib_error_return (0, "interrupt mode is not supported");
633     }
634
635   if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
636     vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
637   else
638     vring->avail->flags &= ~VIRTIO_RING_FLAG_MASK_INT;
639
640   return 0;
641 }
642
643 static clib_error_t *
644 virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
645 {
646   virtio_main_t *mm = &virtio_main;
647   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
648   virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
649
650   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
651     {
652       vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
653       vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
654                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
655     }
656   else
657     {
658       vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
659       vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
660     }
661   return 0;
662 }
663
664 static clib_error_t *
665 virtio_subif_add_del_function (vnet_main_t * vnm,
666                                u32 hw_if_index,
667                                struct vnet_sw_interface_t *st, int is_add)
668 {
669   /* Nothing for now */
670   return 0;
671 }
672
673 /* *INDENT-OFF* */
674 VNET_DEVICE_CLASS (virtio_device_class) = {
675   .name = "virtio",
676   .format_device_name = format_virtio_device_name,
677   .format_device = format_virtio_device,
678   .format_tx_trace = format_virtio_tx_trace,
679   .tx_function_n_errors = VIRTIO_TX_N_ERROR,
680   .tx_function_error_strings = virtio_tx_func_error_strings,
681   .rx_redirect_to_node = virtio_set_interface_next_node,
682   .clear_counters = virtio_clear_hw_interface_counters,
683   .admin_up_down_function = virtio_interface_admin_up_down,
684   .subif_add_del_function = virtio_subif_add_del_function,
685   .rx_mode_change_function = virtio_interface_rx_mode_change,
686 };
687
688 /* *INDENT-ON* */
689
690 /*
691  * fd.io coding-style-patch-verification: ON
692  *
693  * Local Variables:
694  * eval: (c-set-style "gnu")
695  * End:
696  */