blob: b5f45799c2f3905300fac9d4eb061cae89534731 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/netdevice.h>
16#include <linux/if.h>
17#include <net/sock.h>
Tommy S. Christensencacaddf2005-05-03 16:18:52 -070018#include <net/pkt_sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/rtnetlink.h>
20#include <linux/jiffies.h>
21#include <linux/spinlock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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,
Linus Torvalds1da177e2005-04-16 15:20:36 -070030};
31
32static unsigned long linkwatch_flags;
33static unsigned long linkwatch_nextevent;
34
David Howells65f27f32006-11-22 14:55:48 +000035static void linkwatch_event(struct work_struct *dummy);
36static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Herbert Xu572a1032007-05-08 18:34:17 -070038static struct net_device *lweventlist;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039static DEFINE_SPINLOCK(lweventlist_lock);
40
Stefan Rompfb00055a2006-03-20 17:09:11 -080041static 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;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -070072 }
Stefan Rompfb00055a2006-03-20 17:09:11 -080073
74 dev->operstate = operstate;
75
76 write_unlock_bh(&dev_base_lock);
77}
78
79
Herbert Xu294cc442007-05-08 18:36:28 -070080static 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. */
104 if (delay > HZ)
105 delay = 0;
106
107 schedule_delayed_work(&linkwatch_work, delay);
108}
109
110
111static void __linkwatch_run_queue(int urgent_only)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Herbert Xu572a1032007-05-08 18:34:17 -0700113 struct net_device *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Herbert Xu294cc442007-05-08 18:36:28 -0700115 /*
116 * Limit the number of linkwatch events to one
117 * per second so that a runaway driver does not
118 * cause a storm of messages on the netlink
119 * socket. This limit does not apply to up events
120 * while the device qdisc is down.
121 */
122 if (!urgent_only)
123 linkwatch_nextevent = jiffies + HZ;
124 clear_bit(LW_RUNNING, &linkwatch_flags);
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 spin_lock_irq(&lweventlist_lock);
Herbert Xu572a1032007-05-08 18:34:17 -0700127 next = lweventlist;
128 lweventlist = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 spin_unlock_irq(&lweventlist_lock);
130
Herbert Xu572a1032007-05-08 18:34:17 -0700131 while (next) {
132 struct net_device *dev = next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Herbert Xu572a1032007-05-08 18:34:17 -0700134 next = dev->link_watch_next;
135
Herbert Xu294cc442007-05-08 18:36:28 -0700136 if (urgent_only && !linkwatch_urgent_event(dev)) {
137 linkwatch_add_event(dev);
138 continue;
139 }
140
Herbert Xu572a1032007-05-08 18:34:17 -0700141 /*
142 * Make sure the above read is complete since it can be
143 * rewritten as soon as we clear the bit below.
144 */
145 smp_mb__before_clear_bit();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147 /* We are about to handle this device,
148 * so new events can be accepted
149 */
150 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
151
Stefan Rompfb00055a2006-03-20 17:09:11 -0800152 rfc2863_policy(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (dev->flags & IFF_UP) {
Tommy S. Christensencacaddf2005-05-03 16:18:52 -0700154 if (netif_carrier_ok(dev)) {
155 WARN_ON(dev->qdisc_sleeping == &noop_qdisc);
156 dev_activate(dev);
157 } else
158 dev_deactivate(dev);
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 netdev_state_change(dev);
161 }
162
163 dev_put(dev);
164 }
Herbert Xu294cc442007-05-08 18:36:28 -0700165
166 if (lweventlist)
167 linkwatch_schedule_work(linkwatch_nextevent - jiffies);
168}
169
170
171/* Must be called with the rtnl semaphore held */
172void linkwatch_run_queue(void)
173{
174 __linkwatch_run_queue(0);
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900175}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
177
David Howells65f27f32006-11-22 14:55:48 +0000178static void linkwatch_event(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800180 rtnl_lock();
Herbert Xu294cc442007-05-08 18:36:28 -0700181 __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies));
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800182 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
185
186void linkwatch_fire_event(struct net_device *dev)
187{
188 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
Herbert Xu294cc442007-05-08 18:36:28 -0700189 unsigned long delay;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Herbert Xu294cc442007-05-08 18:36:28 -0700193 linkwatch_add_event(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Herbert Xu294cc442007-05-08 18:36:28 -0700195 delay = linkwatch_nextevent - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Herbert Xu294cc442007-05-08 18:36:28 -0700197 /* Minimise down-time: drop delay for up event. */
198 if (linkwatch_urgent_event(dev))
199 delay = 0;
200
201 linkwatch_schedule_work(delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203}
204
205EXPORT_SYMBOL(linkwatch_fire_event);