root / hw / xen_nic.c @ 9a3e12c8
History | View | Annotate | Download (12 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 | e613b064 | aliguori | * with this program; if not, write to the Free Software Foundation, Inc.,
|
17 | e613b064 | aliguori | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
18 | e613b064 | aliguori | */
|
19 | e613b064 | aliguori | |
20 | e613b064 | aliguori | #include <stdio.h> |
21 | e613b064 | aliguori | #include <stdlib.h> |
22 | e613b064 | aliguori | #include <stdarg.h> |
23 | e613b064 | aliguori | #include <string.h> |
24 | e613b064 | aliguori | #include <unistd.h> |
25 | e613b064 | aliguori | #include <signal.h> |
26 | e613b064 | aliguori | #include <inttypes.h> |
27 | e613b064 | aliguori | #include <fcntl.h> |
28 | e613b064 | aliguori | #include <errno.h> |
29 | e613b064 | aliguori | #include <pthread.h> |
30 | e613b064 | aliguori | #include <sys/socket.h> |
31 | e613b064 | aliguori | #include <sys/ioctl.h> |
32 | e613b064 | aliguori | #include <sys/types.h> |
33 | e613b064 | aliguori | #include <sys/stat.h> |
34 | e613b064 | aliguori | #include <sys/mman.h> |
35 | e613b064 | aliguori | #include <sys/wait.h> |
36 | e613b064 | aliguori | |
37 | e613b064 | aliguori | #include <xs.h> |
38 | e613b064 | aliguori | #include <xenctrl.h> |
39 | e613b064 | aliguori | #include <xen/io/xenbus.h> |
40 | e613b064 | aliguori | #include <xen/io/netif.h> |
41 | e613b064 | aliguori | |
42 | e613b064 | aliguori | #include "hw.h" |
43 | e613b064 | aliguori | #include "net.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 | e613b064 | aliguori | VLANClientState *vs; |
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 | e613b064 | aliguori | if (txp->flags & NETTXF_extra_info)
|
76 | e613b064 | aliguori | RING_GET_RESPONSE(&netdev->tx_ring, ++i)->status = NETIF_RSP_NULL;
|
77 | e613b064 | aliguori | #endif
|
78 | e613b064 | aliguori | |
79 | e613b064 | aliguori | netdev->tx_ring.rsp_prod_pvt = ++i; |
80 | e613b064 | aliguori | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->tx_ring, notify); |
81 | e613b064 | aliguori | if (notify)
|
82 | e613b064 | aliguori | xen_be_send_notify(&netdev->xendev); |
83 | e613b064 | aliguori | |
84 | e613b064 | aliguori | if (i == netdev->tx_ring.req_cons) {
|
85 | e613b064 | aliguori | int more_to_do;
|
86 | e613b064 | aliguori | RING_FINAL_CHECK_FOR_REQUESTS(&netdev->tx_ring, more_to_do); |
87 | e613b064 | aliguori | if (more_to_do)
|
88 | e613b064 | aliguori | netdev->tx_work++; |
89 | e613b064 | aliguori | } |
90 | e613b064 | aliguori | } |
91 | e613b064 | aliguori | |
92 | e613b064 | aliguori | static void net_tx_error(struct XenNetDev *netdev, netif_tx_request_t *txp, RING_IDX end) |
93 | e613b064 | aliguori | { |
94 | e613b064 | aliguori | #if 0
|
95 | e613b064 | aliguori | /*
|
96 | e613b064 | aliguori | * Hmm, why netback fails everything in the ring?
|
97 | e613b064 | aliguori | * Should we do that even when not supporting SG and TSO?
|
98 | e613b064 | aliguori | */
|
99 | e613b064 | aliguori | RING_IDX cons = netdev->tx_ring.req_cons;
|
100 | e613b064 | aliguori | |
101 | e613b064 | aliguori | do {
|
102 | e613b064 | aliguori | make_tx_response(netif, txp, NETIF_RSP_ERROR);
|
103 | e613b064 | aliguori | if (cons >= end)
|
104 | e613b064 | aliguori | break;
|
105 | e613b064 | aliguori | txp = RING_GET_REQUEST(&netdev->tx_ring, cons++);
|
106 | e613b064 | aliguori | } while (1);
|
107 | e613b064 | aliguori | netdev->tx_ring.req_cons = cons;
|
108 | e613b064 | aliguori | netif_schedule_work(netif);
|
109 | e613b064 | aliguori | netif_put(netif);
|
110 | e613b064 | aliguori | #else
|
111 | e613b064 | aliguori | net_tx_response(netdev, txp, NETIF_RSP_ERROR); |
112 | e613b064 | aliguori | #endif
|
113 | e613b064 | aliguori | } |
114 | e613b064 | aliguori | |
115 | e613b064 | aliguori | static void net_tx_packets(struct XenNetDev *netdev) |
116 | e613b064 | aliguori | { |
117 | e613b064 | aliguori | netif_tx_request_t txreq; |
118 | e613b064 | aliguori | RING_IDX rc, rp; |
119 | e613b064 | aliguori | void *page;
|
120 | e613b064 | aliguori | void *tmpbuf = NULL; |
121 | e613b064 | aliguori | |
122 | e613b064 | aliguori | for (;;) {
|
123 | e613b064 | aliguori | rc = netdev->tx_ring.req_cons; |
124 | e613b064 | aliguori | rp = netdev->tx_ring.sring->req_prod; |
125 | e613b064 | aliguori | xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
|
126 | e613b064 | aliguori | |
127 | e613b064 | aliguori | while ((rc != rp)) {
|
128 | e613b064 | aliguori | if (RING_REQUEST_CONS_OVERFLOW(&netdev->tx_ring, rc))
|
129 | e613b064 | aliguori | break;
|
130 | e613b064 | aliguori | memcpy(&txreq, RING_GET_REQUEST(&netdev->tx_ring, rc), sizeof(txreq));
|
131 | e613b064 | aliguori | netdev->tx_ring.req_cons = ++rc; |
132 | e613b064 | aliguori | |
133 | e613b064 | aliguori | #if 1 |
134 | e613b064 | aliguori | /* should not happen in theory, we don't announce the *
|
135 | e613b064 | aliguori | * feature-{sg,gso,whatelse} flags in xenstore (yet?) */
|
136 | e613b064 | aliguori | if (txreq.flags & NETTXF_extra_info) {
|
137 | e613b064 | aliguori | xen_be_printf(&netdev->xendev, 0, "FIXME: extra info flag\n"); |
138 | e613b064 | aliguori | net_tx_error(netdev, &txreq, rc); |
139 | e613b064 | aliguori | continue;
|
140 | e613b064 | aliguori | } |
141 | e613b064 | aliguori | if (txreq.flags & NETTXF_more_data) {
|
142 | e613b064 | aliguori | xen_be_printf(&netdev->xendev, 0, "FIXME: more data flag\n"); |
143 | e613b064 | aliguori | net_tx_error(netdev, &txreq, rc); |
144 | e613b064 | aliguori | continue;
|
145 | e613b064 | aliguori | } |
146 | e613b064 | aliguori | #endif
|
147 | e613b064 | aliguori | |
148 | e613b064 | aliguori | if (txreq.size < 14) { |
149 | e613b064 | aliguori | xen_be_printf(&netdev->xendev, 0, "bad packet size: %d\n", txreq.size); |
150 | e613b064 | aliguori | net_tx_error(netdev, &txreq, rc); |
151 | e613b064 | aliguori | continue;
|
152 | e613b064 | aliguori | } |
153 | e613b064 | aliguori | |
154 | e613b064 | aliguori | if ((txreq.offset + txreq.size) > XC_PAGE_SIZE) {
|
155 | e613b064 | aliguori | xen_be_printf(&netdev->xendev, 0, "error: page crossing\n"); |
156 | e613b064 | aliguori | net_tx_error(netdev, &txreq, rc); |
157 | e613b064 | aliguori | continue;
|
158 | e613b064 | aliguori | } |
159 | e613b064 | aliguori | |
160 | e613b064 | aliguori | xen_be_printf(&netdev->xendev, 3, "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n", |
161 | e613b064 | aliguori | txreq.gref, txreq.offset, txreq.size, txreq.flags, |
162 | e613b064 | aliguori | (txreq.flags & NETTXF_csum_blank) ? " csum_blank" : "", |
163 | e613b064 | aliguori | (txreq.flags & NETTXF_data_validated) ? " data_validated" : "", |
164 | e613b064 | aliguori | (txreq.flags & NETTXF_more_data) ? " more_data" : "", |
165 | e613b064 | aliguori | (txreq.flags & NETTXF_extra_info) ? " extra_info" : ""); |
166 | e613b064 | aliguori | |
167 | e613b064 | aliguori | page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, |
168 | e613b064 | aliguori | netdev->xendev.dom, |
169 | e613b064 | aliguori | txreq.gref, PROT_READ); |
170 | e613b064 | aliguori | if (page == NULL) { |
171 | e613b064 | aliguori | xen_be_printf(&netdev->xendev, 0, "error: tx gref dereference failed (%d)\n", |
172 | e613b064 | aliguori | txreq.gref); |
173 | e613b064 | aliguori | net_tx_error(netdev, &txreq, rc); |
174 | e613b064 | aliguori | continue;
|
175 | e613b064 | aliguori | } |
176 | e613b064 | aliguori | if (txreq.flags & NETTXF_csum_blank) {
|
177 | e613b064 | aliguori | /* have read-only mapping -> can't fill checksum in-place */
|
178 | e613b064 | aliguori | if (!tmpbuf)
|
179 | 682aea0e | Gerd Hoffmann | tmpbuf = qemu_malloc(XC_PAGE_SIZE); |
180 | e613b064 | aliguori | memcpy(tmpbuf, page + txreq.offset, txreq.size); |
181 | e613b064 | aliguori | net_checksum_calculate(tmpbuf, txreq.size); |
182 | e613b064 | aliguori | qemu_send_packet(netdev->vs, tmpbuf, txreq.size); |
183 | e613b064 | aliguori | } else {
|
184 | e613b064 | aliguori | qemu_send_packet(netdev->vs, page + txreq.offset, txreq.size); |
185 | e613b064 | aliguori | } |
186 | e613b064 | aliguori | xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1);
|
187 | e613b064 | aliguori | net_tx_response(netdev, &txreq, NETIF_RSP_OKAY); |
188 | e613b064 | aliguori | } |
189 | e613b064 | aliguori | if (!netdev->tx_work)
|
190 | e613b064 | aliguori | break;
|
191 | e613b064 | aliguori | netdev->tx_work = 0;
|
192 | e613b064 | aliguori | } |
193 | 7105b056 | Gerd Hoffmann | qemu_free(tmpbuf); |
194 | e613b064 | aliguori | } |
195 | e613b064 | aliguori | |
196 | e613b064 | aliguori | /* ------------------------------------------------------------- */
|
197 | e613b064 | aliguori | |
198 | e613b064 | aliguori | static void net_rx_response(struct XenNetDev *netdev, |
199 | e613b064 | aliguori | netif_rx_request_t *req, int8_t st, |
200 | e613b064 | aliguori | uint16_t offset, uint16_t size, |
201 | e613b064 | aliguori | uint16_t flags) |
202 | e613b064 | aliguori | { |
203 | e613b064 | aliguori | RING_IDX i = netdev->rx_ring.rsp_prod_pvt; |
204 | e613b064 | aliguori | netif_rx_response_t *resp; |
205 | e613b064 | aliguori | int notify;
|
206 | e613b064 | aliguori | |
207 | e613b064 | aliguori | resp = RING_GET_RESPONSE(&netdev->rx_ring, i); |
208 | e613b064 | aliguori | resp->offset = offset; |
209 | e613b064 | aliguori | resp->flags = flags; |
210 | e613b064 | aliguori | resp->id = req->id; |
211 | e613b064 | aliguori | resp->status = (int16_t)size; |
212 | e613b064 | aliguori | if (st < 0) |
213 | e613b064 | aliguori | resp->status = (int16_t)st; |
214 | e613b064 | aliguori | |
215 | e613b064 | aliguori | xen_be_printf(&netdev->xendev, 3, "rx response: idx %d, status %d, flags 0x%x\n", |
216 | e613b064 | aliguori | i, resp->status, resp->flags); |
217 | e613b064 | aliguori | |
218 | e613b064 | aliguori | netdev->rx_ring.rsp_prod_pvt = ++i; |
219 | e613b064 | aliguori | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->rx_ring, notify); |
220 | e613b064 | aliguori | if (notify)
|
221 | e613b064 | aliguori | xen_be_send_notify(&netdev->xendev); |
222 | e613b064 | aliguori | } |
223 | e613b064 | aliguori | |
224 | e613b064 | aliguori | #define NET_IP_ALIGN 2 |
225 | e613b064 | aliguori | |
226 | e3f5ec2b | Mark McLoughlin | static int net_rx_ok(VLANClientState *vc) |
227 | e613b064 | aliguori | { |
228 | e3f5ec2b | Mark McLoughlin | struct XenNetDev *netdev = vc->opaque;
|
229 | e613b064 | aliguori | RING_IDX rc, rp; |
230 | e613b064 | aliguori | |
231 | e613b064 | aliguori | if (netdev->xendev.be_state != XenbusStateConnected)
|
232 | e613b064 | aliguori | return 0; |
233 | e613b064 | aliguori | |
234 | e613b064 | aliguori | rc = netdev->rx_ring.req_cons; |
235 | e613b064 | aliguori | rp = netdev->rx_ring.sring->req_prod; |
236 | e613b064 | aliguori | xen_rmb(); |
237 | e613b064 | aliguori | |
238 | e613b064 | aliguori | if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) {
|
239 | e613b064 | aliguori | xen_be_printf(&netdev->xendev, 2, "%s: no rx buffers (%d/%d)\n", |
240 | e613b064 | aliguori | __FUNCTION__, rc, rp); |
241 | e613b064 | aliguori | return 0; |
242 | e613b064 | aliguori | } |
243 | e613b064 | aliguori | return 1; |
244 | e613b064 | aliguori | } |
245 | e613b064 | aliguori | |
246 | 4f1c942b | Mark McLoughlin | static ssize_t net_rx_packet(VLANClientState *vc, const uint8_t *buf, size_t size) |
247 | e613b064 | aliguori | { |
248 | e3f5ec2b | Mark McLoughlin | struct XenNetDev *netdev = vc->opaque;
|
249 | e613b064 | aliguori | netif_rx_request_t rxreq; |
250 | e613b064 | aliguori | RING_IDX rc, rp; |
251 | e613b064 | aliguori | void *page;
|
252 | e613b064 | aliguori | |
253 | e613b064 | aliguori | if (netdev->xendev.be_state != XenbusStateConnected)
|
254 | 4f1c942b | Mark McLoughlin | return -1; |
255 | e613b064 | aliguori | |
256 | e613b064 | aliguori | rc = netdev->rx_ring.req_cons; |
257 | e613b064 | aliguori | rp = netdev->rx_ring.sring->req_prod; |
258 | e613b064 | aliguori | xen_rmb(); /* Ensure we see queued requests up to 'rp'. */
|
259 | e613b064 | aliguori | |
260 | e613b064 | aliguori | if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) {
|
261 | e613b064 | aliguori | xen_be_printf(&netdev->xendev, 2, "no buffer, drop packet\n"); |
262 | 4f1c942b | Mark McLoughlin | return -1; |
263 | e613b064 | aliguori | } |
264 | e613b064 | aliguori | if (size > XC_PAGE_SIZE - NET_IP_ALIGN) {
|
265 | cda9046b | Mark McLoughlin | xen_be_printf(&netdev->xendev, 0, "packet too big (%lu > %ld)", |
266 | cda9046b | Mark McLoughlin | (unsigned long)size, XC_PAGE_SIZE - NET_IP_ALIGN); |
267 | 4f1c942b | Mark McLoughlin | return -1; |
268 | e613b064 | aliguori | } |
269 | e613b064 | aliguori | |
270 | e613b064 | aliguori | memcpy(&rxreq, RING_GET_REQUEST(&netdev->rx_ring, rc), sizeof(rxreq));
|
271 | e613b064 | aliguori | netdev->rx_ring.req_cons = ++rc; |
272 | e613b064 | aliguori | |
273 | e613b064 | aliguori | page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, |
274 | e613b064 | aliguori | netdev->xendev.dom, |
275 | e613b064 | aliguori | rxreq.gref, PROT_WRITE); |
276 | e613b064 | aliguori | if (page == NULL) { |
277 | e613b064 | aliguori | xen_be_printf(&netdev->xendev, 0, "error: rx gref dereference failed (%d)\n", |
278 | e613b064 | aliguori | rxreq.gref); |
279 | e613b064 | aliguori | net_rx_response(netdev, &rxreq, NETIF_RSP_ERROR, 0, 0, 0); |
280 | 4f1c942b | Mark McLoughlin | return -1; |
281 | e613b064 | aliguori | } |
282 | e613b064 | aliguori | memcpy(page + NET_IP_ALIGN, buf, size); |
283 | e613b064 | aliguori | xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1);
|
284 | e613b064 | aliguori | net_rx_response(netdev, &rxreq, NETIF_RSP_OKAY, NET_IP_ALIGN, size, 0);
|
285 | 4f1c942b | Mark McLoughlin | |
286 | 4f1c942b | Mark McLoughlin | return size;
|
287 | e613b064 | aliguori | } |
288 | e613b064 | aliguori | |
289 | e613b064 | aliguori | /* ------------------------------------------------------------- */
|
290 | e613b064 | aliguori | |
291 | e613b064 | aliguori | static int net_init(struct XenDevice *xendev) |
292 | e613b064 | aliguori | { |
293 | e613b064 | aliguori | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
294 | e613b064 | aliguori | VLANState *vlan; |
295 | e613b064 | aliguori | |
296 | e613b064 | aliguori | /* read xenstore entries */
|
297 | e613b064 | aliguori | if (netdev->mac == NULL) |
298 | e613b064 | aliguori | netdev->mac = xenstore_read_be_str(&netdev->xendev, "mac");
|
299 | e613b064 | aliguori | |
300 | e613b064 | aliguori | /* do we have all we need? */
|
301 | e613b064 | aliguori | if (netdev->mac == NULL) |
302 | e613b064 | aliguori | return -1; |
303 | e613b064 | aliguori | |
304 | 1a609520 | Jan Kiszka | vlan = qemu_find_vlan(netdev->xendev.dev, 1);
|
305 | e613b064 | aliguori | netdev->vs = qemu_new_vlan_client(vlan, "xen", NULL, |
306 | 463af534 | Mark McLoughlin | net_rx_ok, net_rx_packet, NULL,
|
307 | 463af534 | Mark McLoughlin | NULL, netdev);
|
308 | e613b064 | aliguori | snprintf(netdev->vs->info_str, sizeof(netdev->vs->info_str),
|
309 | e613b064 | aliguori | "nic: xenbus vif macaddr=%s", netdev->mac);
|
310 | e613b064 | aliguori | |
311 | e613b064 | aliguori | /* fill info */
|
312 | e613b064 | aliguori | xenstore_write_be_int(&netdev->xendev, "feature-rx-copy", 1); |
313 | e613b064 | aliguori | xenstore_write_be_int(&netdev->xendev, "feature-rx-flip", 0); |
314 | e613b064 | aliguori | |
315 | e613b064 | aliguori | return 0; |
316 | e613b064 | aliguori | } |
317 | e613b064 | aliguori | |
318 | e613b064 | aliguori | static int net_connect(struct XenDevice *xendev) |
319 | e613b064 | aliguori | { |
320 | e613b064 | aliguori | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
321 | e613b064 | aliguori | int rx_copy;
|
322 | e613b064 | aliguori | |
323 | e613b064 | aliguori | if (xenstore_read_fe_int(&netdev->xendev, "tx-ring-ref", |
324 | e613b064 | aliguori | &netdev->tx_ring_ref) == -1)
|
325 | e613b064 | aliguori | return -1; |
326 | e613b064 | aliguori | if (xenstore_read_fe_int(&netdev->xendev, "rx-ring-ref", |
327 | e613b064 | aliguori | &netdev->rx_ring_ref) == -1)
|
328 | e613b064 | aliguori | return 1; |
329 | e613b064 | aliguori | if (xenstore_read_fe_int(&netdev->xendev, "event-channel", |
330 | e613b064 | aliguori | &netdev->xendev.remote_port) == -1)
|
331 | e613b064 | aliguori | return -1; |
332 | e613b064 | aliguori | |
333 | e613b064 | aliguori | if (xenstore_read_fe_int(&netdev->xendev, "request-rx-copy", &rx_copy) == -1) |
334 | e613b064 | aliguori | rx_copy = 0;
|
335 | e613b064 | aliguori | if (rx_copy == 0) { |
336 | e613b064 | aliguori | xen_be_printf(&netdev->xendev, 0, "frontend doesn't support rx-copy.\n"); |
337 | e613b064 | aliguori | return -1; |
338 | e613b064 | aliguori | } |
339 | e613b064 | aliguori | |
340 | e613b064 | aliguori | netdev->txs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, |
341 | e613b064 | aliguori | netdev->xendev.dom, |
342 | e613b064 | aliguori | netdev->tx_ring_ref, |
343 | e613b064 | aliguori | PROT_READ | PROT_WRITE); |
344 | e613b064 | aliguori | netdev->rxs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, |
345 | e613b064 | aliguori | netdev->xendev.dom, |
346 | e613b064 | aliguori | netdev->rx_ring_ref, |
347 | e613b064 | aliguori | PROT_READ | PROT_WRITE); |
348 | e613b064 | aliguori | if (!netdev->txs || !netdev->rxs)
|
349 | e613b064 | aliguori | return -1; |
350 | e613b064 | aliguori | BACK_RING_INIT(&netdev->tx_ring, netdev->txs, XC_PAGE_SIZE); |
351 | e613b064 | aliguori | BACK_RING_INIT(&netdev->rx_ring, netdev->rxs, XC_PAGE_SIZE); |
352 | e613b064 | aliguori | |
353 | e613b064 | aliguori | xen_be_bind_evtchn(&netdev->xendev); |
354 | e613b064 | aliguori | |
355 | e613b064 | aliguori | xen_be_printf(&netdev->xendev, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, " |
356 | e613b064 | aliguori | "remote port %d, local port %d\n",
|
357 | e613b064 | aliguori | netdev->tx_ring_ref, netdev->rx_ring_ref, |
358 | e613b064 | aliguori | netdev->xendev.remote_port, netdev->xendev.local_port); |
359 | 3e3cabcf | Gerd Hoffmann | |
360 | 3e3cabcf | Gerd Hoffmann | net_tx_packets(netdev); |
361 | e613b064 | aliguori | return 0; |
362 | e613b064 | aliguori | } |
363 | e613b064 | aliguori | |
364 | e613b064 | aliguori | static void net_disconnect(struct XenDevice *xendev) |
365 | e613b064 | aliguori | { |
366 | e613b064 | aliguori | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
367 | e613b064 | aliguori | |
368 | e613b064 | aliguori | xen_be_unbind_evtchn(&netdev->xendev); |
369 | e613b064 | aliguori | |
370 | e613b064 | aliguori | if (netdev->txs) {
|
371 | e613b064 | aliguori | xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->txs, 1);
|
372 | e613b064 | aliguori | netdev->txs = NULL;
|
373 | e613b064 | aliguori | } |
374 | e613b064 | aliguori | if (netdev->rxs) {
|
375 | e613b064 | aliguori | xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->rxs, 1);
|
376 | e613b064 | aliguori | netdev->rxs = NULL;
|
377 | e613b064 | aliguori | } |
378 | e613b064 | aliguori | if (netdev->vs) {
|
379 | e613b064 | aliguori | qemu_del_vlan_client(netdev->vs); |
380 | e613b064 | aliguori | netdev->vs = NULL;
|
381 | e613b064 | aliguori | } |
382 | e613b064 | aliguori | } |
383 | e613b064 | aliguori | |
384 | e613b064 | aliguori | static void net_event(struct XenDevice *xendev) |
385 | e613b064 | aliguori | { |
386 | e613b064 | aliguori | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
387 | e613b064 | aliguori | net_tx_packets(netdev); |
388 | e613b064 | aliguori | } |
389 | e613b064 | aliguori | |
390 | e613b064 | aliguori | static int net_free(struct XenDevice *xendev) |
391 | e613b064 | aliguori | { |
392 | e613b064 | aliguori | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); |
393 | e613b064 | aliguori | |
394 | e613b064 | aliguori | qemu_free(netdev->mac); |
395 | e613b064 | aliguori | return 0; |
396 | e613b064 | aliguori | } |
397 | e613b064 | aliguori | |
398 | e613b064 | aliguori | /* ------------------------------------------------------------- */
|
399 | e613b064 | aliguori | |
400 | e613b064 | aliguori | struct XenDevOps xen_netdev_ops = {
|
401 | e613b064 | aliguori | .size = sizeof(struct XenNetDev), |
402 | e613b064 | aliguori | .flags = DEVOPS_FLAG_NEED_GNTDEV, |
403 | e613b064 | aliguori | .init = net_init, |
404 | e613b064 | aliguori | .connect = net_connect, |
405 | e613b064 | aliguori | .event = net_event, |
406 | e613b064 | aliguori | .disconnect = net_disconnect, |
407 | e613b064 | aliguori | .free = net_free, |
408 | e613b064 | aliguori | }; |