Skip to contentSkip to conversation

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

PATCH In review François Legal 3 participants 7 messages May 17, 2021 – Aug 2, 2021

lore
Share & tools
lore
#0 lore raw

PATCH v2 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              |  8 +++
 .../drivers/net/stack/include/rtnet_socket.h  |  7 +++
 kernel/drivers/net/stack/ipv4/tcp/tcp.c       |  8 +++
 kernel/drivers/net/stack/ipv4/udp/udp.c       |  7 +++
 kernel/drivers/net/stack/packet/af_packet.c   |  7 +++
 kernel/drivers/net/stack/socket.c             | 59 +++++++++++++++++++
 6 files changed, 96 insertions(+)
kernel/drivers/net/stack/Kconfig +8 -0
index 830cec5ad..f8ee0f1ad 100644
@@ -12,6 +12,14 @@ 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 userland access to low level packet timestamps using SO_TIMESTAMPNS
+    ioctl on socket. Timestamp are then returned in recvmsg calls in msg_control
+    structure inside msghdr structure.
+
 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 +7 -0
index d2caab649..dc488a58a 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,12 @@ 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 +8 -0
index d8c189c88..c8b21c521 100644
@@ -2027,6 +2027,14 @@ 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 +7 -0
index 546b35855..ac6448027 100644
@@ -463,6 +463,13 @@ 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 +7 -0
index cc7487303..10e8113fe 100644
@@ -364,6 +364,13 @@ 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 +59 -0

68 lines not on this page · raw message

--
2.20.1

#1 lore raw

Hello,

I wanted to raise this one too, if it's of any interest for the community.

François

Quoted 190 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.

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              |  8 +++
 .../drivers/net/stack/include/rtnet_socket.h  |  7 +++
 kernel/drivers/net/stack/ipv4/tcp/tcp.c       |  8 +++
 kernel/drivers/net/stack/ipv4/udp/udp.c       |  7 +++
 kernel/drivers/net/stack/packet/af_packet.c   |  7 +++

kernel/drivers/net/stack/socket.c | 59 +++++++++++++++++++ 6 files changed, 96 insertions(+)

kernel/drivers/net/stack/Kconfig +8 -0
index 830cec5ad..f8ee0f1ad 100644
@@ -12,6 +12,14 @@ 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 userland access to low level packet timestamps using SO_TIMESTAMPNS
+    ioctl on socket. Timestamp are then returned in recvmsg calls in msg_control
+    structure inside msghdr structure.
+
 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 +7 -0
index d2caab649..dc488a58a 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,12 @@ 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 +8 -0
index d8c189c88..c8b21c521 100644
@@ -2027,6 +2027,14 @@ 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 +7 -0
index 546b35855..ac6448027 100644
@@ -463,6 +463,13 @@ 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 +7 -0
index cc7487303..10e8113fe 100644
@@ -364,6 +364,13 @@ 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 +58 -1
index f030663be..5a9702975 100644
@@ -273,6 +273,15 @@ int rt_socket_if_ioctl(struct rtdm_fd *fd, int request, void __user *arg)
 		return rtnet_put_arg(fd, &u_ifc->ifc_len, &size, sizeof(size));
 	}
 
+#ifdef CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP
+	if (request == SO_TIMESTAMPNS) {
+		struct rtsocket *sock = rtdm_fd_to_private(fd);
+
+		set_bit(SOCKET_FLAG_TIMESTAMP, &sock->flags);
+		return 0;
+	}
+#endif
+
 	u_ifr = arg;
 	ifr = rtnet_get_arg(fd, &_ifr, u_ifr, sizeof(_ifr));
 	if (IS_ERR(ifr))
@@ -393,3 +402,53 @@ int rtnet_put_arg(struct rtdm_fd *fd, void *dst, const void *src, size_t len)
 	return rtdm_copy_to_user(fd, dst, src, len);
 }
 EXPORT_SYMBOL_GPL(rtnet_put_arg);
+
+#ifdef CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP
+int rtnet_put_cmsg(struct rtdm_fd *fd, struct user_msghdr *msg, int level,
+					int type, int len, void *data)
+{
+	struct cmsghdr __user *cm = msg->msg_control;
+	struct cmsghdr cmhdr;
+	int cmlen = CMSG_LEN(len);
+	int err;
+
+	if (MSG_CMSG_COMPAT & msg->msg_flags)
+		return 0; /* XXX: return error? check spec. */
+
+	if ((cm == NULL) || (msg->msg_controllen < sizeof(*cm))) {
+		msg->msg_flags |= MSG_CTRUNC;
+		return 0; /* XXX: return error? check spec. */
+	}
+	if (msg->msg_controllen < cmlen) {
+		msg->msg_flags |= MSG_CTRUNC;
+		cmlen = msg->msg_controllen;
+	}
+	cmhdr.cmsg_level = level;
+	cmhdr.cmsg_type = type;
+	cmhdr.cmsg_len = cmlen;
+
+	err = -EFAULT;
+
+	if (!rtdm_fd_is_user(fd)) {
+		memcpy(cm, &cmhdr, sizeof(struct cmsghdr));
+		memcpy(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr));
+		err = 0;
+	} else {
+		if (rtdm_copy_to_user(fd, cm, &cmhdr, sizeof(struct cmsghdr)))+			goto out;
+		if (rtdm_copy_to_user(fd, CMSG_DATA(cm), data,
+					cmlen - sizeof(struct cmsghdr)))
+			goto out;
+		cmlen = CMSG_SPACE(len);
+		if (msg->msg_controllen < cmlen)
+			cmlen = msg->msg_controllen;
+		msg->msg_control += cmlen;
+		msg->msg_controllen -= cmlen;
+		err = 0;
+	}
+out:
+	return err;
+}
+EXPORT_SYMBOL(rtdm_put_cmsg);
+
+#endif /* CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP */
--

2.20.1

#2 lore raw
In reply to François Legal
Quoted 4 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.

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.

One of my question on v1 is still open: How does Linux handle stamping the context of fragmented UDP and TCP packets? And given that you enable it for those protocols as well, we should likely test that too.

Quoted 17 lines
---
 kernel/drivers/net/stack/Kconfig              |  8 +++
 .../drivers/net/stack/include/rtnet_socket.h  |  7 +++
 kernel/drivers/net/stack/ipv4/tcp/tcp.c       |  8 +++
 kernel/drivers/net/stack/ipv4/udp/udp.c       |  7 +++
 kernel/drivers/net/stack/packet/af_packet.c   |  7 +++
 kernel/drivers/net/stack/socket.c             | 59 +++++++++++++++++++
 6 files changed, 96 insertions(+)
kernel/drivers/net/stack/Kconfig +0 -0
index 830cec5ad..f8ee0f1ad 100644
@@ -12,6 +12,14 @@ config XENO_DRIVERS_NET_RX_FIFO_SIZE
     of two! Effectively, only CONFIG_RTNET_RX_FIFO_SIZE-1 slots will

kernel/drivers/net/stack/Kconfig config XENO_DRIVERS_NET_RX_FIFO_SIZE · line 22

Quoted 10 lines
kernel/drivers/net/stack/Kconfig +8 -0
     be usable.
 
+config XENO_DRIVERS_NET_PACKET_TIMESTAMP
+    bool "Enable packet timestamping (SO_TIMESTAMPNS)"
+    depends on XENO_DRIVERS_NET
+    ---help---
+    Enable userland access to low level packet timestamps using SO_TIMESTAMPNS
+    ioctl on socket. Timestamp are then returned in recvmsg calls in msg_control
+    structure inside msghdr structure.
+

Why do we need a config option? All it adds in the off-case is a single bit test.

Quoted 113 lines
kernel/drivers/net/stack/Kconfig +0 -0
 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 +7 -0
index d2caab649..dc488a58a 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,12 @@ 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 +8 -0
index d8c189c88..c8b21c521 100644
@@ -2027,6 +2027,14 @@ 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 +7 -0
index 546b35855..ac6448027 100644
@@ -463,6 +463,13 @@ 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 +7 -0
index cc7487303..10e8113fe 100644
@@ -364,6 +364,13 @@ 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 +15 -0
index f030663be..5a9702975 100644
@@ -273,6 +273,15 @@ int rt_socket_if_ioctl(struct rtdm_fd *fd, int request, void __user *arg)
 		return rtnet_put_arg(fd, &u_ifc->ifc_len, &size, sizeof(size));
 	}
 
+#ifdef CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP
+	if (request == SO_TIMESTAMPNS) {
+		struct rtsocket *sock = rtdm_fd_to_private(fd);
+
+		set_bit(SOCKET_FLAG_TIMESTAMP, &sock->flags);
+		return 0;
+	}
+#endif
+
 	u_ifr = arg;
 	ifr = rtnet_get_arg(fd, &_ifr, u_ifr, sizeof(_ifr));
 	if (IS_ERR(ifr))
@@ -393,3 +402,53 @@ int rtnet_put_arg(struct rtdm_fd *fd, void *dst, const void *src, size_t len)
 	return rtdm_copy_to_user(fd, dst, src, len);
 }
 EXPORT_SYMBOL_GPL(rtnet_put_arg);
+
+#ifdef CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP
+int rtnet_put_cmsg(struct rtdm_fd *fd, struct user_msghdr *msg, int level,
+					int type, int len, void *data)
+{
+	struct cmsghdr __user *cm = msg->msg_control;

kernel/drivers/net/stack/socket.c int rtnet_put_arg(struct rtdm_fd *fd, void *dst, const void *src, size_t len) · line 420

Quoted 10 lines
kernel/drivers/net/stack/socket.c +10 -0
+	struct cmsghdr cmhdr;
+	int cmlen = CMSG_LEN(len);
+	int err;
+
+	if (MSG_CMSG_COMPAT & msg->msg_flags)
+		return 0; /* XXX: return error? check spec. */
+
+	if ((cm == NULL) || (msg->msg_controllen < sizeof(*cm))) {
+		msg->msg_flags |= MSG_CTRUNC;
+		return 0; /* XXX: return error? check spec. */

Please resolve the "XXX" cases. The first one indicates that this does not work with 32-bit compat apps.

Quoted 5 lines
kernel/drivers/net/stack/socket.c +5 -0
+	}
+	if (msg->msg_controllen < cmlen) {
+		msg->msg_flags |= MSG_CTRUNC;
+		cmlen = msg->msg_controllen;
+	}

kernel/drivers/net/stack/socket.c int rtnet_put_arg(struct rtdm_fd *fd, void *dst, const void *src, size_t len) · line 435

Quoted 10 lines
kernel/drivers/net/stack/socket.c +10 -0
+	cmhdr.cmsg_level = level;
+	cmhdr.cmsg_type = type;
+	cmhdr.cmsg_len = cmlen;
+
+	err = -EFAULT;
+
+	if (!rtdm_fd_is_user(fd)) {
+		memcpy(cm, &cmhdr, sizeof(struct cmsghdr));
+		memcpy(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr));
+		err = 0;

This path does not update msg like below.

kernel/drivers/net/stack/socket.c int rtnet_put_arg(struct rtdm_fd *fd, void *dst, const void *src, size_t len) · line 441

Quoted 6 lines
kernel/drivers/net/stack/socket.c +6 -0
+	} else {
+		if (rtdm_copy_to_user(fd, cm, &cmhdr, sizeof(struct cmsghdr)))
+			goto out;
+		if (rtdm_copy_to_user(fd, CMSG_DATA(cm), data,
+					cmlen - sizeof(struct cmsghdr)))
+			goto out;

Could also be folded into a "if (... || ...)".

Quoted 4 lines
kernel/drivers/net/stack/socket.c +3 -0
+		cmlen = CMSG_SPACE(len);
+		if (msg->msg_controllen < cmlen)
+			cmlen = msg->msg_controllen;

kernel/drivers/net/stack/socket.c int rtnet_put_arg(struct rtdm_fd *fd, void *dst, const void *src, size_t len) · line 454

Quoted 10 lines
kernel/drivers/net/stack/socket.c +10 -0
+		msg->msg_control += cmlen;
+		msg->msg_controllen -= cmlen;
+		err = 0;
+	}
+out:
+	return err;
+}
+EXPORT_SYMBOL(rtdm_put_cmsg);
+
+#endif /* CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP */

Jan

Signature · 2 lines
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux
#3 lore raw
Quoted 9 lines · Jan Kiszka
Quoted 4 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.

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.

One of my question on v1 is still open: How does Linux handle stamping the context of fragmented UDP and TCP packets? And given that you enable it for those protocols as well, we should likely test that too.

I thought I already gave an answer to this. Sorry.
Linux does not handle time stamping of UDP and TCP as far as I understand.
I certainly plan to use the UDP timestamping feature at some point in the future, but maybe I can exclude UDP and TCP for the moment, and stick to af_packet which has be tested by myself. I'll resubmit another patch with (at least) udp support whenever I need it and have time to test it.

What do you think ?

François

Quoted 206 lines
Quoted 18 lines
---
 kernel/drivers/net/stack/Kconfig              |  8 +++
 .../drivers/net/stack/include/rtnet_socket.h  |  7 +++
 kernel/drivers/net/stack/ipv4/tcp/tcp.c       |  8 +++
 kernel/drivers/net/stack/ipv4/udp/udp.c       |  7 +++
 kernel/drivers/net/stack/packet/af_packet.c   |  7 +++
 kernel/drivers/net/stack/socket.c             | 59 +++++++++++++++++++
 6 files changed, 96 insertions(+)
kernel/drivers/net/stack/Kconfig +0 -0
index 830cec5ad..f8ee0f1ad 100644
@@ -12,6 +12,14 @@ config XENO_DRIVERS_NET_RX_FIFO_SIZE
     of two! Effectively, only CONFIG_RTNET_RX_FIFO_SIZE-1 slots will

kernel/drivers/net/stack/Kconfig config XENO_DRIVERS_NET_RX_FIFO_SIZE · line 22

Quoted 10 lines
kernel/drivers/net/stack/Kconfig +8 -0
     be usable.
 
+config XENO_DRIVERS_NET_PACKET_TIMESTAMP
+    bool "Enable packet timestamping (SO_TIMESTAMPNS)"
+    depends on XENO_DRIVERS_NET
+    ---help---
+    Enable userland access to low level packet timestamps using SO_TIMESTAMPNS
+    ioctl on socket. Timestamp are then returned in recvmsg calls in msg_control
+    structure inside msghdr structure.
+

Why do we need a config option? All it adds in the off-case is a single bit test.

Quoted 113 lines
kernel/drivers/net/stack/Kconfig +0 -0
 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 +7 -0
index d2caab649..dc488a58a 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,12 @@ 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 +8 -0
index d8c189c88..c8b21c521 100644
@@ -2027,6 +2027,14 @@ 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 +7 -0
index 546b35855..ac6448027 100644
@@ -463,6 +463,13 @@ 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 +7 -0
index cc7487303..10e8113fe 100644
@@ -364,6 +364,13 @@ 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 +15 -0
index f030663be..5a9702975 100644
@@ -273,6 +273,15 @@ int rt_socket_if_ioctl(struct rtdm_fd *fd, int request, void __user *arg)
 		return rtnet_put_arg(fd, &u_ifc->ifc_len, &size, sizeof(size));
 	}
 
+#ifdef CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP
+	if (request == SO_TIMESTAMPNS) {
+		struct rtsocket *sock = rtdm_fd_to_private(fd);
+
+		set_bit(SOCKET_FLAG_TIMESTAMP, &sock->flags);
+		return 0;
+	}
+#endif
+
 	u_ifr = arg;
 	ifr = rtnet_get_arg(fd, &_ifr, u_ifr, sizeof(_ifr));
 	if (IS_ERR(ifr))
@@ -393,3 +402,53 @@ int rtnet_put_arg(struct rtdm_fd *fd, void *dst, const void *src, size_t len)
 	return rtdm_copy_to_user(fd, dst, src, len);
 }
 EXPORT_SYMBOL_GPL(rtnet_put_arg);
+
+#ifdef CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP
+int rtnet_put_cmsg(struct rtdm_fd *fd, struct user_msghdr *msg, int level,
+					int type, int len, void *data)
+{
+	struct cmsghdr __user *cm = msg->msg_control;

kernel/drivers/net/stack/socket.c int rtnet_put_arg(struct rtdm_fd *fd, void *dst, const void *src, size_t len) · line 420

Quoted 10 lines
kernel/drivers/net/stack/socket.c +10 -0
+	struct cmsghdr cmhdr;
+	int cmlen = CMSG_LEN(len);
+	int err;
+
+	if (MSG_CMSG_COMPAT & msg->msg_flags)
+		return 0; /* XXX: return error? check spec. */
+
+	if ((cm == NULL) || (msg->msg_controllen < sizeof(*cm))) {
+		msg->msg_flags |= MSG_CTRUNC;
+		return 0; /* XXX: return error? check spec. */

Please resolve the "XXX" cases. The first one indicates that this does not work with 32-bit compat apps.

Quoted 5 lines
kernel/drivers/net/stack/socket.c +5 -0
+	}
+	if (msg->msg_controllen < cmlen) {
+		msg->msg_flags |= MSG_CTRUNC;
+		cmlen = msg->msg_controllen;
+	}

kernel/drivers/net/stack/socket.c int rtnet_put_arg(struct rtdm_fd *fd, void *dst, const void *src, size_t len) · line 435

Quoted 10 lines
kernel/drivers/net/stack/socket.c +10 -0
+	cmhdr.cmsg_level = level;
+	cmhdr.cmsg_type = type;
+	cmhdr.cmsg_len = cmlen;
+
+	err = -EFAULT;
+
+	if (!rtdm_fd_is_user(fd)) {
+		memcpy(cm, &cmhdr, sizeof(struct cmsghdr));
+		memcpy(CMSG_DATA(cm), data, cmlen - sizeof(struct cmsghdr));
+		err = 0;

This path does not update msg like below.

kernel/drivers/net/stack/socket.c int rtnet_put_arg(struct rtdm_fd *fd, void *dst, const void *src, size_t len) · line 441

Quoted 6 lines
kernel/drivers/net/stack/socket.c +6 -0
+	} else {
+		if (rtdm_copy_to_user(fd, cm, &cmhdr, sizeof(struct cmsghdr)))
+			goto out;
+		if (rtdm_copy_to_user(fd, CMSG_DATA(cm), data,
+					cmlen - sizeof(struct cmsghdr)))
+			goto out;

Could also be folded into a "if (... || ...)".

Quoted 4 lines
kernel/drivers/net/stack/socket.c +3 -0
+		cmlen = CMSG_SPACE(len);
+		if (msg->msg_controllen < cmlen)
+			cmlen = msg->msg_controllen;

kernel/drivers/net/stack/socket.c int rtnet_put_arg(struct rtdm_fd *fd, void *dst, const void *src, size_t len) · line 454

Quoted 10 lines
kernel/drivers/net/stack/socket.c +10 -0
+		msg->msg_control += cmlen;
+		msg->msg_controllen -= cmlen;
+		err = 0;
+	}
+out:
+	return err;
+}
+EXPORT_SYMBOL(rtdm_put_cmsg);
+
+#endif /* CONFIG_XENO_DRIVERS_NET_PACKET_TIMESTAMP */

Jan

--
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux

#4 lore raw
Quoted 6 lines · François Legal
Quoted 4 lines · Jan Kiszka
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.

Quoted 8 lines
Quoted 5 lines
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.

One of my question on v1 is still open: How does Linux handle stamping the context of fragmented UDP and TCP packets? And given that you enable it for those protocols as well, we should likely test that too.

I thought I already gave an answer to this. Sorry.
Linux does not handle time stamping of UDP and TCP as far as I understand.

Does Linux return an error when you try to enable this for UDP or TCP sockets?

Quoted 3 lines

I certainly plan to use the UDP timestamping feature at some point in the future, but maybe I can exclude UDP and TCP for the moment, and stick to af_packet which has be tested by myself. I'll resubmit another patch with (at least) udp support whenever I need it and have time to test it.

What do you think ?

We need at least a plan how to evolve that interface. Actually doing that can then happen in steps, yes.

Jan

Signature · 2 lines
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux
#5 lore raw
Quoted 13 lines · Jan Kiszka
Quoted 12 lines · François Legal
Quoted 9 lines · Jan Kiszka
Quoted 4 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.

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.

One of my question on v1 is still open: How does Linux handle stamping the context of fragmented UDP and TCP packets? And given that you enable it for those protocols as well, we should likely test that too.

Quoted 5 lines
Quoted 2 lines

I thought I already gave an answer to this. Sorry.
Linux does not handle time stamping of UDP and TCP as far as I understand.

Does Linux return an error when you try to enable this for UDP or TCP sockets?

Alright so I rechecked everything again. Linux does not return an error when requesting timestamping for UDP or TCP (or any other kind of socket it seems).

Despite my previous assertion, timestamping is supported for UDP and if I understood the code correctly, only one timestamp is returned in any case (the time stamp of the last received datagram). This is done in reassembly queues, where the queue always keep the timestamp of the last received datagram, and when doing the reassembly, this last timestamp is reported to the re created skb.

For TCP it is definitely not supported.

So I should modify the patch to make it comply with what linux does I guess ?

Quoted 13 lines
Quoted 4 lines

I certainly plan to use the UDP timestamping feature at some point in the future, but maybe I can exclude UDP and TCP for the moment, and stick to af_packet which has be tested by myself. I'll resubmit another patch with (at least) udp support whenever I need it and have time to test it.

What do you think ?

We need at least a plan how to evolve that interface. Actually doing that can then happen in steps, yes.

Jan

--
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux

#6 lore raw
Quoted 21 lines · François Legal
Quoted 13 lines · Jan Kiszka
Quoted 12 lines · François Legal
Quoted 9 lines · Jan Kiszka
Quoted 4 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.

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.

One of my question on v1 is still open: How does Linux handle stamping the context of fragmented UDP and TCP packets? And given that you enable it for those protocols as well, we should likely test that too.

Quoted 5 lines
Quoted 2 lines

I thought I already gave an answer to this. Sorry.
Linux does not handle time stamping of UDP and TCP as far as I understand.

Does Linux return an error when you try to enable this for UDP or TCP sockets?

Quoted 8 lines

Alright so I rechecked everything again. Linux does not return an error when requesting timestamping for UDP or TCP (or any other kind of socket it seems).

Despite my previous assertion, timestamping is supported for UDP and if I understood the code correctly, only one timestamp is returned in any case (the time stamp of the last received datagram). This is done in reassembly queues, where the queue always keep the timestamp of the last received datagram, and when doing the reassembly, this last timestamp is reported to the re created skb.

For TCP it is definitely not supported.

So I should modify the patch to make it comply with what linux does I guess ?

Yes, unless there is any good (RT/Xenomai-specific) reason speaking against alignment, we should do this - least surprise.

Jan

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