Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NET3 Protocol independent device support routines. |
| 3 | * |
| 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. |
| 8 | * |
| 9 | * Derived from the non IP parts of dev.c 1.0.19 |
Jesper Juhl | 02c30a8 | 2005-05-05 16:16:16 -0700 | [diff] [blame] | 10 | * Authors: Ross Biro |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
| 12 | * Mark Evans, <evansmp@uhura.aston.ac.uk> |
| 13 | * |
| 14 | * Additional Authors: |
| 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> |
| 21 | * |
| 22 | * Changes: |
| 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 |
| 34 | * drivers |
| 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 |
| 44 | * call a packet. |
| 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 |
| 50 | * changes. |
| 51 | * Rudi Cilibrasi : Pass the right thing to |
| 52 | * set_mac_address() |
| 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 |
| 58 | * 1 device. |
| 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 |
| 66 | * the backlog queue. |
| 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 |
| 73 | */ |
| 74 | |
| 75 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | #include <linux/bitops.h> |
Randy Dunlap | 4fc268d | 2006-01-11 12:17:47 -0800 | [diff] [blame] | 77 | #include <linux/capability.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | #include <linux/cpu.h> |
| 79 | #include <linux/types.h> |
| 80 | #include <linux/kernel.h> |
stephen hemminger | 08e9897 | 2009-11-10 07:20:34 +0000 | [diff] [blame] | 81 | #include <linux/hash.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 82 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #include <linux/sched.h> |
Arjan van de Ven | 4a3e2f7 | 2006-03-20 22:33:17 -0800 | [diff] [blame] | 84 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | #include <linux/string.h> |
| 86 | #include <linux/mm.h> |
| 87 | #include <linux/socket.h> |
| 88 | #include <linux/sockios.h> |
| 89 | #include <linux/errno.h> |
| 90 | #include <linux/interrupt.h> |
| 91 | #include <linux/if_ether.h> |
| 92 | #include <linux/netdevice.h> |
| 93 | #include <linux/etherdevice.h> |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 94 | #include <linux/ethtool.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | #include <linux/notifier.h> |
| 96 | #include <linux/skbuff.h> |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 97 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | #include <net/sock.h> |
| 99 | #include <linux/rtnetlink.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | #include <linux/stat.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | #include <net/dst.h> |
| 102 | #include <net/pkt_sched.h> |
| 103 | #include <net/checksum.h> |
Arnd Bergmann | 4454096 | 2009-11-26 06:07:08 +0000 | [diff] [blame] | 104 | #include <net/xfrm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | #include <linux/highmem.h> |
| 106 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | #include <linux/netpoll.h> |
| 109 | #include <linux/rcupdate.h> |
| 110 | #include <linux/delay.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | #include <net/iw_handler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | #include <asm/current.h> |
Steve Grubb | 5bdb988 | 2005-12-03 08:39:35 -0500 | [diff] [blame] | 113 | #include <linux/audit.h> |
Chris Leech | db21733 | 2006-06-17 21:24:58 -0700 | [diff] [blame] | 114 | #include <linux/dmaengine.h> |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 115 | #include <linux/err.h> |
David S. Miller | c7fa9d1 | 2006-08-15 16:34:13 -0700 | [diff] [blame] | 116 | #include <linux/ctype.h> |
Jarek Poplawski | 723e98b | 2007-05-15 22:46:18 -0700 | [diff] [blame] | 117 | #include <linux/if_arp.h> |
Ben Hutchings | 6de329e | 2008-06-16 17:02:28 -0700 | [diff] [blame] | 118 | #include <linux/if_vlan.h> |
David S. Miller | 8f0f222 | 2008-07-15 03:47:03 -0700 | [diff] [blame] | 119 | #include <linux/ip.h> |
Alexander Duyck | ad55dca | 2008-09-20 22:05:50 -0700 | [diff] [blame] | 120 | #include <net/ip.h> |
David S. Miller | 8f0f222 | 2008-07-15 03:47:03 -0700 | [diff] [blame] | 121 | #include <linux/ipv6.h> |
| 122 | #include <linux/in.h> |
David S. Miller | b6b2fed | 2008-07-21 09:48:06 -0700 | [diff] [blame] | 123 | #include <linux/jhash.h> |
| 124 | #include <linux/random.h> |
David S. Miller | 9cbc1cb | 2009-06-15 03:02:23 -0700 | [diff] [blame] | 125 | #include <trace/events/napi.h> |
Koki Sanagi | cf66ba5 | 2010-08-23 18:45:02 +0900 | [diff] [blame] | 126 | #include <trace/events/net.h> |
Koki Sanagi | 07dc22e | 2010-08-23 18:46:12 +0900 | [diff] [blame] | 127 | #include <trace/events/skb.h> |
FUJITA Tomonori | 5acbbd4 | 2010-03-30 22:35:50 +0000 | [diff] [blame] | 128 | #include <linux/pci.h> |
Stephen Rothwell | caeda9b | 2010-09-16 21:39:16 -0700 | [diff] [blame] | 129 | #include <linux/inetdevice.h> |
Ben Hutchings | c445477 | 2011-01-19 11:03:53 +0000 | [diff] [blame] | 130 | #include <linux/cpu_rmap.h> |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 131 | #include <linux/static_key.h> |
Eliezer Tamir | af12fa6 | 2013-06-10 11:39:41 +0300 | [diff] [blame] | 132 | #include <linux/hashtable.h> |
Eric Dumazet | 60877a3 | 2013-06-20 01:15:51 -0700 | [diff] [blame] | 133 | #include <linux/vmalloc.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Pavel Emelyanov | 342709e | 2007-10-23 21:14:45 -0700 | [diff] [blame] | 135 | #include "net-sysfs.h" |
| 136 | |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 137 | /* Instead of increasing this, you should create a hash table. */ |
| 138 | #define MAX_GRO_SKBS 8 |
| 139 | |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 140 | /* This should be increased if a protocol with a bigger head is added. */ |
| 141 | #define GRO_MAX_HEAD (MAX_HEADER + 128) |
| 142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | static DEFINE_SPINLOCK(ptype_lock); |
Vlad Yasevich | 62532da | 2012-11-15 08:49:10 +0000 | [diff] [blame] | 144 | static DEFINE_SPINLOCK(offload_lock); |
Cong Wang | 900ff8c | 2013-02-18 19:20:33 +0000 | [diff] [blame] | 145 | struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly; |
| 146 | struct list_head ptype_all __read_mostly; /* Taps */ |
Vlad Yasevich | 62532da | 2012-11-15 08:49:10 +0000 | [diff] [blame] | 147 | static struct list_head offload_base __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | /* |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 150 | * The @dev_base_head list is protected by @dev_base_lock and the rtnl |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | * semaphore. |
| 152 | * |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 153 | * Pure readers hold dev_base_lock for reading, or rcu_read_lock() |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | * |
| 155 | * Writers must hold the rtnl semaphore while they loop through the |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 156 | * dev_base_head list, and hold dev_base_lock for writing when they do the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | * actual updates. This allows pure readers to access the list even |
| 158 | * while a writer is preparing to update it. |
| 159 | * |
| 160 | * To put it another way, dev_base_lock is held for writing only to |
| 161 | * protect against pure readers; the rtnl semaphore provides the |
| 162 | * protection against other writers. |
| 163 | * |
| 164 | * See, for example usages, register_netdevice() and |
| 165 | * unregister_netdevice(), which must be called with the rtnl |
| 166 | * semaphore held. |
| 167 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | DEFINE_RWLOCK(dev_base_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | EXPORT_SYMBOL(dev_base_lock); |
| 170 | |
Eliezer Tamir | af12fa6 | 2013-06-10 11:39:41 +0300 | [diff] [blame] | 171 | /* protects napi_hash addition/deletion and napi_gen_id */ |
| 172 | static DEFINE_SPINLOCK(napi_hash_lock); |
| 173 | |
| 174 | static unsigned int napi_gen_id; |
| 175 | static DEFINE_HASHTABLE(napi_hash, 8); |
| 176 | |
Eric Dumazet | 30e6c9f | 2012-12-20 17:25:08 +0000 | [diff] [blame] | 177 | seqcount_t devnet_rename_seq; |
Brian Haley | c91f6df | 2012-11-26 05:21:08 +0000 | [diff] [blame] | 178 | |
Thomas Graf | 4e985ad | 2011-06-21 03:11:20 +0000 | [diff] [blame] | 179 | static inline void dev_base_seq_inc(struct net *net) |
| 180 | { |
| 181 | while (++net->dev_base_seq == 0); |
| 182 | } |
| 183 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 184 | static inline struct hlist_head *dev_name_hash(struct net *net, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 186 | unsigned int hash = full_name_hash(name, strnlen(name, IFNAMSIZ)); |
| 187 | |
stephen hemminger | 08e9897 | 2009-11-10 07:20:34 +0000 | [diff] [blame] | 188 | return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 191 | static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
Eric Dumazet | 7c28bd0 | 2009-10-24 06:13:17 -0700 | [diff] [blame] | 193 | return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 196 | static inline void rps_lock(struct softnet_data *sd) |
Changli Gao | 152102c | 2010-03-30 20:16:22 +0000 | [diff] [blame] | 197 | { |
| 198 | #ifdef CONFIG_RPS |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 199 | spin_lock(&sd->input_pkt_queue.lock); |
Changli Gao | 152102c | 2010-03-30 20:16:22 +0000 | [diff] [blame] | 200 | #endif |
| 201 | } |
| 202 | |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 203 | static inline void rps_unlock(struct softnet_data *sd) |
Changli Gao | 152102c | 2010-03-30 20:16:22 +0000 | [diff] [blame] | 204 | { |
| 205 | #ifdef CONFIG_RPS |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 206 | spin_unlock(&sd->input_pkt_queue.lock); |
Changli Gao | 152102c | 2010-03-30 20:16:22 +0000 | [diff] [blame] | 207 | #endif |
| 208 | } |
| 209 | |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 210 | /* Device list insertion */ |
dingtianhong | 53759be | 2013-04-17 22:17:50 +0000 | [diff] [blame] | 211 | static void list_netdevice(struct net_device *dev) |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 212 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 213 | struct net *net = dev_net(dev); |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 214 | |
| 215 | ASSERT_RTNL(); |
| 216 | |
| 217 | write_lock_bh(&dev_base_lock); |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 218 | list_add_tail_rcu(&dev->dev_list, &net->dev_base_head); |
Eric Dumazet | 72c9528 | 2009-10-30 07:11:27 +0000 | [diff] [blame] | 219 | hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name)); |
Eric Dumazet | fb699dfd | 2009-10-19 19:18:49 +0000 | [diff] [blame] | 220 | hlist_add_head_rcu(&dev->index_hlist, |
| 221 | dev_index_hash(net, dev->ifindex)); |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 222 | write_unlock_bh(&dev_base_lock); |
Thomas Graf | 4e985ad | 2011-06-21 03:11:20 +0000 | [diff] [blame] | 223 | |
| 224 | dev_base_seq_inc(net); |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 225 | } |
| 226 | |
Eric Dumazet | fb699dfd | 2009-10-19 19:18:49 +0000 | [diff] [blame] | 227 | /* Device list removal |
| 228 | * caller must respect a RCU grace period before freeing/reusing dev |
| 229 | */ |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 230 | static void unlist_netdevice(struct net_device *dev) |
| 231 | { |
| 232 | ASSERT_RTNL(); |
| 233 | |
| 234 | /* Unlink dev from the device chain */ |
| 235 | write_lock_bh(&dev_base_lock); |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 236 | list_del_rcu(&dev->dev_list); |
Eric Dumazet | 72c9528 | 2009-10-30 07:11:27 +0000 | [diff] [blame] | 237 | hlist_del_rcu(&dev->name_hlist); |
Eric Dumazet | fb699dfd | 2009-10-19 19:18:49 +0000 | [diff] [blame] | 238 | hlist_del_rcu(&dev->index_hlist); |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 239 | write_unlock_bh(&dev_base_lock); |
Thomas Graf | 4e985ad | 2011-06-21 03:11:20 +0000 | [diff] [blame] | 240 | |
| 241 | dev_base_seq_inc(dev_net(dev)); |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 242 | } |
| 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | /* |
| 245 | * Our notifier list |
| 246 | */ |
| 247 | |
Alan Stern | f07d5b9 | 2006-05-09 15:23:03 -0700 | [diff] [blame] | 248 | static RAW_NOTIFIER_HEAD(netdev_chain); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | |
| 250 | /* |
| 251 | * Device drivers call our routines to queue packets here. We empty the |
| 252 | * queue in the local softnet handler. |
| 253 | */ |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 254 | |
Eric Dumazet | 9958da0 | 2010-04-17 04:17:02 +0000 | [diff] [blame] | 255 | DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data); |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 256 | EXPORT_PER_CPU_SYMBOL(softnet_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 258 | #ifdef CONFIG_LOCKDEP |
Jarek Poplawski | 723e98b | 2007-05-15 22:46:18 -0700 | [diff] [blame] | 259 | /* |
David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 260 | * register_netdevice() inits txq->_xmit_lock and sets lockdep class |
Jarek Poplawski | 723e98b | 2007-05-15 22:46:18 -0700 | [diff] [blame] | 261 | * according to dev->type |
| 262 | */ |
| 263 | static const unsigned short netdev_lock_type[] = |
| 264 | {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25, |
| 265 | ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET, |
| 266 | ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM, |
| 267 | ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP, |
| 268 | ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD, |
| 269 | ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25, |
| 270 | ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP, |
| 271 | ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD, |
| 272 | ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI, |
| 273 | ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE, |
| 274 | ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET, |
| 275 | ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL, |
Paul Gortmaker | 211ed86 | 2012-05-10 17:14:35 -0400 | [diff] [blame] | 276 | ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM, |
| 277 | ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE, |
| 278 | ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE}; |
Jarek Poplawski | 723e98b | 2007-05-15 22:46:18 -0700 | [diff] [blame] | 279 | |
Jan Engelhardt | 36cbd3d | 2009-08-05 10:42:58 -0700 | [diff] [blame] | 280 | static const char *const netdev_lock_name[] = |
Jarek Poplawski | 723e98b | 2007-05-15 22:46:18 -0700 | [diff] [blame] | 281 | {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25", |
| 282 | "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET", |
| 283 | "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM", |
| 284 | "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP", |
| 285 | "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD", |
| 286 | "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25", |
| 287 | "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP", |
| 288 | "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD", |
| 289 | "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI", |
| 290 | "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE", |
| 291 | "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET", |
| 292 | "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL", |
Paul Gortmaker | 211ed86 | 2012-05-10 17:14:35 -0400 | [diff] [blame] | 293 | "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM", |
| 294 | "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE", |
| 295 | "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"}; |
Jarek Poplawski | 723e98b | 2007-05-15 22:46:18 -0700 | [diff] [blame] | 296 | |
| 297 | static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)]; |
David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 298 | static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)]; |
Jarek Poplawski | 723e98b | 2007-05-15 22:46:18 -0700 | [diff] [blame] | 299 | |
| 300 | static inline unsigned short netdev_lock_pos(unsigned short dev_type) |
| 301 | { |
| 302 | int i; |
| 303 | |
| 304 | for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++) |
| 305 | if (netdev_lock_type[i] == dev_type) |
| 306 | return i; |
| 307 | /* the last key is used by default */ |
| 308 | return ARRAY_SIZE(netdev_lock_type) - 1; |
| 309 | } |
| 310 | |
David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 311 | static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, |
| 312 | unsigned short dev_type) |
Jarek Poplawski | 723e98b | 2007-05-15 22:46:18 -0700 | [diff] [blame] | 313 | { |
| 314 | int i; |
| 315 | |
| 316 | i = netdev_lock_pos(dev_type); |
| 317 | lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i], |
| 318 | netdev_lock_name[i]); |
| 319 | } |
David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 320 | |
| 321 | static inline void netdev_set_addr_lockdep_class(struct net_device *dev) |
| 322 | { |
| 323 | int i; |
| 324 | |
| 325 | i = netdev_lock_pos(dev->type); |
| 326 | lockdep_set_class_and_name(&dev->addr_list_lock, |
| 327 | &netdev_addr_lock_key[i], |
| 328 | netdev_lock_name[i]); |
| 329 | } |
Jarek Poplawski | 723e98b | 2007-05-15 22:46:18 -0700 | [diff] [blame] | 330 | #else |
David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 331 | static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, |
| 332 | unsigned short dev_type) |
| 333 | { |
| 334 | } |
| 335 | static inline void netdev_set_addr_lockdep_class(struct net_device *dev) |
Jarek Poplawski | 723e98b | 2007-05-15 22:46:18 -0700 | [diff] [blame] | 336 | { |
| 337 | } |
| 338 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | |
| 340 | /******************************************************************************* |
| 341 | |
| 342 | Protocol management and registration routines |
| 343 | |
| 344 | *******************************************************************************/ |
| 345 | |
| 346 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | * Add a protocol ID to the list. Now that the input handler is |
| 348 | * smarter we can dispense with all the messy stuff that used to be |
| 349 | * here. |
| 350 | * |
| 351 | * BEWARE!!! Protocol handlers, mangling input packets, |
| 352 | * MUST BE last in hash buckets and checking protocol handlers |
| 353 | * MUST start from promiscuous ptype_all chain in net_bh. |
| 354 | * It is true now, do not change it. |
| 355 | * Explanation follows: if protocol handler, mangling packet, will |
| 356 | * be the first on list, it is not able to sense, that packet |
| 357 | * is cloned and should be copied-on-write, so that it will |
| 358 | * change it and subsequent readers will get broken packet. |
| 359 | * --ANK (980803) |
| 360 | */ |
| 361 | |
Eric Dumazet | c07b68e | 2010-09-02 03:53:46 +0000 | [diff] [blame] | 362 | static inline struct list_head *ptype_head(const struct packet_type *pt) |
| 363 | { |
| 364 | if (pt->type == htons(ETH_P_ALL)) |
| 365 | return &ptype_all; |
| 366 | else |
| 367 | return &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK]; |
| 368 | } |
| 369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | /** |
| 371 | * dev_add_pack - add packet handler |
| 372 | * @pt: packet type declaration |
| 373 | * |
| 374 | * Add a protocol handler to the networking stack. The passed &packet_type |
| 375 | * is linked into kernel lists and may not be freed until it has been |
| 376 | * removed from the kernel lists. |
| 377 | * |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 378 | * This call does not sleep therefore it can not |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | * guarantee all CPU's that are in middle of receiving packets |
| 380 | * will see the new packet type (until the next received packet). |
| 381 | */ |
| 382 | |
| 383 | void dev_add_pack(struct packet_type *pt) |
| 384 | { |
Eric Dumazet | c07b68e | 2010-09-02 03:53:46 +0000 | [diff] [blame] | 385 | struct list_head *head = ptype_head(pt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
Eric Dumazet | c07b68e | 2010-09-02 03:53:46 +0000 | [diff] [blame] | 387 | spin_lock(&ptype_lock); |
| 388 | list_add_rcu(&pt->list, head); |
| 389 | spin_unlock(&ptype_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 391 | EXPORT_SYMBOL(dev_add_pack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | /** |
| 394 | * __dev_remove_pack - remove packet handler |
| 395 | * @pt: packet type declaration |
| 396 | * |
| 397 | * Remove a protocol handler that was previously added to the kernel |
| 398 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed |
| 399 | * from the kernel lists and can be freed or reused once this function |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 400 | * returns. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | * |
| 402 | * The packet type might still be in use by receivers |
| 403 | * and must not be freed until after all the CPU's have gone |
| 404 | * through a quiescent state. |
| 405 | */ |
| 406 | void __dev_remove_pack(struct packet_type *pt) |
| 407 | { |
Eric Dumazet | c07b68e | 2010-09-02 03:53:46 +0000 | [diff] [blame] | 408 | struct list_head *head = ptype_head(pt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | struct packet_type *pt1; |
| 410 | |
Eric Dumazet | c07b68e | 2010-09-02 03:53:46 +0000 | [diff] [blame] | 411 | spin_lock(&ptype_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | list_for_each_entry(pt1, head, list) { |
| 414 | if (pt == pt1) { |
| 415 | list_del_rcu(&pt->list); |
| 416 | goto out; |
| 417 | } |
| 418 | } |
| 419 | |
Joe Perches | 7b6cd1c | 2012-02-01 10:54:43 +0000 | [diff] [blame] | 420 | pr_warn("dev_remove_pack: %p not found\n", pt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | out: |
Eric Dumazet | c07b68e | 2010-09-02 03:53:46 +0000 | [diff] [blame] | 422 | spin_unlock(&ptype_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 424 | EXPORT_SYMBOL(__dev_remove_pack); |
| 425 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | /** |
| 427 | * dev_remove_pack - remove packet handler |
| 428 | * @pt: packet type declaration |
| 429 | * |
| 430 | * Remove a protocol handler that was previously added to the kernel |
| 431 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed |
| 432 | * from the kernel lists and can be freed or reused once this function |
| 433 | * returns. |
| 434 | * |
| 435 | * This call sleeps to guarantee that no CPU is looking at the packet |
| 436 | * type after return. |
| 437 | */ |
| 438 | void dev_remove_pack(struct packet_type *pt) |
| 439 | { |
| 440 | __dev_remove_pack(pt); |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | synchronize_net(); |
| 443 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 444 | EXPORT_SYMBOL(dev_remove_pack); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | |
Vlad Yasevich | 62532da | 2012-11-15 08:49:10 +0000 | [diff] [blame] | 446 | |
| 447 | /** |
| 448 | * dev_add_offload - register offload handlers |
| 449 | * @po: protocol offload declaration |
| 450 | * |
| 451 | * Add protocol offload handlers to the networking stack. The passed |
| 452 | * &proto_offload is linked into kernel lists and may not be freed until |
| 453 | * it has been removed from the kernel lists. |
| 454 | * |
| 455 | * This call does not sleep therefore it can not |
| 456 | * guarantee all CPU's that are in middle of receiving packets |
| 457 | * will see the new offload handlers (until the next received packet). |
| 458 | */ |
| 459 | void dev_add_offload(struct packet_offload *po) |
| 460 | { |
| 461 | struct list_head *head = &offload_base; |
| 462 | |
| 463 | spin_lock(&offload_lock); |
| 464 | list_add_rcu(&po->list, head); |
| 465 | spin_unlock(&offload_lock); |
| 466 | } |
| 467 | EXPORT_SYMBOL(dev_add_offload); |
| 468 | |
| 469 | /** |
| 470 | * __dev_remove_offload - remove offload handler |
| 471 | * @po: packet offload declaration |
| 472 | * |
| 473 | * Remove a protocol offload handler that was previously added to the |
| 474 | * kernel offload handlers by dev_add_offload(). The passed &offload_type |
| 475 | * is removed from the kernel lists and can be freed or reused once this |
| 476 | * function returns. |
| 477 | * |
| 478 | * The packet type might still be in use by receivers |
| 479 | * and must not be freed until after all the CPU's have gone |
| 480 | * through a quiescent state. |
| 481 | */ |
| 482 | void __dev_remove_offload(struct packet_offload *po) |
| 483 | { |
| 484 | struct list_head *head = &offload_base; |
| 485 | struct packet_offload *po1; |
| 486 | |
Eric Dumazet | c53aa50 | 2012-11-16 08:08:23 +0000 | [diff] [blame] | 487 | spin_lock(&offload_lock); |
Vlad Yasevich | 62532da | 2012-11-15 08:49:10 +0000 | [diff] [blame] | 488 | |
| 489 | list_for_each_entry(po1, head, list) { |
| 490 | if (po == po1) { |
| 491 | list_del_rcu(&po->list); |
| 492 | goto out; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | pr_warn("dev_remove_offload: %p not found\n", po); |
| 497 | out: |
Eric Dumazet | c53aa50 | 2012-11-16 08:08:23 +0000 | [diff] [blame] | 498 | spin_unlock(&offload_lock); |
Vlad Yasevich | 62532da | 2012-11-15 08:49:10 +0000 | [diff] [blame] | 499 | } |
| 500 | EXPORT_SYMBOL(__dev_remove_offload); |
| 501 | |
| 502 | /** |
| 503 | * dev_remove_offload - remove packet offload handler |
| 504 | * @po: packet offload declaration |
| 505 | * |
| 506 | * Remove a packet offload handler that was previously added to the kernel |
| 507 | * offload handlers by dev_add_offload(). The passed &offload_type is |
| 508 | * removed from the kernel lists and can be freed or reused once this |
| 509 | * function returns. |
| 510 | * |
| 511 | * This call sleeps to guarantee that no CPU is looking at the packet |
| 512 | * type after return. |
| 513 | */ |
| 514 | void dev_remove_offload(struct packet_offload *po) |
| 515 | { |
| 516 | __dev_remove_offload(po); |
| 517 | |
| 518 | synchronize_net(); |
| 519 | } |
| 520 | EXPORT_SYMBOL(dev_remove_offload); |
| 521 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | /****************************************************************************** |
| 523 | |
| 524 | Device Boot-time Settings Routines |
| 525 | |
| 526 | *******************************************************************************/ |
| 527 | |
| 528 | /* Boot time configuration table */ |
| 529 | static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX]; |
| 530 | |
| 531 | /** |
| 532 | * netdev_boot_setup_add - add new setup entry |
| 533 | * @name: name of the device |
| 534 | * @map: configured settings for the device |
| 535 | * |
| 536 | * Adds new setup entry to the dev_boot_setup list. The function |
| 537 | * returns 0 on error and 1 on success. This is a generic routine to |
| 538 | * all netdevices. |
| 539 | */ |
| 540 | static int netdev_boot_setup_add(char *name, struct ifmap *map) |
| 541 | { |
| 542 | struct netdev_boot_setup *s; |
| 543 | int i; |
| 544 | |
| 545 | s = dev_boot_setup; |
| 546 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { |
| 547 | if (s[i].name[0] == '\0' || s[i].name[0] == ' ') { |
| 548 | memset(s[i].name, 0, sizeof(s[i].name)); |
Wang Chen | 93b3cff | 2008-07-01 19:57:19 -0700 | [diff] [blame] | 549 | strlcpy(s[i].name, name, IFNAMSIZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | memcpy(&s[i].map, map, sizeof(s[i].map)); |
| 551 | break; |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1; |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * netdev_boot_setup_check - check boot time settings |
| 560 | * @dev: the netdevice |
| 561 | * |
| 562 | * Check boot time settings for the device. |
| 563 | * The found settings are set for the device to be used |
| 564 | * later in the device probing. |
| 565 | * Returns 0 if no settings found, 1 if they are. |
| 566 | */ |
| 567 | int netdev_boot_setup_check(struct net_device *dev) |
| 568 | { |
| 569 | struct netdev_boot_setup *s = dev_boot_setup; |
| 570 | int i; |
| 571 | |
| 572 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { |
| 573 | if (s[i].name[0] != '\0' && s[i].name[0] != ' ' && |
Wang Chen | 93b3cff | 2008-07-01 19:57:19 -0700 | [diff] [blame] | 574 | !strcmp(dev->name, s[i].name)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | dev->irq = s[i].map.irq; |
| 576 | dev->base_addr = s[i].map.base_addr; |
| 577 | dev->mem_start = s[i].map.mem_start; |
| 578 | dev->mem_end = s[i].map.mem_end; |
| 579 | return 1; |
| 580 | } |
| 581 | } |
| 582 | return 0; |
| 583 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 584 | EXPORT_SYMBOL(netdev_boot_setup_check); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
| 586 | |
| 587 | /** |
| 588 | * netdev_boot_base - get address from boot time settings |
| 589 | * @prefix: prefix for network device |
| 590 | * @unit: id for network device |
| 591 | * |
| 592 | * Check boot time settings for the base address of device. |
| 593 | * The found settings are set for the device to be used |
| 594 | * later in the device probing. |
| 595 | * Returns 0 if no settings found. |
| 596 | */ |
| 597 | unsigned long netdev_boot_base(const char *prefix, int unit) |
| 598 | { |
| 599 | const struct netdev_boot_setup *s = dev_boot_setup; |
| 600 | char name[IFNAMSIZ]; |
| 601 | int i; |
| 602 | |
| 603 | sprintf(name, "%s%d", prefix, unit); |
| 604 | |
| 605 | /* |
| 606 | * If device already registered then return base of 1 |
| 607 | * to indicate not to probe for this interface |
| 608 | */ |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 609 | if (__dev_get_by_name(&init_net, name)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | return 1; |
| 611 | |
| 612 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) |
| 613 | if (!strcmp(name, s[i].name)) |
| 614 | return s[i].map.base_addr; |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | /* |
| 619 | * Saves at boot time configured settings for any netdevice. |
| 620 | */ |
| 621 | int __init netdev_boot_setup(char *str) |
| 622 | { |
| 623 | int ints[5]; |
| 624 | struct ifmap map; |
| 625 | |
| 626 | str = get_options(str, ARRAY_SIZE(ints), ints); |
| 627 | if (!str || !*str) |
| 628 | return 0; |
| 629 | |
| 630 | /* Save settings */ |
| 631 | memset(&map, 0, sizeof(map)); |
| 632 | if (ints[0] > 0) |
| 633 | map.irq = ints[1]; |
| 634 | if (ints[0] > 1) |
| 635 | map.base_addr = ints[2]; |
| 636 | if (ints[0] > 2) |
| 637 | map.mem_start = ints[3]; |
| 638 | if (ints[0] > 3) |
| 639 | map.mem_end = ints[4]; |
| 640 | |
| 641 | /* Add new entry to the list */ |
| 642 | return netdev_boot_setup_add(str, &map); |
| 643 | } |
| 644 | |
| 645 | __setup("netdev=", netdev_boot_setup); |
| 646 | |
| 647 | /******************************************************************************* |
| 648 | |
| 649 | Device Interface Subroutines |
| 650 | |
| 651 | *******************************************************************************/ |
| 652 | |
| 653 | /** |
| 654 | * __dev_get_by_name - find a device by its name |
Randy Dunlap | c4ea43c | 2007-10-12 21:17:49 -0700 | [diff] [blame] | 655 | * @net: the applicable net namespace |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | * @name: name to find |
| 657 | * |
| 658 | * Find an interface by name. Must be called under RTNL semaphore |
| 659 | * or @dev_base_lock. If the name is found a pointer to the device |
| 660 | * is returned. If the name is not found then %NULL is returned. The |
| 661 | * reference counters are not incremented so the caller must be |
| 662 | * careful with locks. |
| 663 | */ |
| 664 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 665 | struct net_device *__dev_get_by_name(struct net *net, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 666 | { |
Eric Dumazet | 0bd8d53 | 2009-10-30 01:40:11 -0700 | [diff] [blame] | 667 | struct net_device *dev; |
| 668 | struct hlist_head *head = dev_name_hash(net, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 670 | hlist_for_each_entry(dev, head, name_hlist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | if (!strncmp(dev->name, name, IFNAMSIZ)) |
| 672 | return dev; |
Eric Dumazet | 0bd8d53 | 2009-10-30 01:40:11 -0700 | [diff] [blame] | 673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | return NULL; |
| 675 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 676 | EXPORT_SYMBOL(__dev_get_by_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | |
| 678 | /** |
Eric Dumazet | 72c9528 | 2009-10-30 07:11:27 +0000 | [diff] [blame] | 679 | * dev_get_by_name_rcu - find a device by its name |
| 680 | * @net: the applicable net namespace |
| 681 | * @name: name to find |
| 682 | * |
| 683 | * Find an interface by name. |
| 684 | * If the name is found a pointer to the device is returned. |
| 685 | * If the name is not found then %NULL is returned. |
| 686 | * The reference counters are not incremented so the caller must be |
| 687 | * careful with locks. The caller must hold RCU lock. |
| 688 | */ |
| 689 | |
| 690 | struct net_device *dev_get_by_name_rcu(struct net *net, const char *name) |
| 691 | { |
Eric Dumazet | 72c9528 | 2009-10-30 07:11:27 +0000 | [diff] [blame] | 692 | struct net_device *dev; |
| 693 | struct hlist_head *head = dev_name_hash(net, name); |
| 694 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 695 | hlist_for_each_entry_rcu(dev, head, name_hlist) |
Eric Dumazet | 72c9528 | 2009-10-30 07:11:27 +0000 | [diff] [blame] | 696 | if (!strncmp(dev->name, name, IFNAMSIZ)) |
| 697 | return dev; |
| 698 | |
| 699 | return NULL; |
| 700 | } |
| 701 | EXPORT_SYMBOL(dev_get_by_name_rcu); |
| 702 | |
| 703 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | * dev_get_by_name - find a device by its name |
Randy Dunlap | c4ea43c | 2007-10-12 21:17:49 -0700 | [diff] [blame] | 705 | * @net: the applicable net namespace |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | * @name: name to find |
| 707 | * |
| 708 | * Find an interface by name. This can be called from any |
| 709 | * context and does its own locking. The returned handle has |
| 710 | * the usage count incremented and the caller must use dev_put() to |
| 711 | * release it when it is no longer needed. %NULL is returned if no |
| 712 | * matching device is found. |
| 713 | */ |
| 714 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 715 | struct net_device *dev_get_by_name(struct net *net, const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | { |
| 717 | struct net_device *dev; |
| 718 | |
Eric Dumazet | 72c9528 | 2009-10-30 07:11:27 +0000 | [diff] [blame] | 719 | rcu_read_lock(); |
| 720 | dev = dev_get_by_name_rcu(net, name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | if (dev) |
| 722 | dev_hold(dev); |
Eric Dumazet | 72c9528 | 2009-10-30 07:11:27 +0000 | [diff] [blame] | 723 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 | return dev; |
| 725 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 726 | EXPORT_SYMBOL(dev_get_by_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | |
| 728 | /** |
| 729 | * __dev_get_by_index - find a device by its ifindex |
Randy Dunlap | c4ea43c | 2007-10-12 21:17:49 -0700 | [diff] [blame] | 730 | * @net: the applicable net namespace |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | * @ifindex: index of device |
| 732 | * |
| 733 | * Search for an interface by index. Returns %NULL if the device |
| 734 | * is not found or a pointer to the device. The device has not |
| 735 | * had its reference counter increased so the caller must be careful |
| 736 | * about locking. The caller must hold either the RTNL semaphore |
| 737 | * or @dev_base_lock. |
| 738 | */ |
| 739 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 740 | struct net_device *__dev_get_by_index(struct net *net, int ifindex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | { |
Eric Dumazet | 0bd8d53 | 2009-10-30 01:40:11 -0700 | [diff] [blame] | 742 | struct net_device *dev; |
| 743 | struct hlist_head *head = dev_index_hash(net, ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 745 | hlist_for_each_entry(dev, head, index_hlist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | if (dev->ifindex == ifindex) |
| 747 | return dev; |
Eric Dumazet | 0bd8d53 | 2009-10-30 01:40:11 -0700 | [diff] [blame] | 748 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 749 | return NULL; |
| 750 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 751 | EXPORT_SYMBOL(__dev_get_by_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | |
Eric Dumazet | fb699dfd | 2009-10-19 19:18:49 +0000 | [diff] [blame] | 753 | /** |
| 754 | * dev_get_by_index_rcu - find a device by its ifindex |
| 755 | * @net: the applicable net namespace |
| 756 | * @ifindex: index of device |
| 757 | * |
| 758 | * Search for an interface by index. Returns %NULL if the device |
| 759 | * is not found or a pointer to the device. The device has not |
| 760 | * had its reference counter increased so the caller must be careful |
| 761 | * about locking. The caller must hold RCU lock. |
| 762 | */ |
| 763 | |
| 764 | struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex) |
| 765 | { |
Eric Dumazet | fb699dfd | 2009-10-19 19:18:49 +0000 | [diff] [blame] | 766 | struct net_device *dev; |
| 767 | struct hlist_head *head = dev_index_hash(net, ifindex); |
| 768 | |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 769 | hlist_for_each_entry_rcu(dev, head, index_hlist) |
Eric Dumazet | fb699dfd | 2009-10-19 19:18:49 +0000 | [diff] [blame] | 770 | if (dev->ifindex == ifindex) |
| 771 | return dev; |
| 772 | |
| 773 | return NULL; |
| 774 | } |
| 775 | EXPORT_SYMBOL(dev_get_by_index_rcu); |
| 776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | |
| 778 | /** |
| 779 | * dev_get_by_index - find a device by its ifindex |
Randy Dunlap | c4ea43c | 2007-10-12 21:17:49 -0700 | [diff] [blame] | 780 | * @net: the applicable net namespace |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | * @ifindex: index of device |
| 782 | * |
| 783 | * Search for an interface by index. Returns NULL if the device |
| 784 | * is not found or a pointer to the device. The device returned has |
| 785 | * had a reference added and the pointer is safe until the user calls |
| 786 | * dev_put to indicate they have finished with it. |
| 787 | */ |
| 788 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 789 | struct net_device *dev_get_by_index(struct net *net, int ifindex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | { |
| 791 | struct net_device *dev; |
| 792 | |
Eric Dumazet | fb699dfd | 2009-10-19 19:18:49 +0000 | [diff] [blame] | 793 | rcu_read_lock(); |
| 794 | dev = dev_get_by_index_rcu(net, ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | if (dev) |
| 796 | dev_hold(dev); |
Eric Dumazet | fb699dfd | 2009-10-19 19:18:49 +0000 | [diff] [blame] | 797 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | return dev; |
| 799 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 800 | EXPORT_SYMBOL(dev_get_by_index); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | |
| 802 | /** |
Eric Dumazet | 941666c | 2010-12-05 01:23:53 +0000 | [diff] [blame] | 803 | * dev_getbyhwaddr_rcu - find a device by its hardware address |
Randy Dunlap | c4ea43c | 2007-10-12 21:17:49 -0700 | [diff] [blame] | 804 | * @net: the applicable net namespace |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | * @type: media type of device |
| 806 | * @ha: hardware address |
| 807 | * |
| 808 | * Search for an interface by MAC address. Returns NULL if the device |
Eric Dumazet | c506653 | 2011-01-24 13:16:16 -0800 | [diff] [blame] | 809 | * is not found or a pointer to the device. |
| 810 | * The caller must hold RCU or RTNL. |
Eric Dumazet | 941666c | 2010-12-05 01:23:53 +0000 | [diff] [blame] | 811 | * The returned device has not had its ref count increased |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | * and the caller must therefore be careful about locking |
| 813 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | */ |
| 815 | |
Eric Dumazet | 941666c | 2010-12-05 01:23:53 +0000 | [diff] [blame] | 816 | struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type, |
| 817 | const char *ha) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | { |
| 819 | struct net_device *dev; |
| 820 | |
Eric Dumazet | 941666c | 2010-12-05 01:23:53 +0000 | [diff] [blame] | 821 | for_each_netdev_rcu(net, dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | if (dev->type == type && |
| 823 | !memcmp(dev->dev_addr, ha, dev->addr_len)) |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 824 | return dev; |
| 825 | |
| 826 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | } |
Eric Dumazet | 941666c | 2010-12-05 01:23:53 +0000 | [diff] [blame] | 828 | EXPORT_SYMBOL(dev_getbyhwaddr_rcu); |
Jochen Friedrich | cf309e3 | 2005-09-22 04:44:55 -0300 | [diff] [blame] | 829 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 830 | struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type) |
Patrick McHardy | 4e9cac2 | 2007-05-03 03:28:13 -0700 | [diff] [blame] | 831 | { |
| 832 | struct net_device *dev; |
| 833 | |
| 834 | ASSERT_RTNL(); |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 835 | for_each_netdev(net, dev) |
Patrick McHardy | 4e9cac2 | 2007-05-03 03:28:13 -0700 | [diff] [blame] | 836 | if (dev->type == type) |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 837 | return dev; |
| 838 | |
| 839 | return NULL; |
Patrick McHardy | 4e9cac2 | 2007-05-03 03:28:13 -0700 | [diff] [blame] | 840 | } |
Patrick McHardy | 4e9cac2 | 2007-05-03 03:28:13 -0700 | [diff] [blame] | 841 | EXPORT_SYMBOL(__dev_getfirstbyhwtype); |
| 842 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 843 | struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | { |
Eric Dumazet | 99fe3c3 | 2010-03-18 11:27:25 +0000 | [diff] [blame] | 845 | struct net_device *dev, *ret = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | |
Eric Dumazet | 99fe3c3 | 2010-03-18 11:27:25 +0000 | [diff] [blame] | 847 | rcu_read_lock(); |
| 848 | for_each_netdev_rcu(net, dev) |
| 849 | if (dev->type == type) { |
| 850 | dev_hold(dev); |
| 851 | ret = dev; |
| 852 | break; |
| 853 | } |
| 854 | rcu_read_unlock(); |
| 855 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | EXPORT_SYMBOL(dev_getfirstbyhwtype); |
| 858 | |
| 859 | /** |
Eric Dumazet | bb69ae0 | 2010-06-07 11:42:13 +0000 | [diff] [blame] | 860 | * dev_get_by_flags_rcu - find any device with given flags |
Randy Dunlap | c4ea43c | 2007-10-12 21:17:49 -0700 | [diff] [blame] | 861 | * @net: the applicable net namespace |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 862 | * @if_flags: IFF_* values |
| 863 | * @mask: bitmask of bits in if_flags to check |
| 864 | * |
| 865 | * Search for any interface with the given flags. Returns NULL if a device |
Eric Dumazet | bb69ae0 | 2010-06-07 11:42:13 +0000 | [diff] [blame] | 866 | * is not found or a pointer to the device. Must be called inside |
| 867 | * rcu_read_lock(), and result refcount is unchanged. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | */ |
| 869 | |
Eric Dumazet | bb69ae0 | 2010-06-07 11:42:13 +0000 | [diff] [blame] | 870 | struct net_device *dev_get_by_flags_rcu(struct net *net, unsigned short if_flags, |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 871 | unsigned short mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | { |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 873 | struct net_device *dev, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 875 | ret = NULL; |
Eric Dumazet | c6d14c8 | 2009-11-04 05:43:23 -0800 | [diff] [blame] | 876 | for_each_netdev_rcu(net, dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | if (((dev->flags ^ if_flags) & mask) == 0) { |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 878 | ret = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | break; |
| 880 | } |
| 881 | } |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 882 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | } |
Eric Dumazet | bb69ae0 | 2010-06-07 11:42:13 +0000 | [diff] [blame] | 884 | EXPORT_SYMBOL(dev_get_by_flags_rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | |
| 886 | /** |
| 887 | * dev_valid_name - check if name is okay for network device |
| 888 | * @name: name string |
| 889 | * |
| 890 | * Network device names need to be valid file names to |
David S. Miller | c7fa9d1 | 2006-08-15 16:34:13 -0700 | [diff] [blame] | 891 | * to allow sysfs to work. We also disallow any kind of |
| 892 | * whitespace. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | */ |
David S. Miller | 95f050b | 2012-03-06 16:12:15 -0500 | [diff] [blame] | 894 | bool dev_valid_name(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | { |
David S. Miller | c7fa9d1 | 2006-08-15 16:34:13 -0700 | [diff] [blame] | 896 | if (*name == '\0') |
David S. Miller | 95f050b | 2012-03-06 16:12:15 -0500 | [diff] [blame] | 897 | return false; |
Stephen Hemminger | b6fe17d | 2006-08-29 17:06:13 -0700 | [diff] [blame] | 898 | if (strlen(name) >= IFNAMSIZ) |
David S. Miller | 95f050b | 2012-03-06 16:12:15 -0500 | [diff] [blame] | 899 | return false; |
David S. Miller | c7fa9d1 | 2006-08-15 16:34:13 -0700 | [diff] [blame] | 900 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
David S. Miller | 95f050b | 2012-03-06 16:12:15 -0500 | [diff] [blame] | 901 | return false; |
David S. Miller | c7fa9d1 | 2006-08-15 16:34:13 -0700 | [diff] [blame] | 902 | |
| 903 | while (*name) { |
| 904 | if (*name == '/' || isspace(*name)) |
David S. Miller | 95f050b | 2012-03-06 16:12:15 -0500 | [diff] [blame] | 905 | return false; |
David S. Miller | c7fa9d1 | 2006-08-15 16:34:13 -0700 | [diff] [blame] | 906 | name++; |
| 907 | } |
David S. Miller | 95f050b | 2012-03-06 16:12:15 -0500 | [diff] [blame] | 908 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 910 | EXPORT_SYMBOL(dev_valid_name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 911 | |
| 912 | /** |
Eric W. Biederman | b267b17 | 2007-09-12 13:48:45 +0200 | [diff] [blame] | 913 | * __dev_alloc_name - allocate a name for a device |
| 914 | * @net: network namespace to allocate the device name in |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 915 | * @name: name format string |
Eric W. Biederman | b267b17 | 2007-09-12 13:48:45 +0200 | [diff] [blame] | 916 | * @buf: scratch buffer and result name string |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 917 | * |
| 918 | * Passed a format string - eg "lt%d" it will try and find a suitable |
Stephen Hemminger | 3041a06 | 2006-05-26 13:25:24 -0700 | [diff] [blame] | 919 | * id. It scans list of devices to build up a free map, then chooses |
| 920 | * the first empty slot. The caller must hold the dev_base or rtnl lock |
| 921 | * while allocating the name and adding the device in order to avoid |
| 922 | * duplicates. |
| 923 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). |
| 924 | * Returns the number of the unit assigned or a negative errno code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | */ |
| 926 | |
Eric W. Biederman | b267b17 | 2007-09-12 13:48:45 +0200 | [diff] [blame] | 927 | static int __dev_alloc_name(struct net *net, const char *name, char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | { |
| 929 | int i = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | const char *p; |
| 931 | const int max_netdevices = 8*PAGE_SIZE; |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 932 | unsigned long *inuse; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | struct net_device *d; |
| 934 | |
| 935 | p = strnchr(name, IFNAMSIZ-1, '%'); |
| 936 | if (p) { |
| 937 | /* |
| 938 | * Verify the string as this thing may have come from |
| 939 | * the user. There must be either one "%d" and no other "%" |
| 940 | * characters. |
| 941 | */ |
| 942 | if (p[1] != 'd' || strchr(p + 2, '%')) |
| 943 | return -EINVAL; |
| 944 | |
| 945 | /* Use one page as a bit array of possible slots */ |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 946 | inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | if (!inuse) |
| 948 | return -ENOMEM; |
| 949 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 950 | for_each_netdev(net, d) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | if (!sscanf(d->name, name, &i)) |
| 952 | continue; |
| 953 | if (i < 0 || i >= max_netdevices) |
| 954 | continue; |
| 955 | |
| 956 | /* avoid cases where sscanf is not exact inverse of printf */ |
Eric W. Biederman | b267b17 | 2007-09-12 13:48:45 +0200 | [diff] [blame] | 957 | snprintf(buf, IFNAMSIZ, name, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | if (!strncmp(buf, d->name, IFNAMSIZ)) |
| 959 | set_bit(i, inuse); |
| 960 | } |
| 961 | |
| 962 | i = find_first_zero_bit(inuse, max_netdevices); |
| 963 | free_page((unsigned long) inuse); |
| 964 | } |
| 965 | |
Octavian Purdila | d903102 | 2009-11-18 02:36:59 +0000 | [diff] [blame] | 966 | if (buf != name) |
| 967 | snprintf(buf, IFNAMSIZ, name, i); |
Eric W. Biederman | b267b17 | 2007-09-12 13:48:45 +0200 | [diff] [blame] | 968 | if (!__dev_get_by_name(net, buf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | return i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
| 971 | /* It is possible to run out of possible slots |
| 972 | * when the name is long and there isn't enough space left |
| 973 | * for the digits, or if all bits are used. |
| 974 | */ |
| 975 | return -ENFILE; |
| 976 | } |
| 977 | |
Eric W. Biederman | b267b17 | 2007-09-12 13:48:45 +0200 | [diff] [blame] | 978 | /** |
| 979 | * dev_alloc_name - allocate a name for a device |
| 980 | * @dev: device |
| 981 | * @name: name format string |
| 982 | * |
| 983 | * Passed a format string - eg "lt%d" it will try and find a suitable |
| 984 | * id. It scans list of devices to build up a free map, then chooses |
| 985 | * the first empty slot. The caller must hold the dev_base or rtnl lock |
| 986 | * while allocating the name and adding the device in order to avoid |
| 987 | * duplicates. |
| 988 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). |
| 989 | * Returns the number of the unit assigned or a negative errno code. |
| 990 | */ |
| 991 | |
| 992 | int dev_alloc_name(struct net_device *dev, const char *name) |
| 993 | { |
| 994 | char buf[IFNAMSIZ]; |
| 995 | struct net *net; |
| 996 | int ret; |
| 997 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 998 | BUG_ON(!dev_net(dev)); |
| 999 | net = dev_net(dev); |
Eric W. Biederman | b267b17 | 2007-09-12 13:48:45 +0200 | [diff] [blame] | 1000 | ret = __dev_alloc_name(net, name, buf); |
| 1001 | if (ret >= 0) |
| 1002 | strlcpy(dev->name, buf, IFNAMSIZ); |
| 1003 | return ret; |
| 1004 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 1005 | EXPORT_SYMBOL(dev_alloc_name); |
Eric W. Biederman | b267b17 | 2007-09-12 13:48:45 +0200 | [diff] [blame] | 1006 | |
Gao feng | 828de4f | 2012-09-13 20:58:27 +0000 | [diff] [blame] | 1007 | static int dev_alloc_name_ns(struct net *net, |
| 1008 | struct net_device *dev, |
| 1009 | const char *name) |
Octavian Purdila | d903102 | 2009-11-18 02:36:59 +0000 | [diff] [blame] | 1010 | { |
Gao feng | 828de4f | 2012-09-13 20:58:27 +0000 | [diff] [blame] | 1011 | char buf[IFNAMSIZ]; |
| 1012 | int ret; |
Daniel Lezcano | 8ce6cebc | 2010-05-19 10:12:19 +0000 | [diff] [blame] | 1013 | |
Gao feng | 828de4f | 2012-09-13 20:58:27 +0000 | [diff] [blame] | 1014 | ret = __dev_alloc_name(net, name, buf); |
| 1015 | if (ret >= 0) |
| 1016 | strlcpy(dev->name, buf, IFNAMSIZ); |
| 1017 | return ret; |
| 1018 | } |
| 1019 | |
| 1020 | static int dev_get_valid_name(struct net *net, |
| 1021 | struct net_device *dev, |
| 1022 | const char *name) |
| 1023 | { |
| 1024 | BUG_ON(!net); |
Daniel Lezcano | 8ce6cebc | 2010-05-19 10:12:19 +0000 | [diff] [blame] | 1025 | |
Octavian Purdila | d903102 | 2009-11-18 02:36:59 +0000 | [diff] [blame] | 1026 | if (!dev_valid_name(name)) |
| 1027 | return -EINVAL; |
| 1028 | |
Jiri Pirko | 1c5cae8 | 2011-04-30 01:21:32 +0000 | [diff] [blame] | 1029 | if (strchr(name, '%')) |
Gao feng | 828de4f | 2012-09-13 20:58:27 +0000 | [diff] [blame] | 1030 | return dev_alloc_name_ns(net, dev, name); |
Octavian Purdila | d903102 | 2009-11-18 02:36:59 +0000 | [diff] [blame] | 1031 | else if (__dev_get_by_name(net, name)) |
| 1032 | return -EEXIST; |
Daniel Lezcano | 8ce6cebc | 2010-05-19 10:12:19 +0000 | [diff] [blame] | 1033 | else if (dev->name != name) |
| 1034 | strlcpy(dev->name, name, IFNAMSIZ); |
Octavian Purdila | d903102 | 2009-11-18 02:36:59 +0000 | [diff] [blame] | 1035 | |
| 1036 | return 0; |
| 1037 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | |
| 1039 | /** |
| 1040 | * dev_change_name - change name of a device |
| 1041 | * @dev: device |
| 1042 | * @newname: name (or format string) must be at least IFNAMSIZ |
| 1043 | * |
| 1044 | * Change name of a device, can pass format strings "eth%d". |
| 1045 | * for wildcarding. |
| 1046 | */ |
Stephen Hemminger | cf04a4c7 | 2008-09-30 02:22:14 -0700 | [diff] [blame] | 1047 | int dev_change_name(struct net_device *dev, const char *newname) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1048 | { |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1049 | char oldname[IFNAMSIZ]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | int err = 0; |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1051 | int ret; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1052 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
| 1054 | ASSERT_RTNL(); |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1055 | BUG_ON(!dev_net(dev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1056 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1057 | net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | if (dev->flags & IFF_UP) |
| 1059 | return -EBUSY; |
| 1060 | |
Eric Dumazet | 30e6c9f | 2012-12-20 17:25:08 +0000 | [diff] [blame] | 1061 | write_seqcount_begin(&devnet_rename_seq); |
Brian Haley | c91f6df | 2012-11-26 05:21:08 +0000 | [diff] [blame] | 1062 | |
| 1063 | if (strncmp(newname, dev->name, IFNAMSIZ) == 0) { |
Eric Dumazet | 30e6c9f | 2012-12-20 17:25:08 +0000 | [diff] [blame] | 1064 | write_seqcount_end(&devnet_rename_seq); |
Stephen Hemminger | c8d90dc | 2007-10-26 03:53:42 -0700 | [diff] [blame] | 1065 | return 0; |
Brian Haley | c91f6df | 2012-11-26 05:21:08 +0000 | [diff] [blame] | 1066 | } |
Stephen Hemminger | c8d90dc | 2007-10-26 03:53:42 -0700 | [diff] [blame] | 1067 | |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1068 | memcpy(oldname, dev->name, IFNAMSIZ); |
| 1069 | |
Gao feng | 828de4f | 2012-09-13 20:58:27 +0000 | [diff] [blame] | 1070 | err = dev_get_valid_name(net, dev, newname); |
Brian Haley | c91f6df | 2012-11-26 05:21:08 +0000 | [diff] [blame] | 1071 | if (err < 0) { |
Eric Dumazet | 30e6c9f | 2012-12-20 17:25:08 +0000 | [diff] [blame] | 1072 | write_seqcount_end(&devnet_rename_seq); |
Octavian Purdila | d903102 | 2009-11-18 02:36:59 +0000 | [diff] [blame] | 1073 | return err; |
Brian Haley | c91f6df | 2012-11-26 05:21:08 +0000 | [diff] [blame] | 1074 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1075 | |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1076 | rollback: |
Eric W. Biederman | a1b3f59 | 2010-05-04 17:36:49 -0700 | [diff] [blame] | 1077 | ret = device_rename(&dev->dev, dev->name); |
| 1078 | if (ret) { |
| 1079 | memcpy(dev->name, oldname, IFNAMSIZ); |
Eric Dumazet | 30e6c9f | 2012-12-20 17:25:08 +0000 | [diff] [blame] | 1080 | write_seqcount_end(&devnet_rename_seq); |
Eric W. Biederman | a1b3f59 | 2010-05-04 17:36:49 -0700 | [diff] [blame] | 1081 | return ret; |
Stephen Hemminger | dcc9977 | 2008-05-14 22:33:38 -0700 | [diff] [blame] | 1082 | } |
Herbert Xu | 7f988ea | 2007-07-30 16:35:46 -0700 | [diff] [blame] | 1083 | |
Eric Dumazet | 30e6c9f | 2012-12-20 17:25:08 +0000 | [diff] [blame] | 1084 | write_seqcount_end(&devnet_rename_seq); |
Brian Haley | c91f6df | 2012-11-26 05:21:08 +0000 | [diff] [blame] | 1085 | |
Herbert Xu | 7f988ea | 2007-07-30 16:35:46 -0700 | [diff] [blame] | 1086 | write_lock_bh(&dev_base_lock); |
Eric Dumazet | 372b231 | 2011-05-17 13:56:59 -0400 | [diff] [blame] | 1087 | hlist_del_rcu(&dev->name_hlist); |
Eric Dumazet | 72c9528 | 2009-10-30 07:11:27 +0000 | [diff] [blame] | 1088 | write_unlock_bh(&dev_base_lock); |
| 1089 | |
| 1090 | synchronize_rcu(); |
| 1091 | |
| 1092 | write_lock_bh(&dev_base_lock); |
| 1093 | hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name)); |
Herbert Xu | 7f988ea | 2007-07-30 16:35:46 -0700 | [diff] [blame] | 1094 | write_unlock_bh(&dev_base_lock); |
| 1095 | |
Pavel Emelyanov | 056925a | 2007-09-16 15:42:43 -0700 | [diff] [blame] | 1096 | ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev); |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1097 | ret = notifier_to_errno(ret); |
| 1098 | |
| 1099 | if (ret) { |
Eric Dumazet | 91e9c07b | 2009-11-15 23:30:24 +0000 | [diff] [blame] | 1100 | /* err >= 0 after dev_alloc_name() or stores the first errno */ |
| 1101 | if (err >= 0) { |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1102 | err = ret; |
Eric Dumazet | 30e6c9f | 2012-12-20 17:25:08 +0000 | [diff] [blame] | 1103 | write_seqcount_begin(&devnet_rename_seq); |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1104 | memcpy(dev->name, oldname, IFNAMSIZ); |
| 1105 | goto rollback; |
Eric Dumazet | 91e9c07b | 2009-11-15 23:30:24 +0000 | [diff] [blame] | 1106 | } else { |
Joe Perches | 7b6cd1c | 2012-02-01 10:54:43 +0000 | [diff] [blame] | 1107 | pr_err("%s: name change rollback failed: %d\n", |
Eric Dumazet | 91e9c07b | 2009-11-15 23:30:24 +0000 | [diff] [blame] | 1108 | dev->name, ret); |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1109 | } |
| 1110 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | |
| 1112 | return err; |
| 1113 | } |
| 1114 | |
| 1115 | /** |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1116 | * dev_set_alias - change ifalias of a device |
| 1117 | * @dev: device |
| 1118 | * @alias: name up to IFALIASZ |
Stephen Hemminger | f0db275 | 2008-09-30 02:23:58 -0700 | [diff] [blame] | 1119 | * @len: limit of bytes to copy from info |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1120 | * |
| 1121 | * Set ifalias for a device, |
| 1122 | */ |
| 1123 | int dev_set_alias(struct net_device *dev, const char *alias, size_t len) |
| 1124 | { |
Alexey Khoroshilov | 7364e44 | 2012-08-08 00:33:25 +0000 | [diff] [blame] | 1125 | char *new_ifalias; |
| 1126 | |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1127 | ASSERT_RTNL(); |
| 1128 | |
| 1129 | if (len >= IFALIASZ) |
| 1130 | return -EINVAL; |
| 1131 | |
Oliver Hartkopp | 96ca4a2 | 2008-09-23 21:23:19 -0700 | [diff] [blame] | 1132 | if (!len) { |
Sachin Kamat | 388dfc2 | 2012-11-20 00:57:04 +0000 | [diff] [blame] | 1133 | kfree(dev->ifalias); |
| 1134 | dev->ifalias = NULL; |
Oliver Hartkopp | 96ca4a2 | 2008-09-23 21:23:19 -0700 | [diff] [blame] | 1135 | return 0; |
| 1136 | } |
| 1137 | |
Alexey Khoroshilov | 7364e44 | 2012-08-08 00:33:25 +0000 | [diff] [blame] | 1138 | new_ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL); |
| 1139 | if (!new_ifalias) |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1140 | return -ENOMEM; |
Alexey Khoroshilov | 7364e44 | 2012-08-08 00:33:25 +0000 | [diff] [blame] | 1141 | dev->ifalias = new_ifalias; |
Stephen Hemminger | 0b815a1 | 2008-09-22 21:28:11 -0700 | [diff] [blame] | 1142 | |
| 1143 | strlcpy(dev->ifalias, alias, len+1); |
| 1144 | return len; |
| 1145 | } |
| 1146 | |
| 1147 | |
| 1148 | /** |
Stephen Hemminger | 3041a06 | 2006-05-26 13:25:24 -0700 | [diff] [blame] | 1149 | * netdev_features_change - device changes features |
Stephen Hemminger | d8a33ac | 2005-05-29 14:13:47 -0700 | [diff] [blame] | 1150 | * @dev: device to cause notification |
| 1151 | * |
| 1152 | * Called to indicate a device has changed features. |
| 1153 | */ |
| 1154 | void netdev_features_change(struct net_device *dev) |
| 1155 | { |
Pavel Emelyanov | 056925a | 2007-09-16 15:42:43 -0700 | [diff] [blame] | 1156 | call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev); |
Stephen Hemminger | d8a33ac | 2005-05-29 14:13:47 -0700 | [diff] [blame] | 1157 | } |
| 1158 | EXPORT_SYMBOL(netdev_features_change); |
| 1159 | |
| 1160 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | * netdev_state_change - device changes state |
| 1162 | * @dev: device to cause notification |
| 1163 | * |
| 1164 | * Called to indicate a device has changed state. This function calls |
| 1165 | * the notifier chains for netdev_chain and sends a NEWLINK message |
| 1166 | * to the routing socket. |
| 1167 | */ |
| 1168 | void netdev_state_change(struct net_device *dev) |
| 1169 | { |
| 1170 | if (dev->flags & IFF_UP) { |
Pavel Emelyanov | 056925a | 2007-09-16 15:42:43 -0700 | [diff] [blame] | 1171 | call_netdevice_notifiers(NETDEV_CHANGE, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0); |
| 1173 | } |
| 1174 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 1175 | EXPORT_SYMBOL(netdev_state_change); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | |
Amerigo Wang | ee89bab | 2012-08-09 22:14:56 +0000 | [diff] [blame] | 1177 | /** |
| 1178 | * netdev_notify_peers - notify network peers about existence of @dev |
| 1179 | * @dev: network device |
| 1180 | * |
| 1181 | * Generate traffic such that interested network peers are aware of |
| 1182 | * @dev, such as by generating a gratuitous ARP. This may be used when |
| 1183 | * a device wants to inform the rest of the network about some sort of |
| 1184 | * reconfiguration such as a failover event or virtual machine |
| 1185 | * migration. |
| 1186 | */ |
| 1187 | void netdev_notify_peers(struct net_device *dev) |
Or Gerlitz | c1da4ac | 2008-06-13 18:12:00 -0700 | [diff] [blame] | 1188 | { |
Amerigo Wang | ee89bab | 2012-08-09 22:14:56 +0000 | [diff] [blame] | 1189 | rtnl_lock(); |
| 1190 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev); |
| 1191 | rtnl_unlock(); |
Or Gerlitz | c1da4ac | 2008-06-13 18:12:00 -0700 | [diff] [blame] | 1192 | } |
Amerigo Wang | ee89bab | 2012-08-09 22:14:56 +0000 | [diff] [blame] | 1193 | EXPORT_SYMBOL(netdev_notify_peers); |
Or Gerlitz | c1da4ac | 2008-06-13 18:12:00 -0700 | [diff] [blame] | 1194 | |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 1195 | static int __dev_open(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | { |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1197 | const struct net_device_ops *ops = dev->netdev_ops; |
Johannes Berg | 3b8bcfd | 2009-05-30 01:39:53 +0200 | [diff] [blame] | 1198 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1199 | |
Ben Hutchings | e46b66b | 2008-05-08 02:53:17 -0700 | [diff] [blame] | 1200 | ASSERT_RTNL(); |
| 1201 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | if (!netif_device_present(dev)) |
| 1203 | return -ENODEV; |
| 1204 | |
Neil Horman | ca99ca1 | 2013-02-05 08:05:43 +0000 | [diff] [blame] | 1205 | /* Block netpoll from trying to do any rx path servicing. |
| 1206 | * If we don't do this there is a chance ndo_poll_controller |
| 1207 | * or ndo_poll may be running while we open the device |
| 1208 | */ |
dingtianhong | da6e378 | 2013-05-27 19:53:31 +0000 | [diff] [blame] | 1209 | netpoll_rx_disable(dev); |
Neil Horman | ca99ca1 | 2013-02-05 08:05:43 +0000 | [diff] [blame] | 1210 | |
Johannes Berg | 3b8bcfd | 2009-05-30 01:39:53 +0200 | [diff] [blame] | 1211 | ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev); |
| 1212 | ret = notifier_to_errno(ret); |
| 1213 | if (ret) |
| 1214 | return ret; |
| 1215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | set_bit(__LINK_STATE_START, &dev->state); |
Jeff Garzik | bada339 | 2007-10-23 20:19:37 -0700 | [diff] [blame] | 1217 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1218 | if (ops->ndo_validate_addr) |
| 1219 | ret = ops->ndo_validate_addr(dev); |
Jeff Garzik | bada339 | 2007-10-23 20:19:37 -0700 | [diff] [blame] | 1220 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 1221 | if (!ret && ops->ndo_open) |
| 1222 | ret = ops->ndo_open(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | |
Neil Horman | ca99ca1 | 2013-02-05 08:05:43 +0000 | [diff] [blame] | 1224 | netpoll_rx_enable(dev); |
| 1225 | |
Jeff Garzik | bada339 | 2007-10-23 20:19:37 -0700 | [diff] [blame] | 1226 | if (ret) |
| 1227 | clear_bit(__LINK_STATE_START, &dev->state); |
| 1228 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1229 | dev->flags |= IFF_UP; |
David S. Miller | b4bd07c | 2009-02-06 22:06:43 -0800 | [diff] [blame] | 1230 | net_dmaengine_get(); |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 1231 | dev_set_rx_mode(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | dev_activate(dev); |
Theodore Ts'o | 7bf2357 | 2012-07-04 21:23:25 -0400 | [diff] [blame] | 1233 | add_device_randomness(dev->dev_addr, dev->addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | } |
Jeff Garzik | bada339 | 2007-10-23 20:19:37 -0700 | [diff] [blame] | 1235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | return ret; |
| 1237 | } |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 1238 | |
| 1239 | /** |
| 1240 | * dev_open - prepare an interface for use. |
| 1241 | * @dev: device to open |
| 1242 | * |
| 1243 | * Takes a device from down to up state. The device's private open |
| 1244 | * function is invoked and then the multicast lists are loaded. Finally |
| 1245 | * the device is moved into the up state and a %NETDEV_UP message is |
| 1246 | * sent to the netdev notifier chain. |
| 1247 | * |
| 1248 | * Calling this function on an active interface is a nop. On a failure |
| 1249 | * a negative errno code is returned. |
| 1250 | */ |
| 1251 | int dev_open(struct net_device *dev) |
| 1252 | { |
| 1253 | int ret; |
| 1254 | |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 1255 | if (dev->flags & IFF_UP) |
| 1256 | return 0; |
| 1257 | |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 1258 | ret = __dev_open(dev); |
| 1259 | if (ret < 0) |
| 1260 | return ret; |
| 1261 | |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 1262 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); |
| 1263 | call_netdevice_notifiers(NETDEV_UP, dev); |
| 1264 | |
| 1265 | return ret; |
| 1266 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 1267 | EXPORT_SYMBOL(dev_open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1269 | static int __dev_close_many(struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1270 | { |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1271 | struct net_device *dev; |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 1272 | |
Ben Hutchings | e46b66b | 2008-05-08 02:53:17 -0700 | [diff] [blame] | 1273 | ASSERT_RTNL(); |
David S. Miller | 9d5010d | 2007-09-12 14:33:25 +0200 | [diff] [blame] | 1274 | might_sleep(); |
| 1275 | |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1276 | list_for_each_entry(dev, head, unreg_list) { |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1277 | call_netdevice_notifiers(NETDEV_GOING_DOWN, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1278 | |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1279 | clear_bit(__LINK_STATE_START, &dev->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1280 | |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1281 | /* Synchronize to scheduled poll. We cannot touch poll list, it |
| 1282 | * can be even on different cpu. So just clear netif_running(). |
| 1283 | * |
| 1284 | * dev->stop() will invoke napi_disable() on all of it's |
| 1285 | * napi_struct instances on this device. |
| 1286 | */ |
| 1287 | smp_mb__after_clear_bit(); /* Commit netif_running(). */ |
| 1288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1290 | dev_deactivate_many(head); |
| 1291 | |
| 1292 | list_for_each_entry(dev, head, unreg_list) { |
| 1293 | const struct net_device_ops *ops = dev->netdev_ops; |
| 1294 | |
| 1295 | /* |
| 1296 | * Call the device specific close. This cannot fail. |
| 1297 | * Only if device is UP |
| 1298 | * |
| 1299 | * We allow it to be called even after a DETACH hot-plug |
| 1300 | * event. |
| 1301 | */ |
| 1302 | if (ops->ndo_stop) |
| 1303 | ops->ndo_stop(dev); |
| 1304 | |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1305 | dev->flags &= ~IFF_UP; |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1306 | net_dmaengine_put(); |
| 1307 | } |
| 1308 | |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
| 1312 | static int __dev_close(struct net_device *dev) |
| 1313 | { |
Linus Torvalds | f87e6f4 | 2011-02-17 22:54:38 +0000 | [diff] [blame] | 1314 | int retval; |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1315 | LIST_HEAD(single); |
| 1316 | |
Neil Horman | ca99ca1 | 2013-02-05 08:05:43 +0000 | [diff] [blame] | 1317 | /* Temporarily disable netpoll until the interface is down */ |
dingtianhong | da6e378 | 2013-05-27 19:53:31 +0000 | [diff] [blame] | 1318 | netpoll_rx_disable(dev); |
Neil Horman | ca99ca1 | 2013-02-05 08:05:43 +0000 | [diff] [blame] | 1319 | |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1320 | list_add(&dev->unreg_list, &single); |
Linus Torvalds | f87e6f4 | 2011-02-17 22:54:38 +0000 | [diff] [blame] | 1321 | retval = __dev_close_many(&single); |
| 1322 | list_del(&single); |
Neil Horman | ca99ca1 | 2013-02-05 08:05:43 +0000 | [diff] [blame] | 1323 | |
| 1324 | netpoll_rx_enable(dev); |
Linus Torvalds | f87e6f4 | 2011-02-17 22:54:38 +0000 | [diff] [blame] | 1325 | return retval; |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Eric Dumazet | 3fbd875 | 2011-01-19 21:23:22 +0000 | [diff] [blame] | 1328 | static int dev_close_many(struct list_head *head) |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1329 | { |
| 1330 | struct net_device *dev, *tmp; |
| 1331 | LIST_HEAD(tmp_list); |
| 1332 | |
| 1333 | list_for_each_entry_safe(dev, tmp, head, unreg_list) |
| 1334 | if (!(dev->flags & IFF_UP)) |
| 1335 | list_move(&dev->unreg_list, &tmp_list); |
| 1336 | |
| 1337 | __dev_close_many(head); |
Matti Linnanvuori | d8b2a4d | 2008-02-12 23:10:11 -0800 | [diff] [blame] | 1338 | |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1339 | list_for_each_entry(dev, head, unreg_list) { |
| 1340 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); |
| 1341 | call_netdevice_notifiers(NETDEV_DOWN, dev); |
| 1342 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 1344 | /* rollback_registered_many needs the complete original list */ |
| 1345 | list_splice(&tmp_list, head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | return 0; |
| 1347 | } |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 1348 | |
| 1349 | /** |
| 1350 | * dev_close - shutdown an interface. |
| 1351 | * @dev: device to shutdown |
| 1352 | * |
| 1353 | * This function moves an active device into down state. A |
| 1354 | * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device |
| 1355 | * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier |
| 1356 | * chain. |
| 1357 | */ |
| 1358 | int dev_close(struct net_device *dev) |
| 1359 | { |
Eric Dumazet | e14a599 | 2011-05-10 12:26:06 -0700 | [diff] [blame] | 1360 | if (dev->flags & IFF_UP) { |
| 1361 | LIST_HEAD(single); |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 1362 | |
Neil Horman | ca99ca1 | 2013-02-05 08:05:43 +0000 | [diff] [blame] | 1363 | /* Block netpoll rx while the interface is going down */ |
dingtianhong | da6e378 | 2013-05-27 19:53:31 +0000 | [diff] [blame] | 1364 | netpoll_rx_disable(dev); |
Neil Horman | ca99ca1 | 2013-02-05 08:05:43 +0000 | [diff] [blame] | 1365 | |
Eric Dumazet | e14a599 | 2011-05-10 12:26:06 -0700 | [diff] [blame] | 1366 | list_add(&dev->unreg_list, &single); |
| 1367 | dev_close_many(&single); |
| 1368 | list_del(&single); |
Neil Horman | ca99ca1 | 2013-02-05 08:05:43 +0000 | [diff] [blame] | 1369 | |
| 1370 | netpoll_rx_enable(dev); |
Eric Dumazet | e14a599 | 2011-05-10 12:26:06 -0700 | [diff] [blame] | 1371 | } |
dingtianhong | da6e378 | 2013-05-27 19:53:31 +0000 | [diff] [blame] | 1372 | return 0; |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 1373 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 1374 | EXPORT_SYMBOL(dev_close); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | |
| 1376 | |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 1377 | /** |
| 1378 | * dev_disable_lro - disable Large Receive Offload on a device |
| 1379 | * @dev: device |
| 1380 | * |
| 1381 | * Disable Large Receive Offload (LRO) on a net device. Must be |
| 1382 | * called under RTNL. This is needed if received packets may be |
| 1383 | * forwarded to another interface. |
| 1384 | */ |
| 1385 | void dev_disable_lro(struct net_device *dev) |
| 1386 | { |
Neil Horman | f11970e | 2011-05-24 08:31:09 +0000 | [diff] [blame] | 1387 | /* |
| 1388 | * If we're trying to disable lro on a vlan device |
| 1389 | * use the underlying physical device instead |
| 1390 | */ |
| 1391 | if (is_vlan_dev(dev)) |
| 1392 | dev = vlan_dev_real_dev(dev); |
| 1393 | |
Michał Mirosław | bc5787c6 | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 1394 | dev->wanted_features &= ~NETIF_F_LRO; |
| 1395 | netdev_update_features(dev); |
Michał Mirosław | 2766051 | 2011-03-18 16:56:34 +0000 | [diff] [blame] | 1396 | |
Michał Mirosław | 22d5969 | 2011-04-21 12:42:15 +0000 | [diff] [blame] | 1397 | if (unlikely(dev->features & NETIF_F_LRO)) |
| 1398 | netdev_WARN(dev, "failed to disable LRO!\n"); |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 1399 | } |
| 1400 | EXPORT_SYMBOL(dev_disable_lro); |
| 1401 | |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 1402 | static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val, |
| 1403 | struct net_device *dev) |
| 1404 | { |
| 1405 | struct netdev_notifier_info info; |
| 1406 | |
| 1407 | netdev_notifier_info_init(&info, dev); |
| 1408 | return nb->notifier_call(nb, val, &info); |
| 1409 | } |
Ben Hutchings | 0187bdf | 2008-06-19 16:15:47 -0700 | [diff] [blame] | 1410 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1411 | static int dev_boot_phase = 1; |
| 1412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1413 | /** |
| 1414 | * register_netdevice_notifier - register a network notifier block |
| 1415 | * @nb: notifier |
| 1416 | * |
| 1417 | * Register a notifier to be called when network device events occur. |
| 1418 | * The notifier passed is linked into the kernel structures and must |
| 1419 | * not be reused until it has been unregistered. A negative errno code |
| 1420 | * is returned on a failure. |
| 1421 | * |
| 1422 | * When registered all registration and up events are replayed |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 1423 | * to the new notifier to allow device to have a race free |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | * view of the network device list. |
| 1425 | */ |
| 1426 | |
| 1427 | int register_netdevice_notifier(struct notifier_block *nb) |
| 1428 | { |
| 1429 | struct net_device *dev; |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1430 | struct net_device *last; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1431 | struct net *net; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | int err; |
| 1433 | |
| 1434 | rtnl_lock(); |
Alan Stern | f07d5b9 | 2006-05-09 15:23:03 -0700 | [diff] [blame] | 1435 | err = raw_notifier_chain_register(&netdev_chain, nb); |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1436 | if (err) |
| 1437 | goto unlock; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1438 | if (dev_boot_phase) |
| 1439 | goto unlock; |
| 1440 | for_each_net(net) { |
| 1441 | for_each_netdev(net, dev) { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 1442 | err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev); |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1443 | err = notifier_to_errno(err); |
| 1444 | if (err) |
| 1445 | goto rollback; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1447 | if (!(dev->flags & IFF_UP)) |
| 1448 | continue; |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1449 | |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 1450 | call_netdevice_notifier(nb, NETDEV_UP, dev); |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1451 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | } |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1453 | |
| 1454 | unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | rtnl_unlock(); |
| 1456 | return err; |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1457 | |
| 1458 | rollback: |
| 1459 | last = dev; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1460 | for_each_net(net) { |
| 1461 | for_each_netdev(net, dev) { |
| 1462 | if (dev == last) |
RongQing.Li | 8f89148 | 2011-11-30 23:43:07 -0500 | [diff] [blame] | 1463 | goto outroll; |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1464 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1465 | if (dev->flags & IFF_UP) { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 1466 | call_netdevice_notifier(nb, NETDEV_GOING_DOWN, |
| 1467 | dev); |
| 1468 | call_netdevice_notifier(nb, NETDEV_DOWN, dev); |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 1469 | } |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 1470 | call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev); |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1471 | } |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1472 | } |
Pavel Emelyanov | c67625a | 2007-11-14 15:53:16 -0800 | [diff] [blame] | 1473 | |
RongQing.Li | 8f89148 | 2011-11-30 23:43:07 -0500 | [diff] [blame] | 1474 | outroll: |
Pavel Emelyanov | c67625a | 2007-11-14 15:53:16 -0800 | [diff] [blame] | 1475 | raw_notifier_chain_unregister(&netdev_chain, nb); |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 1476 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 1478 | EXPORT_SYMBOL(register_netdevice_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
| 1480 | /** |
| 1481 | * unregister_netdevice_notifier - unregister a network notifier block |
| 1482 | * @nb: notifier |
| 1483 | * |
| 1484 | * Unregister a notifier previously registered by |
| 1485 | * register_netdevice_notifier(). The notifier is unlinked into the |
| 1486 | * kernel structures and may then be reused. A negative errno code |
| 1487 | * is returned on a failure. |
Eric W. Biederman | 7d3d43d | 2012-04-06 15:33:35 +0000 | [diff] [blame] | 1488 | * |
| 1489 | * After unregistering unregister and down device events are synthesized |
| 1490 | * for all devices on the device list to the removed notifier to remove |
| 1491 | * the need for special case cleanup code. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1492 | */ |
| 1493 | |
| 1494 | int unregister_netdevice_notifier(struct notifier_block *nb) |
| 1495 | { |
Eric W. Biederman | 7d3d43d | 2012-04-06 15:33:35 +0000 | [diff] [blame] | 1496 | struct net_device *dev; |
| 1497 | struct net *net; |
Herbert Xu | 9f51495 | 2006-03-25 01:24:25 -0800 | [diff] [blame] | 1498 | int err; |
| 1499 | |
| 1500 | rtnl_lock(); |
Alan Stern | f07d5b9 | 2006-05-09 15:23:03 -0700 | [diff] [blame] | 1501 | err = raw_notifier_chain_unregister(&netdev_chain, nb); |
Eric W. Biederman | 7d3d43d | 2012-04-06 15:33:35 +0000 | [diff] [blame] | 1502 | if (err) |
| 1503 | goto unlock; |
| 1504 | |
| 1505 | for_each_net(net) { |
| 1506 | for_each_netdev(net, dev) { |
| 1507 | if (dev->flags & IFF_UP) { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 1508 | call_netdevice_notifier(nb, NETDEV_GOING_DOWN, |
| 1509 | dev); |
| 1510 | call_netdevice_notifier(nb, NETDEV_DOWN, dev); |
Eric W. Biederman | 7d3d43d | 2012-04-06 15:33:35 +0000 | [diff] [blame] | 1511 | } |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 1512 | call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev); |
Eric W. Biederman | 7d3d43d | 2012-04-06 15:33:35 +0000 | [diff] [blame] | 1513 | } |
| 1514 | } |
| 1515 | unlock: |
Herbert Xu | 9f51495 | 2006-03-25 01:24:25 -0800 | [diff] [blame] | 1516 | rtnl_unlock(); |
| 1517 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 1519 | EXPORT_SYMBOL(unregister_netdevice_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | |
| 1521 | /** |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 1522 | * call_netdevice_notifiers_info - call all network notifier blocks |
| 1523 | * @val: value passed unmodified to notifier function |
| 1524 | * @dev: net_device pointer passed unmodified to notifier function |
| 1525 | * @info: notifier information data |
| 1526 | * |
| 1527 | * Call all network notifier blocks. Parameters and return value |
| 1528 | * are as for raw_notifier_call_chain(). |
| 1529 | */ |
| 1530 | |
| 1531 | int call_netdevice_notifiers_info(unsigned long val, struct net_device *dev, |
| 1532 | struct netdev_notifier_info *info) |
| 1533 | { |
| 1534 | ASSERT_RTNL(); |
| 1535 | netdev_notifier_info_init(info, dev); |
| 1536 | return raw_notifier_call_chain(&netdev_chain, val, info); |
| 1537 | } |
| 1538 | EXPORT_SYMBOL(call_netdevice_notifiers_info); |
| 1539 | |
| 1540 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | * call_netdevice_notifiers - call all network notifier blocks |
| 1542 | * @val: value passed unmodified to notifier function |
Randy Dunlap | c4ea43c | 2007-10-12 21:17:49 -0700 | [diff] [blame] | 1543 | * @dev: net_device pointer passed unmodified to notifier function |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | * |
| 1545 | * Call all network notifier blocks. Parameters and return value |
Alan Stern | f07d5b9 | 2006-05-09 15:23:03 -0700 | [diff] [blame] | 1546 | * are as for raw_notifier_call_chain(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1547 | */ |
| 1548 | |
Eric W. Biederman | ad7379d | 2007-09-16 15:33:32 -0700 | [diff] [blame] | 1549 | int call_netdevice_notifiers(unsigned long val, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | { |
Jiri Pirko | 351638e | 2013-05-28 01:30:21 +0000 | [diff] [blame] | 1551 | struct netdev_notifier_info info; |
| 1552 | |
| 1553 | return call_netdevice_notifiers_info(val, dev, &info); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | } |
stephen hemminger | edf947f | 2011-03-24 13:24:01 +0000 | [diff] [blame] | 1555 | EXPORT_SYMBOL(call_netdevice_notifiers); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1556 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1557 | static struct static_key netstamp_needed __read_mostly; |
Eric Dumazet | b90e579 | 2011-11-28 11:16:50 +0000 | [diff] [blame] | 1558 | #ifdef HAVE_JUMP_LABEL |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1559 | /* We are not allowed to call static_key_slow_dec() from irq context |
Eric Dumazet | b90e579 | 2011-11-28 11:16:50 +0000 | [diff] [blame] | 1560 | * If net_disable_timestamp() is called from irq context, defer the |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1561 | * static_key_slow_dec() calls. |
Eric Dumazet | b90e579 | 2011-11-28 11:16:50 +0000 | [diff] [blame] | 1562 | */ |
| 1563 | static atomic_t netstamp_needed_deferred; |
| 1564 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | |
| 1566 | void net_enable_timestamp(void) |
| 1567 | { |
Eric Dumazet | b90e579 | 2011-11-28 11:16:50 +0000 | [diff] [blame] | 1568 | #ifdef HAVE_JUMP_LABEL |
| 1569 | int deferred = atomic_xchg(&netstamp_needed_deferred, 0); |
| 1570 | |
| 1571 | if (deferred) { |
| 1572 | while (--deferred) |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1573 | static_key_slow_dec(&netstamp_needed); |
Eric Dumazet | b90e579 | 2011-11-28 11:16:50 +0000 | [diff] [blame] | 1574 | return; |
| 1575 | } |
| 1576 | #endif |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1577 | static_key_slow_inc(&netstamp_needed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 1579 | EXPORT_SYMBOL(net_enable_timestamp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | |
| 1581 | void net_disable_timestamp(void) |
| 1582 | { |
Eric Dumazet | b90e579 | 2011-11-28 11:16:50 +0000 | [diff] [blame] | 1583 | #ifdef HAVE_JUMP_LABEL |
| 1584 | if (in_interrupt()) { |
| 1585 | atomic_inc(&netstamp_needed_deferred); |
| 1586 | return; |
| 1587 | } |
| 1588 | #endif |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1589 | static_key_slow_dec(&netstamp_needed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 1591 | EXPORT_SYMBOL(net_disable_timestamp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | |
Eric Dumazet | 3b098e2 | 2010-05-15 23:57:10 -0700 | [diff] [blame] | 1593 | static inline void net_timestamp_set(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | { |
Eric Dumazet | 588f033 | 2011-11-15 04:12:55 +0000 | [diff] [blame] | 1595 | skb->tstamp.tv64 = 0; |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1596 | if (static_key_false(&netstamp_needed)) |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 1597 | __net_timestamp(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | } |
| 1599 | |
Eric Dumazet | 588f033 | 2011-11-15 04:12:55 +0000 | [diff] [blame] | 1600 | #define net_timestamp_check(COND, SKB) \ |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 1601 | if (static_key_false(&netstamp_needed)) { \ |
Eric Dumazet | 588f033 | 2011-11-15 04:12:55 +0000 | [diff] [blame] | 1602 | if ((COND) && !(SKB)->tstamp.tv64) \ |
| 1603 | __net_timestamp(SKB); \ |
| 1604 | } \ |
Eric Dumazet | 3b098e2 | 2010-05-15 23:57:10 -0700 | [diff] [blame] | 1605 | |
Daniel Lezcano | 79b569f | 2011-03-30 02:42:17 -0700 | [diff] [blame] | 1606 | static inline bool is_skb_forwardable(struct net_device *dev, |
| 1607 | struct sk_buff *skb) |
| 1608 | { |
| 1609 | unsigned int len; |
| 1610 | |
| 1611 | if (!(dev->flags & IFF_UP)) |
| 1612 | return false; |
| 1613 | |
| 1614 | len = dev->mtu + dev->hard_header_len + VLAN_HLEN; |
| 1615 | if (skb->len <= len) |
| 1616 | return true; |
| 1617 | |
| 1618 | /* if TSO is enabled, we don't care about the length as the packet |
| 1619 | * could be forwarded without being segmented before |
| 1620 | */ |
| 1621 | if (skb_is_gso(skb)) |
| 1622 | return true; |
| 1623 | |
| 1624 | return false; |
| 1625 | } |
| 1626 | |
Arnd Bergmann | 4454096 | 2009-11-26 06:07:08 +0000 | [diff] [blame] | 1627 | /** |
| 1628 | * dev_forward_skb - loopback an skb to another netif |
| 1629 | * |
| 1630 | * @dev: destination network device |
| 1631 | * @skb: buffer to forward |
| 1632 | * |
| 1633 | * return values: |
| 1634 | * NET_RX_SUCCESS (no congestion) |
Eric Dumazet | 6ec8256 | 2010-05-06 00:53:53 -0700 | [diff] [blame] | 1635 | * NET_RX_DROP (packet was dropped, but freed) |
Arnd Bergmann | 4454096 | 2009-11-26 06:07:08 +0000 | [diff] [blame] | 1636 | * |
| 1637 | * dev_forward_skb can be used for injecting an skb from the |
| 1638 | * start_xmit function of one device into the receive queue |
| 1639 | * of another device. |
| 1640 | * |
| 1641 | * The receiving device may be in another namespace, so |
| 1642 | * we have to clear all information in the skb that could |
| 1643 | * impact namespace isolation. |
| 1644 | */ |
| 1645 | int dev_forward_skb(struct net_device *dev, struct sk_buff *skb) |
| 1646 | { |
Michael S. Tsirkin | 48c8301 | 2011-08-31 08:03:29 +0000 | [diff] [blame] | 1647 | if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { |
| 1648 | if (skb_copy_ubufs(skb, GFP_ATOMIC)) { |
| 1649 | atomic_long_inc(&dev->rx_dropped); |
| 1650 | kfree_skb(skb); |
| 1651 | return NET_RX_DROP; |
| 1652 | } |
| 1653 | } |
| 1654 | |
Daniel Lezcano | 79b569f | 2011-03-30 02:42:17 -0700 | [diff] [blame] | 1655 | if (unlikely(!is_skb_forwardable(dev, skb))) { |
Eric Dumazet | caf586e | 2010-09-30 21:06:55 +0000 | [diff] [blame] | 1656 | atomic_long_inc(&dev->rx_dropped); |
Eric Dumazet | 6ec8256 | 2010-05-06 00:53:53 -0700 | [diff] [blame] | 1657 | kfree_skb(skb); |
Arnd Bergmann | 4454096 | 2009-11-26 06:07:08 +0000 | [diff] [blame] | 1658 | return NET_RX_DROP; |
Eric Dumazet | 6ec8256 | 2010-05-06 00:53:53 -0700 | [diff] [blame] | 1659 | } |
Nicolas Dichtel | 621e84d | 2013-06-26 16:11:27 +0200 | [diff] [blame] | 1660 | skb_scrub_packet(skb); |
Arnd Bergmann | 4454096 | 2009-11-26 06:07:08 +0000 | [diff] [blame] | 1661 | skb->protocol = eth_type_trans(skb, dev); |
Arnd Bergmann | 4454096 | 2009-11-26 06:07:08 +0000 | [diff] [blame] | 1662 | return netif_rx(skb); |
| 1663 | } |
| 1664 | EXPORT_SYMBOL_GPL(dev_forward_skb); |
| 1665 | |
Changli Gao | 71d9dec | 2010-12-15 19:57:25 +0000 | [diff] [blame] | 1666 | static inline int deliver_skb(struct sk_buff *skb, |
| 1667 | struct packet_type *pt_prev, |
| 1668 | struct net_device *orig_dev) |
| 1669 | { |
Michael S. Tsirkin | 1080e51 | 2012-07-20 09:23:17 +0000 | [diff] [blame] | 1670 | if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) |
| 1671 | return -ENOMEM; |
Changli Gao | 71d9dec | 2010-12-15 19:57:25 +0000 | [diff] [blame] | 1672 | atomic_inc(&skb->users); |
| 1673 | return pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
| 1674 | } |
| 1675 | |
Eric Leblond | c0de08d | 2012-08-16 22:02:58 +0000 | [diff] [blame] | 1676 | static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb) |
| 1677 | { |
Eric Leblond | a3d744e | 2012-11-06 02:10:10 +0000 | [diff] [blame] | 1678 | if (!ptype->af_packet_priv || !skb->sk) |
Eric Leblond | c0de08d | 2012-08-16 22:02:58 +0000 | [diff] [blame] | 1679 | return false; |
| 1680 | |
| 1681 | if (ptype->id_match) |
| 1682 | return ptype->id_match(ptype, skb->sk); |
| 1683 | else if ((struct sock *)ptype->af_packet_priv == skb->sk) |
| 1684 | return true; |
| 1685 | |
| 1686 | return false; |
| 1687 | } |
| 1688 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | /* |
| 1690 | * Support routine. Sends outgoing frames to any network |
| 1691 | * taps currently in use. |
| 1692 | */ |
| 1693 | |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 1694 | static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | { |
| 1696 | struct packet_type *ptype; |
Changli Gao | 71d9dec | 2010-12-15 19:57:25 +0000 | [diff] [blame] | 1697 | struct sk_buff *skb2 = NULL; |
| 1698 | struct packet_type *pt_prev = NULL; |
Patrick McHardy | a61bbcf | 2005-08-14 17:24:31 -0700 | [diff] [blame] | 1699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1700 | rcu_read_lock(); |
| 1701 | list_for_each_entry_rcu(ptype, &ptype_all, list) { |
| 1702 | /* Never send packets back to the socket |
| 1703 | * they originated from - MvS (miquels@drinkel.ow.org) |
| 1704 | */ |
| 1705 | if ((ptype->dev == dev || !ptype->dev) && |
Eric Leblond | c0de08d | 2012-08-16 22:02:58 +0000 | [diff] [blame] | 1706 | (!skb_loop_sk(ptype, skb))) { |
Changli Gao | 71d9dec | 2010-12-15 19:57:25 +0000 | [diff] [blame] | 1707 | if (pt_prev) { |
| 1708 | deliver_skb(skb2, pt_prev, skb->dev); |
| 1709 | pt_prev = ptype; |
| 1710 | continue; |
| 1711 | } |
| 1712 | |
| 1713 | skb2 = skb_clone(skb, GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | if (!skb2) |
| 1715 | break; |
| 1716 | |
Eric Dumazet | 7097818 | 2010-12-20 21:22:51 +0000 | [diff] [blame] | 1717 | net_timestamp_set(skb2); |
| 1718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | /* skb->nh should be correctly |
| 1720 | set by sender, so that the second statement is |
| 1721 | just protection against buggy protocols. |
| 1722 | */ |
Arnaldo Carvalho de Melo | 459a98e | 2007-03-19 15:30:44 -0700 | [diff] [blame] | 1723 | skb_reset_mac_header(skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | |
Arnaldo Carvalho de Melo | d56f90a | 2007-04-10 20:50:43 -0700 | [diff] [blame] | 1725 | if (skb_network_header(skb2) < skb2->data || |
Simon Horman | ced14f6 | 2013-05-28 20:34:25 +0000 | [diff] [blame] | 1726 | skb_network_header(skb2) > skb_tail_pointer(skb2)) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 1727 | net_crit_ratelimited("protocol %04x is buggy, dev %s\n", |
| 1728 | ntohs(skb2->protocol), |
| 1729 | dev->name); |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 1730 | skb_reset_network_header(skb2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
Arnaldo Carvalho de Melo | b0e380b | 2007-04-10 21:21:55 -0700 | [diff] [blame] | 1733 | skb2->transport_header = skb2->network_header; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | skb2->pkt_type = PACKET_OUTGOING; |
Changli Gao | 71d9dec | 2010-12-15 19:57:25 +0000 | [diff] [blame] | 1735 | pt_prev = ptype; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | } |
| 1737 | } |
Changli Gao | 71d9dec | 2010-12-15 19:57:25 +0000 | [diff] [blame] | 1738 | if (pt_prev) |
| 1739 | pt_prev->func(skb2, skb->dev, pt_prev, skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | rcu_read_unlock(); |
| 1741 | } |
| 1742 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 1743 | /** |
| 1744 | * netif_setup_tc - Handle tc mappings on real_num_tx_queues change |
John Fastabend | 4f57c08 | 2011-01-17 08:06:04 +0000 | [diff] [blame] | 1745 | * @dev: Network device |
| 1746 | * @txq: number of queues available |
| 1747 | * |
| 1748 | * If real_num_tx_queues is changed the tc mappings may no longer be |
| 1749 | * valid. To resolve this verify the tc mapping remains valid and if |
| 1750 | * not NULL the mapping. With no priorities mapping to this |
| 1751 | * offset/count pair it will no longer be used. In the worst case TC0 |
| 1752 | * is invalid nothing can be done so disable priority mappings. If is |
| 1753 | * expected that drivers will fix this mapping if they can before |
| 1754 | * calling netif_set_real_num_tx_queues. |
| 1755 | */ |
Eric Dumazet | bb134d2 | 2011-01-20 19:18:08 +0000 | [diff] [blame] | 1756 | static void netif_setup_tc(struct net_device *dev, unsigned int txq) |
John Fastabend | 4f57c08 | 2011-01-17 08:06:04 +0000 | [diff] [blame] | 1757 | { |
| 1758 | int i; |
| 1759 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; |
| 1760 | |
| 1761 | /* If TC0 is invalidated disable TC mapping */ |
| 1762 | if (tc->offset + tc->count > txq) { |
Joe Perches | 7b6cd1c | 2012-02-01 10:54:43 +0000 | [diff] [blame] | 1763 | pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n"); |
John Fastabend | 4f57c08 | 2011-01-17 08:06:04 +0000 | [diff] [blame] | 1764 | dev->num_tc = 0; |
| 1765 | return; |
| 1766 | } |
| 1767 | |
| 1768 | /* Invalidated prio to tc mappings set to TC0 */ |
| 1769 | for (i = 1; i < TC_BITMASK + 1; i++) { |
| 1770 | int q = netdev_get_prio_tc_map(dev, i); |
| 1771 | |
| 1772 | tc = &dev->tc_to_txq[q]; |
| 1773 | if (tc->offset + tc->count > txq) { |
Joe Perches | 7b6cd1c | 2012-02-01 10:54:43 +0000 | [diff] [blame] | 1774 | pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n", |
| 1775 | i, q); |
John Fastabend | 4f57c08 | 2011-01-17 08:06:04 +0000 | [diff] [blame] | 1776 | netdev_set_prio_tc_map(dev, i, 0); |
| 1777 | } |
| 1778 | } |
| 1779 | } |
| 1780 | |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1781 | #ifdef CONFIG_XPS |
| 1782 | static DEFINE_MUTEX(xps_map_mutex); |
| 1783 | #define xmap_dereference(P) \ |
| 1784 | rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex)) |
| 1785 | |
Alexander Duyck | 10cdc3f | 2013-01-10 08:57:17 +0000 | [diff] [blame] | 1786 | static struct xps_map *remove_xps_queue(struct xps_dev_maps *dev_maps, |
| 1787 | int cpu, u16 index) |
| 1788 | { |
| 1789 | struct xps_map *map = NULL; |
| 1790 | int pos; |
| 1791 | |
| 1792 | if (dev_maps) |
| 1793 | map = xmap_dereference(dev_maps->cpu_map[cpu]); |
| 1794 | |
| 1795 | for (pos = 0; map && pos < map->len; pos++) { |
| 1796 | if (map->queues[pos] == index) { |
| 1797 | if (map->len > 1) { |
| 1798 | map->queues[pos] = map->queues[--map->len]; |
| 1799 | } else { |
| 1800 | RCU_INIT_POINTER(dev_maps->cpu_map[cpu], NULL); |
| 1801 | kfree_rcu(map, rcu); |
| 1802 | map = NULL; |
| 1803 | } |
| 1804 | break; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | return map; |
| 1809 | } |
| 1810 | |
Alexander Duyck | 024e967 | 2013-01-10 08:57:46 +0000 | [diff] [blame] | 1811 | static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index) |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1812 | { |
| 1813 | struct xps_dev_maps *dev_maps; |
Alexander Duyck | 024e967 | 2013-01-10 08:57:46 +0000 | [diff] [blame] | 1814 | int cpu, i; |
Alexander Duyck | 10cdc3f | 2013-01-10 08:57:17 +0000 | [diff] [blame] | 1815 | bool active = false; |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1816 | |
| 1817 | mutex_lock(&xps_map_mutex); |
| 1818 | dev_maps = xmap_dereference(dev->xps_maps); |
| 1819 | |
| 1820 | if (!dev_maps) |
| 1821 | goto out_no_maps; |
| 1822 | |
Alexander Duyck | 10cdc3f | 2013-01-10 08:57:17 +0000 | [diff] [blame] | 1823 | for_each_possible_cpu(cpu) { |
Alexander Duyck | 024e967 | 2013-01-10 08:57:46 +0000 | [diff] [blame] | 1824 | for (i = index; i < dev->num_tx_queues; i++) { |
| 1825 | if (!remove_xps_queue(dev_maps, cpu, i)) |
| 1826 | break; |
| 1827 | } |
| 1828 | if (i == dev->num_tx_queues) |
Alexander Duyck | 10cdc3f | 2013-01-10 08:57:17 +0000 | [diff] [blame] | 1829 | active = true; |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1830 | } |
| 1831 | |
Alexander Duyck | 10cdc3f | 2013-01-10 08:57:17 +0000 | [diff] [blame] | 1832 | if (!active) { |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1833 | RCU_INIT_POINTER(dev->xps_maps, NULL); |
| 1834 | kfree_rcu(dev_maps, rcu); |
| 1835 | } |
| 1836 | |
Alexander Duyck | 024e967 | 2013-01-10 08:57:46 +0000 | [diff] [blame] | 1837 | for (i = index; i < dev->num_tx_queues; i++) |
| 1838 | netdev_queue_numa_node_write(netdev_get_tx_queue(dev, i), |
| 1839 | NUMA_NO_NODE); |
| 1840 | |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1841 | out_no_maps: |
| 1842 | mutex_unlock(&xps_map_mutex); |
| 1843 | } |
| 1844 | |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1845 | static struct xps_map *expand_xps_map(struct xps_map *map, |
| 1846 | int cpu, u16 index) |
| 1847 | { |
| 1848 | struct xps_map *new_map; |
| 1849 | int alloc_len = XPS_MIN_MAP_ALLOC; |
| 1850 | int i, pos; |
| 1851 | |
| 1852 | for (pos = 0; map && pos < map->len; pos++) { |
| 1853 | if (map->queues[pos] != index) |
| 1854 | continue; |
| 1855 | return map; |
| 1856 | } |
| 1857 | |
| 1858 | /* Need to add queue to this CPU's existing map */ |
| 1859 | if (map) { |
| 1860 | if (pos < map->alloc_len) |
| 1861 | return map; |
| 1862 | |
| 1863 | alloc_len = map->alloc_len * 2; |
| 1864 | } |
| 1865 | |
| 1866 | /* Need to allocate new map to store queue on this CPU's map */ |
| 1867 | new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL, |
| 1868 | cpu_to_node(cpu)); |
| 1869 | if (!new_map) |
| 1870 | return NULL; |
| 1871 | |
| 1872 | for (i = 0; i < pos; i++) |
| 1873 | new_map->queues[i] = map->queues[i]; |
| 1874 | new_map->alloc_len = alloc_len; |
| 1875 | new_map->len = pos; |
| 1876 | |
| 1877 | return new_map; |
| 1878 | } |
| 1879 | |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1880 | int netif_set_xps_queue(struct net_device *dev, struct cpumask *mask, u16 index) |
| 1881 | { |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1882 | struct xps_dev_maps *dev_maps, *new_dev_maps = NULL; |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1883 | struct xps_map *map, *new_map; |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1884 | int maps_sz = max_t(unsigned int, XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES); |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1885 | int cpu, numa_node_id = -2; |
| 1886 | bool active = false; |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1887 | |
| 1888 | mutex_lock(&xps_map_mutex); |
| 1889 | |
| 1890 | dev_maps = xmap_dereference(dev->xps_maps); |
| 1891 | |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1892 | /* allocate memory for queue storage */ |
| 1893 | for_each_online_cpu(cpu) { |
| 1894 | if (!cpumask_test_cpu(cpu, mask)) |
| 1895 | continue; |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1896 | |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1897 | if (!new_dev_maps) |
| 1898 | new_dev_maps = kzalloc(maps_sz, GFP_KERNEL); |
Alexander Duyck | 2bb60cb | 2013-02-22 06:38:44 +0000 | [diff] [blame] | 1899 | if (!new_dev_maps) { |
| 1900 | mutex_unlock(&xps_map_mutex); |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1901 | return -ENOMEM; |
Alexander Duyck | 2bb60cb | 2013-02-22 06:38:44 +0000 | [diff] [blame] | 1902 | } |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1903 | |
| 1904 | map = dev_maps ? xmap_dereference(dev_maps->cpu_map[cpu]) : |
| 1905 | NULL; |
| 1906 | |
| 1907 | map = expand_xps_map(map, cpu, index); |
| 1908 | if (!map) |
| 1909 | goto error; |
| 1910 | |
| 1911 | RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], map); |
| 1912 | } |
| 1913 | |
| 1914 | if (!new_dev_maps) |
| 1915 | goto out_no_new_maps; |
| 1916 | |
| 1917 | for_each_possible_cpu(cpu) { |
| 1918 | if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu)) { |
| 1919 | /* add queue to CPU maps */ |
| 1920 | int pos = 0; |
| 1921 | |
| 1922 | map = xmap_dereference(new_dev_maps->cpu_map[cpu]); |
| 1923 | while ((pos < map->len) && (map->queues[pos] != index)) |
| 1924 | pos++; |
| 1925 | |
| 1926 | if (pos == map->len) |
| 1927 | map->queues[map->len++] = index; |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1928 | #ifdef CONFIG_NUMA |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1929 | if (numa_node_id == -2) |
| 1930 | numa_node_id = cpu_to_node(cpu); |
| 1931 | else if (numa_node_id != cpu_to_node(cpu)) |
| 1932 | numa_node_id = -1; |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1933 | #endif |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1934 | } else if (dev_maps) { |
| 1935 | /* fill in the new device map from the old device map */ |
| 1936 | map = xmap_dereference(dev_maps->cpu_map[cpu]); |
| 1937 | RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], map); |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1938 | } |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1939 | |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1942 | rcu_assign_pointer(dev->xps_maps, new_dev_maps); |
| 1943 | |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1944 | /* Cleanup old maps */ |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1945 | if (dev_maps) { |
| 1946 | for_each_possible_cpu(cpu) { |
| 1947 | new_map = xmap_dereference(new_dev_maps->cpu_map[cpu]); |
| 1948 | map = xmap_dereference(dev_maps->cpu_map[cpu]); |
| 1949 | if (map && map != new_map) |
| 1950 | kfree_rcu(map, rcu); |
| 1951 | } |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1952 | |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1953 | kfree_rcu(dev_maps, rcu); |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1954 | } |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1955 | |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1956 | dev_maps = new_dev_maps; |
| 1957 | active = true; |
| 1958 | |
| 1959 | out_no_new_maps: |
| 1960 | /* update Tx queue numa node */ |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1961 | netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index), |
| 1962 | (numa_node_id >= 0) ? numa_node_id : |
| 1963 | NUMA_NO_NODE); |
| 1964 | |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1965 | if (!dev_maps) |
| 1966 | goto out_no_maps; |
| 1967 | |
| 1968 | /* removes queue from unused CPUs */ |
| 1969 | for_each_possible_cpu(cpu) { |
| 1970 | if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu)) |
| 1971 | continue; |
| 1972 | |
| 1973 | if (remove_xps_queue(dev_maps, cpu, index)) |
| 1974 | active = true; |
| 1975 | } |
| 1976 | |
| 1977 | /* free map if not active */ |
| 1978 | if (!active) { |
| 1979 | RCU_INIT_POINTER(dev->xps_maps, NULL); |
| 1980 | kfree_rcu(dev_maps, rcu); |
| 1981 | } |
| 1982 | |
| 1983 | out_no_maps: |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1984 | mutex_unlock(&xps_map_mutex); |
| 1985 | |
| 1986 | return 0; |
| 1987 | error: |
Alexander Duyck | 01c5f86 | 2013-01-10 08:57:35 +0000 | [diff] [blame] | 1988 | /* remove any maps that we added */ |
| 1989 | for_each_possible_cpu(cpu) { |
| 1990 | new_map = xmap_dereference(new_dev_maps->cpu_map[cpu]); |
| 1991 | map = dev_maps ? xmap_dereference(dev_maps->cpu_map[cpu]) : |
| 1992 | NULL; |
| 1993 | if (new_map && new_map != map) |
| 1994 | kfree(new_map); |
| 1995 | } |
| 1996 | |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1997 | mutex_unlock(&xps_map_mutex); |
| 1998 | |
Alexander Duyck | 537c00d | 2013-01-10 08:57:02 +0000 | [diff] [blame] | 1999 | kfree(new_dev_maps); |
| 2000 | return -ENOMEM; |
| 2001 | } |
| 2002 | EXPORT_SYMBOL(netif_set_xps_queue); |
| 2003 | |
| 2004 | #endif |
John Fastabend | f0796d5 | 2010-07-01 13:21:57 +0000 | [diff] [blame] | 2005 | /* |
| 2006 | * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues |
| 2007 | * greater then real_num_tx_queues stale skbs on the qdisc must be flushed. |
| 2008 | */ |
Tom Herbert | e648493 | 2010-10-18 18:04:39 +0000 | [diff] [blame] | 2009 | int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq) |
John Fastabend | f0796d5 | 2010-07-01 13:21:57 +0000 | [diff] [blame] | 2010 | { |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 2011 | int rc; |
| 2012 | |
Tom Herbert | e648493 | 2010-10-18 18:04:39 +0000 | [diff] [blame] | 2013 | if (txq < 1 || txq > dev->num_tx_queues) |
| 2014 | return -EINVAL; |
John Fastabend | f0796d5 | 2010-07-01 13:21:57 +0000 | [diff] [blame] | 2015 | |
Ben Hutchings | 5c56580 | 2011-02-15 19:39:21 +0000 | [diff] [blame] | 2016 | if (dev->reg_state == NETREG_REGISTERED || |
| 2017 | dev->reg_state == NETREG_UNREGISTERING) { |
Tom Herbert | e648493 | 2010-10-18 18:04:39 +0000 | [diff] [blame] | 2018 | ASSERT_RTNL(); |
| 2019 | |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 2020 | rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues, |
| 2021 | txq); |
Tom Herbert | bf26414 | 2010-11-26 08:36:09 +0000 | [diff] [blame] | 2022 | if (rc) |
| 2023 | return rc; |
| 2024 | |
John Fastabend | 4f57c08 | 2011-01-17 08:06:04 +0000 | [diff] [blame] | 2025 | if (dev->num_tc) |
| 2026 | netif_setup_tc(dev, txq); |
| 2027 | |
Alexander Duyck | 024e967 | 2013-01-10 08:57:46 +0000 | [diff] [blame] | 2028 | if (txq < dev->real_num_tx_queues) { |
Tom Herbert | e648493 | 2010-10-18 18:04:39 +0000 | [diff] [blame] | 2029 | qdisc_reset_all_tx_gt(dev, txq); |
Alexander Duyck | 024e967 | 2013-01-10 08:57:46 +0000 | [diff] [blame] | 2030 | #ifdef CONFIG_XPS |
| 2031 | netif_reset_xps_queues_gt(dev, txq); |
| 2032 | #endif |
| 2033 | } |
John Fastabend | f0796d5 | 2010-07-01 13:21:57 +0000 | [diff] [blame] | 2034 | } |
Tom Herbert | e648493 | 2010-10-18 18:04:39 +0000 | [diff] [blame] | 2035 | |
| 2036 | dev->real_num_tx_queues = txq; |
| 2037 | return 0; |
John Fastabend | f0796d5 | 2010-07-01 13:21:57 +0000 | [diff] [blame] | 2038 | } |
| 2039 | EXPORT_SYMBOL(netif_set_real_num_tx_queues); |
Denis Vlasenko | 5607943 | 2006-03-29 15:57:29 -0800 | [diff] [blame] | 2040 | |
Ben Hutchings | 62fe0b4 | 2010-09-27 08:24:33 +0000 | [diff] [blame] | 2041 | #ifdef CONFIG_RPS |
| 2042 | /** |
| 2043 | * netif_set_real_num_rx_queues - set actual number of RX queues used |
| 2044 | * @dev: Network device |
| 2045 | * @rxq: Actual number of RX queues |
| 2046 | * |
| 2047 | * This must be called either with the rtnl_lock held or before |
| 2048 | * registration of the net device. Returns 0 on success, or a |
Ben Hutchings | 4e7f795 | 2010-10-08 10:33:39 -0700 | [diff] [blame] | 2049 | * negative error code. If called before registration, it always |
| 2050 | * succeeds. |
Ben Hutchings | 62fe0b4 | 2010-09-27 08:24:33 +0000 | [diff] [blame] | 2051 | */ |
| 2052 | int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq) |
| 2053 | { |
| 2054 | int rc; |
| 2055 | |
Tom Herbert | bd25fa7 | 2010-10-18 18:00:16 +0000 | [diff] [blame] | 2056 | if (rxq < 1 || rxq > dev->num_rx_queues) |
| 2057 | return -EINVAL; |
| 2058 | |
Ben Hutchings | 62fe0b4 | 2010-09-27 08:24:33 +0000 | [diff] [blame] | 2059 | if (dev->reg_state == NETREG_REGISTERED) { |
| 2060 | ASSERT_RTNL(); |
| 2061 | |
Ben Hutchings | 62fe0b4 | 2010-09-27 08:24:33 +0000 | [diff] [blame] | 2062 | rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues, |
| 2063 | rxq); |
| 2064 | if (rc) |
| 2065 | return rc; |
Ben Hutchings | 62fe0b4 | 2010-09-27 08:24:33 +0000 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | dev->real_num_rx_queues = rxq; |
| 2069 | return 0; |
| 2070 | } |
| 2071 | EXPORT_SYMBOL(netif_set_real_num_rx_queues); |
| 2072 | #endif |
| 2073 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 2074 | /** |
| 2075 | * netif_get_num_default_rss_queues - default number of RSS queues |
Yuval Mintz | 16917b8 | 2012-07-01 03:18:50 +0000 | [diff] [blame] | 2076 | * |
| 2077 | * This routine should set an upper limit on the number of RSS queues |
| 2078 | * used by default by multiqueue devices. |
| 2079 | */ |
Ben Hutchings | a55b138 | 2012-07-10 10:54:38 +0000 | [diff] [blame] | 2080 | int netif_get_num_default_rss_queues(void) |
Yuval Mintz | 16917b8 | 2012-07-01 03:18:50 +0000 | [diff] [blame] | 2081 | { |
| 2082 | return min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus()); |
| 2083 | } |
| 2084 | EXPORT_SYMBOL(netif_get_num_default_rss_queues); |
| 2085 | |
Jarek Poplawski | def82a1 | 2008-08-17 21:54:43 -0700 | [diff] [blame] | 2086 | static inline void __netif_reschedule(struct Qdisc *q) |
| 2087 | { |
| 2088 | struct softnet_data *sd; |
| 2089 | unsigned long flags; |
| 2090 | |
| 2091 | local_irq_save(flags); |
| 2092 | sd = &__get_cpu_var(softnet_data); |
Changli Gao | a9cbd58 | 2010-04-26 23:06:24 +0000 | [diff] [blame] | 2093 | q->next_sched = NULL; |
| 2094 | *sd->output_queue_tailp = q; |
| 2095 | sd->output_queue_tailp = &q->next_sched; |
Jarek Poplawski | def82a1 | 2008-08-17 21:54:43 -0700 | [diff] [blame] | 2096 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
| 2097 | local_irq_restore(flags); |
| 2098 | } |
| 2099 | |
David S. Miller | 37437bb | 2008-07-16 02:15:04 -0700 | [diff] [blame] | 2100 | void __netif_schedule(struct Qdisc *q) |
Denis Vlasenko | 5607943 | 2006-03-29 15:57:29 -0800 | [diff] [blame] | 2101 | { |
Jarek Poplawski | def82a1 | 2008-08-17 21:54:43 -0700 | [diff] [blame] | 2102 | if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state)) |
| 2103 | __netif_reschedule(q); |
Denis Vlasenko | 5607943 | 2006-03-29 15:57:29 -0800 | [diff] [blame] | 2104 | } |
| 2105 | EXPORT_SYMBOL(__netif_schedule); |
| 2106 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 2107 | void dev_kfree_skb_irq(struct sk_buff *skb) |
Denis Vlasenko | 5607943 | 2006-03-29 15:57:29 -0800 | [diff] [blame] | 2108 | { |
David S. Miller | 3578b0c | 2010-08-03 00:24:04 -0700 | [diff] [blame] | 2109 | if (atomic_dec_and_test(&skb->users)) { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 2110 | struct softnet_data *sd; |
| 2111 | unsigned long flags; |
Denis Vlasenko | 5607943 | 2006-03-29 15:57:29 -0800 | [diff] [blame] | 2112 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 2113 | local_irq_save(flags); |
| 2114 | sd = &__get_cpu_var(softnet_data); |
| 2115 | skb->next = sd->completion_queue; |
| 2116 | sd->completion_queue = skb; |
| 2117 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
| 2118 | local_irq_restore(flags); |
| 2119 | } |
Denis Vlasenko | 5607943 | 2006-03-29 15:57:29 -0800 | [diff] [blame] | 2120 | } |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 2121 | EXPORT_SYMBOL(dev_kfree_skb_irq); |
Denis Vlasenko | 5607943 | 2006-03-29 15:57:29 -0800 | [diff] [blame] | 2122 | |
| 2123 | void dev_kfree_skb_any(struct sk_buff *skb) |
| 2124 | { |
| 2125 | if (in_irq() || irqs_disabled()) |
| 2126 | dev_kfree_skb_irq(skb); |
| 2127 | else |
| 2128 | dev_kfree_skb(skb); |
| 2129 | } |
| 2130 | EXPORT_SYMBOL(dev_kfree_skb_any); |
| 2131 | |
| 2132 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 2133 | /** |
| 2134 | * netif_device_detach - mark device as removed |
| 2135 | * @dev: network device |
| 2136 | * |
| 2137 | * Mark device as removed from system and therefore no longer available. |
| 2138 | */ |
Denis Vlasenko | 5607943 | 2006-03-29 15:57:29 -0800 | [diff] [blame] | 2139 | void netif_device_detach(struct net_device *dev) |
| 2140 | { |
| 2141 | if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) && |
| 2142 | netif_running(dev)) { |
Alexander Duyck | d543103 | 2009-04-08 13:15:22 +0000 | [diff] [blame] | 2143 | netif_tx_stop_all_queues(dev); |
Denis Vlasenko | 5607943 | 2006-03-29 15:57:29 -0800 | [diff] [blame] | 2144 | } |
| 2145 | } |
| 2146 | EXPORT_SYMBOL(netif_device_detach); |
| 2147 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 2148 | /** |
| 2149 | * netif_device_attach - mark device as attached |
| 2150 | * @dev: network device |
| 2151 | * |
| 2152 | * Mark device as attached from system and restart if needed. |
| 2153 | */ |
Denis Vlasenko | 5607943 | 2006-03-29 15:57:29 -0800 | [diff] [blame] | 2154 | void netif_device_attach(struct net_device *dev) |
| 2155 | { |
| 2156 | if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) && |
| 2157 | netif_running(dev)) { |
Alexander Duyck | d543103 | 2009-04-08 13:15:22 +0000 | [diff] [blame] | 2158 | netif_tx_wake_all_queues(dev); |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 2159 | __netdev_watchdog_up(dev); |
Denis Vlasenko | 5607943 | 2006-03-29 15:57:29 -0800 | [diff] [blame] | 2160 | } |
| 2161 | } |
| 2162 | EXPORT_SYMBOL(netif_device_attach); |
| 2163 | |
Ben Hutchings | 36c9247 | 2012-01-17 07:57:56 +0000 | [diff] [blame] | 2164 | static void skb_warn_bad_offload(const struct sk_buff *skb) |
| 2165 | { |
Michał Mirosław | 65e9d2f | 2012-01-17 10:00:40 +0000 | [diff] [blame] | 2166 | static const netdev_features_t null_features = 0; |
Ben Hutchings | 36c9247 | 2012-01-17 07:57:56 +0000 | [diff] [blame] | 2167 | struct net_device *dev = skb->dev; |
| 2168 | const char *driver = ""; |
| 2169 | |
Ben Greear | c846ad9 | 2013-04-19 10:45:52 +0000 | [diff] [blame] | 2170 | if (!net_ratelimit()) |
| 2171 | return; |
| 2172 | |
Ben Hutchings | 36c9247 | 2012-01-17 07:57:56 +0000 | [diff] [blame] | 2173 | if (dev && dev->dev.parent) |
| 2174 | driver = dev_driver_string(dev->dev.parent); |
| 2175 | |
| 2176 | WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d " |
| 2177 | "gso_type=%d ip_summed=%d\n", |
Michał Mirosław | 65e9d2f | 2012-01-17 10:00:40 +0000 | [diff] [blame] | 2178 | driver, dev ? &dev->features : &null_features, |
| 2179 | skb->sk ? &skb->sk->sk_route_caps : &null_features, |
Ben Hutchings | 36c9247 | 2012-01-17 07:57:56 +0000 | [diff] [blame] | 2180 | skb->len, skb->data_len, skb_shinfo(skb)->gso_size, |
| 2181 | skb_shinfo(skb)->gso_type, skb->ip_summed); |
| 2182 | } |
| 2183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | /* |
| 2185 | * Invalidate hardware checksum when packet is to be mangled, and |
| 2186 | * complete checksum manually on outgoing path. |
| 2187 | */ |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 2188 | int skb_checksum_help(struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | { |
Al Viro | d3bc23e | 2006-11-14 21:24:49 -0800 | [diff] [blame] | 2190 | __wsum csum; |
Herbert Xu | 663ead3 | 2007-04-09 11:59:07 -0700 | [diff] [blame] | 2191 | int ret = 0, offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | |
Patrick McHardy | 84fa793 | 2006-08-29 16:44:56 -0700 | [diff] [blame] | 2193 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
Herbert Xu | a430a43 | 2006-07-08 13:34:56 -0700 | [diff] [blame] | 2194 | goto out_set_summed; |
| 2195 | |
| 2196 | if (unlikely(skb_shinfo(skb)->gso_size)) { |
Ben Hutchings | 36c9247 | 2012-01-17 07:57:56 +0000 | [diff] [blame] | 2197 | skb_warn_bad_offload(skb); |
| 2198 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | } |
| 2200 | |
Eric Dumazet | cef401d | 2013-01-25 20:34:37 +0000 | [diff] [blame] | 2201 | /* Before computing a checksum, we should make sure no frag could |
| 2202 | * be modified by an external entity : checksum could be wrong. |
| 2203 | */ |
| 2204 | if (skb_has_shared_frag(skb)) { |
| 2205 | ret = __skb_linearize(skb); |
| 2206 | if (ret) |
| 2207 | goto out; |
| 2208 | } |
| 2209 | |
Michał Mirosław | 55508d6 | 2010-12-14 15:24:08 +0000 | [diff] [blame] | 2210 | offset = skb_checksum_start_offset(skb); |
Herbert Xu | a030847 | 2007-10-15 01:47:15 -0700 | [diff] [blame] | 2211 | BUG_ON(offset >= skb_headlen(skb)); |
| 2212 | csum = skb_checksum(skb, offset, skb->len - offset, 0); |
| 2213 | |
| 2214 | offset += skb->csum_offset; |
| 2215 | BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb)); |
| 2216 | |
| 2217 | if (skb_cloned(skb) && |
| 2218 | !skb_clone_writable(skb, offset + sizeof(__sum16))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
| 2220 | if (ret) |
| 2221 | goto out; |
| 2222 | } |
| 2223 | |
Herbert Xu | a030847 | 2007-10-15 01:47:15 -0700 | [diff] [blame] | 2224 | *(__sum16 *)(skb->data + offset) = csum_fold(csum); |
Herbert Xu | a430a43 | 2006-07-08 13:34:56 -0700 | [diff] [blame] | 2225 | out_set_summed: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | skb->ip_summed = CHECKSUM_NONE; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 2227 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | return ret; |
| 2229 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 2230 | EXPORT_SYMBOL(skb_checksum_help); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 2232 | __be16 skb_network_protocol(struct sk_buff *skb) |
| 2233 | { |
| 2234 | __be16 type = skb->protocol; |
David S. Miller | 6181659 | 2013-03-20 12:46:26 -0400 | [diff] [blame] | 2235 | int vlan_depth = ETH_HLEN; |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 2236 | |
Pravin B Shelar | 19acc32 | 2013-05-07 20:41:07 +0000 | [diff] [blame] | 2237 | /* Tunnel gso handlers can set protocol to ethernet. */ |
| 2238 | if (type == htons(ETH_P_TEB)) { |
| 2239 | struct ethhdr *eth; |
| 2240 | |
| 2241 | if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr)))) |
| 2242 | return 0; |
| 2243 | |
| 2244 | eth = (struct ethhdr *)skb_mac_header(skb); |
| 2245 | type = eth->h_proto; |
| 2246 | } |
| 2247 | |
Patrick McHardy | 8ad227f | 2013-04-19 02:04:31 +0000 | [diff] [blame] | 2248 | while (type == htons(ETH_P_8021Q) || type == htons(ETH_P_8021AD)) { |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 2249 | struct vlan_hdr *vh; |
| 2250 | |
| 2251 | if (unlikely(!pskb_may_pull(skb, vlan_depth + VLAN_HLEN))) |
| 2252 | return 0; |
| 2253 | |
| 2254 | vh = (struct vlan_hdr *)(skb->data + vlan_depth); |
| 2255 | type = vh->h_vlan_encapsulated_proto; |
| 2256 | vlan_depth += VLAN_HLEN; |
| 2257 | } |
| 2258 | |
| 2259 | return type; |
| 2260 | } |
| 2261 | |
Pravin B Shelar | 05e8ef4 | 2013-02-14 09:44:55 +0000 | [diff] [blame] | 2262 | /** |
| 2263 | * skb_mac_gso_segment - mac layer segmentation handler. |
| 2264 | * @skb: buffer to segment |
| 2265 | * @features: features for the output path (see dev->features) |
| 2266 | */ |
| 2267 | struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb, |
| 2268 | netdev_features_t features) |
| 2269 | { |
| 2270 | struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT); |
| 2271 | struct packet_offload *ptype; |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 2272 | __be16 type = skb_network_protocol(skb); |
Pravin B Shelar | 05e8ef4 | 2013-02-14 09:44:55 +0000 | [diff] [blame] | 2273 | |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 2274 | if (unlikely(!type)) |
| 2275 | return ERR_PTR(-EINVAL); |
Pravin B Shelar | 05e8ef4 | 2013-02-14 09:44:55 +0000 | [diff] [blame] | 2276 | |
| 2277 | __skb_pull(skb, skb->mac_len); |
| 2278 | |
| 2279 | rcu_read_lock(); |
| 2280 | list_for_each_entry_rcu(ptype, &offload_base, list) { |
| 2281 | if (ptype->type == type && ptype->callbacks.gso_segment) { |
| 2282 | if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) { |
| 2283 | int err; |
| 2284 | |
| 2285 | err = ptype->callbacks.gso_send_check(skb); |
| 2286 | segs = ERR_PTR(err); |
| 2287 | if (err || skb_gso_ok(skb, features)) |
| 2288 | break; |
| 2289 | __skb_push(skb, (skb->data - |
| 2290 | skb_network_header(skb))); |
| 2291 | } |
| 2292 | segs = ptype->callbacks.gso_segment(skb, features); |
| 2293 | break; |
| 2294 | } |
| 2295 | } |
| 2296 | rcu_read_unlock(); |
| 2297 | |
| 2298 | __skb_push(skb, skb->data - skb_mac_header(skb)); |
| 2299 | |
| 2300 | return segs; |
| 2301 | } |
| 2302 | EXPORT_SYMBOL(skb_mac_gso_segment); |
| 2303 | |
| 2304 | |
Cong Wang | 12b0004 | 2013-02-05 16:36:38 +0000 | [diff] [blame] | 2305 | /* openvswitch calls this on rx path, so we need a different check. |
| 2306 | */ |
| 2307 | static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path) |
| 2308 | { |
| 2309 | if (tx_path) |
| 2310 | return skb->ip_summed != CHECKSUM_PARTIAL; |
| 2311 | else |
| 2312 | return skb->ip_summed == CHECKSUM_NONE; |
| 2313 | } |
| 2314 | |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2315 | /** |
Cong Wang | 12b0004 | 2013-02-05 16:36:38 +0000 | [diff] [blame] | 2316 | * __skb_gso_segment - Perform segmentation on skb. |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2317 | * @skb: buffer to segment |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 2318 | * @features: features for the output path (see dev->features) |
Cong Wang | 12b0004 | 2013-02-05 16:36:38 +0000 | [diff] [blame] | 2319 | * @tx_path: whether it is called in TX path |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2320 | * |
| 2321 | * This function segments the given skb and returns a list of segments. |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 2322 | * |
| 2323 | * It may return NULL if the skb requires no segmentation. This is |
| 2324 | * only possible when GSO is used for verifying header integrity. |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2325 | */ |
Cong Wang | 12b0004 | 2013-02-05 16:36:38 +0000 | [diff] [blame] | 2326 | struct sk_buff *__skb_gso_segment(struct sk_buff *skb, |
| 2327 | netdev_features_t features, bool tx_path) |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2328 | { |
Cong Wang | 12b0004 | 2013-02-05 16:36:38 +0000 | [diff] [blame] | 2329 | if (unlikely(skb_needs_check(skb, tx_path))) { |
Pravin B Shelar | 05e8ef4 | 2013-02-14 09:44:55 +0000 | [diff] [blame] | 2330 | int err; |
| 2331 | |
Ben Hutchings | 36c9247 | 2012-01-17 07:57:56 +0000 | [diff] [blame] | 2332 | skb_warn_bad_offload(skb); |
Herbert Xu | 67fd1a7 | 2009-01-19 16:26:44 -0800 | [diff] [blame] | 2333 | |
Herbert Xu | a430a43 | 2006-07-08 13:34:56 -0700 | [diff] [blame] | 2334 | if (skb_header_cloned(skb) && |
| 2335 | (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) |
| 2336 | return ERR_PTR(err); |
| 2337 | } |
| 2338 | |
Pravin B Shelar | 68c3316 | 2013-02-14 14:02:41 +0000 | [diff] [blame] | 2339 | SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb); |
Pravin B Shelar | 05e8ef4 | 2013-02-14 09:44:55 +0000 | [diff] [blame] | 2340 | skb_reset_mac_header(skb); |
| 2341 | skb_reset_mac_len(skb); |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2342 | |
Pravin B Shelar | 05e8ef4 | 2013-02-14 09:44:55 +0000 | [diff] [blame] | 2343 | return skb_mac_gso_segment(skb, features); |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2344 | } |
Cong Wang | 12b0004 | 2013-02-05 16:36:38 +0000 | [diff] [blame] | 2345 | EXPORT_SYMBOL(__skb_gso_segment); |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2346 | |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 2347 | /* Take action when hardware reception checksum errors are detected. */ |
| 2348 | #ifdef CONFIG_BUG |
| 2349 | void netdev_rx_csum_fault(struct net_device *dev) |
| 2350 | { |
| 2351 | if (net_ratelimit()) { |
Joe Perches | 7b6cd1c | 2012-02-01 10:54:43 +0000 | [diff] [blame] | 2352 | pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>"); |
Herbert Xu | fb286bb | 2005-11-10 13:01:24 -0800 | [diff] [blame] | 2353 | dump_stack(); |
| 2354 | } |
| 2355 | } |
| 2356 | EXPORT_SYMBOL(netdev_rx_csum_fault); |
| 2357 | #endif |
| 2358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2359 | /* Actually, we should eliminate this check as soon as we know, that: |
| 2360 | * 1. IOMMU is present and allows to map all the memory. |
| 2361 | * 2. No high memory really exists on this machine. |
| 2362 | */ |
| 2363 | |
Eric Dumazet | 9092c65 | 2010-04-02 13:34:49 -0700 | [diff] [blame] | 2364 | static int illegal_highdma(struct net_device *dev, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | { |
Herbert Xu | 3d3a853 | 2006-06-27 13:33:10 -0700 | [diff] [blame] | 2366 | #ifdef CONFIG_HIGHMEM |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2367 | int i; |
FUJITA Tomonori | 5acbbd4 | 2010-03-30 22:35:50 +0000 | [diff] [blame] | 2368 | if (!(dev->features & NETIF_F_HIGHDMA)) { |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2369 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 2370 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 2371 | if (PageHighMem(skb_frag_page(frag))) |
FUJITA Tomonori | 5acbbd4 | 2010-03-30 22:35:50 +0000 | [diff] [blame] | 2372 | return 1; |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2373 | } |
FUJITA Tomonori | 5acbbd4 | 2010-03-30 22:35:50 +0000 | [diff] [blame] | 2374 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | |
FUJITA Tomonori | 5acbbd4 | 2010-03-30 22:35:50 +0000 | [diff] [blame] | 2376 | if (PCI_DMA_BUS_IS_PHYS) { |
| 2377 | struct device *pdev = dev->dev.parent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | |
Eric Dumazet | 9092c65 | 2010-04-02 13:34:49 -0700 | [diff] [blame] | 2379 | if (!pdev) |
| 2380 | return 0; |
FUJITA Tomonori | 5acbbd4 | 2010-03-30 22:35:50 +0000 | [diff] [blame] | 2381 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 2382 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
| 2383 | dma_addr_t addr = page_to_phys(skb_frag_page(frag)); |
FUJITA Tomonori | 5acbbd4 | 2010-03-30 22:35:50 +0000 | [diff] [blame] | 2384 | if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask) |
| 2385 | return 1; |
| 2386 | } |
| 2387 | } |
Herbert Xu | 3d3a853 | 2006-06-27 13:33:10 -0700 | [diff] [blame] | 2388 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2389 | return 0; |
| 2390 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2392 | struct dev_gso_cb { |
| 2393 | void (*destructor)(struct sk_buff *skb); |
| 2394 | }; |
| 2395 | |
| 2396 | #define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb) |
| 2397 | |
| 2398 | static void dev_gso_skb_destructor(struct sk_buff *skb) |
| 2399 | { |
| 2400 | struct dev_gso_cb *cb; |
| 2401 | |
| 2402 | do { |
| 2403 | struct sk_buff *nskb = skb->next; |
| 2404 | |
| 2405 | skb->next = nskb->next; |
| 2406 | nskb->next = NULL; |
| 2407 | kfree_skb(nskb); |
| 2408 | } while (skb->next); |
| 2409 | |
| 2410 | cb = DEV_GSO_CB(skb); |
| 2411 | if (cb->destructor) |
| 2412 | cb->destructor(skb); |
| 2413 | } |
| 2414 | |
| 2415 | /** |
| 2416 | * dev_gso_segment - Perform emulated hardware segmentation on skb. |
| 2417 | * @skb: buffer to segment |
Jesse Gross | 91ecb63 | 2011-01-09 06:23:33 +0000 | [diff] [blame] | 2418 | * @features: device features as applicable to this skb |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2419 | * |
| 2420 | * This function segments the given skb and stores the list of segments |
| 2421 | * in skb->next. |
| 2422 | */ |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 2423 | static int dev_gso_segment(struct sk_buff *skb, netdev_features_t features) |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2424 | { |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2425 | struct sk_buff *segs; |
| 2426 | |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 2427 | segs = skb_gso_segment(skb, features); |
| 2428 | |
| 2429 | /* Verifying header integrity only. */ |
| 2430 | if (!segs) |
| 2431 | return 0; |
| 2432 | |
Hirofumi Nakagawa | 801678c | 2008-04-29 01:03:09 -0700 | [diff] [blame] | 2433 | if (IS_ERR(segs)) |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2434 | return PTR_ERR(segs); |
| 2435 | |
| 2436 | skb->next = segs; |
| 2437 | DEV_GSO_CB(skb)->destructor = skb->destructor; |
| 2438 | skb->destructor = dev_gso_skb_destructor; |
| 2439 | |
| 2440 | return 0; |
| 2441 | } |
| 2442 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 2443 | static netdev_features_t harmonize_features(struct sk_buff *skb, |
| 2444 | __be16 protocol, netdev_features_t features) |
Jesse Gross | f01a523 | 2011-01-09 06:23:31 +0000 | [diff] [blame] | 2445 | { |
Ed Cashin | c0d680e | 2012-09-19 15:49:00 +0000 | [diff] [blame] | 2446 | if (skb->ip_summed != CHECKSUM_NONE && |
| 2447 | !can_checksum_protocol(features, protocol)) { |
Jesse Gross | f01a523 | 2011-01-09 06:23:31 +0000 | [diff] [blame] | 2448 | features &= ~NETIF_F_ALL_CSUM; |
Jesse Gross | f01a523 | 2011-01-09 06:23:31 +0000 | [diff] [blame] | 2449 | } else if (illegal_highdma(skb->dev, skb)) { |
| 2450 | features &= ~NETIF_F_SG; |
| 2451 | } |
| 2452 | |
| 2453 | return features; |
| 2454 | } |
| 2455 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 2456 | netdev_features_t netif_skb_features(struct sk_buff *skb) |
Jesse Gross | 58e998c | 2010-10-29 12:14:55 +0000 | [diff] [blame] | 2457 | { |
| 2458 | __be16 protocol = skb->protocol; |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 2459 | netdev_features_t features = skb->dev->features; |
Jesse Gross | 58e998c | 2010-10-29 12:14:55 +0000 | [diff] [blame] | 2460 | |
Ben Hutchings | 30b678d | 2012-07-30 15:57:00 +0000 | [diff] [blame] | 2461 | if (skb_shinfo(skb)->gso_segs > skb->dev->gso_max_segs) |
| 2462 | features &= ~NETIF_F_GSO_MASK; |
| 2463 | |
Patrick McHardy | 8ad227f | 2013-04-19 02:04:31 +0000 | [diff] [blame] | 2464 | if (protocol == htons(ETH_P_8021Q) || protocol == htons(ETH_P_8021AD)) { |
Jesse Gross | 58e998c | 2010-10-29 12:14:55 +0000 | [diff] [blame] | 2465 | struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data; |
| 2466 | protocol = veh->h_vlan_encapsulated_proto; |
Jesse Gross | f01a523 | 2011-01-09 06:23:31 +0000 | [diff] [blame] | 2467 | } else if (!vlan_tx_tag_present(skb)) { |
| 2468 | return harmonize_features(skb, protocol, features); |
| 2469 | } |
Jesse Gross | 58e998c | 2010-10-29 12:14:55 +0000 | [diff] [blame] | 2470 | |
Patrick McHardy | 8ad227f | 2013-04-19 02:04:31 +0000 | [diff] [blame] | 2471 | features &= (skb->dev->vlan_features | NETIF_F_HW_VLAN_CTAG_TX | |
| 2472 | NETIF_F_HW_VLAN_STAG_TX); |
Jesse Gross | f01a523 | 2011-01-09 06:23:31 +0000 | [diff] [blame] | 2473 | |
Patrick McHardy | 8ad227f | 2013-04-19 02:04:31 +0000 | [diff] [blame] | 2474 | if (protocol != htons(ETH_P_8021Q) && protocol != htons(ETH_P_8021AD)) { |
Jesse Gross | f01a523 | 2011-01-09 06:23:31 +0000 | [diff] [blame] | 2475 | return harmonize_features(skb, protocol, features); |
| 2476 | } else { |
| 2477 | features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | |
Patrick McHardy | 8ad227f | 2013-04-19 02:04:31 +0000 | [diff] [blame] | 2478 | NETIF_F_GEN_CSUM | NETIF_F_HW_VLAN_CTAG_TX | |
| 2479 | NETIF_F_HW_VLAN_STAG_TX; |
Jesse Gross | f01a523 | 2011-01-09 06:23:31 +0000 | [diff] [blame] | 2480 | return harmonize_features(skb, protocol, features); |
| 2481 | } |
Jesse Gross | 58e998c | 2010-10-29 12:14:55 +0000 | [diff] [blame] | 2482 | } |
Jesse Gross | f01a523 | 2011-01-09 06:23:31 +0000 | [diff] [blame] | 2483 | EXPORT_SYMBOL(netif_skb_features); |
Jesse Gross | 58e998c | 2010-10-29 12:14:55 +0000 | [diff] [blame] | 2484 | |
John Fastabend | 6afff0c | 2010-06-16 14:18:12 +0000 | [diff] [blame] | 2485 | /* |
| 2486 | * Returns true if either: |
| 2487 | * 1. skb has frag_list and the device doesn't support FRAGLIST, or |
Rami Rosen | d1a53df | 2012-08-27 23:39:24 +0000 | [diff] [blame] | 2488 | * 2. skb is fragmented and the device does not support SG. |
John Fastabend | 6afff0c | 2010-06-16 14:18:12 +0000 | [diff] [blame] | 2489 | */ |
| 2490 | static inline int skb_needs_linearize(struct sk_buff *skb, |
Patrick McHardy | 6708c9e | 2013-05-01 22:36:49 +0000 | [diff] [blame] | 2491 | netdev_features_t features) |
John Fastabend | 6afff0c | 2010-06-16 14:18:12 +0000 | [diff] [blame] | 2492 | { |
Jesse Gross | 02932ce | 2011-01-09 06:23:34 +0000 | [diff] [blame] | 2493 | return skb_is_nonlinear(skb) && |
| 2494 | ((skb_has_frag_list(skb) && |
| 2495 | !(features & NETIF_F_FRAGLIST)) || |
Jesse Gross | e1e78db | 2010-10-29 12:14:53 +0000 | [diff] [blame] | 2496 | (skb_shinfo(skb)->nr_frags && |
Jesse Gross | 02932ce | 2011-01-09 06:23:34 +0000 | [diff] [blame] | 2497 | !(features & NETIF_F_SG))); |
John Fastabend | 6afff0c | 2010-06-16 14:18:12 +0000 | [diff] [blame] | 2498 | } |
| 2499 | |
David S. Miller | fd2ea0a | 2008-07-17 01:56:23 -0700 | [diff] [blame] | 2500 | int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev, |
| 2501 | struct netdev_queue *txq) |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2502 | { |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 2503 | const struct net_device_ops *ops = dev->netdev_ops; |
Patrick McHardy | 572a9d7 | 2009-11-10 06:14:14 +0000 | [diff] [blame] | 2504 | int rc = NETDEV_TX_OK; |
Koki Sanagi | ec764bf | 2011-05-30 21:48:34 +0000 | [diff] [blame] | 2505 | unsigned int skb_len; |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 2506 | |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2507 | if (likely(!skb->next)) { |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 2508 | netdev_features_t features; |
Jesse Gross | fc74121 | 2011-01-09 06:23:32 +0000 | [diff] [blame] | 2509 | |
Eric Dumazet | 93f154b | 2009-05-18 22:19:19 -0700 | [diff] [blame] | 2510 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 2511 | * If device doesn't need skb->dst, release it right now while |
Eric Dumazet | 93f154b | 2009-05-18 22:19:19 -0700 | [diff] [blame] | 2512 | * its hot in this cpu cache |
| 2513 | */ |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 2514 | if (dev->priv_flags & IFF_XMIT_DST_RELEASE) |
| 2515 | skb_dst_drop(skb); |
| 2516 | |
Jesse Gross | fc74121 | 2011-01-09 06:23:32 +0000 | [diff] [blame] | 2517 | features = netif_skb_features(skb); |
| 2518 | |
Jesse Gross | 7b9c609 | 2010-10-20 13:56:04 +0000 | [diff] [blame] | 2519 | if (vlan_tx_tag_present(skb) && |
Patrick McHardy | 86a9bad | 2013-04-19 02:04:30 +0000 | [diff] [blame] | 2520 | !vlan_hw_offload_capable(features, skb->vlan_proto)) { |
| 2521 | skb = __vlan_put_tag(skb, skb->vlan_proto, |
| 2522 | vlan_tx_tag_get(skb)); |
Jesse Gross | 7b9c609 | 2010-10-20 13:56:04 +0000 | [diff] [blame] | 2523 | if (unlikely(!skb)) |
| 2524 | goto out; |
| 2525 | |
| 2526 | skb->vlan_tci = 0; |
| 2527 | } |
| 2528 | |
Alexander Duyck | fc70fb6 | 2012-12-07 14:14:15 +0000 | [diff] [blame] | 2529 | /* If encapsulation offload request, verify we are testing |
| 2530 | * hardware encapsulation features instead of standard |
| 2531 | * features for the netdev |
| 2532 | */ |
| 2533 | if (skb->encapsulation) |
| 2534 | features &= dev->hw_enc_features; |
| 2535 | |
Jesse Gross | fc74121 | 2011-01-09 06:23:32 +0000 | [diff] [blame] | 2536 | if (netif_needs_gso(skb, features)) { |
Jesse Gross | 91ecb63 | 2011-01-09 06:23:33 +0000 | [diff] [blame] | 2537 | if (unlikely(dev_gso_segment(skb, features))) |
David S. Miller | 9ccb897 | 2010-04-22 01:02:07 -0700 | [diff] [blame] | 2538 | goto out_kfree_skb; |
| 2539 | if (skb->next) |
| 2540 | goto gso; |
John Fastabend | 6afff0c | 2010-06-16 14:18:12 +0000 | [diff] [blame] | 2541 | } else { |
Jesse Gross | 02932ce | 2011-01-09 06:23:34 +0000 | [diff] [blame] | 2542 | if (skb_needs_linearize(skb, features) && |
John Fastabend | 6afff0c | 2010-06-16 14:18:12 +0000 | [diff] [blame] | 2543 | __skb_linearize(skb)) |
| 2544 | goto out_kfree_skb; |
| 2545 | |
| 2546 | /* If packet is not checksummed and device does not |
| 2547 | * support checksumming for this protocol, complete |
| 2548 | * checksumming here. |
| 2549 | */ |
| 2550 | if (skb->ip_summed == CHECKSUM_PARTIAL) { |
Alexander Duyck | fc70fb6 | 2012-12-07 14:14:15 +0000 | [diff] [blame] | 2551 | if (skb->encapsulation) |
| 2552 | skb_set_inner_transport_header(skb, |
| 2553 | skb_checksum_start_offset(skb)); |
| 2554 | else |
| 2555 | skb_set_transport_header(skb, |
| 2556 | skb_checksum_start_offset(skb)); |
Jesse Gross | 0363466 | 2011-01-09 06:23:35 +0000 | [diff] [blame] | 2557 | if (!(features & NETIF_F_ALL_CSUM) && |
John Fastabend | 6afff0c | 2010-06-16 14:18:12 +0000 | [diff] [blame] | 2558 | skb_checksum_help(skb)) |
| 2559 | goto out_kfree_skb; |
| 2560 | } |
David S. Miller | 9ccb897 | 2010-04-22 01:02:07 -0700 | [diff] [blame] | 2561 | } |
| 2562 | |
Eric Dumazet | b40863c | 2012-09-18 20:44:49 +0000 | [diff] [blame] | 2563 | if (!list_empty(&ptype_all)) |
| 2564 | dev_queue_xmit_nit(skb, dev); |
| 2565 | |
Koki Sanagi | ec764bf | 2011-05-30 21:48:34 +0000 | [diff] [blame] | 2566 | skb_len = skb->len; |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 2567 | rc = ops->ndo_start_xmit(skb, dev); |
Koki Sanagi | ec764bf | 2011-05-30 21:48:34 +0000 | [diff] [blame] | 2568 | trace_net_dev_xmit(skb, rc, dev, skb_len); |
Patrick McHardy | ec634fe | 2009-07-05 19:23:38 -0700 | [diff] [blame] | 2569 | if (rc == NETDEV_TX_OK) |
Eric Dumazet | 08baf56 | 2009-05-25 22:58:01 -0700 | [diff] [blame] | 2570 | txq_trans_update(txq); |
Patrick Ohly | ac45f60 | 2009-02-12 05:03:37 +0000 | [diff] [blame] | 2571 | return rc; |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2572 | } |
| 2573 | |
Herbert Xu | 576a30e | 2006-06-27 13:22:38 -0700 | [diff] [blame] | 2574 | gso: |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2575 | do { |
| 2576 | struct sk_buff *nskb = skb->next; |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2577 | |
| 2578 | skb->next = nskb->next; |
| 2579 | nskb->next = NULL; |
Krishna Kumar | 068a2de | 2009-12-09 20:59:58 +0000 | [diff] [blame] | 2580 | |
Eric Dumazet | b40863c | 2012-09-18 20:44:49 +0000 | [diff] [blame] | 2581 | if (!list_empty(&ptype_all)) |
| 2582 | dev_queue_xmit_nit(nskb, dev); |
| 2583 | |
Koki Sanagi | ec764bf | 2011-05-30 21:48:34 +0000 | [diff] [blame] | 2584 | skb_len = nskb->len; |
Stephen Hemminger | 0082982 | 2008-11-20 20:14:53 -0800 | [diff] [blame] | 2585 | rc = ops->ndo_start_xmit(nskb, dev); |
Koki Sanagi | ec764bf | 2011-05-30 21:48:34 +0000 | [diff] [blame] | 2586 | trace_net_dev_xmit(nskb, rc, dev, skb_len); |
Patrick McHardy | ec634fe | 2009-07-05 19:23:38 -0700 | [diff] [blame] | 2587 | if (unlikely(rc != NETDEV_TX_OK)) { |
Patrick McHardy | 572a9d7 | 2009-11-10 06:14:14 +0000 | [diff] [blame] | 2588 | if (rc & ~NETDEV_TX_MASK) |
| 2589 | goto out_kfree_gso_skb; |
Michael Chan | f54d9e8 | 2006-06-25 23:57:04 -0700 | [diff] [blame] | 2590 | nskb->next = skb->next; |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2591 | skb->next = nskb; |
| 2592 | return rc; |
| 2593 | } |
Eric Dumazet | 08baf56 | 2009-05-25 22:58:01 -0700 | [diff] [blame] | 2594 | txq_trans_update(txq); |
Tom Herbert | 73466498 | 2011-11-28 16:32:44 +0000 | [diff] [blame] | 2595 | if (unlikely(netif_xmit_stopped(txq) && skb->next)) |
Michael Chan | f54d9e8 | 2006-06-25 23:57:04 -0700 | [diff] [blame] | 2596 | return NETDEV_TX_BUSY; |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2597 | } while (skb->next); |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 2598 | |
Patrick McHardy | 572a9d7 | 2009-11-10 06:14:14 +0000 | [diff] [blame] | 2599 | out_kfree_gso_skb: |
Sridhar Samudrala | 0c77215 | 2013-04-29 13:02:42 +0000 | [diff] [blame] | 2600 | if (likely(skb->next == NULL)) { |
Patrick McHardy | 572a9d7 | 2009-11-10 06:14:14 +0000 | [diff] [blame] | 2601 | skb->destructor = DEV_GSO_CB(skb)->destructor; |
Sridhar Samudrala | 0c77215 | 2013-04-29 13:02:42 +0000 | [diff] [blame] | 2602 | consume_skb(skb); |
| 2603 | return rc; |
| 2604 | } |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2605 | out_kfree_skb: |
| 2606 | kfree_skb(skb); |
Jesse Gross | 7b9c609 | 2010-10-20 13:56:04 +0000 | [diff] [blame] | 2607 | out: |
Patrick McHardy | 572a9d7 | 2009-11-10 06:14:14 +0000 | [diff] [blame] | 2608 | return rc; |
Herbert Xu | f6a78bf | 2006-06-22 02:57:17 -0700 | [diff] [blame] | 2609 | } |
| 2610 | |
Eric Dumazet | 1def923 | 2013-01-10 12:36:42 +0000 | [diff] [blame] | 2611 | static void qdisc_pkt_len_init(struct sk_buff *skb) |
| 2612 | { |
| 2613 | const struct skb_shared_info *shinfo = skb_shinfo(skb); |
| 2614 | |
| 2615 | qdisc_skb_cb(skb)->pkt_len = skb->len; |
| 2616 | |
| 2617 | /* To get more precise estimation of bytes sent on wire, |
| 2618 | * we add to pkt_len the headers size of all segments |
| 2619 | */ |
| 2620 | if (shinfo->gso_size) { |
Eric Dumazet | 757b8b1 | 2013-01-15 21:14:21 -0800 | [diff] [blame] | 2621 | unsigned int hdr_len; |
Jason Wang | 15e5a03 | 2013-03-25 20:19:59 +0000 | [diff] [blame] | 2622 | u16 gso_segs = shinfo->gso_segs; |
Eric Dumazet | 1def923 | 2013-01-10 12:36:42 +0000 | [diff] [blame] | 2623 | |
Eric Dumazet | 757b8b1 | 2013-01-15 21:14:21 -0800 | [diff] [blame] | 2624 | /* mac layer + network layer */ |
| 2625 | hdr_len = skb_transport_header(skb) - skb_mac_header(skb); |
| 2626 | |
| 2627 | /* + transport layer */ |
Eric Dumazet | 1def923 | 2013-01-10 12:36:42 +0000 | [diff] [blame] | 2628 | if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) |
| 2629 | hdr_len += tcp_hdrlen(skb); |
| 2630 | else |
| 2631 | hdr_len += sizeof(struct udphdr); |
Jason Wang | 15e5a03 | 2013-03-25 20:19:59 +0000 | [diff] [blame] | 2632 | |
| 2633 | if (shinfo->gso_type & SKB_GSO_DODGY) |
| 2634 | gso_segs = DIV_ROUND_UP(skb->len - hdr_len, |
| 2635 | shinfo->gso_size); |
| 2636 | |
| 2637 | qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len; |
Eric Dumazet | 1def923 | 2013-01-10 12:36:42 +0000 | [diff] [blame] | 2638 | } |
| 2639 | } |
| 2640 | |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 2641 | static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q, |
| 2642 | struct net_device *dev, |
| 2643 | struct netdev_queue *txq) |
| 2644 | { |
| 2645 | spinlock_t *root_lock = qdisc_lock(q); |
Eric Dumazet | a2da570 | 2011-01-20 03:48:19 +0000 | [diff] [blame] | 2646 | bool contended; |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 2647 | int rc; |
| 2648 | |
Eric Dumazet | 1def923 | 2013-01-10 12:36:42 +0000 | [diff] [blame] | 2649 | qdisc_pkt_len_init(skb); |
Eric Dumazet | a2da570 | 2011-01-20 03:48:19 +0000 | [diff] [blame] | 2650 | qdisc_calculate_pkt_len(skb, q); |
Eric Dumazet | 79640a4 | 2010-06-02 05:09:29 -0700 | [diff] [blame] | 2651 | /* |
| 2652 | * Heuristic to force contended enqueues to serialize on a |
| 2653 | * separate lock before trying to get qdisc main lock. |
| 2654 | * This permits __QDISC_STATE_RUNNING owner to get the lock more often |
| 2655 | * and dequeue packets faster. |
| 2656 | */ |
Eric Dumazet | a2da570 | 2011-01-20 03:48:19 +0000 | [diff] [blame] | 2657 | contended = qdisc_is_running(q); |
Eric Dumazet | 79640a4 | 2010-06-02 05:09:29 -0700 | [diff] [blame] | 2658 | if (unlikely(contended)) |
| 2659 | spin_lock(&q->busylock); |
| 2660 | |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 2661 | spin_lock(root_lock); |
| 2662 | if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) { |
| 2663 | kfree_skb(skb); |
| 2664 | rc = NET_XMIT_DROP; |
| 2665 | } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) && |
Eric Dumazet | bc135b2 | 2010-06-02 03:23:51 -0700 | [diff] [blame] | 2666 | qdisc_run_begin(q)) { |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 2667 | /* |
| 2668 | * This is a work-conserving queue; there are no old skbs |
| 2669 | * waiting to be sent out; and the qdisc is not running - |
| 2670 | * xmit the skb directly. |
| 2671 | */ |
Eric Dumazet | 7fee226 | 2010-05-11 23:19:48 +0000 | [diff] [blame] | 2672 | if (!(dev->priv_flags & IFF_XMIT_DST_RELEASE)) |
| 2673 | skb_dst_force(skb); |
Eric Dumazet | bfe0d02 | 2011-01-09 08:30:54 +0000 | [diff] [blame] | 2674 | |
Eric Dumazet | bfe0d02 | 2011-01-09 08:30:54 +0000 | [diff] [blame] | 2675 | qdisc_bstats_update(q, skb); |
| 2676 | |
Eric Dumazet | 79640a4 | 2010-06-02 05:09:29 -0700 | [diff] [blame] | 2677 | if (sch_direct_xmit(skb, q, dev, txq, root_lock)) { |
| 2678 | if (unlikely(contended)) { |
| 2679 | spin_unlock(&q->busylock); |
| 2680 | contended = false; |
| 2681 | } |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 2682 | __qdisc_run(q); |
Eric Dumazet | 79640a4 | 2010-06-02 05:09:29 -0700 | [diff] [blame] | 2683 | } else |
Eric Dumazet | bc135b2 | 2010-06-02 03:23:51 -0700 | [diff] [blame] | 2684 | qdisc_run_end(q); |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 2685 | |
| 2686 | rc = NET_XMIT_SUCCESS; |
| 2687 | } else { |
Eric Dumazet | 7fee226 | 2010-05-11 23:19:48 +0000 | [diff] [blame] | 2688 | skb_dst_force(skb); |
Eric Dumazet | a2da570 | 2011-01-20 03:48:19 +0000 | [diff] [blame] | 2689 | rc = q->enqueue(skb, q) & NET_XMIT_MASK; |
Eric Dumazet | 79640a4 | 2010-06-02 05:09:29 -0700 | [diff] [blame] | 2690 | if (qdisc_run_begin(q)) { |
| 2691 | if (unlikely(contended)) { |
| 2692 | spin_unlock(&q->busylock); |
| 2693 | contended = false; |
| 2694 | } |
| 2695 | __qdisc_run(q); |
| 2696 | } |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 2697 | } |
| 2698 | spin_unlock(root_lock); |
Eric Dumazet | 79640a4 | 2010-06-02 05:09:29 -0700 | [diff] [blame] | 2699 | if (unlikely(contended)) |
| 2700 | spin_unlock(&q->busylock); |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 2701 | return rc; |
| 2702 | } |
| 2703 | |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 2704 | #if IS_ENABLED(CONFIG_NETPRIO_CGROUP) |
| 2705 | static void skb_update_prio(struct sk_buff *skb) |
| 2706 | { |
Igor Maravic | 6977a79 | 2011-11-25 07:44:54 +0000 | [diff] [blame] | 2707 | struct netprio_map *map = rcu_dereference_bh(skb->dev->priomap); |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 2708 | |
Eric Dumazet | 91c68ce | 2012-07-08 21:45:10 +0000 | [diff] [blame] | 2709 | if (!skb->priority && skb->sk && map) { |
| 2710 | unsigned int prioidx = skb->sk->sk_cgrp_prioidx; |
| 2711 | |
| 2712 | if (prioidx < map->priomap_len) |
| 2713 | skb->priority = map->priomap[prioidx]; |
| 2714 | } |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 2715 | } |
| 2716 | #else |
| 2717 | #define skb_update_prio(skb) |
| 2718 | #endif |
| 2719 | |
Eric Dumazet | 745e20f | 2010-09-29 13:23:09 -0700 | [diff] [blame] | 2720 | static DEFINE_PER_CPU(int, xmit_recursion); |
David S. Miller | 11a766c | 2010-10-25 12:51:55 -0700 | [diff] [blame] | 2721 | #define RECURSION_LIMIT 10 |
Eric Dumazet | 745e20f | 2010-09-29 13:23:09 -0700 | [diff] [blame] | 2722 | |
Dave Jones | d29f749 | 2008-07-22 14:09:06 -0700 | [diff] [blame] | 2723 | /** |
Michel Machado | 95603e2 | 2012-06-12 10:16:35 +0000 | [diff] [blame] | 2724 | * dev_loopback_xmit - loop back @skb |
| 2725 | * @skb: buffer to transmit |
| 2726 | */ |
| 2727 | int dev_loopback_xmit(struct sk_buff *skb) |
| 2728 | { |
| 2729 | skb_reset_mac_header(skb); |
| 2730 | __skb_pull(skb, skb_network_offset(skb)); |
| 2731 | skb->pkt_type = PACKET_LOOPBACK; |
| 2732 | skb->ip_summed = CHECKSUM_UNNECESSARY; |
| 2733 | WARN_ON(!skb_dst(skb)); |
| 2734 | skb_dst_force(skb); |
| 2735 | netif_rx_ni(skb); |
| 2736 | return 0; |
| 2737 | } |
| 2738 | EXPORT_SYMBOL(dev_loopback_xmit); |
| 2739 | |
| 2740 | /** |
Dave Jones | d29f749 | 2008-07-22 14:09:06 -0700 | [diff] [blame] | 2741 | * dev_queue_xmit - transmit a buffer |
| 2742 | * @skb: buffer to transmit |
| 2743 | * |
| 2744 | * Queue a buffer for transmission to a network device. The caller must |
| 2745 | * have set the device and priority and built the buffer before calling |
| 2746 | * this function. The function can be called from an interrupt. |
| 2747 | * |
| 2748 | * A negative errno code is returned on a failure. A success does not |
| 2749 | * guarantee the frame will be transmitted as it may be dropped due |
| 2750 | * to congestion or traffic shaping. |
| 2751 | * |
| 2752 | * ----------------------------------------------------------------------------------- |
| 2753 | * I notice this method can also return errors from the queue disciplines, |
| 2754 | * including NET_XMIT_DROP, which is a positive value. So, errors can also |
| 2755 | * be positive. |
| 2756 | * |
| 2757 | * Regardless of the return value, the skb is consumed, so it is currently |
| 2758 | * difficult to retry a send to this method. (You can bump the ref count |
| 2759 | * before sending to hold a reference for retry if you are careful.) |
| 2760 | * |
| 2761 | * When calling this method, interrupts MUST be enabled. This is because |
| 2762 | * the BH enable code must have IRQs enabled so that it will not deadlock. |
| 2763 | * --BLG |
| 2764 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | int dev_queue_xmit(struct sk_buff *skb) |
| 2766 | { |
| 2767 | struct net_device *dev = skb->dev; |
David S. Miller | dc2b484 | 2008-07-08 17:18:23 -0700 | [diff] [blame] | 2768 | struct netdev_queue *txq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | struct Qdisc *q; |
| 2770 | int rc = -ENOMEM; |
| 2771 | |
Eric Dumazet | 6d1ccff | 2013-02-05 20:22:20 +0000 | [diff] [blame] | 2772 | skb_reset_mac_header(skb); |
| 2773 | |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 2774 | /* Disable soft irqs for various locks below. Also |
| 2775 | * stops preemption for RCU. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | */ |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 2777 | rcu_read_lock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | |
Neil Horman | 5bc1421 | 2011-11-22 05:10:51 +0000 | [diff] [blame] | 2779 | skb_update_prio(skb); |
| 2780 | |
Amerigo Wang | 8c4c49d | 2012-09-17 20:16:31 +0000 | [diff] [blame] | 2781 | txq = netdev_pick_tx(dev, skb); |
Paul E. McKenney | a898def | 2010-02-22 17:04:49 -0800 | [diff] [blame] | 2782 | q = rcu_dereference_bh(txq->qdisc); |
David S. Miller | 37437bb | 2008-07-16 02:15:04 -0700 | [diff] [blame] | 2783 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | #ifdef CONFIG_NET_CLS_ACT |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 2785 | skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_EGRESS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | #endif |
Koki Sanagi | cf66ba5 | 2010-08-23 18:45:02 +0900 | [diff] [blame] | 2787 | trace_net_dev_queue(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | if (q->enqueue) { |
Krishna Kumar | bbd8a0d | 2009-08-06 01:44:21 +0000 | [diff] [blame] | 2789 | rc = __dev_xmit_skb(skb, q, dev, txq); |
David S. Miller | 37437bb | 2008-07-16 02:15:04 -0700 | [diff] [blame] | 2790 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | } |
| 2792 | |
| 2793 | /* The device has no queue. Common case for software devices: |
| 2794 | loopback, all the sorts of tunnels... |
| 2795 | |
Herbert Xu | 932ff27 | 2006-06-09 12:20:56 -0700 | [diff] [blame] | 2796 | Really, it is unlikely that netif_tx_lock protection is necessary |
| 2797 | here. (f.e. loopback and IP tunnels are clean ignoring statistics |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | counters.) |
| 2799 | However, it is possible, that they rely on protection |
| 2800 | made by us here. |
| 2801 | |
| 2802 | Check this and shot the lock. It is not prone from deadlocks. |
| 2803 | Either shot noqueue qdisc, it is even simpler 8) |
| 2804 | */ |
| 2805 | if (dev->flags & IFF_UP) { |
| 2806 | int cpu = smp_processor_id(); /* ok because BHs are off */ |
| 2807 | |
David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 2808 | if (txq->xmit_lock_owner != cpu) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | |
Eric Dumazet | 745e20f | 2010-09-29 13:23:09 -0700 | [diff] [blame] | 2810 | if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT) |
| 2811 | goto recursion_alert; |
| 2812 | |
David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 2813 | HARD_TX_LOCK(dev, txq, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | |
Tom Herbert | 73466498 | 2011-11-28 16:32:44 +0000 | [diff] [blame] | 2815 | if (!netif_xmit_stopped(txq)) { |
Eric Dumazet | 745e20f | 2010-09-29 13:23:09 -0700 | [diff] [blame] | 2816 | __this_cpu_inc(xmit_recursion); |
Patrick McHardy | 572a9d7 | 2009-11-10 06:14:14 +0000 | [diff] [blame] | 2817 | rc = dev_hard_start_xmit(skb, dev, txq); |
Eric Dumazet | 745e20f | 2010-09-29 13:23:09 -0700 | [diff] [blame] | 2818 | __this_cpu_dec(xmit_recursion); |
Patrick McHardy | 572a9d7 | 2009-11-10 06:14:14 +0000 | [diff] [blame] | 2819 | if (dev_xmit_complete(rc)) { |
David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 2820 | HARD_TX_UNLOCK(dev, txq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | goto out; |
| 2822 | } |
| 2823 | } |
David S. Miller | c773e84 | 2008-07-08 23:13:53 -0700 | [diff] [blame] | 2824 | HARD_TX_UNLOCK(dev, txq); |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 2825 | net_crit_ratelimited("Virtual device %s asks to queue packet!\n", |
| 2826 | dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | } else { |
| 2828 | /* Recursion is detected! It is possible, |
Eric Dumazet | 745e20f | 2010-09-29 13:23:09 -0700 | [diff] [blame] | 2829 | * unfortunately |
| 2830 | */ |
| 2831 | recursion_alert: |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 2832 | net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n", |
| 2833 | dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | } |
| 2835 | } |
| 2836 | |
| 2837 | rc = -ENETDOWN; |
Herbert Xu | d4828d8 | 2006-06-22 02:28:18 -0700 | [diff] [blame] | 2838 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2839 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | kfree_skb(skb); |
| 2841 | return rc; |
| 2842 | out: |
Herbert Xu | d4828d8 | 2006-06-22 02:28:18 -0700 | [diff] [blame] | 2843 | rcu_read_unlock_bh(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | return rc; |
| 2845 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 2846 | EXPORT_SYMBOL(dev_queue_xmit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | |
| 2848 | |
| 2849 | /*======================================================================= |
| 2850 | Receiver routines |
| 2851 | =======================================================================*/ |
| 2852 | |
Stephen Hemminger | 6b2bedc | 2007-03-12 14:33:50 -0700 | [diff] [blame] | 2853 | int netdev_max_backlog __read_mostly = 1000; |
Eric Dumazet | c9e6bc6 | 2012-09-27 19:29:05 +0000 | [diff] [blame] | 2854 | EXPORT_SYMBOL(netdev_max_backlog); |
| 2855 | |
Eric Dumazet | 3b098e2 | 2010-05-15 23:57:10 -0700 | [diff] [blame] | 2856 | int netdev_tstamp_prequeue __read_mostly = 1; |
Stephen Hemminger | 6b2bedc | 2007-03-12 14:33:50 -0700 | [diff] [blame] | 2857 | int netdev_budget __read_mostly = 300; |
| 2858 | int weight_p __read_mostly = 64; /* old backlog weight */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | |
Eric Dumazet | eecfd7c | 2010-05-06 22:07:48 -0700 | [diff] [blame] | 2860 | /* Called with irq disabled */ |
| 2861 | static inline void ____napi_schedule(struct softnet_data *sd, |
| 2862 | struct napi_struct *napi) |
| 2863 | { |
| 2864 | list_add_tail(&napi->poll_list, &sd->poll_list); |
| 2865 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); |
| 2866 | } |
| 2867 | |
Eric Dumazet | df33454 | 2010-03-24 19:13:54 +0000 | [diff] [blame] | 2868 | #ifdef CONFIG_RPS |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 2869 | |
| 2870 | /* One global table that all flow-based protocols share. */ |
Eric Dumazet | 6e3f7fa | 2010-10-25 03:02:02 +0000 | [diff] [blame] | 2871 | struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly; |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 2872 | EXPORT_SYMBOL(rps_sock_flow_table); |
| 2873 | |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 2874 | struct static_key rps_needed __read_mostly; |
Eric Dumazet | adc9300 | 2011-11-17 03:13:26 +0000 | [diff] [blame] | 2875 | |
Ben Hutchings | c445477 | 2011-01-19 11:03:53 +0000 | [diff] [blame] | 2876 | static struct rps_dev_flow * |
| 2877 | set_rps_cpu(struct net_device *dev, struct sk_buff *skb, |
| 2878 | struct rps_dev_flow *rflow, u16 next_cpu) |
| 2879 | { |
Ben Hutchings | 09994d1 | 2011-10-03 04:42:46 +0000 | [diff] [blame] | 2880 | if (next_cpu != RPS_NO_CPU) { |
Ben Hutchings | c445477 | 2011-01-19 11:03:53 +0000 | [diff] [blame] | 2881 | #ifdef CONFIG_RFS_ACCEL |
| 2882 | struct netdev_rx_queue *rxqueue; |
| 2883 | struct rps_dev_flow_table *flow_table; |
| 2884 | struct rps_dev_flow *old_rflow; |
| 2885 | u32 flow_id; |
| 2886 | u16 rxq_index; |
| 2887 | int rc; |
| 2888 | |
| 2889 | /* Should we steer this flow to a different hardware queue? */ |
Ben Hutchings | 69a19ee | 2011-02-15 20:32:04 +0000 | [diff] [blame] | 2890 | if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap || |
| 2891 | !(dev->features & NETIF_F_NTUPLE)) |
Ben Hutchings | c445477 | 2011-01-19 11:03:53 +0000 | [diff] [blame] | 2892 | goto out; |
| 2893 | rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu); |
| 2894 | if (rxq_index == skb_get_rx_queue(skb)) |
| 2895 | goto out; |
| 2896 | |
| 2897 | rxqueue = dev->_rx + rxq_index; |
| 2898 | flow_table = rcu_dereference(rxqueue->rps_flow_table); |
| 2899 | if (!flow_table) |
| 2900 | goto out; |
| 2901 | flow_id = skb->rxhash & flow_table->mask; |
| 2902 | rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb, |
| 2903 | rxq_index, flow_id); |
| 2904 | if (rc < 0) |
| 2905 | goto out; |
| 2906 | old_rflow = rflow; |
| 2907 | rflow = &flow_table->flows[flow_id]; |
Ben Hutchings | c445477 | 2011-01-19 11:03:53 +0000 | [diff] [blame] | 2908 | rflow->filter = rc; |
| 2909 | if (old_rflow->filter == rflow->filter) |
| 2910 | old_rflow->filter = RPS_NO_FILTER; |
| 2911 | out: |
| 2912 | #endif |
| 2913 | rflow->last_qtail = |
Ben Hutchings | 09994d1 | 2011-10-03 04:42:46 +0000 | [diff] [blame] | 2914 | per_cpu(softnet_data, next_cpu).input_queue_head; |
Ben Hutchings | c445477 | 2011-01-19 11:03:53 +0000 | [diff] [blame] | 2915 | } |
| 2916 | |
Ben Hutchings | 09994d1 | 2011-10-03 04:42:46 +0000 | [diff] [blame] | 2917 | rflow->cpu = next_cpu; |
Ben Hutchings | c445477 | 2011-01-19 11:03:53 +0000 | [diff] [blame] | 2918 | return rflow; |
| 2919 | } |
| 2920 | |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 2921 | /* |
| 2922 | * get_rps_cpu is called from netif_receive_skb and returns the target |
| 2923 | * CPU from the RPS map of the receiving queue for a given skb. |
Eric Dumazet | b0e28f1 | 2010-04-15 00:14:07 -0700 | [diff] [blame] | 2924 | * rcu_read_lock must be held on entry. |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 2925 | */ |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 2926 | static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb, |
| 2927 | struct rps_dev_flow **rflowp) |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 2928 | { |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 2929 | struct netdev_rx_queue *rxqueue; |
Eric Dumazet | 6e3f7fa | 2010-10-25 03:02:02 +0000 | [diff] [blame] | 2930 | struct rps_map *map; |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 2931 | struct rps_dev_flow_table *flow_table; |
| 2932 | struct rps_sock_flow_table *sock_flow_table; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 2933 | int cpu = -1; |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 2934 | u16 tcpu; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 2935 | |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 2936 | if (skb_rx_queue_recorded(skb)) { |
| 2937 | u16 index = skb_get_rx_queue(skb); |
Ben Hutchings | 62fe0b4 | 2010-09-27 08:24:33 +0000 | [diff] [blame] | 2938 | if (unlikely(index >= dev->real_num_rx_queues)) { |
| 2939 | WARN_ONCE(dev->real_num_rx_queues > 1, |
| 2940 | "%s received packet on queue %u, but number " |
| 2941 | "of RX queues is %u\n", |
| 2942 | dev->name, index, dev->real_num_rx_queues); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 2943 | goto done; |
| 2944 | } |
| 2945 | rxqueue = dev->_rx + index; |
| 2946 | } else |
| 2947 | rxqueue = dev->_rx; |
| 2948 | |
Eric Dumazet | 6e3f7fa | 2010-10-25 03:02:02 +0000 | [diff] [blame] | 2949 | map = rcu_dereference(rxqueue->rps_map); |
| 2950 | if (map) { |
Tom Herbert | 8587523 | 2011-01-31 16:23:42 -0800 | [diff] [blame] | 2951 | if (map->len == 1 && |
Eric Dumazet | 33d480c | 2011-08-11 19:30:52 +0000 | [diff] [blame] | 2952 | !rcu_access_pointer(rxqueue->rps_flow_table)) { |
Changli Gao | 6febfca | 2010-09-03 23:12:37 +0000 | [diff] [blame] | 2953 | tcpu = map->cpus[0]; |
| 2954 | if (cpu_online(tcpu)) |
| 2955 | cpu = tcpu; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 2956 | goto done; |
Eric Dumazet | b249dcb | 2010-04-19 21:56:38 +0000 | [diff] [blame] | 2957 | } |
Eric Dumazet | 33d480c | 2011-08-11 19:30:52 +0000 | [diff] [blame] | 2958 | } else if (!rcu_access_pointer(rxqueue->rps_flow_table)) { |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 2959 | goto done; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 2960 | } |
| 2961 | |
Changli Gao | 2d47b45 | 2010-08-17 19:00:56 +0000 | [diff] [blame] | 2962 | skb_reset_network_header(skb); |
Krishna Kumar | bfb564e | 2010-08-04 06:15:52 +0000 | [diff] [blame] | 2963 | if (!skb_get_rxhash(skb)) |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 2964 | goto done; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 2965 | |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 2966 | flow_table = rcu_dereference(rxqueue->rps_flow_table); |
| 2967 | sock_flow_table = rcu_dereference(rps_sock_flow_table); |
| 2968 | if (flow_table && sock_flow_table) { |
| 2969 | u16 next_cpu; |
| 2970 | struct rps_dev_flow *rflow; |
| 2971 | |
| 2972 | rflow = &flow_table->flows[skb->rxhash & flow_table->mask]; |
| 2973 | tcpu = rflow->cpu; |
| 2974 | |
| 2975 | next_cpu = sock_flow_table->ents[skb->rxhash & |
| 2976 | sock_flow_table->mask]; |
| 2977 | |
| 2978 | /* |
| 2979 | * If the desired CPU (where last recvmsg was done) is |
| 2980 | * different from current CPU (one in the rx-queue flow |
| 2981 | * table entry), switch if one of the following holds: |
| 2982 | * - Current CPU is unset (equal to RPS_NO_CPU). |
| 2983 | * - Current CPU is offline. |
| 2984 | * - The current CPU's queue tail has advanced beyond the |
| 2985 | * last packet that was enqueued using this table entry. |
| 2986 | * This guarantees that all previous packets for the flow |
| 2987 | * have been dequeued, thus preserving in order delivery. |
| 2988 | */ |
| 2989 | if (unlikely(tcpu != next_cpu) && |
| 2990 | (tcpu == RPS_NO_CPU || !cpu_online(tcpu) || |
| 2991 | ((int)(per_cpu(softnet_data, tcpu).input_queue_head - |
Tom Herbert | baefa31 | 2012-11-16 09:04:15 +0000 | [diff] [blame] | 2992 | rflow->last_qtail)) >= 0)) { |
| 2993 | tcpu = next_cpu; |
Ben Hutchings | c445477 | 2011-01-19 11:03:53 +0000 | [diff] [blame] | 2994 | rflow = set_rps_cpu(dev, skb, rflow, next_cpu); |
Tom Herbert | baefa31 | 2012-11-16 09:04:15 +0000 | [diff] [blame] | 2995 | } |
Ben Hutchings | c445477 | 2011-01-19 11:03:53 +0000 | [diff] [blame] | 2996 | |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 2997 | if (tcpu != RPS_NO_CPU && cpu_online(tcpu)) { |
| 2998 | *rflowp = rflow; |
| 2999 | cpu = tcpu; |
| 3000 | goto done; |
| 3001 | } |
| 3002 | } |
| 3003 | |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3004 | if (map) { |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 3005 | tcpu = map->cpus[((u64) skb->rxhash * map->len) >> 32]; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3006 | |
| 3007 | if (cpu_online(tcpu)) { |
| 3008 | cpu = tcpu; |
| 3009 | goto done; |
| 3010 | } |
| 3011 | } |
| 3012 | |
| 3013 | done: |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3014 | return cpu; |
| 3015 | } |
| 3016 | |
Ben Hutchings | c445477 | 2011-01-19 11:03:53 +0000 | [diff] [blame] | 3017 | #ifdef CONFIG_RFS_ACCEL |
| 3018 | |
| 3019 | /** |
| 3020 | * rps_may_expire_flow - check whether an RFS hardware filter may be removed |
| 3021 | * @dev: Device on which the filter was set |
| 3022 | * @rxq_index: RX queue index |
| 3023 | * @flow_id: Flow ID passed to ndo_rx_flow_steer() |
| 3024 | * @filter_id: Filter ID returned by ndo_rx_flow_steer() |
| 3025 | * |
| 3026 | * Drivers that implement ndo_rx_flow_steer() should periodically call |
| 3027 | * this function for each installed filter and remove the filters for |
| 3028 | * which it returns %true. |
| 3029 | */ |
| 3030 | bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index, |
| 3031 | u32 flow_id, u16 filter_id) |
| 3032 | { |
| 3033 | struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index; |
| 3034 | struct rps_dev_flow_table *flow_table; |
| 3035 | struct rps_dev_flow *rflow; |
| 3036 | bool expire = true; |
| 3037 | int cpu; |
| 3038 | |
| 3039 | rcu_read_lock(); |
| 3040 | flow_table = rcu_dereference(rxqueue->rps_flow_table); |
| 3041 | if (flow_table && flow_id <= flow_table->mask) { |
| 3042 | rflow = &flow_table->flows[flow_id]; |
| 3043 | cpu = ACCESS_ONCE(rflow->cpu); |
| 3044 | if (rflow->filter == filter_id && cpu != RPS_NO_CPU && |
| 3045 | ((int)(per_cpu(softnet_data, cpu).input_queue_head - |
| 3046 | rflow->last_qtail) < |
| 3047 | (int)(10 * flow_table->mask))) |
| 3048 | expire = false; |
| 3049 | } |
| 3050 | rcu_read_unlock(); |
| 3051 | return expire; |
| 3052 | } |
| 3053 | EXPORT_SYMBOL(rps_may_expire_flow); |
| 3054 | |
| 3055 | #endif /* CONFIG_RFS_ACCEL */ |
| 3056 | |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3057 | /* Called from hardirq (IPI) context */ |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3058 | static void rps_trigger_softirq(void *data) |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3059 | { |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3060 | struct softnet_data *sd = data; |
| 3061 | |
Eric Dumazet | eecfd7c | 2010-05-06 22:07:48 -0700 | [diff] [blame] | 3062 | ____napi_schedule(sd, &sd->backlog); |
Changli Gao | dee4287 | 2010-05-02 05:42:16 +0000 | [diff] [blame] | 3063 | sd->received_rps++; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3064 | } |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3065 | |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 3066 | #endif /* CONFIG_RPS */ |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3067 | |
| 3068 | /* |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3069 | * Check if this softnet_data structure is another cpu one |
| 3070 | * If yes, queue it to our IPI list and return 1 |
| 3071 | * If no, return 0 |
| 3072 | */ |
| 3073 | static int rps_ipi_queued(struct softnet_data *sd) |
| 3074 | { |
| 3075 | #ifdef CONFIG_RPS |
| 3076 | struct softnet_data *mysd = &__get_cpu_var(softnet_data); |
| 3077 | |
| 3078 | if (sd != mysd) { |
| 3079 | sd->rps_ipi_next = mysd->rps_ipi_list; |
| 3080 | mysd->rps_ipi_list = sd; |
| 3081 | |
| 3082 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); |
| 3083 | return 1; |
| 3084 | } |
| 3085 | #endif /* CONFIG_RPS */ |
| 3086 | return 0; |
| 3087 | } |
| 3088 | |
Willem de Bruijn | 99bbc70 | 2013-05-20 04:02:32 +0000 | [diff] [blame] | 3089 | #ifdef CONFIG_NET_FLOW_LIMIT |
| 3090 | int netdev_flow_limit_table_len __read_mostly = (1 << 12); |
| 3091 | #endif |
| 3092 | |
| 3093 | static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen) |
| 3094 | { |
| 3095 | #ifdef CONFIG_NET_FLOW_LIMIT |
| 3096 | struct sd_flow_limit *fl; |
| 3097 | struct softnet_data *sd; |
| 3098 | unsigned int old_flow, new_flow; |
| 3099 | |
| 3100 | if (qlen < (netdev_max_backlog >> 1)) |
| 3101 | return false; |
| 3102 | |
| 3103 | sd = &__get_cpu_var(softnet_data); |
| 3104 | |
| 3105 | rcu_read_lock(); |
| 3106 | fl = rcu_dereference(sd->flow_limit); |
| 3107 | if (fl) { |
| 3108 | new_flow = skb_get_rxhash(skb) & (fl->num_buckets - 1); |
| 3109 | old_flow = fl->history[fl->history_head]; |
| 3110 | fl->history[fl->history_head] = new_flow; |
| 3111 | |
| 3112 | fl->history_head++; |
| 3113 | fl->history_head &= FLOW_LIMIT_HISTORY - 1; |
| 3114 | |
| 3115 | if (likely(fl->buckets[old_flow])) |
| 3116 | fl->buckets[old_flow]--; |
| 3117 | |
| 3118 | if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) { |
| 3119 | fl->count++; |
| 3120 | rcu_read_unlock(); |
| 3121 | return true; |
| 3122 | } |
| 3123 | } |
| 3124 | rcu_read_unlock(); |
| 3125 | #endif |
| 3126 | return false; |
| 3127 | } |
| 3128 | |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3129 | /* |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3130 | * enqueue_to_backlog is called to queue an skb to a per CPU backlog |
| 3131 | * queue (may be a remote CPU queue). |
| 3132 | */ |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 3133 | static int enqueue_to_backlog(struct sk_buff *skb, int cpu, |
| 3134 | unsigned int *qtail) |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3135 | { |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3136 | struct softnet_data *sd; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3137 | unsigned long flags; |
Willem de Bruijn | 99bbc70 | 2013-05-20 04:02:32 +0000 | [diff] [blame] | 3138 | unsigned int qlen; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3139 | |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3140 | sd = &per_cpu(softnet_data, cpu); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3141 | |
| 3142 | local_irq_save(flags); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3143 | |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3144 | rps_lock(sd); |
Willem de Bruijn | 99bbc70 | 2013-05-20 04:02:32 +0000 | [diff] [blame] | 3145 | qlen = skb_queue_len(&sd->input_pkt_queue); |
| 3146 | if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) { |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 3147 | if (skb_queue_len(&sd->input_pkt_queue)) { |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3148 | enqueue: |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3149 | __skb_queue_tail(&sd->input_pkt_queue, skb); |
Tom Herbert | 76cc8b1 | 2010-05-20 18:37:59 +0000 | [diff] [blame] | 3150 | input_queue_tail_incr_save(sd, qtail); |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3151 | rps_unlock(sd); |
Changli Gao | 152102c | 2010-03-30 20:16:22 +0000 | [diff] [blame] | 3152 | local_irq_restore(flags); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3153 | return NET_RX_SUCCESS; |
| 3154 | } |
| 3155 | |
Eric Dumazet | ebda37c2 | 2010-05-06 23:51:21 +0000 | [diff] [blame] | 3156 | /* Schedule NAPI for backlog device |
| 3157 | * We can use non atomic operation since we own the queue lock |
| 3158 | */ |
| 3159 | if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) { |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3160 | if (!rps_ipi_queued(sd)) |
Eric Dumazet | eecfd7c | 2010-05-06 22:07:48 -0700 | [diff] [blame] | 3161 | ____napi_schedule(sd, &sd->backlog); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3162 | } |
| 3163 | goto enqueue; |
| 3164 | } |
| 3165 | |
Changli Gao | dee4287 | 2010-05-02 05:42:16 +0000 | [diff] [blame] | 3166 | sd->dropped++; |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3167 | rps_unlock(sd); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3168 | |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3169 | local_irq_restore(flags); |
| 3170 | |
Eric Dumazet | caf586e | 2010-09-30 21:06:55 +0000 | [diff] [blame] | 3171 | atomic_long_inc(&skb->dev->rx_dropped); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3172 | kfree_skb(skb); |
| 3173 | return NET_RX_DROP; |
| 3174 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3176 | /** |
| 3177 | * netif_rx - post buffer to the network code |
| 3178 | * @skb: buffer to post |
| 3179 | * |
| 3180 | * This function receives a packet from a device driver and queues it for |
| 3181 | * the upper (protocol) levels to process. It always succeeds. The buffer |
| 3182 | * may be dropped during processing for congestion control or by the |
| 3183 | * protocol layers. |
| 3184 | * |
| 3185 | * return values: |
| 3186 | * NET_RX_SUCCESS (no congestion) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3187 | * NET_RX_DROP (packet was dropped) |
| 3188 | * |
| 3189 | */ |
| 3190 | |
| 3191 | int netif_rx(struct sk_buff *skb) |
| 3192 | { |
Eric Dumazet | b0e28f1 | 2010-04-15 00:14:07 -0700 | [diff] [blame] | 3193 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | |
| 3195 | /* if netpoll wants it, pretend we never saw it */ |
| 3196 | if (netpoll_rx(skb)) |
| 3197 | return NET_RX_DROP; |
| 3198 | |
Eric Dumazet | 588f033 | 2011-11-15 04:12:55 +0000 | [diff] [blame] | 3199 | net_timestamp_check(netdev_tstamp_prequeue, skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | |
Koki Sanagi | cf66ba5 | 2010-08-23 18:45:02 +0900 | [diff] [blame] | 3201 | trace_netif_rx(skb); |
Eric Dumazet | df33454 | 2010-03-24 19:13:54 +0000 | [diff] [blame] | 3202 | #ifdef CONFIG_RPS |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 3203 | if (static_key_false(&rps_needed)) { |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 3204 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
Eric Dumazet | b0e28f1 | 2010-04-15 00:14:07 -0700 | [diff] [blame] | 3205 | int cpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3206 | |
Changli Gao | cece194 | 2010-08-07 20:35:43 -0700 | [diff] [blame] | 3207 | preempt_disable(); |
Eric Dumazet | b0e28f1 | 2010-04-15 00:14:07 -0700 | [diff] [blame] | 3208 | rcu_read_lock(); |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 3209 | |
| 3210 | cpu = get_rps_cpu(skb->dev, skb, &rflow); |
Eric Dumazet | b0e28f1 | 2010-04-15 00:14:07 -0700 | [diff] [blame] | 3211 | if (cpu < 0) |
| 3212 | cpu = smp_processor_id(); |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 3213 | |
| 3214 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); |
| 3215 | |
Eric Dumazet | b0e28f1 | 2010-04-15 00:14:07 -0700 | [diff] [blame] | 3216 | rcu_read_unlock(); |
Changli Gao | cece194 | 2010-08-07 20:35:43 -0700 | [diff] [blame] | 3217 | preempt_enable(); |
Eric Dumazet | adc9300 | 2011-11-17 03:13:26 +0000 | [diff] [blame] | 3218 | } else |
| 3219 | #endif |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 3220 | { |
| 3221 | unsigned int qtail; |
| 3222 | ret = enqueue_to_backlog(skb, get_cpu(), &qtail); |
| 3223 | put_cpu(); |
| 3224 | } |
Eric Dumazet | b0e28f1 | 2010-04-15 00:14:07 -0700 | [diff] [blame] | 3225 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3226 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 3227 | EXPORT_SYMBOL(netif_rx); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3228 | |
| 3229 | int netif_rx_ni(struct sk_buff *skb) |
| 3230 | { |
| 3231 | int err; |
| 3232 | |
| 3233 | preempt_disable(); |
| 3234 | err = netif_rx(skb); |
| 3235 | if (local_softirq_pending()) |
| 3236 | do_softirq(); |
| 3237 | preempt_enable(); |
| 3238 | |
| 3239 | return err; |
| 3240 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3241 | EXPORT_SYMBOL(netif_rx_ni); |
| 3242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3243 | static void net_tx_action(struct softirq_action *h) |
| 3244 | { |
| 3245 | struct softnet_data *sd = &__get_cpu_var(softnet_data); |
| 3246 | |
| 3247 | if (sd->completion_queue) { |
| 3248 | struct sk_buff *clist; |
| 3249 | |
| 3250 | local_irq_disable(); |
| 3251 | clist = sd->completion_queue; |
| 3252 | sd->completion_queue = NULL; |
| 3253 | local_irq_enable(); |
| 3254 | |
| 3255 | while (clist) { |
| 3256 | struct sk_buff *skb = clist; |
| 3257 | clist = clist->next; |
| 3258 | |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 3259 | WARN_ON(atomic_read(&skb->users)); |
Koki Sanagi | 07dc22e | 2010-08-23 18:46:12 +0900 | [diff] [blame] | 3260 | trace_kfree_skb(skb, net_tx_action); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3261 | __kfree_skb(skb); |
| 3262 | } |
| 3263 | } |
| 3264 | |
| 3265 | if (sd->output_queue) { |
David S. Miller | 37437bb | 2008-07-16 02:15:04 -0700 | [diff] [blame] | 3266 | struct Qdisc *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3267 | |
| 3268 | local_irq_disable(); |
| 3269 | head = sd->output_queue; |
| 3270 | sd->output_queue = NULL; |
Changli Gao | a9cbd58 | 2010-04-26 23:06:24 +0000 | [diff] [blame] | 3271 | sd->output_queue_tailp = &sd->output_queue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3272 | local_irq_enable(); |
| 3273 | |
| 3274 | while (head) { |
David S. Miller | 37437bb | 2008-07-16 02:15:04 -0700 | [diff] [blame] | 3275 | struct Qdisc *q = head; |
| 3276 | spinlock_t *root_lock; |
| 3277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | head = head->next_sched; |
| 3279 | |
David S. Miller | 5fb6622 | 2008-08-02 20:02:43 -0700 | [diff] [blame] | 3280 | root_lock = qdisc_lock(q); |
David S. Miller | 37437bb | 2008-07-16 02:15:04 -0700 | [diff] [blame] | 3281 | if (spin_trylock(root_lock)) { |
Jarek Poplawski | def82a1 | 2008-08-17 21:54:43 -0700 | [diff] [blame] | 3282 | smp_mb__before_clear_bit(); |
| 3283 | clear_bit(__QDISC_STATE_SCHED, |
| 3284 | &q->state); |
David S. Miller | 37437bb | 2008-07-16 02:15:04 -0700 | [diff] [blame] | 3285 | qdisc_run(q); |
| 3286 | spin_unlock(root_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3287 | } else { |
David S. Miller | 195648b | 2008-08-19 04:00:36 -0700 | [diff] [blame] | 3288 | if (!test_bit(__QDISC_STATE_DEACTIVATED, |
Jarek Poplawski | e8a83e1 | 2008-09-07 18:41:21 -0700 | [diff] [blame] | 3289 | &q->state)) { |
David S. Miller | 195648b | 2008-08-19 04:00:36 -0700 | [diff] [blame] | 3290 | __netif_reschedule(q); |
Jarek Poplawski | e8a83e1 | 2008-09-07 18:41:21 -0700 | [diff] [blame] | 3291 | } else { |
| 3292 | smp_mb__before_clear_bit(); |
| 3293 | clear_bit(__QDISC_STATE_SCHED, |
| 3294 | &q->state); |
| 3295 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3296 | } |
| 3297 | } |
| 3298 | } |
| 3299 | } |
| 3300 | |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 3301 | #if (defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)) && \ |
| 3302 | (defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE)) |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 3303 | /* This hook is defined here for ATM LANE */ |
| 3304 | int (*br_fdb_test_addr_hook)(struct net_device *dev, |
| 3305 | unsigned char *addr) __read_mostly; |
Stephen Hemminger | 4fb019a | 2009-09-11 11:50:08 -0700 | [diff] [blame] | 3306 | EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook); |
Michał Mirosław | da67829 | 2009-06-05 05:35:28 +0000 | [diff] [blame] | 3307 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3309 | #ifdef CONFIG_NET_CLS_ACT |
| 3310 | /* TODO: Maybe we should just force sch_ingress to be compiled in |
| 3311 | * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions |
| 3312 | * a compare and 2 stores extra right now if we dont have it on |
| 3313 | * but have CONFIG_NET_CLS_ACT |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 3314 | * NOTE: This doesn't stop any functionality; if you dont have |
| 3315 | * the ingress scheduler, you just can't add policies on ingress. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | * |
| 3317 | */ |
Eric Dumazet | 24824a0 | 2010-10-02 06:11:55 +0000 | [diff] [blame] | 3318 | static int ing_filter(struct sk_buff *skb, struct netdev_queue *rxq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3319 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | struct net_device *dev = skb->dev; |
Herbert Xu | f697c3e | 2007-10-14 00:38:47 -0700 | [diff] [blame] | 3321 | u32 ttl = G_TC_RTTL(skb->tc_verd); |
David S. Miller | 555353c | 2008-07-08 17:33:13 -0700 | [diff] [blame] | 3322 | int result = TC_ACT_OK; |
| 3323 | struct Qdisc *q; |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 3324 | |
Stephen Hemminger | de38483 | 2010-08-01 00:33:23 -0700 | [diff] [blame] | 3325 | if (unlikely(MAX_RED_LOOP < ttl++)) { |
Joe Perches | e87cc47 | 2012-05-13 21:56:26 +0000 | [diff] [blame] | 3326 | net_warn_ratelimited("Redir loop detected Dropping packet (%d->%d)\n", |
| 3327 | skb->skb_iif, dev->ifindex); |
Herbert Xu | f697c3e | 2007-10-14 00:38:47 -0700 | [diff] [blame] | 3328 | return TC_ACT_SHOT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | } |
| 3330 | |
Herbert Xu | f697c3e | 2007-10-14 00:38:47 -0700 | [diff] [blame] | 3331 | skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl); |
| 3332 | skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS); |
| 3333 | |
David S. Miller | 8387400 | 2008-07-17 00:53:03 -0700 | [diff] [blame] | 3334 | q = rxq->qdisc; |
David S. Miller | 8d50b53 | 2008-07-30 02:37:46 -0700 | [diff] [blame] | 3335 | if (q != &noop_qdisc) { |
David S. Miller | 8387400 | 2008-07-17 00:53:03 -0700 | [diff] [blame] | 3336 | spin_lock(qdisc_lock(q)); |
David S. Miller | a9312ae | 2008-08-17 21:51:03 -0700 | [diff] [blame] | 3337 | if (likely(!test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) |
| 3338 | result = qdisc_enqueue_root(skb, q); |
David S. Miller | 8387400 | 2008-07-17 00:53:03 -0700 | [diff] [blame] | 3339 | spin_unlock(qdisc_lock(q)); |
| 3340 | } |
Herbert Xu | f697c3e | 2007-10-14 00:38:47 -0700 | [diff] [blame] | 3341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3342 | return result; |
| 3343 | } |
Herbert Xu | f697c3e | 2007-10-14 00:38:47 -0700 | [diff] [blame] | 3344 | |
| 3345 | static inline struct sk_buff *handle_ing(struct sk_buff *skb, |
| 3346 | struct packet_type **pt_prev, |
| 3347 | int *ret, struct net_device *orig_dev) |
| 3348 | { |
Eric Dumazet | 24824a0 | 2010-10-02 06:11:55 +0000 | [diff] [blame] | 3349 | struct netdev_queue *rxq = rcu_dereference(skb->dev->ingress_queue); |
| 3350 | |
| 3351 | if (!rxq || rxq->qdisc == &noop_qdisc) |
Herbert Xu | f697c3e | 2007-10-14 00:38:47 -0700 | [diff] [blame] | 3352 | goto out; |
| 3353 | |
| 3354 | if (*pt_prev) { |
| 3355 | *ret = deliver_skb(skb, *pt_prev, orig_dev); |
| 3356 | *pt_prev = NULL; |
Herbert Xu | f697c3e | 2007-10-14 00:38:47 -0700 | [diff] [blame] | 3357 | } |
| 3358 | |
Eric Dumazet | 24824a0 | 2010-10-02 06:11:55 +0000 | [diff] [blame] | 3359 | switch (ing_filter(skb, rxq)) { |
Herbert Xu | f697c3e | 2007-10-14 00:38:47 -0700 | [diff] [blame] | 3360 | case TC_ACT_SHOT: |
| 3361 | case TC_ACT_STOLEN: |
| 3362 | kfree_skb(skb); |
| 3363 | return NULL; |
| 3364 | } |
| 3365 | |
| 3366 | out: |
| 3367 | skb->tc_verd = 0; |
| 3368 | return skb; |
| 3369 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3370 | #endif |
| 3371 | |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 3372 | /** |
| 3373 | * netdev_rx_handler_register - register receive handler |
| 3374 | * @dev: device to register a handler for |
| 3375 | * @rx_handler: receive handler to register |
Jiri Pirko | 93e2c32 | 2010-06-10 03:34:59 +0000 | [diff] [blame] | 3376 | * @rx_handler_data: data pointer that is used by rx handler |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 3377 | * |
| 3378 | * Register a receive hander for a device. This handler will then be |
| 3379 | * called from __netif_receive_skb. A negative errno code is returned |
| 3380 | * on a failure. |
| 3381 | * |
| 3382 | * The caller must hold the rtnl_mutex. |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 3383 | * |
| 3384 | * For a general description of rx_handler, see enum rx_handler_result. |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 3385 | */ |
| 3386 | int netdev_rx_handler_register(struct net_device *dev, |
Jiri Pirko | 93e2c32 | 2010-06-10 03:34:59 +0000 | [diff] [blame] | 3387 | rx_handler_func_t *rx_handler, |
| 3388 | void *rx_handler_data) |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 3389 | { |
| 3390 | ASSERT_RTNL(); |
| 3391 | |
| 3392 | if (dev->rx_handler) |
| 3393 | return -EBUSY; |
| 3394 | |
Eric Dumazet | 00cfec3 | 2013-03-29 03:01:22 +0000 | [diff] [blame] | 3395 | /* Note: rx_handler_data must be set before rx_handler */ |
Jiri Pirko | 93e2c32 | 2010-06-10 03:34:59 +0000 | [diff] [blame] | 3396 | rcu_assign_pointer(dev->rx_handler_data, rx_handler_data); |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 3397 | rcu_assign_pointer(dev->rx_handler, rx_handler); |
| 3398 | |
| 3399 | return 0; |
| 3400 | } |
| 3401 | EXPORT_SYMBOL_GPL(netdev_rx_handler_register); |
| 3402 | |
| 3403 | /** |
| 3404 | * netdev_rx_handler_unregister - unregister receive handler |
| 3405 | * @dev: device to unregister a handler from |
| 3406 | * |
Kusanagi Kouichi | 166ec36 | 2013-03-18 02:59:52 +0000 | [diff] [blame] | 3407 | * Unregister a receive handler from a device. |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 3408 | * |
| 3409 | * The caller must hold the rtnl_mutex. |
| 3410 | */ |
| 3411 | void netdev_rx_handler_unregister(struct net_device *dev) |
| 3412 | { |
| 3413 | |
| 3414 | ASSERT_RTNL(); |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 3415 | RCU_INIT_POINTER(dev->rx_handler, NULL); |
Eric Dumazet | 00cfec3 | 2013-03-29 03:01:22 +0000 | [diff] [blame] | 3416 | /* a reader seeing a non NULL rx_handler in a rcu_read_lock() |
| 3417 | * section has a guarantee to see a non NULL rx_handler_data |
| 3418 | * as well. |
| 3419 | */ |
| 3420 | synchronize_net(); |
Stephen Hemminger | a9b3cd7 | 2011-08-01 16:19:00 +0000 | [diff] [blame] | 3421 | RCU_INIT_POINTER(dev->rx_handler_data, NULL); |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 3422 | } |
| 3423 | EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister); |
| 3424 | |
Mel Gorman | b4b9e35 | 2012-07-31 16:44:26 -0700 | [diff] [blame] | 3425 | /* |
| 3426 | * Limit the use of PFMEMALLOC reserves to those protocols that implement |
| 3427 | * the special handling of PFMEMALLOC skbs. |
| 3428 | */ |
| 3429 | static bool skb_pfmemalloc_protocol(struct sk_buff *skb) |
| 3430 | { |
| 3431 | switch (skb->protocol) { |
| 3432 | case __constant_htons(ETH_P_ARP): |
| 3433 | case __constant_htons(ETH_P_IP): |
| 3434 | case __constant_htons(ETH_P_IPV6): |
| 3435 | case __constant_htons(ETH_P_8021Q): |
Patrick McHardy | 8ad227f | 2013-04-19 02:04:31 +0000 | [diff] [blame] | 3436 | case __constant_htons(ETH_P_8021AD): |
Mel Gorman | b4b9e35 | 2012-07-31 16:44:26 -0700 | [diff] [blame] | 3437 | return true; |
| 3438 | default: |
| 3439 | return false; |
| 3440 | } |
| 3441 | } |
| 3442 | |
David S. Miller | 9754e29 | 2013-02-14 15:57:38 -0500 | [diff] [blame] | 3443 | static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3444 | { |
| 3445 | struct packet_type *ptype, *pt_prev; |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 3446 | rx_handler_func_t *rx_handler; |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 3447 | struct net_device *orig_dev; |
David S. Miller | 63d8ea7 | 2011-02-28 10:48:59 -0800 | [diff] [blame] | 3448 | struct net_device *null_or_dev; |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 3449 | bool deliver_exact = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3450 | int ret = NET_RX_DROP; |
Al Viro | 252e334 | 2006-11-14 20:48:11 -0800 | [diff] [blame] | 3451 | __be16 type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | |
Eric Dumazet | 588f033 | 2011-11-15 04:12:55 +0000 | [diff] [blame] | 3453 | net_timestamp_check(!netdev_tstamp_prequeue, skb); |
Eric Dumazet | 81bbb3d | 2009-09-30 16:42:42 -0700 | [diff] [blame] | 3454 | |
Koki Sanagi | cf66ba5 | 2010-08-23 18:45:02 +0900 | [diff] [blame] | 3455 | trace_netif_receive_skb(skb); |
Patrick McHardy | 9b22ea5 | 2008-11-04 14:49:57 -0800 | [diff] [blame] | 3456 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | /* if we've gotten here through NAPI, check netpoll */ |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 3458 | if (netpoll_receive_skb(skb)) |
Mel Gorman | b4b9e35 | 2012-07-31 16:44:26 -0700 | [diff] [blame] | 3459 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | |
Joe Eykholt | cc9bd5c | 2008-07-02 18:22:00 -0700 | [diff] [blame] | 3461 | orig_dev = skb->dev; |
Jiri Pirko | 1765a57 | 2011-02-12 06:48:36 +0000 | [diff] [blame] | 3462 | |
Arnaldo Carvalho de Melo | c1d2bbe | 2007-04-10 20:45:18 -0700 | [diff] [blame] | 3463 | skb_reset_network_header(skb); |
Eric Dumazet | fda55ec | 2013-01-07 09:28:21 +0000 | [diff] [blame] | 3464 | if (!skb_transport_header_was_set(skb)) |
| 3465 | skb_reset_transport_header(skb); |
Jiri Pirko | 0b5c9db | 2011-06-10 06:56:58 +0000 | [diff] [blame] | 3466 | skb_reset_mac_len(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3467 | |
| 3468 | pt_prev = NULL; |
| 3469 | |
| 3470 | rcu_read_lock(); |
| 3471 | |
David S. Miller | 63d8ea7 | 2011-02-28 10:48:59 -0800 | [diff] [blame] | 3472 | another_round: |
David S. Miller | b685817 | 2012-07-23 16:27:54 -0700 | [diff] [blame] | 3473 | skb->skb_iif = skb->dev->ifindex; |
David S. Miller | 63d8ea7 | 2011-02-28 10:48:59 -0800 | [diff] [blame] | 3474 | |
| 3475 | __this_cpu_inc(softnet_data.processed); |
| 3476 | |
Patrick McHardy | 8ad227f | 2013-04-19 02:04:31 +0000 | [diff] [blame] | 3477 | if (skb->protocol == cpu_to_be16(ETH_P_8021Q) || |
| 3478 | skb->protocol == cpu_to_be16(ETH_P_8021AD)) { |
Jiri Pirko | bcc6d47 | 2011-04-07 19:48:33 +0000 | [diff] [blame] | 3479 | skb = vlan_untag(skb); |
| 3480 | if (unlikely(!skb)) |
Mel Gorman | b4b9e35 | 2012-07-31 16:44:26 -0700 | [diff] [blame] | 3481 | goto unlock; |
Jiri Pirko | bcc6d47 | 2011-04-07 19:48:33 +0000 | [diff] [blame] | 3482 | } |
| 3483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | #ifdef CONFIG_NET_CLS_ACT |
| 3485 | if (skb->tc_verd & TC_NCLS) { |
| 3486 | skb->tc_verd = CLR_TC_NCLS(skb->tc_verd); |
| 3487 | goto ncls; |
| 3488 | } |
| 3489 | #endif |
| 3490 | |
David S. Miller | 9754e29 | 2013-02-14 15:57:38 -0500 | [diff] [blame] | 3491 | if (pfmemalloc) |
Mel Gorman | b4b9e35 | 2012-07-31 16:44:26 -0700 | [diff] [blame] | 3492 | goto skip_taps; |
| 3493 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3494 | list_for_each_entry_rcu(ptype, &ptype_all, list) { |
David S. Miller | 63d8ea7 | 2011-02-28 10:48:59 -0800 | [diff] [blame] | 3495 | if (!ptype->dev || ptype->dev == skb->dev) { |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 3496 | if (pt_prev) |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 3497 | ret = deliver_skb(skb, pt_prev, orig_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3498 | pt_prev = ptype; |
| 3499 | } |
| 3500 | } |
| 3501 | |
Mel Gorman | b4b9e35 | 2012-07-31 16:44:26 -0700 | [diff] [blame] | 3502 | skip_taps: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3503 | #ifdef CONFIG_NET_CLS_ACT |
Herbert Xu | f697c3e | 2007-10-14 00:38:47 -0700 | [diff] [blame] | 3504 | skb = handle_ing(skb, &pt_prev, &ret, orig_dev); |
| 3505 | if (!skb) |
Mel Gorman | b4b9e35 | 2012-07-31 16:44:26 -0700 | [diff] [blame] | 3506 | goto unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3507 | ncls: |
| 3508 | #endif |
| 3509 | |
David S. Miller | 9754e29 | 2013-02-14 15:57:38 -0500 | [diff] [blame] | 3510 | if (pfmemalloc && !skb_pfmemalloc_protocol(skb)) |
Mel Gorman | b4b9e35 | 2012-07-31 16:44:26 -0700 | [diff] [blame] | 3511 | goto drop; |
| 3512 | |
John Fastabend | 2425717 | 2011-10-10 09:16:41 +0000 | [diff] [blame] | 3513 | if (vlan_tx_tag_present(skb)) { |
| 3514 | if (pt_prev) { |
| 3515 | ret = deliver_skb(skb, pt_prev, orig_dev); |
| 3516 | pt_prev = NULL; |
| 3517 | } |
Florian Zumbiehl | 48cc32d3 | 2012-10-07 15:51:58 +0000 | [diff] [blame] | 3518 | if (vlan_do_receive(&skb)) |
John Fastabend | 2425717 | 2011-10-10 09:16:41 +0000 | [diff] [blame] | 3519 | goto another_round; |
| 3520 | else if (unlikely(!skb)) |
Mel Gorman | b4b9e35 | 2012-07-31 16:44:26 -0700 | [diff] [blame] | 3521 | goto unlock; |
John Fastabend | 2425717 | 2011-10-10 09:16:41 +0000 | [diff] [blame] | 3522 | } |
| 3523 | |
Florian Zumbiehl | 48cc32d3 | 2012-10-07 15:51:58 +0000 | [diff] [blame] | 3524 | rx_handler = rcu_dereference(skb->dev->rx_handler); |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 3525 | if (rx_handler) { |
| 3526 | if (pt_prev) { |
| 3527 | ret = deliver_skb(skb, pt_prev, orig_dev); |
| 3528 | pt_prev = NULL; |
| 3529 | } |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 3530 | switch (rx_handler(&skb)) { |
| 3531 | case RX_HANDLER_CONSUMED: |
Cristian Bercaru | 3bc1b1a | 2013-03-08 07:03:38 +0000 | [diff] [blame] | 3532 | ret = NET_RX_SUCCESS; |
Mel Gorman | b4b9e35 | 2012-07-31 16:44:26 -0700 | [diff] [blame] | 3533 | goto unlock; |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 3534 | case RX_HANDLER_ANOTHER: |
David S. Miller | 63d8ea7 | 2011-02-28 10:48:59 -0800 | [diff] [blame] | 3535 | goto another_round; |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 3536 | case RX_HANDLER_EXACT: |
| 3537 | deliver_exact = true; |
| 3538 | case RX_HANDLER_PASS: |
| 3539 | break; |
| 3540 | default: |
| 3541 | BUG(); |
| 3542 | } |
Jiri Pirko | ab95bfe | 2010-06-01 21:52:08 +0000 | [diff] [blame] | 3543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3544 | |
Florian Zumbiehl | 48cc32d3 | 2012-10-07 15:51:58 +0000 | [diff] [blame] | 3545 | if (vlan_tx_nonzero_tag_present(skb)) |
| 3546 | skb->pkt_type = PACKET_OTHERHOST; |
| 3547 | |
David S. Miller | 63d8ea7 | 2011-02-28 10:48:59 -0800 | [diff] [blame] | 3548 | /* deliver only exact match when indicated */ |
Jiri Pirko | 8a4eb57 | 2011-03-12 03:14:39 +0000 | [diff] [blame] | 3549 | null_or_dev = deliver_exact ? skb->dev : NULL; |
Andy Gospodarek | 1f3c880 | 2009-12-14 10:48:58 +0000 | [diff] [blame] | 3550 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3551 | type = skb->protocol; |
Pavel Emelyanov | 82d8a867 | 2007-11-26 20:12:58 +0800 | [diff] [blame] | 3552 | list_for_each_entry_rcu(ptype, |
| 3553 | &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) { |
David S. Miller | 63d8ea7 | 2011-02-28 10:48:59 -0800 | [diff] [blame] | 3554 | if (ptype->type == type && |
Jiri Pirko | e3f48d3 | 2011-02-28 20:26:31 +0000 | [diff] [blame] | 3555 | (ptype->dev == null_or_dev || ptype->dev == skb->dev || |
| 3556 | ptype->dev == orig_dev)) { |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 3557 | if (pt_prev) |
David S. Miller | f2ccd8f | 2005-08-09 19:34:12 -0700 | [diff] [blame] | 3558 | ret = deliver_skb(skb, pt_prev, orig_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3559 | pt_prev = ptype; |
| 3560 | } |
| 3561 | } |
| 3562 | |
| 3563 | if (pt_prev) { |
Michael S. Tsirkin | 1080e51 | 2012-07-20 09:23:17 +0000 | [diff] [blame] | 3564 | if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) |
Michael S. Tsirkin | 0e698bf | 2012-09-15 22:44:16 +0000 | [diff] [blame] | 3565 | goto drop; |
Michael S. Tsirkin | 1080e51 | 2012-07-20 09:23:17 +0000 | [diff] [blame] | 3566 | else |
| 3567 | ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3568 | } else { |
Mel Gorman | b4b9e35 | 2012-07-31 16:44:26 -0700 | [diff] [blame] | 3569 | drop: |
Eric Dumazet | caf586e | 2010-09-30 21:06:55 +0000 | [diff] [blame] | 3570 | atomic_long_inc(&skb->dev->rx_dropped); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3571 | kfree_skb(skb); |
| 3572 | /* Jamal, now you will not able to escape explaining |
| 3573 | * me how you were going to use this. :-) |
| 3574 | */ |
| 3575 | ret = NET_RX_DROP; |
| 3576 | } |
| 3577 | |
Mel Gorman | b4b9e35 | 2012-07-31 16:44:26 -0700 | [diff] [blame] | 3578 | unlock: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3579 | rcu_read_unlock(); |
Mel Gorman | b4b9e35 | 2012-07-31 16:44:26 -0700 | [diff] [blame] | 3580 | out: |
David S. Miller | 9754e29 | 2013-02-14 15:57:38 -0500 | [diff] [blame] | 3581 | return ret; |
| 3582 | } |
| 3583 | |
| 3584 | static int __netif_receive_skb(struct sk_buff *skb) |
| 3585 | { |
| 3586 | int ret; |
| 3587 | |
| 3588 | if (sk_memalloc_socks() && skb_pfmemalloc(skb)) { |
| 3589 | unsigned long pflags = current->flags; |
| 3590 | |
| 3591 | /* |
| 3592 | * PFMEMALLOC skbs are special, they should |
| 3593 | * - be delivered to SOCK_MEMALLOC sockets only |
| 3594 | * - stay away from userspace |
| 3595 | * - have bounded memory usage |
| 3596 | * |
| 3597 | * Use PF_MEMALLOC as this saves us from propagating the allocation |
| 3598 | * context down to all allocation sites. |
| 3599 | */ |
| 3600 | current->flags |= PF_MEMALLOC; |
| 3601 | ret = __netif_receive_skb_core(skb, true); |
| 3602 | tsk_restore_flags(current, pflags, PF_MEMALLOC); |
| 3603 | } else |
| 3604 | ret = __netif_receive_skb_core(skb, false); |
| 3605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3606 | return ret; |
| 3607 | } |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3608 | |
| 3609 | /** |
| 3610 | * netif_receive_skb - process receive buffer from network |
| 3611 | * @skb: buffer to process |
| 3612 | * |
| 3613 | * netif_receive_skb() is the main receive data processing function. |
| 3614 | * It always succeeds. The buffer may be dropped during processing |
| 3615 | * for congestion control or by the protocol layers. |
| 3616 | * |
| 3617 | * This function may only be called from softirq context and interrupts |
| 3618 | * should be enabled. |
| 3619 | * |
| 3620 | * Return values (usually ignored): |
| 3621 | * NET_RX_SUCCESS: no congestion |
| 3622 | * NET_RX_DROP: packet was dropped |
| 3623 | */ |
| 3624 | int netif_receive_skb(struct sk_buff *skb) |
| 3625 | { |
Eric Dumazet | 588f033 | 2011-11-15 04:12:55 +0000 | [diff] [blame] | 3626 | net_timestamp_check(netdev_tstamp_prequeue, skb); |
Eric Dumazet | 3b098e2 | 2010-05-15 23:57:10 -0700 | [diff] [blame] | 3627 | |
Richard Cochran | c1f19b5 | 2010-07-17 08:49:36 +0000 | [diff] [blame] | 3628 | if (skb_defer_rx_timestamp(skb)) |
| 3629 | return NET_RX_SUCCESS; |
| 3630 | |
Eric Dumazet | df33454 | 2010-03-24 19:13:54 +0000 | [diff] [blame] | 3631 | #ifdef CONFIG_RPS |
Ingo Molnar | c5905af | 2012-02-24 08:31:31 +0100 | [diff] [blame] | 3632 | if (static_key_false(&rps_needed)) { |
Eric Dumazet | 3b098e2 | 2010-05-15 23:57:10 -0700 | [diff] [blame] | 3633 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
| 3634 | int cpu, ret; |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3635 | |
Eric Dumazet | 3b098e2 | 2010-05-15 23:57:10 -0700 | [diff] [blame] | 3636 | rcu_read_lock(); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3637 | |
Eric Dumazet | 3b098e2 | 2010-05-15 23:57:10 -0700 | [diff] [blame] | 3638 | cpu = get_rps_cpu(skb->dev, skb, &rflow); |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 3639 | |
Eric Dumazet | 3b098e2 | 2010-05-15 23:57:10 -0700 | [diff] [blame] | 3640 | if (cpu >= 0) { |
| 3641 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); |
| 3642 | rcu_read_unlock(); |
Eric Dumazet | adc9300 | 2011-11-17 03:13:26 +0000 | [diff] [blame] | 3643 | return ret; |
Eric Dumazet | 3b098e2 | 2010-05-15 23:57:10 -0700 | [diff] [blame] | 3644 | } |
Eric Dumazet | adc9300 | 2011-11-17 03:13:26 +0000 | [diff] [blame] | 3645 | rcu_read_unlock(); |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 3646 | } |
Tom Herbert | 1e94d72 | 2010-03-18 17:45:44 -0700 | [diff] [blame] | 3647 | #endif |
Eric Dumazet | adc9300 | 2011-11-17 03:13:26 +0000 | [diff] [blame] | 3648 | return __netif_receive_skb(skb); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 3649 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 3650 | EXPORT_SYMBOL(netif_receive_skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3651 | |
Eric Dumazet | 8875127 | 2010-04-19 05:07:33 +0000 | [diff] [blame] | 3652 | /* Network device is going away, flush any packets still pending |
| 3653 | * Called with irqs disabled. |
| 3654 | */ |
Changli Gao | 152102c | 2010-03-30 20:16:22 +0000 | [diff] [blame] | 3655 | static void flush_backlog(void *arg) |
Stephen Hemminger | 6e583ce | 2008-08-03 21:29:57 -0700 | [diff] [blame] | 3656 | { |
Changli Gao | 152102c | 2010-03-30 20:16:22 +0000 | [diff] [blame] | 3657 | struct net_device *dev = arg; |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3658 | struct softnet_data *sd = &__get_cpu_var(softnet_data); |
Stephen Hemminger | 6e583ce | 2008-08-03 21:29:57 -0700 | [diff] [blame] | 3659 | struct sk_buff *skb, *tmp; |
| 3660 | |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3661 | rps_lock(sd); |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 3662 | skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) { |
Stephen Hemminger | 6e583ce | 2008-08-03 21:29:57 -0700 | [diff] [blame] | 3663 | if (skb->dev == dev) { |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3664 | __skb_unlink(skb, &sd->input_pkt_queue); |
Stephen Hemminger | 6e583ce | 2008-08-03 21:29:57 -0700 | [diff] [blame] | 3665 | kfree_skb(skb); |
Tom Herbert | 76cc8b1 | 2010-05-20 18:37:59 +0000 | [diff] [blame] | 3666 | input_queue_head_incr(sd); |
Stephen Hemminger | 6e583ce | 2008-08-03 21:29:57 -0700 | [diff] [blame] | 3667 | } |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 3668 | } |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 3669 | rps_unlock(sd); |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 3670 | |
| 3671 | skb_queue_walk_safe(&sd->process_queue, skb, tmp) { |
| 3672 | if (skb->dev == dev) { |
| 3673 | __skb_unlink(skb, &sd->process_queue); |
| 3674 | kfree_skb(skb); |
Tom Herbert | 76cc8b1 | 2010-05-20 18:37:59 +0000 | [diff] [blame] | 3675 | input_queue_head_incr(sd); |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 3676 | } |
| 3677 | } |
Stephen Hemminger | 6e583ce | 2008-08-03 21:29:57 -0700 | [diff] [blame] | 3678 | } |
| 3679 | |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3680 | static int napi_gro_complete(struct sk_buff *skb) |
| 3681 | { |
Vlad Yasevich | 22061d8 | 2012-11-15 08:49:11 +0000 | [diff] [blame] | 3682 | struct packet_offload *ptype; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3683 | __be16 type = skb->protocol; |
Vlad Yasevich | 22061d8 | 2012-11-15 08:49:11 +0000 | [diff] [blame] | 3684 | struct list_head *head = &offload_base; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3685 | int err = -ENOENT; |
| 3686 | |
Eric Dumazet | c3c7c25 | 2012-12-06 13:54:59 +0000 | [diff] [blame] | 3687 | BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb)); |
| 3688 | |
Herbert Xu | fc59f9a | 2009-04-14 15:11:06 -0700 | [diff] [blame] | 3689 | if (NAPI_GRO_CB(skb)->count == 1) { |
| 3690 | skb_shinfo(skb)->gso_size = 0; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3691 | goto out; |
Herbert Xu | fc59f9a | 2009-04-14 15:11:06 -0700 | [diff] [blame] | 3692 | } |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3693 | |
| 3694 | rcu_read_lock(); |
| 3695 | list_for_each_entry_rcu(ptype, head, list) { |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 3696 | if (ptype->type != type || !ptype->callbacks.gro_complete) |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3697 | continue; |
| 3698 | |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 3699 | err = ptype->callbacks.gro_complete(skb); |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3700 | break; |
| 3701 | } |
| 3702 | rcu_read_unlock(); |
| 3703 | |
| 3704 | if (err) { |
| 3705 | WARN_ON(&ptype->list == head); |
| 3706 | kfree_skb(skb); |
| 3707 | return NET_RX_SUCCESS; |
| 3708 | } |
| 3709 | |
| 3710 | out: |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3711 | return netif_receive_skb(skb); |
| 3712 | } |
| 3713 | |
Eric Dumazet | 2e71a6f | 2012-10-06 08:08:49 +0000 | [diff] [blame] | 3714 | /* napi->gro_list contains packets ordered by age. |
| 3715 | * youngest packets at the head of it. |
| 3716 | * Complete skbs in reverse order to reduce latencies. |
| 3717 | */ |
| 3718 | void napi_gro_flush(struct napi_struct *napi, bool flush_old) |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3719 | { |
Eric Dumazet | 2e71a6f | 2012-10-06 08:08:49 +0000 | [diff] [blame] | 3720 | struct sk_buff *skb, *prev = NULL; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3721 | |
Eric Dumazet | 2e71a6f | 2012-10-06 08:08:49 +0000 | [diff] [blame] | 3722 | /* scan list and build reverse chain */ |
| 3723 | for (skb = napi->gro_list; skb != NULL; skb = skb->next) { |
| 3724 | skb->prev = prev; |
| 3725 | prev = skb; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3726 | } |
| 3727 | |
Eric Dumazet | 2e71a6f | 2012-10-06 08:08:49 +0000 | [diff] [blame] | 3728 | for (skb = prev; skb; skb = prev) { |
| 3729 | skb->next = NULL; |
| 3730 | |
| 3731 | if (flush_old && NAPI_GRO_CB(skb)->age == jiffies) |
| 3732 | return; |
| 3733 | |
| 3734 | prev = skb->prev; |
| 3735 | napi_gro_complete(skb); |
| 3736 | napi->gro_count--; |
| 3737 | } |
| 3738 | |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3739 | napi->gro_list = NULL; |
| 3740 | } |
Eric Dumazet | 86cac58 | 2010-08-31 18:25:32 +0000 | [diff] [blame] | 3741 | EXPORT_SYMBOL(napi_gro_flush); |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3742 | |
Eric Dumazet | 89c5fa3 | 2012-12-10 13:28:16 +0000 | [diff] [blame] | 3743 | static void gro_list_prepare(struct napi_struct *napi, struct sk_buff *skb) |
| 3744 | { |
| 3745 | struct sk_buff *p; |
| 3746 | unsigned int maclen = skb->dev->hard_header_len; |
| 3747 | |
| 3748 | for (p = napi->gro_list; p; p = p->next) { |
| 3749 | unsigned long diffs; |
| 3750 | |
| 3751 | diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev; |
| 3752 | diffs |= p->vlan_tci ^ skb->vlan_tci; |
| 3753 | if (maclen == ETH_HLEN) |
| 3754 | diffs |= compare_ether_header(skb_mac_header(p), |
| 3755 | skb_gro_mac_header(skb)); |
| 3756 | else if (!diffs) |
| 3757 | diffs = memcmp(skb_mac_header(p), |
| 3758 | skb_gro_mac_header(skb), |
| 3759 | maclen); |
| 3760 | NAPI_GRO_CB(p)->same_flow = !diffs; |
| 3761 | NAPI_GRO_CB(p)->flush = 0; |
| 3762 | } |
| 3763 | } |
| 3764 | |
Rami Rosen | bb72882 | 2012-11-28 21:55:25 +0000 | [diff] [blame] | 3765 | static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb) |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3766 | { |
| 3767 | struct sk_buff **pp = NULL; |
Vlad Yasevich | 22061d8 | 2012-11-15 08:49:11 +0000 | [diff] [blame] | 3768 | struct packet_offload *ptype; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3769 | __be16 type = skb->protocol; |
Vlad Yasevich | 22061d8 | 2012-11-15 08:49:11 +0000 | [diff] [blame] | 3770 | struct list_head *head = &offload_base; |
Herbert Xu | 0da2afd5 | 2008-12-26 14:57:42 -0800 | [diff] [blame] | 3771 | int same_flow; |
Ben Hutchings | 5b252f0 | 2009-10-29 07:17:09 +0000 | [diff] [blame] | 3772 | enum gro_result ret; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3773 | |
Jarek Poplawski | ce9e76c | 2010-08-05 01:19:11 +0000 | [diff] [blame] | 3774 | if (!(skb->dev->features & NETIF_F_GRO) || netpoll_rx_on(skb)) |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3775 | goto normal; |
| 3776 | |
David S. Miller | 21dc330 | 2010-08-23 00:13:46 -0700 | [diff] [blame] | 3777 | if (skb_is_gso(skb) || skb_has_frag_list(skb)) |
Herbert Xu | f17f5c9 | 2009-01-14 14:36:12 -0800 | [diff] [blame] | 3778 | goto normal; |
| 3779 | |
Eric Dumazet | 89c5fa3 | 2012-12-10 13:28:16 +0000 | [diff] [blame] | 3780 | gro_list_prepare(napi, skb); |
| 3781 | |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3782 | rcu_read_lock(); |
| 3783 | list_for_each_entry_rcu(ptype, head, list) { |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 3784 | if (ptype->type != type || !ptype->callbacks.gro_receive) |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3785 | continue; |
| 3786 | |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3787 | skb_set_network_header(skb, skb_gro_offset(skb)); |
Eric Dumazet | efd9450 | 2013-02-14 17:31:48 +0000 | [diff] [blame] | 3788 | skb_reset_mac_len(skb); |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3789 | NAPI_GRO_CB(skb)->same_flow = 0; |
| 3790 | NAPI_GRO_CB(skb)->flush = 0; |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3791 | NAPI_GRO_CB(skb)->free = 0; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3792 | |
Vlad Yasevich | f191a1d | 2012-11-15 08:49:23 +0000 | [diff] [blame] | 3793 | pp = ptype->callbacks.gro_receive(&napi->gro_list, skb); |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3794 | break; |
| 3795 | } |
| 3796 | rcu_read_unlock(); |
| 3797 | |
| 3798 | if (&ptype->list == head) |
| 3799 | goto normal; |
| 3800 | |
Herbert Xu | 0da2afd5 | 2008-12-26 14:57:42 -0800 | [diff] [blame] | 3801 | same_flow = NAPI_GRO_CB(skb)->same_flow; |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3802 | ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED; |
Herbert Xu | 0da2afd5 | 2008-12-26 14:57:42 -0800 | [diff] [blame] | 3803 | |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3804 | if (pp) { |
| 3805 | struct sk_buff *nskb = *pp; |
| 3806 | |
| 3807 | *pp = nskb->next; |
| 3808 | nskb->next = NULL; |
| 3809 | napi_gro_complete(nskb); |
Herbert Xu | 4ae5544 | 2009-02-08 18:00:36 +0000 | [diff] [blame] | 3810 | napi->gro_count--; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3811 | } |
| 3812 | |
Herbert Xu | 0da2afd5 | 2008-12-26 14:57:42 -0800 | [diff] [blame] | 3813 | if (same_flow) |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3814 | goto ok; |
| 3815 | |
Herbert Xu | 4ae5544 | 2009-02-08 18:00:36 +0000 | [diff] [blame] | 3816 | if (NAPI_GRO_CB(skb)->flush || napi->gro_count >= MAX_GRO_SKBS) |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3817 | goto normal; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3818 | |
Herbert Xu | 4ae5544 | 2009-02-08 18:00:36 +0000 | [diff] [blame] | 3819 | napi->gro_count++; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3820 | NAPI_GRO_CB(skb)->count = 1; |
Eric Dumazet | 2e71a6f | 2012-10-06 08:08:49 +0000 | [diff] [blame] | 3821 | NAPI_GRO_CB(skb)->age = jiffies; |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3822 | skb_shinfo(skb)->gso_size = skb_gro_len(skb); |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3823 | skb->next = napi->gro_list; |
| 3824 | napi->gro_list = skb; |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3825 | ret = GRO_HELD; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3826 | |
Herbert Xu | ad0f990 | 2009-02-01 01:24:55 -0800 | [diff] [blame] | 3827 | pull: |
Herbert Xu | cb18978 | 2009-05-26 18:50:31 +0000 | [diff] [blame] | 3828 | if (skb_headlen(skb) < skb_gro_offset(skb)) { |
| 3829 | int grow = skb_gro_offset(skb) - skb_headlen(skb); |
| 3830 | |
| 3831 | BUG_ON(skb->end - skb->tail < grow); |
| 3832 | |
| 3833 | memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow); |
| 3834 | |
| 3835 | skb->tail += grow; |
| 3836 | skb->data_len -= grow; |
| 3837 | |
| 3838 | skb_shinfo(skb)->frags[0].page_offset += grow; |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3839 | skb_frag_size_sub(&skb_shinfo(skb)->frags[0], grow); |
Herbert Xu | cb18978 | 2009-05-26 18:50:31 +0000 | [diff] [blame] | 3840 | |
Eric Dumazet | 9e903e0 | 2011-10-18 21:00:24 +0000 | [diff] [blame] | 3841 | if (unlikely(!skb_frag_size(&skb_shinfo(skb)->frags[0]))) { |
Ian Campbell | ea2ab69 | 2011-08-22 23:44:58 +0000 | [diff] [blame] | 3842 | skb_frag_unref(skb, 0); |
Herbert Xu | cb18978 | 2009-05-26 18:50:31 +0000 | [diff] [blame] | 3843 | memmove(skb_shinfo(skb)->frags, |
| 3844 | skb_shinfo(skb)->frags + 1, |
Jarek Poplawski | e5093ae | 2010-08-11 02:02:10 +0000 | [diff] [blame] | 3845 | --skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t)); |
Herbert Xu | cb18978 | 2009-05-26 18:50:31 +0000 | [diff] [blame] | 3846 | } |
Herbert Xu | ad0f990 | 2009-02-01 01:24:55 -0800 | [diff] [blame] | 3847 | } |
| 3848 | |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3849 | ok: |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3850 | return ret; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3851 | |
| 3852 | normal: |
Herbert Xu | ad0f990 | 2009-02-01 01:24:55 -0800 | [diff] [blame] | 3853 | ret = GRO_NORMAL; |
| 3854 | goto pull; |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3855 | } |
Herbert Xu | 96e93ea | 2009-01-06 10:49:34 -0800 | [diff] [blame] | 3856 | |
Herbert Xu | 96e93ea | 2009-01-06 10:49:34 -0800 | [diff] [blame] | 3857 | |
Rami Rosen | bb72882 | 2012-11-28 21:55:25 +0000 | [diff] [blame] | 3858 | static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb) |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3859 | { |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3860 | switch (ret) { |
| 3861 | case GRO_NORMAL: |
Ben Hutchings | c7c4b3b | 2009-10-29 21:36:53 -0700 | [diff] [blame] | 3862 | if (netif_receive_skb(skb)) |
| 3863 | ret = GRO_DROP; |
| 3864 | break; |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3865 | |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3866 | case GRO_DROP: |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3867 | kfree_skb(skb); |
| 3868 | break; |
Ben Hutchings | 5b252f0 | 2009-10-29 07:17:09 +0000 | [diff] [blame] | 3869 | |
Eric Dumazet | daa8654 | 2012-04-19 07:07:40 +0000 | [diff] [blame] | 3870 | case GRO_MERGED_FREE: |
Eric Dumazet | d7e8883 | 2012-04-30 08:10:34 +0000 | [diff] [blame] | 3871 | if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) |
| 3872 | kmem_cache_free(skbuff_head_cache, skb); |
| 3873 | else |
| 3874 | __kfree_skb(skb); |
Eric Dumazet | daa8654 | 2012-04-19 07:07:40 +0000 | [diff] [blame] | 3875 | break; |
| 3876 | |
Ben Hutchings | 5b252f0 | 2009-10-29 07:17:09 +0000 | [diff] [blame] | 3877 | case GRO_HELD: |
| 3878 | case GRO_MERGED: |
| 3879 | break; |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3880 | } |
| 3881 | |
Ben Hutchings | c7c4b3b | 2009-10-29 21:36:53 -0700 | [diff] [blame] | 3882 | return ret; |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3883 | } |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3884 | |
Eric Dumazet | ca07e43 | 2012-10-06 22:28:06 +0000 | [diff] [blame] | 3885 | static void skb_gro_reset_offset(struct sk_buff *skb) |
Herbert Xu | 78a478d | 2009-05-26 18:50:21 +0000 | [diff] [blame] | 3886 | { |
Eric Dumazet | ca07e43 | 2012-10-06 22:28:06 +0000 | [diff] [blame] | 3887 | const struct skb_shared_info *pinfo = skb_shinfo(skb); |
| 3888 | const skb_frag_t *frag0 = &pinfo->frags[0]; |
| 3889 | |
Herbert Xu | 78a478d | 2009-05-26 18:50:21 +0000 | [diff] [blame] | 3890 | NAPI_GRO_CB(skb)->data_offset = 0; |
| 3891 | NAPI_GRO_CB(skb)->frag0 = NULL; |
Herbert Xu | 7489594 | 2009-05-26 18:50:27 +0000 | [diff] [blame] | 3892 | NAPI_GRO_CB(skb)->frag0_len = 0; |
Herbert Xu | 78a478d | 2009-05-26 18:50:21 +0000 | [diff] [blame] | 3893 | |
Simon Horman | ced14f6 | 2013-05-28 20:34:25 +0000 | [diff] [blame] | 3894 | if (skb_mac_header(skb) == skb_tail_pointer(skb) && |
Eric Dumazet | ca07e43 | 2012-10-06 22:28:06 +0000 | [diff] [blame] | 3895 | pinfo->nr_frags && |
| 3896 | !PageHighMem(skb_frag_page(frag0))) { |
| 3897 | NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0); |
| 3898 | NAPI_GRO_CB(skb)->frag0_len = skb_frag_size(frag0); |
Herbert Xu | 7489594 | 2009-05-26 18:50:27 +0000 | [diff] [blame] | 3899 | } |
Herbert Xu | 78a478d | 2009-05-26 18:50:21 +0000 | [diff] [blame] | 3900 | } |
Herbert Xu | 78a478d | 2009-05-26 18:50:21 +0000 | [diff] [blame] | 3901 | |
Ben Hutchings | c7c4b3b | 2009-10-29 21:36:53 -0700 | [diff] [blame] | 3902 | gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb) |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3903 | { |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3904 | skb_gro_reset_offset(skb); |
| 3905 | |
Eric Dumazet | 89c5fa3 | 2012-12-10 13:28:16 +0000 | [diff] [blame] | 3906 | return napi_skb_finish(dev_gro_receive(napi, skb), skb); |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 3907 | } |
| 3908 | EXPORT_SYMBOL(napi_gro_receive); |
| 3909 | |
stephen hemminger | d0c2b0d | 2010-10-19 07:12:10 +0000 | [diff] [blame] | 3910 | static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb) |
Herbert Xu | 96e93ea | 2009-01-06 10:49:34 -0800 | [diff] [blame] | 3911 | { |
Herbert Xu | 96e93ea | 2009-01-06 10:49:34 -0800 | [diff] [blame] | 3912 | __skb_pull(skb, skb_headlen(skb)); |
Eric Dumazet | 2a2a459 | 2012-03-21 06:58:03 +0000 | [diff] [blame] | 3913 | /* restore the reserve we had after netdev_alloc_skb_ip_align() */ |
| 3914 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb)); |
Jesse Gross | 3701e51 | 2010-10-20 13:56:06 +0000 | [diff] [blame] | 3915 | skb->vlan_tci = 0; |
Herbert Xu | 66c46d7 | 2011-01-29 20:44:54 -0800 | [diff] [blame] | 3916 | skb->dev = napi->dev; |
Andy Gospodarek | 6d152e2 | 2011-02-02 14:53:25 -0800 | [diff] [blame] | 3917 | skb->skb_iif = 0; |
Herbert Xu | 96e93ea | 2009-01-06 10:49:34 -0800 | [diff] [blame] | 3918 | |
| 3919 | napi->skb = skb; |
| 3920 | } |
Herbert Xu | 96e93ea | 2009-01-06 10:49:34 -0800 | [diff] [blame] | 3921 | |
Herbert Xu | 76620aa | 2009-04-16 02:02:07 -0700 | [diff] [blame] | 3922 | struct sk_buff *napi_get_frags(struct napi_struct *napi) |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3923 | { |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3924 | struct sk_buff *skb = napi->skb; |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3925 | |
| 3926 | if (!skb) { |
Eric Dumazet | 89d71a6 | 2009-10-13 05:34:20 +0000 | [diff] [blame] | 3927 | skb = netdev_alloc_skb_ip_align(napi->dev, GRO_MAX_HEAD); |
| 3928 | if (skb) |
| 3929 | napi->skb = skb; |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 3930 | } |
Herbert Xu | 96e93ea | 2009-01-06 10:49:34 -0800 | [diff] [blame] | 3931 | return skb; |
| 3932 | } |
Herbert Xu | 76620aa | 2009-04-16 02:02:07 -0700 | [diff] [blame] | 3933 | EXPORT_SYMBOL(napi_get_frags); |
Herbert Xu | 96e93ea | 2009-01-06 10:49:34 -0800 | [diff] [blame] | 3934 | |
Rami Rosen | bb72882 | 2012-11-28 21:55:25 +0000 | [diff] [blame] | 3935 | static gro_result_t napi_frags_finish(struct napi_struct *napi, struct sk_buff *skb, |
Ben Hutchings | c7c4b3b | 2009-10-29 21:36:53 -0700 | [diff] [blame] | 3936 | gro_result_t ret) |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3937 | { |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3938 | switch (ret) { |
| 3939 | case GRO_NORMAL: |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3940 | case GRO_HELD: |
Ajit Khaparde | e76b69c | 2010-02-16 20:25:43 +0000 | [diff] [blame] | 3941 | skb->protocol = eth_type_trans(skb, skb->dev); |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3942 | |
Ben Hutchings | c7c4b3b | 2009-10-29 21:36:53 -0700 | [diff] [blame] | 3943 | if (ret == GRO_HELD) |
| 3944 | skb_gro_pull(skb, -ETH_HLEN); |
| 3945 | else if (netif_receive_skb(skb)) |
| 3946 | ret = GRO_DROP; |
Herbert Xu | 8691173 | 2009-01-29 14:19:50 +0000 | [diff] [blame] | 3947 | break; |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3948 | |
| 3949 | case GRO_DROP: |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3950 | case GRO_MERGED_FREE: |
| 3951 | napi_reuse_skb(napi, skb); |
| 3952 | break; |
Ben Hutchings | 5b252f0 | 2009-10-29 07:17:09 +0000 | [diff] [blame] | 3953 | |
| 3954 | case GRO_MERGED: |
| 3955 | break; |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3956 | } |
| 3957 | |
Ben Hutchings | c7c4b3b | 2009-10-29 21:36:53 -0700 | [diff] [blame] | 3958 | return ret; |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3959 | } |
Herbert Xu | 5d0d9be | 2009-01-29 14:19:48 +0000 | [diff] [blame] | 3960 | |
Eric Dumazet | 4adb9c4 | 2012-05-18 20:49:06 +0000 | [diff] [blame] | 3961 | static struct sk_buff *napi_frags_skb(struct napi_struct *napi) |
Herbert Xu | 96e93ea | 2009-01-06 10:49:34 -0800 | [diff] [blame] | 3962 | { |
Herbert Xu | 76620aa | 2009-04-16 02:02:07 -0700 | [diff] [blame] | 3963 | struct sk_buff *skb = napi->skb; |
| 3964 | struct ethhdr *eth; |
Herbert Xu | a5b1cf2 | 2009-05-26 18:50:28 +0000 | [diff] [blame] | 3965 | unsigned int hlen; |
| 3966 | unsigned int off; |
Herbert Xu | 76620aa | 2009-04-16 02:02:07 -0700 | [diff] [blame] | 3967 | |
| 3968 | napi->skb = NULL; |
| 3969 | |
| 3970 | skb_reset_mac_header(skb); |
| 3971 | skb_gro_reset_offset(skb); |
| 3972 | |
Herbert Xu | a5b1cf2 | 2009-05-26 18:50:28 +0000 | [diff] [blame] | 3973 | off = skb_gro_offset(skb); |
| 3974 | hlen = off + sizeof(*eth); |
| 3975 | eth = skb_gro_header_fast(skb, off); |
| 3976 | if (skb_gro_header_hard(skb, hlen)) { |
| 3977 | eth = skb_gro_header_slow(skb, hlen, off); |
| 3978 | if (unlikely(!eth)) { |
| 3979 | napi_reuse_skb(napi, skb); |
| 3980 | skb = NULL; |
| 3981 | goto out; |
| 3982 | } |
Herbert Xu | 76620aa | 2009-04-16 02:02:07 -0700 | [diff] [blame] | 3983 | } |
| 3984 | |
| 3985 | skb_gro_pull(skb, sizeof(*eth)); |
| 3986 | |
| 3987 | /* |
| 3988 | * This works because the only protocols we care about don't require |
| 3989 | * special handling. We'll fix it up properly at the end. |
| 3990 | */ |
| 3991 | skb->protocol = eth->h_proto; |
| 3992 | |
| 3993 | out: |
| 3994 | return skb; |
| 3995 | } |
Herbert Xu | 76620aa | 2009-04-16 02:02:07 -0700 | [diff] [blame] | 3996 | |
Ben Hutchings | c7c4b3b | 2009-10-29 21:36:53 -0700 | [diff] [blame] | 3997 | gro_result_t napi_gro_frags(struct napi_struct *napi) |
Herbert Xu | 76620aa | 2009-04-16 02:02:07 -0700 | [diff] [blame] | 3998 | { |
| 3999 | struct sk_buff *skb = napi_frags_skb(napi); |
Herbert Xu | 96e93ea | 2009-01-06 10:49:34 -0800 | [diff] [blame] | 4000 | |
| 4001 | if (!skb) |
Ben Hutchings | c7c4b3b | 2009-10-29 21:36:53 -0700 | [diff] [blame] | 4002 | return GRO_DROP; |
Herbert Xu | 96e93ea | 2009-01-06 10:49:34 -0800 | [diff] [blame] | 4003 | |
Eric Dumazet | 89c5fa3 | 2012-12-10 13:28:16 +0000 | [diff] [blame] | 4004 | return napi_frags_finish(napi, skb, dev_gro_receive(napi, skb)); |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 4005 | } |
| 4006 | EXPORT_SYMBOL(napi_gro_frags); |
| 4007 | |
Eric Dumazet | e326bed | 2010-04-22 00:22:45 -0700 | [diff] [blame] | 4008 | /* |
| 4009 | * net_rps_action sends any pending IPI's for rps. |
| 4010 | * Note: called with local irq disabled, but exits with local irq enabled. |
| 4011 | */ |
| 4012 | static void net_rps_action_and_irq_enable(struct softnet_data *sd) |
| 4013 | { |
| 4014 | #ifdef CONFIG_RPS |
| 4015 | struct softnet_data *remsd = sd->rps_ipi_list; |
| 4016 | |
| 4017 | if (remsd) { |
| 4018 | sd->rps_ipi_list = NULL; |
| 4019 | |
| 4020 | local_irq_enable(); |
| 4021 | |
| 4022 | /* Send pending IPI's to kick RPS processing on remote cpus. */ |
| 4023 | while (remsd) { |
| 4024 | struct softnet_data *next = remsd->rps_ipi_next; |
| 4025 | |
| 4026 | if (cpu_online(remsd->cpu)) |
| 4027 | __smp_call_function_single(remsd->cpu, |
| 4028 | &remsd->csd, 0); |
| 4029 | remsd = next; |
| 4030 | } |
| 4031 | } else |
| 4032 | #endif |
| 4033 | local_irq_enable(); |
| 4034 | } |
| 4035 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4036 | static int process_backlog(struct napi_struct *napi, int quota) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4037 | { |
| 4038 | int work = 0; |
Eric Dumazet | eecfd7c | 2010-05-06 22:07:48 -0700 | [diff] [blame] | 4039 | struct softnet_data *sd = container_of(napi, struct softnet_data, backlog); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4040 | |
Eric Dumazet | e326bed | 2010-04-22 00:22:45 -0700 | [diff] [blame] | 4041 | #ifdef CONFIG_RPS |
| 4042 | /* Check if we have pending ipi, its better to send them now, |
| 4043 | * not waiting net_rx_action() end. |
| 4044 | */ |
| 4045 | if (sd->rps_ipi_list) { |
| 4046 | local_irq_disable(); |
| 4047 | net_rps_action_and_irq_enable(sd); |
| 4048 | } |
| 4049 | #endif |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4050 | napi->weight = weight_p; |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 4051 | local_irq_disable(); |
| 4052 | while (work < quota) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4053 | struct sk_buff *skb; |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 4054 | unsigned int qlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4055 | |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 4056 | while ((skb = __skb_dequeue(&sd->process_queue))) { |
Eric Dumazet | e400827 | 2010-04-05 15:42:39 -0700 | [diff] [blame] | 4057 | local_irq_enable(); |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 4058 | __netif_receive_skb(skb); |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 4059 | local_irq_disable(); |
Tom Herbert | 76cc8b1 | 2010-05-20 18:37:59 +0000 | [diff] [blame] | 4060 | input_queue_head_incr(sd); |
| 4061 | if (++work >= quota) { |
| 4062 | local_irq_enable(); |
| 4063 | return work; |
| 4064 | } |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4065 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4066 | |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 4067 | rps_lock(sd); |
| 4068 | qlen = skb_queue_len(&sd->input_pkt_queue); |
Tom Herbert | 76cc8b1 | 2010-05-20 18:37:59 +0000 | [diff] [blame] | 4069 | if (qlen) |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 4070 | skb_queue_splice_tail_init(&sd->input_pkt_queue, |
| 4071 | &sd->process_queue); |
Tom Herbert | 76cc8b1 | 2010-05-20 18:37:59 +0000 | [diff] [blame] | 4072 | |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 4073 | if (qlen < quota - work) { |
Eric Dumazet | eecfd7c | 2010-05-06 22:07:48 -0700 | [diff] [blame] | 4074 | /* |
| 4075 | * Inline a custom version of __napi_complete(). |
| 4076 | * only current cpu owns and manipulates this napi, |
| 4077 | * and NAPI_STATE_SCHED is the only possible flag set on backlog. |
| 4078 | * we can use a plain write instead of clear_bit(), |
| 4079 | * and we dont need an smp_mb() memory barrier. |
| 4080 | */ |
| 4081 | list_del(&napi->poll_list); |
| 4082 | napi->state = 0; |
| 4083 | |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 4084 | quota = work + qlen; |
| 4085 | } |
| 4086 | rps_unlock(sd); |
| 4087 | } |
| 4088 | local_irq_enable(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4089 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4090 | return work; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4091 | } |
| 4092 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4093 | /** |
| 4094 | * __napi_schedule - schedule for receive |
Randy Dunlap | c4ea43c | 2007-10-12 21:17:49 -0700 | [diff] [blame] | 4095 | * @n: entry to schedule |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4096 | * |
| 4097 | * The entry's receive function will be scheduled to run |
| 4098 | */ |
Harvey Harrison | b5606c2 | 2008-02-13 15:03:16 -0800 | [diff] [blame] | 4099 | void __napi_schedule(struct napi_struct *n) |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4100 | { |
| 4101 | unsigned long flags; |
| 4102 | |
| 4103 | local_irq_save(flags); |
Eric Dumazet | eecfd7c | 2010-05-06 22:07:48 -0700 | [diff] [blame] | 4104 | ____napi_schedule(&__get_cpu_var(softnet_data), n); |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4105 | local_irq_restore(flags); |
| 4106 | } |
| 4107 | EXPORT_SYMBOL(__napi_schedule); |
| 4108 | |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 4109 | void __napi_complete(struct napi_struct *n) |
| 4110 | { |
| 4111 | BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state)); |
| 4112 | BUG_ON(n->gro_list); |
| 4113 | |
| 4114 | list_del(&n->poll_list); |
| 4115 | smp_mb__before_clear_bit(); |
| 4116 | clear_bit(NAPI_STATE_SCHED, &n->state); |
| 4117 | } |
| 4118 | EXPORT_SYMBOL(__napi_complete); |
| 4119 | |
| 4120 | void napi_complete(struct napi_struct *n) |
| 4121 | { |
| 4122 | unsigned long flags; |
| 4123 | |
| 4124 | /* |
| 4125 | * don't let napi dequeue from the cpu poll list |
| 4126 | * just in case its running on a different cpu |
| 4127 | */ |
| 4128 | if (unlikely(test_bit(NAPI_STATE_NPSVC, &n->state))) |
| 4129 | return; |
| 4130 | |
Eric Dumazet | 2e71a6f | 2012-10-06 08:08:49 +0000 | [diff] [blame] | 4131 | napi_gro_flush(n, false); |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 4132 | local_irq_save(flags); |
| 4133 | __napi_complete(n); |
| 4134 | local_irq_restore(flags); |
| 4135 | } |
| 4136 | EXPORT_SYMBOL(napi_complete); |
| 4137 | |
Eliezer Tamir | af12fa6 | 2013-06-10 11:39:41 +0300 | [diff] [blame] | 4138 | /* must be called under rcu_read_lock(), as we dont take a reference */ |
| 4139 | struct napi_struct *napi_by_id(unsigned int napi_id) |
| 4140 | { |
| 4141 | unsigned int hash = napi_id % HASH_SIZE(napi_hash); |
| 4142 | struct napi_struct *napi; |
| 4143 | |
| 4144 | hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node) |
| 4145 | if (napi->napi_id == napi_id) |
| 4146 | return napi; |
| 4147 | |
| 4148 | return NULL; |
| 4149 | } |
| 4150 | EXPORT_SYMBOL_GPL(napi_by_id); |
| 4151 | |
| 4152 | void napi_hash_add(struct napi_struct *napi) |
| 4153 | { |
| 4154 | if (!test_and_set_bit(NAPI_STATE_HASHED, &napi->state)) { |
| 4155 | |
| 4156 | spin_lock(&napi_hash_lock); |
| 4157 | |
| 4158 | /* 0 is not a valid id, we also skip an id that is taken |
| 4159 | * we expect both events to be extremely rare |
| 4160 | */ |
| 4161 | napi->napi_id = 0; |
| 4162 | while (!napi->napi_id) { |
| 4163 | napi->napi_id = ++napi_gen_id; |
| 4164 | if (napi_by_id(napi->napi_id)) |
| 4165 | napi->napi_id = 0; |
| 4166 | } |
| 4167 | |
| 4168 | hlist_add_head_rcu(&napi->napi_hash_node, |
| 4169 | &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]); |
| 4170 | |
| 4171 | spin_unlock(&napi_hash_lock); |
| 4172 | } |
| 4173 | } |
| 4174 | EXPORT_SYMBOL_GPL(napi_hash_add); |
| 4175 | |
| 4176 | /* Warning : caller is responsible to make sure rcu grace period |
| 4177 | * is respected before freeing memory containing @napi |
| 4178 | */ |
| 4179 | void napi_hash_del(struct napi_struct *napi) |
| 4180 | { |
| 4181 | spin_lock(&napi_hash_lock); |
| 4182 | |
| 4183 | if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) |
| 4184 | hlist_del_rcu(&napi->napi_hash_node); |
| 4185 | |
| 4186 | spin_unlock(&napi_hash_lock); |
| 4187 | } |
| 4188 | EXPORT_SYMBOL_GPL(napi_hash_del); |
| 4189 | |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 4190 | void netif_napi_add(struct net_device *dev, struct napi_struct *napi, |
| 4191 | int (*poll)(struct napi_struct *, int), int weight) |
| 4192 | { |
| 4193 | INIT_LIST_HEAD(&napi->poll_list); |
Herbert Xu | 4ae5544 | 2009-02-08 18:00:36 +0000 | [diff] [blame] | 4194 | napi->gro_count = 0; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 4195 | napi->gro_list = NULL; |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 4196 | napi->skb = NULL; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 4197 | napi->poll = poll; |
Eric Dumazet | 82dc3c6 | 2013-03-05 15:57:22 +0000 | [diff] [blame] | 4198 | if (weight > NAPI_POLL_WEIGHT) |
| 4199 | pr_err_once("netif_napi_add() called with weight %d on device %s\n", |
| 4200 | weight, dev->name); |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 4201 | napi->weight = weight; |
| 4202 | list_add(&napi->dev_list, &dev->napi_list); |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 4203 | napi->dev = dev; |
Herbert Xu | 5d38a07 | 2009-01-04 16:13:40 -0800 | [diff] [blame] | 4204 | #ifdef CONFIG_NETPOLL |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 4205 | spin_lock_init(&napi->poll_lock); |
| 4206 | napi->poll_owner = -1; |
| 4207 | #endif |
| 4208 | set_bit(NAPI_STATE_SCHED, &napi->state); |
| 4209 | } |
| 4210 | EXPORT_SYMBOL(netif_napi_add); |
| 4211 | |
| 4212 | void netif_napi_del(struct napi_struct *napi) |
| 4213 | { |
| 4214 | struct sk_buff *skb, *next; |
| 4215 | |
Peter P Waskiewicz Jr | d7b0663 | 2008-12-26 01:35:35 -0800 | [diff] [blame] | 4216 | list_del_init(&napi->dev_list); |
Herbert Xu | 76620aa | 2009-04-16 02:02:07 -0700 | [diff] [blame] | 4217 | napi_free_frags(napi); |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 4218 | |
| 4219 | for (skb = napi->gro_list; skb; skb = next) { |
| 4220 | next = skb->next; |
| 4221 | skb->next = NULL; |
| 4222 | kfree_skb(skb); |
| 4223 | } |
| 4224 | |
| 4225 | napi->gro_list = NULL; |
Herbert Xu | 4ae5544 | 2009-02-08 18:00:36 +0000 | [diff] [blame] | 4226 | napi->gro_count = 0; |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 4227 | } |
| 4228 | EXPORT_SYMBOL(netif_napi_del); |
| 4229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | static void net_rx_action(struct softirq_action *h) |
| 4231 | { |
Eric Dumazet | e326bed | 2010-04-22 00:22:45 -0700 | [diff] [blame] | 4232 | struct softnet_data *sd = &__get_cpu_var(softnet_data); |
Stephen Hemminger | 24f8b23 | 2008-11-03 17:14:38 -0800 | [diff] [blame] | 4233 | unsigned long time_limit = jiffies + 2; |
Stephen Hemminger | 51b0bde | 2005-06-23 20:14:40 -0700 | [diff] [blame] | 4234 | int budget = netdev_budget; |
Matt Mackall | 53fb95d | 2005-08-11 19:27:43 -0700 | [diff] [blame] | 4235 | void *have; |
| 4236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | local_irq_disable(); |
| 4238 | |
Eric Dumazet | e326bed | 2010-04-22 00:22:45 -0700 | [diff] [blame] | 4239 | while (!list_empty(&sd->poll_list)) { |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4240 | struct napi_struct *n; |
| 4241 | int work, weight; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4243 | /* If softirq window is exhuasted then punt. |
Stephen Hemminger | 24f8b23 | 2008-11-03 17:14:38 -0800 | [diff] [blame] | 4244 | * Allow this to run for 2 jiffies since which will allow |
| 4245 | * an average latency of 1.5/HZ. |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4246 | */ |
Eric Dumazet | d1f41b6 | 2013-03-05 07:15:13 +0000 | [diff] [blame] | 4247 | if (unlikely(budget <= 0 || time_after_eq(jiffies, time_limit))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | goto softnet_break; |
| 4249 | |
| 4250 | local_irq_enable(); |
| 4251 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4252 | /* Even though interrupts have been re-enabled, this |
| 4253 | * access is safe because interrupts can only add new |
| 4254 | * entries to the tail of this list, and only ->poll() |
| 4255 | * calls can remove this head entry from the list. |
| 4256 | */ |
Eric Dumazet | e326bed | 2010-04-22 00:22:45 -0700 | [diff] [blame] | 4257 | n = list_first_entry(&sd->poll_list, struct napi_struct, poll_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4258 | |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4259 | have = netpoll_poll_lock(n); |
| 4260 | |
| 4261 | weight = n->weight; |
| 4262 | |
David S. Miller | 0a7606c | 2007-10-29 21:28:47 -0700 | [diff] [blame] | 4263 | /* This NAPI_STATE_SCHED test is for avoiding a race |
| 4264 | * with netpoll's poll_napi(). Only the entity which |
| 4265 | * obtains the lock and sees NAPI_STATE_SCHED set will |
| 4266 | * actually make the ->poll() call. Therefore we avoid |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 4267 | * accidentally calling ->poll() when NAPI is not scheduled. |
David S. Miller | 0a7606c | 2007-10-29 21:28:47 -0700 | [diff] [blame] | 4268 | */ |
| 4269 | work = 0; |
Neil Horman | 4ea7e38 | 2009-05-21 07:36:08 +0000 | [diff] [blame] | 4270 | if (test_bit(NAPI_STATE_SCHED, &n->state)) { |
David S. Miller | 0a7606c | 2007-10-29 21:28:47 -0700 | [diff] [blame] | 4271 | work = n->poll(n, weight); |
Neil Horman | 4ea7e38 | 2009-05-21 07:36:08 +0000 | [diff] [blame] | 4272 | trace_napi_poll(n); |
| 4273 | } |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4274 | |
| 4275 | WARN_ON_ONCE(work > weight); |
| 4276 | |
| 4277 | budget -= work; |
| 4278 | |
| 4279 | local_irq_disable(); |
| 4280 | |
| 4281 | /* Drivers must not modify the NAPI state if they |
| 4282 | * consume the entire weight. In such cases this code |
| 4283 | * still "owns" the NAPI instance and therefore can |
| 4284 | * move the instance around on the list at-will. |
| 4285 | */ |
David S. Miller | fed17f3 | 2008-01-07 21:00:40 -0800 | [diff] [blame] | 4286 | if (unlikely(work == weight)) { |
Herbert Xu | ff780cd | 2009-06-26 19:27:04 -0700 | [diff] [blame] | 4287 | if (unlikely(napi_disable_pending(n))) { |
| 4288 | local_irq_enable(); |
| 4289 | napi_complete(n); |
| 4290 | local_irq_disable(); |
Eric Dumazet | 2e71a6f | 2012-10-06 08:08:49 +0000 | [diff] [blame] | 4291 | } else { |
| 4292 | if (n->gro_list) { |
| 4293 | /* flush too old packets |
| 4294 | * If HZ < 1000, flush all packets. |
| 4295 | */ |
| 4296 | local_irq_enable(); |
| 4297 | napi_gro_flush(n, HZ >= 1000); |
| 4298 | local_irq_disable(); |
| 4299 | } |
Eric Dumazet | e326bed | 2010-04-22 00:22:45 -0700 | [diff] [blame] | 4300 | list_move_tail(&n->poll_list, &sd->poll_list); |
Eric Dumazet | 2e71a6f | 2012-10-06 08:08:49 +0000 | [diff] [blame] | 4301 | } |
David S. Miller | fed17f3 | 2008-01-07 21:00:40 -0800 | [diff] [blame] | 4302 | } |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4303 | |
| 4304 | netpoll_poll_unlock(have); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | } |
| 4306 | out: |
Eric Dumazet | e326bed | 2010-04-22 00:22:45 -0700 | [diff] [blame] | 4307 | net_rps_action_and_irq_enable(sd); |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 4308 | |
Chris Leech | db21733 | 2006-06-17 21:24:58 -0700 | [diff] [blame] | 4309 | #ifdef CONFIG_NET_DMA |
| 4310 | /* |
| 4311 | * There may not be any more sk_buffs coming right now, so push |
| 4312 | * any pending DMA copies to hardware |
| 4313 | */ |
Dan Williams | 2ba0562 | 2009-01-06 11:38:14 -0700 | [diff] [blame] | 4314 | dma_issue_pending_all(); |
Chris Leech | db21733 | 2006-06-17 21:24:58 -0700 | [diff] [blame] | 4315 | #endif |
Stephen Hemminger | bea3348 | 2007-10-03 16:41:36 -0700 | [diff] [blame] | 4316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4317 | return; |
| 4318 | |
| 4319 | softnet_break: |
Changli Gao | dee4287 | 2010-05-02 05:42:16 +0000 | [diff] [blame] | 4320 | sd->time_squeeze++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); |
| 4322 | goto out; |
| 4323 | } |
| 4324 | |
Jiri Pirko | 9ff162a | 2013-01-03 22:48:49 +0000 | [diff] [blame] | 4325 | struct netdev_upper { |
| 4326 | struct net_device *dev; |
| 4327 | bool master; |
| 4328 | struct list_head list; |
| 4329 | struct rcu_head rcu; |
| 4330 | struct list_head search_list; |
| 4331 | }; |
| 4332 | |
| 4333 | static void __append_search_uppers(struct list_head *search_list, |
| 4334 | struct net_device *dev) |
| 4335 | { |
| 4336 | struct netdev_upper *upper; |
| 4337 | |
| 4338 | list_for_each_entry(upper, &dev->upper_dev_list, list) { |
| 4339 | /* check if this upper is not already in search list */ |
| 4340 | if (list_empty(&upper->search_list)) |
| 4341 | list_add_tail(&upper->search_list, search_list); |
| 4342 | } |
| 4343 | } |
| 4344 | |
| 4345 | static bool __netdev_search_upper_dev(struct net_device *dev, |
| 4346 | struct net_device *upper_dev) |
| 4347 | { |
| 4348 | LIST_HEAD(search_list); |
| 4349 | struct netdev_upper *upper; |
| 4350 | struct netdev_upper *tmp; |
| 4351 | bool ret = false; |
| 4352 | |
| 4353 | __append_search_uppers(&search_list, dev); |
| 4354 | list_for_each_entry(upper, &search_list, search_list) { |
| 4355 | if (upper->dev == upper_dev) { |
| 4356 | ret = true; |
| 4357 | break; |
| 4358 | } |
| 4359 | __append_search_uppers(&search_list, upper->dev); |
| 4360 | } |
| 4361 | list_for_each_entry_safe(upper, tmp, &search_list, search_list) |
| 4362 | INIT_LIST_HEAD(&upper->search_list); |
| 4363 | return ret; |
| 4364 | } |
| 4365 | |
| 4366 | static struct netdev_upper *__netdev_find_upper(struct net_device *dev, |
| 4367 | struct net_device *upper_dev) |
| 4368 | { |
| 4369 | struct netdev_upper *upper; |
| 4370 | |
| 4371 | list_for_each_entry(upper, &dev->upper_dev_list, list) { |
| 4372 | if (upper->dev == upper_dev) |
| 4373 | return upper; |
| 4374 | } |
| 4375 | return NULL; |
| 4376 | } |
| 4377 | |
| 4378 | /** |
| 4379 | * netdev_has_upper_dev - Check if device is linked to an upper device |
| 4380 | * @dev: device |
| 4381 | * @upper_dev: upper device to check |
| 4382 | * |
| 4383 | * Find out if a device is linked to specified upper device and return true |
| 4384 | * in case it is. Note that this checks only immediate upper device, |
| 4385 | * not through a complete stack of devices. The caller must hold the RTNL lock. |
| 4386 | */ |
| 4387 | bool netdev_has_upper_dev(struct net_device *dev, |
| 4388 | struct net_device *upper_dev) |
| 4389 | { |
| 4390 | ASSERT_RTNL(); |
| 4391 | |
| 4392 | return __netdev_find_upper(dev, upper_dev); |
| 4393 | } |
| 4394 | EXPORT_SYMBOL(netdev_has_upper_dev); |
| 4395 | |
| 4396 | /** |
| 4397 | * netdev_has_any_upper_dev - Check if device is linked to some device |
| 4398 | * @dev: device |
| 4399 | * |
| 4400 | * Find out if a device is linked to an upper device and return true in case |
| 4401 | * it is. The caller must hold the RTNL lock. |
| 4402 | */ |
| 4403 | bool netdev_has_any_upper_dev(struct net_device *dev) |
| 4404 | { |
| 4405 | ASSERT_RTNL(); |
| 4406 | |
| 4407 | return !list_empty(&dev->upper_dev_list); |
| 4408 | } |
| 4409 | EXPORT_SYMBOL(netdev_has_any_upper_dev); |
| 4410 | |
| 4411 | /** |
| 4412 | * netdev_master_upper_dev_get - Get master upper device |
| 4413 | * @dev: device |
| 4414 | * |
| 4415 | * Find a master upper device and return pointer to it or NULL in case |
| 4416 | * it's not there. The caller must hold the RTNL lock. |
| 4417 | */ |
| 4418 | struct net_device *netdev_master_upper_dev_get(struct net_device *dev) |
| 4419 | { |
| 4420 | struct netdev_upper *upper; |
| 4421 | |
| 4422 | ASSERT_RTNL(); |
| 4423 | |
| 4424 | if (list_empty(&dev->upper_dev_list)) |
| 4425 | return NULL; |
| 4426 | |
| 4427 | upper = list_first_entry(&dev->upper_dev_list, |
| 4428 | struct netdev_upper, list); |
| 4429 | if (likely(upper->master)) |
| 4430 | return upper->dev; |
| 4431 | return NULL; |
| 4432 | } |
| 4433 | EXPORT_SYMBOL(netdev_master_upper_dev_get); |
| 4434 | |
| 4435 | /** |
| 4436 | * netdev_master_upper_dev_get_rcu - Get master upper device |
| 4437 | * @dev: device |
| 4438 | * |
| 4439 | * Find a master upper device and return pointer to it or NULL in case |
| 4440 | * it's not there. The caller must hold the RCU read lock. |
| 4441 | */ |
| 4442 | struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev) |
| 4443 | { |
| 4444 | struct netdev_upper *upper; |
| 4445 | |
| 4446 | upper = list_first_or_null_rcu(&dev->upper_dev_list, |
| 4447 | struct netdev_upper, list); |
| 4448 | if (upper && likely(upper->master)) |
| 4449 | return upper->dev; |
| 4450 | return NULL; |
| 4451 | } |
| 4452 | EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu); |
| 4453 | |
| 4454 | static int __netdev_upper_dev_link(struct net_device *dev, |
| 4455 | struct net_device *upper_dev, bool master) |
| 4456 | { |
| 4457 | struct netdev_upper *upper; |
| 4458 | |
| 4459 | ASSERT_RTNL(); |
| 4460 | |
| 4461 | if (dev == upper_dev) |
| 4462 | return -EBUSY; |
| 4463 | |
| 4464 | /* To prevent loops, check if dev is not upper device to upper_dev. */ |
| 4465 | if (__netdev_search_upper_dev(upper_dev, dev)) |
| 4466 | return -EBUSY; |
| 4467 | |
| 4468 | if (__netdev_find_upper(dev, upper_dev)) |
| 4469 | return -EEXIST; |
| 4470 | |
| 4471 | if (master && netdev_master_upper_dev_get(dev)) |
| 4472 | return -EBUSY; |
| 4473 | |
| 4474 | upper = kmalloc(sizeof(*upper), GFP_KERNEL); |
| 4475 | if (!upper) |
| 4476 | return -ENOMEM; |
| 4477 | |
| 4478 | upper->dev = upper_dev; |
| 4479 | upper->master = master; |
| 4480 | INIT_LIST_HEAD(&upper->search_list); |
| 4481 | |
| 4482 | /* Ensure that master upper link is always the first item in list. */ |
| 4483 | if (master) |
| 4484 | list_add_rcu(&upper->list, &dev->upper_dev_list); |
| 4485 | else |
| 4486 | list_add_tail_rcu(&upper->list, &dev->upper_dev_list); |
| 4487 | dev_hold(upper_dev); |
Jiri Pirko | 42e52bf | 2013-05-25 04:12:10 +0000 | [diff] [blame] | 4488 | call_netdevice_notifiers(NETDEV_CHANGEUPPER, dev); |
Jiri Pirko | 9ff162a | 2013-01-03 22:48:49 +0000 | [diff] [blame] | 4489 | return 0; |
| 4490 | } |
| 4491 | |
| 4492 | /** |
| 4493 | * netdev_upper_dev_link - Add a link to the upper device |
| 4494 | * @dev: device |
| 4495 | * @upper_dev: new upper device |
| 4496 | * |
| 4497 | * Adds a link to device which is upper to this one. The caller must hold |
| 4498 | * the RTNL lock. On a failure a negative errno code is returned. |
| 4499 | * On success the reference counts are adjusted and the function |
| 4500 | * returns zero. |
| 4501 | */ |
| 4502 | int netdev_upper_dev_link(struct net_device *dev, |
| 4503 | struct net_device *upper_dev) |
| 4504 | { |
| 4505 | return __netdev_upper_dev_link(dev, upper_dev, false); |
| 4506 | } |
| 4507 | EXPORT_SYMBOL(netdev_upper_dev_link); |
| 4508 | |
| 4509 | /** |
| 4510 | * netdev_master_upper_dev_link - Add a master link to the upper device |
| 4511 | * @dev: device |
| 4512 | * @upper_dev: new upper device |
| 4513 | * |
| 4514 | * Adds a link to device which is upper to this one. In this case, only |
| 4515 | * one master upper device can be linked, although other non-master devices |
| 4516 | * might be linked as well. The caller must hold the RTNL lock. |
| 4517 | * On a failure a negative errno code is returned. On success the reference |
| 4518 | * counts are adjusted and the function returns zero. |
| 4519 | */ |
| 4520 | int netdev_master_upper_dev_link(struct net_device *dev, |
| 4521 | struct net_device *upper_dev) |
| 4522 | { |
| 4523 | return __netdev_upper_dev_link(dev, upper_dev, true); |
| 4524 | } |
| 4525 | EXPORT_SYMBOL(netdev_master_upper_dev_link); |
| 4526 | |
| 4527 | /** |
| 4528 | * netdev_upper_dev_unlink - Removes a link to upper device |
| 4529 | * @dev: device |
| 4530 | * @upper_dev: new upper device |
| 4531 | * |
| 4532 | * Removes a link to device which is upper to this one. The caller must hold |
| 4533 | * the RTNL lock. |
| 4534 | */ |
| 4535 | void netdev_upper_dev_unlink(struct net_device *dev, |
| 4536 | struct net_device *upper_dev) |
| 4537 | { |
| 4538 | struct netdev_upper *upper; |
| 4539 | |
| 4540 | ASSERT_RTNL(); |
| 4541 | |
| 4542 | upper = __netdev_find_upper(dev, upper_dev); |
| 4543 | if (!upper) |
| 4544 | return; |
| 4545 | list_del_rcu(&upper->list); |
| 4546 | dev_put(upper_dev); |
| 4547 | kfree_rcu(upper, rcu); |
Jiri Pirko | 42e52bf | 2013-05-25 04:12:10 +0000 | [diff] [blame] | 4548 | call_netdevice_notifiers(NETDEV_CHANGEUPPER, dev); |
Jiri Pirko | 9ff162a | 2013-01-03 22:48:49 +0000 | [diff] [blame] | 4549 | } |
| 4550 | EXPORT_SYMBOL(netdev_upper_dev_unlink); |
| 4551 | |
Patrick McHardy | b6c40d6 | 2008-10-07 15:26:48 -0700 | [diff] [blame] | 4552 | static void dev_change_rx_flags(struct net_device *dev, int flags) |
| 4553 | { |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 4554 | const struct net_device_ops *ops = dev->netdev_ops; |
| 4555 | |
| 4556 | if ((dev->flags & IFF_UP) && ops->ndo_change_rx_flags) |
| 4557 | ops->ndo_change_rx_flags(dev, flags); |
Patrick McHardy | b6c40d6 | 2008-10-07 15:26:48 -0700 | [diff] [blame] | 4558 | } |
| 4559 | |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4560 | static int __dev_set_promiscuity(struct net_device *dev, int inc) |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4561 | { |
Eric Dumazet | b536db9 | 2011-11-30 21:42:26 +0000 | [diff] [blame] | 4562 | unsigned int old_flags = dev->flags; |
Eric W. Biederman | d04a48b | 2012-05-23 17:01:57 -0600 | [diff] [blame] | 4563 | kuid_t uid; |
| 4564 | kgid_t gid; |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4565 | |
Patrick McHardy | 2402345 | 2007-07-14 18:51:31 -0700 | [diff] [blame] | 4566 | ASSERT_RTNL(); |
| 4567 | |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4568 | dev->flags |= IFF_PROMISC; |
| 4569 | dev->promiscuity += inc; |
| 4570 | if (dev->promiscuity == 0) { |
| 4571 | /* |
| 4572 | * Avoid overflow. |
| 4573 | * If inc causes overflow, untouch promisc and return error. |
| 4574 | */ |
| 4575 | if (inc < 0) |
| 4576 | dev->flags &= ~IFF_PROMISC; |
| 4577 | else { |
| 4578 | dev->promiscuity -= inc; |
Joe Perches | 7b6cd1c | 2012-02-01 10:54:43 +0000 | [diff] [blame] | 4579 | pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n", |
| 4580 | dev->name); |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4581 | return -EOVERFLOW; |
| 4582 | } |
| 4583 | } |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4584 | if (dev->flags != old_flags) { |
Joe Perches | 7b6cd1c | 2012-02-01 10:54:43 +0000 | [diff] [blame] | 4585 | pr_info("device %s %s promiscuous mode\n", |
| 4586 | dev->name, |
| 4587 | dev->flags & IFF_PROMISC ? "entered" : "left"); |
David Howells | 8192b0c | 2008-11-14 10:39:10 +1100 | [diff] [blame] | 4588 | if (audit_enabled) { |
| 4589 | current_uid_gid(&uid, &gid); |
Klaus Heinrich Kiwi | 7759db8 | 2008-01-23 22:57:45 -0500 | [diff] [blame] | 4590 | audit_log(current->audit_context, GFP_ATOMIC, |
| 4591 | AUDIT_ANOM_PROMISCUOUS, |
| 4592 | "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u", |
| 4593 | dev->name, (dev->flags & IFF_PROMISC), |
| 4594 | (old_flags & IFF_PROMISC), |
Eric W. Biederman | e1760bd | 2012-09-10 22:39:43 -0700 | [diff] [blame] | 4595 | from_kuid(&init_user_ns, audit_get_loginuid(current)), |
Eric W. Biederman | d04a48b | 2012-05-23 17:01:57 -0600 | [diff] [blame] | 4596 | from_kuid(&init_user_ns, uid), |
| 4597 | from_kgid(&init_user_ns, gid), |
Klaus Heinrich Kiwi | 7759db8 | 2008-01-23 22:57:45 -0500 | [diff] [blame] | 4598 | audit_get_sessionid(current)); |
David Howells | 8192b0c | 2008-11-14 10:39:10 +1100 | [diff] [blame] | 4599 | } |
Patrick McHardy | 2402345 | 2007-07-14 18:51:31 -0700 | [diff] [blame] | 4600 | |
Patrick McHardy | b6c40d6 | 2008-10-07 15:26:48 -0700 | [diff] [blame] | 4601 | dev_change_rx_flags(dev, IFF_PROMISC); |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4602 | } |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4603 | return 0; |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4604 | } |
| 4605 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4606 | /** |
| 4607 | * dev_set_promiscuity - update promiscuity count on a device |
| 4608 | * @dev: device |
| 4609 | * @inc: modifier |
| 4610 | * |
Stephen Hemminger | 3041a06 | 2006-05-26 13:25:24 -0700 | [diff] [blame] | 4611 | * Add or remove promiscuity from a device. While the count in the device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | * remains above zero the interface remains promiscuous. Once it hits zero |
| 4613 | * the device reverts back to normal filtering operation. A negative inc |
| 4614 | * value is used to drop promiscuity on the device. |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4615 | * Return 0 if successful or a negative errno code on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4616 | */ |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4617 | int dev_set_promiscuity(struct net_device *dev, int inc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | { |
Eric Dumazet | b536db9 | 2011-11-30 21:42:26 +0000 | [diff] [blame] | 4619 | unsigned int old_flags = dev->flags; |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4620 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4621 | |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4622 | err = __dev_set_promiscuity(dev, inc); |
Patrick McHardy | 4b5a698 | 2008-07-06 15:49:08 -0700 | [diff] [blame] | 4623 | if (err < 0) |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4624 | return err; |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4625 | if (dev->flags != old_flags) |
| 4626 | dev_set_rx_mode(dev); |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4627 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 4629 | EXPORT_SYMBOL(dev_set_promiscuity); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | |
| 4631 | /** |
| 4632 | * dev_set_allmulti - update allmulti count on a device |
| 4633 | * @dev: device |
| 4634 | * @inc: modifier |
| 4635 | * |
| 4636 | * Add or remove reception of all multicast frames to a device. While the |
| 4637 | * count in the device remains above zero the interface remains listening |
| 4638 | * to all interfaces. Once it hits zero the device reverts back to normal |
| 4639 | * filtering operation. A negative @inc value is used to drop the counter |
| 4640 | * when releasing a resource needing all multicasts. |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4641 | * Return 0 if successful or a negative errno code on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | */ |
| 4643 | |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4644 | int dev_set_allmulti(struct net_device *dev, int inc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | { |
Eric Dumazet | b536db9 | 2011-11-30 21:42:26 +0000 | [diff] [blame] | 4646 | unsigned int old_flags = dev->flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | |
Patrick McHardy | 2402345 | 2007-07-14 18:51:31 -0700 | [diff] [blame] | 4648 | ASSERT_RTNL(); |
| 4649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4650 | dev->flags |= IFF_ALLMULTI; |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4651 | dev->allmulti += inc; |
| 4652 | if (dev->allmulti == 0) { |
| 4653 | /* |
| 4654 | * Avoid overflow. |
| 4655 | * If inc causes overflow, untouch allmulti and return error. |
| 4656 | */ |
| 4657 | if (inc < 0) |
| 4658 | dev->flags &= ~IFF_ALLMULTI; |
| 4659 | else { |
| 4660 | dev->allmulti -= inc; |
Joe Perches | 7b6cd1c | 2012-02-01 10:54:43 +0000 | [diff] [blame] | 4661 | pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n", |
| 4662 | dev->name); |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4663 | return -EOVERFLOW; |
| 4664 | } |
| 4665 | } |
Patrick McHardy | 2402345 | 2007-07-14 18:51:31 -0700 | [diff] [blame] | 4666 | if (dev->flags ^ old_flags) { |
Patrick McHardy | b6c40d6 | 2008-10-07 15:26:48 -0700 | [diff] [blame] | 4667 | dev_change_rx_flags(dev, IFF_ALLMULTI); |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4668 | dev_set_rx_mode(dev); |
Patrick McHardy | 2402345 | 2007-07-14 18:51:31 -0700 | [diff] [blame] | 4669 | } |
Wang Chen | dad9b33 | 2008-06-18 01:48:28 -0700 | [diff] [blame] | 4670 | return 0; |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4671 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 4672 | EXPORT_SYMBOL(dev_set_allmulti); |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4673 | |
| 4674 | /* |
| 4675 | * Upload unicast and multicast address lists to device and |
| 4676 | * configure RX filtering. When the device doesn't support unicast |
Joe Perches | 53ccaae | 2007-12-20 14:02:06 -0800 | [diff] [blame] | 4677 | * filtering it is put in promiscuous mode while unicast addresses |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4678 | * are present. |
| 4679 | */ |
| 4680 | void __dev_set_rx_mode(struct net_device *dev) |
| 4681 | { |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 4682 | const struct net_device_ops *ops = dev->netdev_ops; |
| 4683 | |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4684 | /* dev_open will call this function so the list will stay sane. */ |
| 4685 | if (!(dev->flags&IFF_UP)) |
| 4686 | return; |
| 4687 | |
| 4688 | if (!netif_device_present(dev)) |
YOSHIFUJI Hideaki | 40b77c9 | 2007-07-19 10:43:23 +0900 | [diff] [blame] | 4689 | return; |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4690 | |
Jiri Pirko | 0178934 | 2011-08-16 06:29:00 +0000 | [diff] [blame] | 4691 | if (!(dev->priv_flags & IFF_UNICAST_FLT)) { |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4692 | /* Unicast addresses changes may only happen under the rtnl, |
| 4693 | * therefore calling __dev_set_promiscuity here is safe. |
| 4694 | */ |
Jiri Pirko | 32e7bfc | 2010-01-25 13:36:10 -0800 | [diff] [blame] | 4695 | if (!netdev_uc_empty(dev) && !dev->uc_promisc) { |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4696 | __dev_set_promiscuity(dev, 1); |
Joe Perches | 2d348d1 | 2011-07-25 16:17:35 -0700 | [diff] [blame] | 4697 | dev->uc_promisc = true; |
Jiri Pirko | 32e7bfc | 2010-01-25 13:36:10 -0800 | [diff] [blame] | 4698 | } else if (netdev_uc_empty(dev) && dev->uc_promisc) { |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4699 | __dev_set_promiscuity(dev, -1); |
Joe Perches | 2d348d1 | 2011-07-25 16:17:35 -0700 | [diff] [blame] | 4700 | dev->uc_promisc = false; |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4701 | } |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4702 | } |
Jiri Pirko | 0178934 | 2011-08-16 06:29:00 +0000 | [diff] [blame] | 4703 | |
| 4704 | if (ops->ndo_set_rx_mode) |
| 4705 | ops->ndo_set_rx_mode(dev); |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4706 | } |
| 4707 | |
| 4708 | void dev_set_rx_mode(struct net_device *dev) |
| 4709 | { |
David S. Miller | b9e4085 | 2008-07-15 00:15:08 -0700 | [diff] [blame] | 4710 | netif_addr_lock_bh(dev); |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4711 | __dev_set_rx_mode(dev); |
David S. Miller | b9e4085 | 2008-07-15 00:15:08 -0700 | [diff] [blame] | 4712 | netif_addr_unlock_bh(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | } |
| 4714 | |
Stephen Hemminger | f0db275 | 2008-09-30 02:23:58 -0700 | [diff] [blame] | 4715 | /** |
| 4716 | * dev_get_flags - get flags reported to userspace |
| 4717 | * @dev: device |
| 4718 | * |
| 4719 | * Get the combination of flag bits exported through APIs to userspace. |
| 4720 | */ |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4721 | unsigned int dev_get_flags(const struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4722 | { |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 4723 | unsigned int flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | |
| 4725 | flags = (dev->flags & ~(IFF_PROMISC | |
| 4726 | IFF_ALLMULTI | |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 4727 | IFF_RUNNING | |
| 4728 | IFF_LOWER_UP | |
| 4729 | IFF_DORMANT)) | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4730 | (dev->gflags & (IFF_PROMISC | |
| 4731 | IFF_ALLMULTI)); |
| 4732 | |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 4733 | if (netif_running(dev)) { |
| 4734 | if (netif_oper_up(dev)) |
| 4735 | flags |= IFF_RUNNING; |
| 4736 | if (netif_carrier_ok(dev)) |
| 4737 | flags |= IFF_LOWER_UP; |
| 4738 | if (netif_dormant(dev)) |
| 4739 | flags |= IFF_DORMANT; |
| 4740 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4741 | |
| 4742 | return flags; |
| 4743 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 4744 | EXPORT_SYMBOL(dev_get_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4745 | |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 4746 | int __dev_change_flags(struct net_device *dev, unsigned int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4747 | { |
Eric Dumazet | b536db9 | 2011-11-30 21:42:26 +0000 | [diff] [blame] | 4748 | unsigned int old_flags = dev->flags; |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 4749 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4750 | |
Patrick McHardy | 2402345 | 2007-07-14 18:51:31 -0700 | [diff] [blame] | 4751 | ASSERT_RTNL(); |
| 4752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | /* |
| 4754 | * Set the flags on our device. |
| 4755 | */ |
| 4756 | |
| 4757 | dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP | |
| 4758 | IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL | |
| 4759 | IFF_AUTOMEDIA)) | |
| 4760 | (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC | |
| 4761 | IFF_ALLMULTI)); |
| 4762 | |
| 4763 | /* |
| 4764 | * Load in the correct multicast list now the flags have changed. |
| 4765 | */ |
| 4766 | |
Patrick McHardy | b6c40d6 | 2008-10-07 15:26:48 -0700 | [diff] [blame] | 4767 | if ((old_flags ^ flags) & IFF_MULTICAST) |
| 4768 | dev_change_rx_flags(dev, IFF_MULTICAST); |
Patrick McHardy | 2402345 | 2007-07-14 18:51:31 -0700 | [diff] [blame] | 4769 | |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4770 | dev_set_rx_mode(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4771 | |
| 4772 | /* |
| 4773 | * Have we downed the interface. We handle IFF_UP ourselves |
| 4774 | * according to user attempts to set it, rather than blindly |
| 4775 | * setting it. |
| 4776 | */ |
| 4777 | |
| 4778 | ret = 0; |
| 4779 | if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */ |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 4780 | ret = ((old_flags & IFF_UP) ? __dev_close : __dev_open)(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4781 | |
| 4782 | if (!ret) |
Patrick McHardy | 4417da6 | 2007-06-27 01:28:10 -0700 | [diff] [blame] | 4783 | dev_set_rx_mode(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4784 | } |
| 4785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4786 | if ((flags ^ dev->gflags) & IFF_PROMISC) { |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 4787 | int inc = (flags & IFF_PROMISC) ? 1 : -1; |
| 4788 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | dev->gflags ^= IFF_PROMISC; |
| 4790 | dev_set_promiscuity(dev, inc); |
| 4791 | } |
| 4792 | |
| 4793 | /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI |
| 4794 | is important. Some (broken) drivers set IFF_PROMISC, when |
| 4795 | IFF_ALLMULTI is requested not asking us and not reporting. |
| 4796 | */ |
| 4797 | if ((flags ^ dev->gflags) & IFF_ALLMULTI) { |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 4798 | int inc = (flags & IFF_ALLMULTI) ? 1 : -1; |
| 4799 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4800 | dev->gflags ^= IFF_ALLMULTI; |
| 4801 | dev_set_allmulti(dev, inc); |
| 4802 | } |
| 4803 | |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 4804 | return ret; |
| 4805 | } |
| 4806 | |
| 4807 | void __dev_notify_flags(struct net_device *dev, unsigned int old_flags) |
| 4808 | { |
| 4809 | unsigned int changes = dev->flags ^ old_flags; |
| 4810 | |
| 4811 | if (changes & IFF_UP) { |
| 4812 | if (dev->flags & IFF_UP) |
| 4813 | call_netdevice_notifiers(NETDEV_UP, dev); |
| 4814 | else |
| 4815 | call_netdevice_notifiers(NETDEV_DOWN, dev); |
| 4816 | } |
| 4817 | |
| 4818 | if (dev->flags & IFF_UP && |
Jiri Pirko | be9efd3 | 2013-05-28 01:30:22 +0000 | [diff] [blame] | 4819 | (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) { |
| 4820 | struct netdev_notifier_change_info change_info; |
| 4821 | |
| 4822 | change_info.flags_changed = changes; |
| 4823 | call_netdevice_notifiers_info(NETDEV_CHANGE, dev, |
| 4824 | &change_info.info); |
| 4825 | } |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 4826 | } |
| 4827 | |
| 4828 | /** |
| 4829 | * dev_change_flags - change device settings |
| 4830 | * @dev: device |
| 4831 | * @flags: device state flags |
| 4832 | * |
| 4833 | * Change settings on device based state flags. The flags are |
| 4834 | * in the userspace exported format. |
| 4835 | */ |
Eric Dumazet | b536db9 | 2011-11-30 21:42:26 +0000 | [diff] [blame] | 4836 | int dev_change_flags(struct net_device *dev, unsigned int flags) |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 4837 | { |
Eric Dumazet | b536db9 | 2011-11-30 21:42:26 +0000 | [diff] [blame] | 4838 | int ret; |
| 4839 | unsigned int changes, old_flags = dev->flags; |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 4840 | |
| 4841 | ret = __dev_change_flags(dev, flags); |
| 4842 | if (ret < 0) |
| 4843 | return ret; |
| 4844 | |
| 4845 | changes = old_flags ^ dev->flags; |
Thomas Graf | 7c355f5 | 2007-06-05 16:03:03 -0700 | [diff] [blame] | 4846 | if (changes) |
| 4847 | rtmsg_ifinfo(RTM_NEWLINK, dev, changes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4848 | |
Patrick McHardy | bd38081 | 2010-02-26 06:34:53 +0000 | [diff] [blame] | 4849 | __dev_notify_flags(dev, old_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4850 | return ret; |
| 4851 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 4852 | EXPORT_SYMBOL(dev_change_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | |
Stephen Hemminger | f0db275 | 2008-09-30 02:23:58 -0700 | [diff] [blame] | 4854 | /** |
| 4855 | * dev_set_mtu - Change maximum transfer unit |
| 4856 | * @dev: device |
| 4857 | * @new_mtu: new transfer unit |
| 4858 | * |
| 4859 | * Change the maximum transfer size of the network device. |
| 4860 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4861 | int dev_set_mtu(struct net_device *dev, int new_mtu) |
| 4862 | { |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 4863 | const struct net_device_ops *ops = dev->netdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4864 | int err; |
| 4865 | |
| 4866 | if (new_mtu == dev->mtu) |
| 4867 | return 0; |
| 4868 | |
| 4869 | /* MTU must be positive. */ |
| 4870 | if (new_mtu < 0) |
| 4871 | return -EINVAL; |
| 4872 | |
| 4873 | if (!netif_device_present(dev)) |
| 4874 | return -ENODEV; |
| 4875 | |
| 4876 | err = 0; |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 4877 | if (ops->ndo_change_mtu) |
| 4878 | err = ops->ndo_change_mtu(dev, new_mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4879 | else |
| 4880 | dev->mtu = new_mtu; |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 4881 | |
Jiri Pirko | e3d8fab | 2012-12-03 01:16:32 +0000 | [diff] [blame] | 4882 | if (!err) |
Pavel Emelyanov | 056925a | 2007-09-16 15:42:43 -0700 | [diff] [blame] | 4883 | call_netdevice_notifiers(NETDEV_CHANGEMTU, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4884 | return err; |
| 4885 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 4886 | EXPORT_SYMBOL(dev_set_mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4887 | |
Stephen Hemminger | f0db275 | 2008-09-30 02:23:58 -0700 | [diff] [blame] | 4888 | /** |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 4889 | * dev_set_group - Change group this device belongs to |
| 4890 | * @dev: device |
| 4891 | * @new_group: group this device should belong to |
| 4892 | */ |
| 4893 | void dev_set_group(struct net_device *dev, int new_group) |
| 4894 | { |
| 4895 | dev->group = new_group; |
| 4896 | } |
| 4897 | EXPORT_SYMBOL(dev_set_group); |
| 4898 | |
| 4899 | /** |
Stephen Hemminger | f0db275 | 2008-09-30 02:23:58 -0700 | [diff] [blame] | 4900 | * dev_set_mac_address - Change Media Access Control Address |
| 4901 | * @dev: device |
| 4902 | * @sa: new address |
| 4903 | * |
| 4904 | * Change the hardware (MAC) address of the device |
| 4905 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4906 | int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa) |
| 4907 | { |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 4908 | const struct net_device_ops *ops = dev->netdev_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4909 | int err; |
| 4910 | |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 4911 | if (!ops->ndo_set_mac_address) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | return -EOPNOTSUPP; |
| 4913 | if (sa->sa_family != dev->type) |
| 4914 | return -EINVAL; |
| 4915 | if (!netif_device_present(dev)) |
| 4916 | return -ENODEV; |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 4917 | err = ops->ndo_set_mac_address(dev, sa); |
Jiri Pirko | f652151 | 2013-01-01 03:30:14 +0000 | [diff] [blame] | 4918 | if (err) |
| 4919 | return err; |
Jiri Pirko | fbdeca2 | 2013-01-01 03:30:16 +0000 | [diff] [blame] | 4920 | dev->addr_assign_type = NET_ADDR_SET; |
Jiri Pirko | f652151 | 2013-01-01 03:30:14 +0000 | [diff] [blame] | 4921 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
Theodore Ts'o | 7bf2357 | 2012-07-04 21:23:25 -0400 | [diff] [blame] | 4922 | add_device_randomness(dev->dev_addr, dev->addr_len); |
Jiri Pirko | f652151 | 2013-01-01 03:30:14 +0000 | [diff] [blame] | 4923 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 4925 | EXPORT_SYMBOL(dev_set_mac_address); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4926 | |
Jiri Pirko | 4bf84c3 | 2012-12-27 23:49:37 +0000 | [diff] [blame] | 4927 | /** |
| 4928 | * dev_change_carrier - Change device carrier |
| 4929 | * @dev: device |
Randy Dunlap | 691b3b7 | 2013-03-04 12:32:43 +0000 | [diff] [blame] | 4930 | * @new_carrier: new value |
Jiri Pirko | 4bf84c3 | 2012-12-27 23:49:37 +0000 | [diff] [blame] | 4931 | * |
| 4932 | * Change device carrier |
| 4933 | */ |
| 4934 | int dev_change_carrier(struct net_device *dev, bool new_carrier) |
| 4935 | { |
| 4936 | const struct net_device_ops *ops = dev->netdev_ops; |
| 4937 | |
| 4938 | if (!ops->ndo_change_carrier) |
| 4939 | return -EOPNOTSUPP; |
| 4940 | if (!netif_device_present(dev)) |
| 4941 | return -ENODEV; |
| 4942 | return ops->ndo_change_carrier(dev, new_carrier); |
| 4943 | } |
| 4944 | EXPORT_SYMBOL(dev_change_carrier); |
| 4945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4946 | /** |
| 4947 | * dev_new_index - allocate an ifindex |
Randy Dunlap | c4ea43c | 2007-10-12 21:17:49 -0700 | [diff] [blame] | 4948 | * @net: the applicable net namespace |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4949 | * |
| 4950 | * Returns a suitable unique value for a new device interface |
| 4951 | * number. The caller must hold the rtnl semaphore or the |
| 4952 | * dev_base_lock to be sure it remains unique. |
| 4953 | */ |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 4954 | static int dev_new_index(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | { |
Pavel Emelyanov | aa79e66 | 2012-08-08 21:53:19 +0000 | [diff] [blame] | 4956 | int ifindex = net->ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | for (;;) { |
| 4958 | if (++ifindex <= 0) |
| 4959 | ifindex = 1; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 4960 | if (!__dev_get_by_index(net, ifindex)) |
Pavel Emelyanov | aa79e66 | 2012-08-08 21:53:19 +0000 | [diff] [blame] | 4961 | return net->ifindex = ifindex; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | } |
| 4963 | } |
| 4964 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4965 | /* Delayed registration/unregisteration */ |
Denis Cheng | 3b5b34f | 2007-12-07 00:49:17 -0800 | [diff] [blame] | 4966 | static LIST_HEAD(net_todo_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | |
Stephen Hemminger | 6f05f62 | 2007-03-08 20:46:03 -0800 | [diff] [blame] | 4968 | static void net_set_todo(struct net_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4970 | list_add_tail(&dev->todo_list, &net_todo_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4971 | } |
| 4972 | |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 4973 | static void rollback_registered_many(struct list_head *head) |
Daniel Lezcano | 93ee31f | 2007-10-30 15:38:18 -0700 | [diff] [blame] | 4974 | { |
Krishna Kumar | e93737b | 2009-12-08 22:26:02 +0000 | [diff] [blame] | 4975 | struct net_device *dev, *tmp; |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 4976 | |
Daniel Lezcano | 93ee31f | 2007-10-30 15:38:18 -0700 | [diff] [blame] | 4977 | BUG_ON(dev_boot_phase); |
| 4978 | ASSERT_RTNL(); |
| 4979 | |
Krishna Kumar | e93737b | 2009-12-08 22:26:02 +0000 | [diff] [blame] | 4980 | list_for_each_entry_safe(dev, tmp, head, unreg_list) { |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 4981 | /* Some devices call without registering |
Krishna Kumar | e93737b | 2009-12-08 22:26:02 +0000 | [diff] [blame] | 4982 | * for initialization unwind. Remove those |
| 4983 | * devices and proceed with the remaining. |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 4984 | */ |
| 4985 | if (dev->reg_state == NETREG_UNINITIALIZED) { |
Joe Perches | 7b6cd1c | 2012-02-01 10:54:43 +0000 | [diff] [blame] | 4986 | pr_debug("unregister_netdevice: device %s/%p never was registered\n", |
| 4987 | dev->name, dev); |
Daniel Lezcano | 93ee31f | 2007-10-30 15:38:18 -0700 | [diff] [blame] | 4988 | |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 4989 | WARN_ON(1); |
Krishna Kumar | e93737b | 2009-12-08 22:26:02 +0000 | [diff] [blame] | 4990 | list_del(&dev->unreg_list); |
| 4991 | continue; |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 4992 | } |
Eric Dumazet | 449f454 | 2011-05-19 12:24:16 +0000 | [diff] [blame] | 4993 | dev->dismantle = true; |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 4994 | BUG_ON(dev->reg_state != NETREG_REGISTERED); |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 4995 | } |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 4996 | |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 4997 | /* If device is running, close it first. */ |
| 4998 | dev_close_many(head); |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 4999 | |
Octavian Purdila | 4434572 | 2010-12-13 12:44:07 +0000 | [diff] [blame] | 5000 | list_for_each_entry(dev, head, unreg_list) { |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 5001 | /* And unlink it from device chain. */ |
| 5002 | unlist_netdevice(dev); |
| 5003 | |
| 5004 | dev->reg_state = NETREG_UNREGISTERING; |
Daniel Lezcano | 93ee31f | 2007-10-30 15:38:18 -0700 | [diff] [blame] | 5005 | } |
| 5006 | |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 5007 | synchronize_net(); |
Daniel Lezcano | 93ee31f | 2007-10-30 15:38:18 -0700 | [diff] [blame] | 5008 | |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 5009 | list_for_each_entry(dev, head, unreg_list) { |
| 5010 | /* Shutdown queueing discipline. */ |
| 5011 | dev_shutdown(dev); |
Daniel Lezcano | 93ee31f | 2007-10-30 15:38:18 -0700 | [diff] [blame] | 5012 | |
Daniel Lezcano | 93ee31f | 2007-10-30 15:38:18 -0700 | [diff] [blame] | 5013 | |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 5014 | /* Notify protocols, that we are about to destroy |
| 5015 | this device. They should clean all the things. |
| 5016 | */ |
| 5017 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
| 5018 | |
Patrick McHardy | a283576 | 2010-02-26 06:34:51 +0000 | [diff] [blame] | 5019 | if (!dev->rtnl_link_ops || |
| 5020 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) |
| 5021 | rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); |
| 5022 | |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 5023 | /* |
| 5024 | * Flush the unicast and multicast chains |
| 5025 | */ |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 5026 | dev_uc_flush(dev); |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 5027 | dev_mc_flush(dev); |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 5028 | |
| 5029 | if (dev->netdev_ops->ndo_uninit) |
| 5030 | dev->netdev_ops->ndo_uninit(dev); |
| 5031 | |
Jiri Pirko | 9ff162a | 2013-01-03 22:48:49 +0000 | [diff] [blame] | 5032 | /* Notifier chain MUST detach us all upper devices. */ |
| 5033 | WARN_ON(netdev_has_any_upper_dev(dev)); |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 5034 | |
| 5035 | /* Remove entries from kobject tree */ |
| 5036 | netdev_unregister_kobject(dev); |
Alexander Duyck | 024e967 | 2013-01-10 08:57:46 +0000 | [diff] [blame] | 5037 | #ifdef CONFIG_XPS |
| 5038 | /* Remove XPS queueing entries */ |
| 5039 | netif_reset_xps_queues_gt(dev, 0); |
| 5040 | #endif |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 5041 | } |
Daniel Lezcano | 93ee31f | 2007-10-30 15:38:18 -0700 | [diff] [blame] | 5042 | |
Eric W. Biederman | 850a545 | 2011-10-13 22:25:23 +0000 | [diff] [blame] | 5043 | synchronize_net(); |
Daniel Lezcano | 93ee31f | 2007-10-30 15:38:18 -0700 | [diff] [blame] | 5044 | |
Eric W. Biederman | a5ee155 | 2009-11-29 15:45:58 +0000 | [diff] [blame] | 5045 | list_for_each_entry(dev, head, unreg_list) |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 5046 | dev_put(dev); |
| 5047 | } |
Daniel Lezcano | 93ee31f | 2007-10-30 15:38:18 -0700 | [diff] [blame] | 5048 | |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 5049 | static void rollback_registered(struct net_device *dev) |
| 5050 | { |
| 5051 | LIST_HEAD(single); |
Daniel Lezcano | 93ee31f | 2007-10-30 15:38:18 -0700 | [diff] [blame] | 5052 | |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 5053 | list_add(&dev->unreg_list, &single); |
| 5054 | rollback_registered_many(&single); |
Eric Dumazet | ceaaec9 | 2011-02-17 22:59:19 +0000 | [diff] [blame] | 5055 | list_del(&single); |
Daniel Lezcano | 93ee31f | 2007-10-30 15:38:18 -0700 | [diff] [blame] | 5056 | } |
| 5057 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 5058 | static netdev_features_t netdev_fix_features(struct net_device *dev, |
| 5059 | netdev_features_t features) |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 5060 | { |
Michał Mirosław | 57422dc | 2011-01-22 12:14:12 +0000 | [diff] [blame] | 5061 | /* Fix illegal checksum combinations */ |
| 5062 | if ((features & NETIF_F_HW_CSUM) && |
| 5063 | (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) { |
Michał Mirosław | 6f404e4 | 2011-05-16 15:14:21 -0400 | [diff] [blame] | 5064 | netdev_warn(dev, "mixed HW and IP checksum settings.\n"); |
Michał Mirosław | 57422dc | 2011-01-22 12:14:12 +0000 | [diff] [blame] | 5065 | features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM); |
| 5066 | } |
| 5067 | |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 5068 | /* TSO requires that SG is present as well. */ |
Ben Hutchings | ea2d368 | 2011-04-12 14:38:37 +0000 | [diff] [blame] | 5069 | if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) { |
Michał Mirosław | 6f404e4 | 2011-05-16 15:14:21 -0400 | [diff] [blame] | 5070 | netdev_dbg(dev, "Dropping TSO features since no SG feature.\n"); |
Ben Hutchings | ea2d368 | 2011-04-12 14:38:37 +0000 | [diff] [blame] | 5071 | features &= ~NETIF_F_ALL_TSO; |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 5072 | } |
| 5073 | |
Pravin B Shelar | ec5f061 | 2013-03-07 09:28:01 +0000 | [diff] [blame] | 5074 | if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) && |
| 5075 | !(features & NETIF_F_IP_CSUM)) { |
| 5076 | netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n"); |
| 5077 | features &= ~NETIF_F_TSO; |
| 5078 | features &= ~NETIF_F_TSO_ECN; |
| 5079 | } |
| 5080 | |
| 5081 | if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) && |
| 5082 | !(features & NETIF_F_IPV6_CSUM)) { |
| 5083 | netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n"); |
| 5084 | features &= ~NETIF_F_TSO6; |
| 5085 | } |
| 5086 | |
Ben Hutchings | 31d8b9e | 2011-04-12 14:47:15 +0000 | [diff] [blame] | 5087 | /* TSO ECN requires that TSO is present as well. */ |
| 5088 | if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN) |
| 5089 | features &= ~NETIF_F_TSO_ECN; |
| 5090 | |
Michał Mirosław | 212b573 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 5091 | /* Software GSO depends on SG. */ |
| 5092 | if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) { |
Michał Mirosław | 6f404e4 | 2011-05-16 15:14:21 -0400 | [diff] [blame] | 5093 | netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n"); |
Michał Mirosław | 212b573 | 2011-02-15 16:59:16 +0000 | [diff] [blame] | 5094 | features &= ~NETIF_F_GSO; |
| 5095 | } |
| 5096 | |
Michał Mirosław | acd1130 | 2011-01-24 15:45:15 -0800 | [diff] [blame] | 5097 | /* UFO needs SG and checksumming */ |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 5098 | if (features & NETIF_F_UFO) { |
Michał Mirosław | 7903264 | 2010-11-30 06:38:00 +0000 | [diff] [blame] | 5099 | /* maybe split UFO into V4 and V6? */ |
| 5100 | if (!((features & NETIF_F_GEN_CSUM) || |
| 5101 | (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)) |
| 5102 | == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) { |
Michał Mirosław | 6f404e4 | 2011-05-16 15:14:21 -0400 | [diff] [blame] | 5103 | netdev_dbg(dev, |
Michał Mirosław | acd1130 | 2011-01-24 15:45:15 -0800 | [diff] [blame] | 5104 | "Dropping NETIF_F_UFO since no checksum offload features.\n"); |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 5105 | features &= ~NETIF_F_UFO; |
| 5106 | } |
| 5107 | |
| 5108 | if (!(features & NETIF_F_SG)) { |
Michał Mirosław | 6f404e4 | 2011-05-16 15:14:21 -0400 | [diff] [blame] | 5109 | netdev_dbg(dev, |
Michał Mirosław | acd1130 | 2011-01-24 15:45:15 -0800 | [diff] [blame] | 5110 | "Dropping NETIF_F_UFO since no NETIF_F_SG feature.\n"); |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 5111 | features &= ~NETIF_F_UFO; |
| 5112 | } |
| 5113 | } |
| 5114 | |
| 5115 | return features; |
| 5116 | } |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 5117 | |
Michał Mirosław | 6cb6a27 | 2011-04-02 22:48:47 -0700 | [diff] [blame] | 5118 | int __netdev_update_features(struct net_device *dev) |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 5119 | { |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 5120 | netdev_features_t features; |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 5121 | int err = 0; |
| 5122 | |
Michał Mirosław | 8726748 | 2011-04-12 09:56:38 +0000 | [diff] [blame] | 5123 | ASSERT_RTNL(); |
| 5124 | |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 5125 | features = netdev_get_wanted_features(dev); |
| 5126 | |
| 5127 | if (dev->netdev_ops->ndo_fix_features) |
| 5128 | features = dev->netdev_ops->ndo_fix_features(dev, features); |
| 5129 | |
| 5130 | /* driver might be less strict about feature dependencies */ |
| 5131 | features = netdev_fix_features(dev, features); |
| 5132 | |
| 5133 | if (dev->features == features) |
Michał Mirosław | 6cb6a27 | 2011-04-02 22:48:47 -0700 | [diff] [blame] | 5134 | return 0; |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 5135 | |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 5136 | netdev_dbg(dev, "Features changed: %pNF -> %pNF\n", |
| 5137 | &dev->features, &features); |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 5138 | |
| 5139 | if (dev->netdev_ops->ndo_set_features) |
| 5140 | err = dev->netdev_ops->ndo_set_features(dev, features); |
| 5141 | |
Michał Mirosław | 6cb6a27 | 2011-04-02 22:48:47 -0700 | [diff] [blame] | 5142 | if (unlikely(err < 0)) { |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 5143 | netdev_err(dev, |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 5144 | "set_features() failed (%d); wanted %pNF, left %pNF\n", |
| 5145 | err, &features, &dev->features); |
Michał Mirosław | 6cb6a27 | 2011-04-02 22:48:47 -0700 | [diff] [blame] | 5146 | return -1; |
| 5147 | } |
| 5148 | |
| 5149 | if (!err) |
| 5150 | dev->features = features; |
| 5151 | |
| 5152 | return 1; |
| 5153 | } |
| 5154 | |
Michał Mirosław | afe12cc | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 5155 | /** |
| 5156 | * netdev_update_features - recalculate device features |
| 5157 | * @dev: the device to check |
| 5158 | * |
| 5159 | * Recalculate dev->features set and send notifications if it |
| 5160 | * has changed. Should be called after driver or hardware dependent |
| 5161 | * conditions might have changed that influence the features. |
| 5162 | */ |
Michał Mirosław | 6cb6a27 | 2011-04-02 22:48:47 -0700 | [diff] [blame] | 5163 | void netdev_update_features(struct net_device *dev) |
| 5164 | { |
| 5165 | if (__netdev_update_features(dev)) |
| 5166 | netdev_features_change(dev); |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 5167 | } |
| 5168 | EXPORT_SYMBOL(netdev_update_features); |
| 5169 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | /** |
Michał Mirosław | afe12cc | 2011-05-07 03:22:17 +0000 | [diff] [blame] | 5171 | * netdev_change_features - recalculate device features |
| 5172 | * @dev: the device to check |
| 5173 | * |
| 5174 | * Recalculate dev->features set and send notifications even |
| 5175 | * if they have not changed. Should be called instead of |
| 5176 | * netdev_update_features() if also dev->vlan_features might |
| 5177 | * have changed to allow the changes to be propagated to stacked |
| 5178 | * VLAN devices. |
| 5179 | */ |
| 5180 | void netdev_change_features(struct net_device *dev) |
| 5181 | { |
| 5182 | __netdev_update_features(dev); |
| 5183 | netdev_features_change(dev); |
| 5184 | } |
| 5185 | EXPORT_SYMBOL(netdev_change_features); |
| 5186 | |
| 5187 | /** |
Patrick Mullaney | fc4a748 | 2009-12-03 15:59:22 -0800 | [diff] [blame] | 5188 | * netif_stacked_transfer_operstate - transfer operstate |
| 5189 | * @rootdev: the root or lower level device to transfer state from |
| 5190 | * @dev: the device to transfer operstate to |
| 5191 | * |
| 5192 | * Transfer operational state from root to device. This is normally |
| 5193 | * called when a stacking relationship exists between the root |
| 5194 | * device and the device(a leaf device). |
| 5195 | */ |
| 5196 | void netif_stacked_transfer_operstate(const struct net_device *rootdev, |
| 5197 | struct net_device *dev) |
| 5198 | { |
| 5199 | if (rootdev->operstate == IF_OPER_DORMANT) |
| 5200 | netif_dormant_on(dev); |
| 5201 | else |
| 5202 | netif_dormant_off(dev); |
| 5203 | |
| 5204 | if (netif_carrier_ok(rootdev)) { |
| 5205 | if (!netif_carrier_ok(dev)) |
| 5206 | netif_carrier_on(dev); |
| 5207 | } else { |
| 5208 | if (netif_carrier_ok(dev)) |
| 5209 | netif_carrier_off(dev); |
| 5210 | } |
| 5211 | } |
| 5212 | EXPORT_SYMBOL(netif_stacked_transfer_operstate); |
| 5213 | |
Tom Herbert | bf26414 | 2010-11-26 08:36:09 +0000 | [diff] [blame] | 5214 | #ifdef CONFIG_RPS |
Eric Dumazet | 1b4bf46 | 2010-09-23 17:26:35 +0000 | [diff] [blame] | 5215 | static int netif_alloc_rx_queues(struct net_device *dev) |
| 5216 | { |
Eric Dumazet | 1b4bf46 | 2010-09-23 17:26:35 +0000 | [diff] [blame] | 5217 | unsigned int i, count = dev->num_rx_queues; |
Tom Herbert | bd25fa7 | 2010-10-18 18:00:16 +0000 | [diff] [blame] | 5218 | struct netdev_rx_queue *rx; |
Eric Dumazet | 1b4bf46 | 2010-09-23 17:26:35 +0000 | [diff] [blame] | 5219 | |
Tom Herbert | bd25fa7 | 2010-10-18 18:00:16 +0000 | [diff] [blame] | 5220 | BUG_ON(count < 1); |
Eric Dumazet | 1b4bf46 | 2010-09-23 17:26:35 +0000 | [diff] [blame] | 5221 | |
Tom Herbert | bd25fa7 | 2010-10-18 18:00:16 +0000 | [diff] [blame] | 5222 | rx = kcalloc(count, sizeof(struct netdev_rx_queue), GFP_KERNEL); |
Joe Perches | 62b5942 | 2013-02-04 16:48:16 +0000 | [diff] [blame] | 5223 | if (!rx) |
Tom Herbert | bd25fa7 | 2010-10-18 18:00:16 +0000 | [diff] [blame] | 5224 | return -ENOMEM; |
Joe Perches | 62b5942 | 2013-02-04 16:48:16 +0000 | [diff] [blame] | 5225 | |
Tom Herbert | bd25fa7 | 2010-10-18 18:00:16 +0000 | [diff] [blame] | 5226 | dev->_rx = rx; |
| 5227 | |
Tom Herbert | bd25fa7 | 2010-10-18 18:00:16 +0000 | [diff] [blame] | 5228 | for (i = 0; i < count; i++) |
Tom Herbert | fe82224 | 2010-11-09 10:47:38 +0000 | [diff] [blame] | 5229 | rx[i].dev = dev; |
Eric Dumazet | 1b4bf46 | 2010-09-23 17:26:35 +0000 | [diff] [blame] | 5230 | return 0; |
| 5231 | } |
Tom Herbert | bf26414 | 2010-11-26 08:36:09 +0000 | [diff] [blame] | 5232 | #endif |
Eric Dumazet | 1b4bf46 | 2010-09-23 17:26:35 +0000 | [diff] [blame] | 5233 | |
Changli Gao | aa94210 | 2010-12-04 02:31:41 +0000 | [diff] [blame] | 5234 | static void netdev_init_one_queue(struct net_device *dev, |
| 5235 | struct netdev_queue *queue, void *_unused) |
| 5236 | { |
| 5237 | /* Initialize queue lock */ |
| 5238 | spin_lock_init(&queue->_xmit_lock); |
| 5239 | netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type); |
| 5240 | queue->xmit_lock_owner = -1; |
Changli Gao | b236da6 | 2010-12-14 03:09:15 +0000 | [diff] [blame] | 5241 | netdev_queue_numa_node_write(queue, NUMA_NO_NODE); |
Changli Gao | aa94210 | 2010-12-04 02:31:41 +0000 | [diff] [blame] | 5242 | queue->dev = dev; |
Tom Herbert | 114cf58 | 2011-11-28 16:33:09 +0000 | [diff] [blame] | 5243 | #ifdef CONFIG_BQL |
| 5244 | dql_init(&queue->dql, HZ); |
| 5245 | #endif |
Changli Gao | aa94210 | 2010-12-04 02:31:41 +0000 | [diff] [blame] | 5246 | } |
| 5247 | |
Eric Dumazet | 60877a3 | 2013-06-20 01:15:51 -0700 | [diff] [blame] | 5248 | static void netif_free_tx_queues(struct net_device *dev) |
| 5249 | { |
| 5250 | if (is_vmalloc_addr(dev->_tx)) |
| 5251 | vfree(dev->_tx); |
| 5252 | else |
| 5253 | kfree(dev->_tx); |
| 5254 | } |
| 5255 | |
Tom Herbert | e648493 | 2010-10-18 18:04:39 +0000 | [diff] [blame] | 5256 | static int netif_alloc_netdev_queues(struct net_device *dev) |
| 5257 | { |
| 5258 | unsigned int count = dev->num_tx_queues; |
| 5259 | struct netdev_queue *tx; |
Eric Dumazet | 60877a3 | 2013-06-20 01:15:51 -0700 | [diff] [blame] | 5260 | size_t sz = count * sizeof(*tx); |
Tom Herbert | e648493 | 2010-10-18 18:04:39 +0000 | [diff] [blame] | 5261 | |
Eric Dumazet | 60877a3 | 2013-06-20 01:15:51 -0700 | [diff] [blame] | 5262 | BUG_ON(count < 1 || count > 0xffff); |
Tom Herbert | e648493 | 2010-10-18 18:04:39 +0000 | [diff] [blame] | 5263 | |
Eric Dumazet | 60877a3 | 2013-06-20 01:15:51 -0700 | [diff] [blame] | 5264 | tx = kzalloc(sz, GFP_KERNEL | __GFP_NOWARN | __GFP_REPEAT); |
| 5265 | if (!tx) { |
| 5266 | tx = vzalloc(sz); |
| 5267 | if (!tx) |
| 5268 | return -ENOMEM; |
| 5269 | } |
Tom Herbert | e648493 | 2010-10-18 18:04:39 +0000 | [diff] [blame] | 5270 | dev->_tx = tx; |
Tom Herbert | 1d24eb4 | 2010-11-21 13:17:27 +0000 | [diff] [blame] | 5271 | |
Tom Herbert | e648493 | 2010-10-18 18:04:39 +0000 | [diff] [blame] | 5272 | netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL); |
| 5273 | spin_lock_init(&dev->tx_global_lock); |
Changli Gao | aa94210 | 2010-12-04 02:31:41 +0000 | [diff] [blame] | 5274 | |
| 5275 | return 0; |
Tom Herbert | e648493 | 2010-10-18 18:04:39 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
Patrick Mullaney | fc4a748 | 2009-12-03 15:59:22 -0800 | [diff] [blame] | 5278 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | * register_netdevice - register a network device |
| 5280 | * @dev: device to register |
| 5281 | * |
| 5282 | * Take a completed network device structure and add it to the kernel |
| 5283 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier |
| 5284 | * chain. 0 is returned on success. A negative errno code is returned |
| 5285 | * on a failure to set up the device, or if the name is a duplicate. |
| 5286 | * |
| 5287 | * Callers must hold the rtnl semaphore. You may want |
| 5288 | * register_netdev() instead of this. |
| 5289 | * |
| 5290 | * BUGS: |
| 5291 | * The locking appears insufficient to guarantee two parallel registers |
| 5292 | * will not get the same name. |
| 5293 | */ |
| 5294 | |
| 5295 | int register_netdevice(struct net_device *dev) |
| 5296 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | int ret; |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 5298 | struct net *net = dev_net(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5299 | |
| 5300 | BUG_ON(dev_boot_phase); |
| 5301 | ASSERT_RTNL(); |
| 5302 | |
Stephen Hemminger | b17a7c1 | 2006-05-10 13:21:17 -0700 | [diff] [blame] | 5303 | might_sleep(); |
| 5304 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5305 | /* When net_device's are persistent, this will be fatal. */ |
| 5306 | BUG_ON(dev->reg_state != NETREG_UNINITIALIZED); |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 5307 | BUG_ON(!net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5308 | |
David S. Miller | f1f28aa | 2008-07-15 00:08:33 -0700 | [diff] [blame] | 5309 | spin_lock_init(&dev->addr_list_lock); |
David S. Miller | cf508b1 | 2008-07-22 14:16:42 -0700 | [diff] [blame] | 5310 | netdev_set_addr_lockdep_class(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5311 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | dev->iflink = -1; |
| 5313 | |
Gao feng | 828de4f | 2012-09-13 20:58:27 +0000 | [diff] [blame] | 5314 | ret = dev_get_valid_name(net, dev, dev->name); |
Peter Pan(潘卫平) | 0696c3a | 2011-05-12 15:46:56 +0000 | [diff] [blame] | 5315 | if (ret < 0) |
| 5316 | goto out; |
| 5317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5318 | /* Init, if this function is available */ |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 5319 | if (dev->netdev_ops->ndo_init) { |
| 5320 | ret = dev->netdev_ops->ndo_init(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5321 | if (ret) { |
| 5322 | if (ret > 0) |
| 5323 | ret = -EIO; |
Adrian Bunk | 90833aa | 2006-11-13 16:02:22 -0800 | [diff] [blame] | 5324 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | } |
| 5326 | } |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 5327 | |
Patrick McHardy | f646968 | 2013-04-19 02:04:27 +0000 | [diff] [blame] | 5328 | if (((dev->hw_features | dev->features) & |
| 5329 | NETIF_F_HW_VLAN_CTAG_FILTER) && |
Michał Mirosław | d2ed273 | 2013-01-29 15:14:16 +0000 | [diff] [blame] | 5330 | (!dev->netdev_ops->ndo_vlan_rx_add_vid || |
| 5331 | !dev->netdev_ops->ndo_vlan_rx_kill_vid)) { |
| 5332 | netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n"); |
| 5333 | ret = -EINVAL; |
| 5334 | goto err_uninit; |
| 5335 | } |
| 5336 | |
Pavel Emelyanov | 9c7dafb | 2012-08-08 21:52:46 +0000 | [diff] [blame] | 5337 | ret = -EBUSY; |
| 5338 | if (!dev->ifindex) |
| 5339 | dev->ifindex = dev_new_index(net); |
| 5340 | else if (__dev_get_by_index(net, dev->ifindex)) |
| 5341 | goto err_uninit; |
| 5342 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5343 | if (dev->iflink == -1) |
| 5344 | dev->iflink = dev->ifindex; |
| 5345 | |
Michał Mirosław | 5455c69 | 2011-02-15 16:59:17 +0000 | [diff] [blame] | 5346 | /* Transfer changeable features to wanted_features and enable |
| 5347 | * software offloads (GSO and GRO). |
| 5348 | */ |
| 5349 | dev->hw_features |= NETIF_F_SOFT_FEATURES; |
Michał Mirosław | 14d1232 | 2011-02-22 16:52:28 +0000 | [diff] [blame] | 5350 | dev->features |= NETIF_F_SOFT_FEATURES; |
| 5351 | dev->wanted_features = dev->features & dev->hw_features; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5352 | |
Tom Herbert | c6e1a0d | 2011-04-04 22:30:30 -0700 | [diff] [blame] | 5353 | /* Turn on no cache copy if HW is doing checksum */ |
Michał Mirosław | 34324dc | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 5354 | if (!(dev->flags & IFF_LOOPBACK)) { |
| 5355 | dev->hw_features |= NETIF_F_NOCACHE_COPY; |
| 5356 | if (dev->features & NETIF_F_ALL_CSUM) { |
| 5357 | dev->wanted_features |= NETIF_F_NOCACHE_COPY; |
| 5358 | dev->features |= NETIF_F_NOCACHE_COPY; |
| 5359 | } |
Tom Herbert | c6e1a0d | 2011-04-04 22:30:30 -0700 | [diff] [blame] | 5360 | } |
| 5361 | |
Michał Mirosław | 1180e7d | 2011-07-14 14:41:11 -0700 | [diff] [blame] | 5362 | /* Make NETIF_F_HIGHDMA inheritable to VLAN devices. |
Brandon Philips | 16c3ea7 | 2010-09-15 09:24:24 +0000 | [diff] [blame] | 5363 | */ |
Michał Mirosław | 1180e7d | 2011-07-14 14:41:11 -0700 | [diff] [blame] | 5364 | dev->vlan_features |= NETIF_F_HIGHDMA; |
Brandon Philips | 16c3ea7 | 2010-09-15 09:24:24 +0000 | [diff] [blame] | 5365 | |
Pravin B Shelar | ee57967 | 2013-03-07 09:28:08 +0000 | [diff] [blame] | 5366 | /* Make NETIF_F_SG inheritable to tunnel devices. |
| 5367 | */ |
| 5368 | dev->hw_enc_features |= NETIF_F_SG; |
| 5369 | |
Simon Horman | 0d89d20 | 2013-05-23 21:02:52 +0000 | [diff] [blame] | 5370 | /* Make NETIF_F_SG inheritable to MPLS. |
| 5371 | */ |
| 5372 | dev->mpls_features |= NETIF_F_SG; |
| 5373 | |
Johannes Berg | 7ffbe3f | 2009-10-02 05:15:27 +0000 | [diff] [blame] | 5374 | ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev); |
| 5375 | ret = notifier_to_errno(ret); |
| 5376 | if (ret) |
| 5377 | goto err_uninit; |
| 5378 | |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 5379 | ret = netdev_register_kobject(dev); |
Stephen Hemminger | b17a7c1 | 2006-05-10 13:21:17 -0700 | [diff] [blame] | 5380 | if (ret) |
Herbert Xu | 7ce1b0e | 2007-07-30 16:29:40 -0700 | [diff] [blame] | 5381 | goto err_uninit; |
Stephen Hemminger | b17a7c1 | 2006-05-10 13:21:17 -0700 | [diff] [blame] | 5382 | dev->reg_state = NETREG_REGISTERED; |
| 5383 | |
Michał Mirosław | 6cb6a27 | 2011-04-02 22:48:47 -0700 | [diff] [blame] | 5384 | __netdev_update_features(dev); |
Michał Mirosław | 8e9b59b | 2011-02-22 16:52:28 +0000 | [diff] [blame] | 5385 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5386 | /* |
| 5387 | * Default initial state at registry is that the |
| 5388 | * device is present. |
| 5389 | */ |
| 5390 | |
| 5391 | set_bit(__LINK_STATE_PRESENT, &dev->state); |
| 5392 | |
Ben Hutchings | 8f4cccb | 2012-08-20 22:16:51 +0100 | [diff] [blame] | 5393 | linkwatch_init_dev(dev); |
| 5394 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5395 | dev_init_scheduler(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5396 | dev_hold(dev); |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 5397 | list_netdevice(dev); |
Theodore Ts'o | 7bf2357 | 2012-07-04 21:23:25 -0400 | [diff] [blame] | 5398 | add_device_randomness(dev->dev_addr, dev->addr_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | |
Jiri Pirko | 948b337 | 2013-01-08 01:38:25 +0000 | [diff] [blame] | 5400 | /* If the device has permanent device address, driver should |
| 5401 | * set dev_addr and also addr_assign_type should be set to |
| 5402 | * NET_ADDR_PERM (default value). |
| 5403 | */ |
| 5404 | if (dev->addr_assign_type == NET_ADDR_PERM) |
| 5405 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); |
| 5406 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5407 | /* Notify protocols, that a new device appeared. */ |
Pavel Emelyanov | 056925a | 2007-09-16 15:42:43 -0700 | [diff] [blame] | 5408 | ret = call_netdevice_notifiers(NETDEV_REGISTER, dev); |
Herbert Xu | fcc5a03 | 2007-07-30 17:03:38 -0700 | [diff] [blame] | 5409 | ret = notifier_to_errno(ret); |
Daniel Lezcano | 93ee31f | 2007-10-30 15:38:18 -0700 | [diff] [blame] | 5410 | if (ret) { |
| 5411 | rollback_registered(dev); |
| 5412 | dev->reg_state = NETREG_UNREGISTERED; |
| 5413 | } |
Eric W. Biederman | d90a909 | 2009-12-12 22:11:15 +0000 | [diff] [blame] | 5414 | /* |
| 5415 | * Prevent userspace races by waiting until the network |
| 5416 | * device is fully setup before sending notifications. |
| 5417 | */ |
Patrick McHardy | a283576 | 2010-02-26 06:34:51 +0000 | [diff] [blame] | 5418 | if (!dev->rtnl_link_ops || |
| 5419 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) |
| 5420 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | |
| 5422 | out: |
| 5423 | return ret; |
Herbert Xu | 7ce1b0e | 2007-07-30 16:29:40 -0700 | [diff] [blame] | 5424 | |
| 5425 | err_uninit: |
Stephen Hemminger | d314774 | 2008-11-19 21:32:24 -0800 | [diff] [blame] | 5426 | if (dev->netdev_ops->ndo_uninit) |
| 5427 | dev->netdev_ops->ndo_uninit(dev); |
Herbert Xu | 7ce1b0e | 2007-07-30 16:29:40 -0700 | [diff] [blame] | 5428 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5429 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 5430 | EXPORT_SYMBOL(register_netdevice); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | |
| 5432 | /** |
Benjamin Herrenschmidt | 937f1ba | 2009-01-14 21:05:05 -0800 | [diff] [blame] | 5433 | * init_dummy_netdev - init a dummy network device for NAPI |
| 5434 | * @dev: device to init |
| 5435 | * |
| 5436 | * This takes a network device structure and initialize the minimum |
| 5437 | * amount of fields so it can be used to schedule NAPI polls without |
| 5438 | * registering a full blown interface. This is to be used by drivers |
| 5439 | * that need to tie several hardware interfaces to a single NAPI |
| 5440 | * poll scheduler due to HW limitations. |
| 5441 | */ |
| 5442 | int init_dummy_netdev(struct net_device *dev) |
| 5443 | { |
| 5444 | /* Clear everything. Note we don't initialize spinlocks |
| 5445 | * are they aren't supposed to be taken by any of the |
| 5446 | * NAPI code and this dummy netdev is supposed to be |
| 5447 | * only ever used for NAPI polls |
| 5448 | */ |
| 5449 | memset(dev, 0, sizeof(struct net_device)); |
| 5450 | |
| 5451 | /* make sure we BUG if trying to hit standard |
| 5452 | * register/unregister code path |
| 5453 | */ |
| 5454 | dev->reg_state = NETREG_DUMMY; |
| 5455 | |
Benjamin Herrenschmidt | 937f1ba | 2009-01-14 21:05:05 -0800 | [diff] [blame] | 5456 | /* NAPI wants this */ |
| 5457 | INIT_LIST_HEAD(&dev->napi_list); |
| 5458 | |
| 5459 | /* a dummy interface is started by default */ |
| 5460 | set_bit(__LINK_STATE_PRESENT, &dev->state); |
| 5461 | set_bit(__LINK_STATE_START, &dev->state); |
| 5462 | |
Eric Dumazet | 29b4433 | 2010-10-11 10:22:12 +0000 | [diff] [blame] | 5463 | /* Note : We dont allocate pcpu_refcnt for dummy devices, |
| 5464 | * because users of this 'device' dont need to change |
| 5465 | * its refcount. |
| 5466 | */ |
| 5467 | |
Benjamin Herrenschmidt | 937f1ba | 2009-01-14 21:05:05 -0800 | [diff] [blame] | 5468 | return 0; |
| 5469 | } |
| 5470 | EXPORT_SYMBOL_GPL(init_dummy_netdev); |
| 5471 | |
| 5472 | |
| 5473 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | * register_netdev - register a network device |
| 5475 | * @dev: device to register |
| 5476 | * |
| 5477 | * Take a completed network device structure and add it to the kernel |
| 5478 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier |
| 5479 | * chain. 0 is returned on success. A negative errno code is returned |
| 5480 | * on a failure to set up the device, or if the name is a duplicate. |
| 5481 | * |
Borislav Petkov | 38b4da3 | 2007-04-20 22:14:10 -0700 | [diff] [blame] | 5482 | * This is a wrapper around register_netdevice that takes the rtnl semaphore |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5483 | * and expands the device name if you passed a format string to |
| 5484 | * alloc_netdev. |
| 5485 | */ |
| 5486 | int register_netdev(struct net_device *dev) |
| 5487 | { |
| 5488 | int err; |
| 5489 | |
| 5490 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5491 | err = register_netdevice(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5492 | rtnl_unlock(); |
| 5493 | return err; |
| 5494 | } |
| 5495 | EXPORT_SYMBOL(register_netdev); |
| 5496 | |
Eric Dumazet | 29b4433 | 2010-10-11 10:22:12 +0000 | [diff] [blame] | 5497 | int netdev_refcnt_read(const struct net_device *dev) |
| 5498 | { |
| 5499 | int i, refcnt = 0; |
| 5500 | |
| 5501 | for_each_possible_cpu(i) |
| 5502 | refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i); |
| 5503 | return refcnt; |
| 5504 | } |
| 5505 | EXPORT_SYMBOL(netdev_refcnt_read); |
| 5506 | |
Ben Hutchings | 2c53040 | 2012-07-10 10:55:09 +0000 | [diff] [blame] | 5507 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5508 | * netdev_wait_allrefs - wait until all references are gone. |
Randy Dunlap | 3de7a37 | 2012-08-18 14:36:44 +0000 | [diff] [blame] | 5509 | * @dev: target net_device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | * |
| 5511 | * This is called when unregistering network devices. |
| 5512 | * |
| 5513 | * Any protocol or device that holds a reference should register |
| 5514 | * for netdevice notification, and cleanup and put back the |
| 5515 | * reference if they receive an UNREGISTER event. |
| 5516 | * We can get stuck here if buggy protocols don't correctly |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 5517 | * call dev_put. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5518 | */ |
| 5519 | static void netdev_wait_allrefs(struct net_device *dev) |
| 5520 | { |
| 5521 | unsigned long rebroadcast_time, warning_time; |
Eric Dumazet | 29b4433 | 2010-10-11 10:22:12 +0000 | [diff] [blame] | 5522 | int refcnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5523 | |
Eric Dumazet | e014deb | 2009-11-17 05:59:21 +0000 | [diff] [blame] | 5524 | linkwatch_forget_dev(dev); |
| 5525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | rebroadcast_time = warning_time = jiffies; |
Eric Dumazet | 29b4433 | 2010-10-11 10:22:12 +0000 | [diff] [blame] | 5527 | refcnt = netdev_refcnt_read(dev); |
| 5528 | |
| 5529 | while (refcnt != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5530 | if (time_after(jiffies, rebroadcast_time + 1 * HZ)) { |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 5531 | rtnl_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5532 | |
| 5533 | /* Rebroadcast unregister notification */ |
Pavel Emelyanov | 056925a | 2007-09-16 15:42:43 -0700 | [diff] [blame] | 5534 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5535 | |
Eric Dumazet | 748e2d9 | 2012-08-22 21:50:59 +0000 | [diff] [blame] | 5536 | __rtnl_unlock(); |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 5537 | rcu_barrier(); |
Eric Dumazet | 748e2d9 | 2012-08-22 21:50:59 +0000 | [diff] [blame] | 5538 | rtnl_lock(); |
| 5539 | |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 5540 | call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5541 | if (test_bit(__LINK_STATE_LINKWATCH_PENDING, |
| 5542 | &dev->state)) { |
| 5543 | /* We must not have linkwatch events |
| 5544 | * pending on unregister. If this |
| 5545 | * happens, we simply run the queue |
| 5546 | * unscheduled, resulting in a noop |
| 5547 | * for this device. |
| 5548 | */ |
| 5549 | linkwatch_run_queue(); |
| 5550 | } |
| 5551 | |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 5552 | __rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5553 | |
| 5554 | rebroadcast_time = jiffies; |
| 5555 | } |
| 5556 | |
| 5557 | msleep(250); |
| 5558 | |
Eric Dumazet | 29b4433 | 2010-10-11 10:22:12 +0000 | [diff] [blame] | 5559 | refcnt = netdev_refcnt_read(dev); |
| 5560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5561 | if (time_after(jiffies, warning_time + 10 * HZ)) { |
Joe Perches | 7b6cd1c | 2012-02-01 10:54:43 +0000 | [diff] [blame] | 5562 | pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n", |
| 5563 | dev->name, refcnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5564 | warning_time = jiffies; |
| 5565 | } |
| 5566 | } |
| 5567 | } |
| 5568 | |
| 5569 | /* The sequence is: |
| 5570 | * |
| 5571 | * rtnl_lock(); |
| 5572 | * ... |
| 5573 | * register_netdevice(x1); |
| 5574 | * register_netdevice(x2); |
| 5575 | * ... |
| 5576 | * unregister_netdevice(y1); |
| 5577 | * unregister_netdevice(y2); |
| 5578 | * ... |
| 5579 | * rtnl_unlock(); |
| 5580 | * free_netdev(y1); |
| 5581 | * free_netdev(y2); |
| 5582 | * |
Herbert Xu | 58ec3b4 | 2008-10-07 15:50:03 -0700 | [diff] [blame] | 5583 | * We are invoked by rtnl_unlock(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5584 | * This allows us to deal with problems: |
Stephen Hemminger | b17a7c1 | 2006-05-10 13:21:17 -0700 | [diff] [blame] | 5585 | * 1) We can delete sysfs objects which invoke hotplug |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5586 | * without deadlocking with linkwatch via keventd. |
| 5587 | * 2) Since we run with the RTNL semaphore not held, we can sleep |
| 5588 | * safely in order to wait for the netdev refcnt to drop to zero. |
Herbert Xu | 58ec3b4 | 2008-10-07 15:50:03 -0700 | [diff] [blame] | 5589 | * |
| 5590 | * We must not return until all unregister events added during |
| 5591 | * the interval the lock was held have been completed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5592 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5593 | void netdev_run_todo(void) |
| 5594 | { |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 5595 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5596 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5597 | /* Snapshot list, allow later requests */ |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 5598 | list_replace_init(&net_todo_list, &list); |
Herbert Xu | 58ec3b4 | 2008-10-07 15:50:03 -0700 | [diff] [blame] | 5599 | |
| 5600 | __rtnl_unlock(); |
Oleg Nesterov | 626ab0e | 2006-06-23 02:05:55 -0700 | [diff] [blame] | 5601 | |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 5602 | |
| 5603 | /* Wait for rcu callbacks to finish before next phase */ |
Eric W. Biederman | 850a545 | 2011-10-13 22:25:23 +0000 | [diff] [blame] | 5604 | if (!list_empty(&list)) |
| 5605 | rcu_barrier(); |
| 5606 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5607 | while (!list_empty(&list)) { |
| 5608 | struct net_device *dev |
stephen hemminger | e5e26d7 | 2010-02-24 14:01:38 +0000 | [diff] [blame] | 5609 | = list_first_entry(&list, struct net_device, todo_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | list_del(&dev->todo_list); |
| 5611 | |
Eric Dumazet | 748e2d9 | 2012-08-22 21:50:59 +0000 | [diff] [blame] | 5612 | rtnl_lock(); |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 5613 | call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev); |
Eric Dumazet | 748e2d9 | 2012-08-22 21:50:59 +0000 | [diff] [blame] | 5614 | __rtnl_unlock(); |
Eric Dumazet | 0115e8e | 2012-08-22 17:19:46 +0000 | [diff] [blame] | 5615 | |
Stephen Hemminger | b17a7c1 | 2006-05-10 13:21:17 -0700 | [diff] [blame] | 5616 | if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) { |
Joe Perches | 7b6cd1c | 2012-02-01 10:54:43 +0000 | [diff] [blame] | 5617 | pr_err("network todo '%s' but state %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5618 | dev->name, dev->reg_state); |
Stephen Hemminger | b17a7c1 | 2006-05-10 13:21:17 -0700 | [diff] [blame] | 5619 | dump_stack(); |
| 5620 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5621 | } |
Stephen Hemminger | b17a7c1 | 2006-05-10 13:21:17 -0700 | [diff] [blame] | 5622 | |
Stephen Hemminger | b17a7c1 | 2006-05-10 13:21:17 -0700 | [diff] [blame] | 5623 | dev->reg_state = NETREG_UNREGISTERED; |
| 5624 | |
Changli Gao | 152102c | 2010-03-30 20:16:22 +0000 | [diff] [blame] | 5625 | on_each_cpu(flush_backlog, dev, 1); |
Stephen Hemminger | 6e583ce | 2008-08-03 21:29:57 -0700 | [diff] [blame] | 5626 | |
Stephen Hemminger | b17a7c1 | 2006-05-10 13:21:17 -0700 | [diff] [blame] | 5627 | netdev_wait_allrefs(dev); |
| 5628 | |
| 5629 | /* paranoia */ |
Eric Dumazet | 29b4433 | 2010-10-11 10:22:12 +0000 | [diff] [blame] | 5630 | BUG_ON(netdev_refcnt_read(dev)); |
Eric Dumazet | 33d480c | 2011-08-11 19:30:52 +0000 | [diff] [blame] | 5631 | WARN_ON(rcu_access_pointer(dev->ip_ptr)); |
| 5632 | WARN_ON(rcu_access_pointer(dev->ip6_ptr)); |
Ilpo Järvinen | 547b792 | 2008-07-25 21:43:18 -0700 | [diff] [blame] | 5633 | WARN_ON(dev->dn_ptr); |
Stephen Hemminger | b17a7c1 | 2006-05-10 13:21:17 -0700 | [diff] [blame] | 5634 | |
Stephen Hemminger | b17a7c1 | 2006-05-10 13:21:17 -0700 | [diff] [blame] | 5635 | if (dev->destructor) |
| 5636 | dev->destructor(dev); |
Stephen Hemminger | 9093bbb | 2007-05-19 15:39:25 -0700 | [diff] [blame] | 5637 | |
| 5638 | /* Free network device */ |
| 5639 | kobject_put(&dev->dev.kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5640 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5641 | } |
| 5642 | |
Ben Hutchings | 3cfde79 | 2010-07-09 09:11:52 +0000 | [diff] [blame] | 5643 | /* Convert net_device_stats to rtnl_link_stats64. They have the same |
| 5644 | * fields in the same order, with only the type differing. |
| 5645 | */ |
Eric Dumazet | 77a1abf | 2012-03-05 04:50:09 +0000 | [diff] [blame] | 5646 | void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64, |
| 5647 | const struct net_device_stats *netdev_stats) |
Ben Hutchings | 3cfde79 | 2010-07-09 09:11:52 +0000 | [diff] [blame] | 5648 | { |
| 5649 | #if BITS_PER_LONG == 64 |
Eric Dumazet | 77a1abf | 2012-03-05 04:50:09 +0000 | [diff] [blame] | 5650 | BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats)); |
| 5651 | memcpy(stats64, netdev_stats, sizeof(*stats64)); |
Ben Hutchings | 3cfde79 | 2010-07-09 09:11:52 +0000 | [diff] [blame] | 5652 | #else |
| 5653 | size_t i, n = sizeof(*stats64) / sizeof(u64); |
| 5654 | const unsigned long *src = (const unsigned long *)netdev_stats; |
| 5655 | u64 *dst = (u64 *)stats64; |
| 5656 | |
| 5657 | BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) != |
| 5658 | sizeof(*stats64) / sizeof(u64)); |
| 5659 | for (i = 0; i < n; i++) |
| 5660 | dst[i] = src[i]; |
| 5661 | #endif |
| 5662 | } |
Eric Dumazet | 77a1abf | 2012-03-05 04:50:09 +0000 | [diff] [blame] | 5663 | EXPORT_SYMBOL(netdev_stats_to_stats64); |
Ben Hutchings | 3cfde79 | 2010-07-09 09:11:52 +0000 | [diff] [blame] | 5664 | |
Eric Dumazet | d83345a | 2009-11-16 03:36:51 +0000 | [diff] [blame] | 5665 | /** |
Stephen Hemminger | eeda3fd | 2008-11-19 21:40:23 -0800 | [diff] [blame] | 5666 | * dev_get_stats - get network device statistics |
| 5667 | * @dev: device to get statistics from |
Eric Dumazet | 2817273 | 2010-07-07 14:58:56 -0700 | [diff] [blame] | 5668 | * @storage: place to store stats |
Stephen Hemminger | eeda3fd | 2008-11-19 21:40:23 -0800 | [diff] [blame] | 5669 | * |
Ben Hutchings | d775351 | 2010-07-09 09:12:41 +0000 | [diff] [blame] | 5670 | * Get network statistics from device. Return @storage. |
| 5671 | * The device driver may provide its own method by setting |
| 5672 | * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats; |
| 5673 | * otherwise the internal statistics structure is used. |
Stephen Hemminger | eeda3fd | 2008-11-19 21:40:23 -0800 | [diff] [blame] | 5674 | */ |
Ben Hutchings | d775351 | 2010-07-09 09:12:41 +0000 | [diff] [blame] | 5675 | struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev, |
| 5676 | struct rtnl_link_stats64 *storage) |
Eric Dumazet | 7004bf2 | 2009-05-18 00:34:33 +0000 | [diff] [blame] | 5677 | { |
Stephen Hemminger | eeda3fd | 2008-11-19 21:40:23 -0800 | [diff] [blame] | 5678 | const struct net_device_ops *ops = dev->netdev_ops; |
| 5679 | |
Eric Dumazet | 2817273 | 2010-07-07 14:58:56 -0700 | [diff] [blame] | 5680 | if (ops->ndo_get_stats64) { |
| 5681 | memset(storage, 0, sizeof(*storage)); |
Eric Dumazet | caf586e | 2010-09-30 21:06:55 +0000 | [diff] [blame] | 5682 | ops->ndo_get_stats64(dev, storage); |
| 5683 | } else if (ops->ndo_get_stats) { |
Ben Hutchings | 3cfde79 | 2010-07-09 09:11:52 +0000 | [diff] [blame] | 5684 | netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev)); |
Eric Dumazet | caf586e | 2010-09-30 21:06:55 +0000 | [diff] [blame] | 5685 | } else { |
| 5686 | netdev_stats_to_stats64(storage, &dev->stats); |
Eric Dumazet | 2817273 | 2010-07-07 14:58:56 -0700 | [diff] [blame] | 5687 | } |
Eric Dumazet | caf586e | 2010-09-30 21:06:55 +0000 | [diff] [blame] | 5688 | storage->rx_dropped += atomic_long_read(&dev->rx_dropped); |
Eric Dumazet | 2817273 | 2010-07-07 14:58:56 -0700 | [diff] [blame] | 5689 | return storage; |
Rusty Russell | c45d286 | 2007-03-28 14:29:08 -0700 | [diff] [blame] | 5690 | } |
Stephen Hemminger | eeda3fd | 2008-11-19 21:40:23 -0800 | [diff] [blame] | 5691 | EXPORT_SYMBOL(dev_get_stats); |
Rusty Russell | c45d286 | 2007-03-28 14:29:08 -0700 | [diff] [blame] | 5692 | |
Eric Dumazet | 24824a0 | 2010-10-02 06:11:55 +0000 | [diff] [blame] | 5693 | struct netdev_queue *dev_ingress_queue_create(struct net_device *dev) |
David S. Miller | dc2b484 | 2008-07-08 17:18:23 -0700 | [diff] [blame] | 5694 | { |
Eric Dumazet | 24824a0 | 2010-10-02 06:11:55 +0000 | [diff] [blame] | 5695 | struct netdev_queue *queue = dev_ingress_queue(dev); |
David S. Miller | dc2b484 | 2008-07-08 17:18:23 -0700 | [diff] [blame] | 5696 | |
Eric Dumazet | 24824a0 | 2010-10-02 06:11:55 +0000 | [diff] [blame] | 5697 | #ifdef CONFIG_NET_CLS_ACT |
| 5698 | if (queue) |
| 5699 | return queue; |
| 5700 | queue = kzalloc(sizeof(*queue), GFP_KERNEL); |
| 5701 | if (!queue) |
| 5702 | return NULL; |
| 5703 | netdev_init_one_queue(dev, queue, NULL); |
Eric Dumazet | 24824a0 | 2010-10-02 06:11:55 +0000 | [diff] [blame] | 5704 | queue->qdisc = &noop_qdisc; |
| 5705 | queue->qdisc_sleeping = &noop_qdisc; |
| 5706 | rcu_assign_pointer(dev->ingress_queue, queue); |
| 5707 | #endif |
| 5708 | return queue; |
David S. Miller | bb949fb | 2008-07-08 16:55:56 -0700 | [diff] [blame] | 5709 | } |
| 5710 | |
Eric Dumazet | 2c60db0 | 2012-09-16 09:17:26 +0000 | [diff] [blame] | 5711 | static const struct ethtool_ops default_ethtool_ops; |
| 5712 | |
Stanislaw Gruszka | d07d750 | 2013-01-10 23:19:10 +0000 | [diff] [blame] | 5713 | void netdev_set_default_ethtool_ops(struct net_device *dev, |
| 5714 | const struct ethtool_ops *ops) |
| 5715 | { |
| 5716 | if (dev->ethtool_ops == &default_ethtool_ops) |
| 5717 | dev->ethtool_ops = ops; |
| 5718 | } |
| 5719 | EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops); |
| 5720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5721 | /** |
Tom Herbert | 36909ea | 2011-01-09 19:36:31 +0000 | [diff] [blame] | 5722 | * alloc_netdev_mqs - allocate network device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5723 | * @sizeof_priv: size of private data to allocate space for |
| 5724 | * @name: device name format string |
| 5725 | * @setup: callback to initialize device |
Tom Herbert | 36909ea | 2011-01-09 19:36:31 +0000 | [diff] [blame] | 5726 | * @txqs: the number of TX subqueues to allocate |
| 5727 | * @rxqs: the number of RX subqueues to allocate |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | * |
| 5729 | * Allocates a struct net_device with private data area for driver use |
Peter P Waskiewicz Jr | f25f4e4 | 2007-07-06 13:36:20 -0700 | [diff] [blame] | 5730 | * and performs basic initialization. Also allocates subquue structs |
Tom Herbert | 36909ea | 2011-01-09 19:36:31 +0000 | [diff] [blame] | 5731 | * for each queue on the device. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5732 | */ |
Tom Herbert | 36909ea | 2011-01-09 19:36:31 +0000 | [diff] [blame] | 5733 | struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name, |
| 5734 | void (*setup)(struct net_device *), |
| 5735 | unsigned int txqs, unsigned int rxqs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5737 | struct net_device *dev; |
Stephen Hemminger | 7943986 | 2008-07-21 13:28:44 -0700 | [diff] [blame] | 5738 | size_t alloc_size; |
Eric Dumazet | 1ce8e7b | 2009-05-27 04:42:37 +0000 | [diff] [blame] | 5739 | struct net_device *p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | |
Stephen Hemminger | b6fe17d | 2006-08-29 17:06:13 -0700 | [diff] [blame] | 5741 | BUG_ON(strlen(name) >= sizeof(dev->name)); |
| 5742 | |
Tom Herbert | 36909ea | 2011-01-09 19:36:31 +0000 | [diff] [blame] | 5743 | if (txqs < 1) { |
Joe Perches | 7b6cd1c | 2012-02-01 10:54:43 +0000 | [diff] [blame] | 5744 | pr_err("alloc_netdev: Unable to allocate device with zero queues\n"); |
Tom Herbert | 55513fb | 2010-10-18 17:55:58 +0000 | [diff] [blame] | 5745 | return NULL; |
| 5746 | } |
| 5747 | |
Tom Herbert | 36909ea | 2011-01-09 19:36:31 +0000 | [diff] [blame] | 5748 | #ifdef CONFIG_RPS |
| 5749 | if (rxqs < 1) { |
Joe Perches | 7b6cd1c | 2012-02-01 10:54:43 +0000 | [diff] [blame] | 5750 | pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n"); |
Tom Herbert | 36909ea | 2011-01-09 19:36:31 +0000 | [diff] [blame] | 5751 | return NULL; |
| 5752 | } |
| 5753 | #endif |
| 5754 | |
David S. Miller | fd2ea0a | 2008-07-17 01:56:23 -0700 | [diff] [blame] | 5755 | alloc_size = sizeof(struct net_device); |
Alexey Dobriyan | d1643d2 | 2008-04-18 15:43:32 -0700 | [diff] [blame] | 5756 | if (sizeof_priv) { |
| 5757 | /* ensure 32-byte alignment of private area */ |
Eric Dumazet | 1ce8e7b | 2009-05-27 04:42:37 +0000 | [diff] [blame] | 5758 | alloc_size = ALIGN(alloc_size, NETDEV_ALIGN); |
Alexey Dobriyan | d1643d2 | 2008-04-18 15:43:32 -0700 | [diff] [blame] | 5759 | alloc_size += sizeof_priv; |
| 5760 | } |
| 5761 | /* ensure 32-byte alignment of whole construct */ |
Eric Dumazet | 1ce8e7b | 2009-05-27 04:42:37 +0000 | [diff] [blame] | 5762 | alloc_size += NETDEV_ALIGN - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5763 | |
Paolo 'Blaisorblade' Giarrusso | 31380de | 2006-04-06 22:38:28 -0700 | [diff] [blame] | 5764 | p = kzalloc(alloc_size, GFP_KERNEL); |
Joe Perches | 62b5942 | 2013-02-04 16:48:16 +0000 | [diff] [blame] | 5765 | if (!p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5767 | |
Eric Dumazet | 1ce8e7b | 2009-05-27 04:42:37 +0000 | [diff] [blame] | 5768 | dev = PTR_ALIGN(p, NETDEV_ALIGN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5769 | dev->padded = (char *)dev - (char *)p; |
Jiri Pirko | ab9c73c | 2009-05-08 13:30:17 +0000 | [diff] [blame] | 5770 | |
Eric Dumazet | 29b4433 | 2010-10-11 10:22:12 +0000 | [diff] [blame] | 5771 | dev->pcpu_refcnt = alloc_percpu(int); |
| 5772 | if (!dev->pcpu_refcnt) |
Tom Herbert | e648493 | 2010-10-18 18:04:39 +0000 | [diff] [blame] | 5773 | goto free_p; |
Jiri Pirko | ab9c73c | 2009-05-08 13:30:17 +0000 | [diff] [blame] | 5774 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5775 | if (dev_addr_init(dev)) |
Eric Dumazet | 29b4433 | 2010-10-11 10:22:12 +0000 | [diff] [blame] | 5776 | goto free_pcpu; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5777 | |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 5778 | dev_mc_init(dev); |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 5779 | dev_uc_init(dev); |
Jiri Pirko | ccffad25 | 2009-05-22 23:22:17 +0000 | [diff] [blame] | 5780 | |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 5781 | dev_net_set(dev, &init_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5782 | |
Peter P Waskiewicz Jr | 82cc1a7 | 2008-03-21 03:43:19 -0700 | [diff] [blame] | 5783 | dev->gso_max_size = GSO_MAX_SIZE; |
Ben Hutchings | 30b678d | 2012-07-30 15:57:00 +0000 | [diff] [blame] | 5784 | dev->gso_max_segs = GSO_MAX_SEGS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5785 | |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 5786 | INIT_LIST_HEAD(&dev->napi_list); |
Eric W. Biederman | 9fdce09 | 2009-10-30 14:51:13 +0000 | [diff] [blame] | 5787 | INIT_LIST_HEAD(&dev->unreg_list); |
Eric Dumazet | e014deb | 2009-11-17 05:59:21 +0000 | [diff] [blame] | 5788 | INIT_LIST_HEAD(&dev->link_watch_list); |
Jiri Pirko | 9ff162a | 2013-01-03 22:48:49 +0000 | [diff] [blame] | 5789 | INIT_LIST_HEAD(&dev->upper_dev_list); |
Eric Dumazet | 93f154b | 2009-05-18 22:19:19 -0700 | [diff] [blame] | 5790 | dev->priv_flags = IFF_XMIT_DST_RELEASE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5791 | setup(dev); |
David S. Miller | 8d3bdbd | 2011-02-08 15:02:50 -0800 | [diff] [blame] | 5792 | |
| 5793 | dev->num_tx_queues = txqs; |
| 5794 | dev->real_num_tx_queues = txqs; |
| 5795 | if (netif_alloc_netdev_queues(dev)) |
| 5796 | goto free_all; |
| 5797 | |
| 5798 | #ifdef CONFIG_RPS |
| 5799 | dev->num_rx_queues = rxqs; |
| 5800 | dev->real_num_rx_queues = rxqs; |
| 5801 | if (netif_alloc_rx_queues(dev)) |
| 5802 | goto free_all; |
| 5803 | #endif |
| 5804 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | strcpy(dev->name, name); |
Vlad Dogaru | cbda10f | 2011-01-13 23:38:30 +0000 | [diff] [blame] | 5806 | dev->group = INIT_NETDEV_GROUP; |
Eric Dumazet | 2c60db0 | 2012-09-16 09:17:26 +0000 | [diff] [blame] | 5807 | if (!dev->ethtool_ops) |
| 5808 | dev->ethtool_ops = &default_ethtool_ops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5809 | return dev; |
Jiri Pirko | ab9c73c | 2009-05-08 13:30:17 +0000 | [diff] [blame] | 5810 | |
David S. Miller | 8d3bdbd | 2011-02-08 15:02:50 -0800 | [diff] [blame] | 5811 | free_all: |
| 5812 | free_netdev(dev); |
| 5813 | return NULL; |
| 5814 | |
Eric Dumazet | 29b4433 | 2010-10-11 10:22:12 +0000 | [diff] [blame] | 5815 | free_pcpu: |
| 5816 | free_percpu(dev->pcpu_refcnt); |
Eric Dumazet | 60877a3 | 2013-06-20 01:15:51 -0700 | [diff] [blame] | 5817 | netif_free_tx_queues(dev); |
Tom Herbert | fe82224 | 2010-11-09 10:47:38 +0000 | [diff] [blame] | 5818 | #ifdef CONFIG_RPS |
| 5819 | kfree(dev->_rx); |
| 5820 | #endif |
| 5821 | |
Jiri Pirko | ab9c73c | 2009-05-08 13:30:17 +0000 | [diff] [blame] | 5822 | free_p: |
| 5823 | kfree(p); |
| 5824 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5825 | } |
Tom Herbert | 36909ea | 2011-01-09 19:36:31 +0000 | [diff] [blame] | 5826 | EXPORT_SYMBOL(alloc_netdev_mqs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5827 | |
| 5828 | /** |
| 5829 | * free_netdev - free network device |
| 5830 | * @dev: device |
| 5831 | * |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 5832 | * This function does the last stage of destroying an allocated device |
| 5833 | * interface. The reference to the device object is released. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5834 | * If this is the last reference then it will be freed. |
| 5835 | */ |
| 5836 | void free_netdev(struct net_device *dev) |
| 5837 | { |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 5838 | struct napi_struct *p, *n; |
| 5839 | |
Denis V. Lunev | f3005d7 | 2008-04-16 02:02:18 -0700 | [diff] [blame] | 5840 | release_net(dev_net(dev)); |
| 5841 | |
Eric Dumazet | 60877a3 | 2013-06-20 01:15:51 -0700 | [diff] [blame] | 5842 | netif_free_tx_queues(dev); |
Tom Herbert | fe82224 | 2010-11-09 10:47:38 +0000 | [diff] [blame] | 5843 | #ifdef CONFIG_RPS |
| 5844 | kfree(dev->_rx); |
| 5845 | #endif |
David S. Miller | e8a0464 | 2008-07-17 00:34:19 -0700 | [diff] [blame] | 5846 | |
Eric Dumazet | 33d480c | 2011-08-11 19:30:52 +0000 | [diff] [blame] | 5847 | kfree(rcu_dereference_protected(dev->ingress_queue, 1)); |
Eric Dumazet | 24824a0 | 2010-10-02 06:11:55 +0000 | [diff] [blame] | 5848 | |
Jiri Pirko | f001fde | 2009-05-05 02:48:28 +0000 | [diff] [blame] | 5849 | /* Flush device addresses */ |
| 5850 | dev_addr_flush(dev); |
| 5851 | |
Herbert Xu | d565b0a | 2008-12-15 23:38:52 -0800 | [diff] [blame] | 5852 | list_for_each_entry_safe(p, n, &dev->napi_list, dev_list) |
| 5853 | netif_napi_del(p); |
| 5854 | |
Eric Dumazet | 29b4433 | 2010-10-11 10:22:12 +0000 | [diff] [blame] | 5855 | free_percpu(dev->pcpu_refcnt); |
| 5856 | dev->pcpu_refcnt = NULL; |
| 5857 | |
Stephen Hemminger | 3041a06 | 2006-05-26 13:25:24 -0700 | [diff] [blame] | 5858 | /* Compatibility with error handling in drivers */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5859 | if (dev->reg_state == NETREG_UNINITIALIZED) { |
| 5860 | kfree((char *)dev - dev->padded); |
| 5861 | return; |
| 5862 | } |
| 5863 | |
| 5864 | BUG_ON(dev->reg_state != NETREG_UNREGISTERED); |
| 5865 | dev->reg_state = NETREG_RELEASED; |
| 5866 | |
Greg Kroah-Hartman | 43cb76d | 2002-04-09 12:14:34 -0700 | [diff] [blame] | 5867 | /* will free via device release */ |
| 5868 | put_device(&dev->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5869 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 5870 | EXPORT_SYMBOL(free_netdev); |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 5871 | |
Stephen Hemminger | f0db275 | 2008-09-30 02:23:58 -0700 | [diff] [blame] | 5872 | /** |
| 5873 | * synchronize_net - Synchronize with packet receive processing |
| 5874 | * |
| 5875 | * Wait for packets currently being received to be done. |
| 5876 | * Does not block later packets from starting. |
| 5877 | */ |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 5878 | void synchronize_net(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5879 | { |
| 5880 | might_sleep(); |
Eric Dumazet | be3fc41 | 2011-05-23 23:07:32 +0000 | [diff] [blame] | 5881 | if (rtnl_is_locked()) |
| 5882 | synchronize_rcu_expedited(); |
| 5883 | else |
| 5884 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | } |
Eric Dumazet | d1b19df | 2009-09-03 01:29:39 -0700 | [diff] [blame] | 5886 | EXPORT_SYMBOL(synchronize_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5887 | |
| 5888 | /** |
Eric Dumazet | 44a0873 | 2009-10-27 07:03:04 +0000 | [diff] [blame] | 5889 | * unregister_netdevice_queue - remove device from the kernel |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5890 | * @dev: device |
Eric Dumazet | 44a0873 | 2009-10-27 07:03:04 +0000 | [diff] [blame] | 5891 | * @head: list |
Jaswinder Singh Rajput | 6ebfbc0 | 2009-11-22 20:43:13 -0800 | [diff] [blame] | 5892 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5893 | * This function shuts down a device interface and removes it |
Wang Chen | d59b54b | 2007-12-11 02:28:03 -0800 | [diff] [blame] | 5894 | * from the kernel tables. |
Eric Dumazet | 44a0873 | 2009-10-27 07:03:04 +0000 | [diff] [blame] | 5895 | * If head not NULL, device is queued to be unregistered later. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5896 | * |
| 5897 | * Callers must hold the rtnl semaphore. You may want |
| 5898 | * unregister_netdev() instead of this. |
| 5899 | */ |
| 5900 | |
Eric Dumazet | 44a0873 | 2009-10-27 07:03:04 +0000 | [diff] [blame] | 5901 | void unregister_netdevice_queue(struct net_device *dev, struct list_head *head) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5902 | { |
Herbert Xu | a662071 | 2007-12-12 19:21:56 -0800 | [diff] [blame] | 5903 | ASSERT_RTNL(); |
| 5904 | |
Eric Dumazet | 44a0873 | 2009-10-27 07:03:04 +0000 | [diff] [blame] | 5905 | if (head) { |
Eric W. Biederman | 9fdce09 | 2009-10-30 14:51:13 +0000 | [diff] [blame] | 5906 | list_move_tail(&dev->unreg_list, head); |
Eric Dumazet | 44a0873 | 2009-10-27 07:03:04 +0000 | [diff] [blame] | 5907 | } else { |
| 5908 | rollback_registered(dev); |
| 5909 | /* Finish processing unregister after unlock */ |
| 5910 | net_set_todo(dev); |
| 5911 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | } |
Eric Dumazet | 44a0873 | 2009-10-27 07:03:04 +0000 | [diff] [blame] | 5913 | EXPORT_SYMBOL(unregister_netdevice_queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | |
| 5915 | /** |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 5916 | * unregister_netdevice_many - unregister many devices |
| 5917 | * @head: list of devices |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 5918 | */ |
| 5919 | void unregister_netdevice_many(struct list_head *head) |
| 5920 | { |
| 5921 | struct net_device *dev; |
| 5922 | |
| 5923 | if (!list_empty(head)) { |
| 5924 | rollback_registered_many(head); |
| 5925 | list_for_each_entry(dev, head, unreg_list) |
| 5926 | net_set_todo(dev); |
| 5927 | } |
| 5928 | } |
Eric Dumazet | 63c8099 | 2009-10-27 07:06:49 +0000 | [diff] [blame] | 5929 | EXPORT_SYMBOL(unregister_netdevice_many); |
Eric Dumazet | 9b5e383 | 2009-10-27 07:04:19 +0000 | [diff] [blame] | 5930 | |
| 5931 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5932 | * unregister_netdev - remove device from the kernel |
| 5933 | * @dev: device |
| 5934 | * |
| 5935 | * This function shuts down a device interface and removes it |
Wang Chen | d59b54b | 2007-12-11 02:28:03 -0800 | [diff] [blame] | 5936 | * from the kernel tables. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | * |
| 5938 | * This is just a wrapper for unregister_netdevice that takes |
| 5939 | * the rtnl semaphore. In general you want to use this and not |
| 5940 | * unregister_netdevice. |
| 5941 | */ |
| 5942 | void unregister_netdev(struct net_device *dev) |
| 5943 | { |
| 5944 | rtnl_lock(); |
| 5945 | unregister_netdevice(dev); |
| 5946 | rtnl_unlock(); |
| 5947 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5948 | EXPORT_SYMBOL(unregister_netdev); |
| 5949 | |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 5950 | /** |
| 5951 | * dev_change_net_namespace - move device to different nethost namespace |
| 5952 | * @dev: device |
| 5953 | * @net: network namespace |
| 5954 | * @pat: If not NULL name pattern to try if the current device name |
| 5955 | * is already taken in the destination network namespace. |
| 5956 | * |
| 5957 | * This function shuts down a device interface and moves it |
| 5958 | * to a new network namespace. On success 0 is returned, on |
| 5959 | * a failure a netagive errno code is returned. |
| 5960 | * |
| 5961 | * Callers must hold the rtnl semaphore. |
| 5962 | */ |
| 5963 | |
| 5964 | int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat) |
| 5965 | { |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 5966 | int err; |
| 5967 | |
| 5968 | ASSERT_RTNL(); |
| 5969 | |
| 5970 | /* Don't allow namespace local devices to be moved. */ |
| 5971 | err = -EINVAL; |
| 5972 | if (dev->features & NETIF_F_NETNS_LOCAL) |
| 5973 | goto out; |
| 5974 | |
| 5975 | /* Ensure the device has been registrered */ |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 5976 | if (dev->reg_state != NETREG_REGISTERED) |
| 5977 | goto out; |
| 5978 | |
| 5979 | /* Get out if there is nothing todo */ |
| 5980 | err = 0; |
YOSHIFUJI Hideaki | 878628f | 2008-03-26 03:57:35 +0900 | [diff] [blame] | 5981 | if (net_eq(dev_net(dev), net)) |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 5982 | goto out; |
| 5983 | |
| 5984 | /* Pick the destination device name, and ensure |
| 5985 | * we can use it in the destination network namespace. |
| 5986 | */ |
| 5987 | err = -EEXIST; |
Octavian Purdila | d903102 | 2009-11-18 02:36:59 +0000 | [diff] [blame] | 5988 | if (__dev_get_by_name(net, dev->name)) { |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 5989 | /* We get here if we can't use the current device name */ |
| 5990 | if (!pat) |
| 5991 | goto out; |
Gao feng | 828de4f | 2012-09-13 20:58:27 +0000 | [diff] [blame] | 5992 | if (dev_get_valid_name(net, dev, pat) < 0) |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 5993 | goto out; |
| 5994 | } |
| 5995 | |
| 5996 | /* |
| 5997 | * And now a mini version of register_netdevice unregister_netdevice. |
| 5998 | */ |
| 5999 | |
| 6000 | /* If device is running close it first. */ |
Pavel Emelyanov | 9b77265 | 2007-10-10 02:49:09 -0700 | [diff] [blame] | 6001 | dev_close(dev); |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6002 | |
| 6003 | /* And unlink it from device chain */ |
| 6004 | err = -ENODEV; |
| 6005 | unlist_netdevice(dev); |
| 6006 | |
| 6007 | synchronize_net(); |
| 6008 | |
| 6009 | /* Shutdown queueing discipline. */ |
| 6010 | dev_shutdown(dev); |
| 6011 | |
| 6012 | /* Notify protocols, that we are about to destroy |
| 6013 | this device. They should clean all the things. |
David Lamparter | 3b27e10 | 2010-09-17 03:22:19 +0000 | [diff] [blame] | 6014 | |
| 6015 | Note that dev->reg_state stays at NETREG_REGISTERED. |
| 6016 | This is wanted because this way 8021q and macvlan know |
| 6017 | the device is just moving and can keep their slaves up. |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6018 | */ |
| 6019 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
Gao feng | 6549dd4 | 2012-08-23 15:36:55 +0000 | [diff] [blame] | 6020 | rcu_barrier(); |
| 6021 | call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev); |
Eric W. Biederman | d2237d3 | 2011-10-21 06:24:20 +0000 | [diff] [blame] | 6022 | rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6023 | |
| 6024 | /* |
| 6025 | * Flush the unicast and multicast chains |
| 6026 | */ |
Jiri Pirko | a748ee2 | 2010-04-01 21:22:09 +0000 | [diff] [blame] | 6027 | dev_uc_flush(dev); |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 6028 | dev_mc_flush(dev); |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6029 | |
Serge Hallyn | 4e66ae2 | 2012-12-03 16:17:12 +0000 | [diff] [blame] | 6030 | /* Send a netdev-removed uevent to the old namespace */ |
| 6031 | kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE); |
| 6032 | |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6033 | /* Actually switch the network namespace */ |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 6034 | dev_net_set(dev, net); |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6035 | |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6036 | /* If there is an ifindex conflict assign a new one */ |
| 6037 | if (__dev_get_by_index(net, dev->ifindex)) { |
| 6038 | int iflink = (dev->iflink == dev->ifindex); |
| 6039 | dev->ifindex = dev_new_index(net); |
| 6040 | if (iflink) |
| 6041 | dev->iflink = dev->ifindex; |
| 6042 | } |
| 6043 | |
Serge Hallyn | 4e66ae2 | 2012-12-03 16:17:12 +0000 | [diff] [blame] | 6044 | /* Send a netdev-add uevent to the new namespace */ |
| 6045 | kobject_uevent(&dev->dev.kobj, KOBJ_ADD); |
| 6046 | |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 6047 | /* Fixup kobjects */ |
Eric W. Biederman | a1b3f59 | 2010-05-04 17:36:49 -0700 | [diff] [blame] | 6048 | err = device_rename(&dev->dev, dev->name); |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 6049 | WARN_ON(err); |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6050 | |
| 6051 | /* Add the device back in the hashes */ |
| 6052 | list_netdevice(dev); |
| 6053 | |
| 6054 | /* Notify protocols, that a new device appeared. */ |
| 6055 | call_netdevice_notifiers(NETDEV_REGISTER, dev); |
| 6056 | |
Eric W. Biederman | d90a909 | 2009-12-12 22:11:15 +0000 | [diff] [blame] | 6057 | /* |
| 6058 | * Prevent userspace races by waiting until the network |
| 6059 | * device is fully setup before sending notifications. |
| 6060 | */ |
| 6061 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); |
| 6062 | |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6063 | synchronize_net(); |
| 6064 | err = 0; |
| 6065 | out: |
| 6066 | return err; |
| 6067 | } |
Johannes Berg | 463d018 | 2009-07-14 00:33:35 +0200 | [diff] [blame] | 6068 | EXPORT_SYMBOL_GPL(dev_change_net_namespace); |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6069 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | static int dev_cpu_callback(struct notifier_block *nfb, |
| 6071 | unsigned long action, |
| 6072 | void *ocpu) |
| 6073 | { |
| 6074 | struct sk_buff **list_skb; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | struct sk_buff *skb; |
| 6076 | unsigned int cpu, oldcpu = (unsigned long)ocpu; |
| 6077 | struct softnet_data *sd, *oldsd; |
| 6078 | |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 6079 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | return NOTIFY_OK; |
| 6081 | |
| 6082 | local_irq_disable(); |
| 6083 | cpu = smp_processor_id(); |
| 6084 | sd = &per_cpu(softnet_data, cpu); |
| 6085 | oldsd = &per_cpu(softnet_data, oldcpu); |
| 6086 | |
| 6087 | /* Find end of our completion_queue. */ |
| 6088 | list_skb = &sd->completion_queue; |
| 6089 | while (*list_skb) |
| 6090 | list_skb = &(*list_skb)->next; |
| 6091 | /* Append completion queue from offline CPU. */ |
| 6092 | *list_skb = oldsd->completion_queue; |
| 6093 | oldsd->completion_queue = NULL; |
| 6094 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6095 | /* Append output queue from offline CPU. */ |
Changli Gao | a9cbd58 | 2010-04-26 23:06:24 +0000 | [diff] [blame] | 6096 | if (oldsd->output_queue) { |
| 6097 | *sd->output_queue_tailp = oldsd->output_queue; |
| 6098 | sd->output_queue_tailp = oldsd->output_queue_tailp; |
| 6099 | oldsd->output_queue = NULL; |
| 6100 | oldsd->output_queue_tailp = &oldsd->output_queue; |
| 6101 | } |
Heiko Carstens | 264524d | 2011-06-06 20:50:03 +0000 | [diff] [blame] | 6102 | /* Append NAPI poll list from offline CPU. */ |
| 6103 | if (!list_empty(&oldsd->poll_list)) { |
| 6104 | list_splice_init(&oldsd->poll_list, &sd->poll_list); |
| 6105 | raise_softirq_irqoff(NET_RX_SOFTIRQ); |
| 6106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6107 | |
| 6108 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
| 6109 | local_irq_enable(); |
| 6110 | |
| 6111 | /* Process offline CPU's input_pkt_queue */ |
Tom Herbert | 76cc8b1 | 2010-05-20 18:37:59 +0000 | [diff] [blame] | 6112 | while ((skb = __skb_dequeue(&oldsd->process_queue))) { |
| 6113 | netif_rx(skb); |
| 6114 | input_queue_head_incr(oldsd); |
| 6115 | } |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 6116 | while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6117 | netif_rx(skb); |
Tom Herbert | 76cc8b1 | 2010-05-20 18:37:59 +0000 | [diff] [blame] | 6118 | input_queue_head_incr(oldsd); |
Tom Herbert | fec5e65 | 2010-04-16 16:01:27 -0700 | [diff] [blame] | 6119 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6120 | |
| 6121 | return NOTIFY_OK; |
| 6122 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | |
| 6124 | |
Herbert Xu | 7f353bf | 2007-08-10 15:47:58 -0700 | [diff] [blame] | 6125 | /** |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 6126 | * netdev_increment_features - increment feature set by one |
| 6127 | * @all: current feature set |
| 6128 | * @one: new feature set |
| 6129 | * @mask: mask feature set |
Herbert Xu | 7f353bf | 2007-08-10 15:47:58 -0700 | [diff] [blame] | 6130 | * |
| 6131 | * Computes a new feature set after adding a device with feature set |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 6132 | * @one to the master device with current feature set @all. Will not |
| 6133 | * enable anything that is off in @mask. Returns the new feature set. |
Herbert Xu | 7f353bf | 2007-08-10 15:47:58 -0700 | [diff] [blame] | 6134 | */ |
Michał Mirosław | c8f44af | 2011-11-15 15:29:55 +0000 | [diff] [blame] | 6135 | netdev_features_t netdev_increment_features(netdev_features_t all, |
| 6136 | netdev_features_t one, netdev_features_t mask) |
Herbert Xu | 7f353bf | 2007-08-10 15:47:58 -0700 | [diff] [blame] | 6137 | { |
Michał Mirosław | 1742f18 | 2011-04-22 06:31:16 +0000 | [diff] [blame] | 6138 | if (mask & NETIF_F_GEN_CSUM) |
| 6139 | mask |= NETIF_F_ALL_CSUM; |
| 6140 | mask |= NETIF_F_VLAN_CHALLENGED; |
| 6141 | |
| 6142 | all |= one & (NETIF_F_ONE_FOR_ALL|NETIF_F_ALL_CSUM) & mask; |
| 6143 | all &= one | ~NETIF_F_ALL_FOR_ALL; |
| 6144 | |
Michał Mirosław | 1742f18 | 2011-04-22 06:31:16 +0000 | [diff] [blame] | 6145 | /* If one device supports hw checksumming, set for all. */ |
| 6146 | if (all & NETIF_F_GEN_CSUM) |
| 6147 | all &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_GEN_CSUM); |
Herbert Xu | 7f353bf | 2007-08-10 15:47:58 -0700 | [diff] [blame] | 6148 | |
| 6149 | return all; |
| 6150 | } |
Herbert Xu | b63365a | 2008-10-23 01:11:29 -0700 | [diff] [blame] | 6151 | EXPORT_SYMBOL(netdev_increment_features); |
Herbert Xu | 7f353bf | 2007-08-10 15:47:58 -0700 | [diff] [blame] | 6152 | |
Baruch Siach | 430f03c | 2013-06-02 20:43:55 +0000 | [diff] [blame] | 6153 | static struct hlist_head * __net_init netdev_create_hash(void) |
Pavel Emelyanov | 30d97d3 | 2007-09-16 15:40:33 -0700 | [diff] [blame] | 6154 | { |
| 6155 | int i; |
| 6156 | struct hlist_head *hash; |
| 6157 | |
| 6158 | hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL); |
| 6159 | if (hash != NULL) |
| 6160 | for (i = 0; i < NETDEV_HASHENTRIES; i++) |
| 6161 | INIT_HLIST_HEAD(&hash[i]); |
| 6162 | |
| 6163 | return hash; |
| 6164 | } |
| 6165 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 6166 | /* Initialize per network namespace state */ |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 6167 | static int __net_init netdev_init(struct net *net) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 6168 | { |
Rustad, Mark D | 734b654 | 2012-07-18 09:06:07 +0000 | [diff] [blame] | 6169 | if (net != &init_net) |
| 6170 | INIT_LIST_HEAD(&net->dev_base_head); |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 6171 | |
Pavel Emelyanov | 30d97d3 | 2007-09-16 15:40:33 -0700 | [diff] [blame] | 6172 | net->dev_name_head = netdev_create_hash(); |
| 6173 | if (net->dev_name_head == NULL) |
| 6174 | goto err_name; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 6175 | |
Pavel Emelyanov | 30d97d3 | 2007-09-16 15:40:33 -0700 | [diff] [blame] | 6176 | net->dev_index_head = netdev_create_hash(); |
| 6177 | if (net->dev_index_head == NULL) |
| 6178 | goto err_idx; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 6179 | |
| 6180 | return 0; |
Pavel Emelyanov | 30d97d3 | 2007-09-16 15:40:33 -0700 | [diff] [blame] | 6181 | |
| 6182 | err_idx: |
| 6183 | kfree(net->dev_name_head); |
| 6184 | err_name: |
| 6185 | return -ENOMEM; |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 6186 | } |
| 6187 | |
Stephen Hemminger | f0db275 | 2008-09-30 02:23:58 -0700 | [diff] [blame] | 6188 | /** |
| 6189 | * netdev_drivername - network driver for the device |
| 6190 | * @dev: network device |
Stephen Hemminger | f0db275 | 2008-09-30 02:23:58 -0700 | [diff] [blame] | 6191 | * |
| 6192 | * Determine network driver for device. |
| 6193 | */ |
David S. Miller | 3019de1 | 2011-06-06 16:41:33 -0700 | [diff] [blame] | 6194 | const char *netdev_drivername(const struct net_device *dev) |
Arjan van de Ven | 6579e57 | 2008-07-21 13:31:48 -0700 | [diff] [blame] | 6195 | { |
Stephen Hemminger | cf04a4c7 | 2008-09-30 02:22:14 -0700 | [diff] [blame] | 6196 | const struct device_driver *driver; |
| 6197 | const struct device *parent; |
David S. Miller | 3019de1 | 2011-06-06 16:41:33 -0700 | [diff] [blame] | 6198 | const char *empty = ""; |
Arjan van de Ven | 6579e57 | 2008-07-21 13:31:48 -0700 | [diff] [blame] | 6199 | |
| 6200 | parent = dev->dev.parent; |
Arjan van de Ven | 6579e57 | 2008-07-21 13:31:48 -0700 | [diff] [blame] | 6201 | if (!parent) |
David S. Miller | 3019de1 | 2011-06-06 16:41:33 -0700 | [diff] [blame] | 6202 | return empty; |
Arjan van de Ven | 6579e57 | 2008-07-21 13:31:48 -0700 | [diff] [blame] | 6203 | |
| 6204 | driver = parent->driver; |
| 6205 | if (driver && driver->name) |
David S. Miller | 3019de1 | 2011-06-06 16:41:33 -0700 | [diff] [blame] | 6206 | return driver->name; |
| 6207 | return empty; |
Arjan van de Ven | 6579e57 | 2008-07-21 13:31:48 -0700 | [diff] [blame] | 6208 | } |
| 6209 | |
Joe Perches | b004ff4 | 2012-09-12 20:12:19 -0700 | [diff] [blame] | 6210 | static int __netdev_printk(const char *level, const struct net_device *dev, |
Joe Perches | 256df2f | 2010-06-27 01:02:35 +0000 | [diff] [blame] | 6211 | struct va_format *vaf) |
| 6212 | { |
| 6213 | int r; |
| 6214 | |
Joe Perches | b004ff4 | 2012-09-12 20:12:19 -0700 | [diff] [blame] | 6215 | if (dev && dev->dev.parent) { |
Joe Perches | 666f355 | 2012-09-12 20:14:11 -0700 | [diff] [blame] | 6216 | r = dev_printk_emit(level[1] - '0', |
| 6217 | dev->dev.parent, |
| 6218 | "%s %s %s: %pV", |
| 6219 | dev_driver_string(dev->dev.parent), |
| 6220 | dev_name(dev->dev.parent), |
| 6221 | netdev_name(dev), vaf); |
Joe Perches | b004ff4 | 2012-09-12 20:12:19 -0700 | [diff] [blame] | 6222 | } else if (dev) { |
Joe Perches | 256df2f | 2010-06-27 01:02:35 +0000 | [diff] [blame] | 6223 | r = printk("%s%s: %pV", level, netdev_name(dev), vaf); |
Joe Perches | b004ff4 | 2012-09-12 20:12:19 -0700 | [diff] [blame] | 6224 | } else { |
Joe Perches | 256df2f | 2010-06-27 01:02:35 +0000 | [diff] [blame] | 6225 | r = printk("%s(NULL net_device): %pV", level, vaf); |
Joe Perches | b004ff4 | 2012-09-12 20:12:19 -0700 | [diff] [blame] | 6226 | } |
Joe Perches | 256df2f | 2010-06-27 01:02:35 +0000 | [diff] [blame] | 6227 | |
| 6228 | return r; |
| 6229 | } |
| 6230 | |
| 6231 | int netdev_printk(const char *level, const struct net_device *dev, |
| 6232 | const char *format, ...) |
| 6233 | { |
| 6234 | struct va_format vaf; |
| 6235 | va_list args; |
| 6236 | int r; |
| 6237 | |
| 6238 | va_start(args, format); |
| 6239 | |
| 6240 | vaf.fmt = format; |
| 6241 | vaf.va = &args; |
| 6242 | |
| 6243 | r = __netdev_printk(level, dev, &vaf); |
Joe Perches | b004ff4 | 2012-09-12 20:12:19 -0700 | [diff] [blame] | 6244 | |
Joe Perches | 256df2f | 2010-06-27 01:02:35 +0000 | [diff] [blame] | 6245 | va_end(args); |
| 6246 | |
| 6247 | return r; |
| 6248 | } |
| 6249 | EXPORT_SYMBOL(netdev_printk); |
| 6250 | |
| 6251 | #define define_netdev_printk_level(func, level) \ |
| 6252 | int func(const struct net_device *dev, const char *fmt, ...) \ |
| 6253 | { \ |
| 6254 | int r; \ |
| 6255 | struct va_format vaf; \ |
| 6256 | va_list args; \ |
| 6257 | \ |
| 6258 | va_start(args, fmt); \ |
| 6259 | \ |
| 6260 | vaf.fmt = fmt; \ |
| 6261 | vaf.va = &args; \ |
| 6262 | \ |
| 6263 | r = __netdev_printk(level, dev, &vaf); \ |
Joe Perches | b004ff4 | 2012-09-12 20:12:19 -0700 | [diff] [blame] | 6264 | \ |
Joe Perches | 256df2f | 2010-06-27 01:02:35 +0000 | [diff] [blame] | 6265 | va_end(args); \ |
| 6266 | \ |
| 6267 | return r; \ |
| 6268 | } \ |
| 6269 | EXPORT_SYMBOL(func); |
| 6270 | |
| 6271 | define_netdev_printk_level(netdev_emerg, KERN_EMERG); |
| 6272 | define_netdev_printk_level(netdev_alert, KERN_ALERT); |
| 6273 | define_netdev_printk_level(netdev_crit, KERN_CRIT); |
| 6274 | define_netdev_printk_level(netdev_err, KERN_ERR); |
| 6275 | define_netdev_printk_level(netdev_warn, KERN_WARNING); |
| 6276 | define_netdev_printk_level(netdev_notice, KERN_NOTICE); |
| 6277 | define_netdev_printk_level(netdev_info, KERN_INFO); |
| 6278 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 6279 | static void __net_exit netdev_exit(struct net *net) |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 6280 | { |
| 6281 | kfree(net->dev_name_head); |
| 6282 | kfree(net->dev_index_head); |
| 6283 | } |
| 6284 | |
Denis V. Lunev | 022cbae | 2007-11-13 03:23:50 -0800 | [diff] [blame] | 6285 | static struct pernet_operations __net_initdata netdev_net_ops = { |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 6286 | .init = netdev_init, |
| 6287 | .exit = netdev_exit, |
| 6288 | }; |
| 6289 | |
Pavel Emelyanov | 4665079 | 2007-10-08 20:38:39 -0700 | [diff] [blame] | 6290 | static void __net_exit default_device_exit(struct net *net) |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6291 | { |
Eric W. Biederman | e008b5f | 2009-11-29 22:25:30 +0000 | [diff] [blame] | 6292 | struct net_device *dev, *aux; |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6293 | /* |
Eric W. Biederman | e008b5f | 2009-11-29 22:25:30 +0000 | [diff] [blame] | 6294 | * Push all migratable network devices back to the |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6295 | * initial network namespace |
| 6296 | */ |
| 6297 | rtnl_lock(); |
Eric W. Biederman | e008b5f | 2009-11-29 22:25:30 +0000 | [diff] [blame] | 6298 | for_each_netdev_safe(net, dev, aux) { |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6299 | int err; |
Pavel Emelyanov | aca5139 | 2008-05-08 01:24:25 -0700 | [diff] [blame] | 6300 | char fb_name[IFNAMSIZ]; |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6301 | |
| 6302 | /* Ignore unmoveable devices (i.e. loopback) */ |
| 6303 | if (dev->features & NETIF_F_NETNS_LOCAL) |
| 6304 | continue; |
| 6305 | |
Eric W. Biederman | e008b5f | 2009-11-29 22:25:30 +0000 | [diff] [blame] | 6306 | /* Leave virtual devices for the generic cleanup */ |
| 6307 | if (dev->rtnl_link_ops) |
| 6308 | continue; |
Eric W. Biederman | d0c082c | 2008-11-05 15:59:38 -0800 | [diff] [blame] | 6309 | |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6310 | /* Push remaining network devices to init_net */ |
Pavel Emelyanov | aca5139 | 2008-05-08 01:24:25 -0700 | [diff] [blame] | 6311 | snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex); |
| 6312 | err = dev_change_net_namespace(dev, &init_net, fb_name); |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6313 | if (err) { |
Joe Perches | 7b6cd1c | 2012-02-01 10:54:43 +0000 | [diff] [blame] | 6314 | pr_emerg("%s: failed to move %s to init_net: %d\n", |
| 6315 | __func__, dev->name, err); |
Pavel Emelyanov | aca5139 | 2008-05-08 01:24:25 -0700 | [diff] [blame] | 6316 | BUG(); |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6317 | } |
| 6318 | } |
| 6319 | rtnl_unlock(); |
| 6320 | } |
| 6321 | |
Eric W. Biederman | 04dc7f6b | 2009-12-03 02:29:04 +0000 | [diff] [blame] | 6322 | static void __net_exit default_device_exit_batch(struct list_head *net_list) |
| 6323 | { |
| 6324 | /* At exit all network devices most be removed from a network |
Uwe Kleine-König | b595076 | 2010-11-01 15:38:34 -0400 | [diff] [blame] | 6325 | * namespace. Do this in the reverse order of registration. |
Eric W. Biederman | 04dc7f6b | 2009-12-03 02:29:04 +0000 | [diff] [blame] | 6326 | * Do this across as many network namespaces as possible to |
| 6327 | * improve batching efficiency. |
| 6328 | */ |
| 6329 | struct net_device *dev; |
| 6330 | struct net *net; |
| 6331 | LIST_HEAD(dev_kill_list); |
| 6332 | |
| 6333 | rtnl_lock(); |
| 6334 | list_for_each_entry(net, net_list, exit_list) { |
| 6335 | for_each_netdev_reverse(net, dev) { |
| 6336 | if (dev->rtnl_link_ops) |
| 6337 | dev->rtnl_link_ops->dellink(dev, &dev_kill_list); |
| 6338 | else |
| 6339 | unregister_netdevice_queue(dev, &dev_kill_list); |
| 6340 | } |
| 6341 | } |
| 6342 | unregister_netdevice_many(&dev_kill_list); |
Eric Dumazet | ceaaec9 | 2011-02-17 22:59:19 +0000 | [diff] [blame] | 6343 | list_del(&dev_kill_list); |
Eric W. Biederman | 04dc7f6b | 2009-12-03 02:29:04 +0000 | [diff] [blame] | 6344 | rtnl_unlock(); |
| 6345 | } |
| 6346 | |
Denis V. Lunev | 022cbae | 2007-11-13 03:23:50 -0800 | [diff] [blame] | 6347 | static struct pernet_operations __net_initdata default_device_ops = { |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6348 | .exit = default_device_exit, |
Eric W. Biederman | 04dc7f6b | 2009-12-03 02:29:04 +0000 | [diff] [blame] | 6349 | .exit_batch = default_device_exit_batch, |
Eric W. Biederman | ce286d3 | 2007-09-12 13:53:49 +0200 | [diff] [blame] | 6350 | }; |
| 6351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6352 | /* |
| 6353 | * Initialize the DEV module. At boot time this walks the device list and |
| 6354 | * unhooks any devices that fail to initialise (normally hardware not |
| 6355 | * present) and leaves us with a valid list of present and active devices. |
| 6356 | * |
| 6357 | */ |
| 6358 | |
| 6359 | /* |
| 6360 | * This is called single threaded during boot, so no need |
| 6361 | * to take the rtnl semaphore. |
| 6362 | */ |
| 6363 | static int __init net_dev_init(void) |
| 6364 | { |
| 6365 | int i, rc = -ENOMEM; |
| 6366 | |
| 6367 | BUG_ON(!dev_boot_phase); |
| 6368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6369 | if (dev_proc_init()) |
| 6370 | goto out; |
| 6371 | |
Eric W. Biederman | 8b41d18 | 2007-09-26 22:02:53 -0700 | [diff] [blame] | 6372 | if (netdev_kobject_init()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6373 | goto out; |
| 6374 | |
| 6375 | INIT_LIST_HEAD(&ptype_all); |
Pavel Emelyanov | 82d8a867 | 2007-11-26 20:12:58 +0800 | [diff] [blame] | 6376 | for (i = 0; i < PTYPE_HASH_SIZE; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6377 | INIT_LIST_HEAD(&ptype_base[i]); |
| 6378 | |
Vlad Yasevich | 62532da | 2012-11-15 08:49:10 +0000 | [diff] [blame] | 6379 | INIT_LIST_HEAD(&offload_base); |
| 6380 | |
Eric W. Biederman | 881d966 | 2007-09-17 11:56:21 -0700 | [diff] [blame] | 6381 | if (register_pernet_subsys(&netdev_net_ops)) |
| 6382 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | |
| 6384 | /* |
| 6385 | * Initialise the packet receive queues. |
| 6386 | */ |
| 6387 | |
KAMEZAWA Hiroyuki | 6f91204 | 2006-04-10 22:52:50 -0700 | [diff] [blame] | 6388 | for_each_possible_cpu(i) { |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 6389 | struct softnet_data *sd = &per_cpu(softnet_data, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6390 | |
Changli Gao | dee4287 | 2010-05-02 05:42:16 +0000 | [diff] [blame] | 6391 | memset(sd, 0, sizeof(*sd)); |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 6392 | skb_queue_head_init(&sd->input_pkt_queue); |
Changli Gao | 6e7676c | 2010-04-27 15:07:33 -0700 | [diff] [blame] | 6393 | skb_queue_head_init(&sd->process_queue); |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 6394 | sd->completion_queue = NULL; |
| 6395 | INIT_LIST_HEAD(&sd->poll_list); |
Changli Gao | a9cbd58 | 2010-04-26 23:06:24 +0000 | [diff] [blame] | 6396 | sd->output_queue = NULL; |
| 6397 | sd->output_queue_tailp = &sd->output_queue; |
Eric Dumazet | df33454 | 2010-03-24 19:13:54 +0000 | [diff] [blame] | 6398 | #ifdef CONFIG_RPS |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 6399 | sd->csd.func = rps_trigger_softirq; |
| 6400 | sd->csd.info = sd; |
| 6401 | sd->csd.flags = 0; |
| 6402 | sd->cpu = i; |
Tom Herbert | 1e94d72 | 2010-03-18 17:45:44 -0700 | [diff] [blame] | 6403 | #endif |
Tom Herbert | 0a9627f | 2010-03-16 08:03:29 +0000 | [diff] [blame] | 6404 | |
Eric Dumazet | e36fa2f | 2010-04-19 21:17:14 +0000 | [diff] [blame] | 6405 | sd->backlog.poll = process_backlog; |
| 6406 | sd->backlog.weight = weight_p; |
| 6407 | sd->backlog.gro_list = NULL; |
| 6408 | sd->backlog.gro_count = 0; |
Willem de Bruijn | 99bbc70 | 2013-05-20 04:02:32 +0000 | [diff] [blame] | 6409 | |
| 6410 | #ifdef CONFIG_NET_FLOW_LIMIT |
| 6411 | sd->flow_limit = NULL; |
| 6412 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6413 | } |
| 6414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6415 | dev_boot_phase = 0; |
| 6416 | |
Eric W. Biederman | 505d4f7 | 2008-11-07 22:54:20 -0800 | [diff] [blame] | 6417 | /* The loopback device is special if any other network devices |
| 6418 | * is present in a network namespace the loopback device must |
| 6419 | * be present. Since we now dynamically allocate and free the |
| 6420 | * loopback device ensure this invariant is maintained by |
| 6421 | * keeping the loopback device as the first device on the |
| 6422 | * list of network devices. Ensuring the loopback devices |
| 6423 | * is the first device that appears and the last network device |
| 6424 | * that disappears. |
| 6425 | */ |
| 6426 | if (register_pernet_device(&loopback_net_ops)) |
| 6427 | goto out; |
| 6428 | |
| 6429 | if (register_pernet_device(&default_device_ops)) |
| 6430 | goto out; |
| 6431 | |
Carlos R. Mafra | 962cf36 | 2008-05-15 11:15:37 -0300 | [diff] [blame] | 6432 | open_softirq(NET_TX_SOFTIRQ, net_tx_action); |
| 6433 | open_softirq(NET_RX_SOFTIRQ, net_rx_action); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | |
| 6435 | hotcpu_notifier(dev_cpu_callback, 0); |
| 6436 | dst_init(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6437 | rc = 0; |
| 6438 | out: |
| 6439 | return rc; |
| 6440 | } |
| 6441 | |
| 6442 | subsys_initcall(net_dev_init); |