]>
Commit | Line | Data |
---|---|---|
fbe78f4f AL |
1 | /* |
2 | * Virtio Network Device | |
3 | * | |
4 | * Copyright IBM, Corp. 2007 | |
5 | * | |
6 | * Authors: | |
7 | * Anthony Liguori <aliguori@us.ibm.com> | |
8 | * | |
9 | * This work is licensed under the terms of the GNU GPL, version 2. See | |
10 | * the COPYING file in the top-level directory. | |
11 | * | |
12 | */ | |
13 | ||
14 | #include "virtio.h" | |
15 | #include "net.h" | |
16 | #include "qemu-timer.h" | |
17 | #include "virtio-net.h" | |
18 | ||
015cb166 | 19 | #define VIRTIO_NET_VM_VERSION 10 |
b6503ed9 | 20 | |
4ffb17f5 | 21 | #define MAC_TABLE_ENTRIES 64 |
f21c0ed9 | 22 | #define MAX_VLAN (1 << 12) /* Per 802.1Q definition */ |
9d6271b8 | 23 | |
fbe78f4f AL |
24 | typedef struct VirtIONet |
25 | { | |
26 | VirtIODevice vdev; | |
79674068 | 27 | uint8_t mac[ETH_ALEN]; |
554c97dd | 28 | uint16_t status; |
fbe78f4f AL |
29 | VirtQueue *rx_vq; |
30 | VirtQueue *tx_vq; | |
3d11d36c | 31 | VirtQueue *ctrl_vq; |
fbe78f4f AL |
32 | VLANClientState *vc; |
33 | QEMUTimer *tx_timer; | |
34 | int tx_timer_active; | |
35 | int mergeable_rx_bufs; | |
f10c592e AW |
36 | uint8_t promisc; |
37 | uint8_t allmulti; | |
015cb166 AW |
38 | uint8_t alluni; |
39 | uint8_t nomulti; | |
40 | uint8_t nouni; | |
41 | uint8_t nobcast; | |
b6503ed9 AL |
42 | struct { |
43 | int in_use; | |
2d9aba39 | 44 | int first_multi; |
8fd2a2f1 AW |
45 | uint8_t multi_overflow; |
46 | uint8_t uni_overflow; | |
b6503ed9 AL |
47 | uint8_t *macs; |
48 | } mac_table; | |
f21c0ed9 | 49 | uint32_t *vlans; |
fbe78f4f AL |
50 | } VirtIONet; |
51 | ||
52 | /* TODO | |
53 | * - we could suppress RX interrupt if we were so inclined. | |
54 | */ | |
55 | ||
56 | static VirtIONet *to_virtio_net(VirtIODevice *vdev) | |
57 | { | |
58 | return (VirtIONet *)vdev; | |
59 | } | |
60 | ||
0f03eca6 | 61 | static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config) |
fbe78f4f AL |
62 | { |
63 | VirtIONet *n = to_virtio_net(vdev); | |
64 | struct virtio_net_config netcfg; | |
65 | ||
554c97dd | 66 | netcfg.status = n->status; |
79674068 | 67 | memcpy(netcfg.mac, n->mac, ETH_ALEN); |
fbe78f4f AL |
68 | memcpy(config, &netcfg, sizeof(netcfg)); |
69 | } | |
70 | ||
0f03eca6 AL |
71 | static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config) |
72 | { | |
73 | VirtIONet *n = to_virtio_net(vdev); | |
74 | struct virtio_net_config netcfg; | |
75 | ||
76 | memcpy(&netcfg, config, sizeof(netcfg)); | |
77 | ||
79674068 AL |
78 | if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) { |
79 | memcpy(n->mac, netcfg.mac, ETH_ALEN); | |
0f03eca6 AL |
80 | qemu_format_nic_info_str(n->vc, n->mac); |
81 | } | |
82 | } | |
83 | ||
554c97dd AL |
84 | static void virtio_net_set_link_status(VLANClientState *vc) |
85 | { | |
86 | VirtIONet *n = vc->opaque; | |
87 | uint16_t old_status = n->status; | |
88 | ||
89 | if (vc->link_down) | |
90 | n->status &= ~VIRTIO_NET_S_LINK_UP; | |
91 | else | |
92 | n->status |= VIRTIO_NET_S_LINK_UP; | |
93 | ||
94 | if (n->status != old_status) | |
95 | virtio_notify_config(&n->vdev); | |
96 | } | |
97 | ||
002437cd AL |
98 | static void virtio_net_reset(VirtIODevice *vdev) |
99 | { | |
100 | VirtIONet *n = to_virtio_net(vdev); | |
101 | ||
102 | /* Reset back to compatibility mode */ | |
103 | n->promisc = 1; | |
104 | n->allmulti = 0; | |
015cb166 AW |
105 | n->alluni = 0; |
106 | n->nomulti = 0; | |
107 | n->nouni = 0; | |
108 | n->nobcast = 0; | |
b6503ed9 | 109 | |
f21c0ed9 | 110 | /* Flush any MAC and VLAN filter table state */ |
b6503ed9 | 111 | n->mac_table.in_use = 0; |
2d9aba39 | 112 | n->mac_table.first_multi = 0; |
8fd2a2f1 AW |
113 | n->mac_table.multi_overflow = 0; |
114 | n->mac_table.uni_overflow = 0; | |
b6503ed9 | 115 | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); |
f21c0ed9 | 116 | memset(n->vlans, 0, MAX_VLAN >> 3); |
002437cd AL |
117 | } |
118 | ||
fbe78f4f AL |
119 | static uint32_t virtio_net_get_features(VirtIODevice *vdev) |
120 | { | |
3d11d36c | 121 | uint32_t features = (1 << VIRTIO_NET_F_MAC) | |
e16044ef | 122 | (1 << VIRTIO_NET_F_MRG_RXBUF) | |
3d11d36c | 123 | (1 << VIRTIO_NET_F_STATUS) | |
b6503ed9 | 124 | (1 << VIRTIO_NET_F_CTRL_VQ) | |
f21c0ed9 | 125 | (1 << VIRTIO_NET_F_CTRL_RX) | |
015cb166 AW |
126 | (1 << VIRTIO_NET_F_CTRL_VLAN) | |
127 | (1 << VIRTIO_NET_F_CTRL_RX_EXTRA); | |
fbe78f4f AL |
128 | |
129 | return features; | |
130 | } | |
131 | ||
8eca6b1b AL |
132 | static uint32_t virtio_net_bad_features(VirtIODevice *vdev) |
133 | { | |
134 | uint32_t features = 0; | |
135 | ||
136 | /* Linux kernel 2.6.25. It understood MAC (as everyone must), | |
137 | * but also these: */ | |
138 | features |= (1 << VIRTIO_NET_F_MAC); | |
139 | features |= (1 << VIRTIO_NET_F_GUEST_CSUM); | |
140 | features |= (1 << VIRTIO_NET_F_GUEST_TSO4); | |
141 | features |= (1 << VIRTIO_NET_F_GUEST_TSO6); | |
142 | features |= (1 << VIRTIO_NET_F_GUEST_ECN); | |
143 | ||
144 | return features & virtio_net_get_features(vdev); | |
145 | } | |
146 | ||
fbe78f4f AL |
147 | static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features) |
148 | { | |
149 | VirtIONet *n = to_virtio_net(vdev); | |
150 | ||
151 | n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF)); | |
152 | } | |
153 | ||
002437cd AL |
154 | static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd, |
155 | VirtQueueElement *elem) | |
156 | { | |
157 | uint8_t on; | |
158 | ||
159 | if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) { | |
160 | fprintf(stderr, "virtio-net ctrl invalid rx mode command\n"); | |
161 | exit(1); | |
162 | } | |
163 | ||
164 | on = ldub_p(elem->out_sg[1].iov_base); | |
165 | ||
166 | if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC) | |
167 | n->promisc = on; | |
168 | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI) | |
169 | n->allmulti = on; | |
015cb166 AW |
170 | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI) |
171 | n->alluni = on; | |
172 | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI) | |
173 | n->nomulti = on; | |
174 | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI) | |
175 | n->nouni = on; | |
176 | else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST) | |
177 | n->nobcast = on; | |
002437cd AL |
178 | else |
179 | return VIRTIO_NET_ERR; | |
180 | ||
181 | return VIRTIO_NET_OK; | |
182 | } | |
183 | ||
b6503ed9 AL |
184 | static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd, |
185 | VirtQueueElement *elem) | |
186 | { | |
187 | struct virtio_net_ctrl_mac mac_data; | |
188 | ||
189 | if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 || | |
190 | elem->out_sg[1].iov_len < sizeof(mac_data) || | |
191 | elem->out_sg[2].iov_len < sizeof(mac_data)) | |
192 | return VIRTIO_NET_ERR; | |
193 | ||
194 | n->mac_table.in_use = 0; | |
2d9aba39 | 195 | n->mac_table.first_multi = 0; |
8fd2a2f1 AW |
196 | n->mac_table.uni_overflow = 0; |
197 | n->mac_table.multi_overflow = 0; | |
b6503ed9 AL |
198 | memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN); |
199 | ||
200 | mac_data.entries = ldl_le_p(elem->out_sg[1].iov_base); | |
201 | ||
202 | if (sizeof(mac_data.entries) + | |
203 | (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len) | |
204 | return VIRTIO_NET_ERR; | |
205 | ||
206 | if (mac_data.entries <= MAC_TABLE_ENTRIES) { | |
207 | memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data), | |
208 | mac_data.entries * ETH_ALEN); | |
209 | n->mac_table.in_use += mac_data.entries; | |
210 | } else { | |
8fd2a2f1 | 211 | n->mac_table.uni_overflow = 1; |
b6503ed9 AL |
212 | } |
213 | ||
2d9aba39 AW |
214 | n->mac_table.first_multi = n->mac_table.in_use; |
215 | ||
b6503ed9 AL |
216 | mac_data.entries = ldl_le_p(elem->out_sg[2].iov_base); |
217 | ||
218 | if (sizeof(mac_data.entries) + | |
219 | (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len) | |
220 | return VIRTIO_NET_ERR; | |
221 | ||
222 | if (mac_data.entries) { | |
223 | if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) { | |
224 | memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN), | |
225 | elem->out_sg[2].iov_base + sizeof(mac_data), | |
226 | mac_data.entries * ETH_ALEN); | |
227 | n->mac_table.in_use += mac_data.entries; | |
8fd2a2f1 AW |
228 | } else { |
229 | n->mac_table.multi_overflow = 1; | |
230 | } | |
b6503ed9 AL |
231 | } |
232 | ||
233 | return VIRTIO_NET_OK; | |
234 | } | |
235 | ||
f21c0ed9 AL |
236 | static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd, |
237 | VirtQueueElement *elem) | |
238 | { | |
239 | uint16_t vid; | |
240 | ||
241 | if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) { | |
242 | fprintf(stderr, "virtio-net ctrl invalid vlan command\n"); | |
243 | return VIRTIO_NET_ERR; | |
244 | } | |
245 | ||
246 | vid = lduw_le_p(elem->out_sg[1].iov_base); | |
247 | ||
248 | if (vid >= MAX_VLAN) | |
249 | return VIRTIO_NET_ERR; | |
250 | ||
251 | if (cmd == VIRTIO_NET_CTRL_VLAN_ADD) | |
252 | n->vlans[vid >> 5] |= (1U << (vid & 0x1f)); | |
253 | else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL) | |
254 | n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f)); | |
255 | else | |
256 | return VIRTIO_NET_ERR; | |
257 | ||
258 | return VIRTIO_NET_OK; | |
259 | } | |
260 | ||
3d11d36c AL |
261 | static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq) |
262 | { | |
002437cd | 263 | VirtIONet *n = to_virtio_net(vdev); |
3d11d36c AL |
264 | struct virtio_net_ctrl_hdr ctrl; |
265 | virtio_net_ctrl_ack status = VIRTIO_NET_ERR; | |
266 | VirtQueueElement elem; | |
267 | ||
268 | while (virtqueue_pop(vq, &elem)) { | |
269 | if ((elem.in_num < 1) || (elem.out_num < 1)) { | |
270 | fprintf(stderr, "virtio-net ctrl missing headers\n"); | |
271 | exit(1); | |
272 | } | |
273 | ||
274 | if (elem.out_sg[0].iov_len < sizeof(ctrl) || | |
c6bb9a32 | 275 | elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) { |
3d11d36c AL |
276 | fprintf(stderr, "virtio-net ctrl header not in correct element\n"); |
277 | exit(1); | |
278 | } | |
279 | ||
280 | ctrl.class = ldub_p(elem.out_sg[0].iov_base); | |
281 | ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class)); | |
282 | ||
002437cd AL |
283 | if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE) |
284 | status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem); | |
b6503ed9 AL |
285 | else if (ctrl.class == VIRTIO_NET_CTRL_MAC) |
286 | status = virtio_net_handle_mac(n, ctrl.cmd, &elem); | |
f21c0ed9 AL |
287 | else if (ctrl.class == VIRTIO_NET_CTRL_VLAN) |
288 | status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem); | |
002437cd | 289 | |
3d11d36c AL |
290 | stb_p(elem.in_sg[elem.in_num - 1].iov_base, status); |
291 | ||
292 | virtqueue_push(vq, &elem, sizeof(status)); | |
293 | virtio_notify(vdev, vq); | |
294 | } | |
295 | } | |
296 | ||
fbe78f4f AL |
297 | /* RX */ |
298 | ||
299 | static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq) | |
300 | { | |
8aeff62d MM |
301 | VirtIONet *n = to_virtio_net(vdev); |
302 | ||
303 | qemu_flush_queued_packets(n->vc); | |
fbe78f4f AL |
304 | } |
305 | ||
306 | static int do_virtio_net_can_receive(VirtIONet *n, int bufsize) | |
307 | { | |
308 | if (!virtio_queue_ready(n->rx_vq) || | |
309 | !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) | |
310 | return 0; | |
311 | ||
312 | if (virtio_queue_empty(n->rx_vq) || | |
313 | (n->mergeable_rx_bufs && | |
314 | !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) { | |
315 | virtio_queue_set_notification(n->rx_vq, 1); | |
316 | return 0; | |
317 | } | |
318 | ||
319 | virtio_queue_set_notification(n->rx_vq, 0); | |
320 | return 1; | |
321 | } | |
322 | ||
e3f5ec2b | 323 | static int virtio_net_can_receive(VLANClientState *vc) |
fbe78f4f | 324 | { |
e3f5ec2b | 325 | VirtIONet *n = vc->opaque; |
fbe78f4f AL |
326 | |
327 | return do_virtio_net_can_receive(n, VIRTIO_NET_MAX_BUFSIZE); | |
328 | } | |
329 | ||
330 | static int iov_fill(struct iovec *iov, int iovcnt, const void *buf, int count) | |
331 | { | |
332 | int offset, i; | |
333 | ||
334 | offset = i = 0; | |
335 | while (offset < count && i < iovcnt) { | |
336 | int len = MIN(iov[i].iov_len, count - offset); | |
337 | memcpy(iov[i].iov_base, buf + offset, len); | |
338 | offset += len; | |
339 | i++; | |
340 | } | |
341 | ||
342 | return offset; | |
343 | } | |
344 | ||
345 | static int receive_header(VirtIONet *n, struct iovec *iov, int iovcnt, | |
4689f4b3 | 346 | const void *buf, size_t size, size_t hdr_len) |
fbe78f4f | 347 | { |
3f4cb3d3 | 348 | struct virtio_net_hdr *hdr = (struct virtio_net_hdr *)iov[0].iov_base; |
fbe78f4f AL |
349 | int offset = 0; |
350 | ||
351 | hdr->flags = 0; | |
352 | hdr->gso_type = VIRTIO_NET_HDR_GSO_NONE; | |
353 | ||
354 | /* We only ever receive a struct virtio_net_hdr from the tapfd, | |
355 | * but we may be passing along a larger header to the guest. | |
356 | */ | |
357 | iov[0].iov_base += hdr_len; | |
358 | iov[0].iov_len -= hdr_len; | |
359 | ||
360 | return offset; | |
361 | } | |
362 | ||
3831ab20 AL |
363 | static int receive_filter(VirtIONet *n, const uint8_t *buf, int size) |
364 | { | |
365 | static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; | |
f21c0ed9 | 366 | static const uint8_t vlan[] = {0x81, 0x00}; |
3831ab20 | 367 | uint8_t *ptr = (uint8_t *)buf; |
b6503ed9 | 368 | int i; |
3831ab20 AL |
369 | |
370 | if (n->promisc) | |
371 | return 1; | |
372 | ||
f21c0ed9 AL |
373 | if (!memcmp(&ptr[12], vlan, sizeof(vlan))) { |
374 | int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff; | |
375 | if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f)))) | |
376 | return 0; | |
377 | } | |
378 | ||
bbe2f399 AW |
379 | if (ptr[0] & 1) { // multicast |
380 | if (!memcmp(ptr, bcast, sizeof(bcast))) { | |
015cb166 AW |
381 | return !n->nobcast; |
382 | } else if (n->nomulti) { | |
383 | return 0; | |
8fd2a2f1 | 384 | } else if (n->allmulti || n->mac_table.multi_overflow) { |
bbe2f399 AW |
385 | return 1; |
386 | } | |
2d9aba39 AW |
387 | |
388 | for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) { | |
389 | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { | |
390 | return 1; | |
391 | } | |
392 | } | |
bbe2f399 | 393 | } else { // unicast |
015cb166 AW |
394 | if (n->nouni) { |
395 | return 0; | |
396 | } else if (n->alluni || n->mac_table.uni_overflow) { | |
8fd2a2f1 AW |
397 | return 1; |
398 | } else if (!memcmp(ptr, n->mac, ETH_ALEN)) { | |
bbe2f399 AW |
399 | return 1; |
400 | } | |
3831ab20 | 401 | |
2d9aba39 AW |
402 | for (i = 0; i < n->mac_table.first_multi; i++) { |
403 | if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) { | |
404 | return 1; | |
405 | } | |
406 | } | |
b6503ed9 AL |
407 | } |
408 | ||
3831ab20 AL |
409 | return 0; |
410 | } | |
411 | ||
4f1c942b | 412 | static ssize_t virtio_net_receive(VLANClientState *vc, const uint8_t *buf, size_t size) |
fbe78f4f | 413 | { |
e3f5ec2b | 414 | VirtIONet *n = vc->opaque; |
fbe78f4f | 415 | struct virtio_net_hdr_mrg_rxbuf *mhdr = NULL; |
4689f4b3 | 416 | size_t hdr_len, offset, i; |
fbe78f4f AL |
417 | |
418 | if (!do_virtio_net_can_receive(n, size)) | |
8aeff62d | 419 | return 0; |
fbe78f4f | 420 | |
3831ab20 | 421 | if (!receive_filter(n, buf, size)) |
4f1c942b | 422 | return size; |
3831ab20 | 423 | |
fbe78f4f AL |
424 | /* hdr_len refers to the header we supply to the guest */ |
425 | hdr_len = n->mergeable_rx_bufs ? | |
426 | sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr); | |
427 | ||
428 | offset = i = 0; | |
429 | ||
430 | while (offset < size) { | |
431 | VirtQueueElement elem; | |
432 | int len, total; | |
433 | struct iovec sg[VIRTQUEUE_MAX_SIZE]; | |
434 | ||
435 | len = total = 0; | |
436 | ||
437 | if ((i != 0 && !n->mergeable_rx_bufs) || | |
438 | virtqueue_pop(n->rx_vq, &elem) == 0) { | |
439 | if (i == 0) | |
4f1c942b | 440 | return -1; |
fbe78f4f AL |
441 | fprintf(stderr, "virtio-net truncating packet\n"); |
442 | exit(1); | |
443 | } | |
444 | ||
445 | if (elem.in_num < 1) { | |
446 | fprintf(stderr, "virtio-net receive queue contains no in buffers\n"); | |
447 | exit(1); | |
448 | } | |
449 | ||
450 | if (!n->mergeable_rx_bufs && elem.in_sg[0].iov_len != hdr_len) { | |
451 | fprintf(stderr, "virtio-net header not in first element\n"); | |
452 | exit(1); | |
453 | } | |
454 | ||
455 | memcpy(&sg, &elem.in_sg[0], sizeof(sg[0]) * elem.in_num); | |
456 | ||
457 | if (i == 0) { | |
458 | if (n->mergeable_rx_bufs) | |
459 | mhdr = (struct virtio_net_hdr_mrg_rxbuf *)sg[0].iov_base; | |
460 | ||
461 | offset += receive_header(n, sg, elem.in_num, | |
462 | buf + offset, size - offset, hdr_len); | |
463 | total += hdr_len; | |
464 | } | |
465 | ||
466 | /* copy in packet. ugh */ | |
467 | len = iov_fill(sg, elem.in_num, | |
468 | buf + offset, size - offset); | |
469 | total += len; | |
470 | ||
471 | /* signal other side */ | |
472 | virtqueue_fill(n->rx_vq, &elem, total, i++); | |
473 | ||
474 | offset += len; | |
475 | } | |
476 | ||
477 | if (mhdr) | |
478 | mhdr->num_buffers = i; | |
479 | ||
480 | virtqueue_flush(n->rx_vq, i); | |
481 | virtio_notify(&n->vdev, n->rx_vq); | |
4f1c942b MM |
482 | |
483 | return size; | |
fbe78f4f AL |
484 | } |
485 | ||
486 | /* TX */ | |
487 | static void virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq) | |
488 | { | |
489 | VirtQueueElement elem; | |
490 | int has_vnet_hdr = 0; | |
491 | ||
492 | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) | |
493 | return; | |
494 | ||
495 | while (virtqueue_pop(vq, &elem)) { | |
496 | ssize_t len = 0; | |
497 | unsigned int out_num = elem.out_num; | |
498 | struct iovec *out_sg = &elem.out_sg[0]; | |
499 | unsigned hdr_len; | |
500 | ||
501 | /* hdr_len refers to the header received from the guest */ | |
502 | hdr_len = n->mergeable_rx_bufs ? | |
503 | sizeof(struct virtio_net_hdr_mrg_rxbuf) : | |
504 | sizeof(struct virtio_net_hdr); | |
505 | ||
506 | if (out_num < 1 || out_sg->iov_len != hdr_len) { | |
507 | fprintf(stderr, "virtio-net header not in first element\n"); | |
508 | exit(1); | |
509 | } | |
510 | ||
511 | /* ignore the header if GSO is not supported */ | |
512 | if (!has_vnet_hdr) { | |
513 | out_num--; | |
514 | out_sg++; | |
515 | len += hdr_len; | |
516 | } else if (n->mergeable_rx_bufs) { | |
517 | /* tapfd expects a struct virtio_net_hdr */ | |
518 | hdr_len -= sizeof(struct virtio_net_hdr); | |
519 | out_sg->iov_len -= hdr_len; | |
520 | len += hdr_len; | |
521 | } | |
522 | ||
523 | len += qemu_sendv_packet(n->vc, out_sg, out_num); | |
524 | ||
525 | virtqueue_push(vq, &elem, len); | |
526 | virtio_notify(&n->vdev, vq); | |
527 | } | |
528 | } | |
529 | ||
530 | static void virtio_net_handle_tx(VirtIODevice *vdev, VirtQueue *vq) | |
531 | { | |
532 | VirtIONet *n = to_virtio_net(vdev); | |
533 | ||
534 | if (n->tx_timer_active) { | |
535 | virtio_queue_set_notification(vq, 1); | |
536 | qemu_del_timer(n->tx_timer); | |
537 | n->tx_timer_active = 0; | |
538 | virtio_net_flush_tx(n, vq); | |
539 | } else { | |
540 | qemu_mod_timer(n->tx_timer, | |
541 | qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); | |
542 | n->tx_timer_active = 1; | |
543 | virtio_queue_set_notification(vq, 0); | |
544 | } | |
545 | } | |
546 | ||
547 | static void virtio_net_tx_timer(void *opaque) | |
548 | { | |
549 | VirtIONet *n = opaque; | |
550 | ||
551 | n->tx_timer_active = 0; | |
552 | ||
553 | /* Just in case the driver is not ready on more */ | |
554 | if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) | |
555 | return; | |
556 | ||
557 | virtio_queue_set_notification(n->tx_vq, 1); | |
558 | virtio_net_flush_tx(n, n->tx_vq); | |
559 | } | |
560 | ||
561 | static void virtio_net_save(QEMUFile *f, void *opaque) | |
562 | { | |
563 | VirtIONet *n = opaque; | |
564 | ||
565 | virtio_save(&n->vdev, f); | |
566 | ||
79674068 | 567 | qemu_put_buffer(f, n->mac, ETH_ALEN); |
fbe78f4f | 568 | qemu_put_be32(f, n->tx_timer_active); |
e46cb38f | 569 | qemu_put_be32(f, n->mergeable_rx_bufs); |
9d6271b8 | 570 | qemu_put_be16(f, n->status); |
f10c592e AW |
571 | qemu_put_byte(f, n->promisc); |
572 | qemu_put_byte(f, n->allmulti); | |
b6503ed9 AL |
573 | qemu_put_be32(f, n->mac_table.in_use); |
574 | qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN); | |
f21c0ed9 | 575 | qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); |
6c042c16 | 576 | qemu_put_be32(f, 0); /* vnet-hdr placeholder */ |
8fd2a2f1 AW |
577 | qemu_put_byte(f, n->mac_table.multi_overflow); |
578 | qemu_put_byte(f, n->mac_table.uni_overflow); | |
015cb166 AW |
579 | qemu_put_byte(f, n->alluni); |
580 | qemu_put_byte(f, n->nomulti); | |
581 | qemu_put_byte(f, n->nouni); | |
582 | qemu_put_byte(f, n->nobcast); | |
fbe78f4f AL |
583 | } |
584 | ||
585 | static int virtio_net_load(QEMUFile *f, void *opaque, int version_id) | |
586 | { | |
587 | VirtIONet *n = opaque; | |
2d9aba39 | 588 | int i; |
fbe78f4f | 589 | |
9d6271b8 | 590 | if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION) |
fbe78f4f AL |
591 | return -EINVAL; |
592 | ||
593 | virtio_load(&n->vdev, f); | |
594 | ||
79674068 | 595 | qemu_get_buffer(f, n->mac, ETH_ALEN); |
fbe78f4f | 596 | n->tx_timer_active = qemu_get_be32(f); |
e46cb38f | 597 | n->mergeable_rx_bufs = qemu_get_be32(f); |
fbe78f4f | 598 | |
9d6271b8 AL |
599 | if (version_id >= 3) |
600 | n->status = qemu_get_be16(f); | |
601 | ||
002437cd | 602 | if (version_id >= 4) { |
f10c592e AW |
603 | if (version_id < 8) { |
604 | n->promisc = qemu_get_be32(f); | |
605 | n->allmulti = qemu_get_be32(f); | |
606 | } else { | |
607 | n->promisc = qemu_get_byte(f); | |
608 | n->allmulti = qemu_get_byte(f); | |
609 | } | |
002437cd AL |
610 | } |
611 | ||
b6503ed9 AL |
612 | if (version_id >= 5) { |
613 | n->mac_table.in_use = qemu_get_be32(f); | |
614 | /* MAC_TABLE_ENTRIES may be different from the saved image */ | |
615 | if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) { | |
616 | qemu_get_buffer(f, n->mac_table.macs, | |
617 | n->mac_table.in_use * ETH_ALEN); | |
618 | } else if (n->mac_table.in_use) { | |
619 | qemu_fseek(f, n->mac_table.in_use * ETH_ALEN, SEEK_CUR); | |
8fd2a2f1 | 620 | n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1; |
b6503ed9 AL |
621 | n->mac_table.in_use = 0; |
622 | } | |
623 | } | |
624 | ||
f21c0ed9 AL |
625 | if (version_id >= 6) |
626 | qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3); | |
627 | ||
6c042c16 AW |
628 | if (version_id >= 7 && qemu_get_be32(f)) { |
629 | fprintf(stderr, | |
630 | "virtio-net: saved image requires vnet header support\n"); | |
631 | exit(1); | |
632 | } | |
633 | ||
8fd2a2f1 AW |
634 | if (version_id >= 9) { |
635 | n->mac_table.multi_overflow = qemu_get_byte(f); | |
636 | n->mac_table.uni_overflow = qemu_get_byte(f); | |
637 | } | |
638 | ||
015cb166 AW |
639 | if (version_id >= 10) { |
640 | n->alluni = qemu_get_byte(f); | |
641 | n->nomulti = qemu_get_byte(f); | |
642 | n->nouni = qemu_get_byte(f); | |
643 | n->nobcast = qemu_get_byte(f); | |
644 | } | |
645 | ||
2d9aba39 AW |
646 | /* Find the first multicast entry in the saved MAC filter */ |
647 | for (i = 0; i < n->mac_table.in_use; i++) { | |
648 | if (n->mac_table.macs[i * ETH_ALEN] & 1) { | |
649 | break; | |
650 | } | |
651 | } | |
652 | n->mac_table.first_multi = i; | |
653 | ||
fbe78f4f AL |
654 | if (n->tx_timer_active) { |
655 | qemu_mod_timer(n->tx_timer, | |
656 | qemu_get_clock(vm_clock) + TX_TIMER_INTERVAL); | |
657 | } | |
658 | ||
659 | return 0; | |
660 | } | |
661 | ||
b946a153 AL |
662 | static void virtio_net_cleanup(VLANClientState *vc) |
663 | { | |
664 | VirtIONet *n = vc->opaque; | |
665 | ||
666 | unregister_savevm("virtio-net", n); | |
667 | ||
668 | qemu_free(n->mac_table.macs); | |
669 | qemu_free(n->vlans); | |
670 | ||
671 | qemu_del_timer(n->tx_timer); | |
672 | qemu_free_timer(n->tx_timer); | |
673 | ||
674 | virtio_cleanup(&n->vdev); | |
675 | } | |
676 | ||
53c25cea | 677 | VirtIODevice *virtio_net_init(DeviceState *dev) |
fbe78f4f AL |
678 | { |
679 | VirtIONet *n; | |
680 | static int virtio_net_id; | |
681 | ||
53c25cea PB |
682 | n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET, |
683 | sizeof(struct virtio_net_config), | |
684 | sizeof(VirtIONet)); | |
fbe78f4f | 685 | |
0f03eca6 AL |
686 | n->vdev.get_config = virtio_net_get_config; |
687 | n->vdev.set_config = virtio_net_set_config; | |
fbe78f4f AL |
688 | n->vdev.get_features = virtio_net_get_features; |
689 | n->vdev.set_features = virtio_net_set_features; | |
8eca6b1b | 690 | n->vdev.bad_features = virtio_net_bad_features; |
002437cd | 691 | n->vdev.reset = virtio_net_reset; |
fbe78f4f AL |
692 | n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx); |
693 | n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx); | |
4ffb17f5 | 694 | n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl); |
53c25cea | 695 | qdev_get_macaddr(dev, n->mac); |
554c97dd | 696 | n->status = VIRTIO_NET_S_LINK_UP; |
53c25cea | 697 | n->vc = qdev_get_vlan_client(dev, |
b946a153 | 698 | virtio_net_can_receive, |
463af534 | 699 | virtio_net_receive, NULL, |
b946a153 | 700 | virtio_net_cleanup, n); |
554c97dd | 701 | n->vc->link_status_changed = virtio_net_set_link_status; |
fbe78f4f | 702 | |
96d5e201 AL |
703 | qemu_format_nic_info_str(n->vc, n->mac); |
704 | ||
fbe78f4f AL |
705 | n->tx_timer = qemu_new_timer(vm_clock, virtio_net_tx_timer, n); |
706 | n->tx_timer_active = 0; | |
707 | n->mergeable_rx_bufs = 0; | |
002437cd | 708 | n->promisc = 1; /* for compatibility */ |
fbe78f4f | 709 | |
b6503ed9 | 710 | n->mac_table.macs = qemu_mallocz(MAC_TABLE_ENTRIES * ETH_ALEN); |
b6503ed9 | 711 | |
f21c0ed9 | 712 | n->vlans = qemu_mallocz(MAX_VLAN >> 3); |
f21c0ed9 | 713 | |
9d6271b8 | 714 | register_savevm("virtio-net", virtio_net_id++, VIRTIO_NET_VM_VERSION, |
fbe78f4f | 715 | virtio_net_save, virtio_net_load, n); |
cf21e106 | 716 | |
53c25cea | 717 | return &n->vdev; |
cf21e106 | 718 | } |