]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - net/core/dev.c
net: add strict checks in netdev_name_node_alt_destroy()
[mirror_ubuntu-jammy-kernel.git] / net / core / dev.c
CommitLineData
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 154static DEFINE_SPINLOCK(ptype_lock);
62532da9 155static DEFINE_SPINLOCK(offload_lock);
900ff8c6
CW
156struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
157struct list_head ptype_all __read_mostly; /* Taps */
62532da9 158static struct list_head offload_base __read_mostly;
1da177e4 159
ae78dbfa 160static int netif_rx_internal(struct sk_buff *skb);
54951194 161static int call_netdevice_notifiers_info(unsigned long val,
54951194 162 struct netdev_notifier_info *info);
26372605
PM
163static int call_netdevice_notifiers_extack(unsigned long val,
164 struct net_device *dev,
165 struct netlink_ext_ack *extack);
90b602f8 166static 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 187DEFINE_RWLOCK(dev_base_lock);
1da177e4
LT
188EXPORT_SYMBOL(dev_base_lock);
189
6c557001
FW
190static DEFINE_MUTEX(ifalias_mutex);
191
af12fa6e
ET
192/* protects napi_hash addition/deletion and napi_gen_id */
193static DEFINE_SPINLOCK(napi_hash_lock);
194
52bd2d62 195static unsigned int napi_gen_id = NR_CPUS;
6180d9de 196static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
af12fa6e 197
18afa4b0 198static seqcount_t devnet_rename_seq;
c91f6df2 199
4e985ada
TG
200static inline void dev_base_seq_inc(struct net *net)
201{
643aa9cb 202 while (++net->dev_base_seq == 0)
203 ;
4e985ada
TG
204}
205
881d966b 206static 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 213static 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 218static 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 225static 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
232static 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
246static struct netdev_name_node *
247netdev_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
258static void netdev_name_node_free(struct netdev_name_node *name_node)
259{
260 kfree(name_node);
261}
262
263static 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
270static void netdev_name_node_del(struct netdev_name_node *name_node)
271{
272 hlist_del_rcu(&name_node->hlist);
273}
274
275static 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
287static 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
299int 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}
316EXPORT_SYMBOL(netdev_name_node_alt_create);
317
318static 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
326int 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;
e08ad805
ED
334 /* lookup might have found our primary name or a name belonging
335 * to another device.
336 */
337 if (name_node == dev->name_node || name_node->dev != dev)
338 return -EINVAL;
339
36fbf1e5
JP
340 __netdev_name_node_alt_destroy(name_node);
341
342 return 0;
343}
344EXPORT_SYMBOL(netdev_name_node_alt_destroy);
345
346static void netdev_name_node_alt_flush(struct net_device *dev)
347{
348 struct netdev_name_node *name_node, *tmp;
349
350 list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list)
351 __netdev_name_node_alt_destroy(name_node);
352}
353
ce286d32 354/* Device list insertion */
53759be9 355static void list_netdevice(struct net_device *dev)
ce286d32 356{
c346dca1 357 struct net *net = dev_net(dev);
ce286d32
EB
358
359 ASSERT_RTNL();
360
361 write_lock_bh(&dev_base_lock);
c6d14c84 362 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
ff927412 363 netdev_name_node_add(net, dev->name_node);
fb699dfd
ED
364 hlist_add_head_rcu(&dev->index_hlist,
365 dev_index_hash(net, dev->ifindex));
ce286d32 366 write_unlock_bh(&dev_base_lock);
4e985ada
TG
367
368 dev_base_seq_inc(net);
ce286d32
EB
369}
370
fb699dfd
ED
371/* Device list removal
372 * caller must respect a RCU grace period before freeing/reusing dev
373 */
ce286d32
EB
374static void unlist_netdevice(struct net_device *dev)
375{
376 ASSERT_RTNL();
377
378 /* Unlink dev from the device chain */
379 write_lock_bh(&dev_base_lock);
c6d14c84 380 list_del_rcu(&dev->dev_list);
ff927412 381 netdev_name_node_del(dev->name_node);
fb699dfd 382 hlist_del_rcu(&dev->index_hlist);
ce286d32 383 write_unlock_bh(&dev_base_lock);
4e985ada
TG
384
385 dev_base_seq_inc(dev_net(dev));
ce286d32
EB
386}
387
1da177e4
LT
388/*
389 * Our notifier list
390 */
391
f07d5b94 392static RAW_NOTIFIER_HEAD(netdev_chain);
1da177e4
LT
393
394/*
395 * Device drivers call our routines to queue packets here. We empty the
396 * queue in the local softnet handler.
397 */
bea3348e 398
9958da05 399DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
d1b19dff 400EXPORT_PER_CPU_SYMBOL(softnet_data);
1da177e4 401
1da177e4 402/*******************************************************************************
eb13da1a 403 *
404 * Protocol management and registration routines
405 *
406 *******************************************************************************/
1da177e4 407
1da177e4 408
1da177e4
LT
409/*
410 * Add a protocol ID to the list. Now that the input handler is
411 * smarter we can dispense with all the messy stuff that used to be
412 * here.
413 *
414 * BEWARE!!! Protocol handlers, mangling input packets,
415 * MUST BE last in hash buckets and checking protocol handlers
416 * MUST start from promiscuous ptype_all chain in net_bh.
417 * It is true now, do not change it.
418 * Explanation follows: if protocol handler, mangling packet, will
419 * be the first on list, it is not able to sense, that packet
420 * is cloned and should be copied-on-write, so that it will
421 * change it and subsequent readers will get broken packet.
422 * --ANK (980803)
423 */
424
c07b68e8
ED
425static inline struct list_head *ptype_head(const struct packet_type *pt)
426{
427 if (pt->type == htons(ETH_P_ALL))
7866a621 428 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
c07b68e8 429 else
7866a621
SN
430 return pt->dev ? &pt->dev->ptype_specific :
431 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
c07b68e8
ED
432}
433
1da177e4
LT
434/**
435 * dev_add_pack - add packet handler
436 * @pt: packet type declaration
437 *
438 * Add a protocol handler to the networking stack. The passed &packet_type
439 * is linked into kernel lists and may not be freed until it has been
440 * removed from the kernel lists.
441 *
4ec93edb 442 * This call does not sleep therefore it can not
1da177e4
LT
443 * guarantee all CPU's that are in middle of receiving packets
444 * will see the new packet type (until the next received packet).
445 */
446
447void dev_add_pack(struct packet_type *pt)
448{
c07b68e8 449 struct list_head *head = ptype_head(pt);
1da177e4 450
c07b68e8
ED
451 spin_lock(&ptype_lock);
452 list_add_rcu(&pt->list, head);
453 spin_unlock(&ptype_lock);
1da177e4 454}
d1b19dff 455EXPORT_SYMBOL(dev_add_pack);
1da177e4 456
1da177e4
LT
457/**
458 * __dev_remove_pack - remove packet handler
459 * @pt: packet type declaration
460 *
461 * Remove a protocol handler that was previously added to the kernel
462 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
463 * from the kernel lists and can be freed or reused once this function
4ec93edb 464 * returns.
1da177e4
LT
465 *
466 * The packet type might still be in use by receivers
467 * and must not be freed until after all the CPU's have gone
468 * through a quiescent state.
469 */
470void __dev_remove_pack(struct packet_type *pt)
471{
c07b68e8 472 struct list_head *head = ptype_head(pt);
1da177e4
LT
473 struct packet_type *pt1;
474
c07b68e8 475 spin_lock(&ptype_lock);
1da177e4
LT
476
477 list_for_each_entry(pt1, head, list) {
478 if (pt == pt1) {
479 list_del_rcu(&pt->list);
480 goto out;
481 }
482 }
483
7b6cd1ce 484 pr_warn("dev_remove_pack: %p not found\n", pt);
1da177e4 485out:
c07b68e8 486 spin_unlock(&ptype_lock);
1da177e4 487}
d1b19dff
ED
488EXPORT_SYMBOL(__dev_remove_pack);
489
1da177e4
LT
490/**
491 * dev_remove_pack - remove packet handler
492 * @pt: packet type declaration
493 *
494 * Remove a protocol handler that was previously added to the kernel
495 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
496 * from the kernel lists and can be freed or reused once this function
497 * returns.
498 *
499 * This call sleeps to guarantee that no CPU is looking at the packet
500 * type after return.
501 */
502void dev_remove_pack(struct packet_type *pt)
503{
504 __dev_remove_pack(pt);
4ec93edb 505
1da177e4
LT
506 synchronize_net();
507}
d1b19dff 508EXPORT_SYMBOL(dev_remove_pack);
1da177e4 509
62532da9
VY
510
511/**
512 * dev_add_offload - register offload handlers
513 * @po: protocol offload declaration
514 *
515 * Add protocol offload handlers to the networking stack. The passed
516 * &proto_offload is linked into kernel lists and may not be freed until
517 * it has been removed from the kernel lists.
518 *
519 * This call does not sleep therefore it can not
520 * guarantee all CPU's that are in middle of receiving packets
521 * will see the new offload handlers (until the next received packet).
522 */
523void dev_add_offload(struct packet_offload *po)
524{
bdef7de4 525 struct packet_offload *elem;
62532da9
VY
526
527 spin_lock(&offload_lock);
bdef7de4
DM
528 list_for_each_entry(elem, &offload_base, list) {
529 if (po->priority < elem->priority)
530 break;
531 }
532 list_add_rcu(&po->list, elem->list.prev);
62532da9
VY
533 spin_unlock(&offload_lock);
534}
535EXPORT_SYMBOL(dev_add_offload);
536
537/**
538 * __dev_remove_offload - remove offload handler
539 * @po: packet offload declaration
540 *
541 * Remove a protocol offload handler that was previously added to the
542 * kernel offload handlers by dev_add_offload(). The passed &offload_type
543 * is removed from the kernel lists and can be freed or reused once this
544 * function returns.
545 *
546 * The packet type might still be in use by receivers
547 * and must not be freed until after all the CPU's have gone
548 * through a quiescent state.
549 */
1d143d9f 550static void __dev_remove_offload(struct packet_offload *po)
62532da9
VY
551{
552 struct list_head *head = &offload_base;
553 struct packet_offload *po1;
554
c53aa505 555 spin_lock(&offload_lock);
62532da9
VY
556
557 list_for_each_entry(po1, head, list) {
558 if (po == po1) {
559 list_del_rcu(&po->list);
560 goto out;
561 }
562 }
563
564 pr_warn("dev_remove_offload: %p not found\n", po);
565out:
c53aa505 566 spin_unlock(&offload_lock);
62532da9 567}
62532da9
VY
568
569/**
570 * dev_remove_offload - remove packet offload handler
571 * @po: packet offload declaration
572 *
573 * Remove a packet offload handler that was previously added to the kernel
574 * offload handlers by dev_add_offload(). The passed &offload_type is
575 * removed from the kernel lists and can be freed or reused once this
576 * function returns.
577 *
578 * This call sleeps to guarantee that no CPU is looking at the packet
579 * type after return.
580 */
581void dev_remove_offload(struct packet_offload *po)
582{
583 __dev_remove_offload(po);
584
585 synchronize_net();
586}
587EXPORT_SYMBOL(dev_remove_offload);
588
1da177e4 589/******************************************************************************
eb13da1a 590 *
591 * Device Boot-time Settings Routines
592 *
593 ******************************************************************************/
1da177e4
LT
594
595/* Boot time configuration table */
596static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
597
598/**
599 * netdev_boot_setup_add - add new setup entry
600 * @name: name of the device
601 * @map: configured settings for the device
602 *
603 * Adds new setup entry to the dev_boot_setup list. The function
604 * returns 0 on error and 1 on success. This is a generic routine to
605 * all netdevices.
606 */
607static int netdev_boot_setup_add(char *name, struct ifmap *map)
608{
609 struct netdev_boot_setup *s;
610 int i;
611
612 s = dev_boot_setup;
613 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
614 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
615 memset(s[i].name, 0, sizeof(s[i].name));
93b3cff9 616 strlcpy(s[i].name, name, IFNAMSIZ);
1da177e4
LT
617 memcpy(&s[i].map, map, sizeof(s[i].map));
618 break;
619 }
620 }
621
622 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
623}
624
625/**
722c9a0c 626 * netdev_boot_setup_check - check boot time settings
627 * @dev: the netdevice
1da177e4 628 *
722c9a0c 629 * Check boot time settings for the device.
630 * The found settings are set for the device to be used
631 * later in the device probing.
632 * Returns 0 if no settings found, 1 if they are.
1da177e4
LT
633 */
634int netdev_boot_setup_check(struct net_device *dev)
635{
636 struct netdev_boot_setup *s = dev_boot_setup;
637 int i;
638
639 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
640 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
93b3cff9 641 !strcmp(dev->name, s[i].name)) {
722c9a0c 642 dev->irq = s[i].map.irq;
643 dev->base_addr = s[i].map.base_addr;
644 dev->mem_start = s[i].map.mem_start;
645 dev->mem_end = s[i].map.mem_end;
1da177e4
LT
646 return 1;
647 }
648 }
649 return 0;
650}
d1b19dff 651EXPORT_SYMBOL(netdev_boot_setup_check);
1da177e4
LT
652
653
654/**
722c9a0c 655 * netdev_boot_base - get address from boot time settings
656 * @prefix: prefix for network device
657 * @unit: id for network device
658 *
659 * Check boot time settings for the base address of device.
660 * The found settings are set for the device to be used
661 * later in the device probing.
662 * Returns 0 if no settings found.
1da177e4
LT
663 */
664unsigned long netdev_boot_base(const char *prefix, int unit)
665{
666 const struct netdev_boot_setup *s = dev_boot_setup;
667 char name[IFNAMSIZ];
668 int i;
669
670 sprintf(name, "%s%d", prefix, unit);
671
672 /*
673 * If device already registered then return base of 1
674 * to indicate not to probe for this interface
675 */
881d966b 676 if (__dev_get_by_name(&init_net, name))
1da177e4
LT
677 return 1;
678
679 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
680 if (!strcmp(name, s[i].name))
681 return s[i].map.base_addr;
682 return 0;
683}
684
685/*
686 * Saves at boot time configured settings for any netdevice.
687 */
688int __init netdev_boot_setup(char *str)
689{
690 int ints[5];
691 struct ifmap map;
692
693 str = get_options(str, ARRAY_SIZE(ints), ints);
694 if (!str || !*str)
695 return 0;
696
697 /* Save settings */
698 memset(&map, 0, sizeof(map));
699 if (ints[0] > 0)
700 map.irq = ints[1];
701 if (ints[0] > 1)
702 map.base_addr = ints[2];
703 if (ints[0] > 2)
704 map.mem_start = ints[3];
705 if (ints[0] > 3)
706 map.mem_end = ints[4];
707
708 /* Add new entry to the list */
709 return netdev_boot_setup_add(str, &map);
710}
711
712__setup("netdev=", netdev_boot_setup);
713
714/*******************************************************************************
eb13da1a 715 *
716 * Device Interface Subroutines
717 *
718 *******************************************************************************/
1da177e4 719
a54acb3a
ND
720/**
721 * dev_get_iflink - get 'iflink' value of a interface
722 * @dev: targeted interface
723 *
724 * Indicates the ifindex the interface is linked to.
725 * Physical interfaces have the same 'ifindex' and 'iflink' values.
726 */
727
728int dev_get_iflink(const struct net_device *dev)
729{
730 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
731 return dev->netdev_ops->ndo_get_iflink(dev);
732
7a66bbc9 733 return dev->ifindex;
a54acb3a
ND
734}
735EXPORT_SYMBOL(dev_get_iflink);
736
fc4099f1
PS
737/**
738 * dev_fill_metadata_dst - Retrieve tunnel egress information.
739 * @dev: targeted interface
740 * @skb: The packet.
741 *
742 * For better visibility of tunnel traffic OVS needs to retrieve
743 * egress tunnel information for a packet. Following API allows
744 * user to get this info.
745 */
746int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
747{
748 struct ip_tunnel_info *info;
749
750 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
751 return -EINVAL;
752
753 info = skb_tunnel_info_unclone(skb);
754 if (!info)
755 return -ENOMEM;
756 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
757 return -EINVAL;
758
759 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
760}
761EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
762
1da177e4
LT
763/**
764 * __dev_get_by_name - find a device by its name
c4ea43c5 765 * @net: the applicable net namespace
1da177e4
LT
766 * @name: name to find
767 *
768 * Find an interface by name. Must be called under RTNL semaphore
769 * or @dev_base_lock. If the name is found a pointer to the device
770 * is returned. If the name is not found then %NULL is returned. The
771 * reference counters are not incremented so the caller must be
772 * careful with locks.
773 */
774
881d966b 775struct net_device *__dev_get_by_name(struct net *net, const char *name)
1da177e4 776{
ff927412 777 struct netdev_name_node *node_name;
1da177e4 778
ff927412
JP
779 node_name = netdev_name_node_lookup(net, name);
780 return node_name ? node_name->dev : NULL;
1da177e4 781}
d1b19dff 782EXPORT_SYMBOL(__dev_get_by_name);
1da177e4 783
72c9528b 784/**
722c9a0c 785 * dev_get_by_name_rcu - find a device by its name
786 * @net: the applicable net namespace
787 * @name: name to find
788 *
789 * Find an interface by name.
790 * If the name is found a pointer to the device is returned.
791 * If the name is not found then %NULL is returned.
792 * The reference counters are not incremented so the caller must be
793 * careful with locks. The caller must hold RCU lock.
72c9528b
ED
794 */
795
796struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
797{
ff927412 798 struct netdev_name_node *node_name;
72c9528b 799
ff927412
JP
800 node_name = netdev_name_node_lookup_rcu(net, name);
801 return node_name ? node_name->dev : NULL;
72c9528b
ED
802}
803EXPORT_SYMBOL(dev_get_by_name_rcu);
804
1da177e4
LT
805/**
806 * dev_get_by_name - find a device by its name
c4ea43c5 807 * @net: the applicable net namespace
1da177e4
LT
808 * @name: name to find
809 *
810 * Find an interface by name. This can be called from any
811 * context and does its own locking. The returned handle has
812 * the usage count incremented and the caller must use dev_put() to
813 * release it when it is no longer needed. %NULL is returned if no
814 * matching device is found.
815 */
816
881d966b 817struct net_device *dev_get_by_name(struct net *net, const char *name)
1da177e4
LT
818{
819 struct net_device *dev;
820
72c9528b
ED
821 rcu_read_lock();
822 dev = dev_get_by_name_rcu(net, name);
1da177e4
LT
823 if (dev)
824 dev_hold(dev);
72c9528b 825 rcu_read_unlock();
1da177e4
LT
826 return dev;
827}
d1b19dff 828EXPORT_SYMBOL(dev_get_by_name);
1da177e4
LT
829
830/**
831 * __dev_get_by_index - find a device by its ifindex
c4ea43c5 832 * @net: the applicable net namespace
1da177e4
LT
833 * @ifindex: index of device
834 *
835 * Search for an interface by index. Returns %NULL if the device
836 * is not found or a pointer to the device. The device has not
837 * had its reference counter increased so the caller must be careful
838 * about locking. The caller must hold either the RTNL semaphore
839 * or @dev_base_lock.
840 */
841
881d966b 842struct net_device *__dev_get_by_index(struct net *net, int ifindex)
1da177e4 843{
0bd8d536
ED
844 struct net_device *dev;
845 struct hlist_head *head = dev_index_hash(net, ifindex);
1da177e4 846
b67bfe0d 847 hlist_for_each_entry(dev, head, index_hlist)
1da177e4
LT
848 if (dev->ifindex == ifindex)
849 return dev;
0bd8d536 850
1da177e4
LT
851 return NULL;
852}
d1b19dff 853EXPORT_SYMBOL(__dev_get_by_index);
1da177e4 854
fb699dfd
ED
855/**
856 * dev_get_by_index_rcu - find a device by its ifindex
857 * @net: the applicable net namespace
858 * @ifindex: index of device
859 *
860 * Search for an interface by index. Returns %NULL if the device
861 * is not found or a pointer to the device. The device has not
862 * had its reference counter increased so the caller must be careful
863 * about locking. The caller must hold RCU lock.
864 */
865
866struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
867{
fb699dfd
ED
868 struct net_device *dev;
869 struct hlist_head *head = dev_index_hash(net, ifindex);
870
b67bfe0d 871 hlist_for_each_entry_rcu(dev, head, index_hlist)
fb699dfd
ED
872 if (dev->ifindex == ifindex)
873 return dev;
874
875 return NULL;
876}
877EXPORT_SYMBOL(dev_get_by_index_rcu);
878
1da177e4
LT
879
880/**
881 * dev_get_by_index - find a device by its ifindex
c4ea43c5 882 * @net: the applicable net namespace
1da177e4
LT
883 * @ifindex: index of device
884 *
885 * Search for an interface by index. Returns NULL if the device
886 * is not found or a pointer to the device. The device returned has
887 * had a reference added and the pointer is safe until the user calls
888 * dev_put to indicate they have finished with it.
889 */
890
881d966b 891struct net_device *dev_get_by_index(struct net *net, int ifindex)
1da177e4
LT
892{
893 struct net_device *dev;
894
fb699dfd
ED
895 rcu_read_lock();
896 dev = dev_get_by_index_rcu(net, ifindex);
1da177e4
LT
897 if (dev)
898 dev_hold(dev);
fb699dfd 899 rcu_read_unlock();
1da177e4
LT
900 return dev;
901}
d1b19dff 902EXPORT_SYMBOL(dev_get_by_index);
1da177e4 903
90b602f8
ML
904/**
905 * dev_get_by_napi_id - find a device by napi_id
906 * @napi_id: ID of the NAPI struct
907 *
908 * Search for an interface by NAPI ID. Returns %NULL if the device
909 * is not found or a pointer to the device. The device has not had
910 * its reference counter increased so the caller must be careful
911 * about locking. The caller must hold RCU lock.
912 */
913
914struct net_device *dev_get_by_napi_id(unsigned int napi_id)
915{
916 struct napi_struct *napi;
917
918 WARN_ON_ONCE(!rcu_read_lock_held());
919
920 if (napi_id < MIN_NAPI_ID)
921 return NULL;
922
923 napi = napi_by_id(napi_id);
924
925 return napi ? napi->dev : NULL;
926}
927EXPORT_SYMBOL(dev_get_by_napi_id);
928
5dbe7c17
NS
929/**
930 * netdev_get_name - get a netdevice name, knowing its ifindex.
931 * @net: network namespace
932 * @name: a pointer to the buffer where the name will be stored.
933 * @ifindex: the ifindex of the interface to get the name from.
934 *
935 * The use of raw_seqcount_begin() and cond_resched() before
936 * retrying is required as we want to give the writers a chance
2da2b32f 937 * to complete when CONFIG_PREEMPTION is not set.
5dbe7c17
NS
938 */
939int netdev_get_name(struct net *net, char *name, int ifindex)
940{
941 struct net_device *dev;
942 unsigned int seq;
943
944retry:
945 seq = raw_seqcount_begin(&devnet_rename_seq);
946 rcu_read_lock();
947 dev = dev_get_by_index_rcu(net, ifindex);
948 if (!dev) {
949 rcu_read_unlock();
950 return -ENODEV;
951 }
952
953 strcpy(name, dev->name);
954 rcu_read_unlock();
955 if (read_seqcount_retry(&devnet_rename_seq, seq)) {
956 cond_resched();
957 goto retry;
958 }
959
960 return 0;
961}
962
1da177e4 963/**
941666c2 964 * dev_getbyhwaddr_rcu - find a device by its hardware address
c4ea43c5 965 * @net: the applicable net namespace
1da177e4
LT
966 * @type: media type of device
967 * @ha: hardware address
968 *
969 * Search for an interface by MAC address. Returns NULL if the device
c506653d
ED
970 * is not found or a pointer to the device.
971 * The caller must hold RCU or RTNL.
941666c2 972 * The returned device has not had its ref count increased
1da177e4
LT
973 * and the caller must therefore be careful about locking
974 *
1da177e4
LT
975 */
976
941666c2
ED
977struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
978 const char *ha)
1da177e4
LT
979{
980 struct net_device *dev;
981
941666c2 982 for_each_netdev_rcu(net, dev)
1da177e4
LT
983 if (dev->type == type &&
984 !memcmp(dev->dev_addr, ha, dev->addr_len))
7562f876
PE
985 return dev;
986
987 return NULL;
1da177e4 988}
941666c2 989EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
cf309e3f 990
881d966b 991struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
1da177e4
LT
992{
993 struct net_device *dev;
994
4e9cac2b 995 ASSERT_RTNL();
881d966b 996 for_each_netdev(net, dev)
4e9cac2b 997 if (dev->type == type)
7562f876
PE
998 return dev;
999
1000 return NULL;
4e9cac2b 1001}
4e9cac2b
PM
1002EXPORT_SYMBOL(__dev_getfirstbyhwtype);
1003
881d966b 1004struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
4e9cac2b 1005{
99fe3c39 1006 struct net_device *dev, *ret = NULL;
4e9cac2b 1007
99fe3c39
ED
1008 rcu_read_lock();
1009 for_each_netdev_rcu(net, dev)
1010 if (dev->type == type) {
1011 dev_hold(dev);
1012 ret = dev;
1013 break;
1014 }
1015 rcu_read_unlock();
1016 return ret;
1da177e4 1017}
1da177e4
LT
1018EXPORT_SYMBOL(dev_getfirstbyhwtype);
1019
1020/**
6c555490 1021 * __dev_get_by_flags - find any device with given flags
c4ea43c5 1022 * @net: the applicable net namespace
1da177e4
LT
1023 * @if_flags: IFF_* values
1024 * @mask: bitmask of bits in if_flags to check
1025 *
1026 * Search for any interface with the given flags. Returns NULL if a device
bb69ae04 1027 * is not found or a pointer to the device. Must be called inside
6c555490 1028 * rtnl_lock(), and result refcount is unchanged.
1da177e4
LT
1029 */
1030
6c555490
WC
1031struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
1032 unsigned short mask)
1da177e4 1033{
7562f876 1034 struct net_device *dev, *ret;
1da177e4 1035
6c555490
WC
1036 ASSERT_RTNL();
1037
7562f876 1038 ret = NULL;
6c555490 1039 for_each_netdev(net, dev) {
1da177e4 1040 if (((dev->flags ^ if_flags) & mask) == 0) {
7562f876 1041 ret = dev;
1da177e4
LT
1042 break;
1043 }
1044 }
7562f876 1045 return ret;
1da177e4 1046}
6c555490 1047EXPORT_SYMBOL(__dev_get_by_flags);
1da177e4
LT
1048
1049/**
1050 * dev_valid_name - check if name is okay for network device
1051 * @name: name string
1052 *
1053 * Network device names need to be valid file names to
c7fa9d18
DM
1054 * to allow sysfs to work. We also disallow any kind of
1055 * whitespace.
1da177e4 1056 */
95f050bf 1057bool dev_valid_name(const char *name)
1da177e4 1058{
c7fa9d18 1059 if (*name == '\0')
95f050bf 1060 return false;
a9d48205 1061 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
95f050bf 1062 return false;
c7fa9d18 1063 if (!strcmp(name, ".") || !strcmp(name, ".."))
95f050bf 1064 return false;
c7fa9d18
DM
1065
1066 while (*name) {
a4176a93 1067 if (*name == '/' || *name == ':' || isspace(*name))
95f050bf 1068 return false;
c7fa9d18
DM
1069 name++;
1070 }
95f050bf 1071 return true;
1da177e4 1072}
d1b19dff 1073EXPORT_SYMBOL(dev_valid_name);
1da177e4
LT
1074
1075/**
b267b179
EB
1076 * __dev_alloc_name - allocate a name for a device
1077 * @net: network namespace to allocate the device name in
1da177e4 1078 * @name: name format string
b267b179 1079 * @buf: scratch buffer and result name string
1da177e4
LT
1080 *
1081 * Passed a format string - eg "lt%d" it will try and find a suitable
3041a069
SH
1082 * id. It scans list of devices to build up a free map, then chooses
1083 * the first empty slot. The caller must hold the dev_base or rtnl lock
1084 * while allocating the name and adding the device in order to avoid
1085 * duplicates.
1086 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1087 * Returns the number of the unit assigned or a negative errno code.
1da177e4
LT
1088 */
1089
b267b179 1090static int __dev_alloc_name(struct net *net, const char *name, char *buf)
1da177e4
LT
1091{
1092 int i = 0;
1da177e4
LT
1093 const char *p;
1094 const int max_netdevices = 8*PAGE_SIZE;
cfcabdcc 1095 unsigned long *inuse;
1da177e4
LT
1096 struct net_device *d;
1097
93809105
RV
1098 if (!dev_valid_name(name))
1099 return -EINVAL;
1100
51f299dd 1101 p = strchr(name, '%');
1da177e4
LT
1102 if (p) {
1103 /*
1104 * Verify the string as this thing may have come from
1105 * the user. There must be either one "%d" and no other "%"
1106 * characters.
1107 */
1108 if (p[1] != 'd' || strchr(p + 2, '%'))
1109 return -EINVAL;
1110
1111 /* Use one page as a bit array of possible slots */
cfcabdcc 1112 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
1da177e4
LT
1113 if (!inuse)
1114 return -ENOMEM;
1115
881d966b 1116 for_each_netdev(net, d) {
1da177e4
LT
1117 if (!sscanf(d->name, name, &i))
1118 continue;
1119 if (i < 0 || i >= max_netdevices)
1120 continue;
1121
1122 /* avoid cases where sscanf is not exact inverse of printf */
b267b179 1123 snprintf(buf, IFNAMSIZ, name, i);
1da177e4
LT
1124 if (!strncmp(buf, d->name, IFNAMSIZ))
1125 set_bit(i, inuse);
1126 }
1127
1128 i = find_first_zero_bit(inuse, max_netdevices);
1129 free_page((unsigned long) inuse);
1130 }
1131
6224abda 1132 snprintf(buf, IFNAMSIZ, name, i);
b267b179 1133 if (!__dev_get_by_name(net, buf))
1da177e4 1134 return i;
1da177e4
LT
1135
1136 /* It is possible to run out of possible slots
1137 * when the name is long and there isn't enough space left
1138 * for the digits, or if all bits are used.
1139 */
029b6d14 1140 return -ENFILE;
1da177e4
LT
1141}
1142
2c88b855
RV
1143static int dev_alloc_name_ns(struct net *net,
1144 struct net_device *dev,
1145 const char *name)
1146{
1147 char buf[IFNAMSIZ];
1148 int ret;
1149
c46d7642 1150 BUG_ON(!net);
2c88b855
RV
1151 ret = __dev_alloc_name(net, name, buf);
1152 if (ret >= 0)
1153 strlcpy(dev->name, buf, IFNAMSIZ);
1154 return ret;
1da177e4
LT
1155}
1156
b267b179
EB
1157/**
1158 * dev_alloc_name - allocate a name for a device
1159 * @dev: device
1160 * @name: name format string
1161 *
1162 * Passed a format string - eg "lt%d" it will try and find a suitable
1163 * id. It scans list of devices to build up a free map, then chooses
1164 * the first empty slot. The caller must hold the dev_base or rtnl lock
1165 * while allocating the name and adding the device in order to avoid
1166 * duplicates.
1167 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1168 * Returns the number of the unit assigned or a negative errno code.
1169 */
1170
1171int dev_alloc_name(struct net_device *dev, const char *name)
1172{
c46d7642 1173 return dev_alloc_name_ns(dev_net(dev), dev, name);
b267b179 1174}
d1b19dff 1175EXPORT_SYMBOL(dev_alloc_name);
b267b179 1176
bacb7e18
ED
1177static int dev_get_valid_name(struct net *net, struct net_device *dev,
1178 const char *name)
828de4f6 1179{
55a5ec9b
DM
1180 BUG_ON(!net);
1181
1182 if (!dev_valid_name(name))
1183 return -EINVAL;
1184
1185 if (strchr(name, '%'))
1186 return dev_alloc_name_ns(net, dev, name);
1187 else if (__dev_get_by_name(net, name))
1188 return -EEXIST;
1189 else if (dev->name != name)
1190 strlcpy(dev->name, name, IFNAMSIZ);
1191
1192 return 0;
d9031024 1193}
1da177e4
LT
1194
1195/**
1196 * dev_change_name - change name of a device
1197 * @dev: device
1198 * @newname: name (or format string) must be at least IFNAMSIZ
1199 *
1200 * Change name of a device, can pass format strings "eth%d".
1201 * for wildcarding.
1202 */
cf04a4c7 1203int dev_change_name(struct net_device *dev, const char *newname)
1da177e4 1204{
238fa362 1205 unsigned char old_assign_type;
fcc5a03a 1206 char oldname[IFNAMSIZ];
1da177e4 1207 int err = 0;
fcc5a03a 1208 int ret;
881d966b 1209 struct net *net;
1da177e4
LT
1210
1211 ASSERT_RTNL();
c346dca1 1212 BUG_ON(!dev_net(dev));
1da177e4 1213
c346dca1 1214 net = dev_net(dev);
8065a779
SWL
1215
1216 /* Some auto-enslaved devices e.g. failover slaves are
1217 * special, as userspace might rename the device after
1218 * the interface had been brought up and running since
1219 * the point kernel initiated auto-enslavement. Allow
1220 * live name change even when these slave devices are
1221 * up and running.
1222 *
1223 * Typically, users of these auto-enslaving devices
1224 * don't actually care about slave name change, as
1225 * they are supposed to operate on master interface
1226 * directly.
1227 */
1228 if (dev->flags & IFF_UP &&
1229 likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK)))
1da177e4
LT
1230 return -EBUSY;
1231
30e6c9fa 1232 write_seqcount_begin(&devnet_rename_seq);
c91f6df2
BH
1233
1234 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
30e6c9fa 1235 write_seqcount_end(&devnet_rename_seq);
c8d90dca 1236 return 0;
c91f6df2 1237 }
c8d90dca 1238
fcc5a03a
HX
1239 memcpy(oldname, dev->name, IFNAMSIZ);
1240
828de4f6 1241 err = dev_get_valid_name(net, dev, newname);
c91f6df2 1242 if (err < 0) {
30e6c9fa 1243 write_seqcount_end(&devnet_rename_seq);
d9031024 1244 return err;
c91f6df2 1245 }
1da177e4 1246
6fe82a39
VF
1247 if (oldname[0] && !strchr(oldname, '%'))
1248 netdev_info(dev, "renamed from %s\n", oldname);
1249
238fa362
TG
1250 old_assign_type = dev->name_assign_type;
1251 dev->name_assign_type = NET_NAME_RENAMED;
1252
fcc5a03a 1253rollback:
a1b3f594
EB
1254 ret = device_rename(&dev->dev, dev->name);
1255 if (ret) {
1256 memcpy(dev->name, oldname, IFNAMSIZ);
238fa362 1257 dev->name_assign_type = old_assign_type;
30e6c9fa 1258 write_seqcount_end(&devnet_rename_seq);
a1b3f594 1259 return ret;
dcc99773 1260 }
7f988eab 1261
30e6c9fa 1262 write_seqcount_end(&devnet_rename_seq);
c91f6df2 1263
5bb025fa
VF
1264 netdev_adjacent_rename_links(dev, oldname);
1265
7f988eab 1266 write_lock_bh(&dev_base_lock);
ff927412 1267 netdev_name_node_del(dev->name_node);
72c9528b
ED
1268 write_unlock_bh(&dev_base_lock);
1269
1270 synchronize_rcu();
1271
1272 write_lock_bh(&dev_base_lock);
ff927412 1273 netdev_name_node_add(net, dev->name_node);
7f988eab
HX
1274 write_unlock_bh(&dev_base_lock);
1275
056925ab 1276 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
fcc5a03a
HX
1277 ret = notifier_to_errno(ret);
1278
1279 if (ret) {
91e9c07b
ED
1280 /* err >= 0 after dev_alloc_name() or stores the first errno */
1281 if (err >= 0) {
fcc5a03a 1282 err = ret;
30e6c9fa 1283 write_seqcount_begin(&devnet_rename_seq);
fcc5a03a 1284 memcpy(dev->name, oldname, IFNAMSIZ);
5bb025fa 1285 memcpy(oldname, newname, IFNAMSIZ);
238fa362
TG
1286 dev->name_assign_type = old_assign_type;
1287 old_assign_type = NET_NAME_RENAMED;
fcc5a03a 1288 goto rollback;
91e9c07b 1289 } else {
7b6cd1ce 1290 pr_err("%s: name change rollback failed: %d\n",
91e9c07b 1291 dev->name, ret);
fcc5a03a
HX
1292 }
1293 }
1da177e4
LT
1294
1295 return err;
1296}
1297
0b815a1a
SH
1298/**
1299 * dev_set_alias - change ifalias of a device
1300 * @dev: device
1301 * @alias: name up to IFALIASZ
f0db275a 1302 * @len: limit of bytes to copy from info
0b815a1a
SH
1303 *
1304 * Set ifalias for a device,
1305 */
1306int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1307{
6c557001 1308 struct dev_ifalias *new_alias = NULL;
0b815a1a
SH
1309
1310 if (len >= IFALIASZ)
1311 return -EINVAL;
1312
6c557001
FW
1313 if (len) {
1314 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1315 if (!new_alias)
1316 return -ENOMEM;
1317
1318 memcpy(new_alias->ifalias, alias, len);
1319 new_alias->ifalias[len] = 0;
96ca4a2c
OH
1320 }
1321
6c557001 1322 mutex_lock(&ifalias_mutex);
e3f0d761
PM
1323 new_alias = rcu_replace_pointer(dev->ifalias, new_alias,
1324 mutex_is_locked(&ifalias_mutex));
6c557001
FW
1325 mutex_unlock(&ifalias_mutex);
1326
1327 if (new_alias)
1328 kfree_rcu(new_alias, rcuhead);
0b815a1a 1329
0b815a1a
SH
1330 return len;
1331}
0fe554a4 1332EXPORT_SYMBOL(dev_set_alias);
0b815a1a 1333
6c557001
FW
1334/**
1335 * dev_get_alias - get ifalias of a device
1336 * @dev: device
20e88320 1337 * @name: buffer to store name of ifalias
6c557001
FW
1338 * @len: size of buffer
1339 *
1340 * get ifalias for a device. Caller must make sure dev cannot go
1341 * away, e.g. rcu read lock or own a reference count to device.
1342 */
1343int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1344{
1345 const struct dev_ifalias *alias;
1346 int ret = 0;
1347
1348 rcu_read_lock();
1349 alias = rcu_dereference(dev->ifalias);
1350 if (alias)
1351 ret = snprintf(name, len, "%s", alias->ifalias);
1352 rcu_read_unlock();
1353
1354 return ret;
1355}
0b815a1a 1356
d8a33ac4 1357/**
3041a069 1358 * netdev_features_change - device changes features
d8a33ac4
SH
1359 * @dev: device to cause notification
1360 *
1361 * Called to indicate a device has changed features.
1362 */
1363void netdev_features_change(struct net_device *dev)
1364{
056925ab 1365 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
d8a33ac4
SH
1366}
1367EXPORT_SYMBOL(netdev_features_change);
1368
1da177e4
LT
1369/**
1370 * netdev_state_change - device changes state
1371 * @dev: device to cause notification
1372 *
1373 * Called to indicate a device has changed state. This function calls
1374 * the notifier chains for netdev_chain and sends a NEWLINK message
1375 * to the routing socket.
1376 */
1377void netdev_state_change(struct net_device *dev)
1378{
1379 if (dev->flags & IFF_UP) {
51d0c047
DA
1380 struct netdev_notifier_change_info change_info = {
1381 .info.dev = dev,
1382 };
54951194 1383
51d0c047 1384 call_netdevice_notifiers_info(NETDEV_CHANGE,
54951194 1385 &change_info.info);
7f294054 1386 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
1da177e4
LT
1387 }
1388}
d1b19dff 1389EXPORT_SYMBOL(netdev_state_change);
1da177e4 1390
ee89bab1 1391/**
722c9a0c 1392 * netdev_notify_peers - notify network peers about existence of @dev
1393 * @dev: network device
ee89bab1
AW
1394 *
1395 * Generate traffic such that interested network peers are aware of
1396 * @dev, such as by generating a gratuitous ARP. This may be used when
1397 * a device wants to inform the rest of the network about some sort of
1398 * reconfiguration such as a failover event or virtual machine
1399 * migration.
1400 */
1401void netdev_notify_peers(struct net_device *dev)
c1da4ac7 1402{
ee89bab1
AW
1403 rtnl_lock();
1404 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
37c343b4 1405 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
ee89bab1 1406 rtnl_unlock();
c1da4ac7 1407}
ee89bab1 1408EXPORT_SYMBOL(netdev_notify_peers);
c1da4ac7 1409
40c900aa 1410static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
1da177e4 1411{
d314774c 1412 const struct net_device_ops *ops = dev->netdev_ops;
3b8bcfd5 1413 int ret;
1da177e4 1414
e46b66bc
BH
1415 ASSERT_RTNL();
1416
1da177e4
LT
1417 if (!netif_device_present(dev))
1418 return -ENODEV;
1419
ca99ca14
NH
1420 /* Block netpoll from trying to do any rx path servicing.
1421 * If we don't do this there is a chance ndo_poll_controller
1422 * or ndo_poll may be running while we open the device
1423 */
66b5552f 1424 netpoll_poll_disable(dev);
ca99ca14 1425
40c900aa 1426 ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack);
3b8bcfd5
JB
1427 ret = notifier_to_errno(ret);
1428 if (ret)
1429 return ret;
1430
1da177e4 1431 set_bit(__LINK_STATE_START, &dev->state);
bada339b 1432
d314774c
SH
1433 if (ops->ndo_validate_addr)
1434 ret = ops->ndo_validate_addr(dev);
bada339b 1435
d314774c
SH
1436 if (!ret && ops->ndo_open)
1437 ret = ops->ndo_open(dev);
1da177e4 1438
66b5552f 1439 netpoll_poll_enable(dev);
ca99ca14 1440
bada339b
JG
1441 if (ret)
1442 clear_bit(__LINK_STATE_START, &dev->state);
1443 else {
1da177e4 1444 dev->flags |= IFF_UP;
4417da66 1445 dev_set_rx_mode(dev);
1da177e4 1446 dev_activate(dev);
7bf23575 1447 add_device_randomness(dev->dev_addr, dev->addr_len);
1da177e4 1448 }
bada339b 1449
1da177e4
LT
1450 return ret;
1451}
1452
1453/**
bd380811 1454 * dev_open - prepare an interface for use.
00f54e68
PM
1455 * @dev: device to open
1456 * @extack: netlink extended ack
1da177e4 1457 *
bd380811
PM
1458 * Takes a device from down to up state. The device's private open
1459 * function is invoked and then the multicast lists are loaded. Finally
1460 * the device is moved into the up state and a %NETDEV_UP message is
1461 * sent to the netdev notifier chain.
1462 *
1463 * Calling this function on an active interface is a nop. On a failure
1464 * a negative errno code is returned.
1da177e4 1465 */
00f54e68 1466int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
bd380811
PM
1467{
1468 int ret;
1469
bd380811
PM
1470 if (dev->flags & IFF_UP)
1471 return 0;
1472
40c900aa 1473 ret = __dev_open(dev, extack);
bd380811
PM
1474 if (ret < 0)
1475 return ret;
1476
7f294054 1477 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
bd380811
PM
1478 call_netdevice_notifiers(NETDEV_UP, dev);
1479
1480 return ret;
1481}
1482EXPORT_SYMBOL(dev_open);
1483
7051b88a 1484static void __dev_close_many(struct list_head *head)
1da177e4 1485{
44345724 1486 struct net_device *dev;
e46b66bc 1487
bd380811 1488 ASSERT_RTNL();
9d5010db
DM
1489 might_sleep();
1490
5cde2829 1491 list_for_each_entry(dev, head, close_list) {
3f4df206 1492 /* Temporarily disable netpoll until the interface is down */
66b5552f 1493 netpoll_poll_disable(dev);
3f4df206 1494
44345724 1495 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1da177e4 1496
44345724 1497 clear_bit(__LINK_STATE_START, &dev->state);
1da177e4 1498
44345724
OP
1499 /* Synchronize to scheduled poll. We cannot touch poll list, it
1500 * can be even on different cpu. So just clear netif_running().
1501 *
1502 * dev->stop() will invoke napi_disable() on all of it's
1503 * napi_struct instances on this device.
1504 */
4e857c58 1505 smp_mb__after_atomic(); /* Commit netif_running(). */
44345724 1506 }
1da177e4 1507
44345724 1508 dev_deactivate_many(head);
d8b2a4d2 1509
5cde2829 1510 list_for_each_entry(dev, head, close_list) {
44345724 1511 const struct net_device_ops *ops = dev->netdev_ops;
1da177e4 1512
44345724
OP
1513 /*
1514 * Call the device specific close. This cannot fail.
1515 * Only if device is UP
1516 *
1517 * We allow it to be called even after a DETACH hot-plug
1518 * event.
1519 */
1520 if (ops->ndo_stop)
1521 ops->ndo_stop(dev);
1522
44345724 1523 dev->flags &= ~IFF_UP;
66b5552f 1524 netpoll_poll_enable(dev);
44345724 1525 }
44345724
OP
1526}
1527
7051b88a 1528static void __dev_close(struct net_device *dev)
44345724
OP
1529{
1530 LIST_HEAD(single);
1531
5cde2829 1532 list_add(&dev->close_list, &single);
7051b88a 1533 __dev_close_many(&single);
f87e6f47 1534 list_del(&single);
44345724
OP
1535}
1536
7051b88a 1537void dev_close_many(struct list_head *head, bool unlink)
44345724
OP
1538{
1539 struct net_device *dev, *tmp;
1da177e4 1540
5cde2829
EB
1541 /* Remove the devices that don't need to be closed */
1542 list_for_each_entry_safe(dev, tmp, head, close_list)
44345724 1543 if (!(dev->flags & IFF_UP))
5cde2829 1544 list_del_init(&dev->close_list);
44345724
OP
1545
1546 __dev_close_many(head);
1da177e4 1547
5cde2829 1548 list_for_each_entry_safe(dev, tmp, head, close_list) {
7f294054 1549 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
44345724 1550 call_netdevice_notifiers(NETDEV_DOWN, dev);
99c4a26a
DM
1551 if (unlink)
1552 list_del_init(&dev->close_list);
44345724 1553 }
bd380811 1554}
99c4a26a 1555EXPORT_SYMBOL(dev_close_many);
bd380811
PM
1556
1557/**
1558 * dev_close - shutdown an interface.
1559 * @dev: device to shutdown
1560 *
1561 * This function moves an active device into down state. A
1562 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1563 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1564 * chain.
1565 */
7051b88a 1566void dev_close(struct net_device *dev)
bd380811 1567{
e14a5993
ED
1568 if (dev->flags & IFF_UP) {
1569 LIST_HEAD(single);
1da177e4 1570
5cde2829 1571 list_add(&dev->close_list, &single);
99c4a26a 1572 dev_close_many(&single, true);
e14a5993
ED
1573 list_del(&single);
1574 }
1da177e4 1575}
d1b19dff 1576EXPORT_SYMBOL(dev_close);
1da177e4
LT
1577
1578
0187bdfb
BH
1579/**
1580 * dev_disable_lro - disable Large Receive Offload on a device
1581 * @dev: device
1582 *
1583 * Disable Large Receive Offload (LRO) on a net device. Must be
1584 * called under RTNL. This is needed if received packets may be
1585 * forwarded to another interface.
1586 */
1587void dev_disable_lro(struct net_device *dev)
1588{
fbe168ba
MK
1589 struct net_device *lower_dev;
1590 struct list_head *iter;
529d0489 1591
bc5787c6
MM
1592 dev->wanted_features &= ~NETIF_F_LRO;
1593 netdev_update_features(dev);
27660515 1594
22d5969f
MM
1595 if (unlikely(dev->features & NETIF_F_LRO))
1596 netdev_WARN(dev, "failed to disable LRO!\n");
fbe168ba
MK
1597
1598 netdev_for_each_lower_dev(dev, lower_dev, iter)
1599 dev_disable_lro(lower_dev);
0187bdfb
BH
1600}
1601EXPORT_SYMBOL(dev_disable_lro);
1602
56f5aa77
MC
1603/**
1604 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1605 * @dev: device
1606 *
1607 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
1608 * called under RTNL. This is needed if Generic XDP is installed on
1609 * the device.
1610 */
1611static void dev_disable_gro_hw(struct net_device *dev)
1612{
1613 dev->wanted_features &= ~NETIF_F_GRO_HW;
1614 netdev_update_features(dev);
1615
1616 if (unlikely(dev->features & NETIF_F_GRO_HW))
1617 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1618}
1619
ede2762d
KT
1620const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1621{
1622#define N(val) \
1623 case NETDEV_##val: \
1624 return "NETDEV_" __stringify(val);
1625 switch (cmd) {
1626 N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1627 N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1628 N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1629 N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
1630 N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
1631 N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
1632 N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
9daae9bd
GP
1633 N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1634 N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
1570415f 1635 N(PRE_CHANGEADDR)
3f5ecd8a 1636 }
ede2762d
KT
1637#undef N
1638 return "UNKNOWN_NETDEV_EVENT";
1639}
1640EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1641
351638e7
JP
1642static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1643 struct net_device *dev)
1644{
51d0c047
DA
1645 struct netdev_notifier_info info = {
1646 .dev = dev,
1647 };
351638e7 1648
351638e7
JP
1649 return nb->notifier_call(nb, val, &info);
1650}
0187bdfb 1651
afa0df59
JP
1652static int call_netdevice_register_notifiers(struct notifier_block *nb,
1653 struct net_device *dev)
1654{
1655 int err;
1656
1657 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
1658 err = notifier_to_errno(err);
1659 if (err)
1660 return err;
1661
1662 if (!(dev->flags & IFF_UP))
1663 return 0;
1664
1665 call_netdevice_notifier(nb, NETDEV_UP, dev);
1666 return 0;
1667}
1668
1669static void call_netdevice_unregister_notifiers(struct notifier_block *nb,
1670 struct net_device *dev)
1671{
1672 if (dev->flags & IFF_UP) {
1673 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1674 dev);
1675 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1676 }
1677 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1678}
1679
1680static int call_netdevice_register_net_notifiers(struct notifier_block *nb,
1681 struct net *net)
1682{
1683 struct net_device *dev;
1684 int err;
1685
1686 for_each_netdev(net, dev) {
1687 err = call_netdevice_register_notifiers(nb, dev);
1688 if (err)
1689 goto rollback;
1690 }
1691 return 0;
1692
1693rollback:
1694 for_each_netdev_continue_reverse(net, dev)
1695 call_netdevice_unregister_notifiers(nb, dev);
1696 return err;
1697}
1698
1699static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb,
1700 struct net *net)
1701{
1702 struct net_device *dev;
1703
1704 for_each_netdev(net, dev)
1705 call_netdevice_unregister_notifiers(nb, dev);
1706}
1707
881d966b
EB
1708static int dev_boot_phase = 1;
1709
1da177e4 1710/**
722c9a0c 1711 * register_netdevice_notifier - register a network notifier block
1712 * @nb: notifier
1da177e4 1713 *
722c9a0c 1714 * Register a notifier to be called when network device events occur.
1715 * The notifier passed is linked into the kernel structures and must
1716 * not be reused until it has been unregistered. A negative errno code
1717 * is returned on a failure.
1da177e4 1718 *
722c9a0c 1719 * When registered all registration and up events are replayed
1720 * to the new notifier to allow device to have a race free
1721 * view of the network device list.
1da177e4
LT
1722 */
1723
1724int register_netdevice_notifier(struct notifier_block *nb)
1725{
881d966b 1726 struct net *net;
1da177e4
LT
1727 int err;
1728
328fbe74
KT
1729 /* Close race with setup_net() and cleanup_net() */
1730 down_write(&pernet_ops_rwsem);
1da177e4 1731 rtnl_lock();
f07d5b94 1732 err = raw_notifier_chain_register(&netdev_chain, nb);
fcc5a03a
HX
1733 if (err)
1734 goto unlock;
881d966b
EB
1735 if (dev_boot_phase)
1736 goto unlock;
1737 for_each_net(net) {
afa0df59
JP
1738 err = call_netdevice_register_net_notifiers(nb, net);
1739 if (err)
1740 goto rollback;
1da177e4 1741 }
fcc5a03a
HX
1742
1743unlock:
1da177e4 1744 rtnl_unlock();
328fbe74 1745 up_write(&pernet_ops_rwsem);
1da177e4 1746 return err;
fcc5a03a
HX
1747
1748rollback:
afa0df59
JP
1749 for_each_net_continue_reverse(net)
1750 call_netdevice_unregister_net_notifiers(nb, net);
c67625a1
PE
1751
1752 raw_notifier_chain_unregister(&netdev_chain, nb);
fcc5a03a 1753 goto unlock;
1da177e4 1754}
d1b19dff 1755EXPORT_SYMBOL(register_netdevice_notifier);
1da177e4
LT
1756
1757/**
722c9a0c 1758 * unregister_netdevice_notifier - unregister a network notifier block
1759 * @nb: notifier
1da177e4 1760 *
722c9a0c 1761 * Unregister a notifier previously registered by
1762 * register_netdevice_notifier(). The notifier is unlinked into the
1763 * kernel structures and may then be reused. A negative errno code
1764 * is returned on a failure.
7d3d43da 1765 *
722c9a0c 1766 * After unregistering unregister and down device events are synthesized
1767 * for all devices on the device list to the removed notifier to remove
1768 * the need for special case cleanup code.
1da177e4
LT
1769 */
1770
1771int unregister_netdevice_notifier(struct notifier_block *nb)
1772{
7d3d43da 1773 struct net *net;
9f514950
HX
1774 int err;
1775
328fbe74
KT
1776 /* Close race with setup_net() and cleanup_net() */
1777 down_write(&pernet_ops_rwsem);
9f514950 1778 rtnl_lock();
f07d5b94 1779 err = raw_notifier_chain_unregister(&netdev_chain, nb);
7d3d43da
EB
1780 if (err)
1781 goto unlock;
1782
48b3a137
JP
1783 for_each_net(net)
1784 call_netdevice_unregister_net_notifiers(nb, net);
1785
7d3d43da 1786unlock:
9f514950 1787 rtnl_unlock();
328fbe74 1788 up_write(&pernet_ops_rwsem);
9f514950 1789 return err;
1da177e4 1790}
d1b19dff 1791EXPORT_SYMBOL(unregister_netdevice_notifier);
1da177e4 1792
1f637703
JP
1793static int __register_netdevice_notifier_net(struct net *net,
1794 struct notifier_block *nb,
1795 bool ignore_call_fail)
1796{
1797 int err;
1798
1799 err = raw_notifier_chain_register(&net->netdev_chain, nb);
1800 if (err)
1801 return err;
1802 if (dev_boot_phase)
1803 return 0;
1804
1805 err = call_netdevice_register_net_notifiers(nb, net);
1806 if (err && !ignore_call_fail)
1807 goto chain_unregister;
1808
1809 return 0;
1810
1811chain_unregister:
1812 raw_notifier_chain_unregister(&net->netdev_chain, nb);
1813 return err;
1814}
1815
1816static int __unregister_netdevice_notifier_net(struct net *net,
1817 struct notifier_block *nb)
1818{
1819 int err;
1820
1821 err = raw_notifier_chain_unregister(&net->netdev_chain, nb);
1822 if (err)
1823 return err;
1824
1825 call_netdevice_unregister_net_notifiers(nb, net);
1826 return 0;
1827}
1828
a30c7b42
JP
1829/**
1830 * register_netdevice_notifier_net - register a per-netns network notifier block
1831 * @net: network namespace
1832 * @nb: notifier
1833 *
1834 * Register a notifier to be called when network device events occur.
1835 * The notifier passed is linked into the kernel structures and must
1836 * not be reused until it has been unregistered. A negative errno code
1837 * is returned on a failure.
1838 *
1839 * When registered all registration and up events are replayed
1840 * to the new notifier to allow device to have a race free
1841 * view of the network device list.
1842 */
1843
1844int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb)
1845{
1846 int err;
1847
1848 rtnl_lock();
1f637703 1849 err = __register_netdevice_notifier_net(net, nb, false);
a30c7b42
JP
1850 rtnl_unlock();
1851 return err;
a30c7b42
JP
1852}
1853EXPORT_SYMBOL(register_netdevice_notifier_net);
1854
1855/**
1856 * unregister_netdevice_notifier_net - unregister a per-netns
1857 * network notifier block
1858 * @net: network namespace
1859 * @nb: notifier
1860 *
1861 * Unregister a notifier previously registered by
1862 * register_netdevice_notifier(). The notifier is unlinked into the
1863 * kernel structures and may then be reused. A negative errno code
1864 * is returned on a failure.
1865 *
1866 * After unregistering unregister and down device events are synthesized
1867 * for all devices on the device list to the removed notifier to remove
1868 * the need for special case cleanup code.
1869 */
1870
1871int unregister_netdevice_notifier_net(struct net *net,
1872 struct notifier_block *nb)
1873{
1874 int err;
1875
1876 rtnl_lock();
1f637703 1877 err = __unregister_netdevice_notifier_net(net, nb);
a30c7b42
JP
1878 rtnl_unlock();
1879 return err;
1880}
1881EXPORT_SYMBOL(unregister_netdevice_notifier_net);
a30c7b42 1882
93642e14
JP
1883int register_netdevice_notifier_dev_net(struct net_device *dev,
1884 struct notifier_block *nb,
1885 struct netdev_net_notifier *nn)
1886{
1887 int err;
a30c7b42 1888
93642e14
JP
1889 rtnl_lock();
1890 err = __register_netdevice_notifier_net(dev_net(dev), nb, false);
1891 if (!err) {
1892 nn->nb = nb;
1893 list_add(&nn->list, &dev->net_notifier_list);
1894 }
a30c7b42
JP
1895 rtnl_unlock();
1896 return err;
1897}
93642e14
JP
1898EXPORT_SYMBOL(register_netdevice_notifier_dev_net);
1899
1900int unregister_netdevice_notifier_dev_net(struct net_device *dev,
1901 struct notifier_block *nb,
1902 struct netdev_net_notifier *nn)
1903{
1904 int err;
1905
1906 rtnl_lock();
1907 list_del(&nn->list);
1908 err = __unregister_netdevice_notifier_net(dev_net(dev), nb);
1909 rtnl_unlock();
1910 return err;
1911}
1912EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net);
1913
1914static void move_netdevice_notifiers_dev_net(struct net_device *dev,
1915 struct net *net)
1916{
1917 struct netdev_net_notifier *nn;
1918
1919 list_for_each_entry(nn, &dev->net_notifier_list, list) {
1920 __unregister_netdevice_notifier_net(dev_net(dev), nn->nb);
1921 __register_netdevice_notifier_net(net, nn->nb, true);
1922 }
1923}
a30c7b42 1924
351638e7
JP
1925/**
1926 * call_netdevice_notifiers_info - call all network notifier blocks
1927 * @val: value passed unmodified to notifier function
351638e7
JP
1928 * @info: notifier information data
1929 *
1930 * Call all network notifier blocks. Parameters and return value
1931 * are as for raw_notifier_call_chain().
1932 */
1933
1d143d9f 1934static int call_netdevice_notifiers_info(unsigned long val,
1d143d9f 1935 struct netdev_notifier_info *info)
351638e7 1936{
a30c7b42
JP
1937 struct net *net = dev_net(info->dev);
1938 int ret;
1939
351638e7 1940 ASSERT_RTNL();
a30c7b42
JP
1941
1942 /* Run per-netns notifier block chain first, then run the global one.
1943 * Hopefully, one day, the global one is going to be removed after
1944 * all notifier block registrators get converted to be per-netns.
1945 */
1946 ret = raw_notifier_call_chain(&net->netdev_chain, val, info);
1947 if (ret & NOTIFY_STOP_MASK)
1948 return ret;
351638e7
JP
1949 return raw_notifier_call_chain(&netdev_chain, val, info);
1950}
351638e7 1951
26372605
PM
1952static int call_netdevice_notifiers_extack(unsigned long val,
1953 struct net_device *dev,
1954 struct netlink_ext_ack *extack)
1955{
1956 struct netdev_notifier_info info = {
1957 .dev = dev,
1958 .extack = extack,
1959 };
1960
1961 return call_netdevice_notifiers_info(val, &info);
1962}
1963
1da177e4
LT
1964/**
1965 * call_netdevice_notifiers - call all network notifier blocks
1966 * @val: value passed unmodified to notifier function
c4ea43c5 1967 * @dev: net_device pointer passed unmodified to notifier function
1da177e4
LT
1968 *
1969 * Call all network notifier blocks. Parameters and return value
f07d5b94 1970 * are as for raw_notifier_call_chain().
1da177e4
LT
1971 */
1972
ad7379d4 1973int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1da177e4 1974{
26372605 1975 return call_netdevice_notifiers_extack(val, dev, NULL);
1da177e4 1976}
edf947f1 1977EXPORT_SYMBOL(call_netdevice_notifiers);
1da177e4 1978
af7d6cce
SD
1979/**
1980 * call_netdevice_notifiers_mtu - call all network notifier blocks
1981 * @val: value passed unmodified to notifier function
1982 * @dev: net_device pointer passed unmodified to notifier function
1983 * @arg: additional u32 argument passed to the notifier function
1984 *
1985 * Call all network notifier blocks. Parameters and return value
1986 * are as for raw_notifier_call_chain().
1987 */
1988static int call_netdevice_notifiers_mtu(unsigned long val,
1989 struct net_device *dev, u32 arg)
1990{
1991 struct netdev_notifier_info_ext info = {
1992 .info.dev = dev,
1993 .ext.mtu = arg,
1994 };
1995
1996 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
1997
1998 return call_netdevice_notifiers_info(val, &info.info);
1999}
2000
1cf51900 2001#ifdef CONFIG_NET_INGRESS
aabf6772 2002static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
4577139b
DB
2003
2004void net_inc_ingress_queue(void)
2005{
aabf6772 2006 static_branch_inc(&ingress_needed_key);
4577139b
DB
2007}
2008EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
2009
2010void net_dec_ingress_queue(void)
2011{
aabf6772 2012 static_branch_dec(&ingress_needed_key);
4577139b
DB
2013}
2014EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
2015#endif
2016
1f211a1b 2017#ifdef CONFIG_NET_EGRESS
aabf6772 2018static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
1f211a1b
DB
2019
2020void net_inc_egress_queue(void)
2021{
aabf6772 2022 static_branch_inc(&egress_needed_key);
1f211a1b
DB
2023}
2024EXPORT_SYMBOL_GPL(net_inc_egress_queue);
2025
2026void net_dec_egress_queue(void)
2027{
aabf6772 2028 static_branch_dec(&egress_needed_key);
1f211a1b
DB
2029}
2030EXPORT_SYMBOL_GPL(net_dec_egress_queue);
2031#endif
2032
39e83922 2033static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
e9666d10 2034#ifdef CONFIG_JUMP_LABEL
b90e5794 2035static atomic_t netstamp_needed_deferred;
13baa00a 2036static atomic_t netstamp_wanted;
5fa8bbda 2037static void netstamp_clear(struct work_struct *work)
1da177e4 2038{
b90e5794 2039 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
13baa00a 2040 int wanted;
b90e5794 2041
13baa00a
ED
2042 wanted = atomic_add_return(deferred, &netstamp_wanted);
2043 if (wanted > 0)
39e83922 2044 static_branch_enable(&netstamp_needed_key);
13baa00a 2045 else
39e83922 2046 static_branch_disable(&netstamp_needed_key);
5fa8bbda
ED
2047}
2048static DECLARE_WORK(netstamp_work, netstamp_clear);
b90e5794 2049#endif
5fa8bbda
ED
2050
2051void net_enable_timestamp(void)
2052{
e9666d10 2053#ifdef CONFIG_JUMP_LABEL
13baa00a
ED
2054 int wanted;
2055
2056 while (1) {
2057 wanted = atomic_read(&netstamp_wanted);
2058 if (wanted <= 0)
2059 break;
2060 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
2061 return;
2062 }
2063 atomic_inc(&netstamp_needed_deferred);
2064 schedule_work(&netstamp_work);
2065#else
39e83922 2066 static_branch_inc(&netstamp_needed_key);
13baa00a 2067#endif
1da177e4 2068}
d1b19dff 2069EXPORT_SYMBOL(net_enable_timestamp);
1da177e4
LT
2070
2071void net_disable_timestamp(void)
2072{
e9666d10 2073#ifdef CONFIG_JUMP_LABEL
13baa00a
ED
2074 int wanted;
2075
2076 while (1) {
2077 wanted = atomic_read(&netstamp_wanted);
2078 if (wanted <= 1)
2079 break;
2080 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
2081 return;
2082 }
2083 atomic_dec(&netstamp_needed_deferred);
5fa8bbda
ED
2084 schedule_work(&netstamp_work);
2085#else
39e83922 2086 static_branch_dec(&netstamp_needed_key);
5fa8bbda 2087#endif
1da177e4 2088}
d1b19dff 2089EXPORT_SYMBOL(net_disable_timestamp);
1da177e4 2090
3b098e2d 2091static inline void net_timestamp_set(struct sk_buff *skb)
1da177e4 2092{
2456e855 2093 skb->tstamp = 0;
39e83922 2094 if (static_branch_unlikely(&netstamp_needed_key))
a61bbcf2 2095 __net_timestamp(skb);
1da177e4
LT
2096}
2097
39e83922
DB
2098#define net_timestamp_check(COND, SKB) \
2099 if (static_branch_unlikely(&netstamp_needed_key)) { \
2100 if ((COND) && !(SKB)->tstamp) \
2101 __net_timestamp(SKB); \
2102 } \
3b098e2d 2103
f4b05d27 2104bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
79b569f0
DL
2105{
2106 unsigned int len;
2107
2108 if (!(dev->flags & IFF_UP))
2109 return false;
2110
2111 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
2112 if (skb->len <= len)
2113 return true;
2114
2115 /* if TSO is enabled, we don't care about the length as the packet
2116 * could be forwarded without being segmented before
2117 */
2118 if (skb_is_gso(skb))
2119 return true;
2120
2121 return false;
2122}
1ee481fb 2123EXPORT_SYMBOL_GPL(is_skb_forwardable);
79b569f0 2124
a0265d28
HX
2125int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2126{
4e3264d2 2127 int ret = ____dev_forward_skb(dev, skb);
a0265d28 2128
4e3264d2
MKL
2129 if (likely(!ret)) {
2130 skb->protocol = eth_type_trans(skb, dev);
2131 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
2132 }
a0265d28 2133
4e3264d2 2134 return ret;
a0265d28
HX
2135}
2136EXPORT_SYMBOL_GPL(__dev_forward_skb);
2137
44540960
AB
2138/**
2139 * dev_forward_skb - loopback an skb to another netif
2140 *
2141 * @dev: destination network device
2142 * @skb: buffer to forward
2143 *
2144 * return values:
2145 * NET_RX_SUCCESS (no congestion)
6ec82562 2146 * NET_RX_DROP (packet was dropped, but freed)
44540960
AB
2147 *
2148 * dev_forward_skb can be used for injecting an skb from the
2149 * start_xmit function of one device into the receive queue
2150 * of another device.
2151 *
2152 * The receiving device may be in another namespace, so
2153 * we have to clear all information in the skb that could
2154 * impact namespace isolation.
2155 */
2156int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2157{
a0265d28 2158 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
44540960
AB
2159}
2160EXPORT_SYMBOL_GPL(dev_forward_skb);
2161
71d9dec2
CG
2162static inline int deliver_skb(struct sk_buff *skb,
2163 struct packet_type *pt_prev,
2164 struct net_device *orig_dev)
2165{
1f8b977a 2166 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
1080e512 2167 return -ENOMEM;
63354797 2168 refcount_inc(&skb->users);
71d9dec2
CG
2169 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2170}
2171
7866a621
SN
2172static inline void deliver_ptype_list_skb(struct sk_buff *skb,
2173 struct packet_type **pt,
fbcb2170
JP
2174 struct net_device *orig_dev,
2175 __be16 type,
7866a621
SN
2176 struct list_head *ptype_list)
2177{
2178 struct packet_type *ptype, *pt_prev = *pt;
2179
2180 list_for_each_entry_rcu(ptype, ptype_list, list) {
2181 if (ptype->type != type)
2182 continue;
2183 if (pt_prev)
fbcb2170 2184 deliver_skb(skb, pt_prev, orig_dev);
7866a621
SN
2185 pt_prev = ptype;
2186 }
2187 *pt = pt_prev;
2188}
2189
c0de08d0
EL
2190static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
2191{
a3d744e9 2192 if (!ptype->af_packet_priv || !skb->sk)
c0de08d0
EL
2193 return false;
2194
2195 if (ptype->id_match)
2196 return ptype->id_match(ptype, skb->sk);
2197 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
2198 return true;
2199
2200 return false;
2201}
2202
9f9a742d
MR
2203/**
2204 * dev_nit_active - return true if any network interface taps are in use
2205 *
2206 * @dev: network device to check for the presence of taps
2207 */
2208bool dev_nit_active(struct net_device *dev)
2209{
2210 return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all);
2211}
2212EXPORT_SYMBOL_GPL(dev_nit_active);
2213
1da177e4
LT
2214/*
2215 * Support routine. Sends outgoing frames to any network
2216 * taps currently in use.
2217 */
2218
74b20582 2219void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1da177e4
LT
2220{
2221 struct packet_type *ptype;
71d9dec2
CG
2222 struct sk_buff *skb2 = NULL;
2223 struct packet_type *pt_prev = NULL;
7866a621 2224 struct list_head *ptype_list = &ptype_all;
a61bbcf2 2225
1da177e4 2226 rcu_read_lock();
7866a621
SN
2227again:
2228 list_for_each_entry_rcu(ptype, ptype_list, list) {
fa788d98
VW
2229 if (ptype->ignore_outgoing)
2230 continue;
2231
1da177e4
LT
2232 /* Never send packets back to the socket
2233 * they originated from - MvS (miquels@drinkel.ow.org)
2234 */
7866a621
SN
2235 if (skb_loop_sk(ptype, skb))
2236 continue;
71d9dec2 2237
7866a621
SN
2238 if (pt_prev) {
2239 deliver_skb(skb2, pt_prev, skb->dev);
2240 pt_prev = ptype;
2241 continue;
2242 }
1da177e4 2243
7866a621
SN
2244 /* need to clone skb, done only once */
2245 skb2 = skb_clone(skb, GFP_ATOMIC);
2246 if (!skb2)
2247 goto out_unlock;
70978182 2248
7866a621 2249 net_timestamp_set(skb2);
1da177e4 2250
7866a621
SN
2251 /* skb->nh should be correctly
2252 * set by sender, so that the second statement is
2253 * just protection against buggy protocols.
2254 */
2255 skb_reset_mac_header(skb2);
2256
2257 if (skb_network_header(skb2) < skb2->data ||
2258 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2259 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2260 ntohs(skb2->protocol),
2261 dev->name);
2262 skb_reset_network_header(skb2);
1da177e4 2263 }
7866a621
SN
2264
2265 skb2->transport_header = skb2->network_header;
2266 skb2->pkt_type = PACKET_OUTGOING;
2267 pt_prev = ptype;
2268 }
2269
2270 if (ptype_list == &ptype_all) {
2271 ptype_list = &dev->ptype_all;
2272 goto again;
1da177e4 2273 }
7866a621 2274out_unlock:
581fe0ea
WB
2275 if (pt_prev) {
2276 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2277 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2278 else
2279 kfree_skb(skb2);
2280 }
1da177e4
LT
2281 rcu_read_unlock();
2282}
74b20582 2283EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
1da177e4 2284
2c53040f
BH
2285/**
2286 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
4f57c087
JF
2287 * @dev: Network device
2288 * @txq: number of queues available
2289 *
2290 * If real_num_tx_queues is changed the tc mappings may no longer be
2291 * valid. To resolve this verify the tc mapping remains valid and if
2292 * not NULL the mapping. With no priorities mapping to this
2293 * offset/count pair it will no longer be used. In the worst case TC0
2294 * is invalid nothing can be done so disable priority mappings. If is
2295 * expected that drivers will fix this mapping if they can before
2296 * calling netif_set_real_num_tx_queues.
2297 */
bb134d22 2298static void netif_setup_tc(struct net_device *dev, unsigned int txq)
4f57c087
JF
2299{
2300 int i;
2301 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2302
2303 /* If TC0 is invalidated disable TC mapping */
2304 if (tc->offset + tc->count > txq) {
7b6cd1ce 2305 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
4f57c087
JF
2306 dev->num_tc = 0;
2307 return;
2308 }
2309
2310 /* Invalidated prio to tc mappings set to TC0 */
2311 for (i = 1; i < TC_BITMASK + 1; i++) {
2312 int q = netdev_get_prio_tc_map(dev, i);
2313
2314 tc = &dev->tc_to_txq[q];
2315 if (tc->offset + tc->count > txq) {
7b6cd1ce
JP
2316 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2317 i, q);
4f57c087
JF
2318 netdev_set_prio_tc_map(dev, i, 0);
2319 }
2320 }
2321}
2322
8d059b0f
AD
2323int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2324{
2325 if (dev->num_tc) {
2326 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2327 int i;
2328
ffcfe25b 2329 /* walk through the TCs and see if it falls into any of them */
8d059b0f
AD
2330 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2331 if ((txq - tc->offset) < tc->count)
2332 return i;
2333 }
2334
ffcfe25b 2335 /* didn't find it, just return -1 to indicate no match */
8d059b0f
AD
2336 return -1;
2337 }
2338
2339 return 0;
2340}
8a5f2166 2341EXPORT_SYMBOL(netdev_txq_to_tc);
8d059b0f 2342
537c00de 2343#ifdef CONFIG_XPS
04157469
AN
2344struct static_key xps_needed __read_mostly;
2345EXPORT_SYMBOL(xps_needed);
2346struct static_key xps_rxqs_needed __read_mostly;
2347EXPORT_SYMBOL(xps_rxqs_needed);
537c00de
AD
2348static DEFINE_MUTEX(xps_map_mutex);
2349#define xmap_dereference(P) \
2350 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2351
6234f874
AD
2352static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2353 int tci, u16 index)
537c00de 2354{
10cdc3f3
AD
2355 struct xps_map *map = NULL;
2356 int pos;
537c00de 2357
10cdc3f3 2358 if (dev_maps)
80d19669 2359 map = xmap_dereference(dev_maps->attr_map[tci]);
6234f874
AD
2360 if (!map)
2361 return false;
537c00de 2362
6234f874
AD
2363 for (pos = map->len; pos--;) {
2364 if (map->queues[pos] != index)
2365 continue;
2366
2367 if (map->len > 1) {
2368 map->queues[pos] = map->queues[--map->len];
10cdc3f3 2369 break;
537c00de 2370 }
6234f874 2371
80d19669 2372 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
6234f874
AD
2373 kfree_rcu(map, rcu);
2374 return false;
537c00de
AD
2375 }
2376
6234f874 2377 return true;
10cdc3f3
AD
2378}
2379
6234f874
AD
2380static bool remove_xps_queue_cpu(struct net_device *dev,
2381 struct xps_dev_maps *dev_maps,
2382 int cpu, u16 offset, u16 count)
2383{
184c449f
AD
2384 int num_tc = dev->num_tc ? : 1;
2385 bool active = false;
2386 int tci;
6234f874 2387
184c449f
AD
2388 for (tci = cpu * num_tc; num_tc--; tci++) {
2389 int i, j;
2390
2391 for (i = count, j = offset; i--; j++) {
6358d49a 2392 if (!remove_xps_queue(dev_maps, tci, j))
184c449f
AD
2393 break;
2394 }
2395
2396 active |= i < 0;
6234f874
AD
2397 }
2398
184c449f 2399 return active;
6234f874
AD
2400}
2401
867d0ad4
SD
2402static void reset_xps_maps(struct net_device *dev,
2403 struct xps_dev_maps *dev_maps,
2404 bool is_rxqs_map)
2405{
2406 if (is_rxqs_map) {
2407 static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
2408 RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
2409 } else {
2410 RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
2411 }
2412 static_key_slow_dec_cpuslocked(&xps_needed);
2413 kfree_rcu(dev_maps, rcu);
2414}
2415
80d19669
AN
2416static void clean_xps_maps(struct net_device *dev, const unsigned long *mask,
2417 struct xps_dev_maps *dev_maps, unsigned int nr_ids,
2418 u16 offset, u16 count, bool is_rxqs_map)
2419{
2420 bool active = false;
2421 int i, j;
2422
2423 for (j = -1; j = netif_attrmask_next(j, mask, nr_ids),
2424 j < nr_ids;)
2425 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset,
2426 count);
867d0ad4
SD
2427 if (!active)
2428 reset_xps_maps(dev, dev_maps, is_rxqs_map);
80d19669 2429
f28c020f
SD
2430 if (!is_rxqs_map) {
2431 for (i = offset + (count - 1); count--; i--) {
2432 netdev_queue_numa_node_write(
2433 netdev_get_tx_queue(dev, i),
2434 NUMA_NO_NODE);
80d19669 2435 }
80d19669
AN
2436 }
2437}
2438
6234f874
AD
2439static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2440 u16 count)
10cdc3f3 2441{
80d19669 2442 const unsigned long *possible_mask = NULL;
10cdc3f3 2443 struct xps_dev_maps *dev_maps;
80d19669 2444 unsigned int nr_ids;
10cdc3f3 2445
04157469
AN
2446 if (!static_key_false(&xps_needed))
2447 return;
10cdc3f3 2448
4d99f660 2449 cpus_read_lock();
04157469 2450 mutex_lock(&xps_map_mutex);
10cdc3f3 2451
04157469
AN
2452 if (static_key_false(&xps_rxqs_needed)) {
2453 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2454 if (dev_maps) {
2455 nr_ids = dev->num_rx_queues;
2456 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids,
2457 offset, count, true);
2458 }
537c00de
AD
2459 }
2460
80d19669
AN
2461 dev_maps = xmap_dereference(dev->xps_cpus_map);
2462 if (!dev_maps)
2463 goto out_no_maps;
2464
2465 if (num_possible_cpus() > 1)
2466 possible_mask = cpumask_bits(cpu_possible_mask);
2467 nr_ids = nr_cpu_ids;
2468 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count,
2469 false);
024e9679 2470
537c00de
AD
2471out_no_maps:
2472 mutex_unlock(&xps_map_mutex);
4d99f660 2473 cpus_read_unlock();
537c00de
AD
2474}
2475
6234f874
AD
2476static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2477{
2478 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2479}
2480
80d19669
AN
2481static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2482 u16 index, bool is_rxqs_map)
01c5f864
AD
2483{
2484 struct xps_map *new_map;
2485 int alloc_len = XPS_MIN_MAP_ALLOC;
2486 int i, pos;
2487
2488 for (pos = 0; map && pos < map->len; pos++) {
2489 if (map->queues[pos] != index)
2490 continue;
2491 return map;
2492 }
2493
80d19669 2494 /* Need to add tx-queue to this CPU's/rx-queue's existing map */
01c5f864
AD
2495 if (map) {
2496 if (pos < map->alloc_len)
2497 return map;
2498
2499 alloc_len = map->alloc_len * 2;
2500 }
2501
80d19669
AN
2502 /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2503 * map
2504 */
2505 if (is_rxqs_map)
2506 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2507 else
2508 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2509 cpu_to_node(attr_index));
01c5f864
AD
2510 if (!new_map)
2511 return NULL;
2512
2513 for (i = 0; i < pos; i++)
2514 new_map->queues[i] = map->queues[i];
2515 new_map->alloc_len = alloc_len;
2516 new_map->len = pos;
2517
2518 return new_map;
2519}
2520
4d99f660 2521/* Must be called under cpus_read_lock */
80d19669
AN
2522int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
2523 u16 index, bool is_rxqs_map)
537c00de 2524{
80d19669 2525 const unsigned long *online_mask = NULL, *possible_mask = NULL;
01c5f864 2526 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
80d19669 2527 int i, j, tci, numa_node_id = -2;
184c449f 2528 int maps_sz, num_tc = 1, tc = 0;
537c00de 2529 struct xps_map *map, *new_map;
01c5f864 2530 bool active = false;
80d19669 2531 unsigned int nr_ids;
537c00de 2532
184c449f 2533 if (dev->num_tc) {
ffcfe25b 2534 /* Do not allow XPS on subordinate device directly */
184c449f 2535 num_tc = dev->num_tc;
ffcfe25b
AD
2536 if (num_tc < 0)
2537 return -EINVAL;
2538
2539 /* If queue belongs to subordinate dev use its map */
2540 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2541
184c449f
AD
2542 tc = netdev_txq_to_tc(dev, index);
2543 if (tc < 0)
2544 return -EINVAL;
2545 }
2546
537c00de 2547 mutex_lock(&xps_map_mutex);
80d19669
AN
2548 if (is_rxqs_map) {
2549 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
2550 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2551 nr_ids = dev->num_rx_queues;
2552 } else {
2553 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
2554 if (num_possible_cpus() > 1) {
2555 online_mask = cpumask_bits(cpu_online_mask);
2556 possible_mask = cpumask_bits(cpu_possible_mask);
2557 }
2558 dev_maps = xmap_dereference(dev->xps_cpus_map);
2559 nr_ids = nr_cpu_ids;
2560 }
537c00de 2561
80d19669
AN
2562 if (maps_sz < L1_CACHE_BYTES)
2563 maps_sz = L1_CACHE_BYTES;
537c00de 2564
01c5f864 2565 /* allocate memory for queue storage */
80d19669
AN
2566 for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2567 j < nr_ids;) {
01c5f864
AD
2568 if (!new_dev_maps)
2569 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
2bb60cb9
AD
2570 if (!new_dev_maps) {
2571 mutex_unlock(&xps_map_mutex);
01c5f864 2572 return -ENOMEM;
2bb60cb9 2573 }
01c5f864 2574
80d19669
AN
2575 tci = j * num_tc + tc;
2576 map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) :
01c5f864
AD
2577 NULL;
2578
80d19669 2579 map = expand_xps_map(map, j, index, is_rxqs_map);
01c5f864
AD
2580 if (!map)
2581 goto error;
2582
80d19669 2583 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
01c5f864
AD
2584 }
2585
2586 if (!new_dev_maps)
2587 goto out_no_new_maps;
2588
867d0ad4
SD
2589 if (!dev_maps) {
2590 /* Increment static keys at most once per type */
2591 static_key_slow_inc_cpuslocked(&xps_needed);
2592 if (is_rxqs_map)
2593 static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
2594 }
04157469 2595
80d19669
AN
2596 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2597 j < nr_ids;) {
184c449f 2598 /* copy maps belonging to foreign traffic classes */
80d19669 2599 for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) {
184c449f 2600 /* fill in the new device map from the old device map */
80d19669
AN
2601 map = xmap_dereference(dev_maps->attr_map[tci]);
2602 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
184c449f
AD
2603 }
2604
2605 /* We need to explicitly update tci as prevous loop
2606 * could break out early if dev_maps is NULL.
2607 */
80d19669 2608 tci = j * num_tc + tc;
184c449f 2609
80d19669
AN
2610 if (netif_attr_test_mask(j, mask, nr_ids) &&
2611 netif_attr_test_online(j, online_mask, nr_ids)) {
2612 /* add tx-queue to CPU/rx-queue maps */
01c5f864
AD
2613 int pos = 0;
2614
80d19669 2615 map = xmap_dereference(new_dev_maps->attr_map[tci]);
01c5f864
AD
2616 while ((pos < map->len) && (map->queues[pos] != index))
2617 pos++;
2618
2619 if (pos == map->len)
2620 map->queues[map->len++] = index;
537c00de 2621#ifdef CONFIG_NUMA
80d19669
AN
2622 if (!is_rxqs_map) {
2623 if (numa_node_id == -2)
2624 numa_node_id = cpu_to_node(j);
2625 else if (numa_node_id != cpu_to_node(j))
2626 numa_node_id = -1;
2627 }
537c00de 2628#endif
01c5f864
AD
2629 } else if (dev_maps) {
2630 /* fill in the new device map from the old device map */
80d19669
AN
2631 map = xmap_dereference(dev_maps->attr_map[tci]);
2632 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
537c00de 2633 }
01c5f864 2634
184c449f
AD
2635 /* copy maps belonging to foreign traffic classes */
2636 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
2637 /* fill in the new device map from the old device map */
80d19669
AN
2638 map = xmap_dereference(dev_maps->attr_map[tci]);
2639 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
184c449f 2640 }
537c00de
AD
2641 }
2642
80d19669
AN
2643 if (is_rxqs_map)
2644 rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps);
2645 else
2646 rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps);
01c5f864 2647
537c00de 2648 /* Cleanup old maps */
184c449f
AD
2649 if (!dev_maps)
2650 goto out_no_old_maps;
2651
80d19669
AN
2652 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2653 j < nr_ids;) {
2654 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2655 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2656 map = xmap_dereference(dev_maps->attr_map[tci]);
01c5f864
AD
2657 if (map && map != new_map)
2658 kfree_rcu(map, rcu);
2659 }
537c00de
AD
2660 }
2661
184c449f
AD
2662 kfree_rcu(dev_maps, rcu);
2663
2664out_no_old_maps:
01c5f864
AD
2665 dev_maps = new_dev_maps;
2666 active = true;
537c00de 2667
01c5f864 2668out_no_new_maps:
80d19669
AN
2669 if (!is_rxqs_map) {
2670 /* update Tx queue numa node */
2671 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2672 (numa_node_id >= 0) ?
2673 numa_node_id : NUMA_NO_NODE);
2674 }
537c00de 2675
01c5f864
AD
2676 if (!dev_maps)
2677 goto out_no_maps;
2678
80d19669
AN
2679 /* removes tx-queue from unused CPUs/rx-queues */
2680 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2681 j < nr_ids;) {
2682 for (i = tc, tci = j * num_tc; i--; tci++)
184c449f 2683 active |= remove_xps_queue(dev_maps, tci, index);
80d19669
AN
2684 if (!netif_attr_test_mask(j, mask, nr_ids) ||
2685 !netif_attr_test_online(j, online_mask, nr_ids))
184c449f
AD
2686 active |= remove_xps_queue(dev_maps, tci, index);
2687 for (i = num_tc - tc, tci++; --i; tci++)
2688 active |= remove_xps_queue(dev_maps, tci, index);
01c5f864
AD
2689 }
2690
2691 /* free map if not active */
867d0ad4
SD
2692 if (!active)
2693 reset_xps_maps(dev, dev_maps, is_rxqs_map);
01c5f864
AD
2694
2695out_no_maps:
537c00de
AD
2696 mutex_unlock(&xps_map_mutex);
2697
2698 return 0;
2699error:
01c5f864 2700 /* remove any maps that we added */
80d19669
AN
2701 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2702 j < nr_ids;) {
2703 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2704 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
184c449f 2705 map = dev_maps ?
80d19669 2706 xmap_dereference(dev_maps->attr_map[tci]) :
184c449f
AD
2707 NULL;
2708 if (new_map && new_map != map)
2709 kfree(new_map);
2710 }
01c5f864
AD
2711 }
2712
537c00de
AD
2713 mutex_unlock(&xps_map_mutex);
2714
537c00de
AD
2715 kfree(new_dev_maps);
2716 return -ENOMEM;
2717}
4d99f660 2718EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
80d19669
AN
2719
2720int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2721 u16 index)
2722{
4d99f660
AV
2723 int ret;
2724
2725 cpus_read_lock();
2726 ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
2727 cpus_read_unlock();
2728
2729 return ret;
80d19669 2730}
537c00de
AD
2731EXPORT_SYMBOL(netif_set_xps_queue);
2732
2733#endif
ffcfe25b
AD
2734static void netdev_unbind_all_sb_channels(struct net_device *dev)
2735{
2736 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2737
2738 /* Unbind any subordinate channels */
2739 while (txq-- != &dev->_tx[0]) {
2740 if (txq->sb_dev)
2741 netdev_unbind_sb_channel(dev, txq->sb_dev);
2742 }
2743}
2744
9cf1f6a8
AD
2745void netdev_reset_tc(struct net_device *dev)
2746{
6234f874
AD
2747#ifdef CONFIG_XPS
2748 netif_reset_xps_queues_gt(dev, 0);
2749#endif
ffcfe25b
AD
2750 netdev_unbind_all_sb_channels(dev);
2751
2752 /* Reset TC configuration of device */
9cf1f6a8
AD
2753 dev->num_tc = 0;
2754 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2755 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2756}
2757EXPORT_SYMBOL(netdev_reset_tc);
2758
2759int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2760{
2761 if (tc >= dev->num_tc)
2762 return -EINVAL;
2763
6234f874
AD
2764#ifdef CONFIG_XPS
2765 netif_reset_xps_queues(dev, offset, count);
2766#endif
9cf1f6a8
AD
2767 dev->tc_to_txq[tc].count = count;
2768 dev->tc_to_txq[tc].offset = offset;
2769 return 0;
2770}
2771EXPORT_SYMBOL(netdev_set_tc_queue);
2772
2773int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2774{
2775 if (num_tc > TC_MAX_QUEUE)
2776 return -EINVAL;
2777
6234f874
AD
2778#ifdef CONFIG_XPS
2779 netif_reset_xps_queues_gt(dev, 0);
2780#endif
ffcfe25b
AD
2781 netdev_unbind_all_sb_channels(dev);
2782
9cf1f6a8
AD
2783 dev->num_tc = num_tc;
2784 return 0;
2785}
2786EXPORT_SYMBOL(netdev_set_num_tc);
2787
ffcfe25b
AD
2788void netdev_unbind_sb_channel(struct net_device *dev,
2789 struct net_device *sb_dev)
2790{
2791 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2792
2793#ifdef CONFIG_XPS
2794 netif_reset_xps_queues_gt(sb_dev, 0);
2795#endif
2796 memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2797 memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2798
2799 while (txq-- != &dev->_tx[0]) {
2800 if (txq->sb_dev == sb_dev)
2801 txq->sb_dev = NULL;
2802 }
2803}
2804EXPORT_SYMBOL(netdev_unbind_sb_channel);
2805
2806int netdev_bind_sb_channel_queue(struct net_device *dev,
2807 struct net_device *sb_dev,
2808 u8 tc, u16 count, u16 offset)
2809{
2810 /* Make certain the sb_dev and dev are already configured */
2811 if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2812 return -EINVAL;
2813
2814 /* We cannot hand out queues we don't have */
2815 if ((offset + count) > dev->real_num_tx_queues)
2816 return -EINVAL;
2817
2818 /* Record the mapping */
2819 sb_dev->tc_to_txq[tc].count = count;
2820 sb_dev->tc_to_txq[tc].offset = offset;
2821
2822 /* Provide a way for Tx queue to find the tc_to_txq map or
2823 * XPS map for itself.
2824 */
2825 while (count--)
2826 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2827
2828 return 0;
2829}
2830EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2831
2832int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2833{
2834 /* Do not use a multiqueue device to represent a subordinate channel */
2835 if (netif_is_multiqueue(dev))
2836 return -ENODEV;
2837
2838 /* We allow channels 1 - 32767 to be used for subordinate channels.
2839 * Channel 0 is meant to be "native" mode and used only to represent
2840 * the main root device. We allow writing 0 to reset the device back
2841 * to normal mode after being used as a subordinate channel.
2842 */
2843 if (channel > S16_MAX)
2844 return -EINVAL;
2845
2846 dev->num_tc = -channel;
2847
2848 return 0;
2849}
2850EXPORT_SYMBOL(netdev_set_sb_channel);
2851
f0796d5c
JF
2852/*
2853 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
3a053b1a 2854 * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
f0796d5c 2855 */
e6484930 2856int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
f0796d5c 2857{
ac5b7019 2858 bool disabling;
1d24eb48
TH
2859 int rc;
2860
ac5b7019
JK
2861 disabling = txq < dev->real_num_tx_queues;
2862
e6484930
TH
2863 if (txq < 1 || txq > dev->num_tx_queues)
2864 return -EINVAL;
f0796d5c 2865
5c56580b
BH
2866 if (dev->reg_state == NETREG_REGISTERED ||
2867 dev->reg_state == NETREG_UNREGISTERING) {
e6484930
TH
2868 ASSERT_RTNL();
2869
1d24eb48
TH
2870 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2871 txq);
bf264145
TH
2872 if (rc)
2873 return rc;
2874
4f57c087
JF
2875 if (dev->num_tc)
2876 netif_setup_tc(dev, txq);
2877
ac5b7019
JK
2878 dev->real_num_tx_queues = txq;
2879
2880 if (disabling) {
2881 synchronize_net();
e6484930 2882 qdisc_reset_all_tx_gt(dev, txq);
024e9679
AD
2883#ifdef CONFIG_XPS
2884 netif_reset_xps_queues_gt(dev, txq);
2885#endif
2886 }
ac5b7019
JK
2887 } else {
2888 dev->real_num_tx_queues = txq;
f0796d5c 2889 }
e6484930 2890
e6484930 2891 return 0;
f0796d5c
JF
2892}
2893EXPORT_SYMBOL(netif_set_real_num_tx_queues);
56079431 2894
a953be53 2895#ifdef CONFIG_SYSFS
62fe0b40
BH
2896/**
2897 * netif_set_real_num_rx_queues - set actual number of RX queues used
2898 * @dev: Network device
2899 * @rxq: Actual number of RX queues
2900 *
2901 * This must be called either with the rtnl_lock held or before
2902 * registration of the net device. Returns 0 on success, or a
4e7f7951
BH
2903 * negative error code. If called before registration, it always
2904 * succeeds.
62fe0b40
BH
2905 */
2906int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2907{
2908 int rc;
2909
bd25fa7b
TH
2910 if (rxq < 1 || rxq > dev->num_rx_queues)
2911 return -EINVAL;
2912
62fe0b40
BH
2913 if (dev->reg_state == NETREG_REGISTERED) {
2914 ASSERT_RTNL();
2915
62fe0b40
BH
2916 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2917 rxq);
2918 if (rc)
2919 return rc;
62fe0b40
BH
2920 }
2921
2922 dev->real_num_rx_queues = rxq;
2923 return 0;
2924}
2925EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2926#endif
2927
2c53040f
BH
2928/**
2929 * netif_get_num_default_rss_queues - default number of RSS queues
16917b87
YM
2930 *
2931 * This routine should set an upper limit on the number of RSS queues
2932 * used by default by multiqueue devices.
2933 */
a55b138b 2934int netif_get_num_default_rss_queues(void)
16917b87 2935{
40e4e713
HS
2936 return is_kdump_kernel() ?
2937 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
16917b87
YM
2938}
2939EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2940
3bcb846c 2941static void __netif_reschedule(struct Qdisc *q)
56079431 2942{
def82a1d
JP
2943 struct softnet_data *sd;
2944 unsigned long flags;
56079431 2945
def82a1d 2946 local_irq_save(flags);
903ceff7 2947 sd = this_cpu_ptr(&softnet_data);
a9cbd588
CG
2948 q->next_sched = NULL;
2949 *sd->output_queue_tailp = q;
2950 sd->output_queue_tailp = &q->next_sched;
def82a1d
JP
2951 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2952 local_irq_restore(flags);
2953}
2954
2955void __netif_schedule(struct Qdisc *q)
2956{
2957 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2958 __netif_reschedule(q);
56079431
DV
2959}
2960EXPORT_SYMBOL(__netif_schedule);
2961
e6247027
ED
2962struct dev_kfree_skb_cb {
2963 enum skb_free_reason reason;
2964};
2965
2966static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
56079431 2967{
e6247027
ED
2968 return (struct dev_kfree_skb_cb *)skb->cb;
2969}
2970
46e5da40
JF
2971void netif_schedule_queue(struct netdev_queue *txq)
2972{
2973 rcu_read_lock();
5be5515a 2974 if (!netif_xmit_stopped(txq)) {
46e5da40
JF
2975 struct Qdisc *q = rcu_dereference(txq->qdisc);
2976
2977 __netif_schedule(q);
2978 }
2979 rcu_read_unlock();
2980}
2981EXPORT_SYMBOL(netif_schedule_queue);
2982
46e5da40
JF
2983void netif_tx_wake_queue(struct netdev_queue *dev_queue)
2984{
2985 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
2986 struct Qdisc *q;
2987
2988 rcu_read_lock();
2989 q = rcu_dereference(dev_queue->qdisc);
2990 __netif_schedule(q);
2991 rcu_read_unlock();
2992 }
2993}
2994EXPORT_SYMBOL(netif_tx_wake_queue);
2995
e6247027 2996void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
56079431 2997{
e6247027 2998 unsigned long flags;
56079431 2999
9899886d
MJ
3000 if (unlikely(!skb))
3001 return;
3002
63354797 3003 if (likely(refcount_read(&skb->users) == 1)) {
e6247027 3004 smp_rmb();
63354797
RE
3005 refcount_set(&skb->users, 0);
3006 } else if (likely(!refcount_dec_and_test(&skb->users))) {
e6247027 3007 return;
bea3348e 3008 }
e6247027
ED
3009 get_kfree_skb_cb(skb)->reason = reason;
3010 local_irq_save(flags);
3011 skb->next = __this_cpu_read(softnet_data.completion_queue);
3012 __this_cpu_write(softnet_data.completion_queue, skb);
3013 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3014 local_irq_restore(flags);
56079431 3015}
e6247027 3016EXPORT_SYMBOL(__dev_kfree_skb_irq);
56079431 3017
e6247027 3018void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
56079431
DV
3019{
3020 if (in_irq() || irqs_disabled())
e6247027 3021 __dev_kfree_skb_irq(skb, reason);
56079431
DV
3022 else
3023 dev_kfree_skb(skb);
3024}
e6247027 3025EXPORT_SYMBOL(__dev_kfree_skb_any);
56079431
DV
3026
3027
bea3348e
SH
3028/**
3029 * netif_device_detach - mark device as removed
3030 * @dev: network device
3031 *
3032 * Mark device as removed from system and therefore no longer available.
3033 */
56079431
DV
3034void netif_device_detach(struct net_device *dev)
3035{
3036 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
3037 netif_running(dev)) {
d543103a 3038 netif_tx_stop_all_queues(dev);
56079431
DV
3039 }
3040}
3041EXPORT_SYMBOL(netif_device_detach);
3042
bea3348e
SH
3043/**
3044 * netif_device_attach - mark device as attached
3045 * @dev: network device
3046 *
3047 * Mark device as attached from system and restart if needed.
3048 */
56079431
DV
3049void netif_device_attach(struct net_device *dev)
3050{
3051 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
3052 netif_running(dev)) {
d543103a 3053 netif_tx_wake_all_queues(dev);
4ec93edb 3054 __netdev_watchdog_up(dev);
56079431
DV
3055 }
3056}
3057EXPORT_SYMBOL(netif_device_attach);
3058
5605c762
JP
3059/*
3060 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
3061 * to be used as a distribution range.
3062 */
eadec877
AD
3063static u16 skb_tx_hash(const struct net_device *dev,
3064 const struct net_device *sb_dev,
3065 struct sk_buff *skb)
5605c762
JP
3066{
3067 u32 hash;
3068 u16 qoffset = 0;
1b837d48 3069 u16 qcount = dev->real_num_tx_queues;
5605c762 3070
eadec877
AD
3071 if (dev->num_tc) {
3072 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
3073
3074 qoffset = sb_dev->tc_to_txq[tc].offset;
3075 qcount = sb_dev->tc_to_txq[tc].count;
3076 }
3077
5605c762
JP
3078 if (skb_rx_queue_recorded(skb)) {
3079 hash = skb_get_rx_queue(skb);
1b837d48
AD
3080 while (unlikely(hash >= qcount))
3081 hash -= qcount;
eadec877 3082 return hash + qoffset;
5605c762
JP
3083 }
3084
3085 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
3086}
5605c762 3087
36c92474
BH
3088static void skb_warn_bad_offload(const struct sk_buff *skb)
3089{
84d15ae5 3090 static const netdev_features_t null_features;
36c92474 3091 struct net_device *dev = skb->dev;
88ad4175 3092 const char *name = "";
36c92474 3093
c846ad9b
BG
3094 if (!net_ratelimit())
3095 return;
3096
88ad4175
BM
3097 if (dev) {
3098 if (dev->dev.parent)
3099 name = dev_driver_string(dev->dev.parent);
3100 else
3101 name = netdev_name(dev);
3102 }
6413139d
WB
3103 skb_dump(KERN_WARNING, skb, false);
3104 WARN(1, "%s: caps=(%pNF, %pNF)\n",
88ad4175 3105 name, dev ? &dev->features : &null_features,
6413139d 3106 skb->sk ? &skb->sk->sk_route_caps : &null_features);
36c92474
BH
3107}
3108
1da177e4
LT
3109/*
3110 * Invalidate hardware checksum when packet is to be mangled, and
3111 * complete checksum manually on outgoing path.
3112 */
84fa7933 3113int skb_checksum_help(struct sk_buff *skb)
1da177e4 3114{
d3bc23e7 3115 __wsum csum;
663ead3b 3116 int ret = 0, offset;
1da177e4 3117
84fa7933 3118 if (skb->ip_summed == CHECKSUM_COMPLETE)
a430a43d
HX
3119 goto out_set_summed;
3120
3121 if (unlikely(skb_shinfo(skb)->gso_size)) {
36c92474
BH
3122 skb_warn_bad_offload(skb);
3123 return -EINVAL;
1da177e4
LT
3124 }
3125
cef401de
ED
3126 /* Before computing a checksum, we should make sure no frag could
3127 * be modified by an external entity : checksum could be wrong.
3128 */
3129 if (skb_has_shared_frag(skb)) {
3130 ret = __skb_linearize(skb);
3131 if (ret)
3132 goto out;
3133 }
3134
55508d60 3135 offset = skb_checksum_start_offset(skb);
a030847e
HX
3136 BUG_ON(offset >= skb_headlen(skb));
3137 csum = skb_checksum(skb, offset, skb->len - offset, 0);
3138
3139 offset += skb->csum_offset;
3140 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
3141
8211fbfa
HK
3142 ret = skb_ensure_writable(skb, offset + sizeof(__sum16));
3143 if (ret)
3144 goto out;
1da177e4 3145
4f2e4ad5 3146 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
a430a43d 3147out_set_summed:
1da177e4 3148 skb->ip_summed = CHECKSUM_NONE;
4ec93edb 3149out:
1da177e4
LT
3150 return ret;
3151}
d1b19dff 3152EXPORT_SYMBOL(skb_checksum_help);
1da177e4 3153
b72b5bf6
DC
3154int skb_crc32c_csum_help(struct sk_buff *skb)
3155{
3156 __le32 crc32c_csum;
3157 int ret = 0, offset, start;
3158
3159 if (skb->ip_summed != CHECKSUM_PARTIAL)
3160 goto out;
3161
3162 if (unlikely(skb_is_gso(skb)))
3163 goto out;
3164
3165 /* Before computing a checksum, we should make sure no frag could
3166 * be modified by an external entity : checksum could be wrong.
3167 */
3168 if (unlikely(skb_has_shared_frag(skb))) {
3169 ret = __skb_linearize(skb);
3170 if (ret)
3171 goto out;
3172 }
3173 start = skb_checksum_start_offset(skb);
3174 offset = start + offsetof(struct sctphdr, checksum);
3175 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
3176 ret = -EINVAL;
3177 goto out;
3178 }
8211fbfa
HK
3179
3180 ret = skb_ensure_writable(skb, offset + sizeof(__le32));
3181 if (ret)
3182 goto out;
3183
b72b5bf6
DC
3184 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
3185 skb->len - start, ~(__u32)0,
3186 crc32c_csum_stub));
3187 *(__le32 *)(skb->data + offset) = crc32c_csum;
3188 skb->ip_summed = CHECKSUM_NONE;
dba00306 3189 skb->csum_not_inet = 0;
b72b5bf6
DC
3190out:
3191 return ret;
3192}
3193
53d6471c 3194__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
f6a78bfc 3195{
252e3346 3196 __be16 type = skb->protocol;
f6a78bfc 3197
19acc327
PS
3198 /* Tunnel gso handlers can set protocol to ethernet. */
3199 if (type == htons(ETH_P_TEB)) {
3200 struct ethhdr *eth;
3201
3202 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
3203 return 0;
3204
1dfe82eb 3205 eth = (struct ethhdr *)skb->data;
19acc327
PS
3206 type = eth->h_proto;
3207 }
3208
d4bcef3f 3209 return __vlan_get_protocol(skb, type, depth);
ec5f0615
PS
3210}
3211
3212/**
3213 * skb_mac_gso_segment - mac layer segmentation handler.
3214 * @skb: buffer to segment
3215 * @features: features for the output path (see dev->features)
3216 */
3217struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
3218 netdev_features_t features)
3219{
3220 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
3221 struct packet_offload *ptype;
53d6471c
VY
3222 int vlan_depth = skb->mac_len;
3223 __be16 type = skb_network_protocol(skb, &vlan_depth);
ec5f0615
PS
3224
3225 if (unlikely(!type))
3226 return ERR_PTR(-EINVAL);
3227
53d6471c 3228 __skb_pull(skb, vlan_depth);
f6a78bfc
HX
3229
3230 rcu_read_lock();
22061d80 3231 list_for_each_entry_rcu(ptype, &offload_base, list) {
f191a1d1 3232 if (ptype->type == type && ptype->callbacks.gso_segment) {
f191a1d1 3233 segs = ptype->callbacks.gso_segment(skb, features);
f6a78bfc
HX
3234 break;
3235 }
3236 }
3237 rcu_read_unlock();
3238
98e399f8 3239 __skb_push(skb, skb->data - skb_mac_header(skb));
576a30eb 3240
f6a78bfc
HX
3241 return segs;
3242}
05e8ef4a
PS
3243EXPORT_SYMBOL(skb_mac_gso_segment);
3244
3245
3246/* openvswitch calls this on rx path, so we need a different check.
3247 */
3248static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
3249{
3250 if (tx_path)
0c19f846
WB
3251 return skb->ip_summed != CHECKSUM_PARTIAL &&
3252 skb->ip_summed != CHECKSUM_UNNECESSARY;
6e7bc478
ED
3253
3254 return skb->ip_summed == CHECKSUM_NONE;
05e8ef4a
PS
3255}
3256
3257/**
3258 * __skb_gso_segment - Perform segmentation on skb.
3259 * @skb: buffer to segment
3260 * @features: features for the output path (see dev->features)
3261 * @tx_path: whether it is called in TX path
3262 *
3263 * This function segments the given skb and returns a list of segments.
3264 *
3265 * It may return NULL if the skb requires no segmentation. This is
3266 * only possible when GSO is used for verifying header integrity.
9207f9d4
KK
3267 *
3268 * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb.
05e8ef4a
PS
3269 */
3270struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
3271 netdev_features_t features, bool tx_path)
3272{
b2504a5d
ED
3273 struct sk_buff *segs;
3274
05e8ef4a
PS
3275 if (unlikely(skb_needs_check(skb, tx_path))) {
3276 int err;
3277
b2504a5d 3278 /* We're going to init ->check field in TCP or UDP header */
a40e0a66 3279 err = skb_cow_head(skb, 0);
3280 if (err < 0)
05e8ef4a
PS
3281 return ERR_PTR(err);
3282 }
3283
802ab55a
AD
3284 /* Only report GSO partial support if it will enable us to
3285 * support segmentation on this frame without needing additional
3286 * work.
3287 */
3288 if (features & NETIF_F_GSO_PARTIAL) {
3289 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
3290 struct net_device *dev = skb->dev;
3291
3292 partial_features |= dev->features & dev->gso_partial_features;
3293 if (!skb_gso_ok(skb, features | partial_features))
3294 features &= ~NETIF_F_GSO_PARTIAL;
3295 }
3296
9207f9d4
KK
3297 BUILD_BUG_ON(SKB_SGO_CB_OFFSET +
3298 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
3299
68c33163 3300 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
3347c960
ED
3301 SKB_GSO_CB(skb)->encap_level = 0;
3302
05e8ef4a
PS
3303 skb_reset_mac_header(skb);
3304 skb_reset_mac_len(skb);
3305
b2504a5d
ED
3306 segs = skb_mac_gso_segment(skb, features);
3307
3a1296a3 3308 if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
b2504a5d
ED
3309 skb_warn_bad_offload(skb);
3310
3311 return segs;
05e8ef4a 3312}
12b0004d 3313EXPORT_SYMBOL(__skb_gso_segment);
f6a78bfc 3314
fb286bb2
HX
3315/* Take action when hardware reception checksum errors are detected. */
3316#ifdef CONFIG_BUG
7fe50ac8 3317void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
fb286bb2
HX
3318{
3319 if (net_ratelimit()) {
7b6cd1ce 3320 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
6413139d 3321 skb_dump(KERN_ERR, skb, true);
fb286bb2
HX
3322 dump_stack();
3323 }
3324}
3325EXPORT_SYMBOL(netdev_rx_csum_fault);
3326#endif
3327
ab74cfeb 3328/* XXX: check that highmem exists at all on the given machine. */
c1e756bf 3329static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1da177e4 3330{
3d3a8533 3331#ifdef CONFIG_HIGHMEM
1da177e4 3332 int i;
f4563a75 3333
5acbbd42 3334 if (!(dev->features & NETIF_F_HIGHDMA)) {
ea2ab693
IC
3335 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3336 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
f4563a75 3337
ea2ab693 3338 if (PageHighMem(skb_frag_page(frag)))
5acbbd42 3339 return 1;
ea2ab693 3340 }
5acbbd42 3341 }
3d3a8533 3342#endif
1da177e4
LT
3343 return 0;
3344}
1da177e4 3345
3b392ddb
SH
3346/* If MPLS offload request, verify we are testing hardware MPLS features
3347 * instead of standard features for the netdev.
3348 */
d0edc7bf 3349#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
3b392ddb
SH
3350static netdev_features_t net_mpls_features(struct sk_buff *skb,
3351 netdev_features_t features,
3352 __be16 type)
3353{
25cd9ba0 3354 if (eth_p_mpls(type))
3b392ddb
SH
3355 features &= skb->dev->mpls_features;
3356
3357 return features;
3358}
3359#else
3360static netdev_features_t net_mpls_features(struct sk_buff *skb,
3361 netdev_features_t features,
3362 __be16 type)
3363{
3364 return features;
3365}
3366#endif
3367
c8f44aff 3368static netdev_features_t harmonize_features(struct sk_buff *skb,
c1e756bf 3369 netdev_features_t features)
f01a5236 3370{
53d6471c 3371 int tmp;
3b392ddb
SH
3372 __be16 type;
3373
3374 type = skb_network_protocol(skb, &tmp);
3375 features = net_mpls_features(skb, features, type);
53d6471c 3376
c0d680e5 3377 if (skb->ip_summed != CHECKSUM_NONE &&
3b392ddb 3378 !can_checksum_protocol(features, type)) {
996e8021 3379 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
f01a5236 3380 }
7be2c82c
ED
3381 if (illegal_highdma(skb->dev, skb))
3382 features &= ~NETIF_F_SG;
f01a5236
JG
3383
3384 return features;
3385}
3386
e38f3025
TM
3387netdev_features_t passthru_features_check(struct sk_buff *skb,
3388 struct net_device *dev,
3389 netdev_features_t features)
3390{
3391 return features;
3392}
3393EXPORT_SYMBOL(passthru_features_check);
3394
7ce23672 3395static netdev_features_t dflt_features_check(struct sk_buff *skb,
8cb65d00
TM
3396 struct net_device *dev,
3397 netdev_features_t features)
3398{
3399 return vlan_features_check(skb, features);
3400}
3401
cbc53e08
AD
3402static netdev_features_t gso_features_check(const struct sk_buff *skb,
3403 struct net_device *dev,
3404 netdev_features_t features)
3405{
3406 u16 gso_segs = skb_shinfo(skb)->gso_segs;
3407
3408 if (gso_segs > dev->gso_max_segs)
3409 return features & ~NETIF_F_GSO_MASK;
3410
802ab55a
AD
3411 /* Support for GSO partial features requires software
3412 * intervention before we can actually process the packets
3413 * so we need to strip support for any partial features now
3414 * and we can pull them back in after we have partially
3415 * segmented the frame.
3416 */
3417 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
3418 features &= ~dev->gso_partial_features;
3419
3420 /* Make sure to clear the IPv4 ID mangling feature if the
3421 * IPv4 header has the potential to be fragmented.
cbc53e08
AD
3422 */
3423 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
3424 struct iphdr *iph = skb->encapsulation ?
3425 inner_ip_hdr(skb) : ip_hdr(skb);
3426
3427 if (!(iph->frag_off & htons(IP_DF)))
3428 features &= ~NETIF_F_TSO_MANGLEID;
3429 }
3430
3431 return features;
3432}
3433
c1e756bf 3434netdev_features_t netif_skb_features(struct sk_buff *skb)
58e998c6 3435{
5f35227e 3436 struct net_device *dev = skb->dev;
fcbeb976 3437 netdev_features_t features = dev->features;
58e998c6 3438
cbc53e08
AD
3439 if (skb_is_gso(skb))
3440 features = gso_features_check(skb, dev, features);
30b678d8 3441
5f35227e
JG
3442 /* If encapsulation offload request, verify we are testing
3443 * hardware encapsulation features instead of standard
3444 * features for the netdev
3445 */
3446 if (skb->encapsulation)
3447 features &= dev->hw_enc_features;
3448
f5a7fb88
TM
3449 if (skb_vlan_tagged(skb))
3450 features = netdev_intersect_features(features,
3451 dev->vlan_features |
3452 NETIF_F_HW_VLAN_CTAG_TX |
3453 NETIF_F_HW_VLAN_STAG_TX);
f01a5236 3454
5f35227e
JG
3455 if (dev->netdev_ops->ndo_features_check)
3456 features &= dev->netdev_ops->ndo_features_check(skb, dev,
3457 features);
8cb65d00
TM
3458 else
3459 features &= dflt_features_check(skb, dev, features);
5f35227e 3460
c1e756bf 3461 return harmonize_features(skb, features);
58e998c6 3462}
c1e756bf 3463EXPORT_SYMBOL(netif_skb_features);
58e998c6 3464
2ea25513 3465static int xmit_one(struct sk_buff *skb, struct net_device *dev,
95f6b3dd 3466 struct netdev_queue *txq, bool more)
f6a78bfc 3467{
2ea25513
DM
3468 unsigned int len;
3469 int rc;
00829823 3470
9f9a742d 3471 if (dev_nit_active(dev))
2ea25513 3472 dev_queue_xmit_nit(skb, dev);
fc741216 3473
2ea25513
DM
3474 len = skb->len;
3475 trace_net_dev_start_xmit(skb, dev);
95f6b3dd 3476 rc = netdev_start_xmit(skb, dev, txq, more);
2ea25513 3477 trace_net_dev_xmit(skb, rc, dev, len);
adf30907 3478
2ea25513
DM
3479 return rc;
3480}
7b9c6090 3481
8dcda22a
DM
3482struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3483 struct netdev_queue *txq, int *ret)
7f2e870f
DM
3484{
3485 struct sk_buff *skb = first;
3486 int rc = NETDEV_TX_OK;
7b9c6090 3487
7f2e870f
DM
3488 while (skb) {
3489 struct sk_buff *next = skb->next;
fc70fb64 3490
a8305bff 3491 skb_mark_not_on_list(skb);
95f6b3dd 3492 rc = xmit_one(skb, dev, txq, next != NULL);
7f2e870f
DM
3493 if (unlikely(!dev_xmit_complete(rc))) {
3494 skb->next = next;
3495 goto out;
3496 }
6afff0ca 3497
7f2e870f 3498 skb = next;
fe60faa5 3499 if (netif_tx_queue_stopped(txq) && skb) {
7f2e870f
DM
3500 rc = NETDEV_TX_BUSY;
3501 break;
9ccb8975 3502 }
7f2e870f 3503 }
9ccb8975 3504
7f2e870f
DM
3505out:
3506 *ret = rc;
3507 return skb;
3508}
b40863c6 3509
1ff0dc94
ED
3510static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3511 netdev_features_t features)
f6a78bfc 3512{
df8a39de 3513 if (skb_vlan_tag_present(skb) &&
5968250c
JP
3514 !vlan_hw_offload_capable(features, skb->vlan_proto))
3515 skb = __vlan_hwaccel_push_inside(skb);
eae3f88e
DM
3516 return skb;
3517}
f6a78bfc 3518
43c26a1a
DC
3519int skb_csum_hwoffload_help(struct sk_buff *skb,
3520 const netdev_features_t features)
3521{
3522 if (unlikely(skb->csum_not_inet))
3523 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3524 skb_crc32c_csum_help(skb);
3525
3526 return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb);
3527}
3528EXPORT_SYMBOL(skb_csum_hwoffload_help);
3529
f53c7239 3530static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
eae3f88e
DM
3531{
3532 netdev_features_t features;
f6a78bfc 3533
eae3f88e
DM
3534 features = netif_skb_features(skb);
3535 skb = validate_xmit_vlan(skb, features);
3536 if (unlikely(!skb))
3537 goto out_null;
7b9c6090 3538
ebf4e808
IL
3539 skb = sk_validate_xmit_skb(skb, dev);
3540 if (unlikely(!skb))
3541 goto out_null;
3542
8b86a61d 3543 if (netif_needs_gso(skb, features)) {
ce93718f
DM
3544 struct sk_buff *segs;
3545
3546 segs = skb_gso_segment(skb, features);
cecda693 3547 if (IS_ERR(segs)) {
af6dabc9 3548 goto out_kfree_skb;
cecda693
JW
3549 } else if (segs) {
3550 consume_skb(skb);
3551 skb = segs;
f6a78bfc 3552 }
eae3f88e
DM
3553 } else {
3554 if (skb_needs_linearize(skb, features) &&
3555 __skb_linearize(skb))
3556 goto out_kfree_skb;
4ec93edb 3557
eae3f88e
DM
3558 /* If packet is not checksummed and device does not
3559 * support checksumming for this protocol, complete
3560 * checksumming here.
3561 */
3562 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3563 if (skb->encapsulation)
3564 skb_set_inner_transport_header(skb,
3565 skb_checksum_start_offset(skb));
3566 else
3567 skb_set_transport_header(skb,
3568 skb_checksum_start_offset(skb));
43c26a1a 3569 if (skb_csum_hwoffload_help(skb, features))
eae3f88e 3570 goto out_kfree_skb;
7b9c6090 3571 }
0c772159 3572 }
7b9c6090 3573
f53c7239 3574 skb = validate_xmit_xfrm(skb, features, again);
3dca3f38 3575
eae3f88e 3576 return skb;
fc70fb64 3577
f6a78bfc
HX
3578out_kfree_skb:
3579 kfree_skb(skb);
eae3f88e 3580out_null:
d21fd63e 3581 atomic_long_inc(&dev->tx_dropped);
eae3f88e
DM
3582 return NULL;
3583}
6afff0ca 3584
f53c7239 3585struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
55a93b3e
ED
3586{
3587 struct sk_buff *next, *head = NULL, *tail;
3588
bec3cfdc 3589 for (; skb != NULL; skb = next) {
55a93b3e 3590 next = skb->next;
a8305bff 3591 skb_mark_not_on_list(skb);
bec3cfdc
ED
3592
3593 /* in case skb wont be segmented, point to itself */
3594 skb->prev = skb;
3595
f53c7239 3596 skb = validate_xmit_skb(skb, dev, again);
bec3cfdc
ED
3597 if (!skb)
3598 continue;
55a93b3e 3599
bec3cfdc
ED
3600 if (!head)
3601 head = skb;
3602 else
3603 tail->next = skb;
3604 /* If skb was segmented, skb->prev points to
3605 * the last segment. If not, it still contains skb.
3606 */
3607 tail = skb->prev;
55a93b3e
ED
3608 }
3609 return head;
f6a78bfc 3610}
104ba78c 3611EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
f6a78bfc 3612
1def9238
ED
3613static void qdisc_pkt_len_init(struct sk_buff *skb)
3614{
3615 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3616
3617 qdisc_skb_cb(skb)->pkt_len = skb->len;
3618
3619 /* To get more precise estimation of bytes sent on wire,
3620 * we add to pkt_len the headers size of all segments
3621 */
a0dce875 3622 if (shinfo->gso_size && skb_transport_header_was_set(skb)) {
757b8b1d 3623 unsigned int hdr_len;
15e5a030 3624 u16 gso_segs = shinfo->gso_segs;
1def9238 3625
757b8b1d
ED
3626 /* mac layer + network layer */
3627 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3628
3629 /* + transport layer */
7c68d1a6
ED
3630 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3631 const struct tcphdr *th;
3632 struct tcphdr _tcphdr;
3633
3634 th = skb_header_pointer(skb, skb_transport_offset(skb),
3635 sizeof(_tcphdr), &_tcphdr);
3636 if (likely(th))
3637 hdr_len += __tcp_hdrlen(th);
3638 } else {
3639 struct udphdr _udphdr;
3640
3641 if (skb_header_pointer(skb, skb_transport_offset(skb),
3642 sizeof(_udphdr), &_udphdr))
3643 hdr_len += sizeof(struct udphdr);
3644 }
15e5a030
JW
3645
3646 if (shinfo->gso_type & SKB_GSO_DODGY)
3647 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3648 shinfo->gso_size);
3649
3650 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
1def9238
ED
3651 }
3652}
3653
bbd8a0d3
KK
3654static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3655 struct net_device *dev,
3656 struct netdev_queue *txq)
3657{
3658 spinlock_t *root_lock = qdisc_lock(q);
520ac30f 3659 struct sk_buff *to_free = NULL;
a2da570d 3660 bool contended;
bbd8a0d3
KK
3661 int rc;
3662
a2da570d 3663 qdisc_calculate_pkt_len(skb, q);
6b3ba914
JF
3664
3665 if (q->flags & TCQ_F_NOLOCK) {
90b2be27 3666 if ((q->flags & TCQ_F_CAN_BYPASS) && READ_ONCE(q->empty) &&
d518d2ed
PA
3667 qdisc_run_begin(q)) {
3668 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED,
3669 &q->state))) {
3670 __qdisc_drop(skb, &to_free);
3671 rc = NET_XMIT_DROP;
3672 goto end_run;
3673 }
ba27b4cd
PA
3674 qdisc_bstats_cpu_update(q, skb);
3675
d518d2ed 3676 rc = NET_XMIT_SUCCESS;
ba27b4cd
PA
3677 if (sch_direct_xmit(skb, q, dev, txq, NULL, true))
3678 __qdisc_run(q);
3679
d518d2ed 3680end_run:
ba27b4cd 3681 qdisc_run_end(q);
6b3ba914
JF
3682 } else {
3683 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
32f7b44d 3684 qdisc_run(q);
6b3ba914
JF
3685 }
3686
3687 if (unlikely(to_free))
3688 kfree_skb_list(to_free);
3689 return rc;
3690 }
3691
79640a4c
ED
3692 /*
3693 * Heuristic to force contended enqueues to serialize on a
3694 * separate lock before trying to get qdisc main lock.
f9eb8aea 3695 * This permits qdisc->running owner to get the lock more
9bf2b8c2 3696 * often and dequeue packets faster.
79640a4c 3697 */
a2da570d 3698 contended = qdisc_is_running(q);
79640a4c
ED
3699 if (unlikely(contended))
3700 spin_lock(&q->busylock);
3701
bbd8a0d3
KK
3702 spin_lock(root_lock);
3703 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
520ac30f 3704 __qdisc_drop(skb, &to_free);
bbd8a0d3
KK
3705 rc = NET_XMIT_DROP;
3706 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
bc135b23 3707 qdisc_run_begin(q)) {
bbd8a0d3
KK
3708 /*
3709 * This is a work-conserving queue; there are no old skbs
3710 * waiting to be sent out; and the qdisc is not running -
3711 * xmit the skb directly.
3712 */
bfe0d029 3713
bfe0d029
ED
3714 qdisc_bstats_update(q, skb);
3715
55a93b3e 3716 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
79640a4c
ED
3717 if (unlikely(contended)) {
3718 spin_unlock(&q->busylock);
3719 contended = false;
3720 }
bbd8a0d3 3721 __qdisc_run(q);
6c148184 3722 }
bbd8a0d3 3723
6c148184 3724 qdisc_run_end(q);
bbd8a0d3
KK
3725 rc = NET_XMIT_SUCCESS;
3726 } else {
520ac30f 3727 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
79640a4c
ED
3728 if (qdisc_run_begin(q)) {
3729 if (unlikely(contended)) {
3730 spin_unlock(&q->busylock);
3731 contended = false;
3732 }
3733 __qdisc_run(q);
6c148184 3734 qdisc_run_end(q);
79640a4c 3735 }
bbd8a0d3
KK
3736 }
3737 spin_unlock(root_lock);
520ac30f
ED
3738 if (unlikely(to_free))
3739 kfree_skb_list(to_free);
79640a4c
ED
3740 if (unlikely(contended))
3741 spin_unlock(&q->busylock);
bbd8a0d3
KK
3742 return rc;
3743}
3744
86f8515f 3745#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
5bc1421e
NH
3746static void skb_update_prio(struct sk_buff *skb)
3747{
4dcb31d4
ED
3748 const struct netprio_map *map;
3749 const struct sock *sk;
3750 unsigned int prioidx;
5bc1421e 3751
4dcb31d4
ED
3752 if (skb->priority)
3753 return;
3754 map = rcu_dereference_bh(skb->dev->priomap);
3755 if (!map)
3756 return;
3757 sk = skb_to_full_sk(skb);
3758 if (!sk)
3759 return;
91c68ce2 3760
4dcb31d4
ED
3761 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3762
3763 if (prioidx < map->priomap_len)
3764 skb->priority = map->priomap[prioidx];
5bc1421e
NH
3765}
3766#else
3767#define skb_update_prio(skb)
3768#endif
3769
95603e22
MM
3770/**
3771 * dev_loopback_xmit - loop back @skb
0c4b51f0
EB
3772 * @net: network namespace this loopback is happening in
3773 * @sk: sk needed to be a netfilter okfn
95603e22
MM
3774 * @skb: buffer to transmit
3775 */
0c4b51f0 3776int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
95603e22
MM
3777{
3778 skb_reset_mac_header(skb);
3779 __skb_pull(skb, skb_network_offset(skb));
3780 skb->pkt_type = PACKET_LOOPBACK;
3781 skb->ip_summed = CHECKSUM_UNNECESSARY;
3782 WARN_ON(!skb_dst(skb));
3783 skb_dst_force(skb);
3784 netif_rx_ni(skb);
3785 return 0;
3786}
3787EXPORT_SYMBOL(dev_loopback_xmit);
3788
1f211a1b
DB
3789#ifdef CONFIG_NET_EGRESS
3790static struct sk_buff *
3791sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3792{
46209401 3793 struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
1f211a1b
DB
3794 struct tcf_result cl_res;
3795
46209401 3796 if (!miniq)
1f211a1b
DB
3797 return skb;
3798
8dc07fdb 3799 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
46209401 3800 mini_qdisc_bstats_cpu_update(miniq, skb);
1f211a1b 3801
46209401 3802 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
1f211a1b
DB
3803 case TC_ACT_OK:
3804 case TC_ACT_RECLASSIFY:
3805 skb->tc_index = TC_H_MIN(cl_res.classid);
3806 break;
3807 case TC_ACT_SHOT:
46209401 3808 mini_qdisc_qstats_cpu_drop(miniq);
1f211a1b 3809 *ret = NET_XMIT_DROP;
7e2c3aea
DB
3810 kfree_skb(skb);
3811 return NULL;
1f211a1b
DB
3812 case TC_ACT_STOLEN:
3813 case TC_ACT_QUEUED:
e25ea21f 3814 case TC_ACT_TRAP:
1f211a1b 3815 *ret = NET_XMIT_SUCCESS;
7e2c3aea 3816 consume_skb(skb);
1f211a1b
DB
3817 return NULL;
3818 case TC_ACT_REDIRECT:
3819 /* No need to push/pop skb's mac_header here on egress! */
3820 skb_do_redirect(skb);
3821 *ret = NET_XMIT_SUCCESS;
3822 return NULL;
3823 default:
3824 break;
3825 }
3826
3827 return skb;
3828}
3829#endif /* CONFIG_NET_EGRESS */
3830
fc9bab24
AN
3831#ifdef CONFIG_XPS
3832static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
3833 struct xps_dev_maps *dev_maps, unsigned int tci)
3834{
3835 struct xps_map *map;
3836 int queue_index = -1;
3837
3838 if (dev->num_tc) {
3839 tci *= dev->num_tc;
3840 tci += netdev_get_prio_tc_map(dev, skb->priority);
3841 }
3842
3843 map = rcu_dereference(dev_maps->attr_map[tci]);
3844 if (map) {
3845 if (map->len == 1)
3846 queue_index = map->queues[0];
3847 else
3848 queue_index = map->queues[reciprocal_scale(
3849 skb_get_hash(skb), map->len)];
3850 if (unlikely(queue_index >= dev->real_num_tx_queues))
3851 queue_index = -1;
3852 }
3853 return queue_index;
3854}
3855#endif
3856
eadec877
AD
3857static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
3858 struct sk_buff *skb)
638b2a69
JP
3859{
3860#ifdef CONFIG_XPS
3861 struct xps_dev_maps *dev_maps;
fc9bab24 3862 struct sock *sk = skb->sk;
638b2a69
JP
3863 int queue_index = -1;
3864
04157469
AN
3865 if (!static_key_false(&xps_needed))
3866 return -1;
3867
638b2a69 3868 rcu_read_lock();
fc9bab24
AN
3869 if (!static_key_false(&xps_rxqs_needed))
3870 goto get_cpus_map;
3871
eadec877 3872 dev_maps = rcu_dereference(sb_dev->xps_rxqs_map);
638b2a69 3873 if (dev_maps) {
fc9bab24 3874 int tci = sk_rx_queue_get(sk);
184c449f 3875
fc9bab24
AN
3876 if (tci >= 0 && tci < dev->num_rx_queues)
3877 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3878 tci);
3879 }
184c449f 3880
fc9bab24
AN
3881get_cpus_map:
3882 if (queue_index < 0) {
eadec877 3883 dev_maps = rcu_dereference(sb_dev->xps_cpus_map);
fc9bab24
AN
3884 if (dev_maps) {
3885 unsigned int tci = skb->sender_cpu - 1;
3886
3887 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3888 tci);
638b2a69
JP
3889 }
3890 }
3891 rcu_read_unlock();
3892
3893 return queue_index;
3894#else
3895 return -1;
3896#endif
3897}
3898
a4ea8a3d 3899u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
a350ecce 3900 struct net_device *sb_dev)
a4ea8a3d
AD
3901{
3902 return 0;
3903}
3904EXPORT_SYMBOL(dev_pick_tx_zero);
3905
3906u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
a350ecce 3907 struct net_device *sb_dev)
a4ea8a3d
AD
3908{
3909 return (u16)raw_smp_processor_id() % dev->real_num_tx_queues;
3910}
3911EXPORT_SYMBOL(dev_pick_tx_cpu_id);
3912
b71b5837
PA
3913u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
3914 struct net_device *sb_dev)
638b2a69
JP
3915{
3916 struct sock *sk = skb->sk;
3917 int queue_index = sk_tx_queue_get(sk);
3918
eadec877
AD
3919 sb_dev = sb_dev ? : dev;
3920
638b2a69
JP
3921 if (queue_index < 0 || skb->ooo_okay ||
3922 queue_index >= dev->real_num_tx_queues) {
eadec877 3923 int new_index = get_xps_queue(dev, sb_dev, skb);
f4563a75 3924
638b2a69 3925 if (new_index < 0)
eadec877 3926 new_index = skb_tx_hash(dev, sb_dev, skb);
638b2a69
JP
3927
3928 if (queue_index != new_index && sk &&
004a5d01 3929 sk_fullsock(sk) &&
638b2a69
JP
3930 rcu_access_pointer(sk->sk_dst_cache))
3931 sk_tx_queue_set(sk, new_index);
3932
3933 queue_index = new_index;
3934 }
3935
3936 return queue_index;
3937}
b71b5837 3938EXPORT_SYMBOL(netdev_pick_tx);
638b2a69 3939
4bd97d51
PA
3940struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
3941 struct sk_buff *skb,
3942 struct net_device *sb_dev)
638b2a69
JP
3943{
3944 int queue_index = 0;
3945
3946#ifdef CONFIG_XPS
52bd2d62
ED
3947 u32 sender_cpu = skb->sender_cpu - 1;
3948
3949 if (sender_cpu >= (u32)NR_CPUS)
638b2a69
JP
3950 skb->sender_cpu = raw_smp_processor_id() + 1;
3951#endif
3952
3953 if (dev->real_num_tx_queues != 1) {
3954 const struct net_device_ops *ops = dev->netdev_ops;
f4563a75 3955
638b2a69 3956 if (ops->ndo_select_queue)
a350ecce 3957 queue_index = ops->ndo_select_queue(dev, skb, sb_dev);
638b2a69 3958 else
4bd97d51 3959 queue_index = netdev_pick_tx(dev, skb, sb_dev);
638b2a69 3960
d584527c 3961 queue_index = netdev_cap_txqueue(dev, queue_index);
638b2a69
JP
3962 }
3963
3964 skb_set_queue_mapping(skb, queue_index);
3965 return netdev_get_tx_queue(dev, queue_index);
3966}
3967
d29f749e 3968/**
9d08dd3d 3969 * __dev_queue_xmit - transmit a buffer
d29f749e 3970 * @skb: buffer to transmit
eadec877 3971 * @sb_dev: suboordinate device used for L2 forwarding offload
d29f749e
DJ
3972 *
3973 * Queue a buffer for transmission to a network device. The caller must
3974 * have set the device and priority and built the buffer before calling
3975 * this function. The function can be called from an interrupt.
3976 *
3977 * A negative errno code is returned on a failure. A success does not
3978 * guarantee the frame will be transmitted as it may be dropped due
3979 * to congestion or traffic shaping.
3980 *
3981 * -----------------------------------------------------------------------------------
3982 * I notice this method can also return errors from the queue disciplines,
3983 * including NET_XMIT_DROP, which is a positive value. So, errors can also
3984 * be positive.
3985 *
3986 * Regardless of the return value, the skb is consumed, so it is currently
3987 * difficult to retry a send to this method. (You can bump the ref count
3988 * before sending to hold a reference for retry if you are careful.)
3989 *
3990 * When calling this method, interrupts MUST be enabled. This is because
3991 * the BH enable code must have IRQs enabled so that it will not deadlock.
3992 * --BLG
3993 */
eadec877 3994static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
1da177e4
LT
3995{
3996 struct net_device *dev = skb->dev;
dc2b4847 3997 struct netdev_queue *txq;
1da177e4
LT
3998 struct Qdisc *q;
3999 int rc = -ENOMEM;
f53c7239 4000 bool again = false;
1da177e4 4001
6d1ccff6
ED
4002 skb_reset_mac_header(skb);
4003
e7fd2885
WB
4004 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
4005 __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
4006
4ec93edb
YH
4007 /* Disable soft irqs for various locks below. Also
4008 * stops preemption for RCU.
1da177e4 4009 */
4ec93edb 4010 rcu_read_lock_bh();
1da177e4 4011
5bc1421e
NH
4012 skb_update_prio(skb);
4013
1f211a1b
DB
4014 qdisc_pkt_len_init(skb);
4015#ifdef CONFIG_NET_CLS_ACT
8dc07fdb 4016 skb->tc_at_ingress = 0;
1f211a1b 4017# ifdef CONFIG_NET_EGRESS
aabf6772 4018 if (static_branch_unlikely(&egress_needed_key)) {
1f211a1b
DB
4019 skb = sch_handle_egress(skb, &rc, dev);
4020 if (!skb)
4021 goto out;
4022 }
4023# endif
4024#endif
02875878
ED
4025 /* If device/qdisc don't need skb->dst, release it right now while
4026 * its hot in this cpu cache.
4027 */
4028 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
4029 skb_dst_drop(skb);
4030 else
4031 skb_dst_force(skb);
4032
4bd97d51 4033 txq = netdev_core_pick_tx(dev, skb, sb_dev);
a898def2 4034 q = rcu_dereference_bh(txq->qdisc);
37437bb2 4035
cf66ba58 4036 trace_net_dev_queue(skb);
1da177e4 4037 if (q->enqueue) {
bbd8a0d3 4038 rc = __dev_xmit_skb(skb, q, dev, txq);
37437bb2 4039 goto out;
1da177e4
LT
4040 }
4041
4042 /* The device has no queue. Common case for software devices:
eb13da1a 4043 * loopback, all the sorts of tunnels...
1da177e4 4044
eb13da1a 4045 * Really, it is unlikely that netif_tx_lock protection is necessary
4046 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
4047 * counters.)
4048 * However, it is possible, that they rely on protection
4049 * made by us here.
1da177e4 4050
eb13da1a 4051 * Check this and shot the lock. It is not prone from deadlocks.
4052 *Either shot noqueue qdisc, it is even simpler 8)
1da177e4
LT
4053 */
4054 if (dev->flags & IFF_UP) {
4055 int cpu = smp_processor_id(); /* ok because BHs are off */
4056
c773e847 4057 if (txq->xmit_lock_owner != cpu) {
97cdcf37 4058 if (dev_xmit_recursion())
745e20f1
ED
4059 goto recursion_alert;
4060
f53c7239 4061 skb = validate_xmit_skb(skb, dev, &again);
1f59533f 4062 if (!skb)
d21fd63e 4063 goto out;
1f59533f 4064
c773e847 4065 HARD_TX_LOCK(dev, txq, cpu);
1da177e4 4066
73466498 4067 if (!netif_xmit_stopped(txq)) {
97cdcf37 4068 dev_xmit_recursion_inc();
ce93718f 4069 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
97cdcf37 4070 dev_xmit_recursion_dec();
572a9d7b 4071 if (dev_xmit_complete(rc)) {
c773e847 4072 HARD_TX_UNLOCK(dev, txq);
1da177e4
LT
4073 goto out;
4074 }
4075 }
c773e847 4076 HARD_TX_UNLOCK(dev, txq);
e87cc472
JP
4077 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
4078 dev->name);
1da177e4
LT
4079 } else {
4080 /* Recursion is detected! It is possible,
745e20f1
ED
4081 * unfortunately
4082 */
4083recursion_alert:
e87cc472
JP
4084 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
4085 dev->name);
1da177e4
LT
4086 }
4087 }
4088
4089 rc = -ENETDOWN;
d4828d85 4090 rcu_read_unlock_bh();
1da177e4 4091
015f0688 4092 atomic_long_inc(&dev->tx_dropped);
1f59533f 4093 kfree_skb_list(skb);
1da177e4
LT
4094 return rc;
4095out:
d4828d85 4096 rcu_read_unlock_bh();
1da177e4
LT
4097 return rc;
4098}
f663dd9a 4099
2b4aa3ce 4100int dev_queue_xmit(struct sk_buff *skb)
f663dd9a
JW
4101{
4102 return __dev_queue_xmit(skb, NULL);
4103}
2b4aa3ce 4104EXPORT_SYMBOL(dev_queue_xmit);
1da177e4 4105
eadec877 4106int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
f663dd9a 4107{
eadec877 4108 return __dev_queue_xmit(skb, sb_dev);
f663dd9a
JW
4109}
4110EXPORT_SYMBOL(dev_queue_xmit_accel);
4111
865b03f2
MK
4112int dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
4113{
4114 struct net_device *dev = skb->dev;
4115 struct sk_buff *orig_skb = skb;
4116 struct netdev_queue *txq;
4117 int ret = NETDEV_TX_BUSY;
4118 bool again = false;
4119
4120 if (unlikely(!netif_running(dev) ||
4121 !netif_carrier_ok(dev)))
4122 goto drop;
4123
4124 skb = validate_xmit_skb_list(skb, dev, &again);
4125 if (skb != orig_skb)
4126 goto drop;
4127
4128 skb_set_queue_mapping(skb, queue_id);
4129 txq = skb_get_tx_queue(dev, skb);
4130
4131 local_bh_disable();
4132
4133 HARD_TX_LOCK(dev, txq, smp_processor_id());
4134 if (!netif_xmit_frozen_or_drv_stopped(txq))
4135 ret = netdev_start_xmit(skb, dev, txq, false);
4136 HARD_TX_UNLOCK(dev, txq);
4137
4138 local_bh_enable();
4139
4140 if (!dev_xmit_complete(ret))
4141 kfree_skb(skb);
4142
4143 return ret;
4144drop:
4145 atomic_long_inc(&dev->tx_dropped);
4146 kfree_skb_list(skb);
4147 return NET_XMIT_DROP;
4148}
4149EXPORT_SYMBOL(dev_direct_xmit);
1da177e4 4150
eb13da1a 4151/*************************************************************************
4152 * Receiver routines
4153 *************************************************************************/
1da177e4 4154
6b2bedc3 4155int netdev_max_backlog __read_mostly = 1000;
c9e6bc64
ED
4156EXPORT_SYMBOL(netdev_max_backlog);
4157
3b098e2d 4158int netdev_tstamp_prequeue __read_mostly = 1;
6b2bedc3 4159int netdev_budget __read_mostly = 300;
7acf8a1e 4160unsigned int __read_mostly netdev_budget_usecs = 2000;
3d48b53f
MT
4161int weight_p __read_mostly = 64; /* old backlog weight */
4162int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
4163int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
4164int dev_rx_weight __read_mostly = 64;
4165int dev_tx_weight __read_mostly = 64;
323ebb61
EC
4166/* Maximum number of GRO_NORMAL skbs to batch up for list-RX */
4167int gro_normal_batch __read_mostly = 8;
1da177e4 4168
eecfd7c4
ED
4169/* Called with irq disabled */
4170static inline void ____napi_schedule(struct softnet_data *sd,
4171 struct napi_struct *napi)
4172{
4173 list_add_tail(&napi->poll_list, &sd->poll_list);
4174 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4175}
4176
bfb564e7
KK
4177#ifdef CONFIG_RPS
4178
4179/* One global table that all flow-based protocols share. */
6e3f7faf 4180struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
bfb564e7 4181EXPORT_SYMBOL(rps_sock_flow_table);
567e4b79
ED
4182u32 rps_cpu_mask __read_mostly;
4183EXPORT_SYMBOL(rps_cpu_mask);
bfb564e7 4184
dc05360f 4185struct static_key_false rps_needed __read_mostly;
3df97ba8 4186EXPORT_SYMBOL(rps_needed);
dc05360f 4187struct static_key_false rfs_needed __read_mostly;
13bfff25 4188EXPORT_SYMBOL(rfs_needed);
adc9300e 4189
c445477d
BH
4190static struct rps_dev_flow *
4191set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4192 struct rps_dev_flow *rflow, u16 next_cpu)
4193{
a31196b0 4194 if (next_cpu < nr_cpu_ids) {
c445477d
BH
4195#ifdef CONFIG_RFS_ACCEL
4196 struct netdev_rx_queue *rxqueue;
4197 struct rps_dev_flow_table *flow_table;
4198 struct rps_dev_flow *old_rflow;
4199 u32 flow_id;
4200 u16 rxq_index;
4201 int rc;
4202
4203 /* Should we steer this flow to a different hardware queue? */
69a19ee6
BH
4204 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
4205 !(dev->features & NETIF_F_NTUPLE))
c445477d
BH
4206 goto out;
4207 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
4208 if (rxq_index == skb_get_rx_queue(skb))
4209 goto out;
4210
4211 rxqueue = dev->_rx + rxq_index;
4212 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4213 if (!flow_table)
4214 goto out;
61b905da 4215 flow_id = skb_get_hash(skb) & flow_table->mask;
c445477d
BH
4216 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
4217 rxq_index, flow_id);
4218 if (rc < 0)
4219 goto out;
4220 old_rflow = rflow;
4221 rflow = &flow_table->flows[flow_id];
c445477d
BH
4222 rflow->filter = rc;
4223 if (old_rflow->filter == rflow->filter)
4224 old_rflow->filter = RPS_NO_FILTER;
4225 out:
4226#endif
4227 rflow->last_qtail =
09994d1b 4228 per_cpu(softnet_data, next_cpu).input_queue_head;
c445477d
BH
4229 }
4230
09994d1b 4231 rflow->cpu = next_cpu;
c445477d
BH
4232 return rflow;
4233}
4234
bfb564e7
KK
4235/*
4236 * get_rps_cpu is called from netif_receive_skb and returns the target
4237 * CPU from the RPS map of the receiving queue for a given skb.
4238 * rcu_read_lock must be held on entry.
4239 */
4240static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4241 struct rps_dev_flow **rflowp)
4242{
567e4b79
ED
4243 const struct rps_sock_flow_table *sock_flow_table;
4244 struct netdev_rx_queue *rxqueue = dev->_rx;
bfb564e7 4245 struct rps_dev_flow_table *flow_table;
567e4b79 4246 struct rps_map *map;
bfb564e7 4247 int cpu = -1;
567e4b79 4248 u32 tcpu;
61b905da 4249 u32 hash;
bfb564e7
KK
4250
4251 if (skb_rx_queue_recorded(skb)) {
4252 u16 index = skb_get_rx_queue(skb);
567e4b79 4253
62fe0b40
BH
4254 if (unlikely(index >= dev->real_num_rx_queues)) {
4255 WARN_ONCE(dev->real_num_rx_queues > 1,
4256 "%s received packet on queue %u, but number "
4257 "of RX queues is %u\n",
4258 dev->name, index, dev->real_num_rx_queues);
bfb564e7
KK
4259 goto done;
4260 }
567e4b79
ED
4261 rxqueue += index;
4262 }
bfb564e7 4263
567e4b79
ED
4264 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4265
4266 flow_table = rcu_dereference(rxqueue->rps_flow_table);
6e3f7faf 4267 map = rcu_dereference(rxqueue->rps_map);
567e4b79 4268 if (!flow_table && !map)
bfb564e7
KK
4269 goto done;
4270
2d47b459 4271 skb_reset_network_header(skb);
61b905da
TH
4272 hash = skb_get_hash(skb);
4273 if (!hash)
bfb564e7
KK
4274 goto done;
4275
fec5e652
TH
4276 sock_flow_table = rcu_dereference(rps_sock_flow_table);
4277 if (flow_table && sock_flow_table) {
fec5e652 4278 struct rps_dev_flow *rflow;
567e4b79
ED
4279 u32 next_cpu;
4280 u32 ident;
4281
4282 /* First check into global flow table if there is a match */
4283 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4284 if ((ident ^ hash) & ~rps_cpu_mask)
4285 goto try_rps;
fec5e652 4286
567e4b79
ED
4287 next_cpu = ident & rps_cpu_mask;
4288
4289 /* OK, now we know there is a match,
4290 * we can look at the local (per receive queue) flow table
4291 */
61b905da 4292 rflow = &flow_table->flows[hash & flow_table->mask];
fec5e652
TH
4293 tcpu = rflow->cpu;
4294
fec5e652
TH
4295 /*
4296 * If the desired CPU (where last recvmsg was done) is
4297 * different from current CPU (one in the rx-queue flow
4298 * table entry), switch if one of the following holds:
a31196b0 4299 * - Current CPU is unset (>= nr_cpu_ids).
fec5e652
TH
4300 * - Current CPU is offline.
4301 * - The current CPU's queue tail has advanced beyond the
4302 * last packet that was enqueued using this table entry.
4303 * This guarantees that all previous packets for the flow
4304 * have been dequeued, thus preserving in order delivery.
4305 */
4306 if (unlikely(tcpu != next_cpu) &&
a31196b0 4307 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
fec5e652 4308 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
baefa31d
TH
4309 rflow->last_qtail)) >= 0)) {
4310 tcpu = next_cpu;
c445477d 4311 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
baefa31d 4312 }
c445477d 4313
a31196b0 4314 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
fec5e652
TH
4315 *rflowp = rflow;
4316 cpu = tcpu;
4317 goto done;
4318 }
4319 }
4320
567e4b79
ED
4321try_rps:
4322
0a9627f2 4323 if (map) {
8fc54f68 4324 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
0a9627f2
TH
4325 if (cpu_online(tcpu)) {
4326 cpu = tcpu;
4327 goto done;
4328 }
4329 }
4330
4331done:
0a9627f2
TH
4332 return cpu;
4333}
4334
c445477d
BH
4335#ifdef CONFIG_RFS_ACCEL
4336
4337/**
4338 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4339 * @dev: Device on which the filter was set
4340 * @rxq_index: RX queue index
4341 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4342 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4343 *
4344 * Drivers that implement ndo_rx_flow_steer() should periodically call
4345 * this function for each installed filter and remove the filters for
4346 * which it returns %true.
4347 */
4348bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4349 u32 flow_id, u16 filter_id)
4350{
4351 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4352 struct rps_dev_flow_table *flow_table;
4353 struct rps_dev_flow *rflow;
4354 bool expire = true;
a31196b0 4355 unsigned int cpu;
c445477d
BH
4356
4357 rcu_read_lock();
4358 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4359 if (flow_table && flow_id <= flow_table->mask) {
4360 rflow = &flow_table->flows[flow_id];
6aa7de05 4361 cpu = READ_ONCE(rflow->cpu);
a31196b0 4362 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
c445477d
BH
4363 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
4364 rflow->last_qtail) <
4365 (int)(10 * flow_table->mask)))
4366 expire = false;
4367 }
4368 rcu_read_unlock();
4369 return expire;
4370}
4371EXPORT_SYMBOL(rps_may_expire_flow);
4372
4373#endif /* CONFIG_RFS_ACCEL */
4374
0a9627f2 4375/* Called from hardirq (IPI) context */
e36fa2f7 4376static void rps_trigger_softirq(void *data)
0a9627f2 4377{
e36fa2f7
ED
4378 struct softnet_data *sd = data;
4379
eecfd7c4 4380 ____napi_schedule(sd, &sd->backlog);
dee42870 4381 sd->received_rps++;
0a9627f2 4382}
e36fa2f7 4383
fec5e652 4384#endif /* CONFIG_RPS */
0a9627f2 4385
e36fa2f7
ED
4386/*
4387 * Check if this softnet_data structure is another cpu one
4388 * If yes, queue it to our IPI list and return 1
4389 * If no, return 0
4390 */
4391static int rps_ipi_queued(struct softnet_data *sd)
4392{
4393#ifdef CONFIG_RPS
903ceff7 4394 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
e36fa2f7
ED
4395
4396 if (sd != mysd) {
4397 sd->rps_ipi_next = mysd->rps_ipi_list;
4398 mysd->rps_ipi_list = sd;
4399
4400 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4401 return 1;
4402 }
4403#endif /* CONFIG_RPS */
4404 return 0;
4405}
4406
99bbc707
WB
4407#ifdef CONFIG_NET_FLOW_LIMIT
4408int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4409#endif
4410
4411static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4412{
4413#ifdef CONFIG_NET_FLOW_LIMIT
4414 struct sd_flow_limit *fl;
4415 struct softnet_data *sd;
4416 unsigned int old_flow, new_flow;
4417
4418 if (qlen < (netdev_max_backlog >> 1))
4419 return false;
4420
903ceff7 4421 sd = this_cpu_ptr(&softnet_data);
99bbc707
WB
4422
4423 rcu_read_lock();
4424 fl = rcu_dereference(sd->flow_limit);
4425 if (fl) {
3958afa1 4426 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
99bbc707
WB
4427 old_flow = fl->history[fl->history_head];
4428 fl->history[fl->history_head] = new_flow;
4429
4430 fl->history_head++;
4431 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4432
4433 if (likely(fl->buckets[old_flow]))
4434 fl->buckets[old_flow]--;
4435
4436 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4437 fl->count++;
4438 rcu_read_unlock();
4439 return true;
4440 }
4441 }
4442 rcu_read_unlock();
4443#endif
4444 return false;
4445}
4446
0a9627f2
TH
4447/*
4448 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4449 * queue (may be a remote CPU queue).
4450 */
fec5e652
TH
4451static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4452 unsigned int *qtail)
0a9627f2 4453{
e36fa2f7 4454 struct softnet_data *sd;
0a9627f2 4455 unsigned long flags;
99bbc707 4456 unsigned int qlen;
0a9627f2 4457
e36fa2f7 4458 sd = &per_cpu(softnet_data, cpu);
0a9627f2
TH
4459
4460 local_irq_save(flags);
0a9627f2 4461
e36fa2f7 4462 rps_lock(sd);
e9e4dd32
JA
4463 if (!netif_running(skb->dev))
4464 goto drop;
99bbc707
WB
4465 qlen = skb_queue_len(&sd->input_pkt_queue);
4466 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
e008f3f0 4467 if (qlen) {
0a9627f2 4468enqueue:
e36fa2f7 4469 __skb_queue_tail(&sd->input_pkt_queue, skb);
76cc8b13 4470 input_queue_tail_incr_save(sd, qtail);
e36fa2f7 4471 rps_unlock(sd);
152102c7 4472 local_irq_restore(flags);
0a9627f2
TH
4473 return NET_RX_SUCCESS;
4474 }
4475
ebda37c2
ED
4476 /* Schedule NAPI for backlog device
4477 * We can use non atomic operation since we own the queue lock
4478 */
4479 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
e36fa2f7 4480 if (!rps_ipi_queued(sd))
eecfd7c4 4481 ____napi_schedule(sd, &sd->backlog);
0a9627f2
TH
4482 }
4483 goto enqueue;
4484 }
4485
e9e4dd32 4486drop:
dee42870 4487 sd->dropped++;
e36fa2f7 4488 rps_unlock(sd);
0a9627f2 4489
0a9627f2
TH
4490 local_irq_restore(flags);
4491
caf586e5 4492 atomic_long_inc(&skb->dev->rx_dropped);
0a9627f2
TH
4493 kfree_skb(skb);
4494 return NET_RX_DROP;
4495}
1da177e4 4496
e817f856
JDB
4497static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4498{
4499 struct net_device *dev = skb->dev;
4500 struct netdev_rx_queue *rxqueue;
4501
4502 rxqueue = dev->_rx;
4503
4504 if (skb_rx_queue_recorded(skb)) {
4505 u16 index = skb_get_rx_queue(skb);
4506
4507 if (unlikely(index >= dev->real_num_rx_queues)) {
4508 WARN_ONCE(dev->real_num_rx_queues > 1,
4509 "%s received packet on queue %u, but number "
4510 "of RX queues is %u\n",
4511 dev->name, index, dev->real_num_rx_queues);
4512
4513 return rxqueue; /* Return first rxqueue */
4514 }
4515 rxqueue += index;
4516 }
4517 return rxqueue;
4518}
4519
d4455169 4520static u32 netif_receive_generic_xdp(struct sk_buff *skb,
02671e23 4521 struct xdp_buff *xdp,
d4455169
JF
4522 struct bpf_prog *xdp_prog)
4523{
e817f856 4524 struct netdev_rx_queue *rxqueue;
198d83bb 4525 void *orig_data, *orig_data_end;
de8f3a83 4526 u32 metalen, act = XDP_DROP;
29724956
JDB
4527 __be16 orig_eth_type;
4528 struct ethhdr *eth;
4529 bool orig_bcast;
d4455169
JF
4530 int hlen, off;
4531 u32 mac_len;
4532
4533 /* Reinjected packets coming from act_mirred or similar should
4534 * not get XDP generic processing.
4535 */
ad1e03b2 4536 if (skb_is_tc_redirected(skb))
d4455169
JF
4537 return XDP_PASS;
4538
de8f3a83
DB
4539 /* XDP packets must be linear and must have sufficient headroom
4540 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
4541 * native XDP provides, thus we need to do it here as well.
4542 */
ad1e03b2 4543 if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
de8f3a83
DB
4544 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
4545 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
4546 int troom = skb->tail + skb->data_len - skb->end;
4547
4548 /* In case we have to go down the path and also linearize,
4549 * then lets do the pskb_expand_head() work just once here.
4550 */
4551 if (pskb_expand_head(skb,
4552 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
4553 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
4554 goto do_drop;
2d17d8d7 4555 if (skb_linearize(skb))
de8f3a83
DB
4556 goto do_drop;
4557 }
d4455169
JF
4558
4559 /* The XDP program wants to see the packet starting at the MAC
4560 * header.
4561 */
4562 mac_len = skb->data - skb_mac_header(skb);
4563 hlen = skb_headlen(skb) + mac_len;
02671e23
BT
4564 xdp->data = skb->data - mac_len;
4565 xdp->data_meta = xdp->data;
4566 xdp->data_end = xdp->data + hlen;
4567 xdp->data_hard_start = skb->data - skb_headroom(skb);
4568 orig_data_end = xdp->data_end;
4569 orig_data = xdp->data;
29724956
JDB
4570 eth = (struct ethhdr *)xdp->data;
4571 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4572 orig_eth_type = eth->h_proto;
d4455169 4573
e817f856 4574 rxqueue = netif_get_rxqueue(skb);
02671e23 4575 xdp->rxq = &rxqueue->xdp_rxq;
e817f856 4576
02671e23 4577 act = bpf_prog_run_xdp(xdp_prog, xdp);
d4455169 4578
065af355 4579 /* check if bpf_xdp_adjust_head was used */
02671e23 4580 off = xdp->data - orig_data;
065af355
JDB
4581 if (off) {
4582 if (off > 0)
4583 __skb_pull(skb, off);
4584 else if (off < 0)
4585 __skb_push(skb, -off);
4586
4587 skb->mac_header += off;
4588 skb_reset_network_header(skb);
4589 }
d4455169 4590
198d83bb
NS
4591 /* check if bpf_xdp_adjust_tail was used. it can only "shrink"
4592 * pckt.
4593 */
02671e23 4594 off = orig_data_end - xdp->data_end;
f7613120 4595 if (off != 0) {
02671e23 4596 skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
f7613120 4597 skb->len -= off;
02671e23 4598
f7613120 4599 }
198d83bb 4600
29724956
JDB
4601 /* check if XDP changed eth hdr such SKB needs update */
4602 eth = (struct ethhdr *)xdp->data;
4603 if ((orig_eth_type != eth->h_proto) ||
4604 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
4605 __skb_push(skb, ETH_HLEN);
4606 skb->protocol = eth_type_trans(skb, skb->dev);
4607 }
4608
d4455169 4609 switch (act) {
6103aa96 4610 case XDP_REDIRECT:
d4455169
JF
4611 case XDP_TX:
4612 __skb_push(skb, mac_len);
de8f3a83 4613 break;
d4455169 4614 case XDP_PASS:
02671e23 4615 metalen = xdp->data - xdp->data_meta;
de8f3a83
DB
4616 if (metalen)
4617 skb_metadata_set(skb, metalen);
d4455169 4618 break;
d4455169
JF
4619 default:
4620 bpf_warn_invalid_xdp_action(act);
4621 /* fall through */
4622 case XDP_ABORTED:
4623 trace_xdp_exception(skb->dev, xdp_prog, act);
4624 /* fall through */
4625 case XDP_DROP:
4626 do_drop:
4627 kfree_skb(skb);
4628 break;
4629 }
4630
4631 return act;
4632}
4633
4634/* When doing generic XDP we have to bypass the qdisc layer and the
4635 * network taps in order to match in-driver-XDP behavior.
4636 */
7c497478 4637void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
d4455169
JF
4638{
4639 struct net_device *dev = skb->dev;
4640 struct netdev_queue *txq;
4641 bool free_skb = true;
4642 int cpu, rc;
4643
4bd97d51 4644 txq = netdev_core_pick_tx(dev, skb, NULL);
d4455169
JF
4645 cpu = smp_processor_id();
4646 HARD_TX_LOCK(dev, txq, cpu);
4647 if (!netif_xmit_stopped(txq)) {
4648 rc = netdev_start_xmit(skb, dev, txq, 0);
4649 if (dev_xmit_complete(rc))
4650 free_skb = false;
4651 }
4652 HARD_TX_UNLOCK(dev, txq);
4653 if (free_skb) {
4654 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4655 kfree_skb(skb);
4656 }
4657}
7c497478 4658EXPORT_SYMBOL_GPL(generic_xdp_tx);
d4455169 4659
02786475 4660static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
d4455169 4661
7c497478 4662int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
d4455169 4663{
d4455169 4664 if (xdp_prog) {
02671e23
BT
4665 struct xdp_buff xdp;
4666 u32 act;
6103aa96 4667 int err;
d4455169 4668
02671e23 4669 act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
d4455169 4670 if (act != XDP_PASS) {
6103aa96
JF
4671 switch (act) {
4672 case XDP_REDIRECT:
2facaad6 4673 err = xdp_do_generic_redirect(skb->dev, skb,
02671e23 4674 &xdp, xdp_prog);
6103aa96
JF
4675 if (err)
4676 goto out_redir;
02671e23 4677 break;
6103aa96 4678 case XDP_TX:
d4455169 4679 generic_xdp_tx(skb, xdp_prog);
6103aa96
JF
4680 break;
4681 }
d4455169
JF
4682 return XDP_DROP;
4683 }
4684 }
4685 return XDP_PASS;
6103aa96 4686out_redir:
6103aa96
JF
4687 kfree_skb(skb);
4688 return XDP_DROP;
d4455169 4689}
7c497478 4690EXPORT_SYMBOL_GPL(do_xdp_generic);
d4455169 4691
ae78dbfa 4692static int netif_rx_internal(struct sk_buff *skb)
1da177e4 4693{
b0e28f1e 4694 int ret;
1da177e4 4695
588f0330 4696 net_timestamp_check(netdev_tstamp_prequeue, skb);
1da177e4 4697
cf66ba58 4698 trace_netif_rx(skb);
d4455169 4699
df334545 4700#ifdef CONFIG_RPS
dc05360f 4701 if (static_branch_unlikely(&rps_needed)) {
fec5e652 4702 struct rps_dev_flow voidflow, *rflow = &voidflow;
b0e28f1e
ED
4703 int cpu;
4704
cece1945 4705 preempt_disable();
b0e28f1e 4706 rcu_read_lock();
fec5e652
TH
4707
4708 cpu = get_rps_cpu(skb->dev, skb, &rflow);
b0e28f1e
ED
4709 if (cpu < 0)
4710 cpu = smp_processor_id();
fec5e652
TH
4711
4712 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4713
b0e28f1e 4714 rcu_read_unlock();
cece1945 4715 preempt_enable();
adc9300e
ED
4716 } else
4717#endif
fec5e652
TH
4718 {
4719 unsigned int qtail;
f4563a75 4720
fec5e652
TH
4721 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4722 put_cpu();
4723 }
b0e28f1e 4724 return ret;
1da177e4 4725}
ae78dbfa
BH
4726
4727/**
4728 * netif_rx - post buffer to the network code
4729 * @skb: buffer to post
4730 *
4731 * This function receives a packet from a device driver and queues it for
4732 * the upper (protocol) levels to process. It always succeeds. The buffer
4733 * may be dropped during processing for congestion control or by the
4734 * protocol layers.
4735 *
4736 * return values:
4737 * NET_RX_SUCCESS (no congestion)
4738 * NET_RX_DROP (packet was dropped)
4739 *
4740 */
4741
4742int netif_rx(struct sk_buff *skb)
4743{
b0e3f1bd
GB
4744 int ret;
4745
ae78dbfa
BH
4746 trace_netif_rx_entry(skb);
4747
b0e3f1bd
GB
4748 ret = netif_rx_internal(skb);
4749 trace_netif_rx_exit(ret);
4750
4751 return ret;
ae78dbfa 4752}
d1b19dff 4753EXPORT_SYMBOL(netif_rx);
1da177e4
LT
4754
4755int netif_rx_ni(struct sk_buff *skb)
4756{
4757 int err;
4758
ae78dbfa
BH
4759 trace_netif_rx_ni_entry(skb);
4760
1da177e4 4761 preempt_disable();
ae78dbfa 4762 err = netif_rx_internal(skb);
1da177e4
LT
4763 if (local_softirq_pending())
4764 do_softirq();
4765 preempt_enable();
b0e3f1bd 4766 trace_netif_rx_ni_exit(err);
1da177e4
LT
4767
4768 return err;
4769}
1da177e4
LT
4770EXPORT_SYMBOL(netif_rx_ni);
4771
0766f788 4772static __latent_entropy void net_tx_action(struct softirq_action *h)
1da177e4 4773{
903ceff7 4774 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
1da177e4
LT
4775
4776 if (sd->completion_queue) {
4777 struct sk_buff *clist;
4778
4779 local_irq_disable();
4780 clist = sd->completion_queue;
4781 sd->completion_queue = NULL;
4782 local_irq_enable();
4783
4784 while (clist) {
4785 struct sk_buff *skb = clist;
f4563a75 4786
1da177e4
LT
4787 clist = clist->next;
4788
63354797 4789 WARN_ON(refcount_read(&skb->users));
e6247027
ED
4790 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4791 trace_consume_skb(skb);
4792 else
4793 trace_kfree_skb(skb, net_tx_action);
15fad714
JDB
4794
4795 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4796 __kfree_skb(skb);
4797 else
4798 __kfree_skb_defer(skb);
1da177e4 4799 }
15fad714
JDB
4800
4801 __kfree_skb_flush();
1da177e4
LT
4802 }
4803
4804 if (sd->output_queue) {
37437bb2 4805 struct Qdisc *head;
1da177e4
LT
4806
4807 local_irq_disable();
4808 head = sd->output_queue;
4809 sd->output_queue = NULL;
a9cbd588 4810 sd->output_queue_tailp = &sd->output_queue;
1da177e4
LT
4811 local_irq_enable();
4812
4813 while (head) {
37437bb2 4814 struct Qdisc *q = head;
6b3ba914 4815 spinlock_t *root_lock = NULL;
37437bb2 4816
1da177e4
LT
4817 head = head->next_sched;
4818
6b3ba914
JF
4819 if (!(q->flags & TCQ_F_NOLOCK)) {
4820 root_lock = qdisc_lock(q);
4821 spin_lock(root_lock);
4822 }
3bcb846c
ED
4823 /* We need to make sure head->next_sched is read
4824 * before clearing __QDISC_STATE_SCHED
4825 */
4826 smp_mb__before_atomic();
4827 clear_bit(__QDISC_STATE_SCHED, &q->state);
4828 qdisc_run(q);
6b3ba914
JF
4829 if (root_lock)
4830 spin_unlock(root_lock);
1da177e4
LT
4831 }
4832 }
f53c7239
SK
4833
4834 xfrm_dev_backlog(sd);
1da177e4
LT
4835}
4836
181402a5 4837#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
da678292
MM
4838/* This hook is defined here for ATM LANE */
4839int (*br_fdb_test_addr_hook)(struct net_device *dev,
4840 unsigned char *addr) __read_mostly;
4fb019a0 4841EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
da678292 4842#endif
1da177e4 4843
1f211a1b
DB
4844static inline struct sk_buff *
4845sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
4846 struct net_device *orig_dev)
f697c3e8 4847{
e7582bab 4848#ifdef CONFIG_NET_CLS_ACT
46209401 4849 struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
d2788d34 4850 struct tcf_result cl_res;
24824a09 4851
c9e99fd0
DB
4852 /* If there's at least one ingress present somewhere (so
4853 * we get here via enabled static key), remaining devices
4854 * that are not configured with an ingress qdisc will bail
d2788d34 4855 * out here.
c9e99fd0 4856 */
46209401 4857 if (!miniq)
4577139b 4858 return skb;
46209401 4859
f697c3e8
HX
4860 if (*pt_prev) {
4861 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4862 *pt_prev = NULL;
1da177e4
LT
4863 }
4864
3365495c 4865 qdisc_skb_cb(skb)->pkt_len = skb->len;
8dc07fdb 4866 skb->tc_at_ingress = 1;
46209401 4867 mini_qdisc_bstats_cpu_update(miniq, skb);
c9e99fd0 4868
46209401 4869 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
d2788d34
DB
4870 case TC_ACT_OK:
4871 case TC_ACT_RECLASSIFY:
4872 skb->tc_index = TC_H_MIN(cl_res.classid);
4873 break;
4874 case TC_ACT_SHOT:
46209401 4875 mini_qdisc_qstats_cpu_drop(miniq);
8a3a4c6e
ED
4876 kfree_skb(skb);
4877 return NULL;
d2788d34
DB
4878 case TC_ACT_STOLEN:
4879 case TC_ACT_QUEUED:
e25ea21f 4880 case TC_ACT_TRAP:
8a3a4c6e 4881 consume_skb(skb);
d2788d34 4882 return NULL;
27b29f63
AS
4883 case TC_ACT_REDIRECT:
4884 /* skb_mac_header check was done by cls/act_bpf, so
4885 * we can safely push the L2 header back before
4886 * redirecting to another netdev
4887 */
4888 __skb_push(skb, skb->mac_len);
4889 skb_do_redirect(skb);
4890 return NULL;
720f22fe 4891 case TC_ACT_CONSUMED:
cd11b164 4892 return NULL;
d2788d34
DB
4893 default:
4894 break;
f697c3e8 4895 }
e7582bab 4896#endif /* CONFIG_NET_CLS_ACT */
e687ad60
PN
4897 return skb;
4898}
1da177e4 4899
24b27fc4
MB
4900/**
4901 * netdev_is_rx_handler_busy - check if receive handler is registered
4902 * @dev: device to check
4903 *
4904 * Check if a receive handler is already registered for a given device.
4905 * Return true if there one.
4906 *
4907 * The caller must hold the rtnl_mutex.
4908 */
4909bool netdev_is_rx_handler_busy(struct net_device *dev)
4910{
4911 ASSERT_RTNL();
4912 return dev && rtnl_dereference(dev->rx_handler);
4913}
4914EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
4915
ab95bfe0
JP
4916/**
4917 * netdev_rx_handler_register - register receive handler
4918 * @dev: device to register a handler for
4919 * @rx_handler: receive handler to register
93e2c32b 4920 * @rx_handler_data: data pointer that is used by rx handler
ab95bfe0 4921 *
e227867f 4922 * Register a receive handler for a device. This handler will then be
ab95bfe0
JP
4923 * called from __netif_receive_skb. A negative errno code is returned
4924 * on a failure.
4925 *
4926 * The caller must hold the rtnl_mutex.
8a4eb573
JP
4927 *
4928 * For a general description of rx_handler, see enum rx_handler_result.
ab95bfe0
JP
4929 */
4930int netdev_rx_handler_register(struct net_device *dev,
93e2c32b
JP
4931 rx_handler_func_t *rx_handler,
4932 void *rx_handler_data)
ab95bfe0 4933{
1b7cd004 4934 if (netdev_is_rx_handler_busy(dev))
ab95bfe0
JP
4935 return -EBUSY;
4936
f5426250
PA
4937 if (dev->priv_flags & IFF_NO_RX_HANDLER)
4938 return -EINVAL;
4939
00cfec37 4940 /* Note: rx_handler_data must be set before rx_handler */
93e2c32b 4941 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
ab95bfe0
JP
4942 rcu_assign_pointer(dev->rx_handler, rx_handler);
4943
4944 return 0;
4945}
4946EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
4947
4948/**
4949 * netdev_rx_handler_unregister - unregister receive handler
4950 * @dev: device to unregister a handler from
4951 *
166ec369 4952 * Unregister a receive handler from a device.
ab95bfe0
JP
4953 *
4954 * The caller must hold the rtnl_mutex.
4955 */
4956void netdev_rx_handler_unregister(struct net_device *dev)
4957{
4958
4959 ASSERT_RTNL();
a9b3cd7f 4960 RCU_INIT_POINTER(dev->rx_handler, NULL);
00cfec37
ED
4961 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
4962 * section has a guarantee to see a non NULL rx_handler_data
4963 * as well.
4964 */
4965 synchronize_net();
a9b3cd7f 4966 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
ab95bfe0
JP
4967}
4968EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
4969
b4b9e355
MG
4970/*
4971 * Limit the use of PFMEMALLOC reserves to those protocols that implement
4972 * the special handling of PFMEMALLOC skbs.
4973 */
4974static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
4975{
4976 switch (skb->protocol) {
2b8837ae
JP
4977 case htons(ETH_P_ARP):
4978 case htons(ETH_P_IP):
4979 case htons(ETH_P_IPV6):
4980 case htons(ETH_P_8021Q):
4981 case htons(ETH_P_8021AD):
b4b9e355
MG
4982 return true;
4983 default:
4984 return false;
4985 }
4986}
4987
e687ad60
PN
4988static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
4989 int *ret, struct net_device *orig_dev)
4990{
4991 if (nf_hook_ingress_active(skb)) {
2c1e2703
AC
4992 int ingress_retval;
4993
e687ad60
PN
4994 if (*pt_prev) {
4995 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4996 *pt_prev = NULL;
4997 }
4998
2c1e2703
AC
4999 rcu_read_lock();
5000 ingress_retval = nf_hook_ingress(skb);
5001 rcu_read_unlock();
5002 return ingress_retval;
e687ad60
PN
5003 }
5004 return 0;
5005}
e687ad60 5006
88eb1944
EC
5007static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc,
5008 struct packet_type **ppt_prev)
1da177e4
LT
5009{
5010 struct packet_type *ptype, *pt_prev;
ab95bfe0 5011 rx_handler_func_t *rx_handler;
f2ccd8fa 5012 struct net_device *orig_dev;
8a4eb573 5013 bool deliver_exact = false;
1da177e4 5014 int ret = NET_RX_DROP;
252e3346 5015 __be16 type;
1da177e4 5016
588f0330 5017 net_timestamp_check(!netdev_tstamp_prequeue, skb);
81bbb3d4 5018
cf66ba58 5019 trace_netif_receive_skb(skb);
9b22ea56 5020
cc9bd5ce 5021 orig_dev = skb->dev;
8f903c70 5022
c1d2bbe1 5023 skb_reset_network_header(skb);
fda55eca
ED
5024 if (!skb_transport_header_was_set(skb))
5025 skb_reset_transport_header(skb);
0b5c9db1 5026 skb_reset_mac_len(skb);
1da177e4
LT
5027
5028 pt_prev = NULL;
5029
63d8ea7f 5030another_round:
b6858177 5031 skb->skb_iif = skb->dev->ifindex;
63d8ea7f
DM
5032
5033 __this_cpu_inc(softnet_data.processed);
5034
458bf2f2
SH
5035 if (static_branch_unlikely(&generic_xdp_needed_key)) {
5036 int ret2;
5037
5038 preempt_disable();
5039 ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
5040 preempt_enable();
5041
5042 if (ret2 != XDP_PASS)
5043 return NET_RX_DROP;
5044 skb_reset_mac_len(skb);
5045 }
5046
8ad227ff
PM
5047 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5048 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
0d5501c1 5049 skb = skb_vlan_untag(skb);
bcc6d479 5050 if (unlikely(!skb))
2c17d27c 5051 goto out;
bcc6d479
JP
5052 }
5053
e7246e12
WB
5054 if (skb_skip_tc_classify(skb))
5055 goto skip_classify;
1da177e4 5056
9754e293 5057 if (pfmemalloc)
b4b9e355
MG
5058 goto skip_taps;
5059
1da177e4 5060 list_for_each_entry_rcu(ptype, &ptype_all, list) {
7866a621
SN
5061 if (pt_prev)
5062 ret = deliver_skb(skb, pt_prev, orig_dev);
5063 pt_prev = ptype;
5064 }
5065
5066 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
5067 if (pt_prev)
5068 ret = deliver_skb(skb, pt_prev, orig_dev);
5069 pt_prev = ptype;
1da177e4
LT
5070 }
5071
b4b9e355 5072skip_taps:
1cf51900 5073#ifdef CONFIG_NET_INGRESS
aabf6772 5074 if (static_branch_unlikely(&ingress_needed_key)) {
1f211a1b 5075 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev);
4577139b 5076 if (!skb)
2c17d27c 5077 goto out;
e687ad60
PN
5078
5079 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
2c17d27c 5080 goto out;
4577139b 5081 }
1cf51900 5082#endif
a5135bcf 5083 skb_reset_tc(skb);
e7246e12 5084skip_classify:
9754e293 5085 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
b4b9e355
MG
5086 goto drop;
5087
df8a39de 5088 if (skb_vlan_tag_present(skb)) {
2425717b
JF
5089 if (pt_prev) {
5090 ret = deliver_skb(skb, pt_prev, orig_dev);
5091 pt_prev = NULL;
5092 }
48cc32d3 5093 if (vlan_do_receive(&skb))
2425717b
JF
5094 goto another_round;
5095 else if (unlikely(!skb))
2c17d27c 5096 goto out;
2425717b
JF
5097 }
5098
48cc32d3 5099 rx_handler = rcu_dereference(skb->dev->rx_handler);
ab95bfe0
JP
5100 if (rx_handler) {
5101 if (pt_prev) {
5102 ret = deliver_skb(skb, pt_prev, orig_dev);
5103 pt_prev = NULL;
5104 }
8a4eb573
JP
5105 switch (rx_handler(&skb)) {
5106 case RX_HANDLER_CONSUMED:
3bc1b1ad 5107 ret = NET_RX_SUCCESS;
2c17d27c 5108 goto out;
8a4eb573 5109 case RX_HANDLER_ANOTHER:
63d8ea7f 5110 goto another_round;
8a4eb573
JP
5111 case RX_HANDLER_EXACT:
5112 deliver_exact = true;
5113 case RX_HANDLER_PASS:
5114 break;
5115 default:
5116 BUG();
5117 }
ab95bfe0 5118 }
1da177e4 5119
df8a39de 5120 if (unlikely(skb_vlan_tag_present(skb))) {
36b2f61a
GV
5121check_vlan_id:
5122 if (skb_vlan_tag_get_id(skb)) {
5123 /* Vlan id is non 0 and vlan_do_receive() above couldn't
5124 * find vlan device.
5125 */
d4b812de 5126 skb->pkt_type = PACKET_OTHERHOST;
36b2f61a
GV
5127 } else if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5128 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
5129 /* Outer header is 802.1P with vlan 0, inner header is
5130 * 802.1Q or 802.1AD and vlan_do_receive() above could
5131 * not find vlan dev for vlan id 0.
5132 */
5133 __vlan_hwaccel_clear_tag(skb);
5134 skb = skb_vlan_untag(skb);
5135 if (unlikely(!skb))
5136 goto out;
5137 if (vlan_do_receive(&skb))
5138 /* After stripping off 802.1P header with vlan 0
5139 * vlan dev is found for inner header.
5140 */
5141 goto another_round;
5142 else if (unlikely(!skb))
5143 goto out;
5144 else
5145 /* We have stripped outer 802.1P vlan 0 header.
5146 * But could not find vlan dev.
5147 * check again for vlan id to set OTHERHOST.
5148 */
5149 goto check_vlan_id;
5150 }
d4b812de
ED
5151 /* Note: we might in the future use prio bits
5152 * and set skb->priority like in vlan_do_receive()
5153 * For the time being, just ignore Priority Code Point
5154 */
b1817524 5155 __vlan_hwaccel_clear_tag(skb);
d4b812de 5156 }
48cc32d3 5157
7866a621
SN
5158 type = skb->protocol;
5159
63d8ea7f 5160 /* deliver only exact match when indicated */
7866a621
SN
5161 if (likely(!deliver_exact)) {
5162 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5163 &ptype_base[ntohs(type) &
5164 PTYPE_HASH_MASK]);
5165 }
1f3c8804 5166
7866a621
SN
5167 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5168 &orig_dev->ptype_specific);
5169
5170 if (unlikely(skb->dev != orig_dev)) {
5171 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5172 &skb->dev->ptype_specific);
1da177e4
LT
5173 }
5174
5175 if (pt_prev) {
1f8b977a 5176 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
0e698bf6 5177 goto drop;
88eb1944 5178 *ppt_prev = pt_prev;
1da177e4 5179 } else {
b4b9e355 5180drop:
6e7333d3
JW
5181 if (!deliver_exact)
5182 atomic_long_inc(&skb->dev->rx_dropped);
5183 else
5184 atomic_long_inc(&skb->dev->rx_nohandler);
1da177e4
LT
5185 kfree_skb(skb);
5186 /* Jamal, now you will not able to escape explaining
5187 * me how you were going to use this. :-)
5188 */
5189 ret = NET_RX_DROP;
5190 }
5191
2c17d27c 5192out:
9754e293
DM
5193 return ret;
5194}
5195
88eb1944
EC
5196static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
5197{
5198 struct net_device *orig_dev = skb->dev;
5199 struct packet_type *pt_prev = NULL;
5200 int ret;
5201
5202 ret = __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
5203 if (pt_prev)
f5737cba
PA
5204 ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb,
5205 skb->dev, pt_prev, orig_dev);
88eb1944
EC
5206 return ret;
5207}
5208
1c601d82
JDB
5209/**
5210 * netif_receive_skb_core - special purpose version of netif_receive_skb
5211 * @skb: buffer to process
5212 *
5213 * More direct receive version of netif_receive_skb(). It should
5214 * only be used by callers that have a need to skip RPS and Generic XDP.
5215 * Caller must also take care of handling if (page_is_)pfmemalloc.
5216 *
5217 * This function may only be called from softirq context and interrupts
5218 * should be enabled.
5219 *
5220 * Return values (usually ignored):
5221 * NET_RX_SUCCESS: no congestion
5222 * NET_RX_DROP: packet was dropped
5223 */
5224int netif_receive_skb_core(struct sk_buff *skb)
5225{
5226 int ret;
5227
5228 rcu_read_lock();
88eb1944 5229 ret = __netif_receive_skb_one_core(skb, false);
1c601d82
JDB
5230 rcu_read_unlock();
5231
5232 return ret;
5233}
5234EXPORT_SYMBOL(netif_receive_skb_core);
5235
88eb1944
EC
5236static inline void __netif_receive_skb_list_ptype(struct list_head *head,
5237 struct packet_type *pt_prev,
5238 struct net_device *orig_dev)
4ce0017a
EC
5239{
5240 struct sk_buff *skb, *next;
5241
88eb1944
EC
5242 if (!pt_prev)
5243 return;
5244 if (list_empty(head))
5245 return;
17266ee9 5246 if (pt_prev->list_func != NULL)
fdf71426
PA
5247 INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv,
5248 ip_list_rcv, head, pt_prev, orig_dev);
17266ee9 5249 else
9a5a90d1
AL
5250 list_for_each_entry_safe(skb, next, head, list) {
5251 skb_list_del_init(skb);
fdf71426 5252 pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
9a5a90d1 5253 }
88eb1944
EC
5254}
5255
5256static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
5257{
5258 /* Fast-path assumptions:
5259 * - There is no RX handler.
5260 * - Only one packet_type matches.
5261 * If either of these fails, we will end up doing some per-packet
5262 * processing in-line, then handling the 'last ptype' for the whole
5263 * sublist. This can't cause out-of-order delivery to any single ptype,
5264 * because the 'last ptype' must be constant across the sublist, and all
5265 * other ptypes are handled per-packet.
5266 */
5267 /* Current (common) ptype of sublist */
5268 struct packet_type *pt_curr = NULL;
5269 /* Current (common) orig_dev of sublist */
5270 struct net_device *od_curr = NULL;
5271 struct list_head sublist;
5272 struct sk_buff *skb, *next;
5273
9af86f93 5274 INIT_LIST_HEAD(&sublist);
88eb1944
EC
5275 list_for_each_entry_safe(skb, next, head, list) {
5276 struct net_device *orig_dev = skb->dev;
5277 struct packet_type *pt_prev = NULL;
5278
22f6bbb7 5279 skb_list_del_init(skb);
88eb1944 5280 __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
9af86f93
EC
5281 if (!pt_prev)
5282 continue;
88eb1944
EC
5283 if (pt_curr != pt_prev || od_curr != orig_dev) {
5284 /* dispatch old sublist */
88eb1944
EC
5285 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5286 /* start new sublist */
9af86f93 5287 INIT_LIST_HEAD(&sublist);
88eb1944
EC
5288 pt_curr = pt_prev;
5289 od_curr = orig_dev;
5290 }
9af86f93 5291 list_add_tail(&skb->list, &sublist);
88eb1944
EC
5292 }
5293
5294 /* dispatch final sublist */
9af86f93 5295 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
4ce0017a
EC
5296}
5297
9754e293
DM
5298static int __netif_receive_skb(struct sk_buff *skb)
5299{
5300 int ret;
5301
5302 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
f1083048 5303 unsigned int noreclaim_flag;
9754e293
DM
5304
5305 /*
5306 * PFMEMALLOC skbs are special, they should
5307 * - be delivered to SOCK_MEMALLOC sockets only
5308 * - stay away from userspace
5309 * - have bounded memory usage
5310 *
5311 * Use PF_MEMALLOC as this saves us from propagating the allocation
5312 * context down to all allocation sites.
5313 */
f1083048 5314 noreclaim_flag = memalloc_noreclaim_save();
88eb1944 5315 ret = __netif_receive_skb_one_core(skb, true);
f1083048 5316 memalloc_noreclaim_restore(noreclaim_flag);
9754e293 5317 } else
88eb1944 5318 ret = __netif_receive_skb_one_core(skb, false);
9754e293 5319
1da177e4
LT
5320 return ret;
5321}
0a9627f2 5322
4ce0017a
EC
5323static void __netif_receive_skb_list(struct list_head *head)
5324{
5325 unsigned long noreclaim_flag = 0;
5326 struct sk_buff *skb, *next;
5327 bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
5328
5329 list_for_each_entry_safe(skb, next, head, list) {
5330 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5331 struct list_head sublist;
5332
5333 /* Handle the previous sublist */
5334 list_cut_before(&sublist, head, &skb->list);
b9f463d6
EC
5335 if (!list_empty(&sublist))
5336 __netif_receive_skb_list_core(&sublist, pfmemalloc);
4ce0017a
EC
5337 pfmemalloc = !pfmemalloc;
5338 /* See comments in __netif_receive_skb */
5339 if (pfmemalloc)
5340 noreclaim_flag = memalloc_noreclaim_save();
5341 else
5342 memalloc_noreclaim_restore(noreclaim_flag);
5343 }
5344 }
5345 /* Handle the remaining sublist */
b9f463d6
EC
5346 if (!list_empty(head))
5347 __netif_receive_skb_list_core(head, pfmemalloc);
4ce0017a
EC
5348 /* Restore pflags */
5349 if (pfmemalloc)
5350 memalloc_noreclaim_restore(noreclaim_flag);
5351}
5352
f4e63525 5353static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
b5cdae32 5354{
58038695 5355 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
b5cdae32
DM
5356 struct bpf_prog *new = xdp->prog;
5357 int ret = 0;
5358
5359 switch (xdp->command) {
58038695 5360 case XDP_SETUP_PROG:
b5cdae32
DM
5361 rcu_assign_pointer(dev->xdp_prog, new);
5362 if (old)
5363 bpf_prog_put(old);
5364
5365 if (old && !new) {
02786475 5366 static_branch_dec(&generic_xdp_needed_key);
b5cdae32 5367 } else if (new && !old) {
02786475 5368 static_branch_inc(&generic_xdp_needed_key);
b5cdae32 5369 dev_disable_lro(dev);
56f5aa77 5370 dev_disable_gro_hw(dev);
b5cdae32
DM
5371 }
5372 break;
b5cdae32
DM
5373
5374 case XDP_QUERY_PROG:
58038695 5375 xdp->prog_id = old ? old->aux->id : 0;
b5cdae32
DM
5376 break;
5377
5378 default:
5379 ret = -EINVAL;
5380 break;
5381 }
5382
5383 return ret;
5384}
5385
ae78dbfa 5386static int netif_receive_skb_internal(struct sk_buff *skb)
0a9627f2 5387{
2c17d27c
JA
5388 int ret;
5389
588f0330 5390 net_timestamp_check(netdev_tstamp_prequeue, skb);
3b098e2d 5391
c1f19b51
RC
5392 if (skb_defer_rx_timestamp(skb))
5393 return NET_RX_SUCCESS;
5394
bbbe211c 5395 rcu_read_lock();
df334545 5396#ifdef CONFIG_RPS
dc05360f 5397 if (static_branch_unlikely(&rps_needed)) {
3b098e2d 5398 struct rps_dev_flow voidflow, *rflow = &voidflow;
2c17d27c 5399 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
0a9627f2 5400
3b098e2d
ED
5401 if (cpu >= 0) {
5402 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5403 rcu_read_unlock();
adc9300e 5404 return ret;
3b098e2d 5405 }
fec5e652 5406 }
1e94d72f 5407#endif
2c17d27c
JA
5408 ret = __netif_receive_skb(skb);
5409 rcu_read_unlock();
5410 return ret;
0a9627f2 5411}
ae78dbfa 5412
7da517a3
EC
5413static void netif_receive_skb_list_internal(struct list_head *head)
5414{
7da517a3 5415 struct sk_buff *skb, *next;
8c057efa 5416 struct list_head sublist;
7da517a3 5417
8c057efa 5418 INIT_LIST_HEAD(&sublist);
7da517a3
EC
5419 list_for_each_entry_safe(skb, next, head, list) {
5420 net_timestamp_check(netdev_tstamp_prequeue, skb);
22f6bbb7 5421 skb_list_del_init(skb);
8c057efa
EC
5422 if (!skb_defer_rx_timestamp(skb))
5423 list_add_tail(&skb->list, &sublist);
7da517a3 5424 }
8c057efa 5425 list_splice_init(&sublist, head);
7da517a3 5426
7da517a3
EC
5427 rcu_read_lock();
5428#ifdef CONFIG_RPS
dc05360f 5429 if (static_branch_unlikely(&rps_needed)) {
7da517a3
EC
5430 list_for_each_entry_safe(skb, next, head, list) {
5431 struct rps_dev_flow voidflow, *rflow = &voidflow;
5432 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5433
5434 if (cpu >= 0) {
8c057efa 5435 /* Will be handled, remove from list */
22f6bbb7 5436 skb_list_del_init(skb);
8c057efa 5437 enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
7da517a3
EC
5438 }
5439 }
5440 }
5441#endif
5442 __netif_receive_skb_list(head);
5443 rcu_read_unlock();
5444}
5445
ae78dbfa
BH
5446/**
5447 * netif_receive_skb - process receive buffer from network
5448 * @skb: buffer to process
5449 *
5450 * netif_receive_skb() is the main receive data processing function.
5451 * It always succeeds. The buffer may be dropped during processing
5452 * for congestion control or by the protocol layers.
5453 *
5454 * This function may only be called from softirq context and interrupts
5455 * should be enabled.
5456 *
5457 * Return values (usually ignored):
5458 * NET_RX_SUCCESS: no congestion
5459 * NET_RX_DROP: packet was dropped
5460 */
04eb4489 5461int netif_receive_skb(struct sk_buff *skb)
ae78dbfa 5462{
b0e3f1bd
GB
5463 int ret;
5464
ae78dbfa
BH
5465 trace_netif_receive_skb_entry(skb);
5466
b0e3f1bd
GB
5467 ret = netif_receive_skb_internal(skb);
5468 trace_netif_receive_skb_exit(ret);
5469
5470 return ret;
ae78dbfa 5471}
04eb4489 5472EXPORT_SYMBOL(netif_receive_skb);
1da177e4 5473
f6ad8c1b
EC
5474/**
5475 * netif_receive_skb_list - process many receive buffers from network
5476 * @head: list of skbs to process.
5477 *
7da517a3
EC
5478 * Since return value of netif_receive_skb() is normally ignored, and
5479 * wouldn't be meaningful for a list, this function returns void.
f6ad8c1b
EC
5480 *
5481 * This function may only be called from softirq context and interrupts
5482 * should be enabled.
5483 */
5484void netif_receive_skb_list(struct list_head *head)
5485{
7da517a3 5486 struct sk_buff *skb;
f6ad8c1b 5487
b9f463d6
EC
5488 if (list_empty(head))
5489 return;
b0e3f1bd
GB
5490 if (trace_netif_receive_skb_list_entry_enabled()) {
5491 list_for_each_entry(skb, head, list)
5492 trace_netif_receive_skb_list_entry(skb);
5493 }
7da517a3 5494 netif_receive_skb_list_internal(head);
b0e3f1bd 5495 trace_netif_receive_skb_list_exit(0);
f6ad8c1b
EC
5496}
5497EXPORT_SYMBOL(netif_receive_skb_list);
5498
41852497 5499DEFINE_PER_CPU(struct work_struct, flush_works);
145dd5f9
PA
5500
5501/* Network device is going away, flush any packets still pending */
5502static void flush_backlog(struct work_struct *work)
6e583ce5 5503{
6e583ce5 5504 struct sk_buff *skb, *tmp;
145dd5f9
PA
5505 struct softnet_data *sd;
5506
5507 local_bh_disable();
5508 sd = this_cpu_ptr(&softnet_data);
6e583ce5 5509
145dd5f9 5510 local_irq_disable();
e36fa2f7 5511 rps_lock(sd);
6e7676c1 5512 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
41852497 5513 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
e36fa2f7 5514 __skb_unlink(skb, &sd->input_pkt_queue);
6e583ce5 5515 kfree_skb(skb);
76cc8b13 5516 input_queue_head_incr(sd);
6e583ce5 5517 }
6e7676c1 5518 }
e36fa2f7 5519 rps_unlock(sd);
145dd5f9 5520 local_irq_enable();
6e7676c1
CG
5521
5522 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
41852497 5523 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
6e7676c1
CG
5524 __skb_unlink(skb, &sd->process_queue);
5525 kfree_skb(skb);
76cc8b13 5526 input_queue_head_incr(sd);
6e7676c1
CG
5527 }
5528 }
145dd5f9
PA
5529 local_bh_enable();
5530}
5531
41852497 5532static void flush_all_backlogs(void)
145dd5f9
PA
5533{
5534 unsigned int cpu;
5535
5536 get_online_cpus();
5537
41852497
ED
5538 for_each_online_cpu(cpu)
5539 queue_work_on(cpu, system_highpri_wq,
5540 per_cpu_ptr(&flush_works, cpu));
145dd5f9
PA
5541
5542 for_each_online_cpu(cpu)
41852497 5543 flush_work(per_cpu_ptr(&flush_works, cpu));
145dd5f9
PA
5544
5545 put_online_cpus();
6e583ce5
SH
5546}
5547
c8079432
MM
5548/* Pass the currently batched GRO_NORMAL SKBs up to the stack. */
5549static void gro_normal_list(struct napi_struct *napi)
5550{
5551 if (!napi->rx_count)
5552 return;
5553 netif_receive_skb_list_internal(&napi->rx_list);
5554 INIT_LIST_HEAD(&napi->rx_list);
5555 napi->rx_count = 0;
5556}
5557
5558/* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded,
5559 * pass the whole batch up to the stack.
5560 */
5561static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb)
5562{
5563 list_add_tail(&skb->list, &napi->rx_list);
5564 if (++napi->rx_count >= gro_normal_batch)
5565 gro_normal_list(napi);
5566}
5567
aaa5d90b
PA
5568INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int));
5569INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int));
c8079432 5570static int napi_gro_complete(struct napi_struct *napi, struct sk_buff *skb)
d565b0a1 5571{
22061d80 5572 struct packet_offload *ptype;
d565b0a1 5573 __be16 type = skb->protocol;
22061d80 5574 struct list_head *head = &offload_base;
d565b0a1
HX
5575 int err = -ENOENT;
5576
c3c7c254
ED
5577 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
5578
fc59f9a3
HX
5579 if (NAPI_GRO_CB(skb)->count == 1) {
5580 skb_shinfo(skb)->gso_size = 0;
d565b0a1 5581 goto out;
fc59f9a3 5582 }
d565b0a1
HX
5583
5584 rcu_read_lock();
5585 list_for_each_entry_rcu(ptype, head, list) {
f191a1d1 5586 if (ptype->type != type || !ptype->callbacks.gro_complete)
d565b0a1
HX
5587 continue;
5588
aaa5d90b
PA
5589 err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
5590 ipv6_gro_complete, inet_gro_complete,
5591 skb, 0);
d565b0a1
HX
5592 break;
5593 }
5594 rcu_read_unlock();
5595
5596 if (err) {
5597 WARN_ON(&ptype->list == head);
5598 kfree_skb(skb);
5599 return NET_RX_SUCCESS;
5600 }
5601
5602out:
c8079432
MM
5603 gro_normal_one(napi, skb);
5604 return NET_RX_SUCCESS;
d565b0a1
HX
5605}
5606
6312fe77 5607static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
07d78363 5608 bool flush_old)
d565b0a1 5609{
6312fe77 5610 struct list_head *head = &napi->gro_hash[index].list;
d4546c25 5611 struct sk_buff *skb, *p;
2e71a6f8 5612
07d78363 5613 list_for_each_entry_safe_reverse(skb, p, head, list) {
2e71a6f8
ED
5614 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
5615 return;
992cba7e 5616 skb_list_del_init(skb);
c8079432 5617 napi_gro_complete(napi, skb);
6312fe77 5618 napi->gro_hash[index].count--;
d565b0a1 5619 }
d9f37d01
LR
5620
5621 if (!napi->gro_hash[index].count)
5622 __clear_bit(index, &napi->gro_bitmask);
d565b0a1 5623}
07d78363 5624
6312fe77 5625/* napi->gro_hash[].list contains packets ordered by age.
07d78363
DM
5626 * youngest packets at the head of it.
5627 * Complete skbs in reverse order to reduce latencies.
5628 */
5629void napi_gro_flush(struct napi_struct *napi, bool flush_old)
5630{
42519ede
ED
5631 unsigned long bitmask = napi->gro_bitmask;
5632 unsigned int i, base = ~0U;
07d78363 5633
42519ede
ED
5634 while ((i = ffs(bitmask)) != 0) {
5635 bitmask >>= i;
5636 base += i;
5637 __napi_gro_flush_chain(napi, base, flush_old);
d9f37d01 5638 }
07d78363 5639}
86cac58b 5640EXPORT_SYMBOL(napi_gro_flush);
d565b0a1 5641
07d78363
DM
5642static struct list_head *gro_list_prepare(struct napi_struct *napi,
5643 struct sk_buff *skb)
89c5fa33 5644{
89c5fa33 5645 unsigned int maclen = skb->dev->hard_header_len;
0b4cec8c 5646 u32 hash = skb_get_hash_raw(skb);
07d78363 5647 struct list_head *head;
d4546c25 5648 struct sk_buff *p;
89c5fa33 5649
6312fe77 5650 head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list;
07d78363 5651 list_for_each_entry(p, head, list) {
89c5fa33
ED
5652 unsigned long diffs;
5653
0b4cec8c
TH
5654 NAPI_GRO_CB(p)->flush = 0;
5655
5656 if (hash != skb_get_hash_raw(p)) {
5657 NAPI_GRO_CB(p)->same_flow = 0;
5658 continue;
5659 }
5660
89c5fa33 5661 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
b1817524
MM
5662 diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb);
5663 if (skb_vlan_tag_present(p))
fc5141cb 5664 diffs |= skb_vlan_tag_get(p) ^ skb_vlan_tag_get(skb);
ce87fc6c 5665 diffs |= skb_metadata_dst_cmp(p, skb);
de8f3a83 5666 diffs |= skb_metadata_differs(p, skb);
89c5fa33
ED
5667 if (maclen == ETH_HLEN)
5668 diffs |= compare_ether_header(skb_mac_header(p),
a50e233c 5669 skb_mac_header(skb));
89c5fa33
ED
5670 else if (!diffs)
5671 diffs = memcmp(skb_mac_header(p),
a50e233c 5672 skb_mac_header(skb),
89c5fa33
ED
5673 maclen);
5674 NAPI_GRO_CB(p)->same_flow = !diffs;
89c5fa33 5675 }
07d78363
DM
5676
5677 return head;
89c5fa33
ED
5678}
5679
299603e8
JC
5680static void skb_gro_reset_offset(struct sk_buff *skb)
5681{
5682 const struct skb_shared_info *pinfo = skb_shinfo(skb);
5683 const skb_frag_t *frag0 = &pinfo->frags[0];
5684
5685 NAPI_GRO_CB(skb)->data_offset = 0;
5686 NAPI_GRO_CB(skb)->frag0 = NULL;
5687 NAPI_GRO_CB(skb)->frag0_len = 0;
5688
8aef998d 5689 if (!skb_headlen(skb) && pinfo->nr_frags &&
299603e8
JC
5690 !PageHighMem(skb_frag_page(frag0))) {
5691 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
7cfd5fd5
ED
5692 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
5693 skb_frag_size(frag0),
5694 skb->end - skb->tail);
89c5fa33
ED
5695 }
5696}
5697
a50e233c
ED
5698static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
5699{
5700 struct skb_shared_info *pinfo = skb_shinfo(skb);
5701
5702 BUG_ON(skb->end - skb->tail < grow);
5703
5704 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
5705
5706 skb->data_len -= grow;
5707 skb->tail += grow;
5708
b54c9d5b 5709 skb_frag_off_add(&pinfo->frags[0], grow);
a50e233c
ED
5710 skb_frag_size_sub(&pinfo->frags[0], grow);
5711
5712 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
5713 skb_frag_unref(skb, 0);
5714 memmove(pinfo->frags, pinfo->frags + 1,
5715 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
5716 }
5717}
5718
c8079432 5719static void gro_flush_oldest(struct napi_struct *napi, struct list_head *head)
07d78363 5720{
6312fe77 5721 struct sk_buff *oldest;
07d78363 5722
6312fe77 5723 oldest = list_last_entry(head, struct sk_buff, list);
07d78363 5724
6312fe77 5725 /* We are called with head length >= MAX_GRO_SKBS, so this is
07d78363
DM
5726 * impossible.
5727 */
5728 if (WARN_ON_ONCE(!oldest))
5729 return;
5730
d9f37d01
LR
5731 /* Do not adjust napi->gro_hash[].count, caller is adding a new
5732 * SKB to the chain.
07d78363 5733 */
ece23711 5734 skb_list_del_init(oldest);
c8079432 5735 napi_gro_complete(napi, oldest);
07d78363
DM
5736}
5737
aaa5d90b
PA
5738INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *,
5739 struct sk_buff *));
5740INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *,
5741 struct sk_buff *));
bb728820 5742static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
d565b0a1 5743{
6312fe77 5744 u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
d4546c25 5745 struct list_head *head = &offload_base;
22061d80 5746 struct packet_offload *ptype;
d565b0a1 5747 __be16 type = skb->protocol;
07d78363 5748 struct list_head *gro_head;
d4546c25 5749 struct sk_buff *pp = NULL;
5b252f0c 5750 enum gro_result ret;
d4546c25 5751 int same_flow;
a50e233c 5752 int grow;
d565b0a1 5753
b5cdae32 5754 if (netif_elide_gro(skb->dev))
d565b0a1
HX
5755 goto normal;
5756
07d78363 5757 gro_head = gro_list_prepare(napi, skb);
89c5fa33 5758
d565b0a1
HX
5759 rcu_read_lock();
5760 list_for_each_entry_rcu(ptype, head, list) {
f191a1d1 5761 if (ptype->type != type || !ptype->callbacks.gro_receive)
d565b0a1
HX
5762 continue;
5763
86911732 5764 skb_set_network_header(skb, skb_gro_offset(skb));
efd9450e 5765 skb_reset_mac_len(skb);
d565b0a1 5766 NAPI_GRO_CB(skb)->same_flow = 0;
d61d072e 5767 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
5d38a079 5768 NAPI_GRO_CB(skb)->free = 0;
fac8e0f5 5769 NAPI_GRO_CB(skb)->encap_mark = 0;
fcd91dd4 5770 NAPI_GRO_CB(skb)->recursion_counter = 0;
a0ca153f 5771 NAPI_GRO_CB(skb)->is_fou = 0;
1530545e 5772 NAPI_GRO_CB(skb)->is_atomic = 1;
15e2396d 5773 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
d565b0a1 5774
662880f4
TH
5775 /* Setup for GRO checksum validation */
5776 switch (skb->ip_summed) {
5777 case CHECKSUM_COMPLETE:
5778 NAPI_GRO_CB(skb)->csum = skb->csum;
5779 NAPI_GRO_CB(skb)->csum_valid = 1;
5780 NAPI_GRO_CB(skb)->csum_cnt = 0;
5781 break;
5782 case CHECKSUM_UNNECESSARY:
5783 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
5784 NAPI_GRO_CB(skb)->csum_valid = 0;
5785 break;
5786 default:
5787 NAPI_GRO_CB(skb)->csum_cnt = 0;
5788 NAPI_GRO_CB(skb)->csum_valid = 0;
5789 }
d565b0a1 5790
aaa5d90b
PA
5791 pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive,
5792 ipv6_gro_receive, inet_gro_receive,
5793 gro_head, skb);
d565b0a1
HX
5794 break;
5795 }
5796 rcu_read_unlock();
5797
5798 if (&ptype->list == head)
5799 goto normal;
5800
45586c70 5801 if (PTR_ERR(pp) == -EINPROGRESS) {
25393d3f
SK
5802 ret = GRO_CONSUMED;
5803 goto ok;
5804 }
5805
0da2afd5 5806 same_flow = NAPI_GRO_CB(skb)->same_flow;
5d0d9be8 5807 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
0da2afd5 5808
d565b0a1 5809 if (pp) {
992cba7e 5810 skb_list_del_init(pp);
c8079432 5811 napi_gro_complete(napi, pp);
6312fe77 5812 napi->gro_hash[hash].count--;
d565b0a1
HX
5813 }
5814
0da2afd5 5815 if (same_flow)
d565b0a1
HX
5816 goto ok;
5817
600adc18 5818 if (NAPI_GRO_CB(skb)->flush)
d565b0a1 5819 goto normal;
d565b0a1 5820
6312fe77 5821 if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) {
c8079432 5822 gro_flush_oldest(napi, gro_head);
600adc18 5823 } else {
6312fe77 5824 napi->gro_hash[hash].count++;
600adc18 5825 }
d565b0a1 5826 NAPI_GRO_CB(skb)->count = 1;
2e71a6f8 5827 NAPI_GRO_CB(skb)->age = jiffies;
29e98242 5828 NAPI_GRO_CB(skb)->last = skb;
86911732 5829 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
07d78363 5830 list_add(&skb->list, gro_head);
5d0d9be8 5831 ret = GRO_HELD;
d565b0a1 5832
ad0f9904 5833pull:
a50e233c
ED
5834 grow = skb_gro_offset(skb) - skb_headlen(skb);
5835 if (grow > 0)
5836 gro_pull_from_frag0(skb, grow);
d565b0a1 5837ok:
d9f37d01
LR
5838 if (napi->gro_hash[hash].count) {
5839 if (!test_bit(hash, &napi->gro_bitmask))
5840 __set_bit(hash, &napi->gro_bitmask);
5841 } else if (test_bit(hash, &napi->gro_bitmask)) {
5842 __clear_bit(hash, &napi->gro_bitmask);
5843 }
5844
5d0d9be8 5845 return ret;
d565b0a1
HX
5846
5847normal:
ad0f9904
HX
5848 ret = GRO_NORMAL;
5849 goto pull;
5d38a079 5850}
96e93eab 5851
bf5a755f
JC
5852struct packet_offload *gro_find_receive_by_type(__be16 type)
5853{
5854 struct list_head *offload_head = &offload_base;
5855 struct packet_offload *ptype;
5856
5857 list_for_each_entry_rcu(ptype, offload_head, list) {
5858 if (ptype->type != type || !ptype->callbacks.gro_receive)
5859 continue;
5860 return ptype;
5861 }
5862 return NULL;
5863}
e27a2f83 5864EXPORT_SYMBOL(gro_find_receive_by_type);
bf5a755f
JC
5865
5866struct packet_offload *gro_find_complete_by_type(__be16 type)
5867{
5868 struct list_head *offload_head = &offload_base;
5869 struct packet_offload *ptype;
5870
5871 list_for_each_entry_rcu(ptype, offload_head, list) {
5872 if (ptype->type != type || !ptype->callbacks.gro_complete)
5873 continue;
5874 return ptype;
5875 }
5876 return NULL;
5877}
e27a2f83 5878EXPORT_SYMBOL(gro_find_complete_by_type);
5d38a079 5879
e44699d2
MK
5880static void napi_skb_free_stolen_head(struct sk_buff *skb)
5881{
5882 skb_dst_drop(skb);
174e2381 5883 skb_ext_put(skb);
e44699d2
MK
5884 kmem_cache_free(skbuff_head_cache, skb);
5885}
5886
6570bc79
AL
5887static gro_result_t napi_skb_finish(struct napi_struct *napi,
5888 struct sk_buff *skb,
5889 gro_result_t ret)
5d38a079 5890{
5d0d9be8
HX
5891 switch (ret) {
5892 case GRO_NORMAL:
6570bc79 5893 gro_normal_one(napi, skb);
c7c4b3b6 5894 break;
5d38a079 5895
5d0d9be8 5896 case GRO_DROP:
5d38a079
HX
5897 kfree_skb(skb);
5898 break;
5b252f0c 5899
daa86548 5900 case GRO_MERGED_FREE:
e44699d2
MK
5901 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5902 napi_skb_free_stolen_head(skb);
5903 else
d7e8883c 5904 __kfree_skb(skb);
daa86548
ED
5905 break;
5906
5b252f0c
BH
5907 case GRO_HELD:
5908 case GRO_MERGED:
25393d3f 5909 case GRO_CONSUMED:
5b252f0c 5910 break;
5d38a079
HX
5911 }
5912
c7c4b3b6 5913 return ret;
5d0d9be8 5914}
5d0d9be8 5915
c7c4b3b6 5916gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
5d0d9be8 5917{
b0e3f1bd
GB
5918 gro_result_t ret;
5919
93f93a44 5920 skb_mark_napi_id(skb, napi);
ae78dbfa 5921 trace_napi_gro_receive_entry(skb);
86911732 5922
a50e233c
ED
5923 skb_gro_reset_offset(skb);
5924
6570bc79 5925 ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb));
b0e3f1bd
GB
5926 trace_napi_gro_receive_exit(ret);
5927
5928 return ret;
d565b0a1
HX
5929}
5930EXPORT_SYMBOL(napi_gro_receive);
5931
d0c2b0d2 5932static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
96e93eab 5933{
93a35f59
ED
5934 if (unlikely(skb->pfmemalloc)) {
5935 consume_skb(skb);
5936 return;
5937 }
96e93eab 5938 __skb_pull(skb, skb_headlen(skb));
2a2a459e
ED
5939 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
5940 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
b1817524 5941 __vlan_hwaccel_clear_tag(skb);
66c46d74 5942 skb->dev = napi->dev;
6d152e23 5943 skb->skb_iif = 0;
33d9a2c7
ED
5944
5945 /* eth_type_trans() assumes pkt_type is PACKET_HOST */
5946 skb->pkt_type = PACKET_HOST;
5947
c3caf119
JC
5948 skb->encapsulation = 0;
5949 skb_shinfo(skb)->gso_type = 0;
e33d0ba8 5950 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
174e2381 5951 skb_ext_reset(skb);
96e93eab
HX
5952
5953 napi->skb = skb;
5954}
96e93eab 5955
76620aaf 5956struct sk_buff *napi_get_frags(struct napi_struct *napi)
5d38a079 5957{
5d38a079 5958 struct sk_buff *skb = napi->skb;
5d38a079
HX
5959
5960 if (!skb) {
fd11a83d 5961 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
e2f9dc3b
ED
5962 if (skb) {
5963 napi->skb = skb;
5964 skb_mark_napi_id(skb, napi);
5965 }
80595d59 5966 }
96e93eab
HX
5967 return skb;
5968}
76620aaf 5969EXPORT_SYMBOL(napi_get_frags);
96e93eab 5970
a50e233c
ED
5971static gro_result_t napi_frags_finish(struct napi_struct *napi,
5972 struct sk_buff *skb,
5973 gro_result_t ret)
96e93eab 5974{
5d0d9be8
HX
5975 switch (ret) {
5976 case GRO_NORMAL:
a50e233c
ED
5977 case GRO_HELD:
5978 __skb_push(skb, ETH_HLEN);
5979 skb->protocol = eth_type_trans(skb, skb->dev);
323ebb61
EC
5980 if (ret == GRO_NORMAL)
5981 gro_normal_one(napi, skb);
86911732 5982 break;
5d38a079 5983
5d0d9be8 5984 case GRO_DROP:
5d0d9be8
HX
5985 napi_reuse_skb(napi, skb);
5986 break;
5b252f0c 5987
e44699d2
MK
5988 case GRO_MERGED_FREE:
5989 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5990 napi_skb_free_stolen_head(skb);
5991 else
5992 napi_reuse_skb(napi, skb);
5993 break;
5994
5b252f0c 5995 case GRO_MERGED:
25393d3f 5996 case GRO_CONSUMED:
5b252f0c 5997 break;
5d0d9be8 5998 }
5d38a079 5999
c7c4b3b6 6000 return ret;
5d38a079 6001}
5d0d9be8 6002
a50e233c
ED
6003/* Upper GRO stack assumes network header starts at gro_offset=0
6004 * Drivers could call both napi_gro_frags() and napi_gro_receive()
6005 * We copy ethernet header into skb->data to have a common layout.
6006 */
4adb9c4a 6007static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
76620aaf
HX
6008{
6009 struct sk_buff *skb = napi->skb;
a50e233c
ED
6010 const struct ethhdr *eth;
6011 unsigned int hlen = sizeof(*eth);
76620aaf
HX
6012
6013 napi->skb = NULL;
6014
a50e233c
ED
6015 skb_reset_mac_header(skb);
6016 skb_gro_reset_offset(skb);
6017
a50e233c
ED
6018 if (unlikely(skb_gro_header_hard(skb, hlen))) {
6019 eth = skb_gro_header_slow(skb, hlen, 0);
6020 if (unlikely(!eth)) {
4da46ceb
AC
6021 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
6022 __func__, napi->dev->name);
a50e233c
ED
6023 napi_reuse_skb(napi, skb);
6024 return NULL;
6025 }
6026 } else {
a4270d67 6027 eth = (const struct ethhdr *)skb->data;
a50e233c
ED
6028 gro_pull_from_frag0(skb, hlen);
6029 NAPI_GRO_CB(skb)->frag0 += hlen;
6030 NAPI_GRO_CB(skb)->frag0_len -= hlen;
76620aaf 6031 }
a50e233c
ED
6032 __skb_pull(skb, hlen);
6033
6034 /*
6035 * This works because the only protocols we care about don't require
6036 * special handling.
6037 * We'll fix it up properly in napi_frags_finish()
6038 */
6039 skb->protocol = eth->h_proto;
76620aaf 6040
76620aaf
HX
6041 return skb;
6042}
76620aaf 6043
c7c4b3b6 6044gro_result_t napi_gro_frags(struct napi_struct *napi)
5d0d9be8 6045{
b0e3f1bd 6046 gro_result_t ret;
76620aaf 6047 struct sk_buff *skb = napi_frags_skb(napi);
5d0d9be8
HX
6048
6049 if (!skb)
c7c4b3b6 6050 return GRO_DROP;
5d0d9be8 6051
ae78dbfa
BH
6052 trace_napi_gro_frags_entry(skb);
6053
b0e3f1bd
GB
6054 ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
6055 trace_napi_gro_frags_exit(ret);
6056
6057 return ret;
5d0d9be8 6058}
5d38a079
HX
6059EXPORT_SYMBOL(napi_gro_frags);
6060
573e8fca
TH
6061/* Compute the checksum from gro_offset and return the folded value
6062 * after adding in any pseudo checksum.
6063 */
6064__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
6065{
6066 __wsum wsum;
6067 __sum16 sum;
6068
6069 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
6070
6071 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
6072 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
14641931 6073 /* See comments in __skb_checksum_complete(). */
573e8fca
TH
6074 if (likely(!sum)) {
6075 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
6076 !skb->csum_complete_sw)
7fe50ac8 6077 netdev_rx_csum_fault(skb->dev, skb);
573e8fca
TH
6078 }
6079
6080 NAPI_GRO_CB(skb)->csum = wsum;
6081 NAPI_GRO_CB(skb)->csum_valid = 1;
6082
6083 return sum;
6084}
6085EXPORT_SYMBOL(__skb_gro_checksum_complete);
6086
773fc8f6 6087static void net_rps_send_ipi(struct softnet_data *remsd)
6088{
6089#ifdef CONFIG_RPS
6090 while (remsd) {
6091 struct softnet_data *next = remsd->rps_ipi_next;
6092
6093 if (cpu_online(remsd->cpu))
6094 smp_call_function_single_async(remsd->cpu, &remsd->csd);
6095 remsd = next;
6096 }
6097#endif
6098}
6099
e326bed2 6100/*
855abcf0 6101 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
e326bed2
ED
6102 * Note: called with local irq disabled, but exits with local irq enabled.
6103 */
6104static void net_rps_action_and_irq_enable(struct softnet_data *sd)
6105{
6106#ifdef CONFIG_RPS
6107 struct softnet_data *remsd = sd->rps_ipi_list;
6108
6109 if (remsd) {
6110 sd->rps_ipi_list = NULL;
6111
6112 local_irq_enable();
6113
6114 /* Send pending IPI's to kick RPS processing on remote cpus. */
773fc8f6 6115 net_rps_send_ipi(remsd);
e326bed2
ED
6116 } else
6117#endif
6118 local_irq_enable();
6119}
6120
d75b1ade
ED
6121static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
6122{
6123#ifdef CONFIG_RPS
6124 return sd->rps_ipi_list != NULL;
6125#else
6126 return false;
6127#endif
6128}
6129
bea3348e 6130static int process_backlog(struct napi_struct *napi, int quota)
1da177e4 6131{
eecfd7c4 6132 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
145dd5f9
PA
6133 bool again = true;
6134 int work = 0;
1da177e4 6135
e326bed2
ED
6136 /* Check if we have pending ipi, its better to send them now,
6137 * not waiting net_rx_action() end.
6138 */
d75b1ade 6139 if (sd_has_rps_ipi_waiting(sd)) {
e326bed2
ED
6140 local_irq_disable();
6141 net_rps_action_and_irq_enable(sd);
6142 }
d75b1ade 6143
3d48b53f 6144 napi->weight = dev_rx_weight;
145dd5f9 6145 while (again) {
1da177e4 6146 struct sk_buff *skb;
6e7676c1
CG
6147
6148 while ((skb = __skb_dequeue(&sd->process_queue))) {
2c17d27c 6149 rcu_read_lock();
6e7676c1 6150 __netif_receive_skb(skb);
2c17d27c 6151 rcu_read_unlock();
76cc8b13 6152 input_queue_head_incr(sd);
145dd5f9 6153 if (++work >= quota)
76cc8b13 6154 return work;
145dd5f9 6155
6e7676c1 6156 }
1da177e4 6157
145dd5f9 6158 local_irq_disable();
e36fa2f7 6159 rps_lock(sd);
11ef7a89 6160 if (skb_queue_empty(&sd->input_pkt_queue)) {
eecfd7c4
ED
6161 /*
6162 * Inline a custom version of __napi_complete().
6163 * only current cpu owns and manipulates this napi,
11ef7a89
TH
6164 * and NAPI_STATE_SCHED is the only possible flag set
6165 * on backlog.
6166 * We can use a plain write instead of clear_bit(),
eecfd7c4
ED
6167 * and we dont need an smp_mb() memory barrier.
6168 */
eecfd7c4 6169 napi->state = 0;
145dd5f9
PA
6170 again = false;
6171 } else {
6172 skb_queue_splice_tail_init(&sd->input_pkt_queue,
6173 &sd->process_queue);
bea3348e 6174 }
e36fa2f7 6175 rps_unlock(sd);
145dd5f9 6176 local_irq_enable();
6e7676c1 6177 }
1da177e4 6178
bea3348e
SH
6179 return work;
6180}
1da177e4 6181
bea3348e
SH
6182/**
6183 * __napi_schedule - schedule for receive
c4ea43c5 6184 * @n: entry to schedule
bea3348e 6185 *
bc9ad166
ED
6186 * The entry's receive function will be scheduled to run.
6187 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
bea3348e 6188 */
b5606c2d 6189void __napi_schedule(struct napi_struct *n)
bea3348e
SH
6190{
6191 unsigned long flags;
1da177e4 6192
bea3348e 6193 local_irq_save(flags);
903ceff7 6194 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
bea3348e 6195 local_irq_restore(flags);
1da177e4 6196}
bea3348e
SH
6197EXPORT_SYMBOL(__napi_schedule);
6198
39e6c820
ED
6199/**
6200 * napi_schedule_prep - check if napi can be scheduled
6201 * @n: napi context
6202 *
6203 * Test if NAPI routine is already running, and if not mark
6204 * it as running. This is used as a condition variable
6205 * insure only one NAPI poll instance runs. We also make
6206 * sure there is no pending NAPI disable.
6207 */
6208bool napi_schedule_prep(struct napi_struct *n)
6209{
6210 unsigned long val, new;
6211
6212 do {
6213 val = READ_ONCE(n->state);
6214 if (unlikely(val & NAPIF_STATE_DISABLE))
6215 return false;
6216 new = val | NAPIF_STATE_SCHED;
6217
6218 /* Sets STATE_MISSED bit if STATE_SCHED was already set
6219 * This was suggested by Alexander Duyck, as compiler
6220 * emits better code than :
6221 * if (val & NAPIF_STATE_SCHED)
6222 * new |= NAPIF_STATE_MISSED;
6223 */
6224 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
6225 NAPIF_STATE_MISSED;
6226 } while (cmpxchg(&n->state, val, new) != val);
6227
6228 return !(val & NAPIF_STATE_SCHED);
6229}
6230EXPORT_SYMBOL(napi_schedule_prep);
6231
bc9ad166
ED
6232/**
6233 * __napi_schedule_irqoff - schedule for receive
6234 * @n: entry to schedule
6235 *
6236 * Variant of __napi_schedule() assuming hard irqs are masked
6237 */
6238void __napi_schedule_irqoff(struct napi_struct *n)
6239{
6240 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
6241}
6242EXPORT_SYMBOL(__napi_schedule_irqoff);
6243
364b6055 6244bool napi_complete_done(struct napi_struct *n, int work_done)
d565b0a1 6245{
39e6c820 6246 unsigned long flags, val, new;
d565b0a1
HX
6247
6248 /*
217f6974
ED
6249 * 1) Don't let napi dequeue from the cpu poll list
6250 * just in case its running on a different cpu.
6251 * 2) If we are busy polling, do nothing here, we have
6252 * the guarantee we will be called later.
d565b0a1 6253 */
217f6974
ED
6254 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
6255 NAPIF_STATE_IN_BUSY_POLL)))
364b6055 6256 return false;
d565b0a1 6257
d9f37d01 6258 if (n->gro_bitmask) {
3b47d303 6259 unsigned long timeout = 0;
d75b1ade 6260
3b47d303
ED
6261 if (work_done)
6262 timeout = n->dev->gro_flush_timeout;
6263
605108ac
PA
6264 /* When the NAPI instance uses a timeout and keeps postponing
6265 * it, we need to bound somehow the time packets are kept in
6266 * the GRO layer
6267 */
6268 napi_gro_flush(n, !!timeout);
3b47d303
ED
6269 if (timeout)
6270 hrtimer_start(&n->timer, ns_to_ktime(timeout),
6271 HRTIMER_MODE_REL_PINNED);
3b47d303 6272 }
c8079432
MM
6273
6274 gro_normal_list(n);
6275
02c1602e 6276 if (unlikely(!list_empty(&n->poll_list))) {
d75b1ade
ED
6277 /* If n->poll_list is not empty, we need to mask irqs */
6278 local_irq_save(flags);
02c1602e 6279 list_del_init(&n->poll_list);
d75b1ade
ED
6280 local_irq_restore(flags);
6281 }
39e6c820
ED
6282
6283 do {
6284 val = READ_ONCE(n->state);
6285
6286 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
6287
6288 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED);
6289
6290 /* If STATE_MISSED was set, leave STATE_SCHED set,
6291 * because we will call napi->poll() one more time.
6292 * This C code was suggested by Alexander Duyck to help gcc.
6293 */
6294 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
6295 NAPIF_STATE_SCHED;
6296 } while (cmpxchg(&n->state, val, new) != val);
6297
6298 if (unlikely(val & NAPIF_STATE_MISSED)) {
6299 __napi_schedule(n);
6300 return false;
6301 }
6302
364b6055 6303 return true;
d565b0a1 6304}
3b47d303 6305EXPORT_SYMBOL(napi_complete_done);
d565b0a1 6306
af12fa6e 6307/* must be called under rcu_read_lock(), as we dont take a reference */
02d62e86 6308static struct napi_struct *napi_by_id(unsigned int napi_id)
af12fa6e
ET
6309{
6310 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6311 struct napi_struct *napi;
6312
6313 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6314 if (napi->napi_id == napi_id)
6315 return napi;
6316
6317 return NULL;
6318}
02d62e86
ED
6319
6320#if defined(CONFIG_NET_RX_BUSY_POLL)
217f6974 6321
ce6aea93 6322#define BUSY_POLL_BUDGET 8
217f6974
ED
6323
6324static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock)
6325{
6326 int rc;
6327
39e6c820
ED
6328 /* Busy polling means there is a high chance device driver hard irq
6329 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6330 * set in napi_schedule_prep().
6331 * Since we are about to call napi->poll() once more, we can safely
6332 * clear NAPI_STATE_MISSED.
6333 *
6334 * Note: x86 could use a single "lock and ..." instruction
6335 * to perform these two clear_bit()
6336 */
6337 clear_bit(NAPI_STATE_MISSED, &napi->state);
217f6974
ED
6338 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6339
6340 local_bh_disable();
6341
6342 /* All we really want here is to re-enable device interrupts.
6343 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6344 */
6345 rc = napi->poll(napi, BUSY_POLL_BUDGET);
323ebb61
EC
6346 /* We can't gro_normal_list() here, because napi->poll() might have
6347 * rearmed the napi (napi_complete_done()) in which case it could
6348 * already be running on another CPU.
6349 */
1e22391e 6350 trace_napi_poll(napi, rc, BUSY_POLL_BUDGET);
217f6974 6351 netpoll_poll_unlock(have_poll_lock);
323ebb61
EC
6352 if (rc == BUSY_POLL_BUDGET) {
6353 /* As the whole budget was spent, we still own the napi so can
6354 * safely handle the rx_list.
6355 */
6356 gro_normal_list(napi);
217f6974 6357 __napi_schedule(napi);
323ebb61 6358 }
217f6974 6359 local_bh_enable();
217f6974
ED
6360}
6361
7db6b048
SS
6362void napi_busy_loop(unsigned int napi_id,
6363 bool (*loop_end)(void *, unsigned long),
6364 void *loop_end_arg)
02d62e86 6365{
7db6b048 6366 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
217f6974 6367 int (*napi_poll)(struct napi_struct *napi, int budget);
217f6974 6368 void *have_poll_lock = NULL;
02d62e86 6369 struct napi_struct *napi;
217f6974
ED
6370
6371restart:
217f6974 6372 napi_poll = NULL;
02d62e86 6373
2a028ecb 6374 rcu_read_lock();
02d62e86 6375
545cd5e5 6376 napi = napi_by_id(napi_id);
02d62e86
ED
6377 if (!napi)
6378 goto out;
6379
217f6974
ED
6380 preempt_disable();
6381 for (;;) {
2b5cd0df
AD
6382 int work = 0;
6383
2a028ecb 6384 local_bh_disable();
217f6974
ED
6385 if (!napi_poll) {
6386 unsigned long val = READ_ONCE(napi->state);
6387
6388 /* If multiple threads are competing for this napi,
6389 * we avoid dirtying napi->state as much as we can.
6390 */
6391 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
6392 NAPIF_STATE_IN_BUSY_POLL))
6393 goto count;
6394 if (cmpxchg(&napi->state, val,
6395 val | NAPIF_STATE_IN_BUSY_POLL |
6396 NAPIF_STATE_SCHED) != val)
6397 goto count;
6398 have_poll_lock = netpoll_poll_lock(napi);
6399 napi_poll = napi->poll;
6400 }
2b5cd0df
AD
6401 work = napi_poll(napi, BUSY_POLL_BUDGET);
6402 trace_napi_poll(napi, work, BUSY_POLL_BUDGET);
323ebb61 6403 gro_normal_list(napi);
217f6974 6404count:
2b5cd0df 6405 if (work > 0)
7db6b048 6406 __NET_ADD_STATS(dev_net(napi->dev),
2b5cd0df 6407 LINUX_MIB_BUSYPOLLRXPACKETS, work);
2a028ecb 6408 local_bh_enable();
02d62e86 6409
7db6b048 6410 if (!loop_end || loop_end(loop_end_arg, start_time))
217f6974 6411 break;
02d62e86 6412
217f6974
ED
6413 if (unlikely(need_resched())) {
6414 if (napi_poll)
6415 busy_poll_stop(napi, have_poll_lock);
6416 preempt_enable();
6417 rcu_read_unlock();
6418 cond_resched();
7db6b048 6419 if (loop_end(loop_end_arg, start_time))
2b5cd0df 6420 return;
217f6974
ED
6421 goto restart;
6422 }
6cdf89b1 6423 cpu_relax();
217f6974
ED
6424 }
6425 if (napi_poll)
6426 busy_poll_stop(napi, have_poll_lock);
6427 preempt_enable();
02d62e86 6428out:
2a028ecb 6429 rcu_read_unlock();
02d62e86 6430}
7db6b048 6431EXPORT_SYMBOL(napi_busy_loop);
02d62e86
ED
6432
6433#endif /* CONFIG_NET_RX_BUSY_POLL */
af12fa6e 6434
149d6ad8 6435static void napi_hash_add(struct napi_struct *napi)
af12fa6e 6436{
d64b5e85
ED
6437 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) ||
6438 test_and_set_bit(NAPI_STATE_HASHED, &napi->state))
52bd2d62 6439 return;
af12fa6e 6440
52bd2d62 6441 spin_lock(&napi_hash_lock);
af12fa6e 6442
545cd5e5 6443 /* 0..NR_CPUS range is reserved for sender_cpu use */
52bd2d62 6444 do {
545cd5e5
AD
6445 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6446 napi_gen_id = MIN_NAPI_ID;
52bd2d62
ED
6447 } while (napi_by_id(napi_gen_id));
6448 napi->napi_id = napi_gen_id;
af12fa6e 6449
52bd2d62
ED
6450 hlist_add_head_rcu(&napi->napi_hash_node,
6451 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
af12fa6e 6452
52bd2d62 6453 spin_unlock(&napi_hash_lock);
af12fa6e 6454}
af12fa6e
ET
6455
6456/* Warning : caller is responsible to make sure rcu grace period
6457 * is respected before freeing memory containing @napi
6458 */
34cbe27e 6459bool napi_hash_del(struct napi_struct *napi)
af12fa6e 6460{
34cbe27e
ED
6461 bool rcu_sync_needed = false;
6462
af12fa6e
ET
6463 spin_lock(&napi_hash_lock);
6464
34cbe27e
ED
6465 if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) {
6466 rcu_sync_needed = true;
af12fa6e 6467 hlist_del_rcu(&napi->napi_hash_node);
34cbe27e 6468 }
af12fa6e 6469 spin_unlock(&napi_hash_lock);
34cbe27e 6470 return rcu_sync_needed;
af12fa6e
ET
6471}
6472EXPORT_SYMBOL_GPL(napi_hash_del);
6473
3b47d303
ED
6474static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6475{
6476 struct napi_struct *napi;
6477
6478 napi = container_of(timer, struct napi_struct, timer);
39e6c820
ED
6479
6480 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
6481 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6482 */
d9f37d01 6483 if (napi->gro_bitmask && !napi_disable_pending(napi) &&
39e6c820
ED
6484 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
6485 __napi_schedule_irqoff(napi);
3b47d303
ED
6486
6487 return HRTIMER_NORESTART;
6488}
6489
7c4ec749 6490static void init_gro_hash(struct napi_struct *napi)
d565b0a1 6491{
07d78363
DM
6492 int i;
6493
6312fe77
LR
6494 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6495 INIT_LIST_HEAD(&napi->gro_hash[i].list);
6496 napi->gro_hash[i].count = 0;
6497 }
7c4ec749
DM
6498 napi->gro_bitmask = 0;
6499}
6500
6501void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
6502 int (*poll)(struct napi_struct *, int), int weight)
6503{
6504 INIT_LIST_HEAD(&napi->poll_list);
6505 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6506 napi->timer.function = napi_watchdog;
6507 init_gro_hash(napi);
5d38a079 6508 napi->skb = NULL;
323ebb61
EC
6509 INIT_LIST_HEAD(&napi->rx_list);
6510 napi->rx_count = 0;
d565b0a1 6511 napi->poll = poll;
82dc3c63 6512 if (weight > NAPI_POLL_WEIGHT)
bf29e9e9
QC
6513 netdev_err_once(dev, "%s() called with weight %d\n", __func__,
6514 weight);
d565b0a1
HX
6515 napi->weight = weight;
6516 list_add(&napi->dev_list, &dev->napi_list);
d565b0a1 6517 napi->dev = dev;
5d38a079 6518#ifdef CONFIG_NETPOLL
d565b0a1
HX
6519 napi->poll_owner = -1;
6520#endif
6521 set_bit(NAPI_STATE_SCHED, &napi->state);
93d05d4a 6522 napi_hash_add(napi);
d565b0a1
HX
6523}
6524EXPORT_SYMBOL(netif_napi_add);
6525
3b47d303
ED
6526void napi_disable(struct napi_struct *n)
6527{
6528 might_sleep();
6529 set_bit(NAPI_STATE_DISABLE, &n->state);
6530
6531 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
6532 msleep(1);
2d8bff12
NH
6533 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
6534 msleep(1);
3b47d303
ED
6535
6536 hrtimer_cancel(&n->timer);
6537
6538 clear_bit(NAPI_STATE_DISABLE, &n->state);
6539}
6540EXPORT_SYMBOL(napi_disable);
6541
07d78363 6542static void flush_gro_hash(struct napi_struct *napi)
d4546c25 6543{
07d78363 6544 int i;
d4546c25 6545
07d78363
DM
6546 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6547 struct sk_buff *skb, *n;
6548
6312fe77 6549 list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
07d78363 6550 kfree_skb(skb);
6312fe77 6551 napi->gro_hash[i].count = 0;
07d78363 6552 }
d4546c25
DM
6553}
6554
93d05d4a 6555/* Must be called in process context */
d565b0a1
HX
6556void netif_napi_del(struct napi_struct *napi)
6557{
93d05d4a
ED
6558 might_sleep();
6559 if (napi_hash_del(napi))
6560 synchronize_net();
d7b06636 6561 list_del_init(&napi->dev_list);
76620aaf 6562 napi_free_frags(napi);
d565b0a1 6563
07d78363 6564 flush_gro_hash(napi);
d9f37d01 6565 napi->gro_bitmask = 0;
d565b0a1
HX
6566}
6567EXPORT_SYMBOL(netif_napi_del);
6568
726ce70e
HX
6569static int napi_poll(struct napi_struct *n, struct list_head *repoll)
6570{
6571 void *have;
6572 int work, weight;
6573
6574 list_del_init(&n->poll_list);
6575
6576 have = netpoll_poll_lock(n);
6577
6578 weight = n->weight;
6579
6580 /* This NAPI_STATE_SCHED test is for avoiding a race
6581 * with netpoll's poll_napi(). Only the entity which
6582 * obtains the lock and sees NAPI_STATE_SCHED set will
6583 * actually make the ->poll() call. Therefore we avoid
6584 * accidentally calling ->poll() when NAPI is not scheduled.
6585 */
6586 work = 0;
6587 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
6588 work = n->poll(n, weight);
1db19db7 6589 trace_napi_poll(n, work, weight);
726ce70e
HX
6590 }
6591
6592 WARN_ON_ONCE(work > weight);
6593
6594 if (likely(work < weight))
6595 goto out_unlock;
6596
6597 /* Drivers must not modify the NAPI state if they
6598 * consume the entire weight. In such cases this code
6599 * still "owns" the NAPI instance and therefore can
6600 * move the instance around on the list at-will.
6601 */
6602 if (unlikely(napi_disable_pending(n))) {
6603 napi_complete(n);
6604 goto out_unlock;
6605 }
6606
d9f37d01 6607 if (n->gro_bitmask) {
726ce70e
HX
6608 /* flush too old packets
6609 * If HZ < 1000, flush all packets.
6610 */
6611 napi_gro_flush(n, HZ >= 1000);
6612 }
6613
c8079432
MM
6614 gro_normal_list(n);
6615
001ce546
HX
6616 /* Some drivers may have called napi_schedule
6617 * prior to exhausting their budget.
6618 */
6619 if (unlikely(!list_empty(&n->poll_list))) {
6620 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6621 n->dev ? n->dev->name : "backlog");
6622 goto out_unlock;
6623 }
6624
726ce70e
HX
6625 list_add_tail(&n->poll_list, repoll);
6626
6627out_unlock:
6628 netpoll_poll_unlock(have);
6629
6630 return work;
6631}
6632
0766f788 6633static __latent_entropy void net_rx_action(struct softirq_action *h)
1da177e4 6634{
903ceff7 6635 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
7acf8a1e
MW
6636 unsigned long time_limit = jiffies +
6637 usecs_to_jiffies(netdev_budget_usecs);
51b0bded 6638 int budget = netdev_budget;
d75b1ade
ED
6639 LIST_HEAD(list);
6640 LIST_HEAD(repoll);
53fb95d3 6641
1da177e4 6642 local_irq_disable();
d75b1ade
ED
6643 list_splice_init(&sd->poll_list, &list);
6644 local_irq_enable();
1da177e4 6645
ceb8d5bf 6646 for (;;) {
bea3348e 6647 struct napi_struct *n;
1da177e4 6648
ceb8d5bf
HX
6649 if (list_empty(&list)) {
6650 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
f52dffe0 6651 goto out;
ceb8d5bf
HX
6652 break;
6653 }
6654
6bd373eb
HX
6655 n = list_first_entry(&list, struct napi_struct, poll_list);
6656 budget -= napi_poll(n, &repoll);
6657
d75b1ade 6658 /* If softirq window is exhausted then punt.
24f8b238
SH
6659 * Allow this to run for 2 jiffies since which will allow
6660 * an average latency of 1.5/HZ.
bea3348e 6661 */
ceb8d5bf
HX
6662 if (unlikely(budget <= 0 ||
6663 time_after_eq(jiffies, time_limit))) {
6664 sd->time_squeeze++;
6665 break;
6666 }
1da177e4 6667 }
d75b1ade 6668
d75b1ade
ED
6669 local_irq_disable();
6670
6671 list_splice_tail_init(&sd->poll_list, &list);
6672 list_splice_tail(&repoll, &list);
6673 list_splice(&list, &sd->poll_list);
6674 if (!list_empty(&sd->poll_list))
6675 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
6676
e326bed2 6677 net_rps_action_and_irq_enable(sd);
f52dffe0
ED
6678out:
6679 __kfree_skb_flush();
1da177e4
LT
6680}
6681
aa9d8560 6682struct netdev_adjacent {
9ff162a8 6683 struct net_device *dev;
5d261913
VF
6684
6685 /* upper master flag, there can only be one master device per list */
9ff162a8 6686 bool master;
5d261913 6687
32b6d34f
TY
6688 /* lookup ignore flag */
6689 bool ignore;
6690
5d261913
VF
6691 /* counter for the number of times this device was added to us */
6692 u16 ref_nr;
6693
402dae96
VF
6694 /* private field for the users */
6695 void *private;
6696
9ff162a8
JP
6697 struct list_head list;
6698 struct rcu_head rcu;
9ff162a8
JP
6699};
6700
6ea29da1 6701static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
2f268f12 6702 struct list_head *adj_list)
9ff162a8 6703{
5d261913 6704 struct netdev_adjacent *adj;
5d261913 6705
2f268f12 6706 list_for_each_entry(adj, adj_list, list) {
5d261913
VF
6707 if (adj->dev == adj_dev)
6708 return adj;
9ff162a8
JP
6709 }
6710 return NULL;
6711}
6712
32b6d34f 6713static int ____netdev_has_upper_dev(struct net_device *upper_dev, void *data)
f1170fd4
DA
6714{
6715 struct net_device *dev = data;
6716
6717 return upper_dev == dev;
6718}
6719
9ff162a8
JP
6720/**
6721 * netdev_has_upper_dev - Check if device is linked to an upper device
6722 * @dev: device
6723 * @upper_dev: upper device to check
6724 *
6725 * Find out if a device is linked to specified upper device and return true
6726 * in case it is. Note that this checks only immediate upper device,
6727 * not through a complete stack of devices. The caller must hold the RTNL lock.
6728 */
6729bool netdev_has_upper_dev(struct net_device *dev,
6730 struct net_device *upper_dev)
6731{
6732 ASSERT_RTNL();
6733
32b6d34f 6734 return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
f1170fd4 6735 upper_dev);
9ff162a8
JP
6736}
6737EXPORT_SYMBOL(netdev_has_upper_dev);
6738
1a3f060c
DA
6739/**
6740 * netdev_has_upper_dev_all - Check if device is linked to an upper device
6741 * @dev: device
6742 * @upper_dev: upper device to check
6743 *
6744 * Find out if a device is linked to specified upper device and return true
6745 * in case it is. Note that this checks the entire upper device chain.
6746 * The caller must hold rcu lock.
6747 */
6748
1a3f060c
DA
6749bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
6750 struct net_device *upper_dev)
6751{
32b6d34f 6752 return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
1a3f060c
DA
6753 upper_dev);
6754}
6755EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
6756
9ff162a8
JP
6757/**
6758 * netdev_has_any_upper_dev - Check if device is linked to some device
6759 * @dev: device
6760 *
6761 * Find out if a device is linked to an upper device and return true in case
6762 * it is. The caller must hold the RTNL lock.
6763 */
25cc72a3 6764bool netdev_has_any_upper_dev(struct net_device *dev)
9ff162a8
JP
6765{
6766 ASSERT_RTNL();
6767
f1170fd4 6768 return !list_empty(&dev->adj_list.upper);
9ff162a8 6769}
25cc72a3 6770EXPORT_SYMBOL(netdev_has_any_upper_dev);
9ff162a8
JP
6771
6772/**
6773 * netdev_master_upper_dev_get - Get master upper device
6774 * @dev: device
6775 *
6776 * Find a master upper device and return pointer to it or NULL in case
6777 * it's not there. The caller must hold the RTNL lock.
6778 */
6779struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
6780{
aa9d8560 6781 struct netdev_adjacent *upper;
9ff162a8
JP
6782
6783 ASSERT_RTNL();
6784
2f268f12 6785 if (list_empty(&dev->adj_list.upper))
9ff162a8
JP
6786 return NULL;
6787
2f268f12 6788 upper = list_first_entry(&dev->adj_list.upper,
aa9d8560 6789 struct netdev_adjacent, list);
9ff162a8
JP
6790 if (likely(upper->master))
6791 return upper->dev;
6792 return NULL;
6793}
6794EXPORT_SYMBOL(netdev_master_upper_dev_get);
6795
32b6d34f
TY
6796static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev)
6797{
6798 struct netdev_adjacent *upper;
6799
6800 ASSERT_RTNL();
6801
6802 if (list_empty(&dev->adj_list.upper))
6803 return NULL;
6804
6805 upper = list_first_entry(&dev->adj_list.upper,
6806 struct netdev_adjacent, list);
6807 if (likely(upper->master) && !upper->ignore)
6808 return upper->dev;
6809 return NULL;
6810}
6811
0f524a80
DA
6812/**
6813 * netdev_has_any_lower_dev - Check if device is linked to some device
6814 * @dev: device
6815 *
6816 * Find out if a device is linked to a lower device and return true in case
6817 * it is. The caller must hold the RTNL lock.
6818 */
6819static bool netdev_has_any_lower_dev(struct net_device *dev)
6820{
6821 ASSERT_RTNL();
6822
6823 return !list_empty(&dev->adj_list.lower);
6824}
6825
b6ccba4c
VF
6826void *netdev_adjacent_get_private(struct list_head *adj_list)
6827{
6828 struct netdev_adjacent *adj;
6829
6830 adj = list_entry(adj_list, struct netdev_adjacent, list);
6831
6832 return adj->private;
6833}
6834EXPORT_SYMBOL(netdev_adjacent_get_private);
6835
44a40855
VY
6836/**
6837 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
6838 * @dev: device
6839 * @iter: list_head ** of the current position
6840 *
6841 * Gets the next device from the dev's upper list, starting from iter
6842 * position. The caller must hold RCU read lock.
6843 */
6844struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
6845 struct list_head **iter)
6846{
6847 struct netdev_adjacent *upper;
6848
6849 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6850
6851 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6852
6853 if (&upper->list == &dev->adj_list.upper)
6854 return NULL;
6855
6856 *iter = &upper->list;
6857
6858 return upper->dev;
6859}
6860EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
6861
32b6d34f
TY
6862static struct net_device *__netdev_next_upper_dev(struct net_device *dev,
6863 struct list_head **iter,
6864 bool *ignore)
5343da4c
TY
6865{
6866 struct netdev_adjacent *upper;
6867
6868 upper = list_entry((*iter)->next, struct netdev_adjacent, list);
6869
6870 if (&upper->list == &dev->adj_list.upper)
6871 return NULL;
6872
6873 *iter = &upper->list;
32b6d34f 6874 *ignore = upper->ignore;
5343da4c
TY
6875
6876 return upper->dev;
6877}
6878
1a3f060c
DA
6879static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
6880 struct list_head **iter)
6881{
6882 struct netdev_adjacent *upper;
6883
6884 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6885
6886 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6887
6888 if (&upper->list == &dev->adj_list.upper)
6889 return NULL;
6890
6891 *iter = &upper->list;
6892
6893 return upper->dev;
6894}
6895
32b6d34f
TY
6896static int __netdev_walk_all_upper_dev(struct net_device *dev,
6897 int (*fn)(struct net_device *dev,
6898 void *data),
6899 void *data)
5343da4c
TY
6900{
6901 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6902 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6903 int ret, cur = 0;
32b6d34f 6904 bool ignore;
5343da4c
TY
6905
6906 now = dev;
6907 iter = &dev->adj_list.upper;
6908
6909 while (1) {
6910 if (now != dev) {
6911 ret = fn(now, data);
6912 if (ret)
6913 return ret;
6914 }
6915
6916 next = NULL;
6917 while (1) {
32b6d34f 6918 udev = __netdev_next_upper_dev(now, &iter, &ignore);
5343da4c
TY
6919 if (!udev)
6920 break;
32b6d34f
TY
6921 if (ignore)
6922 continue;
5343da4c
TY
6923
6924 next = udev;
6925 niter = &udev->adj_list.upper;
6926 dev_stack[cur] = now;
6927 iter_stack[cur++] = iter;
6928 break;
6929 }
6930
6931 if (!next) {
6932 if (!cur)
6933 return 0;
6934 next = dev_stack[--cur];
6935 niter = iter_stack[cur];
6936 }
6937
6938 now = next;
6939 iter = niter;
6940 }
6941
6942 return 0;
6943}
6944
1a3f060c
DA
6945int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
6946 int (*fn)(struct net_device *dev,
6947 void *data),
6948 void *data)
6949{
5343da4c
TY
6950 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6951 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6952 int ret, cur = 0;
1a3f060c 6953
5343da4c
TY
6954 now = dev;
6955 iter = &dev->adj_list.upper;
1a3f060c 6956
5343da4c
TY
6957 while (1) {
6958 if (now != dev) {
6959 ret = fn(now, data);
6960 if (ret)
6961 return ret;
6962 }
6963
6964 next = NULL;
6965 while (1) {
6966 udev = netdev_next_upper_dev_rcu(now, &iter);
6967 if (!udev)
6968 break;
6969
6970 next = udev;
6971 niter = &udev->adj_list.upper;
6972 dev_stack[cur] = now;
6973 iter_stack[cur++] = iter;
6974 break;
6975 }
6976
6977 if (!next) {
6978 if (!cur)
6979 return 0;
6980 next = dev_stack[--cur];
6981 niter = iter_stack[cur];
6982 }
6983
6984 now = next;
6985 iter = niter;
1a3f060c
DA
6986 }
6987
6988 return 0;
6989}
6990EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
6991
32b6d34f
TY
6992static bool __netdev_has_upper_dev(struct net_device *dev,
6993 struct net_device *upper_dev)
6994{
6995 ASSERT_RTNL();
6996
6997 return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev,
6998 upper_dev);
6999}
7000
31088a11
VF
7001/**
7002 * netdev_lower_get_next_private - Get the next ->private from the
7003 * lower neighbour list
7004 * @dev: device
7005 * @iter: list_head ** of the current position
7006 *
7007 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7008 * list, starting from iter position. The caller must hold either hold the
7009 * RTNL lock or its own locking that guarantees that the neighbour lower
b469139e 7010 * list will remain unchanged.
31088a11
VF
7011 */
7012void *netdev_lower_get_next_private(struct net_device *dev,
7013 struct list_head **iter)
7014{
7015 struct netdev_adjacent *lower;
7016
7017 lower = list_entry(*iter, struct netdev_adjacent, list);
7018
7019 if (&lower->list == &dev->adj_list.lower)
7020 return NULL;
7021
6859e7df 7022 *iter = lower->list.next;
31088a11
VF
7023
7024 return lower->private;
7025}
7026EXPORT_SYMBOL(netdev_lower_get_next_private);
7027
7028/**
7029 * netdev_lower_get_next_private_rcu - Get the next ->private from the
7030 * lower neighbour list, RCU
7031 * variant
7032 * @dev: device
7033 * @iter: list_head ** of the current position
7034 *
7035 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7036 * list, starting from iter position. The caller must hold RCU read lock.
7037 */
7038void *netdev_lower_get_next_private_rcu(struct net_device *dev,
7039 struct list_head **iter)
7040{
7041 struct netdev_adjacent *lower;
7042
7043 WARN_ON_ONCE(!rcu_read_lock_held());
7044
7045 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7046
7047 if (&lower->list == &dev->adj_list.lower)
7048 return NULL;
7049
6859e7df 7050 *iter = &lower->list;
31088a11
VF
7051
7052 return lower->private;
7053}
7054EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
7055
4085ebe8
VY
7056/**
7057 * netdev_lower_get_next - Get the next device from the lower neighbour
7058 * list
7059 * @dev: device
7060 * @iter: list_head ** of the current position
7061 *
7062 * Gets the next netdev_adjacent from the dev's lower neighbour
7063 * list, starting from iter position. The caller must hold RTNL lock or
7064 * its own locking that guarantees that the neighbour lower
b469139e 7065 * list will remain unchanged.
4085ebe8
VY
7066 */
7067void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
7068{
7069 struct netdev_adjacent *lower;
7070
cfdd28be 7071 lower = list_entry(*iter, struct netdev_adjacent, list);
4085ebe8
VY
7072
7073 if (&lower->list == &dev->adj_list.lower)
7074 return NULL;
7075
cfdd28be 7076 *iter = lower->list.next;
4085ebe8
VY
7077
7078 return lower->dev;
7079}
7080EXPORT_SYMBOL(netdev_lower_get_next);
7081
1a3f060c
DA
7082static struct net_device *netdev_next_lower_dev(struct net_device *dev,
7083 struct list_head **iter)
7084{
7085 struct netdev_adjacent *lower;
7086
46b5ab1a 7087 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
1a3f060c
DA
7088
7089 if (&lower->list == &dev->adj_list.lower)
7090 return NULL;
7091
46b5ab1a 7092 *iter = &lower->list;
1a3f060c
DA
7093
7094 return lower->dev;
7095}
7096
32b6d34f
TY
7097static struct net_device *__netdev_next_lower_dev(struct net_device *dev,
7098 struct list_head **iter,
7099 bool *ignore)
7100{
7101 struct netdev_adjacent *lower;
7102
7103 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
7104
7105 if (&lower->list == &dev->adj_list.lower)
7106 return NULL;
7107
7108 *iter = &lower->list;
7109 *ignore = lower->ignore;
7110
7111 return lower->dev;
7112}
7113
1a3f060c
DA
7114int netdev_walk_all_lower_dev(struct net_device *dev,
7115 int (*fn)(struct net_device *dev,
7116 void *data),
7117 void *data)
7118{
5343da4c
TY
7119 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7120 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7121 int ret, cur = 0;
1a3f060c 7122
5343da4c
TY
7123 now = dev;
7124 iter = &dev->adj_list.lower;
1a3f060c 7125
5343da4c
TY
7126 while (1) {
7127 if (now != dev) {
7128 ret = fn(now, data);
7129 if (ret)
7130 return ret;
7131 }
7132
7133 next = NULL;
7134 while (1) {
7135 ldev = netdev_next_lower_dev(now, &iter);
7136 if (!ldev)
7137 break;
7138
7139 next = ldev;
7140 niter = &ldev->adj_list.lower;
7141 dev_stack[cur] = now;
7142 iter_stack[cur++] = iter;
7143 break;
7144 }
7145
7146 if (!next) {
7147 if (!cur)
7148 return 0;
7149 next = dev_stack[--cur];
7150 niter = iter_stack[cur];
7151 }
7152
7153 now = next;
7154 iter = niter;
1a3f060c
DA
7155 }
7156
7157 return 0;
7158}
7159EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
7160
32b6d34f
TY
7161static int __netdev_walk_all_lower_dev(struct net_device *dev,
7162 int (*fn)(struct net_device *dev,
7163 void *data),
7164 void *data)
7165{
7166 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7167 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7168 int ret, cur = 0;
7169 bool ignore;
7170
7171 now = dev;
7172 iter = &dev->adj_list.lower;
7173
7174 while (1) {
7175 if (now != dev) {
7176 ret = fn(now, data);
7177 if (ret)
7178 return ret;
7179 }
7180
7181 next = NULL;
7182 while (1) {
7183 ldev = __netdev_next_lower_dev(now, &iter, &ignore);
7184 if (!ldev)
7185 break;
7186 if (ignore)
7187 continue;
7188
7189 next = ldev;
7190 niter = &ldev->adj_list.lower;
7191 dev_stack[cur] = now;
7192 iter_stack[cur++] = iter;
7193 break;
7194 }
7195
7196 if (!next) {
7197 if (!cur)
7198 return 0;
7199 next = dev_stack[--cur];
7200 niter = iter_stack[cur];
7201 }
7202
7203 now = next;
7204 iter = niter;
7205 }
7206
7207 return 0;
7208}
7209
1a3f060c
DA
7210static struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
7211 struct list_head **iter)
7212{
7213 struct netdev_adjacent *lower;
7214
7215 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7216 if (&lower->list == &dev->adj_list.lower)
7217 return NULL;
7218
7219 *iter = &lower->list;
7220
7221 return lower->dev;
7222}
7223
5343da4c
TY
7224static u8 __netdev_upper_depth(struct net_device *dev)
7225{
7226 struct net_device *udev;
7227 struct list_head *iter;
7228 u8 max_depth = 0;
32b6d34f 7229 bool ignore;
5343da4c
TY
7230
7231 for (iter = &dev->adj_list.upper,
32b6d34f 7232 udev = __netdev_next_upper_dev(dev, &iter, &ignore);
5343da4c 7233 udev;
32b6d34f
TY
7234 udev = __netdev_next_upper_dev(dev, &iter, &ignore)) {
7235 if (ignore)
7236 continue;
5343da4c
TY
7237 if (max_depth < udev->upper_level)
7238 max_depth = udev->upper_level;
7239 }
7240
7241 return max_depth;
7242}
7243
7244static u8 __netdev_lower_depth(struct net_device *dev)
1a3f060c
DA
7245{
7246 struct net_device *ldev;
7247 struct list_head *iter;
5343da4c 7248 u8 max_depth = 0;
32b6d34f 7249 bool ignore;
1a3f060c
DA
7250
7251 for (iter = &dev->adj_list.lower,
32b6d34f 7252 ldev = __netdev_next_lower_dev(dev, &iter, &ignore);
1a3f060c 7253 ldev;
32b6d34f
TY
7254 ldev = __netdev_next_lower_dev(dev, &iter, &ignore)) {
7255 if (ignore)
7256 continue;
5343da4c
TY
7257 if (max_depth < ldev->lower_level)
7258 max_depth = ldev->lower_level;
7259 }
1a3f060c 7260
5343da4c
TY
7261 return max_depth;
7262}
7263
7264static int __netdev_update_upper_level(struct net_device *dev, void *data)
7265{
7266 dev->upper_level = __netdev_upper_depth(dev) + 1;
7267 return 0;
7268}
7269
7270static int __netdev_update_lower_level(struct net_device *dev, void *data)
7271{
7272 dev->lower_level = __netdev_lower_depth(dev) + 1;
7273 return 0;
7274}
7275
7276int netdev_walk_all_lower_dev_rcu(struct net_device *dev,
7277 int (*fn)(struct net_device *dev,
7278 void *data),
7279 void *data)
7280{
7281 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7282 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7283 int ret, cur = 0;
7284
7285 now = dev;
7286 iter = &dev->adj_list.lower;
7287
7288 while (1) {
7289 if (now != dev) {
7290 ret = fn(now, data);
7291 if (ret)
7292 return ret;
7293 }
7294
7295 next = NULL;
7296 while (1) {
7297 ldev = netdev_next_lower_dev_rcu(now, &iter);
7298 if (!ldev)
7299 break;
7300
7301 next = ldev;
7302 niter = &ldev->adj_list.lower;
7303 dev_stack[cur] = now;
7304 iter_stack[cur++] = iter;
7305 break;
7306 }
7307
7308 if (!next) {
7309 if (!cur)
7310 return 0;
7311 next = dev_stack[--cur];
7312 niter = iter_stack[cur];
7313 }
7314
7315 now = next;
7316 iter = niter;
1a3f060c
DA
7317 }
7318
7319 return 0;
7320}
7321EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev_rcu);
7322
e001bfad 7323/**
7324 * netdev_lower_get_first_private_rcu - Get the first ->private from the
7325 * lower neighbour list, RCU
7326 * variant
7327 * @dev: device
7328 *
7329 * Gets the first netdev_adjacent->private from the dev's lower neighbour
7330 * list. The caller must hold RCU read lock.
7331 */
7332void *netdev_lower_get_first_private_rcu(struct net_device *dev)
7333{
7334 struct netdev_adjacent *lower;
7335
7336 lower = list_first_or_null_rcu(&dev->adj_list.lower,
7337 struct netdev_adjacent, list);
7338 if (lower)
7339 return lower->private;
7340 return NULL;
7341}
7342EXPORT_SYMBOL(netdev_lower_get_first_private_rcu);
7343
9ff162a8
JP
7344/**
7345 * netdev_master_upper_dev_get_rcu - Get master upper device
7346 * @dev: device
7347 *
7348 * Find a master upper device and return pointer to it or NULL in case
7349 * it's not there. The caller must hold the RCU read lock.
7350 */
7351struct net_device *netdev_master_upper_dev_get_rcu(struct net_device *dev)
7352{
aa9d8560 7353 struct netdev_adjacent *upper;
9ff162a8 7354
2f268f12 7355 upper = list_first_or_null_rcu(&dev->adj_list.upper,
aa9d8560 7356 struct netdev_adjacent, list);
9ff162a8
JP
7357 if (upper && likely(upper->master))
7358 return upper->dev;
7359 return NULL;
7360}
7361EXPORT_SYMBOL(netdev_master_upper_dev_get_rcu);
7362
0a59f3a9 7363static int netdev_adjacent_sysfs_add(struct net_device *dev,
3ee32707
VF
7364 struct net_device *adj_dev,
7365 struct list_head *dev_list)
7366{
7367 char linkname[IFNAMSIZ+7];
f4563a75 7368
3ee32707
VF
7369 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7370 "upper_%s" : "lower_%s", adj_dev->name);
7371 return sysfs_create_link(&(dev->dev.kobj), &(adj_dev->dev.kobj),
7372 linkname);
7373}
0a59f3a9 7374static void netdev_adjacent_sysfs_del(struct net_device *dev,
3ee32707
VF
7375 char *name,
7376 struct list_head *dev_list)
7377{
7378 char linkname[IFNAMSIZ+7];
f4563a75 7379
3ee32707
VF
7380 sprintf(linkname, dev_list == &dev->adj_list.upper ?
7381 "upper_%s" : "lower_%s", name);
7382 sysfs_remove_link(&(dev->dev.kobj), linkname);
7383}
7384
7ce64c79
AF
7385static inline bool netdev_adjacent_is_neigh_list(struct net_device *dev,
7386 struct net_device *adj_dev,
7387 struct list_head *dev_list)
7388{
7389 return (dev_list == &dev->adj_list.upper ||
7390 dev_list == &dev->adj_list.lower) &&
7391 net_eq(dev_net(dev), dev_net(adj_dev));
7392}
3ee32707 7393
5d261913
VF
7394static int __netdev_adjacent_dev_insert(struct net_device *dev,
7395 struct net_device *adj_dev,
7863c054 7396 struct list_head *dev_list,
402dae96 7397 void *private, bool master)
5d261913
VF
7398{
7399 struct netdev_adjacent *adj;
842d67a7 7400 int ret;
5d261913 7401
6ea29da1 7402 adj = __netdev_find_adj(adj_dev, dev_list);
5d261913
VF
7403
7404 if (adj) {
790510d9 7405 adj->ref_nr += 1;
67b62f98
DA
7406 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d\n",
7407 dev->name, adj_dev->name, adj->ref_nr);
7408
5d261913
VF
7409 return 0;
7410 }
7411
7412 adj = kmalloc(sizeof(*adj), GFP_KERNEL);
7413 if (!adj)
7414 return -ENOMEM;
7415
7416 adj->dev = adj_dev;
7417 adj->master = master;
790510d9 7418 adj->ref_nr = 1;
402dae96 7419 adj->private = private;
32b6d34f 7420 adj->ignore = false;
5d261913 7421 dev_hold(adj_dev);
2f268f12 7422
67b62f98
DA
7423 pr_debug("Insert adjacency: dev %s adj_dev %s adj->ref_nr %d; dev_hold on %s\n",
7424 dev->name, adj_dev->name, adj->ref_nr, adj_dev->name);
5d261913 7425
7ce64c79 7426 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list)) {
3ee32707 7427 ret = netdev_adjacent_sysfs_add(dev, adj_dev, dev_list);
5831d66e
VF
7428 if (ret)
7429 goto free_adj;
7430 }
7431
7863c054 7432 /* Ensure that master link is always the first item in list. */
842d67a7
VF
7433 if (master) {
7434 ret = sysfs_create_link(&(dev->dev.kobj),
7435 &(adj_dev->dev.kobj), "master");
7436 if (ret)
5831d66e 7437 goto remove_symlinks;
842d67a7 7438
7863c054 7439 list_add_rcu(&adj->list, dev_list);
842d67a7 7440 } else {
7863c054 7441 list_add_tail_rcu(&adj->list, dev_list);
842d67a7 7442 }
5d261913
VF
7443
7444 return 0;
842d67a7 7445
5831d66e 7446remove_symlinks:
7ce64c79 7447 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
3ee32707 7448 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
842d67a7
VF
7449free_adj:
7450 kfree(adj);
974daef7 7451 dev_put(adj_dev);
842d67a7
VF
7452
7453 return ret;
5d261913
VF
7454}
7455
1d143d9f 7456static void __netdev_adjacent_dev_remove(struct net_device *dev,
7457 struct net_device *adj_dev,
93409033 7458 u16 ref_nr,
1d143d9f 7459 struct list_head *dev_list)
5d261913
VF
7460{
7461 struct netdev_adjacent *adj;
7462
67b62f98
DA
7463 pr_debug("Remove adjacency: dev %s adj_dev %s ref_nr %d\n",
7464 dev->name, adj_dev->name, ref_nr);
7465
6ea29da1 7466 adj = __netdev_find_adj(adj_dev, dev_list);
5d261913 7467
2f268f12 7468 if (!adj) {
67b62f98 7469 pr_err("Adjacency does not exist for device %s from %s\n",
2f268f12 7470 dev->name, adj_dev->name);
67b62f98
DA
7471 WARN_ON(1);
7472 return;
2f268f12 7473 }
5d261913 7474
93409033 7475 if (adj->ref_nr > ref_nr) {
67b62f98
DA
7476 pr_debug("adjacency: %s to %s ref_nr - %d = %d\n",
7477 dev->name, adj_dev->name, ref_nr,
7478 adj->ref_nr - ref_nr);
93409033 7479 adj->ref_nr -= ref_nr;
5d261913
VF
7480 return;
7481 }
7482
842d67a7
VF
7483 if (adj->master)
7484 sysfs_remove_link(&(dev->dev.kobj), "master");
7485
7ce64c79 7486 if (netdev_adjacent_is_neigh_list(dev, adj_dev, dev_list))
3ee32707 7487 netdev_adjacent_sysfs_del(dev, adj_dev->name, dev_list);
5831d66e 7488
5d261913 7489 list_del_rcu(&adj->list);
67b62f98 7490 pr_debug("adjacency: dev_put for %s, because link removed from %s to %s\n",
2f268f12 7491 adj_dev->name, dev->name, adj_dev->name);
5d261913
VF
7492 dev_put(adj_dev);
7493 kfree_rcu(adj, rcu);
7494}
7495
1d143d9f 7496static int __netdev_adjacent_dev_link_lists(struct net_device *dev,
7497 struct net_device *upper_dev,
7498 struct list_head *up_list,
7499 struct list_head *down_list,
7500 void *private, bool master)
5d261913
VF
7501{
7502 int ret;
7503
790510d9 7504 ret = __netdev_adjacent_dev_insert(dev, upper_dev, up_list,
93409033 7505 private, master);
5d261913
VF
7506 if (ret)
7507 return ret;
7508
790510d9 7509 ret = __netdev_adjacent_dev_insert(upper_dev, dev, down_list,
93409033 7510 private, false);
5d261913 7511 if (ret) {
790510d9 7512 __netdev_adjacent_dev_remove(dev, upper_dev, 1, up_list);
5d261913
VF
7513 return ret;
7514 }
7515
7516 return 0;
7517}
7518
1d143d9f 7519static void __netdev_adjacent_dev_unlink_lists(struct net_device *dev,
7520 struct net_device *upper_dev,
93409033 7521 u16 ref_nr,
1d143d9f 7522 struct list_head *up_list,
7523 struct list_head *down_list)
5d261913 7524{
93409033
AC
7525 __netdev_adjacent_dev_remove(dev, upper_dev, ref_nr, up_list);
7526 __netdev_adjacent_dev_remove(upper_dev, dev, ref_nr, down_list);
5d261913
VF
7527}
7528
1d143d9f 7529static int __netdev_adjacent_dev_link_neighbour(struct net_device *dev,
7530 struct net_device *upper_dev,
7531 void *private, bool master)
2f268f12 7532{
f1170fd4
DA
7533 return __netdev_adjacent_dev_link_lists(dev, upper_dev,
7534 &dev->adj_list.upper,
7535 &upper_dev->adj_list.lower,
7536 private, master);
5d261913
VF
7537}
7538
1d143d9f 7539static void __netdev_adjacent_dev_unlink_neighbour(struct net_device *dev,
7540 struct net_device *upper_dev)
2f268f12 7541{
93409033 7542 __netdev_adjacent_dev_unlink_lists(dev, upper_dev, 1,
2f268f12
VF
7543 &dev->adj_list.upper,
7544 &upper_dev->adj_list.lower);
7545}
5d261913 7546
9ff162a8 7547static int __netdev_upper_dev_link(struct net_device *dev,
402dae96 7548 struct net_device *upper_dev, bool master,
42ab19ee
DA
7549 void *upper_priv, void *upper_info,
7550 struct netlink_ext_ack *extack)
9ff162a8 7551{
51d0c047
DA
7552 struct netdev_notifier_changeupper_info changeupper_info = {
7553 .info = {
7554 .dev = dev,
42ab19ee 7555 .extack = extack,
51d0c047
DA
7556 },
7557 .upper_dev = upper_dev,
7558 .master = master,
7559 .linking = true,
7560 .upper_info = upper_info,
7561 };
50d629e7 7562 struct net_device *master_dev;
5d261913 7563 int ret = 0;
9ff162a8
JP
7564
7565 ASSERT_RTNL();
7566
7567 if (dev == upper_dev)
7568 return -EBUSY;
7569
7570 /* To prevent loops, check if dev is not upper device to upper_dev. */
32b6d34f 7571 if (__netdev_has_upper_dev(upper_dev, dev))
9ff162a8
JP
7572 return -EBUSY;
7573
5343da4c
TY
7574 if ((dev->lower_level + upper_dev->upper_level) > MAX_NEST_DEV)
7575 return -EMLINK;
7576
50d629e7 7577 if (!master) {
32b6d34f 7578 if (__netdev_has_upper_dev(dev, upper_dev))
50d629e7
MM
7579 return -EEXIST;
7580 } else {
32b6d34f 7581 master_dev = __netdev_master_upper_dev_get(dev);
50d629e7
MM
7582 if (master_dev)
7583 return master_dev == upper_dev ? -EEXIST : -EBUSY;
7584 }
9ff162a8 7585
51d0c047 7586 ret = call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
573c7ba0
JP
7587 &changeupper_info.info);
7588 ret = notifier_to_errno(ret);
7589 if (ret)
7590 return ret;
7591
6dffb044 7592 ret = __netdev_adjacent_dev_link_neighbour(dev, upper_dev, upper_priv,
402dae96 7593 master);
5d261913
VF
7594 if (ret)
7595 return ret;
9ff162a8 7596
51d0c047 7597 ret = call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
b03804e7
IS
7598 &changeupper_info.info);
7599 ret = notifier_to_errno(ret);
7600 if (ret)
f1170fd4 7601 goto rollback;
b03804e7 7602
5343da4c 7603 __netdev_update_upper_level(dev, NULL);
32b6d34f 7604 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
5343da4c
TY
7605
7606 __netdev_update_lower_level(upper_dev, NULL);
32b6d34f
TY
7607 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
7608 NULL);
5343da4c 7609
9ff162a8 7610 return 0;
5d261913 7611
f1170fd4 7612rollback:
2f268f12 7613 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
5d261913
VF
7614
7615 return ret;
9ff162a8
JP
7616}
7617
7618/**
7619 * netdev_upper_dev_link - Add a link to the upper device
7620 * @dev: device
7621 * @upper_dev: new upper device
7a006d59 7622 * @extack: netlink extended ack
9ff162a8
JP
7623 *
7624 * Adds a link to device which is upper to this one. The caller must hold
7625 * the RTNL lock. On a failure a negative errno code is returned.
7626 * On success the reference counts are adjusted and the function
7627 * returns zero.
7628 */
7629int netdev_upper_dev_link(struct net_device *dev,
42ab19ee
DA
7630 struct net_device *upper_dev,
7631 struct netlink_ext_ack *extack)
9ff162a8 7632{
42ab19ee
DA
7633 return __netdev_upper_dev_link(dev, upper_dev, false,
7634 NULL, NULL, extack);
9ff162a8
JP
7635}
7636EXPORT_SYMBOL(netdev_upper_dev_link);
7637
7638/**
7639 * netdev_master_upper_dev_link - Add a master link to the upper device
7640 * @dev: device
7641 * @upper_dev: new upper device
6dffb044 7642 * @upper_priv: upper device private
29bf24af 7643 * @upper_info: upper info to be passed down via notifier
7a006d59 7644 * @extack: netlink extended ack
9ff162a8
JP
7645 *
7646 * Adds a link to device which is upper to this one. In this case, only
7647 * one master upper device can be linked, although other non-master devices
7648 * might be linked as well. The caller must hold the RTNL lock.
7649 * On a failure a negative errno code is returned. On success the reference
7650 * counts are adjusted and the function returns zero.
7651 */
7652int netdev_master_upper_dev_link(struct net_device *dev,
6dffb044 7653 struct net_device *upper_dev,
42ab19ee
DA
7654 void *upper_priv, void *upper_info,
7655 struct netlink_ext_ack *extack)
9ff162a8 7656{
29bf24af 7657 return __netdev_upper_dev_link(dev, upper_dev, true,
42ab19ee 7658 upper_priv, upper_info, extack);
9ff162a8
JP
7659}
7660EXPORT_SYMBOL(netdev_master_upper_dev_link);
7661
7662/**
7663 * netdev_upper_dev_unlink - Removes a link to upper device
7664 * @dev: device
7665 * @upper_dev: new upper device
7666 *
7667 * Removes a link to device which is upper to this one. The caller must hold
7668 * the RTNL lock.
7669 */
7670void netdev_upper_dev_unlink(struct net_device *dev,
7671 struct net_device *upper_dev)
7672{
51d0c047
DA
7673 struct netdev_notifier_changeupper_info changeupper_info = {
7674 .info = {
7675 .dev = dev,
7676 },
7677 .upper_dev = upper_dev,
7678 .linking = false,
7679 };
f4563a75 7680
9ff162a8
JP
7681 ASSERT_RTNL();
7682
0e4ead9d 7683 changeupper_info.master = netdev_master_upper_dev_get(dev) == upper_dev;
0e4ead9d 7684
51d0c047 7685 call_netdevice_notifiers_info(NETDEV_PRECHANGEUPPER,
573c7ba0
JP
7686 &changeupper_info.info);
7687
2f268f12 7688 __netdev_adjacent_dev_unlink_neighbour(dev, upper_dev);
5d261913 7689
51d0c047 7690 call_netdevice_notifiers_info(NETDEV_CHANGEUPPER,
0e4ead9d 7691 &changeupper_info.info);
5343da4c
TY
7692
7693 __netdev_update_upper_level(dev, NULL);
32b6d34f 7694 __netdev_walk_all_lower_dev(dev, __netdev_update_upper_level, NULL);
5343da4c
TY
7695
7696 __netdev_update_lower_level(upper_dev, NULL);
32b6d34f
TY
7697 __netdev_walk_all_upper_dev(upper_dev, __netdev_update_lower_level,
7698 NULL);
9ff162a8
JP
7699}
7700EXPORT_SYMBOL(netdev_upper_dev_unlink);
7701
32b6d34f
TY
7702static void __netdev_adjacent_dev_set(struct net_device *upper_dev,
7703 struct net_device *lower_dev,
7704 bool val)
7705{
7706 struct netdev_adjacent *adj;
7707
7708 adj = __netdev_find_adj(lower_dev, &upper_dev->adj_list.lower);
7709 if (adj)
7710 adj->ignore = val;
7711
7712 adj = __netdev_find_adj(upper_dev, &lower_dev->adj_list.upper);
7713 if (adj)
7714 adj->ignore = val;
7715}
7716
7717static void netdev_adjacent_dev_disable(struct net_device *upper_dev,
7718 struct net_device *lower_dev)
7719{
7720 __netdev_adjacent_dev_set(upper_dev, lower_dev, true);
7721}
7722
7723static void netdev_adjacent_dev_enable(struct net_device *upper_dev,
7724 struct net_device *lower_dev)
7725{
7726 __netdev_adjacent_dev_set(upper_dev, lower_dev, false);
7727}
7728
7729int netdev_adjacent_change_prepare(struct net_device *old_dev,
7730 struct net_device *new_dev,
7731 struct net_device *dev,
7732 struct netlink_ext_ack *extack)
7733{
7734 int err;
7735
7736 if (!new_dev)
7737 return 0;
7738
7739 if (old_dev && new_dev != old_dev)
7740 netdev_adjacent_dev_disable(dev, old_dev);
7741
7742 err = netdev_upper_dev_link(new_dev, dev, extack);
7743 if (err) {
7744 if (old_dev && new_dev != old_dev)
7745 netdev_adjacent_dev_enable(dev, old_dev);
7746 return err;
7747 }
7748
7749 return 0;
7750}
7751EXPORT_SYMBOL(netdev_adjacent_change_prepare);
7752
7753void netdev_adjacent_change_commit(struct net_device *old_dev,
7754 struct net_device *new_dev,
7755 struct net_device *dev)
7756{
7757 if (!new_dev || !old_dev)
7758 return;
7759
7760 if (new_dev == old_dev)
7761 return;
7762
7763 netdev_adjacent_dev_enable(dev, old_dev);
7764 netdev_upper_dev_unlink(old_dev, dev);
7765}
7766EXPORT_SYMBOL(netdev_adjacent_change_commit);
7767
7768void netdev_adjacent_change_abort(struct net_device *old_dev,
7769 struct net_device *new_dev,
7770 struct net_device *dev)
7771{
7772 if (!new_dev)
7773 return;
7774
7775 if (old_dev && new_dev != old_dev)
7776 netdev_adjacent_dev_enable(dev, old_dev);
7777
7778 netdev_upper_dev_unlink(new_dev, dev);
7779}
7780EXPORT_SYMBOL(netdev_adjacent_change_abort);
7781
61bd3857
MS
7782/**
7783 * netdev_bonding_info_change - Dispatch event about slave change
7784 * @dev: device
4a26e453 7785 * @bonding_info: info to dispatch
61bd3857
MS
7786 *
7787 * Send NETDEV_BONDING_INFO to netdev notifiers with info.
7788 * The caller must hold the RTNL lock.
7789 */
7790void netdev_bonding_info_change(struct net_device *dev,
7791 struct netdev_bonding_info *bonding_info)
7792{
51d0c047
DA
7793 struct netdev_notifier_bonding_info info = {
7794 .info.dev = dev,
7795 };
61bd3857
MS
7796
7797 memcpy(&info.bonding_info, bonding_info,
7798 sizeof(struct netdev_bonding_info));
51d0c047 7799 call_netdevice_notifiers_info(NETDEV_BONDING_INFO,
61bd3857
MS
7800 &info.info);
7801}
7802EXPORT_SYMBOL(netdev_bonding_info_change);
7803
2ce1ee17 7804static void netdev_adjacent_add_links(struct net_device *dev)
4c75431a
AF
7805{
7806 struct netdev_adjacent *iter;
7807
7808 struct net *net = dev_net(dev);
7809
7810 list_for_each_entry(iter, &dev->adj_list.upper, list) {
be4da0e3 7811 if (!net_eq(net, dev_net(iter->dev)))
4c75431a
AF
7812 continue;
7813 netdev_adjacent_sysfs_add(iter->dev, dev,
7814 &iter->dev->adj_list.lower);
7815 netdev_adjacent_sysfs_add(dev, iter->dev,
7816 &dev->adj_list.upper);
7817 }
7818
7819 list_for_each_entry(iter, &dev->adj_list.lower, list) {
be4da0e3 7820 if (!net_eq(net, dev_net(iter->dev)))
4c75431a
AF
7821 continue;
7822 netdev_adjacent_sysfs_add(iter->dev, dev,
7823 &iter->dev->adj_list.upper);
7824 netdev_adjacent_sysfs_add(dev, iter->dev,
7825 &dev->adj_list.lower);
7826 }
7827}
7828
2ce1ee17 7829static void netdev_adjacent_del_links(struct net_device *dev)
4c75431a
AF
7830{
7831 struct netdev_adjacent *iter;
7832
7833 struct net *net = dev_net(dev);
7834
7835 list_for_each_entry(iter, &dev->adj_list.upper, list) {
be4da0e3 7836 if (!net_eq(net, dev_net(iter->dev)))
4c75431a
AF
7837 continue;
7838 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7839 &iter->dev->adj_list.lower);
7840 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7841 &dev->adj_list.upper);
7842 }
7843
7844 list_for_each_entry(iter, &dev->adj_list.lower, list) {
be4da0e3 7845 if (!net_eq(net, dev_net(iter->dev)))
4c75431a
AF
7846 continue;
7847 netdev_adjacent_sysfs_del(iter->dev, dev->name,
7848 &iter->dev->adj_list.upper);
7849 netdev_adjacent_sysfs_del(dev, iter->dev->name,
7850 &dev->adj_list.lower);
7851 }
7852}
7853
5bb025fa 7854void netdev_adjacent_rename_links(struct net_device *dev, char *oldname)
402dae96 7855{
5bb025fa 7856 struct netdev_adjacent *iter;
402dae96 7857
4c75431a
AF
7858 struct net *net = dev_net(dev);
7859
5bb025fa 7860 list_for_each_entry(iter, &dev->adj_list.upper, list) {
be4da0e3 7861 if (!net_eq(net, dev_net(iter->dev)))
4c75431a 7862 continue;
5bb025fa
VF
7863 netdev_adjacent_sysfs_del(iter->dev, oldname,
7864 &iter->dev->adj_list.lower);
7865 netdev_adjacent_sysfs_add(iter->dev, dev,
7866 &iter->dev->adj_list.lower);
7867 }
402dae96 7868
5bb025fa 7869 list_for_each_entry(iter, &dev->adj_list.lower, list) {
be4da0e3 7870 if (!net_eq(net, dev_net(iter->dev)))
4c75431a 7871 continue;
5bb025fa
VF
7872 netdev_adjacent_sysfs_del(iter->dev, oldname,
7873 &iter->dev->adj_list.upper);
7874 netdev_adjacent_sysfs_add(iter->dev, dev,
7875 &iter->dev->adj_list.upper);
7876 }
402dae96 7877}
402dae96
VF
7878
7879void *netdev_lower_dev_get_private(struct net_device *dev,
7880 struct net_device *lower_dev)
7881{
7882 struct netdev_adjacent *lower;
7883
7884 if (!lower_dev)
7885 return NULL;
6ea29da1 7886 lower = __netdev_find_adj(lower_dev, &dev->adj_list.lower);
402dae96
VF
7887 if (!lower)
7888 return NULL;
7889
7890 return lower->private;
7891}
7892EXPORT_SYMBOL(netdev_lower_dev_get_private);
7893
4085ebe8 7894
04d48266
JP
7895/**
7896 * netdev_lower_change - Dispatch event about lower device state change
7897 * @lower_dev: device
7898 * @lower_state_info: state to dispatch
7899 *
7900 * Send NETDEV_CHANGELOWERSTATE to netdev notifiers with info.
7901 * The caller must hold the RTNL lock.
7902 */
7903void netdev_lower_state_changed(struct net_device *lower_dev,
7904 void *lower_state_info)
7905{
51d0c047
DA
7906 struct netdev_notifier_changelowerstate_info changelowerstate_info = {
7907 .info.dev = lower_dev,
7908 };
04d48266
JP
7909
7910 ASSERT_RTNL();
7911 changelowerstate_info.lower_state_info = lower_state_info;
51d0c047 7912 call_netdevice_notifiers_info(NETDEV_CHANGELOWERSTATE,
04d48266
JP
7913 &changelowerstate_info.info);
7914}
7915EXPORT_SYMBOL(netdev_lower_state_changed);
7916
b6c40d68
PM
7917static void dev_change_rx_flags(struct net_device *dev, int flags)
7918{
d314774c
SH
7919 const struct net_device_ops *ops = dev->netdev_ops;
7920
d2615bf4 7921 if (ops->ndo_change_rx_flags)
d314774c 7922 ops->ndo_change_rx_flags(dev, flags);
b6c40d68
PM
7923}
7924
991fb3f7 7925static int __dev_set_promiscuity(struct net_device *dev, int inc, bool notify)
1da177e4 7926{
b536db93 7927 unsigned int old_flags = dev->flags;
d04a48b0
EB
7928 kuid_t uid;
7929 kgid_t gid;
1da177e4 7930
24023451
PM
7931 ASSERT_RTNL();
7932
dad9b335
WC
7933 dev->flags |= IFF_PROMISC;
7934 dev->promiscuity += inc;
7935 if (dev->promiscuity == 0) {
7936 /*
7937 * Avoid overflow.
7938 * If inc causes overflow, untouch promisc and return error.
7939 */
7940 if (inc < 0)
7941 dev->flags &= ~IFF_PROMISC;
7942 else {
7943 dev->promiscuity -= inc;
7b6cd1ce
JP
7944 pr_warn("%s: promiscuity touches roof, set promiscuity failed. promiscuity feature of device might be broken.\n",
7945 dev->name);
dad9b335
WC
7946 return -EOVERFLOW;
7947 }
7948 }
52609c0b 7949 if (dev->flags != old_flags) {
7b6cd1ce
JP
7950 pr_info("device %s %s promiscuous mode\n",
7951 dev->name,
7952 dev->flags & IFF_PROMISC ? "entered" : "left");
8192b0c4
DH
7953 if (audit_enabled) {
7954 current_uid_gid(&uid, &gid);
cdfb6b34
RGB
7955 audit_log(audit_context(), GFP_ATOMIC,
7956 AUDIT_ANOM_PROMISCUOUS,
7957 "dev=%s prom=%d old_prom=%d auid=%u uid=%u gid=%u ses=%u",
7958 dev->name, (dev->flags & IFF_PROMISC),
7959 (old_flags & IFF_PROMISC),
7960 from_kuid(&init_user_ns, audit_get_loginuid(current)),
7961 from_kuid(&init_user_ns, uid),
7962 from_kgid(&init_user_ns, gid),
7963 audit_get_sessionid(current));
8192b0c4 7964 }
24023451 7965
b6c40d68 7966 dev_change_rx_flags(dev, IFF_PROMISC);
1da177e4 7967 }
991fb3f7
ND
7968 if (notify)
7969 __dev_notify_flags(dev, old_flags, IFF_PROMISC);
dad9b335 7970 return 0;
1da177e4
LT
7971}
7972
4417da66
PM
7973/**
7974 * dev_set_promiscuity - update promiscuity count on a device
7975 * @dev: device
7976 * @inc: modifier
7977 *
7978 * Add or remove promiscuity from a device. While the count in the device
7979 * remains above zero the interface remains promiscuous. Once it hits zero
7980 * the device reverts back to normal filtering operation. A negative inc
7981 * value is used to drop promiscuity on the device.
dad9b335 7982 * Return 0 if successful or a negative errno code on error.
4417da66 7983 */
dad9b335 7984int dev_set_promiscuity(struct net_device *dev, int inc)
4417da66 7985{
b536db93 7986 unsigned int old_flags = dev->flags;
dad9b335 7987 int err;
4417da66 7988
991fb3f7 7989 err = __dev_set_promiscuity(dev, inc, true);
4b5a698e 7990 if (err < 0)
dad9b335 7991 return err;
4417da66
PM
7992 if (dev->flags != old_flags)
7993 dev_set_rx_mode(dev);
dad9b335 7994 return err;
4417da66 7995}
d1b19dff 7996EXPORT_SYMBOL(dev_set_promiscuity);
4417da66 7997
991fb3f7 7998static int __dev_set_allmulti(struct net_device *dev, int inc, bool notify)
1da177e4 7999{
991fb3f7 8000 unsigned int old_flags = dev->flags, old_gflags = dev->gflags;
1da177e4 8001
24023451
PM
8002 ASSERT_RTNL();
8003
1da177e4 8004 dev->flags |= IFF_ALLMULTI;
dad9b335
WC
8005 dev->allmulti += inc;
8006 if (dev->allmulti == 0) {
8007 /*
8008 * Avoid overflow.
8009 * If inc causes overflow, untouch allmulti and return error.
8010 */
8011 if (inc < 0)
8012 dev->flags &= ~IFF_ALLMULTI;
8013 else {
8014 dev->allmulti -= inc;
7b6cd1ce
JP
8015 pr_warn("%s: allmulti touches roof, set allmulti failed. allmulti feature of device might be broken.\n",
8016 dev->name);
dad9b335
WC
8017 return -EOVERFLOW;
8018 }
8019 }
24023451 8020 if (dev->flags ^ old_flags) {
b6c40d68 8021 dev_change_rx_flags(dev, IFF_ALLMULTI);
4417da66 8022 dev_set_rx_mode(dev);
991fb3f7
ND
8023 if (notify)
8024 __dev_notify_flags(dev, old_flags,
8025 dev->gflags ^ old_gflags);
24023451 8026 }
dad9b335 8027 return 0;
4417da66 8028}
991fb3f7
ND
8029
8030/**
8031 * dev_set_allmulti - update allmulti count on a device
8032 * @dev: device
8033 * @inc: modifier
8034 *
8035 * Add or remove reception of all multicast frames to a device. While the
8036 * count in the device remains above zero the interface remains listening
8037 * to all interfaces. Once it hits zero the device reverts back to normal
8038 * filtering operation. A negative @inc value is used to drop the counter
8039 * when releasing a resource needing all multicasts.
8040 * Return 0 if successful or a negative errno code on error.
8041 */
8042
8043int dev_set_allmulti(struct net_device *dev, int inc)
8044{
8045 return __dev_set_allmulti(dev, inc, true);
8046}
d1b19dff 8047EXPORT_SYMBOL(dev_set_allmulti);
4417da66
PM
8048
8049/*
8050 * Upload unicast and multicast address lists to device and
8051 * configure RX filtering. When the device doesn't support unicast
53ccaae1 8052 * filtering it is put in promiscuous mode while unicast addresses
4417da66
PM
8053 * are present.
8054 */
8055void __dev_set_rx_mode(struct net_device *dev)
8056{
d314774c
SH
8057 const struct net_device_ops *ops = dev->netdev_ops;
8058
4417da66
PM
8059 /* dev_open will call this function so the list will stay sane. */
8060 if (!(dev->flags&IFF_UP))
8061 return;
8062
8063 if (!netif_device_present(dev))
40b77c94 8064 return;
4417da66 8065
01789349 8066 if (!(dev->priv_flags & IFF_UNICAST_FLT)) {
4417da66
PM
8067 /* Unicast addresses changes may only happen under the rtnl,
8068 * therefore calling __dev_set_promiscuity here is safe.
8069 */
32e7bfc4 8070 if (!netdev_uc_empty(dev) && !dev->uc_promisc) {
991fb3f7 8071 __dev_set_promiscuity(dev, 1, false);
2d348d1f 8072 dev->uc_promisc = true;
32e7bfc4 8073 } else if (netdev_uc_empty(dev) && dev->uc_promisc) {
991fb3f7 8074 __dev_set_promiscuity(dev, -1, false);
2d348d1f 8075 dev->uc_promisc = false;
4417da66 8076 }
4417da66 8077 }
01789349
JP
8078
8079 if (ops->ndo_set_rx_mode)
8080 ops->ndo_set_rx_mode(dev);
4417da66
PM
8081}
8082
8083void dev_set_rx_mode(struct net_device *dev)
8084{
b9e40857 8085 netif_addr_lock_bh(dev);
4417da66 8086 __dev_set_rx_mode(dev);
b9e40857 8087 netif_addr_unlock_bh(dev);
1da177e4
LT
8088}
8089
f0db275a
SH
8090/**
8091 * dev_get_flags - get flags reported to userspace
8092 * @dev: device
8093 *
8094 * Get the combination of flag bits exported through APIs to userspace.
8095 */
95c96174 8096unsigned int dev_get_flags(const struct net_device *dev)
1da177e4 8097{
95c96174 8098 unsigned int flags;
1da177e4
LT
8099
8100 flags = (dev->flags & ~(IFF_PROMISC |
8101 IFF_ALLMULTI |
b00055aa
SR
8102 IFF_RUNNING |
8103 IFF_LOWER_UP |
8104 IFF_DORMANT)) |
1da177e4
LT
8105 (dev->gflags & (IFF_PROMISC |
8106 IFF_ALLMULTI));
8107
b00055aa
SR
8108 if (netif_running(dev)) {
8109 if (netif_oper_up(dev))
8110 flags |= IFF_RUNNING;
8111 if (netif_carrier_ok(dev))
8112 flags |= IFF_LOWER_UP;
8113 if (netif_dormant(dev))
8114 flags |= IFF_DORMANT;
8115 }
1da177e4
LT
8116
8117 return flags;
8118}
d1b19dff 8119EXPORT_SYMBOL(dev_get_flags);
1da177e4 8120
6d040321
PM
8121int __dev_change_flags(struct net_device *dev, unsigned int flags,
8122 struct netlink_ext_ack *extack)
1da177e4 8123{
b536db93 8124 unsigned int old_flags = dev->flags;
bd380811 8125 int ret;
1da177e4 8126
24023451
PM
8127 ASSERT_RTNL();
8128
1da177e4
LT
8129 /*
8130 * Set the flags on our device.
8131 */
8132
8133 dev->flags = (flags & (IFF_DEBUG | IFF_NOTRAILERS | IFF_NOARP |
8134 IFF_DYNAMIC | IFF_MULTICAST | IFF_PORTSEL |
8135 IFF_AUTOMEDIA)) |
8136 (dev->flags & (IFF_UP | IFF_VOLATILE | IFF_PROMISC |
8137 IFF_ALLMULTI));
8138
8139 /*
8140 * Load in the correct multicast list now the flags have changed.
8141 */
8142
b6c40d68
PM
8143 if ((old_flags ^ flags) & IFF_MULTICAST)
8144 dev_change_rx_flags(dev, IFF_MULTICAST);
24023451 8145
4417da66 8146 dev_set_rx_mode(dev);
1da177e4
LT
8147
8148 /*
8149 * Have we downed the interface. We handle IFF_UP ourselves
8150 * according to user attempts to set it, rather than blindly
8151 * setting it.
8152 */
8153
8154 ret = 0;
7051b88a 8155 if ((old_flags ^ flags) & IFF_UP) {
8156 if (old_flags & IFF_UP)
8157 __dev_close(dev);
8158 else
40c900aa 8159 ret = __dev_open(dev, extack);
7051b88a 8160 }
1da177e4 8161
1da177e4 8162 if ((flags ^ dev->gflags) & IFF_PROMISC) {
d1b19dff 8163 int inc = (flags & IFF_PROMISC) ? 1 : -1;
991fb3f7 8164 unsigned int old_flags = dev->flags;
d1b19dff 8165
1da177e4 8166 dev->gflags ^= IFF_PROMISC;
991fb3f7
ND
8167
8168 if (__dev_set_promiscuity(dev, inc, false) >= 0)
8169 if (dev->flags != old_flags)
8170 dev_set_rx_mode(dev);
1da177e4
LT
8171 }
8172
8173 /* NOTE: order of synchronization of IFF_PROMISC and IFF_ALLMULTI
eb13da1a 8174 * is important. Some (broken) drivers set IFF_PROMISC, when
8175 * IFF_ALLMULTI is requested not asking us and not reporting.
1da177e4
LT
8176 */
8177 if ((flags ^ dev->gflags) & IFF_ALLMULTI) {
d1b19dff
ED
8178 int inc = (flags & IFF_ALLMULTI) ? 1 : -1;
8179
1da177e4 8180 dev->gflags ^= IFF_ALLMULTI;
991fb3f7 8181 __dev_set_allmulti(dev, inc, false);
1da177e4
LT
8182 }
8183
bd380811
PM
8184 return ret;
8185}
8186
a528c219
ND
8187void __dev_notify_flags(struct net_device *dev, unsigned int old_flags,
8188 unsigned int gchanges)
bd380811
PM
8189{
8190 unsigned int changes = dev->flags ^ old_flags;
8191
a528c219 8192 if (gchanges)
7f294054 8193 rtmsg_ifinfo(RTM_NEWLINK, dev, gchanges, GFP_ATOMIC);
a528c219 8194
bd380811
PM
8195 if (changes & IFF_UP) {
8196 if (dev->flags & IFF_UP)
8197 call_netdevice_notifiers(NETDEV_UP, dev);
8198 else
8199 call_netdevice_notifiers(NETDEV_DOWN, dev);
8200 }
8201
8202 if (dev->flags & IFF_UP &&
be9efd36 8203 (changes & ~(IFF_UP | IFF_PROMISC | IFF_ALLMULTI | IFF_VOLATILE))) {
51d0c047
DA
8204 struct netdev_notifier_change_info change_info = {
8205 .info = {
8206 .dev = dev,
8207 },
8208 .flags_changed = changes,
8209 };
be9efd36 8210
51d0c047 8211 call_netdevice_notifiers_info(NETDEV_CHANGE, &change_info.info);
be9efd36 8212 }
bd380811
PM
8213}
8214
8215/**
8216 * dev_change_flags - change device settings
8217 * @dev: device
8218 * @flags: device state flags
567c5e13 8219 * @extack: netlink extended ack
bd380811
PM
8220 *
8221 * Change settings on device based state flags. The flags are
8222 * in the userspace exported format.
8223 */
567c5e13
PM
8224int dev_change_flags(struct net_device *dev, unsigned int flags,
8225 struct netlink_ext_ack *extack)
bd380811 8226{
b536db93 8227 int ret;
991fb3f7 8228 unsigned int changes, old_flags = dev->flags, old_gflags = dev->gflags;
bd380811 8229
6d040321 8230 ret = __dev_change_flags(dev, flags, extack);
bd380811
PM
8231 if (ret < 0)
8232 return ret;
8233
991fb3f7 8234 changes = (old_flags ^ dev->flags) | (old_gflags ^ dev->gflags);
a528c219 8235 __dev_notify_flags(dev, old_flags, changes);
1da177e4
LT
8236 return ret;
8237}
d1b19dff 8238EXPORT_SYMBOL(dev_change_flags);
1da177e4 8239
f51048c3 8240int __dev_set_mtu(struct net_device *dev, int new_mtu)
2315dc91
VF
8241{
8242 const struct net_device_ops *ops = dev->netdev_ops;
8243
8244 if (ops->ndo_change_mtu)
8245 return ops->ndo_change_mtu(dev, new_mtu);
8246
501a90c9
ED
8247 /* Pairs with all the lockless reads of dev->mtu in the stack */
8248 WRITE_ONCE(dev->mtu, new_mtu);
2315dc91
VF
8249 return 0;
8250}
f51048c3 8251EXPORT_SYMBOL(__dev_set_mtu);
2315dc91 8252
d836f5c6
ED
8253int dev_validate_mtu(struct net_device *dev, int new_mtu,
8254 struct netlink_ext_ack *extack)
8255{
8256 /* MTU must be positive, and in range */
8257 if (new_mtu < 0 || new_mtu < dev->min_mtu) {
8258 NL_SET_ERR_MSG(extack, "mtu less than device minimum");
8259 return -EINVAL;
8260 }
8261
8262 if (dev->max_mtu > 0 && new_mtu > dev->max_mtu) {
8263 NL_SET_ERR_MSG(extack, "mtu greater than device maximum");
8264 return -EINVAL;
8265 }
8266 return 0;
8267}
8268
f0db275a 8269/**
7a4c53be 8270 * dev_set_mtu_ext - Change maximum transfer unit
f0db275a
SH
8271 * @dev: device
8272 * @new_mtu: new transfer unit
7a4c53be 8273 * @extack: netlink extended ack
f0db275a
SH
8274 *
8275 * Change the maximum transfer size of the network device.
8276 */
7a4c53be
SH
8277int dev_set_mtu_ext(struct net_device *dev, int new_mtu,
8278 struct netlink_ext_ack *extack)
1da177e4 8279{
2315dc91 8280 int err, orig_mtu;
1da177e4
LT
8281
8282 if (new_mtu == dev->mtu)
8283 return 0;
8284
d836f5c6
ED
8285 err = dev_validate_mtu(dev, new_mtu, extack);
8286 if (err)
8287 return err;
1da177e4
LT
8288
8289 if (!netif_device_present(dev))
8290 return -ENODEV;
8291
1d486bfb
VF
8292 err = call_netdevice_notifiers(NETDEV_PRECHANGEMTU, dev);
8293 err = notifier_to_errno(err);
8294 if (err)
8295 return err;
d314774c 8296
2315dc91
VF
8297 orig_mtu = dev->mtu;
8298 err = __dev_set_mtu(dev, new_mtu);
d314774c 8299
2315dc91 8300 if (!err) {
af7d6cce
SD
8301 err = call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8302 orig_mtu);
2315dc91
VF
8303 err = notifier_to_errno(err);
8304 if (err) {
8305 /* setting mtu back and notifying everyone again,
8306 * so that they have a chance to revert changes.
8307 */
8308 __dev_set_mtu(dev, orig_mtu);
af7d6cce
SD
8309 call_netdevice_notifiers_mtu(NETDEV_CHANGEMTU, dev,
8310 new_mtu);
2315dc91
VF
8311 }
8312 }
1da177e4
LT
8313 return err;
8314}
7a4c53be
SH
8315
8316int dev_set_mtu(struct net_device *dev, int new_mtu)
8317{
8318 struct netlink_ext_ack extack;
8319 int err;
8320
a6bcfc89 8321 memset(&extack, 0, sizeof(extack));
7a4c53be 8322 err = dev_set_mtu_ext(dev, new_mtu, &extack);
a6bcfc89 8323 if (err && extack._msg)
7a4c53be
SH
8324 net_err_ratelimited("%s: %s\n", dev->name, extack._msg);
8325 return err;
8326}
d1b19dff 8327EXPORT_SYMBOL(dev_set_mtu);
1da177e4 8328
6a643ddb
CW
8329/**
8330 * dev_change_tx_queue_len - Change TX queue length of a netdevice
8331 * @dev: device
8332 * @new_len: new tx queue length
8333 */
8334int dev_change_tx_queue_len(struct net_device *dev, unsigned long new_len)
8335{
8336 unsigned int orig_len = dev->tx_queue_len;
8337 int res;
8338
8339 if (new_len != (unsigned int)new_len)
8340 return -ERANGE;
8341
8342 if (new_len != orig_len) {
8343 dev->tx_queue_len = new_len;
8344 res = call_netdevice_notifiers(NETDEV_CHANGE_TX_QUEUE_LEN, dev);
8345 res = notifier_to_errno(res);
7effaf06
TT
8346 if (res)
8347 goto err_rollback;
8348 res = dev_qdisc_change_tx_queue_len(dev);
8349 if (res)
8350 goto err_rollback;
6a643ddb
CW
8351 }
8352
8353 return 0;
7effaf06
TT
8354
8355err_rollback:
8356 netdev_err(dev, "refused to change device tx_queue_len\n");
8357 dev->tx_queue_len = orig_len;
8358 return res;
6a643ddb
CW
8359}
8360
cbda10fa
VD
8361/**
8362 * dev_set_group - Change group this device belongs to
8363 * @dev: device
8364 * @new_group: group this device should belong to
8365 */
8366void dev_set_group(struct net_device *dev, int new_group)
8367{
8368 dev->group = new_group;
8369}
8370EXPORT_SYMBOL(dev_set_group);
8371
d59cdf94
PM
8372/**
8373 * dev_pre_changeaddr_notify - Call NETDEV_PRE_CHANGEADDR.
8374 * @dev: device
8375 * @addr: new address
8376 * @extack: netlink extended ack
8377 */
8378int dev_pre_changeaddr_notify(struct net_device *dev, const char *addr,
8379 struct netlink_ext_ack *extack)
8380{
8381 struct netdev_notifier_pre_changeaddr_info info = {
8382 .info.dev = dev,
8383 .info.extack = extack,
8384 .dev_addr = addr,
8385 };
8386 int rc;
8387
8388 rc = call_netdevice_notifiers_info(NETDEV_PRE_CHANGEADDR, &info.info);
8389 return notifier_to_errno(rc);
8390}
8391EXPORT_SYMBOL(dev_pre_changeaddr_notify);
8392
f0db275a
SH
8393/**
8394 * dev_set_mac_address - Change Media Access Control Address
8395 * @dev: device
8396 * @sa: new address
3a37a963 8397 * @extack: netlink extended ack
f0db275a
SH
8398 *
8399 * Change the hardware (MAC) address of the device
8400 */
3a37a963
PM
8401int dev_set_mac_address(struct net_device *dev, struct sockaddr *sa,
8402 struct netlink_ext_ack *extack)
1da177e4 8403{
d314774c 8404 const struct net_device_ops *ops = dev->netdev_ops;
1da177e4
LT
8405 int err;
8406
d314774c 8407 if (!ops->ndo_set_mac_address)
1da177e4
LT
8408 return -EOPNOTSUPP;
8409 if (sa->sa_family != dev->type)
8410 return -EINVAL;
8411 if (!netif_device_present(dev))
8412 return -ENODEV;
d59cdf94
PM
8413 err = dev_pre_changeaddr_notify(dev, sa->sa_data, extack);
8414 if (err)
8415 return err;
d314774c 8416 err = ops->ndo_set_mac_address(dev, sa);
f6521516
JP
8417 if (err)
8418 return err;
fbdeca2d 8419 dev->addr_assign_type = NET_ADDR_SET;
f6521516 8420 call_netdevice_notifiers(NETDEV_CHANGEADDR, dev);
7bf23575 8421 add_device_randomness(dev->dev_addr, dev->addr_len);
f6521516 8422 return 0;
1da177e4 8423}
d1b19dff 8424EXPORT_SYMBOL(dev_set_mac_address);
1da177e4 8425
4bf84c35
JP
8426/**
8427 * dev_change_carrier - Change device carrier
8428 * @dev: device
691b3b7e 8429 * @new_carrier: new value
4bf84c35
JP
8430 *
8431 * Change device carrier
8432 */
8433int dev_change_carrier(struct net_device *dev, bool new_carrier)
8434{
8435 const struct net_device_ops *ops = dev->netdev_ops;
8436
8437 if (!ops->ndo_change_carrier)
8438 return -EOPNOTSUPP;
8439 if (!netif_device_present(dev))
8440 return -ENODEV;
8441 return ops->ndo_change_carrier(dev, new_carrier);
8442}
8443EXPORT_SYMBOL(dev_change_carrier);
8444
66b52b0d
JP
8445/**
8446 * dev_get_phys_port_id - Get device physical port ID
8447 * @dev: device
8448 * @ppid: port ID
8449 *
8450 * Get device physical port ID
8451 */
8452int dev_get_phys_port_id(struct net_device *dev,
02637fce 8453 struct netdev_phys_item_id *ppid)
66b52b0d
JP
8454{
8455 const struct net_device_ops *ops = dev->netdev_ops;
8456
8457 if (!ops->ndo_get_phys_port_id)
8458 return -EOPNOTSUPP;
8459 return ops->ndo_get_phys_port_id(dev, ppid);
8460}
8461EXPORT_SYMBOL(dev_get_phys_port_id);
8462
db24a904
DA
8463/**
8464 * dev_get_phys_port_name - Get device physical port name
8465 * @dev: device
8466 * @name: port name
ed49e650 8467 * @len: limit of bytes to copy to name
db24a904
DA
8468 *
8469 * Get device physical port name
8470 */
8471int dev_get_phys_port_name(struct net_device *dev,
8472 char *name, size_t len)
8473{
8474 const struct net_device_ops *ops = dev->netdev_ops;
af3836df 8475 int err;
db24a904 8476
af3836df
JP
8477 if (ops->ndo_get_phys_port_name) {
8478 err = ops->ndo_get_phys_port_name(dev, name, len);
8479 if (err != -EOPNOTSUPP)
8480 return err;
8481 }
8482 return devlink_compat_phys_port_name_get(dev, name, len);
db24a904
DA
8483}
8484EXPORT_SYMBOL(dev_get_phys_port_name);
8485
d6abc596
FF
8486/**
8487 * dev_get_port_parent_id - Get the device's port parent identifier
8488 * @dev: network device
8489 * @ppid: pointer to a storage for the port's parent identifier
8490 * @recurse: allow/disallow recursion to lower devices
8491 *
8492 * Get the devices's port parent identifier
8493 */
8494int dev_get_port_parent_id(struct net_device *dev,
8495 struct netdev_phys_item_id *ppid,
8496 bool recurse)
8497{
8498 const struct net_device_ops *ops = dev->netdev_ops;
8499 struct netdev_phys_item_id first = { };
8500 struct net_device *lower_dev;
8501 struct list_head *iter;
7e1146e8
JP
8502 int err;
8503
8504 if (ops->ndo_get_port_parent_id) {
8505 err = ops->ndo_get_port_parent_id(dev, ppid);
8506 if (err != -EOPNOTSUPP)
8507 return err;
8508 }
d6abc596 8509
7e1146e8
JP
8510 err = devlink_compat_switch_id_get(dev, ppid);
8511 if (!err || err != -EOPNOTSUPP)
8512 return err;
d6abc596
FF
8513
8514 if (!recurse)
7e1146e8 8515 return -EOPNOTSUPP;
d6abc596
FF
8516
8517 netdev_for_each_lower_dev(dev, lower_dev, iter) {
8518 err = dev_get_port_parent_id(lower_dev, ppid, recurse);
8519 if (err)
8520 break;
8521 if (!first.id_len)
8522 first = *ppid;
8523 else if (memcmp(&first, ppid, sizeof(*ppid)))
8524 return -ENODATA;
8525 }
8526
8527 return err;
8528}
8529EXPORT_SYMBOL(dev_get_port_parent_id);
8530
8531/**
8532 * netdev_port_same_parent_id - Indicate if two network devices have
8533 * the same port parent identifier
8534 * @a: first network device
8535 * @b: second network device
8536 */
8537bool netdev_port_same_parent_id(struct net_device *a, struct net_device *b)
8538{
8539 struct netdev_phys_item_id a_id = { };
8540 struct netdev_phys_item_id b_id = { };
8541
8542 if (dev_get_port_parent_id(a, &a_id, true) ||
8543 dev_get_port_parent_id(b, &b_id, true))
8544 return false;
8545
8546 return netdev_phys_item_id_same(&a_id, &b_id);
8547}
8548EXPORT_SYMBOL(netdev_port_same_parent_id);
8549
d746d707
AK
8550/**
8551 * dev_change_proto_down - update protocol port state information
8552 * @dev: device
8553 * @proto_down: new value
8554 *
8555 * This info can be used by switch drivers to set the phys state of the
8556 * port.
8557 */
8558int dev_change_proto_down(struct net_device *dev, bool proto_down)
8559{
8560 const struct net_device_ops *ops = dev->netdev_ops;
8561
8562 if (!ops->ndo_change_proto_down)
8563 return -EOPNOTSUPP;
8564 if (!netif_device_present(dev))
8565 return -ENODEV;
8566 return ops->ndo_change_proto_down(dev, proto_down);
8567}
8568EXPORT_SYMBOL(dev_change_proto_down);
8569
b5899679
AR
8570/**
8571 * dev_change_proto_down_generic - generic implementation for
8572 * ndo_change_proto_down that sets carrier according to
8573 * proto_down.
8574 *
8575 * @dev: device
8576 * @proto_down: new value
8577 */
8578int dev_change_proto_down_generic(struct net_device *dev, bool proto_down)
8579{
8580 if (proto_down)
8581 netif_carrier_off(dev);
8582 else
8583 netif_carrier_on(dev);
8584 dev->proto_down = proto_down;
8585 return 0;
8586}
8587EXPORT_SYMBOL(dev_change_proto_down_generic);
8588
a25717d2
JK
8589u32 __dev_xdp_query(struct net_device *dev, bpf_op_t bpf_op,
8590 enum bpf_netdev_command cmd)
d67b9cd2 8591{
a25717d2 8592 struct netdev_bpf xdp;
d67b9cd2 8593
a25717d2
JK
8594 if (!bpf_op)
8595 return 0;
118b4aa2 8596
a25717d2
JK
8597 memset(&xdp, 0, sizeof(xdp));
8598 xdp.command = cmd;
118b4aa2 8599
a25717d2
JK
8600 /* Query must always succeed. */
8601 WARN_ON(bpf_op(dev, &xdp) < 0 && cmd == XDP_QUERY_PROG);
58038695 8602
6b867589 8603 return xdp.prog_id;
d67b9cd2
DB
8604}
8605
f4e63525 8606static int dev_xdp_install(struct net_device *dev, bpf_op_t bpf_op,
32d60277 8607 struct netlink_ext_ack *extack, u32 flags,
d67b9cd2
DB
8608 struct bpf_prog *prog)
8609{
7e6897f9
BT
8610 bool non_hw = !(flags & XDP_FLAGS_HW_MODE);
8611 struct bpf_prog *prev_prog = NULL;
f4e63525 8612 struct netdev_bpf xdp;
7e6897f9
BT
8613 int err;
8614
8615 if (non_hw) {
8616 prev_prog = bpf_prog_by_id(__dev_xdp_query(dev, bpf_op,
8617 XDP_QUERY_PROG));
8618 if (IS_ERR(prev_prog))
8619 prev_prog = NULL;
8620 }
d67b9cd2
DB
8621
8622 memset(&xdp, 0, sizeof(xdp));
ee5d032f
JK
8623 if (flags & XDP_FLAGS_HW_MODE)
8624 xdp.command = XDP_SETUP_PROG_HW;
8625 else
8626 xdp.command = XDP_SETUP_PROG;
d67b9cd2 8627 xdp.extack = extack;
32d60277 8628 xdp.flags = flags;
d67b9cd2
DB
8629 xdp.prog = prog;
8630
7e6897f9
BT
8631 err = bpf_op(dev, &xdp);
8632 if (!err && non_hw)
8633 bpf_prog_change_xdp(prev_prog, prog);
8634
8635 if (prev_prog)
8636 bpf_prog_put(prev_prog);
8637
8638 return err;
d67b9cd2
DB
8639}
8640
bd0b2e7f
JK
8641static void dev_xdp_uninstall(struct net_device *dev)
8642{
8643 struct netdev_bpf xdp;
8644 bpf_op_t ndo_bpf;
8645
8646 /* Remove generic XDP */
8647 WARN_ON(dev_xdp_install(dev, generic_xdp_install, NULL, 0, NULL));
8648
8649 /* Remove from the driver */
8650 ndo_bpf = dev->netdev_ops->ndo_bpf;
8651 if (!ndo_bpf)
8652 return;
8653
a25717d2
JK
8654 memset(&xdp, 0, sizeof(xdp));
8655 xdp.command = XDP_QUERY_PROG;
8656 WARN_ON(ndo_bpf(dev, &xdp));
8657 if (xdp.prog_id)
8658 WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags,
8659 NULL));
bd0b2e7f 8660
a25717d2
JK
8661 /* Remove HW offload */
8662 memset(&xdp, 0, sizeof(xdp));
8663 xdp.command = XDP_QUERY_PROG_HW;
8664 if (!ndo_bpf(dev, &xdp) && xdp.prog_id)
8665 WARN_ON(dev_xdp_install(dev, ndo_bpf, NULL, xdp.prog_flags,
8666 NULL));
bd0b2e7f
JK
8667}
8668
a7862b45
BB
8669/**
8670 * dev_change_xdp_fd - set or clear a bpf program for a device rx path
8671 * @dev: device
b5d60989 8672 * @extack: netlink extended ack
a7862b45 8673 * @fd: new program fd or negative value to clear
85de8576 8674 * @flags: xdp-related flags
a7862b45
BB
8675 *
8676 * Set or clear a bpf program for a device
8677 */
ddf9f970
JK
8678int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
8679 int fd, u32 flags)
a7862b45
BB
8680{
8681 const struct net_device_ops *ops = dev->netdev_ops;
a25717d2 8682 enum bpf_netdev_command query;
a7862b45 8683 struct bpf_prog *prog = NULL;
f4e63525 8684 bpf_op_t bpf_op, bpf_chk;
9ee963d6 8685 bool offload;
a7862b45
BB
8686 int err;
8687
85de8576
DB
8688 ASSERT_RTNL();
8689
9ee963d6
JK
8690 offload = flags & XDP_FLAGS_HW_MODE;
8691 query = offload ? XDP_QUERY_PROG_HW : XDP_QUERY_PROG;
a25717d2 8692
f4e63525 8693 bpf_op = bpf_chk = ops->ndo_bpf;
01dde20c
MF
8694 if (!bpf_op && (flags & (XDP_FLAGS_DRV_MODE | XDP_FLAGS_HW_MODE))) {
8695 NL_SET_ERR_MSG(extack, "underlying driver does not support XDP in native mode");
0489df9a 8696 return -EOPNOTSUPP;
01dde20c 8697 }
f4e63525
JK
8698 if (!bpf_op || (flags & XDP_FLAGS_SKB_MODE))
8699 bpf_op = generic_xdp_install;
8700 if (bpf_op == bpf_chk)
8701 bpf_chk = generic_xdp_install;
b5cdae32 8702
a7862b45 8703 if (fd >= 0) {
c14a9f63
MM
8704 u32 prog_id;
8705
9ee963d6 8706 if (!offload && __dev_xdp_query(dev, bpf_chk, XDP_QUERY_PROG)) {
01dde20c 8707 NL_SET_ERR_MSG(extack, "native and generic XDP can't be active at the same time");
d67b9cd2 8708 return -EEXIST;
01dde20c 8709 }
c14a9f63
MM
8710
8711 prog_id = __dev_xdp_query(dev, bpf_op, query);
8712 if ((flags & XDP_FLAGS_UPDATE_IF_NOEXIST) && prog_id) {
01dde20c 8713 NL_SET_ERR_MSG(extack, "XDP program already attached");
d67b9cd2 8714 return -EBUSY;
01dde20c 8715 }
85de8576 8716
288b3de5
JK
8717 prog = bpf_prog_get_type_dev(fd, BPF_PROG_TYPE_XDP,
8718 bpf_op == ops->ndo_bpf);
a7862b45
BB
8719 if (IS_ERR(prog))
8720 return PTR_ERR(prog);
441a3303 8721
9ee963d6 8722 if (!offload && bpf_prog_is_dev_bound(prog->aux)) {
441a3303
JK
8723 NL_SET_ERR_MSG(extack, "using device-bound program without HW_MODE flag is not supported");
8724 bpf_prog_put(prog);
8725 return -EINVAL;
8726 }
c14a9f63 8727
aefc3e72
JK
8728 /* prog->aux->id may be 0 for orphaned device-bound progs */
8729 if (prog->aux->id && prog->aux->id == prog_id) {
c14a9f63
MM
8730 bpf_prog_put(prog);
8731 return 0;
8732 }
8733 } else {
8734 if (!__dev_xdp_query(dev, bpf_op, query))
8735 return 0;
a7862b45
BB
8736 }
8737
f4e63525 8738 err = dev_xdp_install(dev, bpf_op, extack, flags, prog);
a7862b45
BB
8739 if (err < 0 && prog)
8740 bpf_prog_put(prog);
8741
8742 return err;
8743}
a7862b45 8744
1da177e4
LT
8745/**
8746 * dev_new_index - allocate an ifindex
c4ea43c5 8747 * @net: the applicable net namespace
1da177e4
LT
8748 *
8749 * Returns a suitable unique value for a new device interface
8750 * number. The caller must hold the rtnl semaphore or the
8751 * dev_base_lock to be sure it remains unique.
8752 */
881d966b 8753static int dev_new_index(struct net *net)
1da177e4 8754{
aa79e66e 8755 int ifindex = net->ifindex;
f4563a75 8756
1da177e4
LT
8757 for (;;) {
8758 if (++ifindex <= 0)
8759 ifindex = 1;
881d966b 8760 if (!__dev_get_by_index(net, ifindex))
aa79e66e 8761 return net->ifindex = ifindex;
1da177e4
LT
8762 }
8763}
8764
1da177e4 8765/* Delayed registration/unregisteration */
3b5b34fd 8766static LIST_HEAD(net_todo_list);
200b916f 8767DECLARE_WAIT_QUEUE_HEAD(netdev_unregistering_wq);
1da177e4 8768
6f05f629 8769static void net_set_todo(struct net_device *dev)
1da177e4 8770{
1da177e4 8771 list_add_tail(&dev->todo_list, &net_todo_list);
50624c93 8772 dev_net(dev)->dev_unreg_count++;
1da177e4
LT
8773}
8774
9b5e383c 8775static void rollback_registered_many(struct list_head *head)
93ee31f1 8776{
e93737b0 8777 struct net_device *dev, *tmp;
5cde2829 8778 LIST_HEAD(close_head);
9b5e383c 8779
93ee31f1
DL
8780 BUG_ON(dev_boot_phase);
8781 ASSERT_RTNL();
8782
e93737b0 8783 list_for_each_entry_safe(dev, tmp, head, unreg_list) {
9b5e383c 8784 /* Some devices call without registering
e93737b0
KK
8785 * for initialization unwind. Remove those
8786 * devices and proceed with the remaining.
9b5e383c
ED
8787 */
8788 if (dev->reg_state == NETREG_UNINITIALIZED) {
7b6cd1ce
JP
8789 pr_debug("unregister_netdevice: device %s/%p never was registered\n",
8790 dev->name, dev);
93ee31f1 8791
9b5e383c 8792 WARN_ON(1);
e93737b0
KK
8793 list_del(&dev->unreg_list);
8794 continue;
9b5e383c 8795 }
449f4544 8796 dev->dismantle = true;
9b5e383c 8797 BUG_ON(dev->reg_state != NETREG_REGISTERED);
44345724 8798 }
93ee31f1 8799
44345724 8800 /* If device is running, close it first. */
5cde2829
EB
8801 list_for_each_entry(dev, head, unreg_list)
8802 list_add_tail(&dev->close_list, &close_head);
99c4a26a 8803 dev_close_many(&close_head, true);
93ee31f1 8804
44345724 8805 list_for_each_entry(dev, head, unreg_list) {
9b5e383c
ED
8806 /* And unlink it from device chain. */
8807 unlist_netdevice(dev);
93ee31f1 8808
9b5e383c
ED
8809 dev->reg_state = NETREG_UNREGISTERING;
8810 }
41852497 8811 flush_all_backlogs();
93ee31f1
DL
8812
8813 synchronize_net();
8814
9b5e383c 8815 list_for_each_entry(dev, head, unreg_list) {
395eea6c
MB
8816 struct sk_buff *skb = NULL;
8817
9b5e383c
ED
8818 /* Shutdown queueing discipline. */
8819 dev_shutdown(dev);
93ee31f1 8820
bd0b2e7f 8821 dev_xdp_uninstall(dev);
93ee31f1 8822
9b5e383c 8823 /* Notify protocols, that we are about to destroy
eb13da1a 8824 * this device. They should clean all the things.
8825 */
9b5e383c 8826 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
93ee31f1 8827
395eea6c
MB
8828 if (!dev->rtnl_link_ops ||
8829 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
3d3ea5af 8830 skb = rtmsg_ifinfo_build_skb(RTM_DELLINK, dev, ~0U, 0,
38e01b30 8831 GFP_KERNEL, NULL, 0);
395eea6c 8832
9b5e383c
ED
8833 /*
8834 * Flush the unicast and multicast chains
8835 */
a748ee24 8836 dev_uc_flush(dev);
22bedad3 8837 dev_mc_flush(dev);
93ee31f1 8838
36fbf1e5 8839 netdev_name_node_alt_flush(dev);
ff927412
JP
8840 netdev_name_node_free(dev->name_node);
8841
9b5e383c
ED
8842 if (dev->netdev_ops->ndo_uninit)
8843 dev->netdev_ops->ndo_uninit(dev);
93ee31f1 8844
395eea6c
MB
8845 if (skb)
8846 rtmsg_ifinfo_send(skb, dev, GFP_KERNEL);
56bfa7ee 8847
9ff162a8
JP
8848 /* Notifier chain MUST detach us all upper devices. */
8849 WARN_ON(netdev_has_any_upper_dev(dev));
0f524a80 8850 WARN_ON(netdev_has_any_lower_dev(dev));
93ee31f1 8851
9b5e383c
ED
8852 /* Remove entries from kobject tree */
8853 netdev_unregister_kobject(dev);
024e9679
AD
8854#ifdef CONFIG_XPS
8855 /* Remove XPS queueing entries */
8856 netif_reset_xps_queues_gt(dev, 0);
8857#endif
9b5e383c 8858 }
93ee31f1 8859
850a545b 8860 synchronize_net();
395264d5 8861
a5ee1551 8862 list_for_each_entry(dev, head, unreg_list)
9b5e383c
ED
8863 dev_put(dev);
8864}
8865
8866static void rollback_registered(struct net_device *dev)
8867{
8868 LIST_HEAD(single);
8869
8870 list_add(&dev->unreg_list, &single);
8871 rollback_registered_many(&single);
ceaaec98 8872 list_del(&single);
93ee31f1
DL
8873}
8874
fd867d51
JW
8875static netdev_features_t netdev_sync_upper_features(struct net_device *lower,
8876 struct net_device *upper, netdev_features_t features)
8877{
8878 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
8879 netdev_features_t feature;
5ba3f7d6 8880 int feature_bit;
fd867d51 8881
3b89ea9c 8882 for_each_netdev_feature(upper_disables, feature_bit) {
5ba3f7d6 8883 feature = __NETIF_F_BIT(feature_bit);
fd867d51
JW
8884 if (!(upper->wanted_features & feature)
8885 && (features & feature)) {
8886 netdev_dbg(lower, "Dropping feature %pNF, upper dev %s has it off.\n",
8887 &feature, upper->name);
8888 features &= ~feature;
8889 }
8890 }
8891
8892 return features;
8893}
8894
8895static void netdev_sync_lower_features(struct net_device *upper,
8896 struct net_device *lower, netdev_features_t features)
8897{
8898 netdev_features_t upper_disables = NETIF_F_UPPER_DISABLES;
8899 netdev_features_t feature;
5ba3f7d6 8900 int feature_bit;
fd867d51 8901
3b89ea9c 8902 for_each_netdev_feature(upper_disables, feature_bit) {
5ba3f7d6 8903 feature = __NETIF_F_BIT(feature_bit);
fd867d51
JW
8904 if (!(features & feature) && (lower->features & feature)) {
8905 netdev_dbg(upper, "Disabling feature %pNF on lower dev %s.\n",
8906 &feature, lower->name);
8907 lower->wanted_features &= ~feature;
8908 netdev_update_features(lower);
8909
8910 if (unlikely(lower->features & feature))
8911 netdev_WARN(upper, "failed to disable %pNF on %s!\n",
8912 &feature, lower->name);
8913 }
8914 }
8915}
8916
c8f44aff
MM
8917static netdev_features_t netdev_fix_features(struct net_device *dev,
8918 netdev_features_t features)
b63365a2 8919{
57422dc5
MM
8920 /* Fix illegal checksum combinations */
8921 if ((features & NETIF_F_HW_CSUM) &&
8922 (features & (NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM))) {
6f404e44 8923 netdev_warn(dev, "mixed HW and IP checksum settings.\n");
57422dc5
MM
8924 features &= ~(NETIF_F_IP_CSUM|NETIF_F_IPV6_CSUM);
8925 }
8926
b63365a2 8927 /* TSO requires that SG is present as well. */
ea2d3688 8928 if ((features & NETIF_F_ALL_TSO) && !(features & NETIF_F_SG)) {
6f404e44 8929 netdev_dbg(dev, "Dropping TSO features since no SG feature.\n");
ea2d3688 8930 features &= ~NETIF_F_ALL_TSO;
b63365a2
HX
8931 }
8932
ec5f0615
PS
8933 if ((features & NETIF_F_TSO) && !(features & NETIF_F_HW_CSUM) &&
8934 !(features & NETIF_F_IP_CSUM)) {
8935 netdev_dbg(dev, "Dropping TSO features since no CSUM feature.\n");
8936 features &= ~NETIF_F_TSO;
8937 features &= ~NETIF_F_TSO_ECN;
8938 }
8939
8940 if ((features & NETIF_F_TSO6) && !(features & NETIF_F_HW_CSUM) &&
8941 !(features & NETIF_F_IPV6_CSUM)) {
8942 netdev_dbg(dev, "Dropping TSO6 features since no CSUM feature.\n");
8943 features &= ~NETIF_F_TSO6;
8944 }
8945
b1dc497b
AD
8946 /* TSO with IPv4 ID mangling requires IPv4 TSO be enabled */
8947 if ((features & NETIF_F_TSO_MANGLEID) && !(features & NETIF_F_TSO))
8948 features &= ~NETIF_F_TSO_MANGLEID;
8949
31d8b9e0
BH
8950 /* TSO ECN requires that TSO is present as well. */
8951 if ((features & NETIF_F_ALL_TSO) == NETIF_F_TSO_ECN)
8952 features &= ~NETIF_F_TSO_ECN;
8953
212b573f
MM
8954 /* Software GSO depends on SG. */
8955 if ((features & NETIF_F_GSO) && !(features & NETIF_F_SG)) {
6f404e44 8956 netdev_dbg(dev, "Dropping NETIF_F_GSO since no SG feature.\n");
212b573f
MM
8957 features &= ~NETIF_F_GSO;
8958 }
8959
802ab55a
AD
8960 /* GSO partial features require GSO partial be set */
8961 if ((features & dev->gso_partial_features) &&
8962 !(features & NETIF_F_GSO_PARTIAL)) {
8963 netdev_dbg(dev,
8964 "Dropping partially supported GSO features since no GSO partial.\n");
8965 features &= ~dev->gso_partial_features;
8966 }
8967
fb1f5f79
MC
8968 if (!(features & NETIF_F_RXCSUM)) {
8969 /* NETIF_F_GRO_HW implies doing RXCSUM since every packet
8970 * successfully merged by hardware must also have the
8971 * checksum verified by hardware. If the user does not
8972 * want to enable RXCSUM, logically, we should disable GRO_HW.
8973 */
8974 if (features & NETIF_F_GRO_HW) {
8975 netdev_dbg(dev, "Dropping NETIF_F_GRO_HW since no RXCSUM feature.\n");
8976 features &= ~NETIF_F_GRO_HW;
8977 }
8978 }
8979
de8d5ab2
GP
8980 /* LRO/HW-GRO features cannot be combined with RX-FCS */
8981 if (features & NETIF_F_RXFCS) {
8982 if (features & NETIF_F_LRO) {
8983 netdev_dbg(dev, "Dropping LRO feature since RX-FCS is requested.\n");
8984 features &= ~NETIF_F_LRO;
8985 }
8986
8987 if (features & NETIF_F_GRO_HW) {
8988 netdev_dbg(dev, "Dropping HW-GRO feature since RX-FCS is requested.\n");
8989 features &= ~NETIF_F_GRO_HW;
8990 }
e6c6a929
GP
8991 }
8992
b63365a2
HX
8993 return features;
8994}
b63365a2 8995
6cb6a27c 8996int __netdev_update_features(struct net_device *dev)
5455c699 8997{
fd867d51 8998 struct net_device *upper, *lower;
c8f44aff 8999 netdev_features_t features;
fd867d51 9000 struct list_head *iter;
e7868a85 9001 int err = -1;
5455c699 9002
87267485
MM
9003 ASSERT_RTNL();
9004
5455c699
MM
9005 features = netdev_get_wanted_features(dev);
9006
9007 if (dev->netdev_ops->ndo_fix_features)
9008 features = dev->netdev_ops->ndo_fix_features(dev, features);
9009
9010 /* driver might be less strict about feature dependencies */
9011 features = netdev_fix_features(dev, features);
9012
fd867d51
JW
9013 /* some features can't be enabled if they're off an an upper device */
9014 netdev_for_each_upper_dev_rcu(dev, upper, iter)
9015 features = netdev_sync_upper_features(dev, upper, features);
9016
5455c699 9017 if (dev->features == features)
e7868a85 9018 goto sync_lower;
5455c699 9019
c8f44aff
MM
9020 netdev_dbg(dev, "Features changed: %pNF -> %pNF\n",
9021 &dev->features, &features);
5455c699
MM
9022
9023 if (dev->netdev_ops->ndo_set_features)
9024 err = dev->netdev_ops->ndo_set_features(dev, features);
5f8dc33e
NA
9025 else
9026 err = 0;
5455c699 9027
6cb6a27c 9028 if (unlikely(err < 0)) {
5455c699 9029 netdev_err(dev,
c8f44aff
MM
9030 "set_features() failed (%d); wanted %pNF, left %pNF\n",
9031 err, &features, &dev->features);
17b85d29
NA
9032 /* return non-0 since some features might have changed and
9033 * it's better to fire a spurious notification than miss it
9034 */
9035 return -1;
6cb6a27c
MM
9036 }
9037
e7868a85 9038sync_lower:
fd867d51
JW
9039 /* some features must be disabled on lower devices when disabled
9040 * on an upper device (think: bonding master or bridge)
9041 */
9042 netdev_for_each_lower_dev(dev, lower, iter)
9043 netdev_sync_lower_features(dev, lower, features);
9044
ae847f40
SD
9045 if (!err) {
9046 netdev_features_t diff = features ^ dev->features;
9047
9048 if (diff & NETIF_F_RX_UDP_TUNNEL_PORT) {
9049 /* udp_tunnel_{get,drop}_rx_info both need
9050 * NETIF_F_RX_UDP_TUNNEL_PORT enabled on the
9051 * device, or they won't do anything.
9052 * Thus we need to update dev->features
9053 * *before* calling udp_tunnel_get_rx_info,
9054 * but *after* calling udp_tunnel_drop_rx_info.
9055 */
9056 if (features & NETIF_F_RX_UDP_TUNNEL_PORT) {
9057 dev->features = features;
9058 udp_tunnel_get_rx_info(dev);
9059 } else {
9060 udp_tunnel_drop_rx_info(dev);
9061 }
9062 }
9063
9daae9bd
GP
9064 if (diff & NETIF_F_HW_VLAN_CTAG_FILTER) {
9065 if (features & NETIF_F_HW_VLAN_CTAG_FILTER) {
9066 dev->features = features;
9067 err |= vlan_get_rx_ctag_filter_info(dev);
9068 } else {
9069 vlan_drop_rx_ctag_filter_info(dev);
9070 }
9071 }
9072
9073 if (diff & NETIF_F_HW_VLAN_STAG_FILTER) {
9074 if (features & NETIF_F_HW_VLAN_STAG_FILTER) {
9075 dev->features = features;
9076 err |= vlan_get_rx_stag_filter_info(dev);
9077 } else {
9078 vlan_drop_rx_stag_filter_info(dev);
9079 }
9080 }
9081
6cb6a27c 9082 dev->features = features;
ae847f40 9083 }
6cb6a27c 9084
e7868a85 9085 return err < 0 ? 0 : 1;
6cb6a27c
MM
9086}
9087
afe12cc8
MM
9088/**
9089 * netdev_update_features - recalculate device features
9090 * @dev: the device to check
9091 *
9092 * Recalculate dev->features set and send notifications if it
9093 * has changed. Should be called after driver or hardware dependent
9094 * conditions might have changed that influence the features.
9095 */
6cb6a27c
MM
9096void netdev_update_features(struct net_device *dev)
9097{
9098 if (__netdev_update_features(dev))
9099 netdev_features_change(dev);
5455c699
MM
9100}
9101EXPORT_SYMBOL(netdev_update_features);
9102
afe12cc8
MM
9103/**
9104 * netdev_change_features - recalculate device features
9105 * @dev: the device to check
9106 *
9107 * Recalculate dev->features set and send notifications even
9108 * if they have not changed. Should be called instead of
9109 * netdev_update_features() if also dev->vlan_features might
9110 * have changed to allow the changes to be propagated to stacked
9111 * VLAN devices.
9112 */
9113void netdev_change_features(struct net_device *dev)
9114{
9115 __netdev_update_features(dev);
9116 netdev_features_change(dev);
9117}
9118EXPORT_SYMBOL(netdev_change_features);
9119
fc4a7489
PM
9120/**
9121 * netif_stacked_transfer_operstate - transfer operstate
9122 * @rootdev: the root or lower level device to transfer state from
9123 * @dev: the device to transfer operstate to
9124 *
9125 * Transfer operational state from root to device. This is normally
9126 * called when a stacking relationship exists between the root
9127 * device and the device(a leaf device).
9128 */
9129void netif_stacked_transfer_operstate(const struct net_device *rootdev,
9130 struct net_device *dev)
9131{
9132 if (rootdev->operstate == IF_OPER_DORMANT)
9133 netif_dormant_on(dev);
9134 else
9135 netif_dormant_off(dev);
9136
0575c86b
ZS
9137 if (netif_carrier_ok(rootdev))
9138 netif_carrier_on(dev);
9139 else
9140 netif_carrier_off(dev);
fc4a7489
PM
9141}
9142EXPORT_SYMBOL(netif_stacked_transfer_operstate);
9143
1b4bf461
ED
9144static int netif_alloc_rx_queues(struct net_device *dev)
9145{
1b4bf461 9146 unsigned int i, count = dev->num_rx_queues;
bd25fa7b 9147 struct netdev_rx_queue *rx;
10595902 9148 size_t sz = count * sizeof(*rx);
e817f856 9149 int err = 0;
1b4bf461 9150
bd25fa7b 9151 BUG_ON(count < 1);
1b4bf461 9152
dcda9b04 9153 rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
da6bc57a
MH
9154 if (!rx)
9155 return -ENOMEM;
9156
bd25fa7b
TH
9157 dev->_rx = rx;
9158
e817f856 9159 for (i = 0; i < count; i++) {
fe822240 9160 rx[i].dev = dev;
e817f856
JDB
9161
9162 /* XDP RX-queue setup */
9163 err = xdp_rxq_info_reg(&rx[i].xdp_rxq, dev, i);
9164 if (err < 0)
9165 goto err_rxq_info;
9166 }
1b4bf461 9167 return 0;
e817f856
JDB
9168
9169err_rxq_info:
9170 /* Rollback successful reg's and free other resources */
9171 while (i--)
9172 xdp_rxq_info_unreg(&rx[i].xdp_rxq);
141b52a9 9173 kvfree(dev->_rx);
e817f856
JDB
9174 dev->_rx = NULL;
9175 return err;
9176}
9177
9178static void netif_free_rx_queues(struct net_device *dev)
9179{
9180 unsigned int i, count = dev->num_rx_queues;
e817f856
JDB
9181
9182 /* netif_alloc_rx_queues alloc failed, resources have been unreg'ed */
9183 if (!dev->_rx)
9184 return;
9185
e817f856 9186 for (i = 0; i < count; i++)
82aaff2f
JK
9187 xdp_rxq_info_unreg(&dev->_rx[i].xdp_rxq);
9188
9189 kvfree(dev->_rx);
1b4bf461
ED
9190}
9191
aa942104
CG
9192static void netdev_init_one_queue(struct net_device *dev,
9193 struct netdev_queue *queue, void *_unused)
9194{
9195 /* Initialize queue lock */
9196 spin_lock_init(&queue->_xmit_lock);
ab92d68f 9197 lockdep_set_class(&queue->_xmit_lock, &dev->qdisc_xmit_lock_key);
aa942104 9198 queue->xmit_lock_owner = -1;
b236da69 9199 netdev_queue_numa_node_write(queue, NUMA_NO_NODE);
aa942104 9200 queue->dev = dev;
114cf580
TH
9201#ifdef CONFIG_BQL
9202 dql_init(&queue->dql, HZ);
9203#endif
aa942104
CG
9204}
9205
60877a32
ED
9206static void netif_free_tx_queues(struct net_device *dev)
9207{
4cb28970 9208 kvfree(dev->_tx);
60877a32
ED
9209}
9210
e6484930
TH
9211static int netif_alloc_netdev_queues(struct net_device *dev)
9212{
9213 unsigned int count = dev->num_tx_queues;
9214 struct netdev_queue *tx;
60877a32 9215 size_t sz = count * sizeof(*tx);
e6484930 9216
d339727c
ED
9217 if (count < 1 || count > 0xffff)
9218 return -EINVAL;
62b5942a 9219
dcda9b04 9220 tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
da6bc57a
MH
9221 if (!tx)
9222 return -ENOMEM;
9223
e6484930 9224 dev->_tx = tx;
1d24eb48 9225
e6484930
TH
9226 netdev_for_each_tx_queue(dev, netdev_init_one_queue, NULL);
9227 spin_lock_init(&dev->tx_global_lock);
aa942104
CG
9228
9229 return 0;
e6484930
TH
9230}
9231
a2029240
DV
9232void netif_tx_stop_all_queues(struct net_device *dev)
9233{
9234 unsigned int i;
9235
9236 for (i = 0; i < dev->num_tx_queues; i++) {
9237 struct netdev_queue *txq = netdev_get_tx_queue(dev, i);
f4563a75 9238
a2029240
DV
9239 netif_tx_stop_queue(txq);
9240 }
9241}
9242EXPORT_SYMBOL(netif_tx_stop_all_queues);
9243
ab92d68f
TY
9244static void netdev_register_lockdep_key(struct net_device *dev)
9245{
9246 lockdep_register_key(&dev->qdisc_tx_busylock_key);
9247 lockdep_register_key(&dev->qdisc_running_key);
9248 lockdep_register_key(&dev->qdisc_xmit_lock_key);
9249 lockdep_register_key(&dev->addr_list_lock_key);
9250}
9251
9252static void netdev_unregister_lockdep_key(struct net_device *dev)
9253{
9254 lockdep_unregister_key(&dev->qdisc_tx_busylock_key);
9255 lockdep_unregister_key(&dev->qdisc_running_key);
9256 lockdep_unregister_key(&dev->qdisc_xmit_lock_key);
9257 lockdep_unregister_key(&dev->addr_list_lock_key);
9258}
9259
9260void netdev_update_lockdep_key(struct net_device *dev)
9261{
ab92d68f 9262 lockdep_unregister_key(&dev->addr_list_lock_key);
ab92d68f
TY
9263 lockdep_register_key(&dev->addr_list_lock_key);
9264
9265 lockdep_set_class(&dev->addr_list_lock, &dev->addr_list_lock_key);
ab92d68f
TY
9266}
9267EXPORT_SYMBOL(netdev_update_lockdep_key);
9268
1da177e4
LT
9269/**
9270 * register_netdevice - register a network device
9271 * @dev: device to register
9272 *
9273 * Take a completed network device structure and add it to the kernel
9274 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9275 * chain. 0 is returned on success. A negative errno code is returned
9276 * on a failure to set up the device, or if the name is a duplicate.
9277 *
9278 * Callers must hold the rtnl semaphore. You may want
9279 * register_netdev() instead of this.
9280 *
9281 * BUGS:
9282 * The locking appears insufficient to guarantee two parallel registers
9283 * will not get the same name.
9284 */
9285
9286int register_netdevice(struct net_device *dev)
9287{
1da177e4 9288 int ret;
d314774c 9289 struct net *net = dev_net(dev);
1da177e4 9290
e283de3a
FF
9291 BUILD_BUG_ON(sizeof(netdev_features_t) * BITS_PER_BYTE <
9292 NETDEV_FEATURE_COUNT);
1da177e4
LT
9293 BUG_ON(dev_boot_phase);
9294 ASSERT_RTNL();
9295
b17a7c17
SH
9296 might_sleep();
9297
1da177e4
LT
9298 /* When net_device's are persistent, this will be fatal. */
9299 BUG_ON(dev->reg_state != NETREG_UNINITIALIZED);
d314774c 9300 BUG_ON(!net);
1da177e4 9301
f1f28aa3 9302 spin_lock_init(&dev->addr_list_lock);
ab92d68f 9303 lockdep_set_class(&dev->addr_list_lock, &dev->addr_list_lock_key);
1da177e4 9304
828de4f6 9305 ret = dev_get_valid_name(net, dev, dev->name);
0696c3a8
PP
9306 if (ret < 0)
9307 goto out;
9308
9077f052 9309 ret = -ENOMEM;
ff927412
JP
9310 dev->name_node = netdev_name_node_head_alloc(dev);
9311 if (!dev->name_node)
9312 goto out;
9313
1da177e4 9314 /* Init, if this function is available */
d314774c
SH
9315 if (dev->netdev_ops->ndo_init) {
9316 ret = dev->netdev_ops->ndo_init(dev);
1da177e4
LT
9317 if (ret) {
9318 if (ret > 0)
9319 ret = -EIO;
42c17fa6 9320 goto err_free_name;
1da177e4
LT
9321 }
9322 }
4ec93edb 9323
f646968f
PM
9324 if (((dev->hw_features | dev->features) &
9325 NETIF_F_HW_VLAN_CTAG_FILTER) &&
d2ed273d
MM
9326 (!dev->netdev_ops->ndo_vlan_rx_add_vid ||
9327 !dev->netdev_ops->ndo_vlan_rx_kill_vid)) {
9328 netdev_WARN(dev, "Buggy VLAN acceleration in driver!\n");
9329 ret = -EINVAL;
9330 goto err_uninit;
9331 }
9332
9c7dafbf
PE
9333 ret = -EBUSY;
9334 if (!dev->ifindex)
9335 dev->ifindex = dev_new_index(net);
9336 else if (__dev_get_by_index(net, dev->ifindex))
9337 goto err_uninit;
9338
5455c699
MM
9339 /* Transfer changeable features to wanted_features and enable
9340 * software offloads (GSO and GRO).
9341 */
1a3c998f 9342 dev->hw_features |= (NETIF_F_SOFT_FEATURES | NETIF_F_SOFT_FEATURES_OFF);
14d1232f 9343 dev->features |= NETIF_F_SOFT_FEATURES;
d764a122
SD
9344
9345 if (dev->netdev_ops->ndo_udp_tunnel_add) {
9346 dev->features |= NETIF_F_RX_UDP_TUNNEL_PORT;
9347 dev->hw_features |= NETIF_F_RX_UDP_TUNNEL_PORT;
9348 }
9349
14d1232f 9350 dev->wanted_features = dev->features & dev->hw_features;
1da177e4 9351
cbc53e08 9352 if (!(dev->flags & IFF_LOOPBACK))
34324dc2 9353 dev->hw_features |= NETIF_F_NOCACHE_COPY;
cbc53e08 9354
7f348a60
AD
9355 /* If IPv4 TCP segmentation offload is supported we should also
9356 * allow the device to enable segmenting the frame with the option
9357 * of ignoring a static IP ID value. This doesn't enable the
9358 * feature itself but allows the user to enable it later.
9359 */
cbc53e08
AD
9360 if (dev->hw_features & NETIF_F_TSO)
9361 dev->hw_features |= NETIF_F_TSO_MANGLEID;
7f348a60
AD
9362 if (dev->vlan_features & NETIF_F_TSO)
9363 dev->vlan_features |= NETIF_F_TSO_MANGLEID;
9364 if (dev->mpls_features & NETIF_F_TSO)
9365 dev->mpls_features |= NETIF_F_TSO_MANGLEID;
9366 if (dev->hw_enc_features & NETIF_F_TSO)
9367 dev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
c6e1a0d1 9368
1180e7d6 9369 /* Make NETIF_F_HIGHDMA inheritable to VLAN devices.
16c3ea78 9370 */
1180e7d6 9371 dev->vlan_features |= NETIF_F_HIGHDMA;
16c3ea78 9372
ee579677
PS
9373 /* Make NETIF_F_SG inheritable to tunnel devices.
9374 */
802ab55a 9375 dev->hw_enc_features |= NETIF_F_SG | NETIF_F_GSO_PARTIAL;
ee579677 9376
0d89d203
SH
9377 /* Make NETIF_F_SG inheritable to MPLS.
9378 */
9379 dev->mpls_features |= NETIF_F_SG;
9380
7ffbe3fd
JB
9381 ret = call_netdevice_notifiers(NETDEV_POST_INIT, dev);
9382 ret = notifier_to_errno(ret);
9383 if (ret)
9384 goto err_uninit;
9385
8b41d188 9386 ret = netdev_register_kobject(dev);
cb626bf5
JH
9387 if (ret) {
9388 dev->reg_state = NETREG_UNREGISTERED;
7ce1b0ed 9389 goto err_uninit;
cb626bf5 9390 }
b17a7c17
SH
9391 dev->reg_state = NETREG_REGISTERED;
9392
6cb6a27c 9393 __netdev_update_features(dev);
8e9b59b2 9394
1da177e4
LT
9395 /*
9396 * Default initial state at registry is that the
9397 * device is present.
9398 */
9399
9400 set_bit(__LINK_STATE_PRESENT, &dev->state);
9401
8f4cccbb
BH
9402 linkwatch_init_dev(dev);
9403
1da177e4 9404 dev_init_scheduler(dev);
1da177e4 9405 dev_hold(dev);
ce286d32 9406 list_netdevice(dev);
7bf23575 9407 add_device_randomness(dev->dev_addr, dev->addr_len);
1da177e4 9408
948b337e
JP
9409 /* If the device has permanent device address, driver should
9410 * set dev_addr and also addr_assign_type should be set to
9411 * NET_ADDR_PERM (default value).
9412 */
9413 if (dev->addr_assign_type == NET_ADDR_PERM)
9414 memcpy(dev->perm_addr, dev->dev_addr, dev->addr_len);
9415
1da177e4 9416 /* Notify protocols, that a new device appeared. */
056925ab 9417 ret = call_netdevice_notifiers(NETDEV_REGISTER, dev);
fcc5a03a 9418 ret = notifier_to_errno(ret);
93ee31f1
DL
9419 if (ret) {
9420 rollback_registered(dev);
10cc514f
SAK
9421 rcu_barrier();
9422
93ee31f1
DL
9423 dev->reg_state = NETREG_UNREGISTERED;
9424 }
d90a909e
EB
9425 /*
9426 * Prevent userspace races by waiting until the network
9427 * device is fully setup before sending notifications.
9428 */
a2835763
PM
9429 if (!dev->rtnl_link_ops ||
9430 dev->rtnl_link_state == RTNL_LINK_INITIALIZED)
7f294054 9431 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
1da177e4
LT
9432
9433out:
9434 return ret;
7ce1b0ed
HX
9435
9436err_uninit:
d314774c
SH
9437 if (dev->netdev_ops->ndo_uninit)
9438 dev->netdev_ops->ndo_uninit(dev);
cf124db5
DM
9439 if (dev->priv_destructor)
9440 dev->priv_destructor(dev);
42c17fa6
DC
9441err_free_name:
9442 netdev_name_node_free(dev->name_node);
7ce1b0ed 9443 goto out;
1da177e4 9444}
d1b19dff 9445EXPORT_SYMBOL(register_netdevice);
1da177e4 9446
937f1ba5
BH
9447/**
9448 * init_dummy_netdev - init a dummy network device for NAPI
9449 * @dev: device to init
9450 *
9451 * This takes a network device structure and initialize the minimum
9452 * amount of fields so it can be used to schedule NAPI polls without
9453 * registering a full blown interface. This is to be used by drivers
9454 * that need to tie several hardware interfaces to a single NAPI
9455 * poll scheduler due to HW limitations.
9456 */
9457int init_dummy_netdev(struct net_device *dev)
9458{
9459 /* Clear everything. Note we don't initialize spinlocks
9460 * are they aren't supposed to be taken by any of the
9461 * NAPI code and this dummy netdev is supposed to be
9462 * only ever used for NAPI polls
9463 */
9464 memset(dev, 0, sizeof(struct net_device));
9465
9466 /* make sure we BUG if trying to hit standard
9467 * register/unregister code path
9468 */
9469 dev->reg_state = NETREG_DUMMY;
9470
937f1ba5
BH
9471 /* NAPI wants this */
9472 INIT_LIST_HEAD(&dev->napi_list);
9473
9474 /* a dummy interface is started by default */
9475 set_bit(__LINK_STATE_PRESENT, &dev->state);
9476 set_bit(__LINK_STATE_START, &dev->state);
9477
35edfdc7
JE
9478 /* napi_busy_loop stats accounting wants this */
9479 dev_net_set(dev, &init_net);
9480
29b4433d
ED
9481 /* Note : We dont allocate pcpu_refcnt for dummy devices,
9482 * because users of this 'device' dont need to change
9483 * its refcount.
9484 */
9485
937f1ba5
BH
9486 return 0;
9487}
9488EXPORT_SYMBOL_GPL(init_dummy_netdev);
9489
9490
1da177e4
LT
9491/**
9492 * register_netdev - register a network device
9493 * @dev: device to register
9494 *
9495 * Take a completed network device structure and add it to the kernel
9496 * interfaces. A %NETDEV_REGISTER message is sent to the netdev notifier
9497 * chain. 0 is returned on success. A negative errno code is returned
9498 * on a failure to set up the device, or if the name is a duplicate.
9499 *
38b4da38 9500 * This is a wrapper around register_netdevice that takes the rtnl semaphore
1da177e4
LT
9501 * and expands the device name if you passed a format string to
9502 * alloc_netdev.
9503 */
9504int register_netdev(struct net_device *dev)
9505{
9506 int err;
9507
b0f3debc
KT
9508 if (rtnl_lock_killable())
9509 return -EINTR;
1da177e4 9510 err = register_netdevice(dev);
1da177e4
LT
9511 rtnl_unlock();
9512 return err;
9513}
9514EXPORT_SYMBOL(register_netdev);
9515
29b4433d
ED
9516int netdev_refcnt_read(const struct net_device *dev)
9517{
9518 int i, refcnt = 0;
9519
9520 for_each_possible_cpu(i)
9521 refcnt += *per_cpu_ptr(dev->pcpu_refcnt, i);
9522 return refcnt;
9523}
9524EXPORT_SYMBOL(netdev_refcnt_read);
9525
2c53040f 9526/**
1da177e4 9527 * netdev_wait_allrefs - wait until all references are gone.
3de7a37b 9528 * @dev: target net_device
1da177e4
LT
9529 *
9530 * This is called when unregistering network devices.
9531 *
9532 * Any protocol or device that holds a reference should register
9533 * for netdevice notification, and cleanup and put back the
9534 * reference if they receive an UNREGISTER event.
9535 * We can get stuck here if buggy protocols don't correctly
4ec93edb 9536 * call dev_put.
1da177e4
LT
9537 */
9538static void netdev_wait_allrefs(struct net_device *dev)
9539{
9540 unsigned long rebroadcast_time, warning_time;
29b4433d 9541 int refcnt;
1da177e4 9542
e014debe
ED
9543 linkwatch_forget_dev(dev);
9544
1da177e4 9545 rebroadcast_time = warning_time = jiffies;
29b4433d
ED
9546 refcnt = netdev_refcnt_read(dev);
9547
9548 while (refcnt != 0) {
1da177e4 9549 if (time_after(jiffies, rebroadcast_time + 1 * HZ)) {
6756ae4b 9550 rtnl_lock();
1da177e4
LT
9551
9552 /* Rebroadcast unregister notification */
056925ab 9553 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
1da177e4 9554
748e2d93 9555 __rtnl_unlock();
0115e8e3 9556 rcu_barrier();
748e2d93
ED
9557 rtnl_lock();
9558
1da177e4
LT
9559 if (test_bit(__LINK_STATE_LINKWATCH_PENDING,
9560 &dev->state)) {
9561 /* We must not have linkwatch events
9562 * pending on unregister. If this
9563 * happens, we simply run the queue
9564 * unscheduled, resulting in a noop
9565 * for this device.
9566 */
9567 linkwatch_run_queue();
9568 }
9569
6756ae4b 9570 __rtnl_unlock();
1da177e4
LT
9571
9572 rebroadcast_time = jiffies;
9573 }
9574
9575 msleep(250);
9576
29b4433d
ED
9577 refcnt = netdev_refcnt_read(dev);
9578
d7c04b05 9579 if (refcnt && time_after(jiffies, warning_time + 10 * HZ)) {
7b6cd1ce
JP
9580 pr_emerg("unregister_netdevice: waiting for %s to become free. Usage count = %d\n",
9581 dev->name, refcnt);
1da177e4
LT
9582 warning_time = jiffies;
9583 }
9584 }
9585}
9586
9587/* The sequence is:
9588 *
9589 * rtnl_lock();
9590 * ...
9591 * register_netdevice(x1);
9592 * register_netdevice(x2);
9593 * ...
9594 * unregister_netdevice(y1);
9595 * unregister_netdevice(y2);
9596 * ...
9597 * rtnl_unlock();
9598 * free_netdev(y1);
9599 * free_netdev(y2);
9600 *
58ec3b4d 9601 * We are invoked by rtnl_unlock().
1da177e4 9602 * This allows us to deal with problems:
b17a7c17 9603 * 1) We can delete sysfs objects which invoke hotplug
1da177e4
LT
9604 * without deadlocking with linkwatch via keventd.
9605 * 2) Since we run with the RTNL semaphore not held, we can sleep
9606 * safely in order to wait for the netdev refcnt to drop to zero.
58ec3b4d
HX
9607 *
9608 * We must not return until all unregister events added during
9609 * the interval the lock was held have been completed.
1da177e4 9610 */
1da177e4
LT
9611void netdev_run_todo(void)
9612{
626ab0e6 9613 struct list_head list;
1da177e4 9614
1da177e4 9615 /* Snapshot list, allow later requests */
626ab0e6 9616 list_replace_init(&net_todo_list, &list);
58ec3b4d
HX
9617
9618 __rtnl_unlock();
626ab0e6 9619
0115e8e3
ED
9620
9621 /* Wait for rcu callbacks to finish before next phase */
850a545b
EB
9622 if (!list_empty(&list))
9623 rcu_barrier();
9624
1da177e4
LT
9625 while (!list_empty(&list)) {
9626 struct net_device *dev
e5e26d75 9627 = list_first_entry(&list, struct net_device, todo_list);
1da177e4
LT
9628 list_del(&dev->todo_list);
9629
b17a7c17 9630 if (unlikely(dev->reg_state != NETREG_UNREGISTERING)) {
7b6cd1ce 9631 pr_err("network todo '%s' but state %d\n",
b17a7c17
SH
9632 dev->name, dev->reg_state);
9633 dump_stack();
9634 continue;
9635 }
1da177e4 9636
b17a7c17 9637 dev->reg_state = NETREG_UNREGISTERED;
1da177e4 9638
b17a7c17 9639 netdev_wait_allrefs(dev);
1da177e4 9640
b17a7c17 9641 /* paranoia */
29b4433d 9642 BUG_ON(netdev_refcnt_read(dev));
7866a621
SN
9643 BUG_ON(!list_empty(&dev->ptype_all));
9644 BUG_ON(!list_empty(&dev->ptype_specific));
33d480ce
ED
9645 WARN_ON(rcu_access_pointer(dev->ip_ptr));
9646 WARN_ON(rcu_access_pointer(dev->ip6_ptr));
330c7272 9647#if IS_ENABLED(CONFIG_DECNET)
547b792c 9648 WARN_ON(dev->dn_ptr);
330c7272 9649#endif
cf124db5
DM
9650 if (dev->priv_destructor)
9651 dev->priv_destructor(dev);
9652 if (dev->needs_free_netdev)
9653 free_netdev(dev);
9093bbb2 9654
50624c93
EB
9655 /* Report a network device has been unregistered */
9656 rtnl_lock();
9657 dev_net(dev)->dev_unreg_count--;
9658 __rtnl_unlock();
9659 wake_up(&netdev_unregistering_wq);
9660
9093bbb2
SH
9661 /* Free network device */
9662 kobject_put(&dev->dev.kobj);
1da177e4 9663 }
1da177e4
LT
9664}
9665
9256645a
JW
9666/* Convert net_device_stats to rtnl_link_stats64. rtnl_link_stats64 has
9667 * all the same fields in the same order as net_device_stats, with only
9668 * the type differing, but rtnl_link_stats64 may have additional fields
9669 * at the end for newer counters.
3cfde79c 9670 */
77a1abf5
ED
9671void netdev_stats_to_stats64(struct rtnl_link_stats64 *stats64,
9672 const struct net_device_stats *netdev_stats)
3cfde79c
BH
9673{
9674#if BITS_PER_LONG == 64
9256645a 9675 BUILD_BUG_ON(sizeof(*stats64) < sizeof(*netdev_stats));
9af9959e 9676 memcpy(stats64, netdev_stats, sizeof(*netdev_stats));
9256645a
JW
9677 /* zero out counters that only exist in rtnl_link_stats64 */
9678 memset((char *)stats64 + sizeof(*netdev_stats), 0,
9679 sizeof(*stats64) - sizeof(*netdev_stats));
3cfde79c 9680#else
9256645a 9681 size_t i, n = sizeof(*netdev_stats) / sizeof(unsigned long);
3cfde79c
BH
9682 const unsigned long *src = (const unsigned long *)netdev_stats;
9683 u64 *dst = (u64 *)stats64;
9684
9256645a 9685 BUILD_BUG_ON(n > sizeof(*stats64) / sizeof(u64));
3cfde79c
BH
9686 for (i = 0; i < n; i++)
9687 dst[i] = src[i];
9256645a
JW
9688 /* zero out counters that only exist in rtnl_link_stats64 */
9689 memset((char *)stats64 + n * sizeof(u64), 0,
9690 sizeof(*stats64) - n * sizeof(u64));
3cfde79c
BH
9691#endif
9692}
77a1abf5 9693EXPORT_SYMBOL(netdev_stats_to_stats64);
3cfde79c 9694
eeda3fd6
SH
9695/**
9696 * dev_get_stats - get network device statistics
9697 * @dev: device to get statistics from
28172739 9698 * @storage: place to store stats
eeda3fd6 9699 *
d7753516
BH
9700 * Get network statistics from device. Return @storage.
9701 * The device driver may provide its own method by setting
9702 * dev->netdev_ops->get_stats64 or dev->netdev_ops->get_stats;
9703 * otherwise the internal statistics structure is used.
eeda3fd6 9704 */
d7753516
BH
9705struct rtnl_link_stats64 *dev_get_stats(struct net_device *dev,
9706 struct rtnl_link_stats64 *storage)
7004bf25 9707{
eeda3fd6
SH
9708 const struct net_device_ops *ops = dev->netdev_ops;
9709
28172739
ED
9710 if (ops->ndo_get_stats64) {
9711 memset(storage, 0, sizeof(*storage));
caf586e5
ED
9712 ops->ndo_get_stats64(dev, storage);
9713 } else if (ops->ndo_get_stats) {
3cfde79c 9714 netdev_stats_to_stats64(storage, ops->ndo_get_stats(dev));
caf586e5
ED
9715 } else {
9716 netdev_stats_to_stats64(storage, &dev->stats);
28172739 9717 }
6f64ec74
ED
9718 storage->rx_dropped += (unsigned long)atomic_long_read(&dev->rx_dropped);
9719 storage->tx_dropped += (unsigned long)atomic_long_read(&dev->tx_dropped);
9720 storage->rx_nohandler += (unsigned long)atomic_long_read(&dev->rx_nohandler);
28172739 9721 return storage;
c45d286e 9722}
eeda3fd6 9723EXPORT_SYMBOL(dev_get_stats);
c45d286e 9724
24824a09 9725struct netdev_queue *dev_ingress_queue_create(struct net_device *dev)
dc2b4847 9726{
24824a09 9727 struct netdev_queue *queue = dev_ingress_queue(dev);
dc2b4847 9728
24824a09
ED
9729#ifdef CONFIG_NET_CLS_ACT
9730 if (queue)
9731 return queue;
9732 queue = kzalloc(sizeof(*queue), GFP_KERNEL);
9733 if (!queue)
9734 return NULL;
9735 netdev_init_one_queue(dev, queue, NULL);
2ce1ee17 9736 RCU_INIT_POINTER(queue->qdisc, &noop_qdisc);
24824a09
ED
9737 queue->qdisc_sleeping = &noop_qdisc;
9738 rcu_assign_pointer(dev->ingress_queue, queue);
9739#endif
9740 return queue;
bb949fbd
DM
9741}
9742
2c60db03
ED
9743static const struct ethtool_ops default_ethtool_ops;
9744
d07d7507
SG
9745void netdev_set_default_ethtool_ops(struct net_device *dev,
9746 const struct ethtool_ops *ops)
9747{
9748 if (dev->ethtool_ops == &default_ethtool_ops)
9749 dev->ethtool_ops = ops;
9750}
9751EXPORT_SYMBOL_GPL(netdev_set_default_ethtool_ops);
9752
74d332c1
ED
9753void netdev_freemem(struct net_device *dev)
9754{
9755 char *addr = (char *)dev - dev->padded;
9756
4cb28970 9757 kvfree(addr);
74d332c1
ED
9758}
9759
1da177e4 9760/**
722c9a0c 9761 * alloc_netdev_mqs - allocate network device
9762 * @sizeof_priv: size of private data to allocate space for
9763 * @name: device name format string
9764 * @name_assign_type: origin of device name
9765 * @setup: callback to initialize device
9766 * @txqs: the number of TX subqueues to allocate
9767 * @rxqs: the number of RX subqueues to allocate
9768 *
9769 * Allocates a struct net_device with private data area for driver use
9770 * and performs basic initialization. Also allocates subqueue structs
9771 * for each queue on the device.
1da177e4 9772 */
36909ea4 9773struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
c835a677 9774 unsigned char name_assign_type,
36909ea4
TH
9775 void (*setup)(struct net_device *),
9776 unsigned int txqs, unsigned int rxqs)
1da177e4 9777{
1da177e4 9778 struct net_device *dev;
52a59bd5 9779 unsigned int alloc_size;
1ce8e7b5 9780 struct net_device *p;
1da177e4 9781
b6fe17d6
SH
9782 BUG_ON(strlen(name) >= sizeof(dev->name));
9783
36909ea4 9784 if (txqs < 1) {
7b6cd1ce 9785 pr_err("alloc_netdev: Unable to allocate device with zero queues\n");
55513fb4
TH
9786 return NULL;
9787 }
9788
36909ea4 9789 if (rxqs < 1) {
7b6cd1ce 9790 pr_err("alloc_netdev: Unable to allocate device with zero RX queues\n");
36909ea4
TH
9791 return NULL;
9792 }
36909ea4 9793
fd2ea0a7 9794 alloc_size = sizeof(struct net_device);
d1643d24
AD
9795 if (sizeof_priv) {
9796 /* ensure 32-byte alignment of private area */
1ce8e7b5 9797 alloc_size = ALIGN(alloc_size, NETDEV_ALIGN);
d1643d24
AD
9798 alloc_size += sizeof_priv;
9799 }
9800 /* ensure 32-byte alignment of whole construct */
1ce8e7b5 9801 alloc_size += NETDEV_ALIGN - 1;
1da177e4 9802
dcda9b04 9803 p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
62b5942a 9804 if (!p)
1da177e4 9805 return NULL;
1da177e4 9806
1ce8e7b5 9807 dev = PTR_ALIGN(p, NETDEV_ALIGN);
1da177e4 9808 dev->padded = (char *)dev - (char *)p;
ab9c73cc 9809
29b4433d
ED
9810 dev->pcpu_refcnt = alloc_percpu(int);
9811 if (!dev->pcpu_refcnt)
74d332c1 9812 goto free_dev;
ab9c73cc 9813
ab9c73cc 9814 if (dev_addr_init(dev))
29b4433d 9815 goto free_pcpu;
ab9c73cc 9816
22bedad3 9817 dev_mc_init(dev);
a748ee24 9818 dev_uc_init(dev);
ccffad25 9819
c346dca1 9820 dev_net_set(dev, &init_net);
1da177e4 9821
ab92d68f
TY
9822 netdev_register_lockdep_key(dev);
9823
8d3bdbd5 9824 dev->gso_max_size = GSO_MAX_SIZE;
30b678d8 9825 dev->gso_max_segs = GSO_MAX_SEGS;
5343da4c
TY
9826 dev->upper_level = 1;
9827 dev->lower_level = 1;
8d3bdbd5 9828
8d3bdbd5
DM
9829 INIT_LIST_HEAD(&dev->napi_list);
9830 INIT_LIST_HEAD(&dev->unreg_list);
5cde2829 9831 INIT_LIST_HEAD(&dev->close_list);
8d3bdbd5 9832 INIT_LIST_HEAD(&dev->link_watch_list);
2f268f12
VF
9833 INIT_LIST_HEAD(&dev->adj_list.upper);
9834 INIT_LIST_HEAD(&dev->adj_list.lower);
7866a621
SN
9835 INIT_LIST_HEAD(&dev->ptype_all);
9836 INIT_LIST_HEAD(&dev->ptype_specific);
93642e14 9837 INIT_LIST_HEAD(&dev->net_notifier_list);
59cc1f61
JK
9838#ifdef CONFIG_NET_SCHED
9839 hash_init(dev->qdisc_hash);
9840#endif
02875878 9841 dev->priv_flags = IFF_XMIT_DST_RELEASE | IFF_XMIT_DST_RELEASE_PERM;
8d3bdbd5
DM
9842 setup(dev);
9843
a813104d 9844 if (!dev->tx_queue_len) {
f84bb1ea 9845 dev->priv_flags |= IFF_NO_QUEUE;
11597084 9846 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
a813104d 9847 }
906470c1 9848
36909ea4
TH
9849 dev->num_tx_queues = txqs;
9850 dev->real_num_tx_queues = txqs;
ed9af2e8 9851 if (netif_alloc_netdev_queues(dev))
8d3bdbd5 9852 goto free_all;
e8a0464c 9853
36909ea4
TH
9854 dev->num_rx_queues = rxqs;
9855 dev->real_num_rx_queues = rxqs;
fe822240 9856 if (netif_alloc_rx_queues(dev))
8d3bdbd5 9857 goto free_all;
0a9627f2 9858
1da177e4 9859 strcpy(dev->name, name);
c835a677 9860 dev->name_assign_type = name_assign_type;
cbda10fa 9861 dev->group = INIT_NETDEV_GROUP;
2c60db03
ED
9862 if (!dev->ethtool_ops)
9863 dev->ethtool_ops = &default_ethtool_ops;
e687ad60
PN
9864
9865 nf_hook_ingress_init(dev);
9866
1da177e4 9867 return dev;
ab9c73cc 9868
8d3bdbd5
DM
9869free_all:
9870 free_netdev(dev);
9871 return NULL;
9872
29b4433d
ED
9873free_pcpu:
9874 free_percpu(dev->pcpu_refcnt);
74d332c1
ED
9875free_dev:
9876 netdev_freemem(dev);
ab9c73cc 9877 return NULL;
1da177e4 9878}
36909ea4 9879EXPORT_SYMBOL(alloc_netdev_mqs);
1da177e4
LT
9880
9881/**
722c9a0c 9882 * free_netdev - free network device
9883 * @dev: device
1da177e4 9884 *
722c9a0c 9885 * This function does the last stage of destroying an allocated device
9886 * interface. The reference to the device object is released. If this
9887 * is the last reference then it will be freed.Must be called in process
9888 * context.
1da177e4
LT
9889 */
9890void free_netdev(struct net_device *dev)
9891{
d565b0a1
HX
9892 struct napi_struct *p, *n;
9893
93d05d4a 9894 might_sleep();
60877a32 9895 netif_free_tx_queues(dev);
e817f856 9896 netif_free_rx_queues(dev);
e8a0464c 9897
33d480ce 9898 kfree(rcu_dereference_protected(dev->ingress_queue, 1));
24824a09 9899
f001fde5
JP
9900 /* Flush device addresses */
9901 dev_addr_flush(dev);
9902
d565b0a1
HX
9903 list_for_each_entry_safe(p, n, &dev->napi_list, dev_list)
9904 netif_napi_del(p);
9905
29b4433d
ED
9906 free_percpu(dev->pcpu_refcnt);
9907 dev->pcpu_refcnt = NULL;
75ccae62
THJ
9908 free_percpu(dev->xdp_bulkq);
9909 dev->xdp_bulkq = NULL;
29b4433d 9910
ab92d68f
TY
9911 netdev_unregister_lockdep_key(dev);
9912
3041a069 9913 /* Compatibility with error handling in drivers */
1da177e4 9914 if (dev->reg_state == NETREG_UNINITIALIZED) {
74d332c1 9915 netdev_freemem(dev);
1da177e4
LT
9916 return;
9917 }
9918
9919 BUG_ON(dev->reg_state != NETREG_UNREGISTERED);
9920 dev->reg_state = NETREG_RELEASED;
9921
43cb76d9
GKH
9922 /* will free via device release */
9923 put_device(&dev->dev);
1da177e4 9924}
d1b19dff 9925EXPORT_SYMBOL(free_netdev);
4ec93edb 9926
f0db275a
SH
9927/**
9928 * synchronize_net - Synchronize with packet receive processing
9929 *
9930 * Wait for packets currently being received to be done.
9931 * Does not block later packets from starting.
9932 */
4ec93edb 9933void synchronize_net(void)
1da177e4
LT
9934{
9935 might_sleep();
be3fc413
ED
9936 if (rtnl_is_locked())
9937 synchronize_rcu_expedited();
9938 else
9939 synchronize_rcu();
1da177e4 9940}
d1b19dff 9941EXPORT_SYMBOL(synchronize_net);
1da177e4
LT
9942
9943/**
44a0873d 9944 * unregister_netdevice_queue - remove device from the kernel
1da177e4 9945 * @dev: device
44a0873d 9946 * @head: list
6ebfbc06 9947 *
1da177e4 9948 * This function shuts down a device interface and removes it
d59b54b1 9949 * from the kernel tables.
44a0873d 9950 * If head not NULL, device is queued to be unregistered later.
1da177e4
LT
9951 *
9952 * Callers must hold the rtnl semaphore. You may want
9953 * unregister_netdev() instead of this.
9954 */
9955
44a0873d 9956void unregister_netdevice_queue(struct net_device *dev, struct list_head *head)
1da177e4 9957{
a6620712
HX
9958 ASSERT_RTNL();
9959
44a0873d 9960 if (head) {
9fdce099 9961 list_move_tail(&dev->unreg_list, head);
44a0873d
ED
9962 } else {
9963 rollback_registered(dev);
9964 /* Finish processing unregister after unlock */
9965 net_set_todo(dev);
9966 }
1da177e4 9967}
44a0873d 9968EXPORT_SYMBOL(unregister_netdevice_queue);
1da177e4 9969
9b5e383c
ED
9970/**
9971 * unregister_netdevice_many - unregister many devices
9972 * @head: list of devices
87757a91
ED
9973 *
9974 * Note: As most callers use a stack allocated list_head,
9975 * we force a list_del() to make sure stack wont be corrupted later.
9b5e383c
ED
9976 */
9977void unregister_netdevice_many(struct list_head *head)
9978{
9979 struct net_device *dev;
9980
9981 if (!list_empty(head)) {
9982 rollback_registered_many(head);
9983 list_for_each_entry(dev, head, unreg_list)
9984 net_set_todo(dev);
87757a91 9985 list_del(head);
9b5e383c
ED
9986 }
9987}
63c8099d 9988EXPORT_SYMBOL(unregister_netdevice_many);
9b5e383c 9989
1da177e4
LT
9990/**
9991 * unregister_netdev - remove device from the kernel
9992 * @dev: device
9993 *
9994 * This function shuts down a device interface and removes it
d59b54b1 9995 * from the kernel tables.
1da177e4
LT
9996 *
9997 * This is just a wrapper for unregister_netdevice that takes
9998 * the rtnl semaphore. In general you want to use this and not
9999 * unregister_netdevice.
10000 */
10001void unregister_netdev(struct net_device *dev)
10002{
10003 rtnl_lock();
10004 unregister_netdevice(dev);
10005 rtnl_unlock();
10006}
1da177e4
LT
10007EXPORT_SYMBOL(unregister_netdev);
10008
ce286d32
EB
10009/**
10010 * dev_change_net_namespace - move device to different nethost namespace
10011 * @dev: device
10012 * @net: network namespace
10013 * @pat: If not NULL name pattern to try if the current device name
10014 * is already taken in the destination network namespace.
10015 *
10016 * This function shuts down a device interface and moves it
10017 * to a new network namespace. On success 0 is returned, on
10018 * a failure a netagive errno code is returned.
10019 *
10020 * Callers must hold the rtnl semaphore.
10021 */
10022
10023int dev_change_net_namespace(struct net_device *dev, struct net *net, const char *pat)
10024{
38e01b30 10025 int err, new_nsid, new_ifindex;
ce286d32
EB
10026
10027 ASSERT_RTNL();
10028
10029 /* Don't allow namespace local devices to be moved. */
10030 err = -EINVAL;
10031 if (dev->features & NETIF_F_NETNS_LOCAL)
10032 goto out;
10033
10034 /* Ensure the device has been registrered */
ce286d32
EB
10035 if (dev->reg_state != NETREG_REGISTERED)
10036 goto out;
10037
10038 /* Get out if there is nothing todo */
10039 err = 0;
878628fb 10040 if (net_eq(dev_net(dev), net))
ce286d32
EB
10041 goto out;
10042
10043 /* Pick the destination device name, and ensure
10044 * we can use it in the destination network namespace.
10045 */
10046 err = -EEXIST;
d9031024 10047 if (__dev_get_by_name(net, dev->name)) {
ce286d32
EB
10048 /* We get here if we can't use the current device name */
10049 if (!pat)
10050 goto out;
7892bd08
LR
10051 err = dev_get_valid_name(net, dev, pat);
10052 if (err < 0)
ce286d32
EB
10053 goto out;
10054 }
10055
10056 /*
10057 * And now a mini version of register_netdevice unregister_netdevice.
10058 */
10059
10060 /* If device is running close it first. */
9b772652 10061 dev_close(dev);
ce286d32
EB
10062
10063 /* And unlink it from device chain */
ce286d32
EB
10064 unlist_netdevice(dev);
10065
10066 synchronize_net();
10067
10068 /* Shutdown queueing discipline. */
10069 dev_shutdown(dev);
10070
10071 /* Notify protocols, that we are about to destroy
eb13da1a 10072 * this device. They should clean all the things.
10073 *
10074 * Note that dev->reg_state stays at NETREG_REGISTERED.
10075 * This is wanted because this way 8021q and macvlan know
10076 * the device is just moving and can keep their slaves up.
10077 */
ce286d32 10078 call_netdevice_notifiers(NETDEV_UNREGISTER, dev);
6549dd43 10079 rcu_barrier();
38e01b30 10080
d4e4fdf9 10081 new_nsid = peernet2id_alloc(dev_net(dev), net, GFP_KERNEL);
38e01b30
ND
10082 /* If there is an ifindex conflict assign a new one */
10083 if (__dev_get_by_index(net, dev->ifindex))
10084 new_ifindex = dev_new_index(net);
10085 else
10086 new_ifindex = dev->ifindex;
10087
10088 rtmsg_ifinfo_newnet(RTM_DELLINK, dev, ~0U, GFP_KERNEL, &new_nsid,
10089 new_ifindex);
ce286d32
EB
10090
10091 /*
10092 * Flush the unicast and multicast chains
10093 */
a748ee24 10094 dev_uc_flush(dev);
22bedad3 10095 dev_mc_flush(dev);
ce286d32 10096
4e66ae2e
SH
10097 /* Send a netdev-removed uevent to the old namespace */
10098 kobject_uevent(&dev->dev.kobj, KOBJ_REMOVE);
4c75431a 10099 netdev_adjacent_del_links(dev);
4e66ae2e 10100
93642e14
JP
10101 /* Move per-net netdevice notifiers that are following the netdevice */
10102 move_netdevice_notifiers_dev_net(dev, net);
10103
ce286d32 10104 /* Actually switch the network namespace */
c346dca1 10105 dev_net_set(dev, net);
38e01b30 10106 dev->ifindex = new_ifindex;
ce286d32 10107
4e66ae2e
SH
10108 /* Send a netdev-add uevent to the new namespace */
10109 kobject_uevent(&dev->dev.kobj, KOBJ_ADD);
4c75431a 10110 netdev_adjacent_add_links(dev);
4e66ae2e 10111
8b41d188 10112 /* Fixup kobjects */
a1b3f594 10113 err = device_rename(&dev->dev, dev->name);
8b41d188 10114 WARN_ON(err);
ce286d32
EB
10115
10116 /* Add the device back in the hashes */
10117 list_netdevice(dev);
10118
10119 /* Notify protocols, that a new device appeared. */
10120 call_netdevice_notifiers(NETDEV_REGISTER, dev);
10121
d90a909e
EB
10122 /*
10123 * Prevent userspace races by waiting until the network
10124 * device is fully setup before sending notifications.
10125 */
7f294054 10126 rtmsg_ifinfo(RTM_NEWLINK, dev, ~0U, GFP_KERNEL);
d90a909e 10127
ce286d32
EB
10128 synchronize_net();
10129 err = 0;
10130out:
10131 return err;
10132}
463d0183 10133EXPORT_SYMBOL_GPL(dev_change_net_namespace);
ce286d32 10134
f0bf90de 10135static int dev_cpu_dead(unsigned int oldcpu)
1da177e4
LT
10136{
10137 struct sk_buff **list_skb;
1da177e4 10138 struct sk_buff *skb;
f0bf90de 10139 unsigned int cpu;
97d8b6e3 10140 struct softnet_data *sd, *oldsd, *remsd = NULL;
1da177e4 10141
1da177e4
LT
10142 local_irq_disable();
10143 cpu = smp_processor_id();
10144 sd = &per_cpu(softnet_data, cpu);
10145 oldsd = &per_cpu(softnet_data, oldcpu);
10146
10147 /* Find end of our completion_queue. */
10148 list_skb = &sd->completion_queue;
10149 while (*list_skb)
10150 list_skb = &(*list_skb)->next;
10151 /* Append completion queue from offline CPU. */
10152 *list_skb = oldsd->completion_queue;
10153 oldsd->completion_queue = NULL;
10154
1da177e4 10155 /* Append output queue from offline CPU. */
a9cbd588
CG
10156 if (oldsd->output_queue) {
10157 *sd->output_queue_tailp = oldsd->output_queue;
10158 sd->output_queue_tailp = oldsd->output_queue_tailp;
10159 oldsd->output_queue = NULL;
10160 oldsd->output_queue_tailp = &oldsd->output_queue;
10161 }
ac64da0b
ED
10162 /* Append NAPI poll list from offline CPU, with one exception :
10163 * process_backlog() must be called by cpu owning percpu backlog.
10164 * We properly handle process_queue & input_pkt_queue later.
10165 */
10166 while (!list_empty(&oldsd->poll_list)) {
10167 struct napi_struct *napi = list_first_entry(&oldsd->poll_list,
10168 struct napi_struct,
10169 poll_list);
10170
10171 list_del_init(&napi->poll_list);
10172 if (napi->poll == process_backlog)
10173 napi->state = 0;
10174 else
10175 ____napi_schedule(sd, napi);
264524d5 10176 }
1da177e4
LT
10177
10178 raise_softirq_irqoff(NET_TX_SOFTIRQ);
10179 local_irq_enable();
10180
773fc8f6 10181#ifdef CONFIG_RPS
10182 remsd = oldsd->rps_ipi_list;
10183 oldsd->rps_ipi_list = NULL;
10184#endif
10185 /* send out pending IPI's on offline CPU */
10186 net_rps_send_ipi(remsd);
10187
1da177e4 10188 /* Process offline CPU's input_pkt_queue */
76cc8b13 10189 while ((skb = __skb_dequeue(&oldsd->process_queue))) {
91e83133 10190 netif_rx_ni(skb);
76cc8b13 10191 input_queue_head_incr(oldsd);
fec5e652 10192 }
ac64da0b 10193 while ((skb = skb_dequeue(&oldsd->input_pkt_queue))) {
91e83133 10194 netif_rx_ni(skb);
76cc8b13
TH
10195 input_queue_head_incr(oldsd);
10196 }
1da177e4 10197
f0bf90de 10198 return 0;
1da177e4 10199}
1da177e4 10200
7f353bf2 10201/**
b63365a2
HX
10202 * netdev_increment_features - increment feature set by one
10203 * @all: current feature set
10204 * @one: new feature set
10205 * @mask: mask feature set
7f353bf2
HX
10206 *
10207 * Computes a new feature set after adding a device with feature set
b63365a2
HX
10208 * @one to the master device with current feature set @all. Will not
10209 * enable anything that is off in @mask. Returns the new feature set.
7f353bf2 10210 */
c8f44aff
MM
10211netdev_features_t netdev_increment_features(netdev_features_t all,
10212 netdev_features_t one, netdev_features_t mask)
b63365a2 10213{
c8cd0989 10214 if (mask & NETIF_F_HW_CSUM)
a188222b 10215 mask |= NETIF_F_CSUM_MASK;
1742f183 10216 mask |= NETIF_F_VLAN_CHALLENGED;
7f353bf2 10217
a188222b 10218 all |= one & (NETIF_F_ONE_FOR_ALL | NETIF_F_CSUM_MASK) & mask;
1742f183 10219 all &= one | ~NETIF_F_ALL_FOR_ALL;
c6e1a0d1 10220
1742f183 10221 /* If one device supports hw checksumming, set for all. */
c8cd0989
TH
10222 if (all & NETIF_F_HW_CSUM)
10223 all &= ~(NETIF_F_CSUM_MASK & ~NETIF_F_HW_CSUM);
7f353bf2
HX
10224
10225 return all;
10226}
b63365a2 10227EXPORT_SYMBOL(netdev_increment_features);
7f353bf2 10228
430f03cd 10229static struct hlist_head * __net_init netdev_create_hash(void)
30d97d35
PE
10230{
10231 int i;
10232 struct hlist_head *hash;
10233
6da2ec56 10234 hash = kmalloc_array(NETDEV_HASHENTRIES, sizeof(*hash), GFP_KERNEL);
30d97d35
PE
10235 if (hash != NULL)
10236 for (i = 0; i < NETDEV_HASHENTRIES; i++)
10237 INIT_HLIST_HEAD(&hash[i]);
10238
10239 return hash;
10240}
10241
881d966b 10242/* Initialize per network namespace state */
4665079c 10243static int __net_init netdev_init(struct net *net)
881d966b 10244{
d9f37d01 10245 BUILD_BUG_ON(GRO_HASH_BUCKETS >
c593642c 10246 8 * sizeof_field(struct napi_struct, gro_bitmask));
d9f37d01 10247
734b6541
RM
10248 if (net != &init_net)
10249 INIT_LIST_HEAD(&net->dev_base_head);
881d966b 10250
30d97d35
PE
10251 net->dev_name_head = netdev_create_hash();
10252 if (net->dev_name_head == NULL)
10253 goto err_name;
881d966b 10254
30d97d35
PE
10255 net->dev_index_head = netdev_create_hash();
10256 if (net->dev_index_head == NULL)
10257 goto err_idx;
881d966b 10258
a30c7b42
JP
10259 RAW_INIT_NOTIFIER_HEAD(&net->netdev_chain);
10260
881d966b 10261 return 0;
30d97d35
PE
10262
10263err_idx:
10264 kfree(net->dev_name_head);
10265err_name:
10266 return -ENOMEM;
881d966b
EB
10267}
10268
f0db275a
SH
10269/**
10270 * netdev_drivername - network driver for the device
10271 * @dev: network device
f0db275a
SH
10272 *
10273 * Determine network driver for device.
10274 */
3019de12 10275const char *netdev_drivername(const struct net_device *dev)
6579e57b 10276{
cf04a4c7
SH
10277 const struct device_driver *driver;
10278 const struct device *parent;
3019de12 10279 const char *empty = "";
6579e57b
AV
10280
10281 parent = dev->dev.parent;
6579e57b 10282 if (!parent)
3019de12 10283 return empty;
6579e57b
AV
10284
10285 driver = parent->driver;
10286 if (driver && driver->name)
3019de12
DM
10287 return driver->name;
10288 return empty;
6579e57b
AV
10289}
10290
6ea754eb
JP
10291static void __netdev_printk(const char *level, const struct net_device *dev,
10292 struct va_format *vaf)
256df2f3 10293{
b004ff49 10294 if (dev && dev->dev.parent) {
6ea754eb
JP
10295 dev_printk_emit(level[1] - '0',
10296 dev->dev.parent,
10297 "%s %s %s%s: %pV",
10298 dev_driver_string(dev->dev.parent),
10299 dev_name(dev->dev.parent),
10300 netdev_name(dev), netdev_reg_state(dev),
10301 vaf);
b004ff49 10302 } else if (dev) {
6ea754eb
JP
10303 printk("%s%s%s: %pV",
10304 level, netdev_name(dev), netdev_reg_state(dev), vaf);
b004ff49 10305 } else {
6ea754eb 10306 printk("%s(NULL net_device): %pV", level, vaf);
b004ff49 10307 }
256df2f3
JP
10308}
10309
6ea754eb
JP
10310void netdev_printk(const char *level, const struct net_device *dev,
10311 const char *format, ...)
256df2f3
JP
10312{
10313 struct va_format vaf;
10314 va_list args;
256df2f3
JP
10315
10316 va_start(args, format);
10317
10318 vaf.fmt = format;
10319 vaf.va = &args;
10320
6ea754eb 10321 __netdev_printk(level, dev, &vaf);
b004ff49 10322
256df2f3 10323 va_end(args);
256df2f3
JP
10324}
10325EXPORT_SYMBOL(netdev_printk);
10326
10327#define define_netdev_printk_level(func, level) \
6ea754eb 10328void func(const struct net_device *dev, const char *fmt, ...) \
256df2f3 10329{ \
256df2f3
JP
10330 struct va_format vaf; \
10331 va_list args; \
10332 \
10333 va_start(args, fmt); \
10334 \
10335 vaf.fmt = fmt; \
10336 vaf.va = &args; \
10337 \
6ea754eb 10338 __netdev_printk(level, dev, &vaf); \
b004ff49 10339 \
256df2f3 10340 va_end(args); \
256df2f3
JP
10341} \
10342EXPORT_SYMBOL(func);
10343
10344define_netdev_printk_level(netdev_emerg, KERN_EMERG);
10345define_netdev_printk_level(netdev_alert, KERN_ALERT);
10346define_netdev_printk_level(netdev_crit, KERN_CRIT);
10347define_netdev_printk_level(netdev_err, KERN_ERR);
10348define_netdev_printk_level(netdev_warn, KERN_WARNING);
10349define_netdev_printk_level(netdev_notice, KERN_NOTICE);
10350define_netdev_printk_level(netdev_info, KERN_INFO);
10351
4665079c 10352static void __net_exit netdev_exit(struct net *net)
881d966b
EB
10353{
10354 kfree(net->dev_name_head);
10355 kfree(net->dev_index_head);
ee21b18b
VA
10356 if (net != &init_net)
10357 WARN_ON_ONCE(!list_empty(&net->dev_base_head));
881d966b
EB
10358}
10359
022cbae6 10360static struct pernet_operations __net_initdata netdev_net_ops = {
881d966b
EB
10361 .init = netdev_init,
10362 .exit = netdev_exit,
10363};
10364
4665079c 10365static void __net_exit default_device_exit(struct net *net)
ce286d32 10366{
e008b5fc 10367 struct net_device *dev, *aux;
ce286d32 10368 /*
e008b5fc 10369 * Push all migratable network devices back to the
ce286d32
EB
10370 * initial network namespace
10371 */
10372 rtnl_lock();
e008b5fc 10373 for_each_netdev_safe(net, dev, aux) {
ce286d32 10374 int err;
aca51397 10375 char fb_name[IFNAMSIZ];
ce286d32
EB
10376
10377 /* Ignore unmoveable devices (i.e. loopback) */
10378 if (dev->features & NETIF_F_NETNS_LOCAL)
10379 continue;
10380
e008b5fc
EB
10381 /* Leave virtual devices for the generic cleanup */
10382 if (dev->rtnl_link_ops)
10383 continue;
d0c082ce 10384
25985edc 10385 /* Push remaining network devices to init_net */
aca51397 10386 snprintf(fb_name, IFNAMSIZ, "dev%d", dev->ifindex);
55b40dbf
JP
10387 if (__dev_get_by_name(&init_net, fb_name))
10388 snprintf(fb_name, IFNAMSIZ, "dev%%d");
aca51397 10389 err = dev_change_net_namespace(dev, &init_net, fb_name);
ce286d32 10390 if (err) {
7b6cd1ce
JP
10391 pr_emerg("%s: failed to move %s to init_net: %d\n",
10392 __func__, dev->name, err);
aca51397 10393 BUG();
ce286d32
EB
10394 }
10395 }
10396 rtnl_unlock();
10397}
10398
50624c93
EB
10399static void __net_exit rtnl_lock_unregistering(struct list_head *net_list)
10400{
10401 /* Return with the rtnl_lock held when there are no network
10402 * devices unregistering in any network namespace in net_list.
10403 */
10404 struct net *net;
10405 bool unregistering;
ff960a73 10406 DEFINE_WAIT_FUNC(wait, woken_wake_function);
50624c93 10407
ff960a73 10408 add_wait_queue(&netdev_unregistering_wq, &wait);
50624c93 10409 for (;;) {
50624c93
EB
10410 unregistering = false;
10411 rtnl_lock();
10412 list_for_each_entry(net, net_list, exit_list) {
10413 if (net->dev_unreg_count > 0) {
10414 unregistering = true;
10415 break;
10416 }
10417 }
10418 if (!unregistering)
10419 break;
10420 __rtnl_unlock();
ff960a73
PZ
10421
10422 wait_woken(&wait, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
50624c93 10423 }
ff960a73 10424 remove_wait_queue(&netdev_unregistering_wq, &wait);
50624c93
EB
10425}
10426
04dc7f6b
EB
10427static void __net_exit default_device_exit_batch(struct list_head *net_list)
10428{
10429 /* At exit all network devices most be removed from a network
b595076a 10430 * namespace. Do this in the reverse order of registration.
04dc7f6b
EB
10431 * Do this across as many network namespaces as possible to
10432 * improve batching efficiency.
10433 */
10434 struct net_device *dev;
10435 struct net *net;
10436 LIST_HEAD(dev_kill_list);
10437
50624c93
EB
10438 /* To prevent network device cleanup code from dereferencing
10439 * loopback devices or network devices that have been freed
10440 * wait here for all pending unregistrations to complete,
10441 * before unregistring the loopback device and allowing the
10442 * network namespace be freed.
10443 *
10444 * The netdev todo list containing all network devices
10445 * unregistrations that happen in default_device_exit_batch
10446 * will run in the rtnl_unlock() at the end of
10447 * default_device_exit_batch.
10448 */
10449 rtnl_lock_unregistering(net_list);
04dc7f6b
EB
10450 list_for_each_entry(net, net_list, exit_list) {
10451 for_each_netdev_reverse(net, dev) {
b0ab2fab 10452 if (dev->rtnl_link_ops && dev->rtnl_link_ops->dellink)
04dc7f6b
EB
10453 dev->rtnl_link_ops->dellink(dev, &dev_kill_list);
10454 else
10455 unregister_netdevice_queue(dev, &dev_kill_list);
10456 }
10457 }
10458 unregister_netdevice_many(&dev_kill_list);
10459 rtnl_unlock();
10460}
10461
022cbae6 10462static struct pernet_operations __net_initdata default_device_ops = {
ce286d32 10463 .exit = default_device_exit,
04dc7f6b 10464 .exit_batch = default_device_exit_batch,
ce286d32
EB
10465};
10466
1da177e4
LT
10467/*
10468 * Initialize the DEV module. At boot time this walks the device list and
10469 * unhooks any devices that fail to initialise (normally hardware not
10470 * present) and leaves us with a valid list of present and active devices.
10471 *
10472 */
10473
10474/*
10475 * This is called single threaded during boot, so no need
10476 * to take the rtnl semaphore.
10477 */
10478static int __init net_dev_init(void)
10479{
10480 int i, rc = -ENOMEM;
10481
10482 BUG_ON(!dev_boot_phase);
10483
1da177e4
LT
10484 if (dev_proc_init())
10485 goto out;
10486
8b41d188 10487 if (netdev_kobject_init())
1da177e4
LT
10488 goto out;
10489
10490 INIT_LIST_HEAD(&ptype_all);
82d8a867 10491 for (i = 0; i < PTYPE_HASH_SIZE; i++)
1da177e4
LT
10492 INIT_LIST_HEAD(&ptype_base[i]);
10493
62532da9
VY
10494 INIT_LIST_HEAD(&offload_base);
10495
881d966b
EB
10496 if (register_pernet_subsys(&netdev_net_ops))
10497 goto out;
1da177e4
LT
10498
10499 /*
10500 * Initialise the packet receive queues.
10501 */
10502
6f912042 10503 for_each_possible_cpu(i) {
41852497 10504 struct work_struct *flush = per_cpu_ptr(&flush_works, i);
e36fa2f7 10505 struct softnet_data *sd = &per_cpu(softnet_data, i);
1da177e4 10506
41852497
ED
10507 INIT_WORK(flush, flush_backlog);
10508
e36fa2f7 10509 skb_queue_head_init(&sd->input_pkt_queue);
6e7676c1 10510 skb_queue_head_init(&sd->process_queue);
f53c7239
SK
10511#ifdef CONFIG_XFRM_OFFLOAD
10512 skb_queue_head_init(&sd->xfrm_backlog);
10513#endif
e36fa2f7 10514 INIT_LIST_HEAD(&sd->poll_list);
a9cbd588 10515 sd->output_queue_tailp = &sd->output_queue;
df334545 10516#ifdef CONFIG_RPS
e36fa2f7
ED
10517 sd->csd.func = rps_trigger_softirq;
10518 sd->csd.info = sd;
e36fa2f7 10519 sd->cpu = i;
1e94d72f 10520#endif
0a9627f2 10521
7c4ec749 10522 init_gro_hash(&sd->backlog);
e36fa2f7
ED
10523 sd->backlog.poll = process_backlog;
10524 sd->backlog.weight = weight_p;
1da177e4
LT
10525 }
10526
1da177e4
LT
10527 dev_boot_phase = 0;
10528
505d4f73
EB
10529 /* The loopback device is special if any other network devices
10530 * is present in a network namespace the loopback device must
10531 * be present. Since we now dynamically allocate and free the
10532 * loopback device ensure this invariant is maintained by
10533 * keeping the loopback device as the first device on the
10534 * list of network devices. Ensuring the loopback devices
10535 * is the first device that appears and the last network device
10536 * that disappears.
10537 */
10538 if (register_pernet_device(&loopback_net_ops))
10539 goto out;
10540
10541 if (register_pernet_device(&default_device_ops))
10542 goto out;
10543
962cf36c
CM
10544 open_softirq(NET_TX_SOFTIRQ, net_tx_action);
10545 open_softirq(NET_RX_SOFTIRQ, net_rx_action);
1da177e4 10546
f0bf90de
SAS
10547 rc = cpuhp_setup_state_nocalls(CPUHP_NET_DEV_DEAD, "net/dev:dead",
10548 NULL, dev_cpu_dead);
10549 WARN_ON(rc < 0);
1da177e4
LT
10550 rc = 0;
10551out:
10552 return rc;
10553}
10554
10555subsys_initcall(net_dev_init);