blob: 55f3d945f166d4a71b6a671066e14ce87a858dbe [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 Williamsonf0c07c72010-09-02 09:00:50 -060039 uint32_t tx_timeout;
Alex Williamsone3f30482010-09-02 09:00:57 -060040 int32_t tx_burst;
aliguorifbe78f42008-12-17 19:13:11 +000041 int tx_timer_active;
Mark McLoughlin3a330132009-10-22 17:43:45 +010042 uint32_t has_vnet_hdr;
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010043 uint8_t has_ufo;
Mark McLoughlin62433752009-06-18 18:21:36 +010044 struct {
45 VirtQueueElement elem;
46 ssize_t len;
47 } async_tx;
aliguorifbe78f42008-12-17 19:13:11 +000048 int mergeable_rx_bufs;
Alex Williamsonf10c5922009-06-05 14:46:57 -060049 uint8_t promisc;
50 uint8_t allmulti;
Alex Williamson015cb162009-06-05 14:47:18 -060051 uint8_t alluni;
52 uint8_t nomulti;
53 uint8_t nouni;
54 uint8_t nobcast;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +020055 uint8_t vhost_started;
56 VMChangeStateEntry *vmstate;
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
aliguori554c97d2009-01-08 19:46:33 +000082 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
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000100static void virtio_net_set_link_status(VLANClientState *nc)
aliguori554c97d2009-01-08 19:46:33 +0000101{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000102 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguori554c97d2009-01-08 19:46:33 +0000103 uint16_t old_status = n->status;
104
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000105 if (nc->link_down)
aliguori554c97d2009-01-08 19:46:33 +0000106 n->status &= ~VIRTIO_NET_S_LINK_UP;
107 else
108 n->status |= VIRTIO_NET_S_LINK_UP;
109
110 if (n->status != old_status)
111 virtio_notify_config(&n->vdev);
112}
113
aliguori002437c2009-02-05 22:36:20 +0000114static void virtio_net_reset(VirtIODevice *vdev)
115{
116 VirtIONet *n = to_virtio_net(vdev);
117
118 /* Reset back to compatibility mode */
119 n->promisc = 1;
120 n->allmulti = 0;
Alex Williamson015cb162009-06-05 14:47:18 -0600121 n->alluni = 0;
122 n->nomulti = 0;
123 n->nouni = 0;
124 n->nobcast = 0;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200125 if (n->vhost_started) {
126 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), vdev);
127 n->vhost_started = 0;
128 }
aliguorib6503ed2009-02-05 22:36:28 +0000129
aliguorif21c0ed2009-02-05 22:36:32 +0000130 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000131 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600132 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600133 n->mac_table.multi_overflow = 0;
134 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000135 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000136 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000137}
138
Mark McLoughlin3a330132009-10-22 17:43:45 +0100139static int peer_has_vnet_hdr(VirtIONet *n)
140{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000141 if (!n->nic->nc.peer)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100142 return 0;
143
Mark McLoughlin665a3b02009-11-25 18:49:30 +0000144 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100145 return 0;
146
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000147 n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100148
149 return n->has_vnet_hdr;
150}
151
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100152static int peer_has_ufo(VirtIONet *n)
153{
154 if (!peer_has_vnet_hdr(n))
155 return 0;
156
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000157 n->has_ufo = tap_has_ufo(n->nic->nc.peer);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100158
159 return n->has_ufo;
160}
161
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200162static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
aliguorifbe78f42008-12-17 19:13:11 +0000163{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100164 VirtIONet *n = to_virtio_net(vdev);
aliguorifbe78f42008-12-17 19:13:11 +0000165
Michael S. Tsirkinc9f79a32010-01-12 20:50:17 +0200166 features |= (1 << VIRTIO_NET_F_MAC);
167
Mark McLoughlin3a330132009-10-22 17:43:45 +0100168 if (peer_has_vnet_hdr(n)) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000169 tap_using_vnet_hdr(n->nic->nc.peer, 1);
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200170 } else {
171 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
172 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
173 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
174 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100175
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200176 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
177 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
178 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
179 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
180 }
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100181
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200182 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
183 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
184 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100185 }
186
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200187 if (!n->nic->nc.peer ||
188 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
189 return features;
190 }
191 if (!tap_get_vhost_net(n->nic->nc.peer)) {
192 return features;
193 }
194 return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000195}
196
aliguori8eca6b12009-04-05 17:40:08 +0000197static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
198{
199 uint32_t features = 0;
200
201 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
202 * but also these: */
203 features |= (1 << VIRTIO_NET_F_MAC);
Dustin Kirkland184bd042009-10-29 10:34:15 -0500204 features |= (1 << VIRTIO_NET_F_CSUM);
205 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
206 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
207 features |= (1 << VIRTIO_NET_F_HOST_ECN);
aliguori8eca6b12009-04-05 17:40:08 +0000208
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200209 return features;
aliguori8eca6b12009-04-05 17:40:08 +0000210}
211
aliguorifbe78f42008-12-17 19:13:11 +0000212static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
213{
214 VirtIONet *n = to_virtio_net(vdev);
215
216 n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100217
218 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000219 tap_set_offload(n->nic->nc.peer,
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100220 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
221 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
222 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100223 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
224 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100225 }
David L Stevensdc14a392010-03-31 21:20:31 +0300226 if (!n->nic->nc.peer ||
227 n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
228 return;
229 }
230 if (!tap_get_vhost_net(n->nic->nc.peer)) {
231 return;
232 }
Michael S. Tsirkin57c32292010-05-09 14:35:43 +0300233 vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000234}
235
aliguori002437c2009-02-05 22:36:20 +0000236static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
237 VirtQueueElement *elem)
238{
239 uint8_t on;
240
241 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
242 fprintf(stderr, "virtio-net ctrl invalid rx mode command\n");
243 exit(1);
244 }
245
246 on = ldub_p(elem->out_sg[1].iov_base);
247
248 if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
249 n->promisc = on;
250 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
251 n->allmulti = on;
Alex Williamson015cb162009-06-05 14:47:18 -0600252 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
253 n->alluni = on;
254 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
255 n->nomulti = on;
256 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
257 n->nouni = on;
258 else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
259 n->nobcast = on;
aliguori002437c2009-02-05 22:36:20 +0000260 else
261 return VIRTIO_NET_ERR;
262
263 return VIRTIO_NET_OK;
264}
265
aliguorib6503ed2009-02-05 22:36:28 +0000266static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
267 VirtQueueElement *elem)
268{
269 struct virtio_net_ctrl_mac mac_data;
270
271 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
272 elem->out_sg[1].iov_len < sizeof(mac_data) ||
273 elem->out_sg[2].iov_len < sizeof(mac_data))
274 return VIRTIO_NET_ERR;
275
276 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600277 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600278 n->mac_table.uni_overflow = 0;
279 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000280 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
281
282 mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base);
283
284 if (sizeof(mac_data.entries) +
285 (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
286 return VIRTIO_NET_ERR;
287
288 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
289 memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
290 mac_data.entries * ETH_ALEN);
291 n->mac_table.in_use += mac_data.entries;
292 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600293 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000294 }
295
Alex Williamson2d9aba32009-06-05 14:47:13 -0600296 n->mac_table.first_multi = n->mac_table.in_use;
297
aliguorib6503ed2009-02-05 22:36:28 +0000298 mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base);
299
300 if (sizeof(mac_data.entries) +
301 (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
302 return VIRTIO_NET_ERR;
303
304 if (mac_data.entries) {
305 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
306 memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
307 elem->out_sg[2].iov_base + sizeof(mac_data),
308 mac_data.entries * ETH_ALEN);
309 n->mac_table.in_use += mac_data.entries;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600310 } else {
311 n->mac_table.multi_overflow = 1;
312 }
aliguorib6503ed2009-02-05 22:36:28 +0000313 }
314
315 return VIRTIO_NET_OK;
316}
317
aliguorif21c0ed2009-02-05 22:36:32 +0000318static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
319 VirtQueueElement *elem)
320{
321 uint16_t vid;
322
323 if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
324 fprintf(stderr, "virtio-net ctrl invalid vlan command\n");
325 return VIRTIO_NET_ERR;
326 }
327
328 vid = lduw_le_p(elem->out_sg[1].iov_base);
329
330 if (vid >= MAX_VLAN)
331 return VIRTIO_NET_ERR;
332
333 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
334 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
335 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
336 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
337 else
338 return VIRTIO_NET_ERR;
339
340 return VIRTIO_NET_OK;
341}
342
aliguori3d11d362009-02-05 22:36:16 +0000343static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
344{
aliguori002437c2009-02-05 22:36:20 +0000345 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000346 struct virtio_net_ctrl_hdr ctrl;
347 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
348 VirtQueueElement elem;
349
350 while (virtqueue_pop(vq, &elem)) {
351 if ((elem.in_num < 1) || (elem.out_num < 1)) {
352 fprintf(stderr, "virtio-net ctrl missing headers\n");
353 exit(1);
354 }
355
356 if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
aliguoric6bb9a32009-03-13 15:04:02 +0000357 elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
aliguori3d11d362009-02-05 22:36:16 +0000358 fprintf(stderr, "virtio-net ctrl header not in correct element\n");
359 exit(1);
360 }
361
362 ctrl.class = ldub_p(elem.out_sg[0].iov_base);
363 ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
364
aliguori002437c2009-02-05 22:36:20 +0000365 if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
366 status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
aliguorib6503ed2009-02-05 22:36:28 +0000367 else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
368 status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
aliguorif21c0ed2009-02-05 22:36:32 +0000369 else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
370 status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
aliguori002437c2009-02-05 22:36:20 +0000371
aliguori3d11d362009-02-05 22:36:16 +0000372 stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
373
374 virtqueue_push(vq, &elem, sizeof(status));
375 virtio_notify(vdev, vq);
376 }
377}
378
aliguorifbe78f42008-12-17 19:13:11 +0000379/* RX */
380
381static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
382{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100383 VirtIONet *n = to_virtio_net(vdev);
384
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000385 qemu_flush_queued_packets(&n->nic->nc);
Glauber Costaa61d1f62009-07-20 13:07:41 -0400386
387 /* We now have RX buffers, signal to the IO thread to break out of the
388 * select to re-poll the tap file descriptor */
389 qemu_notify_event();
aliguorifbe78f42008-12-17 19:13:11 +0000390}
391
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000392static int virtio_net_can_receive(VLANClientState *nc)
aliguorifbe78f42008-12-17 19:13:11 +0000393{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000394 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000395
aliguorifbe78f42008-12-17 19:13:11 +0000396 if (!virtio_queue_ready(n->rx_vq) ||
397 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
398 return 0;
399
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000400 return 1;
401}
402
403static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
404{
aliguorifbe78f42008-12-17 19:13:11 +0000405 if (virtio_queue_empty(n->rx_vq) ||
406 (n->mergeable_rx_bufs &&
407 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
408 virtio_queue_set_notification(n->rx_vq, 1);
Tom Lendacky06b12972010-02-08 10:10:01 -0600409
410 /* To avoid a race condition where the guest has made some buffers
411 * available after the above check but before notification was
412 * enabled, check for available buffers again.
413 */
414 if (virtio_queue_empty(n->rx_vq) ||
415 (n->mergeable_rx_bufs &&
416 !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
417 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000418 }
419
420 virtio_queue_set_notification(n->rx_vq, 0);
421 return 1;
422}
423
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100424/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
425 * it never finds out that the packets don't have valid checksums. This
426 * causes dhclient to get upset. Fedora's carried a patch for ages to
427 * fix this with Xen but it hasn't appeared in an upstream release of
428 * dhclient yet.
429 *
430 * To avoid breaking existing guests, we catch udp packets and add
431 * checksums. This is terrible but it's better than hacking the guest
432 * kernels.
433 *
434 * N.B. if we introduce a zero-copy API, this operation is no longer free so
435 * we should provide a mechanism to disable it to avoid polluting the host
436 * cache.
437 */
438static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
439 const uint8_t *buf, size_t size)
440{
441 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
442 (size > 27 && size < 1500) && /* normal sized MTU */
443 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
444 (buf[23] == 17) && /* ip.protocol == UDP */
445 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
446 /* FIXME this cast is evil */
447 net_checksum_calculate((uint8_t *)buf, size);
448 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
449 }
450}
451
aliguorifbe78f42008-12-17 19:13:11 +0000452static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt,
aliguori4689f4b2008-12-17 19:45:40 +0000453 const void *buf, size_t size, size_t hdr_len)
aliguorifbe78f42008-12-17 19:13:11 +0000454{
blueswir13f4cb3d2009-04-13 16:31:01 +0000455 struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base;
aliguorifbe78f42008-12-17 19:13:11 +0000456 int offset = 0;
457
458 hdr->flags = 0;
459 hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE;
460
Mark McLoughlin3a330132009-10-22 17:43:45 +0100461 if (n->has_vnet_hdr) {
462 memcpy(hdr, buf, sizeof(*hdr));
463 offset = sizeof(*hdr);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100464 work_around_broken_dhclient(hdr, buf + offset, size - offset);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100465 }
466
aliguorifbe78f42008-12-17 19:13:11 +0000467 /* We only ever receive a struct virtio_net_hdr from the tapfd,
468 * but we may be passing along a larger header to the guest.
469 */
470 iov[0].iov_base += hdr_len;
471 iov[0].iov_len -= hdr_len;
472
473 return offset;
474}
475
aliguori3831ab22009-02-05 22:36:24 +0000476static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
477{
478 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000479 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000480 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000481 int i;
aliguori3831ab22009-02-05 22:36:24 +0000482
483 if (n->promisc)
484 return 1;
485
Mark McLoughlin3a330132009-10-22 17:43:45 +0100486 if (n->has_vnet_hdr) {
487 ptr += sizeof(struct virtio_net_hdr);
488 }
489
aliguorif21c0ed2009-02-05 22:36:32 +0000490 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
491 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
492 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
493 return 0;
494 }
495
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600496 if (ptr[0] & 1) { // multicast
497 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600498 return !n->nobcast;
499 } else if (n->nomulti) {
500 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600501 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600502 return 1;
503 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600504
505 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
506 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
507 return 1;
508 }
509 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600510 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600511 if (n->nouni) {
512 return 0;
513 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600514 return 1;
515 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600516 return 1;
517 }
aliguori3831ab22009-02-05 22:36:24 +0000518
Alex Williamson2d9aba32009-06-05 14:47:13 -0600519 for (i = 0; i < n->mac_table.first_multi; i++) {
520 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
521 return 1;
522 }
523 }
aliguorib6503ed2009-02-05 22:36:28 +0000524 }
525
aliguori3831ab22009-02-05 22:36:24 +0000526 return 0;
527}
528
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000529static ssize_t virtio_net_receive(VLANClientState *nc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000530{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000531 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorifbe78f42008-12-17 19:13:11 +0000532 struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300533 size_t guest_hdr_len, offset, i, host_hdr_len;
aliguorifbe78f42008-12-17 19:13:11 +0000534
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000535 if (!virtio_net_can_receive(&n->nic->nc))
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000536 return -1;
537
Michael S. Tsirkin940cda92010-06-06 18:53:10 +0300538 /* hdr_len refers to the header we supply to the guest */
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300539 guest_hdr_len = n->mergeable_rx_bufs ?
Michael S. Tsirkin940cda92010-06-06 18:53:10 +0300540 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
541
542
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300543 host_hdr_len = n->has_vnet_hdr ? sizeof(struct virtio_net_hdr) : 0;
544 if (!virtio_net_has_buffers(n, size + guest_hdr_len - host_hdr_len))
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100545 return 0;
aliguorifbe78f42008-12-17 19:13:11 +0000546
aliguori3831ab22009-02-05 22:36:24 +0000547 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100548 return size;
aliguori3831ab22009-02-05 22:36:24 +0000549
aliguorifbe78f42008-12-17 19:13:11 +0000550 offset = i = 0;
551
552 while (offset < size) {
553 VirtQueueElement elem;
554 int len, total;
555 struct iovec sg[VIRTQUEUE_MAX_SIZE];
556
Amit Shah22c253d2010-01-13 16:24:43 +0530557 total = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000558
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300559 if (virtqueue_pop(n->rx_vq, &elem) == 0) {
aliguorifbe78f42008-12-17 19:13:11 +0000560 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100561 return -1;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300562 fprintf(stderr, "virtio-net unexpected empty queue: "
563 "i %zd mergeable %d offset %zd, size %zd, "
564 "guest hdr len %zd, host hdr len %zd guest features 0x%x\n",
565 i, n->mergeable_rx_bufs, offset, size,
566 guest_hdr_len, host_hdr_len, n->vdev.guest_features);
aliguorifbe78f42008-12-17 19:13:11 +0000567 exit(1);
568 }
569
570 if (elem.in_num < 1) {
571 fprintf(stderr, "virtio-net receive queue contains no in buffers\n");
572 exit(1);
573 }
574
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300575 if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != guest_hdr_len) {
aliguorifbe78f42008-12-17 19:13:11 +0000576 fprintf(stderr, "virtio-net header not in first element\n");
577 exit(1);
578 }
579
580 memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num);
581
582 if (i == 0) {
583 if (n->mergeable_rx_bufs)
584 mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base;
585
586 offset += receive_header(n, sg, elem.in_num,
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300587 buf + offset, size - offset, guest_hdr_len);
588 total += guest_hdr_len;
aliguorifbe78f42008-12-17 19:13:11 +0000589 }
590
591 /* copy in packet. ugh */
Amit Shahe4d56392010-04-27 18:04:05 +0530592 len = iov_from_buf(sg, elem.in_num,
593 buf + offset, size - offset);
aliguorifbe78f42008-12-17 19:13:11 +0000594 total += len;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300595 offset += len;
596 /* If buffers can't be merged, at this point we
597 * must have consumed the complete packet.
598 * Otherwise, drop it. */
599 if (!n->mergeable_rx_bufs && offset < size) {
600#if 0
601 fprintf(stderr, "virtio-net truncated non-mergeable packet: "
602
603 "i %zd mergeable %d offset %zd, size %zd, "
604 "guest hdr len %zd, host hdr len %zd\n",
605 i, n->mergeable_rx_bufs,
606 offset, size, guest_hdr_len, host_hdr_len);
607#endif
608 return size;
609 }
aliguorifbe78f42008-12-17 19:13:11 +0000610
611 /* signal other side */
612 virtqueue_fill(n->rx_vq, &elem, total, i++);
aliguorifbe78f42008-12-17 19:13:11 +0000613 }
614
615 if (mhdr)
616 mhdr->num_buffers = i;
617
618 virtqueue_flush(n->rx_vq, i);
619 virtio_notify(&n->vdev, n->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100620
621 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000622}
623
Alex Williamsone3f30482010-09-02 09:00:57 -0600624static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
Mark McLoughlin62433752009-06-18 18:21:36 +0100625
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000626static void virtio_net_tx_complete(VLANClientState *nc, ssize_t len)
Mark McLoughlin62433752009-06-18 18:21:36 +0100627{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000628 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
Mark McLoughlin62433752009-06-18 18:21:36 +0100629
630 virtqueue_push(n->tx_vq, &n->async_tx.elem, n->async_tx.len);
631 virtio_notify(&n->vdev, n->tx_vq);
632
633 n->async_tx.elem.out_num = n->async_tx.len = 0;
634
635 virtio_queue_set_notification(n->tx_vq, 1);
636 virtio_net_flush_tx(n, n->tx_vq);
637}
638
aliguorifbe78f42008-12-17 19:13:11 +0000639/* TX */
Alex Williamsone3f30482010-09-02 09:00:57 -0600640static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000641{
642 VirtQueueElement elem;
Alex Williamsone3f30482010-09-02 09:00:57 -0600643 int32_t num_packets = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000644
Alex Williamsone3f30482010-09-02 09:00:57 -0600645 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
646 return num_packets;
647 }
aliguorifbe78f42008-12-17 19:13:11 +0000648
Mark McLoughlin62433752009-06-18 18:21:36 +0100649 if (n->async_tx.elem.out_num) {
650 virtio_queue_set_notification(n->tx_vq, 0);
Alex Williamsone3f30482010-09-02 09:00:57 -0600651 return num_packets;
Mark McLoughlin62433752009-06-18 18:21:36 +0100652 }
653
aliguorifbe78f42008-12-17 19:13:11 +0000654 while (virtqueue_pop(vq, &elem)) {
Mark McLoughlin62433752009-06-18 18:21:36 +0100655 ssize_t ret, len = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000656 unsigned int out_num = elem.out_num;
657 struct iovec *out_sg = &elem.out_sg[0];
658 unsigned hdr_len;
659
660 /* hdr_len refers to the header received from the guest */
661 hdr_len = n->mergeable_rx_bufs ?
662 sizeof(struct virtio_net_hdr_mrg_rxbuf) :
663 sizeof(struct virtio_net_hdr);
664
665 if (out_num < 1 || out_sg->iov_len != hdr_len) {
666 fprintf(stderr, "virtio-net header not in first element\n");
667 exit(1);
668 }
669
670 /* ignore the header if GSO is not supported */
Mark McLoughlin3a330132009-10-22 17:43:45 +0100671 if (!n->has_vnet_hdr) {
aliguorifbe78f42008-12-17 19:13:11 +0000672 out_num--;
673 out_sg++;
674 len += hdr_len;
675 } else if (n->mergeable_rx_bufs) {
676 /* tapfd expects a struct virtio_net_hdr */
677 hdr_len -= sizeof(struct virtio_net_hdr);
678 out_sg->iov_len -= hdr_len;
679 len += hdr_len;
680 }
681
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000682 ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
Mark McLoughlin62433752009-06-18 18:21:36 +0100683 virtio_net_tx_complete);
684 if (ret == 0) {
685 virtio_queue_set_notification(n->tx_vq, 0);
686 n->async_tx.elem = elem;
687 n->async_tx.len = len;
Alex Williamsone3f30482010-09-02 09:00:57 -0600688 return -EBUSY;
Mark McLoughlin62433752009-06-18 18:21:36 +0100689 }
690
691 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000692
693 virtqueue_push(vq, &elem, len);
694 virtio_notify(&n->vdev, vq);
Alex Williamsone3f30482010-09-02 09:00:57 -0600695
696 if (++num_packets >= n->tx_burst) {
697 break;
698 }
aliguorifbe78f42008-12-17 19:13:11 +0000699 }
Alex Williamsone3f30482010-09-02 09:00:57 -0600700 return num_packets;
aliguorifbe78f42008-12-17 19:13:11 +0000701}
702
703static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq)
704{
705 VirtIONet *n = to_virtio_net(vdev);
706
707 if (n->tx_timer_active) {
708 virtio_queue_set_notification(vq, 1);
709 qemu_del_timer(n->tx_timer);
710 n->tx_timer_active = 0;
711 virtio_net_flush_tx(n, vq);
712 } else {
713 qemu_mod_timer(n->tx_timer,
Alex Williamsonf0c07c72010-09-02 09:00:50 -0600714 qemu_get_clock(vm_clock) + n->tx_timeout);
aliguorifbe78f42008-12-17 19:13:11 +0000715 n->tx_timer_active = 1;
716 virtio_queue_set_notification(vq, 0);
717 }
718}
719
720static void virtio_net_tx_timer(void *opaque)
721{
722 VirtIONet *n = opaque;
723
724 n->tx_timer_active = 0;
725
726 /* Just in case the driver is not ready on more */
727 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
728 return;
729
730 virtio_queue_set_notification(n->tx_vq, 1);
731 virtio_net_flush_tx(n, n->tx_vq);
732}
733
734static void virtio_net_save(QEMUFile *f, void *opaque)
735{
736 VirtIONet *n = opaque;
737
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200738 if (n->vhost_started) {
739 /* TODO: should we really stop the backend?
740 * If we don't, it might keep writing to memory. */
741 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
742 n->vhost_started = 0;
743 }
aliguorifbe78f42008-12-17 19:13:11 +0000744 virtio_save(&n->vdev, f);
745
aliguori79674062009-02-05 22:36:12 +0000746 qemu_put_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000747 qemu_put_be32(f, n->tx_timer_active);
aliguorie46cb382009-01-07 17:50:45 +0000748 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000749 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600750 qemu_put_byte(f, n->promisc);
751 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000752 qemu_put_be32(f, n->mac_table.in_use);
753 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000754 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100755 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600756 qemu_put_byte(f, n->mac_table.multi_overflow);
757 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600758 qemu_put_byte(f, n->alluni);
759 qemu_put_byte(f, n->nomulti);
760 qemu_put_byte(f, n->nouni);
761 qemu_put_byte(f, n->nobcast);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100762 qemu_put_byte(f, n->has_ufo);
aliguorifbe78f42008-12-17 19:13:11 +0000763}
764
765static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
766{
767 VirtIONet *n = opaque;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600768 int i;
aliguorifbe78f42008-12-17 19:13:11 +0000769
aliguori9d6271b2009-02-05 22:36:04 +0000770 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000771 return -EINVAL;
772
773 virtio_load(&n->vdev, f);
774
aliguori79674062009-02-05 22:36:12 +0000775 qemu_get_buffer(f, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +0000776 n->tx_timer_active = qemu_get_be32(f);
aliguorie46cb382009-01-07 17:50:45 +0000777 n->mergeable_rx_bufs = qemu_get_be32(f);
aliguorifbe78f42008-12-17 19:13:11 +0000778
aliguori9d6271b2009-02-05 22:36:04 +0000779 if (version_id >= 3)
780 n->status = qemu_get_be16(f);
781
aliguori002437c2009-02-05 22:36:20 +0000782 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600783 if (version_id < 8) {
784 n->promisc = qemu_get_be32(f);
785 n->allmulti = qemu_get_be32(f);
786 } else {
787 n->promisc = qemu_get_byte(f);
788 n->allmulti = qemu_get_byte(f);
789 }
aliguori002437c2009-02-05 22:36:20 +0000790 }
791
aliguorib6503ed2009-02-05 22:36:28 +0000792 if (version_id >= 5) {
793 n->mac_table.in_use = qemu_get_be32(f);
794 /* MAC_TABLE_ENTRIES may be different from the saved image */
795 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
796 qemu_get_buffer(f, n->mac_table.macs,
797 n->mac_table.in_use * ETH_ALEN);
798 } else if (n->mac_table.in_use) {
799 qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600800 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000801 n->mac_table.in_use = 0;
802 }
803 }
804
aliguorif21c0ed2009-02-05 22:36:32 +0000805 if (version_id >= 6)
806 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
807
Mark McLoughlin3a330132009-10-22 17:43:45 +0100808 if (version_id >= 7) {
809 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100810 error_report("virtio-net: saved image requires vnet_hdr=on");
Mark McLoughlin3a330132009-10-22 17:43:45 +0100811 return -1;
812 }
813
814 if (n->has_vnet_hdr) {
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000815 tap_using_vnet_hdr(n->nic->nc.peer, 1);
816 tap_set_offload(n->nic->nc.peer,
Michael S. Tsirkin704a76f2010-01-10 13:52:47 +0200817 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
818 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
819 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
820 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
821 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100822 }
Alex Williamson6c042c12009-06-05 14:46:52 -0600823 }
824
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600825 if (version_id >= 9) {
826 n->mac_table.multi_overflow = qemu_get_byte(f);
827 n->mac_table.uni_overflow = qemu_get_byte(f);
828 }
829
Alex Williamson015cb162009-06-05 14:47:18 -0600830 if (version_id >= 10) {
831 n->alluni = qemu_get_byte(f);
832 n->nomulti = qemu_get_byte(f);
833 n->nouni = qemu_get_byte(f);
834 n->nobcast = qemu_get_byte(f);
835 }
836
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100837 if (version_id >= 11) {
838 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +0100839 error_report("virtio-net: saved image requires TUN_F_UFO support");
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100840 return -1;
841 }
842 }
843
Alex Williamson2d9aba32009-06-05 14:47:13 -0600844 /* Find the first multicast entry in the saved MAC filter */
845 for (i = 0; i < n->mac_table.in_use; i++) {
846 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
847 break;
848 }
849 }
850 n->mac_table.first_multi = i;
851
aliguorifbe78f42008-12-17 19:13:11 +0000852 if (n->tx_timer_active) {
853 qemu_mod_timer(n->tx_timer,
Alex Williamsonf0c07c72010-09-02 09:00:50 -0600854 qemu_get_clock(vm_clock) + n->tx_timeout);
aliguorifbe78f42008-12-17 19:13:11 +0000855 }
aliguorifbe78f42008-12-17 19:13:11 +0000856 return 0;
857}
858
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000859static void virtio_net_cleanup(VLANClientState *nc)
aliguorib946a152009-04-17 17:11:08 +0000860{
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000861 VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
aliguorib946a152009-04-17 17:11:08 +0000862
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000863 n->nic = NULL;
aliguorib946a152009-04-17 17:11:08 +0000864}
865
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000866static NetClientInfo net_virtio_info = {
867 .type = NET_CLIENT_TYPE_NIC,
868 .size = sizeof(NICState),
869 .can_receive = virtio_net_can_receive,
870 .receive = virtio_net_receive,
871 .cleanup = virtio_net_cleanup,
872 .link_status_changed = virtio_net_set_link_status,
873};
874
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200875static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
876{
877 VirtIONet *n = to_virtio_net(vdev);
878 if (!n->nic->nc.peer) {
879 return;
880 }
881 if (n->nic->nc.peer->info->type != NET_CLIENT_TYPE_TAP) {
882 return;
883 }
884
885 if (!tap_get_vhost_net(n->nic->nc.peer)) {
886 return;
887 }
888 if (!!n->vhost_started == !!(status & VIRTIO_CONFIG_S_DRIVER_OK)) {
889 return;
890 }
891 if (status & VIRTIO_CONFIG_S_DRIVER_OK) {
892 int r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), vdev);
893 if (r < 0) {
894 fprintf(stderr, "unable to start vhost net: %d: "
895 "falling back on userspace virtio\n", -r);
896 } else {
897 n->vhost_started = 1;
898 }
899 } else {
900 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), vdev);
901 n->vhost_started = 0;
902 }
903}
904
905static void virtio_net_vmstate_change(void *opaque, int running, int reason)
906{
907 VirtIONet *n = opaque;
Michael S. Tsirkin7f974482010-06-02 11:40:54 +0300908 uint8_t status = running ? VIRTIO_CONFIG_S_DRIVER_OK : 0;
909 /* This is called when vm is started/stopped,
910 * it will start/stop vhost backend if * appropriate
911 * e.g. after migration. */
912 virtio_net_set_status(&n->vdev, n->vdev.status & status);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200913}
914
Alex Williamsonf0c07c72010-09-02 09:00:50 -0600915VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
916 virtio_net_conf *net)
aliguorifbe78f42008-12-17 19:13:11 +0000917{
918 VirtIONet *n;
aliguorifbe78f42008-12-17 19:13:11 +0000919
Paul Brook53c25ce2009-05-18 14:51:59 +0100920 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
921 sizeof(struct virtio_net_config),
922 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +0000923
aliguori0f03eca2009-02-05 22:36:08 +0000924 n->vdev.get_config = virtio_net_get_config;
925 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +0000926 n->vdev.get_features = virtio_net_get_features;
927 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +0000928 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +0000929 n->vdev.reset = virtio_net_reset;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200930 n->vdev.set_status = virtio_net_set_status;
aliguorifbe78f42008-12-17 19:13:11 +0000931 n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
932 n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx);
Alex Williamson4ffb17f2009-06-05 14:47:23 -0600933 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200934 qemu_macaddr_default_if_unset(&conf->macaddr);
Mark McLoughlin3cbe04c2009-10-28 14:07:23 +0000935 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
aliguori554c97d2009-01-08 19:46:33 +0000936 n->status = VIRTIO_NET_S_LINK_UP;
aliguorifbe78f42008-12-17 19:13:11 +0000937
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000938 n->nic = qemu_new_nic(&net_virtio_info, conf, dev->info->name, dev->id, n);
939
940 qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
aliguori96d5e202009-01-07 17:47:15 +0000941
aliguorifbe78f42008-12-17 19:13:11 +0000942 n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n);
943 n->tx_timer_active = 0;
Alex Williamsonf0c07c72010-09-02 09:00:50 -0600944 n->tx_timeout = net->txtimer;
Alex Williamsone3f30482010-09-02 09:00:57 -0600945 n->tx_burst = net->txburst;
aliguorifbe78f42008-12-17 19:13:11 +0000946 n->mergeable_rx_bufs = 0;
aliguori002437c2009-02-05 22:36:20 +0000947 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +0000948
aliguorib6503ed2009-02-05 22:36:28 +0000949 n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +0000950
aliguorif21c0ed2009-02-05 22:36:32 +0000951 n->vlans = qemu_mallocz(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +0000952
Alex Williamson01657c82010-06-25 11:09:28 -0600953 n->qdev = dev;
954 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +0000955 virtio_net_save, virtio_net_load, n);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200956 n->vmstate = qemu_add_vm_change_state_handler(virtio_net_vmstate_change, n);
Paul Brookcf21e102009-05-14 22:35:07 +0100957
Paul Brook53c25ce2009-05-18 14:51:59 +0100958 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +0100959}
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200960
961void virtio_net_exit(VirtIODevice *vdev)
962{
963 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200964 qemu_del_vm_change_state_handler(n->vmstate);
965
966 if (n->vhost_started) {
967 vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), vdev);
968 }
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200969
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000970 qemu_purge_queued_packets(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200971
Alex Williamson01657c82010-06-25 11:09:28 -0600972 unregister_savevm(n->qdev, "virtio-net", n);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200973
974 qemu_free(n->mac_table.macs);
975 qemu_free(n->vlans);
976
977 qemu_del_timer(n->tx_timer);
978 qemu_free_timer(n->tx_timer);
979
980 virtio_cleanup(&n->vdev);
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000981 qemu_del_vlan_client(&n->nic->nc);
Gerd Hoffmann97b15622009-10-21 15:25:35 +0200982}