]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/core/link_watch.c
[NET] link_watch: Eliminate potential delay on wrap-around
[net-next-2.6.git] / net / core / link_watch.c
CommitLineData
1da177e4
LT
1/*
2 * Linux network device link state notification
3 *
4 * Author:
5 * Stefan Rompf <sux@loplof.de>
6 *
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.
11 *
12 */
13
1da177e4
LT
14#include <linux/module.h>
15#include <linux/netdevice.h>
16#include <linux/if.h>
17#include <net/sock.h>
cacaddf5 18#include <net/pkt_sched.h>
1da177e4
LT
19#include <linux/rtnetlink.h>
20#include <linux/jiffies.h>
21#include <linux/spinlock.h>
1da177e4
LT
22#include <linux/slab.h>
23#include <linux/workqueue.h>
24#include <linux/bitops.h>
25#include <asm/types.h>
26
27
28enum lw_bits {
29 LW_RUNNING = 0,
1da177e4
LT
30};
31
32static unsigned long linkwatch_flags;
33static unsigned long linkwatch_nextevent;
34
65f27f38
DH
35static void linkwatch_event(struct work_struct *dummy);
36static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
1da177e4 37
572a103d 38static struct net_device *lweventlist;
1da177e4
LT
39static DEFINE_SPINLOCK(lweventlist_lock);
40
b00055aa
SR
41static unsigned char default_operstate(const struct net_device *dev)
42{
43 if (!netif_carrier_ok(dev))
44 return (dev->ifindex != dev->iflink ?
45 IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
46
47 if (netif_dormant(dev))
48 return IF_OPER_DORMANT;
49
50 return IF_OPER_UP;
51}
52
53
54static void rfc2863_policy(struct net_device *dev)
55{
56 unsigned char operstate = default_operstate(dev);
57
58 if (operstate == dev->operstate)
59 return;
60
61 write_lock_bh(&dev_base_lock);
62
63 switch(dev->link_mode) {
64 case IF_LINK_MODE_DORMANT:
65 if (operstate == IF_OPER_UP)
66 operstate = IF_OPER_DORMANT;
67 break;
68
69 case IF_LINK_MODE_DEFAULT:
70 default:
71 break;
3ff50b79 72 }
b00055aa
SR
73
74 dev->operstate = operstate;
75
76 write_unlock_bh(&dev_base_lock);
77}
78
79
294cc44b
HX
80static int linkwatch_urgent_event(struct net_device *dev)
81{
82 return netif_running(dev) && netif_carrier_ok(dev) &&
83 dev->qdisc != dev->qdisc_sleeping;
84}
85
86
87static void linkwatch_add_event(struct net_device *dev)
88{
89 unsigned long flags;
90
91 spin_lock_irqsave(&lweventlist_lock, flags);
92 dev->link_watch_next = lweventlist;
93 lweventlist = dev;
94 spin_unlock_irqrestore(&lweventlist_lock, flags);
95}
96
97
98static void linkwatch_schedule_work(unsigned long delay)
99{
100 if (test_and_set_bit(LW_RUNNING, &linkwatch_flags))
101 return;
102
103 /* If we wrap around we'll delay it by at most HZ. */
db0ccffe
HX
104 if (delay > HZ) {
105 linkwatch_nextevent = jiffies;
294cc44b 106 delay = 0;
db0ccffe 107 }
294cc44b
HX
108
109 schedule_delayed_work(&linkwatch_work, delay);
110}
111
112
113static void __linkwatch_run_queue(int urgent_only)
1da177e4 114{
572a103d 115 struct net_device *next;
1da177e4 116
294cc44b
HX
117 /*
118 * Limit the number of linkwatch events to one
119 * per second so that a runaway driver does not
120 * cause a storm of messages on the netlink
121 * socket. This limit does not apply to up events
122 * while the device qdisc is down.
123 */
124 if (!urgent_only)
125 linkwatch_nextevent = jiffies + HZ;
126 clear_bit(LW_RUNNING, &linkwatch_flags);
127
1da177e4 128 spin_lock_irq(&lweventlist_lock);
572a103d
HX
129 next = lweventlist;
130 lweventlist = NULL;
1da177e4
LT
131 spin_unlock_irq(&lweventlist_lock);
132
572a103d
HX
133 while (next) {
134 struct net_device *dev = next;
1da177e4 135
572a103d
HX
136 next = dev->link_watch_next;
137
294cc44b
HX
138 if (urgent_only && !linkwatch_urgent_event(dev)) {
139 linkwatch_add_event(dev);
140 continue;
141 }
142
572a103d
HX
143 /*
144 * Make sure the above read is complete since it can be
145 * rewritten as soon as we clear the bit below.
146 */
147 smp_mb__before_clear_bit();
1da177e4
LT
148
149 /* We are about to handle this device,
150 * so new events can be accepted
151 */
152 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
153
b00055aa 154 rfc2863_policy(dev);
1da177e4 155 if (dev->flags & IFF_UP) {
cacaddf5
TC
156 if (netif_carrier_ok(dev)) {
157 WARN_ON(dev->qdisc_sleeping == &noop_qdisc);
158 dev_activate(dev);
159 } else
160 dev_deactivate(dev);
161
1da177e4
LT
162 netdev_state_change(dev);
163 }
164
165 dev_put(dev);
166 }
294cc44b
HX
167
168 if (lweventlist)
169 linkwatch_schedule_work(linkwatch_nextevent - jiffies);
4ec93edb 170}
1da177e4
LT
171
172
294cc44b
HX
173/* Must be called with the rtnl semaphore held */
174void linkwatch_run_queue(void)
1da177e4 175{
294cc44b
HX
176 __linkwatch_run_queue(0);
177}
178
1da177e4 179
294cc44b
HX
180static void linkwatch_event(struct work_struct *dummy)
181{
6756ae4b 182 rtnl_lock();
294cc44b 183 __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies));
6756ae4b 184 rtnl_unlock();
1da177e4
LT
185}
186
187
188void linkwatch_fire_event(struct net_device *dev)
189{
190 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
294cc44b 191 unsigned long delay;
1da177e4
LT
192
193 dev_hold(dev);
1da177e4 194
294cc44b 195 linkwatch_add_event(dev);
1da177e4 196
294cc44b 197 delay = linkwatch_nextevent - jiffies;
1da177e4 198
294cc44b
HX
199 /* Minimise down-time: drop delay for up event. */
200 if (linkwatch_urgent_event(dev))
201 delay = 0;
202
203 linkwatch_schedule_work(delay);
1da177e4
LT
204 }
205}
206
207EXPORT_SYMBOL(linkwatch_fire_event);