link_watch.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. #include <linux/module.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/if.h>
  16. #include <net/sock.h>
  17. #include <net/pkt_sched.h>
  18. #include <linux/rtnetlink.h>
  19. #include <linux/jiffies.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/slab.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/bitops.h>
  24. #include <asm/types.h>
  25. enum lw_bits {
  26. LW_URGENT = 0,
  27. };
  28. static unsigned long linkwatch_flags;
  29. static unsigned long linkwatch_nextevent;
  30. static void linkwatch_event(struct work_struct *dummy);
  31. static DECLARE_DELAYED_WORK(linkwatch_work, linkwatch_event);
  32. static struct net_device *lweventlist;
  33. static DEFINE_SPINLOCK(lweventlist_lock);
  34. static unsigned char default_operstate(const struct net_device *dev)
  35. {
  36. if (!netif_carrier_ok(dev))
  37. return (dev->ifindex != dev->iflink ?
  38. IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
  39. if (netif_dormant(dev))
  40. return IF_OPER_DORMANT;
  41. return IF_OPER_UP;
  42. }
  43. static void rfc2863_policy(struct net_device *dev)
  44. {
  45. unsigned char operstate = default_operstate(dev);
  46. if (operstate == dev->operstate)
  47. return;
  48. write_lock_bh(&dev_base_lock);
  49. switch(dev->link_mode) {
  50. case IF_LINK_MODE_DORMANT:
  51. if (operstate == IF_OPER_UP)
  52. operstate = IF_OPER_DORMANT;
  53. break;
  54. case IF_LINK_MODE_DEFAULT:
  55. default:
  56. break;
  57. }
  58. dev->operstate = operstate;
  59. write_unlock_bh(&dev_base_lock);
  60. }
  61. static int linkwatch_urgent_event(struct net_device *dev)
  62. {
  63. struct netdev_queue *txq = &dev->tx_queue;
  64. return netif_running(dev) && netif_carrier_ok(dev) &&
  65. txq->qdisc != txq->qdisc_sleeping;
  66. }
  67. static void linkwatch_add_event(struct net_device *dev)
  68. {
  69. unsigned long flags;
  70. spin_lock_irqsave(&lweventlist_lock, flags);
  71. dev->link_watch_next = lweventlist;
  72. lweventlist = dev;
  73. spin_unlock_irqrestore(&lweventlist_lock, flags);
  74. }
  75. static void linkwatch_schedule_work(int urgent)
  76. {
  77. unsigned long delay = linkwatch_nextevent - jiffies;
  78. if (test_bit(LW_URGENT, &linkwatch_flags))
  79. return;
  80. /* Minimise down-time: drop delay for up event. */
  81. if (urgent) {
  82. if (test_and_set_bit(LW_URGENT, &linkwatch_flags))
  83. return;
  84. delay = 0;
  85. }
  86. /* If we wrap around we'll delay it by at most HZ. */
  87. if (delay > HZ)
  88. delay = 0;
  89. /*
  90. * This is true if we've scheduled it immeditately or if we don't
  91. * need an immediate execution and it's already pending.
  92. */
  93. if (schedule_delayed_work(&linkwatch_work, delay) == !delay)
  94. return;
  95. /* Don't bother if there is nothing urgent. */
  96. if (!test_bit(LW_URGENT, &linkwatch_flags))
  97. return;
  98. /* It's already running which is good enough. */
  99. if (!cancel_delayed_work(&linkwatch_work))
  100. return;
  101. /* Otherwise we reschedule it again for immediate exection. */
  102. schedule_delayed_work(&linkwatch_work, 0);
  103. }
  104. static void __linkwatch_run_queue(int urgent_only)
  105. {
  106. struct net_device *next;
  107. /*
  108. * Limit the number of linkwatch events to one
  109. * per second so that a runaway driver does not
  110. * cause a storm of messages on the netlink
  111. * socket. This limit does not apply to up events
  112. * while the device qdisc is down.
  113. */
  114. if (!urgent_only)
  115. linkwatch_nextevent = jiffies + HZ;
  116. /* Limit wrap-around effect on delay. */
  117. else if (time_after(linkwatch_nextevent, jiffies + HZ))
  118. linkwatch_nextevent = jiffies;
  119. clear_bit(LW_URGENT, &linkwatch_flags);
  120. spin_lock_irq(&lweventlist_lock);
  121. next = lweventlist;
  122. lweventlist = NULL;
  123. spin_unlock_irq(&lweventlist_lock);
  124. while (next) {
  125. struct net_device *dev = next;
  126. next = dev->link_watch_next;
  127. if (urgent_only && !linkwatch_urgent_event(dev)) {
  128. linkwatch_add_event(dev);
  129. continue;
  130. }
  131. /*
  132. * Make sure the above read is complete since it can be
  133. * rewritten as soon as we clear the bit below.
  134. */
  135. smp_mb__before_clear_bit();
  136. /* We are about to handle this device,
  137. * so new events can be accepted
  138. */
  139. clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
  140. rfc2863_policy(dev);
  141. if (dev->flags & IFF_UP) {
  142. if (netif_carrier_ok(dev)) {
  143. struct netdev_queue *txq = &dev->tx_queue;
  144. WARN_ON(txq->qdisc_sleeping == &noop_qdisc);
  145. dev_activate(dev);
  146. } else
  147. dev_deactivate(dev);
  148. netdev_state_change(dev);
  149. }
  150. dev_put(dev);
  151. }
  152. if (lweventlist)
  153. linkwatch_schedule_work(0);
  154. }
  155. /* Must be called with the rtnl semaphore held */
  156. void linkwatch_run_queue(void)
  157. {
  158. __linkwatch_run_queue(0);
  159. }
  160. static void linkwatch_event(struct work_struct *dummy)
  161. {
  162. rtnl_lock();
  163. __linkwatch_run_queue(time_after(linkwatch_nextevent, jiffies));
  164. rtnl_unlock();
  165. }
  166. void linkwatch_fire_event(struct net_device *dev)
  167. {
  168. int urgent = linkwatch_urgent_event(dev);
  169. if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
  170. dev_hold(dev);
  171. linkwatch_add_event(dev);
  172. } else if (!urgent)
  173. return;
  174. linkwatch_schedule_work(urgent);
  175. }
  176. EXPORT_SYMBOL(linkwatch_fire_event);