gso: add vxlan tunnel support
[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 {
173   if (b->flags & VNET_BUFFER_F_IS_IP4)
174     {
175       ip4_header_t *ip4;
176       generic_header_offset_t gho = { 0 };
177       vnet_generic_header_offset_parser (b, &gho);
178       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
179       hdr->csum_start = gho.l4_hdr_offset;      // 0x22;
180       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
181         {
182           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
183         }
184       else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
185         {
186           hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
187         }
188
189       /*
190        * virtio devices do not support IP4 checksum offload. So driver takes care
191        * of it while doing tx.
192        */
193       ip4 =
194         (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
195       if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
196         ip4->checksum = ip4_header_checksum (ip4);
197     }
198   else if (b->flags & VNET_BUFFER_F_IS_IP6)
199     {
200       generic_header_offset_t gho = { 0 };
201       vnet_generic_header_offset_parser (b, &gho);
202       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
203       hdr->csum_start = gho.l4_hdr_offset;      // 0x36;
204       if (b->flags & VNET_BUFFER_F_OFFLOAD_TCP_CKSUM)
205         {
206           hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
207         }
208       else if (b->flags & VNET_BUFFER_F_OFFLOAD_UDP_CKSUM)
209         {
210           hdr->csum_offset = STRUCT_OFFSET_OF (udp_header_t, checksum);
211         }
212     }
213 }
214
215 static_always_inline void
216 set_gso_offsets (vlib_buffer_t * b, struct virtio_net_hdr_v1 *hdr)
217 {
218   if (b->flags & VNET_BUFFER_F_IS_IP4)
219     {
220       ip4_header_t *ip4;
221       generic_header_offset_t gho = { 0 };
222       vnet_generic_header_offset_parser (b, &gho);
223       hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
224       hdr->gso_size = vnet_buffer2 (b)->gso_size;
225       hdr->hdr_len = gho.hdr_sz;
226       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
227       hdr->csum_start = gho.l4_hdr_offset;      // 0x22;
228       hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
229       ip4 =
230         (ip4_header_t *) (vlib_buffer_get_current (b) + gho.l3_hdr_offset);
231       /*
232        * virtio devices do not support IP4 checksum offload. So driver takes care
233        * of it while doing tx.
234        */
235       if (b->flags & VNET_BUFFER_F_OFFLOAD_IP_CKSUM)
236         ip4->checksum = ip4_header_checksum (ip4);
237     }
238   else if (b->flags & VNET_BUFFER_F_IS_IP6)
239     {
240       generic_header_offset_t gho = { 0 };
241       vnet_generic_header_offset_parser (b, &gho);
242       hdr->gso_type = VIRTIO_NET_HDR_GSO_TCPV6;
243       hdr->gso_size = vnet_buffer2 (b)->gso_size;
244       hdr->hdr_len = gho.hdr_sz;
245       hdr->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
246       hdr->csum_start = gho.l4_hdr_offset;      // 0x36;
247       hdr->csum_offset = STRUCT_OFFSET_OF (tcp_header_t, checksum);
248     }
249 }
250
251 static_always_inline u16
252 add_buffer_to_slot (vlib_main_t * vm, virtio_if_t * vif,
253                     virtio_vring_t * vring, u32 bi, u16 free_desc_count,
254                     u16 avail, u16 next, u16 mask, int do_gso,
255                     int csum_offload, uword node_index)
256 {
257   u16 n_added = 0;
258   int hdr_sz = vif->virtio_net_hdr_sz;
259   struct vring_desc *d;
260   d = &vring->desc[next];
261   vlib_buffer_t *b = vlib_get_buffer (vm, bi);
262   struct virtio_net_hdr_v1 *hdr = vlib_buffer_get_current (b) - hdr_sz;
263
264   clib_memset (hdr, 0, hdr_sz);
265
266   if (b->flags & VNET_BUFFER_F_GSO)
267     {
268       if (do_gso)
269         set_gso_offsets (b, hdr);
270       else
271         {
272           virtio_interface_drop_inline (vm, node_index, &bi, 1,
273                                         VIRTIO_TX_ERROR_GSO_PACKET_DROP);
274           return n_added;
275         }
276     }
277   else if (b->flags & (VNET_BUFFER_F_OFFLOAD_TCP_CKSUM |
278                        VNET_BUFFER_F_OFFLOAD_UDP_CKSUM))
279     {
280       if (csum_offload)
281         set_checksum_offsets (b, hdr);
282       else
283         {
284           virtio_interface_drop_inline (vm, node_index, &bi, 1,
285                                         VIRTIO_TX_ERROR_CSUM_OFFLOAD_PACKET_DROP);
286           return n_added;
287         }
288     }
289
290   if (PREDICT_TRUE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) == 0))
291     {
292       d->addr =
293         ((vif->type == VIRTIO_IF_TYPE_PCI) ? vlib_buffer_get_current_pa (vm,
294                                                                          b) :
295          pointer_to_uword (vlib_buffer_get_current (b))) - hdr_sz;
296       d->len = b->current_length + hdr_sz;
297       d->flags = 0;
298     }
299   else if (vif->features & VIRTIO_FEATURE (VIRTIO_RING_F_INDIRECT_DESC))
300     {
301       /*
302        * We are using single vlib_buffer_t for indirect descriptor(s)
303        * chain. Single descriptor is 16 bytes and vlib_buffer_t
304        * has 2048 bytes space. So maximum long chain can have 128
305        * (=2048/16) indirect descriptors.
306        * It can easily support 65535 bytes of Jumbo frames with
307        * each data buffer size of 512 bytes minimum.
308        */
309       u32 indirect_buffer = 0;
310       if (PREDICT_FALSE (vlib_buffer_alloc (vm, &indirect_buffer, 1) == 0))
311         {
312           virtio_interface_drop_inline (vm, node_index, &bi, 1,
313                                         VIRTIO_TX_ERROR_INDIRECT_DESC_ALLOC_FAILED);
314           return n_added;
315         }
316
317       vlib_buffer_t *indirect_desc = vlib_get_buffer (vm, indirect_buffer);
318       indirect_desc->current_data = 0;
319       indirect_desc->flags |= VLIB_BUFFER_NEXT_PRESENT;
320       indirect_desc->next_buffer = bi;
321       bi = indirect_buffer;
322
323       struct vring_desc *id =
324         (struct vring_desc *) vlib_buffer_get_current (indirect_desc);
325       u32 count = 1;
326       if (vif->type == VIRTIO_IF_TYPE_PCI)
327         {
328           d->addr = vlib_physmem_get_pa (vm, id);
329           id->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
330
331           /*
332            * If VIRTIO_F_ANY_LAYOUT is not negotiated, then virtio_net_hdr
333            * should be presented in separate descriptor and data will start
334            * from next descriptor.
335            */
336           if (PREDICT_TRUE
337               (vif->features & VIRTIO_FEATURE (VIRTIO_F_ANY_LAYOUT)))
338             id->len = b->current_length + hdr_sz;
339           else
340             {
341               id->len = hdr_sz;
342               id->flags = VRING_DESC_F_NEXT;
343               id->next = count;
344               count++;
345               id++;
346               id->addr = vlib_buffer_get_current_pa (vm, b);
347               id->len = b->current_length;
348             }
349           while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
350             {
351               id->flags = VRING_DESC_F_NEXT;
352               id->next = count;
353               count++;
354               id++;
355               b = vlib_get_buffer (vm, b->next_buffer);
356               id->addr = vlib_buffer_get_current_pa (vm, b);
357               id->len = b->current_length;
358             }
359         }
360       else                      /* VIRTIO_IF_TYPE_[TAP | TUN] */
361         {
362           d->addr = pointer_to_uword (id);
363           /* first buffer in chain */
364           id->addr = pointer_to_uword (vlib_buffer_get_current (b)) - hdr_sz;
365           id->len = b->current_length + hdr_sz;
366
367           while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
368             {
369               id->flags = VRING_DESC_F_NEXT;
370               id->next = count;
371               count++;
372               id++;
373               b = vlib_get_buffer (vm, b->next_buffer);
374               id->addr = pointer_to_uword (vlib_buffer_get_current (b));
375               id->len = b->current_length;
376             }
377         }
378       id->flags = 0;
379       id->next = 0;
380       d->len = count * sizeof (struct vring_desc);
381       d->flags = VRING_DESC_F_INDIRECT;
382     }
383   else if (vif->type == VIRTIO_IF_TYPE_PCI)
384     {
385       u16 count = next;
386       vlib_buffer_t *b_temp = b;
387       u16 n_buffers_in_chain = 1;
388
389       /*
390        * Check the length of the chain for the required number of
391        * descriptors. Return from here, retry to get more descriptors,
392        * if chain length is greater than available descriptors.
393        */
394       while (b_temp->flags & VLIB_BUFFER_NEXT_PRESENT)
395         {
396           n_buffers_in_chain++;
397           b_temp = vlib_get_buffer (vm, b_temp->next_buffer);
398         }
399
400       if (n_buffers_in_chain > free_desc_count)
401         return n_buffers_in_chain;
402
403       d->addr = vlib_buffer_get_current_pa (vm, b) - hdr_sz;
404       d->len = b->current_length + hdr_sz;
405
406       while (b->flags & VLIB_BUFFER_NEXT_PRESENT)
407         {
408           d->flags = VRING_DESC_F_NEXT;
409           vring->buffers[count] = bi;
410           b->flags &=
411             ~(VLIB_BUFFER_NEXT_PRESENT | VLIB_BUFFER_TOTAL_LENGTH_VALID);
412           bi = b->next_buffer;
413           b->next_buffer = 0;
414           n_added++;
415           count = (count + 1) & mask;
416           d->next = count;
417           d = &vring->desc[count];
418           b = vlib_get_buffer (vm, bi);
419           d->addr = vlib_buffer_get_current_pa (vm, b);
420           d->len = b->current_length;
421         }
422       d->flags = 0;
423       vring->buffers[count] = bi;
424       vring->avail->ring[avail & mask] = next;
425       n_added++;
426       return n_added;
427     }
428   else
429     {
430       ASSERT (0);
431     }
432   vring->buffers[next] = bi;
433   vring->avail->ring[avail & mask] = next;
434   n_added++;
435   return n_added;
436 }
437
438 static_always_inline void
439 virtio_find_free_desc (virtio_vring_t * vring, u16 size, u16 mask,
440                        u16 req, u16 next, u32 * first_free_desc_index,
441                        u16 * free_desc_count)
442 {
443   u16 start = 0;
444   /* next is used as hint: from where to start looking */
445   for (u16 i = 0; i < size; i++, next++)
446     {
447       if (vring->buffers[next & mask] == ~0)
448         {
449           if (*first_free_desc_index == ~0)
450             {
451               *first_free_desc_index = (next & mask);
452               start = i;
453               (*free_desc_count)++;
454               req--;
455               if (req == 0)
456                 break;
457             }
458           else
459             {
460               if (start + *free_desc_count == i)
461                 {
462                   (*free_desc_count)++;
463                   req--;
464                   if (req == 0)
465                     break;
466                 }
467               else
468                 break;
469             }
470         }
471     }
472 }
473
474 static_always_inline uword
475 virtio_interface_tx_inline (vlib_main_t * vm, vlib_node_runtime_t * node,
476                             vlib_frame_t * frame, virtio_if_t * vif,
477                             int do_gso, int csum_offload)
478 {
479   u16 n_left = frame->n_vectors;
480   virtio_vring_t *vring;
481   u16 qid = vm->thread_index % vif->num_txqs;
482   vring = vec_elt_at_index (vif->txq_vrings, qid);
483   u16 used, next, avail;
484   u16 sz = vring->size;
485   u16 mask = sz - 1;
486   u16 retry_count = 2;
487   u32 *buffers = vlib_frame_vector_args (frame);
488
489   clib_spinlock_lock_if_init (&vring->lockp);
490
491   if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0 &&
492       (vring->last_kick_avail_idx != vring->avail->idx))
493     virtio_kick (vm, vring, vif);
494
495 retry:
496   /* free consumed buffers */
497   virtio_free_used_device_desc (vm, vring, node->node_index);
498
499   used = vring->desc_in_use;
500   next = vring->desc_next;
501   avail = vring->avail->idx;
502
503   u16 free_desc_count = 0;
504
505   if (PREDICT_FALSE (vring->flags & VRING_TX_OUT_OF_ORDER))
506     {
507       u32 first_free_desc_index = ~0;
508
509       virtio_find_free_desc (vring, sz, mask, n_left, next,
510                              &first_free_desc_index, &free_desc_count);
511
512       if (free_desc_count)
513         next = first_free_desc_index;
514     }
515   else
516     free_desc_count = sz - used;
517
518   while (n_left && free_desc_count)
519     {
520       u16 n_added = 0;
521       n_added =
522         add_buffer_to_slot (vm, vif, vring, buffers[0], free_desc_count,
523                             avail, next, mask, do_gso, csum_offload,
524                             node->node_index);
525
526       if (PREDICT_FALSE (n_added == 0))
527         {
528           buffers++;
529           n_left--;
530           continue;
531         }
532       else if (PREDICT_FALSE (n_added > free_desc_count))
533         break;
534
535       avail++;
536       next = (next + n_added) & mask;
537       used += n_added;
538       buffers++;
539       n_left--;
540       free_desc_count -= n_added;
541     }
542
543   if (n_left != frame->n_vectors)
544     {
545       CLIB_MEMORY_STORE_BARRIER ();
546       vring->avail->idx = avail;
547       vring->desc_next = next;
548       vring->desc_in_use = used;
549       if ((vring->used->flags & VIRTIO_RING_FLAG_MASK_INT) == 0)
550         virtio_kick (vm, vring, vif);
551     }
552
553   if (n_left)
554     {
555       if (retry_count--)
556         goto retry;
557
558       virtio_interface_drop_inline (vm, node->node_index, buffers, n_left,
559                                     VIRTIO_TX_ERROR_NO_FREE_SLOTS);
560     }
561
562   clib_spinlock_unlock_if_init (&vring->lockp);
563
564   return frame->n_vectors - n_left;
565 }
566
567 VNET_DEVICE_CLASS_TX_FN (virtio_device_class) (vlib_main_t * vm,
568                                                vlib_node_runtime_t * node,
569                                                vlib_frame_t * frame)
570 {
571   vnet_main_t *vnm = vnet_get_main ();
572   virtio_main_t *nm = &virtio_main;
573   vnet_interface_output_runtime_t *rund = (void *) node->runtime_data;
574   virtio_if_t *vif = pool_elt_at_index (nm->interfaces, rund->dev_instance);
575   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, vif->hw_if_index);
576
577   if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_GSO)
578     return virtio_interface_tx_inline (vm, node, frame, vif, 1 /* do_gso */ ,
579                                        1);
580   else if (hw->flags & VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD)
581     return virtio_interface_tx_inline (vm, node, frame, vif,
582                                        0 /* no do_gso */ , 1);
583   else
584     return virtio_interface_tx_inline (vm, node, frame, vif,
585                                        0 /* no do_gso */ , 0);
586 }
587
588 static void
589 virtio_set_interface_next_node (vnet_main_t * vnm, u32 hw_if_index,
590                                 u32 node_index)
591 {
592   virtio_main_t *apm = &virtio_main;
593   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
594   virtio_if_t *vif = pool_elt_at_index (apm->interfaces, hw->dev_instance);
595
596   /* Shut off redirection */
597   if (node_index == ~0)
598     {
599       vif->per_interface_next_index = node_index;
600       return;
601     }
602
603   vif->per_interface_next_index =
604     vlib_node_add_next (vlib_get_main (), virtio_input_node.index,
605                         node_index);
606 }
607
608 static void
609 virtio_clear_hw_interface_counters (u32 instance)
610 {
611   /* Nothing for now */
612 }
613
614 static clib_error_t *
615 virtio_interface_rx_mode_change (vnet_main_t * vnm, u32 hw_if_index, u32 qid,
616                                  vnet_hw_interface_rx_mode mode)
617 {
618   virtio_main_t *mm = &virtio_main;
619   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
620   virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
621   virtio_vring_t *vring = vec_elt_at_index (vif->rxq_vrings, qid);
622
623   if (vif->type == VIRTIO_IF_TYPE_PCI && !(vif->support_int_mode))
624     {
625       vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
626       return clib_error_return (0, "interrupt mode is not supported");
627     }
628
629   if (mode == VNET_HW_INTERFACE_RX_MODE_POLLING)
630     vring->avail->flags |= VIRTIO_RING_FLAG_MASK_INT;
631   else
632     vring->avail->flags &= ~VIRTIO_RING_FLAG_MASK_INT;
633
634   return 0;
635 }
636
637 static clib_error_t *
638 virtio_interface_admin_up_down (vnet_main_t * vnm, u32 hw_if_index, u32 flags)
639 {
640   virtio_main_t *mm = &virtio_main;
641   vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
642   virtio_if_t *vif = pool_elt_at_index (mm->interfaces, hw->dev_instance);
643
644   if (flags & VNET_SW_INTERFACE_FLAG_ADMIN_UP)
645     {
646       vif->flags |= VIRTIO_IF_FLAG_ADMIN_UP;
647       vnet_hw_interface_set_flags (vnm, vif->hw_if_index,
648                                    VNET_HW_INTERFACE_FLAG_LINK_UP);
649     }
650   else
651     {
652       vif->flags &= ~VIRTIO_IF_FLAG_ADMIN_UP;
653       vnet_hw_interface_set_flags (vnm, vif->hw_if_index, 0);
654     }
655   return 0;
656 }
657
658 static clib_error_t *
659 virtio_subif_add_del_function (vnet_main_t * vnm,
660                                u32 hw_if_index,
661                                struct vnet_sw_interface_t *st, int is_add)
662 {
663   /* Nothing for now */
664   return 0;
665 }
666
667 /* *INDENT-OFF* */
668 VNET_DEVICE_CLASS (virtio_device_class) = {
669   .name = "virtio",
670   .format_device_name = format_virtio_device_name,
671   .format_device = format_virtio_device,
672   .format_tx_trace = format_virtio_tx_trace,
673   .tx_function_n_errors = VIRTIO_TX_N_ERROR,
674   .tx_function_error_strings = virtio_tx_func_error_strings,
675   .rx_redirect_to_node = virtio_set_interface_next_node,
676   .clear_counters = virtio_clear_hw_interface_counters,
677   .admin_up_down_function = virtio_interface_admin_up_down,
678   .subif_add_del_function = virtio_subif_add_del_function,
679   .rx_mode_change_function = virtio_interface_rx_mode_change,
680 };
681
682 /* *INDENT-ON* */
683
684 /*
685  * fd.io coding-style-patch-verification: ON
686  *
687  * Local Variables:
688  * eval: (c-set-style "gnu")
689  * End:
690  */