Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/if.h> |
| 17 | #include <net/sock.h> |
Tommy S. Christensen | cacaddf | 2005-05-03 16:18:52 -0700 | [diff] [blame] | 18 | #include <net/pkt_sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/rtnetlink.h> |
| 20 | #include <linux/jiffies.h> |
| 21 | #include <linux/spinlock.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | #include <linux/slab.h> |
| 23 | #include <linux/workqueue.h> |
| 24 | #include <linux/bitops.h> |
| 25 | #include <asm/types.h> |
| 26 | |
| 27 | |
| 28 | enum lw_bits { |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 29 | LW_URGENT = 0, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
| 32 | static unsigned long linkwatch_flags; |
| 33 | static unsigned long linkwatch_nextevent; |
| 34 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 35 | static void linkwatch_event(struct work_struct *dummy); |
| 36 | static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
Herbert Xu | 572a103 | 2007-05-08 18:34:17 -0700 | [diff] [blame] | 38 | static struct net_device *lweventlist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | static DEFINE_SPINLOCK(lweventlist_lock); |
| 40 | |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 41 | static 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 | |
| 54 | static 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 Hemminger | 3ff50b7 | 2007-04-20 17:09:22 -0700 | [diff] [blame] | 72 | } |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 73 | |
| 74 | dev->operstate = operstate; |
| 75 | |
| 76 | write_unlock_bh(&dev_base_lock); |
| 77 | } |
| 78 | |
| 79 | |
David S. Miller | 6fa9864 | 2008-07-08 23:01:06 -0700 | [diff] [blame] | 80 | static bool linkwatch_urgent_event(struct net_device *dev) |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 81 | { |
| 82 | return netif_running(dev) && netif_carrier_ok(dev) && |
David S. Miller | 6fa9864 | 2008-07-08 23:01:06 -0700 | [diff] [blame] | 83 | qdisc_tx_changing(dev); |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | |
| 87 | static 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 | |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 98 | static void linkwatch_schedule_work(int urgent) |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 99 | { |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 100 | unsigned long delay = linkwatch_nextevent - jiffies; |
| 101 | |
| 102 | if (test_bit(LW_URGENT, &linkwatch_flags)) |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 103 | return; |
| 104 | |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 105 | /* Minimise down-time: drop delay for up event. */ |
| 106 | if (urgent) { |
| 107 | if (test_and_set_bit(LW_URGENT, &linkwatch_flags)) |
| 108 | return; |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 109 | delay = 0; |
Herbert Xu | db0ccff | 2007-05-08 23:22:43 -0700 | [diff] [blame] | 110 | } |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 111 | |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 112 | /* If we wrap around we'll delay it by at most HZ. */ |
| 113 | if (delay > HZ) |
| 114 | delay = 0; |
| 115 | |
| 116 | /* |
| 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. |
| 119 | */ |
| 120 | if (schedule_delayed_work(&linkwatch_work, delay) == !delay) |
| 121 | return; |
| 122 | |
| 123 | /* Don't bother if there is nothing urgent. */ |
| 124 | if (!test_bit(LW_URGENT, &linkwatch_flags)) |
| 125 | return; |
| 126 | |
| 127 | /* It's already running which is good enough. */ |
| 128 | if (!cancel_delayed_work(&linkwatch_work)) |
| 129 | return; |
| 130 | |
| 131 | /* Otherwise we reschedule it again for immediate exection. */ |
| 132 | schedule_delayed_work(&linkwatch_work, 0); |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | |
| 136 | static void __linkwatch_run_queue(int urgent_only) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | { |
Herbert Xu | 572a103 | 2007-05-08 18:34:17 -0700 | [diff] [blame] | 138 | struct net_device *next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 140 | /* |
| 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. |
| 146 | */ |
| 147 | if (!urgent_only) |
| 148 | linkwatch_nextevent = jiffies + HZ; |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 149 | /* Limit wrap-around effect on delay. */ |
| 150 | else if (time_after(linkwatch_nextevent, jiffies + HZ)) |
| 151 | linkwatch_nextevent = jiffies; |
| 152 | |
| 153 | clear_bit(LW_URGENT, &linkwatch_flags); |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 154 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | spin_lock_irq(&lweventlist_lock); |
Herbert Xu | 572a103 | 2007-05-08 18:34:17 -0700 | [diff] [blame] | 156 | next = lweventlist; |
| 157 | lweventlist = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | spin_unlock_irq(&lweventlist_lock); |
| 159 | |
Herbert Xu | 572a103 | 2007-05-08 18:34:17 -0700 | [diff] [blame] | 160 | while (next) { |
| 161 | struct net_device *dev = next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Herbert Xu | 572a103 | 2007-05-08 18:34:17 -0700 | [diff] [blame] | 163 | next = dev->link_watch_next; |
| 164 | |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 165 | if (urgent_only && !linkwatch_urgent_event(dev)) { |
| 166 | linkwatch_add_event(dev); |
| 167 | continue; |
| 168 | } |
| 169 | |
Herbert Xu | 572a103 | 2007-05-08 18:34:17 -0700 | [diff] [blame] | 170 | /* |
| 171 | * Make sure the above read is complete since it can be |
| 172 | * rewritten as soon as we clear the bit below. |
| 173 | */ |
| 174 | smp_mb__before_clear_bit(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | /* We are about to handle this device, |
| 177 | * so new events can be accepted |
| 178 | */ |
| 179 | clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state); |
| 180 | |
Stefan Rompf | b00055a | 2006-03-20 17:09:11 -0800 | [diff] [blame] | 181 | rfc2863_policy(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | if (dev->flags & IFF_UP) { |
David S. Miller | 6fa9864 | 2008-07-08 23:01:06 -0700 | [diff] [blame] | 183 | if (netif_carrier_ok(dev)) |
Tommy S. Christensen | cacaddf | 2005-05-03 16:18:52 -0700 | [diff] [blame] | 184 | dev_activate(dev); |
David S. Miller | 6fa9864 | 2008-07-08 23:01:06 -0700 | [diff] [blame] | 185 | else |
Tommy S. Christensen | cacaddf | 2005-05-03 16:18:52 -0700 | [diff] [blame] | 186 | dev_deactivate(dev); |
| 187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | netdev_state_change(dev); |
| 189 | } |
| 190 | |
| 191 | dev_put(dev); |
| 192 | } |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 193 | |
| 194 | if (lweventlist) |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 195 | linkwatch_schedule_work(0); |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | |
| 199 | /* Must be called with the rtnl semaphore held */ |
| 200 | void linkwatch_run_queue(void) |
| 201 | { |
| 202 | __linkwatch_run_queue(0); |
YOSHIFUJI Hideaki | 4ec93ed | 2007-02-09 23:24:36 +0900 | [diff] [blame] | 203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 206 | static void linkwatch_event(struct work_struct *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | { |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 208 | rtnl_lock(); |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 209 | __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies)); |
Stephen Hemminger | 6756ae4 | 2006-03-20 22:23:58 -0800 | [diff] [blame] | 210 | rtnl_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | |
| 214 | void linkwatch_fire_event(struct net_device *dev) |
| 215 | { |
David S. Miller | 6fa9864 | 2008-07-08 23:01:06 -0700 | [diff] [blame] | 216 | bool urgent = linkwatch_urgent_event(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 218 | if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | dev_hold(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | |
Herbert Xu | 294cc44 | 2007-05-08 18:36:28 -0700 | [diff] [blame] | 221 | linkwatch_add_event(dev); |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 222 | } else if (!urgent) |
| 223 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | |
Herbert Xu | d9568ba | 2007-05-09 00:17:30 -0700 | [diff] [blame] | 225 | linkwatch_schedule_work(urgent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | EXPORT_SYMBOL(linkwatch_fire_event); |