]>
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 | |
d7108d90 JW |
117 | if (!!n->vhost_started == |
118 | (virtio_net_started(n, status) && !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 | ||
ec57db16 | 312 | static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue); |
fed699f9 | 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 | ||
644c9858 DF |
362 | static void virtio_net_apply_guest_offloads(VirtIONet *n) |
363 | { | |
364 | tap_set_offload(qemu_get_subqueue(n->nic, 0)->peer, | |
365 | !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_CSUM)), | |
366 | !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO4)), | |
367 | !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_TSO6)), | |
368 | !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_ECN)), | |
369 | !!(n->curr_guest_offloads & (1ULL << VIRTIO_NET_F_GUEST_UFO))); | |
370 | } | |
371 | ||
372 | static uint64_t virtio_net_guest_offloads_by_features(uint32_t features) | |
373 | { | |
374 | static const uint64_t guest_offloads_mask = | |
375 | (1ULL << VIRTIO_NET_F_GUEST_CSUM) | | |
376 | (1ULL << VIRTIO_NET_F_GUEST_TSO4) | | |
377 | (1ULL << VIRTIO_NET_F_GUEST_TSO6) | | |
378 | (1ULL << VIRTIO_NET_F_GUEST_ECN) | | |
379 | (1ULL << VIRTIO_NET_F_GUEST_UFO); | |
380 | ||
381 | return guest_offloads_mask & features; | |
382 | } | |
383 | ||
384 | static inline uint64_t virtio_net_supported_guest_offloads(VirtIONet *n) | |
385 | { | |
386 | VirtIODevice *vdev = VIRTIO_DEVICE(n); | |
387 | return virtio_net_guest_offloads_by_features(vdev->guest_features); | |
388 | } | |
389 | ||
fbe78f4f AL |
390 | static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features) |
391 | { | |
17a0ca55 | 392 | VirtIONet *n = VIRTIO_NET(vdev); |
fed699f9 JW |
393 | int i; |
394 | ||
ec57db16 | 395 | virtio_net_set_multiqueue(n, !!(features & (1 << VIRTIO_NET_F_MQ))); |
fbe78f4f | 396 | |
ff3a8066 | 397 | virtio_net_set_mrg_rx_bufs(n, !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF))); |
f5436dd9 MM |
398 | |
399 | if (n->has_vnet_hdr) { | |
644c9858 DF |
400 | n->curr_guest_offloads = |
401 | virtio_net_guest_offloads_by_features(features); | |
402 | virtio_net_apply_guest_offloads(n); | |
f5436dd9 | 403 | } |
fed699f9 JW |
404 | |
405 | for (i = 0; i < n->max_queues; i++) { | |
406 | NetClientState *nc = qemu_get_subqueue(n->nic, i); | |
407 | ||
408 | if (!nc->peer || nc->peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) { | |
409 | continue; | |
410 | } | |
411 | if (!tap_get_vhost_net(nc->peer)) { | |
412 | continue; | |
413 | } | |
414 | vhost_net_ack_features(tap_get_vhost_net(nc->peer), features); | |
dc14a397 | 415 | } |
fbe78f4f AL |
416 | } |
417 | ||
002437cd | 418 | static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, |
921ac5d0 | 419 | struct iovec *iov, unsigned int iov_cnt) |
002437cd AL |
420 | { |
421 | uint8_t on; | |
921ac5d0 | 422 | size_t s; |
002437cd | 423 | |
921ac5d0 MT |
424 | s = iov_to_buf(iov, iov_cnt, 0, &on, sizeof(on)); |
425 | if (s != sizeof(on)) { | |
426 | return VIRTIO_NET_ERR; | |
002437cd AL |
427 | } |
428 | ||
dd23454b | 429 | if (cmd == VIRTIO_NET_CTRL_RX_PROMISC) { |
002437cd | 430 | n->promisc = on; |
dd23454b | 431 | } else if (cmd == VIRTIO_NET_CTRL_RX_ALLMULTI) { |
002437cd | 432 | n->allmulti = on; |
dd23454b | 433 | } else if (cmd == VIRTIO_NET_CTRL_RX_ALLUNI) { |
015cb166 | 434 | n->alluni = on; |
dd23454b | 435 | } else if (cmd == VIRTIO_NET_CTRL_RX_NOMULTI) { |
015cb166 | 436 | n->nomulti = on; |
dd23454b | 437 | } else if (cmd == VIRTIO_NET_CTRL_RX_NOUNI) { |
015cb166 | 438 | n->nouni = on; |
dd23454b | 439 | } else if (cmd == VIRTIO_NET_CTRL_RX_NOBCAST) { |
015cb166 | 440 | n->nobcast = on; |
921ac5d0 | 441 | } else { |
002437cd | 442 | return VIRTIO_NET_ERR; |
921ac5d0 | 443 | } |
002437cd AL |
444 | |
445 | return VIRTIO_NET_OK; | |
446 | } | |
447 | ||
644c9858 DF |
448 | static int virtio_net_handle_offloads(VirtIONet *n, uint8_t cmd, |
449 | struct iovec *iov, unsigned int iov_cnt) | |
450 | { | |
451 | VirtIODevice *vdev = VIRTIO_DEVICE(n); | |
452 | uint64_t offloads; | |
453 | size_t s; | |
454 | ||
455 | if (!((1 << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) & vdev->guest_features)) { | |
456 | return VIRTIO_NET_ERR; | |
457 | } | |
458 | ||
459 | s = iov_to_buf(iov, iov_cnt, 0, &offloads, sizeof(offloads)); | |
460 | if (s != sizeof(offloads)) { | |
461 | return VIRTIO_NET_ERR; | |
462 | } | |
463 | ||
464 | if (cmd == VIRTIO_NET_CTRL_GUEST_OFFLOADS_SET) { | |
465 | uint64_t supported_offloads; | |
466 | ||
467 | if (!n->has_vnet_hdr) { | |
468 | return VIRTIO_NET_ERR; | |
469 | } | |
470 | ||
471 | supported_offloads = virtio_net_supported_guest_offloads(n); | |
472 | if (offloads & ~supported_offloads) { | |
473 | return VIRTIO_NET_ERR; | |
474 | } | |
475 | ||
476 | n->curr_guest_offloads = offloads; | |
477 | virtio_net_apply_guest_offloads(n); | |
478 | ||
479 | return VIRTIO_NET_OK; | |
480 | } else { | |
481 | return VIRTIO_NET_ERR; | |
482 | } | |
483 | } | |
484 | ||
b6503ed9 | 485 | static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, |
921ac5d0 | 486 | struct iovec *iov, unsigned int iov_cnt) |
b6503ed9 AL |
487 | { |
488 | struct virtio_net_ctrl_mac mac_data; | |
921ac5d0 | 489 | size_t s; |
b6503ed9 | 490 | |
c1943a3f AK |
491 | if (cmd == VIRTIO_NET_CTRL_MAC_ADDR_SET) { |
492 | if (iov_size(iov, iov_cnt) != sizeof(n->mac)) { | |
493 | return VIRTIO_NET_ERR; | |
494 | } | |
495 | s = iov_to_buf(iov, iov_cnt, 0, &n->mac, sizeof(n->mac)); | |
496 | assert(s == sizeof(n->mac)); | |
b356f76d | 497 | qemu_format_nic_info_str(qemu_get_queue(n->nic), n->mac); |
c1943a3f AK |
498 | return VIRTIO_NET_OK; |
499 | } | |
500 | ||
921ac5d0 | 501 | if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET) { |
b6503ed9 | 502 | return VIRTIO_NET_ERR; |
921ac5d0 | 503 | } |
b6503ed9 AL |
504 | |
505 | n->mac_table.in_use = 0; | |
2d9aba39 | 506 | n->mac_table.first_multi = 0; |
8fd2a2f1 AW |
507 | n->mac_table.uni_overflow = 0; |
508 | n->mac_table.multi_overflow = 0; | |
b6503ed9 AL |
509 | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); |
510 | ||
921ac5d0 MT |
511 | s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, |
512 | sizeof(mac_data.entries)); | |
513 | mac_data.entries = ldl_p(&mac_data.entries); | |
514 | if (s != sizeof(mac_data.entries)) { | |
515 | return VIRTIO_NET_ERR; | |
516 | } | |
517 | iov_discard_front(&iov, &iov_cnt, s); | |
b6503ed9 | 518 | |
921ac5d0 | 519 | if (mac_data.entries * ETH_ALEN > iov_size(iov, iov_cnt)) { |
b6503ed9 | 520 | return VIRTIO_NET_ERR; |
921ac5d0 | 521 | } |
b6503ed9 AL |
522 | |
523 | if (mac_data.entries <= MAC_TABLE_ENTRIES) { | |
921ac5d0 MT |
524 | s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs, |
525 | mac_data.entries * ETH_ALEN); | |
526 | if (s != mac_data.entries * ETH_ALEN) { | |
527 | return VIRTIO_NET_ERR; | |
528 | } | |
b6503ed9 AL |
529 | n->mac_table.in_use += mac_data.entries; |
530 | } else { | |
8fd2a2f1 | 531 | n->mac_table.uni_overflow = 1; |
b6503ed9 AL |
532 | } |
533 | ||
921ac5d0 MT |
534 | iov_discard_front(&iov, &iov_cnt, mac_data.entries * ETH_ALEN); |
535 | ||
2d9aba39 AW |
536 | n->mac_table.first_multi = n->mac_table.in_use; |
537 | ||
921ac5d0 MT |
538 | s = iov_to_buf(iov, iov_cnt, 0, &mac_data.entries, |
539 | sizeof(mac_data.entries)); | |
540 | mac_data.entries = ldl_p(&mac_data.entries); | |
541 | if (s != sizeof(mac_data.entries)) { | |
542 | return VIRTIO_NET_ERR; | |
543 | } | |
544 | ||
545 | iov_discard_front(&iov, &iov_cnt, s); | |
b6503ed9 | 546 | |
921ac5d0 | 547 | if (mac_data.entries * ETH_ALEN != iov_size(iov, iov_cnt)) { |
b6503ed9 | 548 | return VIRTIO_NET_ERR; |
921ac5d0 | 549 | } |
b6503ed9 | 550 | |
921ac5d0 MT |
551 | if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) { |
552 | s = iov_to_buf(iov, iov_cnt, 0, n->mac_table.macs, | |
553 | mac_data.entries * ETH_ALEN); | |
554 | if (s != mac_data.entries * ETH_ALEN) { | |
555 | return VIRTIO_NET_ERR; | |
8fd2a2f1 | 556 | } |
921ac5d0 MT |
557 | n->mac_table.in_use += mac_data.entries; |
558 | } else { | |
559 | n->mac_table.multi_overflow = 1; | |
b6503ed9 AL |
560 | } |
561 | ||
562 | return VIRTIO_NET_OK; | |
563 | } | |
564 | ||
f21c0ed9 | 565 | static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, |
921ac5d0 | 566 | struct iovec *iov, unsigned int iov_cnt) |
f21c0ed9 AL |
567 | { |
568 | uint16_t vid; | |
921ac5d0 | 569 | size_t s; |
f21c0ed9 | 570 | |
921ac5d0 MT |
571 | s = iov_to_buf(iov, iov_cnt, 0, &vid, sizeof(vid)); |
572 | vid = lduw_p(&vid); | |
573 | if (s != sizeof(vid)) { | |
f21c0ed9 AL |
574 | return VIRTIO_NET_ERR; |
575 | } | |
576 | ||
f21c0ed9 AL |
577 | if (vid >= MAX_VLAN) |
578 | return VIRTIO_NET_ERR; | |
579 | ||
580 | if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) | |
581 | n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); | |
582 | else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) | |
583 | n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); | |
584 | else | |
585 | return VIRTIO_NET_ERR; | |
586 | ||
587 | return VIRTIO_NET_OK; | |
588 | } | |
589 | ||
fed699f9 | 590 | static int virtio_net_handle_mq(VirtIONet *n, uint8_t cmd, |
f8f7c533 | 591 | struct iovec *iov, unsigned int iov_cnt) |
fed699f9 | 592 | { |
17a0ca55 | 593 | VirtIODevice *vdev = VIRTIO_DEVICE(n); |
f8f7c533 JW |
594 | struct virtio_net_ctrl_mq mq; |
595 | size_t s; | |
596 | uint16_t queues; | |
fed699f9 | 597 | |
f8f7c533 JW |
598 | s = iov_to_buf(iov, iov_cnt, 0, &mq, sizeof(mq)); |
599 | if (s != sizeof(mq)) { | |
fed699f9 JW |
600 | return VIRTIO_NET_ERR; |
601 | } | |
602 | ||
603 | if (cmd != VIRTIO_NET_CTRL_MQ_VQ_PAIRS_SET) { | |
604 | return VIRTIO_NET_ERR; | |
605 | } | |
606 | ||
f8f7c533 | 607 | queues = lduw_p(&mq.virtqueue_pairs); |
fed699f9 | 608 | |
f8f7c533 JW |
609 | if (queues < VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MIN || |
610 | queues > VIRTIO_NET_CTRL_MQ_VQ_PAIRS_MAX || | |
611 | queues > n->max_queues || | |
fed699f9 JW |
612 | !n->multiqueue) { |
613 | return VIRTIO_NET_ERR; | |
614 | } | |
615 | ||
f8f7c533 | 616 | n->curr_queues = queues; |
fed699f9 JW |
617 | /* stop the backend before changing the number of queues to avoid handling a |
618 | * disabled queue */ | |
17a0ca55 | 619 | virtio_net_set_status(vdev, vdev->status); |
fed699f9 JW |
620 | virtio_net_set_queues(n); |
621 | ||
622 | return VIRTIO_NET_OK; | |
623 | } | |
3d11d36c AL |
624 | static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) |
625 | { | |
17a0ca55 | 626 | VirtIONet *n = VIRTIO_NET(vdev); |
3d11d36c AL |
627 | struct virtio_net_ctrl_hdr ctrl; |
628 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; | |
629 | VirtQueueElement elem; | |
921ac5d0 MT |
630 | size_t s; |
631 | struct iovec *iov; | |
632 | unsigned int iov_cnt; | |
3d11d36c AL |
633 | |
634 | while (virtqueue_pop(vq, &elem)) { | |
921ac5d0 MT |
635 | if (iov_size(elem.in_sg, elem.in_num) < sizeof(status) || |
636 | iov_size(elem.out_sg, elem.out_num) < sizeof(ctrl)) { | |
e7b43f7e | 637 | error_report("virtio-net ctrl missing headers"); |
3d11d36c AL |
638 | exit(1); |
639 | } | |
640 | ||
921ac5d0 MT |
641 | iov = elem.out_sg; |
642 | iov_cnt = elem.out_num; | |
643 | s = iov_to_buf(iov, iov_cnt, 0, &ctrl, sizeof(ctrl)); | |
644 | iov_discard_front(&iov, &iov_cnt, sizeof(ctrl)); | |
645 | if (s != sizeof(ctrl)) { | |
646 | status = VIRTIO_NET_ERR; | |
dd23454b | 647 | } else if (ctrl.class == VIRTIO_NET_CTRL_RX) { |
921ac5d0 MT |
648 | status = virtio_net_handle_rx_mode(n, ctrl.cmd, iov, iov_cnt); |
649 | } else if (ctrl.class == VIRTIO_NET_CTRL_MAC) { | |
650 | status = virtio_net_handle_mac(n, ctrl.cmd, iov, iov_cnt); | |
651 | } else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) { | |
652 | status = virtio_net_handle_vlan_table(n, ctrl.cmd, iov, iov_cnt); | |
fed699f9 | 653 | } else if (ctrl.class == VIRTIO_NET_CTRL_MQ) { |
f8f7c533 | 654 | status = virtio_net_handle_mq(n, ctrl.cmd, iov, iov_cnt); |
644c9858 DF |
655 | } else if (ctrl.class == VIRTIO_NET_CTRL_GUEST_OFFLOADS) { |
656 | status = virtio_net_handle_offloads(n, ctrl.cmd, iov, iov_cnt); | |
3d11d36c AL |
657 | } |
658 | ||
921ac5d0 MT |
659 | s = iov_from_buf(elem.in_sg, elem.in_num, 0, &status, sizeof(status)); |
660 | assert(s == sizeof(status)); | |
3d11d36c AL |
661 | |
662 | virtqueue_push(vq, &elem, sizeof(status)); | |
663 | virtio_notify(vdev, vq); | |
664 | } | |
665 | } | |
666 | ||
fbe78f4f AL |
667 | /* RX */ |
668 | ||
669 | static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) | |
670 | { | |
17a0ca55 | 671 | VirtIONet *n = VIRTIO_NET(vdev); |
fed699f9 | 672 | int queue_index = vq2q(virtio_get_queue_index(vq)); |
8aeff62d | 673 | |
fed699f9 | 674 | qemu_flush_queued_packets(qemu_get_subqueue(n->nic, queue_index)); |
fbe78f4f AL |
675 | } |
676 | ||
4e68f7a0 | 677 | static int virtio_net_can_receive(NetClientState *nc) |
fbe78f4f | 678 | { |
cc1f0f45 | 679 | VirtIONet *n = qemu_get_nic_opaque(nc); |
17a0ca55 | 680 | VirtIODevice *vdev = VIRTIO_DEVICE(n); |
fed699f9 | 681 | VirtIONetQueue *q = virtio_net_get_subqueue(nc); |
0c87e93e | 682 | |
17a0ca55 | 683 | if (!vdev->vm_running) { |
95477323 MT |
684 | return 0; |
685 | } | |
cdd5cc12 | 686 | |
fed699f9 JW |
687 | if (nc->queue_index >= n->curr_queues) { |
688 | return 0; | |
689 | } | |
690 | ||
0c87e93e | 691 | if (!virtio_queue_ready(q->rx_vq) || |
17a0ca55 | 692 | !(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
fbe78f4f | 693 | return 0; |
0c87e93e | 694 | } |
fbe78f4f | 695 | |
cdd5cc12 MM |
696 | return 1; |
697 | } | |
698 | ||
0c87e93e | 699 | static int virtio_net_has_buffers(VirtIONetQueue *q, int bufsize) |
cdd5cc12 | 700 | { |
0c87e93e JW |
701 | VirtIONet *n = q->n; |
702 | if (virtio_queue_empty(q->rx_vq) || | |
fbe78f4f | 703 | (n->mergeable_rx_bufs && |
0c87e93e JW |
704 | !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { |
705 | virtio_queue_set_notification(q->rx_vq, 1); | |
06b12970 TL |
706 | |
707 | /* To avoid a race condition where the guest has made some buffers | |
708 | * available after the above check but before notification was | |
709 | * enabled, check for available buffers again. | |
710 | */ | |
0c87e93e | 711 | if (virtio_queue_empty(q->rx_vq) || |
06b12970 | 712 | (n->mergeable_rx_bufs && |
0c87e93e | 713 | !virtqueue_avail_bytes(q->rx_vq, bufsize, 0))) { |
06b12970 | 714 | return 0; |
0c87e93e | 715 | } |
fbe78f4f AL |
716 | } |
717 | ||
0c87e93e | 718 | virtio_queue_set_notification(q->rx_vq, 0); |
fbe78f4f AL |
719 | return 1; |
720 | } | |
721 | ||
1d41b0c1 AL |
722 | /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so |
723 | * it never finds out that the packets don't have valid checksums. This | |
724 | * causes dhclient to get upset. Fedora's carried a patch for ages to | |
725 | * fix this with Xen but it hasn't appeared in an upstream release of | |
726 | * dhclient yet. | |
727 | * | |
728 | * To avoid breaking existing guests, we catch udp packets and add | |
729 | * checksums. This is terrible but it's better than hacking the guest | |
730 | * kernels. | |
731 | * | |
732 | * N.B. if we introduce a zero-copy API, this operation is no longer free so | |
733 | * we should provide a mechanism to disable it to avoid polluting the host | |
734 | * cache. | |
735 | */ | |
736 | static void work_around_broken_dhclient(struct virtio_net_hdr *hdr, | |
22cc84db | 737 | uint8_t *buf, size_t size) |
1d41b0c1 AL |
738 | { |
739 | if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */ | |
740 | (size > 27 && size < 1500) && /* normal sized MTU */ | |
741 | (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */ | |
742 | (buf[23] == 17) && /* ip.protocol == UDP */ | |
743 | (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */ | |
22cc84db | 744 | net_checksum_calculate(buf, size); |
1d41b0c1 AL |
745 | hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM; |
746 | } | |
747 | } | |
748 | ||
280598b7 MT |
749 | static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt, |
750 | const void *buf, size_t size) | |
fbe78f4f | 751 | { |
3a330134 | 752 | if (n->has_vnet_hdr) { |
22cc84db MT |
753 | /* FIXME this cast is evil */ |
754 | void *wbuf = (void *)buf; | |
280598b7 MT |
755 | work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len, |
756 | size - n->host_hdr_len); | |
757 | iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr)); | |
22cc84db MT |
758 | } else { |
759 | struct virtio_net_hdr hdr = { | |
760 | .flags = 0, | |
761 | .gso_type = VIRTIO_NET_HDR_GSO_NONE | |
762 | }; | |
763 | iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr); | |
3a330134 | 764 | } |
fbe78f4f AL |
765 | } |
766 | ||
3831ab20 AL |
767 | static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) |
768 | { | |
769 | static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; | |
f21c0ed9 | 770 | static const uint8_t vlan[] = {0x81, 0x00}; |
3831ab20 | 771 | uint8_t *ptr = (uint8_t *)buf; |
b6503ed9 | 772 | int i; |
3831ab20 AL |
773 | |
774 | if (n->promisc) | |
775 | return 1; | |
776 | ||
e043ebc6 | 777 | ptr += n->host_hdr_len; |
3a330134 | 778 | |
f21c0ed9 AL |
779 | if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { |
780 | int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff; | |
781 | if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) | |
782 | return 0; | |
783 | } | |
784 | ||
bbe2f399 AW |
785 | if (ptr[0] & 1) { // multicast |
786 | if (!memcmp(ptr, bcast, sizeof(bcast))) { | |
015cb166 AW |
787 | return !n->nobcast; |
788 | } else if (n->nomulti) { | |
789 | return 0; | |
8fd2a2f1 | 790 | } else if (n->allmulti || n->mac_table.multi_overflow) { |
bbe2f399 AW |
791 | return 1; |
792 | } | |
2d9aba39 AW |
793 | |
794 | for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { | |
795 | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { | |
796 | return 1; | |
797 | } | |
798 | } | |
bbe2f399 | 799 | } else { // unicast |
015cb166 AW |
800 | if (n->nouni) { |
801 | return 0; | |
802 | } else if (n->alluni || n->mac_table.uni_overflow) { | |
8fd2a2f1 AW |
803 | return 1; |
804 | } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { | |
bbe2f399 AW |
805 | return 1; |
806 | } | |
3831ab20 | 807 | |
2d9aba39 AW |
808 | for (i = 0; i < n->mac_table.first_multi; i++) { |
809 | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { | |
810 | return 1; | |
811 | } | |
812 | } | |
b6503ed9 AL |
813 | } |
814 | ||
3831ab20 AL |
815 | return 0; |
816 | } | |
817 | ||
4e68f7a0 | 818 | static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size) |
fbe78f4f | 819 | { |
cc1f0f45 | 820 | VirtIONet *n = qemu_get_nic_opaque(nc); |
fed699f9 | 821 | VirtIONetQueue *q = virtio_net_get_subqueue(nc); |
17a0ca55 | 822 | VirtIODevice *vdev = VIRTIO_DEVICE(n); |
63c58728 MT |
823 | struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE]; |
824 | struct virtio_net_hdr_mrg_rxbuf mhdr; | |
825 | unsigned mhdr_cnt = 0; | |
22cc84db | 826 | size_t offset, i, guest_offset; |
fbe78f4f | 827 | |
fed699f9 | 828 | if (!virtio_net_can_receive(nc)) { |
cdd5cc12 | 829 | return -1; |
b356f76d | 830 | } |
cdd5cc12 | 831 | |
940cda94 | 832 | /* hdr_len refers to the header we supply to the guest */ |
0c87e93e | 833 | if (!virtio_net_has_buffers(q, size + n->guest_hdr_len - n->host_hdr_len)) { |
8aeff62d | 834 | return 0; |
0c87e93e | 835 | } |
fbe78f4f | 836 | |
3831ab20 | 837 | if (!receive_filter(n, buf, size)) |
4f1c942b | 838 | return size; |
3831ab20 | 839 | |
fbe78f4f AL |
840 | offset = i = 0; |
841 | ||
842 | while (offset < size) { | |
843 | VirtQueueElement elem; | |
844 | int len, total; | |
22cc84db | 845 | const struct iovec *sg = elem.in_sg; |
fbe78f4f | 846 | |
22c253d9 | 847 | total = 0; |
fbe78f4f | 848 | |
0c87e93e | 849 | if (virtqueue_pop(q->rx_vq, &elem) == 0) { |
fbe78f4f | 850 | if (i == 0) |
4f1c942b | 851 | return -1; |
e7b43f7e | 852 | error_report("virtio-net unexpected empty queue: " |
279a4253 | 853 | "i %zd mergeable %d offset %zd, size %zd, " |
e7b43f7e | 854 | "guest hdr len %zd, host hdr len %zd guest features 0x%x", |
279a4253 | 855 | i, n->mergeable_rx_bufs, offset, size, |
17a0ca55 | 856 | n->guest_hdr_len, n->host_hdr_len, vdev->guest_features); |
fbe78f4f AL |
857 | exit(1); |
858 | } | |
859 | ||
860 | if (elem.in_num < 1) { | |
e7b43f7e | 861 | error_report("virtio-net receive queue contains no in buffers"); |
fbe78f4f AL |
862 | exit(1); |
863 | } | |
864 | ||
fbe78f4f | 865 | if (i == 0) { |
c8d28e7e | 866 | assert(offset == 0); |
63c58728 MT |
867 | if (n->mergeable_rx_bufs) { |
868 | mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg), | |
869 | sg, elem.in_num, | |
870 | offsetof(typeof(mhdr), num_buffers), | |
871 | sizeof(mhdr.num_buffers)); | |
872 | } | |
fbe78f4f | 873 | |
c8d28e7e MT |
874 | receive_header(n, sg, elem.in_num, buf, size); |
875 | offset = n->host_hdr_len; | |
e35e23f6 | 876 | total += n->guest_hdr_len; |
22cc84db MT |
877 | guest_offset = n->guest_hdr_len; |
878 | } else { | |
879 | guest_offset = 0; | |
fbe78f4f AL |
880 | } |
881 | ||
882 | /* copy in packet. ugh */ | |
22cc84db | 883 | len = iov_from_buf(sg, elem.in_num, guest_offset, |
dcf6f5e1 | 884 | buf + offset, size - offset); |
fbe78f4f | 885 | total += len; |
279a4253 MT |
886 | offset += len; |
887 | /* If buffers can't be merged, at this point we | |
888 | * must have consumed the complete packet. | |
889 | * Otherwise, drop it. */ | |
890 | if (!n->mergeable_rx_bufs && offset < size) { | |
891 | #if 0 | |
e7b43f7e SH |
892 | error_report("virtio-net truncated non-mergeable packet: " |
893 | "i %zd mergeable %d offset %zd, size %zd, " | |
894 | "guest hdr len %zd, host hdr len %zd", | |
895 | i, n->mergeable_rx_bufs, | |
e35e23f6 | 896 | offset, size, n->guest_hdr_len, n->host_hdr_len); |
279a4253 MT |
897 | #endif |
898 | return size; | |
899 | } | |
fbe78f4f AL |
900 | |
901 | /* signal other side */ | |
0c87e93e | 902 | virtqueue_fill(q->rx_vq, &elem, total, i++); |
fbe78f4f AL |
903 | } |
904 | ||
63c58728 MT |
905 | if (mhdr_cnt) { |
906 | stw_p(&mhdr.num_buffers, i); | |
907 | iov_from_buf(mhdr_sg, mhdr_cnt, | |
908 | 0, | |
909 | &mhdr.num_buffers, sizeof mhdr.num_buffers); | |
44b15bc5 | 910 | } |
fbe78f4f | 911 | |
0c87e93e | 912 | virtqueue_flush(q->rx_vq, i); |
17a0ca55 | 913 | virtio_notify(vdev, q->rx_vq); |
4f1c942b MM |
914 | |
915 | return size; | |
fbe78f4f AL |
916 | } |
917 | ||
0c87e93e | 918 | static int32_t virtio_net_flush_tx(VirtIONetQueue *q); |
6243375f | 919 | |
4e68f7a0 | 920 | static void virtio_net_tx_complete(NetClientState *nc, ssize_t len) |
6243375f | 921 | { |
cc1f0f45 | 922 | VirtIONet *n = qemu_get_nic_opaque(nc); |
fed699f9 | 923 | VirtIONetQueue *q = virtio_net_get_subqueue(nc); |
17a0ca55 | 924 | VirtIODevice *vdev = VIRTIO_DEVICE(n); |
6243375f | 925 | |
0c87e93e | 926 | virtqueue_push(q->tx_vq, &q->async_tx.elem, 0); |
17a0ca55 | 927 | virtio_notify(vdev, q->tx_vq); |
6243375f | 928 | |
0c87e93e | 929 | q->async_tx.elem.out_num = q->async_tx.len = 0; |
6243375f | 930 | |
0c87e93e JW |
931 | virtio_queue_set_notification(q->tx_vq, 1); |
932 | virtio_net_flush_tx(q); | |
6243375f MM |
933 | } |
934 | ||
fbe78f4f | 935 | /* TX */ |
0c87e93e | 936 | static int32_t virtio_net_flush_tx(VirtIONetQueue *q) |
fbe78f4f | 937 | { |
0c87e93e | 938 | VirtIONet *n = q->n; |
17a0ca55 | 939 | VirtIODevice *vdev = VIRTIO_DEVICE(n); |
fbe78f4f | 940 | VirtQueueElement elem; |
e3f30488 | 941 | int32_t num_packets = 0; |
fed699f9 | 942 | int queue_index = vq2q(virtio_get_queue_index(q->tx_vq)); |
17a0ca55 | 943 | if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
e3f30488 AW |
944 | return num_packets; |
945 | } | |
fbe78f4f | 946 | |
17a0ca55 | 947 | assert(vdev->vm_running); |
783e7706 | 948 | |
0c87e93e JW |
949 | if (q->async_tx.elem.out_num) { |
950 | virtio_queue_set_notification(q->tx_vq, 0); | |
e3f30488 | 951 | return num_packets; |
6243375f MM |
952 | } |
953 | ||
0c87e93e | 954 | while (virtqueue_pop(q->tx_vq, &elem)) { |
14761f9c | 955 | ssize_t ret, len; |
fbe78f4f AL |
956 | unsigned int out_num = elem.out_num; |
957 | struct iovec *out_sg = &elem.out_sg[0]; | |
14761f9c | 958 | struct iovec sg[VIRTQUEUE_MAX_SIZE]; |
fbe78f4f | 959 | |
7b80d08e | 960 | if (out_num < 1) { |
e7b43f7e | 961 | error_report("virtio-net header not in first element"); |
fbe78f4f AL |
962 | exit(1); |
963 | } | |
964 | ||
14761f9c MT |
965 | /* |
966 | * If host wants to see the guest header as is, we can | |
967 | * pass it on unchanged. Otherwise, copy just the parts | |
968 | * that host is interested in. | |
969 | */ | |
970 | assert(n->host_hdr_len <= n->guest_hdr_len); | |
971 | if (n->host_hdr_len != n->guest_hdr_len) { | |
972 | unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg), | |
973 | out_sg, out_num, | |
974 | 0, n->host_hdr_len); | |
975 | sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num, | |
976 | out_sg, out_num, | |
977 | n->guest_hdr_len, -1); | |
978 | out_num = sg_num; | |
979 | out_sg = sg; | |
fbe78f4f AL |
980 | } |
981 | ||
7b80d08e | 982 | len = n->guest_hdr_len; |
14761f9c | 983 | |
fed699f9 JW |
984 | ret = qemu_sendv_packet_async(qemu_get_subqueue(n->nic, queue_index), |
985 | out_sg, out_num, virtio_net_tx_complete); | |
6243375f | 986 | if (ret == 0) { |
0c87e93e JW |
987 | virtio_queue_set_notification(q->tx_vq, 0); |
988 | q->async_tx.elem = elem; | |
989 | q->async_tx.len = len; | |
e3f30488 | 990 | return -EBUSY; |
6243375f MM |
991 | } |
992 | ||
993 | len += ret; | |
fbe78f4f | 994 | |
0c87e93e | 995 | virtqueue_push(q->tx_vq, &elem, 0); |
17a0ca55 | 996 | virtio_notify(vdev, q->tx_vq); |
e3f30488 AW |
997 | |
998 | if (++num_packets >= n->tx_burst) { | |
999 | break; | |
1000 | } | |
fbe78f4f | 1001 | } |
e3f30488 | 1002 | return num_packets; |
fbe78f4f AL |
1003 | } |
1004 | ||
a697a334 | 1005 | static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq) |
fbe78f4f | 1006 | { |
17a0ca55 | 1007 | VirtIONet *n = VIRTIO_NET(vdev); |
fed699f9 | 1008 | VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; |
fbe78f4f | 1009 | |
783e7706 | 1010 | /* This happens when device was stopped but VCPU wasn't. */ |
17a0ca55 | 1011 | if (!vdev->vm_running) { |
0c87e93e | 1012 | q->tx_waiting = 1; |
783e7706 MT |
1013 | return; |
1014 | } | |
1015 | ||
0c87e93e | 1016 | if (q->tx_waiting) { |
fbe78f4f | 1017 | virtio_queue_set_notification(vq, 1); |
0c87e93e JW |
1018 | qemu_del_timer(q->tx_timer); |
1019 | q->tx_waiting = 0; | |
1020 | virtio_net_flush_tx(q); | |
fbe78f4f | 1021 | } else { |
0c87e93e | 1022 | qemu_mod_timer(q->tx_timer, |
74475455 | 1023 | qemu_get_clock_ns(vm_clock) + n->tx_timeout); |
0c87e93e | 1024 | q->tx_waiting = 1; |
fbe78f4f AL |
1025 | virtio_queue_set_notification(vq, 0); |
1026 | } | |
1027 | } | |
1028 | ||
a697a334 AW |
1029 | static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq) |
1030 | { | |
17a0ca55 | 1031 | VirtIONet *n = VIRTIO_NET(vdev); |
fed699f9 | 1032 | VirtIONetQueue *q = &n->vqs[vq2q(virtio_get_queue_index(vq))]; |
a697a334 | 1033 | |
0c87e93e | 1034 | if (unlikely(q->tx_waiting)) { |
a697a334 AW |
1035 | return; |
1036 | } | |
0c87e93e | 1037 | q->tx_waiting = 1; |
783e7706 | 1038 | /* This happens when device was stopped but VCPU wasn't. */ |
17a0ca55 | 1039 | if (!vdev->vm_running) { |
783e7706 MT |
1040 | return; |
1041 | } | |
a697a334 | 1042 | virtio_queue_set_notification(vq, 0); |
0c87e93e | 1043 | qemu_bh_schedule(q->tx_bh); |
a697a334 AW |
1044 | } |
1045 | ||
fbe78f4f AL |
1046 | static void virtio_net_tx_timer(void *opaque) |
1047 | { | |
0c87e93e JW |
1048 | VirtIONetQueue *q = opaque; |
1049 | VirtIONet *n = q->n; | |
17a0ca55 FK |
1050 | VirtIODevice *vdev = VIRTIO_DEVICE(n); |
1051 | assert(vdev->vm_running); | |
fbe78f4f | 1052 | |
0c87e93e | 1053 | q->tx_waiting = 0; |
fbe78f4f AL |
1054 | |
1055 | /* Just in case the driver is not ready on more */ | |
17a0ca55 | 1056 | if (!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
fbe78f4f | 1057 | return; |
17a0ca55 | 1058 | } |
fbe78f4f | 1059 | |
0c87e93e JW |
1060 | virtio_queue_set_notification(q->tx_vq, 1); |
1061 | virtio_net_flush_tx(q); | |
fbe78f4f AL |
1062 | } |
1063 | ||
a697a334 AW |
1064 | static void virtio_net_tx_bh(void *opaque) |
1065 | { | |
0c87e93e JW |
1066 | VirtIONetQueue *q = opaque; |
1067 | VirtIONet *n = q->n; | |
17a0ca55 | 1068 | VirtIODevice *vdev = VIRTIO_DEVICE(n); |
a697a334 AW |
1069 | int32_t ret; |
1070 | ||
17a0ca55 | 1071 | assert(vdev->vm_running); |
783e7706 | 1072 | |
0c87e93e | 1073 | q->tx_waiting = 0; |
a697a334 AW |
1074 | |
1075 | /* Just in case the driver is not ready on more */ | |
17a0ca55 | 1076 | if (unlikely(!(vdev->status & VIRTIO_CONFIG_S_DRIVER_OK))) { |
a697a334 | 1077 | return; |
17a0ca55 | 1078 | } |
a697a334 | 1079 | |
0c87e93e | 1080 | ret = virtio_net_flush_tx(q); |
a697a334 AW |
1081 | if (ret == -EBUSY) { |
1082 | return; /* Notification re-enable handled by tx_complete */ | |
1083 | } | |
1084 | ||
1085 | /* If we flush a full burst of packets, assume there are | |
1086 | * more coming and immediately reschedule */ | |
1087 | if (ret >= n->tx_burst) { | |
0c87e93e JW |
1088 | qemu_bh_schedule(q->tx_bh); |
1089 | q->tx_waiting = 1; | |
a697a334 AW |
1090 | return; |
1091 | } | |
1092 | ||
1093 | /* If less than a full burst, re-enable notification and flush | |
1094 | * anything that may have come in while we weren't looking. If | |
1095 | * we find something, assume the guest is still active and reschedule */ | |
0c87e93e JW |
1096 | virtio_queue_set_notification(q->tx_vq, 1); |
1097 | if (virtio_net_flush_tx(q) > 0) { | |
1098 | virtio_queue_set_notification(q->tx_vq, 0); | |
1099 | qemu_bh_schedule(q->tx_bh); | |
1100 | q->tx_waiting = 1; | |
a697a334 AW |
1101 | } |
1102 | } | |
1103 | ||
ec57db16 | 1104 | static void virtio_net_set_multiqueue(VirtIONet *n, int multiqueue) |
fed699f9 | 1105 | { |
17a0ca55 | 1106 | VirtIODevice *vdev = VIRTIO_DEVICE(n); |
fed699f9 JW |
1107 | int i, max = multiqueue ? n->max_queues : 1; |
1108 | ||
1109 | n->multiqueue = multiqueue; | |
1110 | ||
1111 | for (i = 2; i <= n->max_queues * 2 + 1; i++) { | |
1112 | virtio_del_queue(vdev, i); | |
1113 | } | |
1114 | ||
1115 | for (i = 1; i < max; i++) { | |
1116 | n->vqs[i].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx); | |
1117 | if (n->vqs[i].tx_timer) { | |
1118 | n->vqs[i].tx_vq = | |
1119 | virtio_add_queue(vdev, 256, virtio_net_handle_tx_timer); | |
1120 | n->vqs[i].tx_timer = qemu_new_timer_ns(vm_clock, | |
1121 | virtio_net_tx_timer, | |
1122 | &n->vqs[i]); | |
1123 | } else { | |
1124 | n->vqs[i].tx_vq = | |
1125 | virtio_add_queue(vdev, 256, virtio_net_handle_tx_bh); | |
1126 | n->vqs[i].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[i]); | |
1127 | } | |
1128 | ||
1129 | n->vqs[i].tx_waiting = 0; | |
1130 | n->vqs[i].n = n; | |
1131 | } | |
1132 | ||
ec57db16 JW |
1133 | /* Note: Minux Guests (version 3.2.1) use ctrl vq but don't ack |
1134 | * VIRTIO_NET_F_CTRL_VQ. Create ctrl vq unconditionally to avoid | |
1135 | * breaking them. | |
1136 | */ | |
1137 | n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); | |
fed699f9 JW |
1138 | |
1139 | virtio_net_set_queues(n); | |
1140 | } | |
1141 | ||
fbe78f4f AL |
1142 | static void virtio_net_save(QEMUFile *f, void *opaque) |
1143 | { | |
5f800801 | 1144 | int i; |
fbe78f4f | 1145 | VirtIONet *n = opaque; |
17a0ca55 | 1146 | VirtIODevice *vdev = VIRTIO_DEVICE(n); |
fbe78f4f | 1147 | |
afbaa7b4 MT |
1148 | /* At this point, backend must be stopped, otherwise |
1149 | * it might keep writing to memory. */ | |
1150 | assert(!n->vhost_started); | |
17a0ca55 | 1151 | virtio_save(vdev, f); |
fbe78f4f | 1152 | |
79674068 | 1153 | qemu_put_buffer(f, n->mac, ETH_ALEN); |
5f800801 | 1154 | qemu_put_be32(f, n->vqs[0].tx_waiting); |
e46cb38f | 1155 | qemu_put_be32(f, n->mergeable_rx_bufs); |
9d6271b8 | 1156 | qemu_put_be16(f, n->status); |
f10c592e AW |
1157 | qemu_put_byte(f, n->promisc); |
1158 | qemu_put_byte(f, n->allmulti); | |
b6503ed9 AL |
1159 | qemu_put_be32(f, n->mac_table.in_use); |
1160 | qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN); | |
f21c0ed9 | 1161 | qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); |
3a330134 | 1162 | qemu_put_be32(f, n->has_vnet_hdr); |
8fd2a2f1 AW |
1163 | qemu_put_byte(f, n->mac_table.multi_overflow); |
1164 | qemu_put_byte(f, n->mac_table.uni_overflow); | |
015cb166 AW |
1165 | qemu_put_byte(f, n->alluni); |
1166 | qemu_put_byte(f, n->nomulti); | |
1167 | qemu_put_byte(f, n->nouni); | |
1168 | qemu_put_byte(f, n->nobcast); | |
0ce0e8f4 | 1169 | qemu_put_byte(f, n->has_ufo); |
5f800801 JW |
1170 | if (n->max_queues > 1) { |
1171 | qemu_put_be16(f, n->max_queues); | |
1172 | qemu_put_be16(f, n->curr_queues); | |
1173 | for (i = 1; i < n->curr_queues; i++) { | |
1174 | qemu_put_be32(f, n->vqs[i].tx_waiting); | |
1175 | } | |
1176 | } | |
644c9858 DF |
1177 | |
1178 | if ((1 << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) & vdev->guest_features) { | |
1179 | qemu_put_be64(f, n->curr_guest_offloads); | |
1180 | } | |
fbe78f4f AL |
1181 | } |
1182 | ||
1183 | static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) | |
1184 | { | |
1185 | VirtIONet *n = opaque; | |
17a0ca55 | 1186 | VirtIODevice *vdev = VIRTIO_DEVICE(n); |
5f800801 | 1187 | int ret, i, link_down; |
fbe78f4f | 1188 | |
9d6271b8 | 1189 | if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION) |
fbe78f4f AL |
1190 | return -EINVAL; |
1191 | ||
17a0ca55 | 1192 | ret = virtio_load(vdev, f); |
2a633c46 OW |
1193 | if (ret) { |
1194 | return ret; | |
1195 | } | |
fbe78f4f | 1196 | |
79674068 | 1197 | qemu_get_buffer(f, n->mac, ETH_ALEN); |
5f800801 | 1198 | n->vqs[0].tx_waiting = qemu_get_be32(f); |
ff3a8066 MT |
1199 | |
1200 | virtio_net_set_mrg_rx_bufs(n, qemu_get_be32(f)); | |
fbe78f4f | 1201 | |
9d6271b8 AL |
1202 | if (version_id >= 3) |
1203 | n->status = qemu_get_be16(f); | |
1204 | ||
002437cd | 1205 | if (version_id >= 4) { |
f10c592e AW |
1206 | if (version_id < 8) { |
1207 | n->promisc = qemu_get_be32(f); | |
1208 | n->allmulti = qemu_get_be32(f); | |
1209 | } else { | |
1210 | n->promisc = qemu_get_byte(f); | |
1211 | n->allmulti = qemu_get_byte(f); | |
1212 | } | |
002437cd AL |
1213 | } |
1214 | ||
b6503ed9 AL |
1215 | if (version_id >= 5) { |
1216 | n->mac_table.in_use = qemu_get_be32(f); | |
1217 | /* MAC_TABLE_ENTRIES may be different from the saved image */ | |
1218 | if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) { | |
1219 | qemu_get_buffer(f, n->mac_table.macs, | |
1220 | n->mac_table.in_use * ETH_ALEN); | |
1221 | } else if (n->mac_table.in_use) { | |
e398d61b JQ |
1222 | uint8_t *buf = g_malloc0(n->mac_table.in_use); |
1223 | qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN); | |
1224 | g_free(buf); | |
8fd2a2f1 | 1225 | n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1; |
b6503ed9 AL |
1226 | n->mac_table.in_use = 0; |
1227 | } | |
1228 | } | |
1229 | ||
f21c0ed9 AL |
1230 | if (version_id >= 6) |
1231 | qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); | |
1232 | ||
3a330134 MM |
1233 | if (version_id >= 7) { |
1234 | if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) { | |
1ecda02b | 1235 | error_report("virtio-net: saved image requires vnet_hdr=on"); |
3a330134 MM |
1236 | return -1; |
1237 | } | |
6c042c16 AW |
1238 | } |
1239 | ||
8fd2a2f1 AW |
1240 | if (version_id >= 9) { |
1241 | n->mac_table.multi_overflow = qemu_get_byte(f); | |
1242 | n->mac_table.uni_overflow = qemu_get_byte(f); | |
1243 | } | |
1244 | ||
015cb166 AW |
1245 | if (version_id >= 10) { |
1246 | n->alluni = qemu_get_byte(f); | |
1247 | n->nomulti = qemu_get_byte(f); | |
1248 | n->nouni = qemu_get_byte(f); | |
1249 | n->nobcast = qemu_get_byte(f); | |
1250 | } | |
1251 | ||
0ce0e8f4 MM |
1252 | if (version_id >= 11) { |
1253 | if (qemu_get_byte(f) && !peer_has_ufo(n)) { | |
1ecda02b | 1254 | error_report("virtio-net: saved image requires TUN_F_UFO support"); |
0ce0e8f4 MM |
1255 | return -1; |
1256 | } | |
1257 | } | |
1258 | ||
5f800801 JW |
1259 | if (n->max_queues > 1) { |
1260 | if (n->max_queues != qemu_get_be16(f)) { | |
1261 | error_report("virtio-net: different max_queues "); | |
1262 | return -1; | |
1263 | } | |
1264 | ||
1265 | n->curr_queues = qemu_get_be16(f); | |
1266 | for (i = 1; i < n->curr_queues; i++) { | |
1267 | n->vqs[i].tx_waiting = qemu_get_be32(f); | |
1268 | } | |
1269 | } | |
1270 | ||
644c9858 DF |
1271 | if ((1 << VIRTIO_NET_F_CTRL_GUEST_OFFLOADS) & vdev->guest_features) { |
1272 | n->curr_guest_offloads = qemu_get_be64(f); | |
1273 | } else { | |
1274 | n->curr_guest_offloads = virtio_net_supported_guest_offloads(n); | |
1275 | } | |
1276 | ||
1277 | if (peer_has_vnet_hdr(n)) { | |
1278 | virtio_net_apply_guest_offloads(n); | |
1279 | } | |
1280 | ||
5f800801 JW |
1281 | virtio_net_set_queues(n); |
1282 | ||
2d9aba39 AW |
1283 | /* Find the first multicast entry in the saved MAC filter */ |
1284 | for (i = 0; i < n->mac_table.in_use; i++) { | |
1285 | if (n->mac_table.macs[i * ETH_ALEN] & 1) { | |
1286 | break; | |
1287 | } | |
1288 | } | |
1289 | n->mac_table.first_multi = i; | |
98991481 AK |
1290 | |
1291 | /* nc.link_down can't be migrated, so infer link_down according | |
1292 | * to link status bit in n->status */ | |
5f800801 JW |
1293 | link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0; |
1294 | for (i = 0; i < n->max_queues; i++) { | |
1295 | qemu_get_subqueue(n->nic, i)->link_down = link_down; | |
1296 | } | |
98991481 | 1297 | |
fbe78f4f AL |
1298 | return 0; |
1299 | } | |
1300 | ||
4e68f7a0 | 1301 | static void virtio_net_cleanup(NetClientState *nc) |
b946a153 | 1302 | { |
cc1f0f45 | 1303 | VirtIONet *n = qemu_get_nic_opaque(nc); |
b946a153 | 1304 | |
eb6b6c12 | 1305 | n->nic = NULL; |
b946a153 AL |
1306 | } |
1307 | ||
eb6b6c12 | 1308 | static NetClientInfo net_virtio_info = { |
2be64a68 | 1309 | .type = NET_CLIENT_OPTIONS_KIND_NIC, |
eb6b6c12 MM |
1310 | .size = sizeof(NICState), |
1311 | .can_receive = virtio_net_can_receive, | |
1312 | .receive = virtio_net_receive, | |
1313 | .cleanup = virtio_net_cleanup, | |
1314 | .link_status_changed = virtio_net_set_link_status, | |
1315 | }; | |
1316 | ||
f56a1247 MT |
1317 | static bool virtio_net_guest_notifier_pending(VirtIODevice *vdev, int idx) |
1318 | { | |
17a0ca55 | 1319 | VirtIONet *n = VIRTIO_NET(vdev); |
fed699f9 | 1320 | NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); |
f56a1247 | 1321 | assert(n->vhost_started); |
b356f76d | 1322 | return vhost_net_virtqueue_pending(tap_get_vhost_net(nc->peer), idx); |
f56a1247 MT |
1323 | } |
1324 | ||
1325 | static void virtio_net_guest_notifier_mask(VirtIODevice *vdev, int idx, | |
1326 | bool mask) | |
1327 | { | |
17a0ca55 | 1328 | VirtIONet *n = VIRTIO_NET(vdev); |
fed699f9 | 1329 | NetClientState *nc = qemu_get_subqueue(n->nic, vq2q(idx)); |
f56a1247 | 1330 | assert(n->vhost_started); |
b356f76d | 1331 | vhost_net_virtqueue_mask(tap_get_vhost_net(nc->peer), |
f56a1247 MT |
1332 | vdev, idx, mask); |
1333 | } | |
1334 | ||
17ec5a86 | 1335 | void virtio_net_set_config_size(VirtIONet *n, uint32_t host_features) |
fbe78f4f | 1336 | { |
14f9b664 | 1337 | int i, config_size = 0; |
e9016ee2 | 1338 | host_features |= (1 << VIRTIO_NET_F_MAC); |
14f9b664 JL |
1339 | for (i = 0; feature_sizes[i].flags != 0; i++) { |
1340 | if (host_features & feature_sizes[i].flags) { | |
1341 | config_size = MAX(feature_sizes[i].end, config_size); | |
1342 | } | |
1343 | } | |
17ec5a86 FK |
1344 | n->config_size = config_size; |
1345 | } | |
1346 | ||
8a253ec2 FK |
1347 | void virtio_net_set_netclient_name(VirtIONet *n, const char *name, |
1348 | const char *type) | |
1349 | { | |
1350 | /* | |
1351 | * The name can be NULL, the netclient name will be type.x. | |
1352 | */ | |
1353 | assert(type != NULL); | |
1354 | ||
1355 | if (n->netclient_name) { | |
1356 | g_free(n->netclient_name); | |
1357 | n->netclient_name = NULL; | |
1358 | } | |
1359 | if (n->netclient_type) { | |
1360 | g_free(n->netclient_type); | |
1361 | n->netclient_type = NULL; | |
1362 | } | |
1363 | ||
1364 | if (name != NULL) { | |
1365 | n->netclient_name = g_strdup(name); | |
1366 | } | |
1367 | n->netclient_type = g_strdup(type); | |
1368 | } | |
1369 | ||
1773d9ee | 1370 | static int virtio_net_device_init(VirtIODevice *vdev) |
17ec5a86 | 1371 | { |
1773d9ee | 1372 | int i; |
fbe78f4f | 1373 | |
1773d9ee FK |
1374 | DeviceState *qdev = DEVICE(vdev); |
1375 | VirtIONet *n = VIRTIO_NET(vdev); | |
1376 | ||
1377 | virtio_init(VIRTIO_DEVICE(n), "virtio-net", VIRTIO_ID_NET, | |
1378 | n->config_size); | |
fbe78f4f | 1379 | |
1773d9ee | 1380 | n->max_queues = MAX(n->nic_conf.queues, 1); |
f6b26cf2 | 1381 | n->vqs = g_malloc0(sizeof(VirtIONetQueue) * n->max_queues); |
17a0ca55 | 1382 | n->vqs[0].rx_vq = virtio_add_queue(vdev, 256, virtio_net_handle_rx); |
fed699f9 JW |
1383 | n->curr_queues = 1; |
1384 | n->vqs[0].n = n; | |
1773d9ee | 1385 | n->tx_timeout = n->net_conf.txtimer; |
a697a334 | 1386 | |
1773d9ee FK |
1387 | if (n->net_conf.tx && strcmp(n->net_conf.tx, "timer") |
1388 | && strcmp(n->net_conf.tx, "bh")) { | |
e7b43f7e SH |
1389 | error_report("virtio-net: " |
1390 | "Unknown option tx=%s, valid options: \"timer\" \"bh\"", | |
1773d9ee | 1391 | n->net_conf.tx); |
e7b43f7e | 1392 | error_report("Defaulting to \"bh\""); |
a697a334 AW |
1393 | } |
1394 | ||
1773d9ee | 1395 | if (n->net_conf.tx && !strcmp(n->net_conf.tx, "timer")) { |
17a0ca55 | 1396 | n->vqs[0].tx_vq = virtio_add_queue(vdev, 256, |
fed699f9 JW |
1397 | virtio_net_handle_tx_timer); |
1398 | n->vqs[0].tx_timer = qemu_new_timer_ns(vm_clock, virtio_net_tx_timer, | |
1399 | &n->vqs[0]); | |
a697a334 | 1400 | } else { |
17a0ca55 | 1401 | n->vqs[0].tx_vq = virtio_add_queue(vdev, 256, |
fed699f9 JW |
1402 | virtio_net_handle_tx_bh); |
1403 | n->vqs[0].tx_bh = qemu_bh_new(virtio_net_tx_bh, &n->vqs[0]); | |
a697a334 | 1404 | } |
17a0ca55 | 1405 | n->ctrl_vq = virtio_add_queue(vdev, 64, virtio_net_handle_ctrl); |
1773d9ee FK |
1406 | qemu_macaddr_default_if_unset(&n->nic_conf.macaddr); |
1407 | memcpy(&n->mac[0], &n->nic_conf.macaddr, sizeof(n->mac)); | |
554c97dd | 1408 | n->status = VIRTIO_NET_S_LINK_UP; |
fbe78f4f | 1409 | |
8a253ec2 FK |
1410 | if (n->netclient_type) { |
1411 | /* | |
1412 | * Happen when virtio_net_set_netclient_name has been called. | |
1413 | */ | |
1414 | n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, | |
1415 | n->netclient_type, n->netclient_name, n); | |
1416 | } else { | |
1417 | n->nic = qemu_new_nic(&net_virtio_info, &n->nic_conf, | |
1418 | object_get_typename(OBJECT(qdev)), qdev->id, n); | |
1419 | } | |
1420 | ||
6e371ab8 MT |
1421 | peer_test_vnet_hdr(n); |
1422 | if (peer_has_vnet_hdr(n)) { | |
fed699f9 JW |
1423 | for (i = 0; i < n->max_queues; i++) { |
1424 | tap_using_vnet_hdr(qemu_get_subqueue(n->nic, i)->peer, true); | |
1425 | } | |
6e371ab8 MT |
1426 | n->host_hdr_len = sizeof(struct virtio_net_hdr); |
1427 | } else { | |
1428 | n->host_hdr_len = 0; | |
1429 | } | |
eb6b6c12 | 1430 | |
1773d9ee | 1431 | qemu_format_nic_info_str(qemu_get_queue(n->nic), n->nic_conf.macaddr.a); |
96d5e201 | 1432 | |
fed699f9 | 1433 | n->vqs[0].tx_waiting = 0; |
1773d9ee | 1434 | n->tx_burst = n->net_conf.txburst; |
ff3a8066 | 1435 | virtio_net_set_mrg_rx_bufs(n, 0); |
002437cd | 1436 | n->promisc = 1; /* for compatibility */ |
fbe78f4f | 1437 | |
7267c094 | 1438 | n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN); |
b6503ed9 | 1439 | |
7267c094 | 1440 | n->vlans = g_malloc0(MAX_VLAN >> 3); |
f21c0ed9 | 1441 | |
1773d9ee FK |
1442 | n->qdev = qdev; |
1443 | register_savevm(qdev, "virtio-net", -1, VIRTIO_NET_VM_VERSION, | |
fbe78f4f | 1444 | virtio_net_save, virtio_net_load, n); |
cf21e106 | 1445 | |
1773d9ee | 1446 | add_boot_device_path(n->nic_conf.bootindex, qdev, "/ethernet-phy@0"); |
17ec5a86 FK |
1447 | return 0; |
1448 | } | |
1449 | ||
1450 | static int virtio_net_device_exit(DeviceState *qdev) | |
1451 | { | |
1452 | VirtIONet *n = VIRTIO_NET(qdev); | |
1453 | VirtIODevice *vdev = VIRTIO_DEVICE(qdev); | |
1454 | int i; | |
1455 | ||
1456 | /* This will stop vhost backend if appropriate. */ | |
1457 | virtio_net_set_status(vdev, 0); | |
1458 | ||
1459 | unregister_savevm(qdev, "virtio-net", n); | |
1460 | ||
8a253ec2 FK |
1461 | if (n->netclient_name) { |
1462 | g_free(n->netclient_name); | |
1463 | n->netclient_name = NULL; | |
1464 | } | |
1465 | if (n->netclient_type) { | |
1466 | g_free(n->netclient_type); | |
1467 | n->netclient_type = NULL; | |
1468 | } | |
1469 | ||
17ec5a86 FK |
1470 | g_free(n->mac_table.macs); |
1471 | g_free(n->vlans); | |
1472 | ||
1473 | for (i = 0; i < n->max_queues; i++) { | |
1474 | VirtIONetQueue *q = &n->vqs[i]; | |
1475 | NetClientState *nc = qemu_get_subqueue(n->nic, i); | |
1476 | ||
1477 | qemu_purge_queued_packets(nc); | |
1478 | ||
1479 | if (q->tx_timer) { | |
1480 | qemu_del_timer(q->tx_timer); | |
1481 | qemu_free_timer(q->tx_timer); | |
1482 | } else { | |
1483 | qemu_bh_delete(q->tx_bh); | |
1484 | } | |
1485 | } | |
1486 | ||
1487 | g_free(n->vqs); | |
1488 | qemu_del_nic(n->nic); | |
6a1a8cc7 | 1489 | virtio_cleanup(vdev); |
17ec5a86 FK |
1490 | |
1491 | return 0; | |
1492 | } | |
1493 | ||
1494 | static void virtio_net_instance_init(Object *obj) | |
1495 | { | |
1496 | VirtIONet *n = VIRTIO_NET(obj); | |
1497 | ||
1498 | /* | |
1499 | * The default config_size is sizeof(struct virtio_net_config). | |
1500 | * Can be overriden with virtio_net_set_config_size. | |
1501 | */ | |
1502 | n->config_size = sizeof(struct virtio_net_config); | |
1503 | } | |
1504 | ||
1505 | static Property virtio_net_properties[] = { | |
1506 | DEFINE_NIC_PROPERTIES(VirtIONet, nic_conf), | |
1507 | DEFINE_PROP_UINT32("x-txtimer", VirtIONet, net_conf.txtimer, | |
1508 | TX_TIMER_INTERVAL), | |
1509 | DEFINE_PROP_INT32("x-txburst", VirtIONet, net_conf.txburst, TX_BURST), | |
1510 | DEFINE_PROP_STRING("tx", VirtIONet, net_conf.tx), | |
1511 | DEFINE_PROP_END_OF_LIST(), | |
1512 | }; | |
1513 | ||
1514 | static void virtio_net_class_init(ObjectClass *klass, void *data) | |
1515 | { | |
1516 | DeviceClass *dc = DEVICE_CLASS(klass); | |
1517 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | |
1518 | dc->exit = virtio_net_device_exit; | |
1519 | dc->props = virtio_net_properties; | |
1520 | vdc->init = virtio_net_device_init; | |
1521 | vdc->get_config = virtio_net_get_config; | |
1522 | vdc->set_config = virtio_net_set_config; | |
1523 | vdc->get_features = virtio_net_get_features; | |
1524 | vdc->set_features = virtio_net_set_features; | |
1525 | vdc->bad_features = virtio_net_bad_features; | |
1526 | vdc->reset = virtio_net_reset; | |
1527 | vdc->set_status = virtio_net_set_status; | |
1528 | vdc->guest_notifier_mask = virtio_net_guest_notifier_mask; | |
1529 | vdc->guest_notifier_pending = virtio_net_guest_notifier_pending; | |
1530 | } | |
1531 | ||
1532 | static const TypeInfo virtio_net_info = { | |
1533 | .name = TYPE_VIRTIO_NET, | |
1534 | .parent = TYPE_VIRTIO_DEVICE, | |
1535 | .instance_size = sizeof(VirtIONet), | |
1536 | .instance_init = virtio_net_instance_init, | |
1537 | .class_init = virtio_net_class_init, | |
1538 | }; | |
1539 | ||
1540 | static void virtio_register_types(void) | |
1541 | { | |
1542 | type_register_static(&virtio_net_info); | |
1543 | } | |
1544 | ||
1545 | type_init(virtio_register_types) |