blob: 4b285c128d9b55a535c04403760128c96bfb6c8e [file] [log] [blame]
aliguorifbe78f42008-12-17 19:13:11 +00001/*
2 * Virtio Network Device
3 *
4 * Copyright IBM, Corp. 2007
5 *
6 * Authors:
7 * Anthony Liguori <aliguori@us.ibm.com>
8 *
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
11 *
12 */
13
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010014#include "qemu/iov.h"
aliguorifbe78f42008-12-17 19:13:11 +000015#include "virtio.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020016#include "net/net.h"
Mark McLoughlin7200ac32009-10-22 17:49:03 +010017#include "net/checksum.h"
Mark McLoughlina8ed73f2009-10-22 17:49:05 +010018#include "net/tap.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010019#include "qemu/error-report.h"
20#include "qemu/timer.h"
aliguorifbe78f42008-12-17 19:13:11 +000021#include "virtio-net.h"
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +020022#include "vhost_net.h"
aliguorifbe78f42008-12-17 19:13:11 +000023
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010024#define VIRTIO_NET_VM_VERSION 11
aliguorib6503ed2009-02-05 22:36:28 +000025
Alex Williamson4ffb17f2009-06-05 14:47:23 -060026#define MAC_TABLE_ENTRIES 64
aliguorif21c0ed2009-02-05 22:36:32 +000027#define MAX_VLAN (1 << 12) /* Per 802.1Q definition */
aliguori9d6271b2009-02-05 22:36:04 +000028
Jason Wang0c87e932013-01-30 19:12:38 +080029typedef struct VirtIONetQueue {
30 VirtQueue *rx_vq;
31 VirtQueue *tx_vq;
32 QEMUTimer *tx_timer;
33 QEMUBH *tx_bh;
34 int tx_waiting;
35 struct {
36 VirtQueueElement elem;
37 ssize_t len;
38 } async_tx;
39 struct VirtIONet *n;
40} VirtIONetQueue;
41
aliguorifbe78f42008-12-17 19:13:11 +000042typedef struct VirtIONet
43{
44 VirtIODevice vdev;
aliguori79674062009-02-05 22:36:12 +000045 uint8_t mac[ETH_ALEN];
aliguori554c97d2009-01-08 19:46:33 +000046 uint16_t status;
Jason Wang0c87e932013-01-30 19:12:38 +080047 VirtIONetQueue vq;
aliguori3d11d362009-02-05 22:36:16 +000048 VirtQueue *ctrl_vq;
Mark McLoughlineb6b6c12009-11-25 18:49:11 +000049 NICState *nic;
Alex Williamsonf0c07c72010-09-02 09:00:50 -060050 uint32_t tx_timeout;
Alex Williamsone3f30482010-09-02 09:00:57 -060051 int32_t tx_burst;
Mark McLoughlin3a330132009-10-22 17:43:45 +010052 uint32_t has_vnet_hdr;
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +020053 size_t host_hdr_len;
54 size_t guest_hdr_len;
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +010055 uint8_t has_ufo;
aliguorifbe78f42008-12-17 19:13:11 +000056 int mergeable_rx_bufs;
Alex Williamsonf10c5922009-06-05 14:46:57 -060057 uint8_t promisc;
58 uint8_t allmulti;
Alex Williamson015cb162009-06-05 14:47:18 -060059 uint8_t alluni;
60 uint8_t nomulti;
61 uint8_t nouni;
62 uint8_t nobcast;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +020063 uint8_t vhost_started;
aliguorib6503ed2009-02-05 22:36:28 +000064 struct {
65 int in_use;
Alex Williamson2d9aba32009-06-05 14:47:13 -060066 int first_multi;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -060067 uint8_t multi_overflow;
68 uint8_t uni_overflow;
aliguorib6503ed2009-02-05 22:36:28 +000069 uint8_t *macs;
70 } mac_table;
aliguorif21c0ed2009-02-05 22:36:32 +000071 uint32_t *vlans;
Alex Williamson01657c82010-06-25 11:09:28 -060072 DeviceState *qdev;
aliguorifbe78f42008-12-17 19:13:11 +000073} VirtIONet;
74
Jason Wang0c87e932013-01-30 19:12:38 +080075static VirtIONetQueue *virtio_net_get_queue(NetClientState *nc)
76{
77 VirtIONet *n = qemu_get_nic_opaque(nc);
78
79 return &n->vq;
80}
aliguorifbe78f42008-12-17 19:13:11 +000081/* TODO
82 * - we could suppress RX interrupt if we were so inclined.
83 */
84
85static VirtIONet *to_virtio_net(VirtIODevice *vdev)
86{
87 return (VirtIONet *)vdev;
88}
89
aliguori0f03eca2009-02-05 22:36:08 +000090static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
aliguorifbe78f42008-12-17 19:13:11 +000091{
92 VirtIONet *n = to_virtio_net(vdev);
93 struct virtio_net_config netcfg;
94
Stefan Hajnoczib46d97f2011-03-03 21:42:28 +000095 stw_p(&netcfg.status, n->status);
aliguori79674062009-02-05 22:36:12 +000096 memcpy(netcfg.mac, n->mac, ETH_ALEN);
aliguorifbe78f42008-12-17 19:13:11 +000097 memcpy(config, &netcfg, sizeof(netcfg));
98}
99
aliguori0f03eca2009-02-05 22:36:08 +0000100static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
101{
102 VirtIONet *n = to_virtio_net(vdev);
103 struct virtio_net_config netcfg;
104
105 memcpy(&netcfg, config, sizeof(netcfg));
106
Amos Kongc1943a32013-01-22 23:44:45 +0800107 if (!(n->vdev.guest_features >> VIRTIO_NET_F_CTRL_MAC_ADDR & 1) &&
108 memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
aliguori79674062009-02-05 22:36:12 +0000109 memcpy(n->mac, netcfg.mac, ETH_ALEN);
Jason Wangb356f762013-01-30 19:12:22 +0800110 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
aliguori0f03eca2009-02-05 22:36:08 +0000111 }
112}
113
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200114static bool virtio_net_started(VirtIONet *n, uint8_t status)
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200115{
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200116 return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200117 (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running;
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200118}
119
120static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
121{
Jason Wangb356f762013-01-30 19:12:22 +0800122 NetClientState *nc = qemu_get_queue(n->nic);
123
124 if (!nc->peer) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200125 return;
126 }
Jason Wangb356f762013-01-30 19:12:22 +0800127 if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200128 return;
129 }
130
Jason Wangb356f762013-01-30 19:12:22 +0800131 if (!tap_get_vhost_net(nc->peer)) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200132 return;
133 }
Michael S. Tsirkin32993692011-02-09 18:45:09 +0200134 if (!!n->vhost_started == virtio_net_started(n, status) &&
Jason Wangb356f762013-01-30 19:12:22 +0800135 !nc->peer->link_down) {
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200136 return;
137 }
138 if (!n->vhost_started) {
mst@redhat.com5430a282011-02-01 22:13:42 +0200139 int r;
Jason Wangb356f762013-01-30 19:12:22 +0800140 if (!vhost_net_query(tap_get_vhost_net(nc->peer), &n->vdev)) {
mst@redhat.com5430a282011-02-01 22:13:42 +0200141 return;
142 }
Michael S. Tsirkin1830b802012-12-25 17:38:59 +0200143 n->vhost_started = 1;
Jason Wanga9f98bb2013-01-30 19:12:35 +0800144 r = vhost_net_start(&n->vdev, nc, 1);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200145 if (r < 0) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000146 error_report("unable to start vhost net: %d: "
147 "falling back on userspace virtio", -r);
Michael S. Tsirkin1830b802012-12-25 17:38:59 +0200148 n->vhost_started = 0;
Jason Wang0c87e932013-01-30 19:12:38 +0800149 } else {
150 n->vhost_started = 1;
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200151 }
152 } else {
Jason Wanga9f98bb2013-01-30 19:12:35 +0800153 vhost_net_stop(&n->vdev, nc, 1);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200154 n->vhost_started = 0;
155 }
156}
157
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200158static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
159{
160 VirtIONet *n = to_virtio_net(vdev);
Jason Wang0c87e932013-01-30 19:12:38 +0800161 VirtIONetQueue *q = &n->vq;
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200162
163 virtio_net_vhost_status(n, status);
164
Jason Wang0c87e932013-01-30 19:12:38 +0800165 if (!q->tx_waiting) {
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200166 return;
167 }
168
169 if (virtio_net_started(n, status) && !n->vhost_started) {
Jason Wang0c87e932013-01-30 19:12:38 +0800170 if (q->tx_timer) {
171 qemu_mod_timer(q->tx_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100172 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200173 } else {
Jason Wang0c87e932013-01-30 19:12:38 +0800174 qemu_bh_schedule(q->tx_bh);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200175 }
176 } else {
Jason Wang0c87e932013-01-30 19:12:38 +0800177 if (q->tx_timer) {
178 qemu_del_timer(q->tx_timer);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200179 } else {
Jason Wang0c87e932013-01-30 19:12:38 +0800180 qemu_bh_cancel(q->tx_bh);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200181 }
182 }
183}
184
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100185static void virtio_net_set_link_status(NetClientState *nc)
aliguori554c97d2009-01-08 19:46:33 +0000186{
Jason Wangcc1f0f42013-01-30 19:12:23 +0800187 VirtIONet *n = qemu_get_nic_opaque(nc);
aliguori554c97d2009-01-08 19:46:33 +0000188 uint16_t old_status = n->status;
189
Mark McLoughlineb6b6c12009-11-25 18:49:11 +0000190 if (nc->link_down)
aliguori554c97d2009-01-08 19:46:33 +0000191 n->status &= ~VIRTIO_NET_S_LINK_UP;
192 else
193 n->status |= VIRTIO_NET_S_LINK_UP;
194
195 if (n->status != old_status)
196 virtio_notify_config(&n->vdev);
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200197
198 virtio_net_set_status(&n->vdev, n->vdev.status);
aliguori554c97d2009-01-08 19:46:33 +0000199}
200
aliguori002437c2009-02-05 22:36:20 +0000201static void virtio_net_reset(VirtIODevice *vdev)
202{
203 VirtIONet *n = to_virtio_net(vdev);
204
205 /* Reset back to compatibility mode */
206 n->promisc = 1;
207 n->allmulti = 0;
Alex Williamson015cb162009-06-05 14:47:18 -0600208 n->alluni = 0;
209 n->nomulti = 0;
210 n->nouni = 0;
211 n->nobcast = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000212
aliguorif21c0ed2009-02-05 22:36:32 +0000213 /* Flush any MAC and VLAN filter table state */
aliguorib6503ed2009-02-05 22:36:28 +0000214 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600215 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600216 n->mac_table.multi_overflow = 0;
217 n->mac_table.uni_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000218 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
Michael S. Tsirkin41dc8a62013-01-16 11:37:40 +0200219 memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac));
aliguorif21c0ed2009-02-05 22:36:32 +0000220 memset(n->vlans, 0, MAX_VLAN >> 3);
aliguori002437c2009-02-05 22:36:20 +0000221}
222
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200223static void peer_test_vnet_hdr(VirtIONet *n)
Mark McLoughlin3a330132009-10-22 17:43:45 +0100224{
Jason Wangb356f762013-01-30 19:12:22 +0800225 NetClientState *nc = qemu_get_queue(n->nic);
226 if (!nc->peer) {
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200227 return;
Jason Wangb356f762013-01-30 19:12:22 +0800228 }
Mark McLoughlin3a330132009-10-22 17:43:45 +0100229
Jason Wangb356f762013-01-30 19:12:22 +0800230 if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200231 return;
Jason Wangb356f762013-01-30 19:12:22 +0800232 }
Mark McLoughlin3a330132009-10-22 17:43:45 +0100233
Jason Wangb356f762013-01-30 19:12:22 +0800234 n->has_vnet_hdr = tap_has_vnet_hdr(nc->peer);
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200235}
Mark McLoughlin3a330132009-10-22 17:43:45 +0100236
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200237static int peer_has_vnet_hdr(VirtIONet *n)
238{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100239 return n->has_vnet_hdr;
240}
241
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100242static int peer_has_ufo(VirtIONet *n)
243{
244 if (!peer_has_vnet_hdr(n))
245 return 0;
246
Jason Wangb356f762013-01-30 19:12:22 +0800247 n->has_ufo = tap_has_ufo(qemu_get_queue(n->nic)->peer);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100248
249 return n->has_ufo;
250}
251
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +0200252static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs)
253{
254 n->mergeable_rx_bufs = mergeable_rx_bufs;
255
256 n->guest_hdr_len = n->mergeable_rx_bufs ?
257 sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
258
259 if (peer_has_vnet_hdr(n) &&
Jason Wangb356f762013-01-30 19:12:22 +0800260 tap_has_vnet_hdr_len(qemu_get_queue(n->nic)->peer, n->guest_hdr_len)) {
261 tap_set_vnet_hdr_len(qemu_get_queue(n->nic)->peer, n->guest_hdr_len);
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +0200262 n->host_hdr_len = n->guest_hdr_len;
263 }
264}
265
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200266static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
aliguorifbe78f42008-12-17 19:13:11 +0000267{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100268 VirtIONet *n = to_virtio_net(vdev);
Jason Wangb356f762013-01-30 19:12:22 +0800269 NetClientState *nc = qemu_get_queue(n->nic);
aliguorifbe78f42008-12-17 19:13:11 +0000270
Michael S. Tsirkinc9f79a32010-01-12 20:50:17 +0200271 features |= (1 << VIRTIO_NET_F_MAC);
272
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +0200273 if (!peer_has_vnet_hdr(n)) {
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200274 features &= ~(0x1 << VIRTIO_NET_F_CSUM);
275 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
276 features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
277 features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100278
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200279 features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
280 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
281 features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
282 features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
283 }
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100284
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200285 if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
286 features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
287 features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100288 }
289
Jason Wangb356f762013-01-30 19:12:22 +0800290 if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200291 return features;
292 }
Jason Wangb356f762013-01-30 19:12:22 +0800293 if (!tap_get_vhost_net(nc->peer)) {
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +0200294 return features;
295 }
Jason Wangb356f762013-01-30 19:12:22 +0800296 return vhost_net_get_features(tap_get_vhost_net(nc->peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000297}
298
aliguori8eca6b12009-04-05 17:40:08 +0000299static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
300{
301 uint32_t features = 0;
302
303 /* Linux kernel 2.6.25. It understood MAC (as everyone must),
304 * but also these: */
305 features |= (1 << VIRTIO_NET_F_MAC);
Dustin Kirkland184bd042009-10-29 10:34:15 -0500306 features |= (1 << VIRTIO_NET_F_CSUM);
307 features |= (1 << VIRTIO_NET_F_HOST_TSO4);
308 features |= (1 << VIRTIO_NET_F_HOST_TSO6);
309 features |= (1 << VIRTIO_NET_F_HOST_ECN);
aliguori8eca6b12009-04-05 17:40:08 +0000310
Michael S. Tsirkin81725392010-01-10 13:52:53 +0200311 return features;
aliguori8eca6b12009-04-05 17:40:08 +0000312}
313
aliguorifbe78f42008-12-17 19:13:11 +0000314static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
315{
316 VirtIONet *n = to_virtio_net(vdev);
Jason Wangb356f762013-01-30 19:12:22 +0800317 NetClientState *nc = qemu_get_queue(n->nic);
aliguorifbe78f42008-12-17 19:13:11 +0000318
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +0200319 virtio_net_set_mrg_rx_bufs(n, !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF)));
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100320
321 if (n->has_vnet_hdr) {
Jason Wangb356f762013-01-30 19:12:22 +0800322 tap_set_offload(nc->peer,
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100323 (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
324 (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
325 (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
Sridhar Samudrala6c9f58b2009-10-22 17:43:49 +0100326 (features >> VIRTIO_NET_F_GUEST_ECN) & 1,
327 (features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlinf5436dd2009-10-22 17:43:47 +0100328 }
Jason Wangb356f762013-01-30 19:12:22 +0800329 if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
David L Stevensdc14a392010-03-31 21:20:31 +0300330 return;
331 }
Jason Wangb356f762013-01-30 19:12:22 +0800332 if (!tap_get_vhost_net(nc->peer)) {
David L Stevensdc14a392010-03-31 21:20:31 +0300333 return;
334 }
Jason Wangb356f762013-01-30 19:12:22 +0800335 vhost_net_ack_features(tap_get_vhost_net(nc->peer), features);
aliguorifbe78f42008-12-17 19:13:11 +0000336}
337
aliguori002437c2009-02-05 22:36:20 +0000338static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800339 struct iovec *iov, unsigned int iov_cnt)
aliguori002437c2009-02-05 22:36:20 +0000340{
341 uint8_t on;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800342 size_t s;
aliguori002437c2009-02-05 22:36:20 +0000343
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800344 s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on));
345 if (s != sizeof(on)) {
346 return VIRTIO_NET_ERR;
aliguori002437c2009-02-05 22:36:20 +0000347 }
348
Amos Kongdd234542013-01-22 23:44:46 +0800349 if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) {
aliguori002437c2009-02-05 22:36:20 +0000350 n->promisc = on;
Amos Kongdd234542013-01-22 23:44:46 +0800351 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) {
aliguori002437c2009-02-05 22:36:20 +0000352 n->allmulti = on;
Amos Kongdd234542013-01-22 23:44:46 +0800353 } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) {
Alex Williamson015cb162009-06-05 14:47:18 -0600354 n->alluni = on;
Amos Kongdd234542013-01-22 23:44:46 +0800355 } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) {
Alex Williamson015cb162009-06-05 14:47:18 -0600356 n->nomulti = on;
Amos Kongdd234542013-01-22 23:44:46 +0800357 } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) {
Alex Williamson015cb162009-06-05 14:47:18 -0600358 n->nouni = on;
Amos Kongdd234542013-01-22 23:44:46 +0800359 } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) {
Alex Williamson015cb162009-06-05 14:47:18 -0600360 n->nobcast = on;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800361 } else {
aliguori002437c2009-02-05 22:36:20 +0000362 return VIRTIO_NET_ERR;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800363 }
aliguori002437c2009-02-05 22:36:20 +0000364
365 return VIRTIO_NET_OK;
366}
367
aliguorib6503ed2009-02-05 22:36:28 +0000368static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800369 struct iovec *iov, unsigned int iov_cnt)
aliguorib6503ed2009-02-05 22:36:28 +0000370{
371 struct virtio_net_ctrl_mac mac_data;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800372 size_t s;
aliguorib6503ed2009-02-05 22:36:28 +0000373
Amos Kongc1943a32013-01-22 23:44:45 +0800374 if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) {
375 if (iov_size(iov, iov_cnt) != sizeof(n->mac)) {
376 return VIRTIO_NET_ERR;
377 }
378 s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac));
379 assert(s == sizeof(n->mac));
Jason Wangb356f762013-01-30 19:12:22 +0800380 qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac);
Amos Kongc1943a32013-01-22 23:44:45 +0800381 return VIRTIO_NET_OK;
382 }
383
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800384 if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) {
aliguorib6503ed2009-02-05 22:36:28 +0000385 return VIRTIO_NET_ERR;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800386 }
aliguorib6503ed2009-02-05 22:36:28 +0000387
388 n->mac_table.in_use = 0;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600389 n->mac_table.first_multi = 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600390 n->mac_table.uni_overflow = 0;
391 n->mac_table.multi_overflow = 0;
aliguorib6503ed2009-02-05 22:36:28 +0000392 memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
393
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800394 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
395 sizeof(mac_data.entries));
396 mac_data.entries = ldl_p(&mac_data.entries);
397 if (s != sizeof(mac_data.entries)) {
aliguorib6503ed2009-02-05 22:36:28 +0000398 return VIRTIO_NET_ERR;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800399 }
400 iov_discard_front(&iov, &iov_cnt, s);
401
402 if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) {
403 return VIRTIO_NET_ERR;
404 }
aliguorib6503ed2009-02-05 22:36:28 +0000405
406 if (mac_data.entries <= MAC_TABLE_ENTRIES) {
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800407 s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs,
408 mac_data.entries * ETH_ALEN);
409 if (s != mac_data.entries * ETH_ALEN) {
410 return VIRTIO_NET_ERR;
411 }
aliguorib6503ed2009-02-05 22:36:28 +0000412 n->mac_table.in_use += mac_data.entries;
413 } else {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600414 n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000415 }
416
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800417 iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN);
418
Alex Williamson2d9aba32009-06-05 14:47:13 -0600419 n->mac_table.first_multi = n->mac_table.in_use;
420
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800421 s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries,
422 sizeof(mac_data.entries));
423 mac_data.entries = ldl_p(&mac_data.entries);
424 if (s != sizeof(mac_data.entries)) {
aliguorib6503ed2009-02-05 22:36:28 +0000425 return VIRTIO_NET_ERR;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800426 }
aliguorib6503ed2009-02-05 22:36:28 +0000427
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800428 iov_discard_front(&iov, &iov_cnt, s);
429
430 if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) {
431 return VIRTIO_NET_ERR;
432 }
433
434 if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
435 s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs,
436 mac_data.entries * ETH_ALEN);
437 if (s != mac_data.entries * ETH_ALEN) {
438 return VIRTIO_NET_ERR;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600439 }
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800440 n->mac_table.in_use += mac_data.entries;
441 } else {
442 n->mac_table.multi_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +0000443 }
444
445 return VIRTIO_NET_OK;
446}
447
aliguorif21c0ed2009-02-05 22:36:32 +0000448static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800449 struct iovec *iov, unsigned int iov_cnt)
aliguorif21c0ed2009-02-05 22:36:32 +0000450{
451 uint16_t vid;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800452 size_t s;
aliguorif21c0ed2009-02-05 22:36:32 +0000453
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800454 s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid));
455 vid = lduw_p(&vid);
456 if (s != sizeof(vid)) {
aliguorif21c0ed2009-02-05 22:36:32 +0000457 return VIRTIO_NET_ERR;
458 }
459
aliguorif21c0ed2009-02-05 22:36:32 +0000460 if (vid >= MAX_VLAN)
461 return VIRTIO_NET_ERR;
462
463 if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
464 n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
465 else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
466 n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
467 else
468 return VIRTIO_NET_ERR;
469
470 return VIRTIO_NET_OK;
471}
472
aliguori3d11d362009-02-05 22:36:16 +0000473static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
474{
aliguori002437c2009-02-05 22:36:20 +0000475 VirtIONet *n = to_virtio_net(vdev);
aliguori3d11d362009-02-05 22:36:16 +0000476 struct virtio_net_ctrl_hdr ctrl;
477 virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
478 VirtQueueElement elem;
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800479 size_t s;
480 struct iovec *iov;
481 unsigned int iov_cnt;
aliguori3d11d362009-02-05 22:36:16 +0000482
483 while (virtqueue_pop(vq, &elem)) {
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800484 if (iov_size(elem.in_sg, elem.in_num) < sizeof(status) ||
485 iov_size(elem.out_sg, elem.out_num) < sizeof(ctrl)) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000486 error_report("virtio-net ctrl missing headers");
aliguori3d11d362009-02-05 22:36:16 +0000487 exit(1);
488 }
489
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800490 iov = elem.out_sg;
491 iov_cnt = elem.out_num;
492 s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl));
493 iov_discard_front(&iov, &iov_cnt, sizeof(ctrl));
494 if (s != sizeof(ctrl)) {
495 status = VIRTIO_NET_ERR;
Amos Kongdd234542013-01-22 23:44:46 +0800496 } else if (ctrl.class == VIRTIO_NET_CTRL_RX) {
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800497 status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt);
498 } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) {
499 status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt);
500 } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) {
501 status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt);
aliguori3d11d362009-02-05 22:36:16 +0000502 }
503
Michael S. Tsirkin921ac5d2013-01-22 23:44:44 +0800504 s = iov_from_buf(elem.in_sg, elem.in_num, 0, &status, sizeof(status));
505 assert(s == sizeof(status));
aliguori3d11d362009-02-05 22:36:16 +0000506
507 virtqueue_push(vq, &elem, sizeof(status));
508 virtio_notify(vdev, vq);
509 }
510}
511
aliguorifbe78f42008-12-17 19:13:11 +0000512/* RX */
513
514static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
515{
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100516 VirtIONet *n = to_virtio_net(vdev);
517
Jason Wangb356f762013-01-30 19:12:22 +0800518 qemu_flush_queued_packets(qemu_get_queue(n->nic));
aliguorifbe78f42008-12-17 19:13:11 +0000519}
520
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100521static int virtio_net_can_receive(NetClientState *nc)
aliguorifbe78f42008-12-17 19:13:11 +0000522{
Jason Wangcc1f0f42013-01-30 19:12:23 +0800523 VirtIONet *n = qemu_get_nic_opaque(nc);
Jason Wang0c87e932013-01-30 19:12:38 +0800524 VirtIONetQueue *q = virtio_net_get_queue(nc);
525
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200526 if (!n->vdev.vm_running) {
Michael S. Tsirkin95477322010-11-22 19:52:19 +0200527 return 0;
528 }
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000529
Jason Wang0c87e932013-01-30 19:12:38 +0800530 if (!virtio_queue_ready(q->rx_vq) ||
531 !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
aliguorifbe78f42008-12-17 19:13:11 +0000532 return 0;
Jason Wang0c87e932013-01-30 19:12:38 +0800533 }
aliguorifbe78f42008-12-17 19:13:11 +0000534
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000535 return 1;
536}
537
Jason Wang0c87e932013-01-30 19:12:38 +0800538static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize)
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000539{
Jason Wang0c87e932013-01-30 19:12:38 +0800540 VirtIONet *n = q->n;
541 if (virtio_queue_empty(q->rx_vq) ||
aliguorifbe78f42008-12-17 19:13:11 +0000542 (n->mergeable_rx_bufs &&
Jason Wang0c87e932013-01-30 19:12:38 +0800543 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
544 virtio_queue_set_notification(q->rx_vq, 1);
Tom Lendacky06b12972010-02-08 10:10:01 -0600545
546 /* To avoid a race condition where the guest has made some buffers
547 * available after the above check but before notification was
548 * enabled, check for available buffers again.
549 */
Jason Wang0c87e932013-01-30 19:12:38 +0800550 if (virtio_queue_empty(q->rx_vq) ||
Tom Lendacky06b12972010-02-08 10:10:01 -0600551 (n->mergeable_rx_bufs &&
Jason Wang0c87e932013-01-30 19:12:38 +0800552 !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) {
Tom Lendacky06b12972010-02-08 10:10:01 -0600553 return 0;
Jason Wang0c87e932013-01-30 19:12:38 +0800554 }
aliguorifbe78f42008-12-17 19:13:11 +0000555 }
556
Jason Wang0c87e932013-01-30 19:12:38 +0800557 virtio_queue_set_notification(q->rx_vq, 0);
aliguorifbe78f42008-12-17 19:13:11 +0000558 return 1;
559}
560
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100561/* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
562 * it never finds out that the packets don't have valid checksums. This
563 * causes dhclient to get upset. Fedora's carried a patch for ages to
564 * fix this with Xen but it hasn't appeared in an upstream release of
565 * dhclient yet.
566 *
567 * To avoid breaking existing guests, we catch udp packets and add
568 * checksums. This is terrible but it's better than hacking the guest
569 * kernels.
570 *
571 * N.B. if we introduce a zero-copy API, this operation is no longer free so
572 * we should provide a mechanism to disable it to avoid polluting the host
573 * cache.
574 */
575static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200576 uint8_t *buf, size_t size)
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100577{
578 if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
579 (size > 27 && size < 1500) && /* normal sized MTU */
580 (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
581 (buf[23] == 17) && /* ip.protocol == UDP */
582 (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200583 net_checksum_calculate(buf, size);
Anthony Liguori1d41b0c2009-10-22 17:43:48 +0100584 hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
585 }
586}
587
Michael S. Tsirkin280598b2012-09-24 13:24:17 +0200588static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
589 const void *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000590{
Mark McLoughlin3a330132009-10-22 17:43:45 +0100591 if (n->has_vnet_hdr) {
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200592 /* FIXME this cast is evil */
593 void *wbuf = (void *)buf;
Michael S. Tsirkin280598b2012-09-24 13:24:17 +0200594 work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
595 size - n->host_hdr_len);
596 iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200597 } else {
598 struct virtio_net_hdr hdr = {
599 .flags = 0,
600 .gso_type = VIRTIO_NET_HDR_GSO_NONE
601 };
602 iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100603 }
aliguorifbe78f42008-12-17 19:13:11 +0000604}
605
aliguori3831ab22009-02-05 22:36:24 +0000606static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
607{
608 static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
aliguorif21c0ed2009-02-05 22:36:32 +0000609 static const uint8_t vlan[] = {0x81, 0x00};
aliguori3831ab22009-02-05 22:36:24 +0000610 uint8_t *ptr = (uint8_t *)buf;
aliguorib6503ed2009-02-05 22:36:28 +0000611 int i;
aliguori3831ab22009-02-05 22:36:24 +0000612
613 if (n->promisc)
614 return 1;
615
Michael S. Tsirkine043ebc2012-09-24 16:27:27 +0200616 ptr += n->host_hdr_len;
Mark McLoughlin3a330132009-10-22 17:43:45 +0100617
aliguorif21c0ed2009-02-05 22:36:32 +0000618 if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
619 int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
620 if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
621 return 0;
622 }
623
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600624 if (ptr[0] & 1) { // multicast
625 if (!memcmp(ptr, bcast, sizeof(bcast))) {
Alex Williamson015cb162009-06-05 14:47:18 -0600626 return !n->nobcast;
627 } else if (n->nomulti) {
628 return 0;
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600629 } else if (n->allmulti || n->mac_table.multi_overflow) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600630 return 1;
631 }
Alex Williamson2d9aba32009-06-05 14:47:13 -0600632
633 for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
634 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
635 return 1;
636 }
637 }
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600638 } else { // unicast
Alex Williamson015cb162009-06-05 14:47:18 -0600639 if (n->nouni) {
640 return 0;
641 } else if (n->alluni || n->mac_table.uni_overflow) {
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600642 return 1;
643 } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
Alex Williamsonbbe2f392009-06-05 14:47:02 -0600644 return 1;
645 }
aliguori3831ab22009-02-05 22:36:24 +0000646
Alex Williamson2d9aba32009-06-05 14:47:13 -0600647 for (i = 0; i < n->mac_table.first_multi; i++) {
648 if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
649 return 1;
650 }
651 }
aliguorib6503ed2009-02-05 22:36:28 +0000652 }
653
aliguori3831ab22009-02-05 22:36:24 +0000654 return 0;
655}
656
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100657static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
aliguorifbe78f42008-12-17 19:13:11 +0000658{
Jason Wangcc1f0f42013-01-30 19:12:23 +0800659 VirtIONet *n = qemu_get_nic_opaque(nc);
Jason Wang0c87e932013-01-30 19:12:38 +0800660 VirtIONetQueue *q = virtio_net_get_queue(nc);
Michael S. Tsirkin63c58722012-09-24 13:17:13 +0200661 struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
662 struct virtio_net_hdr_mrg_rxbuf mhdr;
663 unsigned mhdr_cnt = 0;
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200664 size_t offset, i, guest_offset;
aliguorifbe78f42008-12-17 19:13:11 +0000665
Jason Wangb356f762013-01-30 19:12:22 +0800666 if (!virtio_net_can_receive(qemu_get_queue(n->nic))) {
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000667 return -1;
Jason Wangb356f762013-01-30 19:12:22 +0800668 }
Mark McLoughlincdd5cc12009-10-27 18:16:38 +0000669
Michael S. Tsirkin940cda92010-06-06 18:53:10 +0300670 /* hdr_len refers to the header we supply to the guest */
Jason Wang0c87e932013-01-30 19:12:38 +0800671 if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) {
Mark McLoughlin8aeff622009-04-29 13:40:02 +0100672 return 0;
Jason Wang0c87e932013-01-30 19:12:38 +0800673 }
aliguorifbe78f42008-12-17 19:13:11 +0000674
aliguori3831ab22009-02-05 22:36:24 +0000675 if (!receive_filter(n, buf, size))
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100676 return size;
aliguori3831ab22009-02-05 22:36:24 +0000677
aliguorifbe78f42008-12-17 19:13:11 +0000678 offset = i = 0;
679
680 while (offset < size) {
681 VirtQueueElement elem;
682 int len, total;
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200683 const struct iovec *sg = elem.in_sg;
aliguorifbe78f42008-12-17 19:13:11 +0000684
Amit Shah22c253d2010-01-13 16:24:43 +0530685 total = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000686
Jason Wang0c87e932013-01-30 19:12:38 +0800687 if (virtqueue_pop(q->rx_vq, &elem) == 0) {
aliguorifbe78f42008-12-17 19:13:11 +0000688 if (i == 0)
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100689 return -1;
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000690 error_report("virtio-net unexpected empty queue: "
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300691 "i %zd mergeable %d offset %zd, size %zd, "
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000692 "guest hdr len %zd, host hdr len %zd guest features 0x%x",
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300693 i, n->mergeable_rx_bufs, offset, size,
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200694 n->guest_hdr_len, n->host_hdr_len, n->vdev.guest_features);
aliguorifbe78f42008-12-17 19:13:11 +0000695 exit(1);
696 }
697
698 if (elem.in_num < 1) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000699 error_report("virtio-net receive queue contains no in buffers");
aliguorifbe78f42008-12-17 19:13:11 +0000700 exit(1);
701 }
702
aliguorifbe78f42008-12-17 19:13:11 +0000703 if (i == 0) {
Michael S. Tsirkinc8d28e72012-09-24 13:26:55 +0200704 assert(offset == 0);
Michael S. Tsirkin63c58722012-09-24 13:17:13 +0200705 if (n->mergeable_rx_bufs) {
706 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
707 sg, elem.in_num,
708 offsetof(typeof(mhdr), num_buffers),
709 sizeof(mhdr.num_buffers));
710 }
aliguorifbe78f42008-12-17 19:13:11 +0000711
Michael S. Tsirkinc8d28e72012-09-24 13:26:55 +0200712 receive_header(n, sg, elem.in_num, buf, size);
713 offset = n->host_hdr_len;
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200714 total += n->guest_hdr_len;
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200715 guest_offset = n->guest_hdr_len;
716 } else {
717 guest_offset = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000718 }
719
720 /* copy in packet. ugh */
Michael S. Tsirkin22cc84d2012-09-24 13:14:16 +0200721 len = iov_from_buf(sg, elem.in_num, guest_offset,
Michael Tokarevdcf6f5e2012-03-11 18:05:12 +0400722 buf + offset, size - offset);
aliguorifbe78f42008-12-17 19:13:11 +0000723 total += len;
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300724 offset += len;
725 /* If buffers can't be merged, at this point we
726 * must have consumed the complete packet.
727 * Otherwise, drop it. */
728 if (!n->mergeable_rx_bufs && offset < size) {
729#if 0
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000730 error_report("virtio-net truncated non-mergeable packet: "
731 "i %zd mergeable %d offset %zd, size %zd, "
732 "guest hdr len %zd, host hdr len %zd",
733 i, n->mergeable_rx_bufs,
Michael S. Tsirkine35e23f2012-09-24 12:12:25 +0200734 offset, size, n->guest_hdr_len, n->host_hdr_len);
Michael S. Tsirkin279a4252010-06-22 16:22:49 +0300735#endif
736 return size;
737 }
aliguorifbe78f42008-12-17 19:13:11 +0000738
739 /* signal other side */
Jason Wang0c87e932013-01-30 19:12:38 +0800740 virtqueue_fill(q->rx_vq, &elem, total, i++);
aliguorifbe78f42008-12-17 19:13:11 +0000741 }
742
Michael S. Tsirkin63c58722012-09-24 13:17:13 +0200743 if (mhdr_cnt) {
744 stw_p(&mhdr.num_buffers, i);
745 iov_from_buf(mhdr_sg, mhdr_cnt,
746 0,
747 &mhdr.num_buffers, sizeof mhdr.num_buffers);
Aurelien Jarno44b15bc2011-01-25 11:55:14 +0100748 }
aliguorifbe78f42008-12-17 19:13:11 +0000749
Jason Wang0c87e932013-01-30 19:12:38 +0800750 virtqueue_flush(q->rx_vq, i);
751 virtio_notify(&n->vdev, q->rx_vq);
Mark McLoughlin4f1c9422009-05-18 13:40:55 +0100752
753 return size;
aliguorifbe78f42008-12-17 19:13:11 +0000754}
755
Jason Wang0c87e932013-01-30 19:12:38 +0800756static int32_t virtio_net_flush_tx(VirtIONetQueue *q);
Mark McLoughlin62433752009-06-18 18:21:36 +0100757
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +0100758static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
Mark McLoughlin62433752009-06-18 18:21:36 +0100759{
Jason Wangcc1f0f42013-01-30 19:12:23 +0800760 VirtIONet *n = qemu_get_nic_opaque(nc);
Jason Wang0c87e932013-01-30 19:12:38 +0800761 VirtIONetQueue *q = virtio_net_get_queue(nc);
Mark McLoughlin62433752009-06-18 18:21:36 +0100762
Jason Wang0c87e932013-01-30 19:12:38 +0800763 virtqueue_push(q->tx_vq, &q->async_tx.elem, 0);
764 virtio_notify(&n->vdev, q->tx_vq);
Mark McLoughlin62433752009-06-18 18:21:36 +0100765
Jason Wang0c87e932013-01-30 19:12:38 +0800766 q->async_tx.elem.out_num = q->async_tx.len = 0;
Mark McLoughlin62433752009-06-18 18:21:36 +0100767
Jason Wang0c87e932013-01-30 19:12:38 +0800768 virtio_queue_set_notification(q->tx_vq, 1);
769 virtio_net_flush_tx(q);
Mark McLoughlin62433752009-06-18 18:21:36 +0100770}
771
aliguorifbe78f42008-12-17 19:13:11 +0000772/* TX */
Jason Wang0c87e932013-01-30 19:12:38 +0800773static int32_t virtio_net_flush_tx(VirtIONetQueue *q)
aliguorifbe78f42008-12-17 19:13:11 +0000774{
Jason Wang0c87e932013-01-30 19:12:38 +0800775 VirtIONet *n = q->n;
aliguorifbe78f42008-12-17 19:13:11 +0000776 VirtQueueElement elem;
Alex Williamsone3f30482010-09-02 09:00:57 -0600777 int32_t num_packets = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -0600778 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
779 return num_packets;
780 }
aliguorifbe78f42008-12-17 19:13:11 +0000781
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200782 assert(n->vdev.vm_running);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200783
Jason Wang0c87e932013-01-30 19:12:38 +0800784 if (q->async_tx.elem.out_num) {
785 virtio_queue_set_notification(q->tx_vq, 0);
Alex Williamsone3f30482010-09-02 09:00:57 -0600786 return num_packets;
Mark McLoughlin62433752009-06-18 18:21:36 +0100787 }
788
Jason Wang0c87e932013-01-30 19:12:38 +0800789 while (virtqueue_pop(q->tx_vq, &elem)) {
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200790 ssize_t ret, len;
aliguorifbe78f42008-12-17 19:13:11 +0000791 unsigned int out_num = elem.out_num;
792 struct iovec *out_sg = &elem.out_sg[0];
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200793 struct iovec sg[VIRTQUEUE_MAX_SIZE];
aliguorifbe78f42008-12-17 19:13:11 +0000794
Michael S. Tsirkin7b80d082012-09-24 14:54:44 +0200795 if (out_num < 1) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +0000796 error_report("virtio-net header not in first element");
aliguorifbe78f42008-12-17 19:13:11 +0000797 exit(1);
798 }
799
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200800 /*
801 * If host wants to see the guest header as is, we can
802 * pass it on unchanged. Otherwise, copy just the parts
803 * that host is interested in.
804 */
805 assert(n->host_hdr_len <= n->guest_hdr_len);
806 if (n->host_hdr_len != n->guest_hdr_len) {
807 unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
808 out_sg, out_num,
809 0, n->host_hdr_len);
810 sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
811 out_sg, out_num,
812 n->guest_hdr_len, -1);
813 out_num = sg_num;
814 out_sg = sg;
aliguorifbe78f42008-12-17 19:13:11 +0000815 }
816
Michael S. Tsirkin7b80d082012-09-24 14:54:44 +0200817 len = n->guest_hdr_len;
Michael S. Tsirkin14761f92012-09-24 14:52:28 +0200818
Jason Wangb356f762013-01-30 19:12:22 +0800819 ret = qemu_sendv_packet_async(qemu_get_queue(n->nic), out_sg, out_num,
Mark McLoughlin62433752009-06-18 18:21:36 +0100820 virtio_net_tx_complete);
821 if (ret == 0) {
Jason Wang0c87e932013-01-30 19:12:38 +0800822 virtio_queue_set_notification(q->tx_vq, 0);
823 q->async_tx.elem = elem;
824 q->async_tx.len = len;
Alex Williamsone3f30482010-09-02 09:00:57 -0600825 return -EBUSY;
Mark McLoughlin62433752009-06-18 18:21:36 +0100826 }
827
828 len += ret;
aliguorifbe78f42008-12-17 19:13:11 +0000829
Jason Wang0c87e932013-01-30 19:12:38 +0800830 virtqueue_push(q->tx_vq, &elem, 0);
831 virtio_notify(&n->vdev, q->tx_vq);
Alex Williamsone3f30482010-09-02 09:00:57 -0600832
833 if (++num_packets >= n->tx_burst) {
834 break;
835 }
aliguorifbe78f42008-12-17 19:13:11 +0000836 }
Alex Williamsone3f30482010-09-02 09:00:57 -0600837 return num_packets;
aliguorifbe78f42008-12-17 19:13:11 +0000838}
839
Alex Williamsona697a332010-09-02 09:01:10 -0600840static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
aliguorifbe78f42008-12-17 19:13:11 +0000841{
842 VirtIONet *n = to_virtio_net(vdev);
Jason Wang0c87e932013-01-30 19:12:38 +0800843 VirtIONetQueue *q = &n->vq;
aliguorifbe78f42008-12-17 19:13:11 +0000844
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200845 /* This happens when device was stopped but VCPU wasn't. */
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200846 if (!n->vdev.vm_running) {
Jason Wang0c87e932013-01-30 19:12:38 +0800847 q->tx_waiting = 1;
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200848 return;
849 }
850
Jason Wang0c87e932013-01-30 19:12:38 +0800851 if (q->tx_waiting) {
aliguorifbe78f42008-12-17 19:13:11 +0000852 virtio_queue_set_notification(vq, 1);
Jason Wang0c87e932013-01-30 19:12:38 +0800853 qemu_del_timer(q->tx_timer);
854 q->tx_waiting = 0;
855 virtio_net_flush_tx(q);
aliguorifbe78f42008-12-17 19:13:11 +0000856 } else {
Jason Wang0c87e932013-01-30 19:12:38 +0800857 qemu_mod_timer(q->tx_timer,
Paolo Bonzini74475452011-03-11 16:47:48 +0100858 qemu_get_clock_ns(vm_clock) + n->tx_timeout);
Jason Wang0c87e932013-01-30 19:12:38 +0800859 q->tx_waiting = 1;
aliguorifbe78f42008-12-17 19:13:11 +0000860 virtio_queue_set_notification(vq, 0);
861 }
862}
863
Alex Williamsona697a332010-09-02 09:01:10 -0600864static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
865{
866 VirtIONet *n = to_virtio_net(vdev);
Jason Wang0c87e932013-01-30 19:12:38 +0800867 VirtIONetQueue *q = &n->vq;
Alex Williamsona697a332010-09-02 09:01:10 -0600868
Jason Wang0c87e932013-01-30 19:12:38 +0800869 if (unlikely(q->tx_waiting)) {
Alex Williamsona697a332010-09-02 09:01:10 -0600870 return;
871 }
Jason Wang0c87e932013-01-30 19:12:38 +0800872 q->tx_waiting = 1;
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200873 /* This happens when device was stopped but VCPU wasn't. */
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200874 if (!n->vdev.vm_running) {
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200875 return;
876 }
Alex Williamsona697a332010-09-02 09:01:10 -0600877 virtio_queue_set_notification(vq, 0);
Jason Wang0c87e932013-01-30 19:12:38 +0800878 qemu_bh_schedule(q->tx_bh);
Alex Williamsona697a332010-09-02 09:01:10 -0600879}
880
aliguorifbe78f42008-12-17 19:13:11 +0000881static void virtio_net_tx_timer(void *opaque)
882{
Jason Wang0c87e932013-01-30 19:12:38 +0800883 VirtIONetQueue *q = opaque;
884 VirtIONet *n = q->n;
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200885 assert(n->vdev.vm_running);
aliguorifbe78f42008-12-17 19:13:11 +0000886
Jason Wang0c87e932013-01-30 19:12:38 +0800887 q->tx_waiting = 0;
aliguorifbe78f42008-12-17 19:13:11 +0000888
889 /* Just in case the driver is not ready on more */
890 if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
891 return;
892
Jason Wang0c87e932013-01-30 19:12:38 +0800893 virtio_queue_set_notification(q->tx_vq, 1);
894 virtio_net_flush_tx(q);
aliguorifbe78f42008-12-17 19:13:11 +0000895}
896
Alex Williamsona697a332010-09-02 09:01:10 -0600897static void virtio_net_tx_bh(void *opaque)
898{
Jason Wang0c87e932013-01-30 19:12:38 +0800899 VirtIONetQueue *q = opaque;
900 VirtIONet *n = q->n;
Alex Williamsona697a332010-09-02 09:01:10 -0600901 int32_t ret;
902
Michael S. Tsirkin85cf2a82011-01-10 14:28:40 +0200903 assert(n->vdev.vm_running);
Michael S. Tsirkin783e7702010-11-22 19:52:30 +0200904
Jason Wang0c87e932013-01-30 19:12:38 +0800905 q->tx_waiting = 0;
Alex Williamsona697a332010-09-02 09:01:10 -0600906
907 /* Just in case the driver is not ready on more */
908 if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
909 return;
910
Jason Wang0c87e932013-01-30 19:12:38 +0800911 ret = virtio_net_flush_tx(q);
Alex Williamsona697a332010-09-02 09:01:10 -0600912 if (ret == -EBUSY) {
913 return; /* Notification re-enable handled by tx_complete */
914 }
915
916 /* If we flush a full burst of packets, assume there are
917 * more coming and immediately reschedule */
918 if (ret >= n->tx_burst) {
Jason Wang0c87e932013-01-30 19:12:38 +0800919 qemu_bh_schedule(q->tx_bh);
920 q->tx_waiting = 1;
Alex Williamsona697a332010-09-02 09:01:10 -0600921 return;
922 }
923
924 /* If less than a full burst, re-enable notification and flush
925 * anything that may have come in while we weren't looking. If
926 * we find something, assume the guest is still active and reschedule */
Jason Wang0c87e932013-01-30 19:12:38 +0800927 virtio_queue_set_notification(q->tx_vq, 1);
928 if (virtio_net_flush_tx(q) > 0) {
929 virtio_queue_set_notification(q->tx_vq, 0);
930 qemu_bh_schedule(q->tx_bh);
931 q->tx_waiting = 1;
Alex Williamsona697a332010-09-02 09:01:10 -0600932 }
933}
934
aliguorifbe78f42008-12-17 19:13:11 +0000935static void virtio_net_save(QEMUFile *f, void *opaque)
936{
937 VirtIONet *n = opaque;
Jason Wang0c87e932013-01-30 19:12:38 +0800938 VirtIONetQueue *q = &n->vq;
aliguorifbe78f42008-12-17 19:13:11 +0000939
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +0200940 /* At this point, backend must be stopped, otherwise
941 * it might keep writing to memory. */
942 assert(!n->vhost_started);
aliguorifbe78f42008-12-17 19:13:11 +0000943 virtio_save(&n->vdev, f);
944
aliguori79674062009-02-05 22:36:12 +0000945 qemu_put_buffer(f, n->mac, ETH_ALEN);
Jason Wang0c87e932013-01-30 19:12:38 +0800946 qemu_put_be32(f, q->tx_waiting);
aliguorie46cb382009-01-07 17:50:45 +0000947 qemu_put_be32(f, n->mergeable_rx_bufs);
aliguori9d6271b2009-02-05 22:36:04 +0000948 qemu_put_be16(f, n->status);
Alex Williamsonf10c5922009-06-05 14:46:57 -0600949 qemu_put_byte(f, n->promisc);
950 qemu_put_byte(f, n->allmulti);
aliguorib6503ed2009-02-05 22:36:28 +0000951 qemu_put_be32(f, n->mac_table.in_use);
952 qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
aliguorif21c0ed2009-02-05 22:36:32 +0000953 qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
Mark McLoughlin3a330132009-10-22 17:43:45 +0100954 qemu_put_be32(f, n->has_vnet_hdr);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -0600955 qemu_put_byte(f, n->mac_table.multi_overflow);
956 qemu_put_byte(f, n->mac_table.uni_overflow);
Alex Williamson015cb162009-06-05 14:47:18 -0600957 qemu_put_byte(f, n->alluni);
958 qemu_put_byte(f, n->nomulti);
959 qemu_put_byte(f, n->nouni);
960 qemu_put_byte(f, n->nobcast);
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +0100961 qemu_put_byte(f, n->has_ufo);
aliguorifbe78f42008-12-17 19:13:11 +0000962}
963
964static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
965{
966 VirtIONet *n = opaque;
Jason Wang0c87e932013-01-30 19:12:38 +0800967 VirtIONetQueue *q = &n->vq;
Alex Williamson2d9aba32009-06-05 14:47:13 -0600968 int i;
Orit Wassermann2a633c42012-05-16 12:21:35 +0200969 int ret;
aliguorifbe78f42008-12-17 19:13:11 +0000970
aliguori9d6271b2009-02-05 22:36:04 +0000971 if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
aliguorifbe78f42008-12-17 19:13:11 +0000972 return -EINVAL;
973
Orit Wassermann2a633c42012-05-16 12:21:35 +0200974 ret = virtio_load(&n->vdev, f);
975 if (ret) {
976 return ret;
977 }
aliguorifbe78f42008-12-17 19:13:11 +0000978
aliguori79674062009-02-05 22:36:12 +0000979 qemu_get_buffer(f, n->mac, ETH_ALEN);
Jason Wang0c87e932013-01-30 19:12:38 +0800980 q->tx_waiting = qemu_get_be32(f);
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +0200981
982 virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f));
aliguorifbe78f42008-12-17 19:13:11 +0000983
aliguori9d6271b2009-02-05 22:36:04 +0000984 if (version_id >= 3)
985 n->status = qemu_get_be16(f);
986
aliguori002437c2009-02-05 22:36:20 +0000987 if (version_id >= 4) {
Alex Williamsonf10c5922009-06-05 14:46:57 -0600988 if (version_id < 8) {
989 n->promisc = qemu_get_be32(f);
990 n->allmulti = qemu_get_be32(f);
991 } else {
992 n->promisc = qemu_get_byte(f);
993 n->allmulti = qemu_get_byte(f);
994 }
aliguori002437c2009-02-05 22:36:20 +0000995 }
996
aliguorib6503ed2009-02-05 22:36:28 +0000997 if (version_id >= 5) {
998 n->mac_table.in_use = qemu_get_be32(f);
999 /* MAC_TABLE_ENTRIES may be different from the saved image */
1000 if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
1001 qemu_get_buffer(f, n->mac_table.macs,
1002 n->mac_table.in_use * ETH_ALEN);
1003 } else if (n->mac_table.in_use) {
Juan Quintelae398d612012-08-29 19:03:09 +02001004 uint8_t *buf = g_malloc0(n->mac_table.in_use);
1005 qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN);
1006 g_free(buf);
Alex Williamson8fd2a2f2009-06-05 14:47:08 -06001007 n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
aliguorib6503ed2009-02-05 22:36:28 +00001008 n->mac_table.in_use = 0;
1009 }
1010 }
1011
aliguorif21c0ed2009-02-05 22:36:32 +00001012 if (version_id >= 6)
1013 qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
1014
Mark McLoughlin3a330132009-10-22 17:43:45 +01001015 if (version_id >= 7) {
1016 if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001017 error_report("virtio-net: saved image requires vnet_hdr=on");
Mark McLoughlin3a330132009-10-22 17:43:45 +01001018 return -1;
1019 }
1020
1021 if (n->has_vnet_hdr) {
Jason Wangb356f762013-01-30 19:12:22 +08001022 tap_set_offload(qemu_get_queue(n->nic)->peer,
Michael S. Tsirkin704a76f2010-01-10 13:52:47 +02001023 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
1024 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
1025 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
1026 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1,
1027 (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1);
Mark McLoughlin3a330132009-10-22 17:43:45 +01001028 }
Alex Williamson6c042c12009-06-05 14:46:52 -06001029 }
1030
Alex Williamson8fd2a2f2009-06-05 14:47:08 -06001031 if (version_id >= 9) {
1032 n->mac_table.multi_overflow = qemu_get_byte(f);
1033 n->mac_table.uni_overflow = qemu_get_byte(f);
1034 }
1035
Alex Williamson015cb162009-06-05 14:47:18 -06001036 if (version_id >= 10) {
1037 n->alluni = qemu_get_byte(f);
1038 n->nomulti = qemu_get_byte(f);
1039 n->nouni = qemu_get_byte(f);
1040 n->nobcast = qemu_get_byte(f);
1041 }
1042
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +01001043 if (version_id >= 11) {
1044 if (qemu_get_byte(f) && !peer_has_ufo(n)) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01001045 error_report("virtio-net: saved image requires TUN_F_UFO support");
Mark McLoughlin0ce0e8f2009-10-22 17:43:50 +01001046 return -1;
1047 }
1048 }
1049
Alex Williamson2d9aba32009-06-05 14:47:13 -06001050 /* Find the first multicast entry in the saved MAC filter */
1051 for (i = 0; i < n->mac_table.in_use; i++) {
1052 if (n->mac_table.macs[i * ETH_ALEN] & 1) {
1053 break;
1054 }
1055 }
1056 n->mac_table.first_multi = i;
Amos Kong98991482012-09-28 10:06:02 +08001057
1058 /* nc.link_down can't be migrated, so infer link_down according
1059 * to link status bit in n->status */
Jason Wangb356f762013-01-30 19:12:22 +08001060 qemu_get_queue(n->nic)->link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
Amos Kong98991482012-09-28 10:06:02 +08001061
aliguorifbe78f42008-12-17 19:13:11 +00001062 return 0;
1063}
1064
Stefan Hajnoczi4e68f7a2012-07-24 16:35:13 +01001065static void virtio_net_cleanup(NetClientState *nc)
aliguorib946a152009-04-17 17:11:08 +00001066{
Jason Wangcc1f0f42013-01-30 19:12:23 +08001067 VirtIONet *n = qemu_get_nic_opaque(nc);
aliguorib946a152009-04-17 17:11:08 +00001068
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001069 n->nic = NULL;
aliguorib946a152009-04-17 17:11:08 +00001070}
1071
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001072static NetClientInfo net_virtio_info = {
Laszlo Ersek2be64a62012-07-17 16:17:12 +02001073 .type = NET_CLIENT_OPTIONS_KIND_NIC,
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001074 .size = sizeof(NICState),
1075 .can_receive = virtio_net_can_receive,
1076 .receive = virtio_net_receive,
1077 .cleanup = virtio_net_cleanup,
1078 .link_status_changed = virtio_net_set_link_status,
1079};
1080
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001081static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx)
1082{
1083 VirtIONet *n = to_virtio_net(vdev);
Jason Wangb356f762013-01-30 19:12:22 +08001084 NetClientState *nc = qemu_get_queue(n->nic);
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001085 assert(n->vhost_started);
Jason Wangb356f762013-01-30 19:12:22 +08001086 return vhost_net_virtqueue_pending(tap_get_vhost_net(nc->peer), idx);
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001087}
1088
1089static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx,
1090 bool mask)
1091{
1092 VirtIONet *n = to_virtio_net(vdev);
Jason Wangb356f762013-01-30 19:12:22 +08001093 NetClientState *nc = qemu_get_queue(n->nic);
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001094 assert(n->vhost_started);
Jason Wangb356f762013-01-30 19:12:22 +08001095 vhost_net_virtqueue_mask(tap_get_vhost_net(nc->peer),
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001096 vdev, idx, mask);
1097}
1098
Alex Williamsonf0c07c72010-09-02 09:00:50 -06001099VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
1100 virtio_net_conf *net)
aliguorifbe78f42008-12-17 19:13:11 +00001101{
1102 VirtIONet *n;
aliguorifbe78f42008-12-17 19:13:11 +00001103
Paul Brook53c25ce2009-05-18 14:51:59 +01001104 n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
1105 sizeof(struct virtio_net_config),
1106 sizeof(VirtIONet));
aliguorifbe78f42008-12-17 19:13:11 +00001107
aliguori0f03eca2009-02-05 22:36:08 +00001108 n->vdev.get_config = virtio_net_get_config;
1109 n->vdev.set_config = virtio_net_set_config;
aliguorifbe78f42008-12-17 19:13:11 +00001110 n->vdev.get_features = virtio_net_get_features;
1111 n->vdev.set_features = virtio_net_set_features;
aliguori8eca6b12009-04-05 17:40:08 +00001112 n->vdev.bad_features = virtio_net_bad_features;
aliguori002437c2009-02-05 22:36:20 +00001113 n->vdev.reset = virtio_net_reset;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001114 n->vdev.set_status = virtio_net_set_status;
Michael S. Tsirkinf56a1242012-12-24 17:37:01 +02001115 n->vdev.guest_notifier_mask = virtio_net_guest_notifier_mask;
1116 n->vdev.guest_notifier_pending = virtio_net_guest_notifier_pending;
Jason Wang0c87e932013-01-30 19:12:38 +08001117 n->vq.rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
1118 n->vq.n = n;
Alex Williamsona697a332010-09-02 09:01:10 -06001119
1120 if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
Stefan Hajnoczie7b43f72010-11-15 20:44:37 +00001121 error_report("virtio-net: "
1122 "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1123 net->tx);
1124 error_report("Defaulting to \"bh\"");
Alex Williamsona697a332010-09-02 09:01:10 -06001125 }
1126
1127 if (net->tx && !strcmp(net->tx, "timer")) {
Jason Wang0c87e932013-01-30 19:12:38 +08001128 n->vq.tx_vq = virtio_add_queue(&n->vdev, 256,
1129 virtio_net_handle_tx_timer);
1130 n->vq.tx_timer = qemu_new_timer_ns(vm_clock,
1131 virtio_net_tx_timer, &n->vq);
Alex Williamsona697a332010-09-02 09:01:10 -06001132 n->tx_timeout = net->txtimer;
1133 } else {
Jason Wang0c87e932013-01-30 19:12:38 +08001134 n->vq.tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
1135 n->vq.tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vq);
Alex Williamsona697a332010-09-02 09:01:10 -06001136 }
Alex Williamson4ffb17f2009-06-05 14:47:23 -06001137 n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001138 qemu_macaddr_default_if_unset(&conf->macaddr);
Mark McLoughlin3cbe04c2009-10-28 14:07:23 +00001139 memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
aliguori554c97d2009-01-08 19:46:33 +00001140 n->status = VIRTIO_NET_S_LINK_UP;
aliguorifbe78f42008-12-17 19:13:11 +00001141
Anthony Liguorif79f2bf2011-12-04 11:17:51 -06001142 n->nic = qemu_new_nic(&net_virtio_info, conf, object_get_typename(OBJECT(dev)), dev->id, n);
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +02001143 peer_test_vnet_hdr(n);
1144 if (peer_has_vnet_hdr(n)) {
Jason Wangb356f762013-01-30 19:12:22 +08001145 tap_using_vnet_hdr(qemu_get_queue(n->nic)->peer, true);
Michael S. Tsirkin6e371ab2012-09-24 17:04:21 +02001146 n->host_hdr_len = sizeof(struct virtio_net_hdr);
1147 } else {
1148 n->host_hdr_len = 0;
1149 }
Mark McLoughlineb6b6c12009-11-25 18:49:11 +00001150
Jason Wangb356f762013-01-30 19:12:22 +08001151 qemu_format_nic_info_str(qemu_get_queue(n->nic), conf->macaddr.a);
aliguori96d5e202009-01-07 17:47:15 +00001152
Jason Wang0c87e932013-01-30 19:12:38 +08001153 n->vq.tx_waiting = 0;
Alex Williamsone3f30482010-09-02 09:00:57 -06001154 n->tx_burst = net->txburst;
Michael S. Tsirkinff3a8062012-09-24 21:05:03 +02001155 virtio_net_set_mrg_rx_bufs(n, 0);
aliguori002437c2009-02-05 22:36:20 +00001156 n->promisc = 1; /* for compatibility */
aliguorifbe78f42008-12-17 19:13:11 +00001157
Anthony Liguori7267c092011-08-20 22:09:37 -05001158 n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
aliguorib6503ed2009-02-05 22:36:28 +00001159
Anthony Liguori7267c092011-08-20 22:09:37 -05001160 n->vlans = g_malloc0(MAX_VLAN >> 3);
aliguorif21c0ed2009-02-05 22:36:32 +00001161
Alex Williamson01657c82010-06-25 11:09:28 -06001162 n->qdev = dev;
1163 register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
aliguorifbe78f42008-12-17 19:13:11 +00001164 virtio_net_save, virtio_net_load, n);
Paul Brookcf21e102009-05-14 22:35:07 +01001165
Gleb Natapov1ca4d092010-12-08 13:35:05 +02001166 add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0");
1167
Paul Brook53c25ce2009-05-18 14:51:59 +01001168 return &n->vdev;
Paul Brookcf21e102009-05-14 22:35:07 +01001169}
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001170
1171void virtio_net_exit(VirtIODevice *vdev)
1172{
1173 VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
Jason Wang0c87e932013-01-30 19:12:38 +08001174 VirtIONetQueue *q = &n->vq;
Michael S. Tsirkin9bc63042010-03-17 13:08:42 +02001175
Michael S. Tsirkinafbaa7b2010-09-27 18:41:30 +02001176 /* This will stop vhost backend if appropriate. */
1177 virtio_net_set_status(vdev, 0);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001178
Jason Wangb356f762013-01-30 19:12:22 +08001179 qemu_purge_queued_packets(qemu_get_queue(n->nic));
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001180
Alex Williamson01657c82010-06-25 11:09:28 -06001181 unregister_savevm(n->qdev, "virtio-net", n);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001182
Anthony Liguori7267c092011-08-20 22:09:37 -05001183 g_free(n->mac_table.macs);
1184 g_free(n->vlans);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001185
Jason Wang0c87e932013-01-30 19:12:38 +08001186 if (q->tx_timer) {
1187 qemu_del_timer(q->tx_timer);
1188 qemu_free_timer(q->tx_timer);
Alex Williamsona697a332010-09-02 09:01:10 -06001189 } else {
Jason Wang0c87e932013-01-30 19:12:38 +08001190 qemu_bh_delete(q->tx_bh);
Alex Williamsona697a332010-09-02 09:01:10 -06001191 }
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001192
Jason Wang948ecf22013-01-30 19:12:24 +08001193 qemu_del_nic(n->nic);
Amit Shahb52dfd72011-07-27 14:00:31 +05301194 virtio_cleanup(&n->vdev);
Gerd Hoffmann97b15622009-10-21 15:25:35 +02001195}