1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Linux network device link state notification
6 * Stefan Rompf <sux@loplof.de>
9 #include <linux/module.h>
10 #include <linux/netdevice.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>
26 static unsigned long linkwatch_flags;
27 static unsigned long linkwatch_nextevent;
29 static void linkwatch_event(struct work_struct *dummy);
30 static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
32 static LIST_HEAD(lweventlist);
33 static DEFINE_SPINLOCK(lweventlist_lock);
35 static unsigned char default_operstate(const struct net_device *dev)
37 if (netif_testing(dev))
38 return IF_OPER_TESTING;
40 if (!netif_carrier_ok(dev))
41 return (dev->ifindex != dev_get_iflink(dev) ?
42 IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
44 if (netif_dormant(dev))
45 return IF_OPER_DORMANT;
51 static void rfc2863_policy(struct net_device *dev)
53 unsigned char operstate = default_operstate(dev);
55 if (operstate == dev->operstate)
58 write_lock_bh(&dev_base_lock);
60 switch(dev->link_mode) {
61 case IF_LINK_MODE_TESTING:
62 if (operstate == IF_OPER_UP)
63 operstate = IF_OPER_TESTING;
66 case IF_LINK_MODE_DORMANT:
67 if (operstate == IF_OPER_UP)
68 operstate = IF_OPER_DORMANT;
70 case IF_LINK_MODE_DEFAULT:
75 dev->operstate = operstate;
77 write_unlock_bh(&dev_base_lock);
81 void linkwatch_init_dev(struct net_device *dev)
83 /* Handle pre-registration link state changes */
84 if (!netif_carrier_ok(dev) || netif_dormant(dev) ||
90 static bool linkwatch_urgent_event(struct net_device *dev)
92 if (!netif_running(dev))
95 if (dev->ifindex != dev_get_iflink(dev))
98 if (netif_is_lag_port(dev) || netif_is_lag_master(dev))
101 return netif_carrier_ok(dev) && qdisc_tx_changing(dev);
105 static void linkwatch_add_event(struct net_device *dev)
109 spin_lock_irqsave(&lweventlist_lock, flags);
110 if (list_empty(&dev->link_watch_list)) {
111 list_add_tail(&dev->link_watch_list, &lweventlist);
114 spin_unlock_irqrestore(&lweventlist_lock, flags);
118 static void linkwatch_schedule_work(int urgent)
120 unsigned long delay = linkwatch_nextevent - jiffies;
122 if (test_bit(LW_URGENT, &linkwatch_flags))
125 /* Minimise down-time: drop delay for up event. */
127 if (test_and_set_bit(LW_URGENT, &linkwatch_flags))
132 /* If we wrap around we'll delay it by at most HZ. */
137 * If urgent, schedule immediate execution; otherwise, don't
138 * override the existing timer.
140 if (test_bit(LW_URGENT, &linkwatch_flags))
141 mod_delayed_work(system_wq, &linkwatch_work, 0);
143 schedule_delayed_work(&linkwatch_work, delay);
147 static void linkwatch_do_dev(struct net_device *dev)
150 * Make sure the above read is complete since it can be
151 * rewritten as soon as we clear the bit below.
153 smp_mb__before_atomic();
155 /* We are about to handle this device,
156 * so new events can be accepted
158 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
161 if (dev->flags & IFF_UP) {
162 if (netif_carrier_ok(dev))
167 netdev_state_change(dev);
172 static void __linkwatch_run_queue(int urgent_only)
174 #define MAX_DO_DEV_PER_LOOP 100
176 int do_dev = MAX_DO_DEV_PER_LOOP;
177 struct net_device *dev;
180 /* Give urgent case more budget */
182 do_dev += MAX_DO_DEV_PER_LOOP;
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.
192 linkwatch_nextevent = jiffies + HZ;
193 /* Limit wrap-around effect on delay. */
194 else if (time_after(linkwatch_nextevent, jiffies + HZ))
195 linkwatch_nextevent = jiffies;
197 clear_bit(LW_URGENT, &linkwatch_flags);
199 spin_lock_irq(&lweventlist_lock);
200 list_splice_init(&lweventlist, &wrk);
202 while (!list_empty(&wrk) && do_dev > 0) {
204 dev = list_first_entry(&wrk, struct net_device, link_watch_list);
205 list_del_init(&dev->link_watch_list);
207 if (!netif_device_present(dev) ||
208 (urgent_only && !linkwatch_urgent_event(dev))) {
209 list_add_tail(&dev->link_watch_list, &lweventlist);
212 spin_unlock_irq(&lweventlist_lock);
213 linkwatch_do_dev(dev);
215 spin_lock_irq(&lweventlist_lock);
218 /* Add the remaining work back to lweventlist */
219 list_splice_init(&wrk, &lweventlist);
221 if (!list_empty(&lweventlist))
222 linkwatch_schedule_work(0);
223 spin_unlock_irq(&lweventlist_lock);
226 void linkwatch_forget_dev(struct net_device *dev)
231 spin_lock_irqsave(&lweventlist_lock, flags);
232 if (!list_empty(&dev->link_watch_list)) {
233 list_del_init(&dev->link_watch_list);
236 spin_unlock_irqrestore(&lweventlist_lock, flags);
238 linkwatch_do_dev(dev);
242 /* Must be called with the rtnl semaphore held */
243 void linkwatch_run_queue(void)
245 __linkwatch_run_queue(0);
249 static void linkwatch_event(struct work_struct *dummy)
252 __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies));
257 void linkwatch_fire_event(struct net_device *dev)
259 bool urgent = linkwatch_urgent_event(dev);
261 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
262 linkwatch_add_event(dev);
266 linkwatch_schedule_work(urgent);
268 EXPORT_SYMBOL(linkwatch_fire_event);