ia64/xen-unstable

view extras/mini-os/netfront.c @ 17275:7fc9767f966a

minios: Automatically set IP from XenStore information

Signed-off-by: Samuel Thibault <samuel.thibault@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Mar 19 16:20:14 2008 +0000 (2008-03-19)
parents ea5ee63548e4
children d02490be30f5
line source
1 /* Minimal network driver for Mini-OS.
2 * Copyright (c) 2006-2007 Jacob Gorm Hansen, University of Copenhagen.
3 * Based on netfront.c from Xen Linux.
4 *
5 * Does not handle fragments or extras.
6 */
8 #include <os.h>
9 #include <xenbus.h>
10 #include <events.h>
11 #include <errno.h>
12 #include <xen/io/netif.h>
13 #include <gnttab.h>
14 #include <xmalloc.h>
15 #include <time.h>
16 #include <netfront.h>
17 #include <lib.h>
18 #include <semaphore.h>
20 DECLARE_WAIT_QUEUE_HEAD(netfront_queue);
22 #ifdef HAVE_LIBC
23 #define NETIF_SELECT_RX ((void*)-1)
24 #endif
28 #define NET_TX_RING_SIZE __RING_SIZE((struct netif_tx_sring *)0, PAGE_SIZE)
29 #define NET_RX_RING_SIZE __RING_SIZE((struct netif_rx_sring *)0, PAGE_SIZE)
30 #define GRANT_INVALID_REF 0
33 struct net_buffer {
34 void* page;
35 grant_ref_t gref;
36 };
38 struct netfront_dev {
39 domid_t dom;
41 unsigned short tx_freelist[NET_TX_RING_SIZE];
42 struct semaphore tx_sem;
44 struct net_buffer rx_buffers[NET_RX_RING_SIZE];
45 struct net_buffer tx_buffers[NET_TX_RING_SIZE];
47 struct netif_tx_front_ring tx;
48 struct netif_rx_front_ring rx;
49 grant_ref_t tx_ring_ref;
50 grant_ref_t rx_ring_ref;
51 evtchn_port_t evtchn;
53 char *nodename;
54 char *backend;
56 #ifdef HAVE_LIBC
57 int fd;
58 unsigned char *data;
59 size_t len;
60 size_t rlen;
61 #endif
63 void (*netif_rx)(unsigned char* data, int len);
64 };
66 void init_rx_buffers(struct netfront_dev *dev);
68 static inline void add_id_to_freelist(unsigned int id,unsigned short* freelist)
69 {
70 freelist[id] = freelist[0];
71 freelist[0] = id;
72 }
74 static inline unsigned short get_id_from_freelist(unsigned short* freelist)
75 {
76 unsigned int id = freelist[0];
77 freelist[0] = freelist[id];
78 return id;
79 }
81 __attribute__((weak)) void netif_rx(unsigned char* data,int len)
82 {
83 printk("%d bytes incoming at %p\n",len,data);
84 }
86 __attribute__((weak)) void net_app_main(void*si,unsigned char*mac) {}
88 static inline int xennet_rxidx(RING_IDX idx)
89 {
90 return idx & (NET_RX_RING_SIZE - 1);
91 }
93 void network_rx(struct netfront_dev *dev)
94 {
95 RING_IDX rp,cons;
96 struct netif_rx_response *rx;
99 moretodo:
100 rp = dev->rx.sring->rsp_prod;
101 rmb(); /* Ensure we see queued responses up to 'rp'. */
102 cons = dev->rx.rsp_cons;
104 int nr_consumed=0;
105 int some = 0;
106 while ((cons != rp) && !some)
107 {
108 struct net_buffer* buf;
109 unsigned char* page;
111 rx = RING_GET_RESPONSE(&dev->rx, cons);
113 if (rx->flags & NETRXF_extra_info)
114 {
115 printk("+++++++++++++++++++++ we have extras!\n");
116 continue;
117 }
120 if (rx->status == NETIF_RSP_NULL) continue;
122 int id = rx->id;
124 buf = &dev->rx_buffers[id];
125 page = (unsigned char*)buf->page;
126 gnttab_end_access(buf->gref);
128 if(rx->status>0)
129 {
130 #ifdef HAVE_LIBC
131 if (dev->netif_rx == NETIF_SELECT_RX) {
132 int len = rx->status;
133 ASSERT(current == main_thread);
134 if (len > dev->len)
135 len = dev->len;
136 memcpy(dev->data, page+rx->offset, len);
137 dev->rlen = len;
138 some = 1;
139 } else
140 #endif
141 dev->netif_rx(page+rx->offset,rx->status);
142 }
144 nr_consumed++;
146 ++cons;
147 }
148 dev->rx.rsp_cons=cons;
150 int more;
151 RING_FINAL_CHECK_FOR_RESPONSES(&dev->rx,more);
152 if(more && !some) goto moretodo;
154 RING_IDX req_prod = dev->rx.req_prod_pvt;
156 int i;
157 netif_rx_request_t *req;
159 for(i=0; i<nr_consumed; i++)
160 {
161 int id = xennet_rxidx(req_prod + i);
162 req = RING_GET_REQUEST(&dev->rx, req_prod + i);
163 struct net_buffer* buf = &dev->rx_buffers[id];
164 void* page = buf->page;
166 /* We are sure to have free gnttab entries since they got released above */
167 buf->gref = req->gref =
168 gnttab_grant_access(dev->dom,virt_to_mfn(page),0);
170 req->id = id;
171 }
173 wmb();
175 dev->rx.req_prod_pvt = req_prod + i;
177 int notify;
178 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&dev->rx, notify);
179 if (notify)
180 notify_remote_via_evtchn(dev->evtchn);
182 }
184 void network_tx_buf_gc(struct netfront_dev *dev)
185 {
188 RING_IDX cons, prod;
189 unsigned short id;
191 do {
192 prod = dev->tx.sring->rsp_prod;
193 rmb(); /* Ensure we see responses up to 'rp'. */
195 for (cons = dev->tx.rsp_cons; cons != prod; cons++)
196 {
197 struct netif_tx_response *txrsp;
199 txrsp = RING_GET_RESPONSE(&dev->tx, cons);
200 if (txrsp->status == NETIF_RSP_NULL)
201 continue;
203 if (txrsp->status == NETIF_RSP_ERROR)
204 printk("packet error\n");
206 id = txrsp->id;
207 struct net_buffer* buf = &dev->tx_buffers[id];
208 gnttab_end_access(buf->gref);
209 buf->gref=GRANT_INVALID_REF;
211 add_id_to_freelist(id,dev->tx_freelist);
212 up(&dev->tx_sem);
213 }
215 dev->tx.rsp_cons = prod;
217 /*
218 * Set a new event, then check for race with update of tx_cons.
219 * Note that it is essential to schedule a callback, no matter
220 * how few tx_buffers are pending. Even if there is space in the
221 * transmit ring, higher layers may be blocked because too much
222 * data is outstanding: in such cases notification from Xen is
223 * likely to be the only kick that we'll get.
224 */
225 dev->tx.sring->rsp_event =
226 prod + ((dev->tx.sring->req_prod - prod) >> 1) + 1;
227 mb();
228 } while ((cons == prod) && (prod != dev->tx.sring->rsp_prod));
231 }
233 void netfront_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
234 {
235 int flags;
236 struct netfront_dev *dev = data;
238 local_irq_save(flags);
240 network_tx_buf_gc(dev);
241 network_rx(dev);
243 local_irq_restore(flags);
244 }
246 #ifdef HAVE_LIBC
247 void netfront_select_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
248 {
249 int flags;
250 struct netfront_dev *dev = data;
251 int fd = dev->fd;
253 local_irq_save(flags);
254 network_tx_buf_gc(dev);
255 local_irq_restore(flags);
257 files[fd].read = 1;
258 wake_up(&netfront_queue);
259 }
260 #endif
262 struct netfront_dev *init_netfront(char *nodename, void (*thenetif_rx)(unsigned char* data, int len), unsigned char rawmac[6], char **ip)
263 {
264 xenbus_transaction_t xbt;
265 char* err;
266 char* message=NULL;
267 struct netif_tx_sring *txs;
268 struct netif_rx_sring *rxs;
269 int retry=0;
270 int i;
271 char* mac;
272 char* msg;
274 struct netfront_dev *dev;
276 if (!nodename)
277 nodename = "device/vif/0";
279 char path[strlen(nodename) + 1 + 10 + 1];
281 if (!thenetif_rx)
282 thenetif_rx = netif_rx;
284 printk("************************ NETFRONT for %s **********\n\n\n", nodename);
286 dev = malloc(sizeof(*dev));
287 dev->nodename = strdup(nodename);
289 printk("net TX ring size %d\n", NET_TX_RING_SIZE);
290 printk("net RX ring size %d\n", NET_RX_RING_SIZE);
291 init_SEMAPHORE(&dev->tx_sem, NET_TX_RING_SIZE);
292 for(i=0;i<NET_TX_RING_SIZE;i++)
293 {
294 add_id_to_freelist(i,dev->tx_freelist);
295 dev->tx_buffers[i].page = NULL;
296 }
298 for(i=0;i<NET_RX_RING_SIZE;i++)
299 {
300 /* TODO: that's a lot of memory */
301 dev->rx_buffers[i].page = (char*)alloc_page();
302 }
304 snprintf(path, sizeof(path), "%s/backend-id", nodename);
305 dev->dom = xenbus_read_integer(path);
306 #ifdef HAVE_LIBC
307 if (thenetif_rx == NETIF_SELECT_RX)
308 evtchn_alloc_unbound(dev->dom, netfront_select_handler, dev, &dev->evtchn);
309 else
310 #endif
311 evtchn_alloc_unbound(dev->dom, netfront_handler, dev, &dev->evtchn);
313 txs = (struct netif_tx_sring*) alloc_page();
314 rxs = (struct netif_rx_sring *) alloc_page();
315 memset(txs,0,PAGE_SIZE);
316 memset(rxs,0,PAGE_SIZE);
319 SHARED_RING_INIT(txs);
320 SHARED_RING_INIT(rxs);
321 FRONT_RING_INIT(&dev->tx, txs, PAGE_SIZE);
322 FRONT_RING_INIT(&dev->rx, rxs, PAGE_SIZE);
324 dev->tx_ring_ref = gnttab_grant_access(dev->dom,virt_to_mfn(txs),0);
325 dev->rx_ring_ref = gnttab_grant_access(dev->dom,virt_to_mfn(rxs),0);
327 dev->netif_rx = thenetif_rx;
329 // FIXME: proper frees on failures
330 again:
331 err = xenbus_transaction_start(&xbt);
332 if (err) {
333 printk("starting transaction\n");
334 }
336 err = xenbus_printf(xbt, nodename, "tx-ring-ref","%u",
337 dev->tx_ring_ref);
338 if (err) {
339 message = "writing tx ring-ref";
340 goto abort_transaction;
341 }
342 err = xenbus_printf(xbt, nodename, "rx-ring-ref","%u",
343 dev->rx_ring_ref);
344 if (err) {
345 message = "writing rx ring-ref";
346 goto abort_transaction;
347 }
348 err = xenbus_printf(xbt, nodename,
349 "event-channel", "%u", dev->evtchn);
350 if (err) {
351 message = "writing event-channel";
352 goto abort_transaction;
353 }
355 err = xenbus_printf(xbt, nodename, "request-rx-copy", "%u", 1);
357 if (err) {
358 message = "writing request-rx-copy";
359 goto abort_transaction;
360 }
362 err = xenbus_printf(xbt, nodename, "state", "%u",
363 4); /* connected */
366 err = xenbus_transaction_end(xbt, 0, &retry);
367 if (retry) {
368 goto again;
369 printk("completing transaction\n");
370 }
372 goto done;
374 abort_transaction:
375 xenbus_transaction_end(xbt, 1, &retry);
376 return NULL;
378 done:
380 snprintf(path, sizeof(path), "%s/backend", nodename);
381 msg = xenbus_read(XBT_NIL, path, &dev->backend);
382 snprintf(path, sizeof(path), "%s/mac", nodename);
383 msg = xenbus_read(XBT_NIL, path, &mac);
385 if ((dev->backend == NULL) || (mac == NULL)) {
386 struct evtchn_close op = { dev->evtchn };
387 printk("%s: backend/mac failed\n", __func__);
388 unbind_evtchn(dev->evtchn);
389 HYPERVISOR_event_channel_op(EVTCHNOP_close, &op);
390 return NULL;
391 }
393 printk("backend at %s\n",dev->backend);
394 printk("mac is %s\n",mac);
396 {
397 char path[strlen(dev->backend) + 1 + 5 + 1];
398 snprintf(path, sizeof(path), "%s/state", dev->backend);
400 xenbus_watch_path(XBT_NIL, path);
402 xenbus_wait_for_value(path,"4");
404 xenbus_unwatch_path(XBT_NIL, path);
406 if (ip) {
407 snprintf(path, sizeof(path), "%s/ip", dev->backend);
408 xenbus_read(XBT_NIL, path, ip);
409 }
410 }
412 printk("**************************\n");
414 init_rx_buffers(dev);
415 unmask_evtchn(dev->evtchn);
417 /* Special conversion specifier 'hh' needed for __ia64__. Without
418 this mini-os panics with 'Unaligned reference'. */
419 if (rawmac)
420 sscanf(mac,"%hhx:%hhx:%hhx:%hhx:%hhx:%hhx",
421 &rawmac[0],
422 &rawmac[1],
423 &rawmac[2],
424 &rawmac[3],
425 &rawmac[4],
426 &rawmac[5]);
428 return dev;
429 }
431 #ifdef HAVE_LIBC
432 int netfront_tap_open(char *nodename) {
433 struct netfront_dev *dev;
435 dev = init_netfront(nodename, NETIF_SELECT_RX, NULL, NULL);
436 if (!dev) {
437 printk("TAP open failed\n");
438 errno = EIO;
439 return -1;
440 }
441 dev->fd = alloc_fd(FTYPE_TAP);
442 printk("tap_open(%s) -> %d\n", nodename, dev->fd);
443 files[dev->fd].tap.dev = dev;
444 return dev->fd;
445 }
446 #endif
448 void shutdown_netfront(struct netfront_dev *dev)
449 {
450 char* err;
451 char *nodename = dev->nodename;
453 char path[strlen(dev->backend) + 1 + 5 + 1];
455 printk("close network: backend at %s\n",dev->backend);
457 snprintf(path, sizeof(path), "%s/state", dev->backend);
458 err = xenbus_printf(XBT_NIL, nodename, "state", "%u", 5); /* closing */
459 xenbus_wait_for_value(path,"5");
461 err = xenbus_printf(XBT_NIL, nodename, "state", "%u", 6);
462 xenbus_wait_for_value(path,"6");
464 unbind_evtchn(dev->evtchn);
466 free(nodename);
467 free(dev->backend);
468 free(dev);
469 }
472 void init_rx_buffers(struct netfront_dev *dev)
473 {
474 int i, requeue_idx;
475 netif_rx_request_t *req;
476 int notify;
478 /* Rebuild the RX buffer freelist and the RX ring itself. */
479 for (requeue_idx = 0, i = 0; i < NET_RX_RING_SIZE; i++)
480 {
481 struct net_buffer* buf = &dev->rx_buffers[requeue_idx];
482 req = RING_GET_REQUEST(&dev->rx, requeue_idx);
484 buf->gref = req->gref =
485 gnttab_grant_access(dev->dom,virt_to_mfn(buf->page),0);
487 req->id = requeue_idx;
489 requeue_idx++;
490 }
492 dev->rx.req_prod_pvt = requeue_idx;
494 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&dev->rx, notify);
496 if (notify)
497 notify_remote_via_evtchn(dev->evtchn);
499 dev->rx.sring->rsp_event = dev->rx.rsp_cons + 1;
500 }
503 void netfront_xmit(struct netfront_dev *dev, unsigned char* data,int len)
504 {
505 int flags;
506 struct netif_tx_request *tx;
507 RING_IDX i;
508 int notify;
509 unsigned short id;
510 struct net_buffer* buf;
511 void* page;
513 down(&dev->tx_sem);
515 local_irq_save(flags);
516 id = get_id_from_freelist(dev->tx_freelist);
517 local_irq_restore(flags);
519 buf = &dev->tx_buffers[id];
520 page = buf->page;
521 if (!page)
522 page = buf->page = (char*) alloc_page();
524 i = dev->tx.req_prod_pvt;
525 tx = RING_GET_REQUEST(&dev->tx, i);
527 memcpy(page,data,len);
529 buf->gref =
530 tx->gref = gnttab_grant_access(dev->dom,virt_to_mfn(page),1);
532 tx->offset=0;
533 tx->size = len;
534 tx->flags=0;
535 tx->id = id;
536 dev->tx.req_prod_pvt = i + 1;
538 wmb();
540 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&dev->tx, notify);
542 if(notify) notify_remote_via_evtchn(dev->evtchn);
544 local_irq_save(flags);
545 network_tx_buf_gc(dev);
546 local_irq_restore(flags);
547 }
549 #ifdef HAVE_LIBC
550 ssize_t netfront_receive(struct netfront_dev *dev, unsigned char *data, size_t len)
551 {
552 unsigned long flags;
553 int fd = dev->fd;
554 ASSERT(current == main_thread);
556 dev->rlen = 0;
557 dev->data = data;
558 dev->len = len;
560 local_irq_save(flags);
561 network_rx(dev);
562 if (!dev->rlen)
563 /* No data for us, make select stop returning */
564 files[fd].read = 0;
565 /* Before re-enabling the interrupts, in case a packet just arrived in the
566 * meanwhile. */
567 local_irq_restore(flags);
569 dev->data = NULL;
570 dev->len = 0;
572 return dev->rlen;
573 }
574 #endif