Statistics
| Branch: | Revision:

root / hw / xen_nic.c @ 7830cf78

History | View | Annotate | Download (13.8 kB)

1 e613b064 aliguori
/*
2 e613b064 aliguori
 *  xen paravirt network card backend
3 e613b064 aliguori
 *
4 e613b064 aliguori
 *  (c) Gerd Hoffmann <kraxel@redhat.com>
5 e613b064 aliguori
 *
6 e613b064 aliguori
 *  This program is free software; you can redistribute it and/or modify
7 e613b064 aliguori
 *  it under the terms of the GNU General Public License as published by
8 e613b064 aliguori
 *  the Free Software Foundation; under version 2 of the License.
9 e613b064 aliguori
 *
10 e613b064 aliguori
 *  This program is distributed in the hope that it will be useful,
11 e613b064 aliguori
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 e613b064 aliguori
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 e613b064 aliguori
 *  GNU General Public License for more details.
14 e613b064 aliguori
 *
15 e613b064 aliguori
 *  You should have received a copy of the GNU General Public License along
16 8167ee88 Blue Swirl
 *  with this program; if not, see <http://www.gnu.org/licenses/>.
17 6b620ca3 Paolo Bonzini
 *
18 6b620ca3 Paolo Bonzini
 *  Contributions after 2012-01-13 are licensed under the terms of the
19 6b620ca3 Paolo Bonzini
 *  GNU GPL, version 2 or (at your option) any later version.
20 e613b064 aliguori
 */
21 e613b064 aliguori
22 e613b064 aliguori
#include <stdio.h>
23 e613b064 aliguori
#include <stdlib.h>
24 e613b064 aliguori
#include <stdarg.h>
25 e613b064 aliguori
#include <string.h>
26 e613b064 aliguori
#include <unistd.h>
27 e613b064 aliguori
#include <signal.h>
28 e613b064 aliguori
#include <inttypes.h>
29 e613b064 aliguori
#include <fcntl.h>
30 e613b064 aliguori
#include <errno.h>
31 e613b064 aliguori
#include <sys/socket.h>
32 e613b064 aliguori
#include <sys/ioctl.h>
33 e613b064 aliguori
#include <sys/types.h>
34 e613b064 aliguori
#include <sys/stat.h>
35 e613b064 aliguori
#include <sys/mman.h>
36 e613b064 aliguori
#include <sys/wait.h>
37 e613b064 aliguori
38 e613b064 aliguori
#include "hw.h"
39 1422e32d Paolo Bonzini
#include "net/net.h"
40 7200ac3c Mark McLoughlin
#include "net/checksum.h"
41 658788c5 Mark McLoughlin
#include "net/util.h"
42 e613b064 aliguori
#include "xen_backend.h"
43 e613b064 aliguori
44 b41f6719 Anthony PERARD
#include <xen/io/netif.h>
45 b41f6719 Anthony PERARD
46 e613b064 aliguori
/* ------------------------------------------------------------- */
47 e613b064 aliguori
48 e613b064 aliguori
struct XenNetDev {
49 e613b064 aliguori
    struct XenDevice      xendev;  /* must be first */
50 e613b064 aliguori
    char                  *mac;
51 e613b064 aliguori
    int                   tx_work;
52 e613b064 aliguori
    int                   tx_ring_ref;
53 e613b064 aliguori
    int                   rx_ring_ref;
54 e613b064 aliguori
    struct netif_tx_sring *txs;
55 e613b064 aliguori
    struct netif_rx_sring *rxs;
56 e613b064 aliguori
    netif_tx_back_ring_t  tx_ring;
57 e613b064 aliguori
    netif_rx_back_ring_t  rx_ring;
58 658788c5 Mark McLoughlin
    NICConf               conf;
59 658788c5 Mark McLoughlin
    NICState              *nic;
60 e613b064 aliguori
};
61 e613b064 aliguori
62 e613b064 aliguori
/* ------------------------------------------------------------- */
63 e613b064 aliguori
64 e613b064 aliguori
static void net_tx_response(struct XenNetDev *netdev, netif_tx_request_t *txp, int8_t st)
65 e613b064 aliguori
{
66 e613b064 aliguori
    RING_IDX i = netdev->tx_ring.rsp_prod_pvt;
67 e613b064 aliguori
    netif_tx_response_t *resp;
68 e613b064 aliguori
    int notify;
69 e613b064 aliguori
70 e613b064 aliguori
    resp = RING_GET_RESPONSE(&netdev->tx_ring, i);
71 e613b064 aliguori
    resp->id     = txp->id;
72 e613b064 aliguori
    resp->status = st;
73 e613b064 aliguori
74 e613b064 aliguori
#if 0
75 209cd7ab Anthony PERARD
    if (txp->flags & NETTXF_extra_info) {
76 209cd7ab Anthony PERARD
        RING_GET_RESPONSE(&netdev->tx_ring, ++i)->status = NETIF_RSP_NULL;
77 209cd7ab Anthony PERARD
    }
78 e613b064 aliguori
#endif
79 e613b064 aliguori
80 e613b064 aliguori
    netdev->tx_ring.rsp_prod_pvt = ++i;
81 e613b064 aliguori
    RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->tx_ring, notify);
82 209cd7ab Anthony PERARD
    if (notify) {
83 209cd7ab Anthony PERARD
        xen_be_send_notify(&netdev->xendev);
84 209cd7ab Anthony PERARD
    }
85 e613b064 aliguori
86 e613b064 aliguori
    if (i == netdev->tx_ring.req_cons) {
87 209cd7ab Anthony PERARD
        int more_to_do;
88 209cd7ab Anthony PERARD
        RING_FINAL_CHECK_FOR_REQUESTS(&netdev->tx_ring, more_to_do);
89 209cd7ab Anthony PERARD
        if (more_to_do) {
90 209cd7ab Anthony PERARD
            netdev->tx_work++;
91 209cd7ab Anthony PERARD
        }
92 e613b064 aliguori
    }
93 e613b064 aliguori
}
94 e613b064 aliguori
95 e613b064 aliguori
static void net_tx_error(struct XenNetDev *netdev, netif_tx_request_t *txp, RING_IDX end)
96 e613b064 aliguori
{
97 e613b064 aliguori
#if 0
98 e613b064 aliguori
    /*
99 e613b064 aliguori
     * Hmm, why netback fails everything in the ring?
100 e613b064 aliguori
     * Should we do that even when not supporting SG and TSO?
101 e613b064 aliguori
     */
102 e613b064 aliguori
    RING_IDX cons = netdev->tx_ring.req_cons;
103 e613b064 aliguori

104 e613b064 aliguori
    do {
105 209cd7ab Anthony PERARD
        make_tx_response(netif, txp, NETIF_RSP_ERROR);
106 209cd7ab Anthony PERARD
        if (cons >= end) {
107 209cd7ab Anthony PERARD
            break;
108 209cd7ab Anthony PERARD
        }
109 209cd7ab Anthony PERARD
        txp = RING_GET_REQUEST(&netdev->tx_ring, cons++);
110 e613b064 aliguori
    } while (1);
111 e613b064 aliguori
    netdev->tx_ring.req_cons = cons;
112 e613b064 aliguori
    netif_schedule_work(netif);
113 e613b064 aliguori
    netif_put(netif);
114 e613b064 aliguori
#else
115 e613b064 aliguori
    net_tx_response(netdev, txp, NETIF_RSP_ERROR);
116 e613b064 aliguori
#endif
117 e613b064 aliguori
}
118 e613b064 aliguori
119 e613b064 aliguori
static void net_tx_packets(struct XenNetDev *netdev)
120 e613b064 aliguori
{
121 e613b064 aliguori
    netif_tx_request_t txreq;
122 e613b064 aliguori
    RING_IDX rc, rp;
123 e613b064 aliguori
    void *page;
124 e613b064 aliguori
    void *tmpbuf = NULL;
125 e613b064 aliguori
126 e613b064 aliguori
    for (;;) {
127 209cd7ab Anthony PERARD
        rc = netdev->tx_ring.req_cons;
128 209cd7ab Anthony PERARD
        rp = netdev->tx_ring.sring->req_prod;
129 209cd7ab Anthony PERARD
        xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
130 e613b064 aliguori
131 209cd7ab Anthony PERARD
        while ((rc != rp)) {
132 209cd7ab Anthony PERARD
            if (RING_REQUEST_CONS_OVERFLOW(&netdev->tx_ring, rc)) {
133 209cd7ab Anthony PERARD
                break;
134 209cd7ab Anthony PERARD
            }
135 209cd7ab Anthony PERARD
            memcpy(&txreq, RING_GET_REQUEST(&netdev->tx_ring, rc), sizeof(txreq));
136 209cd7ab Anthony PERARD
            netdev->tx_ring.req_cons = ++rc;
137 e613b064 aliguori
138 e613b064 aliguori
#if 1
139 209cd7ab Anthony PERARD
            /* should not happen in theory, we don't announce the *
140 209cd7ab Anthony PERARD
             * feature-{sg,gso,whatelse} flags in xenstore (yet?) */
141 209cd7ab Anthony PERARD
            if (txreq.flags & NETTXF_extra_info) {
142 209cd7ab Anthony PERARD
                xen_be_printf(&netdev->xendev, 0, "FIXME: extra info flag\n");
143 209cd7ab Anthony PERARD
                net_tx_error(netdev, &txreq, rc);
144 209cd7ab Anthony PERARD
                continue;
145 209cd7ab Anthony PERARD
            }
146 209cd7ab Anthony PERARD
            if (txreq.flags & NETTXF_more_data) {
147 209cd7ab Anthony PERARD
                xen_be_printf(&netdev->xendev, 0, "FIXME: more data flag\n");
148 209cd7ab Anthony PERARD
                net_tx_error(netdev, &txreq, rc);
149 209cd7ab Anthony PERARD
                continue;
150 209cd7ab Anthony PERARD
            }
151 e613b064 aliguori
#endif
152 e613b064 aliguori
153 209cd7ab Anthony PERARD
            if (txreq.size < 14) {
154 209cd7ab Anthony PERARD
                xen_be_printf(&netdev->xendev, 0, "bad packet size: %d\n", txreq.size);
155 209cd7ab Anthony PERARD
                net_tx_error(netdev, &txreq, rc);
156 209cd7ab Anthony PERARD
                continue;
157 209cd7ab Anthony PERARD
            }
158 209cd7ab Anthony PERARD
159 209cd7ab Anthony PERARD
            if ((txreq.offset + txreq.size) > XC_PAGE_SIZE) {
160 209cd7ab Anthony PERARD
                xen_be_printf(&netdev->xendev, 0, "error: page crossing\n");
161 209cd7ab Anthony PERARD
                net_tx_error(netdev, &txreq, rc);
162 209cd7ab Anthony PERARD
                continue;
163 209cd7ab Anthony PERARD
            }
164 209cd7ab Anthony PERARD
165 209cd7ab Anthony PERARD
            xen_be_printf(&netdev->xendev, 3, "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n",
166 209cd7ab Anthony PERARD
                          txreq.gref, txreq.offset, txreq.size, txreq.flags,
167 209cd7ab Anthony PERARD
                          (txreq.flags & NETTXF_csum_blank)     ? " csum_blank"     : "",
168 209cd7ab Anthony PERARD
                          (txreq.flags & NETTXF_data_validated) ? " data_validated" : "",
169 209cd7ab Anthony PERARD
                          (txreq.flags & NETTXF_more_data)      ? " more_data"      : "",
170 209cd7ab Anthony PERARD
                          (txreq.flags & NETTXF_extra_info)     ? " extra_info"     : "");
171 209cd7ab Anthony PERARD
172 209cd7ab Anthony PERARD
            page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
173 209cd7ab Anthony PERARD
                                           netdev->xendev.dom,
174 209cd7ab Anthony PERARD
                                           txreq.gref, PROT_READ);
175 209cd7ab Anthony PERARD
            if (page == NULL) {
176 209cd7ab Anthony PERARD
                xen_be_printf(&netdev->xendev, 0, "error: tx gref dereference failed (%d)\n",
177 e613b064 aliguori
                              txreq.gref);
178 209cd7ab Anthony PERARD
                net_tx_error(netdev, &txreq, rc);
179 209cd7ab Anthony PERARD
                continue;
180 209cd7ab Anthony PERARD
            }
181 209cd7ab Anthony PERARD
            if (txreq.flags & NETTXF_csum_blank) {
182 e613b064 aliguori
                /* have read-only mapping -> can't fill checksum in-place */
183 209cd7ab Anthony PERARD
                if (!tmpbuf) {
184 7267c094 Anthony Liguori
                    tmpbuf = g_malloc(XC_PAGE_SIZE);
185 209cd7ab Anthony PERARD
                }
186 e613b064 aliguori
                memcpy(tmpbuf, page + txreq.offset, txreq.size);
187 209cd7ab Anthony PERARD
                net_checksum_calculate(tmpbuf, txreq.size);
188 b356f76d Jason Wang
                qemu_send_packet(qemu_get_queue(netdev->nic), tmpbuf,
189 b356f76d Jason Wang
                                 txreq.size);
190 e613b064 aliguori
            } else {
191 b356f76d Jason Wang
                qemu_send_packet(qemu_get_queue(netdev->nic),
192 b356f76d Jason Wang
                                 page + txreq.offset, txreq.size);
193 e613b064 aliguori
            }
194 209cd7ab Anthony PERARD
            xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1);
195 209cd7ab Anthony PERARD
            net_tx_response(netdev, &txreq, NETIF_RSP_OKAY);
196 209cd7ab Anthony PERARD
        }
197 209cd7ab Anthony PERARD
        if (!netdev->tx_work) {
198 209cd7ab Anthony PERARD
            break;
199 209cd7ab Anthony PERARD
        }
200 209cd7ab Anthony PERARD
        netdev->tx_work = 0;
201 e613b064 aliguori
    }
202 7267c094 Anthony Liguori
    g_free(tmpbuf);
203 e613b064 aliguori
}
204 e613b064 aliguori
205 e613b064 aliguori
/* ------------------------------------------------------------- */
206 e613b064 aliguori
207 e613b064 aliguori
static void net_rx_response(struct XenNetDev *netdev,
208 209cd7ab Anthony PERARD
                            netif_rx_request_t *req, int8_t st,
209 209cd7ab Anthony PERARD
                            uint16_t offset, uint16_t size,
210 209cd7ab Anthony PERARD
                            uint16_t flags)
211 e613b064 aliguori
{
212 e613b064 aliguori
    RING_IDX i = netdev->rx_ring.rsp_prod_pvt;
213 e613b064 aliguori
    netif_rx_response_t *resp;
214 e613b064 aliguori
    int notify;
215 e613b064 aliguori
216 e613b064 aliguori
    resp = RING_GET_RESPONSE(&netdev->rx_ring, i);
217 e613b064 aliguori
    resp->offset     = offset;
218 e613b064 aliguori
    resp->flags      = flags;
219 e613b064 aliguori
    resp->id         = req->id;
220 e613b064 aliguori
    resp->status     = (int16_t)size;
221 209cd7ab Anthony PERARD
    if (st < 0) {
222 209cd7ab Anthony PERARD
        resp->status = (int16_t)st;
223 209cd7ab Anthony PERARD
    }
224 e613b064 aliguori
225 e613b064 aliguori
    xen_be_printf(&netdev->xendev, 3, "rx response: idx %d, status %d, flags 0x%x\n",
226 209cd7ab Anthony PERARD
                  i, resp->status, resp->flags);
227 e613b064 aliguori
228 e613b064 aliguori
    netdev->rx_ring.rsp_prod_pvt = ++i;
229 e613b064 aliguori
    RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->rx_ring, notify);
230 209cd7ab Anthony PERARD
    if (notify) {
231 209cd7ab Anthony PERARD
        xen_be_send_notify(&netdev->xendev);
232 209cd7ab Anthony PERARD
    }
233 e613b064 aliguori
}
234 e613b064 aliguori
235 e613b064 aliguori
#define NET_IP_ALIGN 2
236 e613b064 aliguori
237 4e68f7a0 Stefan Hajnoczi
static int net_rx_ok(NetClientState *nc)
238 e613b064 aliguori
{
239 cc1f0f45 Jason Wang
    struct XenNetDev *netdev = qemu_get_nic_opaque(nc);
240 e613b064 aliguori
    RING_IDX rc, rp;
241 e613b064 aliguori
242 209cd7ab Anthony PERARD
    if (netdev->xendev.be_state != XenbusStateConnected) {
243 209cd7ab Anthony PERARD
        return 0;
244 209cd7ab Anthony PERARD
    }
245 e613b064 aliguori
246 e613b064 aliguori
    rc = netdev->rx_ring.req_cons;
247 e613b064 aliguori
    rp = netdev->rx_ring.sring->req_prod;
248 e613b064 aliguori
    xen_rmb();
249 e613b064 aliguori
250 e613b064 aliguori
    if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) {
251 209cd7ab Anthony PERARD
        xen_be_printf(&netdev->xendev, 2, "%s: no rx buffers (%d/%d)\n",
252 209cd7ab Anthony PERARD
                      __FUNCTION__, rc, rp);
253 209cd7ab Anthony PERARD
        return 0;
254 e613b064 aliguori
    }
255 e613b064 aliguori
    return 1;
256 e613b064 aliguori
}
257 e613b064 aliguori
258 4e68f7a0 Stefan Hajnoczi
static ssize_t net_rx_packet(NetClientState *nc, const uint8_t *buf, size_t size)
259 e613b064 aliguori
{
260 cc1f0f45 Jason Wang
    struct XenNetDev *netdev = qemu_get_nic_opaque(nc);
261 e613b064 aliguori
    netif_rx_request_t rxreq;
262 e613b064 aliguori
    RING_IDX rc, rp;
263 e613b064 aliguori
    void *page;
264 e613b064 aliguori
265 209cd7ab Anthony PERARD
    if (netdev->xendev.be_state != XenbusStateConnected) {
266 209cd7ab Anthony PERARD
        return -1;
267 209cd7ab Anthony PERARD
    }
268 e613b064 aliguori
269 e613b064 aliguori
    rc = netdev->rx_ring.req_cons;
270 e613b064 aliguori
    rp = netdev->rx_ring.sring->req_prod;
271 e613b064 aliguori
    xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
272 e613b064 aliguori
273 e613b064 aliguori
    if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) {
274 209cd7ab Anthony PERARD
        xen_be_printf(&netdev->xendev, 2, "no buffer, drop packet\n");
275 209cd7ab Anthony PERARD
        return -1;
276 e613b064 aliguori
    }
277 e613b064 aliguori
    if (size > XC_PAGE_SIZE - NET_IP_ALIGN) {
278 209cd7ab Anthony PERARD
        xen_be_printf(&netdev->xendev, 0, "packet too big (%lu > %ld)",
279 209cd7ab Anthony PERARD
                      (unsigned long)size, XC_PAGE_SIZE - NET_IP_ALIGN);
280 209cd7ab Anthony PERARD
        return -1;
281 e613b064 aliguori
    }
282 e613b064 aliguori
283 e613b064 aliguori
    memcpy(&rxreq, RING_GET_REQUEST(&netdev->rx_ring, rc), sizeof(rxreq));
284 e613b064 aliguori
    netdev->rx_ring.req_cons = ++rc;
285 e613b064 aliguori
286 e613b064 aliguori
    page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
287 209cd7ab Anthony PERARD
                                   netdev->xendev.dom,
288 209cd7ab Anthony PERARD
                                   rxreq.gref, PROT_WRITE);
289 e613b064 aliguori
    if (page == NULL) {
290 209cd7ab Anthony PERARD
        xen_be_printf(&netdev->xendev, 0, "error: rx gref dereference failed (%d)\n",
291 e613b064 aliguori
                      rxreq.gref);
292 209cd7ab Anthony PERARD
        net_rx_response(netdev, &rxreq, NETIF_RSP_ERROR, 0, 0, 0);
293 209cd7ab Anthony PERARD
        return -1;
294 e613b064 aliguori
    }
295 e613b064 aliguori
    memcpy(page + NET_IP_ALIGN, buf, size);
296 e613b064 aliguori
    xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1);
297 e613b064 aliguori
    net_rx_response(netdev, &rxreq, NETIF_RSP_OKAY, NET_IP_ALIGN, size, 0);
298 4f1c942b Mark McLoughlin
299 4f1c942b Mark McLoughlin
    return size;
300 e613b064 aliguori
}
301 e613b064 aliguori
302 e613b064 aliguori
/* ------------------------------------------------------------- */
303 e613b064 aliguori
304 658788c5 Mark McLoughlin
static NetClientInfo net_xen_info = {
305 2be64a68 Laszlo Ersek
    .type = NET_CLIENT_OPTIONS_KIND_NIC,
306 658788c5 Mark McLoughlin
    .size = sizeof(NICState),
307 658788c5 Mark McLoughlin
    .can_receive = net_rx_ok,
308 658788c5 Mark McLoughlin
    .receive = net_rx_packet,
309 658788c5 Mark McLoughlin
};
310 658788c5 Mark McLoughlin
311 e613b064 aliguori
static int net_init(struct XenDevice *xendev)
312 e613b064 aliguori
{
313 e613b064 aliguori
    struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
314 e613b064 aliguori
315 e613b064 aliguori
    /* read xenstore entries */
316 209cd7ab Anthony PERARD
    if (netdev->mac == NULL) {
317 209cd7ab Anthony PERARD
        netdev->mac = xenstore_read_be_str(&netdev->xendev, "mac");
318 209cd7ab Anthony PERARD
    }
319 e613b064 aliguori
320 e613b064 aliguori
    /* do we have all we need? */
321 209cd7ab Anthony PERARD
    if (netdev->mac == NULL) {
322 209cd7ab Anthony PERARD
        return -1;
323 209cd7ab Anthony PERARD
    }
324 e613b064 aliguori
325 209cd7ab Anthony PERARD
    if (net_parse_macaddr(netdev->conf.macaddr.a, netdev->mac) < 0) {
326 658788c5 Mark McLoughlin
        return -1;
327 209cd7ab Anthony PERARD
    }
328 658788c5 Mark McLoughlin
329 658788c5 Mark McLoughlin
    netdev->nic = qemu_new_nic(&net_xen_info, &netdev->conf,
330 658788c5 Mark McLoughlin
                               "xen", NULL, netdev);
331 658788c5 Mark McLoughlin
332 b356f76d Jason Wang
    snprintf(qemu_get_queue(netdev->nic)->info_str,
333 b356f76d Jason Wang
             sizeof(qemu_get_queue(netdev->nic)->info_str),
334 e613b064 aliguori
             "nic: xenbus vif macaddr=%s", netdev->mac);
335 e613b064 aliguori
336 e613b064 aliguori
    /* fill info */
337 e613b064 aliguori
    xenstore_write_be_int(&netdev->xendev, "feature-rx-copy", 1);
338 e613b064 aliguori
    xenstore_write_be_int(&netdev->xendev, "feature-rx-flip", 0);
339 e613b064 aliguori
340 e613b064 aliguori
    return 0;
341 e613b064 aliguori
}
342 e613b064 aliguori
343 e613b064 aliguori
static int net_connect(struct XenDevice *xendev)
344 e613b064 aliguori
{
345 e613b064 aliguori
    struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
346 e613b064 aliguori
    int rx_copy;
347 e613b064 aliguori
348 e613b064 aliguori
    if (xenstore_read_fe_int(&netdev->xendev, "tx-ring-ref",
349 209cd7ab Anthony PERARD
                             &netdev->tx_ring_ref) == -1) {
350 209cd7ab Anthony PERARD
        return -1;
351 209cd7ab Anthony PERARD
    }
352 e613b064 aliguori
    if (xenstore_read_fe_int(&netdev->xendev, "rx-ring-ref",
353 209cd7ab Anthony PERARD
                             &netdev->rx_ring_ref) == -1) {
354 209cd7ab Anthony PERARD
        return 1;
355 209cd7ab Anthony PERARD
    }
356 e613b064 aliguori
    if (xenstore_read_fe_int(&netdev->xendev, "event-channel",
357 209cd7ab Anthony PERARD
                             &netdev->xendev.remote_port) == -1) {
358 209cd7ab Anthony PERARD
        return -1;
359 209cd7ab Anthony PERARD
    }
360 e613b064 aliguori
361 209cd7ab Anthony PERARD
    if (xenstore_read_fe_int(&netdev->xendev, "request-rx-copy", &rx_copy) == -1) {
362 209cd7ab Anthony PERARD
        rx_copy = 0;
363 209cd7ab Anthony PERARD
    }
364 e613b064 aliguori
    if (rx_copy == 0) {
365 209cd7ab Anthony PERARD
        xen_be_printf(&netdev->xendev, 0, "frontend doesn't support rx-copy.\n");
366 209cd7ab Anthony PERARD
        return -1;
367 e613b064 aliguori
    }
368 e613b064 aliguori
369 e613b064 aliguori
    netdev->txs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
370 209cd7ab Anthony PERARD
                                          netdev->xendev.dom,
371 209cd7ab Anthony PERARD
                                          netdev->tx_ring_ref,
372 209cd7ab Anthony PERARD
                                          PROT_READ | PROT_WRITE);
373 e613b064 aliguori
    netdev->rxs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
374 209cd7ab Anthony PERARD
                                          netdev->xendev.dom,
375 209cd7ab Anthony PERARD
                                          netdev->rx_ring_ref,
376 209cd7ab Anthony PERARD
                                          PROT_READ | PROT_WRITE);
377 209cd7ab Anthony PERARD
    if (!netdev->txs || !netdev->rxs) {
378 209cd7ab Anthony PERARD
        return -1;
379 209cd7ab Anthony PERARD
    }
380 e613b064 aliguori
    BACK_RING_INIT(&netdev->tx_ring, netdev->txs, XC_PAGE_SIZE);
381 e613b064 aliguori
    BACK_RING_INIT(&netdev->rx_ring, netdev->rxs, XC_PAGE_SIZE);
382 e613b064 aliguori
383 e613b064 aliguori
    xen_be_bind_evtchn(&netdev->xendev);
384 e613b064 aliguori
385 e613b064 aliguori
    xen_be_printf(&netdev->xendev, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, "
386 209cd7ab Anthony PERARD
                  "remote port %d, local port %d\n",
387 209cd7ab Anthony PERARD
                  netdev->tx_ring_ref, netdev->rx_ring_ref,
388 209cd7ab Anthony PERARD
                  netdev->xendev.remote_port, netdev->xendev.local_port);
389 3e3cabcf Gerd Hoffmann
390 3e3cabcf Gerd Hoffmann
    net_tx_packets(netdev);
391 e613b064 aliguori
    return 0;
392 e613b064 aliguori
}
393 e613b064 aliguori
394 e613b064 aliguori
static void net_disconnect(struct XenDevice *xendev)
395 e613b064 aliguori
{
396 e613b064 aliguori
    struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
397 e613b064 aliguori
398 e613b064 aliguori
    xen_be_unbind_evtchn(&netdev->xendev);
399 e613b064 aliguori
400 e613b064 aliguori
    if (netdev->txs) {
401 209cd7ab Anthony PERARD
        xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->txs, 1);
402 209cd7ab Anthony PERARD
        netdev->txs = NULL;
403 e613b064 aliguori
    }
404 e613b064 aliguori
    if (netdev->rxs) {
405 209cd7ab Anthony PERARD
        xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->rxs, 1);
406 209cd7ab Anthony PERARD
        netdev->rxs = NULL;
407 e613b064 aliguori
    }
408 658788c5 Mark McLoughlin
    if (netdev->nic) {
409 948ecf21 Jason Wang
        qemu_del_nic(netdev->nic);
410 658788c5 Mark McLoughlin
        netdev->nic = NULL;
411 e613b064 aliguori
    }
412 e613b064 aliguori
}
413 e613b064 aliguori
414 e613b064 aliguori
static void net_event(struct XenDevice *xendev)
415 e613b064 aliguori
{
416 e613b064 aliguori
    struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
417 e613b064 aliguori
    net_tx_packets(netdev);
418 b356f76d Jason Wang
    qemu_flush_queued_packets(qemu_get_queue(netdev->nic));
419 e613b064 aliguori
}
420 e613b064 aliguori
421 e613b064 aliguori
static int net_free(struct XenDevice *xendev)
422 e613b064 aliguori
{
423 e613b064 aliguori
    struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
424 e613b064 aliguori
425 7267c094 Anthony Liguori
    g_free(netdev->mac);
426 e613b064 aliguori
    return 0;
427 e613b064 aliguori
}
428 e613b064 aliguori
429 e613b064 aliguori
/* ------------------------------------------------------------- */
430 e613b064 aliguori
431 e613b064 aliguori
struct XenDevOps xen_netdev_ops = {
432 e613b064 aliguori
    .size       = sizeof(struct XenNetDev),
433 e613b064 aliguori
    .flags      = DEVOPS_FLAG_NEED_GNTDEV,
434 e613b064 aliguori
    .init       = net_init,
435 384087b2 John Haxby
    .initialise    = net_connect,
436 e613b064 aliguori
    .event      = net_event,
437 e613b064 aliguori
    .disconnect = net_disconnect,
438 e613b064 aliguori
    .free       = net_free,
439 e613b064 aliguori
};