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