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