blob: b5579b4dbf609333daa33ae47157277198013f7a [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);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000099 qemu_format_nic_info_str(&n->nic->nc, 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{
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200111 if (!n->nic->nc.peer) {
112 return;
113 }
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200114 if (n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200115 return;
116 }
117
118 if (!tap_get_vhost_net(n->nic->nc.peer)) {
119 return;
120 }
Michael S. Tsirkin32993692011-02-09 18:45:09 +0200121 if (!!n->vhost_started == virtio_net_started(n, status) &&
122 !n->nic->nc.peer->link_down) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200123 return;
124 }
125 if (!n->vhost_started) {
mst@redhat.com5430a282011-02-01 22:13:42 +0200126 int r;
127 if (!vhost_net_query(tap_get_vhost_net(n->nic->nc.peer), &n->vdev)) {
128 return;
129 }
Michael S. Tsirkin1830b802012-12-25 17:38:59 +0200130 n->vhost_started = 1;
mst@redhat.com5430a282011-02-01 22:13:42 +0200131 r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200132 if (r < 0) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000133 error_report("unable to start vhost net: %d: "
134 "falling back on userspace virtio", -r);
Michael S. Tsirkin1830b802012-12-25 17:38:59 +0200135 n->vhost_started = 0;
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200136 }
137 } else {
138 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
139 n->vhost_started = 0;
140 }
141}
142
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200143static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
144{
145 VirtIONet *n = to_virtio_net(vdev);
146
147 virtio_net_vhost_status(n, status);
148
149 if (!n->tx_waiting) {
150 return;
151 }
152
153 if (virtio_net_started(n, status) && !n->vhost_started) {
154 if (n->tx_timer) {
155 qemu_mod_timer(n->tx_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100156 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200157 } else {
158 qemu_bh_schedule(n->tx_bh);
159 }
160 } else {
161 if (n->tx_timer) {
162 qemu_del_timer(n->tx_timer);
163 } else {
164 qemu_bh_cancel(n->tx_bh);
165 }
166 }
167}
168
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100169static void virtio_net_set_link_status(NetClientState *nc)
aliguori554c97d2009-01-08 19:46:33 +0000170{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000171 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguori554c97d2009-01-08 19:46:33 +0000172 uint16_t old_status = n->status;
173
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000174 if (nc->link_down)
aliguori554c97d2009-01-08 19:46:33 +0000175 n->status &= ~VIRTIO_NET_S_LINK_UP;
176 else
177 n->status |= VIRTIO_NET_S_LINK_UP;
178
179 if (n->status != old_status)
180 virtio_notify_config(&n->vdev);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200181
182 virtio_net_set_status(&n->vdev, n->vdev.status);
aliguori554c97d2009-01-08 19:46:33 +0000183}
184
aliguori002437c2009-02-05 22:36:20 +0000185static void virtio_net_reset(VirtIODevice *vdev)
186{
187 VirtIONet *n = to_virtio_net(vdev);
188
189 /* Reset back to compatibility mode */
190 n->promisc = 1;
191 n->allmulti = 0;
Alex Williamson015cb162009-06-05 14:47:18 -0600192 n->alluni = 0;
193 n->nomulti = 0;
194 n->nouni = 0;
195 n->nobcast = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000196
aliguorif21c0ed2009-02-05 22:36:32 +0000197 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000198 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600199 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600200 n->mac_table.multi_overflow = 0;
201 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000202 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
Michael S. Tsirkin41dc8a62013-01-16 11:37:40 +0200203 memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
aliguorif21c0ed2009-02-05 22:36:32 +0000204 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000205}
206
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200207static void peer_test_vnet_hdr(VirtIONet *n)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100208{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000209 if (!n->nic->nc.peer)
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200210 return;
Mark McLoughlin3a330132009-10-22 17:43:45 +0100211
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200212 if (n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP)
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200213 return;
Mark McLoughlin3a330132009-10-22 17:43:45 +0100214
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000215 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200216}
Mark McLoughlin3a330132009-10-22 17:43:45 +0100217
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200218static int peer_has_vnet_hdr(VirtIONet *n)
219{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100220 return n->has_vnet_hdr;
221}
222
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100223static int peer_has_ufo(VirtIONet *n)
224{
225 if (!peer_has_vnet_hdr(n))
226 return 0;
227
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000228 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100229
230 return n->has_ufo;
231}
232
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +0200233static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs)
234{
235 n->mergeable_rx_bufs = mergeable_rx_bufs;
236
237 n->guest_hdr_len = n->mergeable_rx_bufs ?
238 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
239
240 if (peer_has_vnet_hdr(n) &&
241 tap_has_vnet_hdr_len(n->nic->nc.peer, n->guest_hdr_len)) {
242 tap_set_vnet_hdr_len(n->nic->nc.peer, n->guest_hdr_len);
243 n->host_hdr_len = n->guest_hdr_len;
244 }
245}
246
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200247static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
aliguorifbe78f42008-12-17 19:13:11 +0000248{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100249 VirtIONet *n = to_virtio_net(vdev);
aliguorifbe78f42008-12-17 19:13:11 +0000250
Michael S. Tsirkinc9f79a32010-01-12 20:50:17 +0200251 features |= (1 << VIRTIO_NET_F_MAC);
252
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200253 if (!peer_has_vnet_hdr(n)) {
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200254 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
255 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
256 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
257 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100258
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200259 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
260 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
261 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
262 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
263 }
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100264
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200265 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
266 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
267 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100268 }
269
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200270 if (!n->nic->nc.peer ||
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200271 n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200272 return features;
273 }
274 if (!tap_get_vhost_net(n->nic->nc.peer)) {
275 return features;
276 }
277 return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000278}
279
aliguori8eca6b12009-04-05 17:40:08 +0000280static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
281{
282 uint32_t features = 0;
283
284 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
285 * but also these: */
286 features |= (1 << VIRTIO_NET_F_MAC);
Dustin Kirkland184bd042009-10-29 10:34:15 -0500287 features |= (1 << VIRTIO_NET_F_CSUM);
288 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
289 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
290 features |= (1 << VIRTIO_NET_F_HOST_ECN);
aliguori8eca6b12009-04-05 17:40:08 +0000291
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200292 return features;
aliguori8eca6b12009-04-05 17:40:08 +0000293}
294
aliguorifbe78f42008-12-17 19:13:11 +0000295static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
296{
297 VirtIONet *n = to_virtio_net(vdev);
298
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +0200299 virtio_net_set_mrg_rx_bufs(n, !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF)));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100300
301 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000302 tap_set_offload(n->nic->nc.peer,
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100303 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
304 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
305 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100306 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
307 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100308 }
David L Stevensdc14a392010-03-31 21:20:31 +0300309 if (!n->nic->nc.peer ||
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200310 n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
David L Stevensdc14a392010-03-31 21:20:31 +0300311 return;
312 }
313 if (!tap_get_vhost_net(n->nic->nc.peer)) {
314 return;
315 }
Michael S. Tsirkin57c32292010-05-09 14:35:43 +0300316 vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000317}
318
aliguori002437c2009-02-05 22:36:20 +0000319static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800320 struct iovec *iov, unsigned int iov_cnt)
aliguori002437c2009-02-05 22:36:20 +0000321{
322 uint8_t on;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800323 size_t s;
aliguori002437c2009-02-05 22:36:20 +0000324
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800325 s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
326 if (s != sizeof(on)) {
327 return VIRTIO_NET_ERR;
aliguori002437c2009-02-05 22:36:20 +0000328 }
329
Amos Kongdd234542013-01-22 23:44:46 +0800330 if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
aliguori002437c2009-02-05 22:36:20 +0000331 n->promisc = on;
Amos Kongdd234542013-01-22 23:44:46 +0800332 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
aliguori002437c2009-02-05 22:36:20 +0000333 n->allmulti = on;
Amos Kongdd234542013-01-22 23:44:46 +0800334 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
Alex Williamson015cb162009-06-05 14:47:18 -0600335 n->alluni = on;
Amos Kongdd234542013-01-22 23:44:46 +0800336 } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
Alex Williamson015cb162009-06-05 14:47:18 -0600337 n->nomulti = on;
Amos Kongdd234542013-01-22 23:44:46 +0800338 } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
Alex Williamson015cb162009-06-05 14:47:18 -0600339 n->nouni = on;
Amos Kongdd234542013-01-22 23:44:46 +0800340 } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
Alex Williamson015cb162009-06-05 14:47:18 -0600341 n->nobcast = on;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800342 } else {
aliguori002437c2009-02-05 22:36:20 +0000343 return VIRTIO_NET_ERR;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800344 }
aliguori002437c2009-02-05 22:36:20 +0000345
346 return VIRTIO_NET_OK;
347}
348
aliguorib6503ed2009-02-05 22:36:28 +0000349static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800350 struct iovec *iov, unsigned int iov_cnt)
aliguorib6503ed2009-02-05 22:36:28 +0000351{
352 struct virtio_net_ctrl_mac mac_data;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800353 size_t s;
aliguorib6503ed2009-02-05 22:36:28 +0000354
Amos Kongc1943a32013-01-22 23:44:45 +0800355 if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
356 if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
357 return VIRTIO_NET_ERR;
358 }
359 s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
360 assert(s == sizeof(n->mac));
361 qemu_format_nic_info_str(&n->nic->nc, n->mac);
362 return VIRTIO_NET_OK;
363 }
364
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800365 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
aliguorib6503ed2009-02-05 22:36:28 +0000366 return VIRTIO_NET_ERR;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800367 }
aliguorib6503ed2009-02-05 22:36:28 +0000368
369 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600370 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600371 n->mac_table.uni_overflow = 0;
372 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000373 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
374
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800375 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
376 sizeof(mac_data.entries));
377 mac_data.entries = ldl_p(&mac_data.entries);
378 if (s != sizeof(mac_data.entries)) {
aliguorib6503ed2009-02-05 22:36:28 +0000379 return VIRTIO_NET_ERR;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800380 }
381 iov_discard_front(&iov, &iov_cnt, s);
382
383 if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
384 return VIRTIO_NET_ERR;
385 }
aliguorib6503ed2009-02-05 22:36:28 +0000386
387 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800388 s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs,
389 mac_data.entries * ETH_ALEN);
390 if (s != mac_data.entries * ETH_ALEN) {
391 return VIRTIO_NET_ERR;
392 }
aliguorib6503ed2009-02-05 22:36:28 +0000393 n->mac_table.in_use += mac_data.entries;
394 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600395 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000396 }
397
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800398 iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
399
Alex Williamson2d9aba32009-06-05 14:47:13 -0600400 n->mac_table.first_multi = n->mac_table.in_use;
401
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800402 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
403 sizeof(mac_data.entries));
404 mac_data.entries = ldl_p(&mac_data.entries);
405 if (s != sizeof(mac_data.entries)) {
aliguorib6503ed2009-02-05 22:36:28 +0000406 return VIRTIO_NET_ERR;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800407 }
aliguorib6503ed2009-02-05 22:36:28 +0000408
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800409 iov_discard_front(&iov, &iov_cnt, s);
410
411 if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
412 return VIRTIO_NET_ERR;
413 }
414
415 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
416 s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs,
417 mac_data.entries * ETH_ALEN);
418 if (s != mac_data.entries * ETH_ALEN) {
419 return VIRTIO_NET_ERR;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600420 }
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800421 n->mac_table.in_use += mac_data.entries;
422 } else {
423 n->mac_table.multi_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000424 }
425
426 return VIRTIO_NET_OK;
427}
428
aliguorif21c0ed2009-02-05 22:36:32 +0000429static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800430 struct iovec *iov, unsigned int iov_cnt)
aliguorif21c0ed2009-02-05 22:36:32 +0000431{
432 uint16_t vid;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800433 size_t s;
aliguorif21c0ed2009-02-05 22:36:32 +0000434
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800435 s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
436 vid = lduw_p(&vid);
437 if (s != sizeof(vid)) {
aliguorif21c0ed2009-02-05 22:36:32 +0000438 return VIRTIO_NET_ERR;
439 }
440
aliguorif21c0ed2009-02-05 22:36:32 +0000441 if (vid >= MAX_VLAN)
442 return VIRTIO_NET_ERR;
443
444 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
445 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
446 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
447 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
448 else
449 return VIRTIO_NET_ERR;
450
451 return VIRTIO_NET_OK;
452}
453
aliguori3d11d362009-02-05 22:36:16 +0000454static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
455{
aliguori002437c2009-02-05 22:36:20 +0000456 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000457 struct virtio_net_ctrl_hdr ctrl;
458 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
459 VirtQueueElement elem;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800460 size_t s;
461 struct iovec *iov;
462 unsigned int iov_cnt;
aliguori3d11d362009-02-05 22:36:16 +0000463
464 while (virtqueue_pop(vq, &elem)) {
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800465 if (iov_size(elem.in_sg, elem.in_num) < sizeof(status) ||
466 iov_size(elem.out_sg, elem.out_num) < sizeof(ctrl)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000467 error_report("virtio-net ctrl missing headers");
aliguori3d11d362009-02-05 22:36:16 +0000468 exit(1);
469 }
470
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800471 iov = elem.out_sg;
472 iov_cnt = elem.out_num;
473 s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
474 iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
475 if (s != sizeof(ctrl)) {
476 status = VIRTIO_NET_ERR;
Amos Kongdd234542013-01-22 23:44:46 +0800477 } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800478 status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
479 } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
480 status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
481 } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
482 status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
aliguori3d11d362009-02-05 22:36:16 +0000483 }
484
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800485 s = iov_from_buf(elem.in_sg, elem.in_num, 0, &status, sizeof(status));
486 assert(s == sizeof(status));
aliguori3d11d362009-02-05 22:36:16 +0000487
488 virtqueue_push(vq, &elem, sizeof(status));
489 virtio_notify(vdev, vq);
490 }
491}
492
aliguorifbe78f42008-12-17 19:13:11 +0000493/* RX */
494
495static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
496{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100497 VirtIONet *n = to_virtio_net(vdev);
498
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000499 qemu_flush_queued_packets(&n->nic->nc);
aliguorifbe78f42008-12-17 19:13:11 +0000500}
501
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100502static int virtio_net_can_receive(NetClientState *nc)
aliguorifbe78f42008-12-17 19:13:11 +0000503{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000504 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200505 if (!n->vdev.vm_running) {
Michael S. Tsirkin95477322010-11-22 19:52:19 +0200506 return 0;
507 }
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000508
aliguorifbe78f42008-12-17 19:13:11 +0000509 if (!virtio_queue_ready(n->rx_vq) ||
510 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
511 return 0;
512
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000513 return 1;
514}
515
516static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
517{
aliguorifbe78f42008-12-17 19:13:11 +0000518 if (virtio_queue_empty(n->rx_vq) ||
519 (n->mergeable_rx_bufs &&
520 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
521 virtio_queue_set_notification(n->rx_vq, 1);
Tom Lendacky06b12972010-02-08 10:10:01 -0600522
523 /* To avoid a race condition where the guest has made some buffers
524 * available after the above check but before notification was
525 * enabled, check for available buffers again.
526 */
527 if (virtio_queue_empty(n->rx_vq) ||
528 (n->mergeable_rx_bufs &&
529 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
530 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000531 }
532
533 virtio_queue_set_notification(n->rx_vq, 0);
534 return 1;
535}
536
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100537/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
538 * it never finds out that the packets don't have valid checksums. This
539 * causes dhclient to get upset. Fedora's carried a patch for ages to
540 * fix this with Xen but it hasn't appeared in an upstream release of
541 * dhclient yet.
542 *
543 * To avoid breaking existing guests, we catch udp packets and add
544 * checksums. This is terrible but it's better than hacking the guest
545 * kernels.
546 *
547 * N.B. if we introduce a zero-copy API, this operation is no longer free so
548 * we should provide a mechanism to disable it to avoid polluting the host
549 * cache.
550 */
551static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200552 uint8_t *buf, size_t size)
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100553{
554 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
555 (size > 27 && size < 1500) && /* normal sized MTU */
556 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
557 (buf[23] == 17) && /* ip.protocol == UDP */
558 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200559 net_checksum_calculate(buf, size);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100560 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
561 }
562}
563
Michael S. Tsirkin280598b2012-09-24 13:24:17 +0200564static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
565 const void *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000566{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100567 if (n->has_vnet_hdr) {
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200568 /* FIXME this cast is evil */
569 void *wbuf = (void *)buf;
Michael S. Tsirkin280598b2012-09-24 13:24:17 +0200570 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
571 size - n->host_hdr_len);
572 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200573 } else {
574 struct virtio_net_hdr hdr = {
575 .flags = 0,
576 .gso_type = VIRTIO_NET_HDR_GSO_NONE
577 };
578 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100579 }
aliguorifbe78f42008-12-17 19:13:11 +0000580}
581
aliguori3831ab22009-02-05 22:36:24 +0000582static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
583{
584 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000585 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000586 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000587 int i;
aliguori3831ab22009-02-05 22:36:24 +0000588
589 if (n->promisc)
590 return 1;
591
Michael S. Tsirkine043ebc2012-09-24 16:27:27 +0200592 ptr += n->host_hdr_len;
Mark McLoughlin3a330132009-10-22 17:43:45 +0100593
aliguorif21c0ed2009-02-05 22:36:32 +0000594 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
595 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
596 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
597 return 0;
598 }
599
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600600 if (ptr[0] & 1) { // multicast
601 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600602 return !n->nobcast;
603 } else if (n->nomulti) {
604 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600605 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600606 return 1;
607 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600608
609 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
610 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
611 return 1;
612 }
613 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600614 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600615 if (n->nouni) {
616 return 0;
617 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600618 return 1;
619 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600620 return 1;
621 }
aliguori3831ab22009-02-05 22:36:24 +0000622
Alex Williamson2d9aba32009-06-05 14:47:13 -0600623 for (i = 0; i < n->mac_table.first_multi; i++) {
624 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
625 return 1;
626 }
627 }
aliguorib6503ed2009-02-05 22:36:28 +0000628 }
629
aliguori3831ab22009-02-05 22:36:24 +0000630 return 0;
631}
632
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100633static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000634{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000635 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Michael S. Tsirkin63c58722012-09-24 13:17:13 +0200636 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
637 struct virtio_net_hdr_mrg_rxbuf mhdr;
638 unsigned mhdr_cnt = 0;
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200639 size_t offset, i, guest_offset;
aliguorifbe78f42008-12-17 19:13:11 +0000640
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000641 if (!virtio_net_can_receive(&n->nic->nc))
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000642 return -1;
643
Michael S. Tsirkin940cda92010-06-06 18:53:10 +0300644 /* hdr_len refers to the header we supply to the guest */
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200645 if (!virtio_net_has_buffers(n, size + n->guest_hdr_len - n->host_hdr_len))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100646 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000647
aliguori3831ab22009-02-05 22:36:24 +0000648 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100649 return size;
aliguori3831ab22009-02-05 22:36:24 +0000650
aliguorifbe78f42008-12-17 19:13:11 +0000651 offset = i = 0;
652
653 while (offset < size) {
654 VirtQueueElement elem;
655 int len, total;
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200656 const struct iovec *sg = elem.in_sg;
aliguorifbe78f42008-12-17 19:13:11 +0000657
Amit Shah22c253d2010-01-13 16:24:43 +0530658 total = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000659
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300660 if (virtqueue_pop(n->rx_vq, &elem) == 0) {
aliguorifbe78f42008-12-17 19:13:11 +0000661 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100662 return -1;
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000663 error_report("virtio-net unexpected empty queue: "
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300664 "i %zd mergeable %d offset %zd, size %zd, "
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000665 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300666 i, n->mergeable_rx_bufs, offset, size,
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200667 n->guest_hdr_len, n->host_hdr_len, n->vdev.guest_features);
aliguorifbe78f42008-12-17 19:13:11 +0000668 exit(1);
669 }
670
671 if (elem.in_num < 1) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000672 error_report("virtio-net receive queue contains no in buffers");
aliguorifbe78f42008-12-17 19:13:11 +0000673 exit(1);
674 }
675
aliguorifbe78f42008-12-17 19:13:11 +0000676 if (i == 0) {
Michael S. Tsirkinc8d28e72012-09-24 13:26:55 +0200677 assert(offset == 0);
Michael S. Tsirkin63c58722012-09-24 13:17:13 +0200678 if (n->mergeable_rx_bufs) {
679 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
680 sg, elem.in_num,
681 offsetof(typeof(mhdr), num_buffers),
682 sizeof(mhdr.num_buffers));
683 }
aliguorifbe78f42008-12-17 19:13:11 +0000684
Michael S. Tsirkinc8d28e72012-09-24 13:26:55 +0200685 receive_header(n, sg, elem.in_num, buf, size);
686 offset = n->host_hdr_len;
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200687 total += n->guest_hdr_len;
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200688 guest_offset = n->guest_hdr_len;
689 } else {
690 guest_offset = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000691 }
692
693 /* copy in packet. ugh */
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200694 len = iov_from_buf(sg, elem.in_num, guest_offset,
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400695 buf + offset, size - offset);
aliguorifbe78f42008-12-17 19:13:11 +0000696 total += len;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300697 offset += len;
698 /* If buffers can't be merged, at this point we
699 * must have consumed the complete packet.
700 * Otherwise, drop it. */
701 if (!n->mergeable_rx_bufs && offset < size) {
702#if 0
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000703 error_report("virtio-net truncated non-mergeable packet: "
704 "i %zd mergeable %d offset %zd, size %zd, "
705 "guest hdr len %zd, host hdr len %zd",
706 i, n->mergeable_rx_bufs,
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200707 offset, size, n->guest_hdr_len, n->host_hdr_len);
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300708#endif
709 return size;
710 }
aliguorifbe78f42008-12-17 19:13:11 +0000711
712 /* signal other side */
713 virtqueue_fill(n->rx_vq, &elem, total, i++);
aliguorifbe78f42008-12-17 19:13:11 +0000714 }
715
Michael S. Tsirkin63c58722012-09-24 13:17:13 +0200716 if (mhdr_cnt) {
717 stw_p(&mhdr.num_buffers, i);
718 iov_from_buf(mhdr_sg, mhdr_cnt,
719 0,
720 &mhdr.num_buffers, sizeof mhdr.num_buffers);
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100721 }
aliguorifbe78f42008-12-17 19:13:11 +0000722
723 virtqueue_flush(n->rx_vq, i);
724 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100725
726 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000727}
728
Alex Williamsone3f30482010-09-02 09:00:57 -0600729static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
Mark McLoughlin62433752009-06-18 18:21:36 +0100730
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100731static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
Mark McLoughlin62433752009-06-18 18:21:36 +0100732{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000733 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlin62433752009-06-18 18:21:36 +0100734
Michael S. Tsirkin40bad8f2012-09-24 15:15:43 +0200735 virtqueue_push(n->tx_vq, &n->async_tx.elem, 0);
Mark McLoughlin62433752009-06-18 18:21:36 +0100736 virtio_notify(&n->vdev, n->tx_vq);
737
738 n->async_tx.elem.out_num = n->async_tx.len = 0;
739
740 virtio_queue_set_notification(n->tx_vq, 1);
741 virtio_net_flush_tx(n, n->tx_vq);
742}
743
aliguorifbe78f42008-12-17 19:13:11 +0000744/* TX */
Alex Williamsone3f30482010-09-02 09:00:57 -0600745static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000746{
747 VirtQueueElement elem;
Alex Williamsone3f30482010-09-02 09:00:57 -0600748 int32_t num_packets = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -0600749 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
750 return num_packets;
751 }
aliguorifbe78f42008-12-17 19:13:11 +0000752
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200753 assert(n->vdev.vm_running);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200754
Mark McLoughlin62433752009-06-18 18:21:36 +0100755 if (n->async_tx.elem.out_num) {
756 virtio_queue_set_notification(n->tx_vq, 0);
Alex Williamsone3f30482010-09-02 09:00:57 -0600757 return num_packets;
Mark McLoughlin62433752009-06-18 18:21:36 +0100758 }
759
aliguorifbe78f42008-12-17 19:13:11 +0000760 while (virtqueue_pop(vq, &elem)) {
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200761 ssize_t ret, len;
aliguorifbe78f42008-12-17 19:13:11 +0000762 unsigned int out_num = elem.out_num;
763 struct iovec *out_sg = &elem.out_sg[0];
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200764 struct iovec sg[VIRTQUEUE_MAX_SIZE];
aliguorifbe78f42008-12-17 19:13:11 +0000765
Michael S. Tsirkin7b80d082012-09-24 14:54:44 +0200766 if (out_num < 1) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000767 error_report("virtio-net header not in first element");
aliguorifbe78f42008-12-17 19:13:11 +0000768 exit(1);
769 }
770
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200771 /*
772 * If host wants to see the guest header as is, we can
773 * pass it on unchanged. Otherwise, copy just the parts
774 * that host is interested in.
775 */
776 assert(n->host_hdr_len <= n->guest_hdr_len);
777 if (n->host_hdr_len != n->guest_hdr_len) {
778 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
779 out_sg, out_num,
780 0, n->host_hdr_len);
781 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
782 out_sg, out_num,
783 n->guest_hdr_len, -1);
784 out_num = sg_num;
785 out_sg = sg;
aliguorifbe78f42008-12-17 19:13:11 +0000786 }
787
Michael S. Tsirkin7b80d082012-09-24 14:54:44 +0200788 len = n->guest_hdr_len;
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200789
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000790 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
Mark McLoughlin62433752009-06-18 18:21:36 +0100791 virtio_net_tx_complete);
792 if (ret == 0) {
793 virtio_queue_set_notification(n->tx_vq, 0);
794 n->async_tx.elem = elem;
795 n->async_tx.len = len;
Alex Williamsone3f30482010-09-02 09:00:57 -0600796 return -EBUSY;
Mark McLoughlin62433752009-06-18 18:21:36 +0100797 }
798
799 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000800
Michael S. Tsirkin40bad8f2012-09-24 15:15:43 +0200801 virtqueue_push(vq, &elem, 0);
aliguorifbe78f42008-12-17 19:13:11 +0000802 virtio_notify(&n->vdev, vq);
Alex Williamsone3f30482010-09-02 09:00:57 -0600803
804 if (++num_packets >= n->tx_burst) {
805 break;
806 }
aliguorifbe78f42008-12-17 19:13:11 +0000807 }
Alex Williamsone3f30482010-09-02 09:00:57 -0600808 return num_packets;
aliguorifbe78f42008-12-17 19:13:11 +0000809}
810
Alex Williamsona697a332010-09-02 09:01:10 -0600811static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000812{
813 VirtIONet *n = to_virtio_net(vdev);
814
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200815 /* This happens when device was stopped but VCPU wasn't. */
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200816 if (!n->vdev.vm_running) {
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200817 n->tx_waiting = 1;
818 return;
819 }
820
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600821 if (n->tx_waiting) {
aliguorifbe78f42008-12-17 19:13:11 +0000822 virtio_queue_set_notification(vq, 1);
823 qemu_del_timer(n->tx_timer);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600824 n->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000825 virtio_net_flush_tx(n, vq);
826 } else {
827 qemu_mod_timer(n->tx_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100828 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600829 n->tx_waiting = 1;
aliguorifbe78f42008-12-17 19:13:11 +0000830 virtio_queue_set_notification(vq, 0);
831 }
832}
833
Alex Williamsona697a332010-09-02 09:01:10 -0600834static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
835{
836 VirtIONet *n = to_virtio_net(vdev);
837
838 if (unlikely(n->tx_waiting)) {
839 return;
840 }
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200841 n->tx_waiting = 1;
842 /* This happens when device was stopped but VCPU wasn't. */
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200843 if (!n->vdev.vm_running) {
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200844 return;
845 }
Alex Williamsona697a332010-09-02 09:01:10 -0600846 virtio_queue_set_notification(vq, 0);
847 qemu_bh_schedule(n->tx_bh);
Alex Williamsona697a332010-09-02 09:01:10 -0600848}
849
aliguorifbe78f42008-12-17 19:13:11 +0000850static void virtio_net_tx_timer(void *opaque)
851{
852 VirtIONet *n = opaque;
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200853 assert(n->vdev.vm_running);
aliguorifbe78f42008-12-17 19:13:11 +0000854
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600855 n->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000856
857 /* Just in case the driver is not ready on more */
858 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
859 return;
860
861 virtio_queue_set_notification(n->tx_vq, 1);
862 virtio_net_flush_tx(n, n->tx_vq);
863}
864
Alex Williamsona697a332010-09-02 09:01:10 -0600865static void virtio_net_tx_bh(void *opaque)
866{
867 VirtIONet *n = opaque;
868 int32_t ret;
869
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200870 assert(n->vdev.vm_running);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200871
Alex Williamsona697a332010-09-02 09:01:10 -0600872 n->tx_waiting = 0;
873
874 /* Just in case the driver is not ready on more */
875 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
876 return;
877
878 ret = virtio_net_flush_tx(n, n->tx_vq);
879 if (ret == -EBUSY) {
880 return; /* Notification re-enable handled by tx_complete */
881 }
882
883 /* If we flush a full burst of packets, assume there are
884 * more coming and immediately reschedule */
885 if (ret >= n->tx_burst) {
886 qemu_bh_schedule(n->tx_bh);
887 n->tx_waiting = 1;
888 return;
889 }
890
891 /* If less than a full burst, re-enable notification and flush
892 * anything that may have come in while we weren't looking. If
893 * we find something, assume the guest is still active and reschedule */
894 virtio_queue_set_notification(n->tx_vq, 1);
895 if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
896 virtio_queue_set_notification(n->tx_vq, 0);
897 qemu_bh_schedule(n->tx_bh);
898 n->tx_waiting = 1;
899 }
900}
901
aliguorifbe78f42008-12-17 19:13:11 +0000902static void virtio_net_save(QEMUFile *f, void *opaque)
903{
904 VirtIONet *n = opaque;
905
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200906 /* At this point, backend must be stopped, otherwise
907 * it might keep writing to memory. */
908 assert(!n->vhost_started);
aliguorifbe78f42008-12-17 19:13:11 +0000909 virtio_save(&n->vdev, f);
910
aliguori79674062009-02-05 22:36:12 +0000911 qemu_put_buffer(f, n->mac, ETH_ALEN);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600912 qemu_put_be32(f, n->tx_waiting);
aliguorie46cb382009-01-07 17:50:45 +0000913 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000914 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600915 qemu_put_byte(f, n->promisc);
916 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000917 qemu_put_be32(f, n->mac_table.in_use);
918 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000919 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100920 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600921 qemu_put_byte(f, n->mac_table.multi_overflow);
922 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600923 qemu_put_byte(f, n->alluni);
924 qemu_put_byte(f, n->nomulti);
925 qemu_put_byte(f, n->nouni);
926 qemu_put_byte(f, n->nobcast);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100927 qemu_put_byte(f, n->has_ufo);
aliguorifbe78f42008-12-17 19:13:11 +0000928}
929
930static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
931{
932 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600933 int i;
Orit Wassermann2a633c42012-05-16 12:21:35 +0200934 int ret;
aliguorifbe78f42008-12-17 19:13:11 +0000935
aliguori9d6271b2009-02-05 22:36:04 +0000936 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000937 return -EINVAL;
938
Orit Wassermann2a633c42012-05-16 12:21:35 +0200939 ret = virtio_load(&n->vdev, f);
940 if (ret) {
941 return ret;
942 }
aliguorifbe78f42008-12-17 19:13:11 +0000943
aliguori79674062009-02-05 22:36:12 +0000944 qemu_get_buffer(f, n->mac, ETH_ALEN);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600945 n->tx_waiting = qemu_get_be32(f);
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +0200946
947 virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f));
aliguorifbe78f42008-12-17 19:13:11 +0000948
aliguori9d6271b2009-02-05 22:36:04 +0000949 if (version_id >= 3)
950 n->status = qemu_get_be16(f);
951
aliguori002437c2009-02-05 22:36:20 +0000952 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600953 if (version_id < 8) {
954 n->promisc = qemu_get_be32(f);
955 n->allmulti = qemu_get_be32(f);
956 } else {
957 n->promisc = qemu_get_byte(f);
958 n->allmulti = qemu_get_byte(f);
959 }
aliguori002437c2009-02-05 22:36:20 +0000960 }
961
aliguorib6503ed2009-02-05 22:36:28 +0000962 if (version_id >= 5) {
963 n->mac_table.in_use = qemu_get_be32(f);
964 /* MAC_TABLE_ENTRIES may be different from the saved image */
965 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
966 qemu_get_buffer(f, n->mac_table.macs,
967 n->mac_table.in_use * ETH_ALEN);
968 } else if (n->mac_table.in_use) {
Juan Quintelae398d612012-08-29 19:03:09 +0200969 uint8_t *buf = g_malloc0(n->mac_table.in_use);
970 qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN);
971 g_free(buf);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600972 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000973 n->mac_table.in_use = 0;
974 }
975 }
976
aliguorif21c0ed2009-02-05 22:36:32 +0000977 if (version_id >= 6)
978 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
979
Mark McLoughlin3a330132009-10-22 17:43:45 +0100980 if (version_id >= 7) {
981 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100982 error_report("virtio-net: saved image requires vnet_hdr=on");
Mark McLoughlin3a330132009-10-22 17:43:45 +0100983 return -1;
984 }
985
986 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000987 tap_set_offload(n->nic->nc.peer,
Michael S. Tsirkin704a76f2010-01-10 13:52:47 +0200988 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
989 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
990 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
991 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
992 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100993 }
Alex Williamson6c042c12009-06-05 14:46:52 -0600994 }
995
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600996 if (version_id >= 9) {
997 n->mac_table.multi_overflow = qemu_get_byte(f);
998 n->mac_table.uni_overflow = qemu_get_byte(f);
999 }
1000
Alex Williamson015cb162009-06-05 14:47:18 -06001001 if (version_id >= 10) {
1002 n->alluni = qemu_get_byte(f);
1003 n->nomulti = qemu_get_byte(f);
1004 n->nouni = qemu_get_byte(f);
1005 n->nobcast = qemu_get_byte(f);
1006 }
1007
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +01001008 if (version_id >= 11) {
1009 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001010 error_report("virtio-net: saved image requires TUN_F_UFO support");
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +01001011 return -1;
1012 }
1013 }
1014
Alex Williamson2d9aba32009-06-05 14:47:13 -06001015 /* Find the first multicast entry in the saved MAC filter */
1016 for (i = 0; i < n->mac_table.in_use; i++) {
1017 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
1018 break;
1019 }
1020 }
1021 n->mac_table.first_multi = i;
Amos Kong98991482012-09-28 10:06:02 +08001022
1023 /* nc.link_down can't be migrated, so infer link_down according
1024 * to link status bit in n->status */
1025 n->nic->nc.link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
1026
aliguorifbe78f42008-12-17 19:13:11 +00001027 return 0;
1028}
1029
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +01001030static void virtio_net_cleanup(NetClientState *nc)
aliguorib946a152009-04-17 17:11:08 +00001031{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001032 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorib946a152009-04-17 17:11:08 +00001033
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001034 n->nic = NULL;
aliguorib946a152009-04-17 17:11:08 +00001035}
1036
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001037static NetClientInfo net_virtio_info = {
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001038 .type = NET_CLIENT_OPTIONS_KIND_NIC,
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001039 .size = sizeof(NICState),
1040 .can_receive = virtio_net_can_receive,
1041 .receive = virtio_net_receive,
1042 .cleanup = virtio_net_cleanup,
1043 .link_status_changed = virtio_net_set_link_status,
1044};
1045
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001046static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
1047{
1048 VirtIONet *n = to_virtio_net(vdev);
1049 assert(n->vhost_started);
1050 return vhost_net_virtqueue_pending(tap_get_vhost_net(n->nic->nc.peer), idx);
1051}
1052
1053static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
1054 bool mask)
1055{
1056 VirtIONet *n = to_virtio_net(vdev);
1057 assert(n->vhost_started);
1058 vhost_net_virtqueue_mask(tap_get_vhost_net(n->nic->nc.peer),
1059 vdev, idx, mask);
1060}
1061
Alex Williamsonf0c07c72010-09-02 09:00:50 -06001062VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
1063 virtio_net_conf *net)
aliguorifbe78f42008-12-17 19:13:11 +00001064{
1065 VirtIONet *n;
aliguorifbe78f42008-12-17 19:13:11 +00001066
Paul Brook53c25ce2009-05-18 14:51:59 +01001067 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
1068 sizeof(struct virtio_net_config),
1069 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +00001070
aliguori0f03eca2009-02-05 22:36:08 +00001071 n->vdev.get_config = virtio_net_get_config;
1072 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +00001073 n->vdev.get_features = virtio_net_get_features;
1074 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +00001075 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +00001076 n->vdev.reset = virtio_net_reset;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001077 n->vdev.set_status = virtio_net_set_status;
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001078 n->vdev.guest_notifier_mask = virtio_net_guest_notifier_mask;
1079 n->vdev.guest_notifier_pending = virtio_net_guest_notifier_pending;
aliguorifbe78f42008-12-17 19:13:11 +00001080 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
Alex Williamsona697a332010-09-02 09:01:10 -06001081
1082 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +00001083 error_report("virtio-net: "
1084 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1085 net->tx);
1086 error_report("Defaulting to \"bh\"");
Alex Williamsona697a332010-09-02 09:01:10 -06001087 }
1088
1089 if (net->tx && !strcmp(net->tx, "timer")) {
1090 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
Paolo Bonzini74475452011-03-11 16:47:48 +01001091 n->tx_timer = qemu_new_timer_ns(vm_clock, virtio_net_tx_timer, n);
Alex Williamsona697a332010-09-02 09:01:10 -06001092 n->tx_timeout = net->txtimer;
1093 } else {
1094 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
1095 n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
1096 }
Alex Williamson4ffb17f2009-06-05 14:47:23 -06001097 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001098 qemu_macaddr_default_if_unset(&conf->macaddr);
Mark McLoughlin3cbe04c2009-10-28 14:07:23 +00001099 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
aliguori554c97d2009-01-08 19:46:33 +00001100 n->status = VIRTIO_NET_S_LINK_UP;
aliguorifbe78f42008-12-17 19:13:11 +00001101
Anthony Liguorif79f2bf2011-12-04 11:17:51 -06001102 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 +02001103 peer_test_vnet_hdr(n);
1104 if (peer_has_vnet_hdr(n)) {
Jason Wangec45f082013-01-30 19:12:20 +08001105 tap_using_vnet_hdr(n->nic->nc.peer, true);
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +02001106 n->host_hdr_len = sizeof(struct virtio_net_hdr);
1107 } else {
1108 n->host_hdr_len = 0;
1109 }
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001110
1111 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
aliguori96d5e202009-01-07 17:47:15 +00001112
Alex Williamson4b4b8d32010-09-02 09:01:04 -06001113 n->tx_waiting = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -06001114 n->tx_burst = net->txburst;
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +02001115 virtio_net_set_mrg_rx_bufs(n, 0);
aliguori002437c2009-02-05 22:36:20 +00001116 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +00001117
Anthony Liguori7267c092011-08-20 22:09:37 -05001118 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +00001119
Anthony Liguori7267c092011-08-20 22:09:37 -05001120 n->vlans = g_malloc0(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +00001121
Alex Williamson01657c82010-06-25 11:09:28 -06001122 n->qdev = dev;
1123 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +00001124 virtio_net_save, virtio_net_load, n);
Paul Brookcf21e102009-05-14 22:35:07 +01001125
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001126 add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0");
1127
Paul Brook53c25ce2009-05-18 14:51:59 +01001128 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +01001129}
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001130
1131void virtio_net_exit(VirtIODevice *vdev)
1132{
1133 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001134
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +02001135 /* This will stop vhost backend if appropriate. */
1136 virtio_net_set_status(vdev, 0);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001137
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001138 qemu_purge_queued_packets(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001139
Alex Williamson01657c82010-06-25 11:09:28 -06001140 unregister_savevm(n->qdev, "virtio-net", n);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001141
Anthony Liguori7267c092011-08-20 22:09:37 -05001142 g_free(n->mac_table.macs);
1143 g_free(n->vlans);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001144
Alex Williamsona697a332010-09-02 09:01:10 -06001145 if (n->tx_timer) {
1146 qemu_del_timer(n->tx_timer);
1147 qemu_free_timer(n->tx_timer);
1148 } else {
1149 qemu_bh_delete(n->tx_bh);
1150 }
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001151
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001152 qemu_del_net_client(&n->nic->nc);
Amit Shahb52dfd72011-07-27 14:00:31 +05301153 virtio_cleanup(&n->vdev);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001154}