link_watch.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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/config.h>
  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/list.h>
  23. #include <linux/slab.h>
  24. #include <linux/workqueue.h>
  25. #include <linux/bitops.h>
  26. #include <asm/types.h>
  27. enum lw_bits {
  28. LW_RUNNING = 0,
  29. LW_SE_USED
  30. };
  31. static unsigned long linkwatch_flags;
  32. static unsigned long linkwatch_nextevent;
  33. static void linkwatch_event(void *dummy);
  34. static DECLARE_WORK(linkwatch_work, linkwatch_event, NULL);
  35. static LIST_HEAD(lweventlist);
  36. static DEFINE_SPINLOCK(lweventlist_lock);
  37. struct lw_event {
  38. struct list_head list;
  39. struct net_device *dev;
  40. };
  41. /* Avoid kmalloc() for most systems */
  42. static struct lw_event singleevent;
  43. static unsigned char default_operstate(const struct net_device *dev)
  44. {
  45. if (!netif_carrier_ok(dev))
  46. return (dev->ifindex != dev->iflink ?
  47. IF_OPER_LOWERLAYERDOWN : IF_OPER_DOWN);
  48. if (netif_dormant(dev))
  49. return IF_OPER_DORMANT;
  50. return IF_OPER_UP;
  51. }
  52. static void rfc2863_policy(struct net_device *dev)
  53. {
  54. unsigned char operstate = default_operstate(dev);
  55. if (operstate == dev->operstate)
  56. return;
  57. write_lock_bh(&dev_base_lock);
  58. switch(dev->link_mode) {
  59. case IF_LINK_MODE_DORMANT:
  60. if (operstate == IF_OPER_UP)
  61. operstate = IF_OPER_DORMANT;
  62. break;
  63. case IF_LINK_MODE_DEFAULT:
  64. default:
  65. break;
  66. };
  67. dev->operstate = operstate;
  68. write_unlock_bh(&dev_base_lock);
  69. }
  70. /* Must be called with the rtnl semaphore held */
  71. void linkwatch_run_queue(void)
  72. {
  73. struct list_head head, *n, *next;
  74. spin_lock_irq(&lweventlist_lock);
  75. list_replace_init(&lweventlist, &head);
  76. spin_unlock_irq(&lweventlist_lock);
  77. list_for_each_safe(n, next, &head) {
  78. struct lw_event *event = list_entry(n, struct lw_event, list);
  79. struct net_device *dev = event->dev;
  80. if (event == &singleevent) {
  81. clear_bit(LW_SE_USED, &linkwatch_flags);
  82. } else {
  83. kfree(event);
  84. }
  85. /* We are about to handle this device,
  86. * so new events can be accepted
  87. */
  88. clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
  89. rfc2863_policy(dev);
  90. if (dev->flags & IFF_UP) {
  91. if (netif_carrier_ok(dev)) {
  92. WARN_ON(dev->qdisc_sleeping == &noop_qdisc);
  93. dev_activate(dev);
  94. } else
  95. dev_deactivate(dev);
  96. netdev_state_change(dev);
  97. }
  98. dev_put(dev);
  99. }
  100. }
  101. static void linkwatch_event(void *dummy)
  102. {
  103. /* Limit the number of linkwatch events to one
  104. * per second so that a runaway driver does not
  105. * cause a storm of messages on the netlink
  106. * socket
  107. */
  108. linkwatch_nextevent = jiffies + HZ;
  109. clear_bit(LW_RUNNING, &linkwatch_flags);
  110. rtnl_lock();
  111. linkwatch_run_queue();
  112. rtnl_unlock();
  113. }
  114. void linkwatch_fire_event(struct net_device *dev)
  115. {
  116. if (!test_and_set_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state)) {
  117. unsigned long flags;
  118. struct lw_event *event;
  119. if (test_and_set_bit(LW_SE_USED, &linkwatch_flags)) {
  120. event = kmalloc(sizeof(struct lw_event), GFP_ATOMIC);
  121. if (unlikely(event == NULL)) {
  122. clear_bit(__LINK_STATE_LINKWATCH_PENDING, &dev->state);
  123. return;
  124. }
  125. } else {
  126. event = &singleevent;
  127. }
  128. dev_hold(dev);
  129. event->dev = dev;
  130. spin_lock_irqsave(&lweventlist_lock, flags);
  131. list_add_tail(&event->list, &lweventlist);
  132. spin_unlock_irqrestore(&lweventlist_lock, flags);
  133. if (!test_and_set_bit(LW_RUNNING, &linkwatch_flags)) {
  134. unsigned long delay = linkwatch_nextevent - jiffies;
  135. /* If we wrap around we'll delay it by at most HZ. */
  136. if (!delay || delay > HZ)
  137. schedule_work(&linkwatch_work);
  138. else
  139. schedule_delayed_work(&linkwatch_work, delay);
  140. }
  141. }
  142. }
  143. EXPORT_SYMBOL(linkwatch_fire_event);