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