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