LCOV - code coverage report
Current view: top level - net/core - link_watch.c (source / functions) Hit Total Coverage
Test: landlock.info Lines: 74 112 66.1 %
Date: 2021-04-22 12:43:58 Functions: 10 12 83.3 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-or-later
       2             : /*
       3             :  * Linux network device link state notification
       4             :  *
       5             :  * Author:
       6             :  *     Stefan Rompf <sux@loplof.de>
       7             :  */
       8             : 
       9             : #include <linux/module.h>
      10             : #include <linux/netdevice.h>
      11             : #include <linux/if.h>
      12             : #include <net/sock.h>
      13             : #include <net/pkt_sched.h>
      14             : #include <linux/rtnetlink.h>
      15             : #include <linux/jiffies.h>
      16             : #include <linux/spinlock.h>
      17             : #include <linux/workqueue.h>
      18             : #include <linux/bitops.h>
      19             : #include <linux/types.h>
      20             : 
      21             : 
      22             : enum lw_bits {
      23             :         LW_URGENT = 0,
      24             : };
      25             : 
      26             : static unsigned long linkwatch_flags;
      27             : static unsigned long linkwatch_nextevent;
      28             : 
      29             : static void linkwatch_event(struct work_struct *dummy);
      30             : static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
      31             : 
      32             : static LIST_HEAD(lweventlist);
      33             : static DEFINE_SPINLOCK(lweventlist_lock);
      34             : 
      35           2 : static unsigned char default_operstate(const struct net_device *dev)
      36             : {
      37           2 :         if (netif_testing(dev))
      38             :                 return IF_OPER_TESTING;
      39             : 
      40           2 :         if (!netif_carrier_ok(dev))
      41           1 :                 return (dev->ifindex != dev_get_iflink(dev) ?
      42           1 :                         IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
      43             : 
      44           1 :         if (netif_dormant(dev))
      45           0 :                 return IF_OPER_DORMANT;
      46             : 
      47             :         return IF_OPER_UP;
      48             : }
      49             : 
      50             : 
      51           2 : static void rfc2863_policy(struct net_device *dev)
      52             : {
      53           2 :         unsigned char operstate = default_operstate(dev);
      54             : 
      55           2 :         if (operstate == dev->operstate)
      56             :                 return;
      57             : 
      58           2 :         write_lock_bh(&dev_base_lock);
      59             : 
      60           2 :         switch(dev->link_mode) {
      61           0 :         case IF_LINK_MODE_TESTING:
      62           0 :                 if (operstate == IF_OPER_UP)
      63           0 :                         operstate = IF_OPER_TESTING;
      64             :                 break;
      65             : 
      66           0 :         case IF_LINK_MODE_DORMANT:
      67           0 :                 if (operstate == IF_OPER_UP)
      68           0 :                         operstate = IF_OPER_DORMANT;
      69             :                 break;
      70             :         case IF_LINK_MODE_DEFAULT:
      71             :         default:
      72             :                 break;
      73             :         }
      74             : 
      75           2 :         dev->operstate = operstate;
      76             : 
      77           2 :         write_unlock_bh(&dev_base_lock);
      78             : }
      79             : 
      80             : 
      81           2 : void linkwatch_init_dev(struct net_device *dev)
      82             : {
      83             :         /* Handle pre-registration link state changes */
      84           4 :         if (!netif_carrier_ok(dev) || netif_dormant(dev) ||
      85           2 :             netif_testing(dev))
      86           0 :                 rfc2863_policy(dev);
      87           2 : }
      88             : 
      89             : 
      90           2 : static bool linkwatch_urgent_event(struct net_device *dev)
      91             : {
      92           2 :         if (!netif_running(dev))
      93             :                 return false;
      94             : 
      95           0 :         if (dev->ifindex != dev_get_iflink(dev))
      96             :                 return true;
      97             : 
      98           0 :         if (netif_is_lag_port(dev) || netif_is_lag_master(dev))
      99             :                 return true;
     100             : 
     101           0 :         return netif_carrier_ok(dev) && qdisc_tx_changing(dev);
     102             : }
     103             : 
     104             : 
     105           2 : static void linkwatch_add_event(struct net_device *dev)
     106             : {
     107           2 :         unsigned long flags;
     108             : 
     109           2 :         spin_lock_irqsave(&lweventlist_lock, flags);
     110           2 :         if (list_empty(&dev->link_watch_list)) {
     111           2 :                 list_add_tail(&dev->link_watch_list, &lweventlist);
     112           2 :                 dev_hold(dev);
     113             :         }
     114           2 :         spin_unlock_irqrestore(&lweventlist_lock, flags);
     115           2 : }
     116             : 
     117             : 
     118           2 : static void linkwatch_schedule_work(int urgent)
     119             : {
     120           2 :         unsigned long delay = linkwatch_nextevent - jiffies;
     121             : 
     122           2 :         if (test_bit(LW_URGENT, &linkwatch_flags))
     123             :                 return;
     124             : 
     125             :         /* Minimise down-time: drop delay for up event. */
     126           2 :         if (urgent) {
     127           0 :                 if (test_and_set_bit(LW_URGENT, &linkwatch_flags))
     128             :                         return;
     129             :                 delay = 0;
     130             :         }
     131             : 
     132             :         /* If we wrap around we'll delay it by at most HZ. */
     133           2 :         if (delay > HZ)
     134           1 :                 delay = 0;
     135             : 
     136             :         /*
     137             :          * If urgent, schedule immediate execution; otherwise, don't
     138             :          * override the existing timer.
     139             :          */
     140           2 :         if (test_bit(LW_URGENT, &linkwatch_flags))
     141           0 :                 mod_delayed_work(system_wq, &linkwatch_work, 0);
     142             :         else
     143           2 :                 schedule_delayed_work(&linkwatch_work, delay);
     144             : }
     145             : 
     146             : 
     147           2 : static void linkwatch_do_dev(struct net_device *dev)
     148             : {
     149             :         /*
     150             :          * Make sure the above read is complete since it can be
     151             :          * rewritten as soon as we clear the bit below.
     152             :          */
     153           2 :         smp_mb__before_atomic();
     154             : 
     155             :         /* We are about to handle this device,
     156             :          * so new events can be accepted
     157             :          */
     158           2 :         clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
     159             : 
     160           2 :         rfc2863_policy(dev);
     161           2 :         if (dev->flags & IFF_UP && netif_device_present(dev)) {
     162           0 :                 if (netif_carrier_ok(dev))
     163           0 :                         dev_activate(dev);
     164             :                 else
     165           0 :                         dev_deactivate(dev);
     166             : 
     167           0 :                 netdev_state_change(dev);
     168             :         }
     169           2 :         dev_put(dev);
     170           2 : }
     171             : 
     172           2 : static void __linkwatch_run_queue(int urgent_only)
     173             : {
     174             : #define MAX_DO_DEV_PER_LOOP     100
     175             : 
     176           2 :         int do_dev = MAX_DO_DEV_PER_LOOP;
     177           2 :         struct net_device *dev;
     178           2 :         LIST_HEAD(wrk);
     179             : 
     180             :         /* Give urgent case more budget */
     181           2 :         if (urgent_only)
     182           0 :                 do_dev += MAX_DO_DEV_PER_LOOP;
     183             : 
     184             :         /*
     185             :          * Limit the number of linkwatch events to one
     186             :          * per second so that a runaway driver does not
     187             :          * cause a storm of messages on the netlink
     188             :          * socket.  This limit does not apply to up events
     189             :          * while the device qdisc is down.
     190             :          */
     191           2 :         if (!urgent_only)
     192           2 :                 linkwatch_nextevent = jiffies + HZ;
     193             :         /* Limit wrap-around effect on delay. */
     194           0 :         else if (time_after(linkwatch_nextevent, jiffies + HZ))
     195           0 :                 linkwatch_nextevent = jiffies;
     196             : 
     197           2 :         clear_bit(LW_URGENT, &linkwatch_flags);
     198             : 
     199           2 :         spin_lock_irq(&lweventlist_lock);
     200           2 :         list_splice_init(&lweventlist, &wrk);
     201             : 
     202           2 :         while (!list_empty(&wrk) && do_dev > 0) {
     203             : 
     204           2 :                 dev = list_first_entry(&wrk, struct net_device, link_watch_list);
     205           2 :                 list_del_init(&dev->link_watch_list);
     206             : 
     207           2 :                 if (urgent_only && !linkwatch_urgent_event(dev)) {
     208           0 :                         list_add_tail(&dev->link_watch_list, &lweventlist);
     209           0 :                         continue;
     210             :                 }
     211           2 :                 spin_unlock_irq(&lweventlist_lock);
     212           2 :                 linkwatch_do_dev(dev);
     213           2 :                 do_dev--;
     214           6 :                 spin_lock_irq(&lweventlist_lock);
     215             :         }
     216             : 
     217             :         /* Add the remaining work back to lweventlist */
     218           2 :         list_splice_init(&wrk, &lweventlist);
     219             : 
     220           2 :         if (!list_empty(&lweventlist))
     221           0 :                 linkwatch_schedule_work(0);
     222           2 :         spin_unlock_irq(&lweventlist_lock);
     223           2 : }
     224             : 
     225           0 : void linkwatch_forget_dev(struct net_device *dev)
     226             : {
     227           0 :         unsigned long flags;
     228           0 :         int clean = 0;
     229             : 
     230           0 :         spin_lock_irqsave(&lweventlist_lock, flags);
     231           0 :         if (!list_empty(&dev->link_watch_list)) {
     232           0 :                 list_del_init(&dev->link_watch_list);
     233           0 :                 clean = 1;
     234             :         }
     235           0 :         spin_unlock_irqrestore(&lweventlist_lock, flags);
     236           0 :         if (clean)
     237           0 :                 linkwatch_do_dev(dev);
     238           0 : }
     239             : 
     240             : 
     241             : /* Must be called with the rtnl semaphore held */
     242           0 : void linkwatch_run_queue(void)
     243             : {
     244           0 :         __linkwatch_run_queue(0);
     245           0 : }
     246             : 
     247             : 
     248           2 : static void linkwatch_event(struct work_struct *dummy)
     249             : {
     250           2 :         rtnl_lock();
     251           4 :         __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies));
     252           2 :         rtnl_unlock();
     253           2 : }
     254             : 
     255             : 
     256           2 : void linkwatch_fire_event(struct net_device *dev)
     257             : {
     258           2 :         bool urgent = linkwatch_urgent_event(dev);
     259             : 
     260           2 :         if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
     261           2 :                 linkwatch_add_event(dev);
     262           0 :         } else if (!urgent)
     263             :                 return;
     264             : 
     265           2 :         linkwatch_schedule_work(urgent);
     266             : }
     267             : EXPORT_SYMBOL(linkwatch_fire_event);

Generated by: LCOV version 1.14