udp_netpoll.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /****************************************************************************
  2. * net/udp/udp_netpoll.c
  3. *
  4. * Copyright (C) 2008-2009, 2011-2015, 2018 Gregory Nutt. All rights
  5. * reserved.
  6. * Author: Gregory Nutt <gnutt@nuttx.org>
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. * 3. Neither the name NuttX nor the names of its contributors may be
  19. * used to endorse or promote products derived from this software
  20. * without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  24. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  25. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  26. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  27. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  28. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  29. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  32. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. ****************************************************************************/
  36. /****************************************************************************
  37. * Included Files
  38. ****************************************************************************/
  39. #include <nuttx/config.h>
  40. #include <stdint.h>
  41. #include <poll.h>
  42. #include <debug.h>
  43. #include <nuttx/kmalloc.h>
  44. #include <nuttx/wqueue.h>
  45. #include <nuttx/mm/iob.h>
  46. #include <nuttx/net/net.h>
  47. #include "devif/devif.h"
  48. #include "netdev/netdev.h"
  49. #include "socket/socket.h"
  50. #include "udp/udp.h"
  51. #ifdef HAVE_UDP_POLL
  52. /****************************************************************************
  53. * Private Types
  54. ****************************************************************************/
  55. /* This is an allocated container that holds the poll-related information */
  56. struct udp_poll_s
  57. {
  58. FAR struct socket *psock; /* Needed to handle loss of connection */
  59. FAR struct net_driver_s *dev; /* Needed to free the callback structure */
  60. struct pollfd *fds; /* Needed to handle poll events */
  61. FAR struct devif_callback_s *cb; /* Needed to teardown the poll */
  62. #if defined(CONFIG_NET_UDP_WRITE_BUFFERS) && defined(CONFIG_IOB_NOTIFIER)
  63. int16_t key; /* Needed to cancel pending notification */
  64. #endif
  65. };
  66. /****************************************************************************
  67. * Private Functions
  68. ****************************************************************************/
  69. /****************************************************************************
  70. * Name: udp_poll_eventhandler
  71. *
  72. * Description:
  73. * This function is called to perform the actual UDP receive operation
  74. * via the device interface layer.
  75. *
  76. * Input Parameters:
  77. * dev The structure of the network driver that caused the event
  78. * conn The connection structure associated with the socket
  79. * flags Set of events describing why the callback was invoked
  80. *
  81. * Returned Value:
  82. * None
  83. *
  84. * Assumptions:
  85. * This function must be called with the network locked.
  86. *
  87. ****************************************************************************/
  88. static uint16_t udp_poll_eventhandler(FAR struct net_driver_s *dev,
  89. FAR void *conn,
  90. FAR void *pvpriv, uint16_t flags)
  91. {
  92. FAR struct udp_poll_s *info = (FAR struct udp_poll_s *)pvpriv;
  93. ninfo("flags: %04x\n", flags);
  94. DEBUGASSERT(!info || (info->psock && info->fds));
  95. /* 'priv' might be null in some race conditions (?) */
  96. if (info)
  97. {
  98. pollevent_t eventset = 0;
  99. /* Check for data or connection availability events. */
  100. if ((flags & UDP_NEWDATA) != 0)
  101. {
  102. eventset |= (POLLIN & info->fds->events);
  103. }
  104. /* A poll is a sign that we are free to send data.
  105. * REVISIT: This is bogus: If CONFIG_UDP_WRITE_BUFFERS=y then
  106. * we never have to wait to send; otherwise, we always have to
  107. * wait to send. Receiving a poll is irrelevant.
  108. */
  109. if ((flags & UDP_POLL) != 0)
  110. {
  111. eventset |= (POLLOUT & info->fds->events);
  112. }
  113. /* Check for loss of connection events. */
  114. if ((flags & NETDEV_DOWN) != 0)
  115. {
  116. eventset |= ((POLLHUP | POLLERR) & info->fds->events);
  117. }
  118. /* Awaken the caller of poll() is requested event occurred. */
  119. if (eventset)
  120. {
  121. info->fds->revents |= eventset;
  122. nxsem_post(info->fds->sem);
  123. }
  124. }
  125. return flags;
  126. }
  127. /****************************************************************************
  128. * Name: udp_iob_work
  129. *
  130. * Description:
  131. * Work thread callback function execute when an IOB because available.
  132. *
  133. * Input Parameters:
  134. * psock - Socket state structure
  135. *
  136. * Returned Value:
  137. * None
  138. *
  139. ****************************************************************************/
  140. #if defined(CONFIG_NET_UDP_WRITE_BUFFERS) && defined(CONFIG_IOB_NOTIFIER)
  141. static inline void udp_iob_work(FAR void *arg)
  142. {
  143. FAR struct work_notifier_entry_s *entry;
  144. FAR struct work_notifier_s *ninfo;
  145. FAR struct udp_poll_s *pinfo;
  146. FAR struct socket *psock;
  147. FAR struct pollfd *fds;
  148. entry = (FAR struct work_notifier_entry_s *)arg;
  149. DEBUGASSERT(entry != NULL);
  150. ninfo = &entry->info;
  151. DEBUGASSERT(ninfo->arg != NULL);
  152. pinfo = (FAR struct udp_poll_s *)ninfo->arg;
  153. DEBUGASSERT(pinfo->psock != NULL && pinfo->fds != NULL);
  154. psock = pinfo->psock;
  155. fds = pinfo->fds;
  156. /* Handle a race condition. Check if we have already posted the POLLOUT
  157. * event. If so, don't do it again.
  158. */
  159. if ((fds->events && POLLWRNORM) == 0 ||
  160. (fds->revents && POLLWRNORM) != 0)
  161. {
  162. /* Check if we are now able to send */
  163. if (psock_udp_cansend(psock) >= 0)
  164. {
  165. /* Yes.. then signal the poll logic */
  166. fds->revents |= POLLWRNORM;
  167. nxsem_post(fds->sem);
  168. }
  169. else
  170. {
  171. /* No.. ask for the IOB free notification again */
  172. pinfo->key = iob_notifier_setup(LPWORK, udp_iob_work, pinfo);
  173. }
  174. }
  175. /* Protocol for the use of the IOB notifier is that we free the argument
  176. * after the notification has been processed.
  177. */
  178. kmm_free(arg);
  179. }
  180. #endif
  181. /****************************************************************************
  182. * Name: udp_poll_txnotify
  183. *
  184. * Description:
  185. * Notify the appropriate device driver that we are have data ready to
  186. * be sent (UDP)
  187. *
  188. * Input Parameters:
  189. * psock - Socket state structure
  190. *
  191. * Returned Value:
  192. * None
  193. *
  194. ****************************************************************************/
  195. #if !defined(CONFIG_NET_UDP_WRITE_BUFFERS) || !defined(CONFIG_IOB_NOTIFIER)
  196. static inline void udp_poll_txnotify(FAR struct socket *psock)
  197. {
  198. FAR struct udp_conn_s *conn = psock->s_conn;
  199. #ifdef CONFIG_NET_IPv4
  200. #ifdef CONFIG_NET_IPv6
  201. /* If both IPv4 and IPv6 support are enabled, then we will need to select
  202. * the device driver using the appropriate IP domain.
  203. */
  204. if (psock->s_domain == PF_INET)
  205. #endif
  206. {
  207. /* Notify the device driver that send data is available */
  208. netdev_ipv4_txnotify(conn->u.ipv4.laddr, conn->u.ipv4.raddr);
  209. }
  210. #endif /* CONFIG_NET_IPv4 */
  211. #ifdef CONFIG_NET_IPv6
  212. #ifdef CONFIG_NET_IPv4
  213. else /* if (psock->s_domain == PF_INET6) */
  214. #endif /* CONFIG_NET_IPv4 */
  215. {
  216. /* Notify the device driver that send data is available */
  217. DEBUGASSERT(psock->s_domain == PF_INET6);
  218. netdev_ipv6_txnotify(conn->u.ipv6.laddr, conn->u.ipv6.raddr);
  219. }
  220. #endif /* CONFIG_NET_IPv6 */
  221. }
  222. #endif
  223. /****************************************************************************
  224. * Public Functions
  225. ****************************************************************************/
  226. /****************************************************************************
  227. * Name: udp_pollsetup
  228. *
  229. * Description:
  230. * Setup to monitor events on one UDP/IP socket
  231. *
  232. * Input Parameters:
  233. * psock - The UDP/IP socket of interest
  234. * fds - The structure describing the events to be monitored, OR NULL if
  235. * this is a request to stop monitoring events.
  236. *
  237. * Returned Value:
  238. * 0: Success; Negated errno on failure
  239. *
  240. ****************************************************************************/
  241. int udp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds)
  242. {
  243. FAR struct udp_conn_s *conn = psock->s_conn;
  244. FAR struct udp_poll_s *info;
  245. FAR struct devif_callback_s *cb;
  246. int ret;
  247. /* Sanity check */
  248. #ifdef CONFIG_DEBUG_FEATURES
  249. if (conn == NULL || fds == NULL)
  250. {
  251. return -EINVAL;
  252. }
  253. #endif
  254. /* Allocate a container to hold the poll information */
  255. info = (FAR struct udp_poll_s *)kmm_malloc(sizeof(struct udp_poll_s));
  256. if (!info)
  257. {
  258. return -ENOMEM;
  259. }
  260. /* Some of the following must be atomic */
  261. net_lock();
  262. /* Get the device that will provide the provide the NETDEV_DOWN event.
  263. * NOTE: in the event that the local socket is bound to INADDR_ANY, the
  264. * dev value will be zero and there will be no NETDEV_DOWN notifications.
  265. */
  266. info->dev = udp_find_laddr_device(conn);
  267. /* Allocate a UDP callback structure */
  268. cb = udp_callback_alloc(info->dev, conn);
  269. if (cb == NULL)
  270. {
  271. ret = -EBUSY;
  272. goto errout_with_lock;
  273. }
  274. /* Initialize the poll info container */
  275. info->psock = psock;
  276. info->fds = fds;
  277. info->cb = cb;
  278. #if defined(CONFIG_NET_UDP_WRITE_BUFFERS) && defined(CONFIG_IOB_NOTIFIER)
  279. info->key = 0;
  280. #endif
  281. /* Initialize the callback structure. Save the reference to the info
  282. * structure as callback private data so that it will be available during
  283. * callback processing.
  284. */
  285. cb->flags = 0;
  286. cb->priv = (FAR void *)info;
  287. cb->event = udp_poll_eventhandler;
  288. if ((info->fds->events & POLLOUT) != 0)
  289. {
  290. cb->flags |= UDP_POLL;
  291. }
  292. if ((info->fds->events & POLLIN) != 0)
  293. {
  294. cb->flags |= UDP_NEWDATA;
  295. }
  296. if ((info->fds->events & (POLLHUP | POLLERR)) != 0)
  297. {
  298. cb->flags |= NETDEV_DOWN;
  299. }
  300. /* Save the reference in the poll info structure as fds private as well
  301. * for use during poll teardown as well.
  302. */
  303. fds->priv = (FAR void *)info;
  304. /* Check for read data availability now */
  305. if (!IOB_QEMPTY(&conn->readahead))
  306. {
  307. /* Normal data may be read without blocking. */
  308. fds->revents |= (POLLRDNORM & fds->events);
  309. }
  310. if (psock_udp_cansend(psock) >= 0)
  311. {
  312. /* Normal data may be sent without blocking (at least one byte). */
  313. fds->revents |= (POLLWRNORM & fds->events);
  314. }
  315. /* Check if any requested events are already in effect */
  316. if (fds->revents != 0)
  317. {
  318. /* Yes.. then signal the poll logic */
  319. nxsem_post(fds->sem);
  320. }
  321. #if defined(CONFIG_NET_UDP_WRITE_BUFFERS) && defined(CONFIG_IOB_NOTIFIER)
  322. /* If (1) revents == 0, (2) write buffering is enabled, and (3) the
  323. * POLLOUT event is needed, then setup to receive a notification an IOB
  324. * is freed.
  325. */
  326. else if ((fds->events & POLLOUT) != 0)
  327. {
  328. /* Ask for the IOB free notification */
  329. info->key = iob_notifier_setup(LPWORK, udp_iob_work, info);
  330. }
  331. #else
  332. /* If (1) the socket is in a bound state via bind() or via the
  333. * UDP_BINDTODEVICE socket options, (2) revents == 0, (3) write buffering
  334. * is not enabled (determined by a configuration setting), and (3) the
  335. * POLLOUT event is needed then request an immediate Tx poll from the
  336. * device associated with the binding.
  337. */
  338. else if ((fds->events & POLLOUT) != 0)
  339. {
  340. /* Check if the socket has been bound to a local address (might be
  341. * INADDR_ANY or the IPv6 unspecified address! In that case the
  342. * notification will fail)
  343. */
  344. if (_SS_ISBOUND(psock->s_flags))
  345. {
  346. udp_poll_txnotify(psock);
  347. }
  348. #ifdef CONFIG_NET_UDP_BINDTODEVICE
  349. /* Check if the socket has been bound to a device interface index via
  350. * the UDP_BINDTODEVICE socket option.
  351. */
  352. else if (conn->boundto > 0)
  353. {
  354. /* Yes, find the device associated with the interface index */
  355. FAR struct net_driver_s *dev = netdev_findbyindex(conn->boundto);
  356. if (dev != NULL)
  357. {
  358. /* And request a poll from the device */
  359. netdev_txnotify_dev(dev);
  360. }
  361. }
  362. #endif
  363. }
  364. #endif
  365. net_unlock();
  366. return OK;
  367. errout_with_lock:
  368. kmm_free(info);
  369. net_unlock();
  370. return ret;
  371. }
  372. /****************************************************************************
  373. * Name: udp_pollteardown
  374. *
  375. * Description:
  376. * Teardown monitoring of events on an UDP/IP socket
  377. *
  378. * Input Parameters:
  379. * psock - The UDP socket of interest
  380. * fds - The structure describing the events to be monitored, OR NULL if
  381. * this is a request to stop monitoring events.
  382. *
  383. * Returned Value:
  384. * 0: Success; Negated errno on failure
  385. *
  386. ****************************************************************************/
  387. int udp_pollteardown(FAR struct socket *psock, FAR struct pollfd *fds)
  388. {
  389. FAR struct udp_conn_s *conn = psock->s_conn;
  390. FAR struct udp_poll_s *info;
  391. /* Sanity check */
  392. #ifdef CONFIG_DEBUG_FEATURES
  393. if (!conn || !fds->priv)
  394. {
  395. return -EINVAL;
  396. }
  397. #endif
  398. /* Recover the socket descriptor poll state info from the poll structure */
  399. info = (FAR struct udp_poll_s *)fds->priv;
  400. DEBUGASSERT(info != NULL && info->fds != NULL && info->cb != NULL);
  401. if (info != NULL)
  402. {
  403. #if defined(CONFIG_NET_UDP_WRITE_BUFFERS) && defined(CONFIG_IOB_NOTIFIER)
  404. /* Cancel any pending IOB free notification */
  405. if (info->key > 0)
  406. {
  407. /* Ask for the IOB free notification */
  408. iob_notifier_teardown(info->key);
  409. }
  410. #endif
  411. /* Release the callback */
  412. net_lock();
  413. udp_callback_free(info->dev, conn, info->cb);
  414. net_unlock();
  415. /* Release the poll/select data slot */
  416. info->fds->priv = NULL;
  417. /* Then free the poll info container */
  418. kmm_free(info);
  419. }
  420. return OK;
  421. }
  422. #endif /* !HAVE_UDP_POLL */