]>
Commit | Line | Data |
---|---|---|
fbe78f4f AL |
1 | /* |
2 | * Virtio Network Device | |
3 | * | |
4 | * Copyright IBM, Corp. 2007 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <[email protected]> | |
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 | ||
1de7afc9 | 14 | #include "qemu/iov.h" |
0d09e41a | 15 | #include "hw/virtio/virtio.h" |
1422e32d | 16 | #include "net/net.h" |
7200ac3c | 17 | #include "net/checksum.h" |
a8ed73f7 | 18 | #include "net/tap.h" |
1de7afc9 PB |
19 | #include "qemu/error-report.h" |
20 | #include "qemu/timer.h" | |
0d09e41a PB |
21 | #include "hw/virtio/virtio-net.h" |
22 | #include "net/vhost_net.h" | |
17ec5a86 | 23 | #include "hw/virtio/virtio-bus.h" |
fbe78f4f | 24 | |
0ce0e8f4 | 25 | #define VIRTIO_NET_VM_VERSION 11 |
b6503ed9 | 26 | |
4ffb17f5 | 27 | #define MAC_TABLE_ENTRIES 64 |
f21c0ed9 | 28 | #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ |
9d6271b8 | 29 | |
14f9b664 JL |
30 | /* |
31 | * Calculate the number of bytes up to and including the given 'field' of | |
32 | * 'container'. | |
33 | */ | |
34 | #define endof(container, field) \ | |
35 | (offsetof(container, field) + sizeof(((container *)0)->field)) | |
36 | ||
37 | typedef struct VirtIOFeature { | |
38 | uint32_t flags; | |
39 | size_t end; | |
40 | } VirtIOFeature; | |
41 | ||
42 | static VirtIOFeature feature_sizes[] = { | |
43 | {.flags = 1 << VIRTIO_NET_F_MAC, | |
44 | .end = endof(struct virtio_net_config, mac)}, | |
45 | {.flags = 1 << VIRTIO_NET_F_STATUS, | |
46 | .end = endof(struct virtio_net_config, status)}, | |
47 | {.flags = 1 << VIRTIO_NET_F_MQ, | |
48 | .end = endof(struct virtio_net_config, max_virtqueue_pairs)}, | |
49 | {} | |
50 | }; | |
51 | ||
fed699f9 | 52 | static VirtIONetQueue *virtio_net_get_subqueue(NetClientState *nc) |
0c87e93e JW |
53 | { |
54 | VirtIONet *n = qemu_get_nic_opaque(nc); | |
55 | ||
fed699f9 | 56 | return &n->vqs[nc->queue_index]; |
0c87e93e | 57 | } |
fed699f9 JW |
58 | |
59 | static int vq2q(int queue_index) | |
60 | { | |
61 | return queue_index / 2; | |
62 | } | |
63 | ||
fbe78f4f AL |
64 | /* TODO |
65 | * - we could suppress RX interrupt if we were so inclined. | |
66 | */ | |
67 | ||
17ec5a86 FK |
68 | /* |
69 | * Moving to QOM later in this serie. | |
70 | */ | |
fbe78f4f AL |
71 | static VirtIONet *to_virtio_net(VirtIODevice *vdev) |
72 | { | |
73 | return (VirtIONet *)vdev; | |
74 | } | |
75 | ||
0f03eca6 | 76 | static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) |
fbe78f4f AL |
77 | { |
78 | VirtIONet *n = to_virtio_net(vdev); | |
79 | struct virtio_net_config netcfg; | |
80 | ||
b46d97f2 | 81 | stw_p(&netcfg.status, n->status); |
fed699f9 | 82 | stw_p(&netcfg.max_virtqueue_pairs, n->max_queues); |
79674068 | 83 | memcpy(netcfg.mac, n->mac, ETH_ALEN); |
14f9b664 | 84 | memcpy(config, &netcfg, n->config_size); |
fbe78f4f AL |
85 | } |
86 | ||
0f03eca6 AL |
87 | static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) |
88 | { | |
89 | VirtIONet *n = to_virtio_net(vdev); | |
14f9b664 | 90 | struct virtio_net_config netcfg = {}; |
0f03eca6 | 91 | |
14f9b664 | 92 | memcpy(&netcfg, config, n->config_size); |
0f03eca6 | 93 | |
c1943a3f AK |
94 | if (!(n->vdev.guest_features >> VIRTIO_NET_F_CTRL_MAC_ADDR & 1) && |
95 | memcmp(netcfg.mac, n->mac, ETH_ALEN)) { | |
79674068 | 96 | memcpy(n->mac, netcfg.mac, ETH_ALEN); |
b356f76d | 97 | qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); |
0f03eca6 AL |
98 | } |
99 | } | |
100 | ||
783e7706 MT |
101 | static bool virtio_net_started(VirtIONet *n, uint8_t status) |
102 | { | |
103 | return (status & VIRTIO_CONFIG_S_DRIVER_OK) && | |
85cf2a8d | 104 | (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running; |
783e7706 MT |
105 | } |
106 | ||
107 | static void virtio_net_vhost_status(VirtIONet *n, uint8_t status) | |
afbaa7b4 | 108 | { |
b356f76d | 109 | NetClientState *nc = qemu_get_queue(n->nic); |
fed699f9 | 110 | int queues = n->multiqueue ? n->max_queues : 1; |
b356f76d JW |
111 | |
112 | if (!nc->peer) { | |
afbaa7b4 MT |
113 | return; |
114 | } | |
b356f76d | 115 | if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { |
afbaa7b4 MT |
116 | return; |
117 | } | |
118 | ||
b356f76d | 119 | if (!tap_get_vhost_net(nc->peer)) { |
afbaa7b4 MT |
120 | return; |
121 | } | |
fed699f9 | 122 | |
32993698 | 123 | if (!!n->vhost_started == virtio_net_started(n, status) && |
b356f76d | 124 | !nc->peer->link_down) { |
afbaa7b4 MT |
125 | return; |
126 | } | |
127 | if (!n->vhost_started) { | |
5430a28f | 128 | int r; |
b356f76d | 129 | if (!vhost_net_query(tap_get_vhost_net(nc->peer), &n->vdev)) { |
5430a28f MT |
130 | return; |
131 | } | |
1830b80f | 132 | n->vhost_started = 1; |
fed699f9 | 133 | r = vhost_net_start(&n->vdev, n->nic->ncs, queues); |
afbaa7b4 | 134 | if (r < 0) { |
e7b43f7e SH |
135 | error_report("unable to start vhost net: %d: " |
136 | "falling back on userspace virtio", -r); | |
1830b80f | 137 | n->vhost_started = 0; |
afbaa7b4 MT |
138 | } |
139 | } else { | |
fed699f9 | 140 | vhost_net_stop(&n->vdev, n->nic->ncs, queues); |
afbaa7b4 MT |
141 | n->vhost_started = 0; |
142 | } | |
143 | } | |
144 | ||
783e7706 MT |
145 | static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status) |
146 | { | |
147 | VirtIONet *n = to_virtio_net(vdev); | |
fed699f9 JW |
148 | VirtIONetQueue *q; |
149 | int i; | |
150 | uint8_t queue_status; | |
783e7706 MT |
151 | |
152 | virtio_net_vhost_status(n, status); | |
153 | ||
fed699f9 JW |
154 | for (i = 0; i < n->max_queues; i++) { |
155 | q = &n->vqs[i]; | |
783e7706 | 156 | |
fed699f9 JW |
157 | if ((!n->multiqueue && i != 0) || i >= n->curr_queues) { |
158 | queue_status = 0; | |
783e7706 | 159 | } else { |
fed699f9 | 160 | queue_status = status; |
783e7706 | 161 | } |
fed699f9 JW |
162 | |
163 | if (!q->tx_waiting) { | |
164 | continue; | |
165 | } | |
166 | ||
167 | if (virtio_net_started(n, queue_status) && !n->vhost_started) { | |
168 | if (q->tx_timer) { | |
169 | qemu_mod_timer(q->tx_timer, | |
170 | qemu_get_clock_ns(vm_clock) + n->tx_timeout); | |
171 | } else { | |
172 | qemu_bh_schedule(q->tx_bh); | |
173 | } | |
783e7706 | 174 | } else { |
fed699f9 JW |
175 | if (q->tx_timer) { |
176 | qemu_del_timer(q->tx_timer); | |
177 | } else { | |
178 | qemu_bh_cancel(q->tx_bh); | |
179 | } | |
783e7706 MT |
180 | } |
181 | } | |
182 | } | |
183 | ||
4e68f7a0 | 184 | static void virtio_net_set_link_status(NetClientState *nc) |
554c97dd | 185 | { |
cc1f0f45 | 186 | VirtIONet *n = qemu_get_nic_opaque(nc); |
554c97dd AL |
187 | uint16_t old_status = n->status; |
188 | ||
eb6b6c12 | 189 | if (nc->link_down) |
554c97dd AL |
190 | n->status &= ~VIRTIO_NET_S_LINK_UP; |
191 | else | |
192 | n->status |= VIRTIO_NET_S_LINK_UP; | |
193 | ||
194 | if (n->status != old_status) | |
195 | virtio_notify_config(&n->vdev); | |
afbaa7b4 MT |
196 | |
197 | virtio_net_set_status(&n->vdev, n->vdev.status); | |
554c97dd AL |
198 | } |
199 | ||
002437cd AL |
200 | static void virtio_net_reset(VirtIODevice *vdev) |
201 | { | |
202 | VirtIONet *n = to_virtio_net(vdev); | |
203 | ||
204 | /* Reset back to compatibility mode */ | |
205 | n->promisc = 1; | |
206 | n->allmulti = 0; | |
015cb166 AW |
207 | n->alluni = 0; |
208 | n->nomulti = 0; | |
209 | n->nouni = 0; | |
210 | n->nobcast = 0; | |
fed699f9 JW |
211 | /* multiqueue is disabled by default */ |
212 | n->curr_queues = 1; | |
b6503ed9 | 213 | |
f21c0ed9 | 214 | /* Flush any MAC and VLAN filter table state */ |
b6503ed9 | 215 | n->mac_table.in_use = 0; |
2d9aba39 | 216 | n->mac_table.first_multi = 0; |
8fd2a2f1 AW |
217 | n->mac_table.multi_overflow = 0; |
218 | n->mac_table.uni_overflow = 0; | |
b6503ed9 | 219 | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); |
41dc8a67 | 220 | memcpy(&n->mac[0], &n->nic->conf->macaddr, sizeof(n->mac)); |
f21c0ed9 | 221 | memset(n->vlans, 0, MAX_VLAN >> 3); |
002437cd AL |
222 | } |
223 | ||
6e371ab8 | 224 | static void peer_test_vnet_hdr(VirtIONet *n) |
3a330134 | 225 | { |
b356f76d JW |
226 | NetClientState *nc = qemu_get_queue(n->nic); |
227 | if (!nc->peer) { | |
6e371ab8 | 228 | return; |
b356f76d | 229 | } |
3a330134 | 230 | |
b356f76d | 231 | if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { |
6e371ab8 | 232 | return; |
b356f76d | 233 | } |
3a330134 | 234 | |
b356f76d | 235 | n->has_vnet_hdr = tap_has_vnet_hdr(nc->peer); |
6e371ab8 | 236 | } |
3a330134 | 237 | |
6e371ab8 MT |
238 | static int peer_has_vnet_hdr(VirtIONet *n) |
239 | { | |
3a330134 MM |
240 | return n->has_vnet_hdr; |
241 | } | |
242 | ||
0ce0e8f4 MM |
243 | static int peer_has_ufo(VirtIONet *n) |
244 | { | |
245 | if (!peer_has_vnet_hdr(n)) | |
246 | return 0; | |
247 | ||
b356f76d | 248 | n->has_ufo = tap_has_ufo(qemu_get_queue(n->nic)->peer); |
0ce0e8f4 MM |
249 | |
250 | return n->has_ufo; | |
251 | } | |
252 | ||
ff3a8066 MT |
253 | static void virtio_net_set_mrg_rx_bufs(VirtIONet *n, int mergeable_rx_bufs) |
254 | { | |
fed699f9 JW |
255 | int i; |
256 | NetClientState *nc; | |
257 | ||
ff3a8066 MT |
258 | n->mergeable_rx_bufs = mergeable_rx_bufs; |
259 | ||
260 | n->guest_hdr_len = n->mergeable_rx_bufs ? | |
261 | sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr); | |
262 | ||
fed699f9 JW |
263 | for (i = 0; i < n->max_queues; i++) { |
264 | nc = qemu_get_subqueue(n->nic, i); | |
265 | ||
266 | if (peer_has_vnet_hdr(n) && | |
267 | tap_has_vnet_hdr_len(nc->peer, n->guest_hdr_len)) { | |
268 | tap_set_vnet_hdr_len(nc->peer, n->guest_hdr_len); | |
269 | n->host_hdr_len = n->guest_hdr_len; | |
270 | } | |
ff3a8066 MT |
271 | } |
272 | } | |
273 | ||
fed699f9 JW |
274 | static int peer_attach(VirtIONet *n, int index) |
275 | { | |
276 | NetClientState *nc = qemu_get_subqueue(n->nic, index); | |
277 | ||
278 | if (!nc->peer) { | |
279 | return 0; | |
280 | } | |
281 | ||
282 | if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { | |
283 | return 0; | |
284 | } | |
285 | ||
286 | return tap_enable(nc->peer); | |
287 | } | |
288 | ||
289 | static int peer_detach(VirtIONet *n, int index) | |
290 | { | |
291 | NetClientState *nc = qemu_get_subqueue(n->nic, index); | |
292 | ||
293 | if (!nc->peer) { | |
294 | return 0; | |
295 | } | |
296 | ||
297 | if (nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { | |
298 | return 0; | |
299 | } | |
300 | ||
301 | return tap_disable(nc->peer); | |
302 | } | |
303 | ||
304 | static void virtio_net_set_queues(VirtIONet *n) | |
305 | { | |
306 | int i; | |
307 | ||
308 | for (i = 0; i < n->max_queues; i++) { | |
309 | if (i < n->curr_queues) { | |
310 | assert(!peer_attach(n, i)); | |
311 | } else { | |
312 | assert(!peer_detach(n, i)); | |
313 | } | |
314 | } | |
315 | } | |
316 | ||
317 | static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue, int ctrl); | |
318 | ||
8172539d | 319 | static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features) |
fbe78f4f | 320 | { |
3a330134 | 321 | VirtIONet *n = to_virtio_net(vdev); |
b356f76d | 322 | NetClientState *nc = qemu_get_queue(n->nic); |
fbe78f4f | 323 | |
c9f79a3f MT |
324 | features |= (1 << VIRTIO_NET_F_MAC); |
325 | ||
6e371ab8 | 326 | if (!peer_has_vnet_hdr(n)) { |
8172539d MT |
327 | features &= ~(0x1 << VIRTIO_NET_F_CSUM); |
328 | features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4); | |
329 | features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6); | |
330 | features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN); | |
331 | ||
332 | features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM); | |
333 | features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4); | |
334 | features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6); | |
335 | features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN); | |
336 | } | |
3a330134 | 337 | |
8172539d MT |
338 | if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) { |
339 | features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO); | |
340 | features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO); | |
3a330134 MM |
341 | } |
342 | ||
b356f76d | 343 | if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { |
9bc6304c MT |
344 | return features; |
345 | } | |
b356f76d | 346 | if (!tap_get_vhost_net(nc->peer)) { |
9bc6304c MT |
347 | return features; |
348 | } | |
b356f76d | 349 | return vhost_net_get_features(tap_get_vhost_net(nc->peer), features); |
fbe78f4f AL |
350 | } |
351 | ||
8eca6b1b AL |
352 | static uint32_t virtio_net_bad_features(VirtIODevice *vdev) |
353 | { | |
354 | uint32_t features = 0; | |
355 | ||
356 | /* Linux kernel 2.6.25. It understood MAC (as everyone must), | |
357 | * but also these: */ | |
358 | features |= (1 << VIRTIO_NET_F_MAC); | |
184bd048 DK |
359 | features |= (1 << VIRTIO_NET_F_CSUM); |
360 | features |= (1 << VIRTIO_NET_F_HOST_TSO4); | |
361 | features |= (1 << VIRTIO_NET_F_HOST_TSO6); | |
362 | features |= (1 << VIRTIO_NET_F_HOST_ECN); | |
8eca6b1b | 363 | |
8172539d | 364 | return features; |
8eca6b1b AL |
365 | } |
366 | ||
fbe78f4f AL |
367 | static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features) |
368 | { | |
369 | VirtIONet *n = to_virtio_net(vdev); | |
fed699f9 JW |
370 | int i; |
371 | ||
372 | virtio_net_set_multiqueue(n, !!(features & (1 << VIRTIO_NET_F_MQ)), | |
373 | !!(features & (1 << VIRTIO_NET_F_CTRL_VQ))); | |
fbe78f4f | 374 | |
ff3a8066 | 375 | virtio_net_set_mrg_rx_bufs(n, !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF))); |
f5436dd9 MM |
376 | |
377 | if (n->has_vnet_hdr) { | |
fed699f9 | 378 | tap_set_offload(qemu_get_subqueue(n->nic, 0)->peer, |
f5436dd9 MM |
379 | (features >> VIRTIO_NET_F_GUEST_CSUM) & 1, |
380 | (features >> VIRTIO_NET_F_GUEST_TSO4) & 1, | |
381 | (features >> VIRTIO_NET_F_GUEST_TSO6) & 1, | |
6c9f58ba SS |
382 | (features >> VIRTIO_NET_F_GUEST_ECN) & 1, |
383 | (features >> VIRTIO_NET_F_GUEST_UFO) & 1); | |
f5436dd9 | 384 | } |
fed699f9 JW |
385 | |
386 | for (i = 0; i < n->max_queues; i++) { | |
387 | NetClientState *nc = qemu_get_subqueue(n->nic, i); | |
388 | ||
389 | if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { | |
390 | continue; | |
391 | } | |
392 | if (!tap_get_vhost_net(nc->peer)) { | |
393 | continue; | |
394 | } | |
395 | vhost_net_ack_features(tap_get_vhost_net(nc->peer), features); | |
dc14a397 | 396 | } |
fbe78f4f AL |
397 | } |
398 | ||
002437cd | 399 | static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, |
921ac5d0 | 400 | struct iovec *iov, unsigned int iov_cnt) |
002437cd AL |
401 | { |
402 | uint8_t on; | |
921ac5d0 | 403 | size_t s; |
002437cd | 404 | |
921ac5d0 MT |
405 | s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on)); |
406 | if (s != sizeof(on)) { | |
407 | return VIRTIO_NET_ERR; | |
002437cd AL |
408 | } |
409 | ||
dd23454b | 410 | if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) { |
002437cd | 411 | n->promisc = on; |
dd23454b | 412 | } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) { |
002437cd | 413 | n->allmulti = on; |
dd23454b | 414 | } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) { |
015cb166 | 415 | n->alluni = on; |
dd23454b | 416 | } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) { |
015cb166 | 417 | n->nomulti = on; |
dd23454b | 418 | } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) { |
015cb166 | 419 | n->nouni = on; |
dd23454b | 420 | } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) { |
015cb166 | 421 | n->nobcast = on; |
921ac5d0 | 422 | } else { |
002437cd | 423 | return VIRTIO_NET_ERR; |
921ac5d0 | 424 | } |
002437cd AL |
425 | |
426 | return VIRTIO_NET_OK; | |
427 | } | |
428 | ||
b6503ed9 | 429 | static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, |
921ac5d0 | 430 | struct iovec *iov, unsigned int iov_cnt) |
b6503ed9 AL |
431 | { |
432 | struct virtio_net_ctrl_mac mac_data; | |
921ac5d0 | 433 | size_t s; |
b6503ed9 | 434 | |
c1943a3f AK |
435 | if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) { |
436 | if (iov_size(iov, iov_cnt) != sizeof(n->mac)) { | |
437 | return VIRTIO_NET_ERR; | |
438 | } | |
439 | s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac)); | |
440 | assert(s == sizeof(n->mac)); | |
b356f76d | 441 | qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); |
c1943a3f AK |
442 | return VIRTIO_NET_OK; |
443 | } | |
444 | ||
921ac5d0 | 445 | if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) { |
b6503ed9 | 446 | return VIRTIO_NET_ERR; |
921ac5d0 | 447 | } |
b6503ed9 AL |
448 | |
449 | n->mac_table.in_use = 0; | |
2d9aba39 | 450 | n->mac_table.first_multi = 0; |
8fd2a2f1 AW |
451 | n->mac_table.uni_overflow = 0; |
452 | n->mac_table.multi_overflow = 0; | |
b6503ed9 AL |
453 | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); |
454 | ||
921ac5d0 MT |
455 | s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, |
456 | sizeof(mac_data.entries)); | |
457 | mac_data.entries = ldl_p(&mac_data.entries); | |
458 | if (s != sizeof(mac_data.entries)) { | |
459 | return VIRTIO_NET_ERR; | |
460 | } | |
461 | iov_discard_front(&iov, &iov_cnt, s); | |
b6503ed9 | 462 | |
921ac5d0 | 463 | if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) { |
b6503ed9 | 464 | return VIRTIO_NET_ERR; |
921ac5d0 | 465 | } |
b6503ed9 AL |
466 | |
467 | if (mac_data.entries <= MAC_TABLE_ENTRIES) { | |
921ac5d0 MT |
468 | s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs, |
469 | mac_data.entries * ETH_ALEN); | |
470 | if (s != mac_data.entries * ETH_ALEN) { | |
471 | return VIRTIO_NET_ERR; | |
472 | } | |
b6503ed9 AL |
473 | n->mac_table.in_use += mac_data.entries; |
474 | } else { | |
8fd2a2f1 | 475 | n->mac_table.uni_overflow = 1; |
b6503ed9 AL |
476 | } |
477 | ||
921ac5d0 MT |
478 | iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN); |
479 | ||
2d9aba39 AW |
480 | n->mac_table.first_multi = n->mac_table.in_use; |
481 | ||
921ac5d0 MT |
482 | s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, |
483 | sizeof(mac_data.entries)); | |
484 | mac_data.entries = ldl_p(&mac_data.entries); | |
485 | if (s != sizeof(mac_data.entries)) { | |
486 | return VIRTIO_NET_ERR; | |
487 | } | |
488 | ||
489 | iov_discard_front(&iov, &iov_cnt, s); | |
b6503ed9 | 490 | |
921ac5d0 | 491 | if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) { |
b6503ed9 | 492 | return VIRTIO_NET_ERR; |
921ac5d0 | 493 | } |
b6503ed9 | 494 | |
921ac5d0 MT |
495 | if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) { |
496 | s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs, | |
497 | mac_data.entries * ETH_ALEN); | |
498 | if (s != mac_data.entries * ETH_ALEN) { | |
499 | return VIRTIO_NET_ERR; | |
8fd2a2f1 | 500 | } |
921ac5d0 MT |
501 | n->mac_table.in_use += mac_data.entries; |
502 | } else { | |
503 | n->mac_table.multi_overflow = 1; | |
b6503ed9 AL |
504 | } |
505 | ||
506 | return VIRTIO_NET_OK; | |
507 | } | |
508 | ||
f21c0ed9 | 509 | static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, |
921ac5d0 | 510 | struct iovec *iov, unsigned int iov_cnt) |
f21c0ed9 AL |
511 | { |
512 | uint16_t vid; | |
921ac5d0 | 513 | size_t s; |
f21c0ed9 | 514 | |
921ac5d0 MT |
515 | s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid)); |
516 | vid = lduw_p(&vid); | |
517 | if (s != sizeof(vid)) { | |
f21c0ed9 AL |
518 | return VIRTIO_NET_ERR; |
519 | } | |
520 | ||
f21c0ed9 AL |
521 | if (vid >= MAX_VLAN) |
522 | return VIRTIO_NET_ERR; | |
523 | ||
524 | if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) | |
525 | n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); | |
526 | else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) | |
527 | n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); | |
528 | else | |
529 | return VIRTIO_NET_ERR; | |
530 | ||
531 | return VIRTIO_NET_OK; | |
532 | } | |
533 | ||
fed699f9 | 534 | static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, |
f8f7c533 | 535 | struct iovec *iov, unsigned int iov_cnt) |
fed699f9 | 536 | { |
f8f7c533 JW |
537 | struct virtio_net_ctrl_mq mq; |
538 | size_t s; | |
539 | uint16_t queues; | |
fed699f9 | 540 | |
f8f7c533 JW |
541 | s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq)); |
542 | if (s != sizeof(mq)) { | |
fed699f9 JW |
543 | return VIRTIO_NET_ERR; |
544 | } | |
545 | ||
546 | if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { | |
547 | return VIRTIO_NET_ERR; | |
548 | } | |
549 | ||
f8f7c533 | 550 | queues = lduw_p(&mq.virtqueue_pairs); |
fed699f9 | 551 | |
f8f7c533 JW |
552 | if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || |
553 | queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || | |
554 | queues > n->max_queues || | |
fed699f9 JW |
555 | !n->multiqueue) { |
556 | return VIRTIO_NET_ERR; | |
557 | } | |
558 | ||
f8f7c533 | 559 | n->curr_queues = queues; |
fed699f9 JW |
560 | /* stop the backend before changing the number of queues to avoid handling a |
561 | * disabled queue */ | |
562 | virtio_net_set_status(&n->vdev, n->vdev.status); | |
563 | virtio_net_set_queues(n); | |
564 | ||
565 | return VIRTIO_NET_OK; | |
566 | } | |
3d11d36c AL |
567 | static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) |
568 | { | |
002437cd | 569 | VirtIONet *n = to_virtio_net(vdev); |
3d11d36c AL |
570 | struct virtio_net_ctrl_hdr ctrl; |
571 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; | |
572 | VirtQueueElement elem; | |
921ac5d0 MT |
573 | size_t s; |
574 | struct iovec *iov; | |
575 | unsigned int iov_cnt; | |
3d11d36c AL |
576 | |
577 | while (virtqueue_pop(vq, &elem)) { | |
921ac5d0 MT |
578 | if (iov_size(elem.in_sg, elem.in_num) < sizeof(status) || |
579 | iov_size(elem.out_sg, elem.out_num) < sizeof(ctrl)) { | |
e7b43f7e | 580 | error_report("virtio-net ctrl missing headers"); |
3d11d36c AL |
581 | exit(1); |
582 | } | |
583 | ||
921ac5d0 MT |
584 | iov = elem.out_sg; |
585 | iov_cnt = elem.out_num; | |
586 | s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl)); | |
587 | iov_discard_front(&iov, &iov_cnt, sizeof(ctrl)); | |
588 | if (s != sizeof(ctrl)) { | |
589 | status = VIRTIO_NET_ERR; | |
dd23454b | 590 | } else if (ctrl.class == VIRTIO_NET_CTRL_RX) { |
921ac5d0 MT |
591 | status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt); |
592 | } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) { | |
593 | status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt); | |
594 | } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) { | |
595 | status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt); | |
fed699f9 | 596 | } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) { |
f8f7c533 | 597 | status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt); |
3d11d36c AL |
598 | } |
599 | ||
921ac5d0 MT |
600 | s = iov_from_buf(elem.in_sg, elem.in_num, 0, &status, sizeof(status)); |
601 | assert(s == sizeof(status)); | |
3d11d36c AL |
602 | |
603 | virtqueue_push(vq, &elem, sizeof(status)); | |
604 | virtio_notify(vdev, vq); | |
605 | } | |
606 | } | |
607 | ||
fbe78f4f AL |
608 | /* RX */ |
609 | ||
610 | static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) | |
611 | { | |
8aeff62d | 612 | VirtIONet *n = to_virtio_net(vdev); |
fed699f9 | 613 | int queue_index = vq2q(virtio_get_queue_index(vq)); |
8aeff62d | 614 | |
fed699f9 | 615 | qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index)); |
fbe78f4f AL |
616 | } |
617 | ||
4e68f7a0 | 618 | static int virtio_net_can_receive(NetClientState *nc) |
fbe78f4f | 619 | { |
cc1f0f45 | 620 | VirtIONet *n = qemu_get_nic_opaque(nc); |
fed699f9 | 621 | VirtIONetQueue *q = virtio_net_get_subqueue(nc); |
0c87e93e | 622 | |
85cf2a8d | 623 | if (!n->vdev.vm_running) { |
95477323 MT |
624 | return 0; |
625 | } | |
cdd5cc12 | 626 | |
fed699f9 JW |
627 | if (nc->queue_index >= n->curr_queues) { |
628 | return 0; | |
629 | } | |
630 | ||
0c87e93e JW |
631 | if (!virtio_queue_ready(q->rx_vq) || |
632 | !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) { | |
fbe78f4f | 633 | return 0; |
0c87e93e | 634 | } |
fbe78f4f | 635 | |
cdd5cc12 MM |
636 | return 1; |
637 | } | |
638 | ||
0c87e93e | 639 | static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize) |
cdd5cc12 | 640 | { |
0c87e93e JW |
641 | VirtIONet *n = q->n; |
642 | if (virtio_queue_empty(q->rx_vq) || | |
fbe78f4f | 643 | (n->mergeable_rx_bufs && |
0c87e93e JW |
644 | !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { |
645 | virtio_queue_set_notification(q->rx_vq, 1); | |
06b12970 TL |
646 | |
647 | /* To avoid a race condition where the guest has made some buffers | |
648 | * available after the above check but before notification was | |
649 | * enabled, check for available buffers again. | |
650 | */ | |
0c87e93e | 651 | if (virtio_queue_empty(q->rx_vq) || |
06b12970 | 652 | (n->mergeable_rx_bufs && |
0c87e93e | 653 | !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { |
06b12970 | 654 | return 0; |
0c87e93e | 655 | } |
fbe78f4f AL |
656 | } |
657 | ||
0c87e93e | 658 | virtio_queue_set_notification(q->rx_vq, 0); |
fbe78f4f AL |
659 | return 1; |
660 | } | |
661 | ||
1d41b0c1 AL |
662 | /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so |
663 | * it never finds out that the packets don't have valid checksums. This | |
664 | * causes dhclient to get upset. Fedora's carried a patch for ages to | |
665 | * fix this with Xen but it hasn't appeared in an upstream release of | |
666 | * dhclient yet. | |
667 | * | |
668 | * To avoid breaking existing guests, we catch udp packets and add | |
669 | * checksums. This is terrible but it's better than hacking the guest | |
670 | * kernels. | |
671 | * | |
672 | * N.B. if we introduce a zero-copy API, this operation is no longer free so | |
673 | * we should provide a mechanism to disable it to avoid polluting the host | |
674 | * cache. | |
675 | */ | |
676 | static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, | |
22cc84db | 677 | uint8_t *buf, size_t size) |
1d41b0c1 AL |
678 | { |
679 | if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ | |
680 | (size > 27 && size < 1500) && /* normal sized MTU */ | |
681 | (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ | |
682 | (buf[23] == 17) && /* ip.protocol == UDP */ | |
683 | (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ | |
22cc84db | 684 | net_checksum_calculate(buf, size); |
1d41b0c1 AL |
685 | hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; |
686 | } | |
687 | } | |
688 | ||
280598b7 MT |
689 | static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt, |
690 | const void *buf, size_t size) | |
fbe78f4f | 691 | { |
3a330134 | 692 | if (n->has_vnet_hdr) { |
22cc84db MT |
693 | /* FIXME this cast is evil */ |
694 | void *wbuf = (void *)buf; | |
280598b7 MT |
695 | work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len, |
696 | size - n->host_hdr_len); | |
697 | iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); | |
22cc84db MT |
698 | } else { |
699 | struct virtio_net_hdr hdr = { | |
700 | .flags = 0, | |
701 | .gso_type = VIRTIO_NET_HDR_GSO_NONE | |
702 | }; | |
703 | iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr); | |
3a330134 | 704 | } |
fbe78f4f AL |
705 | } |
706 | ||
3831ab20 AL |
707 | static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) |
708 | { | |
709 | static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; | |
f21c0ed9 | 710 | static const uint8_t vlan[] = {0x81, 0x00}; |
3831ab20 | 711 | uint8_t *ptr = (uint8_t *)buf; |
b6503ed9 | 712 | int i; |
3831ab20 AL |
713 | |
714 | if (n->promisc) | |
715 | return 1; | |
716 | ||
e043ebc6 | 717 | ptr += n->host_hdr_len; |
3a330134 | 718 | |
f21c0ed9 AL |
719 | if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { |
720 | int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff; | |
721 | if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) | |
722 | return 0; | |
723 | } | |
724 | ||
bbe2f399 AW |
725 | if (ptr[0] & 1) { // multicast |
726 | if (!memcmp(ptr, bcast, sizeof(bcast))) { | |
015cb166 AW |
727 | return !n->nobcast; |
728 | } else if (n->nomulti) { | |
729 | return 0; | |
8fd2a2f1 | 730 | } else if (n->allmulti || n->mac_table.multi_overflow) { |
bbe2f399 AW |
731 | return 1; |
732 | } | |
2d9aba39 AW |
733 | |
734 | for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { | |
735 | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { | |
736 | return 1; | |
737 | } | |
738 | } | |
bbe2f399 | 739 | } else { // unicast |
015cb166 AW |
740 | if (n->nouni) { |
741 | return 0; | |
742 | } else if (n->alluni || n->mac_table.uni_overflow) { | |
8fd2a2f1 AW |
743 | return 1; |
744 | } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { | |
bbe2f399 AW |
745 | return 1; |
746 | } | |
3831ab20 | 747 | |
2d9aba39 AW |
748 | for (i = 0; i < n->mac_table.first_multi; i++) { |
749 | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { | |
750 | return 1; | |
751 | } | |
752 | } | |
b6503ed9 AL |
753 | } |
754 | ||
3831ab20 AL |
755 | return 0; |
756 | } | |
757 | ||
4e68f7a0 | 758 | static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size) |
fbe78f4f | 759 | { |
cc1f0f45 | 760 | VirtIONet *n = qemu_get_nic_opaque(nc); |
fed699f9 | 761 | VirtIONetQueue *q = virtio_net_get_subqueue(nc); |
63c58728 MT |
762 | struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; |
763 | struct virtio_net_hdr_mrg_rxbuf mhdr; | |
764 | unsigned mhdr_cnt = 0; | |
22cc84db | 765 | size_t offset, i, guest_offset; |
fbe78f4f | 766 | |
fed699f9 | 767 | if (!virtio_net_can_receive(nc)) { |
cdd5cc12 | 768 | return -1; |
b356f76d | 769 | } |
cdd5cc12 | 770 | |
940cda94 | 771 | /* hdr_len refers to the header we supply to the guest */ |
0c87e93e | 772 | if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) { |
8aeff62d | 773 | return 0; |
0c87e93e | 774 | } |
fbe78f4f | 775 | |
3831ab20 | 776 | if (!receive_filter(n, buf, size)) |
4f1c942b | 777 | return size; |
3831ab20 | 778 | |
fbe78f4f AL |
779 | offset = i = 0; |
780 | ||
781 | while (offset < size) { | |
782 | VirtQueueElement elem; | |
783 | int len, total; | |
22cc84db | 784 | const struct iovec *sg = elem.in_sg; |
fbe78f4f | 785 | |
22c253d9 | 786 | total = 0; |
fbe78f4f | 787 | |
0c87e93e | 788 | if (virtqueue_pop(q->rx_vq, &elem) == 0) { |
fbe78f4f | 789 | if (i == 0) |
4f1c942b | 790 | return -1; |
e7b43f7e | 791 | error_report("virtio-net unexpected empty queue: " |
279a4253 | 792 | "i %zd mergeable %d offset %zd, size %zd, " |
e7b43f7e | 793 | "guest hdr len %zd, host hdr len %zd guest features 0x%x", |
279a4253 | 794 | i, n->mergeable_rx_bufs, offset, size, |
e35e23f6 | 795 | n->guest_hdr_len, n->host_hdr_len, n->vdev.guest_features); |
fbe78f4f AL |
796 | exit(1); |
797 | } | |
798 | ||
799 | if (elem.in_num < 1) { | |
e7b43f7e | 800 | error_report("virtio-net receive queue contains no in buffers"); |
fbe78f4f AL |
801 | exit(1); |
802 | } | |
803 | ||
fbe78f4f | 804 | if (i == 0) { |
c8d28e7e | 805 | assert(offset == 0); |
63c58728 MT |
806 | if (n->mergeable_rx_bufs) { |
807 | mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), | |
808 | sg, elem.in_num, | |
809 | offsetof(typeof(mhdr), num_buffers), | |
810 | sizeof(mhdr.num_buffers)); | |
811 | } | |
fbe78f4f | 812 | |
c8d28e7e MT |
813 | receive_header(n, sg, elem.in_num, buf, size); |
814 | offset = n->host_hdr_len; | |
e35e23f6 | 815 | total += n->guest_hdr_len; |
22cc84db MT |
816 | guest_offset = n->guest_hdr_len; |
817 | } else { | |
818 | guest_offset = 0; | |
fbe78f4f AL |
819 | } |
820 | ||
821 | /* copy in packet. ugh */ | |
22cc84db | 822 | len = iov_from_buf(sg, elem.in_num, guest_offset, |
dcf6f5e1 | 823 | buf + offset, size - offset); |
fbe78f4f | 824 | total += len; |
279a4253 MT |
825 | offset += len; |
826 | /* If buffers can't be merged, at this point we | |
827 | * must have consumed the complete packet. | |
828 | * Otherwise, drop it. */ | |
829 | if (!n->mergeable_rx_bufs && offset < size) { | |
830 | #if 0 | |
e7b43f7e SH |
831 | error_report("virtio-net truncated non-mergeable packet: " |
832 | "i %zd mergeable %d offset %zd, size %zd, " | |
833 | "guest hdr len %zd, host hdr len %zd", | |
834 | i, n->mergeable_rx_bufs, | |
e35e23f6 | 835 | offset, size, n->guest_hdr_len, n->host_hdr_len); |
279a4253 MT |
836 | #endif |
837 | return size; | |
838 | } | |
fbe78f4f AL |
839 | |
840 | /* signal other side */ | |
0c87e93e | 841 | virtqueue_fill(q->rx_vq, &elem, total, i++); |
fbe78f4f AL |
842 | } |
843 | ||
63c58728 MT |
844 | if (mhdr_cnt) { |
845 | stw_p(&mhdr.num_buffers, i); | |
846 | iov_from_buf(mhdr_sg, mhdr_cnt, | |
847 | 0, | |
848 | &mhdr.num_buffers, sizeof mhdr.num_buffers); | |
44b15bc5 | 849 | } |
fbe78f4f | 850 | |
0c87e93e JW |
851 | virtqueue_flush(q->rx_vq, i); |
852 | virtio_notify(&n->vdev, q->rx_vq); | |
4f1c942b MM |
853 | |
854 | return size; | |
fbe78f4f AL |
855 | } |
856 | ||
0c87e93e | 857 | static int32_t virtio_net_flush_tx(VirtIONetQueue *q); |
6243375f | 858 | |
4e68f7a0 | 859 | static void virtio_net_tx_complete(NetClientState *nc, ssize_t len) |
6243375f | 860 | { |
cc1f0f45 | 861 | VirtIONet *n = qemu_get_nic_opaque(nc); |
fed699f9 | 862 | VirtIONetQueue *q = virtio_net_get_subqueue(nc); |
6243375f | 863 | |
0c87e93e JW |
864 | virtqueue_push(q->tx_vq, &q->async_tx.elem, 0); |
865 | virtio_notify(&n->vdev, q->tx_vq); | |
6243375f | 866 | |
0c87e93e | 867 | q->async_tx.elem.out_num = q->async_tx.len = 0; |
6243375f | 868 | |
0c87e93e JW |
869 | virtio_queue_set_notification(q->tx_vq, 1); |
870 | virtio_net_flush_tx(q); | |
6243375f MM |
871 | } |
872 | ||
fbe78f4f | 873 | /* TX */ |
0c87e93e | 874 | static int32_t virtio_net_flush_tx(VirtIONetQueue *q) |
fbe78f4f | 875 | { |
0c87e93e | 876 | VirtIONet *n = q->n; |
fbe78f4f | 877 | VirtQueueElement elem; |
e3f30488 | 878 | int32_t num_packets = 0; |
fed699f9 | 879 | int queue_index = vq2q(virtio_get_queue_index(q->tx_vq)); |
e3f30488 AW |
880 | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
881 | return num_packets; | |
882 | } | |
fbe78f4f | 883 | |
85cf2a8d | 884 | assert(n->vdev.vm_running); |
783e7706 | 885 | |
0c87e93e JW |
886 | if (q->async_tx.elem.out_num) { |
887 | virtio_queue_set_notification(q->tx_vq, 0); | |
e3f30488 | 888 | return num_packets; |
6243375f MM |
889 | } |
890 | ||
0c87e93e | 891 | while (virtqueue_pop(q->tx_vq, &elem)) { |
14761f9c | 892 | ssize_t ret, len; |
fbe78f4f AL |
893 | unsigned int out_num = elem.out_num; |
894 | struct iovec *out_sg = &elem.out_sg[0]; | |
14761f9c | 895 | struct iovec sg[VIRTQUEUE_MAX_SIZE]; |
fbe78f4f | 896 | |
7b80d08e | 897 | if (out_num < 1) { |
e7b43f7e | 898 | error_report("virtio-net header not in first element"); |
fbe78f4f AL |
899 | exit(1); |
900 | } | |
901 | ||
14761f9c MT |
902 | /* |
903 | * If host wants to see the guest header as is, we can | |
904 | * pass it on unchanged. Otherwise, copy just the parts | |
905 | * that host is interested in. | |
906 | */ | |
907 | assert(n->host_hdr_len <= n->guest_hdr_len); | |
908 | if (n->host_hdr_len != n->guest_hdr_len) { | |
909 | unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), | |
910 | out_sg, out_num, | |
911 | 0, n->host_hdr_len); | |
912 | sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num, | |
913 | out_sg, out_num, | |
914 | n->guest_hdr_len, -1); | |
915 | out_num = sg_num; | |
916 | out_sg = sg; | |
fbe78f4f AL |
917 | } |
918 | ||
7b80d08e | 919 | len = n->guest_hdr_len; |
14761f9c | 920 | |
fed699f9 JW |
921 | ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index), |
922 | out_sg, out_num, virtio_net_tx_complete); | |
6243375f | 923 | if (ret == 0) { |
0c87e93e JW |
924 | virtio_queue_set_notification(q->tx_vq, 0); |
925 | q->async_tx.elem = elem; | |
926 | q->async_tx.len = len; | |
e3f30488 | 927 | return -EBUSY; |
6243375f MM |
928 | } |
929 | ||
930 | len += ret; | |
fbe78f4f | 931 | |
0c87e93e JW |
932 | virtqueue_push(q->tx_vq, &elem, 0); |
933 | virtio_notify(&n->vdev, q->tx_vq); | |
e3f30488 AW |
934 | |
935 | if (++num_packets >= n->tx_burst) { | |
936 | break; | |
937 | } | |
fbe78f4f | 938 | } |
e3f30488 | 939 | return num_packets; |
fbe78f4f AL |
940 | } |
941 | ||
a697a334 | 942 | static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq) |
fbe78f4f AL |
943 | { |
944 | VirtIONet *n = to_virtio_net(vdev); | |
fed699f9 | 945 | VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; |
fbe78f4f | 946 | |
783e7706 | 947 | /* This happens when device was stopped but VCPU wasn't. */ |
85cf2a8d | 948 | if (!n->vdev.vm_running) { |
0c87e93e | 949 | q->tx_waiting = 1; |
783e7706 MT |
950 | return; |
951 | } | |
952 | ||
0c87e93e | 953 | if (q->tx_waiting) { |
fbe78f4f | 954 | virtio_queue_set_notification(vq, 1); |
0c87e93e JW |
955 | qemu_del_timer(q->tx_timer); |
956 | q->tx_waiting = 0; | |
957 | virtio_net_flush_tx(q); | |
fbe78f4f | 958 | } else { |
0c87e93e | 959 | qemu_mod_timer(q->tx_timer, |
74475455 | 960 | qemu_get_clock_ns(vm_clock) + n->tx_timeout); |
0c87e93e | 961 | q->tx_waiting = 1; |
fbe78f4f AL |
962 | virtio_queue_set_notification(vq, 0); |
963 | } | |
964 | } | |
965 | ||
a697a334 AW |
966 | static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) |
967 | { | |
968 | VirtIONet *n = to_virtio_net(vdev); | |
fed699f9 | 969 | VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; |
a697a334 | 970 | |
0c87e93e | 971 | if (unlikely(q->tx_waiting)) { |
a697a334 AW |
972 | return; |
973 | } | |
0c87e93e | 974 | q->tx_waiting = 1; |
783e7706 | 975 | /* This happens when device was stopped but VCPU wasn't. */ |
85cf2a8d | 976 | if (!n->vdev.vm_running) { |
783e7706 MT |
977 | return; |
978 | } | |
a697a334 | 979 | virtio_queue_set_notification(vq, 0); |
0c87e93e | 980 | qemu_bh_schedule(q->tx_bh); |
a697a334 AW |
981 | } |
982 | ||
fbe78f4f AL |
983 | static void virtio_net_tx_timer(void *opaque) |
984 | { | |
0c87e93e JW |
985 | VirtIONetQueue *q = opaque; |
986 | VirtIONet *n = q->n; | |
85cf2a8d | 987 | assert(n->vdev.vm_running); |
fbe78f4f | 988 | |
0c87e93e | 989 | q->tx_waiting = 0; |
fbe78f4f AL |
990 | |
991 | /* Just in case the driver is not ready on more */ | |
992 | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) | |
993 | return; | |
994 | ||
0c87e93e JW |
995 | virtio_queue_set_notification(q->tx_vq, 1); |
996 | virtio_net_flush_tx(q); | |
fbe78f4f AL |
997 | } |
998 | ||
a697a334 AW |
999 | static void virtio_net_tx_bh(void *opaque) |
1000 | { | |
0c87e93e JW |
1001 | VirtIONetQueue *q = opaque; |
1002 | VirtIONet *n = q->n; | |
a697a334 AW |
1003 | int32_t ret; |
1004 | ||
85cf2a8d | 1005 | assert(n->vdev.vm_running); |
783e7706 | 1006 | |
0c87e93e | 1007 | q->tx_waiting = 0; |
a697a334 AW |
1008 | |
1009 | /* Just in case the driver is not ready on more */ | |
1010 | if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))) | |
1011 | return; | |
1012 | ||
0c87e93e | 1013 | ret = virtio_net_flush_tx(q); |
a697a334 AW |
1014 | if (ret == -EBUSY) { |
1015 | return; /* Notification re-enable handled by tx_complete */ | |
1016 | } | |
1017 | ||
1018 | /* If we flush a full burst of packets, assume there are | |
1019 | * more coming and immediately reschedule */ | |
1020 | if (ret >= n->tx_burst) { | |
0c87e93e JW |
1021 | qemu_bh_schedule(q->tx_bh); |
1022 | q->tx_waiting = 1; | |
a697a334 AW |
1023 | return; |
1024 | } | |
1025 | ||
1026 | /* If less than a full burst, re-enable notification and flush | |
1027 | * anything that may have come in while we weren't looking. If | |
1028 | * we find something, assume the guest is still active and reschedule */ | |
0c87e93e JW |
1029 | virtio_queue_set_notification(q->tx_vq, 1); |
1030 | if (virtio_net_flush_tx(q) > 0) { | |
1031 | virtio_queue_set_notification(q->tx_vq, 0); | |
1032 | qemu_bh_schedule(q->tx_bh); | |
1033 | q->tx_waiting = 1; | |
a697a334 AW |
1034 | } |
1035 | } | |
1036 | ||
fed699f9 JW |
1037 | static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue, int ctrl) |
1038 | { | |
1039 | VirtIODevice *vdev = &n->vdev; | |
1040 | int i, max = multiqueue ? n->max_queues : 1; | |
1041 | ||
1042 | n->multiqueue = multiqueue; | |
1043 | ||
1044 | for (i = 2; i <= n->max_queues * 2 + 1; i++) { | |
1045 | virtio_del_queue(vdev, i); | |
1046 | } | |
1047 | ||
1048 | for (i = 1; i < max; i++) { | |
1049 | n->vqs[i].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx); | |
1050 | if (n->vqs[i].tx_timer) { | |
1051 | n->vqs[i].tx_vq = | |
1052 | virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer); | |
1053 | n->vqs[i].tx_timer = qemu_new_timer_ns(vm_clock, | |
1054 | virtio_net_tx_timer, | |
1055 | &n->vqs[i]); | |
1056 | } else { | |
1057 | n->vqs[i].tx_vq = | |
1058 | virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh); | |
1059 | n->vqs[i].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[i]); | |
1060 | } | |
1061 | ||
1062 | n->vqs[i].tx_waiting = 0; | |
1063 | n->vqs[i].n = n; | |
1064 | } | |
1065 | ||
1066 | if (ctrl) { | |
1067 | n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); | |
1068 | } | |
1069 | ||
1070 | virtio_net_set_queues(n); | |
1071 | } | |
1072 | ||
fbe78f4f AL |
1073 | static void virtio_net_save(QEMUFile *f, void *opaque) |
1074 | { | |
5f800801 | 1075 | int i; |
fbe78f4f AL |
1076 | VirtIONet *n = opaque; |
1077 | ||
afbaa7b4 MT |
1078 | /* At this point, backend must be stopped, otherwise |
1079 | * it might keep writing to memory. */ | |
1080 | assert(!n->vhost_started); | |
fbe78f4f AL |
1081 | virtio_save(&n->vdev, f); |
1082 | ||
79674068 | 1083 | qemu_put_buffer(f, n->mac, ETH_ALEN); |
5f800801 | 1084 | qemu_put_be32(f, n->vqs[0].tx_waiting); |
e46cb38f | 1085 | qemu_put_be32(f, n->mergeable_rx_bufs); |
9d6271b8 | 1086 | qemu_put_be16(f, n->status); |
f10c592e AW |
1087 | qemu_put_byte(f, n->promisc); |
1088 | qemu_put_byte(f, n->allmulti); | |
b6503ed9 AL |
1089 | qemu_put_be32(f, n->mac_table.in_use); |
1090 | qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN); | |
f21c0ed9 | 1091 | qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); |
3a330134 | 1092 | qemu_put_be32(f, n->has_vnet_hdr); |
8fd2a2f1 AW |
1093 | qemu_put_byte(f, n->mac_table.multi_overflow); |
1094 | qemu_put_byte(f, n->mac_table.uni_overflow); | |
015cb166 AW |
1095 | qemu_put_byte(f, n->alluni); |
1096 | qemu_put_byte(f, n->nomulti); | |
1097 | qemu_put_byte(f, n->nouni); | |
1098 | qemu_put_byte(f, n->nobcast); | |
0ce0e8f4 | 1099 | qemu_put_byte(f, n->has_ufo); |
5f800801 JW |
1100 | if (n->max_queues > 1) { |
1101 | qemu_put_be16(f, n->max_queues); | |
1102 | qemu_put_be16(f, n->curr_queues); | |
1103 | for (i = 1; i < n->curr_queues; i++) { | |
1104 | qemu_put_be32(f, n->vqs[i].tx_waiting); | |
1105 | } | |
1106 | } | |
fbe78f4f AL |
1107 | } |
1108 | ||
1109 | static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) | |
1110 | { | |
1111 | VirtIONet *n = opaque; | |
5f800801 | 1112 | int ret, i, link_down; |
fbe78f4f | 1113 | |
9d6271b8 | 1114 | if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION) |
fbe78f4f AL |
1115 | return -EINVAL; |
1116 | ||
2a633c46 OW |
1117 | ret = virtio_load(&n->vdev, f); |
1118 | if (ret) { | |
1119 | return ret; | |
1120 | } | |
fbe78f4f | 1121 | |
79674068 | 1122 | qemu_get_buffer(f, n->mac, ETH_ALEN); |
5f800801 | 1123 | n->vqs[0].tx_waiting = qemu_get_be32(f); |
ff3a8066 MT |
1124 | |
1125 | virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f)); | |
fbe78f4f | 1126 | |
9d6271b8 AL |
1127 | if (version_id >= 3) |
1128 | n->status = qemu_get_be16(f); | |
1129 | ||
002437cd | 1130 | if (version_id >= 4) { |
f10c592e AW |
1131 | if (version_id < 8) { |
1132 | n->promisc = qemu_get_be32(f); | |
1133 | n->allmulti = qemu_get_be32(f); | |
1134 | } else { | |
1135 | n->promisc = qemu_get_byte(f); | |
1136 | n->allmulti = qemu_get_byte(f); | |
1137 | } | |
002437cd AL |
1138 | } |
1139 | ||
b6503ed9 AL |
1140 | if (version_id >= 5) { |
1141 | n->mac_table.in_use = qemu_get_be32(f); | |
1142 | /* MAC_TABLE_ENTRIES may be different from the saved image */ | |
1143 | if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) { | |
1144 | qemu_get_buffer(f, n->mac_table.macs, | |
1145 | n->mac_table.in_use * ETH_ALEN); | |
1146 | } else if (n->mac_table.in_use) { | |
e398d61b JQ |
1147 | uint8_t *buf = g_malloc0(n->mac_table.in_use); |
1148 | qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN); | |
1149 | g_free(buf); | |
8fd2a2f1 | 1150 | n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1; |
b6503ed9 AL |
1151 | n->mac_table.in_use = 0; |
1152 | } | |
1153 | } | |
1154 | ||
f21c0ed9 AL |
1155 | if (version_id >= 6) |
1156 | qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); | |
1157 | ||
3a330134 MM |
1158 | if (version_id >= 7) { |
1159 | if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) { | |
1ecda02b | 1160 | error_report("virtio-net: saved image requires vnet_hdr=on"); |
3a330134 MM |
1161 | return -1; |
1162 | } | |
1163 | ||
1164 | if (n->has_vnet_hdr) { | |
b356f76d | 1165 | tap_set_offload(qemu_get_queue(n->nic)->peer, |
704a76fc MT |
1166 | (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1, |
1167 | (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1, | |
1168 | (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1, | |
1169 | (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN) & 1, | |
1170 | (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO) & 1); | |
3a330134 | 1171 | } |
6c042c16 AW |
1172 | } |
1173 | ||
8fd2a2f1 AW |
1174 | if (version_id >= 9) { |
1175 | n->mac_table.multi_overflow = qemu_get_byte(f); | |
1176 | n->mac_table.uni_overflow = qemu_get_byte(f); | |
1177 | } | |
1178 | ||
015cb166 AW |
1179 | if (version_id >= 10) { |
1180 | n->alluni = qemu_get_byte(f); | |
1181 | n->nomulti = qemu_get_byte(f); | |
1182 | n->nouni = qemu_get_byte(f); | |
1183 | n->nobcast = qemu_get_byte(f); | |
1184 | } | |
1185 | ||
0ce0e8f4 MM |
1186 | if (version_id >= 11) { |
1187 | if (qemu_get_byte(f) && !peer_has_ufo(n)) { | |
1ecda02b | 1188 | error_report("virtio-net: saved image requires TUN_F_UFO support"); |
0ce0e8f4 MM |
1189 | return -1; |
1190 | } | |
1191 | } | |
1192 | ||
5f800801 JW |
1193 | if (n->max_queues > 1) { |
1194 | if (n->max_queues != qemu_get_be16(f)) { | |
1195 | error_report("virtio-net: different max_queues "); | |
1196 | return -1; | |
1197 | } | |
1198 | ||
1199 | n->curr_queues = qemu_get_be16(f); | |
1200 | for (i = 1; i < n->curr_queues; i++) { | |
1201 | n->vqs[i].tx_waiting = qemu_get_be32(f); | |
1202 | } | |
1203 | } | |
1204 | ||
1205 | virtio_net_set_queues(n); | |
1206 | ||
2d9aba39 AW |
1207 | /* Find the first multicast entry in the saved MAC filter */ |
1208 | for (i = 0; i < n->mac_table.in_use; i++) { | |
1209 | if (n->mac_table.macs[i * ETH_ALEN] & 1) { | |
1210 | break; | |
1211 | } | |
1212 | } | |
1213 | n->mac_table.first_multi = i; | |
98991481 AK |
1214 | |
1215 | /* nc.link_down can't be migrated, so infer link_down according | |
1216 | * to link status bit in n->status */ | |
5f800801 JW |
1217 | link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0; |
1218 | for (i = 0; i < n->max_queues; i++) { | |
1219 | qemu_get_subqueue(n->nic, i)->link_down = link_down; | |
1220 | } | |
98991481 | 1221 | |
fbe78f4f AL |
1222 | return 0; |
1223 | } | |
1224 | ||
4e68f7a0 | 1225 | static void virtio_net_cleanup(NetClientState *nc) |
b946a153 | 1226 | { |
cc1f0f45 | 1227 | VirtIONet *n = qemu_get_nic_opaque(nc); |
b946a153 | 1228 | |
eb6b6c12 | 1229 | n->nic = NULL; |
b946a153 AL |
1230 | } |
1231 | ||
eb6b6c12 | 1232 | static NetClientInfo net_virtio_info = { |
2be64a68 | 1233 | .type = NET_CLIENT_OPTIONS_KIND_NIC, |
eb6b6c12 MM |
1234 | .size = sizeof(NICState), |
1235 | .can_receive = virtio_net_can_receive, | |
1236 | .receive = virtio_net_receive, | |
1237 | .cleanup = virtio_net_cleanup, | |
1238 | .link_status_changed = virtio_net_set_link_status, | |
1239 | }; | |
1240 | ||
f56a1247 MT |
1241 | static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) |
1242 | { | |
1243 | VirtIONet *n = to_virtio_net(vdev); | |
fed699f9 | 1244 | NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); |
f56a1247 | 1245 | assert(n->vhost_started); |
b356f76d | 1246 | return vhost_net_virtqueue_pending(tap_get_vhost_net(nc->peer), idx); |
f56a1247 MT |
1247 | } |
1248 | ||
1249 | static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx, | |
1250 | bool mask) | |
1251 | { | |
1252 | VirtIONet *n = to_virtio_net(vdev); | |
fed699f9 | 1253 | NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); |
f56a1247 | 1254 | assert(n->vhost_started); |
b356f76d | 1255 | vhost_net_virtqueue_mask(tap_get_vhost_net(nc->peer), |
f56a1247 MT |
1256 | vdev, idx, mask); |
1257 | } | |
1258 | ||
17ec5a86 | 1259 | void virtio_net_set_config_size(VirtIONet *n, uint32_t host_features) |
fbe78f4f | 1260 | { |
14f9b664 | 1261 | int i, config_size = 0; |
14f9b664 JL |
1262 | for (i = 0; feature_sizes[i].flags != 0; i++) { |
1263 | if (host_features & feature_sizes[i].flags) { | |
1264 | config_size = MAX(feature_sizes[i].end, config_size); | |
1265 | } | |
1266 | } | |
17ec5a86 FK |
1267 | n->config_size = config_size; |
1268 | } | |
1269 | ||
1270 | static VirtIODevice *virtio_net_common_init(DeviceState *dev, NICConf *conf, | |
1271 | virtio_net_conf *net, | |
1272 | uint32_t host_features, | |
1273 | VirtIONet **pn) | |
1274 | { | |
1275 | VirtIONet *n = *pn; | |
1276 | int i, config_size = 0; | |
fbe78f4f | 1277 | |
17ec5a86 FK |
1278 | /* |
1279 | * We have two cases here: the old virtio-net-pci device, and the | |
1280 | * refactored virtio-net. | |
1281 | */ | |
1282 | if (n == NULL) { | |
1283 | /* virtio-net-pci */ | |
1284 | for (i = 0; feature_sizes[i].flags != 0; i++) { | |
1285 | if (host_features & feature_sizes[i].flags) { | |
1286 | config_size = MAX(feature_sizes[i].end, config_size); | |
1287 | } | |
1288 | } | |
1289 | n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET, | |
1290 | config_size, sizeof(VirtIONet)); | |
1291 | n->config_size = config_size; | |
1292 | } else { | |
1293 | /* virtio-net */ | |
1294 | virtio_init(VIRTIO_DEVICE(n), "virtio-net", VIRTIO_ID_NET, | |
1295 | n->config_size); | |
1296 | } | |
fbe78f4f | 1297 | |
0f03eca6 AL |
1298 | n->vdev.get_config = virtio_net_get_config; |
1299 | n->vdev.set_config = virtio_net_set_config; | |
fbe78f4f AL |
1300 | n->vdev.get_features = virtio_net_get_features; |
1301 | n->vdev.set_features = virtio_net_set_features; | |
8eca6b1b | 1302 | n->vdev.bad_features = virtio_net_bad_features; |
002437cd | 1303 | n->vdev.reset = virtio_net_reset; |
9bc6304c | 1304 | n->vdev.set_status = virtio_net_set_status; |
f56a1247 MT |
1305 | n->vdev.guest_notifier_mask = virtio_net_guest_notifier_mask; |
1306 | n->vdev.guest_notifier_pending = virtio_net_guest_notifier_pending; | |
f6b26cf2 JW |
1307 | n->max_queues = MAX(conf->queues, 1); |
1308 | n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues); | |
fed699f9 | 1309 | n->vqs[0].rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx); |
fed699f9 JW |
1310 | n->curr_queues = 1; |
1311 | n->vqs[0].n = n; | |
1312 | n->tx_timeout = net->txtimer; | |
a697a334 AW |
1313 | |
1314 | if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) { | |
e7b43f7e SH |
1315 | error_report("virtio-net: " |
1316 | "Unknown option tx=%s, valid options: \"timer\" \"bh\"", | |
1317 | net->tx); | |
1318 | error_report("Defaulting to \"bh\""); | |
a697a334 AW |
1319 | } |
1320 | ||
1321 | if (net->tx && !strcmp(net->tx, "timer")) { | |
fed699f9 JW |
1322 | n->vqs[0].tx_vq = virtio_add_queue(&n->vdev, 256, |
1323 | virtio_net_handle_tx_timer); | |
1324 | n->vqs[0].tx_timer = qemu_new_timer_ns(vm_clock, virtio_net_tx_timer, | |
1325 | &n->vqs[0]); | |
a697a334 | 1326 | } else { |
fed699f9 JW |
1327 | n->vqs[0].tx_vq = virtio_add_queue(&n->vdev, 256, |
1328 | virtio_net_handle_tx_bh); | |
1329 | n->vqs[0].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[0]); | |
a697a334 | 1330 | } |
4ffb17f5 | 1331 | n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl); |
97b15621 | 1332 | qemu_macaddr_default_if_unset(&conf->macaddr); |
3cbe04c4 | 1333 | memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac)); |
554c97dd | 1334 | n->status = VIRTIO_NET_S_LINK_UP; |
fbe78f4f | 1335 | |
f79f2bfc | 1336 | n->nic = qemu_new_nic(&net_virtio_info, conf, object_get_typename(OBJECT(dev)), dev->id, n); |
6e371ab8 MT |
1337 | peer_test_vnet_hdr(n); |
1338 | if (peer_has_vnet_hdr(n)) { | |
fed699f9 JW |
1339 | for (i = 0; i < n->max_queues; i++) { |
1340 | tap_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true); | |
1341 | } | |
6e371ab8 MT |
1342 | n->host_hdr_len = sizeof(struct virtio_net_hdr); |
1343 | } else { | |
1344 | n->host_hdr_len = 0; | |
1345 | } | |
eb6b6c12 | 1346 | |
b356f76d | 1347 | qemu_format_nic_info_str(qemu_get_queue(n->nic), conf->macaddr.a); |
96d5e201 | 1348 | |
fed699f9 | 1349 | n->vqs[0].tx_waiting = 0; |
e3f30488 | 1350 | n->tx_burst = net->txburst; |
ff3a8066 | 1351 | virtio_net_set_mrg_rx_bufs(n, 0); |
002437cd | 1352 | n->promisc = 1; /* for compatibility */ |
fbe78f4f | 1353 | |
7267c094 | 1354 | n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); |
b6503ed9 | 1355 | |
7267c094 | 1356 | n->vlans = g_malloc0(MAX_VLAN >> 3); |
f21c0ed9 | 1357 | |
01657c86 AW |
1358 | n->qdev = dev; |
1359 | register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION, | |
fbe78f4f | 1360 | virtio_net_save, virtio_net_load, n); |
cf21e106 | 1361 | |
1ca4d09a GN |
1362 | add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0"); |
1363 | ||
53c25cea | 1364 | return &n->vdev; |
cf21e106 | 1365 | } |
97b15621 | 1366 | |
17ec5a86 FK |
1367 | VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf, |
1368 | virtio_net_conf *net, uint32_t host_features) | |
1369 | { | |
1370 | VirtIONet *n = NULL; | |
1371 | return virtio_net_common_init(dev, conf, net, host_features, &n); | |
1372 | } | |
1373 | ||
97b15621 GH |
1374 | void virtio_net_exit(VirtIODevice *vdev) |
1375 | { | |
1376 | VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev); | |
fed699f9 | 1377 | int i; |
9bc6304c | 1378 | |
afbaa7b4 MT |
1379 | /* This will stop vhost backend if appropriate. */ |
1380 | virtio_net_set_status(vdev, 0); | |
97b15621 | 1381 | |
01657c86 | 1382 | unregister_savevm(n->qdev, "virtio-net", n); |
97b15621 | 1383 | |
7267c094 AL |
1384 | g_free(n->mac_table.macs); |
1385 | g_free(n->vlans); | |
97b15621 | 1386 | |
fed699f9 JW |
1387 | for (i = 0; i < n->max_queues; i++) { |
1388 | VirtIONetQueue *q = &n->vqs[i]; | |
1389 | NetClientState *nc = qemu_get_subqueue(n->nic, i); | |
1390 | ||
1391 | qemu_purge_queued_packets(nc); | |
1392 | ||
1393 | if (q->tx_timer) { | |
1394 | qemu_del_timer(q->tx_timer); | |
1395 | qemu_free_timer(q->tx_timer); | |
1396 | } else { | |
1397 | qemu_bh_delete(q->tx_bh); | |
1398 | } | |
a697a334 | 1399 | } |
97b15621 | 1400 | |
f6b26cf2 | 1401 | g_free(n->vqs); |
948ecf21 | 1402 | qemu_del_nic(n->nic); |
b52dfd71 | 1403 | virtio_cleanup(&n->vdev); |
97b15621 | 1404 | } |
17ec5a86 FK |
1405 | |
1406 | static int virtio_net_device_init(VirtIODevice *vdev) | |
1407 | { | |
1408 | DeviceState *qdev = DEVICE(vdev); | |
1409 | VirtIONet *n = VIRTIO_NET(vdev); | |
1410 | ||
1411 | /* | |
1412 | * Initially, the new VirtIONet device will have a config size = | |
1413 | * sizeof(struct config), because we can't get host_features here. | |
1414 | */ | |
1415 | if (virtio_net_common_init(qdev, &(n->nic_conf), | |
1416 | &(n->net_conf), 0, &n) == NULL) { | |
1417 | return -1; | |
1418 | } | |
1419 | return 0; | |
1420 | } | |
1421 | ||
1422 | static int virtio_net_device_exit(DeviceState *qdev) | |
1423 | { | |
1424 | VirtIONet *n = VIRTIO_NET(qdev); | |
1425 | VirtIODevice *vdev = VIRTIO_DEVICE(qdev); | |
1426 | int i; | |
1427 | ||
1428 | /* This will stop vhost backend if appropriate. */ | |
1429 | virtio_net_set_status(vdev, 0); | |
1430 | ||
1431 | unregister_savevm(qdev, "virtio-net", n); | |
1432 | ||
1433 | g_free(n->mac_table.macs); | |
1434 | g_free(n->vlans); | |
1435 | ||
1436 | for (i = 0; i < n->max_queues; i++) { | |
1437 | VirtIONetQueue *q = &n->vqs[i]; | |
1438 | NetClientState *nc = qemu_get_subqueue(n->nic, i); | |
1439 | ||
1440 | qemu_purge_queued_packets(nc); | |
1441 | ||
1442 | if (q->tx_timer) { | |
1443 | qemu_del_timer(q->tx_timer); | |
1444 | qemu_free_timer(q->tx_timer); | |
1445 | } else { | |
1446 | qemu_bh_delete(q->tx_bh); | |
1447 | } | |
1448 | } | |
1449 | ||
1450 | g_free(n->vqs); | |
1451 | qemu_del_nic(n->nic); | |
1452 | virtio_common_cleanup(&n->vdev); | |
1453 | ||
1454 | return 0; | |
1455 | } | |
1456 | ||
1457 | static void virtio_net_instance_init(Object *obj) | |
1458 | { | |
1459 | VirtIONet *n = VIRTIO_NET(obj); | |
1460 | ||
1461 | /* | |
1462 | * The default config_size is sizeof(struct virtio_net_config). | |
1463 | * Can be overriden with virtio_net_set_config_size. | |
1464 | */ | |
1465 | n->config_size = sizeof(struct virtio_net_config); | |
1466 | } | |
1467 | ||
1468 | static Property virtio_net_properties[] = { | |
1469 | DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf), | |
1470 | DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer, | |
1471 | TX_TIMER_INTERVAL), | |
1472 | DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST), | |
1473 | DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx), | |
1474 | DEFINE_PROP_END_OF_LIST(), | |
1475 | }; | |
1476 | ||
1477 | static void virtio_net_class_init(ObjectClass *klass, void *data) | |
1478 | { | |
1479 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1480 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | |
1481 | dc->exit = virtio_net_device_exit; | |
1482 | dc->props = virtio_net_properties; | |
1483 | vdc->init = virtio_net_device_init; | |
1484 | vdc->get_config = virtio_net_get_config; | |
1485 | vdc->set_config = virtio_net_set_config; | |
1486 | vdc->get_features = virtio_net_get_features; | |
1487 | vdc->set_features = virtio_net_set_features; | |
1488 | vdc->bad_features = virtio_net_bad_features; | |
1489 | vdc->reset = virtio_net_reset; | |
1490 | vdc->set_status = virtio_net_set_status; | |
1491 | vdc->guest_notifier_mask = virtio_net_guest_notifier_mask; | |
1492 | vdc->guest_notifier_pending = virtio_net_guest_notifier_pending; | |
1493 | } | |
1494 | ||
1495 | static const TypeInfo virtio_net_info = { | |
1496 | .name = TYPE_VIRTIO_NET, | |
1497 | .parent = TYPE_VIRTIO_DEVICE, | |
1498 | .instance_size = sizeof(VirtIONet), | |
1499 | .instance_init = virtio_net_instance_init, | |
1500 | .class_init = virtio_net_class_init, | |
1501 | }; | |
1502 | ||
1503 | static void virtio_register_types(void) | |
1504 | { | |
1505 | type_register_static(&virtio_net_info); | |
1506 | } | |
1507 | ||
1508 | type_init(virtio_register_types) |