Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / net / core / link_watch.c
bloba5e372b9ec4df3a866ff4a26b44c4fed96021b8b
1 /*
2 * Linux network device link state notification
4 * Author:
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>
16 #include <linux/if.h>
17 #include <net/sock.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>
28 enum lw_bits {
29 LW_URGENT = 0,
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;
50 return IF_OPER_UP;
54 static void rfc2863_policy(struct net_device *dev)
56 unsigned char operstate = default_operstate(dev);
58 if (operstate == dev->operstate)
59 return;
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;
67 break;
69 case IF_LINK_MODE_DEFAULT:
70 default:
71 break;
74 dev->operstate = operstate;
76 write_unlock_bh(&dev_base_lock);
80 static int linkwatch_urgent_event(struct net_device *dev)
82 return netif_running(dev) && netif_carrier_ok(dev) &&
83 dev->qdisc != dev->qdisc_sleeping;
87 static void linkwatch_add_event(struct net_device *dev)
89 unsigned long flags;
91 spin_lock_irqsave(&lweventlist_lock, flags);
92 dev->link_watch_next = lweventlist;
93 lweventlist = dev;
94 spin_unlock_irqrestore(&lweventlist_lock, flags);
98 static void linkwatch_schedule_work(int urgent)
100 unsigned long delay = linkwatch_nextevent - jiffies;
102 if (test_bit(LW_URGENT, &linkwatch_flags))
103 return;
105 /* Minimise down-time: drop delay for up event. */
106 if (urgent) {
107 if (test_and_set_bit(LW_URGENT, &linkwatch_flags))
108 return;
109 delay = 0;
112 /* If we wrap around we'll delay it by at most HZ. */
113 if (delay > HZ)
114 delay = 0;
117 * This is true if we've scheduled it immeditately or if we don't
118 * need an immediate execution and it's already pending.
120 if (schedule_delayed_work(&linkwatch_work, delay) == !delay)
121 return;
123 /* Don't bother if there is nothing urgent. */
124 if (!test_bit(LW_URGENT, &linkwatch_flags))
125 return;
127 /* It's already running which is good enough. */
128 if (!cancel_delayed_work(&linkwatch_work))
129 return;
131 /* Otherwise we reschedule it again for immediate exection. */
132 schedule_delayed_work(&linkwatch_work, 0);
136 static void __linkwatch_run_queue(int urgent_only)
138 struct net_device *next;
141 * Limit the number of linkwatch events to one
142 * per second so that a runaway driver does not
143 * cause a storm of messages on the netlink
144 * socket. This limit does not apply to up events
145 * while the device qdisc is down.
147 if (!urgent_only)
148 linkwatch_nextevent = jiffies + HZ;
149 /* Limit wrap-around effect on delay. */
150 else if (time_after(linkwatch_nextevent, jiffies + HZ))
151 linkwatch_nextevent = jiffies;
153 clear_bit(LW_URGENT, &linkwatch_flags);
155 spin_lock_irq(&lweventlist_lock);
156 next = lweventlist;
157 lweventlist = NULL;
158 spin_unlock_irq(&lweventlist_lock);
160 while (next) {
161 struct net_device *dev = next;
163 next = dev->link_watch_next;
165 if (urgent_only && !linkwatch_urgent_event(dev)) {
166 linkwatch_add_event(dev);
167 continue;
171 * Make sure the above read is complete since it can be
172 * rewritten as soon as we clear the bit below.
174 smp_mb__before_clear_bit();
176 /* We are about to handle this device,
177 * so new events can be accepted
179 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
181 rfc2863_policy(dev);
182 if (dev->flags & IFF_UP) {
183 if (netif_carrier_ok(dev)) {
184 WARN_ON(dev->qdisc_sleeping == &noop_qdisc);
185 dev_activate(dev);
186 } else
187 dev_deactivate(dev);
189 netdev_state_change(dev);
192 dev_put(dev);
195 if (lweventlist)
196 linkwatch_schedule_work(0);
200 /* Must be called with the rtnl semaphore held */
201 void linkwatch_run_queue(void)
203 __linkwatch_run_queue(0);
207 static void linkwatch_event(struct work_struct *dummy)
209 rtnl_lock();
210 __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies));
211 rtnl_unlock();
215 void linkwatch_fire_event(struct net_device *dev)
217 int urgent = linkwatch_urgent_event(dev);
219 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
220 dev_hold(dev);
222 linkwatch_add_event(dev);
223 } else if (!urgent)
224 return;
226 linkwatch_schedule_work(urgent);
229 EXPORT_SYMBOL(linkwatch_fire_event);