]>
Commit | Line | Data |
---|---|---|
e613b064 AL |
1 | /* |
2 | * xen paravirt network card backend | |
3 | * | |
4 | * (c) Gerd Hoffmann <kraxel@redhat.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; under version 2 of the License. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
8167ee88 | 16 | * with this program; if not, see <http://www.gnu.org/licenses/>. |
6b620ca3 PB |
17 | * |
18 | * Contributions after 2012-01-13 are licensed under the terms of the | |
19 | * GNU GPL, version 2 or (at your option) any later version. | |
e613b064 AL |
20 | */ |
21 | ||
22 | #include <stdio.h> | |
23 | #include <stdlib.h> | |
24 | #include <stdarg.h> | |
25 | #include <string.h> | |
26 | #include <unistd.h> | |
27 | #include <signal.h> | |
28 | #include <inttypes.h> | |
29 | #include <fcntl.h> | |
30 | #include <errno.h> | |
e613b064 AL |
31 | #include <sys/socket.h> |
32 | #include <sys/ioctl.h> | |
33 | #include <sys/types.h> | |
34 | #include <sys/stat.h> | |
35 | #include <sys/mman.h> | |
36 | #include <sys/wait.h> | |
e613b064 | 37 | |
e613b064 | 38 | #include "hw.h" |
1422e32d | 39 | #include "net/net.h" |
7200ac3c | 40 | #include "net/checksum.h" |
658788c5 | 41 | #include "net/util.h" |
e613b064 AL |
42 | #include "xen_backend.h" |
43 | ||
b41f6719 AP |
44 | #include <xen/io/netif.h> |
45 | ||
e613b064 AL |
46 | /* ------------------------------------------------------------- */ |
47 | ||
48 | struct XenNetDev { | |
49 | struct XenDevice xendev; /* must be first */ | |
50 | char *mac; | |
51 | int tx_work; | |
52 | int tx_ring_ref; | |
53 | int rx_ring_ref; | |
54 | struct netif_tx_sring *txs; | |
55 | struct netif_rx_sring *rxs; | |
56 | netif_tx_back_ring_t tx_ring; | |
57 | netif_rx_back_ring_t rx_ring; | |
658788c5 MM |
58 | NICConf conf; |
59 | NICState *nic; | |
e613b064 AL |
60 | }; |
61 | ||
62 | /* ------------------------------------------------------------- */ | |
63 | ||
64 | static void net_tx_response(struct XenNetDev *netdev, netif_tx_request_t *txp, int8_t st) | |
65 | { | |
66 | RING_IDX i = netdev->tx_ring.rsp_prod_pvt; | |
67 | netif_tx_response_t *resp; | |
68 | int notify; | |
69 | ||
70 | resp = RING_GET_RESPONSE(&netdev->tx_ring, i); | |
71 | resp->id = txp->id; | |
72 | resp->status = st; | |
73 | ||
74 | #if 0 | |
209cd7ab AP |
75 | if (txp->flags & NETTXF_extra_info) { |
76 | RING_GET_RESPONSE(&netdev->tx_ring, ++i)->status = NETIF_RSP_NULL; | |
77 | } | |
e613b064 AL |
78 | #endif |
79 | ||
80 | netdev->tx_ring.rsp_prod_pvt = ++i; | |
81 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->tx_ring, notify); | |
209cd7ab AP |
82 | if (notify) { |
83 | xen_be_send_notify(&netdev->xendev); | |
84 | } | |
e613b064 AL |
85 | |
86 | if (i == netdev->tx_ring.req_cons) { | |
209cd7ab AP |
87 | int more_to_do; |
88 | RING_FINAL_CHECK_FOR_REQUESTS(&netdev->tx_ring, more_to_do); | |
89 | if (more_to_do) { | |
90 | netdev->tx_work++; | |
91 | } | |
e613b064 AL |
92 | } |
93 | } | |
94 | ||
95 | static void net_tx_error(struct XenNetDev *netdev, netif_tx_request_t *txp, RING_IDX end) | |
96 | { | |
97 | #if 0 | |
98 | /* | |
99 | * Hmm, why netback fails everything in the ring? | |
100 | * Should we do that even when not supporting SG and TSO? | |
101 | */ | |
102 | RING_IDX cons = netdev->tx_ring.req_cons; | |
103 | ||
104 | do { | |
209cd7ab AP |
105 | make_tx_response(netif, txp, NETIF_RSP_ERROR); |
106 | if (cons >= end) { | |
107 | break; | |
108 | } | |
109 | txp = RING_GET_REQUEST(&netdev->tx_ring, cons++); | |
e613b064 AL |
110 | } while (1); |
111 | netdev->tx_ring.req_cons = cons; | |
112 | netif_schedule_work(netif); | |
113 | netif_put(netif); | |
114 | #else | |
115 | net_tx_response(netdev, txp, NETIF_RSP_ERROR); | |
116 | #endif | |
117 | } | |
118 | ||
119 | static void net_tx_packets(struct XenNetDev *netdev) | |
120 | { | |
121 | netif_tx_request_t txreq; | |
122 | RING_IDX rc, rp; | |
123 | void *page; | |
124 | void *tmpbuf = NULL; | |
125 | ||
126 | for (;;) { | |
209cd7ab AP |
127 | rc = netdev->tx_ring.req_cons; |
128 | rp = netdev->tx_ring.sring->req_prod; | |
129 | xen_rmb(); /* Ensure we see queued requests up to 'rp'. */ | |
e613b064 | 130 | |
209cd7ab AP |
131 | while ((rc != rp)) { |
132 | if (RING_REQUEST_CONS_OVERFLOW(&netdev->tx_ring, rc)) { | |
133 | break; | |
134 | } | |
135 | memcpy(&txreq, RING_GET_REQUEST(&netdev->tx_ring, rc), sizeof(txreq)); | |
136 | netdev->tx_ring.req_cons = ++rc; | |
e613b064 AL |
137 | |
138 | #if 1 | |
209cd7ab AP |
139 | /* should not happen in theory, we don't announce the * |
140 | * feature-{sg,gso,whatelse} flags in xenstore (yet?) */ | |
141 | if (txreq.flags & NETTXF_extra_info) { | |
142 | xen_be_printf(&netdev->xendev, 0, "FIXME: extra info flag\n"); | |
143 | net_tx_error(netdev, &txreq, rc); | |
144 | continue; | |
145 | } | |
146 | if (txreq.flags & NETTXF_more_data) { | |
147 | xen_be_printf(&netdev->xendev, 0, "FIXME: more data flag\n"); | |
148 | net_tx_error(netdev, &txreq, rc); | |
149 | continue; | |
150 | } | |
e613b064 AL |
151 | #endif |
152 | ||
209cd7ab AP |
153 | if (txreq.size < 14) { |
154 | xen_be_printf(&netdev->xendev, 0, "bad packet size: %d\n", txreq.size); | |
155 | net_tx_error(netdev, &txreq, rc); | |
156 | continue; | |
157 | } | |
158 | ||
159 | if ((txreq.offset + txreq.size) > XC_PAGE_SIZE) { | |
160 | xen_be_printf(&netdev->xendev, 0, "error: page crossing\n"); | |
161 | net_tx_error(netdev, &txreq, rc); | |
162 | continue; | |
163 | } | |
164 | ||
165 | xen_be_printf(&netdev->xendev, 3, "tx packet ref %d, off %d, len %d, flags 0x%x%s%s%s%s\n", | |
166 | txreq.gref, txreq.offset, txreq.size, txreq.flags, | |
167 | (txreq.flags & NETTXF_csum_blank) ? " csum_blank" : "", | |
168 | (txreq.flags & NETTXF_data_validated) ? " data_validated" : "", | |
169 | (txreq.flags & NETTXF_more_data) ? " more_data" : "", | |
170 | (txreq.flags & NETTXF_extra_info) ? " extra_info" : ""); | |
171 | ||
172 | page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, | |
173 | netdev->xendev.dom, | |
174 | txreq.gref, PROT_READ); | |
175 | if (page == NULL) { | |
176 | xen_be_printf(&netdev->xendev, 0, "error: tx gref dereference failed (%d)\n", | |
e613b064 | 177 | txreq.gref); |
209cd7ab AP |
178 | net_tx_error(netdev, &txreq, rc); |
179 | continue; | |
180 | } | |
181 | if (txreq.flags & NETTXF_csum_blank) { | |
e613b064 | 182 | /* have read-only mapping -> can't fill checksum in-place */ |
209cd7ab | 183 | if (!tmpbuf) { |
7267c094 | 184 | tmpbuf = g_malloc(XC_PAGE_SIZE); |
209cd7ab | 185 | } |
e613b064 | 186 | memcpy(tmpbuf, page + txreq.offset, txreq.size); |
209cd7ab | 187 | net_checksum_calculate(tmpbuf, txreq.size); |
658788c5 | 188 | qemu_send_packet(&netdev->nic->nc, tmpbuf, txreq.size); |
e613b064 | 189 | } else { |
658788c5 | 190 | qemu_send_packet(&netdev->nic->nc, page + txreq.offset, txreq.size); |
e613b064 | 191 | } |
209cd7ab AP |
192 | xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1); |
193 | net_tx_response(netdev, &txreq, NETIF_RSP_OKAY); | |
194 | } | |
195 | if (!netdev->tx_work) { | |
196 | break; | |
197 | } | |
198 | netdev->tx_work = 0; | |
e613b064 | 199 | } |
7267c094 | 200 | g_free(tmpbuf); |
e613b064 AL |
201 | } |
202 | ||
203 | /* ------------------------------------------------------------- */ | |
204 | ||
205 | static void net_rx_response(struct XenNetDev *netdev, | |
209cd7ab AP |
206 | netif_rx_request_t *req, int8_t st, |
207 | uint16_t offset, uint16_t size, | |
208 | uint16_t flags) | |
e613b064 AL |
209 | { |
210 | RING_IDX i = netdev->rx_ring.rsp_prod_pvt; | |
211 | netif_rx_response_t *resp; | |
212 | int notify; | |
213 | ||
214 | resp = RING_GET_RESPONSE(&netdev->rx_ring, i); | |
215 | resp->offset = offset; | |
216 | resp->flags = flags; | |
217 | resp->id = req->id; | |
218 | resp->status = (int16_t)size; | |
209cd7ab AP |
219 | if (st < 0) { |
220 | resp->status = (int16_t)st; | |
221 | } | |
e613b064 AL |
222 | |
223 | xen_be_printf(&netdev->xendev, 3, "rx response: idx %d, status %d, flags 0x%x\n", | |
209cd7ab | 224 | i, resp->status, resp->flags); |
e613b064 AL |
225 | |
226 | netdev->rx_ring.rsp_prod_pvt = ++i; | |
227 | RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&netdev->rx_ring, notify); | |
209cd7ab AP |
228 | if (notify) { |
229 | xen_be_send_notify(&netdev->xendev); | |
230 | } | |
e613b064 AL |
231 | } |
232 | ||
233 | #define NET_IP_ALIGN 2 | |
234 | ||
4e68f7a0 | 235 | static int net_rx_ok(NetClientState *nc) |
e613b064 | 236 | { |
658788c5 | 237 | struct XenNetDev *netdev = DO_UPCAST(NICState, nc, nc)->opaque; |
e613b064 AL |
238 | RING_IDX rc, rp; |
239 | ||
209cd7ab AP |
240 | if (netdev->xendev.be_state != XenbusStateConnected) { |
241 | return 0; | |
242 | } | |
e613b064 AL |
243 | |
244 | rc = netdev->rx_ring.req_cons; | |
245 | rp = netdev->rx_ring.sring->req_prod; | |
246 | xen_rmb(); | |
247 | ||
248 | if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) { | |
209cd7ab AP |
249 | xen_be_printf(&netdev->xendev, 2, "%s: no rx buffers (%d/%d)\n", |
250 | __FUNCTION__, rc, rp); | |
251 | return 0; | |
e613b064 AL |
252 | } |
253 | return 1; | |
254 | } | |
255 | ||
4e68f7a0 | 256 | static ssize_t net_rx_packet(NetClientState *nc, const uint8_t *buf, size_t size) |
e613b064 | 257 | { |
658788c5 | 258 | struct XenNetDev *netdev = DO_UPCAST(NICState, nc, nc)->opaque; |
e613b064 AL |
259 | netif_rx_request_t rxreq; |
260 | RING_IDX rc, rp; | |
261 | void *page; | |
262 | ||
209cd7ab AP |
263 | if (netdev->xendev.be_state != XenbusStateConnected) { |
264 | return -1; | |
265 | } | |
e613b064 AL |
266 | |
267 | rc = netdev->rx_ring.req_cons; | |
268 | rp = netdev->rx_ring.sring->req_prod; | |
269 | xen_rmb(); /* Ensure we see queued requests up to 'rp'. */ | |
270 | ||
271 | if (rc == rp || RING_REQUEST_CONS_OVERFLOW(&netdev->rx_ring, rc)) { | |
209cd7ab AP |
272 | xen_be_printf(&netdev->xendev, 2, "no buffer, drop packet\n"); |
273 | return -1; | |
e613b064 AL |
274 | } |
275 | if (size > XC_PAGE_SIZE - NET_IP_ALIGN) { | |
209cd7ab AP |
276 | xen_be_printf(&netdev->xendev, 0, "packet too big (%lu > %ld)", |
277 | (unsigned long)size, XC_PAGE_SIZE - NET_IP_ALIGN); | |
278 | return -1; | |
e613b064 AL |
279 | } |
280 | ||
281 | memcpy(&rxreq, RING_GET_REQUEST(&netdev->rx_ring, rc), sizeof(rxreq)); | |
282 | netdev->rx_ring.req_cons = ++rc; | |
283 | ||
284 | page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, | |
209cd7ab AP |
285 | netdev->xendev.dom, |
286 | rxreq.gref, PROT_WRITE); | |
e613b064 | 287 | if (page == NULL) { |
209cd7ab | 288 | xen_be_printf(&netdev->xendev, 0, "error: rx gref dereference failed (%d)\n", |
e613b064 | 289 | rxreq.gref); |
209cd7ab AP |
290 | net_rx_response(netdev, &rxreq, NETIF_RSP_ERROR, 0, 0, 0); |
291 | return -1; | |
e613b064 AL |
292 | } |
293 | memcpy(page + NET_IP_ALIGN, buf, size); | |
294 | xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1); | |
295 | net_rx_response(netdev, &rxreq, NETIF_RSP_OKAY, NET_IP_ALIGN, size, 0); | |
4f1c942b MM |
296 | |
297 | return size; | |
e613b064 AL |
298 | } |
299 | ||
300 | /* ------------------------------------------------------------- */ | |
301 | ||
658788c5 | 302 | static NetClientInfo net_xen_info = { |
2be64a68 | 303 | .type = NET_CLIENT_OPTIONS_KIND_NIC, |
658788c5 MM |
304 | .size = sizeof(NICState), |
305 | .can_receive = net_rx_ok, | |
306 | .receive = net_rx_packet, | |
307 | }; | |
308 | ||
e613b064 AL |
309 | static int net_init(struct XenDevice *xendev) |
310 | { | |
311 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); | |
e613b064 AL |
312 | |
313 | /* read xenstore entries */ | |
209cd7ab AP |
314 | if (netdev->mac == NULL) { |
315 | netdev->mac = xenstore_read_be_str(&netdev->xendev, "mac"); | |
316 | } | |
e613b064 AL |
317 | |
318 | /* do we have all we need? */ | |
209cd7ab AP |
319 | if (netdev->mac == NULL) { |
320 | return -1; | |
321 | } | |
e613b064 | 322 | |
209cd7ab | 323 | if (net_parse_macaddr(netdev->conf.macaddr.a, netdev->mac) < 0) { |
658788c5 | 324 | return -1; |
209cd7ab | 325 | } |
658788c5 | 326 | |
658788c5 MM |
327 | netdev->conf.peer = NULL; |
328 | ||
329 | netdev->nic = qemu_new_nic(&net_xen_info, &netdev->conf, | |
330 | "xen", NULL, netdev); | |
331 | ||
332 | snprintf(netdev->nic->nc.info_str, sizeof(netdev->nic->nc.info_str), | |
e613b064 AL |
333 | "nic: xenbus vif macaddr=%s", netdev->mac); |
334 | ||
335 | /* fill info */ | |
336 | xenstore_write_be_int(&netdev->xendev, "feature-rx-copy", 1); | |
337 | xenstore_write_be_int(&netdev->xendev, "feature-rx-flip", 0); | |
338 | ||
339 | return 0; | |
340 | } | |
341 | ||
342 | static int net_connect(struct XenDevice *xendev) | |
343 | { | |
344 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); | |
345 | int rx_copy; | |
346 | ||
347 | if (xenstore_read_fe_int(&netdev->xendev, "tx-ring-ref", | |
209cd7ab AP |
348 | &netdev->tx_ring_ref) == -1) { |
349 | return -1; | |
350 | } | |
e613b064 | 351 | if (xenstore_read_fe_int(&netdev->xendev, "rx-ring-ref", |
209cd7ab AP |
352 | &netdev->rx_ring_ref) == -1) { |
353 | return 1; | |
354 | } | |
e613b064 | 355 | if (xenstore_read_fe_int(&netdev->xendev, "event-channel", |
209cd7ab AP |
356 | &netdev->xendev.remote_port) == -1) { |
357 | return -1; | |
358 | } | |
e613b064 | 359 | |
209cd7ab AP |
360 | if (xenstore_read_fe_int(&netdev->xendev, "request-rx-copy", &rx_copy) == -1) { |
361 | rx_copy = 0; | |
362 | } | |
e613b064 | 363 | if (rx_copy == 0) { |
209cd7ab AP |
364 | xen_be_printf(&netdev->xendev, 0, "frontend doesn't support rx-copy.\n"); |
365 | return -1; | |
e613b064 AL |
366 | } |
367 | ||
368 | netdev->txs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, | |
209cd7ab AP |
369 | netdev->xendev.dom, |
370 | netdev->tx_ring_ref, | |
371 | PROT_READ | PROT_WRITE); | |
e613b064 | 372 | netdev->rxs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev, |
209cd7ab AP |
373 | netdev->xendev.dom, |
374 | netdev->rx_ring_ref, | |
375 | PROT_READ | PROT_WRITE); | |
376 | if (!netdev->txs || !netdev->rxs) { | |
377 | return -1; | |
378 | } | |
e613b064 AL |
379 | BACK_RING_INIT(&netdev->tx_ring, netdev->txs, XC_PAGE_SIZE); |
380 | BACK_RING_INIT(&netdev->rx_ring, netdev->rxs, XC_PAGE_SIZE); | |
381 | ||
382 | xen_be_bind_evtchn(&netdev->xendev); | |
383 | ||
384 | xen_be_printf(&netdev->xendev, 1, "ok: tx-ring-ref %d, rx-ring-ref %d, " | |
209cd7ab AP |
385 | "remote port %d, local port %d\n", |
386 | netdev->tx_ring_ref, netdev->rx_ring_ref, | |
387 | netdev->xendev.remote_port, netdev->xendev.local_port); | |
3e3cabcf GH |
388 | |
389 | net_tx_packets(netdev); | |
e613b064 AL |
390 | return 0; |
391 | } | |
392 | ||
393 | static void net_disconnect(struct XenDevice *xendev) | |
394 | { | |
395 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); | |
396 | ||
397 | xen_be_unbind_evtchn(&netdev->xendev); | |
398 | ||
399 | if (netdev->txs) { | |
209cd7ab AP |
400 | xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->txs, 1); |
401 | netdev->txs = NULL; | |
e613b064 AL |
402 | } |
403 | if (netdev->rxs) { | |
209cd7ab AP |
404 | xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->rxs, 1); |
405 | netdev->rxs = NULL; | |
e613b064 | 406 | } |
658788c5 | 407 | if (netdev->nic) { |
b20c6b9e | 408 | qemu_del_net_client(&netdev->nic->nc); |
658788c5 | 409 | netdev->nic = NULL; |
e613b064 AL |
410 | } |
411 | } | |
412 | ||
413 | static void net_event(struct XenDevice *xendev) | |
414 | { | |
415 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); | |
416 | net_tx_packets(netdev); | |
a98b1402 | 417 | qemu_flush_queued_packets(&netdev->nic->nc); |
e613b064 AL |
418 | } |
419 | ||
420 | static int net_free(struct XenDevice *xendev) | |
421 | { | |
422 | struct XenNetDev *netdev = container_of(xendev, struct XenNetDev, xendev); | |
423 | ||
7267c094 | 424 | g_free(netdev->mac); |
e613b064 AL |
425 | return 0; |
426 | } | |
427 | ||
428 | /* ------------------------------------------------------------- */ | |
429 | ||
430 | struct XenDevOps xen_netdev_ops = { | |
431 | .size = sizeof(struct XenNetDev), | |
432 | .flags = DEVOPS_FLAG_NEED_GNTDEV, | |
433 | .init = net_init, | |
384087b2 | 434 | .initialise = net_connect, |
e613b064 AL |
435 | .event = net_event, |
436 | .disconnect = net_disconnect, | |
437 | .free = net_free, | |
438 | }; |