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