LCOV - code coverage report
Current view: top level - net/core - dev.c (source / functions) Hit Total Coverage
Test: gcov_data_raspi2_real_modules_combined.info Lines: 836 3293 25.4 %
Date: 2020-09-30 20:25:40 Functions: 97 328 29.6 %
Branches: 412 2570 16.0 %

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

Generated by: LCOV version 1.14