ia64/xen-unstable

view linux-2.6-xen-sparse/drivers/xen/netfront/netfront.c @ 10040:91c77df11b43

When we copy packet sin netback/netfront make sure the new skb has
all the necessary fields initialised. In particular, before we were
not copying ip_summed and that screws up checksum offload.

Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Wed May 10 17:30:42 2006 +0100 (2006-05-10)
parents 8f0f24dae963
children 21bd82ade5cc
line source
1 /******************************************************************************
2 * Virtual network driver for conversing with remote driver backends.
3 *
4 * Copyright (c) 2002-2005, K A Fraser
5 * Copyright (c) 2005, XenSource Ltd
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation; or, when distributed
10 * separately from the Linux kernel or incorporated into other
11 * software packages, subject to the following license:
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a copy
14 * of this source file (the "Software"), to deal in the Software without
15 * restriction, including without limitation the rights to use, copy, modify,
16 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
17 * and to permit persons to whom the Software is furnished to do so, subject to
18 * the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included in
21 * all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
29 * IN THE SOFTWARE.
30 */
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/version.h>
35 #include <linux/kernel.h>
36 #include <linux/sched.h>
37 #include <linux/slab.h>
38 #include <linux/string.h>
39 #include <linux/errno.h>
40 #include <linux/netdevice.h>
41 #include <linux/inetdevice.h>
42 #include <linux/etherdevice.h>
43 #include <linux/skbuff.h>
44 #include <linux/init.h>
45 #include <linux/bitops.h>
46 #include <linux/proc_fs.h>
47 #include <linux/ethtool.h>
48 #include <linux/in.h>
49 #include <net/sock.h>
50 #include <net/pkt_sched.h>
51 #include <net/arp.h>
52 #include <net/route.h>
53 #include <asm/io.h>
54 #include <asm/uaccess.h>
55 #include <xen/evtchn.h>
56 #include <xen/xenbus.h>
57 #include <xen/interface/io/netif.h>
58 #include <xen/interface/memory.h>
59 #include <xen/balloon.h>
60 #include <asm/page.h>
61 #include <asm/uaccess.h>
62 #include <xen/interface/grant_table.h>
63 #include <xen/gnttab.h>
64 #include <xen/net_driver_util.h>
66 #define GRANT_INVALID_REF 0
68 #define NET_TX_RING_SIZE __RING_SIZE((netif_tx_sring_t *)0, PAGE_SIZE)
69 #define NET_RX_RING_SIZE __RING_SIZE((netif_rx_sring_t *)0, PAGE_SIZE)
71 static inline void init_skb_shinfo(struct sk_buff *skb)
72 {
73 atomic_set(&(skb_shinfo(skb)->dataref), 1);
74 skb_shinfo(skb)->nr_frags = 0;
75 skb_shinfo(skb)->frag_list = NULL;
76 }
78 struct netfront_info
79 {
80 struct list_head list;
81 struct net_device *netdev;
83 struct net_device_stats stats;
84 unsigned int tx_full;
86 netif_tx_front_ring_t tx;
87 netif_rx_front_ring_t rx;
89 spinlock_t tx_lock;
90 spinlock_t rx_lock;
92 unsigned int handle;
93 unsigned int evtchn, irq;
95 /* What is the status of our connection to the remote backend? */
96 #define BEST_CLOSED 0
97 #define BEST_DISCONNECTED 1
98 #define BEST_CONNECTED 2
99 unsigned int backend_state;
101 /* Is this interface open or closed (down or up)? */
102 #define UST_CLOSED 0
103 #define UST_OPEN 1
104 unsigned int user_state;
106 /* Receive-ring batched refills. */
107 #define RX_MIN_TARGET 8
108 #define RX_DFL_MIN_TARGET 64
109 #define RX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
110 int rx_min_target, rx_max_target, rx_target;
111 struct sk_buff_head rx_batch;
113 struct timer_list rx_refill_timer;
115 /*
116 * {tx,rx}_skbs store outstanding skbuffs. The first entry in each
117 * array is an index into a chain of free entries.
118 */
119 struct sk_buff *tx_skbs[NET_TX_RING_SIZE+1];
120 struct sk_buff *rx_skbs[NET_RX_RING_SIZE+1];
122 #define TX_MAX_TARGET min_t(int, NET_RX_RING_SIZE, 256)
123 grant_ref_t gref_tx_head;
124 grant_ref_t grant_tx_ref[NET_TX_RING_SIZE + 1];
125 grant_ref_t gref_rx_head;
126 grant_ref_t grant_rx_ref[NET_TX_RING_SIZE + 1];
128 struct xenbus_device *xbdev;
129 int tx_ring_ref;
130 int rx_ring_ref;
131 u8 mac[ETH_ALEN];
133 unsigned long rx_pfn_array[NET_RX_RING_SIZE];
134 multicall_entry_t rx_mcl[NET_RX_RING_SIZE+1];
135 mmu_update_t rx_mmu[NET_RX_RING_SIZE];
136 };
138 /*
139 * Access macros for acquiring freeing slots in {tx,rx}_skbs[].
140 */
142 static inline void add_id_to_freelist(struct sk_buff **list, unsigned short id)
143 {
144 list[id] = list[0];
145 list[0] = (void *)(unsigned long)id;
146 }
148 static inline unsigned short get_id_from_freelist(struct sk_buff **list)
149 {
150 unsigned int id = (unsigned int)(unsigned long)list[0];
151 list[0] = list[id];
152 return id;
153 }
155 #ifdef DEBUG
156 static char *be_state_name[] = {
157 [BEST_CLOSED] = "closed",
158 [BEST_DISCONNECTED] = "disconnected",
159 [BEST_CONNECTED] = "connected",
160 };
161 #endif
163 #define DPRINTK(fmt, args...) pr_debug("netfront (%s:%d) " fmt, \
164 __FUNCTION__, __LINE__, ##args)
165 #define IPRINTK(fmt, args...) \
166 printk(KERN_INFO "netfront: " fmt, ##args)
167 #define WPRINTK(fmt, args...) \
168 printk(KERN_WARNING "netfront: " fmt, ##args)
171 static int talk_to_backend(struct xenbus_device *, struct netfront_info *);
172 static int setup_device(struct xenbus_device *, struct netfront_info *);
173 static int create_netdev(int, struct xenbus_device *, struct net_device **);
175 static void netfront_closing(struct xenbus_device *);
177 static void end_access(int, void *);
178 static void netif_disconnect_backend(struct netfront_info *);
179 static void close_netdev(struct netfront_info *);
180 static void netif_free(struct netfront_info *);
182 static void show_device(struct netfront_info *);
184 static void network_connect(struct net_device *);
185 static void network_tx_buf_gc(struct net_device *);
186 static void network_alloc_rx_buffers(struct net_device *);
187 static int send_fake_arp(struct net_device *);
189 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs);
191 #ifdef CONFIG_PROC_FS
192 static int xennet_proc_init(void);
193 static int xennet_proc_addif(struct net_device *dev);
194 static void xennet_proc_delif(struct net_device *dev);
195 #else
196 #define xennet_proc_init() (0)
197 #define xennet_proc_addif(d) (0)
198 #define xennet_proc_delif(d) ((void)0)
199 #endif
202 /**
203 * Entry point to this code when a new device is created. Allocate the basic
204 * structures and the ring buffers for communication with the backend, and
205 * inform the backend of the appropriate details for those. Switch to
206 * Connected state.
207 */
208 static int netfront_probe(struct xenbus_device *dev,
209 const struct xenbus_device_id *id)
210 {
211 int err;
212 struct net_device *netdev;
213 struct netfront_info *info;
214 unsigned int handle;
216 err = xenbus_scanf(XBT_NULL, dev->nodename, "handle", "%u", &handle);
217 if (err != 1) {
218 xenbus_dev_fatal(dev, err, "reading handle");
219 return err;
220 }
222 err = create_netdev(handle, dev, &netdev);
223 if (err) {
224 xenbus_dev_fatal(dev, err, "creating netdev");
225 return err;
226 }
228 info = netdev_priv(netdev);
229 dev->data = info;
231 err = talk_to_backend(dev, info);
232 if (err) {
233 kfree(info);
234 dev->data = NULL;
235 return err;
236 }
238 return 0;
239 }
242 /**
243 * We are reconnecting to the backend, due to a suspend/resume, or a backend
244 * driver restart. We tear down our netif structure and recreate it, but
245 * leave the device-layer structures intact so that this is transparent to the
246 * rest of the kernel.
247 */
248 static int netfront_resume(struct xenbus_device *dev)
249 {
250 struct netfront_info *info = dev->data;
252 DPRINTK("%s\n", dev->nodename);
254 netif_disconnect_backend(info);
255 return talk_to_backend(dev, info);
256 }
259 /* Common code used when first setting up, and when resuming. */
260 static int talk_to_backend(struct xenbus_device *dev,
261 struct netfront_info *info)
262 {
263 const char *message;
264 xenbus_transaction_t xbt;
265 int err;
267 err = xen_net_read_mac(dev, info->mac);
268 if (err) {
269 xenbus_dev_fatal(dev, err, "parsing %s/mac", dev->nodename);
270 goto out;
271 }
273 /* Create shared ring, alloc event channel. */
274 err = setup_device(dev, info);
275 if (err)
276 goto out;
278 again:
279 err = xenbus_transaction_start(&xbt);
280 if (err) {
281 xenbus_dev_fatal(dev, err, "starting transaction");
282 goto destroy_ring;
283 }
285 err = xenbus_printf(xbt, dev->nodename, "tx-ring-ref","%u",
286 info->tx_ring_ref);
287 if (err) {
288 message = "writing tx ring-ref";
289 goto abort_transaction;
290 }
291 err = xenbus_printf(xbt, dev->nodename, "rx-ring-ref","%u",
292 info->rx_ring_ref);
293 if (err) {
294 message = "writing rx ring-ref";
295 goto abort_transaction;
296 }
297 err = xenbus_printf(xbt, dev->nodename,
298 "event-channel", "%u", info->evtchn);
299 if (err) {
300 message = "writing event-channel";
301 goto abort_transaction;
302 }
304 err = xenbus_transaction_end(xbt, 0);
305 if (err) {
306 if (err == -EAGAIN)
307 goto again;
308 xenbus_dev_fatal(dev, err, "completing transaction");
309 goto destroy_ring;
310 }
312 xenbus_switch_state(dev, XenbusStateConnected);
314 return 0;
316 abort_transaction:
317 xenbus_transaction_end(xbt, 1);
318 xenbus_dev_fatal(dev, err, "%s", message);
319 destroy_ring:
320 netif_free(info);
321 out:
322 return err;
323 }
326 static int setup_device(struct xenbus_device *dev, struct netfront_info *info)
327 {
328 netif_tx_sring_t *txs;
329 netif_rx_sring_t *rxs;
330 int err;
331 struct net_device *netdev = info->netdev;
333 info->tx_ring_ref = GRANT_INVALID_REF;
334 info->rx_ring_ref = GRANT_INVALID_REF;
335 info->rx.sring = NULL;
336 info->tx.sring = NULL;
337 info->irq = 0;
339 txs = (netif_tx_sring_t *)__get_free_page(GFP_KERNEL);
340 if (!txs) {
341 err = -ENOMEM;
342 xenbus_dev_fatal(dev, err, "allocating tx ring page");
343 goto fail;
344 }
345 rxs = (netif_rx_sring_t *)__get_free_page(GFP_KERNEL);
346 if (!rxs) {
347 err = -ENOMEM;
348 xenbus_dev_fatal(dev, err, "allocating rx ring page");
349 goto fail;
350 }
351 memset(txs, 0, PAGE_SIZE);
352 memset(rxs, 0, PAGE_SIZE);
353 info->backend_state = BEST_DISCONNECTED;
355 SHARED_RING_INIT(txs);
356 FRONT_RING_INIT(&info->tx, txs, PAGE_SIZE);
358 SHARED_RING_INIT(rxs);
359 FRONT_RING_INIT(&info->rx, rxs, PAGE_SIZE);
361 err = xenbus_grant_ring(dev, virt_to_mfn(txs));
362 if (err < 0)
363 goto fail;
364 info->tx_ring_ref = err;
366 err = xenbus_grant_ring(dev, virt_to_mfn(rxs));
367 if (err < 0)
368 goto fail;
369 info->rx_ring_ref = err;
371 err = xenbus_alloc_evtchn(dev, &info->evtchn);
372 if (err)
373 goto fail;
375 memcpy(netdev->dev_addr, info->mac, ETH_ALEN);
376 network_connect(netdev);
377 info->irq = bind_evtchn_to_irqhandler(
378 info->evtchn, netif_int, SA_SAMPLE_RANDOM, netdev->name,
379 netdev);
380 (void)send_fake_arp(netdev);
381 show_device(info);
383 return 0;
385 fail:
386 netif_free(info);
387 return err;
388 }
391 /**
392 * Callback received when the backend's state changes.
393 */
394 static void backend_changed(struct xenbus_device *dev,
395 XenbusState backend_state)
396 {
397 DPRINTK("\n");
399 switch (backend_state) {
400 case XenbusStateInitialising:
401 case XenbusStateInitWait:
402 case XenbusStateInitialised:
403 case XenbusStateConnected:
404 case XenbusStateUnknown:
405 case XenbusStateClosed:
406 break;
408 case XenbusStateClosing:
409 netfront_closing(dev);
410 break;
411 }
412 }
415 /** Send a packet on a net device to encourage switches to learn the
416 * MAC. We send a fake ARP request.
417 *
418 * @param dev device
419 * @return 0 on success, error code otherwise
420 */
421 static int send_fake_arp(struct net_device *dev)
422 {
423 struct sk_buff *skb;
424 u32 src_ip, dst_ip;
426 dst_ip = INADDR_BROADCAST;
427 src_ip = inet_select_addr(dev, dst_ip, RT_SCOPE_LINK);
429 /* No IP? Then nothing to do. */
430 if (src_ip == 0)
431 return 0;
433 skb = arp_create(ARPOP_REPLY, ETH_P_ARP,
434 dst_ip, dev, src_ip,
435 /*dst_hw*/ NULL, /*src_hw*/ NULL,
436 /*target_hw*/ dev->dev_addr);
437 if (skb == NULL)
438 return -ENOMEM;
440 return dev_queue_xmit(skb);
441 }
444 static int network_open(struct net_device *dev)
445 {
446 struct netfront_info *np = netdev_priv(dev);
448 memset(&np->stats, 0, sizeof(np->stats));
450 np->user_state = UST_OPEN;
452 network_alloc_rx_buffers(dev);
453 np->rx.sring->rsp_event = np->rx.rsp_cons + 1;
455 netif_start_queue(dev);
457 return 0;
458 }
460 static void network_tx_buf_gc(struct net_device *dev)
461 {
462 RING_IDX i, prod;
463 unsigned short id;
464 struct netfront_info *np = netdev_priv(dev);
465 struct sk_buff *skb;
467 if (np->backend_state != BEST_CONNECTED)
468 return;
470 do {
471 prod = np->tx.sring->rsp_prod;
472 rmb(); /* Ensure we see responses up to 'rp'. */
474 for (i = np->tx.rsp_cons; i != prod; i++) {
475 id = RING_GET_RESPONSE(&np->tx, i)->id;
476 skb = np->tx_skbs[id];
477 if (unlikely(gnttab_query_foreign_access(
478 np->grant_tx_ref[id]) != 0)) {
479 printk(KERN_ALERT "network_tx_buf_gc: warning "
480 "-- grant still in use by backend "
481 "domain.\n");
482 goto out;
483 }
484 gnttab_end_foreign_access_ref(
485 np->grant_tx_ref[id], GNTMAP_readonly);
486 gnttab_release_grant_reference(
487 &np->gref_tx_head, np->grant_tx_ref[id]);
488 np->grant_tx_ref[id] = GRANT_INVALID_REF;
489 add_id_to_freelist(np->tx_skbs, id);
490 dev_kfree_skb_irq(skb);
491 }
493 np->tx.rsp_cons = prod;
495 /*
496 * Set a new event, then check for race with update of tx_cons.
497 * Note that it is essential to schedule a callback, no matter
498 * how few buffers are pending. Even if there is space in the
499 * transmit ring, higher layers may be blocked because too much
500 * data is outstanding: in such cases notification from Xen is
501 * likely to be the only kick that we'll get.
502 */
503 np->tx.sring->rsp_event =
504 prod + ((np->tx.sring->req_prod - prod) >> 1) + 1;
505 mb();
506 } while (prod != np->tx.sring->rsp_prod);
508 out:
509 if ((np->tx_full) &&
510 ((np->tx.sring->req_prod - prod) < NET_TX_RING_SIZE) &&
511 !gnttab_empty_grant_references(&np->gref_tx_head)) {
512 np->tx_full = 0;
513 if (np->user_state == UST_OPEN)
514 netif_wake_queue(dev);
515 }
516 }
519 static void rx_refill_timeout(unsigned long data)
520 {
521 struct net_device *dev = (struct net_device *)data;
522 netif_rx_schedule(dev);
523 }
526 static void network_alloc_rx_buffers(struct net_device *dev)
527 {
528 unsigned short id;
529 struct netfront_info *np = netdev_priv(dev);
530 struct sk_buff *skb;
531 int i, batch_target;
532 RING_IDX req_prod = np->rx.req_prod_pvt;
533 struct xen_memory_reservation reservation;
534 grant_ref_t ref;
536 if (unlikely(np->backend_state != BEST_CONNECTED))
537 return;
539 /*
540 * Allocate skbuffs greedily, even though we batch updates to the
541 * receive ring. This creates a less bursty demand on the memory
542 * allocator, so should reduce the chance of failed allocation requests
543 * both for ourself and for other kernel subsystems.
544 */
545 batch_target = np->rx_target - (req_prod - np->rx.rsp_cons);
546 for (i = skb_queue_len(&np->rx_batch); i < batch_target; i++) {
547 /*
548 * Subtract dev_alloc_skb headroom (16 bytes) and shared info
549 * tailroom then round down to SKB_DATA_ALIGN boundary.
550 */
551 skb = __dev_alloc_skb(
552 ((PAGE_SIZE - sizeof(struct skb_shared_info)) &
553 (-SKB_DATA_ALIGN(1))) - 16,
554 GFP_ATOMIC|__GFP_NOWARN);
555 if (skb == NULL) {
556 /* Any skbuffs queued for refill? Force them out. */
557 if (i != 0)
558 goto refill;
559 /* Could not allocate any skbuffs. Try again later. */
560 mod_timer(&np->rx_refill_timer,
561 jiffies + (HZ/10));
562 return;
563 }
564 __skb_queue_tail(&np->rx_batch, skb);
565 }
567 /* Is the batch large enough to be worthwhile? */
568 if (i < (np->rx_target/2))
569 return;
571 /* Adjust our fill target if we risked running out of buffers. */
572 if (((req_prod - np->rx.sring->rsp_prod) < (np->rx_target / 4)) &&
573 ((np->rx_target *= 2) > np->rx_max_target))
574 np->rx_target = np->rx_max_target;
576 refill:
577 for (i = 0; ; i++) {
578 if ((skb = __skb_dequeue(&np->rx_batch)) == NULL)
579 break;
581 skb->dev = dev;
583 id = get_id_from_freelist(np->rx_skbs);
585 np->rx_skbs[id] = skb;
587 RING_GET_REQUEST(&np->rx, req_prod + i)->id = id;
588 ref = gnttab_claim_grant_reference(&np->gref_rx_head);
589 BUG_ON((signed short)ref < 0);
590 np->grant_rx_ref[id] = ref;
591 gnttab_grant_foreign_transfer_ref(ref,
592 np->xbdev->otherend_id,
593 __pa(skb->head) >> PAGE_SHIFT);
594 RING_GET_REQUEST(&np->rx, req_prod + i)->gref = ref;
595 np->rx_pfn_array[i] = virt_to_mfn(skb->head);
597 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
598 /* Remove this page before passing back to Xen. */
599 set_phys_to_machine(__pa(skb->head) >> PAGE_SHIFT,
600 INVALID_P2M_ENTRY);
601 MULTI_update_va_mapping(np->rx_mcl+i,
602 (unsigned long)skb->head,
603 __pte(0), 0);
604 }
605 }
607 /* Tell the ballon driver what is going on. */
608 balloon_update_driver_allowance(i);
610 set_xen_guest_handle(reservation.extent_start, np->rx_pfn_array);
611 reservation.nr_extents = i;
612 reservation.extent_order = 0;
613 reservation.address_bits = 0;
614 reservation.domid = DOMID_SELF;
616 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
617 /* After all PTEs have been zapped, flush the TLB. */
618 np->rx_mcl[i-1].args[MULTI_UVMFLAGS_INDEX] =
619 UVMF_TLB_FLUSH|UVMF_ALL;
621 /* Give away a batch of pages. */
622 np->rx_mcl[i].op = __HYPERVISOR_memory_op;
623 np->rx_mcl[i].args[0] = XENMEM_decrease_reservation;
624 np->rx_mcl[i].args[1] = (unsigned long)&reservation;
626 /* Zap PTEs and give away pages in one big multicall. */
627 (void)HYPERVISOR_multicall(np->rx_mcl, i+1);
629 /* Check return status of HYPERVISOR_memory_op(). */
630 if (unlikely(np->rx_mcl[i].result != i))
631 panic("Unable to reduce memory reservation\n");
632 } else
633 if (HYPERVISOR_memory_op(XENMEM_decrease_reservation,
634 &reservation) != i)
635 panic("Unable to reduce memory reservation\n");
637 /* Above is a suitable barrier to ensure backend will see requests. */
638 np->rx.req_prod_pvt = req_prod + i;
639 RING_PUSH_REQUESTS(&np->rx);
640 }
643 static int network_start_xmit(struct sk_buff *skb, struct net_device *dev)
644 {
645 unsigned short id;
646 struct netfront_info *np = netdev_priv(dev);
647 netif_tx_request_t *tx;
648 RING_IDX i;
649 grant_ref_t ref;
650 unsigned long mfn;
651 int notify;
653 if (unlikely(np->tx_full)) {
654 printk(KERN_ALERT "%s: full queue wasn't stopped!\n",
655 dev->name);
656 netif_stop_queue(dev);
657 goto drop;
658 }
660 if (unlikely((((unsigned long)skb->data & ~PAGE_MASK) + skb->len) >=
661 PAGE_SIZE)) {
662 struct sk_buff *nskb;
663 nskb = __dev_alloc_skb(skb->len, GFP_ATOMIC|__GFP_NOWARN);
664 if (unlikely(nskb == NULL))
665 goto drop;
666 skb_put(nskb, skb->len);
667 memcpy(nskb->data, skb->data, skb->len);
668 /* Copy only the header fields we use in this driver. */
669 nskb->dev = skb->dev;
670 nskb->ip_summed = skb->ip_summed;
671 nskb->proto_data_valid = skb->proto_data_valid;
672 dev_kfree_skb(skb);
673 skb = nskb;
674 }
676 spin_lock_irq(&np->tx_lock);
678 if (np->backend_state != BEST_CONNECTED) {
679 spin_unlock_irq(&np->tx_lock);
680 goto drop;
681 }
683 i = np->tx.req_prod_pvt;
685 id = get_id_from_freelist(np->tx_skbs);
686 np->tx_skbs[id] = skb;
688 tx = RING_GET_REQUEST(&np->tx, i);
690 tx->id = id;
691 ref = gnttab_claim_grant_reference(&np->gref_tx_head);
692 BUG_ON((signed short)ref < 0);
693 mfn = virt_to_mfn(skb->data);
694 gnttab_grant_foreign_access_ref(
695 ref, np->xbdev->otherend_id, mfn, GNTMAP_readonly);
696 tx->gref = np->grant_tx_ref[id] = ref;
697 tx->offset = (unsigned long)skb->data & ~PAGE_MASK;
698 tx->size = skb->len;
700 tx->flags = 0;
701 if (skb->ip_summed == CHECKSUM_HW) /* local packet? */
702 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
703 if (skb->proto_data_valid) /* remote but checksummed? */
704 tx->flags |= NETTXF_data_validated;
706 np->tx.req_prod_pvt = i + 1;
707 RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&np->tx, notify);
708 if (notify)
709 notify_remote_via_irq(np->irq);
711 network_tx_buf_gc(dev);
713 if (RING_FULL(&np->tx) ||
714 gnttab_empty_grant_references(&np->gref_tx_head)) {
715 np->tx_full = 1;
716 netif_stop_queue(dev);
717 }
719 spin_unlock_irq(&np->tx_lock);
721 np->stats.tx_bytes += skb->len;
722 np->stats.tx_packets++;
724 return 0;
726 drop:
727 np->stats.tx_dropped++;
728 dev_kfree_skb(skb);
729 return 0;
730 }
732 static irqreturn_t netif_int(int irq, void *dev_id, struct pt_regs *ptregs)
733 {
734 struct net_device *dev = dev_id;
735 struct netfront_info *np = netdev_priv(dev);
736 unsigned long flags;
738 spin_lock_irqsave(&np->tx_lock, flags);
739 network_tx_buf_gc(dev);
740 spin_unlock_irqrestore(&np->tx_lock, flags);
742 if (RING_HAS_UNCONSUMED_RESPONSES(&np->rx) &&
743 (np->user_state == UST_OPEN))
744 netif_rx_schedule(dev);
746 return IRQ_HANDLED;
747 }
750 static int netif_poll(struct net_device *dev, int *pbudget)
751 {
752 struct netfront_info *np = netdev_priv(dev);
753 struct sk_buff *skb, *nskb;
754 netif_rx_response_t *rx;
755 RING_IDX i, rp;
756 mmu_update_t *mmu = np->rx_mmu;
757 multicall_entry_t *mcl = np->rx_mcl;
758 int work_done, budget, more_to_do = 1;
759 struct sk_buff_head rxq;
760 unsigned long flags;
761 unsigned long mfn;
762 grant_ref_t ref;
764 spin_lock(&np->rx_lock);
766 if (np->backend_state != BEST_CONNECTED) {
767 spin_unlock(&np->rx_lock);
768 return 0;
769 }
771 skb_queue_head_init(&rxq);
773 if ((budget = *pbudget) > dev->quota)
774 budget = dev->quota;
775 rp = np->rx.sring->rsp_prod;
776 rmb(); /* Ensure we see queued responses up to 'rp'. */
778 for (i = np->rx.rsp_cons, work_done = 0;
779 (i != rp) && (work_done < budget);
780 i++, work_done++) {
781 rx = RING_GET_RESPONSE(&np->rx, i);
783 /*
784 * This definitely indicates a bug, either in this driver or
785 * in the backend driver. In future this should flag the bad
786 * situation to the system controller to reboot the backed.
787 */
788 if ((ref = np->grant_rx_ref[rx->id]) == GRANT_INVALID_REF) {
789 WPRINTK("Bad rx response id %d.\n", rx->id);
790 work_done--;
791 continue;
792 }
794 /* Memory pressure, insufficient buffer headroom, ... */
795 if ((mfn = gnttab_end_foreign_transfer_ref(ref)) == 0) {
796 if (net_ratelimit())
797 WPRINTK("Unfulfilled rx req (id=%d, st=%d).\n",
798 rx->id, rx->status);
799 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->id =
800 rx->id;
801 RING_GET_REQUEST(&np->rx, np->rx.req_prod_pvt)->gref =
802 ref;
803 np->rx.req_prod_pvt++;
804 RING_PUSH_REQUESTS(&np->rx);
805 work_done--;
806 continue;
807 }
809 gnttab_release_grant_reference(&np->gref_rx_head, ref);
810 np->grant_rx_ref[rx->id] = GRANT_INVALID_REF;
812 skb = np->rx_skbs[rx->id];
813 add_id_to_freelist(np->rx_skbs, rx->id);
815 /* NB. We handle skb overflow later. */
816 skb->data = skb->head + rx->offset;
817 skb->len = rx->status;
818 skb->tail = skb->data + skb->len;
820 /*
821 * Old backends do not assert data_validated but we
822 * can infer it from csum_blank so test both flags.
823 */
824 if (rx->flags & (NETRXF_data_validated|NETRXF_csum_blank)) {
825 skb->ip_summed = CHECKSUM_UNNECESSARY;
826 skb->proto_data_valid = 1;
827 } else {
828 skb->ip_summed = CHECKSUM_NONE;
829 skb->proto_data_valid = 0;
830 }
831 skb->proto_csum_blank = !!(rx->flags & NETRXF_csum_blank);
833 np->stats.rx_packets++;
834 np->stats.rx_bytes += rx->status;
836 if (!xen_feature(XENFEAT_auto_translated_physmap)) {
837 /* Remap the page. */
838 MULTI_update_va_mapping(mcl, (unsigned long)skb->head,
839 pfn_pte_ma(mfn, PAGE_KERNEL),
840 0);
841 mcl++;
842 mmu->ptr = ((maddr_t)mfn << PAGE_SHIFT)
843 | MMU_MACHPHYS_UPDATE;
844 mmu->val = __pa(skb->head) >> PAGE_SHIFT;
845 mmu++;
847 set_phys_to_machine(__pa(skb->head) >> PAGE_SHIFT,
848 mfn);
849 }
851 __skb_queue_tail(&rxq, skb);
852 }
854 /* Some pages are no longer absent... */
855 balloon_update_driver_allowance(-work_done);
857 /* Do all the remapping work, and M2P updates, in one big hypercall. */
858 if (likely((mcl - np->rx_mcl) != 0)) {
859 mcl->op = __HYPERVISOR_mmu_update;
860 mcl->args[0] = (unsigned long)np->rx_mmu;
861 mcl->args[1] = mmu - np->rx_mmu;
862 mcl->args[2] = 0;
863 mcl->args[3] = DOMID_SELF;
864 mcl++;
865 (void)HYPERVISOR_multicall(np->rx_mcl, mcl - np->rx_mcl);
866 }
868 while ((skb = __skb_dequeue(&rxq)) != NULL) {
869 if (skb->len > (dev->mtu + ETH_HLEN + 4)) {
870 if (net_ratelimit())
871 printk(KERN_INFO "Received packet too big for "
872 "MTU (%d > %d)\n",
873 skb->len - ETH_HLEN - 4, dev->mtu);
874 skb->len = 0;
875 skb->tail = skb->data;
876 init_skb_shinfo(skb);
877 dev_kfree_skb(skb);
878 continue;
879 }
881 /*
882 * Enough room in skbuff for the data we were passed? Also,
883 * Linux expects at least 16 bytes headroom in each rx buffer.
884 */
885 if (unlikely(skb->tail > skb->end) ||
886 unlikely((skb->data - skb->head) < 16)) {
887 if (net_ratelimit()) {
888 if (skb->tail > skb->end)
889 printk(KERN_INFO "Received packet "
890 "is %zd bytes beyond tail.\n",
891 skb->tail - skb->end);
892 else
893 printk(KERN_INFO "Received packet "
894 "is %zd bytes before head.\n",
895 16 - (skb->data - skb->head));
896 }
898 nskb = __dev_alloc_skb(skb->len + 2,
899 GFP_ATOMIC|__GFP_NOWARN);
900 if (nskb != NULL) {
901 skb_reserve(nskb, 2);
902 skb_put(nskb, skb->len);
903 memcpy(nskb->data, skb->data, skb->len);
904 /* Copy any other fields we already set up. */
905 nskb->dev = skb->dev;
906 nskb->ip_summed = skb->ip_summed;
907 nskb->proto_data_valid = skb->proto_data_valid;
908 nskb->proto_csum_blank = skb->proto_csum_blank;
909 }
911 /* Reinitialise and then destroy the old skbuff. */
912 skb->len = 0;
913 skb->tail = skb->data;
914 init_skb_shinfo(skb);
915 dev_kfree_skb(skb);
917 /* Switch old for new, if we copied the buffer. */
918 if ((skb = nskb) == NULL)
919 continue;
920 }
922 /* Set the shinfo area, which is hidden behind the data. */
923 init_skb_shinfo(skb);
924 /* Ethernet work: Delayed to here as it peeks the header. */
925 skb->protocol = eth_type_trans(skb, dev);
927 /* Pass it up. */
928 netif_receive_skb(skb);
929 dev->last_rx = jiffies;
930 }
932 np->rx.rsp_cons = i;
934 /* If we get a callback with very few responses, reduce fill target. */
935 /* NB. Note exponential increase, linear decrease. */
936 if (((np->rx.req_prod_pvt - np->rx.sring->rsp_prod) >
937 ((3*np->rx_target) / 4)) &&
938 (--np->rx_target < np->rx_min_target))
939 np->rx_target = np->rx_min_target;
941 network_alloc_rx_buffers(dev);
943 *pbudget -= work_done;
944 dev->quota -= work_done;
946 if (work_done < budget) {
947 local_irq_save(flags);
949 RING_FINAL_CHECK_FOR_RESPONSES(&np->rx, more_to_do);
950 if (!more_to_do)
951 __netif_rx_complete(dev);
953 local_irq_restore(flags);
954 }
956 spin_unlock(&np->rx_lock);
958 return more_to_do;
959 }
962 static int network_close(struct net_device *dev)
963 {
964 struct netfront_info *np = netdev_priv(dev);
965 np->user_state = UST_CLOSED;
966 netif_stop_queue(np->netdev);
967 return 0;
968 }
971 static struct net_device_stats *network_get_stats(struct net_device *dev)
972 {
973 struct netfront_info *np = netdev_priv(dev);
974 return &np->stats;
975 }
977 static void network_connect(struct net_device *dev)
978 {
979 struct netfront_info *np;
980 int i, requeue_idx;
981 netif_tx_request_t *tx;
982 struct sk_buff *skb;
984 np = netdev_priv(dev);
985 spin_lock_irq(&np->tx_lock);
986 spin_lock(&np->rx_lock);
988 /* Recovery procedure: */
990 /* Step 1: Reinitialise variables. */
991 np->tx_full = 0;
993 /*
994 * Step 2: Rebuild the RX and TX ring contents.
995 * NB. We could just free the queued TX packets now but we hope
996 * that sending them out might do some good. We have to rebuild
997 * the RX ring because some of our pages are currently flipped out
998 * so we can't just free the RX skbs.
999 * NB2. Freelist index entries are always going to be less than
1000 * PAGE_OFFSET, whereas pointers to skbs will always be equal or
1001 * greater than PAGE_OFFSET: we use this property to distinguish
1002 * them.
1003 */
1005 /*
1006 * Rebuild the TX buffer freelist and the TX ring itself.
1007 * NB. This reorders packets. We could keep more private state
1008 * to avoid this but maybe it doesn't matter so much given the
1009 * interface has been down.
1010 */
1011 for (requeue_idx = 0, i = 1; i <= NET_TX_RING_SIZE; i++) {
1012 if ((unsigned long)np->tx_skbs[i] < PAGE_OFFSET)
1013 continue;
1015 skb = np->tx_skbs[i];
1017 tx = RING_GET_REQUEST(&np->tx, requeue_idx);
1018 requeue_idx++;
1020 tx->id = i;
1021 gnttab_grant_foreign_access_ref(
1022 np->grant_tx_ref[i], np->xbdev->otherend_id,
1023 virt_to_mfn(np->tx_skbs[i]->data),
1024 GNTMAP_readonly);
1025 tx->gref = np->grant_tx_ref[i];
1026 tx->offset = (unsigned long)skb->data & ~PAGE_MASK;
1027 tx->size = skb->len;
1028 tx->flags = 0;
1029 if (skb->ip_summed == CHECKSUM_HW) /* local packet? */
1030 tx->flags |= NETTXF_csum_blank | NETTXF_data_validated;
1031 if (skb->proto_data_valid) /* remote but checksummed? */
1032 tx->flags |= NETTXF_data_validated;
1034 np->stats.tx_bytes += skb->len;
1035 np->stats.tx_packets++;
1038 np->tx.req_prod_pvt = requeue_idx;
1039 RING_PUSH_REQUESTS(&np->tx);
1041 /* Rebuild the RX buffer freelist and the RX ring itself. */
1042 for (requeue_idx = 0, i = 1; i <= NET_RX_RING_SIZE; i++) {
1043 if ((unsigned long)np->rx_skbs[i] < PAGE_OFFSET)
1044 continue;
1045 gnttab_grant_foreign_transfer_ref(
1046 np->grant_rx_ref[i], np->xbdev->otherend_id,
1047 __pa(np->rx_skbs[i]->data) >> PAGE_SHIFT);
1048 RING_GET_REQUEST(&np->rx, requeue_idx)->gref =
1049 np->grant_rx_ref[i];
1050 RING_GET_REQUEST(&np->rx, requeue_idx)->id = i;
1051 requeue_idx++;
1054 np->rx.req_prod_pvt = requeue_idx;
1055 RING_PUSH_REQUESTS(&np->rx);
1057 /*
1058 * Step 3: All public and private state should now be sane. Get
1059 * ready to start sending and receiving packets and give the driver
1060 * domain a kick because we've probably just requeued some
1061 * packets.
1062 */
1063 np->backend_state = BEST_CONNECTED;
1064 notify_remote_via_irq(np->irq);
1065 network_tx_buf_gc(dev);
1067 if (np->user_state == UST_OPEN)
1068 netif_start_queue(dev);
1070 spin_unlock(&np->rx_lock);
1071 spin_unlock_irq(&np->tx_lock);
1074 static void show_device(struct netfront_info *np)
1076 #ifdef DEBUG
1077 if (np) {
1078 IPRINTK("<vif handle=%u %s(%s) evtchn=%u tx=%p rx=%p>\n",
1079 np->handle,
1080 be_state_name[np->backend_state],
1081 np->user_state ? "open" : "closed",
1082 np->evtchn,
1083 np->tx,
1084 np->rx);
1085 } else
1086 IPRINTK("<vif NULL>\n");
1087 #endif
1090 static void netif_uninit(struct net_device *dev)
1092 struct netfront_info *np = netdev_priv(dev);
1093 gnttab_free_grant_references(np->gref_tx_head);
1094 gnttab_free_grant_references(np->gref_rx_head);
1097 static struct ethtool_ops network_ethtool_ops =
1099 .get_tx_csum = ethtool_op_get_tx_csum,
1100 .set_tx_csum = ethtool_op_set_tx_csum,
1101 };
1103 /*
1104 * Nothing to do here. Virtual interface is point-to-point and the
1105 * physical interface is probably promiscuous anyway.
1106 */
1107 static void network_set_multicast_list(struct net_device *dev)
1111 /** Create a network device.
1112 * @param handle device handle
1113 * @param val return parameter for created device
1114 * @return 0 on success, error code otherwise
1115 */
1116 static int create_netdev(int handle, struct xenbus_device *dev,
1117 struct net_device **val)
1119 int i, err = 0;
1120 struct net_device *netdev = NULL;
1121 struct netfront_info *np = NULL;
1123 if ((netdev = alloc_etherdev(sizeof(struct netfront_info))) == NULL) {
1124 printk(KERN_WARNING "%s> alloc_etherdev failed.\n",
1125 __FUNCTION__);
1126 err = -ENOMEM;
1127 goto exit;
1130 np = netdev_priv(netdev);
1131 np->backend_state = BEST_CLOSED;
1132 np->user_state = UST_CLOSED;
1133 np->handle = handle;
1134 np->xbdev = dev;
1136 spin_lock_init(&np->tx_lock);
1137 spin_lock_init(&np->rx_lock);
1139 skb_queue_head_init(&np->rx_batch);
1140 np->rx_target = RX_DFL_MIN_TARGET;
1141 np->rx_min_target = RX_DFL_MIN_TARGET;
1142 np->rx_max_target = RX_MAX_TARGET;
1144 init_timer(&np->rx_refill_timer);
1145 np->rx_refill_timer.data = (unsigned long)netdev;
1146 np->rx_refill_timer.function = rx_refill_timeout;
1148 /* Initialise {tx,rx}_skbs as a free chain containing every entry. */
1149 for (i = 0; i <= NET_TX_RING_SIZE; i++) {
1150 np->tx_skbs[i] = (void *)((unsigned long) i+1);
1151 np->grant_tx_ref[i] = GRANT_INVALID_REF;
1154 for (i = 0; i <= NET_RX_RING_SIZE; i++) {
1155 np->rx_skbs[i] = (void *)((unsigned long) i+1);
1156 np->grant_rx_ref[i] = GRANT_INVALID_REF;
1159 /* A grant for every tx ring slot */
1160 if (gnttab_alloc_grant_references(TX_MAX_TARGET,
1161 &np->gref_tx_head) < 0) {
1162 printk(KERN_ALERT "#### netfront can't alloc tx grant refs\n");
1163 err = -ENOMEM;
1164 goto exit;
1166 /* A grant for every rx ring slot */
1167 if (gnttab_alloc_grant_references(RX_MAX_TARGET,
1168 &np->gref_rx_head) < 0) {
1169 printk(KERN_ALERT "#### netfront can't alloc rx grant refs\n");
1170 gnttab_free_grant_references(np->gref_tx_head);
1171 err = -ENOMEM;
1172 goto exit;
1175 netdev->open = network_open;
1176 netdev->hard_start_xmit = network_start_xmit;
1177 netdev->stop = network_close;
1178 netdev->get_stats = network_get_stats;
1179 netdev->poll = netif_poll;
1180 netdev->set_multicast_list = network_set_multicast_list;
1181 netdev->uninit = netif_uninit;
1182 netdev->weight = 64;
1183 netdev->features = NETIF_F_IP_CSUM;
1185 SET_ETHTOOL_OPS(netdev, &network_ethtool_ops);
1186 SET_MODULE_OWNER(netdev);
1187 SET_NETDEV_DEV(netdev, &dev->dev);
1189 if ((err = register_netdev(netdev)) != 0) {
1190 printk(KERN_WARNING "%s> register_netdev err=%d\n",
1191 __FUNCTION__, err);
1192 goto exit_free_grefs;
1195 if ((err = xennet_proc_addif(netdev)) != 0) {
1196 unregister_netdev(netdev);
1197 goto exit_free_grefs;
1200 np->netdev = netdev;
1202 exit:
1203 if (err != 0)
1204 kfree(netdev);
1205 else if (val != NULL)
1206 *val = netdev;
1207 return err;
1209 exit_free_grefs:
1210 gnttab_free_grant_references(np->gref_tx_head);
1211 gnttab_free_grant_references(np->gref_rx_head);
1212 goto exit;
1215 /*
1216 * We use this notifier to send out a fake ARP reply to reset switches and
1217 * router ARP caches when an IP interface is brought up on a VIF.
1218 */
1219 static int
1220 inetdev_notify(struct notifier_block *this, unsigned long event, void *ptr)
1222 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
1223 struct net_device *dev = ifa->ifa_dev->dev;
1225 /* UP event and is it one of our devices? */
1226 if (event == NETDEV_UP && dev->open == network_open)
1227 (void)send_fake_arp(dev);
1229 return NOTIFY_DONE;
1233 /* ** Close down ** */
1236 /**
1237 * Handle the change of state of the backend to Closing. We must delete our
1238 * device-layer structures now, to ensure that writes are flushed through to
1239 * the backend. Once is this done, we can switch to Closed in
1240 * acknowledgement.
1241 */
1242 static void netfront_closing(struct xenbus_device *dev)
1244 struct netfront_info *info = dev->data;
1246 DPRINTK("netfront_closing: %s removed\n", dev->nodename);
1248 close_netdev(info);
1250 xenbus_switch_state(dev, XenbusStateClosed);
1254 static int netfront_remove(struct xenbus_device *dev)
1256 struct netfront_info *info = dev->data;
1258 DPRINTK("%s\n", dev->nodename);
1260 netif_disconnect_backend(info);
1261 free_netdev(info->netdev);
1263 return 0;
1267 static void close_netdev(struct netfront_info *info)
1269 spin_lock_irq(&info->netdev->xmit_lock);
1270 netif_stop_queue(info->netdev);
1271 spin_unlock_irq(&info->netdev->xmit_lock);
1273 #ifdef CONFIG_PROC_FS
1274 xennet_proc_delif(info->netdev);
1275 #endif
1277 del_timer_sync(&info->rx_refill_timer);
1279 unregister_netdev(info->netdev);
1283 static void netif_disconnect_backend(struct netfront_info *info)
1285 /* Stop old i/f to prevent errors whilst we rebuild the state. */
1286 spin_lock_irq(&info->tx_lock);
1287 spin_lock(&info->rx_lock);
1288 info->backend_state = BEST_DISCONNECTED;
1289 spin_unlock(&info->rx_lock);
1290 spin_unlock_irq(&info->tx_lock);
1292 if (info->irq)
1293 unbind_from_irqhandler(info->irq, info->netdev);
1294 info->evtchn = info->irq = 0;
1296 end_access(info->tx_ring_ref, info->tx.sring);
1297 end_access(info->rx_ring_ref, info->rx.sring);
1298 info->tx_ring_ref = GRANT_INVALID_REF;
1299 info->rx_ring_ref = GRANT_INVALID_REF;
1300 info->tx.sring = NULL;
1301 info->rx.sring = NULL;
1305 static void netif_free(struct netfront_info *info)
1307 close_netdev(info);
1308 netif_disconnect_backend(info);
1309 free_netdev(info->netdev);
1313 static void end_access(int ref, void *page)
1315 if (ref != GRANT_INVALID_REF)
1316 gnttab_end_foreign_access(ref, 0, (unsigned long)page);
1320 /* ** Driver registration ** */
1323 static struct xenbus_device_id netfront_ids[] = {
1324 { "vif" },
1325 { "" }
1326 };
1329 static struct xenbus_driver netfront = {
1330 .name = "vif",
1331 .owner = THIS_MODULE,
1332 .ids = netfront_ids,
1333 .probe = netfront_probe,
1334 .remove = netfront_remove,
1335 .resume = netfront_resume,
1336 .otherend_changed = backend_changed,
1337 };
1340 static struct notifier_block notifier_inetdev = {
1341 .notifier_call = inetdev_notify,
1342 .next = NULL,
1343 .priority = 0
1344 };
1346 static int __init netif_init(void)
1348 int err = 0;
1350 if (xen_start_info->flags & SIF_INITDOMAIN)
1351 return 0;
1353 if ((err = xennet_proc_init()) != 0)
1354 return err;
1356 IPRINTK("Initialising virtual ethernet driver.\n");
1358 (void)register_inetaddr_notifier(&notifier_inetdev);
1360 return xenbus_register_frontend(&netfront);
1362 module_init(netif_init);
1365 static void netif_exit(void)
1367 unregister_inetaddr_notifier(&notifier_inetdev);
1369 return xenbus_unregister_driver(&netfront);
1371 module_exit(netif_exit);
1373 MODULE_LICENSE("Dual BSD/GPL");
1376 /* ** /proc **/
1379 #ifdef CONFIG_PROC_FS
1381 #define TARGET_MIN 0UL
1382 #define TARGET_MAX 1UL
1383 #define TARGET_CUR 2UL
1385 static int xennet_proc_read(
1386 char *page, char **start, off_t off, int count, int *eof, void *data)
1388 struct net_device *dev =
1389 (struct net_device *)((unsigned long)data & ~3UL);
1390 struct netfront_info *np = netdev_priv(dev);
1391 int len = 0, which_target = (long)data & 3;
1393 switch (which_target) {
1394 case TARGET_MIN:
1395 len = sprintf(page, "%d\n", np->rx_min_target);
1396 break;
1397 case TARGET_MAX:
1398 len = sprintf(page, "%d\n", np->rx_max_target);
1399 break;
1400 case TARGET_CUR:
1401 len = sprintf(page, "%d\n", np->rx_target);
1402 break;
1405 *eof = 1;
1406 return len;
1409 static int xennet_proc_write(
1410 struct file *file, const char __user *buffer,
1411 unsigned long count, void *data)
1413 struct net_device *dev =
1414 (struct net_device *)((unsigned long)data & ~3UL);
1415 struct netfront_info *np = netdev_priv(dev);
1416 int which_target = (long)data & 3;
1417 char string[64];
1418 long target;
1420 if (!capable(CAP_SYS_ADMIN))
1421 return -EPERM;
1423 if (count <= 1)
1424 return -EBADMSG; /* runt */
1425 if (count > sizeof(string))
1426 return -EFBIG; /* too long */
1428 if (copy_from_user(string, buffer, count))
1429 return -EFAULT;
1430 string[sizeof(string)-1] = '\0';
1432 target = simple_strtol(string, NULL, 10);
1433 if (target < RX_MIN_TARGET)
1434 target = RX_MIN_TARGET;
1435 if (target > RX_MAX_TARGET)
1436 target = RX_MAX_TARGET;
1438 spin_lock(&np->rx_lock);
1440 switch (which_target) {
1441 case TARGET_MIN:
1442 if (target > np->rx_max_target)
1443 np->rx_max_target = target;
1444 np->rx_min_target = target;
1445 if (target > np->rx_target)
1446 np->rx_target = target;
1447 break;
1448 case TARGET_MAX:
1449 if (target < np->rx_min_target)
1450 np->rx_min_target = target;
1451 np->rx_max_target = target;
1452 if (target < np->rx_target)
1453 np->rx_target = target;
1454 break;
1455 case TARGET_CUR:
1456 break;
1459 network_alloc_rx_buffers(dev);
1461 spin_unlock(&np->rx_lock);
1463 return count;
1466 static int xennet_proc_init(void)
1468 if (proc_mkdir("xen/net", NULL) == NULL)
1469 return -ENOMEM;
1470 return 0;
1473 static int xennet_proc_addif(struct net_device *dev)
1475 struct proc_dir_entry *dir, *min, *max, *cur;
1476 char name[30];
1478 sprintf(name, "xen/net/%s", dev->name);
1480 dir = proc_mkdir(name, NULL);
1481 if (!dir)
1482 goto nomem;
1484 min = create_proc_entry("rxbuf_min", 0644, dir);
1485 max = create_proc_entry("rxbuf_max", 0644, dir);
1486 cur = create_proc_entry("rxbuf_cur", 0444, dir);
1487 if (!min || !max || !cur)
1488 goto nomem;
1490 min->read_proc = xennet_proc_read;
1491 min->write_proc = xennet_proc_write;
1492 min->data = (void *)((unsigned long)dev | TARGET_MIN);
1494 max->read_proc = xennet_proc_read;
1495 max->write_proc = xennet_proc_write;
1496 max->data = (void *)((unsigned long)dev | TARGET_MAX);
1498 cur->read_proc = xennet_proc_read;
1499 cur->write_proc = xennet_proc_write;
1500 cur->data = (void *)((unsigned long)dev | TARGET_CUR);
1502 return 0;
1504 nomem:
1505 xennet_proc_delif(dev);
1506 return -ENOMEM;
1509 static void xennet_proc_delif(struct net_device *dev)
1511 char name[30];
1513 sprintf(name, "xen/net/%s/rxbuf_min", dev->name);
1514 remove_proc_entry(name, NULL);
1516 sprintf(name, "xen/net/%s/rxbuf_max", dev->name);
1517 remove_proc_entry(name, NULL);
1519 sprintf(name, "xen/net/%s/rxbuf_cur", dev->name);
1520 remove_proc_entry(name, NULL);
1522 sprintf(name, "xen/net/%s", dev->name);
1523 remove_proc_entry(name, NULL);
1526 #endif
1529 /*
1530 * Local variables:
1531 * c-file-style: "linux"
1532 * indent-tabs-mode: t
1533 * c-indent-level: 8
1534 * c-basic-offset: 8
1535 * tab-width: 8
1536 * End:
1537 */