2 * Linux network device link state notification
5 * Stefan Rompf <sux@loplof.de>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
14 #include <linux/module.h>
15 #include <linux/netdevice.h>
18 #include <net/pkt_sched.h>
19 #include <linux/rtnetlink.h>
20 #include <linux/jiffies.h>
21 #include <linux/spinlock.h>
22 #include <linux/workqueue.h>
23 #include <linux/bitops.h>
24 #include <asm/types.h>
31 static unsigned long linkwatch_flags
;
32 static unsigned long linkwatch_nextevent
;
34 static void linkwatch_event(struct work_struct
*dummy
);
35 static DECLARE_DELAYED_WORK(linkwatch_work
, linkwatch_event
);
37 static LIST_HEAD(lweventlist
);
38 static DEFINE_SPINLOCK(lweventlist_lock
);
40 static unsigned char default_operstate(const struct net_device
*dev
)
42 if (!netif_carrier_ok(dev
))
43 return (dev
->ifindex
!= dev
->iflink
?
44 IF_OPER_LOWERLAYERDOWN
: IF_OPER_DOWN
);
46 if (netif_dormant(dev
))
47 return IF_OPER_DORMANT
;
53 static void rfc2863_policy(struct net_device
*dev
)
55 unsigned char operstate
= default_operstate(dev
);
57 if (operstate
== dev
->operstate
)
60 write_lock_bh(&dev_base_lock
);
62 switch(dev
->link_mode
) {
63 case IF_LINK_MODE_DORMANT
:
64 if (operstate
== IF_OPER_UP
)
65 operstate
= IF_OPER_DORMANT
;
68 case IF_LINK_MODE_DEFAULT
:
73 dev
->operstate
= operstate
;
75 write_unlock_bh(&dev_base_lock
);
79 static bool linkwatch_urgent_event(struct net_device
*dev
)
81 if (!netif_running(dev
))
84 if (dev
->ifindex
!= dev
->iflink
)
87 return netif_carrier_ok(dev
) && qdisc_tx_changing(dev
);
91 static void linkwatch_add_event(struct net_device
*dev
)
95 spin_lock_irqsave(&lweventlist_lock
, flags
);
96 if (list_empty(&dev
->link_watch_list
)) {
97 list_add_tail(&dev
->link_watch_list
, &lweventlist
);
100 spin_unlock_irqrestore(&lweventlist_lock
, flags
);
104 static void linkwatch_schedule_work(int urgent
)
106 unsigned long delay
= linkwatch_nextevent
- jiffies
;
108 if (test_bit(LW_URGENT
, &linkwatch_flags
))
111 /* Minimise down-time: drop delay for up event. */
113 if (test_and_set_bit(LW_URGENT
, &linkwatch_flags
))
118 /* If we wrap around we'll delay it by at most HZ. */
123 * This is true if we've scheduled it immeditately or if we don't
124 * need an immediate execution and it's already pending.
126 if (schedule_delayed_work(&linkwatch_work
, delay
) == !delay
)
129 /* Don't bother if there is nothing urgent. */
130 if (!test_bit(LW_URGENT
, &linkwatch_flags
))
133 /* It's already running which is good enough. */
134 if (!__cancel_delayed_work(&linkwatch_work
))
137 /* Otherwise we reschedule it again for immediate execution. */
138 schedule_delayed_work(&linkwatch_work
, 0);
142 static void linkwatch_do_dev(struct net_device
*dev
)
145 * Make sure the above read is complete since it can be
146 * rewritten as soon as we clear the bit below.
148 smp_mb__before_clear_bit();
150 /* We are about to handle this device,
151 * so new events can be accepted
153 clear_bit(__LINK_STATE_LINKWATCH_PENDING
, &dev
->state
);
156 if (dev
->flags
& IFF_UP
) {
157 if (netif_carrier_ok(dev
))
162 netdev_state_change(dev
);
167 static void __linkwatch_run_queue(int urgent_only
)
169 struct net_device
*dev
;
173 * Limit the number of linkwatch events to one
174 * per second so that a runaway driver does not
175 * cause a storm of messages on the netlink
176 * socket. This limit does not apply to up events
177 * while the device qdisc is down.
180 linkwatch_nextevent
= jiffies
+ HZ
;
181 /* Limit wrap-around effect on delay. */
182 else if (time_after(linkwatch_nextevent
, jiffies
+ HZ
))
183 linkwatch_nextevent
= jiffies
;
185 clear_bit(LW_URGENT
, &linkwatch_flags
);
187 spin_lock_irq(&lweventlist_lock
);
188 list_splice_init(&lweventlist
, &wrk
);
190 while (!list_empty(&wrk
)) {
192 dev
= list_first_entry(&wrk
, struct net_device
, link_watch_list
);
193 list_del_init(&dev
->link_watch_list
);
195 if (urgent_only
&& !linkwatch_urgent_event(dev
)) {
196 list_add_tail(&dev
->link_watch_list
, &lweventlist
);
199 spin_unlock_irq(&lweventlist_lock
);
200 linkwatch_do_dev(dev
);
201 spin_lock_irq(&lweventlist_lock
);
204 if (!list_empty(&lweventlist
))
205 linkwatch_schedule_work(0);
206 spin_unlock_irq(&lweventlist_lock
);
209 void linkwatch_forget_dev(struct net_device
*dev
)
214 spin_lock_irqsave(&lweventlist_lock
, flags
);
215 if (!list_empty(&dev
->link_watch_list
)) {
216 list_del_init(&dev
->link_watch_list
);
219 spin_unlock_irqrestore(&lweventlist_lock
, flags
);
221 linkwatch_do_dev(dev
);
225 /* Must be called with the rtnl semaphore held */
226 void linkwatch_run_queue(void)
228 __linkwatch_run_queue(0);
232 static void linkwatch_event(struct work_struct
*dummy
)
235 __linkwatch_run_queue(time_after(linkwatch_nextevent
, jiffies
));
240 void linkwatch_fire_event(struct net_device
*dev
)
242 bool urgent
= linkwatch_urgent_event(dev
);
244 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING
, &dev
->state
)) {
245 linkwatch_add_event(dev
);
249 linkwatch_schedule_work(urgent
);
251 EXPORT_SYMBOL(linkwatch_fire_event
);