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_carrier_ok(dev
))
38 return (dev
->ifindex
!= dev_get_iflink(dev
) ?
39 IF_OPER_LOWERLAYERDOWN
: IF_OPER_DOWN
);
41 if (netif_dormant(dev
))
42 return IF_OPER_DORMANT
;
48 static void rfc2863_policy(struct net_device
*dev
)
50 unsigned char operstate
= default_operstate(dev
);
52 if (operstate
== dev
->operstate
)
55 write_lock_bh(&dev_base_lock
);
57 switch(dev
->link_mode
) {
58 case IF_LINK_MODE_DORMANT
:
59 if (operstate
== IF_OPER_UP
)
60 operstate
= IF_OPER_DORMANT
;
63 case IF_LINK_MODE_DEFAULT
:
68 dev
->operstate
= operstate
;
70 write_unlock_bh(&dev_base_lock
);
74 void linkwatch_init_dev(struct net_device
*dev
)
76 /* Handle pre-registration link state changes */
77 if (!netif_carrier_ok(dev
) || netif_dormant(dev
))
82 static bool linkwatch_urgent_event(struct net_device
*dev
)
84 if (!netif_running(dev
))
87 if (dev
->ifindex
!= dev_get_iflink(dev
))
90 if (netif_is_lag_port(dev
) || netif_is_lag_master(dev
))
93 return netif_carrier_ok(dev
) && qdisc_tx_changing(dev
);
97 static void linkwatch_add_event(struct net_device
*dev
)
101 spin_lock_irqsave(&lweventlist_lock
, flags
);
102 if (list_empty(&dev
->link_watch_list
)) {
103 list_add_tail(&dev
->link_watch_list
, &lweventlist
);
106 spin_unlock_irqrestore(&lweventlist_lock
, flags
);
110 static void linkwatch_schedule_work(int urgent
)
112 unsigned long delay
= linkwatch_nextevent
- jiffies
;
114 if (test_bit(LW_URGENT
, &linkwatch_flags
))
117 /* Minimise down-time: drop delay for up event. */
119 if (test_and_set_bit(LW_URGENT
, &linkwatch_flags
))
124 /* If we wrap around we'll delay it by at most HZ. */
129 * If urgent, schedule immediate execution; otherwise, don't
130 * override the existing timer.
132 if (test_bit(LW_URGENT
, &linkwatch_flags
))
133 mod_delayed_work(system_wq
, &linkwatch_work
, 0);
135 schedule_delayed_work(&linkwatch_work
, delay
);
139 static void linkwatch_do_dev(struct net_device
*dev
)
142 * Make sure the above read is complete since it can be
143 * rewritten as soon as we clear the bit below.
145 smp_mb__before_atomic();
147 /* We are about to handle this device,
148 * so new events can be accepted
150 clear_bit(__LINK_STATE_LINKWATCH_PENDING
, &dev
->state
);
153 if (dev
->flags
& IFF_UP
&& netif_device_present(dev
)) {
154 if (netif_carrier_ok(dev
))
159 netdev_state_change(dev
);
164 static void __linkwatch_run_queue(int urgent_only
)
166 struct net_device
*dev
;
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.
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
;
182 clear_bit(LW_URGENT
, &linkwatch_flags
);
184 spin_lock_irq(&lweventlist_lock
);
185 list_splice_init(&lweventlist
, &wrk
);
187 while (!list_empty(&wrk
)) {
189 dev
= list_first_entry(&wrk
, struct net_device
, link_watch_list
);
190 list_del_init(&dev
->link_watch_list
);
192 if (urgent_only
&& !linkwatch_urgent_event(dev
)) {
193 list_add_tail(&dev
->link_watch_list
, &lweventlist
);
196 spin_unlock_irq(&lweventlist_lock
);
197 linkwatch_do_dev(dev
);
198 spin_lock_irq(&lweventlist_lock
);
201 if (!list_empty(&lweventlist
))
202 linkwatch_schedule_work(0);
203 spin_unlock_irq(&lweventlist_lock
);
206 void linkwatch_forget_dev(struct net_device
*dev
)
211 spin_lock_irqsave(&lweventlist_lock
, flags
);
212 if (!list_empty(&dev
->link_watch_list
)) {
213 list_del_init(&dev
->link_watch_list
);
216 spin_unlock_irqrestore(&lweventlist_lock
, flags
);
218 linkwatch_do_dev(dev
);
222 /* Must be called with the rtnl semaphore held */
223 void linkwatch_run_queue(void)
225 __linkwatch_run_queue(0);
229 static void linkwatch_event(struct work_struct
*dummy
)
232 __linkwatch_run_queue(time_after(linkwatch_nextevent
, jiffies
));
237 void linkwatch_fire_event(struct net_device
*dev
)
239 bool urgent
= linkwatch_urgent_event(dev
);
241 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING
, &dev
->state
)) {
242 linkwatch_add_event(dev
);
246 linkwatch_schedule_work(urgent
);
248 EXPORT_SYMBOL(linkwatch_fire_event
);