Commit | Line | Data |
---|---|---|
1da177e4 LT |
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 | |
02c30a84 | 10 | * Authors: Ross Biro |
1da177e4 LT |
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> | |
1da177e4 | 76 | #include <linux/bitops.h> |
4fc268d2 | 77 | #include <linux/capability.h> |
1da177e4 LT |
78 | #include <linux/cpu.h> |
79 | #include <linux/types.h> | |
80 | #include <linux/kernel.h> | |
08e9897d | 81 | #include <linux/hash.h> |
5a0e3ad6 | 82 | #include <linux/slab.h> |
1da177e4 | 83 | #include <linux/sched.h> |
4a3e2f71 | 84 | #include <linux/mutex.h> |
1da177e4 LT |
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> | |
0187bdfb | 94 | #include <linux/ethtool.h> |
1da177e4 LT |
95 | #include <linux/notifier.h> |
96 | #include <linux/skbuff.h> | |
457c4cbc | 97 | #include <net/net_namespace.h> |
1da177e4 LT |
98 | #include <net/sock.h> |
99 | #include <linux/rtnetlink.h> | |
100 | #include <linux/proc_fs.h> | |
101 | #include <linux/seq_file.h> | |
102 | #include <linux/stat.h> | |
1da177e4 LT |
103 | #include <net/dst.h> |
104 | #include <net/pkt_sched.h> | |
105 | #include <net/checksum.h> | |
44540960 | 106 | #include <net/xfrm.h> |
1da177e4 LT |
107 | #include <linux/highmem.h> |
108 | #include <linux/init.h> | |
109 | #include <linux/kmod.h> | |
110 | #include <linux/module.h> | |
1da177e4 LT |
111 | #include <linux/netpoll.h> |
112 | #include <linux/rcupdate.h> | |
113 | #include <linux/delay.h> | |
295f4a1f | 114 | #include <net/wext.h> |
1da177e4 | 115 | #include <net/iw_handler.h> |
1da177e4 | 116 | #include <asm/current.h> |
5bdb9886 | 117 | #include <linux/audit.h> |
db217334 | 118 | #include <linux/dmaengine.h> |
f6a78bfc | 119 | #include <linux/err.h> |
c7fa9d18 | 120 | #include <linux/ctype.h> |
723e98b7 | 121 | #include <linux/if_arp.h> |
6de329e2 | 122 | #include <linux/if_vlan.h> |
8f0f2223 | 123 | #include <linux/ip.h> |
ad55dcaf | 124 | #include <net/ip.h> |
8f0f2223 DM |
125 | #include <linux/ipv6.h> |
126 | #include <linux/in.h> | |
b6b2fed1 DM |
127 | #include <linux/jhash.h> |
128 | #include <linux/random.h> | |
9cbc1cb8 | 129 | #include <trace/events/napi.h> |
cf66ba58 | 130 | #include <trace/events/net.h> |
07dc22e7 | 131 | #include <trace/events/skb.h> |
5acbbd42 | 132 | #include <linux/pci.h> |
caeda9b9 | 133 | #include <linux/inetdevice.h> |
c445477d | 134 | #include <linux/cpu_rmap.h> |
4dc360c5 | 135 | #include <linux/net_tstamp.h> |
c5905afb | 136 | #include <linux/static_key.h> |
4504b861 | 137 | #include <net/flow_keys.h> |
1da177e4 | 138 | |
342709ef PE |
139 | #include "net-sysfs.h" |
140 | ||
d565b0a1 HX |
141 | /* Instead of increasing this, you should create a hash table. */ |
142 | #define MAX_GRO_SKBS 8 | |
143 | ||
5d38a079 HX |
144 | /* This should be increased if a protocol with a bigger head is added. */ |
145 | #define GRO_MAX_HEAD (MAX_HEADER + 128) | |
146 | ||
1da177e4 LT |
147 | /* |
148 | * The list of packet types we will receive (as opposed to discard) | |
149 | * and the routines to invoke. | |
150 | * | |
151 | * Why 16. Because with 16 the only overlap we get on a hash of the | |
152 | * low nibble of the protocol value is RARP/SNAP/X.25. | |
153 | * | |
154 | * NOTE: That is no longer true with the addition of VLAN tags. Not | |
155 | * sure which should go first, but I bet it won't make much | |
156 | * difference if we are running VLANs. The good news is that | |
157 | * this protocol won't be in the list unless compiled in, so | |
3041a069 | 158 | * the average user (w/out VLANs) will not be adversely affected. |
1da177e4 LT |
159 | * --BLG |
160 | * | |
161 | * 0800 IP | |
162 | * 8100 802.1Q VLAN | |
163 | * 0001 802.3 | |
164 | * 0002 AX.25 | |
165 | * 0004 802.2 | |
166 | * 8035 RARP | |
167 | * 0005 SNAP | |
168 | * 0805 X.25 | |
169 | * 0806 ARP | |
170 | * 8137 IPX | |
171 | * 0009 Localtalk | |
172 | * 86DD IPv6 | |
173 | */ | |
174 | ||
82d8a867 PE |
175 | #define PTYPE_HASH_SIZE (16) |
176 | #define PTYPE_HASH_MASK (PTYPE_HASH_SIZE - 1) | |
177 | ||
1da177e4 | 178 | static DEFINE_SPINLOCK(ptype_lock); |
62532da9 | 179 | static DEFINE_SPINLOCK(offload_lock); |
82d8a867 | 180 | static struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly; |
6b2bedc3 | 181 | static struct list_head ptype_all __read_mostly; /* Taps */ |
62532da9 | 182 | static struct list_head offload_base __read_mostly; |
1da177e4 | 183 | |
1da177e4 | 184 | /* |
7562f876 | 185 | * The @dev_base_head list is protected by @dev_base_lock and the rtnl |
1da177e4 LT |
186 | * semaphore. |
187 | * | |
c6d14c84 | 188 | * Pure readers hold dev_base_lock for reading, or rcu_read_lock() |
1da177e4 LT |
189 | * |
190 | * Writers must hold the rtnl semaphore while they loop through the | |
7562f876 | 191 | * dev_base_head list, and hold dev_base_lock for writing when they do the |
1da177e4 LT |
192 | * actual updates. This allows pure readers to access the list even |
193 | * while a writer is preparing to update it. | |
194 | * | |
195 | * To put it another way, dev_base_lock is held for writing only to | |
196 | * protect against pure readers; the rtnl semaphore provides the | |
197 | * protection against other writers. | |
198 | * | |
199 | * See, for example usages, register_netdevice() and | |
200 | * unregister_netdevice(), which must be called with the rtnl | |
201 | * semaphore held. | |
202 | */ | |
1da177e4 | 203 | DEFINE_RWLOCK(dev_base_lock); |
1da177e4 LT |
204 | EXPORT_SYMBOL(dev_base_lock); |
205 | ||
30e6c9fa | 206 | seqcount_t devnet_rename_seq; |
c91f6df2 | 207 | |
4e985ada TG |
208 | static inline void dev_base_seq_inc(struct net *net) |
209 | { | |
210 | while (++net->dev_base_seq == 0); | |
211 | } | |
212 | ||
881d966b | 213 | static inline struct hlist_head *dev_name_hash(struct net *net, const char *name) |
1da177e4 | 214 | { |
95c96174 ED |
215 | unsigned int hash = full_name_hash(name, strnlen(name, IFNAMSIZ)); |
216 | ||
08e9897d | 217 | return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)]; |
1da177e4 LT |
218 | } |
219 | ||
881d966b | 220 | static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex) |
1da177e4 | 221 | { |
7c28bd0b | 222 | return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)]; |
1da177e4 LT |
223 | } |
224 | ||
e36fa2f7 | 225 | static inline void rps_lock(struct softnet_data *sd) |
152102c7 CG |
226 | { |
227 | #ifdef CONFIG_RPS | |
e36fa2f7 | 228 | spin_lock(&sd->input_pkt_queue.lock); |
152102c7 CG |
229 | #endif |
230 | } | |
231 | ||
e36fa2f7 | 232 | static inline void rps_unlock(struct softnet_data *sd) |
152102c7 CG |
233 | { |
234 | #ifdef CONFIG_RPS | |
e36fa2f7 | 235 | spin_unlock(&sd->input_pkt_queue.lock); |
152102c7 CG |
236 | #endif |
237 | } | |
238 | ||
ce286d32 EB |
239 | /* Device list insertion */ |
240 | static int list_netdevice(struct net_device *dev) | |
241 | { | |
c346dca1 | 242 | struct net *net = dev_net(dev); |
ce286d32 EB |
243 | |
244 | ASSERT_RTNL(); | |
245 | ||
246 | write_lock_bh(&dev_base_lock); | |
c6d14c84 | 247 | list_add_tail_rcu(&dev->dev_list, &net->dev_base_head); |
72c9528b | 248 | hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name)); |
fb699dfd ED |
249 | hlist_add_head_rcu(&dev->index_hlist, |
250 | dev_index_hash(net, dev->ifindex)); | |
ce286d32 | 251 | write_unlock_bh(&dev_base_lock); |
4e985ada TG |
252 | |
253 | dev_base_seq_inc(net); | |
254 | ||
ce286d32 EB |
255 | return 0; |
256 | } | |
257 | ||
fb699dfd ED |
258 | /* Device list removal |
259 | * caller must respect a RCU grace period before freeing/reusing dev | |
260 | */ | |
ce286d32 EB |
261 | static void unlist_netdevice(struct net_device *dev) |
262 | { | |
263 | ASSERT_RTNL(); | |
264 | ||
265 | /* Unlink dev from the device chain */ | |
266 | write_lock_bh(&dev_base_lock); | |
c6d14c84 | 267 | list_del_rcu(&dev->dev_list); |
72c9528b | 268 | hlist_del_rcu(&dev->name_hlist); |
fb699dfd | 269 | hlist_del_rcu(&dev->index_hlist); |
ce286d32 | 270 | write_unlock_bh(&dev_base_lock); |
4e985ada TG |
271 | |
272 | dev_base_seq_inc(dev_net(dev)); | |
ce286d32 EB |
273 | } |
274 | ||
1da177e4 LT |
275 | /* |
276 | * Our notifier list | |
277 | */ | |
278 | ||
f07d5b94 | 279 | static RAW_NOTIFIER_HEAD(netdev_chain); |
1da177e4 LT |
280 | |
281 | /* | |
282 | * Device drivers call our routines to queue packets here. We empty the | |
283 | * queue in the local softnet handler. | |
284 | */ | |
bea3348e | 285 | |
9958da05 | 286 | DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data); |
d1b19dff | 287 | EXPORT_PER_CPU_SYMBOL(softnet_data); |
1da177e4 | 288 | |
cf508b12 | 289 | #ifdef CONFIG_LOCKDEP |
723e98b7 | 290 | /* |
c773e847 | 291 | * register_netdevice() inits txq->_xmit_lock and sets lockdep class |
723e98b7 JP |
292 | * according to dev->type |
293 | */ | |
294 | static const unsigned short netdev_lock_type[] = | |
295 | {ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25, | |
296 | ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET, | |
297 | ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM, | |
298 | ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP, | |
299 | ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD, | |
300 | ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25, | |
301 | ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP, | |
302 | ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD, | |
303 | ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI, | |
304 | ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE, | |
305 | ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET, | |
306 | ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL, | |
211ed865 PG |
307 | ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM, |
308 | ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE, | |
309 | ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE}; | |
723e98b7 | 310 | |
36cbd3dc | 311 | static const char *const netdev_lock_name[] = |
723e98b7 JP |
312 | {"_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25", |
313 | "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET", | |
314 | "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM", | |
315 | "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP", | |
316 | "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD", | |
317 | "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25", | |
318 | "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP", | |
319 | "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD", | |
320 | "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI", | |
321 | "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE", | |
322 | "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET", | |
323 | "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL", | |
211ed865 PG |
324 | "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM", |
325 | "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE", | |
326 | "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"}; | |
723e98b7 JP |
327 | |
328 | static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)]; | |
cf508b12 | 329 | static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)]; |
723e98b7 JP |
330 | |
331 | static inline unsigned short netdev_lock_pos(unsigned short dev_type) | |
332 | { | |
333 | int i; | |
334 | ||
335 | for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++) | |
336 | if (netdev_lock_type[i] == dev_type) | |
337 | return i; | |
338 | /* the last key is used by default */ | |
339 | return ARRAY_SIZE(netdev_lock_type) - 1; | |
340 | } | |
341 | ||
cf508b12 DM |
342 | static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, |
343 | unsigned short dev_type) | |
723e98b7 JP |
344 | { |
345 | int i; | |
346 | ||
347 | i = netdev_lock_pos(dev_type); | |
348 | lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i], | |
349 | netdev_lock_name[i]); | |
350 | } | |
cf508b12 DM |
351 | |
352 | static inline void netdev_set_addr_lockdep_class(struct net_device *dev) | |
353 | { | |
354 | int i; | |
355 | ||
356 | i = netdev_lock_pos(dev->type); | |
357 | lockdep_set_class_and_name(&dev->addr_list_lock, | |
358 | &netdev_addr_lock_key[i], | |
359 | netdev_lock_name[i]); | |
360 | } | |
723e98b7 | 361 | #else |
cf508b12 DM |
362 | static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock, |
363 | unsigned short dev_type) | |
364 | { | |
365 | } | |
366 | static inline void netdev_set_addr_lockdep_class(struct net_device *dev) | |
723e98b7 JP |
367 | { |
368 | } | |
369 | #endif | |
1da177e4 LT |
370 | |
371 | /******************************************************************************* | |
372 | ||
373 | Protocol management and registration routines | |
374 | ||
375 | *******************************************************************************/ | |
376 | ||
1da177e4 LT |
377 | /* |
378 | * Add a protocol ID to the list. Now that the input handler is | |
379 | * smarter we can dispense with all the messy stuff that used to be | |
380 | * here. | |
381 | * | |
382 | * BEWARE!!! Protocol handlers, mangling input packets, | |
383 | * MUST BE last in hash buckets and checking protocol handlers | |
384 | * MUST start from promiscuous ptype_all chain in net_bh. | |
385 | * It is true now, do not change it. | |
386 | * Explanation follows: if protocol handler, mangling packet, will | |
387 | * be the first on list, it is not able to sense, that packet | |
388 | * is cloned and should be copied-on-write, so that it will | |
389 | * change it and subsequent readers will get broken packet. | |
390 | * --ANK (980803) | |
391 | */ | |
392 | ||
c07b68e8 ED |
393 | static inline struct list_head *ptype_head(const struct packet_type *pt) |
394 | { | |
395 | if (pt->type == htons(ETH_P_ALL)) | |
396 | return &ptype_all; | |
397 | else | |
398 | return &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK]; | |
399 | } | |
400 | ||
1da177e4 LT |
401 | /** |
402 | * dev_add_pack - add packet handler | |
403 | * @pt: packet type declaration | |
404 | * | |
405 | * Add a protocol handler to the networking stack. The passed &packet_type | |
406 | * is linked into kernel lists and may not be freed until it has been | |
407 | * removed from the kernel lists. | |
408 | * | |
4ec93edb | 409 | * This call does not sleep therefore it can not |
1da177e4 LT |
410 | * guarantee all CPU's that are in middle of receiving packets |
411 | * will see the new packet type (until the next received packet). | |
412 | */ | |
413 | ||
414 | void dev_add_pack(struct packet_type *pt) | |
415 | { | |
c07b68e8 | 416 | struct list_head *head = ptype_head(pt); |
1da177e4 | 417 | |
c07b68e8 ED |
418 | spin_lock(&ptype_lock); |
419 | list_add_rcu(&pt->list, head); | |
420 | spin_unlock(&ptype_lock); | |
1da177e4 | 421 | } |
d1b19dff | 422 | EXPORT_SYMBOL(dev_add_pack); |
1da177e4 | 423 | |
1da177e4 LT |
424 | /** |
425 | * __dev_remove_pack - remove packet handler | |
426 | * @pt: packet type declaration | |
427 | * | |
428 | * Remove a protocol handler that was previously added to the kernel | |
429 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed | |
430 | * from the kernel lists and can be freed or reused once this function | |
4ec93edb | 431 | * returns. |
1da177e4 LT |
432 | * |
433 | * The packet type might still be in use by receivers | |
434 | * and must not be freed until after all the CPU's have gone | |
435 | * through a quiescent state. | |
436 | */ | |
437 | void __dev_remove_pack(struct packet_type *pt) | |
438 | { | |
c07b68e8 | 439 | struct list_head *head = ptype_head(pt); |
1da177e4 LT |
440 | struct packet_type *pt1; |
441 | ||
c07b68e8 | 442 | spin_lock(&ptype_lock); |
1da177e4 LT |
443 | |
444 | list_for_each_entry(pt1, head, list) { | |
445 | if (pt == pt1) { | |
446 | list_del_rcu(&pt->list); | |
447 | goto out; | |
448 | } | |
449 | } | |
450 | ||
7b6cd1ce | 451 | pr_warn("dev_remove_pack: %p not found\n", pt); |
1da177e4 | 452 | out: |
c07b68e8 | 453 | spin_unlock(&ptype_lock); |
1da177e4 | 454 | } |
d1b19dff ED |
455 | EXPORT_SYMBOL(__dev_remove_pack); |
456 | ||
1da177e4 LT |
457 | /** |
458 | * dev_remove_pack - remove packet handler | |
459 | * @pt: packet type declaration | |
460 | * | |
461 | * Remove a protocol handler that was previously added to the kernel | |
462 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed | |
463 | * from the kernel lists and can be freed or reused once this function | |
464 | * returns. | |
465 | * | |
466 | * This call sleeps to guarantee that no CPU is looking at the packet | |
467 | * type after return. | |
468 | */ | |
469 | void dev_remove_pack(struct packet_type *pt) | |
470 | { | |
471 | __dev_remove_pack(pt); | |
4ec93edb | 472 | |
1da177e4 LT |
473 | synchronize_net(); |
474 | } | |
d1b19dff | 475 | EXPORT_SYMBOL(dev_remove_pack); |
1da177e4 | 476 | |
62532da9 VY |
477 | |
478 | /** | |
479 | * dev_add_offload - register offload handlers | |
480 | * @po: protocol offload declaration | |
481 | * | |
482 | * Add protocol offload handlers to the networking stack. The passed | |
483 | * &proto_offload is linked into kernel lists and may not be freed until | |
484 | * it has been removed from the kernel lists. | |
485 | * | |
486 | * This call does not sleep therefore it can not | |
487 | * guarantee all CPU's that are in middle of receiving packets | |
488 | * will see the new offload handlers (until the next received packet). | |
489 | */ | |
490 | void dev_add_offload(struct packet_offload *po) | |
491 | { | |
492 | struct list_head *head = &offload_base; | |
493 | ||
494 | spin_lock(&offload_lock); | |
495 | list_add_rcu(&po->list, head); | |
496 | spin_unlock(&offload_lock); | |
497 | } | |
498 | EXPORT_SYMBOL(dev_add_offload); | |
499 | ||
500 | /** | |
501 | * __dev_remove_offload - remove offload handler | |
502 | * @po: packet offload declaration | |
503 | * | |
504 | * Remove a protocol offload handler that was previously added to the | |
505 | * kernel offload handlers by dev_add_offload(). The passed &offload_type | |
506 | * is removed from the kernel lists and can be freed or reused once this | |
507 | * function returns. | |
508 | * | |
509 | * The packet type might still be in use by receivers | |
510 | * and must not be freed until after all the CPU's have gone | |
511 | * through a quiescent state. | |
512 | */ | |
513 | void __dev_remove_offload(struct packet_offload *po) | |
514 | { | |
515 | struct list_head *head = &offload_base; | |
516 | struct packet_offload *po1; | |
517 | ||
c53aa505 | 518 | spin_lock(&offload_lock); |
62532da9 VY |
519 | |
520 | list_for_each_entry(po1, head, list) { | |
521 | if (po == po1) { | |
522 | list_del_rcu(&po->list); | |
523 | goto out; | |
524 | } | |
525 | } | |
526 | ||
527 | pr_warn("dev_remove_offload: %p not found\n", po); | |
528 | out: | |
c53aa505 | 529 | spin_unlock(&offload_lock); |
62532da9 VY |
530 | } |
531 | EXPORT_SYMBOL(__dev_remove_offload); | |
532 | ||
533 | /** | |
534 | * dev_remove_offload - remove packet offload handler | |
535 | * @po: packet offload declaration | |
536 | * | |
537 | * Remove a packet offload handler that was previously added to the kernel | |
538 | * offload handlers by dev_add_offload(). The passed &offload_type is | |
539 | * removed from the kernel lists and can be freed or reused once this | |
540 | * function returns. | |
541 | * | |
542 | * This call sleeps to guarantee that no CPU is looking at the packet | |
543 | * type after return. | |
544 | */ | |
545 | void dev_remove_offload(struct packet_offload *po) | |
546 | { | |
547 | __dev_remove_offload(po); | |
548 | ||
549 | synchronize_net(); | |
550 | } | |
551 | EXPORT_SYMBOL(dev_remove_offload); | |
552 | ||
1da177e4 LT |
553 | /****************************************************************************** |
554 | ||
555 | Device Boot-time Settings Routines | |
556 | ||
557 | *******************************************************************************/ | |
558 | ||
559 | /* Boot time configuration table */ | |
560 | static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX]; | |
561 | ||
562 | /** | |
563 | * netdev_boot_setup_add - add new setup entry | |
564 | * @name: name of the device | |
565 | * @map: configured settings for the device | |
566 | * | |
567 | * Adds new setup entry to the dev_boot_setup list. The function | |
568 | * returns 0 on error and 1 on success. This is a generic routine to | |
569 | * all netdevices. | |
570 | */ | |
571 | static int netdev_boot_setup_add(char *name, struct ifmap *map) | |
572 | { | |
573 | struct netdev_boot_setup *s; | |
574 | int i; | |
575 | ||
576 | s = dev_boot_setup; | |
577 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { | |
578 | if (s[i].name[0] == '\0' || s[i].name[0] == ' ') { | |
579 | memset(s[i].name, 0, sizeof(s[i].name)); | |
93b3cff9 | 580 | strlcpy(s[i].name, name, IFNAMSIZ); |
1da177e4 LT |
581 | memcpy(&s[i].map, map, sizeof(s[i].map)); |
582 | break; | |
583 | } | |
584 | } | |
585 | ||
586 | return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1; | |
587 | } | |
588 | ||
589 | /** | |
590 | * netdev_boot_setup_check - check boot time settings | |
591 | * @dev: the netdevice | |
592 | * | |
593 | * Check boot time settings for the device. | |
594 | * The found settings are set for the device to be used | |
595 | * later in the device probing. | |
596 | * Returns 0 if no settings found, 1 if they are. | |
597 | */ | |
598 | int netdev_boot_setup_check(struct net_device *dev) | |
599 | { | |
600 | struct netdev_boot_setup *s = dev_boot_setup; | |
601 | int i; | |
602 | ||
603 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { | |
604 | if (s[i].name[0] != '\0' && s[i].name[0] != ' ' && | |
93b3cff9 | 605 | !strcmp(dev->name, s[i].name)) { |
1da177e4 LT |
606 | dev->irq = s[i].map.irq; |
607 | dev->base_addr = s[i].map.base_addr; | |
608 | dev->mem_start = s[i].map.mem_start; | |
609 | dev->mem_end = s[i].map.mem_end; | |
610 | return 1; | |
611 | } | |
612 | } | |
613 | return 0; | |
614 | } | |
d1b19dff | 615 | EXPORT_SYMBOL(netdev_boot_setup_check); |
1da177e4 LT |
616 | |
617 | ||
618 | /** | |
619 | * netdev_boot_base - get address from boot time settings | |
620 | * @prefix: prefix for network device | |
621 | * @unit: id for network device | |
622 | * | |
623 | * Check boot time settings for the base address of device. | |
624 | * The found settings are set for the device to be used | |
625 | * later in the device probing. | |
626 | * Returns 0 if no settings found. | |
627 | */ | |
628 | unsigned long netdev_boot_base(const char *prefix, int unit) | |
629 | { | |
630 | const struct netdev_boot_setup *s = dev_boot_setup; | |
631 | char name[IFNAMSIZ]; | |
632 | int i; | |
633 | ||
634 | sprintf(name, "%s%d", prefix, unit); | |
635 | ||
636 | /* | |
637 | * If device already registered then return base of 1 | |
638 | * to indicate not to probe for this interface | |
639 | */ | |
881d966b | 640 | if (__dev_get_by_name(&init_net, name)) |
1da177e4 LT |
641 | return 1; |
642 | ||
643 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) | |
644 | if (!strcmp(name, s[i].name)) | |
645 | return s[i].map.base_addr; | |
646 | return 0; | |
647 | } | |
648 | ||
649 | /* | |
650 | * Saves at boot time configured settings for any netdevice. | |
651 | */ | |
652 | int __init netdev_boot_setup(char *str) | |
653 | { | |
654 | int ints[5]; | |
655 | struct ifmap map; | |
656 | ||
657 | str = get_options(str, ARRAY_SIZE(ints), ints); | |
658 | if (!str || !*str) | |
659 | return 0; | |
660 | ||
661 | /* Save settings */ | |
662 | memset(&map, 0, sizeof(map)); | |
663 | if (ints[0] > 0) | |
664 | map.irq = ints[1]; | |
665 | if (ints[0] > 1) | |
666 | map.base_addr = ints[2]; | |
667 | if (ints[0] > 2) | |
668 | map.mem_start = ints[3]; | |
669 | if (ints[0] > 3) | |
670 | map.mem_end = ints[4]; | |
671 | ||
672 | /* Add new entry to the list */ | |
673 | return netdev_boot_setup_add(str, &map); | |
674 | } | |
675 | ||
676 | __setup("netdev=", netdev_boot_setup); | |
677 | ||
678 | /******************************************************************************* | |
679 | ||
680 | Device Interface Subroutines | |
681 | ||
682 | *******************************************************************************/ | |
683 | ||
684 | /** | |
685 | * __dev_get_by_name - find a device by its name | |
c4ea43c5 | 686 | * @net: the applicable net namespace |
1da177e4 LT |
687 | * @name: name to find |
688 | * | |
689 | * Find an interface by name. Must be called under RTNL semaphore | |
690 | * or @dev_base_lock. If the name is found a pointer to the device | |
691 | * is returned. If the name is not found then %NULL is returned. The | |
692 | * reference counters are not incremented so the caller must be | |
693 | * careful with locks. | |
694 | */ | |
695 | ||
881d966b | 696 | struct net_device *__dev_get_by_name(struct net *net, const char *name) |
1da177e4 LT |
697 | { |
698 | struct hlist_node *p; | |
0bd8d536 ED |
699 | struct net_device *dev; |
700 | struct hlist_head *head = dev_name_hash(net, name); | |
1da177e4 | 701 | |
0bd8d536 | 702 | hlist_for_each_entry(dev, p, head, name_hlist) |
1da177e4 LT |
703 | if (!strncmp(dev->name, name, IFNAMSIZ)) |
704 | return dev; | |
0bd8d536 | 705 | |
1da177e4 LT |
706 | return NULL; |
707 | } | |
d1b19dff | 708 | EXPORT_SYMBOL(__dev_get_by_name); |
1da177e4 | 709 | |
72c9528b ED |
710 | /** |
711 | * dev_get_by_name_rcu - find a device by its name | |
712 | * @net: the applicable net namespace | |
713 | * @name: name to find | |
714 | * | |
715 | * Find an interface by name. | |
716 | * If the name is found a pointer to the device is returned. | |
717 | * If the name is not found then %NULL is returned. | |
718 | * The reference counters are not incremented so the caller must be | |
719 | * careful with locks. The caller must hold RCU lock. | |
720 | */ | |
721 | ||
722 | struct net_device *dev_get_by_name_rcu(struct net *net, const char *name) | |
723 | { | |
724 | struct hlist_node *p; | |
725 | struct net_device *dev; | |
726 | struct hlist_head *head = dev_name_hash(net, name); | |
727 | ||
728 | hlist_for_each_entry_rcu(dev, p, head, name_hlist) | |
729 | if (!strncmp(dev->name, name, IFNAMSIZ)) | |
730 | return dev; | |
731 | ||
732 | return NULL; | |
733 | } | |
734 | EXPORT_SYMBOL(dev_get_by_name_rcu); | |
735 | ||
1da177e4 LT |
736 | /** |
737 | * dev_get_by_name - find a device by its name | |
c4ea43c5 | 738 | * @net: the applicable net namespace |
1da177e4 LT |
739 | * @name: name to find |
740 | * | |
741 | * Find an interface by name. This can be called from any | |
742 | * context and does its own locking. The returned handle has | |
743 | * the usage count incremented and the caller must use dev_put() to | |
744 | * release it when it is no longer needed. %NULL is returned if no | |
745 | * matching device is found. | |
746 | */ | |
747 | ||
881d966b | 748 | struct net_device *dev_get_by_name(struct net *net, const char *name) |
1da177e4 LT |
749 | { |
750 | struct net_device *dev; | |
751 | ||
72c9528b ED |
752 | rcu_read_lock(); |
753 | dev = dev_get_by_name_rcu(net, name); | |
1da177e4 LT |
754 | if (dev) |
755 | dev_hold(dev); | |
72c9528b | 756 | rcu_read_unlock(); |
1da177e4 LT |
757 | return dev; |
758 | } | |
d1b19dff | 759 | EXPORT_SYMBOL(dev_get_by_name); |
1da177e4 LT |
760 | |
761 | /** | |
762 | * __dev_get_by_index - find a device by its ifindex | |
c4ea43c5 | 763 | * @net: the applicable net namespace |
1da177e4 LT |
764 | * @ifindex: index of device |
765 | * | |
766 | * Search for an interface by index. Returns %NULL if the device | |
767 | * is not found or a pointer to the device. The device has not | |
768 | * had its reference counter increased so the caller must be careful | |
769 | * about locking. The caller must hold either the RTNL semaphore | |
770 | * or @dev_base_lock. | |
771 | */ | |
772 | ||
881d966b | 773 | struct net_device *__dev_get_by_index(struct net *net, int ifindex) |
1da177e4 LT |
774 | { |
775 | struct hlist_node *p; | |
0bd8d536 ED |
776 | struct net_device *dev; |
777 | struct hlist_head *head = dev_index_hash(net, ifindex); | |
1da177e4 | 778 | |
0bd8d536 | 779 | hlist_for_each_entry(dev, p, head, index_hlist) |
1da177e4 LT |
780 | if (dev->ifindex == ifindex) |
781 | return dev; | |
0bd8d536 | 782 | |
1da177e4 LT |
783 | return NULL; |
784 | } | |
d1b19dff | 785 | EXPORT_SYMBOL(__dev_get_by_index); |
1da177e4 | 786 | |
fb699dfd ED |
787 | /** |
788 | * dev_get_by_index_rcu - find a device by its ifindex | |
789 | * @net: the applicable net namespace | |
790 | * @ifindex: index of device | |
791 | * | |
792 | * Search for an interface by index. Returns %NULL if the device | |
793 | * is not found or a pointer to the device. The device has not | |
794 | * had its reference counter increased so the caller must be careful | |
795 | * about locking. The caller must hold RCU lock. | |
796 | */ | |
797 | ||
798 | struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex) | |
799 | { | |
800 | struct hlist_node *p; | |
801 | struct net_device *dev; | |
802 | struct hlist_head *head = dev_index_hash(net, ifindex); | |
803 | ||
804 | hlist_for_each_entry_rcu(dev, p, head, index_hlist) | |
805 | if (dev->ifindex == ifindex) | |
806 | return dev; | |
807 | ||
808 | return NULL; | |
809 | } | |
810 | EXPORT_SYMBOL(dev_get_by_index_rcu); | |
811 | ||
1da177e4 LT |
812 | |
813 | /** | |
814 | * dev_get_by_index - find a device by its ifindex | |
c4ea43c5 | 815 | * @net: the applicable net namespace |
1da177e4 LT |
816 | * @ifindex: index of device |
817 | * | |
818 | * Search for an interface by index. Returns NULL if the device | |
819 | * is not found or a pointer to the device. The device returned has | |
820 | * had a reference added and the pointer is safe until the user calls | |
821 | * dev_put to indicate they have finished with it. | |
822 | */ | |
823 | ||
881d966b | 824 | struct net_device *dev_get_by_index(struct net *net, int ifindex) |
1da177e4 LT |
825 | { |
826 | struct net_device *dev; | |
827 | ||
fb699dfd ED |
828 | rcu_read_lock(); |
829 | dev = dev_get_by_index_rcu(net, ifindex); | |
1da177e4 LT |
830 | if (dev) |
831 | dev_hold(dev); | |
fb699dfd | 832 | rcu_read_unlock(); |
1da177e4 LT |
833 | return dev; |
834 | } | |
d1b19dff | 835 | EXPORT_SYMBOL(dev_get_by_index); |
1da177e4 LT |
836 | |
837 | /** | |
941666c2 | 838 | * dev_getbyhwaddr_rcu - find a device by its hardware address |
c4ea43c5 | 839 | * @net: the applicable net namespace |
1da177e4 LT |
840 | * @type: media type of device |
841 | * @ha: hardware address | |
842 | * | |
843 | * Search for an interface by MAC address. Returns NULL if the device | |
c506653d ED |
844 | * is not found or a pointer to the device. |
845 | * The caller must hold RCU or RTNL. | |
941666c2 | 846 | * The returned device has not had its ref count increased |
1da177e4 LT |
847 | * and the caller must therefore be careful about locking |
848 | * | |
1da177e4 LT |
849 | */ |
850 | ||
941666c2 ED |
851 | struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type, |
852 | const char *ha) | |
1da177e4 LT |
853 | { |
854 | struct net_device *dev; | |
855 | ||
941666c2 | 856 | for_each_netdev_rcu(net, dev) |
1da177e4 LT |
857 | if (dev->type == type && |
858 | !memcmp(dev->dev_addr, ha, dev->addr_len)) | |
7562f876 PE |
859 | return dev; |
860 | ||
861 | return NULL; | |
1da177e4 | 862 | } |
941666c2 | 863 | EXPORT_SYMBOL(dev_getbyhwaddr_rcu); |
cf309e3f | 864 | |
881d966b | 865 | struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type) |
1da177e4 LT |
866 | { |
867 | struct net_device *dev; | |
868 | ||
4e9cac2b | 869 | ASSERT_RTNL(); |
881d966b | 870 | for_each_netdev(net, dev) |
4e9cac2b | 871 | if (dev->type == type) |
7562f876 PE |
872 | return dev; |
873 | ||
874 | return NULL; | |
4e9cac2b | 875 | } |
4e9cac2b PM |
876 | EXPORT_SYMBOL(__dev_getfirstbyhwtype); |
877 | ||
881d966b | 878 | struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type) |
4e9cac2b | 879 | { |
99fe3c39 | 880 | struct net_device *dev, *ret = NULL; |
4e9cac2b | 881 | |
99fe3c39 ED |
882 | rcu_read_lock(); |
883 | for_each_netdev_rcu(net, dev) | |
884 | if (dev->type == type) { | |
885 | dev_hold(dev); | |
886 | ret = dev; | |
887 | break; | |
888 | } | |
889 | rcu_read_unlock(); | |
890 | return ret; | |
1da177e4 | 891 | } |
1da177e4 LT |
892 | EXPORT_SYMBOL(dev_getfirstbyhwtype); |
893 | ||
894 | /** | |
bb69ae04 | 895 | * dev_get_by_flags_rcu - find any device with given flags |
c4ea43c5 | 896 | * @net: the applicable net namespace |
1da177e4 LT |
897 | * @if_flags: IFF_* values |
898 | * @mask: bitmask of bits in if_flags to check | |
899 | * | |
900 | * Search for any interface with the given flags. Returns NULL if a device | |
bb69ae04 ED |
901 | * is not found or a pointer to the device. Must be called inside |
902 | * rcu_read_lock(), and result refcount is unchanged. | |
1da177e4 LT |
903 | */ |
904 | ||
bb69ae04 | 905 | struct net_device *dev_get_by_flags_rcu(struct net *net, unsigned short if_flags, |
d1b19dff | 906 | unsigned short mask) |
1da177e4 | 907 | { |
7562f876 | 908 | struct net_device *dev, *ret; |
1da177e4 | 909 | |
7562f876 | 910 | ret = NULL; |
c6d14c84 | 911 | for_each_netdev_rcu(net, dev) { |
1da177e4 | 912 | if (((dev->flags ^ if_flags) & mask) == 0) { |
7562f876 | 913 | ret = dev; |
1da177e4 LT |
914 | break; |
915 | } | |
916 | } | |
7562f876 | 917 | return ret; |
1da177e4 | 918 | } |
bb69ae04 | 919 | EXPORT_SYMBOL(dev_get_by_flags_rcu); |
1da177e4 LT |
920 | |
921 | /** | |
922 | * dev_valid_name - check if name is okay for network device | |
923 | * @name: name string | |
924 | * | |
925 | * Network device names need to be valid file names to | |
c7fa9d18 DM |
926 | * to allow sysfs to work. We also disallow any kind of |
927 | * whitespace. | |
1da177e4 | 928 | */ |
95f050bf | 929 | bool dev_valid_name(const char *name) |
1da177e4 | 930 | { |
c7fa9d18 | 931 | if (*name == '\0') |
95f050bf | 932 | return false; |
b6fe17d6 | 933 | if (strlen(name) >= IFNAMSIZ) |
95f050bf | 934 | return false; |
c7fa9d18 | 935 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
95f050bf | 936 | return false; |
c7fa9d18 DM |
937 | |
938 | while (*name) { | |
939 | if (*name == '/' || isspace(*name)) | |
95f050bf | 940 | return false; |
c7fa9d18 DM |
941 | name++; |
942 | } | |
95f050bf | 943 | return true; |
1da177e4 | 944 | } |
d1b19dff | 945 | EXPORT_SYMBOL(dev_valid_name); |
1da177e4 LT |
946 | |
947 | /** | |
b267b179 EB |
948 | * __dev_alloc_name - allocate a name for a device |
949 | * @net: network namespace to allocate the device name in | |
1da177e4 | 950 | * @name: name format string |
b267b179 | 951 | * @buf: scratch buffer and result name string |
1da177e4 LT |
952 | * |
953 | * Passed a format string - eg "lt%d" it will try and find a suitable | |
3041a069 SH |
954 | * id. It scans list of devices to build up a free map, then chooses |
955 | * the first empty slot. The caller must hold the dev_base or rtnl lock | |
956 | * while allocating the name and adding the device in order to avoid | |
957 | * duplicates. | |
958 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). | |
959 | * Returns the number of the unit assigned or a negative errno code. | |
1da177e4 LT |
960 | */ |
961 | ||
b267b179 | 962 | static int __dev_alloc_name(struct net *net, const char *name, char *buf) |
1da177e4 LT |
963 | { |
964 | int i = 0; | |
1da177e4 LT |
965 | const char *p; |
966 | const int max_netdevices = 8*PAGE_SIZE; | |
cfcabdcc | 967 | unsigned long *inuse; |
1da177e4 LT |
968 | struct net_device *d; |
969 | ||
970 | p = strnchr(name, IFNAMSIZ-1, '%'); | |
971 | if (p) { | |
972 | /* | |
973 | * Verify the string as this thing may have come from | |
974 | * the user. There must be either one "%d" and no other "%" | |
975 | * characters. | |
976 | */ | |
977 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
978 | return -EINVAL; | |
979 | ||
980 | /* Use one page as a bit array of possible slots */ | |
cfcabdcc | 981 | inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC); |
1da177e4 LT |
982 | if (!inuse) |
983 | return -ENOMEM; | |
984 | ||
881d966b | 985 | for_each_netdev(net, d) { |
1da177e4 LT |
986 | if (!sscanf(d->name, name, &i)) |
987 | continue; | |
988 | if (i < 0 || i >= max_netdevices) | |
989 | continue; | |
990 | ||
991 | /* avoid cases where sscanf is not exact inverse of printf */ | |
b267b179 | 992 | snprintf(buf, IFNAMSIZ, name, i); |
1da177e4 LT |
993 | if (!strncmp(buf, d->name, IFNAMSIZ)) |
994 | set_bit(i, inuse); | |
995 | } | |
996 | ||
997 | i = find_first_zero_bit(inuse, max_netdevices); | |
998 | free_page((unsigned long) inuse); | |
999 | } | |
1000 | ||
d9031024 OP |
1001 | if (buf != name) |
1002 | snprintf(buf, IFNAMSIZ, name, i); | |
b267b179 | 1003 | if (!__dev_get_by_name(net, buf)) |
1da177e4 | 1004 | return i; |
1da177e4 LT |
1005 | |
1006 | /* It is possible to run out of possible slots | |
1007 | * when the name is long and there isn't enough space left | |
1008 | * for the digits, or if all bits are used. | |
1009 | */ | |
1010 | return -ENFILE; | |
1011 | } | |
1012 | ||
b267b179 EB |
1013 | /** |
1014 | * dev_alloc_name - allocate a name for a device | |
1015 | * @dev: device | |
1016 | * @name: name format string | |
1017 | * | |
1018 | * Passed a format string - eg "lt%d" it will try and find a suitable | |
1019 | * id. It scans list of devices to build up a free map, then chooses | |
1020 | * the first empty slot. The caller must hold the dev_base or rtnl lock | |
1021 | * while allocating the name and adding the device in order to avoid | |
1022 | * duplicates. | |
1023 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). | |
1024 | * Returns the number of the unit assigned or a negative errno code. | |
1025 | */ | |
1026 | ||
1027 | int dev_alloc_name(struct net_device *dev, const char *name) | |
1028 | { | |
1029 | char buf[IFNAMSIZ]; | |
1030 | struct net *net; | |
1031 | int ret; | |
1032 | ||
c346dca1 YH |
1033 | BUG_ON(!dev_net(dev)); |
1034 | net = dev_net(dev); | |
b267b179 EB |
1035 | ret = __dev_alloc_name(net, name, buf); |
1036 | if (ret >= 0) | |
1037 | strlcpy(dev->name, buf, IFNAMSIZ); | |
1038 | return ret; | |
1039 | } | |
d1b19dff | 1040 | EXPORT_SYMBOL(dev_alloc_name); |
b267b179 | 1041 | |
828de4f6 G |
1042 | static int dev_alloc_name_ns(struct net *net, |
1043 | struct net_device *dev, | |
1044 | const char *name) | |
d9031024 | 1045 | { |
828de4f6 G |
1046 | char buf[IFNAMSIZ]; |
1047 | int ret; | |
8ce6cebc | 1048 | |
828de4f6 G |
1049 | ret = __dev_alloc_name(net, name, buf); |
1050 | if (ret >= 0) | |
1051 | strlcpy(dev->name, buf, IFNAMSIZ); | |
1052 | return ret; | |
1053 | } | |
1054 | ||
1055 | static int dev_get_valid_name(struct net *net, | |
1056 | struct net_device *dev, | |
1057 | const char *name) | |
1058 | { | |
1059 | BUG_ON(!net); | |
8ce6cebc | 1060 | |
d9031024 OP |
1061 | if (!dev_valid_name(name)) |
1062 | return -EINVAL; | |
1063 | ||
1c5cae81 | 1064 | if (strchr(name, '%')) |
828de4f6 | 1065 | return dev_alloc_name_ns(net, dev, name); |
d9031024 OP |
1066 | else if (__dev_get_by_name(net, name)) |
1067 | return -EEXIST; | |
8ce6cebc DL |
1068 | else if (dev->name != name) |
1069 | strlcpy(dev->name, name, IFNAMSIZ); | |
d9031024 OP |
1070 | |
1071 | return 0; | |
1072 | } | |
1da177e4 LT |
1073 | |
1074 | /** | |
1075 | * dev_change_name - change name of a device | |
1076 | * @dev: device | |
1077 | * @newname: name (or format string) must be at least IFNAMSIZ | |
1078 | * | |
1079 | * Change name of a device, can pass format strings "eth%d". | |
1080 | * for wildcarding. | |
1081 | */ | |
cf04a4c7 | 1082 | int dev_change_name(struct net_device *dev, const char *newname) |
1da177e4 | 1083 | { |
fcc5a03a | 1084 | char oldname[IFNAMSIZ]; |
1da177e4 | 1085 | int err = 0; |
fcc5a03a | 1086 | int ret; |
881d966b | 1087 | struct net *net; |
1da177e4 LT |
1088 | |
1089 | ASSERT_RTNL(); | |
c346dca1 | 1090 | BUG_ON(!dev_net(dev)); |
1da177e4 | 1091 | |
c346dca1 | 1092 | net = dev_net(dev); |
1da177e4 LT |
1093 | if (dev->flags & IFF_UP) |
1094 | return -EBUSY; | |
1095 | ||
30e6c9fa | 1096 | write_seqcount_begin(&devnet_rename_seq); |
c91f6df2 BH |
1097 | |
1098 | if (strncmp(newname, dev->name, IFNAMSIZ) == 0) { | |
30e6c9fa | 1099 | write_seqcount_end(&devnet_rename_seq); |
c8d90dca | 1100 | return 0; |
c91f6df2 | 1101 | } |
c8d90dca | 1102 | |
fcc5a03a HX |
1103 | memcpy(oldname, dev->name, IFNAMSIZ); |
1104 | ||
828de4f6 | 1105 | err = dev_get_valid_name(net, dev, newname); |
c91f6df2 | 1106 | if (err < 0) { |
30e6c9fa | 1107 | write_seqcount_end(&devnet_rename_seq); |
d9031024 | 1108 | return err; |
c91f6df2 | 1109 | } |
1da177e4 | 1110 | |
fcc5a03a | 1111 | rollback: |
a1b3f594 EB |
1112 | ret = device_rename(&dev->dev, dev->name); |
1113 | if (ret) { | |
1114 | memcpy(dev->name, oldname, IFNAMSIZ); | |
30e6c9fa | 1115 | write_seqcount_end(&devnet_rename_seq); |
a1b3f594 | 1116 | return ret; |
dcc99773 | 1117 | } |
7f988eab | 1118 | |
30e6c9fa | 1119 | write_seqcount_end(&devnet_rename_seq); |
c91f6df2 | 1120 | |
7f988eab | 1121 | write_lock_bh(&dev_base_lock); |
372b2312 | 1122 | hlist_del_rcu(&dev->name_hlist); |
72c9528b ED |
1123 | write_unlock_bh(&dev_base_lock); |
1124 | ||
1125 | synchronize_rcu(); | |
1126 | ||
1127 | write_lock_bh(&dev_base_lock); | |
1128 | hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name)); | |
7f988eab HX |
1129 | write_unlock_bh(&dev_base_lock); |
1130 | ||
056925ab | 1131 | ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev); |
fcc5a03a HX |
1132 | ret = notifier_to_errno(ret); |
1133 | ||
1134 | if (ret) { | |
91e9c07b ED |
1135 | /* err >= 0 after dev_alloc_name() or stores the first errno */ |
1136 | if (err >= 0) { | |
fcc5a03a | 1137 | err = ret; |
30e6c9fa | 1138 | write_seqcount_begin(&devnet_rename_seq); |
fcc5a03a HX |
1139 | memcpy(dev->name, oldname, IFNAMSIZ); |
1140 | goto rollback; | |
91e9c07b | 1141 | } else { |
7b6cd1ce | 1142 | pr_err("%s: name change rollback failed: %d\n", |
91e9c07b | 1143 | dev->name, ret); |
fcc5a03a HX |
1144 | } |
1145 | } | |
1da177e4 LT |
1146 | |
1147 | return err; | |
1148 | } | |
1149 | ||
0b815a1a SH |
1150 | /** |
1151 | * dev_set_alias - change ifalias of a device | |
1152 | * @dev: device | |
1153 | * @alias: name up to IFALIASZ | |
f0db275a | 1154 | * @len: limit of bytes to copy from info |
0b815a1a SH |
1155 | * |
1156 | * Set ifalias for a device, | |
1157 | */ | |
1158 | int dev_set_alias(struct net_device *dev, const char *alias, size_t len) | |
1159 | { | |
7364e445 AK |
1160 | char *new_ifalias; |
1161 | ||
0b815a1a SH |
1162 | ASSERT_RTNL(); |
1163 | ||
1164 | if (len >= IFALIASZ) | |
1165 | return -EINVAL; | |
1166 | ||
96ca4a2c | 1167 | if (!len) { |
388dfc2d SK |
1168 | kfree(dev->ifalias); |
1169 | dev->ifalias = NULL; | |
96ca4a2c OH |
1170 | return 0; |
1171 | } | |
1172 | ||
7364e445 AK |
1173 | new_ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL); |
1174 | if (!new_ifalias) | |
0b815a1a | 1175 | return -ENOMEM; |
7364e445 | 1176 | dev->ifalias = new_ifalias; |
0b815a1a SH |
1177 | |
1178 | strlcpy(dev->ifalias, alias, len+1); | |
1179 | return len; | |
1180 | } | |
1181 | ||
1182 | ||
d8a33ac4 | 1183 | /** |
3041a069 | 1184 | * netdev_features_change - device changes features |
d8a33ac4 SH |
1185 | * @dev: device to cause notification |
1186 | * | |
1187 | * Called to indicate a device has changed features. | |
1188 | */ | |
1189 | void netdev_features_change(struct net_device *dev) | |
1190 | { | |
056925ab | 1191 | call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev); |
d8a33ac4 SH |
1192 | } |
1193 | EXPORT_SYMBOL(netdev_features_change); | |
1194 | ||
1da177e4 LT |
1195 | /** |
1196 | * netdev_state_change - device changes state | |
1197 | * @dev: device to cause notification | |
1198 | * | |
1199 | * Called to indicate a device has changed state. This function calls | |
1200 | * the notifier chains for netdev_chain and sends a NEWLINK message | |
1201 | * to the routing socket. | |
1202 | */ | |
1203 | void netdev_state_change(struct net_device *dev) | |
1204 | { | |
1205 | if (dev->flags & IFF_UP) { | |
056925ab | 1206 | call_netdevice_notifiers(NETDEV_CHANGE, dev); |
1da177e4 LT |
1207 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0); |
1208 | } | |
1209 | } | |
d1b19dff | 1210 | EXPORT_SYMBOL(netdev_state_change); |
1da177e4 | 1211 | |
ee89bab1 AW |
1212 | /** |
1213 | * netdev_notify_peers - notify network peers about existence of @dev | |
1214 | * @dev: network device | |
1215 | * | |
1216 | * Generate traffic such that interested network peers are aware of | |
1217 | * @dev, such as by generating a gratuitous ARP. This may be used when | |
1218 | * a device wants to inform the rest of the network about some sort of | |
1219 | * reconfiguration such as a failover event or virtual machine | |
1220 | * migration. | |
1221 | */ | |
1222 | void netdev_notify_peers(struct net_device *dev) | |
c1da4ac7 | 1223 | { |
ee89bab1 AW |
1224 | rtnl_lock(); |
1225 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev); | |
1226 | rtnl_unlock(); | |
c1da4ac7 | 1227 | } |
ee89bab1 | 1228 | EXPORT_SYMBOL(netdev_notify_peers); |
c1da4ac7 | 1229 | |
1da177e4 LT |
1230 | /** |
1231 | * dev_load - load a network module | |
c4ea43c5 | 1232 | * @net: the applicable net namespace |
1da177e4 LT |
1233 | * @name: name of interface |
1234 | * | |
1235 | * If a network interface is not present and the process has suitable | |
1236 | * privileges this function loads the module. If module loading is not | |
1237 | * available in this kernel then it becomes a nop. | |
1238 | */ | |
1239 | ||
881d966b | 1240 | void dev_load(struct net *net, const char *name) |
1da177e4 | 1241 | { |
4ec93edb | 1242 | struct net_device *dev; |
8909c9ad | 1243 | int no_module; |
1da177e4 | 1244 | |
72c9528b ED |
1245 | rcu_read_lock(); |
1246 | dev = dev_get_by_name_rcu(net, name); | |
1247 | rcu_read_unlock(); | |
1da177e4 | 1248 | |
8909c9ad VK |
1249 | no_module = !dev; |
1250 | if (no_module && capable(CAP_NET_ADMIN)) | |
1251 | no_module = request_module("netdev-%s", name); | |
1252 | if (no_module && capable(CAP_SYS_MODULE)) { | |
1253 | if (!request_module("%s", name)) | |
7cecb523 VL |
1254 | pr_warn("Loading kernel module for a network device with CAP_SYS_MODULE (deprecated). Use CAP_NET_ADMIN and alias netdev-%s instead.\n", |
1255 | name); | |
8909c9ad | 1256 | } |
1da177e4 | 1257 | } |
d1b19dff | 1258 | EXPORT_SYMBOL(dev_load); |
1da177e4 | 1259 | |
bd380811 | 1260 | static int __dev_open(struct net_device *dev) |
1da177e4 | 1261 | { |
d314774c | 1262 | const struct net_device_ops *ops = dev->netdev_ops; |
3b8bcfd5 | 1263 | int ret; |
1da177e4 | 1264 | |
e46b66bc BH |
1265 | ASSERT_RTNL(); |
1266 | ||
1da177e4 LT |
1267 | if (!netif_device_present(dev)) |
1268 | return -ENODEV; | |
1269 | ||
3b8bcfd5 JB |
1270 | ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev); |
1271 | ret = notifier_to_errno(ret); | |
1272 | if (ret) | |
1273 | return ret; | |
1274 | ||
1da177e4 | 1275 | set_bit(__LINK_STATE_START, &dev->state); |
bada339b | 1276 | |
d314774c SH |
1277 | if (ops->ndo_validate_addr) |
1278 | ret = ops->ndo_validate_addr(dev); | |
bada339b | 1279 | |
d314774c SH |
1280 | if (!ret && ops->ndo_open) |
1281 | ret = ops->ndo_open(dev); | |
1da177e4 | 1282 | |
bada339b JG |
1283 | if (ret) |
1284 | clear_bit(__LINK_STATE_START, &dev->state); | |
1285 | else { | |
1da177e4 | 1286 | dev->flags |= IFF_UP; |
b4bd07c2 | 1287 | net_dmaengine_get(); |
4417da66 | 1288 | dev_set_rx_mode(dev); |
1da177e4 | 1289 | dev_activate(dev); |
7bf23575 | 1290 | add_device_randomness(dev->dev_addr, dev->addr_len); |
1da177e4 | 1291 | } |
bada339b | 1292 | |
1da177e4 LT |
1293 | return ret; |
1294 | } | |
1295 | ||
1296 | /** | |
bd380811 PM |
1297 | * dev_open - prepare an interface for use. |
1298 | * @dev: device to open | |
1da177e4 | 1299 | * |
bd380811 PM |
1300 | * Takes a device from down to up state. The device's private open |
1301 | * function is invoked and then the multicast lists are loaded. Finally | |
1302 | * the device is moved into the up state and a %NETDEV_UP message is | |
1303 | * sent to the netdev notifier chain. | |
1304 | * | |
1305 | * Calling this function on an active interface is a nop. On a failure | |
1306 | * a negative errno code is returned. | |
1da177e4 | 1307 | */ |
bd380811 PM |
1308 | int dev_open(struct net_device *dev) |
1309 | { | |
1310 | int ret; | |
1311 | ||
bd380811 PM |
1312 | if (dev->flags & IFF_UP) |
1313 | return 0; | |
1314 | ||
bd380811 PM |
1315 | ret = __dev_open(dev); |
1316 | if (ret < 0) | |
1317 | return ret; | |
1318 | ||
bd380811 PM |
1319 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); |
1320 | call_netdevice_notifiers(NETDEV_UP, dev); | |
1321 | ||
1322 | return ret; | |
1323 | } | |
1324 | EXPORT_SYMBOL(dev_open); | |
1325 | ||
44345724 | 1326 | static int __dev_close_many(struct list_head *head) |
1da177e4 | 1327 | { |
44345724 | 1328 | struct net_device *dev; |
e46b66bc | 1329 | |
bd380811 | 1330 | ASSERT_RTNL(); |
9d5010db DM |
1331 | might_sleep(); |
1332 | ||
44345724 | 1333 | list_for_each_entry(dev, head, unreg_list) { |
44345724 | 1334 | call_netdevice_notifiers(NETDEV_GOING_DOWN, dev); |
1da177e4 | 1335 | |
44345724 | 1336 | clear_bit(__LINK_STATE_START, &dev->state); |
1da177e4 | 1337 | |
44345724 OP |
1338 | /* Synchronize to scheduled poll. We cannot touch poll list, it |
1339 | * can be even on different cpu. So just clear netif_running(). | |
1340 | * | |
1341 | * dev->stop() will invoke napi_disable() on all of it's | |
1342 | * napi_struct instances on this device. | |
1343 | */ | |
1344 | smp_mb__after_clear_bit(); /* Commit netif_running(). */ | |
1345 | } | |
1da177e4 | 1346 | |
44345724 | 1347 | dev_deactivate_many(head); |
d8b2a4d2 | 1348 | |
44345724 OP |
1349 | list_for_each_entry(dev, head, unreg_list) { |
1350 | const struct net_device_ops *ops = dev->netdev_ops; | |
1da177e4 | 1351 | |
44345724 OP |
1352 | /* |
1353 | * Call the device specific close. This cannot fail. | |
1354 | * Only if device is UP | |
1355 | * | |
1356 | * We allow it to be called even after a DETACH hot-plug | |
1357 | * event. | |
1358 | */ | |
1359 | if (ops->ndo_stop) | |
1360 | ops->ndo_stop(dev); | |
1361 | ||
44345724 | 1362 | dev->flags &= ~IFF_UP; |
44345724 OP |
1363 | net_dmaengine_put(); |
1364 | } | |
1365 | ||
1366 | return 0; | |
1367 | } | |
1368 | ||
1369 | static int __dev_close(struct net_device *dev) | |
1370 | { | |
f87e6f47 | 1371 | int retval; |
44345724 OP |
1372 | LIST_HEAD(single); |
1373 | ||
1374 | list_add(&dev->unreg_list, &single); | |
f87e6f47 LT |
1375 | retval = __dev_close_many(&single); |
1376 | list_del(&single); | |
1377 | return retval; | |
44345724 OP |
1378 | } |
1379 | ||
3fbd8758 | 1380 | static int dev_close_many(struct list_head *head) |
44345724 OP |
1381 | { |
1382 | struct net_device *dev, *tmp; | |
1383 | LIST_HEAD(tmp_list); | |
1da177e4 | 1384 | |
44345724 OP |
1385 | list_for_each_entry_safe(dev, tmp, head, unreg_list) |
1386 | if (!(dev->flags & IFF_UP)) | |
1387 | list_move(&dev->unreg_list, &tmp_list); | |
1388 | ||
1389 | __dev_close_many(head); | |
1da177e4 | 1390 | |
44345724 OP |
1391 | list_for_each_entry(dev, head, unreg_list) { |
1392 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING); | |
1393 | call_netdevice_notifiers(NETDEV_DOWN, dev); | |
1394 | } | |
bd380811 | 1395 | |
44345724 OP |
1396 | /* rollback_registered_many needs the complete original list */ |
1397 | list_splice(&tmp_list, head); | |
bd380811 PM |
1398 | return 0; |
1399 | } | |
1400 | ||
1401 | /** | |
1402 | * dev_close - shutdown an interface. | |
1403 | * @dev: device to shutdown | |
1404 | * | |
1405 | * This function moves an active device into down state. A | |
1406 | * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device | |
1407 | * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier | |
1408 | * chain. | |
1409 | */ | |
1410 | int dev_close(struct net_device *dev) | |
1411 | { | |
e14a5993 ED |
1412 | if (dev->flags & IFF_UP) { |
1413 | LIST_HEAD(single); | |
1da177e4 | 1414 | |
e14a5993 ED |
1415 | list_add(&dev->unreg_list, &single); |
1416 | dev_close_many(&single); | |
1417 | list_del(&single); | |
1418 | } | |
1da177e4 LT |
1419 | return 0; |
1420 | } | |
d1b19dff | 1421 | EXPORT_SYMBOL(dev_close); |
1da177e4 LT |
1422 | |
1423 | ||
0187bdfb BH |
1424 | /** |
1425 | * dev_disable_lro - disable Large Receive Offload on a device | |
1426 | * @dev: device | |
1427 | * | |
1428 | * Disable Large Receive Offload (LRO) on a net device. Must be | |
1429 | * called under RTNL. This is needed if received packets may be | |
1430 | * forwarded to another interface. | |
1431 | */ | |
1432 | void dev_disable_lro(struct net_device *dev) | |
1433 | { | |
f11970e3 NH |
1434 | /* |
1435 | * If we're trying to disable lro on a vlan device | |
1436 | * use the underlying physical device instead | |
1437 | */ | |
1438 | if (is_vlan_dev(dev)) | |
1439 | dev = vlan_dev_real_dev(dev); | |
1440 | ||
bc5787c6 MM |
1441 | dev->wanted_features &= ~NETIF_F_LRO; |
1442 | netdev_update_features(dev); | |
27660515 | 1443 | |
22d5969f MM |
1444 | if (unlikely(dev->features & NETIF_F_LRO)) |
1445 | netdev_WARN(dev, "failed to disable LRO!\n"); | |
0187bdfb BH |
1446 | } |
1447 | EXPORT_SYMBOL(dev_disable_lro); | |
1448 | ||
1449 | ||
881d966b EB |
1450 | static int dev_boot_phase = 1; |
1451 | ||
1da177e4 LT |
1452 | /** |
1453 | * register_netdevice_notifier - register a network notifier block | |
1454 | * @nb: notifier | |
1455 | * | |
1456 | * Register a notifier to be called when network device events occur. | |
1457 | * The notifier passed is linked into the kernel structures and must | |
1458 | * not be reused until it has been unregistered. A negative errno code | |
1459 | * is returned on a failure. | |
1460 | * | |
1461 | * When registered all registration and up events are replayed | |
4ec93edb | 1462 | * to the new notifier to allow device to have a race free |
1da177e4 LT |
1463 | * view of the network device list. |
1464 | */ | |
1465 | ||
1466 | int register_netdevice_notifier(struct notifier_block *nb) | |
1467 | { | |
1468 | struct net_device *dev; | |
fcc5a03a | 1469 | struct net_device *last; |
881d966b | 1470 | struct net *net; |
1da177e4 LT |
1471 | int err; |
1472 | ||
1473 | rtnl_lock(); | |
f07d5b94 | 1474 | err = raw_notifier_chain_register(&netdev_chain, nb); |
fcc5a03a HX |
1475 | if (err) |
1476 | goto unlock; | |
881d966b EB |
1477 | if (dev_boot_phase) |
1478 | goto unlock; | |
1479 | for_each_net(net) { | |
1480 | for_each_netdev(net, dev) { | |
1481 | err = nb->notifier_call(nb, NETDEV_REGISTER, dev); | |
1482 | err = notifier_to_errno(err); | |
1483 | if (err) | |
1484 | goto rollback; | |
1485 | ||
1486 | if (!(dev->flags & IFF_UP)) | |
1487 | continue; | |
1da177e4 | 1488 | |
881d966b EB |
1489 | nb->notifier_call(nb, NETDEV_UP, dev); |
1490 | } | |
1da177e4 | 1491 | } |
fcc5a03a HX |
1492 | |
1493 | unlock: | |
1da177e4 LT |
1494 | rtnl_unlock(); |
1495 | return err; | |
fcc5a03a HX |
1496 | |
1497 | rollback: | |
1498 | last = dev; | |
881d966b EB |
1499 | for_each_net(net) { |
1500 | for_each_netdev(net, dev) { | |
1501 | if (dev == last) | |
8f891489 | 1502 | goto outroll; |
fcc5a03a | 1503 | |
881d966b EB |
1504 | if (dev->flags & IFF_UP) { |
1505 | nb->notifier_call(nb, NETDEV_GOING_DOWN, dev); | |
1506 | nb->notifier_call(nb, NETDEV_DOWN, dev); | |
1507 | } | |
1508 | nb->notifier_call(nb, NETDEV_UNREGISTER, dev); | |
fcc5a03a | 1509 | } |
fcc5a03a | 1510 | } |
c67625a1 | 1511 | |
8f891489 | 1512 | outroll: |
c67625a1 | 1513 | raw_notifier_chain_unregister(&netdev_chain, nb); |
fcc5a03a | 1514 | goto unlock; |
1da177e4 | 1515 | } |
d1b19dff | 1516 | EXPORT_SYMBOL(register_netdevice_notifier); |
1da177e4 LT |
1517 | |
1518 | /** | |
1519 | * unregister_netdevice_notifier - unregister a network notifier block | |
1520 | * @nb: notifier | |
1521 | * | |
1522 | * Unregister a notifier previously registered by | |
1523 | * register_netdevice_notifier(). The notifier is unlinked into the | |
1524 | * kernel structures and may then be reused. A negative errno code | |
1525 | * is returned on a failure. | |
7d3d43da EB |
1526 | * |
1527 | * After unregistering unregister and down device events are synthesized | |
1528 | * for all devices on the device list to the removed notifier to remove | |
1529 | * the need for special case cleanup code. | |
1da177e4 LT |
1530 | */ |
1531 | ||
1532 | int unregister_netdevice_notifier(struct notifier_block *nb) | |
1533 | { | |
7d3d43da EB |
1534 | struct net_device *dev; |
1535 | struct net *net; | |
9f514950 HX |
1536 | int err; |
1537 | ||
1538 | rtnl_lock(); | |
f07d5b94 | 1539 | err = raw_notifier_chain_unregister(&netdev_chain, nb); |
7d3d43da EB |
1540 | if (err) |
1541 | goto unlock; | |
1542 | ||
1543 | for_each_net(net) { | |
1544 | for_each_netdev(net, dev) { | |
1545 | if (dev->flags & IFF_UP) { | |
1546 | nb->notifier_call(nb, NETDEV_GOING_DOWN, dev); | |
1547 | nb->notifier_call(nb, NETDEV_DOWN, dev); | |
1548 | } | |
1549 | nb->notifier_call(nb, NETDEV_UNREGISTER, dev); | |
7d3d43da EB |
1550 | } |
1551 | } | |
1552 | unlock: | |
9f514950 HX |
1553 | rtnl_unlock(); |
1554 | return err; | |
1da177e4 | 1555 | } |
d1b19dff | 1556 | EXPORT_SYMBOL(unregister_netdevice_notifier); |
1da177e4 LT |
1557 | |
1558 | /** | |
1559 | * call_netdevice_notifiers - call all network notifier blocks | |
1560 | * @val: value passed unmodified to notifier function | |
c4ea43c5 | 1561 | * @dev: net_device pointer passed unmodified to notifier function |
1da177e4 LT |
1562 | * |
1563 | * Call all network notifier blocks. Parameters and return value | |
f07d5b94 | 1564 | * are as for raw_notifier_call_chain(). |
1da177e4 LT |
1565 | */ |
1566 | ||
ad7379d4 | 1567 | int call_netdevice_notifiers(unsigned long val, struct net_device *dev) |
1da177e4 | 1568 | { |
ab930471 | 1569 | ASSERT_RTNL(); |
ad7379d4 | 1570 | return raw_notifier_call_chain(&netdev_chain, val, dev); |
1da177e4 | 1571 | } |
edf947f1 | 1572 | EXPORT_SYMBOL(call_netdevice_notifiers); |
1da177e4 | 1573 | |
c5905afb | 1574 | static struct static_key netstamp_needed __read_mostly; |
b90e5794 | 1575 | #ifdef HAVE_JUMP_LABEL |
c5905afb | 1576 | /* We are not allowed to call static_key_slow_dec() from irq context |
b90e5794 | 1577 | * If net_disable_timestamp() is called from irq context, defer the |
c5905afb | 1578 | * static_key_slow_dec() calls. |
b90e5794 ED |
1579 | */ |
1580 | static atomic_t netstamp_needed_deferred; | |
1581 | #endif | |
1da177e4 LT |
1582 | |
1583 | void net_enable_timestamp(void) | |
1584 | { | |
b90e5794 ED |
1585 | #ifdef HAVE_JUMP_LABEL |
1586 | int deferred = atomic_xchg(&netstamp_needed_deferred, 0); | |
1587 | ||
1588 | if (deferred) { | |
1589 | while (--deferred) | |
c5905afb | 1590 | static_key_slow_dec(&netstamp_needed); |
b90e5794 ED |
1591 | return; |
1592 | } | |
1593 | #endif | |
1594 | WARN_ON(in_interrupt()); | |
c5905afb | 1595 | static_key_slow_inc(&netstamp_needed); |
1da177e4 | 1596 | } |
d1b19dff | 1597 | EXPORT_SYMBOL(net_enable_timestamp); |
1da177e4 LT |
1598 | |
1599 | void net_disable_timestamp(void) | |
1600 | { | |
b90e5794 ED |
1601 | #ifdef HAVE_JUMP_LABEL |
1602 | if (in_interrupt()) { | |
1603 | atomic_inc(&netstamp_needed_deferred); | |
1604 | return; | |
1605 | } | |
1606 | #endif | |
c5905afb | 1607 | static_key_slow_dec(&netstamp_needed); |
1da177e4 | 1608 | } |
d1b19dff | 1609 | EXPORT_SYMBOL(net_disable_timestamp); |
1da177e4 | 1610 | |
3b098e2d | 1611 | static inline void net_timestamp_set(struct sk_buff *skb) |
1da177e4 | 1612 | { |
588f0330 | 1613 | skb->tstamp.tv64 = 0; |
c5905afb | 1614 | if (static_key_false(&netstamp_needed)) |
a61bbcf2 | 1615 | __net_timestamp(skb); |
1da177e4 LT |
1616 | } |
1617 | ||
588f0330 | 1618 | #define net_timestamp_check(COND, SKB) \ |
c5905afb | 1619 | if (static_key_false(&netstamp_needed)) { \ |
588f0330 ED |
1620 | if ((COND) && !(SKB)->tstamp.tv64) \ |
1621 | __net_timestamp(SKB); \ | |
1622 | } \ | |
3b098e2d | 1623 | |
4dc360c5 RC |
1624 | static int net_hwtstamp_validate(struct ifreq *ifr) |
1625 | { | |
1626 | struct hwtstamp_config cfg; | |
1627 | enum hwtstamp_tx_types tx_type; | |
1628 | enum hwtstamp_rx_filters rx_filter; | |
1629 | int tx_type_valid = 0; | |
1630 | int rx_filter_valid = 0; | |
1631 | ||
1632 | if (copy_from_user(&cfg, ifr->ifr_data, sizeof(cfg))) | |
1633 | return -EFAULT; | |
1634 | ||
1635 | if (cfg.flags) /* reserved for future extensions */ | |
1636 | return -EINVAL; | |
1637 | ||
1638 | tx_type = cfg.tx_type; | |
1639 | rx_filter = cfg.rx_filter; | |
1640 | ||
1641 | switch (tx_type) { | |
1642 | case HWTSTAMP_TX_OFF: | |
1643 | case HWTSTAMP_TX_ON: | |
1644 | case HWTSTAMP_TX_ONESTEP_SYNC: | |
1645 | tx_type_valid = 1; | |
1646 | break; | |
1647 | } | |
1648 | ||
1649 | switch (rx_filter) { | |
1650 | case HWTSTAMP_FILTER_NONE: | |
1651 | case HWTSTAMP_FILTER_ALL: | |
1652 | case HWTSTAMP_FILTER_SOME: | |
1653 | case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: | |
1654 | case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: | |
1655 | case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: | |
1656 | case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: | |
1657 | case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: | |
1658 | case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: | |
1659 | case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: | |
1660 | case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: | |
1661 | case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: | |
1662 | case HWTSTAMP_FILTER_PTP_V2_EVENT: | |
1663 | case HWTSTAMP_FILTER_PTP_V2_SYNC: | |
1664 | case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: | |
1665 | rx_filter_valid = 1; | |
1666 | break; | |
1667 | } | |
1668 | ||
1669 | if (!tx_type_valid || !rx_filter_valid) | |
1670 | return -ERANGE; | |
1671 | ||
1672 | return 0; | |
1673 | } | |
1674 | ||
79b569f0 DL |
1675 | static inline bool is_skb_forwardable(struct net_device *dev, |
1676 | struct sk_buff *skb) | |
1677 | { | |
1678 | unsigned int len; | |
1679 | ||
1680 | if (!(dev->flags & IFF_UP)) | |
1681 | return false; | |
1682 | ||
1683 | len = dev->mtu + dev->hard_header_len + VLAN_HLEN; | |
1684 | if (skb->len <= len) | |
1685 | return true; | |
1686 | ||
1687 | /* if TSO is enabled, we don't care about the length as the packet | |
1688 | * could be forwarded without being segmented before | |
1689 | */ | |
1690 | if (skb_is_gso(skb)) | |
1691 | return true; | |
1692 | ||
1693 | return false; | |
1694 | } | |
1695 | ||
44540960 AB |
1696 | /** |
1697 | * dev_forward_skb - loopback an skb to another netif | |
1698 | * | |
1699 | * @dev: destination network device | |
1700 | * @skb: buffer to forward | |
1701 | * | |
1702 | * return values: | |
1703 | * NET_RX_SUCCESS (no congestion) | |
6ec82562 | 1704 | * NET_RX_DROP (packet was dropped, but freed) |
44540960 AB |
1705 | * |
1706 | * dev_forward_skb can be used for injecting an skb from the | |
1707 | * start_xmit function of one device into the receive queue | |
1708 | * of another device. | |
1709 | * | |
1710 | * The receiving device may be in another namespace, so | |
1711 | * we have to clear all information in the skb that could | |
1712 | * impact namespace isolation. | |
1713 | */ | |
1714 | int dev_forward_skb(struct net_device *dev, struct sk_buff *skb) | |
1715 | { | |
48c83012 MT |
1716 | if (skb_shinfo(skb)->tx_flags & SKBTX_DEV_ZEROCOPY) { |
1717 | if (skb_copy_ubufs(skb, GFP_ATOMIC)) { | |
1718 | atomic_long_inc(&dev->rx_dropped); | |
1719 | kfree_skb(skb); | |
1720 | return NET_RX_DROP; | |
1721 | } | |
1722 | } | |
1723 | ||
44540960 | 1724 | skb_orphan(skb); |
c736eefa | 1725 | nf_reset(skb); |
44540960 | 1726 | |
79b569f0 | 1727 | if (unlikely(!is_skb_forwardable(dev, skb))) { |
caf586e5 | 1728 | atomic_long_inc(&dev->rx_dropped); |
6ec82562 | 1729 | kfree_skb(skb); |
44540960 | 1730 | return NET_RX_DROP; |
6ec82562 | 1731 | } |
3b9785c6 | 1732 | skb->skb_iif = 0; |
59b9997b DM |
1733 | skb->dev = dev; |
1734 | skb_dst_drop(skb); | |
44540960 AB |
1735 | skb->tstamp.tv64 = 0; |
1736 | skb->pkt_type = PACKET_HOST; | |
1737 | skb->protocol = eth_type_trans(skb, dev); | |
59b9997b DM |
1738 | skb->mark = 0; |
1739 | secpath_reset(skb); | |
1740 | nf_reset(skb); | |
44540960 AB |
1741 | return netif_rx(skb); |
1742 | } | |
1743 | EXPORT_SYMBOL_GPL(dev_forward_skb); | |
1744 | ||
71d9dec2 CG |
1745 | static inline int deliver_skb(struct sk_buff *skb, |
1746 | struct packet_type *pt_prev, | |
1747 | struct net_device *orig_dev) | |
1748 | { | |
1080e512 MT |
1749 | if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) |
1750 | return -ENOMEM; | |
71d9dec2 CG |
1751 | atomic_inc(&skb->users); |
1752 | return pt_prev->func(skb, skb->dev, pt_prev, orig_dev); | |
1753 | } | |
1754 | ||
c0de08d0 EL |
1755 | static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb) |
1756 | { | |
a3d744e9 | 1757 | if (!ptype->af_packet_priv || !skb->sk) |
c0de08d0 EL |
1758 | return false; |
1759 | ||
1760 | if (ptype->id_match) | |
1761 | return ptype->id_match(ptype, skb->sk); | |
1762 | else if ((struct sock *)ptype->af_packet_priv == skb->sk) | |
1763 | return true; | |
1764 | ||
1765 | return false; | |
1766 | } | |
1767 | ||
1da177e4 LT |
1768 | /* |
1769 | * Support routine. Sends outgoing frames to any network | |
1770 | * taps currently in use. | |
1771 | */ | |
1772 | ||
f6a78bfc | 1773 | static void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev) |
1da177e4 LT |
1774 | { |
1775 | struct packet_type *ptype; | |
71d9dec2 CG |
1776 | struct sk_buff *skb2 = NULL; |
1777 | struct packet_type *pt_prev = NULL; | |
a61bbcf2 | 1778 | |
1da177e4 LT |
1779 | rcu_read_lock(); |
1780 | list_for_each_entry_rcu(ptype, &ptype_all, list) { | |
1781 | /* Never send packets back to the socket | |
1782 | * they originated from - MvS (miquels@drinkel.ow.org) | |
1783 | */ | |
1784 | if ((ptype->dev == dev || !ptype->dev) && | |
c0de08d0 | 1785 | (!skb_loop_sk(ptype, skb))) { |
71d9dec2 CG |
1786 | if (pt_prev) { |
1787 | deliver_skb(skb2, pt_prev, skb->dev); | |
1788 | pt_prev = ptype; | |
1789 | continue; | |
1790 | } | |
1791 | ||
1792 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
1da177e4 LT |
1793 | if (!skb2) |
1794 | break; | |
1795 | ||
70978182 ED |
1796 | net_timestamp_set(skb2); |
1797 | ||
1da177e4 LT |
1798 | /* skb->nh should be correctly |
1799 | set by sender, so that the second statement is | |
1800 | just protection against buggy protocols. | |
1801 | */ | |
459a98ed | 1802 | skb_reset_mac_header(skb2); |
1da177e4 | 1803 | |
d56f90a7 | 1804 | if (skb_network_header(skb2) < skb2->data || |
27a884dc | 1805 | skb2->network_header > skb2->tail) { |
e87cc472 JP |
1806 | net_crit_ratelimited("protocol %04x is buggy, dev %s\n", |
1807 | ntohs(skb2->protocol), | |
1808 | dev->name); | |
c1d2bbe1 | 1809 | skb_reset_network_header(skb2); |
1da177e4 LT |
1810 | } |
1811 | ||
b0e380b1 | 1812 | skb2->transport_header = skb2->network_header; |
1da177e4 | 1813 | skb2->pkt_type = PACKET_OUTGOING; |
71d9dec2 | 1814 | pt_prev = ptype; |
1da177e4 LT |
1815 | } |
1816 | } | |
71d9dec2 CG |
1817 | if (pt_prev) |
1818 | pt_prev->func(skb2, skb->dev, pt_prev, skb->dev); | |
1da177e4 LT |
1819 | rcu_read_unlock(); |
1820 | } | |
1821 | ||
2c53040f BH |
1822 | /** |
1823 | * netif_setup_tc - Handle tc mappings on real_num_tx_queues change | |
4f57c087 JF |
1824 | * @dev: Network device |
1825 | * @txq: number of queues available | |
1826 | * | |
1827 | * If real_num_tx_queues is changed the tc mappings may no longer be | |
1828 | * valid. To resolve this verify the tc mapping remains valid and if | |
1829 | * not NULL the mapping. With no priorities mapping to this | |
1830 | * offset/count pair it will no longer be used. In the worst case TC0 | |
1831 | * is invalid nothing can be done so disable priority mappings. If is | |
1832 | * expected that drivers will fix this mapping if they can before | |
1833 | * calling netif_set_real_num_tx_queues. | |
1834 | */ | |
bb134d22 | 1835 | static void netif_setup_tc(struct net_device *dev, unsigned int txq) |
4f57c087 JF |
1836 | { |
1837 | int i; | |
1838 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; | |
1839 | ||
1840 | /* If TC0 is invalidated disable TC mapping */ | |
1841 | if (tc->offset + tc->count > txq) { | |
7b6cd1ce | 1842 | pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n"); |
4f57c087 JF |
1843 | dev->num_tc = 0; |
1844 | return; | |
1845 | } | |
1846 | ||
1847 | /* Invalidated prio to tc mappings set to TC0 */ | |
1848 | for (i = 1; i < TC_BITMASK + 1; i++) { | |
1849 | int q = netdev_get_prio_tc_map(dev, i); | |
1850 | ||
1851 | tc = &dev->tc_to_txq[q]; | |
1852 | if (tc->offset + tc->count > txq) { | |
7b6cd1ce JP |
1853 | pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n", |
1854 | i, q); | |
4f57c087 JF |
1855 | netdev_set_prio_tc_map(dev, i, 0); |
1856 | } | |
1857 | } | |
1858 | } | |
1859 | ||
537c00de AD |
1860 | #ifdef CONFIG_XPS |
1861 | static DEFINE_MUTEX(xps_map_mutex); | |
1862 | #define xmap_dereference(P) \ | |
1863 | rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex)) | |
1864 | ||
10cdc3f3 AD |
1865 | static struct xps_map *remove_xps_queue(struct xps_dev_maps *dev_maps, |
1866 | int cpu, u16 index) | |
537c00de | 1867 | { |
10cdc3f3 AD |
1868 | struct xps_map *map = NULL; |
1869 | int pos; | |
537c00de | 1870 | |
10cdc3f3 AD |
1871 | if (dev_maps) |
1872 | map = xmap_dereference(dev_maps->cpu_map[cpu]); | |
537c00de | 1873 | |
10cdc3f3 AD |
1874 | for (pos = 0; map && pos < map->len; pos++) { |
1875 | if (map->queues[pos] == index) { | |
537c00de AD |
1876 | if (map->len > 1) { |
1877 | map->queues[pos] = map->queues[--map->len]; | |
1878 | } else { | |
10cdc3f3 | 1879 | RCU_INIT_POINTER(dev_maps->cpu_map[cpu], NULL); |
537c00de AD |
1880 | kfree_rcu(map, rcu); |
1881 | map = NULL; | |
1882 | } | |
10cdc3f3 | 1883 | break; |
537c00de | 1884 | } |
537c00de AD |
1885 | } |
1886 | ||
10cdc3f3 AD |
1887 | return map; |
1888 | } | |
1889 | ||
024e9679 | 1890 | static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index) |
10cdc3f3 AD |
1891 | { |
1892 | struct xps_dev_maps *dev_maps; | |
024e9679 | 1893 | int cpu, i; |
10cdc3f3 AD |
1894 | bool active = false; |
1895 | ||
1896 | mutex_lock(&xps_map_mutex); | |
1897 | dev_maps = xmap_dereference(dev->xps_maps); | |
1898 | ||
1899 | if (!dev_maps) | |
1900 | goto out_no_maps; | |
1901 | ||
1902 | for_each_possible_cpu(cpu) { | |
024e9679 AD |
1903 | for (i = index; i < dev->num_tx_queues; i++) { |
1904 | if (!remove_xps_queue(dev_maps, cpu, i)) | |
1905 | break; | |
1906 | } | |
1907 | if (i == dev->num_tx_queues) | |
10cdc3f3 AD |
1908 | active = true; |
1909 | } | |
1910 | ||
1911 | if (!active) { | |
537c00de AD |
1912 | RCU_INIT_POINTER(dev->xps_maps, NULL); |
1913 | kfree_rcu(dev_maps, rcu); | |
1914 | } | |
1915 | ||
024e9679 AD |
1916 | for (i = index; i < dev->num_tx_queues; i++) |
1917 | netdev_queue_numa_node_write(netdev_get_tx_queue(dev, i), | |
1918 | NUMA_NO_NODE); | |
1919 | ||
537c00de AD |
1920 | out_no_maps: |
1921 | mutex_unlock(&xps_map_mutex); | |
1922 | } | |
1923 | ||
01c5f864 AD |
1924 | static struct xps_map *expand_xps_map(struct xps_map *map, |
1925 | int cpu, u16 index) | |
1926 | { | |
1927 | struct xps_map *new_map; | |
1928 | int alloc_len = XPS_MIN_MAP_ALLOC; | |
1929 | int i, pos; | |
1930 | ||
1931 | for (pos = 0; map && pos < map->len; pos++) { | |
1932 | if (map->queues[pos] != index) | |
1933 | continue; | |
1934 | return map; | |
1935 | } | |
1936 | ||
1937 | /* Need to add queue to this CPU's existing map */ | |
1938 | if (map) { | |
1939 | if (pos < map->alloc_len) | |
1940 | return map; | |
1941 | ||
1942 | alloc_len = map->alloc_len * 2; | |
1943 | } | |
1944 | ||
1945 | /* Need to allocate new map to store queue on this CPU's map */ | |
1946 | new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL, | |
1947 | cpu_to_node(cpu)); | |
1948 | if (!new_map) | |
1949 | return NULL; | |
1950 | ||
1951 | for (i = 0; i < pos; i++) | |
1952 | new_map->queues[i] = map->queues[i]; | |
1953 | new_map->alloc_len = alloc_len; | |
1954 | new_map->len = pos; | |
1955 | ||
1956 | return new_map; | |
1957 | } | |
1958 | ||
537c00de AD |
1959 | int netif_set_xps_queue(struct net_device *dev, struct cpumask *mask, u16 index) |
1960 | { | |
01c5f864 | 1961 | struct xps_dev_maps *dev_maps, *new_dev_maps = NULL; |
537c00de | 1962 | struct xps_map *map, *new_map; |
537c00de | 1963 | int maps_sz = max_t(unsigned int, XPS_DEV_MAPS_SIZE, L1_CACHE_BYTES); |
01c5f864 AD |
1964 | int cpu, numa_node_id = -2; |
1965 | bool active = false; | |
537c00de AD |
1966 | |
1967 | mutex_lock(&xps_map_mutex); | |
1968 | ||
1969 | dev_maps = xmap_dereference(dev->xps_maps); | |
1970 | ||
01c5f864 AD |
1971 | /* allocate memory for queue storage */ |
1972 | for_each_online_cpu(cpu) { | |
1973 | if (!cpumask_test_cpu(cpu, mask)) | |
1974 | continue; | |
1975 | ||
1976 | if (!new_dev_maps) | |
1977 | new_dev_maps = kzalloc(maps_sz, GFP_KERNEL); | |
1978 | if (!new_dev_maps) | |
1979 | return -ENOMEM; | |
1980 | ||
1981 | map = dev_maps ? xmap_dereference(dev_maps->cpu_map[cpu]) : | |
1982 | NULL; | |
1983 | ||
1984 | map = expand_xps_map(map, cpu, index); | |
1985 | if (!map) | |
1986 | goto error; | |
1987 | ||
1988 | RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], map); | |
1989 | } | |
1990 | ||
1991 | if (!new_dev_maps) | |
1992 | goto out_no_new_maps; | |
1993 | ||
537c00de | 1994 | for_each_possible_cpu(cpu) { |
01c5f864 AD |
1995 | if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu)) { |
1996 | /* add queue to CPU maps */ | |
1997 | int pos = 0; | |
1998 | ||
1999 | map = xmap_dereference(new_dev_maps->cpu_map[cpu]); | |
2000 | while ((pos < map->len) && (map->queues[pos] != index)) | |
2001 | pos++; | |
2002 | ||
2003 | if (pos == map->len) | |
2004 | map->queues[map->len++] = index; | |
537c00de | 2005 | #ifdef CONFIG_NUMA |
537c00de AD |
2006 | if (numa_node_id == -2) |
2007 | numa_node_id = cpu_to_node(cpu); | |
2008 | else if (numa_node_id != cpu_to_node(cpu)) | |
2009 | numa_node_id = -1; | |
537c00de | 2010 | #endif |
01c5f864 AD |
2011 | } else if (dev_maps) { |
2012 | /* fill in the new device map from the old device map */ | |
2013 | map = xmap_dereference(dev_maps->cpu_map[cpu]); | |
2014 | RCU_INIT_POINTER(new_dev_maps->cpu_map[cpu], map); | |
537c00de | 2015 | } |
01c5f864 | 2016 | |
537c00de AD |
2017 | } |
2018 | ||
01c5f864 AD |
2019 | rcu_assign_pointer(dev->xps_maps, new_dev_maps); |
2020 | ||
537c00de | 2021 | /* Cleanup old maps */ |
01c5f864 AD |
2022 | if (dev_maps) { |
2023 | for_each_possible_cpu(cpu) { | |
2024 | new_map = xmap_dereference(new_dev_maps->cpu_map[cpu]); | |
2025 | map = xmap_dereference(dev_maps->cpu_map[cpu]); | |
2026 | if (map && map != new_map) | |
2027 | kfree_rcu(map, rcu); | |
2028 | } | |
537c00de | 2029 | |
01c5f864 | 2030 | kfree_rcu(dev_maps, rcu); |
537c00de AD |
2031 | } |
2032 | ||
01c5f864 AD |
2033 | dev_maps = new_dev_maps; |
2034 | active = true; | |
537c00de | 2035 | |
01c5f864 AD |
2036 | out_no_new_maps: |
2037 | /* update Tx queue numa node */ | |
537c00de AD |
2038 | netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index), |
2039 | (numa_node_id >= 0) ? numa_node_id : | |
2040 | NUMA_NO_NODE); | |
2041 | ||
01c5f864 AD |
2042 | if (!dev_maps) |
2043 | goto out_no_maps; | |
2044 | ||
2045 | /* removes queue from unused CPUs */ | |
2046 | for_each_possible_cpu(cpu) { | |
2047 | if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu)) | |
2048 | continue; | |
2049 | ||
2050 | if (remove_xps_queue(dev_maps, cpu, index)) | |
2051 | active = true; | |
2052 | } | |
2053 | ||
2054 | /* free map if not active */ | |
2055 | if (!active) { | |
2056 | RCU_INIT_POINTER(dev->xps_maps, NULL); | |
2057 | kfree_rcu(dev_maps, rcu); | |
2058 | } | |
2059 | ||
2060 | out_no_maps: | |
537c00de AD |
2061 | mutex_unlock(&xps_map_mutex); |
2062 | ||
2063 | return 0; | |
2064 | error: | |
01c5f864 AD |
2065 | /* remove any maps that we added */ |
2066 | for_each_possible_cpu(cpu) { | |
2067 | new_map = xmap_dereference(new_dev_maps->cpu_map[cpu]); | |
2068 | map = dev_maps ? xmap_dereference(dev_maps->cpu_map[cpu]) : | |
2069 | NULL; | |
2070 | if (new_map && new_map != map) | |
2071 | kfree(new_map); | |
2072 | } | |
2073 | ||
537c00de AD |
2074 | mutex_unlock(&xps_map_mutex); |
2075 | ||
537c00de AD |
2076 | kfree(new_dev_maps); |
2077 | return -ENOMEM; | |
2078 | } | |
2079 | EXPORT_SYMBOL(netif_set_xps_queue); | |
2080 | ||
2081 | #endif | |
f0796d5c JF |
2082 | /* |
2083 | * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues | |
2084 | * greater then real_num_tx_queues stale skbs on the qdisc must be flushed. | |
2085 | */ | |
e6484930 | 2086 | int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq) |
f0796d5c | 2087 | { |
1d24eb48 TH |
2088 | int rc; |
2089 | ||
e6484930 TH |
2090 | if (txq < 1 || txq > dev->num_tx_queues) |
2091 | return -EINVAL; | |
f0796d5c | 2092 | |
5c56580b BH |
2093 | if (dev->reg_state == NETREG_REGISTERED || |
2094 | dev->reg_state == NETREG_UNREGISTERING) { | |
e6484930 TH |
2095 | ASSERT_RTNL(); |
2096 | ||
1d24eb48 TH |
2097 | rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues, |
2098 | txq); | |
bf264145 TH |
2099 | if (rc) |
2100 | return rc; | |
2101 | ||
4f57c087 JF |
2102 | if (dev->num_tc) |
2103 | netif_setup_tc(dev, txq); | |
2104 | ||
024e9679 | 2105 | if (txq < dev->real_num_tx_queues) { |
e6484930 | 2106 | qdisc_reset_all_tx_gt(dev, txq); |
024e9679 AD |
2107 | #ifdef CONFIG_XPS |
2108 | netif_reset_xps_queues_gt(dev, txq); | |
2109 | #endif | |
2110 | } | |
f0796d5c | 2111 | } |
e6484930 TH |
2112 | |
2113 | dev->real_num_tx_queues = txq; | |
2114 | return 0; | |
f0796d5c JF |
2115 | } |
2116 | EXPORT_SYMBOL(netif_set_real_num_tx_queues); | |
56079431 | 2117 | |
62fe0b40 BH |
2118 | #ifdef CONFIG_RPS |
2119 | /** | |
2120 | * netif_set_real_num_rx_queues - set actual number of RX queues used | |
2121 | * @dev: Network device | |
2122 | * @rxq: Actual number of RX queues | |
2123 | * | |
2124 | * This must be called either with the rtnl_lock held or before | |
2125 | * registration of the net device. Returns 0 on success, or a | |
4e7f7951 BH |
2126 | * negative error code. If called before registration, it always |
2127 | * succeeds. | |
62fe0b40 BH |
2128 | */ |
2129 | int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq) | |
2130 | { | |
2131 | int rc; | |
2132 | ||
bd25fa7b TH |
2133 | if (rxq < 1 || rxq > dev->num_rx_queues) |
2134 | return -EINVAL; | |
2135 | ||
62fe0b40 BH |
2136 | if (dev->reg_state == NETREG_REGISTERED) { |
2137 | ASSERT_RTNL(); | |
2138 | ||
62fe0b40 BH |
2139 | rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues, |
2140 | rxq); | |
2141 | if (rc) | |
2142 | return rc; | |
62fe0b40 BH |
2143 | } |
2144 | ||
2145 | dev->real_num_rx_queues = rxq; | |
2146 | return 0; | |
2147 | } | |
2148 | EXPORT_SYMBOL(netif_set_real_num_rx_queues); | |
2149 | #endif | |
2150 | ||
2c53040f BH |
2151 | /** |
2152 | * netif_get_num_default_rss_queues - default number of RSS queues | |
16917b87 YM |
2153 | * |
2154 | * This routine should set an upper limit on the number of RSS queues | |
2155 | * used by default by multiqueue devices. | |
2156 | */ | |
a55b138b | 2157 | int netif_get_num_default_rss_queues(void) |
16917b87 YM |
2158 | { |
2159 | return min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus()); | |
2160 | } | |
2161 | EXPORT_SYMBOL(netif_get_num_default_rss_queues); | |
2162 | ||
def82a1d | 2163 | static inline void __netif_reschedule(struct Qdisc *q) |
56079431 | 2164 | { |
def82a1d JP |
2165 | struct softnet_data *sd; |
2166 | unsigned long flags; | |
56079431 | 2167 | |
def82a1d JP |
2168 | local_irq_save(flags); |
2169 | sd = &__get_cpu_var(softnet_data); | |
a9cbd588 CG |
2170 | q->next_sched = NULL; |
2171 | *sd->output_queue_tailp = q; | |
2172 | sd->output_queue_tailp = &q->next_sched; | |
def82a1d JP |
2173 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
2174 | local_irq_restore(flags); | |
2175 | } | |
2176 | ||
2177 | void __netif_schedule(struct Qdisc *q) | |
2178 | { | |
2179 | if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state)) | |
2180 | __netif_reschedule(q); | |
56079431 DV |
2181 | } |
2182 | EXPORT_SYMBOL(__netif_schedule); | |
2183 | ||
bea3348e | 2184 | void dev_kfree_skb_irq(struct sk_buff *skb) |
56079431 | 2185 | { |
3578b0c8 | 2186 | if (atomic_dec_and_test(&skb->users)) { |
bea3348e SH |
2187 | struct softnet_data *sd; |
2188 | unsigned long flags; | |
56079431 | 2189 | |
bea3348e SH |
2190 | local_irq_save(flags); |
2191 | sd = &__get_cpu_var(softnet_data); | |
2192 | skb->next = sd->completion_queue; | |
2193 | sd->completion_queue = skb; | |
2194 | raise_softirq_irqoff(NET_TX_SOFTIRQ); | |
2195 | local_irq_restore(flags); | |
2196 | } | |
56079431 | 2197 | } |
bea3348e | 2198 | EXPORT_SYMBOL(dev_kfree_skb_irq); |
56079431 DV |
2199 | |
2200 | void dev_kfree_skb_any(struct sk_buff *skb) | |
2201 | { | |
2202 | if (in_irq() || irqs_disabled()) | |
2203 | dev_kfree_skb_irq(skb); | |
2204 | else | |
2205 | dev_kfree_skb(skb); | |
2206 | } | |
2207 | EXPORT_SYMBOL(dev_kfree_skb_any); | |
2208 | ||
2209 | ||
bea3348e SH |
2210 | /** |
2211 | * netif_device_detach - mark device as removed | |
2212 | * @dev: network device | |
2213 | * | |
2214 | * Mark device as removed from system and therefore no longer available. | |
2215 | */ | |
56079431 DV |
2216 | void netif_device_detach(struct net_device *dev) |
2217 | { | |
2218 | if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) && | |
2219 | netif_running(dev)) { | |
d543103a | 2220 | netif_tx_stop_all_queues(dev); |
56079431 DV |
2221 | } |
2222 | } | |
2223 | EXPORT_SYMBOL(netif_device_detach); | |
2224 | ||
bea3348e SH |
2225 | /** |
2226 | * netif_device_attach - mark device as attached | |
2227 | * @dev: network device | |
2228 | * | |
2229 | * Mark device as attached from system and restart if needed. | |
2230 | */ | |
56079431 DV |
2231 | void netif_device_attach(struct net_device *dev) |
2232 | { | |
2233 | if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) && | |
2234 | netif_running(dev)) { | |
d543103a | 2235 | netif_tx_wake_all_queues(dev); |
4ec93edb | 2236 | __netdev_watchdog_up(dev); |
56079431 DV |
2237 | } |
2238 | } | |
2239 | EXPORT_SYMBOL(netif_device_attach); | |
2240 | ||
36c92474 BH |
2241 | static void skb_warn_bad_offload(const struct sk_buff *skb) |
2242 | { | |
65e9d2fa | 2243 | static const netdev_features_t null_features = 0; |
36c92474 BH |
2244 | struct net_device *dev = skb->dev; |
2245 | const char *driver = ""; | |
2246 | ||
2247 | if (dev && dev->dev.parent) | |
2248 | driver = dev_driver_string(dev->dev.parent); | |
2249 | ||
2250 | WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d " | |
2251 | "gso_type=%d ip_summed=%d\n", | |
65e9d2fa MM |
2252 | driver, dev ? &dev->features : &null_features, |
2253 | skb->sk ? &skb->sk->sk_route_caps : &null_features, | |
36c92474 BH |
2254 | skb->len, skb->data_len, skb_shinfo(skb)->gso_size, |
2255 | skb_shinfo(skb)->gso_type, skb->ip_summed); | |
2256 | } | |
2257 | ||
1da177e4 LT |
2258 | /* |
2259 | * Invalidate hardware checksum when packet is to be mangled, and | |
2260 | * complete checksum manually on outgoing path. | |
2261 | */ | |
84fa7933 | 2262 | int skb_checksum_help(struct sk_buff *skb) |
1da177e4 | 2263 | { |
d3bc23e7 | 2264 | __wsum csum; |
663ead3b | 2265 | int ret = 0, offset; |
1da177e4 | 2266 | |
84fa7933 | 2267 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
a430a43d HX |
2268 | goto out_set_summed; |
2269 | ||
2270 | if (unlikely(skb_shinfo(skb)->gso_size)) { | |
36c92474 BH |
2271 | skb_warn_bad_offload(skb); |
2272 | return -EINVAL; | |
1da177e4 LT |
2273 | } |
2274 | ||
55508d60 | 2275 | offset = skb_checksum_start_offset(skb); |
a030847e HX |
2276 | BUG_ON(offset >= skb_headlen(skb)); |
2277 | csum = skb_checksum(skb, offset, skb->len - offset, 0); | |
2278 | ||
2279 | offset += skb->csum_offset; | |
2280 | BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb)); | |
2281 | ||
2282 | if (skb_cloned(skb) && | |
2283 | !skb_clone_writable(skb, offset + sizeof(__sum16))) { | |
1da177e4 LT |
2284 | ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC); |
2285 | if (ret) | |
2286 | goto out; | |
2287 | } | |
2288 | ||
a030847e | 2289 | *(__sum16 *)(skb->data + offset) = csum_fold(csum); |
a430a43d | 2290 | out_set_summed: |
1da177e4 | 2291 | skb->ip_summed = CHECKSUM_NONE; |
4ec93edb | 2292 | out: |
1da177e4 LT |
2293 | return ret; |
2294 | } | |
d1b19dff | 2295 | EXPORT_SYMBOL(skb_checksum_help); |
1da177e4 | 2296 | |
f6a78bfc HX |
2297 | /** |
2298 | * skb_gso_segment - Perform segmentation on skb. | |
2299 | * @skb: buffer to segment | |
576a30eb | 2300 | * @features: features for the output path (see dev->features) |
f6a78bfc HX |
2301 | * |
2302 | * This function segments the given skb and returns a list of segments. | |
576a30eb HX |
2303 | * |
2304 | * It may return NULL if the skb requires no segmentation. This is | |
2305 | * only possible when GSO is used for verifying header integrity. | |
f6a78bfc | 2306 | */ |
c8f44aff MM |
2307 | struct sk_buff *skb_gso_segment(struct sk_buff *skb, |
2308 | netdev_features_t features) | |
f6a78bfc HX |
2309 | { |
2310 | struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT); | |
22061d80 | 2311 | struct packet_offload *ptype; |
252e3346 | 2312 | __be16 type = skb->protocol; |
c8d5bcd1 | 2313 | int vlan_depth = ETH_HLEN; |
a430a43d | 2314 | int err; |
f6a78bfc | 2315 | |
c8d5bcd1 JG |
2316 | while (type == htons(ETH_P_8021Q)) { |
2317 | struct vlan_hdr *vh; | |
7b9c6090 | 2318 | |
c8d5bcd1 | 2319 | if (unlikely(!pskb_may_pull(skb, vlan_depth + VLAN_HLEN))) |
7b9c6090 JG |
2320 | return ERR_PTR(-EINVAL); |
2321 | ||
c8d5bcd1 JG |
2322 | vh = (struct vlan_hdr *)(skb->data + vlan_depth); |
2323 | type = vh->h_vlan_encapsulated_proto; | |
2324 | vlan_depth += VLAN_HLEN; | |
7b9c6090 JG |
2325 | } |
2326 | ||
459a98ed | 2327 | skb_reset_mac_header(skb); |
b0e380b1 | 2328 | skb->mac_len = skb->network_header - skb->mac_header; |
f6a78bfc HX |
2329 | __skb_pull(skb, skb->mac_len); |
2330 | ||
67fd1a73 | 2331 | if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) { |
36c92474 | 2332 | skb_warn_bad_offload(skb); |
67fd1a73 | 2333 | |
a430a43d HX |
2334 | if (skb_header_cloned(skb) && |
2335 | (err = pskb_expand_head(skb, 0, 0, GFP_ATOMIC))) | |
2336 | return ERR_PTR(err); | |
2337 | } | |
2338 | ||
f6a78bfc | 2339 | rcu_read_lock(); |
22061d80 | 2340 | list_for_each_entry_rcu(ptype, &offload_base, list) { |
f191a1d1 | 2341 | if (ptype->type == type && ptype->callbacks.gso_segment) { |
84fa7933 | 2342 | if (unlikely(skb->ip_summed != CHECKSUM_PARTIAL)) { |
f191a1d1 | 2343 | err = ptype->callbacks.gso_send_check(skb); |
a430a43d HX |
2344 | segs = ERR_PTR(err); |
2345 | if (err || skb_gso_ok(skb, features)) | |
2346 | break; | |
d56f90a7 ACM |
2347 | __skb_push(skb, (skb->data - |
2348 | skb_network_header(skb))); | |
a430a43d | 2349 | } |
f191a1d1 | 2350 | segs = ptype->callbacks.gso_segment(skb, features); |
f6a78bfc HX |
2351 | break; |
2352 | } | |
2353 | } | |
2354 | rcu_read_unlock(); | |
2355 | ||
98e399f8 | 2356 | __skb_push(skb, skb->data - skb_mac_header(skb)); |
576a30eb | 2357 | |
f6a78bfc HX |
2358 | return segs; |
2359 | } | |
f6a78bfc HX |
2360 | EXPORT_SYMBOL(skb_gso_segment); |
2361 | ||
fb286bb2 HX |
2362 | /* Take action when hardware reception checksum errors are detected. */ |
2363 | #ifdef CONFIG_BUG | |
2364 | void netdev_rx_csum_fault(struct net_device *dev) | |
2365 | { | |
2366 | if (net_ratelimit()) { | |
7b6cd1ce | 2367 | pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>"); |
fb286bb2 HX |
2368 | dump_stack(); |
2369 | } | |
2370 | } | |
2371 | EXPORT_SYMBOL(netdev_rx_csum_fault); | |
2372 | #endif | |
2373 | ||
1da177e4 LT |
2374 | /* Actually, we should eliminate this check as soon as we know, that: |
2375 | * 1. IOMMU is present and allows to map all the memory. | |
2376 | * 2. No high memory really exists on this machine. | |
2377 | */ | |
2378 | ||
9092c658 | 2379 | static int illegal_highdma(struct net_device *dev, struct sk_buff *skb) |
1da177e4 | 2380 | { |
3d3a8533 | 2381 | #ifdef CONFIG_HIGHMEM |
1da177e4 | 2382 | int i; |
5acbbd42 | 2383 | if (!(dev->features & NETIF_F_HIGHDMA)) { |
ea2ab693 IC |
2384 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
2385 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
2386 | if (PageHighMem(skb_frag_page(frag))) | |
5acbbd42 | 2387 | return 1; |
ea2ab693 | 2388 | } |
5acbbd42 | 2389 | } |
1da177e4 | 2390 | |
5acbbd42 FT |
2391 | if (PCI_DMA_BUS_IS_PHYS) { |
2392 | struct device *pdev = dev->dev.parent; | |
1da177e4 | 2393 | |
9092c658 ED |
2394 | if (!pdev) |
2395 | return 0; | |
5acbbd42 | 2396 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
ea2ab693 IC |
2397 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; |
2398 | dma_addr_t addr = page_to_phys(skb_frag_page(frag)); | |
5acbbd42 FT |
2399 | if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask) |
2400 | return 1; | |
2401 | } | |
2402 | } | |
3d3a8533 | 2403 | #endif |
1da177e4 LT |
2404 | return 0; |
2405 | } | |
1da177e4 | 2406 | |
f6a78bfc HX |
2407 | struct dev_gso_cb { |
2408 | void (*destructor)(struct sk_buff *skb); | |
2409 | }; | |
2410 | ||
2411 | #define DEV_GSO_CB(skb) ((struct dev_gso_cb *)(skb)->cb) | |
2412 | ||
2413 | static void dev_gso_skb_destructor(struct sk_buff *skb) | |
2414 | { | |
2415 | struct dev_gso_cb *cb; | |
2416 | ||
2417 | do { | |
2418 | struct sk_buff *nskb = skb->next; | |
2419 | ||
2420 | skb->next = nskb->next; | |
2421 | nskb->next = NULL; | |
2422 | kfree_skb(nskb); | |
2423 | } while (skb->next); | |
2424 | ||
2425 | cb = DEV_GSO_CB(skb); | |
2426 | if (cb->destructor) | |
2427 | cb->destructor(skb); | |
2428 | } | |
2429 | ||
2430 | /** | |
2431 | * dev_gso_segment - Perform emulated hardware segmentation on skb. | |
2432 | * @skb: buffer to segment | |
91ecb63c | 2433 | * @features: device features as applicable to this skb |
f6a78bfc HX |
2434 | * |
2435 | * This function segments the given skb and stores the list of segments | |
2436 | * in skb->next. | |
2437 | */ | |
c8f44aff | 2438 | static int dev_gso_segment(struct sk_buff *skb, netdev_features_t features) |
f6a78bfc | 2439 | { |
f6a78bfc | 2440 | struct sk_buff *segs; |
576a30eb HX |
2441 | |
2442 | segs = skb_gso_segment(skb, features); | |
2443 | ||
2444 | /* Verifying header integrity only. */ | |
2445 | if (!segs) | |
2446 | return 0; | |
f6a78bfc | 2447 | |
801678c5 | 2448 | if (IS_ERR(segs)) |
f6a78bfc HX |
2449 | return PTR_ERR(segs); |
2450 | ||
2451 | skb->next = segs; | |
2452 | DEV_GSO_CB(skb)->destructor = skb->destructor; | |
2453 | skb->destructor = dev_gso_skb_destructor; | |
2454 | ||
2455 | return 0; | |
2456 | } | |
2457 | ||
c8f44aff | 2458 | static bool can_checksum_protocol(netdev_features_t features, __be16 protocol) |
03634668 JG |
2459 | { |
2460 | return ((features & NETIF_F_GEN_CSUM) || | |
2461 | ((features & NETIF_F_V4_CSUM) && | |
2462 | protocol == htons(ETH_P_IP)) || | |
2463 | ((features & NETIF_F_V6_CSUM) && | |
2464 | protocol == htons(ETH_P_IPV6)) || | |
2465 | ((features & NETIF_F_FCOE_CRC) && | |
2466 | protocol == htons(ETH_P_FCOE))); | |
2467 | } | |
2468 | ||
c8f44aff MM |
2469 | static netdev_features_t harmonize_features(struct sk_buff *skb, |
2470 | __be16 protocol, netdev_features_t features) | |
f01a5236 | 2471 | { |
c0d680e5 EC |
2472 | if (skb->ip_summed != CHECKSUM_NONE && |
2473 | !can_checksum_protocol(features, protocol)) { | |
f01a5236 JG |
2474 | features &= ~NETIF_F_ALL_CSUM; |
2475 | features &= ~NETIF_F_SG; | |
2476 | } else if (illegal_highdma(skb->dev, skb)) { | |
2477 | features &= ~NETIF_F_SG; | |
2478 | } | |
2479 | ||
2480 | return features; | |
2481 | } | |
2482 | ||
c8f44aff | 2483 | netdev_features_t netif_skb_features(struct sk_buff *skb) |
58e998c6 JG |
2484 | { |
2485 | __be16 protocol = skb->protocol; | |
c8f44aff | 2486 | netdev_features_t features = skb->dev->features; |
58e998c6 | 2487 | |
30b678d8 BH |
2488 | if (skb_shinfo(skb)->gso_segs > skb->dev->gso_max_segs) |
2489 | features &= ~NETIF_F_GSO_MASK; | |
2490 | ||
58e998c6 JG |
2491 | if (protocol == htons(ETH_P_8021Q)) { |
2492 | struct vlan_ethhdr *veh = (struct vlan_ethhdr *)skb->data; | |
2493 | protocol = veh->h_vlan_encapsulated_proto; | |
f01a5236 JG |
2494 | } else if (!vlan_tx_tag_present(skb)) { |
2495 | return harmonize_features(skb, protocol, features); | |
2496 | } | |
58e998c6 | 2497 | |
6ee400aa | 2498 | features &= (skb->dev->vlan_features | NETIF_F_HW_VLAN_TX); |
f01a5236 JG |
2499 | |
2500 | if (protocol != htons(ETH_P_8021Q)) { | |
2501 | return harmonize_features(skb, protocol, features); | |
2502 | } else { | |
2503 | features &= NETIF_F_SG | NETIF_F_HIGHDMA | NETIF_F_FRAGLIST | | |
6ee400aa | 2504 | NETIF_F_GEN_CSUM | NETIF_F_HW_VLAN_TX; |
f01a5236 JG |
2505 | return harmonize_features(skb, protocol, features); |
2506 | } | |
58e998c6 | 2507 | } |
f01a5236 | 2508 | EXPORT_SYMBOL(netif_skb_features); |
58e998c6 | 2509 | |
6afff0ca JF |
2510 | /* |
2511 | * Returns true if either: | |
2512 | * 1. skb has frag_list and the device doesn't support FRAGLIST, or | |
d1a53dfd | 2513 | * 2. skb is fragmented and the device does not support SG. |
6afff0ca JF |
2514 | */ |
2515 | static inline int skb_needs_linearize(struct sk_buff *skb, | |
02932ce9 | 2516 | int features) |
6afff0ca | 2517 | { |
02932ce9 JG |
2518 | return skb_is_nonlinear(skb) && |
2519 | ((skb_has_frag_list(skb) && | |
2520 | !(features & NETIF_F_FRAGLIST)) || | |
e1e78db6 | 2521 | (skb_shinfo(skb)->nr_frags && |
02932ce9 | 2522 | !(features & NETIF_F_SG))); |
6afff0ca JF |
2523 | } |
2524 | ||
fd2ea0a7 DM |
2525 | int dev_hard_start_xmit(struct sk_buff *skb, struct net_device *dev, |
2526 | struct netdev_queue *txq) | |
f6a78bfc | 2527 | { |
00829823 | 2528 | const struct net_device_ops *ops = dev->netdev_ops; |
572a9d7b | 2529 | int rc = NETDEV_TX_OK; |
ec764bf0 | 2530 | unsigned int skb_len; |
00829823 | 2531 | |
f6a78bfc | 2532 | if (likely(!skb->next)) { |
c8f44aff | 2533 | netdev_features_t features; |
fc741216 | 2534 | |
93f154b5 | 2535 | /* |
25985edc | 2536 | * If device doesn't need skb->dst, release it right now while |
93f154b5 ED |
2537 | * its hot in this cpu cache |
2538 | */ | |
adf30907 ED |
2539 | if (dev->priv_flags & IFF_XMIT_DST_RELEASE) |
2540 | skb_dst_drop(skb); | |
2541 | ||
fc741216 JG |
2542 | features = netif_skb_features(skb); |
2543 | ||
7b9c6090 | 2544 | if (vlan_tx_tag_present(skb) && |
fc741216 | 2545 | !(features & NETIF_F_HW_VLAN_TX)) { |
7b9c6090 JG |
2546 | skb = __vlan_put_tag(skb, vlan_tx_tag_get(skb)); |
2547 | if (unlikely(!skb)) | |
2548 | goto out; | |
2549 | ||
2550 | skb->vlan_tci = 0; | |
2551 | } | |
2552 | ||
fc70fb64 AD |
2553 | /* If encapsulation offload request, verify we are testing |
2554 | * hardware encapsulation features instead of standard | |
2555 | * features for the netdev | |
2556 | */ | |
2557 | if (skb->encapsulation) | |
2558 | features &= dev->hw_enc_features; | |
2559 | ||
fc741216 | 2560 | if (netif_needs_gso(skb, features)) { |
91ecb63c | 2561 | if (unlikely(dev_gso_segment(skb, features))) |
9ccb8975 DM |
2562 | goto out_kfree_skb; |
2563 | if (skb->next) | |
2564 | goto gso; | |
6afff0ca | 2565 | } else { |
02932ce9 | 2566 | if (skb_needs_linearize(skb, features) && |
6afff0ca JF |
2567 | __skb_linearize(skb)) |
2568 | goto out_kfree_skb; | |
2569 | ||
2570 | /* If packet is not checksummed and device does not | |
2571 | * support checksumming for this protocol, complete | |
2572 | * checksumming here. | |
2573 | */ | |
2574 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | |
fc70fb64 AD |
2575 | if (skb->encapsulation) |
2576 | skb_set_inner_transport_header(skb, | |
2577 | skb_checksum_start_offset(skb)); | |
2578 | else | |
2579 | skb_set_transport_header(skb, | |
2580 | skb_checksum_start_offset(skb)); | |
03634668 | 2581 | if (!(features & NETIF_F_ALL_CSUM) && |
6afff0ca JF |
2582 | skb_checksum_help(skb)) |
2583 | goto out_kfree_skb; | |
2584 | } | |
9ccb8975 DM |
2585 | } |
2586 | ||
b40863c6 ED |
2587 | if (!list_empty(&ptype_all)) |
2588 | dev_queue_xmit_nit(skb, dev); | |
2589 | ||
ec764bf0 | 2590 | skb_len = skb->len; |
ac45f602 | 2591 | rc = ops->ndo_start_xmit(skb, dev); |
ec764bf0 | 2592 | trace_net_dev_xmit(skb, rc, dev, skb_len); |
ec634fe3 | 2593 | if (rc == NETDEV_TX_OK) |
08baf561 | 2594 | txq_trans_update(txq); |
ac45f602 | 2595 | return rc; |
f6a78bfc HX |
2596 | } |
2597 | ||
576a30eb | 2598 | gso: |
f6a78bfc HX |
2599 | do { |
2600 | struct sk_buff *nskb = skb->next; | |
f6a78bfc HX |
2601 | |
2602 | skb->next = nskb->next; | |
2603 | nskb->next = NULL; | |
068a2de5 KK |
2604 | |
2605 | /* | |
25985edc | 2606 | * If device doesn't need nskb->dst, release it right now while |
068a2de5 KK |
2607 | * its hot in this cpu cache |
2608 | */ | |
2609 | if (dev->priv_flags & IFF_XMIT_DST_RELEASE) | |
2610 | skb_dst_drop(nskb); | |
2611 | ||
b40863c6 ED |
2612 | if (!list_empty(&ptype_all)) |
2613 | dev_queue_xmit_nit(nskb, dev); | |
2614 | ||
ec764bf0 | 2615 | skb_len = nskb->len; |
00829823 | 2616 | rc = ops->ndo_start_xmit(nskb, dev); |
ec764bf0 | 2617 | trace_net_dev_xmit(nskb, rc, dev, skb_len); |
ec634fe3 | 2618 | if (unlikely(rc != NETDEV_TX_OK)) { |
572a9d7b PM |
2619 | if (rc & ~NETDEV_TX_MASK) |
2620 | goto out_kfree_gso_skb; | |
f54d9e8d | 2621 | nskb->next = skb->next; |
f6a78bfc HX |
2622 | skb->next = nskb; |
2623 | return rc; | |
2624 | } | |
08baf561 | 2625 | txq_trans_update(txq); |
73466498 | 2626 | if (unlikely(netif_xmit_stopped(txq) && skb->next)) |
f54d9e8d | 2627 | return NETDEV_TX_BUSY; |
f6a78bfc | 2628 | } while (skb->next); |
4ec93edb | 2629 | |
572a9d7b PM |
2630 | out_kfree_gso_skb: |
2631 | if (likely(skb->next == NULL)) | |
2632 | skb->destructor = DEV_GSO_CB(skb)->destructor; | |
f6a78bfc HX |
2633 | out_kfree_skb: |
2634 | kfree_skb(skb); | |
7b9c6090 | 2635 | out: |
572a9d7b | 2636 | return rc; |
f6a78bfc HX |
2637 | } |
2638 | ||
0a9627f2 | 2639 | static u32 hashrnd __read_mostly; |
b6b2fed1 | 2640 | |
a3d22a68 VZ |
2641 | /* |
2642 | * Returns a Tx hash based on the given packet descriptor a Tx queues' number | |
2643 | * to be used as a distribution range. | |
2644 | */ | |
2645 | u16 __skb_tx_hash(const struct net_device *dev, const struct sk_buff *skb, | |
2646 | unsigned int num_tx_queues) | |
8f0f2223 | 2647 | { |
7019298a | 2648 | u32 hash; |
4f57c087 JF |
2649 | u16 qoffset = 0; |
2650 | u16 qcount = num_tx_queues; | |
b6b2fed1 | 2651 | |
513de11b DM |
2652 | if (skb_rx_queue_recorded(skb)) { |
2653 | hash = skb_get_rx_queue(skb); | |
a3d22a68 VZ |
2654 | while (unlikely(hash >= num_tx_queues)) |
2655 | hash -= num_tx_queues; | |
513de11b DM |
2656 | return hash; |
2657 | } | |
ec581f6a | 2658 | |
4f57c087 JF |
2659 | if (dev->num_tc) { |
2660 | u8 tc = netdev_get_prio_tc_map(dev, skb->priority); | |
2661 | qoffset = dev->tc_to_txq[tc].offset; | |
2662 | qcount = dev->tc_to_txq[tc].count; | |
2663 | } | |
2664 | ||
ec581f6a | 2665 | if (skb->sk && skb->sk->sk_hash) |
7019298a | 2666 | hash = skb->sk->sk_hash; |
ec581f6a | 2667 | else |
62b1a8ab | 2668 | hash = (__force u16) skb->protocol; |
0a9627f2 | 2669 | hash = jhash_1word(hash, hashrnd); |
b6b2fed1 | 2670 | |
4f57c087 | 2671 | return (u16) (((u64) hash * qcount) >> 32) + qoffset; |
8f0f2223 | 2672 | } |
a3d22a68 | 2673 | EXPORT_SYMBOL(__skb_tx_hash); |
8f0f2223 | 2674 | |
ed04642f ED |
2675 | static inline u16 dev_cap_txqueue(struct net_device *dev, u16 queue_index) |
2676 | { | |
2677 | if (unlikely(queue_index >= dev->real_num_tx_queues)) { | |
e87cc472 JP |
2678 | net_warn_ratelimited("%s selects TX queue %d, but real number of TX queues is %d\n", |
2679 | dev->name, queue_index, | |
2680 | dev->real_num_tx_queues); | |
ed04642f ED |
2681 | return 0; |
2682 | } | |
2683 | return queue_index; | |
2684 | } | |
2685 | ||
1d24eb48 TH |
2686 | static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb) |
2687 | { | |
bf264145 | 2688 | #ifdef CONFIG_XPS |
1d24eb48 TH |
2689 | struct xps_dev_maps *dev_maps; |
2690 | struct xps_map *map; | |
2691 | int queue_index = -1; | |
2692 | ||
2693 | rcu_read_lock(); | |
2694 | dev_maps = rcu_dereference(dev->xps_maps); | |
2695 | if (dev_maps) { | |
2696 | map = rcu_dereference( | |
2697 | dev_maps->cpu_map[raw_smp_processor_id()]); | |
2698 | if (map) { | |
2699 | if (map->len == 1) | |
2700 | queue_index = map->queues[0]; | |
2701 | else { | |
2702 | u32 hash; | |
2703 | if (skb->sk && skb->sk->sk_hash) | |
2704 | hash = skb->sk->sk_hash; | |
2705 | else | |
2706 | hash = (__force u16) skb->protocol ^ | |
2707 | skb->rxhash; | |
2708 | hash = jhash_1word(hash, hashrnd); | |
2709 | queue_index = map->queues[ | |
2710 | ((u64)hash * map->len) >> 32]; | |
2711 | } | |
2712 | if (unlikely(queue_index >= dev->real_num_tx_queues)) | |
2713 | queue_index = -1; | |
2714 | } | |
2715 | } | |
2716 | rcu_read_unlock(); | |
2717 | ||
2718 | return queue_index; | |
2719 | #else | |
2720 | return -1; | |
2721 | #endif | |
2722 | } | |
2723 | ||
416186fb | 2724 | u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb) |
e8a0464c | 2725 | { |
416186fb AD |
2726 | struct sock *sk = skb->sk; |
2727 | int queue_index = sk_tx_queue_get(sk); | |
a4ee3ce3 | 2728 | |
416186fb AD |
2729 | if (queue_index < 0 || skb->ooo_okay || |
2730 | queue_index >= dev->real_num_tx_queues) { | |
2731 | int new_index = get_xps_queue(dev, skb); | |
2732 | if (new_index < 0) | |
2733 | new_index = skb_tx_hash(dev, skb); | |
fd2ea0a7 | 2734 | |
416186fb AD |
2735 | if (queue_index != new_index && sk) { |
2736 | struct dst_entry *dst = | |
3853b584 | 2737 | rcu_dereference_check(sk->sk_dst_cache, 1); |
8728c544 | 2738 | |
416186fb AD |
2739 | if (dst && skb_dst(skb) == dst) |
2740 | sk_tx_queue_set(sk, queue_index); | |
2741 | ||
a4ee3ce3 | 2742 | } |
416186fb AD |
2743 | |
2744 | queue_index = new_index; | |
2745 | } | |
2746 | ||
2747 | return queue_index; | |
2748 | } | |
87696f92 | 2749 | EXPORT_SYMBOL(__netdev_pick_tx); |
416186fb AD |
2750 | |
2751 | struct netdev_queue *netdev_pick_tx(struct net_device *dev, | |
2752 | struct sk_buff *skb) | |
2753 | { | |
2754 | int queue_index = 0; | |
2755 | ||
2756 | if (dev->real_num_tx_queues != 1) { | |
2757 | const struct net_device_ops *ops = dev->netdev_ops; | |
2758 | if (ops->ndo_select_queue) | |
2759 | queue_index = ops->ndo_select_queue(dev, skb); | |
2760 | else | |
2761 | queue_index = __netdev_pick_tx(dev, skb); | |
2762 | queue_index = dev_cap_txqueue(dev, queue_index); | |
a4ee3ce3 | 2763 | } |
eae792b7 | 2764 | |
fd2ea0a7 DM |
2765 | skb_set_queue_mapping(skb, queue_index); |
2766 | return netdev_get_tx_queue(dev, queue_index); | |
e8a0464c DM |
2767 | } |
2768 | ||
1def9238 ED |
2769 | static void qdisc_pkt_len_init(struct sk_buff *skb) |
2770 | { | |
2771 | const struct skb_shared_info *shinfo = skb_shinfo(skb); | |
2772 | ||
2773 | qdisc_skb_cb(skb)->pkt_len = skb->len; | |
2774 | ||
2775 | /* To get more precise estimation of bytes sent on wire, | |
2776 | * we add to pkt_len the headers size of all segments | |
2777 | */ | |
2778 | if (shinfo->gso_size) { | |
2779 | unsigned int hdr_len = skb_transport_offset(skb); | |
2780 | ||
2781 | if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) | |
2782 | hdr_len += tcp_hdrlen(skb); | |
2783 | else | |
2784 | hdr_len += sizeof(struct udphdr); | |
2785 | qdisc_skb_cb(skb)->pkt_len += (shinfo->gso_segs - 1) * hdr_len; | |
2786 | } | |
2787 | } | |
2788 | ||
bbd8a0d3 KK |
2789 | static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q, |
2790 | struct net_device *dev, | |
2791 | struct netdev_queue *txq) | |
2792 | { | |
2793 | spinlock_t *root_lock = qdisc_lock(q); | |
a2da570d | 2794 | bool contended; |
bbd8a0d3 KK |
2795 | int rc; |
2796 | ||
1def9238 | 2797 | qdisc_pkt_len_init(skb); |
a2da570d | 2798 | qdisc_calculate_pkt_len(skb, q); |
79640a4c ED |
2799 | /* |
2800 | * Heuristic to force contended enqueues to serialize on a | |
2801 | * separate lock before trying to get qdisc main lock. | |
2802 | * This permits __QDISC_STATE_RUNNING owner to get the lock more often | |
2803 | * and dequeue packets faster. | |
2804 | */ | |
a2da570d | 2805 | contended = qdisc_is_running(q); |
79640a4c ED |
2806 | if (unlikely(contended)) |
2807 | spin_lock(&q->busylock); | |
2808 | ||
bbd8a0d3 KK |
2809 | spin_lock(root_lock); |
2810 | if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) { | |
2811 | kfree_skb(skb); | |
2812 | rc = NET_XMIT_DROP; | |
2813 | } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) && | |
bc135b23 | 2814 | qdisc_run_begin(q)) { |
bbd8a0d3 KK |
2815 | /* |
2816 | * This is a work-conserving queue; there are no old skbs | |
2817 | * waiting to be sent out; and the qdisc is not running - | |
2818 | * xmit the skb directly. | |
2819 | */ | |
7fee226a ED |
2820 | if (!(dev->priv_flags & IFF_XMIT_DST_RELEASE)) |
2821 | skb_dst_force(skb); | |
bfe0d029 | 2822 | |
bfe0d029 ED |
2823 | qdisc_bstats_update(q, skb); |
2824 | ||
79640a4c ED |
2825 | if (sch_direct_xmit(skb, q, dev, txq, root_lock)) { |
2826 | if (unlikely(contended)) { | |
2827 | spin_unlock(&q->busylock); | |
2828 | contended = false; | |
2829 | } | |
bbd8a0d3 | 2830 | __qdisc_run(q); |
79640a4c | 2831 | } else |
bc135b23 | 2832 | qdisc_run_end(q); |
bbd8a0d3 KK |
2833 | |
2834 | rc = NET_XMIT_SUCCESS; | |
2835 | } else { | |
7fee226a | 2836 | skb_dst_force(skb); |
a2da570d | 2837 | rc = q->enqueue(skb, q) & NET_XMIT_MASK; |
79640a4c ED |
2838 | if (qdisc_run_begin(q)) { |
2839 | if (unlikely(contended)) { | |
2840 | spin_unlock(&q->busylock); | |
2841 | contended = false; | |
2842 | } | |
2843 | __qdisc_run(q); | |
2844 | } | |
bbd8a0d3 KK |
2845 | } |
2846 | spin_unlock(root_lock); | |
79640a4c ED |
2847 | if (unlikely(contended)) |
2848 | spin_unlock(&q->busylock); | |
bbd8a0d3 KK |
2849 | return rc; |
2850 | } | |
2851 | ||
5bc1421e NH |
2852 | #if IS_ENABLED(CONFIG_NETPRIO_CGROUP) |
2853 | static void skb_update_prio(struct sk_buff *skb) | |
2854 | { | |
6977a79d | 2855 | struct netprio_map *map = rcu_dereference_bh(skb->dev->priomap); |
5bc1421e | 2856 | |
91c68ce2 ED |
2857 | if (!skb->priority && skb->sk && map) { |
2858 | unsigned int prioidx = skb->sk->sk_cgrp_prioidx; | |
2859 | ||
2860 | if (prioidx < map->priomap_len) | |
2861 | skb->priority = map->priomap[prioidx]; | |
2862 | } | |
5bc1421e NH |
2863 | } |
2864 | #else | |
2865 | #define skb_update_prio(skb) | |
2866 | #endif | |
2867 | ||
745e20f1 | 2868 | static DEFINE_PER_CPU(int, xmit_recursion); |
11a766ce | 2869 | #define RECURSION_LIMIT 10 |
745e20f1 | 2870 | |
95603e22 MM |
2871 | /** |
2872 | * dev_loopback_xmit - loop back @skb | |
2873 | * @skb: buffer to transmit | |
2874 | */ | |
2875 | int dev_loopback_xmit(struct sk_buff *skb) | |
2876 | { | |
2877 | skb_reset_mac_header(skb); | |
2878 | __skb_pull(skb, skb_network_offset(skb)); | |
2879 | skb->pkt_type = PACKET_LOOPBACK; | |
2880 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
2881 | WARN_ON(!skb_dst(skb)); | |
2882 | skb_dst_force(skb); | |
2883 | netif_rx_ni(skb); | |
2884 | return 0; | |
2885 | } | |
2886 | EXPORT_SYMBOL(dev_loopback_xmit); | |
2887 | ||
d29f749e DJ |
2888 | /** |
2889 | * dev_queue_xmit - transmit a buffer | |
2890 | * @skb: buffer to transmit | |
2891 | * | |
2892 | * Queue a buffer for transmission to a network device. The caller must | |
2893 | * have set the device and priority and built the buffer before calling | |
2894 | * this function. The function can be called from an interrupt. | |
2895 | * | |
2896 | * A negative errno code is returned on a failure. A success does not | |
2897 | * guarantee the frame will be transmitted as it may be dropped due | |
2898 | * to congestion or traffic shaping. | |
2899 | * | |
2900 | * ----------------------------------------------------------------------------------- | |
2901 | * I notice this method can also return errors from the queue disciplines, | |
2902 | * including NET_XMIT_DROP, which is a positive value. So, errors can also | |
2903 | * be positive. | |
2904 | * | |
2905 | * Regardless of the return value, the skb is consumed, so it is currently | |
2906 | * difficult to retry a send to this method. (You can bump the ref count | |
2907 | * before sending to hold a reference for retry if you are careful.) | |
2908 | * | |
2909 | * When calling this method, interrupts MUST be enabled. This is because | |
2910 | * the BH enable code must have IRQs enabled so that it will not deadlock. | |
2911 | * --BLG | |
2912 | */ | |
1da177e4 LT |
2913 | int dev_queue_xmit(struct sk_buff *skb) |
2914 | { | |
2915 | struct net_device *dev = skb->dev; | |
dc2b4847 | 2916 | struct netdev_queue *txq; |
1da177e4 LT |
2917 | struct Qdisc *q; |
2918 | int rc = -ENOMEM; | |
2919 | ||
4ec93edb YH |
2920 | /* Disable soft irqs for various locks below. Also |
2921 | * stops preemption for RCU. | |
1da177e4 | 2922 | */ |
4ec93edb | 2923 | rcu_read_lock_bh(); |
1da177e4 | 2924 | |
5bc1421e NH |
2925 | skb_update_prio(skb); |
2926 | ||
8c4c49df | 2927 | txq = netdev_pick_tx(dev, skb); |
a898def2 | 2928 | q = rcu_dereference_bh(txq->qdisc); |
37437bb2 | 2929 | |
1da177e4 | 2930 | #ifdef CONFIG_NET_CLS_ACT |
d1b19dff | 2931 | skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_EGRESS); |
1da177e4 | 2932 | #endif |
cf66ba58 | 2933 | trace_net_dev_queue(skb); |
1da177e4 | 2934 | if (q->enqueue) { |
bbd8a0d3 | 2935 | rc = __dev_xmit_skb(skb, q, dev, txq); |
37437bb2 | 2936 | goto out; |
1da177e4 LT |
2937 | } |
2938 | ||
2939 | /* The device has no queue. Common case for software devices: | |
2940 | loopback, all the sorts of tunnels... | |
2941 | ||
932ff279 HX |
2942 | Really, it is unlikely that netif_tx_lock protection is necessary |
2943 | here. (f.e. loopback and IP tunnels are clean ignoring statistics | |
1da177e4 LT |
2944 | counters.) |
2945 | However, it is possible, that they rely on protection | |
2946 | made by us here. | |
2947 | ||
2948 | Check this and shot the lock. It is not prone from deadlocks. | |
2949 | Either shot noqueue qdisc, it is even simpler 8) | |
2950 | */ | |
2951 | if (dev->flags & IFF_UP) { | |
2952 | int cpu = smp_processor_id(); /* ok because BHs are off */ | |
2953 | ||
c773e847 | 2954 | if (txq->xmit_lock_owner != cpu) { |
1da177e4 | 2955 | |
745e20f1 ED |
2956 | if (__this_cpu_read(xmit_recursion) > RECURSION_LIMIT) |
2957 | goto recursion_alert; | |
2958 | ||
c773e847 | 2959 | HARD_TX_LOCK(dev, txq, cpu); |
1da177e4 | 2960 | |
73466498 | 2961 | if (!netif_xmit_stopped(txq)) { |
745e20f1 | 2962 | __this_cpu_inc(xmit_recursion); |
572a9d7b | 2963 | rc = dev_hard_start_xmit(skb, dev, txq); |
745e20f1 | 2964 | __this_cpu_dec(xmit_recursion); |
572a9d7b | 2965 | if (dev_xmit_complete(rc)) { |
c773e847 | 2966 | HARD_TX_UNLOCK(dev, txq); |
1da177e4 LT |
2967 | goto out; |
2968 | } | |
2969 | } | |
c773e847 | 2970 | HARD_TX_UNLOCK(dev, txq); |
e87cc472 JP |
2971 | net_crit_ratelimited("Virtual device %s asks to queue packet!\n", |
2972 | dev->name); | |
1da177e4 LT |
2973 | } else { |
2974 | /* Recursion is detected! It is possible, | |
745e20f1 ED |
2975 | * unfortunately |
2976 | */ | |
2977 | recursion_alert: | |
e87cc472 JP |
2978 | net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n", |
2979 | dev->name); | |
1da177e4 LT |
2980 | } |
2981 | } | |
2982 | ||
2983 | rc = -ENETDOWN; | |
d4828d85 | 2984 | rcu_read_unlock_bh(); |
1da177e4 | 2985 | |
1da177e4 LT |
2986 | kfree_skb(skb); |
2987 | return rc; | |
2988 | out: | |
d4828d85 | 2989 | rcu_read_unlock_bh(); |
1da177e4 LT |
2990 | return rc; |
2991 | } | |
d1b19dff | 2992 | EXPORT_SYMBOL(dev_queue_xmit); |
1da177e4 LT |
2993 | |
2994 | ||
2995 | /*======================================================================= | |
2996 | Receiver routines | |
2997 | =======================================================================*/ | |
2998 | ||
6b2bedc3 | 2999 | int netdev_max_backlog __read_mostly = 1000; |
c9e6bc64 ED |
3000 | EXPORT_SYMBOL(netdev_max_backlog); |
3001 | ||
3b098e2d | 3002 | int netdev_tstamp_prequeue __read_mostly = 1; |
6b2bedc3 SH |
3003 | int netdev_budget __read_mostly = 300; |
3004 | int weight_p __read_mostly = 64; /* old backlog weight */ | |
1da177e4 | 3005 | |
eecfd7c4 ED |
3006 | /* Called with irq disabled */ |
3007 | static inline void ____napi_schedule(struct softnet_data *sd, | |
3008 | struct napi_struct *napi) | |
3009 | { | |
3010 | list_add_tail(&napi->poll_list, &sd->poll_list); | |
3011 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
3012 | } | |
3013 | ||
0a9627f2 | 3014 | /* |
bfb564e7 | 3015 | * __skb_get_rxhash: calculate a flow hash based on src/dst addresses |
bdeab991 TH |
3016 | * and src/dst port numbers. Sets rxhash in skb to non-zero hash value |
3017 | * on success, zero indicates no valid hash. Also, sets l4_rxhash in skb | |
3018 | * if hash is a canonical 4-tuple hash over transport ports. | |
0a9627f2 | 3019 | */ |
bdeab991 | 3020 | void __skb_get_rxhash(struct sk_buff *skb) |
0a9627f2 | 3021 | { |
4504b861 ED |
3022 | struct flow_keys keys; |
3023 | u32 hash; | |
c6865cb3 | 3024 | |
4504b861 ED |
3025 | if (!skb_flow_dissect(skb, &keys)) |
3026 | return; | |
e971b722 | 3027 | |
68622342 | 3028 | if (keys.ports) |
4504b861 | 3029 | skb->l4_rxhash = 1; |
0a9627f2 | 3030 | |
b249dcb8 | 3031 | /* get a consistent hash (same value on both flow directions) */ |
68622342 CG |
3032 | if (((__force u32)keys.dst < (__force u32)keys.src) || |
3033 | (((__force u32)keys.dst == (__force u32)keys.src) && | |
3034 | ((__force u16)keys.port16[1] < (__force u16)keys.port16[0]))) { | |
4504b861 | 3035 | swap(keys.dst, keys.src); |
68622342 CG |
3036 | swap(keys.port16[0], keys.port16[1]); |
3037 | } | |
0a9627f2 | 3038 | |
4504b861 ED |
3039 | hash = jhash_3words((__force u32)keys.dst, |
3040 | (__force u32)keys.src, | |
3041 | (__force u32)keys.ports, hashrnd); | |
bfb564e7 KK |
3042 | if (!hash) |
3043 | hash = 1; | |
3044 | ||
bdeab991 | 3045 | skb->rxhash = hash; |
bfb564e7 KK |
3046 | } |
3047 | EXPORT_SYMBOL(__skb_get_rxhash); | |
3048 | ||
3049 | #ifdef CONFIG_RPS | |
3050 | ||
3051 | /* One global table that all flow-based protocols share. */ | |
6e3f7faf | 3052 | struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly; |
bfb564e7 KK |
3053 | EXPORT_SYMBOL(rps_sock_flow_table); |
3054 | ||
c5905afb | 3055 | struct static_key rps_needed __read_mostly; |
adc9300e | 3056 | |
c445477d BH |
3057 | static struct rps_dev_flow * |
3058 | set_rps_cpu(struct net_device *dev, struct sk_buff *skb, | |
3059 | struct rps_dev_flow *rflow, u16 next_cpu) | |
3060 | { | |
09994d1b | 3061 | if (next_cpu != RPS_NO_CPU) { |
c445477d BH |
3062 | #ifdef CONFIG_RFS_ACCEL |
3063 | struct netdev_rx_queue *rxqueue; | |
3064 | struct rps_dev_flow_table *flow_table; | |
3065 | struct rps_dev_flow *old_rflow; | |
3066 | u32 flow_id; | |
3067 | u16 rxq_index; | |
3068 | int rc; | |
3069 | ||
3070 | /* Should we steer this flow to a different hardware queue? */ | |
69a19ee6 BH |
3071 | if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap || |
3072 | !(dev->features & NETIF_F_NTUPLE)) | |
c445477d BH |
3073 | goto out; |
3074 | rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu); | |
3075 | if (rxq_index == skb_get_rx_queue(skb)) | |
3076 | goto out; | |
3077 | ||
3078 | rxqueue = dev->_rx + rxq_index; | |
3079 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
3080 | if (!flow_table) | |
3081 | goto out; | |
3082 | flow_id = skb->rxhash & flow_table->mask; | |
3083 | rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb, | |
3084 | rxq_index, flow_id); | |
3085 | if (rc < 0) | |
3086 | goto out; | |
3087 | old_rflow = rflow; | |
3088 | rflow = &flow_table->flows[flow_id]; | |
c445477d BH |
3089 | rflow->filter = rc; |
3090 | if (old_rflow->filter == rflow->filter) | |
3091 | old_rflow->filter = RPS_NO_FILTER; | |
3092 | out: | |
3093 | #endif | |
3094 | rflow->last_qtail = | |
09994d1b | 3095 | per_cpu(softnet_data, next_cpu).input_queue_head; |
c445477d BH |
3096 | } |
3097 | ||
09994d1b | 3098 | rflow->cpu = next_cpu; |
c445477d BH |
3099 | return rflow; |
3100 | } | |
3101 | ||
bfb564e7 KK |
3102 | /* |
3103 | * get_rps_cpu is called from netif_receive_skb and returns the target | |
3104 | * CPU from the RPS map of the receiving queue for a given skb. | |
3105 | * rcu_read_lock must be held on entry. | |
3106 | */ | |
3107 | static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb, | |
3108 | struct rps_dev_flow **rflowp) | |
3109 | { | |
3110 | struct netdev_rx_queue *rxqueue; | |
6e3f7faf | 3111 | struct rps_map *map; |
bfb564e7 KK |
3112 | struct rps_dev_flow_table *flow_table; |
3113 | struct rps_sock_flow_table *sock_flow_table; | |
3114 | int cpu = -1; | |
3115 | u16 tcpu; | |
3116 | ||
3117 | if (skb_rx_queue_recorded(skb)) { | |
3118 | u16 index = skb_get_rx_queue(skb); | |
62fe0b40 BH |
3119 | if (unlikely(index >= dev->real_num_rx_queues)) { |
3120 | WARN_ONCE(dev->real_num_rx_queues > 1, | |
3121 | "%s received packet on queue %u, but number " | |
3122 | "of RX queues is %u\n", | |
3123 | dev->name, index, dev->real_num_rx_queues); | |
bfb564e7 KK |
3124 | goto done; |
3125 | } | |
3126 | rxqueue = dev->_rx + index; | |
3127 | } else | |
3128 | rxqueue = dev->_rx; | |
3129 | ||
6e3f7faf ED |
3130 | map = rcu_dereference(rxqueue->rps_map); |
3131 | if (map) { | |
85875236 | 3132 | if (map->len == 1 && |
33d480ce | 3133 | !rcu_access_pointer(rxqueue->rps_flow_table)) { |
6febfca9 CG |
3134 | tcpu = map->cpus[0]; |
3135 | if (cpu_online(tcpu)) | |
3136 | cpu = tcpu; | |
3137 | goto done; | |
3138 | } | |
33d480ce | 3139 | } else if (!rcu_access_pointer(rxqueue->rps_flow_table)) { |
bfb564e7 | 3140 | goto done; |
6febfca9 | 3141 | } |
bfb564e7 | 3142 | |
2d47b459 | 3143 | skb_reset_network_header(skb); |
bfb564e7 KK |
3144 | if (!skb_get_rxhash(skb)) |
3145 | goto done; | |
3146 | ||
fec5e652 TH |
3147 | flow_table = rcu_dereference(rxqueue->rps_flow_table); |
3148 | sock_flow_table = rcu_dereference(rps_sock_flow_table); | |
3149 | if (flow_table && sock_flow_table) { | |
3150 | u16 next_cpu; | |
3151 | struct rps_dev_flow *rflow; | |
3152 | ||
3153 | rflow = &flow_table->flows[skb->rxhash & flow_table->mask]; | |
3154 | tcpu = rflow->cpu; | |
3155 | ||
3156 | next_cpu = sock_flow_table->ents[skb->rxhash & | |
3157 | sock_flow_table->mask]; | |
3158 | ||
3159 | /* | |
3160 | * If the desired CPU (where last recvmsg was done) is | |
3161 | * different from current CPU (one in the rx-queue flow | |
3162 | * table entry), switch if one of the following holds: | |
3163 | * - Current CPU is unset (equal to RPS_NO_CPU). | |
3164 | * - Current CPU is offline. | |
3165 | * - The current CPU's queue tail has advanced beyond the | |
3166 | * last packet that was enqueued using this table entry. | |
3167 | * This guarantees that all previous packets for the flow | |
3168 | * have been dequeued, thus preserving in order delivery. | |
3169 | */ | |
3170 | if (unlikely(tcpu != next_cpu) && | |
3171 | (tcpu == RPS_NO_CPU || !cpu_online(tcpu) || | |
3172 | ((int)(per_cpu(softnet_data, tcpu).input_queue_head - | |
baefa31d TH |
3173 | rflow->last_qtail)) >= 0)) { |
3174 | tcpu = next_cpu; | |
c445477d | 3175 | rflow = set_rps_cpu(dev, skb, rflow, next_cpu); |
baefa31d | 3176 | } |
c445477d | 3177 | |
fec5e652 TH |
3178 | if (tcpu != RPS_NO_CPU && cpu_online(tcpu)) { |
3179 | *rflowp = rflow; | |
3180 | cpu = tcpu; | |
3181 | goto done; | |
3182 | } | |
3183 | } | |
3184 | ||
0a9627f2 | 3185 | if (map) { |
fec5e652 | 3186 | tcpu = map->cpus[((u64) skb->rxhash * map->len) >> 32]; |
0a9627f2 TH |
3187 | |
3188 | if (cpu_online(tcpu)) { | |
3189 | cpu = tcpu; | |
3190 | goto done; | |
3191 | } | |
3192 | } | |
3193 | ||
3194 | done: | |
0a9627f2 TH |
3195 | return cpu; |
3196 | } | |
3197 | ||
c445477d BH |
3198 | #ifdef CONFIG_RFS_ACCEL |
3199 | ||
3200 | /** | |
3201 | * rps_may_expire_flow - check whether an RFS hardware filter may be removed | |
3202 | * @dev: Device on which the filter was set | |
3203 | * @rxq_index: RX queue index | |
3204 | * @flow_id: Flow ID passed to ndo_rx_flow_steer() | |
3205 | * @filter_id: Filter ID returned by ndo_rx_flow_steer() | |
3206 | * | |
3207 | * Drivers that implement ndo_rx_flow_steer() should periodically call | |
3208 | * this function for each installed filter and remove the filters for | |
3209 | * which it returns %true. | |
3210 | */ | |
3211 | bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index, | |
3212 | u32 flow_id, u16 filter_id) | |
3213 | { | |
3214 | struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index; | |
3215 | struct rps_dev_flow_table *flow_table; | |
3216 | struct rps_dev_flow *rflow; | |
3217 | bool expire = true; | |
3218 | int cpu; | |
3219 | ||
3220 | rcu_read_lock(); | |
3221 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
3222 | if (flow_table && flow_id <= flow_table->mask) { | |
3223 | rflow = &flow_table->flows[flow_id]; | |
3224 | cpu = ACCESS_ONCE(rflow->cpu); | |
3225 | if (rflow->filter == filter_id && cpu != RPS_NO_CPU && | |
3226 | ((int)(per_cpu(softnet_data, cpu).input_queue_head - | |
3227 | rflow->last_qtail) < | |
3228 | (int)(10 * flow_table->mask))) | |
3229 | expire = false; | |
3230 | } | |
3231 | rcu_read_unlock(); | |
3232 | return expire; | |
3233 | } | |
3234 | EXPORT_SYMBOL(rps_may_expire_flow); | |
3235 | ||
3236 | #endif /* CONFIG_RFS_ACCEL */ | |
3237 | ||
0a9627f2 | 3238 | /* Called from hardirq (IPI) context */ |
e36fa2f7 | 3239 | static void rps_trigger_softirq(void *data) |
0a9627f2 | 3240 | { |
e36fa2f7 ED |
3241 | struct softnet_data *sd = data; |
3242 | ||
eecfd7c4 | 3243 | ____napi_schedule(sd, &sd->backlog); |
dee42870 | 3244 | sd->received_rps++; |
0a9627f2 | 3245 | } |
e36fa2f7 | 3246 | |
fec5e652 | 3247 | #endif /* CONFIG_RPS */ |
0a9627f2 | 3248 | |
e36fa2f7 ED |
3249 | /* |
3250 | * Check if this softnet_data structure is another cpu one | |
3251 | * If yes, queue it to our IPI list and return 1 | |
3252 | * If no, return 0 | |
3253 | */ | |
3254 | static int rps_ipi_queued(struct softnet_data *sd) | |
3255 | { | |
3256 | #ifdef CONFIG_RPS | |
3257 | struct softnet_data *mysd = &__get_cpu_var(softnet_data); | |
3258 | ||
3259 | if (sd != mysd) { | |
3260 | sd->rps_ipi_next = mysd->rps_ipi_list; | |
3261 | mysd->rps_ipi_list = sd; | |
3262 | ||
3263 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
3264 | return 1; | |
3265 | } | |
3266 | #endif /* CONFIG_RPS */ | |
3267 | return 0; | |
3268 | } | |
3269 | ||
0a9627f2 TH |
3270 | /* |
3271 | * enqueue_to_backlog is called to queue an skb to a per CPU backlog | |
3272 | * queue (may be a remote CPU queue). | |
3273 | */ | |
fec5e652 TH |
3274 | static int enqueue_to_backlog(struct sk_buff *skb, int cpu, |
3275 | unsigned int *qtail) | |
0a9627f2 | 3276 | { |
e36fa2f7 | 3277 | struct softnet_data *sd; |
0a9627f2 TH |
3278 | unsigned long flags; |
3279 | ||
e36fa2f7 | 3280 | sd = &per_cpu(softnet_data, cpu); |
0a9627f2 TH |
3281 | |
3282 | local_irq_save(flags); | |
0a9627f2 | 3283 | |
e36fa2f7 | 3284 | rps_lock(sd); |
6e7676c1 CG |
3285 | if (skb_queue_len(&sd->input_pkt_queue) <= netdev_max_backlog) { |
3286 | if (skb_queue_len(&sd->input_pkt_queue)) { | |
0a9627f2 | 3287 | enqueue: |
e36fa2f7 | 3288 | __skb_queue_tail(&sd->input_pkt_queue, skb); |
76cc8b13 | 3289 | input_queue_tail_incr_save(sd, qtail); |
e36fa2f7 | 3290 | rps_unlock(sd); |
152102c7 | 3291 | local_irq_restore(flags); |
0a9627f2 TH |
3292 | return NET_RX_SUCCESS; |
3293 | } | |
3294 | ||
ebda37c2 ED |
3295 | /* Schedule NAPI for backlog device |
3296 | * We can use non atomic operation since we own the queue lock | |
3297 | */ | |
3298 | if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) { | |
e36fa2f7 | 3299 | if (!rps_ipi_queued(sd)) |
eecfd7c4 | 3300 | ____napi_schedule(sd, &sd->backlog); |
0a9627f2 TH |
3301 | } |
3302 | goto enqueue; | |
3303 | } | |
3304 | ||
dee42870 | 3305 | sd->dropped++; |
e36fa2f7 | 3306 | rps_unlock(sd); |
0a9627f2 | 3307 | |
0a9627f2 TH |
3308 | local_irq_restore(flags); |
3309 | ||
caf586e5 | 3310 | atomic_long_inc(&skb->dev->rx_dropped); |
0a9627f2 TH |
3311 | kfree_skb(skb); |
3312 | return NET_RX_DROP; | |
3313 | } | |
1da177e4 | 3314 | |
1da177e4 LT |
3315 | /** |
3316 | * netif_rx - post buffer to the network code | |
3317 | * @skb: buffer to post | |
3318 | * | |
3319 | * This function receives a packet from a device driver and queues it for | |
3320 | * the upper (protocol) levels to process. It always succeeds. The buffer | |
3321 | * may be dropped during processing for congestion control or by the | |
3322 | * protocol layers. | |
3323 | * | |
3324 | * return values: | |
3325 | * NET_RX_SUCCESS (no congestion) | |
1da177e4 LT |
3326 | * NET_RX_DROP (packet was dropped) |
3327 | * | |
3328 | */ | |
3329 | ||
3330 | int netif_rx(struct sk_buff *skb) | |
3331 | { | |
b0e28f1e | 3332 | int ret; |
1da177e4 LT |
3333 | |
3334 | /* if netpoll wants it, pretend we never saw it */ | |
3335 | if (netpoll_rx(skb)) | |
3336 | return NET_RX_DROP; | |
3337 | ||
588f0330 | 3338 | net_timestamp_check(netdev_tstamp_prequeue, skb); |
1da177e4 | 3339 | |
cf66ba58 | 3340 | trace_netif_rx(skb); |
df334545 | 3341 | #ifdef CONFIG_RPS |
c5905afb | 3342 | if (static_key_false(&rps_needed)) { |
fec5e652 | 3343 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
b0e28f1e ED |
3344 | int cpu; |
3345 | ||
cece1945 | 3346 | preempt_disable(); |
b0e28f1e | 3347 | rcu_read_lock(); |
fec5e652 TH |
3348 | |
3349 | cpu = get_rps_cpu(skb->dev, skb, &rflow); | |
b0e28f1e ED |
3350 | if (cpu < 0) |
3351 | cpu = smp_processor_id(); | |
fec5e652 TH |
3352 | |
3353 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); | |
3354 | ||
b0e28f1e | 3355 | rcu_read_unlock(); |
cece1945 | 3356 | preempt_enable(); |
adc9300e ED |
3357 | } else |
3358 | #endif | |
fec5e652 TH |
3359 | { |
3360 | unsigned int qtail; | |
3361 | ret = enqueue_to_backlog(skb, get_cpu(), &qtail); | |
3362 | put_cpu(); | |
3363 | } | |
b0e28f1e | 3364 | return ret; |
1da177e4 | 3365 | } |
d1b19dff | 3366 | EXPORT_SYMBOL(netif_rx); |
1da177e4 LT |
3367 | |
3368 | int netif_rx_ni(struct sk_buff *skb) | |
3369 | { | |
3370 | int err; | |
3371 | ||
3372 | preempt_disable(); | |
3373 | err = netif_rx(skb); | |
3374 | if (local_softirq_pending()) | |
3375 | do_softirq(); | |
3376 | preempt_enable(); | |
3377 | ||
3378 | return err; | |
3379 | } | |
1da177e4 LT |
3380 | EXPORT_SYMBOL(netif_rx_ni); |
3381 | ||
1da177e4 LT |
3382 | static void net_tx_action(struct softirq_action *h) |
3383 | { | |
3384 | struct softnet_data *sd = &__get_cpu_var(softnet_data); | |
3385 | ||
3386 | if (sd->completion_queue) { | |
3387 | struct sk_buff *clist; | |
3388 | ||
3389 | local_irq_disable(); | |
3390 | clist = sd->completion_queue; | |
3391 | sd->completion_queue = NULL; | |
3392 | local_irq_enable(); | |
3393 | ||
3394 | while (clist) { | |
3395 | struct sk_buff *skb = clist; | |
3396 | clist = clist->next; | |
3397 | ||
547b792c | 3398 | WARN_ON(atomic_read(&skb->users)); |
07dc22e7 | 3399 | trace_kfree_skb(skb, net_tx_action); |
1da177e4 LT |
3400 | __kfree_skb(skb); |
3401 | } | |
3402 | } | |
3403 | ||
3404 | if (sd->output_queue) { | |
37437bb2 | 3405 | struct Qdisc *head; |
1da177e4 LT |
3406 | |
3407 | local_irq_disable(); | |
3408 | head = sd->output_queue; | |
3409 | sd->output_queue = NULL; | |
a9cbd588 | 3410 | sd->output_queue_tailp = &sd->output_queue; |
1da177e4 LT |
3411 | local_irq_enable(); |
3412 | ||
3413 | while (head) { | |
37437bb2 DM |
3414 | struct Qdisc *q = head; |
3415 | spinlock_t *root_lock; | |
3416 | ||
1da177e4 LT |
3417 | head = head->next_sched; |
3418 | ||
5fb66229 | 3419 | root_lock = qdisc_lock(q); |
37437bb2 | 3420 | if (spin_trylock(root_lock)) { |
def82a1d JP |
3421 | smp_mb__before_clear_bit(); |
3422 | clear_bit(__QDISC_STATE_SCHED, | |
3423 | &q->state); | |
37437bb2 DM |
3424 | qdisc_run(q); |
3425 | spin_unlock(root_lock); | |
1da177e4 | 3426 | } else { |
195648bb | 3427 | if (!test_bit(__QDISC_STATE_DEACTIVATED, |
e8a83e10 | 3428 | &q->state)) { |
195648bb | 3429 | __netif_reschedule(q); |
e8a83e10 JP |
3430 | } else { |
3431 | smp_mb__before_clear_bit(); | |
3432 | clear_bit(__QDISC_STATE_SCHED, | |
3433 | &q->state); | |
3434 | } | |
1da177e4 LT |
3435 | } |
3436 | } | |
3437 | } | |
3438 | } | |
3439 | ||
ab95bfe0 JP |
3440 | #if (defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)) && \ |
3441 | (defined(CONFIG_ATM_LANE) || defined(CONFIG_ATM_LANE_MODULE)) | |
da678292 MM |
3442 | /* This hook is defined here for ATM LANE */ |
3443 | int (*br_fdb_test_addr_hook)(struct net_device *dev, | |
3444 | unsigned char *addr) __read_mostly; | |
4fb019a0 | 3445 | EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook); |
da678292 | 3446 | #endif |
1da177e4 | 3447 | |
1da177e4 LT |
3448 | #ifdef CONFIG_NET_CLS_ACT |
3449 | /* TODO: Maybe we should just force sch_ingress to be compiled in | |
3450 | * when CONFIG_NET_CLS_ACT is? otherwise some useless instructions | |
3451 | * a compare and 2 stores extra right now if we dont have it on | |
3452 | * but have CONFIG_NET_CLS_ACT | |
25985edc LDM |
3453 | * NOTE: This doesn't stop any functionality; if you dont have |
3454 | * the ingress scheduler, you just can't add policies on ingress. | |
1da177e4 LT |
3455 | * |
3456 | */ | |
24824a09 | 3457 | static int ing_filter(struct sk_buff *skb, struct netdev_queue *rxq) |
1da177e4 | 3458 | { |
1da177e4 | 3459 | struct net_device *dev = skb->dev; |
f697c3e8 | 3460 | u32 ttl = G_TC_RTTL(skb->tc_verd); |
555353cf DM |
3461 | int result = TC_ACT_OK; |
3462 | struct Qdisc *q; | |
4ec93edb | 3463 | |
de384830 | 3464 | if (unlikely(MAX_RED_LOOP < ttl++)) { |
e87cc472 JP |
3465 | net_warn_ratelimited("Redir loop detected Dropping packet (%d->%d)\n", |
3466 | skb->skb_iif, dev->ifindex); | |
f697c3e8 HX |
3467 | return TC_ACT_SHOT; |
3468 | } | |
1da177e4 | 3469 | |
f697c3e8 HX |
3470 | skb->tc_verd = SET_TC_RTTL(skb->tc_verd, ttl); |
3471 | skb->tc_verd = SET_TC_AT(skb->tc_verd, AT_INGRESS); | |
1da177e4 | 3472 | |
83874000 | 3473 | q = rxq->qdisc; |
8d50b53d | 3474 | if (q != &noop_qdisc) { |
83874000 | 3475 | spin_lock(qdisc_lock(q)); |
a9312ae8 DM |
3476 | if (likely(!test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) |
3477 | result = qdisc_enqueue_root(skb, q); | |
83874000 DM |
3478 | spin_unlock(qdisc_lock(q)); |
3479 | } | |
f697c3e8 HX |
3480 | |
3481 | return result; | |
3482 | } | |
86e65da9 | 3483 | |
f697c3e8 HX |
3484 | static inline struct sk_buff *handle_ing(struct sk_buff *skb, |
3485 | struct packet_type **pt_prev, | |
3486 | int *ret, struct net_device *orig_dev) | |
3487 | { | |
24824a09 ED |
3488 | struct netdev_queue *rxq = rcu_dereference(skb->dev->ingress_queue); |
3489 | ||
3490 | if (!rxq || rxq->qdisc == &noop_qdisc) | |
f697c3e8 | 3491 | goto out; |
1da177e4 | 3492 | |
f697c3e8 HX |
3493 | if (*pt_prev) { |
3494 | *ret = deliver_skb(skb, *pt_prev, orig_dev); | |
3495 | *pt_prev = NULL; | |
1da177e4 LT |
3496 | } |
3497 | ||
24824a09 | 3498 | switch (ing_filter(skb, rxq)) { |
f697c3e8 HX |
3499 | case TC_ACT_SHOT: |
3500 | case TC_ACT_STOLEN: | |
3501 | kfree_skb(skb); | |
3502 | return NULL; | |
3503 | } | |
3504 | ||
3505 | out: | |
3506 | skb->tc_verd = 0; | |
3507 | return skb; | |
1da177e4 LT |
3508 | } |
3509 | #endif | |
3510 | ||
ab95bfe0 JP |
3511 | /** |
3512 | * netdev_rx_handler_register - register receive handler | |
3513 | * @dev: device to register a handler for | |
3514 | * @rx_handler: receive handler to register | |
93e2c32b | 3515 | * @rx_handler_data: data pointer that is used by rx handler |
ab95bfe0 JP |
3516 | * |
3517 | * Register a receive hander for a device. This handler will then be | |
3518 | * called from __netif_receive_skb. A negative errno code is returned | |
3519 | * on a failure. | |
3520 | * | |
3521 | * The caller must hold the rtnl_mutex. | |
8a4eb573 JP |
3522 | * |
3523 | * For a general description of rx_handler, see enum rx_handler_result. | |
ab95bfe0 JP |
3524 | */ |
3525 | int netdev_rx_handler_register(struct net_device *dev, | |
93e2c32b JP |
3526 | rx_handler_func_t *rx_handler, |
3527 | void *rx_handler_data) | |
ab95bfe0 JP |
3528 | { |
3529 | ASSERT_RTNL(); | |
3530 | ||
3531 | if (dev->rx_handler) | |
3532 | return -EBUSY; | |
3533 | ||
93e2c32b | 3534 | rcu_assign_pointer(dev->rx_handler_data, rx_handler_data); |
ab95bfe0 JP |
3535 | rcu_assign_pointer(dev->rx_handler, rx_handler); |
3536 | ||
3537 | return 0; | |
3538 | } | |
3539 | EXPORT_SYMBOL_GPL(netdev_rx_handler_register); | |
3540 | ||
3541 | /** | |
3542 | * netdev_rx_handler_unregister - unregister receive handler | |
3543 | * @dev: device to unregister a handler from | |
3544 | * | |
3545 | * Unregister a receive hander from a device. | |
3546 | * | |
3547 | * The caller must hold the rtnl_mutex. | |
3548 | */ | |
3549 | void netdev_rx_handler_unregister(struct net_device *dev) | |
3550 | { | |
3551 | ||
3552 | ASSERT_RTNL(); | |
a9b3cd7f SH |
3553 | RCU_INIT_POINTER(dev->rx_handler, NULL); |
3554 | RCU_INIT_POINTER(dev->rx_handler_data, NULL); | |
ab95bfe0 JP |
3555 | } |
3556 | EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister); | |
3557 | ||
b4b9e355 MG |
3558 | /* |
3559 | * Limit the use of PFMEMALLOC reserves to those protocols that implement | |
3560 | * the special handling of PFMEMALLOC skbs. | |
3561 | */ | |
3562 | static bool skb_pfmemalloc_protocol(struct sk_buff *skb) | |
3563 | { | |
3564 | switch (skb->protocol) { | |
3565 | case __constant_htons(ETH_P_ARP): | |
3566 | case __constant_htons(ETH_P_IP): | |
3567 | case __constant_htons(ETH_P_IPV6): | |
3568 | case __constant_htons(ETH_P_8021Q): | |
3569 | return true; | |
3570 | default: | |
3571 | return false; | |
3572 | } | |
3573 | } | |
3574 | ||
10f744d2 | 3575 | static int __netif_receive_skb(struct sk_buff *skb) |
1da177e4 LT |
3576 | { |
3577 | struct packet_type *ptype, *pt_prev; | |
ab95bfe0 | 3578 | rx_handler_func_t *rx_handler; |
f2ccd8fa | 3579 | struct net_device *orig_dev; |
63d8ea7f | 3580 | struct net_device *null_or_dev; |
8a4eb573 | 3581 | bool deliver_exact = false; |
1da177e4 | 3582 | int ret = NET_RX_DROP; |
252e3346 | 3583 | __be16 type; |
b4b9e355 | 3584 | unsigned long pflags = current->flags; |
1da177e4 | 3585 | |
588f0330 | 3586 | net_timestamp_check(!netdev_tstamp_prequeue, skb); |
81bbb3d4 | 3587 | |
cf66ba58 | 3588 | trace_netif_receive_skb(skb); |
9b22ea56 | 3589 | |
b4b9e355 MG |
3590 | /* |
3591 | * PFMEMALLOC skbs are special, they should | |
3592 | * - be delivered to SOCK_MEMALLOC sockets only | |
3593 | * - stay away from userspace | |
3594 | * - have bounded memory usage | |
3595 | * | |
3596 | * Use PF_MEMALLOC as this saves us from propagating the allocation | |
3597 | * context down to all allocation sites. | |
3598 | */ | |
3599 | if (sk_memalloc_socks() && skb_pfmemalloc(skb)) | |
3600 | current->flags |= PF_MEMALLOC; | |
3601 | ||
1da177e4 | 3602 | /* if we've gotten here through NAPI, check netpoll */ |
bea3348e | 3603 | if (netpoll_receive_skb(skb)) |
b4b9e355 | 3604 | goto out; |
1da177e4 | 3605 | |
cc9bd5ce | 3606 | orig_dev = skb->dev; |
8f903c70 | 3607 | |
c1d2bbe1 | 3608 | skb_reset_network_header(skb); |
fda55eca ED |
3609 | if (!skb_transport_header_was_set(skb)) |
3610 | skb_reset_transport_header(skb); | |
0b5c9db1 | 3611 | skb_reset_mac_len(skb); |
1da177e4 LT |
3612 | |
3613 | pt_prev = NULL; | |
3614 | ||
3615 | rcu_read_lock(); | |
3616 | ||
63d8ea7f | 3617 | another_round: |
b6858177 | 3618 | skb->skb_iif = skb->dev->ifindex; |
63d8ea7f DM |
3619 | |
3620 | __this_cpu_inc(softnet_data.processed); | |
3621 | ||
bcc6d479 JP |
3622 | if (skb->protocol == cpu_to_be16(ETH_P_8021Q)) { |
3623 | skb = vlan_untag(skb); | |
3624 | if (unlikely(!skb)) | |
b4b9e355 | 3625 | goto unlock; |
bcc6d479 JP |
3626 | } |
3627 | ||
1da177e4 LT |
3628 | #ifdef CONFIG_NET_CLS_ACT |
3629 | if (skb->tc_verd & TC_NCLS) { | |
3630 | skb->tc_verd = CLR_TC_NCLS(skb->tc_verd); | |
3631 | goto ncls; | |
3632 | } | |
3633 | #endif | |
3634 | ||
b4b9e355 MG |
3635 | if (sk_memalloc_socks() && skb_pfmemalloc(skb)) |
3636 | goto skip_taps; | |
3637 | ||
1da177e4 | 3638 | list_for_each_entry_rcu(ptype, &ptype_all, list) { |
63d8ea7f | 3639 | if (!ptype->dev || ptype->dev == skb->dev) { |
4ec93edb | 3640 | if (pt_prev) |
f2ccd8fa | 3641 | ret = deliver_skb(skb, pt_prev, orig_dev); |
1da177e4 LT |
3642 | pt_prev = ptype; |
3643 | } | |
3644 | } | |
3645 | ||
b4b9e355 | 3646 | skip_taps: |
1da177e4 | 3647 | #ifdef CONFIG_NET_CLS_ACT |
f697c3e8 HX |
3648 | skb = handle_ing(skb, &pt_prev, &ret, orig_dev); |
3649 | if (!skb) | |
b4b9e355 | 3650 | goto unlock; |
1da177e4 LT |
3651 | ncls: |
3652 | #endif | |
3653 | ||
b4b9e355 MG |
3654 | if (sk_memalloc_socks() && skb_pfmemalloc(skb) |
3655 | && !skb_pfmemalloc_protocol(skb)) | |
3656 | goto drop; | |
3657 | ||
2425717b JF |
3658 | if (vlan_tx_tag_present(skb)) { |
3659 | if (pt_prev) { | |
3660 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
3661 | pt_prev = NULL; | |
3662 | } | |
48cc32d3 | 3663 | if (vlan_do_receive(&skb)) |
2425717b JF |
3664 | goto another_round; |
3665 | else if (unlikely(!skb)) | |
b4b9e355 | 3666 | goto unlock; |
2425717b JF |
3667 | } |
3668 | ||
48cc32d3 | 3669 | rx_handler = rcu_dereference(skb->dev->rx_handler); |
ab95bfe0 JP |
3670 | if (rx_handler) { |
3671 | if (pt_prev) { | |
3672 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
3673 | pt_prev = NULL; | |
3674 | } | |
8a4eb573 JP |
3675 | switch (rx_handler(&skb)) { |
3676 | case RX_HANDLER_CONSUMED: | |
b4b9e355 | 3677 | goto unlock; |
8a4eb573 | 3678 | case RX_HANDLER_ANOTHER: |
63d8ea7f | 3679 | goto another_round; |
8a4eb573 JP |
3680 | case RX_HANDLER_EXACT: |
3681 | deliver_exact = true; | |
3682 | case RX_HANDLER_PASS: | |
3683 | break; | |
3684 | default: | |
3685 | BUG(); | |
3686 | } | |
ab95bfe0 | 3687 | } |
1da177e4 | 3688 | |
48cc32d3 FZ |
3689 | if (vlan_tx_nonzero_tag_present(skb)) |
3690 | skb->pkt_type = PACKET_OTHERHOST; | |
3691 | ||
63d8ea7f | 3692 | /* deliver only exact match when indicated */ |
8a4eb573 | 3693 | null_or_dev = deliver_exact ? skb->dev : NULL; |
1f3c8804 | 3694 | |
1da177e4 | 3695 | type = skb->protocol; |
82d8a867 PE |
3696 | list_for_each_entry_rcu(ptype, |
3697 | &ptype_base[ntohs(type) & PTYPE_HASH_MASK], list) { | |
63d8ea7f | 3698 | if (ptype->type == type && |
e3f48d37 JP |
3699 | (ptype->dev == null_or_dev || ptype->dev == skb->dev || |
3700 | ptype->dev == orig_dev)) { | |
4ec93edb | 3701 | if (pt_prev) |
f2ccd8fa | 3702 | ret = deliver_skb(skb, pt_prev, orig_dev); |
1da177e4 LT |
3703 | pt_prev = ptype; |
3704 | } | |
3705 | } | |
3706 | ||
3707 | if (pt_prev) { | |
1080e512 | 3708 | if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC))) |
0e698bf6 | 3709 | goto drop; |
1080e512 MT |
3710 | else |
3711 | ret = pt_prev->func(skb, skb->dev, pt_prev, orig_dev); | |
1da177e4 | 3712 | } else { |
b4b9e355 | 3713 | drop: |
caf586e5 | 3714 | atomic_long_inc(&skb->dev->rx_dropped); |
1da177e4 LT |
3715 | kfree_skb(skb); |
3716 | /* Jamal, now you will not able to escape explaining | |
3717 | * me how you were going to use this. :-) | |
3718 | */ | |
3719 | ret = NET_RX_DROP; | |
3720 | } | |
3721 | ||
b4b9e355 | 3722 | unlock: |
1da177e4 | 3723 | rcu_read_unlock(); |
b4b9e355 MG |
3724 | out: |
3725 | tsk_restore_flags(current, pflags, PF_MEMALLOC); | |
1da177e4 LT |
3726 | return ret; |
3727 | } | |
0a9627f2 TH |
3728 | |
3729 | /** | |
3730 | * netif_receive_skb - process receive buffer from network | |
3731 | * @skb: buffer to process | |
3732 | * | |
3733 | * netif_receive_skb() is the main receive data processing function. | |
3734 | * It always succeeds. The buffer may be dropped during processing | |
3735 | * for congestion control or by the protocol layers. | |
3736 | * | |
3737 | * This function may only be called from softirq context and interrupts | |
3738 | * should be enabled. | |
3739 | * | |
3740 | * Return values (usually ignored): | |
3741 | * NET_RX_SUCCESS: no congestion | |
3742 | * NET_RX_DROP: packet was dropped | |
3743 | */ | |
3744 | int netif_receive_skb(struct sk_buff *skb) | |
3745 | { | |
588f0330 | 3746 | net_timestamp_check(netdev_tstamp_prequeue, skb); |
3b098e2d | 3747 | |
c1f19b51 RC |
3748 | if (skb_defer_rx_timestamp(skb)) |
3749 | return NET_RX_SUCCESS; | |
3750 | ||
df334545 | 3751 | #ifdef CONFIG_RPS |
c5905afb | 3752 | if (static_key_false(&rps_needed)) { |
3b098e2d ED |
3753 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
3754 | int cpu, ret; | |
fec5e652 | 3755 | |
3b098e2d ED |
3756 | rcu_read_lock(); |
3757 | ||
3758 | cpu = get_rps_cpu(skb->dev, skb, &rflow); | |
0a9627f2 | 3759 | |
3b098e2d ED |
3760 | if (cpu >= 0) { |
3761 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); | |
3762 | rcu_read_unlock(); | |
adc9300e | 3763 | return ret; |
3b098e2d | 3764 | } |
adc9300e | 3765 | rcu_read_unlock(); |
fec5e652 | 3766 | } |
1e94d72f | 3767 | #endif |
adc9300e | 3768 | return __netif_receive_skb(skb); |
0a9627f2 | 3769 | } |
d1b19dff | 3770 | EXPORT_SYMBOL(netif_receive_skb); |
1da177e4 | 3771 | |
88751275 ED |
3772 | /* Network device is going away, flush any packets still pending |
3773 | * Called with irqs disabled. | |
3774 | */ | |
152102c7 | 3775 | static void flush_backlog(void *arg) |
6e583ce5 | 3776 | { |
152102c7 | 3777 | struct net_device *dev = arg; |
e36fa2f7 | 3778 | struct softnet_data *sd = &__get_cpu_var(softnet_data); |
6e583ce5 SH |
3779 | struct sk_buff *skb, *tmp; |
3780 | ||
e36fa2f7 | 3781 | rps_lock(sd); |
6e7676c1 | 3782 | skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) { |
6e583ce5 | 3783 | if (skb->dev == dev) { |
e36fa2f7 | 3784 | __skb_unlink(skb, &sd->input_pkt_queue); |
6e583ce5 | 3785 | kfree_skb(skb); |
76cc8b13 | 3786 | input_queue_head_incr(sd); |
6e583ce5 | 3787 | } |
6e7676c1 | 3788 | } |
e36fa2f7 | 3789 | rps_unlock(sd); |
6e7676c1 CG |
3790 | |
3791 | skb_queue_walk_safe(&sd->process_queue, skb, tmp) { | |
3792 | if (skb->dev == dev) { | |
3793 | __skb_unlink(skb, &sd->process_queue); | |
3794 | kfree_skb(skb); | |
76cc8b13 | 3795 | input_queue_head_incr(sd); |
6e7676c1 CG |
3796 | } |
3797 | } | |
6e583ce5 SH |
3798 | } |
3799 | ||
d565b0a1 HX |
3800 | static int napi_gro_complete(struct sk_buff *skb) |
3801 | { | |
22061d80 | 3802 | struct packet_offload *ptype; |
d565b0a1 | 3803 | __be16 type = skb->protocol; |
22061d80 | 3804 | struct list_head *head = &offload_base; |
d565b0a1 HX |
3805 | int err = -ENOENT; |
3806 | ||
c3c7c254 ED |
3807 | BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb)); |
3808 | ||
fc59f9a3 HX |
3809 | if (NAPI_GRO_CB(skb)->count == 1) { |
3810 | skb_shinfo(skb)->gso_size = 0; | |
d565b0a1 | 3811 | goto out; |
fc59f9a3 | 3812 | } |
d565b0a1 HX |
3813 | |
3814 | rcu_read_lock(); | |
3815 | list_for_each_entry_rcu(ptype, head, list) { | |
f191a1d1 | 3816 | if (ptype->type != type || !ptype->callbacks.gro_complete) |
d565b0a1 HX |
3817 | continue; |
3818 | ||
f191a1d1 | 3819 | err = ptype->callbacks.gro_complete(skb); |
d565b0a1 HX |
3820 | break; |
3821 | } | |
3822 | rcu_read_unlock(); | |
3823 | ||
3824 | if (err) { | |
3825 | WARN_ON(&ptype->list == head); | |
3826 | kfree_skb(skb); | |
3827 | return NET_RX_SUCCESS; | |
3828 | } | |
3829 | ||
3830 | out: | |
d565b0a1 HX |
3831 | return netif_receive_skb(skb); |
3832 | } | |
3833 | ||
2e71a6f8 ED |
3834 | /* napi->gro_list contains packets ordered by age. |
3835 | * youngest packets at the head of it. | |
3836 | * Complete skbs in reverse order to reduce latencies. | |
3837 | */ | |
3838 | void napi_gro_flush(struct napi_struct *napi, bool flush_old) | |
d565b0a1 | 3839 | { |
2e71a6f8 | 3840 | struct sk_buff *skb, *prev = NULL; |
d565b0a1 | 3841 | |
2e71a6f8 ED |
3842 | /* scan list and build reverse chain */ |
3843 | for (skb = napi->gro_list; skb != NULL; skb = skb->next) { | |
3844 | skb->prev = prev; | |
3845 | prev = skb; | |
3846 | } | |
3847 | ||
3848 | for (skb = prev; skb; skb = prev) { | |
d565b0a1 | 3849 | skb->next = NULL; |
2e71a6f8 ED |
3850 | |
3851 | if (flush_old && NAPI_GRO_CB(skb)->age == jiffies) | |
3852 | return; | |
3853 | ||
3854 | prev = skb->prev; | |
d565b0a1 | 3855 | napi_gro_complete(skb); |
2e71a6f8 | 3856 | napi->gro_count--; |
d565b0a1 HX |
3857 | } |
3858 | ||
3859 | napi->gro_list = NULL; | |
3860 | } | |
86cac58b | 3861 | EXPORT_SYMBOL(napi_gro_flush); |
d565b0a1 | 3862 | |
89c5fa33 ED |
3863 | static void gro_list_prepare(struct napi_struct *napi, struct sk_buff *skb) |
3864 | { | |
3865 | struct sk_buff *p; | |
3866 | unsigned int maclen = skb->dev->hard_header_len; | |
3867 | ||
3868 | for (p = napi->gro_list; p; p = p->next) { | |
3869 | unsigned long diffs; | |
3870 | ||
3871 | diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev; | |
3872 | diffs |= p->vlan_tci ^ skb->vlan_tci; | |
3873 | if (maclen == ETH_HLEN) | |
3874 | diffs |= compare_ether_header(skb_mac_header(p), | |
3875 | skb_gro_mac_header(skb)); | |
3876 | else if (!diffs) | |
3877 | diffs = memcmp(skb_mac_header(p), | |
3878 | skb_gro_mac_header(skb), | |
3879 | maclen); | |
3880 | NAPI_GRO_CB(p)->same_flow = !diffs; | |
3881 | NAPI_GRO_CB(p)->flush = 0; | |
3882 | } | |
3883 | } | |
3884 | ||
bb728820 | 3885 | static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb) |
d565b0a1 HX |
3886 | { |
3887 | struct sk_buff **pp = NULL; | |
22061d80 | 3888 | struct packet_offload *ptype; |
d565b0a1 | 3889 | __be16 type = skb->protocol; |
22061d80 | 3890 | struct list_head *head = &offload_base; |
0da2afd5 | 3891 | int same_flow; |
d565b0a1 | 3892 | int mac_len; |
5b252f0c | 3893 | enum gro_result ret; |
d565b0a1 | 3894 | |
ce9e76c8 | 3895 | if (!(skb->dev->features & NETIF_F_GRO) || netpoll_rx_on(skb)) |
d565b0a1 HX |
3896 | goto normal; |
3897 | ||
21dc3301 | 3898 | if (skb_is_gso(skb) || skb_has_frag_list(skb)) |
f17f5c91 HX |
3899 | goto normal; |
3900 | ||
89c5fa33 ED |
3901 | gro_list_prepare(napi, skb); |
3902 | ||
d565b0a1 HX |
3903 | rcu_read_lock(); |
3904 | list_for_each_entry_rcu(ptype, head, list) { | |
f191a1d1 | 3905 | if (ptype->type != type || !ptype->callbacks.gro_receive) |
d565b0a1 HX |
3906 | continue; |
3907 | ||
86911732 | 3908 | skb_set_network_header(skb, skb_gro_offset(skb)); |
d565b0a1 HX |
3909 | mac_len = skb->network_header - skb->mac_header; |
3910 | skb->mac_len = mac_len; | |
3911 | NAPI_GRO_CB(skb)->same_flow = 0; | |
3912 | NAPI_GRO_CB(skb)->flush = 0; | |
5d38a079 | 3913 | NAPI_GRO_CB(skb)->free = 0; |
d565b0a1 | 3914 | |
f191a1d1 | 3915 | pp = ptype->callbacks.gro_receive(&napi->gro_list, skb); |
d565b0a1 HX |
3916 | break; |
3917 | } | |
3918 | rcu_read_unlock(); | |
3919 | ||
3920 | if (&ptype->list == head) | |
3921 | goto normal; | |
3922 | ||
0da2afd5 | 3923 | same_flow = NAPI_GRO_CB(skb)->same_flow; |
5d0d9be8 | 3924 | ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED; |
0da2afd5 | 3925 | |
d565b0a1 HX |
3926 | if (pp) { |
3927 | struct sk_buff *nskb = *pp; | |
3928 | ||
3929 | *pp = nskb->next; | |
3930 | nskb->next = NULL; | |
3931 | napi_gro_complete(nskb); | |
4ae5544f | 3932 | napi->gro_count--; |
d565b0a1 HX |
3933 | } |
3934 | ||
0da2afd5 | 3935 | if (same_flow) |
d565b0a1 HX |
3936 | goto ok; |
3937 | ||
4ae5544f | 3938 | if (NAPI_GRO_CB(skb)->flush || napi->gro_count >= MAX_GRO_SKBS) |
d565b0a1 | 3939 | goto normal; |
d565b0a1 | 3940 | |
4ae5544f | 3941 | napi->gro_count++; |
d565b0a1 | 3942 | NAPI_GRO_CB(skb)->count = 1; |
2e71a6f8 | 3943 | NAPI_GRO_CB(skb)->age = jiffies; |
86911732 | 3944 | skb_shinfo(skb)->gso_size = skb_gro_len(skb); |
d565b0a1 HX |
3945 | skb->next = napi->gro_list; |
3946 | napi->gro_list = skb; | |
5d0d9be8 | 3947 | ret = GRO_HELD; |
d565b0a1 | 3948 | |
ad0f9904 | 3949 | pull: |
cb18978c HX |
3950 | if (skb_headlen(skb) < skb_gro_offset(skb)) { |
3951 | int grow = skb_gro_offset(skb) - skb_headlen(skb); | |
3952 | ||
3953 | BUG_ON(skb->end - skb->tail < grow); | |
3954 | ||
3955 | memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow); | |
3956 | ||
3957 | skb->tail += grow; | |
3958 | skb->data_len -= grow; | |
3959 | ||
3960 | skb_shinfo(skb)->frags[0].page_offset += grow; | |
9e903e08 | 3961 | skb_frag_size_sub(&skb_shinfo(skb)->frags[0], grow); |
cb18978c | 3962 | |
9e903e08 | 3963 | if (unlikely(!skb_frag_size(&skb_shinfo(skb)->frags[0]))) { |
ea2ab693 | 3964 | skb_frag_unref(skb, 0); |
cb18978c HX |
3965 | memmove(skb_shinfo(skb)->frags, |
3966 | skb_shinfo(skb)->frags + 1, | |
e5093aec | 3967 | --skb_shinfo(skb)->nr_frags * sizeof(skb_frag_t)); |
cb18978c | 3968 | } |
ad0f9904 HX |
3969 | } |
3970 | ||
d565b0a1 | 3971 | ok: |
5d0d9be8 | 3972 | return ret; |
d565b0a1 HX |
3973 | |
3974 | normal: | |
ad0f9904 HX |
3975 | ret = GRO_NORMAL; |
3976 | goto pull; | |
5d38a079 | 3977 | } |
96e93eab | 3978 | |
5d38a079 | 3979 | |
bb728820 | 3980 | static gro_result_t napi_skb_finish(gro_result_t ret, struct sk_buff *skb) |
5d38a079 | 3981 | { |
5d0d9be8 HX |
3982 | switch (ret) { |
3983 | case GRO_NORMAL: | |
c7c4b3b6 BH |
3984 | if (netif_receive_skb(skb)) |
3985 | ret = GRO_DROP; | |
3986 | break; | |
5d38a079 | 3987 | |
5d0d9be8 | 3988 | case GRO_DROP: |
5d38a079 HX |
3989 | kfree_skb(skb); |
3990 | break; | |
5b252f0c | 3991 | |
daa86548 | 3992 | case GRO_MERGED_FREE: |
d7e8883c ED |
3993 | if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) |
3994 | kmem_cache_free(skbuff_head_cache, skb); | |
3995 | else | |
3996 | __kfree_skb(skb); | |
daa86548 ED |
3997 | break; |
3998 | ||
5b252f0c BH |
3999 | case GRO_HELD: |
4000 | case GRO_MERGED: | |
4001 | break; | |
5d38a079 HX |
4002 | } |
4003 | ||
c7c4b3b6 | 4004 | return ret; |
5d0d9be8 | 4005 | } |
5d0d9be8 | 4006 | |
ca07e43e | 4007 | static void skb_gro_reset_offset(struct sk_buff *skb) |
78a478d0 | 4008 | { |
ca07e43e ED |
4009 | const struct skb_shared_info *pinfo = skb_shinfo(skb); |
4010 | const skb_frag_t *frag0 = &pinfo->frags[0]; | |
4011 | ||
78a478d0 HX |
4012 | NAPI_GRO_CB(skb)->data_offset = 0; |
4013 | NAPI_GRO_CB(skb)->frag0 = NULL; | |
7489594c | 4014 | NAPI_GRO_CB(skb)->frag0_len = 0; |
78a478d0 | 4015 | |
78d3fd0b | 4016 | if (skb->mac_header == skb->tail && |
ca07e43e ED |
4017 | pinfo->nr_frags && |
4018 | !PageHighMem(skb_frag_page(frag0))) { | |
4019 | NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0); | |
4020 | NAPI_GRO_CB(skb)->frag0_len = skb_frag_size(frag0); | |
7489594c | 4021 | } |
78a478d0 | 4022 | } |
78a478d0 | 4023 | |
c7c4b3b6 | 4024 | gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb) |
5d0d9be8 | 4025 | { |
86911732 HX |
4026 | skb_gro_reset_offset(skb); |
4027 | ||
89c5fa33 | 4028 | return napi_skb_finish(dev_gro_receive(napi, skb), skb); |
d565b0a1 HX |
4029 | } |
4030 | EXPORT_SYMBOL(napi_gro_receive); | |
4031 | ||
d0c2b0d2 | 4032 | static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb) |
96e93eab | 4033 | { |
96e93eab | 4034 | __skb_pull(skb, skb_headlen(skb)); |
2a2a459e ED |
4035 | /* restore the reserve we had after netdev_alloc_skb_ip_align() */ |
4036 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb)); | |
3701e513 | 4037 | skb->vlan_tci = 0; |
66c46d74 | 4038 | skb->dev = napi->dev; |
6d152e23 | 4039 | skb->skb_iif = 0; |
96e93eab HX |
4040 | |
4041 | napi->skb = skb; | |
4042 | } | |
96e93eab | 4043 | |
76620aaf | 4044 | struct sk_buff *napi_get_frags(struct napi_struct *napi) |
5d38a079 | 4045 | { |
5d38a079 | 4046 | struct sk_buff *skb = napi->skb; |
5d38a079 HX |
4047 | |
4048 | if (!skb) { | |
89d71a66 ED |
4049 | skb = netdev_alloc_skb_ip_align(napi->dev, GRO_MAX_HEAD); |
4050 | if (skb) | |
4051 | napi->skb = skb; | |
80595d59 | 4052 | } |
96e93eab HX |
4053 | return skb; |
4054 | } | |
76620aaf | 4055 | EXPORT_SYMBOL(napi_get_frags); |
96e93eab | 4056 | |
bb728820 | 4057 | static gro_result_t napi_frags_finish(struct napi_struct *napi, struct sk_buff *skb, |
c7c4b3b6 | 4058 | gro_result_t ret) |
96e93eab | 4059 | { |
5d0d9be8 HX |
4060 | switch (ret) { |
4061 | case GRO_NORMAL: | |
86911732 | 4062 | case GRO_HELD: |
e76b69cc | 4063 | skb->protocol = eth_type_trans(skb, skb->dev); |
86911732 | 4064 | |
c7c4b3b6 BH |
4065 | if (ret == GRO_HELD) |
4066 | skb_gro_pull(skb, -ETH_HLEN); | |
4067 | else if (netif_receive_skb(skb)) | |
4068 | ret = GRO_DROP; | |
86911732 | 4069 | break; |
5d38a079 | 4070 | |
5d0d9be8 | 4071 | case GRO_DROP: |
5d0d9be8 HX |
4072 | case GRO_MERGED_FREE: |
4073 | napi_reuse_skb(napi, skb); | |
4074 | break; | |
5b252f0c BH |
4075 | |
4076 | case GRO_MERGED: | |
4077 | break; | |
5d0d9be8 | 4078 | } |
5d38a079 | 4079 | |
c7c4b3b6 | 4080 | return ret; |
5d38a079 | 4081 | } |
5d0d9be8 | 4082 | |
4adb9c4a | 4083 | static struct sk_buff *napi_frags_skb(struct napi_struct *napi) |
76620aaf HX |
4084 | { |
4085 | struct sk_buff *skb = napi->skb; | |
4086 | struct ethhdr *eth; | |
a5b1cf28 HX |
4087 | unsigned int hlen; |
4088 | unsigned int off; | |
76620aaf HX |
4089 | |
4090 | napi->skb = NULL; | |
4091 | ||
4092 | skb_reset_mac_header(skb); | |
4093 | skb_gro_reset_offset(skb); | |
4094 | ||
a5b1cf28 HX |
4095 | off = skb_gro_offset(skb); |
4096 | hlen = off + sizeof(*eth); | |
4097 | eth = skb_gro_header_fast(skb, off); | |
4098 | if (skb_gro_header_hard(skb, hlen)) { | |
4099 | eth = skb_gro_header_slow(skb, hlen, off); | |
4100 | if (unlikely(!eth)) { | |
4101 | napi_reuse_skb(napi, skb); | |
4102 | skb = NULL; | |
4103 | goto out; | |
4104 | } | |
76620aaf HX |
4105 | } |
4106 | ||
4107 | skb_gro_pull(skb, sizeof(*eth)); | |
4108 | ||
4109 | /* | |
4110 | * This works because the only protocols we care about don't require | |
4111 | * special handling. We'll fix it up properly at the end. | |
4112 | */ | |
4113 | skb->protocol = eth->h_proto; | |
4114 | ||
4115 | out: | |
4116 | return skb; | |
4117 | } | |
76620aaf | 4118 | |
c7c4b3b6 | 4119 | gro_result_t napi_gro_frags(struct napi_struct *napi) |
5d0d9be8 | 4120 | { |
76620aaf | 4121 | struct sk_buff *skb = napi_frags_skb(napi); |
5d0d9be8 HX |
4122 | |
4123 | if (!skb) | |
c7c4b3b6 | 4124 | return GRO_DROP; |
5d0d9be8 | 4125 | |
89c5fa33 | 4126 | return napi_frags_finish(napi, skb, dev_gro_receive(napi, skb)); |
5d0d9be8 | 4127 | } |
5d38a079 HX |
4128 | EXPORT_SYMBOL(napi_gro_frags); |
4129 | ||
e326bed2 ED |
4130 | /* |
4131 | * net_rps_action sends any pending IPI's for rps. | |
4132 | * Note: called with local irq disabled, but exits with local irq enabled. | |
4133 | */ | |
4134 | static void net_rps_action_and_irq_enable(struct softnet_data *sd) | |
4135 | { | |
4136 | #ifdef CONFIG_RPS | |
4137 | struct softnet_data *remsd = sd->rps_ipi_list; | |
4138 | ||
4139 | if (remsd) { | |
4140 | sd->rps_ipi_list = NULL; | |
4141 | ||
4142 | local_irq_enable(); | |
4143 | ||
4144 | /* Send pending IPI's to kick RPS processing on remote cpus. */ | |
4145 | while (remsd) { | |
4146 | struct softnet_data *next = remsd->rps_ipi_next; | |
4147 | ||
4148 | if (cpu_online(remsd->cpu)) | |
4149 | __smp_call_function_single(remsd->cpu, | |
4150 | &remsd->csd, 0); | |
4151 | remsd = next; | |
4152 | } | |
4153 | } else | |
4154 | #endif | |
4155 | local_irq_enable(); | |
4156 | } | |
4157 | ||
bea3348e | 4158 | static int process_backlog(struct napi_struct *napi, int quota) |
1da177e4 LT |
4159 | { |
4160 | int work = 0; | |
eecfd7c4 | 4161 | struct softnet_data *sd = container_of(napi, struct softnet_data, backlog); |
1da177e4 | 4162 | |
e326bed2 ED |
4163 | #ifdef CONFIG_RPS |
4164 | /* Check if we have pending ipi, its better to send them now, | |
4165 | * not waiting net_rx_action() end. | |
4166 | */ | |
4167 | if (sd->rps_ipi_list) { | |
4168 | local_irq_disable(); | |
4169 | net_rps_action_and_irq_enable(sd); | |
4170 | } | |
4171 | #endif | |
bea3348e | 4172 | napi->weight = weight_p; |
6e7676c1 CG |
4173 | local_irq_disable(); |
4174 | while (work < quota) { | |
1da177e4 | 4175 | struct sk_buff *skb; |
6e7676c1 CG |
4176 | unsigned int qlen; |
4177 | ||
4178 | while ((skb = __skb_dequeue(&sd->process_queue))) { | |
4179 | local_irq_enable(); | |
4180 | __netif_receive_skb(skb); | |
6e7676c1 | 4181 | local_irq_disable(); |
76cc8b13 TH |
4182 | input_queue_head_incr(sd); |
4183 | if (++work >= quota) { | |
4184 | local_irq_enable(); | |
4185 | return work; | |
4186 | } | |
6e7676c1 | 4187 | } |
1da177e4 | 4188 | |
e36fa2f7 | 4189 | rps_lock(sd); |
6e7676c1 | 4190 | qlen = skb_queue_len(&sd->input_pkt_queue); |
76cc8b13 | 4191 | if (qlen) |
6e7676c1 CG |
4192 | skb_queue_splice_tail_init(&sd->input_pkt_queue, |
4193 | &sd->process_queue); | |
76cc8b13 | 4194 | |
6e7676c1 | 4195 | if (qlen < quota - work) { |
eecfd7c4 ED |
4196 | /* |
4197 | * Inline a custom version of __napi_complete(). | |
4198 | * only current cpu owns and manipulates this napi, | |
4199 | * and NAPI_STATE_SCHED is the only possible flag set on backlog. | |
4200 | * we can use a plain write instead of clear_bit(), | |
4201 | * and we dont need an smp_mb() memory barrier. | |
4202 | */ | |
4203 | list_del(&napi->poll_list); | |
4204 | napi->state = 0; | |
4205 | ||
6e7676c1 | 4206 | quota = work + qlen; |
bea3348e | 4207 | } |
e36fa2f7 | 4208 | rps_unlock(sd); |
6e7676c1 CG |
4209 | } |
4210 | local_irq_enable(); | |
1da177e4 | 4211 | |
bea3348e SH |
4212 | return work; |
4213 | } | |
1da177e4 | 4214 | |
bea3348e SH |
4215 | /** |
4216 | * __napi_schedule - schedule for receive | |
c4ea43c5 | 4217 | * @n: entry to schedule |
bea3348e SH |
4218 | * |
4219 | * The entry's receive function will be scheduled to run | |
4220 | */ | |
b5606c2d | 4221 | void __napi_schedule(struct napi_struct *n) |
bea3348e SH |
4222 | { |
4223 | unsigned long flags; | |
1da177e4 | 4224 | |
bea3348e | 4225 | local_irq_save(flags); |
eecfd7c4 | 4226 | ____napi_schedule(&__get_cpu_var(softnet_data), n); |
bea3348e | 4227 | local_irq_restore(flags); |
1da177e4 | 4228 | } |
bea3348e SH |
4229 | EXPORT_SYMBOL(__napi_schedule); |
4230 | ||
d565b0a1 HX |
4231 | void __napi_complete(struct napi_struct *n) |
4232 | { | |
4233 | BUG_ON(!test_bit(NAPI_STATE_SCHED, &n->state)); | |
4234 | BUG_ON(n->gro_list); | |
4235 | ||
4236 | list_del(&n->poll_list); | |
4237 | smp_mb__before_clear_bit(); | |
4238 | clear_bit(NAPI_STATE_SCHED, &n->state); | |
4239 | } | |
4240 | EXPORT_SYMBOL(__napi_complete); | |
4241 | ||
4242 | void napi_complete(struct napi_struct *n) | |
4243 | { | |
4244 | unsigned long flags; | |
4245 | ||
4246 | /* | |
4247 | * don't let napi dequeue from the cpu poll list | |
4248 | * just in case its running on a different cpu | |
4249 | */ | |
4250 | if (unlikely(test_bit(NAPI_STATE_NPSVC, &n->state))) | |
4251 | return; | |
4252 | ||
2e71a6f8 | 4253 | napi_gro_flush(n, false); |
d565b0a1 HX |
4254 | local_irq_save(flags); |
4255 | __napi_complete(n); | |
4256 | local_irq_restore(flags); | |
4257 | } | |
4258 | EXPORT_SYMBOL(napi_complete); | |
4259 | ||
4260 | void netif_napi_add(struct net_device *dev, struct napi_struct *napi, | |
4261 | int (*poll)(struct napi_struct *, int), int weight) | |
4262 | { | |
4263 | INIT_LIST_HEAD(&napi->poll_list); | |
4ae5544f | 4264 | napi->gro_count = 0; |
d565b0a1 | 4265 | napi->gro_list = NULL; |
5d38a079 | 4266 | napi->skb = NULL; |
d565b0a1 HX |
4267 | napi->poll = poll; |
4268 | napi->weight = weight; | |
4269 | list_add(&napi->dev_list, &dev->napi_list); | |
d565b0a1 | 4270 | napi->dev = dev; |
5d38a079 | 4271 | #ifdef CONFIG_NETPOLL |
d565b0a1 HX |
4272 | spin_lock_init(&napi->poll_lock); |
4273 | napi->poll_owner = -1; | |
4274 | #endif | |
4275 | set_bit(NAPI_STATE_SCHED, &napi->state); | |
4276 | } | |
4277 | EXPORT_SYMBOL(netif_napi_add); | |
4278 | ||
4279 | void netif_napi_del(struct napi_struct *napi) | |
4280 | { | |
4281 | struct sk_buff *skb, *next; | |
4282 | ||
d7b06636 | 4283 | list_del_init(&napi->dev_list); |
76620aaf | 4284 | napi_free_frags(napi); |
d565b0a1 HX |
4285 | |
4286 | for (skb = napi->gro_list; skb; skb = next) { | |
4287 | next = skb->next; | |
4288 | skb->next = NULL; | |
4289 | kfree_skb(skb); | |
4290 | } | |
4291 | ||
4292 | napi->gro_list = NULL; | |
4ae5544f | 4293 | napi->gro_count = 0; |
d565b0a1 HX |
4294 | } |
4295 | EXPORT_SYMBOL(netif_napi_del); | |
4296 | ||
1da177e4 LT |
4297 | static void net_rx_action(struct softirq_action *h) |
4298 | { | |
e326bed2 | 4299 | struct softnet_data *sd = &__get_cpu_var(softnet_data); |
24f8b238 | 4300 | unsigned long time_limit = jiffies + 2; |
51b0bded | 4301 | int budget = netdev_budget; |
53fb95d3 MM |
4302 | void *have; |
4303 | ||
1da177e4 LT |
4304 | local_irq_disable(); |
4305 | ||
e326bed2 | 4306 | while (!list_empty(&sd->poll_list)) { |
bea3348e SH |
4307 | struct napi_struct *n; |
4308 | int work, weight; | |
1da177e4 | 4309 | |
bea3348e | 4310 | /* If softirq window is exhuasted then punt. |
24f8b238 SH |
4311 | * Allow this to run for 2 jiffies since which will allow |
4312 | * an average latency of 1.5/HZ. | |
bea3348e | 4313 | */ |
24f8b238 | 4314 | if (unlikely(budget <= 0 || time_after(jiffies, time_limit))) |
1da177e4 LT |
4315 | goto softnet_break; |
4316 | ||
4317 | local_irq_enable(); | |
4318 | ||
bea3348e SH |
4319 | /* Even though interrupts have been re-enabled, this |
4320 | * access is safe because interrupts can only add new | |
4321 | * entries to the tail of this list, and only ->poll() | |
4322 | * calls can remove this head entry from the list. | |
4323 | */ | |
e326bed2 | 4324 | n = list_first_entry(&sd->poll_list, struct napi_struct, poll_list); |
1da177e4 | 4325 | |
bea3348e SH |
4326 | have = netpoll_poll_lock(n); |
4327 | ||
4328 | weight = n->weight; | |
4329 | ||
0a7606c1 DM |
4330 | /* This NAPI_STATE_SCHED test is for avoiding a race |
4331 | * with netpoll's poll_napi(). Only the entity which | |
4332 | * obtains the lock and sees NAPI_STATE_SCHED set will | |
4333 | * actually make the ->poll() call. Therefore we avoid | |
25985edc | 4334 | * accidentally calling ->poll() when NAPI is not scheduled. |
0a7606c1 DM |
4335 | */ |
4336 | work = 0; | |
4ea7e386 | 4337 | if (test_bit(NAPI_STATE_SCHED, &n->state)) { |
0a7606c1 | 4338 | work = n->poll(n, weight); |
4ea7e386 NH |
4339 | trace_napi_poll(n); |
4340 | } | |
bea3348e SH |
4341 | |
4342 | WARN_ON_ONCE(work > weight); | |
4343 | ||
4344 | budget -= work; | |
4345 | ||
4346 | local_irq_disable(); | |
4347 | ||
4348 | /* Drivers must not modify the NAPI state if they | |
4349 | * consume the entire weight. In such cases this code | |
4350 | * still "owns" the NAPI instance and therefore can | |
4351 | * move the instance around on the list at-will. | |
4352 | */ | |
fed17f30 | 4353 | if (unlikely(work == weight)) { |
ff780cd8 HX |
4354 | if (unlikely(napi_disable_pending(n))) { |
4355 | local_irq_enable(); | |
4356 | napi_complete(n); | |
4357 | local_irq_disable(); | |
2e71a6f8 ED |
4358 | } else { |
4359 | if (n->gro_list) { | |
4360 | /* flush too old packets | |
4361 | * If HZ < 1000, flush all packets. | |
4362 | */ | |
4363 | local_irq_enable(); | |
4364 | napi_gro_flush(n, HZ >= 1000); | |
4365 | local_irq_disable(); | |
4366 | } | |
e326bed2 | 4367 | list_move_tail(&n->poll_list, &sd->poll_list); |
2e71a6f8 | 4368 | } |
fed17f30 | 4369 | } |
bea3348e SH |
4370 | |
4371 | netpoll_poll_unlock(have); | |
1da177e4 LT |
4372 | } |
4373 | out: | |
e326bed2 | 4374 | net_rps_action_and_irq_enable(sd); |
0a9627f2 | 4375 | |
db217334 CL |
4376 | #ifdef CONFIG_NET_DMA |
4377 | /* | |
4378 | * There may not be any more sk_buffs coming right now, so push | |
4379 | * any pending DMA copies to hardware | |
4380 | */ | |
2ba05622 | 4381 | dma_issue_pending_all(); |
db217334 | 4382 | #endif |
bea3348e | 4383 | |
1da177e4 LT |
4384 | return; |
4385 | ||
4386 | softnet_break: | |
dee42870 | 4387 | sd->time_squeeze++; |
1da177e4 LT |
4388 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); |
4389 | goto out; | |
4390 | } | |
4391 | ||
d1b19dff | 4392 | static gifconf_func_t *gifconf_list[NPROTO]; |
1da177e4 LT |
4393 | |
4394 | /** | |
4395 | * register_gifconf - register a SIOCGIF handler | |
4396 | * @family: Address family | |
4397 | * @gifconf: Function handler | |
4398 | * | |
4399 | * Register protocol dependent address dumping routines. The handler | |
4400 | * that is passed must not be freed or reused until it has been replaced | |
4401 | * by another handler. | |
4402 | */ | |
d1b19dff | 4403 | int register_gifconf(unsigned int family, gifconf_func_t *gifconf) |
1da177e4 LT |
4404 | { |
4405 | if (family >= NPROTO) | |
4406 | return -EINVAL; | |
4407 | gifconf_list[family] = gifconf; | |
4408 | return 0; | |
4409 | } | |
d1b19dff | 4410 | EXPORT_SYMBOL(register_gifconf); |
1da177e4 LT |
4411 | |
4412 | ||
4413 | /* | |
4414 | * Map an interface index to its name (SIOCGIFNAME) | |
4415 | */ | |
4416 | ||
4417 | /* | |
4418 | * We need this ioctl for efficient implementation of the | |
4419 | * if_indextoname() function required by the IPv6 API. Without | |
4420 | * it, we would have to search all the interfaces to find a | |
4421 | * match. --pb | |
4422 | */ | |
4423 | ||
881d966b | 4424 | static int dev_ifname(struct net *net, struct ifreq __user *arg) |
1da177e4 LT |
4425 | { |
4426 | struct net_device *dev; | |
4427 | struct ifreq ifr; | |
c91f6df2 | 4428 | unsigned seq; |
1da177e4 LT |
4429 | |
4430 | /* | |
4431 | * Fetch the caller's info block. | |
4432 | */ | |
4433 | ||
4434 | if (copy_from_user(&ifr, arg, sizeof(struct ifreq))) | |
4435 | return -EFAULT; | |
4436 | ||
c91f6df2 | 4437 | retry: |
30e6c9fa | 4438 | seq = read_seqcount_begin(&devnet_rename_seq); |
fb699dfd ED |
4439 | rcu_read_lock(); |
4440 | dev = dev_get_by_index_rcu(net, ifr.ifr_ifindex); | |
1da177e4 | 4441 | if (!dev) { |
fb699dfd | 4442 | rcu_read_unlock(); |
1da177e4 LT |
4443 | return -ENODEV; |
4444 | } | |
4445 | ||
4446 | strcpy(ifr.ifr_name, dev->name); | |
fb699dfd | 4447 | rcu_read_unlock(); |
30e6c9fa | 4448 | if (read_seqcount_retry(&devnet_rename_seq, seq)) |
c91f6df2 | 4449 | goto retry; |
1da177e4 LT |
4450 | |
4451 | if (copy_to_user(arg, &ifr, sizeof(struct ifreq))) | |
4452 | return -EFAULT; | |
4453 | return 0; | |
4454 | } | |
4455 | ||
4456 | /* | |
4457 | * Perform a SIOCGIFCONF call. This structure will change | |
4458 | * size eventually, and there is nothing I can do about it. | |
4459 | * Thus we will need a 'compatibility mode'. | |
4460 | */ | |
4461 | ||
881d966b | 4462 | static int dev_ifconf(struct net *net, char __user *arg) |
1da177e4 LT |
4463 | { |
4464 | struct ifconf ifc; | |
4465 | struct net_device *dev; | |
4466 | char __user *pos; | |
4467 | int len; | |
4468 | int total; | |
4469 | int i; | |
4470 | ||
4471 | /* | |
4472 | * Fetch the caller's info block. | |
4473 | */ | |
4474 | ||
4475 | if (copy_from_user(&ifc, arg, sizeof(struct ifconf))) | |
4476 | return -EFAULT; | |
4477 | ||
4478 | pos = ifc.ifc_buf; | |
4479 | len = ifc.ifc_len; | |
4480 | ||
4481 | /* | |
4482 | * Loop over the interfaces, and write an info block for each. | |
4483 | */ | |
4484 | ||
4485 | total = 0; | |
881d966b | 4486 | for_each_netdev(net, dev) { |
1da177e4 LT |
4487 | for (i = 0; i < NPROTO; i++) { |
4488 | if (gifconf_list[i]) { | |
4489 | int done; | |
4490 | if (!pos) | |
4491 | done = gifconf_list[i](dev, NULL, 0); | |
4492 | else | |
4493 | done = gifconf_list[i](dev, pos + total, | |
4494 | len - total); | |
4495 | if (done < 0) | |
4496 | return -EFAULT; | |
4497 | total += done; | |
4498 | } | |
4499 | } | |
4ec93edb | 4500 | } |
1da177e4 LT |
4501 | |
4502 | /* | |
4503 | * All done. Write the updated control block back to the caller. | |
4504 | */ | |
4505 | ifc.ifc_len = total; | |
4506 | ||
4507 | /* | |
4508 | * Both BSD and Solaris return 0 here, so we do too. | |
4509 | */ | |
4510 | return copy_to_user(arg, &ifc, sizeof(struct ifconf)) ? -EFAULT : 0; | |
4511 | } | |
4512 | ||
4513 | #ifdef CONFIG_PROC_FS | |
f04565dd | 4514 | |
2def16ae | 4515 | #define BUCKET_SPACE (32 - NETDEV_HASHBITS - 1) |
f04565dd MM |
4516 | |
4517 | #define get_bucket(x) ((x) >> BUCKET_SPACE) | |
4518 | #define get_offset(x) ((x) & ((1 << BUCKET_SPACE) - 1)) | |
4519 | #define set_bucket_offset(b, o) ((b) << BUCKET_SPACE | (o)) | |
4520 | ||
2def16ae | 4521 | static inline struct net_device *dev_from_same_bucket(struct seq_file *seq, loff_t *pos) |
f04565dd | 4522 | { |
f04565dd MM |
4523 | struct net *net = seq_file_net(seq); |
4524 | struct net_device *dev; | |
4525 | struct hlist_node *p; | |
4526 | struct hlist_head *h; | |
2def16ae | 4527 | unsigned int count = 0, offset = get_offset(*pos); |
f04565dd | 4528 | |
2def16ae | 4529 | h = &net->dev_name_head[get_bucket(*pos)]; |
f04565dd | 4530 | hlist_for_each_entry_rcu(dev, p, h, name_hlist) { |
2def16ae | 4531 | if (++count == offset) |
f04565dd | 4532 | return dev; |
f04565dd MM |
4533 | } |
4534 | ||
4535 | return NULL; | |
4536 | } | |
4537 | ||
2def16ae | 4538 | static inline struct net_device *dev_from_bucket(struct seq_file *seq, loff_t *pos) |
f04565dd | 4539 | { |
f04565dd MM |
4540 | struct net_device *dev; |
4541 | unsigned int bucket; | |
4542 | ||
f04565dd | 4543 | do { |
2def16ae | 4544 | dev = dev_from_same_bucket(seq, pos); |
f04565dd MM |
4545 | if (dev) |
4546 | return dev; | |
4547 | ||
2def16ae ED |
4548 | bucket = get_bucket(*pos) + 1; |
4549 | *pos = set_bucket_offset(bucket, 1); | |
f04565dd MM |
4550 | } while (bucket < NETDEV_HASHENTRIES); |
4551 | ||
4552 | return NULL; | |
4553 | } | |
4554 | ||
1da177e4 LT |
4555 | /* |
4556 | * This is invoked by the /proc filesystem handler to display a device | |
4557 | * in detail. | |
4558 | */ | |
7562f876 | 4559 | void *dev_seq_start(struct seq_file *seq, loff_t *pos) |
c6d14c84 | 4560 | __acquires(RCU) |
1da177e4 | 4561 | { |
c6d14c84 | 4562 | rcu_read_lock(); |
7562f876 PE |
4563 | if (!*pos) |
4564 | return SEQ_START_TOKEN; | |
1da177e4 | 4565 | |
2def16ae | 4566 | if (get_bucket(*pos) >= NETDEV_HASHENTRIES) |
f04565dd | 4567 | return NULL; |
1da177e4 | 4568 | |
2def16ae | 4569 | return dev_from_bucket(seq, pos); |
1da177e4 LT |
4570 | } |
4571 | ||
4572 | void *dev_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
4573 | { | |
f04565dd | 4574 | ++*pos; |
2def16ae | 4575 | return dev_from_bucket(seq, pos); |
1da177e4 LT |
4576 | } |
4577 | ||
4578 | void dev_seq_stop(struct seq_file *seq, void *v) | |
c6d14c84 | 4579 | __releases(RCU) |
1da177e4 | 4580 | { |
c6d14c84 | 4581 | rcu_read_unlock(); |
1da177e4 LT |
4582 | } |
4583 | ||
4584 | static void dev_seq_printf_stats(struct seq_file *seq, struct net_device *dev) | |
4585 | { | |
28172739 ED |
4586 | struct rtnl_link_stats64 temp; |
4587 | const struct rtnl_link_stats64 *stats = dev_get_stats(dev, &temp); | |
1da177e4 | 4588 | |
be1f3c2c BH |
4589 | seq_printf(seq, "%6s: %7llu %7llu %4llu %4llu %4llu %5llu %10llu %9llu " |
4590 | "%8llu %7llu %4llu %4llu %4llu %5llu %7llu %10llu\n", | |
5a1b5898 RR |
4591 | dev->name, stats->rx_bytes, stats->rx_packets, |
4592 | stats->rx_errors, | |
4593 | stats->rx_dropped + stats->rx_missed_errors, | |
4594 | stats->rx_fifo_errors, | |
4595 | stats->rx_length_errors + stats->rx_over_errors + | |
4596 | stats->rx_crc_errors + stats->rx_frame_errors, | |
4597 | stats->rx_compressed, stats->multicast, | |
4598 | stats->tx_bytes, stats->tx_packets, | |
4599 | stats->tx_errors, stats->tx_dropped, | |
4600 | stats->tx_fifo_errors, stats->collisions, | |
4601 | stats->tx_carrier_errors + | |
4602 | stats->tx_aborted_errors + | |
4603 | stats->tx_window_errors + | |
4604 | stats->tx_heartbeat_errors, | |
4605 | stats->tx_compressed); | |
1da177e4 LT |
4606 | } |
4607 | ||
4608 | /* | |
4609 | * Called from the PROCfs module. This now uses the new arbitrary sized | |
4610 | * /proc/net interface to create /proc/net/dev | |
4611 | */ | |
4612 | static int dev_seq_show(struct seq_file *seq, void *v) | |
4613 | { | |
4614 | if (v == SEQ_START_TOKEN) | |
4615 | seq_puts(seq, "Inter-| Receive " | |
4616 | " | Transmit\n" | |
4617 | " face |bytes packets errs drop fifo frame " | |
4618 | "compressed multicast|bytes packets errs " | |
4619 | "drop fifo colls carrier compressed\n"); | |
4620 | else | |
4621 | dev_seq_printf_stats(seq, v); | |
4622 | return 0; | |
4623 | } | |
4624 | ||
dee42870 | 4625 | static struct softnet_data *softnet_get_online(loff_t *pos) |
1da177e4 | 4626 | { |
dee42870 | 4627 | struct softnet_data *sd = NULL; |
1da177e4 | 4628 | |
0c0b0aca | 4629 | while (*pos < nr_cpu_ids) |
4ec93edb | 4630 | if (cpu_online(*pos)) { |
dee42870 | 4631 | sd = &per_cpu(softnet_data, *pos); |
1da177e4 LT |
4632 | break; |
4633 | } else | |
4634 | ++*pos; | |
dee42870 | 4635 | return sd; |
1da177e4 LT |
4636 | } |
4637 | ||
4638 | static void *softnet_seq_start(struct seq_file *seq, loff_t *pos) | |
4639 | { | |
4640 | return softnet_get_online(pos); | |
4641 | } | |
4642 | ||
4643 | static void *softnet_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
4644 | { | |
4645 | ++*pos; | |
4646 | return softnet_get_online(pos); | |
4647 | } | |
4648 | ||
4649 | static void softnet_seq_stop(struct seq_file *seq, void *v) | |
4650 | { | |
4651 | } | |
4652 | ||
4653 | static int softnet_seq_show(struct seq_file *seq, void *v) | |
4654 | { | |
dee42870 | 4655 | struct softnet_data *sd = v; |
1da177e4 | 4656 | |
0a9627f2 | 4657 | seq_printf(seq, "%08x %08x %08x %08x %08x %08x %08x %08x %08x %08x\n", |
dee42870 | 4658 | sd->processed, sd->dropped, sd->time_squeeze, 0, |
c1ebcdb8 | 4659 | 0, 0, 0, 0, /* was fastroute */ |
dee42870 | 4660 | sd->cpu_collision, sd->received_rps); |
1da177e4 LT |
4661 | return 0; |
4662 | } | |
4663 | ||
f690808e | 4664 | static const struct seq_operations dev_seq_ops = { |
1da177e4 LT |
4665 | .start = dev_seq_start, |
4666 | .next = dev_seq_next, | |
4667 | .stop = dev_seq_stop, | |
4668 | .show = dev_seq_show, | |
4669 | }; | |
4670 | ||
4671 | static int dev_seq_open(struct inode *inode, struct file *file) | |
4672 | { | |
e372c414 | 4673 | return seq_open_net(inode, file, &dev_seq_ops, |
2def16ae | 4674 | sizeof(struct seq_net_private)); |
5cac98dd AB |
4675 | } |
4676 | ||
9a32144e | 4677 | static const struct file_operations dev_seq_fops = { |
1da177e4 LT |
4678 | .owner = THIS_MODULE, |
4679 | .open = dev_seq_open, | |
4680 | .read = seq_read, | |
4681 | .llseek = seq_lseek, | |
e372c414 | 4682 | .release = seq_release_net, |
1da177e4 LT |
4683 | }; |
4684 | ||
f690808e | 4685 | static const struct seq_operations softnet_seq_ops = { |
1da177e4 LT |
4686 | .start = softnet_seq_start, |
4687 | .next = softnet_seq_next, | |
4688 | .stop = softnet_seq_stop, | |
4689 | .show = softnet_seq_show, | |
4690 | }; | |
4691 | ||
4692 | static int softnet_seq_open(struct inode *inode, struct file *file) | |
4693 | { | |
4694 | return seq_open(file, &softnet_seq_ops); | |
4695 | } | |
4696 | ||
9a32144e | 4697 | static const struct file_operations softnet_seq_fops = { |
1da177e4 LT |
4698 | .owner = THIS_MODULE, |
4699 | .open = softnet_seq_open, | |
4700 | .read = seq_read, | |
4701 | .llseek = seq_lseek, | |
4702 | .release = seq_release, | |
4703 | }; | |
4704 | ||
0e1256ff SH |
4705 | static void *ptype_get_idx(loff_t pos) |
4706 | { | |
4707 | struct packet_type *pt = NULL; | |
4708 | loff_t i = 0; | |
4709 | int t; | |
4710 | ||
4711 | list_for_each_entry_rcu(pt, &ptype_all, list) { | |
4712 | if (i == pos) | |
4713 | return pt; | |
4714 | ++i; | |
4715 | } | |
4716 | ||
82d8a867 | 4717 | for (t = 0; t < PTYPE_HASH_SIZE; t++) { |
0e1256ff SH |
4718 | list_for_each_entry_rcu(pt, &ptype_base[t], list) { |
4719 | if (i == pos) | |
4720 | return pt; | |
4721 | ++i; | |
4722 | } | |
4723 | } | |
4724 | return NULL; | |
4725 | } | |
4726 | ||
4727 | static void *ptype_seq_start(struct seq_file *seq, loff_t *pos) | |
72348a42 | 4728 | __acquires(RCU) |
0e1256ff SH |
4729 | { |
4730 | rcu_read_lock(); | |
4731 | return *pos ? ptype_get_idx(*pos - 1) : SEQ_START_TOKEN; | |
4732 | } | |
4733 | ||
4734 | static void *ptype_seq_next(struct seq_file *seq, void *v, loff_t *pos) | |
4735 | { | |
4736 | struct packet_type *pt; | |
4737 | struct list_head *nxt; | |
4738 | int hash; | |
4739 | ||
4740 | ++*pos; | |
4741 | if (v == SEQ_START_TOKEN) | |
4742 | return ptype_get_idx(0); | |
4743 | ||
4744 | pt = v; | |
4745 | nxt = pt->list.next; | |
4746 | if (pt->type == htons(ETH_P_ALL)) { | |
4747 | if (nxt != &ptype_all) | |
4748 | goto found; | |
4749 | hash = 0; | |
4750 | nxt = ptype_base[0].next; | |
4751 | } else | |
82d8a867 | 4752 | hash = ntohs(pt->type) & PTYPE_HASH_MASK; |
0e1256ff SH |
4753 | |
4754 | while (nxt == &ptype_base[hash]) { | |
82d8a867 | 4755 | if (++hash >= PTYPE_HASH_SIZE) |
0e1256ff SH |
4756 | return NULL; |
4757 | nxt = ptype_base[hash].next; | |
4758 | } | |
4759 | found: | |
4760 | return list_entry(nxt, struct packet_type, list); | |
4761 | } | |
4762 | ||
4763 | static void ptype_seq_stop(struct seq_file *seq, void *v) | |
72348a42 | 4764 | __releases(RCU) |
0e1256ff SH |
4765 | { |
4766 | rcu_read_unlock(); | |
4767 | } | |
4768 | ||
0e1256ff SH |
4769 | static int ptype_seq_show(struct seq_file *seq, void *v) |
4770 | { | |
4771 | struct packet_type *pt = v; | |
4772 | ||
4773 | if (v == SEQ_START_TOKEN) | |
4774 | seq_puts(seq, "Type Device Function\n"); | |
c346dca1 | 4775 | else if (pt->dev == NULL || dev_net(pt->dev) == seq_file_net(seq)) { |
0e1256ff SH |
4776 | if (pt->type == htons(ETH_P_ALL)) |
4777 | seq_puts(seq, "ALL "); | |
4778 | else | |
4779 | seq_printf(seq, "%04x", ntohs(pt->type)); | |
4780 | ||
908cd2da AD |
4781 | seq_printf(seq, " %-8s %pF\n", |
4782 | pt->dev ? pt->dev->name : "", pt->func); | |
0e1256ff SH |
4783 | } |
4784 | ||
4785 | return 0; | |
4786 | } | |
4787 | ||
4788 | static const struct seq_operations ptype_seq_ops = { | |
4789 | .start = ptype_seq_start, | |
4790 | .next = ptype_seq_next, | |
4791 | .stop = ptype_seq_stop, | |
4792 | .show = ptype_seq_show, | |
4793 | }; | |
4794 | ||
4795 | static int ptype_seq_open(struct inode *inode, struct file *file) | |
4796 | { | |
2feb27db PE |
4797 | return seq_open_net(inode, file, &ptype_seq_ops, |
4798 | sizeof(struct seq_net_private)); | |
0e1256ff SH |
4799 | } |
4800 | ||
4801 | static const struct file_operations ptype_seq_fops = { | |
4802 | .owner = THIS_MODULE, | |
4803 | .open = ptype_seq_open, | |
4804 | .read = seq_read, | |
4805 | .llseek = seq_lseek, | |
2feb27db | 4806 | .release = seq_release_net, |
0e1256ff SH |
4807 | }; |
4808 | ||
4809 | ||
4665079c | 4810 | static int __net_init dev_proc_net_init(struct net *net) |
1da177e4 LT |
4811 | { |
4812 | int rc = -ENOMEM; | |
4813 | ||
881d966b | 4814 | if (!proc_net_fops_create(net, "dev", S_IRUGO, &dev_seq_fops)) |
1da177e4 | 4815 | goto out; |
881d966b | 4816 | if (!proc_net_fops_create(net, "softnet_stat", S_IRUGO, &softnet_seq_fops)) |
1da177e4 | 4817 | goto out_dev; |
881d966b | 4818 | if (!proc_net_fops_create(net, "ptype", S_IRUGO, &ptype_seq_fops)) |
457c4cbc | 4819 | goto out_softnet; |
0e1256ff | 4820 | |
881d966b | 4821 | if (wext_proc_init(net)) |
457c4cbc | 4822 | goto out_ptype; |
1da177e4 LT |
4823 | rc = 0; |
4824 | out: | |
4825 | return rc; | |
457c4cbc | 4826 | out_ptype: |
881d966b | 4827 | proc_net_remove(net, "ptype"); |
1da177e4 | 4828 | out_softnet: |
881d966b | 4829 | proc_net_remove(net, "softnet_stat"); |
1da177e4 | 4830 | out_dev: |
881d966b | 4831 | proc_net_remove(net, "dev"); |
1da177e4 LT |
4832 | goto out; |
4833 | } | |
881d966b | 4834 | |
4665079c | 4835 | static void __net_exit dev_proc_net_exit(struct net *net) |
881d966b EB |
4836 | { |
4837 | wext_proc_exit(net); | |
4838 | ||
4839 | proc_net_remove(net, "ptype"); | |
4840 | proc_net_remove(net, "softnet_stat"); | |
4841 | proc_net_remove(net, "dev"); | |
4842 | } | |
4843 | ||
022cbae6 | 4844 | static struct pernet_operations __net_initdata dev_proc_ops = { |
881d966b EB |
4845 | .init = dev_proc_net_init, |
4846 | .exit = dev_proc_net_exit, | |
4847 | }; | |
4848 | ||
4849 | static int __init dev_proc_init(void) | |
4850 | { | |
4851 | return register_pernet_subsys(&dev_proc_ops); | |
4852 | } | |
1da177e4 LT |
4853 | #else |
4854 | #define dev_proc_init() 0 | |
4855 | #endif /* CONFIG_PROC_FS */ | |
4856 | ||
4857 | ||
9ff162a8 JP |
4858 | struct netdev_upper { |
4859 | struct net_device *dev; | |
4860 | bool master; | |
4861 | struct list_head list; | |
4862 | struct rcu_head rcu; | |
4863 | struct list_head search_list; | |
4864 | }; | |
4865 | ||
4866 | static void __append_search_uppers(struct list_head *search_list, | |
4867 | struct net_device *dev) | |
4868 | { | |
4869 | struct netdev_upper *upper; | |
4870 | ||
4871 | list_for_each_entry(upper, &dev->upper_dev_list, list) { | |
4872 | /* check if this upper is not already in search list */ | |
4873 | if (list_empty(&upper->search_list)) | |
4874 | list_add_tail(&upper->search_list, search_list); | |
4875 | } | |
4876 | } | |
4877 | ||
4878 | static bool __netdev_search_upper_dev(struct net_device *dev, | |
4879 | struct net_device *upper_dev) | |
4880 | { | |
4881 | LIST_HEAD(search_list); | |
4882 | struct netdev_upper *upper; | |
4883 | struct netdev_upper *tmp; | |
4884 | bool ret = false; | |
4885 | ||
4886 | __append_search_uppers(&search_list, dev); | |
4887 | list_for_each_entry(upper, &search_list, search_list) { | |
4888 | if (upper->dev == upper_dev) { | |
4889 | ret = true; | |
4890 | break; | |
4891 | } | |
4892 | __append_search_uppers(&search_list, upper->dev); | |
4893 | } | |
4894 | list_for_each_entry_safe(upper, tmp, &search_list, search_list) | |
4895 | INIT_LIST_HEAD(&upper->search_list); | |
4896 | return ret; | |
4897 | } | |
4898 | ||
4899 | static struct netdev_upper *__netdev_find_upper(struct net_device *dev, | |
4900 | struct net_device *upper_dev) | |
4901 | { | |
4902 | struct netdev_upper *upper; | |
4903 | ||
4904 | list_for_each_entry(upper, &dev->upper_dev_list, list) { | |
4905 | if (upper->dev == upper_dev) | |
4906 | return upper; | |
4907 | } | |
4908 | return NULL; | |
4909 | } | |
4910 | ||
4911 | /** | |
4912 | * netdev_has_upper_dev - Check if device is linked to an upper device | |
4913 | * @dev: device | |
4914 | * @upper_dev: upper device to check | |
4915 | * | |
4916 | * Find out if a device is linked to specified upper device and return true | |
4917 | * in case it is. Note that this checks only immediate upper device, | |
4918 | * not through a complete stack of devices. The caller must hold the RTNL lock. | |
4919 | */ | |
4920 | bool netdev_has_upper_dev(struct net_device *dev, | |
4921 | struct net_device *upper_dev) | |
4922 | { | |
4923 | ASSERT_RTNL(); | |
4924 | ||
4925 | return __netdev_find_upper(dev, upper_dev); | |
4926 | } | |
4927 | EXPORT_SYMBOL(netdev_has_upper_dev); | |
4928 | ||
4929 | /** | |
4930 | * netdev_has_any_upper_dev - Check if device is linked to some device | |
4931 | * @dev: device | |
4932 | * | |
4933 | * Find out if a device is linked to an upper device and return true in case | |
4934 | * it is. The caller must hold the RTNL lock. | |
4935 | */ | |
4936 | bool netdev_has_any_upper_dev(struct net_device *dev) | |
4937 | { | |
4938 | ASSERT_RTNL(); | |
4939 | ||
4940 | return !list_empty(&dev->upper_dev_list); | |
4941 | } | |
4942 | EXPORT_SYMBOL(netdev_has_any_upper_dev); | |
4943 | ||
4944 | /** | |
4945 | * netdev_master_upper_dev_get - Get master upper device | |
4946 | * @dev: device | |
4947 | * | |
4948 | * Find a master upper device and return pointer to it or NULL in case | |
4949 | * it's not there. The caller must hold the RTNL lock. | |
4950 | */ | |
4951 | struct net_device *netdev_master_upper_dev_get(struct net_device *dev) | |
4952 | { | |
4953 | struct netdev_upper *upper; | |
4954 | ||
4955 | ASSERT_RTNL(); | |
4956 | ||
4957 | if (list_empty(&dev->upper_dev_list)) | |
4958 | return NULL; | |
4959 | ||
4960 | upper = list_first_entry(&dev->upper_dev_list, | |
4961 | struct netdev_upper, list); | |
4962 | if (likely(upper->master)) | |
4963 | return upper->dev; | |
4964 | return NULL; | |
4965 | } | |
4966 | EXPORT_SYMBOL(netdev_master_upper_dev_get); | |
4967 | ||
4968 | /** | |
4969 | * netdev_master_upper_dev_get_rcu - Get master upper device | |
4970 | * @dev: device | |
4971 | * | |
4972 | * Find a master upper device and return pointer to it or NULL in case | |
4973 | * it's not there. The caller must hold the RCU read lock. | |
4974 | */ | |
4975 | struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev) | |
4976 | { | |
4977 | struct netdev_upper *upper; | |
4978 | ||
4979 | upper = list_first_or_null_rcu(&dev->upper_dev_list, | |
4980 | struct netdev_upper, list); | |
4981 | if (upper && likely(upper->master)) | |
4982 | return upper->dev; | |
4983 | return NULL; | |
4984 | } | |
4985 | EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu); | |
4986 | ||
4987 | static int __netdev_upper_dev_link(struct net_device *dev, | |
4988 | struct net_device *upper_dev, bool master) | |
4989 | { | |
4990 | struct netdev_upper *upper; | |
4991 | ||
4992 | ASSERT_RTNL(); | |
4993 | ||
4994 | if (dev == upper_dev) | |
4995 | return -EBUSY; | |
4996 | ||
4997 | /* To prevent loops, check if dev is not upper device to upper_dev. */ | |
4998 | if (__netdev_search_upper_dev(upper_dev, dev)) | |
4999 | return -EBUSY; | |
5000 | ||
5001 | if (__netdev_find_upper(dev, upper_dev)) | |
5002 | return -EEXIST; | |
5003 | ||
5004 | if (master && netdev_master_upper_dev_get(dev)) | |
5005 | return -EBUSY; | |
5006 | ||
5007 | upper = kmalloc(sizeof(*upper), GFP_KERNEL); | |
5008 | if (!upper) | |
5009 | return -ENOMEM; | |
5010 | ||
5011 | upper->dev = upper_dev; | |
5012 | upper->master = master; | |
5013 | INIT_LIST_HEAD(&upper->search_list); | |
5014 | ||
5015 | /* Ensure that master upper link is always the first item in list. */ | |
5016 | if (master) | |
5017 | list_add_rcu(&upper->list, &dev->upper_dev_list); | |
5018 | else | |
5019 | list_add_tail_rcu(&upper->list, &dev->upper_dev_list); | |
5020 | dev_hold(upper_dev); | |
5021 | ||
5022 | return 0; | |
5023 | } | |
5024 | ||
5025 | /** | |
5026 | * netdev_upper_dev_link - Add a link to the upper device | |
5027 | * @dev: device | |
5028 | * @upper_dev: new upper device | |
5029 | * | |
5030 | * Adds a link to device which is upper to this one. The caller must hold | |
5031 | * the RTNL lock. On a failure a negative errno code is returned. | |
5032 | * On success the reference counts are adjusted and the function | |
5033 | * returns zero. | |
5034 | */ | |
5035 | int netdev_upper_dev_link(struct net_device *dev, | |
5036 | struct net_device *upper_dev) | |
5037 | { | |
5038 | return __netdev_upper_dev_link(dev, upper_dev, false); | |
5039 | } | |
5040 | EXPORT_SYMBOL(netdev_upper_dev_link); | |
5041 | ||
5042 | /** | |
5043 | * netdev_master_upper_dev_link - Add a master link to the upper device | |
5044 | * @dev: device | |
5045 | * @upper_dev: new upper device | |
5046 | * | |
5047 | * Adds a link to device which is upper to this one. In this case, only | |
5048 | * one master upper device can be linked, although other non-master devices | |
5049 | * might be linked as well. The caller must hold the RTNL lock. | |
5050 | * On a failure a negative errno code is returned. On success the reference | |
5051 | * counts are adjusted and the function returns zero. | |
5052 | */ | |
5053 | int netdev_master_upper_dev_link(struct net_device *dev, | |
5054 | struct net_device *upper_dev) | |
5055 | { | |
5056 | return __netdev_upper_dev_link(dev, upper_dev, true); | |
5057 | } | |
5058 | EXPORT_SYMBOL(netdev_master_upper_dev_link); | |
5059 | ||
5060 | /** | |
5061 | * netdev_upper_dev_unlink - Removes a link to upper device | |
5062 | * @dev: device | |
5063 | * @upper_dev: new upper device | |
5064 | * | |
5065 | * Removes a link to device which is upper to this one. The caller must hold | |
5066 | * the RTNL lock. | |
5067 | */ | |
5068 | void netdev_upper_dev_unlink(struct net_device *dev, | |
5069 | struct net_device *upper_dev) | |
5070 | { | |
5071 | struct netdev_upper *upper; | |
5072 | ||
5073 | ASSERT_RTNL(); | |
5074 | ||
5075 | upper = __netdev_find_upper(dev, upper_dev); | |
5076 | if (!upper) | |
5077 | return; | |
5078 | list_del_rcu(&upper->list); | |
5079 | dev_put(upper_dev); | |
5080 | kfree_rcu(upper, rcu); | |
5081 | } | |
5082 | EXPORT_SYMBOL(netdev_upper_dev_unlink); | |
5083 | ||
b6c40d68 PM |
5084 | static void dev_change_rx_flags(struct net_device *dev, int flags) |
5085 | { | |
d314774c SH |
5086 | const struct net_device_ops *ops = dev->netdev_ops; |
5087 | ||
5088 | if ((dev->flags & IFF_UP) && ops->ndo_change_rx_flags) | |
5089 | ops->ndo_change_rx_flags(dev, flags); | |
b6c40d68 PM |
5090 | } |
5091 | ||
dad9b335 | 5092 | static int __dev_set_promiscuity(struct net_device *dev, int inc) |
1da177e4 | 5093 | { |
b536db93 | 5094 | unsigned int old_flags = dev->flags; |
d04a48b0 EB |
5095 | kuid_t uid; |
5096 | kgid_t gid; | |
1da177e4 | 5097 | |
24023451 PM |
5098 | ASSERT_RTNL(); |
5099 | ||
dad9b335 WC |
5100 | dev->flags |= IFF_PROMISC; |
5101 | dev->promiscuity += inc; | |
5102 | if (dev->promiscuity == 0) { | |
5103 | /* | |
5104 | * Avoid overflow. | |
5105 | * If inc causes overflow, untouch promisc and return error. | |
5106 | */ | |
5107 | if (inc < 0) | |
5108 | dev->flags &= ~IFF_PROMISC; | |
5109 | else { | |
5110 | dev->promiscuity -= inc; | |
7b6cd1ce JP |
5111 | pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n", |
5112 | dev->name); | |
dad9b335 WC |
5113 | return -EOVERFLOW; |
5114 | } | |
5115 | } | |
52609c0b | 5116 | if (dev->flags != old_flags) { |
7b6cd1ce JP |
5117 | pr_info("device %s %s promiscuous mode\n", |
5118 | dev->name, | |
5119 | dev->flags & IFF_PROMISC ? "entered" : "left"); | |
8192b0c4 DH |
5120 | if (audit_enabled) { |
5121 | current_uid_gid(&uid, &gid); | |
7759db82 KHK |
5122 | audit_log(current->audit_context, GFP_ATOMIC, |
5123 | AUDIT_ANOM_PROMISCUOUS, | |
5124 | "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u", | |
5125 | dev->name, (dev->flags & IFF_PROMISC), | |
5126 | (old_flags & IFF_PROMISC), | |
e1760bd5 | 5127 | from_kuid(&init_user_ns, audit_get_loginuid(current)), |
d04a48b0 EB |
5128 | from_kuid(&init_user_ns, uid), |
5129 | from_kgid(&init_user_ns, gid), | |
7759db82 | 5130 | audit_get_sessionid(current)); |
8192b0c4 | 5131 | } |
24023451 | 5132 | |
b6c40d68 | 5133 | dev_change_rx_flags(dev, IFF_PROMISC); |
1da177e4 | 5134 | } |
dad9b335 | 5135 | return 0; |
1da177e4 LT |
5136 | } |
5137 | ||
4417da66 PM |
5138 | /** |
5139 | * dev_set_promiscuity - update promiscuity count on a device | |
5140 | * @dev: device | |
5141 | * @inc: modifier | |
5142 | * | |
5143 | * Add or remove promiscuity from a device. While the count in the device | |
5144 | * remains above zero the interface remains promiscuous. Once it hits zero | |
5145 | * the device reverts back to normal filtering operation. A negative inc | |
5146 | * value is used to drop promiscuity on the device. | |
dad9b335 | 5147 | * Return 0 if successful or a negative errno code on error. |
4417da66 | 5148 | */ |
dad9b335 | 5149 | int dev_set_promiscuity(struct net_device *dev, int inc) |
4417da66 | 5150 | { |
b536db93 | 5151 | unsigned int old_flags = dev->flags; |
dad9b335 | 5152 | int err; |
4417da66 | 5153 | |
dad9b335 | 5154 | err = __dev_set_promiscuity(dev, inc); |
4b5a698e | 5155 | if (err < 0) |
dad9b335 | 5156 | return err; |
4417da66 PM |
5157 | if (dev->flags != old_flags) |
5158 | dev_set_rx_mode(dev); | |
dad9b335 | 5159 | return err; |
4417da66 | 5160 | } |
d1b19dff | 5161 | EXPORT_SYMBOL(dev_set_promiscuity); |
4417da66 | 5162 | |
1da177e4 LT |
5163 | /** |
5164 | * dev_set_allmulti - update allmulti count on a device | |
5165 | * @dev: device | |
5166 | * @inc: modifier | |
5167 | * | |
5168 | * Add or remove reception of all multicast frames to a device. While the | |
5169 | * count in the device remains above zero the interface remains listening | |
5170 | * to all interfaces. Once it hits zero the device reverts back to normal | |
5171 | * filtering operation. A negative @inc value is used to drop the counter | |
5172 | * when releasing a resource needing all multicasts. | |
dad9b335 | 5173 | * Return 0 if successful or a negative errno code on error. |
1da177e4 LT |
5174 | */ |
5175 | ||
dad9b335 | 5176 | int dev_set_allmulti(struct net_device *dev, int inc) |
1da177e4 | 5177 | { |
b536db93 | 5178 | unsigned int old_flags = dev->flags; |
1da177e4 | 5179 | |
24023451 PM |
5180 | ASSERT_RTNL(); |
5181 | ||
1da177e4 | 5182 | dev->flags |= IFF_ALLMULTI; |
dad9b335 WC |
5183 | dev->allmulti += inc; |
5184 | if (dev->allmulti == 0) { | |
5185 | /* | |
5186 | * Avoid overflow. | |
5187 | * If inc causes overflow, untouch allmulti and return error. | |
5188 | */ | |
5189 | if (inc < 0) | |
5190 | dev->flags &= ~IFF_ALLMULTI; | |
5191 | else { | |
5192 | dev->allmulti -= inc; | |
7b6cd1ce JP |
5193 | pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n", |
5194 | dev->name); | |
dad9b335 WC |
5195 | return -EOVERFLOW; |
5196 | } | |
5197 | } | |
24023451 | 5198 | if (dev->flags ^ old_flags) { |
b6c40d68 | 5199 | dev_change_rx_flags(dev, IFF_ALLMULTI); |
4417da66 | 5200 | dev_set_rx_mode(dev); |
24023451 | 5201 | } |
dad9b335 | 5202 | return 0; |
4417da66 | 5203 | } |
d1b19dff | 5204 | EXPORT_SYMBOL(dev_set_allmulti); |
4417da66 PM |
5205 | |
5206 | /* | |
5207 | * Upload unicast and multicast address lists to device and | |
5208 | * configure RX filtering. When the device doesn't support unicast | |
53ccaae1 | 5209 | * filtering it is put in promiscuous mode while unicast addresses |
4417da66 PM |
5210 | * are present. |
5211 | */ | |
5212 | void __dev_set_rx_mode(struct net_device *dev) | |
5213 | { | |
d314774c SH |
5214 | const struct net_device_ops *ops = dev->netdev_ops; |
5215 | ||
4417da66 PM |
5216 | /* dev_open will call this function so the list will stay sane. */ |
5217 | if (!(dev->flags&IFF_UP)) | |
5218 | return; | |
5219 | ||
5220 | if (!netif_device_present(dev)) | |
40b77c94 | 5221 | return; |
4417da66 | 5222 | |
01789349 | 5223 | if (!(dev->priv_flags & IFF_UNICAST_FLT)) { |
4417da66 PM |
5224 | /* Unicast addresses changes may only happen under the rtnl, |
5225 | * therefore calling __dev_set_promiscuity here is safe. | |
5226 | */ | |
32e7bfc4 | 5227 | if (!netdev_uc_empty(dev) && !dev->uc_promisc) { |
4417da66 | 5228 | __dev_set_promiscuity(dev, 1); |
2d348d1f | 5229 | dev->uc_promisc = true; |
32e7bfc4 | 5230 | } else if (netdev_uc_empty(dev) && dev->uc_promisc) { |
4417da66 | 5231 | __dev_set_promiscuity(dev, -1); |
2d348d1f | 5232 | dev->uc_promisc = false; |
4417da66 | 5233 | } |
4417da66 | 5234 | } |
01789349 JP |
5235 | |
5236 | if (ops->ndo_set_rx_mode) | |
5237 | ops->ndo_set_rx_mode(dev); | |
4417da66 PM |
5238 | } |
5239 | ||
5240 | void dev_set_rx_mode(struct net_device *dev) | |
5241 | { | |
b9e40857 | 5242 | netif_addr_lock_bh(dev); |
4417da66 | 5243 | __dev_set_rx_mode(dev); |
b9e40857 | 5244 | netif_addr_unlock_bh(dev); |
1da177e4 LT |
5245 | } |
5246 | ||
f0db275a SH |
5247 | /** |
5248 | * dev_get_flags - get flags reported to userspace | |
5249 | * @dev: device | |
5250 | * | |
5251 | * Get the combination of flag bits exported through APIs to userspace. | |
5252 | */ | |
95c96174 | 5253 | unsigned int dev_get_flags(const struct net_device *dev) |
1da177e4 | 5254 | { |
95c96174 | 5255 | unsigned int flags; |
1da177e4 LT |
5256 | |
5257 | flags = (dev->flags & ~(IFF_PROMISC | | |
5258 | IFF_ALLMULTI | | |
b00055aa SR |
5259 | IFF_RUNNING | |
5260 | IFF_LOWER_UP | | |
5261 | IFF_DORMANT)) | | |
1da177e4 LT |
5262 | (dev->gflags & (IFF_PROMISC | |
5263 | IFF_ALLMULTI)); | |
5264 | ||
b00055aa SR |
5265 | if (netif_running(dev)) { |
5266 | if (netif_oper_up(dev)) | |
5267 | flags |= IFF_RUNNING; | |
5268 | if (netif_carrier_ok(dev)) | |
5269 | flags |= IFF_LOWER_UP; | |
5270 | if (netif_dormant(dev)) | |
5271 | flags |= IFF_DORMANT; | |
5272 | } | |
1da177e4 LT |
5273 | |
5274 | return flags; | |
5275 | } | |
d1b19dff | 5276 | EXPORT_SYMBOL(dev_get_flags); |
1da177e4 | 5277 | |
bd380811 | 5278 | int __dev_change_flags(struct net_device *dev, unsigned int flags) |
1da177e4 | 5279 | { |
b536db93 | 5280 | unsigned int old_flags = dev->flags; |
bd380811 | 5281 | int ret; |
1da177e4 | 5282 | |
24023451 PM |
5283 | ASSERT_RTNL(); |
5284 | ||
1da177e4 LT |
5285 | /* |
5286 | * Set the flags on our device. | |
5287 | */ | |
5288 | ||
5289 | dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP | | |
5290 | IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL | | |
5291 | IFF_AUTOMEDIA)) | | |
5292 | (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC | | |
5293 | IFF_ALLMULTI)); | |
5294 | ||
5295 | /* | |
5296 | * Load in the correct multicast list now the flags have changed. | |
5297 | */ | |
5298 | ||
b6c40d68 PM |
5299 | if ((old_flags ^ flags) & IFF_MULTICAST) |
5300 | dev_change_rx_flags(dev, IFF_MULTICAST); | |
24023451 | 5301 | |
4417da66 | 5302 | dev_set_rx_mode(dev); |
1da177e4 LT |
5303 | |
5304 | /* | |
5305 | * Have we downed the interface. We handle IFF_UP ourselves | |
5306 | * according to user attempts to set it, rather than blindly | |
5307 | * setting it. | |
5308 | */ | |
5309 | ||
5310 | ret = 0; | |
5311 | if ((old_flags ^ flags) & IFF_UP) { /* Bit is different ? */ | |
bd380811 | 5312 | ret = ((old_flags & IFF_UP) ? __dev_close : __dev_open)(dev); |
1da177e4 LT |
5313 | |
5314 | if (!ret) | |
4417da66 | 5315 | dev_set_rx_mode(dev); |
1da177e4 LT |
5316 | } |
5317 | ||
1da177e4 | 5318 | if ((flags ^ dev->gflags) & IFF_PROMISC) { |
d1b19dff ED |
5319 | int inc = (flags & IFF_PROMISC) ? 1 : -1; |
5320 | ||
1da177e4 LT |
5321 | dev->gflags ^= IFF_PROMISC; |
5322 | dev_set_promiscuity(dev, inc); | |
5323 | } | |
5324 | ||
5325 | /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI | |
5326 | is important. Some (broken) drivers set IFF_PROMISC, when | |
5327 | IFF_ALLMULTI is requested not asking us and not reporting. | |
5328 | */ | |
5329 | if ((flags ^ dev->gflags) & IFF_ALLMULTI) { | |
d1b19dff ED |
5330 | int inc = (flags & IFF_ALLMULTI) ? 1 : -1; |
5331 | ||
1da177e4 LT |
5332 | dev->gflags ^= IFF_ALLMULTI; |
5333 | dev_set_allmulti(dev, inc); | |
5334 | } | |
5335 | ||
bd380811 PM |
5336 | return ret; |
5337 | } | |
5338 | ||
5339 | void __dev_notify_flags(struct net_device *dev, unsigned int old_flags) | |
5340 | { | |
5341 | unsigned int changes = dev->flags ^ old_flags; | |
5342 | ||
5343 | if (changes & IFF_UP) { | |
5344 | if (dev->flags & IFF_UP) | |
5345 | call_netdevice_notifiers(NETDEV_UP, dev); | |
5346 | else | |
5347 | call_netdevice_notifiers(NETDEV_DOWN, dev); | |
5348 | } | |
5349 | ||
5350 | if (dev->flags & IFF_UP && | |
5351 | (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) | |
5352 | call_netdevice_notifiers(NETDEV_CHANGE, dev); | |
5353 | } | |
5354 | ||
5355 | /** | |
5356 | * dev_change_flags - change device settings | |
5357 | * @dev: device | |
5358 | * @flags: device state flags | |
5359 | * | |
5360 | * Change settings on device based state flags. The flags are | |
5361 | * in the userspace exported format. | |
5362 | */ | |
b536db93 | 5363 | int dev_change_flags(struct net_device *dev, unsigned int flags) |
bd380811 | 5364 | { |
b536db93 ED |
5365 | int ret; |
5366 | unsigned int changes, old_flags = dev->flags; | |
bd380811 PM |
5367 | |
5368 | ret = __dev_change_flags(dev, flags); | |
5369 | if (ret < 0) | |
5370 | return ret; | |
5371 | ||
5372 | changes = old_flags ^ dev->flags; | |
7c355f53 TG |
5373 | if (changes) |
5374 | rtmsg_ifinfo(RTM_NEWLINK, dev, changes); | |
1da177e4 | 5375 | |
bd380811 | 5376 | __dev_notify_flags(dev, old_flags); |
1da177e4 LT |
5377 | return ret; |
5378 | } | |
d1b19dff | 5379 | EXPORT_SYMBOL(dev_change_flags); |
1da177e4 | 5380 | |
f0db275a SH |
5381 | /** |
5382 | * dev_set_mtu - Change maximum transfer unit | |
5383 | * @dev: device | |
5384 | * @new_mtu: new transfer unit | |
5385 | * | |
5386 | * Change the maximum transfer size of the network device. | |
5387 | */ | |
1da177e4 LT |
5388 | int dev_set_mtu(struct net_device *dev, int new_mtu) |
5389 | { | |
d314774c | 5390 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 LT |
5391 | int err; |
5392 | ||
5393 | if (new_mtu == dev->mtu) | |
5394 | return 0; | |
5395 | ||
5396 | /* MTU must be positive. */ | |
5397 | if (new_mtu < 0) | |
5398 | return -EINVAL; | |
5399 | ||
5400 | if (!netif_device_present(dev)) | |
5401 | return -ENODEV; | |
5402 | ||
5403 | err = 0; | |
d314774c SH |
5404 | if (ops->ndo_change_mtu) |
5405 | err = ops->ndo_change_mtu(dev, new_mtu); | |
1da177e4 LT |
5406 | else |
5407 | dev->mtu = new_mtu; | |
d314774c | 5408 | |
e3d8fabe | 5409 | if (!err) |
056925ab | 5410 | call_netdevice_notifiers(NETDEV_CHANGEMTU, dev); |
1da177e4 LT |
5411 | return err; |
5412 | } | |
d1b19dff | 5413 | EXPORT_SYMBOL(dev_set_mtu); |
1da177e4 | 5414 | |
cbda10fa VD |
5415 | /** |
5416 | * dev_set_group - Change group this device belongs to | |
5417 | * @dev: device | |
5418 | * @new_group: group this device should belong to | |
5419 | */ | |
5420 | void dev_set_group(struct net_device *dev, int new_group) | |
5421 | { | |
5422 | dev->group = new_group; | |
5423 | } | |
5424 | EXPORT_SYMBOL(dev_set_group); | |
5425 | ||
f0db275a SH |
5426 | /** |
5427 | * dev_set_mac_address - Change Media Access Control Address | |
5428 | * @dev: device | |
5429 | * @sa: new address | |
5430 | * | |
5431 | * Change the hardware (MAC) address of the device | |
5432 | */ | |
1da177e4 LT |
5433 | int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa) |
5434 | { | |
d314774c | 5435 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 LT |
5436 | int err; |
5437 | ||
d314774c | 5438 | if (!ops->ndo_set_mac_address) |
1da177e4 LT |
5439 | return -EOPNOTSUPP; |
5440 | if (sa->sa_family != dev->type) | |
5441 | return -EINVAL; | |
5442 | if (!netif_device_present(dev)) | |
5443 | return -ENODEV; | |
d314774c | 5444 | err = ops->ndo_set_mac_address(dev, sa); |
f6521516 JP |
5445 | if (err) |
5446 | return err; | |
fbdeca2d | 5447 | dev->addr_assign_type = NET_ADDR_SET; |
f6521516 | 5448 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
7bf23575 | 5449 | add_device_randomness(dev->dev_addr, dev->addr_len); |
f6521516 | 5450 | return 0; |
1da177e4 | 5451 | } |
d1b19dff | 5452 | EXPORT_SYMBOL(dev_set_mac_address); |
1da177e4 | 5453 | |
4bf84c35 JP |
5454 | /** |
5455 | * dev_change_carrier - Change device carrier | |
5456 | * @dev: device | |
5457 | * @new_carries: new value | |
5458 | * | |
5459 | * Change device carrier | |
5460 | */ | |
5461 | int dev_change_carrier(struct net_device *dev, bool new_carrier) | |
5462 | { | |
5463 | const struct net_device_ops *ops = dev->netdev_ops; | |
5464 | ||
5465 | if (!ops->ndo_change_carrier) | |
5466 | return -EOPNOTSUPP; | |
5467 | if (!netif_device_present(dev)) | |
5468 | return -ENODEV; | |
5469 | return ops->ndo_change_carrier(dev, new_carrier); | |
5470 | } | |
5471 | EXPORT_SYMBOL(dev_change_carrier); | |
5472 | ||
1da177e4 | 5473 | /* |
3710becf | 5474 | * Perform the SIOCxIFxxx calls, inside rcu_read_lock() |
1da177e4 | 5475 | */ |
14e3e079 | 5476 | static int dev_ifsioc_locked(struct net *net, struct ifreq *ifr, unsigned int cmd) |
1da177e4 LT |
5477 | { |
5478 | int err; | |
3710becf | 5479 | struct net_device *dev = dev_get_by_name_rcu(net, ifr->ifr_name); |
1da177e4 LT |
5480 | |
5481 | if (!dev) | |
5482 | return -ENODEV; | |
5483 | ||
5484 | switch (cmd) { | |
d1b19dff ED |
5485 | case SIOCGIFFLAGS: /* Get interface flags */ |
5486 | ifr->ifr_flags = (short) dev_get_flags(dev); | |
5487 | return 0; | |
1da177e4 | 5488 | |
d1b19dff ED |
5489 | case SIOCGIFMETRIC: /* Get the metric on the interface |
5490 | (currently unused) */ | |
5491 | ifr->ifr_metric = 0; | |
5492 | return 0; | |
1da177e4 | 5493 | |
d1b19dff ED |
5494 | case SIOCGIFMTU: /* Get the MTU of a device */ |
5495 | ifr->ifr_mtu = dev->mtu; | |
5496 | return 0; | |
1da177e4 | 5497 | |
d1b19dff ED |
5498 | case SIOCGIFHWADDR: |
5499 | if (!dev->addr_len) | |
5500 | memset(ifr->ifr_hwaddr.sa_data, 0, sizeof ifr->ifr_hwaddr.sa_data); | |
5501 | else | |
5502 | memcpy(ifr->ifr_hwaddr.sa_data, dev->dev_addr, | |
5503 | min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len)); | |
5504 | ifr->ifr_hwaddr.sa_family = dev->type; | |
5505 | return 0; | |
1da177e4 | 5506 | |
d1b19dff ED |
5507 | case SIOCGIFSLAVE: |
5508 | err = -EINVAL; | |
5509 | break; | |
14e3e079 | 5510 | |
d1b19dff ED |
5511 | case SIOCGIFMAP: |
5512 | ifr->ifr_map.mem_start = dev->mem_start; | |
5513 | ifr->ifr_map.mem_end = dev->mem_end; | |
5514 | ifr->ifr_map.base_addr = dev->base_addr; | |
5515 | ifr->ifr_map.irq = dev->irq; | |
5516 | ifr->ifr_map.dma = dev->dma; | |
5517 | ifr->ifr_map.port = dev->if_port; | |
5518 | return 0; | |
14e3e079 | 5519 | |
d1b19dff ED |
5520 | case SIOCGIFINDEX: |
5521 | ifr->ifr_ifindex = dev->ifindex; | |
5522 | return 0; | |
14e3e079 | 5523 | |
d1b19dff ED |
5524 | case SIOCGIFTXQLEN: |
5525 | ifr->ifr_qlen = dev->tx_queue_len; | |
5526 | return 0; | |
14e3e079 | 5527 | |
d1b19dff ED |
5528 | default: |
5529 | /* dev_ioctl() should ensure this case | |
5530 | * is never reached | |
5531 | */ | |
5532 | WARN_ON(1); | |
41c31f31 | 5533 | err = -ENOTTY; |
d1b19dff | 5534 | break; |
14e3e079 JG |
5535 | |
5536 | } | |
5537 | return err; | |
5538 | } | |
5539 | ||
5540 | /* | |
5541 | * Perform the SIOCxIFxxx calls, inside rtnl_lock() | |
5542 | */ | |
5543 | static int dev_ifsioc(struct net *net, struct ifreq *ifr, unsigned int cmd) | |
5544 | { | |
5545 | int err; | |
5546 | struct net_device *dev = __dev_get_by_name(net, ifr->ifr_name); | |
5f2f6da7 | 5547 | const struct net_device_ops *ops; |
14e3e079 JG |
5548 | |
5549 | if (!dev) | |
5550 | return -ENODEV; | |
5551 | ||
5f2f6da7 JP |
5552 | ops = dev->netdev_ops; |
5553 | ||
14e3e079 | 5554 | switch (cmd) { |
d1b19dff ED |
5555 | case SIOCSIFFLAGS: /* Set interface flags */ |
5556 | return dev_change_flags(dev, ifr->ifr_flags); | |
14e3e079 | 5557 | |
d1b19dff ED |
5558 | case SIOCSIFMETRIC: /* Set the metric on the interface |
5559 | (currently unused) */ | |
5560 | return -EOPNOTSUPP; | |
14e3e079 | 5561 | |
d1b19dff ED |
5562 | case SIOCSIFMTU: /* Set the MTU of a device */ |
5563 | return dev_set_mtu(dev, ifr->ifr_mtu); | |
1da177e4 | 5564 | |
d1b19dff ED |
5565 | case SIOCSIFHWADDR: |
5566 | return dev_set_mac_address(dev, &ifr->ifr_hwaddr); | |
1da177e4 | 5567 | |
d1b19dff ED |
5568 | case SIOCSIFHWBROADCAST: |
5569 | if (ifr->ifr_hwaddr.sa_family != dev->type) | |
5570 | return -EINVAL; | |
5571 | memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data, | |
5572 | min(sizeof ifr->ifr_hwaddr.sa_data, (size_t) dev->addr_len)); | |
5573 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); | |
5574 | return 0; | |
1da177e4 | 5575 | |
d1b19dff ED |
5576 | case SIOCSIFMAP: |
5577 | if (ops->ndo_set_config) { | |
1da177e4 LT |
5578 | if (!netif_device_present(dev)) |
5579 | return -ENODEV; | |
d1b19dff ED |
5580 | return ops->ndo_set_config(dev, &ifr->ifr_map); |
5581 | } | |
5582 | return -EOPNOTSUPP; | |
1da177e4 | 5583 | |
d1b19dff | 5584 | case SIOCADDMULTI: |
b81693d9 | 5585 | if (!ops->ndo_set_rx_mode || |
d1b19dff ED |
5586 | ifr->ifr_hwaddr.sa_family != AF_UNSPEC) |
5587 | return -EINVAL; | |
5588 | if (!netif_device_present(dev)) | |
5589 | return -ENODEV; | |
22bedad3 | 5590 | return dev_mc_add_global(dev, ifr->ifr_hwaddr.sa_data); |
d1b19dff ED |
5591 | |
5592 | case SIOCDELMULTI: | |
b81693d9 | 5593 | if (!ops->ndo_set_rx_mode || |
d1b19dff ED |
5594 | ifr->ifr_hwaddr.sa_family != AF_UNSPEC) |
5595 | return -EINVAL; | |
5596 | if (!netif_device_present(dev)) | |
5597 | return -ENODEV; | |
22bedad3 | 5598 | return dev_mc_del_global(dev, ifr->ifr_hwaddr.sa_data); |
1da177e4 | 5599 | |
d1b19dff ED |
5600 | case SIOCSIFTXQLEN: |
5601 | if (ifr->ifr_qlen < 0) | |
5602 | return -EINVAL; | |
5603 | dev->tx_queue_len = ifr->ifr_qlen; | |
5604 | return 0; | |
1da177e4 | 5605 | |
d1b19dff ED |
5606 | case SIOCSIFNAME: |
5607 | ifr->ifr_newname[IFNAMSIZ-1] = '\0'; | |
5608 | return dev_change_name(dev, ifr->ifr_newname); | |
1da177e4 | 5609 | |
4dc360c5 RC |
5610 | case SIOCSHWTSTAMP: |
5611 | err = net_hwtstamp_validate(ifr); | |
5612 | if (err) | |
5613 | return err; | |
5614 | /* fall through */ | |
5615 | ||
d1b19dff ED |
5616 | /* |
5617 | * Unknown or private ioctl | |
5618 | */ | |
5619 | default: | |
5620 | if ((cmd >= SIOCDEVPRIVATE && | |
5621 | cmd <= SIOCDEVPRIVATE + 15) || | |
5622 | cmd == SIOCBONDENSLAVE || | |
5623 | cmd == SIOCBONDRELEASE || | |
5624 | cmd == SIOCBONDSETHWADDR || | |
5625 | cmd == SIOCBONDSLAVEINFOQUERY || | |
5626 | cmd == SIOCBONDINFOQUERY || | |
5627 | cmd == SIOCBONDCHANGEACTIVE || | |
5628 | cmd == SIOCGMIIPHY || | |
5629 | cmd == SIOCGMIIREG || | |
5630 | cmd == SIOCSMIIREG || | |
5631 | cmd == SIOCBRADDIF || | |
5632 | cmd == SIOCBRDELIF || | |
5633 | cmd == SIOCSHWTSTAMP || | |
5634 | cmd == SIOCWANDEV) { | |
5635 | err = -EOPNOTSUPP; | |
5636 | if (ops->ndo_do_ioctl) { | |
5637 | if (netif_device_present(dev)) | |
5638 | err = ops->ndo_do_ioctl(dev, ifr, cmd); | |
5639 | else | |
5640 | err = -ENODEV; | |
5641 | } | |
5642 | } else | |
5643 | err = -EINVAL; | |
1da177e4 LT |
5644 | |
5645 | } | |
5646 | return err; | |
5647 | } | |
5648 | ||
5649 | /* | |
5650 | * This function handles all "interface"-type I/O control requests. The actual | |
5651 | * 'doing' part of this is dev_ifsioc above. | |
5652 | */ | |
5653 | ||
5654 | /** | |
5655 | * dev_ioctl - network device ioctl | |
c4ea43c5 | 5656 | * @net: the applicable net namespace |
1da177e4 LT |
5657 | * @cmd: command to issue |
5658 | * @arg: pointer to a struct ifreq in user space | |
5659 | * | |
5660 | * Issue ioctl functions to devices. This is normally called by the | |
5661 | * user space syscall interfaces but can sometimes be useful for | |
5662 | * other purposes. The return value is the return from the syscall if | |
5663 | * positive or a negative errno code on error. | |
5664 | */ | |
5665 | ||
881d966b | 5666 | int dev_ioctl(struct net *net, unsigned int cmd, void __user *arg) |
1da177e4 LT |
5667 | { |
5668 | struct ifreq ifr; | |
5669 | int ret; | |
5670 | char *colon; | |
5671 | ||
5672 | /* One special case: SIOCGIFCONF takes ifconf argument | |
5673 | and requires shared lock, because it sleeps writing | |
5674 | to user space. | |
5675 | */ | |
5676 | ||
5677 | if (cmd == SIOCGIFCONF) { | |
6756ae4b | 5678 | rtnl_lock(); |
881d966b | 5679 | ret = dev_ifconf(net, (char __user *) arg); |
6756ae4b | 5680 | rtnl_unlock(); |
1da177e4 LT |
5681 | return ret; |
5682 | } | |
5683 | if (cmd == SIOCGIFNAME) | |
881d966b | 5684 | return dev_ifname(net, (struct ifreq __user *)arg); |
1da177e4 LT |
5685 | |
5686 | if (copy_from_user(&ifr, arg, sizeof(struct ifreq))) | |
5687 | return -EFAULT; | |
5688 | ||
5689 | ifr.ifr_name[IFNAMSIZ-1] = 0; | |
5690 | ||
5691 | colon = strchr(ifr.ifr_name, ':'); | |
5692 | if (colon) | |
5693 | *colon = 0; | |
5694 | ||
5695 | /* | |
5696 | * See which interface the caller is talking about. | |
5697 | */ | |
5698 | ||
5699 | switch (cmd) { | |
d1b19dff ED |
5700 | /* |
5701 | * These ioctl calls: | |
5702 | * - can be done by all. | |
5703 | * - atomic and do not require locking. | |
5704 | * - return a value | |
5705 | */ | |
5706 | case SIOCGIFFLAGS: | |
5707 | case SIOCGIFMETRIC: | |
5708 | case SIOCGIFMTU: | |
5709 | case SIOCGIFHWADDR: | |
5710 | case SIOCGIFSLAVE: | |
5711 | case SIOCGIFMAP: | |
5712 | case SIOCGIFINDEX: | |
5713 | case SIOCGIFTXQLEN: | |
5714 | dev_load(net, ifr.ifr_name); | |
3710becf | 5715 | rcu_read_lock(); |
d1b19dff | 5716 | ret = dev_ifsioc_locked(net, &ifr, cmd); |
3710becf | 5717 | rcu_read_unlock(); |
d1b19dff ED |
5718 | if (!ret) { |
5719 | if (colon) | |
5720 | *colon = ':'; | |
5721 | if (copy_to_user(arg, &ifr, | |
5722 | sizeof(struct ifreq))) | |
5723 | ret = -EFAULT; | |
5724 | } | |
5725 | return ret; | |
1da177e4 | 5726 | |
d1b19dff ED |
5727 | case SIOCETHTOOL: |
5728 | dev_load(net, ifr.ifr_name); | |
5729 | rtnl_lock(); | |
5730 | ret = dev_ethtool(net, &ifr); | |
5731 | rtnl_unlock(); | |
5732 | if (!ret) { | |
5733 | if (colon) | |
5734 | *colon = ':'; | |
5735 | if (copy_to_user(arg, &ifr, | |
5736 | sizeof(struct ifreq))) | |
5737 | ret = -EFAULT; | |
5738 | } | |
5739 | return ret; | |
1da177e4 | 5740 | |
d1b19dff ED |
5741 | /* |
5742 | * These ioctl calls: | |
5743 | * - require superuser power. | |
5744 | * - require strict serialization. | |
5745 | * - return a value | |
5746 | */ | |
5747 | case SIOCGMIIPHY: | |
5748 | case SIOCGMIIREG: | |
5749 | case SIOCSIFNAME: | |
5e1fccc0 | 5750 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
d1b19dff ED |
5751 | return -EPERM; |
5752 | dev_load(net, ifr.ifr_name); | |
5753 | rtnl_lock(); | |
5754 | ret = dev_ifsioc(net, &ifr, cmd); | |
5755 | rtnl_unlock(); | |
5756 | if (!ret) { | |
5757 | if (colon) | |
5758 | *colon = ':'; | |
5759 | if (copy_to_user(arg, &ifr, | |
5760 | sizeof(struct ifreq))) | |
5761 | ret = -EFAULT; | |
5762 | } | |
5763 | return ret; | |
1da177e4 | 5764 | |
d1b19dff ED |
5765 | /* |
5766 | * These ioctl calls: | |
5767 | * - require superuser power. | |
5768 | * - require strict serialization. | |
5769 | * - do not return a value | |
5770 | */ | |
5e1fccc0 EB |
5771 | case SIOCSIFMAP: |
5772 | case SIOCSIFTXQLEN: | |
5773 | if (!capable(CAP_NET_ADMIN)) | |
5774 | return -EPERM; | |
5775 | /* fall through */ | |
5776 | /* | |
5777 | * These ioctl calls: | |
5778 | * - require local superuser power. | |
5779 | * - require strict serialization. | |
5780 | * - do not return a value | |
5781 | */ | |
d1b19dff ED |
5782 | case SIOCSIFFLAGS: |
5783 | case SIOCSIFMETRIC: | |
5784 | case SIOCSIFMTU: | |
d1b19dff ED |
5785 | case SIOCSIFHWADDR: |
5786 | case SIOCSIFSLAVE: | |
5787 | case SIOCADDMULTI: | |
5788 | case SIOCDELMULTI: | |
5789 | case SIOCSIFHWBROADCAST: | |
d1b19dff ED |
5790 | case SIOCSMIIREG: |
5791 | case SIOCBONDENSLAVE: | |
5792 | case SIOCBONDRELEASE: | |
5793 | case SIOCBONDSETHWADDR: | |
5794 | case SIOCBONDCHANGEACTIVE: | |
5795 | case SIOCBRADDIF: | |
5796 | case SIOCBRDELIF: | |
5797 | case SIOCSHWTSTAMP: | |
5e1fccc0 | 5798 | if (!ns_capable(net->user_ns, CAP_NET_ADMIN)) |
d1b19dff ED |
5799 | return -EPERM; |
5800 | /* fall through */ | |
5801 | case SIOCBONDSLAVEINFOQUERY: | |
5802 | case SIOCBONDINFOQUERY: | |
5803 | dev_load(net, ifr.ifr_name); | |
5804 | rtnl_lock(); | |
5805 | ret = dev_ifsioc(net, &ifr, cmd); | |
5806 | rtnl_unlock(); | |
5807 | return ret; | |
5808 | ||
5809 | case SIOCGIFMEM: | |
5810 | /* Get the per device memory space. We can add this but | |
5811 | * currently do not support it */ | |
5812 | case SIOCSIFMEM: | |
5813 | /* Set the per device memory buffer space. | |
5814 | * Not applicable in our case */ | |
5815 | case SIOCSIFLINK: | |
41c31f31 | 5816 | return -ENOTTY; |
d1b19dff ED |
5817 | |
5818 | /* | |
5819 | * Unknown or private ioctl. | |
5820 | */ | |
5821 | default: | |
5822 | if (cmd == SIOCWANDEV || | |
5823 | (cmd >= SIOCDEVPRIVATE && | |
5824 | cmd <= SIOCDEVPRIVATE + 15)) { | |
881d966b | 5825 | dev_load(net, ifr.ifr_name); |
1da177e4 | 5826 | rtnl_lock(); |
881d966b | 5827 | ret = dev_ifsioc(net, &ifr, cmd); |
1da177e4 | 5828 | rtnl_unlock(); |
d1b19dff ED |
5829 | if (!ret && copy_to_user(arg, &ifr, |
5830 | sizeof(struct ifreq))) | |
5831 | ret = -EFAULT; | |
1da177e4 | 5832 | return ret; |
d1b19dff ED |
5833 | } |
5834 | /* Take care of Wireless Extensions */ | |
5835 | if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) | |
5836 | return wext_handle_ioctl(net, &ifr, cmd, arg); | |
41c31f31 | 5837 | return -ENOTTY; |
1da177e4 LT |
5838 | } |
5839 | } | |
5840 | ||
5841 | ||
5842 | /** | |
5843 | * dev_new_index - allocate an ifindex | |
c4ea43c5 | 5844 | * @net: the applicable net namespace |
1da177e4 LT |
5845 | * |
5846 | * Returns a suitable unique value for a new device interface | |
5847 | * number. The caller must hold the rtnl semaphore or the | |
5848 | * dev_base_lock to be sure it remains unique. | |
5849 | */ | |
881d966b | 5850 | static int dev_new_index(struct net *net) |
1da177e4 | 5851 | { |
aa79e66e | 5852 | int ifindex = net->ifindex; |
1da177e4 LT |
5853 | for (;;) { |
5854 | if (++ifindex <= 0) | |
5855 | ifindex = 1; | |
881d966b | 5856 | if (!__dev_get_by_index(net, ifindex)) |
aa79e66e | 5857 | return net->ifindex = ifindex; |
1da177e4 LT |
5858 | } |
5859 | } | |
5860 | ||
1da177e4 | 5861 | /* Delayed registration/unregisteration */ |
3b5b34fd | 5862 | static LIST_HEAD(net_todo_list); |
1da177e4 | 5863 | |
6f05f629 | 5864 | static void net_set_todo(struct net_device *dev) |
1da177e4 | 5865 | { |
1da177e4 | 5866 | list_add_tail(&dev->todo_list, &net_todo_list); |
1da177e4 LT |
5867 | } |
5868 | ||
9b5e383c | 5869 | static void rollback_registered_many(struct list_head *head) |
93ee31f1 | 5870 | { |
e93737b0 | 5871 | struct net_device *dev, *tmp; |
9b5e383c | 5872 | |
93ee31f1 DL |
5873 | BUG_ON(dev_boot_phase); |
5874 | ASSERT_RTNL(); | |
5875 | ||
e93737b0 | 5876 | list_for_each_entry_safe(dev, tmp, head, unreg_list) { |
9b5e383c | 5877 | /* Some devices call without registering |
e93737b0 KK |
5878 | * for initialization unwind. Remove those |
5879 | * devices and proceed with the remaining. | |
9b5e383c ED |
5880 | */ |
5881 | if (dev->reg_state == NETREG_UNINITIALIZED) { | |
7b6cd1ce JP |
5882 | pr_debug("unregister_netdevice: device %s/%p never was registered\n", |
5883 | dev->name, dev); | |
93ee31f1 | 5884 | |
9b5e383c | 5885 | WARN_ON(1); |
e93737b0 KK |
5886 | list_del(&dev->unreg_list); |
5887 | continue; | |
9b5e383c | 5888 | } |
449f4544 | 5889 | dev->dismantle = true; |
9b5e383c | 5890 | BUG_ON(dev->reg_state != NETREG_REGISTERED); |
44345724 | 5891 | } |
93ee31f1 | 5892 | |
44345724 OP |
5893 | /* If device is running, close it first. */ |
5894 | dev_close_many(head); | |
93ee31f1 | 5895 | |
44345724 | 5896 | list_for_each_entry(dev, head, unreg_list) { |
9b5e383c ED |
5897 | /* And unlink it from device chain. */ |
5898 | unlist_netdevice(dev); | |
93ee31f1 | 5899 | |
9b5e383c ED |
5900 | dev->reg_state = NETREG_UNREGISTERING; |
5901 | } | |
93ee31f1 DL |
5902 | |
5903 | synchronize_net(); | |
5904 | ||
9b5e383c ED |
5905 | list_for_each_entry(dev, head, unreg_list) { |
5906 | /* Shutdown queueing discipline. */ | |
5907 | dev_shutdown(dev); | |
93ee31f1 DL |
5908 | |
5909 | ||
9b5e383c ED |
5910 | /* Notify protocols, that we are about to destroy |
5911 | this device. They should clean all the things. | |
5912 | */ | |
5913 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); | |
93ee31f1 | 5914 | |
a2835763 PM |
5915 | if (!dev->rtnl_link_ops || |
5916 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) | |
5917 | rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); | |
5918 | ||
9b5e383c ED |
5919 | /* |
5920 | * Flush the unicast and multicast chains | |
5921 | */ | |
a748ee24 | 5922 | dev_uc_flush(dev); |
22bedad3 | 5923 | dev_mc_flush(dev); |
93ee31f1 | 5924 | |
9b5e383c ED |
5925 | if (dev->netdev_ops->ndo_uninit) |
5926 | dev->netdev_ops->ndo_uninit(dev); | |
93ee31f1 | 5927 | |
9ff162a8 JP |
5928 | /* Notifier chain MUST detach us all upper devices. */ |
5929 | WARN_ON(netdev_has_any_upper_dev(dev)); | |
93ee31f1 | 5930 | |
9b5e383c ED |
5931 | /* Remove entries from kobject tree */ |
5932 | netdev_unregister_kobject(dev); | |
024e9679 AD |
5933 | #ifdef CONFIG_XPS |
5934 | /* Remove XPS queueing entries */ | |
5935 | netif_reset_xps_queues_gt(dev, 0); | |
5936 | #endif | |
9b5e383c | 5937 | } |
93ee31f1 | 5938 | |
850a545b | 5939 | synchronize_net(); |
395264d5 | 5940 | |
a5ee1551 | 5941 | list_for_each_entry(dev, head, unreg_list) |
9b5e383c ED |
5942 | dev_put(dev); |
5943 | } | |
5944 | ||
5945 | static void rollback_registered(struct net_device *dev) | |
5946 | { | |
5947 | LIST_HEAD(single); | |
5948 | ||
5949 | list_add(&dev->unreg_list, &single); | |
5950 | rollback_registered_many(&single); | |
ceaaec98 | 5951 | list_del(&single); |
93ee31f1 DL |
5952 | } |
5953 | ||
c8f44aff MM |
5954 | static netdev_features_t netdev_fix_features(struct net_device *dev, |
5955 | netdev_features_t features) | |
b63365a2 | 5956 | { |
57422dc5 MM |
5957 | /* Fix illegal checksum combinations */ |
5958 | if ((features & NETIF_F_HW_CSUM) && | |
5959 | (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) { | |
6f404e44 | 5960 | netdev_warn(dev, "mixed HW and IP checksum settings.\n"); |
57422dc5 MM |
5961 | features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM); |
5962 | } | |
5963 | ||
b63365a2 HX |
5964 | /* Fix illegal SG+CSUM combinations. */ |
5965 | if ((features & NETIF_F_SG) && | |
5966 | !(features & NETIF_F_ALL_CSUM)) { | |
6f404e44 MM |
5967 | netdev_dbg(dev, |
5968 | "Dropping NETIF_F_SG since no checksum feature.\n"); | |
b63365a2 HX |
5969 | features &= ~NETIF_F_SG; |
5970 | } | |
5971 | ||
5972 | /* TSO requires that SG is present as well. */ | |
ea2d3688 | 5973 | if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) { |
6f404e44 | 5974 | netdev_dbg(dev, "Dropping TSO features since no SG feature.\n"); |
ea2d3688 | 5975 | features &= ~NETIF_F_ALL_TSO; |
b63365a2 HX |
5976 | } |
5977 | ||
31d8b9e0 BH |
5978 | /* TSO ECN requires that TSO is present as well. */ |
5979 | if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN) | |
5980 | features &= ~NETIF_F_TSO_ECN; | |
5981 | ||
212b573f MM |
5982 | /* Software GSO depends on SG. */ |
5983 | if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) { | |
6f404e44 | 5984 | netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n"); |
212b573f MM |
5985 | features &= ~NETIF_F_GSO; |
5986 | } | |
5987 | ||
acd1130e | 5988 | /* UFO needs SG and checksumming */ |
b63365a2 | 5989 | if (features & NETIF_F_UFO) { |
79032644 MM |
5990 | /* maybe split UFO into V4 and V6? */ |
5991 | if (!((features & NETIF_F_GEN_CSUM) || | |
5992 | (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM)) | |
5993 | == (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) { | |
6f404e44 | 5994 | netdev_dbg(dev, |
acd1130e | 5995 | "Dropping NETIF_F_UFO since no checksum offload features.\n"); |
b63365a2 HX |
5996 | features &= ~NETIF_F_UFO; |
5997 | } | |
5998 | ||
5999 | if (!(features & NETIF_F_SG)) { | |
6f404e44 | 6000 | netdev_dbg(dev, |
acd1130e | 6001 | "Dropping NETIF_F_UFO since no NETIF_F_SG feature.\n"); |
b63365a2 HX |
6002 | features &= ~NETIF_F_UFO; |
6003 | } | |
6004 | } | |
6005 | ||
6006 | return features; | |
6007 | } | |
b63365a2 | 6008 | |
6cb6a27c | 6009 | int __netdev_update_features(struct net_device *dev) |
5455c699 | 6010 | { |
c8f44aff | 6011 | netdev_features_t features; |
5455c699 MM |
6012 | int err = 0; |
6013 | ||
87267485 MM |
6014 | ASSERT_RTNL(); |
6015 | ||
5455c699 MM |
6016 | features = netdev_get_wanted_features(dev); |
6017 | ||
6018 | if (dev->netdev_ops->ndo_fix_features) | |
6019 | features = dev->netdev_ops->ndo_fix_features(dev, features); | |
6020 | ||
6021 | /* driver might be less strict about feature dependencies */ | |
6022 | features = netdev_fix_features(dev, features); | |
6023 | ||
6024 | if (dev->features == features) | |
6cb6a27c | 6025 | return 0; |
5455c699 | 6026 | |
c8f44aff MM |
6027 | netdev_dbg(dev, "Features changed: %pNF -> %pNF\n", |
6028 | &dev->features, &features); | |
5455c699 MM |
6029 | |
6030 | if (dev->netdev_ops->ndo_set_features) | |
6031 | err = dev->netdev_ops->ndo_set_features(dev, features); | |
6032 | ||
6cb6a27c | 6033 | if (unlikely(err < 0)) { |
5455c699 | 6034 | netdev_err(dev, |
c8f44aff MM |
6035 | "set_features() failed (%d); wanted %pNF, left %pNF\n", |
6036 | err, &features, &dev->features); | |
6cb6a27c MM |
6037 | return -1; |
6038 | } | |
6039 | ||
6040 | if (!err) | |
6041 | dev->features = features; | |
6042 | ||
6043 | return 1; | |
6044 | } | |
6045 | ||
afe12cc8 MM |
6046 | /** |
6047 | * netdev_update_features - recalculate device features | |
6048 | * @dev: the device to check | |
6049 | * | |
6050 | * Recalculate dev->features set and send notifications if it | |
6051 | * has changed. Should be called after driver or hardware dependent | |
6052 | * conditions might have changed that influence the features. | |
6053 | */ | |
6cb6a27c MM |
6054 | void netdev_update_features(struct net_device *dev) |
6055 | { | |
6056 | if (__netdev_update_features(dev)) | |
6057 | netdev_features_change(dev); | |
5455c699 MM |
6058 | } |
6059 | EXPORT_SYMBOL(netdev_update_features); | |
6060 | ||
afe12cc8 MM |
6061 | /** |
6062 | * netdev_change_features - recalculate device features | |
6063 | * @dev: the device to check | |
6064 | * | |
6065 | * Recalculate dev->features set and send notifications even | |
6066 | * if they have not changed. Should be called instead of | |
6067 | * netdev_update_features() if also dev->vlan_features might | |
6068 | * have changed to allow the changes to be propagated to stacked | |
6069 | * VLAN devices. | |
6070 | */ | |
6071 | void netdev_change_features(struct net_device *dev) | |
6072 | { | |
6073 | __netdev_update_features(dev); | |
6074 | netdev_features_change(dev); | |
6075 | } | |
6076 | EXPORT_SYMBOL(netdev_change_features); | |
6077 | ||
fc4a7489 PM |
6078 | /** |
6079 | * netif_stacked_transfer_operstate - transfer operstate | |
6080 | * @rootdev: the root or lower level device to transfer state from | |
6081 | * @dev: the device to transfer operstate to | |
6082 | * | |
6083 | * Transfer operational state from root to device. This is normally | |
6084 | * called when a stacking relationship exists between the root | |
6085 | * device and the device(a leaf device). | |
6086 | */ | |
6087 | void netif_stacked_transfer_operstate(const struct net_device *rootdev, | |
6088 | struct net_device *dev) | |
6089 | { | |
6090 | if (rootdev->operstate == IF_OPER_DORMANT) | |
6091 | netif_dormant_on(dev); | |
6092 | else | |
6093 | netif_dormant_off(dev); | |
6094 | ||
6095 | if (netif_carrier_ok(rootdev)) { | |
6096 | if (!netif_carrier_ok(dev)) | |
6097 | netif_carrier_on(dev); | |
6098 | } else { | |
6099 | if (netif_carrier_ok(dev)) | |
6100 | netif_carrier_off(dev); | |
6101 | } | |
6102 | } | |
6103 | EXPORT_SYMBOL(netif_stacked_transfer_operstate); | |
6104 | ||
bf264145 | 6105 | #ifdef CONFIG_RPS |
1b4bf461 ED |
6106 | static int netif_alloc_rx_queues(struct net_device *dev) |
6107 | { | |
1b4bf461 | 6108 | unsigned int i, count = dev->num_rx_queues; |
bd25fa7b | 6109 | struct netdev_rx_queue *rx; |
1b4bf461 | 6110 | |
bd25fa7b | 6111 | BUG_ON(count < 1); |
1b4bf461 | 6112 | |
bd25fa7b TH |
6113 | rx = kcalloc(count, sizeof(struct netdev_rx_queue), GFP_KERNEL); |
6114 | if (!rx) { | |
7b6cd1ce | 6115 | pr_err("netdev: Unable to allocate %u rx queues\n", count); |
bd25fa7b | 6116 | return -ENOMEM; |
1b4bf461 | 6117 | } |
bd25fa7b TH |
6118 | dev->_rx = rx; |
6119 | ||
bd25fa7b | 6120 | for (i = 0; i < count; i++) |
fe822240 | 6121 | rx[i].dev = dev; |
1b4bf461 ED |
6122 | return 0; |
6123 | } | |
bf264145 | 6124 | #endif |
1b4bf461 | 6125 | |
aa942104 CG |
6126 | static void netdev_init_one_queue(struct net_device *dev, |
6127 | struct netdev_queue *queue, void *_unused) | |
6128 | { | |
6129 | /* Initialize queue lock */ | |
6130 | spin_lock_init(&queue->_xmit_lock); | |
6131 | netdev_set_xmit_lockdep_class(&queue->_xmit_lock, dev->type); | |
6132 | queue->xmit_lock_owner = -1; | |
b236da69 | 6133 | netdev_queue_numa_node_write(queue, NUMA_NO_NODE); |
aa942104 | 6134 | queue->dev = dev; |
114cf580 TH |
6135 | #ifdef CONFIG_BQL |
6136 | dql_init(&queue->dql, HZ); | |
6137 | #endif | |
aa942104 CG |
6138 | } |
6139 | ||
e6484930 TH |
6140 | static int netif_alloc_netdev_queues(struct net_device *dev) |
6141 | { | |
6142 | unsigned int count = dev->num_tx_queues; | |
6143 | struct netdev_queue *tx; | |
6144 | ||
6145 | BUG_ON(count < 1); | |
6146 | ||
6147 | tx = kcalloc(count, sizeof(struct netdev_queue), GFP_KERNEL); | |
6148 | if (!tx) { | |
7b6cd1ce | 6149 | pr_err("netdev: Unable to allocate %u tx queues\n", count); |
e6484930 TH |
6150 | return -ENOMEM; |
6151 | } | |
6152 | dev->_tx = tx; | |
1d24eb48 | 6153 | |
e6484930 TH |
6154 | netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL); |
6155 | spin_lock_init(&dev->tx_global_lock); | |
aa942104 CG |
6156 | |
6157 | return 0; | |
e6484930 TH |
6158 | } |
6159 | ||
1da177e4 LT |
6160 | /** |
6161 | * register_netdevice - register a network device | |
6162 | * @dev: device to register | |
6163 | * | |
6164 | * Take a completed network device structure and add it to the kernel | |
6165 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier | |
6166 | * chain. 0 is returned on success. A negative errno code is returned | |
6167 | * on a failure to set up the device, or if the name is a duplicate. | |
6168 | * | |
6169 | * Callers must hold the rtnl semaphore. You may want | |
6170 | * register_netdev() instead of this. | |
6171 | * | |
6172 | * BUGS: | |
6173 | * The locking appears insufficient to guarantee two parallel registers | |
6174 | * will not get the same name. | |
6175 | */ | |
6176 | ||
6177 | int register_netdevice(struct net_device *dev) | |
6178 | { | |
1da177e4 | 6179 | int ret; |
d314774c | 6180 | struct net *net = dev_net(dev); |
1da177e4 LT |
6181 | |
6182 | BUG_ON(dev_boot_phase); | |
6183 | ASSERT_RTNL(); | |
6184 | ||
b17a7c17 SH |
6185 | might_sleep(); |
6186 | ||
1da177e4 LT |
6187 | /* When net_device's are persistent, this will be fatal. */ |
6188 | BUG_ON(dev->reg_state != NETREG_UNINITIALIZED); | |
d314774c | 6189 | BUG_ON(!net); |
1da177e4 | 6190 | |
f1f28aa3 | 6191 | spin_lock_init(&dev->addr_list_lock); |
cf508b12 | 6192 | netdev_set_addr_lockdep_class(dev); |
1da177e4 | 6193 | |
1da177e4 LT |
6194 | dev->iflink = -1; |
6195 | ||
828de4f6 | 6196 | ret = dev_get_valid_name(net, dev, dev->name); |
0696c3a8 PP |
6197 | if (ret < 0) |
6198 | goto out; | |
6199 | ||
1da177e4 | 6200 | /* Init, if this function is available */ |
d314774c SH |
6201 | if (dev->netdev_ops->ndo_init) { |
6202 | ret = dev->netdev_ops->ndo_init(dev); | |
1da177e4 LT |
6203 | if (ret) { |
6204 | if (ret > 0) | |
6205 | ret = -EIO; | |
90833aa4 | 6206 | goto out; |
1da177e4 LT |
6207 | } |
6208 | } | |
4ec93edb | 6209 | |
9c7dafbf PE |
6210 | ret = -EBUSY; |
6211 | if (!dev->ifindex) | |
6212 | dev->ifindex = dev_new_index(net); | |
6213 | else if (__dev_get_by_index(net, dev->ifindex)) | |
6214 | goto err_uninit; | |
6215 | ||
1da177e4 LT |
6216 | if (dev->iflink == -1) |
6217 | dev->iflink = dev->ifindex; | |
6218 | ||
5455c699 MM |
6219 | /* Transfer changeable features to wanted_features and enable |
6220 | * software offloads (GSO and GRO). | |
6221 | */ | |
6222 | dev->hw_features |= NETIF_F_SOFT_FEATURES; | |
14d1232f MM |
6223 | dev->features |= NETIF_F_SOFT_FEATURES; |
6224 | dev->wanted_features = dev->features & dev->hw_features; | |
1da177e4 | 6225 | |
c6e1a0d1 | 6226 | /* Turn on no cache copy if HW is doing checksum */ |
34324dc2 MM |
6227 | if (!(dev->flags & IFF_LOOPBACK)) { |
6228 | dev->hw_features |= NETIF_F_NOCACHE_COPY; | |
6229 | if (dev->features & NETIF_F_ALL_CSUM) { | |
6230 | dev->wanted_features |= NETIF_F_NOCACHE_COPY; | |
6231 | dev->features |= NETIF_F_NOCACHE_COPY; | |
6232 | } | |
c6e1a0d1 TH |
6233 | } |
6234 | ||
1180e7d6 | 6235 | /* Make NETIF_F_HIGHDMA inheritable to VLAN devices. |
16c3ea78 | 6236 | */ |
1180e7d6 | 6237 | dev->vlan_features |= NETIF_F_HIGHDMA; |
16c3ea78 | 6238 | |
7ffbe3fd JB |
6239 | ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev); |
6240 | ret = notifier_to_errno(ret); | |
6241 | if (ret) | |
6242 | goto err_uninit; | |
6243 | ||
8b41d188 | 6244 | ret = netdev_register_kobject(dev); |
b17a7c17 | 6245 | if (ret) |
7ce1b0ed | 6246 | goto err_uninit; |
b17a7c17 SH |
6247 | dev->reg_state = NETREG_REGISTERED; |
6248 | ||
6cb6a27c | 6249 | __netdev_update_features(dev); |
8e9b59b2 | 6250 | |
1da177e4 LT |
6251 | /* |
6252 | * Default initial state at registry is that the | |
6253 | * device is present. | |
6254 | */ | |
6255 | ||
6256 | set_bit(__LINK_STATE_PRESENT, &dev->state); | |
6257 | ||
8f4cccbb BH |
6258 | linkwatch_init_dev(dev); |
6259 | ||
1da177e4 | 6260 | dev_init_scheduler(dev); |
1da177e4 | 6261 | dev_hold(dev); |
ce286d32 | 6262 | list_netdevice(dev); |
7bf23575 | 6263 | add_device_randomness(dev->dev_addr, dev->addr_len); |
1da177e4 | 6264 | |
948b337e JP |
6265 | /* If the device has permanent device address, driver should |
6266 | * set dev_addr and also addr_assign_type should be set to | |
6267 | * NET_ADDR_PERM (default value). | |
6268 | */ | |
6269 | if (dev->addr_assign_type == NET_ADDR_PERM) | |
6270 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); | |
6271 | ||
1da177e4 | 6272 | /* Notify protocols, that a new device appeared. */ |
056925ab | 6273 | ret = call_netdevice_notifiers(NETDEV_REGISTER, dev); |
fcc5a03a | 6274 | ret = notifier_to_errno(ret); |
93ee31f1 DL |
6275 | if (ret) { |
6276 | rollback_registered(dev); | |
6277 | dev->reg_state = NETREG_UNREGISTERED; | |
6278 | } | |
d90a909e EB |
6279 | /* |
6280 | * Prevent userspace races by waiting until the network | |
6281 | * device is fully setup before sending notifications. | |
6282 | */ | |
a2835763 PM |
6283 | if (!dev->rtnl_link_ops || |
6284 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) | |
6285 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); | |
1da177e4 LT |
6286 | |
6287 | out: | |
6288 | return ret; | |
7ce1b0ed HX |
6289 | |
6290 | err_uninit: | |
d314774c SH |
6291 | if (dev->netdev_ops->ndo_uninit) |
6292 | dev->netdev_ops->ndo_uninit(dev); | |
7ce1b0ed | 6293 | goto out; |
1da177e4 | 6294 | } |
d1b19dff | 6295 | EXPORT_SYMBOL(register_netdevice); |
1da177e4 | 6296 | |
937f1ba5 BH |
6297 | /** |
6298 | * init_dummy_netdev - init a dummy network device for NAPI | |
6299 | * @dev: device to init | |
6300 | * | |
6301 | * This takes a network device structure and initialize the minimum | |
6302 | * amount of fields so it can be used to schedule NAPI polls without | |
6303 | * registering a full blown interface. This is to be used by drivers | |
6304 | * that need to tie several hardware interfaces to a single NAPI | |
6305 | * poll scheduler due to HW limitations. | |
6306 | */ | |
6307 | int init_dummy_netdev(struct net_device *dev) | |
6308 | { | |
6309 | /* Clear everything. Note we don't initialize spinlocks | |
6310 | * are they aren't supposed to be taken by any of the | |
6311 | * NAPI code and this dummy netdev is supposed to be | |
6312 | * only ever used for NAPI polls | |
6313 | */ | |
6314 | memset(dev, 0, sizeof(struct net_device)); | |
6315 | ||
6316 | /* make sure we BUG if trying to hit standard | |
6317 | * register/unregister code path | |
6318 | */ | |
6319 | dev->reg_state = NETREG_DUMMY; | |
6320 | ||
937f1ba5 BH |
6321 | /* NAPI wants this */ |
6322 | INIT_LIST_HEAD(&dev->napi_list); | |
6323 | ||
6324 | /* a dummy interface is started by default */ | |
6325 | set_bit(__LINK_STATE_PRESENT, &dev->state); | |
6326 | set_bit(__LINK_STATE_START, &dev->state); | |
6327 | ||
29b4433d ED |
6328 | /* Note : We dont allocate pcpu_refcnt for dummy devices, |
6329 | * because users of this 'device' dont need to change | |
6330 | * its refcount. | |
6331 | */ | |
6332 | ||
937f1ba5 BH |
6333 | return 0; |
6334 | } | |
6335 | EXPORT_SYMBOL_GPL(init_dummy_netdev); | |
6336 | ||
6337 | ||
1da177e4 LT |
6338 | /** |
6339 | * register_netdev - register a network device | |
6340 | * @dev: device to register | |
6341 | * | |
6342 | * Take a completed network device structure and add it to the kernel | |
6343 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier | |
6344 | * chain. 0 is returned on success. A negative errno code is returned | |
6345 | * on a failure to set up the device, or if the name is a duplicate. | |
6346 | * | |
38b4da38 | 6347 | * This is a wrapper around register_netdevice that takes the rtnl semaphore |
1da177e4 LT |
6348 | * and expands the device name if you passed a format string to |
6349 | * alloc_netdev. | |
6350 | */ | |
6351 | int register_netdev(struct net_device *dev) | |
6352 | { | |
6353 | int err; | |
6354 | ||
6355 | rtnl_lock(); | |
1da177e4 | 6356 | err = register_netdevice(dev); |
1da177e4 LT |
6357 | rtnl_unlock(); |
6358 | return err; | |
6359 | } | |
6360 | EXPORT_SYMBOL(register_netdev); | |
6361 | ||
29b4433d ED |
6362 | int netdev_refcnt_read(const struct net_device *dev) |
6363 | { | |
6364 | int i, refcnt = 0; | |
6365 | ||
6366 | for_each_possible_cpu(i) | |
6367 | refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i); | |
6368 | return refcnt; | |
6369 | } | |
6370 | EXPORT_SYMBOL(netdev_refcnt_read); | |
6371 | ||
2c53040f | 6372 | /** |
1da177e4 | 6373 | * netdev_wait_allrefs - wait until all references are gone. |
3de7a37b | 6374 | * @dev: target net_device |
1da177e4 LT |
6375 | * |
6376 | * This is called when unregistering network devices. | |
6377 | * | |
6378 | * Any protocol or device that holds a reference should register | |
6379 | * for netdevice notification, and cleanup and put back the | |
6380 | * reference if they receive an UNREGISTER event. | |
6381 | * We can get stuck here if buggy protocols don't correctly | |
4ec93edb | 6382 | * call dev_put. |
1da177e4 LT |
6383 | */ |
6384 | static void netdev_wait_allrefs(struct net_device *dev) | |
6385 | { | |
6386 | unsigned long rebroadcast_time, warning_time; | |
29b4433d | 6387 | int refcnt; |
1da177e4 | 6388 | |
e014debe ED |
6389 | linkwatch_forget_dev(dev); |
6390 | ||
1da177e4 | 6391 | rebroadcast_time = warning_time = jiffies; |
29b4433d ED |
6392 | refcnt = netdev_refcnt_read(dev); |
6393 | ||
6394 | while (refcnt != 0) { | |
1da177e4 | 6395 | if (time_after(jiffies, rebroadcast_time + 1 * HZ)) { |
6756ae4b | 6396 | rtnl_lock(); |
1da177e4 LT |
6397 | |
6398 | /* Rebroadcast unregister notification */ | |
056925ab | 6399 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
1da177e4 | 6400 | |
748e2d93 | 6401 | __rtnl_unlock(); |
0115e8e3 | 6402 | rcu_barrier(); |
748e2d93 ED |
6403 | rtnl_lock(); |
6404 | ||
0115e8e3 | 6405 | call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev); |
1da177e4 LT |
6406 | if (test_bit(__LINK_STATE_LINKWATCH_PENDING, |
6407 | &dev->state)) { | |
6408 | /* We must not have linkwatch events | |
6409 | * pending on unregister. If this | |
6410 | * happens, we simply run the queue | |
6411 | * unscheduled, resulting in a noop | |
6412 | * for this device. | |
6413 | */ | |
6414 | linkwatch_run_queue(); | |
6415 | } | |
6416 | ||
6756ae4b | 6417 | __rtnl_unlock(); |
1da177e4 LT |
6418 | |
6419 | rebroadcast_time = jiffies; | |
6420 | } | |
6421 | ||
6422 | msleep(250); | |
6423 | ||
29b4433d ED |
6424 | refcnt = netdev_refcnt_read(dev); |
6425 | ||
1da177e4 | 6426 | if (time_after(jiffies, warning_time + 10 * HZ)) { |
7b6cd1ce JP |
6427 | pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n", |
6428 | dev->name, refcnt); | |
1da177e4 LT |
6429 | warning_time = jiffies; |
6430 | } | |
6431 | } | |
6432 | } | |
6433 | ||
6434 | /* The sequence is: | |
6435 | * | |
6436 | * rtnl_lock(); | |
6437 | * ... | |
6438 | * register_netdevice(x1); | |
6439 | * register_netdevice(x2); | |
6440 | * ... | |
6441 | * unregister_netdevice(y1); | |
6442 | * unregister_netdevice(y2); | |
6443 | * ... | |
6444 | * rtnl_unlock(); | |
6445 | * free_netdev(y1); | |
6446 | * free_netdev(y2); | |
6447 | * | |
58ec3b4d | 6448 | * We are invoked by rtnl_unlock(). |
1da177e4 | 6449 | * This allows us to deal with problems: |
b17a7c17 | 6450 | * 1) We can delete sysfs objects which invoke hotplug |
1da177e4 LT |
6451 | * without deadlocking with linkwatch via keventd. |
6452 | * 2) Since we run with the RTNL semaphore not held, we can sleep | |
6453 | * safely in order to wait for the netdev refcnt to drop to zero. | |
58ec3b4d HX |
6454 | * |
6455 | * We must not return until all unregister events added during | |
6456 | * the interval the lock was held have been completed. | |
1da177e4 | 6457 | */ |
1da177e4 LT |
6458 | void netdev_run_todo(void) |
6459 | { | |
626ab0e6 | 6460 | struct list_head list; |
1da177e4 | 6461 | |
1da177e4 | 6462 | /* Snapshot list, allow later requests */ |
626ab0e6 | 6463 | list_replace_init(&net_todo_list, &list); |
58ec3b4d HX |
6464 | |
6465 | __rtnl_unlock(); | |
626ab0e6 | 6466 | |
0115e8e3 ED |
6467 | |
6468 | /* Wait for rcu callbacks to finish before next phase */ | |
850a545b EB |
6469 | if (!list_empty(&list)) |
6470 | rcu_barrier(); | |
6471 | ||
1da177e4 LT |
6472 | while (!list_empty(&list)) { |
6473 | struct net_device *dev | |
e5e26d75 | 6474 | = list_first_entry(&list, struct net_device, todo_list); |
1da177e4 LT |
6475 | list_del(&dev->todo_list); |
6476 | ||
748e2d93 | 6477 | rtnl_lock(); |
0115e8e3 | 6478 | call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev); |
748e2d93 | 6479 | __rtnl_unlock(); |
0115e8e3 | 6480 | |
b17a7c17 | 6481 | if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) { |
7b6cd1ce | 6482 | pr_err("network todo '%s' but state %d\n", |
b17a7c17 SH |
6483 | dev->name, dev->reg_state); |
6484 | dump_stack(); | |
6485 | continue; | |
6486 | } | |
1da177e4 | 6487 | |
b17a7c17 | 6488 | dev->reg_state = NETREG_UNREGISTERED; |
1da177e4 | 6489 | |
152102c7 | 6490 | on_each_cpu(flush_backlog, dev, 1); |
6e583ce5 | 6491 | |
b17a7c17 | 6492 | netdev_wait_allrefs(dev); |
1da177e4 | 6493 | |
b17a7c17 | 6494 | /* paranoia */ |
29b4433d | 6495 | BUG_ON(netdev_refcnt_read(dev)); |
33d480ce ED |
6496 | WARN_ON(rcu_access_pointer(dev->ip_ptr)); |
6497 | WARN_ON(rcu_access_pointer(dev->ip6_ptr)); | |
547b792c | 6498 | WARN_ON(dev->dn_ptr); |
1da177e4 | 6499 | |
b17a7c17 SH |
6500 | if (dev->destructor) |
6501 | dev->destructor(dev); | |
9093bbb2 SH |
6502 | |
6503 | /* Free network device */ | |
6504 | kobject_put(&dev->dev.kobj); | |
1da177e4 | 6505 | } |
1da177e4 LT |
6506 | } |
6507 | ||
3cfde79c BH |
6508 | /* Convert net_device_stats to rtnl_link_stats64. They have the same |
6509 | * fields in the same order, with only the type differing. | |
6510 | */ | |
77a1abf5 ED |
6511 | void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64, |
6512 | const struct net_device_stats *netdev_stats) | |
3cfde79c BH |
6513 | { |
6514 | #if BITS_PER_LONG == 64 | |
77a1abf5 ED |
6515 | BUILD_BUG_ON(sizeof(*stats64) != sizeof(*netdev_stats)); |
6516 | memcpy(stats64, netdev_stats, sizeof(*stats64)); | |
3cfde79c BH |
6517 | #else |
6518 | size_t i, n = sizeof(*stats64) / sizeof(u64); | |
6519 | const unsigned long *src = (const unsigned long *)netdev_stats; | |
6520 | u64 *dst = (u64 *)stats64; | |
6521 | ||
6522 | BUILD_BUG_ON(sizeof(*netdev_stats) / sizeof(unsigned long) != | |
6523 | sizeof(*stats64) / sizeof(u64)); | |
6524 | for (i = 0; i < n; i++) | |
6525 | dst[i] = src[i]; | |
6526 | #endif | |
6527 | } | |
77a1abf5 | 6528 | EXPORT_SYMBOL(netdev_stats_to_stats64); |
3cfde79c | 6529 | |
eeda3fd6 SH |
6530 | /** |
6531 | * dev_get_stats - get network device statistics | |
6532 | * @dev: device to get statistics from | |
28172739 | 6533 | * @storage: place to store stats |
eeda3fd6 | 6534 | * |
d7753516 BH |
6535 | * Get network statistics from device. Return @storage. |
6536 | * The device driver may provide its own method by setting | |
6537 | * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats; | |
6538 | * otherwise the internal statistics structure is used. | |
eeda3fd6 | 6539 | */ |
d7753516 BH |
6540 | struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev, |
6541 | struct rtnl_link_stats64 *storage) | |
7004bf25 | 6542 | { |
eeda3fd6 SH |
6543 | const struct net_device_ops *ops = dev->netdev_ops; |
6544 | ||
28172739 ED |
6545 | if (ops->ndo_get_stats64) { |
6546 | memset(storage, 0, sizeof(*storage)); | |
caf586e5 ED |
6547 | ops->ndo_get_stats64(dev, storage); |
6548 | } else if (ops->ndo_get_stats) { | |
3cfde79c | 6549 | netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev)); |
caf586e5 ED |
6550 | } else { |
6551 | netdev_stats_to_stats64(storage, &dev->stats); | |
28172739 | 6552 | } |
caf586e5 | 6553 | storage->rx_dropped += atomic_long_read(&dev->rx_dropped); |
28172739 | 6554 | return storage; |
c45d286e | 6555 | } |
eeda3fd6 | 6556 | EXPORT_SYMBOL(dev_get_stats); |
c45d286e | 6557 | |
24824a09 | 6558 | struct netdev_queue *dev_ingress_queue_create(struct net_device *dev) |
dc2b4847 | 6559 | { |
24824a09 | 6560 | struct netdev_queue *queue = dev_ingress_queue(dev); |
dc2b4847 | 6561 | |
24824a09 ED |
6562 | #ifdef CONFIG_NET_CLS_ACT |
6563 | if (queue) | |
6564 | return queue; | |
6565 | queue = kzalloc(sizeof(*queue), GFP_KERNEL); | |
6566 | if (!queue) | |
6567 | return NULL; | |
6568 | netdev_init_one_queue(dev, queue, NULL); | |
24824a09 ED |
6569 | queue->qdisc = &noop_qdisc; |
6570 | queue->qdisc_sleeping = &noop_qdisc; | |
6571 | rcu_assign_pointer(dev->ingress_queue, queue); | |
6572 | #endif | |
6573 | return queue; | |
bb949fbd DM |
6574 | } |
6575 | ||
2c60db03 ED |
6576 | static const struct ethtool_ops default_ethtool_ops; |
6577 | ||
d07d7507 SG |
6578 | void netdev_set_default_ethtool_ops(struct net_device *dev, |
6579 | const struct ethtool_ops *ops) | |
6580 | { | |
6581 | if (dev->ethtool_ops == &default_ethtool_ops) | |
6582 | dev->ethtool_ops = ops; | |
6583 | } | |
6584 | EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops); | |
6585 | ||
1da177e4 | 6586 | /** |
36909ea4 | 6587 | * alloc_netdev_mqs - allocate network device |
1da177e4 LT |
6588 | * @sizeof_priv: size of private data to allocate space for |
6589 | * @name: device name format string | |
6590 | * @setup: callback to initialize device | |
36909ea4 TH |
6591 | * @txqs: the number of TX subqueues to allocate |
6592 | * @rxqs: the number of RX subqueues to allocate | |
1da177e4 LT |
6593 | * |
6594 | * Allocates a struct net_device with private data area for driver use | |
f25f4e44 | 6595 | * and performs basic initialization. Also allocates subquue structs |
36909ea4 | 6596 | * for each queue on the device. |
1da177e4 | 6597 | */ |
36909ea4 TH |
6598 | struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name, |
6599 | void (*setup)(struct net_device *), | |
6600 | unsigned int txqs, unsigned int rxqs) | |
1da177e4 | 6601 | { |
1da177e4 | 6602 | struct net_device *dev; |
7943986c | 6603 | size_t alloc_size; |
1ce8e7b5 | 6604 | struct net_device *p; |
1da177e4 | 6605 | |
b6fe17d6 SH |
6606 | BUG_ON(strlen(name) >= sizeof(dev->name)); |
6607 | ||
36909ea4 | 6608 | if (txqs < 1) { |
7b6cd1ce | 6609 | pr_err("alloc_netdev: Unable to allocate device with zero queues\n"); |
55513fb4 TH |
6610 | return NULL; |
6611 | } | |
6612 | ||
36909ea4 TH |
6613 | #ifdef CONFIG_RPS |
6614 | if (rxqs < 1) { | |
7b6cd1ce | 6615 | pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n"); |
36909ea4 TH |
6616 | return NULL; |
6617 | } | |
6618 | #endif | |
6619 | ||
fd2ea0a7 | 6620 | alloc_size = sizeof(struct net_device); |
d1643d24 AD |
6621 | if (sizeof_priv) { |
6622 | /* ensure 32-byte alignment of private area */ | |
1ce8e7b5 | 6623 | alloc_size = ALIGN(alloc_size, NETDEV_ALIGN); |
d1643d24 AD |
6624 | alloc_size += sizeof_priv; |
6625 | } | |
6626 | /* ensure 32-byte alignment of whole construct */ | |
1ce8e7b5 | 6627 | alloc_size += NETDEV_ALIGN - 1; |
1da177e4 | 6628 | |
31380de9 | 6629 | p = kzalloc(alloc_size, GFP_KERNEL); |
1da177e4 | 6630 | if (!p) { |
7b6cd1ce | 6631 | pr_err("alloc_netdev: Unable to allocate device\n"); |
1da177e4 LT |
6632 | return NULL; |
6633 | } | |
1da177e4 | 6634 | |
1ce8e7b5 | 6635 | dev = PTR_ALIGN(p, NETDEV_ALIGN); |
1da177e4 | 6636 | dev->padded = (char *)dev - (char *)p; |
ab9c73cc | 6637 | |
29b4433d ED |
6638 | dev->pcpu_refcnt = alloc_percpu(int); |
6639 | if (!dev->pcpu_refcnt) | |
e6484930 | 6640 | goto free_p; |
ab9c73cc | 6641 | |
ab9c73cc | 6642 | if (dev_addr_init(dev)) |
29b4433d | 6643 | goto free_pcpu; |
ab9c73cc | 6644 | |
22bedad3 | 6645 | dev_mc_init(dev); |
a748ee24 | 6646 | dev_uc_init(dev); |
ccffad25 | 6647 | |
c346dca1 | 6648 | dev_net_set(dev, &init_net); |
1da177e4 | 6649 | |
8d3bdbd5 | 6650 | dev->gso_max_size = GSO_MAX_SIZE; |
30b678d8 | 6651 | dev->gso_max_segs = GSO_MAX_SEGS; |
8d3bdbd5 | 6652 | |
8d3bdbd5 DM |
6653 | INIT_LIST_HEAD(&dev->napi_list); |
6654 | INIT_LIST_HEAD(&dev->unreg_list); | |
6655 | INIT_LIST_HEAD(&dev->link_watch_list); | |
9ff162a8 | 6656 | INIT_LIST_HEAD(&dev->upper_dev_list); |
8d3bdbd5 DM |
6657 | dev->priv_flags = IFF_XMIT_DST_RELEASE; |
6658 | setup(dev); | |
6659 | ||
36909ea4 TH |
6660 | dev->num_tx_queues = txqs; |
6661 | dev->real_num_tx_queues = txqs; | |
ed9af2e8 | 6662 | if (netif_alloc_netdev_queues(dev)) |
8d3bdbd5 | 6663 | goto free_all; |
e8a0464c | 6664 | |
df334545 | 6665 | #ifdef CONFIG_RPS |
36909ea4 TH |
6666 | dev->num_rx_queues = rxqs; |
6667 | dev->real_num_rx_queues = rxqs; | |
fe822240 | 6668 | if (netif_alloc_rx_queues(dev)) |
8d3bdbd5 | 6669 | goto free_all; |
df334545 | 6670 | #endif |
0a9627f2 | 6671 | |
1da177e4 | 6672 | strcpy(dev->name, name); |
cbda10fa | 6673 | dev->group = INIT_NETDEV_GROUP; |
2c60db03 ED |
6674 | if (!dev->ethtool_ops) |
6675 | dev->ethtool_ops = &default_ethtool_ops; | |
1da177e4 | 6676 | return dev; |
ab9c73cc | 6677 | |
8d3bdbd5 DM |
6678 | free_all: |
6679 | free_netdev(dev); | |
6680 | return NULL; | |
6681 | ||
29b4433d ED |
6682 | free_pcpu: |
6683 | free_percpu(dev->pcpu_refcnt); | |
ed9af2e8 | 6684 | kfree(dev->_tx); |
fe822240 TH |
6685 | #ifdef CONFIG_RPS |
6686 | kfree(dev->_rx); | |
6687 | #endif | |
6688 | ||
ab9c73cc JP |
6689 | free_p: |
6690 | kfree(p); | |
6691 | return NULL; | |
1da177e4 | 6692 | } |
36909ea4 | 6693 | EXPORT_SYMBOL(alloc_netdev_mqs); |
1da177e4 LT |
6694 | |
6695 | /** | |
6696 | * free_netdev - free network device | |
6697 | * @dev: device | |
6698 | * | |
4ec93edb YH |
6699 | * This function does the last stage of destroying an allocated device |
6700 | * interface. The reference to the device object is released. | |
1da177e4 LT |
6701 | * If this is the last reference then it will be freed. |
6702 | */ | |
6703 | void free_netdev(struct net_device *dev) | |
6704 | { | |
d565b0a1 HX |
6705 | struct napi_struct *p, *n; |
6706 | ||
f3005d7f DL |
6707 | release_net(dev_net(dev)); |
6708 | ||
e8a0464c | 6709 | kfree(dev->_tx); |
fe822240 TH |
6710 | #ifdef CONFIG_RPS |
6711 | kfree(dev->_rx); | |
6712 | #endif | |
e8a0464c | 6713 | |
33d480ce | 6714 | kfree(rcu_dereference_protected(dev->ingress_queue, 1)); |
24824a09 | 6715 | |
f001fde5 JP |
6716 | /* Flush device addresses */ |
6717 | dev_addr_flush(dev); | |
6718 | ||
d565b0a1 HX |
6719 | list_for_each_entry_safe(p, n, &dev->napi_list, dev_list) |
6720 | netif_napi_del(p); | |
6721 | ||
29b4433d ED |
6722 | free_percpu(dev->pcpu_refcnt); |
6723 | dev->pcpu_refcnt = NULL; | |
6724 | ||
3041a069 | 6725 | /* Compatibility with error handling in drivers */ |
1da177e4 LT |
6726 | if (dev->reg_state == NETREG_UNINITIALIZED) { |
6727 | kfree((char *)dev - dev->padded); | |
6728 | return; | |
6729 | } | |
6730 | ||
6731 | BUG_ON(dev->reg_state != NETREG_UNREGISTERED); | |
6732 | dev->reg_state = NETREG_RELEASED; | |
6733 | ||
43cb76d9 GKH |
6734 | /* will free via device release */ |
6735 | put_device(&dev->dev); | |
1da177e4 | 6736 | } |
d1b19dff | 6737 | EXPORT_SYMBOL(free_netdev); |
4ec93edb | 6738 | |
f0db275a SH |
6739 | /** |
6740 | * synchronize_net - Synchronize with packet receive processing | |
6741 | * | |
6742 | * Wait for packets currently being received to be done. | |
6743 | * Does not block later packets from starting. | |
6744 | */ | |
4ec93edb | 6745 | void synchronize_net(void) |
1da177e4 LT |
6746 | { |
6747 | might_sleep(); | |
be3fc413 ED |
6748 | if (rtnl_is_locked()) |
6749 | synchronize_rcu_expedited(); | |
6750 | else | |
6751 | synchronize_rcu(); | |
1da177e4 | 6752 | } |
d1b19dff | 6753 | EXPORT_SYMBOL(synchronize_net); |
1da177e4 LT |
6754 | |
6755 | /** | |
44a0873d | 6756 | * unregister_netdevice_queue - remove device from the kernel |
1da177e4 | 6757 | * @dev: device |
44a0873d | 6758 | * @head: list |
6ebfbc06 | 6759 | * |
1da177e4 | 6760 | * This function shuts down a device interface and removes it |
d59b54b1 | 6761 | * from the kernel tables. |
44a0873d | 6762 | * If head not NULL, device is queued to be unregistered later. |
1da177e4 LT |
6763 | * |
6764 | * Callers must hold the rtnl semaphore. You may want | |
6765 | * unregister_netdev() instead of this. | |
6766 | */ | |
6767 | ||
44a0873d | 6768 | void unregister_netdevice_queue(struct net_device *dev, struct list_head *head) |
1da177e4 | 6769 | { |
a6620712 HX |
6770 | ASSERT_RTNL(); |
6771 | ||
44a0873d | 6772 | if (head) { |
9fdce099 | 6773 | list_move_tail(&dev->unreg_list, head); |
44a0873d ED |
6774 | } else { |
6775 | rollback_registered(dev); | |
6776 | /* Finish processing unregister after unlock */ | |
6777 | net_set_todo(dev); | |
6778 | } | |
1da177e4 | 6779 | } |
44a0873d | 6780 | EXPORT_SYMBOL(unregister_netdevice_queue); |
1da177e4 | 6781 | |
9b5e383c ED |
6782 | /** |
6783 | * unregister_netdevice_many - unregister many devices | |
6784 | * @head: list of devices | |
9b5e383c ED |
6785 | */ |
6786 | void unregister_netdevice_many(struct list_head *head) | |
6787 | { | |
6788 | struct net_device *dev; | |
6789 | ||
6790 | if (!list_empty(head)) { | |
6791 | rollback_registered_many(head); | |
6792 | list_for_each_entry(dev, head, unreg_list) | |
6793 | net_set_todo(dev); | |
6794 | } | |
6795 | } | |
63c8099d | 6796 | EXPORT_SYMBOL(unregister_netdevice_many); |
9b5e383c | 6797 | |
1da177e4 LT |
6798 | /** |
6799 | * unregister_netdev - remove device from the kernel | |
6800 | * @dev: device | |
6801 | * | |
6802 | * This function shuts down a device interface and removes it | |
d59b54b1 | 6803 | * from the kernel tables. |
1da177e4 LT |
6804 | * |
6805 | * This is just a wrapper for unregister_netdevice that takes | |
6806 | * the rtnl semaphore. In general you want to use this and not | |
6807 | * unregister_netdevice. | |
6808 | */ | |
6809 | void unregister_netdev(struct net_device *dev) | |
6810 | { | |
6811 | rtnl_lock(); | |
6812 | unregister_netdevice(dev); | |
6813 | rtnl_unlock(); | |
6814 | } | |
1da177e4 LT |
6815 | EXPORT_SYMBOL(unregister_netdev); |
6816 | ||
ce286d32 EB |
6817 | /** |
6818 | * dev_change_net_namespace - move device to different nethost namespace | |
6819 | * @dev: device | |
6820 | * @net: network namespace | |
6821 | * @pat: If not NULL name pattern to try if the current device name | |
6822 | * is already taken in the destination network namespace. | |
6823 | * | |
6824 | * This function shuts down a device interface and moves it | |
6825 | * to a new network namespace. On success 0 is returned, on | |
6826 | * a failure a netagive errno code is returned. | |
6827 | * | |
6828 | * Callers must hold the rtnl semaphore. | |
6829 | */ | |
6830 | ||
6831 | int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat) | |
6832 | { | |
ce286d32 EB |
6833 | int err; |
6834 | ||
6835 | ASSERT_RTNL(); | |
6836 | ||
6837 | /* Don't allow namespace local devices to be moved. */ | |
6838 | err = -EINVAL; | |
6839 | if (dev->features & NETIF_F_NETNS_LOCAL) | |
6840 | goto out; | |
6841 | ||
6842 | /* Ensure the device has been registrered */ | |
ce286d32 EB |
6843 | if (dev->reg_state != NETREG_REGISTERED) |
6844 | goto out; | |
6845 | ||
6846 | /* Get out if there is nothing todo */ | |
6847 | err = 0; | |
878628fb | 6848 | if (net_eq(dev_net(dev), net)) |
ce286d32 EB |
6849 | goto out; |
6850 | ||
6851 | /* Pick the destination device name, and ensure | |
6852 | * we can use it in the destination network namespace. | |
6853 | */ | |
6854 | err = -EEXIST; | |
d9031024 | 6855 | if (__dev_get_by_name(net, dev->name)) { |
ce286d32 EB |
6856 | /* We get here if we can't use the current device name */ |
6857 | if (!pat) | |
6858 | goto out; | |
828de4f6 | 6859 | if (dev_get_valid_name(net, dev, pat) < 0) |
ce286d32 EB |
6860 | goto out; |
6861 | } | |
6862 | ||
6863 | /* | |
6864 | * And now a mini version of register_netdevice unregister_netdevice. | |
6865 | */ | |
6866 | ||
6867 | /* If device is running close it first. */ | |
9b772652 | 6868 | dev_close(dev); |
ce286d32 EB |
6869 | |
6870 | /* And unlink it from device chain */ | |
6871 | err = -ENODEV; | |
6872 | unlist_netdevice(dev); | |
6873 | ||
6874 | synchronize_net(); | |
6875 | ||
6876 | /* Shutdown queueing discipline. */ | |
6877 | dev_shutdown(dev); | |
6878 | ||
6879 | /* Notify protocols, that we are about to destroy | |
6880 | this device. They should clean all the things. | |
3b27e105 DL |
6881 | |
6882 | Note that dev->reg_state stays at NETREG_REGISTERED. | |
6883 | This is wanted because this way 8021q and macvlan know | |
6884 | the device is just moving and can keep their slaves up. | |
ce286d32 EB |
6885 | */ |
6886 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); | |
6549dd43 G |
6887 | rcu_barrier(); |
6888 | call_netdevice_notifiers(NETDEV_UNREGISTER_FINAL, dev); | |
d2237d35 | 6889 | rtmsg_ifinfo(RTM_DELLINK, dev, ~0U); |
ce286d32 EB |
6890 | |
6891 | /* | |
6892 | * Flush the unicast and multicast chains | |
6893 | */ | |
a748ee24 | 6894 | dev_uc_flush(dev); |
22bedad3 | 6895 | dev_mc_flush(dev); |
ce286d32 | 6896 | |
4e66ae2e SH |
6897 | /* Send a netdev-removed uevent to the old namespace */ |
6898 | kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE); | |
6899 | ||
ce286d32 | 6900 | /* Actually switch the network namespace */ |
c346dca1 | 6901 | dev_net_set(dev, net); |
ce286d32 | 6902 | |
ce286d32 EB |
6903 | /* If there is an ifindex conflict assign a new one */ |
6904 | if (__dev_get_by_index(net, dev->ifindex)) { | |
6905 | int iflink = (dev->iflink == dev->ifindex); | |
6906 | dev->ifindex = dev_new_index(net); | |
6907 | if (iflink) | |
6908 | dev->iflink = dev->ifindex; | |
6909 | } | |
6910 | ||
4e66ae2e SH |
6911 | /* Send a netdev-add uevent to the new namespace */ |
6912 | kobject_uevent(&dev->dev.kobj, KOBJ_ADD); | |
6913 | ||
8b41d188 | 6914 | /* Fixup kobjects */ |
a1b3f594 | 6915 | err = device_rename(&dev->dev, dev->name); |
8b41d188 | 6916 | WARN_ON(err); |
ce286d32 EB |
6917 | |
6918 | /* Add the device back in the hashes */ | |
6919 | list_netdevice(dev); | |
6920 | ||
6921 | /* Notify protocols, that a new device appeared. */ | |
6922 | call_netdevice_notifiers(NETDEV_REGISTER, dev); | |
6923 | ||
d90a909e EB |
6924 | /* |
6925 | * Prevent userspace races by waiting until the network | |
6926 | * device is fully setup before sending notifications. | |
6927 | */ | |
6928 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U); | |
6929 | ||
ce286d32 EB |
6930 | synchronize_net(); |
6931 | err = 0; | |
6932 | out: | |
6933 | return err; | |
6934 | } | |
463d0183 | 6935 | EXPORT_SYMBOL_GPL(dev_change_net_namespace); |
ce286d32 | 6936 | |
1da177e4 LT |
6937 | static int dev_cpu_callback(struct notifier_block *nfb, |
6938 | unsigned long action, | |
6939 | void *ocpu) | |
6940 | { | |
6941 | struct sk_buff **list_skb; | |
1da177e4 LT |
6942 | struct sk_buff *skb; |
6943 | unsigned int cpu, oldcpu = (unsigned long)ocpu; | |
6944 | struct softnet_data *sd, *oldsd; | |
6945 | ||
8bb78442 | 6946 | if (action != CPU_DEAD && action != CPU_DEAD_FROZEN) |
1da177e4 LT |
6947 | return NOTIFY_OK; |
6948 | ||
6949 | local_irq_disable(); | |
6950 | cpu = smp_processor_id(); | |
6951 | sd = &per_cpu(softnet_data, cpu); | |
6952 | oldsd = &per_cpu(softnet_data, oldcpu); | |
6953 | ||
6954 | /* Find end of our completion_queue. */ | |
6955 | list_skb = &sd->completion_queue; | |
6956 | while (*list_skb) | |
6957 | list_skb = &(*list_skb)->next; | |
6958 | /* Append completion queue from offline CPU. */ | |
6959 | *list_skb = oldsd->completion_queue; | |
6960 | oldsd->completion_queue = NULL; | |
6961 | ||
1da177e4 | 6962 | /* Append output queue from offline CPU. */ |
a9cbd588 CG |
6963 | if (oldsd->output_queue) { |
6964 | *sd->output_queue_tailp = oldsd->output_queue; | |
6965 | sd->output_queue_tailp = oldsd->output_queue_tailp; | |
6966 | oldsd->output_queue = NULL; | |
6967 | oldsd->output_queue_tailp = &oldsd->output_queue; | |
6968 | } | |
264524d5 HC |
6969 | /* Append NAPI poll list from offline CPU. */ |
6970 | if (!list_empty(&oldsd->poll_list)) { | |
6971 | list_splice_init(&oldsd->poll_list, &sd->poll_list); | |
6972 | raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
6973 | } | |
1da177e4 LT |
6974 | |
6975 | raise_softirq_irqoff(NET_TX_SOFTIRQ); | |
6976 | local_irq_enable(); | |
6977 | ||
6978 | /* Process offline CPU's input_pkt_queue */ | |
76cc8b13 | 6979 | while ((skb = __skb_dequeue(&oldsd->process_queue))) { |
1da177e4 | 6980 | netif_rx(skb); |
76cc8b13 | 6981 | input_queue_head_incr(oldsd); |
fec5e652 | 6982 | } |
76cc8b13 | 6983 | while ((skb = __skb_dequeue(&oldsd->input_pkt_queue))) { |
6e7676c1 | 6984 | netif_rx(skb); |
76cc8b13 TH |
6985 | input_queue_head_incr(oldsd); |
6986 | } | |
1da177e4 LT |
6987 | |
6988 | return NOTIFY_OK; | |
6989 | } | |
1da177e4 LT |
6990 | |
6991 | ||
7f353bf2 | 6992 | /** |
b63365a2 HX |
6993 | * netdev_increment_features - increment feature set by one |
6994 | * @all: current feature set | |
6995 | * @one: new feature set | |
6996 | * @mask: mask feature set | |
7f353bf2 HX |
6997 | * |
6998 | * Computes a new feature set after adding a device with feature set | |
b63365a2 HX |
6999 | * @one to the master device with current feature set @all. Will not |
7000 | * enable anything that is off in @mask. Returns the new feature set. | |
7f353bf2 | 7001 | */ |
c8f44aff MM |
7002 | netdev_features_t netdev_increment_features(netdev_features_t all, |
7003 | netdev_features_t one, netdev_features_t mask) | |
b63365a2 | 7004 | { |
1742f183 MM |
7005 | if (mask & NETIF_F_GEN_CSUM) |
7006 | mask |= NETIF_F_ALL_CSUM; | |
7007 | mask |= NETIF_F_VLAN_CHALLENGED; | |
7f353bf2 | 7008 | |
1742f183 MM |
7009 | all |= one & (NETIF_F_ONE_FOR_ALL|NETIF_F_ALL_CSUM) & mask; |
7010 | all &= one | ~NETIF_F_ALL_FOR_ALL; | |
c6e1a0d1 | 7011 | |
1742f183 MM |
7012 | /* If one device supports hw checksumming, set for all. */ |
7013 | if (all & NETIF_F_GEN_CSUM) | |
7014 | all &= ~(NETIF_F_ALL_CSUM & ~NETIF_F_GEN_CSUM); | |
7f353bf2 HX |
7015 | |
7016 | return all; | |
7017 | } | |
b63365a2 | 7018 | EXPORT_SYMBOL(netdev_increment_features); |
7f353bf2 | 7019 | |
30d97d35 PE |
7020 | static struct hlist_head *netdev_create_hash(void) |
7021 | { | |
7022 | int i; | |
7023 | struct hlist_head *hash; | |
7024 | ||
7025 | hash = kmalloc(sizeof(*hash) * NETDEV_HASHENTRIES, GFP_KERNEL); | |
7026 | if (hash != NULL) | |
7027 | for (i = 0; i < NETDEV_HASHENTRIES; i++) | |
7028 | INIT_HLIST_HEAD(&hash[i]); | |
7029 | ||
7030 | return hash; | |
7031 | } | |
7032 | ||
881d966b | 7033 | /* Initialize per network namespace state */ |
4665079c | 7034 | static int __net_init netdev_init(struct net *net) |
881d966b | 7035 | { |
734b6541 RM |
7036 | if (net != &init_net) |
7037 | INIT_LIST_HEAD(&net->dev_base_head); | |
881d966b | 7038 | |
30d97d35 PE |
7039 | net->dev_name_head = netdev_create_hash(); |
7040 | if (net->dev_name_head == NULL) | |
7041 | goto err_name; | |
881d966b | 7042 | |
30d97d35 PE |
7043 | net->dev_index_head = netdev_create_hash(); |
7044 | if (net->dev_index_head == NULL) | |
7045 | goto err_idx; | |
881d966b EB |
7046 | |
7047 | return 0; | |
30d97d35 PE |
7048 | |
7049 | err_idx: | |
7050 | kfree(net->dev_name_head); | |
7051 | err_name: | |
7052 | return -ENOMEM; | |
881d966b EB |
7053 | } |
7054 | ||
f0db275a SH |
7055 | /** |
7056 | * netdev_drivername - network driver for the device | |
7057 | * @dev: network device | |
f0db275a SH |
7058 | * |
7059 | * Determine network driver for device. | |
7060 | */ | |
3019de12 | 7061 | const char *netdev_drivername(const struct net_device *dev) |
6579e57b | 7062 | { |
cf04a4c7 SH |
7063 | const struct device_driver *driver; |
7064 | const struct device *parent; | |
3019de12 | 7065 | const char *empty = ""; |
6579e57b AV |
7066 | |
7067 | parent = dev->dev.parent; | |
6579e57b | 7068 | if (!parent) |
3019de12 | 7069 | return empty; |
6579e57b AV |
7070 | |
7071 | driver = parent->driver; | |
7072 | if (driver && driver->name) | |
3019de12 DM |
7073 | return driver->name; |
7074 | return empty; | |
6579e57b AV |
7075 | } |
7076 | ||
b004ff49 | 7077 | static int __netdev_printk(const char *level, const struct net_device *dev, |
256df2f3 JP |
7078 | struct va_format *vaf) |
7079 | { | |
7080 | int r; | |
7081 | ||
b004ff49 | 7082 | if (dev && dev->dev.parent) { |
666f355f JP |
7083 | r = dev_printk_emit(level[1] - '0', |
7084 | dev->dev.parent, | |
7085 | "%s %s %s: %pV", | |
7086 | dev_driver_string(dev->dev.parent), | |
7087 | dev_name(dev->dev.parent), | |
7088 | netdev_name(dev), vaf); | |
b004ff49 | 7089 | } else if (dev) { |
256df2f3 | 7090 | r = printk("%s%s: %pV", level, netdev_name(dev), vaf); |
b004ff49 | 7091 | } else { |
256df2f3 | 7092 | r = printk("%s(NULL net_device): %pV", level, vaf); |
b004ff49 | 7093 | } |
256df2f3 JP |
7094 | |
7095 | return r; | |
7096 | } | |
7097 | ||
7098 | int netdev_printk(const char *level, const struct net_device *dev, | |
7099 | const char *format, ...) | |
7100 | { | |
7101 | struct va_format vaf; | |
7102 | va_list args; | |
7103 | int r; | |
7104 | ||
7105 | va_start(args, format); | |
7106 | ||
7107 | vaf.fmt = format; | |
7108 | vaf.va = &args; | |
7109 | ||
7110 | r = __netdev_printk(level, dev, &vaf); | |
b004ff49 | 7111 | |
256df2f3 JP |
7112 | va_end(args); |
7113 | ||
7114 | return r; | |
7115 | } | |
7116 | EXPORT_SYMBOL(netdev_printk); | |
7117 | ||
7118 | #define define_netdev_printk_level(func, level) \ | |
7119 | int func(const struct net_device *dev, const char *fmt, ...) \ | |
7120 | { \ | |
7121 | int r; \ | |
7122 | struct va_format vaf; \ | |
7123 | va_list args; \ | |
7124 | \ | |
7125 | va_start(args, fmt); \ | |
7126 | \ | |
7127 | vaf.fmt = fmt; \ | |
7128 | vaf.va = &args; \ | |
7129 | \ | |
7130 | r = __netdev_printk(level, dev, &vaf); \ | |
b004ff49 | 7131 | \ |
256df2f3 JP |
7132 | va_end(args); \ |
7133 | \ | |
7134 | return r; \ | |
7135 | } \ | |
7136 | EXPORT_SYMBOL(func); | |
7137 | ||
7138 | define_netdev_printk_level(netdev_emerg, KERN_EMERG); | |
7139 | define_netdev_printk_level(netdev_alert, KERN_ALERT); | |
7140 | define_netdev_printk_level(netdev_crit, KERN_CRIT); | |
7141 | define_netdev_printk_level(netdev_err, KERN_ERR); | |
7142 | define_netdev_printk_level(netdev_warn, KERN_WARNING); | |
7143 | define_netdev_printk_level(netdev_notice, KERN_NOTICE); | |
7144 | define_netdev_printk_level(netdev_info, KERN_INFO); | |
7145 | ||
4665079c | 7146 | static void __net_exit netdev_exit(struct net *net) |
881d966b EB |
7147 | { |
7148 | kfree(net->dev_name_head); | |
7149 | kfree(net->dev_index_head); | |
7150 | } | |
7151 | ||
022cbae6 | 7152 | static struct pernet_operations __net_initdata netdev_net_ops = { |
881d966b EB |
7153 | .init = netdev_init, |
7154 | .exit = netdev_exit, | |
7155 | }; | |
7156 | ||
4665079c | 7157 | static void __net_exit default_device_exit(struct net *net) |
ce286d32 | 7158 | { |
e008b5fc | 7159 | struct net_device *dev, *aux; |
ce286d32 | 7160 | /* |
e008b5fc | 7161 | * Push all migratable network devices back to the |
ce286d32 EB |
7162 | * initial network namespace |
7163 | */ | |
7164 | rtnl_lock(); | |
e008b5fc | 7165 | for_each_netdev_safe(net, dev, aux) { |
ce286d32 | 7166 | int err; |
aca51397 | 7167 | char fb_name[IFNAMSIZ]; |
ce286d32 EB |
7168 | |
7169 | /* Ignore unmoveable devices (i.e. loopback) */ | |
7170 | if (dev->features & NETIF_F_NETNS_LOCAL) | |
7171 | continue; | |
7172 | ||
e008b5fc EB |
7173 | /* Leave virtual devices for the generic cleanup */ |
7174 | if (dev->rtnl_link_ops) | |
7175 | continue; | |
d0c082ce | 7176 | |
25985edc | 7177 | /* Push remaining network devices to init_net */ |
aca51397 PE |
7178 | snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex); |
7179 | err = dev_change_net_namespace(dev, &init_net, fb_name); | |
ce286d32 | 7180 | if (err) { |
7b6cd1ce JP |
7181 | pr_emerg("%s: failed to move %s to init_net: %d\n", |
7182 | __func__, dev->name, err); | |
aca51397 | 7183 | BUG(); |
ce286d32 EB |
7184 | } |
7185 | } | |
7186 | rtnl_unlock(); | |
7187 | } | |
7188 | ||
04dc7f6b EB |
7189 | static void __net_exit default_device_exit_batch(struct list_head *net_list) |
7190 | { | |
7191 | /* At exit all network devices most be removed from a network | |
b595076a | 7192 | * namespace. Do this in the reverse order of registration. |
04dc7f6b EB |
7193 | * Do this across as many network namespaces as possible to |
7194 | * improve batching efficiency. | |
7195 | */ | |
7196 | struct net_device *dev; | |
7197 | struct net *net; | |
7198 | LIST_HEAD(dev_kill_list); | |
7199 | ||
7200 | rtnl_lock(); | |
7201 | list_for_each_entry(net, net_list, exit_list) { | |
7202 | for_each_netdev_reverse(net, dev) { | |
7203 | if (dev->rtnl_link_ops) | |
7204 | dev->rtnl_link_ops->dellink(dev, &dev_kill_list); | |
7205 | else | |
7206 | unregister_netdevice_queue(dev, &dev_kill_list); | |
7207 | } | |
7208 | } | |
7209 | unregister_netdevice_many(&dev_kill_list); | |
ceaaec98 | 7210 | list_del(&dev_kill_list); |
04dc7f6b EB |
7211 | rtnl_unlock(); |
7212 | } | |
7213 | ||
022cbae6 | 7214 | static struct pernet_operations __net_initdata default_device_ops = { |
ce286d32 | 7215 | .exit = default_device_exit, |
04dc7f6b | 7216 | .exit_batch = default_device_exit_batch, |
ce286d32 EB |
7217 | }; |
7218 | ||
1da177e4 LT |
7219 | /* |
7220 | * Initialize the DEV module. At boot time this walks the device list and | |
7221 | * unhooks any devices that fail to initialise (normally hardware not | |
7222 | * present) and leaves us with a valid list of present and active devices. | |
7223 | * | |
7224 | */ | |
7225 | ||
7226 | /* | |
7227 | * This is called single threaded during boot, so no need | |
7228 | * to take the rtnl semaphore. | |
7229 | */ | |
7230 | static int __init net_dev_init(void) | |
7231 | { | |
7232 | int i, rc = -ENOMEM; | |
7233 | ||
7234 | BUG_ON(!dev_boot_phase); | |
7235 | ||
1da177e4 LT |
7236 | if (dev_proc_init()) |
7237 | goto out; | |
7238 | ||
8b41d188 | 7239 | if (netdev_kobject_init()) |
1da177e4 LT |
7240 | goto out; |
7241 | ||
7242 | INIT_LIST_HEAD(&ptype_all); | |
82d8a867 | 7243 | for (i = 0; i < PTYPE_HASH_SIZE; i++) |
1da177e4 LT |
7244 | INIT_LIST_HEAD(&ptype_base[i]); |
7245 | ||
62532da9 VY |
7246 | INIT_LIST_HEAD(&offload_base); |
7247 | ||
881d966b EB |
7248 | if (register_pernet_subsys(&netdev_net_ops)) |
7249 | goto out; | |
1da177e4 LT |
7250 | |
7251 | /* | |
7252 | * Initialise the packet receive queues. | |
7253 | */ | |
7254 | ||
6f912042 | 7255 | for_each_possible_cpu(i) { |
e36fa2f7 | 7256 | struct softnet_data *sd = &per_cpu(softnet_data, i); |
1da177e4 | 7257 | |
dee42870 | 7258 | memset(sd, 0, sizeof(*sd)); |
e36fa2f7 | 7259 | skb_queue_head_init(&sd->input_pkt_queue); |
6e7676c1 | 7260 | skb_queue_head_init(&sd->process_queue); |
e36fa2f7 ED |
7261 | sd->completion_queue = NULL; |
7262 | INIT_LIST_HEAD(&sd->poll_list); | |
a9cbd588 CG |
7263 | sd->output_queue = NULL; |
7264 | sd->output_queue_tailp = &sd->output_queue; | |
df334545 | 7265 | #ifdef CONFIG_RPS |
e36fa2f7 ED |
7266 | sd->csd.func = rps_trigger_softirq; |
7267 | sd->csd.info = sd; | |
7268 | sd->csd.flags = 0; | |
7269 | sd->cpu = i; | |
1e94d72f | 7270 | #endif |
0a9627f2 | 7271 | |
e36fa2f7 ED |
7272 | sd->backlog.poll = process_backlog; |
7273 | sd->backlog.weight = weight_p; | |
7274 | sd->backlog.gro_list = NULL; | |
7275 | sd->backlog.gro_count = 0; | |
1da177e4 LT |
7276 | } |
7277 | ||
1da177e4 LT |
7278 | dev_boot_phase = 0; |
7279 | ||
505d4f73 EB |
7280 | /* The loopback device is special if any other network devices |
7281 | * is present in a network namespace the loopback device must | |
7282 | * be present. Since we now dynamically allocate and free the | |
7283 | * loopback device ensure this invariant is maintained by | |
7284 | * keeping the loopback device as the first device on the | |
7285 | * list of network devices. Ensuring the loopback devices | |
7286 | * is the first device that appears and the last network device | |
7287 | * that disappears. | |
7288 | */ | |
7289 | if (register_pernet_device(&loopback_net_ops)) | |
7290 | goto out; | |
7291 | ||
7292 | if (register_pernet_device(&default_device_ops)) | |
7293 | goto out; | |
7294 | ||
962cf36c CM |
7295 | open_softirq(NET_TX_SOFTIRQ, net_tx_action); |
7296 | open_softirq(NET_RX_SOFTIRQ, net_rx_action); | |
1da177e4 LT |
7297 | |
7298 | hotcpu_notifier(dev_cpu_callback, 0); | |
7299 | dst_init(); | |
7300 | dev_mcast_init(); | |
7301 | rc = 0; | |
7302 | out: | |
7303 | return rc; | |
7304 | } | |
7305 | ||
7306 | subsys_initcall(net_dev_init); | |
7307 | ||
e88721f8 KK |
7308 | static int __init initialize_hashrnd(void) |
7309 | { | |
0a9627f2 | 7310 | get_random_bytes(&hashrnd, sizeof(hashrnd)); |
e88721f8 KK |
7311 | return 0; |
7312 | } | |
7313 | ||
7314 | late_initcall_sync(initialize_hashrnd); | |
7315 |