link_watch.c 3.9 KB

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