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/config.h>
15 #include <linux/module.h>
16 #include <linux/netdevice.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(void *dummy
);
38 static DECLARE_WORK(linkwatch_work
, linkwatch_event
, NULL
);
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 /* Must be called with the rtnl semaphore held */
52 void linkwatch_run_queue(void)
55 struct list_head
*n
, *next
;
57 spin_lock_irq(&lweventlist_lock
);
58 list_splice_init(&lweventlist
, &head
);
59 spin_unlock_irq(&lweventlist_lock
);
61 list_for_each_safe(n
, next
, &head
) {
62 struct lw_event
*event
= list_entry(n
, struct lw_event
, list
);
63 struct net_device
*dev
= event
->dev
;
65 if (event
== &singleevent
) {
66 clear_bit(LW_SE_USED
, &linkwatch_flags
);
71 /* We are about to handle this device,
72 * so new events can be accepted
74 clear_bit(__LINK_STATE_LINKWATCH_PENDING
, &dev
->state
);
76 if (dev
->flags
& IFF_UP
) {
77 netdev_state_change(dev
);
85 static void linkwatch_event(void *dummy
)
87 /* Limit the number of linkwatch events to one
88 * per second so that a runaway driver does not
89 * cause a storm of messages on the netlink
92 linkwatch_nextevent
= jiffies
+ HZ
;
93 clear_bit(LW_RUNNING
, &linkwatch_flags
);
96 linkwatch_run_queue();
101 void linkwatch_fire_event(struct net_device
*dev
)
103 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING
, &dev
->state
)) {
105 struct lw_event
*event
;
107 if (test_and_set_bit(LW_SE_USED
, &linkwatch_flags
)) {
108 event
= kmalloc(sizeof(struct lw_event
), GFP_ATOMIC
);
110 if (unlikely(event
== NULL
)) {
111 clear_bit(__LINK_STATE_LINKWATCH_PENDING
, &dev
->state
);
115 event
= &singleevent
;
121 spin_lock_irqsave(&lweventlist_lock
, flags
);
122 list_add_tail(&event
->list
, &lweventlist
);
123 spin_unlock_irqrestore(&lweventlist_lock
, flags
);
125 if (!test_and_set_bit(LW_RUNNING
, &linkwatch_flags
)) {
126 unsigned long thisevent
= jiffies
;
128 if (thisevent
>= linkwatch_nextevent
) {
129 schedule_work(&linkwatch_work
);
131 schedule_delayed_work(&linkwatch_work
, linkwatch_nextevent
- thisevent
);
137 EXPORT_SYMBOL(linkwatch_fire_event
);