]> git.proxmox.com Git - mirror_qemu.git/blob - hw/net/virtio-net.c
virtio-net: fix offload ctrl endian
[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/osdep.h"
15 #include "qemu/iov.h"
16 #include "hw/virtio/virtio.h"
17 #include "net/net.h"
18 #include "net/checksum.h"
19 #include "net/tap.h"
20 #include "qemu/error-report.h"
21 #include "qemu/timer.h"
22 #include "hw/virtio/virtio-net.h"
23 #include "net/vhost_net.h"
24 #include "hw/virtio/virtio-bus.h"
25 #include "qapi/qmp/qjson.h"
26 #include "qapi-event.h"
27 #include "hw/virtio/virtio-access.h"
28
29 #define VIRTIO_NET_VM_VERSION 11
30
31 #define MAC_TABLE_ENTRIES 64
32 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
33
34 /* previously fixed value */
35 #define VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE 256
36 /* for now, only allow larger queues; with virtio-1, guest can downsize */
37 #define VIRTIO_NET_RX_QUEUE_MIN_SIZE VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE
38
39 /*
40 * Calculate the number of bytes up to and including the given 'field' of
41 * 'container'.
42 */
43 #define endof(container, field) \
44 (offsetof(container, field) + sizeof(((container *)0)->field))
45
46 typedef struct VirtIOFeature {
47 uint32_t flags;
48 size_t end;
49 } VirtIOFeature;
50
51 static VirtIOFeature feature_sizes[] = {
52 {.flags = 1 << VIRTIO_NET_F_MAC,
53 .end = endof(struct virtio_net_config, mac)},
54 {.flags = 1 << VIRTIO_NET_F_STATUS,
55 .end = endof(struct virtio_net_config, status)},
56 {.flags = 1 << VIRTIO_NET_F_MQ,
57 .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
58 {.flags = 1 << VIRTIO_NET_F_MTU,
59 .end = endof(struct virtio_net_config, mtu)},
60 {}
61 };
62
63 static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
64 {
65 VirtIONet *n = qemu_get_nic_opaque(nc);
66
67 return &n->vqs[nc->queue_index];
68 }
69
70 static int vq2q(int queue_index)
71 {
72 return queue_index / 2;
73 }
74
75 /* TODO
76 * - we could suppress RX interrupt if we were so inclined.
77 */
78
79 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
80 {
81 VirtIONet *n = VIRTIO_NET(vdev);
82 struct virtio_net_config netcfg;
83
84 virtio_stw_p(vdev, &netcfg.status, n->status);
85 virtio_stw_p(vdev, &netcfg.max_virtqueue_pairs, n->max_queues);
86 virtio_stw_p(vdev, &netcfg.mtu, n->net_conf.mtu);
87 memcpy(netcfg.mac, n->mac, ETH_ALEN);
88 memcpy(config, &netcfg, n->config_size);
89 }
90
91 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
92 {
93 VirtIONet *n = VIRTIO_NET(vdev);
94 struct virtio_net_config netcfg = {};
95
96 memcpy(&netcfg, config, n->config_size);
97
98 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_MAC_ADDR) &&
99 !virtio_vdev_has_feature(vdev, VIRTIO_F_VERSION_1) &&
100 memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
101 memcpy(n->mac, netcfg.mac, ETH_ALEN);
102 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
103 }
104 }
105
106 static bool virtio_net_started(VirtIONet *n, uint8_t status)
107 {
108 VirtIODevice *vdev = VIRTIO_DEVICE(n);
109 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
110 (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
111 }
112
113 static void virtio_net_announce_timer(void *opaque)
114 {
115 VirtIONet *n = opaque;
116 VirtIODevice *vdev = VIRTIO_DEVICE(n);
117
118 n->announce_counter--;
119 n->status |= VIRTIO_NET_S_ANNOUNCE;
120 virtio_notify_config(vdev);
121 }
122
123 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
124 {
125 VirtIODevice *vdev = VIRTIO_DEVICE(n);
126 NetClientState *nc = qemu_get_queue(n->nic);
127 int queues = n->multiqueue ? n->max_queues : 1;
128
129 if (!get_vhost_net(nc->peer)) {
130 return;
131 }
132
133 if ((virtio_net_started(n, status) && !nc->peer->link_down) ==
134 !!n->vhost_started) {
135 return;
136 }
137 if (!n->vhost_started) {
138 int r, i;
139
140 if (n->needs_vnet_hdr_swap) {
141 error_report("backend does not support %s vnet headers; "
142 "falling back on userspace virtio",
143 virtio_is_big_endian(vdev) ? "BE" : "LE");
144 return;
145 }
146
147 /* Any packets outstanding? Purge them to avoid touching rings
148 * when vhost is running.
149 */
150 for (i = 0; i < queues; i++) {
151 NetClientState *qnc = qemu_get_subqueue(n->nic, i);
152
153 /* Purge both directions: TX and RX. */
154 qemu_net_queue_purge(qnc->peer->incoming_queue, qnc);
155 qemu_net_queue_purge(qnc->incoming_queue, qnc->peer);
156 }
157
158 if (virtio_has_feature(vdev->guest_features, VIRTIO_NET_F_MTU)) {
159 r = vhost_net_set_mtu(get_vhost_net(nc->peer), n->net_conf.mtu);
160 if (r < 0) {
161 error_report("%uBytes MTU not supported by the backend",
162 n->net_conf.mtu);
163
164 return;
165 }
166 }
167
168 n->vhost_started = 1;
169 r = vhost_net_start(vdev, n->nic->ncs, queues);
170 if (r < 0) {
171 error_report("unable to start vhost net: %d: "
172 "falling back on userspace virtio", -r);
173 n->vhost_started = 0;
174 }
175 } else {
176 vhost_net_stop(vdev, n->nic->ncs, queues);
177 n->vhost_started = 0;
178 }
179 }
180
181 static int virtio_net_set_vnet_endian_one(VirtIODevice *vdev,
182 NetClientState *peer,
183 bool enable)
184 {
185 if (virtio_is_big_endian(vdev)) {
186 return qemu_set_vnet_be(peer, enable);
187 } else {
188 return qemu_set_vnet_le(peer, enable);
189 }
190 }
191
192 static bool virtio_net_set_vnet_endian(VirtIODevice *vdev, NetClientState *ncs,
193 int queues, bool enable)
194 {
195 int i;
196
197 for (i = 0; i < queues; i++) {
198 if (virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, enable) < 0 &&
199 enable) {
200 while (--i >= 0) {
201 virtio_net_set_vnet_endian_one(vdev, ncs[i].peer, false);
202 }
203
204 return true;
205 }
206 }
207
208 return false;
209 }
210
211 static void virtio_net_vnet_endian_status(VirtIONet *n, uint8_t status)
212 {
213 VirtIODevice *vdev = VIRTIO_DEVICE(n);
214 int queues = n->multiqueue ? n->max_queues : 1;
215
216 if (virtio_net_started(n, status)) {
217 /* Before using the device, we tell the network backend about the
218 * endianness to use when parsing vnet headers. If the backend
219 * can't do it, we fallback onto fixing the headers in the core
220 * virtio-net code.
221 */
222 n->needs_vnet_hdr_swap = virtio_net_set_vnet_endian(vdev, n->nic->ncs,
223 queues, true);
224 } else if (virtio_net_started(n, vdev->status)) {
225 /* After using the device, we need to reset the network backend to
226 * the default (guest native endianness), otherwise the guest may
227 * lose network connectivity if it is rebooted into a different
228 * endianness.
229 */
230 virtio_net_set_vnet_endian(vdev, n->nic->ncs, queues, false);
231 }
232 }
233
234 static void virtio_net_drop_tx_queue_data(VirtIODevice *vdev, VirtQueue *vq)
235 {
236 unsigned int dropped = virtqueue_drop_all(vq);
237 if (dropped) {
238 virtio_notify(vdev, vq);
239 }
240 }
241
242 static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
243 {
244 VirtIONet *n = VIRTIO_NET(vdev);
245 VirtIONetQueue *q;
246 int i;
247 uint8_t queue_status;
248
249 virtio_net_vnet_endian_status(n, status);
250 virtio_net_vhost_status(n, status);
251
252 for (i = 0; i < n->max_queues; i++) {
253 NetClientState *ncs = qemu_get_subqueue(n->nic, i);
254 bool queue_started;
255 q = &n->vqs[i];
256
257 if ((!n->multiqueue && i != 0) || i >= n->curr_queues) {
258 queue_status = 0;
259 } else {
260 queue_status = status;
261 }
262 queue_started =
263 virtio_net_started(n, queue_status) && !n->vhost_started;
264
265 if (queue_started) {
266 qemu_flush_queued_packets(ncs);
267 }
268
269 if (!q->tx_waiting) {
270 continue;
271 }
272
273 if (queue_started) {
274 if (q->tx_timer) {
275 timer_mod(q->tx_timer,
276 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
277 } else {
278 qemu_bh_schedule(q->tx_bh);
279 }
280 } else {
281 if (q->tx_timer) {
282 timer_del(q->tx_timer);
283 } else {
284 qemu_bh_cancel(q->tx_bh);
285 }
286 if ((n->status & VIRTIO_NET_S_LINK_UP) == 0 &&
287 (queue_status & VIRTIO_CONFIG_S_DRIVER_OK)) {
288 /* if tx is waiting we are likely have some packets in tx queue
289 * and disabled notification */
290 q->tx_waiting = 0;
291 virtio_queue_set_notification(q->tx_vq, 1);
292 virtio_net_drop_tx_queue_data(vdev, q->tx_vq);
293 }
294 }
295 }
296 }
297
298 static void virtio_net_set_link_status(NetClientState *nc)
299 {
300 VirtIONet *n = qemu_get_nic_opaque(nc);
301 VirtIODevice *vdev = VIRTIO_DEVICE(n);
302 uint16_t old_status = n->status;
303
304 if (nc->link_down)
305 n->status &= ~VIRTIO_NET_S_LINK_UP;
306 else
307 n->status |= VIRTIO_NET_S_LINK_UP;
308
309 if (n->status != old_status)
310 virtio_notify_config(vdev);
311
312 virtio_net_set_status(vdev, vdev->status);
313 }
314
315 static void rxfilter_notify(NetClientState *nc)
316 {
317 VirtIONet *n = qemu_get_nic_opaque(nc);
318
319 if (nc->rxfilter_notify_enabled) {
320 gchar *path = object_get_canonical_path(OBJECT(n->qdev));
321 qapi_event_send_nic_rx_filter_changed(!!n->netclient_name,
322 n->netclient_name, path, &error_abort);
323 g_free(path);
324
325 /* disable event notification to avoid events flooding */
326 nc->rxfilter_notify_enabled = 0;
327 }
328 }
329
330 static intList *get_vlan_table(VirtIONet *n)
331 {
332 intList *list, *entry;
333 int i, j;
334
335 list = NULL;
336 for (i = 0; i < MAX_VLAN >> 5; i++) {
337 for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
338 if (n->vlans[i] & (1U << j)) {
339 entry = g_malloc0(sizeof(*entry));
340 entry->value = (i << 5) + j;
341 entry->next = list;
342 list = entry;
343 }
344 }
345 }
346
347 return list;
348 }
349
350 static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
351 {
352 VirtIONet *n = qemu_get_nic_opaque(nc);
353 VirtIODevice *vdev = VIRTIO_DEVICE(n);
354 RxFilterInfo *info;
355 strList *str_list, *entry;
356 int i;
357
358 info = g_malloc0(sizeof(*info));
359 info->name = g_strdup(nc->name);
360 info->promiscuous = n->promisc;
361
362 if (n->nouni) {
363 info->unicast = RX_STATE_NONE;
364 } else if (n->alluni) {
365 info->unicast = RX_STATE_ALL;
366 } else {
367 info->unicast = RX_STATE_NORMAL;
368 }
369
370 if (n->nomulti) {
371 info->multicast = RX_STATE_NONE;
372 } else if (n->allmulti) {
373 info->multicast = RX_STATE_ALL;
374 } else {
375 info->multicast = RX_STATE_NORMAL;
376 }
377
378 info->broadcast_allowed = n->nobcast;
379 info->multicast_overflow = n->mac_table.multi_overflow;
380 info->unicast_overflow = n->mac_table.uni_overflow;
381
382 info->main_mac = qemu_mac_strdup_printf(n->mac);
383
384 str_list = NULL;
385 for (i = 0; i < n->mac_table.first_multi; i++) {
386 entry = g_malloc0(sizeof(*entry));
387 entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
388 entry->next = str_list;
389 str_list = entry;
390 }
391 info->unicast_table = str_list;
392
393 str_list = NULL;
394 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
395 entry = g_malloc0(sizeof(*entry));
396 entry->value = qemu_mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
397 entry->next = str_list;
398 str_list = entry;
399 }
400 info->multicast_table = str_list;
401 info->vlan_table = get_vlan_table(n);
402
403 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VLAN)) {
404 info->vlan = RX_STATE_ALL;
405 } else if (!info->vlan_table) {
406 info->vlan = RX_STATE_NONE;
407 } else {
408 info->vlan = RX_STATE_NORMAL;
409 }
410
411 /* enable event notification after query */
412 nc->rxfilter_notify_enabled = 1;
413
414 return info;
415 }
416
417 static void virtio_net_reset(VirtIODevice *vdev)
418 {
419 VirtIONet *n = VIRTIO_NET(vdev);
420
421 /* Reset back to compatibility mode */
422 n->promisc = 1;
423 n->allmulti = 0;
424 n->alluni = 0;
425 n->nomulti = 0;
426 n->nouni = 0;
427 n->nobcast = 0;
428 /* multiqueue is disabled by default */
429 n->curr_queues = 1;
430 timer_del(n->announce_timer);
431 n->announce_counter = 0;
432 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
433
434 /* Flush any MAC and VLAN filter table state */
435 n->mac_table.in_use = 0;
436 n->mac_table.first_multi = 0;
437 n->mac_table.multi_overflow = 0;
438 n->mac_table.uni_overflow = 0;
439 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
440 memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
441 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
442 memset(n->vlans, 0, MAX_VLAN >> 3);
443 }
444
445 static void peer_test_vnet_hdr(VirtIONet *n)
446 {
447 NetClientState *nc = qemu_get_queue(n->nic);
448 if (!nc->peer) {
449 return;
450 }
451
452 n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
453 }
454
455 static int peer_has_vnet_hdr(VirtIONet *n)
456 {
457 return n->has_vnet_hdr;
458 }
459
460 static int peer_has_ufo(VirtIONet *n)
461 {
462 if (!peer_has_vnet_hdr(n))
463 return 0;
464
465 n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
466
467 return n->has_ufo;
468 }
469
470 static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs,
471 int version_1)
472 {
473 int i;
474 NetClientState *nc;
475
476 n->mergeable_rx_bufs = mergeable_rx_bufs;
477
478 if (version_1) {
479 n->guest_hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
480 } else {
481 n->guest_hdr_len = n->mergeable_rx_bufs ?
482 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
483 sizeof(struct virtio_net_hdr);
484 }
485
486 for (i = 0; i < n->max_queues; i++) {
487 nc = qemu_get_subqueue(n->nic, i);
488
489 if (peer_has_vnet_hdr(n) &&
490 qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
491 qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
492 n->host_hdr_len = n->guest_hdr_len;
493 }
494 }
495 }
496
497 static int peer_attach(VirtIONet *n, int index)
498 {
499 NetClientState *nc = qemu_get_subqueue(n->nic, index);
500
501 if (!nc->peer) {
502 return 0;
503 }
504
505 if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
506 vhost_set_vring_enable(nc->peer, 1);
507 }
508
509 if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
510 return 0;
511 }
512
513 if (n->max_queues == 1) {
514 return 0;
515 }
516
517 return tap_enable(nc->peer);
518 }
519
520 static int peer_detach(VirtIONet *n, int index)
521 {
522 NetClientState *nc = qemu_get_subqueue(n->nic, index);
523
524 if (!nc->peer) {
525 return 0;
526 }
527
528 if (nc->peer->info->type == NET_CLIENT_DRIVER_VHOST_USER) {
529 vhost_set_vring_enable(nc->peer, 0);
530 }
531
532 if (nc->peer->info->type != NET_CLIENT_DRIVER_TAP) {
533 return 0;
534 }
535
536 return tap_disable(nc->peer);
537 }
538
539 static void virtio_net_set_queues(VirtIONet *n)
540 {
541 int i;
542 int r;
543
544 if (n->nic->peer_deleted) {
545 return;
546 }
547
548 for (i = 0; i < n->max_queues; i++) {
549 if (i < n->curr_queues) {
550 r = peer_attach(n, i);
551 assert(!r);
552 } else {
553 r = peer_detach(n, i);
554 assert(!r);
555 }
556 }
557 }
558
559 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
560
561 static uint64_t virtio_net_get_features(VirtIODevice *vdev, uint64_t features,
562 Error **errp)
563 {
564 VirtIONet *n = VIRTIO_NET(vdev);
565 NetClientState *nc = qemu_get_queue(n->nic);
566
567 /* Firstly sync all virtio-net possible supported features */
568 features |= n->host_features;
569
570 virtio_add_feature(&features, VIRTIO_NET_F_MAC);
571
572 if (!peer_has_vnet_hdr(n)) {
573 virtio_clear_feature(&features, VIRTIO_NET_F_CSUM);
574 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO4);
575 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_TSO6);
576 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_ECN);
577
578 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_CSUM);
579 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO4);
580 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_TSO6);
581 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_ECN);
582 }
583
584 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
585 virtio_clear_feature(&features, VIRTIO_NET_F_GUEST_UFO);
586 virtio_clear_feature(&features, VIRTIO_NET_F_HOST_UFO);
587 }
588
589 if (!get_vhost_net(nc->peer)) {
590 return features;
591 }
592 return vhost_net_get_features(get_vhost_net(nc->peer), features);
593 }
594
595 static uint64_t virtio_net_bad_features(VirtIODevice *vdev)
596 {
597 uint64_t features = 0;
598
599 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
600 * but also these: */
601 virtio_add_feature(&features, VIRTIO_NET_F_MAC);
602 virtio_add_feature(&features, VIRTIO_NET_F_CSUM);
603 virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO4);
604 virtio_add_feature(&features, VIRTIO_NET_F_HOST_TSO6);
605 virtio_add_feature(&features, VIRTIO_NET_F_HOST_ECN);
606
607 return features;
608 }
609
610 static void virtio_net_apply_guest_offloads(VirtIONet *n)
611 {
612 qemu_set_offload(qemu_get_queue(n->nic)->peer,
613 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
614 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
615 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
616 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
617 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)));
618 }
619
620 static uint64_t virtio_net_guest_offloads_by_features(uint32_t features)
621 {
622 static const uint64_t guest_offloads_mask =
623 (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
624 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
625 (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
626 (1ULL << VIRTIO_NET_F_GUEST_ECN) |
627 (1ULL << VIRTIO_NET_F_GUEST_UFO);
628
629 return guest_offloads_mask & features;
630 }
631
632 static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n)
633 {
634 VirtIODevice *vdev = VIRTIO_DEVICE(n);
635 return virtio_net_guest_offloads_by_features(vdev->guest_features);
636 }
637
638 static void virtio_net_set_features(VirtIODevice *vdev, uint64_t features)
639 {
640 VirtIONet *n = VIRTIO_NET(vdev);
641 int i;
642
643 virtio_net_set_multiqueue(n,
644 virtio_has_feature(features, VIRTIO_NET_F_MQ));
645
646 virtio_net_set_mrg_rx_bufs(n,
647 virtio_has_feature(features,
648 VIRTIO_NET_F_MRG_RXBUF),
649 virtio_has_feature(features,
650 VIRTIO_F_VERSION_1));
651
652 if (n->has_vnet_hdr) {
653 n->curr_guest_offloads =
654 virtio_net_guest_offloads_by_features(features);
655 virtio_net_apply_guest_offloads(n);
656 }
657
658 for (i = 0; i < n->max_queues; i++) {
659 NetClientState *nc = qemu_get_subqueue(n->nic, i);
660
661 if (!get_vhost_net(nc->peer)) {
662 continue;
663 }
664 vhost_net_ack_features(get_vhost_net(nc->peer), features);
665 }
666
667 if (virtio_has_feature(features, VIRTIO_NET_F_CTRL_VLAN)) {
668 memset(n->vlans, 0, MAX_VLAN >> 3);
669 } else {
670 memset(n->vlans, 0xff, MAX_VLAN >> 3);
671 }
672 }
673
674 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
675 struct iovec *iov, unsigned int iov_cnt)
676 {
677 uint8_t on;
678 size_t s;
679 NetClientState *nc = qemu_get_queue(n->nic);
680
681 s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
682 if (s != sizeof(on)) {
683 return VIRTIO_NET_ERR;
684 }
685
686 if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
687 n->promisc = on;
688 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
689 n->allmulti = on;
690 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
691 n->alluni = on;
692 } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
693 n->nomulti = on;
694 } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
695 n->nouni = on;
696 } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
697 n->nobcast = on;
698 } else {
699 return VIRTIO_NET_ERR;
700 }
701
702 rxfilter_notify(nc);
703
704 return VIRTIO_NET_OK;
705 }
706
707 static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
708 struct iovec *iov, unsigned int iov_cnt)
709 {
710 VirtIODevice *vdev = VIRTIO_DEVICE(n);
711 uint64_t offloads;
712 size_t s;
713
714 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
715 return VIRTIO_NET_ERR;
716 }
717
718 s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
719 if (s != sizeof(offloads)) {
720 return VIRTIO_NET_ERR;
721 }
722
723 if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
724 uint64_t supported_offloads;
725
726 offloads = virtio_ldq_p(vdev, &offloads);
727
728 if (!n->has_vnet_hdr) {
729 return VIRTIO_NET_ERR;
730 }
731
732 supported_offloads = virtio_net_supported_guest_offloads(n);
733 if (offloads & ~supported_offloads) {
734 return VIRTIO_NET_ERR;
735 }
736
737 n->curr_guest_offloads = offloads;
738 virtio_net_apply_guest_offloads(n);
739
740 return VIRTIO_NET_OK;
741 } else {
742 return VIRTIO_NET_ERR;
743 }
744 }
745
746 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
747 struct iovec *iov, unsigned int iov_cnt)
748 {
749 VirtIODevice *vdev = VIRTIO_DEVICE(n);
750 struct virtio_net_ctrl_mac mac_data;
751 size_t s;
752 NetClientState *nc = qemu_get_queue(n->nic);
753
754 if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
755 if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
756 return VIRTIO_NET_ERR;
757 }
758 s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
759 assert(s == sizeof(n->mac));
760 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
761 rxfilter_notify(nc);
762
763 return VIRTIO_NET_OK;
764 }
765
766 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
767 return VIRTIO_NET_ERR;
768 }
769
770 int in_use = 0;
771 int first_multi = 0;
772 uint8_t uni_overflow = 0;
773 uint8_t multi_overflow = 0;
774 uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
775
776 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
777 sizeof(mac_data.entries));
778 mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
779 if (s != sizeof(mac_data.entries)) {
780 goto error;
781 }
782 iov_discard_front(&iov, &iov_cnt, s);
783
784 if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
785 goto error;
786 }
787
788 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
789 s = iov_to_buf(iov, iov_cnt, 0, macs,
790 mac_data.entries * ETH_ALEN);
791 if (s != mac_data.entries * ETH_ALEN) {
792 goto error;
793 }
794 in_use += mac_data.entries;
795 } else {
796 uni_overflow = 1;
797 }
798
799 iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
800
801 first_multi = in_use;
802
803 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
804 sizeof(mac_data.entries));
805 mac_data.entries = virtio_ldl_p(vdev, &mac_data.entries);
806 if (s != sizeof(mac_data.entries)) {
807 goto error;
808 }
809
810 iov_discard_front(&iov, &iov_cnt, s);
811
812 if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
813 goto error;
814 }
815
816 if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
817 s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
818 mac_data.entries * ETH_ALEN);
819 if (s != mac_data.entries * ETH_ALEN) {
820 goto error;
821 }
822 in_use += mac_data.entries;
823 } else {
824 multi_overflow = 1;
825 }
826
827 n->mac_table.in_use = in_use;
828 n->mac_table.first_multi = first_multi;
829 n->mac_table.uni_overflow = uni_overflow;
830 n->mac_table.multi_overflow = multi_overflow;
831 memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
832 g_free(macs);
833 rxfilter_notify(nc);
834
835 return VIRTIO_NET_OK;
836
837 error:
838 g_free(macs);
839 return VIRTIO_NET_ERR;
840 }
841
842 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
843 struct iovec *iov, unsigned int iov_cnt)
844 {
845 VirtIODevice *vdev = VIRTIO_DEVICE(n);
846 uint16_t vid;
847 size_t s;
848 NetClientState *nc = qemu_get_queue(n->nic);
849
850 s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
851 vid = virtio_lduw_p(vdev, &vid);
852 if (s != sizeof(vid)) {
853 return VIRTIO_NET_ERR;
854 }
855
856 if (vid >= MAX_VLAN)
857 return VIRTIO_NET_ERR;
858
859 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
860 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
861 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
862 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
863 else
864 return VIRTIO_NET_ERR;
865
866 rxfilter_notify(nc);
867
868 return VIRTIO_NET_OK;
869 }
870
871 static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
872 struct iovec *iov, unsigned int iov_cnt)
873 {
874 if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
875 n->status & VIRTIO_NET_S_ANNOUNCE) {
876 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
877 if (n->announce_counter) {
878 timer_mod(n->announce_timer,
879 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
880 self_announce_delay(n->announce_counter));
881 }
882 return VIRTIO_NET_OK;
883 } else {
884 return VIRTIO_NET_ERR;
885 }
886 }
887
888 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
889 struct iovec *iov, unsigned int iov_cnt)
890 {
891 VirtIODevice *vdev = VIRTIO_DEVICE(n);
892 struct virtio_net_ctrl_mq mq;
893 size_t s;
894 uint16_t queues;
895
896 s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
897 if (s != sizeof(mq)) {
898 return VIRTIO_NET_ERR;
899 }
900
901 if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
902 return VIRTIO_NET_ERR;
903 }
904
905 queues = virtio_lduw_p(vdev, &mq.virtqueue_pairs);
906
907 if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
908 queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
909 queues > n->max_queues ||
910 !n->multiqueue) {
911 return VIRTIO_NET_ERR;
912 }
913
914 n->curr_queues = queues;
915 /* stop the backend before changing the number of queues to avoid handling a
916 * disabled queue */
917 virtio_net_set_status(vdev, vdev->status);
918 virtio_net_set_queues(n);
919
920 return VIRTIO_NET_OK;
921 }
922
923 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
924 {
925 VirtIONet *n = VIRTIO_NET(vdev);
926 struct virtio_net_ctrl_hdr ctrl;
927 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
928 VirtQueueElement *elem;
929 size_t s;
930 struct iovec *iov, *iov2;
931 unsigned int iov_cnt;
932
933 for (;;) {
934 elem = virtqueue_pop(vq, sizeof(VirtQueueElement));
935 if (!elem) {
936 break;
937 }
938 if (iov_size(elem->in_sg, elem->in_num) < sizeof(status) ||
939 iov_size(elem->out_sg, elem->out_num) < sizeof(ctrl)) {
940 virtio_error(vdev, "virtio-net ctrl missing headers");
941 virtqueue_detach_element(vq, elem, 0);
942 g_free(elem);
943 break;
944 }
945
946 iov_cnt = elem->out_num;
947 iov2 = iov = g_memdup(elem->out_sg, sizeof(struct iovec) * elem->out_num);
948 s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
949 iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
950 if (s != sizeof(ctrl)) {
951 status = VIRTIO_NET_ERR;
952 } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
953 status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
954 } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
955 status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
956 } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
957 status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
958 } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
959 status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
960 } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
961 status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
962 } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
963 status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
964 }
965
966 s = iov_from_buf(elem->in_sg, elem->in_num, 0, &status, sizeof(status));
967 assert(s == sizeof(status));
968
969 virtqueue_push(vq, elem, sizeof(status));
970 virtio_notify(vdev, vq);
971 g_free(iov2);
972 g_free(elem);
973 }
974 }
975
976 /* RX */
977
978 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
979 {
980 VirtIONet *n = VIRTIO_NET(vdev);
981 int queue_index = vq2q(virtio_get_queue_index(vq));
982
983 qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
984 }
985
986 static int virtio_net_can_receive(NetClientState *nc)
987 {
988 VirtIONet *n = qemu_get_nic_opaque(nc);
989 VirtIODevice *vdev = VIRTIO_DEVICE(n);
990 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
991
992 if (!vdev->vm_running) {
993 return 0;
994 }
995
996 if (nc->queue_index >= n->curr_queues) {
997 return 0;
998 }
999
1000 if (!virtio_queue_ready(q->rx_vq) ||
1001 !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1002 return 0;
1003 }
1004
1005 return 1;
1006 }
1007
1008 static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
1009 {
1010 VirtIONet *n = q->n;
1011 if (virtio_queue_empty(q->rx_vq) ||
1012 (n->mergeable_rx_bufs &&
1013 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
1014 virtio_queue_set_notification(q->rx_vq, 1);
1015
1016 /* To avoid a race condition where the guest has made some buffers
1017 * available after the above check but before notification was
1018 * enabled, check for available buffers again.
1019 */
1020 if (virtio_queue_empty(q->rx_vq) ||
1021 (n->mergeable_rx_bufs &&
1022 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
1023 return 0;
1024 }
1025 }
1026
1027 virtio_queue_set_notification(q->rx_vq, 0);
1028 return 1;
1029 }
1030
1031 static void virtio_net_hdr_swap(VirtIODevice *vdev, struct virtio_net_hdr *hdr)
1032 {
1033 virtio_tswap16s(vdev, &hdr->hdr_len);
1034 virtio_tswap16s(vdev, &hdr->gso_size);
1035 virtio_tswap16s(vdev, &hdr->csum_start);
1036 virtio_tswap16s(vdev, &hdr->csum_offset);
1037 }
1038
1039 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
1040 * it never finds out that the packets don't have valid checksums. This
1041 * causes dhclient to get upset. Fedora's carried a patch for ages to
1042 * fix this with Xen but it hasn't appeared in an upstream release of
1043 * dhclient yet.
1044 *
1045 * To avoid breaking existing guests, we catch udp packets and add
1046 * checksums. This is terrible but it's better than hacking the guest
1047 * kernels.
1048 *
1049 * N.B. if we introduce a zero-copy API, this operation is no longer free so
1050 * we should provide a mechanism to disable it to avoid polluting the host
1051 * cache.
1052 */
1053 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
1054 uint8_t *buf, size_t size)
1055 {
1056 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
1057 (size > 27 && size < 1500) && /* normal sized MTU */
1058 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
1059 (buf[23] == 17) && /* ip.protocol == UDP */
1060 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
1061 net_checksum_calculate(buf, size);
1062 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
1063 }
1064 }
1065
1066 static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
1067 const void *buf, size_t size)
1068 {
1069 if (n->has_vnet_hdr) {
1070 /* FIXME this cast is evil */
1071 void *wbuf = (void *)buf;
1072 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
1073 size - n->host_hdr_len);
1074
1075 if (n->needs_vnet_hdr_swap) {
1076 virtio_net_hdr_swap(VIRTIO_DEVICE(n), wbuf);
1077 }
1078 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
1079 } else {
1080 struct virtio_net_hdr hdr = {
1081 .flags = 0,
1082 .gso_type = VIRTIO_NET_HDR_GSO_NONE
1083 };
1084 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
1085 }
1086 }
1087
1088 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
1089 {
1090 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
1091 static const uint8_t vlan[] = {0x81, 0x00};
1092 uint8_t *ptr = (uint8_t *)buf;
1093 int i;
1094
1095 if (n->promisc)
1096 return 1;
1097
1098 ptr += n->host_hdr_len;
1099
1100 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
1101 int vid = lduw_be_p(ptr + 14) & 0xfff;
1102 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
1103 return 0;
1104 }
1105
1106 if (ptr[0] & 1) { // multicast
1107 if (!memcmp(ptr, bcast, sizeof(bcast))) {
1108 return !n->nobcast;
1109 } else if (n->nomulti) {
1110 return 0;
1111 } else if (n->allmulti || n->mac_table.multi_overflow) {
1112 return 1;
1113 }
1114
1115 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
1116 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1117 return 1;
1118 }
1119 }
1120 } else { // unicast
1121 if (n->nouni) {
1122 return 0;
1123 } else if (n->alluni || n->mac_table.uni_overflow) {
1124 return 1;
1125 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
1126 return 1;
1127 }
1128
1129 for (i = 0; i < n->mac_table.first_multi; i++) {
1130 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
1131 return 1;
1132 }
1133 }
1134 }
1135
1136 return 0;
1137 }
1138
1139 static ssize_t virtio_net_receive_rcu(NetClientState *nc, const uint8_t *buf,
1140 size_t size)
1141 {
1142 VirtIONet *n = qemu_get_nic_opaque(nc);
1143 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
1144 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1145 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
1146 struct virtio_net_hdr_mrg_rxbuf mhdr;
1147 unsigned mhdr_cnt = 0;
1148 size_t offset, i, guest_offset;
1149
1150 if (!virtio_net_can_receive(nc)) {
1151 return -1;
1152 }
1153
1154 /* hdr_len refers to the header we supply to the guest */
1155 if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
1156 return 0;
1157 }
1158
1159 if (!receive_filter(n, buf, size))
1160 return size;
1161
1162 offset = i = 0;
1163
1164 while (offset < size) {
1165 VirtQueueElement *elem;
1166 int len, total;
1167 const struct iovec *sg;
1168
1169 total = 0;
1170
1171 elem = virtqueue_pop(q->rx_vq, sizeof(VirtQueueElement));
1172 if (!elem) {
1173 if (i) {
1174 virtio_error(vdev, "virtio-net unexpected empty queue: "
1175 "i %zd mergeable %d offset %zd, size %zd, "
1176 "guest hdr len %zd, host hdr len %zd "
1177 "guest features 0x%" PRIx64,
1178 i, n->mergeable_rx_bufs, offset, size,
1179 n->guest_hdr_len, n->host_hdr_len,
1180 vdev->guest_features);
1181 }
1182 return -1;
1183 }
1184
1185 if (elem->in_num < 1) {
1186 virtio_error(vdev,
1187 "virtio-net receive queue contains no in buffers");
1188 virtqueue_detach_element(q->rx_vq, elem, 0);
1189 g_free(elem);
1190 return -1;
1191 }
1192
1193 sg = elem->in_sg;
1194 if (i == 0) {
1195 assert(offset == 0);
1196 if (n->mergeable_rx_bufs) {
1197 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
1198 sg, elem->in_num,
1199 offsetof(typeof(mhdr), num_buffers),
1200 sizeof(mhdr.num_buffers));
1201 }
1202
1203 receive_header(n, sg, elem->in_num, buf, size);
1204 offset = n->host_hdr_len;
1205 total += n->guest_hdr_len;
1206 guest_offset = n->guest_hdr_len;
1207 } else {
1208 guest_offset = 0;
1209 }
1210
1211 /* copy in packet. ugh */
1212 len = iov_from_buf(sg, elem->in_num, guest_offset,
1213 buf + offset, size - offset);
1214 total += len;
1215 offset += len;
1216 /* If buffers can't be merged, at this point we
1217 * must have consumed the complete packet.
1218 * Otherwise, drop it. */
1219 if (!n->mergeable_rx_bufs && offset < size) {
1220 virtqueue_unpop(q->rx_vq, elem, total);
1221 g_free(elem);
1222 return size;
1223 }
1224
1225 /* signal other side */
1226 virtqueue_fill(q->rx_vq, elem, total, i++);
1227 g_free(elem);
1228 }
1229
1230 if (mhdr_cnt) {
1231 virtio_stw_p(vdev, &mhdr.num_buffers, i);
1232 iov_from_buf(mhdr_sg, mhdr_cnt,
1233 0,
1234 &mhdr.num_buffers, sizeof mhdr.num_buffers);
1235 }
1236
1237 virtqueue_flush(q->rx_vq, i);
1238 virtio_notify(vdev, q->rx_vq);
1239
1240 return size;
1241 }
1242
1243 static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf,
1244 size_t size)
1245 {
1246 ssize_t r;
1247
1248 rcu_read_lock();
1249 r = virtio_net_receive_rcu(nc, buf, size);
1250 rcu_read_unlock();
1251 return r;
1252 }
1253
1254 static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
1255
1256 static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
1257 {
1258 VirtIONet *n = qemu_get_nic_opaque(nc);
1259 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
1260 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1261
1262 virtqueue_push(q->tx_vq, q->async_tx.elem, 0);
1263 virtio_notify(vdev, q->tx_vq);
1264
1265 g_free(q->async_tx.elem);
1266 q->async_tx.elem = NULL;
1267
1268 virtio_queue_set_notification(q->tx_vq, 1);
1269 virtio_net_flush_tx(q);
1270 }
1271
1272 /* TX */
1273 static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
1274 {
1275 VirtIONet *n = q->n;
1276 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1277 VirtQueueElement *elem;
1278 int32_t num_packets = 0;
1279 int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
1280 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1281 return num_packets;
1282 }
1283
1284 if (q->async_tx.elem) {
1285 virtio_queue_set_notification(q->tx_vq, 0);
1286 return num_packets;
1287 }
1288
1289 for (;;) {
1290 ssize_t ret;
1291 unsigned int out_num;
1292 struct iovec sg[VIRTQUEUE_MAX_SIZE], sg2[VIRTQUEUE_MAX_SIZE + 1], *out_sg;
1293 struct virtio_net_hdr_mrg_rxbuf mhdr;
1294
1295 elem = virtqueue_pop(q->tx_vq, sizeof(VirtQueueElement));
1296 if (!elem) {
1297 break;
1298 }
1299
1300 out_num = elem->out_num;
1301 out_sg = elem->out_sg;
1302 if (out_num < 1) {
1303 virtio_error(vdev, "virtio-net header not in first element");
1304 virtqueue_detach_element(q->tx_vq, elem, 0);
1305 g_free(elem);
1306 return -EINVAL;
1307 }
1308
1309 if (n->has_vnet_hdr) {
1310 if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) <
1311 n->guest_hdr_len) {
1312 virtio_error(vdev, "virtio-net header incorrect");
1313 virtqueue_detach_element(q->tx_vq, elem, 0);
1314 g_free(elem);
1315 return -EINVAL;
1316 }
1317 if (n->needs_vnet_hdr_swap) {
1318 virtio_net_hdr_swap(vdev, (void *) &mhdr);
1319 sg2[0].iov_base = &mhdr;
1320 sg2[0].iov_len = n->guest_hdr_len;
1321 out_num = iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1,
1322 out_sg, out_num,
1323 n->guest_hdr_len, -1);
1324 if (out_num == VIRTQUEUE_MAX_SIZE) {
1325 goto drop;
1326 }
1327 out_num += 1;
1328 out_sg = sg2;
1329 }
1330 }
1331 /*
1332 * If host wants to see the guest header as is, we can
1333 * pass it on unchanged. Otherwise, copy just the parts
1334 * that host is interested in.
1335 */
1336 assert(n->host_hdr_len <= n->guest_hdr_len);
1337 if (n->host_hdr_len != n->guest_hdr_len) {
1338 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
1339 out_sg, out_num,
1340 0, n->host_hdr_len);
1341 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
1342 out_sg, out_num,
1343 n->guest_hdr_len, -1);
1344 out_num = sg_num;
1345 out_sg = sg;
1346 }
1347
1348 ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
1349 out_sg, out_num, virtio_net_tx_complete);
1350 if (ret == 0) {
1351 virtio_queue_set_notification(q->tx_vq, 0);
1352 q->async_tx.elem = elem;
1353 return -EBUSY;
1354 }
1355
1356 drop:
1357 virtqueue_push(q->tx_vq, elem, 0);
1358 virtio_notify(vdev, q->tx_vq);
1359 g_free(elem);
1360
1361 if (++num_packets >= n->tx_burst) {
1362 break;
1363 }
1364 }
1365 return num_packets;
1366 }
1367
1368 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
1369 {
1370 VirtIONet *n = VIRTIO_NET(vdev);
1371 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
1372
1373 if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
1374 virtio_net_drop_tx_queue_data(vdev, vq);
1375 return;
1376 }
1377
1378 /* This happens when device was stopped but VCPU wasn't. */
1379 if (!vdev->vm_running) {
1380 q->tx_waiting = 1;
1381 return;
1382 }
1383
1384 if (q->tx_waiting) {
1385 virtio_queue_set_notification(vq, 1);
1386 timer_del(q->tx_timer);
1387 q->tx_waiting = 0;
1388 if (virtio_net_flush_tx(q) == -EINVAL) {
1389 return;
1390 }
1391 } else {
1392 timer_mod(q->tx_timer,
1393 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
1394 q->tx_waiting = 1;
1395 virtio_queue_set_notification(vq, 0);
1396 }
1397 }
1398
1399 static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
1400 {
1401 VirtIONet *n = VIRTIO_NET(vdev);
1402 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
1403
1404 if (unlikely((n->status & VIRTIO_NET_S_LINK_UP) == 0)) {
1405 virtio_net_drop_tx_queue_data(vdev, vq);
1406 return;
1407 }
1408
1409 if (unlikely(q->tx_waiting)) {
1410 return;
1411 }
1412 q->tx_waiting = 1;
1413 /* This happens when device was stopped but VCPU wasn't. */
1414 if (!vdev->vm_running) {
1415 return;
1416 }
1417 virtio_queue_set_notification(vq, 0);
1418 qemu_bh_schedule(q->tx_bh);
1419 }
1420
1421 static void virtio_net_tx_timer(void *opaque)
1422 {
1423 VirtIONetQueue *q = opaque;
1424 VirtIONet *n = q->n;
1425 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1426 /* This happens when device was stopped but BH wasn't. */
1427 if (!vdev->vm_running) {
1428 /* Make sure tx waiting is set, so we'll run when restarted. */
1429 assert(q->tx_waiting);
1430 return;
1431 }
1432
1433 q->tx_waiting = 0;
1434
1435 /* Just in case the driver is not ready on more */
1436 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1437 return;
1438 }
1439
1440 virtio_queue_set_notification(q->tx_vq, 1);
1441 virtio_net_flush_tx(q);
1442 }
1443
1444 static void virtio_net_tx_bh(void *opaque)
1445 {
1446 VirtIONetQueue *q = opaque;
1447 VirtIONet *n = q->n;
1448 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1449 int32_t ret;
1450
1451 /* This happens when device was stopped but BH wasn't. */
1452 if (!vdev->vm_running) {
1453 /* Make sure tx waiting is set, so we'll run when restarted. */
1454 assert(q->tx_waiting);
1455 return;
1456 }
1457
1458 q->tx_waiting = 0;
1459
1460 /* Just in case the driver is not ready on more */
1461 if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
1462 return;
1463 }
1464
1465 ret = virtio_net_flush_tx(q);
1466 if (ret == -EBUSY || ret == -EINVAL) {
1467 return; /* Notification re-enable handled by tx_complete or device
1468 * broken */
1469 }
1470
1471 /* If we flush a full burst of packets, assume there are
1472 * more coming and immediately reschedule */
1473 if (ret >= n->tx_burst) {
1474 qemu_bh_schedule(q->tx_bh);
1475 q->tx_waiting = 1;
1476 return;
1477 }
1478
1479 /* If less than a full burst, re-enable notification and flush
1480 * anything that may have come in while we weren't looking. If
1481 * we find something, assume the guest is still active and reschedule */
1482 virtio_queue_set_notification(q->tx_vq, 1);
1483 ret = virtio_net_flush_tx(q);
1484 if (ret == -EINVAL) {
1485 return;
1486 } else if (ret > 0) {
1487 virtio_queue_set_notification(q->tx_vq, 0);
1488 qemu_bh_schedule(q->tx_bh);
1489 q->tx_waiting = 1;
1490 }
1491 }
1492
1493 static void virtio_net_add_queue(VirtIONet *n, int index)
1494 {
1495 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1496
1497 n->vqs[index].rx_vq = virtio_add_queue(vdev, n->net_conf.rx_queue_size,
1498 virtio_net_handle_rx);
1499 if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
1500 n->vqs[index].tx_vq =
1501 virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer);
1502 n->vqs[index].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
1503 virtio_net_tx_timer,
1504 &n->vqs[index]);
1505 } else {
1506 n->vqs[index].tx_vq =
1507 virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh);
1508 n->vqs[index].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[index]);
1509 }
1510
1511 n->vqs[index].tx_waiting = 0;
1512 n->vqs[index].n = n;
1513 }
1514
1515 static void virtio_net_del_queue(VirtIONet *n, int index)
1516 {
1517 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1518 VirtIONetQueue *q = &n->vqs[index];
1519 NetClientState *nc = qemu_get_subqueue(n->nic, index);
1520
1521 qemu_purge_queued_packets(nc);
1522
1523 virtio_del_queue(vdev, index * 2);
1524 if (q->tx_timer) {
1525 timer_del(q->tx_timer);
1526 timer_free(q->tx_timer);
1527 q->tx_timer = NULL;
1528 } else {
1529 qemu_bh_delete(q->tx_bh);
1530 q->tx_bh = NULL;
1531 }
1532 q->tx_waiting = 0;
1533 virtio_del_queue(vdev, index * 2 + 1);
1534 }
1535
1536 static void virtio_net_change_num_queues(VirtIONet *n, int new_max_queues)
1537 {
1538 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1539 int old_num_queues = virtio_get_num_queues(vdev);
1540 int new_num_queues = new_max_queues * 2 + 1;
1541 int i;
1542
1543 assert(old_num_queues >= 3);
1544 assert(old_num_queues % 2 == 1);
1545
1546 if (old_num_queues == new_num_queues) {
1547 return;
1548 }
1549
1550 /*
1551 * We always need to remove and add ctrl vq if
1552 * old_num_queues != new_num_queues. Remove ctrl_vq first,
1553 * and then we only enter one of the following too loops.
1554 */
1555 virtio_del_queue(vdev, old_num_queues - 1);
1556
1557 for (i = new_num_queues - 1; i < old_num_queues - 1; i += 2) {
1558 /* new_num_queues < old_num_queues */
1559 virtio_net_del_queue(n, i / 2);
1560 }
1561
1562 for (i = old_num_queues - 1; i < new_num_queues - 1; i += 2) {
1563 /* new_num_queues > old_num_queues */
1564 virtio_net_add_queue(n, i / 2);
1565 }
1566
1567 /* add ctrl_vq last */
1568 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
1569 }
1570
1571 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
1572 {
1573 int max = multiqueue ? n->max_queues : 1;
1574
1575 n->multiqueue = multiqueue;
1576 virtio_net_change_num_queues(n, max);
1577
1578 virtio_net_set_queues(n);
1579 }
1580
1581 static int virtio_net_post_load_device(void *opaque, int version_id)
1582 {
1583 VirtIONet *n = opaque;
1584 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1585 int i, link_down;
1586
1587 virtio_net_set_mrg_rx_bufs(n, n->mergeable_rx_bufs,
1588 virtio_vdev_has_feature(vdev,
1589 VIRTIO_F_VERSION_1));
1590
1591 /* MAC_TABLE_ENTRIES may be different from the saved image */
1592 if (n->mac_table.in_use > MAC_TABLE_ENTRIES) {
1593 n->mac_table.in_use = 0;
1594 }
1595
1596 if (!virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_GUEST_OFFLOADS)) {
1597 n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
1598 }
1599
1600 if (peer_has_vnet_hdr(n)) {
1601 virtio_net_apply_guest_offloads(n);
1602 }
1603
1604 virtio_net_set_queues(n);
1605
1606 /* Find the first multicast entry in the saved MAC filter */
1607 for (i = 0; i < n->mac_table.in_use; i++) {
1608 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
1609 break;
1610 }
1611 }
1612 n->mac_table.first_multi = i;
1613
1614 /* nc.link_down can't be migrated, so infer link_down according
1615 * to link status bit in n->status */
1616 link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
1617 for (i = 0; i < n->max_queues; i++) {
1618 qemu_get_subqueue(n->nic, i)->link_down = link_down;
1619 }
1620
1621 if (virtio_vdev_has_feature(vdev, VIRTIO_NET_F_GUEST_ANNOUNCE) &&
1622 virtio_vdev_has_feature(vdev, VIRTIO_NET_F_CTRL_VQ)) {
1623 n->announce_counter = SELF_ANNOUNCE_ROUNDS;
1624 timer_mod(n->announce_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL));
1625 }
1626
1627 return 0;
1628 }
1629
1630 /* tx_waiting field of a VirtIONetQueue */
1631 static const VMStateDescription vmstate_virtio_net_queue_tx_waiting = {
1632 .name = "virtio-net-queue-tx_waiting",
1633 .fields = (VMStateField[]) {
1634 VMSTATE_UINT32(tx_waiting, VirtIONetQueue),
1635 VMSTATE_END_OF_LIST()
1636 },
1637 };
1638
1639 static bool max_queues_gt_1(void *opaque, int version_id)
1640 {
1641 return VIRTIO_NET(opaque)->max_queues > 1;
1642 }
1643
1644 static bool has_ctrl_guest_offloads(void *opaque, int version_id)
1645 {
1646 return virtio_vdev_has_feature(VIRTIO_DEVICE(opaque),
1647 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS);
1648 }
1649
1650 static bool mac_table_fits(void *opaque, int version_id)
1651 {
1652 return VIRTIO_NET(opaque)->mac_table.in_use <= MAC_TABLE_ENTRIES;
1653 }
1654
1655 static bool mac_table_doesnt_fit(void *opaque, int version_id)
1656 {
1657 return !mac_table_fits(opaque, version_id);
1658 }
1659
1660 /* This temporary type is shared by all the WITH_TMP methods
1661 * although only some fields are used by each.
1662 */
1663 struct VirtIONetMigTmp {
1664 VirtIONet *parent;
1665 VirtIONetQueue *vqs_1;
1666 uint16_t curr_queues_1;
1667 uint8_t has_ufo;
1668 uint32_t has_vnet_hdr;
1669 };
1670
1671 /* The 2nd and subsequent tx_waiting flags are loaded later than
1672 * the 1st entry in the queues and only if there's more than one
1673 * entry. We use the tmp mechanism to calculate a temporary
1674 * pointer and count and also validate the count.
1675 */
1676
1677 static void virtio_net_tx_waiting_pre_save(void *opaque)
1678 {
1679 struct VirtIONetMigTmp *tmp = opaque;
1680
1681 tmp->vqs_1 = tmp->parent->vqs + 1;
1682 tmp->curr_queues_1 = tmp->parent->curr_queues - 1;
1683 if (tmp->parent->curr_queues == 0) {
1684 tmp->curr_queues_1 = 0;
1685 }
1686 }
1687
1688 static int virtio_net_tx_waiting_pre_load(void *opaque)
1689 {
1690 struct VirtIONetMigTmp *tmp = opaque;
1691
1692 /* Reuse the pointer setup from save */
1693 virtio_net_tx_waiting_pre_save(opaque);
1694
1695 if (tmp->parent->curr_queues > tmp->parent->max_queues) {
1696 error_report("virtio-net: curr_queues %x > max_queues %x",
1697 tmp->parent->curr_queues, tmp->parent->max_queues);
1698
1699 return -EINVAL;
1700 }
1701
1702 return 0; /* all good */
1703 }
1704
1705 static const VMStateDescription vmstate_virtio_net_tx_waiting = {
1706 .name = "virtio-net-tx_waiting",
1707 .pre_load = virtio_net_tx_waiting_pre_load,
1708 .pre_save = virtio_net_tx_waiting_pre_save,
1709 .fields = (VMStateField[]) {
1710 VMSTATE_STRUCT_VARRAY_POINTER_UINT16(vqs_1, struct VirtIONetMigTmp,
1711 curr_queues_1,
1712 vmstate_virtio_net_queue_tx_waiting,
1713 struct VirtIONetQueue),
1714 VMSTATE_END_OF_LIST()
1715 },
1716 };
1717
1718 /* the 'has_ufo' flag is just tested; if the incoming stream has the
1719 * flag set we need to check that we have it
1720 */
1721 static int virtio_net_ufo_post_load(void *opaque, int version_id)
1722 {
1723 struct VirtIONetMigTmp *tmp = opaque;
1724
1725 if (tmp->has_ufo && !peer_has_ufo(tmp->parent)) {
1726 error_report("virtio-net: saved image requires TUN_F_UFO support");
1727 return -EINVAL;
1728 }
1729
1730 return 0;
1731 }
1732
1733 static void virtio_net_ufo_pre_save(void *opaque)
1734 {
1735 struct VirtIONetMigTmp *tmp = opaque;
1736
1737 tmp->has_ufo = tmp->parent->has_ufo;
1738 }
1739
1740 static const VMStateDescription vmstate_virtio_net_has_ufo = {
1741 .name = "virtio-net-ufo",
1742 .post_load = virtio_net_ufo_post_load,
1743 .pre_save = virtio_net_ufo_pre_save,
1744 .fields = (VMStateField[]) {
1745 VMSTATE_UINT8(has_ufo, struct VirtIONetMigTmp),
1746 VMSTATE_END_OF_LIST()
1747 },
1748 };
1749
1750 /* the 'has_vnet_hdr' flag is just tested; if the incoming stream has the
1751 * flag set we need to check that we have it
1752 */
1753 static int virtio_net_vnet_post_load(void *opaque, int version_id)
1754 {
1755 struct VirtIONetMigTmp *tmp = opaque;
1756
1757 if (tmp->has_vnet_hdr && !peer_has_vnet_hdr(tmp->parent)) {
1758 error_report("virtio-net: saved image requires vnet_hdr=on");
1759 return -EINVAL;
1760 }
1761
1762 return 0;
1763 }
1764
1765 static void virtio_net_vnet_pre_save(void *opaque)
1766 {
1767 struct VirtIONetMigTmp *tmp = opaque;
1768
1769 tmp->has_vnet_hdr = tmp->parent->has_vnet_hdr;
1770 }
1771
1772 static const VMStateDescription vmstate_virtio_net_has_vnet = {
1773 .name = "virtio-net-vnet",
1774 .post_load = virtio_net_vnet_post_load,
1775 .pre_save = virtio_net_vnet_pre_save,
1776 .fields = (VMStateField[]) {
1777 VMSTATE_UINT32(has_vnet_hdr, struct VirtIONetMigTmp),
1778 VMSTATE_END_OF_LIST()
1779 },
1780 };
1781
1782 static const VMStateDescription vmstate_virtio_net_device = {
1783 .name = "virtio-net-device",
1784 .version_id = VIRTIO_NET_VM_VERSION,
1785 .minimum_version_id = VIRTIO_NET_VM_VERSION,
1786 .post_load = virtio_net_post_load_device,
1787 .fields = (VMStateField[]) {
1788 VMSTATE_UINT8_ARRAY(mac, VirtIONet, ETH_ALEN),
1789 VMSTATE_STRUCT_POINTER(vqs, VirtIONet,
1790 vmstate_virtio_net_queue_tx_waiting,
1791 VirtIONetQueue),
1792 VMSTATE_UINT32(mergeable_rx_bufs, VirtIONet),
1793 VMSTATE_UINT16(status, VirtIONet),
1794 VMSTATE_UINT8(promisc, VirtIONet),
1795 VMSTATE_UINT8(allmulti, VirtIONet),
1796 VMSTATE_UINT32(mac_table.in_use, VirtIONet),
1797
1798 /* Guarded pair: If it fits we load it, else we throw it away
1799 * - can happen if source has a larger MAC table.; post-load
1800 * sets flags in this case.
1801 */
1802 VMSTATE_VBUFFER_MULTIPLY(mac_table.macs, VirtIONet,
1803 0, mac_table_fits, mac_table.in_use,
1804 ETH_ALEN),
1805 VMSTATE_UNUSED_VARRAY_UINT32(VirtIONet, mac_table_doesnt_fit, 0,
1806 mac_table.in_use, ETH_ALEN),
1807
1808 /* Note: This is an array of uint32's that's always been saved as a
1809 * buffer; hold onto your endiannesses; it's actually used as a bitmap
1810 * but based on the uint.
1811 */
1812 VMSTATE_BUFFER_POINTER_UNSAFE(vlans, VirtIONet, 0, MAX_VLAN >> 3),
1813 VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
1814 vmstate_virtio_net_has_vnet),
1815 VMSTATE_UINT8(mac_table.multi_overflow, VirtIONet),
1816 VMSTATE_UINT8(mac_table.uni_overflow, VirtIONet),
1817 VMSTATE_UINT8(alluni, VirtIONet),
1818 VMSTATE_UINT8(nomulti, VirtIONet),
1819 VMSTATE_UINT8(nouni, VirtIONet),
1820 VMSTATE_UINT8(nobcast, VirtIONet),
1821 VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
1822 vmstate_virtio_net_has_ufo),
1823 VMSTATE_SINGLE_TEST(max_queues, VirtIONet, max_queues_gt_1, 0,
1824 vmstate_info_uint16_equal, uint16_t),
1825 VMSTATE_UINT16_TEST(curr_queues, VirtIONet, max_queues_gt_1),
1826 VMSTATE_WITH_TMP(VirtIONet, struct VirtIONetMigTmp,
1827 vmstate_virtio_net_tx_waiting),
1828 VMSTATE_UINT64_TEST(curr_guest_offloads, VirtIONet,
1829 has_ctrl_guest_offloads),
1830 VMSTATE_END_OF_LIST()
1831 },
1832 };
1833
1834 static NetClientInfo net_virtio_info = {
1835 .type = NET_CLIENT_DRIVER_NIC,
1836 .size = sizeof(NICState),
1837 .can_receive = virtio_net_can_receive,
1838 .receive = virtio_net_receive,
1839 .link_status_changed = virtio_net_set_link_status,
1840 .query_rx_filter = virtio_net_query_rxfilter,
1841 };
1842
1843 static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
1844 {
1845 VirtIONet *n = VIRTIO_NET(vdev);
1846 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
1847 assert(n->vhost_started);
1848 return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
1849 }
1850
1851 static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
1852 bool mask)
1853 {
1854 VirtIONet *n = VIRTIO_NET(vdev);
1855 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
1856 assert(n->vhost_started);
1857 vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
1858 vdev, idx, mask);
1859 }
1860
1861 static void virtio_net_set_config_size(VirtIONet *n, uint64_t host_features)
1862 {
1863 int i, config_size = 0;
1864 virtio_add_feature(&host_features, VIRTIO_NET_F_MAC);
1865
1866 for (i = 0; feature_sizes[i].flags != 0; i++) {
1867 if (host_features & feature_sizes[i].flags) {
1868 config_size = MAX(feature_sizes[i].end, config_size);
1869 }
1870 }
1871 n->config_size = config_size;
1872 }
1873
1874 void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
1875 const char *type)
1876 {
1877 /*
1878 * The name can be NULL, the netclient name will be type.x.
1879 */
1880 assert(type != NULL);
1881
1882 g_free(n->netclient_name);
1883 g_free(n->netclient_type);
1884 n->netclient_name = g_strdup(name);
1885 n->netclient_type = g_strdup(type);
1886 }
1887
1888 static void virtio_net_device_realize(DeviceState *dev, Error **errp)
1889 {
1890 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1891 VirtIONet *n = VIRTIO_NET(dev);
1892 NetClientState *nc;
1893 int i;
1894
1895 if (n->net_conf.mtu) {
1896 n->host_features |= (0x1 << VIRTIO_NET_F_MTU);
1897 }
1898
1899 virtio_net_set_config_size(n, n->host_features);
1900 virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size);
1901
1902 /*
1903 * We set a lower limit on RX queue size to what it always was.
1904 * Guests that want a smaller ring can always resize it without
1905 * help from us (using virtio 1 and up).
1906 */
1907 if (n->net_conf.rx_queue_size < VIRTIO_NET_RX_QUEUE_MIN_SIZE ||
1908 n->net_conf.rx_queue_size > VIRTQUEUE_MAX_SIZE ||
1909 (n->net_conf.rx_queue_size & (n->net_conf.rx_queue_size - 1))) {
1910 error_setg(errp, "Invalid rx_queue_size (= %" PRIu16 "), "
1911 "must be a power of 2 between %d and %d.",
1912 n->net_conf.rx_queue_size, VIRTIO_NET_RX_QUEUE_MIN_SIZE,
1913 VIRTQUEUE_MAX_SIZE);
1914 virtio_cleanup(vdev);
1915 return;
1916 }
1917
1918 n->max_queues = MAX(n->nic_conf.peers.queues, 1);
1919 if (n->max_queues * 2 + 1 > VIRTIO_QUEUE_MAX) {
1920 error_setg(errp, "Invalid number of queues (= %" PRIu32 "), "
1921 "must be a positive integer less than %d.",
1922 n->max_queues, (VIRTIO_QUEUE_MAX - 1) / 2);
1923 virtio_cleanup(vdev);
1924 return;
1925 }
1926 n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
1927 n->curr_queues = 1;
1928 n->tx_timeout = n->net_conf.txtimer;
1929
1930 if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
1931 && strcmp(n->net_conf.tx, "bh")) {
1932 error_report("virtio-net: "
1933 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1934 n->net_conf.tx);
1935 error_report("Defaulting to \"bh\"");
1936 }
1937
1938 for (i = 0; i < n->max_queues; i++) {
1939 virtio_net_add_queue(n, i);
1940 }
1941
1942 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
1943 qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
1944 memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
1945 n->status = VIRTIO_NET_S_LINK_UP;
1946 n->announce_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
1947 virtio_net_announce_timer, n);
1948
1949 if (n->netclient_type) {
1950 /*
1951 * Happen when virtio_net_set_netclient_name has been called.
1952 */
1953 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
1954 n->netclient_type, n->netclient_name, n);
1955 } else {
1956 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
1957 object_get_typename(OBJECT(dev)), dev->id, n);
1958 }
1959
1960 peer_test_vnet_hdr(n);
1961 if (peer_has_vnet_hdr(n)) {
1962 for (i = 0; i < n->max_queues; i++) {
1963 qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
1964 }
1965 n->host_hdr_len = sizeof(struct virtio_net_hdr);
1966 } else {
1967 n->host_hdr_len = 0;
1968 }
1969
1970 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
1971
1972 n->vqs[0].tx_waiting = 0;
1973 n->tx_burst = n->net_conf.txburst;
1974 virtio_net_set_mrg_rx_bufs(n, 0, 0);
1975 n->promisc = 1; /* for compatibility */
1976
1977 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
1978
1979 n->vlans = g_malloc0(MAX_VLAN >> 3);
1980
1981 nc = qemu_get_queue(n->nic);
1982 nc->rxfilter_notify_enabled = 1;
1983
1984 n->qdev = dev;
1985 }
1986
1987 static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
1988 {
1989 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1990 VirtIONet *n = VIRTIO_NET(dev);
1991 int i, max_queues;
1992
1993 /* This will stop vhost backend if appropriate. */
1994 virtio_net_set_status(vdev, 0);
1995
1996 g_free(n->netclient_name);
1997 n->netclient_name = NULL;
1998 g_free(n->netclient_type);
1999 n->netclient_type = NULL;
2000
2001 g_free(n->mac_table.macs);
2002 g_free(n->vlans);
2003
2004 max_queues = n->multiqueue ? n->max_queues : 1;
2005 for (i = 0; i < max_queues; i++) {
2006 virtio_net_del_queue(n, i);
2007 }
2008
2009 timer_del(n->announce_timer);
2010 timer_free(n->announce_timer);
2011 g_free(n->vqs);
2012 qemu_del_nic(n->nic);
2013 virtio_cleanup(vdev);
2014 }
2015
2016 static void virtio_net_instance_init(Object *obj)
2017 {
2018 VirtIONet *n = VIRTIO_NET(obj);
2019
2020 /*
2021 * The default config_size is sizeof(struct virtio_net_config).
2022 * Can be overriden with virtio_net_set_config_size.
2023 */
2024 n->config_size = sizeof(struct virtio_net_config);
2025 device_add_bootindex_property(obj, &n->nic_conf.bootindex,
2026 "bootindex", "/ethernet-phy@0",
2027 DEVICE(n), NULL);
2028 }
2029
2030 static void virtio_net_pre_save(void *opaque)
2031 {
2032 VirtIONet *n = opaque;
2033
2034 /* At this point, backend must be stopped, otherwise
2035 * it might keep writing to memory. */
2036 assert(!n->vhost_started);
2037 }
2038
2039 static const VMStateDescription vmstate_virtio_net = {
2040 .name = "virtio-net",
2041 .minimum_version_id = VIRTIO_NET_VM_VERSION,
2042 .version_id = VIRTIO_NET_VM_VERSION,
2043 .fields = (VMStateField[]) {
2044 VMSTATE_VIRTIO_DEVICE,
2045 VMSTATE_END_OF_LIST()
2046 },
2047 .pre_save = virtio_net_pre_save,
2048 };
2049
2050 static Property virtio_net_properties[] = {
2051 DEFINE_PROP_BIT("csum", VirtIONet, host_features, VIRTIO_NET_F_CSUM, true),
2052 DEFINE_PROP_BIT("guest_csum", VirtIONet, host_features,
2053 VIRTIO_NET_F_GUEST_CSUM, true),
2054 DEFINE_PROP_BIT("gso", VirtIONet, host_features, VIRTIO_NET_F_GSO, true),
2055 DEFINE_PROP_BIT("guest_tso4", VirtIONet, host_features,
2056 VIRTIO_NET_F_GUEST_TSO4, true),
2057 DEFINE_PROP_BIT("guest_tso6", VirtIONet, host_features,
2058 VIRTIO_NET_F_GUEST_TSO6, true),
2059 DEFINE_PROP_BIT("guest_ecn", VirtIONet, host_features,
2060 VIRTIO_NET_F_GUEST_ECN, true),
2061 DEFINE_PROP_BIT("guest_ufo", VirtIONet, host_features,
2062 VIRTIO_NET_F_GUEST_UFO, true),
2063 DEFINE_PROP_BIT("guest_announce", VirtIONet, host_features,
2064 VIRTIO_NET_F_GUEST_ANNOUNCE, true),
2065 DEFINE_PROP_BIT("host_tso4", VirtIONet, host_features,
2066 VIRTIO_NET_F_HOST_TSO4, true),
2067 DEFINE_PROP_BIT("host_tso6", VirtIONet, host_features,
2068 VIRTIO_NET_F_HOST_TSO6, true),
2069 DEFINE_PROP_BIT("host_ecn", VirtIONet, host_features,
2070 VIRTIO_NET_F_HOST_ECN, true),
2071 DEFINE_PROP_BIT("host_ufo", VirtIONet, host_features,
2072 VIRTIO_NET_F_HOST_UFO, true),
2073 DEFINE_PROP_BIT("mrg_rxbuf", VirtIONet, host_features,
2074 VIRTIO_NET_F_MRG_RXBUF, true),
2075 DEFINE_PROP_BIT("status", VirtIONet, host_features,
2076 VIRTIO_NET_F_STATUS, true),
2077 DEFINE_PROP_BIT("ctrl_vq", VirtIONet, host_features,
2078 VIRTIO_NET_F_CTRL_VQ, true),
2079 DEFINE_PROP_BIT("ctrl_rx", VirtIONet, host_features,
2080 VIRTIO_NET_F_CTRL_RX, true),
2081 DEFINE_PROP_BIT("ctrl_vlan", VirtIONet, host_features,
2082 VIRTIO_NET_F_CTRL_VLAN, true),
2083 DEFINE_PROP_BIT("ctrl_rx_extra", VirtIONet, host_features,
2084 VIRTIO_NET_F_CTRL_RX_EXTRA, true),
2085 DEFINE_PROP_BIT("ctrl_mac_addr", VirtIONet, host_features,
2086 VIRTIO_NET_F_CTRL_MAC_ADDR, true),
2087 DEFINE_PROP_BIT("ctrl_guest_offloads", VirtIONet, host_features,
2088 VIRTIO_NET_F_CTRL_GUEST_OFFLOADS, true),
2089 DEFINE_PROP_BIT("mq", VirtIONet, host_features, VIRTIO_NET_F_MQ, false),
2090 DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
2091 DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
2092 TX_TIMER_INTERVAL),
2093 DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
2094 DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
2095 DEFINE_PROP_UINT16("rx_queue_size", VirtIONet, net_conf.rx_queue_size,
2096 VIRTIO_NET_RX_QUEUE_DEFAULT_SIZE),
2097 DEFINE_PROP_UINT16("host_mtu", VirtIONet, net_conf.mtu, 0),
2098 DEFINE_PROP_END_OF_LIST(),
2099 };
2100
2101 static void virtio_net_class_init(ObjectClass *klass, void *data)
2102 {
2103 DeviceClass *dc = DEVICE_CLASS(klass);
2104 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
2105
2106 dc->props = virtio_net_properties;
2107 dc->vmsd = &vmstate_virtio_net;
2108 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
2109 vdc->realize = virtio_net_device_realize;
2110 vdc->unrealize = virtio_net_device_unrealize;
2111 vdc->get_config = virtio_net_get_config;
2112 vdc->set_config = virtio_net_set_config;
2113 vdc->get_features = virtio_net_get_features;
2114 vdc->set_features = virtio_net_set_features;
2115 vdc->bad_features = virtio_net_bad_features;
2116 vdc->reset = virtio_net_reset;
2117 vdc->set_status = virtio_net_set_status;
2118 vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
2119 vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
2120 vdc->legacy_features |= (0x1 << VIRTIO_NET_F_GSO);
2121 vdc->vmsd = &vmstate_virtio_net_device;
2122 }
2123
2124 static const TypeInfo virtio_net_info = {
2125 .name = TYPE_VIRTIO_NET,
2126 .parent = TYPE_VIRTIO_DEVICE,
2127 .instance_size = sizeof(VirtIONet),
2128 .instance_init = virtio_net_instance_init,
2129 .class_init = virtio_net_class_init,
2130 };
2131
2132 static void virtio_register_types(void)
2133 {
2134 type_register_static(&virtio_net_info);
2135 }
2136
2137 type_init(virtio_register_types)