]> git.proxmox.com Git - mirror_qemu.git/blob - hw/net/virtio-net.c
988fe03271bf29a646015ab42994a7619b2eceb0
[mirror_qemu.git] / hw / net / virtio-net.c
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 "qemu/iov.h"
15 #include "hw/virtio/virtio.h"
16 #include "net/net.h"
17 #include "net/checksum.h"
18 #include "net/tap.h"
19 #include "qemu/error-report.h"
20 #include "qemu/timer.h"
21 #include "hw/virtio/virtio-net.h"
22 #include "net/vhost_net.h"
23 #include "hw/virtio/virtio-bus.h"
24
25 #define VIRTIO_NET_VM_VERSION 11
26
27 #define MAC_TABLE_ENTRIES 64
28 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
29
30 /*
31 * Calculate the number of bytes up to and including the given 'field' of
32 * 'container'.
33 */
34 #define endof(container, field) \
35 (offsetof(container, field) + sizeof(((container *)0)->field))
36
37 typedef struct VirtIOFeature {
38 uint32_t flags;
39 size_t end;
40 } VirtIOFeature;
41
42 static VirtIOFeature feature_sizes[] = {
43 {.flags = 1 << VIRTIO_NET_F_MAC,
44 .end = endof(struct virtio_net_config, mac)},
45 {.flags = 1 << VIRTIO_NET_F_STATUS,
46 .end = endof(struct virtio_net_config, status)},
47 {.flags = 1 << VIRTIO_NET_F_MQ,
48 .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
49 {}
50 };
51
52 static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
53 {
54 VirtIONet *n = qemu_get_nic_opaque(nc);
55
56 return &n->vqs[nc->queue_index];
57 }
58
59 static int vq2q(int queue_index)
60 {
61 return queue_index / 2;
62 }
63
64 /* TODO
65 * - we could suppress RX interrupt if we were so inclined.
66 */
67
68 /*
69 * Moving to QOM later in this serie.
70 */
71 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
72 {
73 return (VirtIONet *)vdev;
74 }
75
76 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
77 {
78 VirtIONet *n = to_virtio_net(vdev);
79 struct virtio_net_config netcfg;
80
81 stw_p(&netcfg.status, n->status);
82 stw_p(&netcfg.max_virtqueue_pairs, n->max_queues);
83 memcpy(netcfg.mac, n->mac, ETH_ALEN);
84 memcpy(config, &netcfg, n->config_size);
85 }
86
87 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
88 {
89 VirtIONet *n = to_virtio_net(vdev);
90 struct virtio_net_config netcfg = {};
91
92 memcpy(&netcfg, config, n->config_size);
93
94 if (!(n->vdev.guest_features >> VIRTIO_NET_F_CTRL_MAC_ADDR & 1) &&
95 memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
96 memcpy(n->mac, netcfg.mac, ETH_ALEN);
97 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
98 }
99 }
100
101 static bool virtio_net_started(VirtIONet *n, uint8_t status)
102 {
103 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
104 (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running;
105 }
106
107 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
108 {
109 NetClientState *nc = qemu_get_queue(n->nic);
110 int queues = n->multiqueue ? n->max_queues : 1;
111
112 if (!nc->peer) {
113 return;
114 }
115 if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
116 return;
117 }
118
119 if (!tap_get_vhost_net(nc->peer)) {
120 return;
121 }
122
123 if (!!n->vhost_started == virtio_net_started(n, status) &&
124 !nc->peer->link_down) {
125 return;
126 }
127 if (!n->vhost_started) {
128 int r;
129 if (!vhost_net_query(tap_get_vhost_net(nc->peer), &n->vdev)) {
130 return;
131 }
132 n->vhost_started = 1;
133 r = vhost_net_start(&n->vdev, n->nic->ncs, queues);
134 if (r < 0) {
135 error_report("unable to start vhost net: %d: "
136 "falling back on userspace virtio", -r);
137 n->vhost_started = 0;
138 }
139 } else {
140 vhost_net_stop(&n->vdev, n->nic->ncs, queues);
141 n->vhost_started = 0;
142 }
143 }
144
145 static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
146 {
147 VirtIONet *n = to_virtio_net(vdev);
148 VirtIONetQueue *q;
149 int i;
150 uint8_t queue_status;
151
152 virtio_net_vhost_status(n, status);
153
154 for (i = 0; i < n->max_queues; i++) {
155 q = &n->vqs[i];
156
157 if ((!n->multiqueue && i != 0) || i >= n->curr_queues) {
158 queue_status = 0;
159 } else {
160 queue_status = status;
161 }
162
163 if (!q->tx_waiting) {
164 continue;
165 }
166
167 if (virtio_net_started(n, queue_status) && !n->vhost_started) {
168 if (q->tx_timer) {
169 qemu_mod_timer(q->tx_timer,
170 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
171 } else {
172 qemu_bh_schedule(q->tx_bh);
173 }
174 } else {
175 if (q->tx_timer) {
176 qemu_del_timer(q->tx_timer);
177 } else {
178 qemu_bh_cancel(q->tx_bh);
179 }
180 }
181 }
182 }
183
184 static void virtio_net_set_link_status(NetClientState *nc)
185 {
186 VirtIONet *n = qemu_get_nic_opaque(nc);
187 uint16_t old_status = n->status;
188
189 if (nc->link_down)
190 n->status &= ~VIRTIO_NET_S_LINK_UP;
191 else
192 n->status |= VIRTIO_NET_S_LINK_UP;
193
194 if (n->status != old_status)
195 virtio_notify_config(&n->vdev);
196
197 virtio_net_set_status(&n->vdev, n->vdev.status);
198 }
199
200 static void virtio_net_reset(VirtIODevice *vdev)
201 {
202 VirtIONet *n = to_virtio_net(vdev);
203
204 /* Reset back to compatibility mode */
205 n->promisc = 1;
206 n->allmulti = 0;
207 n->alluni = 0;
208 n->nomulti = 0;
209 n->nouni = 0;
210 n->nobcast = 0;
211 /* multiqueue is disabled by default */
212 n->curr_queues = 1;
213
214 /* Flush any MAC and VLAN filter table state */
215 n->mac_table.in_use = 0;
216 n->mac_table.first_multi = 0;
217 n->mac_table.multi_overflow = 0;
218 n->mac_table.uni_overflow = 0;
219 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
220 memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
221 memset(n->vlans, 0, MAX_VLAN >> 3);
222 }
223
224 static void peer_test_vnet_hdr(VirtIONet *n)
225 {
226 NetClientState *nc = qemu_get_queue(n->nic);
227 if (!nc->peer) {
228 return;
229 }
230
231 if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
232 return;
233 }
234
235 n->has_vnet_hdr = tap_has_vnet_hdr(nc->peer);
236 }
237
238 static int peer_has_vnet_hdr(VirtIONet *n)
239 {
240 return n->has_vnet_hdr;
241 }
242
243 static int peer_has_ufo(VirtIONet *n)
244 {
245 if (!peer_has_vnet_hdr(n))
246 return 0;
247
248 n->has_ufo = tap_has_ufo(qemu_get_queue(n->nic)->peer);
249
250 return n->has_ufo;
251 }
252
253 static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs)
254 {
255 int i;
256 NetClientState *nc;
257
258 n->mergeable_rx_bufs = mergeable_rx_bufs;
259
260 n->guest_hdr_len = n->mergeable_rx_bufs ?
261 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
262
263 for (i = 0; i < n->max_queues; i++) {
264 nc = qemu_get_subqueue(n->nic, i);
265
266 if (peer_has_vnet_hdr(n) &&
267 tap_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
268 tap_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
269 n->host_hdr_len = n->guest_hdr_len;
270 }
271 }
272 }
273
274 static int peer_attach(VirtIONet *n, int index)
275 {
276 NetClientState *nc = qemu_get_subqueue(n->nic, index);
277
278 if (!nc->peer) {
279 return 0;
280 }
281
282 if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
283 return 0;
284 }
285
286 return tap_enable(nc->peer);
287 }
288
289 static int peer_detach(VirtIONet *n, int index)
290 {
291 NetClientState *nc = qemu_get_subqueue(n->nic, index);
292
293 if (!nc->peer) {
294 return 0;
295 }
296
297 if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
298 return 0;
299 }
300
301 return tap_disable(nc->peer);
302 }
303
304 static void virtio_net_set_queues(VirtIONet *n)
305 {
306 int i;
307
308 for (i = 0; i < n->max_queues; i++) {
309 if (i < n->curr_queues) {
310 assert(!peer_attach(n, i));
311 } else {
312 assert(!peer_detach(n, i));
313 }
314 }
315 }
316
317 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue, int ctrl);
318
319 static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
320 {
321 VirtIONet *n = to_virtio_net(vdev);
322 NetClientState *nc = qemu_get_queue(n->nic);
323
324 features |= (1 << VIRTIO_NET_F_MAC);
325
326 if (!peer_has_vnet_hdr(n)) {
327 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
328 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
329 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
330 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
331
332 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
333 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
334 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
335 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
336 }
337
338 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
339 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
340 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
341 }
342
343 if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
344 return features;
345 }
346 if (!tap_get_vhost_net(nc->peer)) {
347 return features;
348 }
349 return vhost_net_get_features(tap_get_vhost_net(nc->peer), features);
350 }
351
352 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
353 {
354 uint32_t features = 0;
355
356 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
357 * but also these: */
358 features |= (1 << VIRTIO_NET_F_MAC);
359 features |= (1 << VIRTIO_NET_F_CSUM);
360 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
361 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
362 features |= (1 << VIRTIO_NET_F_HOST_ECN);
363
364 return features;
365 }
366
367 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
368 {
369 VirtIONet *n = to_virtio_net(vdev);
370 int i;
371
372 virtio_net_set_multiqueue(n, !!(features & (1 << VIRTIO_NET_F_MQ)),
373 !!(features & (1 << VIRTIO_NET_F_CTRL_VQ)));
374
375 virtio_net_set_mrg_rx_bufs(n, !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF)));
376
377 if (n->has_vnet_hdr) {
378 tap_set_offload(qemu_get_subqueue(n->nic, 0)->peer,
379 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
380 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
381 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
382 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
383 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
384 }
385
386 for (i = 0; i < n->max_queues; i++) {
387 NetClientState *nc = qemu_get_subqueue(n->nic, i);
388
389 if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
390 continue;
391 }
392 if (!tap_get_vhost_net(nc->peer)) {
393 continue;
394 }
395 vhost_net_ack_features(tap_get_vhost_net(nc->peer), features);
396 }
397 }
398
399 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
400 struct iovec *iov, unsigned int iov_cnt)
401 {
402 uint8_t on;
403 size_t s;
404
405 s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
406 if (s != sizeof(on)) {
407 return VIRTIO_NET_ERR;
408 }
409
410 if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
411 n->promisc = on;
412 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
413 n->allmulti = on;
414 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
415 n->alluni = on;
416 } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
417 n->nomulti = on;
418 } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
419 n->nouni = on;
420 } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
421 n->nobcast = on;
422 } else {
423 return VIRTIO_NET_ERR;
424 }
425
426 return VIRTIO_NET_OK;
427 }
428
429 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
430 struct iovec *iov, unsigned int iov_cnt)
431 {
432 struct virtio_net_ctrl_mac mac_data;
433 size_t s;
434
435 if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
436 if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
437 return VIRTIO_NET_ERR;
438 }
439 s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
440 assert(s == sizeof(n->mac));
441 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
442 return VIRTIO_NET_OK;
443 }
444
445 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
446 return VIRTIO_NET_ERR;
447 }
448
449 n->mac_table.in_use = 0;
450 n->mac_table.first_multi = 0;
451 n->mac_table.uni_overflow = 0;
452 n->mac_table.multi_overflow = 0;
453 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
454
455 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
456 sizeof(mac_data.entries));
457 mac_data.entries = ldl_p(&mac_data.entries);
458 if (s != sizeof(mac_data.entries)) {
459 return VIRTIO_NET_ERR;
460 }
461 iov_discard_front(&iov, &iov_cnt, s);
462
463 if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
464 return VIRTIO_NET_ERR;
465 }
466
467 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
468 s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs,
469 mac_data.entries * ETH_ALEN);
470 if (s != mac_data.entries * ETH_ALEN) {
471 return VIRTIO_NET_ERR;
472 }
473 n->mac_table.in_use += mac_data.entries;
474 } else {
475 n->mac_table.uni_overflow = 1;
476 }
477
478 iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
479
480 n->mac_table.first_multi = n->mac_table.in_use;
481
482 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
483 sizeof(mac_data.entries));
484 mac_data.entries = ldl_p(&mac_data.entries);
485 if (s != sizeof(mac_data.entries)) {
486 return VIRTIO_NET_ERR;
487 }
488
489 iov_discard_front(&iov, &iov_cnt, s);
490
491 if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
492 return VIRTIO_NET_ERR;
493 }
494
495 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
496 s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs,
497 mac_data.entries * ETH_ALEN);
498 if (s != mac_data.entries * ETH_ALEN) {
499 return VIRTIO_NET_ERR;
500 }
501 n->mac_table.in_use += mac_data.entries;
502 } else {
503 n->mac_table.multi_overflow = 1;
504 }
505
506 return VIRTIO_NET_OK;
507 }
508
509 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
510 struct iovec *iov, unsigned int iov_cnt)
511 {
512 uint16_t vid;
513 size_t s;
514
515 s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
516 vid = lduw_p(&vid);
517 if (s != sizeof(vid)) {
518 return VIRTIO_NET_ERR;
519 }
520
521 if (vid >= MAX_VLAN)
522 return VIRTIO_NET_ERR;
523
524 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
525 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
526 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
527 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
528 else
529 return VIRTIO_NET_ERR;
530
531 return VIRTIO_NET_OK;
532 }
533
534 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
535 struct iovec *iov, unsigned int iov_cnt)
536 {
537 struct virtio_net_ctrl_mq mq;
538 size_t s;
539 uint16_t queues;
540
541 s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
542 if (s != sizeof(mq)) {
543 return VIRTIO_NET_ERR;
544 }
545
546 if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
547 return VIRTIO_NET_ERR;
548 }
549
550 queues = lduw_p(&mq.virtqueue_pairs);
551
552 if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
553 queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
554 queues > n->max_queues ||
555 !n->multiqueue) {
556 return VIRTIO_NET_ERR;
557 }
558
559 n->curr_queues = queues;
560 /* stop the backend before changing the number of queues to avoid handling a
561 * disabled queue */
562 virtio_net_set_status(&n->vdev, n->vdev.status);
563 virtio_net_set_queues(n);
564
565 return VIRTIO_NET_OK;
566 }
567 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
568 {
569 VirtIONet *n = to_virtio_net(vdev);
570 struct virtio_net_ctrl_hdr ctrl;
571 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
572 VirtQueueElement elem;
573 size_t s;
574 struct iovec *iov;
575 unsigned int iov_cnt;
576
577 while (virtqueue_pop(vq, &elem)) {
578 if (iov_size(elem.in_sg, elem.in_num) < sizeof(status) ||
579 iov_size(elem.out_sg, elem.out_num) < sizeof(ctrl)) {
580 error_report("virtio-net ctrl missing headers");
581 exit(1);
582 }
583
584 iov = elem.out_sg;
585 iov_cnt = elem.out_num;
586 s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
587 iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
588 if (s != sizeof(ctrl)) {
589 status = VIRTIO_NET_ERR;
590 } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
591 status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
592 } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
593 status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
594 } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
595 status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
596 } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
597 status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
598 }
599
600 s = iov_from_buf(elem.in_sg, elem.in_num, 0, &status, sizeof(status));
601 assert(s == sizeof(status));
602
603 virtqueue_push(vq, &elem, sizeof(status));
604 virtio_notify(vdev, vq);
605 }
606 }
607
608 /* RX */
609
610 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
611 {
612 VirtIONet *n = to_virtio_net(vdev);
613 int queue_index = vq2q(virtio_get_queue_index(vq));
614
615 qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
616 }
617
618 static int virtio_net_can_receive(NetClientState *nc)
619 {
620 VirtIONet *n = qemu_get_nic_opaque(nc);
621 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
622
623 if (!n->vdev.vm_running) {
624 return 0;
625 }
626
627 if (nc->queue_index >= n->curr_queues) {
628 return 0;
629 }
630
631 if (!virtio_queue_ready(q->rx_vq) ||
632 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
633 return 0;
634 }
635
636 return 1;
637 }
638
639 static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
640 {
641 VirtIONet *n = q->n;
642 if (virtio_queue_empty(q->rx_vq) ||
643 (n->mergeable_rx_bufs &&
644 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
645 virtio_queue_set_notification(q->rx_vq, 1);
646
647 /* To avoid a race condition where the guest has made some buffers
648 * available after the above check but before notification was
649 * enabled, check for available buffers again.
650 */
651 if (virtio_queue_empty(q->rx_vq) ||
652 (n->mergeable_rx_bufs &&
653 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
654 return 0;
655 }
656 }
657
658 virtio_queue_set_notification(q->rx_vq, 0);
659 return 1;
660 }
661
662 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
663 * it never finds out that the packets don't have valid checksums. This
664 * causes dhclient to get upset. Fedora's carried a patch for ages to
665 * fix this with Xen but it hasn't appeared in an upstream release of
666 * dhclient yet.
667 *
668 * To avoid breaking existing guests, we catch udp packets and add
669 * checksums. This is terrible but it's better than hacking the guest
670 * kernels.
671 *
672 * N.B. if we introduce a zero-copy API, this operation is no longer free so
673 * we should provide a mechanism to disable it to avoid polluting the host
674 * cache.
675 */
676 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
677 uint8_t *buf, size_t size)
678 {
679 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
680 (size > 27 && size < 1500) && /* normal sized MTU */
681 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
682 (buf[23] == 17) && /* ip.protocol == UDP */
683 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
684 net_checksum_calculate(buf, size);
685 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
686 }
687 }
688
689 static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
690 const void *buf, size_t size)
691 {
692 if (n->has_vnet_hdr) {
693 /* FIXME this cast is evil */
694 void *wbuf = (void *)buf;
695 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
696 size - n->host_hdr_len);
697 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
698 } else {
699 struct virtio_net_hdr hdr = {
700 .flags = 0,
701 .gso_type = VIRTIO_NET_HDR_GSO_NONE
702 };
703 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
704 }
705 }
706
707 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
708 {
709 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
710 static const uint8_t vlan[] = {0x81, 0x00};
711 uint8_t *ptr = (uint8_t *)buf;
712 int i;
713
714 if (n->promisc)
715 return 1;
716
717 ptr += n->host_hdr_len;
718
719 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
720 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
721 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
722 return 0;
723 }
724
725 if (ptr[0] & 1) { // multicast
726 if (!memcmp(ptr, bcast, sizeof(bcast))) {
727 return !n->nobcast;
728 } else if (n->nomulti) {
729 return 0;
730 } else if (n->allmulti || n->mac_table.multi_overflow) {
731 return 1;
732 }
733
734 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
735 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
736 return 1;
737 }
738 }
739 } else { // unicast
740 if (n->nouni) {
741 return 0;
742 } else if (n->alluni || n->mac_table.uni_overflow) {
743 return 1;
744 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
745 return 1;
746 }
747
748 for (i = 0; i < n->mac_table.first_multi; i++) {
749 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
750 return 1;
751 }
752 }
753 }
754
755 return 0;
756 }
757
758 static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
759 {
760 VirtIONet *n = qemu_get_nic_opaque(nc);
761 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
762 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
763 struct virtio_net_hdr_mrg_rxbuf mhdr;
764 unsigned mhdr_cnt = 0;
765 size_t offset, i, guest_offset;
766
767 if (!virtio_net_can_receive(nc)) {
768 return -1;
769 }
770
771 /* hdr_len refers to the header we supply to the guest */
772 if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
773 return 0;
774 }
775
776 if (!receive_filter(n, buf, size))
777 return size;
778
779 offset = i = 0;
780
781 while (offset < size) {
782 VirtQueueElement elem;
783 int len, total;
784 const struct iovec *sg = elem.in_sg;
785
786 total = 0;
787
788 if (virtqueue_pop(q->rx_vq, &elem) == 0) {
789 if (i == 0)
790 return -1;
791 error_report("virtio-net unexpected empty queue: "
792 "i %zd mergeable %d offset %zd, size %zd, "
793 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
794 i, n->mergeable_rx_bufs, offset, size,
795 n->guest_hdr_len, n->host_hdr_len, n->vdev.guest_features);
796 exit(1);
797 }
798
799 if (elem.in_num < 1) {
800 error_report("virtio-net receive queue contains no in buffers");
801 exit(1);
802 }
803
804 if (i == 0) {
805 assert(offset == 0);
806 if (n->mergeable_rx_bufs) {
807 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
808 sg, elem.in_num,
809 offsetof(typeof(mhdr), num_buffers),
810 sizeof(mhdr.num_buffers));
811 }
812
813 receive_header(n, sg, elem.in_num, buf, size);
814 offset = n->host_hdr_len;
815 total += n->guest_hdr_len;
816 guest_offset = n->guest_hdr_len;
817 } else {
818 guest_offset = 0;
819 }
820
821 /* copy in packet. ugh */
822 len = iov_from_buf(sg, elem.in_num, guest_offset,
823 buf + offset, size - offset);
824 total += len;
825 offset += len;
826 /* If buffers can't be merged, at this point we
827 * must have consumed the complete packet.
828 * Otherwise, drop it. */
829 if (!n->mergeable_rx_bufs && offset < size) {
830 #if 0
831 error_report("virtio-net truncated non-mergeable packet: "
832 "i %zd mergeable %d offset %zd, size %zd, "
833 "guest hdr len %zd, host hdr len %zd",
834 i, n->mergeable_rx_bufs,
835 offset, size, n->guest_hdr_len, n->host_hdr_len);
836 #endif
837 return size;
838 }
839
840 /* signal other side */
841 virtqueue_fill(q->rx_vq, &elem, total, i++);
842 }
843
844 if (mhdr_cnt) {
845 stw_p(&mhdr.num_buffers, i);
846 iov_from_buf(mhdr_sg, mhdr_cnt,
847 0,
848 &mhdr.num_buffers, sizeof mhdr.num_buffers);
849 }
850
851 virtqueue_flush(q->rx_vq, i);
852 virtio_notify(&n->vdev, q->rx_vq);
853
854 return size;
855 }
856
857 static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
858
859 static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
860 {
861 VirtIONet *n = qemu_get_nic_opaque(nc);
862 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
863
864 virtqueue_push(q->tx_vq, &q->async_tx.elem, 0);
865 virtio_notify(&n->vdev, q->tx_vq);
866
867 q->async_tx.elem.out_num = q->async_tx.len = 0;
868
869 virtio_queue_set_notification(q->tx_vq, 1);
870 virtio_net_flush_tx(q);
871 }
872
873 /* TX */
874 static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
875 {
876 VirtIONet *n = q->n;
877 VirtQueueElement elem;
878 int32_t num_packets = 0;
879 int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
880 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
881 return num_packets;
882 }
883
884 assert(n->vdev.vm_running);
885
886 if (q->async_tx.elem.out_num) {
887 virtio_queue_set_notification(q->tx_vq, 0);
888 return num_packets;
889 }
890
891 while (virtqueue_pop(q->tx_vq, &elem)) {
892 ssize_t ret, len;
893 unsigned int out_num = elem.out_num;
894 struct iovec *out_sg = &elem.out_sg[0];
895 struct iovec sg[VIRTQUEUE_MAX_SIZE];
896
897 if (out_num < 1) {
898 error_report("virtio-net header not in first element");
899 exit(1);
900 }
901
902 /*
903 * If host wants to see the guest header as is, we can
904 * pass it on unchanged. Otherwise, copy just the parts
905 * that host is interested in.
906 */
907 assert(n->host_hdr_len <= n->guest_hdr_len);
908 if (n->host_hdr_len != n->guest_hdr_len) {
909 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
910 out_sg, out_num,
911 0, n->host_hdr_len);
912 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
913 out_sg, out_num,
914 n->guest_hdr_len, -1);
915 out_num = sg_num;
916 out_sg = sg;
917 }
918
919 len = n->guest_hdr_len;
920
921 ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
922 out_sg, out_num, virtio_net_tx_complete);
923 if (ret == 0) {
924 virtio_queue_set_notification(q->tx_vq, 0);
925 q->async_tx.elem = elem;
926 q->async_tx.len = len;
927 return -EBUSY;
928 }
929
930 len += ret;
931
932 virtqueue_push(q->tx_vq, &elem, 0);
933 virtio_notify(&n->vdev, q->tx_vq);
934
935 if (++num_packets >= n->tx_burst) {
936 break;
937 }
938 }
939 return num_packets;
940 }
941
942 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
943 {
944 VirtIONet *n = to_virtio_net(vdev);
945 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
946
947 /* This happens when device was stopped but VCPU wasn't. */
948 if (!n->vdev.vm_running) {
949 q->tx_waiting = 1;
950 return;
951 }
952
953 if (q->tx_waiting) {
954 virtio_queue_set_notification(vq, 1);
955 qemu_del_timer(q->tx_timer);
956 q->tx_waiting = 0;
957 virtio_net_flush_tx(q);
958 } else {
959 qemu_mod_timer(q->tx_timer,
960 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
961 q->tx_waiting = 1;
962 virtio_queue_set_notification(vq, 0);
963 }
964 }
965
966 static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
967 {
968 VirtIONet *n = to_virtio_net(vdev);
969 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
970
971 if (unlikely(q->tx_waiting)) {
972 return;
973 }
974 q->tx_waiting = 1;
975 /* This happens when device was stopped but VCPU wasn't. */
976 if (!n->vdev.vm_running) {
977 return;
978 }
979 virtio_queue_set_notification(vq, 0);
980 qemu_bh_schedule(q->tx_bh);
981 }
982
983 static void virtio_net_tx_timer(void *opaque)
984 {
985 VirtIONetQueue *q = opaque;
986 VirtIONet *n = q->n;
987 assert(n->vdev.vm_running);
988
989 q->tx_waiting = 0;
990
991 /* Just in case the driver is not ready on more */
992 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
993 return;
994
995 virtio_queue_set_notification(q->tx_vq, 1);
996 virtio_net_flush_tx(q);
997 }
998
999 static void virtio_net_tx_bh(void *opaque)
1000 {
1001 VirtIONetQueue *q = opaque;
1002 VirtIONet *n = q->n;
1003 int32_t ret;
1004
1005 assert(n->vdev.vm_running);
1006
1007 q->tx_waiting = 0;
1008
1009 /* Just in case the driver is not ready on more */
1010 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
1011 return;
1012
1013 ret = virtio_net_flush_tx(q);
1014 if (ret == -EBUSY) {
1015 return; /* Notification re-enable handled by tx_complete */
1016 }
1017
1018 /* If we flush a full burst of packets, assume there are
1019 * more coming and immediately reschedule */
1020 if (ret >= n->tx_burst) {
1021 qemu_bh_schedule(q->tx_bh);
1022 q->tx_waiting = 1;
1023 return;
1024 }
1025
1026 /* If less than a full burst, re-enable notification and flush
1027 * anything that may have come in while we weren't looking. If
1028 * we find something, assume the guest is still active and reschedule */
1029 virtio_queue_set_notification(q->tx_vq, 1);
1030 if (virtio_net_flush_tx(q) > 0) {
1031 virtio_queue_set_notification(q->tx_vq, 0);
1032 qemu_bh_schedule(q->tx_bh);
1033 q->tx_waiting = 1;
1034 }
1035 }
1036
1037 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue, int ctrl)
1038 {
1039 VirtIODevice *vdev = &n->vdev;
1040 int i, max = multiqueue ? n->max_queues : 1;
1041
1042 n->multiqueue = multiqueue;
1043
1044 for (i = 2; i <= n->max_queues * 2 + 1; i++) {
1045 virtio_del_queue(vdev, i);
1046 }
1047
1048 for (i = 1; i < max; i++) {
1049 n->vqs[i].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx);
1050 if (n->vqs[i].tx_timer) {
1051 n->vqs[i].tx_vq =
1052 virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer);
1053 n->vqs[i].tx_timer = qemu_new_timer_ns(vm_clock,
1054 virtio_net_tx_timer,
1055 &n->vqs[i]);
1056 } else {
1057 n->vqs[i].tx_vq =
1058 virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh);
1059 n->vqs[i].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[i]);
1060 }
1061
1062 n->vqs[i].tx_waiting = 0;
1063 n->vqs[i].n = n;
1064 }
1065
1066 if (ctrl) {
1067 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
1068 }
1069
1070 virtio_net_set_queues(n);
1071 }
1072
1073 static void virtio_net_save(QEMUFile *f, void *opaque)
1074 {
1075 int i;
1076 VirtIONet *n = opaque;
1077
1078 /* At this point, backend must be stopped, otherwise
1079 * it might keep writing to memory. */
1080 assert(!n->vhost_started);
1081 virtio_save(&n->vdev, f);
1082
1083 qemu_put_buffer(f, n->mac, ETH_ALEN);
1084 qemu_put_be32(f, n->vqs[0].tx_waiting);
1085 qemu_put_be32(f, n->mergeable_rx_bufs);
1086 qemu_put_be16(f, n->status);
1087 qemu_put_byte(f, n->promisc);
1088 qemu_put_byte(f, n->allmulti);
1089 qemu_put_be32(f, n->mac_table.in_use);
1090 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
1091 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
1092 qemu_put_be32(f, n->has_vnet_hdr);
1093 qemu_put_byte(f, n->mac_table.multi_overflow);
1094 qemu_put_byte(f, n->mac_table.uni_overflow);
1095 qemu_put_byte(f, n->alluni);
1096 qemu_put_byte(f, n->nomulti);
1097 qemu_put_byte(f, n->nouni);
1098 qemu_put_byte(f, n->nobcast);
1099 qemu_put_byte(f, n->has_ufo);
1100 if (n->max_queues > 1) {
1101 qemu_put_be16(f, n->max_queues);
1102 qemu_put_be16(f, n->curr_queues);
1103 for (i = 1; i < n->curr_queues; i++) {
1104 qemu_put_be32(f, n->vqs[i].tx_waiting);
1105 }
1106 }
1107 }
1108
1109 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
1110 {
1111 VirtIONet *n = opaque;
1112 int ret, i, link_down;
1113
1114 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
1115 return -EINVAL;
1116
1117 ret = virtio_load(&n->vdev, f);
1118 if (ret) {
1119 return ret;
1120 }
1121
1122 qemu_get_buffer(f, n->mac, ETH_ALEN);
1123 n->vqs[0].tx_waiting = qemu_get_be32(f);
1124
1125 virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f));
1126
1127 if (version_id >= 3)
1128 n->status = qemu_get_be16(f);
1129
1130 if (version_id >= 4) {
1131 if (version_id < 8) {
1132 n->promisc = qemu_get_be32(f);
1133 n->allmulti = qemu_get_be32(f);
1134 } else {
1135 n->promisc = qemu_get_byte(f);
1136 n->allmulti = qemu_get_byte(f);
1137 }
1138 }
1139
1140 if (version_id >= 5) {
1141 n->mac_table.in_use = qemu_get_be32(f);
1142 /* MAC_TABLE_ENTRIES may be different from the saved image */
1143 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
1144 qemu_get_buffer(f, n->mac_table.macs,
1145 n->mac_table.in_use * ETH_ALEN);
1146 } else if (n->mac_table.in_use) {
1147 uint8_t *buf = g_malloc0(n->mac_table.in_use);
1148 qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN);
1149 g_free(buf);
1150 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
1151 n->mac_table.in_use = 0;
1152 }
1153 }
1154
1155 if (version_id >= 6)
1156 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
1157
1158 if (version_id >= 7) {
1159 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
1160 error_report("virtio-net: saved image requires vnet_hdr=on");
1161 return -1;
1162 }
1163
1164 if (n->has_vnet_hdr) {
1165 tap_set_offload(qemu_get_queue(n->nic)->peer,
1166 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
1167 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
1168 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
1169 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
1170 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
1171 }
1172 }
1173
1174 if (version_id >= 9) {
1175 n->mac_table.multi_overflow = qemu_get_byte(f);
1176 n->mac_table.uni_overflow = qemu_get_byte(f);
1177 }
1178
1179 if (version_id >= 10) {
1180 n->alluni = qemu_get_byte(f);
1181 n->nomulti = qemu_get_byte(f);
1182 n->nouni = qemu_get_byte(f);
1183 n->nobcast = qemu_get_byte(f);
1184 }
1185
1186 if (version_id >= 11) {
1187 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
1188 error_report("virtio-net: saved image requires TUN_F_UFO support");
1189 return -1;
1190 }
1191 }
1192
1193 if (n->max_queues > 1) {
1194 if (n->max_queues != qemu_get_be16(f)) {
1195 error_report("virtio-net: different max_queues ");
1196 return -1;
1197 }
1198
1199 n->curr_queues = qemu_get_be16(f);
1200 for (i = 1; i < n->curr_queues; i++) {
1201 n->vqs[i].tx_waiting = qemu_get_be32(f);
1202 }
1203 }
1204
1205 virtio_net_set_queues(n);
1206
1207 /* Find the first multicast entry in the saved MAC filter */
1208 for (i = 0; i < n->mac_table.in_use; i++) {
1209 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
1210 break;
1211 }
1212 }
1213 n->mac_table.first_multi = i;
1214
1215 /* nc.link_down can't be migrated, so infer link_down according
1216 * to link status bit in n->status */
1217 link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
1218 for (i = 0; i < n->max_queues; i++) {
1219 qemu_get_subqueue(n->nic, i)->link_down = link_down;
1220 }
1221
1222 return 0;
1223 }
1224
1225 static void virtio_net_cleanup(NetClientState *nc)
1226 {
1227 VirtIONet *n = qemu_get_nic_opaque(nc);
1228
1229 n->nic = NULL;
1230 }
1231
1232 static NetClientInfo net_virtio_info = {
1233 .type = NET_CLIENT_OPTIONS_KIND_NIC,
1234 .size = sizeof(NICState),
1235 .can_receive = virtio_net_can_receive,
1236 .receive = virtio_net_receive,
1237 .cleanup = virtio_net_cleanup,
1238 .link_status_changed = virtio_net_set_link_status,
1239 };
1240
1241 static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
1242 {
1243 VirtIONet *n = to_virtio_net(vdev);
1244 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
1245 assert(n->vhost_started);
1246 return vhost_net_virtqueue_pending(tap_get_vhost_net(nc->peer), idx);
1247 }
1248
1249 static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
1250 bool mask)
1251 {
1252 VirtIONet *n = to_virtio_net(vdev);
1253 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
1254 assert(n->vhost_started);
1255 vhost_net_virtqueue_mask(tap_get_vhost_net(nc->peer),
1256 vdev, idx, mask);
1257 }
1258
1259 void virtio_net_set_config_size(VirtIONet *n, uint32_t host_features)
1260 {
1261 int i, config_size = 0;
1262 for (i = 0; feature_sizes[i].flags != 0; i++) {
1263 if (host_features & feature_sizes[i].flags) {
1264 config_size = MAX(feature_sizes[i].end, config_size);
1265 }
1266 }
1267 n->config_size = config_size;
1268 }
1269
1270 static VirtIODevice *virtio_net_common_init(DeviceState *dev, NICConf *conf,
1271 virtio_net_conf *net,
1272 uint32_t host_features,
1273 VirtIONet **pn)
1274 {
1275 VirtIONet *n = *pn;
1276 int i, config_size = 0;
1277
1278 /*
1279 * We have two cases here: the old virtio-net-pci device, and the
1280 * refactored virtio-net.
1281 */
1282 if (n == NULL) {
1283 /* virtio-net-pci */
1284 for (i = 0; feature_sizes[i].flags != 0; i++) {
1285 if (host_features & feature_sizes[i].flags) {
1286 config_size = MAX(feature_sizes[i].end, config_size);
1287 }
1288 }
1289 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
1290 config_size, sizeof(VirtIONet));
1291 n->config_size = config_size;
1292 } else {
1293 /* virtio-net */
1294 virtio_init(VIRTIO_DEVICE(n), "virtio-net", VIRTIO_ID_NET,
1295 n->config_size);
1296 }
1297
1298 n->vdev.get_config = virtio_net_get_config;
1299 n->vdev.set_config = virtio_net_set_config;
1300 n->vdev.get_features = virtio_net_get_features;
1301 n->vdev.set_features = virtio_net_set_features;
1302 n->vdev.bad_features = virtio_net_bad_features;
1303 n->vdev.reset = virtio_net_reset;
1304 n->vdev.set_status = virtio_net_set_status;
1305 n->vdev.guest_notifier_mask = virtio_net_guest_notifier_mask;
1306 n->vdev.guest_notifier_pending = virtio_net_guest_notifier_pending;
1307 n->max_queues = MAX(conf->queues, 1);
1308 n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
1309 n->vqs[0].rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
1310 n->curr_queues = 1;
1311 n->vqs[0].n = n;
1312 n->tx_timeout = net->txtimer;
1313
1314 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
1315 error_report("virtio-net: "
1316 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1317 net->tx);
1318 error_report("Defaulting to \"bh\"");
1319 }
1320
1321 if (net->tx && !strcmp(net->tx, "timer")) {
1322 n->vqs[0].tx_vq = virtio_add_queue(&n->vdev, 256,
1323 virtio_net_handle_tx_timer);
1324 n->vqs[0].tx_timer = qemu_new_timer_ns(vm_clock, virtio_net_tx_timer,
1325 &n->vqs[0]);
1326 } else {
1327 n->vqs[0].tx_vq = virtio_add_queue(&n->vdev, 256,
1328 virtio_net_handle_tx_bh);
1329 n->vqs[0].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[0]);
1330 }
1331 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
1332 qemu_macaddr_default_if_unset(&conf->macaddr);
1333 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
1334 n->status = VIRTIO_NET_S_LINK_UP;
1335
1336 n->nic = qemu_new_nic(&net_virtio_info, conf, object_get_typename(OBJECT(dev)), dev->id, n);
1337 peer_test_vnet_hdr(n);
1338 if (peer_has_vnet_hdr(n)) {
1339 for (i = 0; i < n->max_queues; i++) {
1340 tap_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
1341 }
1342 n->host_hdr_len = sizeof(struct virtio_net_hdr);
1343 } else {
1344 n->host_hdr_len = 0;
1345 }
1346
1347 qemu_format_nic_info_str(qemu_get_queue(n->nic), conf->macaddr.a);
1348
1349 n->vqs[0].tx_waiting = 0;
1350 n->tx_burst = net->txburst;
1351 virtio_net_set_mrg_rx_bufs(n, 0);
1352 n->promisc = 1; /* for compatibility */
1353
1354 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
1355
1356 n->vlans = g_malloc0(MAX_VLAN >> 3);
1357
1358 n->qdev = dev;
1359 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
1360 virtio_net_save, virtio_net_load, n);
1361
1362 add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0");
1363
1364 return &n->vdev;
1365 }
1366
1367 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
1368 virtio_net_conf *net, uint32_t host_features)
1369 {
1370 VirtIONet *n = NULL;
1371 return virtio_net_common_init(dev, conf, net, host_features, &n);
1372 }
1373
1374 void virtio_net_exit(VirtIODevice *vdev)
1375 {
1376 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
1377 int i;
1378
1379 /* This will stop vhost backend if appropriate. */
1380 virtio_net_set_status(vdev, 0);
1381
1382 unregister_savevm(n->qdev, "virtio-net", n);
1383
1384 g_free(n->mac_table.macs);
1385 g_free(n->vlans);
1386
1387 for (i = 0; i < n->max_queues; i++) {
1388 VirtIONetQueue *q = &n->vqs[i];
1389 NetClientState *nc = qemu_get_subqueue(n->nic, i);
1390
1391 qemu_purge_queued_packets(nc);
1392
1393 if (q->tx_timer) {
1394 qemu_del_timer(q->tx_timer);
1395 qemu_free_timer(q->tx_timer);
1396 } else {
1397 qemu_bh_delete(q->tx_bh);
1398 }
1399 }
1400
1401 g_free(n->vqs);
1402 qemu_del_nic(n->nic);
1403 virtio_cleanup(&n->vdev);
1404 }
1405
1406 static int virtio_net_device_init(VirtIODevice *vdev)
1407 {
1408 DeviceState *qdev = DEVICE(vdev);
1409 VirtIONet *n = VIRTIO_NET(vdev);
1410
1411 /*
1412 * Initially, the new VirtIONet device will have a config size =
1413 * sizeof(struct config), because we can't get host_features here.
1414 */
1415 if (virtio_net_common_init(qdev, &(n->nic_conf),
1416 &(n->net_conf), 0, &n) == NULL) {
1417 return -1;
1418 }
1419 return 0;
1420 }
1421
1422 static int virtio_net_device_exit(DeviceState *qdev)
1423 {
1424 VirtIONet *n = VIRTIO_NET(qdev);
1425 VirtIODevice *vdev = VIRTIO_DEVICE(qdev);
1426 int i;
1427
1428 /* This will stop vhost backend if appropriate. */
1429 virtio_net_set_status(vdev, 0);
1430
1431 unregister_savevm(qdev, "virtio-net", n);
1432
1433 g_free(n->mac_table.macs);
1434 g_free(n->vlans);
1435
1436 for (i = 0; i < n->max_queues; i++) {
1437 VirtIONetQueue *q = &n->vqs[i];
1438 NetClientState *nc = qemu_get_subqueue(n->nic, i);
1439
1440 qemu_purge_queued_packets(nc);
1441
1442 if (q->tx_timer) {
1443 qemu_del_timer(q->tx_timer);
1444 qemu_free_timer(q->tx_timer);
1445 } else {
1446 qemu_bh_delete(q->tx_bh);
1447 }
1448 }
1449
1450 g_free(n->vqs);
1451 qemu_del_nic(n->nic);
1452 virtio_common_cleanup(&n->vdev);
1453
1454 return 0;
1455 }
1456
1457 static void virtio_net_instance_init(Object *obj)
1458 {
1459 VirtIONet *n = VIRTIO_NET(obj);
1460
1461 /*
1462 * The default config_size is sizeof(struct virtio_net_config).
1463 * Can be overriden with virtio_net_set_config_size.
1464 */
1465 n->config_size = sizeof(struct virtio_net_config);
1466 }
1467
1468 static Property virtio_net_properties[] = {
1469 DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
1470 DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
1471 TX_TIMER_INTERVAL),
1472 DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
1473 DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
1474 DEFINE_PROP_END_OF_LIST(),
1475 };
1476
1477 static void virtio_net_class_init(ObjectClass *klass, void *data)
1478 {
1479 DeviceClass *dc = DEVICE_CLASS(klass);
1480 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1481 dc->exit = virtio_net_device_exit;
1482 dc->props = virtio_net_properties;
1483 vdc->init = virtio_net_device_init;
1484 vdc->get_config = virtio_net_get_config;
1485 vdc->set_config = virtio_net_set_config;
1486 vdc->get_features = virtio_net_get_features;
1487 vdc->set_features = virtio_net_set_features;
1488 vdc->bad_features = virtio_net_bad_features;
1489 vdc->reset = virtio_net_reset;
1490 vdc->set_status = virtio_net_set_status;
1491 vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
1492 vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
1493 }
1494
1495 static const TypeInfo virtio_net_info = {
1496 .name = TYPE_VIRTIO_NET,
1497 .parent = TYPE_VIRTIO_DEVICE,
1498 .instance_size = sizeof(VirtIONet),
1499 .instance_init = virtio_net_instance_init,
1500 .class_init = virtio_net_class_init,
1501 };
1502
1503 static void virtio_register_types(void)
1504 {
1505 type_register_static(&virtio_net_info);
1506 }
1507
1508 type_init(virtio_register_types)