blob: 50ba728c021f4c403b5bd15e14fc7c35689bed46 [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
Amit Shahe4d56392010-04-27 18:04:05 +053014#include "iov.h"
aliguorifbe78f42008-12-17 19:13:11 +000015#include "virtio.h"
16#include "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"
Markus Armbruster2f792012010-02-18 16:24:31 +010019#include "qemu-error.h"
aliguorifbe78f42008-12-17 19:13:11 +000020#include "qemu-timer.h"
21#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;
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010044 uint8_t has_ufo;
Mark McLoughlin62433752009-06-18 18:21:36 +010045 struct {
46 VirtQueueElement elem;
47 ssize_t len;
48 } async_tx;
aliguorifbe78f42008-12-17 19:13:11 +000049 int mergeable_rx_bufs;
Alex Williamsonf10c5922009-06-05 14:46:57 -060050 uint8_t promisc;
51 uint8_t allmulti;
Alex Williamson015cb162009-06-05 14:47:18 -060052 uint8_t alluni;
53 uint8_t nomulti;
54 uint8_t nouni;
55 uint8_t nobcast;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +020056 uint8_t vhost_started;
aliguorib6503ed2009-02-05 22:36:28 +000057 struct {
58 int in_use;
Alex Williamson2d9aba32009-06-05 14:47:13 -060059 int first_multi;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -060060 uint8_t multi_overflow;
61 uint8_t uni_overflow;
aliguorib6503ed2009-02-05 22:36:28 +000062 uint8_t *macs;
63 } mac_table;
aliguorif21c0ed2009-02-05 22:36:32 +000064 uint32_t *vlans;
Alex Williamson01657c82010-06-25 11:09:28 -060065 DeviceState *qdev;
aliguorifbe78f42008-12-17 19:13:11 +000066} VirtIONet;
67
68/* TODO
69 * - we could suppress RX interrupt if we were so inclined.
70 */
71
72static VirtIONet *to_virtio_net(VirtIODevice *vdev)
73{
74 return (VirtIONet *)vdev;
75}
76
aliguori0f03eca2009-02-05 22:36:08 +000077static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
aliguorifbe78f42008-12-17 19:13:11 +000078{
79 VirtIONet *n = to_virtio_net(vdev);
80 struct virtio_net_config netcfg;
81
Stefan Hajnoczib46d97f2011-03-03 21:42:28 +000082 stw_p(&netcfg.status, n->status);
aliguori79674062009-02-05 22:36:12 +000083 memcpy(netcfg.mac, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +000084 memcpy(config, &netcfg, sizeof(netcfg));
85}
86
aliguori0f03eca2009-02-05 22:36:08 +000087static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
88{
89 VirtIONet *n = to_virtio_net(vdev);
90 struct virtio_net_config netcfg;
91
92 memcpy(&netcfg, config, sizeof(netcfg));
93
aliguori79674062009-02-05 22:36:12 +000094 if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
95 memcpy(n->mac, netcfg.mac, ETH_ALEN);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000096 qemu_format_nic_info_str(&n->nic->nc, n->mac);
aliguori0f03eca2009-02-05 22:36:08 +000097 }
98}
99
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200100static bool virtio_net_started(VirtIONet *n, uint8_t status)
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200101{
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200102 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200103 (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running;
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200104}
105
106static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
107{
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200108 if (!n->nic->nc.peer) {
109 return;
110 }
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200111 if (n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200112 return;
113 }
114
115 if (!tap_get_vhost_net(n->nic->nc.peer)) {
116 return;
117 }
Michael S. Tsirkin32993692011-02-09 18:45:09 +0200118 if (!!n->vhost_started == virtio_net_started(n, status) &&
119 !n->nic->nc.peer->link_down) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200120 return;
121 }
122 if (!n->vhost_started) {
mst@redhat.com5430a282011-02-01 22:13:42 +0200123 int r;
124 if (!vhost_net_query(tap_get_vhost_net(n->nic->nc.peer), &n->vdev)) {
125 return;
126 }
127 r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200128 if (r < 0) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000129 error_report("unable to start vhost net: %d: "
130 "falling back on userspace virtio", -r);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200131 } else {
132 n->vhost_started = 1;
133 }
134 } else {
135 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
136 n->vhost_started = 0;
137 }
138}
139
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200140static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
141{
142 VirtIONet *n = to_virtio_net(vdev);
143
144 virtio_net_vhost_status(n, status);
145
146 if (!n->tx_waiting) {
147 return;
148 }
149
150 if (virtio_net_started(n, status) && !n->vhost_started) {
151 if (n->tx_timer) {
152 qemu_mod_timer(n->tx_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100153 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200154 } else {
155 qemu_bh_schedule(n->tx_bh);
156 }
157 } else {
158 if (n->tx_timer) {
159 qemu_del_timer(n->tx_timer);
160 } else {
161 qemu_bh_cancel(n->tx_bh);
162 }
163 }
164}
165
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100166static void virtio_net_set_link_status(NetClientState *nc)
aliguori554c97d2009-01-08 19:46:33 +0000167{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000168 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguori554c97d2009-01-08 19:46:33 +0000169 uint16_t old_status = n->status;
170
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000171 if (nc->link_down)
aliguori554c97d2009-01-08 19:46:33 +0000172 n->status &= ~VIRTIO_NET_S_LINK_UP;
173 else
174 n->status |= VIRTIO_NET_S_LINK_UP;
175
176 if (n->status != old_status)
177 virtio_notify_config(&n->vdev);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200178
179 virtio_net_set_status(&n->vdev, n->vdev.status);
aliguori554c97d2009-01-08 19:46:33 +0000180}
181
aliguori002437c2009-02-05 22:36:20 +0000182static void virtio_net_reset(VirtIODevice *vdev)
183{
184 VirtIONet *n = to_virtio_net(vdev);
185
186 /* Reset back to compatibility mode */
187 n->promisc = 1;
188 n->allmulti = 0;
Alex Williamson015cb162009-06-05 14:47:18 -0600189 n->alluni = 0;
190 n->nomulti = 0;
191 n->nouni = 0;
192 n->nobcast = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000193
aliguorif21c0ed2009-02-05 22:36:32 +0000194 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000195 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600196 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600197 n->mac_table.multi_overflow = 0;
198 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000199 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000200 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000201}
202
Mark McLoughlin3a330132009-10-22 17:43:45 +0100203static int peer_has_vnet_hdr(VirtIONet *n)
204{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000205 if (!n->nic->nc.peer)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100206 return 0;
207
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200208 if (n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100209 return 0;
210
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000211 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100212
213 return n->has_vnet_hdr;
214}
215
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100216static int peer_has_ufo(VirtIONet *n)
217{
218 if (!peer_has_vnet_hdr(n))
219 return 0;
220
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000221 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100222
223 return n->has_ufo;
224}
225
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200226static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
aliguorifbe78f42008-12-17 19:13:11 +0000227{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100228 VirtIONet *n = to_virtio_net(vdev);
aliguorifbe78f42008-12-17 19:13:11 +0000229
Michael S. Tsirkinc9f79a32010-01-12 20:50:17 +0200230 features |= (1 << VIRTIO_NET_F_MAC);
231
Mark McLoughlin3a330132009-10-22 17:43:45 +0100232 if (peer_has_vnet_hdr(n)) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000233 tap_using_vnet_hdr(n->nic->nc.peer, 1);
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200234 } else {
235 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
236 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
237 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
238 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100239
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200240 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
241 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
242 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
243 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
244 }
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100245
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200246 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
247 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
248 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100249 }
250
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200251 if (!n->nic->nc.peer ||
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200252 n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200253 return features;
254 }
255 if (!tap_get_vhost_net(n->nic->nc.peer)) {
256 return features;
257 }
258 return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000259}
260
aliguori8eca6b12009-04-05 17:40:08 +0000261static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
262{
263 uint32_t features = 0;
264
265 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
266 * but also these: */
267 features |= (1 << VIRTIO_NET_F_MAC);
Dustin Kirkland184bd042009-10-29 10:34:15 -0500268 features |= (1 << VIRTIO_NET_F_CSUM);
269 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
270 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
271 features |= (1 << VIRTIO_NET_F_HOST_ECN);
aliguori8eca6b12009-04-05 17:40:08 +0000272
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200273 return features;
aliguori8eca6b12009-04-05 17:40:08 +0000274}
275
aliguorifbe78f42008-12-17 19:13:11 +0000276static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
277{
278 VirtIONet *n = to_virtio_net(vdev);
279
280 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100281
282 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000283 tap_set_offload(n->nic->nc.peer,
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100284 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
285 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
286 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100287 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
288 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100289 }
David L Stevensdc14a392010-03-31 21:20:31 +0300290 if (!n->nic->nc.peer ||
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200291 n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
David L Stevensdc14a392010-03-31 21:20:31 +0300292 return;
293 }
294 if (!tap_get_vhost_net(n->nic->nc.peer)) {
295 return;
296 }
Michael S. Tsirkin57c32292010-05-09 14:35:43 +0300297 vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000298}
299
aliguori002437c2009-02-05 22:36:20 +0000300static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
301 VirtQueueElement *elem)
302{
303 uint8_t on;
304
305 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000306 error_report("virtio-net ctrl invalid rx mode command");
aliguori002437c2009-02-05 22:36:20 +0000307 exit(1);
308 }
309
310 on = ldub_p(elem->out_sg[1].iov_base);
311
312 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
313 n->promisc = on;
314 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
315 n->allmulti = on;
Alex Williamson015cb162009-06-05 14:47:18 -0600316 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
317 n->alluni = on;
318 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
319 n->nomulti = on;
320 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
321 n->nouni = on;
322 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
323 n->nobcast = on;
aliguori002437c2009-02-05 22:36:20 +0000324 else
325 return VIRTIO_NET_ERR;
326
327 return VIRTIO_NET_OK;
328}
329
aliguorib6503ed2009-02-05 22:36:28 +0000330static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
331 VirtQueueElement *elem)
332{
333 struct virtio_net_ctrl_mac mac_data;
334
335 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
336 elem->out_sg[1].iov_len < sizeof(mac_data) ||
337 elem->out_sg[2].iov_len < sizeof(mac_data))
338 return VIRTIO_NET_ERR;
339
340 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600341 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600342 n->mac_table.uni_overflow = 0;
343 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000344 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
345
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100346 mac_data.entries = ldl_p(elem->out_sg[1].iov_base);
aliguorib6503ed2009-02-05 22:36:28 +0000347
348 if (sizeof(mac_data.entries) +
349 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
350 return VIRTIO_NET_ERR;
351
352 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
353 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
354 mac_data.entries * ETH_ALEN);
355 n->mac_table.in_use += mac_data.entries;
356 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600357 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000358 }
359
Alex Williamson2d9aba32009-06-05 14:47:13 -0600360 n->mac_table.first_multi = n->mac_table.in_use;
361
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100362 mac_data.entries = ldl_p(elem->out_sg[2].iov_base);
aliguorib6503ed2009-02-05 22:36:28 +0000363
364 if (sizeof(mac_data.entries) +
365 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
366 return VIRTIO_NET_ERR;
367
368 if (mac_data.entries) {
369 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
370 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
371 elem->out_sg[2].iov_base + sizeof(mac_data),
372 mac_data.entries * ETH_ALEN);
373 n->mac_table.in_use += mac_data.entries;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600374 } else {
375 n->mac_table.multi_overflow = 1;
376 }
aliguorib6503ed2009-02-05 22:36:28 +0000377 }
378
379 return VIRTIO_NET_OK;
380}
381
aliguorif21c0ed2009-02-05 22:36:32 +0000382static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
383 VirtQueueElement *elem)
384{
385 uint16_t vid;
386
387 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000388 error_report("virtio-net ctrl invalid vlan command");
aliguorif21c0ed2009-02-05 22:36:32 +0000389 return VIRTIO_NET_ERR;
390 }
391
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100392 vid = lduw_p(elem->out_sg[1].iov_base);
aliguorif21c0ed2009-02-05 22:36:32 +0000393
394 if (vid >= MAX_VLAN)
395 return VIRTIO_NET_ERR;
396
397 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
398 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
399 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
400 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
401 else
402 return VIRTIO_NET_ERR;
403
404 return VIRTIO_NET_OK;
405}
406
aliguori3d11d362009-02-05 22:36:16 +0000407static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
408{
aliguori002437c2009-02-05 22:36:20 +0000409 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000410 struct virtio_net_ctrl_hdr ctrl;
411 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
412 VirtQueueElement elem;
413
414 while (virtqueue_pop(vq, &elem)) {
415 if ((elem.in_num < 1) || (elem.out_num < 1)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000416 error_report("virtio-net ctrl missing headers");
aliguori3d11d362009-02-05 22:36:16 +0000417 exit(1);
418 }
419
420 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000421 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000422 error_report("virtio-net ctrl header not in correct element");
aliguori3d11d362009-02-05 22:36:16 +0000423 exit(1);
424 }
425
426 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
427 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
428
aliguori002437c2009-02-05 22:36:20 +0000429 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
430 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000431 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
432 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000433 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
434 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000435
aliguori3d11d362009-02-05 22:36:16 +0000436 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
437
438 virtqueue_push(vq, &elem, sizeof(status));
439 virtio_notify(vdev, vq);
440 }
441}
442
aliguorifbe78f42008-12-17 19:13:11 +0000443/* RX */
444
445static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
446{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100447 VirtIONet *n = to_virtio_net(vdev);
448
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000449 qemu_flush_queued_packets(&n->nic->nc);
aliguorifbe78f42008-12-17 19:13:11 +0000450}
451
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100452static int virtio_net_can_receive(NetClientState *nc)
aliguorifbe78f42008-12-17 19:13:11 +0000453{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000454 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200455 if (!n->vdev.vm_running) {
Michael S. Tsirkin95477322010-11-22 19:52:19 +0200456 return 0;
457 }
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000458
aliguorifbe78f42008-12-17 19:13:11 +0000459 if (!virtio_queue_ready(n->rx_vq) ||
460 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
461 return 0;
462
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000463 return 1;
464}
465
466static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
467{
aliguorifbe78f42008-12-17 19:13:11 +0000468 if (virtio_queue_empty(n->rx_vq) ||
469 (n->mergeable_rx_bufs &&
470 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
471 virtio_queue_set_notification(n->rx_vq, 1);
Tom Lendacky06b12972010-02-08 10:10:01 -0600472
473 /* To avoid a race condition where the guest has made some buffers
474 * available after the above check but before notification was
475 * enabled, check for available buffers again.
476 */
477 if (virtio_queue_empty(n->rx_vq) ||
478 (n->mergeable_rx_bufs &&
479 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
480 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000481 }
482
483 virtio_queue_set_notification(n->rx_vq, 0);
484 return 1;
485}
486
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100487/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
488 * it never finds out that the packets don't have valid checksums. This
489 * causes dhclient to get upset. Fedora's carried a patch for ages to
490 * fix this with Xen but it hasn't appeared in an upstream release of
491 * dhclient yet.
492 *
493 * To avoid breaking existing guests, we catch udp packets and add
494 * checksums. This is terrible but it's better than hacking the guest
495 * kernels.
496 *
497 * N.B. if we introduce a zero-copy API, this operation is no longer free so
498 * we should provide a mechanism to disable it to avoid polluting the host
499 * cache.
500 */
501static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
502 const uint8_t *buf, size_t size)
503{
504 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
505 (size > 27 && size < 1500) && /* normal sized MTU */
506 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
507 (buf[23] == 17) && /* ip.protocol == UDP */
508 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
509 /* FIXME this cast is evil */
510 net_checksum_calculate((uint8_t *)buf, size);
511 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
512 }
513}
514
aliguorifbe78f42008-12-17 19:13:11 +0000515static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000516 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000517{
blueswir13f4cb3d2009-04-13 16:31:01 +0000518 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
aliguorifbe78f42008-12-17 19:13:11 +0000519 int offset = 0;
520
521 hdr->flags = 0;
522 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
523
Mark McLoughlin3a330132009-10-22 17:43:45 +0100524 if (n->has_vnet_hdr) {
525 memcpy(hdr, buf, sizeof(*hdr));
526 offset = sizeof(*hdr);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100527 work_around_broken_dhclient(hdr, buf + offset, size - offset);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100528 }
529
aliguorifbe78f42008-12-17 19:13:11 +0000530 /* We only ever receive a struct virtio_net_hdr from the tapfd,
531 * but we may be passing along a larger header to the guest.
532 */
533 iov[0].iov_base += hdr_len;
534 iov[0].iov_len -= hdr_len;
535
536 return offset;
537}
538
aliguori3831ab22009-02-05 22:36:24 +0000539static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
540{
541 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000542 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000543 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000544 int i;
aliguori3831ab22009-02-05 22:36:24 +0000545
546 if (n->promisc)
547 return 1;
548
Mark McLoughlin3a330132009-10-22 17:43:45 +0100549 if (n->has_vnet_hdr) {
550 ptr += sizeof(struct virtio_net_hdr);
551 }
552
aliguorif21c0ed2009-02-05 22:36:32 +0000553 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
554 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
555 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
556 return 0;
557 }
558
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600559 if (ptr[0] & 1) { // multicast
560 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600561 return !n->nobcast;
562 } else if (n->nomulti) {
563 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600564 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600565 return 1;
566 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600567
568 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
569 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
570 return 1;
571 }
572 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600573 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600574 if (n->nouni) {
575 return 0;
576 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600577 return 1;
578 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600579 return 1;
580 }
aliguori3831ab22009-02-05 22:36:24 +0000581
Alex Williamson2d9aba32009-06-05 14:47:13 -0600582 for (i = 0; i < n->mac_table.first_multi; i++) {
583 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
584 return 1;
585 }
586 }
aliguorib6503ed2009-02-05 22:36:28 +0000587 }
588
aliguori3831ab22009-02-05 22:36:24 +0000589 return 0;
590}
591
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100592static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000593{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000594 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000595 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300596 size_t guest_hdr_len, offset, i, host_hdr_len;
aliguorifbe78f42008-12-17 19:13:11 +0000597
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000598 if (!virtio_net_can_receive(&n->nic->nc))
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000599 return -1;
600
Michael S. Tsirkin940cda92010-06-06 18:53:10 +0300601 /* hdr_len refers to the header we supply to the guest */
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300602 guest_hdr_len = n->mergeable_rx_bufs ?
Michael S. Tsirkin940cda92010-06-06 18:53:10 +0300603 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
604
605
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300606 host_hdr_len = n->has_vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
607 if (!virtio_net_has_buffers(n, size + guest_hdr_len - host_hdr_len))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100608 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000609
aliguori3831ab22009-02-05 22:36:24 +0000610 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100611 return size;
aliguori3831ab22009-02-05 22:36:24 +0000612
aliguorifbe78f42008-12-17 19:13:11 +0000613 offset = i = 0;
614
615 while (offset < size) {
616 VirtQueueElement elem;
617 int len, total;
618 struct iovec sg[VIRTQUEUE_MAX_SIZE];
619
Amit Shah22c253d2010-01-13 16:24:43 +0530620 total = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000621
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300622 if (virtqueue_pop(n->rx_vq, &elem) == 0) {
aliguorifbe78f42008-12-17 19:13:11 +0000623 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100624 return -1;
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000625 error_report("virtio-net unexpected empty queue: "
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300626 "i %zd mergeable %d offset %zd, size %zd, "
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000627 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300628 i, n->mergeable_rx_bufs, offset, size,
629 guest_hdr_len, host_hdr_len, n->vdev.guest_features);
aliguorifbe78f42008-12-17 19:13:11 +0000630 exit(1);
631 }
632
633 if (elem.in_num < 1) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000634 error_report("virtio-net receive queue contains no in buffers");
aliguorifbe78f42008-12-17 19:13:11 +0000635 exit(1);
636 }
637
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300638 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != guest_hdr_len) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000639 error_report("virtio-net header not in first element");
aliguorifbe78f42008-12-17 19:13:11 +0000640 exit(1);
641 }
642
643 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
644
645 if (i == 0) {
646 if (n->mergeable_rx_bufs)
647 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
648
649 offset += receive_header(n, sg, elem.in_num,
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300650 buf + offset, size - offset, guest_hdr_len);
651 total += guest_hdr_len;
aliguorifbe78f42008-12-17 19:13:11 +0000652 }
653
654 /* copy in packet. ugh */
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400655 len = iov_from_buf(sg, elem.in_num, 0,
656 buf + offset, size - offset);
aliguorifbe78f42008-12-17 19:13:11 +0000657 total += len;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300658 offset += len;
659 /* If buffers can't be merged, at this point we
660 * must have consumed the complete packet.
661 * Otherwise, drop it. */
662 if (!n->mergeable_rx_bufs && offset < size) {
663#if 0
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000664 error_report("virtio-net truncated non-mergeable packet: "
665 "i %zd mergeable %d offset %zd, size %zd, "
666 "guest hdr len %zd, host hdr len %zd",
667 i, n->mergeable_rx_bufs,
668 offset, size, guest_hdr_len, host_hdr_len);
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300669#endif
670 return size;
671 }
aliguorifbe78f42008-12-17 19:13:11 +0000672
673 /* signal other side */
674 virtqueue_fill(n->rx_vq, &elem, total, i++);
aliguorifbe78f42008-12-17 19:13:11 +0000675 }
676
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100677 if (mhdr) {
Stefan Hajnoczib46d97f2011-03-03 21:42:28 +0000678 stw_p(&mhdr->num_buffers, i);
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100679 }
aliguorifbe78f42008-12-17 19:13:11 +0000680
681 virtqueue_flush(n->rx_vq, i);
682 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100683
684 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000685}
686
Alex Williamsone3f30482010-09-02 09:00:57 -0600687static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
Mark McLoughlin62433752009-06-18 18:21:36 +0100688
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100689static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
Mark McLoughlin62433752009-06-18 18:21:36 +0100690{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000691 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlin62433752009-06-18 18:21:36 +0100692
Michael S. Tsirkin40bad8f2012-09-24 15:15:43 +0200693 virtqueue_push(n->tx_vq, &n->async_tx.elem, 0);
Mark McLoughlin62433752009-06-18 18:21:36 +0100694 virtio_notify(&n->vdev, n->tx_vq);
695
696 n->async_tx.elem.out_num = n->async_tx.len = 0;
697
698 virtio_queue_set_notification(n->tx_vq, 1);
699 virtio_net_flush_tx(n, n->tx_vq);
700}
701
aliguorifbe78f42008-12-17 19:13:11 +0000702/* TX */
Alex Williamsone3f30482010-09-02 09:00:57 -0600703static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000704{
705 VirtQueueElement elem;
Alex Williamsone3f30482010-09-02 09:00:57 -0600706 int32_t num_packets = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -0600707 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
708 return num_packets;
709 }
aliguorifbe78f42008-12-17 19:13:11 +0000710
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200711 assert(n->vdev.vm_running);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200712
Mark McLoughlin62433752009-06-18 18:21:36 +0100713 if (n->async_tx.elem.out_num) {
714 virtio_queue_set_notification(n->tx_vq, 0);
Alex Williamsone3f30482010-09-02 09:00:57 -0600715 return num_packets;
Mark McLoughlin62433752009-06-18 18:21:36 +0100716 }
717
aliguorifbe78f42008-12-17 19:13:11 +0000718 while (virtqueue_pop(vq, &elem)) {
Mark McLoughlin62433752009-06-18 18:21:36 +0100719 ssize_t ret, len = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000720 unsigned int out_num = elem.out_num;
721 struct iovec *out_sg = &elem.out_sg[0];
722 unsigned hdr_len;
723
724 /* hdr_len refers to the header received from the guest */
725 hdr_len = n->mergeable_rx_bufs ?
726 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
727 sizeof(struct virtio_net_hdr);
728
729 if (out_num < 1 || out_sg->iov_len != hdr_len) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000730 error_report("virtio-net header not in first element");
aliguorifbe78f42008-12-17 19:13:11 +0000731 exit(1);
732 }
733
734 /* ignore the header if GSO is not supported */
Mark McLoughlin3a330132009-10-22 17:43:45 +0100735 if (!n->has_vnet_hdr) {
aliguorifbe78f42008-12-17 19:13:11 +0000736 out_num--;
737 out_sg++;
738 len += hdr_len;
739 } else if (n->mergeable_rx_bufs) {
740 /* tapfd expects a struct virtio_net_hdr */
741 hdr_len -= sizeof(struct virtio_net_hdr);
742 out_sg->iov_len -= hdr_len;
743 len += hdr_len;
744 }
745
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000746 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
Mark McLoughlin62433752009-06-18 18:21:36 +0100747 virtio_net_tx_complete);
748 if (ret == 0) {
749 virtio_queue_set_notification(n->tx_vq, 0);
750 n->async_tx.elem = elem;
751 n->async_tx.len = len;
Alex Williamsone3f30482010-09-02 09:00:57 -0600752 return -EBUSY;
Mark McLoughlin62433752009-06-18 18:21:36 +0100753 }
754
755 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000756
Michael S. Tsirkin40bad8f2012-09-24 15:15:43 +0200757 virtqueue_push(vq, &elem, 0);
aliguorifbe78f42008-12-17 19:13:11 +0000758 virtio_notify(&n->vdev, vq);
Alex Williamsone3f30482010-09-02 09:00:57 -0600759
760 if (++num_packets >= n->tx_burst) {
761 break;
762 }
aliguorifbe78f42008-12-17 19:13:11 +0000763 }
Alex Williamsone3f30482010-09-02 09:00:57 -0600764 return num_packets;
aliguorifbe78f42008-12-17 19:13:11 +0000765}
766
Alex Williamsona697a332010-09-02 09:01:10 -0600767static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000768{
769 VirtIONet *n = to_virtio_net(vdev);
770
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200771 /* This happens when device was stopped but VCPU wasn't. */
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200772 if (!n->vdev.vm_running) {
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200773 n->tx_waiting = 1;
774 return;
775 }
776
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600777 if (n->tx_waiting) {
aliguorifbe78f42008-12-17 19:13:11 +0000778 virtio_queue_set_notification(vq, 1);
779 qemu_del_timer(n->tx_timer);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600780 n->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000781 virtio_net_flush_tx(n, vq);
782 } else {
783 qemu_mod_timer(n->tx_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100784 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600785 n->tx_waiting = 1;
aliguorifbe78f42008-12-17 19:13:11 +0000786 virtio_queue_set_notification(vq, 0);
787 }
788}
789
Alex Williamsona697a332010-09-02 09:01:10 -0600790static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
791{
792 VirtIONet *n = to_virtio_net(vdev);
793
794 if (unlikely(n->tx_waiting)) {
795 return;
796 }
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200797 n->tx_waiting = 1;
798 /* This happens when device was stopped but VCPU wasn't. */
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200799 if (!n->vdev.vm_running) {
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200800 return;
801 }
Alex Williamsona697a332010-09-02 09:01:10 -0600802 virtio_queue_set_notification(vq, 0);
803 qemu_bh_schedule(n->tx_bh);
Alex Williamsona697a332010-09-02 09:01:10 -0600804}
805
aliguorifbe78f42008-12-17 19:13:11 +0000806static void virtio_net_tx_timer(void *opaque)
807{
808 VirtIONet *n = opaque;
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200809 assert(n->vdev.vm_running);
aliguorifbe78f42008-12-17 19:13:11 +0000810
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600811 n->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000812
813 /* Just in case the driver is not ready on more */
814 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
815 return;
816
817 virtio_queue_set_notification(n->tx_vq, 1);
818 virtio_net_flush_tx(n, n->tx_vq);
819}
820
Alex Williamsona697a332010-09-02 09:01:10 -0600821static void virtio_net_tx_bh(void *opaque)
822{
823 VirtIONet *n = opaque;
824 int32_t ret;
825
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200826 assert(n->vdev.vm_running);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200827
Alex Williamsona697a332010-09-02 09:01:10 -0600828 n->tx_waiting = 0;
829
830 /* Just in case the driver is not ready on more */
831 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
832 return;
833
834 ret = virtio_net_flush_tx(n, n->tx_vq);
835 if (ret == -EBUSY) {
836 return; /* Notification re-enable handled by tx_complete */
837 }
838
839 /* If we flush a full burst of packets, assume there are
840 * more coming and immediately reschedule */
841 if (ret >= n->tx_burst) {
842 qemu_bh_schedule(n->tx_bh);
843 n->tx_waiting = 1;
844 return;
845 }
846
847 /* If less than a full burst, re-enable notification and flush
848 * anything that may have come in while we weren't looking. If
849 * we find something, assume the guest is still active and reschedule */
850 virtio_queue_set_notification(n->tx_vq, 1);
851 if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
852 virtio_queue_set_notification(n->tx_vq, 0);
853 qemu_bh_schedule(n->tx_bh);
854 n->tx_waiting = 1;
855 }
856}
857
aliguorifbe78f42008-12-17 19:13:11 +0000858static void virtio_net_save(QEMUFile *f, void *opaque)
859{
860 VirtIONet *n = opaque;
861
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200862 /* At this point, backend must be stopped, otherwise
863 * it might keep writing to memory. */
864 assert(!n->vhost_started);
aliguorifbe78f42008-12-17 19:13:11 +0000865 virtio_save(&n->vdev, f);
866
aliguori79674062009-02-05 22:36:12 +0000867 qemu_put_buffer(f, n->mac, ETH_ALEN);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600868 qemu_put_be32(f, n->tx_waiting);
aliguorie46cb382009-01-07 17:50:45 +0000869 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000870 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600871 qemu_put_byte(f, n->promisc);
872 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000873 qemu_put_be32(f, n->mac_table.in_use);
874 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000875 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100876 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600877 qemu_put_byte(f, n->mac_table.multi_overflow);
878 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600879 qemu_put_byte(f, n->alluni);
880 qemu_put_byte(f, n->nomulti);
881 qemu_put_byte(f, n->nouni);
882 qemu_put_byte(f, n->nobcast);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100883 qemu_put_byte(f, n->has_ufo);
aliguorifbe78f42008-12-17 19:13:11 +0000884}
885
886static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
887{
888 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600889 int i;
Orit Wassermann2a633c42012-05-16 12:21:35 +0200890 int ret;
aliguorifbe78f42008-12-17 19:13:11 +0000891
aliguori9d6271b2009-02-05 22:36:04 +0000892 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000893 return -EINVAL;
894
Orit Wassermann2a633c42012-05-16 12:21:35 +0200895 ret = virtio_load(&n->vdev, f);
896 if (ret) {
897 return ret;
898 }
aliguorifbe78f42008-12-17 19:13:11 +0000899
aliguori79674062009-02-05 22:36:12 +0000900 qemu_get_buffer(f, n->mac, ETH_ALEN);
Alex Williamson4b4b8d32010-09-02 09:01:04 -0600901 n->tx_waiting = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000902 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000903
aliguori9d6271b2009-02-05 22:36:04 +0000904 if (version_id >= 3)
905 n->status = qemu_get_be16(f);
906
aliguori002437c2009-02-05 22:36:20 +0000907 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600908 if (version_id < 8) {
909 n->promisc = qemu_get_be32(f);
910 n->allmulti = qemu_get_be32(f);
911 } else {
912 n->promisc = qemu_get_byte(f);
913 n->allmulti = qemu_get_byte(f);
914 }
aliguori002437c2009-02-05 22:36:20 +0000915 }
916
aliguorib6503ed2009-02-05 22:36:28 +0000917 if (version_id >= 5) {
918 n->mac_table.in_use = qemu_get_be32(f);
919 /* MAC_TABLE_ENTRIES may be different from the saved image */
920 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
921 qemu_get_buffer(f, n->mac_table.macs,
922 n->mac_table.in_use * ETH_ALEN);
923 } else if (n->mac_table.in_use) {
Juan Quintelae398d612012-08-29 19:03:09 +0200924 uint8_t *buf = g_malloc0(n->mac_table.in_use);
925 qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN);
926 g_free(buf);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600927 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000928 n->mac_table.in_use = 0;
929 }
930 }
931
aliguorif21c0ed2009-02-05 22:36:32 +0000932 if (version_id >= 6)
933 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
934
Mark McLoughlin3a330132009-10-22 17:43:45 +0100935 if (version_id >= 7) {
936 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100937 error_report("virtio-net: saved image requires vnet_hdr=on");
Mark McLoughlin3a330132009-10-22 17:43:45 +0100938 return -1;
939 }
940
941 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000942 tap_using_vnet_hdr(n->nic->nc.peer, 1);
943 tap_set_offload(n->nic->nc.peer,
Michael S. Tsirkin704a76f2010-01-10 13:52:47 +0200944 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
945 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
946 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
947 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
948 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100949 }
Alex Williamson6c042c12009-06-05 14:46:52 -0600950 }
951
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600952 if (version_id >= 9) {
953 n->mac_table.multi_overflow = qemu_get_byte(f);
954 n->mac_table.uni_overflow = qemu_get_byte(f);
955 }
956
Alex Williamson015cb162009-06-05 14:47:18 -0600957 if (version_id >= 10) {
958 n->alluni = qemu_get_byte(f);
959 n->nomulti = qemu_get_byte(f);
960 n->nouni = qemu_get_byte(f);
961 n->nobcast = qemu_get_byte(f);
962 }
963
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100964 if (version_id >= 11) {
965 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100966 error_report("virtio-net: saved image requires TUN_F_UFO support");
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100967 return -1;
968 }
969 }
970
Alex Williamson2d9aba32009-06-05 14:47:13 -0600971 /* Find the first multicast entry in the saved MAC filter */
972 for (i = 0; i < n->mac_table.in_use; i++) {
973 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
974 break;
975 }
976 }
977 n->mac_table.first_multi = i;
Amos Kong98991482012-09-28 10:06:02 +0800978
979 /* nc.link_down can't be migrated, so infer link_down according
980 * to link status bit in n->status */
981 n->nic->nc.link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
982
aliguorifbe78f42008-12-17 19:13:11 +0000983 return 0;
984}
985
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100986static void virtio_net_cleanup(NetClientState *nc)
aliguorib946a152009-04-17 17:11:08 +0000987{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000988 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorib946a152009-04-17 17:11:08 +0000989
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000990 n->nic = NULL;
aliguorib946a152009-04-17 17:11:08 +0000991}
992
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000993static NetClientInfo net_virtio_info = {
Laszlo Ersek2be64a62012-07-17 16:17:12 +0200994 .type = NET_CLIENT_OPTIONS_KIND_NIC,
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000995 .size = sizeof(NICState),
996 .can_receive = virtio_net_can_receive,
997 .receive = virtio_net_receive,
998 .cleanup = virtio_net_cleanup,
999 .link_status_changed = virtio_net_set_link_status,
1000};
1001
Alex Williamsonf0c07c72010-09-02 09:00:50 -06001002VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
1003 virtio_net_conf *net)
aliguorifbe78f42008-12-17 19:13:11 +00001004{
1005 VirtIONet *n;
aliguorifbe78f42008-12-17 19:13:11 +00001006
Paul Brook53c25ce2009-05-18 14:51:59 +01001007 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
1008 sizeof(struct virtio_net_config),
1009 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +00001010
aliguori0f03eca2009-02-05 22:36:08 +00001011 n->vdev.get_config = virtio_net_get_config;
1012 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +00001013 n->vdev.get_features = virtio_net_get_features;
1014 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +00001015 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +00001016 n->vdev.reset = virtio_net_reset;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001017 n->vdev.set_status = virtio_net_set_status;
aliguorifbe78f42008-12-17 19:13:11 +00001018 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
Alex Williamsona697a332010-09-02 09:01:10 -06001019
1020 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +00001021 error_report("virtio-net: "
1022 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1023 net->tx);
1024 error_report("Defaulting to \"bh\"");
Alex Williamsona697a332010-09-02 09:01:10 -06001025 }
1026
1027 if (net->tx && !strcmp(net->tx, "timer")) {
1028 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
Paolo Bonzini74475452011-03-11 16:47:48 +01001029 n->tx_timer = qemu_new_timer_ns(vm_clock, virtio_net_tx_timer, n);
Alex Williamsona697a332010-09-02 09:01:10 -06001030 n->tx_timeout = net->txtimer;
1031 } else {
1032 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
1033 n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
1034 }
Alex Williamson4ffb17f2009-06-05 14:47:23 -06001035 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001036 qemu_macaddr_default_if_unset(&conf->macaddr);
Mark McLoughlin3cbe04c2009-10-28 14:07:23 +00001037 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
aliguori554c97d2009-01-08 19:46:33 +00001038 n->status = VIRTIO_NET_S_LINK_UP;
aliguorifbe78f42008-12-17 19:13:11 +00001039
Anthony Liguorif79f2bf2011-12-04 11:17:51 -06001040 n->nic = qemu_new_nic(&net_virtio_info, conf, object_get_typename(OBJECT(dev)), dev->id, n);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001041
1042 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
aliguori96d5e202009-01-07 17:47:15 +00001043
Alex Williamson4b4b8d32010-09-02 09:01:04 -06001044 n->tx_waiting = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -06001045 n->tx_burst = net->txburst;
aliguorifbe78f42008-12-17 19:13:11 +00001046 n->mergeable_rx_bufs = 0;
aliguori002437c2009-02-05 22:36:20 +00001047 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +00001048
Anthony Liguori7267c092011-08-20 22:09:37 -05001049 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +00001050
Anthony Liguori7267c092011-08-20 22:09:37 -05001051 n->vlans = g_malloc0(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +00001052
Alex Williamson01657c82010-06-25 11:09:28 -06001053 n->qdev = dev;
1054 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +00001055 virtio_net_save, virtio_net_load, n);
Paul Brookcf21e102009-05-14 22:35:07 +01001056
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001057 add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0");
1058
Paul Brook53c25ce2009-05-18 14:51:59 +01001059 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +01001060}
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001061
1062void virtio_net_exit(VirtIODevice *vdev)
1063{
1064 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001065
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +02001066 /* This will stop vhost backend if appropriate. */
1067 virtio_net_set_status(vdev, 0);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001068
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001069 qemu_purge_queued_packets(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001070
Alex Williamson01657c82010-06-25 11:09:28 -06001071 unregister_savevm(n->qdev, "virtio-net", n);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001072
Anthony Liguori7267c092011-08-20 22:09:37 -05001073 g_free(n->mac_table.macs);
1074 g_free(n->vlans);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001075
Alex Williamsona697a332010-09-02 09:01:10 -06001076 if (n->tx_timer) {
1077 qemu_del_timer(n->tx_timer);
1078 qemu_free_timer(n->tx_timer);
1079 } else {
1080 qemu_bh_delete(n->tx_bh);
1081 }
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001082
Stefan Hajnoczib20c6b92012-07-24 16:35:15 +01001083 qemu_del_net_client(&n->nic->nc);
Amit Shahb52dfd72011-07-27 14:00:31 +05301084 virtio_cleanup(&n->vdev);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001085}