xref: /illumos-kvm-cmd/hw/virtio-net.c (revision c59cf16e)
1 /*
2  * Virtio Network Device
3  *
4  * Copyright IBM, Corp. 2007
5  *
6  * Authors:
7  *  Anthony Liguori   <aliguori@us.ibm.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2.  See
10  * the COPYING file in the top-level directory.
11  *
12  */
13 
14 #include "iov.h"
15 #include "virtio.h"
16 #include "net.h"
17 #include "net/checksum.h"
18 #include "net/tap.h"
19 #include "qemu-error.h"
20 #include "qemu-timer.h"
21 #include "virtio-net.h"
22 #include "vhost_net.h"
23 
24 #define VIRTIO_NET_VM_VERSION    11
25 
26 #define MAC_TABLE_ENTRIES    64
27 #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */
28 
29 /*
30  * Unfortunately some guest virtio drivers are a little racy with respect to
31  * when they notify us and when they unmask their respective interrupts.
32  * Currently we have to work around this in QEMU. While OSes normally work
33  * around pathological devices, virtual devices here will have to work around
34  * virtual hardware. To put this more concretely, a Linux guest will notify the
35  * host to do processing work before it unmasks interrupts. Therefore, by the
36  * time that we get to virtio_notify interrupts on the available ring won't be
37  * unmasked so we won't inject the interrupt, but the guest will instead wait
38  * indefinitely for one. This leads to us losing data.
39  *
40  * We need to note whether or not we injected an interrupt during a
41  * virtio_notify. If we did not and either of the following conditions about the
42  * ring buffers are true:
43  *
44  * o The last available index processed equals the used index
45  * o The last available index processed does not equal the current
46  *   available index
47  *
48  * If this is the case, then we set up a small timer that runs for 500 ticks,
49  * each tick is 10ms long. If we reach 500 ticks, then we just ignore it. This
50  * is actually a valid position because the guest could have transmitted a small
51  * amount of packets, but not enough to actually cause it to need injection. If
52  * we get notified, aka hit virtio_net_handle_tx_timer, then we stop the timer,
53  * because we're about to do processing that may inject an interrupt. Finally,
54  * if on a tick we check two different conditions. The first is to see if the
55  * last processed available ring index is not equal to the current available
56  * ring index. If that is true, then we effectively call virtqueue_flush as
57  * virtio_net_tx_timer would.  Finally we check if the last available ring index
58  * is equal to the used ring index and interrupts are not masked. If this is the
59  * case, then we simply inject the interrupt and continue.
60  *
61  * This is summarized by the following rough state transition diagram:
62  *
63  *                                    Otherwise     +---+
64  *  virtqueue_ --+                    increment +---*   |
65  *  flush()      |                    tick count   \|/  |  + avail ring
66  *  finishes     |                     +-------------+  |  | index >
67  *  without  +---*-------------------->|             |--+  | last avail
68  *  injecting|                         |   Timer     |     | index pro-
69  *  an intr. |     +-----*-------------|   Active    |     | cessed
70  *           |     |     |             |             |-----*-----------+
71  *           |     |     |             +-------------+                 |
72  *           |     |     +- 500 ticks    |     |                       |
73  *           |     |        elapse       |     *--+ Avail ring         |
74  *           |    \|/                    |     |    unmasked           |
75  *         +-------------+               |     |                       |
76  *         |             |<--*-----------+     |     +--------+        |
77  *         |   Timer     |   |                 |     |        |        |
78  *         |   Inactive  |   +- virtio_net_    +---->| Inject |        |
79  *         |             |      handle_tx_           | MSI/x  |        |
80  *         +-------------+      timer() runs         |        |        |
81  *           ^      ^                                +--------+        |
82  *           |      |                       +- always    |             |
83  *           |      |                       |            |             |
84  *           |      +-----------------------*------------+             |
85  *           |                                                         |
86  *           |               +- always       +------------------+      |
87  *           |               |               |                  |      |
88  *           +---------------*---------------| Flush Virtqueues |<-----+
89  *                                           |                  |
90  *                                           +------------------+
91  */
92 
93 
94 #define	REINJECT_TICK_RATE	(10000000)	/* 10ms in ns */
95 #define REINJECT_DEADMAN	500		/* 5s in ticks */
96 
97 typedef enum rein_act {
98 	REIN_INJECT,
99 	REIN_DEADMAN,
100 	REIN_RUN
101 } rein_act_t;
102 
103 #define	REIN_RING_MAX	64
104 
105 typedef struct rein_event {
106 	rein_act_t 	re_act;
107 	hrtime_t	re_time;
108 	uint64_t	re_other;
109 	struct timeval	re_tval;
110 } rein_event_t;
111 
112 typedef struct VirtIONet
113 {
114     VirtIODevice vdev;
115     uint8_t mac[ETH_ALEN];
116     uint16_t status;
117     VirtQueue *rx_vq;
118     VirtQueue *tx_vq;
119     VirtQueue *ctrl_vq;
120     NICState *nic;
121     QEMUTimer *tx_timer;
122     QEMUBH *tx_bh;
123     uint32_t tx_timeout;
124     int32_t tx_burst;
125     int tx_waiting;
126     uint32_t has_vnet_hdr;
127     uint8_t has_ufo;
128     struct {
129         VirtQueueElement elem;
130         ssize_t len;
131     } async_tx;
132     int mergeable_rx_bufs;
133     uint8_t promisc;
134     uint8_t allmulti;
135     uint8_t alluni;
136     uint8_t nomulti;
137     uint8_t nouni;
138     uint8_t nobcast;
139     uint8_t vhost_started;
140     struct {
141         int in_use;
142         int first_multi;
143         uint8_t multi_overflow;
144         uint8_t uni_overflow;
145         uint8_t *macs;
146     } mac_table;
147     uint32_t *vlans;
148     DeviceState *qdev;
149     QEMUTimer *rein_timer;
150     uint32_t rein_timer_ticks;
151     uint8_t rein_timer_act;
152     uint32_t rein_ring_idx;
153     rein_event_t rein_ring[REIN_RING_MAX];
154     uint64_t rein_n_dead;
155     uint64_t rein_n_inject;
156     uint64_t rein_n_rerun;
157 } VirtIONet;
158 
159 static void virtio_net_handle_tx_timer(VirtIODevice *, VirtQueue *);
160 
virtio_net_rein_event(VirtIONet * n,rein_act_t act,uint64_t other)161 static void virtio_net_rein_event(VirtIONet *n, rein_act_t act, uint64_t other)
162 {
163 	int index = n->rein_ring_idx;
164 	n->rein_ring_idx = (n->rein_ring_idx + 1) % REIN_RING_MAX;
165 	rein_event_t *rep = n->rein_ring + index;
166 	rep->re_time = gethrtime();
167 	rep->re_act = act;
168 	rep->re_other = other;
169 	(void) gettimeofday(&rep->re_tval, NULL);
170 }
171 
virtio_net_rein_disable(VirtIONet * n)172 static void virtio_net_rein_disable(VirtIONet *n)
173 {
174 	qemu_del_timer(n->rein_timer);
175 	n->rein_timer_act = 0;
176 }
177 
virtio_net_rein_enable(VirtIONet * n)178 static void virtio_net_rein_enable(VirtIONet *n)
179 {
180 	n->rein_timer_ticks = 0;
181 	qemu_mod_timer(n->rein_timer,
182 	    qemu_get_clock(vm_clock) + REINJECT_TICK_RATE);
183 	n->rein_timer_act = 1;
184 }
185 
virtio_net_rein_tick(void * opaque)186 static void virtio_net_rein_tick(void *opaque)
187 {
188 	int ret;
189 	VirtIONet *n = opaque;
190 	assert(n->rein_timer_act);
191 
192 	n->rein_timer_ticks++;
193 
194 	/* Give up, this may be completely reasonable */
195 	if (n->rein_timer_ticks > REINJECT_DEADMAN) {
196 		virtio_net_rein_event(n, REIN_DEADMAN, n->rein_timer_ticks);
197 		virtio_net_rein_disable(n);
198 		n->rein_n_dead++;
199 		return;
200 	}
201 
202 	ret = virtqueue_stalled(&n->vdev, n->tx_vq);
203 	if (ret == 1) {
204 		virtio_net_rein_event(n, REIN_INJECT, n->rein_timer_ticks);
205 		virtio_net_rein_disable(n);
206 		n->rein_n_inject++;
207 		return;
208 	} else if (ret == 2) {
209 		virtio_net_rein_event(n, REIN_RUN, n->rein_timer_ticks);
210 		virtio_net_rein_disable(n);
211 		virtio_net_handle_tx_timer(&n->vdev, n->tx_vq);
212 		n->rein_n_rerun++;
213 		return;
214 	}
215 
216 	assert(ret == 0);
217 	qemu_mod_timer(n->rein_timer,
218 	    qemu_get_clock(vm_clock) + REINJECT_TICK_RATE);
219 }
220 
221 /* TODO
222  * - we could suppress RX interrupt if we were so inclined.
223  */
224 
to_virtio_net(VirtIODevice * vdev)225 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
226 {
227     return (VirtIONet *)vdev;
228 }
229 
virtio_net_get_config(VirtIODevice * vdev,uint8_t * config)230 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
231 {
232     VirtIONet *n = to_virtio_net(vdev);
233     struct virtio_net_config netcfg;
234 
235     stw_p(&netcfg.status, n->status);
236     memcpy(netcfg.mac, n->mac, ETH_ALEN);
237     memcpy(config, &netcfg, sizeof(netcfg));
238 }
239 
virtio_net_set_config(VirtIODevice * vdev,const uint8_t * config)240 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
241 {
242     VirtIONet *n = to_virtio_net(vdev);
243     struct virtio_net_config netcfg;
244 
245     memcpy(&netcfg, config, sizeof(netcfg));
246 
247     if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
248         memcpy(n->mac, netcfg.mac, ETH_ALEN);
249         qemu_format_nic_info_str(&n->nic->nc, n->mac);
250     }
251 }
252 
virtio_net_started(VirtIONet * n,uint8_t status)253 static bool virtio_net_started(VirtIONet *n, uint8_t status)
254 {
255     return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
256         (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running;
257 }
258 
virtio_net_vhost_status(VirtIONet * n,uint8_t status)259 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
260 {
261     if (!n->nic->nc.peer) {
262         return;
263     }
264     if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
265         return;
266     }
267 
268     if (!tap_get_vhost_net(n->nic->nc.peer)) {
269         return;
270     }
271     if (!!n->vhost_started == virtio_net_started(n, status)) {
272         return;
273     }
274     if (!n->vhost_started) {
275         int r;
276         if (!vhost_net_query(tap_get_vhost_net(n->nic->nc.peer), &n->vdev)) {
277             return;
278         }
279         r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
280         if (r < 0) {
281             error_report("unable to start vhost net: %d: "
282                          "falling back on userspace virtio", -r);
283         } else {
284             n->vhost_started = 1;
285         }
286     } else {
287         vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
288         n->vhost_started = 0;
289     }
290 }
291 
virtio_net_set_status(struct VirtIODevice * vdev,uint8_t status)292 static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
293 {
294     VirtIONet *n = to_virtio_net(vdev);
295 
296     virtio_net_vhost_status(n, status);
297 
298     if (!n->tx_waiting) {
299         return;
300     }
301 
302     if (virtio_net_started(n, status) && !n->vhost_started) {
303         if (n->tx_timer) {
304             qemu_mod_timer(n->tx_timer,
305                            qemu_get_clock(vm_clock) + n->tx_timeout);
306         } else {
307             qemu_bh_schedule(n->tx_bh);
308         }
309     } else {
310         if (n->tx_timer) {
311             qemu_del_timer(n->tx_timer);
312         } else {
313             qemu_bh_cancel(n->tx_bh);
314         }
315     }
316 }
317 
virtio_net_set_link_status(VLANClientState * nc)318 static void virtio_net_set_link_status(VLANClientState *nc)
319 {
320     VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
321     uint16_t old_status = n->status;
322 
323     if (nc->link_down)
324         n->status &= ~VIRTIO_NET_S_LINK_UP;
325     else
326         n->status |= VIRTIO_NET_S_LINK_UP;
327 
328     if (n->status != old_status)
329         virtio_notify_config(&n->vdev);
330 
331     virtio_net_set_status(&n->vdev, n->vdev.status);
332 }
333 
virtio_net_reset(VirtIODevice * vdev)334 static void virtio_net_reset(VirtIODevice *vdev)
335 {
336     VirtIONet *n = to_virtio_net(vdev);
337 
338     /* Reset back to compatibility mode */
339     n->promisc = 1;
340     n->allmulti = 0;
341     n->alluni = 0;
342     n->nomulti = 0;
343     n->nouni = 0;
344     n->nobcast = 0;
345 
346     /* Flush any MAC and VLAN filter table state */
347     n->mac_table.in_use = 0;
348     n->mac_table.first_multi = 0;
349     n->mac_table.multi_overflow = 0;
350     n->mac_table.uni_overflow = 0;
351     memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
352     memset(n->vlans, 0, MAX_VLAN >> 3);
353 }
354 
peer_has_vnet_hdr(VirtIONet * n)355 static int peer_has_vnet_hdr(VirtIONet *n)
356 {
357     if (!n->nic->nc.peer)
358         return 0;
359 
360     if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
361         return 0;
362 
363     n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
364 
365     return n->has_vnet_hdr;
366 }
367 
peer_has_ufo(VirtIONet * n)368 static int peer_has_ufo(VirtIONet *n)
369 {
370     if (!peer_has_vnet_hdr(n))
371         return 0;
372 
373     n->has_ufo = tap_has_ufo(n->nic->nc.peer);
374 
375     return n->has_ufo;
376 }
377 
virtio_net_get_features(VirtIODevice * vdev,uint32_t features)378 static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
379 {
380     VirtIONet *n = to_virtio_net(vdev);
381 
382     features |= (1 << VIRTIO_NET_F_MAC);
383 
384     if (peer_has_vnet_hdr(n)) {
385         tap_using_vnet_hdr(n->nic->nc.peer, 1);
386     } else {
387         features &= ~(0x1 << VIRTIO_NET_F_CSUM);
388         features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
389         features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
390         features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
391 
392         features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
393         features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
394         features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
395         features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
396     }
397 
398     if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
399         features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
400         features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
401     }
402 
403     if (!n->nic->nc.peer ||
404         n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
405         return features;
406     }
407     if (!tap_get_vhost_net(n->nic->nc.peer)) {
408         return features;
409     }
410     return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
411 }
412 
virtio_net_bad_features(VirtIODevice * vdev)413 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
414 {
415     uint32_t features = 0;
416 
417     /* Linux kernel 2.6.25.  It understood MAC (as everyone must),
418      * but also these: */
419     features |= (1 << VIRTIO_NET_F_MAC);
420     features |= (1 << VIRTIO_NET_F_CSUM);
421     features |= (1 << VIRTIO_NET_F_HOST_TSO4);
422     features |= (1 << VIRTIO_NET_F_HOST_TSO6);
423     features |= (1 << VIRTIO_NET_F_HOST_ECN);
424 
425     return features;
426 }
427 
virtio_net_set_features(VirtIODevice * vdev,uint32_t features)428 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
429 {
430     VirtIONet *n = to_virtio_net(vdev);
431 
432     n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
433 
434     if (n->has_vnet_hdr) {
435         tap_set_offload(n->nic->nc.peer,
436                         (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
437                         (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
438                         (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
439                         (features >> VIRTIO_NET_F_GUEST_ECN)  & 1,
440                         (features >> VIRTIO_NET_F_GUEST_UFO)  & 1);
441     }
442     if (!n->nic->nc.peer ||
443         n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
444         return;
445     }
446     if (!tap_get_vhost_net(n->nic->nc.peer)) {
447         return;
448     }
449     vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
450 }
451 
virtio_net_handle_rx_mode(VirtIONet * n,uint8_t cmd,VirtQueueElement * elem)452 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
453                                      VirtQueueElement *elem)
454 {
455     uint8_t on;
456 
457     if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
458         error_report("virtio-net ctrl invalid rx mode command");
459         exit(1);
460     }
461 
462     on = ldub_p(elem->out_sg[1].iov_base);
463 
464     if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
465         n->promisc = on;
466     else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
467         n->allmulti = on;
468     else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
469         n->alluni = on;
470     else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
471         n->nomulti = on;
472     else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
473         n->nouni = on;
474     else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
475         n->nobcast = on;
476     else
477         return VIRTIO_NET_ERR;
478 
479     return VIRTIO_NET_OK;
480 }
481 
virtio_net_handle_mac(VirtIONet * n,uint8_t cmd,VirtQueueElement * elem)482 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
483                                  VirtQueueElement *elem)
484 {
485     struct virtio_net_ctrl_mac mac_data;
486 
487     if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
488         elem->out_sg[1].iov_len < sizeof(mac_data) ||
489         elem->out_sg[2].iov_len < sizeof(mac_data))
490         return VIRTIO_NET_ERR;
491 
492     n->mac_table.in_use = 0;
493     n->mac_table.first_multi = 0;
494     n->mac_table.uni_overflow = 0;
495     n->mac_table.multi_overflow = 0;
496     memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
497 
498     mac_data.entries = ldl_p(elem->out_sg[1].iov_base);
499 
500     if (sizeof(mac_data.entries) +
501         (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
502         return VIRTIO_NET_ERR;
503 
504     if (mac_data.entries <= MAC_TABLE_ENTRIES) {
505         memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
506                mac_data.entries * ETH_ALEN);
507         n->mac_table.in_use += mac_data.entries;
508     } else {
509         n->mac_table.uni_overflow = 1;
510     }
511 
512     n->mac_table.first_multi = n->mac_table.in_use;
513 
514     mac_data.entries = ldl_p(elem->out_sg[2].iov_base);
515 
516     if (sizeof(mac_data.entries) +
517         (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
518         return VIRTIO_NET_ERR;
519 
520     if (mac_data.entries) {
521         if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
522             memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
523                    elem->out_sg[2].iov_base + sizeof(mac_data),
524                    mac_data.entries * ETH_ALEN);
525             n->mac_table.in_use += mac_data.entries;
526         } else {
527             n->mac_table.multi_overflow = 1;
528         }
529     }
530 
531     return VIRTIO_NET_OK;
532 }
533 
virtio_net_handle_vlan_table(VirtIONet * n,uint8_t cmd,VirtQueueElement * elem)534 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
535                                         VirtQueueElement *elem)
536 {
537     uint16_t vid;
538 
539     if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
540         error_report("virtio-net ctrl invalid vlan command");
541         return VIRTIO_NET_ERR;
542     }
543 
544     vid = lduw_p(elem->out_sg[1].iov_base);
545 
546     if (vid >= MAX_VLAN)
547         return VIRTIO_NET_ERR;
548 
549     if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
550         n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
551     else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
552         n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
553     else
554         return VIRTIO_NET_ERR;
555 
556     return VIRTIO_NET_OK;
557 }
558 
virtio_net_handle_ctrl(VirtIODevice * vdev,VirtQueue * vq)559 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
560 {
561     VirtIONet *n = to_virtio_net(vdev);
562     struct virtio_net_ctrl_hdr ctrl;
563     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
564     VirtQueueElement elem;
565 
566     while (virtqueue_pop(vq, &elem)) {
567         if ((elem.in_num < 1) || (elem.out_num < 1)) {
568             error_report("virtio-net ctrl missing headers");
569             exit(1);
570         }
571 
572         if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
573             elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
574             error_report("virtio-net ctrl header not in correct element");
575             exit(1);
576         }
577 
578         ctrl.class = ldub_p(elem.out_sg[0].iov_base);
579         ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
580 
581         if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
582             status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
583         else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
584             status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
585         else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
586             status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
587 
588         stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
589 
590         virtqueue_push(vq, &elem, sizeof(status));
591         virtio_notify(vdev, vq);
592     }
593 }
594 
595 /* RX */
596 
virtio_net_handle_rx(VirtIODevice * vdev,VirtQueue * vq)597 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
598 {
599     VirtIONet *n = to_virtio_net(vdev);
600 
601     qemu_flush_queued_packets(&n->nic->nc);
602 
603     /* We now have RX buffers, signal to the IO thread to break out of the
604      * select to re-poll the tap file descriptor */
605     qemu_notify_event();
606 }
607 
virtio_net_can_receive(VLANClientState * nc)608 static int virtio_net_can_receive(VLANClientState *nc)
609 {
610     VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
611     if (!n->vdev.vm_running) {
612         return 0;
613     }
614 
615     if (!virtio_queue_ready(n->rx_vq) ||
616         !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
617         return 0;
618 
619     return 1;
620 }
621 
virtio_net_has_buffers(VirtIONet * n,int bufsize)622 static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
623 {
624     if (virtio_queue_empty(n->rx_vq) ||
625         (n->mergeable_rx_bufs &&
626          !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
627         virtio_queue_set_notification(n->rx_vq, 1);
628 
629         /* To avoid a race condition where the guest has made some buffers
630          * available after the above check but before notification was
631          * enabled, check for available buffers again.
632          */
633         if (virtio_queue_empty(n->rx_vq) ||
634             (n->mergeable_rx_bufs &&
635              !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
636             return 0;
637     }
638 
639     virtio_queue_set_notification(n->rx_vq, 0);
640     return 1;
641 }
642 
643 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
644  * it never finds out that the packets don't have valid checksums.  This
645  * causes dhclient to get upset.  Fedora's carried a patch for ages to
646  * fix this with Xen but it hasn't appeared in an upstream release of
647  * dhclient yet.
648  *
649  * To avoid breaking existing guests, we catch udp packets and add
650  * checksums.  This is terrible but it's better than hacking the guest
651  * kernels.
652  *
653  * N.B. if we introduce a zero-copy API, this operation is no longer free so
654  * we should provide a mechanism to disable it to avoid polluting the host
655  * cache.
656  */
work_around_broken_dhclient(struct virtio_net_hdr * hdr,const uint8_t * buf,size_t size)657 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
658                                         const uint8_t *buf, size_t size)
659 {
660     if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
661         (size > 27 && size < 1500) && /* normal sized MTU */
662         (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
663         (buf[23] == 17) && /* ip.protocol == UDP */
664         (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
665         /* FIXME this cast is evil */
666         net_checksum_calculate((uint8_t *)buf, size);
667         hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
668     }
669 }
670 
receive_header(VirtIONet * n,struct iovec * iov,int iovcnt,const void * buf,size_t size,size_t hdr_len)671 static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
672                           const void *buf, size_t size, size_t hdr_len)
673 {
674     struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
675     int offset = 0;
676 
677     hdr->flags = 0;
678     hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
679 
680     if (n->has_vnet_hdr) {
681         memcpy(hdr, buf, sizeof(*hdr));
682         offset = sizeof(*hdr);
683         work_around_broken_dhclient(hdr, buf + offset, size - offset);
684     }
685 
686     /* We only ever receive a struct virtio_net_hdr from the tapfd,
687      * but we may be passing along a larger header to the guest.
688      */
689     iov[0].iov_base += hdr_len;
690     iov[0].iov_len  -= hdr_len;
691 
692     return offset;
693 }
694 
receive_filter(VirtIONet * n,const uint8_t * buf,int size)695 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
696 {
697     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
698     static const uint8_t vlan[] = {0x81, 0x00};
699     uint8_t *ptr = (uint8_t *)buf;
700     int i;
701 
702     if (n->promisc)
703         return 1;
704 
705     if (n->has_vnet_hdr) {
706         ptr += sizeof(struct virtio_net_hdr);
707     }
708 
709     if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
710         int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
711         if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
712             return 0;
713     }
714 
715     if (ptr[0] & 1) { // multicast
716         if (!memcmp(ptr, bcast, sizeof(bcast))) {
717             return !n->nobcast;
718         } else if (n->nomulti) {
719             return 0;
720         } else if (n->allmulti || n->mac_table.multi_overflow) {
721             return 1;
722         }
723 
724         for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
725             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
726                 return 1;
727             }
728         }
729     } else { // unicast
730         if (n->nouni) {
731             return 0;
732         } else if (n->alluni || n->mac_table.uni_overflow) {
733             return 1;
734         } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
735             return 1;
736         }
737 
738         for (i = 0; i < n->mac_table.first_multi; i++) {
739             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
740                 return 1;
741             }
742         }
743     }
744 
745     return 0;
746 }
747 
virtio_net_receive(VLANClientState * nc,const uint8_t * buf,size_t size)748 static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
749 {
750     VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
751     struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
752     size_t guest_hdr_len, offset, i, host_hdr_len;
753 
754     if (!virtio_net_can_receive(&n->nic->nc))
755         return -1;
756 
757     /* hdr_len refers to the header we supply to the guest */
758     guest_hdr_len = n->mergeable_rx_bufs ?
759         sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
760 
761 
762     host_hdr_len = n->has_vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
763     if (!virtio_net_has_buffers(n, size + guest_hdr_len - host_hdr_len))
764         return 0;
765 
766     if (!receive_filter(n, buf, size))
767         return size;
768 
769     offset = i = 0;
770 
771     while (offset < size) {
772         VirtQueueElement elem;
773         int len, total;
774         struct iovec sg[VIRTQUEUE_MAX_SIZE];
775 
776         total = 0;
777 
778         if (virtqueue_pop(n->rx_vq, &elem) == 0) {
779             if (i == 0)
780                 return -1;
781             error_report("virtio-net unexpected empty queue: "
782                     "i %zd mergeable %d offset %zd, size %zd, "
783                     "guest hdr len %zd, host hdr len %zd guest features 0x%x",
784                     i, n->mergeable_rx_bufs, offset, size,
785                     guest_hdr_len, host_hdr_len, n->vdev.guest_features);
786             exit(1);
787         }
788 
789         if (elem.in_num < 1) {
790             error_report("virtio-net receive queue contains no in buffers");
791             exit(1);
792         }
793 
794         if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != guest_hdr_len) {
795             error_report("virtio-net header not in first element");
796             exit(1);
797         }
798 
799         memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
800 
801         if (i == 0) {
802             if (n->mergeable_rx_bufs)
803                 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
804 
805             offset += receive_header(n, sg, elem.in_num,
806                                      buf + offset, size - offset, guest_hdr_len);
807             total += guest_hdr_len;
808         }
809 
810         /* copy in packet.  ugh */
811         len = iov_from_buf(sg, elem.in_num,
812                            buf + offset, size - offset);
813         total += len;
814         offset += len;
815         /* If buffers can't be merged, at this point we
816          * must have consumed the complete packet.
817          * Otherwise, drop it. */
818         if (!n->mergeable_rx_bufs && offset < size) {
819 #if 0
820             error_report("virtio-net truncated non-mergeable packet: "
821                          "i %zd mergeable %d offset %zd, size %zd, "
822                          "guest hdr len %zd, host hdr len %zd",
823                          i, n->mergeable_rx_bufs,
824                          offset, size, guest_hdr_len, host_hdr_len);
825 #endif
826             return size;
827         }
828 
829         /* signal other side */
830         virtqueue_fill(n->rx_vq, &elem, total, i++);
831     }
832 
833     if (mhdr) {
834         stw_p(&mhdr->num_buffers, i);
835     }
836 
837     virtqueue_flush(n->rx_vq, i);
838     virtio_notify(&n->vdev, n->rx_vq);
839 
840     return size;
841 }
842 
843 static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
844 
virtio_net_tx_complete(VLANClientState * nc,ssize_t len)845 static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
846 {
847     VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
848 
849     virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
850     virtio_notify(&n->vdev, n->tx_vq);
851 
852     n->async_tx.elem.out_num = n->async_tx.len = 0;
853 
854     virtio_queue_set_notification(n->tx_vq, 1);
855     virtio_net_flush_tx(n, n->tx_vq);
856 }
857 
858 /* TX */
virtio_net_flush_tx(VirtIONet * n,VirtQueue * vq)859 static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
860 {
861     VirtQueueElement elem;
862     int32_t num_packets = 0;
863     int32_t inject = 1;
864     if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
865         return num_packets;
866     }
867 
868     assert(n->vdev.vm_running);
869 
870     if (n->async_tx.elem.out_num) {
871         virtio_queue_set_notification(n->tx_vq, 0);
872         return num_packets;
873     }
874 
875     while (virtqueue_pop(vq, &elem)) {
876         ssize_t ret, len = 0;
877         unsigned int out_num = elem.out_num;
878         struct iovec *out_sg = &elem.out_sg[0];
879         unsigned hdr_len;
880 
881         /* hdr_len refers to the header received from the guest */
882         hdr_len = n->mergeable_rx_bufs ?
883             sizeof(struct virtio_net_hdr_mrg_rxbuf) :
884             sizeof(struct virtio_net_hdr);
885 
886         if (out_num < 1 || out_sg->iov_len != hdr_len) {
887             error_report("virtio-net header not in first element");
888             exit(1);
889         }
890 
891         /* ignore the header if GSO is not supported */
892         if (!n->has_vnet_hdr) {
893             out_num--;
894             out_sg++;
895             len += hdr_len;
896         } else if (n->mergeable_rx_bufs) {
897             /* tapfd expects a struct virtio_net_hdr */
898             hdr_len -= sizeof(struct virtio_net_hdr);
899             out_sg->iov_len -= hdr_len;
900             len += hdr_len;
901         }
902 
903         ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
904                                       virtio_net_tx_complete);
905         if (ret == 0) {
906             virtio_queue_set_notification(n->tx_vq, 0);
907             n->async_tx.elem = elem;
908             n->async_tx.len  = len;
909             return -EBUSY;
910         }
911 
912         len += ret;
913 
914         virtqueue_push(vq, &elem, len);
915         inject = virtio_notify(&n->vdev, vq);
916 
917         if (++num_packets >= n->tx_burst) {
918             break;
919         }
920     }
921 
922     if (inject == 0 && virtqueue_handled(vq))
923 	    virtio_net_rein_enable(n);
924 
925     return num_packets;
926 }
927 
virtio_net_handle_tx_timer(VirtIODevice * vdev,VirtQueue * vq)928 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
929 {
930     VirtIONet *n = to_virtio_net(vdev);
931 
932     /* This happens when device was stopped but VCPU wasn't. */
933     if (!n->vdev.vm_running) {
934         n->tx_waiting = 1;
935         return;
936     }
937 
938     /*
939      * Kill the broken guest timer. The reason we are here is because the guest
940      * has kicked us to send packets therefore we don't need to go back and
941      * consider injecting it with interrupts because we will do that again
942      * naturally. We also don't reset
943      */
944     if (n->rein_timer_act)
945 	    virtio_net_rein_disable(n);
946 
947 
948     if (n->tx_waiting) {
949         virtio_queue_set_notification(vq, 1);
950         qemu_del_timer(n->tx_timer);
951         n->tx_waiting = 0;
952         virtio_net_flush_tx(n, vq);
953     } else {
954         qemu_mod_timer(n->tx_timer,
955                        qemu_get_clock(vm_clock) + n->tx_timeout);
956         n->tx_waiting = 1;
957         virtio_queue_set_notification(vq, 0);
958     }
959 }
960 
virtio_net_handle_tx_bh(VirtIODevice * vdev,VirtQueue * vq)961 static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
962 {
963     VirtIONet *n = to_virtio_net(vdev);
964 
965     if (unlikely(n->tx_waiting)) {
966         return;
967     }
968     n->tx_waiting = 1;
969     /* This happens when device was stopped but VCPU wasn't. */
970     if (!n->vdev.vm_running) {
971         return;
972     }
973     virtio_queue_set_notification(vq, 0);
974     qemu_bh_schedule(n->tx_bh);
975 }
976 
virtio_net_tx_timer(void * opaque)977 static void virtio_net_tx_timer(void *opaque)
978 {
979     VirtIONet *n = opaque;
980     assert(n->vdev.vm_running);
981 
982     n->tx_waiting = 0;
983 
984     /* Just in case the driver is not ready on more */
985     if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
986         return;
987 
988     virtio_queue_set_notification(n->tx_vq, 1);
989     virtio_net_flush_tx(n, n->tx_vq);
990 }
991 
virtio_net_tx_bh(void * opaque)992 static void virtio_net_tx_bh(void *opaque)
993 {
994     VirtIONet *n = opaque;
995     int32_t ret;
996 
997     assert(n->vdev.vm_running);
998 
999     n->tx_waiting = 0;
1000 
1001     /* Just in case the driver is not ready on more */
1002     if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
1003         return;
1004 
1005     ret = virtio_net_flush_tx(n, n->tx_vq);
1006     if (ret == -EBUSY) {
1007         return; /* Notification re-enable handled by tx_complete */
1008     }
1009 
1010     /* If we flush a full burst of packets, assume there are
1011      * more coming and immediately reschedule */
1012     if (ret >= n->tx_burst) {
1013         qemu_bh_schedule(n->tx_bh);
1014         n->tx_waiting = 1;
1015         return;
1016     }
1017 
1018     /* If less than a full burst, re-enable notification and flush
1019      * anything that may have come in while we weren't looking.  If
1020      * we find something, assume the guest is still active and reschedule */
1021     virtio_queue_set_notification(n->tx_vq, 1);
1022     if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
1023         virtio_queue_set_notification(n->tx_vq, 0);
1024         qemu_bh_schedule(n->tx_bh);
1025         n->tx_waiting = 1;
1026     }
1027 }
1028 
virtio_net_save(QEMUFile * f,void * opaque)1029 static void virtio_net_save(QEMUFile *f, void *opaque)
1030 {
1031     VirtIONet *n = opaque;
1032 
1033     /* At this point, backend must be stopped, otherwise
1034      * it might keep writing to memory. */
1035     assert(!n->vhost_started);
1036     virtio_save(&n->vdev, f);
1037 
1038     qemu_put_buffer(f, n->mac, ETH_ALEN);
1039     qemu_put_be32(f, n->tx_waiting);
1040     qemu_put_be32(f, n->mergeable_rx_bufs);
1041     qemu_put_be16(f, n->status);
1042     qemu_put_byte(f, n->promisc);
1043     qemu_put_byte(f, n->allmulti);
1044     qemu_put_be32(f, n->mac_table.in_use);
1045     qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
1046     qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
1047     qemu_put_be32(f, n->has_vnet_hdr);
1048     qemu_put_byte(f, n->mac_table.multi_overflow);
1049     qemu_put_byte(f, n->mac_table.uni_overflow);
1050     qemu_put_byte(f, n->alluni);
1051     qemu_put_byte(f, n->nomulti);
1052     qemu_put_byte(f, n->nouni);
1053     qemu_put_byte(f, n->nobcast);
1054     qemu_put_byte(f, n->has_ufo);
1055 }
1056 
virtio_net_load(QEMUFile * f,void * opaque,int version_id)1057 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
1058 {
1059     VirtIONet *n = opaque;
1060     int i;
1061 
1062     if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
1063         return -EINVAL;
1064 
1065     virtio_load(&n->vdev, f);
1066 
1067     qemu_get_buffer(f, n->mac, ETH_ALEN);
1068     n->tx_waiting = qemu_get_be32(f);
1069     n->mergeable_rx_bufs = qemu_get_be32(f);
1070 
1071     if (version_id >= 3)
1072         n->status = qemu_get_be16(f);
1073 
1074     if (version_id >= 4) {
1075         if (version_id < 8) {
1076             n->promisc = qemu_get_be32(f);
1077             n->allmulti = qemu_get_be32(f);
1078         } else {
1079             n->promisc = qemu_get_byte(f);
1080             n->allmulti = qemu_get_byte(f);
1081         }
1082     }
1083 
1084     if (version_id >= 5) {
1085         n->mac_table.in_use = qemu_get_be32(f);
1086         /* MAC_TABLE_ENTRIES may be different from the saved image */
1087         if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
1088             qemu_get_buffer(f, n->mac_table.macs,
1089                             n->mac_table.in_use * ETH_ALEN);
1090         } else if (n->mac_table.in_use) {
1091             qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
1092             n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
1093             n->mac_table.in_use = 0;
1094         }
1095     }
1096 
1097     if (version_id >= 6)
1098         qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
1099 
1100     if (version_id >= 7) {
1101         if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
1102             error_report("virtio-net: saved image requires vnet_hdr=on");
1103             return -1;
1104         }
1105 
1106         if (n->has_vnet_hdr) {
1107             tap_using_vnet_hdr(n->nic->nc.peer, 1);
1108             tap_set_offload(n->nic->nc.peer,
1109                     (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
1110                     (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
1111                     (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
1112                     (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN)  & 1,
1113                     (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO)  & 1);
1114         }
1115     }
1116 
1117     if (version_id >= 9) {
1118         n->mac_table.multi_overflow = qemu_get_byte(f);
1119         n->mac_table.uni_overflow = qemu_get_byte(f);
1120     }
1121 
1122     if (version_id >= 10) {
1123         n->alluni = qemu_get_byte(f);
1124         n->nomulti = qemu_get_byte(f);
1125         n->nouni = qemu_get_byte(f);
1126         n->nobcast = qemu_get_byte(f);
1127     }
1128 
1129     if (version_id >= 11) {
1130         if (qemu_get_byte(f) && !peer_has_ufo(n)) {
1131             error_report("virtio-net: saved image requires TUN_F_UFO support");
1132             return -1;
1133         }
1134     }
1135 
1136     /* Find the first multicast entry in the saved MAC filter */
1137     for (i = 0; i < n->mac_table.in_use; i++) {
1138         if (n->mac_table.macs[i * ETH_ALEN] & 1) {
1139             break;
1140         }
1141     }
1142     n->mac_table.first_multi = i;
1143     return 0;
1144 }
1145 
virtio_net_cleanup(VLANClientState * nc)1146 static void virtio_net_cleanup(VLANClientState *nc)
1147 {
1148     VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
1149 
1150     n->nic = NULL;
1151 }
1152 
1153 static NetClientInfo net_virtio_info = {
1154     .type = NET_CLIENT_TYPE_NIC,
1155     .size = sizeof(NICState),
1156     .can_receive = virtio_net_can_receive,
1157     .receive = virtio_net_receive,
1158         .cleanup = virtio_net_cleanup,
1159     .link_status_changed = virtio_net_set_link_status,
1160 };
1161 
virtio_net_init(DeviceState * dev,NICConf * conf,virtio_net_conf * net)1162 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
1163                               virtio_net_conf *net)
1164 {
1165     VirtIONet *n;
1166 
1167     n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
1168                                         sizeof(struct virtio_net_config),
1169                                         sizeof(VirtIONet));
1170 
1171     n->vdev.get_config = virtio_net_get_config;
1172     n->vdev.set_config = virtio_net_set_config;
1173     n->vdev.get_features = virtio_net_get_features;
1174     n->vdev.set_features = virtio_net_set_features;
1175     n->vdev.bad_features = virtio_net_bad_features;
1176     n->vdev.reset = virtio_net_reset;
1177     n->vdev.set_status = virtio_net_set_status;
1178     n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
1179 
1180     if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
1181         error_report("virtio-net: "
1182                      "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1183                      net->tx);
1184         error_report("Defaulting to \"bh\"");
1185     }
1186 
1187     if (net->tx && !strcmp(net->tx, "timer")) {
1188         n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
1189         n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
1190         n->tx_timeout = net->txtimer;
1191     } else {
1192         n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
1193         n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
1194     }
1195     n->rein_timer = qemu_new_timer(vm_clock, virtio_net_rein_tick, n);
1196     n->rein_ring_idx = 0;
1197     bzero(n->rein_ring, sizeof (rein_event_t) * REIN_RING_MAX);
1198     n->rein_n_dead = 0;
1199     n->rein_n_inject = 0;
1200     n->rein_n_rerun = 0;
1201     n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
1202     qemu_macaddr_default_if_unset(&conf->macaddr);
1203     memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
1204     n->status = VIRTIO_NET_S_LINK_UP;
1205 
1206     n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
1207 
1208     qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
1209 
1210     n->tx_waiting = 0;
1211     n->tx_burst = net->txburst;
1212     n->mergeable_rx_bufs = 0;
1213     n->promisc = 1; /* for compatibility */
1214 
1215     n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
1216 
1217     n->vlans = qemu_mallocz(MAX_VLAN >> 3);
1218 
1219     n->qdev = dev;
1220     register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
1221                     virtio_net_save, virtio_net_load, n);
1222 
1223     add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0");
1224 
1225     return &n->vdev;
1226 }
1227 
virtio_net_exit(VirtIODevice * vdev)1228 void virtio_net_exit(VirtIODevice *vdev)
1229 {
1230     VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
1231 
1232     /* This will stop vhost backend if appropriate. */
1233     virtio_net_set_status(vdev, 0);
1234 
1235     qemu_purge_queued_packets(&n->nic->nc);
1236 
1237     unregister_savevm(n->qdev, "virtio-net", n);
1238 
1239     qemu_free(n->mac_table.macs);
1240     qemu_free(n->vlans);
1241 
1242     if (n->tx_timer) {
1243         qemu_del_timer(n->tx_timer);
1244         qemu_free_timer(n->tx_timer);
1245     } else {
1246         qemu_bh_delete(n->tx_bh);
1247     }
1248 
1249     virtio_cleanup(&n->vdev);
1250     qemu_del_vlan_client(&n->nic->nc);
1251 }
1252