arp_notify.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /****************************************************************************
  2. * net/arp/arp_notify.c
  3. *
  4. * Copyright (C) 2014, 2016 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <time.h>
  40. #include <semaphore.h>
  41. #include <errno.h>
  42. #include <assert.h>
  43. #include <debug.h>
  44. #include <netinet/in.h>
  45. #include <nuttx/irq.h>
  46. #include <nuttx/semaphore.h>
  47. #include <nuttx/net/net.h>
  48. #include "arp/arp.h"
  49. #ifdef CONFIG_NET_ARP_SEND
  50. /****************************************************************************
  51. * Private Data
  52. ****************************************************************************/
  53. /* List of tasks waiting for ARP events */
  54. static FAR struct arp_notify_s *g_arp_waiters;
  55. /****************************************************************************
  56. * Public Functions
  57. ****************************************************************************/
  58. /****************************************************************************
  59. * Name: arp_wait_setup
  60. *
  61. * Description:
  62. * Called BEFORE an ARP request is sent. This function sets up the ARP
  63. * response timeout before the ARP request is sent so that there is
  64. * no race condition when arp_wait() is called.
  65. *
  66. * Assumptions:
  67. * This function is called from ARP send and executes in the normal
  68. * tasking environment.
  69. *
  70. ****************************************************************************/
  71. void arp_wait_setup(in_addr_t ipaddr, FAR struct arp_notify_s *notify)
  72. {
  73. irqstate_t flags;
  74. /* Initialize the wait structure */
  75. notify->nt_ipaddr = ipaddr;
  76. notify->nt_result = -ETIMEDOUT;
  77. /* This semaphore is used for signaling and, hence, should not have
  78. * priority inheritance enabled.
  79. */
  80. (void)nxsem_init(&notify->nt_sem, 0, 0);
  81. nxsem_setprotocol(&notify->nt_sem, SEM_PRIO_NONE);
  82. /* Add the wait structure to the list with interrupts disabled */
  83. flags = enter_critical_section();
  84. notify->nt_flink = g_arp_waiters;
  85. g_arp_waiters = notify;
  86. leave_critical_section(flags);
  87. }
  88. /****************************************************************************
  89. * Name: arp_wait_cancel
  90. *
  91. * Description:
  92. * Cancel any wait set after arp_wait_setup is called but before arp_wait()
  93. * is called (arp_wait() will automatically cancel the wait).
  94. *
  95. * Assumptions:
  96. * This function may execute with interrupts disabled when called from
  97. * arp_wait().
  98. *
  99. ****************************************************************************/
  100. int arp_wait_cancel(FAR struct arp_notify_s *notify)
  101. {
  102. FAR struct arp_notify_s *curr;
  103. FAR struct arp_notify_s *prev;
  104. irqstate_t flags;
  105. int ret = -ENOENT;
  106. /* Remove our wait structure from the list (we may no longer be at the
  107. * head of the list).
  108. */
  109. flags = enter_critical_section();
  110. for (prev = NULL, curr = g_arp_waiters;
  111. curr && curr != notify;
  112. prev = curr, curr = curr->nt_flink);
  113. DEBUGASSERT(curr && curr == notify);
  114. if (curr)
  115. {
  116. if (prev)
  117. {
  118. prev->nt_flink = notify->nt_flink;
  119. }
  120. else
  121. {
  122. g_arp_waiters = notify->nt_flink;
  123. }
  124. ret = OK;
  125. }
  126. leave_critical_section(flags);
  127. (void)nxsem_destroy(&notify->nt_sem);
  128. return ret;
  129. }
  130. /****************************************************************************
  131. * Name: arp_wait
  132. *
  133. * Description:
  134. * Called each time that a ARP request is sent. This function will sleep
  135. * until either: (1) the matching ARP response is received, or (2) a
  136. * timeout occurs.
  137. *
  138. * Assumptions:
  139. * This function is called from ARP send must execute with the network
  140. * locked.
  141. *
  142. ****************************************************************************/
  143. int arp_wait(FAR struct arp_notify_s *notify, FAR struct timespec *timeout)
  144. {
  145. struct timespec abstime;
  146. irqstate_t flags;
  147. int errcode;
  148. int ret;
  149. /* And wait for the ARP response (or a timeout). Interrupts will be re-
  150. * enabled while we wait.
  151. */
  152. flags = enter_critical_section();
  153. DEBUGVERIFY(clock_gettime(CLOCK_REALTIME, &abstime));
  154. abstime.tv_sec += timeout->tv_sec;
  155. abstime.tv_nsec += timeout->tv_nsec;
  156. if (abstime.tv_nsec >= 1000000000)
  157. {
  158. abstime.tv_sec++;
  159. abstime.tv_nsec -= 1000000000;
  160. }
  161. /* Wait to get either the correct response or a timeout. */
  162. do
  163. {
  164. /* The only errors that we expect would be if the abstime timeout
  165. * expires or if the wait were interrupted by a signal.
  166. */
  167. ret = net_timedwait(&notify->nt_sem, &abstime);
  168. errcode = ((ret < 0) ? -ret : 0);
  169. }
  170. while (ret < 0 && errcode == EINTR);
  171. /* Then get the real result of the transfer */
  172. ret = notify->nt_result;
  173. /* Remove our wait structure from the list (we may no longer be at the
  174. * head of the list).
  175. */
  176. (void)arp_wait_cancel(notify);
  177. /* Re-enable interrupts and return the result of the wait */
  178. leave_critical_section(flags);
  179. return ret;
  180. }
  181. /****************************************************************************
  182. * Name: arp_notify
  183. *
  184. * Description:
  185. * Called each time that a ARP response is received in order to wake-up
  186. * any threads that may be waiting for this particular ARP response.
  187. *
  188. * Assumptions:
  189. * This function is called from the MAC device driver indirectly through
  190. * arp_arpin() will execute with the network locked.
  191. *
  192. ****************************************************************************/
  193. void arp_notify(in_addr_t ipaddr)
  194. {
  195. FAR struct arp_notify_s *curr;
  196. /* Find an entry with the matching IP address in the list of waiters */
  197. for (curr = g_arp_waiters; curr; curr = curr->nt_flink)
  198. {
  199. /* Does this entry match? If the result is okay, then we have
  200. * already notified this waiter and it has not yet taken the
  201. * entry from the list.
  202. */
  203. if (curr->nt_result != OK && curr->nt_ipaddr == ipaddr)
  204. {
  205. /* Yes.. Signal the waiting, returning success */
  206. curr->nt_result = OK;
  207. nxsem_post(&curr->nt_sem);
  208. break;
  209. }
  210. }
  211. }
  212. #endif /* CONFIG_NET_ARP_SEND */