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>
95 #include <net/net_namespace.h>
97 #include <linux/rtnetlink.h>
98 #include <linux/proc_fs.h>
99 #include <linux/seq_file.h>
100 #include <linux/stat.h>
101 #include <linux/if_bridge.h>
102 #include <linux/if_macvlan.h>
104 #include <net/pkt_sched.h>
105 #include <net/checksum.h>
106 #include <linux/highmem.h>
107 #include <linux/init.h>
108 #include <linux/kmod.h>
109 #include <linux/module.h>
110 #include <linux/kallsyms.h>
111 #include <linux/netpoll.h>
112 #include <linux/rcupdate.h>
113 #include <linux/delay.h>
114 #include <net/wext.h>
115 #include <net/iw_handler.h>
116 #include <asm/current.h>
117 #include <linux/audit.h>
118 #include <linux/dmaengine.h>
119 #include <linux/err.h>
120 #include <linux/ctype.h>
121 #include <linux/if_arp.h>
124 * The list of packet types we will receive (as opposed to discard)
125 * and the routines to invoke.
127 * Why 16. Because with 16 the only overlap we get on a hash of the
128 * low nibble of the protocol value is RARP/SNAP/X.25.
130 * NOTE: That is no longer true with the addition of VLAN tags. Not
131 * sure which should go first, but I bet it won't make much
132 * difference if we are running VLANs. The good news is that
133 * this protocol won't be in the list unless compiled in, so
134 * the average user (w/out VLANs) will not be adversely affected.
151 static DEFINE_SPINLOCK(ptype_lock);
152 static struct list_head ptype_base[16] __read_mostly; /* 16 way hashed list */
153 static struct list_head ptype_all __read_mostly; /* Taps */
155 #ifdef CONFIG_NET_DMA
157 struct dma_client client;
159 cpumask_t channel_mask;
160 struct dma_chan *channels[NR_CPUS];
163 static enum dma_state_client
164 netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
165 enum dma_state state);
167 static struct net_dma net_dma = {
169 .event_callback = netdev_dma_event,
175 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
178 * Pure readers hold dev_base_lock for reading.
180 * Writers must hold the rtnl semaphore while they loop through the
181 * dev_base_head list, and hold dev_base_lock for writing when they do the
182 * actual updates. This allows pure readers to access the list even
183 * while a writer is preparing to update it.
185 * To put it another way, dev_base_lock is held for writing only to
186 * protect against pure readers; the rtnl semaphore provides the
187 * protection against other writers.
189 * See, for example usages, register_netdevice() and
190 * unregister_netdevice(), which must be called with the rtnl
193 LIST_HEAD(dev_base_head);
194 DEFINE_RWLOCK(dev_base_lock);
196 EXPORT_SYMBOL(dev_base_head);
197 EXPORT_SYMBOL(dev_base_lock);
199 #define NETDEV_HASHBITS 8
200 static struct hlist_head dev_name_head[1<<NETDEV_HASHBITS];
201 static struct hlist_head dev_index_head[1<<NETDEV_HASHBITS];
203 static inline struct hlist_head *dev_name_hash(const char *name)
205 unsigned hash = full_name_hash(name, strnlen(name, IFNAMSIZ));
206 return &dev_name_head[hash & ((1<<NETDEV_HASHBITS)-1)];
209 static inline struct hlist_head *dev_index_hash(int ifindex)
211 return &dev_index_head[ifindex & ((1<<NETDEV_HASHBITS)-1)];
218 static RAW_NOTIFIER_HEAD(netdev_chain);
221 * Device drivers call our routines to queue packets here. We empty the
222 * queue in the local softnet handler.
225 DEFINE_PER_CPU(struct softnet_data, softnet_data);
228 extern int netdev_sysfs_init(void);
229 extern int netdev_register_sysfs(struct net_device *);
230 extern void netdev_unregister_sysfs(struct net_device *);
232 #define netdev_sysfs_init() (0)
233 #define netdev_register_sysfs(dev) (0)
234 #define netdev_unregister_sysfs(dev) do { } while(0)
237 #ifdef CONFIG_DEBUG_LOCK_ALLOC
239 * register_netdevice() inits dev->_xmit_lock and sets lockdep class
240 * according to dev->type
242 static const unsigned short netdev_lock_type[] =
243 {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
244 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
245 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
246 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
247 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
248 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
249 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
250 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
251 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
252 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
253 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
254 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
255 ARPHRD_FCFABRIC, ARPHRD_IEEE802_TR, ARPHRD_IEEE80211,
256 ARPHRD_IEEE80211_PRISM, ARPHRD_IEEE80211_RADIOTAP, ARPHRD_VOID,
259 static const char *netdev_lock_name[] =
260 {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
261 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
262 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
263 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
264 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
265 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
266 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
267 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
268 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
269 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
270 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
271 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
272 "_xmit_FCFABRIC", "_xmit_IEEE802_TR", "_xmit_IEEE80211",
273 "_xmit_IEEE80211_PRISM", "_xmit_IEEE80211_RADIOTAP", "_xmit_VOID",
276 static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
278 static inline unsigned short netdev_lock_pos(unsigned short dev_type)
282 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
283 if (netdev_lock_type[i] == dev_type)
285 /* the last key is used by default */
286 return ARRAY_SIZE(netdev_lock_type) - 1;
289 static inline void netdev_set_lockdep_class(spinlock_t *lock,
290 unsigned short dev_type)
294 i = netdev_lock_pos(dev_type);
295 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
296 netdev_lock_name[i]);
299 static inline void netdev_set_lockdep_class(spinlock_t *lock,
300 unsigned short dev_type)
305 /*******************************************************************************
307 Protocol management and registration routines
309 *******************************************************************************/
312 * Add a protocol ID to the list. Now that the input handler is
313 * smarter we can dispense with all the messy stuff that used to be
316 * BEWARE!!! Protocol handlers, mangling input packets,
317 * MUST BE last in hash buckets and checking protocol handlers
318 * MUST start from promiscuous ptype_all chain in net_bh.
319 * It is true now, do not change it.
320 * Explanation follows: if protocol handler, mangling packet, will
321 * be the first on list, it is not able to sense, that packet
322 * is cloned and should be copied-on-write, so that it will
323 * change it and subsequent readers will get broken packet.
328 * dev_add_pack - add packet handler
329 * @pt: packet type declaration
331 * Add a protocol handler to the networking stack. The passed &packet_type
332 * is linked into kernel lists and may not be freed until it has been
333 * removed from the kernel lists.
335 * This call does not sleep therefore it can not
336 * guarantee all CPU's that are in middle of receiving packets
337 * will see the new packet type (until the next received packet).
340 void dev_add_pack(struct packet_type *pt)
344 spin_lock_bh(&ptype_lock);
345 if (pt->type == htons(ETH_P_ALL))
346 list_add_rcu(&pt->list, &ptype_all);
348 hash = ntohs(pt->type) & 15;
349 list_add_rcu(&pt->list, &ptype_base[hash]);
351 spin_unlock_bh(&ptype_lock);
355 * __dev_remove_pack - remove packet handler
356 * @pt: packet type declaration
358 * Remove a protocol handler that was previously added to the kernel
359 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
360 * from the kernel lists and can be freed or reused once this function
363 * The packet type might still be in use by receivers
364 * and must not be freed until after all the CPU's have gone
365 * through a quiescent state.
367 void __dev_remove_pack(struct packet_type *pt)
369 struct list_head *head;
370 struct packet_type *pt1;
372 spin_lock_bh(&ptype_lock);
374 if (pt->type == htons(ETH_P_ALL))
377 head = &ptype_base[ntohs(pt->type) & 15];
379 list_for_each_entry(pt1, head, list) {
381 list_del_rcu(&pt->list);
386 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
388 spin_unlock_bh(&ptype_lock);
391 * dev_remove_pack - remove packet handler
392 * @pt: packet type declaration
394 * Remove a protocol handler that was previously added to the kernel
395 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
396 * from the kernel lists and can be freed or reused once this function
399 * This call sleeps to guarantee that no CPU is looking at the packet
402 void dev_remove_pack(struct packet_type *pt)
404 __dev_remove_pack(pt);
409 /******************************************************************************
411 Device Boot-time Settings Routines
413 *******************************************************************************/
415 /* Boot time configuration table */
416 static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
419 * netdev_boot_setup_add - add new setup entry
420 * @name: name of the device
421 * @map: configured settings for the device
423 * Adds new setup entry to the dev_boot_setup list. The function
424 * returns 0 on error and 1 on success. This is a generic routine to
427 static int netdev_boot_setup_add(char *name, struct ifmap *map)
429 struct netdev_boot_setup *s;
433 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
434 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
435 memset(s[i].name, 0, sizeof(s[i].name));
436 strcpy(s[i].name, name);
437 memcpy(&s[i].map, map, sizeof(s[i].map));
442 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
446 * netdev_boot_setup_check - check boot time settings
447 * @dev: the netdevice
449 * Check boot time settings for the device.
450 * The found settings are set for the device to be used
451 * later in the device probing.
452 * Returns 0 if no settings found, 1 if they are.
454 int netdev_boot_setup_check(struct net_device *dev)
456 struct netdev_boot_setup *s = dev_boot_setup;
459 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
460 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
461 !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
462 dev->irq = s[i].map.irq;
463 dev->base_addr = s[i].map.base_addr;
464 dev->mem_start = s[i].map.mem_start;
465 dev->mem_end = s[i].map.mem_end;
474 * netdev_boot_base - get address from boot time settings
475 * @prefix: prefix for network device
476 * @unit: id for network device
478 * Check boot time settings for the base address of device.
479 * The found settings are set for the device to be used
480 * later in the device probing.
481 * Returns 0 if no settings found.
483 unsigned long netdev_boot_base(const char *prefix, int unit)
485 const struct netdev_boot_setup *s = dev_boot_setup;
489 sprintf(name, "%s%d", prefix, unit);
492 * If device already registered then return base of 1
493 * to indicate not to probe for this interface
495 if (__dev_get_by_name(name))
498 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
499 if (!strcmp(name, s[i].name))
500 return s[i].map.base_addr;
505 * Saves at boot time configured settings for any netdevice.
507 int __init netdev_boot_setup(char *str)
512 str = get_options(str, ARRAY_SIZE(ints), ints);
517 memset(&map, 0, sizeof(map));
521 map.base_addr = ints[2];
523 map.mem_start = ints[3];
525 map.mem_end = ints[4];
527 /* Add new entry to the list */
528 return netdev_boot_setup_add(str, &map);
531 __setup("netdev=", netdev_boot_setup);
533 /*******************************************************************************
535 Device Interface Subroutines
537 *******************************************************************************/
540 * __dev_get_by_name - find a device by its name
541 * @name: name to find
543 * Find an interface by name. Must be called under RTNL semaphore
544 * or @dev_base_lock. If the name is found a pointer to the device
545 * is returned. If the name is not found then %NULL is returned. The
546 * reference counters are not incremented so the caller must be
547 * careful with locks.
550 struct net_device *__dev_get_by_name(const char *name)
552 struct hlist_node *p;
554 hlist_for_each(p, dev_name_hash(name)) {
555 struct net_device *dev
556 = hlist_entry(p, struct net_device, name_hlist);
557 if (!strncmp(dev->name, name, IFNAMSIZ))
564 * dev_get_by_name - find a device by its name
565 * @name: name to find
567 * Find an interface by name. This can be called from any
568 * context and does its own locking. The returned handle has
569 * the usage count incremented and the caller must use dev_put() to
570 * release it when it is no longer needed. %NULL is returned if no
571 * matching device is found.
574 struct net_device *dev_get_by_name(const char *name)
576 struct net_device *dev;
578 read_lock(&dev_base_lock);
579 dev = __dev_get_by_name(name);
582 read_unlock(&dev_base_lock);
587 * __dev_get_by_index - find a device by its ifindex
588 * @ifindex: index of device
590 * Search for an interface by index. Returns %NULL if the device
591 * is not found or a pointer to the device. The device has not
592 * had its reference counter increased so the caller must be careful
593 * about locking. The caller must hold either the RTNL semaphore
597 struct net_device *__dev_get_by_index(int ifindex)
599 struct hlist_node *p;
601 hlist_for_each(p, dev_index_hash(ifindex)) {
602 struct net_device *dev
603 = hlist_entry(p, struct net_device, index_hlist);
604 if (dev->ifindex == ifindex)
612 * dev_get_by_index - find a device by its ifindex
613 * @ifindex: index of device
615 * Search for an interface by index. Returns NULL if the device
616 * is not found or a pointer to the device. The device returned has
617 * had a reference added and the pointer is safe until the user calls
618 * dev_put to indicate they have finished with it.
621 struct net_device *dev_get_by_index(int ifindex)
623 struct net_device *dev;
625 read_lock(&dev_base_lock);
626 dev = __dev_get_by_index(ifindex);
629 read_unlock(&dev_base_lock);
634 * dev_getbyhwaddr - find a device by its hardware address
635 * @type: media type of device
636 * @ha: hardware address
638 * Search for an interface by MAC address. Returns NULL if the device
639 * is not found or a pointer to the device. The caller must hold the
640 * rtnl semaphore. The returned device has not had its ref count increased
641 * and the caller must therefore be careful about locking
644 * If the API was consistent this would be __dev_get_by_hwaddr
647 struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
649 struct net_device *dev;
654 if (dev->type == type &&
655 !memcmp(dev->dev_addr, ha, dev->addr_len))
661 EXPORT_SYMBOL(dev_getbyhwaddr);
663 struct net_device *__dev_getfirstbyhwtype(unsigned short type)
665 struct net_device *dev;
669 if (dev->type == type)
675 EXPORT_SYMBOL(__dev_getfirstbyhwtype);
677 struct net_device *dev_getfirstbyhwtype(unsigned short type)
679 struct net_device *dev;
682 dev = __dev_getfirstbyhwtype(type);
689 EXPORT_SYMBOL(dev_getfirstbyhwtype);
692 * dev_get_by_flags - find any device with given flags
693 * @if_flags: IFF_* values
694 * @mask: bitmask of bits in if_flags to check
696 * Search for any interface with the given flags. Returns NULL if a device
697 * is not found or a pointer to the device. The device returned has
698 * had a reference added and the pointer is safe until the user calls
699 * dev_put to indicate they have finished with it.
702 struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
704 struct net_device *dev, *ret;
707 read_lock(&dev_base_lock);
708 for_each_netdev(dev) {
709 if (((dev->flags ^ if_flags) & mask) == 0) {
715 read_unlock(&dev_base_lock);
720 * dev_valid_name - check if name is okay for network device
723 * Network device names need to be valid file names to
724 * to allow sysfs to work. We also disallow any kind of
727 int dev_valid_name(const char *name)
731 if (strlen(name) >= IFNAMSIZ)
733 if (!strcmp(name, ".") || !strcmp(name, ".."))
737 if (*name == '/' || isspace(*name))
745 * dev_alloc_name - allocate a name for a device
747 * @name: name format string
749 * Passed a format string - eg "lt%d" it will try and find a suitable
750 * id. It scans list of devices to build up a free map, then chooses
751 * the first empty slot. The caller must hold the dev_base or rtnl lock
752 * while allocating the name and adding the device in order to avoid
754 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
755 * Returns the number of the unit assigned or a negative errno code.
758 int dev_alloc_name(struct net_device *dev, const char *name)
763 const int max_netdevices = 8*PAGE_SIZE;
765 struct net_device *d;
767 p = strnchr(name, IFNAMSIZ-1, '%');
770 * Verify the string as this thing may have come from
771 * the user. There must be either one "%d" and no other "%"
774 if (p[1] != 'd' || strchr(p + 2, '%'))
777 /* Use one page as a bit array of possible slots */
778 inuse = (long *) get_zeroed_page(GFP_ATOMIC);
783 if (!sscanf(d->name, name, &i))
785 if (i < 0 || i >= max_netdevices)
788 /* avoid cases where sscanf is not exact inverse of printf */
789 snprintf(buf, sizeof(buf), name, i);
790 if (!strncmp(buf, d->name, IFNAMSIZ))
794 i = find_first_zero_bit(inuse, max_netdevices);
795 free_page((unsigned long) inuse);
798 snprintf(buf, sizeof(buf), name, i);
799 if (!__dev_get_by_name(buf)) {
800 strlcpy(dev->name, buf, IFNAMSIZ);
804 /* It is possible to run out of possible slots
805 * when the name is long and there isn't enough space left
806 * for the digits, or if all bits are used.
813 * dev_change_name - change name of a device
815 * @newname: name (or format string) must be at least IFNAMSIZ
817 * Change name of a device, can pass format strings "eth%d".
820 int dev_change_name(struct net_device *dev, char *newname)
822 char oldname[IFNAMSIZ];
828 if (dev->flags & IFF_UP)
831 if (!dev_valid_name(newname))
834 memcpy(oldname, dev->name, IFNAMSIZ);
836 if (strchr(newname, '%')) {
837 err = dev_alloc_name(dev, newname);
840 strcpy(newname, dev->name);
842 else if (__dev_get_by_name(newname))
845 strlcpy(dev->name, newname, IFNAMSIZ);
848 device_rename(&dev->dev, dev->name);
850 write_lock_bh(&dev_base_lock);
851 hlist_del(&dev->name_hlist);
852 hlist_add_head(&dev->name_hlist, dev_name_hash(dev->name));
853 write_unlock_bh(&dev_base_lock);
855 ret = raw_notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
856 ret = notifier_to_errno(ret);
861 "%s: name change rollback failed: %d.\n",
865 memcpy(dev->name, oldname, IFNAMSIZ);
874 * netdev_features_change - device changes features
875 * @dev: device to cause notification
877 * Called to indicate a device has changed features.
879 void netdev_features_change(struct net_device *dev)
881 raw_notifier_call_chain(&netdev_chain, NETDEV_FEAT_CHANGE, dev);
883 EXPORT_SYMBOL(netdev_features_change);
886 * netdev_state_change - device changes state
887 * @dev: device to cause notification
889 * Called to indicate a device has changed state. This function calls
890 * the notifier chains for netdev_chain and sends a NEWLINK message
891 * to the routing socket.
893 void netdev_state_change(struct net_device *dev)
895 if (dev->flags & IFF_UP) {
896 raw_notifier_call_chain(&netdev_chain,
898 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
903 * dev_load - load a network module
904 * @name: name of interface
906 * If a network interface is not present and the process has suitable
907 * privileges this function loads the module. If module loading is not
908 * available in this kernel then it becomes a nop.
911 void dev_load(const char *name)
913 struct net_device *dev;
915 read_lock(&dev_base_lock);
916 dev = __dev_get_by_name(name);
917 read_unlock(&dev_base_lock);
919 if (!dev && capable(CAP_SYS_MODULE))
920 request_module("%s", name);
923 static int default_rebuild_header(struct sk_buff *skb)
925 printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n",
926 skb->dev ? skb->dev->name : "NULL!!!");
932 * dev_open - prepare an interface for use.
933 * @dev: device to open
935 * Takes a device from down to up state. The device's private open
936 * function is invoked and then the multicast lists are loaded. Finally
937 * the device is moved into the up state and a %NETDEV_UP message is
938 * sent to the netdev notifier chain.
940 * Calling this function on an active interface is a nop. On a failure
941 * a negative errno code is returned.
943 int dev_open(struct net_device *dev)
951 if (dev->flags & IFF_UP)
955 * Is it even present?
957 if (!netif_device_present(dev))
961 * Call device private open method
963 set_bit(__LINK_STATE_START, &dev->state);
965 ret = dev->open(dev);
967 clear_bit(__LINK_STATE_START, &dev->state);
971 * If it went open OK then:
978 dev->flags |= IFF_UP;
981 * Initialize multicasting status
983 dev_set_rx_mode(dev);
986 * Wakeup transmit queue engine
991 * ... and announce new interface.
993 raw_notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
999 * dev_close - shutdown an interface.
1000 * @dev: device to shutdown
1002 * This function moves an active device into down state. A
1003 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1004 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1007 int dev_close(struct net_device *dev)
1009 if (!(dev->flags & IFF_UP))
1013 * Tell people we are going down, so that they can
1014 * prepare to death, when device is still operating.
1016 raw_notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
1018 dev_deactivate(dev);
1020 clear_bit(__LINK_STATE_START, &dev->state);
1022 /* Synchronize to scheduled poll. We cannot touch poll list,
1023 * it can be even on different cpu. So just clear netif_running().
1025 * dev->stop() will invoke napi_disable() on all of it's
1026 * napi_struct instances on this device.
1028 smp_mb__after_clear_bit(); /* Commit netif_running(). */
1031 * Call the device specific close. This cannot fail.
1032 * Only if device is UP
1034 * We allow it to be called even after a DETACH hot-plug
1041 * Device is now down.
1044 dev->flags &= ~IFF_UP;
1047 * Tell people we are down
1049 raw_notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
1056 * Device change register/unregister. These are not inline or static
1057 * as we export them to the world.
1061 * register_netdevice_notifier - register a network notifier block
1064 * Register a notifier to be called when network device events occur.
1065 * The notifier passed is linked into the kernel structures and must
1066 * not be reused until it has been unregistered. A negative errno code
1067 * is returned on a failure.
1069 * When registered all registration and up events are replayed
1070 * to the new notifier to allow device to have a race free
1071 * view of the network device list.
1074 int register_netdevice_notifier(struct notifier_block *nb)
1076 struct net_device *dev;
1077 struct net_device *last;
1081 err = raw_notifier_chain_register(&netdev_chain, nb);
1085 for_each_netdev(dev) {
1086 err = nb->notifier_call(nb, NETDEV_REGISTER, dev);
1087 err = notifier_to_errno(err);
1091 if (!(dev->flags & IFF_UP))
1094 nb->notifier_call(nb, NETDEV_UP, dev);
1103 for_each_netdev(dev) {
1107 if (dev->flags & IFF_UP) {
1108 nb->notifier_call(nb, NETDEV_GOING_DOWN, dev);
1109 nb->notifier_call(nb, NETDEV_DOWN, dev);
1111 nb->notifier_call(nb, NETDEV_UNREGISTER, dev);
1117 * unregister_netdevice_notifier - unregister a network notifier block
1120 * Unregister a notifier previously registered by
1121 * register_netdevice_notifier(). The notifier is unlinked into the
1122 * kernel structures and may then be reused. A negative errno code
1123 * is returned on a failure.
1126 int unregister_netdevice_notifier(struct notifier_block *nb)
1131 err = raw_notifier_chain_unregister(&netdev_chain, nb);
1137 * call_netdevice_notifiers - call all network notifier blocks
1138 * @val: value passed unmodified to notifier function
1139 * @v: pointer passed unmodified to notifier function
1141 * Call all network notifier blocks. Parameters and return value
1142 * are as for raw_notifier_call_chain().
1145 int call_netdevice_notifiers(unsigned long val, void *v)
1147 return raw_notifier_call_chain(&netdev_chain, val, v);
1150 /* When > 0 there are consumers of rx skb time stamps */
1151 static atomic_t netstamp_needed = ATOMIC_INIT(0);
1153 void net_enable_timestamp(void)
1155 atomic_inc(&netstamp_needed);
1158 void net_disable_timestamp(void)
1160 atomic_dec(&netstamp_needed);
1163 static inline void net_timestamp(struct sk_buff *skb)
1165 if (atomic_read(&netstamp_needed))
1166 __net_timestamp(skb);
1168 skb->tstamp.tv64 = 0;
1172 * Support routine. Sends outgoing frames to any network
1173 * taps currently in use.
1176 static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1178 struct packet_type *ptype;
1183 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1184 /* Never send packets back to the socket
1185 * they originated from - MvS (miquels@drinkel.ow.org)
1187 if ((ptype->dev == dev || !ptype->dev) &&
1188 (ptype->af_packet_priv == NULL ||
1189 (struct sock *)ptype->af_packet_priv != skb->sk)) {
1190 struct sk_buff *skb2= skb_clone(skb, GFP_ATOMIC);
1194 /* skb->nh should be correctly
1195 set by sender, so that the second statement is
1196 just protection against buggy protocols.
1198 skb_reset_mac_header(skb2);
1200 if (skb_network_header(skb2) < skb2->data ||
1201 skb2->network_header > skb2->tail) {
1202 if (net_ratelimit())
1203 printk(KERN_CRIT "protocol %04x is "
1205 skb2->protocol, dev->name);
1206 skb_reset_network_header(skb2);
1209 skb2->transport_header = skb2->network_header;
1210 skb2->pkt_type = PACKET_OUTGOING;
1211 ptype->func(skb2, skb->dev, ptype, skb->dev);
1218 void __netif_schedule(struct net_device *dev)
1220 if (!test_and_set_bit(__LINK_STATE_SCHED, &dev->state)) {
1221 unsigned long flags;
1222 struct softnet_data *sd;
1224 local_irq_save(flags);
1225 sd = &__get_cpu_var(softnet_data);
1226 dev->next_sched = sd->output_queue;
1227 sd->output_queue = dev;
1228 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1229 local_irq_restore(flags);
1232 EXPORT_SYMBOL(__netif_schedule);
1234 void dev_kfree_skb_irq(struct sk_buff *skb)
1236 if (atomic_dec_and_test(&skb->users)) {
1237 struct softnet_data *sd;
1238 unsigned long flags;
1240 local_irq_save(flags);
1241 sd = &__get_cpu_var(softnet_data);
1242 skb->next = sd->completion_queue;
1243 sd->completion_queue = skb;
1244 raise_softirq_irqoff(NET_TX_SOFTIRQ);
1245 local_irq_restore(flags);
1248 EXPORT_SYMBOL(dev_kfree_skb_irq);
1250 void dev_kfree_skb_any(struct sk_buff *skb)
1252 if (in_irq() || irqs_disabled())
1253 dev_kfree_skb_irq(skb);
1257 EXPORT_SYMBOL(dev_kfree_skb_any);
1261 * netif_device_detach - mark device as removed
1262 * @dev: network device
1264 * Mark device as removed from system and therefore no longer available.
1266 void netif_device_detach(struct net_device *dev)
1268 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
1269 netif_running(dev)) {
1270 netif_stop_queue(dev);
1273 EXPORT_SYMBOL(netif_device_detach);
1276 * netif_device_attach - mark device as attached
1277 * @dev: network device
1279 * Mark device as attached from system and restart if needed.
1281 void netif_device_attach(struct net_device *dev)
1283 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
1284 netif_running(dev)) {
1285 netif_wake_queue(dev);
1286 __netdev_watchdog_up(dev);
1289 EXPORT_SYMBOL(netif_device_attach);
1293 * Invalidate hardware checksum when packet is to be mangled, and
1294 * complete checksum manually on outgoing path.
1296 int skb_checksum_help(struct sk_buff *skb)
1299 int ret = 0, offset;
1301 if (skb->ip_summed == CHECKSUM_COMPLETE)
1302 goto out_set_summed;
1304 if (unlikely(skb_shinfo(skb)->gso_size)) {
1305 /* Let GSO fix up the checksum. */
1306 goto out_set_summed;
1309 if (skb_cloned(skb)) {
1310 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
1315 offset = skb->csum_start - skb_headroom(skb);
1316 BUG_ON(offset > (int)skb->len);
1317 csum = skb_checksum(skb, offset, skb->len-offset, 0);
1319 offset = skb_headlen(skb) - offset;
1320 BUG_ON(offset <= 0);
1321 BUG_ON(skb->csum_offset + 2 > offset);
1323 *(__sum16 *)(skb->head + skb->csum_start + skb->csum_offset) =
1326 skb->ip_summed = CHECKSUM_NONE;
1332 * skb_gso_segment - Perform segmentation on skb.
1333 * @skb: buffer to segment
1334 * @features: features for the output path (see dev->features)
1336 * This function segments the given skb and returns a list of segments.
1338 * It may return NULL if the skb requires no segmentation. This is
1339 * only possible when GSO is used for verifying header integrity.
1341 struct sk_buff *skb_gso_segment(struct sk_buff *skb, int features)
1343 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
1344 struct packet_type *ptype;
1345 __be16 type = skb->protocol;
1348 BUG_ON(skb_shinfo(skb)->frag_list);
1350 skb_reset_mac_header(skb);
1351 skb->mac_len = skb->network_header - skb->mac_header;
1352 __skb_pull(skb, skb->mac_len);
1354 if (WARN_ON(skb->ip_summed != CHECKSUM_PARTIAL)) {
1355 if (skb_header_cloned(skb) &&
1356 (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC)))
1357 return ERR_PTR(err);
1361 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type) & 15], list) {
1362 if (ptype->type == type && !ptype->dev && ptype->gso_segment) {
1363 if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) {
1364 err = ptype->gso_send_check(skb);
1365 segs = ERR_PTR(err);
1366 if (err || skb_gso_ok(skb, features))
1368 __skb_push(skb, (skb->data -
1369 skb_network_header(skb)));
1371 segs = ptype->gso_segment(skb, features);
1377 __skb_push(skb, skb->data - skb_mac_header(skb));
1382 EXPORT_SYMBOL(skb_gso_segment);
1384 /* Take action when hardware reception checksum errors are detected. */
1386 void netdev_rx_csum_fault(struct net_device *dev)
1388 if (net_ratelimit()) {
1389 printk(KERN_ERR "%s: hw csum failure.\n",
1390 dev ? dev->name : "<unknown>");
1394 EXPORT_SYMBOL(netdev_rx_csum_fault);
1397 /* Actually, we should eliminate this check as soon as we know, that:
1398 * 1. IOMMU is present and allows to map all the memory.
1399 * 2. No high memory really exists on this machine.
1402 static inline int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1404 #ifdef CONFIG_HIGHMEM
1407 if (dev->features & NETIF_F_HIGHDMA)
1410 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1411 if (PageHighMem(skb_shinfo(skb)->frags[i].page))
1419 void (*destructor)(struct sk_buff *skb);
1422 #define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb)
1424 static void dev_gso_skb_destructor(struct sk_buff *skb)
1426 struct dev_gso_cb *cb;
1429 struct sk_buff *nskb = skb->next;
1431 skb->next = nskb->next;
1434 } while (skb->next);
1436 cb = DEV_GSO_CB(skb);
1438 cb->destructor(skb);
1442 * dev_gso_segment - Perform emulated hardware segmentation on skb.
1443 * @skb: buffer to segment
1445 * This function segments the given skb and stores the list of segments
1448 static int dev_gso_segment(struct sk_buff *skb)
1450 struct net_device *dev = skb->dev;
1451 struct sk_buff *segs;
1452 int features = dev->features & ~(illegal_highdma(dev, skb) ?
1455 segs = skb_gso_segment(skb, features);
1457 /* Verifying header integrity only. */
1461 if (unlikely(IS_ERR(segs)))
1462 return PTR_ERR(segs);
1465 DEV_GSO_CB(skb)->destructor = skb->destructor;
1466 skb->destructor = dev_gso_skb_destructor;
1471 int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev)
1473 if (likely(!skb->next)) {
1474 if (!list_empty(&ptype_all))
1475 dev_queue_xmit_nit(skb, dev);
1477 if (netif_needs_gso(dev, skb)) {
1478 if (unlikely(dev_gso_segment(skb)))
1484 return dev->hard_start_xmit(skb, dev);
1489 struct sk_buff *nskb = skb->next;
1492 skb->next = nskb->next;
1494 rc = dev->hard_start_xmit(nskb, dev);
1496 nskb->next = skb->next;
1500 if (unlikely((netif_queue_stopped(dev) ||
1501 netif_subqueue_stopped(dev, skb->queue_mapping)) &&
1503 return NETDEV_TX_BUSY;
1504 } while (skb->next);
1506 skb->destructor = DEV_GSO_CB(skb)->destructor;
1513 #define HARD_TX_LOCK(dev, cpu) { \
1514 if ((dev->features & NETIF_F_LLTX) == 0) { \
1515 netif_tx_lock(dev); \
1519 #define HARD_TX_UNLOCK(dev) { \
1520 if ((dev->features & NETIF_F_LLTX) == 0) { \
1521 netif_tx_unlock(dev); \
1526 * dev_queue_xmit - transmit a buffer
1527 * @skb: buffer to transmit
1529 * Queue a buffer for transmission to a network device. The caller must
1530 * have set the device and priority and built the buffer before calling
1531 * this function. The function can be called from an interrupt.
1533 * A negative errno code is returned on a failure. A success does not
1534 * guarantee the frame will be transmitted as it may be dropped due
1535 * to congestion or traffic shaping.
1537 * -----------------------------------------------------------------------------------
1538 * I notice this method can also return errors from the queue disciplines,
1539 * including NET_XMIT_DROP, which is a positive value. So, errors can also
1542 * Regardless of the return value, the skb is consumed, so it is currently
1543 * difficult to retry a send to this method. (You can bump the ref count
1544 * before sending to hold a reference for retry if you are careful.)
1546 * When calling this method, interrupts MUST be enabled. This is because
1547 * the BH enable code must have IRQs enabled so that it will not deadlock.
1551 int dev_queue_xmit(struct sk_buff *skb)
1553 struct net_device *dev = skb->dev;
1557 /* GSO will handle the following emulations directly. */
1558 if (netif_needs_gso(dev, skb))
1561 if (skb_shinfo(skb)->frag_list &&
1562 !(dev->features & NETIF_F_FRAGLIST) &&
1563 __skb_linearize(skb))
1566 /* Fragmented skb is linearized if device does not support SG,
1567 * or if at least one of fragments is in highmem and device
1568 * does not support DMA from it.
1570 if (skb_shinfo(skb)->nr_frags &&
1571 (!(dev->features & NETIF_F_SG) || illegal_highdma(dev, skb)) &&
1572 __skb_linearize(skb))
1575 /* If packet is not checksummed and device does not support
1576 * checksumming for this protocol, complete checksumming here.
1578 if (skb->ip_summed == CHECKSUM_PARTIAL) {
1579 skb_set_transport_header(skb, skb->csum_start -
1582 if (!(dev->features & NETIF_F_GEN_CSUM) &&
1583 !((dev->features & NETIF_F_IP_CSUM) &&
1584 skb->protocol == htons(ETH_P_IP)) &&
1585 !((dev->features & NETIF_F_IPV6_CSUM) &&
1586 skb->protocol == htons(ETH_P_IPV6)))
1587 if (skb_checksum_help(skb))
1592 spin_lock_prefetch(&dev->queue_lock);
1594 /* Disable soft irqs for various locks below. Also
1595 * stops preemption for RCU.
1599 /* Updates of qdisc are serialized by queue_lock.
1600 * The struct Qdisc which is pointed to by qdisc is now a
1601 * rcu structure - it may be accessed without acquiring
1602 * a lock (but the structure may be stale.) The freeing of the
1603 * qdisc will be deferred until it's known that there are no
1604 * more references to it.
1606 * If the qdisc has an enqueue function, we still need to
1607 * hold the queue_lock before calling it, since queue_lock
1608 * also serializes access to the device queue.
1611 q = rcu_dereference(dev->qdisc);
1612 #ifdef CONFIG_NET_CLS_ACT
1613 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_EGRESS);
1616 /* Grab device queue */
1617 spin_lock(&dev->queue_lock);
1620 /* reset queue_mapping to zero */
1621 skb->queue_mapping = 0;
1622 rc = q->enqueue(skb, q);
1624 spin_unlock(&dev->queue_lock);
1626 rc = rc == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : rc;
1629 spin_unlock(&dev->queue_lock);
1632 /* The device has no queue. Common case for software devices:
1633 loopback, all the sorts of tunnels...
1635 Really, it is unlikely that netif_tx_lock protection is necessary
1636 here. (f.e. loopback and IP tunnels are clean ignoring statistics
1638 However, it is possible, that they rely on protection
1641 Check this and shot the lock. It is not prone from deadlocks.
1642 Either shot noqueue qdisc, it is even simpler 8)
1644 if (dev->flags & IFF_UP) {
1645 int cpu = smp_processor_id(); /* ok because BHs are off */
1647 if (dev->xmit_lock_owner != cpu) {
1649 HARD_TX_LOCK(dev, cpu);
1651 if (!netif_queue_stopped(dev) &&
1652 !netif_subqueue_stopped(dev, skb->queue_mapping)) {
1654 if (!dev_hard_start_xmit(skb, dev)) {
1655 HARD_TX_UNLOCK(dev);
1659 HARD_TX_UNLOCK(dev);
1660 if (net_ratelimit())
1661 printk(KERN_CRIT "Virtual device %s asks to "
1662 "queue packet!\n", dev->name);
1664 /* Recursion is detected! It is possible,
1666 if (net_ratelimit())
1667 printk(KERN_CRIT "Dead loop on virtual device "
1668 "%s, fix it urgently!\n", dev->name);
1673 rcu_read_unlock_bh();
1679 rcu_read_unlock_bh();
1684 /*=======================================================================
1686 =======================================================================*/
1688 int netdev_max_backlog __read_mostly = 1000;
1689 int netdev_budget __read_mostly = 300;
1690 int weight_p __read_mostly = 64; /* old backlog weight */
1692 DEFINE_PER_CPU(struct netif_rx_stats, netdev_rx_stat) = { 0, };
1696 * netif_rx - post buffer to the network code
1697 * @skb: buffer to post
1699 * This function receives a packet from a device driver and queues it for
1700 * the upper (protocol) levels to process. It always succeeds. The buffer
1701 * may be dropped during processing for congestion control or by the
1705 * NET_RX_SUCCESS (no congestion)
1706 * NET_RX_CN_LOW (low congestion)
1707 * NET_RX_CN_MOD (moderate congestion)
1708 * NET_RX_CN_HIGH (high congestion)
1709 * NET_RX_DROP (packet was dropped)
1713 int netif_rx(struct sk_buff *skb)
1715 struct softnet_data *queue;
1716 unsigned long flags;
1718 /* if netpoll wants it, pretend we never saw it */
1719 if (netpoll_rx(skb))
1722 if (!skb->tstamp.tv64)
1726 * The code is rearranged so that the path is the most
1727 * short when CPU is congested, but is still operating.
1729 local_irq_save(flags);
1730 queue = &__get_cpu_var(softnet_data);
1732 __get_cpu_var(netdev_rx_stat).total++;
1733 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1734 if (queue->input_pkt_queue.qlen) {
1737 __skb_queue_tail(&queue->input_pkt_queue, skb);
1738 local_irq_restore(flags);
1739 return NET_RX_SUCCESS;
1742 napi_schedule(&queue->backlog);
1746 __get_cpu_var(netdev_rx_stat).dropped++;
1747 local_irq_restore(flags);
1753 int netif_rx_ni(struct sk_buff *skb)
1758 err = netif_rx(skb);
1759 if (local_softirq_pending())
1766 EXPORT_SYMBOL(netif_rx_ni);
1768 static inline struct net_device *skb_bond(struct sk_buff *skb)
1770 struct net_device *dev = skb->dev;
1773 if (skb_bond_should_drop(skb)) {
1777 skb->dev = dev->master;
1784 static void net_tx_action(struct softirq_action *h)
1786 struct softnet_data *sd = &__get_cpu_var(softnet_data);
1788 if (sd->completion_queue) {
1789 struct sk_buff *clist;
1791 local_irq_disable();
1792 clist = sd->completion_queue;
1793 sd->completion_queue = NULL;
1797 struct sk_buff *skb = clist;
1798 clist = clist->next;
1800 BUG_TRAP(!atomic_read(&skb->users));
1805 if (sd->output_queue) {
1806 struct net_device *head;
1808 local_irq_disable();
1809 head = sd->output_queue;
1810 sd->output_queue = NULL;
1814 struct net_device *dev = head;
1815 head = head->next_sched;
1817 smp_mb__before_clear_bit();
1818 clear_bit(__LINK_STATE_SCHED, &dev->state);
1820 if (spin_trylock(&dev->queue_lock)) {
1822 spin_unlock(&dev->queue_lock);
1824 netif_schedule(dev);
1830 static inline int deliver_skb(struct sk_buff *skb,
1831 struct packet_type *pt_prev,
1832 struct net_device *orig_dev)
1834 atomic_inc(&skb->users);
1835 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1838 #if defined(CONFIG_BRIDGE) || defined (CONFIG_BRIDGE_MODULE)
1839 /* These hooks defined here for ATM */
1841 struct net_bridge_fdb_entry *(*br_fdb_get_hook)(struct net_bridge *br,
1842 unsigned char *addr);
1843 void (*br_fdb_put_hook)(struct net_bridge_fdb_entry *ent) __read_mostly;
1846 * If bridge module is loaded call bridging hook.
1847 * returns NULL if packet was consumed.
1849 struct sk_buff *(*br_handle_frame_hook)(struct net_bridge_port *p,
1850 struct sk_buff *skb) __read_mostly;
1851 static inline struct sk_buff *handle_bridge(struct sk_buff *skb,
1852 struct packet_type **pt_prev, int *ret,
1853 struct net_device *orig_dev)
1855 struct net_bridge_port *port;
1857 if (skb->pkt_type == PACKET_LOOPBACK ||
1858 (port = rcu_dereference(skb->dev->br_port)) == NULL)
1862 *ret = deliver_skb(skb, *pt_prev, orig_dev);
1866 return br_handle_frame_hook(port, skb);
1869 #define handle_bridge(skb, pt_prev, ret, orig_dev) (skb)
1872 #if defined(CONFIG_MACVLAN) || defined(CONFIG_MACVLAN_MODULE)
1873 struct sk_buff *(*macvlan_handle_frame_hook)(struct sk_buff *skb) __read_mostly;
1874 EXPORT_SYMBOL_GPL(macvlan_handle_frame_hook);
1876 static inline struct sk_buff *handle_macvlan(struct sk_buff *skb,
1877 struct packet_type **pt_prev,
1879 struct net_device *orig_dev)
1881 if (skb->dev->macvlan_port == NULL)
1885 *ret = deliver_skb(skb, *pt_prev, orig_dev);
1888 return macvlan_handle_frame_hook(skb);
1891 #define handle_macvlan(skb, pt_prev, ret, orig_dev) (skb)
1894 #ifdef CONFIG_NET_CLS_ACT
1895 /* TODO: Maybe we should just force sch_ingress to be compiled in
1896 * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions
1897 * a compare and 2 stores extra right now if we dont have it on
1898 * but have CONFIG_NET_CLS_ACT
1899 * NOTE: This doesnt stop any functionality; if you dont have
1900 * the ingress scheduler, you just cant add policies on ingress.
1903 static int ing_filter(struct sk_buff *skb)
1906 struct net_device *dev = skb->dev;
1907 int result = TC_ACT_OK;
1909 if (dev->qdisc_ingress) {
1910 __u32 ttl = (__u32) G_TC_RTTL(skb->tc_verd);
1911 if (MAX_RED_LOOP < ttl++) {
1912 printk(KERN_WARNING "Redir loop detected Dropping packet (%d->%d)\n",
1913 skb->iif, skb->dev->ifindex);
1917 skb->tc_verd = SET_TC_RTTL(skb->tc_verd,ttl);
1919 skb->tc_verd = SET_TC_AT(skb->tc_verd,AT_INGRESS);
1921 spin_lock(&dev->ingress_lock);
1922 if ((q = dev->qdisc_ingress) != NULL)
1923 result = q->enqueue(skb, q);
1924 spin_unlock(&dev->ingress_lock);
1932 int netif_receive_skb(struct sk_buff *skb)
1934 struct packet_type *ptype, *pt_prev;
1935 struct net_device *orig_dev;
1936 int ret = NET_RX_DROP;
1939 /* if we've gotten here through NAPI, check netpoll */
1940 if (netpoll_receive_skb(skb))
1943 if (!skb->tstamp.tv64)
1947 skb->iif = skb->dev->ifindex;
1949 orig_dev = skb_bond(skb);
1954 __get_cpu_var(netdev_rx_stat).total++;
1956 skb_reset_network_header(skb);
1957 skb_reset_transport_header(skb);
1958 skb->mac_len = skb->network_header - skb->mac_header;
1964 #ifdef CONFIG_NET_CLS_ACT
1965 if (skb->tc_verd & TC_NCLS) {
1966 skb->tc_verd = CLR_TC_NCLS(skb->tc_verd);
1971 list_for_each_entry_rcu(ptype, &ptype_all, list) {
1972 if (!ptype->dev || ptype->dev == skb->dev) {
1974 ret = deliver_skb(skb, pt_prev, orig_dev);
1979 #ifdef CONFIG_NET_CLS_ACT
1981 ret = deliver_skb(skb, pt_prev, orig_dev);
1982 pt_prev = NULL; /* noone else should process this after*/
1984 skb->tc_verd = SET_TC_OK2MUNGE(skb->tc_verd);
1987 ret = ing_filter(skb);
1989 if (ret == TC_ACT_SHOT || (ret == TC_ACT_STOLEN)) {
1998 skb = handle_bridge(skb, &pt_prev, &ret, orig_dev);
2001 skb = handle_macvlan(skb, &pt_prev, &ret, orig_dev);
2005 type = skb->protocol;
2006 list_for_each_entry_rcu(ptype, &ptype_base[ntohs(type)&15], list) {
2007 if (ptype->type == type &&
2008 (!ptype->dev || ptype->dev == skb->dev)) {
2010 ret = deliver_skb(skb, pt_prev, orig_dev);
2016 ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2019 /* Jamal, now you will not able to escape explaining
2020 * me how you were going to use this. :-)
2030 static int process_backlog(struct napi_struct *napi, int quota)
2033 struct softnet_data *queue = &__get_cpu_var(softnet_data);
2034 unsigned long start_time = jiffies;
2036 napi->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);
2044 __napi_complete(napi);
2053 netif_receive_skb(skb);
2056 } while (++work < quota && jiffies == start_time);
2062 * __napi_schedule - schedule for receive
2063 * @napi: entry to schedule
2065 * The entry's receive function will be scheduled to run
2067 void fastcall __napi_schedule(struct napi_struct *n)
2069 unsigned long flags;
2071 local_irq_save(flags);
2072 list_add_tail(&n->poll_list, &__get_cpu_var(softnet_data).poll_list);
2073 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2074 local_irq_restore(flags);
2076 EXPORT_SYMBOL(__napi_schedule);
2079 static void net_rx_action(struct softirq_action *h)
2081 struct list_head *list = &__get_cpu_var(softnet_data).poll_list;
2082 unsigned long start_time = jiffies;
2083 int budget = netdev_budget;
2086 local_irq_disable();
2088 while (!list_empty(list)) {
2089 struct napi_struct *n;
2092 /* If softirq window is exhuasted then punt.
2094 * Note that this is a slight policy change from the
2095 * previous NAPI code, which would allow up to 2
2096 * jiffies to pass before breaking out. The test
2097 * used to be "jiffies - start_time > 1".
2099 if (unlikely(budget <= 0 || jiffies != start_time))
2104 /* Even though interrupts have been re-enabled, this
2105 * access is safe because interrupts can only add new
2106 * entries to the tail of this list, and only ->poll()
2107 * calls can remove this head entry from the list.
2109 n = list_entry(list->next, struct napi_struct, poll_list);
2111 have = netpoll_poll_lock(n);
2115 work = n->poll(n, weight);
2117 WARN_ON_ONCE(work > weight);
2121 local_irq_disable();
2123 /* Drivers must not modify the NAPI state if they
2124 * consume the entire weight. In such cases this code
2125 * still "owns" the NAPI instance and therefore can
2126 * move the instance around on the list at-will.
2128 if (unlikely(work == weight))
2129 list_move_tail(&n->poll_list, list);
2131 netpoll_poll_unlock(have);
2136 #ifdef CONFIG_NET_DMA
2138 * There may not be any more sk_buffs coming right now, so push
2139 * any pending DMA copies to hardware
2141 if (!cpus_empty(net_dma.channel_mask)) {
2143 for_each_cpu_mask(chan_idx, net_dma.channel_mask) {
2144 struct dma_chan *chan = net_dma.channels[chan_idx];
2146 dma_async_memcpy_issue_pending(chan);
2154 __get_cpu_var(netdev_rx_stat).time_squeeze++;
2155 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
2159 static gifconf_func_t * gifconf_list [NPROTO];
2162 * register_gifconf - register a SIOCGIF handler
2163 * @family: Address family
2164 * @gifconf: Function handler
2166 * Register protocol dependent address dumping routines. The handler
2167 * that is passed must not be freed or reused until it has been replaced
2168 * by another handler.
2170 int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
2172 if (family >= NPROTO)
2174 gifconf_list[family] = gifconf;
2180 * Map an interface index to its name (SIOCGIFNAME)
2184 * We need this ioctl for efficient implementation of the
2185 * if_indextoname() function required by the IPv6 API. Without
2186 * it, we would have to search all the interfaces to find a
2190 static int dev_ifname(struct ifreq __user *arg)
2192 struct net_device *dev;
2196 * Fetch the caller's info block.
2199 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2202 read_lock(&dev_base_lock);
2203 dev = __dev_get_by_index(ifr.ifr_ifindex);
2205 read_unlock(&dev_base_lock);
2209 strcpy(ifr.ifr_name, dev->name);
2210 read_unlock(&dev_base_lock);
2212 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2218 * Perform a SIOCGIFCONF call. This structure will change
2219 * size eventually, and there is nothing I can do about it.
2220 * Thus we will need a 'compatibility mode'.
2223 static int dev_ifconf(char __user *arg)
2226 struct net_device *dev;
2233 * Fetch the caller's info block.
2236 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
2243 * Loop over the interfaces, and write an info block for each.
2247 for_each_netdev(dev) {
2248 for (i = 0; i < NPROTO; i++) {
2249 if (gifconf_list[i]) {
2252 done = gifconf_list[i](dev, NULL, 0);
2254 done = gifconf_list[i](dev, pos + total,
2264 * All done. Write the updated control block back to the caller.
2266 ifc.ifc_len = total;
2269 * Both BSD and Solaris return 0 here, so we do too.
2271 return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0;
2274 #ifdef CONFIG_PROC_FS
2276 * This is invoked by the /proc filesystem handler to display a device
2279 void *dev_seq_start(struct seq_file *seq, loff_t *pos)
2282 struct net_device *dev;
2284 read_lock(&dev_base_lock);
2286 return SEQ_START_TOKEN;
2289 for_each_netdev(dev)
2296 void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2299 return v == SEQ_START_TOKEN ?
2300 first_net_device() : next_net_device((struct net_device *)v);
2303 void dev_seq_stop(struct seq_file *seq, void *v)
2305 read_unlock(&dev_base_lock);
2308 static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev)
2310 struct net_device_stats *stats = dev->get_stats(dev);
2312 seq_printf(seq, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu "
2313 "%8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
2314 dev->name, stats->rx_bytes, stats->rx_packets,
2316 stats->rx_dropped + stats->rx_missed_errors,
2317 stats->rx_fifo_errors,
2318 stats->rx_length_errors + stats->rx_over_errors +
2319 stats->rx_crc_errors + stats->rx_frame_errors,
2320 stats->rx_compressed, stats->multicast,
2321 stats->tx_bytes, stats->tx_packets,
2322 stats->tx_errors, stats->tx_dropped,
2323 stats->tx_fifo_errors, stats->collisions,
2324 stats->tx_carrier_errors +
2325 stats->tx_aborted_errors +
2326 stats->tx_window_errors +
2327 stats->tx_heartbeat_errors,
2328 stats->tx_compressed);
2332 * Called from the PROCfs module. This now uses the new arbitrary sized
2333 * /proc/net interface to create /proc/net/dev
2335 static int dev_seq_show(struct seq_file *seq, void *v)
2337 if (v == SEQ_START_TOKEN)
2338 seq_puts(seq, "Inter-| Receive "
2340 " face |bytes packets errs drop fifo frame "
2341 "compressed multicast|bytes packets errs "
2342 "drop fifo colls carrier compressed\n");
2344 dev_seq_printf_stats(seq, v);
2348 static struct netif_rx_stats *softnet_get_online(loff_t *pos)
2350 struct netif_rx_stats *rc = NULL;
2352 while (*pos < NR_CPUS)
2353 if (cpu_online(*pos)) {
2354 rc = &per_cpu(netdev_rx_stat, *pos);
2361 static void *softnet_seq_start(struct seq_file *seq, loff_t *pos)
2363 return softnet_get_online(pos);
2366 static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2369 return softnet_get_online(pos);
2372 static void softnet_seq_stop(struct seq_file *seq, void *v)
2376 static int softnet_seq_show(struct seq_file *seq, void *v)
2378 struct netif_rx_stats *s = v;
2380 seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
2381 s->total, s->dropped, s->time_squeeze, 0,
2382 0, 0, 0, 0, /* was fastroute */
2387 static const struct seq_operations dev_seq_ops = {
2388 .start = dev_seq_start,
2389 .next = dev_seq_next,
2390 .stop = dev_seq_stop,
2391 .show = dev_seq_show,
2394 static int dev_seq_open(struct inode *inode, struct file *file)
2396 return seq_open(file, &dev_seq_ops);
2399 static const struct file_operations dev_seq_fops = {
2400 .owner = THIS_MODULE,
2401 .open = dev_seq_open,
2403 .llseek = seq_lseek,
2404 .release = seq_release,
2407 static const struct seq_operations softnet_seq_ops = {
2408 .start = softnet_seq_start,
2409 .next = softnet_seq_next,
2410 .stop = softnet_seq_stop,
2411 .show = softnet_seq_show,
2414 static int softnet_seq_open(struct inode *inode, struct file *file)
2416 return seq_open(file, &softnet_seq_ops);
2419 static const struct file_operations softnet_seq_fops = {
2420 .owner = THIS_MODULE,
2421 .open = softnet_seq_open,
2423 .llseek = seq_lseek,
2424 .release = seq_release,
2427 static void *ptype_get_idx(loff_t pos)
2429 struct packet_type *pt = NULL;
2433 list_for_each_entry_rcu(pt, &ptype_all, list) {
2439 for (t = 0; t < 16; t++) {
2440 list_for_each_entry_rcu(pt, &ptype_base[t], list) {
2449 static void *ptype_seq_start(struct seq_file *seq, loff_t *pos)
2452 return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN;
2455 static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2457 struct packet_type *pt;
2458 struct list_head *nxt;
2462 if (v == SEQ_START_TOKEN)
2463 return ptype_get_idx(0);
2466 nxt = pt->list.next;
2467 if (pt->type == htons(ETH_P_ALL)) {
2468 if (nxt != &ptype_all)
2471 nxt = ptype_base[0].next;
2473 hash = ntohs(pt->type) & 15;
2475 while (nxt == &ptype_base[hash]) {
2478 nxt = ptype_base[hash].next;
2481 return list_entry(nxt, struct packet_type, list);
2484 static void ptype_seq_stop(struct seq_file *seq, void *v)
2489 static void ptype_seq_decode(struct seq_file *seq, void *sym)
2491 #ifdef CONFIG_KALLSYMS
2492 unsigned long offset = 0, symsize;
2493 const char *symname;
2497 symname = kallsyms_lookup((unsigned long)sym, &symsize, &offset,
2504 modname = delim = "";
2505 seq_printf(seq, "%s%s%s%s+0x%lx", delim, modname, delim,
2511 seq_printf(seq, "[%p]", sym);
2514 static int ptype_seq_show(struct seq_file *seq, void *v)
2516 struct packet_type *pt = v;
2518 if (v == SEQ_START_TOKEN)
2519 seq_puts(seq, "Type Device Function\n");
2521 if (pt->type == htons(ETH_P_ALL))
2522 seq_puts(seq, "ALL ");
2524 seq_printf(seq, "%04x", ntohs(pt->type));
2526 seq_printf(seq, " %-8s ",
2527 pt->dev ? pt->dev->name : "");
2528 ptype_seq_decode(seq, pt->func);
2529 seq_putc(seq, '\n');
2535 static const struct seq_operations ptype_seq_ops = {
2536 .start = ptype_seq_start,
2537 .next = ptype_seq_next,
2538 .stop = ptype_seq_stop,
2539 .show = ptype_seq_show,
2542 static int ptype_seq_open(struct inode *inode, struct file *file)
2544 return seq_open(file, &ptype_seq_ops);
2547 static const struct file_operations ptype_seq_fops = {
2548 .owner = THIS_MODULE,
2549 .open = ptype_seq_open,
2551 .llseek = seq_lseek,
2552 .release = seq_release,
2556 static int __init dev_proc_init(void)
2560 if (!proc_net_fops_create(&init_net, "dev", S_IRUGO, &dev_seq_fops))
2562 if (!proc_net_fops_create(&init_net, "softnet_stat", S_IRUGO, &softnet_seq_fops))
2564 if (!proc_net_fops_create(&init_net, "ptype", S_IRUGO, &ptype_seq_fops))
2567 if (wext_proc_init())
2573 proc_net_remove(&init_net, "ptype");
2575 proc_net_remove(&init_net, "softnet_stat");
2577 proc_net_remove(&init_net, "dev");
2581 #define dev_proc_init() 0
2582 #endif /* CONFIG_PROC_FS */
2586 * netdev_set_master - set up master/slave pair
2587 * @slave: slave device
2588 * @master: new master device
2590 * Changes the master device of the slave. Pass %NULL to break the
2591 * bonding. The caller must hold the RTNL semaphore. On a failure
2592 * a negative errno code is returned. On success the reference counts
2593 * are adjusted, %RTM_NEWLINK is sent to the routing socket and the
2594 * function returns zero.
2596 int netdev_set_master(struct net_device *slave, struct net_device *master)
2598 struct net_device *old = slave->master;
2608 slave->master = master;
2616 slave->flags |= IFF_SLAVE;
2618 slave->flags &= ~IFF_SLAVE;
2620 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
2624 static void __dev_set_promiscuity(struct net_device *dev, int inc)
2626 unsigned short old_flags = dev->flags;
2630 if ((dev->promiscuity += inc) == 0)
2631 dev->flags &= ~IFF_PROMISC;
2633 dev->flags |= IFF_PROMISC;
2634 if (dev->flags != old_flags) {
2635 printk(KERN_INFO "device %s %s promiscuous mode\n",
2636 dev->name, (dev->flags & IFF_PROMISC) ? "entered" :
2638 audit_log(current->audit_context, GFP_ATOMIC,
2639 AUDIT_ANOM_PROMISCUOUS,
2640 "dev=%s prom=%d old_prom=%d auid=%u",
2641 dev->name, (dev->flags & IFF_PROMISC),
2642 (old_flags & IFF_PROMISC),
2643 audit_get_loginuid(current->audit_context));
2645 if (dev->change_rx_flags)
2646 dev->change_rx_flags(dev, IFF_PROMISC);
2651 * dev_set_promiscuity - update promiscuity count on a device
2655 * Add or remove promiscuity from a device. While the count in the device
2656 * remains above zero the interface remains promiscuous. Once it hits zero
2657 * the device reverts back to normal filtering operation. A negative inc
2658 * value is used to drop promiscuity on the device.
2660 void dev_set_promiscuity(struct net_device *dev, int inc)
2662 unsigned short old_flags = dev->flags;
2664 __dev_set_promiscuity(dev, inc);
2665 if (dev->flags != old_flags)
2666 dev_set_rx_mode(dev);
2670 * dev_set_allmulti - update allmulti count on a device
2674 * Add or remove reception of all multicast frames to a device. While the
2675 * count in the device remains above zero the interface remains listening
2676 * to all interfaces. Once it hits zero the device reverts back to normal
2677 * filtering operation. A negative @inc value is used to drop the counter
2678 * when releasing a resource needing all multicasts.
2681 void dev_set_allmulti(struct net_device *dev, int inc)
2683 unsigned short old_flags = dev->flags;
2687 dev->flags |= IFF_ALLMULTI;
2688 if ((dev->allmulti += inc) == 0)
2689 dev->flags &= ~IFF_ALLMULTI;
2690 if (dev->flags ^ old_flags) {
2691 if (dev->change_rx_flags)
2692 dev->change_rx_flags(dev, IFF_ALLMULTI);
2693 dev_set_rx_mode(dev);
2698 * Upload unicast and multicast address lists to device and
2699 * configure RX filtering. When the device doesn't support unicast
2700 * filtering it is put in promiscous mode while unicast addresses
2703 void __dev_set_rx_mode(struct net_device *dev)
2705 /* dev_open will call this function so the list will stay sane. */
2706 if (!(dev->flags&IFF_UP))
2709 if (!netif_device_present(dev))
2712 if (dev->set_rx_mode)
2713 dev->set_rx_mode(dev);
2715 /* Unicast addresses changes may only happen under the rtnl,
2716 * therefore calling __dev_set_promiscuity here is safe.
2718 if (dev->uc_count > 0 && !dev->uc_promisc) {
2719 __dev_set_promiscuity(dev, 1);
2720 dev->uc_promisc = 1;
2721 } else if (dev->uc_count == 0 && dev->uc_promisc) {
2722 __dev_set_promiscuity(dev, -1);
2723 dev->uc_promisc = 0;
2726 if (dev->set_multicast_list)
2727 dev->set_multicast_list(dev);
2731 void dev_set_rx_mode(struct net_device *dev)
2733 netif_tx_lock_bh(dev);
2734 __dev_set_rx_mode(dev);
2735 netif_tx_unlock_bh(dev);
2738 int __dev_addr_delete(struct dev_addr_list **list, int *count,
2739 void *addr, int alen, int glbl)
2741 struct dev_addr_list *da;
2743 for (; (da = *list) != NULL; list = &da->next) {
2744 if (memcmp(da->da_addr, addr, da->da_addrlen) == 0 &&
2745 alen == da->da_addrlen) {
2747 int old_glbl = da->da_gusers;
2764 int __dev_addr_add(struct dev_addr_list **list, int *count,
2765 void *addr, int alen, int glbl)
2767 struct dev_addr_list *da;
2769 for (da = *list; da != NULL; da = da->next) {
2770 if (memcmp(da->da_addr, addr, da->da_addrlen) == 0 &&
2771 da->da_addrlen == alen) {
2773 int old_glbl = da->da_gusers;
2783 da = kmalloc(sizeof(*da), GFP_ATOMIC);
2786 memcpy(da->da_addr, addr, alen);
2787 da->da_addrlen = alen;
2789 da->da_gusers = glbl ? 1 : 0;
2797 * dev_unicast_delete - Release secondary unicast address.
2799 * @addr: address to delete
2800 * @alen: length of @addr
2802 * Release reference to a secondary unicast address and remove it
2803 * from the device if the reference count drops to zero.
2805 * The caller must hold the rtnl_mutex.
2807 int dev_unicast_delete(struct net_device *dev, void *addr, int alen)
2813 netif_tx_lock_bh(dev);
2814 err = __dev_addr_delete(&dev->uc_list, &dev->uc_count, addr, alen, 0);
2816 __dev_set_rx_mode(dev);
2817 netif_tx_unlock_bh(dev);
2820 EXPORT_SYMBOL(dev_unicast_delete);
2823 * dev_unicast_add - add a secondary unicast address
2825 * @addr: address to delete
2826 * @alen: length of @addr
2828 * Add a secondary unicast address to the device or increase
2829 * the reference count if it already exists.
2831 * The caller must hold the rtnl_mutex.
2833 int dev_unicast_add(struct net_device *dev, void *addr, int alen)
2839 netif_tx_lock_bh(dev);
2840 err = __dev_addr_add(&dev->uc_list, &dev->uc_count, addr, alen, 0);
2842 __dev_set_rx_mode(dev);
2843 netif_tx_unlock_bh(dev);
2846 EXPORT_SYMBOL(dev_unicast_add);
2848 static void __dev_addr_discard(struct dev_addr_list **list)
2850 struct dev_addr_list *tmp;
2852 while (*list != NULL) {
2855 if (tmp->da_users > tmp->da_gusers)
2856 printk("__dev_addr_discard: address leakage! "
2857 "da_users=%d\n", tmp->da_users);
2862 static void dev_addr_discard(struct net_device *dev)
2864 netif_tx_lock_bh(dev);
2866 __dev_addr_discard(&dev->uc_list);
2869 __dev_addr_discard(&dev->mc_list);
2872 netif_tx_unlock_bh(dev);
2875 unsigned dev_get_flags(const struct net_device *dev)
2879 flags = (dev->flags & ~(IFF_PROMISC |
2884 (dev->gflags & (IFF_PROMISC |
2887 if (netif_running(dev)) {
2888 if (netif_oper_up(dev))
2889 flags |= IFF_RUNNING;
2890 if (netif_carrier_ok(dev))
2891 flags |= IFF_LOWER_UP;
2892 if (netif_dormant(dev))
2893 flags |= IFF_DORMANT;
2899 int dev_change_flags(struct net_device *dev, unsigned flags)
2902 int old_flags = dev->flags;
2907 * Set the flags on our device.
2910 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
2911 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
2913 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
2917 * Load in the correct multicast list now the flags have changed.
2920 if (dev->change_rx_flags && (dev->flags ^ flags) & IFF_MULTICAST)
2921 dev->change_rx_flags(dev, IFF_MULTICAST);
2923 dev_set_rx_mode(dev);
2926 * Have we downed the interface. We handle IFF_UP ourselves
2927 * according to user attempts to set it, rather than blindly
2932 if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */
2933 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2936 dev_set_rx_mode(dev);
2939 if (dev->flags & IFF_UP &&
2940 ((old_flags ^ dev->flags) &~ (IFF_UP | IFF_PROMISC | IFF_ALLMULTI |
2942 raw_notifier_call_chain(&netdev_chain,
2943 NETDEV_CHANGE, dev);
2945 if ((flags ^ dev->gflags) & IFF_PROMISC) {
2946 int inc = (flags & IFF_PROMISC) ? +1 : -1;
2947 dev->gflags ^= IFF_PROMISC;
2948 dev_set_promiscuity(dev, inc);
2951 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
2952 is important. Some (broken) drivers set IFF_PROMISC, when
2953 IFF_ALLMULTI is requested not asking us and not reporting.
2955 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
2956 int inc = (flags & IFF_ALLMULTI) ? +1 : -1;
2957 dev->gflags ^= IFF_ALLMULTI;
2958 dev_set_allmulti(dev, inc);
2961 /* Exclude state transition flags, already notified */
2962 changes = (old_flags ^ dev->flags) & ~(IFF_UP | IFF_RUNNING);
2964 rtmsg_ifinfo(RTM_NEWLINK, dev, changes);
2969 int dev_set_mtu(struct net_device *dev, int new_mtu)
2973 if (new_mtu == dev->mtu)
2976 /* MTU must be positive. */
2980 if (!netif_device_present(dev))
2984 if (dev->change_mtu)
2985 err = dev->change_mtu(dev, new_mtu);
2988 if (!err && dev->flags & IFF_UP)
2989 raw_notifier_call_chain(&netdev_chain,
2990 NETDEV_CHANGEMTU, dev);
2994 int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa)
2998 if (!dev->set_mac_address)
3000 if (sa->sa_family != dev->type)
3002 if (!netif_device_present(dev))
3004 err = dev->set_mac_address(dev, sa);
3006 raw_notifier_call_chain(&netdev_chain,
3007 NETDEV_CHANGEADDR, dev);
3012 * Perform the SIOCxIFxxx calls.
3014 static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
3017 struct net_device *dev = __dev_get_by_name(ifr->ifr_name);
3023 case SIOCGIFFLAGS: /* Get interface flags */
3024 ifr->ifr_flags = dev_get_flags(dev);
3027 case SIOCSIFFLAGS: /* Set interface flags */
3028 return dev_change_flags(dev, ifr->ifr_flags);
3030 case SIOCGIFMETRIC: /* Get the metric on the interface
3031 (currently unused) */
3032 ifr->ifr_metric = 0;
3035 case SIOCSIFMETRIC: /* Set the metric on the interface
3036 (currently unused) */
3039 case SIOCGIFMTU: /* Get the MTU of a device */
3040 ifr->ifr_mtu = dev->mtu;
3043 case SIOCSIFMTU: /* Set the MTU of a device */
3044 return dev_set_mtu(dev, ifr->ifr_mtu);
3048 memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data);
3050 memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr,
3051 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
3052 ifr->ifr_hwaddr.sa_family = dev->type;
3056 return dev_set_mac_address(dev, &ifr->ifr_hwaddr);
3058 case SIOCSIFHWBROADCAST:
3059 if (ifr->ifr_hwaddr.sa_family != dev->type)
3061 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data,
3062 min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len));
3063 raw_notifier_call_chain(&netdev_chain,
3064 NETDEV_CHANGEADDR, dev);
3068 ifr->ifr_map.mem_start = dev->mem_start;
3069 ifr->ifr_map.mem_end = dev->mem_end;
3070 ifr->ifr_map.base_addr = dev->base_addr;
3071 ifr->ifr_map.irq = dev->irq;
3072 ifr->ifr_map.dma = dev->dma;
3073 ifr->ifr_map.port = dev->if_port;
3077 if (dev->set_config) {
3078 if (!netif_device_present(dev))
3080 return dev->set_config(dev, &ifr->ifr_map);
3085 if (!dev->set_multicast_list ||
3086 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
3088 if (!netif_device_present(dev))
3090 return dev_mc_add(dev, ifr->ifr_hwaddr.sa_data,
3094 if (!dev->set_multicast_list ||
3095 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
3097 if (!netif_device_present(dev))
3099 return dev_mc_delete(dev, ifr->ifr_hwaddr.sa_data,
3103 ifr->ifr_ifindex = dev->ifindex;
3107 ifr->ifr_qlen = dev->tx_queue_len;
3111 if (ifr->ifr_qlen < 0)
3113 dev->tx_queue_len = ifr->ifr_qlen;
3117 ifr->ifr_newname[IFNAMSIZ-1] = '\0';
3118 return dev_change_name(dev, ifr->ifr_newname);
3121 * Unknown or private ioctl
3125 if ((cmd >= SIOCDEVPRIVATE &&
3126 cmd <= SIOCDEVPRIVATE + 15) ||
3127 cmd == SIOCBONDENSLAVE ||
3128 cmd == SIOCBONDRELEASE ||
3129 cmd == SIOCBONDSETHWADDR ||
3130 cmd == SIOCBONDSLAVEINFOQUERY ||
3131 cmd == SIOCBONDINFOQUERY ||
3132 cmd == SIOCBONDCHANGEACTIVE ||
3133 cmd == SIOCGMIIPHY ||
3134 cmd == SIOCGMIIREG ||
3135 cmd == SIOCSMIIREG ||
3136 cmd == SIOCBRADDIF ||
3137 cmd == SIOCBRDELIF ||
3138 cmd == SIOCWANDEV) {
3140 if (dev->do_ioctl) {
3141 if (netif_device_present(dev))
3142 err = dev->do_ioctl(dev, ifr,
3155 * This function handles all "interface"-type I/O control requests. The actual
3156 * 'doing' part of this is dev_ifsioc above.
3160 * dev_ioctl - network device ioctl
3161 * @cmd: command to issue
3162 * @arg: pointer to a struct ifreq in user space
3164 * Issue ioctl functions to devices. This is normally called by the
3165 * user space syscall interfaces but can sometimes be useful for
3166 * other purposes. The return value is the return from the syscall if
3167 * positive or a negative errno code on error.
3170 int dev_ioctl(unsigned int cmd, void __user *arg)
3176 /* One special case: SIOCGIFCONF takes ifconf argument
3177 and requires shared lock, because it sleeps writing
3181 if (cmd == SIOCGIFCONF) {
3183 ret = dev_ifconf((char __user *) arg);
3187 if (cmd == SIOCGIFNAME)
3188 return dev_ifname((struct ifreq __user *)arg);
3190 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
3193 ifr.ifr_name[IFNAMSIZ-1] = 0;
3195 colon = strchr(ifr.ifr_name, ':');
3200 * See which interface the caller is talking about.
3205 * These ioctl calls:
3206 * - can be done by all.
3207 * - atomic and do not require locking.
3218 dev_load(ifr.ifr_name);
3219 read_lock(&dev_base_lock);
3220 ret = dev_ifsioc(&ifr, cmd);
3221 read_unlock(&dev_base_lock);
3225 if (copy_to_user(arg, &ifr,
3226 sizeof(struct ifreq)))
3232 dev_load(ifr.ifr_name);
3234 ret = dev_ethtool(&ifr);
3239 if (copy_to_user(arg, &ifr,
3240 sizeof(struct ifreq)))
3246 * These ioctl calls:
3247 * - require superuser power.
3248 * - require strict serialization.
3254 if (!capable(CAP_NET_ADMIN))
3256 dev_load(ifr.ifr_name);
3258 ret = dev_ifsioc(&ifr, cmd);
3263 if (copy_to_user(arg, &ifr,
3264 sizeof(struct ifreq)))
3270 * These ioctl calls:
3271 * - require superuser power.
3272 * - require strict serialization.
3273 * - do not return a value
3283 case SIOCSIFHWBROADCAST:
3286 case SIOCBONDENSLAVE:
3287 case SIOCBONDRELEASE:
3288 case SIOCBONDSETHWADDR:
3289 case SIOCBONDCHANGEACTIVE:
3292 if (!capable(CAP_NET_ADMIN))
3295 case SIOCBONDSLAVEINFOQUERY:
3296 case SIOCBONDINFOQUERY:
3297 dev_load(ifr.ifr_name);
3299 ret = dev_ifsioc(&ifr, cmd);
3304 /* Get the per device memory space. We can add this but
3305 * currently do not support it */
3307 /* Set the per device memory buffer space.
3308 * Not applicable in our case */
3313 * Unknown or private ioctl.
3316 if (cmd == SIOCWANDEV ||
3317 (cmd >= SIOCDEVPRIVATE &&
3318 cmd <= SIOCDEVPRIVATE + 15)) {
3319 dev_load(ifr.ifr_name);
3321 ret = dev_ifsioc(&ifr, cmd);
3323 if (!ret && copy_to_user(arg, &ifr,
3324 sizeof(struct ifreq)))
3328 /* Take care of Wireless Extensions */
3329 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST)
3330 return wext_handle_ioctl(&ifr, cmd, arg);
3337 * dev_new_index - allocate an ifindex
3339 * Returns a suitable unique value for a new device interface
3340 * number. The caller must hold the rtnl semaphore or the
3341 * dev_base_lock to be sure it remains unique.
3343 static int dev_new_index(void)
3349 if (!__dev_get_by_index(ifindex))
3354 static int dev_boot_phase = 1;
3356 /* Delayed registration/unregisteration */
3357 static DEFINE_SPINLOCK(net_todo_list_lock);
3358 static struct list_head net_todo_list = LIST_HEAD_INIT(net_todo_list);
3360 static void net_set_todo(struct net_device *dev)
3362 spin_lock(&net_todo_list_lock);
3363 list_add_tail(&dev->todo_list, &net_todo_list);
3364 spin_unlock(&net_todo_list_lock);
3368 * register_netdevice - register a network device
3369 * @dev: device to register
3371 * Take a completed network device structure and add it to the kernel
3372 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3373 * chain. 0 is returned on success. A negative errno code is returned
3374 * on a failure to set up the device, or if the name is a duplicate.
3376 * Callers must hold the rtnl semaphore. You may want
3377 * register_netdev() instead of this.
3380 * The locking appears insufficient to guarantee two parallel registers
3381 * will not get the same name.
3384 int register_netdevice(struct net_device *dev)
3386 struct hlist_head *head;
3387 struct hlist_node *p;
3390 BUG_ON(dev_boot_phase);
3395 /* When net_device's are persistent, this will be fatal. */
3396 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
3398 spin_lock_init(&dev->queue_lock);
3399 spin_lock_init(&dev->_xmit_lock);
3400 netdev_set_lockdep_class(&dev->_xmit_lock, dev->type);
3401 dev->xmit_lock_owner = -1;
3402 spin_lock_init(&dev->ingress_lock);
3406 /* Init, if this function is available */
3408 ret = dev->init(dev);
3416 if (!dev_valid_name(dev->name)) {
3421 dev->ifindex = dev_new_index();
3422 if (dev->iflink == -1)
3423 dev->iflink = dev->ifindex;
3425 /* Check for existence of name */
3426 head = dev_name_hash(dev->name);
3427 hlist_for_each(p, head) {
3428 struct net_device *d
3429 = hlist_entry(p, struct net_device, name_hlist);
3430 if (!strncmp(d->name, dev->name, IFNAMSIZ)) {
3436 /* Fix illegal checksum combinations */
3437 if ((dev->features & NETIF_F_HW_CSUM) &&
3438 (dev->features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3439 printk(KERN_NOTICE "%s: mixed HW and IP checksum settings.\n",
3441 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
3444 if ((dev->features & NETIF_F_NO_CSUM) &&
3445 (dev->features & (NETIF_F_HW_CSUM|NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
3446 printk(KERN_NOTICE "%s: mixed no checksumming and other settings.\n",
3448 dev->features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM|NETIF_F_HW_CSUM);
3452 /* Fix illegal SG+CSUM combinations. */
3453 if ((dev->features & NETIF_F_SG) &&
3454 !(dev->features & NETIF_F_ALL_CSUM)) {
3455 printk(KERN_NOTICE "%s: Dropping NETIF_F_SG since no checksum feature.\n",
3457 dev->features &= ~NETIF_F_SG;
3460 /* TSO requires that SG is present as well. */
3461 if ((dev->features & NETIF_F_TSO) &&
3462 !(dev->features & NETIF_F_SG)) {
3463 printk(KERN_NOTICE "%s: Dropping NETIF_F_TSO since no SG feature.\n",
3465 dev->features &= ~NETIF_F_TSO;
3467 if (dev->features & NETIF_F_UFO) {
3468 if (!(dev->features & NETIF_F_HW_CSUM)) {
3469 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3470 "NETIF_F_HW_CSUM feature.\n",
3472 dev->features &= ~NETIF_F_UFO;
3474 if (!(dev->features & NETIF_F_SG)) {
3475 printk(KERN_ERR "%s: Dropping NETIF_F_UFO since no "
3476 "NETIF_F_SG feature.\n",
3478 dev->features &= ~NETIF_F_UFO;
3483 * nil rebuild_header routine,
3484 * that should be never called and used as just bug trap.
3487 if (!dev->rebuild_header)
3488 dev->rebuild_header = default_rebuild_header;
3490 ret = netdev_register_sysfs(dev);
3493 dev->reg_state = NETREG_REGISTERED;
3496 * Default initial state at registry is that the
3497 * device is present.
3500 set_bit(__LINK_STATE_PRESENT, &dev->state);
3502 dev_init_scheduler(dev);
3503 write_lock_bh(&dev_base_lock);
3504 list_add_tail(&dev->dev_list, &dev_base_head);
3505 hlist_add_head(&dev->name_hlist, head);
3506 hlist_add_head(&dev->index_hlist, dev_index_hash(dev->ifindex));
3508 write_unlock_bh(&dev_base_lock);
3510 /* Notify protocols, that a new device appeared. */
3511 ret = raw_notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
3512 ret = notifier_to_errno(ret);
3514 unregister_netdevice(dev);
3526 * register_netdev - register a network device
3527 * @dev: device to register
3529 * Take a completed network device structure and add it to the kernel
3530 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
3531 * chain. 0 is returned on success. A negative errno code is returned
3532 * on a failure to set up the device, or if the name is a duplicate.
3534 * This is a wrapper around register_netdevice that takes the rtnl semaphore
3535 * and expands the device name if you passed a format string to
3538 int register_netdev(struct net_device *dev)
3545 * If the name is a format string the caller wants us to do a
3548 if (strchr(dev->name, '%')) {
3549 err = dev_alloc_name(dev, dev->name);
3554 err = register_netdevice(dev);
3559 EXPORT_SYMBOL(register_netdev);
3562 * netdev_wait_allrefs - wait until all references are gone.
3564 * This is called when unregistering network devices.
3566 * Any protocol or device that holds a reference should register
3567 * for netdevice notification, and cleanup and put back the
3568 * reference if they receive an UNREGISTER event.
3569 * We can get stuck here if buggy protocols don't correctly
3572 static void netdev_wait_allrefs(struct net_device *dev)
3574 unsigned long rebroadcast_time, warning_time;
3576 rebroadcast_time = warning_time = jiffies;
3577 while (atomic_read(&dev->refcnt) != 0) {
3578 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
3581 /* Rebroadcast unregister notification */
3582 raw_notifier_call_chain(&netdev_chain,
3583 NETDEV_UNREGISTER, dev);
3585 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
3587 /* We must not have linkwatch events
3588 * pending on unregister. If this
3589 * happens, we simply run the queue
3590 * unscheduled, resulting in a noop
3593 linkwatch_run_queue();
3598 rebroadcast_time = jiffies;
3603 if (time_after(jiffies, warning_time + 10 * HZ)) {
3604 printk(KERN_EMERG "unregister_netdevice: "
3605 "waiting for %s to become free. Usage "
3607 dev->name, atomic_read(&dev->refcnt));
3608 warning_time = jiffies;
3617 * register_netdevice(x1);
3618 * register_netdevice(x2);
3620 * unregister_netdevice(y1);
3621 * unregister_netdevice(y2);
3627 * We are invoked by rtnl_unlock() after it drops the semaphore.
3628 * This allows us to deal with problems:
3629 * 1) We can delete sysfs objects which invoke hotplug
3630 * without deadlocking with linkwatch via keventd.
3631 * 2) Since we run with the RTNL semaphore not held, we can sleep
3632 * safely in order to wait for the netdev refcnt to drop to zero.
3634 static DEFINE_MUTEX(net_todo_run_mutex);
3635 void netdev_run_todo(void)
3637 struct list_head list;
3639 /* Need to guard against multiple cpu's getting out of order. */
3640 mutex_lock(&net_todo_run_mutex);
3642 /* Not safe to do outside the semaphore. We must not return
3643 * until all unregister events invoked by the local processor
3644 * have been completed (either by this todo run, or one on
3647 if (list_empty(&net_todo_list))
3650 /* Snapshot list, allow later requests */
3651 spin_lock(&net_todo_list_lock);
3652 list_replace_init(&net_todo_list, &list);
3653 spin_unlock(&net_todo_list_lock);
3655 while (!list_empty(&list)) {
3656 struct net_device *dev
3657 = list_entry(list.next, struct net_device, todo_list);
3658 list_del(&dev->todo_list);
3660 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
3661 printk(KERN_ERR "network todo '%s' but state %d\n",
3662 dev->name, dev->reg_state);
3667 dev->reg_state = NETREG_UNREGISTERED;
3669 netdev_wait_allrefs(dev);
3672 BUG_ON(atomic_read(&dev->refcnt));
3673 BUG_TRAP(!dev->ip_ptr);
3674 BUG_TRAP(!dev->ip6_ptr);
3675 BUG_TRAP(!dev->dn_ptr);
3677 if (dev->destructor)
3678 dev->destructor(dev);
3680 /* Free network device */
3681 kobject_put(&dev->dev.kobj);
3685 mutex_unlock(&net_todo_run_mutex);
3688 static struct net_device_stats *internal_stats(struct net_device *dev)
3694 * alloc_netdev_mq - allocate network device
3695 * @sizeof_priv: size of private data to allocate space for
3696 * @name: device name format string
3697 * @setup: callback to initialize device
3698 * @queue_count: the number of subqueues to allocate
3700 * Allocates a struct net_device with private data area for driver use
3701 * and performs basic initialization. Also allocates subquue structs
3702 * for each queue on the device at the end of the netdevice.
3704 struct net_device *alloc_netdev_mq(int sizeof_priv, const char *name,
3705 void (*setup)(struct net_device *), unsigned int queue_count)
3708 struct net_device *dev;
3711 BUG_ON(strlen(name) >= sizeof(dev->name));
3713 /* ensure 32-byte alignment of both the device and private area */
3714 alloc_size = (sizeof(*dev) + NETDEV_ALIGN_CONST +
3715 (sizeof(struct net_device_subqueue) * (queue_count - 1))) &
3716 ~NETDEV_ALIGN_CONST;
3717 alloc_size += sizeof_priv + NETDEV_ALIGN_CONST;
3719 p = kzalloc(alloc_size, GFP_KERNEL);
3721 printk(KERN_ERR "alloc_netdev: Unable to allocate device.\n");
3725 dev = (struct net_device *)
3726 (((long)p + NETDEV_ALIGN_CONST) & ~NETDEV_ALIGN_CONST);
3727 dev->padded = (char *)dev - (char *)p;
3730 dev->priv = ((char *)dev +
3731 ((sizeof(struct net_device) +
3732 (sizeof(struct net_device_subqueue) *
3733 (queue_count - 1)) + NETDEV_ALIGN_CONST)
3734 & ~NETDEV_ALIGN_CONST));
3737 dev->egress_subqueue_count = queue_count;
3739 dev->get_stats = internal_stats;
3740 netpoll_netdev_init(dev);
3742 strcpy(dev->name, name);
3745 EXPORT_SYMBOL(alloc_netdev_mq);
3748 * free_netdev - free network device
3751 * This function does the last stage of destroying an allocated device
3752 * interface. The reference to the device object is released.
3753 * If this is the last reference then it will be freed.
3755 void free_netdev(struct net_device *dev)
3758 /* Compatibility with error handling in drivers */
3759 if (dev->reg_state == NETREG_UNINITIALIZED) {
3760 kfree((char *)dev - dev->padded);
3764 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
3765 dev->reg_state = NETREG_RELEASED;
3767 /* will free via device release */
3768 put_device(&dev->dev);
3770 kfree((char *)dev - dev->padded);
3774 /* Synchronize with packet receive processing. */
3775 void synchronize_net(void)
3782 * unregister_netdevice - remove device from the kernel
3785 * This function shuts down a device interface and removes it
3786 * from the kernel tables. On success 0 is returned, on a failure
3787 * a negative errno code is returned.
3789 * Callers must hold the rtnl semaphore. You may want
3790 * unregister_netdev() instead of this.
3793 void unregister_netdevice(struct net_device *dev)
3795 BUG_ON(dev_boot_phase);
3798 /* Some devices call without registering for initialization unwind. */
3799 if (dev->reg_state == NETREG_UNINITIALIZED) {
3800 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never "
3801 "was registered\n", dev->name, dev);
3807 BUG_ON(dev->reg_state != NETREG_REGISTERED);
3809 /* If device is running, close it first. */
3810 if (dev->flags & IFF_UP)
3813 /* And unlink it from device chain. */
3814 write_lock_bh(&dev_base_lock);
3815 list_del(&dev->dev_list);
3816 hlist_del(&dev->name_hlist);
3817 hlist_del(&dev->index_hlist);
3818 write_unlock_bh(&dev_base_lock);
3820 dev->reg_state = NETREG_UNREGISTERING;
3824 /* Shutdown queueing discipline. */
3828 /* Notify protocols, that we are about to destroy
3829 this device. They should clean all the things.
3831 raw_notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
3834 * Flush the unicast and multicast chains
3836 dev_addr_discard(dev);
3841 /* Notifier chain MUST detach us from master device. */
3842 BUG_TRAP(!dev->master);
3844 /* Remove entries from sysfs */
3845 netdev_unregister_sysfs(dev);
3847 /* Finish processing unregister after unlock */
3856 * unregister_netdev - remove device from the kernel
3859 * This function shuts down a device interface and removes it
3860 * from the kernel tables. On success 0 is returned, on a failure
3861 * a negative errno code is returned.
3863 * This is just a wrapper for unregister_netdevice that takes
3864 * the rtnl semaphore. In general you want to use this and not
3865 * unregister_netdevice.
3867 void unregister_netdev(struct net_device *dev)
3870 unregister_netdevice(dev);
3874 EXPORT_SYMBOL(unregister_netdev);
3876 static int dev_cpu_callback(struct notifier_block *nfb,
3877 unsigned long action,
3880 struct sk_buff **list_skb;
3881 struct net_device **list_net;
3882 struct sk_buff *skb;
3883 unsigned int cpu, oldcpu = (unsigned long)ocpu;
3884 struct softnet_data *sd, *oldsd;
3886 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
3889 local_irq_disable();
3890 cpu = smp_processor_id();
3891 sd = &per_cpu(softnet_data, cpu);
3892 oldsd = &per_cpu(softnet_data, oldcpu);
3894 /* Find end of our completion_queue. */
3895 list_skb = &sd->completion_queue;
3897 list_skb = &(*list_skb)->next;
3898 /* Append completion queue from offline CPU. */
3899 *list_skb = oldsd->completion_queue;
3900 oldsd->completion_queue = NULL;
3902 /* Find end of our output_queue. */
3903 list_net = &sd->output_queue;
3905 list_net = &(*list_net)->next_sched;
3906 /* Append output queue from offline CPU. */
3907 *list_net = oldsd->output_queue;
3908 oldsd->output_queue = NULL;
3910 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3913 /* Process offline CPU's input_pkt_queue */
3914 while ((skb = __skb_dequeue(&oldsd->input_pkt_queue)))
3920 #ifdef CONFIG_NET_DMA
3922 * net_dma_rebalance - try to maintain one DMA channel per CPU
3923 * @net_dma: DMA client and associated data (lock, channels, channel_mask)
3925 * This is called when the number of channels allocated to the net_dma client
3926 * changes. The net_dma client tries to have one DMA channel per CPU.
3929 static void net_dma_rebalance(struct net_dma *net_dma)
3931 unsigned int cpu, i, n, chan_idx;
3932 struct dma_chan *chan;
3934 if (cpus_empty(net_dma->channel_mask)) {
3935 for_each_online_cpu(cpu)
3936 rcu_assign_pointer(per_cpu(softnet_data, cpu).net_dma, NULL);
3941 cpu = first_cpu(cpu_online_map);
3943 for_each_cpu_mask(chan_idx, net_dma->channel_mask) {
3944 chan = net_dma->channels[chan_idx];
3946 n = ((num_online_cpus() / cpus_weight(net_dma->channel_mask))
3947 + (i < (num_online_cpus() %
3948 cpus_weight(net_dma->channel_mask)) ? 1 : 0));
3951 per_cpu(softnet_data, cpu).net_dma = chan;
3952 cpu = next_cpu(cpu, cpu_online_map);
3960 * netdev_dma_event - event callback for the net_dma_client
3961 * @client: should always be net_dma_client
3962 * @chan: DMA channel for the event
3963 * @state: DMA state to be handled
3965 static enum dma_state_client
3966 netdev_dma_event(struct dma_client *client, struct dma_chan *chan,
3967 enum dma_state state)
3969 int i, found = 0, pos = -1;
3970 struct net_dma *net_dma =
3971 container_of(client, struct net_dma, client);
3972 enum dma_state_client ack = DMA_DUP; /* default: take no action */
3974 spin_lock(&net_dma->lock);
3976 case DMA_RESOURCE_AVAILABLE:
3977 for (i = 0; i < NR_CPUS; i++)
3978 if (net_dma->channels[i] == chan) {
3981 } else if (net_dma->channels[i] == NULL && pos < 0)
3984 if (!found && pos >= 0) {
3986 net_dma->channels[pos] = chan;
3987 cpu_set(pos, net_dma->channel_mask);
3988 net_dma_rebalance(net_dma);
3991 case DMA_RESOURCE_REMOVED:
3992 for (i = 0; i < NR_CPUS; i++)
3993 if (net_dma->channels[i] == chan) {
4001 cpu_clear(pos, net_dma->channel_mask);
4002 net_dma->channels[i] = NULL;
4003 net_dma_rebalance(net_dma);
4009 spin_unlock(&net_dma->lock);
4015 * netdev_dma_regiser - register the networking subsystem as a DMA client
4017 static int __init netdev_dma_register(void)
4019 spin_lock_init(&net_dma.lock);
4020 dma_cap_set(DMA_MEMCPY, net_dma.client.cap_mask);
4021 dma_async_client_register(&net_dma.client);
4022 dma_async_client_chan_request(&net_dma.client);
4027 static int __init netdev_dma_register(void) { return -ENODEV; }
4028 #endif /* CONFIG_NET_DMA */
4031 * netdev_compute_feature - compute conjunction of two feature sets
4032 * @all: first feature set
4033 * @one: second feature set
4035 * Computes a new feature set after adding a device with feature set
4036 * @one to the master device with current feature set @all. Returns
4037 * the new feature set.
4039 int netdev_compute_features(unsigned long all, unsigned long one)
4041 /* if device needs checksumming, downgrade to hw checksumming */
4042 if (all & NETIF_F_NO_CSUM && !(one & NETIF_F_NO_CSUM))
4043 all ^= NETIF_F_NO_CSUM | NETIF_F_HW_CSUM;
4045 /* if device can't do all checksum, downgrade to ipv4/ipv6 */
4046 if (all & NETIF_F_HW_CSUM && !(one & NETIF_F_HW_CSUM))
4047 all ^= NETIF_F_HW_CSUM
4048 | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
4050 if (one & NETIF_F_GSO)
4051 one |= NETIF_F_GSO_SOFTWARE;
4054 /* If even one device supports robust GSO, enable it for all. */
4055 if (one & NETIF_F_GSO_ROBUST)
4056 all |= NETIF_F_GSO_ROBUST;
4058 all &= one | NETIF_F_LLTX;
4060 if (!(all & NETIF_F_ALL_CSUM))
4062 if (!(all & NETIF_F_SG))
4063 all &= ~NETIF_F_GSO_MASK;
4067 EXPORT_SYMBOL(netdev_compute_features);
4070 * Initialize the DEV module. At boot time this walks the device list and
4071 * unhooks any devices that fail to initialise (normally hardware not
4072 * present) and leaves us with a valid list of present and active devices.
4077 * This is called single threaded during boot, so no need
4078 * to take the rtnl semaphore.
4080 static int __init net_dev_init(void)
4082 int i, rc = -ENOMEM;
4084 BUG_ON(!dev_boot_phase);
4086 if (dev_proc_init())
4089 if (netdev_sysfs_init())
4092 INIT_LIST_HEAD(&ptype_all);
4093 for (i = 0; i < 16; i++)
4094 INIT_LIST_HEAD(&ptype_base[i]);
4096 for (i = 0; i < ARRAY_SIZE(dev_name_head); i++)
4097 INIT_HLIST_HEAD(&dev_name_head[i]);
4099 for (i = 0; i < ARRAY_SIZE(dev_index_head); i++)
4100 INIT_HLIST_HEAD(&dev_index_head[i]);
4103 * Initialise the packet receive queues.
4106 for_each_possible_cpu(i) {
4107 struct softnet_data *queue;
4109 queue = &per_cpu(softnet_data, i);
4110 skb_queue_head_init(&queue->input_pkt_queue);
4111 queue->completion_queue = NULL;
4112 INIT_LIST_HEAD(&queue->poll_list);
4114 queue->backlog.poll = process_backlog;
4115 queue->backlog.weight = weight_p;
4118 netdev_dma_register();
4122 open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
4123 open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
4125 hotcpu_notifier(dev_cpu_callback, 0);
4133 subsys_initcall(net_dev_init);
4135 EXPORT_SYMBOL(__dev_get_by_index);
4136 EXPORT_SYMBOL(__dev_get_by_name);
4137 EXPORT_SYMBOL(__dev_remove_pack);
4138 EXPORT_SYMBOL(dev_valid_name);
4139 EXPORT_SYMBOL(dev_add_pack);
4140 EXPORT_SYMBOL(dev_alloc_name);
4141 EXPORT_SYMBOL(dev_close);
4142 EXPORT_SYMBOL(dev_get_by_flags);
4143 EXPORT_SYMBOL(dev_get_by_index);
4144 EXPORT_SYMBOL(dev_get_by_name);
4145 EXPORT_SYMBOL(dev_open);
4146 EXPORT_SYMBOL(dev_queue_xmit);
4147 EXPORT_SYMBOL(dev_remove_pack);
4148 EXPORT_SYMBOL(dev_set_allmulti);
4149 EXPORT_SYMBOL(dev_set_promiscuity);
4150 EXPORT_SYMBOL(dev_change_flags);
4151 EXPORT_SYMBOL(dev_set_mtu);
4152 EXPORT_SYMBOL(dev_set_mac_address);
4153 EXPORT_SYMBOL(free_netdev);
4154 EXPORT_SYMBOL(netdev_boot_setup_check);
4155 EXPORT_SYMBOL(netdev_set_master);
4156 EXPORT_SYMBOL(netdev_state_change);
4157 EXPORT_SYMBOL(netif_receive_skb);
4158 EXPORT_SYMBOL(netif_rx);
4159 EXPORT_SYMBOL(register_gifconf);
4160 EXPORT_SYMBOL(register_netdevice);
4161 EXPORT_SYMBOL(register_netdevice_notifier);
4162 EXPORT_SYMBOL(skb_checksum_help);
4163 EXPORT_SYMBOL(synchronize_net);
4164 EXPORT_SYMBOL(unregister_netdevice);
4165 EXPORT_SYMBOL(unregister_netdevice_notifier);
4166 EXPORT_SYMBOL(net_enable_timestamp);
4167 EXPORT_SYMBOL(net_disable_timestamp);
4168 EXPORT_SYMBOL(dev_get_flags);
4170 #if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
4171 EXPORT_SYMBOL(br_handle_frame_hook);
4172 EXPORT_SYMBOL(br_fdb_get_hook);
4173 EXPORT_SYMBOL(br_fdb_put_hook);
4177 EXPORT_SYMBOL(dev_load);
4180 EXPORT_PER_CPU_SYMBOL(softnet_data);