]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - net/core/dev.c
net: phy: broadcom: Fix a typo ("firsly")
[mirror_ubuntu-hirsute-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
HX
148#define MAX_GRO_SKBS 8
149
5d38a079
HX
150/* This should be increased if a protocol with a bigger head is added. */
151#define GRO_MAX_HEAD (MAX_HEADER + 128)
152
1da177e4 153static DEFINE_SPINLOCK(ptype_lock);
62532da9 154static DEFINE_SPINLOCK(offload_lock);
900ff8c6
CW
155struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
156struct list_head ptype_all __read_mostly; /* Taps */
62532da9 157static struct list_head offload_base __read_mostly;
1da177e4 158
ae78dbfa 159static int netif_rx_internal(struct sk_buff *skb);
54951194 160static int call_netdevice_notifiers_info(unsigned long val,
54951194 161 struct netdev_notifier_info *info);
26372605
PM
162static int call_netdevice_notifiers_extack(unsigned long val,
163 struct net_device *dev,
164 struct netlink_ext_ack *extack);
90b602f8 165static struct napi_struct *napi_by_id(unsigned int napi_id);
ae78dbfa 166
1da177e4 167/*
7562f876 168 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
1da177e4
LT
169 * semaphore.
170 *
c6d14c84 171 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
1da177e4
LT
172 *
173 * Writers must hold the rtnl semaphore while they loop through the
7562f876 174 * dev_base_head list, and hold dev_base_lock for writing when they do the
1da177e4
LT
175 * actual updates. This allows pure readers to access the list even
176 * while a writer is preparing to update it.
177 *
178 * To put it another way, dev_base_lock is held for writing only to
179 * protect against pure readers; the rtnl semaphore provides the
180 * protection against other writers.
181 *
182 * See, for example usages, register_netdevice() and
183 * unregister_netdevice(), which must be called with the rtnl
184 * semaphore held.
185 */
1da177e4 186DEFINE_RWLOCK(dev_base_lock);
1da177e4
LT
187EXPORT_SYMBOL(dev_base_lock);
188
6c557001
FW
189static DEFINE_MUTEX(ifalias_mutex);
190
af12fa6e
ET
191/* protects napi_hash addition/deletion and napi_gen_id */
192static DEFINE_SPINLOCK(napi_hash_lock);
193
52bd2d62 194static unsigned int napi_gen_id = NR_CPUS;
6180d9de 195static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
af12fa6e 196
18afa4b0 197static seqcount_t devnet_rename_seq;
c91f6df2 198
4e985ada
TG
199static inline void dev_base_seq_inc(struct net *net)
200{
643aa9cb 201 while (++net->dev_base_seq == 0)
202 ;
4e985ada
TG
203}
204
881d966b 205static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
1da177e4 206{
8387ff25 207 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
95c96174 208
08e9897d 209 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
1da177e4
LT
210}
211
881d966b 212static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
1da177e4 213{
7c28bd0b 214 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
1da177e4
LT
215}
216
e36fa2f7 217static inline void rps_lock(struct softnet_data *sd)
152102c7
CG
218{
219#ifdef CONFIG_RPS
e36fa2f7 220 spin_lock(&sd->input_pkt_queue.lock);
152102c7
CG
221#endif
222}
223
e36fa2f7 224static inline void rps_unlock(struct softnet_data *sd)
152102c7
CG
225{
226#ifdef CONFIG_RPS
e36fa2f7 227 spin_unlock(&sd->input_pkt_queue.lock);
152102c7
CG
228#endif
229}
230
ff927412
JP
231static struct netdev_name_node *netdev_name_node_alloc(struct net_device *dev,
232 const char *name)
233{
234 struct netdev_name_node *name_node;
235
236 name_node = kmalloc(sizeof(*name_node), GFP_KERNEL);
237 if (!name_node)
238 return NULL;
239 INIT_HLIST_NODE(&name_node->hlist);
240 name_node->dev = dev;
241 name_node->name = name;
242 return name_node;
243}
244
245static struct netdev_name_node *
246netdev_name_node_head_alloc(struct net_device *dev)
247{
36fbf1e5
JP
248 struct netdev_name_node *name_node;
249
250 name_node = netdev_name_node_alloc(dev, dev->name);
251 if (!name_node)
252 return NULL;
253 INIT_LIST_HEAD(&name_node->list);
254 return name_node;
ff927412
JP
255}
256
257static void netdev_name_node_free(struct netdev_name_node *name_node)
258{
259 kfree(name_node);
260}
261
262static void netdev_name_node_add(struct net *net,
263 struct netdev_name_node *name_node)
264{
265 hlist_add_head_rcu(&name_node->hlist,
266 dev_name_hash(net, name_node->name));
267}
268
269static void netdev_name_node_del(struct netdev_name_node *name_node)
270{
271 hlist_del_rcu(&name_node->hlist);
272}
273
274static struct netdev_name_node *netdev_name_node_lookup(struct net *net,
275 const char *name)
276{
277 struct hlist_head *head = dev_name_hash(net, name);
278 struct netdev_name_node *name_node;
279
280 hlist_for_each_entry(name_node, head, hlist)
281 if (!strcmp(name_node->name, name))
282 return name_node;
283 return NULL;
284}
285
286static struct netdev_name_node *netdev_name_node_lookup_rcu(struct net *net,
287 const char *name)
288{
289 struct hlist_head *head = dev_name_hash(net, name);
290 struct netdev_name_node *name_node;
291
292 hlist_for_each_entry_rcu(name_node, head, hlist)
293 if (!strcmp(name_node->name, name))
294 return name_node;
295 return NULL;
296}
297
36fbf1e5
JP
298int netdev_name_node_alt_create(struct net_device *dev, const char *name)
299{
300 struct netdev_name_node *name_node;
301 struct net *net = dev_net(dev);
302
303 name_node = netdev_name_node_lookup(net, name);
304 if (name_node)
305 return -EEXIST;
306 name_node = netdev_name_node_alloc(dev, name);
307 if (!name_node)
308 return -ENOMEM;
309 netdev_name_node_add(net, name_node);
310 /* The node that holds dev->name acts as a head of per-device list. */
311 list_add_tail(&name_node->list, &dev->name_node->list);
312
313 return 0;
314}
315EXPORT_SYMBOL(netdev_name_node_alt_create);
316
317static void __netdev_name_node_alt_destroy(struct netdev_name_node *name_node)
318{
319 list_del(&name_node->list);
320 netdev_name_node_del(name_node);
321 kfree(name_node->name);
322 netdev_name_node_free(name_node);
323}
324
325int netdev_name_node_alt_destroy(struct net_device *dev, const char *name)
326{
327 struct netdev_name_node *name_node;
328 struct net *net = dev_net(dev);
329
330 name_node = netdev_name_node_lookup(net, name);
331 if (!name_node)
332 return -ENOENT;
e08ad805
ED
333 /* lookup might have found our primary name or a name belonging
334 * to another device.
335 */
336 if (name_node == dev->name_node || name_node->dev != dev)
337 return -EINVAL;
338
36fbf1e5
JP
339 __netdev_name_node_alt_destroy(name_node);
340
341 return 0;
342}
343EXPORT_SYMBOL(netdev_name_node_alt_destroy);
344
345static void netdev_name_node_alt_flush(struct net_device *dev)
346{
347 struct netdev_name_node *name_node, *tmp;
348
349 list_for_each_entry_safe(name_node, tmp, &dev->name_node->list, list)
350 __netdev_name_node_alt_destroy(name_node);
351}
352
ce286d32 353/* Device list insertion */
53759be9 354static void list_netdevice(struct net_device *dev)
ce286d32 355{
c346dca1 356 struct net *net = dev_net(dev);
ce286d32
EB
357
358 ASSERT_RTNL();
359
360 write_lock_bh(&dev_base_lock);
c6d14c84 361 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
ff927412 362 netdev_name_node_add(net, dev->name_node);
fb699dfd
ED
363 hlist_add_head_rcu(&dev->index_hlist,
364 dev_index_hash(net, dev->ifindex));
ce286d32 365 write_unlock_bh(&dev_base_lock);
4e985ada
TG
366
367 dev_base_seq_inc(net);
ce286d32
EB
368}
369
fb699dfd
ED
370/* Device list removal
371 * caller must respect a RCU grace period before freeing/reusing dev
372 */
ce286d32
EB
373static void unlist_netdevice(struct net_device *dev)
374{
375 ASSERT_RTNL();
376
377 /* Unlink dev from the device chain */
378 write_lock_bh(&dev_base_lock);
c6d14c84 379 list_del_rcu(&dev->dev_list);
ff927412 380 netdev_name_node_del(dev->name_node);
fb699dfd 381 hlist_del_rcu(&dev->index_hlist);
ce286d32 382 write_unlock_bh(&dev_base_lock);
4e985ada
TG
383
384 dev_base_seq_inc(dev_net(dev));
ce286d32
EB
385}
386
1da177e4
LT
387/*
388 * Our notifier list
389 */
390
f07d5b94 391static RAW_NOTIFIER_HEAD(netdev_chain);
1da177e4
LT
392
393/*
394 * Device drivers call our routines to queue packets here. We empty the
395 * queue in the local softnet handler.
396 */
bea3348e 397
9958da05 398DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
d1b19dff 399EXPORT_PER_CPU_SYMBOL(softnet_data);
1da177e4 400
1da177e4 401/*******************************************************************************
eb13da1a 402 *
403 * Protocol management and registration routines
404 *
405 *******************************************************************************/
1da177e4 406
1da177e4 407
1da177e4
LT
408/*
409 * Add a protocol ID to the list. Now that the input handler is
410 * smarter we can dispense with all the messy stuff that used to be
411 * here.
412 *
413 * BEWARE!!! Protocol handlers, mangling input packets,
414 * MUST BE last in hash buckets and checking protocol handlers
415 * MUST start from promiscuous ptype_all chain in net_bh.
416 * It is true now, do not change it.
417 * Explanation follows: if protocol handler, mangling packet, will
418 * be the first on list, it is not able to sense, that packet
419 * is cloned and should be copied-on-write, so that it will
420 * change it and subsequent readers will get broken packet.
421 * --ANK (980803)
422 */
423
c07b68e8
ED
424static inline struct list_head *ptype_head(const struct packet_type *pt)
425{
426 if (pt->type == htons(ETH_P_ALL))
7866a621 427 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
c07b68e8 428 else
7866a621
SN
429 return pt->dev ? &pt->dev->ptype_specific :
430 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
c07b68e8
ED
431}
432
1da177e4
LT
433/**
434 * dev_add_pack - add packet handler
435 * @pt: packet type declaration
436 *
437 * Add a protocol handler to the networking stack. The passed &packet_type
438 * is linked into kernel lists and may not be freed until it has been
439 * removed from the kernel lists.
440 *
4ec93edb 441 * This call does not sleep therefore it can not
1da177e4
LT
442 * guarantee all CPU's that are in middle of receiving packets
443 * will see the new packet type (until the next received packet).
444 */
445
446void dev_add_pack(struct packet_type *pt)
447{
c07b68e8 448 struct list_head *head = ptype_head(pt);
1da177e4 449
c07b68e8
ED
450 spin_lock(&ptype_lock);
451 list_add_rcu(&pt->list, head);
452 spin_unlock(&ptype_lock);
1da177e4 453}
d1b19dff 454EXPORT_SYMBOL(dev_add_pack);
1da177e4 455
1da177e4
LT
456/**
457 * __dev_remove_pack - remove packet handler
458 * @pt: packet type declaration
459 *
460 * Remove a protocol handler that was previously added to the kernel
461 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
462 * from the kernel lists and can be freed or reused once this function
4ec93edb 463 * returns.
1da177e4
LT
464 *
465 * The packet type might still be in use by receivers
466 * and must not be freed until after all the CPU's have gone
467 * through a quiescent state.
468 */
469void __dev_remove_pack(struct packet_type *pt)
470{
c07b68e8 471 struct list_head *head = ptype_head(pt);
1da177e4
LT
472 struct packet_type *pt1;
473
c07b68e8 474 spin_lock(&ptype_lock);
1da177e4
LT
475
476 list_for_each_entry(pt1, head, list) {
477 if (pt == pt1) {
478 list_del_rcu(&pt->list);
479 goto out;
480 }
481 }
482
7b6cd1ce 483 pr_warn("dev_remove_pack: %p not found\n", pt);
1da177e4 484out:
c07b68e8 485 spin_unlock(&ptype_lock);
1da177e4 486}
d1b19dff
ED
487EXPORT_SYMBOL(__dev_remove_pack);
488
1da177e4
LT
489/**
490 * dev_remove_pack - remove packet handler
491 * @pt: packet type declaration
492 *
493 * Remove a protocol handler that was previously added to the kernel
494 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
495 * from the kernel lists and can be freed or reused once this function
496 * returns.
497 *
498 * This call sleeps to guarantee that no CPU is looking at the packet
499 * type after return.
500 */
501void dev_remove_pack(struct packet_type *pt)
502{
503 __dev_remove_pack(pt);
4ec93edb 504
1da177e4
LT
505 synchronize_net();
506}
d1b19dff 507EXPORT_SYMBOL(dev_remove_pack);
1da177e4 508
62532da9
VY
509
510/**
511 * dev_add_offload - register offload handlers
512 * @po: protocol offload declaration
513 *
514 * Add protocol offload handlers to the networking stack. The passed
515 * &proto_offload is linked into kernel lists and may not be freed until
516 * it has been removed from the kernel lists.
517 *
518 * This call does not sleep therefore it can not
519 * guarantee all CPU's that are in middle of receiving packets
520 * will see the new offload handlers (until the next received packet).
521 */
522void dev_add_offload(struct packet_offload *po)
523{
bdef7de4 524 struct packet_offload *elem;
62532da9
VY
525
526 spin_lock(&offload_lock);
bdef7de4
DM
527 list_for_each_entry(elem, &offload_base, list) {
528 if (po->priority < elem->priority)
529 break;
530 }
531 list_add_rcu(&po->list, elem->list.prev);
62532da9
VY
532 spin_unlock(&offload_lock);
533}
534EXPORT_SYMBOL(dev_add_offload);
535
536/**
537 * __dev_remove_offload - remove offload handler
538 * @po: packet offload declaration
539 *
540 * Remove a protocol offload handler that was previously added to the
541 * kernel offload handlers by dev_add_offload(). The passed &offload_type
542 * is removed from the kernel lists and can be freed or reused once this
543 * function returns.
544 *
545 * The packet type might still be in use by receivers
546 * and must not be freed until after all the CPU's have gone
547 * through a quiescent state.
548 */
1d143d9f 549static void __dev_remove_offload(struct packet_offload *po)
62532da9
VY
550{
551 struct list_head *head = &offload_base;
552 struct packet_offload *po1;
553
c53aa505 554 spin_lock(&offload_lock);
62532da9
VY
555
556 list_for_each_entry(po1, head, list) {
557 if (po == po1) {
558 list_del_rcu(&po->list);
559 goto out;
560 }
561 }
562
563 pr_warn("dev_remove_offload: %p not found\n", po);
564out:
c53aa505 565 spin_unlock(&offload_lock);
62532da9 566}
62532da9
VY
567
568/**
569 * dev_remove_offload - remove packet offload handler
570 * @po: packet offload declaration
571 *
572 * Remove a packet offload handler that was previously added to the kernel
573 * offload handlers by dev_add_offload(). The passed &offload_type is
574 * removed from the kernel lists and can be freed or reused once this
575 * function returns.
576 *
577 * This call sleeps to guarantee that no CPU is looking at the packet
578 * type after return.
579 */
580void dev_remove_offload(struct packet_offload *po)
581{
582 __dev_remove_offload(po);
583
584 synchronize_net();
585}
586EXPORT_SYMBOL(dev_remove_offload);
587
1da177e4 588/******************************************************************************
eb13da1a 589 *
590 * Device Boot-time Settings Routines
591 *
592 ******************************************************************************/
1da177e4
LT
593
594/* Boot time configuration table */
595static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
596
597/**
598 * netdev_boot_setup_add - add new setup entry
599 * @name: name of the device
600 * @map: configured settings for the device
601 *
602 * Adds new setup entry to the dev_boot_setup list. The function
603 * returns 0 on error and 1 on success. This is a generic routine to
604 * all netdevices.
605 */
606static int netdev_boot_setup_add(char *name, struct ifmap *map)
607{
608 struct netdev_boot_setup *s;
609 int i;
610
611 s = dev_boot_setup;
612 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
613 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
614 memset(s[i].name, 0, sizeof(s[i].name));
93b3cff9 615 strlcpy(s[i].name, name, IFNAMSIZ);
1da177e4
LT
616 memcpy(&s[i].map, map, sizeof(s[i].map));
617 break;
618 }
619 }
620
621 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
622}
623
624/**
722c9a0c 625 * netdev_boot_setup_check - check boot time settings
626 * @dev: the netdevice
1da177e4 627 *
722c9a0c 628 * Check boot time settings for the device.
629 * The found settings are set for the device to be used
630 * later in the device probing.
631 * Returns 0 if no settings found, 1 if they are.
1da177e4
LT
632 */
633int netdev_boot_setup_check(struct net_device *dev)
634{
635 struct netdev_boot_setup *s = dev_boot_setup;
636 int i;
637
638 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
639 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
93b3cff9 640 !strcmp(dev->name, s[i].name)) {
722c9a0c 641 dev->irq = s[i].map.irq;
642 dev->base_addr = s[i].map.base_addr;
643 dev->mem_start = s[i].map.mem_start;
644 dev->mem_end = s[i].map.mem_end;
1da177e4
LT
645 return 1;
646 }
647 }
648 return 0;
649}
d1b19dff 650EXPORT_SYMBOL(netdev_boot_setup_check);
1da177e4
LT
651
652
653/**
722c9a0c 654 * netdev_boot_base - get address from boot time settings
655 * @prefix: prefix for network device
656 * @unit: id for network device
657 *
658 * Check boot time settings for the base address of device.
659 * The found settings are set for the device to be used
660 * later in the device probing.
661 * Returns 0 if no settings found.
1da177e4
LT
662 */
663unsigned long netdev_boot_base(const char *prefix, int unit)
664{
665 const struct netdev_boot_setup *s = dev_boot_setup;
666 char name[IFNAMSIZ];
667 int i;
668
669 sprintf(name, "%s%d", prefix, unit);
670
671 /*
672 * If device already registered then return base of 1
673 * to indicate not to probe for this interface
674 */
881d966b 675 if (__dev_get_by_name(&init_net, name))
1da177e4
LT
676 return 1;
677
678 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
679 if (!strcmp(name, s[i].name))
680 return s[i].map.base_addr;
681 return 0;
682}
683
684/*
685 * Saves at boot time configured settings for any netdevice.
686 */
687int __init netdev_boot_setup(char *str)
688{
689 int ints[5];
690 struct ifmap map;
691
692 str = get_options(str, ARRAY_SIZE(ints), ints);
693 if (!str || !*str)
694 return 0;
695
696 /* Save settings */
697 memset(&map, 0, sizeof(map));
698 if (ints[0] > 0)
699 map.irq = ints[1];
700 if (ints[0] > 1)
701 map.base_addr = ints[2];
702 if (ints[0] > 2)
703 map.mem_start = ints[3];
704 if (ints[0] > 3)
705 map.mem_end = ints[4];
706
707 /* Add new entry to the list */
708 return netdev_boot_setup_add(str, &map);
709}
710
711__setup("netdev=", netdev_boot_setup);
712
713/*******************************************************************************
eb13da1a 714 *
715 * Device Interface Subroutines
716 *
717 *******************************************************************************/
1da177e4 718
a54acb3a
ND
719/**
720 * dev_get_iflink - get 'iflink' value of a interface
721 * @dev: targeted interface
722 *
723 * Indicates the ifindex the interface is linked to.
724 * Physical interfaces have the same 'ifindex' and 'iflink' values.
725 */
726
727int dev_get_iflink(const struct net_device *dev)
728{
729 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
730 return dev->netdev_ops->ndo_get_iflink(dev);
731
7a66bbc9 732 return dev->ifindex;
a54acb3a
ND
733}
734EXPORT_SYMBOL(dev_get_iflink);
735
fc4099f1
PS
736/**
737 * dev_fill_metadata_dst - Retrieve tunnel egress information.
738 * @dev: targeted interface
739 * @skb: The packet.
740 *
741 * For better visibility of tunnel traffic OVS needs to retrieve
742 * egress tunnel information for a packet. Following API allows
743 * user to get this info.
744 */
745int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
746{
747 struct ip_tunnel_info *info;
748
749 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
750 return -EINVAL;
751
752 info = skb_tunnel_info_unclone(skb);
753 if (!info)
754 return -ENOMEM;
755 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
756 return -EINVAL;
757
758 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
759}
760EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
761
1da177e4
LT
762/**
763 * __dev_get_by_name - find a device by its name
c4ea43c5 764 * @net: the applicable net namespace
1da177e4
LT
765 * @name: name to find
766 *
767 * Find an interface by name. Must be called under RTNL semaphore
768 * or @dev_base_lock. If the name is found a pointer to the device
769 * is returned. If the name is not found then %NULL is returned. The
770 * reference counters are not incremented so the caller must be
771 * careful with locks.
772 */
773
881d966b 774struct net_device *__dev_get_by_name(struct net *net, const char *name)
1da177e4 775{
ff927412 776 struct netdev_name_node *node_name;
1da177e4 777
ff927412
JP
778 node_name = netdev_name_node_lookup(net, name);
779 return node_name ? node_name->dev : NULL;
1da177e4 780}
d1b19dff 781EXPORT_SYMBOL(__dev_get_by_name);
1da177e4 782
72c9528b 783/**
722c9a0c 784 * dev_get_by_name_rcu - find a device by its name
785 * @net: the applicable net namespace
786 * @name: name to find
787 *
788 * Find an interface by name.
789 * If the name is found a pointer to the device is returned.
790 * If the name is not found then %NULL is returned.
791 * The reference counters are not incremented so the caller must be
792 * careful with locks. The caller must hold RCU lock.
72c9528b
ED
793 */
794
795struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
796{
ff927412 797 struct netdev_name_node *node_name;
72c9528b 798
ff927412
JP
799 node_name = netdev_name_node_lookup_rcu(net, name);
800 return node_name ? node_name->dev : NULL;
72c9528b
ED
801}
802EXPORT_SYMBOL(dev_get_by_name_rcu);
803
1da177e4
LT
804/**
805 * dev_get_by_name - find a device by its name
c4ea43c5 806 * @net: the applicable net namespace
1da177e4
LT
807 * @name: name to find
808 *
809 * Find an interface by name. This can be called from any
810 * context and does its own locking. The returned handle has
811 * the usage count incremented and the caller must use dev_put() to
812 * release it when it is no longer needed. %NULL is returned if no
813 * matching device is found.
814 */
815
881d966b 816struct net_device *dev_get_by_name(struct net *net, const char *name)
1da177e4
LT
817{
818 struct net_device *dev;
819
72c9528b
ED
820 rcu_read_lock();
821 dev = dev_get_by_name_rcu(net, name);
1da177e4
LT
822 if (dev)
823 dev_hold(dev);
72c9528b 824 rcu_read_unlock();
1da177e4
LT
825 return dev;
826}
d1b19dff 827EXPORT_SYMBOL(dev_get_by_name);
1da177e4
LT
828
829/**
830 * __dev_get_by_index - find a device by its ifindex
c4ea43c5 831 * @net: the applicable net namespace
1da177e4
LT
832 * @ifindex: index of device
833 *
834 * Search for an interface by index. Returns %NULL if the device
835 * is not found or a pointer to the device. The device has not
836 * had its reference counter increased so the caller must be careful
837 * about locking. The caller must hold either the RTNL semaphore
838 * or @dev_base_lock.
839 */
840
881d966b 841struct net_device *__dev_get_by_index(struct net *net, int ifindex)
1da177e4 842{
0bd8d536
ED
843 struct net_device *dev;
844 struct hlist_head *head = dev_index_hash(net, ifindex);
1da177e4 845
b67bfe0d 846 hlist_for_each_entry(dev, head, index_hlist)
1da177e4
LT
847 if (dev->ifindex == ifindex)
848 return dev;
0bd8d536 849
1da177e4
LT
850 return NULL;
851}
d1b19dff 852EXPORT_SYMBOL(__dev_get_by_index);
1da177e4 853
fb699dfd
ED
854/**
855 * dev_get_by_index_rcu - find a device by its ifindex
856 * @net: the applicable net namespace
857 * @ifindex: index of device
858 *
859 * Search for an interface by index. Returns %NULL if the device
860 * is not found or a pointer to the device. The device has not
861 * had its reference counter increased so the caller must be careful
862 * about locking. The caller must hold RCU lock.
863 */
864
865struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
866{
fb699dfd
ED
867 struct net_device *dev;
868 struct hlist_head *head = dev_index_hash(net, ifindex);
869
b67bfe0d 870 hlist_for_each_entry_rcu(dev, head, index_hlist)
fb699dfd
ED
871 if (dev->ifindex == ifindex)
872 return dev;
873
874 return NULL;
875}
876EXPORT_SYMBOL(dev_get_by_index_rcu);
877
1da177e4
LT
878
879/**
880 * dev_get_by_index - find a device by its ifindex
c4ea43c5 881 * @net: the applicable net namespace
1da177e4
LT
882 * @ifindex: index of device
883 *
884 * Search for an interface by index. Returns NULL if the device
885 * is not found or a pointer to the device. The device returned has
886 * had a reference added and the pointer is safe until the user calls
887 * dev_put to indicate they have finished with it.
888 */
889
881d966b 890struct net_device *dev_get_by_index(struct net *net, int ifindex)
1da177e4
LT
891{
892 struct net_device *dev;
893
fb699dfd
ED
894 rcu_read_lock();
895 dev = dev_get_by_index_rcu(net, ifindex);
1da177e4
LT
896 if (dev)
897 dev_hold(dev);
fb699dfd 898 rcu_read_unlock();
1da177e4
LT
899 return dev;
900}
d1b19dff 901EXPORT_SYMBOL(dev_get_by_index);
1da177e4 902
90b602f8
ML
903/**
904 * dev_get_by_napi_id - find a device by napi_id
905 * @napi_id: ID of the NAPI struct
906 *
907 * Search for an interface by NAPI ID. Returns %NULL if the device
908 * is not found or a pointer to the device. The device has not had
909 * its reference counter increased so the caller must be careful
910 * about locking. The caller must hold RCU lock.
911 */
912
913struct net_device *dev_get_by_napi_id(unsigned int napi_id)
914{
915 struct napi_struct *napi;
916
917 WARN_ON_ONCE(!rcu_read_lock_held());
918
919 if (napi_id < MIN_NAPI_ID)
920 return NULL;
921
922 napi = napi_by_id(napi_id);
923
924 return napi ? napi->dev : NULL;
925}
926EXPORT_SYMBOL(dev_get_by_napi_id);
927
5dbe7c17
NS
928/**
929 * netdev_get_name - get a netdevice name, knowing its ifindex.
930 * @net: network namespace
931 * @name: a pointer to the buffer where the name will be stored.
932 * @ifindex: the ifindex of the interface to get the name from.
933 *
934 * The use of raw_seqcount_begin() and cond_resched() before
935 * retrying is required as we want to give the writers a chance
2da2b32f 936 * to complete when CONFIG_PREEMPTION is not set.
5dbe7c17
NS
937 */
938int netdev_get_name(struct net *net, char *name, int ifindex)
939{
940 struct net_device *dev;
941 unsigned int seq;
942
943retry:
944 seq = raw_seqcount_begin(&devnet_rename_seq);
945 rcu_read_lock();
946 dev = dev_get_by_index_rcu(net, ifindex);
947 if (!dev) {
948 rcu_read_unlock();
949 return -ENODEV;
950 }
951
952 strcpy(name, dev->name);
953 rcu_read_unlock();
954 if (read_seqcount_retry(&devnet_rename_seq, seq)) {
955 cond_resched();
956 goto retry;
957 }
958
959 return 0;
960}
961
1da177e4 962/**
941666c2 963 * dev_getbyhwaddr_rcu - find a device by its hardware address
c4ea43c5 964 * @net: the applicable net namespace
1da177e4
LT
965 * @type: media type of device
966 * @ha: hardware address
967 *
968 * Search for an interface by MAC address. Returns NULL if the device
c506653d
ED
969 * is not found or a pointer to the device.
970 * The caller must hold RCU or RTNL.
941666c2 971 * The returned device has not had its ref count increased
1da177e4
LT
972 * and the caller must therefore be careful about locking
973 *
1da177e4
LT
974 */
975
941666c2
ED
976struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
977 const char *ha)
1da177e4
LT
978{
979 struct net_device *dev;
980
941666c2 981 for_each_netdev_rcu(net, dev)
1da177e4
LT
982 if (dev->type == type &&
983 !memcmp(dev->dev_addr, ha, dev->addr_len))
7562f876
PE
984 return dev;
985
986 return NULL;
1da177e4 987}
941666c2 988EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
cf309e3f 989
881d966b 990struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
1da177e4
LT
991{
992 struct net_device *dev;
993
4e9cac2b 994 ASSERT_RTNL();
881d966b 995 for_each_netdev(net, dev)
4e9cac2b 996 if (dev->type == type)
7562f876
PE
997 return dev;
998
999 return NULL;
4e9cac2b 1000}
4e9cac2b
PM
1001EXPORT_SYMBOL(__dev_getfirstbyhwtype);
1002
881d966b 1003struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
4e9cac2b 1004{
99fe3c39 1005 struct net_device *dev, *ret = NULL;
4e9cac2b 1006
99fe3c39
ED
1007 rcu_read_lock();
1008 for_each_netdev_rcu(net, dev)
1009 if (dev->type == type) {
1010 dev_hold(dev);
1011 ret = dev;
1012 break;
1013 }
1014 rcu_read_unlock();
1015 return ret;
1da177e4 1016}
1da177e4
LT
1017EXPORT_SYMBOL(dev_getfirstbyhwtype);
1018
1019/**
6c555490 1020 * __dev_get_by_flags - find any device with given flags
c4ea43c5 1021 * @net: the applicable net namespace
1da177e4
LT
1022 * @if_flags: IFF_* values
1023 * @mask: bitmask of bits in if_flags to check
1024 *
1025 * Search for any interface with the given flags. Returns NULL if a device
bb69ae04 1026 * is not found or a pointer to the device. Must be called inside
6c555490 1027 * rtnl_lock(), and result refcount is unchanged.
1da177e4
LT
1028 */
1029
6c555490
WC
1030struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
1031 unsigned short mask)
1da177e4 1032{
7562f876 1033 struct net_device *dev, *ret;
1da177e4 1034
6c555490
WC
1035 ASSERT_RTNL();
1036
7562f876 1037 ret = NULL;
6c555490 1038 for_each_netdev(net, dev) {
1da177e4 1039 if (((dev->flags ^ if_flags) & mask) == 0) {
7562f876 1040 ret = dev;
1da177e4
LT
1041 break;
1042 }
1043 }
7562f876 1044 return ret;
1da177e4 1045}
6c555490 1046EXPORT_SYMBOL(__dev_get_by_flags);
1da177e4
LT
1047
1048/**
1049 * dev_valid_name - check if name is okay for network device
1050 * @name: name string
1051 *
1052 * Network device names need to be valid file names to
c7fa9d18
DM
1053 * to allow sysfs to work. We also disallow any kind of
1054 * whitespace.
1da177e4 1055 */
95f050bf 1056bool dev_valid_name(const char *name)
1da177e4 1057{
c7fa9d18 1058 if (*name == '\0')
95f050bf 1059 return false;
a9d48205 1060 if (strnlen(name, IFNAMSIZ) == IFNAMSIZ)
95f050bf 1061 return false;
c7fa9d18 1062 if (!strcmp(name, ".") || !strcmp(name, ".."))
95f050bf 1063 return false;
c7fa9d18
DM
1064
1065 while (*name) {
a4176a93 1066 if (*name == '/' || *name == ':' || isspace(*name))
95f050bf 1067 return false;
c7fa9d18
DM
1068 name++;
1069 }
95f050bf 1070 return true;
1da177e4 1071}
d1b19dff 1072EXPORT_SYMBOL(dev_valid_name);
1da177e4
LT
1073
1074/**
b267b179
EB
1075 * __dev_alloc_name - allocate a name for a device
1076 * @net: network namespace to allocate the device name in
1da177e4 1077 * @name: name format string
b267b179 1078 * @buf: scratch buffer and result name string
1da177e4
LT
1079 *
1080 * Passed a format string - eg "lt%d" it will try and find a suitable
3041a069
SH
1081 * id. It scans list of devices to build up a free map, then chooses
1082 * the first empty slot. The caller must hold the dev_base or rtnl lock
1083 * while allocating the name and adding the device in order to avoid
1084 * duplicates.
1085 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1086 * Returns the number of the unit assigned or a negative errno code.
1da177e4
LT
1087 */
1088
b267b179 1089static int __dev_alloc_name(struct net *net, const char *name, char *buf)
1da177e4
LT
1090{
1091 int i = 0;
1da177e4
LT
1092 const char *p;
1093 const int max_netdevices = 8*PAGE_SIZE;
cfcabdcc 1094 unsigned long *inuse;
1da177e4
LT
1095 struct net_device *d;
1096
93809105
RV
1097 if (!dev_valid_name(name))
1098 return -EINVAL;
1099
51f299dd 1100 p = strchr(name, '%');
1da177e4
LT
1101 if (p) {
1102 /*
1103 * Verify the string as this thing may have come from
1104 * the user. There must be either one "%d" and no other "%"
1105 * characters.
1106 */
1107 if (p[1] != 'd' || strchr(p + 2, '%'))
1108 return -EINVAL;
1109
1110 /* Use one page as a bit array of possible slots */
cfcabdcc 1111 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
1da177e4
LT
1112 if (!inuse)
1113 return -ENOMEM;
1114
881d966b 1115 for_each_netdev(net, d) {
1da177e4
LT
1116 if (!sscanf(d->name, name, &i))
1117 continue;
1118 if (i < 0 || i >= max_netdevices)
1119 continue;
1120
1121 /* avoid cases where sscanf is not exact inverse of printf */
b267b179 1122 snprintf(buf, IFNAMSIZ, name, i);
1da177e4
LT
1123 if (!strncmp(buf, d->name, IFNAMSIZ))
1124 set_bit(i, inuse);
1125 }
1126
1127 i = find_first_zero_bit(inuse, max_netdevices);
1128 free_page((unsigned long) inuse);
1129 }
1130
6224abda 1131 snprintf(buf, IFNAMSIZ, name, i);
b267b179 1132 if (!__dev_get_by_name(net, buf))
1da177e4 1133 return i;
1da177e4
LT
1134
1135 /* It is possible to run out of possible slots
1136 * when the name is long and there isn't enough space left
1137 * for the digits, or if all bits are used.
1138 */
029b6d14 1139 return -ENFILE;
1da177e4
LT
1140}
1141
2c88b855
RV
1142static int dev_alloc_name_ns(struct net *net,
1143 struct net_device *dev,
1144 const char *name)
1145{
1146 char buf[IFNAMSIZ];
1147 int ret;
1148
c46d7642 1149 BUG_ON(!net);
2c88b855
RV
1150 ret = __dev_alloc_name(net, name, buf);
1151 if (ret >= 0)
1152 strlcpy(dev->name, buf, IFNAMSIZ);
1153 return ret;
1da177e4
LT
1154}
1155
b267b179
EB
1156/**
1157 * dev_alloc_name - allocate a name for a device
1158 * @dev: device
1159 * @name: name format string
1160 *
1161 * Passed a format string - eg "lt%d" it will try and find a suitable
1162 * id. It scans list of devices to build up a free map, then chooses
1163 * the first empty slot. The caller must hold the dev_base or rtnl lock
1164 * while allocating the name and adding the device in order to avoid
1165 * duplicates.
1166 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1167 * Returns the number of the unit assigned or a negative errno code.
1168 */
1169
1170int dev_alloc_name(struct net_device *dev, const char *name)
1171{
c46d7642 1172 return dev_alloc_name_ns(dev_net(dev), dev, name);
b267b179 1173}
d1b19dff 1174EXPORT_SYMBOL(dev_alloc_name);
b267b179 1175
bacb7e18
ED
1176static int dev_get_valid_name(struct net *net, struct net_device *dev,
1177 const char *name)
828de4f6 1178{
55a5ec9b
DM
1179 BUG_ON(!net);
1180
1181 if (!dev_valid_name(name))
1182 return -EINVAL;
1183
1184 if (strchr(name, '%'))
1185 return dev_alloc_name_ns(net, dev, name);
1186 else if (__dev_get_by_name(net, name))
1187 return -EEXIST;
1188 else if (dev->name != name)
1189 strlcpy(dev->name, name, IFNAMSIZ);
1190
1191 return 0;
d9031024 1192}
1da177e4
LT
1193
1194/**
1195 * dev_change_name - change name of a device
1196 * @dev: device
1197 * @newname: name (or format string) must be at least IFNAMSIZ
1198 *
1199 * Change name of a device, can pass format strings "eth%d".
1200 * for wildcarding.
1201 */
cf04a4c7 1202int dev_change_name(struct net_device *dev, const char *newname)
1da177e4 1203{
238fa362 1204 unsigned char old_assign_type;
fcc5a03a 1205 char oldname[IFNAMSIZ];
1da177e4 1206 int err = 0;
fcc5a03a 1207 int ret;
881d966b 1208 struct net *net;
1da177e4
LT
1209
1210 ASSERT_RTNL();
c346dca1 1211 BUG_ON(!dev_net(dev));
1da177e4 1212
c346dca1 1213 net = dev_net(dev);
8065a779
SWL
1214
1215 /* Some auto-enslaved devices e.g. failover slaves are
1216 * special, as userspace might rename the device after
1217 * the interface had been brought up and running since
1218 * the point kernel initiated auto-enslavement. Allow
1219 * live name change even when these slave devices are
1220 * up and running.
1221 *
1222 * Typically, users of these auto-enslaving devices
1223 * don't actually care about slave name change, as
1224 * they are supposed to operate on master interface
1225 * directly.
1226 */
1227 if (dev->flags & IFF_UP &&
1228 likely(!(dev->priv_flags & IFF_LIVE_RENAME_OK)))
1da177e4
LT
1229 return -EBUSY;
1230
30e6c9fa 1231 write_seqcount_begin(&devnet_rename_seq);
c91f6df2
BH
1232
1233 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
30e6c9fa 1234 write_seqcount_end(&devnet_rename_seq);
c8d90dca 1235 return 0;
c91f6df2 1236 }
c8d90dca 1237
fcc5a03a
HX
1238 memcpy(oldname, dev->name, IFNAMSIZ);
1239
828de4f6 1240 err = dev_get_valid_name(net, dev, newname);
c91f6df2 1241 if (err < 0) {
30e6c9fa 1242 write_seqcount_end(&devnet_rename_seq);
d9031024 1243 return err;
c91f6df2 1244 }
1da177e4 1245
6fe82a39
VF
1246 if (oldname[0] && !strchr(oldname, '%'))
1247 netdev_info(dev, "renamed from %s\n", oldname);
1248
238fa362
TG
1249 old_assign_type = dev->name_assign_type;
1250 dev->name_assign_type = NET_NAME_RENAMED;
1251
fcc5a03a 1252rollback:
a1b3f594
EB
1253 ret = device_rename(&dev->dev, dev->name);
1254 if (ret) {
1255 memcpy(dev->name, oldname, IFNAMSIZ);
238fa362 1256 dev->name_assign_type = old_assign_type;
30e6c9fa 1257 write_seqcount_end(&devnet_rename_seq);
a1b3f594 1258 return ret;
dcc99773 1259 }
7f988eab 1260
30e6c9fa 1261 write_seqcount_end(&devnet_rename_seq);
c91f6df2 1262
5bb025fa
VF
1263 netdev_adjacent_rename_links(dev, oldname);
1264
7f988eab 1265 write_lock_bh(&dev_base_lock);
ff927412 1266 netdev_name_node_del(dev->name_node);
72c9528b
ED
1267 write_unlock_bh(&dev_base_lock);
1268
1269 synchronize_rcu();
1270
1271 write_lock_bh(&dev_base_lock);
ff927412 1272 netdev_name_node_add(net, dev->name_node);
7f988eab
HX
1273 write_unlock_bh(&dev_base_lock);
1274
056925ab 1275 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
fcc5a03a
HX
1276 ret = notifier_to_errno(ret);
1277
1278 if (ret) {
91e9c07b
ED
1279 /* err >= 0 after dev_alloc_name() or stores the first errno */
1280 if (err >= 0) {
fcc5a03a 1281 err = ret;
30e6c9fa 1282 write_seqcount_begin(&devnet_rename_seq);
fcc5a03a 1283 memcpy(dev->name, oldname, IFNAMSIZ);
5bb025fa 1284 memcpy(oldname, newname, IFNAMSIZ);
238fa362
TG
1285 dev->name_assign_type = old_assign_type;
1286 old_assign_type = NET_NAME_RENAMED;
fcc5a03a 1287 goto rollback;
91e9c07b 1288 } else {
7b6cd1ce 1289 pr_err("%s: name change rollback failed: %d\n",
91e9c07b 1290 dev->name, ret);
fcc5a03a
HX
1291 }
1292 }
1da177e4
LT
1293
1294 return err;
1295}
1296
0b815a1a
SH
1297/**
1298 * dev_set_alias - change ifalias of a device
1299 * @dev: device
1300 * @alias: name up to IFALIASZ
f0db275a 1301 * @len: limit of bytes to copy from info
0b815a1a
SH
1302 *
1303 * Set ifalias for a device,
1304 */
1305int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1306{
6c557001 1307 struct dev_ifalias *new_alias = NULL;
0b815a1a
SH
1308
1309 if (len >= IFALIASZ)
1310 return -EINVAL;
1311
6c557001
FW
1312 if (len) {
1313 new_alias = kmalloc(sizeof(*new_alias) + len + 1, GFP_KERNEL);
1314 if (!new_alias)
1315 return -ENOMEM;
1316
1317 memcpy(new_alias->ifalias, alias, len);
1318 new_alias->ifalias[len] = 0;
96ca4a2c
OH
1319 }
1320
6c557001 1321 mutex_lock(&ifalias_mutex);
e3f0d761
PM
1322 new_alias = rcu_replace_pointer(dev->ifalias, new_alias,
1323 mutex_is_locked(&ifalias_mutex));
6c557001
FW
1324 mutex_unlock(&ifalias_mutex);
1325
1326 if (new_alias)
1327 kfree_rcu(new_alias, rcuhead);
0b815a1a 1328
0b815a1a
SH
1329 return len;
1330}
0fe554a4 1331EXPORT_SYMBOL(dev_set_alias);
0b815a1a 1332
6c557001
FW
1333/**
1334 * dev_get_alias - get ifalias of a device
1335 * @dev: device
20e88320 1336 * @name: buffer to store name of ifalias
6c557001
FW
1337 * @len: size of buffer
1338 *
1339 * get ifalias for a device. Caller must make sure dev cannot go
1340 * away, e.g. rcu read lock or own a reference count to device.
1341 */
1342int dev_get_alias(const struct net_device *dev, char *name, size_t len)
1343{
1344 const struct dev_ifalias *alias;
1345 int ret = 0;
1346
1347 rcu_read_lock();
1348 alias = rcu_dereference(dev->ifalias);
1349 if (alias)
1350 ret = snprintf(name, len, "%s", alias->ifalias);
1351 rcu_read_unlock();
1352
1353 return ret;
1354}
0b815a1a 1355
d8a33ac4 1356/**
3041a069 1357 * netdev_features_change - device changes features
d8a33ac4
SH
1358 * @dev: device to cause notification
1359 *
1360 * Called to indicate a device has changed features.
1361 */
1362void netdev_features_change(struct net_device *dev)
1363{
056925ab 1364 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
d8a33ac4
SH
1365}
1366EXPORT_SYMBOL(netdev_features_change);
1367
1da177e4
LT
1368/**
1369 * netdev_state_change - device changes state
1370 * @dev: device to cause notification
1371 *
1372 * Called to indicate a device has changed state. This function calls
1373 * the notifier chains for netdev_chain and sends a NEWLINK message
1374 * to the routing socket.
1375 */
1376void netdev_state_change(struct net_device *dev)
1377{
1378 if (dev->flags & IFF_UP) {
51d0c047
DA
1379 struct netdev_notifier_change_info change_info = {
1380 .info.dev = dev,
1381 };
54951194 1382
51d0c047 1383 call_netdevice_notifiers_info(NETDEV_CHANGE,
54951194 1384 &change_info.info);
7f294054 1385 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
1da177e4
LT
1386 }
1387}
d1b19dff 1388EXPORT_SYMBOL(netdev_state_change);
1da177e4 1389
ee89bab1 1390/**
722c9a0c 1391 * netdev_notify_peers - notify network peers about existence of @dev
1392 * @dev: network device
ee89bab1
AW
1393 *
1394 * Generate traffic such that interested network peers are aware of
1395 * @dev, such as by generating a gratuitous ARP. This may be used when
1396 * a device wants to inform the rest of the network about some sort of
1397 * reconfiguration such as a failover event or virtual machine
1398 * migration.
1399 */
1400void netdev_notify_peers(struct net_device *dev)
c1da4ac7 1401{
ee89bab1
AW
1402 rtnl_lock();
1403 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
37c343b4 1404 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
ee89bab1 1405 rtnl_unlock();
c1da4ac7 1406}
ee89bab1 1407EXPORT_SYMBOL(netdev_notify_peers);
c1da4ac7 1408
40c900aa 1409static int __dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
1da177e4 1410{
d314774c 1411 const struct net_device_ops *ops = dev->netdev_ops;
3b8bcfd5 1412 int ret;
1da177e4 1413
e46b66bc
BH
1414 ASSERT_RTNL();
1415
1da177e4
LT
1416 if (!netif_device_present(dev))
1417 return -ENODEV;
1418
ca99ca14
NH
1419 /* Block netpoll from trying to do any rx path servicing.
1420 * If we don't do this there is a chance ndo_poll_controller
1421 * or ndo_poll may be running while we open the device
1422 */
66b5552f 1423 netpoll_poll_disable(dev);
ca99ca14 1424
40c900aa 1425 ret = call_netdevice_notifiers_extack(NETDEV_PRE_UP, dev, extack);
3b8bcfd5
JB
1426 ret = notifier_to_errno(ret);
1427 if (ret)
1428 return ret;
1429
1da177e4 1430 set_bit(__LINK_STATE_START, &dev->state);
bada339b 1431
d314774c
SH
1432 if (ops->ndo_validate_addr)
1433 ret = ops->ndo_validate_addr(dev);
bada339b 1434
d314774c
SH
1435 if (!ret && ops->ndo_open)
1436 ret = ops->ndo_open(dev);
1da177e4 1437
66b5552f 1438 netpoll_poll_enable(dev);
ca99ca14 1439
bada339b
JG
1440 if (ret)
1441 clear_bit(__LINK_STATE_START, &dev->state);
1442 else {
1da177e4 1443 dev->flags |= IFF_UP;
4417da66 1444 dev_set_rx_mode(dev);
1da177e4 1445 dev_activate(dev);
7bf23575 1446 add_device_randomness(dev->dev_addr, dev->addr_len);
1da177e4 1447 }
bada339b 1448
1da177e4
LT
1449 return ret;
1450}
1451
1452/**
bd380811 1453 * dev_open - prepare an interface for use.
00f54e68
PM
1454 * @dev: device to open
1455 * @extack: netlink extended ack
1da177e4 1456 *
bd380811
PM
1457 * Takes a device from down to up state. The device's private open
1458 * function is invoked and then the multicast lists are loaded. Finally
1459 * the device is moved into the up state and a %NETDEV_UP message is
1460 * sent to the netdev notifier chain.
1461 *
1462 * Calling this function on an active interface is a nop. On a failure
1463 * a negative errno code is returned.
1da177e4 1464 */
00f54e68 1465int dev_open(struct net_device *dev, struct netlink_ext_ack *extack)
bd380811
PM
1466{
1467 int ret;
1468
bd380811
PM
1469 if (dev->flags & IFF_UP)
1470 return 0;
1471
40c900aa 1472 ret = __dev_open(dev, extack);
bd380811
PM
1473 if (ret < 0)
1474 return ret;
1475
7f294054 1476 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
bd380811
PM
1477 call_netdevice_notifiers(NETDEV_UP, dev);
1478
1479 return ret;
1480}
1481EXPORT_SYMBOL(dev_open);
1482
7051b88a 1483static void __dev_close_many(struct list_head *head)
1da177e4 1484{
44345724 1485 struct net_device *dev;
e46b66bc 1486
bd380811 1487 ASSERT_RTNL();
9d5010db
DM
1488 might_sleep();
1489
5cde2829 1490 list_for_each_entry(dev, head, close_list) {
3f4df206 1491 /* Temporarily disable netpoll until the interface is down */
66b5552f 1492 netpoll_poll_disable(dev);
3f4df206 1493
44345724 1494 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1da177e4 1495
44345724 1496 clear_bit(__LINK_STATE_START, &dev->state);
1da177e4 1497
44345724
OP
1498 /* Synchronize to scheduled poll. We cannot touch poll list, it
1499 * can be even on different cpu. So just clear netif_running().
1500 *
1501 * dev->stop() will invoke napi_disable() on all of it's
1502 * napi_struct instances on this device.
1503 */
4e857c58 1504 smp_mb__after_atomic(); /* Commit netif_running(). */
44345724 1505 }
1da177e4 1506
44345724 1507 dev_deactivate_many(head);
d8b2a4d2 1508
5cde2829 1509 list_for_each_entry(dev, head, close_list) {
44345724 1510 const struct net_device_ops *ops = dev->netdev_ops;
1da177e4 1511
44345724
OP
1512 /*
1513 * Call the device specific close. This cannot fail.
1514 * Only if device is UP
1515 *
1516 * We allow it to be called even after a DETACH hot-plug
1517 * event.
1518 */
1519 if (ops->ndo_stop)
1520 ops->ndo_stop(dev);
1521
44345724 1522 dev->flags &= ~IFF_UP;
66b5552f 1523 netpoll_poll_enable(dev);
44345724 1524 }
44345724
OP
1525}
1526
7051b88a 1527static void __dev_close(struct net_device *dev)
44345724
OP
1528{
1529 LIST_HEAD(single);
1530
5cde2829 1531 list_add(&dev->close_list, &single);
7051b88a 1532 __dev_close_many(&single);
f87e6f47 1533 list_del(&single);
44345724
OP
1534}
1535
7051b88a 1536void dev_close_many(struct list_head *head, bool unlink)
44345724
OP
1537{
1538 struct net_device *dev, *tmp;
1da177e4 1539
5cde2829
EB
1540 /* Remove the devices that don't need to be closed */
1541 list_for_each_entry_safe(dev, tmp, head, close_list)
44345724 1542 if (!(dev->flags & IFF_UP))
5cde2829 1543 list_del_init(&dev->close_list);
44345724
OP
1544
1545 __dev_close_many(head);
1da177e4 1546
5cde2829 1547 list_for_each_entry_safe(dev, tmp, head, close_list) {
7f294054 1548 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
44345724 1549 call_netdevice_notifiers(NETDEV_DOWN, dev);
99c4a26a
DM
1550 if (unlink)
1551 list_del_init(&dev->close_list);
44345724 1552 }
bd380811 1553}
99c4a26a 1554EXPORT_SYMBOL(dev_close_many);
bd380811
PM
1555
1556/**
1557 * dev_close - shutdown an interface.
1558 * @dev: device to shutdown
1559 *
1560 * This function moves an active device into down state. A
1561 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1562 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1563 * chain.
1564 */
7051b88a 1565void dev_close(struct net_device *dev)
bd380811 1566{
e14a5993
ED
1567 if (dev->flags & IFF_UP) {
1568 LIST_HEAD(single);
1da177e4 1569
5cde2829 1570 list_add(&dev->close_list, &single);
99c4a26a 1571 dev_close_many(&single, true);
e14a5993
ED
1572 list_del(&single);
1573 }
1da177e4 1574}
d1b19dff 1575EXPORT_SYMBOL(dev_close);
1da177e4
LT
1576
1577
0187bdfb
BH
1578/**
1579 * dev_disable_lro - disable Large Receive Offload on a device
1580 * @dev: device
1581 *
1582 * Disable Large Receive Offload (LRO) on a net device. Must be
1583 * called under RTNL. This is needed if received packets may be
1584 * forwarded to another interface.
1585 */
1586void dev_disable_lro(struct net_device *dev)
1587{
fbe168ba
MK
1588 struct net_device *lower_dev;
1589 struct list_head *iter;
529d0489 1590
bc5787c6
MM
1591 dev->wanted_features &= ~NETIF_F_LRO;
1592 netdev_update_features(dev);
27660515 1593
22d5969f
MM
1594 if (unlikely(dev->features & NETIF_F_LRO))
1595 netdev_WARN(dev, "failed to disable LRO!\n");
fbe168ba
MK
1596
1597 netdev_for_each_lower_dev(dev, lower_dev, iter)
1598 dev_disable_lro(lower_dev);
0187bdfb
BH
1599}
1600EXPORT_SYMBOL(dev_disable_lro);
1601
56f5aa77
MC
1602/**
1603 * dev_disable_gro_hw - disable HW Generic Receive Offload on a device
1604 * @dev: device
1605 *
1606 * Disable HW Generic Receive Offload (GRO_HW) on a net device. Must be
1607 * called under RTNL. This is needed if Generic XDP is installed on
1608 * the device.
1609 */
1610static void dev_disable_gro_hw(struct net_device *dev)
1611{
1612 dev->wanted_features &= ~NETIF_F_GRO_HW;
1613 netdev_update_features(dev);
1614
1615 if (unlikely(dev->features & NETIF_F_GRO_HW))
1616 netdev_WARN(dev, "failed to disable GRO_HW!\n");
1617}
1618
ede2762d
KT
1619const char *netdev_cmd_to_name(enum netdev_cmd cmd)
1620{
1621#define N(val) \
1622 case NETDEV_##val: \
1623 return "NETDEV_" __stringify(val);
1624 switch (cmd) {
1625 N(UP) N(DOWN) N(REBOOT) N(CHANGE) N(REGISTER) N(UNREGISTER)
1626 N(CHANGEMTU) N(CHANGEADDR) N(GOING_DOWN) N(CHANGENAME) N(FEAT_CHANGE)
1627 N(BONDING_FAILOVER) N(PRE_UP) N(PRE_TYPE_CHANGE) N(POST_TYPE_CHANGE)
1628 N(POST_INIT) N(RELEASE) N(NOTIFY_PEERS) N(JOIN) N(CHANGEUPPER)
1629 N(RESEND_IGMP) N(PRECHANGEMTU) N(CHANGEINFODATA) N(BONDING_INFO)
1630 N(PRECHANGEUPPER) N(CHANGELOWERSTATE) N(UDP_TUNNEL_PUSH_INFO)
1631 N(UDP_TUNNEL_DROP_INFO) N(CHANGE_TX_QUEUE_LEN)
9daae9bd
GP
1632 N(CVLAN_FILTER_PUSH_INFO) N(CVLAN_FILTER_DROP_INFO)
1633 N(SVLAN_FILTER_PUSH_INFO) N(SVLAN_FILTER_DROP_INFO)
1570415f 1634 N(PRE_CHANGEADDR)
3f5ecd8a 1635 }
ede2762d
KT
1636#undef N
1637 return "UNKNOWN_NETDEV_EVENT";
1638}
1639EXPORT_SYMBOL_GPL(netdev_cmd_to_name);
1640
351638e7
JP
1641static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1642 struct net_device *dev)
1643{
51d0c047
DA
1644 struct netdev_notifier_info info = {
1645 .dev = dev,
1646 };
351638e7 1647
351638e7
JP
1648 return nb->notifier_call(nb, val, &info);
1649}
0187bdfb 1650
afa0df59
JP
1651static int call_netdevice_register_notifiers(struct notifier_block *nb,
1652 struct net_device *dev)
1653{
1654 int err;
1655
1656 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
1657 err = notifier_to_errno(err);
1658 if (err)
1659 return err;
1660
1661 if (!(dev->flags & IFF_UP))
1662 return 0;
1663
1664 call_netdevice_notifier(nb, NETDEV_UP, dev);
1665 return 0;
1666}
1667
1668static void call_netdevice_unregister_notifiers(struct notifier_block *nb,
1669 struct net_device *dev)
1670{
1671 if (dev->flags & IFF_UP) {
1672 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1673 dev);
1674 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1675 }
1676 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1677}
1678
1679static int call_netdevice_register_net_notifiers(struct notifier_block *nb,
1680 struct net *net)
1681{
1682 struct net_device *dev;
1683 int err;
1684
1685 for_each_netdev(net, dev) {
1686 err = call_netdevice_register_notifiers(nb, dev);
1687 if (err)
1688 goto rollback;
1689 }
1690 return 0;
1691
1692rollback:
1693 for_each_netdev_continue_reverse(net, dev)
1694 call_netdevice_unregister_notifiers(nb, dev);
1695 return err;
1696}
1697
1698static void call_netdevice_unregister_net_notifiers(struct notifier_block *nb,
1699 struct net *net)
1700{
1701 struct net_device *dev;
1702
1703 for_each_netdev(net, dev)
1704 call_netdevice_unregister_notifiers(nb, dev);
1705}
1706
881d966b
EB
1707static int dev_boot_phase = 1;
1708
1da177e4 1709/**
722c9a0c 1710 * register_netdevice_notifier - register a network notifier block
1711 * @nb: notifier
1da177e4 1712 *
722c9a0c 1713 * Register a notifier to be called when network device events occur.
1714 * The notifier passed is linked into the kernel structures and must
1715 * not be reused until it has been unregistered. A negative errno code
1716 * is returned on a failure.
1da177e4 1717 *
722c9a0c 1718 * When registered all registration and up events are replayed
1719 * to the new notifier to allow device to have a race free
1720 * view of the network device list.
1da177e4
LT
1721 */
1722
1723int register_netdevice_notifier(struct notifier_block *nb)
1724{
881d966b 1725 struct net *net;
1da177e4
LT
1726 int err;
1727
328fbe74
KT
1728 /* Close race with setup_net() and cleanup_net() */
1729 down_write(&pernet_ops_rwsem);
1da177e4 1730 rtnl_lock();
f07d5b94 1731 err = raw_notifier_chain_register(&netdev_chain, nb);
fcc5a03a
HX
1732 if (err)
1733 goto unlock;
881d966b
EB
1734 if (dev_boot_phase)
1735 goto unlock;
1736 for_each_net(net) {
afa0df59
JP
1737 err = call_netdevice_register_net_notifiers(nb, net);
1738 if (err)
1739 goto rollback;
1da177e4 1740 }
fcc5a03a
HX
1741
1742unlock:
1da177e4 1743 rtnl_unlock();
328fbe74 1744 up_write(&pernet_ops_rwsem);
1da177e4 1745 return err;
fcc5a03a
HX
1746
1747rollback:
afa0df59
JP
1748 for_each_net_continue_reverse(net)
1749 call_netdevice_unregister_net_notifiers(nb, net);
c67625a1
PE
1750
1751 raw_notifier_chain_unregister(&netdev_chain, nb);
fcc5a03a 1752 goto unlock;
1da177e4 1753}
d1b19dff 1754EXPORT_SYMBOL(register_netdevice_notifier);
1da177e4
LT
1755
1756/**
722c9a0c 1757 * unregister_netdevice_notifier - unregister a network notifier block
1758 * @nb: notifier
1da177e4 1759 *
722c9a0c 1760 * Unregister a notifier previously registered by
1761 * register_netdevice_notifier(). The notifier is unlinked into the
1762 * kernel structures and may then be reused. A negative errno code
1763 * is returned on a failure.
7d3d43da 1764 *
722c9a0c 1765 * After unregistering unregister and down device events are synthesized
1766 * for all devices on the device list to the removed notifier to remove
1767 * the need for special case cleanup code.
1da177e4
LT
1768 */
1769
1770int unregister_netdevice_notifier(struct notifier_block *nb)
1771{
7d3d43da 1772 struct net *net;
9f514950
HX
1773 int err;
1774
328fbe74
KT
1775 /* Close race with setup_net() and cleanup_net() */
1776 down_write(&pernet_ops_rwsem);
9f514950 1777 rtnl_lock();
f07d5b94 1778 err = raw_notifier_chain_unregister(&netdev_chain, nb);
7d3d43da
EB
1779 if (err)
1780 goto unlock;
1781
48b3a137
JP
1782 for_each_net(net)
1783 call_netdevice_unregister_net_notifiers(nb, net);
1784
7d3d43da 1785unlock:
9f514950 1786 rtnl_unlock();
328fbe74 1787 up_write(&pernet_ops_rwsem);
9f514950 1788 return err;
1da177e4 1789}
d1b19dff 1790EXPORT_SYMBOL(unregister_netdevice_notifier);
1da177e4 1791
1f637703
JP
1792static int __register_netdevice_notifier_net(struct net *net,
1793 struct notifier_block *nb,
1794 bool ignore_call_fail)
1795{
1796 int err;
1797
1798 err = raw_notifier_chain_register(&net->netdev_chain, nb);
1799 if (err)
1800 return err;
1801 if (dev_boot_phase)
1802 return 0;
1803
1804 err = call_netdevice_register_net_notifiers(nb, net);
1805 if (err && !ignore_call_fail)
1806 goto chain_unregister;
1807
1808 return 0;
1809
1810chain_unregister:
1811 raw_notifier_chain_unregister(&net->netdev_chain, nb);
1812 return err;
1813}
1814
1815static int __unregister_netdevice_notifier_net(struct net *net,
1816 struct notifier_block *nb)
1817{
1818 int err;
1819
1820 err = raw_notifier_chain_unregister(&net->netdev_chain, nb);
1821 if (err)
1822 return err;
1823
1824 call_netdevice_unregister_net_notifiers(nb, net);
1825 return 0;
1826}
1827
a30c7b42
JP
1828/**
1829 * register_netdevice_notifier_net - register a per-netns network notifier block
1830 * @net: network namespace
1831 * @nb: notifier
1832 *
1833 * Register a notifier to be called when network device events occur.
1834 * The notifier passed is linked into the kernel structures and must
1835 * not be reused until it has been unregistered. A negative errno code
1836 * is returned on a failure.
1837 *
1838 * When registered all registration and up events are replayed
1839 * to the new notifier to allow device to have a race free
1840 * view of the network device list.
1841 */
1842
1843int register_netdevice_notifier_net(struct net *net, struct notifier_block *nb)
1844{
1845 int err;
1846
1847 rtnl_lock();
1f637703 1848 err = __register_netdevice_notifier_net(net, nb, false);
a30c7b42
JP
1849 rtnl_unlock();
1850 return err;
a30c7b42
JP
1851}
1852EXPORT_SYMBOL(register_netdevice_notifier_net);
1853
1854/**
1855 * unregister_netdevice_notifier_net - unregister a per-netns
1856 * network notifier block
1857 * @net: network namespace
1858 * @nb: notifier
1859 *
1860 * Unregister a notifier previously registered by
1861 * register_netdevice_notifier(). The notifier is unlinked into the
1862 * kernel structures and may then be reused. A negative errno code
1863 * is returned on a failure.
1864 *
1865 * After unregistering unregister and down device events are synthesized
1866 * for all devices on the device list to the removed notifier to remove
1867 * the need for special case cleanup code.
1868 */
1869
1870int unregister_netdevice_notifier_net(struct net *net,
1871 struct notifier_block *nb)
1872{
1873 int err;
1874
1875 rtnl_lock();
1f637703 1876 err = __unregister_netdevice_notifier_net(net, nb);
a30c7b42
JP
1877 rtnl_unlock();
1878 return err;
1879}
1880EXPORT_SYMBOL(unregister_netdevice_notifier_net);
a30c7b42 1881
93642e14
JP
1882int register_netdevice_notifier_dev_net(struct net_device *dev,
1883 struct notifier_block *nb,
1884 struct netdev_net_notifier *nn)
1885{
1886 int err;
a30c7b42 1887
93642e14
JP
1888 rtnl_lock();
1889 err = __register_netdevice_notifier_net(dev_net(dev), nb, false);
1890 if (!err) {
1891 nn->nb = nb;
1892 list_add(&nn->list, &dev->net_notifier_list);
1893 }
a30c7b42
JP
1894 rtnl_unlock();
1895 return err;
1896}
93642e14
JP
1897EXPORT_SYMBOL(register_netdevice_notifier_dev_net);
1898
1899int unregister_netdevice_notifier_dev_net(struct net_device *dev,
1900 struct notifier_block *nb,
1901 struct netdev_net_notifier *nn)
1902{
1903 int err;
1904
1905 rtnl_lock();
1906 list_del(&nn->list);
1907 err = __unregister_netdevice_notifier_net(dev_net(dev), nb);
1908 rtnl_unlock();
1909 return err;
1910}
1911EXPORT_SYMBOL(unregister_netdevice_notifier_dev_net);
1912
1913static void move_netdevice_notifiers_dev_net(struct net_device *dev,
1914 struct net *net)
1915{
1916 struct netdev_net_notifier *nn;
1917
1918 list_for_each_entry(nn, &dev->net_notifier_list, list) {
1919 __unregister_netdevice_notifier_net(dev_net(dev), nn->nb);
1920 __register_netdevice_notifier_net(net, nn->nb, true);
1921 }
1922}
a30c7b42 1923
351638e7
JP
1924/**
1925 * call_netdevice_notifiers_info - call all network notifier blocks
1926 * @val: value passed unmodified to notifier function
351638e7
JP
1927 * @info: notifier information data
1928 *
1929 * Call all network notifier blocks. Parameters and return value
1930 * are as for raw_notifier_call_chain().
1931 */
1932
1d143d9f 1933static int call_netdevice_notifiers_info(unsigned long val,
1d143d9f 1934 struct netdev_notifier_info *info)
351638e7 1935{
a30c7b42
JP
1936 struct net *net = dev_net(info->dev);
1937 int ret;
1938
351638e7 1939 ASSERT_RTNL();
a30c7b42
JP
1940
1941 /* Run per-netns notifier block chain first, then run the global one.
1942 * Hopefully, one day, the global one is going to be removed after
1943 * all notifier block registrators get converted to be per-netns.
1944 */
1945 ret = raw_notifier_call_chain(&net->netdev_chain, val, info);
1946 if (ret & NOTIFY_STOP_MASK)
1947 return ret;
351638e7
JP
1948 return raw_notifier_call_chain(&netdev_chain, val, info);
1949}
351638e7 1950
26372605
PM
1951static int call_netdevice_notifiers_extack(unsigned long val,
1952 struct net_device *dev,
1953 struct netlink_ext_ack *extack)
1954{
1955 struct netdev_notifier_info info = {
1956 .dev = dev,
1957 .extack = extack,
1958 };
1959
1960 return call_netdevice_notifiers_info(val, &info);
1961}
1962
1da177e4
LT
1963/**
1964 * call_netdevice_notifiers - call all network notifier blocks
1965 * @val: value passed unmodified to notifier function
c4ea43c5 1966 * @dev: net_device pointer passed unmodified to notifier function
1da177e4
LT
1967 *
1968 * Call all network notifier blocks. Parameters and return value
f07d5b94 1969 * are as for raw_notifier_call_chain().
1da177e4
LT
1970 */
1971
ad7379d4 1972int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1da177e4 1973{
26372605 1974 return call_netdevice_notifiers_extack(val, dev, NULL);
1da177e4 1975}
edf947f1 1976EXPORT_SYMBOL(call_netdevice_notifiers);
1da177e4 1977
af7d6cce
SD
1978/**
1979 * call_netdevice_notifiers_mtu - call all network notifier blocks
1980 * @val: value passed unmodified to notifier function
1981 * @dev: net_device pointer passed unmodified to notifier function
1982 * @arg: additional u32 argument passed to the notifier function
1983 *
1984 * Call all network notifier blocks. Parameters and return value
1985 * are as for raw_notifier_call_chain().
1986 */
1987static int call_netdevice_notifiers_mtu(unsigned long val,
1988 struct net_device *dev, u32 arg)
1989{
1990 struct netdev_notifier_info_ext info = {
1991 .info.dev = dev,
1992 .ext.mtu = arg,
1993 };
1994
1995 BUILD_BUG_ON(offsetof(struct netdev_notifier_info_ext, info) != 0);
1996
1997 return call_netdevice_notifiers_info(val, &info.info);
1998}
1999
1cf51900 2000#ifdef CONFIG_NET_INGRESS
aabf6772 2001static DEFINE_STATIC_KEY_FALSE(ingress_needed_key);
4577139b
DB
2002
2003void net_inc_ingress_queue(void)
2004{
aabf6772 2005 static_branch_inc(&ingress_needed_key);
4577139b
DB
2006}
2007EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
2008
2009void net_dec_ingress_queue(void)
2010{
aabf6772 2011 static_branch_dec(&ingress_needed_key);
4577139b
DB
2012}
2013EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
2014#endif
2015
1f211a1b 2016#ifdef CONFIG_NET_EGRESS
aabf6772 2017static DEFINE_STATIC_KEY_FALSE(egress_needed_key);
1f211a1b
DB
2018
2019void net_inc_egress_queue(void)
2020{
aabf6772 2021 static_branch_inc(&egress_needed_key);
1f211a1b
DB
2022}
2023EXPORT_SYMBOL_GPL(net_inc_egress_queue);
2024
2025void net_dec_egress_queue(void)
2026{
aabf6772 2027 static_branch_dec(&egress_needed_key);
1f211a1b
DB
2028}
2029EXPORT_SYMBOL_GPL(net_dec_egress_queue);
2030#endif
2031
39e83922 2032static DEFINE_STATIC_KEY_FALSE(netstamp_needed_key);
e9666d10 2033#ifdef CONFIG_JUMP_LABEL
b90e5794 2034static atomic_t netstamp_needed_deferred;
13baa00a 2035static atomic_t netstamp_wanted;
5fa8bbda 2036static void netstamp_clear(struct work_struct *work)
1da177e4 2037{
b90e5794 2038 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
13baa00a 2039 int wanted;
b90e5794 2040
13baa00a
ED
2041 wanted = atomic_add_return(deferred, &netstamp_wanted);
2042 if (wanted > 0)
39e83922 2043 static_branch_enable(&netstamp_needed_key);
13baa00a 2044 else
39e83922 2045 static_branch_disable(&netstamp_needed_key);
5fa8bbda
ED
2046}
2047static DECLARE_WORK(netstamp_work, netstamp_clear);
b90e5794 2048#endif
5fa8bbda
ED
2049
2050void net_enable_timestamp(void)
2051{
e9666d10 2052#ifdef CONFIG_JUMP_LABEL
13baa00a
ED
2053 int wanted;
2054
2055 while (1) {
2056 wanted = atomic_read(&netstamp_wanted);
2057 if (wanted <= 0)
2058 break;
2059 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
2060 return;
2061 }
2062 atomic_inc(&netstamp_needed_deferred);
2063 schedule_work(&netstamp_work);
2064#else
39e83922 2065 static_branch_inc(&netstamp_needed_key);
13baa00a 2066#endif
1da177e4 2067}
d1b19dff 2068EXPORT_SYMBOL(net_enable_timestamp);
1da177e4
LT
2069
2070void net_disable_timestamp(void)
2071{
e9666d10 2072#ifdef CONFIG_JUMP_LABEL
13baa00a
ED
2073 int wanted;
2074
2075 while (1) {
2076 wanted = atomic_read(&netstamp_wanted);
2077 if (wanted <= 1)
2078 break;
2079 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
2080 return;
2081 }
2082 atomic_dec(&netstamp_needed_deferred);
5fa8bbda
ED
2083 schedule_work(&netstamp_work);
2084#else
39e83922 2085 static_branch_dec(&netstamp_needed_key);
5fa8bbda 2086#endif
1da177e4 2087}
d1b19dff 2088EXPORT_SYMBOL(net_disable_timestamp);
1da177e4 2089
3b098e2d 2090static inline void net_timestamp_set(struct sk_buff *skb)
1da177e4 2091{
2456e855 2092 skb->tstamp = 0;
39e83922 2093 if (static_branch_unlikely(&netstamp_needed_key))
a61bbcf2 2094 __net_timestamp(skb);
1da177e4
LT
2095}
2096
39e83922
DB
2097#define net_timestamp_check(COND, SKB) \
2098 if (static_branch_unlikely(&netstamp_needed_key)) { \
2099 if ((COND) && !(SKB)->tstamp) \
2100 __net_timestamp(SKB); \
2101 } \
3b098e2d 2102
f4b05d27 2103bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
79b569f0
DL
2104{
2105 unsigned int len;
2106
2107 if (!(dev->flags & IFF_UP))
2108 return false;
2109
2110 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
2111 if (skb->len <= len)
2112 return true;
2113
2114 /* if TSO is enabled, we don't care about the length as the packet
2115 * could be forwarded without being segmented before
2116 */
2117 if (skb_is_gso(skb))
2118 return true;
2119
2120 return false;
2121}
1ee481fb 2122EXPORT_SYMBOL_GPL(is_skb_forwardable);
79b569f0 2123
a0265d28
HX
2124int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2125{
4e3264d2 2126 int ret = ____dev_forward_skb(dev, skb);
a0265d28 2127
4e3264d2
MKL
2128 if (likely(!ret)) {
2129 skb->protocol = eth_type_trans(skb, dev);
2130 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
2131 }
a0265d28 2132
4e3264d2 2133 return ret;
a0265d28
HX
2134}
2135EXPORT_SYMBOL_GPL(__dev_forward_skb);
2136
44540960
AB
2137/**
2138 * dev_forward_skb - loopback an skb to another netif
2139 *
2140 * @dev: destination network device
2141 * @skb: buffer to forward
2142 *
2143 * return values:
2144 * NET_RX_SUCCESS (no congestion)
6ec82562 2145 * NET_RX_DROP (packet was dropped, but freed)
44540960
AB
2146 *
2147 * dev_forward_skb can be used for injecting an skb from the
2148 * start_xmit function of one device into the receive queue
2149 * of another device.
2150 *
2151 * The receiving device may be in another namespace, so
2152 * we have to clear all information in the skb that could
2153 * impact namespace isolation.
2154 */
2155int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
2156{
a0265d28 2157 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
44540960
AB
2158}
2159EXPORT_SYMBOL_GPL(dev_forward_skb);
2160
71d9dec2
CG
2161static inline int deliver_skb(struct sk_buff *skb,
2162 struct packet_type *pt_prev,
2163 struct net_device *orig_dev)
2164{
1f8b977a 2165 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
1080e512 2166 return -ENOMEM;
63354797 2167 refcount_inc(&skb->users);
71d9dec2
CG
2168 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
2169}
2170
7866a621
SN
2171static inline void deliver_ptype_list_skb(struct sk_buff *skb,
2172 struct packet_type **pt,
fbcb2170
JP
2173 struct net_device *orig_dev,
2174 __be16 type,
7866a621
SN
2175 struct list_head *ptype_list)
2176{
2177 struct packet_type *ptype, *pt_prev = *pt;
2178
2179 list_for_each_entry_rcu(ptype, ptype_list, list) {
2180 if (ptype->type != type)
2181 continue;
2182 if (pt_prev)
fbcb2170 2183 deliver_skb(skb, pt_prev, orig_dev);
7866a621
SN
2184 pt_prev = ptype;
2185 }
2186 *pt = pt_prev;
2187}
2188
c0de08d0
EL
2189static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
2190{
a3d744e9 2191 if (!ptype->af_packet_priv || !skb->sk)
c0de08d0
EL
2192 return false;
2193
2194 if (ptype->id_match)
2195 return ptype->id_match(ptype, skb->sk);
2196 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
2197 return true;
2198
2199 return false;
2200}
2201
9f9a742d
MR
2202/**
2203 * dev_nit_active - return true if any network interface taps are in use
2204 *
2205 * @dev: network device to check for the presence of taps
2206 */
2207bool dev_nit_active(struct net_device *dev)
2208{
2209 return !list_empty(&ptype_all) || !list_empty(&dev->ptype_all);
2210}
2211EXPORT_SYMBOL_GPL(dev_nit_active);
2212
1da177e4
LT
2213/*
2214 * Support routine. Sends outgoing frames to any network
2215 * taps currently in use.
2216 */
2217
74b20582 2218void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1da177e4
LT
2219{
2220 struct packet_type *ptype;
71d9dec2
CG
2221 struct sk_buff *skb2 = NULL;
2222 struct packet_type *pt_prev = NULL;
7866a621 2223 struct list_head *ptype_list = &ptype_all;
a61bbcf2 2224
1da177e4 2225 rcu_read_lock();
7866a621
SN
2226again:
2227 list_for_each_entry_rcu(ptype, ptype_list, list) {
fa788d98
VW
2228 if (ptype->ignore_outgoing)
2229 continue;
2230
1da177e4
LT
2231 /* Never send packets back to the socket
2232 * they originated from - MvS (miquels@drinkel.ow.org)
2233 */
7866a621
SN
2234 if (skb_loop_sk(ptype, skb))
2235 continue;
71d9dec2 2236
7866a621
SN
2237 if (pt_prev) {
2238 deliver_skb(skb2, pt_prev, skb->dev);
2239 pt_prev = ptype;
2240 continue;
2241 }
1da177e4 2242
7866a621
SN
2243 /* need to clone skb, done only once */
2244 skb2 = skb_clone(skb, GFP_ATOMIC);
2245 if (!skb2)
2246 goto out_unlock;
70978182 2247
7866a621 2248 net_timestamp_set(skb2);
1da177e4 2249
7866a621
SN
2250 /* skb->nh should be correctly
2251 * set by sender, so that the second statement is
2252 * just protection against buggy protocols.
2253 */
2254 skb_reset_mac_header(skb2);
2255
2256 if (skb_network_header(skb2) < skb2->data ||
2257 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
2258 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
2259 ntohs(skb2->protocol),
2260 dev->name);
2261 skb_reset_network_header(skb2);
1da177e4 2262 }
7866a621
SN
2263
2264 skb2->transport_header = skb2->network_header;
2265 skb2->pkt_type = PACKET_OUTGOING;
2266 pt_prev = ptype;
2267 }
2268
2269 if (ptype_list == &ptype_all) {
2270 ptype_list = &dev->ptype_all;
2271 goto again;
1da177e4 2272 }
7866a621 2273out_unlock:
581fe0ea
WB
2274 if (pt_prev) {
2275 if (!skb_orphan_frags_rx(skb2, GFP_ATOMIC))
2276 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
2277 else
2278 kfree_skb(skb2);
2279 }
1da177e4
LT
2280 rcu_read_unlock();
2281}
74b20582 2282EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
1da177e4 2283
2c53040f
BH
2284/**
2285 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
4f57c087
JF
2286 * @dev: Network device
2287 * @txq: number of queues available
2288 *
2289 * If real_num_tx_queues is changed the tc mappings may no longer be
2290 * valid. To resolve this verify the tc mapping remains valid and if
2291 * not NULL the mapping. With no priorities mapping to this
2292 * offset/count pair it will no longer be used. In the worst case TC0
2293 * is invalid nothing can be done so disable priority mappings. If is
2294 * expected that drivers will fix this mapping if they can before
2295 * calling netif_set_real_num_tx_queues.
2296 */
bb134d22 2297static void netif_setup_tc(struct net_device *dev, unsigned int txq)
4f57c087
JF
2298{
2299 int i;
2300 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2301
2302 /* If TC0 is invalidated disable TC mapping */
2303 if (tc->offset + tc->count > txq) {
7b6cd1ce 2304 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
4f57c087
JF
2305 dev->num_tc = 0;
2306 return;
2307 }
2308
2309 /* Invalidated prio to tc mappings set to TC0 */
2310 for (i = 1; i < TC_BITMASK + 1; i++) {
2311 int q = netdev_get_prio_tc_map(dev, i);
2312
2313 tc = &dev->tc_to_txq[q];
2314 if (tc->offset + tc->count > txq) {
7b6cd1ce
JP
2315 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
2316 i, q);
4f57c087
JF
2317 netdev_set_prio_tc_map(dev, i, 0);
2318 }
2319 }
2320}
2321
8d059b0f
AD
2322int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
2323{
2324 if (dev->num_tc) {
2325 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
2326 int i;
2327
ffcfe25b 2328 /* walk through the TCs and see if it falls into any of them */
8d059b0f
AD
2329 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
2330 if ((txq - tc->offset) < tc->count)
2331 return i;
2332 }
2333
ffcfe25b 2334 /* didn't find it, just return -1 to indicate no match */
8d059b0f
AD
2335 return -1;
2336 }
2337
2338 return 0;
2339}
8a5f2166 2340EXPORT_SYMBOL(netdev_txq_to_tc);
8d059b0f 2341
537c00de 2342#ifdef CONFIG_XPS
04157469
AN
2343struct static_key xps_needed __read_mostly;
2344EXPORT_SYMBOL(xps_needed);
2345struct static_key xps_rxqs_needed __read_mostly;
2346EXPORT_SYMBOL(xps_rxqs_needed);
537c00de
AD
2347static DEFINE_MUTEX(xps_map_mutex);
2348#define xmap_dereference(P) \
2349 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
2350
6234f874
AD
2351static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
2352 int tci, u16 index)
537c00de 2353{
10cdc3f3
AD
2354 struct xps_map *map = NULL;
2355 int pos;
537c00de 2356
10cdc3f3 2357 if (dev_maps)
80d19669 2358 map = xmap_dereference(dev_maps->attr_map[tci]);
6234f874
AD
2359 if (!map)
2360 return false;
537c00de 2361
6234f874
AD
2362 for (pos = map->len; pos--;) {
2363 if (map->queues[pos] != index)
2364 continue;
2365
2366 if (map->len > 1) {
2367 map->queues[pos] = map->queues[--map->len];
10cdc3f3 2368 break;
537c00de 2369 }
6234f874 2370
80d19669 2371 RCU_INIT_POINTER(dev_maps->attr_map[tci], NULL);
6234f874
AD
2372 kfree_rcu(map, rcu);
2373 return false;
537c00de
AD
2374 }
2375
6234f874 2376 return true;
10cdc3f3
AD
2377}
2378
6234f874
AD
2379static bool remove_xps_queue_cpu(struct net_device *dev,
2380 struct xps_dev_maps *dev_maps,
2381 int cpu, u16 offset, u16 count)
2382{
184c449f
AD
2383 int num_tc = dev->num_tc ? : 1;
2384 bool active = false;
2385 int tci;
6234f874 2386
184c449f
AD
2387 for (tci = cpu * num_tc; num_tc--; tci++) {
2388 int i, j;
2389
2390 for (i = count, j = offset; i--; j++) {
6358d49a 2391 if (!remove_xps_queue(dev_maps, tci, j))
184c449f
AD
2392 break;
2393 }
2394
2395 active |= i < 0;
6234f874
AD
2396 }
2397
184c449f 2398 return active;
6234f874
AD
2399}
2400
867d0ad4
SD
2401static void reset_xps_maps(struct net_device *dev,
2402 struct xps_dev_maps *dev_maps,
2403 bool is_rxqs_map)
2404{
2405 if (is_rxqs_map) {
2406 static_key_slow_dec_cpuslocked(&xps_rxqs_needed);
2407 RCU_INIT_POINTER(dev->xps_rxqs_map, NULL);
2408 } else {
2409 RCU_INIT_POINTER(dev->xps_cpus_map, NULL);
2410 }
2411 static_key_slow_dec_cpuslocked(&xps_needed);
2412 kfree_rcu(dev_maps, rcu);
2413}
2414
80d19669
AN
2415static void clean_xps_maps(struct net_device *dev, const unsigned long *mask,
2416 struct xps_dev_maps *dev_maps, unsigned int nr_ids,
2417 u16 offset, u16 count, bool is_rxqs_map)
2418{
2419 bool active = false;
2420 int i, j;
2421
2422 for (j = -1; j = netif_attrmask_next(j, mask, nr_ids),
2423 j < nr_ids;)
2424 active |= remove_xps_queue_cpu(dev, dev_maps, j, offset,
2425 count);
867d0ad4
SD
2426 if (!active)
2427 reset_xps_maps(dev, dev_maps, is_rxqs_map);
80d19669 2428
f28c020f
SD
2429 if (!is_rxqs_map) {
2430 for (i = offset + (count - 1); count--; i--) {
2431 netdev_queue_numa_node_write(
2432 netdev_get_tx_queue(dev, i),
2433 NUMA_NO_NODE);
80d19669 2434 }
80d19669
AN
2435 }
2436}
2437
6234f874
AD
2438static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2439 u16 count)
10cdc3f3 2440{
80d19669 2441 const unsigned long *possible_mask = NULL;
10cdc3f3 2442 struct xps_dev_maps *dev_maps;
80d19669 2443 unsigned int nr_ids;
10cdc3f3 2444
04157469
AN
2445 if (!static_key_false(&xps_needed))
2446 return;
10cdc3f3 2447
4d99f660 2448 cpus_read_lock();
04157469 2449 mutex_lock(&xps_map_mutex);
10cdc3f3 2450
04157469
AN
2451 if (static_key_false(&xps_rxqs_needed)) {
2452 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2453 if (dev_maps) {
2454 nr_ids = dev->num_rx_queues;
2455 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids,
2456 offset, count, true);
2457 }
537c00de
AD
2458 }
2459
80d19669
AN
2460 dev_maps = xmap_dereference(dev->xps_cpus_map);
2461 if (!dev_maps)
2462 goto out_no_maps;
2463
2464 if (num_possible_cpus() > 1)
2465 possible_mask = cpumask_bits(cpu_possible_mask);
2466 nr_ids = nr_cpu_ids;
2467 clean_xps_maps(dev, possible_mask, dev_maps, nr_ids, offset, count,
2468 false);
024e9679 2469
537c00de
AD
2470out_no_maps:
2471 mutex_unlock(&xps_map_mutex);
4d99f660 2472 cpus_read_unlock();
537c00de
AD
2473}
2474
6234f874
AD
2475static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2476{
2477 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2478}
2479
80d19669
AN
2480static struct xps_map *expand_xps_map(struct xps_map *map, int attr_index,
2481 u16 index, bool is_rxqs_map)
01c5f864
AD
2482{
2483 struct xps_map *new_map;
2484 int alloc_len = XPS_MIN_MAP_ALLOC;
2485 int i, pos;
2486
2487 for (pos = 0; map && pos < map->len; pos++) {
2488 if (map->queues[pos] != index)
2489 continue;
2490 return map;
2491 }
2492
80d19669 2493 /* Need to add tx-queue to this CPU's/rx-queue's existing map */
01c5f864
AD
2494 if (map) {
2495 if (pos < map->alloc_len)
2496 return map;
2497
2498 alloc_len = map->alloc_len * 2;
2499 }
2500
80d19669
AN
2501 /* Need to allocate new map to store tx-queue on this CPU's/rx-queue's
2502 * map
2503 */
2504 if (is_rxqs_map)
2505 new_map = kzalloc(XPS_MAP_SIZE(alloc_len), GFP_KERNEL);
2506 else
2507 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2508 cpu_to_node(attr_index));
01c5f864
AD
2509 if (!new_map)
2510 return NULL;
2511
2512 for (i = 0; i < pos; i++)
2513 new_map->queues[i] = map->queues[i];
2514 new_map->alloc_len = alloc_len;
2515 new_map->len = pos;
2516
2517 return new_map;
2518}
2519
4d99f660 2520/* Must be called under cpus_read_lock */
80d19669
AN
2521int __netif_set_xps_queue(struct net_device *dev, const unsigned long *mask,
2522 u16 index, bool is_rxqs_map)
537c00de 2523{
80d19669 2524 const unsigned long *online_mask = NULL, *possible_mask = NULL;
01c5f864 2525 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
80d19669 2526 int i, j, tci, numa_node_id = -2;
184c449f 2527 int maps_sz, num_tc = 1, tc = 0;
537c00de 2528 struct xps_map *map, *new_map;
01c5f864 2529 bool active = false;
80d19669 2530 unsigned int nr_ids;
537c00de 2531
184c449f 2532 if (dev->num_tc) {
ffcfe25b 2533 /* Do not allow XPS on subordinate device directly */
184c449f 2534 num_tc = dev->num_tc;
ffcfe25b
AD
2535 if (num_tc < 0)
2536 return -EINVAL;
2537
2538 /* If queue belongs to subordinate dev use its map */
2539 dev = netdev_get_tx_queue(dev, index)->sb_dev ? : dev;
2540
184c449f
AD
2541 tc = netdev_txq_to_tc(dev, index);
2542 if (tc < 0)
2543 return -EINVAL;
2544 }
2545
537c00de 2546 mutex_lock(&xps_map_mutex);
80d19669
AN
2547 if (is_rxqs_map) {
2548 maps_sz = XPS_RXQ_DEV_MAPS_SIZE(num_tc, dev->num_rx_queues);
2549 dev_maps = xmap_dereference(dev->xps_rxqs_map);
2550 nr_ids = dev->num_rx_queues;
2551 } else {
2552 maps_sz = XPS_CPU_DEV_MAPS_SIZE(num_tc);
2553 if (num_possible_cpus() > 1) {
2554 online_mask = cpumask_bits(cpu_online_mask);
2555 possible_mask = cpumask_bits(cpu_possible_mask);
2556 }
2557 dev_maps = xmap_dereference(dev->xps_cpus_map);
2558 nr_ids = nr_cpu_ids;
2559 }
537c00de 2560
80d19669
AN
2561 if (maps_sz < L1_CACHE_BYTES)
2562 maps_sz = L1_CACHE_BYTES;
537c00de 2563
01c5f864 2564 /* allocate memory for queue storage */
80d19669
AN
2565 for (j = -1; j = netif_attrmask_next_and(j, online_mask, mask, nr_ids),
2566 j < nr_ids;) {
01c5f864
AD
2567 if (!new_dev_maps)
2568 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
2bb60cb9
AD
2569 if (!new_dev_maps) {
2570 mutex_unlock(&xps_map_mutex);
01c5f864 2571 return -ENOMEM;
2bb60cb9 2572 }
01c5f864 2573
80d19669
AN
2574 tci = j * num_tc + tc;
2575 map = dev_maps ? xmap_dereference(dev_maps->attr_map[tci]) :
01c5f864
AD
2576 NULL;
2577
80d19669 2578 map = expand_xps_map(map, j, index, is_rxqs_map);
01c5f864
AD
2579 if (!map)
2580 goto error;
2581
80d19669 2582 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
01c5f864
AD
2583 }
2584
2585 if (!new_dev_maps)
2586 goto out_no_new_maps;
2587
867d0ad4
SD
2588 if (!dev_maps) {
2589 /* Increment static keys at most once per type */
2590 static_key_slow_inc_cpuslocked(&xps_needed);
2591 if (is_rxqs_map)
2592 static_key_slow_inc_cpuslocked(&xps_rxqs_needed);
2593 }
04157469 2594
80d19669
AN
2595 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2596 j < nr_ids;) {
184c449f 2597 /* copy maps belonging to foreign traffic classes */
80d19669 2598 for (i = tc, tci = j * num_tc; dev_maps && i--; tci++) {
184c449f 2599 /* fill in the new device map from the old device map */
80d19669
AN
2600 map = xmap_dereference(dev_maps->attr_map[tci]);
2601 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
184c449f
AD
2602 }
2603
2604 /* We need to explicitly update tci as prevous loop
2605 * could break out early if dev_maps is NULL.
2606 */
80d19669 2607 tci = j * num_tc + tc;
184c449f 2608
80d19669
AN
2609 if (netif_attr_test_mask(j, mask, nr_ids) &&
2610 netif_attr_test_online(j, online_mask, nr_ids)) {
2611 /* add tx-queue to CPU/rx-queue maps */
01c5f864
AD
2612 int pos = 0;
2613
80d19669 2614 map = xmap_dereference(new_dev_maps->attr_map[tci]);
01c5f864
AD
2615 while ((pos < map->len) && (map->queues[pos] != index))
2616 pos++;
2617
2618 if (pos == map->len)
2619 map->queues[map->len++] = index;
537c00de 2620#ifdef CONFIG_NUMA
80d19669
AN
2621 if (!is_rxqs_map) {
2622 if (numa_node_id == -2)
2623 numa_node_id = cpu_to_node(j);
2624 else if (numa_node_id != cpu_to_node(j))
2625 numa_node_id = -1;
2626 }
537c00de 2627#endif
01c5f864
AD
2628 } else if (dev_maps) {
2629 /* fill in the new device map from the old device map */
80d19669
AN
2630 map = xmap_dereference(dev_maps->attr_map[tci]);
2631 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
537c00de 2632 }
01c5f864 2633
184c449f
AD
2634 /* copy maps belonging to foreign traffic classes */
2635 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
2636 /* fill in the new device map from the old device map */
80d19669
AN
2637 map = xmap_dereference(dev_maps->attr_map[tci]);
2638 RCU_INIT_POINTER(new_dev_maps->attr_map[tci], map);
184c449f 2639 }
537c00de
AD
2640 }
2641
80d19669
AN
2642 if (is_rxqs_map)
2643 rcu_assign_pointer(dev->xps_rxqs_map, new_dev_maps);
2644 else
2645 rcu_assign_pointer(dev->xps_cpus_map, new_dev_maps);
01c5f864 2646
537c00de 2647 /* Cleanup old maps */
184c449f
AD
2648 if (!dev_maps)
2649 goto out_no_old_maps;
2650
80d19669
AN
2651 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2652 j < nr_ids;) {
2653 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2654 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
2655 map = xmap_dereference(dev_maps->attr_map[tci]);
01c5f864
AD
2656 if (map && map != new_map)
2657 kfree_rcu(map, rcu);
2658 }
537c00de
AD
2659 }
2660
184c449f
AD
2661 kfree_rcu(dev_maps, rcu);
2662
2663out_no_old_maps:
01c5f864
AD
2664 dev_maps = new_dev_maps;
2665 active = true;
537c00de 2666
01c5f864 2667out_no_new_maps:
80d19669
AN
2668 if (!is_rxqs_map) {
2669 /* update Tx queue numa node */
2670 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2671 (numa_node_id >= 0) ?
2672 numa_node_id : NUMA_NO_NODE);
2673 }
537c00de 2674
01c5f864
AD
2675 if (!dev_maps)
2676 goto out_no_maps;
2677
80d19669
AN
2678 /* removes tx-queue from unused CPUs/rx-queues */
2679 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2680 j < nr_ids;) {
2681 for (i = tc, tci = j * num_tc; i--; tci++)
184c449f 2682 active |= remove_xps_queue(dev_maps, tci, index);
80d19669
AN
2683 if (!netif_attr_test_mask(j, mask, nr_ids) ||
2684 !netif_attr_test_online(j, online_mask, nr_ids))
184c449f
AD
2685 active |= remove_xps_queue(dev_maps, tci, index);
2686 for (i = num_tc - tc, tci++; --i; tci++)
2687 active |= remove_xps_queue(dev_maps, tci, index);
01c5f864
AD
2688 }
2689
2690 /* free map if not active */
867d0ad4
SD
2691 if (!active)
2692 reset_xps_maps(dev, dev_maps, is_rxqs_map);
01c5f864
AD
2693
2694out_no_maps:
537c00de
AD
2695 mutex_unlock(&xps_map_mutex);
2696
2697 return 0;
2698error:
01c5f864 2699 /* remove any maps that we added */
80d19669
AN
2700 for (j = -1; j = netif_attrmask_next(j, possible_mask, nr_ids),
2701 j < nr_ids;) {
2702 for (i = num_tc, tci = j * num_tc; i--; tci++) {
2703 new_map = xmap_dereference(new_dev_maps->attr_map[tci]);
184c449f 2704 map = dev_maps ?
80d19669 2705 xmap_dereference(dev_maps->attr_map[tci]) :
184c449f
AD
2706 NULL;
2707 if (new_map && new_map != map)
2708 kfree(new_map);
2709 }
01c5f864
AD
2710 }
2711
537c00de
AD
2712 mutex_unlock(&xps_map_mutex);
2713
537c00de
AD
2714 kfree(new_dev_maps);
2715 return -ENOMEM;
2716}
4d99f660 2717EXPORT_SYMBOL_GPL(__netif_set_xps_queue);
80d19669
AN
2718
2719int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2720 u16 index)
2721{
4d99f660
AV
2722 int ret;
2723
2724 cpus_read_lock();
2725 ret = __netif_set_xps_queue(dev, cpumask_bits(mask), index, false);
2726 cpus_read_unlock();
2727
2728 return ret;
80d19669 2729}
537c00de
AD
2730EXPORT_SYMBOL(netif_set_xps_queue);
2731
2732#endif
ffcfe25b
AD
2733static void netdev_unbind_all_sb_channels(struct net_device *dev)
2734{
2735 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2736
2737 /* Unbind any subordinate channels */
2738 while (txq-- != &dev->_tx[0]) {
2739 if (txq->sb_dev)
2740 netdev_unbind_sb_channel(dev, txq->sb_dev);
2741 }
2742}
2743
9cf1f6a8
AD
2744void netdev_reset_tc(struct net_device *dev)
2745{
6234f874
AD
2746#ifdef CONFIG_XPS
2747 netif_reset_xps_queues_gt(dev, 0);
2748#endif
ffcfe25b
AD
2749 netdev_unbind_all_sb_channels(dev);
2750
2751 /* Reset TC configuration of device */
9cf1f6a8
AD
2752 dev->num_tc = 0;
2753 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2754 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2755}
2756EXPORT_SYMBOL(netdev_reset_tc);
2757
2758int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2759{
2760 if (tc >= dev->num_tc)
2761 return -EINVAL;
2762
6234f874
AD
2763#ifdef CONFIG_XPS
2764 netif_reset_xps_queues(dev, offset, count);
2765#endif
9cf1f6a8
AD
2766 dev->tc_to_txq[tc].count = count;
2767 dev->tc_to_txq[tc].offset = offset;
2768 return 0;
2769}
2770EXPORT_SYMBOL(netdev_set_tc_queue);
2771
2772int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2773{
2774 if (num_tc > TC_MAX_QUEUE)
2775 return -EINVAL;
2776
6234f874
AD
2777#ifdef CONFIG_XPS
2778 netif_reset_xps_queues_gt(dev, 0);
2779#endif
ffcfe25b
AD
2780 netdev_unbind_all_sb_channels(dev);
2781
9cf1f6a8
AD
2782 dev->num_tc = num_tc;
2783 return 0;
2784}
2785EXPORT_SYMBOL(netdev_set_num_tc);
2786
ffcfe25b
AD
2787void netdev_unbind_sb_channel(struct net_device *dev,
2788 struct net_device *sb_dev)
2789{
2790 struct netdev_queue *txq = &dev->_tx[dev->num_tx_queues];
2791
2792#ifdef CONFIG_XPS
2793 netif_reset_xps_queues_gt(sb_dev, 0);
2794#endif
2795 memset(sb_dev->tc_to_txq, 0, sizeof(sb_dev->tc_to_txq));
2796 memset(sb_dev->prio_tc_map, 0, sizeof(sb_dev->prio_tc_map));
2797
2798 while (txq-- != &dev->_tx[0]) {
2799 if (txq->sb_dev == sb_dev)
2800 txq->sb_dev = NULL;
2801 }
2802}
2803EXPORT_SYMBOL(netdev_unbind_sb_channel);
2804
2805int netdev_bind_sb_channel_queue(struct net_device *dev,
2806 struct net_device *sb_dev,
2807 u8 tc, u16 count, u16 offset)
2808{
2809 /* Make certain the sb_dev and dev are already configured */
2810 if (sb_dev->num_tc >= 0 || tc >= dev->num_tc)
2811 return -EINVAL;
2812
2813 /* We cannot hand out queues we don't have */
2814 if ((offset + count) > dev->real_num_tx_queues)
2815 return -EINVAL;
2816
2817 /* Record the mapping */
2818 sb_dev->tc_to_txq[tc].count = count;
2819 sb_dev->tc_to_txq[tc].offset = offset;
2820
2821 /* Provide a way for Tx queue to find the tc_to_txq map or
2822 * XPS map for itself.
2823 */
2824 while (count--)
2825 netdev_get_tx_queue(dev, count + offset)->sb_dev = sb_dev;
2826
2827 return 0;
2828}
2829EXPORT_SYMBOL(netdev_bind_sb_channel_queue);
2830
2831int netdev_set_sb_channel(struct net_device *dev, u16 channel)
2832{
2833 /* Do not use a multiqueue device to represent a subordinate channel */
2834 if (netif_is_multiqueue(dev))
2835 return -ENODEV;
2836
2837 /* We allow channels 1 - 32767 to be used for subordinate channels.
2838 * Channel 0 is meant to be "native" mode and used only to represent
2839 * the main root device. We allow writing 0 to reset the device back
2840 * to normal mode after being used as a subordinate channel.
2841 */
2842 if (channel > S16_MAX)
2843 return -EINVAL;
2844
2845 dev->num_tc = -channel;
2846
2847 return 0;
2848}
2849EXPORT_SYMBOL(netdev_set_sb_channel);
2850
f0796d5c
JF
2851/*
2852 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
3a053b1a 2853 * greater than real_num_tx_queues stale skbs on the qdisc must be flushed.
f0796d5c 2854 */
e6484930 2855int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
f0796d5c 2856{
ac5b7019 2857 bool disabling;
1d24eb48
TH
2858 int rc;
2859
ac5b7019
JK
2860 disabling = txq < dev->real_num_tx_queues;
2861
e6484930
TH
2862 if (txq < 1 || txq > dev->num_tx_queues)
2863 return -EINVAL;
f0796d5c 2864
5c56580b
BH
2865 if (dev->reg_state == NETREG_REGISTERED ||
2866 dev->reg_state == NETREG_UNREGISTERING) {
e6484930
TH
2867 ASSERT_RTNL();
2868
1d24eb48
TH
2869 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2870 txq);
bf264145
TH
2871 if (rc)
2872 return rc;
2873
4f57c087
JF
2874 if (dev->num_tc)
2875 netif_setup_tc(dev, txq);
2876
ac5b7019
JK
2877 dev->real_num_tx_queues = txq;
2878
2879 if (disabling) {
2880 synchronize_net();
e6484930 2881 qdisc_reset_all_tx_gt(dev, txq);
024e9679
AD
2882#ifdef CONFIG_XPS
2883 netif_reset_xps_queues_gt(dev, txq);
2884#endif
2885 }
ac5b7019
JK
2886 } else {
2887 dev->real_num_tx_queues = txq;
f0796d5c 2888 }
e6484930 2889
e6484930 2890 return 0;
f0796d5c
JF
2891}
2892EXPORT_SYMBOL(netif_set_real_num_tx_queues);
56079431 2893
a953be53 2894#ifdef CONFIG_SYSFS
62fe0b40
BH
2895/**
2896 * netif_set_real_num_rx_queues - set actual number of RX queues used
2897 * @dev: Network device
2898 * @rxq: Actual number of RX queues
2899 *
2900 * This must be called either with the rtnl_lock held or before
2901 * registration of the net device. Returns 0 on success, or a
4e7f7951
BH
2902 * negative error code. If called before registration, it always
2903 * succeeds.
62fe0b40
BH
2904 */
2905int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2906{
2907 int rc;
2908
bd25fa7b
TH
2909 if (rxq < 1 || rxq > dev->num_rx_queues)
2910 return -EINVAL;
2911
62fe0b40
BH
2912 if (dev->reg_state == NETREG_REGISTERED) {
2913 ASSERT_RTNL();
2914
62fe0b40
BH
2915 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2916 rxq);
2917 if (rc)
2918 return rc;
62fe0b40
BH
2919 }
2920
2921 dev->real_num_rx_queues = rxq;
2922 return 0;
2923}
2924EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2925#endif
2926
2c53040f
BH
2927/**
2928 * netif_get_num_default_rss_queues - default number of RSS queues
16917b87
YM
2929 *
2930 * This routine should set an upper limit on the number of RSS queues
2931 * used by default by multiqueue devices.
2932 */
a55b138b 2933int netif_get_num_default_rss_queues(void)
16917b87 2934{
40e4e713
HS
2935 return is_kdump_kernel() ?
2936 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
16917b87
YM
2937}
2938EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2939
3bcb846c 2940static void __netif_reschedule(struct Qdisc *q)
56079431 2941{
def82a1d
JP
2942 struct softnet_data *sd;
2943 unsigned long flags;
56079431 2944
def82a1d 2945 local_irq_save(flags);
903ceff7 2946 sd = this_cpu_ptr(&softnet_data);
a9cbd588
CG
2947 q->next_sched = NULL;
2948 *sd->output_queue_tailp = q;
2949 sd->output_queue_tailp = &q->next_sched;
def82a1d
JP
2950 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2951 local_irq_restore(flags);
2952}
2953
2954void __netif_schedule(struct Qdisc *q)
2955{
2956 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2957 __netif_reschedule(q);
56079431
DV
2958}
2959EXPORT_SYMBOL(__netif_schedule);
2960
e6247027
ED
2961struct dev_kfree_skb_cb {
2962 enum skb_free_reason reason;
2963};
2964
2965static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
56079431 2966{
e6247027
ED
2967 return (struct dev_kfree_skb_cb *)skb->cb;
2968}
2969
46e5da40
JF
2970void netif_schedule_queue(struct netdev_queue *txq)
2971{
2972 rcu_read_lock();
5be5515a 2973 if (!netif_xmit_stopped(txq)) {
46e5da40
JF
2974 struct Qdisc *q = rcu_dereference(txq->qdisc);
2975
2976 __netif_schedule(q);
2977 }
2978 rcu_read_unlock();
2979}
2980EXPORT_SYMBOL(netif_schedule_queue);
2981
46e5da40
JF
2982void netif_tx_wake_queue(struct netdev_queue *dev_queue)
2983{
2984 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
2985 struct Qdisc *q;
2986
2987 rcu_read_lock();
2988 q = rcu_dereference(dev_queue->qdisc);
2989 __netif_schedule(q);
2990 rcu_read_unlock();
2991 }
2992}
2993EXPORT_SYMBOL(netif_tx_wake_queue);
2994
e6247027 2995void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
56079431 2996{
e6247027 2997 unsigned long flags;
56079431 2998
9899886d
MJ
2999 if (unlikely(!skb))
3000 return;
3001
63354797 3002 if (likely(refcount_read(&skb->users) == 1)) {
e6247027 3003 smp_rmb();
63354797
RE
3004 refcount_set(&skb->users, 0);
3005 } else if (likely(!refcount_dec_and_test(&skb->users))) {
e6247027 3006 return;
bea3348e 3007 }
e6247027
ED
3008 get_kfree_skb_cb(skb)->reason = reason;
3009 local_irq_save(flags);
3010 skb->next = __this_cpu_read(softnet_data.completion_queue);
3011 __this_cpu_write(softnet_data.completion_queue, skb);
3012 raise_softirq_irqoff(NET_TX_SOFTIRQ);
3013 local_irq_restore(flags);
56079431 3014}
e6247027 3015EXPORT_SYMBOL(__dev_kfree_skb_irq);
56079431 3016
e6247027 3017void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
56079431
DV
3018{
3019 if (in_irq() || irqs_disabled())
e6247027 3020 __dev_kfree_skb_irq(skb, reason);
56079431
DV
3021 else
3022 dev_kfree_skb(skb);
3023}
e6247027 3024EXPORT_SYMBOL(__dev_kfree_skb_any);
56079431
DV
3025
3026
bea3348e
SH
3027/**
3028 * netif_device_detach - mark device as removed
3029 * @dev: network device
3030 *
3031 * Mark device as removed from system and therefore no longer available.
3032 */
56079431
DV
3033void netif_device_detach(struct net_device *dev)
3034{
3035 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
3036 netif_running(dev)) {
d543103a 3037 netif_tx_stop_all_queues(dev);
56079431
DV
3038 }
3039}
3040EXPORT_SYMBOL(netif_device_detach);
3041
bea3348e
SH
3042/**
3043 * netif_device_attach - mark device as attached
3044 * @dev: network device
3045 *
3046 * Mark device as attached from system and restart if needed.
3047 */
56079431
DV
3048void netif_device_attach(struct net_device *dev)
3049{
3050 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
3051 netif_running(dev)) {
d543103a 3052 netif_tx_wake_all_queues(dev);
4ec93edb 3053 __netdev_watchdog_up(dev);
56079431
DV
3054 }
3055}
3056EXPORT_SYMBOL(netif_device_attach);
3057
5605c762
JP
3058/*
3059 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
3060 * to be used as a distribution range.
3061 */
eadec877
AD
3062static u16 skb_tx_hash(const struct net_device *dev,
3063 const struct net_device *sb_dev,
3064 struct sk_buff *skb)
5605c762
JP
3065{
3066 u32 hash;
3067 u16 qoffset = 0;
1b837d48 3068 u16 qcount = dev->real_num_tx_queues;
5605c762 3069
eadec877
AD
3070 if (dev->num_tc) {
3071 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
3072
3073 qoffset = sb_dev->tc_to_txq[tc].offset;
3074 qcount = sb_dev->tc_to_txq[tc].count;
3075 }
3076
5605c762
JP
3077 if (skb_rx_queue_recorded(skb)) {
3078 hash = skb_get_rx_queue(skb);
1b837d48
AD
3079 while (unlikely(hash >= qcount))
3080 hash -= qcount;
eadec877 3081 return hash + qoffset;
5605c762
JP
3082 }
3083
3084 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
3085}
5605c762 3086
36c92474
BH
3087static void skb_warn_bad_offload(const struct sk_buff *skb)
3088{
84d15ae5 3089 static const netdev_features_t null_features;
36c92474 3090 struct net_device *dev = skb->dev;
88ad4175 3091 const char *name = "";
36c92474 3092
c846ad9b
BG
3093 if (!net_ratelimit())
3094 return;
3095
88ad4175
BM
3096 if (dev) {
3097 if (dev->dev.parent)
3098 name = dev_driver_string(dev->dev.parent);
3099 else
3100 name = netdev_name(dev);
3101 }
6413139d
WB
3102 skb_dump(KERN_WARNING, skb, false);
3103 WARN(1, "%s: caps=(%pNF, %pNF)\n",
88ad4175 3104 name, dev ? &dev->features : &null_features,
6413139d 3105 skb->sk ? &skb->sk->sk_route_caps : &null_features);
36c92474
BH
3106}
3107
1da177e4
LT
3108/*
3109 * Invalidate hardware checksum when packet is to be mangled, and
3110 * complete checksum manually on outgoing path.
3111 */
84fa7933 3112int skb_checksum_help(struct sk_buff *skb)
1da177e4 3113{
d3bc23e7 3114 __wsum csum;
663ead3b 3115 int ret = 0, offset;
1da177e4 3116
84fa7933 3117 if (skb->ip_summed == CHECKSUM_COMPLETE)
a430a43d
HX
3118 goto out_set_summed;
3119
3120 if (unlikely(skb_shinfo(skb)->gso_size)) {
36c92474
BH
3121 skb_warn_bad_offload(skb);
3122 return -EINVAL;
1da177e4
LT
3123 }
3124
cef401de
ED
3125 /* Before computing a checksum, we should make sure no frag could
3126 * be modified by an external entity : checksum could be wrong.
3127 */
3128 if (skb_has_shared_frag(skb)) {
3129 ret = __skb_linearize(skb);
3130 if (ret)
3131 goto out;
3132 }
3133
55508d60 3134 offset = skb_checksum_start_offset(skb);
a030847e
HX
3135 BUG_ON(offset >= skb_headlen(skb));
3136 csum = skb_checksum(skb, offset, skb->len - offset, 0);
3137
3138 offset += skb->csum_offset;
3139 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
3140
8211fbfa
HK
3141 ret = skb_ensure_writable(skb, offset + sizeof(__sum16));
3142 if (ret)
3143 goto out;
1da177e4 3144
4f2e4ad5 3145 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
a430a43d 3146out_set_summed:
1da177e4 3147 skb->ip_summed = CHECKSUM_NONE;
4ec93edb 3148out:
1da177e4
LT
3149 return ret;
3150}
d1b19dff 3151EXPORT_SYMBOL(skb_checksum_help);
1da177e4 3152
b72b5bf6
DC
3153int skb_crc32c_csum_help(struct sk_buff *skb)
3154{
3155 __le32 crc32c_csum;
3156 int ret = 0, offset, start;
3157
3158 if (skb->ip_summed != CHECKSUM_PARTIAL)
3159 goto out;
3160
3161 if (unlikely(skb_is_gso(skb)))
3162 goto out;
3163
3164 /* Before computing a checksum, we should make sure no frag could
3165 * be modified by an external entity : checksum could be wrong.
3166 */
3167 if (unlikely(skb_has_shared_frag(skb))) {
3168 ret = __skb_linearize(skb);
3169 if (ret)
3170 goto out;
3171 }
3172 start = skb_checksum_start_offset(skb);
3173 offset = start + offsetof(struct sctphdr, checksum);
3174 if (WARN_ON_ONCE(offset >= skb_headlen(skb))) {
3175 ret = -EINVAL;
3176 goto out;
3177 }
8211fbfa
HK
3178
3179 ret = skb_ensure_writable(skb, offset + sizeof(__le32));
3180 if (ret)
3181 goto out;
3182
b72b5bf6
DC
3183 crc32c_csum = cpu_to_le32(~__skb_checksum(skb, start,
3184 skb->len - start, ~(__u32)0,
3185 crc32c_csum_stub));
3186 *(__le32 *)(skb->data + offset) = crc32c_csum;
3187 skb->ip_summed = CHECKSUM_NONE;
dba00306 3188 skb->csum_not_inet = 0;
b72b5bf6
DC
3189out:
3190 return ret;
3191}
3192
53d6471c 3193__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
f6a78bfc 3194{
252e3346 3195 __be16 type = skb->protocol;
f6a78bfc 3196
19acc327
PS
3197 /* Tunnel gso handlers can set protocol to ethernet. */
3198 if (type == htons(ETH_P_TEB)) {
3199 struct ethhdr *eth;
3200
3201 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
3202 return 0;
3203
1dfe82eb 3204 eth = (struct ethhdr *)skb->data;
19acc327
PS
3205 type = eth->h_proto;
3206 }
3207
d4bcef3f 3208 return __vlan_get_protocol(skb, type, depth);
ec5f0615
PS
3209}
3210
3211/**
3212 * skb_mac_gso_segment - mac layer segmentation handler.
3213 * @skb: buffer to segment
3214 * @features: features for the output path (see dev->features)
3215 */
3216struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
3217 netdev_features_t features)
3218{
3219 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
3220 struct packet_offload *ptype;
53d6471c
VY
3221 int vlan_depth = skb->mac_len;
3222 __be16 type = skb_network_protocol(skb, &vlan_depth);
ec5f0615
PS
3223
3224 if (unlikely(!type))
3225 return ERR_PTR(-EINVAL);
3226
53d6471c 3227 __skb_pull(skb, vlan_depth);
f6a78bfc
HX
3228
3229 rcu_read_lock();
22061d80 3230 list_for_each_entry_rcu(ptype, &offload_base, list) {
f191a1d1 3231 if (ptype->type == type && ptype->callbacks.gso_segment) {
f191a1d1 3232 segs = ptype->callbacks.gso_segment(skb, features);
f6a78bfc
HX
3233 break;
3234 }
3235 }
3236 rcu_read_unlock();
3237
98e399f8 3238 __skb_push(skb, skb->data - skb_mac_header(skb));
576a30eb 3239
f6a78bfc
HX
3240 return segs;
3241}
05e8ef4a
PS
3242EXPORT_SYMBOL(skb_mac_gso_segment);
3243
3244
3245/* openvswitch calls this on rx path, so we need a different check.
3246 */
3247static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
3248{
3249 if (tx_path)
0c19f846
WB
3250 return skb->ip_summed != CHECKSUM_PARTIAL &&
3251 skb->ip_summed != CHECKSUM_UNNECESSARY;
6e7bc478
ED
3252
3253 return skb->ip_summed == CHECKSUM_NONE;
05e8ef4a
PS
3254}
3255
3256/**
3257 * __skb_gso_segment - Perform segmentation on skb.
3258 * @skb: buffer to segment
3259 * @features: features for the output path (see dev->features)
3260 * @tx_path: whether it is called in TX path
3261 *
3262 * This function segments the given skb and returns a list of segments.
3263 *
3264 * It may return NULL if the skb requires no segmentation. This is
3265 * only possible when GSO is used for verifying header integrity.
9207f9d4
KK
3266 *
3267 * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb.
05e8ef4a
PS
3268 */
3269struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
3270 netdev_features_t features, bool tx_path)
3271{
b2504a5d
ED
3272 struct sk_buff *segs;
3273
05e8ef4a
PS
3274 if (unlikely(skb_needs_check(skb, tx_path))) {
3275 int err;
3276
b2504a5d 3277 /* We're going to init ->check field in TCP or UDP header */
a40e0a66 3278 err = skb_cow_head(skb, 0);
3279 if (err < 0)
05e8ef4a
PS
3280 return ERR_PTR(err);
3281 }
3282
802ab55a
AD
3283 /* Only report GSO partial support if it will enable us to
3284 * support segmentation on this frame without needing additional
3285 * work.
3286 */
3287 if (features & NETIF_F_GSO_PARTIAL) {
3288 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
3289 struct net_device *dev = skb->dev;
3290
3291 partial_features |= dev->features & dev->gso_partial_features;
3292 if (!skb_gso_ok(skb, features | partial_features))
3293 features &= ~NETIF_F_GSO_PARTIAL;
3294 }
3295
9207f9d4
KK
3296 BUILD_BUG_ON(SKB_SGO_CB_OFFSET +
3297 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
3298
68c33163 3299 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
3347c960
ED
3300 SKB_GSO_CB(skb)->encap_level = 0;
3301
05e8ef4a
PS
3302 skb_reset_mac_header(skb);
3303 skb_reset_mac_len(skb);
3304
b2504a5d
ED
3305 segs = skb_mac_gso_segment(skb, features);
3306
3a1296a3 3307 if (segs != skb && unlikely(skb_needs_check(skb, tx_path) && !IS_ERR(segs)))
b2504a5d
ED
3308 skb_warn_bad_offload(skb);
3309
3310 return segs;
05e8ef4a 3311}
12b0004d 3312EXPORT_SYMBOL(__skb_gso_segment);
f6a78bfc 3313
fb286bb2
HX
3314/* Take action when hardware reception checksum errors are detected. */
3315#ifdef CONFIG_BUG
7fe50ac8 3316void netdev_rx_csum_fault(struct net_device *dev, struct sk_buff *skb)
fb286bb2
HX
3317{
3318 if (net_ratelimit()) {
7b6cd1ce 3319 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
6413139d 3320 skb_dump(KERN_ERR, skb, true);
fb286bb2
HX
3321 dump_stack();
3322 }
3323}
3324EXPORT_SYMBOL(netdev_rx_csum_fault);
3325#endif
3326
ab74cfeb 3327/* XXX: check that highmem exists at all on the given machine. */
c1e756bf 3328static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1da177e4 3329{
3d3a8533 3330#ifdef CONFIG_HIGHMEM
1da177e4 3331 int i;
f4563a75 3332
5acbbd42 3333 if (!(dev->features & NETIF_F_HIGHDMA)) {
ea2ab693
IC
3334 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
3335 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
f4563a75 3336
ea2ab693 3337 if (PageHighMem(skb_frag_page(frag)))
5acbbd42 3338 return 1;
ea2ab693 3339 }
5acbbd42 3340 }
3d3a8533 3341#endif
1da177e4
LT
3342 return 0;
3343}
1da177e4 3344
3b392ddb
SH
3345/* If MPLS offload request, verify we are testing hardware MPLS features
3346 * instead of standard features for the netdev.
3347 */
d0edc7bf 3348#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
3b392ddb
SH
3349static netdev_features_t net_mpls_features(struct sk_buff *skb,
3350 netdev_features_t features,
3351 __be16 type)
3352{
25cd9ba0 3353 if (eth_p_mpls(type))
3b392ddb
SH
3354 features &= skb->dev->mpls_features;
3355
3356 return features;
3357}
3358#else
3359static netdev_features_t net_mpls_features(struct sk_buff *skb,
3360 netdev_features_t features,
3361 __be16 type)
3362{
3363 return features;
3364}
3365#endif
3366
c8f44aff 3367static netdev_features_t harmonize_features(struct sk_buff *skb,
c1e756bf 3368 netdev_features_t features)
f01a5236 3369{
53d6471c 3370 int tmp;
3b392ddb
SH
3371 __be16 type;
3372
3373 type = skb_network_protocol(skb, &tmp);
3374 features = net_mpls_features(skb, features, type);
53d6471c 3375
c0d680e5 3376 if (skb->ip_summed != CHECKSUM_NONE &&
3b392ddb 3377 !can_checksum_protocol(features, type)) {
996e8021 3378 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
f01a5236 3379 }
7be2c82c
ED
3380 if (illegal_highdma(skb->dev, skb))
3381 features &= ~NETIF_F_SG;
f01a5236
JG
3382
3383 return features;
3384}
3385
e38f3025
TM
3386netdev_features_t passthru_features_check(struct sk_buff *skb,
3387 struct net_device *dev,
3388 netdev_features_t features)
3389{
3390 return features;
3391}
3392EXPORT_SYMBOL(passthru_features_check);
3393
7ce23672 3394static netdev_features_t dflt_features_check(struct sk_buff *skb,
8cb65d00
TM
3395 struct net_device *dev,
3396 netdev_features_t features)
3397{
3398 return vlan_features_check(skb, features);
3399}
3400
cbc53e08
AD
3401static netdev_features_t gso_features_check(const struct sk_buff *skb,
3402 struct net_device *dev,
3403 netdev_features_t features)
3404{
3405 u16 gso_segs = skb_shinfo(skb)->gso_segs;
3406
3407 if (gso_segs > dev->gso_max_segs)
3408 return features & ~NETIF_F_GSO_MASK;
3409
802ab55a
AD
3410 /* Support for GSO partial features requires software
3411 * intervention before we can actually process the packets
3412 * so we need to strip support for any partial features now
3413 * and we can pull them back in after we have partially
3414 * segmented the frame.
3415 */
3416 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
3417 features &= ~dev->gso_partial_features;
3418
3419 /* Make sure to clear the IPv4 ID mangling feature if the
3420 * IPv4 header has the potential to be fragmented.
cbc53e08
AD
3421 */
3422 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
3423 struct iphdr *iph = skb->encapsulation ?
3424 inner_ip_hdr(skb) : ip_hdr(skb);
3425
3426 if (!(iph->frag_off & htons(IP_DF)))
3427 features &= ~NETIF_F_TSO_MANGLEID;
3428 }
3429
3430 return features;
3431}
3432
c1e756bf 3433netdev_features_t netif_skb_features(struct sk_buff *skb)
58e998c6 3434{
5f35227e 3435 struct net_device *dev = skb->dev;
fcbeb976 3436 netdev_features_t features = dev->features;
58e998c6 3437
cbc53e08
AD
3438 if (skb_is_gso(skb))
3439 features = gso_features_check(skb, dev, features);
30b678d8 3440
5f35227e
JG
3441 /* If encapsulation offload request, verify we are testing
3442 * hardware encapsulation features instead of standard
3443 * features for the netdev
3444 */
3445 if (skb->encapsulation)
3446 features &= dev->hw_enc_features;
3447
f5a7fb88
TM
3448 if (skb_vlan_tagged(skb))
3449 features = netdev_intersect_features(features,
3450 dev->vlan_features |
3451 NETIF_F_HW_VLAN_CTAG_TX |
3452 NETIF_F_HW_VLAN_STAG_TX);
f01a5236 3453
5f35227e
JG
3454 if (dev->netdev_ops->ndo_features_check)
3455 features &= dev->netdev_ops->ndo_features_check(skb, dev,
3456 features);
8cb65d00
TM
3457 else
3458 features &= dflt_features_check(skb, dev, features);
5f35227e 3459
c1e756bf 3460 return harmonize_features(skb, features);
58e998c6 3461}
c1e756bf 3462EXPORT_SYMBOL(netif_skb_features);
58e998c6 3463
2ea25513 3464static int xmit_one(struct sk_buff *skb, struct net_device *dev,
95f6b3dd 3465 struct netdev_queue *txq, bool more)
f6a78bfc 3466{
2ea25513
DM
3467 unsigned int len;
3468 int rc;
00829823 3469
9f9a742d 3470 if (dev_nit_active(dev))
2ea25513 3471 dev_queue_xmit_nit(skb, dev);
fc741216 3472
2ea25513
DM
3473 len = skb->len;
3474 trace_net_dev_start_xmit(skb, dev);
95f6b3dd 3475 rc = netdev_start_xmit(skb, dev, txq, more);
2ea25513 3476 trace_net_dev_xmit(skb, rc, dev, len);
adf30907 3477
2ea25513
DM
3478 return rc;
3479}
7b9c6090 3480
8dcda22a
DM
3481struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
3482 struct netdev_queue *txq, int *ret)
7f2e870f
DM
3483{
3484 struct sk_buff *skb = first;
3485 int rc = NETDEV_TX_OK;
7b9c6090 3486
7f2e870f
DM
3487 while (skb) {
3488 struct sk_buff *next = skb->next;
fc70fb64 3489
a8305bff 3490 skb_mark_not_on_list(skb);
95f6b3dd 3491 rc = xmit_one(skb, dev, txq, next != NULL);
7f2e870f
DM
3492 if (unlikely(!dev_xmit_complete(rc))) {
3493 skb->next = next;
3494 goto out;
3495 }
6afff0ca 3496
7f2e870f 3497 skb = next;
fe60faa5 3498 if (netif_tx_queue_stopped(txq) && skb) {
7f2e870f
DM
3499 rc = NETDEV_TX_BUSY;
3500 break;
9ccb8975 3501 }
7f2e870f 3502 }
9ccb8975 3503
7f2e870f
DM
3504out:
3505 *ret = rc;
3506 return skb;
3507}
b40863c6 3508
1ff0dc94
ED
3509static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
3510 netdev_features_t features)
f6a78bfc 3511{
df8a39de 3512 if (skb_vlan_tag_present(skb) &&
5968250c
JP
3513 !vlan_hw_offload_capable(features, skb->vlan_proto))
3514 skb = __vlan_hwaccel_push_inside(skb);
eae3f88e
DM
3515 return skb;
3516}
f6a78bfc 3517
43c26a1a
DC
3518int skb_csum_hwoffload_help(struct sk_buff *skb,
3519 const netdev_features_t features)
3520{
3521 if (unlikely(skb->csum_not_inet))
3522 return !!(features & NETIF_F_SCTP_CRC) ? 0 :
3523 skb_crc32c_csum_help(skb);
3524
3525 return !!(features & NETIF_F_CSUM_MASK) ? 0 : skb_checksum_help(skb);
3526}
3527EXPORT_SYMBOL(skb_csum_hwoffload_help);
3528
f53c7239 3529static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev, bool *again)
eae3f88e
DM
3530{
3531 netdev_features_t features;
f6a78bfc 3532
eae3f88e
DM
3533 features = netif_skb_features(skb);
3534 skb = validate_xmit_vlan(skb, features);
3535 if (unlikely(!skb))
3536 goto out_null;
7b9c6090 3537
ebf4e808
IL
3538 skb = sk_validate_xmit_skb(skb, dev);
3539 if (unlikely(!skb))
3540 goto out_null;
3541
8b86a61d 3542 if (netif_needs_gso(skb, features)) {
ce93718f
DM
3543 struct sk_buff *segs;
3544
3545 segs = skb_gso_segment(skb, features);
cecda693 3546 if (IS_ERR(segs)) {
af6dabc9 3547 goto out_kfree_skb;
cecda693
JW
3548 } else if (segs) {
3549 consume_skb(skb);
3550 skb = segs;
f6a78bfc 3551 }
eae3f88e
DM
3552 } else {
3553 if (skb_needs_linearize(skb, features) &&
3554 __skb_linearize(skb))
3555 goto out_kfree_skb;
4ec93edb 3556
eae3f88e
DM
3557 /* If packet is not checksummed and device does not
3558 * support checksumming for this protocol, complete
3559 * checksumming here.
3560 */
3561 if (skb->ip_summed == CHECKSUM_PARTIAL) {
3562 if (skb->encapsulation)
3563 skb_set_inner_transport_header(skb,
3564 skb_checksum_start_offset(skb));
3565 else
3566 skb_set_transport_header(skb,
3567 skb_checksum_start_offset(skb));
43c26a1a 3568 if (skb_csum_hwoffload_help(skb, features))
eae3f88e 3569 goto out_kfree_skb;
7b9c6090 3570 }
0c772159 3571 }
7b9c6090 3572
f53c7239 3573 skb = validate_xmit_xfrm(skb, features, again);
3dca3f38 3574
eae3f88e 3575 return skb;
fc70fb64 3576
f6a78bfc
HX
3577out_kfree_skb:
3578 kfree_skb(skb);
eae3f88e 3579out_null:
d21fd63e 3580 atomic_long_inc(&dev->tx_dropped);
eae3f88e
DM
3581 return NULL;
3582}
6afff0ca 3583
f53c7239 3584struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev, bool *again)
55a93b3e
ED
3585{
3586 struct sk_buff *next, *head = NULL, *tail;
3587
bec3cfdc 3588 for (; skb != NULL; skb = next) {
55a93b3e 3589 next = skb->next;
a8305bff 3590 skb_mark_not_on_list(skb);
bec3cfdc
ED
3591
3592 /* in case skb wont be segmented, point to itself */
3593 skb->prev = skb;
3594
f53c7239 3595 skb = validate_xmit_skb(skb, dev, again);
bec3cfdc
ED
3596 if (!skb)
3597 continue;
55a93b3e 3598
bec3cfdc
ED
3599 if (!head)
3600 head = skb;
3601 else
3602 tail->next = skb;
3603 /* If skb was segmented, skb->prev points to
3604 * the last segment. If not, it still contains skb.
3605 */
3606 tail = skb->prev;
55a93b3e
ED
3607 }
3608 return head;
f6a78bfc 3609}
104ba78c 3610EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
f6a78bfc 3611
1def9238
ED
3612static void qdisc_pkt_len_init(struct sk_buff *skb)
3613{
3614 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3615
3616 qdisc_skb_cb(skb)->pkt_len = skb->len;
3617
3618 /* To get more precise estimation of bytes sent on wire,
3619 * we add to pkt_len the headers size of all segments
3620 */
a0dce875 3621 if (shinfo->gso_size && skb_transport_header_was_set(skb)) {
757b8b1d 3622 unsigned int hdr_len;
15e5a030 3623 u16 gso_segs = shinfo->gso_segs;
1def9238 3624
757b8b1d
ED
3625 /* mac layer + network layer */
3626 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3627
3628 /* + transport layer */
7c68d1a6
ED
3629 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6))) {
3630 const struct tcphdr *th;
3631 struct tcphdr _tcphdr;
3632
3633 th = skb_header_pointer(skb, skb_transport_offset(skb),
3634 sizeof(_tcphdr), &_tcphdr);
3635 if (likely(th))
3636 hdr_len += __tcp_hdrlen(th);
3637 } else {
3638 struct udphdr _udphdr;
3639
3640 if (skb_header_pointer(skb, skb_transport_offset(skb),
3641 sizeof(_udphdr), &_udphdr))
3642 hdr_len += sizeof(struct udphdr);
3643 }
15e5a030
JW
3644
3645 if (shinfo->gso_type & SKB_GSO_DODGY)
3646 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3647 shinfo->gso_size);
3648
3649 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
1def9238
ED
3650 }
3651}
3652
bbd8a0d3
KK
3653static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3654 struct net_device *dev,
3655 struct netdev_queue *txq)
3656{
3657 spinlock_t *root_lock = qdisc_lock(q);
520ac30f 3658 struct sk_buff *to_free = NULL;
a2da570d 3659 bool contended;
bbd8a0d3
KK
3660 int rc;
3661
a2da570d 3662 qdisc_calculate_pkt_len(skb, q);
6b3ba914
JF
3663
3664 if (q->flags & TCQ_F_NOLOCK) {
90b2be27 3665 if ((q->flags & TCQ_F_CAN_BYPASS) && READ_ONCE(q->empty) &&
d518d2ed
PA
3666 qdisc_run_begin(q)) {
3667 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED,
3668 &q->state))) {
3669 __qdisc_drop(skb, &to_free);
3670 rc = NET_XMIT_DROP;
3671 goto end_run;
3672 }
ba27b4cd
PA
3673 qdisc_bstats_cpu_update(q, skb);
3674
d518d2ed 3675 rc = NET_XMIT_SUCCESS;
ba27b4cd
PA
3676 if (sch_direct_xmit(skb, q, dev, txq, NULL, true))
3677 __qdisc_run(q);
3678
d518d2ed 3679end_run:
ba27b4cd 3680 qdisc_run_end(q);
6b3ba914
JF
3681 } else {
3682 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
32f7b44d 3683 qdisc_run(q);
6b3ba914
JF
3684 }
3685
3686 if (unlikely(to_free))
3687 kfree_skb_list(to_free);
3688 return rc;
3689 }
3690
79640a4c
ED
3691 /*
3692 * Heuristic to force contended enqueues to serialize on a
3693 * separate lock before trying to get qdisc main lock.
f9eb8aea 3694 * This permits qdisc->running owner to get the lock more
9bf2b8c2 3695 * often and dequeue packets faster.
79640a4c 3696 */
a2da570d 3697 contended = qdisc_is_running(q);
79640a4c
ED
3698 if (unlikely(contended))
3699 spin_lock(&q->busylock);
3700
bbd8a0d3
KK
3701 spin_lock(root_lock);
3702 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
520ac30f 3703 __qdisc_drop(skb, &to_free);
bbd8a0d3
KK
3704 rc = NET_XMIT_DROP;
3705 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
bc135b23 3706 qdisc_run_begin(q)) {
bbd8a0d3
KK
3707 /*
3708 * This is a work-conserving queue; there are no old skbs
3709 * waiting to be sent out; and the qdisc is not running -
3710 * xmit the skb directly.
3711 */
bfe0d029 3712
bfe0d029
ED
3713 qdisc_bstats_update(q, skb);
3714
55a93b3e 3715 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
79640a4c
ED
3716 if (unlikely(contended)) {
3717 spin_unlock(&q->busylock);
3718 contended = false;
3719 }
bbd8a0d3 3720 __qdisc_run(q);
6c148184 3721 }
bbd8a0d3 3722
6c148184 3723 qdisc_run_end(q);
bbd8a0d3
KK
3724 rc = NET_XMIT_SUCCESS;
3725 } else {
520ac30f 3726 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
79640a4c
ED
3727 if (qdisc_run_begin(q)) {
3728 if (unlikely(contended)) {
3729 spin_unlock(&q->busylock);
3730 contended = false;
3731 }
3732 __qdisc_run(q);
6c148184 3733 qdisc_run_end(q);
79640a4c 3734 }
bbd8a0d3
KK
3735 }
3736 spin_unlock(root_lock);
520ac30f
ED
3737 if (unlikely(to_free))
3738 kfree_skb_list(to_free);
79640a4c
ED
3739 if (unlikely(contended))
3740 spin_unlock(&q->busylock);
bbd8a0d3
KK
3741 return rc;
3742}
3743
86f8515f 3744#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
5bc1421e
NH
3745static void skb_update_prio(struct sk_buff *skb)
3746{
4dcb31d4
ED
3747 const struct netprio_map *map;
3748 const struct sock *sk;
3749 unsigned int prioidx;
5bc1421e 3750
4dcb31d4
ED
3751 if (skb->priority)
3752 return;
3753 map = rcu_dereference_bh(skb->dev->priomap);
3754 if (!map)
3755 return;
3756 sk = skb_to_full_sk(skb);
3757 if (!sk)
3758 return;
91c68ce2 3759
4dcb31d4
ED
3760 prioidx = sock_cgroup_prioidx(&sk->sk_cgrp_data);
3761
3762 if (prioidx < map->priomap_len)
3763 skb->priority = map->priomap[prioidx];
5bc1421e
NH
3764}
3765#else
3766#define skb_update_prio(skb)
3767#endif
3768
95603e22
MM
3769/**
3770 * dev_loopback_xmit - loop back @skb
0c4b51f0
EB
3771 * @net: network namespace this loopback is happening in
3772 * @sk: sk needed to be a netfilter okfn
95603e22
MM
3773 * @skb: buffer to transmit
3774 */
0c4b51f0 3775int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
95603e22
MM
3776{
3777 skb_reset_mac_header(skb);
3778 __skb_pull(skb, skb_network_offset(skb));
3779 skb->pkt_type = PACKET_LOOPBACK;
3780 skb->ip_summed = CHECKSUM_UNNECESSARY;
3781 WARN_ON(!skb_dst(skb));
3782 skb_dst_force(skb);
3783 netif_rx_ni(skb);
3784 return 0;
3785}
3786EXPORT_SYMBOL(dev_loopback_xmit);
3787
1f211a1b
DB
3788#ifdef CONFIG_NET_EGRESS
3789static struct sk_buff *
3790sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3791{
46209401 3792 struct mini_Qdisc *miniq = rcu_dereference_bh(dev->miniq_egress);
1f211a1b
DB
3793 struct tcf_result cl_res;
3794
46209401 3795 if (!miniq)
1f211a1b
DB
3796 return skb;
3797
8dc07fdb 3798 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
46209401 3799 mini_qdisc_bstats_cpu_update(miniq, skb);
1f211a1b 3800
46209401 3801 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
1f211a1b
DB
3802 case TC_ACT_OK:
3803 case TC_ACT_RECLASSIFY:
3804 skb->tc_index = TC_H_MIN(cl_res.classid);
3805 break;
3806 case TC_ACT_SHOT:
46209401 3807 mini_qdisc_qstats_cpu_drop(miniq);
1f211a1b 3808 *ret = NET_XMIT_DROP;
7e2c3aea
DB
3809 kfree_skb(skb);
3810 return NULL;
1f211a1b
DB
3811 case TC_ACT_STOLEN:
3812 case TC_ACT_QUEUED:
e25ea21f 3813 case TC_ACT_TRAP:
1f211a1b 3814 *ret = NET_XMIT_SUCCESS;
7e2c3aea 3815 consume_skb(skb);
1f211a1b
DB
3816 return NULL;
3817 case TC_ACT_REDIRECT:
3818 /* No need to push/pop skb's mac_header here on egress! */
3819 skb_do_redirect(skb);
3820 *ret = NET_XMIT_SUCCESS;
3821 return NULL;
3822 default:
3823 break;
3824 }
3825
3826 return skb;
3827}
3828#endif /* CONFIG_NET_EGRESS */
3829
fc9bab24
AN
3830#ifdef CONFIG_XPS
3831static int __get_xps_queue_idx(struct net_device *dev, struct sk_buff *skb,
3832 struct xps_dev_maps *dev_maps, unsigned int tci)
3833{
3834 struct xps_map *map;
3835 int queue_index = -1;
3836
3837 if (dev->num_tc) {
3838 tci *= dev->num_tc;
3839 tci += netdev_get_prio_tc_map(dev, skb->priority);
3840 }
3841
3842 map = rcu_dereference(dev_maps->attr_map[tci]);
3843 if (map) {
3844 if (map->len == 1)
3845 queue_index = map->queues[0];
3846 else
3847 queue_index = map->queues[reciprocal_scale(
3848 skb_get_hash(skb), map->len)];
3849 if (unlikely(queue_index >= dev->real_num_tx_queues))
3850 queue_index = -1;
3851 }
3852 return queue_index;
3853}
3854#endif
3855
eadec877
AD
3856static int get_xps_queue(struct net_device *dev, struct net_device *sb_dev,
3857 struct sk_buff *skb)
638b2a69
JP
3858{
3859#ifdef CONFIG_XPS
3860 struct xps_dev_maps *dev_maps;
fc9bab24 3861 struct sock *sk = skb->sk;
638b2a69
JP
3862 int queue_index = -1;
3863
04157469
AN
3864 if (!static_key_false(&xps_needed))
3865 return -1;
3866
638b2a69 3867 rcu_read_lock();
fc9bab24
AN
3868 if (!static_key_false(&xps_rxqs_needed))
3869 goto get_cpus_map;
3870
eadec877 3871 dev_maps = rcu_dereference(sb_dev->xps_rxqs_map);
638b2a69 3872 if (dev_maps) {
fc9bab24 3873 int tci = sk_rx_queue_get(sk);
184c449f 3874
fc9bab24
AN
3875 if (tci >= 0 && tci < dev->num_rx_queues)
3876 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3877 tci);
3878 }
184c449f 3879
fc9bab24
AN
3880get_cpus_map:
3881 if (queue_index < 0) {
eadec877 3882 dev_maps = rcu_dereference(sb_dev->xps_cpus_map);
fc9bab24
AN
3883 if (dev_maps) {
3884 unsigned int tci = skb->sender_cpu - 1;
3885
3886 queue_index = __get_xps_queue_idx(dev, skb, dev_maps,
3887 tci);
638b2a69
JP
3888 }
3889 }
3890 rcu_read_unlock();
3891
3892 return queue_index;
3893#else
3894 return -1;
3895#endif
3896}
3897
a4ea8a3d 3898u16 dev_pick_tx_zero(struct net_device *dev, struct sk_buff *skb,
a350ecce 3899 struct net_device *sb_dev)
a4ea8a3d
AD
3900{
3901 return 0;
3902}
3903EXPORT_SYMBOL(dev_pick_tx_zero);
3904
3905u16 dev_pick_tx_cpu_id(struct net_device *dev, struct sk_buff *skb,
a350ecce 3906 struct net_device *sb_dev)
a4ea8a3d
AD
3907{
3908 return (u16)raw_smp_processor_id() % dev->real_num_tx_queues;
3909}
3910EXPORT_SYMBOL(dev_pick_tx_cpu_id);
3911
b71b5837
PA
3912u16 netdev_pick_tx(struct net_device *dev, struct sk_buff *skb,
3913 struct net_device *sb_dev)
638b2a69
JP
3914{
3915 struct sock *sk = skb->sk;
3916 int queue_index = sk_tx_queue_get(sk);
3917
eadec877
AD
3918 sb_dev = sb_dev ? : dev;
3919
638b2a69
JP
3920 if (queue_index < 0 || skb->ooo_okay ||
3921 queue_index >= dev->real_num_tx_queues) {
eadec877 3922 int new_index = get_xps_queue(dev, sb_dev, skb);
f4563a75 3923
638b2a69 3924 if (new_index < 0)
eadec877 3925 new_index = skb_tx_hash(dev, sb_dev, skb);
638b2a69
JP
3926
3927 if (queue_index != new_index && sk &&
004a5d01 3928 sk_fullsock(sk) &&
638b2a69
JP
3929 rcu_access_pointer(sk->sk_dst_cache))
3930 sk_tx_queue_set(sk, new_index);
3931
3932 queue_index = new_index;
3933 }
3934
3935 return queue_index;
3936}
b71b5837 3937EXPORT_SYMBOL(netdev_pick_tx);
638b2a69 3938
4bd97d51
PA
3939struct netdev_queue *netdev_core_pick_tx(struct net_device *dev,
3940 struct sk_buff *skb,
3941 struct net_device *sb_dev)
638b2a69
JP
3942{
3943 int queue_index = 0;
3944
3945#ifdef CONFIG_XPS
52bd2d62
ED
3946 u32 sender_cpu = skb->sender_cpu - 1;
3947
3948 if (sender_cpu >= (u32)NR_CPUS)
638b2a69
JP
3949 skb->sender_cpu = raw_smp_processor_id() + 1;
3950#endif
3951
3952 if (dev->real_num_tx_queues != 1) {
3953 const struct net_device_ops *ops = dev->netdev_ops;
f4563a75 3954
638b2a69 3955 if (ops->ndo_select_queue)
a350ecce 3956 queue_index = ops->ndo_select_queue(dev, skb, sb_dev);
638b2a69 3957 else
4bd97d51 3958 queue_index = netdev_pick_tx(dev, skb, sb_dev);
638b2a69 3959
d584527c 3960 queue_index = netdev_cap_txqueue(dev, queue_index);
638b2a69
JP
3961 }
3962
3963 skb_set_queue_mapping(skb, queue_index);
3964 return netdev_get_tx_queue(dev, queue_index);
3965}
3966
d29f749e 3967/**
9d08dd3d 3968 * __dev_queue_xmit - transmit a buffer
d29f749e 3969 * @skb: buffer to transmit
eadec877 3970 * @sb_dev: suboordinate device used for L2 forwarding offload
d29f749e
DJ
3971 *
3972 * Queue a buffer for transmission to a network device. The caller must
3973 * have set the device and priority and built the buffer before calling
3974 * this function. The function can be called from an interrupt.
3975 *
3976 * A negative errno code is returned on a failure. A success does not
3977 * guarantee the frame will be transmitted as it may be dropped due
3978 * to congestion or traffic shaping.
3979 *
3980 * -----------------------------------------------------------------------------------
3981 * I notice this method can also return errors from the queue disciplines,
3982 * including NET_XMIT_DROP, which is a positive value. So, errors can also
3983 * be positive.
3984 *
3985 * Regardless of the return value, the skb is consumed, so it is currently
3986 * difficult to retry a send to this method. (You can bump the ref count
3987 * before sending to hold a reference for retry if you are careful.)
3988 *
3989 * When calling this method, interrupts MUST be enabled. This is because
3990 * the BH enable code must have IRQs enabled so that it will not deadlock.
3991 * --BLG
3992 */
eadec877 3993static int __dev_queue_xmit(struct sk_buff *skb, struct net_device *sb_dev)
1da177e4
LT
3994{
3995 struct net_device *dev = skb->dev;
dc2b4847 3996 struct netdev_queue *txq;
1da177e4
LT
3997 struct Qdisc *q;
3998 int rc = -ENOMEM;
f53c7239 3999 bool again = false;
1da177e4 4000
6d1ccff6
ED
4001 skb_reset_mac_header(skb);
4002
e7fd2885
WB
4003 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
4004 __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
4005
4ec93edb
YH
4006 /* Disable soft irqs for various locks below. Also
4007 * stops preemption for RCU.
1da177e4 4008 */
4ec93edb 4009 rcu_read_lock_bh();
1da177e4 4010
5bc1421e
NH
4011 skb_update_prio(skb);
4012
1f211a1b
DB
4013 qdisc_pkt_len_init(skb);
4014#ifdef CONFIG_NET_CLS_ACT
8dc07fdb 4015 skb->tc_at_ingress = 0;
1f211a1b 4016# ifdef CONFIG_NET_EGRESS
aabf6772 4017 if (static_branch_unlikely(&egress_needed_key)) {
1f211a1b
DB
4018 skb = sch_handle_egress(skb, &rc, dev);
4019 if (!skb)
4020 goto out;
4021 }
4022# endif
4023#endif
02875878
ED
4024 /* If device/qdisc don't need skb->dst, release it right now while
4025 * its hot in this cpu cache.
4026 */
4027 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
4028 skb_dst_drop(skb);
4029 else
4030 skb_dst_force(skb);
4031
4bd97d51 4032 txq = netdev_core_pick_tx(dev, skb, sb_dev);
a898def2 4033 q = rcu_dereference_bh(txq->qdisc);
37437bb2 4034
cf66ba58 4035 trace_net_dev_queue(skb);
1da177e4 4036 if (q->enqueue) {
bbd8a0d3 4037 rc = __dev_xmit_skb(skb, q, dev, txq);
37437bb2 4038 goto out;
1da177e4
LT
4039 }
4040
4041 /* The device has no queue. Common case for software devices:
eb13da1a 4042 * loopback, all the sorts of tunnels...
1da177e4 4043
eb13da1a 4044 * Really, it is unlikely that netif_tx_lock protection is necessary
4045 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
4046 * counters.)
4047 * However, it is possible, that they rely on protection
4048 * made by us here.
1da177e4 4049
eb13da1a 4050 * Check this and shot the lock. It is not prone from deadlocks.
4051 *Either shot noqueue qdisc, it is even simpler 8)
1da177e4
LT
4052 */
4053 if (dev->flags & IFF_UP) {
4054 int cpu = smp_processor_id(); /* ok because BHs are off */
4055
c773e847 4056 if (txq->xmit_lock_owner != cpu) {
97cdcf37 4057 if (dev_xmit_recursion())
745e20f1
ED
4058 goto recursion_alert;
4059
f53c7239 4060 skb = validate_xmit_skb(skb, dev, &again);
1f59533f 4061 if (!skb)
d21fd63e 4062 goto out;
1f59533f 4063
c773e847 4064 HARD_TX_LOCK(dev, txq, cpu);
1da177e4 4065
73466498 4066 if (!netif_xmit_stopped(txq)) {
97cdcf37 4067 dev_xmit_recursion_inc();
ce93718f 4068 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
97cdcf37 4069 dev_xmit_recursion_dec();
572a9d7b 4070 if (dev_xmit_complete(rc)) {
c773e847 4071 HARD_TX_UNLOCK(dev, txq);
1da177e4
LT
4072 goto out;
4073 }
4074 }
c773e847 4075 HARD_TX_UNLOCK(dev, txq);
e87cc472
JP
4076 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
4077 dev->name);
1da177e4
LT
4078 } else {
4079 /* Recursion is detected! It is possible,
745e20f1
ED
4080 * unfortunately
4081 */
4082recursion_alert:
e87cc472
JP
4083 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
4084 dev->name);
1da177e4
LT
4085 }
4086 }
4087
4088 rc = -ENETDOWN;
d4828d85 4089 rcu_read_unlock_bh();
1da177e4 4090
015f0688 4091 atomic_long_inc(&dev->tx_dropped);
1f59533f 4092 kfree_skb_list(skb);
1da177e4
LT
4093 return rc;
4094out:
d4828d85 4095 rcu_read_unlock_bh();
1da177e4
LT
4096 return rc;
4097}
f663dd9a 4098
2b4aa3ce 4099int dev_queue_xmit(struct sk_buff *skb)
f663dd9a
JW
4100{
4101 return __dev_queue_xmit(skb, NULL);
4102}
2b4aa3ce 4103EXPORT_SYMBOL(dev_queue_xmit);
1da177e4 4104
eadec877 4105int dev_queue_xmit_accel(struct sk_buff *skb, struct net_device *sb_dev)
f663dd9a 4106{
eadec877 4107 return __dev_queue_xmit(skb, sb_dev);
f663dd9a
JW
4108}
4109EXPORT_SYMBOL(dev_queue_xmit_accel);
4110
865b03f2
MK
4111int dev_direct_xmit(struct sk_buff *skb, u16 queue_id)
4112{
4113 struct net_device *dev = skb->dev;
4114 struct sk_buff *orig_skb = skb;
4115 struct netdev_queue *txq;
4116 int ret = NETDEV_TX_BUSY;
4117 bool again = false;
4118
4119 if (unlikely(!netif_running(dev) ||
4120 !netif_carrier_ok(dev)))
4121 goto drop;
4122
4123 skb = validate_xmit_skb_list(skb, dev, &again);
4124 if (skb != orig_skb)
4125 goto drop;
4126
4127 skb_set_queue_mapping(skb, queue_id);
4128 txq = skb_get_tx_queue(dev, skb);
4129
4130 local_bh_disable();
4131
4132 HARD_TX_LOCK(dev, txq, smp_processor_id());
4133 if (!netif_xmit_frozen_or_drv_stopped(txq))
4134 ret = netdev_start_xmit(skb, dev, txq, false);
4135 HARD_TX_UNLOCK(dev, txq);
4136
4137 local_bh_enable();
4138
4139 if (!dev_xmit_complete(ret))
4140 kfree_skb(skb);
4141
4142 return ret;
4143drop:
4144 atomic_long_inc(&dev->tx_dropped);
4145 kfree_skb_list(skb);
4146 return NET_XMIT_DROP;
4147}
4148EXPORT_SYMBOL(dev_direct_xmit);
1da177e4 4149
eb13da1a 4150/*************************************************************************
4151 * Receiver routines
4152 *************************************************************************/
1da177e4 4153
6b2bedc3 4154int netdev_max_backlog __read_mostly = 1000;
c9e6bc64
ED
4155EXPORT_SYMBOL(netdev_max_backlog);
4156
3b098e2d 4157int netdev_tstamp_prequeue __read_mostly = 1;
6b2bedc3 4158int netdev_budget __read_mostly = 300;
7acf8a1e 4159unsigned int __read_mostly netdev_budget_usecs = 2000;
3d48b53f
MT
4160int weight_p __read_mostly = 64; /* old backlog weight */
4161int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
4162int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
4163int dev_rx_weight __read_mostly = 64;
4164int dev_tx_weight __read_mostly = 64;
323ebb61
EC
4165/* Maximum number of GRO_NORMAL skbs to batch up for list-RX */
4166int gro_normal_batch __read_mostly = 8;
1da177e4 4167
eecfd7c4
ED
4168/* Called with irq disabled */
4169static inline void ____napi_schedule(struct softnet_data *sd,
4170 struct napi_struct *napi)
4171{
4172 list_add_tail(&napi->poll_list, &sd->poll_list);
4173 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4174}
4175
bfb564e7
KK
4176#ifdef CONFIG_RPS
4177
4178/* One global table that all flow-based protocols share. */
6e3f7faf 4179struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
bfb564e7 4180EXPORT_SYMBOL(rps_sock_flow_table);
567e4b79
ED
4181u32 rps_cpu_mask __read_mostly;
4182EXPORT_SYMBOL(rps_cpu_mask);
bfb564e7 4183
dc05360f 4184struct static_key_false rps_needed __read_mostly;
3df97ba8 4185EXPORT_SYMBOL(rps_needed);
dc05360f 4186struct static_key_false rfs_needed __read_mostly;
13bfff25 4187EXPORT_SYMBOL(rfs_needed);
adc9300e 4188
c445477d
BH
4189static struct rps_dev_flow *
4190set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4191 struct rps_dev_flow *rflow, u16 next_cpu)
4192{
a31196b0 4193 if (next_cpu < nr_cpu_ids) {
c445477d
BH
4194#ifdef CONFIG_RFS_ACCEL
4195 struct netdev_rx_queue *rxqueue;
4196 struct rps_dev_flow_table *flow_table;
4197 struct rps_dev_flow *old_rflow;
4198 u32 flow_id;
4199 u16 rxq_index;
4200 int rc;
4201
4202 /* Should we steer this flow to a different hardware queue? */
69a19ee6
BH
4203 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
4204 !(dev->features & NETIF_F_NTUPLE))
c445477d
BH
4205 goto out;
4206 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
4207 if (rxq_index == skb_get_rx_queue(skb))
4208 goto out;
4209
4210 rxqueue = dev->_rx + rxq_index;
4211 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4212 if (!flow_table)
4213 goto out;
61b905da 4214 flow_id = skb_get_hash(skb) & flow_table->mask;
c445477d
BH
4215 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
4216 rxq_index, flow_id);
4217 if (rc < 0)
4218 goto out;
4219 old_rflow = rflow;
4220 rflow = &flow_table->flows[flow_id];
c445477d
BH
4221 rflow->filter = rc;
4222 if (old_rflow->filter == rflow->filter)
4223 old_rflow->filter = RPS_NO_FILTER;
4224 out:
4225#endif
4226 rflow->last_qtail =
09994d1b 4227 per_cpu(softnet_data, next_cpu).input_queue_head;
c445477d
BH
4228 }
4229
09994d1b 4230 rflow->cpu = next_cpu;
c445477d
BH
4231 return rflow;
4232}
4233
bfb564e7
KK
4234/*
4235 * get_rps_cpu is called from netif_receive_skb and returns the target
4236 * CPU from the RPS map of the receiving queue for a given skb.
4237 * rcu_read_lock must be held on entry.
4238 */
4239static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
4240 struct rps_dev_flow **rflowp)
4241{
567e4b79
ED
4242 const struct rps_sock_flow_table *sock_flow_table;
4243 struct netdev_rx_queue *rxqueue = dev->_rx;
bfb564e7 4244 struct rps_dev_flow_table *flow_table;
567e4b79 4245 struct rps_map *map;
bfb564e7 4246 int cpu = -1;
567e4b79 4247 u32 tcpu;
61b905da 4248 u32 hash;
bfb564e7
KK
4249
4250 if (skb_rx_queue_recorded(skb)) {
4251 u16 index = skb_get_rx_queue(skb);
567e4b79 4252
62fe0b40
BH
4253 if (unlikely(index >= dev->real_num_rx_queues)) {
4254 WARN_ONCE(dev->real_num_rx_queues > 1,
4255 "%s received packet on queue %u, but number "
4256 "of RX queues is %u\n",
4257 dev->name, index, dev->real_num_rx_queues);
bfb564e7
KK
4258 goto done;
4259 }
567e4b79
ED
4260 rxqueue += index;
4261 }
bfb564e7 4262
567e4b79
ED
4263 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
4264
4265 flow_table = rcu_dereference(rxqueue->rps_flow_table);
6e3f7faf 4266 map = rcu_dereference(rxqueue->rps_map);
567e4b79 4267 if (!flow_table && !map)
bfb564e7
KK
4268 goto done;
4269
2d47b459 4270 skb_reset_network_header(skb);
61b905da
TH
4271 hash = skb_get_hash(skb);
4272 if (!hash)
bfb564e7
KK
4273 goto done;
4274
fec5e652
TH
4275 sock_flow_table = rcu_dereference(rps_sock_flow_table);
4276 if (flow_table && sock_flow_table) {
fec5e652 4277 struct rps_dev_flow *rflow;
567e4b79
ED
4278 u32 next_cpu;
4279 u32 ident;
4280
4281 /* First check into global flow table if there is a match */
4282 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
4283 if ((ident ^ hash) & ~rps_cpu_mask)
4284 goto try_rps;
fec5e652 4285
567e4b79
ED
4286 next_cpu = ident & rps_cpu_mask;
4287
4288 /* OK, now we know there is a match,
4289 * we can look at the local (per receive queue) flow table
4290 */
61b905da 4291 rflow = &flow_table->flows[hash & flow_table->mask];
fec5e652
TH
4292 tcpu = rflow->cpu;
4293
fec5e652
TH
4294 /*
4295 * If the desired CPU (where last recvmsg was done) is
4296 * different from current CPU (one in the rx-queue flow
4297 * table entry), switch if one of the following holds:
a31196b0 4298 * - Current CPU is unset (>= nr_cpu_ids).
fec5e652
TH
4299 * - Current CPU is offline.
4300 * - The current CPU's queue tail has advanced beyond the
4301 * last packet that was enqueued using this table entry.
4302 * This guarantees that all previous packets for the flow
4303 * have been dequeued, thus preserving in order delivery.
4304 */
4305 if (unlikely(tcpu != next_cpu) &&
a31196b0 4306 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
fec5e652 4307 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
baefa31d
TH
4308 rflow->last_qtail)) >= 0)) {
4309 tcpu = next_cpu;
c445477d 4310 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
baefa31d 4311 }
c445477d 4312
a31196b0 4313 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
fec5e652
TH
4314 *rflowp = rflow;
4315 cpu = tcpu;
4316 goto done;
4317 }
4318 }
4319
567e4b79
ED
4320try_rps:
4321
0a9627f2 4322 if (map) {
8fc54f68 4323 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
0a9627f2
TH
4324 if (cpu_online(tcpu)) {
4325 cpu = tcpu;
4326 goto done;
4327 }
4328 }
4329
4330done:
0a9627f2
TH
4331 return cpu;
4332}
4333
c445477d
BH
4334#ifdef CONFIG_RFS_ACCEL
4335
4336/**
4337 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
4338 * @dev: Device on which the filter was set
4339 * @rxq_index: RX queue index
4340 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
4341 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
4342 *
4343 * Drivers that implement ndo_rx_flow_steer() should periodically call
4344 * this function for each installed filter and remove the filters for
4345 * which it returns %true.
4346 */
4347bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
4348 u32 flow_id, u16 filter_id)
4349{
4350 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
4351 struct rps_dev_flow_table *flow_table;
4352 struct rps_dev_flow *rflow;
4353 bool expire = true;
a31196b0 4354 unsigned int cpu;
c445477d
BH
4355
4356 rcu_read_lock();
4357 flow_table = rcu_dereference(rxqueue->rps_flow_table);
4358 if (flow_table && flow_id <= flow_table->mask) {
4359 rflow = &flow_table->flows[flow_id];
6aa7de05 4360 cpu = READ_ONCE(rflow->cpu);
a31196b0 4361 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
c445477d
BH
4362 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
4363 rflow->last_qtail) <
4364 (int)(10 * flow_table->mask)))
4365 expire = false;
4366 }
4367 rcu_read_unlock();
4368 return expire;
4369}
4370EXPORT_SYMBOL(rps_may_expire_flow);
4371
4372#endif /* CONFIG_RFS_ACCEL */
4373
0a9627f2 4374/* Called from hardirq (IPI) context */
e36fa2f7 4375static void rps_trigger_softirq(void *data)
0a9627f2 4376{
e36fa2f7
ED
4377 struct softnet_data *sd = data;
4378
eecfd7c4 4379 ____napi_schedule(sd, &sd->backlog);
dee42870 4380 sd->received_rps++;
0a9627f2 4381}
e36fa2f7 4382
fec5e652 4383#endif /* CONFIG_RPS */
0a9627f2 4384
e36fa2f7
ED
4385/*
4386 * Check if this softnet_data structure is another cpu one
4387 * If yes, queue it to our IPI list and return 1
4388 * If no, return 0
4389 */
4390static int rps_ipi_queued(struct softnet_data *sd)
4391{
4392#ifdef CONFIG_RPS
903ceff7 4393 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
e36fa2f7
ED
4394
4395 if (sd != mysd) {
4396 sd->rps_ipi_next = mysd->rps_ipi_list;
4397 mysd->rps_ipi_list = sd;
4398
4399 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
4400 return 1;
4401 }
4402#endif /* CONFIG_RPS */
4403 return 0;
4404}
4405
99bbc707
WB
4406#ifdef CONFIG_NET_FLOW_LIMIT
4407int netdev_flow_limit_table_len __read_mostly = (1 << 12);
4408#endif
4409
4410static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
4411{
4412#ifdef CONFIG_NET_FLOW_LIMIT
4413 struct sd_flow_limit *fl;
4414 struct softnet_data *sd;
4415 unsigned int old_flow, new_flow;
4416
4417 if (qlen < (netdev_max_backlog >> 1))
4418 return false;
4419
903ceff7 4420 sd = this_cpu_ptr(&softnet_data);
99bbc707
WB
4421
4422 rcu_read_lock();
4423 fl = rcu_dereference(sd->flow_limit);
4424 if (fl) {
3958afa1 4425 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
99bbc707
WB
4426 old_flow = fl->history[fl->history_head];
4427 fl->history[fl->history_head] = new_flow;
4428
4429 fl->history_head++;
4430 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
4431
4432 if (likely(fl->buckets[old_flow]))
4433 fl->buckets[old_flow]--;
4434
4435 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
4436 fl->count++;
4437 rcu_read_unlock();
4438 return true;
4439 }
4440 }
4441 rcu_read_unlock();
4442#endif
4443 return false;
4444}
4445
0a9627f2
TH
4446/*
4447 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
4448 * queue (may be a remote CPU queue).
4449 */
fec5e652
TH
4450static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
4451 unsigned int *qtail)
0a9627f2 4452{
e36fa2f7 4453 struct softnet_data *sd;
0a9627f2 4454 unsigned long flags;
99bbc707 4455 unsigned int qlen;
0a9627f2 4456
e36fa2f7 4457 sd = &per_cpu(softnet_data, cpu);
0a9627f2
TH
4458
4459 local_irq_save(flags);
0a9627f2 4460
e36fa2f7 4461 rps_lock(sd);
e9e4dd32
JA
4462 if (!netif_running(skb->dev))
4463 goto drop;
99bbc707
WB
4464 qlen = skb_queue_len(&sd->input_pkt_queue);
4465 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
e008f3f0 4466 if (qlen) {
0a9627f2 4467enqueue:
e36fa2f7 4468 __skb_queue_tail(&sd->input_pkt_queue, skb);
76cc8b13 4469 input_queue_tail_incr_save(sd, qtail);
e36fa2f7 4470 rps_unlock(sd);
152102c7 4471 local_irq_restore(flags);
0a9627f2
TH
4472 return NET_RX_SUCCESS;
4473 }
4474
ebda37c2
ED
4475 /* Schedule NAPI for backlog device
4476 * We can use non atomic operation since we own the queue lock
4477 */
4478 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
e36fa2f7 4479 if (!rps_ipi_queued(sd))
eecfd7c4 4480 ____napi_schedule(sd, &sd->backlog);
0a9627f2
TH
4481 }
4482 goto enqueue;
4483 }
4484
e9e4dd32 4485drop:
dee42870 4486 sd->dropped++;
e36fa2f7 4487 rps_unlock(sd);
0a9627f2 4488
0a9627f2
TH
4489 local_irq_restore(flags);
4490
caf586e5 4491 atomic_long_inc(&skb->dev->rx_dropped);
0a9627f2
TH
4492 kfree_skb(skb);
4493 return NET_RX_DROP;
4494}
1da177e4 4495
e817f856
JDB
4496static struct netdev_rx_queue *netif_get_rxqueue(struct sk_buff *skb)
4497{
4498 struct net_device *dev = skb->dev;
4499 struct netdev_rx_queue *rxqueue;
4500
4501 rxqueue = dev->_rx;
4502
4503 if (skb_rx_queue_recorded(skb)) {
4504 u16 index = skb_get_rx_queue(skb);
4505
4506 if (unlikely(index >= dev->real_num_rx_queues)) {
4507 WARN_ONCE(dev->real_num_rx_queues > 1,
4508 "%s received packet on queue %u, but number "
4509 "of RX queues is %u\n",
4510 dev->name, index, dev->real_num_rx_queues);
4511
4512 return rxqueue; /* Return first rxqueue */
4513 }
4514 rxqueue += index;
4515 }
4516 return rxqueue;
4517}
4518
d4455169 4519static u32 netif_receive_generic_xdp(struct sk_buff *skb,
02671e23 4520 struct xdp_buff *xdp,
d4455169
JF
4521 struct bpf_prog *xdp_prog)
4522{
e817f856 4523 struct netdev_rx_queue *rxqueue;
198d83bb 4524 void *orig_data, *orig_data_end;
de8f3a83 4525 u32 metalen, act = XDP_DROP;
29724956
JDB
4526 __be16 orig_eth_type;
4527 struct ethhdr *eth;
4528 bool orig_bcast;
d4455169
JF
4529 int hlen, off;
4530 u32 mac_len;
4531
4532 /* Reinjected packets coming from act_mirred or similar should
4533 * not get XDP generic processing.
4534 */
ad1e03b2 4535 if (skb_is_tc_redirected(skb))
d4455169
JF
4536 return XDP_PASS;
4537
de8f3a83
DB
4538 /* XDP packets must be linear and must have sufficient headroom
4539 * of XDP_PACKET_HEADROOM bytes. This is the guarantee that also
4540 * native XDP provides, thus we need to do it here as well.
4541 */
ad1e03b2 4542 if (skb_cloned(skb) || skb_is_nonlinear(skb) ||
de8f3a83
DB
4543 skb_headroom(skb) < XDP_PACKET_HEADROOM) {
4544 int hroom = XDP_PACKET_HEADROOM - skb_headroom(skb);
4545 int troom = skb->tail + skb->data_len - skb->end;
4546
4547 /* In case we have to go down the path and also linearize,
4548 * then lets do the pskb_expand_head() work just once here.
4549 */
4550 if (pskb_expand_head(skb,
4551 hroom > 0 ? ALIGN(hroom, NET_SKB_PAD) : 0,
4552 troom > 0 ? troom + 128 : 0, GFP_ATOMIC))
4553 goto do_drop;
2d17d8d7 4554 if (skb_linearize(skb))
de8f3a83
DB
4555 goto do_drop;
4556 }
d4455169
JF
4557
4558 /* The XDP program wants to see the packet starting at the MAC
4559 * header.
4560 */
4561 mac_len = skb->data - skb_mac_header(skb);
4562 hlen = skb_headlen(skb) + mac_len;
02671e23
BT
4563 xdp->data = skb->data - mac_len;
4564 xdp->data_meta = xdp->data;
4565 xdp->data_end = xdp->data + hlen;
4566 xdp->data_hard_start = skb->data - skb_headroom(skb);
4567 orig_data_end = xdp->data_end;
4568 orig_data = xdp->data;
29724956
JDB
4569 eth = (struct ethhdr *)xdp->data;
4570 orig_bcast = is_multicast_ether_addr_64bits(eth->h_dest);
4571 orig_eth_type = eth->h_proto;
d4455169 4572
e817f856 4573 rxqueue = netif_get_rxqueue(skb);
02671e23 4574 xdp->rxq = &rxqueue->xdp_rxq;
e817f856 4575
02671e23 4576 act = bpf_prog_run_xdp(xdp_prog, xdp);
d4455169 4577
065af355 4578 /* check if bpf_xdp_adjust_head was used */
02671e23 4579 off = xdp->data - orig_data;
065af355
JDB
4580 if (off) {
4581 if (off > 0)
4582 __skb_pull(skb, off);
4583 else if (off < 0)
4584 __skb_push(skb, -off);
4585
4586 skb->mac_header += off;
4587 skb_reset_network_header(skb);
4588 }
d4455169 4589
198d83bb
NS
4590 /* check if bpf_xdp_adjust_tail was used. it can only "shrink"
4591 * pckt.
4592 */
02671e23 4593 off = orig_data_end - xdp->data_end;
f7613120 4594 if (off != 0) {
02671e23 4595 skb_set_tail_pointer(skb, xdp->data_end - xdp->data);
f7613120 4596 skb->len -= off;
02671e23 4597
f7613120 4598 }
198d83bb 4599
29724956
JDB
4600 /* check if XDP changed eth hdr such SKB needs update */
4601 eth = (struct ethhdr *)xdp->data;
4602 if ((orig_eth_type != eth->h_proto) ||
4603 (orig_bcast != is_multicast_ether_addr_64bits(eth->h_dest))) {
4604 __skb_push(skb, ETH_HLEN);
4605 skb->protocol = eth_type_trans(skb, skb->dev);
4606 }
4607
d4455169 4608 switch (act) {
6103aa96 4609 case XDP_REDIRECT:
d4455169
JF
4610 case XDP_TX:
4611 __skb_push(skb, mac_len);
de8f3a83 4612 break;
d4455169 4613 case XDP_PASS:
02671e23 4614 metalen = xdp->data - xdp->data_meta;
de8f3a83
DB
4615 if (metalen)
4616 skb_metadata_set(skb, metalen);
d4455169 4617 break;
d4455169
JF
4618 default:
4619 bpf_warn_invalid_xdp_action(act);
4620 /* fall through */
4621 case XDP_ABORTED:
4622 trace_xdp_exception(skb->dev, xdp_prog, act);
4623 /* fall through */
4624 case XDP_DROP:
4625 do_drop:
4626 kfree_skb(skb);
4627 break;
4628 }
4629
4630 return act;
4631}
4632
4633/* When doing generic XDP we have to bypass the qdisc layer and the
4634 * network taps in order to match in-driver-XDP behavior.
4635 */
7c497478 4636void generic_xdp_tx(struct sk_buff *skb, struct bpf_prog *xdp_prog)
d4455169
JF
4637{
4638 struct net_device *dev = skb->dev;
4639 struct netdev_queue *txq;
4640 bool free_skb = true;
4641 int cpu, rc;
4642
4bd97d51 4643 txq = netdev_core_pick_tx(dev, skb, NULL);
d4455169
JF
4644 cpu = smp_processor_id();
4645 HARD_TX_LOCK(dev, txq, cpu);
4646 if (!netif_xmit_stopped(txq)) {
4647 rc = netdev_start_xmit(skb, dev, txq, 0);
4648 if (dev_xmit_complete(rc))
4649 free_skb = false;
4650 }
4651 HARD_TX_UNLOCK(dev, txq);
4652 if (free_skb) {
4653 trace_xdp_exception(dev, xdp_prog, XDP_TX);
4654 kfree_skb(skb);
4655 }
4656}
7c497478 4657EXPORT_SYMBOL_GPL(generic_xdp_tx);
d4455169 4658
02786475 4659static DEFINE_STATIC_KEY_FALSE(generic_xdp_needed_key);
d4455169 4660
7c497478 4661int do_xdp_generic(struct bpf_prog *xdp_prog, struct sk_buff *skb)
d4455169 4662{
d4455169 4663 if (xdp_prog) {
02671e23
BT
4664 struct xdp_buff xdp;
4665 u32 act;
6103aa96 4666 int err;
d4455169 4667
02671e23 4668 act = netif_receive_generic_xdp(skb, &xdp, xdp_prog);
d4455169 4669 if (act != XDP_PASS) {
6103aa96
JF
4670 switch (act) {
4671 case XDP_REDIRECT:
2facaad6 4672 err = xdp_do_generic_redirect(skb->dev, skb,
02671e23 4673 &xdp, xdp_prog);
6103aa96
JF
4674 if (err)
4675 goto out_redir;
02671e23 4676 break;
6103aa96 4677 case XDP_TX:
d4455169 4678 generic_xdp_tx(skb, xdp_prog);
6103aa96
JF
4679 break;
4680 }
d4455169
JF
4681 return XDP_DROP;
4682 }
4683 }
4684 return XDP_PASS;
6103aa96 4685out_redir:
6103aa96
JF
4686 kfree_skb(skb);
4687 return XDP_DROP;
d4455169 4688}
7c497478 4689EXPORT_SYMBOL_GPL(do_xdp_generic);
d4455169 4690
ae78dbfa 4691static int netif_rx_internal(struct sk_buff *skb)
1da177e4 4692{
b0e28f1e 4693 int ret;
1da177e4 4694
588f0330 4695 net_timestamp_check(netdev_tstamp_prequeue, skb);
1da177e4 4696
cf66ba58 4697 trace_netif_rx(skb);
d4455169 4698
df334545 4699#ifdef CONFIG_RPS
dc05360f 4700 if (static_branch_unlikely(&rps_needed)) {
fec5e652 4701 struct rps_dev_flow voidflow, *rflow = &voidflow;
b0e28f1e
ED
4702 int cpu;
4703
cece1945 4704 preempt_disable();
b0e28f1e 4705 rcu_read_lock();
fec5e652
TH
4706
4707 cpu = get_rps_cpu(skb->dev, skb, &rflow);
b0e28f1e
ED
4708 if (cpu < 0)
4709 cpu = smp_processor_id();
fec5e652
TH
4710
4711 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
4712
b0e28f1e 4713 rcu_read_unlock();
cece1945 4714 preempt_enable();
adc9300e
ED
4715 } else
4716#endif
fec5e652
TH
4717 {
4718 unsigned int qtail;
f4563a75 4719
fec5e652
TH
4720 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
4721 put_cpu();
4722 }
b0e28f1e 4723 return ret;
1da177e4 4724}
ae78dbfa
BH
4725
4726/**
4727 * netif_rx - post buffer to the network code
4728 * @skb: buffer to post
4729 *
4730 * This function receives a packet from a device driver and queues it for
4731 * the upper (protocol) levels to process. It always succeeds. The buffer
4732 * may be dropped during processing for congestion control or by the
4733 * protocol layers.
4734 *
4735 * return values:
4736 * NET_RX_SUCCESS (no congestion)
4737 * NET_RX_DROP (packet was dropped)
4738 *
4739 */
4740
4741int netif_rx(struct sk_buff *skb)
4742{
b0e3f1bd
GB
4743 int ret;
4744
ae78dbfa
BH
4745 trace_netif_rx_entry(skb);
4746
b0e3f1bd
GB
4747 ret = netif_rx_internal(skb);
4748 trace_netif_rx_exit(ret);
4749
4750 return ret;
ae78dbfa 4751}
d1b19dff 4752EXPORT_SYMBOL(netif_rx);
1da177e4
LT
4753
4754int netif_rx_ni(struct sk_buff *skb)
4755{
4756 int err;
4757
ae78dbfa
BH
4758 trace_netif_rx_ni_entry(skb);
4759
1da177e4 4760 preempt_disable();
ae78dbfa 4761 err = netif_rx_internal(skb);
1da177e4
LT
4762 if (local_softirq_pending())
4763 do_softirq();
4764 preempt_enable();
b0e3f1bd 4765 trace_netif_rx_ni_exit(err);
1da177e4
LT
4766
4767 return err;
4768}
1da177e4
LT
4769EXPORT_SYMBOL(netif_rx_ni);
4770
0766f788 4771static __latent_entropy void net_tx_action(struct softirq_action *h)
1da177e4 4772{
903ceff7 4773 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
1da177e4
LT
4774
4775 if (sd->completion_queue) {
4776 struct sk_buff *clist;
4777
4778 local_irq_disable();
4779 clist = sd->completion_queue;
4780 sd->completion_queue = NULL;
4781 local_irq_enable();
4782
4783 while (clist) {
4784 struct sk_buff *skb = clist;
f4563a75 4785
1da177e4
LT
4786 clist = clist->next;
4787
63354797 4788 WARN_ON(refcount_read(&skb->users));
e6247027
ED
4789 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
4790 trace_consume_skb(skb);
4791 else
4792 trace_kfree_skb(skb, net_tx_action);
15fad714
JDB
4793
4794 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
4795 __kfree_skb(skb);
4796 else
4797 __kfree_skb_defer(skb);
1da177e4 4798 }
15fad714
JDB
4799
4800 __kfree_skb_flush();
1da177e4
LT
4801 }
4802
4803 if (sd->output_queue) {
37437bb2 4804 struct Qdisc *head;
1da177e4
LT
4805
4806 local_irq_disable();
4807 head = sd->output_queue;
4808 sd->output_queue = NULL;
a9cbd588 4809 sd->output_queue_tailp = &sd->output_queue;
1da177e4
LT
4810 local_irq_enable();
4811
4812 while (head) {
37437bb2 4813 struct Qdisc *q = head;
6b3ba914 4814 spinlock_t *root_lock = NULL;
37437bb2 4815
1da177e4
LT
4816 head = head->next_sched;
4817
6b3ba914
JF
4818 if (!(q->flags & TCQ_F_NOLOCK)) {
4819 root_lock = qdisc_lock(q);
4820 spin_lock(root_lock);
4821 }
3bcb846c
ED
4822 /* We need to make sure head->next_sched is read
4823 * before clearing __QDISC_STATE_SCHED
4824 */
4825 smp_mb__before_atomic();
4826 clear_bit(__QDISC_STATE_SCHED, &q->state);
4827 qdisc_run(q);
6b3ba914
JF
4828 if (root_lock)
4829 spin_unlock(root_lock);
1da177e4
LT
4830 }
4831 }
f53c7239
SK
4832
4833 xfrm_dev_backlog(sd);
1da177e4
LT
4834}
4835
181402a5 4836#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
da678292
MM
4837/* This hook is defined here for ATM LANE */
4838int (*br_fdb_test_addr_hook)(struct net_device *dev,
4839 unsigned char *addr) __read_mostly;
4fb019a0 4840EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
da678292 4841#endif
1da177e4 4842
1f211a1b
DB
4843static inline struct sk_buff *
4844sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
4845 struct net_device *orig_dev)
f697c3e8 4846{
e7582bab 4847#ifdef CONFIG_NET_CLS_ACT
46209401 4848 struct mini_Qdisc *miniq = rcu_dereference_bh(skb->dev->miniq_ingress);
d2788d34 4849 struct tcf_result cl_res;
24824a09 4850
c9e99fd0
DB
4851 /* If there's at least one ingress present somewhere (so
4852 * we get here via enabled static key), remaining devices
4853 * that are not configured with an ingress qdisc will bail
d2788d34 4854 * out here.
c9e99fd0 4855 */
46209401 4856 if (!miniq)
4577139b 4857 return skb;
46209401 4858
f697c3e8
HX
4859 if (*pt_prev) {
4860 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4861 *pt_prev = NULL;
1da177e4
LT
4862 }
4863
3365495c 4864 qdisc_skb_cb(skb)->pkt_len = skb->len;
8dc07fdb 4865 skb->tc_at_ingress = 1;
46209401 4866 mini_qdisc_bstats_cpu_update(miniq, skb);
c9e99fd0 4867
46209401 4868 switch (tcf_classify(skb, miniq->filter_list, &cl_res, false)) {
d2788d34
DB
4869 case TC_ACT_OK:
4870 case TC_ACT_RECLASSIFY:
4871 skb->tc_index = TC_H_MIN(cl_res.classid);
4872 break;
4873 case TC_ACT_SHOT:
46209401 4874 mini_qdisc_qstats_cpu_drop(miniq);
8a3a4c6e
ED
4875 kfree_skb(skb);
4876 return NULL;
d2788d34
DB
4877 case TC_ACT_STOLEN:
4878 case TC_ACT_QUEUED:
e25ea21f 4879 case TC_ACT_TRAP:
8a3a4c6e 4880 consume_skb(skb);
d2788d34 4881 return NULL;
27b29f63
AS
4882 case TC_ACT_REDIRECT:
4883 /* skb_mac_header check was done by cls/act_bpf, so
4884 * we can safely push the L2 header back before
4885 * redirecting to another netdev
4886 */
4887 __skb_push(skb, skb->mac_len);
4888 skb_do_redirect(skb);
4889 return NULL;
720f22fe 4890 case TC_ACT_CONSUMED:
cd11b164 4891 return NULL;
d2788d34
DB
4892 default:
4893 break;
f697c3e8 4894 }
e7582bab 4895#endif /* CONFIG_NET_CLS_ACT */
e687ad60
PN
4896 return skb;
4897}
1da177e4 4898
24b27fc4
MB
4899/**
4900 * netdev_is_rx_handler_busy - check if receive handler is registered
4901 * @dev: device to check
4902 *
4903 * Check if a receive handler is already registered for a given device.
4904 * Return true if there one.
4905 *
4906 * The caller must hold the rtnl_mutex.
4907 */
4908bool netdev_is_rx_handler_busy(struct net_device *dev)
4909{
4910 ASSERT_RTNL();
4911 return dev && rtnl_dereference(dev->rx_handler);
4912}
4913EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
4914
ab95bfe0
JP
4915/**
4916 * netdev_rx_handler_register - register receive handler
4917 * @dev: device to register a handler for
4918 * @rx_handler: receive handler to register
93e2c32b 4919 * @rx_handler_data: data pointer that is used by rx handler
ab95bfe0 4920 *
e227867f 4921 * Register a receive handler for a device. This handler will then be
ab95bfe0
JP
4922 * called from __netif_receive_skb. A negative errno code is returned
4923 * on a failure.
4924 *
4925 * The caller must hold the rtnl_mutex.
8a4eb573
JP
4926 *
4927 * For a general description of rx_handler, see enum rx_handler_result.
ab95bfe0
JP
4928 */
4929int netdev_rx_handler_register(struct net_device *dev,
93e2c32b
JP
4930 rx_handler_func_t *rx_handler,
4931 void *rx_handler_data)
ab95bfe0 4932{
1b7cd004 4933 if (netdev_is_rx_handler_busy(dev))
ab95bfe0
JP
4934 return -EBUSY;
4935
f5426250
PA
4936 if (dev->priv_flags & IFF_NO_RX_HANDLER)
4937 return -EINVAL;
4938
00cfec37 4939 /* Note: rx_handler_data must be set before rx_handler */
93e2c32b 4940 rcu_assign_pointer(dev->rx_handler_data, rx_handler_data);
ab95bfe0
JP
4941 rcu_assign_pointer(dev->rx_handler, rx_handler);
4942
4943 return 0;
4944}
4945EXPORT_SYMBOL_GPL(netdev_rx_handler_register);
4946
4947/**
4948 * netdev_rx_handler_unregister - unregister receive handler
4949 * @dev: device to unregister a handler from
4950 *
166ec369 4951 * Unregister a receive handler from a device.
ab95bfe0
JP
4952 *
4953 * The caller must hold the rtnl_mutex.
4954 */
4955void netdev_rx_handler_unregister(struct net_device *dev)
4956{
4957
4958 ASSERT_RTNL();
a9b3cd7f 4959 RCU_INIT_POINTER(dev->rx_handler, NULL);
00cfec37
ED
4960 /* a reader seeing a non NULL rx_handler in a rcu_read_lock()
4961 * section has a guarantee to see a non NULL rx_handler_data
4962 * as well.
4963 */
4964 synchronize_net();
a9b3cd7f 4965 RCU_INIT_POINTER(dev->rx_handler_data, NULL);
ab95bfe0
JP
4966}
4967EXPORT_SYMBOL_GPL(netdev_rx_handler_unregister);
4968
b4b9e355
MG
4969/*
4970 * Limit the use of PFMEMALLOC reserves to those protocols that implement
4971 * the special handling of PFMEMALLOC skbs.
4972 */
4973static bool skb_pfmemalloc_protocol(struct sk_buff *skb)
4974{
4975 switch (skb->protocol) {
2b8837ae
JP
4976 case htons(ETH_P_ARP):
4977 case htons(ETH_P_IP):
4978 case htons(ETH_P_IPV6):
4979 case htons(ETH_P_8021Q):
4980 case htons(ETH_P_8021AD):
b4b9e355
MG
4981 return true;
4982 default:
4983 return false;
4984 }
4985}
4986
e687ad60
PN
4987static inline int nf_ingress(struct sk_buff *skb, struct packet_type **pt_prev,
4988 int *ret, struct net_device *orig_dev)
4989{
4990 if (nf_hook_ingress_active(skb)) {
2c1e2703
AC
4991 int ingress_retval;
4992
e687ad60
PN
4993 if (*pt_prev) {
4994 *ret = deliver_skb(skb, *pt_prev, orig_dev);
4995 *pt_prev = NULL;
4996 }
4997
2c1e2703
AC
4998 rcu_read_lock();
4999 ingress_retval = nf_hook_ingress(skb);
5000 rcu_read_unlock();
5001 return ingress_retval;
e687ad60
PN
5002 }
5003 return 0;
5004}
e687ad60 5005
88eb1944
EC
5006static int __netif_receive_skb_core(struct sk_buff *skb, bool pfmemalloc,
5007 struct packet_type **ppt_prev)
1da177e4
LT
5008{
5009 struct packet_type *ptype, *pt_prev;
ab95bfe0 5010 rx_handler_func_t *rx_handler;
f2ccd8fa 5011 struct net_device *orig_dev;
8a4eb573 5012 bool deliver_exact = false;
1da177e4 5013 int ret = NET_RX_DROP;
252e3346 5014 __be16 type;
1da177e4 5015
588f0330 5016 net_timestamp_check(!netdev_tstamp_prequeue, skb);
81bbb3d4 5017
cf66ba58 5018 trace_netif_receive_skb(skb);
9b22ea56 5019
cc9bd5ce 5020 orig_dev = skb->dev;
8f903c70 5021
c1d2bbe1 5022 skb_reset_network_header(skb);
fda55eca
ED
5023 if (!skb_transport_header_was_set(skb))
5024 skb_reset_transport_header(skb);
0b5c9db1 5025 skb_reset_mac_len(skb);
1da177e4
LT
5026
5027 pt_prev = NULL;
5028
63d8ea7f 5029another_round:
b6858177 5030 skb->skb_iif = skb->dev->ifindex;
63d8ea7f
DM
5031
5032 __this_cpu_inc(softnet_data.processed);
5033
458bf2f2
SH
5034 if (static_branch_unlikely(&generic_xdp_needed_key)) {
5035 int ret2;
5036
5037 preempt_disable();
5038 ret2 = do_xdp_generic(rcu_dereference(skb->dev->xdp_prog), skb);
5039 preempt_enable();
5040
5041 if (ret2 != XDP_PASS)
5042 return NET_RX_DROP;
5043 skb_reset_mac_len(skb);
5044 }
5045
8ad227ff
PM
5046 if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5047 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
0d5501c1 5048 skb = skb_vlan_untag(skb);
bcc6d479 5049 if (unlikely(!skb))
2c17d27c 5050 goto out;
bcc6d479
JP
5051 }
5052
e7246e12
WB
5053 if (skb_skip_tc_classify(skb))
5054 goto skip_classify;
1da177e4 5055
9754e293 5056 if (pfmemalloc)
b4b9e355
MG
5057 goto skip_taps;
5058
1da177e4 5059 list_for_each_entry_rcu(ptype, &ptype_all, list) {
7866a621
SN
5060 if (pt_prev)
5061 ret = deliver_skb(skb, pt_prev, orig_dev);
5062 pt_prev = ptype;
5063 }
5064
5065 list_for_each_entry_rcu(ptype, &skb->dev->ptype_all, list) {
5066 if (pt_prev)
5067 ret = deliver_skb(skb, pt_prev, orig_dev);
5068 pt_prev = ptype;
1da177e4
LT
5069 }
5070
b4b9e355 5071skip_taps:
1cf51900 5072#ifdef CONFIG_NET_INGRESS
aabf6772 5073 if (static_branch_unlikely(&ingress_needed_key)) {
1f211a1b 5074 skb = sch_handle_ingress(skb, &pt_prev, &ret, orig_dev);
4577139b 5075 if (!skb)
2c17d27c 5076 goto out;
e687ad60
PN
5077
5078 if (nf_ingress(skb, &pt_prev, &ret, orig_dev) < 0)
2c17d27c 5079 goto out;
4577139b 5080 }
1cf51900 5081#endif
a5135bcf 5082 skb_reset_tc(skb);
e7246e12 5083skip_classify:
9754e293 5084 if (pfmemalloc && !skb_pfmemalloc_protocol(skb))
b4b9e355
MG
5085 goto drop;
5086
df8a39de 5087 if (skb_vlan_tag_present(skb)) {
2425717b
JF
5088 if (pt_prev) {
5089 ret = deliver_skb(skb, pt_prev, orig_dev);
5090 pt_prev = NULL;
5091 }
48cc32d3 5092 if (vlan_do_receive(&skb))
2425717b
JF
5093 goto another_round;
5094 else if (unlikely(!skb))
2c17d27c 5095 goto out;
2425717b
JF
5096 }
5097
48cc32d3 5098 rx_handler = rcu_dereference(skb->dev->rx_handler);
ab95bfe0
JP
5099 if (rx_handler) {
5100 if (pt_prev) {
5101 ret = deliver_skb(skb, pt_prev, orig_dev);
5102 pt_prev = NULL;
5103 }
8a4eb573
JP
5104 switch (rx_handler(&skb)) {
5105 case RX_HANDLER_CONSUMED:
3bc1b1ad 5106 ret = NET_RX_SUCCESS;
2c17d27c 5107 goto out;
8a4eb573 5108 case RX_HANDLER_ANOTHER:
63d8ea7f 5109 goto another_round;
8a4eb573
JP
5110 case RX_HANDLER_EXACT:
5111 deliver_exact = true;
5112 case RX_HANDLER_PASS:
5113 break;
5114 default:
5115 BUG();
5116 }
ab95bfe0 5117 }
1da177e4 5118
df8a39de 5119 if (unlikely(skb_vlan_tag_present(skb))) {
36b2f61a
GV
5120check_vlan_id:
5121 if (skb_vlan_tag_get_id(skb)) {
5122 /* Vlan id is non 0 and vlan_do_receive() above couldn't
5123 * find vlan device.
5124 */
d4b812de 5125 skb->pkt_type = PACKET_OTHERHOST;
36b2f61a
GV
5126 } else if (skb->protocol == cpu_to_be16(ETH_P_8021Q) ||
5127 skb->protocol == cpu_to_be16(ETH_P_8021AD)) {
5128 /* Outer header is 802.1P with vlan 0, inner header is
5129 * 802.1Q or 802.1AD and vlan_do_receive() above could
5130 * not find vlan dev for vlan id 0.
5131 */
5132 __vlan_hwaccel_clear_tag(skb);
5133 skb = skb_vlan_untag(skb);
5134 if (unlikely(!skb))
5135 goto out;
5136 if (vlan_do_receive(&skb))
5137 /* After stripping off 802.1P header with vlan 0
5138 * vlan dev is found for inner header.
5139 */
5140 goto another_round;
5141 else if (unlikely(!skb))
5142 goto out;
5143 else
5144 /* We have stripped outer 802.1P vlan 0 header.
5145 * But could not find vlan dev.
5146 * check again for vlan id to set OTHERHOST.
5147 */
5148 goto check_vlan_id;
5149 }
d4b812de
ED
5150 /* Note: we might in the future use prio bits
5151 * and set skb->priority like in vlan_do_receive()
5152 * For the time being, just ignore Priority Code Point
5153 */
b1817524 5154 __vlan_hwaccel_clear_tag(skb);
d4b812de 5155 }
48cc32d3 5156
7866a621
SN
5157 type = skb->protocol;
5158
63d8ea7f 5159 /* deliver only exact match when indicated */
7866a621
SN
5160 if (likely(!deliver_exact)) {
5161 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5162 &ptype_base[ntohs(type) &
5163 PTYPE_HASH_MASK]);
5164 }
1f3c8804 5165
7866a621
SN
5166 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5167 &orig_dev->ptype_specific);
5168
5169 if (unlikely(skb->dev != orig_dev)) {
5170 deliver_ptype_list_skb(skb, &pt_prev, orig_dev, type,
5171 &skb->dev->ptype_specific);
1da177e4
LT
5172 }
5173
5174 if (pt_prev) {
1f8b977a 5175 if (unlikely(skb_orphan_frags_rx(skb, GFP_ATOMIC)))
0e698bf6 5176 goto drop;
88eb1944 5177 *ppt_prev = pt_prev;
1da177e4 5178 } else {
b4b9e355 5179drop:
6e7333d3
JW
5180 if (!deliver_exact)
5181 atomic_long_inc(&skb->dev->rx_dropped);
5182 else
5183 atomic_long_inc(&skb->dev->rx_nohandler);
1da177e4
LT
5184 kfree_skb(skb);
5185 /* Jamal, now you will not able to escape explaining
5186 * me how you were going to use this. :-)
5187 */
5188 ret = NET_RX_DROP;
5189 }
5190
2c17d27c 5191out:
9754e293
DM
5192 return ret;
5193}
5194
88eb1944
EC
5195static int __netif_receive_skb_one_core(struct sk_buff *skb, bool pfmemalloc)
5196{
5197 struct net_device *orig_dev = skb->dev;
5198 struct packet_type *pt_prev = NULL;
5199 int ret;
5200
5201 ret = __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
5202 if (pt_prev)
f5737cba
PA
5203 ret = INDIRECT_CALL_INET(pt_prev->func, ipv6_rcv, ip_rcv, skb,
5204 skb->dev, pt_prev, orig_dev);
88eb1944
EC
5205 return ret;
5206}
5207
1c601d82
JDB
5208/**
5209 * netif_receive_skb_core - special purpose version of netif_receive_skb
5210 * @skb: buffer to process
5211 *
5212 * More direct receive version of netif_receive_skb(). It should
5213 * only be used by callers that have a need to skip RPS and Generic XDP.
5214 * Caller must also take care of handling if (page_is_)pfmemalloc.
5215 *
5216 * This function may only be called from softirq context and interrupts
5217 * should be enabled.
5218 *
5219 * Return values (usually ignored):
5220 * NET_RX_SUCCESS: no congestion
5221 * NET_RX_DROP: packet was dropped
5222 */
5223int netif_receive_skb_core(struct sk_buff *skb)
5224{
5225 int ret;
5226
5227 rcu_read_lock();
88eb1944 5228 ret = __netif_receive_skb_one_core(skb, false);
1c601d82
JDB
5229 rcu_read_unlock();
5230
5231 return ret;
5232}
5233EXPORT_SYMBOL(netif_receive_skb_core);
5234
88eb1944
EC
5235static inline void __netif_receive_skb_list_ptype(struct list_head *head,
5236 struct packet_type *pt_prev,
5237 struct net_device *orig_dev)
4ce0017a
EC
5238{
5239 struct sk_buff *skb, *next;
5240
88eb1944
EC
5241 if (!pt_prev)
5242 return;
5243 if (list_empty(head))
5244 return;
17266ee9 5245 if (pt_prev->list_func != NULL)
fdf71426
PA
5246 INDIRECT_CALL_INET(pt_prev->list_func, ipv6_list_rcv,
5247 ip_list_rcv, head, pt_prev, orig_dev);
17266ee9 5248 else
9a5a90d1
AL
5249 list_for_each_entry_safe(skb, next, head, list) {
5250 skb_list_del_init(skb);
fdf71426 5251 pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
9a5a90d1 5252 }
88eb1944
EC
5253}
5254
5255static void __netif_receive_skb_list_core(struct list_head *head, bool pfmemalloc)
5256{
5257 /* Fast-path assumptions:
5258 * - There is no RX handler.
5259 * - Only one packet_type matches.
5260 * If either of these fails, we will end up doing some per-packet
5261 * processing in-line, then handling the 'last ptype' for the whole
5262 * sublist. This can't cause out-of-order delivery to any single ptype,
5263 * because the 'last ptype' must be constant across the sublist, and all
5264 * other ptypes are handled per-packet.
5265 */
5266 /* Current (common) ptype of sublist */
5267 struct packet_type *pt_curr = NULL;
5268 /* Current (common) orig_dev of sublist */
5269 struct net_device *od_curr = NULL;
5270 struct list_head sublist;
5271 struct sk_buff *skb, *next;
5272
9af86f93 5273 INIT_LIST_HEAD(&sublist);
88eb1944
EC
5274 list_for_each_entry_safe(skb, next, head, list) {
5275 struct net_device *orig_dev = skb->dev;
5276 struct packet_type *pt_prev = NULL;
5277
22f6bbb7 5278 skb_list_del_init(skb);
88eb1944 5279 __netif_receive_skb_core(skb, pfmemalloc, &pt_prev);
9af86f93
EC
5280 if (!pt_prev)
5281 continue;
88eb1944
EC
5282 if (pt_curr != pt_prev || od_curr != orig_dev) {
5283 /* dispatch old sublist */
88eb1944
EC
5284 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
5285 /* start new sublist */
9af86f93 5286 INIT_LIST_HEAD(&sublist);
88eb1944
EC
5287 pt_curr = pt_prev;
5288 od_curr = orig_dev;
5289 }
9af86f93 5290 list_add_tail(&skb->list, &sublist);
88eb1944
EC
5291 }
5292
5293 /* dispatch final sublist */
9af86f93 5294 __netif_receive_skb_list_ptype(&sublist, pt_curr, od_curr);
4ce0017a
EC
5295}
5296
9754e293
DM
5297static int __netif_receive_skb(struct sk_buff *skb)
5298{
5299 int ret;
5300
5301 if (sk_memalloc_socks() && skb_pfmemalloc(skb)) {
f1083048 5302 unsigned int noreclaim_flag;
9754e293
DM
5303
5304 /*
5305 * PFMEMALLOC skbs are special, they should
5306 * - be delivered to SOCK_MEMALLOC sockets only
5307 * - stay away from userspace
5308 * - have bounded memory usage
5309 *
5310 * Use PF_MEMALLOC as this saves us from propagating the allocation
5311 * context down to all allocation sites.
5312 */
f1083048 5313 noreclaim_flag = memalloc_noreclaim_save();
88eb1944 5314 ret = __netif_receive_skb_one_core(skb, true);
f1083048 5315 memalloc_noreclaim_restore(noreclaim_flag);
9754e293 5316 } else
88eb1944 5317 ret = __netif_receive_skb_one_core(skb, false);
9754e293 5318
1da177e4
LT
5319 return ret;
5320}
0a9627f2 5321
4ce0017a
EC
5322static void __netif_receive_skb_list(struct list_head *head)
5323{
5324 unsigned long noreclaim_flag = 0;
5325 struct sk_buff *skb, *next;
5326 bool pfmemalloc = false; /* Is current sublist PF_MEMALLOC? */
5327
5328 list_for_each_entry_safe(skb, next, head, list) {
5329 if ((sk_memalloc_socks() && skb_pfmemalloc(skb)) != pfmemalloc) {
5330 struct list_head sublist;
5331
5332 /* Handle the previous sublist */
5333 list_cut_before(&sublist, head, &skb->list);
b9f463d6
EC
5334 if (!list_empty(&sublist))
5335 __netif_receive_skb_list_core(&sublist, pfmemalloc);
4ce0017a
EC
5336 pfmemalloc = !pfmemalloc;
5337 /* See comments in __netif_receive_skb */
5338 if (pfmemalloc)
5339 noreclaim_flag = memalloc_noreclaim_save();
5340 else
5341 memalloc_noreclaim_restore(noreclaim_flag);
5342 }
5343 }
5344 /* Handle the remaining sublist */
b9f463d6
EC
5345 if (!list_empty(head))
5346 __netif_receive_skb_list_core(head, pfmemalloc);
4ce0017a
EC
5347 /* Restore pflags */
5348 if (pfmemalloc)
5349 memalloc_noreclaim_restore(noreclaim_flag);
5350}
5351
f4e63525 5352static int generic_xdp_install(struct net_device *dev, struct netdev_bpf *xdp)
b5cdae32 5353{
58038695 5354 struct bpf_prog *old = rtnl_dereference(dev->xdp_prog);
b5cdae32
DM
5355 struct bpf_prog *new = xdp->prog;
5356 int ret = 0;
5357
5358 switch (xdp->command) {
58038695 5359 case XDP_SETUP_PROG:
b5cdae32
DM
5360 rcu_assign_pointer(dev->xdp_prog, new);
5361 if (old)
5362 bpf_prog_put(old);
5363
5364 if (old && !new) {
02786475 5365 static_branch_dec(&generic_xdp_needed_key);
b5cdae32 5366 } else if (new && !old) {
02786475 5367 static_branch_inc(&generic_xdp_needed_key);
b5cdae32 5368 dev_disable_lro(dev);
56f5aa77 5369 dev_disable_gro_hw(dev);
b5cdae32
DM
5370 }
5371 break;
b5cdae32
DM
5372
5373 case XDP_QUERY_PROG:
58038695 5374 xdp->prog_id = old ? old->aux->id : 0;
b5cdae32
DM
5375 break;
5376
5377 default:
5378 ret = -EINVAL;
5379 break;
5380 }
5381
5382 return ret;
5383}
5384
ae78dbfa 5385static int netif_receive_skb_internal(struct sk_buff *skb)
0a9627f2 5386{
2c17d27c
JA
5387 int ret;
5388
588f0330 5389 net_timestamp_check(netdev_tstamp_prequeue, skb);
3b098e2d 5390
c1f19b51
RC
5391 if (skb_defer_rx_timestamp(skb))
5392 return NET_RX_SUCCESS;
5393
bbbe211c 5394 rcu_read_lock();
df334545 5395#ifdef CONFIG_RPS
dc05360f 5396 if (static_branch_unlikely(&rps_needed)) {
3b098e2d 5397 struct rps_dev_flow voidflow, *rflow = &voidflow;
2c17d27c 5398 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
0a9627f2 5399
3b098e2d
ED
5400 if (cpu >= 0) {
5401 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
5402 rcu_read_unlock();
adc9300e 5403 return ret;
3b098e2d 5404 }
fec5e652 5405 }
1e94d72f 5406#endif
2c17d27c
JA
5407 ret = __netif_receive_skb(skb);
5408 rcu_read_unlock();
5409 return ret;
0a9627f2 5410}
ae78dbfa 5411
7da517a3
EC
5412static void netif_receive_skb_list_internal(struct list_head *head)
5413{
7da517a3 5414 struct sk_buff *skb, *next;
8c057efa 5415 struct list_head sublist;
7da517a3 5416
8c057efa 5417 INIT_LIST_HEAD(&sublist);
7da517a3
EC
5418 list_for_each_entry_safe(skb, next, head, list) {
5419 net_timestamp_check(netdev_tstamp_prequeue, skb);
22f6bbb7 5420 skb_list_del_init(skb);
8c057efa
EC
5421 if (!skb_defer_rx_timestamp(skb))
5422 list_add_tail(&skb->list, &sublist);
7da517a3 5423 }
8c057efa 5424 list_splice_init(&sublist, head);
7da517a3 5425
7da517a3
EC
5426 rcu_read_lock();
5427#ifdef CONFIG_RPS
dc05360f 5428 if (static_branch_unlikely(&rps_needed)) {
7da517a3
EC
5429 list_for_each_entry_safe(skb, next, head, list) {
5430 struct rps_dev_flow voidflow, *rflow = &voidflow;
5431 int cpu = get_rps_cpu(skb->dev, skb, &rflow);
5432
5433 if (cpu >= 0) {
8c057efa 5434 /* Will be handled, remove from list */
22f6bbb7 5435 skb_list_del_init(skb);
8c057efa 5436 enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
7da517a3
EC
5437 }
5438 }
5439 }
5440#endif
5441 __netif_receive_skb_list(head);
5442 rcu_read_unlock();
5443}
5444
ae78dbfa
BH
5445/**
5446 * netif_receive_skb - process receive buffer from network
5447 * @skb: buffer to process
5448 *
5449 * netif_receive_skb() is the main receive data processing function.
5450 * It always succeeds. The buffer may be dropped during processing
5451 * for congestion control or by the protocol layers.
5452 *
5453 * This function may only be called from softirq context and interrupts
5454 * should be enabled.
5455 *
5456 * Return values (usually ignored):
5457 * NET_RX_SUCCESS: no congestion
5458 * NET_RX_DROP: packet was dropped
5459 */
04eb4489 5460int netif_receive_skb(struct sk_buff *skb)
ae78dbfa 5461{
b0e3f1bd
GB
5462 int ret;
5463
ae78dbfa
BH
5464 trace_netif_receive_skb_entry(skb);
5465
b0e3f1bd
GB
5466 ret = netif_receive_skb_internal(skb);
5467 trace_netif_receive_skb_exit(ret);
5468
5469 return ret;
ae78dbfa 5470}
04eb4489 5471EXPORT_SYMBOL(netif_receive_skb);
1da177e4 5472
f6ad8c1b
EC
5473/**
5474 * netif_receive_skb_list - process many receive buffers from network
5475 * @head: list of skbs to process.
5476 *
7da517a3
EC
5477 * Since return value of netif_receive_skb() is normally ignored, and
5478 * wouldn't be meaningful for a list, this function returns void.
f6ad8c1b
EC
5479 *
5480 * This function may only be called from softirq context and interrupts
5481 * should be enabled.
5482 */
5483void netif_receive_skb_list(struct list_head *head)
5484{
7da517a3 5485 struct sk_buff *skb;
f6ad8c1b 5486
b9f463d6
EC
5487 if (list_empty(head))
5488 return;
b0e3f1bd
GB
5489 if (trace_netif_receive_skb_list_entry_enabled()) {
5490 list_for_each_entry(skb, head, list)
5491 trace_netif_receive_skb_list_entry(skb);
5492 }
7da517a3 5493 netif_receive_skb_list_internal(head);
b0e3f1bd 5494 trace_netif_receive_skb_list_exit(0);
f6ad8c1b
EC
5495}
5496EXPORT_SYMBOL(netif_receive_skb_list);
5497
41852497 5498DEFINE_PER_CPU(struct work_struct, flush_works);
145dd5f9
PA
5499
5500/* Network device is going away, flush any packets still pending */
5501static void flush_backlog(struct work_struct *work)
6e583ce5 5502{
6e583ce5 5503 struct sk_buff *skb, *tmp;
145dd5f9
PA
5504 struct softnet_data *sd;
5505
5506 local_bh_disable();
5507 sd = this_cpu_ptr(&softnet_data);
6e583ce5 5508
145dd5f9 5509 local_irq_disable();
e36fa2f7 5510 rps_lock(sd);
6e7676c1 5511 skb_queue_walk_safe(&sd->input_pkt_queue, skb, tmp) {
41852497 5512 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
e36fa2f7 5513 __skb_unlink(skb, &sd->input_pkt_queue);
6e583ce5 5514 kfree_skb(skb);
76cc8b13 5515 input_queue_head_incr(sd);
6e583ce5 5516 }
6e7676c1 5517 }
e36fa2f7 5518 rps_unlock(sd);
145dd5f9 5519 local_irq_enable();
6e7676c1
CG
5520
5521 skb_queue_walk_safe(&sd->process_queue, skb, tmp) {
41852497 5522 if (skb->dev->reg_state == NETREG_UNREGISTERING) {
6e7676c1
CG
5523 __skb_unlink(skb, &sd->process_queue);
5524 kfree_skb(skb);
76cc8b13 5525 input_queue_head_incr(sd);
6e7676c1
CG
5526 }
5527 }
145dd5f9
PA
5528 local_bh_enable();
5529}
5530
41852497 5531static void flush_all_backlogs(void)
145dd5f9
PA
5532{
5533 unsigned int cpu;
5534
5535 get_online_cpus();
5536
41852497
ED
5537 for_each_online_cpu(cpu)
5538 queue_work_on(cpu, system_highpri_wq,
5539 per_cpu_ptr(&flush_works, cpu));
145dd5f9
PA
5540
5541 for_each_online_cpu(cpu)
41852497 5542 flush_work(per_cpu_ptr(&flush_works, cpu));
145dd5f9
PA
5543
5544 put_online_cpus();
6e583ce5
SH
5545}
5546
c8079432
MM
5547/* Pass the currently batched GRO_NORMAL SKBs up to the stack. */
5548static void gro_normal_list(struct napi_struct *napi)
5549{
5550 if (!napi->rx_count)
5551 return;
5552 netif_receive_skb_list_internal(&napi->rx_list);
5553 INIT_LIST_HEAD(&napi->rx_list);
5554 napi->rx_count = 0;
5555}
5556
5557/* Queue one GRO_NORMAL SKB up for list processing. If batch size exceeded,
5558 * pass the whole batch up to the stack.
5559 */
5560static void gro_normal_one(struct napi_struct *napi, struct sk_buff *skb)
5561{
5562 list_add_tail(&skb->list, &napi->rx_list);
5563 if (++napi->rx_count >= gro_normal_batch)
5564 gro_normal_list(napi);
5565}
5566
aaa5d90b
PA
5567INDIRECT_CALLABLE_DECLARE(int inet_gro_complete(struct sk_buff *, int));
5568INDIRECT_CALLABLE_DECLARE(int ipv6_gro_complete(struct sk_buff *, int));
c8079432 5569static int napi_gro_complete(struct napi_struct *napi, struct sk_buff *skb)
d565b0a1 5570{
22061d80 5571 struct packet_offload *ptype;
d565b0a1 5572 __be16 type = skb->protocol;
22061d80 5573 struct list_head *head = &offload_base;
d565b0a1
HX
5574 int err = -ENOENT;
5575
c3c7c254
ED
5576 BUILD_BUG_ON(sizeof(struct napi_gro_cb) > sizeof(skb->cb));
5577
fc59f9a3
HX
5578 if (NAPI_GRO_CB(skb)->count == 1) {
5579 skb_shinfo(skb)->gso_size = 0;
d565b0a1 5580 goto out;
fc59f9a3 5581 }
d565b0a1
HX
5582
5583 rcu_read_lock();
5584 list_for_each_entry_rcu(ptype, head, list) {
f191a1d1 5585 if (ptype->type != type || !ptype->callbacks.gro_complete)
d565b0a1
HX
5586 continue;
5587
aaa5d90b
PA
5588 err = INDIRECT_CALL_INET(ptype->callbacks.gro_complete,
5589 ipv6_gro_complete, inet_gro_complete,
5590 skb, 0);
d565b0a1
HX
5591 break;
5592 }
5593 rcu_read_unlock();
5594
5595 if (err) {
5596 WARN_ON(&ptype->list == head);
5597 kfree_skb(skb);
5598 return NET_RX_SUCCESS;
5599 }
5600
5601out:
c8079432
MM
5602 gro_normal_one(napi, skb);
5603 return NET_RX_SUCCESS;
d565b0a1
HX
5604}
5605
6312fe77 5606static void __napi_gro_flush_chain(struct napi_struct *napi, u32 index,
07d78363 5607 bool flush_old)
d565b0a1 5608{
6312fe77 5609 struct list_head *head = &napi->gro_hash[index].list;
d4546c25 5610 struct sk_buff *skb, *p;
2e71a6f8 5611
07d78363 5612 list_for_each_entry_safe_reverse(skb, p, head, list) {
2e71a6f8
ED
5613 if (flush_old && NAPI_GRO_CB(skb)->age == jiffies)
5614 return;
992cba7e 5615 skb_list_del_init(skb);
c8079432 5616 napi_gro_complete(napi, skb);
6312fe77 5617 napi->gro_hash[index].count--;
d565b0a1 5618 }
d9f37d01
LR
5619
5620 if (!napi->gro_hash[index].count)
5621 __clear_bit(index, &napi->gro_bitmask);
d565b0a1 5622}
07d78363 5623
6312fe77 5624/* napi->gro_hash[].list contains packets ordered by age.
07d78363
DM
5625 * youngest packets at the head of it.
5626 * Complete skbs in reverse order to reduce latencies.
5627 */
5628void napi_gro_flush(struct napi_struct *napi, bool flush_old)
5629{
42519ede
ED
5630 unsigned long bitmask = napi->gro_bitmask;
5631 unsigned int i, base = ~0U;
07d78363 5632
42519ede
ED
5633 while ((i = ffs(bitmask)) != 0) {
5634 bitmask >>= i;
5635 base += i;
5636 __napi_gro_flush_chain(napi, base, flush_old);
d9f37d01 5637 }
07d78363 5638}
86cac58b 5639EXPORT_SYMBOL(napi_gro_flush);
d565b0a1 5640
07d78363
DM
5641static struct list_head *gro_list_prepare(struct napi_struct *napi,
5642 struct sk_buff *skb)
89c5fa33 5643{
89c5fa33 5644 unsigned int maclen = skb->dev->hard_header_len;
0b4cec8c 5645 u32 hash = skb_get_hash_raw(skb);
07d78363 5646 struct list_head *head;
d4546c25 5647 struct sk_buff *p;
89c5fa33 5648
6312fe77 5649 head = &napi->gro_hash[hash & (GRO_HASH_BUCKETS - 1)].list;
07d78363 5650 list_for_each_entry(p, head, list) {
89c5fa33
ED
5651 unsigned long diffs;
5652
0b4cec8c
TH
5653 NAPI_GRO_CB(p)->flush = 0;
5654
5655 if (hash != skb_get_hash_raw(p)) {
5656 NAPI_GRO_CB(p)->same_flow = 0;
5657 continue;
5658 }
5659
89c5fa33 5660 diffs = (unsigned long)p->dev ^ (unsigned long)skb->dev;
b1817524
MM
5661 diffs |= skb_vlan_tag_present(p) ^ skb_vlan_tag_present(skb);
5662 if (skb_vlan_tag_present(p))
fc5141cb 5663 diffs |= skb_vlan_tag_get(p) ^ skb_vlan_tag_get(skb);
ce87fc6c 5664 diffs |= skb_metadata_dst_cmp(p, skb);
de8f3a83 5665 diffs |= skb_metadata_differs(p, skb);
89c5fa33
ED
5666 if (maclen == ETH_HLEN)
5667 diffs |= compare_ether_header(skb_mac_header(p),
a50e233c 5668 skb_mac_header(skb));
89c5fa33
ED
5669 else if (!diffs)
5670 diffs = memcmp(skb_mac_header(p),
a50e233c 5671 skb_mac_header(skb),
89c5fa33
ED
5672 maclen);
5673 NAPI_GRO_CB(p)->same_flow = !diffs;
89c5fa33 5674 }
07d78363
DM
5675
5676 return head;
89c5fa33
ED
5677}
5678
299603e8
JC
5679static void skb_gro_reset_offset(struct sk_buff *skb)
5680{
5681 const struct skb_shared_info *pinfo = skb_shinfo(skb);
5682 const skb_frag_t *frag0 = &pinfo->frags[0];
5683
5684 NAPI_GRO_CB(skb)->data_offset = 0;
5685 NAPI_GRO_CB(skb)->frag0 = NULL;
5686 NAPI_GRO_CB(skb)->frag0_len = 0;
5687
8aef998d 5688 if (!skb_headlen(skb) && pinfo->nr_frags &&
299603e8
JC
5689 !PageHighMem(skb_frag_page(frag0))) {
5690 NAPI_GRO_CB(skb)->frag0 = skb_frag_address(frag0);
7cfd5fd5
ED
5691 NAPI_GRO_CB(skb)->frag0_len = min_t(unsigned int,
5692 skb_frag_size(frag0),
5693 skb->end - skb->tail);
89c5fa33
ED
5694 }
5695}
5696
a50e233c
ED
5697static void gro_pull_from_frag0(struct sk_buff *skb, int grow)
5698{
5699 struct skb_shared_info *pinfo = skb_shinfo(skb);
5700
5701 BUG_ON(skb->end - skb->tail < grow);
5702
5703 memcpy(skb_tail_pointer(skb), NAPI_GRO_CB(skb)->frag0, grow);
5704
5705 skb->data_len -= grow;
5706 skb->tail += grow;
5707
b54c9d5b 5708 skb_frag_off_add(&pinfo->frags[0], grow);
a50e233c
ED
5709 skb_frag_size_sub(&pinfo->frags[0], grow);
5710
5711 if (unlikely(!skb_frag_size(&pinfo->frags[0]))) {
5712 skb_frag_unref(skb, 0);
5713 memmove(pinfo->frags, pinfo->frags + 1,
5714 --pinfo->nr_frags * sizeof(pinfo->frags[0]));
5715 }
5716}
5717
c8079432 5718static void gro_flush_oldest(struct napi_struct *napi, struct list_head *head)
07d78363 5719{
6312fe77 5720 struct sk_buff *oldest;
07d78363 5721
6312fe77 5722 oldest = list_last_entry(head, struct sk_buff, list);
07d78363 5723
6312fe77 5724 /* We are called with head length >= MAX_GRO_SKBS, so this is
07d78363
DM
5725 * impossible.
5726 */
5727 if (WARN_ON_ONCE(!oldest))
5728 return;
5729
d9f37d01
LR
5730 /* Do not adjust napi->gro_hash[].count, caller is adding a new
5731 * SKB to the chain.
07d78363 5732 */
ece23711 5733 skb_list_del_init(oldest);
c8079432 5734 napi_gro_complete(napi, oldest);
07d78363
DM
5735}
5736
aaa5d90b
PA
5737INDIRECT_CALLABLE_DECLARE(struct sk_buff *inet_gro_receive(struct list_head *,
5738 struct sk_buff *));
5739INDIRECT_CALLABLE_DECLARE(struct sk_buff *ipv6_gro_receive(struct list_head *,
5740 struct sk_buff *));
bb728820 5741static enum gro_result dev_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
d565b0a1 5742{
6312fe77 5743 u32 hash = skb_get_hash_raw(skb) & (GRO_HASH_BUCKETS - 1);
d4546c25 5744 struct list_head *head = &offload_base;
22061d80 5745 struct packet_offload *ptype;
d565b0a1 5746 __be16 type = skb->protocol;
07d78363 5747 struct list_head *gro_head;
d4546c25 5748 struct sk_buff *pp = NULL;
5b252f0c 5749 enum gro_result ret;
d4546c25 5750 int same_flow;
a50e233c 5751 int grow;
d565b0a1 5752
b5cdae32 5753 if (netif_elide_gro(skb->dev))
d565b0a1
HX
5754 goto normal;
5755
07d78363 5756 gro_head = gro_list_prepare(napi, skb);
89c5fa33 5757
d565b0a1
HX
5758 rcu_read_lock();
5759 list_for_each_entry_rcu(ptype, head, list) {
f191a1d1 5760 if (ptype->type != type || !ptype->callbacks.gro_receive)
d565b0a1
HX
5761 continue;
5762
86911732 5763 skb_set_network_header(skb, skb_gro_offset(skb));
efd9450e 5764 skb_reset_mac_len(skb);
d565b0a1 5765 NAPI_GRO_CB(skb)->same_flow = 0;
d61d072e 5766 NAPI_GRO_CB(skb)->flush = skb_is_gso(skb) || skb_has_frag_list(skb);
5d38a079 5767 NAPI_GRO_CB(skb)->free = 0;
fac8e0f5 5768 NAPI_GRO_CB(skb)->encap_mark = 0;
fcd91dd4 5769 NAPI_GRO_CB(skb)->recursion_counter = 0;
a0ca153f 5770 NAPI_GRO_CB(skb)->is_fou = 0;
1530545e 5771 NAPI_GRO_CB(skb)->is_atomic = 1;
15e2396d 5772 NAPI_GRO_CB(skb)->gro_remcsum_start = 0;
d565b0a1 5773
662880f4
TH
5774 /* Setup for GRO checksum validation */
5775 switch (skb->ip_summed) {
5776 case CHECKSUM_COMPLETE:
5777 NAPI_GRO_CB(skb)->csum = skb->csum;
5778 NAPI_GRO_CB(skb)->csum_valid = 1;
5779 NAPI_GRO_CB(skb)->csum_cnt = 0;
5780 break;
5781 case CHECKSUM_UNNECESSARY:
5782 NAPI_GRO_CB(skb)->csum_cnt = skb->csum_level + 1;
5783 NAPI_GRO_CB(skb)->csum_valid = 0;
5784 break;
5785 default:
5786 NAPI_GRO_CB(skb)->csum_cnt = 0;
5787 NAPI_GRO_CB(skb)->csum_valid = 0;
5788 }
d565b0a1 5789
aaa5d90b
PA
5790 pp = INDIRECT_CALL_INET(ptype->callbacks.gro_receive,
5791 ipv6_gro_receive, inet_gro_receive,
5792 gro_head, skb);
d565b0a1
HX
5793 break;
5794 }
5795 rcu_read_unlock();
5796
5797 if (&ptype->list == head)
5798 goto normal;
5799
45586c70 5800 if (PTR_ERR(pp) == -EINPROGRESS) {
25393d3f
SK
5801 ret = GRO_CONSUMED;
5802 goto ok;
5803 }
5804
0da2afd5 5805 same_flow = NAPI_GRO_CB(skb)->same_flow;
5d0d9be8 5806 ret = NAPI_GRO_CB(skb)->free ? GRO_MERGED_FREE : GRO_MERGED;
0da2afd5 5807
d565b0a1 5808 if (pp) {
992cba7e 5809 skb_list_del_init(pp);
c8079432 5810 napi_gro_complete(napi, pp);
6312fe77 5811 napi->gro_hash[hash].count--;
d565b0a1
HX
5812 }
5813
0da2afd5 5814 if (same_flow)
d565b0a1
HX
5815 goto ok;
5816
600adc18 5817 if (NAPI_GRO_CB(skb)->flush)
d565b0a1 5818 goto normal;
d565b0a1 5819
6312fe77 5820 if (unlikely(napi->gro_hash[hash].count >= MAX_GRO_SKBS)) {
c8079432 5821 gro_flush_oldest(napi, gro_head);
600adc18 5822 } else {
6312fe77 5823 napi->gro_hash[hash].count++;
600adc18 5824 }
d565b0a1 5825 NAPI_GRO_CB(skb)->count = 1;
2e71a6f8 5826 NAPI_GRO_CB(skb)->age = jiffies;
29e98242 5827 NAPI_GRO_CB(skb)->last = skb;
86911732 5828 skb_shinfo(skb)->gso_size = skb_gro_len(skb);
07d78363 5829 list_add(&skb->list, gro_head);
5d0d9be8 5830 ret = GRO_HELD;
d565b0a1 5831
ad0f9904 5832pull:
a50e233c
ED
5833 grow = skb_gro_offset(skb) - skb_headlen(skb);
5834 if (grow > 0)
5835 gro_pull_from_frag0(skb, grow);
d565b0a1 5836ok:
d9f37d01
LR
5837 if (napi->gro_hash[hash].count) {
5838 if (!test_bit(hash, &napi->gro_bitmask))
5839 __set_bit(hash, &napi->gro_bitmask);
5840 } else if (test_bit(hash, &napi->gro_bitmask)) {
5841 __clear_bit(hash, &napi->gro_bitmask);
5842 }
5843
5d0d9be8 5844 return ret;
d565b0a1
HX
5845
5846normal:
ad0f9904
HX
5847 ret = GRO_NORMAL;
5848 goto pull;
5d38a079 5849}
96e93eab 5850
bf5a755f
JC
5851struct packet_offload *gro_find_receive_by_type(__be16 type)
5852{
5853 struct list_head *offload_head = &offload_base;
5854 struct packet_offload *ptype;
5855
5856 list_for_each_entry_rcu(ptype, offload_head, list) {
5857 if (ptype->type != type || !ptype->callbacks.gro_receive)
5858 continue;
5859 return ptype;
5860 }
5861 return NULL;
5862}
e27a2f83 5863EXPORT_SYMBOL(gro_find_receive_by_type);
bf5a755f
JC
5864
5865struct packet_offload *gro_find_complete_by_type(__be16 type)
5866{
5867 struct list_head *offload_head = &offload_base;
5868 struct packet_offload *ptype;
5869
5870 list_for_each_entry_rcu(ptype, offload_head, list) {
5871 if (ptype->type != type || !ptype->callbacks.gro_complete)
5872 continue;
5873 return ptype;
5874 }
5875 return NULL;
5876}
e27a2f83 5877EXPORT_SYMBOL(gro_find_complete_by_type);
5d38a079 5878
e44699d2
MK
5879static void napi_skb_free_stolen_head(struct sk_buff *skb)
5880{
5881 skb_dst_drop(skb);
174e2381 5882 skb_ext_put(skb);
e44699d2
MK
5883 kmem_cache_free(skbuff_head_cache, skb);
5884}
5885
6570bc79
AL
5886static gro_result_t napi_skb_finish(struct napi_struct *napi,
5887 struct sk_buff *skb,
5888 gro_result_t ret)
5d38a079 5889{
5d0d9be8
HX
5890 switch (ret) {
5891 case GRO_NORMAL:
6570bc79 5892 gro_normal_one(napi, skb);
c7c4b3b6 5893 break;
5d38a079 5894
5d0d9be8 5895 case GRO_DROP:
5d38a079
HX
5896 kfree_skb(skb);
5897 break;
5b252f0c 5898
daa86548 5899 case GRO_MERGED_FREE:
e44699d2
MK
5900 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5901 napi_skb_free_stolen_head(skb);
5902 else
d7e8883c 5903 __kfree_skb(skb);
daa86548
ED
5904 break;
5905
5b252f0c
BH
5906 case GRO_HELD:
5907 case GRO_MERGED:
25393d3f 5908 case GRO_CONSUMED:
5b252f0c 5909 break;
5d38a079
HX
5910 }
5911
c7c4b3b6 5912 return ret;
5d0d9be8 5913}
5d0d9be8 5914
c7c4b3b6 5915gro_result_t napi_gro_receive(struct napi_struct *napi, struct sk_buff *skb)
5d0d9be8 5916{
b0e3f1bd
GB
5917 gro_result_t ret;
5918
93f93a44 5919 skb_mark_napi_id(skb, napi);
ae78dbfa 5920 trace_napi_gro_receive_entry(skb);
86911732 5921
a50e233c
ED
5922 skb_gro_reset_offset(skb);
5923
6570bc79 5924 ret = napi_skb_finish(napi, skb, dev_gro_receive(napi, skb));
b0e3f1bd
GB
5925 trace_napi_gro_receive_exit(ret);
5926
5927 return ret;
d565b0a1
HX
5928}
5929EXPORT_SYMBOL(napi_gro_receive);
5930
d0c2b0d2 5931static void napi_reuse_skb(struct napi_struct *napi, struct sk_buff *skb)
96e93eab 5932{
93a35f59
ED
5933 if (unlikely(skb->pfmemalloc)) {
5934 consume_skb(skb);
5935 return;
5936 }
96e93eab 5937 __skb_pull(skb, skb_headlen(skb));
2a2a459e
ED
5938 /* restore the reserve we had after netdev_alloc_skb_ip_align() */
5939 skb_reserve(skb, NET_SKB_PAD + NET_IP_ALIGN - skb_headroom(skb));
b1817524 5940 __vlan_hwaccel_clear_tag(skb);
66c46d74 5941 skb->dev = napi->dev;
6d152e23 5942 skb->skb_iif = 0;
33d9a2c7
ED
5943
5944 /* eth_type_trans() assumes pkt_type is PACKET_HOST */
5945 skb->pkt_type = PACKET_HOST;
5946
c3caf119
JC
5947 skb->encapsulation = 0;
5948 skb_shinfo(skb)->gso_type = 0;
e33d0ba8 5949 skb->truesize = SKB_TRUESIZE(skb_end_offset(skb));
174e2381 5950 skb_ext_reset(skb);
96e93eab
HX
5951
5952 napi->skb = skb;
5953}
96e93eab 5954
76620aaf 5955struct sk_buff *napi_get_frags(struct napi_struct *napi)
5d38a079 5956{
5d38a079 5957 struct sk_buff *skb = napi->skb;
5d38a079
HX
5958
5959 if (!skb) {
fd11a83d 5960 skb = napi_alloc_skb(napi, GRO_MAX_HEAD);
e2f9dc3b
ED
5961 if (skb) {
5962 napi->skb = skb;
5963 skb_mark_napi_id(skb, napi);
5964 }
80595d59 5965 }
96e93eab
HX
5966 return skb;
5967}
76620aaf 5968EXPORT_SYMBOL(napi_get_frags);
96e93eab 5969
a50e233c
ED
5970static gro_result_t napi_frags_finish(struct napi_struct *napi,
5971 struct sk_buff *skb,
5972 gro_result_t ret)
96e93eab 5973{
5d0d9be8
HX
5974 switch (ret) {
5975 case GRO_NORMAL:
a50e233c
ED
5976 case GRO_HELD:
5977 __skb_push(skb, ETH_HLEN);
5978 skb->protocol = eth_type_trans(skb, skb->dev);
323ebb61
EC
5979 if (ret == GRO_NORMAL)
5980 gro_normal_one(napi, skb);
86911732 5981 break;
5d38a079 5982
5d0d9be8 5983 case GRO_DROP:
5d0d9be8
HX
5984 napi_reuse_skb(napi, skb);
5985 break;
5b252f0c 5986
e44699d2
MK
5987 case GRO_MERGED_FREE:
5988 if (NAPI_GRO_CB(skb)->free == NAPI_GRO_FREE_STOLEN_HEAD)
5989 napi_skb_free_stolen_head(skb);
5990 else
5991 napi_reuse_skb(napi, skb);
5992 break;
5993
5b252f0c 5994 case GRO_MERGED:
25393d3f 5995 case GRO_CONSUMED:
5b252f0c 5996 break;
5d0d9be8 5997 }
5d38a079 5998
c7c4b3b6 5999 return ret;
5d38a079 6000}
5d0d9be8 6001
a50e233c
ED
6002/* Upper GRO stack assumes network header starts at gro_offset=0
6003 * Drivers could call both napi_gro_frags() and napi_gro_receive()
6004 * We copy ethernet header into skb->data to have a common layout.
6005 */
4adb9c4a 6006static struct sk_buff *napi_frags_skb(struct napi_struct *napi)
76620aaf
HX
6007{
6008 struct sk_buff *skb = napi->skb;
a50e233c
ED
6009 const struct ethhdr *eth;
6010 unsigned int hlen = sizeof(*eth);
76620aaf
HX
6011
6012 napi->skb = NULL;
6013
a50e233c
ED
6014 skb_reset_mac_header(skb);
6015 skb_gro_reset_offset(skb);
6016
a50e233c
ED
6017 if (unlikely(skb_gro_header_hard(skb, hlen))) {
6018 eth = skb_gro_header_slow(skb, hlen, 0);
6019 if (unlikely(!eth)) {
4da46ceb
AC
6020 net_warn_ratelimited("%s: dropping impossible skb from %s\n",
6021 __func__, napi->dev->name);
a50e233c
ED
6022 napi_reuse_skb(napi, skb);
6023 return NULL;
6024 }
6025 } else {
a4270d67 6026 eth = (const struct ethhdr *)skb->data;
a50e233c
ED
6027 gro_pull_from_frag0(skb, hlen);
6028 NAPI_GRO_CB(skb)->frag0 += hlen;
6029 NAPI_GRO_CB(skb)->frag0_len -= hlen;
76620aaf 6030 }
a50e233c
ED
6031 __skb_pull(skb, hlen);
6032
6033 /*
6034 * This works because the only protocols we care about don't require
6035 * special handling.
6036 * We'll fix it up properly in napi_frags_finish()
6037 */
6038 skb->protocol = eth->h_proto;
76620aaf 6039
76620aaf
HX
6040 return skb;
6041}
76620aaf 6042
c7c4b3b6 6043gro_result_t napi_gro_frags(struct napi_struct *napi)
5d0d9be8 6044{
b0e3f1bd 6045 gro_result_t ret;
76620aaf 6046 struct sk_buff *skb = napi_frags_skb(napi);
5d0d9be8
HX
6047
6048 if (!skb)
c7c4b3b6 6049 return GRO_DROP;
5d0d9be8 6050
ae78dbfa
BH
6051 trace_napi_gro_frags_entry(skb);
6052
b0e3f1bd
GB
6053 ret = napi_frags_finish(napi, skb, dev_gro_receive(napi, skb));
6054 trace_napi_gro_frags_exit(ret);
6055
6056 return ret;
5d0d9be8 6057}
5d38a079
HX
6058EXPORT_SYMBOL(napi_gro_frags);
6059
573e8fca
TH
6060/* Compute the checksum from gro_offset and return the folded value
6061 * after adding in any pseudo checksum.
6062 */
6063__sum16 __skb_gro_checksum_complete(struct sk_buff *skb)
6064{
6065 __wsum wsum;
6066 __sum16 sum;
6067
6068 wsum = skb_checksum(skb, skb_gro_offset(skb), skb_gro_len(skb), 0);
6069
6070 /* NAPI_GRO_CB(skb)->csum holds pseudo checksum */
6071 sum = csum_fold(csum_add(NAPI_GRO_CB(skb)->csum, wsum));
14641931 6072 /* See comments in __skb_checksum_complete(). */
573e8fca
TH
6073 if (likely(!sum)) {
6074 if (unlikely(skb->ip_summed == CHECKSUM_COMPLETE) &&
6075 !skb->csum_complete_sw)
7fe50ac8 6076 netdev_rx_csum_fault(skb->dev, skb);
573e8fca
TH
6077 }
6078
6079 NAPI_GRO_CB(skb)->csum = wsum;
6080 NAPI_GRO_CB(skb)->csum_valid = 1;
6081
6082 return sum;
6083}
6084EXPORT_SYMBOL(__skb_gro_checksum_complete);
6085
773fc8f6 6086static void net_rps_send_ipi(struct softnet_data *remsd)
6087{
6088#ifdef CONFIG_RPS
6089 while (remsd) {
6090 struct softnet_data *next = remsd->rps_ipi_next;
6091
6092 if (cpu_online(remsd->cpu))
6093 smp_call_function_single_async(remsd->cpu, &remsd->csd);
6094 remsd = next;
6095 }
6096#endif
6097}
6098
e326bed2 6099/*
855abcf0 6100 * net_rps_action_and_irq_enable sends any pending IPI's for rps.
e326bed2
ED
6101 * Note: called with local irq disabled, but exits with local irq enabled.
6102 */
6103static void net_rps_action_and_irq_enable(struct softnet_data *sd)
6104{
6105#ifdef CONFIG_RPS
6106 struct softnet_data *remsd = sd->rps_ipi_list;
6107
6108 if (remsd) {
6109 sd->rps_ipi_list = NULL;
6110
6111 local_irq_enable();
6112
6113 /* Send pending IPI's to kick RPS processing on remote cpus. */
773fc8f6 6114 net_rps_send_ipi(remsd);
e326bed2
ED
6115 } else
6116#endif
6117 local_irq_enable();
6118}
6119
d75b1ade
ED
6120static bool sd_has_rps_ipi_waiting(struct softnet_data *sd)
6121{
6122#ifdef CONFIG_RPS
6123 return sd->rps_ipi_list != NULL;
6124#else
6125 return false;
6126#endif
6127}
6128
bea3348e 6129static int process_backlog(struct napi_struct *napi, int quota)
1da177e4 6130{
eecfd7c4 6131 struct softnet_data *sd = container_of(napi, struct softnet_data, backlog);
145dd5f9
PA
6132 bool again = true;
6133 int work = 0;
1da177e4 6134
e326bed2
ED
6135 /* Check if we have pending ipi, its better to send them now,
6136 * not waiting net_rx_action() end.
6137 */
d75b1ade 6138 if (sd_has_rps_ipi_waiting(sd)) {
e326bed2
ED
6139 local_irq_disable();
6140 net_rps_action_and_irq_enable(sd);
6141 }
d75b1ade 6142
3d48b53f 6143 napi->weight = dev_rx_weight;
145dd5f9 6144 while (again) {
1da177e4 6145 struct sk_buff *skb;
6e7676c1
CG
6146
6147 while ((skb = __skb_dequeue(&sd->process_queue))) {
2c17d27c 6148 rcu_read_lock();
6e7676c1 6149 __netif_receive_skb(skb);
2c17d27c 6150 rcu_read_unlock();
76cc8b13 6151 input_queue_head_incr(sd);
145dd5f9 6152 if (++work >= quota)
76cc8b13 6153 return work;
145dd5f9 6154
6e7676c1 6155 }
1da177e4 6156
145dd5f9 6157 local_irq_disable();
e36fa2f7 6158 rps_lock(sd);
11ef7a89 6159 if (skb_queue_empty(&sd->input_pkt_queue)) {
eecfd7c4
ED
6160 /*
6161 * Inline a custom version of __napi_complete().
6162 * only current cpu owns and manipulates this napi,
11ef7a89
TH
6163 * and NAPI_STATE_SCHED is the only possible flag set
6164 * on backlog.
6165 * We can use a plain write instead of clear_bit(),
eecfd7c4
ED
6166 * and we dont need an smp_mb() memory barrier.
6167 */
eecfd7c4 6168 napi->state = 0;
145dd5f9
PA
6169 again = false;
6170 } else {
6171 skb_queue_splice_tail_init(&sd->input_pkt_queue,
6172 &sd->process_queue);
bea3348e 6173 }
e36fa2f7 6174 rps_unlock(sd);
145dd5f9 6175 local_irq_enable();
6e7676c1 6176 }
1da177e4 6177
bea3348e
SH
6178 return work;
6179}
1da177e4 6180
bea3348e
SH
6181/**
6182 * __napi_schedule - schedule for receive
c4ea43c5 6183 * @n: entry to schedule
bea3348e 6184 *
bc9ad166
ED
6185 * The entry's receive function will be scheduled to run.
6186 * Consider using __napi_schedule_irqoff() if hard irqs are masked.
bea3348e 6187 */
b5606c2d 6188void __napi_schedule(struct napi_struct *n)
bea3348e
SH
6189{
6190 unsigned long flags;
1da177e4 6191
bea3348e 6192 local_irq_save(flags);
903ceff7 6193 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
bea3348e 6194 local_irq_restore(flags);
1da177e4 6195}
bea3348e
SH
6196EXPORT_SYMBOL(__napi_schedule);
6197
39e6c820
ED
6198/**
6199 * napi_schedule_prep - check if napi can be scheduled
6200 * @n: napi context
6201 *
6202 * Test if NAPI routine is already running, and if not mark
6203 * it as running. This is used as a condition variable
6204 * insure only one NAPI poll instance runs. We also make
6205 * sure there is no pending NAPI disable.
6206 */
6207bool napi_schedule_prep(struct napi_struct *n)
6208{
6209 unsigned long val, new;
6210
6211 do {
6212 val = READ_ONCE(n->state);
6213 if (unlikely(val & NAPIF_STATE_DISABLE))
6214 return false;
6215 new = val | NAPIF_STATE_SCHED;
6216
6217 /* Sets STATE_MISSED bit if STATE_SCHED was already set
6218 * This was suggested by Alexander Duyck, as compiler
6219 * emits better code than :
6220 * if (val & NAPIF_STATE_SCHED)
6221 * new |= NAPIF_STATE_MISSED;
6222 */
6223 new |= (val & NAPIF_STATE_SCHED) / NAPIF_STATE_SCHED *
6224 NAPIF_STATE_MISSED;
6225 } while (cmpxchg(&n->state, val, new) != val);
6226
6227 return !(val & NAPIF_STATE_SCHED);
6228}
6229EXPORT_SYMBOL(napi_schedule_prep);
6230
bc9ad166
ED
6231/**
6232 * __napi_schedule_irqoff - schedule for receive
6233 * @n: entry to schedule
6234 *
6235 * Variant of __napi_schedule() assuming hard irqs are masked
6236 */
6237void __napi_schedule_irqoff(struct napi_struct *n)
6238{
6239 ____napi_schedule(this_cpu_ptr(&softnet_data), n);
6240}
6241EXPORT_SYMBOL(__napi_schedule_irqoff);
6242
364b6055 6243bool napi_complete_done(struct napi_struct *n, int work_done)
d565b0a1 6244{
39e6c820 6245 unsigned long flags, val, new;
d565b0a1
HX
6246
6247 /*
217f6974
ED
6248 * 1) Don't let napi dequeue from the cpu poll list
6249 * just in case its running on a different cpu.
6250 * 2) If we are busy polling, do nothing here, we have
6251 * the guarantee we will be called later.
d565b0a1 6252 */
217f6974
ED
6253 if (unlikely(n->state & (NAPIF_STATE_NPSVC |
6254 NAPIF_STATE_IN_BUSY_POLL)))
364b6055 6255 return false;
d565b0a1 6256
d9f37d01 6257 if (n->gro_bitmask) {
3b47d303 6258 unsigned long timeout = 0;
d75b1ade 6259
3b47d303
ED
6260 if (work_done)
6261 timeout = n->dev->gro_flush_timeout;
6262
605108ac
PA
6263 /* When the NAPI instance uses a timeout and keeps postponing
6264 * it, we need to bound somehow the time packets are kept in
6265 * the GRO layer
6266 */
6267 napi_gro_flush(n, !!timeout);
3b47d303
ED
6268 if (timeout)
6269 hrtimer_start(&n->timer, ns_to_ktime(timeout),
6270 HRTIMER_MODE_REL_PINNED);
3b47d303 6271 }
c8079432
MM
6272
6273 gro_normal_list(n);
6274
02c1602e 6275 if (unlikely(!list_empty(&n->poll_list))) {
d75b1ade
ED
6276 /* If n->poll_list is not empty, we need to mask irqs */
6277 local_irq_save(flags);
02c1602e 6278 list_del_init(&n->poll_list);
d75b1ade
ED
6279 local_irq_restore(flags);
6280 }
39e6c820
ED
6281
6282 do {
6283 val = READ_ONCE(n->state);
6284
6285 WARN_ON_ONCE(!(val & NAPIF_STATE_SCHED));
6286
6287 new = val & ~(NAPIF_STATE_MISSED | NAPIF_STATE_SCHED);
6288
6289 /* If STATE_MISSED was set, leave STATE_SCHED set,
6290 * because we will call napi->poll() one more time.
6291 * This C code was suggested by Alexander Duyck to help gcc.
6292 */
6293 new |= (val & NAPIF_STATE_MISSED) / NAPIF_STATE_MISSED *
6294 NAPIF_STATE_SCHED;
6295 } while (cmpxchg(&n->state, val, new) != val);
6296
6297 if (unlikely(val & NAPIF_STATE_MISSED)) {
6298 __napi_schedule(n);
6299 return false;
6300 }
6301
364b6055 6302 return true;
d565b0a1 6303}
3b47d303 6304EXPORT_SYMBOL(napi_complete_done);
d565b0a1 6305
af12fa6e 6306/* must be called under rcu_read_lock(), as we dont take a reference */
02d62e86 6307static struct napi_struct *napi_by_id(unsigned int napi_id)
af12fa6e
ET
6308{
6309 unsigned int hash = napi_id % HASH_SIZE(napi_hash);
6310 struct napi_struct *napi;
6311
6312 hlist_for_each_entry_rcu(napi, &napi_hash[hash], napi_hash_node)
6313 if (napi->napi_id == napi_id)
6314 return napi;
6315
6316 return NULL;
6317}
02d62e86
ED
6318
6319#if defined(CONFIG_NET_RX_BUSY_POLL)
217f6974 6320
ce6aea93 6321#define BUSY_POLL_BUDGET 8
217f6974
ED
6322
6323static void busy_poll_stop(struct napi_struct *napi, void *have_poll_lock)
6324{
6325 int rc;
6326
39e6c820
ED
6327 /* Busy polling means there is a high chance device driver hard irq
6328 * could not grab NAPI_STATE_SCHED, and that NAPI_STATE_MISSED was
6329 * set in napi_schedule_prep().
6330 * Since we are about to call napi->poll() once more, we can safely
6331 * clear NAPI_STATE_MISSED.
6332 *
6333 * Note: x86 could use a single "lock and ..." instruction
6334 * to perform these two clear_bit()
6335 */
6336 clear_bit(NAPI_STATE_MISSED, &napi->state);
217f6974
ED
6337 clear_bit(NAPI_STATE_IN_BUSY_POLL, &napi->state);
6338
6339 local_bh_disable();
6340
6341 /* All we really want here is to re-enable device interrupts.
6342 * Ideally, a new ndo_busy_poll_stop() could avoid another round.
6343 */
6344 rc = napi->poll(napi, BUSY_POLL_BUDGET);
323ebb61
EC
6345 /* We can't gro_normal_list() here, because napi->poll() might have
6346 * rearmed the napi (napi_complete_done()) in which case it could
6347 * already be running on another CPU.
6348 */
1e22391e 6349 trace_napi_poll(napi, rc, BUSY_POLL_BUDGET);
217f6974 6350 netpoll_poll_unlock(have_poll_lock);
323ebb61
EC
6351 if (rc == BUSY_POLL_BUDGET) {
6352 /* As the whole budget was spent, we still own the napi so can
6353 * safely handle the rx_list.
6354 */
6355 gro_normal_list(napi);
217f6974 6356 __napi_schedule(napi);
323ebb61 6357 }
217f6974 6358 local_bh_enable();
217f6974
ED
6359}
6360
7db6b048
SS
6361void napi_busy_loop(unsigned int napi_id,
6362 bool (*loop_end)(void *, unsigned long),
6363 void *loop_end_arg)
02d62e86 6364{
7db6b048 6365 unsigned long start_time = loop_end ? busy_loop_current_time() : 0;
217f6974 6366 int (*napi_poll)(struct napi_struct *napi, int budget);
217f6974 6367 void *have_poll_lock = NULL;
02d62e86 6368 struct napi_struct *napi;
217f6974
ED
6369
6370restart:
217f6974 6371 napi_poll = NULL;
02d62e86 6372
2a028ecb 6373 rcu_read_lock();
02d62e86 6374
545cd5e5 6375 napi = napi_by_id(napi_id);
02d62e86
ED
6376 if (!napi)
6377 goto out;
6378
217f6974
ED
6379 preempt_disable();
6380 for (;;) {
2b5cd0df
AD
6381 int work = 0;
6382
2a028ecb 6383 local_bh_disable();
217f6974
ED
6384 if (!napi_poll) {
6385 unsigned long val = READ_ONCE(napi->state);
6386
6387 /* If multiple threads are competing for this napi,
6388 * we avoid dirtying napi->state as much as we can.
6389 */
6390 if (val & (NAPIF_STATE_DISABLE | NAPIF_STATE_SCHED |
6391 NAPIF_STATE_IN_BUSY_POLL))
6392 goto count;
6393 if (cmpxchg(&napi->state, val,
6394 val | NAPIF_STATE_IN_BUSY_POLL |
6395 NAPIF_STATE_SCHED) != val)
6396 goto count;
6397 have_poll_lock = netpoll_poll_lock(napi);
6398 napi_poll = napi->poll;
6399 }
2b5cd0df
AD
6400 work = napi_poll(napi, BUSY_POLL_BUDGET);
6401 trace_napi_poll(napi, work, BUSY_POLL_BUDGET);
323ebb61 6402 gro_normal_list(napi);
217f6974 6403count:
2b5cd0df 6404 if (work > 0)
7db6b048 6405 __NET_ADD_STATS(dev_net(napi->dev),
2b5cd0df 6406 LINUX_MIB_BUSYPOLLRXPACKETS, work);
2a028ecb 6407 local_bh_enable();
02d62e86 6408
7db6b048 6409 if (!loop_end || loop_end(loop_end_arg, start_time))
217f6974 6410 break;
02d62e86 6411
217f6974
ED
6412 if (unlikely(need_resched())) {
6413 if (napi_poll)
6414 busy_poll_stop(napi, have_poll_lock);
6415 preempt_enable();
6416 rcu_read_unlock();
6417 cond_resched();
7db6b048 6418 if (loop_end(loop_end_arg, start_time))
2b5cd0df 6419 return;
217f6974
ED
6420 goto restart;
6421 }
6cdf89b1 6422 cpu_relax();
217f6974
ED
6423 }
6424 if (napi_poll)
6425 busy_poll_stop(napi, have_poll_lock);
6426 preempt_enable();
02d62e86 6427out:
2a028ecb 6428 rcu_read_unlock();
02d62e86 6429}
7db6b048 6430EXPORT_SYMBOL(napi_busy_loop);
02d62e86
ED
6431
6432#endif /* CONFIG_NET_RX_BUSY_POLL */
af12fa6e 6433
149d6ad8 6434static void napi_hash_add(struct napi_struct *napi)
af12fa6e 6435{
d64b5e85
ED
6436 if (test_bit(NAPI_STATE_NO_BUSY_POLL, &napi->state) ||
6437 test_and_set_bit(NAPI_STATE_HASHED, &napi->state))
52bd2d62 6438 return;
af12fa6e 6439
52bd2d62 6440 spin_lock(&napi_hash_lock);
af12fa6e 6441
545cd5e5 6442 /* 0..NR_CPUS range is reserved for sender_cpu use */
52bd2d62 6443 do {
545cd5e5
AD
6444 if (unlikely(++napi_gen_id < MIN_NAPI_ID))
6445 napi_gen_id = MIN_NAPI_ID;
52bd2d62
ED
6446 } while (napi_by_id(napi_gen_id));
6447 napi->napi_id = napi_gen_id;
af12fa6e 6448
52bd2d62
ED
6449 hlist_add_head_rcu(&napi->napi_hash_node,
6450 &napi_hash[napi->napi_id % HASH_SIZE(napi_hash)]);
af12fa6e 6451
52bd2d62 6452 spin_unlock(&napi_hash_lock);
af12fa6e 6453}
af12fa6e
ET
6454
6455/* Warning : caller is responsible to make sure rcu grace period
6456 * is respected before freeing memory containing @napi
6457 */
34cbe27e 6458bool napi_hash_del(struct napi_struct *napi)
af12fa6e 6459{
34cbe27e
ED
6460 bool rcu_sync_needed = false;
6461
af12fa6e
ET
6462 spin_lock(&napi_hash_lock);
6463
34cbe27e
ED
6464 if (test_and_clear_bit(NAPI_STATE_HASHED, &napi->state)) {
6465 rcu_sync_needed = true;
af12fa6e 6466 hlist_del_rcu(&napi->napi_hash_node);
34cbe27e 6467 }
af12fa6e 6468 spin_unlock(&napi_hash_lock);
34cbe27e 6469 return rcu_sync_needed;
af12fa6e
ET
6470}
6471EXPORT_SYMBOL_GPL(napi_hash_del);
6472
3b47d303
ED
6473static enum hrtimer_restart napi_watchdog(struct hrtimer *timer)
6474{
6475 struct napi_struct *napi;
6476
6477 napi = container_of(timer, struct napi_struct, timer);
39e6c820
ED
6478
6479 /* Note : we use a relaxed variant of napi_schedule_prep() not setting
6480 * NAPI_STATE_MISSED, since we do not react to a device IRQ.
6481 */
d9f37d01 6482 if (napi->gro_bitmask && !napi_disable_pending(napi) &&
39e6c820
ED
6483 !test_and_set_bit(NAPI_STATE_SCHED, &napi->state))
6484 __napi_schedule_irqoff(napi);
3b47d303
ED
6485
6486 return HRTIMER_NORESTART;
6487}
6488
7c4ec749 6489static void init_gro_hash(struct napi_struct *napi)
d565b0a1 6490{
07d78363
DM
6491 int i;
6492
6312fe77
LR
6493 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6494 INIT_LIST_HEAD(&napi->gro_hash[i].list);
6495 napi->gro_hash[i].count = 0;
6496 }
7c4ec749
DM
6497 napi->gro_bitmask = 0;
6498}
6499
6500void netif_napi_add(struct net_device *dev, struct napi_struct *napi,
6501 int (*poll)(struct napi_struct *, int), int weight)
6502{
6503 INIT_LIST_HEAD(&napi->poll_list);
6504 hrtimer_init(&napi->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL_PINNED);
6505 napi->timer.function = napi_watchdog;
6506 init_gro_hash(napi);
5d38a079 6507 napi->skb = NULL;
323ebb61
EC
6508 INIT_LIST_HEAD(&napi->rx_list);
6509 napi->rx_count = 0;
d565b0a1 6510 napi->poll = poll;
82dc3c63 6511 if (weight > NAPI_POLL_WEIGHT)
bf29e9e9
QC
6512 netdev_err_once(dev, "%s() called with weight %d\n", __func__,
6513 weight);
d565b0a1
HX
6514 napi->weight = weight;
6515 list_add(&napi->dev_list, &dev->napi_list);
d565b0a1 6516 napi->dev = dev;
5d38a079 6517#ifdef CONFIG_NETPOLL
d565b0a1
HX
6518 napi->poll_owner = -1;
6519#endif
6520 set_bit(NAPI_STATE_SCHED, &napi->state);
93d05d4a 6521 napi_hash_add(napi);
d565b0a1
HX
6522}
6523EXPORT_SYMBOL(netif_napi_add);
6524
3b47d303
ED
6525void napi_disable(struct napi_struct *n)
6526{
6527 might_sleep();
6528 set_bit(NAPI_STATE_DISABLE, &n->state);
6529
6530 while (test_and_set_bit(NAPI_STATE_SCHED, &n->state))
6531 msleep(1);
2d8bff12
NH
6532 while (test_and_set_bit(NAPI_STATE_NPSVC, &n->state))
6533 msleep(1);
3b47d303
ED
6534
6535 hrtimer_cancel(&n->timer);
6536
6537 clear_bit(NAPI_STATE_DISABLE, &n->state);
6538}
6539EXPORT_SYMBOL(napi_disable);
6540
07d78363 6541static void flush_gro_hash(struct napi_struct *napi)
d4546c25 6542{
07d78363 6543 int i;
d4546c25 6544
07d78363
DM
6545 for (i = 0; i < GRO_HASH_BUCKETS; i++) {
6546 struct sk_buff *skb, *n;
6547
6312fe77 6548 list_for_each_entry_safe(skb, n, &napi->gro_hash[i].list, list)
07d78363 6549 kfree_skb(skb);
6312fe77 6550 napi->gro_hash[i].count = 0;
07d78363 6551 }
d4546c25
DM
6552}
6553
93d05d4a 6554/* Must be called in process context */
d565b0a1
HX
6555void netif_napi_del(struct napi_struct *napi)
6556{
93d05d4a
ED
6557 might_sleep();
6558 if (napi_hash_del(napi))
6559 synchronize_net();
d7b06636 6560 list_del_init(&napi->dev_list);
76620aaf 6561 napi_free_frags(napi);
d565b0a1 6562
07d78363 6563 flush_gro_hash(napi);
d9f37d01 6564 napi->gro_bitmask = 0;
d565b0a1
HX
6565}
6566EXPORT_SYMBOL(netif_napi_del);
6567
726ce70e
HX
6568static int napi_poll(struct napi_struct *n, struct list_head *repoll)
6569{
6570 void *have;
6571 int work, weight;
6572
6573 list_del_init(&n->poll_list);
6574
6575 have = netpoll_poll_lock(n);
6576
6577 weight = n->weight;
6578
6579 /* This NAPI_STATE_SCHED test is for avoiding a race
6580 * with netpoll's poll_napi(). Only the entity which
6581 * obtains the lock and sees NAPI_STATE_SCHED set will
6582 * actually make the ->poll() call. Therefore we avoid
6583 * accidentally calling ->poll() when NAPI is not scheduled.
6584 */
6585 work = 0;
6586 if (test_bit(NAPI_STATE_SCHED, &n->state)) {
6587 work = n->poll(n, weight);
1db19db7 6588 trace_napi_poll(n, work, weight);
726ce70e
HX
6589 }
6590
6591 WARN_ON_ONCE(work > weight);
6592
6593 if (likely(work < weight))
6594 goto out_unlock;
6595
6596 /* Drivers must not modify the NAPI state if they
6597 * consume the entire weight. In such cases this code
6598 * still "owns" the NAPI instance and therefore can
6599 * move the instance around on the list at-will.
6600 */
6601 if (unlikely(napi_disable_pending(n))) {
6602 napi_complete(n);
6603 goto out_unlock;
6604 }
6605
d9f37d01 6606 if (n->gro_bitmask) {
726ce70e
HX
6607 /* flush too old packets
6608 * If HZ < 1000, flush all packets.
6609 */
6610 napi_gro_flush(n, HZ >= 1000);
6611 }
6612
c8079432
MM
6613 gro_normal_list(n);
6614
001ce546
HX
6615 /* Some drivers may have called napi_schedule
6616 * prior to exhausting their budget.
6617 */
6618 if (unlikely(!list_empty(&n->poll_list))) {
6619 pr_warn_once("%s: Budget exhausted after napi rescheduled\n",
6620 n->dev ? n->dev->name : "backlog");
6621 goto out_unlock;
6622 }
6623
726ce70e
HX
6624 list_add_tail(&n->poll_list, repoll);
6625
6626out_unlock:
6627 netpoll_poll_unlock(have);
6628
6629 return work;
6630}
6631
0766f788 6632static __latent_entropy void net_rx_action(struct softirq_action *h)
1da177e4 6633{
903ceff7 6634 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
7acf8a1e
MW
6635 unsigned long time_limit = jiffies +
6636 usecs_to_jiffies(netdev_budget_usecs);
51b0bded 6637 int budget = netdev_budget;
d75b1ade
ED
6638 LIST_HEAD(list);
6639 LIST_HEAD(repoll);
53fb95d3 6640
1da177e4 6641 local_irq_disable();
d75b1ade
ED
6642 list_splice_init(&sd->poll_list, &list);
6643 local_irq_enable();
1da177e4 6644
ceb8d5bf 6645 for (;;) {
bea3348e 6646 struct napi_struct *n;
1da177e4 6647
ceb8d5bf
HX
6648 if (list_empty(&list)) {
6649 if (!sd_has_rps_ipi_waiting(sd) && list_empty(&repoll))
f52dffe0 6650 goto out;
ceb8d5bf
HX
6651 break;
6652 }
6653
6bd373eb
HX
6654 n = list_first_entry(&list, struct napi_struct, poll_list);
6655 budget -= napi_poll(n, &repoll);
6656
d75b1ade 6657 /* If softirq window is exhausted then punt.
24f8b238
SH
6658 * Allow this to run for 2 jiffies since which will allow
6659 * an average latency of 1.5/HZ.
bea3348e 6660 */
ceb8d5bf
HX
6661 if (unlikely(budget <= 0 ||
6662 time_after_eq(jiffies, time_limit))) {
6663 sd->time_squeeze++;
6664 break;
6665 }
1da177e4 6666 }
d75b1ade 6667
d75b1ade
ED
6668 local_irq_disable();
6669
6670 list_splice_tail_init(&sd->poll_list, &list);
6671 list_splice_tail(&repoll, &list);
6672 list_splice(&list, &sd->poll_list);
6673 if (!list_empty(&sd->poll_list))
6674 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
6675
e326bed2 6676 net_rps_action_and_irq_enable(sd);
f52dffe0
ED
6677out:
6678 __kfree_skb_flush();
1da177e4
LT
6679}
6680
aa9d8560 6681struct netdev_adjacent {
9ff162a8 6682 struct net_device *dev;
5d261913
VF
6683
6684 /* upper master flag, there can only be one master device per list */
9ff162a8 6685 bool master;
5d261913 6686
32b6d34f
TY
6687 /* lookup ignore flag */
6688 bool ignore;
6689
5d261913
VF
6690 /* counter for the number of times this device was added to us */
6691 u16 ref_nr;
6692
402dae96
VF
6693 /* private field for the users */
6694 void *private;
6695
9ff162a8
JP
6696 struct list_head list;
6697 struct rcu_head rcu;
9ff162a8
JP
6698};
6699
6ea29da1 6700static struct netdev_adjacent *__netdev_find_adj(struct net_device *adj_dev,
2f268f12 6701 struct list_head *adj_list)
9ff162a8 6702{
5d261913 6703 struct netdev_adjacent *adj;
5d261913 6704
2f268f12 6705 list_for_each_entry(adj, adj_list, list) {
5d261913
VF
6706 if (adj->dev == adj_dev)
6707 return adj;
9ff162a8
JP
6708 }
6709 return NULL;
6710}
6711
32b6d34f 6712static int ____netdev_has_upper_dev(struct net_device *upper_dev, void *data)
f1170fd4
DA
6713{
6714 struct net_device *dev = data;
6715
6716 return upper_dev == dev;
6717}
6718
9ff162a8
JP
6719/**
6720 * netdev_has_upper_dev - Check if device is linked to an upper device
6721 * @dev: device
6722 * @upper_dev: upper device to check
6723 *
6724 * Find out if a device is linked to specified upper device and return true
6725 * in case it is. Note that this checks only immediate upper device,
6726 * not through a complete stack of devices. The caller must hold the RTNL lock.
6727 */
6728bool netdev_has_upper_dev(struct net_device *dev,
6729 struct net_device *upper_dev)
6730{
6731 ASSERT_RTNL();
6732
32b6d34f 6733 return netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
f1170fd4 6734 upper_dev);
9ff162a8
JP
6735}
6736EXPORT_SYMBOL(netdev_has_upper_dev);
6737
1a3f060c
DA
6738/**
6739 * netdev_has_upper_dev_all - Check if device is linked to an upper device
6740 * @dev: device
6741 * @upper_dev: upper device to check
6742 *
6743 * Find out if a device is linked to specified upper device and return true
6744 * in case it is. Note that this checks the entire upper device chain.
6745 * The caller must hold rcu lock.
6746 */
6747
1a3f060c
DA
6748bool netdev_has_upper_dev_all_rcu(struct net_device *dev,
6749 struct net_device *upper_dev)
6750{
32b6d34f 6751 return !!netdev_walk_all_upper_dev_rcu(dev, ____netdev_has_upper_dev,
1a3f060c
DA
6752 upper_dev);
6753}
6754EXPORT_SYMBOL(netdev_has_upper_dev_all_rcu);
6755
9ff162a8
JP
6756/**
6757 * netdev_has_any_upper_dev - Check if device is linked to some device
6758 * @dev: device
6759 *
6760 * Find out if a device is linked to an upper device and return true in case
6761 * it is. The caller must hold the RTNL lock.
6762 */
25cc72a3 6763bool netdev_has_any_upper_dev(struct net_device *dev)
9ff162a8
JP
6764{
6765 ASSERT_RTNL();
6766
f1170fd4 6767 return !list_empty(&dev->adj_list.upper);
9ff162a8 6768}
25cc72a3 6769EXPORT_SYMBOL(netdev_has_any_upper_dev);
9ff162a8
JP
6770
6771/**
6772 * netdev_master_upper_dev_get - Get master upper device
6773 * @dev: device
6774 *
6775 * Find a master upper device and return pointer to it or NULL in case
6776 * it's not there. The caller must hold the RTNL lock.
6777 */
6778struct net_device *netdev_master_upper_dev_get(struct net_device *dev)
6779{
aa9d8560 6780 struct netdev_adjacent *upper;
9ff162a8
JP
6781
6782 ASSERT_RTNL();
6783
2f268f12 6784 if (list_empty(&dev->adj_list.upper))
9ff162a8
JP
6785 return NULL;
6786
2f268f12 6787 upper = list_first_entry(&dev->adj_list.upper,
aa9d8560 6788 struct netdev_adjacent, list);
9ff162a8
JP
6789 if (likely(upper->master))
6790 return upper->dev;
6791 return NULL;
6792}
6793EXPORT_SYMBOL(netdev_master_upper_dev_get);
6794
32b6d34f
TY
6795static struct net_device *__netdev_master_upper_dev_get(struct net_device *dev)
6796{
6797 struct netdev_adjacent *upper;
6798
6799 ASSERT_RTNL();
6800
6801 if (list_empty(&dev->adj_list.upper))
6802 return NULL;
6803
6804 upper = list_first_entry(&dev->adj_list.upper,
6805 struct netdev_adjacent, list);
6806 if (likely(upper->master) && !upper->ignore)
6807 return upper->dev;
6808 return NULL;
6809}
6810
0f524a80
DA
6811/**
6812 * netdev_has_any_lower_dev - Check if device is linked to some device
6813 * @dev: device
6814 *
6815 * Find out if a device is linked to a lower device and return true in case
6816 * it is. The caller must hold the RTNL lock.
6817 */
6818static bool netdev_has_any_lower_dev(struct net_device *dev)
6819{
6820 ASSERT_RTNL();
6821
6822 return !list_empty(&dev->adj_list.lower);
6823}
6824
b6ccba4c
VF
6825void *netdev_adjacent_get_private(struct list_head *adj_list)
6826{
6827 struct netdev_adjacent *adj;
6828
6829 adj = list_entry(adj_list, struct netdev_adjacent, list);
6830
6831 return adj->private;
6832}
6833EXPORT_SYMBOL(netdev_adjacent_get_private);
6834
44a40855
VY
6835/**
6836 * netdev_upper_get_next_dev_rcu - Get the next dev from upper list
6837 * @dev: device
6838 * @iter: list_head ** of the current position
6839 *
6840 * Gets the next device from the dev's upper list, starting from iter
6841 * position. The caller must hold RCU read lock.
6842 */
6843struct net_device *netdev_upper_get_next_dev_rcu(struct net_device *dev,
6844 struct list_head **iter)
6845{
6846 struct netdev_adjacent *upper;
6847
6848 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6849
6850 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6851
6852 if (&upper->list == &dev->adj_list.upper)
6853 return NULL;
6854
6855 *iter = &upper->list;
6856
6857 return upper->dev;
6858}
6859EXPORT_SYMBOL(netdev_upper_get_next_dev_rcu);
6860
32b6d34f
TY
6861static struct net_device *__netdev_next_upper_dev(struct net_device *dev,
6862 struct list_head **iter,
6863 bool *ignore)
5343da4c
TY
6864{
6865 struct netdev_adjacent *upper;
6866
6867 upper = list_entry((*iter)->next, struct netdev_adjacent, list);
6868
6869 if (&upper->list == &dev->adj_list.upper)
6870 return NULL;
6871
6872 *iter = &upper->list;
32b6d34f 6873 *ignore = upper->ignore;
5343da4c
TY
6874
6875 return upper->dev;
6876}
6877
1a3f060c
DA
6878static struct net_device *netdev_next_upper_dev_rcu(struct net_device *dev,
6879 struct list_head **iter)
6880{
6881 struct netdev_adjacent *upper;
6882
6883 WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
6884
6885 upper = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
6886
6887 if (&upper->list == &dev->adj_list.upper)
6888 return NULL;
6889
6890 *iter = &upper->list;
6891
6892 return upper->dev;
6893}
6894
32b6d34f
TY
6895static int __netdev_walk_all_upper_dev(struct net_device *dev,
6896 int (*fn)(struct net_device *dev,
6897 void *data),
6898 void *data)
5343da4c
TY
6899{
6900 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6901 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6902 int ret, cur = 0;
32b6d34f 6903 bool ignore;
5343da4c
TY
6904
6905 now = dev;
6906 iter = &dev->adj_list.upper;
6907
6908 while (1) {
6909 if (now != dev) {
6910 ret = fn(now, data);
6911 if (ret)
6912 return ret;
6913 }
6914
6915 next = NULL;
6916 while (1) {
32b6d34f 6917 udev = __netdev_next_upper_dev(now, &iter, &ignore);
5343da4c
TY
6918 if (!udev)
6919 break;
32b6d34f
TY
6920 if (ignore)
6921 continue;
5343da4c
TY
6922
6923 next = udev;
6924 niter = &udev->adj_list.upper;
6925 dev_stack[cur] = now;
6926 iter_stack[cur++] = iter;
6927 break;
6928 }
6929
6930 if (!next) {
6931 if (!cur)
6932 return 0;
6933 next = dev_stack[--cur];
6934 niter = iter_stack[cur];
6935 }
6936
6937 now = next;
6938 iter = niter;
6939 }
6940
6941 return 0;
6942}
6943
1a3f060c
DA
6944int netdev_walk_all_upper_dev_rcu(struct net_device *dev,
6945 int (*fn)(struct net_device *dev,
6946 void *data),
6947 void *data)
6948{
5343da4c
TY
6949 struct net_device *udev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
6950 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
6951 int ret, cur = 0;
1a3f060c 6952
5343da4c
TY
6953 now = dev;
6954 iter = &dev->adj_list.upper;
1a3f060c 6955
5343da4c
TY
6956 while (1) {
6957 if (now != dev) {
6958 ret = fn(now, data);
6959 if (ret)
6960 return ret;
6961 }
6962
6963 next = NULL;
6964 while (1) {
6965 udev = netdev_next_upper_dev_rcu(now, &iter);
6966 if (!udev)
6967 break;
6968
6969 next = udev;
6970 niter = &udev->adj_list.upper;
6971 dev_stack[cur] = now;
6972 iter_stack[cur++] = iter;
6973 break;
6974 }
6975
6976 if (!next) {
6977 if (!cur)
6978 return 0;
6979 next = dev_stack[--cur];
6980 niter = iter_stack[cur];
6981 }
6982
6983 now = next;
6984 iter = niter;
1a3f060c
DA
6985 }
6986
6987 return 0;
6988}
6989EXPORT_SYMBOL_GPL(netdev_walk_all_upper_dev_rcu);
6990
32b6d34f
TY
6991static bool __netdev_has_upper_dev(struct net_device *dev,
6992 struct net_device *upper_dev)
6993{
6994 ASSERT_RTNL();
6995
6996 return __netdev_walk_all_upper_dev(dev, ____netdev_has_upper_dev,
6997 upper_dev);
6998}
6999
31088a11
VF
7000/**
7001 * netdev_lower_get_next_private - Get the next ->private from the
7002 * lower neighbour list
7003 * @dev: device
7004 * @iter: list_head ** of the current position
7005 *
7006 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7007 * list, starting from iter position. The caller must hold either hold the
7008 * RTNL lock or its own locking that guarantees that the neighbour lower
b469139e 7009 * list will remain unchanged.
31088a11
VF
7010 */
7011void *netdev_lower_get_next_private(struct net_device *dev,
7012 struct list_head **iter)
7013{
7014 struct netdev_adjacent *lower;
7015
7016 lower = list_entry(*iter, struct netdev_adjacent, list);
7017
7018 if (&lower->list == &dev->adj_list.lower)
7019 return NULL;
7020
6859e7df 7021 *iter = lower->list.next;
31088a11
VF
7022
7023 return lower->private;
7024}
7025EXPORT_SYMBOL(netdev_lower_get_next_private);
7026
7027/**
7028 * netdev_lower_get_next_private_rcu - Get the next ->private from the
7029 * lower neighbour list, RCU
7030 * variant
7031 * @dev: device
7032 * @iter: list_head ** of the current position
7033 *
7034 * Gets the next netdev_adjacent->private from the dev's lower neighbour
7035 * list, starting from iter position. The caller must hold RCU read lock.
7036 */
7037void *netdev_lower_get_next_private_rcu(struct net_device *dev,
7038 struct list_head **iter)
7039{
7040 struct netdev_adjacent *lower;
7041
7042 WARN_ON_ONCE(!rcu_read_lock_held());
7043
7044 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7045
7046 if (&lower->list == &dev->adj_list.lower)
7047 return NULL;
7048
6859e7df 7049 *iter = &lower->list;
31088a11
VF
7050
7051 return lower->private;
7052}
7053EXPORT_SYMBOL(netdev_lower_get_next_private_rcu);
7054
4085ebe8
VY
7055/**
7056 * netdev_lower_get_next - Get the next device from the lower neighbour
7057 * list
7058 * @dev: device
7059 * @iter: list_head ** of the current position
7060 *
7061 * Gets the next netdev_adjacent from the dev's lower neighbour
7062 * list, starting from iter position. The caller must hold RTNL lock or
7063 * its own locking that guarantees that the neighbour lower
b469139e 7064 * list will remain unchanged.
4085ebe8
VY
7065 */
7066void *netdev_lower_get_next(struct net_device *dev, struct list_head **iter)
7067{
7068 struct netdev_adjacent *lower;
7069
cfdd28be 7070 lower = list_entry(*iter, struct netdev_adjacent, list);
4085ebe8
VY
7071
7072 if (&lower->list == &dev->adj_list.lower)
7073 return NULL;
7074
cfdd28be 7075 *iter = lower->list.next;
4085ebe8
VY
7076
7077 return lower->dev;
7078}
7079EXPORT_SYMBOL(netdev_lower_get_next);
7080
1a3f060c
DA
7081static struct net_device *netdev_next_lower_dev(struct net_device *dev,
7082 struct list_head **iter)
7083{
7084 struct netdev_adjacent *lower;
7085
46b5ab1a 7086 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
1a3f060c
DA
7087
7088 if (&lower->list == &dev->adj_list.lower)
7089 return NULL;
7090
46b5ab1a 7091 *iter = &lower->list;
1a3f060c
DA
7092
7093 return lower->dev;
7094}
7095
32b6d34f
TY
7096static struct net_device *__netdev_next_lower_dev(struct net_device *dev,
7097 struct list_head **iter,
7098 bool *ignore)
7099{
7100 struct netdev_adjacent *lower;
7101
7102 lower = list_entry((*iter)->next, struct netdev_adjacent, list);
7103
7104 if (&lower->list == &dev->adj_list.lower)
7105 return NULL;
7106
7107 *iter = &lower->list;
7108 *ignore = lower->ignore;
7109
7110 return lower->dev;
7111}
7112
1a3f060c
DA
7113int netdev_walk_all_lower_dev(struct net_device *dev,
7114 int (*fn)(struct net_device *dev,
7115 void *data),
7116 void *data)
7117{
5343da4c
TY
7118 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7119 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7120 int ret, cur = 0;
1a3f060c 7121
5343da4c
TY
7122 now = dev;
7123 iter = &dev->adj_list.lower;
1a3f060c 7124
5343da4c
TY
7125 while (1) {
7126 if (now != dev) {
7127 ret = fn(now, data);
7128 if (ret)
7129 return ret;
7130 }
7131
7132 next = NULL;
7133 while (1) {
7134 ldev = netdev_next_lower_dev(now, &iter);
7135 if (!ldev)
7136 break;
7137
7138 next = ldev;
7139 niter = &ldev->adj_list.lower;
7140 dev_stack[cur] = now;
7141 iter_stack[cur++] = iter;
7142 break;
7143 }
7144
7145 if (!next) {
7146 if (!cur)
7147 return 0;
7148 next = dev_stack[--cur];
7149 niter = iter_stack[cur];
7150 }
7151
7152 now = next;
7153 iter = niter;
1a3f060c
DA
7154 }
7155
7156 return 0;
7157}
7158EXPORT_SYMBOL_GPL(netdev_walk_all_lower_dev);
7159
32b6d34f
TY
7160static int __netdev_walk_all_lower_dev(struct net_device *dev,
7161 int (*fn)(struct net_device *dev,
7162 void *data),
7163 void *data)
7164{
7165 struct net_device *ldev, *next, *now, *dev_stack[MAX_NEST_DEV + 1];
7166 struct list_head *niter, *iter, *iter_stack[MAX_NEST_DEV + 1];
7167 int ret, cur = 0;
7168 bool ignore;
7169
7170 now = dev;
7171 iter = &dev->adj_list.lower;
7172
7173 while (1) {
7174 if (now != dev) {
7175 ret = fn(now, data);
7176 if (ret)
7177 return ret;
7178 }
7179
7180 next = NULL;
7181 while (1) {
7182 ldev = __netdev_next_lower_dev(now, &iter, &ignore);
7183 if (!ldev)
7184 break;
7185 if (ignore)
7186 continue;
7187
7188 next = ldev;
7189 niter = &ldev->adj_list.lower;
7190 dev_stack[cur] = now;
7191 iter_stack[cur++] = iter;
7192 break;
7193 }
7194
7195 if (!next) {
7196 if (!cur)
7197 return 0;
7198 next = dev_stack[--cur];
7199 niter = iter_stack[cur];
7200 }
7201
7202 now = next;
7203 iter = niter;
7204 }
7205
7206 return 0;
7207}
7208
7151affe
TY
7209struct net_device *netdev_next_lower_dev_rcu(struct net_device *dev,
7210 struct list_head **iter)
1a3f060c
DA
7211{
7212 struct netdev_adjacent *lower;
7213
7214 lower = list_entry_rcu((*iter)->next, struct netdev_adjacent, list);
7215 if (&lower->list == &dev->adj_list.lower)
7216 return NULL;
7217
7218 *iter = &lower->list;
7219
7220 return lower->dev;
7221}
7151affe 7222EXPORT_SYMBOL(netdev_next_lower_dev_rcu);
1a3f060c 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);