Statistics
| Branch: | Revision:

root / hw / xen_nic.c @ 5414dec6

History | View | Annotate | Download (13.9 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 <xs.h>
39 e613b064 aliguori
#include <xenctrl.h>
40 e613b064 aliguori
#include <xen/io/xenbus.h>
41 e613b064 aliguori
#include <xen/io/netif.h>
42 e613b064 aliguori
43 e613b064 aliguori
#include "hw.h"
44 e613b064 aliguori
#include "net.h"
45 7200ac3c Mark McLoughlin
#include "net/checksum.h"
46 658788c5 Mark McLoughlin
#include "net/util.h"
47 e613b064 aliguori
#include "qemu-char.h"
48 e613b064 aliguori
#include "xen_backend.h"
49 e613b064 aliguori
50 e613b064 aliguori
/* ------------------------------------------------------------- */
51 e613b064 aliguori
52 e613b064 aliguori
struct XenNetDev {
53 e613b064 aliguori
    struct XenDevice      xendev;  /* must be first */
54 e613b064 aliguori
    char                  *mac;
55 e613b064 aliguori
    int                   tx_work;
56 e613b064 aliguori
    int                   tx_ring_ref;
57 e613b064 aliguori
    int                   rx_ring_ref;
58 e613b064 aliguori
    struct netif_tx_sring *txs;
59 e613b064 aliguori
    struct netif_rx_sring *rxs;
60 e613b064 aliguori
    netif_tx_back_ring_t  tx_ring;
61 e613b064 aliguori
    netif_rx_back_ring_t  rx_ring;
62 658788c5 Mark McLoughlin
    NICConf               conf;
63 658788c5 Mark McLoughlin
    NICState              *nic;
64 e613b064 aliguori
};
65 e613b064 aliguori
66 e613b064 aliguori
/* ------------------------------------------------------------- */
67 e613b064 aliguori
68 e613b064 aliguori
static void net_tx_response(struct XenNetDev *netdev, netif_tx_request_t *txp, int8_t st)
69 e613b064 aliguori
{
70 e613b064 aliguori
    RING_IDX i = netdev->tx_ring.rsp_prod_pvt;
71 e613b064 aliguori
    netif_tx_response_t *resp;
72 e613b064 aliguori
    int notify;
73 e613b064 aliguori
74 e613b064 aliguori
    resp = RING_GET_RESPONSE(&netdev->tx_ring, i);
75 e613b064 aliguori
    resp->id     = txp->id;
76 e613b064 aliguori
    resp->status = st;
77 e613b064 aliguori
78 e613b064 aliguori
#if 0
79 209cd7ab Anthony PERARD
    if (txp->flags & NETTXF_extra_info) {
80 209cd7ab Anthony PERARD
        RING_GET_RESPONSE(&netdev->tx_ring, ++i)->status = NETIF_RSP_NULL;
81 209cd7ab Anthony PERARD
    }
82 e613b064 aliguori
#endif
83 e613b064 aliguori
84 e613b064 aliguori
    netdev->tx_ring.rsp_prod_pvt = ++i;
85 e613b064 aliguori
    RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->tx_ring, notify);
86 209cd7ab Anthony PERARD
    if (notify) {
87 209cd7ab Anthony PERARD
        xen_be_send_notify(&netdev->xendev);
88 209cd7ab Anthony PERARD
    }
89 e613b064 aliguori
90 e613b064 aliguori
    if (i == netdev->tx_ring.req_cons) {
91 209cd7ab Anthony PERARD
        int more_to_do;
92 209cd7ab Anthony PERARD
        RING_FINAL_CHECK_FOR_REQUESTS(&netdev->tx_ring, more_to_do);
93 209cd7ab Anthony PERARD
        if (more_to_do) {
94 209cd7ab Anthony PERARD
            netdev->tx_work++;
95 209cd7ab Anthony PERARD
        }
96 e613b064 aliguori
    }
97 e613b064 aliguori
}
98 e613b064 aliguori
99 e613b064 aliguori
static void net_tx_error(struct XenNetDev *netdev, netif_tx_request_t *txp, RING_IDX end)
100 e613b064 aliguori
{
101 e613b064 aliguori
#if 0
102 e613b064 aliguori
    /*
103 e613b064 aliguori
     * Hmm, why netback fails everything in the ring?
104 e613b064 aliguori
     * Should we do that even when not supporting SG and TSO?
105 e613b064 aliguori
     */
106 e613b064 aliguori
    RING_IDX cons = netdev->tx_ring.req_cons;
107 e613b064 aliguori

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