1/*
2 * NET3 Protocol independent device support routines.
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
10 * Authors: Ross Biro
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
24 * to 2 if register_netdev gets called
25 * before net_dev_init & also removed a
26 * few lines of code in the process.
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.
39 * Alan Cox : Fix ETH_P_ALL echoback lengths.
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.
49 * Alan Cox : Fixed nasty side effect of device close
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
70 * indefinitely on dev->refcnt
71 * J Hadi Salim : - Backlog queue sampling
72 * - netif_rx() feedback
73 */
74
75#include <linux/uaccess.h>
76#include <linux/bitops.h>
77#include <linux/capability.h>
78#include <linux/cpu.h>
79#include <linux/types.h>
80#include <linux/kernel.h>
81#include <linux/hash.h>
82#include <linux/slab.h>
83#include <linux/sched.h>
84#include <linux/mutex.h>
85#include <linux/string.h>
86#include <linux/mm.h>
87#include <linux/socket.h>
88#include <linux/sockios.h>
89#include <linux/errno.h>
90#include <linux/interrupt.h>
91#include <linux/if_ether.h>
92#include <linux/netdevice.h>
93#include <linux/etherdevice.h>
94#include <linux/ethtool.h>
95#include <linux/notifier.h>
96#include <linux/skbuff.h>
97#include <linux/bpf.h>
98#include <net/net_namespace.h>
99#include <net/sock.h>
100#include <net/busy_poll.h>
101#include <linux/rtnetlink.h>
102#include <linux/stat.h>
103#include <net/dst.h>
104#include <net/dst_metadata.h>
105#include <net/pkt_sched.h>
106#include <net/checksum.h>
107#include <net/xfrm.h>
108#include <linux/highmem.h>
109#include <linux/init.h>
110#include <linux/module.h>
111#include <linux/netpoll.h>
112#include <linux/rcupdate.h>
113#include <linux/delay.h>
114#include <net/iw_handler.h>
115#include <asm/current.h>
116#include <linux/audit.h>
117#include <linux/dmaengine.h>
118#include <linux/err.h>
119#include <linux/ctype.h>
120#include <linux/if_arp.h>
121#include <linux/if_vlan.h>
122#include <linux/ip.h>
123#include <net/ip.h>
124#include <net/mpls.h>
125#include <linux/ipv6.h>
126#include <linux/in.h>
127#include <linux/jhash.h>
128#include <linux/random.h>
129#include <trace/events/napi.h>
130#include <trace/events/net.h>
131#include <trace/events/skb.h>
132#include <linux/pci.h>
133#include <linux/inetdevice.h>
134#include <linux/cpu_rmap.h>
135#include <linux/static_key.h>
136#include <linux/hashtable.h>
137#include <linux/vmalloc.h>
138#include <linux/if_macvlan.h>
139#include <linux/errqueue.h>
140#include <linux/hrtimer.h>
141#include <linux/netfilter_ingress.h>
142#include <linux/crash_dump.h>
143
144#include "net-sysfs.h"
145
146/* Instead of increasing this, you should create a hash table. */
147#define MAX_GRO_SKBS 8
148
149/* This should be increased if a protocol with a bigger head is added. */
150#define GRO_MAX_HEAD (MAX_HEADER + 128)
151
152static DEFINE_SPINLOCK(ptype_lock);
153static DEFINE_SPINLOCK(offload_lock);
154struct list_head ptype_base[PTYPE_HASH_SIZE] __read_mostly;
155struct list_head ptype_all __read_mostly; /* Taps */
156static struct list_head offload_base __read_mostly;
157
158static int netif_rx_internal(struct sk_buff *skb);
159static int call_netdevice_notifiers_info(unsigned long val,
160 struct net_device *dev,
161 struct netdev_notifier_info *info);
162
163/*
164 * The @dev_base_head list is protected by @dev_base_lock and the rtnl
165 * semaphore.
166 *
167 * Pure readers hold dev_base_lock for reading, or rcu_read_lock()
168 *
169 * Writers must hold the rtnl semaphore while they loop through the
170 * dev_base_head list, and hold dev_base_lock for writing when they do the
171 * actual updates. This allows pure readers to access the list even
172 * while a writer is preparing to update it.
173 *
174 * To put it another way, dev_base_lock is held for writing only to
175 * protect against pure readers; the rtnl semaphore provides the
176 * protection against other writers.
177 *
178 * See, for example usages, register_netdevice() and
179 * unregister_netdevice(), which must be called with the rtnl
180 * semaphore held.
181 */
182DEFINE_RWLOCK(dev_base_lock);
183EXPORT_SYMBOL(dev_base_lock);
184
185/* protects napi_hash addition/deletion and napi_gen_id */
186static DEFINE_SPINLOCK(napi_hash_lock);
187
188static unsigned int napi_gen_id = NR_CPUS;
189static DEFINE_READ_MOSTLY_HASHTABLE(napi_hash, 8);
190
191static seqcount_t devnet_rename_seq;
192
193static inline void dev_base_seq_inc(struct net *net)
194{
195 while (++net->dev_base_seq == 0)
196 ;
197}
198
199static inline struct hlist_head *dev_name_hash(struct net *net, const char *name)
200{
201 unsigned int hash = full_name_hash(net, name, strnlen(name, IFNAMSIZ));
202
203 return &net->dev_name_head[hash_32(hash, NETDEV_HASHBITS)];
204}
205
206static inline struct hlist_head *dev_index_hash(struct net *net, int ifindex)
207{
208 return &net->dev_index_head[ifindex & (NETDEV_HASHENTRIES - 1)];
209}
210
211static inline void rps_lock(struct softnet_data *sd)
212{
213#ifdef CONFIG_RPS
214 spin_lock(&sd->input_pkt_queue.lock);
215#endif
216}
217
218static inline void rps_unlock(struct softnet_data *sd)
219{
220#ifdef CONFIG_RPS
221 spin_unlock(&sd->input_pkt_queue.lock);
222#endif
223}
224
225/* Device list insertion */
226static void list_netdevice(struct net_device *dev)
227{
228 struct net *net = dev_net(dev);
229
230 ASSERT_RTNL();
231
232 write_lock_bh(&dev_base_lock);
233 list_add_tail_rcu(&dev->dev_list, &net->dev_base_head);
234 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
235 hlist_add_head_rcu(&dev->index_hlist,
236 dev_index_hash(net, dev->ifindex));
237 write_unlock_bh(&dev_base_lock);
238
239 dev_base_seq_inc(net);
240}
241
242/* Device list removal
243 * caller must respect a RCU grace period before freeing/reusing dev
244 */
245static void unlist_netdevice(struct net_device *dev)
246{
247 ASSERT_RTNL();
248
249 /* Unlink dev from the device chain */
250 write_lock_bh(&dev_base_lock);
251 list_del_rcu(&dev->dev_list);
252 hlist_del_rcu(&dev->name_hlist);
253 hlist_del_rcu(&dev->index_hlist);
254 write_unlock_bh(&dev_base_lock);
255
256 dev_base_seq_inc(dev_net(dev));
257}
258
259/*
260 * Our notifier list
261 */
262
263static RAW_NOTIFIER_HEAD(netdev_chain);
264
265/*
266 * Device drivers call our routines to queue packets here. We empty the
267 * queue in the local softnet handler.
268 */
269
270DEFINE_PER_CPU_ALIGNED(struct softnet_data, softnet_data);
271EXPORT_PER_CPU_SYMBOL(softnet_data);
272
273#ifdef CONFIG_LOCKDEP
274/*
275 * register_netdevice() inits txq->_xmit_lock and sets lockdep class
276 * according to dev->type
277 */
278static const unsigned short netdev_lock_type[] = {
279 ARPHRD_NETROM, ARPHRD_ETHER, ARPHRD_EETHER, ARPHRD_AX25,
280 ARPHRD_PRONET, ARPHRD_CHAOS, ARPHRD_IEEE802, ARPHRD_ARCNET,
281 ARPHRD_APPLETLK, ARPHRD_DLCI, ARPHRD_ATM, ARPHRD_METRICOM,
282 ARPHRD_IEEE1394, ARPHRD_EUI64, ARPHRD_INFINIBAND, ARPHRD_SLIP,
283 ARPHRD_CSLIP, ARPHRD_SLIP6, ARPHRD_CSLIP6, ARPHRD_RSRVD,
284 ARPHRD_ADAPT, ARPHRD_ROSE, ARPHRD_X25, ARPHRD_HWX25,
285 ARPHRD_PPP, ARPHRD_CISCO, ARPHRD_LAPB, ARPHRD_DDCMP,
286 ARPHRD_RAWHDLC, ARPHRD_TUNNEL, ARPHRD_TUNNEL6, ARPHRD_FRAD,
287 ARPHRD_SKIP, ARPHRD_LOOPBACK, ARPHRD_LOCALTLK, ARPHRD_FDDI,
288 ARPHRD_BIF, ARPHRD_SIT, ARPHRD_IPDDP, ARPHRD_IPGRE,
289 ARPHRD_PIMREG, ARPHRD_HIPPI, ARPHRD_ASH, ARPHRD_ECONET,
290 ARPHRD_IRDA, ARPHRD_FCPP, ARPHRD_FCAL, ARPHRD_FCPL,
291 ARPHRD_FCFABRIC, ARPHRD_IEEE80211, ARPHRD_IEEE80211_PRISM,
292 ARPHRD_IEEE80211_RADIOTAP, ARPHRD_PHONET, ARPHRD_PHONET_PIPE,
293 ARPHRD_IEEE802154, ARPHRD_VOID, ARPHRD_NONE};
294
295static const char *const netdev_lock_name[] = {
296 "_xmit_NETROM", "_xmit_ETHER", "_xmit_EETHER", "_xmit_AX25",
297 "_xmit_PRONET", "_xmit_CHAOS", "_xmit_IEEE802", "_xmit_ARCNET",
298 "_xmit_APPLETLK", "_xmit_DLCI", "_xmit_ATM", "_xmit_METRICOM",
299 "_xmit_IEEE1394", "_xmit_EUI64", "_xmit_INFINIBAND", "_xmit_SLIP",
300 "_xmit_CSLIP", "_xmit_SLIP6", "_xmit_CSLIP6", "_xmit_RSRVD",
301 "_xmit_ADAPT", "_xmit_ROSE", "_xmit_X25", "_xmit_HWX25",
302 "_xmit_PPP", "_xmit_CISCO", "_xmit_LAPB", "_xmit_DDCMP",
303 "_xmit_RAWHDLC", "_xmit_TUNNEL", "_xmit_TUNNEL6", "_xmit_FRAD",
304 "_xmit_SKIP", "_xmit_LOOPBACK", "_xmit_LOCALTLK", "_xmit_FDDI",
305 "_xmit_BIF", "_xmit_SIT", "_xmit_IPDDP", "_xmit_IPGRE",
306 "_xmit_PIMREG", "_xmit_HIPPI", "_xmit_ASH", "_xmit_ECONET",
307 "_xmit_IRDA", "_xmit_FCPP", "_xmit_FCAL", "_xmit_FCPL",
308 "_xmit_FCFABRIC", "_xmit_IEEE80211", "_xmit_IEEE80211_PRISM",
309 "_xmit_IEEE80211_RADIOTAP", "_xmit_PHONET", "_xmit_PHONET_PIPE",
310 "_xmit_IEEE802154", "_xmit_VOID", "_xmit_NONE"};
311
312static struct lock_class_key netdev_xmit_lock_key[ARRAY_SIZE(netdev_lock_type)];
313static struct lock_class_key netdev_addr_lock_key[ARRAY_SIZE(netdev_lock_type)];
314
315static inline unsigned short netdev_lock_pos(unsigned short dev_type)
316{
317 int i;
318
319 for (i = 0; i < ARRAY_SIZE(netdev_lock_type); i++)
320 if (netdev_lock_type[i] == dev_type)
321 return i;
322 /* the last key is used by default */
323 return ARRAY_SIZE(netdev_lock_type) - 1;
324}
325
326static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
327 unsigned short dev_type)
328{
329 int i;
330
331 i = netdev_lock_pos(dev_type);
332 lockdep_set_class_and_name(lock, &netdev_xmit_lock_key[i],
333 netdev_lock_name[i]);
334}
335
336static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
337{
338 int i;
339
340 i = netdev_lock_pos(dev->type);
341 lockdep_set_class_and_name(&dev->addr_list_lock,
342 &netdev_addr_lock_key[i],
343 netdev_lock_name[i]);
344}
345#else
346static inline void netdev_set_xmit_lockdep_class(spinlock_t *lock,
347 unsigned short dev_type)
348{
349}
350static inline void netdev_set_addr_lockdep_class(struct net_device *dev)
351{
352}
353#endif
354
355/*******************************************************************************
356 *
357 * Protocol management and registration routines
358 *
359 *******************************************************************************/
360
361
362/*
363 * Add a protocol ID to the list. Now that the input handler is
364 * smarter we can dispense with all the messy stuff that used to be
365 * here.
366 *
367 * BEWARE!!! Protocol handlers, mangling input packets,
368 * MUST BE last in hash buckets and checking protocol handlers
369 * MUST start from promiscuous ptype_all chain in net_bh.
370 * It is true now, do not change it.
371 * Explanation follows: if protocol handler, mangling packet, will
372 * be the first on list, it is not able to sense, that packet
373 * is cloned and should be copied-on-write, so that it will
374 * change it and subsequent readers will get broken packet.
375 * --ANK (980803)
376 */
377
378static inline struct list_head *ptype_head(const struct packet_type *pt)
379{
380 if (pt->type == htons(ETH_P_ALL))
381 return pt->dev ? &pt->dev->ptype_all : &ptype_all;
382 else
383 return pt->dev ? &pt->dev->ptype_specific :
384 &ptype_base[ntohs(pt->type) & PTYPE_HASH_MASK];
385}
386
387/**
388 * dev_add_pack - add packet handler
389 * @pt: packet type declaration
390 *
391 * Add a protocol handler to the networking stack. The passed &packet_type
392 * is linked into kernel lists and may not be freed until it has been
393 * removed from the kernel lists.
394 *
395 * This call does not sleep therefore it can not
396 * guarantee all CPU's that are in middle of receiving packets
397 * will see the new packet type (until the next received packet).
398 */
399
400void dev_add_pack(struct packet_type *pt)
401{
402 struct list_head *head = ptype_head(pt);
403
404 spin_lock(&ptype_lock);
405 list_add_rcu(&pt->list, head);
406 spin_unlock(&ptype_lock);
407}
408EXPORT_SYMBOL(dev_add_pack);
409
410/**
411 * __dev_remove_pack - remove packet handler
412 * @pt: packet type declaration
413 *
414 * Remove a protocol handler that was previously added to the kernel
415 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
416 * from the kernel lists and can be freed or reused once this function
417 * returns.
418 *
419 * The packet type might still be in use by receivers
420 * and must not be freed until after all the CPU's have gone
421 * through a quiescent state.
422 */
423void __dev_remove_pack(struct packet_type *pt)
424{
425 struct list_head *head = ptype_head(pt);
426 struct packet_type *pt1;
427
428 spin_lock(&ptype_lock);
429
430 list_for_each_entry(pt1, head, list) {
431 if (pt == pt1) {
432 list_del_rcu(&pt->list);
433 goto out;
434 }
435 }
436
437 pr_warn("dev_remove_pack: %p not found\n", pt);
438out:
439 spin_unlock(&ptype_lock);
440}
441EXPORT_SYMBOL(__dev_remove_pack);
442
443/**
444 * dev_remove_pack - remove packet handler
445 * @pt: packet type declaration
446 *
447 * Remove a protocol handler that was previously added to the kernel
448 * protocol handlers by dev_add_pack(). The passed &packet_type is removed
449 * from the kernel lists and can be freed or reused once this function
450 * returns.
451 *
452 * This call sleeps to guarantee that no CPU is looking at the packet
453 * type after return.
454 */
455void dev_remove_pack(struct packet_type *pt)
456{
457 __dev_remove_pack(pt);
458
459 synchronize_net();
460}
461EXPORT_SYMBOL(dev_remove_pack);
462
463
464/**
465 * dev_add_offload - register offload handlers
466 * @po: protocol offload declaration
467 *
468 * Add protocol offload handlers to the networking stack. The passed
469 * &proto_offload is linked into kernel lists and may not be freed until
470 * it has been removed from the kernel lists.
471 *
472 * This call does not sleep therefore it can not
473 * guarantee all CPU's that are in middle of receiving packets
474 * will see the new offload handlers (until the next received packet).
475 */
476void dev_add_offload(struct packet_offload *po)
477{
478 struct packet_offload *elem;
479
480 spin_lock(&offload_lock);
481 list_for_each_entry(elem, &offload_base, list) {
482 if (po->priority < elem->priority)
483 break;
484 }
485 list_add_rcu(&po->list, elem->list.prev);
486 spin_unlock(&offload_lock);
487}
488EXPORT_SYMBOL(dev_add_offload);
489
490/**
491 * __dev_remove_offload - remove offload handler
492 * @po: packet offload declaration
493 *
494 * Remove a protocol offload handler that was previously added to the
495 * kernel offload handlers by dev_add_offload(). The passed &offload_type
496 * is removed from the kernel lists and can be freed or reused once this
497 * function returns.
498 *
499 * The packet type might still be in use by receivers
500 * and must not be freed until after all the CPU's have gone
501 * through a quiescent state.
502 */
503static void __dev_remove_offload(struct packet_offload *po)
504{
505 struct list_head *head = &offload_base;
506 struct packet_offload *po1;
507
508 spin_lock(&offload_lock);
509
510 list_for_each_entry(po1, head, list) {
511 if (po == po1) {
512 list_del_rcu(&po->list);
513 goto out;
514 }
515 }
516
517 pr_warn("dev_remove_offload: %p not found\n", po);
518out:
519 spin_unlock(&offload_lock);
520}
521
522/**
523 * dev_remove_offload - remove packet offload handler
524 * @po: packet offload declaration
525 *
526 * Remove a packet offload handler that was previously added to the kernel
527 * offload handlers by dev_add_offload(). The passed &offload_type is
528 * removed from the kernel lists and can be freed or reused once this
529 * function returns.
530 *
531 * This call sleeps to guarantee that no CPU is looking at the packet
532 * type after return.
533 */
534void dev_remove_offload(struct packet_offload *po)
535{
536 __dev_remove_offload(po);
537
538 synchronize_net();
539}
540EXPORT_SYMBOL(dev_remove_offload);
541
542/******************************************************************************
543 *
544 * Device Boot-time Settings Routines
545 *
546 ******************************************************************************/
547
548/* Boot time configuration table */
549static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
550
551/**
552 * netdev_boot_setup_add - add new setup entry
553 * @name: name of the device
554 * @map: configured settings for the device
555 *
556 * Adds new setup entry to the dev_boot_setup list. The function
557 * returns 0 on error and 1 on success. This is a generic routine to
558 * all netdevices.
559 */
560static int netdev_boot_setup_add(char *name, struct ifmap *map)
561{
562 struct netdev_boot_setup *s;
563 int i;
564
565 s = dev_boot_setup;
566 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
567 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
568 memset(s[i].name, 0, sizeof(s[i].name));
569 strlcpy(s[i].name, name, IFNAMSIZ);
570 memcpy(&s[i].map, map, sizeof(s[i].map));
571 break;
572 }
573 }
574
575 return i >= NETDEV_BOOT_SETUP_MAX ? 0 : 1;
576}
577
578/**
579 * netdev_boot_setup_check - check boot time settings
580 * @dev: the netdevice
581 *
582 * Check boot time settings for the device.
583 * The found settings are set for the device to be used
584 * later in the device probing.
585 * Returns 0 if no settings found, 1 if they are.
586 */
587int netdev_boot_setup_check(struct net_device *dev)
588{
589 struct netdev_boot_setup *s = dev_boot_setup;
590 int i;
591
592 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
593 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
594 !strcmp(dev->name, s[i].name)) {
595 dev->irq = s[i].map.irq;
596 dev->base_addr = s[i].map.base_addr;
597 dev->mem_start = s[i].map.mem_start;
598 dev->mem_end = s[i].map.mem_end;
599 return 1;
600 }
601 }
602 return 0;
603}
604EXPORT_SYMBOL(netdev_boot_setup_check);
605
606
607/**
608 * netdev_boot_base - get address from boot time settings
609 * @prefix: prefix for network device
610 * @unit: id for network device
611 *
612 * Check boot time settings for the base address of device.
613 * The found settings are set for the device to be used
614 * later in the device probing.
615 * Returns 0 if no settings found.
616 */
617unsigned long netdev_boot_base(const char *prefix, int unit)
618{
619 const struct netdev_boot_setup *s = dev_boot_setup;
620 char name[IFNAMSIZ];
621 int i;
622
623 sprintf(name, "%s%d", prefix, unit);
624
625 /*
626 * If device already registered then return base of 1
627 * to indicate not to probe for this interface
628 */
629 if (__dev_get_by_name(&init_net, name))
630 return 1;
631
632 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++)
633 if (!strcmp(name, s[i].name))
634 return s[i].map.base_addr;
635 return 0;
636}
637
638/*
639 * Saves at boot time configured settings for any netdevice.
640 */
641int __init netdev_boot_setup(char *str)
642{
643 int ints[5];
644 struct ifmap map;
645
646 str = get_options(str, ARRAY_SIZE(ints), ints);
647 if (!str || !*str)
648 return 0;
649
650 /* Save settings */
651 memset(&map, 0, sizeof(map));
652 if (ints[0] > 0)
653 map.irq = ints[1];
654 if (ints[0] > 1)
655 map.base_addr = ints[2];
656 if (ints[0] > 2)
657 map.mem_start = ints[3];
658 if (ints[0] > 3)
659 map.mem_end = ints[4];
660
661 /* Add new entry to the list */
662 return netdev_boot_setup_add(str, &map);
663}
664
665__setup("netdev=", netdev_boot_setup);
666
667/*******************************************************************************
668 *
669 * Device Interface Subroutines
670 *
671 *******************************************************************************/
672
673/**
674 * dev_get_iflink - get 'iflink' value of a interface
675 * @dev: targeted interface
676 *
677 * Indicates the ifindex the interface is linked to.
678 * Physical interfaces have the same 'ifindex' and 'iflink' values.
679 */
680
681int dev_get_iflink(const struct net_device *dev)
682{
683 if (dev->netdev_ops && dev->netdev_ops->ndo_get_iflink)
684 return dev->netdev_ops->ndo_get_iflink(dev);
685
686 return dev->ifindex;
687}
688EXPORT_SYMBOL(dev_get_iflink);
689
690/**
691 * dev_fill_metadata_dst - Retrieve tunnel egress information.
692 * @dev: targeted interface
693 * @skb: The packet.
694 *
695 * For better visibility of tunnel traffic OVS needs to retrieve
696 * egress tunnel information for a packet. Following API allows
697 * user to get this info.
698 */
699int dev_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
700{
701 struct ip_tunnel_info *info;
702
703 if (!dev->netdev_ops || !dev->netdev_ops->ndo_fill_metadata_dst)
704 return -EINVAL;
705
706 info = skb_tunnel_info_unclone(skb);
707 if (!info)
708 return -ENOMEM;
709 if (unlikely(!(info->mode & IP_TUNNEL_INFO_TX)))
710 return -EINVAL;
711
712 return dev->netdev_ops->ndo_fill_metadata_dst(dev, skb);
713}
714EXPORT_SYMBOL_GPL(dev_fill_metadata_dst);
715
716/**
717 * __dev_get_by_name - find a device by its name
718 * @net: the applicable net namespace
719 * @name: name to find
720 *
721 * Find an interface by name. Must be called under RTNL semaphore
722 * or @dev_base_lock. If the name is found a pointer to the device
723 * is returned. If the name is not found then %NULL is returned. The
724 * reference counters are not incremented so the caller must be
725 * careful with locks.
726 */
727
728struct net_device *__dev_get_by_name(struct net *net, const char *name)
729{
730 struct net_device *dev;
731 struct hlist_head *head = dev_name_hash(net, name);
732
733 hlist_for_each_entry(dev, head, name_hlist)
734 if (!strncmp(dev->name, name, IFNAMSIZ))
735 return dev;
736
737 return NULL;
738}
739EXPORT_SYMBOL(__dev_get_by_name);
740
741/**
742 * dev_get_by_name_rcu - find a device by its name
743 * @net: the applicable net namespace
744 * @name: name to find
745 *
746 * Find an interface by name.
747 * If the name is found a pointer to the device is returned.
748 * If the name is not found then %NULL is returned.
749 * The reference counters are not incremented so the caller must be
750 * careful with locks. The caller must hold RCU lock.
751 */
752
753struct net_device *dev_get_by_name_rcu(struct net *net, const char *name)
754{
755 struct net_device *dev;
756 struct hlist_head *head = dev_name_hash(net, name);
757
758 hlist_for_each_entry_rcu(dev, head, name_hlist)
759 if (!strncmp(dev->name, name, IFNAMSIZ))
760 return dev;
761
762 return NULL;
763}
764EXPORT_SYMBOL(dev_get_by_name_rcu);
765
766/**
767 * dev_get_by_name - find a device by its name
768 * @net: the applicable net namespace
769 * @name: name to find
770 *
771 * Find an interface by name. This can be called from any
772 * context and does its own locking. The returned handle has
773 * the usage count incremented and the caller must use dev_put() to
774 * release it when it is no longer needed. %NULL is returned if no
775 * matching device is found.
776 */
777
778struct net_device *dev_get_by_name(struct net *net, const char *name)
779{
780 struct net_device *dev;
781
782 rcu_read_lock();
783 dev = dev_get_by_name_rcu(net, name);
784 if (dev)
785 dev_hold(dev);
786 rcu_read_unlock();
787 return dev;
788}
789EXPORT_SYMBOL(dev_get_by_name);
790
791/**
792 * __dev_get_by_index - find a device by its ifindex
793 * @net: the applicable net namespace
794 * @ifindex: index of device
795 *
796 * Search for an interface by index. Returns %NULL if the device
797 * is not found or a pointer to the device. The device has not
798 * had its reference counter increased so the caller must be careful
799 * about locking. The caller must hold either the RTNL semaphore
800 * or @dev_base_lock.
801 */
802
803struct net_device *__dev_get_by_index(struct net *net, int ifindex)
804{
805 struct net_device *dev;
806 struct hlist_head *head = dev_index_hash(net, ifindex);
807
808 hlist_for_each_entry(dev, head, index_hlist)
809 if (dev->ifindex == ifindex)
810 return dev;
811
812 return NULL;
813}
814EXPORT_SYMBOL(__dev_get_by_index);
815
816/**
817 * dev_get_by_index_rcu - find a device by its ifindex
818 * @net: the applicable net namespace
819 * @ifindex: index of device
820 *
821 * Search for an interface by index. Returns %NULL if the device
822 * is not found or a pointer to the device. The device has not
823 * had its reference counter increased so the caller must be careful
824 * about locking. The caller must hold RCU lock.
825 */
826
827struct net_device *dev_get_by_index_rcu(struct net *net, int ifindex)
828{
829 struct net_device *dev;
830 struct hlist_head *head = dev_index_hash(net, ifindex);
831
832 hlist_for_each_entry_rcu(dev, head, index_hlist)
833 if (dev->ifindex == ifindex)
834 return dev;
835
836 return NULL;
837}
838EXPORT_SYMBOL(dev_get_by_index_rcu);
839
840
841/**
842 * dev_get_by_index - find a device by its ifindex
843 * @net: the applicable net namespace
844 * @ifindex: index of device
845 *
846 * Search for an interface by index. Returns NULL if the device
847 * is not found or a pointer to the device. The device returned has
848 * had a reference added and the pointer is safe until the user calls
849 * dev_put to indicate they have finished with it.
850 */
851
852struct net_device *dev_get_by_index(struct net *net, int ifindex)
853{
854 struct net_device *dev;
855
856 rcu_read_lock();
857 dev = dev_get_by_index_rcu(net, ifindex);
858 if (dev)
859 dev_hold(dev);
860 rcu_read_unlock();
861 return dev;
862}
863EXPORT_SYMBOL(dev_get_by_index);
864
865/**
866 * netdev_get_name - get a netdevice name, knowing its ifindex.
867 * @net: network namespace
868 * @name: a pointer to the buffer where the name will be stored.
869 * @ifindex: the ifindex of the interface to get the name from.
870 *
871 * The use of raw_seqcount_begin() and cond_resched() before
872 * retrying is required as we want to give the writers a chance
873 * to complete when CONFIG_PREEMPT is not set.
874 */
875int netdev_get_name(struct net *net, char *name, int ifindex)
876{
877 struct net_device *dev;
878 unsigned int seq;
879
880retry:
881 seq = raw_seqcount_begin(&devnet_rename_seq);
882 rcu_read_lock();
883 dev = dev_get_by_index_rcu(net, ifindex);
884 if (!dev) {
885 rcu_read_unlock();
886 return -ENODEV;
887 }
888
889 strcpy(name, dev->name);
890 rcu_read_unlock();
891 if (read_seqcount_retry(&devnet_rename_seq, seq)) {
892 cond_resched();
893 goto retry;
894 }
895
896 return 0;
897}
898
899/**
900 * dev_getbyhwaddr_rcu - find a device by its hardware address
901 * @net: the applicable net namespace
902 * @type: media type of device
903 * @ha: hardware address
904 *
905 * Search for an interface by MAC address. Returns NULL if the device
906 * is not found or a pointer to the device.
907 * The caller must hold RCU or RTNL.
908 * The returned device has not had its ref count increased
909 * and the caller must therefore be careful about locking
910 *
911 */
912
913struct net_device *dev_getbyhwaddr_rcu(struct net *net, unsigned short type,
914 const char *ha)
915{
916 struct net_device *dev;
917
918 for_each_netdev_rcu(net, dev)
919 if (dev->type == type &&
920 !memcmp(dev->dev_addr, ha, dev->addr_len))
921 return dev;
922
923 return NULL;
924}
925EXPORT_SYMBOL(dev_getbyhwaddr_rcu);
926
927struct net_device *__dev_getfirstbyhwtype(struct net *net, unsigned short type)
928{
929 struct net_device *dev;
930
931 ASSERT_RTNL();
932 for_each_netdev(net, dev)
933 if (dev->type == type)
934 return dev;
935
936 return NULL;
937}
938EXPORT_SYMBOL(__dev_getfirstbyhwtype);
939
940struct net_device *dev_getfirstbyhwtype(struct net *net, unsigned short type)
941{
942 struct net_device *dev, *ret = NULL;
943
944 rcu_read_lock();
945 for_each_netdev_rcu(net, dev)
946 if (dev->type == type) {
947 dev_hold(dev);
948 ret = dev;
949 break;
950 }
951 rcu_read_unlock();
952 return ret;
953}
954EXPORT_SYMBOL(dev_getfirstbyhwtype);
955
956/**
957 * __dev_get_by_flags - find any device with given flags
958 * @net: the applicable net namespace
959 * @if_flags: IFF_* values
960 * @mask: bitmask of bits in if_flags to check
961 *
962 * Search for any interface with the given flags. Returns NULL if a device
963 * is not found or a pointer to the device. Must be called inside
964 * rtnl_lock(), and result refcount is unchanged.
965 */
966
967struct net_device *__dev_get_by_flags(struct net *net, unsigned short if_flags,
968 unsigned short mask)
969{
970 struct net_device *dev, *ret;
971
972 ASSERT_RTNL();
973
974 ret = NULL;
975 for_each_netdev(net, dev) {
976 if (((dev->flags ^ if_flags) & mask) == 0) {
977 ret = dev;
978 break;
979 }
980 }
981 return ret;
982}
983EXPORT_SYMBOL(__dev_get_by_flags);
984
985/**
986 * dev_valid_name - check if name is okay for network device
987 * @name: name string
988 *
989 * Network device names need to be valid file names to
990 * to allow sysfs to work. We also disallow any kind of
991 * whitespace.
992 */
993bool dev_valid_name(const char *name)
994{
995 if (*name == '\0')
996 return false;
997 if (strlen(name) >= IFNAMSIZ)
998 return false;
999 if (!strcmp(name, ".") || !strcmp(name, ".."))
1000 return false;
1001
1002 while (*name) {
1003 if (*name == '/' || *name == ':' || isspace(*name))
1004 return false;
1005 name++;
1006 }
1007 return true;
1008}
1009EXPORT_SYMBOL(dev_valid_name);
1010
1011/**
1012 * __dev_alloc_name - allocate a name for a device
1013 * @net: network namespace to allocate the device name in
1014 * @name: name format string
1015 * @buf: scratch buffer and result name string
1016 *
1017 * Passed a format string - eg "lt%d" it will try and find a suitable
1018 * id. It scans list of devices to build up a free map, then chooses
1019 * the first empty slot. The caller must hold the dev_base or rtnl lock
1020 * while allocating the name and adding the device in order to avoid
1021 * duplicates.
1022 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1023 * Returns the number of the unit assigned or a negative errno code.
1024 */
1025
1026static int __dev_alloc_name(struct net *net, const char *name, char *buf)
1027{
1028 int i = 0;
1029 const char *p;
1030 const int max_netdevices = 8*PAGE_SIZE;
1031 unsigned long *inuse;
1032 struct net_device *d;
1033
1034 p = strnchr(name, IFNAMSIZ-1, '%');
1035 if (p) {
1036 /*
1037 * Verify the string as this thing may have come from
1038 * the user. There must be either one "%d" and no other "%"
1039 * characters.
1040 */
1041 if (p[1] != 'd' || strchr(p + 2, '%'))
1042 return -EINVAL;
1043
1044 /* Use one page as a bit array of possible slots */
1045 inuse = (unsigned long *) get_zeroed_page(GFP_ATOMIC);
1046 if (!inuse)
1047 return -ENOMEM;
1048
1049 for_each_netdev(net, d) {
1050 if (!sscanf(d->name, name, &i))
1051 continue;
1052 if (i < 0 || i >= max_netdevices)
1053 continue;
1054
1055 /* avoid cases where sscanf is not exact inverse of printf */
1056 snprintf(buf, IFNAMSIZ, name, i);
1057 if (!strncmp(buf, d->name, IFNAMSIZ))
1058 set_bit(i, inuse);
1059 }
1060
1061 i = find_first_zero_bit(inuse, max_netdevices);
1062 free_page((unsigned long) inuse);
1063 }
1064
1065 if (buf != name)
1066 snprintf(buf, IFNAMSIZ, name, i);
1067 if (!__dev_get_by_name(net, buf))
1068 return i;
1069
1070 /* It is possible to run out of possible slots
1071 * when the name is long and there isn't enough space left
1072 * for the digits, or if all bits are used.
1073 */
1074 return -ENFILE;
1075}
1076
1077/**
1078 * dev_alloc_name - allocate a name for a device
1079 * @dev: device
1080 * @name: name format string
1081 *
1082 * Passed a format string - eg "lt%d" it will try and find a suitable
1083 * id. It scans list of devices to build up a free map, then chooses
1084 * the first empty slot. The caller must hold the dev_base or rtnl lock
1085 * while allocating the name and adding the device in order to avoid
1086 * duplicates.
1087 * Limited to bits_per_byte * page size devices (ie 32K on most platforms).
1088 * Returns the number of the unit assigned or a negative errno code.
1089 */
1090
1091int dev_alloc_name(struct net_device *dev, const char *name)
1092{
1093 char buf[IFNAMSIZ];
1094 struct net *net;
1095 int ret;
1096
1097 BUG_ON(!dev_net(dev));
1098 net = dev_net(dev);
1099 ret = __dev_alloc_name(net, name, buf);
1100 if (ret >= 0)
1101 strlcpy(dev->name, buf, IFNAMSIZ);
1102 return ret;
1103}
1104EXPORT_SYMBOL(dev_alloc_name);
1105
1106static int dev_alloc_name_ns(struct net *net,
1107 struct net_device *dev,
1108 const char *name)
1109{
1110 char buf[IFNAMSIZ];
1111 int ret;
1112
1113 ret = __dev_alloc_name(net, name, buf);
1114 if (ret >= 0)
1115 strlcpy(dev->name, buf, IFNAMSIZ);
1116 return ret;
1117}
1118
1119static int dev_get_valid_name(struct net *net,
1120 struct net_device *dev,
1121 const char *name)
1122{
1123 BUG_ON(!net);
1124
1125 if (!dev_valid_name(name))
1126 return -EINVAL;
1127
1128 if (strchr(name, '%'))
1129 return dev_alloc_name_ns(net, dev, name);
1130 else if (__dev_get_by_name(net, name))
1131 return -EEXIST;
1132 else if (dev->name != name)
1133 strlcpy(dev->name, name, IFNAMSIZ);
1134
1135 return 0;
1136}
1137
1138/**
1139 * dev_change_name - change name of a device
1140 * @dev: device
1141 * @newname: name (or format string) must be at least IFNAMSIZ
1142 *
1143 * Change name of a device, can pass format strings "eth%d".
1144 * for wildcarding.
1145 */
1146int dev_change_name(struct net_device *dev, const char *newname)
1147{
1148 unsigned char old_assign_type;
1149 char oldname[IFNAMSIZ];
1150 int err = 0;
1151 int ret;
1152 struct net *net;
1153
1154 ASSERT_RTNL();
1155 BUG_ON(!dev_net(dev));
1156
1157 net = dev_net(dev);
1158 if (dev->flags & IFF_UP)
1159 return -EBUSY;
1160
1161 write_seqcount_begin(&devnet_rename_seq);
1162
1163 if (strncmp(newname, dev->name, IFNAMSIZ) == 0) {
1164 write_seqcount_end(&devnet_rename_seq);
1165 return 0;
1166 }
1167
1168 memcpy(oldname, dev->name, IFNAMSIZ);
1169
1170 err = dev_get_valid_name(net, dev, newname);
1171 if (err < 0) {
1172 write_seqcount_end(&devnet_rename_seq);
1173 return err;
1174 }
1175
1176 if (oldname[0] && !strchr(oldname, '%'))
1177 netdev_info(dev, "renamed from %s\n", oldname);
1178
1179 old_assign_type = dev->name_assign_type;
1180 dev->name_assign_type = NET_NAME_RENAMED;
1181
1182rollback:
1183 ret = device_rename(&dev->dev, dev->name);
1184 if (ret) {
1185 memcpy(dev->name, oldname, IFNAMSIZ);
1186 dev->name_assign_type = old_assign_type;
1187 write_seqcount_end(&devnet_rename_seq);
1188 return ret;
1189 }
1190
1191 write_seqcount_end(&devnet_rename_seq);
1192
1193 netdev_adjacent_rename_links(dev, oldname);
1194
1195 write_lock_bh(&dev_base_lock);
1196 hlist_del_rcu(&dev->name_hlist);
1197 write_unlock_bh(&dev_base_lock);
1198
1199 synchronize_rcu();
1200
1201 write_lock_bh(&dev_base_lock);
1202 hlist_add_head_rcu(&dev->name_hlist, dev_name_hash(net, dev->name));
1203 write_unlock_bh(&dev_base_lock);
1204
1205 ret = call_netdevice_notifiers(NETDEV_CHANGENAME, dev);
1206 ret = notifier_to_errno(ret);
1207
1208 if (ret) {
1209 /* err >= 0 after dev_alloc_name() or stores the first errno */
1210 if (err >= 0) {
1211 err = ret;
1212 write_seqcount_begin(&devnet_rename_seq);
1213 memcpy(dev->name, oldname, IFNAMSIZ);
1214 memcpy(oldname, newname, IFNAMSIZ);
1215 dev->name_assign_type = old_assign_type;
1216 old_assign_type = NET_NAME_RENAMED;
1217 goto rollback;
1218 } else {
1219 pr_err("%s: name change rollback failed: %d\n",
1220 dev->name, ret);
1221 }
1222 }
1223
1224 return err;
1225}
1226
1227/**
1228 * dev_set_alias - change ifalias of a device
1229 * @dev: device
1230 * @alias: name up to IFALIASZ
1231 * @len: limit of bytes to copy from info
1232 *
1233 * Set ifalias for a device,
1234 */
1235int dev_set_alias(struct net_device *dev, const char *alias, size_t len)
1236{
1237 char *new_ifalias;
1238
1239 ASSERT_RTNL();
1240
1241 if (len >= IFALIASZ)
1242 return -EINVAL;
1243
1244 if (!len) {
1245 kfree(dev->ifalias);
1246 dev->ifalias = NULL;
1247 return 0;
1248 }
1249
1250 new_ifalias = krealloc(dev->ifalias, len + 1, GFP_KERNEL);
1251 if (!new_ifalias)
1252 return -ENOMEM;
1253 dev->ifalias = new_ifalias;
1254
1255 strlcpy(dev->ifalias, alias, len+1);
1256 return len;
1257}
1258
1259
1260/**
1261 * netdev_features_change - device changes features
1262 * @dev: device to cause notification
1263 *
1264 * Called to indicate a device has changed features.
1265 */
1266void netdev_features_change(struct net_device *dev)
1267{
1268 call_netdevice_notifiers(NETDEV_FEAT_CHANGE, dev);
1269}
1270EXPORT_SYMBOL(netdev_features_change);
1271
1272/**
1273 * netdev_state_change - device changes state
1274 * @dev: device to cause notification
1275 *
1276 * Called to indicate a device has changed state. This function calls
1277 * the notifier chains for netdev_chain and sends a NEWLINK message
1278 * to the routing socket.
1279 */
1280void netdev_state_change(struct net_device *dev)
1281{
1282 if (dev->flags & IFF_UP) {
1283 struct netdev_notifier_change_info change_info;
1284
1285 change_info.flags_changed = 0;
1286 call_netdevice_notifiers_info(NETDEV_CHANGE, dev,
1287 &change_info.info);
1288 rtmsg_ifinfo(RTM_NEWLINK, dev, 0, GFP_KERNEL);
1289 }
1290}
1291EXPORT_SYMBOL(netdev_state_change);
1292
1293/**
1294 * netdev_notify_peers - notify network peers about existence of @dev
1295 * @dev: network device
1296 *
1297 * Generate traffic such that interested network peers are aware of
1298 * @dev, such as by generating a gratuitous ARP. This may be used when
1299 * a device wants to inform the rest of the network about some sort of
1300 * reconfiguration such as a failover event or virtual machine
1301 * migration.
1302 */
1303void netdev_notify_peers(struct net_device *dev)
1304{
1305 rtnl_lock();
1306 call_netdevice_notifiers(NETDEV_NOTIFY_PEERS, dev);
1307 call_netdevice_notifiers(NETDEV_RESEND_IGMP, dev);
1308 rtnl_unlock();
1309}
1310EXPORT_SYMBOL(netdev_notify_peers);
1311
1312static int __dev_open(struct net_device *dev)
1313{
1314 const struct net_device_ops *ops = dev->netdev_ops;
1315 int ret;
1316
1317 ASSERT_RTNL();
1318
1319 if (!netif_device_present(dev))
1320 return -ENODEV;
1321
1322 /* Block netpoll from trying to do any rx path servicing.
1323 * If we don't do this there is a chance ndo_poll_controller
1324 * or ndo_poll may be running while we open the device
1325 */
1326 netpoll_poll_disable(dev);
1327
1328 ret = call_netdevice_notifiers(NETDEV_PRE_UP, dev);
1329 ret = notifier_to_errno(ret);
1330 if (ret)
1331 return ret;
1332
1333 set_bit(__LINK_STATE_START, &dev->state);
1334
1335 if (ops->ndo_validate_addr)
1336 ret = ops->ndo_validate_addr(dev);
1337
1338 if (!ret && ops->ndo_open)
1339 ret = ops->ndo_open(dev);
1340
1341 netpoll_poll_enable(dev);
1342
1343 if (ret)
1344 clear_bit(__LINK_STATE_START, &dev->state);
1345 else {
1346 dev->flags |= IFF_UP;
1347 dev_set_rx_mode(dev);
1348 dev_activate(dev);
1349 add_device_randomness(dev->dev_addr, dev->addr_len);
1350 }
1351
1352 return ret;
1353}
1354
1355/**
1356 * dev_open - prepare an interface for use.
1357 * @dev: device to open
1358 *
1359 * Takes a device from down to up state. The device's private open
1360 * function is invoked and then the multicast lists are loaded. Finally
1361 * the device is moved into the up state and a %NETDEV_UP message is
1362 * sent to the netdev notifier chain.
1363 *
1364 * Calling this function on an active interface is a nop. On a failure
1365 * a negative errno code is returned.
1366 */
1367int dev_open(struct net_device *dev)
1368{
1369 int ret;
1370
1371 if (dev->flags & IFF_UP)
1372 return 0;
1373
1374 ret = __dev_open(dev);
1375 if (ret < 0)
1376 return ret;
1377
1378 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
1379 call_netdevice_notifiers(NETDEV_UP, dev);
1380
1381 return ret;
1382}
1383EXPORT_SYMBOL(dev_open);
1384
1385static int __dev_close_many(struct list_head *head)
1386{
1387 struct net_device *dev;
1388
1389 ASSERT_RTNL();
1390 might_sleep();
1391
1392 list_for_each_entry(dev, head, close_list) {
1393 /* Temporarily disable netpoll until the interface is down */
1394 netpoll_poll_disable(dev);
1395
1396 call_netdevice_notifiers(NETDEV_GOING_DOWN, dev);
1397
1398 clear_bit(__LINK_STATE_START, &dev->state);
1399
1400 /* Synchronize to scheduled poll. We cannot touch poll list, it
1401 * can be even on different cpu. So just clear netif_running().
1402 *
1403 * dev->stop() will invoke napi_disable() on all of it's
1404 * napi_struct instances on this device.
1405 */
1406 smp_mb__after_atomic(); /* Commit netif_running(). */
1407 }
1408
1409 dev_deactivate_many(head);
1410
1411 list_for_each_entry(dev, head, close_list) {
1412 const struct net_device_ops *ops = dev->netdev_ops;
1413
1414 /*
1415 * Call the device specific close. This cannot fail.
1416 * Only if device is UP
1417 *
1418 * We allow it to be called even after a DETACH hot-plug
1419 * event.
1420 */
1421 if (ops->ndo_stop)
1422 ops->ndo_stop(dev);
1423
1424 dev->flags &= ~IFF_UP;
1425 netpoll_poll_enable(dev);
1426 }
1427
1428 return 0;
1429}
1430
1431static int __dev_close(struct net_device *dev)
1432{
1433 int retval;
1434 LIST_HEAD(single);
1435
1436 list_add(&dev->close_list, &single);
1437 retval = __dev_close_many(&single);
1438 list_del(&single);
1439
1440 return retval;
1441}
1442
1443int dev_close_many(struct list_head *head, bool unlink)
1444{
1445 struct net_device *dev, *tmp;
1446
1447 /* Remove the devices that don't need to be closed */
1448 list_for_each_entry_safe(dev, tmp, head, close_list)
1449 if (!(dev->flags & IFF_UP))
1450 list_del_init(&dev->close_list);
1451
1452 __dev_close_many(head);
1453
1454 list_for_each_entry_safe(dev, tmp, head, close_list) {
1455 rtmsg_ifinfo(RTM_NEWLINK, dev, IFF_UP|IFF_RUNNING, GFP_KERNEL);
1456 call_netdevice_notifiers(NETDEV_DOWN, dev);
1457 if (unlink)
1458 list_del_init(&dev->close_list);
1459 }
1460
1461 return 0;
1462}
1463EXPORT_SYMBOL(dev_close_many);
1464
1465/**
1466 * dev_close - shutdown an interface.
1467 * @dev: device to shutdown
1468 *
1469 * This function moves an active device into down state. A
1470 * %NETDEV_GOING_DOWN is sent to the netdev notifier chain. The device
1471 * is then deactivated and finally a %NETDEV_DOWN is sent to the notifier
1472 * chain.
1473 */
1474int dev_close(struct net_device *dev)
1475{
1476 if (dev->flags & IFF_UP) {
1477 LIST_HEAD(single);
1478
1479 list_add(&dev->close_list, &single);
1480 dev_close_many(&single, true);
1481 list_del(&single);
1482 }
1483 return 0;
1484}
1485EXPORT_SYMBOL(dev_close);
1486
1487
1488/**
1489 * dev_disable_lro - disable Large Receive Offload on a device
1490 * @dev: device
1491 *
1492 * Disable Large Receive Offload (LRO) on a net device. Must be
1493 * called under RTNL. This is needed if received packets may be
1494 * forwarded to another interface.
1495 */
1496void dev_disable_lro(struct net_device *dev)
1497{
1498 struct net_device *lower_dev;
1499 struct list_head *iter;
1500
1501 dev->wanted_features &= ~NETIF_F_LRO;
1502 netdev_update_features(dev);
1503
1504 if (unlikely(dev->features & NETIF_F_LRO))
1505 netdev_WARN(dev, "failed to disable LRO!\n");
1506
1507 netdev_for_each_lower_dev(dev, lower_dev, iter)
1508 dev_disable_lro(lower_dev);
1509}
1510EXPORT_SYMBOL(dev_disable_lro);
1511
1512static int call_netdevice_notifier(struct notifier_block *nb, unsigned long val,
1513 struct net_device *dev)
1514{
1515 struct netdev_notifier_info info;
1516
1517 netdev_notifier_info_init(&info, dev);
1518 return nb->notifier_call(nb, val, &info);
1519}
1520
1521static int dev_boot_phase = 1;
1522
1523/**
1524 * register_netdevice_notifier - register a network notifier block
1525 * @nb: notifier
1526 *
1527 * Register a notifier to be called when network device events occur.
1528 * The notifier passed is linked into the kernel structures and must
1529 * not be reused until it has been unregistered. A negative errno code
1530 * is returned on a failure.
1531 *
1532 * When registered all registration and up events are replayed
1533 * to the new notifier to allow device to have a race free
1534 * view of the network device list.
1535 */
1536
1537int register_netdevice_notifier(struct notifier_block *nb)
1538{
1539 struct net_device *dev;
1540 struct net_device *last;
1541 struct net *net;
1542 int err;
1543
1544 rtnl_lock();
1545 err = raw_notifier_chain_register(&netdev_chain, nb);
1546 if (err)
1547 goto unlock;
1548 if (dev_boot_phase)
1549 goto unlock;
1550 for_each_net(net) {
1551 for_each_netdev(net, dev) {
1552 err = call_netdevice_notifier(nb, NETDEV_REGISTER, dev);
1553 err = notifier_to_errno(err);
1554 if (err)
1555 goto rollback;
1556
1557 if (!(dev->flags & IFF_UP))
1558 continue;
1559
1560 call_netdevice_notifier(nb, NETDEV_UP, dev);
1561 }
1562 }
1563
1564unlock:
1565 rtnl_unlock();
1566 return err;
1567
1568rollback:
1569 last = dev;
1570 for_each_net(net) {
1571 for_each_netdev(net, dev) {
1572 if (dev == last)
1573 goto outroll;
1574
1575 if (dev->flags & IFF_UP) {
1576 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1577 dev);
1578 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1579 }
1580 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1581 }
1582 }
1583
1584outroll:
1585 raw_notifier_chain_unregister(&netdev_chain, nb);
1586 goto unlock;
1587}
1588EXPORT_SYMBOL(register_netdevice_notifier);
1589
1590/**
1591 * unregister_netdevice_notifier - unregister a network notifier block
1592 * @nb: notifier
1593 *
1594 * Unregister a notifier previously registered by
1595 * register_netdevice_notifier(). The notifier is unlinked into the
1596 * kernel structures and may then be reused. A negative errno code
1597 * is returned on a failure.
1598 *
1599 * After unregistering unregister and down device events are synthesized
1600 * for all devices on the device list to the removed notifier to remove
1601 * the need for special case cleanup code.
1602 */
1603
1604int unregister_netdevice_notifier(struct notifier_block *nb)
1605{
1606 struct net_device *dev;
1607 struct net *net;
1608 int err;
1609
1610 rtnl_lock();
1611 err = raw_notifier_chain_unregister(&netdev_chain, nb);
1612 if (err)
1613 goto unlock;
1614
1615 for_each_net(net) {
1616 for_each_netdev(net, dev) {
1617 if (dev->flags & IFF_UP) {
1618 call_netdevice_notifier(nb, NETDEV_GOING_DOWN,
1619 dev);
1620 call_netdevice_notifier(nb, NETDEV_DOWN, dev);
1621 }
1622 call_netdevice_notifier(nb, NETDEV_UNREGISTER, dev);
1623 }
1624 }
1625unlock:
1626 rtnl_unlock();
1627 return err;
1628}
1629EXPORT_SYMBOL(unregister_netdevice_notifier);
1630
1631/**
1632 * call_netdevice_notifiers_info - call all network notifier blocks
1633 * @val: value passed unmodified to notifier function
1634 * @dev: net_device pointer passed unmodified to notifier function
1635 * @info: notifier information data
1636 *
1637 * Call all network notifier blocks. Parameters and return value
1638 * are as for raw_notifier_call_chain().
1639 */
1640
1641static int call_netdevice_notifiers_info(unsigned long val,
1642 struct net_device *dev,
1643 struct netdev_notifier_info *info)
1644{
1645 ASSERT_RTNL();
1646 netdev_notifier_info_init(info, dev);
1647 return raw_notifier_call_chain(&netdev_chain, val, info);
1648}
1649
1650/**
1651 * call_netdevice_notifiers - call all network notifier blocks
1652 * @val: value passed unmodified to notifier function
1653 * @dev: net_device pointer passed unmodified to notifier function
1654 *
1655 * Call all network notifier blocks. Parameters and return value
1656 * are as for raw_notifier_call_chain().
1657 */
1658
1659int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1660{
1661 struct netdev_notifier_info info;
1662
1663 return call_netdevice_notifiers_info(val, dev, &info);
1664}
1665EXPORT_SYMBOL(call_netdevice_notifiers);
1666
1667#ifdef CONFIG_NET_INGRESS
1668static struct static_key ingress_needed __read_mostly;
1669
1670void net_inc_ingress_queue(void)
1671{
1672 static_key_slow_inc(&ingress_needed);
1673}
1674EXPORT_SYMBOL_GPL(net_inc_ingress_queue);
1675
1676void net_dec_ingress_queue(void)
1677{
1678 static_key_slow_dec(&ingress_needed);
1679}
1680EXPORT_SYMBOL_GPL(net_dec_ingress_queue);
1681#endif
1682
1683#ifdef CONFIG_NET_EGRESS
1684static struct static_key egress_needed __read_mostly;
1685
1686void net_inc_egress_queue(void)
1687{
1688 static_key_slow_inc(&egress_needed);
1689}
1690EXPORT_SYMBOL_GPL(net_inc_egress_queue);
1691
1692void net_dec_egress_queue(void)
1693{
1694 static_key_slow_dec(&egress_needed);
1695}
1696EXPORT_SYMBOL_GPL(net_dec_egress_queue);
1697#endif
1698
1699static struct static_key netstamp_needed __read_mostly;
1700#ifdef HAVE_JUMP_LABEL
1701static atomic_t netstamp_needed_deferred;
1702static atomic_t netstamp_wanted;
1703static void netstamp_clear(struct work_struct *work)
1704{
1705 int deferred = atomic_xchg(&netstamp_needed_deferred, 0);
1706 int wanted;
1707
1708 wanted = atomic_add_return(deferred, &netstamp_wanted);
1709 if (wanted > 0)
1710 static_key_enable(&netstamp_needed);
1711 else
1712 static_key_disable(&netstamp_needed);
1713}
1714static DECLARE_WORK(netstamp_work, netstamp_clear);
1715#endif
1716
1717void net_enable_timestamp(void)
1718{
1719#ifdef HAVE_JUMP_LABEL
1720 int wanted;
1721
1722 while (1) {
1723 wanted = atomic_read(&netstamp_wanted);
1724 if (wanted <= 0)
1725 break;
1726 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted + 1) == wanted)
1727 return;
1728 }
1729 atomic_inc(&netstamp_needed_deferred);
1730 schedule_work(&netstamp_work);
1731#else
1732 static_key_slow_inc(&netstamp_needed);
1733#endif
1734}
1735EXPORT_SYMBOL(net_enable_timestamp);
1736
1737void net_disable_timestamp(void)
1738{
1739#ifdef HAVE_JUMP_LABEL
1740 int wanted;
1741
1742 while (1) {
1743 wanted = atomic_read(&netstamp_wanted);
1744 if (wanted <= 1)
1745 break;
1746 if (atomic_cmpxchg(&netstamp_wanted, wanted, wanted - 1) == wanted)
1747 return;
1748 }
1749 atomic_dec(&netstamp_needed_deferred);
1750 schedule_work(&netstamp_work);
1751#else
1752 static_key_slow_dec(&netstamp_needed);
1753#endif
1754}
1755EXPORT_SYMBOL(net_disable_timestamp);
1756
1757static inline void net_timestamp_set(struct sk_buff *skb)
1758{
1759 skb->tstamp = 0;
1760 if (static_key_false(&netstamp_needed))
1761 __net_timestamp(skb);
1762}
1763
1764#define net_timestamp_check(COND, SKB) \
1765 if (static_key_false(&netstamp_needed)) { \
1766 if ((COND) && !(SKB)->tstamp) \
1767 __net_timestamp(SKB); \
1768 } \
1769
1770bool is_skb_forwardable(const struct net_device *dev, const struct sk_buff *skb)
1771{
1772 unsigned int len;
1773
1774 if (!(dev->flags & IFF_UP))
1775 return false;
1776
1777 len = dev->mtu + dev->hard_header_len + VLAN_HLEN;
1778 if (skb->len <= len)
1779 return true;
1780
1781 /* if TSO is enabled, we don't care about the length as the packet
1782 * could be forwarded without being segmented before
1783 */
1784 if (skb_is_gso(skb))
1785 return true;
1786
1787 return false;
1788}
1789EXPORT_SYMBOL_GPL(is_skb_forwardable);
1790
1791int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1792{
1793 int ret = ____dev_forward_skb(dev, skb);
1794
1795 if (likely(!ret)) {
1796 skb->protocol = eth_type_trans(skb, dev);
1797 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1798 }
1799
1800 return ret;
1801}
1802EXPORT_SYMBOL_GPL(__dev_forward_skb);
1803
1804/**
1805 * dev_forward_skb - loopback an skb to another netif
1806 *
1807 * @dev: destination network device
1808 * @skb: buffer to forward
1809 *
1810 * return values:
1811 * NET_RX_SUCCESS (no congestion)
1812 * NET_RX_DROP (packet was dropped, but freed)
1813 *
1814 * dev_forward_skb can be used for injecting an skb from the
1815 * start_xmit function of one device into the receive queue
1816 * of another device.
1817 *
1818 * The receiving device may be in another namespace, so
1819 * we have to clear all information in the skb that could
1820 * impact namespace isolation.
1821 */
1822int dev_forward_skb(struct net_device *dev, struct sk_buff *skb)
1823{
1824 return __dev_forward_skb(dev, skb) ?: netif_rx_internal(skb);
1825}
1826EXPORT_SYMBOL_GPL(dev_forward_skb);
1827
1828static inline int deliver_skb(struct sk_buff *skb,
1829 struct packet_type *pt_prev,
1830 struct net_device *orig_dev)
1831{
1832 if (unlikely(skb_orphan_frags(skb, GFP_ATOMIC)))
1833 return -ENOMEM;
1834 atomic_inc(&skb->users);
1835 return pt_prev->func(skb, skb->dev, pt_prev, orig_dev);
1836}
1837
1838static inline void deliver_ptype_list_skb(struct sk_buff *skb,
1839 struct packet_type **pt,
1840 struct net_device *orig_dev,
1841 __be16 type,
1842 struct list_head *ptype_list)
1843{
1844 struct packet_type *ptype, *pt_prev = *pt;
1845
1846 list_for_each_entry_rcu(ptype, ptype_list, list) {
1847 if (ptype->type != type)
1848 continue;
1849 if (pt_prev)
1850 deliver_skb(skb, pt_prev, orig_dev);
1851 pt_prev = ptype;
1852 }
1853 *pt = pt_prev;
1854}
1855
1856static inline bool skb_loop_sk(struct packet_type *ptype, struct sk_buff *skb)
1857{
1858 if (!ptype->af_packet_priv || !skb->sk)
1859 return false;
1860
1861 if (ptype->id_match)
1862 return ptype->id_match(ptype, skb->sk);
1863 else if ((struct sock *)ptype->af_packet_priv == skb->sk)
1864 return true;
1865
1866 return false;
1867}
1868
1869/*
1870 * Support routine. Sends outgoing frames to any network
1871 * taps currently in use.
1872 */
1873
1874void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
1875{
1876 struct packet_type *ptype;
1877 struct sk_buff *skb2 = NULL;
1878 struct packet_type *pt_prev = NULL;
1879 struct list_head *ptype_list = &ptype_all;
1880
1881 rcu_read_lock();
1882again:
1883 list_for_each_entry_rcu(ptype, ptype_list, list) {
1884 /* Never send packets back to the socket
1885 * they originated from - MvS (miquels@drinkel.ow.org)
1886 */
1887 if (skb_loop_sk(ptype, skb))
1888 continue;
1889
1890 if (pt_prev) {
1891 deliver_skb(skb2, pt_prev, skb->dev);
1892 pt_prev = ptype;
1893 continue;
1894 }
1895
1896 /* need to clone skb, done only once */
1897 skb2 = skb_clone(skb, GFP_ATOMIC);
1898 if (!skb2)
1899 goto out_unlock;
1900
1901 net_timestamp_set(skb2);
1902
1903 /* skb->nh should be correctly
1904 * set by sender, so that the second statement is
1905 * just protection against buggy protocols.
1906 */
1907 skb_reset_mac_header(skb2);
1908
1909 if (skb_network_header(skb2) < skb2->data ||
1910 skb_network_header(skb2) > skb_tail_pointer(skb2)) {
1911 net_crit_ratelimited("protocol %04x is buggy, dev %s\n",
1912 ntohs(skb2->protocol),
1913 dev->name);
1914 skb_reset_network_header(skb2);
1915 }
1916
1917 skb2->transport_header = skb2->network_header;
1918 skb2->pkt_type = PACKET_OUTGOING;
1919 pt_prev = ptype;
1920 }
1921
1922 if (ptype_list == &ptype_all) {
1923 ptype_list = &dev->ptype_all;
1924 goto again;
1925 }
1926out_unlock:
1927 if (pt_prev)
1928 pt_prev->func(skb2, skb->dev, pt_prev, skb->dev);
1929 rcu_read_unlock();
1930}
1931EXPORT_SYMBOL_GPL(dev_queue_xmit_nit);
1932
1933/**
1934 * netif_setup_tc - Handle tc mappings on real_num_tx_queues change
1935 * @dev: Network device
1936 * @txq: number of queues available
1937 *
1938 * If real_num_tx_queues is changed the tc mappings may no longer be
1939 * valid. To resolve this verify the tc mapping remains valid and if
1940 * not NULL the mapping. With no priorities mapping to this
1941 * offset/count pair it will no longer be used. In the worst case TC0
1942 * is invalid nothing can be done so disable priority mappings. If is
1943 * expected that drivers will fix this mapping if they can before
1944 * calling netif_set_real_num_tx_queues.
1945 */
1946static void netif_setup_tc(struct net_device *dev, unsigned int txq)
1947{
1948 int i;
1949 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
1950
1951 /* If TC0 is invalidated disable TC mapping */
1952 if (tc->offset + tc->count > txq) {
1953 pr_warn("Number of in use tx queues changed invalidating tc mappings. Priority traffic classification disabled!\n");
1954 dev->num_tc = 0;
1955 return;
1956 }
1957
1958 /* Invalidated prio to tc mappings set to TC0 */
1959 for (i = 1; i < TC_BITMASK + 1; i++) {
1960 int q = netdev_get_prio_tc_map(dev, i);
1961
1962 tc = &dev->tc_to_txq[q];
1963 if (tc->offset + tc->count > txq) {
1964 pr_warn("Number of in use tx queues changed. Priority %i to tc mapping %i is no longer valid. Setting map to 0\n",
1965 i, q);
1966 netdev_set_prio_tc_map(dev, i, 0);
1967 }
1968 }
1969}
1970
1971int netdev_txq_to_tc(struct net_device *dev, unsigned int txq)
1972{
1973 if (dev->num_tc) {
1974 struct netdev_tc_txq *tc = &dev->tc_to_txq[0];
1975 int i;
1976
1977 for (i = 0; i < TC_MAX_QUEUE; i++, tc++) {
1978 if ((txq - tc->offset) < tc->count)
1979 return i;
1980 }
1981
1982 return -1;
1983 }
1984
1985 return 0;
1986}
1987
1988#ifdef CONFIG_XPS
1989static DEFINE_MUTEX(xps_map_mutex);
1990#define xmap_dereference(P) \
1991 rcu_dereference_protected((P), lockdep_is_held(&xps_map_mutex))
1992
1993static bool remove_xps_queue(struct xps_dev_maps *dev_maps,
1994 int tci, u16 index)
1995{
1996 struct xps_map *map = NULL;
1997 int pos;
1998
1999 if (dev_maps)
2000 map = xmap_dereference(dev_maps->cpu_map[tci]);
2001 if (!map)
2002 return false;
2003
2004 for (pos = map->len; pos--;) {
2005 if (map->queues[pos] != index)
2006 continue;
2007
2008 if (map->len > 1) {
2009 map->queues[pos] = map->queues[--map->len];
2010 break;
2011 }
2012
2013 RCU_INIT_POINTER(dev_maps->cpu_map[tci], NULL);
2014 kfree_rcu(map, rcu);
2015 return false;
2016 }
2017
2018 return true;
2019}
2020
2021static bool remove_xps_queue_cpu(struct net_device *dev,
2022 struct xps_dev_maps *dev_maps,
2023 int cpu, u16 offset, u16 count)
2024{
2025 int num_tc = dev->num_tc ? : 1;
2026 bool active = false;
2027 int tci;
2028
2029 for (tci = cpu * num_tc; num_tc--; tci++) {
2030 int i, j;
2031
2032 for (i = count, j = offset; i--; j++) {
2033 if (!remove_xps_queue(dev_maps, cpu, j))
2034 break;
2035 }
2036
2037 active |= i < 0;
2038 }
2039
2040 return active;
2041}
2042
2043static void netif_reset_xps_queues(struct net_device *dev, u16 offset,
2044 u16 count)
2045{
2046 struct xps_dev_maps *dev_maps;
2047 int cpu, i;
2048 bool active = false;
2049
2050 mutex_lock(&xps_map_mutex);
2051 dev_maps = xmap_dereference(dev->xps_maps);
2052
2053 if (!dev_maps)
2054 goto out_no_maps;
2055
2056 for_each_possible_cpu(cpu)
2057 active |= remove_xps_queue_cpu(dev, dev_maps, cpu,
2058 offset, count);
2059
2060 if (!active) {
2061 RCU_INIT_POINTER(dev->xps_maps, NULL);
2062 kfree_rcu(dev_maps, rcu);
2063 }
2064
2065 for (i = offset + (count - 1); count--; i--)
2066 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, i),
2067 NUMA_NO_NODE);
2068
2069out_no_maps:
2070 mutex_unlock(&xps_map_mutex);
2071}
2072
2073static void netif_reset_xps_queues_gt(struct net_device *dev, u16 index)
2074{
2075 netif_reset_xps_queues(dev, index, dev->num_tx_queues - index);
2076}
2077
2078static struct xps_map *expand_xps_map(struct xps_map *map,
2079 int cpu, u16 index)
2080{
2081 struct xps_map *new_map;
2082 int alloc_len = XPS_MIN_MAP_ALLOC;
2083 int i, pos;
2084
2085 for (pos = 0; map && pos < map->len; pos++) {
2086 if (map->queues[pos] != index)
2087 continue;
2088 return map;
2089 }
2090
2091 /* Need to add queue to this CPU's existing map */
2092 if (map) {
2093 if (pos < map->alloc_len)
2094 return map;
2095
2096 alloc_len = map->alloc_len * 2;
2097 }
2098
2099 /* Need to allocate new map to store queue on this CPU's map */
2100 new_map = kzalloc_node(XPS_MAP_SIZE(alloc_len), GFP_KERNEL,
2101 cpu_to_node(cpu));
2102 if (!new_map)
2103 return NULL;
2104
2105 for (i = 0; i < pos; i++)
2106 new_map->queues[i] = map->queues[i];
2107 new_map->alloc_len = alloc_len;
2108 new_map->len = pos;
2109
2110 return new_map;
2111}
2112
2113int netif_set_xps_queue(struct net_device *dev, const struct cpumask *mask,
2114 u16 index)
2115{
2116 struct xps_dev_maps *dev_maps, *new_dev_maps = NULL;
2117 int i, cpu, tci, numa_node_id = -2;
2118 int maps_sz, num_tc = 1, tc = 0;
2119 struct xps_map *map, *new_map;
2120 bool active = false;
2121
2122 if (dev->num_tc) {
2123 num_tc = dev->num_tc;
2124 tc = netdev_txq_to_tc(dev, index);
2125 if (tc < 0)
2126 return -EINVAL;
2127 }
2128
2129 maps_sz = XPS_DEV_MAPS_SIZE(num_tc);
2130 if (maps_sz < L1_CACHE_BYTES)
2131 maps_sz = L1_CACHE_BYTES;
2132
2133 mutex_lock(&xps_map_mutex);
2134
2135 dev_maps = xmap_dereference(dev->xps_maps);
2136
2137 /* allocate memory for queue storage */
2138 for_each_cpu_and(cpu, cpu_online_mask, mask) {
2139 if (!new_dev_maps)
2140 new_dev_maps = kzalloc(maps_sz, GFP_KERNEL);
2141 if (!new_dev_maps) {
2142 mutex_unlock(&xps_map_mutex);
2143 return -ENOMEM;
2144 }
2145
2146 tci = cpu * num_tc + tc;
2147 map = dev_maps ? xmap_dereference(dev_maps->cpu_map[tci]) :
2148 NULL;
2149
2150 map = expand_xps_map(map, cpu, index);
2151 if (!map)
2152 goto error;
2153
2154 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
2155 }
2156
2157 if (!new_dev_maps)
2158 goto out_no_new_maps;
2159
2160 for_each_possible_cpu(cpu) {
2161 /* copy maps belonging to foreign traffic classes */
2162 for (i = tc, tci = cpu * num_tc; dev_maps && i--; tci++) {
2163 /* fill in the new device map from the old device map */
2164 map = xmap_dereference(dev_maps->cpu_map[tci]);
2165 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
2166 }
2167
2168 /* We need to explicitly update tci as prevous loop
2169 * could break out early if dev_maps is NULL.
2170 */
2171 tci = cpu * num_tc + tc;
2172
2173 if (cpumask_test_cpu(cpu, mask) && cpu_online(cpu)) {
2174 /* add queue to CPU maps */
2175 int pos = 0;
2176
2177 map = xmap_dereference(new_dev_maps->cpu_map[tci]);
2178 while ((pos < map->len) && (map->queues[pos] != index))
2179 pos++;
2180
2181 if (pos == map->len)
2182 map->queues[map->len++] = index;
2183#ifdef CONFIG_NUMA
2184 if (numa_node_id == -2)
2185 numa_node_id = cpu_to_node(cpu);
2186 else if (numa_node_id != cpu_to_node(cpu))
2187 numa_node_id = -1;
2188#endif
2189 } else if (dev_maps) {
2190 /* fill in the new device map from the old device map */
2191 map = xmap_dereference(dev_maps->cpu_map[tci]);
2192 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
2193 }
2194
2195 /* copy maps belonging to foreign traffic classes */
2196 for (i = num_tc - tc, tci++; dev_maps && --i; tci++) {
2197 /* fill in the new device map from the old device map */
2198 map = xmap_dereference(dev_maps->cpu_map[tci]);
2199 RCU_INIT_POINTER(new_dev_maps->cpu_map[tci], map);
2200 }
2201 }
2202
2203 rcu_assign_pointer(dev->xps_maps, new_dev_maps);
2204
2205 /* Cleanup old maps */
2206 if (!dev_maps)
2207 goto out_no_old_maps;
2208
2209 for_each_possible_cpu(cpu) {
2210 for (i = num_tc, tci = cpu * num_tc; i--; tci++) {
2211 new_map = xmap_dereference(new_dev_maps->cpu_map[tci]);
2212 map = xmap_dereference(dev_maps->cpu_map[tci]);
2213 if (map && map != new_map)
2214 kfree_rcu(map, rcu);
2215 }
2216 }
2217
2218 kfree_rcu(dev_maps, rcu);
2219
2220out_no_old_maps:
2221 dev_maps = new_dev_maps;
2222 active = true;
2223
2224out_no_new_maps:
2225 /* update Tx queue numa node */
2226 netdev_queue_numa_node_write(netdev_get_tx_queue(dev, index),
2227 (numa_node_id >= 0) ? numa_node_id :
2228 NUMA_NO_NODE);
2229
2230 if (!dev_maps)
2231 goto out_no_maps;
2232
2233 /* removes queue from unused CPUs */
2234 for_each_possible_cpu(cpu) {
2235 for (i = tc, tci = cpu * num_tc; i--; tci++)
2236 active |= remove_xps_queue(dev_maps, tci, index);
2237 if (!cpumask_test_cpu(cpu, mask) || !cpu_online(cpu))
2238 active |= remove_xps_queue(dev_maps, tci, index);
2239 for (i = num_tc - tc, tci++; --i; tci++)
2240 active |= remove_xps_queue(dev_maps, tci, index);
2241 }
2242
2243 /* free map if not active */
2244 if (!active) {
2245 RCU_INIT_POINTER(dev->xps_maps, NULL);
2246 kfree_rcu(dev_maps, rcu);
2247 }
2248
2249out_no_maps:
2250 mutex_unlock(&xps_map_mutex);
2251
2252 return 0;
2253error:
2254 /* remove any maps that we added */
2255 for_each_possible_cpu(cpu) {
2256 for (i = num_tc, tci = cpu * num_tc; i--; tci++) {
2257 new_map = xmap_dereference(new_dev_maps->cpu_map[tci]);
2258 map = dev_maps ?
2259 xmap_dereference(dev_maps->cpu_map[tci]) :
2260 NULL;
2261 if (new_map && new_map != map)
2262 kfree(new_map);
2263 }
2264 }
2265
2266 mutex_unlock(&xps_map_mutex);
2267
2268 kfree(new_dev_maps);
2269 return -ENOMEM;
2270}
2271EXPORT_SYMBOL(netif_set_xps_queue);
2272
2273#endif
2274void netdev_reset_tc(struct net_device *dev)
2275{
2276#ifdef CONFIG_XPS
2277 netif_reset_xps_queues_gt(dev, 0);
2278#endif
2279 dev->num_tc = 0;
2280 memset(dev->tc_to_txq, 0, sizeof(dev->tc_to_txq));
2281 memset(dev->prio_tc_map, 0, sizeof(dev->prio_tc_map));
2282}
2283EXPORT_SYMBOL(netdev_reset_tc);
2284
2285int netdev_set_tc_queue(struct net_device *dev, u8 tc, u16 count, u16 offset)
2286{
2287 if (tc >= dev->num_tc)
2288 return -EINVAL;
2289
2290#ifdef CONFIG_XPS
2291 netif_reset_xps_queues(dev, offset, count);
2292#endif
2293 dev->tc_to_txq[tc].count = count;
2294 dev->tc_to_txq[tc].offset = offset;
2295 return 0;
2296}
2297EXPORT_SYMBOL(netdev_set_tc_queue);
2298
2299int netdev_set_num_tc(struct net_device *dev, u8 num_tc)
2300{
2301 if (num_tc > TC_MAX_QUEUE)
2302 return -EINVAL;
2303
2304#ifdef CONFIG_XPS
2305 netif_reset_xps_queues_gt(dev, 0);
2306#endif
2307 dev->num_tc = num_tc;
2308 return 0;
2309}
2310EXPORT_SYMBOL(netdev_set_num_tc);
2311
2312/*
2313 * Routine to help set real_num_tx_queues. To avoid skbs mapped to queues
2314 * greater then real_num_tx_queues stale skbs on the qdisc must be flushed.
2315 */
2316int netif_set_real_num_tx_queues(struct net_device *dev, unsigned int txq)
2317{
2318 int rc;
2319
2320 if (txq < 1 || txq > dev->num_tx_queues)
2321 return -EINVAL;
2322
2323 if (dev->reg_state == NETREG_REGISTERED ||
2324 dev->reg_state == NETREG_UNREGISTERING) {
2325 ASSERT_RTNL();
2326
2327 rc = netdev_queue_update_kobjects(dev, dev->real_num_tx_queues,
2328 txq);
2329 if (rc)
2330 return rc;
2331
2332 if (dev->num_tc)
2333 netif_setup_tc(dev, txq);
2334
2335 if (txq < dev->real_num_tx_queues) {
2336 qdisc_reset_all_tx_gt(dev, txq);
2337#ifdef CONFIG_XPS
2338 netif_reset_xps_queues_gt(dev, txq);
2339#endif
2340 }
2341 }
2342
2343 dev->real_num_tx_queues = txq;
2344 return 0;
2345}
2346EXPORT_SYMBOL(netif_set_real_num_tx_queues);
2347
2348#ifdef CONFIG_SYSFS
2349/**
2350 * netif_set_real_num_rx_queues - set actual number of RX queues used
2351 * @dev: Network device
2352 * @rxq: Actual number of RX queues
2353 *
2354 * This must be called either with the rtnl_lock held or before
2355 * registration of the net device. Returns 0 on success, or a
2356 * negative error code. If called before registration, it always
2357 * succeeds.
2358 */
2359int netif_set_real_num_rx_queues(struct net_device *dev, unsigned int rxq)
2360{
2361 int rc;
2362
2363 if (rxq < 1 || rxq > dev->num_rx_queues)
2364 return -EINVAL;
2365
2366 if (dev->reg_state == NETREG_REGISTERED) {
2367 ASSERT_RTNL();
2368
2369 rc = net_rx_queue_update_kobjects(dev, dev->real_num_rx_queues,
2370 rxq);
2371 if (rc)
2372 return rc;
2373 }
2374
2375 dev->real_num_rx_queues = rxq;
2376 return 0;
2377}
2378EXPORT_SYMBOL(netif_set_real_num_rx_queues);
2379#endif
2380
2381/**
2382 * netif_get_num_default_rss_queues - default number of RSS queues
2383 *
2384 * This routine should set an upper limit on the number of RSS queues
2385 * used by default by multiqueue devices.
2386 */
2387int netif_get_num_default_rss_queues(void)
2388{
2389 return is_kdump_kernel() ?
2390 1 : min_t(int, DEFAULT_MAX_NUM_RSS_QUEUES, num_online_cpus());
2391}
2392EXPORT_SYMBOL(netif_get_num_default_rss_queues);
2393
2394static void __netif_reschedule(struct Qdisc *q)
2395{
2396 struct softnet_data *sd;
2397 unsigned long flags;
2398
2399 local_irq_save(flags);
2400 sd = this_cpu_ptr(&softnet_data);
2401 q->next_sched = NULL;
2402 *sd->output_queue_tailp = q;
2403 sd->output_queue_tailp = &q->next_sched;
2404 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2405 local_irq_restore(flags);
2406}
2407
2408void __netif_schedule(struct Qdisc *q)
2409{
2410 if (!test_and_set_bit(__QDISC_STATE_SCHED, &q->state))
2411 __netif_reschedule(q);
2412}
2413EXPORT_SYMBOL(__netif_schedule);
2414
2415struct dev_kfree_skb_cb {
2416 enum skb_free_reason reason;
2417};
2418
2419static struct dev_kfree_skb_cb *get_kfree_skb_cb(const struct sk_buff *skb)
2420{
2421 return (struct dev_kfree_skb_cb *)skb->cb;
2422}
2423
2424void netif_schedule_queue(struct netdev_queue *txq)
2425{
2426 rcu_read_lock();
2427 if (!(txq->state & QUEUE_STATE_ANY_XOFF)) {
2428 struct Qdisc *q = rcu_dereference(txq->qdisc);
2429
2430 __netif_schedule(q);
2431 }
2432 rcu_read_unlock();
2433}
2434EXPORT_SYMBOL(netif_schedule_queue);
2435
2436void netif_tx_wake_queue(struct netdev_queue *dev_queue)
2437{
2438 if (test_and_clear_bit(__QUEUE_STATE_DRV_XOFF, &dev_queue->state)) {
2439 struct Qdisc *q;
2440
2441 rcu_read_lock();
2442 q = rcu_dereference(dev_queue->qdisc);
2443 __netif_schedule(q);
2444 rcu_read_unlock();
2445 }
2446}
2447EXPORT_SYMBOL(netif_tx_wake_queue);
2448
2449void __dev_kfree_skb_irq(struct sk_buff *skb, enum skb_free_reason reason)
2450{
2451 unsigned long flags;
2452
2453 if (unlikely(!skb))
2454 return;
2455
2456 if (likely(atomic_read(&skb->users) == 1)) {
2457 smp_rmb();
2458 atomic_set(&skb->users, 0);
2459 } else if (likely(!atomic_dec_and_test(&skb->users))) {
2460 return;
2461 }
2462 get_kfree_skb_cb(skb)->reason = reason;
2463 local_irq_save(flags);
2464 skb->next = __this_cpu_read(softnet_data.completion_queue);
2465 __this_cpu_write(softnet_data.completion_queue, skb);
2466 raise_softirq_irqoff(NET_TX_SOFTIRQ);
2467 local_irq_restore(flags);
2468}
2469EXPORT_SYMBOL(__dev_kfree_skb_irq);
2470
2471void __dev_kfree_skb_any(struct sk_buff *skb, enum skb_free_reason reason)
2472{
2473 if (in_irq() || irqs_disabled())
2474 __dev_kfree_skb_irq(skb, reason);
2475 else
2476 dev_kfree_skb(skb);
2477}
2478EXPORT_SYMBOL(__dev_kfree_skb_any);
2479
2480
2481/**
2482 * netif_device_detach - mark device as removed
2483 * @dev: network device
2484 *
2485 * Mark device as removed from system and therefore no longer available.
2486 */
2487void netif_device_detach(struct net_device *dev)
2488{
2489 if (test_and_clear_bit(__LINK_STATE_PRESENT, &dev->state) &&
2490 netif_running(dev)) {
2491 netif_tx_stop_all_queues(dev);
2492 }
2493}
2494EXPORT_SYMBOL(netif_device_detach);
2495
2496/**
2497 * netif_device_attach - mark device as attached
2498 * @dev: network device
2499 *
2500 * Mark device as attached from system and restart if needed.
2501 */
2502void netif_device_attach(struct net_device *dev)
2503{
2504 if (!test_and_set_bit(__LINK_STATE_PRESENT, &dev->state) &&
2505 netif_running(dev)) {
2506 netif_tx_wake_all_queues(dev);
2507 __netdev_watchdog_up(dev);
2508 }
2509}
2510EXPORT_SYMBOL(netif_device_attach);
2511
2512/*
2513 * Returns a Tx hash based on the given packet descriptor a Tx queues' number
2514 * to be used as a distribution range.
2515 */
2516u16 __skb_tx_hash(const struct net_device *dev, struct sk_buff *skb,
2517 unsigned int num_tx_queues)
2518{
2519 u32 hash;
2520 u16 qoffset = 0;
2521 u16 qcount = num_tx_queues;
2522
2523 if (skb_rx_queue_recorded(skb)) {
2524 hash = skb_get_rx_queue(skb);
2525 while (unlikely(hash >= num_tx_queues))
2526 hash -= num_tx_queues;
2527 return hash;
2528 }
2529
2530 if (dev->num_tc) {
2531 u8 tc = netdev_get_prio_tc_map(dev, skb->priority);
2532
2533 qoffset = dev->tc_to_txq[tc].offset;
2534 qcount = dev->tc_to_txq[tc].count;
2535 }
2536
2537 return (u16) reciprocal_scale(skb_get_hash(skb), qcount) + qoffset;
2538}
2539EXPORT_SYMBOL(__skb_tx_hash);
2540
2541static void skb_warn_bad_offload(const struct sk_buff *skb)
2542{
2543 static const netdev_features_t null_features;
2544 struct net_device *dev = skb->dev;
2545 const char *name = "";
2546
2547 if (!net_ratelimit())
2548 return;
2549
2550 if (dev) {
2551 if (dev->dev.parent)
2552 name = dev_driver_string(dev->dev.parent);
2553 else
2554 name = netdev_name(dev);
2555 }
2556 WARN(1, "%s: caps=(%pNF, %pNF) len=%d data_len=%d gso_size=%d "
2557 "gso_type=%d ip_summed=%d\n",
2558 name, dev ? &dev->features : &null_features,
2559 skb->sk ? &skb->sk->sk_route_caps : &null_features,
2560 skb->len, skb->data_len, skb_shinfo(skb)->gso_size,
2561 skb_shinfo(skb)->gso_type, skb->ip_summed);
2562}
2563
2564/*
2565 * Invalidate hardware checksum when packet is to be mangled, and
2566 * complete checksum manually on outgoing path.
2567 */
2568int skb_checksum_help(struct sk_buff *skb)
2569{
2570 __wsum csum;
2571 int ret = 0, offset;
2572
2573 if (skb->ip_summed == CHECKSUM_COMPLETE)
2574 goto out_set_summed;
2575
2576 if (unlikely(skb_shinfo(skb)->gso_size)) {
2577 skb_warn_bad_offload(skb);
2578 return -EINVAL;
2579 }
2580
2581 /* Before computing a checksum, we should make sure no frag could
2582 * be modified by an external entity : checksum could be wrong.
2583 */
2584 if (skb_has_shared_frag(skb)) {
2585 ret = __skb_linearize(skb);
2586 if (ret)
2587 goto out;
2588 }
2589
2590 offset = skb_checksum_start_offset(skb);
2591 BUG_ON(offset >= skb_headlen(skb));
2592 csum = skb_checksum(skb, offset, skb->len - offset, 0);
2593
2594 offset += skb->csum_offset;
2595 BUG_ON(offset + sizeof(__sum16) > skb_headlen(skb));
2596
2597 if (skb_cloned(skb) &&
2598 !skb_clone_writable(skb, offset + sizeof(__sum16))) {
2599 ret = pskb_expand_head(skb, 0, 0, GFP_ATOMIC);
2600 if (ret)
2601 goto out;
2602 }
2603
2604 *(__sum16 *)(skb->data + offset) = csum_fold(csum) ?: CSUM_MANGLED_0;
2605out_set_summed:
2606 skb->ip_summed = CHECKSUM_NONE;
2607out:
2608 return ret;
2609}
2610EXPORT_SYMBOL(skb_checksum_help);
2611
2612__be16 skb_network_protocol(struct sk_buff *skb, int *depth)
2613{
2614 __be16 type = skb->protocol;
2615
2616 /* Tunnel gso handlers can set protocol to ethernet. */
2617 if (type == htons(ETH_P_TEB)) {
2618 struct ethhdr *eth;
2619
2620 if (unlikely(!pskb_may_pull(skb, sizeof(struct ethhdr))))
2621 return 0;
2622
2623 eth = (struct ethhdr *)skb_mac_header(skb);
2624 type = eth->h_proto;
2625 }
2626
2627 return __vlan_get_protocol(skb, type, depth);
2628}
2629
2630/**
2631 * skb_mac_gso_segment - mac layer segmentation handler.
2632 * @skb: buffer to segment
2633 * @features: features for the output path (see dev->features)
2634 */
2635struct sk_buff *skb_mac_gso_segment(struct sk_buff *skb,
2636 netdev_features_t features)
2637{
2638 struct sk_buff *segs = ERR_PTR(-EPROTONOSUPPORT);
2639 struct packet_offload *ptype;
2640 int vlan_depth = skb->mac_len;
2641 __be16 type = skb_network_protocol(skb, &vlan_depth);
2642
2643 if (unlikely(!type))
2644 return ERR_PTR(-EINVAL);
2645
2646 __skb_pull(skb, vlan_depth);
2647
2648 rcu_read_lock();
2649 list_for_each_entry_rcu(ptype, &offload_base, list) {
2650 if (ptype->type == type && ptype->callbacks.gso_segment) {
2651 segs = ptype->callbacks.gso_segment(skb, features);
2652 break;
2653 }
2654 }
2655 rcu_read_unlock();
2656
2657 __skb_push(skb, skb->data - skb_mac_header(skb));
2658
2659 return segs;
2660}
2661EXPORT_SYMBOL(skb_mac_gso_segment);
2662
2663
2664/* openvswitch calls this on rx path, so we need a different check.
2665 */
2666static inline bool skb_needs_check(struct sk_buff *skb, bool tx_path)
2667{
2668 if (tx_path)
2669 return skb->ip_summed != CHECKSUM_PARTIAL &&
2670 skb->ip_summed != CHECKSUM_NONE;
2671
2672 return skb->ip_summed == CHECKSUM_NONE;
2673}
2674
2675/**
2676 * __skb_gso_segment - Perform segmentation on skb.
2677 * @skb: buffer to segment
2678 * @features: features for the output path (see dev->features)
2679 * @tx_path: whether it is called in TX path
2680 *
2681 * This function segments the given skb and returns a list of segments.
2682 *
2683 * It may return NULL if the skb requires no segmentation. This is
2684 * only possible when GSO is used for verifying header integrity.
2685 *
2686 * Segmentation preserves SKB_SGO_CB_OFFSET bytes of previous skb cb.
2687 */
2688struct sk_buff *__skb_gso_segment(struct sk_buff *skb,
2689 netdev_features_t features, bool tx_path)
2690{
2691 struct sk_buff *segs;
2692
2693 if (unlikely(skb_needs_check(skb, tx_path))) {
2694 int err;
2695
2696 /* We're going to init ->check field in TCP or UDP header */
2697 err = skb_cow_head(skb, 0);
2698 if (err < 0)
2699 return ERR_PTR(err);
2700 }
2701
2702 /* Only report GSO partial support if it will enable us to
2703 * support segmentation on this frame without needing additional
2704 * work.
2705 */
2706 if (features & NETIF_F_GSO_PARTIAL) {
2707 netdev_features_t partial_features = NETIF_F_GSO_ROBUST;
2708 struct net_device *dev = skb->dev;
2709
2710 partial_features |= dev->features & dev->gso_partial_features;
2711 if (!skb_gso_ok(skb, features | partial_features))
2712 features &= ~NETIF_F_GSO_PARTIAL;
2713 }
2714
2715 BUILD_BUG_ON(SKB_SGO_CB_OFFSET +
2716 sizeof(*SKB_GSO_CB(skb)) > sizeof(skb->cb));
2717
2718 SKB_GSO_CB(skb)->mac_offset = skb_headroom(skb);
2719 SKB_GSO_CB(skb)->encap_level = 0;
2720
2721 skb_reset_mac_header(skb);
2722 skb_reset_mac_len(skb);
2723
2724 segs = skb_mac_gso_segment(skb, features);
2725
2726 if (unlikely(skb_needs_check(skb, tx_path)))
2727 skb_warn_bad_offload(skb);
2728
2729 return segs;
2730}
2731EXPORT_SYMBOL(__skb_gso_segment);
2732
2733/* Take action when hardware reception checksum errors are detected. */
2734#ifdef CONFIG_BUG
2735void netdev_rx_csum_fault(struct net_device *dev)
2736{
2737 if (net_ratelimit()) {
2738 pr_err("%s: hw csum failure\n", dev ? dev->name : "<unknown>");
2739 dump_stack();
2740 }
2741}
2742EXPORT_SYMBOL(netdev_rx_csum_fault);
2743#endif
2744
2745/* Actually, we should eliminate this check as soon as we know, that:
2746 * 1. IOMMU is present and allows to map all the memory.
2747 * 2. No high memory really exists on this machine.
2748 */
2749
2750static int illegal_highdma(struct net_device *dev, struct sk_buff *skb)
2751{
2752#ifdef CONFIG_HIGHMEM
2753 int i;
2754
2755 if (!(dev->features & NETIF_F_HIGHDMA)) {
2756 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2757 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2758
2759 if (PageHighMem(skb_frag_page(frag)))
2760 return 1;
2761 }
2762 }
2763
2764 if (PCI_DMA_BUS_IS_PHYS) {
2765 struct device *pdev = dev->dev.parent;
2766
2767 if (!pdev)
2768 return 0;
2769 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
2770 skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
2771 dma_addr_t addr = page_to_phys(skb_frag_page(frag));
2772
2773 if (!pdev->dma_mask || addr + PAGE_SIZE - 1 > *pdev->dma_mask)
2774 return 1;
2775 }
2776 }
2777#endif
2778 return 0;
2779}
2780
2781/* If MPLS offload request, verify we are testing hardware MPLS features
2782 * instead of standard features for the netdev.
2783 */
2784#if IS_ENABLED(CONFIG_NET_MPLS_GSO)
2785static netdev_features_t net_mpls_features(struct sk_buff *skb,
2786 netdev_features_t features,
2787 __be16 type)
2788{
2789 if (eth_p_mpls(type))
2790 features &= skb->dev->mpls_features;
2791
2792 return features;
2793}
2794#else
2795static netdev_features_t net_mpls_features(struct sk_buff *skb,
2796 netdev_features_t features,
2797 __be16 type)
2798{
2799 return features;
2800}
2801#endif
2802
2803static netdev_features_t harmonize_features(struct sk_buff *skb,
2804 netdev_features_t features)
2805{
2806 int tmp;
2807 __be16 type;
2808
2809 type = skb_network_protocol(skb, &tmp);
2810 features = net_mpls_features(skb, features, type);
2811
2812 if (skb->ip_summed != CHECKSUM_NONE &&
2813 !can_checksum_protocol(features, type)) {
2814 features &= ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2815 }
2816 if (illegal_highdma(skb->dev, skb))
2817 features &= ~NETIF_F_SG;
2818
2819 return features;
2820}
2821
2822netdev_features_t passthru_features_check(struct sk_buff *skb,
2823 struct net_device *dev,
2824 netdev_features_t features)
2825{
2826 return features;
2827}
2828EXPORT_SYMBOL(passthru_features_check);
2829
2830static netdev_features_t dflt_features_check(const struct sk_buff *skb,
2831 struct net_device *dev,
2832 netdev_features_t features)
2833{
2834 return vlan_features_check(skb, features);
2835}
2836
2837static netdev_features_t gso_features_check(const struct sk_buff *skb,
2838 struct net_device *dev,
2839 netdev_features_t features)
2840{
2841 u16 gso_segs = skb_shinfo(skb)->gso_segs;
2842
2843 if (gso_segs > dev->gso_max_segs)
2844 return features & ~NETIF_F_GSO_MASK;
2845
2846 /* Support for GSO partial features requires software
2847 * intervention before we can actually process the packets
2848 * so we need to strip support for any partial features now
2849 * and we can pull them back in after we have partially
2850 * segmented the frame.
2851 */
2852 if (!(skb_shinfo(skb)->gso_type & SKB_GSO_PARTIAL))
2853 features &= ~dev->gso_partial_features;
2854
2855 /* Make sure to clear the IPv4 ID mangling feature if the
2856 * IPv4 header has the potential to be fragmented.
2857 */
2858 if (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV4) {
2859 struct iphdr *iph = skb->encapsulation ?
2860 inner_ip_hdr(skb) : ip_hdr(skb);
2861
2862 if (!(iph->frag_off & htons(IP_DF)))
2863 features &= ~NETIF_F_TSO_MANGLEID;
2864 }
2865
2866 return features;
2867}
2868
2869netdev_features_t netif_skb_features(struct sk_buff *skb)
2870{
2871 struct net_device *dev = skb->dev;
2872 netdev_features_t features = dev->features;
2873
2874 if (skb_is_gso(skb))
2875 features = gso_features_check(skb, dev, features);
2876
2877 /* If encapsulation offload request, verify we are testing
2878 * hardware encapsulation features instead of standard
2879 * features for the netdev
2880 */
2881 if (skb->encapsulation)
2882 features &= dev->hw_enc_features;
2883
2884 if (skb_vlan_tagged(skb))
2885 features = netdev_intersect_features(features,
2886 dev->vlan_features |
2887 NETIF_F_HW_VLAN_CTAG_TX |
2888 NETIF_F_HW_VLAN_STAG_TX);
2889
2890 if (dev->netdev_ops->ndo_features_check)
2891 features &= dev->netdev_ops->ndo_features_check(skb, dev,
2892 features);
2893 else
2894 features &= dflt_features_check(skb, dev, features);
2895
2896 return harmonize_features(skb, features);
2897}
2898EXPORT_SYMBOL(netif_skb_features);
2899
2900static int xmit_one(struct sk_buff *skb, struct net_device *dev,
2901 struct netdev_queue *txq, bool more)
2902{
2903 unsigned int len;
2904 int rc;
2905
2906 if (!list_empty(&ptype_all) || !list_empty(&dev->ptype_all))
2907 dev_queue_xmit_nit(skb, dev);
2908
2909 len = skb->len;
2910 trace_net_dev_start_xmit(skb, dev);
2911 rc = netdev_start_xmit(skb, dev, txq, more);
2912 trace_net_dev_xmit(skb, rc, dev, len);
2913
2914 return rc;
2915}
2916
2917struct sk_buff *dev_hard_start_xmit(struct sk_buff *first, struct net_device *dev,
2918 struct netdev_queue *txq, int *ret)
2919{
2920 struct sk_buff *skb = first;
2921 int rc = NETDEV_TX_OK;
2922
2923 while (skb) {
2924 struct sk_buff *next = skb->next;
2925
2926 skb->next = NULL;
2927 rc = xmit_one(skb, dev, txq, next != NULL);
2928 if (unlikely(!dev_xmit_complete(rc))) {
2929 skb->next = next;
2930 goto out;
2931 }
2932
2933 skb = next;
2934 if (netif_xmit_stopped(txq) && skb) {
2935 rc = NETDEV_TX_BUSY;
2936 break;
2937 }
2938 }
2939
2940out:
2941 *ret = rc;
2942 return skb;
2943}
2944
2945static struct sk_buff *validate_xmit_vlan(struct sk_buff *skb,
2946 netdev_features_t features)
2947{
2948 if (skb_vlan_tag_present(skb) &&
2949 !vlan_hw_offload_capable(features, skb->vlan_proto))
2950 skb = __vlan_hwaccel_push_inside(skb);
2951 return skb;
2952}
2953
2954static struct sk_buff *validate_xmit_skb(struct sk_buff *skb, struct net_device *dev)
2955{
2956 netdev_features_t features;
2957
2958 features = netif_skb_features(skb);
2959 skb = validate_xmit_vlan(skb, features);
2960 if (unlikely(!skb))
2961 goto out_null;
2962
2963 if (netif_needs_gso(skb, features)) {
2964 struct sk_buff *segs;
2965
2966 segs = skb_gso_segment(skb, features);
2967 if (IS_ERR(segs)) {
2968 goto out_kfree_skb;
2969 } else if (segs) {
2970 consume_skb(skb);
2971 skb = segs;
2972 }
2973 } else {
2974 if (skb_needs_linearize(skb, features) &&
2975 __skb_linearize(skb))
2976 goto out_kfree_skb;
2977
2978 /* If packet is not checksummed and device does not
2979 * support checksumming for this protocol, complete
2980 * checksumming here.
2981 */
2982 if (skb->ip_summed == CHECKSUM_PARTIAL) {
2983 if (skb->encapsulation)
2984 skb_set_inner_transport_header(skb,
2985 skb_checksum_start_offset(skb));
2986 else
2987 skb_set_transport_header(skb,
2988 skb_checksum_start_offset(skb));
2989 if (!(features & NETIF_F_CSUM_MASK) &&
2990 skb_checksum_help(skb))
2991 goto out_kfree_skb;
2992 }
2993 }
2994
2995 return skb;
2996
2997out_kfree_skb:
2998 kfree_skb(skb);
2999out_null:
3000 atomic_long_inc(&dev->tx_dropped);
3001 return NULL;
3002}
3003
3004struct sk_buff *validate_xmit_skb_list(struct sk_buff *skb, struct net_device *dev)
3005{
3006 struct sk_buff *next, *head = NULL, *tail;
3007
3008 for (; skb != NULL; skb = next) {
3009 next = skb->next;
3010 skb->next = NULL;
3011
3012 /* in case skb wont be segmented, point to itself */
3013 skb->prev = skb;
3014
3015 skb = validate_xmit_skb(skb, dev);
3016 if (!skb)
3017 continue;
3018
3019 if (!head)
3020 head = skb;
3021 else
3022 tail->next = skb;
3023 /* If skb was segmented, skb->prev points to
3024 * the last segment. If not, it still contains skb.
3025 */
3026 tail = skb->prev;
3027 }
3028 return head;
3029}
3030EXPORT_SYMBOL_GPL(validate_xmit_skb_list);
3031
3032static void qdisc_pkt_len_init(struct sk_buff *skb)
3033{
3034 const struct skb_shared_info *shinfo = skb_shinfo(skb);
3035
3036 qdisc_skb_cb(skb)->pkt_len = skb->len;
3037
3038 /* To get more precise estimation of bytes sent on wire,
3039 * we add to pkt_len the headers size of all segments
3040 */
3041 if (shinfo->gso_size) {
3042 unsigned int hdr_len;
3043 u16 gso_segs = shinfo->gso_segs;
3044
3045 /* mac layer + network layer */
3046 hdr_len = skb_transport_header(skb) - skb_mac_header(skb);
3047
3048 /* + transport layer */
3049 if (likely(shinfo->gso_type & (SKB_GSO_TCPV4 | SKB_GSO_TCPV6)))
3050 hdr_len += tcp_hdrlen(skb);
3051 else
3052 hdr_len += sizeof(struct udphdr);
3053
3054 if (shinfo->gso_type & SKB_GSO_DODGY)
3055 gso_segs = DIV_ROUND_UP(skb->len - hdr_len,
3056 shinfo->gso_size);
3057
3058 qdisc_skb_cb(skb)->pkt_len += (gso_segs - 1) * hdr_len;
3059 }
3060}
3061
3062static inline int __dev_xmit_skb(struct sk_buff *skb, struct Qdisc *q,
3063 struct net_device *dev,
3064 struct netdev_queue *txq)
3065{
3066 spinlock_t *root_lock = qdisc_lock(q);
3067 struct sk_buff *to_free = NULL;
3068 bool contended;
3069 int rc;
3070
3071 qdisc_calculate_pkt_len(skb, q);
3072 /*
3073 * Heuristic to force contended enqueues to serialize on a
3074 * separate lock before trying to get qdisc main lock.
3075 * This permits qdisc->running owner to get the lock more
3076 * often and dequeue packets faster.
3077 */
3078 contended = qdisc_is_running(q);
3079 if (unlikely(contended))
3080 spin_lock(&q->busylock);
3081
3082 spin_lock(root_lock);
3083 if (unlikely(test_bit(__QDISC_STATE_DEACTIVATED, &q->state))) {
3084 __qdisc_drop(skb, &to_free);
3085 rc = NET_XMIT_DROP;
3086 } else if ((q->flags & TCQ_F_CAN_BYPASS) && !qdisc_qlen(q) &&
3087 qdisc_run_begin(q)) {
3088 /*
3089 * This is a work-conserving queue; there are no old skbs
3090 * waiting to be sent out; and the qdisc is not running -
3091 * xmit the skb directly.
3092 */
3093
3094 qdisc_bstats_update(q, skb);
3095
3096 if (sch_direct_xmit(skb, q, dev, txq, root_lock, true)) {
3097 if (unlikely(contended)) {
3098 spin_unlock(&q->busylock);
3099 contended = false;
3100 }
3101 __qdisc_run(q);
3102 } else
3103 qdisc_run_end(q);
3104
3105 rc = NET_XMIT_SUCCESS;
3106 } else {
3107 rc = q->enqueue(skb, q, &to_free) & NET_XMIT_MASK;
3108 if (qdisc_run_begin(q)) {
3109 if (unlikely(contended)) {
3110 spin_unlock(&q->busylock);
3111 contended = false;
3112 }
3113 __qdisc_run(q);
3114 }
3115 }
3116 spin_unlock(root_lock);
3117 if (unlikely(to_free))
3118 kfree_skb_list(to_free);
3119 if (unlikely(contended))
3120 spin_unlock(&q->busylock);
3121 return rc;
3122}
3123
3124#if IS_ENABLED(CONFIG_CGROUP_NET_PRIO)
3125static void skb_update_prio(struct sk_buff *skb)
3126{
3127 struct netprio_map *map = rcu_dereference_bh(skb->dev->priomap);
3128
3129 if (!skb->priority && skb->sk && map) {
3130 unsigned int prioidx =
3131 sock_cgroup_prioidx(&skb->sk->sk_cgrp_data);
3132
3133 if (prioidx < map->priomap_len)
3134 skb->priority = map->priomap[prioidx];
3135 }
3136}
3137#else
3138#define skb_update_prio(skb)
3139#endif
3140
3141DEFINE_PER_CPU(int, xmit_recursion);
3142EXPORT_SYMBOL(xmit_recursion);
3143
3144/**
3145 * dev_loopback_xmit - loop back @skb
3146 * @net: network namespace this loopback is happening in
3147 * @sk: sk needed to be a netfilter okfn
3148 * @skb: buffer to transmit
3149 */
3150int dev_loopback_xmit(struct net *net, struct sock *sk, struct sk_buff *skb)
3151{
3152 skb_reset_mac_header(skb);
3153 __skb_pull(skb, skb_network_offset(skb));
3154 skb->pkt_type = PACKET_LOOPBACK;
3155 skb->ip_summed = CHECKSUM_UNNECESSARY;
3156 WARN_ON(!skb_dst(skb));
3157 skb_dst_force(skb);
3158 netif_rx_ni(skb);
3159 return 0;
3160}
3161EXPORT_SYMBOL(dev_loopback_xmit);
3162
3163#ifdef CONFIG_NET_EGRESS
3164static struct sk_buff *
3165sch_handle_egress(struct sk_buff *skb, int *ret, struct net_device *dev)
3166{
3167 struct tcf_proto *cl = rcu_dereference_bh(dev->egress_cl_list);
3168 struct tcf_result cl_res;
3169
3170 if (!cl)
3171 return skb;
3172
3173 /* qdisc_skb_cb(skb)->pkt_len was already set by the caller. */
3174 qdisc_bstats_cpu_update(cl->q, skb);
3175
3176 switch (tc_classify(skb, cl, &cl_res, false)) {
3177 case TC_ACT_OK:
3178 case TC_ACT_RECLASSIFY:
3179 skb->tc_index = TC_H_MIN(cl_res.classid);
3180 break;
3181 case TC_ACT_SHOT:
3182 qdisc_qstats_cpu_drop(cl->q);
3183 *ret = NET_XMIT_DROP;
3184 kfree_skb(skb);
3185 return NULL;
3186 case TC_ACT_STOLEN:
3187 case TC_ACT_QUEUED:
3188 *ret = NET_XMIT_SUCCESS;
3189 consume_skb(skb);
3190 return NULL;
3191 case TC_ACT_REDIRECT:
3192 /* No need to push/pop skb's mac_header here on egress! */
3193 skb_do_redirect(skb);
3194 *ret = NET_XMIT_SUCCESS;
3195 return NULL;
3196 default:
3197 break;
3198 }
3199
3200 return skb;
3201}
3202#endif /* CONFIG_NET_EGRESS */
3203
3204static inline int get_xps_queue(struct net_device *dev, struct sk_buff *skb)
3205{
3206#ifdef CONFIG_XPS
3207 struct xps_dev_maps *dev_maps;
3208 struct xps_map *map;
3209 int queue_index = -1;
3210
3211 rcu_read_lock();
3212 dev_maps = rcu_dereference(dev->xps_maps);
3213 if (dev_maps) {
3214 unsigned int tci = skb->sender_cpu - 1;
3215
3216 if (dev->num_tc) {
3217 tci *= dev->num_tc;
3218 tci += netdev_get_prio_tc_map(dev, skb->priority);
3219 }
3220
3221 map = rcu_dereference(dev_maps->cpu_map[tci]);
3222 if (map) {
3223 if (map->len == 1)
3224 queue_index = map->queues[0];
3225 else
3226 queue_index = map->queues[reciprocal_scale(skb_get_hash(skb),
3227 map->len)];
3228 if (unlikely(queue_index >= dev->real_num_tx_queues))
3229 queue_index = -1;
3230 }
3231 }
3232 rcu_read_unlock();
3233
3234 return queue_index;
3235#else
3236 return -1;
3237#endif
3238}
3239
3240static u16 __netdev_pick_tx(struct net_device *dev, struct sk_buff *skb)
3241{
3242 struct sock *sk = skb->sk;
3243 int queue_index = sk_tx_queue_get(sk);
3244
3245 if (queue_index < 0 || skb->ooo_okay ||
3246 queue_index >= dev->real_num_tx_queues) {
3247 int new_index = get_xps_queue(dev, skb);
3248
3249 if (new_index < 0)
3250 new_index = skb_tx_hash(dev, skb);
3251
3252 if (queue_index != new_index && sk &&
3253 sk_fullsock(sk) &&
3254 rcu_access_pointer(sk->sk_dst_cache))
3255 sk_tx_queue_set(sk, new_index);
3256
3257 queue_index = new_index;
3258 }
3259
3260 return queue_index;
3261}
3262
3263struct netdev_queue *netdev_pick_tx(struct net_device *dev,
3264 struct sk_buff *skb,
3265 void *accel_priv)
3266{
3267 int queue_index = 0;
3268
3269#ifdef CONFIG_XPS
3270 u32 sender_cpu = skb->sender_cpu - 1;
3271
3272 if (sender_cpu >= (u32)NR_CPUS)
3273 skb->sender_cpu = raw_smp_processor_id() + 1;
3274#endif
3275
3276 if (dev->real_num_tx_queues != 1) {
3277 const struct net_device_ops *ops = dev->netdev_ops;
3278
3279 if (ops->ndo_select_queue)
3280 queue_index = ops->ndo_select_queue(dev, skb, accel_priv,
3281 __netdev_pick_tx);
3282 else
3283 queue_index = __netdev_pick_tx(dev, skb);
3284
3285 if (!accel_priv)
3286 queue_index = netdev_cap_txqueue(dev, queue_index);
3287 }
3288
3289 skb_set_queue_mapping(skb, queue_index);
3290 return netdev_get_tx_queue(dev, queue_index);
3291}
3292
3293/**
3294 * __dev_queue_xmit - transmit a buffer
3295 * @skb: buffer to transmit
3296 * @accel_priv: private data used for L2 forwarding offload
3297 *
3298 * Queue a buffer for transmission to a network device. The caller must
3299 * have set the device and priority and built the buffer before calling
3300 * this function. The function can be called from an interrupt.
3301 *
3302 * A negative errno code is returned on a failure. A success does not
3303 * guarantee the frame will be transmitted as it may be dropped due
3304 * to congestion or traffic shaping.
3305 *
3306 * -----------------------------------------------------------------------------------
3307 * I notice this method can also return errors from the queue disciplines,
3308 * including NET_XMIT_DROP, which is a positive value. So, errors can also
3309 * be positive.
3310 *
3311 * Regardless of the return value, the skb is consumed, so it is currently
3312 * difficult to retry a send to this method. (You can bump the ref count
3313 * before sending to hold a reference for retry if you are careful.)
3314 *
3315 * When calling this method, interrupts MUST be enabled. This is because
3316 * the BH enable code must have IRQs enabled so that it will not deadlock.
3317 * --BLG
3318 */
3319static int __dev_queue_xmit(struct sk_buff *skb, void *accel_priv)
3320{
3321 struct net_device *dev = skb->dev;
3322 struct netdev_queue *txq;
3323 struct Qdisc *q;
3324 int rc = -ENOMEM;
3325
3326 skb_reset_mac_header(skb);
3327
3328 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_SCHED_TSTAMP))
3329 __skb_tstamp_tx(skb, NULL, skb->sk, SCM_TSTAMP_SCHED);
3330
3331 /* Disable soft irqs for various locks below. Also
3332 * stops preemption for RCU.
3333 */
3334 rcu_read_lock_bh();
3335
3336 skb_update_prio(skb);
3337
3338 qdisc_pkt_len_init(skb);
3339#ifdef CONFIG_NET_CLS_ACT
3340 skb->tc_at_ingress = 0;
3341# ifdef CONFIG_NET_EGRESS
3342 if (static_key_false(&egress_needed)) {
3343 skb = sch_handle_egress(skb, &rc, dev);
3344 if (!skb)
3345 goto out;
3346 }
3347# endif
3348#endif
3349 /* If device/qdisc don't need skb->dst, release it right now while
3350 * its hot in this cpu cache.
3351 */
3352 if (dev->priv_flags & IFF_XMIT_DST_RELEASE)
3353 skb_dst_drop(skb);
3354 else
3355 skb_dst_force(skb);
3356
3357 txq = netdev_pick_tx(dev, skb, accel_priv);
3358 q = rcu_dereference_bh(txq->qdisc);
3359
3360 trace_net_dev_queue(skb);
3361 if (q->enqueue) {
3362 rc = __dev_xmit_skb(skb, q, dev, txq);
3363 goto out;
3364 }
3365
3366 /* The device has no queue. Common case for software devices:
3367 * loopback, all the sorts of tunnels...
3368
3369 * Really, it is unlikely that netif_tx_lock protection is necessary
3370 * here. (f.e. loopback and IP tunnels are clean ignoring statistics
3371 * counters.)
3372 * However, it is possible, that they rely on protection
3373 * made by us here.
3374
3375 * Check this and shot the lock. It is not prone from deadlocks.
3376 *Either shot noqueue qdisc, it is even simpler 8)
3377 */
3378 if (dev->flags & IFF_UP) {
3379 int cpu = smp_processor_id(); /* ok because BHs are off */
3380
3381 if (txq->xmit_lock_owner != cpu) {
3382 if (unlikely(__this_cpu_read(xmit_recursion) >
3383 XMIT_RECURSION_LIMIT))
3384 goto recursion_alert;
3385
3386 skb = validate_xmit_skb(skb, dev);
3387 if (!skb)
3388 goto out;
3389
3390 HARD_TX_LOCK(dev, txq, cpu);
3391
3392 if (!netif_xmit_stopped(txq)) {
3393 __this_cpu_inc(xmit_recursion);
3394 skb = dev_hard_start_xmit(skb, dev, txq, &rc);
3395 __this_cpu_dec(xmit_recursion);
3396 if (dev_xmit_complete(rc)) {
3397 HARD_TX_UNLOCK(dev, txq);
3398 goto out;
3399 }
3400 }
3401 HARD_TX_UNLOCK(dev, txq);
3402 net_crit_ratelimited("Virtual device %s asks to queue packet!\n",
3403 dev->name);
3404 } else {
3405 /* Recursion is detected! It is possible,
3406 * unfortunately
3407 */
3408recursion_alert:
3409 net_crit_ratelimited("Dead loop on virtual device %s, fix it urgently!\n",
3410 dev->name);
3411 }
3412 }
3413
3414 rc = -ENETDOWN;
3415 rcu_read_unlock_bh();
3416
3417 atomic_long_inc(&dev->tx_dropped);
3418 kfree_skb_list(skb);
3419 return rc;
3420out:
3421 rcu_read_unlock_bh();
3422 return rc;
3423}
3424
3425int dev_queue_xmit(struct sk_buff *skb)
3426{
3427 return __dev_queue_xmit(skb, NULL);
3428}
3429EXPORT_SYMBOL(dev_queue_xmit);
3430
3431int dev_queue_xmit_accel(struct sk_buff *skb, void *accel_priv)
3432{
3433 return __dev_queue_xmit(skb, accel_priv);
3434}
3435EXPORT_SYMBOL(dev_queue_xmit_accel);
3436
3437
3438/*************************************************************************
3439 * Receiver routines
3440 *************************************************************************/
3441
3442int netdev_max_backlog __read_mostly = 1000;
3443EXPORT_SYMBOL(netdev_max_backlog);
3444
3445int netdev_tstamp_prequeue __read_mostly = 1;
3446int netdev_budget __read_mostly = 300;
3447int weight_p __read_mostly = 64; /* old backlog weight */
3448int dev_weight_rx_bias __read_mostly = 1; /* bias for backlog weight */
3449int dev_weight_tx_bias __read_mostly = 1; /* bias for output_queue quota */
3450int dev_rx_weight __read_mostly = 64;
3451int dev_tx_weight __read_mostly = 64;
3452
3453/* Called with irq disabled */
3454static inline void ____napi_schedule(struct softnet_data *sd,
3455 struct napi_struct *napi)
3456{
3457 list_add_tail(&napi->poll_list, &sd->poll_list);
3458 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3459}
3460
3461#ifdef CONFIG_RPS
3462
3463/* One global table that all flow-based protocols share. */
3464struct rps_sock_flow_table __rcu *rps_sock_flow_table __read_mostly;
3465EXPORT_SYMBOL(rps_sock_flow_table);
3466u32 rps_cpu_mask __read_mostly;
3467EXPORT_SYMBOL(rps_cpu_mask);
3468
3469struct static_key rps_needed __read_mostly;
3470EXPORT_SYMBOL(rps_needed);
3471struct static_key rfs_needed __read_mostly;
3472EXPORT_SYMBOL(rfs_needed);
3473
3474static struct rps_dev_flow *
3475set_rps_cpu(struct net_device *dev, struct sk_buff *skb,
3476 struct rps_dev_flow *rflow, u16 next_cpu)
3477{
3478 if (next_cpu < nr_cpu_ids) {
3479#ifdef CONFIG_RFS_ACCEL
3480 struct netdev_rx_queue *rxqueue;
3481 struct rps_dev_flow_table *flow_table;
3482 struct rps_dev_flow *old_rflow;
3483 u32 flow_id;
3484 u16 rxq_index;
3485 int rc;
3486
3487 /* Should we steer this flow to a different hardware queue? */
3488 if (!skb_rx_queue_recorded(skb) || !dev->rx_cpu_rmap ||
3489 !(dev->features & NETIF_F_NTUPLE))
3490 goto out;
3491 rxq_index = cpu_rmap_lookup_index(dev->rx_cpu_rmap, next_cpu);
3492 if (rxq_index == skb_get_rx_queue(skb))
3493 goto out;
3494
3495 rxqueue = dev->_rx + rxq_index;
3496 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3497 if (!flow_table)
3498 goto out;
3499 flow_id = skb_get_hash(skb) & flow_table->mask;
3500 rc = dev->netdev_ops->ndo_rx_flow_steer(dev, skb,
3501 rxq_index, flow_id);
3502 if (rc < 0)
3503 goto out;
3504 old_rflow = rflow;
3505 rflow = &flow_table->flows[flow_id];
3506 rflow->filter = rc;
3507 if (old_rflow->filter == rflow->filter)
3508 old_rflow->filter = RPS_NO_FILTER;
3509 out:
3510#endif
3511 rflow->last_qtail =
3512 per_cpu(softnet_data, next_cpu).input_queue_head;
3513 }
3514
3515 rflow->cpu = next_cpu;
3516 return rflow;
3517}
3518
3519/*
3520 * get_rps_cpu is called from netif_receive_skb and returns the target
3521 * CPU from the RPS map of the receiving queue for a given skb.
3522 * rcu_read_lock must be held on entry.
3523 */
3524static int get_rps_cpu(struct net_device *dev, struct sk_buff *skb,
3525 struct rps_dev_flow **rflowp)
3526{
3527 const struct rps_sock_flow_table *sock_flow_table;
3528 struct netdev_rx_queue *rxqueue = dev->_rx;
3529 struct rps_dev_flow_table *flow_table;
3530 struct rps_map *map;
3531 int cpu = -1;
3532 u32 tcpu;
3533 u32 hash;
3534
3535 if (skb_rx_queue_recorded(skb)) {
3536 u16 index = skb_get_rx_queue(skb);
3537
3538 if (unlikely(index >= dev->real_num_rx_queues)) {
3539 WARN_ONCE(dev->real_num_rx_queues > 1,
3540 "%s received packet on queue %u, but number "
3541 "of RX queues is %u\n",
3542 dev->name, index, dev->real_num_rx_queues);
3543 goto done;
3544 }
3545 rxqueue += index;
3546 }
3547
3548 /* Avoid computing hash if RFS/RPS is not active for this rxqueue */
3549
3550 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3551 map = rcu_dereference(rxqueue->rps_map);
3552 if (!flow_table && !map)
3553 goto done;
3554
3555 skb_reset_network_header(skb);
3556 hash = skb_get_hash(skb);
3557 if (!hash)
3558 goto done;
3559
3560 sock_flow_table = rcu_dereference(rps_sock_flow_table);
3561 if (flow_table && sock_flow_table) {
3562 struct rps_dev_flow *rflow;
3563 u32 next_cpu;
3564 u32 ident;
3565
3566 /* First check into global flow table if there is a match */
3567 ident = sock_flow_table->ents[hash & sock_flow_table->mask];
3568 if ((ident ^ hash) & ~rps_cpu_mask)
3569 goto try_rps;
3570
3571 next_cpu = ident & rps_cpu_mask;
3572
3573 /* OK, now we know there is a match,
3574 * we can look at the local (per receive queue) flow table
3575 */
3576 rflow = &flow_table->flows[hash & flow_table->mask];
3577 tcpu = rflow->cpu;
3578
3579 /*
3580 * If the desired CPU (where last recvmsg was done) is
3581 * different from current CPU (one in the rx-queue flow
3582 * table entry), switch if one of the following holds:
3583 * - Current CPU is unset (>= nr_cpu_ids).
3584 * - Current CPU is offline.
3585 * - The current CPU's queue tail has advanced beyond the
3586 * last packet that was enqueued using this table entry.
3587 * This guarantees that all previous packets for the flow
3588 * have been dequeued, thus preserving in order delivery.
3589 */
3590 if (unlikely(tcpu != next_cpu) &&
3591 (tcpu >= nr_cpu_ids || !cpu_online(tcpu) ||
3592 ((int)(per_cpu(softnet_data, tcpu).input_queue_head -
3593 rflow->last_qtail)) >= 0)) {
3594 tcpu = next_cpu;
3595 rflow = set_rps_cpu(dev, skb, rflow, next_cpu);
3596 }
3597
3598 if (tcpu < nr_cpu_ids && cpu_online(tcpu)) {
3599 *rflowp = rflow;
3600 cpu = tcpu;
3601 goto done;
3602 }
3603 }
3604
3605try_rps:
3606
3607 if (map) {
3608 tcpu = map->cpus[reciprocal_scale(hash, map->len)];
3609 if (cpu_online(tcpu)) {
3610 cpu = tcpu;
3611 goto done;
3612 }
3613 }
3614
3615done:
3616 return cpu;
3617}
3618
3619#ifdef CONFIG_RFS_ACCEL
3620
3621/**
3622 * rps_may_expire_flow - check whether an RFS hardware filter may be removed
3623 * @dev: Device on which the filter was set
3624 * @rxq_index: RX queue index
3625 * @flow_id: Flow ID passed to ndo_rx_flow_steer()
3626 * @filter_id: Filter ID returned by ndo_rx_flow_steer()
3627 *
3628 * Drivers that implement ndo_rx_flow_steer() should periodically call
3629 * this function for each installed filter and remove the filters for
3630 * which it returns %true.
3631 */
3632bool rps_may_expire_flow(struct net_device *dev, u16 rxq_index,
3633 u32 flow_id, u16 filter_id)
3634{
3635 struct netdev_rx_queue *rxqueue = dev->_rx + rxq_index;
3636 struct rps_dev_flow_table *flow_table;
3637 struct rps_dev_flow *rflow;
3638 bool expire = true;
3639 unsigned int cpu;
3640
3641 rcu_read_lock();
3642 flow_table = rcu_dereference(rxqueue->rps_flow_table);
3643 if (flow_table && flow_id <= flow_table->mask) {
3644 rflow = &flow_table->flows[flow_id];
3645 cpu = ACCESS_ONCE(rflow->cpu);
3646 if (rflow->filter == filter_id && cpu < nr_cpu_ids &&
3647 ((int)(per_cpu(softnet_data, cpu).input_queue_head -
3648 rflow->last_qtail) <
3649 (int)(10 * flow_table->mask)))
3650 expire = false;
3651 }
3652 rcu_read_unlock();
3653 return expire;
3654}
3655EXPORT_SYMBOL(rps_may_expire_flow);
3656
3657#endif /* CONFIG_RFS_ACCEL */
3658
3659/* Called from hardirq (IPI) context */
3660static void rps_trigger_softirq(void *data)
3661{
3662 struct softnet_data *sd = data;
3663
3664 ____napi_schedule(sd, &sd->backlog);
3665 sd->received_rps++;
3666}
3667
3668#endif /* CONFIG_RPS */
3669
3670/*
3671 * Check if this softnet_data structure is another cpu one
3672 * If yes, queue it to our IPI list and return 1
3673 * If no, return 0
3674 */
3675static int rps_ipi_queued(struct softnet_data *sd)
3676{
3677#ifdef CONFIG_RPS
3678 struct softnet_data *mysd = this_cpu_ptr(&softnet_data);
3679
3680 if (sd != mysd) {
3681 sd->rps_ipi_next = mysd->rps_ipi_list;
3682 mysd->rps_ipi_list = sd;
3683
3684 __raise_softirq_irqoff(NET_RX_SOFTIRQ);
3685 return 1;
3686 }
3687#endif /* CONFIG_RPS */
3688 return 0;
3689}
3690
3691#ifdef CONFIG_NET_FLOW_LIMIT
3692int netdev_flow_limit_table_len __read_mostly = (1 << 12);
3693#endif
3694
3695static bool skb_flow_limit(struct sk_buff *skb, unsigned int qlen)
3696{
3697#ifdef CONFIG_NET_FLOW_LIMIT
3698 struct sd_flow_limit *fl;
3699 struct softnet_data *sd;
3700 unsigned int old_flow, new_flow;
3701
3702 if (qlen < (netdev_max_backlog >> 1))
3703 return false;
3704
3705 sd = this_cpu_ptr(&softnet_data);
3706
3707 rcu_read_lock();
3708 fl = rcu_dereference(sd->flow_limit);
3709 if (fl) {
3710 new_flow = skb_get_hash(skb) & (fl->num_buckets - 1);
3711 old_flow = fl->history[fl->history_head];
3712 fl->history[fl->history_head] = new_flow;
3713
3714 fl->history_head++;
3715 fl->history_head &= FLOW_LIMIT_HISTORY - 1;
3716
3717 if (likely(fl->buckets[old_flow]))
3718 fl->buckets[old_flow]--;
3719
3720 if (++fl->buckets[new_flow] > (FLOW_LIMIT_HISTORY >> 1)) {
3721 fl->count++;
3722 rcu_read_unlock();
3723 return true;
3724 }
3725 }
3726 rcu_read_unlock();
3727#endif
3728 return false;
3729}
3730
3731/*
3732 * enqueue_to_backlog is called to queue an skb to a per CPU backlog
3733 * queue (may be a remote CPU queue).
3734 */
3735static int enqueue_to_backlog(struct sk_buff *skb, int cpu,
3736 unsigned int *qtail)
3737{
3738 struct softnet_data *sd;
3739 unsigned long flags;
3740 unsigned int qlen;
3741
3742 sd = &per_cpu(softnet_data, cpu);
3743
3744 local_irq_save(flags);
3745
3746 rps_lock(sd);
3747 if (!netif_running(skb->dev))
3748 goto drop;
3749 qlen = skb_queue_len(&sd->input_pkt_queue);
3750 if (qlen <= netdev_max_backlog && !skb_flow_limit(skb, qlen)) {
3751 if (qlen) {
3752enqueue:
3753 __skb_queue_tail(&sd->input_pkt_queue, skb);
3754 input_queue_tail_incr_save(sd, qtail);
3755 rps_unlock(sd);
3756 local_irq_restore(flags);
3757 return NET_RX_SUCCESS;
3758 }
3759
3760 /* Schedule NAPI for backlog device
3761 * We can use non atomic operation since we own the queue lock
3762 */
3763 if (!__test_and_set_bit(NAPI_STATE_SCHED, &sd->backlog.state)) {
3764 if (!rps_ipi_queued(sd))
3765 ____napi_schedule(sd, &sd->backlog);
3766 }
3767 goto enqueue;
3768 }
3769
3770drop:
3771 sd->dropped++;
3772 rps_unlock(sd);
3773
3774 local_irq_restore(flags);
3775
3776 atomic_long_inc(&skb->dev->rx_dropped);
3777 kfree_skb(skb);
3778 return NET_RX_DROP;
3779}
3780
3781static int netif_rx_internal(struct sk_buff *skb)
3782{
3783 int ret;
3784
3785 net_timestamp_check(netdev_tstamp_prequeue, skb);
3786
3787 trace_netif_rx(skb);
3788#ifdef CONFIG_RPS
3789 if (static_key_false(&rps_needed)) {
3790 struct rps_dev_flow voidflow, *rflow = &voidflow;
3791 int cpu;
3792
3793 preempt_disable();
3794 rcu_read_lock();
3795
3796 cpu = get_rps_cpu(skb->dev, skb, &rflow);
3797 if (cpu < 0)
3798 cpu = smp_processor_id();
3799
3800 ret = enqueue_to_backlog(skb, cpu, &rflow->last_qtail);
3801
3802 rcu_read_unlock();
3803 preempt_enable();
3804 } else
3805#endif
3806 {
3807 unsigned int qtail;
3808
3809 ret = enqueue_to_backlog(skb, get_cpu(), &qtail);
3810 put_cpu();
3811 }
3812 return ret;
3813}
3814
3815/**
3816 * netif_rx - post buffer to the network code
3817 * @skb: buffer to post
3818 *
3819 * This function receives a packet from a device driver and queues it for
3820 * the upper (protocol) levels to process. It always succeeds. The buffer
3821 * may be dropped during processing for congestion control or by the
3822 * protocol layers.
3823 *
3824 * return values:
3825 * NET_RX_SUCCESS (no congestion)
3826 * NET_RX_DROP (packet was dropped)
3827 *
3828 */
3829
3830int netif_rx(struct sk_buff *skb)
3831{
3832 trace_netif_rx_entry(skb);
3833
3834 return netif_rx_internal(skb);
3835}
3836EXPORT_SYMBOL(netif_rx);
3837
3838int netif_rx_ni(struct sk_buff *skb)
3839{
3840 int err;
3841
3842 trace_netif_rx_ni_entry(skb);
3843
3844 preempt_disable();
3845 err = netif_rx_internal(skb);
3846 if (local_softirq_pending())
3847 do_softirq();
3848 preempt_enable();
3849
3850 return err;
3851}
3852EXPORT_SYMBOL(netif_rx_ni);
3853
3854static __latent_entropy void net_tx_action(struct softirq_action *h)
3855{
3856 struct softnet_data *sd = this_cpu_ptr(&softnet_data);
3857
3858 if (sd->completion_queue) {
3859 struct sk_buff *clist;
3860
3861 local_irq_disable();
3862 clist = sd->completion_queue;
3863 sd->completion_queue = NULL;
3864 local_irq_enable();
3865
3866 while (clist) {
3867 struct sk_buff *skb = clist;
3868
3869 clist = clist->next;
3870
3871 WARN_ON(atomic_read(&skb->users));
3872 if (likely(get_kfree_skb_cb(skb)->reason == SKB_REASON_CONSUMED))
3873 trace_consume_skb(skb);
3874 else
3875 trace_kfree_skb(skb, net_tx_action);
3876
3877 if (skb->fclone != SKB_FCLONE_UNAVAILABLE)
3878 __kfree_skb(skb);
3879 else
3880 __kfree_skb_defer(skb);
3881 }
3882
3883 __kfree_skb_flush();
3884 }
3885
3886 if (sd->output_queue) {
3887 struct Qdisc *head;
3888
3889 local_irq_disable();
3890 head = sd->output_queue;
3891 sd->output_queue = NULL;
3892 sd->output_queue_tailp = &sd->output_queue;
3893 local_irq_enable();
3894
3895 while (head) {
3896 struct Qdisc *q = head;
3897 spinlock_t *root_lock;
3898
3899 head = head->next_sched;
3900
3901 root_lock = qdisc_lock(q);
3902 spin_lock(root_lock);
3903 /* We need to make sure head->next_sched is read
3904 * before clearing __QDISC_STATE_SCHED
3905 */
3906 smp_mb__before_atomic();
3907 clear_bit(__QDISC_STATE_SCHED, &q->state);
3908 qdisc_run(q);
3909 spin_unlock(root_lock);
3910 }
3911 }
3912}
3913
3914#if IS_ENABLED(CONFIG_BRIDGE) && IS_ENABLED(CONFIG_ATM_LANE)
3915/* This hook is defined here for ATM LANE */
3916int (*br_fdb_test_addr_hook)(struct net_device *dev,
3917 unsigned char *addr) __read_mostly;
3918EXPORT_SYMBOL_GPL(br_fdb_test_addr_hook);
3919#endif
3920
3921static inline struct sk_buff *
3922sch_handle_ingress(struct sk_buff *skb, struct packet_type **pt_prev, int *ret,
3923 struct net_device *orig_dev)
3924{
3925#ifdef CONFIG_NET_CLS_ACT
3926 struct tcf_proto *cl = rcu_dereference_bh(skb->dev->ingress_cl_list);
3927 struct tcf_result cl_res;
3928
3929 /* If there's at least one ingress present somewhere (so
3930 * we get here via enabled static key), remaining devices
3931 * that are not configured with an ingress qdisc will bail
3932 * out here.
3933 */
3934 if (!cl)
3935 return skb;
3936 if (*pt_prev) {
3937 *ret = deliver_skb(skb, *pt_prev, orig_dev);
3938 *pt_prev = NULL;
3939 }
3940
3941 qdisc_skb_cb(skb)->pkt_len = skb->len;
3942 skb->tc_at_ingress = 1;
3943 qdisc_bstats_cpu_update(cl->q, skb);
3944
3945 switch (tc_classify(skb, cl, &cl_res, false)) {
3946 case TC_ACT_OK:
3947 case TC_ACT_RECLASSIFY:
3948 skb->tc_index = TC_H_MIN(cl_res.classid);
3949 break;
3950 case TC_ACT_SHOT:
3951 qdisc_qstats_cpu_drop(cl->q);
3952 kfree_skb(skb);
3953 return NULL;
3954 case TC_ACT_STOLEN:
3955 case TC_ACT_QUEUED:
3956 consume_skb(skb);
3957 return NULL;
3958 case TC_ACT_REDIRECT:
3959 /* skb_mac_header check was done by cls/act_bpf, so
3960 * we can safely push the L2 header back before
3961 * redirecting to another netdev
3962 */
3963 __skb_push(skb, skb->mac_len);
3964 skb_do_redirect(skb);
3965 return NULL;
3966 default:
3967 break;
3968 }
3969#endif /* CONFIG_NET_CLS_ACT */
3970 return skb;
3971}
3972
3973/**
3974 * netdev_is_rx_handler_busy - check if receive handler is registered
3975 * @dev: device to check
3976 *
3977 * Check if a receive handler is already registered for a given device.
3978 * Return true if there one.
3979 *
3980 * The caller must hold the rtnl_mutex.
3981 */
3982bool netdev_is_rx_handler_busy(struct net_device *dev)
3983{
3984 ASSERT_RTNL();
3985 return dev && rtnl_dereference(dev->rx_handler);
3986}
3987EXPORT_SYMBOL_GPL(netdev_is_rx_handler_busy);
3988
3989/**
3990 * netdev_rx_handler_register - register receive handler
3991 * @dev: device to register a handler for
3992 * @rx_handler: receive handler to register
3993 * @rx_handler_data: data pointer that is used by rx handler
3994 *
3995 * Register a receive handler for a device. This handler will then be
3996 * called from __netif_receive_skb. A negative errno code is returned
3997 * on a failure.
3998 *
3999 * The caller must hold the rtnl_mutex.
4000 *
4001 * For a general description of rx_handler, see enum rx_handler_result.
4002 */
4003int netdev_rx_handler_register(struct net_device *dev,
4004 rx_handler_func_t *rx_handler,
4005 void *rx_handler_data)
4006{
4007 if (netdev_is_rx_handler_busy(dev))
4008 return -EBUSY;
4009
4010 /* Note: rx_handler_data must be set before rx_handler */
4011