2 * NET3 Protocol independent device support routines.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Derived from the non IP parts of dev.c 1.0.19
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
15 * Florian la Roche <rzsfl@rz.uni-sb.de>
16 * Alan Cox <gw4pts@gw4pts.ampr.org>
17 * David Hinds <dahinds@users.sourceforge.net>
18 * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru>
19 * Adam Sulmicki <adam@cfar.umd.edu>
20 * Pekka Riikonen <priikone@poesidon.pspt.fi>
23 * D.J. Barrow : Fixed bug where dev->refcnt gets set
24 * to 2 if register_netdev gets called
25 * before net_dev_init & also removed a
26 * few lines of code in the process.
27 * Alan Cox : device private ioctl copies fields back.
28 * Alan Cox : Transmit queue code does relevant
29 * stunts to keep the queue safe.
30 * Alan Cox : Fixed double lock.
31 * Alan Cox : Fixed promisc NULL pointer trap
32 * ???????? : Support the full private ioctl range
33 * Alan Cox : Moved ioctl permission check into
35 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
36 * Alan Cox : 100 backlog just doesn't cut it when
37 * you start doing multicast video 8)
38 * Alan Cox : Rewrote net_bh and list manager.
39 * Alan Cox : Fix ETH_P_ALL echoback lengths.
40 * Alan Cox : Took out transmit every packet pass
41 * Saved a few bytes in the ioctl handler
42 * Alan Cox : Network driver sets packet type before
43 * calling netif_rx. Saves a function
45 * Alan Cox : Hashed net_bh()
46 * Richard Kooijman: Timestamp fixes.
47 * Alan Cox : Wrong field in SIOCGIFDSTADDR
48 * Alan Cox : Device lock protection.
49 * Alan Cox : Fixed nasty side effect of device close
51 * Rudi Cilibrasi : Pass the right thing to
53 * Dave Miller : 32bit quantity for the device lock to
54 * make it work out on a Sparc.
55 * Bjorn Ekwall : Added KERNELD hack.
56 * Alan Cox : Cleaned up the backlog initialise.
57 * Craig Metz : SIOCGIFCONF fix if space for under
59 * Thomas Bogendoerfer : Return ENODEV for dev_open, if there
60 * is no device open function.
61 * Andi Kleen : Fix error reporting for SIOCGIFCONF
62 * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF
63 * Cyrus Durgin : Cleaned for KMOD
64 * Adam Sulmicki : Bug Fix : Network Device Unload
65 * A network device unload needs to purge
67 * Paul Rusty Russell : SIOCSIFNAME
68 * Pekka Riikonen : Netdev boot-time settings code
69 * Andrew Morton : Make unregister_netdevice wait
70 * indefinitely on dev->refcnt
71 * J Hadi Salim : - Backlog queue sampling
72 * - netif_rx() feedback
75 #include <asm/uaccess.h>
76 #include <asm/system.h>
77 #include <linux/bitops.h>
78 #include <linux/capability.h>
79 #include <linux/cpu.h>
80 #include <linux/types.h>
81 #include <linux/kernel.h>
82 #include <linux/sched.h>
83 #include <linux/mutex.h>
84 #include <linux/string.h>
86 #include <linux/socket.h>
87 #include <linux/sockios.h>
88 #include <linux/errno.h>
89 #include <linux/interrupt.h>
90 #include <linux/if_ether.h>
91 #include <linux/netdevice.h>
92 #include <linux/etherdevice.h>
93 #include <linux/notifier.h>
94 #include <linux/skbuff.h>
96 #include <linux/rtnetlink.h>
97 #include <linux/proc_fs.h>
98 #include <linux/seq_file.h>
99 #include <linux/stat.h>
100 #include <linux/if_bridge.h>
102 #include <net/pkt_sched.h>
103 #include <net/checksum.h>
104 #include <linux/highmem.h>
105 #include <linux/init.h>
106 #include <linux/kmod.h>
107 #include <linux/module.h>
108 #include <linux/kallsyms.h>
109 #include <linux/netpoll.h>
110 #include <linux/rcupdate.h>
111 #include <linux/delay.h>
112 #include <net/wext.h>
113 #include <net/iw_handler.h>
114 #include <asm/current.h>
115 #include <linux/audit.h>
116 #include <linux/dmaengine.h>
117 #include <linux/err.h>
118 #include <linux/ctype.h>
119 #include <linux/if_arp.h>
122 * The list of packet types we will receive (as opposed to discard)
123 * and the routines to invoke.
125 * Why 16. Because with 16 the only overlap we get on a hash of the
126 * low nibble of the protocol value is RARP/SNAP/X.25.
128 * NOTE: That is no longer true with the addition of VLAN tags. Not
129 * sure which should go first, but I bet it won't make much
130 * difference if we are running VLANs. The good news is that
131 * this protocol won't be in the list unless compiled in, so
132 * the average user (w/out VLANs) will not be adversely affected.
149 static DEFINE_SPINLOCK(ptype_lock);
150 static struct list_head ptype_base[16] __read_mostly; /* 16 way hashed list */
151 static struct list_head ptype_all __read_mostly; /* Taps */
153 #ifdef CONFIG_NET_DMA
154 static struct dma_client *net_dma_client;
155 static unsigned int net_dma_count;
156 static spinlock_t net_dma_event_lock;
160 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
163 * Pure readers hold dev_base_lock for reading.
165 * Writers must hold the rtnl semaphore while they loop through the
166 * dev_base_head list, and hold dev_base_lock for writing when they do the
167 * actual updates. This allows pure readers to access the list even
168 * while a writer is preparing to update it.
170 * To put it another way, dev_base_lock is held for writing only to
171 * protect against pure readers; the rtnl semaphore provides the
172 * protection against other writers.
174 * See, for example usages, register_netdevice() and
175 * unregister_netdevice(), which must be called with the rtnl
178 LIST_HEAD(dev_base_head);
179 DEFINE_RWLOCK(dev_base_lock);
181 EXPORT_SYMBOL(dev_base_head);
182 EXPORT_SYMBOL(dev_base_lock);
184 #define NETDEV_HASHBITS 8
185 static struct hlist_head dev_name_head[1<<NETDEV_HASHBITS];
186 static struct hlist_head dev_index_head[1<<NETDEV_HASHBITS];
188 static inline struct hlist_head *dev_name_hash(const char *name)
190 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
191 return &dev_name_head[hash & ((1<<NETDEV_HASHBITS)-1)];
194 static inline struct hlist_head *dev_index_hash(int ifindex)
196 return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)];
203 static RAW_NOTIFIER_HEAD(netdev_chain);
206 * Device drivers call our routines to queue packets here. We empty the
207 * queue in the local softnet handler.
209 DEFINE_PER_CPU(struct softnet_data, softnet_data) = { NULL };
212 extern int netdev_sysfs_init(void);
213 extern int netdev_register_sysfs(struct net_device *);
214 extern void netdev_unregister_sysfs(struct net_device *);
216 #define netdev_sysfs_init() (0)
217 #define netdev_register_sysfs(dev) (0)
218 #define netdev_unregister_sysfs(dev) do { } while(0)
221 #ifdef CONFIG_DEBUG_LOCK_ALLOC
223 * register_netdevice() inits dev->_xmit_lock and sets lockdep class
224 * according to dev->type
226 static const unsigned short netdev_lock_type[] =
227 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
228 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
229 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
230 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
231 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
232 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
233 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
234 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
235 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
236 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
237 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
238 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
239 ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211,
240 ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_VOID,
243 static const char *netdev_lock_name[] =
244 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
245 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
246 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
247 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
248 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
249 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
250 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
251 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
252 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
253 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
254 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
255 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
256 "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211",
257 "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_VOID",
260 static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
262 static inline unsigned short netdev_lock_pos(unsigned short dev_type)
266 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
267 if (netdev_lock_type[i] == dev_type)
269 /* the last key is used by default */
270 return ARRAY_SIZE(netdev_lock_type) - 1;
273 static inline void netdev_set_lockdep_class(spinlock_t *lock,
274 unsigned short dev_type)
278 i = netdev_lock_pos(dev_type);
279 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
280 netdev_lock_name[i]);
283 static inline void netdev_set_lockdep_class(spinlock_t *lock,
284 unsigned short dev_type)
289 /*******************************************************************************
291 Protocol management and registration routines
293 *******************************************************************************/
296 * Add a protocol ID to the list. Now that the input handler is
297 * smarter we can dispense with all the messy stuff that used to be
300 * BEWARE!!! Protocol handlers, mangling input packets,
301 * MUST BE last in hash buckets and checking protocol handlers
302 * MUST start from promiscuous ptype_all chain in net_bh.
303 * It is true now, do not change it.
304 * Explanation follows: if protocol handler, mangling packet, will
305 * be the first on list, it is not able to sense, that packet
306 * is cloned and should be copied-on-write, so that it will
307 * change it and subsequent readers will get broken packet.
312 * dev_add_pack - add packet handler
313 * @pt: packet type declaration
315 * Add a protocol handler to the networking stack. The passed &packet_type
316 * is linked into kernel lists and may not be freed until it has been
317 * removed from the kernel lists.
319 * This call does not sleep therefore it can not
320 * guarantee all CPU's that are in middle of receiving packets
321 * will see the new packet type (until the next received packet).
324 void dev_add_pack(struct packet_type *pt)
328 spin_lock_bh(&ptype_lock);
329 if (pt->type == htons(ETH_P_ALL))
330 list_add_rcu(&pt->list, &ptype_all);
332 hash = ntohs(pt->type) & 15;
333 list_add_rcu(&pt->list, &ptype_base[hash]);
335 spin_unlock_bh(&ptype_lock);
339 * __dev_remove_pack - remove packet handler
340 * @pt: packet type declaration
342 * Remove a protocol handler that was previously added to the kernel
343 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
344 * from the kernel lists and can be freed or reused once this function
347 * The packet type might still be in use by receivers
348 * and must not be freed until after all the CPU's have gone
349 * through a quiescent state.
351 void __dev_remove_pack(struct packet_type *pt)
353 struct list_head *head;
354 struct packet_type *pt1;
356 spin_lock_bh(&ptype_lock);
358 if (pt->type == htons(ETH_P_ALL))
361 head = &ptype_base[ntohs(pt->type) & 15];
363 list_for_each_entry(pt1, head, list) {
365 list_del_rcu(&pt->list);
370 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
372 spin_unlock_bh(&ptype_lock);
375 * dev_remove_pack - remove packet handler
376 * @pt: packet type declaration
378 * Remove a protocol handler that was previously added to the kernel
379 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
380 * from the kernel lists and can be freed or reused once this function
383 * This call sleeps to guarantee that no CPU is looking at the packet
386 void dev_remove_pack(struct packet_type *pt)
388 __dev_remove_pack(pt);
393 /******************************************************************************
395 Device Boot-time Settings Routines
397 *******************************************************************************/
399 /* Boot time configuration table */
400 static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
403 * netdev_boot_setup_add - add new setup entry
404 * @name: name of the device
405 * @map: configured settings for the device
407 * Adds new setup entry to the dev_boot_setup list. The function
408 * returns 0 on error and 1 on success. This is a generic routine to
411 static int netdev_boot_setup_add(char *name, struct ifmap *map)
413 struct netdev_boot_setup *s;
417 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
418 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
419 memset(s[i].name, 0, sizeof(s[i].name));
420 strcpy(s[i].name, name);
421 memcpy(&s[i].map, map, sizeof(s[i].map));
426 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
430 * netdev_boot_setup_check - check boot time settings
431 * @dev: the netdevice
433 * Check boot time settings for the device.
434 * The found settings are set for the device to be used
435 * later in the device probing.
436 * Returns 0 if no settings found, 1 if they are.
438 int netdev_boot_setup_check(struct net_device *dev)
440 struct netdev_boot_setup *s = dev_boot_setup;
443 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
444 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
445 !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
446 dev->irq = s[i].map.irq;
447 dev->base_addr = s[i].map.base_addr;
448 dev->mem_start = s[i].map.mem_start;
449 dev->mem_end = s[i].map.mem_end;
458 * netdev_boot_base - get address from boot time settings
459 * @prefix: prefix for network device
460 * @unit: id for network device
462 * Check boot time settings for the base address of device.
463 * The found settings are set for the device to be used
464 * later in the device probing.
465 * Returns 0 if no settings found.
467 unsigned long netdev_boot_base(const char *prefix, int unit)
469 const struct netdev_boot_setup *s = dev_boot_setup;
473 sprintf(name, "%s%d", prefix, unit);
476 * If device already registered then return base of 1
477 * to indicate not to probe for this interface
479 if (__dev_get_by_name(name))
482 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
483 if (!strcmp(name, s[i].name))
484 return s[i].map.base_addr;
489 * Saves at boot time configured settings for any netdevice.
491 int __init netdev_boot_setup(char *str)
496 str = get_options(str, ARRAY_SIZE(ints), ints);
501 memset(&map, 0, sizeof(map));
505 map.base_addr = ints[2];
507 map.mem_start = ints[3];
509 map.mem_end = ints[4];
511 /* Add new entry to the list */
512 return netdev_boot_setup_add(str, &map);
515 __setup("netdev=", netdev_boot_setup);
517 /*******************************************************************************
519 Device Interface Subroutines
521 *******************************************************************************/
524 * __dev_get_by_name - find a device by its name
525 * @name: name to find
527 * Find an interface by name. Must be called under RTNL semaphore
528 * or @dev_base_lock. If the name is found a pointer to the device
529 * is returned. If the name is not found then %NULL is returned. The
530 * reference counters are not incremented so the caller must be
531 * careful with locks.
534 struct net_device *__dev_get_by_name(const char *name)
536 struct hlist_node *p;
538 hlist_for_each(p, dev_name_hash(name)) {
539 struct net_device *dev
540 = hlist_entry(p, struct net_device, name_hlist);
541 if (!strncmp(dev->name, name, IFNAMSIZ))
548 * dev_get_by_name - find a device by its name
549 * @name: name to find
551 * Find an interface by name. This can be called from any
552 * context and does its own locking. The returned handle has
553 * the usage count incremented and the caller must use dev_put() to
554 * release it when it is no longer needed. %NULL is returned if no
555 * matching device is found.
558 struct net_device *dev_get_by_name(const char *name)
560 struct net_device *dev;
562 read_lock(&dev_base_lock);
563 dev = __dev_get_by_name(name);
566 read_unlock(&dev_base_lock);
571 * __dev_get_by_index - find a device by its ifindex
572 * @ifindex: index of device
574 * Search for an interface by index. Returns %NULL if the device
575 * is not found or a pointer to the device. The device has not
576 * had its reference counter increased so the caller must be careful
577 * about locking. The caller must hold either the RTNL semaphore
581 struct net_device *__dev_get_by_index(int ifindex)
583 struct hlist_node *p;
585 hlist_for_each(p, dev_index_hash(ifindex)) {
586 struct net_device *dev
587 = hlist_entry(p, struct net_device, index_hlist);
588 if (dev->ifindex == ifindex)
596 * dev_get_by_index - find a device by its ifindex
597 * @ifindex: index of device
599 * Search for an interface by index. Returns NULL if the device
600 * is not found or a pointer to the device. The device returned has
601 * had a reference added and the pointer is safe until the user calls
602 * dev_put to indicate they have finished with it.
605 struct net_device *dev_get_by_index(int ifindex)
607 struct net_device *dev;
609 read_lock(&dev_base_lock);
610 dev = __dev_get_by_index(ifindex);
613 read_unlock(&dev_base_lock);
618 * dev_getbyhwaddr - find a device by its hardware address
619 * @type: media type of device
620 * @ha: hardware address
622 * Search for an interface by MAC address. Returns NULL if the device
623 * is not found or a pointer to the device. The caller must hold the
624 * rtnl semaphore. The returned device has not had its ref count increased
625 * and the caller must therefore be careful about locking
628 * If the API was consistent this would be __dev_get_by_hwaddr
631 struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
633 struct net_device *dev;
638 if (dev->type == type &&
639 !memcmp(dev->dev_addr, ha, dev->addr_len))
645 EXPORT_SYMBOL(dev_getbyhwaddr);
647 struct net_device *__dev_getfirstbyhwtype(unsigned short type)
649 struct net_device *dev;
653 if (dev->type == type)
659 EXPORT_SYMBOL(__dev_getfirstbyhwtype);
661 struct net_device *dev_getfirstbyhwtype(unsigned short type)
663 struct net_device *dev;
666 dev = __dev_getfirstbyhwtype(type);
673 EXPORT_SYMBOL(dev_getfirstbyhwtype);
676 * dev_get_by_flags - find any device with given flags
677 * @if_flags: IFF_* values
678 * @mask: bitmask of bits in if_flags to check
680 * Search for any interface with the given flags. Returns NULL if a device
681 * is not found or a pointer to the device. The device returned has
682 * had a reference added and the pointer is safe until the user calls
683 * dev_put to indicate they have finished with it.
686 struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
688 struct net_device *dev, *ret;
691 read_lock(&dev_base_lock);
692 for_each_netdev(dev) {
693 if (((dev->flags ^ if_flags) & mask) == 0) {
699 read_unlock(&dev_base_lock);
704 * dev_valid_name - check if name is okay for network device
707 * Network device names need to be valid file names to
708 * to allow sysfs to work. We also disallow any kind of
711 int dev_valid_name(const char *name)
715 if (strlen(name) >= IFNAMSIZ)
717 if (!strcmp(name, ".") || !strcmp(name, ".."))
721 if (*name == '/' || isspace(*name))
729 * dev_alloc_name - allocate a name for a device
731 * @name: name format string
733 * Passed a format string - eg "lt%d" it will try and find a suitable
734 * id. It scans list of devices to build up a free map, then chooses
735 * the first empty slot. The caller must hold the dev_base or rtnl lock
736 * while allocating the name and adding the device in order to avoid
738 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
739 * Returns the number of the unit assigned or a negative errno code.
742 int dev_alloc_name(struct net_device *dev, const char *name)
747 const int max_netdevices = 8*PAGE_SIZE;
749 struct net_device *d;
751 p = strnchr(name, IFNAMSIZ-1, '%');
754 * Verify the string as this thing may have come from
755 * the user. There must be either one "%d" and no other "%"
758 if (p[1] != 'd' || strchr(p + 2, '%'))
761 /* Use one page as a bit array of possible slots */
762 inuse = (long *) get_zeroed_page(GFP_ATOMIC);
767 if (!sscanf(d->name, name, &i))
769 if (i < 0 || i >= max_netdevices)
772 /* avoid cases where sscanf is not exact inverse of printf */
773 snprintf(buf, sizeof(buf), name, i);
774 if (!strncmp(buf, d->name, IFNAMSIZ))
778 i = find_first_zero_bit(inuse, max_netdevices);
779 free_page((unsigned long) inuse);
782 snprintf(buf, sizeof(buf), name, i);
783 if (!__dev_get_by_name(buf)) {
784 strlcpy(dev->name, buf, IFNAMSIZ);
788 /* It is possible to run out of possible slots
789 * when the name is long and there isn't enough space left
790 * for the digits, or if all bits are used.
797 * dev_change_name - change name of a device
799 * @newname: name (or format string) must be at least IFNAMSIZ
801 * Change name of a device, can pass format strings "eth%d".
804 int dev_change_name(struct net_device *dev, char *newname)
810 if (dev->flags & IFF_UP)
813 if (!dev_valid_name(newname))
816 if (strchr(newname, '%')) {
817 err = dev_alloc_name(dev, newname);
820 strcpy(newname, dev->name);
822 else if (__dev_get_by_name(newname))
825 strlcpy(dev->name, newname, IFNAMSIZ);
827 device_rename(&dev->dev, dev->name);
828 hlist_del(&dev->name_hlist);
829 hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
830 raw_notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
836 * netdev_features_change - device changes features
837 * @dev: device to cause notification
839 * Called to indicate a device has changed features.
841 void netdev_features_change(struct net_device *dev)
843 raw_notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
845 EXPORT_SYMBOL(netdev_features_change);
848 * netdev_state_change - device changes state
849 * @dev: device to cause notification
851 * Called to indicate a device has changed state. This function calls
852 * the notifier chains for netdev_chain and sends a NEWLINK message
853 * to the routing socket.
855 void netdev_state_change(struct net_device *dev)
857 if (dev->flags & IFF_UP) {
858 raw_notifier_call_chain(&netdev_chain,
860 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
865 * dev_load - load a network module
866 * @name: name of interface
868 * If a network interface is not present and the process has suitable
869 * privileges this function loads the module. If module loading is not
870 * available in this kernel then it becomes a nop.
873 void dev_load(const char *name)
875 struct net_device *dev;
877 read_lock(&dev_base_lock);
878 dev = __dev_get_by_name(name);
879 read_unlock(&dev_base_lock);
881 if (!dev && capable(CAP_SYS_MODULE))
882 request_module("%s", name);
885 static int default_rebuild_header(struct sk_buff *skb)
887 printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
888 skb->dev ? skb->dev->name : "NULL!!!");
894 * dev_open - prepare an interface for use.
895 * @dev: device to open
897 * Takes a device from down to up state. The device's private open
898 * function is invoked and then the multicast lists are loaded. Finally
899 * the device is moved into the up state and a %NETDEV_UP message is
900 * sent to the netdev notifier chain.
902 * Calling this function on an active interface is a nop. On a failure
903 * a negative errno code is returned.
905 int dev_open(struct net_device *dev)
913 if (dev->flags & IFF_UP)
917 * Is it even present?
919 if (!netif_device_present(dev))
923 * Call device private open method
925 set_bit(__LINK_STATE_START, &dev->state);
927 ret = dev->open(dev);
929 clear_bit(__LINK_STATE_START, &dev->state);
933 * If it went open OK then:
940 dev->flags |= IFF_UP;
943 * Initialize multicasting status
945 dev_set_rx_mode(dev);
948 * Wakeup transmit queue engine
953 * ... and announce new interface.
955 raw_notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
961 * dev_close - shutdown an interface.
962 * @dev: device to shutdown
964 * This function moves an active device into down state. A
965 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
966 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
969 int dev_close(struct net_device *dev)
971 if (!(dev->flags & IFF_UP))
975 * Tell people we are going down, so that they can
976 * prepare to death, when device is still operating.
978 raw_notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
982 clear_bit(__LINK_STATE_START, &dev->state);
984 /* Synchronize to scheduled poll. We cannot touch poll list,
985 * it can be even on different cpu. So just clear netif_running(),
986 * and wait when poll really will happen. Actually, the best place
987 * for this is inside dev->stop() after device stopped its irq
988 * engine, but this requires more changes in devices. */
990 smp_mb__after_clear_bit(); /* Commit netif_running(). */
991 while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
997 * Call the device specific close. This cannot fail.
998 * Only if device is UP
1000 * We allow it to be called even after a DETACH hot-plug
1007 * Device is now down.
1010 dev->flags &= ~IFF_UP;
1013 * Tell people we are down
1015 raw_notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
1022 * Device change register/unregister. These are not inline or static
1023 * as we export them to the world.
1027 * register_netdevice_notifier - register a network notifier block
1030 * Register a notifier to be called when network device events occur.
1031 * The notifier passed is linked into the kernel structures and must
1032 * not be reused until it has been unregistered. A negative errno code
1033 * is returned on a failure.
1035 * When registered all registration and up events are replayed
1036 * to the new notifier to allow device to have a race free
1037 * view of the network device list.
1040 int register_netdevice_notifier(struct notifier_block *nb)
1042 struct net_device *dev;
1046 err = raw_notifier_chain_register(&netdev_chain, nb);
1048 for_each_netdev(dev) {
1049 nb->notifier_call(nb, NETDEV_REGISTER, dev);
1051 if (dev->flags & IFF_UP)
1052 nb->notifier_call(nb, NETDEV_UP, dev);
1060 * unregister_netdevice_notifier - unregister a network notifier block
1063 * Unregister a notifier previously registered by
1064 * register_netdevice_notifier(). The notifier is unlinked into the
1065 * kernel structures and may then be reused. A negative errno code
1066 * is returned on a failure.
1069 int unregister_netdevice_notifier(struct notifier_block *nb)
1074 err = raw_notifier_chain_unregister(&netdev_chain, nb);
1080 * call_netdevice_notifiers - call all network notifier blocks
1081 * @val: value passed unmodified to notifier function
1082 * @v: pointer passed unmodified to notifier function
1084 * Call all network notifier blocks. Parameters and return value
1085 * are as for raw_notifier_call_chain().
1088 int call_netdevice_notifiers(unsigned long val, void *v)
1090 return raw_notifier_call_chain(&netdev_chain, val, v);
1093 /* When > 0 there are consumers of rx skb time stamps */
1094 static atomic_t netstamp_needed = ATOMIC_INIT(0);
1096 void net_enable_timestamp(void)
1098 atomic_inc(&netstamp_needed);
1101 void net_disable_timestamp(void)
1103 atomic_dec(&netstamp_needed);
1106 static inline void net_timestamp(struct sk_buff *skb)
1108 if (atomic_read(&netstamp_needed))
1109 __net_timestamp(skb);
1111 skb->tstamp.tv64 = 0;
1115 * Support routine. Sends outgoing frames to any network
1116 * taps currently in use.
1119 static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1121 struct packet_type *ptype;
1126 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1127 /* Never send packets back to the socket
1128 * they originated from - MvS (miquels@drinkel.ow.org)
1130 if ((ptype->dev == dev || !ptype->dev) &&
1131 (ptype->af_packet_priv == NULL ||
1132 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1133 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1137 /* skb->nh should be correctly
1138 set by sender, so that the second statement is
1139 just protection against buggy protocols.
1141 skb_reset_mac_header(skb2);
1143 if (skb_network_header(skb2) < skb2->data ||
1144 skb2->network_header > skb2->tail) {
1145 if (net_ratelimit())
1146 printk(KERN_CRIT "protocol %04x is "
1148 skb2->protocol, dev->name);
1149 skb_reset_network_header(skb2);
1152 skb2->transport_header = skb2->network_header;
1153 skb2->pkt_type = PACKET_OUTGOING;
1154 ptype->func(skb2, skb->dev, ptype, skb->dev);
1161 void __netif_schedule(struct net_device *dev)
1163 if (!test_and_set_bit(__LINK_STATE_SCHED, &dev->state)) {
1164 unsigned long flags;
1165 struct softnet_data *sd;
1167 local_irq_save(flags);
1168 sd = &__get_cpu_var(softnet_data);
1169 dev->next_sched = sd->output_queue;
1170 sd->output_queue = dev;
1171 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1172 local_irq_restore(flags);
1175 EXPORT_SYMBOL(__netif_schedule);
1177 void __netif_rx_schedule(struct net_device *dev)
1179 unsigned long flags;
1181 local_irq_save(flags);
1183 list_add_tail(&dev->poll_list, &__get_cpu_var(softnet_data).poll_list);
1185 dev->quota += dev->weight;
1187 dev->quota = dev->weight;
1188 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
1189 local_irq_restore(flags);
1191 EXPORT_SYMBOL(__netif_rx_schedule);
1193 void dev_kfree_skb_any(struct sk_buff *skb)
1195 if (in_irq() || irqs_disabled())
1196 dev_kfree_skb_irq(skb);
1200 EXPORT_SYMBOL(dev_kfree_skb_any);
1204 void netif_device_detach(struct net_device *dev)
1206 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1207 netif_running(dev)) {
1208 netif_stop_queue(dev);
1211 EXPORT_SYMBOL(netif_device_detach);
1213 void netif_device_attach(struct net_device *dev)
1215 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1216 netif_running(dev)) {
1217 netif_wake_queue(dev);
1218 __netdev_watchdog_up(dev);
1221 EXPORT_SYMBOL(netif_device_attach);
1225 * Invalidate hardware checksum when packet is to be mangled, and
1226 * complete checksum manually on outgoing path.
1228 int skb_checksum_help(struct sk_buff *skb)
1231 int ret = 0, offset;
1233 if (skb->ip_summed == CHECKSUM_COMPLETE)
1234 goto out_set_summed;
1236 if (unlikely(skb_shinfo(skb)->gso_size)) {
1237 /* Let GSO fix up the checksum. */
1238 goto out_set_summed;
1241 if (skb_cloned(skb)) {
1242 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1247 offset = skb->csum_start - skb_headroom(skb);
1248 BUG_ON(offset > (int)skb->len);
1249 csum = skb_checksum(skb, offset, skb->len-offset, 0);
1251 offset = skb_headlen(skb) - offset;
1252 BUG_ON(offset <= 0);
1253 BUG_ON(skb->csum_offset + 2 > offset);
1255 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) =
1258 skb->ip_summed = CHECKSUM_NONE;
1264 * skb_gso_segment - Perform segmentation on skb.
1265 * @skb: buffer to segment
1266 * @features: features for the output path (see dev->features)
1268 * This function segments the given skb and returns a list of segments.
1270 * It may return NULL if the skb requires no segmentation. This is
1271 * only possible when GSO is used for verifying header integrity.
1273 struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
1275 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1276 struct packet_type *ptype;
1277 __be16 type = skb->protocol;
1280 BUG_ON(skb_shinfo(skb)->frag_list);
1282 skb_reset_mac_header(skb);
1283 skb->mac_len = skb->network_header - skb->mac_header;
1284 __skb_pull(skb, skb->mac_len);
1286 if (WARN_ON(skb->ip_summed != CHECKSUM_PARTIAL)) {
1287 if (skb_header_cloned(skb) &&
1288 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1289 return ERR_PTR(err);
1293 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type) & 15], list) {
1294 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
1295 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
1296 err = ptype->gso_send_check(skb);
1297 segs = ERR_PTR(err);
1298 if (err || skb_gso_ok(skb, features))
1300 __skb_push(skb, (skb->data -
1301 skb_network_header(skb)));
1303 segs = ptype->gso_segment(skb, features);
1309 __skb_push(skb, skb->data - skb_mac_header(skb));
1314 EXPORT_SYMBOL(skb_gso_segment);
1316 /* Take action when hardware reception checksum errors are detected. */
1318 void netdev_rx_csum_fault(struct net_device *dev)
1320 if (net_ratelimit()) {
1321 printk(KERN_ERR "%s: hw csum failure.\n",
1322 dev ? dev->name : "<unknown>");
1326 EXPORT_SYMBOL(netdev_rx_csum_fault);
1329 /* Actually, we should eliminate this check as soon as we know, that:
1330 * 1. IOMMU is present and allows to map all the memory.
1331 * 2. No high memory really exists on this machine.
1334 static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1336 #ifdef CONFIG_HIGHMEM
1339 if (dev->features & NETIF_F_HIGHDMA)
1342 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1343 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1351 void (*destructor)(struct sk_buff *skb);
1354 #define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1356 static void dev_gso_skb_destructor(struct sk_buff *skb)
1358 struct dev_gso_cb *cb;
1361 struct sk_buff *nskb = skb->next;
1363 skb->next = nskb->next;
1366 } while (skb->next);
1368 cb = DEV_GSO_CB(skb);
1370 cb->destructor(skb);
1374 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1375 * @skb: buffer to segment
1377 * This function segments the given skb and stores the list of segments
1380 static int dev_gso_segment(struct sk_buff *skb)
1382 struct net_device *dev = skb->dev;
1383 struct sk_buff *segs;
1384 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1387 segs = skb_gso_segment(skb, features);
1389 /* Verifying header integrity only. */
1393 if (unlikely(IS_ERR(segs)))
1394 return PTR_ERR(segs);
1397 DEV_GSO_CB(skb)->destructor = skb->destructor;
1398 skb->destructor = dev_gso_skb_destructor;
1403 int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1405 if (likely(!skb->next)) {
1406 if (!list_empty(&ptype_all))
1407 dev_queue_xmit_nit(skb, dev);
1409 if (netif_needs_gso(dev, skb)) {
1410 if (unlikely(dev_gso_segment(skb)))
1416 return dev->hard_start_xmit(skb, dev);
1421 struct sk_buff *nskb = skb->next;
1424 skb->next = nskb->next;
1426 rc = dev->hard_start_xmit(nskb, dev);
1428 nskb->next = skb->next;
1432 if (unlikely((netif_queue_stopped(dev) ||
1433 netif_subqueue_stopped(dev, skb->queue_mapping)) &&
1435 return NETDEV_TX_BUSY;
1436 } while (skb->next);
1438 skb->destructor = DEV_GSO_CB(skb)->destructor;
1445 #define HARD_TX_LOCK(dev, cpu) { \
1446 if ((dev->features & NETIF_F_LLTX) == 0) { \
1447 netif_tx_lock(dev); \
1451 #define HARD_TX_UNLOCK(dev) { \
1452 if ((dev->features & NETIF_F_LLTX) == 0) { \
1453 netif_tx_unlock(dev); \
1458 * dev_queue_xmit - transmit a buffer
1459 * @skb: buffer to transmit
1461 * Queue a buffer for transmission to a network device. The caller must
1462 * have set the device and priority and built the buffer before calling
1463 * this function. The function can be called from an interrupt.
1465 * A negative errno code is returned on a failure. A success does not
1466 * guarantee the frame will be transmitted as it may be dropped due
1467 * to congestion or traffic shaping.
1469 * -----------------------------------------------------------------------------------
1470 * I notice this method can also return errors from the queue disciplines,
1471 * including NET_XMIT_DROP, which is a positive value. So, errors can also
1474 * Regardless of the return value, the skb is consumed, so it is currently
1475 * difficult to retry a send to this method. (You can bump the ref count
1476 * before sending to hold a reference for retry if you are careful.)
1478 * When calling this method, interrupts MUST be enabled. This is because
1479 * the BH enable code must have IRQs enabled so that it will not deadlock.
1483 int dev_queue_xmit(struct sk_buff *skb)
1485 struct net_device *dev = skb->dev;
1489 /* GSO will handle the following emulations directly. */
1490 if (netif_needs_gso(dev, skb))
1493 if (skb_shinfo(skb)->frag_list &&
1494 !(dev->features & NETIF_F_FRAGLIST) &&
1495 __skb_linearize(skb))
1498 /* Fragmented skb is linearized if device does not support SG,
1499 * or if at least one of fragments is in highmem and device
1500 * does not support DMA from it.
1502 if (skb_shinfo(skb)->nr_frags &&
1503 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
1504 __skb_linearize(skb))
1507 /* If packet is not checksummed and device does not support
1508 * checksumming for this protocol, complete checksumming here.
1510 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1511 skb_set_transport_header(skb, skb->csum_start -
1514 if (!(dev->features & NETIF_F_GEN_CSUM) &&
1515 !((dev->features & NETIF_F_IP_CSUM) &&
1516 skb->protocol == htons(ETH_P_IP)) &&
1517 !((dev->features & NETIF_F_IPV6_CSUM) &&
1518 skb->protocol == htons(ETH_P_IPV6)))
1519 if (skb_checksum_help(skb))
1524 spin_lock_prefetch(&dev->queue_lock);
1526 /* Disable soft irqs for various locks below. Also
1527 * stops preemption for RCU.
1531 /* Updates of qdisc are serialized by queue_lock.
1532 * The struct Qdisc which is pointed to by qdisc is now a
1533 * rcu structure - it may be accessed without acquiring
1534 * a lock (but the structure may be stale.) The freeing of the
1535 * qdisc will be deferred until it's known that there are no
1536 * more references to it.
1538 * If the qdisc has an enqueue function, we still need to
1539 * hold the queue_lock before calling it, since queue_lock
1540 * also serializes access to the device queue.
1543 q = rcu_dereference(dev->qdisc);
1544 #ifdef CONFIG_NET_CLS_ACT
1545 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1548 /* Grab device queue */
1549 spin_lock(&dev->queue_lock);
1552 /* reset queue_mapping to zero */
1553 skb->queue_mapping = 0;
1554 rc = q->enqueue(skb, q);
1556 spin_unlock(&dev->queue_lock);
1558 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1561 spin_unlock(&dev->queue_lock);
1564 /* The device has no queue. Common case for software devices:
1565 loopback, all the sorts of tunnels...
1567 Really, it is unlikely that netif_tx_lock protection is necessary
1568 here. (f.e. loopback and IP tunnels are clean ignoring statistics
1570 However, it is possible, that they rely on protection
1573 Check this and shot the lock. It is not prone from deadlocks.
1574 Either shot noqueue qdisc, it is even simpler 8)
1576 if (dev->flags & IFF_UP) {
1577 int cpu = smp_processor_id(); /* ok because BHs are off */
1579 if (dev->xmit_lock_owner != cpu) {
1581 HARD_TX_LOCK(dev, cpu);
1583 if (!netif_queue_stopped(dev) &&
1584 !netif_subqueue_stopped(dev, skb->queue_mapping)) {
1586 if (!dev_hard_start_xmit(skb, dev)) {
1587 HARD_TX_UNLOCK(dev);
1591 HARD_TX_UNLOCK(dev);
1592 if (net_ratelimit())
1593 printk(KERN_CRIT "Virtual device %s asks to "
1594 "queue packet!\n", dev->name);
1596 /* Recursion is detected! It is possible,
1598 if (net_ratelimit())
1599 printk(KERN_CRIT "Dead loop on virtual device "
1600 "%s, fix it urgently!\n", dev->name);
1605 rcu_read_unlock_bh();
1611 rcu_read_unlock_bh();
1616 /*=======================================================================
1618 =======================================================================*/
1620 int netdev_max_backlog __read_mostly = 1000;
1621 int netdev_budget __read_mostly = 300;
1622 int weight_p __read_mostly = 64; /* old backlog weight */
1624 DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1628 * netif_rx - post buffer to the network code
1629 * @skb: buffer to post
1631 * This function receives a packet from a device driver and queues it for
1632 * the upper (protocol) levels to process. It always succeeds. The buffer
1633 * may be dropped during processing for congestion control or by the
1637 * NET_RX_SUCCESS (no congestion)
1638 * NET_RX_CN_LOW (low congestion)
1639 * NET_RX_CN_MOD (moderate congestion)
1640 * NET_RX_CN_HIGH (high congestion)
1641 * NET_RX_DROP (packet was dropped)
1645 int netif_rx(struct sk_buff *skb)
1647 struct softnet_data *queue;
1648 unsigned long flags;
1650 /* if netpoll wants it, pretend we never saw it */
1651 if (netpoll_rx(skb))
1654 if (!skb->tstamp.tv64)
1658 * The code is rearranged so that the path is the most
1659 * short when CPU is congested, but is still operating.
1661 local_irq_save(flags);
1662 queue = &__get_cpu_var(softnet_data);
1664 __get_cpu_var(netdev_rx_stat).total++;
1665 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1666 if (queue->input_pkt_queue.qlen) {
1669 __skb_queue_tail(&queue->input_pkt_queue, skb);
1670 local_irq_restore(flags);
1671 return NET_RX_SUCCESS;
1674 netif_rx_schedule(&queue->backlog_dev);
1678 __get_cpu_var(netdev_rx_stat).dropped++;
1679 local_irq_restore(flags);
1685 int netif_rx_ni(struct sk_buff *skb)
1690 err = netif_rx(skb);
1691 if (local_softirq_pending())
1698 EXPORT_SYMBOL(netif_rx_ni);
1700 static inline struct net_device *skb_bond(struct sk_buff *skb)
1702 struct net_device *dev = skb->dev;
1705 if (skb_bond_should_drop(skb)) {
1709 skb->dev = dev->master;
1715 static void net_tx_action(struct softirq_action *h)
1717 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1719 if (sd->completion_queue) {
1720 struct sk_buff *clist;
1722 local_irq_disable();
1723 clist = sd->completion_queue;
1724 sd->completion_queue = NULL;
1728 struct sk_buff *skb = clist;
1729 clist = clist->next;
1731 BUG_TRAP(!atomic_read(&skb->users));
1736 if (sd->output_queue) {
1737 struct net_device *head;
1739 local_irq_disable();
1740 head = sd->output_queue;
1741 sd->output_queue = NULL;
1745 struct net_device *dev = head;
1746 head = head->next_sched;
1748 smp_mb__before_clear_bit();
1749 clear_bit(__LINK_STATE_SCHED, &dev->state);
1751 if (spin_trylock(&dev->queue_lock)) {
1753 spin_unlock(&dev->queue_lock);
1755 netif_schedule(dev);
1761 static inline int deliver_skb(struct sk_buff *skb,
1762 struct packet_type *pt_prev,
1763 struct net_device *orig_dev)
1765 atomic_inc(&skb->users);
1766 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1769 #if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
1770 /* These hooks defined here for ATM */
1772 struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1773 unsigned char *addr);
1774 void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent) __read_mostly;
1777 * If bridge module is loaded call bridging hook.
1778 * returns NULL if packet was consumed.
1780 struct sk_buff *(*br_handle_frame_hook)(struct net_bridge_port *p,
1781 struct sk_buff *skb) __read_mostly;
1782 static inline struct sk_buff *handle_bridge(struct sk_buff *skb,
1783 struct packet_type **pt_prev, int *ret,
1784 struct net_device *orig_dev)
1786 struct net_bridge_port *port;
1788 if (skb->pkt_type == PACKET_LOOPBACK ||
1789 (port = rcu_dereference(skb->dev->br_port)) == NULL)
1793 *ret = deliver_skb(skb, *pt_prev, orig_dev);
1797 return br_handle_frame_hook(port, skb);
1800 #define handle_bridge(skb, pt_prev, ret, orig_dev) (skb)
1803 #ifdef CONFIG_NET_CLS_ACT
1804 /* TODO: Maybe we should just force sch_ingress to be compiled in
1805 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1806 * a compare and 2 stores extra right now if we dont have it on
1807 * but have CONFIG_NET_CLS_ACT
1808 * NOTE: This doesnt stop any functionality; if you dont have
1809 * the ingress scheduler, you just cant add policies on ingress.
1812 static int ing_filter(struct sk_buff *skb)
1815 struct net_device *dev = skb->dev;
1816 int result = TC_ACT_OK;
1818 if (dev->qdisc_ingress) {
1819 __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
1820 if (MAX_RED_LOOP < ttl++) {
1821 printk(KERN_WARNING "Redir loop detected Dropping packet (%d->%d)\n",
1822 skb->iif, skb->dev->ifindex);
1826 skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
1828 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
1830 spin_lock(&dev->ingress_lock);
1831 if ((q = dev->qdisc_ingress) != NULL)
1832 result = q->enqueue(skb, q);
1833 spin_unlock(&dev->ingress_lock);
1841 int netif_receive_skb(struct sk_buff *skb)
1843 struct packet_type *ptype, *pt_prev;
1844 struct net_device *orig_dev;
1845 int ret = NET_RX_DROP;
1848 /* if we've gotten here through NAPI, check netpoll */
1849 if (skb->dev->poll && netpoll_rx(skb))
1852 if (!skb->tstamp.tv64)
1856 skb->iif = skb->dev->ifindex;
1858 orig_dev = skb_bond(skb);
1863 __get_cpu_var(netdev_rx_stat).total++;
1865 skb_reset_network_header(skb);
1866 skb_reset_transport_header(skb);
1867 skb->mac_len = skb->network_header - skb->mac_header;
1873 #ifdef CONFIG_NET_CLS_ACT
1874 if (skb->tc_verd & TC_NCLS) {
1875 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1880 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1881 if (!ptype->dev || ptype->dev == skb->dev) {
1883 ret = deliver_skb(skb, pt_prev, orig_dev);
1888 #ifdef CONFIG_NET_CLS_ACT
1890 ret = deliver_skb(skb, pt_prev, orig_dev);
1891 pt_prev = NULL; /* noone else should process this after*/
1893 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1896 ret = ing_filter(skb);
1898 if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
1907 skb = handle_bridge(skb, &pt_prev, &ret, orig_dev);
1911 type = skb->protocol;
1912 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
1913 if (ptype->type == type &&
1914 (!ptype->dev || ptype->dev == skb->dev)) {
1916 ret = deliver_skb(skb, pt_prev, orig_dev);
1922 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1925 /* Jamal, now you will not able to escape explaining
1926 * me how you were going to use this. :-)
1936 static int process_backlog(struct net_device *backlog_dev, int *budget)
1939 int quota = min(backlog_dev->quota, *budget);
1940 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1941 unsigned long start_time = jiffies;
1943 backlog_dev->weight = weight_p;
1945 struct sk_buff *skb;
1946 struct net_device *dev;
1948 local_irq_disable();
1949 skb = __skb_dequeue(&queue->input_pkt_queue);
1956 netif_receive_skb(skb);
1962 if (work >= quota || jiffies - start_time > 1)
1967 backlog_dev->quota -= work;
1972 backlog_dev->quota -= work;
1975 list_del(&backlog_dev->poll_list);
1976 smp_mb__before_clear_bit();
1977 netif_poll_enable(backlog_dev);
1983 static void net_rx_action(struct softirq_action *h)
1985 struct softnet_data *queue = &__get_cpu_var(softnet_data);
1986 unsigned long start_time = jiffies;
1987 int budget = netdev_budget;
1990 local_irq_disable();
1992 while (!list_empty(&queue->poll_list)) {
1993 struct net_device *dev;
1995 if (budget <= 0 || jiffies - start_time > 1)
2000 dev = list_entry(queue->poll_list.next,
2001 struct net_device, poll_list);
2002 have = netpoll_poll_lock(dev);
2004 if (dev->quota <= 0 || dev->poll(dev, &budget)) {
2005 netpoll_poll_unlock(have);
2006 local_irq_disable();
2007 list_move_tail(&dev->poll_list, &queue->poll_list);
2009 dev->quota += dev->weight;
2011 dev->quota = dev->weight;
2013 netpoll_poll_unlock(have);
2015 local_irq_disable();
2020 #ifdef CONFIG_NET_DMA
2022 * There may not be any more sk_buffs coming right now, so push
2023 * any pending DMA copies to hardware
2025 if (net_dma_client) {
2026 struct dma_chan *chan;
2028 list_for_each_entry_rcu(chan, &net_dma_client->channels, client_node)
2029 dma_async_memcpy_issue_pending(chan);
2036 __get_cpu_var(netdev_rx_stat).time_squeeze++;
2037 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2041 static gifconf_func_t * gifconf_list [NPROTO];
2044 * register_gifconf - register a SIOCGIF handler
2045 * @family: Address family
2046 * @gifconf: Function handler
2048 * Register protocol dependent address dumping routines. The handler
2049 * that is passed must not be freed or reused until it has been replaced
2050 * by another handler.
2052 int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
2054 if (family >= NPROTO)
2056 gifconf_list[family] = gifconf;
2062 * Map an interface index to its name (SIOCGIFNAME)
2066 * We need this ioctl for efficient implementation of the
2067 * if_indextoname() function required by the IPv6 API. Without
2068 * it, we would have to search all the interfaces to find a
2072 static int dev_ifname(struct ifreq __user *arg)
2074 struct net_device *dev;
2078 * Fetch the caller's info block.
2081 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2084 read_lock(&dev_base_lock);
2085 dev = __dev_get_by_index(ifr.ifr_ifindex);
2087 read_unlock(&dev_base_lock);
2091 strcpy(ifr.ifr_name, dev->name);
2092 read_unlock(&dev_base_lock);
2094 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2100 * Perform a SIOCGIFCONF call. This structure will change
2101 * size eventually, and there is nothing I can do about it.
2102 * Thus we will need a 'compatibility mode'.
2105 static int dev_ifconf(char __user *arg)
2108 struct net_device *dev;
2115 * Fetch the caller's info block.
2118 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2125 * Loop over the interfaces, and write an info block for each.
2129 for_each_netdev(dev) {
2130 for (i = 0; i < NPROTO; i++) {
2131 if (gifconf_list[i]) {
2134 done = gifconf_list[i](dev, NULL, 0);
2136 done = gifconf_list[i](dev, pos + total,
2146 * All done. Write the updated control block back to the caller.
2148 ifc.ifc_len = total;
2151 * Both BSD and Solaris return 0 here, so we do too.
2153 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2156 #ifdef CONFIG_PROC_FS
2158 * This is invoked by the /proc filesystem handler to display a device
2161 void *dev_seq_start(struct seq_file *seq, loff_t *pos)
2164 struct net_device *dev;
2166 read_lock(&dev_base_lock);
2168 return SEQ_START_TOKEN;
2171 for_each_netdev(dev)
2178 void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2181 return v == SEQ_START_TOKEN ?
2182 first_net_device() : next_net_device((struct net_device *)v);
2185 void dev_seq_stop(struct seq_file *seq, void *v)
2187 read_unlock(&dev_base_lock);
2190 static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2192 struct net_device_stats *stats = dev->get_stats(dev);
2194 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2195 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2196 dev->name, stats->rx_bytes, stats->rx_packets,
2198 stats->rx_dropped + stats->rx_missed_errors,
2199 stats->rx_fifo_errors,
2200 stats->rx_length_errors + stats->rx_over_errors +
2201 stats->rx_crc_errors + stats->rx_frame_errors,
2202 stats->rx_compressed, stats->multicast,
2203 stats->tx_bytes, stats->tx_packets,
2204 stats->tx_errors, stats->tx_dropped,
2205 stats->tx_fifo_errors, stats->collisions,
2206 stats->tx_carrier_errors +
2207 stats->tx_aborted_errors +
2208 stats->tx_window_errors +
2209 stats->tx_heartbeat_errors,
2210 stats->tx_compressed);
2214 * Called from the PROCfs module. This now uses the new arbitrary sized
2215 * /proc/net interface to create /proc/net/dev
2217 static int dev_seq_show(struct seq_file *seq, void *v)
2219 if (v == SEQ_START_TOKEN)
2220 seq_puts(seq, "Inter-| Receive "
2222 " face |bytes packets errs drop fifo frame "
2223 "compressed multicast|bytes packets errs "
2224 "drop fifo colls carrier compressed\n");
2226 dev_seq_printf_stats(seq, v);
2230 static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2232 struct netif_rx_stats *rc = NULL;
2234 while (*pos < NR_CPUS)
2235 if (cpu_online(*pos)) {
2236 rc = &per_cpu(netdev_rx_stat, *pos);
2243 static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2245 return softnet_get_online(pos);
2248 static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2251 return softnet_get_online(pos);
2254 static void softnet_seq_stop(struct seq_file *seq, void *v)
2258 static int softnet_seq_show(struct seq_file *seq, void *v)
2260 struct netif_rx_stats *s = v;
2262 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
2263 s->total, s->dropped, s->time_squeeze, 0,
2264 0, 0, 0, 0, /* was fastroute */
2269 static const struct seq_operations dev_seq_ops = {
2270 .start = dev_seq_start,
2271 .next = dev_seq_next,
2272 .stop = dev_seq_stop,
2273 .show = dev_seq_show,
2276 static int dev_seq_open(struct inode *inode, struct file *file)
2278 return seq_open(file, &dev_seq_ops);
2281 static const struct file_operations dev_seq_fops = {
2282 .owner = THIS_MODULE,
2283 .open = dev_seq_open,
2285 .llseek = seq_lseek,
2286 .release = seq_release,
2289 static const struct seq_operations softnet_seq_ops = {
2290 .start = softnet_seq_start,
2291 .next = softnet_seq_next,
2292 .stop = softnet_seq_stop,
2293 .show = softnet_seq_show,
2296 static int softnet_seq_open(struct inode *inode, struct file *file)
2298 return seq_open(file, &softnet_seq_ops);
2301 static const struct file_operations softnet_seq_fops = {
2302 .owner = THIS_MODULE,
2303 .open = softnet_seq_open,
2305 .llseek = seq_lseek,
2306 .release = seq_release,
2309 static void *ptype_get_idx(loff_t pos)
2311 struct packet_type *pt = NULL;
2315 list_for_each_entry_rcu(pt, &ptype_all, list) {
2321 for (t = 0; t < 16; t++) {
2322 list_for_each_entry_rcu(pt, &ptype_base[t], list) {
2331 static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
2334 return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
2337 static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2339 struct packet_type *pt;
2340 struct list_head *nxt;
2344 if (v == SEQ_START_TOKEN)
2345 return ptype_get_idx(0);
2348 nxt = pt->list.next;
2349 if (pt->type == htons(ETH_P_ALL)) {
2350 if (nxt != &ptype_all)
2353 nxt = ptype_base[0].next;
2355 hash = ntohs(pt->type) & 15;
2357 while (nxt == &ptype_base[hash]) {
2360 nxt = ptype_base[hash].next;
2363 return list_entry(nxt, struct packet_type, list);
2366 static void ptype_seq_stop(struct seq_file *seq, void *v)
2371 static void ptype_seq_decode(struct seq_file *seq, void *sym)
2373 #ifdef CONFIG_KALLSYMS
2374 unsigned long offset = 0, symsize;
2375 const char *symname;
2379 symname = kallsyms_lookup((unsigned long)sym, &symsize, &offset,
2386 modname = delim = "";
2387 seq_printf(seq, "%s%s%s%s+0x%lx", delim, modname, delim,
2393 seq_printf(seq, "[%p]", sym);
2396 static int ptype_seq_show(struct seq_file *seq, void *v)
2398 struct packet_type *pt = v;
2400 if (v == SEQ_START_TOKEN)
2401 seq_puts(seq, "Type Device Function\n");
2403 if (pt->type == htons(ETH_P_ALL))
2404 seq_puts(seq, "ALL ");
2406 seq_printf(seq, "%04x", ntohs(pt->type));
2408 seq_printf(seq, " %-8s ",
2409 pt->dev ? pt->dev->name : "");
2410 ptype_seq_decode(seq, pt->func);
2411 seq_putc(seq, '\n');
2417 static const struct seq_operations ptype_seq_ops = {
2418 .start = ptype_seq_start,
2419 .next = ptype_seq_next,
2420 .stop = ptype_seq_stop,
2421 .show = ptype_seq_show,
2424 static int ptype_seq_open(struct inode *inode, struct file *file)
2426 return seq_open(file, &ptype_seq_ops);
2429 static const struct file_operations ptype_seq_fops = {
2430 .owner = THIS_MODULE,
2431 .open = ptype_seq_open,
2433 .llseek = seq_lseek,
2434 .release = seq_release,
2438 static int __init dev_proc_init(void)
2442 if (!proc_net_fops_create("dev", S_IRUGO, &dev_seq_fops))
2444 if (!proc_net_fops_create("softnet_stat", S_IRUGO, &softnet_seq_fops))
2446 if (!proc_net_fops_create("ptype", S_IRUGO, &ptype_seq_fops))
2449 if (wext_proc_init())
2455 proc_net_remove("ptype");
2457 proc_net_remove("softnet_stat");
2459 proc_net_remove("dev");
2463 #define dev_proc_init() 0
2464 #endif /* CONFIG_PROC_FS */
2468 * netdev_set_master - set up master/slave pair
2469 * @slave: slave device
2470 * @master: new master device
2472 * Changes the master device of the slave. Pass %NULL to break the
2473 * bonding. The caller must hold the RTNL semaphore. On a failure
2474 * a negative errno code is returned. On success the reference counts
2475 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2476 * function returns zero.
2478 int netdev_set_master(struct net_device *slave, struct net_device *master)
2480 struct net_device *old = slave->master;
2490 slave->master = master;
2498 slave->flags |= IFF_SLAVE;
2500 slave->flags &= ~IFF_SLAVE;
2502 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2506 static void __dev_set_promiscuity(struct net_device *dev, int inc)
2508 unsigned short old_flags = dev->flags;
2510 if ((dev->promiscuity += inc) == 0)
2511 dev->flags &= ~IFF_PROMISC;
2513 dev->flags |= IFF_PROMISC;
2514 if (dev->flags != old_flags) {
2515 printk(KERN_INFO "device %s %s promiscuous mode\n",
2516 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
2518 audit_log(current->audit_context, GFP_ATOMIC,
2519 AUDIT_ANOM_PROMISCUOUS,
2520 "dev=%s prom=%d old_prom=%d auid=%u",
2521 dev->name, (dev->flags & IFF_PROMISC),
2522 (old_flags & IFF_PROMISC),
2523 audit_get_loginuid(current->audit_context));
2528 * dev_set_promiscuity - update promiscuity count on a device
2532 * Add or remove promiscuity from a device. While the count in the device
2533 * remains above zero the interface remains promiscuous. Once it hits zero
2534 * the device reverts back to normal filtering operation. A negative inc
2535 * value is used to drop promiscuity on the device.
2537 void dev_set_promiscuity(struct net_device *dev, int inc)
2539 unsigned short old_flags = dev->flags;
2541 __dev_set_promiscuity(dev, inc);
2542 if (dev->flags != old_flags)
2543 dev_set_rx_mode(dev);
2547 * dev_set_allmulti - update allmulti count on a device
2551 * Add or remove reception of all multicast frames to a device. While the
2552 * count in the device remains above zero the interface remains listening
2553 * to all interfaces. Once it hits zero the device reverts back to normal
2554 * filtering operation. A negative @inc value is used to drop the counter
2555 * when releasing a resource needing all multicasts.
2558 void dev_set_allmulti(struct net_device *dev, int inc)
2560 unsigned short old_flags = dev->flags;
2562 dev->flags |= IFF_ALLMULTI;
2563 if ((dev->allmulti += inc) == 0)
2564 dev->flags &= ~IFF_ALLMULTI;
2565 if (dev->flags ^ old_flags)
2566 dev_set_rx_mode(dev);
2570 * Upload unicast and multicast address lists to device and
2571 * configure RX filtering. When the device doesn't support unicast
2572 * filtering it is put in promiscous mode while unicast addresses
2575 void __dev_set_rx_mode(struct net_device *dev)
2577 /* dev_open will call this function so the list will stay sane. */
2578 if (!(dev->flags&IFF_UP))
2581 if (!netif_device_present(dev))
2584 if (dev->set_rx_mode)
2585 dev->set_rx_mode(dev);
2587 /* Unicast addresses changes may only happen under the rtnl,
2588 * therefore calling __dev_set_promiscuity here is safe.
2590 if (dev->uc_count > 0 && !dev->uc_promisc) {
2591 __dev_set_promiscuity(dev, 1);
2592 dev->uc_promisc = 1;
2593 } else if (dev->uc_count == 0 && dev->uc_promisc) {
2594 __dev_set_promiscuity(dev, -1);
2595 dev->uc_promisc = 0;
2598 if (dev->set_multicast_list)
2599 dev->set_multicast_list(dev);
2603 void dev_set_rx_mode(struct net_device *dev)
2605 netif_tx_lock_bh(dev);
2606 __dev_set_rx_mode(dev);
2607 netif_tx_unlock_bh(dev);
2610 int __dev_addr_delete(struct dev_addr_list **list, int *count,
2611 void *addr, int alen, int glbl)
2613 struct dev_addr_list *da;
2615 for (; (da = *list) != NULL; list = &da->next) {
2616 if (memcmp(da->da_addr, addr, da->da_addrlen) == 0 &&
2617 alen == da->da_addrlen) {
2619 int old_glbl = da->da_gusers;
2636 int __dev_addr_add(struct dev_addr_list **list, int *count,
2637 void *addr, int alen, int glbl)
2639 struct dev_addr_list *da;
2641 for (da = *list; da != NULL; da = da->next) {
2642 if (memcmp(da->da_addr, addr, da->da_addrlen) == 0 &&
2643 da->da_addrlen == alen) {
2645 int old_glbl = da->da_gusers;
2655 da = kmalloc(sizeof(*da), GFP_ATOMIC);
2658 memcpy(da->da_addr, addr, alen);
2659 da->da_addrlen = alen;
2661 da->da_gusers = glbl ? 1 : 0;
2668 void __dev_addr_discard(struct dev_addr_list **list)
2670 struct dev_addr_list *tmp;
2672 while (*list != NULL) {
2675 if (tmp->da_users > tmp->da_gusers)
2676 printk("__dev_addr_discard: address leakage! "
2677 "da_users=%d\n", tmp->da_users);
2683 * dev_unicast_delete - Release secondary unicast address.
2686 * Release reference to a secondary unicast address and remove it
2687 * from the device if the reference count drop to zero.
2689 * The caller must hold the rtnl_mutex.
2691 int dev_unicast_delete(struct net_device *dev, void *addr, int alen)
2697 netif_tx_lock_bh(dev);
2698 err = __dev_addr_delete(&dev->uc_list, &dev->uc_count, addr, alen, 0);
2700 __dev_set_rx_mode(dev);
2701 netif_tx_unlock_bh(dev);
2704 EXPORT_SYMBOL(dev_unicast_delete);
2707 * dev_unicast_add - add a secondary unicast address
2710 * Add a secondary unicast address to the device or increase
2711 * the reference count if it already exists.
2713 * The caller must hold the rtnl_mutex.
2715 int dev_unicast_add(struct net_device *dev, void *addr, int alen)
2721 netif_tx_lock_bh(dev);
2722 err = __dev_addr_add(&dev->uc_list, &dev->uc_count, addr, alen, 0);
2724 __dev_set_rx_mode(dev);
2725 netif_tx_unlock_bh(dev);
2728 EXPORT_SYMBOL(dev_unicast_add);
2730 static void dev_unicast_discard(struct net_device *dev)
2732 netif_tx_lock_bh(dev);
2733 __dev_addr_discard(&dev->uc_list);
2735 netif_tx_unlock_bh(dev);
2738 unsigned dev_get_flags(const struct net_device *dev)
2742 flags = (dev->flags & ~(IFF_PROMISC |
2747 (dev->gflags & (IFF_PROMISC |
2750 if (netif_running(dev)) {
2751 if (netif_oper_up(dev))
2752 flags |= IFF_RUNNING;
2753 if (netif_carrier_ok(dev))
2754 flags |= IFF_LOWER_UP;
2755 if (netif_dormant(dev))
2756 flags |= IFF_DORMANT;
2762 int dev_change_flags(struct net_device *dev, unsigned flags)
2765 int old_flags = dev->flags;
2768 * Set the flags on our device.
2771 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
2772 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
2774 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
2778 * Load in the correct multicast list now the flags have changed.
2781 dev_set_rx_mode(dev);
2784 * Have we downed the interface. We handle IFF_UP ourselves
2785 * according to user attempts to set it, rather than blindly
2790 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
2791 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2794 dev_set_rx_mode(dev);
2797 if (dev->flags & IFF_UP &&
2798 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
2800 raw_notifier_call_chain(&netdev_chain,
2801 NETDEV_CHANGE, dev);
2803 if ((flags ^ dev->gflags) & IFF_PROMISC) {
2804 int inc = (flags & IFF_PROMISC) ? +1 : -1;
2805 dev->gflags ^= IFF_PROMISC;
2806 dev_set_promiscuity(dev, inc);
2809 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
2810 is important. Some (broken) drivers set IFF_PROMISC, when
2811 IFF_ALLMULTI is requested not asking us and not reporting.
2813 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
2814 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
2815 dev->gflags ^= IFF_ALLMULTI;
2816 dev_set_allmulti(dev, inc);
2819 /* Exclude state transition flags, already notified */
2820 changes = (old_flags ^ dev->flags) & ~(IFF_UP | IFF_RUNNING);
2822 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
2827 int dev_set_mtu(struct net_device *dev, int new_mtu)
2831 if (new_mtu == dev->mtu)
2834 /* MTU must be positive. */
2838 if (!netif_device_present(dev))
2842 if (dev->change_mtu)
2843 err = dev->change_mtu(dev, new_mtu);
2846 if (!err && dev->flags & IFF_UP)
2847 raw_notifier_call_chain(&netdev_chain,
2848 NETDEV_CHANGEMTU, dev);
2852 int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
2856 if (!dev->set_mac_address)
2858 if (sa->sa_family != dev->type)
2860 if (!netif_device_present(dev))
2862 err = dev->set_mac_address(dev, sa);
2864 raw_notifier_call_chain(&netdev_chain,
2865 NETDEV_CHANGEADDR, dev);
2870 * Perform the SIOCxIFxxx calls.
2872 static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2875 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
2881 case SIOCGIFFLAGS: /* Get interface flags */
2882 ifr->ifr_flags = dev_get_flags(dev);
2885 case SIOCSIFFLAGS: /* Set interface flags */
2886 return dev_change_flags(dev, ifr->ifr_flags);
2888 case SIOCGIFMETRIC: /* Get the metric on the interface
2889 (currently unused) */
2890 ifr->ifr_metric = 0;
2893 case SIOCSIFMETRIC: /* Set the metric on the interface
2894 (currently unused) */
2897 case SIOCGIFMTU: /* Get the MTU of a device */
2898 ifr->ifr_mtu = dev->mtu;
2901 case SIOCSIFMTU: /* Set the MTU of a device */
2902 return dev_set_mtu(dev, ifr->ifr_mtu);
2906 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
2908 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
2909 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2910 ifr->ifr_hwaddr.sa_family = dev->type;
2914 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
2916 case SIOCSIFHWBROADCAST:
2917 if (ifr->ifr_hwaddr.sa_family != dev->type)
2919 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
2920 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
2921 raw_notifier_call_chain(&netdev_chain,
2922 NETDEV_CHANGEADDR, dev);
2926 ifr->ifr_map.mem_start = dev->mem_start;
2927 ifr->ifr_map.mem_end = dev->mem_end;
2928 ifr->ifr_map.base_addr = dev->base_addr;
2929 ifr->ifr_map.irq = dev->irq;
2930 ifr->ifr_map.dma = dev->dma;
2931 ifr->ifr_map.port = dev->if_port;
2935 if (dev->set_config) {
2936 if (!netif_device_present(dev))
2938 return dev->set_config(dev, &ifr->ifr_map);
2943 if (!dev->set_multicast_list ||
2944 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2946 if (!netif_device_present(dev))
2948 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
2952 if (!dev->set_multicast_list ||
2953 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2955 if (!netif_device_present(dev))
2957 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
2961 ifr->ifr_ifindex = dev->ifindex;
2965 ifr->ifr_qlen = dev->tx_queue_len;
2969 if (ifr->ifr_qlen < 0)
2971 dev->tx_queue_len = ifr->ifr_qlen;
2975 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
2976 return dev_change_name(dev, ifr->ifr_newname);
2979 * Unknown or private ioctl
2983 if ((cmd >= SIOCDEVPRIVATE &&
2984 cmd <= SIOCDEVPRIVATE + 15) ||
2985 cmd == SIOCBONDENSLAVE ||
2986 cmd == SIOCBONDRELEASE ||
2987 cmd == SIOCBONDSETHWADDR ||
2988 cmd == SIOCBONDSLAVEINFOQUERY ||
2989 cmd == SIOCBONDINFOQUERY ||
2990 cmd == SIOCBONDCHANGEACTIVE ||
2991 cmd == SIOCGMIIPHY ||
2992 cmd == SIOCGMIIREG ||
2993 cmd == SIOCSMIIREG ||
2994 cmd == SIOCBRADDIF ||
2995 cmd == SIOCBRDELIF ||
2996 cmd == SIOCWANDEV) {
2998 if (dev->do_ioctl) {
2999 if (netif_device_present(dev))
3000 err = dev->do_ioctl(dev, ifr,
3013 * This function handles all "interface"-type I/O control requests. The actual
3014 * 'doing' part of this is dev_ifsioc above.
3018 * dev_ioctl - network device ioctl
3019 * @cmd: command to issue
3020 * @arg: pointer to a struct ifreq in user space
3022 * Issue ioctl functions to devices. This is normally called by the
3023 * user space syscall interfaces but can sometimes be useful for
3024 * other purposes. The return value is the return from the syscall if
3025 * positive or a negative errno code on error.
3028 int dev_ioctl(unsigned int cmd, void __user *arg)
3034 /* One special case: SIOCGIFCONF takes ifconf argument
3035 and requires shared lock, because it sleeps writing
3039 if (cmd == SIOCGIFCONF) {
3041 ret = dev_ifconf((char __user *) arg);
3045 if (cmd == SIOCGIFNAME)
3046 return dev_ifname((struct ifreq __user *)arg);
3048 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
3051 ifr.ifr_name[IFNAMSIZ-1] = 0;
3053 colon = strchr(ifr.ifr_name, ':');
3058 * See which interface the caller is talking about.
3063 * These ioctl calls:
3064 * - can be done by all.
3065 * - atomic and do not require locking.
3076 dev_load(ifr.ifr_name);
3077 read_lock(&dev_base_lock);
3078 ret = dev_ifsioc(&ifr, cmd);
3079 read_unlock(&dev_base_lock);
3083 if (copy_to_user(arg, &ifr,
3084 sizeof(struct ifreq)))
3090 dev_load(ifr.ifr_name);
3092 ret = dev_ethtool(&ifr);
3097 if (copy_to_user(arg, &ifr,
3098 sizeof(struct ifreq)))
3104 * These ioctl calls:
3105 * - require superuser power.
3106 * - require strict serialization.
3112 if (!capable(CAP_NET_ADMIN))
3114 dev_load(ifr.ifr_name);
3116 ret = dev_ifsioc(&ifr, cmd);
3121 if (copy_to_user(arg, &ifr,
3122 sizeof(struct ifreq)))
3128 * These ioctl calls:
3129 * - require superuser power.
3130 * - require strict serialization.
3131 * - do not return a value
3141 case SIOCSIFHWBROADCAST:
3144 case SIOCBONDENSLAVE:
3145 case SIOCBONDRELEASE:
3146 case SIOCBONDSETHWADDR:
3147 case SIOCBONDCHANGEACTIVE:
3150 if (!capable(CAP_NET_ADMIN))
3153 case SIOCBONDSLAVEINFOQUERY:
3154 case SIOCBONDINFOQUERY:
3155 dev_load(ifr.ifr_name);
3157 ret = dev_ifsioc(&ifr, cmd);
3162 /* Get the per device memory space. We can add this but
3163 * currently do not support it */
3165 /* Set the per device memory buffer space.
3166 * Not applicable in our case */
3171 * Unknown or private ioctl.
3174 if (cmd == SIOCWANDEV ||
3175 (cmd >= SIOCDEVPRIVATE &&
3176 cmd <= SIOCDEVPRIVATE + 15)) {
3177 dev_load(ifr.ifr_name);
3179 ret = dev_ifsioc(&ifr, cmd);
3181 if (!ret && copy_to_user(arg, &ifr,
3182 sizeof(struct ifreq)))
3186 /* Take care of Wireless Extensions */
3187 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
3188 return wext_handle_ioctl(&ifr, cmd, arg);
3195 * dev_new_index - allocate an ifindex
3197 * Returns a suitable unique value for a new device interface
3198 * number. The caller must hold the rtnl semaphore or the
3199 * dev_base_lock to be sure it remains unique.
3201 static int dev_new_index(void)
3207 if (!__dev_get_by_index(ifindex))
3212 static int dev_boot_phase = 1;
3214 /* Delayed registration/unregisteration */
3215 static DEFINE_SPINLOCK(net_todo_list_lock);
3216 static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
3218 static void net_set_todo(struct net_device *dev)
3220 spin_lock(&net_todo_list_lock);
3221 list_add_tail(&dev->todo_list, &net_todo_list);
3222 spin_unlock(&net_todo_list_lock);
3226 * register_netdevice - register a network device
3227 * @dev: device to register
3229 * Take a completed network device structure and add it to the kernel
3230 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3231 * chain. 0 is returned on success. A negative errno code is returned
3232 * on a failure to set up the device, or if the name is a duplicate.
3234 * Callers must hold the rtnl semaphore. You may want
3235 * register_netdev() instead of this.
3238 * The locking appears insufficient to guarantee two parallel registers
3239 * will not get the same name.
3242 int register_netdevice(struct net_device *dev)
3244 struct hlist_head *head;
3245 struct hlist_node *p;
3248 BUG_ON(dev_boot_phase);
3253 /* When net_device's are persistent, this will be fatal. */
3254 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
3256 spin_lock_init(&dev->queue_lock);
3257 spin_lock_init(&dev->_xmit_lock);
3258 netdev_set_lockdep_class(&dev->_xmit_lock, dev->type);
3259 dev->xmit_lock_owner = -1;
3260 spin_lock_init(&dev->ingress_lock);
3264 /* Init, if this function is available */
3266 ret = dev->init(dev);
3274 if (!dev_valid_name(dev->name)) {
3279 dev->ifindex = dev_new_index();
3280 if (dev->iflink == -1)
3281 dev->iflink = dev->ifindex;
3283 /* Check for existence of name */
3284 head = dev_name_hash(dev->name);
3285 hlist_for_each(p, head) {
3286 struct net_device *d
3287 = hlist_entry(p, struct net_device, name_hlist);
3288 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
3294 /* Fix illegal checksum combinations */
3295 if ((dev->features & NETIF_F_HW_CSUM) &&
3296 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3297 printk(KERN_NOTICE "%s: mixed HW and IP checksum settings.\n",
3299 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
3302 if ((dev->features & NETIF_F_NO_CSUM) &&
3303 (dev->features & (NETIF_F_HW_CSUM|NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3304 printk(KERN_NOTICE "%s: mixed no checksumming and other settings.\n",
3306 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM|NETIF_F_HW_CSUM);
3310 /* Fix illegal SG+CSUM combinations. */
3311 if ((dev->features & NETIF_F_SG) &&
3312 !(dev->features & NETIF_F_ALL_CSUM)) {
3313 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n",
3315 dev->features &= ~NETIF_F_SG;
3318 /* TSO requires that SG is present as well. */
3319 if ((dev->features & NETIF_F_TSO) &&
3320 !(dev->features & NETIF_F_SG)) {
3321 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n",
3323 dev->features &= ~NETIF_F_TSO;
3325 if (dev->features & NETIF_F_UFO) {
3326 if (!(dev->features & NETIF_F_HW_CSUM)) {
3327 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3328 "NETIF_F_HW_CSUM feature.\n",
3330 dev->features &= ~NETIF_F_UFO;
3332 if (!(dev->features & NETIF_F_SG)) {
3333 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3334 "NETIF_F_SG feature.\n",
3336 dev->features &= ~NETIF_F_UFO;
3341 * nil rebuild_header routine,
3342 * that should be never called and used as just bug trap.
3345 if (!dev->rebuild_header)
3346 dev->rebuild_header = default_rebuild_header;
3348 ret = netdev_register_sysfs(dev);
3351 dev->reg_state = NETREG_REGISTERED;
3354 * Default initial state at registry is that the
3355 * device is present.
3358 set_bit(__LINK_STATE_PRESENT, &dev->state);
3360 dev_init_scheduler(dev);
3361 write_lock_bh(&dev_base_lock);
3362 list_add_tail(&dev->dev_list, &dev_base_head);
3363 hlist_add_head(&dev->name_hlist, head);
3364 hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
3366 write_unlock_bh(&dev_base_lock);
3368 /* Notify protocols, that a new device appeared. */
3369 raw_notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
3378 * register_netdev - register a network device
3379 * @dev: device to register
3381 * Take a completed network device structure and add it to the kernel
3382 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3383 * chain. 0 is returned on success. A negative errno code is returned
3384 * on a failure to set up the device, or if the name is a duplicate.
3386 * This is a wrapper around register_netdevice that takes the rtnl semaphore
3387 * and expands the device name if you passed a format string to
3390 int register_netdev(struct net_device *dev)
3397 * If the name is a format string the caller wants us to do a
3400 if (strchr(dev->name, '%')) {
3401 err = dev_alloc_name(dev, dev->name);
3406 err = register_netdevice(dev);
3411 EXPORT_SYMBOL(register_netdev);
3414 * netdev_wait_allrefs - wait until all references are gone.
3416 * This is called when unregistering network devices.
3418 * Any protocol or device that holds a reference should register
3419 * for netdevice notification, and cleanup and put back the
3420 * reference if they receive an UNREGISTER event.
3421 * We can get stuck here if buggy protocols don't correctly
3424 static void netdev_wait_allrefs(struct net_device *dev)
3426 unsigned long rebroadcast_time, warning_time;
3428 rebroadcast_time = warning_time = jiffies;
3429 while (atomic_read(&dev->refcnt) != 0) {
3430 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
3433 /* Rebroadcast unregister notification */
3434 raw_notifier_call_chain(&netdev_chain,
3435 NETDEV_UNREGISTER, dev);
3437 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
3439 /* We must not have linkwatch events
3440 * pending on unregister. If this
3441 * happens, we simply run the queue
3442 * unscheduled, resulting in a noop
3445 linkwatch_run_queue();
3450 rebroadcast_time = jiffies;
3455 if (time_after(jiffies, warning_time + 10 * HZ)) {
3456 printk(KERN_EMERG "unregister_netdevice: "
3457 "waiting for %s to become free. Usage "
3459 dev->name, atomic_read(&dev->refcnt));
3460 warning_time = jiffies;
3469 * register_netdevice(x1);
3470 * register_netdevice(x2);
3472 * unregister_netdevice(y1);
3473 * unregister_netdevice(y2);
3479 * We are invoked by rtnl_unlock() after it drops the semaphore.
3480 * This allows us to deal with problems:
3481 * 1) We can delete sysfs objects which invoke hotplug
3482 * without deadlocking with linkwatch via keventd.
3483 * 2) Since we run with the RTNL semaphore not held, we can sleep
3484 * safely in order to wait for the netdev refcnt to drop to zero.
3486 static DEFINE_MUTEX(net_todo_run_mutex);
3487 void netdev_run_todo(void)
3489 struct list_head list;
3491 /* Need to guard against multiple cpu's getting out of order. */
3492 mutex_lock(&net_todo_run_mutex);
3494 /* Not safe to do outside the semaphore. We must not return
3495 * until all unregister events invoked by the local processor
3496 * have been completed (either by this todo run, or one on
3499 if (list_empty(&net_todo_list))
3502 /* Snapshot list, allow later requests */
3503 spin_lock(&net_todo_list_lock);
3504 list_replace_init(&net_todo_list, &list);
3505 spin_unlock(&net_todo_list_lock);
3507 while (!list_empty(&list)) {
3508 struct net_device *dev
3509 = list_entry(list.next, struct net_device, todo_list);
3510 list_del(&dev->todo_list);
3512 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
3513 printk(KERN_ERR "network todo '%s' but state %d\n",
3514 dev->name, dev->reg_state);
3519 dev->reg_state = NETREG_UNREGISTERED;
3521 netdev_wait_allrefs(dev);
3524 BUG_ON(atomic_read(&dev->refcnt));
3525 BUG_TRAP(!dev->ip_ptr);
3526 BUG_TRAP(!dev->ip6_ptr);
3527 BUG_TRAP(!dev->dn_ptr);
3529 if (dev->destructor)
3530 dev->destructor(dev);
3532 /* Free network device */
3533 kobject_put(&dev->dev.kobj);
3537 mutex_unlock(&net_todo_run_mutex);
3540 static struct net_device_stats *internal_stats(struct net_device *dev)
3546 * alloc_netdev_mq - allocate network device
3547 * @sizeof_priv: size of private data to allocate space for
3548 * @name: device name format string
3549 * @setup: callback to initialize device
3550 * @queue_count: the number of subqueues to allocate
3552 * Allocates a struct net_device with private data area for driver use
3553 * and performs basic initialization. Also allocates subquue structs
3554 * for each queue on the device at the end of the netdevice.
3556 struct net_device *alloc_netdev_mq(int sizeof_priv, const char *name,
3557 void (*setup)(struct net_device *), unsigned int queue_count)
3560 struct net_device *dev;
3563 BUG_ON(strlen(name) >= sizeof(dev->name));
3565 /* ensure 32-byte alignment of both the device and private area */
3566 alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST +
3567 (sizeof(struct net_device_subqueue) * queue_count)) &
3568 ~NETDEV_ALIGN_CONST;
3569 alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
3571 p = kzalloc(alloc_size, GFP_KERNEL);
3573 printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
3577 dev = (struct net_device *)
3578 (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
3579 dev->padded = (char *)dev - (char *)p;
3582 dev->priv = ((char *)dev +
3583 ((sizeof(struct net_device) +
3584 (sizeof(struct net_device_subqueue) *
3585 queue_count) + NETDEV_ALIGN_CONST)
3586 & ~NETDEV_ALIGN_CONST));
3589 dev->egress_subqueue_count = queue_count;
3591 dev->get_stats = internal_stats;
3593 strcpy(dev->name, name);
3596 EXPORT_SYMBOL(alloc_netdev_mq);
3599 * free_netdev - free network device
3602 * This function does the last stage of destroying an allocated device
3603 * interface. The reference to the device object is released.
3604 * If this is the last reference then it will be freed.
3606 void free_netdev(struct net_device *dev)
3609 /* Compatibility with error handling in drivers */
3610 if (dev->reg_state == NETREG_UNINITIALIZED) {
3611 kfree((char *)dev - dev->padded);
3615 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
3616 dev->reg_state = NETREG_RELEASED;
3618 /* will free via device release */
3619 put_device(&dev->dev);
3621 kfree((char *)dev - dev->padded);
3625 /* Synchronize with packet receive processing. */
3626 void synchronize_net(void)
3633 * unregister_netdevice - remove device from the kernel
3636 * This function shuts down a device interface and removes it
3637 * from the kernel tables. On success 0 is returned, on a failure
3638 * a negative errno code is returned.
3640 * Callers must hold the rtnl semaphore. You may want
3641 * unregister_netdev() instead of this.
3644 void unregister_netdevice(struct net_device *dev)
3646 BUG_ON(dev_boot_phase);
3649 /* Some devices call without registering for initialization unwind. */
3650 if (dev->reg_state == NETREG_UNINITIALIZED) {
3651 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3652 "was registered\n", dev->name, dev);
3658 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3660 /* If device is running, close it first. */
3661 if (dev->flags & IFF_UP)
3664 /* And unlink it from device chain. */
3665 write_lock_bh(&dev_base_lock);
3666 list_del(&dev->dev_list);
3667 hlist_del(&dev->name_hlist);
3668 hlist_del(&dev->index_hlist);
3669 write_unlock_bh(&dev_base_lock);
3671 dev->reg_state = NETREG_UNREGISTERING;
3675 /* Shutdown queueing discipline. */
3679 /* Notify protocols, that we are about to destroy
3680 this device. They should clean all the things.
3682 raw_notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
3685 * Flush the unicast and multicast chains
3687 dev_unicast_discard(dev);
3688 dev_mc_discard(dev);
3693 /* Notifier chain MUST detach us from master device. */
3694 BUG_TRAP(!dev->master);
3696 /* Remove entries from sysfs */
3697 netdev_unregister_sysfs(dev);
3699 /* Finish processing unregister after unlock */
3708 * unregister_netdev - remove device from the kernel
3711 * This function shuts down a device interface and removes it
3712 * from the kernel tables. On success 0 is returned, on a failure
3713 * a negative errno code is returned.
3715 * This is just a wrapper for unregister_netdevice that takes
3716 * the rtnl semaphore. In general you want to use this and not
3717 * unregister_netdevice.
3719 void unregister_netdev(struct net_device *dev)
3722 unregister_netdevice(dev);
3726 EXPORT_SYMBOL(unregister_netdev);
3728 static int dev_cpu_callback(struct notifier_block *nfb,
3729 unsigned long action,
3732 struct sk_buff **list_skb;
3733 struct net_device **list_net;
3734 struct sk_buff *skb;
3735 unsigned int cpu, oldcpu = (unsigned long)ocpu;
3736 struct softnet_data *sd, *oldsd;
3738 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
3741 local_irq_disable();
3742 cpu = smp_processor_id();
3743 sd = &per_cpu(softnet_data, cpu);
3744 oldsd = &per_cpu(softnet_data, oldcpu);
3746 /* Find end of our completion_queue. */
3747 list_skb = &sd->completion_queue;
3749 list_skb = &(*list_skb)->next;
3750 /* Append completion queue from offline CPU. */
3751 *list_skb = oldsd->completion_queue;
3752 oldsd->completion_queue = NULL;
3754 /* Find end of our output_queue. */
3755 list_net = &sd->output_queue;
3757 list_net = &(*list_net)->next_sched;
3758 /* Append output queue from offline CPU. */
3759 *list_net = oldsd->output_queue;
3760 oldsd->output_queue = NULL;
3762 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3765 /* Process offline CPU's input_pkt_queue */
3766 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
3772 #ifdef CONFIG_NET_DMA
3774 * net_dma_rebalance -
3775 * This is called when the number of channels allocated to the net_dma_client
3776 * changes. The net_dma_client tries to have one DMA channel per CPU.
3778 static void net_dma_rebalance(void)
3780 unsigned int cpu, i, n;
3781 struct dma_chan *chan;
3783 if (net_dma_count == 0) {
3784 for_each_online_cpu(cpu)
3785 rcu_assign_pointer(per_cpu(softnet_data, cpu).net_dma, NULL);
3790 cpu = first_cpu(cpu_online_map);
3793 list_for_each_entry(chan, &net_dma_client->channels, client_node) {
3794 n = ((num_online_cpus() / net_dma_count)
3795 + (i < (num_online_cpus() % net_dma_count) ? 1 : 0));
3798 per_cpu(softnet_data, cpu).net_dma = chan;
3799 cpu = next_cpu(cpu, cpu_online_map);
3808 * netdev_dma_event - event callback for the net_dma_client
3809 * @client: should always be net_dma_client
3810 * @chan: DMA channel for the event
3811 * @event: event type
3813 static void netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
3814 enum dma_event event)
3816 spin_lock(&net_dma_event_lock);
3818 case DMA_RESOURCE_ADDED:
3820 net_dma_rebalance();
3822 case DMA_RESOURCE_REMOVED:
3824 net_dma_rebalance();
3829 spin_unlock(&net_dma_event_lock);
3833 * netdev_dma_regiser - register the networking subsystem as a DMA client
3835 static int __init netdev_dma_register(void)
3837 spin_lock_init(&net_dma_event_lock);
3838 net_dma_client = dma_async_client_register(netdev_dma_event);
3839 if (net_dma_client == NULL)
3842 dma_async_client_chan_request(net_dma_client, num_online_cpus());
3847 static int __init netdev_dma_register(void) { return -ENODEV; }
3848 #endif /* CONFIG_NET_DMA */
3851 * Initialize the DEV module. At boot time this walks the device list and
3852 * unhooks any devices that fail to initialise (normally hardware not
3853 * present) and leaves us with a valid list of present and active devices.
3858 * This is called single threaded during boot, so no need
3859 * to take the rtnl semaphore.
3861 static int __init net_dev_init(void)
3863 int i, rc = -ENOMEM;
3865 BUG_ON(!dev_boot_phase);
3867 if (dev_proc_init())
3870 if (netdev_sysfs_init())
3873 INIT_LIST_HEAD(&ptype_all);
3874 for (i = 0; i < 16; i++)
3875 INIT_LIST_HEAD(&ptype_base[i]);
3877 for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
3878 INIT_HLIST_HEAD(&dev_name_head[i]);
3880 for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
3881 INIT_HLIST_HEAD(&dev_index_head[i]);
3884 * Initialise the packet receive queues.
3887 for_each_possible_cpu(i) {
3888 struct softnet_data *queue;
3890 queue = &per_cpu(softnet_data, i);
3891 skb_queue_head_init(&queue->input_pkt_queue);
3892 queue->completion_queue = NULL;
3893 INIT_LIST_HEAD(&queue->poll_list);
3894 set_bit(__LINK_STATE_START, &queue->backlog_dev.state);
3895 queue->backlog_dev.weight = weight_p;
3896 queue->backlog_dev.poll = process_backlog;
3897 atomic_set(&queue->backlog_dev.refcnt, 1);
3900 netdev_dma_register();
3904 open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
3905 open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
3907 hotcpu_notifier(dev_cpu_callback, 0);
3915 subsys_initcall(net_dev_init);
3917 EXPORT_SYMBOL(__dev_get_by_index);
3918 EXPORT_SYMBOL(__dev_get_by_name);
3919 EXPORT_SYMBOL(__dev_remove_pack);
3920 EXPORT_SYMBOL(dev_valid_name);
3921 EXPORT_SYMBOL(dev_add_pack);
3922 EXPORT_SYMBOL(dev_alloc_name);
3923 EXPORT_SYMBOL(dev_close);
3924 EXPORT_SYMBOL(dev_get_by_flags);
3925 EXPORT_SYMBOL(dev_get_by_index);
3926 EXPORT_SYMBOL(dev_get_by_name);
3927 EXPORT_SYMBOL(dev_open);
3928 EXPORT_SYMBOL(dev_queue_xmit);
3929 EXPORT_SYMBOL(dev_remove_pack);
3930 EXPORT_SYMBOL(dev_set_allmulti);
3931 EXPORT_SYMBOL(dev_set_promiscuity);
3932 EXPORT_SYMBOL(dev_change_flags);
3933 EXPORT_SYMBOL(dev_set_mtu);
3934 EXPORT_SYMBOL(dev_set_mac_address);
3935 EXPORT_SYMBOL(free_netdev);
3936 EXPORT_SYMBOL(netdev_boot_setup_check);
3937 EXPORT_SYMBOL(netdev_set_master);
3938 EXPORT_SYMBOL(netdev_state_change);
3939 EXPORT_SYMBOL(netif_receive_skb);
3940 EXPORT_SYMBOL(netif_rx);
3941 EXPORT_SYMBOL(register_gifconf);
3942 EXPORT_SYMBOL(register_netdevice);
3943 EXPORT_SYMBOL(register_netdevice_notifier);
3944 EXPORT_SYMBOL(skb_checksum_help);
3945 EXPORT_SYMBOL(synchronize_net);
3946 EXPORT_SYMBOL(unregister_netdevice);
3947 EXPORT_SYMBOL(unregister_netdevice_notifier);
3948 EXPORT_SYMBOL(net_enable_timestamp);
3949 EXPORT_SYMBOL(net_disable_timestamp);
3950 EXPORT_SYMBOL(dev_get_flags);
3952 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
3953 EXPORT_SYMBOL(br_handle_frame_hook);
3954 EXPORT_SYMBOL(br_fdb_get_hook);
3955 EXPORT_SYMBOL(br_fdb_put_hook);
3959 EXPORT_SYMBOL(dev_load);
3962 EXPORT_PER_CPU_SYMBOL(softnet_data);