]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 | 2 | /* |
722c9a0c | 3 | * NET3 Protocol independent device support routines. |
1da177e4 | 4 | * |
1da177e4 | 5 | * Derived from the non IP parts of dev.c 1.0.19 |
722c9a0c | 6 | * Authors: Ross Biro |
1da177e4 LT |
7 | * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG> |
8 | * Mark Evans, <evansmp@uhura.aston.ac.uk> | |
9 | * | |
10 | * Additional Authors: | |
11 | * Florian la Roche <rzsfl@rz.uni-sb.de> | |
12 | * Alan Cox <gw4pts@gw4pts.ampr.org> | |
13 | * David Hinds <dahinds@users.sourceforge.net> | |
14 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> | |
15 | * Adam Sulmicki <adam@cfar.umd.edu> | |
16 | * Pekka Riikonen <priikone@poesidon.pspt.fi> | |
17 | * | |
18 | * Changes: | |
19 | * D.J. Barrow : Fixed bug where dev->refcnt gets set | |
722c9a0c | 20 | * to 2 if register_netdev gets called |
21 | * before net_dev_init & also removed a | |
22 | * few lines of code in the process. | |
1da177e4 LT |
23 | * Alan Cox : device private ioctl copies fields back. |
24 | * Alan Cox : Transmit queue code does relevant | |
25 | * stunts to keep the queue safe. | |
26 | * Alan Cox : Fixed double lock. | |
27 | * Alan Cox : Fixed promisc NULL pointer trap | |
28 | * ???????? : Support the full private ioctl range | |
29 | * Alan Cox : Moved ioctl permission check into | |
30 | * drivers | |
31 | * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI | |
32 | * Alan Cox : 100 backlog just doesn't cut it when | |
33 | * you start doing multicast video 8) | |
34 | * Alan Cox : Rewrote net_bh and list manager. | |
722c9a0c | 35 | * Alan Cox : Fix ETH_P_ALL echoback lengths. |
1da177e4 LT |
36 | * Alan Cox : Took out transmit every packet pass |
37 | * Saved a few bytes in the ioctl handler | |
38 | * Alan Cox : Network driver sets packet type before | |
39 | * calling netif_rx. Saves a function | |
40 | * call a packet. | |
41 | * Alan Cox : Hashed net_bh() | |
42 | * Richard Kooijman: Timestamp fixes. | |
43 | * Alan Cox : Wrong field in SIOCGIFDSTADDR | |
44 | * Alan Cox : Device lock protection. | |
722c9a0c | 45 | * Alan Cox : Fixed nasty side effect of device close |
1da177e4 LT |
46 | * changes. |
47 | * Rudi Cilibrasi : Pass the right thing to | |
48 | * set_mac_address() | |
49 | * Dave Miller : 32bit quantity for the device lock to | |
50 | * make it work out on a Sparc. | |
51 | * Bjorn Ekwall : Added KERNELD hack. | |
52 | * Alan Cox : Cleaned up the backlog initialise. | |
53 | * Craig Metz : SIOCGIFCONF fix if space for under | |
54 | * 1 device. | |
55 | * Thomas Bogendoerfer : Return ENODEV for dev_open, if there | |
56 | * is no device open function. | |
57 | * Andi Kleen : Fix error reporting for SIOCGIFCONF | |
58 | * Michael Chastain : Fix signed/unsigned for SIOCGIFCONF | |
59 | * Cyrus Durgin : Cleaned for KMOD | |
60 | * Adam Sulmicki : Bug Fix : Network Device Unload | |
61 | * A network device unload needs to purge | |
62 | * the backlog queue. | |
63 | * Paul Rusty Russell : SIOCSIFNAME | |
64 | * Pekka Riikonen : Netdev boot-time settings code | |
65 | * Andrew Morton : Make unregister_netdevice wait | |
722c9a0c | 66 | * indefinitely on dev->refcnt |
67 | * J Hadi Salim : - Backlog queue sampling | |
1da177e4 LT |
68 | * - netif_rx() feedback |
69 | */ | |
70 | ||
7c0f6ba6 | 71 | #include <linux/uaccess.h> |
1da177e4 | 72 | #include <linux/bitops.h> |
4fc268d2 | 73 | #include <linux/capability.h> |
1da177e4 LT |
74 | #include <linux/cpu.h> |
75 | #include <linux/types.h> | |
76 | #include <linux/kernel.h> | |
08e9897d | 77 | #include <linux/hash.h> |
5a0e3ad6 | 78 | #include <linux/slab.h> |
1da177e4 | 79 | #include <linux/sched.h> |
f1083048 | 80 | #include <linux/sched/mm.h> |
4a3e2f71 | 81 | #include <linux/mutex.h> |
1da177e4 LT |
82 | #include <linux/string.h> |
83 | #include <linux/mm.h> | |
84 | #include <linux/socket.h> | |
85 | #include <linux/sockios.h> | |
86 | #include <linux/errno.h> | |
87 | #include <linux/interrupt.h> | |
88 | #include <linux/if_ether.h> | |
89 | #include <linux/netdevice.h> | |
90 | #include <linux/etherdevice.h> | |
0187bdfb | 91 | #include <linux/ethtool.h> |
1da177e4 | 92 | #include <linux/skbuff.h> |
a7862b45 | 93 | #include <linux/bpf.h> |
b5cdae32 | 94 | #include <linux/bpf_trace.h> |
457c4cbc | 95 | #include <net/net_namespace.h> |
1da177e4 | 96 | #include <net/sock.h> |
02d62e86 | 97 | #include <net/busy_poll.h> |
1da177e4 | 98 | #include <linux/rtnetlink.h> |
1da177e4 | 99 | #include <linux/stat.h> |
1da177e4 | 100 | #include <net/dst.h> |
fc4099f1 | 101 | #include <net/dst_metadata.h> |
1da177e4 | 102 | #include <net/pkt_sched.h> |
87d83093 | 103 | #include <net/pkt_cls.h> |
1da177e4 | 104 | #include <net/checksum.h> |
44540960 | 105 | #include <net/xfrm.h> |
1da177e4 LT |
106 | #include <linux/highmem.h> |
107 | #include <linux/init.h> | |
1da177e4 | 108 | #include <linux/module.h> |
1da177e4 LT |
109 | #include <linux/netpoll.h> |
110 | #include <linux/rcupdate.h> | |
111 | #include <linux/delay.h> | |
1da177e4 | 112 | #include <net/iw_handler.h> |
1da177e4 | 113 | #include <asm/current.h> |
5bdb9886 | 114 | #include <linux/audit.h> |
db217334 | 115 | #include <linux/dmaengine.h> |
f6a78bfc | 116 | #include <linux/err.h> |
c7fa9d18 | 117 | #include <linux/ctype.h> |
723e98b7 | 118 | #include <linux/if_arp.h> |
6de329e2 | 119 | #include <linux/if_vlan.h> |
8f0f2223 | 120 | #include <linux/ip.h> |
ad55dcaf | 121 | #include <net/ip.h> |
25cd9ba0 | 122 | #include <net/mpls.h> |
8f0f2223 DM |
123 | #include <linux/ipv6.h> |
124 | #include <linux/in.h> | |
b6b2fed1 DM |
125 | #include <linux/jhash.h> |
126 | #include <linux/random.h> | |
9cbc1cb8 | 127 | #include <trace/events/napi.h> |
cf66ba58 | 128 | #include <trace/events/net.h> |
07dc22e7 | 129 | #include <trace/events/skb.h> |
caeda9b9 | 130 | #include <linux/inetdevice.h> |
c445477d | 131 | #include <linux/cpu_rmap.h> |
c5905afb | 132 | #include <linux/static_key.h> |
af12fa6e | 133 | #include <linux/hashtable.h> |
60877a32 | 134 | #include <linux/vmalloc.h> |
529d0489 | 135 | #include <linux/if_macvlan.h> |
e7fd2885 | 136 | #include <linux/errqueue.h> |
3b47d303 | 137 | #include <linux/hrtimer.h> |
e687ad60 | 138 | #include <linux/netfilter_ingress.h> |
40e4e713 | 139 | #include <linux/crash_dump.h> |
b72b5bf6 | 140 | #include <linux/sctp.h> |
ae847f40 | 141 | #include <net/udp_tunnel.h> |
6621dd29 | 142 | #include <linux/net_namespace.h> |
aaa5d90b | 143 | #include <linux/indirect_call_wrapper.h> |
af3836df | 144 | #include <net/devlink.h> |
1da177e4 | 145 | |
342709ef PE |
146 | #include "net-sysfs.h" |
147 | ||
d565b0a1 | 148 | #define MAX_GRO_SKBS 8 |
5343da4c | 149 | #define MAX_NEST_DEV 8 |
d565b0a1 | 150 | |
5d38a079 HX |
151 | /* This should be increased if a protocol with a bigger head is added. */ |
152 | #define GRO_MAX_HEAD (MAX_HEADER + 128) | |
153 | ||
1da177e4 | 154 | static DEFINE_SPINLOCK(ptype_lock); |
62532da9 | 155 | static DEFINE_SPINLOCK(offload_lock); |
900ff8c6 CW |
156 | struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly; |
157 | struct list_head ptype_all __read_mostly; /* Taps */ | |
62532da9 | 158 | static struct list_head offload_base __read_mostly; |
1da177e4 | 159 | |
ae78dbfa | 160 | static int netif_rx_internal(struct sk_buff *skb); |
54951194 | 161 | static int call_netdevice_notifiers_info(unsigned long val, |
54951194 | 162 | struct netdev_notifier_info *info); |
26372605 PM |
163 | static int call_netdevice_notifiers_extack(unsigned long val, |
164 | struct net_device *dev, | |
165 | struct netlink_ext_ack *extack); | |
90b602f8 | 166 | static struct napi_struct *napi_by_id(unsigned int napi_id); |
ae78dbfa | 167 | |
1da177e4 | 168 | /* |
7562f876 | 169 | * The @dev_base_head list is protected by @dev_base_lock and the rtnl |
1da177e4 LT |
170 | * semaphore. |
171 | * | |
c6d14c84 | 172 | * Pure readers hold dev_base_lock for reading, or rcu_read_lock() |
1da177e4 LT |
173 | * |
174 | * Writers must hold the rtnl semaphore while they loop through the | |
7562f876 | 175 | * dev_base_head list, and hold dev_base_lock for writing when they do the |
1da177e4 LT |
176 | * actual updates. This allows pure readers to access the list even |
177 | * while a writer is preparing to update it. | |
178 | * | |
179 | * To put it another way, dev_base_lock is held for writing only to | |
180 | * protect against pure readers; the rtnl semaphore provides the | |
181 | * protection against other writers. | |
182 | * | |
183 | * See, for example usages, register_netdevice() and | |
184 | * unregister_netdevice(), which must be called with the rtnl | |
185 | * semaphore held. | |
186 | */ | |
1da177e4 | 187 | DEFINE_RWLOCK(dev_base_lock); |
1da177e4 LT |
188 | EXPORT_SYMBOL(dev_base_lock); |
189 | ||
6c557001 FW |
190 | static DEFINE_MUTEX(ifalias_mutex); |
191 | ||
af12fa6e ET |
192 | /* protects napi_hash addition/deletion and napi_gen_id */ |
193 | static DEFINE_SPINLOCK(napi_hash_lock); | |
194 | ||
52bd2d62 | 195 | static unsigned int napi_gen_id = NR_CPUS; |
6180d9de | 196 | static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8); |
af12fa6e | 197 | |
18afa4b0 | 198 | static seqcount_t devnet_rename_seq; |
c91f6df2 | 199 | |
4e985ada TG |
200 | static inline void dev_base_seq_inc(struct net *net) |
201 | { | |
643aa9cb | 202 | while (++net->dev_base_seq == 0) |
203 | ; | |
4e985ada TG |
204 | } |
205 | ||
881d966b | 206 | static inline struct hlist_head *dev_name_hash(struct net *net, const char *name) |
1da177e4 | 207 | { |
8387ff25 | 208 | unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ)); |
95c96174 | 209 | |
08e9897d | 210 | return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)]; |
1da177e4 LT |
211 | } |
212 | ||
881d966b | 213 | static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex) |
1da177e4 | 214 | { |
7c28bd0b | 215 | return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)]; |
1da177e4 LT |
216 | } |
217 | ||
e36fa2f7 | 218 | static inline void rps_lock(struct softnet_data *sd) |
152102c7 CG |
219 | { |
220 | #ifdef CONFIG_RPS | |
e36fa2f7 | 221 | spin_lock(&sd->input_pkt_queue.lock); |
152102c7 CG |
222 | #endif |
223 | } | |
224 | ||
e36fa2f7 | 225 | static inline void rps_unlock(struct softnet_data *sd) |
152102c7 CG |
226 | { |
227 | #ifdef CONFIG_RPS | |
e36fa2f7 | 228 | spin_unlock(&sd->input_pkt_queue.lock); |
152102c7 CG |
229 | #endif |
230 | } | |
231 | ||
ff927412 JP |
232 | static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev, |
233 | const char *name) | |
234 | { | |
235 | struct netdev_name_node *name_node; | |
236 | ||
237 | name_node = kmalloc(sizeof(*name_node), GFP_KERNEL); | |
238 | if (!name_node) | |
239 | return NULL; | |
240 | INIT_HLIST_NODE(&name_node->hlist); | |
241 | name_node->dev = dev; | |
242 | name_node->name = name; | |
243 | return name_node; | |
244 | } | |
245 | ||
246 | static struct netdev_name_node * | |
247 | netdev_name_node_head_alloc(struct net_device *dev) | |
248 | { | |
36fbf1e5 JP |
249 | struct netdev_name_node *name_node; |
250 | ||
251 | name_node = netdev_name_node_alloc(dev, dev->name); | |
252 | if (!name_node) | |
253 | return NULL; | |
254 | INIT_LIST_HEAD(&name_node->list); | |
255 | return name_node; | |
ff927412 JP |
256 | } |
257 | ||
258 | static void netdev_name_node_free(struct netdev_name_node *name_node) | |
259 | { | |
260 | kfree(name_node); | |
261 | } | |
262 | ||
263 | static void netdev_name_node_add(struct net *net, | |
264 | struct netdev_name_node *name_node) | |
265 | { | |
266 | hlist_add_head_rcu(&name_node->hlist, | |
267 | dev_name_hash(net, name_node->name)); | |
268 | } | |
269 | ||
270 | static void netdev_name_node_del(struct netdev_name_node *name_node) | |
271 | { | |
272 | hlist_del_rcu(&name_node->hlist); | |
273 | } | |
274 | ||
275 | static struct netdev_name_node *netdev_name_node_lookup(struct net *net, | |
276 | const char *name) | |
277 | { | |
278 | struct hlist_head *head = dev_name_hash(net, name); | |
279 | struct netdev_name_node *name_node; | |
280 | ||
281 | hlist_for_each_entry(name_node, head, hlist) | |
282 | if (!strcmp(name_node->name, name)) | |
283 | return name_node; | |
284 | return NULL; | |
285 | } | |
286 | ||
287 | static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net, | |
288 | const char *name) | |
289 | { | |
290 | struct hlist_head *head = dev_name_hash(net, name); | |
291 | struct netdev_name_node *name_node; | |
292 | ||
293 | hlist_for_each_entry_rcu(name_node, head, hlist) | |
294 | if (!strcmp(name_node->name, name)) | |
295 | return name_node; | |
296 | return NULL; | |
297 | } | |
298 | ||
36fbf1e5 JP |
299 | int netdev_name_node_alt_create(struct net_device *dev, const char *name) |
300 | { | |
301 | struct netdev_name_node *name_node; | |
302 | struct net *net = dev_net(dev); | |
303 | ||
304 | name_node = netdev_name_node_lookup(net, name); | |
305 | if (name_node) | |
306 | return -EEXIST; | |
307 | name_node = netdev_name_node_alloc(dev, name); | |
308 | if (!name_node) | |
309 | return -ENOMEM; | |
310 | netdev_name_node_add(net, name_node); | |
311 | /* The node that holds dev->name acts as a head of per-device list. */ | |
312 | list_add_tail(&name_node->list, &dev->name_node->list); | |
313 | ||
314 | return 0; | |
315 | } | |
316 | EXPORT_SYMBOL(netdev_name_node_alt_create); | |
317 | ||
318 | static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node) | |
319 | { | |
320 | list_del(&name_node->list); | |
321 | netdev_name_node_del(name_node); | |
322 | kfree(name_node->name); | |
323 | netdev_name_node_free(name_node); | |
324 | } | |
325 | ||
326 | int netdev_name_node_alt_destroy(struct net_device *dev, const char *name) | |
327 | { | |
328 | struct netdev_name_node *name_node; | |
329 | struct net *net = dev_net(dev); | |
330 | ||
331 | name_node = netdev_name_node_lookup(net, name); | |
332 | if (!name_node) | |
333 | return -ENOENT; | |
334 | __netdev_name_node_alt_destroy(name_node); | |
335 | ||
336 | return 0; | |
337 | } | |
338 | EXPORT_SYMBOL(netdev_name_node_alt_destroy); | |
339 | ||
340 | static void netdev_name_node_alt_flush(struct net_device *dev) | |
341 | { | |
342 | struct netdev_name_node *name_node, *tmp; | |
343 | ||
344 | list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list) | |
345 | __netdev_name_node_alt_destroy(name_node); | |
346 | } | |
347 | ||
ce286d32 | 348 | /* Device list insertion */ |
53759be9 | 349 | static void list_netdevice(struct net_device *dev) |
ce286d32 | 350 | { |
c346dca1 | 351 | struct net *net = dev_net(dev); |
ce286d32 EB |
352 | |
353 | ASSERT_RTNL(); | |
354 | ||
355 | write_lock_bh(&dev_base_lock); | |
c6d14c84 | 356 | list_add_tail_rcu(&dev->dev_list, &net->dev_base_head); |
ff927412 | 357 | netdev_name_node_add(net, dev->name_node); |
fb699dfd ED |
358 | hlist_add_head_rcu(&dev->index_hlist, |
359 | dev_index_hash(net, dev->ifindex)); | |
ce286d32 | 360 | write_unlock_bh(&dev_base_lock); |
4e985ada TG |
361 | |
362 | dev_base_seq_inc(net); | |
ce286d32 EB |
363 | } |
364 | ||
fb699dfd ED |
365 | /* Device list removal |
366 | * caller must respect a RCU grace period before freeing/reusing dev | |
367 | */ | |
ce286d32 EB |
368 | static void unlist_netdevice(struct net_device *dev) |
369 | { | |
370 | ASSERT_RTNL(); | |
371 | ||
372 | /* Unlink dev from the device chain */ | |
373 | write_lock_bh(&dev_base_lock); | |
c6d14c84 | 374 | list_del_rcu(&dev->dev_list); |
ff927412 | 375 | netdev_name_node_del(dev->name_node); |
fb699dfd | 376 | hlist_del_rcu(&dev->index_hlist); |
ce286d32 | 377 | write_unlock_bh(&dev_base_lock); |
4e985ada TG |
378 | |
379 | dev_base_seq_inc(dev_net(dev)); | |
ce286d32 EB |
380 | } |
381 | ||
1da177e4 LT |
382 | /* |
383 | * Our notifier list | |
384 | */ | |
385 | ||
f07d5b94 | 386 | static RAW_NOTIFIER_HEAD(netdev_chain); |
1da177e4 LT |
387 | |
388 | /* | |
389 | * Device drivers call our routines to queue packets here. We empty the | |
390 | * queue in the local softnet handler. | |
391 | */ | |
bea3348e | 392 | |
9958da05 | 393 | DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data); |
d1b19dff | 394 | EXPORT_PER_CPU_SYMBOL(softnet_data); |
1da177e4 | 395 | |
1da177e4 | 396 | /******************************************************************************* |
eb13da1a | 397 | * |
398 | * Protocol management and registration routines | |
399 | * | |
400 | *******************************************************************************/ | |
1da177e4 | 401 | |
1da177e4 | 402 | |
1da177e4 LT |
403 | /* |
404 | * Add a protocol ID to the list. Now that the input handler is | |
405 | * smarter we can dispense with all the messy stuff that used to be | |
406 | * here. | |
407 | * | |
408 | * BEWARE!!! Protocol handlers, mangling input packets, | |
409 | * MUST BE last in hash buckets and checking protocol handlers | |
410 | * MUST start from promiscuous ptype_all chain in net_bh. | |
411 | * It is true now, do not change it. | |
412 | * Explanation follows: if protocol handler, mangling packet, will | |
413 | * be the first on list, it is not able to sense, that packet | |
414 | * is cloned and should be copied-on-write, so that it will | |
415 | * change it and subsequent readers will get broken packet. | |
416 | * --ANK (980803) | |
417 | */ | |
418 | ||
c07b68e8 ED |
419 | static inline struct list_head *ptype_head(const struct packet_type *pt) |
420 | { | |
421 | if (pt->type == htons(ETH_P_ALL)) | |
7866a621 | 422 | return pt->dev ? &pt->dev->ptype_all : &ptype_all; |
c07b68e8 | 423 | else |
7866a621 SN |
424 | return pt->dev ? &pt->dev->ptype_specific : |
425 | &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK]; | |
c07b68e8 ED |
426 | } |
427 | ||
1da177e4 LT |
428 | /** |
429 | * dev_add_pack - add packet handler | |
430 | * @pt: packet type declaration | |
431 | * | |
432 | * Add a protocol handler to the networking stack. The passed &packet_type | |
433 | * is linked into kernel lists and may not be freed until it has been | |
434 | * removed from the kernel lists. | |
435 | * | |
4ec93edb | 436 | * This call does not sleep therefore it can not |
1da177e4 LT |
437 | * guarantee all CPU's that are in middle of receiving packets |
438 | * will see the new packet type (until the next received packet). | |
439 | */ | |
440 | ||
441 | void dev_add_pack(struct packet_type *pt) | |
442 | { | |
c07b68e8 | 443 | struct list_head *head = ptype_head(pt); |
1da177e4 | 444 | |
c07b68e8 ED |
445 | spin_lock(&ptype_lock); |
446 | list_add_rcu(&pt->list, head); | |
447 | spin_unlock(&ptype_lock); | |
1da177e4 | 448 | } |
d1b19dff | 449 | EXPORT_SYMBOL(dev_add_pack); |
1da177e4 | 450 | |
1da177e4 LT |
451 | /** |
452 | * __dev_remove_pack - remove packet handler | |
453 | * @pt: packet type declaration | |
454 | * | |
455 | * Remove a protocol handler that was previously added to the kernel | |
456 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed | |
457 | * from the kernel lists and can be freed or reused once this function | |
4ec93edb | 458 | * returns. |
1da177e4 LT |
459 | * |
460 | * The packet type might still be in use by receivers | |
461 | * and must not be freed until after all the CPU's have gone | |
462 | * through a quiescent state. | |
463 | */ | |
464 | void __dev_remove_pack(struct packet_type *pt) | |
465 | { | |
c07b68e8 | 466 | struct list_head *head = ptype_head(pt); |
1da177e4 LT |
467 | struct packet_type *pt1; |
468 | ||
c07b68e8 | 469 | spin_lock(&ptype_lock); |
1da177e4 LT |
470 | |
471 | list_for_each_entry(pt1, head, list) { | |
472 | if (pt == pt1) { | |
473 | list_del_rcu(&pt->list); | |
474 | goto out; | |
475 | } | |
476 | } | |
477 | ||
7b6cd1ce | 478 | pr_warn("dev_remove_pack: %p not found\n", pt); |
1da177e4 | 479 | out: |
c07b68e8 | 480 | spin_unlock(&ptype_lock); |
1da177e4 | 481 | } |
d1b19dff ED |
482 | EXPORT_SYMBOL(__dev_remove_pack); |
483 | ||
1da177e4 LT |
484 | /** |
485 | * dev_remove_pack - remove packet handler | |
486 | * @pt: packet type declaration | |
487 | * | |
488 | * Remove a protocol handler that was previously added to the kernel | |
489 | * protocol handlers by dev_add_pack(). The passed &packet_type is removed | |
490 | * from the kernel lists and can be freed or reused once this function | |
491 | * returns. | |
492 | * | |
493 | * This call sleeps to guarantee that no CPU is looking at the packet | |
494 | * type after return. | |
495 | */ | |
496 | void dev_remove_pack(struct packet_type *pt) | |
497 | { | |
498 | __dev_remove_pack(pt); | |
4ec93edb | 499 | |
1da177e4 LT |
500 | synchronize_net(); |
501 | } | |
d1b19dff | 502 | EXPORT_SYMBOL(dev_remove_pack); |
1da177e4 | 503 | |
62532da9 VY |
504 | |
505 | /** | |
506 | * dev_add_offload - register offload handlers | |
507 | * @po: protocol offload declaration | |
508 | * | |
509 | * Add protocol offload handlers to the networking stack. The passed | |
510 | * &proto_offload is linked into kernel lists and may not be freed until | |
511 | * it has been removed from the kernel lists. | |
512 | * | |
513 | * This call does not sleep therefore it can not | |
514 | * guarantee all CPU's that are in middle of receiving packets | |
515 | * will see the new offload handlers (until the next received packet). | |
516 | */ | |
517 | void dev_add_offload(struct packet_offload *po) | |
518 | { | |
bdef7de4 | 519 | struct packet_offload *elem; |
62532da9 VY |
520 | |
521 | spin_lock(&offload_lock); | |
bdef7de4 DM |
522 | list_for_each_entry(elem, &offload_base, list) { |
523 | if (po->priority < elem->priority) | |
524 | break; | |
525 | } | |
526 | list_add_rcu(&po->list, elem->list.prev); | |
62532da9 VY |
527 | spin_unlock(&offload_lock); |
528 | } | |
529 | EXPORT_SYMBOL(dev_add_offload); | |
530 | ||
531 | /** | |
532 | * __dev_remove_offload - remove offload handler | |
533 | * @po: packet offload declaration | |
534 | * | |
535 | * Remove a protocol offload handler that was previously added to the | |
536 | * kernel offload handlers by dev_add_offload(). The passed &offload_type | |
537 | * is removed from the kernel lists and can be freed or reused once this | |
538 | * function returns. | |
539 | * | |
540 | * The packet type might still be in use by receivers | |
541 | * and must not be freed until after all the CPU's have gone | |
542 | * through a quiescent state. | |
543 | */ | |
1d143d9f | 544 | static void __dev_remove_offload(struct packet_offload *po) |
62532da9 VY |
545 | { |
546 | struct list_head *head = &offload_base; | |
547 | struct packet_offload *po1; | |
548 | ||
c53aa505 | 549 | spin_lock(&offload_lock); |
62532da9 VY |
550 | |
551 | list_for_each_entry(po1, head, list) { | |
552 | if (po == po1) { | |
553 | list_del_rcu(&po->list); | |
554 | goto out; | |
555 | } | |
556 | } | |
557 | ||
558 | pr_warn("dev_remove_offload: %p not found\n", po); | |
559 | out: | |
c53aa505 | 560 | spin_unlock(&offload_lock); |
62532da9 | 561 | } |
62532da9 VY |
562 | |
563 | /** | |
564 | * dev_remove_offload - remove packet offload handler | |
565 | * @po: packet offload declaration | |
566 | * | |
567 | * Remove a packet offload handler that was previously added to the kernel | |
568 | * offload handlers by dev_add_offload(). The passed &offload_type is | |
569 | * removed from the kernel lists and can be freed or reused once this | |
570 | * function returns. | |
571 | * | |
572 | * This call sleeps to guarantee that no CPU is looking at the packet | |
573 | * type after return. | |
574 | */ | |
575 | void dev_remove_offload(struct packet_offload *po) | |
576 | { | |
577 | __dev_remove_offload(po); | |
578 | ||
579 | synchronize_net(); | |
580 | } | |
581 | EXPORT_SYMBOL(dev_remove_offload); | |
582 | ||
1da177e4 | 583 | /****************************************************************************** |
eb13da1a | 584 | * |
585 | * Device Boot-time Settings Routines | |
586 | * | |
587 | ******************************************************************************/ | |
1da177e4 LT |
588 | |
589 | /* Boot time configuration table */ | |
590 | static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX]; | |
591 | ||
592 | /** | |
593 | * netdev_boot_setup_add - add new setup entry | |
594 | * @name: name of the device | |
595 | * @map: configured settings for the device | |
596 | * | |
597 | * Adds new setup entry to the dev_boot_setup list. The function | |
598 | * returns 0 on error and 1 on success. This is a generic routine to | |
599 | * all netdevices. | |
600 | */ | |
601 | static int netdev_boot_setup_add(char *name, struct ifmap *map) | |
602 | { | |
603 | struct netdev_boot_setup *s; | |
604 | int i; | |
605 | ||
606 | s = dev_boot_setup; | |
607 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { | |
608 | if (s[i].name[0] == '\0' || s[i].name[0] == ' ') { | |
609 | memset(s[i].name, 0, sizeof(s[i].name)); | |
93b3cff9 | 610 | strlcpy(s[i].name, name, IFNAMSIZ); |
1da177e4 LT |
611 | memcpy(&s[i].map, map, sizeof(s[i].map)); |
612 | break; | |
613 | } | |
614 | } | |
615 | ||
616 | return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1; | |
617 | } | |
618 | ||
619 | /** | |
722c9a0c | 620 | * netdev_boot_setup_check - check boot time settings |
621 | * @dev: the netdevice | |
1da177e4 | 622 | * |
722c9a0c | 623 | * Check boot time settings for the device. |
624 | * The found settings are set for the device to be used | |
625 | * later in the device probing. | |
626 | * Returns 0 if no settings found, 1 if they are. | |
1da177e4 LT |
627 | */ |
628 | int netdev_boot_setup_check(struct net_device *dev) | |
629 | { | |
630 | struct netdev_boot_setup *s = dev_boot_setup; | |
631 | int i; | |
632 | ||
633 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) { | |
634 | if (s[i].name[0] != '\0' && s[i].name[0] != ' ' && | |
93b3cff9 | 635 | !strcmp(dev->name, s[i].name)) { |
722c9a0c | 636 | dev->irq = s[i].map.irq; |
637 | dev->base_addr = s[i].map.base_addr; | |
638 | dev->mem_start = s[i].map.mem_start; | |
639 | dev->mem_end = s[i].map.mem_end; | |
1da177e4 LT |
640 | return 1; |
641 | } | |
642 | } | |
643 | return 0; | |
644 | } | |
d1b19dff | 645 | EXPORT_SYMBOL(netdev_boot_setup_check); |
1da177e4 LT |
646 | |
647 | ||
648 | /** | |
722c9a0c | 649 | * netdev_boot_base - get address from boot time settings |
650 | * @prefix: prefix for network device | |
651 | * @unit: id for network device | |
652 | * | |
653 | * Check boot time settings for the base address of device. | |
654 | * The found settings are set for the device to be used | |
655 | * later in the device probing. | |
656 | * Returns 0 if no settings found. | |
1da177e4 LT |
657 | */ |
658 | unsigned long netdev_boot_base(const char *prefix, int unit) | |
659 | { | |
660 | const struct netdev_boot_setup *s = dev_boot_setup; | |
661 | char name[IFNAMSIZ]; | |
662 | int i; | |
663 | ||
664 | sprintf(name, "%s%d", prefix, unit); | |
665 | ||
666 | /* | |
667 | * If device already registered then return base of 1 | |
668 | * to indicate not to probe for this interface | |
669 | */ | |
881d966b | 670 | if (__dev_get_by_name(&init_net, name)) |
1da177e4 LT |
671 | return 1; |
672 | ||
673 | for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) | |
674 | if (!strcmp(name, s[i].name)) | |
675 | return s[i].map.base_addr; | |
676 | return 0; | |
677 | } | |
678 | ||
679 | /* | |
680 | * Saves at boot time configured settings for any netdevice. | |
681 | */ | |
682 | int __init netdev_boot_setup(char *str) | |
683 | { | |
684 | int ints[5]; | |
685 | struct ifmap map; | |
686 | ||
687 | str = get_options(str, ARRAY_SIZE(ints), ints); | |
688 | if (!str || !*str) | |
689 | return 0; | |
690 | ||
691 | /* Save settings */ | |
692 | memset(&map, 0, sizeof(map)); | |
693 | if (ints[0] > 0) | |
694 | map.irq = ints[1]; | |
695 | if (ints[0] > 1) | |
696 | map.base_addr = ints[2]; | |
697 | if (ints[0] > 2) | |
698 | map.mem_start = ints[3]; | |
699 | if (ints[0] > 3) | |
700 | map.mem_end = ints[4]; | |
701 | ||
702 | /* Add new entry to the list */ | |
703 | return netdev_boot_setup_add(str, &map); | |
704 | } | |
705 | ||
706 | __setup("netdev=", netdev_boot_setup); | |
707 | ||
708 | /******************************************************************************* | |
eb13da1a | 709 | * |
710 | * Device Interface Subroutines | |
711 | * | |
712 | *******************************************************************************/ | |
1da177e4 | 713 | |
a54acb3a ND |
714 | /** |
715 | * dev_get_iflink - get 'iflink' value of a interface | |
716 | * @dev: targeted interface | |
717 | * | |
718 | * Indicates the ifindex the interface is linked to. | |
719 | * Physical interfaces have the same 'ifindex' and 'iflink' values. | |
720 | */ | |
721 | ||
722 | int dev_get_iflink(const struct net_device *dev) | |
723 | { | |
724 | if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink) | |
725 | return dev->netdev_ops->ndo_get_iflink(dev); | |
726 | ||
7a66bbc9 | 727 | return dev->ifindex; |
a54acb3a ND |
728 | } |
729 | EXPORT_SYMBOL(dev_get_iflink); | |
730 | ||
fc4099f1 PS |
731 | /** |
732 | * dev_fill_metadata_dst - Retrieve tunnel egress information. | |
733 | * @dev: targeted interface | |
734 | * @skb: The packet. | |
735 | * | |
736 | * For better visibility of tunnel traffic OVS needs to retrieve | |
737 | * egress tunnel information for a packet. Following API allows | |
738 | * user to get this info. | |
739 | */ | |
740 | int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb) | |
741 | { | |
742 | struct ip_tunnel_info *info; | |
743 | ||
744 | if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst) | |
745 | return -EINVAL; | |
746 | ||
747 | info = skb_tunnel_info_unclone(skb); | |
748 | if (!info) | |
749 | return -ENOMEM; | |
750 | if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX))) | |
751 | return -EINVAL; | |
752 | ||
753 | return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb); | |
754 | } | |
755 | EXPORT_SYMBOL_GPL(dev_fill_metadata_dst); | |
756 | ||
1da177e4 LT |
757 | /** |
758 | * __dev_get_by_name - find a device by its name | |
c4ea43c5 | 759 | * @net: the applicable net namespace |
1da177e4 LT |
760 | * @name: name to find |
761 | * | |
762 | * Find an interface by name. Must be called under RTNL semaphore | |
763 | * or @dev_base_lock. If the name is found a pointer to the device | |
764 | * is returned. If the name is not found then %NULL is returned. The | |
765 | * reference counters are not incremented so the caller must be | |
766 | * careful with locks. | |
767 | */ | |
768 | ||
881d966b | 769 | struct net_device *__dev_get_by_name(struct net *net, const char *name) |
1da177e4 | 770 | { |
ff927412 | 771 | struct netdev_name_node *node_name; |
1da177e4 | 772 | |
ff927412 JP |
773 | node_name = netdev_name_node_lookup(net, name); |
774 | return node_name ? node_name->dev : NULL; | |
1da177e4 | 775 | } |
d1b19dff | 776 | EXPORT_SYMBOL(__dev_get_by_name); |
1da177e4 | 777 | |
72c9528b | 778 | /** |
722c9a0c | 779 | * dev_get_by_name_rcu - find a device by its name |
780 | * @net: the applicable net namespace | |
781 | * @name: name to find | |
782 | * | |
783 | * Find an interface by name. | |
784 | * If the name is found a pointer to the device is returned. | |
785 | * If the name is not found then %NULL is returned. | |
786 | * The reference counters are not incremented so the caller must be | |
787 | * careful with locks. The caller must hold RCU lock. | |
72c9528b ED |
788 | */ |
789 | ||
790 | struct net_device *dev_get_by_name_rcu(struct net *net, const char *name) | |
791 | { | |
ff927412 | 792 | struct netdev_name_node *node_name; |
72c9528b | 793 | |
ff927412 JP |
794 | node_name = netdev_name_node_lookup_rcu(net, name); |
795 | return node_name ? node_name->dev : NULL; | |
72c9528b ED |
796 | } |
797 | EXPORT_SYMBOL(dev_get_by_name_rcu); | |
798 | ||
1da177e4 LT |
799 | /** |
800 | * dev_get_by_name - find a device by its name | |
c4ea43c5 | 801 | * @net: the applicable net namespace |
1da177e4 LT |
802 | * @name: name to find |
803 | * | |
804 | * Find an interface by name. This can be called from any | |
805 | * context and does its own locking. The returned handle has | |
806 | * the usage count incremented and the caller must use dev_put() to | |
807 | * release it when it is no longer needed. %NULL is returned if no | |
808 | * matching device is found. | |
809 | */ | |
810 | ||
881d966b | 811 | struct net_device *dev_get_by_name(struct net *net, const char *name) |
1da177e4 LT |
812 | { |
813 | struct net_device *dev; | |
814 | ||
72c9528b ED |
815 | rcu_read_lock(); |
816 | dev = dev_get_by_name_rcu(net, name); | |
1da177e4 LT |
817 | if (dev) |
818 | dev_hold(dev); | |
72c9528b | 819 | rcu_read_unlock(); |
1da177e4 LT |
820 | return dev; |
821 | } | |
d1b19dff | 822 | EXPORT_SYMBOL(dev_get_by_name); |
1da177e4 LT |
823 | |
824 | /** | |
825 | * __dev_get_by_index - find a device by its ifindex | |
c4ea43c5 | 826 | * @net: the applicable net namespace |
1da177e4 LT |
827 | * @ifindex: index of device |
828 | * | |
829 | * Search for an interface by index. Returns %NULL if the device | |
830 | * is not found or a pointer to the device. The device has not | |
831 | * had its reference counter increased so the caller must be careful | |
832 | * about locking. The caller must hold either the RTNL semaphore | |
833 | * or @dev_base_lock. | |
834 | */ | |
835 | ||
881d966b | 836 | struct net_device *__dev_get_by_index(struct net *net, int ifindex) |
1da177e4 | 837 | { |
0bd8d536 ED |
838 | struct net_device *dev; |
839 | struct hlist_head *head = dev_index_hash(net, ifindex); | |
1da177e4 | 840 | |
b67bfe0d | 841 | hlist_for_each_entry(dev, head, index_hlist) |
1da177e4 LT |
842 | if (dev->ifindex == ifindex) |
843 | return dev; | |
0bd8d536 | 844 | |
1da177e4 LT |
845 | return NULL; |
846 | } | |
d1b19dff | 847 | EXPORT_SYMBOL(__dev_get_by_index); |
1da177e4 | 848 | |
fb699dfd ED |
849 | /** |
850 | * dev_get_by_index_rcu - find a device by its ifindex | |
851 | * @net: the applicable net namespace | |
852 | * @ifindex: index of device | |
853 | * | |
854 | * Search for an interface by index. Returns %NULL if the device | |
855 | * is not found or a pointer to the device. The device has not | |
856 | * had its reference counter increased so the caller must be careful | |
857 | * about locking. The caller must hold RCU lock. | |
858 | */ | |
859 | ||
860 | struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex) | |
861 | { | |
fb699dfd ED |
862 | struct net_device *dev; |
863 | struct hlist_head *head = dev_index_hash(net, ifindex); | |
864 | ||
b67bfe0d | 865 | hlist_for_each_entry_rcu(dev, head, index_hlist) |
fb699dfd ED |
866 | if (dev->ifindex == ifindex) |
867 | return dev; | |
868 | ||
869 | return NULL; | |
870 | } | |
871 | EXPORT_SYMBOL(dev_get_by_index_rcu); | |
872 | ||
1da177e4 LT |
873 | |
874 | /** | |
875 | * dev_get_by_index - find a device by its ifindex | |
c4ea43c5 | 876 | * @net: the applicable net namespace |
1da177e4 LT |
877 | * @ifindex: index of device |
878 | * | |
879 | * Search for an interface by index. Returns NULL if the device | |
880 | * is not found or a pointer to the device. The device returned has | |
881 | * had a reference added and the pointer is safe until the user calls | |
882 | * dev_put to indicate they have finished with it. | |
883 | */ | |
884 | ||
881d966b | 885 | struct net_device *dev_get_by_index(struct net *net, int ifindex) |
1da177e4 LT |
886 | { |
887 | struct net_device *dev; | |
888 | ||
fb699dfd ED |
889 | rcu_read_lock(); |
890 | dev = dev_get_by_index_rcu(net, ifindex); | |
1da177e4 LT |
891 | if (dev) |
892 | dev_hold(dev); | |
fb699dfd | 893 | rcu_read_unlock(); |
1da177e4 LT |
894 | return dev; |
895 | } | |
d1b19dff | 896 | EXPORT_SYMBOL(dev_get_by_index); |
1da177e4 | 897 | |
90b602f8 ML |
898 | /** |
899 | * dev_get_by_napi_id - find a device by napi_id | |
900 | * @napi_id: ID of the NAPI struct | |
901 | * | |
902 | * Search for an interface by NAPI ID. Returns %NULL if the device | |
903 | * is not found or a pointer to the device. The device has not had | |
904 | * its reference counter increased so the caller must be careful | |
905 | * about locking. The caller must hold RCU lock. | |
906 | */ | |
907 | ||
908 | struct net_device *dev_get_by_napi_id(unsigned int napi_id) | |
909 | { | |
910 | struct napi_struct *napi; | |
911 | ||
912 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
913 | ||
914 | if (napi_id < MIN_NAPI_ID) | |
915 | return NULL; | |
916 | ||
917 | napi = napi_by_id(napi_id); | |
918 | ||
919 | return napi ? napi->dev : NULL; | |
920 | } | |
921 | EXPORT_SYMBOL(dev_get_by_napi_id); | |
922 | ||
5dbe7c17 NS |
923 | /** |
924 | * netdev_get_name - get a netdevice name, knowing its ifindex. | |
925 | * @net: network namespace | |
926 | * @name: a pointer to the buffer where the name will be stored. | |
927 | * @ifindex: the ifindex of the interface to get the name from. | |
928 | * | |
929 | * The use of raw_seqcount_begin() and cond_resched() before | |
930 | * retrying is required as we want to give the writers a chance | |
2da2b32f | 931 | * to complete when CONFIG_PREEMPTION is not set. |
5dbe7c17 NS |
932 | */ |
933 | int netdev_get_name(struct net *net, char *name, int ifindex) | |
934 | { | |
935 | struct net_device *dev; | |
936 | unsigned int seq; | |
937 | ||
938 | retry: | |
939 | seq = raw_seqcount_begin(&devnet_rename_seq); | |
940 | rcu_read_lock(); | |
941 | dev = dev_get_by_index_rcu(net, ifindex); | |
942 | if (!dev) { | |
943 | rcu_read_unlock(); | |
944 | return -ENODEV; | |
945 | } | |
946 | ||
947 | strcpy(name, dev->name); | |
948 | rcu_read_unlock(); | |
949 | if (read_seqcount_retry(&devnet_rename_seq, seq)) { | |
950 | cond_resched(); | |
951 | goto retry; | |
952 | } | |
953 | ||
954 | return 0; | |
955 | } | |
956 | ||
1da177e4 | 957 | /** |
941666c2 | 958 | * dev_getbyhwaddr_rcu - find a device by its hardware address |
c4ea43c5 | 959 | * @net: the applicable net namespace |
1da177e4 LT |
960 | * @type: media type of device |
961 | * @ha: hardware address | |
962 | * | |
963 | * Search for an interface by MAC address. Returns NULL if the device | |
c506653d ED |
964 | * is not found or a pointer to the device. |
965 | * The caller must hold RCU or RTNL. | |
941666c2 | 966 | * The returned device has not had its ref count increased |
1da177e4 LT |
967 | * and the caller must therefore be careful about locking |
968 | * | |
1da177e4 LT |
969 | */ |
970 | ||
941666c2 ED |
971 | struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type, |
972 | const char *ha) | |
1da177e4 LT |
973 | { |
974 | struct net_device *dev; | |
975 | ||
941666c2 | 976 | for_each_netdev_rcu(net, dev) |
1da177e4 LT |
977 | if (dev->type == type && |
978 | !memcmp(dev->dev_addr, ha, dev->addr_len)) | |
7562f876 PE |
979 | return dev; |
980 | ||
981 | return NULL; | |
1da177e4 | 982 | } |
941666c2 | 983 | EXPORT_SYMBOL(dev_getbyhwaddr_rcu); |
cf309e3f | 984 | |
881d966b | 985 | struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type) |
1da177e4 LT |
986 | { |
987 | struct net_device *dev; | |
988 | ||
4e9cac2b | 989 | ASSERT_RTNL(); |
881d966b | 990 | for_each_netdev(net, dev) |
4e9cac2b | 991 | if (dev->type == type) |
7562f876 PE |
992 | return dev; |
993 | ||
994 | return NULL; | |
4e9cac2b | 995 | } |
4e9cac2b PM |
996 | EXPORT_SYMBOL(__dev_getfirstbyhwtype); |
997 | ||
881d966b | 998 | struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type) |
4e9cac2b | 999 | { |
99fe3c39 | 1000 | struct net_device *dev, *ret = NULL; |
4e9cac2b | 1001 | |
99fe3c39 ED |
1002 | rcu_read_lock(); |
1003 | for_each_netdev_rcu(net, dev) | |
1004 | if (dev->type == type) { | |
1005 | dev_hold(dev); | |
1006 | ret = dev; | |
1007 | break; | |
1008 | } | |
1009 | rcu_read_unlock(); | |
1010 | return ret; | |
1da177e4 | 1011 | } |
1da177e4 LT |
1012 | EXPORT_SYMBOL(dev_getfirstbyhwtype); |
1013 | ||
1014 | /** | |
6c555490 | 1015 | * __dev_get_by_flags - find any device with given flags |
c4ea43c5 | 1016 | * @net: the applicable net namespace |
1da177e4 LT |
1017 | * @if_flags: IFF_* values |
1018 | * @mask: bitmask of bits in if_flags to check | |
1019 | * | |
1020 | * Search for any interface with the given flags. Returns NULL if a device | |
bb69ae04 | 1021 | * is not found or a pointer to the device. Must be called inside |
6c555490 | 1022 | * rtnl_lock(), and result refcount is unchanged. |
1da177e4 LT |
1023 | */ |
1024 | ||
6c555490 WC |
1025 | struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags, |
1026 | unsigned short mask) | |
1da177e4 | 1027 | { |
7562f876 | 1028 | struct net_device *dev, *ret; |
1da177e4 | 1029 | |
6c555490 WC |
1030 | ASSERT_RTNL(); |
1031 | ||
7562f876 | 1032 | ret = NULL; |
6c555490 | 1033 | for_each_netdev(net, dev) { |
1da177e4 | 1034 | if (((dev->flags ^ if_flags) & mask) == 0) { |
7562f876 | 1035 | ret = dev; |
1da177e4 LT |
1036 | break; |
1037 | } | |
1038 | } | |
7562f876 | 1039 | return ret; |
1da177e4 | 1040 | } |
6c555490 | 1041 | EXPORT_SYMBOL(__dev_get_by_flags); |
1da177e4 LT |
1042 | |
1043 | /** | |
1044 | * dev_valid_name - check if name is okay for network device | |
1045 | * @name: name string | |
1046 | * | |
1047 | * Network device names need to be valid file names to | |
c7fa9d18 DM |
1048 | * to allow sysfs to work. We also disallow any kind of |
1049 | * whitespace. | |
1da177e4 | 1050 | */ |
95f050bf | 1051 | bool dev_valid_name(const char *name) |
1da177e4 | 1052 | { |
c7fa9d18 | 1053 | if (*name == '\0') |
95f050bf | 1054 | return false; |
a9d48205 | 1055 | if (strnlen(name, IFNAMSIZ) == IFNAMSIZ) |
95f050bf | 1056 | return false; |
c7fa9d18 | 1057 | if (!strcmp(name, ".") || !strcmp(name, "..")) |
95f050bf | 1058 | return false; |
c7fa9d18 DM |
1059 | |
1060 | while (*name) { | |
a4176a93 | 1061 | if (*name == '/' || *name == ':' || isspace(*name)) |
95f050bf | 1062 | return false; |
c7fa9d18 DM |
1063 | name++; |
1064 | } | |
95f050bf | 1065 | return true; |
1da177e4 | 1066 | } |
d1b19dff | 1067 | EXPORT_SYMBOL(dev_valid_name); |
1da177e4 LT |
1068 | |
1069 | /** | |
b267b179 EB |
1070 | * __dev_alloc_name - allocate a name for a device |
1071 | * @net: network namespace to allocate the device name in | |
1da177e4 | 1072 | * @name: name format string |
b267b179 | 1073 | * @buf: scratch buffer and result name string |
1da177e4 LT |
1074 | * |
1075 | * Passed a format string - eg "lt%d" it will try and find a suitable | |
3041a069 SH |
1076 | * id. It scans list of devices to build up a free map, then chooses |
1077 | * the first empty slot. The caller must hold the dev_base or rtnl lock | |
1078 | * while allocating the name and adding the device in order to avoid | |
1079 | * duplicates. | |
1080 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). | |
1081 | * Returns the number of the unit assigned or a negative errno code. | |
1da177e4 LT |
1082 | */ |
1083 | ||
b267b179 | 1084 | static int __dev_alloc_name(struct net *net, const char *name, char *buf) |
1da177e4 LT |
1085 | { |
1086 | int i = 0; | |
1da177e4 LT |
1087 | const char *p; |
1088 | const int max_netdevices = 8*PAGE_SIZE; | |
cfcabdcc | 1089 | unsigned long *inuse; |
1da177e4 LT |
1090 | struct net_device *d; |
1091 | ||
93809105 RV |
1092 | if (!dev_valid_name(name)) |
1093 | return -EINVAL; | |
1094 | ||
51f299dd | 1095 | p = strchr(name, '%'); |
1da177e4 LT |
1096 | if (p) { |
1097 | /* | |
1098 | * Verify the string as this thing may have come from | |
1099 | * the user. There must be either one "%d" and no other "%" | |
1100 | * characters. | |
1101 | */ | |
1102 | if (p[1] != 'd' || strchr(p + 2, '%')) | |
1103 | return -EINVAL; | |
1104 | ||
1105 | /* Use one page as a bit array of possible slots */ | |
cfcabdcc | 1106 | inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC); |
1da177e4 LT |
1107 | if (!inuse) |
1108 | return -ENOMEM; | |
1109 | ||
881d966b | 1110 | for_each_netdev(net, d) { |
1da177e4 LT |
1111 | if (!sscanf(d->name, name, &i)) |
1112 | continue; | |
1113 | if (i < 0 || i >= max_netdevices) | |
1114 | continue; | |
1115 | ||
1116 | /* avoid cases where sscanf is not exact inverse of printf */ | |
b267b179 | 1117 | snprintf(buf, IFNAMSIZ, name, i); |
1da177e4 LT |
1118 | if (!strncmp(buf, d->name, IFNAMSIZ)) |
1119 | set_bit(i, inuse); | |
1120 | } | |
1121 | ||
1122 | i = find_first_zero_bit(inuse, max_netdevices); | |
1123 | free_page((unsigned long) inuse); | |
1124 | } | |
1125 | ||
6224abda | 1126 | snprintf(buf, IFNAMSIZ, name, i); |
b267b179 | 1127 | if (!__dev_get_by_name(net, buf)) |
1da177e4 | 1128 | return i; |
1da177e4 LT |
1129 | |
1130 | /* It is possible to run out of possible slots | |
1131 | * when the name is long and there isn't enough space left | |
1132 | * for the digits, or if all bits are used. | |
1133 | */ | |
029b6d14 | 1134 | return -ENFILE; |
1da177e4 LT |
1135 | } |
1136 | ||
2c88b855 RV |
1137 | static int dev_alloc_name_ns(struct net *net, |
1138 | struct net_device *dev, | |
1139 | const char *name) | |
1140 | { | |
1141 | char buf[IFNAMSIZ]; | |
1142 | int ret; | |
1143 | ||
c46d7642 | 1144 | BUG_ON(!net); |
2c88b855 RV |
1145 | ret = __dev_alloc_name(net, name, buf); |
1146 | if (ret >= 0) | |
1147 | strlcpy(dev->name, buf, IFNAMSIZ); | |
1148 | return ret; | |
1da177e4 LT |
1149 | } |
1150 | ||
b267b179 EB |
1151 | /** |
1152 | * dev_alloc_name - allocate a name for a device | |
1153 | * @dev: device | |
1154 | * @name: name format string | |
1155 | * | |
1156 | * Passed a format string - eg "lt%d" it will try and find a suitable | |
1157 | * id. It scans list of devices to build up a free map, then chooses | |
1158 | * the first empty slot. The caller must hold the dev_base or rtnl lock | |
1159 | * while allocating the name and adding the device in order to avoid | |
1160 | * duplicates. | |
1161 | * Limited to bits_per_byte * page size devices (ie 32K on most platforms). | |
1162 | * Returns the number of the unit assigned or a negative errno code. | |
1163 | */ | |
1164 | ||
1165 | int dev_alloc_name(struct net_device *dev, const char *name) | |
1166 | { | |
c46d7642 | 1167 | return dev_alloc_name_ns(dev_net(dev), dev, name); |
b267b179 | 1168 | } |
d1b19dff | 1169 | EXPORT_SYMBOL(dev_alloc_name); |
b267b179 | 1170 | |
bacb7e18 ED |
1171 | static int dev_get_valid_name(struct net *net, struct net_device *dev, |
1172 | const char *name) | |
828de4f6 | 1173 | { |
55a5ec9b DM |
1174 | BUG_ON(!net); |
1175 | ||
1176 | if (!dev_valid_name(name)) | |
1177 | return -EINVAL; | |
1178 | ||
1179 | if (strchr(name, '%')) | |
1180 | return dev_alloc_name_ns(net, dev, name); | |
1181 | else if (__dev_get_by_name(net, name)) | |
1182 | return -EEXIST; | |
1183 | else if (dev->name != name) | |
1184 | strlcpy(dev->name, name, IFNAMSIZ); | |
1185 | ||
1186 | return 0; | |
d9031024 | 1187 | } |
1da177e4 LT |
1188 | |
1189 | /** | |
1190 | * dev_change_name - change name of a device | |
1191 | * @dev: device | |
1192 | * @newname: name (or format string) must be at least IFNAMSIZ | |
1193 | * | |
1194 | * Change name of a device, can pass format strings "eth%d". | |
1195 | * for wildcarding. | |
1196 | */ | |
cf04a4c7 | 1197 | int dev_change_name(struct net_device *dev, const char *newname) |
1da177e4 | 1198 | { |
238fa362 | 1199 | unsigned char old_assign_type; |
fcc5a03a | 1200 | char oldname[IFNAMSIZ]; |
1da177e4 | 1201 | int err = 0; |
fcc5a03a | 1202 | int ret; |
881d966b | 1203 | struct net *net; |
1da177e4 LT |
1204 | |
1205 | ASSERT_RTNL(); | |
c346dca1 | 1206 | BUG_ON(!dev_net(dev)); |
1da177e4 | 1207 | |
c346dca1 | 1208 | net = dev_net(dev); |
8065a779 SWL |
1209 | |
1210 | /* Some auto-enslaved devices e.g. failover slaves are | |
1211 | * special, as userspace might rename the device after | |
1212 | * the interface had been brought up and running since | |
1213 | * the point kernel initiated auto-enslavement. Allow | |
1214 | * live name change even when these slave devices are | |
1215 | * up and running. | |
1216 | * | |
1217 | * Typically, users of these auto-enslaving devices | |
1218 | * don't actually care about slave name change, as | |
1219 | * they are supposed to operate on master interface | |
1220 | * directly. | |
1221 | */ | |
1222 | if (dev->flags & IFF_UP && | |
1223 | likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK))) | |
1da177e4 LT |
1224 | return -EBUSY; |
1225 | ||
30e6c9fa | 1226 | write_seqcount_begin(&devnet_rename_seq); |
c91f6df2 BH |
1227 | |
1228 | if (strncmp(newname, dev->name, IFNAMSIZ) == 0) { | |
30e6c9fa | 1229 | write_seqcount_end(&devnet_rename_seq); |
c8d90dca | 1230 | return 0; |
c91f6df2 | 1231 | } |
c8d90dca | 1232 | |
fcc5a03a HX |
1233 | memcpy(oldname, dev->name, IFNAMSIZ); |
1234 | ||
828de4f6 | 1235 | err = dev_get_valid_name(net, dev, newname); |
c91f6df2 | 1236 | if (err < 0) { |
30e6c9fa | 1237 | write_seqcount_end(&devnet_rename_seq); |
d9031024 | 1238 | return err; |
c91f6df2 | 1239 | } |
1da177e4 | 1240 | |
6fe82a39 VF |
1241 | if (oldname[0] && !strchr(oldname, '%')) |
1242 | netdev_info(dev, "renamed from %s\n", oldname); | |
1243 | ||
238fa362 TG |
1244 | old_assign_type = dev->name_assign_type; |
1245 | dev->name_assign_type = NET_NAME_RENAMED; | |
1246 | ||
fcc5a03a | 1247 | rollback: |
a1b3f594 EB |
1248 | ret = device_rename(&dev->dev, dev->name); |
1249 | if (ret) { | |
1250 | memcpy(dev->name, oldname, IFNAMSIZ); | |
238fa362 | 1251 | dev->name_assign_type = old_assign_type; |
30e6c9fa | 1252 | write_seqcount_end(&devnet_rename_seq); |
a1b3f594 | 1253 | return ret; |
dcc99773 | 1254 | } |
7f988eab | 1255 | |
30e6c9fa | 1256 | write_seqcount_end(&devnet_rename_seq); |
c91f6df2 | 1257 | |
5bb025fa VF |
1258 | netdev_adjacent_rename_links(dev, oldname); |
1259 | ||
7f988eab | 1260 | write_lock_bh(&dev_base_lock); |
ff927412 | 1261 | netdev_name_node_del(dev->name_node); |
72c9528b ED |
1262 | write_unlock_bh(&dev_base_lock); |
1263 | ||
1264 | synchronize_rcu(); | |
1265 | ||
1266 | write_lock_bh(&dev_base_lock); | |
ff927412 | 1267 | netdev_name_node_add(net, dev->name_node); |
7f988eab HX |
1268 | write_unlock_bh(&dev_base_lock); |
1269 | ||
056925ab | 1270 | ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev); |
fcc5a03a HX |
1271 | ret = notifier_to_errno(ret); |
1272 | ||
1273 | if (ret) { | |
91e9c07b ED |
1274 | /* err >= 0 after dev_alloc_name() or stores the first errno */ |
1275 | if (err >= 0) { | |
fcc5a03a | 1276 | err = ret; |
30e6c9fa | 1277 | write_seqcount_begin(&devnet_rename_seq); |
fcc5a03a | 1278 | memcpy(dev->name, oldname, IFNAMSIZ); |
5bb025fa | 1279 | memcpy(oldname, newname, IFNAMSIZ); |
238fa362 TG |
1280 | dev->name_assign_type = old_assign_type; |
1281 | old_assign_type = NET_NAME_RENAMED; | |
fcc5a03a | 1282 | goto rollback; |
91e9c07b | 1283 | } else { |
7b6cd1ce | 1284 | pr_err("%s: name change rollback failed: %d\n", |
91e9c07b | 1285 | dev->name, ret); |
fcc5a03a HX |
1286 | } |
1287 | } | |
1da177e4 LT |
1288 | |
1289 | return err; | |
1290 | } | |
1291 | ||
0b815a1a SH |
1292 | /** |
1293 | * dev_set_alias - change ifalias of a device | |
1294 | * @dev: device | |
1295 | * @alias: name up to IFALIASZ | |
f0db275a | 1296 | * @len: limit of bytes to copy from info |
0b815a1a SH |
1297 | * |
1298 | * Set ifalias for a device, | |
1299 | */ | |
1300 | int dev_set_alias(struct net_device *dev, const char *alias, size_t len) | |
1301 | { | |
6c557001 | 1302 | struct dev_ifalias *new_alias = NULL; |
0b815a1a SH |
1303 | |
1304 | if (len >= IFALIASZ) | |
1305 | return -EINVAL; | |
1306 | ||
6c557001 FW |
1307 | if (len) { |
1308 | new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL); | |
1309 | if (!new_alias) | |
1310 | return -ENOMEM; | |
1311 | ||
1312 | memcpy(new_alias->ifalias, alias, len); | |
1313 | new_alias->ifalias[len] = 0; | |
96ca4a2c OH |
1314 | } |
1315 | ||
6c557001 | 1316 | mutex_lock(&ifalias_mutex); |
e3f0d761 PM |
1317 | new_alias = rcu_replace_pointer(dev->ifalias, new_alias, |
1318 | mutex_is_locked(&ifalias_mutex)); | |
6c557001 FW |
1319 | mutex_unlock(&ifalias_mutex); |
1320 | ||
1321 | if (new_alias) | |
1322 | kfree_rcu(new_alias, rcuhead); | |
0b815a1a | 1323 | |
0b815a1a SH |
1324 | return len; |
1325 | } | |
0fe554a4 | 1326 | EXPORT_SYMBOL(dev_set_alias); |
0b815a1a | 1327 | |
6c557001 FW |
1328 | /** |
1329 | * dev_get_alias - get ifalias of a device | |
1330 | * @dev: device | |
20e88320 | 1331 | * @name: buffer to store name of ifalias |
6c557001 FW |
1332 | * @len: size of buffer |
1333 | * | |
1334 | * get ifalias for a device. Caller must make sure dev cannot go | |
1335 | * away, e.g. rcu read lock or own a reference count to device. | |
1336 | */ | |
1337 | int dev_get_alias(const struct net_device *dev, char *name, size_t len) | |
1338 | { | |
1339 | const struct dev_ifalias *alias; | |
1340 | int ret = 0; | |
1341 | ||
1342 | rcu_read_lock(); | |
1343 | alias = rcu_dereference(dev->ifalias); | |
1344 | if (alias) | |
1345 | ret = snprintf(name, len, "%s", alias->ifalias); | |
1346 | rcu_read_unlock(); | |
1347 | ||
1348 | return ret; | |
1349 | } | |
0b815a1a | 1350 | |
d8a33ac4 | 1351 | /** |
3041a069 | 1352 | * netdev_features_change - device changes features |
d8a33ac4 SH |
1353 | * @dev: device to cause notification |
1354 | * | |
1355 | * Called to indicate a device has changed features. | |
1356 | */ | |
1357 | void netdev_features_change(struct net_device *dev) | |
1358 | { | |
056925ab | 1359 | call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev); |
d8a33ac4 SH |
1360 | } |
1361 | EXPORT_SYMBOL(netdev_features_change); | |
1362 | ||
1da177e4 LT |
1363 | /** |
1364 | * netdev_state_change - device changes state | |
1365 | * @dev: device to cause notification | |
1366 | * | |
1367 | * Called to indicate a device has changed state. This function calls | |
1368 | * the notifier chains for netdev_chain and sends a NEWLINK message | |
1369 | * to the routing socket. | |
1370 | */ | |
1371 | void netdev_state_change(struct net_device *dev) | |
1372 | { | |
1373 | if (dev->flags & IFF_UP) { | |
51d0c047 DA |
1374 | struct netdev_notifier_change_info change_info = { |
1375 | .info.dev = dev, | |
1376 | }; | |
54951194 | 1377 | |
51d0c047 | 1378 | call_netdevice_notifiers_info(NETDEV_CHANGE, |
54951194 | 1379 | &change_info.info); |
7f294054 | 1380 | rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL); |
1da177e4 LT |
1381 | } |
1382 | } | |
d1b19dff | 1383 | EXPORT_SYMBOL(netdev_state_change); |
1da177e4 | 1384 | |
ee89bab1 | 1385 | /** |
722c9a0c | 1386 | * netdev_notify_peers - notify network peers about existence of @dev |
1387 | * @dev: network device | |
ee89bab1 AW |
1388 | * |
1389 | * Generate traffic such that interested network peers are aware of | |
1390 | * @dev, such as by generating a gratuitous ARP. This may be used when | |
1391 | * a device wants to inform the rest of the network about some sort of | |
1392 | * reconfiguration such as a failover event or virtual machine | |
1393 | * migration. | |
1394 | */ | |
1395 | void netdev_notify_peers(struct net_device *dev) | |
c1da4ac7 | 1396 | { |
ee89bab1 AW |
1397 | rtnl_lock(); |
1398 | call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev); | |
37c343b4 | 1399 | call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev); |
ee89bab1 | 1400 | rtnl_unlock(); |
c1da4ac7 | 1401 | } |
ee89bab1 | 1402 | EXPORT_SYMBOL(netdev_notify_peers); |
c1da4ac7 | 1403 | |
40c900aa | 1404 | static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack) |
1da177e4 | 1405 | { |
d314774c | 1406 | const struct net_device_ops *ops = dev->netdev_ops; |
3b8bcfd5 | 1407 | int ret; |
1da177e4 | 1408 | |
e46b66bc BH |
1409 | ASSERT_RTNL(); |
1410 | ||
1da177e4 LT |
1411 | if (!netif_device_present(dev)) |
1412 | return -ENODEV; | |
1413 | ||
ca99ca14 NH |
1414 | /* Block netpoll from trying to do any rx path servicing. |
1415 | * If we don't do this there is a chance ndo_poll_controller | |
1416 | * or ndo_poll may be running while we open the device | |
1417 | */ | |
66b5552f | 1418 | netpoll_poll_disable(dev); |
ca99ca14 | 1419 | |
40c900aa | 1420 | ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack); |
3b8bcfd5 JB |
1421 | ret = notifier_to_errno(ret); |
1422 | if (ret) | |
1423 | return ret; | |
1424 | ||
1da177e4 | 1425 | set_bit(__LINK_STATE_START, &dev->state); |
bada339b | 1426 | |
d314774c SH |
1427 | if (ops->ndo_validate_addr) |
1428 | ret = ops->ndo_validate_addr(dev); | |
bada339b | 1429 | |
d314774c SH |
1430 | if (!ret && ops->ndo_open) |
1431 | ret = ops->ndo_open(dev); | |
1da177e4 | 1432 | |
66b5552f | 1433 | netpoll_poll_enable(dev); |
ca99ca14 | 1434 | |
bada339b JG |
1435 | if (ret) |
1436 | clear_bit(__LINK_STATE_START, &dev->state); | |
1437 | else { | |
1da177e4 | 1438 | dev->flags |= IFF_UP; |
4417da66 | 1439 | dev_set_rx_mode(dev); |
1da177e4 | 1440 | dev_activate(dev); |
7bf23575 | 1441 | add_device_randomness(dev->dev_addr, dev->addr_len); |
1da177e4 | 1442 | } |
bada339b | 1443 | |
1da177e4 LT |
1444 | return ret; |
1445 | } | |
1446 | ||
1447 | /** | |
bd380811 | 1448 | * dev_open - prepare an interface for use. |
00f54e68 PM |
1449 | * @dev: device to open |
1450 | * @extack: netlink extended ack | |
1da177e4 | 1451 | * |
bd380811 PM |
1452 | * Takes a device from down to up state. The device's private open |
1453 | * function is invoked and then the multicast lists are loaded. Finally | |
1454 | * the device is moved into the up state and a %NETDEV_UP message is | |
1455 | * sent to the netdev notifier chain. | |
1456 | * | |
1457 | * Calling this function on an active interface is a nop. On a failure | |
1458 | * a negative errno code is returned. | |
1da177e4 | 1459 | */ |
00f54e68 | 1460 | int dev_open(struct net_device *dev, struct netlink_ext_ack *extack) |
bd380811 PM |
1461 | { |
1462 | int ret; | |
1463 | ||
bd380811 PM |
1464 | if (dev->flags & IFF_UP) |
1465 | return 0; | |
1466 | ||
40c900aa | 1467 | ret = __dev_open(dev, extack); |
bd380811 PM |
1468 | if (ret < 0) |
1469 | return ret; | |
1470 | ||
7f294054 | 1471 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL); |
bd380811 PM |
1472 | call_netdevice_notifiers(NETDEV_UP, dev); |
1473 | ||
1474 | return ret; | |
1475 | } | |
1476 | EXPORT_SYMBOL(dev_open); | |
1477 | ||
7051b88a | 1478 | static void __dev_close_many(struct list_head *head) |
1da177e4 | 1479 | { |
44345724 | 1480 | struct net_device *dev; |
e46b66bc | 1481 | |
bd380811 | 1482 | ASSERT_RTNL(); |
9d5010db DM |
1483 | might_sleep(); |
1484 | ||
5cde2829 | 1485 | list_for_each_entry(dev, head, close_list) { |
3f4df206 | 1486 | /* Temporarily disable netpoll until the interface is down */ |
66b5552f | 1487 | netpoll_poll_disable(dev); |
3f4df206 | 1488 | |
44345724 | 1489 | call_netdevice_notifiers(NETDEV_GOING_DOWN, dev); |
1da177e4 | 1490 | |
44345724 | 1491 | clear_bit(__LINK_STATE_START, &dev->state); |
1da177e4 | 1492 | |
44345724 OP |
1493 | /* Synchronize to scheduled poll. We cannot touch poll list, it |
1494 | * can be even on different cpu. So just clear netif_running(). | |
1495 | * | |
1496 | * dev->stop() will invoke napi_disable() on all of it's | |
1497 | * napi_struct instances on this device. | |
1498 | */ | |
4e857c58 | 1499 | smp_mb__after_atomic(); /* Commit netif_running(). */ |
44345724 | 1500 | } |
1da177e4 | 1501 | |
44345724 | 1502 | dev_deactivate_many(head); |
d8b2a4d2 | 1503 | |
5cde2829 | 1504 | list_for_each_entry(dev, head, close_list) { |
44345724 | 1505 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 | 1506 | |
44345724 OP |
1507 | /* |
1508 | * Call the device specific close. This cannot fail. | |
1509 | * Only if device is UP | |
1510 | * | |
1511 | * We allow it to be called even after a DETACH hot-plug | |
1512 | * event. | |
1513 | */ | |
1514 | if (ops->ndo_stop) | |
1515 | ops->ndo_stop(dev); | |
1516 | ||
44345724 | 1517 | dev->flags &= ~IFF_UP; |
66b5552f | 1518 | netpoll_poll_enable(dev); |
44345724 | 1519 | } |
44345724 OP |
1520 | } |
1521 | ||
7051b88a | 1522 | static void __dev_close(struct net_device *dev) |
44345724 OP |
1523 | { |
1524 | LIST_HEAD(single); | |
1525 | ||
5cde2829 | 1526 | list_add(&dev->close_list, &single); |
7051b88a | 1527 | __dev_close_many(&single); |
f87e6f47 | 1528 | list_del(&single); |
44345724 OP |
1529 | } |
1530 | ||
7051b88a | 1531 | void dev_close_many(struct list_head *head, bool unlink) |
44345724 OP |
1532 | { |
1533 | struct net_device *dev, *tmp; | |
1da177e4 | 1534 | |
5cde2829 EB |
1535 | /* Remove the devices that don't need to be closed */ |
1536 | list_for_each_entry_safe(dev, tmp, head, close_list) | |
44345724 | 1537 | if (!(dev->flags & IFF_UP)) |
5cde2829 | 1538 | list_del_init(&dev->close_list); |
44345724 OP |
1539 | |
1540 | __dev_close_many(head); | |
1da177e4 | 1541 | |
5cde2829 | 1542 | list_for_each_entry_safe(dev, tmp, head, close_list) { |
7f294054 | 1543 | rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL); |
44345724 | 1544 | call_netdevice_notifiers(NETDEV_DOWN, dev); |
99c4a26a DM |
1545 | if (unlink) |
1546 | list_del_init(&dev->close_list); | |
44345724 | 1547 | } |
bd380811 | 1548 | } |
99c4a26a | 1549 | EXPORT_SYMBOL(dev_close_many); |
bd380811 PM |
1550 | |
1551 | /** | |
1552 | * dev_close - shutdown an interface. | |
1553 | * @dev: device to shutdown | |
1554 | * | |
1555 | * This function moves an active device into down state. A | |
1556 | * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device | |
1557 | * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier | |
1558 | * chain. | |
1559 | */ | |
7051b88a | 1560 | void dev_close(struct net_device *dev) |
bd380811 | 1561 | { |
e14a5993 ED |
1562 | if (dev->flags & IFF_UP) { |
1563 | LIST_HEAD(single); | |
1da177e4 | 1564 | |
5cde2829 | 1565 | list_add(&dev->close_list, &single); |
99c4a26a | 1566 | dev_close_many(&single, true); |
e14a5993 ED |
1567 | list_del(&single); |
1568 | } | |
1da177e4 | 1569 | } |
d1b19dff | 1570 | EXPORT_SYMBOL(dev_close); |
1da177e4 LT |
1571 | |
1572 | ||
0187bdfb BH |
1573 | /** |
1574 | * dev_disable_lro - disable Large Receive Offload on a device | |
1575 | * @dev: device | |
1576 | * | |
1577 | * Disable Large Receive Offload (LRO) on a net device. Must be | |
1578 | * called under RTNL. This is needed if received packets may be | |
1579 | * forwarded to another interface. | |
1580 | */ | |
1581 | void dev_disable_lro(struct net_device *dev) | |
1582 | { | |
fbe168ba MK |
1583 | struct net_device *lower_dev; |
1584 | struct list_head *iter; | |
529d0489 | 1585 | |
bc5787c6 MM |
1586 | dev->wanted_features &= ~NETIF_F_LRO; |
1587 | netdev_update_features(dev); | |
27660515 | 1588 | |
22d5969f MM |
1589 | if (unlikely(dev->features & NETIF_F_LRO)) |
1590 | netdev_WARN(dev, "failed to disable LRO!\n"); | |
fbe168ba MK |
1591 | |
1592 | netdev_for_each_lower_dev(dev, lower_dev, iter) | |
1593 | dev_disable_lro(lower_dev); | |
0187bdfb BH |
1594 | } |
1595 | EXPORT_SYMBOL(dev_disable_lro); | |
1596 | ||
56f5aa77 MC |
1597 | /** |
1598 | * dev_disable_gro_hw - disable HW Generic Receive Offload on a device | |
1599 | * @dev: device | |
1600 | * | |
1601 | * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be | |
1602 | * called under RTNL. This is needed if Generic XDP is installed on | |
1603 | * the device. | |
1604 | */ | |
1605 | static void dev_disable_gro_hw(struct net_device *dev) | |
1606 | { | |
1607 | dev->wanted_features &= ~NETIF_F_GRO_HW; | |
1608 | netdev_update_features(dev); | |
1609 | ||
1610 | if (unlikely(dev->features & NETIF_F_GRO_HW)) | |
1611 | netdev_WARN(dev, "failed to disable GRO_HW!\n"); | |
1612 | } | |
1613 | ||
ede2762d KT |
1614 | const char *netdev_cmd_to_name(enum netdev_cmd cmd) |
1615 | { | |
1616 | #define N(val) \ | |
1617 | case NETDEV_##val: \ | |
1618 | return "NETDEV_" __stringify(val); | |
1619 | switch (cmd) { | |
1620 | N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER) | |
1621 | N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE) | |
1622 | N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE) | |
1623 | N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER) | |
1624 | N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO) | |
1625 | N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO) | |
1626 | N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN) | |
9daae9bd GP |
1627 | N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO) |
1628 | N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO) | |
1570415f | 1629 | N(PRE_CHANGEADDR) |
3f5ecd8a | 1630 | } |
ede2762d KT |
1631 | #undef N |
1632 | return "UNKNOWN_NETDEV_EVENT"; | |
1633 | } | |
1634 | EXPORT_SYMBOL_GPL(netdev_cmd_to_name); | |
1635 | ||
351638e7 JP |
1636 | static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val, |
1637 | struct net_device *dev) | |
1638 | { | |
51d0c047 DA |
1639 | struct netdev_notifier_info info = { |
1640 | .dev = dev, | |
1641 | }; | |
351638e7 | 1642 | |
351638e7 JP |
1643 | return nb->notifier_call(nb, val, &info); |
1644 | } | |
0187bdfb | 1645 | |
afa0df59 JP |
1646 | static int call_netdevice_register_notifiers(struct notifier_block *nb, |
1647 | struct net_device *dev) | |
1648 | { | |
1649 | int err; | |
1650 | ||
1651 | err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev); | |
1652 | err = notifier_to_errno(err); | |
1653 | if (err) | |
1654 | return err; | |
1655 | ||
1656 | if (!(dev->flags & IFF_UP)) | |
1657 | return 0; | |
1658 | ||
1659 | call_netdevice_notifier(nb, NETDEV_UP, dev); | |
1660 | return 0; | |
1661 | } | |
1662 | ||
1663 | static void call_netdevice_unregister_notifiers(struct notifier_block *nb, | |
1664 | struct net_device *dev) | |
1665 | { | |
1666 | if (dev->flags & IFF_UP) { | |
1667 | call_netdevice_notifier(nb, NETDEV_GOING_DOWN, | |
1668 | dev); | |
1669 | call_netdevice_notifier(nb, NETDEV_DOWN, dev); | |
1670 | } | |
1671 | call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev); | |
1672 | } | |
1673 | ||
1674 | static int call_netdevice_register_net_notifiers(struct notifier_block *nb, | |
1675 | struct net *net) | |
1676 | { | |
1677 | struct net_device *dev; | |
1678 | int err; | |
1679 | ||
1680 | for_each_netdev(net, dev) { | |
1681 | err = call_netdevice_register_notifiers(nb, dev); | |
1682 | if (err) | |
1683 | goto rollback; | |
1684 | } | |
1685 | return 0; | |
1686 | ||
1687 | rollback: | |
1688 | for_each_netdev_continue_reverse(net, dev) | |
1689 | call_netdevice_unregister_notifiers(nb, dev); | |
1690 | return err; | |
1691 | } | |
1692 | ||
1693 | static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb, | |
1694 | struct net *net) | |
1695 | { | |
1696 | struct net_device *dev; | |
1697 | ||
1698 | for_each_netdev(net, dev) | |
1699 | call_netdevice_unregister_notifiers(nb, dev); | |
1700 | } | |
1701 | ||
881d966b EB |
1702 | static int dev_boot_phase = 1; |
1703 | ||
1da177e4 | 1704 | /** |
722c9a0c | 1705 | * register_netdevice_notifier - register a network notifier block |
1706 | * @nb: notifier | |
1da177e4 | 1707 | * |
722c9a0c | 1708 | * Register a notifier to be called when network device events occur. |
1709 | * The notifier passed is linked into the kernel structures and must | |
1710 | * not be reused until it has been unregistered. A negative errno code | |
1711 | * is returned on a failure. | |
1da177e4 | 1712 | * |
722c9a0c | 1713 | * When registered all registration and up events are replayed |
1714 | * to the new notifier to allow device to have a race free | |
1715 | * view of the network device list. | |
1da177e4 LT |
1716 | */ |
1717 | ||
1718 | int register_netdevice_notifier(struct notifier_block *nb) | |
1719 | { | |
881d966b | 1720 | struct net *net; |
1da177e4 LT |
1721 | int err; |
1722 | ||
328fbe74 KT |
1723 | /* Close race with setup_net() and cleanup_net() */ |
1724 | down_write(&pernet_ops_rwsem); | |
1da177e4 | 1725 | rtnl_lock(); |
f07d5b94 | 1726 | err = raw_notifier_chain_register(&netdev_chain, nb); |
fcc5a03a HX |
1727 | if (err) |
1728 | goto unlock; | |
881d966b EB |
1729 | if (dev_boot_phase) |
1730 | goto unlock; | |
1731 | for_each_net(net) { | |
afa0df59 JP |
1732 | err = call_netdevice_register_net_notifiers(nb, net); |
1733 | if (err) | |
1734 | goto rollback; | |
1da177e4 | 1735 | } |
fcc5a03a HX |
1736 | |
1737 | unlock: | |
1da177e4 | 1738 | rtnl_unlock(); |
328fbe74 | 1739 | up_write(&pernet_ops_rwsem); |
1da177e4 | 1740 | return err; |
fcc5a03a HX |
1741 | |
1742 | rollback: | |
afa0df59 JP |
1743 | for_each_net_continue_reverse(net) |
1744 | call_netdevice_unregister_net_notifiers(nb, net); | |
c67625a1 PE |
1745 | |
1746 | raw_notifier_chain_unregister(&netdev_chain, nb); | |
fcc5a03a | 1747 | goto unlock; |
1da177e4 | 1748 | } |
d1b19dff | 1749 | EXPORT_SYMBOL(register_netdevice_notifier); |
1da177e4 LT |
1750 | |
1751 | /** | |
722c9a0c | 1752 | * unregister_netdevice_notifier - unregister a network notifier block |
1753 | * @nb: notifier | |
1da177e4 | 1754 | * |
722c9a0c | 1755 | * Unregister a notifier previously registered by |
1756 | * register_netdevice_notifier(). The notifier is unlinked into the | |
1757 | * kernel structures and may then be reused. A negative errno code | |
1758 | * is returned on a failure. | |
7d3d43da | 1759 | * |
722c9a0c | 1760 | * After unregistering unregister and down device events are synthesized |
1761 | * for all devices on the device list to the removed notifier to remove | |
1762 | * the need for special case cleanup code. | |
1da177e4 LT |
1763 | */ |
1764 | ||
1765 | int unregister_netdevice_notifier(struct notifier_block *nb) | |
1766 | { | |
7d3d43da | 1767 | struct net *net; |
9f514950 HX |
1768 | int err; |
1769 | ||
328fbe74 KT |
1770 | /* Close race with setup_net() and cleanup_net() */ |
1771 | down_write(&pernet_ops_rwsem); | |
9f514950 | 1772 | rtnl_lock(); |
f07d5b94 | 1773 | err = raw_notifier_chain_unregister(&netdev_chain, nb); |
7d3d43da EB |
1774 | if (err) |
1775 | goto unlock; | |
1776 | ||
48b3a137 JP |
1777 | for_each_net(net) |
1778 | call_netdevice_unregister_net_notifiers(nb, net); | |
1779 | ||
7d3d43da | 1780 | unlock: |
9f514950 | 1781 | rtnl_unlock(); |
328fbe74 | 1782 | up_write(&pernet_ops_rwsem); |
9f514950 | 1783 | return err; |
1da177e4 | 1784 | } |
d1b19dff | 1785 | EXPORT_SYMBOL(unregister_netdevice_notifier); |
1da177e4 | 1786 | |
1f637703 JP |
1787 | static int __register_netdevice_notifier_net(struct net *net, |
1788 | struct notifier_block *nb, | |
1789 | bool ignore_call_fail) | |
1790 | { | |
1791 | int err; | |
1792 | ||
1793 | err = raw_notifier_chain_register(&net->netdev_chain, nb); | |
1794 | if (err) | |
1795 | return err; | |
1796 | if (dev_boot_phase) | |
1797 | return 0; | |
1798 | ||
1799 | err = call_netdevice_register_net_notifiers(nb, net); | |
1800 | if (err && !ignore_call_fail) | |
1801 | goto chain_unregister; | |
1802 | ||
1803 | return 0; | |
1804 | ||
1805 | chain_unregister: | |
1806 | raw_notifier_chain_unregister(&net->netdev_chain, nb); | |
1807 | return err; | |
1808 | } | |
1809 | ||
1810 | static int __unregister_netdevice_notifier_net(struct net *net, | |
1811 | struct notifier_block *nb) | |
1812 | { | |
1813 | int err; | |
1814 | ||
1815 | err = raw_notifier_chain_unregister(&net->netdev_chain, nb); | |
1816 | if (err) | |
1817 | return err; | |
1818 | ||
1819 | call_netdevice_unregister_net_notifiers(nb, net); | |
1820 | return 0; | |
1821 | } | |
1822 | ||
a30c7b42 JP |
1823 | /** |
1824 | * register_netdevice_notifier_net - register a per-netns network notifier block | |
1825 | * @net: network namespace | |
1826 | * @nb: notifier | |
1827 | * | |
1828 | * Register a notifier to be called when network device events occur. | |
1829 | * The notifier passed is linked into the kernel structures and must | |
1830 | * not be reused until it has been unregistered. A negative errno code | |
1831 | * is returned on a failure. | |
1832 | * | |
1833 | * When registered all registration and up events are replayed | |
1834 | * to the new notifier to allow device to have a race free | |
1835 | * view of the network device list. | |
1836 | */ | |
1837 | ||
1838 | int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb) | |
1839 | { | |
1840 | int err; | |
1841 | ||
1842 | rtnl_lock(); | |
1f637703 | 1843 | err = __register_netdevice_notifier_net(net, nb, false); |
a30c7b42 JP |
1844 | rtnl_unlock(); |
1845 | return err; | |
a30c7b42 JP |
1846 | } |
1847 | EXPORT_SYMBOL(register_netdevice_notifier_net); | |
1848 | ||
1849 | /** | |
1850 | * unregister_netdevice_notifier_net - unregister a per-netns | |
1851 | * network notifier block | |
1852 | * @net: network namespace | |
1853 | * @nb: notifier | |
1854 | * | |
1855 | * Unregister a notifier previously registered by | |
1856 | * register_netdevice_notifier(). The notifier is unlinked into the | |
1857 | * kernel structures and may then be reused. A negative errno code | |
1858 | * is returned on a failure. | |
1859 | * | |
1860 | * After unregistering unregister and down device events are synthesized | |
1861 | * for all devices on the device list to the removed notifier to remove | |
1862 | * the need for special case cleanup code. | |
1863 | */ | |
1864 | ||
1865 | int unregister_netdevice_notifier_net(struct net *net, | |
1866 | struct notifier_block *nb) | |
1867 | { | |
1868 | int err; | |
1869 | ||
1870 | rtnl_lock(); | |
1f637703 | 1871 | err = __unregister_netdevice_notifier_net(net, nb); |
a30c7b42 JP |
1872 | rtnl_unlock(); |
1873 | return err; | |
1874 | } | |
1875 | EXPORT_SYMBOL(unregister_netdevice_notifier_net); | |
a30c7b42 | 1876 | |
93642e14 JP |
1877 | int register_netdevice_notifier_dev_net(struct net_device *dev, |
1878 | struct notifier_block *nb, | |
1879 | struct netdev_net_notifier *nn) | |
1880 | { | |
1881 | int err; | |
a30c7b42 | 1882 | |
93642e14 JP |
1883 | rtnl_lock(); |
1884 | err = __register_netdevice_notifier_net(dev_net(dev), nb, false); | |
1885 | if (!err) { | |
1886 | nn->nb = nb; | |
1887 | list_add(&nn->list, &dev->net_notifier_list); | |
1888 | } | |
a30c7b42 JP |
1889 | rtnl_unlock(); |
1890 | return err; | |
1891 | } | |
93642e14 JP |
1892 | EXPORT_SYMBOL(register_netdevice_notifier_dev_net); |
1893 | ||
1894 | int unregister_netdevice_notifier_dev_net(struct net_device *dev, | |
1895 | struct notifier_block *nb, | |
1896 | struct netdev_net_notifier *nn) | |
1897 | { | |
1898 | int err; | |
1899 | ||
1900 | rtnl_lock(); | |
1901 | list_del(&nn->list); | |
1902 | err = __unregister_netdevice_notifier_net(dev_net(dev), nb); | |
1903 | rtnl_unlock(); | |
1904 | return err; | |
1905 | } | |
1906 | EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net); | |
1907 | ||
1908 | static void move_netdevice_notifiers_dev_net(struct net_device *dev, | |
1909 | struct net *net) | |
1910 | { | |
1911 | struct netdev_net_notifier *nn; | |
1912 | ||
1913 | list_for_each_entry(nn, &dev->net_notifier_list, list) { | |
1914 | __unregister_netdevice_notifier_net(dev_net(dev), nn->nb); | |
1915 | __register_netdevice_notifier_net(net, nn->nb, true); | |
1916 | } | |
1917 | } | |
a30c7b42 | 1918 | |
351638e7 JP |
1919 | /** |
1920 | * call_netdevice_notifiers_info - call all network notifier blocks | |
1921 | * @val: value passed unmodified to notifier function | |
351638e7 JP |
1922 | * @info: notifier information data |
1923 | * | |
1924 | * Call all network notifier blocks. Parameters and return value | |
1925 | * are as for raw_notifier_call_chain(). | |
1926 | */ | |
1927 | ||
1d143d9f | 1928 | static int call_netdevice_notifiers_info(unsigned long val, |
1d143d9f | 1929 | struct netdev_notifier_info *info) |
351638e7 | 1930 | { |
a30c7b42 JP |
1931 | struct net *net = dev_net(info->dev); |
1932 | int ret; | |
1933 | ||
351638e7 | 1934 | ASSERT_RTNL(); |
a30c7b42 JP |
1935 | |
1936 | /* Run per-netns notifier block chain first, then run the global one. | |
1937 | * Hopefully, one day, the global one is going to be removed after | |
1938 | * all notifier block registrators get converted to be per-netns. | |
1939 | */ | |
1940 | ret = raw_notifier_call_chain(&net->netdev_chain, val, info); | |
1941 | if (ret & NOTIFY_STOP_MASK) | |
1942 | return ret; | |
351638e7 JP |
1943 | return raw_notifier_call_chain(&netdev_chain, val, info); |
1944 | } | |
351638e7 | 1945 | |
26372605 PM |
1946 | static int call_netdevice_notifiers_extack(unsigned long val, |
1947 | struct net_device *dev, | |
1948 | struct netlink_ext_ack *extack) | |
1949 | { | |
1950 | struct netdev_notifier_info info = { | |
1951 | .dev = dev, | |
1952 | .extack = extack, | |
1953 | }; | |
1954 | ||
1955 | return call_netdevice_notifiers_info(val, &info); | |
1956 | } | |
1957 | ||
1da177e4 LT |
1958 | /** |
1959 | * call_netdevice_notifiers - call all network notifier blocks | |
1960 | * @val: value passed unmodified to notifier function | |
c4ea43c5 | 1961 | * @dev: net_device pointer passed unmodified to notifier function |
1da177e4 LT |
1962 | * |
1963 | * Call all network notifier blocks. Parameters and return value | |
f07d5b94 | 1964 | * are as for raw_notifier_call_chain(). |
1da177e4 LT |
1965 | */ |
1966 | ||
ad7379d4 | 1967 | int call_netdevice_notifiers(unsigned long val, struct net_device *dev) |
1da177e4 | 1968 | { |
26372605 | 1969 | return call_netdevice_notifiers_extack(val, dev, NULL); |
1da177e4 | 1970 | } |
edf947f1 | 1971 | EXPORT_SYMBOL(call_netdevice_notifiers); |
1da177e4 | 1972 | |
af7d6cce SD |
1973 | /** |
1974 | * call_netdevice_notifiers_mtu - call all network notifier blocks | |
1975 | * @val: value passed unmodified to notifier function | |
1976 | * @dev: net_device pointer passed unmodified to notifier function | |
1977 | * @arg: additional u32 argument passed to the notifier function | |
1978 | * | |
1979 | * Call all network notifier blocks. Parameters and return value | |
1980 | * are as for raw_notifier_call_chain(). | |
1981 | */ | |
1982 | static int call_netdevice_notifiers_mtu(unsigned long val, | |
1983 | struct net_device *dev, u32 arg) | |
1984 | { | |
1985 | struct netdev_notifier_info_ext info = { | |
1986 | .info.dev = dev, | |
1987 | .ext.mtu = arg, | |
1988 | }; | |
1989 | ||
1990 | BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0); | |
1991 | ||
1992 | return call_netdevice_notifiers_info(val, &info.info); | |
1993 | } | |
1994 | ||
1cf51900 | 1995 | #ifdef CONFIG_NET_INGRESS |
aabf6772 | 1996 | static DEFINE_STATIC_KEY_FALSE(ingress_needed_key); |
4577139b DB |
1997 | |
1998 | void net_inc_ingress_queue(void) | |
1999 | { | |
aabf6772 | 2000 | static_branch_inc(&ingress_needed_key); |
4577139b DB |
2001 | } |
2002 | EXPORT_SYMBOL_GPL(net_inc_ingress_queue); | |
2003 | ||
2004 | void net_dec_ingress_queue(void) | |
2005 | { | |
aabf6772 | 2006 | static_branch_dec(&ingress_needed_key); |
4577139b DB |
2007 | } |
2008 | EXPORT_SYMBOL_GPL(net_dec_ingress_queue); | |
2009 | #endif | |
2010 | ||
1f211a1b | 2011 | #ifdef CONFIG_NET_EGRESS |
aabf6772 | 2012 | static DEFINE_STATIC_KEY_FALSE(egress_needed_key); |
1f211a1b DB |
2013 | |
2014 | void net_inc_egress_queue(void) | |
2015 | { | |
aabf6772 | 2016 | static_branch_inc(&egress_needed_key); |
1f211a1b DB |
2017 | } |
2018 | EXPORT_SYMBOL_GPL(net_inc_egress_queue); | |
2019 | ||
2020 | void net_dec_egress_queue(void) | |
2021 | { | |
aabf6772 | 2022 | static_branch_dec(&egress_needed_key); |
1f211a1b DB |
2023 | } |
2024 | EXPORT_SYMBOL_GPL(net_dec_egress_queue); | |
2025 | #endif | |
2026 | ||
39e83922 | 2027 | static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key); |
e9666d10 | 2028 | #ifdef CONFIG_JUMP_LABEL |
b90e5794 | 2029 | static atomic_t netstamp_needed_deferred; |
13baa00a | 2030 | static atomic_t netstamp_wanted; |
5fa8bbda | 2031 | static void netstamp_clear(struct work_struct *work) |
1da177e4 | 2032 | { |
b90e5794 | 2033 | int deferred = atomic_xchg(&netstamp_needed_deferred, 0); |
13baa00a | 2034 | int wanted; |
b90e5794 | 2035 | |
13baa00a ED |
2036 | wanted = atomic_add_return(deferred, &netstamp_wanted); |
2037 | if (wanted > 0) | |
39e83922 | 2038 | static_branch_enable(&netstamp_needed_key); |
13baa00a | 2039 | else |
39e83922 | 2040 | static_branch_disable(&netstamp_needed_key); |
5fa8bbda ED |
2041 | } |
2042 | static DECLARE_WORK(netstamp_work, netstamp_clear); | |
b90e5794 | 2043 | #endif |
5fa8bbda ED |
2044 | |
2045 | void net_enable_timestamp(void) | |
2046 | { | |
e9666d10 | 2047 | #ifdef CONFIG_JUMP_LABEL |
13baa00a ED |
2048 | int wanted; |
2049 | ||
2050 | while (1) { | |
2051 | wanted = atomic_read(&netstamp_wanted); | |
2052 | if (wanted <= 0) | |
2053 | break; | |
2054 | if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted) | |
2055 | return; | |
2056 | } | |
2057 | atomic_inc(&netstamp_needed_deferred); | |
2058 | schedule_work(&netstamp_work); | |
2059 | #else | |
39e83922 | 2060 | static_branch_inc(&netstamp_needed_key); |
13baa00a | 2061 | #endif |
1da177e4 | 2062 | } |
d1b19dff | 2063 | EXPORT_SYMBOL(net_enable_timestamp); |
1da177e4 LT |
2064 | |
2065 | void net_disable_timestamp(void) | |
2066 | { | |
e9666d10 | 2067 | #ifdef CONFIG_JUMP_LABEL |
13baa00a ED |
2068 | int wanted; |
2069 | ||
2070 | while (1) { | |
2071 | wanted = atomic_read(&netstamp_wanted); | |
2072 | if (wanted <= 1) | |
2073 | break; | |
2074 | if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted) | |
2075 | return; | |
2076 | } | |
2077 | atomic_dec(&netstamp_needed_deferred); | |
5fa8bbda ED |
2078 | schedule_work(&netstamp_work); |
2079 | #else | |
39e83922 | 2080 | static_branch_dec(&netstamp_needed_key); |
5fa8bbda | 2081 | #endif |
1da177e4 | 2082 | } |
d1b19dff | 2083 | EXPORT_SYMBOL(net_disable_timestamp); |
1da177e4 | 2084 | |
3b098e2d | 2085 | static inline void net_timestamp_set(struct sk_buff *skb) |
1da177e4 | 2086 | { |
2456e855 | 2087 | skb->tstamp = 0; |
39e83922 | 2088 | if (static_branch_unlikely(&netstamp_needed_key)) |
a61bbcf2 | 2089 | __net_timestamp(skb); |
1da177e4 LT |
2090 | } |
2091 | ||
39e83922 DB |
2092 | #define net_timestamp_check(COND, SKB) \ |
2093 | if (static_branch_unlikely(&netstamp_needed_key)) { \ | |
2094 | if ((COND) && !(SKB)->tstamp) \ | |
2095 | __net_timestamp(SKB); \ | |
2096 | } \ | |
3b098e2d | 2097 | |
f4b05d27 | 2098 | bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb) |
79b569f0 DL |
2099 | { |
2100 | unsigned int len; | |
2101 | ||
2102 | if (!(dev->flags & IFF_UP)) | |
2103 | return false; | |
2104 | ||
2105 | len = dev->mtu + dev->hard_header_len + VLAN_HLEN; | |
2106 | if (skb->len <= len) | |
2107 | return true; | |
2108 | ||
2109 | /* if TSO is enabled, we don't care about the length as the packet | |
2110 | * could be forwarded without being segmented before | |
2111 | */ | |
2112 | if (skb_is_gso(skb)) | |
2113 | return true; | |
2114 | ||
2115 | return false; | |
2116 | } | |
1ee481fb | 2117 | EXPORT_SYMBOL_GPL(is_skb_forwardable); |
79b569f0 | 2118 | |
a0265d28 HX |
2119 | int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb) |
2120 | { | |
4e3264d2 | 2121 | int ret = ____dev_forward_skb(dev, skb); |
a0265d28 | 2122 | |
4e3264d2 MKL |
2123 | if (likely(!ret)) { |
2124 | skb->protocol = eth_type_trans(skb, dev); | |
2125 | skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN); | |
2126 | } | |
a0265d28 | 2127 | |
4e3264d2 | 2128 | return ret; |
a0265d28 HX |
2129 | } |
2130 | EXPORT_SYMBOL_GPL(__dev_forward_skb); | |
2131 | ||
44540960 AB |
2132 | /** |
2133 | * dev_forward_skb - loopback an skb to another netif | |
2134 | * | |
2135 | * @dev: destination network device | |
2136 | * @skb: buffer to forward | |
2137 | * | |
2138 | * return values: | |
2139 | * NET_RX_SUCCESS (no congestion) | |
6ec82562 | 2140 | * NET_RX_DROP (packet was dropped, but freed) |
44540960 AB |
2141 | * |
2142 | * dev_forward_skb can be used for injecting an skb from the | |
2143 | * start_xmit function of one device into the receive queue | |
2144 | * of another device. | |
2145 | * | |
2146 | * The receiving device may be in another namespace, so | |
2147 | * we have to clear all information in the skb that could | |
2148 | * impact namespace isolation. | |
2149 | */ | |
2150 | int dev_forward_skb(struct net_device *dev, struct sk_buff *skb) | |
2151 | { | |
a0265d28 | 2152 | return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb); |
44540960 AB |
2153 | } |
2154 | EXPORT_SYMBOL_GPL(dev_forward_skb); | |
2155 | ||
71d9dec2 CG |
2156 | static inline int deliver_skb(struct sk_buff *skb, |
2157 | struct packet_type *pt_prev, | |
2158 | struct net_device *orig_dev) | |
2159 | { | |
1f8b977a | 2160 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
1080e512 | 2161 | return -ENOMEM; |
63354797 | 2162 | refcount_inc(&skb->users); |
71d9dec2 CG |
2163 | return pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
2164 | } | |
2165 | ||
7866a621 SN |
2166 | static inline void deliver_ptype_list_skb(struct sk_buff *skb, |
2167 | struct packet_type **pt, | |
fbcb2170 JP |
2168 | struct net_device *orig_dev, |
2169 | __be16 type, | |
7866a621 SN |
2170 | struct list_head *ptype_list) |
2171 | { | |
2172 | struct packet_type *ptype, *pt_prev = *pt; | |
2173 | ||
2174 | list_for_each_entry_rcu(ptype, ptype_list, list) { | |
2175 | if (ptype->type != type) | |
2176 | continue; | |
2177 | if (pt_prev) | |
fbcb2170 | 2178 | deliver_skb(skb, pt_prev, orig_dev); |
7866a621 SN |
2179 | pt_prev = ptype; |
2180 | } | |
2181 | *pt = pt_prev; | |
2182 | } | |
2183 | ||
c0de08d0 EL |
2184 | static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb) |
2185 | { | |
a3d744e9 | 2186 | if (!ptype->af_packet_priv || !skb->sk) |
c0de08d0 EL |
2187 | return false; |
2188 | ||
2189 | if (ptype->id_match) | |
2190 | return ptype->id_match(ptype, skb->sk); | |
2191 | else if ((struct sock *)ptype->af_packet_priv == skb->sk) | |
2192 | return true; | |
2193 | ||
2194 | return false; | |
2195 | } | |
2196 | ||
9f9a742d MR |
2197 | /** |
2198 | * dev_nit_active - return true if any network interface taps are in use | |
2199 | * | |
2200 | * @dev: network device to check for the presence of taps | |
2201 | */ | |
2202 | bool dev_nit_active(struct net_device *dev) | |
2203 | { | |
2204 | return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all); | |
2205 | } | |
2206 | EXPORT_SYMBOL_GPL(dev_nit_active); | |
2207 | ||
1da177e4 LT |
2208 | /* |
2209 | * Support routine. Sends outgoing frames to any network | |
2210 | * taps currently in use. | |
2211 | */ | |
2212 | ||
74b20582 | 2213 | void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev) |
1da177e4 LT |
2214 | { |
2215 | struct packet_type *ptype; | |
71d9dec2 CG |
2216 | struct sk_buff *skb2 = NULL; |
2217 | struct packet_type *pt_prev = NULL; | |
7866a621 | 2218 | struct list_head *ptype_list = &ptype_all; |
a61bbcf2 | 2219 | |
1da177e4 | 2220 | rcu_read_lock(); |
7866a621 SN |
2221 | again: |
2222 | list_for_each_entry_rcu(ptype, ptype_list, list) { | |
fa788d98 VW |
2223 | if (ptype->ignore_outgoing) |
2224 | continue; | |
2225 | ||
1da177e4 LT |
2226 | /* Never send packets back to the socket |
2227 | * they originated from - MvS (miquels@drinkel.ow.org) | |
2228 | */ | |
7866a621 SN |
2229 | if (skb_loop_sk(ptype, skb)) |
2230 | continue; | |
71d9dec2 | 2231 | |
7866a621 SN |
2232 | if (pt_prev) { |
2233 | deliver_skb(skb2, pt_prev, skb->dev); | |
2234 | pt_prev = ptype; | |
2235 | continue; | |
2236 | } | |
1da177e4 | 2237 | |
7866a621 SN |
2238 | /* need to clone skb, done only once */ |
2239 | skb2 = skb_clone(skb, GFP_ATOMIC); | |
2240 | if (!skb2) | |
2241 | goto out_unlock; | |
70978182 | 2242 | |
7866a621 | 2243 | net_timestamp_set(skb2); |
1da177e4 | 2244 | |
7866a621 SN |
2245 | /* skb->nh should be correctly |
2246 | * set by sender, so that the second statement is | |
2247 | * just protection against buggy protocols. | |
2248 | */ | |
2249 | skb_reset_mac_header(skb2); | |
2250 | ||
2251 | if (skb_network_header(skb2) < skb2->data || | |
2252 | skb_network_header(skb2) > skb_tail_pointer(skb2)) { | |
2253 | net_crit_ratelimited("protocol %04x is buggy, dev %s\n", | |
2254 | ntohs(skb2->protocol), | |
2255 | dev->name); | |
2256 | skb_reset_network_header(skb2); | |
1da177e4 | 2257 | } |
7866a621 SN |
2258 | |
2259 | skb2->transport_header = skb2->network_header; | |
2260 | skb2->pkt_type = PACKET_OUTGOING; | |
2261 | pt_prev = ptype; | |
2262 | } | |
2263 | ||
2264 | if (ptype_list == &ptype_all) { | |
2265 | ptype_list = &dev->ptype_all; | |
2266 | goto again; | |
1da177e4 | 2267 | } |
7866a621 | 2268 | out_unlock: |
581fe0ea WB |
2269 | if (pt_prev) { |
2270 | if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC)) | |
2271 | pt_prev->func(skb2, skb->dev, pt_prev, skb->dev); | |
2272 | else | |
2273 | kfree_skb(skb2); | |
2274 | } | |
1da177e4 LT |
2275 | rcu_read_unlock(); |
2276 | } | |
74b20582 | 2277 | EXPORT_SYMBOL_GPL(dev_queue_xmit_nit); |
1da177e4 | 2278 | |
2c53040f BH |
2279 | /** |
2280 | * netif_setup_tc - Handle tc mappings on real_num_tx_queues change | |
4f57c087 JF |
2281 | * @dev: Network device |
2282 | * @txq: number of queues available | |
2283 | * | |
2284 | * If real_num_tx_queues is changed the tc mappings may no longer be | |
2285 | * valid. To resolve this verify the tc mapping remains valid and if | |
2286 | * not NULL the mapping. With no priorities mapping to this | |
2287 | * offset/count pair it will no longer be used. In the worst case TC0 | |
2288 | * is invalid nothing can be done so disable priority mappings. If is | |
2289 | * expected that drivers will fix this mapping if they can before | |
2290 | * calling netif_set_real_num_tx_queues. | |
2291 | */ | |
bb134d22 | 2292 | static void netif_setup_tc(struct net_device *dev, unsigned int txq) |
4f57c087 JF |
2293 | { |
2294 | int i; | |
2295 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; | |
2296 | ||
2297 | /* If TC0 is invalidated disable TC mapping */ | |
2298 | if (tc->offset + tc->count > txq) { | |
7b6cd1ce | 2299 | pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n"); |
4f57c087 JF |
2300 | dev->num_tc = 0; |
2301 | return; | |
2302 | } | |
2303 | ||
2304 | /* Invalidated prio to tc mappings set to TC0 */ | |
2305 | for (i = 1; i < TC_BITMASK + 1; i++) { | |
2306 | int q = netdev_get_prio_tc_map(dev, i); | |
2307 | ||
2308 | tc = &dev->tc_to_txq[q]; | |
2309 | if (tc->offset + tc->count > txq) { | |
7b6cd1ce JP |
2310 | pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n", |
2311 | i, q); | |
4f57c087 JF |
2312 | netdev_set_prio_tc_map(dev, i, 0); |
2313 | } | |
2314 | } | |
2315 | } | |
2316 | ||
8d059b0f AD |
2317 | int netdev_txq_to_tc(struct net_device *dev, unsigned int txq) |
2318 | { | |
2319 | if (dev->num_tc) { | |
2320 | struct netdev_tc_txq *tc = &dev->tc_to_txq[0]; | |
2321 | int i; | |
2322 | ||
ffcfe25b | 2323 | /* walk through the TCs and see if it falls into any of them */ |
8d059b0f AD |
2324 | for (i = 0; i < TC_MAX_QUEUE; i++, tc++) { |
2325 | if ((txq - tc->offset) < tc->count) | |
2326 | return i; | |
2327 | } | |
2328 | ||
ffcfe25b | 2329 | /* didn't find it, just return -1 to indicate no match */ |
8d059b0f AD |
2330 | return -1; |
2331 | } | |
2332 | ||
2333 | return 0; | |
2334 | } | |
8a5f2166 | 2335 | EXPORT_SYMBOL(netdev_txq_to_tc); |
8d059b0f | 2336 | |
537c00de | 2337 | #ifdef CONFIG_XPS |
04157469 AN |
2338 | struct static_key xps_needed __read_mostly; |
2339 | EXPORT_SYMBOL(xps_needed); | |
2340 | struct static_key xps_rxqs_needed __read_mostly; | |
2341 | EXPORT_SYMBOL(xps_rxqs_needed); | |
537c00de AD |
2342 | static DEFINE_MUTEX(xps_map_mutex); |
2343 | #define xmap_dereference(P) \ | |
2344 | rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex)) | |
2345 | ||
6234f874 AD |
2346 | static bool remove_xps_queue(struct xps_dev_maps *dev_maps, |
2347 | int tci, u16 index) | |
537c00de | 2348 | { |
10cdc3f3 AD |
2349 | struct xps_map *map = NULL; |
2350 | int pos; | |
537c00de | 2351 | |
10cdc3f3 | 2352 | if (dev_maps) |
80d19669 | 2353 | map = xmap_dereference(dev_maps->attr_map[tci]); |
6234f874 AD |
2354 | if (!map) |
2355 | return false; | |
537c00de | 2356 | |
6234f874 AD |
2357 | for (pos = map->len; pos--;) { |
2358 | if (map->queues[pos] != index) | |
2359 | continue; | |
2360 | ||
2361 | if (map->len > 1) { | |
2362 | map->queues[pos] = map->queues[--map->len]; | |
10cdc3f3 | 2363 | break; |
537c00de | 2364 | } |
6234f874 | 2365 | |
80d19669 | 2366 | RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL); |
6234f874 AD |
2367 | kfree_rcu(map, rcu); |
2368 | return false; | |
537c00de AD |
2369 | } |
2370 | ||
6234f874 | 2371 | return true; |
10cdc3f3 AD |
2372 | } |
2373 | ||
6234f874 AD |
2374 | static bool remove_xps_queue_cpu(struct net_device *dev, |
2375 | struct xps_dev_maps *dev_maps, | |
2376 | int cpu, u16 offset, u16 count) | |
2377 | { | |
184c449f AD |
2378 | int num_tc = dev->num_tc ? : 1; |
2379 | bool active = false; | |
2380 | int tci; | |
6234f874 | 2381 | |
184c449f AD |
2382 | for (tci = cpu * num_tc; num_tc--; tci++) { |
2383 | int i, j; | |
2384 | ||
2385 | for (i = count, j = offset; i--; j++) { | |
6358d49a | 2386 | if (!remove_xps_queue(dev_maps, tci, j)) |
184c449f AD |
2387 | break; |
2388 | } | |
2389 | ||
2390 | active |= i < 0; | |
6234f874 AD |
2391 | } |
2392 | ||
184c449f | 2393 | return active; |
6234f874 AD |
2394 | } |
2395 | ||
867d0ad4 SD |
2396 | static void reset_xps_maps(struct net_device *dev, |
2397 | struct xps_dev_maps *dev_maps, | |
2398 | bool is_rxqs_map) | |
2399 | { | |
2400 | if (is_rxqs_map) { | |
2401 | static_key_slow_dec_cpuslocked(&xps_rxqs_needed); | |
2402 | RCU_INIT_POINTER(dev->xps_rxqs_map, NULL); | |
2403 | } else { | |
2404 | RCU_INIT_POINTER(dev->xps_cpus_map, NULL); | |
2405 | } | |
2406 | static_key_slow_dec_cpuslocked(&xps_needed); | |
2407 | kfree_rcu(dev_maps, rcu); | |
2408 | } | |
2409 | ||
80d19669 AN |
2410 | static void clean_xps_maps(struct net_device *dev, const unsigned long *mask, |
2411 | struct xps_dev_maps *dev_maps, unsigned int nr_ids, | |
2412 | u16 offset, u16 count, bool is_rxqs_map) | |
2413 | { | |
2414 | bool active = false; | |
2415 | int i, j; | |
2416 | ||
2417 | for (j = -1; j = netif_attrmask_next(j, mask, nr_ids), | |
2418 | j < nr_ids;) | |
2419 | active |= remove_xps_queue_cpu(dev, dev_maps, j, offset, | |
2420 | count); | |
867d0ad4 SD |
2421 | if (!active) |
2422 | reset_xps_maps(dev, dev_maps, is_rxqs_map); | |
80d19669 | 2423 | |
f28c020f SD |
2424 | if (!is_rxqs_map) { |
2425 | for (i = offset + (count - 1); count--; i--) { | |
2426 | netdev_queue_numa_node_write( | |
2427 | netdev_get_tx_queue(dev, i), | |
2428 | NUMA_NO_NODE); | |
80d19669 | 2429 | } |
80d19669 AN |
2430 | } |
2431 | } | |
2432 | ||
6234f874 AD |
2433 | static void netif_reset_xps_queues(struct net_device *dev, u16 offset, |
2434 | u16 count) | |
10cdc3f3 | 2435 | { |
80d19669 | 2436 | const unsigned long *possible_mask = NULL; |
10cdc3f3 | 2437 | struct xps_dev_maps *dev_maps; |
80d19669 | 2438 | unsigned int nr_ids; |
10cdc3f3 | 2439 | |
04157469 AN |
2440 | if (!static_key_false(&xps_needed)) |
2441 | return; | |
10cdc3f3 | 2442 | |
4d99f660 | 2443 | cpus_read_lock(); |
04157469 | 2444 | mutex_lock(&xps_map_mutex); |
10cdc3f3 | 2445 | |
04157469 AN |
2446 | if (static_key_false(&xps_rxqs_needed)) { |
2447 | dev_maps = xmap_dereference(dev->xps_rxqs_map); | |
2448 | if (dev_maps) { | |
2449 | nr_ids = dev->num_rx_queues; | |
2450 | clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, | |
2451 | offset, count, true); | |
2452 | } | |
537c00de AD |
2453 | } |
2454 | ||
80d19669 AN |
2455 | dev_maps = xmap_dereference(dev->xps_cpus_map); |
2456 | if (!dev_maps) | |
2457 | goto out_no_maps; | |
2458 | ||
2459 | if (num_possible_cpus() > 1) | |
2460 | possible_mask = cpumask_bits(cpu_possible_mask); | |
2461 | nr_ids = nr_cpu_ids; | |
2462 | clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count, | |
2463 | false); | |
024e9679 | 2464 | |
537c00de AD |
2465 | out_no_maps: |
2466 | mutex_unlock(&xps_map_mutex); | |
4d99f660 | 2467 | cpus_read_unlock(); |
537c00de AD |
2468 | } |
2469 | ||
6234f874 AD |
2470 | static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index) |
2471 | { | |
2472 | netif_reset_xps_queues(dev, index, dev->num_tx_queues - index); | |
2473 | } | |
2474 | ||
80d19669 AN |
2475 | static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index, |
2476 | u16 index, bool is_rxqs_map) | |
01c5f864 AD |
2477 | { |
2478 | struct xps_map *new_map; | |
2479 | int alloc_len = XPS_MIN_MAP_ALLOC; | |
2480 | int i, pos; | |
2481 | ||
2482 | for (pos = 0; map && pos < map->len; pos++) { | |
2483 | if (map->queues[pos] != index) | |
2484 | continue; | |
2485 | return map; | |
2486 | } | |
2487 | ||
80d19669 | 2488 | /* Need to add tx-queue to this CPU's/rx-queue's existing map */ |
01c5f864 AD |
2489 | if (map) { |
2490 | if (pos < map->alloc_len) | |
2491 | return map; | |
2492 | ||
2493 | alloc_len = map->alloc_len * 2; | |
2494 | } | |
2495 | ||
80d19669 AN |
2496 | /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's |
2497 | * map | |
2498 | */ | |
2499 | if (is_rxqs_map) | |
2500 | new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL); | |
2501 | else | |
2502 | new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL, | |
2503 | cpu_to_node(attr_index)); | |
01c5f864 AD |
2504 | if (!new_map) |
2505 | return NULL; | |
2506 | ||
2507 | for (i = 0; i < pos; i++) | |
2508 | new_map->queues[i] = map->queues[i]; | |
2509 | new_map->alloc_len = alloc_len; | |
2510 | new_map->len = pos; | |
2511 | ||
2512 | return new_map; | |
2513 | } | |
2514 | ||
4d99f660 | 2515 | /* Must be called under cpus_read_lock */ |
80d19669 AN |
2516 | int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask, |
2517 | u16 index, bool is_rxqs_map) | |
537c00de | 2518 | { |
80d19669 | 2519 | const unsigned long *online_mask = NULL, *possible_mask = NULL; |
01c5f864 | 2520 | struct xps_dev_maps *dev_maps, *new_dev_maps = NULL; |
80d19669 | 2521 | int i, j, tci, numa_node_id = -2; |
184c449f | 2522 | int maps_sz, num_tc = 1, tc = 0; |
537c00de | 2523 | struct xps_map *map, *new_map; |
01c5f864 | 2524 | bool active = false; |
80d19669 | 2525 | unsigned int nr_ids; |
537c00de | 2526 | |
184c449f | 2527 | if (dev->num_tc) { |
ffcfe25b | 2528 | /* Do not allow XPS on subordinate device directly */ |
184c449f | 2529 | num_tc = dev->num_tc; |
ffcfe25b AD |
2530 | if (num_tc < 0) |
2531 | return -EINVAL; | |
2532 | ||
2533 | /* If queue belongs to subordinate dev use its map */ | |
2534 | dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev; | |
2535 | ||
184c449f AD |
2536 | tc = netdev_txq_to_tc(dev, index); |
2537 | if (tc < 0) | |
2538 | return -EINVAL; | |
2539 | } | |
2540 | ||
537c00de | 2541 | mutex_lock(&xps_map_mutex); |
80d19669 AN |
2542 | if (is_rxqs_map) { |
2543 | maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues); | |
2544 | dev_maps = xmap_dereference(dev->xps_rxqs_map); | |
2545 | nr_ids = dev->num_rx_queues; | |
2546 | } else { | |
2547 | maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc); | |
2548 | if (num_possible_cpus() > 1) { | |
2549 | online_mask = cpumask_bits(cpu_online_mask); | |
2550 | possible_mask = cpumask_bits(cpu_possible_mask); | |
2551 | } | |
2552 | dev_maps = xmap_dereference(dev->xps_cpus_map); | |
2553 | nr_ids = nr_cpu_ids; | |
2554 | } | |
537c00de | 2555 | |
80d19669 AN |
2556 | if (maps_sz < L1_CACHE_BYTES) |
2557 | maps_sz = L1_CACHE_BYTES; | |
537c00de | 2558 | |
01c5f864 | 2559 | /* allocate memory for queue storage */ |
80d19669 AN |
2560 | for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids), |
2561 | j < nr_ids;) { | |
01c5f864 AD |
2562 | if (!new_dev_maps) |
2563 | new_dev_maps = kzalloc(maps_sz, GFP_KERNEL); | |
2bb60cb9 AD |
2564 | if (!new_dev_maps) { |
2565 | mutex_unlock(&xps_map_mutex); | |
01c5f864 | 2566 | return -ENOMEM; |
2bb60cb9 | 2567 | } |
01c5f864 | 2568 | |
80d19669 AN |
2569 | tci = j * num_tc + tc; |
2570 | map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) : | |
01c5f864 AD |
2571 | NULL; |
2572 | ||
80d19669 | 2573 | map = expand_xps_map(map, j, index, is_rxqs_map); |
01c5f864 AD |
2574 | if (!map) |
2575 | goto error; | |
2576 | ||
80d19669 | 2577 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); |
01c5f864 AD |
2578 | } |
2579 | ||
2580 | if (!new_dev_maps) | |
2581 | goto out_no_new_maps; | |
2582 | ||
867d0ad4 SD |
2583 | if (!dev_maps) { |
2584 | /* Increment static keys at most once per type */ | |
2585 | static_key_slow_inc_cpuslocked(&xps_needed); | |
2586 | if (is_rxqs_map) | |
2587 | static_key_slow_inc_cpuslocked(&xps_rxqs_needed); | |
2588 | } | |
04157469 | 2589 | |
80d19669 AN |
2590 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
2591 | j < nr_ids;) { | |
184c449f | 2592 | /* copy maps belonging to foreign traffic classes */ |
80d19669 | 2593 | for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) { |
184c449f | 2594 | /* fill in the new device map from the old device map */ |
80d19669 AN |
2595 | map = xmap_dereference(dev_maps->attr_map[tci]); |
2596 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); | |
184c449f AD |
2597 | } |
2598 | ||
2599 | /* We need to explicitly update tci as prevous loop | |
2600 | * could break out early if dev_maps is NULL. | |
2601 | */ | |
80d19669 | 2602 | tci = j * num_tc + tc; |
184c449f | 2603 | |
80d19669 AN |
2604 | if (netif_attr_test_mask(j, mask, nr_ids) && |
2605 | netif_attr_test_online(j, online_mask, nr_ids)) { | |
2606 | /* add tx-queue to CPU/rx-queue maps */ | |
01c5f864 AD |
2607 | int pos = 0; |
2608 | ||
80d19669 | 2609 | map = xmap_dereference(new_dev_maps->attr_map[tci]); |
01c5f864 AD |
2610 | while ((pos < map->len) && (map->queues[pos] != index)) |
2611 | pos++; | |
2612 | ||
2613 | if (pos == map->len) | |
2614 | map->queues[map->len++] = index; | |
537c00de | 2615 | #ifdef CONFIG_NUMA |
80d19669 AN |
2616 | if (!is_rxqs_map) { |
2617 | if (numa_node_id == -2) | |
2618 | numa_node_id = cpu_to_node(j); | |
2619 | else if (numa_node_id != cpu_to_node(j)) | |
2620 | numa_node_id = -1; | |
2621 | } | |
537c00de | 2622 | #endif |
01c5f864 AD |
2623 | } else if (dev_maps) { |
2624 | /* fill in the new device map from the old device map */ | |
80d19669 AN |
2625 | map = xmap_dereference(dev_maps->attr_map[tci]); |
2626 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); | |
537c00de | 2627 | } |
01c5f864 | 2628 | |
184c449f AD |
2629 | /* copy maps belonging to foreign traffic classes */ |
2630 | for (i = num_tc - tc, tci++; dev_maps && --i; tci++) { | |
2631 | /* fill in the new device map from the old device map */ | |
80d19669 AN |
2632 | map = xmap_dereference(dev_maps->attr_map[tci]); |
2633 | RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map); | |
184c449f | 2634 | } |
537c00de AD |
2635 | } |
2636 | ||
80d19669 AN |
2637 | if (is_rxqs_map) |
2638 | rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps); | |
2639 | else | |
2640 | rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps); | |
01c5f864 | 2641 | |
537c00de | 2642 | /* Cleanup old maps */ |
184c449f AD |
2643 | if (!dev_maps) |
2644 | goto out_no_old_maps; | |
2645 | ||
80d19669 AN |
2646 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
2647 | j < nr_ids;) { | |
2648 | for (i = num_tc, tci = j * num_tc; i--; tci++) { | |
2649 | new_map = xmap_dereference(new_dev_maps->attr_map[tci]); | |
2650 | map = xmap_dereference(dev_maps->attr_map[tci]); | |
01c5f864 AD |
2651 | if (map && map != new_map) |
2652 | kfree_rcu(map, rcu); | |
2653 | } | |
537c00de AD |
2654 | } |
2655 | ||
184c449f AD |
2656 | kfree_rcu(dev_maps, rcu); |
2657 | ||
2658 | out_no_old_maps: | |
01c5f864 AD |
2659 | dev_maps = new_dev_maps; |
2660 | active = true; | |
537c00de | 2661 | |
01c5f864 | 2662 | out_no_new_maps: |
80d19669 AN |
2663 | if (!is_rxqs_map) { |
2664 | /* update Tx queue numa node */ | |
2665 | netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index), | |
2666 | (numa_node_id >= 0) ? | |
2667 | numa_node_id : NUMA_NO_NODE); | |
2668 | } | |
537c00de | 2669 | |
01c5f864 AD |
2670 | if (!dev_maps) |
2671 | goto out_no_maps; | |
2672 | ||
80d19669 AN |
2673 | /* removes tx-queue from unused CPUs/rx-queues */ |
2674 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), | |
2675 | j < nr_ids;) { | |
2676 | for (i = tc, tci = j * num_tc; i--; tci++) | |
184c449f | 2677 | active |= remove_xps_queue(dev_maps, tci, index); |
80d19669 AN |
2678 | if (!netif_attr_test_mask(j, mask, nr_ids) || |
2679 | !netif_attr_test_online(j, online_mask, nr_ids)) | |
184c449f AD |
2680 | active |= remove_xps_queue(dev_maps, tci, index); |
2681 | for (i = num_tc - tc, tci++; --i; tci++) | |
2682 | active |= remove_xps_queue(dev_maps, tci, index); | |
01c5f864 AD |
2683 | } |
2684 | ||
2685 | /* free map if not active */ | |
867d0ad4 SD |
2686 | if (!active) |
2687 | reset_xps_maps(dev, dev_maps, is_rxqs_map); | |
01c5f864 AD |
2688 | |
2689 | out_no_maps: | |
537c00de AD |
2690 | mutex_unlock(&xps_map_mutex); |
2691 | ||
2692 | return 0; | |
2693 | error: | |
01c5f864 | 2694 | /* remove any maps that we added */ |
80d19669 AN |
2695 | for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids), |
2696 | j < nr_ids;) { | |
2697 | for (i = num_tc, tci = j * num_tc; i--; tci++) { | |
2698 | new_map = xmap_dereference(new_dev_maps->attr_map[tci]); | |
184c449f | 2699 | map = dev_maps ? |
80d19669 | 2700 | xmap_dereference(dev_maps->attr_map[tci]) : |
184c449f AD |
2701 | NULL; |
2702 | if (new_map && new_map != map) | |
2703 | kfree(new_map); | |
2704 | } | |
01c5f864 AD |
2705 | } |
2706 | ||
537c00de AD |
2707 | mutex_unlock(&xps_map_mutex); |
2708 | ||
537c00de AD |
2709 | kfree(new_dev_maps); |
2710 | return -ENOMEM; | |
2711 | } | |
4d99f660 | 2712 | EXPORT_SYMBOL_GPL(__netif_set_xps_queue); |
80d19669 AN |
2713 | |
2714 | int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask, | |
2715 | u16 index) | |
2716 | { | |
4d99f660 AV |
2717 | int ret; |
2718 | ||
2719 | cpus_read_lock(); | |
2720 | ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false); | |
2721 | cpus_read_unlock(); | |
2722 | ||
2723 | return ret; | |
80d19669 | 2724 | } |
537c00de AD |
2725 | EXPORT_SYMBOL(netif_set_xps_queue); |
2726 | ||
2727 | #endif | |
ffcfe25b AD |
2728 | static void netdev_unbind_all_sb_channels(struct net_device *dev) |
2729 | { | |
2730 | struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; | |
2731 | ||
2732 | /* Unbind any subordinate channels */ | |
2733 | while (txq-- != &dev->_tx[0]) { | |
2734 | if (txq->sb_dev) | |
2735 | netdev_unbind_sb_channel(dev, txq->sb_dev); | |
2736 | } | |
2737 | } | |
2738 | ||
9cf1f6a8 AD |
2739 | void netdev_reset_tc(struct net_device *dev) |
2740 | { | |
6234f874 AD |
2741 | #ifdef CONFIG_XPS |
2742 | netif_reset_xps_queues_gt(dev, 0); | |
2743 | #endif | |
ffcfe25b AD |
2744 | netdev_unbind_all_sb_channels(dev); |
2745 | ||
2746 | /* Reset TC configuration of device */ | |
9cf1f6a8 AD |
2747 | dev->num_tc = 0; |
2748 | memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq)); | |
2749 | memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map)); | |
2750 | } | |
2751 | EXPORT_SYMBOL(netdev_reset_tc); | |
2752 | ||
2753 | int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset) | |
2754 | { | |
2755 | if (tc >= dev->num_tc) | |
2756 | return -EINVAL; | |
2757 | ||
6234f874 AD |
2758 | #ifdef CONFIG_XPS |
2759 | netif_reset_xps_queues(dev, offset, count); | |
2760 | #endif | |
9cf1f6a8 AD |
2761 | dev->tc_to_txq[tc].count = count; |
2762 | dev->tc_to_txq[tc].offset = offset; | |
2763 | return 0; | |
2764 | } | |
2765 | EXPORT_SYMBOL(netdev_set_tc_queue); | |
2766 | ||
2767 | int netdev_set_num_tc(struct net_device *dev, u8 num_tc) | |
2768 | { | |
2769 | if (num_tc > TC_MAX_QUEUE) | |
2770 | return -EINVAL; | |
2771 | ||
6234f874 AD |
2772 | #ifdef CONFIG_XPS |
2773 | netif_reset_xps_queues_gt(dev, 0); | |
2774 | #endif | |
ffcfe25b AD |
2775 | netdev_unbind_all_sb_channels(dev); |
2776 | ||
9cf1f6a8 AD |
2777 | dev->num_tc = num_tc; |
2778 | return 0; | |
2779 | } | |
2780 | EXPORT_SYMBOL(netdev_set_num_tc); | |
2781 | ||
ffcfe25b AD |
2782 | void netdev_unbind_sb_channel(struct net_device *dev, |
2783 | struct net_device *sb_dev) | |
2784 | { | |
2785 | struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues]; | |
2786 | ||
2787 | #ifdef CONFIG_XPS | |
2788 | netif_reset_xps_queues_gt(sb_dev, 0); | |
2789 | #endif | |
2790 | memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq)); | |
2791 | memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map)); | |
2792 | ||
2793 | while (txq-- != &dev->_tx[0]) { | |
2794 | if (txq->sb_dev == sb_dev) | |
2795 | txq->sb_dev = NULL; | |
2796 | } | |
2797 | } | |
2798 | EXPORT_SYMBOL(netdev_unbind_sb_channel); | |
2799 | ||
2800 | int netdev_bind_sb_channel_queue(struct net_device *dev, | |
2801 | struct net_device *sb_dev, | |
2802 | u8 tc, u16 count, u16 offset) | |
2803 | { | |
2804 | /* Make certain the sb_dev and dev are already configured */ | |
2805 | if (sb_dev->num_tc >= 0 || tc >= dev->num_tc) | |
2806 | return -EINVAL; | |
2807 | ||
2808 | /* We cannot hand out queues we don't have */ | |
2809 | if ((offset + count) > dev->real_num_tx_queues) | |
2810 | return -EINVAL; | |
2811 | ||
2812 | /* Record the mapping */ | |
2813 | sb_dev->tc_to_txq[tc].count = count; | |
2814 | sb_dev->tc_to_txq[tc].offset = offset; | |
2815 | ||
2816 | /* Provide a way for Tx queue to find the tc_to_txq map or | |
2817 | * XPS map for itself. | |
2818 | */ | |
2819 | while (count--) | |
2820 | netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev; | |
2821 | ||
2822 | return 0; | |
2823 | } | |
2824 | EXPORT_SYMBOL(netdev_bind_sb_channel_queue); | |
2825 | ||
2826 | int netdev_set_sb_channel(struct net_device *dev, u16 channel) | |
2827 | { | |
2828 | /* Do not use a multiqueue device to represent a subordinate channel */ | |
2829 | if (netif_is_multiqueue(dev)) | |
2830 | return -ENODEV; | |
2831 | ||
2832 | /* We allow channels 1 - 32767 to be used for subordinate channels. | |
2833 | * Channel 0 is meant to be "native" mode and used only to represent | |
2834 | * the main root device. We allow writing 0 to reset the device back | |
2835 | * to normal mode after being used as a subordinate channel. | |
2836 | */ | |
2837 | if (channel > S16_MAX) | |
2838 | return -EINVAL; | |
2839 | ||
2840 | dev->num_tc = -channel; | |
2841 | ||
2842 | return 0; | |
2843 | } | |
2844 | EXPORT_SYMBOL(netdev_set_sb_channel); | |
2845 | ||
f0796d5c JF |
2846 | /* |
2847 | * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues | |
3a053b1a | 2848 | * greater than real_num_tx_queues stale skbs on the qdisc must be flushed. |
f0796d5c | 2849 | */ |
e6484930 | 2850 | int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq) |
f0796d5c | 2851 | { |
ac5b7019 | 2852 | bool disabling; |
1d24eb48 TH |
2853 | int rc; |
2854 | ||
ac5b7019 JK |
2855 | disabling = txq < dev->real_num_tx_queues; |
2856 | ||
e6484930 TH |
2857 | if (txq < 1 || txq > dev->num_tx_queues) |
2858 | return -EINVAL; | |
f0796d5c | 2859 | |
5c56580b BH |
2860 | if (dev->reg_state == NETREG_REGISTERED || |
2861 | dev->reg_state == NETREG_UNREGISTERING) { | |
e6484930 TH |
2862 | ASSERT_RTNL(); |
2863 | ||
1d24eb48 TH |
2864 | rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues, |
2865 | txq); | |
bf264145 TH |
2866 | if (rc) |
2867 | return rc; | |
2868 | ||
4f57c087 JF |
2869 | if (dev->num_tc) |
2870 | netif_setup_tc(dev, txq); | |
2871 | ||
ac5b7019 JK |
2872 | dev->real_num_tx_queues = txq; |
2873 | ||
2874 | if (disabling) { | |
2875 | synchronize_net(); | |
e6484930 | 2876 | qdisc_reset_all_tx_gt(dev, txq); |
024e9679 AD |
2877 | #ifdef CONFIG_XPS |
2878 | netif_reset_xps_queues_gt(dev, txq); | |
2879 | #endif | |
2880 | } | |
ac5b7019 JK |
2881 | } else { |
2882 | dev->real_num_tx_queues = txq; | |
f0796d5c | 2883 | } |
e6484930 | 2884 | |
e6484930 | 2885 | return 0; |
f0796d5c JF |
2886 | } |
2887 | EXPORT_SYMBOL(netif_set_real_num_tx_queues); | |
56079431 | 2888 | |
a953be53 | 2889 | #ifdef CONFIG_SYSFS |
62fe0b40 BH |
2890 | /** |
2891 | * netif_set_real_num_rx_queues - set actual number of RX queues used | |
2892 | * @dev: Network device | |
2893 | * @rxq: Actual number of RX queues | |
2894 | * | |
2895 | * This must be called either with the rtnl_lock held or before | |
2896 | * registration of the net device. Returns 0 on success, or a | |
4e7f7951 BH |
2897 | * negative error code. If called before registration, it always |
2898 | * succeeds. | |
62fe0b40 BH |
2899 | */ |
2900 | int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq) | |
2901 | { | |
2902 | int rc; | |
2903 | ||
bd25fa7b TH |
2904 | if (rxq < 1 || rxq > dev->num_rx_queues) |
2905 | return -EINVAL; | |
2906 | ||
62fe0b40 BH |
2907 | if (dev->reg_state == NETREG_REGISTERED) { |
2908 | ASSERT_RTNL(); | |
2909 | ||
62fe0b40 BH |
2910 | rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues, |
2911 | rxq); | |
2912 | if (rc) | |
2913 | return rc; | |
62fe0b40 BH |
2914 | } |
2915 | ||
2916 | dev->real_num_rx_queues = rxq; | |
2917 | return 0; | |
2918 | } | |
2919 | EXPORT_SYMBOL(netif_set_real_num_rx_queues); | |
2920 | #endif | |
2921 | ||
2c53040f BH |
2922 | /** |
2923 | * netif_get_num_default_rss_queues - default number of RSS queues | |
16917b87 YM |
2924 | * |
2925 | * This routine should set an upper limit on the number of RSS queues | |
2926 | * used by default by multiqueue devices. | |
2927 | */ | |
a55b138b | 2928 | int netif_get_num_default_rss_queues(void) |
16917b87 | 2929 | { |
40e4e713 HS |
2930 | return is_kdump_kernel() ? |
2931 | 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus()); | |
16917b87 YM |
2932 | } |
2933 | EXPORT_SYMBOL(netif_get_num_default_rss_queues); | |
2934 | ||
3bcb846c | 2935 | static void __netif_reschedule(struct Qdisc *q) |
56079431 | 2936 | { |
def82a1d JP |
2937 | struct softnet_data *sd; |
2938 | unsigned long flags; | |
56079431 | 2939 | |
def82a1d | 2940 | local_irq_save(flags); |
903ceff7 | 2941 | sd = this_cpu_ptr(&softnet_data); |
a9cbd588 CG |
2942 | q->next_sched = NULL; |
2943 | *sd->output_queue_tailp = q; | |
2944 | sd->output_queue_tailp = &q->next_sched; | |
def82a1d JP |
2945 | raise_softirq_irqoff(NET_TX_SOFTIRQ); |
2946 | local_irq_restore(flags); | |
2947 | } | |
2948 | ||
2949 | void __netif_schedule(struct Qdisc *q) | |
2950 | { | |
2951 | if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state)) | |
2952 | __netif_reschedule(q); | |
56079431 DV |
2953 | } |
2954 | EXPORT_SYMBOL(__netif_schedule); | |
2955 | ||
e6247027 ED |
2956 | struct dev_kfree_skb_cb { |
2957 | enum skb_free_reason reason; | |
2958 | }; | |
2959 | ||
2960 | static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb) | |
56079431 | 2961 | { |
e6247027 ED |
2962 | return (struct dev_kfree_skb_cb *)skb->cb; |
2963 | } | |
2964 | ||
46e5da40 JF |
2965 | void netif_schedule_queue(struct netdev_queue *txq) |
2966 | { | |
2967 | rcu_read_lock(); | |
5be5515a | 2968 | if (!netif_xmit_stopped(txq)) { |
46e5da40 JF |
2969 | struct Qdisc *q = rcu_dereference(txq->qdisc); |
2970 | ||
2971 | __netif_schedule(q); | |
2972 | } | |
2973 | rcu_read_unlock(); | |
2974 | } | |
2975 | EXPORT_SYMBOL(netif_schedule_queue); | |
2976 | ||
46e5da40 JF |
2977 | void netif_tx_wake_queue(struct netdev_queue *dev_queue) |
2978 | { | |
2979 | if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) { | |
2980 | struct Qdisc *q; | |
2981 | ||
2982 | rcu_read_lock(); | |
2983 | q = rcu_dereference(dev_queue->qdisc); | |
2984 | __netif_schedule(q); | |
2985 | rcu_read_unlock(); | |
2986 | } | |
2987 | } | |
2988 | EXPORT_SYMBOL(netif_tx_wake_queue); | |
2989 | ||
e6247027 | 2990 | void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason) |
56079431 | 2991 | { |
e6247027 | 2992 | unsigned long flags; |
56079431 | 2993 | |
9899886d MJ |
2994 | if (unlikely(!skb)) |
2995 | return; | |
2996 | ||
63354797 | 2997 | if (likely(refcount_read(&skb->users) == 1)) { |
e6247027 | 2998 | smp_rmb(); |
63354797 RE |
2999 | refcount_set(&skb->users, 0); |
3000 | } else if (likely(!refcount_dec_and_test(&skb->users))) { | |
e6247027 | 3001 | return; |
bea3348e | 3002 | } |
e6247027 ED |
3003 | get_kfree_skb_cb(skb)->reason = reason; |
3004 | local_irq_save(flags); | |
3005 | skb->next = __this_cpu_read(softnet_data.completion_queue); | |
3006 | __this_cpu_write(softnet_data.completion_queue, skb); | |
3007 | raise_softirq_irqoff(NET_TX_SOFTIRQ); | |
3008 | local_irq_restore(flags); | |
56079431 | 3009 | } |
e6247027 | 3010 | EXPORT_SYMBOL(__dev_kfree_skb_irq); |
56079431 | 3011 | |
e6247027 | 3012 | void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason) |
56079431 DV |
3013 | { |
3014 | if (in_irq() || irqs_disabled()) | |
e6247027 | 3015 | __dev_kfree_skb_irq(skb, reason); |
56079431 DV |
3016 | else |
3017 | dev_kfree_skb(skb); | |
3018 | } | |
e6247027 | 3019 | EXPORT_SYMBOL(__dev_kfree_skb_any); |
56079431 DV |
3020 | |
3021 | ||
bea3348e SH |
3022 | /** |
3023 | * netif_device_detach - mark device as removed | |
3024 | * @dev: network device | |
3025 | * | |
3026 | * Mark device as removed from system and therefore no longer available. | |
3027 | */ | |
56079431 DV |
3028 | void netif_device_detach(struct net_device *dev) |
3029 | { | |
3030 | if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) && | |
3031 | netif_running(dev)) { | |
d543103a | 3032 | netif_tx_stop_all_queues(dev); |
56079431 DV |
3033 | } |
3034 | } | |
3035 | EXPORT_SYMBOL(netif_device_detach); | |
3036 | ||
bea3348e SH |
3037 | /** |
3038 | * netif_device_attach - mark device as attached | |
3039 | * @dev: network device | |
3040 | * | |
3041 | * Mark device as attached from system and restart if needed. | |
3042 | */ | |
56079431 DV |
3043 | void netif_device_attach(struct net_device *dev) |
3044 | { | |
3045 | if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) && | |
3046 | netif_running(dev)) { | |
d543103a | 3047 | netif_tx_wake_all_queues(dev); |
4ec93edb | 3048 | __netdev_watchdog_up(dev); |
56079431 DV |
3049 | } |
3050 | } | |
3051 | EXPORT_SYMBOL(netif_device_attach); | |
3052 | ||
5605c762 JP |
3053 | /* |
3054 | * Returns a Tx hash based on the given packet descriptor a Tx queues' number | |
3055 | * to be used as a distribution range. | |
3056 | */ | |
eadec877 AD |
3057 | static u16 skb_tx_hash(const struct net_device *dev, |
3058 | const struct net_device *sb_dev, | |
3059 | struct sk_buff *skb) | |
5605c762 JP |
3060 | { |
3061 | u32 hash; | |
3062 | u16 qoffset = 0; | |
1b837d48 | 3063 | u16 qcount = dev->real_num_tx_queues; |
5605c762 | 3064 | |
eadec877 AD |
3065 | if (dev->num_tc) { |
3066 | u8 tc = netdev_get_prio_tc_map(dev, skb->priority); | |
3067 | ||
3068 | qoffset = sb_dev->tc_to_txq[tc].offset; | |
3069 | qcount = sb_dev->tc_to_txq[tc].count; | |
3070 | } | |
3071 | ||
5605c762 JP |
3072 | if (skb_rx_queue_recorded(skb)) { |
3073 | hash = skb_get_rx_queue(skb); | |
1b837d48 AD |
3074 | while (unlikely(hash >= qcount)) |
3075 | hash -= qcount; | |
eadec877 | 3076 | return hash + qoffset; |
5605c762 JP |
3077 | } |
3078 | ||
3079 | return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset; | |
3080 | } | |
5605c762 | 3081 | |
36c92474 BH |
3082 | static void skb_warn_bad_offload(const struct sk_buff *skb) |
3083 | { | |
84d15ae5 | 3084 | static const netdev_features_t null_features; |
36c92474 | 3085 | struct net_device *dev = skb->dev; |
88ad4175 | 3086 | const char *name = ""; |
36c92474 | 3087 | |
c846ad9b BG |
3088 | if (!net_ratelimit()) |
3089 | return; | |
3090 | ||
88ad4175 BM |
3091 | if (dev) { |
3092 | if (dev->dev.parent) | |
3093 | name = dev_driver_string(dev->dev.parent); | |
3094 | else | |
3095 | name = netdev_name(dev); | |
3096 | } | |
6413139d WB |
3097 | skb_dump(KERN_WARNING, skb, false); |
3098 | WARN(1, "%s: caps=(%pNF, %pNF)\n", | |
88ad4175 | 3099 | name, dev ? &dev->features : &null_features, |
6413139d | 3100 | skb->sk ? &skb->sk->sk_route_caps : &null_features); |
36c92474 BH |
3101 | } |
3102 | ||
1da177e4 LT |
3103 | /* |
3104 | * Invalidate hardware checksum when packet is to be mangled, and | |
3105 | * complete checksum manually on outgoing path. | |
3106 | */ | |
84fa7933 | 3107 | int skb_checksum_help(struct sk_buff *skb) |
1da177e4 | 3108 | { |
d3bc23e7 | 3109 | __wsum csum; |
663ead3b | 3110 | int ret = 0, offset; |
1da177e4 | 3111 | |
84fa7933 | 3112 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
a430a43d HX |
3113 | goto out_set_summed; |
3114 | ||
3115 | if (unlikely(skb_shinfo(skb)->gso_size)) { | |
36c92474 BH |
3116 | skb_warn_bad_offload(skb); |
3117 | return -EINVAL; | |
1da177e4 LT |
3118 | } |
3119 | ||
cef401de ED |
3120 | /* Before computing a checksum, we should make sure no frag could |
3121 | * be modified by an external entity : checksum could be wrong. | |
3122 | */ | |
3123 | if (skb_has_shared_frag(skb)) { | |
3124 | ret = __skb_linearize(skb); | |
3125 | if (ret) | |
3126 | goto out; | |
3127 | } | |
3128 | ||
55508d60 | 3129 | offset = skb_checksum_start_offset(skb); |
a030847e HX |
3130 | BUG_ON(offset >= skb_headlen(skb)); |
3131 | csum = skb_checksum(skb, offset, skb->len - offset, 0); | |
3132 | ||
3133 | offset += skb->csum_offset; | |
3134 | BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb)); | |
3135 | ||
8211fbfa HK |
3136 | ret = skb_ensure_writable(skb, offset + sizeof(__sum16)); |
3137 | if (ret) | |
3138 | goto out; | |
1da177e4 | 3139 | |
4f2e4ad5 | 3140 | *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0; |
a430a43d | 3141 | out_set_summed: |
1da177e4 | 3142 | skb->ip_summed = CHECKSUM_NONE; |
4ec93edb | 3143 | out: |
1da177e4 LT |
3144 | return ret; |
3145 | } | |
d1b19dff | 3146 | EXPORT_SYMBOL(skb_checksum_help); |
1da177e4 | 3147 | |
b72b5bf6 DC |
3148 | int skb_crc32c_csum_help(struct sk_buff *skb) |
3149 | { | |
3150 | __le32 crc32c_csum; | |
3151 | int ret = 0, offset, start; | |
3152 | ||
3153 | if (skb->ip_summed != CHECKSUM_PARTIAL) | |
3154 | goto out; | |
3155 | ||
3156 | if (unlikely(skb_is_gso(skb))) | |
3157 | goto out; | |
3158 | ||
3159 | /* Before computing a checksum, we should make sure no frag could | |
3160 | * be modified by an external entity : checksum could be wrong. | |
3161 | */ | |
3162 | if (unlikely(skb_has_shared_frag(skb))) { | |
3163 | ret = __skb_linearize(skb); | |
3164 | if (ret) | |
3165 | goto out; | |
3166 | } | |
3167 | start = skb_checksum_start_offset(skb); | |
3168 | offset = start + offsetof(struct sctphdr, checksum); | |
3169 | if (WARN_ON_ONCE(offset >= skb_headlen(skb))) { | |
3170 | ret = -EINVAL; | |
3171 | goto out; | |
3172 | } | |
8211fbfa HK |
3173 | |
3174 | ret = skb_ensure_writable(skb, offset + sizeof(__le32)); | |
3175 | if (ret) | |
3176 | goto out; | |
3177 | ||
b72b5bf6 DC |
3178 | crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start, |
3179 | skb->len - start, ~(__u32)0, | |
3180 | crc32c_csum_stub)); | |
3181 | *(__le32 *)(skb->data + offset) = crc32c_csum; | |
3182 | skb->ip_summed = CHECKSUM_NONE; | |
dba00306 | 3183 | skb->csum_not_inet = 0; |
b72b5bf6 DC |
3184 | out: |
3185 | return ret; | |
3186 | } | |
3187 | ||
53d6471c | 3188 | __be16 skb_network_protocol(struct sk_buff *skb, int *depth) |
f6a78bfc | 3189 | { |
252e3346 | 3190 | __be16 type = skb->protocol; |
f6a78bfc | 3191 | |
19acc327 PS |
3192 | /* Tunnel gso handlers can set protocol to ethernet. */ |
3193 | if (type == htons(ETH_P_TEB)) { | |
3194 | struct ethhdr *eth; | |
3195 | ||
3196 | if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr)))) | |
3197 | return 0; | |
3198 | ||
1dfe82eb | 3199 | eth = (struct ethhdr *)skb->data; |
19acc327 PS |
3200 | type = eth->h_proto; |
3201 | } | |
3202 | ||
d4bcef3f | 3203 | return __vlan_get_protocol(skb, type, depth); |
ec5f0615 PS |
3204 | } |
3205 | ||
3206 | /** | |
3207 | * skb_mac_gso_segment - mac layer segmentation handler. | |
3208 | * @skb: buffer to segment | |
3209 | * @features: features for the output path (see dev->features) | |
3210 | */ | |
3211 | struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb, | |
3212 | netdev_features_t features) | |
3213 | { | |
3214 | struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT); | |
3215 | struct packet_offload *ptype; | |
53d6471c VY |
3216 | int vlan_depth = skb->mac_len; |
3217 | __be16 type = skb_network_protocol(skb, &vlan_depth); | |
ec5f0615 PS |
3218 | |
3219 | if (unlikely(!type)) | |
3220 | return ERR_PTR(-EINVAL); | |
3221 | ||
53d6471c | 3222 | __skb_pull(skb, vlan_depth); |
f6a78bfc HX |
3223 | |
3224 | rcu_read_lock(); | |
22061d80 | 3225 | list_for_each_entry_rcu(ptype, &offload_base, list) { |
f191a1d1 | 3226 | if (ptype->type == type && ptype->callbacks.gso_segment) { |
f191a1d1 | 3227 | segs = ptype->callbacks.gso_segment(skb, features); |
f6a78bfc HX |
3228 | break; |
3229 | } | |
3230 | } | |
3231 | rcu_read_unlock(); | |
3232 | ||
98e399f8 | 3233 | __skb_push(skb, skb->data - skb_mac_header(skb)); |
576a30eb | 3234 | |
f6a78bfc HX |
3235 | return segs; |
3236 | } | |
05e8ef4a PS |
3237 | EXPORT_SYMBOL(skb_mac_gso_segment); |
3238 | ||
3239 | ||
3240 | /* openvswitch calls this on rx path, so we need a different check. | |
3241 | */ | |
3242 | static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path) | |
3243 | { | |
3244 | if (tx_path) | |
0c19f846 WB |
3245 | return skb->ip_summed != CHECKSUM_PARTIAL && |
3246 | skb->ip_summed != CHECKSUM_UNNECESSARY; | |
6e7bc478 ED |
3247 | |
3248 | return skb->ip_summed == CHECKSUM_NONE; | |
05e8ef4a PS |
3249 | } |
3250 | ||
3251 | /** | |
3252 | * __skb_gso_segment - Perform segmentation on skb. | |
3253 | * @skb: buffer to segment | |
3254 | * @features: features for the output path (see dev->features) | |
3255 | * @tx_path: whether it is called in TX path | |
3256 | * | |
3257 | * This function segments the given skb and returns a list of segments. | |
3258 | * | |
3259 | * It may return NULL if the skb requires no segmentation. This is | |
3260 | * only possible when GSO is used for verifying header integrity. | |
9207f9d4 KK |
3261 | * |
3262 | * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb. | |
05e8ef4a PS |
3263 | */ |
3264 | struct sk_buff *__skb_gso_segment(struct sk_buff *skb, | |
3265 | netdev_features_t features, bool tx_path) | |
3266 | { | |
b2504a5d ED |
3267 | struct sk_buff *segs; |
3268 | ||
05e8ef4a PS |
3269 | if (unlikely(skb_needs_check(skb, tx_path))) { |
3270 | int err; | |
3271 | ||
b2504a5d | 3272 | /* We're going to init ->check field in TCP or UDP header */ |
a40e0a66 | 3273 | err = skb_cow_head(skb, 0); |
3274 | if (err < 0) | |
05e8ef4a PS |
3275 | return ERR_PTR(err); |
3276 | } | |
3277 | ||
802ab55a AD |
3278 | /* Only report GSO partial support if it will enable us to |
3279 | * support segmentation on this frame without needing additional | |
3280 | * work. | |
3281 | */ | |
3282 | if (features & NETIF_F_GSO_PARTIAL) { | |
3283 | netdev_features_t partial_features = NETIF_F_GSO_ROBUST; | |
3284 | struct net_device *dev = skb->dev; | |
3285 | ||
3286 | partial_features |= dev->features & dev->gso_partial_features; | |
3287 | if (!skb_gso_ok(skb, features | partial_features)) | |
3288 | features &= ~NETIF_F_GSO_PARTIAL; | |
3289 | } | |
3290 | ||
9207f9d4 KK |
3291 | BUILD_BUG_ON(SKB_SGO_CB_OFFSET + |
3292 | sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb)); | |
3293 | ||
68c33163 | 3294 | SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb); |
3347c960 ED |
3295 | SKB_GSO_CB(skb)->encap_level = 0; |
3296 | ||
05e8ef4a PS |
3297 | skb_reset_mac_header(skb); |
3298 | skb_reset_mac_len(skb); | |
3299 | ||
b2504a5d ED |
3300 | segs = skb_mac_gso_segment(skb, features); |
3301 | ||
3a1296a3 | 3302 | if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs))) |
b2504a5d ED |
3303 | skb_warn_bad_offload(skb); |
3304 | ||
3305 | return segs; | |
05e8ef4a | 3306 | } |
12b0004d | 3307 | EXPORT_SYMBOL(__skb_gso_segment); |
f6a78bfc | 3308 | |
fb286bb2 HX |
3309 | /* Take action when hardware reception checksum errors are detected. */ |
3310 | #ifdef CONFIG_BUG | |
7fe50ac8 | 3311 | void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb) |
fb286bb2 HX |
3312 | { |
3313 | if (net_ratelimit()) { | |
7b6cd1ce | 3314 | pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>"); |
6413139d | 3315 | skb_dump(KERN_ERR, skb, true); |
fb286bb2 HX |
3316 | dump_stack(); |
3317 | } | |
3318 | } | |
3319 | EXPORT_SYMBOL(netdev_rx_csum_fault); | |
3320 | #endif | |
3321 | ||
ab74cfeb | 3322 | /* XXX: check that highmem exists at all on the given machine. */ |
c1e756bf | 3323 | static int illegal_highdma(struct net_device *dev, struct sk_buff *skb) |
1da177e4 | 3324 | { |
3d3a8533 | 3325 | #ifdef CONFIG_HIGHMEM |
1da177e4 | 3326 | int i; |
f4563a75 | 3327 | |
5acbbd42 | 3328 | if (!(dev->features & NETIF_F_HIGHDMA)) { |
ea2ab693 IC |
3329 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
3330 | skb_frag_t *frag = &skb_shinfo(skb)->frags[i]; | |
f4563a75 | 3331 | |
ea2ab693 | 3332 | if (PageHighMem(skb_frag_page(frag))) |
5acbbd42 | 3333 | return 1; |
ea2ab693 | 3334 | } |
5acbbd42 | 3335 | } |
3d3a8533 | 3336 | #endif |
1da177e4 LT |
3337 | return 0; |
3338 | } | |
1da177e4 | 3339 | |
3b392ddb SH |
3340 | /* If MPLS offload request, verify we are testing hardware MPLS features |
3341 | * instead of standard features for the netdev. | |
3342 | */ | |
d0edc7bf | 3343 | #if IS_ENABLED(CONFIG_NET_MPLS_GSO) |
3b392ddb SH |
3344 | static netdev_features_t net_mpls_features(struct sk_buff *skb, |
3345 | netdev_features_t features, | |
3346 | __be16 type) | |
3347 | { | |
25cd9ba0 | 3348 | if (eth_p_mpls(type)) |
3b392ddb SH |
3349 | features &= skb->dev->mpls_features; |
3350 | ||
3351 | return features; | |
3352 | } | |
3353 | #else | |
3354 | static netdev_features_t net_mpls_features(struct sk_buff *skb, | |
3355 | netdev_features_t features, | |
3356 | __be16 type) | |
3357 | { | |
3358 | return features; | |
3359 | } | |
3360 | #endif | |
3361 | ||
c8f44aff | 3362 | static netdev_features_t harmonize_features(struct sk_buff *skb, |
c1e756bf | 3363 | netdev_features_t features) |
f01a5236 | 3364 | { |
53d6471c | 3365 | int tmp; |
3b392ddb SH |
3366 | __be16 type; |
3367 | ||
3368 | type = skb_network_protocol(skb, &tmp); | |
3369 | features = net_mpls_features(skb, features, type); | |
53d6471c | 3370 | |
c0d680e5 | 3371 | if (skb->ip_summed != CHECKSUM_NONE && |
3b392ddb | 3372 | !can_checksum_protocol(features, type)) { |
996e8021 | 3373 | features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); |
f01a5236 | 3374 | } |
7be2c82c ED |
3375 | if (illegal_highdma(skb->dev, skb)) |
3376 | features &= ~NETIF_F_SG; | |
f01a5236 JG |
3377 | |
3378 | return features; | |
3379 | } | |
3380 | ||
e38f3025 TM |
3381 | netdev_features_t passthru_features_check(struct sk_buff *skb, |
3382 | struct net_device *dev, | |
3383 | netdev_features_t features) | |
3384 | { | |
3385 | return features; | |
3386 | } | |
3387 | EXPORT_SYMBOL(passthru_features_check); | |
3388 | ||
7ce23672 | 3389 | static netdev_features_t dflt_features_check(struct sk_buff *skb, |
8cb65d00 TM |
3390 | struct net_device *dev, |
3391 | netdev_features_t features) | |
3392 | { | |
3393 | return vlan_features_check(skb, features); | |
3394 | } | |
3395 | ||
cbc53e08 AD |
3396 | static netdev_features_t gso_features_check(const struct sk_buff *skb, |
3397 | struct net_device *dev, | |
3398 | netdev_features_t features) | |
3399 | { | |
3400 | u16 gso_segs = skb_shinfo(skb)->gso_segs; | |
3401 | ||
3402 | if (gso_segs > dev->gso_max_segs) | |
3403 | return features & ~NETIF_F_GSO_MASK; | |
3404 | ||
802ab55a AD |
3405 | /* Support for GSO partial features requires software |
3406 | * intervention before we can actually process the packets | |
3407 | * so we need to strip support for any partial features now | |
3408 | * and we can pull them back in after we have partially | |
3409 | * segmented the frame. | |
3410 | */ | |
3411 | if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL)) | |
3412 | features &= ~dev->gso_partial_features; | |
3413 | ||
3414 | /* Make sure to clear the IPv4 ID mangling feature if the | |
3415 | * IPv4 header has the potential to be fragmented. | |
cbc53e08 AD |
3416 | */ |
3417 | if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) { | |
3418 | struct iphdr *iph = skb->encapsulation ? | |
3419 | inner_ip_hdr(skb) : ip_hdr(skb); | |
3420 | ||
3421 | if (!(iph->frag_off & htons(IP_DF))) | |
3422 | features &= ~NETIF_F_TSO_MANGLEID; | |
3423 | } | |
3424 | ||
3425 | return features; | |
3426 | } | |
3427 | ||
c1e756bf | 3428 | netdev_features_t netif_skb_features(struct sk_buff *skb) |
58e998c6 | 3429 | { |
5f35227e | 3430 | struct net_device *dev = skb->dev; |
fcbeb976 | 3431 | netdev_features_t features = dev->features; |
58e998c6 | 3432 | |
cbc53e08 AD |
3433 | if (skb_is_gso(skb)) |
3434 | features = gso_features_check(skb, dev, features); | |
30b678d8 | 3435 | |
5f35227e JG |
3436 | /* If encapsulation offload request, verify we are testing |
3437 | * hardware encapsulation features instead of standard | |
3438 | * features for the netdev | |
3439 | */ | |
3440 | if (skb->encapsulation) | |
3441 | features &= dev->hw_enc_features; | |
3442 | ||
f5a7fb88 TM |
3443 | if (skb_vlan_tagged(skb)) |
3444 | features = netdev_intersect_features(features, | |
3445 | dev->vlan_features | | |
3446 | NETIF_F_HW_VLAN_CTAG_TX | | |
3447 | NETIF_F_HW_VLAN_STAG_TX); | |
f01a5236 | 3448 | |
5f35227e JG |
3449 | if (dev->netdev_ops->ndo_features_check) |
3450 | features &= dev->netdev_ops->ndo_features_check(skb, dev, | |
3451 | features); | |
8cb65d00 TM |
3452 | else |
3453 | features &= dflt_features_check(skb, dev, features); | |
5f35227e | 3454 | |
c1e756bf | 3455 | return harmonize_features(skb, features); |
58e998c6 | 3456 | } |
c1e756bf | 3457 | EXPORT_SYMBOL(netif_skb_features); |
58e998c6 | 3458 | |
2ea25513 | 3459 | static int xmit_one(struct sk_buff *skb, struct net_device *dev, |
95f6b3dd | 3460 | struct netdev_queue *txq, bool more) |
f6a78bfc | 3461 | { |
2ea25513 DM |
3462 | unsigned int len; |
3463 | int rc; | |
00829823 | 3464 | |
9f9a742d | 3465 | if (dev_nit_active(dev)) |
2ea25513 | 3466 | dev_queue_xmit_nit(skb, dev); |
fc741216 | 3467 | |
2ea25513 DM |
3468 | len = skb->len; |
3469 | trace_net_dev_start_xmit(skb, dev); | |
95f6b3dd | 3470 | rc = netdev_start_xmit(skb, dev, txq, more); |
2ea25513 | 3471 | trace_net_dev_xmit(skb, rc, dev, len); |
adf30907 | 3472 | |
2ea25513 DM |
3473 | return rc; |
3474 | } | |
7b9c6090 | 3475 | |
8dcda22a DM |
3476 | struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev, |
3477 | struct netdev_queue *txq, int *ret) | |
7f2e870f DM |
3478 | { |
3479 | struct sk_buff *skb = first; | |
3480 | int rc = NETDEV_TX_OK; | |
7b9c6090 | 3481 | |
7f2e870f DM |
3482 | while (skb) { |
3483 | struct sk_buff *next = skb->next; | |
fc70fb64 | 3484 | |
a8305bff | 3485 | skb_mark_not_on_list(skb); |
95f6b3dd | 3486 | rc = xmit_one(skb, dev, txq, next != NULL); |
7f2e870f DM |
3487 | if (unlikely(!dev_xmit_complete(rc))) { |
3488 | skb->next = next; | |
3489 | goto out; | |
3490 | } | |
6afff0ca | 3491 | |
7f2e870f | 3492 | skb = next; |
fe60faa5 | 3493 | if (netif_tx_queue_stopped(txq) && skb) { |
7f2e870f DM |
3494 | rc = NETDEV_TX_BUSY; |
3495 | break; | |
9ccb8975 | 3496 | } |
7f2e870f | 3497 | } |
9ccb8975 | 3498 | |
7f2e870f DM |
3499 | out: |
3500 | *ret = rc; | |
3501 | return skb; | |
3502 | } | |
b40863c6 | 3503 | |
1ff0dc94 ED |
3504 | static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb, |
3505 | netdev_features_t features) | |
f6a78bfc | 3506 | { |
df8a39de | 3507 | if (skb_vlan_tag_present(skb) && |
5968250c JP |
3508 | !vlan_hw_offload_capable(features, skb->vlan_proto)) |
3509 | skb = __vlan_hwaccel_push_inside(skb); | |
eae3f88e DM |
3510 | return skb; |
3511 | } | |
f6a78bfc | 3512 | |
43c26a1a DC |
3513 | int skb_csum_hwoffload_help(struct sk_buff *skb, |
3514 | const netdev_features_t features) | |
3515 | { | |
3516 | if (unlikely(skb->csum_not_inet)) | |
3517 | return !!(features & NETIF_F_SCTP_CRC) ? 0 : | |
3518 | skb_crc32c_csum_help(skb); | |
3519 | ||
3520 | return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb); | |
3521 | } | |
3522 | EXPORT_SYMBOL(skb_csum_hwoffload_help); | |
3523 | ||
f53c7239 | 3524 | static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again) |
eae3f88e DM |
3525 | { |
3526 | netdev_features_t features; | |
f6a78bfc | 3527 | |
eae3f88e DM |
3528 | features = netif_skb_features(skb); |
3529 | skb = validate_xmit_vlan(skb, features); | |
3530 | if (unlikely(!skb)) | |
3531 | goto out_null; | |
7b9c6090 | 3532 | |
ebf4e808 IL |
3533 | skb = sk_validate_xmit_skb(skb, dev); |
3534 | if (unlikely(!skb)) | |
3535 | goto out_null; | |
3536 | ||
8b86a61d | 3537 | if (netif_needs_gso(skb, features)) { |
ce93718f DM |
3538 | struct sk_buff *segs; |
3539 | ||
3540 | segs = skb_gso_segment(skb, features); | |
cecda693 | 3541 | if (IS_ERR(segs)) { |
af6dabc9 | 3542 | goto out_kfree_skb; |
cecda693 JW |
3543 | } else if (segs) { |
3544 | consume_skb(skb); | |
3545 | skb = segs; | |
f6a78bfc | 3546 | } |
eae3f88e DM |
3547 | } else { |
3548 | if (skb_needs_linearize(skb, features) && | |
3549 | __skb_linearize(skb)) | |
3550 | goto out_kfree_skb; | |
4ec93edb | 3551 | |
eae3f88e DM |
3552 | /* If packet is not checksummed and device does not |
3553 | * support checksumming for this protocol, complete | |
3554 | * checksumming here. | |
3555 | */ | |
3556 | if (skb->ip_summed == CHECKSUM_PARTIAL) { | |
3557 | if (skb->encapsulation) | |
3558 | skb_set_inner_transport_header(skb, | |
3559 | skb_checksum_start_offset(skb)); | |
3560 | else | |
3561 | skb_set_transport_header(skb, | |
3562 | skb_checksum_start_offset(skb)); | |
43c26a1a | 3563 | if (skb_csum_hwoffload_help(skb, features)) |
eae3f88e | 3564 | goto out_kfree_skb; |
7b9c6090 | 3565 | } |
0c772159 | 3566 | } |
7b9c6090 | 3567 | |
f53c7239 | 3568 | skb = validate_xmit_xfrm(skb, features, again); |
3dca3f38 | 3569 | |
eae3f88e | 3570 | return skb; |
fc70fb64 | 3571 | |
f6a78bfc HX |
3572 | out_kfree_skb: |
3573 | kfree_skb(skb); | |
eae3f88e | 3574 | out_null: |
d21fd63e | 3575 | atomic_long_inc(&dev->tx_dropped); |
eae3f88e DM |
3576 | return NULL; |
3577 | } | |
6afff0ca | 3578 | |
f53c7239 | 3579 | struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again) |
55a93b3e ED |
3580 | { |
3581 | struct sk_buff *next, *head = NULL, *tail; | |
3582 | ||
bec3cfdc | 3583 | for (; skb != NULL; skb = next) { |
55a93b3e | 3584 | next = skb->next; |
a8305bff | 3585 | skb_mark_not_on_list(skb); |
bec3cfdc ED |
3586 | |
3587 | /* in case skb wont be segmented, point to itself */ | |
3588 | skb->prev = skb; | |
3589 | ||
f53c7239 | 3590 | skb = validate_xmit_skb(skb, dev, again); |
bec3cfdc ED |
3591 | if (!skb) |
3592 | continue; | |
55a93b3e | 3593 | |
bec3cfdc ED |
3594 | if (!head) |
3595 | head = skb; | |
3596 | else | |
3597 | tail->next = skb; | |
3598 | /* If skb was segmented, skb->prev points to | |
3599 | * the last segment. If not, it still contains skb. | |
3600 | */ | |
3601 | tail = skb->prev; | |
55a93b3e ED |
3602 | } |
3603 | return head; | |
f6a78bfc | 3604 | } |
104ba78c | 3605 | EXPORT_SYMBOL_GPL(validate_xmit_skb_list); |
f6a78bfc | 3606 | |
1def9238 ED |
3607 | static void qdisc_pkt_len_init(struct sk_buff *skb) |
3608 | { | |
3609 | const struct skb_shared_info *shinfo = skb_shinfo(skb); | |
3610 | ||
3611 | qdisc_skb_cb(skb)->pkt_len = skb->len; | |
3612 | ||
3613 | /* To get more precise estimation of bytes sent on wire, | |
3614 | * we add to pkt_len the headers size of all segments | |
3615 | */ | |
a0dce875 | 3616 | if (shinfo->gso_size && skb_transport_header_was_set(skb)) { |
757b8b1d | 3617 | unsigned int hdr_len; |
15e5a030 | 3618 | u16 gso_segs = shinfo->gso_segs; |
1def9238 | 3619 | |
757b8b1d ED |
3620 | /* mac layer + network layer */ |
3621 | hdr_len = skb_transport_header(skb) - skb_mac_header(skb); | |
3622 | ||
3623 | /* + transport layer */ | |
7c68d1a6 ED |
3624 | if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) { |
3625 | const struct tcphdr *th; | |
3626 | struct tcphdr _tcphdr; | |
3627 | ||
3628 | th = skb_header_pointer(skb, skb_transport_offset(skb), | |
3629 | sizeof(_tcphdr), &_tcphdr); | |
3630 | if (likely(th)) | |
3631 | hdr_len += __tcp_hdrlen(th); | |
3632 | } else { | |
3633 | struct udphdr _udphdr; | |
3634 | ||
3635 | if (skb_header_pointer(skb, skb_transport_offset(skb), | |
3636 | sizeof(_udphdr), &_udphdr)) | |
3637 | hdr_len += sizeof(struct udphdr); | |
3638 | } | |
15e5a030 JW |
3639 | |
3640 | if (shinfo->gso_type & SKB_GSO_DODGY) | |
3641 | gso_segs = DIV_ROUND_UP(skb->len - hdr_len, | |
3642 | shinfo->gso_size); | |
3643 | ||
3644 | qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len; | |
1def9238 ED |
3645 | } |
3646 | } | |
3647 | ||
bbd8a0d3 KK |
3648 | static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q, |
3649 | struct net_device *dev, | |
3650 | struct netdev_queue *txq) | |
3651 | { | |
3652 | spinlock_t *root_lock = qdisc_lock(q); | |
520ac30f | 3653 | struct sk_buff *to_free = NULL; |
a2da570d | 3654 | bool contended; |
bbd8a0d3 KK |
3655 | int rc; |
3656 | ||
a2da570d | 3657 | qdisc_calculate_pkt_len(skb, q); |
6b3ba914 JF |
3658 | |
3659 | if (q->flags & TCQ_F_NOLOCK) { | |
90b2be27 | 3660 | if ((q->flags & TCQ_F_CAN_BYPASS) && READ_ONCE(q->empty) && |
d518d2ed PA |
3661 | qdisc_run_begin(q)) { |
3662 | if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, | |
3663 | &q->state))) { | |
3664 | __qdisc_drop(skb, &to_free); | |
3665 | rc = NET_XMIT_DROP; | |
3666 | goto end_run; | |
3667 | } | |
ba27b4cd PA |
3668 | qdisc_bstats_cpu_update(q, skb); |
3669 | ||
d518d2ed | 3670 | rc = NET_XMIT_SUCCESS; |
ba27b4cd PA |
3671 | if (sch_direct_xmit(skb, q, dev, txq, NULL, true)) |
3672 | __qdisc_run(q); | |
3673 | ||
d518d2ed | 3674 | end_run: |
ba27b4cd | 3675 | qdisc_run_end(q); |
6b3ba914 JF |
3676 | } else { |
3677 | rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK; | |
32f7b44d | 3678 | qdisc_run(q); |
6b3ba914 JF |
3679 | } |
3680 | ||
3681 | if (unlikely(to_free)) | |
3682 | kfree_skb_list(to_free); | |
3683 | return rc; | |
3684 | } | |
3685 | ||
79640a4c ED |
3686 | /* |
3687 | * Heuristic to force contended enqueues to serialize on a | |
3688 | * separate lock before trying to get qdisc main lock. | |
f9eb8aea | 3689 | * This permits qdisc->running owner to get the lock more |
9bf2b8c2 | 3690 | * often and dequeue packets faster. |
79640a4c | 3691 | */ |
a2da570d | 3692 | contended = qdisc_is_running(q); |
79640a4c ED |
3693 | if (unlikely(contended)) |
3694 | spin_lock(&q->busylock); | |
3695 | ||
bbd8a0d3 KK |
3696 | spin_lock(root_lock); |
3697 | if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) { | |
520ac30f | 3698 | __qdisc_drop(skb, &to_free); |
bbd8a0d3 KK |
3699 | rc = NET_XMIT_DROP; |
3700 | } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) && | |
bc135b23 | 3701 | qdisc_run_begin(q)) { |
bbd8a0d3 KK |
3702 | /* |
3703 | * This is a work-conserving queue; there are no old skbs | |
3704 | * waiting to be sent out; and the qdisc is not running - | |
3705 | * xmit the skb directly. | |
3706 | */ | |
bfe0d029 | 3707 | |
bfe0d029 ED |
3708 | qdisc_bstats_update(q, skb); |
3709 | ||
55a93b3e | 3710 | if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) { |
79640a4c ED |
3711 | if (unlikely(contended)) { |
3712 | spin_unlock(&q->busylock); | |
3713 | contended = false; | |
3714 | } | |
bbd8a0d3 | 3715 | __qdisc_run(q); |
6c148184 | 3716 | } |
bbd8a0d3 | 3717 | |
6c148184 | 3718 | qdisc_run_end(q); |
bbd8a0d3 KK |
3719 | rc = NET_XMIT_SUCCESS; |
3720 | } else { | |
520ac30f | 3721 | rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK; |
79640a4c ED |
3722 | if (qdisc_run_begin(q)) { |
3723 | if (unlikely(contended)) { | |
3724 | spin_unlock(&q->busylock); | |
3725 | contended = false; | |
3726 | } | |
3727 | __qdisc_run(q); | |
6c148184 | 3728 | qdisc_run_end(q); |
79640a4c | 3729 | } |
bbd8a0d3 KK |
3730 | } |
3731 | spin_unlock(root_lock); | |
520ac30f ED |
3732 | if (unlikely(to_free)) |
3733 | kfree_skb_list(to_free); | |
79640a4c ED |
3734 | if (unlikely(contended)) |
3735 | spin_unlock(&q->busylock); | |
bbd8a0d3 KK |
3736 | return rc; |
3737 | } | |
3738 | ||
86f8515f | 3739 | #if IS_ENABLED(CONFIG_CGROUP_NET_PRIO) |
5bc1421e NH |
3740 | static void skb_update_prio(struct sk_buff *skb) |
3741 | { | |
4dcb31d4 ED |
3742 | const struct netprio_map *map; |
3743 | const struct sock *sk; | |
3744 | unsigned int prioidx; | |
5bc1421e | 3745 | |
4dcb31d4 ED |
3746 | if (skb->priority) |
3747 | return; | |
3748 | map = rcu_dereference_bh(skb->dev->priomap); | |
3749 | if (!map) | |
3750 | return; | |
3751 | sk = skb_to_full_sk(skb); | |
3752 | if (!sk) | |
3753 | return; | |
91c68ce2 | 3754 | |
4dcb31d4 ED |
3755 | prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data); |
3756 | ||
3757 | if (prioidx < map->priomap_len) | |
3758 | skb->priority = map->priomap[prioidx]; | |
5bc1421e NH |
3759 | } |
3760 | #else | |
3761 | #define skb_update_prio(skb) | |
3762 | #endif | |
3763 | ||
95603e22 MM |
3764 | /** |
3765 | * dev_loopback_xmit - loop back @skb | |
0c4b51f0 EB |
3766 | * @net: network namespace this loopback is happening in |
3767 | * @sk: sk needed to be a netfilter okfn | |
95603e22 MM |
3768 | * @skb: buffer to transmit |
3769 | */ | |
0c4b51f0 | 3770 | int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb) |
95603e22 MM |
3771 | { |
3772 | skb_reset_mac_header(skb); | |
3773 | __skb_pull(skb, skb_network_offset(skb)); | |
3774 | skb->pkt_type = PACKET_LOOPBACK; | |
3775 | skb->ip_summed = CHECKSUM_UNNECESSARY; | |
3776 | WARN_ON(!skb_dst(skb)); | |
3777 | skb_dst_force(skb); | |
3778 | netif_rx_ni(skb); | |
3779 | return 0; | |
3780 | } | |
3781 | EXPORT_SYMBOL(dev_loopback_xmit); | |
3782 | ||
1f211a1b DB |
3783 | #ifdef CONFIG_NET_EGRESS |
3784 | static struct sk_buff * | |
3785 | sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev) | |
3786 | { | |
46209401 | 3787 | struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress); |
1f211a1b DB |
3788 | struct tcf_result cl_res; |
3789 | ||
46209401 | 3790 | if (!miniq) |
1f211a1b DB |
3791 | return skb; |
3792 | ||
8dc07fdb | 3793 | /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */ |
46209401 | 3794 | mini_qdisc_bstats_cpu_update(miniq, skb); |
1f211a1b | 3795 | |
46209401 | 3796 | switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) { |
1f211a1b DB |
3797 | case TC_ACT_OK: |
3798 | case TC_ACT_RECLASSIFY: | |
3799 | skb->tc_index = TC_H_MIN(cl_res.classid); | |
3800 | break; | |
3801 | case TC_ACT_SHOT: | |
46209401 | 3802 | mini_qdisc_qstats_cpu_drop(miniq); |
1f211a1b | 3803 | *ret = NET_XMIT_DROP; |
7e2c3aea DB |
3804 | kfree_skb(skb); |
3805 | return NULL; | |
1f211a1b DB |
3806 | case TC_ACT_STOLEN: |
3807 | case TC_ACT_QUEUED: | |
e25ea21f | 3808 | case TC_ACT_TRAP: |
1f211a1b | 3809 | *ret = NET_XMIT_SUCCESS; |
7e2c3aea | 3810 | consume_skb(skb); |
1f211a1b DB |
3811 | return NULL; |
3812 | case TC_ACT_REDIRECT: | |
3813 | /* No need to push/pop skb's mac_header here on egress! */ | |
3814 | skb_do_redirect(skb); | |
3815 | *ret = NET_XMIT_SUCCESS; | |
3816 | return NULL; | |
3817 | default: | |
3818 | break; | |
3819 | } | |
3820 | ||
3821 | return skb; | |
3822 | } | |
3823 | #endif /* CONFIG_NET_EGRESS */ | |
3824 | ||
fc9bab24 AN |
3825 | #ifdef CONFIG_XPS |
3826 | static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb, | |
3827 | struct xps_dev_maps *dev_maps, unsigned int tci) | |
3828 | { | |
3829 | struct xps_map *map; | |
3830 | int queue_index = -1; | |
3831 | ||
3832 | if (dev->num_tc) { | |
3833 | tci *= dev->num_tc; | |
3834 | tci += netdev_get_prio_tc_map(dev, skb->priority); | |
3835 | } | |
3836 | ||
3837 | map = rcu_dereference(dev_maps->attr_map[tci]); | |
3838 | if (map) { | |
3839 | if (map->len == 1) | |
3840 | queue_index = map->queues[0]; | |
3841 | else | |
3842 | queue_index = map->queues[reciprocal_scale( | |
3843 | skb_get_hash(skb), map->len)]; | |
3844 | if (unlikely(queue_index >= dev->real_num_tx_queues)) | |
3845 | queue_index = -1; | |
3846 | } | |
3847 | return queue_index; | |
3848 | } | |
3849 | #endif | |
3850 | ||
eadec877 AD |
3851 | static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev, |
3852 | struct sk_buff *skb) | |
638b2a69 JP |
3853 | { |
3854 | #ifdef CONFIG_XPS | |
3855 | struct xps_dev_maps *dev_maps; | |
fc9bab24 | 3856 | struct sock *sk = skb->sk; |
638b2a69 JP |
3857 | int queue_index = -1; |
3858 | ||
04157469 AN |
3859 | if (!static_key_false(&xps_needed)) |
3860 | return -1; | |
3861 | ||
638b2a69 | 3862 | rcu_read_lock(); |
fc9bab24 AN |
3863 | if (!static_key_false(&xps_rxqs_needed)) |
3864 | goto get_cpus_map; | |
3865 | ||
eadec877 | 3866 | dev_maps = rcu_dereference(sb_dev->xps_rxqs_map); |
638b2a69 | 3867 | if (dev_maps) { |
fc9bab24 | 3868 | int tci = sk_rx_queue_get(sk); |
184c449f | 3869 | |
fc9bab24 AN |
3870 | if (tci >= 0 && tci < dev->num_rx_queues) |
3871 | queue_index = __get_xps_queue_idx(dev, skb, dev_maps, | |
3872 | tci); | |
3873 | } | |
184c449f | 3874 | |
fc9bab24 AN |
3875 | get_cpus_map: |
3876 | if (queue_index < 0) { | |
eadec877 | 3877 | dev_maps = rcu_dereference(sb_dev->xps_cpus_map); |
fc9bab24 AN |
3878 | if (dev_maps) { |
3879 | unsigned int tci = skb->sender_cpu - 1; | |
3880 | ||
3881 | queue_index = __get_xps_queue_idx(dev, skb, dev_maps, | |
3882 | tci); | |
638b2a69 JP |
3883 | } |
3884 | } | |
3885 | rcu_read_unlock(); | |
3886 | ||
3887 | return queue_index; | |
3888 | #else | |
3889 | return -1; | |
3890 | #endif | |
3891 | } | |
3892 | ||
a4ea8a3d | 3893 | u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb, |
a350ecce | 3894 | struct net_device *sb_dev) |
a4ea8a3d AD |
3895 | { |
3896 | return 0; | |
3897 | } | |
3898 | EXPORT_SYMBOL(dev_pick_tx_zero); | |
3899 | ||
3900 | u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb, | |
a350ecce | 3901 | struct net_device *sb_dev) |
a4ea8a3d AD |
3902 | { |
3903 | return (u16)raw_smp_processor_id() % dev->real_num_tx_queues; | |
3904 | } | |
3905 | EXPORT_SYMBOL(dev_pick_tx_cpu_id); | |
3906 | ||
b71b5837 PA |
3907 | u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb, |
3908 | struct net_device *sb_dev) | |
638b2a69 JP |
3909 | { |
3910 | struct sock *sk = skb->sk; | |
3911 | int queue_index = sk_tx_queue_get(sk); | |
3912 | ||
eadec877 AD |
3913 | sb_dev = sb_dev ? : dev; |
3914 | ||
638b2a69 JP |
3915 | if (queue_index < 0 || skb->ooo_okay || |
3916 | queue_index >= dev->real_num_tx_queues) { | |
eadec877 | 3917 | int new_index = get_xps_queue(dev, sb_dev, skb); |
f4563a75 | 3918 | |
638b2a69 | 3919 | if (new_index < 0) |
eadec877 | 3920 | new_index = skb_tx_hash(dev, sb_dev, skb); |
638b2a69 JP |
3921 | |
3922 | if (queue_index != new_index && sk && | |
004a5d01 | 3923 | sk_fullsock(sk) && |
638b2a69 JP |
3924 | rcu_access_pointer(sk->sk_dst_cache)) |
3925 | sk_tx_queue_set(sk, new_index); | |
3926 | ||
3927 | queue_index = new_index; | |
3928 | } | |
3929 | ||
3930 | return queue_index; | |
3931 | } | |
b71b5837 | 3932 | EXPORT_SYMBOL(netdev_pick_tx); |
638b2a69 | 3933 | |
4bd97d51 PA |
3934 | struct netdev_queue *netdev_core_pick_tx(struct net_device *dev, |
3935 | struct sk_buff *skb, | |
3936 | struct net_device *sb_dev) | |
638b2a69 JP |
3937 | { |
3938 | int queue_index = 0; | |
3939 | ||
3940 | #ifdef CONFIG_XPS | |
52bd2d62 ED |
3941 | u32 sender_cpu = skb->sender_cpu - 1; |
3942 | ||
3943 | if (sender_cpu >= (u32)NR_CPUS) | |
638b2a69 JP |
3944 | skb->sender_cpu = raw_smp_processor_id() + 1; |
3945 | #endif | |
3946 | ||
3947 | if (dev->real_num_tx_queues != 1) { | |
3948 | const struct net_device_ops *ops = dev->netdev_ops; | |
f4563a75 | 3949 | |
638b2a69 | 3950 | if (ops->ndo_select_queue) |
a350ecce | 3951 | queue_index = ops->ndo_select_queue(dev, skb, sb_dev); |
638b2a69 | 3952 | else |
4bd97d51 | 3953 | queue_index = netdev_pick_tx(dev, skb, sb_dev); |
638b2a69 | 3954 | |
d584527c | 3955 | queue_index = netdev_cap_txqueue(dev, queue_index); |
638b2a69 JP |
3956 | } |
3957 | ||
3958 | skb_set_queue_mapping(skb, queue_index); | |
3959 | return netdev_get_tx_queue(dev, queue_index); | |
3960 | } | |
3961 | ||
d29f749e | 3962 | /** |
9d08dd3d | 3963 | * __dev_queue_xmit - transmit a buffer |
d29f749e | 3964 | * @skb: buffer to transmit |
eadec877 | 3965 | * @sb_dev: suboordinate device used for L2 forwarding offload |
d29f749e DJ |
3966 | * |
3967 | * Queue a buffer for transmission to a network device. The caller must | |
3968 | * have set the device and priority and built the buffer before calling | |
3969 | * this function. The function can be called from an interrupt. | |
3970 | * | |
3971 | * A negative errno code is returned on a failure. A success does not | |
3972 | * guarantee the frame will be transmitted as it may be dropped due | |
3973 | * to congestion or traffic shaping. | |
3974 | * | |
3975 | * ----------------------------------------------------------------------------------- | |
3976 | * I notice this method can also return errors from the queue disciplines, | |
3977 | * including NET_XMIT_DROP, which is a positive value. So, errors can also | |
3978 | * be positive. | |
3979 | * | |
3980 | * Regardless of the return value, the skb is consumed, so it is currently | |
3981 | * difficult to retry a send to this method. (You can bump the ref count | |
3982 | * before sending to hold a reference for retry if you are careful.) | |
3983 | * | |
3984 | * When calling this method, interrupts MUST be enabled. This is because | |
3985 | * the BH enable code must have IRQs enabled so that it will not deadlock. | |
3986 | * --BLG | |
3987 | */ | |
eadec877 | 3988 | static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev) |
1da177e4 LT |
3989 | { |
3990 | struct net_device *dev = skb->dev; | |
dc2b4847 | 3991 | struct netdev_queue *txq; |
1da177e4 LT |
3992 | struct Qdisc *q; |
3993 | int rc = -ENOMEM; | |
f53c7239 | 3994 | bool again = false; |
1da177e4 | 3995 | |
6d1ccff6 ED |
3996 | skb_reset_mac_header(skb); |
3997 | ||
e7fd2885 WB |
3998 | if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP)) |
3999 | __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED); | |
4000 | ||
4ec93edb YH |
4001 | /* Disable soft irqs for various locks below. Also |
4002 | * stops preemption for RCU. | |
1da177e4 | 4003 | */ |
4ec93edb | 4004 | rcu_read_lock_bh(); |
1da177e4 | 4005 | |
5bc1421e NH |
4006 | skb_update_prio(skb); |
4007 | ||
1f211a1b DB |
4008 | qdisc_pkt_len_init(skb); |
4009 | #ifdef CONFIG_NET_CLS_ACT | |
8dc07fdb | 4010 | skb->tc_at_ingress = 0; |
1f211a1b | 4011 | # ifdef CONFIG_NET_EGRESS |
aabf6772 | 4012 | if (static_branch_unlikely(&egress_needed_key)) { |
1f211a1b DB |
4013 | skb = sch_handle_egress(skb, &rc, dev); |
4014 | if (!skb) | |
4015 | goto out; | |
4016 | } | |
4017 | # endif | |
4018 | #endif | |
02875878 ED |
4019 | /* If device/qdisc don't need skb->dst, release it right now while |
4020 | * its hot in this cpu cache. | |
4021 | */ | |
4022 | if (dev->priv_flags & IFF_XMIT_DST_RELEASE) | |
4023 | skb_dst_drop(skb); | |
4024 | else | |
4025 | skb_dst_force(skb); | |
4026 | ||
4bd97d51 | 4027 | txq = netdev_core_pick_tx(dev, skb, sb_dev); |
a898def2 | 4028 | q = rcu_dereference_bh(txq->qdisc); |
37437bb2 | 4029 | |
cf66ba58 | 4030 | trace_net_dev_queue(skb); |
1da177e4 | 4031 | if (q->enqueue) { |
bbd8a0d3 | 4032 | rc = __dev_xmit_skb(skb, q, dev, txq); |
37437bb2 | 4033 | goto out; |
1da177e4 LT |
4034 | } |
4035 | ||
4036 | /* The device has no queue. Common case for software devices: | |
eb13da1a | 4037 | * loopback, all the sorts of tunnels... |
1da177e4 | 4038 | |
eb13da1a | 4039 | * Really, it is unlikely that netif_tx_lock protection is necessary |
4040 | * here. (f.e. loopback and IP tunnels are clean ignoring statistics | |
4041 | * counters.) | |
4042 | * However, it is possible, that they rely on protection | |
4043 | * made by us here. | |
1da177e4 | 4044 | |
eb13da1a | 4045 | * Check this and shot the lock. It is not prone from deadlocks. |
4046 | *Either shot noqueue qdisc, it is even simpler 8) | |
1da177e4 LT |
4047 | */ |
4048 | if (dev->flags & IFF_UP) { | |
4049 | int cpu = smp_processor_id(); /* ok because BHs are off */ | |
4050 | ||
c773e847 | 4051 | if (txq->xmit_lock_owner != cpu) { |
97cdcf37 | 4052 | if (dev_xmit_recursion()) |
745e20f1 ED |
4053 | goto recursion_alert; |
4054 | ||
f53c7239 | 4055 | skb = validate_xmit_skb(skb, dev, &again); |
1f59533f | 4056 | if (!skb) |
d21fd63e | 4057 | goto out; |
1f59533f | 4058 | |
c773e847 | 4059 | HARD_TX_LOCK(dev, txq, cpu); |
1da177e4 | 4060 | |
73466498 | 4061 | if (!netif_xmit_stopped(txq)) { |
97cdcf37 | 4062 | dev_xmit_recursion_inc(); |
ce93718f | 4063 | skb = dev_hard_start_xmit(skb, dev, txq, &rc); |
97cdcf37 | 4064 | dev_xmit_recursion_dec(); |
572a9d7b | 4065 | if (dev_xmit_complete(rc)) { |
c773e847 | 4066 | HARD_TX_UNLOCK(dev, txq); |
1da177e4 LT |
4067 | goto out; |
4068 | } | |
4069 | } | |
c773e847 | 4070 | HARD_TX_UNLOCK(dev, txq); |
e87cc472 JP |
4071 | net_crit_ratelimited("Virtual device %s asks to queue packet!\n", |
4072 | dev->name); | |
1da177e4 LT |
4073 | } else { |
4074 | /* Recursion is detected! It is possible, | |
745e20f1 ED |
4075 | * unfortunately |
4076 | */ | |
4077 | recursion_alert: | |
e87cc472 JP |
4078 | net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n", |
4079 | dev->name); | |
1da177e4 LT |
4080 | } |
4081 | } | |
4082 | ||
4083 | rc = -ENETDOWN; | |
d4828d85 | 4084 | rcu_read_unlock_bh(); |
1da177e4 | 4085 | |
015f0688 | 4086 | atomic_long_inc(&dev->tx_dropped); |
1f59533f | 4087 | kfree_skb_list(skb); |
1da177e4 LT |
4088 | return rc; |
4089 | out: | |
d4828d85 | 4090 | rcu_read_unlock_bh(); |
1da177e4 LT |
4091 | return rc; |
4092 | } | |
f663dd9a | 4093 | |
2b4aa3ce | 4094 | int dev_queue_xmit(struct sk_buff *skb) |
f663dd9a JW |
4095 | { |
4096 | return __dev_queue_xmit(skb, NULL); | |
4097 | } | |
2b4aa3ce | 4098 | EXPORT_SYMBOL(dev_queue_xmit); |
1da177e4 | 4099 | |
eadec877 | 4100 | int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev) |
f663dd9a | 4101 | { |
eadec877 | 4102 | return __dev_queue_xmit(skb, sb_dev); |
f663dd9a JW |
4103 | } |
4104 | EXPORT_SYMBOL(dev_queue_xmit_accel); | |
4105 | ||
865b03f2 MK |
4106 | int dev_direct_xmit(struct sk_buff *skb, u16 queue_id) |
4107 | { | |
4108 | struct net_device *dev = skb->dev; | |
4109 | struct sk_buff *orig_skb = skb; | |
4110 | struct netdev_queue *txq; | |
4111 | int ret = NETDEV_TX_BUSY; | |
4112 | bool again = false; | |
4113 | ||
4114 | if (unlikely(!netif_running(dev) || | |
4115 | !netif_carrier_ok(dev))) | |
4116 | goto drop; | |
4117 | ||
4118 | skb = validate_xmit_skb_list(skb, dev, &again); | |
4119 | if (skb != orig_skb) | |
4120 | goto drop; | |
4121 | ||
4122 | skb_set_queue_mapping(skb, queue_id); | |
4123 | txq = skb_get_tx_queue(dev, skb); | |
4124 | ||
4125 | local_bh_disable(); | |
4126 | ||
4127 | HARD_TX_LOCK(dev, txq, smp_processor_id()); | |
4128 | if (!netif_xmit_frozen_or_drv_stopped(txq)) | |
4129 | ret = netdev_start_xmit(skb, dev, txq, false); | |
4130 | HARD_TX_UNLOCK(dev, txq); | |
4131 | ||
4132 | local_bh_enable(); | |
4133 | ||
4134 | if (!dev_xmit_complete(ret)) | |
4135 | kfree_skb(skb); | |
4136 | ||
4137 | return ret; | |
4138 | drop: | |
4139 | atomic_long_inc(&dev->tx_dropped); | |
4140 | kfree_skb_list(skb); | |
4141 | return NET_XMIT_DROP; | |
4142 | } | |
4143 | EXPORT_SYMBOL(dev_direct_xmit); | |
1da177e4 | 4144 | |
eb13da1a | 4145 | /************************************************************************* |
4146 | * Receiver routines | |
4147 | *************************************************************************/ | |
1da177e4 | 4148 | |
6b2bedc3 | 4149 | int netdev_max_backlog __read_mostly = 1000; |
c9e6bc64 ED |
4150 | EXPORT_SYMBOL(netdev_max_backlog); |
4151 | ||
3b098e2d | 4152 | int netdev_tstamp_prequeue __read_mostly = 1; |
6b2bedc3 | 4153 | int netdev_budget __read_mostly = 300; |
7acf8a1e | 4154 | unsigned int __read_mostly netdev_budget_usecs = 2000; |
3d48b53f MT |
4155 | int weight_p __read_mostly = 64; /* old backlog weight */ |
4156 | int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */ | |
4157 | int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */ | |
4158 | int dev_rx_weight __read_mostly = 64; | |
4159 | int dev_tx_weight __read_mostly = 64; | |
323ebb61 EC |
4160 | /* Maximum number of GRO_NORMAL skbs to batch up for list-RX */ |
4161 | int gro_normal_batch __read_mostly = 8; | |
1da177e4 | 4162 | |
eecfd7c4 ED |
4163 | /* Called with irq disabled */ |
4164 | static inline void ____napi_schedule(struct softnet_data *sd, | |
4165 | struct napi_struct *napi) | |
4166 | { | |
4167 | list_add_tail(&napi->poll_list, &sd->poll_list); | |
4168 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
4169 | } | |
4170 | ||
bfb564e7 KK |
4171 | #ifdef CONFIG_RPS |
4172 | ||
4173 | /* One global table that all flow-based protocols share. */ | |
6e3f7faf | 4174 | struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly; |
bfb564e7 | 4175 | EXPORT_SYMBOL(rps_sock_flow_table); |
567e4b79 ED |
4176 | u32 rps_cpu_mask __read_mostly; |
4177 | EXPORT_SYMBOL(rps_cpu_mask); | |
bfb564e7 | 4178 | |
dc05360f | 4179 | struct static_key_false rps_needed __read_mostly; |
3df97ba8 | 4180 | EXPORT_SYMBOL(rps_needed); |
dc05360f | 4181 | struct static_key_false rfs_needed __read_mostly; |
13bfff25 | 4182 | EXPORT_SYMBOL(rfs_needed); |
adc9300e | 4183 | |
c445477d BH |
4184 | static struct rps_dev_flow * |
4185 | set_rps_cpu(struct net_device *dev, struct sk_buff *skb, | |
4186 | struct rps_dev_flow *rflow, u16 next_cpu) | |
4187 | { | |
a31196b0 | 4188 | if (next_cpu < nr_cpu_ids) { |
c445477d BH |
4189 | #ifdef CONFIG_RFS_ACCEL |
4190 | struct netdev_rx_queue *rxqueue; | |
4191 | struct rps_dev_flow_table *flow_table; | |
4192 | struct rps_dev_flow *old_rflow; | |
4193 | u32 flow_id; | |
4194 | u16 rxq_index; | |
4195 | int rc; | |
4196 | ||
4197 | /* Should we steer this flow to a different hardware queue? */ | |
69a19ee6 BH |
4198 | if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap || |
4199 | !(dev->features & NETIF_F_NTUPLE)) | |
c445477d BH |
4200 | goto out; |
4201 | rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu); | |
4202 | if (rxq_index == skb_get_rx_queue(skb)) | |
4203 | goto out; | |
4204 | ||
4205 | rxqueue = dev->_rx + rxq_index; | |
4206 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
4207 | if (!flow_table) | |
4208 | goto out; | |
61b905da | 4209 | flow_id = skb_get_hash(skb) & flow_table->mask; |
c445477d BH |
4210 | rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb, |
4211 | rxq_index, flow_id); | |
4212 | if (rc < 0) | |
4213 | goto out; | |
4214 | old_rflow = rflow; | |
4215 | rflow = &flow_table->flows[flow_id]; | |
c445477d BH |
4216 | rflow->filter = rc; |
4217 | if (old_rflow->filter == rflow->filter) | |
4218 | old_rflow->filter = RPS_NO_FILTER; | |
4219 | out: | |
4220 | #endif | |
4221 | rflow->last_qtail = | |
09994d1b | 4222 | per_cpu(softnet_data, next_cpu).input_queue_head; |
c445477d BH |
4223 | } |
4224 | ||
09994d1b | 4225 | rflow->cpu = next_cpu; |
c445477d BH |
4226 | return rflow; |
4227 | } | |
4228 | ||
bfb564e7 KK |
4229 | /* |
4230 | * get_rps_cpu is called from netif_receive_skb and returns the target | |
4231 | * CPU from the RPS map of the receiving queue for a given skb. | |
4232 | * rcu_read_lock must be held on entry. | |
4233 | */ | |
4234 | static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb, | |
4235 | struct rps_dev_flow **rflowp) | |
4236 | { | |
567e4b79 ED |
4237 | const struct rps_sock_flow_table *sock_flow_table; |
4238 | struct netdev_rx_queue *rxqueue = dev->_rx; | |
bfb564e7 | 4239 | struct rps_dev_flow_table *flow_table; |
567e4b79 | 4240 | struct rps_map *map; |
bfb564e7 | 4241 | int cpu = -1; |
567e4b79 | 4242 | u32 tcpu; |
61b905da | 4243 | u32 hash; |
bfb564e7 KK |
4244 | |
4245 | if (skb_rx_queue_recorded(skb)) { | |
4246 | u16 index = skb_get_rx_queue(skb); | |
567e4b79 | 4247 | |
62fe0b40 BH |
4248 | if (unlikely(index >= dev->real_num_rx_queues)) { |
4249 | WARN_ONCE(dev->real_num_rx_queues > 1, | |
4250 | "%s received packet on queue %u, but number " | |
4251 | "of RX queues is %u\n", | |
4252 | dev->name, index, dev->real_num_rx_queues); | |
bfb564e7 KK |
4253 | goto done; |
4254 | } | |
567e4b79 ED |
4255 | rxqueue += index; |
4256 | } | |
bfb564e7 | 4257 | |
567e4b79 ED |
4258 | /* Avoid computing hash if RFS/RPS is not active for this rxqueue */ |
4259 | ||
4260 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
6e3f7faf | 4261 | map = rcu_dereference(rxqueue->rps_map); |
567e4b79 | 4262 | if (!flow_table && !map) |
bfb564e7 KK |
4263 | goto done; |
4264 | ||
2d47b459 | 4265 | skb_reset_network_header(skb); |
61b905da TH |
4266 | hash = skb_get_hash(skb); |
4267 | if (!hash) | |
bfb564e7 KK |
4268 | goto done; |
4269 | ||
fec5e652 TH |
4270 | sock_flow_table = rcu_dereference(rps_sock_flow_table); |
4271 | if (flow_table && sock_flow_table) { | |
fec5e652 | 4272 | struct rps_dev_flow *rflow; |
567e4b79 ED |
4273 | u32 next_cpu; |
4274 | u32 ident; | |
4275 | ||
4276 | /* First check into global flow table if there is a match */ | |
4277 | ident = sock_flow_table->ents[hash & sock_flow_table->mask]; | |
4278 | if ((ident ^ hash) & ~rps_cpu_mask) | |
4279 | goto try_rps; | |
fec5e652 | 4280 | |
567e4b79 ED |
4281 | next_cpu = ident & rps_cpu_mask; |
4282 | ||
4283 | /* OK, now we know there is a match, | |
4284 | * we can look at the local (per receive queue) flow table | |
4285 | */ | |
61b905da | 4286 | rflow = &flow_table->flows[hash & flow_table->mask]; |
fec5e652 TH |
4287 | tcpu = rflow->cpu; |
4288 | ||
fec5e652 TH |
4289 | /* |
4290 | * If the desired CPU (where last recvmsg was done) is | |
4291 | * different from current CPU (one in the rx-queue flow | |
4292 | * table entry), switch if one of the following holds: | |
a31196b0 | 4293 | * - Current CPU is unset (>= nr_cpu_ids). |
fec5e652 TH |
4294 | * - Current CPU is offline. |
4295 | * - The current CPU's queue tail has advanced beyond the | |
4296 | * last packet that was enqueued using this table entry. | |
4297 | * This guarantees that all previous packets for the flow | |
4298 | * have been dequeued, thus preserving in order delivery. | |
4299 | */ | |
4300 | if (unlikely(tcpu != next_cpu) && | |
a31196b0 | 4301 | (tcpu >= nr_cpu_ids || !cpu_online(tcpu) || |
fec5e652 | 4302 | ((int)(per_cpu(softnet_data, tcpu).input_queue_head - |
baefa31d TH |
4303 | rflow->last_qtail)) >= 0)) { |
4304 | tcpu = next_cpu; | |
c445477d | 4305 | rflow = set_rps_cpu(dev, skb, rflow, next_cpu); |
baefa31d | 4306 | } |
c445477d | 4307 | |
a31196b0 | 4308 | if (tcpu < nr_cpu_ids && cpu_online(tcpu)) { |
fec5e652 TH |
4309 | *rflowp = rflow; |
4310 | cpu = tcpu; | |
4311 | goto done; | |
4312 | } | |
4313 | } | |
4314 | ||
567e4b79 ED |
4315 | try_rps: |
4316 | ||
0a9627f2 | 4317 | if (map) { |
8fc54f68 | 4318 | tcpu = map->cpus[reciprocal_scale(hash, map->len)]; |
0a9627f2 TH |
4319 | if (cpu_online(tcpu)) { |
4320 | cpu = tcpu; | |
4321 | goto done; | |
4322 | } | |
4323 | } | |
4324 | ||
4325 | done: | |
0a9627f2 TH |
4326 | return cpu; |
4327 | } | |
4328 | ||
c445477d BH |
4329 | #ifdef CONFIG_RFS_ACCEL |
4330 | ||
4331 | /** | |
4332 | * rps_may_expire_flow - check whether an RFS hardware filter may be removed | |
4333 | * @dev: Device on which the filter was set | |
4334 | * @rxq_index: RX queue index | |
4335 | * @flow_id: Flow ID passed to ndo_rx_flow_steer() | |
4336 | * @filter_id: Filter ID returned by ndo_rx_flow_steer() | |
4337 | * | |
4338 | * Drivers that implement ndo_rx_flow_steer() should periodically call | |
4339 | * this function for each installed filter and remove the filters for | |
4340 | * which it returns %true. | |
4341 | */ | |
4342 | bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index, | |
4343 | u32 flow_id, u16 filter_id) | |
4344 | { | |
4345 | struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index; | |
4346 | struct rps_dev_flow_table *flow_table; | |
4347 | struct rps_dev_flow *rflow; | |
4348 | bool expire = true; | |
a31196b0 | 4349 | unsigned int cpu; |
c445477d BH |
4350 | |
4351 | rcu_read_lock(); | |
4352 | flow_table = rcu_dereference(rxqueue->rps_flow_table); | |
4353 | if (flow_table && flow_id <= flow_table->mask) { | |
4354 | rflow = &flow_table->flows[flow_id]; | |
6aa7de05 | 4355 | cpu = READ_ONCE(rflow->cpu); |
a31196b0 | 4356 | if (rflow->filter == filter_id && cpu < nr_cpu_ids && |
c445477d BH |
4357 | ((int)(per_cpu(softnet_data, cpu).input_queue_head - |
4358 | rflow->last_qtail) < | |
4359 | (int)(10 * flow_table->mask))) | |
4360 | expire = false; | |
4361 | } | |
4362 | rcu_read_unlock(); | |
4363 | return expire; | |
4364 | } | |
4365 | EXPORT_SYMBOL(rps_may_expire_flow); | |
4366 | ||
4367 | #endif /* CONFIG_RFS_ACCEL */ | |
4368 | ||
0a9627f2 | 4369 | /* Called from hardirq (IPI) context */ |
e36fa2f7 | 4370 | static void rps_trigger_softirq(void *data) |
0a9627f2 | 4371 | { |
e36fa2f7 ED |
4372 | struct softnet_data *sd = data; |
4373 | ||
eecfd7c4 | 4374 | ____napi_schedule(sd, &sd->backlog); |
dee42870 | 4375 | sd->received_rps++; |
0a9627f2 | 4376 | } |
e36fa2f7 | 4377 | |
fec5e652 | 4378 | #endif /* CONFIG_RPS */ |
0a9627f2 | 4379 | |
e36fa2f7 ED |
4380 | /* |
4381 | * Check if this softnet_data structure is another cpu one | |
4382 | * If yes, queue it to our IPI list and return 1 | |
4383 | * If no, return 0 | |
4384 | */ | |
4385 | static int rps_ipi_queued(struct softnet_data *sd) | |
4386 | { | |
4387 | #ifdef CONFIG_RPS | |
903ceff7 | 4388 | struct softnet_data *mysd = this_cpu_ptr(&softnet_data); |
e36fa2f7 ED |
4389 | |
4390 | if (sd != mysd) { | |
4391 | sd->rps_ipi_next = mysd->rps_ipi_list; | |
4392 | mysd->rps_ipi_list = sd; | |
4393 | ||
4394 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
4395 | return 1; | |
4396 | } | |
4397 | #endif /* CONFIG_RPS */ | |
4398 | return 0; | |
4399 | } | |
4400 | ||
99bbc707 WB |
4401 | #ifdef CONFIG_NET_FLOW_LIMIT |
4402 | int netdev_flow_limit_table_len __read_mostly = (1 << 12); | |
4403 | #endif | |
4404 | ||
4405 | static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen) | |
4406 | { | |
4407 | #ifdef CONFIG_NET_FLOW_LIMIT | |
4408 | struct sd_flow_limit *fl; | |
4409 | struct softnet_data *sd; | |
4410 | unsigned int old_flow, new_flow; | |
4411 | ||
4412 | if (qlen < (netdev_max_backlog >> 1)) | |
4413 | return false; | |
4414 | ||
903ceff7 | 4415 | sd = this_cpu_ptr(&softnet_data); |
99bbc707 WB |
4416 | |
4417 | rcu_read_lock(); | |
4418 | fl = rcu_dereference(sd->flow_limit); | |
4419 | if (fl) { | |
3958afa1 | 4420 | new_flow = skb_get_hash(skb) & (fl->num_buckets - 1); |
99bbc707 WB |
4421 | old_flow = fl->history[fl->history_head]; |
4422 | fl->history[fl->history_head] = new_flow; | |
4423 | ||
4424 | fl->history_head++; | |
4425 | fl->history_head &= FLOW_LIMIT_HISTORY - 1; | |
4426 | ||
4427 | if (likely(fl->buckets[old_flow])) | |
4428 | fl->buckets[old_flow]--; | |
4429 | ||
4430 | if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) { | |
4431 | fl->count++; | |
4432 | rcu_read_unlock(); | |
4433 | return true; | |
4434 | } | |
4435 | } | |
4436 | rcu_read_unlock(); | |
4437 | #endif | |
4438 | return false; | |
4439 | } | |
4440 | ||
0a9627f2 TH |
4441 | /* |
4442 | * enqueue_to_backlog is called to queue an skb to a per CPU backlog | |
4443 | * queue (may be a remote CPU queue). | |
4444 | */ | |
fec5e652 TH |
4445 | static int enqueue_to_backlog(struct sk_buff *skb, int cpu, |
4446 | unsigned int *qtail) | |
0a9627f2 | 4447 | { |
e36fa2f7 | 4448 | struct softnet_data *sd; |
0a9627f2 | 4449 | unsigned long flags; |
99bbc707 | 4450 | unsigned int qlen; |
0a9627f2 | 4451 | |
e36fa2f7 | 4452 | sd = &per_cpu(softnet_data, cpu); |
0a9627f2 TH |
4453 | |
4454 | local_irq_save(flags); | |
0a9627f2 | 4455 | |
e36fa2f7 | 4456 | rps_lock(sd); |
e9e4dd32 JA |
4457 | if (!netif_running(skb->dev)) |
4458 | goto drop; | |
99bbc707 WB |
4459 | qlen = skb_queue_len(&sd->input_pkt_queue); |
4460 | if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) { | |
e008f3f0 | 4461 | if (qlen) { |
0a9627f2 | 4462 | enqueue: |
e36fa2f7 | 4463 | __skb_queue_tail(&sd->input_pkt_queue, skb); |
76cc8b13 | 4464 | input_queue_tail_incr_save(sd, qtail); |
e36fa2f7 | 4465 | rps_unlock(sd); |
152102c7 | 4466 | local_irq_restore(flags); |
0a9627f2 TH |
4467 | return NET_RX_SUCCESS; |
4468 | } | |
4469 | ||
ebda37c2 ED |
4470 | /* Schedule NAPI for backlog device |
4471 | * We can use non atomic operation since we own the queue lock | |
4472 | */ | |
4473 | if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) { | |
e36fa2f7 | 4474 | if (!rps_ipi_queued(sd)) |
eecfd7c4 | 4475 | ____napi_schedule(sd, &sd->backlog); |
0a9627f2 TH |
4476 | } |
4477 | goto enqueue; | |
4478 | } | |
4479 | ||
e9e4dd32 | 4480 | drop: |
dee42870 | 4481 | sd->dropped++; |
e36fa2f7 | 4482 | rps_unlock(sd); |
0a9627f2 | 4483 | |
0a9627f2 TH |
4484 | local_irq_restore(flags); |
4485 | ||
caf586e5 | 4486 | atomic_long_inc(&skb->dev->rx_dropped); |
0a9627f2 TH |
4487 | kfree_skb(skb); |
4488 | return NET_RX_DROP; | |
4489 | } | |
1da177e4 | 4490 | |
e817f856 JDB |
4491 | static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb) |
4492 | { | |
4493 | struct net_device *dev = skb->dev; | |
4494 | struct netdev_rx_queue *rxqueue; | |
4495 | ||
4496 | rxqueue = dev->_rx; | |
4497 | ||
4498 | if (skb_rx_queue_recorded(skb)) { | |
4499 | u16 index = skb_get_rx_queue(skb); | |
4500 | ||
4501 | if (unlikely(index >= dev->real_num_rx_queues)) { | |
4502 | WARN_ONCE(dev->real_num_rx_queues > 1, | |
4503 | "%s received packet on queue %u, but number " | |
4504 | "of RX queues is %u\n", | |
4505 | dev->name, index, dev->real_num_rx_queues); | |
4506 | ||
4507 | return rxqueue; /* Return first rxqueue */ | |
4508 | } | |
4509 | rxqueue += index; | |
4510 | } | |
4511 | return rxqueue; | |
4512 | } | |
4513 | ||
d4455169 | 4514 | static u32 netif_receive_generic_xdp(struct sk_buff *skb, |
02671e23 | 4515 | struct xdp_buff *xdp, |
d4455169 JF |
4516 | struct bpf_prog *xdp_prog) |
4517 | { | |
e817f856 | 4518 | struct netdev_rx_queue *rxqueue; |
198d83bb | 4519 | void *orig_data, *orig_data_end; |
de8f3a83 | 4520 | u32 metalen, act = XDP_DROP; |
29724956 JDB |
4521 | __be16 orig_eth_type; |
4522 | struct ethhdr *eth; | |
4523 | bool orig_bcast; | |
d4455169 JF |
4524 | int hlen, off; |
4525 | u32 mac_len; | |
4526 | ||
4527 | /* Reinjected packets coming from act_mirred or similar should | |
4528 | * not get XDP generic processing. | |
4529 | */ | |
ad1e03b2 | 4530 | if (skb_is_tc_redirected(skb)) |
d4455169 JF |
4531 | return XDP_PASS; |
4532 | ||
de8f3a83 DB |
4533 | /* XDP packets must be linear and must have sufficient headroom |
4534 | * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also | |
4535 | * native XDP provides, thus we need to do it here as well. | |
4536 | */ | |
ad1e03b2 | 4537 | if (skb_cloned(skb) || skb_is_nonlinear(skb) || |
de8f3a83 DB |
4538 | skb_headroom(skb) < XDP_PACKET_HEADROOM) { |
4539 | int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb); | |
4540 | int troom = skb->tail + skb->data_len - skb->end; | |
4541 | ||
4542 | /* In case we have to go down the path and also linearize, | |
4543 | * then lets do the pskb_expand_head() work just once here. | |
4544 | */ | |
4545 | if (pskb_expand_head(skb, | |
4546 | hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0, | |
4547 | troom > 0 ? troom + 128 : 0, GFP_ATOMIC)) | |
4548 | goto do_drop; | |
2d17d8d7 | 4549 | if (skb_linearize(skb)) |
de8f3a83 DB |
4550 | goto do_drop; |
4551 | } | |
d4455169 JF |
4552 | |
4553 | /* The XDP program wants to see the packet starting at the MAC | |
4554 | * header. | |
4555 | */ | |
4556 | mac_len = skb->data - skb_mac_header(skb); | |
4557 | hlen = skb_headlen(skb) + mac_len; | |
02671e23 BT |
4558 | xdp->data = skb->data - mac_len; |
4559 | xdp->data_meta = xdp->data; | |
4560 | xdp->data_end = xdp->data + hlen; | |
4561 | xdp->data_hard_start = skb->data - skb_headroom(skb); | |
4562 | orig_data_end = xdp->data_end; | |
4563 | orig_data = xdp->data; | |
29724956 JDB |
4564 | eth = (struct ethhdr *)xdp->data; |
4565 | orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest); | |
4566 | orig_eth_type = eth->h_proto; | |
d4455169 | 4567 | |
e817f856 | 4568 | rxqueue = netif_get_rxqueue(skb); |
02671e23 | 4569 | xdp->rxq = &rxqueue->xdp_rxq; |
e817f856 | 4570 | |
02671e23 | 4571 | act = bpf_prog_run_xdp(xdp_prog, xdp); |
d4455169 | 4572 | |
065af355 | 4573 | /* check if bpf_xdp_adjust_head was used */ |
02671e23 | 4574 | off = xdp->data - orig_data; |
065af355 JDB |
4575 | if (off) { |
4576 | if (off > 0) | |
4577 | __skb_pull(skb, off); | |
4578 | else if (off < 0) | |
4579 | __skb_push(skb, -off); | |
4580 | ||
4581 | skb->mac_header += off; | |
4582 | skb_reset_network_header(skb); | |
4583 | } | |
d4455169 | 4584 | |
198d83bb NS |
4585 | /* check if bpf_xdp_adjust_tail was used. it can only "shrink" |
4586 | * pckt. | |
4587 | */ | |
02671e23 | 4588 | off = orig_data_end - xdp->data_end; |
f7613120 | 4589 | if (off != 0) { |
02671e23 | 4590 | skb_set_tail_pointer(skb, xdp->data_end - xdp->data); |
f7613120 | 4591 | skb->len -= off; |
02671e23 | 4592 | |
f7613120 | 4593 | } |
198d83bb | 4594 | |
29724956 JDB |
4595 | /* check if XDP changed eth hdr such SKB needs update */ |
4596 | eth = (struct ethhdr *)xdp->data; | |
4597 | if ((orig_eth_type != eth->h_proto) || | |
4598 | (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) { | |
4599 | __skb_push(skb, ETH_HLEN); | |
4600 | skb->protocol = eth_type_trans(skb, skb->dev); | |
4601 | } | |
4602 | ||
d4455169 | 4603 | switch (act) { |
6103aa96 | 4604 | case XDP_REDIRECT: |
d4455169 JF |
4605 | case XDP_TX: |
4606 | __skb_push(skb, mac_len); | |
de8f3a83 | 4607 | break; |
d4455169 | 4608 | case XDP_PASS: |
02671e23 | 4609 | metalen = xdp->data - xdp->data_meta; |
de8f3a83 DB |
4610 | if (metalen) |
4611 | skb_metadata_set(skb, metalen); | |
d4455169 | 4612 | break; |
d4455169 JF |
4613 | default: |
4614 | bpf_warn_invalid_xdp_action(act); | |
4615 | /* fall through */ | |
4616 | case XDP_ABORTED: | |
4617 | trace_xdp_exception(skb->dev, xdp_prog, act); | |
4618 | /* fall through */ | |
4619 | case XDP_DROP: | |
4620 | do_drop: | |
4621 | kfree_skb(skb); | |
4622 | break; | |
4623 | } | |
4624 | ||
4625 | return act; | |
4626 | } | |
4627 | ||
4628 | /* When doing generic XDP we have to bypass the qdisc layer and the | |
4629 | * network taps in order to match in-driver-XDP behavior. | |
4630 | */ | |
7c497478 | 4631 | void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog) |
d4455169 JF |
4632 | { |
4633 | struct net_device *dev = skb->dev; | |
4634 | struct netdev_queue *txq; | |
4635 | bool free_skb = true; | |
4636 | int cpu, rc; | |
4637 | ||
4bd97d51 | 4638 | txq = netdev_core_pick_tx(dev, skb, NULL); |
d4455169 JF |
4639 | cpu = smp_processor_id(); |
4640 | HARD_TX_LOCK(dev, txq, cpu); | |
4641 | if (!netif_xmit_stopped(txq)) { | |
4642 | rc = netdev_start_xmit(skb, dev, txq, 0); | |
4643 | if (dev_xmit_complete(rc)) | |
4644 | free_skb = false; | |
4645 | } | |
4646 | HARD_TX_UNLOCK(dev, txq); | |
4647 | if (free_skb) { | |
4648 | trace_xdp_exception(dev, xdp_prog, XDP_TX); | |
4649 | kfree_skb(skb); | |
4650 | } | |
4651 | } | |
7c497478 | 4652 | EXPORT_SYMBOL_GPL(generic_xdp_tx); |
d4455169 | 4653 | |
02786475 | 4654 | static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key); |
d4455169 | 4655 | |
7c497478 | 4656 | int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb) |
d4455169 | 4657 | { |
d4455169 | 4658 | if (xdp_prog) { |
02671e23 BT |
4659 | struct xdp_buff xdp; |
4660 | u32 act; | |
6103aa96 | 4661 | int err; |
d4455169 | 4662 | |
02671e23 | 4663 | act = netif_receive_generic_xdp(skb, &xdp, xdp_prog); |
d4455169 | 4664 | if (act != XDP_PASS) { |
6103aa96 JF |
4665 | switch (act) { |
4666 | case XDP_REDIRECT: | |
2facaad6 | 4667 | err = xdp_do_generic_redirect(skb->dev, skb, |
02671e23 | 4668 | &xdp, xdp_prog); |
6103aa96 JF |
4669 | if (err) |
4670 | goto out_redir; | |
02671e23 | 4671 | break; |
6103aa96 | 4672 | case XDP_TX: |
d4455169 | 4673 | generic_xdp_tx(skb, xdp_prog); |
6103aa96 JF |
4674 | break; |
4675 | } | |
d4455169 JF |
4676 | return XDP_DROP; |
4677 | } | |
4678 | } | |
4679 | return XDP_PASS; | |
6103aa96 | 4680 | out_redir: |
6103aa96 JF |
4681 | kfree_skb(skb); |
4682 | return XDP_DROP; | |
d4455169 | 4683 | } |
7c497478 | 4684 | EXPORT_SYMBOL_GPL(do_xdp_generic); |
d4455169 | 4685 | |
ae78dbfa | 4686 | static int netif_rx_internal(struct sk_buff *skb) |
1da177e4 | 4687 | { |
b0e28f1e | 4688 | int ret; |
1da177e4 | 4689 | |
588f0330 | 4690 | net_timestamp_check(netdev_tstamp_prequeue, skb); |
1da177e4 | 4691 | |
cf66ba58 | 4692 | trace_netif_rx(skb); |
d4455169 | 4693 | |
df334545 | 4694 | #ifdef CONFIG_RPS |
dc05360f | 4695 | if (static_branch_unlikely(&rps_needed)) { |
fec5e652 | 4696 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
b0e28f1e ED |
4697 | int cpu; |
4698 | ||
cece1945 | 4699 | preempt_disable(); |
b0e28f1e | 4700 | rcu_read_lock(); |
fec5e652 TH |
4701 | |
4702 | cpu = get_rps_cpu(skb->dev, skb, &rflow); | |
b0e28f1e ED |
4703 | if (cpu < 0) |
4704 | cpu = smp_processor_id(); | |
fec5e652 TH |
4705 | |
4706 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); | |
4707 | ||
b0e28f1e | 4708 | rcu_read_unlock(); |
cece1945 | 4709 | preempt_enable(); |
adc9300e ED |
4710 | } else |
4711 | #endif | |
fec5e652 TH |
4712 | { |
4713 | unsigned int qtail; | |
f4563a75 | 4714 | |
fec5e652 TH |
4715 | ret = enqueue_to_backlog(skb, get_cpu(), &qtail); |
4716 | put_cpu(); | |
4717 | } | |
b0e28f1e | 4718 | return ret; |
1da177e4 | 4719 | } |
ae78dbfa BH |
4720 | |
4721 | /** | |
4722 | * netif_rx - post buffer to the network code | |
4723 | * @skb: buffer to post | |
4724 | * | |
4725 | * This function receives a packet from a device driver and queues it for | |
4726 | * the upper (protocol) levels to process. It always succeeds. The buffer | |
4727 | * may be dropped during processing for congestion control or by the | |
4728 | * protocol layers. | |
4729 | * | |
4730 | * return values: | |
4731 | * NET_RX_SUCCESS (no congestion) | |
4732 | * NET_RX_DROP (packet was dropped) | |
4733 | * | |
4734 | */ | |
4735 | ||
4736 | int netif_rx(struct sk_buff *skb) | |
4737 | { | |
b0e3f1bd GB |
4738 | int ret; |
4739 | ||
ae78dbfa BH |
4740 | trace_netif_rx_entry(skb); |
4741 | ||
b0e3f1bd GB |
4742 | ret = netif_rx_internal(skb); |
4743 | trace_netif_rx_exit(ret); | |
4744 | ||
4745 | return ret; | |
ae78dbfa | 4746 | } |
d1b19dff | 4747 | EXPORT_SYMBOL(netif_rx); |
1da177e4 LT |
4748 | |
4749 | int netif_rx_ni(struct sk_buff *skb) | |
4750 | { | |
4751 | int err; | |
4752 | ||
ae78dbfa BH |
4753 | trace_netif_rx_ni_entry(skb); |
4754 | ||
1da177e4 | 4755 | preempt_disable(); |
ae78dbfa | 4756 | err = netif_rx_internal(skb); |
1da177e4 LT |
4757 | if (local_softirq_pending()) |
4758 | do_softirq(); | |
4759 | preempt_enable(); | |
b0e3f1bd | 4760 | trace_netif_rx_ni_exit(err); |
1da177e4 LT |
4761 | |
4762 | return err; | |
4763 | } | |
1da177e4 LT |
4764 | EXPORT_SYMBOL(netif_rx_ni); |
4765 | ||
0766f788 | 4766 | static __latent_entropy void net_tx_action(struct softirq_action *h) |
1da177e4 | 4767 | { |
903ceff7 | 4768 | struct softnet_data *sd = this_cpu_ptr(&softnet_data); |
1da177e4 LT |
4769 | |
4770 | if (sd->completion_queue) { | |
4771 | struct sk_buff *clist; | |
4772 | ||
4773 | local_irq_disable(); | |
4774 | clist = sd->completion_queue; | |
4775 | sd->completion_queue = NULL; | |
4776 | local_irq_enable(); | |
4777 | ||
4778 | while (clist) { | |
4779 | struct sk_buff *skb = clist; | |
f4563a75 | 4780 | |
1da177e4 LT |
4781 | clist = clist->next; |
4782 | ||
63354797 | 4783 | WARN_ON(refcount_read(&skb->users)); |
e6247027 ED |
4784 | if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED)) |
4785 | trace_consume_skb(skb); | |
4786 | else | |
4787 | trace_kfree_skb(skb, net_tx_action); | |
15fad714 JDB |
4788 | |
4789 | if (skb->fclone != SKB_FCLONE_UNAVAILABLE) | |
4790 | __kfree_skb(skb); | |
4791 | else | |
4792 | __kfree_skb_defer(skb); | |
1da177e4 | 4793 | } |
15fad714 JDB |
4794 | |
4795 | __kfree_skb_flush(); | |
1da177e4 LT |
4796 | } |
4797 | ||
4798 | if (sd->output_queue) { | |
37437bb2 | 4799 | struct Qdisc *head; |
1da177e4 LT |
4800 | |
4801 | local_irq_disable(); | |
4802 | head = sd->output_queue; | |
4803 | sd->output_queue = NULL; | |
a9cbd588 | 4804 | sd->output_queue_tailp = &sd->output_queue; |
1da177e4 LT |
4805 | local_irq_enable(); |
4806 | ||
4807 | while (head) { | |
37437bb2 | 4808 | struct Qdisc *q = head; |
6b3ba914 | 4809 | spinlock_t *root_lock = NULL; |
37437bb2 | 4810 | |
1da177e4 LT |
4811 | head = head->next_sched; |
4812 | ||
6b3ba914 JF |
4813 | if (!(q->flags & TCQ_F_NOLOCK)) { |
4814 | root_lock = qdisc_lock(q); | |
4815 | spin_lock(root_lock); | |
4816 | } | |
3bcb846c ED |
4817 | /* We need to make sure head->next_sched is read |
4818 | * before clearing __QDISC_STATE_SCHED | |
4819 | */ | |
4820 | smp_mb__before_atomic(); | |
4821 | clear_bit(__QDISC_STATE_SCHED, &q->state); | |
4822 | qdisc_run(q); | |
6b3ba914 JF |
4823 | if (root_lock) |
4824 | spin_unlock(root_lock); | |
1da177e4 LT |
4825 | } |
4826 | } | |
f53c7239 SK |
4827 | |
4828 | xfrm_dev_backlog(sd); | |
1da177e4 LT |
4829 | } |
4830 | ||
181402a5 | 4831 | #if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE) |
da678292 MM |
4832 | /* This hook is defined here for ATM LANE */ |
4833 | int (*br_fdb_test_addr_hook)(struct net_device *dev, | |
4834 | unsigned char *addr) __read_mostly; | |
4fb019a0 | 4835 | EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook); |
da678292 | 4836 | #endif |
1da177e4 | 4837 | |
1f211a1b DB |
4838 | static inline struct sk_buff * |
4839 | sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret, | |
4840 | struct net_device *orig_dev) | |
f697c3e8 | 4841 | { |
e7582bab | 4842 | #ifdef CONFIG_NET_CLS_ACT |
46209401 | 4843 | struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress); |
d2788d34 | 4844 | struct tcf_result cl_res; |
24824a09 | 4845 | |
c9e99fd0 DB |
4846 | /* If there's at least one ingress present somewhere (so |
4847 | * we get here via enabled static key), remaining devices | |
4848 | * that are not configured with an ingress qdisc will bail | |
d2788d34 | 4849 | * out here. |
c9e99fd0 | 4850 | */ |
46209401 | 4851 | if (!miniq) |
4577139b | 4852 | return skb; |
46209401 | 4853 | |
f697c3e8 HX |
4854 | if (*pt_prev) { |
4855 | *ret = deliver_skb(skb, *pt_prev, orig_dev); | |
4856 | *pt_prev = NULL; | |
1da177e4 LT |
4857 | } |
4858 | ||
3365495c | 4859 | qdisc_skb_cb(skb)->pkt_len = skb->len; |
8dc07fdb | 4860 | skb->tc_at_ingress = 1; |
46209401 | 4861 | mini_qdisc_bstats_cpu_update(miniq, skb); |
c9e99fd0 | 4862 | |
9410c940 PB |
4863 | switch (tcf_classify_ingress(skb, miniq->filter_list, &cl_res, |
4864 | false)) { | |
d2788d34 DB |
4865 | case TC_ACT_OK: |
4866 | case TC_ACT_RECLASSIFY: | |
4867 | skb->tc_index = TC_H_MIN(cl_res.classid); | |
4868 | break; | |
4869 | case TC_ACT_SHOT: | |
46209401 | 4870 | mini_qdisc_qstats_cpu_drop(miniq); |
8a3a4c6e ED |
4871 | kfree_skb(skb); |
4872 | return NULL; | |
d2788d34 DB |
4873 | case TC_ACT_STOLEN: |
4874 | case TC_ACT_QUEUED: | |
e25ea21f | 4875 | case TC_ACT_TRAP: |
8a3a4c6e | 4876 | consume_skb(skb); |
d2788d34 | 4877 | return NULL; |
27b29f63 AS |
4878 | case TC_ACT_REDIRECT: |
4879 | /* skb_mac_header check was done by cls/act_bpf, so | |
4880 | * we can safely push the L2 header back before | |
4881 | * redirecting to another netdev | |
4882 | */ | |
4883 | __skb_push(skb, skb->mac_len); | |
4884 | skb_do_redirect(skb); | |
4885 | return NULL; | |
720f22fe | 4886 | case TC_ACT_CONSUMED: |
cd11b164 | 4887 | return NULL; |
d2788d34 DB |
4888 | default: |
4889 | break; | |
f697c3e8 | 4890 | } |
e7582bab | 4891 | #endif /* CONFIG_NET_CLS_ACT */ |
e687ad60 PN |
4892 | return skb; |
4893 | } | |
1da177e4 | 4894 | |
24b27fc4 MB |
4895 | /** |
4896 | * netdev_is_rx_handler_busy - check if receive handler is registered | |
4897 | * @dev: device to check | |
4898 | * | |
4899 | * Check if a receive handler is already registered for a given device. | |
4900 | * Return true if there one. | |
4901 | * | |
4902 | * The caller must hold the rtnl_mutex. | |
4903 | */ | |
4904 | bool netdev_is_rx_handler_busy(struct net_device *dev) | |
4905 | { | |
4906 | ASSERT_RTNL(); | |
4907 | return dev && rtnl_dereference(dev->rx_handler); | |
4908 | } | |
4909 | EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy); | |
4910 | ||
ab95bfe0 JP |
4911 | /** |
4912 | * netdev_rx_handler_register - register receive handler | |
4913 | * @dev: device to register a handler for | |
4914 | * @rx_handler: receive handler to register | |
93e2c32b | 4915 | * @rx_handler_data: data pointer that is used by rx handler |
ab95bfe0 | 4916 | * |
e227867f | 4917 | * Register a receive handler for a device. This handler will then be |
ab95bfe0 JP |
4918 | * called from __netif_receive_skb. A negative errno code is returned |
4919 | * on a failure. | |
4920 | * | |
4921 | * The caller must hold the rtnl_mutex. | |
8a4eb573 JP |
4922 | * |
4923 | * For a general description of rx_handler, see enum rx_handler_result. | |
ab95bfe0 JP |
4924 | */ |
4925 | int netdev_rx_handler_register(struct net_device *dev, | |
93e2c32b JP |
4926 | rx_handler_func_t *rx_handler, |
4927 | void *rx_handler_data) | |
ab95bfe0 | 4928 | { |
1b7cd004 | 4929 | if (netdev_is_rx_handler_busy(dev)) |
ab95bfe0 JP |
4930 | return -EBUSY; |
4931 | ||
f5426250 PA |
4932 | if (dev->priv_flags & IFF_NO_RX_HANDLER) |
4933 | return -EINVAL; | |
4934 | ||
00cfec37 | 4935 | /* Note: rx_handler_data must be set before rx_handler */ |
93e2c32b | 4936 | rcu_assign_pointer(dev->rx_handler_data, rx_handler_data); |
ab95bfe0 JP |
4937 | rcu_assign_pointer(dev->rx_handler, rx_handler); |
4938 | ||
4939 | return 0; | |
4940 | } | |
4941 | EXPORT_SYMBOL_GPL(netdev_rx_handler_register); | |
4942 | ||
4943 | /** | |
4944 | * netdev_rx_handler_unregister - unregister receive handler | |
4945 | * @dev: device to unregister a handler from | |
4946 | * | |
166ec369 | 4947 | * Unregister a receive handler from a device. |
ab95bfe0 JP |
4948 | * |
4949 | * The caller must hold the rtnl_mutex. | |
4950 | */ | |
4951 | void netdev_rx_handler_unregister(struct net_device *dev) | |
4952 | { | |
4953 | ||
4954 | ASSERT_RTNL(); | |
a9b3cd7f | 4955 | RCU_INIT_POINTER(dev->rx_handler, NULL); |
00cfec37 ED |
4956 | /* a reader seeing a non NULL rx_handler in a rcu_read_lock() |
4957 | * section has a guarantee to see a non NULL rx_handler_data | |
4958 | * as well. | |
4959 | */ | |
4960 | synchronize_net(); | |
a9b3cd7f | 4961 | RCU_INIT_POINTER(dev->rx_handler_data, NULL); |
ab95bfe0 JP |
4962 | } |
4963 | EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister); | |
4964 | ||
b4b9e355 MG |
4965 | /* |
4966 | * Limit the use of PFMEMALLOC reserves to those protocols that implement | |
4967 | * the special handling of PFMEMALLOC skbs. | |
4968 | */ | |
4969 | static bool skb_pfmemalloc_protocol(struct sk_buff *skb) | |
4970 | { | |
4971 | switch (skb->protocol) { | |
2b8837ae JP |
4972 | case htons(ETH_P_ARP): |
4973 | case htons(ETH_P_IP): | |
4974 | case htons(ETH_P_IPV6): | |
4975 | case htons(ETH_P_8021Q): | |
4976 | case htons(ETH_P_8021AD): | |
b4b9e355 MG |
4977 | return true; |
4978 | default: | |
4979 | return false; | |
4980 | } | |
4981 | } | |
4982 | ||
e687ad60 PN |
4983 | static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev, |
4984 | int *ret, struct net_device *orig_dev) | |
4985 | { | |
4986 | if (nf_hook_ingress_active(skb)) { | |
2c1e2703 AC |
4987 | int ingress_retval; |
4988 | ||
e687ad60 PN |
4989 | if (*pt_prev) { |
4990 | *ret = deliver_skb(skb, *pt_prev, orig_dev); | |
4991 | *pt_prev = NULL; | |
4992 | } | |
4993 | ||
2c1e2703 AC |
4994 | rcu_read_lock(); |
4995 | ingress_retval = nf_hook_ingress(skb); | |
4996 | rcu_read_unlock(); | |
4997 | return ingress_retval; | |
e687ad60 PN |
4998 | } |
4999 | return 0; | |
5000 | } | |
e687ad60 | 5001 | |
88eb1944 EC |
5002 | static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc, |
5003 | struct packet_type **ppt_prev) | |
1da177e4 LT |
5004 | { |
5005 | struct packet_type *ptype, *pt_prev; | |
ab95bfe0 | 5006 | rx_handler_func_t *rx_handler; |
f2ccd8fa | 5007 | struct net_device *orig_dev; |
8a4eb573 | 5008 | bool deliver_exact = false; |
1da177e4 | 5009 | int ret = NET_RX_DROP; |
252e3346 | 5010 | __be16 type; |
1da177e4 | 5011 | |
588f0330 | 5012 | net_timestamp_check(!netdev_tstamp_prequeue, skb); |
81bbb3d4 | 5013 | |
cf66ba58 | 5014 | trace_netif_receive_skb(skb); |
9b22ea56 | 5015 | |
cc9bd5ce | 5016 | orig_dev = skb->dev; |
8f903c70 | 5017 | |
c1d2bbe1 | 5018 | skb_reset_network_header(skb); |
fda55eca ED |
5019 | if (!skb_transport_header_was_set(skb)) |
5020 | skb_reset_transport_header(skb); | |
0b5c9db1 | 5021 | skb_reset_mac_len(skb); |
1da177e4 LT |
5022 | |
5023 | pt_prev = NULL; | |
5024 | ||
63d8ea7f | 5025 | another_round: |
b6858177 | 5026 | skb->skb_iif = skb->dev->ifindex; |
63d8ea7f DM |
5027 | |
5028 | __this_cpu_inc(softnet_data.processed); | |
5029 | ||
458bf2f2 SH |
5030 | if (static_branch_unlikely(&generic_xdp_needed_key)) { |
5031 | int ret2; | |
5032 | ||
5033 | preempt_disable(); | |
5034 | ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb); | |
5035 | preempt_enable(); | |
5036 | ||
5037 | if (ret2 != XDP_PASS) | |
5038 | return NET_RX_DROP; | |
5039 | skb_reset_mac_len(skb); | |
5040 | } | |
5041 | ||
8ad227ff PM |
5042 | if (skb->protocol == cpu_to_be16(ETH_P_8021Q) || |
5043 | skb->protocol == cpu_to_be16(ETH_P_8021AD)) { | |
0d5501c1 | 5044 | skb = skb_vlan_untag(skb); |
bcc6d479 | 5045 | if (unlikely(!skb)) |
2c17d27c | 5046 | goto out; |
bcc6d479 JP |
5047 | } |
5048 | ||
e7246e12 WB |
5049 | if (skb_skip_tc_classify(skb)) |
5050 | goto skip_classify; | |
1da177e4 | 5051 | |
9754e293 | 5052 | if (pfmemalloc) |
b4b9e355 MG |
5053 | goto skip_taps; |
5054 | ||
1da177e4 | 5055 | list_for_each_entry_rcu(ptype, &ptype_all, list) { |
7866a621 SN |
5056 | if (pt_prev) |
5057 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5058 | pt_prev = ptype; | |
5059 | } | |
5060 | ||
5061 | list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) { | |
5062 | if (pt_prev) | |
5063 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5064 | pt_prev = ptype; | |
1da177e4 LT |
5065 | } |
5066 | ||
b4b9e355 | 5067 | skip_taps: |
1cf51900 | 5068 | #ifdef CONFIG_NET_INGRESS |
aabf6772 | 5069 | if (static_branch_unlikely(&ingress_needed_key)) { |
1f211a1b | 5070 | skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev); |
4577139b | 5071 | if (!skb) |
2c17d27c | 5072 | goto out; |
e687ad60 PN |
5073 | |
5074 | if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0) | |
2c17d27c | 5075 | goto out; |
4577139b | 5076 | } |
1cf51900 | 5077 | #endif |
a5135bcf | 5078 | skb_reset_tc(skb); |
e7246e12 | 5079 | skip_classify: |
9754e293 | 5080 | if (pfmemalloc && !skb_pfmemalloc_protocol(skb)) |
b4b9e355 MG |
5081 | goto drop; |
5082 | ||
df8a39de | 5083 | if (skb_vlan_tag_present(skb)) { |
2425717b JF |
5084 | if (pt_prev) { |
5085 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5086 | pt_prev = NULL; | |
5087 | } | |
48cc32d3 | 5088 | if (vlan_do_receive(&skb)) |
2425717b JF |
5089 | goto another_round; |
5090 | else if (unlikely(!skb)) | |
2c17d27c | 5091 | goto out; |
2425717b JF |
5092 | } |
5093 | ||
48cc32d3 | 5094 | rx_handler = rcu_dereference(skb->dev->rx_handler); |
ab95bfe0 JP |
5095 | if (rx_handler) { |
5096 | if (pt_prev) { | |
5097 | ret = deliver_skb(skb, pt_prev, orig_dev); | |
5098 | pt_prev = NULL; | |
5099 | } | |
8a4eb573 JP |
5100 | switch (rx_handler(&skb)) { |
5101 | case RX_HANDLER_CONSUMED: | |
3bc1b1ad | 5102 | ret = NET_RX_SUCCESS; |
2c17d27c | 5103 | goto out; |
8a4eb573 | 5104 | case RX_HANDLER_ANOTHER: |
63d8ea7f | 5105 | goto another_round; |
8a4eb573 JP |
5106 | case RX_HANDLER_EXACT: |
5107 | deliver_exact = true; | |
5108 | case RX_HANDLER_PASS: | |
5109 | break; | |
5110 | default: | |
5111 | BUG(); | |
5112 | } | |
ab95bfe0 | 5113 | } |
1da177e4 | 5114 | |
df8a39de | 5115 | if (unlikely(skb_vlan_tag_present(skb))) { |
36b2f61a GV |
5116 | check_vlan_id: |
5117 | if (skb_vlan_tag_get_id(skb)) { | |
5118 | /* Vlan id is non 0 and vlan_do_receive() above couldn't | |
5119 | * find vlan device. | |
5120 | */ | |
d4b812de | 5121 | skb->pkt_type = PACKET_OTHERHOST; |
36b2f61a GV |
5122 | } else if (skb->protocol == cpu_to_be16(ETH_P_8021Q) || |
5123 | skb->protocol == cpu_to_be16(ETH_P_8021AD)) { | |
5124 | /* Outer header is 802.1P with vlan 0, inner header is | |
5125 | * 802.1Q or 802.1AD and vlan_do_receive() above could | |
5126 | * not find vlan dev for vlan id 0. | |
5127 | */ | |
5128 | __vlan_hwaccel_clear_tag(skb); | |
5129 | skb = skb_vlan_untag(skb); | |
5130 | if (unlikely(!skb)) | |
5131 | goto out; | |
5132 | if (vlan_do_receive(&skb)) | |
5133 | /* After stripping off 802.1P header with vlan 0 | |
5134 | * vlan dev is found for inner header. | |
5135 | */ | |
5136 | goto another_round; | |
5137 | else if (unlikely(!skb)) | |
5138 | goto out; | |
5139 | else | |
5140 | /* We have stripped outer 802.1P vlan 0 header. | |
5141 | * But could not find vlan dev. | |
5142 | * check again for vlan id to set OTHERHOST. | |
5143 | */ | |
5144 | goto check_vlan_id; | |
5145 | } | |
d4b812de ED |
5146 | /* Note: we might in the future use prio bits |
5147 | * and set skb->priority like in vlan_do_receive() | |
5148 | * For the time being, just ignore Priority Code Point | |
5149 | */ | |
b1817524 | 5150 | __vlan_hwaccel_clear_tag(skb); |
d4b812de | 5151 | } |
48cc32d3 | 5152 | |
7866a621 SN |
5153 | type = skb->protocol; |
5154 | ||
63d8ea7f | 5155 | /* deliver only exact match when indicated */ |
7866a621 SN |
5156 | if (likely(!deliver_exact)) { |
5157 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, | |
5158 | &ptype_base[ntohs(type) & | |
5159 | PTYPE_HASH_MASK]); | |
5160 | } | |
1f3c8804 | 5161 | |
7866a621 SN |
5162 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, |
5163 | &orig_dev->ptype_specific); | |
5164 | ||
5165 | if (unlikely(skb->dev != orig_dev)) { | |
5166 | deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type, | |
5167 | &skb->dev->ptype_specific); | |
1da177e4 LT |
5168 | } |
5169 | ||
5170 | if (pt_prev) { | |
1f8b977a | 5171 | if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC))) |
0e698bf6 | 5172 | goto drop; |
88eb1944 | 5173 | *ppt_prev = pt_prev; |
1da177e4 | 5174 | } else { |
b4b9e355 | 5175 | drop: |
6e7333d3 JW |
5176 | if (!deliver_exact) |
5177 | atomic_long_inc(&skb->dev->rx_dropped); | |
5178 | else | |
5179 | atomic_long_inc(&skb->dev->rx_nohandler); | |
1da177e4 LT |
5180 | kfree_skb(skb); |
5181 | /* Jamal, now you will not able to escape explaining | |
5182 | * me how you were going to use this. :-) | |
5183 | */ | |
5184 | ret = NET_RX_DROP; | |
5185 | } | |
5186 | ||
2c17d27c | 5187 | out: |
9754e293 DM |
5188 | return ret; |
5189 | } | |
5190 | ||
88eb1944 EC |
5191 | static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc) |
5192 | { | |
5193 | struct net_device *orig_dev = skb->dev; | |
5194 | struct packet_type *pt_prev = NULL; | |
5195 | int ret; | |
5196 | ||
5197 | ret = __netif_receive_skb_core(skb, pfmemalloc, &pt_prev); | |
5198 | if (pt_prev) | |
f5737cba PA |
5199 | ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb, |
5200 | skb->dev, pt_prev, orig_dev); | |
88eb1944 EC |
5201 | return ret; |
5202 | } | |
5203 | ||
1c601d82 JDB |
5204 | /** |
5205 | * netif_receive_skb_core - special purpose version of netif_receive_skb | |
5206 | * @skb: buffer to process | |
5207 | * | |
5208 | * More direct receive version of netif_receive_skb(). It should | |
5209 | * only be used by callers that have a need to skip RPS and Generic XDP. | |
5210 | * Caller must also take care of handling if (page_is_)pfmemalloc. | |
5211 | * | |
5212 | * This function may only be called from softirq context and interrupts | |
5213 | * should be enabled. | |
5214 | * | |
5215 | * Return values (usually ignored): | |
5216 | * NET_RX_SUCCESS: no congestion | |
5217 | * NET_RX_DROP: packet was dropped | |
5218 | */ | |
5219 | int netif_receive_skb_core(struct sk_buff *skb) | |
5220 | { | |
5221 | int ret; | |
5222 | ||
5223 | rcu_read_lock(); | |
88eb1944 | 5224 | ret = __netif_receive_skb_one_core(skb, false); |
1c601d82 JDB |
5225 | rcu_read_unlock(); |
5226 | ||
5227 | return ret; | |
5228 | } | |
5229 | EXPORT_SYMBOL(netif_receive_skb_core); | |
5230 | ||
88eb1944 EC |
5231 | static inline void __netif_receive_skb_list_ptype(struct list_head *head, |
5232 | struct packet_type *pt_prev, | |
5233 | struct net_device *orig_dev) | |
4ce0017a EC |
5234 | { |
5235 | struct sk_buff *skb, *next; | |
5236 | ||
88eb1944 EC |
5237 | if (!pt_prev) |
5238 | return; | |
5239 | if (list_empty(head)) | |
5240 | return; | |
17266ee9 | 5241 | if (pt_prev->list_func != NULL) |
fdf71426 PA |
5242 | INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv, |
5243 | ip_list_rcv, head, pt_prev, orig_dev); | |
17266ee9 | 5244 | else |
9a5a90d1 AL |
5245 | list_for_each_entry_safe(skb, next, head, list) { |
5246 | skb_list_del_init(skb); | |
fdf71426 | 5247 | pt_prev->func(skb, skb->dev, pt_prev, orig_dev); |
9a5a90d1 | 5248 | } |
88eb1944 EC |
5249 | } |
5250 | ||
5251 | static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc) | |
5252 | { | |
5253 | /* Fast-path assumptions: | |
5254 | * - There is no RX handler. | |
5255 | * - Only one packet_type matches. | |
5256 | * If either of these fails, we will end up doing some per-packet | |
5257 | * processing in-line, then handling the 'last ptype' for the whole | |
5258 | * sublist. This can't cause out-of-order delivery to any single ptype, | |
5259 | * because the 'last ptype' must be constant across the sublist, and all | |
5260 | * other ptypes are handled per-packet. | |
5261 | */ | |
5262 | /* Current (common) ptype of sublist */ | |
5263 | struct packet_type *pt_curr = NULL; | |
5264 | /* Current (common) orig_dev of sublist */ | |
5265 | struct net_device *od_curr = NULL; | |
5266 | struct list_head sublist; | |
5267 | struct sk_buff *skb, *next; | |
5268 | ||
9af86f93 | 5269 | INIT_LIST_HEAD(&sublist); |
88eb1944 EC |
5270 | list_for_each_entry_safe(skb, next, head, list) { |
5271 | struct net_device *orig_dev = skb->dev; | |
5272 | struct packet_type *pt_prev = NULL; | |
5273 | ||
22f6bbb7 | 5274 | skb_list_del_init(skb); |
88eb1944 | 5275 | __netif_receive_skb_core(skb, pfmemalloc, &pt_prev); |
9af86f93 EC |
5276 | if (!pt_prev) |
5277 | continue; | |
88eb1944 EC |
5278 | if (pt_curr != pt_prev || od_curr != orig_dev) { |
5279 | /* dispatch old sublist */ | |
88eb1944 EC |
5280 | __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); |
5281 | /* start new sublist */ | |
9af86f93 | 5282 | INIT_LIST_HEAD(&sublist); |
88eb1944 EC |
5283 | pt_curr = pt_prev; |
5284 | od_curr = orig_dev; | |
5285 | } | |
9af86f93 | 5286 | list_add_tail(&skb->list, &sublist); |
88eb1944 EC |
5287 | } |
5288 | ||
5289 | /* dispatch final sublist */ | |
9af86f93 | 5290 | __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr); |
4ce0017a EC |
5291 | } |
5292 | ||
9754e293 DM |
5293 | static int __netif_receive_skb(struct sk_buff *skb) |
5294 | { | |
5295 | int ret; | |
5296 | ||
5297 | if (sk_memalloc_socks() && skb_pfmemalloc(skb)) { | |
f1083048 | 5298 | unsigned int noreclaim_flag; |
9754e293 DM |
5299 | |
5300 | /* | |
5301 | * PFMEMALLOC skbs are special, they should | |
5302 | * - be delivered to SOCK_MEMALLOC sockets only | |
5303 | * - stay away from userspace | |
5304 | * - have bounded memory usage | |
5305 | * | |
5306 | * Use PF_MEMALLOC as this saves us from propagating the allocation | |
5307 | * context down to all allocation sites. | |
5308 | */ | |
f1083048 | 5309 | noreclaim_flag = memalloc_noreclaim_save(); |
88eb1944 | 5310 | ret = __netif_receive_skb_one_core(skb, true); |
f1083048 | 5311 | memalloc_noreclaim_restore(noreclaim_flag); |
9754e293 | 5312 | } else |
88eb1944 | 5313 | ret = __netif_receive_skb_one_core(skb, false); |
9754e293 | 5314 | |
1da177e4 LT |
5315 | return ret; |
5316 | } | |
0a9627f2 | 5317 | |
4ce0017a EC |
5318 | static void __netif_receive_skb_list(struct list_head *head) |
5319 | { | |
5320 | unsigned long noreclaim_flag = 0; | |
5321 | struct sk_buff *skb, *next; | |
5322 | bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */ | |
5323 | ||
5324 | list_for_each_entry_safe(skb, next, head, list) { | |
5325 | if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) { | |
5326 | struct list_head sublist; | |
5327 | ||
5328 | /* Handle the previous sublist */ | |
5329 | list_cut_before(&sublist, head, &skb->list); | |
b9f463d6 EC |
5330 | if (!list_empty(&sublist)) |
5331 | __netif_receive_skb_list_core(&sublist, pfmemalloc); | |
4ce0017a EC |
5332 | pfmemalloc = !pfmemalloc; |
5333 | /* See comments in __netif_receive_skb */ | |
5334 | if (pfmemalloc) | |
5335 | noreclaim_flag = memalloc_noreclaim_save(); | |
5336 | else | |
5337 | memalloc_noreclaim_restore(noreclaim_flag); | |
5338 | } | |
5339 | } | |
5340 | /* Handle the remaining sublist */ | |
b9f463d6 EC |
5341 | if (!list_empty(head)) |
5342 | __netif_receive_skb_list_core(head, pfmemalloc); | |
4ce0017a EC |
5343 | /* Restore pflags */ |
5344 | if (pfmemalloc) | |
5345 | memalloc_noreclaim_restore(noreclaim_flag); | |
5346 | } | |
5347 | ||
f4e63525 | 5348 | static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp) |
b5cdae32 | 5349 | { |
58038695 | 5350 | struct bpf_prog *old = rtnl_dereference(dev->xdp_prog); |
b5cdae32 DM |
5351 | struct bpf_prog *new = xdp->prog; |
5352 | int ret = 0; | |
5353 | ||
5354 | switch (xdp->command) { | |
58038695 | 5355 | case XDP_SETUP_PROG: |
b5cdae32 DM |
5356 | rcu_assign_pointer(dev->xdp_prog, new); |
5357 | if (old) | |
5358 | bpf_prog_put(old); | |
5359 | ||
5360 | if (old && !new) { | |
02786475 | 5361 | static_branch_dec(&generic_xdp_needed_key); |
b5cdae32 | 5362 | } else if (new && !old) { |
02786475 | 5363 | static_branch_inc(&generic_xdp_needed_key); |
b5cdae32 | 5364 | dev_disable_lro(dev); |
56f5aa77 | 5365 | dev_disable_gro_hw(dev); |
b5cdae32 DM |
5366 | } |
5367 | break; | |
b5cdae32 DM |
5368 | |
5369 | case XDP_QUERY_PROG: | |
58038695 | 5370 | xdp->prog_id = old ? old->aux->id : 0; |
b5cdae32 DM |
5371 | break; |
5372 | ||
5373 | default: | |
5374 | ret = -EINVAL; | |
5375 | break; | |
5376 | } | |
5377 | ||
5378 | return ret; | |
5379 | } | |
5380 | ||
ae78dbfa | 5381 | static int netif_receive_skb_internal(struct sk_buff *skb) |
0a9627f2 | 5382 | { |
2c17d27c JA |
5383 | int ret; |
5384 | ||
588f0330 | 5385 | net_timestamp_check(netdev_tstamp_prequeue, skb); |
3b098e2d | 5386 | |
c1f19b51 RC |
5387 | if (skb_defer_rx_timestamp(skb)) |
5388 | return NET_RX_SUCCESS; | |
5389 | ||
bbbe211c | 5390 | rcu_read_lock(); |
df334545 | 5391 | #ifdef CONFIG_RPS |
dc05360f | 5392 | if (static_branch_unlikely(&rps_needed)) { |
3b098e2d | 5393 | struct rps_dev_flow voidflow, *rflow = &voidflow; |
2c17d27c | 5394 | int cpu = get_rps_cpu(skb->dev, skb, &rflow); |
0a9627f2 | 5395 | |
3b098e2d ED |
5396 | if (cpu >= 0) { |
5397 | ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail); | |
5398 | rcu_read_unlock(); | |
adc9300e | 5399 | return ret; |
3b098e2d | 5400 | } |
fec5e652 | 5401 | } |
1e94d72f | 5402 | #endif |
2c17d27c JA |
5403 | ret = __netif_receive_skb(skb); |
5404 | rcu_read_unlock(); | |
5405 | return ret; | |
0a9627f2 | 5406 | } |
ae78dbfa | 5407 | |
7da517a3 EC |
5408 | static void netif_receive_skb_list_internal(struct list_head *head) |
5409 | { | |
7da517a3 | 5410 | struct sk_buff *skb, *next; |
8c057efa | 5411 | struct list_head sublist; |
7da517a3 | 5412 | |
8c057efa | 5413 | INIT_LIST_HEAD(&sublist); |
7da517a3 EC |
5414 | list_for_each_entry_safe(skb, next, head, list) { |
5415 | net_timestamp_check(netdev_tstamp_prequeue, skb); | |
22f6bbb7 | 5416 | skb_list_del_init(skb); |
8c057efa EC |
5417 | if (!skb_defer_rx_timestamp(skb)) |
5418 | list_add_tail(&skb->list, &sublist); | |
7da517a3 | 5419 | } |
8c057efa | 5420 | list_splice_init(&sublist, head); |
7da517a3 | 5421 | |
7da517a3 EC |
5422 | rcu_read_lock(); |
5423 | #ifdef CONFIG_RPS | |
dc05360f | 5424 | if (static_branch_unlikely(&rps_needed)) { |
7da517a3 EC |
5425 | list_for_each_entry_safe(skb, next, head, list) { |
5426 | struct rps_dev_flow voidflow, *rflow = &voidflow; | |
5427 | int cpu = get_rps_cpu(skb->dev, skb, &rflow); | |
5428 | ||
5429 | if (cpu >= 0) { | |
8c057efa | 5430 | /* Will be handled, remove from list */ |
22f6bbb7 | 5431 | skb_list_del_init(skb); |
8c057efa | 5432 | enqueue_to_backlog(skb, cpu, &rflow->last_qtail); |
7da517a3 EC |
5433 | } |
5434 | } | |
5435 | } | |
5436 | #endif | |
5437 | __netif_receive_skb_list(head); | |
5438 | rcu_read_unlock(); | |
5439 | } | |
5440 | ||
ae78dbfa BH |
5441 | /** |
5442 | * netif_receive_skb - process receive buffer from network | |
5443 | * @skb: buffer to process | |
5444 | * | |
5445 | * netif_receive_skb() is the main receive data processing function. | |
5446 | * It always succeeds. The buffer may be dropped during processing | |
5447 | * for congestion control or by the protocol layers. | |
5448 | * | |
5449 | * This function may only be called from softirq context and interrupts | |
5450 | * should be enabled. | |
5451 | * | |
5452 | * Return values (usually ignored): | |
5453 | * NET_RX_SUCCESS: no congestion | |
5454 | * NET_RX_DROP: packet was dropped | |
5455 | */ | |
04eb4489 | 5456 | int netif_receive_skb(struct sk_buff *skb) |
ae78dbfa | 5457 | { |
b0e3f1bd GB |
5458 | int ret; |
5459 | ||
ae78dbfa BH |
5460 | trace_netif_receive_skb_entry(skb); |
5461 | ||
b0e3f1bd GB |
5462 | ret = netif_receive_skb_internal(skb); |
5463 | trace_netif_receive_skb_exit(ret); | |
5464 | ||
5465 | return ret; | |
ae78dbfa | 5466 | } |
04eb4489 | 5467 | EXPORT_SYMBOL(netif_receive_skb); |
1da177e4 | 5468 | |
f6ad8c1b EC |
5469 | /** |
5470 | * netif_receive_skb_list - process many receive buffers from network | |
5471 | * @head: list of skbs to process. | |
5472 | * | |
7da517a3 EC |
5473 | * Since return value of netif_receive_skb() is normally ignored, and |
5474 | * wouldn't be meaningful for a list, this function returns void. | |
f6ad8c1b EC |
5475 | * |
5476 | * This function may only be called from softirq context and interrupts | |
5477 | * should be enabled. | |
5478 | */ | |
5479 | void netif_receive_skb_list(struct list_head *head) | |
5480 | { | |
7da517a3 | 5481 | struct sk_buff *skb; |
f6ad8c1b | 5482 | |
b9f463d6 EC |
5483 | if (list_empty(head)) |
5484 | return; | |
b0e3f1bd GB |
5485 | if (trace_netif_receive_skb_list_entry_enabled()) { |
5486 | list_for_each_entry(skb, head, list) | |
5487 | trace_netif_receive_skb_list_entry(skb); | |
5488 | } | |
7da517a3 | 5489 | netif_receive_skb_list_internal(head); |
b0e3f1bd | 5490 | trace_netif_receive_skb_list_exit(0); |
f6ad8c1b EC |
5491 | } |
5492 | EXPORT_SYMBOL(netif_receive_skb_list); | |
5493 | ||
41852497 | 5494 | DEFINE_PER_CPU(struct work_struct, flush_works); |
145dd5f9 PA |
5495 | |
5496 | /* Network device is going away, flush any packets still pending */ | |
5497 | static void flush_backlog(struct work_struct *work) | |
6e583ce5 | 5498 | { |
6e583ce5 | 5499 | struct sk_buff *skb, *tmp; |
145dd5f9 PA |
5500 | struct softnet_data *sd; |
5501 | ||
5502 | local_bh_disable(); | |
5503 | sd = this_cpu_ptr(&softnet_data); | |
6e583ce5 | 5504 | |
145dd5f9 | 5505 | local_irq_disable(); |
e36fa2f7 | 5506 | rps_lock(sd); |
6e7676c1 | 5507 | skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) { |
41852497 | 5508 | if (skb->dev->reg_state == NETREG_UNREGISTERING) { |
e36fa2f7 | 5509 | __skb_unlink(skb, &sd->input_pkt_queue); |
6e583ce5 | 5510 | kfree_skb(skb); |
76cc8b13 | 5511 | input_queue_head_incr(sd); |
6e583ce5 | 5512 | } |
6e7676c1 | 5513 | } |
e36fa2f7 | 5514 | rps_unlock(sd); |
145dd5f9 | 5515 | local_irq_enable(); |
6e7676c1 CG |
5516 | |
5517 | skb_queue_walk_safe(&sd->process_queue, skb, tmp) { | |
41852497 | 5518 | if (skb->dev->reg_state == NETREG_UNREGISTERING) { |
6e7676c1 CG |
5519 | __skb_unlink(skb, &sd->process_queue); |
5520 | kfree_skb(skb); | |
76cc8b13 | 5521 | input_queue_head_incr(sd); |
6e7676c1 CG |
5522 | } |
5523 | } | |
145dd5f9 PA |
5524 | local_bh_enable(); |
5525 | } | |
5526 | ||
41852497 | 5527 | static void flush_all_backlogs(void) |
145dd5f9 PA |
5528 | { |
5529 | unsigned int cpu; | |
5530 | ||
5531 | get_online_cpus(); | |
5532 | ||
41852497 ED |
5533 | for_each_online_cpu(cpu) |
5534 | queue_work_on(cpu, system_highpri_wq, | |
5535 | per_cpu_ptr(&flush_works, cpu)); | |
145dd5f9 PA |
5536 | |
5537 | for_each_online_cpu(cpu) | |
41852497 | 5538 | flush_work(per_cpu_ptr(&flush_works, cpu)); |
145dd5f9 PA |
5539 | |
5540 | put_online_cpus(); | |
6e583ce5 SH |
5541 | } |
5542 | ||
c8079432 MM |
5543 | /* Pass the currently batched GRO_NORMAL SKBs up to the stack. */ |
5544 | static void gro_normal_list(struct napi_struct *napi) | |
5545 | { | |
5546 | if (!napi->rx_count) | |
5547 | return; | |
5548 | netif_receive_skb_list_internal(&napi->rx_list); | |
5549 | INIT_LIST_HEAD(&napi->rx_list); | |
5550 | napi->rx_count = 0; | |
5551 | } | |
5552 | ||
5553 | /* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded, | |
5554 | * pass the whole batch up to the stack. | |
5555 | */ | |
5556 | static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb) | |
5557 | { | |
5558 | list_add_tail(&skb->list, &napi->rx_list); | |
5559 | if (++napi->rx_count >= gro_normal_batch) | |
5560 | gro_normal_list(napi); | |
5561 | } | |
5562 | ||
aaa5d90b PA |
5563 | INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int)); |
5564 | INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int)); | |
c8079432 | 5565 | static int napi_gro_complete(struct napi_struct *napi, struct sk_buff *skb) |
d565b0a1 | 5566 | { |
22061d80 | 5567 | struct packet_offload *ptype; |
d565b0a1 | 5568 | __be16 type = skb->protocol; |
22061d80 | 5569 | struct list_head *head = &offload_base; |
d565b0a1 HX |
5570 | int err = -ENOENT; |
5571 | ||
c3c7c254 ED |
5572 | BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb)); |
5573 | ||
fc59f9a3 HX |
5574 | if (NAPI_GRO_CB(skb)->count == 1) { |
5575 | skb_shinfo(skb)->gso_size = 0; | |
d565b0a1 | 5576 | goto out; |
fc59f9a3 | 5577 | } |
d565b0a1 HX |
5578 | |
5579 | rcu_read_lock(); | |
5580 | list_for_each_entry_rcu(ptype, head, list) { | |
f191a1d1 | 5581 | if (ptype->type != type || !ptype->callbacks.gro_complete) |
d565b0a1 HX |
5582 | continue; |
5583 | ||
aaa5d90b PA |
5584 | err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete, |
5585 | ipv6_gro_complete, inet_gro_complete, | |
5586 | skb, 0); | |
d565b0a1 HX |
5587 | break; |
5588 | } | |
5589 | rcu_read_unlock(); | |
5590 | ||
5591 | if (err) { | |
5592 | WARN_ON(&ptype->list == head); | |
5593 | kfree_skb(skb); | |
5594 | return NET_RX_SUCCESS; | |
5595 | } | |
5596 | ||
5597 | out: | |
c8079432 MM |
5598 | gro_normal_one(napi, skb); |
5599 | return NET_RX_SUCCESS; | |
d565b0a1 HX |
5600 | } |
5601 | ||
6312fe77 | 5602 | static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index, |
07d78363 | 5603 | bool flush_old) |
d565b0a1 | 5604 | { |
6312fe77 | 5605 | struct list_head *head = &napi->gro_hash[index].list; |
d4546c25 | 5606 | struct sk_buff *skb, *p; |
2e71a6f8 | 5607 | |
07d78363 | 5608 | list_for_each_entry_safe_reverse(skb, p, head, list) { |
2e71a6f8 ED |
5609 | if (flush_old && NAPI_GRO_CB(skb)->age == jiffies) |
5610 | return; | |
992cba7e | 5611 | skb_list_del_init(skb); |
c8079432 | 5612 | napi_gro_complete(napi, skb); |
6312fe77 | 5613 | napi->gro_hash[index].count--; |
d565b0a1 | 5614 | } |
d9f37d01 LR |
5615 | |
5616 | if (!napi->gro_hash[index].count) | |
5617 | __clear_bit(index, &napi->gro_bitmask); | |
d565b0a1 | 5618 | } |
07d78363 | 5619 | |
6312fe77 | 5620 | /* napi->gro_hash[].list contains packets ordered by age. |
07d78363 DM |
5621 | * youngest packets at the head of it. |
5622 | * Complete skbs in reverse order to reduce latencies. | |
5623 | */ | |
5624 | void napi_gro_flush(struct napi_struct *napi, bool flush_old) | |
5625 | { | |
42519ede ED |
5626 | unsigned long bitmask = napi->gro_bitmask; |
5627 | unsigned int i, base = ~0U; | |
07d78363 | 5628 | |
42519ede ED |
5629 | while ((i = ffs(bitmask)) != 0) { |
5630 | bitmask >>= i; | |
5631 | base += i; | |
5632 | __napi_gro_flush_chain(napi, base, flush_old); | |
d9f37d01 | 5633 | } |
07d78363 | 5634 | } |
86cac58b | 5635 | EXPORT_SYMBOL(napi_gro_flush); |
d565b0a1 | 5636 | |
07d78363 DM |
5637 | static struct list_head *gro_list_prepare(struct napi_struct *napi, |
5638 | struct sk_buff *skb) | |
89c5fa33 | 5639 | { |
89c5fa33 | 5640 | unsigned int maclen = skb->dev->hard_header_len; |
0b4cec8c | 5641 | u32 hash = skb_get_hash_raw(skb); |
07d78363 | 5642 | struct list_head *head; |
d4546c25 | 5643 | struct sk_buff *p; |
89c5fa33 | 5644 | |
6312fe77 | 5645 | head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list; |
07d78363 | 5646 | list_for_each_entry(p, head, list) { |
89c5fa33 ED |
5647 | unsigned long diffs; |
5648 | ||
0b4cec8c TH |
5649 | NAPI_GRO_CB(p)->flush = 0; |
5650 | ||
5651 | if (hash != skb_get_hash_raw(p)) { | |
5652 | NAPI_GRO_CB(p)->same_flow = 0; | |
5653 | continue; | |
5654 | } | |
5655 | ||
89c5fa33 | 5656 | diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev; |
b1817524 MM |
5657 | diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb); |
5658 | if (skb_vlan_tag_present(p)) | |
fc5141cb | 5659 | diffs |= skb_vlan_tag_get(p) ^ skb_vlan_tag_get(skb); |
ce87fc6c | 5660 | diffs |= skb_metadata_dst_cmp(p, skb); |
de8f3a83 | 5661 | diffs |= skb_metadata_differs(p, skb); |
89c5fa33 ED |
5662 | if (maclen == ETH_HLEN) |
5663 | diffs |= compare_ether_header(skb_mac_header(p), | |
a50e233c | 5664 | skb_mac_header(skb)); |
89c5fa33 ED |
5665 | else if (!diffs) |
5666 | diffs = memcmp(skb_mac_header(p), | |
a50e233c | 5667 | skb_mac_header(skb), |
89c5fa33 ED |
5668 | maclen); |
5669 | NAPI_GRO_CB(p)->same_flow = !diffs; | |
89c5fa33 | 5670 | } |
07d78363 DM |
5671 | |
5672 | return head; | |
89c5fa33 ED |
5673 | } |
5674 | ||
299603e8 JC |
5675 | static void skb_gro_reset_offset(struct sk_buff *skb) |
5676 | { | |
5677 | const struct skb_shared_info *pinfo = skb_shinfo(skb); | |
5678 | const skb_frag_t *frag0 = &pinfo->frags[0]; | |
5679 | ||
5680 | NAPI_GRO_CB(skb)->data_offset = 0; | |
5681 | NAPI_GRO_CB(skb)->frag0 = NULL; | |
5682 | NAPI_GRO_CB(skb)->frag0_len = 0; | |
5683 | ||
8aef998d | 5684 | if (!skb_headlen(skb) && pinfo->nr_frags && |
299603e8 JC |
5685 | !PageHighMem(skb_frag_page(frag0))) { |
5686 | NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0); | |
7cfd5fd5 ED |
5687 | NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int, |
5688 | skb_frag_size(frag0), | |
5689 | skb->end - skb->tail); | |
89c5fa33 ED |
5690 | } |
5691 | } | |
5692 | ||
a50e233c ED |
5693 | static void gro_pull_from_frag0(struct sk_buff *skb, int grow) |
5694 | { | |
5695 | struct skb_shared_info *pinfo = skb_shinfo(skb); | |
5696 | ||
5697 | BUG_ON(skb->end - skb->tail < grow); | |
5698 | ||
5699 | memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow); | |
5700 | ||
5701 | skb->data_len -= grow; | |
5702 | skb->tail += grow; | |
5703 | ||
b54c9d5b | 5704 | skb_frag_off_add(&pinfo->frags[0], grow); |
a50e233c ED |
5705 | skb_frag_size_sub(&pinfo->frags[0], grow); |
5706 | ||
5707 | if (unlikely(!skb_frag_size(&pinfo->frags[0]))) { | |
5708 | skb_frag_unref(skb, 0); | |
5709 | memmove(pinfo->frags, pinfo->frags + 1, | |
5710 | --pinfo->nr_frags * sizeof(pinfo->frags[0])); | |
5711 | } | |
5712 | } | |
5713 | ||
c8079432 | 5714 | static void gro_flush_oldest(struct napi_struct *napi, struct list_head *head) |
07d78363 | 5715 | { |
6312fe77 | 5716 | struct sk_buff *oldest; |
07d78363 | 5717 | |
6312fe77 | 5718 | oldest = list_last_entry(head, struct sk_buff, list); |
07d78363 | 5719 | |
6312fe77 | 5720 | /* We are called with head length >= MAX_GRO_SKBS, so this is |
07d78363 DM |
5721 | * impossible. |
5722 | */ | |
5723 | if (WARN_ON_ONCE(!oldest)) | |
5724 | return; | |
5725 | ||
d9f37d01 LR |
5726 | /* Do not adjust napi->gro_hash[].count, caller is adding a new |
5727 | * SKB to the chain. | |
07d78363 | 5728 | */ |
ece23711 | 5729 | skb_list_del_init(oldest); |
c8079432 | 5730 | napi_gro_complete(napi, oldest); |
07d78363 DM |
5731 | } |
5732 | ||
aaa5d90b PA |
5733 | INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *, |
5734 | struct sk_buff *)); | |
5735 | INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *, | |
5736 | struct sk_buff *)); | |
bb728820 | 5737 | static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb) |
d565b0a1 | 5738 | { |
6312fe77 | 5739 | u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1); |
d4546c25 | 5740 | struct list_head *head = &offload_base; |
22061d80 | 5741 | struct packet_offload *ptype; |
d565b0a1 | 5742 | __be16 type = skb->protocol; |
07d78363 | 5743 | struct list_head *gro_head; |
d4546c25 | 5744 | struct sk_buff *pp = NULL; |
5b252f0c | 5745 | enum gro_result ret; |
d4546c25 | 5746 | int same_flow; |
a50e233c | 5747 | int grow; |
d565b0a1 | 5748 | |
b5cdae32 | 5749 | if (netif_elide_gro(skb->dev)) |
d565b0a1 HX |
5750 | goto normal; |
5751 | ||
07d78363 | 5752 | gro_head = gro_list_prepare(napi, skb); |
89c5fa33 | 5753 | |
d565b0a1 HX |
5754 | rcu_read_lock(); |
5755 | list_for_each_entry_rcu(ptype, head, list) { | |
f191a1d1 | 5756 | if (ptype->type != type || !ptype->callbacks.gro_receive) |
d565b0a1 HX |
5757 | continue; |
5758 | ||
86911732 | 5759 | skb_set_network_header(skb, skb_gro_offset(skb)); |
efd9450e | 5760 | skb_reset_mac_len(skb); |
d565b0a1 | 5761 | NAPI_GRO_CB(skb)->same_flow = 0; |
d61d072e | 5762 | NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb); |
5d38a079 | 5763 | NAPI_GRO_CB(skb)->free = 0; |
fac8e0f5 | 5764 | NAPI_GRO_CB(skb)->encap_mark = 0; |
fcd91dd4 | 5765 | NAPI_GRO_CB(skb)->recursion_counter = 0; |
a0ca153f | 5766 | NAPI_GRO_CB(skb)->is_fou = 0; |
1530545e | 5767 | NAPI_GRO_CB(skb)->is_atomic = 1; |
15e2396d | 5768 | NAPI_GRO_CB(skb)->gro_remcsum_start = 0; |
d565b0a1 | 5769 | |
662880f4 TH |
5770 | /* Setup for GRO checksum validation */ |
5771 | switch (skb->ip_summed) { | |
5772 | case CHECKSUM_COMPLETE: | |
5773 | NAPI_GRO_CB(skb)->csum = skb->csum; | |
5774 | NAPI_GRO_CB(skb)->csum_valid = 1; | |
5775 | NAPI_GRO_CB(skb)->csum_cnt = 0; | |
5776 | break; | |
5777 | case CHECKSUM_UNNECESSARY: | |
5778 | NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1; | |
5779 | NAPI_GRO_CB(skb)->csum_valid = 0; | |
5780 | break; | |
5781 | default: | |
5782 | NAPI_GRO_CB(skb)->csum_cnt = 0; | |
5783 | NAPI_GRO_CB(skb)->csum_valid = 0; | |
5784 | } | |
d565b0a1 | 5785 | |
aaa5d90b PA |
5786 | pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive, |
5787 | ipv6_gro_receive, inet_gro_receive, | |
5788 | gro_head, skb); | |
d565b0a1 HX |
5789 | break; |
5790 | } | |
5791 | rcu_read_unlock(); | |
5792 | ||
5793 | if (&ptype->list == head) | |
5794 | goto normal; | |
5795 | ||
45586c70 | 5796 | if (PTR_ERR(pp) == -EINPROGRESS) { |
25393d3f SK |
5797 | ret = GRO_CONSUMED; |
5798 | goto ok; | |
5799 | } | |
5800 | ||
0da2afd5 | 5801 | same_flow = NAPI_GRO_CB(skb)->same_flow; |
5d0d9be8 | 5802 | ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED; |
0da2afd5 | 5803 | |
d565b0a1 | 5804 | if (pp) { |
992cba7e | 5805 | skb_list_del_init(pp); |
c8079432 | 5806 | napi_gro_complete(napi, pp); |
6312fe77 | 5807 | napi->gro_hash[hash].count--; |
d565b0a1 HX |
5808 | } |
5809 | ||
0da2afd5 | 5810 | if (same_flow) |
d565b0a1 HX |
5811 | goto ok; |
5812 | ||
600adc18 | 5813 | if (NAPI_GRO_CB(skb)->flush) |
d565b0a1 | 5814 | goto normal; |
d565b0a1 | 5815 | |
6312fe77 | 5816 | if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) { |
c8079432 | 5817 | gro_flush_oldest(napi, gro_head); |
600adc18 | 5818 | } else { |
6312fe77 | 5819 | napi->gro_hash[hash].count++; |
600adc18 | 5820 | } |
d565b0a1 | 5821 | NAPI_GRO_CB(skb)->count = 1; |
2e71a6f8 | 5822 | NAPI_GRO_CB(skb)->age = jiffies; |
29e98242 | 5823 | NAPI_GRO_CB(skb)->last = skb; |
86911732 | 5824 | skb_shinfo(skb)->gso_size = skb_gro_len(skb); |
07d78363 | 5825 | list_add(&skb->list, gro_head); |
5d0d9be8 | 5826 | ret = GRO_HELD; |
d565b0a1 | 5827 | |
ad0f9904 | 5828 | pull: |
a50e233c ED |
5829 | grow = skb_gro_offset(skb) - skb_headlen(skb); |
5830 | if (grow > 0) | |
5831 | gro_pull_from_frag0(skb, grow); | |
d565b0a1 | 5832 | ok: |
d9f37d01 LR |
5833 | if (napi->gro_hash[hash].count) { |
5834 | if (!test_bit(hash, &napi->gro_bitmask)) | |
5835 | __set_bit(hash, &napi->gro_bitmask); | |
5836 | } else if (test_bit(hash, &napi->gro_bitmask)) { | |
5837 | __clear_bit(hash, &napi->gro_bitmask); | |
5838 | } | |
5839 | ||
5d0d9be8 | 5840 | return ret; |
d565b0a1 HX |
5841 | |
5842 | normal: | |
ad0f9904 HX |
5843 | ret = GRO_NORMAL; |
5844 | goto pull; | |
5d38a079 | 5845 | } |
96e93eab | 5846 | |
bf5a755f JC |
5847 | struct packet_offload *gro_find_receive_by_type(__be16 type) |
5848 | { | |
5849 | struct list_head *offload_head = &offload_base; | |
5850 | struct packet_offload *ptype; | |
5851 | ||
5852 | list_for_each_entry_rcu(ptype, offload_head, list) { | |
5853 | if (ptype->type != type || !ptype->callbacks.gro_receive) | |
5854 | continue; | |
5855 | return ptype; | |
5856 | } | |
5857 | return NULL; | |
5858 | } | |
e27a2f83 | 5859 | EXPORT_SYMBOL(gro_find_receive_by_type); |
bf5a755f JC |
5860 | |
5861 | struct packet_offload *gro_find_complete_by_type(__be16 type) | |
5862 | { | |
5863 | struct list_head *offload_head = &offload_base; | |
5864 | struct packet_offload *ptype; | |
5865 | ||
5866 | list_for_each_entry_rcu(ptype, offload_head, list) { | |
5867 | if (ptype->type != type || !ptype->callbacks.gro_complete) | |
5868 | continue; | |
5869 | return ptype; | |
5870 | } | |
5871 | return NULL; | |
5872 | } | |
e27a2f83 | 5873 | EXPORT_SYMBOL(gro_find_complete_by_type); |
5d38a079 | 5874 | |
e44699d2 MK |
5875 | static void napi_skb_free_stolen_head(struct sk_buff *skb) |
5876 | { | |
5877 | skb_dst_drop(skb); | |
174e2381 | 5878 | skb_ext_put(skb); |
e44699d2 MK |
5879 | kmem_cache_free(skbuff_head_cache, skb); |
5880 | } | |
5881 | ||
6570bc79 AL |
5882 | static gro_result_t napi_skb_finish(struct napi_struct *napi, |
5883 | struct sk_buff *skb, | |
5884 | gro_result_t ret) | |
5d38a079 | 5885 | { |
5d0d9be8 HX |
5886 | switch (ret) { |
5887 | case GRO_NORMAL: | |
6570bc79 | 5888 | gro_normal_one(napi, skb); |
c7c4b3b6 | 5889 | break; |
5d38a079 | 5890 | |
5d0d9be8 | 5891 | case GRO_DROP: |
5d38a079 HX |
5892 | kfree_skb(skb); |
5893 | break; | |
5b252f0c | 5894 | |
daa86548 | 5895 | case GRO_MERGED_FREE: |
e44699d2 MK |
5896 | if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) |
5897 | napi_skb_free_stolen_head(skb); | |
5898 | else | |
d7e8883c | 5899 | __kfree_skb(skb); |
daa86548 ED |
5900 | break; |
5901 | ||
5b252f0c BH |
5902 | case GRO_HELD: |
5903 | case GRO_MERGED: | |
25393d3f | 5904 | case GRO_CONSUMED: |
5b252f0c | 5905 | break; |
5d38a079 HX |
5906 | } |
5907 | ||
c7c4b3b6 | 5908 | return ret; |
5d0d9be8 | 5909 | } |
5d0d9be8 | 5910 | |
c7c4b3b6 | 5911 | gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb) |
5d0d9be8 | 5912 | { |
b0e3f1bd GB |
5913 | gro_result_t ret; |
5914 | ||
93f93a44 | 5915 | skb_mark_napi_id(skb, napi); |
ae78dbfa | 5916 | trace_napi_gro_receive_entry(skb); |
86911732 | 5917 | |
a50e233c ED |
5918 | skb_gro_reset_offset(skb); |
5919 | ||
6570bc79 | 5920 | ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb)); |
b0e3f1bd GB |
5921 | trace_napi_gro_receive_exit(ret); |
5922 | ||
5923 | return ret; | |
d565b0a1 HX |
5924 | } |
5925 | EXPORT_SYMBOL(napi_gro_receive); | |
5926 | ||
d0c2b0d2 | 5927 | static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb) |
96e93eab | 5928 | { |
93a35f59 ED |
5929 | if (unlikely(skb->pfmemalloc)) { |
5930 | consume_skb(skb); | |
5931 | return; | |
5932 | } | |
96e93eab | 5933 | __skb_pull(skb, skb_headlen(skb)); |
2a2a459e ED |
5934 | /* restore the reserve we had after netdev_alloc_skb_ip_align() */ |
5935 | skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb)); | |
b1817524 | 5936 | __vlan_hwaccel_clear_tag(skb); |
66c46d74 | 5937 | skb->dev = napi->dev; |
6d152e23 | 5938 | skb->skb_iif = 0; |
33d9a2c7 ED |
5939 | |
5940 | /* eth_type_trans() assumes pkt_type is PACKET_HOST */ | |
5941 | skb->pkt_type = PACKET_HOST; | |
5942 | ||
c3caf119 JC |
5943 | skb->encapsulation = 0; |
5944 | skb_shinfo(skb)->gso_type = 0; | |
e33d0ba8 | 5945 | skb->truesize = SKB_TRUESIZE(skb_end_offset(skb)); |
174e2381 | 5946 | skb_ext_reset(skb); |
96e93eab HX |
5947 | |
5948 | napi->skb = skb; | |
5949 | } | |
96e93eab | 5950 | |
76620aaf | 5951 | struct sk_buff *napi_get_frags(struct napi_struct *napi) |
5d38a079 | 5952 | { |
5d38a079 | 5953 | struct sk_buff *skb = napi->skb; |
5d38a079 HX |
5954 | |
5955 | if (!skb) { | |
fd11a83d | 5956 | skb = napi_alloc_skb(napi, GRO_MAX_HEAD); |
e2f9dc3b ED |
5957 | if (skb) { |
5958 | napi->skb = skb; | |
5959 | skb_mark_napi_id(skb, napi); | |
5960 | } | |
80595d59 | 5961 | } |
96e93eab HX |
5962 | return skb; |
5963 | } | |
76620aaf | 5964 | EXPORT_SYMBOL(napi_get_frags); |
96e93eab | 5965 | |
a50e233c ED |
5966 | static gro_result_t napi_frags_finish(struct napi_struct *napi, |
5967 | struct sk_buff *skb, | |
5968 | gro_result_t ret) | |
96e93eab | 5969 | { |
5d0d9be8 HX |
5970 | switch (ret) { |
5971 | case GRO_NORMAL: | |
a50e233c ED |
5972 | case GRO_HELD: |
5973 | __skb_push(skb, ETH_HLEN); | |
5974 | skb->protocol = eth_type_trans(skb, skb->dev); | |
323ebb61 EC |
5975 | if (ret == GRO_NORMAL) |
5976 | gro_normal_one(napi, skb); | |
86911732 | 5977 | break; |
5d38a079 | 5978 | |
5d0d9be8 | 5979 | case GRO_DROP: |
5d0d9be8 HX |
5980 | napi_reuse_skb(napi, skb); |
5981 | break; | |
5b252f0c | 5982 | |
e44699d2 MK |
5983 | case GRO_MERGED_FREE: |
5984 | if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD) | |
5985 | napi_skb_free_stolen_head(skb); | |
5986 | else | |
5987 | napi_reuse_skb(napi, skb); | |
5988 | break; | |
5989 | ||
5b252f0c | 5990 | case GRO_MERGED: |
25393d3f | 5991 | case GRO_CONSUMED: |
5b252f0c | 5992 | break; |
5d0d9be8 | 5993 | } |
5d38a079 | 5994 | |
c7c4b3b6 | 5995 | return ret; |
5d38a079 | 5996 | } |
5d0d9be8 | 5997 | |
a50e233c ED |
5998 | /* Upper GRO stack assumes network header starts at gro_offset=0 |
5999 | * Drivers could call both napi_gro_frags() and napi_gro_receive() | |
6000 | * We copy ethernet header into skb->data to have a common layout. | |
6001 | */ | |
4adb9c4a | 6002 | static struct sk_buff *napi_frags_skb(struct napi_struct *napi) |
76620aaf HX |
6003 | { |
6004 | struct sk_buff *skb = napi->skb; | |
a50e233c ED |
6005 | const struct ethhdr *eth; |
6006 | unsigned int hlen = sizeof(*eth); | |
76620aaf HX |
6007 | |
6008 | napi->skb = NULL; | |
6009 | ||
a50e233c ED |
6010 | skb_reset_mac_header(skb); |
6011 | skb_gro_reset_offset(skb); | |
6012 | ||
a50e233c ED |
6013 | if (unlikely(skb_gro_header_hard(skb, hlen))) { |
6014 | eth = skb_gro_header_slow(skb, hlen, 0); | |
6015 | if (unlikely(!eth)) { | |
4da46ceb AC |
6016 | net_warn_ratelimited("%s: dropping impossible skb from %s\n", |
6017 | __func__, napi->dev->name); | |
a50e233c ED |
6018 | napi_reuse_skb(napi, skb); |
6019 | return NULL; | |
6020 | } | |
6021 | } else { | |
a4270d67 | 6022 | eth = (const struct ethhdr *)skb->data; |
a50e233c ED |
6023 | gro_pull_from_frag0(skb, hlen); |
6024 | NAPI_GRO_CB(skb)->frag0 += hlen; | |
6025 | NAPI_GRO_CB(skb)->frag0_len -= hlen; | |
76620aaf | 6026 | } |
a50e233c ED |
6027 | __skb_pull(skb, hlen); |
6028 | ||
6029 | /* | |
6030 | * This works because the only protocols we care about don't require | |
6031 | * special handling. | |
6032 | * We'll fix it up properly in napi_frags_finish() | |
6033 | */ | |
6034 | skb->protocol = eth->h_proto; | |
76620aaf | 6035 | |
76620aaf HX |
6036 | return skb; |
6037 | } | |
76620aaf | 6038 | |
c7c4b3b6 | 6039 | gro_result_t napi_gro_frags(struct napi_struct *napi) |
5d0d9be8 | 6040 | { |
b0e3f1bd | 6041 | gro_result_t ret; |
76620aaf | 6042 | struct sk_buff *skb = napi_frags_skb(napi); |
5d0d9be8 HX |
6043 | |
6044 | if (!skb) | |
c7c4b3b6 | 6045 | return GRO_DROP; |
5d0d9be8 | 6046 | |
ae78dbfa BH |
6047 | trace_napi_gro_frags_entry(skb); |
6048 | ||
b0e3f1bd GB |
6049 | ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb)); |
6050 | trace_napi_gro_frags_exit(ret); | |
6051 | ||
6052 | return ret; | |
5d0d9be8 | 6053 | } |
5d38a079 HX |
6054 | EXPORT_SYMBOL(napi_gro_frags); |
6055 | ||
573e8fca TH |
6056 | /* Compute the checksum from gro_offset and return the folded value |
6057 | * after adding in any pseudo checksum. | |
6058 | */ | |
6059 | __sum16 __skb_gro_checksum_complete(struct sk_buff *skb) | |
6060 | { | |
6061 | __wsum wsum; | |
6062 | __sum16 sum; | |
6063 | ||
6064 | wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0); | |
6065 | ||
6066 | /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */ | |
6067 | sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum)); | |
14641931 | 6068 | /* See comments in __skb_checksum_complete(). */ |
573e8fca TH |
6069 | if (likely(!sum)) { |
6070 | if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) && | |
6071 | !skb->csum_complete_sw) | |
7fe50ac8 | 6072 | netdev_rx_csum_fault(skb->dev, skb); |
573e8fca TH |
6073 | } |
6074 | ||
6075 | NAPI_GRO_CB(skb)->csum = wsum; | |
6076 | NAPI_GRO_CB(skb)->csum_valid = 1; | |
6077 | ||
6078 | return sum; | |
6079 | } | |
6080 | EXPORT_SYMBOL(__skb_gro_checksum_complete); | |
6081 | ||
773fc8f6 | 6082 | static void net_rps_send_ipi(struct softnet_data *remsd) |
6083 | { | |
6084 | #ifdef CONFIG_RPS | |
6085 | while (remsd) { | |
6086 | struct softnet_data *next = remsd->rps_ipi_next; | |
6087 | ||
6088 | if (cpu_online(remsd->cpu)) | |
6089 | smp_call_function_single_async(remsd->cpu, &remsd->csd); | |
6090 | remsd = next; | |
6091 | } | |
6092 | #endif | |
6093 | } | |
6094 | ||
e326bed2 | 6095 | /* |
855abcf0 | 6096 | * net_rps_action_and_irq_enable sends any pending IPI's for rps. |
e326bed2 ED |
6097 | * Note: called with local irq disabled, but exits with local irq enabled. |
6098 | */ | |
6099 | static void net_rps_action_and_irq_enable(struct softnet_data *sd) | |
6100 | { | |
6101 | #ifdef CONFIG_RPS | |
6102 | struct softnet_data *remsd = sd->rps_ipi_list; | |
6103 | ||
6104 | if (remsd) { | |
6105 | sd->rps_ipi_list = NULL; | |
6106 | ||
6107 | local_irq_enable(); | |
6108 | ||
6109 | /* Send pending IPI's to kick RPS processing on remote cpus. */ | |
773fc8f6 | 6110 | net_rps_send_ipi(remsd); |
e326bed2 ED |
6111 | } else |
6112 | #endif | |
6113 | local_irq_enable(); | |
6114 | } | |
6115 | ||
d75b1ade ED |
6116 | static bool sd_has_rps_ipi_waiting(struct softnet_data *sd) |
6117 | { | |
6118 | #ifdef CONFIG_RPS | |
6119 | return sd->rps_ipi_list != NULL; | |
6120 | #else | |
6121 | return false; | |
6122 | #endif | |
6123 | } | |
6124 | ||
bea3348e | 6125 | static int process_backlog(struct napi_struct *napi, int quota) |
1da177e4 | 6126 | { |
eecfd7c4 | 6127 | struct softnet_data *sd = container_of(napi, struct softnet_data, backlog); |
145dd5f9 PA |
6128 | bool again = true; |
6129 | int work = 0; | |
1da177e4 | 6130 | |
e326bed2 ED |
6131 | /* Check if we have pending ipi, its better to send them now, |
6132 | * not waiting net_rx_action() end. | |
6133 | */ | |
d75b1ade | 6134 | if (sd_has_rps_ipi_waiting(sd)) { |
e326bed2 ED |
6135 | local_irq_disable(); |
6136 | net_rps_action_and_irq_enable(sd); | |
6137 | } | |
d75b1ade | 6138 | |
3d48b53f | 6139 | napi->weight = dev_rx_weight; |
145dd5f9 | 6140 | while (again) { |
1da177e4 | 6141 | struct sk_buff *skb; |
6e7676c1 CG |
6142 | |
6143 | while ((skb = __skb_dequeue(&sd->process_queue))) { | |
2c17d27c | 6144 | rcu_read_lock(); |
6e7676c1 | 6145 | __netif_receive_skb(skb); |
2c17d27c | 6146 | rcu_read_unlock(); |
76cc8b13 | 6147 | input_queue_head_incr(sd); |
145dd5f9 | 6148 | if (++work >= quota) |
76cc8b13 | 6149 | return work; |
145dd5f9 | 6150 | |
6e7676c1 | 6151 | } |
1da177e4 | 6152 | |
145dd5f9 | 6153 | local_irq_disable(); |
e36fa2f7 | 6154 | rps_lock(sd); |
11ef7a89 | 6155 | if (skb_queue_empty(&sd->input_pkt_queue)) { |
eecfd7c4 ED |
6156 | /* |
6157 | * Inline a custom version of __napi_complete(). | |
6158 | * only current cpu owns and manipulates this napi, | |
11ef7a89 TH |
6159 | * and NAPI_STATE_SCHED is the only possible flag set |
6160 | * on backlog. | |
6161 | * We can use a plain write instead of clear_bit(), | |
eecfd7c4 ED |
6162 | * and we dont need an smp_mb() memory barrier. |
6163 | */ | |
eecfd7c4 | 6164 | napi->state = 0; |
145dd5f9 PA |
6165 | again = false; |
6166 | } else { | |
6167 | skb_queue_splice_tail_init(&sd->input_pkt_queue, | |
6168 | &sd->process_queue); | |
bea3348e | 6169 | } |
e36fa2f7 | 6170 | rps_unlock(sd); |
145dd5f9 | 6171 | local_irq_enable(); |
6e7676c1 | 6172 | } |
1da177e4 | 6173 | |
bea3348e SH |
6174 | return work; |
6175 | } | |
1da177e4 | 6176 | |
bea3348e SH |
6177 | /** |
6178 | * __napi_schedule - schedule for receive | |
c4ea43c5 | 6179 | * @n: entry to schedule |
bea3348e | 6180 | * |
bc9ad166 ED |
6181 | * The entry's receive function will be scheduled to run. |
6182 | * Consider using __napi_schedule_irqoff() if hard irqs are masked. | |
bea3348e | 6183 | */ |
b5606c2d | 6184 | void __napi_schedule(struct napi_struct *n) |
bea3348e SH |
6185 | { |
6186 | unsigned long flags; | |
1da177e4 | 6187 | |
bea3348e | 6188 | local_irq_save(flags); |
903ceff7 | 6189 | ____napi_schedule(this_cpu_ptr(&softnet_data), n); |
bea3348e | 6190 | local_irq_restore(flags); |
1da177e4 | 6191 | } |
bea3348e SH |
6192 | EXPORT_SYMBOL(__napi_schedule); |
6193 | ||
39e6c820 ED |
6194 | /** |
6195 | * napi_schedule_prep - check if napi can be scheduled | |
6196 | * @n: napi context | |
6197 | * | |
6198 | * Test if NAPI routine is already running, and if not mark | |
6199 | * it as running. This is used as a condition variable | |
6200 | * insure only one NAPI poll instance runs. We also make | |
6201 | * sure there is no pending NAPI disable. | |
6202 | */ | |
6203 | bool napi_schedule_prep(struct napi_struct *n) | |
6204 | { | |
6205 | unsigned long val, new; | |
6206 | ||
6207 | do { | |
6208 | val = READ_ONCE(n->state); | |
6209 | if (unlikely(val & NAPIF_STATE_DISABLE)) | |
6210 | return false; | |
6211 | new = val | NAPIF_STATE_SCHED; | |
6212 | ||
6213 | /* Sets STATE_MISSED bit if STATE_SCHED was already set | |
6214 | * This was suggested by Alexander Duyck, as compiler | |
6215 | * emits better code than : | |
6216 | * if (val & NAPIF_STATE_SCHED) | |
6217 | * new |= NAPIF_STATE_MISSED; | |
6218 | */ | |
6219 | new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED * | |
6220 | NAPIF_STATE_MISSED; | |
6221 | } while (cmpxchg(&n->state, val, new) != val); | |
6222 | ||
6223 | return !(val & NAPIF_STATE_SCHED); | |
6224 | } | |
6225 | EXPORT_SYMBOL(napi_schedule_prep); | |
6226 | ||
bc9ad166 ED |
6227 | /** |
6228 | * __napi_schedule_irqoff - schedule for receive | |
6229 | * @n: entry to schedule | |
6230 | * | |
6231 | * Variant of __napi_schedule() assuming hard irqs are masked | |
6232 | */ | |
6233 | void __napi_schedule_irqoff(struct napi_struct *n) | |
6234 | { | |
6235 | ____napi_schedule(this_cpu_ptr(&softnet_data), n); | |
6236 | } | |
6237 | EXPORT_SYMBOL(__napi_schedule_irqoff); | |
6238 | ||
364b6055 | 6239 | bool napi_complete_done(struct napi_struct *n, int work_done) |
d565b0a1 | 6240 | { |
39e6c820 | 6241 | unsigned long flags, val, new; |
d565b0a1 HX |
6242 | |
6243 | /* | |
217f6974 ED |
6244 | * 1) Don't let napi dequeue from the cpu poll list |
6245 | * just in case its running on a different cpu. | |
6246 | * 2) If we are busy polling, do nothing here, we have | |
6247 | * the guarantee we will be called later. | |
d565b0a1 | 6248 | */ |
217f6974 ED |
6249 | if (unlikely(n->state & (NAPIF_STATE_NPSVC | |
6250 | NAPIF_STATE_IN_BUSY_POLL))) | |
364b6055 | 6251 | return false; |
d565b0a1 | 6252 | |
d9f37d01 | 6253 | if (n->gro_bitmask) { |
3b47d303 | 6254 | unsigned long timeout = 0; |
d75b1ade | 6255 | |
3b47d303 ED |
6256 | if (work_done) |
6257 | timeout = n->dev->gro_flush_timeout; | |
6258 | ||
605108ac PA |
6259 | /* When the NAPI instance uses a timeout and keeps postponing |
6260 | * it, we need to bound somehow the time packets are kept in | |
6261 | * the GRO layer | |
6262 | */ | |
6263 | napi_gro_flush(n, !!timeout); | |
3b47d303 ED |
6264 | if (timeout) |
6265 | hrtimer_start(&n->timer, ns_to_ktime(timeout), | |
6266 | HRTIMER_MODE_REL_PINNED); | |
3b47d303 | 6267 | } |
c8079432 MM |
6268 | |
6269 | gro_normal_list(n); | |
6270 | ||
02c1602e | 6271 | if (unlikely(!list_empty(&n->poll_list))) { |
d75b1ade ED |
6272 | /* If n->poll_list is not empty, we need to mask irqs */ |
6273 | local_irq_save(flags); | |
02c1602e | 6274 | list_del_init(&n->poll_list); |
d75b1ade ED |
6275 | local_irq_restore(flags); |
6276 | } | |
39e6c820 ED |
6277 | |
6278 | do { | |
6279 | val = READ_ONCE(n->state); | |
6280 | ||
6281 | WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED)); | |
6282 | ||
6283 | new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED); | |
6284 | ||
6285 | /* If STATE_MISSED was set, leave STATE_SCHED set, | |
6286 | * because we will call napi->poll() one more time. | |
6287 | * This C code was suggested by Alexander Duyck to help gcc. | |
6288 | */ | |
6289 | new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED * | |
6290 | NAPIF_STATE_SCHED; | |
6291 | } while (cmpxchg(&n->state, val, new) != val); | |
6292 | ||
6293 | if (unlikely(val & NAPIF_STATE_MISSED)) { | |
6294 | __napi_schedule(n); | |
6295 | return false; | |
6296 | } | |
6297 | ||
364b6055 | 6298 | return true; |
d565b0a1 | 6299 | } |
3b47d303 | 6300 | EXPORT_SYMBOL(napi_complete_done); |
d565b0a1 | 6301 | |
af12fa6e | 6302 | /* must be called under rcu_read_lock(), as we dont take a reference */ |
02d62e86 | 6303 | static struct napi_struct *napi_by_id(unsigned int napi_id) |
af12fa6e ET |
6304 | { |
6305 | unsigned int hash = napi_id % HASH_SIZE(napi_hash); | |
6306 | struct napi_struct *napi; | |
6307 | ||
6308 | hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node) | |
6309 | if (napi->napi_id == napi_id) | |
6310 | return napi; | |
6311 | ||
6312 | return NULL; | |
6313 | } | |
02d62e86 ED |
6314 | |
6315 | #if defined(CONFIG_NET_RX_BUSY_POLL) | |
217f6974 | 6316 | |
ce6aea93 | 6317 | #define BUSY_POLL_BUDGET 8 |
217f6974 ED |
6318 | |
6319 | static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock) | |
6320 | { | |
6321 | int rc; | |
6322 | ||
39e6c820 ED |
6323 | /* Busy polling means there is a high chance device driver hard irq |
6324 | * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was | |
6325 | * set in napi_schedule_prep(). | |
6326 | * Since we are about to call napi->poll() once more, we can safely | |
6327 | * clear NAPI_STATE_MISSED. | |
6328 | * | |
6329 | * Note: x86 could use a single "lock and ..." instruction | |
6330 | * to perform these two clear_bit() | |
6331 | */ | |
6332 | clear_bit(NAPI_STATE_MISSED, &napi->state); | |
217f6974 ED |
6333 | clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state); |
6334 | ||
6335 | local_bh_disable(); | |
6336 | ||
6337 | /* All we really want here is to re-enable device interrupts. | |
6338 | * Ideally, a new ndo_busy_poll_stop() could avoid another round. | |
6339 | */ | |
6340 | rc = napi->poll(napi, BUSY_POLL_BUDGET); | |
323ebb61 EC |
6341 | /* We can't gro_normal_list() here, because napi->poll() might have |
6342 | * rearmed the napi (napi_complete_done()) in which case it could | |
6343 | * already be running on another CPU. | |
6344 | */ | |
1e22391e | 6345 | trace_napi_poll(napi, rc, BUSY_POLL_BUDGET); |
217f6974 | 6346 | netpoll_poll_unlock(have_poll_lock); |
323ebb61 EC |
6347 | if (rc == BUSY_POLL_BUDGET) { |
6348 | /* As the whole budget was spent, we still own the napi so can | |
6349 | * safely handle the rx_list. | |
6350 | */ | |
6351 | gro_normal_list(napi); | |
217f6974 | 6352 | __napi_schedule(napi); |
323ebb61 | 6353 | } |
217f6974 | 6354 | local_bh_enable(); |
217f6974 ED |
6355 | } |
6356 | ||
7db6b048 SS |
6357 | void napi_busy_loop(unsigned int napi_id, |
6358 | bool (*loop_end)(void *, unsigned long), | |
6359 | void *loop_end_arg) | |
02d62e86 | 6360 | { |
7db6b048 | 6361 | unsigned long start_time = loop_end ? busy_loop_current_time() : 0; |
217f6974 | 6362 | int (*napi_poll)(struct napi_struct *napi, int budget); |
217f6974 | 6363 | void *have_poll_lock = NULL; |
02d62e86 | 6364 | struct napi_struct *napi; |
217f6974 ED |
6365 | |
6366 | restart: | |
217f6974 | 6367 | napi_poll = NULL; |
02d62e86 | 6368 | |
2a028ecb | 6369 | rcu_read_lock(); |
02d62e86 | 6370 | |
545cd5e5 | 6371 | napi = napi_by_id(napi_id); |
02d62e86 ED |
6372 | if (!napi) |
6373 | goto out; | |
6374 | ||
217f6974 ED |
6375 | preempt_disable(); |
6376 | for (;;) { | |
2b5cd0df AD |
6377 | int work = 0; |
6378 | ||
2a028ecb | 6379 | local_bh_disable(); |
217f6974 ED |
6380 | if (!napi_poll) { |
6381 | unsigned long val = READ_ONCE(napi->state); | |
6382 | ||
6383 | /* If multiple threads are competing for this napi, | |
6384 | * we avoid dirtying napi->state as much as we can. | |
6385 | */ | |
6386 | if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED | | |
6387 | NAPIF_STATE_IN_BUSY_POLL)) | |
6388 | goto count; | |
6389 | if (cmpxchg(&napi->state, val, | |
6390 | val | NAPIF_STATE_IN_BUSY_POLL | | |
6391 | NAPIF_STATE_SCHED) != val) | |
6392 | goto count; | |
6393 | have_poll_lock = netpoll_poll_lock(napi); | |
6394 | napi_poll = napi->poll; | |
6395 | } | |
2b5cd0df AD |
6396 | work = napi_poll(napi, BUSY_POLL_BUDGET); |
6397 | trace_napi_poll(napi, work, BUSY_POLL_BUDGET); | |
323ebb61 | 6398 | gro_normal_list(napi); |
217f6974 | 6399 | count: |
2b5cd0df | 6400 | if (work > 0) |
7db6b048 | 6401 | __NET_ADD_STATS(dev_net(napi->dev), |
2b5cd0df | 6402 | LINUX_MIB_BUSYPOLLRXPACKETS, work); |
2a028ecb | 6403 | local_bh_enable(); |
02d62e86 | 6404 | |
7db6b048 | 6405 | if (!loop_end || loop_end(loop_end_arg, start_time)) |
217f6974 | 6406 | break; |
02d62e86 | 6407 | |
217f6974 ED |
6408 | if (unlikely(need_resched())) { |
6409 | if (napi_poll) | |
6410 | busy_poll_stop(napi, have_poll_lock); | |
6411 | preempt_enable(); | |
6412 | rcu_read_unlock(); | |
6413 | cond_resched(); | |
7db6b048 | 6414 | if (loop_end(loop_end_arg, start_time)) |
2b5cd0df | 6415 | return; |
217f6974 ED |
6416 | goto restart; |
6417 | } | |
6cdf89b1 | 6418 | cpu_relax(); |
217f6974 ED |
6419 | } |
6420 | if (napi_poll) | |
6421 | busy_poll_stop(napi, have_poll_lock); | |
6422 | preempt_enable(); | |
02d62e86 | 6423 | out: |
2a028ecb | 6424 | rcu_read_unlock(); |
02d62e86 | 6425 | } |
7db6b048 | 6426 | EXPORT_SYMBOL(napi_busy_loop); |
02d62e86 ED |
6427 | |
6428 | #endif /* CONFIG_NET_RX_BUSY_POLL */ | |
af12fa6e | 6429 | |
149d6ad8 | 6430 | static void napi_hash_add(struct napi_struct *napi) |
af12fa6e | 6431 | { |
d64b5e85 ED |
6432 | if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) || |
6433 | test_and_set_bit(NAPI_STATE_HASHED, &napi->state)) | |
52bd2d62 | 6434 | return; |
af12fa6e | 6435 | |
52bd2d62 | 6436 | spin_lock(&napi_hash_lock); |
af12fa6e | 6437 | |
545cd5e5 | 6438 | /* 0..NR_CPUS range is reserved for sender_cpu use */ |
52bd2d62 | 6439 | do { |
545cd5e5 AD |
6440 | if (unlikely(++napi_gen_id < MIN_NAPI_ID)) |
6441 | napi_gen_id = MIN_NAPI_ID; | |
52bd2d62 ED |
6442 | } while (napi_by_id(napi_gen_id)); |
6443 | napi->napi_id = napi_gen_id; | |
af12fa6e | 6444 | |
52bd2d62 ED |
6445 | hlist_add_head_rcu(&napi->napi_hash_node, |
6446 | &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]); | |
af12fa6e | 6447 | |
52bd2d62 | 6448 | spin_unlock(&napi_hash_lock); |
af12fa6e | 6449 | } |
af12fa6e ET |
6450 | |
6451 | /* Warning : caller is responsible to make sure rcu grace period | |
6452 | * is respected before freeing memory containing @napi | |
6453 | */ | |
34cbe27e | 6454 | bool napi_hash_del(struct napi_struct *napi) |
af12fa6e | 6455 | { |
34cbe27e ED |
6456 | bool rcu_sync_needed = false; |
6457 | ||
af12fa6e ET |
6458 | spin_lock(&napi_hash_lock); |
6459 | ||
34cbe27e ED |
6460 | if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) { |
6461 | rcu_sync_needed = true; | |
af12fa6e | 6462 | hlist_del_rcu(&napi->napi_hash_node); |
34cbe27e | 6463 | } |
af12fa6e | 6464 | spin_unlock(&napi_hash_lock); |
34cbe27e | 6465 | return rcu_sync_needed; |
af12fa6e ET |
6466 | } |
6467 | EXPORT_SYMBOL_GPL(napi_hash_del); | |
6468 | ||
3b47d303 ED |
6469 | static enum hrtimer_restart napi_watchdog(struct hrtimer *timer) |
6470 | { | |
6471 | struct napi_struct *napi; | |
6472 | ||
6473 | napi = container_of(timer, struct napi_struct, timer); | |
39e6c820 ED |
6474 | |
6475 | /* Note : we use a relaxed variant of napi_schedule_prep() not setting | |
6476 | * NAPI_STATE_MISSED, since we do not react to a device IRQ. | |
6477 | */ | |
d9f37d01 | 6478 | if (napi->gro_bitmask && !napi_disable_pending(napi) && |
39e6c820 ED |
6479 | !test_and_set_bit(NAPI_STATE_SCHED, &napi->state)) |
6480 | __napi_schedule_irqoff(napi); | |
3b47d303 ED |
6481 | |
6482 | return HRTIMER_NORESTART; | |
6483 | } | |
6484 | ||
7c4ec749 | 6485 | static void init_gro_hash(struct napi_struct *napi) |
d565b0a1 | 6486 | { |
07d78363 DM |
6487 | int i; |
6488 | ||
6312fe77 LR |
6489 | for (i = 0; i < GRO_HASH_BUCKETS; i++) { |
6490 | INIT_LIST_HEAD(&napi->gro_hash[i].list); | |
6491 | napi->gro_hash[i].count = 0; | |
6492 | } | |
7c4ec749 DM |
6493 | napi->gro_bitmask = 0; |
6494 | } | |
6495 | ||
6496 | void netif_napi_add(struct net_device *dev, struct napi_struct *napi, | |
6497 | int (*poll)(struct napi_struct *, int), int weight) | |
6498 | { | |
6499 | INIT_LIST_HEAD(&napi->poll_list); | |
6500 | hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED); | |
6501 | napi->timer.function = napi_watchdog; | |
6502 | init_gro_hash(napi); | |
5d38a079 | 6503 | napi->skb = NULL; |
323ebb61 EC |
6504 | INIT_LIST_HEAD(&napi->rx_list); |
6505 | napi->rx_count = 0; | |
d565b0a1 | 6506 | napi->poll = poll; |
82dc3c63 | 6507 | if (weight > NAPI_POLL_WEIGHT) |
bf29e9e9 QC |
6508 | netdev_err_once(dev, "%s() called with weight %d\n", __func__, |
6509 | weight); | |
d565b0a1 HX |
6510 | napi->weight = weight; |
6511 | list_add(&napi->dev_list, &dev->napi_list); | |
d565b0a1 | 6512 | napi->dev = dev; |
5d38a079 | 6513 | #ifdef CONFIG_NETPOLL |
d565b0a1 HX |
6514 | napi->poll_owner = -1; |
6515 | #endif | |
6516 | set_bit(NAPI_STATE_SCHED, &napi->state); | |
93d05d4a | 6517 | napi_hash_add(napi); |
d565b0a1 HX |
6518 | } |
6519 | EXPORT_SYMBOL(netif_napi_add); | |
6520 | ||
3b47d303 ED |
6521 | void napi_disable(struct napi_struct *n) |
6522 | { | |
6523 | might_sleep(); | |
6524 | set_bit(NAPI_STATE_DISABLE, &n->state); | |
6525 | ||
6526 | while (test_and_set_bit(NAPI_STATE_SCHED, &n->state)) | |
6527 | msleep(1); | |
2d8bff12 NH |
6528 | while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state)) |
6529 | msleep(1); | |
3b47d303 ED |
6530 | |
6531 | hrtimer_cancel(&n->timer); | |
6532 | ||
6533 | clear_bit(NAPI_STATE_DISABLE, &n->state); | |
6534 | } | |
6535 | EXPORT_SYMBOL(napi_disable); | |
6536 | ||
07d78363 | 6537 | static void flush_gro_hash(struct napi_struct *napi) |
d4546c25 | 6538 | { |
07d78363 | 6539 | int i; |
d4546c25 | 6540 | |
07d78363 DM |
6541 | for (i = 0; i < GRO_HASH_BUCKETS; i++) { |
6542 | struct sk_buff *skb, *n; | |
6543 | ||
6312fe77 | 6544 | list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list) |
07d78363 | 6545 | kfree_skb(skb); |
6312fe77 | 6546 | napi->gro_hash[i].count = 0; |
07d78363 | 6547 | } |
d4546c25 DM |
6548 | } |
6549 | ||
93d05d4a | 6550 | /* Must be called in process context */ |
d565b0a1 HX |
6551 | void netif_napi_del(struct napi_struct *napi) |
6552 | { | |
93d05d4a ED |
6553 | might_sleep(); |
6554 | if (napi_hash_del(napi)) | |
6555 | synchronize_net(); | |
d7b06636 | 6556 | list_del_init(&napi->dev_list); |
76620aaf | 6557 | napi_free_frags(napi); |
d565b0a1 | 6558 | |
07d78363 | 6559 | flush_gro_hash(napi); |
d9f37d01 | 6560 | napi->gro_bitmask = 0; |
d565b0a1 HX |
6561 | } |
6562 | EXPORT_SYMBOL(netif_napi_del); | |
6563 | ||
726ce70e HX |
6564 | static int napi_poll(struct napi_struct *n, struct list_head *repoll) |
6565 | { | |
6566 | void *have; | |
6567 | int work, weight; | |
6568 | ||
6569 | list_del_init(&n->poll_list); | |
6570 | ||
6571 | have = netpoll_poll_lock(n); | |
6572 | ||
6573 | weight = n->weight; | |
6574 | ||
6575 | /* This NAPI_STATE_SCHED test is for avoiding a race | |
6576 | * with netpoll's poll_napi(). Only the entity which | |
6577 | * obtains the lock and sees NAPI_STATE_SCHED set will | |
6578 | * actually make the ->poll() call. Therefore we avoid | |
6579 | * accidentally calling ->poll() when NAPI is not scheduled. | |
6580 | */ | |
6581 | work = 0; | |
6582 | if (test_bit(NAPI_STATE_SCHED, &n->state)) { | |
6583 | work = n->poll(n, weight); | |
1db19db7 | 6584 | trace_napi_poll(n, work, weight); |
726ce70e HX |
6585 | } |
6586 | ||
6587 | WARN_ON_ONCE(work > weight); | |
6588 | ||
6589 | if (likely(work < weight)) | |
6590 | goto out_unlock; | |
6591 | ||
6592 | /* Drivers must not modify the NAPI state if they | |
6593 | * consume the entire weight. In such cases this code | |
6594 | * still "owns" the NAPI instance and therefore can | |
6595 | * move the instance around on the list at-will. | |
6596 | */ | |
6597 | if (unlikely(napi_disable_pending(n))) { | |
6598 | napi_complete(n); | |
6599 | goto out_unlock; | |
6600 | } | |
6601 | ||
d9f37d01 | 6602 | if (n->gro_bitmask) { |
726ce70e HX |
6603 | /* flush too old packets |
6604 | * If HZ < 1000, flush all packets. | |
6605 | */ | |
6606 | napi_gro_flush(n, HZ >= 1000); | |
6607 | } | |
6608 | ||
c8079432 MM |
6609 | gro_normal_list(n); |
6610 | ||
001ce546 HX |
6611 | /* Some drivers may have called napi_schedule |
6612 | * prior to exhausting their budget. | |
6613 | */ | |
6614 | if (unlikely(!list_empty(&n->poll_list))) { | |
6615 | pr_warn_once("%s: Budget exhausted after napi rescheduled\n", | |
6616 | n->dev ? n->dev->name : "backlog"); | |
6617 | goto out_unlock; | |
6618 | } | |
6619 | ||
726ce70e HX |
6620 | list_add_tail(&n->poll_list, repoll); |
6621 | ||
6622 | out_unlock: | |
6623 | netpoll_poll_unlock(have); | |
6624 | ||
6625 | return work; | |
6626 | } | |
6627 | ||
0766f788 | 6628 | static __latent_entropy void net_rx_action(struct softirq_action *h) |
1da177e4 | 6629 | { |
903ceff7 | 6630 | struct softnet_data *sd = this_cpu_ptr(&softnet_data); |
7acf8a1e MW |
6631 | unsigned long time_limit = jiffies + |
6632 | usecs_to_jiffies(netdev_budget_usecs); | |
51b0bded | 6633 | int budget = netdev_budget; |
d75b1ade ED |
6634 | LIST_HEAD(list); |
6635 | LIST_HEAD(repoll); | |
53fb95d3 | 6636 | |
1da177e4 | 6637 | local_irq_disable(); |
d75b1ade ED |
6638 | list_splice_init(&sd->poll_list, &list); |
6639 | local_irq_enable(); | |
1da177e4 | 6640 | |
ceb8d5bf | 6641 | for (;;) { |
bea3348e | 6642 | struct napi_struct *n; |
1da177e4 | 6643 | |
ceb8d5bf HX |
6644 | if (list_empty(&list)) { |
6645 | if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll)) | |
f52dffe0 | 6646 | goto out; |
ceb8d5bf HX |
6647 | break; |
6648 | } | |
6649 | ||
6bd373eb HX |
6650 | n = list_first_entry(&list, struct napi_struct, poll_list); |
6651 | budget -= napi_poll(n, &repoll); | |
6652 | ||
d75b1ade | 6653 | /* If softirq window is exhausted then punt. |
24f8b238 SH |
6654 | * Allow this to run for 2 jiffies since which will allow |
6655 | * an average latency of 1.5/HZ. | |
bea3348e | 6656 | */ |
ceb8d5bf HX |
6657 | if (unlikely(budget <= 0 || |
6658 | time_after_eq(jiffies, time_limit))) { | |
6659 | sd->time_squeeze++; | |
6660 | break; | |
6661 | } | |
1da177e4 | 6662 | } |
d75b1ade | 6663 | |
d75b1ade ED |
6664 | local_irq_disable(); |
6665 | ||
6666 | list_splice_tail_init(&sd->poll_list, &list); | |
6667 | list_splice_tail(&repoll, &list); | |
6668 | list_splice(&list, &sd->poll_list); | |
6669 | if (!list_empty(&sd->poll_list)) | |
6670 | __raise_softirq_irqoff(NET_RX_SOFTIRQ); | |
6671 | ||
e326bed2 | 6672 | net_rps_action_and_irq_enable(sd); |
f52dffe0 ED |
6673 | out: |
6674 | __kfree_skb_flush(); | |
1da177e4 LT |
6675 | } |
6676 | ||
aa9d8560 | 6677 | struct netdev_adjacent { |
9ff162a8 | 6678 | struct net_device *dev; |
5d261913 VF |
6679 | |
6680 | /* upper master flag, there can only be one master device per list */ | |
9ff162a8 | 6681 | bool master; |
5d261913 | 6682 | |
32b6d34f TY |
6683 | /* lookup ignore flag */ |
6684 | bool ignore; | |
6685 | ||
5d261913 VF |
6686 | /* counter for the number of times this device was added to us */ |
6687 | u16 ref_nr; | |
6688 | ||
402dae96 VF |
6689 | /* private field for the users */ |
6690 | void *private; | |
6691 | ||
9ff162a8 JP |
6692 | struct list_head list; |
6693 | struct rcu_head rcu; | |
9ff162a8 JP |
6694 | }; |
6695 | ||
6ea29da1 | 6696 | static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev, |
2f268f12 | 6697 | struct list_head *adj_list) |
9ff162a8 | 6698 | { |
5d261913 | 6699 | struct netdev_adjacent *adj; |
5d261913 | 6700 | |
2f268f12 | 6701 | list_for_each_entry(adj, adj_list, list) { |
5d261913 VF |
6702 | if (adj->dev == adj_dev) |
6703 | return adj; | |
9ff162a8 JP |
6704 | } |
6705 | return NULL; | |
6706 | } | |
6707 | ||
32b6d34f | 6708 | static int ____netdev_has_upper_dev(struct net_device *upper_dev, void *data) |
f1170fd4 DA |
6709 | { |
6710 | struct net_device *dev = data; | |
6711 | ||
6712 | return upper_dev == dev; | |
6713 | } | |
6714 | ||
9ff162a8 JP |
6715 | /** |
6716 | * netdev_has_upper_dev - Check if device is linked to an upper device | |
6717 | * @dev: device | |
6718 | * @upper_dev: upper device to check | |
6719 | * | |
6720 | * Find out if a device is linked to specified upper device and return true | |
6721 | * in case it is. Note that this checks only immediate upper device, | |
6722 | * not through a complete stack of devices. The caller must hold the RTNL lock. | |
6723 | */ | |
6724 | bool netdev_has_upper_dev(struct net_device *dev, | |
6725 | struct net_device *upper_dev) | |
6726 | { | |
6727 | ASSERT_RTNL(); | |
6728 | ||
32b6d34f | 6729 | return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev, |
f1170fd4 | 6730 | upper_dev); |
9ff162a8 JP |
6731 | } |
6732 | EXPORT_SYMBOL(netdev_has_upper_dev); | |
6733 | ||
1a3f060c DA |
6734 | /** |
6735 | * netdev_has_upper_dev_all - Check if device is linked to an upper device | |
6736 | * @dev: device | |
6737 | * @upper_dev: upper device to check | |
6738 | * | |
6739 | * Find out if a device is linked to specified upper device and return true | |
6740 | * in case it is. Note that this checks the entire upper device chain. | |
6741 | * The caller must hold rcu lock. | |
6742 | */ | |
6743 | ||
1a3f060c DA |
6744 | bool netdev_has_upper_dev_all_rcu(struct net_device *dev, |
6745 | struct net_device *upper_dev) | |
6746 | { | |
32b6d34f | 6747 | return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev, |
1a3f060c DA |
6748 | upper_dev); |
6749 | } | |
6750 | EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu); | |
6751 | ||
9ff162a8 JP |
6752 | /** |
6753 | * netdev_has_any_upper_dev - Check if device is linked to some device | |
6754 | * @dev: device | |
6755 | * | |
6756 | * Find out if a device is linked to an upper device and return true in case | |
6757 | * it is. The caller must hold the RTNL lock. | |
6758 | */ | |
25cc72a3 | 6759 | bool netdev_has_any_upper_dev(struct net_device *dev) |
9ff162a8 JP |
6760 | { |
6761 | ASSERT_RTNL(); | |
6762 | ||
f1170fd4 | 6763 | return !list_empty(&dev->adj_list.upper); |
9ff162a8 | 6764 | } |
25cc72a3 | 6765 | EXPORT_SYMBOL(netdev_has_any_upper_dev); |
9ff162a8 JP |
6766 | |
6767 | /** | |
6768 | * netdev_master_upper_dev_get - Get master upper device | |
6769 | * @dev: device | |
6770 | * | |
6771 | * Find a master upper device and return pointer to it or NULL in case | |
6772 | * it's not there. The caller must hold the RTNL lock. | |
6773 | */ | |
6774 | struct net_device *netdev_master_upper_dev_get(struct net_device *dev) | |
6775 | { | |
aa9d8560 | 6776 | struct netdev_adjacent *upper; |
9ff162a8 JP |
6777 | |
6778 | ASSERT_RTNL(); | |
6779 | ||
2f268f12 | 6780 | if (list_empty(&dev->adj_list.upper)) |
9ff162a8 JP |
6781 | return NULL; |
6782 | ||
2f268f12 | 6783 | upper = list_first_entry(&dev->adj_list.upper, |
aa9d8560 | 6784 | struct netdev_adjacent, list); |
9ff162a8 JP |
6785 | if (likely(upper->master)) |
6786 | return upper->dev; | |
6787 | return NULL; | |
6788 | } | |
6789 | EXPORT_SYMBOL(netdev_master_upper_dev_get); | |
6790 | ||
32b6d34f TY |
6791 | static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev) |
6792 | { | |
6793 | struct netdev_adjacent *upper; | |
6794 | ||
6795 | ASSERT_RTNL(); | |
6796 | ||
6797 | if (list_empty(&dev->adj_list.upper)) | |
6798 | return NULL; | |
6799 | ||
6800 | upper = list_first_entry(&dev->adj_list.upper, | |
6801 | struct netdev_adjacent, list); | |
6802 | if (likely(upper->master) && !upper->ignore) | |
6803 | return upper->dev; | |
6804 | return NULL; | |
6805 | } | |
6806 | ||
0f524a80 DA |
6807 | /** |
6808 | * netdev_has_any_lower_dev - Check if device is linked to some device | |
6809 | * @dev: device | |
6810 | * | |
6811 | * Find out if a device is linked to a lower device and return true in case | |
6812 | * it is. The caller must hold the RTNL lock. | |
6813 | */ | |
6814 | static bool netdev_has_any_lower_dev(struct net_device *dev) | |
6815 | { | |
6816 | ASSERT_RTNL(); | |
6817 | ||
6818 | return !list_empty(&dev->adj_list.lower); | |
6819 | } | |
6820 | ||
b6ccba4c VF |
6821 | void *netdev_adjacent_get_private(struct list_head *adj_list) |
6822 | { | |
6823 | struct netdev_adjacent *adj; | |
6824 | ||
6825 | adj = list_entry(adj_list, struct netdev_adjacent, list); | |
6826 | ||
6827 | return adj->private; | |
6828 | } | |
6829 | EXPORT_SYMBOL(netdev_adjacent_get_private); | |
6830 | ||
44a40855 VY |
6831 | /** |
6832 | * netdev_upper_get_next_dev_rcu - Get the next dev from upper list | |
6833 | * @dev: device | |
6834 | * @iter: list_head ** of the current position | |
6835 | * | |
6836 | * Gets the next device from the dev's upper list, starting from iter | |
6837 | * position. The caller must hold RCU read lock. | |
6838 | */ | |
6839 | struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev, | |
6840 | struct list_head **iter) | |
6841 | { | |
6842 | struct netdev_adjacent *upper; | |
6843 | ||
6844 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); | |
6845 | ||
6846 | upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
6847 | ||
6848 | if (&upper->list == &dev->adj_list.upper) | |
6849 | return NULL; | |
6850 | ||
6851 | *iter = &upper->list; | |
6852 | ||
6853 | return upper->dev; | |
6854 | } | |
6855 | EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu); | |
6856 | ||
32b6d34f TY |
6857 | static struct net_device *__netdev_next_upper_dev(struct net_device *dev, |
6858 | struct list_head **iter, | |
6859 | bool *ignore) | |
5343da4c TY |
6860 | { |
6861 | struct netdev_adjacent *upper; | |
6862 | ||
6863 | upper = list_entry((*iter)->next, struct netdev_adjacent, list); | |
6864 | ||
6865 | if (&upper->list == &dev->adj_list.upper) | |
6866 | return NULL; | |
6867 | ||
6868 | *iter = &upper->list; | |
32b6d34f | 6869 | *ignore = upper->ignore; |
5343da4c TY |
6870 | |
6871 | return upper->dev; | |
6872 | } | |
6873 | ||
1a3f060c DA |
6874 | static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev, |
6875 | struct list_head **iter) | |
6876 | { | |
6877 | struct netdev_adjacent *upper; | |
6878 | ||
6879 | WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held()); | |
6880 | ||
6881 | upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
6882 | ||
6883 | if (&upper->list == &dev->adj_list.upper) | |
6884 | return NULL; | |
6885 | ||
6886 | *iter = &upper->list; | |
6887 | ||
6888 | return upper->dev; | |
6889 | } | |
6890 | ||
32b6d34f TY |
6891 | static int __netdev_walk_all_upper_dev(struct net_device *dev, |
6892 | int (*fn)(struct net_device *dev, | |
6893 | void *data), | |
6894 | void *data) | |
5343da4c TY |
6895 | { |
6896 | struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; | |
6897 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
6898 | int ret, cur = 0; | |
32b6d34f | 6899 | bool ignore; |
5343da4c TY |
6900 | |
6901 | now = dev; | |
6902 | iter = &dev->adj_list.upper; | |
6903 | ||
6904 | while (1) { | |
6905 | if (now != dev) { | |
6906 | ret = fn(now, data); | |
6907 | if (ret) | |
6908 | return ret; | |
6909 | } | |
6910 | ||
6911 | next = NULL; | |
6912 | while (1) { | |
32b6d34f | 6913 | udev = __netdev_next_upper_dev(now, &iter, &ignore); |
5343da4c TY |
6914 | if (!udev) |
6915 | break; | |
32b6d34f TY |
6916 | if (ignore) |
6917 | continue; | |
5343da4c TY |
6918 | |
6919 | next = udev; | |
6920 | niter = &udev->adj_list.upper; | |
6921 | dev_stack[cur] = now; | |
6922 | iter_stack[cur++] = iter; | |
6923 | break; | |
6924 | } | |
6925 | ||
6926 | if (!next) { | |
6927 | if (!cur) | |
6928 | return 0; | |
6929 | next = dev_stack[--cur]; | |
6930 | niter = iter_stack[cur]; | |
6931 | } | |
6932 | ||
6933 | now = next; | |
6934 | iter = niter; | |
6935 | } | |
6936 | ||
6937 | return 0; | |
6938 | } | |
6939 | ||
1a3f060c DA |
6940 | int netdev_walk_all_upper_dev_rcu(struct net_device *dev, |
6941 | int (*fn)(struct net_device *dev, | |
6942 | void *data), | |
6943 | void *data) | |
6944 | { | |
5343da4c TY |
6945 | struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
6946 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
6947 | int ret, cur = 0; | |
1a3f060c | 6948 | |
5343da4c TY |
6949 | now = dev; |
6950 | iter = &dev->adj_list.upper; | |
1a3f060c | 6951 | |
5343da4c TY |
6952 | while (1) { |
6953 | if (now != dev) { | |
6954 | ret = fn(now, data); | |
6955 | if (ret) | |
6956 | return ret; | |
6957 | } | |
6958 | ||
6959 | next = NULL; | |
6960 | while (1) { | |
6961 | udev = netdev_next_upper_dev_rcu(now, &iter); | |
6962 | if (!udev) | |
6963 | break; | |
6964 | ||
6965 | next = udev; | |
6966 | niter = &udev->adj_list.upper; | |
6967 | dev_stack[cur] = now; | |
6968 | iter_stack[cur++] = iter; | |
6969 | break; | |
6970 | } | |
6971 | ||
6972 | if (!next) { | |
6973 | if (!cur) | |
6974 | return 0; | |
6975 | next = dev_stack[--cur]; | |
6976 | niter = iter_stack[cur]; | |
6977 | } | |
6978 | ||
6979 | now = next; | |
6980 | iter = niter; | |
1a3f060c DA |
6981 | } |
6982 | ||
6983 | return 0; | |
6984 | } | |
6985 | EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu); | |
6986 | ||
32b6d34f TY |
6987 | static bool __netdev_has_upper_dev(struct net_device *dev, |
6988 | struct net_device *upper_dev) | |
6989 | { | |
6990 | ASSERT_RTNL(); | |
6991 | ||
6992 | return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev, | |
6993 | upper_dev); | |
6994 | } | |
6995 | ||
31088a11 VF |
6996 | /** |
6997 | * netdev_lower_get_next_private - Get the next ->private from the | |
6998 | * lower neighbour list | |
6999 | * @dev: device | |
7000 | * @iter: list_head ** of the current position | |
7001 | * | |
7002 | * Gets the next netdev_adjacent->private from the dev's lower neighbour | |
7003 | * list, starting from iter position. The caller must hold either hold the | |
7004 | * RTNL lock or its own locking that guarantees that the neighbour lower | |
b469139e | 7005 | * list will remain unchanged. |
31088a11 VF |
7006 | */ |
7007 | void *netdev_lower_get_next_private(struct net_device *dev, | |
7008 | struct list_head **iter) | |
7009 | { | |
7010 | struct netdev_adjacent *lower; | |
7011 | ||
7012 | lower = list_entry(*iter, struct netdev_adjacent, list); | |
7013 | ||
7014 | if (&lower->list == &dev->adj_list.lower) | |
7015 | return NULL; | |
7016 | ||
6859e7df | 7017 | *iter = lower->list.next; |
31088a11 VF |
7018 | |
7019 | return lower->private; | |
7020 | } | |
7021 | EXPORT_SYMBOL(netdev_lower_get_next_private); | |
7022 | ||
7023 | /** | |
7024 | * netdev_lower_get_next_private_rcu - Get the next ->private from the | |
7025 | * lower neighbour list, RCU | |
7026 | * variant | |
7027 | * @dev: device | |
7028 | * @iter: list_head ** of the current position | |
7029 | * | |
7030 | * Gets the next netdev_adjacent->private from the dev's lower neighbour | |
7031 | * list, starting from iter position. The caller must hold RCU read lock. | |
7032 | */ | |
7033 | void *netdev_lower_get_next_private_rcu(struct net_device *dev, | |
7034 | struct list_head **iter) | |
7035 | { | |
7036 | struct netdev_adjacent *lower; | |
7037 | ||
7038 | WARN_ON_ONCE(!rcu_read_lock_held()); | |
7039 | ||
7040 | lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
7041 | ||
7042 | if (&lower->list == &dev->adj_list.lower) | |
7043 | return NULL; | |
7044 | ||
6859e7df | 7045 | *iter = &lower->list; |
31088a11 VF |
7046 | |
7047 | return lower->private; | |
7048 | } | |
7049 | EXPORT_SYMBOL(netdev_lower_get_next_private_rcu); | |
7050 | ||
4085ebe8 VY |
7051 | /** |
7052 | * netdev_lower_get_next - Get the next device from the lower neighbour | |
7053 | * list | |
7054 | * @dev: device | |
7055 | * @iter: list_head ** of the current position | |
7056 | * | |
7057 | * Gets the next netdev_adjacent from the dev's lower neighbour | |
7058 | * list, starting from iter position. The caller must hold RTNL lock or | |
7059 | * its own locking that guarantees that the neighbour lower | |
b469139e | 7060 | * list will remain unchanged. |
4085ebe8 VY |
7061 | */ |
7062 | void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter) | |
7063 | { | |
7064 | struct netdev_adjacent *lower; | |
7065 | ||
cfdd28be | 7066 | lower = list_entry(*iter, struct netdev_adjacent, list); |
4085ebe8 VY |
7067 | |
7068 | if (&lower->list == &dev->adj_list.lower) | |
7069 | return NULL; | |
7070 | ||
cfdd28be | 7071 | *iter = lower->list.next; |
4085ebe8 VY |
7072 | |
7073 | return lower->dev; | |
7074 | } | |
7075 | EXPORT_SYMBOL(netdev_lower_get_next); | |
7076 | ||
1a3f060c DA |
7077 | static struct net_device *netdev_next_lower_dev(struct net_device *dev, |
7078 | struct list_head **iter) | |
7079 | { | |
7080 | struct netdev_adjacent *lower; | |
7081 | ||
46b5ab1a | 7082 | lower = list_entry((*iter)->next, struct netdev_adjacent, list); |
1a3f060c DA |
7083 | |
7084 | if (&lower->list == &dev->adj_list.lower) | |
7085 | return NULL; | |
7086 | ||
46b5ab1a | 7087 | *iter = &lower->list; |
1a3f060c DA |
7088 | |
7089 | return lower->dev; | |
7090 | } | |
7091 | ||
32b6d34f TY |
7092 | static struct net_device *__netdev_next_lower_dev(struct net_device *dev, |
7093 | struct list_head **iter, | |
7094 | bool *ignore) | |
7095 | { | |
7096 | struct netdev_adjacent *lower; | |
7097 | ||
7098 | lower = list_entry((*iter)->next, struct netdev_adjacent, list); | |
7099 | ||
7100 | if (&lower->list == &dev->adj_list.lower) | |
7101 | return NULL; | |
7102 | ||
7103 | *iter = &lower->list; | |
7104 | *ignore = lower->ignore; | |
7105 | ||
7106 | return lower->dev; | |
7107 | } | |
7108 | ||
1a3f060c DA |
7109 | int netdev_walk_all_lower_dev(struct net_device *dev, |
7110 | int (*fn)(struct net_device *dev, | |
7111 | void *data), | |
7112 | void *data) | |
7113 | { | |
5343da4c TY |
7114 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; |
7115 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7116 | int ret, cur = 0; | |
1a3f060c | 7117 | |
5343da4c TY |
7118 | now = dev; |
7119 | iter = &dev->adj_list.lower; | |
1a3f060c | 7120 | |
5343da4c TY |
7121 | while (1) { |
7122 | if (now != dev) { | |
7123 | ret = fn(now, data); | |
7124 | if (ret) | |
7125 | return ret; | |
7126 | } | |
7127 | ||
7128 | next = NULL; | |
7129 | while (1) { | |
7130 | ldev = netdev_next_lower_dev(now, &iter); | |
7131 | if (!ldev) | |
7132 | break; | |
7133 | ||
7134 | next = ldev; | |
7135 | niter = &ldev->adj_list.lower; | |
7136 | dev_stack[cur] = now; | |
7137 | iter_stack[cur++] = iter; | |
7138 | break; | |
7139 | } | |
7140 | ||
7141 | if (!next) { | |
7142 | if (!cur) | |
7143 | return 0; | |
7144 | next = dev_stack[--cur]; | |
7145 | niter = iter_stack[cur]; | |
7146 | } | |
7147 | ||
7148 | now = next; | |
7149 | iter = niter; | |
1a3f060c DA |
7150 | } |
7151 | ||
7152 | return 0; | |
7153 | } | |
7154 | EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev); | |
7155 | ||
32b6d34f TY |
7156 | static int __netdev_walk_all_lower_dev(struct net_device *dev, |
7157 | int (*fn)(struct net_device *dev, | |
7158 | void *data), | |
7159 | void *data) | |
7160 | { | |
7161 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; | |
7162 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7163 | int ret, cur = 0; | |
7164 | bool ignore; | |
7165 | ||
7166 | now = dev; | |
7167 | iter = &dev->adj_list.lower; | |
7168 | ||
7169 | while (1) { | |
7170 | if (now != dev) { | |
7171 | ret = fn(now, data); | |
7172 | if (ret) | |
7173 | return ret; | |
7174 | } | |
7175 | ||
7176 | next = NULL; | |
7177 | while (1) { | |
7178 | ldev = __netdev_next_lower_dev(now, &iter, &ignore); | |
7179 | if (!ldev) | |
7180 | break; | |
7181 | if (ignore) | |
7182 | continue; | |
7183 | ||
7184 | next = ldev; | |
7185 | niter = &ldev->adj_list.lower; | |
7186 | dev_stack[cur] = now; | |
7187 | iter_stack[cur++] = iter; | |
7188 | break; | |
7189 | } | |
7190 | ||
7191 | if (!next) { | |
7192 | if (!cur) | |
7193 | return 0; | |
7194 | next = dev_stack[--cur]; | |
7195 | niter = iter_stack[cur]; | |
7196 | } | |
7197 | ||
7198 | now = next; | |
7199 | iter = niter; | |
7200 | } | |
7201 | ||
7202 | return 0; | |
7203 | } | |
7204 | ||
1a3f060c DA |
7205 | static struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev, |
7206 | struct list_head **iter) | |
7207 | { | |
7208 | struct netdev_adjacent *lower; | |
7209 | ||
7210 | lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list); | |
7211 | if (&lower->list == &dev->adj_list.lower) | |
7212 | return NULL; | |
7213 | ||
7214 | *iter = &lower->list; | |
7215 | ||
7216 | return lower->dev; | |
7217 | } | |
7218 | ||
5343da4c TY |
7219 | static u8 __netdev_upper_depth(struct net_device *dev) |
7220 | { | |
7221 | struct net_device *udev; | |
7222 | struct list_head *iter; | |
7223 | u8 max_depth = 0; | |
32b6d34f | 7224 | bool ignore; |
5343da4c TY |
7225 | |
7226 | for (iter = &dev->adj_list.upper, | |
32b6d34f | 7227 | udev = __netdev_next_upper_dev(dev, &iter, &ignore); |
5343da4c | 7228 | udev; |
32b6d34f TY |
7229 | udev = __netdev_next_upper_dev(dev, &iter, &ignore)) { |
7230 | if (ignore) | |
7231 | continue; | |
5343da4c TY |
7232 | if (max_depth < udev->upper_level) |
7233 | max_depth = udev->upper_level; | |
7234 | } | |
7235 | ||
7236 | return max_depth; | |
7237 | } | |
7238 | ||
7239 | static u8 __netdev_lower_depth(struct net_device *dev) | |
1a3f060c DA |
7240 | { |
7241 | struct net_device *ldev; | |
7242 | struct list_head *iter; | |
5343da4c | 7243 | u8 max_depth = 0; |
32b6d34f | 7244 | bool ignore; |
1a3f060c DA |
7245 | |
7246 | for (iter = &dev->adj_list.lower, | |
32b6d34f | 7247 | ldev = __netdev_next_lower_dev(dev, &iter, &ignore); |
1a3f060c | 7248 | ldev; |
32b6d34f TY |
7249 | ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) { |
7250 | if (ignore) | |
7251 | continue; | |
5343da4c TY |
7252 | if (max_depth < ldev->lower_level) |
7253 | max_depth = ldev->lower_level; | |
7254 | } | |
1a3f060c | 7255 | |
5343da4c TY |
7256 | return max_depth; |
7257 | } | |
7258 | ||
7259 | static int __netdev_update_upper_level(struct net_device *dev, void *data) | |
7260 | { | |
7261 | dev->upper_level = __netdev_upper_depth(dev) + 1; | |
7262 | return 0; | |
7263 | } | |
7264 | ||
7265 | static int __netdev_update_lower_level(struct net_device *dev, void *data) | |
7266 | { | |
7267 | dev->lower_level = __netdev_lower_depth(dev) + 1; | |
7268 | return 0; | |
7269 | } | |
7270 | ||
7271 | int netdev_walk_all_lower_dev_rcu(struct net_device *dev, | |
7272 | int (*fn)(struct net_device *dev, | |
7273 | void *data), | |
7274 | void *data) | |
7275 | { | |
7276 | struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1]; | |
7277 | struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1]; | |
7278 | int ret, cur = 0; | |
7279 | ||
7280 | now = dev; | |
7281 | iter = &dev->adj_list.lower; | |
7282 | ||
7283 | while (1) { | |
7284 | if (now != dev) { | |
7285 | ret = fn(now, data); | |
7286 | if (ret) | |
7287 | return ret; | |
7288 | } | |
7289 | ||
7290 | next = NULL; | |
7291 | while (1) { | |
7292 | ldev = netdev_next_lower_dev_rcu(now, &iter); | |
7293 | if (!ldev) | |
7294 | break; | |
7295 | ||
7296 | next = ldev; | |
7297 | niter = &ldev->adj_list.lower; | |
7298 | dev_stack[cur] = now; | |
7299 | iter_stack[cur++] = iter; | |
7300 | break; | |
7301 | } | |
7302 | ||
7303 | if (!next) { | |
7304 | if (!cur) | |
7305 | return 0; | |
7306 | next = dev_stack[--cur]; | |
7307 | niter = iter_stack[cur]; | |
7308 | } | |
7309 | ||
7310 | now = next; | |
7311 | iter = niter; | |
1a3f060c DA |
7312 | } |
7313 | ||
7314 | return 0; | |
7315 | } | |
7316 | EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu); | |
7317 | ||
e001bfad | 7318 | /** |
7319 | * netdev_lower_get_first_private_rcu - Get the first ->private from the | |
7320 | * lower neighbour list, RCU | |
7321 | * variant | |
7322 | * @dev: device | |
7323 | * | |
7324 | * Gets the first netdev_adjacent->private from the dev's lower neighbour | |
7325 | * list. The caller must hold RCU read lock. | |
7326 | */ | |
7327 | void *netdev_lower_get_first_private_rcu(struct net_device *dev) | |
7328 | { | |
7329 | struct netdev_adjacent *lower; | |
7330 | ||
7331 | lower = list_first_or_null_rcu(&dev->adj_list.lower, | |
7332 | struct netdev_adjacent, list); | |
7333 | if (lower) | |
7334 | return lower->private; | |
7335 | return NULL; | |
7336 | } | |
7337 | EXPORT_SYMBOL(netdev_lower_get_first_private_rcu); | |
7338 | ||
9ff162a8 JP |
7339 | /** |
7340 | * netdev_master_upper_dev_get_rcu - Get master upper device | |
7341 | * @dev: device | |
7342 | * | |
7343 | * Find a master upper device and return pointer to it or NULL in case | |
7344 | * it's not there. The caller must hold the RCU read lock. | |
7345 | */ | |
7346 | struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev) | |
7347 | { | |
aa9d8560 | 7348 | struct netdev_adjacent *upper; |
9ff162a8 | 7349 | |
2f268f12 | 7350 | upper = list_first_or_null_rcu(&dev->adj_list.upper, |
aa9d8560 | 7351 | struct netdev_adjacent, list); |
9ff162a8 JP |
7352 | if (upper && likely(upper->master)) |
7353 | return upper->dev; | |
7354 | return NULL; | |
7355 | } | |
7356 | EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu); | |
7357 | ||
0a59f3a9 | 7358 | static int netdev_adjacent_sysfs_add(struct net_device *dev, |
3ee32707 VF |
7359 | struct net_device *adj_dev, |
7360 | struct list_head *dev_list) | |
7361 | { | |
7362 | char linkname[IFNAMSIZ+7]; | |
f4563a75 | 7363 | |
3ee32707 VF |
7364 | sprintf(linkname, dev_list == &dev->adj_list.upper ? |
7365 | "upper_%s" : "lower_%s", adj_dev->name); | |
7366 | return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj), | |
7367 | linkname); | |
7368 | } | |
0a59f3a9 | 7369 | static void netdev_adjacent_sysfs_del(struct net_device *dev, |
3ee32707 VF |
7370 | char *name, |
7371 | struct list_head *dev_list) | |
7372 | { | |
7373 | char linkname[IFNAMSIZ+7]; | |
f4563a75 | 7374 | |
3ee32707 VF |
7375 | sprintf(linkname, dev_list == &dev->adj_list.upper ? |
7376 | "upper_%s" : "lower_%s", name); | |
7377 | sysfs_remove_link(&(dev->dev.kobj), linkname); | |
7378 | } | |
7379 | ||
7ce64c79 AF |
7380 | static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev, |
7381 | struct net_device *adj_dev, | |
7382 | struct list_head *dev_list) | |
7383 | { | |
7384 | return (dev_list == &dev->adj_list.upper || | |
7385 | dev_list == &dev->adj_list.lower) && | |
7386 | net_eq(dev_net(dev), dev_net(adj_dev)); | |
7387 | } | |
3ee32707 | 7388 | |
5d261913 VF |
7389 | static int __netdev_adjacent_dev_insert(struct net_device *dev, |
7390 | struct net_device *adj_dev, | |
7863c054 | 7391 | struct list_head *dev_list, |
402dae96 | 7392 | void *private, bool master) |
5d261913 VF |
7393 | { |
7394 | struct netdev_adjacent *adj; | |
842d67a7 | 7395 | int ret; |
5d261913 | 7396 | |
6ea29da1 | 7397 | adj = __netdev_find_adj(adj_dev, dev_list); |
5d261913 VF |
7398 | |
7399 | if (adj) { | |
790510d9 | 7400 | adj->ref_nr += 1; |
67b62f98 DA |
7401 | pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n", |
7402 | dev->name, adj_dev->name, adj->ref_nr); | |
7403 | ||
5d261913 VF |
7404 | return 0; |
7405 | } | |
7406 | ||
7407 | adj = kmalloc(sizeof(*adj), GFP_KERNEL); | |
7408 | if (!adj) | |
7409 | return -ENOMEM; | |
7410 | ||
7411 | adj->dev = adj_dev; | |
7412 | adj->master = master; | |
790510d9 | 7413 | adj->ref_nr = 1; |
402dae96 | 7414 | adj->private = private; |
32b6d34f | 7415 | adj->ignore = false; |
5d261913 | 7416 | dev_hold(adj_dev); |
2f268f12 | 7417 | |
67b62f98 DA |
7418 | pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n", |
7419 | dev->name, adj_dev->name, adj->ref_nr, adj_dev->name); | |
5d261913 | 7420 | |
7ce64c79 | 7421 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) { |
3ee32707 | 7422 | ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list); |
5831d66e VF |
7423 | if (ret) |
7424 | goto free_adj; | |
7425 | } | |
7426 | ||
7863c054 | 7427 | /* Ensure that master link is always the first item in list. */ |
842d67a7 VF |
7428 | if (master) { |
7429 | ret = sysfs_create_link(&(dev->dev.kobj), | |
7430 | &(adj_dev->dev.kobj), "master"); | |
7431 | if (ret) | |
5831d66e | 7432 | goto remove_symlinks; |
842d67a7 | 7433 | |
7863c054 | 7434 | list_add_rcu(&adj->list, dev_list); |
842d67a7 | 7435 | } else { |
7863c054 | 7436 | list_add_tail_rcu(&adj->list, dev_list); |
842d67a7 | 7437 | } |
5d261913 VF |
7438 | |
7439 | return 0; | |
842d67a7 | 7440 | |
5831d66e | 7441 | remove_symlinks: |
7ce64c79 | 7442 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) |
3ee32707 | 7443 | netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); |
842d67a7 VF |
7444 | free_adj: |
7445 | kfree(adj); | |
974daef7 | 7446 | dev_put(adj_dev); |
842d67a7 VF |
7447 | |
7448 | return ret; | |
5d261913 VF |
7449 | } |
7450 | ||
1d143d9f | 7451 | static void __netdev_adjacent_dev_remove(struct net_device *dev, |
7452 | struct net_device *adj_dev, | |
93409033 | 7453 | u16 ref_nr, |
1d143d9f | 7454 | struct list_head *dev_list) |
5d261913 VF |
7455 | { |
7456 | struct netdev_adjacent *adj; | |
7457 | ||
67b62f98 DA |
7458 | pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n", |
7459 | dev->name, adj_dev->name, ref_nr); | |
7460 | ||
6ea29da1 | 7461 | adj = __netdev_find_adj(adj_dev, dev_list); |
5d261913 | 7462 | |
2f268f12 | 7463 | if (!adj) { |
67b62f98 | 7464 | pr_err("Adjacency does not exist for device %s from %s\n", |
2f268f12 | 7465 | dev->name, adj_dev->name); |
67b62f98 DA |
7466 | WARN_ON(1); |
7467 | return; | |
2f268f12 | 7468 | } |
5d261913 | 7469 | |
93409033 | 7470 | if (adj->ref_nr > ref_nr) { |
67b62f98 DA |
7471 | pr_debug("adjacency: %s to %s ref_nr - %d = %d\n", |
7472 | dev->name, adj_dev->name, ref_nr, | |
7473 | adj->ref_nr - ref_nr); | |
93409033 | 7474 | adj->ref_nr -= ref_nr; |
5d261913 VF |
7475 | return; |
7476 | } | |
7477 | ||
842d67a7 VF |
7478 | if (adj->master) |
7479 | sysfs_remove_link(&(dev->dev.kobj), "master"); | |
7480 | ||
7ce64c79 | 7481 | if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) |
3ee32707 | 7482 | netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list); |
5831d66e | 7483 | |
5d261913 | 7484 | list_del_rcu(&adj->list); |
67b62f98 | 7485 | pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n", |
2f268f12 | 7486 | adj_dev->name, dev->name, adj_dev->name); |
5d261913 VF |
7487 | dev_put(adj_dev); |
7488 | kfree_rcu(adj, rcu); | |
7489 | } | |
7490 | ||
1d143d9f | 7491 | static int __netdev_adjacent_dev_link_lists(struct net_device *dev, |
7492 | struct net_device *upper_dev, | |
7493 | struct list_head *up_list, | |
7494 | struct list_head *down_list, | |
7495 | void *private, bool master) | |
5d261913 VF |
7496 | { |
7497 | int ret; | |
7498 | ||
790510d9 | 7499 | ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list, |
93409033 | 7500 | private, master); |
5d261913 VF |
7501 | if (ret) |
7502 | return ret; | |
7503 | ||
790510d9 | 7504 | ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list, |
93409033 | 7505 | private, false); |
5d261913 | 7506 | if (ret) { |
790510d9 | 7507 | __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list); |
5d261913 VF |
7508 | return ret; |
7509 | } | |
7510 | ||
7511 | return 0; | |
7512 | } | |
7513 | ||
1d143d9f | 7514 | static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev, |
7515 | struct net_device *upper_dev, | |
93409033 | 7516 | u16 ref_nr, |
1d143d9f | 7517 | struct list_head *up_list, |
7518 | struct list_head *down_list) | |
5d261913 | 7519 | { |
93409033 AC |
7520 | __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list); |
7521 | __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list); | |
5d261913 VF |
7522 | } |
7523 | ||
1d143d9f | 7524 | static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev, |
7525 | struct net_device *upper_dev, | |
7526 | void *private, bool master) | |
2f268f12 | 7527 | { |
f1170fd4 DA |
7528 | return __netdev_adjacent_dev_link_lists(dev, upper_dev, |
7529 | &dev->adj_list.upper, | |
7530 | &upper_dev->adj_list.lower, | |
7531 | private, master); | |
5d261913 VF |
7532 | } |
7533 | ||
1d143d9f | 7534 | static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev, |
7535 | struct net_device *upper_dev) | |
2f268f12 | 7536 | { |
93409033 | 7537 | __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1, |
2f268f12 VF |
7538 | &dev->adj_list.upper, |
7539 | &upper_dev->adj_list.lower); | |
7540 | } | |
5d261913 | 7541 | |
9ff162a8 | 7542 | static int __netdev_upper_dev_link(struct net_device *dev, |
402dae96 | 7543 | struct net_device *upper_dev, bool master, |
42ab19ee DA |
7544 | void *upper_priv, void *upper_info, |
7545 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7546 | { |
51d0c047 DA |
7547 | struct netdev_notifier_changeupper_info changeupper_info = { |
7548 | .info = { | |
7549 | .dev = dev, | |
42ab19ee | 7550 | .extack = extack, |
51d0c047 DA |
7551 | }, |
7552 | .upper_dev = upper_dev, | |
7553 | .master = master, | |
7554 | .linking = true, | |
7555 | .upper_info = upper_info, | |
7556 | }; | |
50d629e7 | 7557 | struct net_device *master_dev; |
5d261913 | 7558 | int ret = 0; |
9ff162a8 JP |
7559 | |
7560 | ASSERT_RTNL(); | |
7561 | ||
7562 | if (dev == upper_dev) | |
7563 | return -EBUSY; | |
7564 | ||
7565 | /* To prevent loops, check if dev is not upper device to upper_dev. */ | |
32b6d34f | 7566 | if (__netdev_has_upper_dev(upper_dev, dev)) |
9ff162a8 JP |
7567 | return -EBUSY; |
7568 | ||
5343da4c TY |
7569 | if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV) |
7570 | return -EMLINK; | |
7571 | ||
50d629e7 | 7572 | if (!master) { |
32b6d34f | 7573 | if (__netdev_has_upper_dev(dev, upper_dev)) |
50d629e7 MM |
7574 | return -EEXIST; |
7575 | } else { | |
32b6d34f | 7576 | master_dev = __netdev_master_upper_dev_get(dev); |
50d629e7 MM |
7577 | if (master_dev) |
7578 | return master_dev == upper_dev ? -EEXIST : -EBUSY; | |
7579 | } | |
9ff162a8 | 7580 | |
51d0c047 | 7581 | ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, |
573c7ba0 JP |
7582 | &changeupper_info.info); |
7583 | ret = notifier_to_errno(ret); | |
7584 | if (ret) | |
7585 | return ret; | |
7586 | ||
6dffb044 | 7587 | ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv, |
402dae96 | 7588 | master); |
5d261913 VF |
7589 | if (ret) |
7590 | return ret; | |
9ff162a8 | 7591 | |
51d0c047 | 7592 | ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, |
b03804e7 IS |
7593 | &changeupper_info.info); |
7594 | ret = notifier_to_errno(ret); | |
7595 | if (ret) | |
f1170fd4 | 7596 | goto rollback; |
b03804e7 | 7597 | |
5343da4c | 7598 | __netdev_update_upper_level(dev, NULL); |
32b6d34f | 7599 | __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL); |
5343da4c TY |
7600 | |
7601 | __netdev_update_lower_level(upper_dev, NULL); | |
32b6d34f TY |
7602 | __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, |
7603 | NULL); | |
5343da4c | 7604 | |
9ff162a8 | 7605 | return 0; |
5d261913 | 7606 | |
f1170fd4 | 7607 | rollback: |
2f268f12 | 7608 | __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); |
5d261913 VF |
7609 | |
7610 | return ret; | |
9ff162a8 JP |
7611 | } |
7612 | ||
7613 | /** | |
7614 | * netdev_upper_dev_link - Add a link to the upper device | |
7615 | * @dev: device | |
7616 | * @upper_dev: new upper device | |
7a006d59 | 7617 | * @extack: netlink extended ack |
9ff162a8 JP |
7618 | * |
7619 | * Adds a link to device which is upper to this one. The caller must hold | |
7620 | * the RTNL lock. On a failure a negative errno code is returned. | |
7621 | * On success the reference counts are adjusted and the function | |
7622 | * returns zero. | |
7623 | */ | |
7624 | int netdev_upper_dev_link(struct net_device *dev, | |
42ab19ee DA |
7625 | struct net_device *upper_dev, |
7626 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7627 | { |
42ab19ee DA |
7628 | return __netdev_upper_dev_link(dev, upper_dev, false, |
7629 | NULL, NULL, extack); | |
9ff162a8 JP |
7630 | } |
7631 | EXPORT_SYMBOL(netdev_upper_dev_link); | |
7632 | ||
7633 | /** | |
7634 | * netdev_master_upper_dev_link - Add a master link to the upper device | |
7635 | * @dev: device | |
7636 | * @upper_dev: new upper device | |
6dffb044 | 7637 | * @upper_priv: upper device private |
29bf24af | 7638 | * @upper_info: upper info to be passed down via notifier |
7a006d59 | 7639 | * @extack: netlink extended ack |
9ff162a8 JP |
7640 | * |
7641 | * Adds a link to device which is upper to this one. In this case, only | |
7642 | * one master upper device can be linked, although other non-master devices | |
7643 | * might be linked as well. The caller must hold the RTNL lock. | |
7644 | * On a failure a negative errno code is returned. On success the reference | |
7645 | * counts are adjusted and the function returns zero. | |
7646 | */ | |
7647 | int netdev_master_upper_dev_link(struct net_device *dev, | |
6dffb044 | 7648 | struct net_device *upper_dev, |
42ab19ee DA |
7649 | void *upper_priv, void *upper_info, |
7650 | struct netlink_ext_ack *extack) | |
9ff162a8 | 7651 | { |
29bf24af | 7652 | return __netdev_upper_dev_link(dev, upper_dev, true, |
42ab19ee | 7653 | upper_priv, upper_info, extack); |
9ff162a8 JP |
7654 | } |
7655 | EXPORT_SYMBOL(netdev_master_upper_dev_link); | |
7656 | ||
7657 | /** | |
7658 | * netdev_upper_dev_unlink - Removes a link to upper device | |
7659 | * @dev: device | |
7660 | * @upper_dev: new upper device | |
7661 | * | |
7662 | * Removes a link to device which is upper to this one. The caller must hold | |
7663 | * the RTNL lock. | |
7664 | */ | |
7665 | void netdev_upper_dev_unlink(struct net_device *dev, | |
7666 | struct net_device *upper_dev) | |
7667 | { | |
51d0c047 DA |
7668 | struct netdev_notifier_changeupper_info changeupper_info = { |
7669 | .info = { | |
7670 | .dev = dev, | |
7671 | }, | |
7672 | .upper_dev = upper_dev, | |
7673 | .linking = false, | |
7674 | }; | |
f4563a75 | 7675 | |
9ff162a8 JP |
7676 | ASSERT_RTNL(); |
7677 | ||
0e4ead9d | 7678 | changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev; |
0e4ead9d | 7679 | |
51d0c047 | 7680 | call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER, |
573c7ba0 JP |
7681 | &changeupper_info.info); |
7682 | ||
2f268f12 | 7683 | __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev); |
5d261913 | 7684 | |
51d0c047 | 7685 | call_netdevice_notifiers_info(NETDEV_CHANGEUPPER, |
0e4ead9d | 7686 | &changeupper_info.info); |
5343da4c TY |
7687 | |
7688 | __netdev_update_upper_level(dev, NULL); | |
32b6d34f | 7689 | __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL); |
5343da4c TY |
7690 | |
7691 | __netdev_update_lower_level(upper_dev, NULL); | |
32b6d34f TY |
7692 | __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level, |
7693 | NULL); | |
9ff162a8 JP |
7694 | } |
7695 | EXPORT_SYMBOL(netdev_upper_dev_unlink); | |
7696 | ||
32b6d34f TY |
7697 | static void __netdev_adjacent_dev_set(struct net_device *upper_dev, |
7698 | struct net_device *lower_dev, | |
7699 | bool val) | |
7700 | { | |
7701 | struct netdev_adjacent *adj; | |
7702 | ||
7703 | adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower); | |
7704 | if (adj) | |
7705 | adj->ignore = val; | |
7706 | ||
7707 | adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper); | |
7708 | if (adj) | |
7709 | adj->ignore = val; | |
7710 | } | |
7711 | ||
7712 | static void netdev_adjacent_dev_disable(struct net_device *upper_dev, | |
7713 | struct net_device *lower_dev) | |
7714 | { | |
7715 | __netdev_adjacent_dev_set(upper_dev, lower_dev, true); | |
7716 | } | |
7717 | ||
7718 | static void netdev_adjacent_dev_enable(struct net_device *upper_dev, | |
7719 | struct net_device *lower_dev) | |
7720 | { | |
7721 | __netdev_adjacent_dev_set(upper_dev, lower_dev, false); | |
7722 | } | |
7723 | ||
7724 | int netdev_adjacent_change_prepare(struct net_device *old_dev, | |
7725 | struct net_device *new_dev, | |
7726 | struct net_device *dev, | |
7727 | struct netlink_ext_ack *extack) | |
7728 | { | |
7729 | int err; | |
7730 | ||
7731 | if (!new_dev) | |
7732 | return 0; | |
7733 | ||
7734 | if (old_dev && new_dev != old_dev) | |
7735 | netdev_adjacent_dev_disable(dev, old_dev); | |
7736 | ||
7737 | err = netdev_upper_dev_link(new_dev, dev, extack); | |
7738 | if (err) { | |
7739 | if (old_dev && new_dev != old_dev) | |
7740 | netdev_adjacent_dev_enable(dev, old_dev); | |
7741 | return err; | |
7742 | } | |
7743 | ||
7744 | return 0; | |
7745 | } | |
7746 | EXPORT_SYMBOL(netdev_adjacent_change_prepare); | |
7747 | ||
7748 | void netdev_adjacent_change_commit(struct net_device *old_dev, | |
7749 | struct net_device *new_dev, | |
7750 | struct net_device *dev) | |
7751 | { | |
7752 | if (!new_dev || !old_dev) | |
7753 | return; | |
7754 | ||
7755 | if (new_dev == old_dev) | |
7756 | return; | |
7757 | ||
7758 | netdev_adjacent_dev_enable(dev, old_dev); | |
7759 | netdev_upper_dev_unlink(old_dev, dev); | |
7760 | } | |
7761 | EXPORT_SYMBOL(netdev_adjacent_change_commit); | |
7762 | ||
7763 | void netdev_adjacent_change_abort(struct net_device *old_dev, | |
7764 | struct net_device *new_dev, | |
7765 | struct net_device *dev) | |
7766 | { | |
7767 | if (!new_dev) | |
7768 | return; | |
7769 | ||
7770 | if (old_dev && new_dev != old_dev) | |
7771 | netdev_adjacent_dev_enable(dev, old_dev); | |
7772 | ||
7773 | netdev_upper_dev_unlink(new_dev, dev); | |
7774 | } | |
7775 | EXPORT_SYMBOL(netdev_adjacent_change_abort); | |
7776 | ||
61bd3857 MS |
7777 | /** |
7778 | * netdev_bonding_info_change - Dispatch event about slave change | |
7779 | * @dev: device | |
4a26e453 | 7780 | * @bonding_info: info to dispatch |
61bd3857 MS |
7781 | * |
7782 | * Send NETDEV_BONDING_INFO to netdev notifiers with info. | |
7783 | * The caller must hold the RTNL lock. | |
7784 | */ | |
7785 | void netdev_bonding_info_change(struct net_device *dev, | |
7786 | struct netdev_bonding_info *bonding_info) | |
7787 | { | |
51d0c047 DA |
7788 | struct netdev_notifier_bonding_info info = { |
7789 | .info.dev = dev, | |
7790 | }; | |
61bd3857 MS |
7791 | |
7792 | memcpy(&info.bonding_info, bonding_info, | |
7793 | sizeof(struct netdev_bonding_info)); | |
51d0c047 | 7794 | call_netdevice_notifiers_info(NETDEV_BONDING_INFO, |
61bd3857 MS |
7795 | &info.info); |
7796 | } | |
7797 | EXPORT_SYMBOL(netdev_bonding_info_change); | |
7798 | ||
2ce1ee17 | 7799 | static void netdev_adjacent_add_links(struct net_device *dev) |
4c75431a AF |
7800 | { |
7801 | struct netdev_adjacent *iter; | |
7802 | ||
7803 | struct net *net = dev_net(dev); | |
7804 | ||
7805 | list_for_each_entry(iter, &dev->adj_list.upper, list) { | |
be4da0e3 | 7806 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
7807 | continue; |
7808 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
7809 | &iter->dev->adj_list.lower); | |
7810 | netdev_adjacent_sysfs_add(dev, iter->dev, | |
7811 | &dev->adj_list.upper); | |
7812 | } | |
7813 | ||
7814 | list_for_each_entry(iter, &dev->adj_list.lower, list) { | |
be4da0e3 | 7815 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
7816 | continue; |
7817 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
7818 | &iter->dev->adj_list.upper); | |
7819 | netdev_adjacent_sysfs_add(dev, iter->dev, | |
7820 | &dev->adj_list.lower); | |
7821 | } | |
7822 | } | |
7823 | ||
2ce1ee17 | 7824 | static void netdev_adjacent_del_links(struct net_device *dev) |
4c75431a AF |
7825 | { |
7826 | struct netdev_adjacent *iter; | |
7827 | ||
7828 | struct net *net = dev_net(dev); | |
7829 | ||
7830 | list_for_each_entry(iter, &dev->adj_list.upper, list) { | |
be4da0e3 | 7831 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
7832 | continue; |
7833 | netdev_adjacent_sysfs_del(iter->dev, dev->name, | |
7834 | &iter->dev->adj_list.lower); | |
7835 | netdev_adjacent_sysfs_del(dev, iter->dev->name, | |
7836 | &dev->adj_list.upper); | |
7837 | } | |
7838 | ||
7839 | list_for_each_entry(iter, &dev->adj_list.lower, list) { | |
be4da0e3 | 7840 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a AF |
7841 | continue; |
7842 | netdev_adjacent_sysfs_del(iter->dev, dev->name, | |
7843 | &iter->dev->adj_list.upper); | |
7844 | netdev_adjacent_sysfs_del(dev, iter->dev->name, | |
7845 | &dev->adj_list.lower); | |
7846 | } | |
7847 | } | |
7848 | ||
5bb025fa | 7849 | void netdev_adjacent_rename_links(struct net_device *dev, char *oldname) |
402dae96 | 7850 | { |
5bb025fa | 7851 | struct netdev_adjacent *iter; |
402dae96 | 7852 | |
4c75431a AF |
7853 | struct net *net = dev_net(dev); |
7854 | ||
5bb025fa | 7855 | list_for_each_entry(iter, &dev->adj_list.upper, list) { |
be4da0e3 | 7856 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a | 7857 | continue; |
5bb025fa VF |
7858 | netdev_adjacent_sysfs_del(iter->dev, oldname, |
7859 | &iter->dev->adj_list.lower); | |
7860 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
7861 | &iter->dev->adj_list.lower); | |
7862 | } | |
402dae96 | 7863 | |
5bb025fa | 7864 | list_for_each_entry(iter, &dev->adj_list.lower, list) { |
be4da0e3 | 7865 | if (!net_eq(net, dev_net(iter->dev))) |
4c75431a | 7866 | continue; |
5bb025fa VF |
7867 | netdev_adjacent_sysfs_del(iter->dev, oldname, |
7868 | &iter->dev->adj_list.upper); | |
7869 | netdev_adjacent_sysfs_add(iter->dev, dev, | |
7870 | &iter->dev->adj_list.upper); | |
7871 | } | |
402dae96 | 7872 | } |
402dae96 VF |
7873 | |
7874 | void *netdev_lower_dev_get_private(struct net_device *dev, | |
7875 | struct net_device *lower_dev) | |
7876 | { | |
7877 | struct netdev_adjacent *lower; | |
7878 | ||
7879 | if (!lower_dev) | |
7880 | return NULL; | |
6ea29da1 | 7881 | lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower); |
402dae96 VF |
7882 | if (!lower) |
7883 | return NULL; | |
7884 | ||
7885 | return lower->private; | |
7886 | } | |
7887 | EXPORT_SYMBOL(netdev_lower_dev_get_private); | |
7888 | ||
4085ebe8 | 7889 | |
04d48266 JP |
7890 | /** |
7891 | * netdev_lower_change - Dispatch event about lower device state change | |
7892 | * @lower_dev: device | |
7893 | * @lower_state_info: state to dispatch | |
7894 | * | |
7895 | * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info. | |
7896 | * The caller must hold the RTNL lock. | |
7897 | */ | |
7898 | void netdev_lower_state_changed(struct net_device *lower_dev, | |
7899 | void *lower_state_info) | |
7900 | { | |
51d0c047 DA |
7901 | struct netdev_notifier_changelowerstate_info changelowerstate_info = { |
7902 | .info.dev = lower_dev, | |
7903 | }; | |
04d48266 JP |
7904 | |
7905 | ASSERT_RTNL(); | |
7906 | changelowerstate_info.lower_state_info = lower_state_info; | |
51d0c047 | 7907 | call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE, |
04d48266 JP |
7908 | &changelowerstate_info.info); |
7909 | } | |
7910 | EXPORT_SYMBOL(netdev_lower_state_changed); | |
7911 | ||
b6c40d68 PM |
7912 | static void dev_change_rx_flags(struct net_device *dev, int flags) |
7913 | { | |
d314774c SH |
7914 | const struct net_device_ops *ops = dev->netdev_ops; |
7915 | ||
d2615bf4 | 7916 | if (ops->ndo_change_rx_flags) |
d314774c | 7917 | ops->ndo_change_rx_flags(dev, flags); |
b6c40d68 PM |
7918 | } |
7919 | ||
991fb3f7 | 7920 | static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify) |
1da177e4 | 7921 | { |
b536db93 | 7922 | unsigned int old_flags = dev->flags; |
d04a48b0 EB |
7923 | kuid_t uid; |
7924 | kgid_t gid; | |
1da177e4 | 7925 | |
24023451 PM |
7926 | ASSERT_RTNL(); |
7927 | ||
dad9b335 WC |
7928 | dev->flags |= IFF_PROMISC; |
7929 | dev->promiscuity += inc; | |
7930 | if (dev->promiscuity == 0) { | |
7931 | /* | |
7932 | * Avoid overflow. | |
7933 | * If inc causes overflow, untouch promisc and return error. | |
7934 | */ | |
7935 | if (inc < 0) | |
7936 | dev->flags &= ~IFF_PROMISC; | |
7937 | else { | |
7938 | dev->promiscuity -= inc; | |
7b6cd1ce JP |
7939 | pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n", |
7940 | dev->name); | |
dad9b335 WC |
7941 | return -EOVERFLOW; |
7942 | } | |
7943 | } | |
52609c0b | 7944 | if (dev->flags != old_flags) { |
7b6cd1ce JP |
7945 | pr_info("device %s %s promiscuous mode\n", |
7946 | dev->name, | |
7947 | dev->flags & IFF_PROMISC ? "entered" : "left"); | |
8192b0c4 DH |
7948 | if (audit_enabled) { |
7949 | current_uid_gid(&uid, &gid); | |
cdfb6b34 RGB |
7950 | audit_log(audit_context(), GFP_ATOMIC, |
7951 | AUDIT_ANOM_PROMISCUOUS, | |
7952 | "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u", | |
7953 | dev->name, (dev->flags & IFF_PROMISC), | |
7954 | (old_flags & IFF_PROMISC), | |
7955 | from_kuid(&init_user_ns, audit_get_loginuid(current)), | |
7956 | from_kuid(&init_user_ns, uid), | |
7957 | from_kgid(&init_user_ns, gid), | |
7958 | audit_get_sessionid(current)); | |
8192b0c4 | 7959 | } |
24023451 | 7960 | |
b6c40d68 | 7961 | dev_change_rx_flags(dev, IFF_PROMISC); |
1da177e4 | 7962 | } |
991fb3f7 ND |
7963 | if (notify) |
7964 | __dev_notify_flags(dev, old_flags, IFF_PROMISC); | |
dad9b335 | 7965 | return 0; |
1da177e4 LT |
7966 | } |
7967 | ||
4417da66 PM |
7968 | /** |
7969 | * dev_set_promiscuity - update promiscuity count on a device | |
7970 | * @dev: device | |
7971 | * @inc: modifier | |
7972 | * | |
7973 | * Add or remove promiscuity from a device. While the count in the device | |
7974 | * remains above zero the interface remains promiscuous. Once it hits zero | |
7975 | * the device reverts back to normal filtering operation. A negative inc | |
7976 | * value is used to drop promiscuity on the device. | |
dad9b335 | 7977 | * Return 0 if successful or a negative errno code on error. |
4417da66 | 7978 | */ |
dad9b335 | 7979 | int dev_set_promiscuity(struct net_device *dev, int inc) |
4417da66 | 7980 | { |
b536db93 | 7981 | unsigned int old_flags = dev->flags; |
dad9b335 | 7982 | int err; |
4417da66 | 7983 | |
991fb3f7 | 7984 | err = __dev_set_promiscuity(dev, inc, true); |
4b5a698e | 7985 | if (err < 0) |
dad9b335 | 7986 | return err; |
4417da66 PM |
7987 | if (dev->flags != old_flags) |
7988 | dev_set_rx_mode(dev); | |
dad9b335 | 7989 | return err; |
4417da66 | 7990 | } |
d1b19dff | 7991 | EXPORT_SYMBOL(dev_set_promiscuity); |
4417da66 | 7992 | |
991fb3f7 | 7993 | static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify) |
1da177e4 | 7994 | { |
991fb3f7 | 7995 | unsigned int old_flags = dev->flags, old_gflags = dev->gflags; |
1da177e4 | 7996 | |
24023451 PM |
7997 | ASSERT_RTNL(); |
7998 | ||
1da177e4 | 7999 | dev->flags |= IFF_ALLMULTI; |
dad9b335 WC |
8000 | dev->allmulti += inc; |
8001 | if (dev->allmulti == 0) { | |
8002 | /* | |
8003 | * Avoid overflow. | |
8004 | * If inc causes overflow, untouch allmulti and return error. | |
8005 | */ | |
8006 | if (inc < 0) | |
8007 | dev->flags &= ~IFF_ALLMULTI; | |
8008 | else { | |
8009 | dev->allmulti -= inc; | |
7b6cd1ce JP |
8010 | pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n", |
8011 | dev->name); | |
dad9b335 WC |
8012 | return -EOVERFLOW; |
8013 | } | |
8014 | } | |
24023451 | 8015 | if (dev->flags ^ old_flags) { |
b6c40d68 | 8016 | dev_change_rx_flags(dev, IFF_ALLMULTI); |
4417da66 | 8017 | dev_set_rx_mode(dev); |
991fb3f7 ND |
8018 | if (notify) |
8019 | __dev_notify_flags(dev, old_flags, | |
8020 | dev->gflags ^ old_gflags); | |
24023451 | 8021 | } |
dad9b335 | 8022 | return 0; |
4417da66 | 8023 | } |
991fb3f7 ND |
8024 | |
8025 | /** | |
8026 | * dev_set_allmulti - update allmulti count on a device | |
8027 | * @dev: device | |
8028 | * @inc: modifier | |
8029 | * | |
8030 | * Add or remove reception of all multicast frames to a device. While the | |
8031 | * count in the device remains above zero the interface remains listening | |
8032 | * to all interfaces. Once it hits zero the device reverts back to normal | |
8033 | * filtering operation. A negative @inc value is used to drop the counter | |
8034 | * when releasing a resource needing all multicasts. | |
8035 | * Return 0 if successful or a negative errno code on error. | |
8036 | */ | |
8037 | ||
8038 | int dev_set_allmulti(struct net_device *dev, int inc) | |
8039 | { | |
8040 | return __dev_set_allmulti(dev, inc, true); | |
8041 | } | |
d1b19dff | 8042 | EXPORT_SYMBOL(dev_set_allmulti); |
4417da66 PM |
8043 | |
8044 | /* | |
8045 | * Upload unicast and multicast address lists to device and | |
8046 | * configure RX filtering. When the device doesn't support unicast | |
53ccaae1 | 8047 | * filtering it is put in promiscuous mode while unicast addresses |
4417da66 PM |
8048 | * are present. |
8049 | */ | |
8050 | void __dev_set_rx_mode(struct net_device *dev) | |
8051 | { | |
d314774c SH |
8052 | const struct net_device_ops *ops = dev->netdev_ops; |
8053 | ||
4417da66 PM |
8054 | /* dev_open will call this function so the list will stay sane. */ |
8055 | if (!(dev->flags&IFF_UP)) | |
8056 | return; | |
8057 | ||
8058 | if (!netif_device_present(dev)) | |
40b77c94 | 8059 | return; |
4417da66 | 8060 | |
01789349 | 8061 | if (!(dev->priv_flags & IFF_UNICAST_FLT)) { |
4417da66 PM |
8062 | /* Unicast addresses changes may only happen under the rtnl, |
8063 | * therefore calling __dev_set_promiscuity here is safe. | |
8064 | */ | |
32e7bfc4 | 8065 | if (!netdev_uc_empty(dev) && !dev->uc_promisc) { |
991fb3f7 | 8066 | __dev_set_promiscuity(dev, 1, false); |
2d348d1f | 8067 | dev->uc_promisc = true; |
32e7bfc4 | 8068 | } else if (netdev_uc_empty(dev) && dev->uc_promisc) { |
991fb3f7 | 8069 | __dev_set_promiscuity(dev, -1, false); |
2d348d1f | 8070 | dev->uc_promisc = false; |
4417da66 | 8071 | } |
4417da66 | 8072 | } |
01789349 JP |
8073 | |
8074 | if (ops->ndo_set_rx_mode) | |
8075 | ops->ndo_set_rx_mode(dev); | |
4417da66 PM |
8076 | } |
8077 | ||
8078 | void dev_set_rx_mode(struct net_device *dev) | |
8079 | { | |
b9e40857 | 8080 | netif_addr_lock_bh(dev); |
4417da66 | 8081 | __dev_set_rx_mode(dev); |
b9e40857 | 8082 | netif_addr_unlock_bh(dev); |
1da177e4 LT |
8083 | } |
8084 | ||
f0db275a SH |
8085 | /** |
8086 | * dev_get_flags - get flags reported to userspace | |
8087 | * @dev: device | |
8088 | * | |
8089 | * Get the combination of flag bits exported through APIs to userspace. | |
8090 | */ | |
95c96174 | 8091 | unsigned int dev_get_flags(const struct net_device *dev) |
1da177e4 | 8092 | { |
95c96174 | 8093 | unsigned int flags; |
1da177e4 LT |
8094 | |
8095 | flags = (dev->flags & ~(IFF_PROMISC | | |
8096 | IFF_ALLMULTI | | |
b00055aa SR |
8097 | IFF_RUNNING | |
8098 | IFF_LOWER_UP | | |
8099 | IFF_DORMANT)) | | |
1da177e4 LT |
8100 | (dev->gflags & (IFF_PROMISC | |
8101 | IFF_ALLMULTI)); | |
8102 | ||
b00055aa SR |
8103 | if (netif_running(dev)) { |
8104 | if (netif_oper_up(dev)) | |
8105 | flags |= IFF_RUNNING; | |
8106 | if (netif_carrier_ok(dev)) | |
8107 | flags |= IFF_LOWER_UP; | |
8108 | if (netif_dormant(dev)) | |
8109 | flags |= IFF_DORMANT; | |
8110 | } | |
1da177e4 LT |
8111 | |
8112 | return flags; | |
8113 | } | |
d1b19dff | 8114 | EXPORT_SYMBOL(dev_get_flags); |
1da177e4 | 8115 | |
6d040321 PM |
8116 | int __dev_change_flags(struct net_device *dev, unsigned int flags, |
8117 | struct netlink_ext_ack *extack) | |
1da177e4 | 8118 | { |
b536db93 | 8119 | unsigned int old_flags = dev->flags; |
bd380811 | 8120 | int ret; |
1da177e4 | 8121 | |
24023451 PM |
8122 | ASSERT_RTNL(); |
8123 | ||
1da177e4 LT |
8124 | /* |
8125 | * Set the flags on our device. | |
8126 | */ | |
8127 | ||
8128 | dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP | | |
8129 | IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL | | |
8130 | IFF_AUTOMEDIA)) | | |
8131 | (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC | | |
8132 | IFF_ALLMULTI)); | |
8133 | ||
8134 | /* | |
8135 | * Load in the correct multicast list now the flags have changed. | |
8136 | */ | |
8137 | ||
b6c40d68 PM |
8138 | if ((old_flags ^ flags) & IFF_MULTICAST) |
8139 | dev_change_rx_flags(dev, IFF_MULTICAST); | |
24023451 | 8140 | |
4417da66 | 8141 | dev_set_rx_mode(dev); |
1da177e4 LT |
8142 | |
8143 | /* | |
8144 | * Have we downed the interface. We handle IFF_UP ourselves | |
8145 | * according to user attempts to set it, rather than blindly | |
8146 | * setting it. | |
8147 | */ | |
8148 | ||
8149 | ret = 0; | |
7051b88a | 8150 | if ((old_flags ^ flags) & IFF_UP) { |
8151 | if (old_flags & IFF_UP) | |
8152 | __dev_close(dev); | |
8153 | else | |
40c900aa | 8154 | ret = __dev_open(dev, extack); |
7051b88a | 8155 | } |
1da177e4 | 8156 | |
1da177e4 | 8157 | if ((flags ^ dev->gflags) & IFF_PROMISC) { |
d1b19dff | 8158 | int inc = (flags & IFF_PROMISC) ? 1 : -1; |
991fb3f7 | 8159 | unsigned int old_flags = dev->flags; |
d1b19dff | 8160 | |
1da177e4 | 8161 | dev->gflags ^= IFF_PROMISC; |
991fb3f7 ND |
8162 | |
8163 | if (__dev_set_promiscuity(dev, inc, false) >= 0) | |
8164 | if (dev->flags != old_flags) | |
8165 | dev_set_rx_mode(dev); | |
1da177e4 LT |
8166 | } |
8167 | ||
8168 | /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI | |
eb13da1a | 8169 | * is important. Some (broken) drivers set IFF_PROMISC, when |
8170 | * IFF_ALLMULTI is requested not asking us and not reporting. | |
1da177e4 LT |
8171 | */ |
8172 | if ((flags ^ dev->gflags) & IFF_ALLMULTI) { | |
d1b19dff ED |
8173 | int inc = (flags & IFF_ALLMULTI) ? 1 : -1; |
8174 | ||
1da177e4 | 8175 | dev->gflags ^= IFF_ALLMULTI; |
991fb3f7 | 8176 | __dev_set_allmulti(dev, inc, false); |
1da177e4 LT |
8177 | } |
8178 | ||
bd380811 PM |
8179 | return ret; |
8180 | } | |
8181 | ||
a528c219 ND |
8182 | void __dev_notify_flags(struct net_device *dev, unsigned int old_flags, |
8183 | unsigned int gchanges) | |
bd380811 PM |
8184 | { |
8185 | unsigned int changes = dev->flags ^ old_flags; | |
8186 | ||
a528c219 | 8187 | if (gchanges) |
7f294054 | 8188 | rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC); |
a528c219 | 8189 | |
bd380811 PM |
8190 | if (changes & IFF_UP) { |
8191 | if (dev->flags & IFF_UP) | |
8192 | call_netdevice_notifiers(NETDEV_UP, dev); | |
8193 | else | |
8194 | call_netdevice_notifiers(NETDEV_DOWN, dev); | |
8195 | } | |
8196 | ||
8197 | if (dev->flags & IFF_UP && | |
be9efd36 | 8198 | (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) { |
51d0c047 DA |
8199 | struct netdev_notifier_change_info change_info = { |
8200 | .info = { | |
8201 | .dev = dev, | |
8202 | }, | |
8203 | .flags_changed = changes, | |
8204 | }; | |
be9efd36 | 8205 | |
51d0c047 | 8206 | call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info); |
be9efd36 | 8207 | } |
bd380811 PM |
8208 | } |
8209 | ||
8210 | /** | |
8211 | * dev_change_flags - change device settings | |
8212 | * @dev: device | |
8213 | * @flags: device state flags | |
567c5e13 | 8214 | * @extack: netlink extended ack |
bd380811 PM |
8215 | * |
8216 | * Change settings on device based state flags. The flags are | |
8217 | * in the userspace exported format. | |
8218 | */ | |
567c5e13 PM |
8219 | int dev_change_flags(struct net_device *dev, unsigned int flags, |
8220 | struct netlink_ext_ack *extack) | |
bd380811 | 8221 | { |
b536db93 | 8222 | int ret; |
991fb3f7 | 8223 | unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags; |
bd380811 | 8224 | |
6d040321 | 8225 | ret = __dev_change_flags(dev, flags, extack); |
bd380811 PM |
8226 | if (ret < 0) |
8227 | return ret; | |
8228 | ||
991fb3f7 | 8229 | changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags); |
a528c219 | 8230 | __dev_notify_flags(dev, old_flags, changes); |
1da177e4 LT |
8231 | return ret; |
8232 | } | |
d1b19dff | 8233 | EXPORT_SYMBOL(dev_change_flags); |
1da177e4 | 8234 | |
f51048c3 | 8235 | int __dev_set_mtu(struct net_device *dev, int new_mtu) |
2315dc91 VF |
8236 | { |
8237 | const struct net_device_ops *ops = dev->netdev_ops; | |
8238 | ||
8239 | if (ops->ndo_change_mtu) | |
8240 | return ops->ndo_change_mtu(dev, new_mtu); | |
8241 | ||
501a90c9 ED |
8242 | /* Pairs with all the lockless reads of dev->mtu in the stack */ |
8243 | WRITE_ONCE(dev->mtu, new_mtu); | |
2315dc91 VF |
8244 | return 0; |
8245 | } | |
f51048c3 | 8246 | EXPORT_SYMBOL(__dev_set_mtu); |
2315dc91 | 8247 | |
d836f5c6 ED |
8248 | int dev_validate_mtu(struct net_device *dev, int new_mtu, |
8249 | struct netlink_ext_ack *extack) | |
8250 | { | |
8251 | /* MTU must be positive, and in range */ | |
8252 | if (new_mtu < 0 || new_mtu < dev->min_mtu) { | |
8253 | NL_SET_ERR_MSG(extack, "mtu less than device minimum"); | |
8254 | return -EINVAL; | |
8255 | } | |
8256 | ||
8257 | if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) { | |
8258 | NL_SET_ERR_MSG(extack, "mtu greater than device maximum"); | |
8259 | return -EINVAL; | |
8260 | } | |
8261 | return 0; | |
8262 | } | |
8263 | ||
f0db275a | 8264 | /** |
7a4c53be | 8265 | * dev_set_mtu_ext - Change maximum transfer unit |
f0db275a SH |
8266 | * @dev: device |
8267 | * @new_mtu: new transfer unit | |
7a4c53be | 8268 | * @extack: netlink extended ack |
f0db275a SH |
8269 | * |
8270 | * Change the maximum transfer size of the network device. | |
8271 | */ | |
7a4c53be SH |
8272 | int dev_set_mtu_ext(struct net_device *dev, int new_mtu, |
8273 | struct netlink_ext_ack *extack) | |
1da177e4 | 8274 | { |
2315dc91 | 8275 | int err, orig_mtu; |
1da177e4 LT |
8276 | |
8277 | if (new_mtu == dev->mtu) | |
8278 | return 0; | |
8279 | ||
d836f5c6 ED |
8280 | err = dev_validate_mtu(dev, new_mtu, extack); |
8281 | if (err) | |
8282 | return err; | |
1da177e4 LT |
8283 | |
8284 | if (!netif_device_present(dev)) | |
8285 | return -ENODEV; | |
8286 | ||
1d486bfb VF |
8287 | err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev); |
8288 | err = notifier_to_errno(err); | |
8289 | if (err) | |
8290 | return err; | |
d314774c | 8291 | |
2315dc91 VF |
8292 | orig_mtu = dev->mtu; |
8293 | err = __dev_set_mtu(dev, new_mtu); | |
d314774c | 8294 | |
2315dc91 | 8295 | if (!err) { |
af7d6cce SD |
8296 | err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, |
8297 | orig_mtu); | |
2315dc91 VF |
8298 | err = notifier_to_errno(err); |
8299 | if (err) { | |
8300 | /* setting mtu back and notifying everyone again, | |
8301 | * so that they have a chance to revert changes. | |
8302 | */ | |
8303 | __dev_set_mtu(dev, orig_mtu); | |
af7d6cce SD |
8304 | call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev, |
8305 | new_mtu); | |
2315dc91 VF |
8306 | } |
8307 | } | |
1da177e4 LT |
8308 | return err; |
8309 | } | |
7a4c53be SH |
8310 | |
8311 | int dev_set_mtu(struct net_device *dev, int new_mtu) | |
8312 | { | |
8313 | struct netlink_ext_ack extack; | |
8314 | int err; | |
8315 | ||
a6bcfc89 | 8316 | memset(&extack, 0, sizeof(extack)); |
7a4c53be | 8317 | err = dev_set_mtu_ext(dev, new_mtu, &extack); |
a6bcfc89 | 8318 | if (err && extack._msg) |
7a4c53be SH |
8319 | net_err_ratelimited("%s: %s\n", dev->name, extack._msg); |
8320 | return err; | |
8321 | } | |
d1b19dff | 8322 | EXPORT_SYMBOL(dev_set_mtu); |
1da177e4 | 8323 | |
6a643ddb CW |
8324 | /** |
8325 | * dev_change_tx_queue_len - Change TX queue length of a netdevice | |
8326 | * @dev: device | |
8327 | * @new_len: new tx queue length | |
8328 | */ | |
8329 | int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len) | |
8330 | { | |
8331 | unsigned int orig_len = dev->tx_queue_len; | |
8332 | int res; | |
8333 | ||
8334 | if (new_len != (unsigned int)new_len) | |
8335 | return -ERANGE; | |
8336 | ||
8337 | if (new_len != orig_len) { | |
8338 | dev->tx_queue_len = new_len; | |
8339 | res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev); | |
8340 | res = notifier_to_errno(res); | |
7effaf06 TT |
8341 | if (res) |
8342 | goto err_rollback; | |
8343 | res = dev_qdisc_change_tx_queue_len(dev); | |
8344 | if (res) | |
8345 | goto err_rollback; | |
6a643ddb CW |
8346 | } |
8347 | ||
8348 | return 0; | |
7effaf06 TT |
8349 | |
8350 | err_rollback: | |
8351 | netdev_err(dev, "refused to change device tx_queue_len\n"); | |
8352 | dev->tx_queue_len = orig_len; | |
8353 | return res; | |
6a643ddb CW |
8354 | } |
8355 | ||
cbda10fa VD |
8356 | /** |
8357 | * dev_set_group - Change group this device belongs to | |
8358 | * @dev: device | |
8359 | * @new_group: group this device should belong to | |
8360 | */ | |
8361 | void dev_set_group(struct net_device *dev, int new_group) | |
8362 | { | |
8363 | dev->group = new_group; | |
8364 | } | |
8365 | EXPORT_SYMBOL(dev_set_group); | |
8366 | ||
d59cdf94 PM |
8367 | /** |
8368 | * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR. | |
8369 | * @dev: device | |
8370 | * @addr: new address | |
8371 | * @extack: netlink extended ack | |
8372 | */ | |
8373 | int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr, | |
8374 | struct netlink_ext_ack *extack) | |
8375 | { | |
8376 | struct netdev_notifier_pre_changeaddr_info info = { | |
8377 | .info.dev = dev, | |
8378 | .info.extack = extack, | |
8379 | .dev_addr = addr, | |
8380 | }; | |
8381 | int rc; | |
8382 | ||
8383 | rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info); | |
8384 | return notifier_to_errno(rc); | |
8385 | } | |
8386 | EXPORT_SYMBOL(dev_pre_changeaddr_notify); | |
8387 | ||
f0db275a SH |
8388 | /** |
8389 | * dev_set_mac_address - Change Media Access Control Address | |
8390 | * @dev: device | |
8391 | * @sa: new address | |
3a37a963 | 8392 | * @extack: netlink extended ack |
f0db275a SH |
8393 | * |
8394 | * Change the hardware (MAC) address of the device | |
8395 | */ | |
3a37a963 PM |
8396 | int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa, |
8397 | struct netlink_ext_ack *extack) | |
1da177e4 | 8398 | { |
d314774c | 8399 | const struct net_device_ops *ops = dev->netdev_ops; |
1da177e4 LT |
8400 | int err; |
8401 | ||
d314774c | 8402 | if (!ops->ndo_set_mac_address) |
1da177e4 LT |
8403 | return -EOPNOTSUPP; |
8404 | if (sa->sa_family != dev->type) | |
8405 | return -EINVAL; | |
8406 | if (!netif_device_present(dev)) | |
8407 | return -ENODEV; | |
d59cdf94 PM |
8408 | err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack); |
8409 | if (err) | |
8410 | return err; | |
d314774c | 8411 | err = ops->ndo_set_mac_address(dev, sa); |
f6521516 JP |
8412 | if (err) |
8413 | return err; | |
fbdeca2d | 8414 | dev->addr_assign_type = NET_ADDR_SET; |
f6521516 | 8415 | call_netdevice_notifiers(NETDEV_CHANGEADDR, dev); |
7bf23575 | 8416 | add_device_randomness(dev->dev_addr, dev->addr_len); |
f6521516 | 8417 | return 0; |
1da177e4 | 8418 | } |
d1b19dff | 8419 | EXPORT_SYMBOL(dev_set_mac_address); |
1da177e4 | 8420 | |
4bf84c35 JP |
8421 | /** |
8422 | * dev_change_carrier - Change device carrier | |
8423 | * @dev: device | |
691b3b7e | 8424 | * @new_carrier: new value |
4bf84c35 JP |
8425 | * |
8426 | * Change device carrier | |
8427 | */ | |
8428 | int dev_change_carrier(struct net_device *dev, bool new_carrier) | |
8429 | { | |
8430 | const struct net_device_ops *ops = dev->netdev_ops; | |
8431 | ||
8432 | if (!ops->ndo_change_carrier) | |
8433 | return -EOPNOTSUPP; | |
8434 | if (!netif_device_present(dev)) | |
8435 | return -ENODEV; | |
8436 | return ops->ndo_change_carrier(dev, new_carrier); | |
8437 | } | |
8438 | EXPORT_SYMBOL(dev_change_carrier); | |
8439 | ||
66b52b0d JP |
8440 | /** |
8441 | * dev_get_phys_port_id - Get device physical port ID | |
8442 | * @dev: device | |
8443 | * @ppid: port ID | |
8444 | * | |
8445 | * Get device physical port ID | |
8446 | */ | |
8447 | int dev_get_phys_port_id(struct net_device *dev, | |
02637fce | 8448 | struct netdev_phys_item_id *ppid) |
66b52b0d JP |
8449 | { |
8450 | const struct net_device_ops *ops = dev->netdev_ops; | |
8451 | ||
8452 | if (!ops->ndo_get_phys_port_id) | |
8453 | return -EOPNOTSUPP; | |
8454 | return ops->ndo_get_phys_port_id(dev, ppid); | |
8455 | } | |
8456 | EXPORT_SYMBOL(dev_get_phys_port_id); | |
8457 | ||
db24a904 DA |
8458 | /** |
8459 | * dev_get_phys_port_name - Get device physical port name | |
8460 | * @dev: device | |
8461 | * @name: port name | |
ed49e650 | 8462 | * @len: limit of bytes to copy to name |
db24a904 DA |
8463 | * |
8464 | * Get device physical port name | |
8465 | */ | |
8466 | int dev_get_phys_port_name(struct net_device *dev, | |
8467 | char *name, size_t len) | |
8468 | { | |
8469 | const struct net_device_ops *ops = dev->netdev_ops; | |
af3836df | 8470 | int err; |
db24a904 | 8471 | |
af3836df JP |
8472 | if (ops->ndo_get_phys_port_name) { |
8473 | err = ops->ndo_get_phys_port_name(dev, name, len); | |
8474 | if (err != -EOPNOTSUPP) | |
8475 | return err; | |
8476 | } | |
8477 | return devlink_compat_phys_port_name_get(dev, name, len); | |
db24a904 DA |
8478 | } |
8479 | EXPORT_SYMBOL(dev_get_phys_port_name); | |
8480 | ||
d6abc596 FF |
8481 | /** |
8482 | * dev_get_port_parent_id - Get the device's port parent identifier | |
8483 | * @dev: network device | |
8484 | * @ppid: pointer to a storage for the port's parent identifier | |
8485 | * @recurse: allow/disallow recursion to lower devices | |
8486 | * | |
8487 | * Get the devices's port parent identifier | |
8488 | */ | |
8489 | int dev_get_port_parent_id(struct net_device *dev, | |
8490 | struct netdev_phys_item_id *ppid, | |
8491 | bool recurse) | |
8492 | { | |
8493 | const struct net_device_ops *ops = dev->netdev_ops; | |
8494 | struct netdev_phys_item_id first = { }; | |
8495 | struct net_device *lower_dev; | |
8496 | struct list_head *iter; | |
7e1146e8 JP |
8497 | int err; |
8498 | ||
8499 | if (ops->ndo_get_port_parent_id) { | |
8500 | err = ops->ndo_get_port_parent_id(dev, ppid); | |
8501 | if (err != -EOPNOTSUPP) | |
8502 | return err; | |
8503 | } | |
d6abc596 | 8504 | |
7e1146e8 JP |
8505 | err = devlink_compat_switch_id_get(dev, ppid); |
8506 | if (!err || err != -EOPNOTSUPP) | |
8507 | return err; | |
d6abc596 FF |
8508 | |
8509 | if (!recurse) | |
7e1146e8 | 8510 | return -EOPNOTSUPP; |
d6abc596 FF |
8511 | |
8512 | netdev_for_each_lower_dev(dev, lower_dev, iter) { | |
8513 | err = dev_get_port_parent_id(lower_dev, ppid, recurse); | |
8514 | if (err) | |
8515 | break; | |
8516 | if (!first.id_len) | |
8517 | first = *ppid; | |
8518 | else if (memcmp(&first, ppid, sizeof(*ppid))) | |
8519 | return -ENODATA; | |
8520 | } | |
8521 | ||
8522 | return err; | |
8523 | } | |
8524 | EXPORT_SYMBOL(dev_get_port_parent_id); | |
8525 | ||
8526 | /** | |
8527 | * netdev_port_same_parent_id - Indicate if two network devices have | |
8528 | * the same port parent identifier | |
8529 | * @a: first network device | |
8530 | * @b: second network device | |
8531 | */ | |
8532 | bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b) | |
8533 | { | |
8534 | struct netdev_phys_item_id a_id = { }; | |
8535 | struct netdev_phys_item_id b_id = { }; | |
8536 | ||
8537 | if (dev_get_port_parent_id(a, &a_id, true) || | |
8538 | dev_get_port_parent_id(b, &b_id, true)) | |
8539 | return false; | |
8540 | ||
8541 | return netdev_phys_item_id_same(&a_id, &b_id); | |
8542 | } | |
8543 | EXPORT_SYMBOL(netdev_port_same_parent_id); | |
8544 | ||
d746d707 AK |
8545 | /** |
8546 | * dev_change_proto_down - update protocol port state information | |
8547 | * @dev: device | |
8548 | * @proto_down: new value | |
8549 | * | |
8550 | * This info can be used by switch drivers to set the phys state of the | |
8551 | * port. | |
8552 | */ | |
8553 | int dev_change_proto_down(struct net_device *dev, bool proto_down) | |
8554 | { | |
8555 | const struct net_device_ops *ops = dev->netdev_ops; | |
8556 | ||
8557 | if (!ops->ndo_change_proto_down) | |
8558 | return -EOPNOTSUPP; | |
8559 | if (!netif_device_present(dev)) | |
8560 | return -ENODEV; | |
8561 | return ops->ndo_change_proto_down(dev, proto_down); | |
8562 | } | |
8563 | EXPORT_SYMBOL(dev_change_proto_down); | |
8564 | ||
b5899679 AR |
8565 | /** |
8566 | * dev_change_proto_down_generic - generic implementation for | |
8567 | * ndo_change_proto_down that sets carrier according to | |
8568 | * proto_down. | |
8569 | * | |
8570 | * @dev: device | |
8571 | * @proto_down: new value | |
8572 | */ | |
8573 | int dev_change_proto_down_generic(struct net_device *dev, bool proto_down) | |
8574 | { | |
8575 | if (proto_down) | |
8576 | netif_carrier_off(dev); | |
8577 | else | |
8578 | netif_carrier_on(dev); | |
8579 | dev->proto_down = proto_down; | |
8580 | return 0; | |
8581 | } | |
8582 | EXPORT_SYMBOL(dev_change_proto_down_generic); | |
8583 | ||
a25717d2 JK |
8584 | u32 __dev_xdp_query(struct net_device *dev, bpf_op_t bpf_op, |
8585 | enum bpf_netdev_command cmd) | |
d67b9cd2 | 8586 | { |
a25717d2 | 8587 | struct netdev_bpf xdp; |
d67b9cd2 | 8588 | |
a25717d2 JK |
8589 | if (!bpf_op) |
8590 | return 0; | |
118b4aa2 | 8591 | |
a25717d2 JK |
8592 | memset(&xdp, 0, sizeof(xdp)); |
8593 | xdp.command = cmd; | |
118b4aa2 | 8594 | |
a25717d2 JK |
8595 | /* Query must always succeed. */ |
8596 | WARN_ON(bpf_op(dev, &xdp) < 0 && cmd == XDP_QUERY_PROG); | |
58038695 | 8597 | |
6b867589 | 8598 | return xdp.prog_id; |
d67b9cd2 DB |
8599 | } |
8600 | ||
f4e63525 | 8601 | static int dev_xdp_install(struct net_device *dev, bpf_op_t bpf_op, |
32d60277 | 8602 | struct netlink_ext_ack *extack, u32 flags, |
d67b9cd2 DB |
8603 | struct bpf_prog *prog) |
8604 | { | |
7e6897f9 BT |
8605 | bool non_hw = !(flags & XDP_FLAGS_HW_MODE); |
8606 | struct bpf_prog *prev_prog = NULL; | |
f4e63525 | 8607 | struct netdev_bpf xdp; |
7e6897f9 BT |
8608 | int err; |
8609 | ||
8610 | if (non_hw) { | |
8611 | prev_prog = bpf_prog_by_id(__dev_xdp_query(dev, bpf_op, | |
8612 | XDP_QUERY_PROG)); | |
8613 | if (IS_ERR(prev_prog)) | |
8614 | prev_prog = NULL; | |
8615 | } | |
d67b9cd2 DB |
8616 | |
8617 | memset(&xdp, 0, sizeof(xdp)); | |
ee5d032f JK |
8618 | if (flags & XDP_FLAGS_HW_MODE) |
8619 | xdp.command = XDP_SETUP_PROG_HW; | |
8620 | else | |
8621 | xdp.command = XDP_SETUP_PROG; | |
d67b9cd2 | 8622 | xdp.extack = extack; |
32d60277 | 8623 | xdp.flags = flags; |
d67b9cd2 DB |
8624 | xdp.prog = prog; |
8625 | ||
7e6897f9 BT |
8626 | err = bpf_op(dev, &xdp); |
8627 | if (!err && non_hw) | |
8628 | bpf_prog_change_xdp(prev_prog, prog); | |
8629 | ||
8630 | if (prev_prog) | |
8631 | bpf_prog_put(prev_prog); | |
8632 | ||
8633 | return err; | |
d67b9cd2 DB |
8634 | } |
8635 | ||
bd0b2e7f JK |
8636 | static void dev_xdp_uninstall(struct net_device *dev) |
8637 | { | |
8638 | struct netdev_bpf xdp; | |
8639 | bpf_op_t ndo_bpf; | |
8640 | ||
8641 | /* Remove generic XDP */ | |
8642 | WARN_ON(dev_xdp_install(dev, generic_xdp_install, NULL, 0, NULL)); | |
8643 | ||
8644 | /* Remove from the driver */ | |
8645 | ndo_bpf = dev->netdev_ops->ndo_bpf; | |
8646 | if (!ndo_bpf) | |
8647 | return; | |
8648 | ||
a25717d2 JK |
8649 | memset(&xdp, 0, sizeof(xdp)); |
8650 | xdp.command = XDP_QUERY_PROG; | |
8651 | WARN_ON(ndo_bpf(dev, &xdp)); | |
8652 | if (xdp.prog_id) | |
8653 | WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags, | |
8654 | NULL)); | |
bd0b2e7f | 8655 | |
a25717d2 JK |
8656 | /* Remove HW offload */ |
8657 | memset(&xdp, 0, sizeof(xdp)); | |
8658 | xdp.command = XDP_QUERY_PROG_HW; | |
8659 | if (!ndo_bpf(dev, &xdp) && xdp.prog_id) | |
8660 | WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags, | |
8661 | NULL)); | |
bd0b2e7f JK |
8662 | } |
8663 | ||
a7862b45 BB |
8664 | /** |
8665 | * dev_change_xdp_fd - set or clear a bpf program for a device rx path | |
8666 | * @dev: device | |
b5d60989 | 8667 | * @extack: netlink extended ack |
a7862b45 | 8668 | * @fd: new program fd or negative value to clear |
85de8576 | 8669 | * @flags: xdp-related flags |
a7862b45 BB |
8670 | * |
8671 | * Set or clear a bpf program for a device | |
8672 | */ | |
ddf9f970 JK |
8673 | int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack, |
8674 | int fd, u32 flags) | |
a7862b45 BB |
8675 | { |
8676 | const struct net_device_ops *ops = dev->netdev_ops; | |
a25717d2 | 8677 | enum bpf_netdev_command query; |
a7862b45 | 8678 | struct bpf_prog *prog = NULL; |
f4e63525 | 8679 | bpf_op_t bpf_op, bpf_chk; |
9ee963d6 | 8680 | bool offload; |
a7862b45 BB |
8681 | int err; |
8682 | ||
85de8576 DB |
8683 | ASSERT_RTNL(); |
8684 | ||
9ee963d6 JK |
8685 | offload = flags & XDP_FLAGS_HW_MODE; |
8686 | query = offload ? XDP_QUERY_PROG_HW : XDP_QUERY_PROG; | |
a25717d2 | 8687 | |
f4e63525 | 8688 | bpf_op = bpf_chk = ops->ndo_bpf; |
01dde20c MF |
8689 | if (!bpf_op && (flags & (XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE))) { |
8690 | NL_SET_ERR_MSG(extack, "underlying driver does not support XDP in native mode"); | |
0489df9a | 8691 | return -EOPNOTSUPP; |
01dde20c | 8692 | } |
f4e63525 JK |
8693 | if (!bpf_op || (flags & XDP_FLAGS_SKB_MODE)) |
8694 | bpf_op = generic_xdp_install; | |
8695 | if (bpf_op == bpf_chk) | |
8696 | bpf_chk = generic_xdp_install; | |
b5cdae32 | 8697 | |
a7862b45 | 8698 | if (fd >= 0) { |
c14a9f63 MM |
8699 | u32 prog_id; |
8700 | ||
9ee963d6 | 8701 | if (!offload && __dev_xdp_query(dev, bpf_chk, XDP_QUERY_PROG)) { |
01dde20c | 8702 | NL_SET_ERR_MSG(extack, "native and generic XDP can't be active at the same time"); |
d67b9cd2 | 8703 | return -EEXIST; |
01dde20c | 8704 | } |
c14a9f63 MM |
8705 | |
8706 | prog_id = __dev_xdp_query(dev, bpf_op, query); | |
8707 | if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && prog_id) { | |
01dde20c | 8708 | NL_SET_ERR_MSG(extack, "XDP program already attached"); |
d67b9cd2 | 8709 | return -EBUSY; |
01dde20c | 8710 | } |
85de8576 | 8711 | |
288b3de5 JK |
8712 | prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP, |
8713 | bpf_op == ops->ndo_bpf); | |
a7862b45 BB |
8714 | if (IS_ERR(prog)) |
8715 | return PTR_ERR(prog); | |
441a3303 | 8716 | |
9ee963d6 | 8717 | if (!offload && bpf_prog_is_dev_bound(prog->aux)) { |
441a3303 JK |
8718 | NL_SET_ERR_MSG(extack, "using device-bound program without HW_MODE flag is not supported"); |
8719 | bpf_prog_put(prog); | |
8720 | return -EINVAL; | |
8721 | } | |
c14a9f63 | 8722 | |
aefc3e72 JK |
8723 | /* prog->aux->id may be 0 for orphaned device-bound progs */ |
8724 | if (prog->aux->id && prog->aux->id == prog_id) { | |
c14a9f63 MM |
8725 | bpf_prog_put(prog); |
8726 | return 0; | |
8727 | } | |
8728 | } else { | |
8729 | if (!__dev_xdp_query(dev, bpf_op, query)) | |
8730 | return 0; | |
a7862b45 BB |
8731 | } |
8732 | ||
f4e63525 | 8733 | err = dev_xdp_install(dev, bpf_op, extack, flags, prog); |
a7862b45 BB |
8734 | if (err < 0 && prog) |
8735 | bpf_prog_put(prog); | |
8736 | ||
8737 | return err; | |
8738 | } | |
a7862b45 | 8739 | |
1da177e4 LT |
8740 | /** |
8741 | * dev_new_index - allocate an ifindex | |
c4ea43c5 | 8742 | * @net: the applicable net namespace |
1da177e4 LT |
8743 | * |
8744 | * Returns a suitable unique value for a new device interface | |
8745 | * number. The caller must hold the rtnl semaphore or the | |
8746 | * dev_base_lock to be sure it remains unique. | |
8747 | */ | |
881d966b | 8748 | static int dev_new_index(struct net *net) |
1da177e4 | 8749 | { |
aa79e66e | 8750 | int ifindex = net->ifindex; |
f4563a75 | 8751 | |
1da177e4 LT |
8752 | for (;;) { |
8753 | if (++ifindex <= 0) | |
8754 | ifindex = 1; | |
881d966b | 8755 | if (!__dev_get_by_index(net, ifindex)) |
aa79e66e | 8756 | return net->ifindex = ifindex; |
1da177e4 LT |
8757 | } |
8758 | } | |
8759 | ||
1da177e4 | 8760 | /* Delayed registration/unregisteration */ |
3b5b34fd | 8761 | static LIST_HEAD(net_todo_list); |
200b916f | 8762 | DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq); |
1da177e4 | 8763 | |
6f05f629 | 8764 | static void net_set_todo(struct net_device *dev) |
1da177e4 | 8765 | { |
1da177e4 | 8766 | list_add_tail(&dev->todo_list, &net_todo_list); |
50624c93 | 8767 | dev_net(dev)->dev_unreg_count++; |
1da177e4 LT |
8768 | } |
8769 | ||
9b5e383c | 8770 | static void rollback_registered_many(struct list_head *head) |
93ee31f1 | 8771 | { |
e93737b0 | 8772 | struct net_device *dev, *tmp; |
5cde2829 | 8773 | LIST_HEAD(close_head); |
9b5e383c | 8774 | |
93ee31f1 DL |
8775 | BUG_ON(dev_boot_phase); |
8776 | ASSERT_RTNL(); | |
8777 | ||
e93737b0 | 8778 | list_for_each_entry_safe(dev, tmp, head, unreg_list) { |
9b5e383c | 8779 | /* Some devices call without registering |
e93737b0 KK |
8780 | * for initialization unwind. Remove those |
8781 | * devices and proceed with the remaining. | |
9b5e383c ED |
8782 | */ |
8783 | if (dev->reg_state == NETREG_UNINITIALIZED) { | |
7b6cd1ce JP |
8784 | pr_debug("unregister_netdevice: device %s/%p never was registered\n", |
8785 | dev->name, dev); | |
93ee31f1 | 8786 | |
9b5e383c | 8787 | WARN_ON(1); |
e93737b0 KK |
8788 | list_del(&dev->unreg_list); |
8789 | continue; | |
9b5e383c | 8790 | } |
449f4544 | 8791 | dev->dismantle = true; |
9b5e383c | 8792 | BUG_ON(dev->reg_state != NETREG_REGISTERED); |
44345724 | 8793 | } |
93ee31f1 | 8794 | |
44345724 | 8795 | /* If device is running, close it first. */ |
5cde2829 EB |
8796 | list_for_each_entry(dev, head, unreg_list) |
8797 | list_add_tail(&dev->close_list, &close_head); | |
99c4a26a | 8798 | dev_close_many(&close_head, true); |
93ee31f1 | 8799 | |
44345724 | 8800 | list_for_each_entry(dev, head, unreg_list) { |
9b5e383c ED |
8801 | /* And unlink it from device chain. */ |
8802 | unlist_netdevice(dev); | |
93ee31f1 | 8803 | |
9b5e383c ED |
8804 | dev->reg_state = NETREG_UNREGISTERING; |
8805 | } | |
41852497 | 8806 | flush_all_backlogs(); |
93ee31f1 DL |
8807 | |
8808 | synchronize_net(); | |
8809 | ||
9b5e383c | 8810 | list_for_each_entry(dev, head, unreg_list) { |
395eea6c MB |
8811 | struct sk_buff *skb = NULL; |
8812 | ||
9b5e383c ED |
8813 | /* Shutdown queueing discipline. */ |
8814 | dev_shutdown(dev); | |
93ee31f1 | 8815 | |
bd0b2e7f | 8816 | dev_xdp_uninstall(dev); |
93ee31f1 | 8817 | |
9b5e383c | 8818 | /* Notify protocols, that we are about to destroy |
eb13da1a | 8819 | * this device. They should clean all the things. |
8820 | */ | |
9b5e383c | 8821 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
93ee31f1 | 8822 | |
395eea6c MB |
8823 | if (!dev->rtnl_link_ops || |
8824 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) | |
3d3ea5af | 8825 | skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0, |
38e01b30 | 8826 | GFP_KERNEL, NULL, 0); |
395eea6c | 8827 | |
9b5e383c ED |
8828 | /* |
8829 | * Flush the unicast and multicast chains | |
8830 | */ | |
a748ee24 | 8831 | dev_uc_flush(dev); |
22bedad3 | 8832 | dev_mc_flush(dev); |
93ee31f1 | 8833 | |
36fbf1e5 | 8834 | netdev_name_node_alt_flush(dev); |
ff927412 JP |
8835 | netdev_name_node_free(dev->name_node); |
8836 | ||
9b5e383c ED |
8837 | if (dev->netdev_ops->ndo_uninit) |
8838 | dev->netdev_ops->ndo_uninit(dev); | |
93ee31f1 | 8839 | |
395eea6c MB |
8840 | if (skb) |
8841 | rtmsg_ifinfo_send(skb, dev, GFP_KERNEL); | |
56bfa7ee | 8842 | |
9ff162a8 JP |
8843 | /* Notifier chain MUST detach us all upper devices. */ |
8844 | WARN_ON(netdev_has_any_upper_dev(dev)); | |
0f524a80 | 8845 | WARN_ON(netdev_has_any_lower_dev(dev)); |
93ee31f1 | 8846 | |
9b5e383c ED |
8847 | /* Remove entries from kobject tree */ |
8848 | netdev_unregister_kobject(dev); | |
024e9679 AD |
8849 | #ifdef CONFIG_XPS |
8850 | /* Remove XPS queueing entries */ | |
8851 | netif_reset_xps_queues_gt(dev, 0); | |
8852 | #endif | |
9b5e383c | 8853 | } |
93ee31f1 | 8854 | |
850a545b | 8855 | synchronize_net(); |
395264d5 | 8856 | |
a5ee1551 | 8857 | list_for_each_entry(dev, head, unreg_list) |
9b5e383c ED |
8858 | dev_put(dev); |
8859 | } | |
8860 | ||
8861 | static void rollback_registered(struct net_device *dev) | |
8862 | { | |
8863 | LIST_HEAD(single); | |
8864 | ||
8865 | list_add(&dev->unreg_list, &single); | |
8866 | rollback_registered_many(&single); | |
ceaaec98 | 8867 | list_del(&single); |
93ee31f1 DL |
8868 | } |
8869 | ||
fd867d51 JW |
8870 | static netdev_features_t netdev_sync_upper_features(struct net_device *lower, |
8871 | struct net_device *upper, netdev_features_t features) | |
8872 | { | |
8873 | netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; | |
8874 | netdev_features_t feature; | |
5ba3f7d6 | 8875 | int feature_bit; |
fd867d51 | 8876 | |
3b89ea9c | 8877 | for_each_netdev_feature(upper_disables, feature_bit) { |
5ba3f7d6 | 8878 | feature = __NETIF_F_BIT(feature_bit); |
fd867d51 JW |
8879 | if (!(upper->wanted_features & feature) |
8880 | && (features & feature)) { | |
8881 | netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n", | |
8882 | &feature, upper->name); | |
8883 | features &= ~feature; | |
8884 | } | |
8885 | } | |
8886 | ||
8887 | return features; | |
8888 | } | |
8889 | ||
8890 | static void netdev_sync_lower_features(struct net_device *upper, | |
8891 | struct net_device *lower, netdev_features_t features) | |
8892 | { | |
8893 | netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES; | |
8894 | netdev_features_t feature; | |
5ba3f7d6 | 8895 | int feature_bit; |
fd867d51 | 8896 | |
3b89ea9c | 8897 | for_each_netdev_feature(upper_disables, feature_bit) { |
5ba3f7d6 | 8898 | feature = __NETIF_F_BIT(feature_bit); |
fd867d51 JW |
8899 | if (!(features & feature) && (lower->features & feature)) { |
8900 | netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n", | |
8901 | &feature, lower->name); | |
8902 | lower->wanted_features &= ~feature; | |
8903 | netdev_update_features(lower); | |
8904 | ||
8905 | if (unlikely(lower->features & feature)) | |
8906 | netdev_WARN(upper, "failed to disable %pNF on %s!\n", | |
8907 | &feature, lower->name); | |
8908 | } | |
8909 | } | |
8910 | } | |
8911 | ||
c8f44aff MM |
8912 | static netdev_features_t netdev_fix_features(struct net_device *dev, |
8913 | netdev_features_t features) | |
b63365a2 | 8914 | { |
57422dc5 MM |
8915 | /* Fix illegal checksum combinations */ |
8916 | if ((features & NETIF_F_HW_CSUM) && | |
8917 | (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) { | |
6f404e44 | 8918 | netdev_warn(dev, "mixed HW and IP checksum settings.\n"); |
57422dc5 MM |
8919 | features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM); |
8920 | } | |
8921 | ||
b63365a2 | 8922 | /* TSO requires that SG is present as well. */ |
ea2d3688 | 8923 | if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) { |
6f404e44 | 8924 | netdev_dbg(dev, "Dropping TSO features since no SG feature.\n"); |
ea2d3688 | 8925 | features &= ~NETIF_F_ALL_TSO; |
b63365a2 HX |
8926 | } |
8927 | ||
ec5f0615 PS |
8928 | if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) && |
8929 | !(features & NETIF_F_IP_CSUM)) { | |
8930 | netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n"); | |
8931 | features &= ~NETIF_F_TSO; | |
8932 | features &= ~NETIF_F_TSO_ECN; | |
8933 | } | |
8934 | ||
8935 | if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) && | |
8936 | !(features & NETIF_F_IPV6_CSUM)) { | |
8937 | netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n"); | |
8938 | features &= ~NETIF_F_TSO6; | |
8939 | } | |
8940 | ||
b1dc497b AD |
8941 | /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */ |
8942 | if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO)) | |
8943 | features &= ~NETIF_F_TSO_MANGLEID; | |
8944 | ||
31d8b9e0 BH |
8945 | /* TSO ECN requires that TSO is present as well. */ |
8946 | if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN) | |
8947 | features &= ~NETIF_F_TSO_ECN; | |
8948 | ||
212b573f MM |
8949 | /* Software GSO depends on SG. */ |
8950 | if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) { | |
6f404e44 | 8951 | netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n"); |
212b573f MM |
8952 | features &= ~NETIF_F_GSO; |
8953 | } | |
8954 | ||
802ab55a AD |
8955 | /* GSO partial features require GSO partial be set */ |
8956 | if ((features & dev->gso_partial_features) && | |
8957 | !(features & NETIF_F_GSO_PARTIAL)) { | |
8958 | netdev_dbg(dev, | |
8959 | "Dropping partially supported GSO features since no GSO partial.\n"); | |
8960 | features &= ~dev->gso_partial_features; | |
8961 | } | |
8962 | ||
fb1f5f79 MC |
8963 | if (!(features & NETIF_F_RXCSUM)) { |
8964 | /* NETIF_F_GRO_HW implies doing RXCSUM since every packet | |
8965 | * successfully merged by hardware must also have the | |
8966 | * checksum verified by hardware. If the user does not | |
8967 | * want to enable RXCSUM, logically, we should disable GRO_HW. | |
8968 | */ | |
8969 | if (features & NETIF_F_GRO_HW) { | |
8970 | netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n"); | |
8971 | features &= ~NETIF_F_GRO_HW; | |
8972 | } | |
8973 | } | |
8974 | ||
de8d5ab2 GP |
8975 | /* LRO/HW-GRO features cannot be combined with RX-FCS */ |
8976 | if (features & NETIF_F_RXFCS) { | |
8977 | if (features & NETIF_F_LRO) { | |
8978 | netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n"); | |
8979 | features &= ~NETIF_F_LRO; | |
8980 | } | |
8981 | ||
8982 | if (features & NETIF_F_GRO_HW) { | |
8983 | netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n"); | |
8984 | features &= ~NETIF_F_GRO_HW; | |
8985 | } | |
e6c6a929 GP |
8986 | } |
8987 | ||
b63365a2 HX |
8988 | return features; |
8989 | } | |
b63365a2 | 8990 | |
6cb6a27c | 8991 | int __netdev_update_features(struct net_device *dev) |
5455c699 | 8992 | { |
fd867d51 | 8993 | struct net_device *upper, *lower; |
c8f44aff | 8994 | netdev_features_t features; |
fd867d51 | 8995 | struct list_head *iter; |
e7868a85 | 8996 | int err = -1; |
5455c699 | 8997 | |
87267485 MM |
8998 | ASSERT_RTNL(); |
8999 | ||
5455c699 MM |
9000 | features = netdev_get_wanted_features(dev); |
9001 | ||
9002 | if (dev->netdev_ops->ndo_fix_features) | |
9003 | features = dev->netdev_ops->ndo_fix_features(dev, features); | |
9004 | ||
9005 | /* driver might be less strict about feature dependencies */ | |
9006 | features = netdev_fix_features(dev, features); | |
9007 | ||
fd867d51 JW |
9008 | /* some features can't be enabled if they're off an an upper device */ |
9009 | netdev_for_each_upper_dev_rcu(dev, upper, iter) | |
9010 | features = netdev_sync_upper_features(dev, upper, features); | |
9011 | ||
5455c699 | 9012 | if (dev->features == features) |
e7868a85 | 9013 | goto sync_lower; |
5455c699 | 9014 | |
c8f44aff MM |
9015 | netdev_dbg(dev, "Features changed: %pNF -> %pNF\n", |
9016 | &dev->features, &features); | |
5455c699 MM |
9017 | |
9018 | if (dev->netdev_ops->ndo_set_features) | |
9019 | err = dev->netdev_ops->ndo_set_features(dev, features); | |
5f8dc33e NA |
9020 | else |
9021 | err = 0; | |
5455c699 | 9022 | |
6cb6a27c | 9023 | if (unlikely(err < 0)) { |
5455c699 | 9024 | netdev_err(dev, |
c8f44aff MM |
9025 | "set_features() failed (%d); wanted %pNF, left %pNF\n", |
9026 | err, &features, &dev->features); | |
17b85d29 NA |
9027 | /* return non-0 since some features might have changed and |
9028 | * it's better to fire a spurious notification than miss it | |
9029 | */ | |
9030 | return -1; | |
6cb6a27c MM |
9031 | } |
9032 | ||
e7868a85 | 9033 | sync_lower: |
fd867d51 JW |
9034 | /* some features must be disabled on lower devices when disabled |
9035 | * on an upper device (think: bonding master or bridge) | |
9036 | */ | |
9037 | netdev_for_each_lower_dev(dev, lower, iter) | |
9038 | netdev_sync_lower_features(dev, lower, features); | |
9039 | ||
ae847f40 SD |
9040 | if (!err) { |
9041 | netdev_features_t diff = features ^ dev->features; | |
9042 | ||
9043 | if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) { | |
9044 | /* udp_tunnel_{get,drop}_rx_info both need | |
9045 | * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the | |
9046 | * device, or they won't do anything. | |
9047 | * Thus we need to update dev->features | |
9048 | * *before* calling udp_tunnel_get_rx_info, | |
9049 | * but *after* calling udp_tunnel_drop_rx_info. | |
9050 | */ | |
9051 | if (features & NETIF_F_RX_UDP_TUNNEL_PORT) { | |
9052 | dev->features = features; | |
9053 | udp_tunnel_get_rx_info(dev); | |
9054 | } else { | |
9055 | udp_tunnel_drop_rx_info(dev); | |
9056 | } | |
9057 | } | |
9058 | ||
9daae9bd GP |
9059 | if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) { |
9060 | if (features & NETIF_F_HW_VLAN_CTAG_FILTER) { | |
9061 | dev->features = features; | |
9062 | err |= vlan_get_rx_ctag_filter_info(dev); | |
9063 | } else { | |
9064 | vlan_drop_rx_ctag_filter_info(dev); | |
9065 | } | |
9066 | } | |
9067 | ||
9068 | if (diff & NETIF_F_HW_VLAN_STAG_FILTER) { | |
9069 | if (features & NETIF_F_HW_VLAN_STAG_FILTER) { | |
9070 | dev->features = features; | |
9071 | err |= vlan_get_rx_stag_filter_info(dev); | |
9072 | } else { | |
9073 | vlan_drop_rx_stag_filter_info(dev); | |
9074 | } | |
9075 | } | |
9076 | ||
6cb6a27c | 9077 | dev->features = features; |
ae847f40 | 9078 | } |
6cb6a27c | 9079 | |
e7868a85 | 9080 | return err < 0 ? 0 : 1; |
6cb6a27c MM |
9081 | } |
9082 | ||
afe12cc8 MM |
9083 | /** |
9084 | * netdev_update_features - recalculate device features | |
9085 | * @dev: the device to check | |
9086 | * | |
9087 | * Recalculate dev->features set and send notifications if it | |
9088 | * has changed. Should be called after driver or hardware dependent | |
9089 | * conditions might have changed that influence the features. | |
9090 | */ | |
6cb6a27c MM |
9091 | void netdev_update_features(struct net_device *dev) |
9092 | { | |
9093 | if (__netdev_update_features(dev)) | |
9094 | netdev_features_change(dev); | |
5455c699 MM |
9095 | } |
9096 | EXPORT_SYMBOL(netdev_update_features); | |
9097 | ||
afe12cc8 MM |
9098 | /** |
9099 | * netdev_change_features - recalculate device features | |
9100 | * @dev: the device to check | |
9101 | * | |
9102 | * Recalculate dev->features set and send notifications even | |
9103 | * if they have not changed. Should be called instead of | |
9104 | * netdev_update_features() if also dev->vlan_features might | |
9105 | * have changed to allow the changes to be propagated to stacked | |
9106 | * VLAN devices. | |
9107 | */ | |
9108 | void netdev_change_features(struct net_device *dev) | |
9109 | { | |
9110 | __netdev_update_features(dev); | |
9111 | netdev_features_change(dev); | |
9112 | } | |
9113 | EXPORT_SYMBOL(netdev_change_features); | |
9114 | ||
fc4a7489 PM |
9115 | /** |
9116 | * netif_stacked_transfer_operstate - transfer operstate | |
9117 | * @rootdev: the root or lower level device to transfer state from | |
9118 | * @dev: the device to transfer operstate to | |
9119 | * | |
9120 | * Transfer operational state from root to device. This is normally | |
9121 | * called when a stacking relationship exists between the root | |
9122 | * device and the device(a leaf device). | |
9123 | */ | |
9124 | void netif_stacked_transfer_operstate(const struct net_device *rootdev, | |
9125 | struct net_device *dev) | |
9126 | { | |
9127 | if (rootdev->operstate == IF_OPER_DORMANT) | |
9128 | netif_dormant_on(dev); | |
9129 | else | |
9130 | netif_dormant_off(dev); | |
9131 | ||
0575c86b ZS |
9132 | if (netif_carrier_ok(rootdev)) |
9133 | netif_carrier_on(dev); | |
9134 | else | |
9135 | netif_carrier_off(dev); | |
fc4a7489 PM |
9136 | } |
9137 | EXPORT_SYMBOL(netif_stacked_transfer_operstate); | |
9138 | ||
1b4bf461 ED |
9139 | static int netif_alloc_rx_queues(struct net_device *dev) |
9140 | { | |
1b4bf461 | 9141 | unsigned int i, count = dev->num_rx_queues; |
bd25fa7b | 9142 | struct netdev_rx_queue *rx; |
10595902 | 9143 | size_t sz = count * sizeof(*rx); |
e817f856 | 9144 | int err = 0; |
1b4bf461 | 9145 | |
bd25fa7b | 9146 | BUG_ON(count < 1); |
1b4bf461 | 9147 | |
dcda9b04 | 9148 | rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
da6bc57a MH |
9149 | if (!rx) |
9150 | return -ENOMEM; | |
9151 | ||
bd25fa7b TH |
9152 | dev->_rx = rx; |
9153 | ||
e817f856 | 9154 | for (i = 0; i < count; i++) { |
fe822240 | 9155 | rx[i].dev = dev; |
e817f856 JDB |
9156 | |
9157 | /* XDP RX-queue setup */ | |
9158 | err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i); | |
9159 | if (err < 0) | |
9160 | goto err_rxq_info; | |
9161 | } | |
1b4bf461 | 9162 | return 0; |
e817f856 JDB |
9163 | |
9164 | err_rxq_info: | |
9165 | /* Rollback successful reg's and free other resources */ | |
9166 | while (i--) | |
9167 | xdp_rxq_info_unreg(&rx[i].xdp_rxq); | |
141b52a9 | 9168 | kvfree(dev->_rx); |
e817f856 JDB |
9169 | dev->_rx = NULL; |
9170 | return err; | |
9171 | } | |
9172 | ||
9173 | static void netif_free_rx_queues(struct net_device *dev) | |
9174 | { | |
9175 | unsigned int i, count = dev->num_rx_queues; | |
e817f856 JDB |
9176 | |
9177 | /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */ | |
9178 | if (!dev->_rx) | |
9179 | return; | |
9180 | ||
e817f856 | 9181 | for (i = 0; i < count; i++) |
82aaff2f JK |
9182 | xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq); |
9183 | ||
9184 | kvfree(dev->_rx); | |
1b4bf461 ED |
9185 | } |
9186 | ||
aa942104 CG |
9187 | static void netdev_init_one_queue(struct net_device *dev, |
9188 | struct netdev_queue *queue, void *_unused) | |
9189 | { | |
9190 | /* Initialize queue lock */ | |
9191 | spin_lock_init(&queue->_xmit_lock); | |
ab92d68f | 9192 | lockdep_set_class(&queue->_xmit_lock, &dev->qdisc_xmit_lock_key); |
aa942104 | 9193 | queue->xmit_lock_owner = -1; |
b236da69 | 9194 | netdev_queue_numa_node_write(queue, NUMA_NO_NODE); |
aa942104 | 9195 | queue->dev = dev; |
114cf580 TH |
9196 | #ifdef CONFIG_BQL |
9197 | dql_init(&queue->dql, HZ); | |
9198 | #endif | |
aa942104 CG |
9199 | } |
9200 | ||
60877a32 ED |
9201 | static void netif_free_tx_queues(struct net_device *dev) |
9202 | { | |
4cb28970 | 9203 | kvfree(dev->_tx); |
60877a32 ED |
9204 | } |
9205 | ||
e6484930 TH |
9206 | static int netif_alloc_netdev_queues(struct net_device *dev) |
9207 | { | |
9208 | unsigned int count = dev->num_tx_queues; | |
9209 | struct netdev_queue *tx; | |
60877a32 | 9210 | size_t sz = count * sizeof(*tx); |
e6484930 | 9211 | |
d339727c ED |
9212 | if (count < 1 || count > 0xffff) |
9213 | return -EINVAL; | |
62b5942a | 9214 | |
dcda9b04 | 9215 | tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
da6bc57a MH |
9216 | if (!tx) |
9217 | return -ENOMEM; | |
9218 | ||
e6484930 | 9219 | dev->_tx = tx; |
1d24eb48 | 9220 | |
e6484930 TH |
9221 | netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL); |
9222 | spin_lock_init(&dev->tx_global_lock); | |
aa942104 CG |
9223 | |
9224 | return 0; | |
e6484930 TH |
9225 | } |
9226 | ||
a2029240 DV |
9227 | void netif_tx_stop_all_queues(struct net_device *dev) |
9228 | { | |
9229 | unsigned int i; | |
9230 | ||
9231 | for (i = 0; i < dev->num_tx_queues; i++) { | |
9232 | struct netdev_queue *txq = netdev_get_tx_queue(dev, i); | |
f4563a75 | 9233 | |
a2029240 DV |
9234 | netif_tx_stop_queue(txq); |
9235 | } | |
9236 | } | |
9237 | EXPORT_SYMBOL(netif_tx_stop_all_queues); | |
9238 | ||
ab92d68f TY |
9239 | static void netdev_register_lockdep_key(struct net_device *dev) |
9240 | { | |
9241 | lockdep_register_key(&dev->qdisc_tx_busylock_key); | |
9242 | lockdep_register_key(&dev->qdisc_running_key); | |
9243 | lockdep_register_key(&dev->qdisc_xmit_lock_key); | |
9244 | lockdep_register_key(&dev->addr_list_lock_key); | |
9245 | } | |
9246 | ||
9247 | static void netdev_unregister_lockdep_key(struct net_device *dev) | |
9248 | { | |
9249 | lockdep_unregister_key(&dev->qdisc_tx_busylock_key); | |
9250 | lockdep_unregister_key(&dev->qdisc_running_key); | |
9251 | lockdep_unregister_key(&dev->qdisc_xmit_lock_key); | |
9252 | lockdep_unregister_key(&dev->addr_list_lock_key); | |
9253 | } | |
9254 | ||
9255 | void netdev_update_lockdep_key(struct net_device *dev) | |
9256 | { | |
ab92d68f | 9257 | lockdep_unregister_key(&dev->addr_list_lock_key); |
ab92d68f TY |
9258 | lockdep_register_key(&dev->addr_list_lock_key); |
9259 | ||
9260 | lockdep_set_class(&dev->addr_list_lock, &dev->addr_list_lock_key); | |
ab92d68f TY |
9261 | } |
9262 | EXPORT_SYMBOL(netdev_update_lockdep_key); | |
9263 | ||
1da177e4 LT |
9264 | /** |
9265 | * register_netdevice - register a network device | |
9266 | * @dev: device to register | |
9267 | * | |
9268 | * Take a completed network device structure and add it to the kernel | |
9269 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier | |
9270 | * chain. 0 is returned on success. A negative errno code is returned | |
9271 | * on a failure to set up the device, or if the name is a duplicate. | |
9272 | * | |
9273 | * Callers must hold the rtnl semaphore. You may want | |
9274 | * register_netdev() instead of this. | |
9275 | * | |
9276 | * BUGS: | |
9277 | * The locking appears insufficient to guarantee two parallel registers | |
9278 | * will not get the same name. | |
9279 | */ | |
9280 | ||
9281 | int register_netdevice(struct net_device *dev) | |
9282 | { | |
1da177e4 | 9283 | int ret; |
d314774c | 9284 | struct net *net = dev_net(dev); |
1da177e4 | 9285 | |
e283de3a FF |
9286 | BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE < |
9287 | NETDEV_FEATURE_COUNT); | |
1da177e4 LT |
9288 | BUG_ON(dev_boot_phase); |
9289 | ASSERT_RTNL(); | |
9290 | ||
b17a7c17 SH |
9291 | might_sleep(); |
9292 | ||
1da177e4 LT |
9293 | /* When net_device's are persistent, this will be fatal. */ |
9294 | BUG_ON(dev->reg_state != NETREG_UNINITIALIZED); | |
d314774c | 9295 | BUG_ON(!net); |
1da177e4 | 9296 | |
f1f28aa3 | 9297 | spin_lock_init(&dev->addr_list_lock); |
ab92d68f | 9298 | lockdep_set_class(&dev->addr_list_lock, &dev->addr_list_lock_key); |
1da177e4 | 9299 | |
828de4f6 | 9300 | ret = dev_get_valid_name(net, dev, dev->name); |
0696c3a8 PP |
9301 | if (ret < 0) |
9302 | goto out; | |
9303 | ||
9077f052 | 9304 | ret = -ENOMEM; |
ff927412 JP |
9305 | dev->name_node = netdev_name_node_head_alloc(dev); |
9306 | if (!dev->name_node) | |
9307 | goto out; | |
9308 | ||
1da177e4 | 9309 | /* Init, if this function is available */ |
d314774c SH |
9310 | if (dev->netdev_ops->ndo_init) { |
9311 | ret = dev->netdev_ops->ndo_init(dev); | |
1da177e4 LT |
9312 | if (ret) { |
9313 | if (ret > 0) | |
9314 | ret = -EIO; | |
42c17fa6 | 9315 | goto err_free_name; |
1da177e4 LT |
9316 | } |
9317 | } | |
4ec93edb | 9318 | |
f646968f PM |
9319 | if (((dev->hw_features | dev->features) & |
9320 | NETIF_F_HW_VLAN_CTAG_FILTER) && | |
d2ed273d MM |
9321 | (!dev->netdev_ops->ndo_vlan_rx_add_vid || |
9322 | !dev->netdev_ops->ndo_vlan_rx_kill_vid)) { | |
9323 | netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n"); | |
9324 | ret = -EINVAL; | |
9325 | goto err_uninit; | |
9326 | } | |
9327 | ||
9c7dafbf PE |
9328 | ret = -EBUSY; |
9329 | if (!dev->ifindex) | |
9330 | dev->ifindex = dev_new_index(net); | |
9331 | else if (__dev_get_by_index(net, dev->ifindex)) | |
9332 | goto err_uninit; | |
9333 | ||
5455c699 MM |
9334 | /* Transfer changeable features to wanted_features and enable |
9335 | * software offloads (GSO and GRO). | |
9336 | */ | |
1a3c998f | 9337 | dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF); |
14d1232f | 9338 | dev->features |= NETIF_F_SOFT_FEATURES; |
d764a122 SD |
9339 | |
9340 | if (dev->netdev_ops->ndo_udp_tunnel_add) { | |
9341 | dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT; | |
9342 | dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT; | |
9343 | } | |
9344 | ||
14d1232f | 9345 | dev->wanted_features = dev->features & dev->hw_features; |
1da177e4 | 9346 | |
cbc53e08 | 9347 | if (!(dev->flags & IFF_LOOPBACK)) |
34324dc2 | 9348 | dev->hw_features |= NETIF_F_NOCACHE_COPY; |
cbc53e08 | 9349 | |
7f348a60 AD |
9350 | /* If IPv4 TCP segmentation offload is supported we should also |
9351 | * allow the device to enable segmenting the frame with the option | |
9352 | * of ignoring a static IP ID value. This doesn't enable the | |
9353 | * feature itself but allows the user to enable it later. | |
9354 | */ | |
cbc53e08 AD |
9355 | if (dev->hw_features & NETIF_F_TSO) |
9356 | dev->hw_features |= NETIF_F_TSO_MANGLEID; | |
7f348a60 AD |
9357 | if (dev->vlan_features & NETIF_F_TSO) |
9358 | dev->vlan_features |= NETIF_F_TSO_MANGLEID; | |
9359 | if (dev->mpls_features & NETIF_F_TSO) | |
9360 | dev->mpls_features |= NETIF_F_TSO_MANGLEID; | |
9361 | if (dev->hw_enc_features & NETIF_F_TSO) | |
9362 | dev->hw_enc_features |= NETIF_F_TSO_MANGLEID; | |
c6e1a0d1 | 9363 | |
1180e7d6 | 9364 | /* Make NETIF_F_HIGHDMA inheritable to VLAN devices. |
16c3ea78 | 9365 | */ |
1180e7d6 | 9366 | dev->vlan_features |= NETIF_F_HIGHDMA; |
16c3ea78 | 9367 | |
ee579677 PS |
9368 | /* Make NETIF_F_SG inheritable to tunnel devices. |
9369 | */ | |
802ab55a | 9370 | dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL; |
ee579677 | 9371 | |
0d89d203 SH |
9372 | /* Make NETIF_F_SG inheritable to MPLS. |
9373 | */ | |
9374 | dev->mpls_features |= NETIF_F_SG; | |
9375 | ||
7ffbe3fd JB |
9376 | ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev); |
9377 | ret = notifier_to_errno(ret); | |
9378 | if (ret) | |
9379 | goto err_uninit; | |
9380 | ||
8b41d188 | 9381 | ret = netdev_register_kobject(dev); |
cb626bf5 JH |
9382 | if (ret) { |
9383 | dev->reg_state = NETREG_UNREGISTERED; | |
7ce1b0ed | 9384 | goto err_uninit; |
cb626bf5 | 9385 | } |
b17a7c17 SH |
9386 | dev->reg_state = NETREG_REGISTERED; |
9387 | ||
6cb6a27c | 9388 | __netdev_update_features(dev); |
8e9b59b2 | 9389 | |
1da177e4 LT |
9390 | /* |
9391 | * Default initial state at registry is that the | |
9392 | * device is present. | |
9393 | */ | |
9394 | ||
9395 | set_bit(__LINK_STATE_PRESENT, &dev->state); | |
9396 | ||
8f4cccbb BH |
9397 | linkwatch_init_dev(dev); |
9398 | ||
1da177e4 | 9399 | dev_init_scheduler(dev); |
1da177e4 | 9400 | dev_hold(dev); |
ce286d32 | 9401 | list_netdevice(dev); |
7bf23575 | 9402 | add_device_randomness(dev->dev_addr, dev->addr_len); |
1da177e4 | 9403 | |
948b337e JP |
9404 | /* If the device has permanent device address, driver should |
9405 | * set dev_addr and also addr_assign_type should be set to | |
9406 | * NET_ADDR_PERM (default value). | |
9407 | */ | |
9408 | if (dev->addr_assign_type == NET_ADDR_PERM) | |
9409 | memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len); | |
9410 | ||
1da177e4 | 9411 | /* Notify protocols, that a new device appeared. */ |
056925ab | 9412 | ret = call_netdevice_notifiers(NETDEV_REGISTER, dev); |
fcc5a03a | 9413 | ret = notifier_to_errno(ret); |
93ee31f1 DL |
9414 | if (ret) { |
9415 | rollback_registered(dev); | |
10cc514f SAK |
9416 | rcu_barrier(); |
9417 | ||
93ee31f1 DL |
9418 | dev->reg_state = NETREG_UNREGISTERED; |
9419 | } | |
d90a909e EB |
9420 | /* |
9421 | * Prevent userspace races by waiting until the network | |
9422 | * device is fully setup before sending notifications. | |
9423 | */ | |
a2835763 PM |
9424 | if (!dev->rtnl_link_ops || |
9425 | dev->rtnl_link_state == RTNL_LINK_INITIALIZED) | |
7f294054 | 9426 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL); |
1da177e4 LT |
9427 | |
9428 | out: | |
9429 | return ret; | |
7ce1b0ed HX |
9430 | |
9431 | err_uninit: | |
d314774c SH |
9432 | if (dev->netdev_ops->ndo_uninit) |
9433 | dev->netdev_ops->ndo_uninit(dev); | |
cf124db5 DM |
9434 | if (dev->priv_destructor) |
9435 | dev->priv_destructor(dev); | |
42c17fa6 DC |
9436 | err_free_name: |
9437 | netdev_name_node_free(dev->name_node); | |
7ce1b0ed | 9438 | goto out; |
1da177e4 | 9439 | } |
d1b19dff | 9440 | EXPORT_SYMBOL(register_netdevice); |
1da177e4 | 9441 | |
937f1ba5 BH |
9442 | /** |
9443 | * init_dummy_netdev - init a dummy network device for NAPI | |
9444 | * @dev: device to init | |
9445 | * | |
9446 | * This takes a network device structure and initialize the minimum | |
9447 | * amount of fields so it can be used to schedule NAPI polls without | |
9448 | * registering a full blown interface. This is to be used by drivers | |
9449 | * that need to tie several hardware interfaces to a single NAPI | |
9450 | * poll scheduler due to HW limitations. | |
9451 | */ | |
9452 | int init_dummy_netdev(struct net_device *dev) | |
9453 | { | |
9454 | /* Clear everything. Note we don't initialize spinlocks | |
9455 | * are they aren't supposed to be taken by any of the | |
9456 | * NAPI code and this dummy netdev is supposed to be | |
9457 | * only ever used for NAPI polls | |
9458 | */ | |
9459 | memset(dev, 0, sizeof(struct net_device)); | |
9460 | ||
9461 | /* make sure we BUG if trying to hit standard | |
9462 | * register/unregister code path | |
9463 | */ | |
9464 | dev->reg_state = NETREG_DUMMY; | |
9465 | ||
937f1ba5 BH |
9466 | /* NAPI wants this */ |
9467 | INIT_LIST_HEAD(&dev->napi_list); | |
9468 | ||
9469 | /* a dummy interface is started by default */ | |
9470 | set_bit(__LINK_STATE_PRESENT, &dev->state); | |
9471 | set_bit(__LINK_STATE_START, &dev->state); | |
9472 | ||
35edfdc7 JE |
9473 | /* napi_busy_loop stats accounting wants this */ |
9474 | dev_net_set(dev, &init_net); | |
9475 | ||
29b4433d ED |
9476 | /* Note : We dont allocate pcpu_refcnt for dummy devices, |
9477 | * because users of this 'device' dont need to change | |
9478 | * its refcount. | |
9479 | */ | |
9480 | ||
937f1ba5 BH |
9481 | return 0; |
9482 | } | |
9483 | EXPORT_SYMBOL_GPL(init_dummy_netdev); | |
9484 | ||
9485 | ||
1da177e4 LT |
9486 | /** |
9487 | * register_netdev - register a network device | |
9488 | * @dev: device to register | |
9489 | * | |
9490 | * Take a completed network device structure and add it to the kernel | |
9491 | * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier | |
9492 | * chain. 0 is returned on success. A negative errno code is returned | |
9493 | * on a failure to set up the device, or if the name is a duplicate. | |
9494 | * | |
38b4da38 | 9495 | * This is a wrapper around register_netdevice that takes the rtnl semaphore |
1da177e4 LT |
9496 | * and expands the device name if you passed a format string to |
9497 | * alloc_netdev. | |
9498 | */ | |
9499 | int register_netdev(struct net_device *dev) | |
9500 | { | |
9501 | int err; | |
9502 | ||
b0f3debc KT |
9503 | if (rtnl_lock_killable()) |
9504 | return -EINTR; | |
1da177e4 | 9505 | err = register_netdevice(dev); |
1da177e4 LT |
9506 | rtnl_unlock(); |
9507 | return err; | |
9508 | } | |
9509 | EXPORT_SYMBOL(register_netdev); | |
9510 | ||
29b4433d ED |
9511 | int netdev_refcnt_read(const struct net_device *dev) |
9512 | { | |
9513 | int i, refcnt = 0; | |
9514 | ||
9515 | for_each_possible_cpu(i) | |
9516 | refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i); | |
9517 | return refcnt; | |
9518 | } | |
9519 | EXPORT_SYMBOL(netdev_refcnt_read); | |
9520 | ||
2c53040f | 9521 | /** |
1da177e4 | 9522 | * netdev_wait_allrefs - wait until all references are gone. |
3de7a37b | 9523 | * @dev: target net_device |
1da177e4 LT |
9524 | * |
9525 | * This is called when unregistering network devices. | |
9526 | * | |
9527 | * Any protocol or device that holds a reference should register | |
9528 | * for netdevice notification, and cleanup and put back the | |
9529 | * reference if they receive an UNREGISTER event. | |
9530 | * We can get stuck here if buggy protocols don't correctly | |
4ec93edb | 9531 | * call dev_put. |
1da177e4 LT |
9532 | */ |
9533 | static void netdev_wait_allrefs(struct net_device *dev) | |
9534 | { | |
9535 | unsigned long rebroadcast_time, warning_time; | |
29b4433d | 9536 | int refcnt; |
1da177e4 | 9537 | |
e014debe ED |
9538 | linkwatch_forget_dev(dev); |
9539 | ||
1da177e4 | 9540 | rebroadcast_time = warning_time = jiffies; |
29b4433d ED |
9541 | refcnt = netdev_refcnt_read(dev); |
9542 | ||
9543 | while (refcnt != 0) { | |
1da177e4 | 9544 | if (time_after(jiffies, rebroadcast_time + 1 * HZ)) { |
6756ae4b | 9545 | rtnl_lock(); |
1da177e4 LT |
9546 | |
9547 | /* Rebroadcast unregister notification */ | |
056925ab | 9548 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
1da177e4 | 9549 | |
748e2d93 | 9550 | __rtnl_unlock(); |
0115e8e3 | 9551 | rcu_barrier(); |
748e2d93 ED |
9552 | rtnl_lock(); |
9553 | ||
1da177e4 LT |
9554 | if (test_bit(__LINK_STATE_LINKWATCH_PENDING, |
9555 | &dev->state)) { | |
9556 | /* We must not have linkwatch events | |
9557 | * pending on unregister. If this | |
9558 | * happens, we simply run the queue | |
9559 | * unscheduled, resulting in a noop | |
9560 | * for this device. | |
9561 | */ | |
9562 | linkwatch_run_queue(); | |
9563 | } | |
9564 | ||
6756ae4b | 9565 | __rtnl_unlock(); |
1da177e4 LT |
9566 | |
9567 | rebroadcast_time = jiffies; | |
9568 | } | |
9569 | ||
9570 | msleep(250); | |
9571 | ||
29b4433d ED |
9572 | refcnt = netdev_refcnt_read(dev); |
9573 | ||
d7c04b05 | 9574 | if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) { |
7b6cd1ce JP |
9575 | pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n", |
9576 | dev->name, refcnt); | |
1da177e4 LT |
9577 | warning_time = jiffies; |
9578 | } | |
9579 | } | |
9580 | } | |
9581 | ||
9582 | /* The sequence is: | |
9583 | * | |
9584 | * rtnl_lock(); | |
9585 | * ... | |
9586 | * register_netdevice(x1); | |
9587 | * register_netdevice(x2); | |
9588 | * ... | |
9589 | * unregister_netdevice(y1); | |
9590 | * unregister_netdevice(y2); | |
9591 | * ... | |
9592 | * rtnl_unlock(); | |
9593 | * free_netdev(y1); | |
9594 | * free_netdev(y2); | |
9595 | * | |
58ec3b4d | 9596 | * We are invoked by rtnl_unlock(). |
1da177e4 | 9597 | * This allows us to deal with problems: |
b17a7c17 | 9598 | * 1) We can delete sysfs objects which invoke hotplug |
1da177e4 LT |
9599 | * without deadlocking with linkwatch via keventd. |
9600 | * 2) Since we run with the RTNL semaphore not held, we can sleep | |
9601 | * safely in order to wait for the netdev refcnt to drop to zero. | |
58ec3b4d HX |
9602 | * |
9603 | * We must not return until all unregister events added during | |
9604 | * the interval the lock was held have been completed. | |
1da177e4 | 9605 | */ |
1da177e4 LT |
9606 | void netdev_run_todo(void) |
9607 | { | |
626ab0e6 | 9608 | struct list_head list; |
1da177e4 | 9609 | |
1da177e4 | 9610 | /* Snapshot list, allow later requests */ |
626ab0e6 | 9611 | list_replace_init(&net_todo_list, &list); |
58ec3b4d HX |
9612 | |
9613 | __rtnl_unlock(); | |
626ab0e6 | 9614 | |
0115e8e3 ED |
9615 | |
9616 | /* Wait for rcu callbacks to finish before next phase */ | |
850a545b EB |
9617 | if (!list_empty(&list)) |
9618 | rcu_barrier(); | |
9619 | ||
1da177e4 LT |
9620 | while (!list_empty(&list)) { |
9621 | struct net_device *dev | |
e5e26d75 | 9622 | = list_first_entry(&list, struct net_device, todo_list); |
1da177e4 LT |
9623 | list_del(&dev->todo_list); |
9624 | ||
b17a7c17 | 9625 | if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) { |
7b6cd1ce | 9626 | pr_err("network todo '%s' but state %d\n", |
b17a7c17 SH |
9627 | dev->name, dev->reg_state); |
9628 | dump_stack(); | |
9629 | continue; | |
9630 | } | |
1da177e4 | 9631 | |
b17a7c17 | 9632 | dev->reg_state = NETREG_UNREGISTERED; |
1da177e4 | 9633 | |
b17a7c17 | 9634 | netdev_wait_allrefs(dev); |
1da177e4 | 9635 | |
b17a7c17 | 9636 | /* paranoia */ |
29b4433d | 9637 | BUG_ON(netdev_refcnt_read(dev)); |
7866a621 SN |
9638 | BUG_ON(!list_empty(&dev->ptype_all)); |
9639 | BUG_ON(!list_empty(&dev->ptype_specific)); | |
33d480ce ED |
9640 | WARN_ON(rcu_access_pointer(dev->ip_ptr)); |
9641 | WARN_ON(rcu_access_pointer(dev->ip6_ptr)); | |
330c7272 | 9642 | #if IS_ENABLED(CONFIG_DECNET) |
547b792c | 9643 | WARN_ON(dev->dn_ptr); |
330c7272 | 9644 | #endif |
cf124db5 DM |
9645 | if (dev->priv_destructor) |
9646 | dev->priv_destructor(dev); | |
9647 | if (dev->needs_free_netdev) | |
9648 | free_netdev(dev); | |
9093bbb2 | 9649 | |
50624c93 EB |
9650 | /* Report a network device has been unregistered */ |
9651 | rtnl_lock(); | |
9652 | dev_net(dev)->dev_unreg_count--; | |
9653 | __rtnl_unlock(); | |
9654 | wake_up(&netdev_unregistering_wq); | |
9655 | ||
9093bbb2 SH |
9656 | /* Free network device */ |
9657 | kobject_put(&dev->dev.kobj); | |
1da177e4 | 9658 | } |
1da177e4 LT |
9659 | } |
9660 | ||
9256645a JW |
9661 | /* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has |
9662 | * all the same fields in the same order as net_device_stats, with only | |
9663 | * the type differing, but rtnl_link_stats64 may have additional fields | |
9664 | * at the end for newer counters. | |
3cfde79c | 9665 | */ |
77a1abf5 ED |
9666 | void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64, |
9667 | const struct net_device_stats *netdev_stats) | |
3cfde79c BH |
9668 | { |
9669 | #if BITS_PER_LONG == 64 | |
9256645a | 9670 | BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats)); |
9af9959e | 9671 | memcpy(stats64, netdev_stats, sizeof(*netdev_stats)); |
9256645a JW |
9672 | /* zero out counters that only exist in rtnl_link_stats64 */ |
9673 | memset((char *)stats64 + sizeof(*netdev_stats), 0, | |
9674 | sizeof(*stats64) - sizeof(*netdev_stats)); | |
3cfde79c | 9675 | #else |
9256645a | 9676 | size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long); |
3cfde79c BH |
9677 | const unsigned long *src = (const unsigned long *)netdev_stats; |
9678 | u64 *dst = (u64 *)stats64; | |
9679 | ||
9256645a | 9680 | BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64)); |
3cfde79c BH |
9681 | for (i = 0; i < n; i++) |
9682 | dst[i] = src[i]; | |
9256645a JW |
9683 | /* zero out counters that only exist in rtnl_link_stats64 */ |
9684 | memset((char *)stats64 + n * sizeof(u64), 0, | |
9685 | sizeof(*stats64) - n * sizeof(u64)); | |
3cfde79c BH |
9686 | #endif |
9687 | } | |
77a1abf5 | 9688 | EXPORT_SYMBOL(netdev_stats_to_stats64); |
3cfde79c | 9689 | |
eeda3fd6 SH |
9690 | /** |
9691 | * dev_get_stats - get network device statistics | |
9692 | * @dev: device to get statistics from | |
28172739 | 9693 | * @storage: place to store stats |
eeda3fd6 | 9694 | * |
d7753516 BH |
9695 | * Get network statistics from device. Return @storage. |
9696 | * The device driver may provide its own method by setting | |
9697 | * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats; | |
9698 | * otherwise the internal statistics structure is used. | |
eeda3fd6 | 9699 | */ |
d7753516 BH |
9700 | struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev, |
9701 | struct rtnl_link_stats64 *storage) | |
7004bf25 | 9702 | { |
eeda3fd6 SH |
9703 | const struct net_device_ops *ops = dev->netdev_ops; |
9704 | ||
28172739 ED |
9705 | if (ops->ndo_get_stats64) { |
9706 | memset(storage, 0, sizeof(*storage)); | |
caf586e5 ED |
9707 | ops->ndo_get_stats64(dev, storage); |
9708 | } else if (ops->ndo_get_stats) { | |
3cfde79c | 9709 | netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev)); |
caf586e5 ED |
9710 | } else { |
9711 | netdev_stats_to_stats64(storage, &dev->stats); | |
28172739 | 9712 | } |
6f64ec74 ED |
9713 | storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped); |
9714 | storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped); | |
9715 | storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler); | |
28172739 | 9716 | return storage; |
c45d286e | 9717 | } |
eeda3fd6 | 9718 | EXPORT_SYMBOL(dev_get_stats); |
c45d286e | 9719 | |
24824a09 | 9720 | struct netdev_queue *dev_ingress_queue_create(struct net_device *dev) |
dc2b4847 | 9721 | { |
24824a09 | 9722 | struct netdev_queue *queue = dev_ingress_queue(dev); |
dc2b4847 | 9723 | |
24824a09 ED |
9724 | #ifdef CONFIG_NET_CLS_ACT |
9725 | if (queue) | |
9726 | return queue; | |
9727 | queue = kzalloc(sizeof(*queue), GFP_KERNEL); | |
9728 | if (!queue) | |
9729 | return NULL; | |
9730 | netdev_init_one_queue(dev, queue, NULL); | |
2ce1ee17 | 9731 | RCU_INIT_POINTER(queue->qdisc, &noop_qdisc); |
24824a09 ED |
9732 | queue->qdisc_sleeping = &noop_qdisc; |
9733 | rcu_assign_pointer(dev->ingress_queue, queue); | |
9734 | #endif | |
9735 | return queue; | |
bb949fbd DM |
9736 | } |
9737 | ||
2c60db03 ED |
9738 | static const struct ethtool_ops default_ethtool_ops; |
9739 | ||
d07d7507 SG |
9740 | void netdev_set_default_ethtool_ops(struct net_device *dev, |
9741 | const struct ethtool_ops *ops) | |
9742 | { | |
9743 | if (dev->ethtool_ops == &default_ethtool_ops) | |
9744 | dev->ethtool_ops = ops; | |
9745 | } | |
9746 | EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops); | |
9747 | ||
74d332c1 ED |
9748 | void netdev_freemem(struct net_device *dev) |
9749 | { | |
9750 | char *addr = (char *)dev - dev->padded; | |
9751 | ||
4cb28970 | 9752 | kvfree(addr); |
74d332c1 ED |
9753 | } |
9754 | ||
1da177e4 | 9755 | /** |
722c9a0c | 9756 | * alloc_netdev_mqs - allocate network device |
9757 | * @sizeof_priv: size of private data to allocate space for | |
9758 | * @name: device name format string | |
9759 | * @name_assign_type: origin of device name | |
9760 | * @setup: callback to initialize device | |
9761 | * @txqs: the number of TX subqueues to allocate | |
9762 | * @rxqs: the number of RX subqueues to allocate | |
9763 | * | |
9764 | * Allocates a struct net_device with private data area for driver use | |
9765 | * and performs basic initialization. Also allocates subqueue structs | |
9766 | * for each queue on the device. | |
1da177e4 | 9767 | */ |
36909ea4 | 9768 | struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name, |
c835a677 | 9769 | unsigned char name_assign_type, |
36909ea4 TH |
9770 | void (*setup)(struct net_device *), |
9771 | unsigned int txqs, unsigned int rxqs) | |
1da177e4 | 9772 | { |
1da177e4 | 9773 | struct net_device *dev; |
52a59bd5 | 9774 | unsigned int alloc_size; |
1ce8e7b5 | 9775 | struct net_device *p; |
1da177e4 | 9776 | |
b6fe17d6 SH |
9777 | BUG_ON(strlen(name) >= sizeof(dev->name)); |
9778 | ||
36909ea4 | 9779 | if (txqs < 1) { |
7b6cd1ce | 9780 | pr_err("alloc_netdev: Unable to allocate device with zero queues\n"); |
55513fb4 TH |
9781 | return NULL; |
9782 | } | |
9783 | ||
36909ea4 | 9784 | if (rxqs < 1) { |
7b6cd1ce | 9785 | pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n"); |
36909ea4 TH |
9786 | return NULL; |
9787 | } | |
36909ea4 | 9788 | |
fd2ea0a7 | 9789 | alloc_size = sizeof(struct net_device); |
d1643d24 AD |
9790 | if (sizeof_priv) { |
9791 | /* ensure 32-byte alignment of private area */ | |
1ce8e7b5 | 9792 | alloc_size = ALIGN(alloc_size, NETDEV_ALIGN); |
d1643d24 AD |
9793 | alloc_size += sizeof_priv; |
9794 | } | |
9795 | /* ensure 32-byte alignment of whole construct */ | |
1ce8e7b5 | 9796 | alloc_size += NETDEV_ALIGN - 1; |
1da177e4 | 9797 | |
dcda9b04 | 9798 | p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL); |
62b5942a | 9799 | if (!p) |
1da177e4 | 9800 | return NULL; |
1da177e4 | 9801 | |
1ce8e7b5 | 9802 | dev = PTR_ALIGN(p, NETDEV_ALIGN); |
1da177e4 | 9803 | dev->padded = (char *)dev - (char *)p; |
ab9c73cc | 9804 | |
29b4433d ED |
9805 | dev->pcpu_refcnt = alloc_percpu(int); |
9806 | if (!dev->pcpu_refcnt) | |
74d332c1 | 9807 | goto free_dev; |
ab9c73cc | 9808 | |
ab9c73cc | 9809 | if (dev_addr_init(dev)) |
29b4433d | 9810 | goto free_pcpu; |
ab9c73cc | 9811 | |
22bedad3 | 9812 | dev_mc_init(dev); |
a748ee24 | 9813 | dev_uc_init(dev); |
ccffad25 | 9814 | |
c346dca1 | 9815 | dev_net_set(dev, &init_net); |
1da177e4 | 9816 | |
ab92d68f TY |
9817 | netdev_register_lockdep_key(dev); |
9818 | ||
8d3bdbd5 | 9819 | dev->gso_max_size = GSO_MAX_SIZE; |
30b678d8 | 9820 | dev->gso_max_segs = GSO_MAX_SEGS; |
5343da4c TY |
9821 | dev->upper_level = 1; |
9822 | dev->lower_level = 1; | |
8d3bdbd5 | 9823 | |
8d3bdbd5 DM |
9824 | INIT_LIST_HEAD(&dev->napi_list); |
9825 | INIT_LIST_HEAD(&dev->unreg_list); | |
5cde2829 | 9826 | INIT_LIST_HEAD(&dev->close_list); |
8d3bdbd5 | 9827 | INIT_LIST_HEAD(&dev->link_watch_list); |
2f268f12 VF |
9828 | INIT_LIST_HEAD(&dev->adj_list.upper); |
9829 | INIT_LIST_HEAD(&dev->adj_list.lower); | |
7866a621 SN |
9830 | INIT_LIST_HEAD(&dev->ptype_all); |
9831 | INIT_LIST_HEAD(&dev->ptype_specific); | |
93642e14 | 9832 | INIT_LIST_HEAD(&dev->net_notifier_list); |
59cc1f61 JK |
9833 | #ifdef CONFIG_NET_SCHED |
9834 | hash_init(dev->qdisc_hash); | |
9835 | #endif | |
02875878 | 9836 | dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM; |
8d3bdbd5 DM |
9837 | setup(dev); |
9838 | ||
a813104d | 9839 | if (!dev->tx_queue_len) { |
f84bb1ea | 9840 | dev->priv_flags |= IFF_NO_QUEUE; |
11597084 | 9841 | dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN; |
a813104d | 9842 | } |
906470c1 | 9843 | |
36909ea4 TH |
9844 | dev->num_tx_queues = txqs; |
9845 | dev->real_num_tx_queues = txqs; | |
ed9af2e8 | 9846 | if (netif_alloc_netdev_queues(dev)) |
8d3bdbd5 | 9847 | goto free_all; |
e8a0464c | 9848 | |
36909ea4 TH |
9849 | dev->num_rx_queues = rxqs; |
9850 | dev->real_num_rx_queues = rxqs; | |
fe822240 | 9851 | if (netif_alloc_rx_queues(dev)) |
8d3bdbd5 | 9852 | goto free_all; |
0a9627f2 | 9853 | |
1da177e4 | 9854 | strcpy(dev->name, name); |
c835a677 | 9855 | dev->name_assign_type = name_assign_type; |
cbda10fa | 9856 | dev->group = INIT_NETDEV_GROUP; |
2c60db03 ED |
9857 | if (!dev->ethtool_ops) |
9858 | dev->ethtool_ops = &default_ethtool_ops; | |
e687ad60 PN |
9859 | |
9860 | nf_hook_ingress_init(dev); | |
9861 | ||
1da177e4 | 9862 | return dev; |
ab9c73cc | 9863 | |
8d3bdbd5 DM |
9864 | free_all: |
9865 | free_netdev(dev); | |
9866 | return NULL; | |
9867 | ||
29b4433d ED |
9868 | free_pcpu: |
9869 | free_percpu(dev->pcpu_refcnt); | |
74d332c1 ED |
9870 | free_dev: |
9871 | netdev_freemem(dev); | |
ab9c73cc | 9872 | return NULL; |
1da177e4 | 9873 | } |
36909ea4 | 9874 | EXPORT_SYMBOL(alloc_netdev_mqs); |
1da177e4 LT |
9875 | |
9876 | /** | |
722c9a0c | 9877 | * free_netdev - free network device |
9878 | * @dev: device | |
1da177e4 | 9879 | * |
722c9a0c | 9880 | * This function does the last stage of destroying an allocated device |
9881 | * interface. The reference to the device object is released. If this | |
9882 | * is the last reference then it will be freed.Must be called in process | |
9883 | * context. | |
1da177e4 LT |
9884 | */ |
9885 | void free_netdev(struct net_device *dev) | |
9886 | { | |
d565b0a1 HX |
9887 | struct napi_struct *p, *n; |
9888 | ||
93d05d4a | 9889 | might_sleep(); |
60877a32 | 9890 | netif_free_tx_queues(dev); |
e817f856 | 9891 | netif_free_rx_queues(dev); |
e8a0464c | 9892 | |
33d480ce | 9893 | kfree(rcu_dereference_protected(dev->ingress_queue, 1)); |
24824a09 | 9894 | |
f001fde5 JP |
9895 | /* Flush device addresses */ |
9896 | dev_addr_flush(dev); | |
9897 | ||
d565b0a1 HX |
9898 | list_for_each_entry_safe(p, n, &dev->napi_list, dev_list) |
9899 | netif_napi_del(p); | |
9900 | ||
29b4433d ED |
9901 | free_percpu(dev->pcpu_refcnt); |
9902 | dev->pcpu_refcnt = NULL; | |
75ccae62 THJ |
9903 | free_percpu(dev->xdp_bulkq); |
9904 | dev->xdp_bulkq = NULL; | |
29b4433d | 9905 | |
ab92d68f TY |
9906 | netdev_unregister_lockdep_key(dev); |
9907 | ||
3041a069 | 9908 | /* Compatibility with error handling in drivers */ |
1da177e4 | 9909 | if (dev->reg_state == NETREG_UNINITIALIZED) { |
74d332c1 | 9910 | netdev_freemem(dev); |
1da177e4 LT |
9911 | return; |
9912 | } | |
9913 | ||
9914 | BUG_ON(dev->reg_state != NETREG_UNREGISTERED); | |
9915 | dev->reg_state = NETREG_RELEASED; | |
9916 | ||
43cb76d9 GKH |
9917 | /* will free via device release */ |
9918 | put_device(&dev->dev); | |
1da177e4 | 9919 | } |
d1b19dff | 9920 | EXPORT_SYMBOL(free_netdev); |
4ec93edb | 9921 | |
f0db275a SH |
9922 | /** |
9923 | * synchronize_net - Synchronize with packet receive processing | |
9924 | * | |
9925 | * Wait for packets currently being received to be done. | |
9926 | * Does not block later packets from starting. | |
9927 | */ | |
4ec93edb | 9928 | void synchronize_net(void) |
1da177e4 LT |
9929 | { |
9930 | might_sleep(); | |
be3fc413 ED |
9931 | if (rtnl_is_locked()) |
9932 | synchronize_rcu_expedited(); | |
9933 | else | |
9934 | synchronize_rcu(); | |
1da177e4 | 9935 | } |
d1b19dff | 9936 | EXPORT_SYMBOL(synchronize_net); |
1da177e4 LT |
9937 | |
9938 | /** | |
44a0873d | 9939 | * unregister_netdevice_queue - remove device from the kernel |
1da177e4 | 9940 | * @dev: device |
44a0873d | 9941 | * @head: list |
6ebfbc06 | 9942 | * |
1da177e4 | 9943 | * This function shuts down a device interface and removes it |
d59b54b1 | 9944 | * from the kernel tables. |
44a0873d | 9945 | * If head not NULL, device is queued to be unregistered later. |
1da177e4 LT |
9946 | * |
9947 | * Callers must hold the rtnl semaphore. You may want | |
9948 | * unregister_netdev() instead of this. | |
9949 | */ | |
9950 | ||
44a0873d | 9951 | void unregister_netdevice_queue(struct net_device *dev, struct list_head *head) |
1da177e4 | 9952 | { |
a6620712 HX |
9953 | ASSERT_RTNL(); |
9954 | ||
44a0873d | 9955 | if (head) { |
9fdce099 | 9956 | list_move_tail(&dev->unreg_list, head); |
44a0873d ED |
9957 | } else { |
9958 | rollback_registered(dev); | |
9959 | /* Finish processing unregister after unlock */ | |
9960 | net_set_todo(dev); | |
9961 | } | |
1da177e4 | 9962 | } |
44a0873d | 9963 | EXPORT_SYMBOL(unregister_netdevice_queue); |
1da177e4 | 9964 | |
9b5e383c ED |
9965 | /** |
9966 | * unregister_netdevice_many - unregister many devices | |
9967 | * @head: list of devices | |
87757a91 ED |
9968 | * |
9969 | * Note: As most callers use a stack allocated list_head, | |
9970 | * we force a list_del() to make sure stack wont be corrupted later. | |
9b5e383c ED |
9971 | */ |
9972 | void unregister_netdevice_many(struct list_head *head) | |
9973 | { | |
9974 | struct net_device *dev; | |
9975 | ||
9976 | if (!list_empty(head)) { | |
9977 | rollback_registered_many(head); | |
9978 | list_for_each_entry(dev, head, unreg_list) | |
9979 | net_set_todo(dev); | |
87757a91 | 9980 | list_del(head); |
9b5e383c ED |
9981 | } |
9982 | } | |
63c8099d | 9983 | EXPORT_SYMBOL(unregister_netdevice_many); |
9b5e383c | 9984 | |
1da177e4 LT |
9985 | /** |
9986 | * unregister_netdev - remove device from the kernel | |
9987 | * @dev: device | |
9988 | * | |
9989 | * This function shuts down a device interface and removes it | |
d59b54b1 | 9990 | * from the kernel tables. |
1da177e4 LT |
9991 | * |
9992 | * This is just a wrapper for unregister_netdevice that takes | |
9993 | * the rtnl semaphore. In general you want to use this and not | |
9994 | * unregister_netdevice. | |
9995 | */ | |
9996 | void unregister_netdev(struct net_device *dev) | |
9997 | { | |
9998 | rtnl_lock(); | |
9999 | unregister_netdevice(dev); | |
10000 | rtnl_unlock(); | |
10001 | } | |
1da177e4 LT |
10002 | EXPORT_SYMBOL(unregister_netdev); |
10003 | ||
ce286d32 EB |
10004 | /** |
10005 | * dev_change_net_namespace - move device to different nethost namespace | |
10006 | * @dev: device | |
10007 | * @net: network namespace | |
10008 | * @pat: If not NULL name pattern to try if the current device name | |
10009 | * is already taken in the destination network namespace. | |
10010 | * | |
10011 | * This function shuts down a device interface and moves it | |
10012 | * to a new network namespace. On success 0 is returned, on | |
10013 | * a failure a netagive errno code is returned. | |
10014 | * | |
10015 | * Callers must hold the rtnl semaphore. | |
10016 | */ | |
10017 | ||
10018 | int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat) | |
10019 | { | |
38e01b30 | 10020 | int err, new_nsid, new_ifindex; |
ce286d32 EB |
10021 | |
10022 | ASSERT_RTNL(); | |
10023 | ||
10024 | /* Don't allow namespace local devices to be moved. */ | |
10025 | err = -EINVAL; | |
10026 | if (dev->features & NETIF_F_NETNS_LOCAL) | |
10027 | goto out; | |
10028 | ||
10029 | /* Ensure the device has been registrered */ | |
ce286d32 EB |
10030 | if (dev->reg_state != NETREG_REGISTERED) |
10031 | goto out; | |
10032 | ||
10033 | /* Get out if there is nothing todo */ | |
10034 | err = 0; | |
878628fb | 10035 | if (net_eq(dev_net(dev), net)) |
ce286d32 EB |
10036 | goto out; |
10037 | ||
10038 | /* Pick the destination device name, and ensure | |
10039 | * we can use it in the destination network namespace. | |
10040 | */ | |
10041 | err = -EEXIST; | |
d9031024 | 10042 | if (__dev_get_by_name(net, dev->name)) { |
ce286d32 EB |
10043 | /* We get here if we can't use the current device name */ |
10044 | if (!pat) | |
10045 | goto out; | |
7892bd08 LR |
10046 | err = dev_get_valid_name(net, dev, pat); |
10047 | if (err < 0) | |
ce286d32 EB |
10048 | goto out; |
10049 | } | |
10050 | ||
10051 | /* | |
10052 | * And now a mini version of register_netdevice unregister_netdevice. | |
10053 | */ | |
10054 | ||
10055 | /* If device is running close it first. */ | |
9b772652 | 10056 | dev_close(dev); |
ce286d32 EB |
10057 | |
10058 | /* And unlink it from device chain */ | |
ce286d32 EB |
10059 | unlist_netdevice(dev); |
10060 | ||
10061 | synchronize_net(); | |
10062 | ||
10063 | /* Shutdown queueing discipline. */ | |
10064 | dev_shutdown(dev); | |
10065 | ||
10066 | /* Notify protocols, that we are about to destroy | |
eb13da1a | 10067 | * this device. They should clean all the things. |
10068 | * | |
10069 | * Note that dev->reg_state stays at NETREG_REGISTERED. | |
10070 | * This is wanted because this way 8021q and macvlan know | |
10071 | * the device is just moving and can keep their slaves up. | |
10072 | */ | |
ce286d32 | 10073 | call_netdevice_notifiers(NETDEV_UNREGISTER, dev); |
6549dd43 | 10074 | rcu_barrier(); |
38e01b30 | 10075 | |
d4e4fdf9 | 10076 | new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL); |
38e01b30 ND |
10077 | /* If there is an ifindex conflict assign a new one */ |
10078 | if (__dev_get_by_index(net, dev->ifindex)) | |
10079 | new_ifindex = dev_new_index(net); | |
10080 | else | |
10081 | new_ifindex = dev->ifindex; | |
10082 | ||
10083 | rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid, | |
10084 | new_ifindex); | |
ce286d32 EB |
10085 | |
10086 | /* | |
10087 | * Flush the unicast and multicast chains | |
10088 | */ | |
a748ee24 | 10089 | dev_uc_flush(dev); |
22bedad3 | 10090 | dev_mc_flush(dev); |
ce286d32 | 10091 | |
4e66ae2e SH |
10092 | /* Send a netdev-removed uevent to the old namespace */ |
10093 | kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE); | |
4c75431a | 10094 | netdev_adjacent_del_links(dev); |
4e66ae2e | 10095 | |
93642e14 JP |
10096 | /* Move per-net netdevice notifiers that are following the netdevice */ |
10097 | move_netdevice_notifiers_dev_net(dev, net); | |
10098 | ||
ce286d32 | 10099 | /* Actually switch the network namespace */ |
c346dca1 | 10100 | dev_net_set(dev, net); |
38e01b30 | 10101 | dev->ifindex = new_ifindex; |
ce286d32 | 10102 | |
4e66ae2e SH |
10103 | /* Send a netdev-add uevent to the new namespace */ |
10104 | kobject_uevent(&dev->dev.kobj, KOBJ_ADD); | |
4c75431a | 10105 | netdev_adjacent_add_links(dev); |
4e66ae2e | 10106 | |
8b41d188 | 10107 | /* Fixup kobjects */ |
a1b3f594 | 10108 | err = device_rename(&dev->dev, dev->name); |
8b41d188 | 10109 | WARN_ON(err); |
ce286d32 EB |
10110 | |
10111 | /* Add the device back in the hashes */ | |
10112 | list_netdevice(dev); | |
10113 | ||
10114 | /* Notify protocols, that a new device appeared. */ | |
10115 | call_netdevice_notifiers(NETDEV_REGISTER, dev); | |
10116 | ||
d90a909e EB |
10117 | /* |
10118 | * Prevent userspace races by waiting until the network | |
10119 | * device is fully setup before sending notifications. | |
10120 | */ | |
7f294054 | 10121 | rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL); |
d90a909e | 10122 | |
ce286d32 EB |
10123 | synchronize_net(); |
10124 | err = 0; | |
10125 | out: | |
10126 | return err; | |
10127 | } | |
463d0183 | 10128 | EXPORT_SYMBOL_GPL(dev_change_net_namespace); |
ce286d32 | 10129 | |
f0bf90de | 10130 | static int dev_cpu_dead(unsigned int oldcpu) |
1da177e4 LT |
10131 | { |
10132 | struct sk_buff **list_skb; | |
1da177e4 | 10133 | struct sk_buff *skb; |
f0bf90de | 10134 | unsigned int cpu; |
97d8b6e3 | 10135 | struct softnet_data *sd, *oldsd, *remsd = NULL; |
1da177e4 | 10136 | |
1da177e4 LT |
10137 | local_irq_disable(); |
10138 | cpu = smp_processor_id(); | |
10139 | sd = &per_cpu(softnet_data, cpu); | |
10140 | oldsd = &per_cpu(softnet_data, oldcpu); | |
10141 | ||
10142 | /* Find end of our completion_queue. */ | |
10143 | list_skb = &sd->completion_queue; | |
10144 | while (*list_skb) | |
10145 | list_skb = &(*list_skb)->next; | |
10146 | /* Append completion queue from offline CPU. */ | |
10147 | *list_skb = oldsd->completion_queue; | |
10148 | oldsd->completion_queue = NULL; | |
10149 | ||
1da177e4 | 10150 | /* Append output queue from offline CPU. */ |
a9cbd588 CG |
10151 | if (oldsd->output_queue) { |
10152 | *sd->output_queue_tailp = oldsd->output_queue; | |
10153 | sd->output_queue_tailp = oldsd->output_queue_tailp; | |
10154 | oldsd->output_queue = NULL; | |
10155 | oldsd->output_queue_tailp = &oldsd->output_queue; | |
10156 | } | |
ac64da0b ED |
10157 | /* Append NAPI poll list from offline CPU, with one exception : |
10158 | * process_backlog() must be called by cpu owning percpu backlog. | |
10159 | * We properly handle process_queue & input_pkt_queue later. | |
10160 | */ | |
10161 | while (!list_empty(&oldsd->poll_list)) { | |
10162 | struct napi_struct *napi = list_first_entry(&oldsd->poll_list, | |
10163 | struct napi_struct, | |
10164 | poll_list); | |
10165 | ||
10166 | list_del_init(&napi->poll_list); | |
10167 | if (napi->poll == process_backlog) | |
10168 | napi->state = 0; | |
10169 | else | |
10170 | ____napi_schedule(sd, napi); | |
264524d5 | 10171 | } |
1da177e4 LT |
10172 | |
10173 | raise_softirq_irqoff(NET_TX_SOFTIRQ); | |
10174 | local_irq_enable(); | |
10175 | ||
773fc8f6 | 10176 | #ifdef CONFIG_RPS |
10177 | remsd = oldsd->rps_ipi_list; | |
10178 | oldsd->rps_ipi_list = NULL; | |
10179 | #endif | |
10180 | /* send out pending IPI's on offline CPU */ | |
10181 | net_rps_send_ipi(remsd); | |
10182 | ||
1da177e4 | 10183 | /* Process offline CPU's input_pkt_queue */ |
76cc8b13 | 10184 | while ((skb = __skb_dequeue(&oldsd->process_queue))) { |
91e83133 | 10185 | netif_rx_ni(skb); |
76cc8b13 | 10186 | input_queue_head_incr(oldsd); |
fec5e652 | 10187 | } |
ac64da0b | 10188 | while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) { |
91e83133 | 10189 | netif_rx_ni(skb); |
76cc8b13 TH |
10190 | input_queue_head_incr(oldsd); |
10191 | } | |
1da177e4 | 10192 | |
f0bf90de | 10193 | return 0; |
1da177e4 | 10194 | } |
1da177e4 | 10195 | |
7f353bf2 | 10196 | /** |
b63365a2 HX |
10197 | * netdev_increment_features - increment feature set by one |
10198 | * @all: current feature set | |
10199 | * @one: new feature set | |
10200 | * @mask: mask feature set | |
7f353bf2 HX |
10201 | * |
10202 | * Computes a new feature set after adding a device with feature set | |
b63365a2 HX |
10203 | * @one to the master device with current feature set @all. Will not |
10204 | * enable anything that is off in @mask. Returns the new feature set. | |
7f353bf2 | 10205 | */ |
c8f44aff MM |
10206 | netdev_features_t netdev_increment_features(netdev_features_t all, |
10207 | netdev_features_t one, netdev_features_t mask) | |
b63365a2 | 10208 | { |
c8cd0989 | 10209 | if (mask & NETIF_F_HW_CSUM) |
a188222b | 10210 | mask |= NETIF_F_CSUM_MASK; |
1742f183 | 10211 | mask |= NETIF_F_VLAN_CHALLENGED; |
7f353bf2 | 10212 | |
a188222b | 10213 | all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask; |
1742f183 | 10214 | all &= one | ~NETIF_F_ALL_FOR_ALL; |
c6e1a0d1 | 10215 | |
1742f183 | 10216 | /* If one device supports hw checksumming, set for all. */ |
c8cd0989 TH |
10217 | if (all & NETIF_F_HW_CSUM) |
10218 | all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM); | |
7f353bf2 HX |
10219 | |
10220 | return all; | |
10221 | } | |
b63365a2 | 10222 | EXPORT_SYMBOL(netdev_increment_features); |
7f353bf2 | 10223 | |
430f03cd | 10224 | static struct hlist_head * __net_init netdev_create_hash(void) |
30d97d35 PE |
10225 | { |
10226 | int i; | |
10227 | struct hlist_head *hash; | |
10228 | ||
6da2ec56 | 10229 | hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL); |
30d97d35 PE |
10230 | if (hash != NULL) |
10231 | for (i = 0; i < NETDEV_HASHENTRIES; i++) | |
10232 | INIT_HLIST_HEAD(&hash[i]); | |
10233 | ||
10234 | return hash; | |
10235 | } | |
10236 | ||
881d966b | 10237 | /* Initialize per network namespace state */ |
4665079c | 10238 | static int __net_init netdev_init(struct net *net) |
881d966b | 10239 | { |
d9f37d01 | 10240 | BUILD_BUG_ON(GRO_HASH_BUCKETS > |
c593642c | 10241 | 8 * sizeof_field(struct napi_struct, gro_bitmask)); |
d9f37d01 | 10242 | |
734b6541 RM |
10243 | if (net != &init_net) |
10244 | INIT_LIST_HEAD(&net->dev_base_head); | |
881d966b | 10245 | |
30d97d35 PE |
10246 | net->dev_name_head = netdev_create_hash(); |
10247 | if (net->dev_name_head == NULL) | |
10248 | goto err_name; | |
881d966b | 10249 | |
30d97d35 PE |
10250 | net->dev_index_head = netdev_create_hash(); |
10251 | if (net->dev_index_head == NULL) | |
10252 | goto err_idx; | |
881d966b | 10253 | |
a30c7b42 JP |
10254 | RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain); |
10255 | ||
881d966b | 10256 | return 0; |
30d97d35 PE |
10257 | |
10258 | err_idx: | |
10259 | kfree(net->dev_name_head); | |
10260 | err_name: | |
10261 | return -ENOMEM; | |
881d966b EB |
10262 | } |
10263 | ||
f0db275a SH |
10264 | /** |
10265 | * netdev_drivername - network driver for the device | |
10266 | * @dev: network device | |
f0db275a SH |
10267 | * |
10268 | * Determine network driver for device. | |
10269 | */ | |
3019de12 | 10270 | const char *netdev_drivername(const struct net_device *dev) |
6579e57b | 10271 | { |
cf04a4c7 SH |
10272 | const struct device_driver *driver; |
10273 | const struct device *parent; | |
3019de12 | 10274 | const char *empty = ""; |
6579e57b AV |
10275 | |
10276 | parent = dev->dev.parent; | |
6579e57b | 10277 | if (!parent) |
3019de12 | 10278 | return empty; |
6579e57b AV |
10279 | |
10280 | driver = parent->driver; | |
10281 | if (driver && driver->name) | |
3019de12 DM |
10282 | return driver->name; |
10283 | return empty; | |
6579e57b AV |
10284 | } |
10285 | ||
6ea754eb JP |
10286 | static void __netdev_printk(const char *level, const struct net_device *dev, |
10287 | struct va_format *vaf) | |
256df2f3 | 10288 | { |
b004ff49 | 10289 | if (dev && dev->dev.parent) { |
6ea754eb JP |
10290 | dev_printk_emit(level[1] - '0', |
10291 | dev->dev.parent, | |
10292 | "%s %s %s%s: %pV", | |
10293 | dev_driver_string(dev->dev.parent), | |
10294 | dev_name(dev->dev.parent), | |
10295 | netdev_name(dev), netdev_reg_state(dev), | |
10296 | vaf); | |
b004ff49 | 10297 | } else if (dev) { |
6ea754eb JP |
10298 | printk("%s%s%s: %pV", |
10299 | level, netdev_name(dev), netdev_reg_state(dev), vaf); | |
b004ff49 | 10300 | } else { |
6ea754eb | 10301 | printk("%s(NULL net_device): %pV", level, vaf); |
b004ff49 | 10302 | } |
256df2f3 JP |
10303 | } |
10304 | ||
6ea754eb JP |
10305 | void netdev_printk(const char *level, const struct net_device *dev, |
10306 | const char *format, ...) | |
256df2f3 JP |
10307 | { |
10308 | struct va_format vaf; | |
10309 | va_list args; | |
256df2f3 JP |
10310 | |
10311 | va_start(args, format); | |
10312 | ||
10313 | vaf.fmt = format; | |
10314 | vaf.va = &args; | |
10315 | ||
6ea754eb | 10316 | __netdev_printk(level, dev, &vaf); |
b004ff49 | 10317 | |
256df2f3 | 10318 | va_end(args); |
256df2f3 JP |
10319 | } |
10320 | EXPORT_SYMBOL(netdev_printk); | |
10321 | ||
10322 | #define define_netdev_printk_level(func, level) \ | |
6ea754eb | 10323 | void func(const struct net_device *dev, const char *fmt, ...) \ |
256df2f3 | 10324 | { \ |
256df2f3 JP |
10325 | struct va_format vaf; \ |
10326 | va_list args; \ | |
10327 | \ | |
10328 | va_start(args, fmt); \ | |
10329 | \ | |
10330 | vaf.fmt = fmt; \ | |
10331 | vaf.va = &args; \ | |
10332 | \ | |
6ea754eb | 10333 | __netdev_printk(level, dev, &vaf); \ |
b004ff49 | 10334 | \ |
256df2f3 | 10335 | va_end(args); \ |
256df2f3 JP |
10336 | } \ |
10337 | EXPORT_SYMBOL(func); | |
10338 | ||
10339 | define_netdev_printk_level(netdev_emerg, KERN_EMERG); | |
10340 | define_netdev_printk_level(netdev_alert, KERN_ALERT); | |
10341 | define_netdev_printk_level(netdev_crit, KERN_CRIT); | |
10342 | define_netdev_printk_level(netdev_err, KERN_ERR); | |
10343 | define_netdev_printk_level(netdev_warn, KERN_WARNING); | |
10344 | define_netdev_printk_level(netdev_notice, KERN_NOTICE); | |
10345 | define_netdev_printk_level(netdev_info, KERN_INFO); | |
10346 | ||
4665079c | 10347 | static void __net_exit netdev_exit(struct net *net) |
881d966b EB |
10348 | { |
10349 | kfree(net->dev_name_head); | |
10350 | kfree(net->dev_index_head); | |
ee21b18b VA |
10351 | if (net != &init_net) |
10352 | WARN_ON_ONCE(!list_empty(&net->dev_base_head)); | |
881d966b EB |
10353 | } |
10354 | ||
022cbae6 | 10355 | static struct pernet_operations __net_initdata netdev_net_ops = { |
881d966b EB |
10356 | .init = netdev_init, |
10357 | .exit = netdev_exit, | |
10358 | }; | |
10359 | ||
4665079c | 10360 | static void __net_exit default_device_exit(struct net *net) |
ce286d32 | 10361 | { |
e008b5fc | 10362 | struct net_device *dev, *aux; |
ce286d32 | 10363 | /* |
e008b5fc | 10364 | * Push all migratable network devices back to the |
ce286d32 EB |
10365 | * initial network namespace |
10366 | */ | |
10367 | rtnl_lock(); | |
e008b5fc | 10368 | for_each_netdev_safe(net, dev, aux) { |
ce286d32 | 10369 | int err; |
aca51397 | 10370 | char fb_name[IFNAMSIZ]; |
ce286d32 EB |
10371 | |
10372 | /* Ignore unmoveable devices (i.e. loopback) */ | |
10373 | if (dev->features & NETIF_F_NETNS_LOCAL) | |
10374 | continue; | |
10375 | ||
e008b5fc EB |
10376 | /* Leave virtual devices for the generic cleanup */ |
10377 | if (dev->rtnl_link_ops) | |
10378 | continue; | |
d0c082ce | 10379 | |
25985edc | 10380 | /* Push remaining network devices to init_net */ |
aca51397 | 10381 | snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex); |
55b40dbf JP |
10382 | if (__dev_get_by_name(&init_net, fb_name)) |
10383 | snprintf(fb_name, IFNAMSIZ, "dev%%d"); | |
aca51397 | 10384 | err = dev_change_net_namespace(dev, &init_net, fb_name); |
ce286d32 | 10385 | if (err) { |
7b6cd1ce JP |
10386 | pr_emerg("%s: failed to move %s to init_net: %d\n", |
10387 | __func__, dev->name, err); | |
aca51397 | 10388 | BUG(); |
ce286d32 EB |
10389 | } |
10390 | } | |
10391 | rtnl_unlock(); | |
10392 | } | |
10393 | ||
50624c93 EB |
10394 | static void __net_exit rtnl_lock_unregistering(struct list_head *net_list) |
10395 | { | |
10396 | /* Return with the rtnl_lock held when there are no network | |
10397 | * devices unregistering in any network namespace in net_list. | |
10398 | */ | |
10399 | struct net *net; | |
10400 | bool unregistering; | |
ff960a73 | 10401 | DEFINE_WAIT_FUNC(wait, woken_wake_function); |
50624c93 | 10402 | |
ff960a73 | 10403 | add_wait_queue(&netdev_unregistering_wq, &wait); |
50624c93 | 10404 | for (;;) { |
50624c93 EB |
10405 | unregistering = false; |
10406 | rtnl_lock(); | |
10407 | list_for_each_entry(net, net_list, exit_list) { | |
10408 | if (net->dev_unreg_count > 0) { | |
10409 | unregistering = true; | |
10410 | break; | |
10411 | } | |
10412 | } | |
10413 | if (!unregistering) | |
10414 | break; | |
10415 | __rtnl_unlock(); | |
ff960a73 PZ |
10416 | |
10417 | wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); | |
50624c93 | 10418 | } |
ff960a73 | 10419 | remove_wait_queue(&netdev_unregistering_wq, &wait); |
50624c93 EB |
10420 | } |
10421 | ||
04dc7f6b EB |
10422 | static void __net_exit default_device_exit_batch(struct list_head *net_list) |
10423 | { | |
10424 | /* At exit all network devices most be removed from a network | |
b595076a | 10425 | * namespace. Do this in the reverse order of registration. |
04dc7f6b EB |
10426 | * Do this across as many network namespaces as possible to |
10427 | * improve batching efficiency. | |
10428 | */ | |
10429 | struct net_device *dev; | |
10430 | struct net *net; | |
10431 | LIST_HEAD(dev_kill_list); | |
10432 | ||
50624c93 EB |
10433 | /* To prevent network device cleanup code from dereferencing |
10434 | * loopback devices or network devices that have been freed | |
10435 | * wait here for all pending unregistrations to complete, | |
10436 | * before unregistring the loopback device and allowing the | |
10437 | * network namespace be freed. | |
10438 | * | |
10439 | * The netdev todo list containing all network devices | |
10440 | * unregistrations that happen in default_device_exit_batch | |
10441 | * will run in the rtnl_unlock() at the end of | |
10442 | * default_device_exit_batch. | |
10443 | */ | |
10444 | rtnl_lock_unregistering(net_list); | |
04dc7f6b EB |
10445 | list_for_each_entry(net, net_list, exit_list) { |
10446 | for_each_netdev_reverse(net, dev) { | |
b0ab2fab | 10447 | if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink) |
04dc7f6b EB |
10448 | dev->rtnl_link_ops->dellink(dev, &dev_kill_list); |
10449 | else | |
10450 | unregister_netdevice_queue(dev, &dev_kill_list); | |
10451 | } | |
10452 | } | |
10453 | unregister_netdevice_many(&dev_kill_list); | |
10454 | rtnl_unlock(); | |
10455 | } | |
10456 | ||
022cbae6 | 10457 | static struct pernet_operations __net_initdata default_device_ops = { |
ce286d32 | 10458 | .exit = default_device_exit, |
04dc7f6b | 10459 | .exit_batch = default_device_exit_batch, |
ce286d32 EB |
10460 | }; |
10461 | ||
1da177e4 LT |
10462 | /* |
10463 | * Initialize the DEV module. At boot time this walks the device list and | |
10464 | * unhooks any devices that fail to initialise (normally hardware not | |
10465 | * present) and leaves us with a valid list of present and active devices. | |
10466 | * | |
10467 | */ | |
10468 | ||
10469 | /* | |
10470 | * This is called single threaded during boot, so no need | |
10471 | * to take the rtnl semaphore. | |
10472 | */ | |
10473 | static int __init net_dev_init(void) | |
10474 | { | |
10475 | int i, rc = -ENOMEM; | |
10476 | ||
10477 | BUG_ON(!dev_boot_phase); | |
10478 | ||
1da177e4 LT |
10479 | if (dev_proc_init()) |
10480 | goto out; | |
10481 | ||
8b41d188 | 10482 | if (netdev_kobject_init()) |
1da177e4 LT |
10483 | goto out; |
10484 | ||
10485 | INIT_LIST_HEAD(&ptype_all); | |
82d8a867 | 10486 | for (i = 0; i < PTYPE_HASH_SIZE; i++) |
1da177e4 LT |
10487 | INIT_LIST_HEAD(&ptype_base[i]); |
10488 | ||
62532da9 VY |
10489 | INIT_LIST_HEAD(&offload_base); |
10490 | ||
881d966b EB |
10491 | if (register_pernet_subsys(&netdev_net_ops)) |
10492 | goto out; | |
1da177e4 LT |
10493 | |
10494 | /* | |
10495 | * Initialise the packet receive queues. | |
10496 | */ | |
10497 | ||
6f912042 | 10498 | for_each_possible_cpu(i) { |
41852497 | 10499 | struct work_struct *flush = per_cpu_ptr(&flush_works, i); |
e36fa2f7 | 10500 | struct softnet_data *sd = &per_cpu(softnet_data, i); |
1da177e4 | 10501 | |
41852497 ED |
10502 | INIT_WORK(flush, flush_backlog); |
10503 | ||
e36fa2f7 | 10504 | skb_queue_head_init(&sd->input_pkt_queue); |
6e7676c1 | 10505 | skb_queue_head_init(&sd->process_queue); |
f53c7239 SK |
10506 | #ifdef CONFIG_XFRM_OFFLOAD |
10507 | skb_queue_head_init(&sd->xfrm_backlog); | |
10508 | #endif | |
e36fa2f7 | 10509 | INIT_LIST_HEAD(&sd->poll_list); |
a9cbd588 | 10510 | sd->output_queue_tailp = &sd->output_queue; |
df334545 | 10511 | #ifdef CONFIG_RPS |
e36fa2f7 ED |
10512 | sd->csd.func = rps_trigger_softirq; |
10513 | sd->csd.info = sd; | |
e36fa2f7 | 10514 | sd->cpu = i; |
1e94d72f | 10515 | #endif |
0a9627f2 | 10516 | |
7c4ec749 | 10517 | init_gro_hash(&sd->backlog); |
e36fa2f7 ED |
10518 | sd->backlog.poll = process_backlog; |
10519 | sd->backlog.weight = weight_p; | |
1da177e4 LT |
10520 | } |
10521 | ||
1da177e4 LT |
10522 | dev_boot_phase = 0; |
10523 | ||
505d4f73 EB |
10524 | /* The loopback device is special if any other network devices |
10525 | * is present in a network namespace the loopback device must | |
10526 | * be present. Since we now dynamically allocate and free the | |
10527 | * loopback device ensure this invariant is maintained by | |
10528 | * keeping the loopback device as the first device on the | |
10529 | * list of network devices. Ensuring the loopback devices | |
10530 | * is the first device that appears and the last network device | |
10531 | * that disappears. | |
10532 | */ | |
10533 | if (register_pernet_device(&loopback_net_ops)) | |
10534 | goto out; | |
10535 | ||
10536 | if (register_pernet_device(&default_device_ops)) | |
10537 | goto out; | |
10538 | ||
962cf36c CM |
10539 | open_softirq(NET_TX_SOFTIRQ, net_tx_action); |
10540 | open_softirq(NET_RX_SOFTIRQ, net_rx_action); | |
1da177e4 | 10541 | |
f0bf90de SAS |
10542 | rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead", |
10543 | NULL, dev_cpu_dead); | |
10544 | WARN_ON(rc < 0); | |
1da177e4 LT |
10545 | rc = 0; |
10546 | out: | |
10547 | return rc; | |
10548 | } | |
10549 | ||
10550 | subsys_initcall(net_dev_init); |