Statistics
| Branch: | Revision:

root / hw / xen_nic.c @ 1129714f

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

106 e613b064 aliguori
    do {
107 209cd7ab Anthony PERARD
        make_tx_response(netif, txp, NETIF_RSP_ERROR);
108 209cd7ab Anthony PERARD
        if (cons >= end) {
109 209cd7ab Anthony PERARD
            break;
110 209cd7ab Anthony PERARD
        }
111 209cd7ab Anthony PERARD
        txp = RING_GET_REQUEST(&netdev->tx_ring, cons++);
112 e613b064 aliguori
    } while (1);
113 e613b064 aliguori
    netdev->tx_ring.req_cons = cons;
114 e613b064 aliguori
    netif_schedule_work(netif);
115 e613b064 aliguori
    netif_put(netif);
116 e613b064 aliguori
#else
117 e613b064 aliguori
    net_tx_response(netdev, txp, NETIF_RSP_ERROR);
118 e613b064 aliguori
#endif
119 e613b064 aliguori
}
120 e613b064 aliguori
121 e613b064 aliguori
static void net_tx_packets(struct XenNetDev *netdev)
122 e613b064 aliguori
{
123 e613b064 aliguori
    netif_tx_request_t txreq;
124 e613b064 aliguori
    RING_IDX rc, rp;
125 e613b064 aliguori
    void *page;
126 e613b064 aliguori
    void *tmpbuf = NULL;
127 e613b064 aliguori
128 e613b064 aliguori
    for (;;) {
129 209cd7ab Anthony PERARD
        rc = netdev->tx_ring.req_cons;
130 209cd7ab Anthony PERARD
        rp = netdev->tx_ring.sring->req_prod;
131 209cd7ab Anthony PERARD
        xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
132 e613b064 aliguori
133 209cd7ab Anthony PERARD
        while ((rc != rp)) {
134 209cd7ab Anthony PERARD
            if (RING_REQUEST_CONS_OVERFLOW(&netdev->tx_ring, rc)) {
135 209cd7ab Anthony PERARD
                break;
136 209cd7ab Anthony PERARD
            }
137 209cd7ab Anthony PERARD
            memcpy(&txreq, RING_GET_REQUEST(&netdev->tx_ring, rc), sizeof(txreq));
138 209cd7ab Anthony PERARD
            netdev->tx_ring.req_cons = ++rc;
139 e613b064 aliguori
140 e613b064 aliguori
#if 1
141 209cd7ab Anthony PERARD
            /* should not happen in theory, we don't announce the *
142 209cd7ab Anthony PERARD
             * feature-{sg,gso,whatelse} flags in xenstore (yet?) */
143 209cd7ab Anthony PERARD
            if (txreq.flags & NETTXF_extra_info) {
144 209cd7ab Anthony PERARD
                xen_be_printf(&netdev->xendev, 0, "FIXME: extra info flag\n");
145 209cd7ab Anthony PERARD
                net_tx_error(netdev, &txreq, rc);
146 209cd7ab Anthony PERARD
                continue;
147 209cd7ab Anthony PERARD
            }
148 209cd7ab Anthony PERARD
            if (txreq.flags & NETTXF_more_data) {
149 209cd7ab Anthony PERARD
                xen_be_printf(&netdev->xendev, 0, "FIXME: more data flag\n");
150 209cd7ab Anthony PERARD
                net_tx_error(netdev, &txreq, rc);
151 209cd7ab Anthony PERARD
                continue;
152 209cd7ab Anthony PERARD
            }
153 e613b064 aliguori
#endif
154 e613b064 aliguori
155 209cd7ab Anthony PERARD
            if (txreq.size < 14) {
156 209cd7ab Anthony PERARD
                xen_be_printf(&netdev->xendev, 0, "bad packet size: %d\n", txreq.size);
157 209cd7ab Anthony PERARD
                net_tx_error(netdev, &txreq, rc);
158 209cd7ab Anthony PERARD
                continue;
159 209cd7ab Anthony PERARD
            }
160 209cd7ab Anthony PERARD
161 209cd7ab Anthony PERARD
            if ((txreq.offset + txreq.size) > XC_PAGE_SIZE) {
162 209cd7ab Anthony PERARD
                xen_be_printf(&netdev->xendev, 0, "error: page crossing\n");
163 209cd7ab Anthony PERARD
                net_tx_error(netdev, &txreq, rc);
164 209cd7ab Anthony PERARD
                continue;
165 209cd7ab Anthony PERARD
            }
166 209cd7ab Anthony PERARD
167 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",
168 209cd7ab Anthony PERARD
                          txreq.gref, txreq.offset, txreq.size, txreq.flags,
169 209cd7ab Anthony PERARD
                          (txreq.flags & NETTXF_csum_blank)     ? " csum_blank"     : "",
170 209cd7ab Anthony PERARD
                          (txreq.flags & NETTXF_data_validated) ? " data_validated" : "",
171 209cd7ab Anthony PERARD
                          (txreq.flags & NETTXF_more_data)      ? " more_data"      : "",
172 209cd7ab Anthony PERARD
                          (txreq.flags & NETTXF_extra_info)     ? " extra_info"     : "");
173 209cd7ab Anthony PERARD
174 209cd7ab Anthony PERARD
            page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
175 209cd7ab Anthony PERARD
                                           netdev->xendev.dom,
176 209cd7ab Anthony PERARD
                                           txreq.gref, PROT_READ);
177 209cd7ab Anthony PERARD
            if (page == NULL) {
178 209cd7ab Anthony PERARD
                xen_be_printf(&netdev->xendev, 0, "error: tx gref dereference failed (%d)\n",
179 e613b064 aliguori
                              txreq.gref);
180 209cd7ab Anthony PERARD
                net_tx_error(netdev, &txreq, rc);
181 209cd7ab Anthony PERARD
                continue;
182 209cd7ab Anthony PERARD
            }
183 209cd7ab Anthony PERARD
            if (txreq.flags & NETTXF_csum_blank) {
184 e613b064 aliguori
                /* have read-only mapping -> can't fill checksum in-place */
185 209cd7ab Anthony PERARD
                if (!tmpbuf) {
186 682aea0e Gerd Hoffmann
                    tmpbuf = qemu_malloc(XC_PAGE_SIZE);
187 209cd7ab Anthony PERARD
                }
188 e613b064 aliguori
                memcpy(tmpbuf, page + txreq.offset, txreq.size);
189 209cd7ab Anthony PERARD
                net_checksum_calculate(tmpbuf, txreq.size);
190 658788c5 Mark McLoughlin
                qemu_send_packet(&netdev->nic->nc, tmpbuf, txreq.size);
191 e613b064 aliguori
            } else {
192 658788c5 Mark McLoughlin
                qemu_send_packet(&netdev->nic->nc, 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 7105b056 Gerd Hoffmann
    qemu_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 658788c5 Mark McLoughlin
static int net_rx_ok(VLANClientState *nc)
238 e613b064 aliguori
{
239 658788c5 Mark McLoughlin
    struct XenNetDev *netdev = DO_UPCAST(NICState, nc, nc)->opaque;
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 658788c5 Mark McLoughlin
static ssize_t net_rx_packet(VLANClientState *nc, const uint8_t *buf, size_t size)
259 e613b064 aliguori
{
260 658788c5 Mark McLoughlin
    struct XenNetDev *netdev = DO_UPCAST(NICState, nc, nc)->opaque;
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 658788c5 Mark McLoughlin
    .type = NET_CLIENT_TYPE_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->conf.vlan = qemu_find_vlan(netdev->xendev.dev, 1);
330 658788c5 Mark McLoughlin
    netdev->conf.peer = NULL;
331 658788c5 Mark McLoughlin
332 658788c5 Mark McLoughlin
    netdev->nic = qemu_new_nic(&net_xen_info, &netdev->conf,
333 658788c5 Mark McLoughlin
                               "xen", NULL, netdev);
334 658788c5 Mark McLoughlin
335 658788c5 Mark McLoughlin
    snprintf(netdev->nic->nc.info_str, sizeof(netdev->nic->nc.info_str),
336 e613b064 aliguori
             "nic: xenbus vif macaddr=%s", netdev->mac);
337 e613b064 aliguori
338 e613b064 aliguori
    /* fill info */
339 e613b064 aliguori
    xenstore_write_be_int(&netdev->xendev, "feature-rx-copy", 1);
340 e613b064 aliguori
    xenstore_write_be_int(&netdev->xendev, "feature-rx-flip", 0);
341 e613b064 aliguori
342 e613b064 aliguori
    return 0;
343 e613b064 aliguori
}
344 e613b064 aliguori
345 e613b064 aliguori
static int net_connect(struct XenDevice *xendev)
346 e613b064 aliguori
{
347 e613b064 aliguori
    struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
348 e613b064 aliguori
    int rx_copy;
349 e613b064 aliguori
350 e613b064 aliguori
    if (xenstore_read_fe_int(&netdev->xendev, "tx-ring-ref",
351 209cd7ab Anthony PERARD
                             &netdev->tx_ring_ref) == -1) {
352 209cd7ab Anthony PERARD
        return -1;
353 209cd7ab Anthony PERARD
    }
354 e613b064 aliguori
    if (xenstore_read_fe_int(&netdev->xendev, "rx-ring-ref",
355 209cd7ab Anthony PERARD
                             &netdev->rx_ring_ref) == -1) {
356 209cd7ab Anthony PERARD
        return 1;
357 209cd7ab Anthony PERARD
    }
358 e613b064 aliguori
    if (xenstore_read_fe_int(&netdev->xendev, "event-channel",
359 209cd7ab Anthony PERARD
                             &netdev->xendev.remote_port) == -1) {
360 209cd7ab Anthony PERARD
        return -1;
361 209cd7ab Anthony PERARD
    }
362 e613b064 aliguori
363 209cd7ab Anthony PERARD
    if (xenstore_read_fe_int(&netdev->xendev, "request-rx-copy", &rx_copy) == -1) {
364 209cd7ab Anthony PERARD
        rx_copy = 0;
365 209cd7ab Anthony PERARD
    }
366 e613b064 aliguori
    if (rx_copy == 0) {
367 209cd7ab Anthony PERARD
        xen_be_printf(&netdev->xendev, 0, "frontend doesn't support rx-copy.\n");
368 209cd7ab Anthony PERARD
        return -1;
369 e613b064 aliguori
    }
370 e613b064 aliguori
371 e613b064 aliguori
    netdev->txs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
372 209cd7ab Anthony PERARD
                                          netdev->xendev.dom,
373 209cd7ab Anthony PERARD
                                          netdev->tx_ring_ref,
374 209cd7ab Anthony PERARD
                                          PROT_READ | PROT_WRITE);
375 e613b064 aliguori
    netdev->rxs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
376 209cd7ab Anthony PERARD
                                          netdev->xendev.dom,
377 209cd7ab Anthony PERARD
                                          netdev->rx_ring_ref,
378 209cd7ab Anthony PERARD
                                          PROT_READ | PROT_WRITE);
379 209cd7ab Anthony PERARD
    if (!netdev->txs || !netdev->rxs) {
380 209cd7ab Anthony PERARD
        return -1;
381 209cd7ab Anthony PERARD
    }
382 e613b064 aliguori
    BACK_RING_INIT(&netdev->tx_ring, netdev->txs, XC_PAGE_SIZE);
383 e613b064 aliguori
    BACK_RING_INIT(&netdev->rx_ring, netdev->rxs, XC_PAGE_SIZE);
384 e613b064 aliguori
385 e613b064 aliguori
    xen_be_bind_evtchn(&netdev->xendev);
386 e613b064 aliguori
387 e613b064 aliguori
    xen_be_printf(&netdev->xendev, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, "
388 209cd7ab Anthony PERARD
                  "remote port %d, local port %d\n",
389 209cd7ab Anthony PERARD
                  netdev->tx_ring_ref, netdev->rx_ring_ref,
390 209cd7ab Anthony PERARD
                  netdev->xendev.remote_port, netdev->xendev.local_port);
391 3e3cabcf Gerd Hoffmann
392 3e3cabcf Gerd Hoffmann
    net_tx_packets(netdev);
393 e613b064 aliguori
    return 0;
394 e613b064 aliguori
}
395 e613b064 aliguori
396 e613b064 aliguori
static void net_disconnect(struct XenDevice *xendev)
397 e613b064 aliguori
{
398 e613b064 aliguori
    struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
399 e613b064 aliguori
400 e613b064 aliguori
    xen_be_unbind_evtchn(&netdev->xendev);
401 e613b064 aliguori
402 e613b064 aliguori
    if (netdev->txs) {
403 209cd7ab Anthony PERARD
        xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->txs, 1);
404 209cd7ab Anthony PERARD
        netdev->txs = NULL;
405 e613b064 aliguori
    }
406 e613b064 aliguori
    if (netdev->rxs) {
407 209cd7ab Anthony PERARD
        xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->rxs, 1);
408 209cd7ab Anthony PERARD
        netdev->rxs = NULL;
409 e613b064 aliguori
    }
410 658788c5 Mark McLoughlin
    if (netdev->nic) {
411 658788c5 Mark McLoughlin
        qemu_del_vlan_client(&netdev->nic->nc);
412 658788c5 Mark McLoughlin
        netdev->nic = NULL;
413 e613b064 aliguori
    }
414 e613b064 aliguori
}
415 e613b064 aliguori
416 e613b064 aliguori
static void net_event(struct XenDevice *xendev)
417 e613b064 aliguori
{
418 e613b064 aliguori
    struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
419 e613b064 aliguori
    net_tx_packets(netdev);
420 e613b064 aliguori
}
421 e613b064 aliguori
422 e613b064 aliguori
static int net_free(struct XenDevice *xendev)
423 e613b064 aliguori
{
424 e613b064 aliguori
    struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev);
425 e613b064 aliguori
426 e613b064 aliguori
    qemu_free(netdev->mac);
427 e613b064 aliguori
    return 0;
428 e613b064 aliguori
}
429 e613b064 aliguori
430 e613b064 aliguori
/* ------------------------------------------------------------- */
431 e613b064 aliguori
432 e613b064 aliguori
struct XenDevOps xen_netdev_ops = {
433 e613b064 aliguori
    .size       = sizeof(struct XenNetDev),
434 e613b064 aliguori
    .flags      = DEVOPS_FLAG_NEED_GNTDEV,
435 e613b064 aliguori
    .init       = net_init,
436 e613b064 aliguori
    .connect    = net_connect,
437 e613b064 aliguori
    .event      = net_event,
438 e613b064 aliguori
    .disconnect = net_disconnect,
439 e613b064 aliguori
    .free       = net_free,
440 e613b064 aliguori
};