Statistics
| Branch: | Revision:

root / hw / virtio-net.c @ 1e89ad5b

History | View | Annotate | Download (40.1 kB)

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