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