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