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/list.h>
23 #include <linux/slab.h>
24 #include <linux/workqueue.h>
25 #include <linux/bitops.h>
26 #include <asm/types.h>
34 static unsigned long linkwatch_flags
;
35 static unsigned long linkwatch_nextevent
;
37 static void linkwatch_event(struct work_struct
*dummy
);
38 static DECLARE_DELAYED_WORK(linkwatch_work
, linkwatch_event
);
40 static LIST_HEAD(lweventlist
);
41 static DEFINE_SPINLOCK(lweventlist_lock
);
44 struct list_head list
;
45 struct net_device
*dev
;
48 /* Avoid kmalloc() for most systems */
49 static struct lw_event singleevent
;
51 static unsigned char default_operstate(const struct net_device
*dev
)
53 if (!netif_carrier_ok(dev
))
54 return (dev
->ifindex
!= dev
->iflink
?
55 IF_OPER_LOWERLAYERDOWN
: IF_OPER_DOWN
);
57 if (netif_dormant(dev
))
58 return IF_OPER_DORMANT
;
64 static void rfc2863_policy(struct net_device
*dev
)
66 unsigned char operstate
= default_operstate(dev
);
68 if (operstate
== dev
->operstate
)
71 write_lock_bh(&dev_base_lock
);
73 switch(dev
->link_mode
) {
74 case IF_LINK_MODE_DORMANT
:
75 if (operstate
== IF_OPER_UP
)
76 operstate
= IF_OPER_DORMANT
;
79 case IF_LINK_MODE_DEFAULT
:
84 dev
->operstate
= operstate
;
86 write_unlock_bh(&dev_base_lock
);
90 /* Must be called with the rtnl semaphore held */
91 void linkwatch_run_queue(void)
93 struct list_head head
, *n
, *next
;
95 spin_lock_irq(&lweventlist_lock
);
96 list_replace_init(&lweventlist
, &head
);
97 spin_unlock_irq(&lweventlist_lock
);
99 list_for_each_safe(n
, next
, &head
) {
100 struct lw_event
*event
= list_entry(n
, struct lw_event
, list
);
101 struct net_device
*dev
= event
->dev
;
103 if (event
== &singleevent
) {
104 clear_bit(LW_SE_USED
, &linkwatch_flags
);
109 /* We are about to handle this device,
110 * so new events can be accepted
112 clear_bit(__LINK_STATE_LINKWATCH_PENDING
, &dev
->state
);
115 if (dev
->flags
& IFF_UP
) {
116 if (netif_carrier_ok(dev
)) {
117 WARN_ON(dev
->qdisc_sleeping
== &noop_qdisc
);
122 netdev_state_change(dev
);
130 static void linkwatch_event(struct work_struct
*dummy
)
132 /* Limit the number of linkwatch events to one
133 * per second so that a runaway driver does not
134 * cause a storm of messages on the netlink
137 linkwatch_nextevent
= jiffies
+ HZ
;
138 clear_bit(LW_RUNNING
, &linkwatch_flags
);
141 linkwatch_run_queue();
146 void linkwatch_fire_event(struct net_device
*dev
)
148 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING
, &dev
->state
)) {
150 struct lw_event
*event
;
152 if (test_and_set_bit(LW_SE_USED
, &linkwatch_flags
)) {
153 event
= kmalloc(sizeof(struct lw_event
), GFP_ATOMIC
);
155 if (unlikely(event
== NULL
)) {
156 clear_bit(__LINK_STATE_LINKWATCH_PENDING
, &dev
->state
);
160 event
= &singleevent
;
166 spin_lock_irqsave(&lweventlist_lock
, flags
);
167 list_add_tail(&event
->list
, &lweventlist
);
168 spin_unlock_irqrestore(&lweventlist_lock
, flags
);
170 if (!test_and_set_bit(LW_RUNNING
, &linkwatch_flags
)) {
171 unsigned long delay
= linkwatch_nextevent
- jiffies
;
173 /* If we wrap around we'll delay it by at most HZ. */
176 schedule_delayed_work(&linkwatch_work
, delay
);
181 EXPORT_SYMBOL(linkwatch_fire_event
);