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