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