Skip to contentSkip to conversation

Enable retrieving of RTNET network packet timestamp OOB using recvmsg()

PATCH Superseded François Legal 2 participants 2 messages May 12, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Enable retrieving of RTNET network packet timestamp OOB using recvmsg()

This patch enables retrieving, in realtime application, of RTNET enabled adapters packet timestamps. It uses the linux semantic SO_TIMESTAMPNS, and the linux code to put the timestamp in the control_msg structure.

I tested this patch with af_packet sockets only. UDP & TCP might be a little bit trickier as many fragment/segments get reassembled in a single recvmsg. I believe the linux code I used to put the timestamps in the control structure should be OK though.

---
 kernel/drivers/net/stack/Kconfig              |  7 +++
 kernel/drivers/net/stack/include/rtnet_socket.h  |  6 ++
 kernel/drivers/net/stack/ipv4/tcp/tcp.c       |  7 +++
 kernel/drivers/net/stack/ipv4/udp/udp.c       |  6 ++
 kernel/drivers/net/stack/packet/af_packet.c   |  6 ++
 kernel/drivers/net/stack/socket.c             | 57 +++++++++++++++++++
 6 files changed, 89 insertions(+)
kernel/drivers/net/stack/Kconfig +7 -0
index 830cec5ad..cb6133151 100644
@@ -12,6 +12,13 @@ config XENO_DRIVERS_NET_RX_FIFO_SIZE
     of two! Effectively, only CONFIG_RTNET_RX_FIFO_SIZE-1 slots will
     be usable.
 
+config XENO_DRIVERS_NET_PACKET_TIMESTAMP
+    bool "Enable packet timestamping (SO_TIMESTAMPNS)"
+    depends on XENO_DRIVERS_NET
+    ---help---
+    Enable user land access to low level packet timestamps using SO_TIMESTAMPNS
+    ioctl on socket.
+
 config XENO_DRIVERS_NET_ETH_P_ALL
     depends on XENO_DRIVERS_NET
     bool "Support for ETH_P_ALL"
kernel/drivers/net/stack/include/rtnet_socket.h +6 -0
index d2caab649..cc6776c70 100644
@@ -29,6 +29,7 @@
 
 #include <asm/atomic.h>
 #include <linux/list.h>
+#include <net/sock.h>
 
 #include <rtdev.h>
 #include <rtdm/net.h>
@@ -77,6 +78,11 @@ struct rtsocket {
 	} prot;
 };
 
+#ifdef CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP
+#define SOCKET_FLAG_TIMESTAMP   SOCK_RCVTSTAMPNS
+int rtnet_put_cmsg(struct rtdm_fd *fd, struct user_msghdr * msg, int level, int type, int len, void *data);
+#endif
+
 static inline struct rtdm_fd *rt_socket_fd(struct rtsocket *sock)
 {
 	return rtdm_private_to_fd(sock);
kernel/drivers/net/stack/ipv4/tcp/tcp.c +7 -0
index d8c189c88..1608c108e 100644
@@ -2027,6 +2027,13 @@ static ssize_t rt_tcp_read(struct rtdm_fd *fd, void *buf, size_t nbyte)
 			kfree_rtskb(first_skb); /* or store the data? */
 			return -EFAULT;
 		}
+
+#ifdef CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP
+	    if (test_bit(SOCKET_FLAG_TIMESTAMP, & sock->flags)) {
+            rtnet_put_cmsg (fd, msg, SOL_SOCKET, SCM_TIMESTAMPNS, sizeof (nanosecs_abs_t), (void *) & skb->time_stamp);
+        }
+#endif /* CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP */
+
 		rtdm_lock_get_irqsave(&ts->socket_lock, context);
 		if (ts->sync.window) {
 			ts->sync.window += block_size;
kernel/drivers/net/stack/ipv4/udp/udp.c +6 -0
index 546b35855..68c990685 100644
@@ -463,6 +463,12 @@ ssize_t rt_udp_recvmsg(struct rtdm_fd *fd, struct user_msghdr *msg,
 	do {
 		rtskb_trim(skb, data_len);
 
+#ifdef CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP
+	    if (test_bit(SOCKET_FLAG_TIMESTAMP, & sock->flags)) {
+            rtnet_put_cmsg (fd, msg, SOL_SOCKET, SCM_TIMESTAMPNS, sizeof (nanosecs_abs_t), (void *) & skb->time_stamp);
+        }
+#endif /* CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP */
+
 		block_size = skb->len;
 		copied += block_size;
 		data_len -= block_size;
kernel/drivers/net/stack/packet/af_packet.c +6 -0
index cc7487303..54a0f5c43 100644
@@ -364,6 +364,12 @@ static ssize_t rt_packet_recvmsg(struct rtdm_fd *fd, struct user_msghdr *msg,
 	if (rtdm_fd_to_context(fd)->device->driver->socket_type != SOCK_DGRAM)
 		rtskb_push(rtskb, rtskb->data - rtskb->mac.raw);
 
+#ifdef CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP
+	if (test_bit(SOCKET_FLAG_TIMESTAMP, & sock->flags)) {
+	    rtnet_put_cmsg (fd, msg, SOL_SOCKET, SCM_TIMESTAMPNS, sizeof (nanosecs_abs_t), (void *) & rtskb->time_stamp);
+	}
+#endif /* CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP */
+
 	/* The data must not be longer than the available buffer size */
 	copy_len = rtskb->len;
 	len = rtdm_get_iov_flatlen(iov, msg->msg_iovlen);
kernel/drivers/net/stack/socket.c +57 -0

66 lines not on this page · raw message

--
2.20.1

#1 lore raw
Quoted 3 lines · François Legal via Xenomai

This patch enables retrieving, in realtime application, of RTNET enabled adapters packet timestamps. It uses the linux semantic SO_TIMESTAMPNS, and the linux code to put the timestamp in the control_msg structure.

Thanks for the patch. Could you reformat according to kernel style (checkpatch.pl)?

Quoted 1 line

I tested this patch with af_packet sockets only. UDP & TCP might be a little bit trickier as many fragment/segments get reassembled in a single recvmsg. I believe the linux code I used to put the timestamps in the control structure should be OK though.

How does Linux resolve the case of stamping fragmented messages?

Thanks,
Jan

Signature · 2 lines
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux