]> Pileus Git - ~andy/linux/blob - net/core/link_watch.c
Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[~andy/linux] / net / core / link_watch.c
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
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/workqueue.h>
23 #include <linux/bitops.h>
24 #include <asm/types.h>
25
26
27 enum lw_bits {
28         LW_URGENT = 0,
29 };
30
31 static unsigned long linkwatch_flags;
32 static unsigned long linkwatch_nextevent;
33
34 static void linkwatch_event(struct work_struct *dummy);
35 static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
36
37 static LIST_HEAD(lweventlist);
38 static DEFINE_SPINLOCK(lweventlist_lock);
39
40 static unsigned char default_operstate(const struct net_device *dev)
41 {
42         if (!netif_carrier_ok(dev))
43                 return (dev->ifindex != dev->iflink ?
44                         IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
45
46         if (netif_dormant(dev))
47                 return IF_OPER_DORMANT;
48
49         return IF_OPER_UP;
50 }
51
52
53 static void rfc2863_policy(struct net_device *dev)
54 {
55         unsigned char operstate = default_operstate(dev);
56
57         if (operstate == dev->operstate)
58                 return;
59
60         write_lock_bh(&dev_base_lock);
61
62         switch(dev->link_mode) {
63         case IF_LINK_MODE_DORMANT:
64                 if (operstate == IF_OPER_UP)
65                         operstate = IF_OPER_DORMANT;
66                 break;
67
68         case IF_LINK_MODE_DEFAULT:
69         default:
70                 break;
71         }
72
73         dev->operstate = operstate;
74
75         write_unlock_bh(&dev_base_lock);
76 }
77
78
79 void linkwatch_init_dev(struct net_device *dev)
80 {
81         /* Handle pre-registration link state changes */
82         if (!netif_carrier_ok(dev) || netif_dormant(dev))
83                 rfc2863_policy(dev);
84 }
85
86
87 static bool linkwatch_urgent_event(struct net_device *dev)
88 {
89         if (!netif_running(dev))
90                 return false;
91
92         if (dev->ifindex != dev->iflink)
93                 return true;
94
95         return netif_carrier_ok(dev) && qdisc_tx_changing(dev);
96 }
97
98
99 static void linkwatch_add_event(struct net_device *dev)
100 {
101         unsigned long flags;
102
103         spin_lock_irqsave(&lweventlist_lock, flags);
104         if (list_empty(&dev->link_watch_list)) {
105                 list_add_tail(&dev->link_watch_list, &lweventlist);
106                 dev_hold(dev);
107         }
108         spin_unlock_irqrestore(&lweventlist_lock, flags);
109 }
110
111
112 static void linkwatch_schedule_work(int urgent)
113 {
114         unsigned long delay = linkwatch_nextevent - jiffies;
115
116         if (test_bit(LW_URGENT, &linkwatch_flags))
117                 return;
118
119         /* Minimise down-time: drop delay for up event. */
120         if (urgent) {
121                 if (test_and_set_bit(LW_URGENT, &linkwatch_flags))
122                         return;
123                 delay = 0;
124         }
125
126         /* If we wrap around we'll delay it by at most HZ. */
127         if (delay > HZ)
128                 delay = 0;
129
130         /*
131          * This is true if we've scheduled it immeditately or if we don't
132          * need an immediate execution and it's already pending.
133          */
134         if (schedule_delayed_work(&linkwatch_work, delay) == !delay)
135                 return;
136
137         /* Don't bother if there is nothing urgent. */
138         if (!test_bit(LW_URGENT, &linkwatch_flags))
139                 return;
140
141         /* It's already running which is good enough. */
142         if (!__cancel_delayed_work(&linkwatch_work))
143                 return;
144
145         /* Otherwise we reschedule it again for immediate execution. */
146         schedule_delayed_work(&linkwatch_work, 0);
147 }
148
149
150 static void linkwatch_do_dev(struct net_device *dev)
151 {
152         /*
153          * Make sure the above read is complete since it can be
154          * rewritten as soon as we clear the bit below.
155          */
156         smp_mb__before_clear_bit();
157
158         /* We are about to handle this device,
159          * so new events can be accepted
160          */
161         clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
162
163         rfc2863_policy(dev);
164         if (dev->flags & IFF_UP) {
165                 if (netif_carrier_ok(dev))
166                         dev_activate(dev);
167                 else
168                         dev_deactivate(dev);
169
170                 netdev_state_change(dev);
171         }
172         dev_put(dev);
173 }
174
175 static void __linkwatch_run_queue(int urgent_only)
176 {
177         struct net_device *dev;
178         LIST_HEAD(wrk);
179
180         /*
181          * Limit the number of linkwatch events to one
182          * per second so that a runaway driver does not
183          * cause a storm of messages on the netlink
184          * socket.  This limit does not apply to up events
185          * while the device qdisc is down.
186          */
187         if (!urgent_only)
188                 linkwatch_nextevent = jiffies + HZ;
189         /* Limit wrap-around effect on delay. */
190         else if (time_after(linkwatch_nextevent, jiffies + HZ))
191                 linkwatch_nextevent = jiffies;
192
193         clear_bit(LW_URGENT, &linkwatch_flags);
194
195         spin_lock_irq(&lweventlist_lock);
196         list_splice_init(&lweventlist, &wrk);
197
198         while (!list_empty(&wrk)) {
199
200                 dev = list_first_entry(&wrk, struct net_device, link_watch_list);
201                 list_del_init(&dev->link_watch_list);
202
203                 if (urgent_only && !linkwatch_urgent_event(dev)) {
204                         list_add_tail(&dev->link_watch_list, &lweventlist);
205                         continue;
206                 }
207                 spin_unlock_irq(&lweventlist_lock);
208                 linkwatch_do_dev(dev);
209                 spin_lock_irq(&lweventlist_lock);
210         }
211
212         if (!list_empty(&lweventlist))
213                 linkwatch_schedule_work(0);
214         spin_unlock_irq(&lweventlist_lock);
215 }
216
217 void linkwatch_forget_dev(struct net_device *dev)
218 {
219         unsigned long flags;
220         int clean = 0;
221
222         spin_lock_irqsave(&lweventlist_lock, flags);
223         if (!list_empty(&dev->link_watch_list)) {
224                 list_del_init(&dev->link_watch_list);
225                 clean = 1;
226         }
227         spin_unlock_irqrestore(&lweventlist_lock, flags);
228         if (clean)
229                 linkwatch_do_dev(dev);
230 }
231
232
233 /* Must be called with the rtnl semaphore held */
234 void linkwatch_run_queue(void)
235 {
236         __linkwatch_run_queue(0);
237 }
238
239
240 static void linkwatch_event(struct work_struct *dummy)
241 {
242         rtnl_lock();
243         __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies));
244         rtnl_unlock();
245 }
246
247
248 void linkwatch_fire_event(struct net_device *dev)
249 {
250         bool urgent = linkwatch_urgent_event(dev);
251
252         if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
253                 linkwatch_add_event(dev);
254         } else if (!urgent)
255                 return;
256
257         linkwatch_schedule_work(urgent);
258 }
259 EXPORT_SYMBOL(linkwatch_fire_event);