2 * Virtio Network Device
4 * Copyright IBM, Corp. 2007
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
15 #include "hw/virtio/virtio.h"
17 #include "net/checksum.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 #include "qapi/qmp/qjson.h"
25 #include "qapi-event.h"
27 #define VIRTIO_NET_VM_VERSION 11
29 #define MAC_TABLE_ENTRIES 64
30 #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
33 * Calculate the number of bytes up to and including the given 'field' of
36 #define endof(container, field) \
37 (offsetof(container, field) + sizeof(((container *)0)->field))
39 typedef struct VirtIOFeature {
44 static VirtIOFeature feature_sizes[] = {
45 {.flags = 1 << VIRTIO_NET_F_MAC,
46 .end = endof(struct virtio_net_config, mac)},
47 {.flags = 1 << VIRTIO_NET_F_STATUS,
48 .end = endof(struct virtio_net_config, status)},
49 {.flags = 1 << VIRTIO_NET_F_MQ,
50 .end = endof(struct virtio_net_config, max_virtqueue_pairs)},
54 static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc)
56 VirtIONet *n = qemu_get_nic_opaque(nc);
58 return &n->vqs[nc->queue_index];
61 static int vq2q(int queue_index)
63 return queue_index / 2;
67 * - we could suppress RX interrupt if we were so inclined.
70 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
72 VirtIONet *n = VIRTIO_NET(vdev);
73 struct virtio_net_config netcfg;
75 stw_p(&netcfg.status, n->status);
76 stw_p(&netcfg.max_virtqueue_pairs, n->max_queues);
77 memcpy(netcfg.mac, n->mac, ETH_ALEN);
78 memcpy(config, &netcfg, n->config_size);
81 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
83 VirtIONet *n = VIRTIO_NET(vdev);
84 struct virtio_net_config netcfg = {};
86 memcpy(&netcfg, config, n->config_size);
88 if (!(vdev->guest_features >> VIRTIO_NET_F_CTRL_MAC_ADDR & 1) &&
89 memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
90 memcpy(n->mac, netcfg.mac, ETH_ALEN);
91 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
95 static bool virtio_net_started(VirtIONet *n, uint8_t status)
97 VirtIODevice *vdev = VIRTIO_DEVICE(n);
98 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
99 (n->status & VIRTIO_NET_S_LINK_UP) && vdev->vm_running;
102 static void virtio_net_announce_timer(void *opaque)
104 VirtIONet *n = opaque;
105 VirtIODevice *vdev = VIRTIO_DEVICE(n);
107 n->announce_counter--;
108 n->status |= VIRTIO_NET_S_ANNOUNCE;
109 virtio_notify_config(vdev);
112 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
114 VirtIODevice *vdev = VIRTIO_DEVICE(n);
115 NetClientState *nc = qemu_get_queue(n->nic);
116 int queues = n->multiqueue ? n->max_queues : 1;
118 if (!get_vhost_net(nc->peer)) {
122 if (!!n->vhost_started ==
123 (virtio_net_started(n, status) && !nc->peer->link_down)) {
126 if (!n->vhost_started) {
128 if (!vhost_net_query(get_vhost_net(nc->peer), vdev)) {
131 n->vhost_started = 1;
132 r = vhost_net_start(vdev, n->nic->ncs, queues);
134 error_report("unable to start vhost net: %d: "
135 "falling back on userspace virtio", -r);
136 n->vhost_started = 0;
139 vhost_net_stop(vdev, n->nic->ncs, queues);
140 n->vhost_started = 0;
144 static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
146 VirtIONet *n = VIRTIO_NET(vdev);
149 uint8_t queue_status;
151 virtio_net_vhost_status(n, status);
153 for (i = 0; i < n->max_queues; i++) {
156 if ((!n->multiqueue && i != 0) || i >= n->curr_queues) {
159 queue_status = status;
162 if (!q->tx_waiting) {
166 if (virtio_net_started(n, queue_status) && !n->vhost_started) {
168 timer_mod(q->tx_timer,
169 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
171 qemu_bh_schedule(q->tx_bh);
175 timer_del(q->tx_timer);
177 qemu_bh_cancel(q->tx_bh);
183 static void virtio_net_set_link_status(NetClientState *nc)
185 VirtIONet *n = qemu_get_nic_opaque(nc);
186 VirtIODevice *vdev = VIRTIO_DEVICE(n);
187 uint16_t old_status = n->status;
190 n->status &= ~VIRTIO_NET_S_LINK_UP;
192 n->status |= VIRTIO_NET_S_LINK_UP;
194 if (n->status != old_status)
195 virtio_notify_config(vdev);
197 virtio_net_set_status(vdev, vdev->status);
200 static void rxfilter_notify(NetClientState *nc)
202 VirtIONet *n = qemu_get_nic_opaque(nc);
204 if (nc->rxfilter_notify_enabled) {
205 gchar *path = object_get_canonical_path(OBJECT(n->qdev));
206 qapi_event_send_nic_rx_filter_changed(!!n->netclient_name,
207 n->netclient_name, path, &error_abort);
210 /* disable event notification to avoid events flooding */
211 nc->rxfilter_notify_enabled = 0;
215 static char *mac_strdup_printf(const uint8_t *mac)
217 return g_strdup_printf("%.2x:%.2x:%.2x:%.2x:%.2x:%.2x", mac[0],
218 mac[1], mac[2], mac[3], mac[4], mac[5]);
221 static intList *get_vlan_table(VirtIONet *n)
223 intList *list, *entry;
227 for (i = 0; i < MAX_VLAN >> 5; i++) {
228 for (j = 0; n->vlans[i] && j <= 0x1f; j++) {
229 if (n->vlans[i] & (1U << j)) {
230 entry = g_malloc0(sizeof(*entry));
231 entry->value = (i << 5) + j;
241 static RxFilterInfo *virtio_net_query_rxfilter(NetClientState *nc)
243 VirtIONet *n = qemu_get_nic_opaque(nc);
244 VirtIODevice *vdev = VIRTIO_DEVICE(n);
246 strList *str_list, *entry;
249 info = g_malloc0(sizeof(*info));
250 info->name = g_strdup(nc->name);
251 info->promiscuous = n->promisc;
254 info->unicast = RX_STATE_NONE;
255 } else if (n->alluni) {
256 info->unicast = RX_STATE_ALL;
258 info->unicast = RX_STATE_NORMAL;
262 info->multicast = RX_STATE_NONE;
263 } else if (n->allmulti) {
264 info->multicast = RX_STATE_ALL;
266 info->multicast = RX_STATE_NORMAL;
269 info->broadcast_allowed = n->nobcast;
270 info->multicast_overflow = n->mac_table.multi_overflow;
271 info->unicast_overflow = n->mac_table.uni_overflow;
273 info->main_mac = mac_strdup_printf(n->mac);
276 for (i = 0; i < n->mac_table.first_multi; i++) {
277 entry = g_malloc0(sizeof(*entry));
278 entry->value = mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
279 entry->next = str_list;
282 info->unicast_table = str_list;
285 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
286 entry = g_malloc0(sizeof(*entry));
287 entry->value = mac_strdup_printf(n->mac_table.macs + i * ETH_ALEN);
288 entry->next = str_list;
291 info->multicast_table = str_list;
292 info->vlan_table = get_vlan_table(n);
294 if (!((1 << VIRTIO_NET_F_CTRL_VLAN) & vdev->guest_features)) {
295 info->vlan = RX_STATE_ALL;
296 } else if (!info->vlan_table) {
297 info->vlan = RX_STATE_NONE;
299 info->vlan = RX_STATE_NORMAL;
302 /* enable event notification after query */
303 nc->rxfilter_notify_enabled = 1;
308 static void virtio_net_reset(VirtIODevice *vdev)
310 VirtIONet *n = VIRTIO_NET(vdev);
312 /* Reset back to compatibility mode */
319 /* multiqueue is disabled by default */
321 timer_del(n->announce_timer);
322 n->announce_counter = 0;
323 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
325 /* Flush any MAC and VLAN filter table state */
326 n->mac_table.in_use = 0;
327 n->mac_table.first_multi = 0;
328 n->mac_table.multi_overflow = 0;
329 n->mac_table.uni_overflow = 0;
330 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
331 memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
332 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
333 memset(n->vlans, 0, MAX_VLAN >> 3);
336 static void peer_test_vnet_hdr(VirtIONet *n)
338 NetClientState *nc = qemu_get_queue(n->nic);
343 n->has_vnet_hdr = qemu_has_vnet_hdr(nc->peer);
346 static int peer_has_vnet_hdr(VirtIONet *n)
348 return n->has_vnet_hdr;
351 static int peer_has_ufo(VirtIONet *n)
353 if (!peer_has_vnet_hdr(n))
356 n->has_ufo = qemu_has_ufo(qemu_get_queue(n->nic)->peer);
361 static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs)
366 n->mergeable_rx_bufs = mergeable_rx_bufs;
368 n->guest_hdr_len = n->mergeable_rx_bufs ?
369 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
371 for (i = 0; i < n->max_queues; i++) {
372 nc = qemu_get_subqueue(n->nic, i);
374 if (peer_has_vnet_hdr(n) &&
375 qemu_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) {
376 qemu_set_vnet_hdr_len(nc->peer, n->guest_hdr_len);
377 n->host_hdr_len = n->guest_hdr_len;
382 static int peer_attach(VirtIONet *n, int index)
384 NetClientState *nc = qemu_get_subqueue(n->nic, index);
390 if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
394 return tap_enable(nc->peer);
397 static int peer_detach(VirtIONet *n, int index)
399 NetClientState *nc = qemu_get_subqueue(n->nic, index);
405 if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
409 return tap_disable(nc->peer);
412 static void virtio_net_set_queues(VirtIONet *n)
417 for (i = 0; i < n->max_queues; i++) {
418 if (i < n->curr_queues) {
419 r = peer_attach(n, i);
422 r = peer_detach(n, i);
428 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue);
430 static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
432 VirtIONet *n = VIRTIO_NET(vdev);
433 NetClientState *nc = qemu_get_queue(n->nic);
435 features |= (1 << VIRTIO_NET_F_MAC);
437 if (!peer_has_vnet_hdr(n)) {
438 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
439 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
440 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
441 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
443 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
444 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
445 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
446 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
449 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
450 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
451 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
454 if (!get_vhost_net(nc->peer)) {
457 return vhost_net_get_features(get_vhost_net(nc->peer), features);
460 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
462 uint32_t features = 0;
464 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
466 features |= (1 << VIRTIO_NET_F_MAC);
467 features |= (1 << VIRTIO_NET_F_CSUM);
468 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
469 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
470 features |= (1 << VIRTIO_NET_F_HOST_ECN);
475 static void virtio_net_apply_guest_offloads(VirtIONet *n)
477 qemu_set_offload(qemu_get_queue(n->nic)->peer,
478 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)),
479 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)),
480 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)),
481 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)),
482 !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO)));
485 static uint64_t virtio_net_guest_offloads_by_features(uint32_t features)
487 static const uint64_t guest_offloads_mask =
488 (1ULL << VIRTIO_NET_F_GUEST_CSUM) |
489 (1ULL << VIRTIO_NET_F_GUEST_TSO4) |
490 (1ULL << VIRTIO_NET_F_GUEST_TSO6) |
491 (1ULL << VIRTIO_NET_F_GUEST_ECN) |
492 (1ULL << VIRTIO_NET_F_GUEST_UFO);
494 return guest_offloads_mask & features;
497 static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n)
499 VirtIODevice *vdev = VIRTIO_DEVICE(n);
500 return virtio_net_guest_offloads_by_features(vdev->guest_features);
503 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
505 VirtIONet *n = VIRTIO_NET(vdev);
508 virtio_net_set_multiqueue(n, !!(features & (1 << VIRTIO_NET_F_MQ)));
510 virtio_net_set_mrg_rx_bufs(n, !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF)));
512 if (n->has_vnet_hdr) {
513 n->curr_guest_offloads =
514 virtio_net_guest_offloads_by_features(features);
515 virtio_net_apply_guest_offloads(n);
518 for (i = 0; i < n->max_queues; i++) {
519 NetClientState *nc = qemu_get_subqueue(n->nic, i);
521 if (!get_vhost_net(nc->peer)) {
524 vhost_net_ack_features(get_vhost_net(nc->peer), features);
527 if ((1 << VIRTIO_NET_F_CTRL_VLAN) & features) {
528 memset(n->vlans, 0, MAX_VLAN >> 3);
530 memset(n->vlans, 0xff, MAX_VLAN >> 3);
534 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
535 struct iovec *iov, unsigned int iov_cnt)
539 NetClientState *nc = qemu_get_queue(n->nic);
541 s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
542 if (s != sizeof(on)) {
543 return VIRTIO_NET_ERR;
546 if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
548 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
550 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
552 } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
554 } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
556 } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
559 return VIRTIO_NET_ERR;
564 return VIRTIO_NET_OK;
567 static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd,
568 struct iovec *iov, unsigned int iov_cnt)
570 VirtIODevice *vdev = VIRTIO_DEVICE(n);
574 if (!((1 << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) & vdev->guest_features)) {
575 return VIRTIO_NET_ERR;
578 s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads));
579 if (s != sizeof(offloads)) {
580 return VIRTIO_NET_ERR;
583 if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) {
584 uint64_t supported_offloads;
586 if (!n->has_vnet_hdr) {
587 return VIRTIO_NET_ERR;
590 supported_offloads = virtio_net_supported_guest_offloads(n);
591 if (offloads & ~supported_offloads) {
592 return VIRTIO_NET_ERR;
595 n->curr_guest_offloads = offloads;
596 virtio_net_apply_guest_offloads(n);
598 return VIRTIO_NET_OK;
600 return VIRTIO_NET_ERR;
604 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
605 struct iovec *iov, unsigned int iov_cnt)
607 struct virtio_net_ctrl_mac mac_data;
609 NetClientState *nc = qemu_get_queue(n->nic);
611 if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
612 if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
613 return VIRTIO_NET_ERR;
615 s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
616 assert(s == sizeof(n->mac));
617 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
620 return VIRTIO_NET_OK;
623 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
624 return VIRTIO_NET_ERR;
629 uint8_t uni_overflow = 0;
630 uint8_t multi_overflow = 0;
631 uint8_t *macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
633 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
634 sizeof(mac_data.entries));
635 mac_data.entries = ldl_p(&mac_data.entries);
636 if (s != sizeof(mac_data.entries)) {
639 iov_discard_front(&iov, &iov_cnt, s);
641 if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
645 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
646 s = iov_to_buf(iov, iov_cnt, 0, macs,
647 mac_data.entries * ETH_ALEN);
648 if (s != mac_data.entries * ETH_ALEN) {
651 in_use += mac_data.entries;
656 iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
658 first_multi = in_use;
660 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
661 sizeof(mac_data.entries));
662 mac_data.entries = ldl_p(&mac_data.entries);
663 if (s != sizeof(mac_data.entries)) {
667 iov_discard_front(&iov, &iov_cnt, s);
669 if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
673 if (mac_data.entries <= MAC_TABLE_ENTRIES - in_use) {
674 s = iov_to_buf(iov, iov_cnt, 0, &macs[in_use * ETH_ALEN],
675 mac_data.entries * ETH_ALEN);
676 if (s != mac_data.entries * ETH_ALEN) {
679 in_use += mac_data.entries;
684 n->mac_table.in_use = in_use;
685 n->mac_table.first_multi = first_multi;
686 n->mac_table.uni_overflow = uni_overflow;
687 n->mac_table.multi_overflow = multi_overflow;
688 memcpy(n->mac_table.macs, macs, MAC_TABLE_ENTRIES * ETH_ALEN);
692 return VIRTIO_NET_OK;
696 return VIRTIO_NET_ERR;
699 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
700 struct iovec *iov, unsigned int iov_cnt)
704 NetClientState *nc = qemu_get_queue(n->nic);
706 s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
708 if (s != sizeof(vid)) {
709 return VIRTIO_NET_ERR;
713 return VIRTIO_NET_ERR;
715 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
716 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
717 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
718 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
720 return VIRTIO_NET_ERR;
724 return VIRTIO_NET_OK;
727 static int virtio_net_handle_announce(VirtIONet *n, uint8_t cmd,
728 struct iovec *iov, unsigned int iov_cnt)
730 if (cmd == VIRTIO_NET_CTRL_ANNOUNCE_ACK &&
731 n->status & VIRTIO_NET_S_ANNOUNCE) {
732 n->status &= ~VIRTIO_NET_S_ANNOUNCE;
733 if (n->announce_counter) {
734 timer_mod(n->announce_timer,
735 qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL) +
736 self_announce_delay(n->announce_counter));
738 return VIRTIO_NET_OK;
740 return VIRTIO_NET_ERR;
744 static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd,
745 struct iovec *iov, unsigned int iov_cnt)
747 VirtIODevice *vdev = VIRTIO_DEVICE(n);
748 struct virtio_net_ctrl_mq mq;
752 s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq));
753 if (s != sizeof(mq)) {
754 return VIRTIO_NET_ERR;
757 if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) {
758 return VIRTIO_NET_ERR;
761 queues = lduw_p(&mq.virtqueue_pairs);
763 if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN ||
764 queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX ||
765 queues > n->max_queues ||
767 return VIRTIO_NET_ERR;
770 n->curr_queues = queues;
771 /* stop the backend before changing the number of queues to avoid handling a
773 virtio_net_set_status(vdev, vdev->status);
774 virtio_net_set_queues(n);
776 return VIRTIO_NET_OK;
778 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
780 VirtIONet *n = VIRTIO_NET(vdev);
781 struct virtio_net_ctrl_hdr ctrl;
782 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
783 VirtQueueElement elem;
786 unsigned int iov_cnt;
788 while (virtqueue_pop(vq, &elem)) {
789 if (iov_size(elem.in_sg, elem.in_num) < sizeof(status) ||
790 iov_size(elem.out_sg, elem.out_num) < sizeof(ctrl)) {
791 error_report("virtio-net ctrl missing headers");
796 iov_cnt = elem.out_num;
797 s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
798 iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
799 if (s != sizeof(ctrl)) {
800 status = VIRTIO_NET_ERR;
801 } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
802 status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
803 } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
804 status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
805 } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
806 status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
807 } else if (ctrl.class == VIRTIO_NET_CTRL_ANNOUNCE) {
808 status = virtio_net_handle_announce(n, ctrl.cmd, iov, iov_cnt);
809 } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) {
810 status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt);
811 } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) {
812 status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt);
815 s = iov_from_buf(elem.in_sg, elem.in_num, 0, &status, sizeof(status));
816 assert(s == sizeof(status));
818 virtqueue_push(vq, &elem, sizeof(status));
819 virtio_notify(vdev, vq);
825 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
827 VirtIONet *n = VIRTIO_NET(vdev);
828 int queue_index = vq2q(virtio_get_queue_index(vq));
830 qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index));
833 static int virtio_net_can_receive(NetClientState *nc)
835 VirtIONet *n = qemu_get_nic_opaque(nc);
836 VirtIODevice *vdev = VIRTIO_DEVICE(n);
837 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
839 if (!vdev->vm_running) {
843 if (nc->queue_index >= n->curr_queues) {
847 if (!virtio_queue_ready(q->rx_vq) ||
848 !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
855 static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
858 if (virtio_queue_empty(q->rx_vq) ||
859 (n->mergeable_rx_bufs &&
860 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
861 virtio_queue_set_notification(q->rx_vq, 1);
863 /* To avoid a race condition where the guest has made some buffers
864 * available after the above check but before notification was
865 * enabled, check for available buffers again.
867 if (virtio_queue_empty(q->rx_vq) ||
868 (n->mergeable_rx_bufs &&
869 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
874 virtio_queue_set_notification(q->rx_vq, 0);
878 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
879 * it never finds out that the packets don't have valid checksums. This
880 * causes dhclient to get upset. Fedora's carried a patch for ages to
881 * fix this with Xen but it hasn't appeared in an upstream release of
884 * To avoid breaking existing guests, we catch udp packets and add
885 * checksums. This is terrible but it's better than hacking the guest
888 * N.B. if we introduce a zero-copy API, this operation is no longer free so
889 * we should provide a mechanism to disable it to avoid polluting the host
892 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
893 uint8_t *buf, size_t size)
895 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
896 (size > 27 && size < 1500) && /* normal sized MTU */
897 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
898 (buf[23] == 17) && /* ip.protocol == UDP */
899 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
900 net_checksum_calculate(buf, size);
901 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
905 static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
906 const void *buf, size_t size)
908 if (n->has_vnet_hdr) {
909 /* FIXME this cast is evil */
910 void *wbuf = (void *)buf;
911 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
912 size - n->host_hdr_len);
913 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
915 struct virtio_net_hdr hdr = {
917 .gso_type = VIRTIO_NET_HDR_GSO_NONE
919 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
923 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
925 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
926 static const uint8_t vlan[] = {0x81, 0x00};
927 uint8_t *ptr = (uint8_t *)buf;
933 ptr += n->host_hdr_len;
935 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
936 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
937 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
941 if (ptr[0] & 1) { // multicast
942 if (!memcmp(ptr, bcast, sizeof(bcast))) {
944 } else if (n->nomulti) {
946 } else if (n->allmulti || n->mac_table.multi_overflow) {
950 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
951 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
958 } else if (n->alluni || n->mac_table.uni_overflow) {
960 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
964 for (i = 0; i < n->mac_table.first_multi; i++) {
965 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
974 static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
976 VirtIONet *n = qemu_get_nic_opaque(nc);
977 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
978 VirtIODevice *vdev = VIRTIO_DEVICE(n);
979 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
980 struct virtio_net_hdr_mrg_rxbuf mhdr;
981 unsigned mhdr_cnt = 0;
982 size_t offset, i, guest_offset;
984 if (!virtio_net_can_receive(nc)) {
988 /* hdr_len refers to the header we supply to the guest */
989 if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
993 if (!receive_filter(n, buf, size))
998 while (offset < size) {
999 VirtQueueElement elem;
1001 const struct iovec *sg = elem.in_sg;
1005 if (virtqueue_pop(q->rx_vq, &elem) == 0) {
1008 error_report("virtio-net unexpected empty queue: "
1009 "i %zd mergeable %d offset %zd, size %zd, "
1010 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
1011 i, n->mergeable_rx_bufs, offset, size,
1012 n->guest_hdr_len, n->host_hdr_len, vdev->guest_features);
1016 if (elem.in_num < 1) {
1017 error_report("virtio-net receive queue contains no in buffers");
1022 assert(offset == 0);
1023 if (n->mergeable_rx_bufs) {
1024 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
1026 offsetof(typeof(mhdr), num_buffers),
1027 sizeof(mhdr.num_buffers));
1030 receive_header(n, sg, elem.in_num, buf, size);
1031 offset = n->host_hdr_len;
1032 total += n->guest_hdr_len;
1033 guest_offset = n->guest_hdr_len;
1038 /* copy in packet. ugh */
1039 len = iov_from_buf(sg, elem.in_num, guest_offset,
1040 buf + offset, size - offset);
1043 /* If buffers can't be merged, at this point we
1044 * must have consumed the complete packet.
1045 * Otherwise, drop it. */
1046 if (!n->mergeable_rx_bufs && offset < size) {
1048 error_report("virtio-net truncated non-mergeable packet: "
1049 "i %zd mergeable %d offset %zd, size %zd, "
1050 "guest hdr len %zd, host hdr len %zd",
1051 i, n->mergeable_rx_bufs,
1052 offset, size, n->guest_hdr_len, n->host_hdr_len);
1057 /* signal other side */
1058 virtqueue_fill(q->rx_vq, &elem, total, i++);
1062 stw_p(&mhdr.num_buffers, i);
1063 iov_from_buf(mhdr_sg, mhdr_cnt,
1065 &mhdr.num_buffers, sizeof mhdr.num_buffers);
1068 virtqueue_flush(q->rx_vq, i);
1069 virtio_notify(vdev, q->rx_vq);
1074 static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
1076 static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
1078 VirtIONet *n = qemu_get_nic_opaque(nc);
1079 VirtIONetQueue *q = virtio_net_get_subqueue(nc);
1080 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1082 virtqueue_push(q->tx_vq, &q->async_tx.elem, 0);
1083 virtio_notify(vdev, q->tx_vq);
1085 q->async_tx.elem.out_num = q->async_tx.len = 0;
1087 virtio_queue_set_notification(q->tx_vq, 1);
1088 virtio_net_flush_tx(q);
1092 static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
1094 VirtIONet *n = q->n;
1095 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1096 VirtQueueElement elem;
1097 int32_t num_packets = 0;
1098 int queue_index = vq2q(virtio_get_queue_index(q->tx_vq));
1099 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1103 assert(vdev->vm_running);
1105 if (q->async_tx.elem.out_num) {
1106 virtio_queue_set_notification(q->tx_vq, 0);
1110 while (virtqueue_pop(q->tx_vq, &elem)) {
1112 unsigned int out_num = elem.out_num;
1113 struct iovec *out_sg = &elem.out_sg[0];
1114 struct iovec sg[VIRTQUEUE_MAX_SIZE];
1117 error_report("virtio-net header not in first element");
1122 * If host wants to see the guest header as is, we can
1123 * pass it on unchanged. Otherwise, copy just the parts
1124 * that host is interested in.
1126 assert(n->host_hdr_len <= n->guest_hdr_len);
1127 if (n->host_hdr_len != n->guest_hdr_len) {
1128 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
1130 0, n->host_hdr_len);
1131 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
1133 n->guest_hdr_len, -1);
1138 len = n->guest_hdr_len;
1140 ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index),
1141 out_sg, out_num, virtio_net_tx_complete);
1143 virtio_queue_set_notification(q->tx_vq, 0);
1144 q->async_tx.elem = elem;
1145 q->async_tx.len = len;
1151 virtqueue_push(q->tx_vq, &elem, 0);
1152 virtio_notify(vdev, q->tx_vq);
1154 if (++num_packets >= n->tx_burst) {
1161 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
1163 VirtIONet *n = VIRTIO_NET(vdev);
1164 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
1166 /* This happens when device was stopped but VCPU wasn't. */
1167 if (!vdev->vm_running) {
1172 if (q->tx_waiting) {
1173 virtio_queue_set_notification(vq, 1);
1174 timer_del(q->tx_timer);
1176 virtio_net_flush_tx(q);
1178 timer_mod(q->tx_timer,
1179 qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + n->tx_timeout);
1181 virtio_queue_set_notification(vq, 0);
1185 static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
1187 VirtIONet *n = VIRTIO_NET(vdev);
1188 VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))];
1190 if (unlikely(q->tx_waiting)) {
1194 /* This happens when device was stopped but VCPU wasn't. */
1195 if (!vdev->vm_running) {
1198 virtio_queue_set_notification(vq, 0);
1199 qemu_bh_schedule(q->tx_bh);
1202 static void virtio_net_tx_timer(void *opaque)
1204 VirtIONetQueue *q = opaque;
1205 VirtIONet *n = q->n;
1206 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1207 assert(vdev->vm_running);
1211 /* Just in case the driver is not ready on more */
1212 if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) {
1216 virtio_queue_set_notification(q->tx_vq, 1);
1217 virtio_net_flush_tx(q);
1220 static void virtio_net_tx_bh(void *opaque)
1222 VirtIONetQueue *q = opaque;
1223 VirtIONet *n = q->n;
1224 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1227 assert(vdev->vm_running);
1231 /* Just in case the driver is not ready on more */
1232 if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) {
1236 ret = virtio_net_flush_tx(q);
1237 if (ret == -EBUSY) {
1238 return; /* Notification re-enable handled by tx_complete */
1241 /* If we flush a full burst of packets, assume there are
1242 * more coming and immediately reschedule */
1243 if (ret >= n->tx_burst) {
1244 qemu_bh_schedule(q->tx_bh);
1249 /* If less than a full burst, re-enable notification and flush
1250 * anything that may have come in while we weren't looking. If
1251 * we find something, assume the guest is still active and reschedule */
1252 virtio_queue_set_notification(q->tx_vq, 1);
1253 if (virtio_net_flush_tx(q) > 0) {
1254 virtio_queue_set_notification(q->tx_vq, 0);
1255 qemu_bh_schedule(q->tx_bh);
1260 static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue)
1262 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1263 int i, max = multiqueue ? n->max_queues : 1;
1265 n->multiqueue = multiqueue;
1267 for (i = 2; i <= n->max_queues * 2 + 1; i++) {
1268 virtio_del_queue(vdev, i);
1271 for (i = 1; i < max; i++) {
1272 n->vqs[i].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx);
1273 if (n->vqs[i].tx_timer) {
1275 virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer);
1276 n->vqs[i].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
1277 virtio_net_tx_timer,
1281 virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh);
1282 n->vqs[i].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[i]);
1285 n->vqs[i].tx_waiting = 0;
1289 /* Note: Minux Guests (version 3.2.1) use ctrl vq but don't ack
1290 * VIRTIO_NET_F_CTRL_VQ. Create ctrl vq unconditionally to avoid
1293 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
1295 virtio_net_set_queues(n);
1298 static void virtio_net_save(QEMUFile *f, void *opaque)
1301 VirtIONet *n = opaque;
1302 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1304 /* At this point, backend must be stopped, otherwise
1305 * it might keep writing to memory. */
1306 assert(!n->vhost_started);
1307 virtio_save(vdev, f);
1309 qemu_put_buffer(f, n->mac, ETH_ALEN);
1310 qemu_put_be32(f, n->vqs[0].tx_waiting);
1311 qemu_put_be32(f, n->mergeable_rx_bufs);
1312 qemu_put_be16(f, n->status);
1313 qemu_put_byte(f, n->promisc);
1314 qemu_put_byte(f, n->allmulti);
1315 qemu_put_be32(f, n->mac_table.in_use);
1316 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
1317 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
1318 qemu_put_be32(f, n->has_vnet_hdr);
1319 qemu_put_byte(f, n->mac_table.multi_overflow);
1320 qemu_put_byte(f, n->mac_table.uni_overflow);
1321 qemu_put_byte(f, n->alluni);
1322 qemu_put_byte(f, n->nomulti);
1323 qemu_put_byte(f, n->nouni);
1324 qemu_put_byte(f, n->nobcast);
1325 qemu_put_byte(f, n->has_ufo);
1326 if (n->max_queues > 1) {
1327 qemu_put_be16(f, n->max_queues);
1328 qemu_put_be16(f, n->curr_queues);
1329 for (i = 1; i < n->curr_queues; i++) {
1330 qemu_put_be32(f, n->vqs[i].tx_waiting);
1334 if ((1 << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) & vdev->guest_features) {
1335 qemu_put_be64(f, n->curr_guest_offloads);
1339 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
1341 VirtIONet *n = opaque;
1342 VirtIODevice *vdev = VIRTIO_DEVICE(n);
1343 int ret, i, link_down;
1345 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
1348 ret = virtio_load(vdev, f);
1353 qemu_get_buffer(f, n->mac, ETH_ALEN);
1354 n->vqs[0].tx_waiting = qemu_get_be32(f);
1356 virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f));
1358 if (version_id >= 3)
1359 n->status = qemu_get_be16(f);
1361 if (version_id >= 4) {
1362 if (version_id < 8) {
1363 n->promisc = qemu_get_be32(f);
1364 n->allmulti = qemu_get_be32(f);
1366 n->promisc = qemu_get_byte(f);
1367 n->allmulti = qemu_get_byte(f);
1371 if (version_id >= 5) {
1372 n->mac_table.in_use = qemu_get_be32(f);
1373 /* MAC_TABLE_ENTRIES may be different from the saved image */
1374 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
1375 qemu_get_buffer(f, n->mac_table.macs,
1376 n->mac_table.in_use * ETH_ALEN);
1380 /* Overflow detected - can happen if source has a larger MAC table.
1381 * We simply set overflow flag so there's no need to maintain the
1382 * table of addresses, discard them all.
1383 * Note: 64 bit math to avoid integer overflow.
1385 for (i = 0; i < (int64_t)n->mac_table.in_use * ETH_ALEN; ++i) {
1388 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
1389 n->mac_table.in_use = 0;
1393 if (version_id >= 6)
1394 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
1396 if (version_id >= 7) {
1397 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
1398 error_report("virtio-net: saved image requires vnet_hdr=on");
1403 if (version_id >= 9) {
1404 n->mac_table.multi_overflow = qemu_get_byte(f);
1405 n->mac_table.uni_overflow = qemu_get_byte(f);
1408 if (version_id >= 10) {
1409 n->alluni = qemu_get_byte(f);
1410 n->nomulti = qemu_get_byte(f);
1411 n->nouni = qemu_get_byte(f);
1412 n->nobcast = qemu_get_byte(f);
1415 if (version_id >= 11) {
1416 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
1417 error_report("virtio-net: saved image requires TUN_F_UFO support");
1422 if (n->max_queues > 1) {
1423 if (n->max_queues != qemu_get_be16(f)) {
1424 error_report("virtio-net: different max_queues ");
1428 n->curr_queues = qemu_get_be16(f);
1429 if (n->curr_queues > n->max_queues) {
1430 error_report("virtio-net: curr_queues %x > max_queues %x",
1431 n->curr_queues, n->max_queues);
1434 for (i = 1; i < n->curr_queues; i++) {
1435 n->vqs[i].tx_waiting = qemu_get_be32(f);
1439 if ((1 << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) & vdev->guest_features) {
1440 n->curr_guest_offloads = qemu_get_be64(f);
1442 n->curr_guest_offloads = virtio_net_supported_guest_offloads(n);
1445 if (peer_has_vnet_hdr(n)) {
1446 virtio_net_apply_guest_offloads(n);
1449 virtio_net_set_queues(n);
1451 /* Find the first multicast entry in the saved MAC filter */
1452 for (i = 0; i < n->mac_table.in_use; i++) {
1453 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
1457 n->mac_table.first_multi = i;
1459 /* nc.link_down can't be migrated, so infer link_down according
1460 * to link status bit in n->status */
1461 link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
1462 for (i = 0; i < n->max_queues; i++) {
1463 qemu_get_subqueue(n->nic, i)->link_down = link_down;
1466 if (vdev->guest_features & (0x1 << VIRTIO_NET_F_GUEST_ANNOUNCE) &&
1467 vdev->guest_features & (0x1 << VIRTIO_NET_F_CTRL_VQ)) {
1468 n->announce_counter = SELF_ANNOUNCE_ROUNDS;
1469 timer_mod(n->announce_timer, qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL));
1475 static void virtio_net_cleanup(NetClientState *nc)
1477 VirtIONet *n = qemu_get_nic_opaque(nc);
1482 static NetClientInfo net_virtio_info = {
1483 .type = NET_CLIENT_OPTIONS_KIND_NIC,
1484 .size = sizeof(NICState),
1485 .can_receive = virtio_net_can_receive,
1486 .receive = virtio_net_receive,
1487 .cleanup = virtio_net_cleanup,
1488 .link_status_changed = virtio_net_set_link_status,
1489 .query_rx_filter = virtio_net_query_rxfilter,
1492 static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
1494 VirtIONet *n = VIRTIO_NET(vdev);
1495 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
1496 assert(n->vhost_started);
1497 return vhost_net_virtqueue_pending(get_vhost_net(nc->peer), idx);
1500 static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
1503 VirtIONet *n = VIRTIO_NET(vdev);
1504 NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx));
1505 assert(n->vhost_started);
1506 vhost_net_virtqueue_mask(get_vhost_net(nc->peer),
1510 void virtio_net_set_config_size(VirtIONet *n, uint32_t host_features)
1512 int i, config_size = 0;
1513 host_features |= (1 << VIRTIO_NET_F_MAC);
1514 for (i = 0; feature_sizes[i].flags != 0; i++) {
1515 if (host_features & feature_sizes[i].flags) {
1516 config_size = MAX(feature_sizes[i].end, config_size);
1519 n->config_size = config_size;
1522 void virtio_net_set_netclient_name(VirtIONet *n, const char *name,
1526 * The name can be NULL, the netclient name will be type.x.
1528 assert(type != NULL);
1530 g_free(n->netclient_name);
1531 g_free(n->netclient_type);
1532 n->netclient_name = g_strdup(name);
1533 n->netclient_type = g_strdup(type);
1536 static void virtio_net_device_realize(DeviceState *dev, Error **errp)
1538 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1539 VirtIONet *n = VIRTIO_NET(dev);
1543 virtio_init(vdev, "virtio-net", VIRTIO_ID_NET, n->config_size);
1545 n->max_queues = MAX(n->nic_conf.peers.queues, 1);
1546 n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues);
1547 n->vqs[0].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx);
1550 n->tx_timeout = n->net_conf.txtimer;
1552 if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer")
1553 && strcmp(n->net_conf.tx, "bh")) {
1554 error_report("virtio-net: "
1555 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1557 error_report("Defaulting to \"bh\"");
1560 if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) {
1561 n->vqs[0].tx_vq = virtio_add_queue(vdev, 256,
1562 virtio_net_handle_tx_timer);
1563 n->vqs[0].tx_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, virtio_net_tx_timer,
1566 n->vqs[0].tx_vq = virtio_add_queue(vdev, 256,
1567 virtio_net_handle_tx_bh);
1568 n->vqs[0].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[0]);
1570 n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl);
1571 qemu_macaddr_default_if_unset(&n->nic_conf.macaddr);
1572 memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac));
1573 n->status = VIRTIO_NET_S_LINK_UP;
1574 n->announce_timer = timer_new_ms(QEMU_CLOCK_VIRTUAL,
1575 virtio_net_announce_timer, n);
1577 if (n->netclient_type) {
1579 * Happen when virtio_net_set_netclient_name has been called.
1581 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
1582 n->netclient_type, n->netclient_name, n);
1584 n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf,
1585 object_get_typename(OBJECT(dev)), dev->id, n);
1588 peer_test_vnet_hdr(n);
1589 if (peer_has_vnet_hdr(n)) {
1590 for (i = 0; i < n->max_queues; i++) {
1591 qemu_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true);
1593 n->host_hdr_len = sizeof(struct virtio_net_hdr);
1595 n->host_hdr_len = 0;
1598 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a);
1600 n->vqs[0].tx_waiting = 0;
1601 n->tx_burst = n->net_conf.txburst;
1602 virtio_net_set_mrg_rx_bufs(n, 0);
1603 n->promisc = 1; /* for compatibility */
1605 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
1607 n->vlans = g_malloc0(MAX_VLAN >> 3);
1609 nc = qemu_get_queue(n->nic);
1610 nc->rxfilter_notify_enabled = 1;
1613 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
1614 virtio_net_save, virtio_net_load, n);
1616 add_boot_device_path(n->nic_conf.bootindex, dev, "/ethernet-phy@0");
1619 static void virtio_net_device_unrealize(DeviceState *dev, Error **errp)
1621 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
1622 VirtIONet *n = VIRTIO_NET(dev);
1625 /* This will stop vhost backend if appropriate. */
1626 virtio_net_set_status(vdev, 0);
1628 unregister_savevm(dev, "virtio-net", n);
1630 g_free(n->netclient_name);
1631 n->netclient_name = NULL;
1632 g_free(n->netclient_type);
1633 n->netclient_type = NULL;
1635 g_free(n->mac_table.macs);
1638 for (i = 0; i < n->max_queues; i++) {
1639 VirtIONetQueue *q = &n->vqs[i];
1640 NetClientState *nc = qemu_get_subqueue(n->nic, i);
1642 qemu_purge_queued_packets(nc);
1645 timer_del(q->tx_timer);
1646 timer_free(q->tx_timer);
1647 } else if (q->tx_bh) {
1648 qemu_bh_delete(q->tx_bh);
1652 timer_del(n->announce_timer);
1653 timer_free(n->announce_timer);
1655 qemu_del_nic(n->nic);
1656 virtio_cleanup(vdev);
1659 static void virtio_net_instance_init(Object *obj)
1661 VirtIONet *n = VIRTIO_NET(obj);
1664 * The default config_size is sizeof(struct virtio_net_config).
1665 * Can be overriden with virtio_net_set_config_size.
1667 n->config_size = sizeof(struct virtio_net_config);
1670 static Property virtio_net_properties[] = {
1671 DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf),
1672 DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer,
1674 DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST),
1675 DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx),
1676 DEFINE_PROP_END_OF_LIST(),
1679 static void virtio_net_class_init(ObjectClass *klass, void *data)
1681 DeviceClass *dc = DEVICE_CLASS(klass);
1682 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1684 dc->props = virtio_net_properties;
1685 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories);
1686 vdc->realize = virtio_net_device_realize;
1687 vdc->unrealize = virtio_net_device_unrealize;
1688 vdc->get_config = virtio_net_get_config;
1689 vdc->set_config = virtio_net_set_config;
1690 vdc->get_features = virtio_net_get_features;
1691 vdc->set_features = virtio_net_set_features;
1692 vdc->bad_features = virtio_net_bad_features;
1693 vdc->reset = virtio_net_reset;
1694 vdc->set_status = virtio_net_set_status;
1695 vdc->guest_notifier_mask = virtio_net_guest_notifier_mask;
1696 vdc->guest_notifier_pending = virtio_net_guest_notifier_pending;
1699 static const TypeInfo virtio_net_info = {
1700 .name = TYPE_VIRTIO_NET,
1701 .parent = TYPE_VIRTIO_DEVICE,
1702 .instance_size = sizeof(VirtIONet),
1703 .instance_init = virtio_net_instance_init,
1704 .class_init = virtio_net_class_init,
1707 static void virtio_register_types(void)
1709 type_register_static(&virtio_net_info);
1712 type_init(virtio_register_types)