Statistics
| Branch: | Revision:

root / hw / xen_nic.c @ 3204db98

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

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