blob: 8b45c9d3b2490ce22bfdffd522dd5530439e8ca3 [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>
22#include <linux/list.h>
23#include <linux/slab.h>
24#include <linux/workqueue.h>
25#include <linux/bitops.h>
26#include <asm/types.h>
27
28
29enum lw_bits {
30 LW_RUNNING = 0,
31 LW_SE_USED
32};
33
34static unsigned long linkwatch_flags;
35static unsigned long linkwatch_nextevent;
36
David Howells65f27f32006-11-22 14:55:48 +000037static void linkwatch_event(struct work_struct *dummy);
38static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static LIST_HEAD(lweventlist);
41static DEFINE_SPINLOCK(lweventlist_lock);
42
43struct lw_event {
44 struct list_head list;
45 struct net_device *dev;
46};
47
48/* Avoid kmalloc() for most systems */
49static struct lw_event singleevent;
50
Stefan Rompfb00055a2006-03-20 17:09:11 -080051static unsigned char default_operstate(const struct net_device *dev)
52{
53 if (!netif_carrier_ok(dev))
54 return (dev->ifindex != dev->iflink ?
55 IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
56
57 if (netif_dormant(dev))
58 return IF_OPER_DORMANT;
59
60 return IF_OPER_UP;
61}
62
63
64static void rfc2863_policy(struct net_device *dev)
65{
66 unsigned char operstate = default_operstate(dev);
67
68 if (operstate == dev->operstate)
69 return;
70
71 write_lock_bh(&dev_base_lock);
72
73 switch(dev->link_mode) {
74 case IF_LINK_MODE_DORMANT:
75 if (operstate == IF_OPER_UP)
76 operstate = IF_OPER_DORMANT;
77 break;
78
79 case IF_LINK_MODE_DEFAULT:
80 default:
81 break;
82 };
83
84 dev->operstate = operstate;
85
86 write_unlock_bh(&dev_base_lock);
87}
88
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Must be called with the rtnl semaphore held */
91void linkwatch_run_queue(void)
92{
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070093 struct list_head head, *n, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95 spin_lock_irq(&lweventlist_lock);
Oleg Nesterov626ab0e2006-06-23 02:05:55 -070096 list_replace_init(&lweventlist, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 spin_unlock_irq(&lweventlist_lock);
98
99 list_for_each_safe(n, next, &head) {
100 struct lw_event *event = list_entry(n, struct lw_event, list);
101 struct net_device *dev = event->dev;
102
103 if (event == &singleevent) {
104 clear_bit(LW_SE_USED, &linkwatch_flags);
105 } else {
106 kfree(event);
107 }
108
109 /* We are about to handle this device,
110 * so new events can be accepted
111 */
112 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
113
Stefan Rompfb00055a2006-03-20 17:09:11 -0800114 rfc2863_policy(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (dev->flags & IFF_UP) {
Tommy S. Christensencacaddf2005-05-03 16:18:52 -0700116 if (netif_carrier_ok(dev)) {
117 WARN_ON(dev->qdisc_sleeping == &noop_qdisc);
118 dev_activate(dev);
119 } else
120 dev_deactivate(dev);
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 netdev_state_change(dev);
123 }
124
125 dev_put(dev);
126 }
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900127}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129
David Howells65f27f32006-11-22 14:55:48 +0000130static void linkwatch_event(struct work_struct *dummy)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
132 /* Limit the number of linkwatch events to one
133 * per second so that a runaway driver does not
134 * cause a storm of messages on the netlink
135 * socket
YOSHIFUJI Hideaki4ec93ed2007-02-09 23:24:36 +0900136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 linkwatch_nextevent = jiffies + HZ;
138 clear_bit(LW_RUNNING, &linkwatch_flags);
139
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800140 rtnl_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 linkwatch_run_queue();
Stephen Hemminger6756ae42006-03-20 22:23:58 -0800142 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
145
146void linkwatch_fire_event(struct net_device *dev)
147{
148 if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
149 unsigned long flags;
150 struct lw_event *event;
151
152 if (test_and_set_bit(LW_SE_USED, &linkwatch_flags)) {
153 event = kmalloc(sizeof(struct lw_event), GFP_ATOMIC);
154
155 if (unlikely(event == NULL)) {
156 clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
157 return;
158 }
159 } else {
160 event = &singleevent;
161 }
162
163 dev_hold(dev);
164 event->dev = dev;
165
166 spin_lock_irqsave(&lweventlist_lock, flags);
167 list_add_tail(&event->list, &lweventlist);
168 spin_unlock_irqrestore(&lweventlist_lock, flags);
169
170 if (!test_and_set_bit(LW_RUNNING, &linkwatch_flags)) {
Herbert Xu8c105682006-05-09 15:27:54 -0700171 unsigned long delay = linkwatch_nextevent - jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Herbert Xu8c105682006-05-09 15:27:54 -0700173 /* If we wrap around we'll delay it by at most HZ. */
David Howells52bad642006-11-22 14:54:01 +0000174 if (delay > HZ)
175 delay = 0;
176 schedule_delayed_work(&linkwatch_work, delay);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 }
178 }
179}
180
181EXPORT_SYMBOL(linkwatch_fire_event);