diff options
author | Gregory Nutt <gnutt@nuttx.org> | 2015-01-30 06:08:26 -0600 |
---|---|---|
committer | Gregory Nutt <gnutt@nuttx.org> | 2015-01-30 06:08:26 -0600 |
commit | 2a2af92cceae80668ae98106d1ea55c080d483b9 (patch) | |
tree | 3b29c3d377ca869be5c6c9492e9a7986611e190d | |
parent | e2472f9595c3117f3dd01d16721e40d7b3b5ef09 (diff) | |
download | px4-nuttx-2a2af92cceae80668ae98106d1ea55c080d483b9.tar.gz px4-nuttx-2a2af92cceae80668ae98106d1ea55c080d483b9.tar.bz2 px4-nuttx-2a2af92cceae80668ae98106d1ea55c080d483b9.zip |
Networking: Add UDP read-ahead support and support for poll/select on UDP sockets. From Macs N.
-rw-r--r-- | nuttx/net/socket/net_poll.c | 340 | ||||
-rw-r--r-- | nuttx/net/socket/recvfrom.c | 153 | ||||
-rw-r--r-- | nuttx/net/tcp/tcp_send_buffered.c | 2 | ||||
-rw-r--r-- | nuttx/net/udp/Kconfig | 5 | ||||
-rw-r--r-- | nuttx/net/udp/udp.h | 14 | ||||
-rw-r--r-- | nuttx/net/udp/udp_callback.c | 208 |
6 files changed, 689 insertions, 33 deletions
diff --git a/nuttx/net/socket/net_poll.c b/nuttx/net/socket/net_poll.c index 37f990bd2..d2cacf13a 100644 --- a/nuttx/net/socket/net_poll.c +++ b/nuttx/net/socket/net_poll.c @@ -1,7 +1,7 @@ /**************************************************************************** * net/socket/net_poll.c * - * Copyright (C) 2008-2009, 2011-2014 Gregory Nutt. All rights reserved. + * Copyright (C) 2008-2009, 2011-2015 Gregory Nutt. All rights reserved. * Author: Gregory Nutt <gnutt@nuttx.org> * * Redistribution and use in source and binary forms, with or without @@ -56,6 +56,7 @@ #include <devif/devif.h> #include "tcp/tcp.h" +#include "udp/udp.h" #include "socket/socket.h" /**************************************************************************** @@ -91,7 +92,7 @@ struct net_poll_s ****************************************************************************/ /**************************************************************************** - * Function: poll_interrupt + * Function: tcp_poll_interrupt * * Description: * This function is called from the interrupt level to perform the actual @@ -111,8 +112,8 @@ struct net_poll_s ****************************************************************************/ #ifdef HAVE_NETPOLL -static uint16_t poll_interrupt(FAR struct net_driver_s *dev, FAR void *conn, - FAR void *pvpriv, uint16_t flags) +static uint16_t tcp_poll_interrupt(FAR struct net_driver_s *dev, FAR void *conn, + FAR void *pvpriv, uint16_t flags) { FAR struct net_poll_s *info = (FAR struct net_poll_s *)pvpriv; @@ -164,13 +165,76 @@ static uint16_t poll_interrupt(FAR struct net_driver_s *dev, FAR void *conn, #endif /* HAVE_NETPOLL */ /**************************************************************************** - * Function: net_pollsetup + * Function: udp_poll_interrupt + * + * Description: + * This function is called from the interrupt level to perform the actual + * UDP receive operation via by the device interface layer. + * + * Parameters: + * dev The structure of the network driver that caused the interrupt + * conn The connection structure associated with the socket + * flags Set of events describing why the callback was invoked + * + * Returned Value: + * None + * + * Assumptions: + * Running at the interrupt level + * + ****************************************************************************/ + +#ifdef HAVE_NETPOLL +static uint16_t udp_poll_interrupt(FAR struct net_driver_s *dev, FAR void *conn, + FAR void *pvpriv, uint16_t flags) +{ + FAR struct net_poll_s *info = (FAR struct net_poll_s *)pvpriv; + + nllvdbg("flags: %04x\n", flags); + + DEBUGASSERT(!info || (info->psock && info->fds)); + + /* 'priv' might be null in some race conditions (?) */ + + if (info) + { + pollevent_t eventset = 0; + + /* Check for data or connection availability events. */ + + if ((flags & (UDP_NEWDATA)) != 0) + { + eventset |= (POLLIN & info->fds->events); + } + + /* A poll is a sign that we are free to send data. */ + + if ((flags & UDP_POLL) != 0) + { + eventset |= (POLLOUT & info->fds->events); + } + + /* Awaken the caller of poll() is requested event occurred. */ + + if (eventset) + { + info->fds->revents |= eventset; + sem_post(info->fds->sem); + } + } + + return flags; +} +#endif /* HAVE_NETPOLL */ + +/**************************************************************************** + * Function: tcp_pollsetup * * Description: * Setup to monitor events on one TCP/IP socket * * Input Parameters: - * conn - The TCP/IP connection of interest + * psock - The TCP/IP socket of interest * fds - The structure describing the events to be monitored, OR NULL if * this is a request to stop monitoring events. * @@ -180,7 +244,7 @@ static uint16_t poll_interrupt(FAR struct net_driver_s *dev, FAR void *conn, ****************************************************************************/ #ifdef HAVE_NETPOLL -static inline int net_pollsetup(FAR struct socket *psock, +static inline int tcp_pollsetup(FAR struct socket *psock, FAR struct pollfd *fds) { FAR struct tcp_conn_s *conn = psock->s_conn; @@ -233,7 +297,7 @@ static inline int net_pollsetup(FAR struct socket *psock, cb->flags = (TCP_NEWDATA | TCP_BACKLOG | TCP_POLL | TCP_CLOSE | TCP_ABORT | TCP_TIMEDOUT); cb->priv = (FAR void *)info; - cb->event = poll_interrupt; + cb->event = tcp_poll_interrupt; /* Save the reference in the poll info structure as fds private as well * for use durring poll teardown as well. @@ -323,16 +387,169 @@ errout_with_lock: net_unlock(flags); return ret; } + +/**************************************************************************** + * Function: udp_pollsetup + * + * Description: + * Setup to monitor events on one UDP/IP socket + * + * Input Parameters: + * psock - The UDP/IP socket of interest + * fds - The structure describing the events to be monitored, OR NULL if + * this is a request to stop monitoring events. + * + * Returned Value: + * 0: Success; Negated errno on failure + * + ****************************************************************************/ + +static inline int udp_pollsetup(FAR struct socket *psock, + FAR struct pollfd *fds) +{ + FAR struct udp_conn_s *conn = psock->s_conn; + FAR struct net_poll_s *info; + FAR struct devif_callback_s *cb; + net_lock_t flags; + int ret; + + /* Sanity check */ + +#ifdef CONFIG_DEBUG + if (!conn || !fds) + { + return -EINVAL; + } +#endif + + /* Allocate a container to hold the poll information */ + + info = (FAR struct net_poll_s *)kmm_malloc(sizeof(struct net_poll_s)); + if (!info) + { + return -ENOMEM; + } + + /* Some of the following must be atomic */ + + flags = net_lock(); + + /* Setup the UDP remote connection */ + + ret = udp_connect(conn, NULL); + if (ret) + { + goto errout_with_lock; + } + + /* Allocate a TCP/IP callback structure */ + + cb = udp_callback_alloc(conn); + if (!cb) + { + ret = -EBUSY; + goto errout_with_lock; + } + + /* Initialize the poll info container */ + + info->psock = psock; + info->fds = fds; + info->cb = cb; + + /* Initialize the callback structure. Save the reference to the info + * structure as callback private data so that it will be available during + * callback processing. + */ + + cb->flags = (0); + cb->priv = (FAR void *)info; + cb->event = udp_poll_interrupt; + + if (info->fds->events & POLLOUT) + cb->flags |= UDP_POLL; + if (info->fds->events & POLLIN) + cb->flags |= UDP_NEWDATA; + + /* Save the reference in the poll info structure as fds private as well + * for use durring poll teardown as well. + */ + + fds->priv = (FAR void *)info; + + /* Check for read data availability now */ + + if (!IOB_QEMPTY(&conn->readahead)) + { + /* Normal data may be read without blocking. */ + + fds->revents |= (POLLRDNORM & fds->events); + } + + /* Check if any requested events are already in effect */ + + if (fds->revents != 0) + { + /* Yes.. then signal the poll logic */ + sem_post(fds->sem); + } + + net_unlock(flags); + return OK; + +errout_with_lock: + kmm_free(info); + net_unlock(flags); + return ret; +} + +/**************************************************************************** + * Function: net_pollsetup + * + * Description: + * Setup to monitor events on one socket + * + * Input Parameters: + * psock - The socket of interest + * fds - The structure describing the events to be monitored, OR NULL if + * this is a request to stop monitoring events. + * + * Returned Value: + * 0: Success; Negated errno on failure + * + ****************************************************************************/ + +static inline int net_pollsetup(FAR struct socket *psock, + FAR struct pollfd *fds) +{ +#ifdef CONFIG_NET_TCP + if (psock->s_type == SOCK_STREAM) + { + return tcp_pollsetup(psock, fds); + } +#endif + +#ifdef CONFIG_NET_UDP + if (psock->s_type != SOCK_STREAM) + { + return udp_pollsetup(psock, fds); + } +#endif + + return -ENOSYS; +} #endif /* HAVE_NETPOLL */ /**************************************************************************** - * Function: net_pollteardown + * Function: tcp_pollteardown * * Description: * Teardown monitoring of events on an TCP/IP socket * * Input Parameters: - * conn - The TCP/IP connection of interest + * psock - The TCP/IP socket of interest + * fds - The structure describing the events to be monitored, OR NULL if + * this is a request to stop monitoring events. * * Returned Value: * 0: Success; Negated errno on failure @@ -340,7 +557,7 @@ errout_with_lock: ****************************************************************************/ #ifdef HAVE_NETPOLL -static inline int net_pollteardown(FAR struct socket *psock, +static inline int tcp_pollteardown(FAR struct socket *psock, FAR struct pollfd *fds) { FAR struct tcp_conn_s *conn = psock->s_conn; @@ -379,6 +596,98 @@ static inline int net_pollteardown(FAR struct socket *psock, return OK; } + +/**************************************************************************** + * Function: udp_pollteardown + * + * Description: + * Teardown monitoring of events on an UDP/IP socket + * + * Input Parameters: + * psock - The TCP/IP socket of interest + * fds - The structure describing the events to be monitored, OR NULL if + * this is a request to stop monitoring events. + * + * Returned Value: + * 0: Success; Negated errno on failure + * + ****************************************************************************/ + +static inline int udp_pollteardown(FAR struct socket *psock, + FAR struct pollfd *fds) +{ + FAR struct udp_conn_s *conn = psock->s_conn; + FAR struct net_poll_s *info; + net_lock_t flags; + + /* Sanity check */ + +#ifdef CONFIG_DEBUG + if (!conn || !fds->priv) + { + return -EINVAL; + } +#endif + + /* Recover the socket descriptor poll state info from the poll structure */ + + info = (FAR struct net_poll_s *)fds->priv; + DEBUGASSERT(info && info->fds && info->cb); + if (info) + { + /* Release the callback */ + + flags = net_lock(); + udp_callback_free(conn, info->cb); + net_unlock(flags); + + /* Release the poll/select data slot */ + + info->fds->priv = NULL; + + /* Then free the poll info container */ + + kmm_free(info); + } + + return OK; +} + +/**************************************************************************** + * Function: net_pollteardown + * + * Description: + * Teardown monitoring of events on an socket + * + * Input Parameters: + * psock - The TCP/IP socket of interest + * fds - The structure describing the events to be monitored, OR NULL if + * this is a request to stop monitoring events. + * + * Returned Value: + * 0: Success; Negated errno on failure + * + ****************************************************************************/ + +static inline int net_pollteardown(FAR struct socket *psock, + FAR struct pollfd *fds) +{ +#ifdef CONFIG_NET_TCP + if (psock->s_type == SOCK_STREAM) + { + return tcp_pollteardown(psock, fds); + } +#endif + +#ifdef CONFIG_NET_UDP + if (psock->s_type != SOCK_STREAM) + { + return udp_pollteardown(psock, fds); + } +#endif + + return -ENOSYS; +} #endif /* HAVE_NETPOLL */ /**************************************************************************** @@ -408,15 +717,6 @@ int psock_poll(FAR struct socket *psock, FAR struct pollfd *fds, bool setup) { int ret; -#ifdef CONFIG_NET_UDP - /* poll() not supported for UDP */ - - if (psock->s_type != SOCK_STREAM) - { - return -ENOSYS; - } -#endif - /* Check if we are setting up or tearing down the poll */ if (setup) diff --git a/nuttx/net/socket/recvfrom.c b/nuttx/net/socket/recvfrom.c index e9fcd7a83..cdb018093 100644 --- a/nuttx/net/socket/recvfrom.c +++ b/nuttx/net/socket/recvfrom.c @@ -100,6 +100,7 @@ struct recvfrom_s size_t rf_buflen; /* Length of receive buffer */ uint8_t *rf_buffer; /* Pointer to receive buffer */ FAR struct sockaddr *rf_from; /* Address of sender */ + FAR socklen_t *rf_fromlen; /* Number of bytes allocated for address of sender */ size_t rf_recvlen; /* The received length */ int rf_result; /* Success:OK, failure:negated errno */ }; @@ -310,7 +311,7 @@ static inline void recvfrom_newudpdata(FAR struct net_driver_s *dev, #endif /* CONFIG_NET_TCP */ /**************************************************************************** - * Function: recvfrom_readahead + * Function: recvfrom_tcpreadahead * * Description: * Copy the read data from the packet @@ -328,7 +329,7 @@ static inline void recvfrom_newudpdata(FAR struct net_driver_s *dev, ****************************************************************************/ #if defined(CONFIG_NET_TCP) && defined(CONFIG_NET_TCP_READAHEAD) -static inline void recvfrom_readahead(struct recvfrom_s *pstate) +static inline void recvfrom_tcpreadahead(struct recvfrom_s *pstate) { FAR struct tcp_conn_s *conn = (FAR struct tcp_conn_s *)pstate->rf_sock->s_conn; FAR struct iob_s *iob; @@ -391,6 +392,83 @@ static inline void recvfrom_readahead(struct recvfrom_s *pstate) } #endif /* CONFIG_NET_UDP || CONFIG_NET_TCP */ +#if defined(CONFIG_NET_UDP) && defined(CONFIG_NET_UDP_READAHEAD) + +static inline void recvfrom_udpreadahead(struct recvfrom_s *pstate) +{ + FAR struct udp_conn_s *conn = (FAR struct udp_conn_s *)pstate->rf_sock->s_conn; + FAR struct iob_s *iob; + int recvlen; + + /* Check there is any UDP datagram already buffered in a read-ahead + * buffer. + */ + + if ((iob = iob_peek_queue(&conn->readahead)) != NULL && + pstate->rf_buflen > 0) + { + FAR struct iob_s *tmp; + uint8_t src_addr_size; + + DEBUGASSERT(iob->io_pktlen > 0); + + /* Transfer that buffered data from the I/O buffer chain into + * the user buffer. + */ + + recvlen = iob_copyout(&src_addr_size, iob, sizeof(uint8_t), 0); + if (recvlen != sizeof(uint8_t)) + { + goto out; + } + + if ( 0 +#ifdef CONFIG_NET_IPv6 + || src_addr_size == 16 +#endif +#ifdef CONFIG_NET_IPv4 + || src_addr_size == 4 +#endif + ) + { + if (pstate->rf_from) + { + socklen_t len = *pstate->rf_fromlen; + len = (socklen_t)src_addr_size > len ? len : (socklen_t)src_addr_size; + + recvlen = iob_copyout(pstate->rf_from, iob, len, sizeof(uint8_t)); + if (recvlen != len) + { + goto out; + } + } + } + + recvlen = iob_copyout(pstate->rf_buffer, iob, pstate->rf_buflen, src_addr_size + sizeof(uint8_t)); + nllvdbg("Received %d bytes (of %d)\n", recvlen, iob->io_pktlen); + + /* Update the accumulated size of the data read */ + + pstate->rf_recvlen += recvlen; + pstate->rf_buffer += recvlen; + pstate->rf_buflen -= recvlen; + +out: + /* Remove the I/O buffer chain from the head of the read-ahead + * buffer queue. + */ + + tmp = iob_remove_queue(&conn->readahead); + DEBUGASSERT(tmp == iob); + UNUSED(tmp); + + /* And free the I/O buffer chain */ + + (void)iob_free_chain(iob); + } +} +#endif + /**************************************************************************** * Function: recvfrom_timeout * @@ -1015,6 +1093,7 @@ static uint16_t recvfrom_udpinterrupt(struct net_driver_s *dev, void *pvconn, #if defined(CONFIG_NET_UDP) || defined(CONFIG_NET_TCP) static void recvfrom_init(FAR struct socket *psock, FAR void *buf, size_t len, FAR struct sockaddr *infrom, + FAR socklen_t *fromlen, FAR struct recvfrom_s *pstate) { /* Initialize the state structure. */ @@ -1024,6 +1103,7 @@ static void recvfrom_init(FAR struct socket *psock, FAR void *buf, pstate->rf_buflen = len; pstate->rf_buffer = buf; pstate->rf_from = infrom; + pstate->rf_fromlen = fromlen; /* Set up the start time for the timeout */ @@ -1184,7 +1264,7 @@ static inline void recvfrom_udp_rxnotify(FAR struct socket *psock, #ifdef CONFIG_NET_PKT static ssize_t pkt_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, - FAR struct sockaddr *from) + FAR struct sockaddr *from, FAR socklen_t *fromlen) { FAR struct pkt_conn_s *conn = (FAR struct pkt_conn_s *)psock->s_conn; struct recvfrom_s state; @@ -1199,7 +1279,7 @@ static ssize_t pkt_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, */ save = net_lock(); - recvfrom_init(psock, buf, len, from, &state); + recvfrom_init(psock, buf, len, from, fromlen, &state); /* TODO recvfrom_init() expects from to be of type sockaddr_in, but * in our case is sockaddr_ll @@ -1277,7 +1357,7 @@ errout_with_state: #ifdef CONFIG_NET_UDP static ssize_t udp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, - FAR struct sockaddr *from) + FAR struct sockaddr *from, FAR socklen_t *fromlen) { FAR struct udp_conn_s *conn = (FAR struct udp_conn_s *)psock->s_conn; struct recvfrom_s state; @@ -1292,7 +1372,7 @@ static ssize_t udp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, */ save = net_lock(); - recvfrom_init(psock, buf, len, from, &state); + recvfrom_init(psock, buf, len, from, fromlen, &state); /* Setup the UDP remote connection */ @@ -1302,6 +1382,48 @@ static ssize_t udp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, goto errout_with_state; } +#ifdef CONFIG_NET_UDP_READAHEAD + recvfrom_udpreadahead(&state); + + /* The default return value is the number of bytes that we just copied + * into the user buffer. We will return this if the socket has become + * disconnected or if the user request was completely satisfied with + * data from the readahead buffers. + */ + + ret = state.rf_recvlen; + +#else + /* Otherwise, the default return value of zero is used (only for the case + * where len == state.rf_buflen is zero). + */ + + ret = 0; +#endif + +#ifdef CONFIG_NET_UDP_READAHEAD + if (_SS_ISNONBLOCK(psock->s_flags)) + { + /* Return the number of bytes read from the read-ahead buffer if + * something was received (already in 'ret'); EAGAIN if not. + */ + + if (ret <= 0) + { + /* Nothing was received */ + + ret = -EAGAIN; + } + } + + /* It is okay to block if we need to. If there is space to receive anything + * more, then we will wait to receive the data. Otherwise return the number + * of bytes read from the read-ahead buffer (already in 'ret'). + */ + + else if (state.rf_recvlen == 0) +#endif + { /* Set up the callback in the connection */ state.rf_cb = udp_callback_alloc(conn); @@ -1334,6 +1456,7 @@ static ssize_t udp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, { ret = -EBUSY; } + } errout_with_state: net_unlock(save); @@ -1364,7 +1487,7 @@ errout_with_state: #ifdef CONFIG_NET_TCP static ssize_t tcp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, - FAR struct sockaddr *from) + FAR struct sockaddr *from, FAR socklen_t *fromlen) { struct recvfrom_s state; net_lock_t save; @@ -1376,7 +1499,7 @@ static ssize_t tcp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, */ save = net_lock(); - recvfrom_init(psock, buf, len, from, &state); + recvfrom_init(psock, buf, len, from, fromlen, &state); /* Handle any any TCP data already buffered in a read-ahead buffer. NOTE * that there may be read-ahead data to be retrieved even after the @@ -1384,7 +1507,7 @@ static ssize_t tcp_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, */ #ifdef CONFIG_NET_TCP_READAHEAD - recvfrom_readahead(&state); + recvfrom_tcpreadahead(&state); /* The default return value is the number of bytes that we just copied * into the user buffer. We will return this if the socket has become @@ -1597,6 +1720,12 @@ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, } #endif + if (from && !fromlen) + { + err = EINVAL; + goto errout; + } + /* Verify that the sockfd corresponds to valid, allocated socket */ if (!psock || psock->s_crefs <= 0) @@ -1666,7 +1795,7 @@ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, #ifdef CONFIG_NET_PKT case SOCK_RAW: { - ret = pkt_recvfrom(psock, buf, len, from); + ret = pkt_recvfrom(psock, buf, len, from, fromlen); } break; #endif /* CONFIG_NET_PKT */ @@ -1689,7 +1818,7 @@ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, else #endif { - ret = tcp_recvfrom(psock, buf, len, from); + ret = tcp_recvfrom(psock, buf, len, from, fromlen); } #endif /* CONFIG_NET_TCP */ } @@ -1714,7 +1843,7 @@ ssize_t psock_recvfrom(FAR struct socket *psock, FAR void *buf, size_t len, else #endif { - ret = udp_recvfrom(psock, buf, len, from); + ret = udp_recvfrom(psock, buf, len, from, fromlen); } #endif /* CONFIG_NET_UDP */ } diff --git a/nuttx/net/tcp/tcp_send_buffered.c b/nuttx/net/tcp/tcp_send_buffered.c index ca8dddc4a..98621835d 100644 --- a/nuttx/net/tcp/tcp_send_buffered.c +++ b/nuttx/net/tcp/tcp_send_buffered.c @@ -307,7 +307,7 @@ static inline bool psock_send_addrchck(FAR struct tcp_conn_s *conn) } #else /* CONFIG_NET_ETHERNET */ -# psock_send_addrchck(r) (true) +# define psock_send_addrchck(r) (true) #endif /* CONFIG_NET_ETHERNET */ /**************************************************************************** diff --git a/nuttx/net/udp/Kconfig b/nuttx/net/udp/Kconfig index 68a80bced..b387b402d 100644 --- a/nuttx/net/udp/Kconfig +++ b/nuttx/net/udp/Kconfig @@ -50,5 +50,10 @@ config NET_RXAVAIL NOTE: If this option is enabled, the driver must support the rxavail() method in the net_driver_s structure. +config NET_UDP_READAHEAD + bool "Enable UDP/IP read-ahead buffering" + default y + select NET_IOB + endif # NET_UDP endmenu # UDP Networking diff --git a/nuttx/net/udp/udp.h b/nuttx/net/udp/udp.h index 305f28b37..2f193dea0 100644 --- a/nuttx/net/udp/udp.h +++ b/nuttx/net/udp/udp.h @@ -47,6 +47,10 @@ #include <nuttx/net/ip.h> +#ifdef CONFIG_NET_UDP_READAHEAD +# include <nuttx/net/iob.h> +#endif + #ifdef CONFIG_NET_UDP /**************************************************************************** @@ -77,6 +81,16 @@ struct udp_conn_s uint8_t ttl; /* Default time-to-live */ uint8_t crefs; /* Reference counts on this instance */ +#ifdef CONFIG_NET_UDP_READAHEAD + /* Read-ahead buffering. + * + * readahead - A singly linked list of type struct iob_qentry_s + * where the UDP/IP read-ahead data is retained. + */ + + struct iob_queue_s readahead; /* Read-ahead buffering */ +#endif + /* Defines the list of UDP callbacks */ FAR struct devif_callback_s *list; diff --git a/nuttx/net/udp/udp_callback.c b/nuttx/net/udp/udp_callback.c index a1553adb2..ab06e2522 100644 --- a/nuttx/net/udp/udp_callback.c +++ b/nuttx/net/udp/udp_callback.c @@ -48,9 +48,20 @@ #include <nuttx/net/udp.h> #include "devif/devif.h" +#include "iob/iob.h" #include "udp/udp.h" /**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define IPv4BUF ((FAR struct ipv4_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)]) +#define IPv6BUF ((FAR struct ipv6_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev)]) + +#define UDPIPv4BUF ((FAR struct udp_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv4_HDRLEN]) +#define UDPIPv6BUF ((FAR struct udp_hdr_s *)&dev->d_buf[NET_LL_HDRLEN(dev) + IPv6_HDRLEN]) + +/**************************************************************************** * Private Data ****************************************************************************/ @@ -59,6 +70,196 @@ ****************************************************************************/ /**************************************************************************** + * Function: udp_datahandler + * + * Description: + * Handle the receipt of UDP data by adding the newly received packet to + * the UDP read-ahead buffer. + * + ****************************************************************************/ + +#ifdef CONFIG_NET_UDP_READAHEAD +static uint16_t udp_datahandler(FAR struct net_driver_s *dev, FAR struct udp_conn_s *conn, + FAR uint8_t *buffer, uint16_t buflen) +{ + FAR struct iob_s *iob; + int ret; +#ifdef CONFIG_NET_IPv6 + FAR struct sockaddr_in6 src_addr6; +#endif +#ifdef CONFIG_NET_IPv4 + FAR struct sockaddr_in src_addr4; +#endif + FAR void *src_addr; + uint8_t src_addr_size; + + /* Allocate on I/O buffer to start the chain (throttling as necessary). + * We will not wait for an I/O buffer to become available in this context. + */ + + iob = iob_tryalloc(true); + if (iob == NULL) + { + nlldbg("ERROR: Failed to create new I/O buffer chain\n"); + return 0; + } + +#ifdef CONFIG_NET_IPv6 +#ifdef CONFIG_NET_IPv4 + if (IFF_IS_IPv6(dev->d_flags)) +#endif + { + FAR struct udp_hdr_s *udp = UDPIPv6BUF; + FAR struct ipv6_hdr_s *ipv6 = IPv6BUF; + + src_addr6.sin6_family = AF_INET6; + src_addr6.sin6_port = udp->srcport; + + net_ipv6addr_copy(src_addr6.sin6_addr.s6_addr, ipv6->srcipaddr); + + src_addr_size = sizeof(src_addr6); + src_addr = &src_addr6; + } +#endif /* CONFIG_NET_IPv6 */ + +#ifdef CONFIG_NET_IPv4 +#ifdef CONFIG_NET_IPv6 + else +#endif + { + FAR struct udp_hdr_s *udp = UDPIPv4BUF; + FAR struct ipv4_hdr_s *ipv4 = IPv4BUF; + + src_addr4.sin_family = AF_INET; + src_addr4.sin_port = udp->srcport; + + net_ipv4addr_copy(src_addr4.sin_addr.s_addr, + net_ip4addr_conv32(ipv4->srcipaddr)); + + src_addr_size = sizeof(src_addr4); + src_addr = &src_addr4; + } +#endif /* CONFIG_NET_IPv4 */ + + /* Copy the src address info into the I/O buffer chain. We will not wait + * for an I/O buffer to become available in this context. It there is + * any failure to allocated, the entire I/O buffer chain will be discarded. + */ + + ret = iob_trycopyin(iob, (FAR const uint8_t*)&src_addr_size, + sizeof(uint8_t), 0, true); + if (ret < 0) + { + /* On a failure, iob_trycopyin return a negated error value but does + * not free any I/O buffers. + */ + + nlldbg("ERROR: Failed to add data to the I/O buffer chain: %d\n", ret); + (void)iob_free_chain(iob); + return 0; + } + + ret = iob_trycopyin(iob, (FAR const uint8_t*)src_addr, src_addr_size, + sizeof(uint8_t), true); + if (ret < 0) + { + /* On a failure, iob_trycopyin return a negated error value but does + * not free any I/O buffers. + */ + + nlldbg("ERROR: Failed to add data to the I/O buffer chain: %d\n", ret); + (void)iob_free_chain(iob); + return 0; + } + + /* Copy the new appdata into the I/O buffer chain */ + + ret = iob_trycopyin(iob, buffer, buflen, src_addr_size + sizeof(uint8_t), + true); + if (ret < 0) + { + /* On a failure, iob_trycopyin return a negated error value but does + * not free any I/O buffers. + */ + + nlldbg("ERROR: Failed to add data to the I/O buffer chain: %d\n", ret); + (void)iob_free_chain(iob); + return 0; + } + + /* Add the new I/O buffer chain to the tail of the read-ahead queue */ + + ret = iob_tryadd_queue(iob, &conn->readahead); + if (ret < 0) + { + nlldbg("ERROR: Failed to queue the I/O buffer chain: %d\n", ret); + (void)iob_free_chain(iob); + return 0; + } + + nllvdbg("Buffered %d bytes\n", buflen); + return buflen; +} +#endif /* CONFIG_NET_UDP_READAHEAD */ + +/**************************************************************************** + * Function: net_dataevent + * + * Description: + * Handling the network UDP_NEWDATA event. + * + ****************************************************************************/ + +static inline uint16_t +net_dataevent(FAR struct net_driver_s *dev, FAR struct udp_conn_s *conn, + uint16_t flags) +{ + uint16_t ret; + + ret = (flags & ~UDP_NEWDATA); + + /* Is there new data? With non-zero length? (Certain connection events + * can have zero-length with UDP_NEWDATA set just to cause an ACK). + */ + + if (dev->d_len > 0) + { +#ifdef CONFIG_NET_UDP_READAHEAD + uint8_t *buffer = dev->d_appdata; + int buflen = dev->d_len; + uint16_t recvlen; +#endif + + nllvdbg("No receive on connection\n"); + +#ifdef CONFIG_NET_UDP_READAHEAD + /* Save as the packet data as in the read-ahead buffer. NOTE that + * partial packets will not be buffered. + */ + + recvlen = udp_datahandler(dev, conn, buffer, buflen); + if (recvlen < buflen) +#endif + { + /* There is no handler to receive new data and there are no free + * read-ahead buffers to retain the data -- drop the packet. + */ + + nllvdbg("Dropped %d bytes\n", dev->d_len); + + #ifdef CONFIG_NET_STATISTICS + g_netstats.udp.drop++; +#endif + } + } + + /* In any event, the new data has now been handled */ + + dev->d_len = 0; + return ret; +} + +/**************************************************************************** * Public Functions ****************************************************************************/ @@ -88,6 +289,13 @@ uint16_t udp_callback(FAR struct net_driver_s *dev, /* Perform the callback */ flags = devif_callback_execute(dev, conn, flags, conn->list); + + if ((flags & UDP_NEWDATA) != 0) + { + /* Data was not handled.. dispose of it appropriately */ + + flags = net_dataevent(dev, conn, flags); + } } return flags; |