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