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