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