1 diff --unified --recursive --new-file linux-2.6.12.5/include/linux/ring.h linux-2.6.12.5-1-686-smp-ring3/include/linux/ring.h
2 --- linux-2.6.12.5/include/linux/ring.h 1970-01-01 01:00:00.000000000 +0100
3 +++ linux-2.6.12.5-1-686-smp-ring3/include/linux/ring.h 2005-10-22 23:50:44.951445250 +0200
6 + * Definitions for packet ring
8 + * 2004 - Luca Deri <deri@ntop.org>
14 +#define INCLUDE_MAC_INFO
16 +#ifdef INCLUDE_MAC_INFO
17 +#define SKB_DISPLACEMENT 14 /* Include MAC address information */
19 +#define SKB_DISPLACEMENT 0 /* Do NOT include MAC address information */
23 +#define RING_MAGIC_VALUE 0x88
24 +#define RING_FLOWSLOT_VERSION 5
25 +#define RING_VERSION "3.0"
27 +#define SO_ADD_TO_CLUSTER 99
28 +#define SO_REMOVE_FROM_CLUSTER 100
29 +#define SO_SET_REFLECTOR 101
31 +/* *********************************** */
35 + struct timeval ts; /* time stamp */
36 + u_int32_t caplen; /* length of portion present */
37 + u_int32_t len; /* length this packet (off wire) */
41 +/* *********************************** */
44 + cluster_per_flow = 0,
48 +/* *********************************** */
50 +#define RING_MIN_SLOT_SIZE (60+sizeof(struct pcap_pkthdr))
51 +#define RING_MAX_SLOT_SIZE (1514+sizeof(struct pcap_pkthdr))
53 +/* *********************************** */
55 +typedef struct flowSlotInfo {
56 + u_int16_t version, sample_rate;
57 + u_int32_t tot_slots, slot_len, tot_mem;
59 + u_int64_t tot_pkts, tot_lost;
60 + u_int64_t tot_insert, tot_read;
61 + u_int16_t insert_idx;
62 + u_int16_t remove_idx;
65 +/* *********************************** */
67 +typedef struct flowSlot {
69 + u_char magic; /* It must alwasy be zero */
71 + u_char slot_state; /* 0=empty, 1=full */
72 + u_char bucket; /* bucket[bucketLen] */
75 +/* *********************************** */
79 +FlowSlotInfo* getRingPtr(void);
80 +int allocateRing(char *deviceName, u_int numSlots,
81 + u_int bucketLen, u_int sampleRate);
82 +unsigned int pollRing(struct file *fp, struct poll_table_struct * wait);
83 +void deallocateRing(void);
85 +/* ************************* */
87 +typedef int (*handle_ring_skb)(struct sk_buff *skb,
88 + u_char recv_packet, u_char real_skb);
89 +extern handle_ring_skb get_skb_ring_handler(void);
90 +extern void set_skb_ring_handler(handle_ring_skb the_handler);
91 +extern void do_skb_ring_handler(struct sk_buff *skb,
92 + u_char recv_packet, u_char real_skb);
94 +typedef int (*handle_ring_buffer)(struct net_device *dev,
95 + char *data, int len);
96 +extern handle_ring_buffer get_buffer_ring_handler(void);
97 +extern void set_buffer_ring_handler(handle_ring_buffer the_handler);
98 +extern int do_buffer_ring_handler(struct net_device *dev,
99 + char *data, int len);
100 +#endif /* __KERNEL__ */
102 +/* *********************************** */
104 +#define PF_RING 27 /* Packet Ring */
105 +#define SOCK_RING PF_RING
108 +#define SIORINGPOLL 0x8888
110 +/* *********************************** */
112 +#endif /* __RING_H */
113 diff --unified --recursive --new-file linux-2.6.12.5/net/Kconfig linux-2.6.12.5-1-686-smp-ring3/net/Kconfig
114 --- linux-2.6.12.5/net/Kconfig 2005-08-15 02:20:18.000000000 +0200
115 +++ linux-2.6.12.5-1-686-smp-ring3/net/Kconfig 2005-10-22 23:50:45.535481750 +0200
118 Say Y unless you know what you are doing.
120 +source "net/ring/Kconfig"
122 bool "TCP/IP networking"
124 diff --unified --recursive --new-file linux-2.6.12.5/net/Makefile linux-2.6.12.5-1-686-smp-ring3/net/Makefile
125 --- linux-2.6.12.5/net/Makefile 2005-08-15 02:20:18.000000000 +0200
126 +++ linux-2.6.12.5-1-686-smp-ring3/net/Makefile 2005-10-22 23:50:45.491479000 +0200
128 obj-$(CONFIG_DECNET) += decnet/
129 obj-$(CONFIG_ECONET) += econet/
130 obj-$(CONFIG_VLAN_8021Q) += 8021q/
131 +obj-$(CONFIG_RING) += ring/
132 obj-$(CONFIG_IP_SCTP) += sctp/
134 ifeq ($(CONFIG_NET),y)
135 diff --unified --recursive --new-file linux-2.6.12.5/net/Makefile.ORG linux-2.6.12.5-1-686-smp-ring3/net/Makefile.ORG
136 --- linux-2.6.12.5/net/Makefile.ORG 1970-01-01 01:00:00.000000000 +0100
137 +++ linux-2.6.12.5-1-686-smp-ring3/net/Makefile.ORG 2005-10-22 23:50:45.483478500 +0200
140 +# Makefile for the linux networking.
142 +# 2 Sep 2000, Christoph Hellwig <hch@infradead.org>
143 +# Rewritten to use lists instead of if-statements.
148 +obj-$(CONFIG_NET) := socket.o core/
150 +tmp-$(CONFIG_COMPAT) := compat.o
151 +obj-$(CONFIG_NET) += $(tmp-y)
153 +# LLC has to be linked before the files in net/802/
154 +obj-$(CONFIG_LLC) += llc/
155 +obj-$(CONFIG_NET) += ethernet/ 802/ sched/ netlink/
156 +obj-$(CONFIG_INET) += ipv4/
157 +obj-$(CONFIG_XFRM) += xfrm/
158 +obj-$(CONFIG_UNIX) += unix/
159 +ifneq ($(CONFIG_IPV6),)
162 +obj-$(CONFIG_PACKET) += packet/
163 +obj-$(CONFIG_NET_KEY) += key/
164 +obj-$(CONFIG_NET_SCHED) += sched/
165 +obj-$(CONFIG_BRIDGE) += bridge/
166 +obj-$(CONFIG_IPX) += ipx/
167 +obj-$(CONFIG_ATALK) += appletalk/
168 +obj-$(CONFIG_WAN_ROUTER) += wanrouter/
169 +obj-$(CONFIG_X25) += x25/
170 +obj-$(CONFIG_LAPB) += lapb/
171 +obj-$(CONFIG_NETROM) += netrom/
172 +obj-$(CONFIG_ROSE) += rose/
173 +obj-$(CONFIG_AX25) += ax25/
174 +obj-$(CONFIG_IRDA) += irda/
175 +obj-$(CONFIG_BT) += bluetooth/
176 +obj-$(CONFIG_SUNRPC) += sunrpc/
177 +obj-$(CONFIG_RXRPC) += rxrpc/
178 +obj-$(CONFIG_ATM) += atm/
179 +obj-$(CONFIG_DECNET) += decnet/
180 +obj-$(CONFIG_ECONET) += econet/
181 +obj-$(CONFIG_VLAN_8021Q) += 8021q/
182 +obj-$(CONFIG_IP_SCTP) += sctp/
184 +ifeq ($(CONFIG_NET),y)
185 +obj-$(CONFIG_SYSCTL) += sysctl_net.o
187 diff --unified --recursive --new-file linux-2.6.12.5/net/core/dev.c linux-2.6.12.5-1-686-smp-ring3/net/core/dev.c
188 --- linux-2.6.12.5/net/core/dev.c 2005-08-15 02:20:18.000000000 +0200
189 +++ linux-2.6.12.5-1-686-smp-ring3/net/core/dev.c 2005-10-22 23:50:45.479478250 +0200
191 #endif /* CONFIG_NET_RADIO */
192 #include <asm/current.h>
194 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
196 +/* #define RING_DEBUG */
198 +#include <linux/ring.h>
199 +#include <linux/version.h>
201 +static handle_ring_skb ring_handler = NULL;
203 +handle_ring_skb get_skb_ring_handler() { return(ring_handler); }
205 +void set_skb_ring_handler(handle_ring_skb the_handler) {
206 + ring_handler = the_handler;
209 +void do_skb_ring_handler(struct sk_buff *skb,
210 + u_char recv_packet, u_char real_skb) {
212 + ring_handler(skb, recv_packet, real_skb);
215 +/* ******************* */
217 +static handle_ring_buffer buffer_ring_handler = NULL;
219 +handle_ring_buffer get_buffer_ring_handler() { return(buffer_ring_handler); }
221 +void set_buffer_ring_handler(handle_ring_buffer the_handler) {
222 + buffer_ring_handler = the_handler;
225 +int do_buffer_ring_handler(struct net_device *dev, char *data, int len) {
226 + if(buffer_ring_handler) {
227 + buffer_ring_handler(dev, data, len);
233 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
234 +EXPORT_SYMBOL(get_skb_ring_handler);
235 +EXPORT_SYMBOL(set_skb_ring_handler);
236 +EXPORT_SYMBOL(do_skb_ring_handler);
238 +EXPORT_SYMBOL(get_buffer_ring_handler);
239 +EXPORT_SYMBOL(set_buffer_ring_handler);
240 +EXPORT_SYMBOL(do_buffer_ring_handler);
244 /* This define, if set, will randomly drop a packet when congestion
245 * is more than moderate. It helps fairness in the multi-interface
246 * case when one of them is a hog, but it kills performance for the
247 @@ -1293,6 +1343,10 @@
248 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
251 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
252 + if(ring_handler) ring_handler(skb, 0, 1);
253 +#endif /* CONFIG_RING */
255 /* Grab device queue */
256 spin_lock(&dev->queue_lock);
258 @@ -1509,6 +1563,13 @@
262 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
263 + if(ring_handler && ring_handler(skb, 1, 1)) {
264 + /* The packet has been copied into a ring */
265 + return(NET_RX_SUCCESS);
267 +#endif /* CONFIG_RING */
269 if (local_softirq_pending())
272 @@ -1655,6 +1716,13 @@
273 int ret = NET_RX_DROP;
276 +#if defined (CONFIG_RING) || defined(CONFIG_RING_MODULE)
277 + if(ring_handler && ring_handler(skb, 1, 1)) {
278 + /* The packet has been copied into a ring */
279 + return(NET_RX_SUCCESS);
281 +#endif /* CONFIG_RING */
283 /* if we've gotten here through NAPI, check netpoll */
284 if (skb->dev->poll && netpoll_rx(skb))
286 diff --unified --recursive --new-file linux-2.6.12.5/net/core/dev.c.ORG linux-2.6.12.5-1-686-smp-ring3/net/core/dev.c.ORG
287 --- linux-2.6.12.5/net/core/dev.c.ORG 1970-01-01 01:00:00.000000000 +0100
288 +++ linux-2.6.12.5-1-686-smp-ring3/net/core/dev.c.ORG 2005-10-22 23:50:45.203461000 +0200
291 + * NET3 Protocol independent device support routines.
293 + * This program is free software; you can redistribute it and/or
294 + * modify it under the terms of the GNU General Public License
295 + * as published by the Free Software Foundation; either version
296 + * 2 of the License, or (at your option) any later version.
298 + * Derived from the non IP parts of dev.c 1.0.19
299 + * Authors: Ross Biro
300 + * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
301 + * Mark Evans, <evansmp@uhura.aston.ac.uk>
303 + * Additional Authors:
304 + * Florian la Roche <rzsfl@rz.uni-sb.de>
305 + * Alan Cox <gw4pts@gw4pts.ampr.org>
306 + * David Hinds <dahinds@users.sourceforge.net>
307 + * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
308 + * Adam Sulmicki <adam@cfar.umd.edu>
309 + * Pekka Riikonen <priikone@poesidon.pspt.fi>
312 + * D.J. Barrow : Fixed bug where dev->refcnt gets set
313 + * to 2 if register_netdev gets called
314 + * before net_dev_init & also removed a
315 + * few lines of code in the process.
316 + * Alan Cox : device private ioctl copies fields back.
317 + * Alan Cox : Transmit queue code does relevant
318 + * stunts to keep the queue safe.
319 + * Alan Cox : Fixed double lock.
320 + * Alan Cox : Fixed promisc NULL pointer trap
321 + * ???????? : Support the full private ioctl range
322 + * Alan Cox : Moved ioctl permission check into
324 + * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
325 + * Alan Cox : 100 backlog just doesn't cut it when
326 + * you start doing multicast video 8)
327 + * Alan Cox : Rewrote net_bh and list manager.
328 + * Alan Cox : Fix ETH_P_ALL echoback lengths.
329 + * Alan Cox : Took out transmit every packet pass
330 + * Saved a few bytes in the ioctl handler
331 + * Alan Cox : Network driver sets packet type before
332 + * calling netif_rx. Saves a function
334 + * Alan Cox : Hashed net_bh()
335 + * Richard Kooijman: Timestamp fixes.
336 + * Alan Cox : Wrong field in SIOCGIFDSTADDR
337 + * Alan Cox : Device lock protection.
338 + * Alan Cox : Fixed nasty side effect of device close
340 + * Rudi Cilibrasi : Pass the right thing to
341 + * set_mac_address()
342 + * Dave Miller : 32bit quantity for the device lock to
343 + * make it work out on a Sparc.
344 + * Bjorn Ekwall : Added KERNELD hack.
345 + * Alan Cox : Cleaned up the backlog initialise.
346 + * Craig Metz : SIOCGIFCONF fix if space for under
348 + * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
349 + * is no device open function.
350 + * Andi Kleen : Fix error reporting for SIOCGIFCONF
351 + * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
352 + * Cyrus Durgin : Cleaned for KMOD
353 + * Adam Sulmicki : Bug Fix : Network Device Unload
354 + * A network device unload needs to purge
355 + * the backlog queue.
356 + * Paul Rusty Russell : SIOCSIFNAME
357 + * Pekka Riikonen : Netdev boot-time settings code
358 + * Andrew Morton : Make unregister_netdevice wait
359 + * indefinitely on dev->refcnt
360 + * J Hadi Salim : - Backlog queue sampling
361 + * - netif_rx() feedback
364 +#include <asm/uaccess.h>
365 +#include <asm/system.h>
366 +#include <linux/bitops.h>
367 +#include <linux/config.h>
368 +#include <linux/cpu.h>
369 +#include <linux/types.h>
370 +#include <linux/kernel.h>
371 +#include <linux/sched.h>
372 +#include <linux/string.h>
373 +#include <linux/mm.h>
374 +#include <linux/socket.h>
375 +#include <linux/sockios.h>
376 +#include <linux/errno.h>
377 +#include <linux/interrupt.h>
378 +#include <linux/if_ether.h>
379 +#include <linux/netdevice.h>
380 +#include <linux/etherdevice.h>
381 +#include <linux/notifier.h>
382 +#include <linux/skbuff.h>
383 +#include <net/sock.h>
384 +#include <linux/rtnetlink.h>
385 +#include <linux/proc_fs.h>
386 +#include <linux/seq_file.h>
387 +#include <linux/stat.h>
388 +#include <linux/if_bridge.h>
389 +#include <linux/divert.h>
390 +#include <net/dst.h>
391 +#include <net/pkt_sched.h>
392 +#include <net/checksum.h>
393 +#include <linux/highmem.h>
394 +#include <linux/init.h>
395 +#include <linux/kmod.h>
396 +#include <linux/module.h>
397 +#include <linux/kallsyms.h>
398 +#include <linux/netpoll.h>
399 +#include <linux/rcupdate.h>
400 +#include <linux/delay.h>
401 +#ifdef CONFIG_NET_RADIO
402 +#include <linux/wireless.h> /* Note : will define WIRELESS_EXT */
403 +#include <net/iw_handler.h>
404 +#endif /* CONFIG_NET_RADIO */
405 +#include <asm/current.h>
407 +/* This define, if set, will randomly drop a packet when congestion
408 + * is more than moderate. It helps fairness in the multi-interface
409 + * case when one of them is a hog, but it kills performance for the
410 + * single interface case so it is off now by default.
414 +/* Setting this will sample the queue lengths and thus congestion
415 + * via a timer instead of as each packet is received.
417 +#undef OFFLINE_SAMPLE
420 + * The list of packet types we will receive (as opposed to discard)
421 + * and the routines to invoke.
423 + * Why 16. Because with 16 the only overlap we get on a hash of the
424 + * low nibble of the protocol value is RARP/SNAP/X.25.
426 + * NOTE: That is no longer true with the addition of VLAN tags. Not
427 + * sure which should go first, but I bet it won't make much
428 + * difference if we are running VLANs. The good news is that
429 + * this protocol won't be in the list unless compiled in, so
430 + * the average user (w/out VLANs) will not be adversly affected.
447 +static DEFINE_SPINLOCK(ptype_lock);
448 +static struct list_head ptype_base[16]; /* 16 way hashed list */
449 +static struct list_head ptype_all; /* Taps */
451 +#ifdef OFFLINE_SAMPLE
452 +static void sample_queue(unsigned long dummy);
453 +static struct timer_list samp_timer = TIMER_INITIALIZER(sample_queue, 0, 0);
457 + * The @dev_base list is protected by @dev_base_lock and the rtln
460 + * Pure readers hold dev_base_lock for reading.
462 + * Writers must hold the rtnl semaphore while they loop through the
463 + * dev_base list, and hold dev_base_lock for writing when they do the
464 + * actual updates. This allows pure readers to access the list even
465 + * while a writer is preparing to update it.
467 + * To put it another way, dev_base_lock is held for writing only to
468 + * protect against pure readers; the rtnl semaphore provides the
469 + * protection against other writers.
471 + * See, for example usages, register_netdevice() and
472 + * unregister_netdevice(), which must be called with the rtnl
475 +struct net_device *dev_base;
476 +static struct net_device **dev_tail = &dev_base;
477 +DEFINE_RWLOCK(dev_base_lock);
479 +EXPORT_SYMBOL(dev_base);
480 +EXPORT_SYMBOL(dev_base_lock);
482 +#define NETDEV_HASHBITS 8
483 +static struct hlist_head dev_name_head[1<<NETDEV_HASHBITS];
484 +static struct hlist_head dev_index_head[1<<NETDEV_HASHBITS];
486 +static inline struct hlist_head *dev_name_hash(const char *name)
488 + unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
489 + return &dev_name_head[hash & ((1<<NETDEV_HASHBITS)-1)];
492 +static inline struct hlist_head *dev_index_hash(int ifindex)
494 + return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)];
498 + * Our notifier list
501 +static struct notifier_block *netdev_chain;
504 + * Device drivers call our routines to queue packets here. We empty the
505 + * queue in the local softnet handler.
507 +DEFINE_PER_CPU(struct softnet_data, softnet_data) = { 0, };
510 +extern int netdev_sysfs_init(void);
511 +extern int netdev_register_sysfs(struct net_device *);
512 +extern void netdev_unregister_sysfs(struct net_device *);
514 +#define netdev_sysfs_init() (0)
515 +#define netdev_register_sysfs(dev) (0)
516 +#define netdev_unregister_sysfs(dev) do { } while(0)
520 +/*******************************************************************************
522 + Protocol management and registration routines
524 +*******************************************************************************/
533 + * Add a protocol ID to the list. Now that the input handler is
534 + * smarter we can dispense with all the messy stuff that used to be
537 + * BEWARE!!! Protocol handlers, mangling input packets,
538 + * MUST BE last in hash buckets and checking protocol handlers
539 + * MUST start from promiscuous ptype_all chain in net_bh.
540 + * It is true now, do not change it.
541 + * Explanation follows: if protocol handler, mangling packet, will
542 + * be the first on list, it is not able to sense, that packet
543 + * is cloned and should be copied-on-write, so that it will
544 + * change it and subsequent readers will get broken packet.
549 + * dev_add_pack - add packet handler
550 + * @pt: packet type declaration
552 + * Add a protocol handler to the networking stack. The passed &packet_type
553 + * is linked into kernel lists and may not be freed until it has been
554 + * removed from the kernel lists.
556 + * This call does not sleep therefore it can not
557 + * guarantee all CPU's that are in middle of receiving packets
558 + * will see the new packet type (until the next received packet).
561 +void dev_add_pack(struct packet_type *pt)
565 + spin_lock_bh(&ptype_lock);
566 + if (pt->type == htons(ETH_P_ALL)) {
568 + list_add_rcu(&pt->list, &ptype_all);
570 + hash = ntohs(pt->type) & 15;
571 + list_add_rcu(&pt->list, &ptype_base[hash]);
573 + spin_unlock_bh(&ptype_lock);
576 +extern void linkwatch_run_queue(void);
581 + * __dev_remove_pack - remove packet handler
582 + * @pt: packet type declaration
584 + * Remove a protocol handler that was previously added to the kernel
585 + * protocol handlers by dev_add_pack(). The passed &packet_type is removed
586 + * from the kernel lists and can be freed or reused once this function
589 + * The packet type might still be in use by receivers
590 + * and must not be freed until after all the CPU's have gone
591 + * through a quiescent state.
593 +void __dev_remove_pack(struct packet_type *pt)
595 + struct list_head *head;
596 + struct packet_type *pt1;
598 + spin_lock_bh(&ptype_lock);
600 + if (pt->type == htons(ETH_P_ALL)) {
604 + head = &ptype_base[ntohs(pt->type) & 15];
606 + list_for_each_entry(pt1, head, list) {
608 + list_del_rcu(&pt->list);
613 + printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
615 + spin_unlock_bh(&ptype_lock);
618 + * dev_remove_pack - remove packet handler
619 + * @pt: packet type declaration
621 + * Remove a protocol handler that was previously added to the kernel
622 + * protocol handlers by dev_add_pack(). The passed &packet_type is removed
623 + * from the kernel lists and can be freed or reused once this function
626 + * This call sleeps to guarantee that no CPU is looking at the packet
627 + * type after return.
629 +void dev_remove_pack(struct packet_type *pt)
631 + __dev_remove_pack(pt);
636 +/******************************************************************************
638 + Device Boot-time Settings Routines
640 +*******************************************************************************/
642 +/* Boot time configuration table */
643 +static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
646 + * netdev_boot_setup_add - add new setup entry
647 + * @name: name of the device
648 + * @map: configured settings for the device
650 + * Adds new setup entry to the dev_boot_setup list. The function
651 + * returns 0 on error and 1 on success. This is a generic routine to
654 +static int netdev_boot_setup_add(char *name, struct ifmap *map)
656 + struct netdev_boot_setup *s;
659 + s = dev_boot_setup;
660 + for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
661 + if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
662 + memset(s[i].name, 0, sizeof(s[i].name));
663 + strcpy(s[i].name, name);
664 + memcpy(&s[i].map, map, sizeof(s[i].map));
669 + return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
673 + * netdev_boot_setup_check - check boot time settings
674 + * @dev: the netdevice
676 + * Check boot time settings for the device.
677 + * The found settings are set for the device to be used
678 + * later in the device probing.
679 + * Returns 0 if no settings found, 1 if they are.
681 +int netdev_boot_setup_check(struct net_device *dev)
683 + struct netdev_boot_setup *s = dev_boot_setup;
686 + for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
687 + if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
688 + !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
689 + dev->irq = s[i].map.irq;
690 + dev->base_addr = s[i].map.base_addr;
691 + dev->mem_start = s[i].map.mem_start;
692 + dev->mem_end = s[i].map.mem_end;
701 + * netdev_boot_base - get address from boot time settings
702 + * @prefix: prefix for network device
703 + * @unit: id for network device
705 + * Check boot time settings for the base address of device.
706 + * The found settings are set for the device to be used
707 + * later in the device probing.
708 + * Returns 0 if no settings found.
710 +unsigned long netdev_boot_base(const char *prefix, int unit)
712 + const struct netdev_boot_setup *s = dev_boot_setup;
713 + char name[IFNAMSIZ];
716 + sprintf(name, "%s%d", prefix, unit);
719 + * If device already registered then return base of 1
720 + * to indicate not to probe for this interface
722 + if (__dev_get_by_name(name))
725 + for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
726 + if (!strcmp(name, s[i].name))
727 + return s[i].map.base_addr;
732 + * Saves at boot time configured settings for any netdevice.
734 +int __init netdev_boot_setup(char *str)
739 + str = get_options(str, ARRAY_SIZE(ints), ints);
743 + /* Save settings */
744 + memset(&map, 0, sizeof(map));
748 + map.base_addr = ints[2];
750 + map.mem_start = ints[3];
752 + map.mem_end = ints[4];
754 + /* Add new entry to the list */
755 + return netdev_boot_setup_add(str, &map);
758 +__setup("netdev=", netdev_boot_setup);
760 +/*******************************************************************************
762 + Device Interface Subroutines
764 +*******************************************************************************/
767 + * __dev_get_by_name - find a device by its name
768 + * @name: name to find
770 + * Find an interface by name. Must be called under RTNL semaphore
771 + * or @dev_base_lock. If the name is found a pointer to the device
772 + * is returned. If the name is not found then %NULL is returned. The
773 + * reference counters are not incremented so the caller must be
774 + * careful with locks.
777 +struct net_device *__dev_get_by_name(const char *name)
779 + struct hlist_node *p;
781 + hlist_for_each(p, dev_name_hash(name)) {
782 + struct net_device *dev
783 + = hlist_entry(p, struct net_device, name_hlist);
784 + if (!strncmp(dev->name, name, IFNAMSIZ))
791 + * dev_get_by_name - find a device by its name
792 + * @name: name to find
794 + * Find an interface by name. This can be called from any
795 + * context and does its own locking. The returned handle has
796 + * the usage count incremented and the caller must use dev_put() to
797 + * release it when it is no longer needed. %NULL is returned if no
798 + * matching device is found.
801 +struct net_device *dev_get_by_name(const char *name)
803 + struct net_device *dev;
805 + read_lock(&dev_base_lock);
806 + dev = __dev_get_by_name(name);
809 + read_unlock(&dev_base_lock);
814 + * __dev_get_by_index - find a device by its ifindex
815 + * @ifindex: index of device
817 + * Search for an interface by index. Returns %NULL if the device
818 + * is not found or a pointer to the device. The device has not
819 + * had its reference counter increased so the caller must be careful
820 + * about locking. The caller must hold either the RTNL semaphore
821 + * or @dev_base_lock.
824 +struct net_device *__dev_get_by_index(int ifindex)
826 + struct hlist_node *p;
828 + hlist_for_each(p, dev_index_hash(ifindex)) {
829 + struct net_device *dev
830 + = hlist_entry(p, struct net_device, index_hlist);
831 + if (dev->ifindex == ifindex)
839 + * dev_get_by_index - find a device by its ifindex
840 + * @ifindex: index of device
842 + * Search for an interface by index. Returns NULL if the device
843 + * is not found or a pointer to the device. The device returned has
844 + * had a reference added and the pointer is safe until the user calls
845 + * dev_put to indicate they have finished with it.
848 +struct net_device *dev_get_by_index(int ifindex)
850 + struct net_device *dev;
852 + read_lock(&dev_base_lock);
853 + dev = __dev_get_by_index(ifindex);
856 + read_unlock(&dev_base_lock);
861 + * dev_getbyhwaddr - find a device by its hardware address
862 + * @type: media type of device
863 + * @ha: hardware address
865 + * Search for an interface by MAC address. Returns NULL if the device
866 + * is not found or a pointer to the device. The caller must hold the
867 + * rtnl semaphore. The returned device has not had its ref count increased
868 + * and the caller must therefore be careful about locking
871 + * If the API was consistent this would be __dev_get_by_hwaddr
874 +struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
876 + struct net_device *dev;
880 + for (dev = dev_base; dev; dev = dev->next)
881 + if (dev->type == type &&
882 + !memcmp(dev->dev_addr, ha, dev->addr_len))
887 +struct net_device *dev_getfirstbyhwtype(unsigned short type)
889 + struct net_device *dev;
892 + for (dev = dev_base; dev; dev = dev->next) {
893 + if (dev->type == type) {
902 +EXPORT_SYMBOL(dev_getfirstbyhwtype);
905 + * dev_get_by_flags - find any device with given flags
906 + * @if_flags: IFF_* values
907 + * @mask: bitmask of bits in if_flags to check
909 + * Search for any interface with the given flags. Returns NULL if a device
910 + * is not found or a pointer to the device. The device returned has
911 + * had a reference added and the pointer is safe until the user calls
912 + * dev_put to indicate they have finished with it.
915 +struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
917 + struct net_device *dev;
919 + read_lock(&dev_base_lock);
920 + for (dev = dev_base; dev != NULL; dev = dev->next) {
921 + if (((dev->flags ^ if_flags) & mask) == 0) {
926 + read_unlock(&dev_base_lock);
931 + * dev_valid_name - check if name is okay for network device
932 + * @name: name string
934 + * Network device names need to be valid file names to
935 + * to allow sysfs to work
937 +static int dev_valid_name(const char *name)
939 + return !(*name == '\0'
940 + || !strcmp(name, ".")
941 + || !strcmp(name, "..")
942 + || strchr(name, '/'));
946 + * dev_alloc_name - allocate a name for a device
948 + * @name: name format string
950 + * Passed a format string - eg "lt%d" it will try and find a suitable
951 + * id. Not efficient for many devices, not called a lot. The caller
952 + * must hold the dev_base or rtnl lock while allocating the name and
953 + * adding the device in order to avoid duplicates. Returns the number
954 + * of the unit assigned or a negative errno code.
957 +int dev_alloc_name(struct net_device *dev, const char *name)
960 + char buf[IFNAMSIZ];
962 + const int max_netdevices = 8*PAGE_SIZE;
964 + struct net_device *d;
966 + p = strnchr(name, IFNAMSIZ-1, '%');
969 + * Verify the string as this thing may have come from
970 + * the user. There must be either one "%d" and no other "%"
973 + if (p[1] != 'd' || strchr(p + 2, '%'))
976 + /* Use one page as a bit array of possible slots */
977 + inuse = (long *) get_zeroed_page(GFP_ATOMIC);
981 + for (d = dev_base; d; d = d->next) {
982 + if (!sscanf(d->name, name, &i))
984 + if (i < 0 || i >= max_netdevices)
987 + /* avoid cases where sscanf is not exact inverse of printf */
988 + snprintf(buf, sizeof(buf), name, i);
989 + if (!strncmp(buf, d->name, IFNAMSIZ))
993 + i = find_first_zero_bit(inuse, max_netdevices);
994 + free_page((unsigned long) inuse);
997 + snprintf(buf, sizeof(buf), name, i);
998 + if (!__dev_get_by_name(buf)) {
999 + strlcpy(dev->name, buf, IFNAMSIZ);
1003 + /* It is possible to run out of possible slots
1004 + * when the name is long and there isn't enough space left
1005 + * for the digits, or if all bits are used.
1012 + * dev_change_name - change name of a device
1014 + * @newname: name (or format string) must be at least IFNAMSIZ
1016 + * Change name of a device, can pass format strings "eth%d".
1017 + * for wildcarding.
1019 +int dev_change_name(struct net_device *dev, char *newname)
1025 + if (dev->flags & IFF_UP)
1028 + if (!dev_valid_name(newname))
1031 + if (strchr(newname, '%')) {
1032 + err = dev_alloc_name(dev, newname);
1035 + strcpy(newname, dev->name);
1037 + else if (__dev_get_by_name(newname))
1040 + strlcpy(dev->name, newname, IFNAMSIZ);
1042 + err = class_device_rename(&dev->class_dev, dev->name);
1044 + hlist_del(&dev->name_hlist);
1045 + hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
1046 + notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
1053 + * netdev_features_change - device changes fatures
1054 + * @dev: device to cause notification
1056 + * Called to indicate a device has changed features.
1058 +void netdev_features_change(struct net_device *dev)
1060 + notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
1062 +EXPORT_SYMBOL(netdev_features_change);
1065 + * netdev_state_change - device changes state
1066 + * @dev: device to cause notification
1068 + * Called to indicate a device has changed state. This function calls
1069 + * the notifier chains for netdev_chain and sends a NEWLINK message
1070 + * to the routing socket.
1072 +void netdev_state_change(struct net_device *dev)
1074 + if (dev->flags & IFF_UP) {
1075 + notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
1076 + rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
1081 + * dev_load - load a network module
1082 + * @name: name of interface
1084 + * If a network interface is not present and the process has suitable
1085 + * privileges this function loads the module. If module loading is not
1086 + * available in this kernel then it becomes a nop.
1089 +void dev_load(const char *name)
1091 + struct net_device *dev;
1093 + read_lock(&dev_base_lock);
1094 + dev = __dev_get_by_name(name);
1095 + read_unlock(&dev_base_lock);
1097 + if (!dev && capable(CAP_SYS_MODULE))
1098 + request_module("%s", name);
1101 +static int default_rebuild_header(struct sk_buff *skb)
1103 + printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
1104 + skb->dev ? skb->dev->name : "NULL!!!");
1111 + * dev_open - prepare an interface for use.
1112 + * @dev: device to open
1114 + * Takes a device from down to up state. The device's private open
1115 + * function is invoked and then the multicast lists are loaded. Finally
1116 + * the device is moved into the up state and a %NETDEV_UP message is
1117 + * sent to the netdev notifier chain.
1119 + * Calling this function on an active interface is a nop. On a failure
1120 + * a negative errno code is returned.
1122 +int dev_open(struct net_device *dev)
1127 + * Is it already up?
1130 + if (dev->flags & IFF_UP)
1134 + * Is it even present?
1136 + if (!netif_device_present(dev))
1140 + * Call device private open method
1142 + set_bit(__LINK_STATE_START, &dev->state);
1144 + ret = dev->open(dev);
1146 + clear_bit(__LINK_STATE_START, &dev->state);
1150 + * If it went open OK then:
1157 + dev->flags |= IFF_UP;
1160 + * Initialize multicasting status
1162 + dev_mc_upload(dev);
1165 + * Wakeup transmit queue engine
1167 + dev_activate(dev);
1170 + * ... and announce new interface.
1172 + notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
1178 + * dev_close - shutdown an interface.
1179 + * @dev: device to shutdown
1181 + * This function moves an active device into down state. A
1182 + * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1183 + * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1186 +int dev_close(struct net_device *dev)
1188 + if (!(dev->flags & IFF_UP))
1192 + * Tell people we are going down, so that they can
1193 + * prepare to death, when device is still operating.
1195 + notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
1197 + dev_deactivate(dev);
1199 + clear_bit(__LINK_STATE_START, &dev->state);
1201 + /* Synchronize to scheduled poll. We cannot touch poll list,
1202 + * it can be even on different cpu. So just clear netif_running(),
1203 + * and wait when poll really will happen. Actually, the best place
1204 + * for this is inside dev->stop() after device stopped its irq
1205 + * engine, but this requires more changes in devices. */
1207 + smp_mb__after_clear_bit(); /* Commit netif_running(). */
1208 + while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
1210 + current->state = TASK_INTERRUPTIBLE;
1211 + schedule_timeout(1);
1215 + * Call the device specific close. This cannot fail.
1216 + * Only if device is UP
1218 + * We allow it to be called even after a DETACH hot-plug
1225 + * Device is now down.
1228 + dev->flags &= ~IFF_UP;
1231 + * Tell people we are down
1233 + notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
1240 + * Device change register/unregister. These are not inline or static
1241 + * as we export them to the world.
1245 + * register_netdevice_notifier - register a network notifier block
1248 + * Register a notifier to be called when network device events occur.
1249 + * The notifier passed is linked into the kernel structures and must
1250 + * not be reused until it has been unregistered. A negative errno code
1251 + * is returned on a failure.
1253 + * When registered all registration and up events are replayed
1254 + * to the new notifier to allow device to have a race free
1255 + * view of the network device list.
1258 +int register_netdevice_notifier(struct notifier_block *nb)
1260 + struct net_device *dev;
1264 + err = notifier_chain_register(&netdev_chain, nb);
1266 + for (dev = dev_base; dev; dev = dev->next) {
1267 + nb->notifier_call(nb, NETDEV_REGISTER, dev);
1269 + if (dev->flags & IFF_UP)
1270 + nb->notifier_call(nb, NETDEV_UP, dev);
1278 + * unregister_netdevice_notifier - unregister a network notifier block
1281 + * Unregister a notifier previously registered by
1282 + * register_netdevice_notifier(). The notifier is unlinked into the
1283 + * kernel structures and may then be reused. A negative errno code
1284 + * is returned on a failure.
1287 +int unregister_netdevice_notifier(struct notifier_block *nb)
1289 + return notifier_chain_unregister(&netdev_chain, nb);
1293 + * call_netdevice_notifiers - call all network notifier blocks
1294 + * @val: value passed unmodified to notifier function
1295 + * @v: pointer passed unmodified to notifier function
1297 + * Call all network notifier blocks. Parameters and return value
1298 + * are as for notifier_call_chain().
1301 +int call_netdevice_notifiers(unsigned long val, void *v)
1303 + return notifier_call_chain(&netdev_chain, val, v);
1306 +/* When > 0 there are consumers of rx skb time stamps */
1307 +static atomic_t netstamp_needed = ATOMIC_INIT(0);
1309 +void net_enable_timestamp(void)
1311 + atomic_inc(&netstamp_needed);
1314 +void net_disable_timestamp(void)
1316 + atomic_dec(&netstamp_needed);
1319 +static inline void net_timestamp(struct timeval *stamp)
1321 + if (atomic_read(&netstamp_needed))
1322 + do_gettimeofday(stamp);
1324 + stamp->tv_sec = 0;
1325 + stamp->tv_usec = 0;
1330 + * Support routine. Sends outgoing frames to any network
1331 + * taps currently in use.
1334 +void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1336 + struct packet_type *ptype;
1337 + net_timestamp(&skb->stamp);
1340 + list_for_each_entry_rcu(ptype, &ptype_all, list) {
1341 + /* Never send packets back to the socket
1342 + * they originated from - MvS (miquels@drinkel.ow.org)
1344 + if ((ptype->dev == dev || !ptype->dev) &&
1345 + (ptype->af_packet_priv == NULL ||
1346 + (struct sock *)ptype->af_packet_priv != skb->sk)) {
1347 + struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1351 + /* skb->nh should be correctly
1352 + set by sender, so that the second statement is
1353 + just protection against buggy protocols.
1355 + skb2->mac.raw = skb2->data;
1357 + if (skb2->nh.raw < skb2->data ||
1358 + skb2->nh.raw > skb2->tail) {
1359 + if (net_ratelimit())
1360 + printk(KERN_CRIT "protocol %04x is "
1361 + "buggy, dev %s\n",
1362 + skb2->protocol, dev->name);
1363 + skb2->nh.raw = skb2->data;
1366 + skb2->h.raw = skb2->nh.raw;
1367 + skb2->pkt_type = PACKET_OUTGOING;
1368 + ptype->func(skb2, skb->dev, ptype);
1371 + rcu_read_unlock();
1375 + * Invalidate hardware checksum when packet is to be mangled, and
1376 + * complete checksum manually on outgoing path.
1378 +int skb_checksum_help(struct sk_buff *skb, int inward)
1380 + unsigned int csum;
1381 + int ret = 0, offset = skb->h.raw - skb->data;
1384 + skb->ip_summed = CHECKSUM_NONE;
1388 + if (skb_cloned(skb)) {
1389 + ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1394 + if (offset > (int)skb->len)
1396 + csum = skb_checksum(skb, offset, skb->len-offset, 0);
1398 + offset = skb->tail - skb->h.raw;
1401 + if (skb->csum + 2 > offset)
1404 + *(u16*)(skb->h.raw + skb->csum) = csum_fold(csum);
1405 + skb->ip_summed = CHECKSUM_NONE;
1410 +#ifdef CONFIG_HIGHMEM
1411 +/* Actually, we should eliminate this check as soon as we know, that:
1412 + * 1. IOMMU is present and allows to map all the memory.
1413 + * 2. No high memory really exists on this machine.
1416 +static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1420 + if (dev->features & NETIF_F_HIGHDMA)
1423 + for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1424 + if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1430 +#define illegal_highdma(dev, skb) (0)
1433 +extern void skb_release_data(struct sk_buff *);
1435 +/* Keep head the same: replace data */
1436 +int __skb_linearize(struct sk_buff *skb, int gfp_mask)
1438 + unsigned int size;
1441 + struct skb_shared_info *ninfo;
1442 + int headerlen = skb->data - skb->head;
1443 + int expand = (skb->tail + skb->data_len) - skb->end;
1445 + if (skb_shared(skb))
1451 + size = skb->end - skb->head + expand;
1452 + size = SKB_DATA_ALIGN(size);
1453 + data = kmalloc(size + sizeof(struct skb_shared_info), gfp_mask);
1457 + /* Copy entire thing */
1458 + if (skb_copy_bits(skb, -headerlen, data, headerlen + skb->len))
1461 + /* Set up shinfo */
1462 + ninfo = (struct skb_shared_info*)(data + size);
1463 + atomic_set(&ninfo->dataref, 1);
1464 + ninfo->tso_size = skb_shinfo(skb)->tso_size;
1465 + ninfo->tso_segs = skb_shinfo(skb)->tso_segs;
1466 + ninfo->nr_frags = 0;
1467 + ninfo->frag_list = NULL;
1469 + /* Offset between the two in bytes */
1470 + offset = data - skb->head;
1472 + /* Free old data. */
1473 + skb_release_data(skb);
1476 + skb->end = data + size;
1478 + /* Set up new pointers */
1479 + skb->h.raw += offset;
1480 + skb->nh.raw += offset;
1481 + skb->mac.raw += offset;
1482 + skb->tail += offset;
1483 + skb->data += offset;
1485 + /* We are no longer a clone, even if we were. */
1488 + skb->tail += skb->data_len;
1489 + skb->data_len = 0;
1493 +#define HARD_TX_LOCK(dev, cpu) { \
1494 + if ((dev->features & NETIF_F_LLTX) == 0) { \
1495 + spin_lock(&dev->xmit_lock); \
1496 + dev->xmit_lock_owner = cpu; \
1500 +#define HARD_TX_UNLOCK(dev) { \
1501 + if ((dev->features & NETIF_F_LLTX) == 0) { \
1502 + dev->xmit_lock_owner = -1; \
1503 + spin_unlock(&dev->xmit_lock); \
1508 + * dev_queue_xmit - transmit a buffer
1509 + * @skb: buffer to transmit
1511 + * Queue a buffer for transmission to a network device. The caller must
1512 + * have set the device and priority and built the buffer before calling
1513 + * this function. The function can be called from an interrupt.
1515 + * A negative errno code is returned on a failure. A success does not
1516 + * guarantee the frame will be transmitted as it may be dropped due
1517 + * to congestion or traffic shaping.
1519 + * -----------------------------------------------------------------------------------
1520 + * I notice this method can also return errors from the queue disciplines,
1521 + * including NET_XMIT_DROP, which is a positive value. So, errors can also
1524 + * Regardless of the return value, the skb is consumed, so it is currently
1525 + * difficult to retry a send to this method. (You can bump the ref count
1526 + * before sending to hold a reference for retry if you are careful.)
1528 + * When calling this method, interrupts MUST be enabled. This is because
1529 + * the BH enable code must have IRQs enabled so that it will not deadlock.
1533 +int dev_queue_xmit(struct sk_buff *skb)
1535 + struct net_device *dev = skb->dev;
1539 + if (skb_shinfo(skb)->frag_list &&
1540 + !(dev->features & NETIF_F_FRAGLIST) &&
1541 + __skb_linearize(skb, GFP_ATOMIC))
1542 + goto out_kfree_skb;
1544 + /* Fragmented skb is linearized if device does not support SG,
1545 + * or if at least one of fragments is in highmem and device
1546 + * does not support DMA from it.
1548 + if (skb_shinfo(skb)->nr_frags &&
1549 + (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
1550 + __skb_linearize(skb, GFP_ATOMIC))
1551 + goto out_kfree_skb;
1553 + /* If packet is not checksummed and device does not support
1554 + * checksumming for this protocol, complete checksumming here.
1556 + if (skb->ip_summed == CHECKSUM_HW &&
1557 + (!(dev->features & (NETIF_F_HW_CSUM | NETIF_F_NO_CSUM)) &&
1558 + (!(dev->features & NETIF_F_IP_CSUM) ||
1559 + skb->protocol != htons(ETH_P_IP))))
1560 + if (skb_checksum_help(skb, 0))
1561 + goto out_kfree_skb;
1563 + /* Disable soft irqs for various locks below. Also
1564 + * stops preemption for RCU.
1566 + local_bh_disable();
1568 + /* Updates of qdisc are serialized by queue_lock.
1569 + * The struct Qdisc which is pointed to by qdisc is now a
1570 + * rcu structure - it may be accessed without acquiring
1571 + * a lock (but the structure may be stale.) The freeing of the
1572 + * qdisc will be deferred until it's known that there are no
1573 + * more references to it.
1575 + * If the qdisc has an enqueue function, we still need to
1576 + * hold the queue_lock before calling it, since queue_lock
1577 + * also serializes access to the device queue.
1580 + q = rcu_dereference(dev->qdisc);
1581 +#ifdef CONFIG_NET_CLS_ACT
1582 + skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1585 + /* Grab device queue */
1586 + spin_lock(&dev->queue_lock);
1588 + rc = q->enqueue(skb, q);
1592 + spin_unlock(&dev->queue_lock);
1593 + rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1597 + /* The device has no queue. Common case for software devices:
1598 + loopback, all the sorts of tunnels...
1600 + Really, it is unlikely that xmit_lock protection is necessary here.
1601 + (f.e. loopback and IP tunnels are clean ignoring statistics
1603 + However, it is possible, that they rely on protection
1606 + Check this and shot the lock. It is not prone from deadlocks.
1607 + Either shot noqueue qdisc, it is even simpler 8)
1609 + if (dev->flags & IFF_UP) {
1610 + int cpu = smp_processor_id(); /* ok because BHs are off */
1612 + if (dev->xmit_lock_owner != cpu) {
1614 + HARD_TX_LOCK(dev, cpu);
1616 + if (!netif_queue_stopped(dev)) {
1618 + dev_queue_xmit_nit(skb, dev);
1621 + if (!dev->hard_start_xmit(skb, dev)) {
1622 + HARD_TX_UNLOCK(dev);
1626 + HARD_TX_UNLOCK(dev);
1627 + if (net_ratelimit())
1628 + printk(KERN_CRIT "Virtual device %s asks to "
1629 + "queue packet!\n", dev->name);
1631 + /* Recursion is detected! It is possible,
1632 + * unfortunately */
1633 + if (net_ratelimit())
1634 + printk(KERN_CRIT "Dead loop on virtual device "
1635 + "%s, fix it urgently!\n", dev->name);
1640 + local_bh_enable();
1646 + local_bh_enable();
1651 +/*=======================================================================
1653 + =======================================================================*/
1655 +int netdev_max_backlog = 300;
1656 +int weight_p = 64; /* old backlog weight */
1657 +/* These numbers are selected based on intuition and some
1658 + * experimentatiom, if you have more scientific way of doing this
1659 + * please go ahead and fix things.
1661 +int no_cong_thresh = 10;
1664 +int mod_cong = 290;
1666 +DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1669 +static void get_sample_stats(int cpu)
1675 + struct softnet_data *sd = &per_cpu(softnet_data, cpu);
1676 + int blog = sd->input_pkt_queue.qlen;
1677 + int avg_blog = sd->avg_blog;
1679 + avg_blog = (avg_blog >> 1) + (blog >> 1);
1681 + if (avg_blog > mod_cong) {
1682 + /* Above moderate congestion levels. */
1683 + sd->cng_level = NET_RX_CN_HIGH;
1685 + rd = net_random();
1686 + rq = rd % netdev_max_backlog;
1687 + if (rq < avg_blog) /* unlucky bastard */
1688 + sd->cng_level = NET_RX_DROP;
1690 + } else if (avg_blog > lo_cong) {
1691 + sd->cng_level = NET_RX_CN_MOD;
1693 + rd = net_random();
1694 + rq = rd % netdev_max_backlog;
1695 + if (rq < avg_blog) /* unlucky bastard */
1696 + sd->cng_level = NET_RX_CN_HIGH;
1698 + } else if (avg_blog > no_cong)
1699 + sd->cng_level = NET_RX_CN_LOW;
1700 + else /* no congestion */
1701 + sd->cng_level = NET_RX_SUCCESS;
1703 + sd->avg_blog = avg_blog;
1706 +#ifdef OFFLINE_SAMPLE
1707 +static void sample_queue(unsigned long dummy)
1709 +/* 10 ms 0r 1ms -- i don't care -- JHS */
1710 + int next_tick = 1;
1711 + int cpu = smp_processor_id();
1713 + get_sample_stats(cpu);
1714 + next_tick += jiffies;
1715 + mod_timer(&samp_timer, next_tick);
1721 + * netif_rx - post buffer to the network code
1722 + * @skb: buffer to post
1724 + * This function receives a packet from a device driver and queues it for
1725 + * the upper (protocol) levels to process. It always succeeds. The buffer
1726 + * may be dropped during processing for congestion control or by the
1727 + * protocol layers.
1730 + * NET_RX_SUCCESS (no congestion)
1731 + * NET_RX_CN_LOW (low congestion)
1732 + * NET_RX_CN_MOD (moderate congestion)
1733 + * NET_RX_CN_HIGH (high congestion)
1734 + * NET_RX_DROP (packet was dropped)
1738 +int netif_rx(struct sk_buff *skb)
1741 + struct softnet_data *queue;
1742 + unsigned long flags;
1744 + /* if netpoll wants it, pretend we never saw it */
1745 + if (netpoll_rx(skb))
1746 + return NET_RX_DROP;
1748 + if (!skb->stamp.tv_sec)
1749 + net_timestamp(&skb->stamp);
1752 + * The code is rearranged so that the path is the most
1753 + * short when CPU is congested, but is still operating.
1755 + local_irq_save(flags);
1756 + this_cpu = smp_processor_id();
1757 + queue = &__get_cpu_var(softnet_data);
1759 + __get_cpu_var(netdev_rx_stat).total++;
1760 + if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1761 + if (queue->input_pkt_queue.qlen) {
1762 + if (queue->throttle)
1766 + dev_hold(skb->dev);
1767 + __skb_queue_tail(&queue->input_pkt_queue, skb);
1768 +#ifndef OFFLINE_SAMPLE
1769 + get_sample_stats(this_cpu);
1771 + local_irq_restore(flags);
1772 + return queue->cng_level;
1775 + if (queue->throttle)
1776 + queue->throttle = 0;
1778 + netif_rx_schedule(&queue->backlog_dev);
1782 + if (!queue->throttle) {
1783 + queue->throttle = 1;
1784 + __get_cpu_var(netdev_rx_stat).throttled++;
1788 + __get_cpu_var(netdev_rx_stat).dropped++;
1789 + local_irq_restore(flags);
1792 + return NET_RX_DROP;
1795 +int netif_rx_ni(struct sk_buff *skb)
1799 + preempt_disable();
1800 + err = netif_rx(skb);
1801 + if (local_softirq_pending())
1808 +EXPORT_SYMBOL(netif_rx_ni);
1810 +static __inline__ void skb_bond(struct sk_buff *skb)
1812 + struct net_device *dev = skb->dev;
1814 + if (dev->master) {
1815 + skb->real_dev = skb->dev;
1816 + skb->dev = dev->master;
1820 +static void net_tx_action(struct softirq_action *h)
1822 + struct softnet_data *sd = &__get_cpu_var(softnet_data);
1824 + if (sd->completion_queue) {
1825 + struct sk_buff *clist;
1827 + local_irq_disable();
1828 + clist = sd->completion_queue;
1829 + sd->completion_queue = NULL;
1830 + local_irq_enable();
1833 + struct sk_buff *skb = clist;
1834 + clist = clist->next;
1836 + BUG_TRAP(!atomic_read(&skb->users));
1841 + if (sd->output_queue) {
1842 + struct net_device *head;
1844 + local_irq_disable();
1845 + head = sd->output_queue;
1846 + sd->output_queue = NULL;
1847 + local_irq_enable();
1850 + struct net_device *dev = head;
1851 + head = head->next_sched;
1853 + smp_mb__before_clear_bit();
1854 + clear_bit(__LINK_STATE_SCHED, &dev->state);
1856 + if (spin_trylock(&dev->queue_lock)) {
1858 + spin_unlock(&dev->queue_lock);
1860 + netif_schedule(dev);
1866 +static __inline__ int deliver_skb(struct sk_buff *skb,
1867 + struct packet_type *pt_prev)
1869 + atomic_inc(&skb->users);
1870 + return pt_prev->func(skb, skb->dev, pt_prev);
1873 +#if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
1874 +int (*br_handle_frame_hook)(struct net_bridge_port *p, struct sk_buff **pskb);
1876 +struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1877 + unsigned char *addr);
1878 +void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent);
1880 +static __inline__ int handle_bridge(struct sk_buff **pskb,
1881 + struct packet_type **pt_prev, int *ret)
1883 + struct net_bridge_port *port;
1885 + if ((*pskb)->pkt_type == PACKET_LOOPBACK ||
1886 + (port = rcu_dereference((*pskb)->dev->br_port)) == NULL)
1890 + *ret = deliver_skb(*pskb, *pt_prev);
1894 + return br_handle_frame_hook(port, pskb);
1897 +#define handle_bridge(skb, pt_prev, ret) (0)
1900 +#ifdef CONFIG_NET_CLS_ACT
1901 +/* TODO: Maybe we should just force sch_ingress to be compiled in
1902 + * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1903 + * a compare and 2 stores extra right now if we dont have it on
1904 + * but have CONFIG_NET_CLS_ACT
1905 + * NOTE: This doesnt stop any functionality; if you dont have
1906 + * the ingress scheduler, you just cant add policies on ingress.
1909 +static int ing_filter(struct sk_buff *skb)
1912 + struct net_device *dev = skb->dev;
1913 + int result = TC_ACT_OK;
1915 + if (dev->qdisc_ingress) {
1916 + __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
1917 + if (MAX_RED_LOOP < ttl++) {
1918 + printk("Redir loop detected Dropping packet (%s->%s)\n",
1919 + skb->input_dev?skb->input_dev->name:"??",skb->dev->name);
1920 + return TC_ACT_SHOT;
1923 + skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
1925 + skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
1926 + if (NULL == skb->input_dev) {
1927 + skb->input_dev = skb->dev;
1928 + printk("ing_filter: fixed %s out %s\n",skb->input_dev->name,skb->dev->name);
1930 + spin_lock(&dev->ingress_lock);
1931 + if ((q = dev->qdisc_ingress) != NULL)
1932 + result = q->enqueue(skb, q);
1933 + spin_unlock(&dev->ingress_lock);
1941 +int netif_receive_skb(struct sk_buff *skb)
1943 + struct packet_type *ptype, *pt_prev;
1944 + int ret = NET_RX_DROP;
1945 + unsigned short type;
1947 + /* if we've gotten here through NAPI, check netpoll */
1948 + if (skb->dev->poll && netpoll_rx(skb))
1949 + return NET_RX_DROP;
1951 + if (!skb->stamp.tv_sec)
1952 + net_timestamp(&skb->stamp);
1956 + __get_cpu_var(netdev_rx_stat).total++;
1958 + skb->h.raw = skb->nh.raw = skb->data;
1959 + skb->mac_len = skb->nh.raw - skb->mac.raw;
1965 +#ifdef CONFIG_NET_CLS_ACT
1966 + if (skb->tc_verd & TC_NCLS) {
1967 + skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1972 + list_for_each_entry_rcu(ptype, &ptype_all, list) {
1973 + if (!ptype->dev || ptype->dev == skb->dev) {
1975 + ret = deliver_skb(skb, pt_prev);
1980 +#ifdef CONFIG_NET_CLS_ACT
1982 + ret = deliver_skb(skb, pt_prev);
1983 + pt_prev = NULL; /* noone else should process this after*/
1985 + skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1988 + ret = ing_filter(skb);
1990 + if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
1999 + handle_diverter(skb);
2001 + if (handle_bridge(&skb, &pt_prev, &ret))
2004 + type = skb->protocol;
2005 + list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
2006 + if (ptype->type == type &&
2007 + (!ptype->dev || ptype->dev == skb->dev)) {
2009 + ret = deliver_skb(skb, pt_prev);
2015 + ret = pt_prev->func(skb, skb->dev, pt_prev);
2018 + /* Jamal, now you will not able to escape explaining
2019 + * me how you were going to use this. :-)
2021 + ret = NET_RX_DROP;
2025 + rcu_read_unlock();
2029 +static int process_backlog(struct net_device *backlog_dev, int *budget)
2032 + int quota = min(backlog_dev->quota, *budget);
2033 + struct softnet_data *queue = &__get_cpu_var(softnet_data);
2034 + unsigned long start_time = jiffies;
2036 + backlog_dev->weight = weight_p;
2038 + struct sk_buff *skb;
2039 + struct net_device *dev;
2041 + local_irq_disable();
2042 + skb = __skb_dequeue(&queue->input_pkt_queue);
2045 + local_irq_enable();
2049 + netif_receive_skb(skb);
2055 + if (work >= quota || jiffies - start_time > 1)
2060 + backlog_dev->quota -= work;
2065 + backlog_dev->quota -= work;
2068 + list_del(&backlog_dev->poll_list);
2069 + smp_mb__before_clear_bit();
2070 + netif_poll_enable(backlog_dev);
2072 + if (queue->throttle)
2073 + queue->throttle = 0;
2074 + local_irq_enable();
2078 +static void net_rx_action(struct softirq_action *h)
2080 + struct softnet_data *queue = &__get_cpu_var(softnet_data);
2081 + unsigned long start_time = jiffies;
2082 + int budget = netdev_max_backlog;
2085 + local_irq_disable();
2087 + while (!list_empty(&queue->poll_list)) {
2088 + struct net_device *dev;
2090 + if (budget <= 0 || jiffies - start_time > 1)
2091 + goto softnet_break;
2093 + local_irq_enable();
2095 + dev = list_entry(queue->poll_list.next,
2096 + struct net_device, poll_list);
2097 + netpoll_poll_lock(dev);
2099 + if (dev->quota <= 0 || dev->poll(dev, &budget)) {
2100 + netpoll_poll_unlock(dev);
2101 + local_irq_disable();
2102 + list_del(&dev->poll_list);
2103 + list_add_tail(&dev->poll_list, &queue->poll_list);
2104 + if (dev->quota < 0)
2105 + dev->quota += dev->weight;
2107 + dev->quota = dev->weight;
2109 + netpoll_poll_unlock(dev);
2111 + local_irq_disable();
2115 + local_irq_enable();
2119 + __get_cpu_var(netdev_rx_stat).time_squeeze++;
2120 + __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2124 +static gifconf_func_t * gifconf_list [NPROTO];
2127 + * register_gifconf - register a SIOCGIF handler
2128 + * @family: Address family
2129 + * @gifconf: Function handler
2131 + * Register protocol dependent address dumping routines. The handler
2132 + * that is passed must not be freed or reused until it has been replaced
2133 + * by another handler.
2135 +int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
2137 + if (family >= NPROTO)
2139 + gifconf_list[family] = gifconf;
2145 + * Map an interface index to its name (SIOCGIFNAME)
2149 + * We need this ioctl for efficient implementation of the
2150 + * if_indextoname() function required by the IPv6 API. Without
2151 + * it, we would have to search all the interfaces to find a
2155 +static int dev_ifname(struct ifreq __user *arg)
2157 + struct net_device *dev;
2161 + * Fetch the caller's info block.
2164 + if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2167 + read_lock(&dev_base_lock);
2168 + dev = __dev_get_by_index(ifr.ifr_ifindex);
2170 + read_unlock(&dev_base_lock);
2174 + strcpy(ifr.ifr_name, dev->name);
2175 + read_unlock(&dev_base_lock);
2177 + if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2183 + * Perform a SIOCGIFCONF call. This structure will change
2184 + * size eventually, and there is nothing I can do about it.
2185 + * Thus we will need a 'compatibility mode'.
2188 +static int dev_ifconf(char __user *arg)
2190 + struct ifconf ifc;
2191 + struct net_device *dev;
2198 + * Fetch the caller's info block.
2201 + if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2204 + pos = ifc.ifc_buf;
2205 + len = ifc.ifc_len;
2208 + * Loop over the interfaces, and write an info block for each.
2212 + for (dev = dev_base; dev; dev = dev->next) {
2213 + for (i = 0; i < NPROTO; i++) {
2214 + if (gifconf_list[i]) {
2217 + done = gifconf_list[i](dev, NULL, 0);
2219 + done = gifconf_list[i](dev, pos + total,
2229 + * All done. Write the updated control block back to the caller.
2231 + ifc.ifc_len = total;
2234 + * Both BSD and Solaris return 0 here, so we do too.
2236 + return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2239 +#ifdef CONFIG_PROC_FS
2241 + * This is invoked by the /proc filesystem handler to display a device
2244 +static __inline__ struct net_device *dev_get_idx(loff_t pos)
2246 + struct net_device *dev;
2249 + for (i = 0, dev = dev_base; dev && i < pos; ++i, dev = dev->next);
2251 + return i == pos ? dev : NULL;
2254 +void *dev_seq_start(struct seq_file *seq, loff_t *pos)
2256 + read_lock(&dev_base_lock);
2257 + return *pos ? dev_get_idx(*pos - 1) : SEQ_START_TOKEN;
2260 +void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2263 + return v == SEQ_START_TOKEN ? dev_base : ((struct net_device *)v)->next;
2266 +void dev_seq_stop(struct seq_file *seq, void *v)
2268 + read_unlock(&dev_base_lock);
2271 +static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2273 + if (dev->get_stats) {
2274 + struct net_device_stats *stats = dev->get_stats(dev);
2276 + seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2277 + "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2278 + dev->name, stats->rx_bytes, stats->rx_packets,
2280 + stats->rx_dropped + stats->rx_missed_errors,
2281 + stats->rx_fifo_errors,
2282 + stats->rx_length_errors + stats->rx_over_errors +
2283 + stats->rx_crc_errors + stats->rx_frame_errors,
2284 + stats->rx_compressed, stats->multicast,
2285 + stats->tx_bytes, stats->tx_packets,
2286 + stats->tx_errors, stats->tx_dropped,
2287 + stats->tx_fifo_errors, stats->collisions,
2288 + stats->tx_carrier_errors +
2289 + stats->tx_aborted_errors +
2290 + stats->tx_window_errors +
2291 + stats->tx_heartbeat_errors,
2292 + stats->tx_compressed);
2294 + seq_printf(seq, "%6s: No statistics available.\n", dev->name);
2298 + * Called from the PROCfs module. This now uses the new arbitrary sized
2299 + * /proc/net interface to create /proc/net/dev
2301 +static int dev_seq_show(struct seq_file *seq, void *v)
2303 + if (v == SEQ_START_TOKEN)
2304 + seq_puts(seq, "Inter-| Receive "
2306 + " face |bytes packets errs drop fifo frame "
2307 + "compressed multicast|bytes packets errs "
2308 + "drop fifo colls carrier compressed\n");
2310 + dev_seq_printf_stats(seq, v);
2314 +static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2316 + struct netif_rx_stats *rc = NULL;
2318 + while (*pos < NR_CPUS)
2319 + if (cpu_online(*pos)) {
2320 + rc = &per_cpu(netdev_rx_stat, *pos);
2327 +static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2329 + return softnet_get_online(pos);
2332 +static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2335 + return softnet_get_online(pos);
2338 +static void softnet_seq_stop(struct seq_file *seq, void *v)
2342 +static int softnet_seq_show(struct seq_file *seq, void *v)
2344 + struct netif_rx_stats *s = v;
2346 + seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
2347 + s->total, s->dropped, s->time_squeeze, s->throttled,
2348 + s->fastroute_hit, s->fastroute_success, s->fastroute_defer,
2349 + s->fastroute_deferred_out,
2351 + s->fastroute_latency_reduction
2359 +static struct seq_operations dev_seq_ops = {
2360 + .start = dev_seq_start,
2361 + .next = dev_seq_next,
2362 + .stop = dev_seq_stop,
2363 + .show = dev_seq_show,
2366 +static int dev_seq_open(struct inode *inode, struct file *file)
2368 + return seq_open(file, &dev_seq_ops);
2371 +static struct file_operations dev_seq_fops = {
2372 + .owner = THIS_MODULE,
2373 + .open = dev_seq_open,
2375 + .llseek = seq_lseek,
2376 + .release = seq_release,
2379 +static struct seq_operations softnet_seq_ops = {
2380 + .start = softnet_seq_start,
2381 + .next = softnet_seq_next,
2382 + .stop = softnet_seq_stop,
2383 + .show = softnet_seq_show,
2386 +static int softnet_seq_open(struct inode *inode, struct file *file)
2388 + return seq_open(file, &softnet_seq_ops);
2391 +static struct file_operations softnet_seq_fops = {
2392 + .owner = THIS_MODULE,
2393 + .open = softnet_seq_open,
2395 + .llseek = seq_lseek,
2396 + .release = seq_release,
2399 +#ifdef WIRELESS_EXT
2400 +extern int wireless_proc_init(void);
2402 +#define wireless_proc_init() 0
2405 +static int __init dev_proc_init(void)
2409 + if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
2411 + if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
2413 + if (wireless_proc_init())
2419 + proc_net_remove("softnet_stat");
2421 + proc_net_remove("dev");
2425 +#define dev_proc_init() 0
2426 +#endif /* CONFIG_PROC_FS */
2430 + * netdev_set_master - set up master/slave pair
2431 + * @slave: slave device
2432 + * @master: new master device
2434 + * Changes the master device of the slave. Pass %NULL to break the
2435 + * bonding. The caller must hold the RTNL semaphore. On a failure
2436 + * a negative errno code is returned. On success the reference counts
2437 + * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2438 + * function returns zero.
2440 +int netdev_set_master(struct net_device *slave, struct net_device *master)
2442 + struct net_device *old = slave->master;
2452 + slave->master = master;
2454 + synchronize_net();
2460 + slave->flags |= IFF_SLAVE;
2462 + slave->flags &= ~IFF_SLAVE;
2464 + rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2469 + * dev_set_promiscuity - update promiscuity count on a device
2473 + * Add or remove promsicuity from a device. While the count in the device
2474 + * remains above zero the interface remains promiscuous. Once it hits zero
2475 + * the device reverts back to normal filtering operation. A negative inc
2476 + * value is used to drop promiscuity on the device.
2478 +void dev_set_promiscuity(struct net_device *dev, int inc)
2480 + unsigned short old_flags = dev->flags;
2482 + dev->flags |= IFF_PROMISC;
2483 + if ((dev->promiscuity += inc) == 0)
2484 + dev->flags &= ~IFF_PROMISC;
2485 + if (dev->flags ^ old_flags) {
2486 + dev_mc_upload(dev);
2487 + printk(KERN_INFO "device %s %s promiscuous mode\n",
2488 + dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
2494 + * dev_set_allmulti - update allmulti count on a device
2498 + * Add or remove reception of all multicast frames to a device. While the
2499 + * count in the device remains above zero the interface remains listening
2500 + * to all interfaces. Once it hits zero the device reverts back to normal
2501 + * filtering operation. A negative @inc value is used to drop the counter
2502 + * when releasing a resource needing all multicasts.
2505 +void dev_set_allmulti(struct net_device *dev, int inc)
2507 + unsigned short old_flags = dev->flags;
2509 + dev->flags |= IFF_ALLMULTI;
2510 + if ((dev->allmulti += inc) == 0)
2511 + dev->flags &= ~IFF_ALLMULTI;
2512 + if (dev->flags ^ old_flags)
2513 + dev_mc_upload(dev);
2516 +unsigned dev_get_flags(const struct net_device *dev)
2520 + flags = (dev->flags & ~(IFF_PROMISC |
2523 + (dev->gflags & (IFF_PROMISC |
2526 + if (netif_running(dev) && netif_carrier_ok(dev))
2527 + flags |= IFF_RUNNING;
2532 +int dev_change_flags(struct net_device *dev, unsigned flags)
2535 + int old_flags = dev->flags;
2538 + * Set the flags on our device.
2541 + dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
2542 + IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
2544 + (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
2548 + * Load in the correct multicast list now the flags have changed.
2551 + dev_mc_upload(dev);
2554 + * Have we downed the interface. We handle IFF_UP ourselves
2555 + * according to user attempts to set it, rather than blindly
2560 + if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
2561 + ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2564 + dev_mc_upload(dev);
2567 + if (dev->flags & IFF_UP &&
2568 + ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
2570 + notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
2572 + if ((flags ^ dev->gflags) & IFF_PROMISC) {
2573 + int inc = (flags & IFF_PROMISC) ? +1 : -1;
2574 + dev->gflags ^= IFF_PROMISC;
2575 + dev_set_promiscuity(dev, inc);
2578 + /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
2579 + is important. Some (broken) drivers set IFF_PROMISC, when
2580 + IFF_ALLMULTI is requested not asking us and not reporting.
2582 + if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
2583 + int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
2584 + dev->gflags ^= IFF_ALLMULTI;
2585 + dev_set_allmulti(dev, inc);
2588 + if (old_flags ^ dev->flags)
2589 + rtmsg_ifinfo(RTM_NEWLINK, dev, old_flags ^ dev->flags);
2594 +int dev_set_mtu(struct net_device *dev, int new_mtu)
2598 + if (new_mtu == dev->mtu)
2601 + /* MTU must be positive. */
2605 + if (!netif_device_present(dev))
2609 + if (dev->change_mtu)
2610 + err = dev->change_mtu(dev, new_mtu);
2612 + dev->mtu = new_mtu;
2613 + if (!err && dev->flags & IFF_UP)
2614 + notifier_call_chain(&netdev_chain,
2615 + NETDEV_CHANGEMTU, dev);
2619 +int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
2623 + if (!dev->set_mac_address)
2624 + return -EOPNOTSUPP;
2625 + if (sa->sa_family != dev->type)
2627 + if (!netif_device_present(dev))
2629 + err = dev->set_mac_address(dev, sa);
2631 + notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
2636 + * Perform the SIOCxIFxxx calls.
2638 +static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2641 + struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
2647 + case SIOCGIFFLAGS: /* Get interface flags */
2648 + ifr->ifr_flags = dev_get_flags(dev);
2651 + case SIOCSIFFLAGS: /* Set interface flags */
2652 + return dev_change_flags(dev, ifr->ifr_flags);
2654 + case SIOCGIFMETRIC: /* Get the metric on the interface
2655 + (currently unused) */
2656 + ifr->ifr_metric = 0;
2659 + case SIOCSIFMETRIC: /* Set the metric on the interface
2660 + (currently unused) */
2661 + return -EOPNOTSUPP;
2663 + case SIOCGIFMTU: /* Get the MTU of a device */
2664 + ifr->ifr_mtu = dev->mtu;
2667 + case SIOCSIFMTU: /* Set the MTU of a device */
2668 + return dev_set_mtu(dev, ifr->ifr_mtu);
2670 + case SIOCGIFHWADDR:
2671 + if (!dev->addr_len)
2672 + memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
2674 + memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
2675 + min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2676 + ifr->ifr_hwaddr.sa_family = dev->type;
2679 + case SIOCSIFHWADDR:
2680 + return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
2682 + case SIOCSIFHWBROADCAST:
2683 + if (ifr->ifr_hwaddr.sa_family != dev->type)
2685 + memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
2686 + min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2687 + notifier_call_chain(&netdev_chain,
2688 + NETDEV_CHANGEADDR, dev);
2692 + ifr->ifr_map.mem_start = dev->mem_start;
2693 + ifr->ifr_map.mem_end = dev->mem_end;
2694 + ifr->ifr_map.base_addr = dev->base_addr;
2695 + ifr->ifr_map.irq = dev->irq;
2696 + ifr->ifr_map.dma = dev->dma;
2697 + ifr->ifr_map.port = dev->if_port;
2701 + if (dev->set_config) {
2702 + if (!netif_device_present(dev))
2704 + return dev->set_config(dev, &ifr->ifr_map);
2706 + return -EOPNOTSUPP;
2708 + case SIOCADDMULTI:
2709 + if (!dev->set_multicast_list ||
2710 + ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2712 + if (!netif_device_present(dev))
2714 + return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
2715 + dev->addr_len, 1);
2717 + case SIOCDELMULTI:
2718 + if (!dev->set_multicast_list ||
2719 + ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2721 + if (!netif_device_present(dev))
2723 + return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
2724 + dev->addr_len, 1);
2726 + case SIOCGIFINDEX:
2727 + ifr->ifr_ifindex = dev->ifindex;
2730 + case SIOCGIFTXQLEN:
2731 + ifr->ifr_qlen = dev->tx_queue_len;
2734 + case SIOCSIFTXQLEN:
2735 + if (ifr->ifr_qlen < 0)
2737 + dev->tx_queue_len = ifr->ifr_qlen;
2741 + ifr->ifr_newname[IFNAMSIZ-1] = '\0';
2742 + return dev_change_name(dev, ifr->ifr_newname);
2745 + * Unknown or private ioctl
2749 + if ((cmd >= SIOCDEVPRIVATE &&
2750 + cmd <= SIOCDEVPRIVATE + 15) ||
2751 + cmd == SIOCBONDENSLAVE ||
2752 + cmd == SIOCBONDRELEASE ||
2753 + cmd == SIOCBONDSETHWADDR ||
2754 + cmd == SIOCBONDSLAVEINFOQUERY ||
2755 + cmd == SIOCBONDINFOQUERY ||
2756 + cmd == SIOCBONDCHANGEACTIVE ||
2757 + cmd == SIOCGMIIPHY ||
2758 + cmd == SIOCGMIIREG ||
2759 + cmd == SIOCSMIIREG ||
2760 + cmd == SIOCBRADDIF ||
2761 + cmd == SIOCBRDELIF ||
2762 + cmd == SIOCWANDEV) {
2763 + err = -EOPNOTSUPP;
2764 + if (dev->do_ioctl) {
2765 + if (netif_device_present(dev))
2766 + err = dev->do_ioctl(dev, ifr,
2779 + * This function handles all "interface"-type I/O control requests. The actual
2780 + * 'doing' part of this is dev_ifsioc above.
2784 + * dev_ioctl - network device ioctl
2785 + * @cmd: command to issue
2786 + * @arg: pointer to a struct ifreq in user space
2788 + * Issue ioctl functions to devices. This is normally called by the
2789 + * user space syscall interfaces but can sometimes be useful for
2790 + * other purposes. The return value is the return from the syscall if
2791 + * positive or a negative errno code on error.
2794 +int dev_ioctl(unsigned int cmd, void __user *arg)
2800 + /* One special case: SIOCGIFCONF takes ifconf argument
2801 + and requires shared lock, because it sleeps writing
2805 + if (cmd == SIOCGIFCONF) {
2807 + ret = dev_ifconf((char __user *) arg);
2811 + if (cmd == SIOCGIFNAME)
2812 + return dev_ifname((struct ifreq __user *)arg);
2814 + if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2817 + ifr.ifr_name[IFNAMSIZ-1] = 0;
2819 + colon = strchr(ifr.ifr_name, ':');
2824 + * See which interface the caller is talking about.
2829 + * These ioctl calls:
2830 + * - can be done by all.
2831 + * - atomic and do not require locking.
2832 + * - return a value
2834 + case SIOCGIFFLAGS:
2835 + case SIOCGIFMETRIC:
2837 + case SIOCGIFHWADDR:
2838 + case SIOCGIFSLAVE:
2840 + case SIOCGIFINDEX:
2841 + case SIOCGIFTXQLEN:
2842 + dev_load(ifr.ifr_name);
2843 + read_lock(&dev_base_lock);
2844 + ret = dev_ifsioc(&ifr, cmd);
2845 + read_unlock(&dev_base_lock);
2849 + if (copy_to_user(arg, &ifr,
2850 + sizeof(struct ifreq)))
2856 + dev_load(ifr.ifr_name);
2858 + ret = dev_ethtool(&ifr);
2863 + if (copy_to_user(arg, &ifr,
2864 + sizeof(struct ifreq)))
2870 + * These ioctl calls:
2871 + * - require superuser power.
2872 + * - require strict serialization.
2873 + * - return a value
2878 + if (!capable(CAP_NET_ADMIN))
2880 + dev_load(ifr.ifr_name);
2882 + ret = dev_ifsioc(&ifr, cmd);
2887 + if (copy_to_user(arg, &ifr,
2888 + sizeof(struct ifreq)))
2894 + * These ioctl calls:
2895 + * - require superuser power.
2896 + * - require strict serialization.
2897 + * - do not return a value
2899 + case SIOCSIFFLAGS:
2900 + case SIOCSIFMETRIC:
2903 + case SIOCSIFHWADDR:
2904 + case SIOCSIFSLAVE:
2905 + case SIOCADDMULTI:
2906 + case SIOCDELMULTI:
2907 + case SIOCSIFHWBROADCAST:
2908 + case SIOCSIFTXQLEN:
2910 + case SIOCBONDENSLAVE:
2911 + case SIOCBONDRELEASE:
2912 + case SIOCBONDSETHWADDR:
2913 + case SIOCBONDSLAVEINFOQUERY:
2914 + case SIOCBONDINFOQUERY:
2915 + case SIOCBONDCHANGEACTIVE:
2918 + if (!capable(CAP_NET_ADMIN))
2920 + dev_load(ifr.ifr_name);
2922 + ret = dev_ifsioc(&ifr, cmd);
2927 + /* Get the per device memory space. We can add this but
2928 + * currently do not support it */
2930 + /* Set the per device memory buffer space.
2931 + * Not applicable in our case */
2936 + * Unknown or private ioctl.
2939 + if (cmd == SIOCWANDEV ||
2940 + (cmd >= SIOCDEVPRIVATE &&
2941 + cmd <= SIOCDEVPRIVATE + 15)) {
2942 + dev_load(ifr.ifr_name);
2944 + ret = dev_ifsioc(&ifr, cmd);
2946 + if (!ret && copy_to_user(arg, &ifr,
2947 + sizeof(struct ifreq)))
2951 +#ifdef WIRELESS_EXT
2952 + /* Take care of Wireless Extensions */
2953 + if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
2954 + /* If command is `set a parameter', or
2955 + * `get the encoding parameters', check if
2956 + * the user has the right to do it */
2957 + if (IW_IS_SET(cmd) || cmd == SIOCGIWENCODE) {
2958 + if (!capable(CAP_NET_ADMIN))
2961 + dev_load(ifr.ifr_name);
2963 + /* Follow me in net/core/wireless.c */
2964 + ret = wireless_process_ioctl(&ifr, cmd);
2966 + if (IW_IS_GET(cmd) &&
2967 + copy_to_user(arg, &ifr,
2968 + sizeof(struct ifreq)))
2972 +#endif /* WIRELESS_EXT */
2979 + * dev_new_index - allocate an ifindex
2981 + * Returns a suitable unique value for a new device interface
2982 + * number. The caller must hold the rtnl semaphore or the
2983 + * dev_base_lock to be sure it remains unique.
2985 +static int dev_new_index(void)
2987 + static int ifindex;
2989 + if (++ifindex <= 0)
2991 + if (!__dev_get_by_index(ifindex))
2996 +static int dev_boot_phase = 1;
2998 +/* Delayed registration/unregisteration */
2999 +static DEFINE_SPINLOCK(net_todo_list_lock);
3000 +static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
3002 +static inline void net_set_todo(struct net_device *dev)
3004 + spin_lock(&net_todo_list_lock);
3005 + list_add_tail(&dev->todo_list, &net_todo_list);
3006 + spin_unlock(&net_todo_list_lock);
3010 + * register_netdevice - register a network device
3011 + * @dev: device to register
3013 + * Take a completed network device structure and add it to the kernel
3014 + * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3015 + * chain. 0 is returned on success. A negative errno code is returned
3016 + * on a failure to set up the device, or if the name is a duplicate.
3018 + * Callers must hold the rtnl semaphore. You may want
3019 + * register_netdev() instead of this.
3022 + * The locking appears insufficient to guarantee two parallel registers
3023 + * will not get the same name.
3026 +int register_netdevice(struct net_device *dev)
3028 + struct hlist_head *head;
3029 + struct hlist_node *p;
3032 + BUG_ON(dev_boot_phase);
3035 + /* When net_device's are persistent, this will be fatal. */
3036 + BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
3038 + spin_lock_init(&dev->queue_lock);
3039 + spin_lock_init(&dev->xmit_lock);
3040 + dev->xmit_lock_owner = -1;
3041 +#ifdef CONFIG_NET_CLS_ACT
3042 + spin_lock_init(&dev->ingress_lock);
3045 + ret = alloc_divert_blk(dev);
3051 + /* Init, if this function is available */
3053 + ret = dev->init(dev);
3061 + if (!dev_valid_name(dev->name)) {
3066 + dev->ifindex = dev_new_index();
3067 + if (dev->iflink == -1)
3068 + dev->iflink = dev->ifindex;
3070 + /* Check for existence of name */
3071 + head = dev_name_hash(dev->name);
3072 + hlist_for_each(p, head) {
3073 + struct net_device *d
3074 + = hlist_entry(p, struct net_device, name_hlist);
3075 + if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
3081 + /* Fix illegal SG+CSUM combinations. */
3082 + if ((dev->features & NETIF_F_SG) &&
3083 + !(dev->features & (NETIF_F_IP_CSUM |
3085 + NETIF_F_HW_CSUM))) {
3086 + printk("%s: Dropping NETIF_F_SG since no checksum feature.\n",
3088 + dev->features &= ~NETIF_F_SG;
3091 + /* TSO requires that SG is present as well. */
3092 + if ((dev->features & NETIF_F_TSO) &&
3093 + !(dev->features & NETIF_F_SG)) {
3094 + printk("%s: Dropping NETIF_F_TSO since no SG feature.\n",
3096 + dev->features &= ~NETIF_F_TSO;
3100 + * nil rebuild_header routine,
3101 + * that should be never called and used as just bug trap.
3104 + if (!dev->rebuild_header)
3105 + dev->rebuild_header = default_rebuild_header;
3108 + * Default initial state at registry is that the
3109 + * device is present.
3112 + set_bit(__LINK_STATE_PRESENT, &dev->state);
3115 + dev_init_scheduler(dev);
3116 + write_lock_bh(&dev_base_lock);
3118 + dev_tail = &dev->next;
3119 + hlist_add_head(&dev->name_hlist, head);
3120 + hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
3122 + dev->reg_state = NETREG_REGISTERING;
3123 + write_unlock_bh(&dev_base_lock);
3125 + /* Notify protocols, that a new device appeared. */
3126 + notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
3128 + /* Finish registration after unlock */
3129 + net_set_todo(dev);
3135 + free_divert_blk(dev);
3140 + * register_netdev - register a network device
3141 + * @dev: device to register
3143 + * Take a completed network device structure and add it to the kernel
3144 + * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3145 + * chain. 0 is returned on success. A negative errno code is returned
3146 + * on a failure to set up the device, or if the name is a duplicate.
3148 + * This is a wrapper around register_netdev that takes the rtnl semaphore
3149 + * and expands the device name if you passed a format string to
3152 +int register_netdev(struct net_device *dev)
3159 + * If the name is a format string the caller wants us to do a
3160 + * name allocation.
3162 + if (strchr(dev->name, '%')) {
3163 + err = dev_alloc_name(dev, dev->name);
3169 + * Back compatibility hook. Kill this one in 2.5
3171 + if (dev->name[0] == 0 || dev->name[0] == ' ') {
3172 + err = dev_alloc_name(dev, "eth%d");
3177 + err = register_netdevice(dev);
3182 +EXPORT_SYMBOL(register_netdev);
3185 + * netdev_wait_allrefs - wait until all references are gone.
3187 + * This is called when unregistering network devices.
3189 + * Any protocol or device that holds a reference should register
3190 + * for netdevice notification, and cleanup and put back the
3191 + * reference if they receive an UNREGISTER event.
3192 + * We can get stuck here if buggy protocols don't correctly
3195 +static void netdev_wait_allrefs(struct net_device *dev)
3197 + unsigned long rebroadcast_time, warning_time;
3199 + rebroadcast_time = warning_time = jiffies;
3200 + while (atomic_read(&dev->refcnt) != 0) {
3201 + if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
3204 + /* Rebroadcast unregister notification */
3205 + notifier_call_chain(&netdev_chain,
3206 + NETDEV_UNREGISTER, dev);
3208 + if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
3210 + /* We must not have linkwatch events
3211 + * pending on unregister. If this
3212 + * happens, we simply run the queue
3213 + * unscheduled, resulting in a noop
3214 + * for this device.
3216 + linkwatch_run_queue();
3221 + rebroadcast_time = jiffies;
3226 + if (time_after(jiffies, warning_time + 10 * HZ)) {
3227 + printk(KERN_EMERG "unregister_netdevice: "
3228 + "waiting for %s to become free. Usage "
3230 + dev->name, atomic_read(&dev->refcnt));
3231 + warning_time = jiffies;
3236 +/* The sequence is:
3240 + * register_netdevice(x1);
3241 + * register_netdevice(x2);
3243 + * unregister_netdevice(y1);
3244 + * unregister_netdevice(y2);
3247 + * free_netdev(y1);
3248 + * free_netdev(y2);
3250 + * We are invoked by rtnl_unlock() after it drops the semaphore.
3251 + * This allows us to deal with problems:
3252 + * 1) We can create/delete sysfs objects which invoke hotplug
3253 + * without deadlocking with linkwatch via keventd.
3254 + * 2) Since we run with the RTNL semaphore not held, we can sleep
3255 + * safely in order to wait for the netdev refcnt to drop to zero.
3257 +static DECLARE_MUTEX(net_todo_run_mutex);
3258 +void netdev_run_todo(void)
3260 + struct list_head list = LIST_HEAD_INIT(list);
3264 + /* Need to guard against multiple cpu's getting out of order. */
3265 + down(&net_todo_run_mutex);
3267 + /* Not safe to do outside the semaphore. We must not return
3268 + * until all unregister events invoked by the local processor
3269 + * have been completed (either by this todo run, or one on
3272 + if (list_empty(&net_todo_list))
3275 + /* Snapshot list, allow later requests */
3276 + spin_lock(&net_todo_list_lock);
3277 + list_splice_init(&net_todo_list, &list);
3278 + spin_unlock(&net_todo_list_lock);
3280 + while (!list_empty(&list)) {
3281 + struct net_device *dev
3282 + = list_entry(list.next, struct net_device, todo_list);
3283 + list_del(&dev->todo_list);
3285 + switch(dev->reg_state) {
3286 + case NETREG_REGISTERING:
3287 + err = netdev_register_sysfs(dev);
3289 + printk(KERN_ERR "%s: failed sysfs registration (%d)\n",
3291 + dev->reg_state = NETREG_REGISTERED;
3294 + case NETREG_UNREGISTERING:
3295 + netdev_unregister_sysfs(dev);
3296 + dev->reg_state = NETREG_UNREGISTERED;
3298 + netdev_wait_allrefs(dev);
3301 + BUG_ON(atomic_read(&dev->refcnt));
3302 + BUG_TRAP(!dev->ip_ptr);
3303 + BUG_TRAP(!dev->ip6_ptr);
3304 + BUG_TRAP(!dev->dn_ptr);
3307 + /* It must be the very last action,
3308 + * after this 'dev' may point to freed up memory.
3310 + if (dev->destructor)
3311 + dev->destructor(dev);
3315 + printk(KERN_ERR "network todo '%s' but state %d\n",
3316 + dev->name, dev->reg_state);
3322 + up(&net_todo_run_mutex);
3326 + * alloc_netdev - allocate network device
3327 + * @sizeof_priv: size of private data to allocate space for
3328 + * @name: device name format string
3329 + * @setup: callback to initialize device
3331 + * Allocates a struct net_device with private data area for driver use
3332 + * and performs basic initialization.
3334 +struct net_device *alloc_netdev(int sizeof_priv, const char *name,
3335 + void (*setup)(struct net_device *))
3338 + struct net_device *dev;
3341 + /* ensure 32-byte alignment of both the device and private area */
3342 + alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST;
3343 + alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
3345 + p = kmalloc(alloc_size, GFP_KERNEL);
3347 + printk(KERN_ERR "alloc_dev: Unable to allocate device.\n");
3350 + memset(p, 0, alloc_size);
3352 + dev = (struct net_device *)
3353 + (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
3354 + dev->padded = (char *)dev - (char *)p;
3357 + dev->priv = netdev_priv(dev);
3360 + strcpy(dev->name, name);
3363 +EXPORT_SYMBOL(alloc_netdev);
3366 + * free_netdev - free network device
3369 + * This function does the last stage of destroying an allocated device
3370 + * interface. The reference to the device object is released.
3371 + * If this is the last reference then it will be freed.
3373 +void free_netdev(struct net_device *dev)
3375 +#ifdef CONFIG_SYSFS
3376 + /* Compatiablity with error handling in drivers */
3377 + if (dev->reg_state == NETREG_UNINITIALIZED) {
3378 + kfree((char *)dev - dev->padded);
3382 + BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
3383 + dev->reg_state = NETREG_RELEASED;
3385 + /* will free via class release */
3386 + class_device_put(&dev->class_dev);
3388 + kfree((char *)dev - dev->padded);
3392 +/* Synchronize with packet receive processing. */
3393 +void synchronize_net(void)
3396 + synchronize_rcu();
3400 + * unregister_netdevice - remove device from the kernel
3403 + * This function shuts down a device interface and removes it
3404 + * from the kernel tables. On success 0 is returned, on a failure
3405 + * a negative errno code is returned.
3407 + * Callers must hold the rtnl semaphore. You may want
3408 + * unregister_netdev() instead of this.
3411 +int unregister_netdevice(struct net_device *dev)
3413 + struct net_device *d, **dp;
3415 + BUG_ON(dev_boot_phase);
3418 + /* Some devices call without registering for initialization unwind. */
3419 + if (dev->reg_state == NETREG_UNINITIALIZED) {
3420 + printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3421 + "was registered\n", dev->name, dev);
3425 + BUG_ON(dev->reg_state != NETREG_REGISTERED);
3427 + /* If device is running, close it first. */
3428 + if (dev->flags & IFF_UP)
3431 + /* And unlink it from device chain. */
3432 + for (dp = &dev_base; (d = *dp) != NULL; dp = &d->next) {
3434 + write_lock_bh(&dev_base_lock);
3435 + hlist_del(&dev->name_hlist);
3436 + hlist_del(&dev->index_hlist);
3437 + if (dev_tail == &dev->next)
3440 + write_unlock_bh(&dev_base_lock);
3445 + printk(KERN_ERR "unregister net_device: '%s' not found\n",
3450 + dev->reg_state = NETREG_UNREGISTERING;
3452 + synchronize_net();
3454 + /* Shutdown queueing discipline. */
3455 + dev_shutdown(dev);
3458 + /* Notify protocols, that we are about to destroy
3459 + this device. They should clean all the things.
3461 + notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
3464 + * Flush the multicast chain
3466 + dev_mc_discard(dev);
3471 + /* Notifier chain MUST detach us from master device. */
3472 + BUG_TRAP(!dev->master);
3474 + free_divert_blk(dev);
3476 + /* Finish processing unregister after unlock */
3477 + net_set_todo(dev);
3479 + synchronize_net();
3486 + * unregister_netdev - remove device from the kernel
3489 + * This function shuts down a device interface and removes it
3490 + * from the kernel tables. On success 0 is returned, on a failure
3491 + * a negative errno code is returned.
3493 + * This is just a wrapper for unregister_netdevice that takes
3494 + * the rtnl semaphore. In general you want to use this and not
3495 + * unregister_netdevice.
3497 +void unregister_netdev(struct net_device *dev)
3500 + unregister_netdevice(dev);
3504 +EXPORT_SYMBOL(unregister_netdev);
3506 +#ifdef CONFIG_HOTPLUG_CPU
3507 +static int dev_cpu_callback(struct notifier_block *nfb,
3508 + unsigned long action,
3511 + struct sk_buff **list_skb;
3512 + struct net_device **list_net;
3513 + struct sk_buff *skb;
3514 + unsigned int cpu, oldcpu = (unsigned long)ocpu;
3515 + struct softnet_data *sd, *oldsd;
3517 + if (action != CPU_DEAD)
3520 + local_irq_disable();
3521 + cpu = smp_processor_id();
3522 + sd = &per_cpu(softnet_data, cpu);
3523 + oldsd = &per_cpu(softnet_data, oldcpu);
3525 + /* Find end of our completion_queue. */
3526 + list_skb = &sd->completion_queue;
3528 + list_skb = &(*list_skb)->next;
3529 + /* Append completion queue from offline CPU. */
3530 + *list_skb = oldsd->completion_queue;
3531 + oldsd->completion_queue = NULL;
3533 + /* Find end of our output_queue. */
3534 + list_net = &sd->output_queue;
3536 + list_net = &(*list_net)->next_sched;
3537 + /* Append output queue from offline CPU. */
3538 + *list_net = oldsd->output_queue;
3539 + oldsd->output_queue = NULL;
3541 + raise_softirq_irqoff(NET_TX_SOFTIRQ);
3542 + local_irq_enable();
3544 + /* Process offline CPU's input_pkt_queue */
3545 + while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
3550 +#endif /* CONFIG_HOTPLUG_CPU */
3554 + * Initialize the DEV module. At boot time this walks the device list and
3555 + * unhooks any devices that fail to initialise (normally hardware not
3556 + * present) and leaves us with a valid list of present and active devices.
3561 + * This is called single threaded during boot, so no need
3562 + * to take the rtnl semaphore.
3564 +static int __init net_dev_init(void)
3566 + int i, rc = -ENOMEM;
3568 + BUG_ON(!dev_boot_phase);
3570 + net_random_init();
3572 + if (dev_proc_init())
3575 + if (netdev_sysfs_init())
3578 + INIT_LIST_HEAD(&ptype_all);
3579 + for (i = 0; i < 16; i++)
3580 + INIT_LIST_HEAD(&ptype_base[i]);
3582 + for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
3583 + INIT_HLIST_HEAD(&dev_name_head[i]);
3585 + for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
3586 + INIT_HLIST_HEAD(&dev_index_head[i]);
3589 + * Initialise the packet receive queues.
3592 + for (i = 0; i < NR_CPUS; i++) {
3593 + struct softnet_data *queue;
3595 + queue = &per_cpu(softnet_data, i);
3596 + skb_queue_head_init(&queue->input_pkt_queue);
3597 + queue->throttle = 0;
3598 + queue->cng_level = 0;
3599 + queue->avg_blog = 10; /* arbitrary non-zero */
3600 + queue->completion_queue = NULL;
3601 + INIT_LIST_HEAD(&queue->poll_list);
3602 + set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
3603 + queue->backlog_dev.weight = weight_p;
3604 + queue->backlog_dev.poll = process_backlog;
3605 + atomic_set(&queue->backlog_dev.refcnt, 1);
3608 +#ifdef OFFLINE_SAMPLE
3609 + samp_timer.expires = jiffies + (10 * HZ);
3610 + add_timer(&samp_timer);
3613 + dev_boot_phase = 0;
3615 + open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
3616 + open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
3618 + hotcpu_notifier(dev_cpu_callback, 0);
3626 +subsys_initcall(net_dev_init);
3628 +EXPORT_SYMBOL(__dev_get_by_index);
3629 +EXPORT_SYMBOL(__dev_get_by_name);
3630 +EXPORT_SYMBOL(__dev_remove_pack);
3631 +EXPORT_SYMBOL(__skb_linearize);
3632 +EXPORT_SYMBOL(dev_add_pack);
3633 +EXPORT_SYMBOL(dev_alloc_name);
3634 +EXPORT_SYMBOL(dev_close);
3635 +EXPORT_SYMBOL(dev_get_by_flags);
3636 +EXPORT_SYMBOL(dev_get_by_index);
3637 +EXPORT_SYMBOL(dev_get_by_name);
3638 +EXPORT_SYMBOL(dev_ioctl);
3639 +EXPORT_SYMBOL(dev_open);
3640 +EXPORT_SYMBOL(dev_queue_xmit);
3641 +EXPORT_SYMBOL(dev_remove_pack);
3642 +EXPORT_SYMBOL(dev_set_allmulti);
3643 +EXPORT_SYMBOL(dev_set_promiscuity);
3644 +EXPORT_SYMBOL(dev_change_flags);
3645 +EXPORT_SYMBOL(dev_set_mtu);
3646 +EXPORT_SYMBOL(dev_set_mac_address);
3647 +EXPORT_SYMBOL(free_netdev);
3648 +EXPORT_SYMBOL(netdev_boot_setup_check);
3649 +EXPORT_SYMBOL(netdev_set_master);
3650 +EXPORT_SYMBOL(netdev_state_change);
3651 +EXPORT_SYMBOL(netif_receive_skb);
3652 +EXPORT_SYMBOL(netif_rx);
3653 +EXPORT_SYMBOL(register_gifconf);
3654 +EXPORT_SYMBOL(register_netdevice);
3655 +EXPORT_SYMBOL(register_netdevice_notifier);
3656 +EXPORT_SYMBOL(skb_checksum_help);
3657 +EXPORT_SYMBOL(synchronize_net);
3658 +EXPORT_SYMBOL(unregister_netdevice);
3659 +EXPORT_SYMBOL(unregister_netdevice_notifier);
3660 +EXPORT_SYMBOL(net_enable_timestamp);
3661 +EXPORT_SYMBOL(net_disable_timestamp);
3662 +EXPORT_SYMBOL(dev_get_flags);
3664 +#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
3665 +EXPORT_SYMBOL(br_handle_frame_hook);
3666 +EXPORT_SYMBOL(br_fdb_get_hook);
3667 +EXPORT_SYMBOL(br_fdb_put_hook);
3671 +EXPORT_SYMBOL(dev_load);
3674 +EXPORT_PER_CPU_SYMBOL(softnet_data);
3675 diff --unified --recursive --new-file linux-2.6.12.5/net/ring/Kconfig linux-2.6.12.5-1-686-smp-ring3/net/ring/Kconfig
3676 --- linux-2.6.12.5/net/ring/Kconfig 1970-01-01 01:00:00.000000000 +0100
3677 +++ linux-2.6.12.5-1-686-smp-ring3/net/ring/Kconfig 2005-10-22 23:50:45.539482000 +0200
3680 + tristate "PF_RING sockets (EXPERIMENTAL)"
3681 + depends on EXPERIMENTAL
3683 + PF_RING socket family, optimized for packet capture.
3684 + If a PF_RING socket is bound to an adapter (via the bind() system
3685 + call), such adapter will be used in read-only mode until the socket
3686 + is destroyed. Whenever an incoming packet is received from the adapter
3687 + it will not passed to upper layers, but instead it is copied to a ring
3688 + buffer, which in turn is exported to user space applications via mmap.
3689 + Please refer to http://luca.ntop.org/Ring.pdf for more.
3691 + Say N unless you know what you are doing.
3693 diff --unified --recursive --new-file linux-2.6.12.5/net/ring/Makefile linux-2.6.12.5-1-686-smp-ring3/net/ring/Makefile
3694 --- linux-2.6.12.5/net/ring/Makefile 1970-01-01 01:00:00.000000000 +0100
3695 +++ linux-2.6.12.5-1-686-smp-ring3/net/ring/Makefile 2005-10-22 23:50:45.051451500 +0200
3698 +# Makefile for the ring driver.
3703 +ring-objs := ring_packet.o
3704 diff --unified --recursive --new-file linux-2.6.12.5/net/ring/ring_packet.c linux-2.6.12.5-1-686-smp-ring3/net/ring/ring_packet.c
3705 --- linux-2.6.12.5/net/ring/ring_packet.c 1970-01-01 01:00:00.000000000 +0100
3706 +++ linux-2.6.12.5-1-686-smp-ring3/net/ring/ring_packet.c 2005-10-22 23:50:45.159458250 +0200
3710 + * (C) 2004-05 - Luca Deri <deri@ntop.org>
3712 + * This code includes patches courtesy of
3713 + * - Jeff Randall <jrandall@nexvu.com>
3714 + * - Helmut Manck <helmut.manck@secunet.com>
3715 + * - Brad Doctor <bdoctor@ps-ax.com>
3719 +/* FIX: add an entry inside the /proc filesystem */
3721 +#include <linux/version.h>
3722 +#include <linux/config.h>
3723 +#include <linux/module.h>
3724 +#include <linux/kernel.h>
3725 +#include <linux/socket.h>
3726 +#include <linux/skbuff.h>
3727 +#include <linux/rtnetlink.h>
3728 +#include <linux/in.h>
3729 +#include <linux/in6.h>
3730 +#include <linux/init.h>
3731 +#include <linux/filter.h>
3732 +#include <linux/ring.h>
3733 +#include <linux/ip.h>
3734 +#include <linux/tcp.h>
3735 +#include <linux/udp.h>
3736 +#include <linux/list.h>
3737 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
3738 +#include <net/xfrm.h>
3740 +#include <linux/poll.h>
3742 +#include <net/sock.h>
3743 +#include <asm/io.h> /* needed for virt_to_phys() */
3745 +/* #define RING_DEBUG */
3747 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,11))
3748 +static inline int remap_page_range(struct vm_area_struct *vma,
3749 + unsigned long uvaddr,
3750 + unsigned long paddr,
3751 + unsigned long size,
3753 + return(remap_pfn_range(vma, uvaddr, paddr >> PAGE_SHIFT,
3758 +/* ************************************************* */
3760 +#define CLUSTER_LEN 8
3762 +struct ring_cluster {
3763 + u_short cluster_id; /* 0 = no cluster */
3764 + u_short num_cluster_elements;
3765 + enum cluster_type hashing_mode;
3766 + u_short hashing_id;
3767 + struct sock *sk[CLUSTER_LEN];
3768 + struct ring_cluster *next; /* NULL = last element of the cluster */
3771 +/* ************************************************* */
3773 +struct ring_element {
3774 + struct list_head list;
3778 +/* ************************************************* */
3781 + struct net_device *ring_netdev;
3784 + u_short cluster_id; /* 0 = no cluster */
3787 + struct net_device *reflector_dev;
3789 + /* Packet buffers */
3790 + unsigned long order;
3793 + unsigned long ring_memory;
3794 + FlowSlotInfo *slots_info; /* Basically it points to ring_memory */
3795 + char *ring_slots; /* Basically it points to ring_memory
3796 + +sizeof(FlowSlotInfo) */
3798 + /* Packet Sampling */
3799 + u_int pktToSample, sample_rate;
3802 + struct sk_filter *bpfFilter;
3805 + atomic_t num_ring_slots_waiters;
3806 + wait_queue_head_t ring_slots_waitqueue;
3807 + rwlock_t ring_index_lock;
3809 + /* Indexes (Internal) */
3810 + u_int insert_page_id, insert_slot_id;
3813 +/* ************************************************* */
3815 +/* List of all ring sockets. */
3816 +static struct list_head ring_table;
3818 +/* List of all clusters */
3819 +static struct ring_cluster *ring_cluster_list;
3821 +static rwlock_t ring_mgmt_lock = RW_LOCK_UNLOCKED;
3823 +/* ********************************** */
3826 +static struct proto_ops ring_ops;
3828 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11))
3829 +static struct proto ring_proto;
3832 +static int skb_ring_handler(struct sk_buff *skb, u_char recv_packet,
3834 +static int buffer_ring_handler(struct net_device *dev, char *data, int len);
3835 +static int remove_from_cluster(struct sock *sock, struct ring_opt *pfr);
3839 +/* ********************************** */
3842 +static u_int bucket_len = 128, num_slots = 4096, sample_rate = 1,
3843 + transparent_mode = 0, enable_tx_capture = 0;
3845 +MODULE_PARM(bucket_len, "i");
3846 +MODULE_PARM_DESC(bucket_len, "Number of ring buckets");
3847 +MODULE_PARM(num_slots, "i");
3848 +MODULE_PARM_DESC(num_slots, "Number of ring slots");
3849 +MODULE_PARM(sample_rate, "i");
3850 +MODULE_PARM_DESC(sample_rate, "Ring packet sample rate");
3851 +MODULE_PARM(transparent_mode, "i");
3852 +MODULE_PARM_DESC(transparent_mode,
3853 + "Set to 1 to set transparent mode "
3854 + "(slower but backwards compatible)");
3855 +MODULE_PARM(enable_tx_capture, "i");
3856 +MODULE_PARM_DESC(enable_tx_capture, "Set to 1 to capture outgoing packets");
3858 +/* ********************************** */
3860 +#define MIN_QUEUED_PKTS 64
3861 +#define MAX_QUEUE_LOOPS 64
3864 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
3865 +#define ring_sk_datatype(__sk) ((struct ring_opt *)__sk)
3866 +#define ring_sk(__sk) ((__sk)->sk_protinfo)
3868 +#define ring_sk_datatype(a) (a)
3869 +#define ring_sk(__sk) ((__sk)->protinfo.pf_ring)
3873 + int dev_queue_xmit(struct sk_buff *skb)
3875 + struct net_device *dev_get_by_name(const char *name)
3878 +/* ********************************** */
3880 +static void ring_sock_destruct(struct sock *sk) {
3882 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
3883 + skb_queue_purge(&sk->sk_receive_queue);
3885 + if (!sock_flag(sk, SOCK_DEAD)) {
3886 +#if defined(RING_DEBUG)
3887 + printk("Attempt to release alive ring socket: %p\n", sk);
3892 + BUG_TRAP(!atomic_read(&sk->sk_rmem_alloc));
3893 + BUG_TRAP(!atomic_read(&sk->sk_wmem_alloc));
3896 + BUG_TRAP(atomic_read(&sk->rmem_alloc)==0);
3897 + BUG_TRAP(atomic_read(&sk->wmem_alloc)==0);
3900 +#if defined(RING_DEBUG)
3901 + printk("Attempt to release alive ring socket: %p\n", sk);
3907 + kfree(ring_sk(sk));
3909 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
3910 + MOD_DEC_USE_COUNT;
3914 +/* ********************************** */
3918 + * store the sk in a new element and add it
3919 + * to the head of the list.
3921 +static inline void ring_insert(struct sock *sk) {
3922 + struct ring_element *next;
3924 +#if defined(RING_DEBUG)
3925 + printk("RING: ring_insert()\n");
3928 + next = kmalloc(sizeof(struct ring_element), GFP_ATOMIC);
3929 + if(next != NULL) {
3931 + write_lock_irq(&ring_mgmt_lock);
3932 + list_add(&next->list, &ring_table);
3933 + write_unlock_irq(&ring_mgmt_lock);
3935 + if (net_ratelimit())
3936 + printk("RING: could not kmalloc slot!!\n");
3940 +/* ********************************** */
3944 + * For each of the elements in the list:
3945 + * - check if this is the element we want to delete
3946 + * - if it is, remove it from the list, and free it.
3948 + * stop when we find the one we're looking for (break),
3949 + * or when we reach the end of the list.
3951 +static inline void ring_remove(struct sock *sk) {
3952 + struct list_head *ptr;
3953 + struct ring_element *entry;
3956 + for(ptr = ring_table.next; ptr != &ring_table; ptr = ptr->next) {
3957 + entry = list_entry(ptr, struct ring_element, list);
3959 + if(entry->sk == sk) {
3960 + write_lock_irq(&ring_mgmt_lock);
3963 + write_unlock_irq(&ring_mgmt_lock);
3970 +/* ********************************** */
3972 +static u_int32_t num_queued_pkts(struct ring_opt *pfr) {
3974 + if(pfr->ring_slots != NULL) {
3976 + u_int32_t tot_insert = pfr->slots_info->insert_idx,
3977 +#if defined(RING_DEBUG)
3978 + tot_read = pfr->slots_info->tot_read, tot_pkts;
3980 + tot_read = pfr->slots_info->tot_read;
3983 + if(tot_insert >= tot_read) {
3984 +#if defined(RING_DEBUG)
3985 + tot_pkts = tot_insert-tot_read;
3987 + return(tot_insert-tot_read);
3989 +#if defined(RING_DEBUG)
3990 + tot_pkts = ((u_int32_t)-1)+tot_insert-tot_read;
3992 + return(((u_int32_t)-1)+tot_insert-tot_read);
3995 +#if defined(RING_DEBUG)
3996 + printk("-> num_queued_pkts=%d [tot_insert=%d][tot_read=%d]\n",
3997 + tot_pkts, tot_insert, tot_read);
4004 +/* ********************************** */
4006 +static inline FlowSlot* get_insert_slot(struct ring_opt *pfr) {
4007 +#if defined(RING_DEBUG)
4008 + printk("get_insert_slot(%d)\n", pfr->slots_info->insert_idx);
4011 + if(pfr->ring_slots != NULL) {
4012 + FlowSlot *slot = (FlowSlot*)&(pfr->ring_slots[pfr->slots_info->insert_idx
4013 + *pfr->slots_info->slot_len]);
4019 +/* ********************************** */
4021 +static inline FlowSlot* get_remove_slot(struct ring_opt *pfr) {
4022 +#if defined(RING_DEBUG)
4023 + printk("get_remove_slot(%d)\n", pfr->slots_info->remove_idx);
4026 + if(pfr->ring_slots != NULL)
4027 + return((FlowSlot*)&(pfr->ring_slots[pfr->slots_info->remove_idx*
4028 + pfr->slots_info->slot_len]));
4033 +/* ********************************** */
4035 +static void add_skb_to_ring(struct sk_buff *skb,
4036 + struct ring_opt *pfr,
4037 + u_char recv_packet,
4038 + u_char real_skb /* 1=skb 0=faked skb */) {
4039 + FlowSlot *theSlot;
4043 + /* Hack for identifying a packet received by the e1000 */
4045 + displ = SKB_DISPLACEMENT;
4047 + displ = 0; /* Received by the e1000 wrapper */
4051 + write_lock(&pfr->ring_index_lock);
4052 + pfr->slots_info->tot_pkts++;
4053 + write_unlock(&pfr->ring_index_lock);
4055 + /* BPF Filtering (from af_packet.c) */
4056 + if(pfr->bpfFilter != NULL) {
4057 + unsigned res = 1, len;
4059 + len = skb->len-skb->data_len;
4061 + write_lock(&pfr->ring_index_lock);
4062 + skb->data -= displ;
4063 + res = sk_run_filter(skb, pfr->bpfFilter->insns, pfr->bpfFilter->len);
4064 + skb->data += displ;
4065 + write_unlock(&pfr->ring_index_lock);
4068 + /* Filter failed */
4070 +#if defined(RING_DEBUG)
4071 + printk("add_skb_to_ring(skb): Filter failed [len=%d][tot=%llu]"
4072 + "[insertIdx=%d][pkt_type=%d][cloned=%d]\n",
4073 + (int)skb->len, pfr->slots_info->tot_pkts,
4074 + pfr->slots_info->insert_idx,
4075 + skb->pkt_type, skb->cloned);
4082 + /* ************************** */
4084 + if(pfr->sample_rate > 1) {
4085 + if(pfr->pktToSample == 0) {
4086 + write_lock(&pfr->ring_index_lock);
4087 + pfr->pktToSample = pfr->sample_rate;
4088 + write_unlock(&pfr->ring_index_lock);
4090 + write_lock(&pfr->ring_index_lock);
4091 + pfr->pktToSample--;
4092 + write_unlock(&pfr->ring_index_lock);
4094 +#if defined(RING_DEBUG)
4095 + printk("add_skb_to_ring(skb): sampled packet [len=%d]"
4096 + "[tot=%llu][insertIdx=%d][pkt_type=%d][cloned=%d]\n",
4097 + (int)skb->len, pfr->slots_info->tot_pkts,
4098 + pfr->slots_info->insert_idx,
4099 + skb->pkt_type, skb->cloned);
4105 + /* ************************************* */
4107 + if((pfr->reflector_dev != NULL)
4108 + && (!netif_queue_stopped(pfr->reflector_dev))) {
4109 + int cpu = smp_processor_id();
4111 + /* increase reference counter so that this skb is not freed */
4112 + atomic_inc(&skb->users);
4114 + skb->data -= displ;
4117 + if (pfr->reflector_dev->xmit_lock_owner != cpu) {
4118 + spin_lock_bh(&pfr->reflector_dev->xmit_lock);
4119 + pfr->reflector_dev->xmit_lock_owner = cpu;
4120 + spin_unlock_bh(&pfr->reflector_dev->xmit_lock);
4122 + if (pfr->reflector_dev->hard_start_xmit(skb,
4123 + pfr->reflector_dev) == 0) {
4124 + spin_lock_bh(&pfr->reflector_dev->xmit_lock);
4125 + pfr->reflector_dev->xmit_lock_owner = -1;
4126 + skb->data += displ;
4127 + spin_unlock_bh(&pfr->reflector_dev->xmit_lock);
4128 +#if defined(RING_DEBUG)
4129 + printk("++ hard_start_xmit succeeded\n");
4134 + spin_lock_bh(&pfr->reflector_dev->xmit_lock);
4135 + pfr->reflector_dev->xmit_lock_owner = -1;
4136 + spin_unlock_bh(&pfr->reflector_dev->xmit_lock);
4139 +#if defined(RING_DEBUG)
4140 + printk("++ hard_start_xmit failed\n");
4142 + skb->data += displ;
4143 + return; /* -ENETDOWN */
4146 + /* ************************************* */
4148 +#if defined(RING_DEBUG)
4149 + printk("add_skb_to_ring(skb) [len=%d][tot=%llu][insertIdx=%d]"
4150 + "[pkt_type=%d][cloned=%d]\n",
4151 + (int)skb->len, pfr->slots_info->tot_pkts,
4152 + pfr->slots_info->insert_idx,
4153 + skb->pkt_type, skb->cloned);
4156 + idx = pfr->slots_info->insert_idx;
4157 + theSlot = get_insert_slot(pfr);
4159 + if((theSlot != NULL) && (theSlot->slot_state == 0)) {
4160 + struct pcap_pkthdr *hdr;
4161 + unsigned int bucketSpace;
4164 + /* Update Index */
4167 + if(idx == pfr->slots_info->tot_slots) {
4168 + write_lock(&pfr->ring_index_lock);
4169 + pfr->slots_info->insert_idx = 0;
4170 + write_unlock(&pfr->ring_index_lock);
4172 + write_lock(&pfr->ring_index_lock);
4173 + pfr->slots_info->insert_idx = idx;
4174 + write_unlock(&pfr->ring_index_lock);
4177 + bucketSpace = pfr->slots_info->slot_len
4181 + - sizeof(u_char) /* flowSlot.slot_state */
4182 + - sizeof(struct pcap_pkthdr)
4183 + - 1 /* 10 */ /* safe boundary */;
4185 + bucket = &theSlot->bucket;
4186 + hdr = (struct pcap_pkthdr*)bucket;
4188 + if(skb->stamp.tv_sec == 0) do_gettimeofday(&skb->stamp);
4190 + hdr->ts.tv_sec = skb->stamp.tv_sec, hdr->ts.tv_usec = skb->stamp.tv_usec;
4191 + hdr->caplen = skb->len+displ;
4193 + if(hdr->caplen > bucketSpace)
4194 + hdr->caplen = bucketSpace;
4196 + hdr->len = skb->len+displ;
4197 + memcpy(&bucket[sizeof(struct pcap_pkthdr)],
4198 + skb->data-displ, hdr->caplen);
4200 +#if defined(RING_DEBUG)
4202 + static unsigned int lastLoss = 0;
4204 + if(pfr->slots_info->tot_lost
4205 + && (lastLoss != pfr->slots_info->tot_lost)) {
4206 + printk("add_skb_to_ring(%d): [bucketSpace=%d]"
4207 + "[hdr.caplen=%d][skb->len=%d]"
4208 + "[pcap_pkthdr=%d][removeIdx=%d]"
4209 + "[loss=%lu][page=%u][slot=%u]\n",
4210 + idx-1, bucketSpace, hdr->caplen, skb->len,
4211 + sizeof(struct pcap_pkthdr),
4212 + pfr->slots_info->remove_idx,
4213 + (long unsigned int)pfr->slots_info->tot_lost,
4214 + pfr->insert_page_id, pfr->insert_slot_id);
4216 + lastLoss = pfr->slots_info->tot_lost;
4221 + write_lock(&pfr->ring_index_lock);
4222 + pfr->slots_info->tot_insert++;
4223 + theSlot->slot_state = 1;
4224 + write_unlock(&pfr->ring_index_lock);
4226 + write_lock(&pfr->ring_index_lock);
4227 + pfr->slots_info->tot_lost++;
4228 + write_unlock(&pfr->ring_index_lock);
4230 +#if defined(RING_DEBUG)
4231 + printk("add_skb_to_ring(skb): packet lost [loss=%lu]"
4232 + "[removeIdx=%u][insertIdx=%u]\n",
4233 + (long unsigned int)pfr->slots_info->tot_lost,
4234 + pfr->slots_info->remove_idx, pfr->slots_info->insert_idx);
4238 + /* wakeup in case of poll() */
4239 + if(waitqueue_active(&pfr->ring_slots_waitqueue))
4240 + wake_up_interruptible(&pfr->ring_slots_waitqueue);
4243 +/* ********************************** */
4245 +static u_int hash_skb(struct ring_cluster *cluster_ptr,
4246 + struct sk_buff *skb, u_char recv_packet) {
4251 + if(cluster_ptr->hashing_mode == cluster_round_robin) {
4252 + idx = cluster_ptr->hashing_id++;
4254 + /* Per-flow clustering */
4255 + if(skb->len > sizeof(struct iphdr)+sizeof(struct tcphdr)) {
4259 + displ = SKB_DISPLACEMENT;
4264 + Always points to to the IP part of the packet
4267 + ip = (struct iphdr*)(skb->data+displ);
4269 + idx = ip->saddr+ip->daddr+ip->protocol;
4271 + if(ip->protocol == IPPROTO_TCP) {
4272 + struct tcphdr *tcp = (struct tcphdr*)(skb->data+displ
4273 + +sizeof(struct iphdr));
4274 + idx += tcp->source+tcp->dest;
4275 + } else if(ip->protocol == IPPROTO_UDP) {
4276 + struct udphdr *udp = (struct udphdr*)(skb->data+displ
4277 + +sizeof(struct iphdr));
4278 + idx += udp->source+udp->dest;
4284 + return(idx % cluster_ptr->num_cluster_elements);
4287 +/* ********************************** */
4289 +static int skb_ring_handler(struct sk_buff *skb,
4290 + u_char recv_packet,
4291 + u_char real_skb /* 1=skb 0=faked skb */) {
4292 + struct sock *skElement;
4294 + struct list_head *ptr;
4295 + struct ring_cluster *cluster_ptr;
4297 + if((!skb) /* Invalid skb */
4298 + || ((!enable_tx_capture) && (!recv_packet))) {
4300 + An outgoing packet is about to be sent out
4301 + but we decided not to handle transmitted
4307 +#if defined(RING_DEBUG)
4309 + printk("skb_ring_handler() [len=%d][dev=%s]\n", skb->len,
4310 + skb->dev->name == NULL ? "<NULL>" : skb->dev->name);
4314 + /* [1] Check unclustered sockets */
4315 + for (ptr = ring_table.next; ptr != &ring_table; ptr = ptr->next) {
4316 + struct ring_opt *pfr;
4317 + struct ring_element *entry;
4319 + entry = list_entry(ptr, struct ring_element, list);
4321 + read_lock(&ring_mgmt_lock);
4322 + skElement = entry->sk;
4323 + pfr = ring_sk(skElement);
4324 + read_unlock(&ring_mgmt_lock);
4327 + && (pfr->cluster_id == 0 /* No cluster */)
4328 + && (pfr->ring_slots != NULL)
4329 + && (pfr->ring_netdev == skb->dev)) {
4330 + /* We've found the ring where the packet can be stored */
4331 + read_lock(&ring_mgmt_lock);
4332 + add_skb_to_ring(skb, pfr, recv_packet, real_skb);
4333 + read_unlock(&ring_mgmt_lock);
4335 + rc = 1; /* Ring found: we've done our job */
4339 + /* [2] Check socket clusters */
4340 + cluster_ptr = ring_cluster_list;
4342 + while(cluster_ptr != NULL) {
4343 + struct ring_opt *pfr;
4345 + if(cluster_ptr->num_cluster_elements > 0) {
4346 + u_int skb_hash = hash_skb(cluster_ptr, skb, recv_packet);
4348 + read_lock(&ring_mgmt_lock);
4349 + skElement = cluster_ptr->sk[skb_hash];
4350 + read_unlock(&ring_mgmt_lock);
4352 + if(skElement != NULL) {
4353 + pfr = ring_sk(skElement);
4356 + && (pfr->ring_slots != NULL)
4357 + && (pfr->ring_netdev == skb->dev)) {
4358 + /* We've found the ring where the packet can be stored */
4359 + read_lock(&ring_mgmt_lock);
4360 + add_skb_to_ring(skb, pfr, recv_packet, real_skb);
4361 + read_unlock(&ring_mgmt_lock);
4363 + rc = 1; /* Ring found: we've done our job */
4368 + cluster_ptr = cluster_ptr->next;
4371 + if(transparent_mode) rc = 0;
4373 + if((rc != 0) && real_skb)
4374 + dev_kfree_skb(skb); /* Free the skb */
4376 + return(rc); /* 0 = packet not handled */
4379 +/* ********************************** */
4381 +struct sk_buff skb;
4383 +static int buffer_ring_handler(struct net_device *dev,
4384 + char *data, int len) {
4386 +#if defined(RING_DEBUG)
4387 + printk("buffer_ring_handler: [dev=%s][len=%d]\n",
4388 + dev->name == NULL ? "<NULL>" : dev->name, len);
4391 + skb.dev = dev, skb.len = len, skb.data = data,
4392 + skb.data_len = len, skb.stamp.tv_sec = 0; /* Calculate the time */
4394 + skb_ring_handler(&skb, 1, 0 /* fake skb */);
4399 +/* ********************************** */
4401 +static int ring_create(struct socket *sock, int protocol) {
4403 + struct ring_opt *pfr;
4406 +#if defined(RING_DEBUG)
4407 + printk("RING: ring_create()\n");
4410 + /* Are you root, superuser or so ? */
4411 + if(!capable(CAP_NET_ADMIN))
4414 + if(sock->type != SOCK_RAW)
4415 + return -ESOCKTNOSUPPORT;
4417 + if(protocol != htons(ETH_P_ALL))
4418 + return -EPROTONOSUPPORT;
4420 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
4421 + MOD_INC_USE_COUNT;
4426 + // BD: -- broke this out to keep it more simple and clear as to what the
4428 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4429 +#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11))
4430 + sk = sk_alloc(PF_RING, GFP_KERNEL, 1, NULL);
4434 + // BD: API changed in 2.6.12, ref:
4435 + // http://svn.clkao.org/svnweb/linux/revision/?rev=28201
4436 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11))
4437 + sk = sk_alloc(PF_RING, GFP_ATOMIC, &ring_proto, 1);
4443 + sock->ops = &ring_ops;
4444 + sock_init_data(sock, sk);
4445 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4446 +#if (LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,11))
4447 + sk_set_owner(sk, THIS_MODULE);
4452 + ring_sk(sk) = ring_sk_datatype(kmalloc(sizeof(*pfr), GFP_KERNEL));
4454 + if (!(pfr = ring_sk(sk))) {
4458 + memset(pfr, 0, sizeof(*pfr));
4459 + init_waitqueue_head(&pfr->ring_slots_waitqueue);
4460 + pfr->ring_index_lock = RW_LOCK_UNLOCKED;
4461 + atomic_set(&pfr->num_ring_slots_waiters, 0);
4462 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4463 + sk->sk_family = PF_RING;
4464 + sk->sk_destruct = ring_sock_destruct;
4466 + sk->family = PF_RING;
4467 + sk->destruct = ring_sock_destruct;
4468 + sk->num = protocol;
4473 +#if defined(RING_DEBUG)
4474 + printk("RING: ring_create() - created\n");
4479 +#if (LINUX_VERSION_CODE < KERNEL_VERSION(2,6,0))
4480 + MOD_DEC_USE_COUNT;
4485 +/* *********************************************** */
4487 +static int ring_release(struct socket *sock)
4489 + struct sock *sk = sock->sk;
4490 + struct ring_opt *pfr = ring_sk(sk);
4495 +#if defined(RING_DEBUG)
4496 + printk("RING: called ring_release\n");
4499 +#if defined(RING_DEBUG)
4500 + printk("RING: ring_release entered\n");
4508 + /* Free the ring buffer */
4509 + if(pfr->ring_memory) {
4510 + struct page *page, *page_end;
4512 + page_end = virt_to_page(pfr->ring_memory + (PAGE_SIZE << pfr->order) - 1);
4513 + for(page = virt_to_page(pfr->ring_memory); page <= page_end; page++)
4514 + ClearPageReserved(page);
4516 + free_pages(pfr->ring_memory, pfr->order);
4520 + ring_sk(sk) = NULL;
4522 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4523 + skb_queue_purge(&sk->sk_write_queue);
4527 +#if defined(RING_DEBUG)
4528 + printk("RING: ring_release leaving\n");
4534 +/* ********************************** */
4536 + * We create a ring for this socket and bind it to the specified device
4538 +static int packet_ring_bind(struct sock *sk, struct net_device *dev)
4540 + u_int the_slot_len;
4541 + u_int32_t tot_mem;
4542 + struct ring_opt *pfr = ring_sk(sk);
4543 + struct page *page, *page_end;
4545 + if(!dev) return(-1);
4547 +#if defined(RING_DEBUG)
4548 + printk("RING: packet_ring_bind(%s) called\n", dev->name);
4551 + /* **********************************************
4553 + *************************************
4557 + ************************************* <-+
4559 + ************************************* |
4561 + ************************************* +- num_slots
4563 + ************************************* |
4565 + ************************************* <-+
4567 + ********************************************** */
4569 + the_slot_len = sizeof(u_char) /* flowSlot.slot_state */
4570 + + sizeof(u_short) /* flowSlot.slot_len */
4571 + + bucket_len /* flowSlot.bucket */;
4573 + tot_mem = sizeof(FlowSlotInfo) + num_slots*the_slot_len;
4576 + Calculate the value of the order parameter used later.
4577 + See http://www.linuxjournal.com/article.php?sid=1133
4579 + for(pfr->order = 0;(PAGE_SIZE << pfr->order) < tot_mem; pfr->order++) ;
4582 + We now try to allocate the memory as required. If we fail
4583 + we try to allocate a smaller amount or memory (hence a
4586 + while((pfr->ring_memory = __get_free_pages(GFP_ATOMIC, pfr->order)) == 0)
4587 + if(pfr->order-- == 0)
4590 + if(pfr->order == 0) {
4591 +#if defined(RING_DEBUG)
4592 + printk("ERROR: not enough memory\n");
4596 +#if defined(RING_DEBUG)
4597 + printk("RING: succesfully allocated %lu KB [tot_mem=%d][order=%ld]\n",
4598 + PAGE_SIZE >> (10 - pfr->order), tot_mem, pfr->order);
4602 + tot_mem = PAGE_SIZE << pfr->order;
4603 + memset((char*)pfr->ring_memory, 0, tot_mem);
4605 + /* Now we need to reserve the pages */
4606 + page_end = virt_to_page(pfr->ring_memory + (PAGE_SIZE << pfr->order) - 1);
4607 + for(page = virt_to_page(pfr->ring_memory); page <= page_end; page++)
4608 + SetPageReserved(page);
4610 + pfr->slots_info = (FlowSlotInfo*)pfr->ring_memory;
4611 + pfr->ring_slots = (char*)(pfr->ring_memory+sizeof(FlowSlotInfo));
4613 + pfr->slots_info->version = RING_FLOWSLOT_VERSION;
4614 + pfr->slots_info->slot_len = the_slot_len;
4615 + pfr->slots_info->tot_slots = (tot_mem-sizeof(FlowSlotInfo))/the_slot_len;
4616 + pfr->slots_info->tot_mem = tot_mem;
4617 + pfr->slots_info->sample_rate = sample_rate;
4619 +#if defined(RING_DEBUG)
4620 + printk("RING: allocated %d slots [slot_len=%d][tot_mem=%u]\n",
4621 + pfr->slots_info->tot_slots, pfr->slots_info->slot_len,
4622 + pfr->slots_info->tot_mem);
4629 + for(i=0; i<pfr->slots_info->tot_slots; i++) {
4630 + unsigned long idx = i*pfr->slots_info->slot_len;
4631 + FlowSlot *slot = (FlowSlot*)&pfr->ring_slots[idx];
4632 + slot->magic = RING_MAGIC_VALUE; slot->slot_state = 0;
4637 + pfr->insert_page_id = 1, pfr->insert_slot_id = 0;
4641 + Leave this statement here as last one. In fact when
4642 + the ring_netdev != NULL the socket is ready to be used.
4644 + pfr->ring_netdev = dev;
4649 +/* ************************************* */
4651 +/* Bind to a device */
4652 +static int ring_bind(struct socket *sock,
4653 + struct sockaddr *sa, int addr_len)
4655 + struct sock *sk=sock->sk;
4656 + struct net_device *dev = NULL;
4658 +#if defined(RING_DEBUG)
4659 + printk("RING: ring_bind() called\n");
4665 + if (addr_len != sizeof(struct sockaddr))
4667 + if (sa->sa_family != PF_RING)
4670 + /* Safety check: add trailing zero if missing */
4671 + sa->sa_data[sizeof(sa->sa_data)-1] = '\0';
4673 +#if defined(RING_DEBUG)
4674 + printk("RING: searching device %s\n", sa->sa_data);
4677 + if((dev = __dev_get_by_name(sa->sa_data)) == NULL) {
4678 +#if defined(RING_DEBUG)
4679 + printk("RING: search failed\n");
4683 + return(packet_ring_bind(sk, dev));
4686 +/* ************************************* */
4688 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4690 +volatile void* virt_to_kseg(volatile void* address) {
4693 + unsigned long addr = (unsigned long)address;
4695 + pud = pud_offset(pgd_offset_k((unsigned long) address),
4696 + (unsigned long) address);
4699 + High-memory support courtesy of
4700 + Brad Doctor <bdoctor@ps-ax.com>
4702 +#if defined(CONFIG_X86_PAE) && (!defined(CONFIG_NOHIGHMEM))
4703 + pte = pte_offset_map(pmd_offset(pud, addr), addr);
4705 + pte = pmd_offset_map(pud, addr);
4708 + return((volatile void*)pte_page(*pte));
4713 +/* http://www.scs.ch/~frey/linux/memorymap.html */
4714 +volatile void *virt_to_kseg(volatile void *address)
4716 + pgd_t *pgd; pmd_t *pmd; pte_t *ptep, pte;
4717 + unsigned long va, ret = 0UL;
4719 + va=VMALLOC_VMADDR((unsigned long)address);
4721 + /* get the page directory. Use the kernel memory map. */
4722 + pgd = pgd_offset_k(va);
4724 + /* check whether we found an entry */
4725 + if (!pgd_none(*pgd))
4727 + /* get the page middle directory */
4728 + pmd = pmd_offset(pgd, va);
4729 + /* check whether we found an entry */
4730 + if (!pmd_none(*pmd))
4732 + /* get a pointer to the page table entry */
4733 + ptep = pte_offset(pmd, va);
4735 + /* check for a valid page */
4736 + if (pte_present(pte))
4738 + /* get the address the page is refering to */
4739 + ret = (unsigned long)page_address(pte_page(pte));
4740 + /* add the offset within the page to the page address */
4741 + ret |= (va & (PAGE_SIZE -1));
4745 + return((volatile void *)ret);
4749 +/* ************************************* */
4751 +static int ring_mmap(struct file *file,
4752 + struct socket *sock,
4753 + struct vm_area_struct *vma)
4755 + struct sock *sk = sock->sk;
4756 + struct ring_opt *pfr = ring_sk(sk);
4757 + unsigned long size, start;
4761 +#if defined(RING_DEBUG)
4762 + printk("RING: ring_mmap() called\n");
4765 + if(pfr->ring_memory == 0) {
4766 +#if defined(RING_DEBUG)
4767 + printk("RING: ring_mmap() failed: mapping area to an unbound socket\n");
4772 + size = (unsigned long)(vma->vm_end-vma->vm_start);
4774 + if(size % PAGE_SIZE) {
4775 +#if defined(RING_DEBUG)
4776 + printk("RING: ring_mmap() failed: len is not multiple of PAGE_SIZE\n");
4781 + /* if userspace tries to mmap beyond end of our buffer, fail */
4782 + if(size > pfr->slots_info->tot_mem) {
4783 +#if defined(RING_DEBUG)
4784 + printk("proc_mmap() failed: area too large [%ld > %d]\n", size, pfr->slots_info->tot_mem);
4789 + pagesToMap = size/PAGE_SIZE;
4791 +#if defined(RING_DEBUG)
4792 + printk("RING: ring_mmap() called. %d pages to map\n", pagesToMap);
4795 +#if defined(RING_DEBUG)
4796 + printk("RING: mmap [slot_len=%d][tot_slots=%d] for ring on device %s\n",
4797 + pfr->slots_info->slot_len, pfr->slots_info->tot_slots,
4798 + pfr->ring_netdev->name);
4801 + /* we do not want to have this area swapped out, lock it */
4802 + vma->vm_flags |= VM_LOCKED;
4803 + start = vma->vm_start;
4805 + /* Ring slots start from page 1 (page 0 is reserved for FlowSlotInfo) */
4806 + ptr = (char*)(start+PAGE_SIZE);
4808 + if(remap_page_range(
4809 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4813 + __pa(pfr->ring_memory),
4814 + PAGE_SIZE*pagesToMap, vma->vm_page_prot)) {
4815 +#if defined(RING_DEBUG)
4816 + printk("remap_page_range() failed\n");
4821 +#if defined(RING_DEBUG)
4822 + printk("proc_mmap(pagesToMap=%d): success.\n", pagesToMap);
4828 +/* ************************************* */
4830 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
4831 +static int ring_recvmsg(struct kiocb *iocb, struct socket *sock,
4832 + struct msghdr *msg, size_t len, int flags)
4834 + static int ring_recvmsg(struct socket *sock, struct msghdr *msg, int len,
4835 + int flags, struct scm_cookie *scm)
4839 + struct ring_opt *pfr = ring_sk(sock->sk);
4840 + u_int32_t queued_pkts, num_loops = 0;
4842 +#if defined(RING_DEBUG)
4843 + printk("ring_recvmsg called\n");
4846 + slot = get_remove_slot(pfr);
4848 + while((queued_pkts = num_queued_pkts(pfr)) < MIN_QUEUED_PKTS) {
4849 + wait_event_interruptible(pfr->ring_slots_waitqueue, 1);
4851 +#if defined(RING_DEBUG)
4852 + printk("-> ring_recvmsg returning %d [queued_pkts=%d][num_loops=%d]\n",
4853 + slot->slot_state, queued_pkts, num_loops);
4856 + if(queued_pkts > 0) {
4857 + if(num_loops++ > MAX_QUEUE_LOOPS)
4862 +#if defined(RING_DEBUG)
4864 + printk("ring_recvmsg is returning [queued_pkts=%d][num_loops=%d]\n",
4865 + queued_pkts, num_loops);
4868 + return(queued_pkts);
4871 +/* ************************************* */
4873 +unsigned int ring_poll(struct file * file,
4874 + struct socket *sock, poll_table *wait)
4877 + struct ring_opt *pfr = ring_sk(sock->sk);
4879 +#if defined(RING_DEBUG)
4880 + printk("poll called\n");
4883 + slot = get_remove_slot(pfr);
4885 + if((slot != NULL) && (slot->slot_state == 0))
4886 + poll_wait(file, &pfr->ring_slots_waitqueue, wait);
4888 +#if defined(RING_DEBUG)
4889 + printk("poll returning %d\n", slot->slot_state);
4892 + if((slot != NULL) && (slot->slot_state == 1))
4893 + return(POLLIN | POLLRDNORM);
4898 +/* ************************************* */
4900 +int add_to_cluster_list(struct ring_cluster *el,
4901 + struct sock *sock) {
4903 + if(el->num_cluster_elements == CLUSTER_LEN)
4904 + return(-1); /* Cluster full */
4906 + ring_sk_datatype(ring_sk(sock))->cluster_id = el->cluster_id;
4907 + el->sk[el->num_cluster_elements] = sock;
4908 + el->num_cluster_elements++;
4912 +/* ************************************* */
4914 +int remove_from_cluster_list(struct ring_cluster *el,
4915 + struct sock *sock) {
4918 + for(i=0; i<CLUSTER_LEN; i++)
4919 + if(el->sk[i] == sock) {
4920 + el->num_cluster_elements--;
4922 + if(el->num_cluster_elements > 0) {
4923 + /* The cluster contains other elements */
4924 + for(j=i; j<CLUSTER_LEN-1; j++)
4925 + el->sk[j] = el->sk[j+1];
4927 + el->sk[CLUSTER_LEN-1] = NULL;
4929 + /* Empty cluster */
4930 + memset(el->sk, 0, sizeof(el->sk));
4936 + return(-1); /* Not found */
4939 +/* ************************************* */
4941 +static int remove_from_cluster(struct sock *sock,
4942 + struct ring_opt *pfr)
4944 + struct ring_cluster *el;
4946 +#if defined(RING_DEBUG)
4947 + printk("--> remove_from_cluster(%d)\n", pfr->cluster_id);
4950 + if(pfr->cluster_id == 0 /* 0 = No Cluster */)
4951 + return(0); /* Noting to do */
4953 + el = ring_cluster_list;
4955 + while(el != NULL) {
4956 + if(el->cluster_id == pfr->cluster_id) {
4957 + return(remove_from_cluster_list(el, sock));
4962 + return(-EINVAL); /* Not found */
4965 +/* ************************************* */
4967 +static int add_to_cluster(struct sock *sock,
4968 + struct ring_opt *pfr,
4969 + u_short cluster_id)
4971 + struct ring_cluster *el;
4974 + printk("--> add_to_cluster(%d)\n", cluster_id);
4977 + if(cluster_id == 0 /* 0 = No Cluster */) return(-EINVAL);
4979 + if(pfr->cluster_id != 0)
4980 + remove_from_cluster(sock, pfr);
4982 + el = ring_cluster_list;
4984 + while(el != NULL) {
4985 + if(el->cluster_id == cluster_id) {
4986 + return(add_to_cluster_list(el, sock));
4991 + /* There's no existing cluster. We need to create one */
4992 + if((el = kmalloc(sizeof(struct ring_cluster), GFP_KERNEL)) == NULL)
4995 + el->cluster_id = cluster_id;
4996 + el->num_cluster_elements = 1;
4997 + el->hashing_mode = cluster_per_flow; /* Default */
4998 + el->hashing_id = 0;
5000 + memset(el->sk, 0, sizeof(el->sk));
5002 + el->next = ring_cluster_list;
5003 + ring_cluster_list = el;
5004 + pfr->cluster_id = cluster_id;
5006 + return(0); /* 0 = OK */
5009 +/* ************************************* */
5011 +/* Code taken/inspired from core/sock.c */
5012 +static int ring_setsockopt(struct socket *sock,
5013 + int level, int optname,
5014 + char *optval, int optlen)
5016 + struct ring_opt *pfr = ring_sk(sock->sk);
5017 + int val, found, ret = 0;
5021 + if((optlen<sizeof(int)) || (pfr == NULL))
5024 + if (get_user(val, (int *)optval))
5031 + case SO_ATTACH_FILTER:
5033 + if (optlen == sizeof(struct sock_fprog)) {
5034 + unsigned int fsize;
5035 + struct sock_fprog fprog;
5036 + struct sk_filter *filter;
5043 + Do not call copy_from_user within a held
5044 + splinlock (e.g. ring_mgmt_lock) as this caused
5045 + problems when certain debugging was enabled under
5046 + 2.6.5 -- including hard lockups of the machine.
5048 + if(copy_from_user(&fprog, optval, sizeof(fprog)))
5051 + fsize = sizeof(struct sock_filter) * fprog.len;
5052 + filter = kmalloc(fsize, GFP_KERNEL);
5054 + if(filter == NULL) {
5059 + if(copy_from_user(filter->insns, fprog.filter, fsize))
5062 + filter->len = fprog.len;
5064 + if(sk_chk_filter(filter->insns, filter->len) != 0) {
5065 + /* Bad filter specified */
5067 + pfr->bpfFilter = NULL;
5071 + /* get the lock, set the filter, release the lock */
5072 + write_lock(&ring_mgmt_lock);
5073 + pfr->bpfFilter = filter;
5074 + write_unlock(&ring_mgmt_lock);
5079 + case SO_DETACH_FILTER:
5080 + write_lock(&ring_mgmt_lock);
5082 + if(pfr->bpfFilter != NULL) {
5083 + kfree(pfr->bpfFilter);
5084 + pfr->bpfFilter = NULL;
5085 + write_unlock(&ring_mgmt_lock);
5091 + case SO_ADD_TO_CLUSTER:
5092 + if (optlen!=sizeof(val))
5095 + if (copy_from_user(&cluster_id, optval, sizeof(cluster_id)))
5098 + write_lock(&ring_mgmt_lock);
5099 + ret = add_to_cluster(sock->sk, pfr, cluster_id);
5100 + write_unlock(&ring_mgmt_lock);
5103 + case SO_REMOVE_FROM_CLUSTER:
5104 + write_lock(&ring_mgmt_lock);
5105 + ret = remove_from_cluster(sock->sk, pfr);
5106 + write_unlock(&ring_mgmt_lock);
5109 + case SO_SET_REFLECTOR:
5110 + if(optlen >= (sizeof(devName)-1))
5114 + if(copy_from_user(devName, optval, optlen))
5118 + devName[optlen] = '\0';
5120 +#if defined(RING_DEBUG)
5121 + printk("+++ SO_SET_REFLECTOR(%s)\n", devName);
5124 + write_lock(&ring_mgmt_lock);
5125 + pfr->reflector_dev = dev_get_by_name(devName);
5126 + write_unlock(&ring_mgmt_lock);
5128 +#if defined(RING_DEBUG)
5129 + if(pfr->reflector_dev != NULL)
5130 + printk("SO_SET_REFLECTOR(%s): succeded\n", devName);
5132 + printk("SO_SET_REFLECTOR(%s): device unknown\n", devName);
5144 + return(sock_setsockopt(sock, level, optname, optval, optlen));
5147 +/* ************************************* */
5149 +static int ring_ioctl(struct socket *sock,
5150 + unsigned int cmd, unsigned long arg)
5154 + case SIOCGIFFLAGS:
5155 + case SIOCSIFFLAGS:
5157 + case SIOCGIFMETRIC:
5158 + case SIOCSIFMETRIC:
5164 + case SIOCGIFHWADDR:
5165 + case SIOCSIFHWADDR:
5168 + case SIOCSIFSLAVE:
5169 + case SIOCGIFSLAVE:
5170 + case SIOCGIFINDEX:
5172 + case SIOCGIFCOUNT:
5173 + case SIOCSIFHWBROADCAST:
5174 + return(dev_ioctl(cmd,(void *) arg));
5177 + return -EOPNOTSUPP;
5183 +/* ************************************* */
5185 +static struct proto_ops ring_ops = {
5186 + .family = PF_RING,
5187 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
5188 + .owner = THIS_MODULE,
5191 + /* Operations that make no sense on ring sockets. */
5192 + .connect = sock_no_connect,
5193 + .socketpair = sock_no_socketpair,
5194 + .accept = sock_no_accept,
5195 + .getname = sock_no_getname,
5196 + .listen = sock_no_listen,
5197 + .shutdown = sock_no_shutdown,
5198 + .sendpage = sock_no_sendpage,
5199 + .sendmsg = sock_no_sendmsg,
5200 + .getsockopt = sock_no_getsockopt,
5202 + /* Now the operations that really occur. */
5203 + .release = ring_release,
5204 + .bind = ring_bind,
5205 + .mmap = ring_mmap,
5206 + .poll = ring_poll,
5207 + .setsockopt = ring_setsockopt,
5208 + .ioctl = ring_ioctl,
5209 + .recvmsg = ring_recvmsg,
5212 +/* ************************************ */
5214 +static struct net_proto_family ring_family_ops = {
5215 + .family = PF_RING,
5216 + .create = ring_create,
5217 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
5218 + .owner = THIS_MODULE,
5222 +// BD: API changed in 2.6.12, ref:
5223 +// http://svn.clkao.org/svnweb/linux/revision/?rev=28201
5224 +#if (LINUX_VERSION_CODE > KERNEL_VERSION(2,6,11))
5225 +static struct proto ring_proto = {
5226 + .name = "PF_RING",
5227 + .owner = THIS_MODULE,
5228 + .obj_size = sizeof(struct sock),
5232 +/* ************************************ */
5234 +static void __exit ring_exit(void)
5236 + struct list_head *ptr;
5237 + struct ring_element *entry;
5239 + for(ptr = ring_table.next; ptr != &ring_table; ptr = ptr->next) {
5240 + entry = list_entry(ptr, struct ring_element, list);
5244 + while(ring_cluster_list != NULL) {
5245 + struct ring_cluster *next = ring_cluster_list->next;
5246 + kfree(ring_cluster_list);
5247 + ring_cluster_list = next;
5250 + set_skb_ring_handler(NULL);
5251 + set_buffer_ring_handler(NULL);
5252 + sock_unregister(PF_RING);
5254 + printk("PF_RING shut down.\n");
5257 +/* ************************************ */
5259 +static int __init ring_init(void)
5261 + printk("Welcome to PF_RING %s\n(C) 2004 L.Deri <deri@ntop.org>\n",
5264 + INIT_LIST_HEAD(&ring_table);
5265 + ring_cluster_list = NULL;
5267 + sock_register(&ring_family_ops);
5269 + set_skb_ring_handler(skb_ring_handler);
5270 + set_buffer_ring_handler(buffer_ring_handler);
5272 + if(get_buffer_ring_handler() != buffer_ring_handler) {
5273 + printk("PF_RING: set_buffer_ring_handler FAILED\n");
5275 + set_skb_ring_handler(NULL);
5276 + set_buffer_ring_handler(NULL);
5277 + sock_unregister(PF_RING);
5280 + printk("PF_RING: bucket length %d bytes\n", bucket_len);
5281 + printk("PF_RING: ring slots %d\n", num_slots);
5282 + printk("PF_RING: sample rate %d [1=no sampling]\n", sample_rate);
5283 + printk("PF_RING: capture TX %s\n",
5284 + enable_tx_capture ? "Yes [RX+TX]" : "No [RX only]");
5285 + printk("PF_RING: transparent mode %s\n",
5286 + transparent_mode ? "Yes" : "No");
5288 + printk("PF_RING initialized correctly.\n");
5293 +module_init(ring_init);
5294 +module_exit(ring_exit);
5295 +MODULE_LICENSE("GPL");
5297 +#if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,0))
5298 +MODULE_ALIAS_NETPROTO(PF_RING);