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/slab.h>
23 #include <linux/workqueue.h>
24 #include <linux/bitops.h>
25 #include <asm/types.h>
32 static unsigned long linkwatch_flags;
33 static unsigned long linkwatch_nextevent;
35 static void linkwatch_event(struct work_struct *dummy);
36 static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
38 static struct net_device *lweventlist;
39 static DEFINE_SPINLOCK(lweventlist_lock);
41 static unsigned char default_operstate(const struct net_device *dev)
43 if (!netif_carrier_ok(dev))
44 return (dev->ifindex != dev->iflink ?
45 IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
47 if (netif_dormant(dev))
48 return IF_OPER_DORMANT;
54 static void rfc2863_policy(struct net_device *dev)
56 unsigned char operstate = default_operstate(dev);
58 if (operstate == dev->operstate)
61 write_lock_bh(&dev_base_lock);
63 switch(dev->link_mode) {
64 case IF_LINK_MODE_DORMANT:
65 if (operstate == IF_OPER_UP)
66 operstate = IF_OPER_DORMANT;
69 case IF_LINK_MODE_DEFAULT:
74 dev->operstate = operstate;
76 write_unlock_bh(&dev_base_lock);
80 /* Must be called with the rtnl semaphore held */
81 void linkwatch_run_queue(void)
83 struct net_device *next;
85 spin_lock_irq(&lweventlist_lock);
88 spin_unlock_irq(&lweventlist_lock);
91 struct net_device *dev = next;
93 next = dev->link_watch_next;
96 * Make sure the above read is complete since it can be
97 * rewritten as soon as we clear the bit below.
99 smp_mb__before_clear_bit();
101 /* We are about to handle this device,
102 * so new events can be accepted
104 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
107 if (dev->flags & IFF_UP) {
108 if (netif_carrier_ok(dev)) {
109 WARN_ON(dev->qdisc_sleeping == &noop_qdisc);
114 netdev_state_change(dev);
122 static void linkwatch_event(struct work_struct *dummy)
124 /* Limit the number of linkwatch events to one
125 * per second so that a runaway driver does not
126 * cause a storm of messages on the netlink
129 linkwatch_nextevent = jiffies + HZ;
130 clear_bit(LW_RUNNING, &linkwatch_flags);
133 linkwatch_run_queue();
138 void linkwatch_fire_event(struct net_device *dev)
140 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
145 spin_lock_irqsave(&lweventlist_lock, flags);
146 dev->link_watch_next = lweventlist;
148 spin_unlock_irqrestore(&lweventlist_lock, flags);
150 if (!test_and_set_bit(LW_RUNNING, &linkwatch_flags)) {
151 unsigned long delay = linkwatch_nextevent - jiffies;
153 /* If we wrap around we'll delay it by at most HZ. */
156 schedule_delayed_work(&linkwatch_work, delay);
161 EXPORT_SYMBOL(linkwatch_fire_event);