]> Git Repo - qemu.git/blob - hw/virtio-net.c
virtio-net: test peer header support at init time
[qemu.git] / hw / virtio-net.c
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
14 #include "iov.h"
15 #include "virtio.h"
16 #include "net.h"
17 #include "net/checksum.h"
18 #include "net/tap.h"
19 #include "qemu-error.h"
20 #include "qemu-timer.h"
21 #include "virtio-net.h"
22 #include "vhost_net.h"
23
24 #define VIRTIO_NET_VM_VERSION    11
25
26 #define MAC_TABLE_ENTRIES    64
27 #define MAX_VLAN    (1 << 12)   /* Per 802.1Q definition */
28
29 typedef struct VirtIONet
30 {
31     VirtIODevice vdev;
32     uint8_t mac[ETH_ALEN];
33     uint16_t status;
34     VirtQueue *rx_vq;
35     VirtQueue *tx_vq;
36     VirtQueue *ctrl_vq;
37     NICState *nic;
38     QEMUTimer *tx_timer;
39     QEMUBH *tx_bh;
40     uint32_t tx_timeout;
41     int32_t tx_burst;
42     int tx_waiting;
43     uint32_t has_vnet_hdr;
44     size_t host_hdr_len;
45     size_t guest_hdr_len;
46     uint8_t has_ufo;
47     struct {
48         VirtQueueElement elem;
49         ssize_t len;
50     } async_tx;
51     int mergeable_rx_bufs;
52     uint8_t promisc;
53     uint8_t allmulti;
54     uint8_t alluni;
55     uint8_t nomulti;
56     uint8_t nouni;
57     uint8_t nobcast;
58     uint8_t vhost_started;
59     struct {
60         int in_use;
61         int first_multi;
62         uint8_t multi_overflow;
63         uint8_t uni_overflow;
64         uint8_t *macs;
65     } mac_table;
66     uint32_t *vlans;
67     DeviceState *qdev;
68 } VirtIONet;
69
70 /* TODO
71  * - we could suppress RX interrupt if we were so inclined.
72  */
73
74 static VirtIONet *to_virtio_net(VirtIODevice *vdev)
75 {
76     return (VirtIONet *)vdev;
77 }
78
79 static void virtio_net_get_config(VirtIODevice *vdev, uint8_t *config)
80 {
81     VirtIONet *n = to_virtio_net(vdev);
82     struct virtio_net_config netcfg;
83
84     stw_p(&netcfg.status, n->status);
85     memcpy(netcfg.mac, n->mac, ETH_ALEN);
86     memcpy(config, &netcfg, sizeof(netcfg));
87 }
88
89 static void virtio_net_set_config(VirtIODevice *vdev, const uint8_t *config)
90 {
91     VirtIONet *n = to_virtio_net(vdev);
92     struct virtio_net_config netcfg;
93
94     memcpy(&netcfg, config, sizeof(netcfg));
95
96     if (memcmp(netcfg.mac, n->mac, ETH_ALEN)) {
97         memcpy(n->mac, netcfg.mac, ETH_ALEN);
98         qemu_format_nic_info_str(&n->nic->nc, n->mac);
99     }
100 }
101
102 static bool virtio_net_started(VirtIONet *n, uint8_t status)
103 {
104     return (status & VIRTIO_CONFIG_S_DRIVER_OK) &&
105         (n->status & VIRTIO_NET_S_LINK_UP) && n->vdev.vm_running;
106 }
107
108 static void virtio_net_vhost_status(VirtIONet *n, uint8_t status)
109 {
110     if (!n->nic->nc.peer) {
111         return;
112     }
113     if (n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
114         return;
115     }
116
117     if (!tap_get_vhost_net(n->nic->nc.peer)) {
118         return;
119     }
120     if (!!n->vhost_started == virtio_net_started(n, status) &&
121                               !n->nic->nc.peer->link_down) {
122         return;
123     }
124     if (!n->vhost_started) {
125         int r;
126         if (!vhost_net_query(tap_get_vhost_net(n->nic->nc.peer), &n->vdev)) {
127             return;
128         }
129         r = vhost_net_start(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
130         if (r < 0) {
131             error_report("unable to start vhost net: %d: "
132                          "falling back on userspace virtio", -r);
133         } else {
134             n->vhost_started = 1;
135         }
136     } else {
137         vhost_net_stop(tap_get_vhost_net(n->nic->nc.peer), &n->vdev);
138         n->vhost_started = 0;
139     }
140 }
141
142 static void virtio_net_set_status(struct VirtIODevice *vdev, uint8_t status)
143 {
144     VirtIONet *n = to_virtio_net(vdev);
145
146     virtio_net_vhost_status(n, status);
147
148     if (!n->tx_waiting) {
149         return;
150     }
151
152     if (virtio_net_started(n, status) && !n->vhost_started) {
153         if (n->tx_timer) {
154             qemu_mod_timer(n->tx_timer,
155                            qemu_get_clock_ns(vm_clock) + n->tx_timeout);
156         } else {
157             qemu_bh_schedule(n->tx_bh);
158         }
159     } else {
160         if (n->tx_timer) {
161             qemu_del_timer(n->tx_timer);
162         } else {
163             qemu_bh_cancel(n->tx_bh);
164         }
165     }
166 }
167
168 static void virtio_net_set_link_status(NetClientState *nc)
169 {
170     VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
171     uint16_t old_status = n->status;
172
173     if (nc->link_down)
174         n->status &= ~VIRTIO_NET_S_LINK_UP;
175     else
176         n->status |= VIRTIO_NET_S_LINK_UP;
177
178     if (n->status != old_status)
179         virtio_notify_config(&n->vdev);
180
181     virtio_net_set_status(&n->vdev, n->vdev.status);
182 }
183
184 static void virtio_net_reset(VirtIODevice *vdev)
185 {
186     VirtIONet *n = to_virtio_net(vdev);
187
188     /* Reset back to compatibility mode */
189     n->promisc = 1;
190     n->allmulti = 0;
191     n->alluni = 0;
192     n->nomulti = 0;
193     n->nouni = 0;
194     n->nobcast = 0;
195
196     /* Flush any MAC and VLAN filter table state */
197     n->mac_table.in_use = 0;
198     n->mac_table.first_multi = 0;
199     n->mac_table.multi_overflow = 0;
200     n->mac_table.uni_overflow = 0;
201     memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
202     memset(n->vlans, 0, MAX_VLAN >> 3);
203 }
204
205 static void peer_test_vnet_hdr(VirtIONet *n)
206 {
207     if (!n->nic->nc.peer)
208         return;
209
210     if (n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP)
211         return;
212
213     n->has_vnet_hdr = tap_has_vnet_hdr(n->nic->nc.peer);
214 }
215
216 static int peer_has_vnet_hdr(VirtIONet *n)
217 {
218     return n->has_vnet_hdr;
219 }
220
221 static int peer_has_ufo(VirtIONet *n)
222 {
223     if (!peer_has_vnet_hdr(n))
224         return 0;
225
226     n->has_ufo = tap_has_ufo(n->nic->nc.peer);
227
228     return n->has_ufo;
229 }
230
231 static uint32_t virtio_net_get_features(VirtIODevice *vdev, uint32_t features)
232 {
233     VirtIONet *n = to_virtio_net(vdev);
234
235     features |= (1 << VIRTIO_NET_F_MAC);
236
237     if (!peer_has_vnet_hdr(n)) {
238         features &= ~(0x1 << VIRTIO_NET_F_CSUM);
239         features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO4);
240         features &= ~(0x1 << VIRTIO_NET_F_HOST_TSO6);
241         features &= ~(0x1 << VIRTIO_NET_F_HOST_ECN);
242
243         features &= ~(0x1 << VIRTIO_NET_F_GUEST_CSUM);
244         features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO4);
245         features &= ~(0x1 << VIRTIO_NET_F_GUEST_TSO6);
246         features &= ~(0x1 << VIRTIO_NET_F_GUEST_ECN);
247     }
248
249     if (!peer_has_vnet_hdr(n) || !peer_has_ufo(n)) {
250         features &= ~(0x1 << VIRTIO_NET_F_GUEST_UFO);
251         features &= ~(0x1 << VIRTIO_NET_F_HOST_UFO);
252     }
253
254     if (!n->nic->nc.peer ||
255         n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
256         return features;
257     }
258     if (!tap_get_vhost_net(n->nic->nc.peer)) {
259         return features;
260     }
261     return vhost_net_get_features(tap_get_vhost_net(n->nic->nc.peer), features);
262 }
263
264 static uint32_t virtio_net_bad_features(VirtIODevice *vdev)
265 {
266     uint32_t features = 0;
267
268     /* Linux kernel 2.6.25.  It understood MAC (as everyone must),
269      * but also these: */
270     features |= (1 << VIRTIO_NET_F_MAC);
271     features |= (1 << VIRTIO_NET_F_CSUM);
272     features |= (1 << VIRTIO_NET_F_HOST_TSO4);
273     features |= (1 << VIRTIO_NET_F_HOST_TSO6);
274     features |= (1 << VIRTIO_NET_F_HOST_ECN);
275
276     return features;
277 }
278
279 static void virtio_net_set_features(VirtIODevice *vdev, uint32_t features)
280 {
281     VirtIONet *n = to_virtio_net(vdev);
282
283     n->mergeable_rx_bufs = !!(features & (1 << VIRTIO_NET_F_MRG_RXBUF));
284     n->guest_hdr_len = n->mergeable_rx_bufs ?
285         sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
286
287     if (n->has_vnet_hdr) {
288         tap_set_offload(n->nic->nc.peer,
289                         (features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
290                         (features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
291                         (features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
292                         (features >> VIRTIO_NET_F_GUEST_ECN)  & 1,
293                         (features >> VIRTIO_NET_F_GUEST_UFO)  & 1);
294     }
295     if (!n->nic->nc.peer ||
296         n->nic->nc.peer->info->type != NET_CLIENT_OPTIONS_KIND_TAP) {
297         return;
298     }
299     if (!tap_get_vhost_net(n->nic->nc.peer)) {
300         return;
301     }
302     vhost_net_ack_features(tap_get_vhost_net(n->nic->nc.peer), features);
303 }
304
305 static int virtio_net_handle_rx_mode(VirtIONet *n, uint8_t cmd,
306                                      VirtQueueElement *elem)
307 {
308     uint8_t on;
309
310     if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(on)) {
311         error_report("virtio-net ctrl invalid rx mode command");
312         exit(1);
313     }
314
315     on = ldub_p(elem->out_sg[1].iov_base);
316
317     if (cmd == VIRTIO_NET_CTRL_RX_MODE_PROMISC)
318         n->promisc = on;
319     else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLMULTI)
320         n->allmulti = on;
321     else if (cmd == VIRTIO_NET_CTRL_RX_MODE_ALLUNI)
322         n->alluni = on;
323     else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOMULTI)
324         n->nomulti = on;
325     else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOUNI)
326         n->nouni = on;
327     else if (cmd == VIRTIO_NET_CTRL_RX_MODE_NOBCAST)
328         n->nobcast = on;
329     else
330         return VIRTIO_NET_ERR;
331
332     return VIRTIO_NET_OK;
333 }
334
335 static int virtio_net_handle_mac(VirtIONet *n, uint8_t cmd,
336                                  VirtQueueElement *elem)
337 {
338     struct virtio_net_ctrl_mac mac_data;
339
340     if (cmd != VIRTIO_NET_CTRL_MAC_TABLE_SET || elem->out_num != 3 ||
341         elem->out_sg[1].iov_len < sizeof(mac_data) ||
342         elem->out_sg[2].iov_len < sizeof(mac_data))
343         return VIRTIO_NET_ERR;
344
345     n->mac_table.in_use = 0;
346     n->mac_table.first_multi = 0;
347     n->mac_table.uni_overflow = 0;
348     n->mac_table.multi_overflow = 0;
349     memset(n->mac_table.macs, 0, MAC_TABLE_ENTRIES * ETH_ALEN);
350
351     mac_data.entries = ldl_p(elem->out_sg[1].iov_base);
352
353     if (sizeof(mac_data.entries) +
354         (mac_data.entries * ETH_ALEN) > elem->out_sg[1].iov_len)
355         return VIRTIO_NET_ERR;
356
357     if (mac_data.entries <= MAC_TABLE_ENTRIES) {
358         memcpy(n->mac_table.macs, elem->out_sg[1].iov_base + sizeof(mac_data),
359                mac_data.entries * ETH_ALEN);
360         n->mac_table.in_use += mac_data.entries;
361     } else {
362         n->mac_table.uni_overflow = 1;
363     }
364
365     n->mac_table.first_multi = n->mac_table.in_use;
366
367     mac_data.entries = ldl_p(elem->out_sg[2].iov_base);
368
369     if (sizeof(mac_data.entries) +
370         (mac_data.entries * ETH_ALEN) > elem->out_sg[2].iov_len)
371         return VIRTIO_NET_ERR;
372
373     if (mac_data.entries) {
374         if (n->mac_table.in_use + mac_data.entries <= MAC_TABLE_ENTRIES) {
375             memcpy(n->mac_table.macs + (n->mac_table.in_use * ETH_ALEN),
376                    elem->out_sg[2].iov_base + sizeof(mac_data),
377                    mac_data.entries * ETH_ALEN);
378             n->mac_table.in_use += mac_data.entries;
379         } else {
380             n->mac_table.multi_overflow = 1;
381         }
382     }
383
384     return VIRTIO_NET_OK;
385 }
386
387 static int virtio_net_handle_vlan_table(VirtIONet *n, uint8_t cmd,
388                                         VirtQueueElement *elem)
389 {
390     uint16_t vid;
391
392     if (elem->out_num != 2 || elem->out_sg[1].iov_len != sizeof(vid)) {
393         error_report("virtio-net ctrl invalid vlan command");
394         return VIRTIO_NET_ERR;
395     }
396
397     vid = lduw_p(elem->out_sg[1].iov_base);
398
399     if (vid >= MAX_VLAN)
400         return VIRTIO_NET_ERR;
401
402     if (cmd == VIRTIO_NET_CTRL_VLAN_ADD)
403         n->vlans[vid >> 5] |= (1U << (vid & 0x1f));
404     else if (cmd == VIRTIO_NET_CTRL_VLAN_DEL)
405         n->vlans[vid >> 5] &= ~(1U << (vid & 0x1f));
406     else
407         return VIRTIO_NET_ERR;
408
409     return VIRTIO_NET_OK;
410 }
411
412 static void virtio_net_handle_ctrl(VirtIODevice *vdev, VirtQueue *vq)
413 {
414     VirtIONet *n = to_virtio_net(vdev);
415     struct virtio_net_ctrl_hdr ctrl;
416     virtio_net_ctrl_ack status = VIRTIO_NET_ERR;
417     VirtQueueElement elem;
418
419     while (virtqueue_pop(vq, &elem)) {
420         if ((elem.in_num < 1) || (elem.out_num < 1)) {
421             error_report("virtio-net ctrl missing headers");
422             exit(1);
423         }
424
425         if (elem.out_sg[0].iov_len < sizeof(ctrl) ||
426             elem.in_sg[elem.in_num - 1].iov_len < sizeof(status)) {
427             error_report("virtio-net ctrl header not in correct element");
428             exit(1);
429         }
430
431         ctrl.class = ldub_p(elem.out_sg[0].iov_base);
432         ctrl.cmd = ldub_p(elem.out_sg[0].iov_base + sizeof(ctrl.class));
433
434         if (ctrl.class == VIRTIO_NET_CTRL_RX_MODE)
435             status = virtio_net_handle_rx_mode(n, ctrl.cmd, &elem);
436         else if (ctrl.class == VIRTIO_NET_CTRL_MAC)
437             status = virtio_net_handle_mac(n, ctrl.cmd, &elem);
438         else if (ctrl.class == VIRTIO_NET_CTRL_VLAN)
439             status = virtio_net_handle_vlan_table(n, ctrl.cmd, &elem);
440
441         stb_p(elem.in_sg[elem.in_num - 1].iov_base, status);
442
443         virtqueue_push(vq, &elem, sizeof(status));
444         virtio_notify(vdev, vq);
445     }
446 }
447
448 /* RX */
449
450 static void virtio_net_handle_rx(VirtIODevice *vdev, VirtQueue *vq)
451 {
452     VirtIONet *n = to_virtio_net(vdev);
453
454     qemu_flush_queued_packets(&n->nic->nc);
455 }
456
457 static int virtio_net_can_receive(NetClientState *nc)
458 {
459     VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
460     if (!n->vdev.vm_running) {
461         return 0;
462     }
463
464     if (!virtio_queue_ready(n->rx_vq) ||
465         !(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
466         return 0;
467
468     return 1;
469 }
470
471 static int virtio_net_has_buffers(VirtIONet *n, int bufsize)
472 {
473     if (virtio_queue_empty(n->rx_vq) ||
474         (n->mergeable_rx_bufs &&
475          !virtqueue_avail_bytes(n->rx_vq, bufsize, 0))) {
476         virtio_queue_set_notification(n->rx_vq, 1);
477
478         /* To avoid a race condition where the guest has made some buffers
479          * available after the above check but before notification was
480          * enabled, check for available buffers again.
481          */
482         if (virtio_queue_empty(n->rx_vq) ||
483             (n->mergeable_rx_bufs &&
484              !virtqueue_avail_bytes(n->rx_vq, bufsize, 0)))
485             return 0;
486     }
487
488     virtio_queue_set_notification(n->rx_vq, 0);
489     return 1;
490 }
491
492 /* dhclient uses AF_PACKET but doesn't pass auxdata to the kernel so
493  * it never finds out that the packets don't have valid checksums.  This
494  * causes dhclient to get upset.  Fedora's carried a patch for ages to
495  * fix this with Xen but it hasn't appeared in an upstream release of
496  * dhclient yet.
497  *
498  * To avoid breaking existing guests, we catch udp packets and add
499  * checksums.  This is terrible but it's better than hacking the guest
500  * kernels.
501  *
502  * N.B. if we introduce a zero-copy API, this operation is no longer free so
503  * we should provide a mechanism to disable it to avoid polluting the host
504  * cache.
505  */
506 static void work_around_broken_dhclient(struct virtio_net_hdr *hdr,
507                                         uint8_t *buf, size_t size)
508 {
509     if ((hdr->flags & VIRTIO_NET_HDR_F_NEEDS_CSUM) && /* missing csum */
510         (size > 27 && size < 1500) && /* normal sized MTU */
511         (buf[12] == 0x08 && buf[13] == 0x00) && /* ethertype == IPv4 */
512         (buf[23] == 17) && /* ip.protocol == UDP */
513         (buf[34] == 0 && buf[35] == 67)) { /* udp.srcport == bootps */
514         net_checksum_calculate(buf, size);
515         hdr->flags &= ~VIRTIO_NET_HDR_F_NEEDS_CSUM;
516     }
517 }
518
519 static void receive_header(VirtIONet *n, const struct iovec *iov, int iov_cnt,
520                            const void *buf, size_t size)
521 {
522     if (n->has_vnet_hdr) {
523         /* FIXME this cast is evil */
524         void *wbuf = (void *)buf;
525         work_around_broken_dhclient(wbuf, wbuf + n->host_hdr_len,
526                                     size - n->host_hdr_len);
527         iov_from_buf(iov, iov_cnt, 0, buf, sizeof(struct virtio_net_hdr));
528     } else {
529         struct virtio_net_hdr hdr = {
530             .flags = 0,
531             .gso_type = VIRTIO_NET_HDR_GSO_NONE
532         };
533         iov_from_buf(iov, iov_cnt, 0, &hdr, sizeof hdr);
534     }
535 }
536
537 static int receive_filter(VirtIONet *n, const uint8_t *buf, int size)
538 {
539     static const uint8_t bcast[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
540     static const uint8_t vlan[] = {0x81, 0x00};
541     uint8_t *ptr = (uint8_t *)buf;
542     int i;
543
544     if (n->promisc)
545         return 1;
546
547     ptr += n->host_hdr_len;
548
549     if (!memcmp(&ptr[12], vlan, sizeof(vlan))) {
550         int vid = be16_to_cpup((uint16_t *)(ptr + 14)) & 0xfff;
551         if (!(n->vlans[vid >> 5] & (1U << (vid & 0x1f))))
552             return 0;
553     }
554
555     if (ptr[0] & 1) { // multicast
556         if (!memcmp(ptr, bcast, sizeof(bcast))) {
557             return !n->nobcast;
558         } else if (n->nomulti) {
559             return 0;
560         } else if (n->allmulti || n->mac_table.multi_overflow) {
561             return 1;
562         }
563
564         for (i = n->mac_table.first_multi; i < n->mac_table.in_use; i++) {
565             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
566                 return 1;
567             }
568         }
569     } else { // unicast
570         if (n->nouni) {
571             return 0;
572         } else if (n->alluni || n->mac_table.uni_overflow) {
573             return 1;
574         } else if (!memcmp(ptr, n->mac, ETH_ALEN)) {
575             return 1;
576         }
577
578         for (i = 0; i < n->mac_table.first_multi; i++) {
579             if (!memcmp(ptr, &n->mac_table.macs[i * ETH_ALEN], ETH_ALEN)) {
580                 return 1;
581             }
582         }
583     }
584
585     return 0;
586 }
587
588 static ssize_t virtio_net_receive(NetClientState *nc, const uint8_t *buf, size_t size)
589 {
590     VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
591     struct iovec mhdr_sg[VIRTQUEUE_MAX_SIZE];
592     struct virtio_net_hdr_mrg_rxbuf mhdr;
593     unsigned mhdr_cnt = 0;
594     size_t offset, i, guest_offset;
595
596     if (!virtio_net_can_receive(&n->nic->nc))
597         return -1;
598
599     /* hdr_len refers to the header we supply to the guest */
600     if (!virtio_net_has_buffers(n, size + n->guest_hdr_len - n->host_hdr_len))
601         return 0;
602
603     if (!receive_filter(n, buf, size))
604         return size;
605
606     offset = i = 0;
607
608     while (offset < size) {
609         VirtQueueElement elem;
610         int len, total;
611         const struct iovec *sg = elem.in_sg;
612
613         total = 0;
614
615         if (virtqueue_pop(n->rx_vq, &elem) == 0) {
616             if (i == 0)
617                 return -1;
618             error_report("virtio-net unexpected empty queue: "
619                     "i %zd mergeable %d offset %zd, size %zd, "
620                     "guest hdr len %zd, host hdr len %zd guest features 0x%x",
621                     i, n->mergeable_rx_bufs, offset, size,
622                     n->guest_hdr_len, n->host_hdr_len, n->vdev.guest_features);
623             exit(1);
624         }
625
626         if (elem.in_num < 1) {
627             error_report("virtio-net receive queue contains no in buffers");
628             exit(1);
629         }
630
631         if (i == 0) {
632             assert(offset == 0);
633             if (n->mergeable_rx_bufs) {
634                 mhdr_cnt = iov_copy(mhdr_sg, ARRAY_SIZE(mhdr_sg),
635                                     sg, elem.in_num,
636                                     offsetof(typeof(mhdr), num_buffers),
637                                     sizeof(mhdr.num_buffers));
638             }
639
640             receive_header(n, sg, elem.in_num, buf, size);
641             offset = n->host_hdr_len;
642             total += n->guest_hdr_len;
643             guest_offset = n->guest_hdr_len;
644         } else {
645             guest_offset = 0;
646         }
647
648         /* copy in packet.  ugh */
649         len = iov_from_buf(sg, elem.in_num, guest_offset,
650                            buf + offset, size - offset);
651         total += len;
652         offset += len;
653         /* If buffers can't be merged, at this point we
654          * must have consumed the complete packet.
655          * Otherwise, drop it. */
656         if (!n->mergeable_rx_bufs && offset < size) {
657 #if 0
658             error_report("virtio-net truncated non-mergeable packet: "
659                          "i %zd mergeable %d offset %zd, size %zd, "
660                          "guest hdr len %zd, host hdr len %zd",
661                          i, n->mergeable_rx_bufs,
662                          offset, size, n->guest_hdr_len, n->host_hdr_len);
663 #endif
664             return size;
665         }
666
667         /* signal other side */
668         virtqueue_fill(n->rx_vq, &elem, total, i++);
669     }
670
671     if (mhdr_cnt) {
672         stw_p(&mhdr.num_buffers, i);
673         iov_from_buf(mhdr_sg, mhdr_cnt,
674                      0,
675                      &mhdr.num_buffers, sizeof mhdr.num_buffers);
676     }
677
678     virtqueue_flush(n->rx_vq, i);
679     virtio_notify(&n->vdev, n->rx_vq);
680
681     return size;
682 }
683
684 static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq);
685
686 static void virtio_net_tx_complete(NetClientState *nc, ssize_t len)
687 {
688     VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
689
690     virtqueue_push(n->tx_vq, &n->async_tx.elem, 0);
691     virtio_notify(&n->vdev, n->tx_vq);
692
693     n->async_tx.elem.out_num = n->async_tx.len = 0;
694
695     virtio_queue_set_notification(n->tx_vq, 1);
696     virtio_net_flush_tx(n, n->tx_vq);
697 }
698
699 /* TX */
700 static int32_t virtio_net_flush_tx(VirtIONet *n, VirtQueue *vq)
701 {
702     VirtQueueElement elem;
703     int32_t num_packets = 0;
704     if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)) {
705         return num_packets;
706     }
707
708     assert(n->vdev.vm_running);
709
710     if (n->async_tx.elem.out_num) {
711         virtio_queue_set_notification(n->tx_vq, 0);
712         return num_packets;
713     }
714
715     while (virtqueue_pop(vq, &elem)) {
716         ssize_t ret, len;
717         unsigned int out_num = elem.out_num;
718         struct iovec *out_sg = &elem.out_sg[0];
719         struct iovec sg[VIRTQUEUE_MAX_SIZE];
720
721         if (out_num < 1) {
722             error_report("virtio-net header not in first element");
723             exit(1);
724         }
725
726         /*
727          * If host wants to see the guest header as is, we can
728          * pass it on unchanged. Otherwise, copy just the parts
729          * that host is interested in.
730          */
731         assert(n->host_hdr_len <= n->guest_hdr_len);
732         if (n->host_hdr_len != n->guest_hdr_len) {
733             unsigned sg_num = iov_copy(sg, ARRAY_SIZE(sg),
734                                        out_sg, out_num,
735                                        0, n->host_hdr_len);
736             sg_num += iov_copy(sg + sg_num, ARRAY_SIZE(sg) - sg_num,
737                              out_sg, out_num,
738                              n->guest_hdr_len, -1);
739             out_num = sg_num;
740             out_sg = sg;
741         }
742
743         len = n->guest_hdr_len;
744
745         ret = qemu_sendv_packet_async(&n->nic->nc, out_sg, out_num,
746                                       virtio_net_tx_complete);
747         if (ret == 0) {
748             virtio_queue_set_notification(n->tx_vq, 0);
749             n->async_tx.elem = elem;
750             n->async_tx.len  = len;
751             return -EBUSY;
752         }
753
754         len += ret;
755
756         virtqueue_push(vq, &elem, 0);
757         virtio_notify(&n->vdev, vq);
758
759         if (++num_packets >= n->tx_burst) {
760             break;
761         }
762     }
763     return num_packets;
764 }
765
766 static void virtio_net_handle_tx_timer(VirtIODevice *vdev, VirtQueue *vq)
767 {
768     VirtIONet *n = to_virtio_net(vdev);
769
770     /* This happens when device was stopped but VCPU wasn't. */
771     if (!n->vdev.vm_running) {
772         n->tx_waiting = 1;
773         return;
774     }
775
776     if (n->tx_waiting) {
777         virtio_queue_set_notification(vq, 1);
778         qemu_del_timer(n->tx_timer);
779         n->tx_waiting = 0;
780         virtio_net_flush_tx(n, vq);
781     } else {
782         qemu_mod_timer(n->tx_timer,
783                        qemu_get_clock_ns(vm_clock) + n->tx_timeout);
784         n->tx_waiting = 1;
785         virtio_queue_set_notification(vq, 0);
786     }
787 }
788
789 static void virtio_net_handle_tx_bh(VirtIODevice *vdev, VirtQueue *vq)
790 {
791     VirtIONet *n = to_virtio_net(vdev);
792
793     if (unlikely(n->tx_waiting)) {
794         return;
795     }
796     n->tx_waiting = 1;
797     /* This happens when device was stopped but VCPU wasn't. */
798     if (!n->vdev.vm_running) {
799         return;
800     }
801     virtio_queue_set_notification(vq, 0);
802     qemu_bh_schedule(n->tx_bh);
803 }
804
805 static void virtio_net_tx_timer(void *opaque)
806 {
807     VirtIONet *n = opaque;
808     assert(n->vdev.vm_running);
809
810     n->tx_waiting = 0;
811
812     /* Just in case the driver is not ready on more */
813     if (!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK))
814         return;
815
816     virtio_queue_set_notification(n->tx_vq, 1);
817     virtio_net_flush_tx(n, n->tx_vq);
818 }
819
820 static void virtio_net_tx_bh(void *opaque)
821 {
822     VirtIONet *n = opaque;
823     int32_t ret;
824
825     assert(n->vdev.vm_running);
826
827     n->tx_waiting = 0;
828
829     /* Just in case the driver is not ready on more */
830     if (unlikely(!(n->vdev.status & VIRTIO_CONFIG_S_DRIVER_OK)))
831         return;
832
833     ret = virtio_net_flush_tx(n, n->tx_vq);
834     if (ret == -EBUSY) {
835         return; /* Notification re-enable handled by tx_complete */
836     }
837
838     /* If we flush a full burst of packets, assume there are
839      * more coming and immediately reschedule */
840     if (ret >= n->tx_burst) {
841         qemu_bh_schedule(n->tx_bh);
842         n->tx_waiting = 1;
843         return;
844     }
845
846     /* If less than a full burst, re-enable notification and flush
847      * anything that may have come in while we weren't looking.  If
848      * we find something, assume the guest is still active and reschedule */
849     virtio_queue_set_notification(n->tx_vq, 1);
850     if (virtio_net_flush_tx(n, n->tx_vq) > 0) {
851         virtio_queue_set_notification(n->tx_vq, 0);
852         qemu_bh_schedule(n->tx_bh);
853         n->tx_waiting = 1;
854     }
855 }
856
857 static void virtio_net_save(QEMUFile *f, void *opaque)
858 {
859     VirtIONet *n = opaque;
860
861     /* At this point, backend must be stopped, otherwise
862      * it might keep writing to memory. */
863     assert(!n->vhost_started);
864     virtio_save(&n->vdev, f);
865
866     qemu_put_buffer(f, n->mac, ETH_ALEN);
867     qemu_put_be32(f, n->tx_waiting);
868     qemu_put_be32(f, n->mergeable_rx_bufs);
869     qemu_put_be16(f, n->status);
870     qemu_put_byte(f, n->promisc);
871     qemu_put_byte(f, n->allmulti);
872     qemu_put_be32(f, n->mac_table.in_use);
873     qemu_put_buffer(f, n->mac_table.macs, n->mac_table.in_use * ETH_ALEN);
874     qemu_put_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
875     qemu_put_be32(f, n->has_vnet_hdr);
876     qemu_put_byte(f, n->mac_table.multi_overflow);
877     qemu_put_byte(f, n->mac_table.uni_overflow);
878     qemu_put_byte(f, n->alluni);
879     qemu_put_byte(f, n->nomulti);
880     qemu_put_byte(f, n->nouni);
881     qemu_put_byte(f, n->nobcast);
882     qemu_put_byte(f, n->has_ufo);
883 }
884
885 static int virtio_net_load(QEMUFile *f, void *opaque, int version_id)
886 {
887     VirtIONet *n = opaque;
888     int i;
889     int ret;
890
891     if (version_id < 2 || version_id > VIRTIO_NET_VM_VERSION)
892         return -EINVAL;
893
894     ret = virtio_load(&n->vdev, f);
895     if (ret) {
896         return ret;
897     }
898
899     qemu_get_buffer(f, n->mac, ETH_ALEN);
900     n->tx_waiting = qemu_get_be32(f);
901     n->mergeable_rx_bufs = qemu_get_be32(f);
902     n->guest_hdr_len = n->mergeable_rx_bufs ?
903         sizeof(struct virtio_net_hdr_mrg_rxbuf) : sizeof(struct virtio_net_hdr);
904
905     if (version_id >= 3)
906         n->status = qemu_get_be16(f);
907
908     if (version_id >= 4) {
909         if (version_id < 8) {
910             n->promisc = qemu_get_be32(f);
911             n->allmulti = qemu_get_be32(f);
912         } else {
913             n->promisc = qemu_get_byte(f);
914             n->allmulti = qemu_get_byte(f);
915         }
916     }
917
918     if (version_id >= 5) {
919         n->mac_table.in_use = qemu_get_be32(f);
920         /* MAC_TABLE_ENTRIES may be different from the saved image */
921         if (n->mac_table.in_use <= MAC_TABLE_ENTRIES) {
922             qemu_get_buffer(f, n->mac_table.macs,
923                             n->mac_table.in_use * ETH_ALEN);
924         } else if (n->mac_table.in_use) {
925             uint8_t *buf = g_malloc0(n->mac_table.in_use);
926             qemu_get_buffer(f, buf, n->mac_table.in_use * ETH_ALEN);
927             g_free(buf);
928             n->mac_table.multi_overflow = n->mac_table.uni_overflow = 1;
929             n->mac_table.in_use = 0;
930         }
931     }
932  
933     if (version_id >= 6)
934         qemu_get_buffer(f, (uint8_t *)n->vlans, MAX_VLAN >> 3);
935
936     if (version_id >= 7) {
937         if (qemu_get_be32(f) && !peer_has_vnet_hdr(n)) {
938             error_report("virtio-net: saved image requires vnet_hdr=on");
939             return -1;
940         }
941
942         if (n->has_vnet_hdr) {
943             tap_set_offload(n->nic->nc.peer,
944                     (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_CSUM) & 1,
945                     (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO4) & 1,
946                     (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_TSO6) & 1,
947                     (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_ECN)  & 1,
948                     (n->vdev.guest_features >> VIRTIO_NET_F_GUEST_UFO)  & 1);
949         }
950     }
951
952     if (version_id >= 9) {
953         n->mac_table.multi_overflow = qemu_get_byte(f);
954         n->mac_table.uni_overflow = qemu_get_byte(f);
955     }
956
957     if (version_id >= 10) {
958         n->alluni = qemu_get_byte(f);
959         n->nomulti = qemu_get_byte(f);
960         n->nouni = qemu_get_byte(f);
961         n->nobcast = qemu_get_byte(f);
962     }
963
964     if (version_id >= 11) {
965         if (qemu_get_byte(f) && !peer_has_ufo(n)) {
966             error_report("virtio-net: saved image requires TUN_F_UFO support");
967             return -1;
968         }
969     }
970
971     /* Find the first multicast entry in the saved MAC filter */
972     for (i = 0; i < n->mac_table.in_use; i++) {
973         if (n->mac_table.macs[i * ETH_ALEN] & 1) {
974             break;
975         }
976     }
977     n->mac_table.first_multi = i;
978
979     /* nc.link_down can't be migrated, so infer link_down according
980      * to link status bit in n->status */
981     n->nic->nc.link_down = (n->status & VIRTIO_NET_S_LINK_UP) == 0;
982
983     return 0;
984 }
985
986 static void virtio_net_cleanup(NetClientState *nc)
987 {
988     VirtIONet *n = DO_UPCAST(NICState, nc, nc)->opaque;
989
990     n->nic = NULL;
991 }
992
993 static NetClientInfo net_virtio_info = {
994     .type = NET_CLIENT_OPTIONS_KIND_NIC,
995     .size = sizeof(NICState),
996     .can_receive = virtio_net_can_receive,
997     .receive = virtio_net_receive,
998         .cleanup = virtio_net_cleanup,
999     .link_status_changed = virtio_net_set_link_status,
1000 };
1001
1002 VirtIODevice *virtio_net_init(DeviceState *dev, NICConf *conf,
1003                               virtio_net_conf *net)
1004 {
1005     VirtIONet *n;
1006
1007     n = (VirtIONet *)virtio_common_init("virtio-net", VIRTIO_ID_NET,
1008                                         sizeof(struct virtio_net_config),
1009                                         sizeof(VirtIONet));
1010
1011     n->vdev.get_config = virtio_net_get_config;
1012     n->vdev.set_config = virtio_net_set_config;
1013     n->vdev.get_features = virtio_net_get_features;
1014     n->vdev.set_features = virtio_net_set_features;
1015     n->vdev.bad_features = virtio_net_bad_features;
1016     n->vdev.reset = virtio_net_reset;
1017     n->vdev.set_status = virtio_net_set_status;
1018     n->rx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_rx);
1019
1020     if (net->tx && strcmp(net->tx, "timer") && strcmp(net->tx, "bh")) {
1021         error_report("virtio-net: "
1022                      "Unknown option tx=%s, valid options: \"timer\" \"bh\"",
1023                      net->tx);
1024         error_report("Defaulting to \"bh\"");
1025     }
1026
1027     if (net->tx && !strcmp(net->tx, "timer")) {
1028         n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_timer);
1029         n->tx_timer = qemu_new_timer_ns(vm_clock, virtio_net_tx_timer, n);
1030         n->tx_timeout = net->txtimer;
1031     } else {
1032         n->tx_vq = virtio_add_queue(&n->vdev, 256, virtio_net_handle_tx_bh);
1033         n->tx_bh = qemu_bh_new(virtio_net_tx_bh, n);
1034     }
1035     n->ctrl_vq = virtio_add_queue(&n->vdev, 64, virtio_net_handle_ctrl);
1036     qemu_macaddr_default_if_unset(&conf->macaddr);
1037     memcpy(&n->mac[0], &conf->macaddr, sizeof(n->mac));
1038     n->status = VIRTIO_NET_S_LINK_UP;
1039
1040     n->nic = qemu_new_nic(&net_virtio_info, conf, object_get_typename(OBJECT(dev)), dev->id, n);
1041     peer_test_vnet_hdr(n);
1042     if (peer_has_vnet_hdr(n)) {
1043         tap_using_vnet_hdr(n->nic->nc.peer, 1);
1044         n->host_hdr_len = sizeof(struct virtio_net_hdr);
1045     } else {
1046         n->host_hdr_len = 0;
1047     }
1048
1049     qemu_format_nic_info_str(&n->nic->nc, conf->macaddr.a);
1050
1051     n->tx_waiting = 0;
1052     n->tx_burst = net->txburst;
1053     n->mergeable_rx_bufs = 0;
1054     n->guest_hdr_len = sizeof(struct virtio_net_hdr);
1055     n->promisc = 1; /* for compatibility */
1056
1057     n->mac_table.macs = g_malloc0(MAC_TABLE_ENTRIES * ETH_ALEN);
1058
1059     n->vlans = g_malloc0(MAX_VLAN >> 3);
1060
1061     n->qdev = dev;
1062     register_savevm(dev, "virtio-net", -1, VIRTIO_NET_VM_VERSION,
1063                     virtio_net_save, virtio_net_load, n);
1064
1065     add_boot_device_path(conf->bootindex, dev, "/ethernet-phy@0");
1066
1067     return &n->vdev;
1068 }
1069
1070 void virtio_net_exit(VirtIODevice *vdev)
1071 {
1072     VirtIONet *n = DO_UPCAST(VirtIONet, vdev, vdev);
1073
1074     /* This will stop vhost backend if appropriate. */
1075     virtio_net_set_status(vdev, 0);
1076
1077     qemu_purge_queued_packets(&n->nic->nc);
1078
1079     unregister_savevm(n->qdev, "virtio-net", n);
1080
1081     g_free(n->mac_table.macs);
1082     g_free(n->vlans);
1083
1084     if (n->tx_timer) {
1085         qemu_del_timer(n->tx_timer);
1086         qemu_free_timer(n->tx_timer);
1087     } else {
1088         qemu_bh_delete(n->tx_bh);
1089     }
1090
1091     qemu_del_net_client(&n->nic->nc);
1092     virtio_cleanup(&n->vdev);
1093 }
This page took 0.087233 seconds and 4 git commands to generate.