Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/nuttx/net/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ struct socket_conn_s
uint8_t s_ttl; /* Default time-to-live */
#endif

#ifdef CONFIG_NET_TIMESTAMP
struct iob_queue_s s_errahead; /* TX timestamp error queue */
#endif

/* Connection-specific content may follow */
};

Expand Down
37 changes: 22 additions & 15 deletions net/devif/ipv4_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,23 +232,40 @@ static int ipv4_in(FAR struct net_driver_s *dev)
bool isfrag;
int ret = OK;

/* Storing reception timestamp provided by realtime
* if timestamp no provided by hardware.
/* This is where the input processing starts.
*
* Get the size of the packet minus the size of link layer header,
* and make sure that all packet processing logic knows that there
* is an IPv4 packet in the device buffer.
*/

dev->d_len -= NET_LL_HDRLEN(dev);
IFF_SET_IPv4(dev->d_flags);

#ifdef CONFIG_NET_TIMESTAMP
#ifdef NET_UDP_HAVE_STACK
if (dev->d_iob->io_conn != NULL)
{
/* This is a TX timestamp clone looped back by the driver.
* Skip all validation (dest IP, checksum, etc.) and deliver
* it to the originating socket's error queue directly.
*/

udp_ipv4_input(dev);
goto drop;
}
#endif

if ((dev->d_features & NETDEV_RX_STAMP) == 0)
{
clock_gettime(CLOCK_REALTIME, &dev->d_iob->io_time);
}
#endif /* CONFIG_NET_TIMESTAMP */
#endif

/* Handle ARP on input then give the IPv4 packet to the network layer */

arp_ipin(dev);

/* This is where the input processing starts. */

#ifdef CONFIG_NET_STATISTICS
g_netstats.ipv4.recv++;
#endif
Expand All @@ -275,22 +292,12 @@ static int ipv4_in(FAR struct net_driver_s *dev)
goto drop;
}

/* Get the size of the packet minus the size of link layer header */

dev->d_len -= NET_LL_HDRLEN(dev);

if (((ipv4->vhl & IPv4_HLMASK) << 2) > dev->d_len)
{
nwarn("WARNING: Packet shorter than IPv4 header\n");
goto drop;
}

/* Make sure that all packet processing logic knows that there is an IPv4
* packet in the device buffer.
*/

IFF_SET_IPv4(dev->d_flags);

/* Check the size of the packet. If the size reported to us in d_len is
* smaller the size reported in the IP header, we assume that the packet
* has been corrupted in transit. If the size of d_len is larger than the
Expand Down
37 changes: 22 additions & 15 deletions net/devif/ipv6_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,35 @@ static int ipv6_in(FAR struct net_driver_s *dev)
bool isfrag = false;
#endif

/* Storing reception timestamp provided by realtime
* if timestamp no provided by hardware.
/* This is where the input processing starts.
*
* Get the size of the packet minus the size of link layer header,
* and make sure that all packet processing logic knows that there
* is an IPv6 packet in the device buffer.
*/

dev->d_len -= NET_LL_HDRLEN(dev);
IFF_SET_IPv6(dev->d_flags);

#ifdef CONFIG_NET_TIMESTAMP
#ifdef NET_UDP_HAVE_STACK
if (dev->d_iob->io_conn != NULL)
{
/* This is a TX timestamp clone looped back by the driver.
* Skip all validation and deliver it to the originating
* socket's error queue directly.
*/

udp_ipv6_input(dev, IPv6_HDRLEN);
goto drop;
}
#endif

if ((dev->d_features & NETDEV_RX_STAMP) == 0)
{
clock_gettime(CLOCK_REALTIME, &dev->d_iob->io_time);
}
#endif /* CONFIG_NET_TIMESTAMP */

/* This is where the input processing starts. */
#endif

#ifdef CONFIG_NET_STATISTICS
g_netstats.ipv6.recv++;
Expand All @@ -263,22 +280,12 @@ static int ipv6_in(FAR struct net_driver_s *dev)
goto drop;
}

/* Get the size of the packet minus the size of link layer header */

dev->d_len -= NET_LL_HDRLEN(dev);

if (IPv6_HDRLEN > dev->d_len)
{
nwarn("WARNING: Packet shorter than IPv6 header\n");
goto drop;
}

/* Make sure that all packet processing logic knows that there is an IPv6
* packet in the device buffer.
*/

IFF_SET_IPv6(dev->d_flags);

/* Check the size of the packet. If the size reported to us in d_len is
* smaller the size reported in the IP header, we assume that the packet
* has been corrupted in transit. If the size of d_len is larger than the
Expand Down
4 changes: 0 additions & 4 deletions net/pkt/pkt.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ struct pkt_conn_s

struct iob_queue_s readahead; /* Read-ahead buffering */

#ifdef CONFIG_NET_TIMESTAMP
struct iob_queue_s errahead; /* Error-ahead buffering */
#endif

FAR struct iob_s *pendiob; /* The iob currently being sent */

/* The following is a list of poll structures of threads waiting for
Expand Down
8 changes: 0 additions & 8 deletions net/pkt/pkt_conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,12 @@ void pkt_free(FAR struct pkt_conn_s *conn)

iob_free_queue(&conn->readahead);

#ifdef CONFIG_NET_TIMESTAMP
iob_free_queue(&conn->errahead);
#endif

/* Free the read-ahead data. pkt_in_() queues frames with the list
* locked, so no new frame can come in after dq_rem().
*/

iob_free_queue(&conn->readahead);

#ifdef CONFIG_NET_TIMESTAMP
iob_free_queue(&conn->errahead);
#endif

#ifdef CONFIG_NET_PKT_WRITE_BUFFERS
/* Free the write queue */

Expand Down
2 changes: 1 addition & 1 deletion net/pkt/pkt_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ static int pkt_in_(FAR struct net_driver_s *dev, bool loopback)

if (dev->d_iob->io_conn == &conn->sconn)
{
if (pkt_datahandler(dev, conn, &conn->errahead) > 0)
if (pkt_datahandler(dev, conn, &conn->sconn.s_errahead) > 0)
{
pkt_callback(dev, conn, PKT_NEWDATA);
}
Expand Down
4 changes: 2 additions & 2 deletions net/pkt/pkt_netpoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ static uint32_t pkt_poll_eventhandler(FAR struct net_driver_s *dev,
#ifdef CONFIG_NET_TIMESTAMP
/* Check for timestamping data */

if (!IOB_QEMPTY(&info->conn->errahead))
if (!IOB_QEMPTY(&info->conn->sconn.s_errahead))
{
eventset |= POLLPRI | POLLERR;
}
Expand Down Expand Up @@ -281,7 +281,7 @@ int pkt_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds)
/* Check for timestamping data */

#ifdef CONFIG_NET_TIMESTAMP
if (!IOB_QEMPTY(&conn->errahead))
if (!IOB_QEMPTY(&conn->sconn.s_errahead))
{
eventset |= POLLPRI | POLLERR;
}
Expand Down
5 changes: 3 additions & 2 deletions net/pkt/pkt_recvmsg.c
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,10 @@ ssize_t pkt_recvmsg(FAR struct socket *psock, FAR struct msghdr *msg,
#ifdef CONFIG_NET_TIMESTAMP
if (flags & MSG_ERRQUEUE)
{
if (!IOB_QEMPTY(&conn->errahead))
if (!IOB_QEMPTY(&conn->sconn.s_errahead))
{
ret = pkt_readdata(&state, &conn->errahead, append_timestamping);
ret = pkt_readdata(&state, &conn->sconn.s_errahead,
append_timestamping);
}
else
{
Expand Down
10 changes: 9 additions & 1 deletion net/tcp/tcp_send.c
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,17 @@ static void tcp_sendcommon(FAR struct net_driver_s *dev,
else
#endif
{
/* Copy out of the address union: taking the address of the IPv4
* member makes GCC 11/12 narrow the whole union to 4 bytes and
* warn about the 16-byte IPv6 read in the branch above.
*/

in_addr_t laddr = conn->u.ipv4.laddr;
in_addr_t raddr = conn->u.ipv4.raddr;

ninfo("do IPv4 IP header build!\n");
ipv4_build_header(IPv4BUF, dev->d_len, IP_PROTO_TCP,
&dev->d_ipaddr, &conn->u.ipv4.raddr,
&laddr, &raddr,
conn->sconn.s_ttl, conn->sconn.s_tos, NULL);

/* Calculate TCP checksum. */
Expand Down
14 changes: 13 additions & 1 deletion net/udp/udp_callback.c
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ static uint16_t udp_datahandler(FAR struct net_driver_s *dev,
#if CONFIG_NET_RECV_BUFSIZE > 0
if (conn->readahead && conn->readahead->io_pktlen > conn->rcvbufs)
{
/* The read-ahead buffer is full, drop the datagram. */

ninfo("Dropped %d bytes\n", dev->d_len);

#ifdef CONFIG_NET_STATISTICS
g_netstats.udp.drop++;
#endif

conn_unlock(&conn->sconn);
netdev_iob_release(dev);
return 0;
Expand Down Expand Up @@ -296,7 +304,11 @@ uint32_t udp_callback(FAR struct net_driver_s *dev,
conn_lock(&conn->sconn);
flags = devif_conn_event(dev, flags, conn->sconn.s_list);

if ((flags & UDP_NEWDATA) != 0)
if ((flags & UDP_NEWDATA) != 0
#ifdef CONFIG_NET_TIMESTAMP
&& dev->d_iob->io_conn == NULL
#endif
)
{
/* Data was not handled.. dispose of it appropriately */

Expand Down
54 changes: 51 additions & 3 deletions net/udp/udp_input.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@
#include <nuttx/net/netstats.h>

#include "devif/devif.h"
#include "utils/utils.h"
#include "udp/udp.h"
#include "icmp/icmp.h"
#include "icmpv6/icmpv6.h"
#include "socket/socket.h"
#include "udp/udp.h"
#include "utils/utils.h"

/****************************************************************************
* Private Functions
Expand Down Expand Up @@ -103,6 +104,7 @@ static bool udp_is_broadcast(FAR struct net_driver_s *dev)
# endif
{
FAR struct ipv6_hdr_s *ipv6 = IPv6BUF;

return net_is_addr_mcast(ipv6->destipaddr);
}
#endif
Expand Down Expand Up @@ -221,6 +223,52 @@ static int udp_input(FAR struct net_driver_s *dev, unsigned int iplen)
#endif
int ret = OK;

#ifdef CONFIG_NET_TIMESTAMP
/* Handle TX timestamp: if the IOB was tagged with a connection on the
* send path, queue it to that connection's error-ahead buffer for
* MSG_ERRQUEUE. Skip checksum and normal RX processing entirely.
*/

if (dev->d_iob->io_conn != NULL)
{
udp_conn_list_lock();

/* Validate that the connection is still in the active list.
* The connection may have been closed and freed between the
* send path (where io_conn was set) and now.
*/

for (conn = udp_nextconn(NULL); conn != NULL;
conn = udp_nextconn(conn))
{
if (&conn->sconn == dev->d_iob->io_conn)
{
break;
}
}

if (conn != NULL)
{
/* Move dev->d_iob directly into the error-ahead queue
* (zero-copy), then prepare a fresh iob for the device.
*/

iob_add_queue(dev->d_iob, &conn->sconn.s_errahead);

/* Notify all waiters (poll and blocking recvfrom)
* through the standard callback chain.
*/

udp_callback(dev, conn, UDP_NEWDATA);
netdev_iob_clear(dev);
}

dev->d_len = 0;
udp_conn_list_unlock();
return OK;
}
#endif

/* Update the count of UDP packets received */

#ifdef CONFIG_NET_STATISTICS
Expand Down Expand Up @@ -381,7 +429,7 @@ static int udp_input(FAR struct net_driver_s *dev, unsigned int iplen)
# endif /* CONFIG_NET_IPv4 */
# ifdef CONFIG_NET_IPv6
# ifdef CONFIG_NET_IPv4
else
else
# endif
{
# ifdef CONFIG_NET_ICMPv6
Expand Down
23 changes: 23 additions & 0 deletions net/udp/udp_netpoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,15 @@ static uint32_t udp_poll_eventhandler(FAR struct net_driver_s *dev,
eventset |= POLLOUT;
}

#ifdef CONFIG_NET_TIMESTAMP
/* Check for timestamping data */

if (!IOB_QEMPTY(&info->conn->sconn.s_errahead))
{
eventset |= POLLPRI | POLLERR;
}
#endif

/* Awaken the caller of poll() is requested event occurred. */

poll_notify(&info->fds, 1, eventset);
Expand Down Expand Up @@ -207,6 +216,13 @@ int udp_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds)
cb->flags |= UDP_NEWDATA;
}

#ifdef CONFIG_NET_TIMESTAMP
if ((fds->events & POLLPRI) != 0)
{
cb->flags |= UDP_NEWDATA;
}
#endif

/* Save the reference in the poll info structure as fds private as well
* for use during poll teardown as well.
*/
Expand All @@ -222,6 +238,13 @@ int udp_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds)
eventset |= POLLRDNORM;
}

#ifdef CONFIG_NET_TIMESTAMP
if (!IOB_QEMPTY(&conn->sconn.s_errahead))
{
eventset |= POLLPRI | POLLERR;
}
#endif

if (psock_udp_cansend(conn) >= 0)
{
/* Normal data may be sent without blocking (at least one byte). */
Expand Down
Loading
Loading