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