blob: f4146aafc832c81019155332b1dd655b7c47bea0 [file] [log] [blame]
aliguorifbe78f42008-12-17 19:13:11 +00001/*
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
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010014#include "qemu/iov.h"
aliguorifbe78f42008-12-17 19:13:11 +000015#include "virtio.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020016#include "net/net.h"
Mark McLoughlin7200ac32009-10-22 17:49:03 +010017#include "net/checksum.h"
Mark McLoughlina8ed73f2009-10-22 17:49:05 +010018#include "net/tap.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010019#include "qemu/error-report.h"
20#include "qemu/timer.h"
aliguorifbe78f42008-12-17 19:13:11 +000021#include "virtio-net.h"
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +020022#include "vhost_net.h"
aliguorifbe78f42008-12-17 19:13:11 +000023
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010024#define VIRTIO_NET_VM_VERSION 11
aliguorib6503ed2009-02-05 22:36:28 +000025
Alex Williamson4ffb17f2009-06-05 14:47:23 -060026#define MAC_TABLE_ENTRIES 64
aliguorif21c0ed2009-02-05 22:36:32 +000027#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
aliguori9d6271b2009-02-05 22:36:04 +000028
aliguorifbe78f42008-12-17 19:13:11 +000029typedef struct VirtIONet
30{
31 VirtIODevice vdev;
aliguori79674062009-02-05 22:36:12 +000032 uint8_t mac[ETH_ALEN];
aliguori554c97d2009-01-08 19:46:33 +000033 uint16_t status;
aliguorifbe78f42008-12-17 19:13:11 +000034 VirtQueue *rx_vq;
35 VirtQueue *tx_vq;
aliguori3d11d362009-02-05 22:36:16 +000036 VirtQueue *ctrl_vq;
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000037 NICState *nic;
aliguorifbe78f42008-12-17 19:13:11 +000038 QEMUTimer *tx_timer;
Alex Williamsona697a332010-09-02 09:01:10 -060039 QEMUBH *tx_bh;
Alex Williamsonf0c07c72010-09-02 09:00:50 -060040 uint32_t tx_timeout;
Alex Williamsone3f30482010-09-02 09:00:57 -060041 int32_t tx_burst;
Alex Williamson4b4b8d32010-09-02 09:01:04 -060042 int tx_waiting;
Mark McLoughlin3a330132009-10-22 17:43:45 +010043 uint32_t has_vnet_hdr;
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +020044 size_t host_hdr_len;
45 size_t guest_hdr_len;
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010046 uint8_t has_ufo;
Mark McLoughlin62433752009-06-18 18:21:36 +010047 struct {
48 VirtQueueElement elem;
49 ssize_t len;
50 } async_tx;
aliguorifbe78f42008-12-17 19:13:11 +000051 int mergeable_rx_bufs;
Alex Williamsonf10c5922009-06-05 14:46:57 -060052 uint8_t promisc;
53 uint8_t allmulti;
Alex Williamson015cb162009-06-05 14:47:18 -060054 uint8_t alluni;
55 uint8_t nomulti;
56 uint8_t nouni;
57 uint8_t nobcast;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +020058 uint8_t vhost_started;
aliguorib6503ed2009-02-05 22:36:28 +000059 struct {
60 int in_use;
Alex Williamson2d9aba32009-06-05 14:47:13 -060061 int first_multi;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -060062 uint8_t multi_overflow;
63 uint8_t uni_overflow;
aliguorib6503ed2009-02-05 22:36:28 +000064 uint8_t *macs;
65 } mac_table;
aliguorif21c0ed2009-02-05 22:36:32 +000066 uint32_t *vlans;
Alex Williamson01657c82010-06-25 11:09:28 -060067 DeviceState *qdev;
aliguorifbe78f42008-12-17 19:13:11 +000068} VirtIONet;
69
70/* TODO
71 * - we could suppress RX interrupt if we were so inclined.
72 */
73
74static VirtIONet *to_virtio_net(VirtIODevice *vdev)
75{
76 return (VirtIONet *)vdev;
77}
78
aliguori0f03eca2009-02-05 22:36:08 +000079static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
aliguorifbe78f42008-12-17 19:13:11 +000080{
81 VirtIONet *n = to_virtio_net(vdev);
82 struct virtio_net_config netcfg;
83
Stefan Hajnoczib46d97f2011-03-03 21:42:28 +000084 stw_p(&netcfg.status, n->status);
aliguori79674062009-02-05 22:36:12 +000085 memcpy(netcfg.mac, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +000086 memcpy(config, &netcfg, sizeof(netcfg));
87}
88
aliguori0f03eca2009-02-05 22:36:08 +000089static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
90{
91 VirtIONet *n = to_virtio_net(vdev);
92 struct virtio_net_config netcfg;
93
94 memcpy(&netcfg, config, sizeof(netcfg));
95
Amos Kongc1943a32013-01-22 23:44:45 +080096 if (!(n->vdev.guest_features >> VIRTIO_NET_F_CTRL_MAC_ADDR & 1) &&
97 memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
aliguori79674062009-02-05 22:36:12 +000098 memcpy(n->mac, netcfg.mac, ETH_ALEN);
Jason Wangb356f762013-01-30 19:12:22 +080099 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
aliguori0f03eca2009-02-05 22:36:08 +0000100 }
101}
102
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200103static bool virtio_net_started(VirtIONet *n, uint8_t status)
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200104{
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200105 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200106 (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running;
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200107}
108
109static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
110{
Jason Wangb356f762013-01-30 19:12:22 +0800111 NetClientState *nc = qemu_get_queue(n->nic);
112
113 if (!nc->peer) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200114 return;
115 }
Jason Wangb356f762013-01-30 19:12:22 +0800116 if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200117 return;
118 }
119
Jason Wangb356f762013-01-30 19:12:22 +0800120 if (!tap_get_vhost_net(nc->peer)) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200121 return;
122 }
Michael S. Tsirkin32993692011-02-09 18:45:09 +0200123 if (!!n->vhost_started == virtio_net_started(n, status) &&
Jason Wangb356f762013-01-30 19:12:22 +0800124 !nc->peer->link_down) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200125 return;
126 }
127 if (!n->vhost_started) {
mst@redhat.com5430a282011-02-01 22:13:42 +0200128 int r;
Jason Wangb356f762013-01-30 19:12:22 +0800129 if (!vhost_net_query(tap_get_vhost_net(nc->peer), &n->vdev)) {
mst@redhat.com5430a282011-02-01 22:13:42 +0200130 return;
131 }
Michael S. Tsirkin1830b802012-12-25 17:38:59 +0200132 n->vhost_started = 1;
Jason Wanga9f98bb2013-01-30 19:12:35 +0800133 r = vhost_net_start(&n->vdev, nc, 1);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200134 if (r < 0) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000135 error_report("unable to start vhost net: %d: "
136 "falling back on userspace virtio", -r);
Michael S. Tsirkin1830b802012-12-25 17:38:59 +0200137 n->vhost_started = 0;
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200138 }
139 } else {
Jason Wanga9f98bb2013-01-30 19:12:35 +0800140 vhost_net_stop(&n->vdev, nc, 1);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200141 n->vhost_started = 0;
142 }
143}
144
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200145static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
146{
147 VirtIONet *n = to_virtio_net(vdev);
148
149 virtio_net_vhost_status(n, status);
150
151 if (!n->tx_waiting) {
152 return;
153 }
154
155 if (virtio_net_started(n, status) && !n->vhost_started) {
156 if (n->tx_timer) {
157 qemu_mod_timer(n->tx_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100158 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200159 } else {
160 qemu_bh_schedule(n->tx_bh);
161 }
162 } else {
163 if (n->tx_timer) {
164 qemu_del_timer(n->tx_timer);
165 } else {
166 qemu_bh_cancel(n->tx_bh);
167 }
168 }
169}
170
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100171static void virtio_net_set_link_status(NetClientState *nc)
aliguori554c97d2009-01-08 19:46:33 +0000172{
Jason Wangcc1f0f42013-01-30 19:12:23 +0800173 VirtIONet *n = qemu_get_nic_opaque(nc);
aliguori554c97d2009-01-08 19:46:33 +0000174 uint16_t old_status = n->status;
175
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000176 if (nc->link_down)
aliguori554c97d2009-01-08 19:46:33 +0000177 n->status &= ~VIRTIO_NET_S_LINK_UP;
178 else
179 n->status |= VIRTIO_NET_S_LINK_UP;
180
181 if (n->status != old_status)
182 virtio_notify_config(&n->vdev);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200183
184 virtio_net_set_status(&n->vdev, n->vdev.status);
aliguori554c97d2009-01-08 19:46:33 +0000185}
186
aliguori002437c2009-02-05 22:36:20 +0000187static void virtio_net_reset(VirtIODevice *vdev)
188{
189 VirtIONet *n = to_virtio_net(vdev);
190
191 /* Reset back to compatibility mode */
192 n->promisc = 1;
193 n->allmulti = 0;
Alex Williamson015cb162009-06-05 14:47:18 -0600194 n->alluni = 0;
195 n->nomulti = 0;
196 n->nouni = 0;
197 n->nobcast = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000198
aliguorif21c0ed2009-02-05 22:36:32 +0000199 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000200 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600201 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600202 n->mac_table.multi_overflow = 0;
203 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000204 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
Michael S. Tsirkin41dc8a62013-01-16 11:37:40 +0200205 memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
aliguorif21c0ed2009-02-05 22:36:32 +0000206 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000207}
208
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200209static void peer_test_vnet_hdr(VirtIONet *n)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100210{
Jason Wangb356f762013-01-30 19:12:22 +0800211 NetClientState *nc = qemu_get_queue(n->nic);
212 if (!nc->peer) {
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200213 return;
Jason Wangb356f762013-01-30 19:12:22 +0800214 }
Mark McLoughlin3a330132009-10-22 17:43:45 +0100215
Jason Wangb356f762013-01-30 19:12:22 +0800216 if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200217 return;
Jason Wangb356f762013-01-30 19:12:22 +0800218 }
Mark McLoughlin3a330132009-10-22 17:43:45 +0100219
Jason Wangb356f762013-01-30 19:12:22 +0800220 n->has_vnet_hdr = tap_has_vnet_hdr(nc->peer);
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200221}
Mark McLoughlin3a330132009-10-22 17:43:45 +0100222
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200223static int peer_has_vnet_hdr(VirtIONet *n)
224{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100225 return n->has_vnet_hdr;
226}
227
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100228static int peer_has_ufo(VirtIONet *n)
229{
230 if (!peer_has_vnet_hdr(n))
231 return 0;
232
Jason Wangb356f762013-01-30 19:12:22 +0800233 n->has_ufo = tap_has_ufo(qemu_get_queue(n->nic)->peer);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100234
235 return n->has_ufo;
236}
237
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +0200238static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs)
239{
240 n->mergeable_rx_bufs = mergeable_rx_bufs;
241
242 n->guest_hdr_len = n->mergeable_rx_bufs ?
243 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
244
245 if (peer_has_vnet_hdr(n) &&
Jason Wangb356f762013-01-30 19:12:22 +0800246 tap_has_vnet_hdr_len(qemu_get_queue(n->nic)->peer, n->guest_hdr_len)) {
247 tap_set_vnet_hdr_len(qemu_get_queue(n->nic)->peer, n->guest_hdr_len);
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +0200248 n->host_hdr_len = n->guest_hdr_len;
249 }
250}
251
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200252static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
aliguorifbe78f42008-12-17 19:13:11 +0000253{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100254 VirtIONet *n = to_virtio_net(vdev);
Jason Wangb356f762013-01-30 19:12:22 +0800255 NetClientState *nc = qemu_get_queue(n->nic);
aliguorifbe78f42008-12-17 19:13:11 +0000256
Michael S. Tsirkinc9f79a32010-01-12 20:50:17 +0200257 features |= (1 << VIRTIO_NET_F_MAC);
258
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200259 if (!peer_has_vnet_hdr(n)) {
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200260 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
261 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
262 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
263 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100264
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200265 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
266 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
267 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
268 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
269 }
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100270
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200271 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
272 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
273 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100274 }
275
Jason Wangb356f762013-01-30 19:12:22 +0800276 if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200277 return features;
278 }
Jason Wangb356f762013-01-30 19:12:22 +0800279 if (!tap_get_vhost_net(nc->peer)) {
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200280 return features;
281 }
Jason Wangb356f762013-01-30 19:12:22 +0800282 return vhost_net_get_features(tap_get_vhost_net(nc->peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000283}
284
aliguori8eca6b12009-04-05 17:40:08 +0000285static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
286{
287 uint32_t features = 0;
288
289 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
290 * but also these: */
291 features |= (1 << VIRTIO_NET_F_MAC);
Dustin Kirkland184bd042009-10-29 10:34:15 -0500292 features |= (1 << VIRTIO_NET_F_CSUM);
293 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
294 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
295 features |= (1 << VIRTIO_NET_F_HOST_ECN);
aliguori8eca6b12009-04-05 17:40:08 +0000296
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200297 return features;
aliguori8eca6b12009-04-05 17:40:08 +0000298}
299
aliguorifbe78f42008-12-17 19:13:11 +0000300static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
301{
302 VirtIONet *n = to_virtio_net(vdev);
Jason Wangb356f762013-01-30 19:12:22 +0800303 NetClientState *nc = qemu_get_queue(n->nic);
aliguorifbe78f42008-12-17 19:13:11 +0000304
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +0200305 virtio_net_set_mrg_rx_bufs(n, !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF)));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100306
307 if (n->has_vnet_hdr) {
Jason Wangb356f762013-01-30 19:12:22 +0800308 tap_set_offload(nc->peer,
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100309 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
310 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
311 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100312 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
313 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100314 }
Jason Wangb356f762013-01-30 19:12:22 +0800315 if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
David L Stevensdc14a392010-03-31 21:20:31 +0300316 return;
317 }
Jason Wangb356f762013-01-30 19:12:22 +0800318 if (!tap_get_vhost_net(nc->peer)) {
David L Stevensdc14a392010-03-31 21:20:31 +0300319 return;
320 }
Jason Wangb356f762013-01-30 19:12:22 +0800321 vhost_net_ack_features(tap_get_vhost_net(nc->peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000322}
323
aliguori002437c2009-02-05 22:36:20 +0000324static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800325 struct iovec *iov, unsigned int iov_cnt)
aliguori002437c2009-02-05 22:36:20 +0000326{
327 uint8_t on;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800328 size_t s;
aliguori002437c2009-02-05 22:36:20 +0000329
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800330 s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
331 if (s != sizeof(on)) {
332 return VIRTIO_NET_ERR;
aliguori002437c2009-02-05 22:36:20 +0000333 }
334
Amos Kongdd234542013-01-22 23:44:46 +0800335 if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
aliguori002437c2009-02-05 22:36:20 +0000336 n->promisc = on;
Amos Kongdd234542013-01-22 23:44:46 +0800337 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
aliguori002437c2009-02-05 22:36:20 +0000338 n->allmulti = on;
Amos Kongdd234542013-01-22 23:44:46 +0800339 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
Alex Williamson015cb162009-06-05 14:47:18 -0600340 n->alluni = on;
Amos Kongdd234542013-01-22 23:44:46 +0800341 } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
Alex Williamson015cb162009-06-05 14:47:18 -0600342 n->nomulti = on;
Amos Kongdd234542013-01-22 23:44:46 +0800343 } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
Alex Williamson015cb162009-06-05 14:47:18 -0600344 n->nouni = on;
Amos Kongdd234542013-01-22 23:44:46 +0800345 } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
Alex Williamson015cb162009-06-05 14:47:18 -0600346 n->nobcast = on;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800347 } else {
aliguori002437c2009-02-05 22:36:20 +0000348 return VIRTIO_NET_ERR;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800349 }
aliguori002437c2009-02-05 22:36:20 +0000350
351 return VIRTIO_NET_OK;
352}
353
aliguorib6503ed2009-02-05 22:36:28 +0000354static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800355 struct iovec *iov, unsigned int iov_cnt)
aliguorib6503ed2009-02-05 22:36:28 +0000356{
357 struct virtio_net_ctrl_mac mac_data;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800358 size_t s;
aliguorib6503ed2009-02-05 22:36:28 +0000359
Amos Kongc1943a32013-01-22 23:44:45 +0800360 if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
361 if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
362 return VIRTIO_NET_ERR;
363 }
364 s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
365 assert(s == sizeof(n->mac));
Jason Wangb356f762013-01-30 19:12:22 +0800366 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
Amos Kongc1943a32013-01-22 23:44:45 +0800367 return VIRTIO_NET_OK;
368 }
369
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800370 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
aliguorib6503ed2009-02-05 22:36:28 +0000371 return VIRTIO_NET_ERR;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800372 }
aliguorib6503ed2009-02-05 22:36:28 +0000373
374 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600375 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600376 n->mac_table.uni_overflow = 0;
377 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000378 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
379
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800380 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
381 sizeof(mac_data.entries));
382 mac_data.entries = ldl_p(&mac_data.entries);
383 if (s != sizeof(mac_data.entries)) {
aliguorib6503ed2009-02-05 22:36:28 +0000384 return VIRTIO_NET_ERR;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800385 }
386 iov_discard_front(&iov, &iov_cnt, s);
387
388 if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
389 return VIRTIO_NET_ERR;
390 }
aliguorib6503ed2009-02-05 22:36:28 +0000391
392 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800393 s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs,
394 mac_data.entries * ETH_ALEN);
395 if (s != mac_data.entries * ETH_ALEN) {
396 return VIRTIO_NET_ERR;
397 }
aliguorib6503ed2009-02-05 22:36:28 +0000398 n->mac_table.in_use += mac_data.entries;
399 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600400 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000401 }
402
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800403 iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
404
Alex Williamson2d9aba32009-06-05 14:47:13 -0600405 n->mac_table.first_multi = n->mac_table.in_use;
406
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800407 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
408 sizeof(mac_data.entries));
409 mac_data.entries = ldl_p(&mac_data.entries);
410 if (s != sizeof(mac_data.entries)) {
aliguorib6503ed2009-02-05 22:36:28 +0000411 return VIRTIO_NET_ERR;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800412 }
aliguorib6503ed2009-02-05 22:36:28 +0000413
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800414 iov_discard_front(&iov, &iov_cnt, s);
415
416 if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
417 return VIRTIO_NET_ERR;
418 }
419
420 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
421 s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs,
422 mac_data.entries * ETH_ALEN);
423 if (s != mac_data.entries * ETH_ALEN) {
424 return VIRTIO_NET_ERR;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600425 }
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800426 n->mac_table.in_use += mac_data.entries;
427 } else {
428 n->mac_table.multi_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000429 }
430
431 return VIRTIO_NET_OK;
432}
433
aliguorif21c0ed2009-02-05 22:36:32 +0000434static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800435 struct iovec *iov, unsigned int iov_cnt)
aliguorif21c0ed2009-02-05 22:36:32 +0000436{
437 uint16_t vid;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800438 size_t s;
aliguorif21c0ed2009-02-05 22:36:32 +0000439
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800440 s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
441 vid = lduw_p(&vid);
442 if (s != sizeof(vid)) {
aliguorif21c0ed2009-02-05 22:36:32 +0000443 return VIRTIO_NET_ERR;
444 }
445
aliguorif21c0ed2009-02-05 22:36:32 +0000446 if (vid >= MAX_VLAN)
447 return VIRTIO_NET_ERR;
448
449 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
450 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
451 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
452 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
453 else
454 return VIRTIO_NET_ERR;
455
456 return VIRTIO_NET_OK;
457}
458
aliguori3d11d362009-02-05 22:36:16 +0000459static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
460{
aliguori002437c2009-02-05 22:36:20 +0000461 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000462 struct virtio_net_ctrl_hdr ctrl;
463 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
464 VirtQueueElement elem;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800465 size_t s;
466 struct iovec *iov;
467 unsigned int iov_cnt;
aliguori3d11d362009-02-05 22:36:16 +0000468
469 while (virtqueue_pop(vq, &elem)) {
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800470 if (iov_size(elem.in_sg, elem.in_num) < sizeof(status) ||
471 iov_size(elem.out_sg, elem.out_num) < sizeof(ctrl)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000472 error_report("virtio-net ctrl missing headers");
aliguori3d11d362009-02-05 22:36:16 +0000473 exit(1);
474 }
475
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800476 iov = elem.out_sg;
477 iov_cnt = elem.out_num;
478 s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
479 iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
480 if (s != sizeof(ctrl)) {
481 status = VIRTIO_NET_ERR;
Amos Kongdd234542013-01-22 23:44:46 +0800482 } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800483 status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
484 } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
485 status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
486 } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
487 status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
aliguori3d11d362009-02-05 22:36:16 +0000488 }
489
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800490 s = iov_from_buf(elem.in_sg, elem.in_num, 0, &status, sizeof(status));
491 assert(s == sizeof(status));
aliguori3d11d362009-02-05 22:36:16 +0000492
493 virtqueue_push(vq, &elem, sizeof(status));
494 virtio_notify(vdev, vq);
495 }
496}
497
aliguorifbe78f42008-12-17 19:13:11 +0000498/* RX */
499
500static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
501{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100502 VirtIONet *n = to_virtio_net(vdev);
503
Jason Wangb356f762013-01-30 19:12:22 +0800504 qemu_flush_queued_packets(qemu_get_queue(n->nic));
aliguorifbe78f42008-12-17 19:13:11 +0000505}
506
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100507static int virtio_net_can_receive(NetClientState *nc)
aliguorifbe78f42008-12-17 19:13:11 +0000508{
Jason Wangcc1f0f42013-01-30 19:12:23 +0800509 VirtIONet *n = qemu_get_nic_opaque(nc);
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200510 if (!n->vdev.vm_running) {
Michael S. Tsirkin95477322010-11-22 19:52:19 +0200511 return 0;
512 }
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000513
aliguorifbe78f42008-12-17 19:13:11 +0000514 if (!virtio_queue_ready(n->rx_vq) ||
515 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
516 return 0;
517
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000518 return 1;
519}
520
521static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
522{
aliguorifbe78f42008-12-17 19:13:11 +0000523 if (virtio_queue_empty(n->rx_vq) ||
524 (n->mergeable_rx_bufs &&
525 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
526 virtio_queue_set_notification(n->rx_vq, 1);
Tom Lendacky06b12972010-02-08 10:10:01 -0600527
528 /* To avoid a race condition where the guest has made some buffers
529 * available after the above check but before notification was
530 * enabled, check for available buffers again.
531 */
532 if (virtio_queue_empty(n->rx_vq) ||
533 (n->mergeable_rx_bufs &&
534 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
535 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000536 }
537
538 virtio_queue_set_notification(n->rx_vq, 0);
539 return 1;
540}
541
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100542/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
543 * it never finds out that the packets don't have valid checksums. This
544 * causes dhclient to get upset. Fedora's carried a patch for ages to
545 * fix this with Xen but it hasn't appeared in an upstream release of
546 * dhclient yet.
547 *
548 * To avoid breaking existing guests, we catch udp packets and add
549 * checksums. This is terrible but it's better than hacking the guest
550 * kernels.
551 *
552 * N.B. if we introduce a zero-copy API, this operation is no longer free so
553 * we should provide a mechanism to disable it to avoid polluting the host
554 * cache.
555 */
556static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200557 uint8_t *buf, size_t size)
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100558{
559 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
560 (size > 27 && size < 1500) && /* normal sized MTU */
561 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
562 (buf[23] == 17) && /* ip.protocol == UDP */
563 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200564 net_checksum_calculate(buf, size);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100565 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
566 }
567}
568
Michael S. Tsirkin280598b2012-09-24 13:24:17 +0200569static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
570 const void *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000571{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100572 if (n->has_vnet_hdr) {
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200573 /* FIXME this cast is evil */
574 void *wbuf = (void *)buf;
Michael S. Tsirkin280598b2012-09-24 13:24:17 +0200575 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
576 size - n->host_hdr_len);
577 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200578 } else {
579 struct virtio_net_hdr hdr = {
580 .flags = 0,
581 .gso_type = VIRTIO_NET_HDR_GSO_NONE
582 };
583 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100584 }
aliguorifbe78f42008-12-17 19:13:11 +0000585}
586
aliguori3831ab22009-02-05 22:36:24 +0000587static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
588{
589 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000590 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000591 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000592 int i;
aliguori3831ab22009-02-05 22:36:24 +0000593
594 if (n->promisc)
595 return 1;
596
Michael S. Tsirkine043ebc2012-09-24 16:27:27 +0200597 ptr += n->host_hdr_len;
Mark McLoughlin3a330132009-10-22 17:43:45 +0100598
aliguorif21c0ed2009-02-05 22:36:32 +0000599 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
600 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
601 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
602 return 0;
603 }
604
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600605 if (ptr[0] & 1) { // multicast
606 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600607 return !n->nobcast;
608 } else if (n->nomulti) {
609 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600610 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600611 return 1;
612 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600613
614 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
615 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
616 return 1;
617 }
618 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600619 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600620 if (n->nouni) {
621 return 0;
622 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600623 return 1;
624 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600625 return 1;
626 }
aliguori3831ab22009-02-05 22:36:24 +0000627
Alex Williamson2d9aba32009-06-05 14:47:13 -0600628 for (i = 0; i < n->mac_table.first_multi; i++) {
629 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
630 return 1;
631 }
632 }
aliguorib6503ed2009-02-05 22:36:28 +0000633 }
634
aliguori3831ab22009-02-05 22:36:24 +0000635 return 0;
636}
637
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100638static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000639{
Jason Wangcc1f0f42013-01-30 19:12:23 +0800640 VirtIONet *n = qemu_get_nic_opaque(nc);
Michael S. Tsirkin63c58722012-09-24 13:17:13 +0200641 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
642 struct virtio_net_hdr_mrg_rxbuf mhdr;
643 unsigned mhdr_cnt = 0;
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200644 size_t offset, i, guest_offset;
aliguorifbe78f42008-12-17 19:13:11 +0000645
Jason Wangb356f762013-01-30 19:12:22 +0800646 if (!virtio_net_can_receive(qemu_get_queue(n->nic))) {
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000647 return -1;
Jason Wangb356f762013-01-30 19:12:22 +0800648 }
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000649
Michael S. Tsirkin940cda92010-06-06 18:53:10 +0300650 /* hdr_len refers to the header we supply to the guest */
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200651 if (!virtio_net_has_buffers(n, size + n->guest_hdr_len - n->host_hdr_len))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100652 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000653
aliguori3831ab22009-02-05 22:36:24 +0000654 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100655 return size;
aliguori3831ab22009-02-05 22:36:24 +0000656
aliguorifbe78f42008-12-17 19:13:11 +0000657 offset = i = 0;
658
659 while (offset < size) {
660 VirtQueueElement elem;
661 int len, total;
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200662 const struct iovec *sg = elem.in_sg;
aliguorifbe78f42008-12-17 19:13:11 +0000663
Amit Shah22c253d2010-01-13 16:24:43 +0530664 total = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000665
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300666 if (virtqueue_pop(n->rx_vq, &elem) == 0) {
aliguorifbe78f42008-12-17 19:13:11 +0000667 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100668 return -1;
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000669 error_report("virtio-net unexpected empty queue: "
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300670 "i %zd mergeable %d offset %zd, size %zd, "
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000671 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300672 i, n->mergeable_rx_bufs, offset, size,
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200673 n->guest_hdr_len, n->host_hdr_len, n->vdev.guest_features);
aliguorifbe78f42008-12-17 19:13:11 +0000674 exit(1);
675 }
676
677 if (elem.in_num < 1) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000678 error_report("virtio-net receive queue contains no in buffers");
aliguorifbe78f42008-12-17 19:13:11 +0000679 exit(1);
680 }
681
aliguorifbe78f42008-12-17 19:13:11 +0000682 if (i == 0) {
Michael S. Tsirkinc8d28e72012-09-24 13:26:55 +0200683 assert(offset == 0);
Michael S. Tsirkin63c58722012-09-24 13:17:13 +0200684 if (n->mergeable_rx_bufs) {
685 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
686 sg, elem.in_num,
687 offsetof(typeof(mhdr), num_buffers),
688 sizeof(mhdr.num_buffers));
689 }
aliguorifbe78f42008-12-17 19:13:11 +0000690
Michael S. Tsirkinc8d28e72012-09-24 13:26:55 +0200691 receive_header(n, sg, elem.in_num, buf, size);
692 offset = n->host_hdr_len;
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200693 total += n->guest_hdr_len;
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200694 guest_offset = n->guest_hdr_len;
695 } else {
696 guest_offset = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000697 }
698
699 /* copy in packet. ugh */
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200700 len = iov_from_buf(sg, elem.in_num, guest_offset,
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400701 buf + offset, size - offset);
aliguorifbe78f42008-12-17 19:13:11 +0000702 total += len;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300703 offset += len;
704 /* If buffers can't be merged, at this point we
705 * must have consumed the complete packet.
706 * Otherwise, drop it. */
707 if (!n->mergeable_rx_bufs && offset < size) {
708#if 0
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000709 error_report("virtio-net truncated non-mergeable packet: "
710 "i %zd mergeable %d offset %zd, size %zd, "
711 "guest hdr len %zd, host hdr len %zd",
712 i, n->mergeable_rx_bufs,
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200713 offset, size, n->guest_hdr_len, n->host_hdr_len);
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300714#endif
715 return size;
716 }
aliguorifbe78f42008-12-17 19:13:11 +0000717
718 /* signal other side */
719 virtqueue_fill(n->rx_vq, &elem, total, i++);
aliguorifbe78f42008-12-17 19:13:11 +0000720 }
721
Michael S. Tsirkin63c58722012-09-24 13:17:13 +0200722 if (mhdr_cnt) {
723 stw_p(&mhdr.num_buffers, i);
724 iov_from_buf(mhdr_sg, mhdr_cnt,
725 0,
726 &mhdr.num_buffers, sizeof mhdr.num_buffers);
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100727 }
aliguorifbe78f42008-12-17 19:13:11 +0000728
729 virtqueue_flush(n->rx_vq, i);
730 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100731
732 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000733}
734
Alex Williamsone3f30482010-09-02 09:00:57 -0600735static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
Mark McLoughlin62433752009-06-18 18:21:36 +0100736
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100737static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
Mark McLoughlin62433752009-06-18 18:21:36 +0100738{
Jason Wangcc1f0f42013-01-30 19:12:23 +0800739 VirtIONet *n = qemu_get_nic_opaque(nc);
Mark McLoughlin62433752009-06-18 18:21:36 +0100740
Michael S. Tsirkin40bad8f2012-09-24 15:15:43 +0200741 virtqueue_push(n->tx_vq, &n->async_tx.elem, 0);
Mark McLoughlin62433752009-06-18 18:21:36 +0100742 virtio_notify(&n->vdev, n->tx_vq);
743
744 n->async_tx.elem.out_num = n->async_tx.len = 0;
745
746 virtio_queue_set_notification(n->tx_vq, 1);
747 virtio_net_flush_tx(n, n->tx_vq);
748}
749
aliguorifbe78f42008-12-17 19:13:11 +0000750/* TX */
Alex Williamsone3f30482010-09-02 09:00:57 -0600751static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000752{
753 VirtQueueElement elem;
Alex Williamsone3f30482010-09-02 09:00:57 -0600754 int32_t num_packets = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -0600755 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
756 return num_packets;
757 }
aliguorifbe78f42008-12-17 19:13:11 +0000758
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200759 assert(n->vdev.vm_running);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200760
Mark McLoughlin62433752009-06-18 18:21:36 +0100761 if (n->async_tx.elem.out_num) {
762 virtio_queue_set_notification(n->tx_vq, 0);
Alex Williamsone3f30482010-09-02 09:00:57 -0600763 return num_packets;
Mark McLoughlin62433752009-06-18 18:21:36 +0100764 }
765
aliguorifbe78f42008-12-17 19:13:11 +0000766 while (virtqueue_pop(vq, &elem)) {
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200767 ssize_t ret, len;
aliguorifbe78f42008-12-17 19:13:11 +0000768 unsigned int out_num = elem.out_num;
769 struct iovec *out_sg = &elem.out_sg[0];
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200770 struct iovec sg[VIRTQUEUE_MAX_SIZE];
aliguorifbe78f42008-12-17 19:13:11 +0000771
Michael S. Tsirkin7b80d082012-09-24 14:54:44 +0200772 if (out_num < 1) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000773 error_report("virtio-net header not in first element");
aliguorifbe78f42008-12-17 19:13:11 +0000774 exit(1);
775 }
776
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200777 /*
778 * If host wants to see the guest header as is, we can
779 * pass it on unchanged. Otherwise, copy just the parts
780 * that host is interested in.
781 */
782 assert(n->host_hdr_len <= n->guest_hdr_len);
783 if (n->host_hdr_len != n->guest_hdr_len) {
784 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
785 out_sg, out_num,
786 0, n->host_hdr_len);
787 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
788 out_sg, out_num,
789 n->guest_hdr_len, -1);
790 out_num = sg_num;
791 out_sg = sg;
aliguorifbe78f42008-12-17 19:13:11 +0000792 }
793
Michael S. Tsirkin7b80d082012-09-24 14:54:44 +0200794 len = n->guest_hdr_len;
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200795
Jason Wangb356f762013-01-30 19:12:22 +0800796 ret = qemu_sendv_packet_async(qemu_get_queue(n->nic), out_sg, out_num,
Mark McLoughlin62433752009-06-18 18:21:36 +0100797 virtio_net_tx_complete);
798 if (ret == 0) {
799 virtio_queue_set_notification(n->tx_vq, 0);
800 n->async_tx.elem = elem;
801 n->async_tx.len = len;
Alex Williamsone3f30482010-09-02 09:00:57 -0600802 return -EBUSY;
Mark McLoughlin62433752009-06-18 18:21:36 +0100803 }
804
805 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000806
Michael S. Tsirkin40bad8f2012-09-24 15:15:43 +0200807 virtqueue_push(vq, &elem, 0);
aliguorifbe78f42008-12-17 19:13:11 +0000808 virtio_notify(&n->vdev, vq);
Alex Williamsone3f30482010-09-02 09:00:57 -0600809
810 if (++num_packets >= n->tx_burst) {
811 break;
812 }
aliguorifbe78f42008-12-17 19:13:11 +0000813 }
Alex Williamsone3f30482010-09-02 09:00:57 -0600814 return num_packets;
aliguorifbe78f42008-12-17 19:13:11 +0000815}
816
Alex Williamsona697a332010-09-02 09:01:10 -0600817static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000818{
819 VirtIONet *n = to_virtio_net(vdev);
820
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200821 /* This happens when device was stopped but VCPU wasn't. */
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200822 if (!n->vdev.vm_running) {
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200823 n->tx_waiting = 1;
824 return;
825 }
826
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600827 if (n->tx_waiting) {
aliguorifbe78f42008-12-17 19:13:11 +0000828 virtio_queue_set_notification(vq, 1);
829 qemu_del_timer(n->tx_timer);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600830 n->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000831 virtio_net_flush_tx(n, vq);
832 } else {
833 qemu_mod_timer(n->tx_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100834 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600835 n->tx_waiting = 1;
aliguorifbe78f42008-12-17 19:13:11 +0000836 virtio_queue_set_notification(vq, 0);
837 }
838}
839
Alex Williamsona697a332010-09-02 09:01:10 -0600840static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
841{
842 VirtIONet *n = to_virtio_net(vdev);
843
844 if (unlikely(n->tx_waiting)) {
845 return;
846 }
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200847 n->tx_waiting = 1;
848 /* This happens when device was stopped but VCPU wasn't. */
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200849 if (!n->vdev.vm_running) {
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200850 return;
851 }
Alex Williamsona697a332010-09-02 09:01:10 -0600852 virtio_queue_set_notification(vq, 0);
853 qemu_bh_schedule(n->tx_bh);
Alex Williamsona697a332010-09-02 09:01:10 -0600854}
855
aliguorifbe78f42008-12-17 19:13:11 +0000856static void virtio_net_tx_timer(void *opaque)
857{
858 VirtIONet *n = opaque;
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200859 assert(n->vdev.vm_running);
aliguorifbe78f42008-12-17 19:13:11 +0000860
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600861 n->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000862
863 /* Just in case the driver is not ready on more */
864 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
865 return;
866
867 virtio_queue_set_notification(n->tx_vq, 1);
868 virtio_net_flush_tx(n, n->tx_vq);
869}
870
Alex Williamsona697a332010-09-02 09:01:10 -0600871static void virtio_net_tx_bh(void *opaque)
872{
873 VirtIONet *n = opaque;
874 int32_t ret;
875
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200876 assert(n->vdev.vm_running);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200877
Alex Williamsona697a332010-09-02 09:01:10 -0600878 n->tx_waiting = 0;
879
880 /* Just in case the driver is not ready on more */
881 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
882 return;
883
884 ret = virtio_net_flush_tx(n, n->tx_vq);
885 if (ret == -EBUSY) {
886 return; /* Notification re-enable handled by tx_complete */
887 }
888
889 /* If we flush a full burst of packets, assume there are
890 * more coming and immediately reschedule */
891 if (ret >= n->tx_burst) {
892 qemu_bh_schedule(n->tx_bh);
893 n->tx_waiting = 1;
894 return;
895 }
896
897 /* If less than a full burst, re-enable notification and flush
898 * anything that may have come in while we weren't looking. If
899 * we find something, assume the guest is still active and reschedule */
900 virtio_queue_set_notification(n->tx_vq, 1);
901 if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
902 virtio_queue_set_notification(n->tx_vq, 0);
903 qemu_bh_schedule(n->tx_bh);
904 n->tx_waiting = 1;
905 }
906}
907
aliguorifbe78f42008-12-17 19:13:11 +0000908static void virtio_net_save(QEMUFile *f, void *opaque)
909{
910 VirtIONet *n = opaque;
911
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200912 /* At this point, backend must be stopped, otherwise
913 * it might keep writing to memory. */
914 assert(!n->vhost_started);
aliguorifbe78f42008-12-17 19:13:11 +0000915 virtio_save(&n->vdev, f);
916
aliguori79674062009-02-05 22:36:12 +0000917 qemu_put_buffer(f, n->mac, ETH_ALEN);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600918 qemu_put_be32(f, n->tx_waiting);
aliguorie46cb382009-01-07 17:50:45 +0000919 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000920 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600921 qemu_put_byte(f, n->promisc);
922 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000923 qemu_put_be32(f, n->mac_table.in_use);
924 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000925 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100926 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600927 qemu_put_byte(f, n->mac_table.multi_overflow);
928 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600929 qemu_put_byte(f, n->alluni);
930 qemu_put_byte(f, n->nomulti);
931 qemu_put_byte(f, n->nouni);
932 qemu_put_byte(f, n->nobcast);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100933 qemu_put_byte(f, n->has_ufo);
aliguorifbe78f42008-12-17 19:13:11 +0000934}
935
936static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
937{
938 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600939 int i;
Orit Wassermann2a633c42012-05-16 12:21:35 +0200940 int ret;
aliguorifbe78f42008-12-17 19:13:11 +0000941
aliguori9d6271b2009-02-05 22:36:04 +0000942 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000943 return -EINVAL;
944
Orit Wassermann2a633c42012-05-16 12:21:35 +0200945 ret = virtio_load(&n->vdev, f);
946 if (ret) {
947 return ret;
948 }
aliguorifbe78f42008-12-17 19:13:11 +0000949
aliguori79674062009-02-05 22:36:12 +0000950 qemu_get_buffer(f, n->mac, ETH_ALEN);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600951 n->tx_waiting = qemu_get_be32(f);
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +0200952
953 virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f));
aliguorifbe78f42008-12-17 19:13:11 +0000954
aliguori9d6271b2009-02-05 22:36:04 +0000955 if (version_id >= 3)
956 n->status = qemu_get_be16(f);
957
aliguori002437c2009-02-05 22:36:20 +0000958 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600959 if (version_id < 8) {
960 n->promisc = qemu_get_be32(f);
961 n->allmulti = qemu_get_be32(f);
962 } else {
963 n->promisc = qemu_get_byte(f);
964 n->allmulti = qemu_get_byte(f);
965 }
aliguori002437c2009-02-05 22:36:20 +0000966 }
967
aliguorib6503ed2009-02-05 22:36:28 +0000968 if (version_id >= 5) {
969 n->mac_table.in_use = qemu_get_be32(f);
970 /* MAC_TABLE_ENTRIES may be different from the saved image */
971 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
972 qemu_get_buffer(f, n->mac_table.macs,
973 n->mac_table.in_use * ETH_ALEN);
974 } else if (n->mac_table.in_use) {
Juan Quintelae398d612012-08-29 19:03:09 +0200975 uint8_t *buf = g_malloc0(n->mac_table.in_use);
976 qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN);
977 g_free(buf);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600978 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000979 n->mac_table.in_use = 0;
980 }
981 }
982
aliguorif21c0ed2009-02-05 22:36:32 +0000983 if (version_id >= 6)
984 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
985
Mark McLoughlin3a330132009-10-22 17:43:45 +0100986 if (version_id >= 7) {
987 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100988 error_report("virtio-net: saved image requires vnet_hdr=on");
Mark McLoughlin3a330132009-10-22 17:43:45 +0100989 return -1;
990 }
991
992 if (n->has_vnet_hdr) {
Jason Wangb356f762013-01-30 19:12:22 +0800993 tap_set_offload(qemu_get_queue(n->nic)->peer,
Michael S. Tsirkin704a76f2010-01-10 13:52:47 +0200994 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
995 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
996 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
997 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
998 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100999 }
Alex Williamson6c042c12009-06-05 14:46:52 -06001000 }
1001
Alex Williamson8fd2a2f2009-06-05 14:47:08 -06001002 if (version_id >= 9) {
1003 n->mac_table.multi_overflow = qemu_get_byte(f);
1004 n->mac_table.uni_overflow = qemu_get_byte(f);
1005 }
1006
Alex Williamson015cb162009-06-05 14:47:18 -06001007 if (version_id >= 10) {
1008 n->alluni = qemu_get_byte(f);
1009 n->nomulti = qemu_get_byte(f);
1010 n->nouni = qemu_get_byte(f);
1011 n->nobcast = qemu_get_byte(f);
1012 }
1013
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +01001014 if (version_id >= 11) {
1015 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001016 error_report("virtio-net: saved image requires TUN_F_UFO support");
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +01001017 return -1;
1018 }
1019 }
1020
Alex Williamson2d9aba32009-06-05 14:47:13 -06001021 /* Find the first multicast entry in the saved MAC filter */
1022 for (i = 0; i < n->mac_table.in_use; i++) {
1023 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
1024 break;
1025 }
1026 }
1027 n->mac_table.first_multi = i;
Amos Kong98991482012-09-28 10:06:02 +08001028
1029 /* nc.link_down can't be migrated, so infer link_down according
1030 * to link status bit in n->status */
Jason Wangb356f762013-01-30 19:12:22 +08001031 qemu_get_queue(n->nic)->link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
Amos Kong98991482012-09-28 10:06:02 +08001032
aliguorifbe78f42008-12-17 19:13:11 +00001033 return 0;
1034}
1035
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +01001036static void virtio_net_cleanup(NetClientState *nc)
aliguorib946a152009-04-17 17:11:08 +00001037{
Jason Wangcc1f0f42013-01-30 19:12:23 +08001038 VirtIONet *n = qemu_get_nic_opaque(nc);
aliguorib946a152009-04-17 17:11:08 +00001039
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001040 n->nic = NULL;
aliguorib946a152009-04-17 17:11:08 +00001041}
1042
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001043static NetClientInfo net_virtio_info = {
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001044 .type = NET_CLIENT_OPTIONS_KIND_NIC,
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001045 .size = sizeof(NICState),
1046 .can_receive = virtio_net_can_receive,
1047 .receive = virtio_net_receive,
1048 .cleanup = virtio_net_cleanup,
1049 .link_status_changed = virtio_net_set_link_status,
1050};
1051
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001052static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
1053{
1054 VirtIONet *n = to_virtio_net(vdev);
Jason Wangb356f762013-01-30 19:12:22 +08001055 NetClientState *nc = qemu_get_queue(n->nic);
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001056 assert(n->vhost_started);
Jason Wangb356f762013-01-30 19:12:22 +08001057 return vhost_net_virtqueue_pending(tap_get_vhost_net(nc->peer), idx);
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001058}
1059
1060static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
1061 bool mask)
1062{
1063 VirtIONet *n = to_virtio_net(vdev);
Jason Wangb356f762013-01-30 19:12:22 +08001064 NetClientState *nc = qemu_get_queue(n->nic);
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001065 assert(n->vhost_started);
Jason Wangb356f762013-01-30 19:12:22 +08001066 vhost_net_virtqueue_mask(tap_get_vhost_net(nc->peer),
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001067 vdev, idx, mask);
1068}
1069
Alex Williamsonf0c07c72010-09-02 09:00:50 -06001070VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
1071 virtio_net_conf *net)
aliguorifbe78f42008-12-17 19:13:11 +00001072{
1073 VirtIONet *n;
aliguorifbe78f42008-12-17 19:13:11 +00001074
Paul Brook53c25ce2009-05-18 14:51:59 +01001075 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
1076 sizeof(struct virtio_net_config),
1077 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +00001078
aliguori0f03eca2009-02-05 22:36:08 +00001079 n->vdev.get_config = virtio_net_get_config;
1080 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +00001081 n->vdev.get_features = virtio_net_get_features;
1082 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +00001083 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +00001084 n->vdev.reset = virtio_net_reset;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001085 n->vdev.set_status = virtio_net_set_status;
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001086 n->vdev.guest_notifier_mask = virtio_net_guest_notifier_mask;
1087 n->vdev.guest_notifier_pending = virtio_net_guest_notifier_pending;
aliguorifbe78f42008-12-17 19:13:11 +00001088 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
Alex Williamsona697a332010-09-02 09:01:10 -06001089
1090 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +00001091 error_report("virtio-net: "
1092 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1093 net->tx);
1094 error_report("Defaulting to \"bh\"");
Alex Williamsona697a332010-09-02 09:01:10 -06001095 }
1096
1097 if (net->tx && !strcmp(net->tx, "timer")) {
1098 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
Paolo Bonzini74475452011-03-11 16:47:48 +01001099 n->tx_timer = qemu_new_timer_ns(vm_clock, virtio_net_tx_timer, n);
Alex Williamsona697a332010-09-02 09:01:10 -06001100 n->tx_timeout = net->txtimer;
1101 } else {
1102 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
1103 n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
1104 }
Alex Williamson4ffb17f2009-06-05 14:47:23 -06001105 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001106 qemu_macaddr_default_if_unset(&conf->macaddr);
Mark McLoughlin3cbe04c2009-10-28 14:07:23 +00001107 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
aliguori554c97d2009-01-08 19:46:33 +00001108 n->status = VIRTIO_NET_S_LINK_UP;
aliguorifbe78f42008-12-17 19:13:11 +00001109
Anthony Liguorif79f2bf2011-12-04 11:17:51 -06001110 n->nic = qemu_new_nic(&net_virtio_info, conf, object_get_typename(OBJECT(dev)), dev->id, n);
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +02001111 peer_test_vnet_hdr(n);
1112 if (peer_has_vnet_hdr(n)) {
Jason Wangb356f762013-01-30 19:12:22 +08001113 tap_using_vnet_hdr(qemu_get_queue(n->nic)->peer, true);
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +02001114 n->host_hdr_len = sizeof(struct virtio_net_hdr);
1115 } else {
1116 n->host_hdr_len = 0;
1117 }
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001118
Jason Wangb356f762013-01-30 19:12:22 +08001119 qemu_format_nic_info_str(qemu_get_queue(n->nic), conf->macaddr.a);
aliguori96d5e202009-01-07 17:47:15 +00001120
Alex Williamson4b4b8d32010-09-02 09:01:04 -06001121 n->tx_waiting = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -06001122 n->tx_burst = net->txburst;
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +02001123 virtio_net_set_mrg_rx_bufs(n, 0);
aliguori002437c2009-02-05 22:36:20 +00001124 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +00001125
Anthony Liguori7267c092011-08-20 22:09:37 -05001126 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +00001127
Anthony Liguori7267c092011-08-20 22:09:37 -05001128 n->vlans = g_malloc0(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +00001129
Alex Williamson01657c82010-06-25 11:09:28 -06001130 n->qdev = dev;
1131 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +00001132 virtio_net_save, virtio_net_load, n);
Paul Brookcf21e102009-05-14 22:35:07 +01001133
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001134 add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0");
1135
Paul Brook53c25ce2009-05-18 14:51:59 +01001136 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +01001137}
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001138
1139void virtio_net_exit(VirtIODevice *vdev)
1140{
1141 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001142
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +02001143 /* This will stop vhost backend if appropriate. */
1144 virtio_net_set_status(vdev, 0);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001145
Jason Wangb356f762013-01-30 19:12:22 +08001146 qemu_purge_queued_packets(qemu_get_queue(n->nic));
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001147
Alex Williamson01657c82010-06-25 11:09:28 -06001148 unregister_savevm(n->qdev, "virtio-net", n);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001149
Anthony Liguori7267c092011-08-20 22:09:37 -05001150 g_free(n->mac_table.macs);
1151 g_free(n->vlans);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001152
Alex Williamsona697a332010-09-02 09:01:10 -06001153 if (n->tx_timer) {
1154 qemu_del_timer(n->tx_timer);
1155 qemu_free_timer(n->tx_timer);
1156 } else {
1157 qemu_bh_delete(n->tx_bh);
1158 }
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001159
Jason Wang948ecf22013-01-30 19:12:24 +08001160 qemu_del_nic(n->nic);
Amit Shahb52dfd72011-07-27 14:00:31 +05301161 virtio_cleanup(&n->vdev);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001162}