netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jakub Kicinski <kuba@kernel.org>
To: davem@davemloft.net
Cc: netdev@vger.kernel.org, willemb@google.com, lorenzo@google.com,
	maze@google.com, dsahern@kernel.org, yoshfuji@linux-ipv6.org,
	shuah@kernel.org, linux-kselftest@vger.kernel.org,
	Jakub Kicinski <kuba@kernel.org>
Subject: [PATCH net-next 09/11] selftests: net: cmsg_sender: support setting SO_TXTIME
Date: Wed,  9 Feb 2022 16:36:47 -0800	[thread overview]
Message-ID: <20220210003649.3120861-10-kuba@kernel.org> (raw)
In-Reply-To: <20220210003649.3120861-1-kuba@kernel.org>

Add ability to send delayed packets.

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
---
 tools/testing/selftests/net/cmsg_sender.c | 49 +++++++++++++++++++++--
 1 file changed, 46 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/net/cmsg_sender.c b/tools/testing/selftests/net/cmsg_sender.c
index c7586a4b0361..5a722baa108b 100644
--- a/tools/testing/selftests/net/cmsg_sender.c
+++ b/tools/testing/selftests/net/cmsg_sender.c
@@ -6,9 +6,11 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <time.h>
 #include <unistd.h>
 #include <linux/icmp.h>
 #include <linux/icmpv6.h>
+#include <linux/net_tstamp.h>
 #include <linux/types.h>
 #include <linux/udp.h>
 #include <sys/socket.h>
@@ -23,6 +25,8 @@ enum {
 	ERN_SOCK_CREATE,
 	ERN_RESOLVE,
 	ERN_CMSG_WR,
+	ERN_SOCKOPT,
+	ERN_GETTIME,
 };
 
 struct options {
@@ -41,6 +45,10 @@ struct options {
 		bool ena;
 		unsigned int val;
 	} mark;
+	struct {
+		bool ena;
+		unsigned int delay;
+	} txtime;
 } opt = {
 	.sock = {
 		.family	= AF_UNSPEC,
@@ -49,6 +57,8 @@ struct options {
 	},
 };
 
+static struct timespec time_start_mono;
+
 static void __attribute__((noreturn)) cs_usage(const char *bin)
 {
 	printf("Usage: %s [opts] <dst host> <dst port / service>\n", bin);
@@ -60,6 +70,7 @@ static void __attribute__((noreturn)) cs_usage(const char *bin)
 	       "\n"
 	       "\t\t-m val  Set SO_MARK with given value\n"
 	       "\t\t-M val  Set SO_MARK via setsockopt\n"
+	       "\t\t-d val  Set SO_TXTIME with given delay (usec)\n"
 	       "");
 	exit(ERN_HELP);
 }
@@ -68,7 +79,7 @@ static void cs_parse_args(int argc, char *argv[])
 {
 	char o;
 
-	while ((o = getopt(argc, argv, "46sp:m:M:")) != -1) {
+	while ((o = getopt(argc, argv, "46sp:m:M:d:")) != -1) {
 		switch (o) {
 		case 's':
 			opt.silent_send = true;
@@ -91,6 +102,7 @@ static void cs_parse_args(int argc, char *argv[])
 				cs_usage(argv[0]);
 			}
 			break;
+
 		case 'm':
 			opt.mark.ena = true;
 			opt.mark.val = atoi(optarg);
@@ -98,6 +110,10 @@ static void cs_parse_args(int argc, char *argv[])
 		case 'M':
 			opt.sockopt.mark = atoi(optarg);
 			break;
+		case 'd':
+			opt.txtime.ena = true;
+			opt.txtime.delay = atoi(optarg);
+			break;
 		}
 	}
 
@@ -109,7 +125,7 @@ static void cs_parse_args(int argc, char *argv[])
 }
 
 static void
-cs_write_cmsg(struct msghdr *msg, char *cbuf, size_t cbuf_sz)
+cs_write_cmsg(int fd, struct msghdr *msg, char *cbuf, size_t cbuf_sz)
 {
 	struct cmsghdr *cmsg;
 	size_t cmsg_len;
@@ -128,6 +144,30 @@ cs_write_cmsg(struct msghdr *msg, char *cbuf, size_t cbuf_sz)
 		cmsg->cmsg_len = CMSG_LEN(sizeof(__u32));
 		*(__u32 *)CMSG_DATA(cmsg) = opt.mark.val;
 	}
+	if (opt.txtime.ena) {
+		struct sock_txtime so_txtime = {
+			.clockid = CLOCK_MONOTONIC,
+		};
+		__u64 txtime;
+
+		if (setsockopt(fd, SOL_SOCKET, SO_TXTIME,
+			       &so_txtime, sizeof(so_txtime)))
+			error(ERN_SOCKOPT, errno, "setsockopt TXTIME");
+
+		txtime = time_start_mono.tv_sec * (1000ULL * 1000 * 1000) +
+			 time_start_mono.tv_nsec +
+			 opt.txtime.delay * 1000;
+
+		cmsg = (struct cmsghdr *)(cbuf + cmsg_len);
+		cmsg_len += CMSG_SPACE(sizeof(txtime));
+		if (cbuf_sz < cmsg_len)
+			error(ERN_CMSG_WR, EFAULT, "cmsg buffer too small");
+
+		cmsg->cmsg_level = SOL_SOCKET;
+		cmsg->cmsg_type = SCM_TXTIME;
+		cmsg->cmsg_len = CMSG_LEN(sizeof(txtime));
+		memcpy(CMSG_DATA(cmsg), &txtime, sizeof(txtime));
+	}
 
 	if (cmsg_len)
 		msg->msg_controllen = cmsg_len;
@@ -187,6 +227,9 @@ int main(int argc, char *argv[])
 		       &opt.sockopt.mark, sizeof(opt.sockopt.mark)))
 		error(ERN_SOCKOPT, errno, "setsockopt SO_MARK");
 
+	if (clock_gettime(CLOCK_MONOTONIC, &time_start_mono))
+		error(ERN_GETTIME, errno, "gettime MONOTINIC");
+
 	iov[0].iov_base = buf;
 	iov[0].iov_len = sizeof(buf);
 
@@ -196,7 +239,7 @@ int main(int argc, char *argv[])
 	msg.msg_iov = iov;
 	msg.msg_iovlen = 1;
 
-	cs_write_cmsg(&msg, cbuf, sizeof(cbuf));
+	cs_write_cmsg(fd, &msg, cbuf, sizeof(cbuf));
 
 	err = sendmsg(fd, &msg, 0);
 	if (err < 0) {
-- 
2.34.1


  parent reply	other threads:[~2022-02-10  2:02 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-10  0:36 [PATCH net-next 00/11] net: ping6: support basic socket cmsgs Jakub Kicinski
2022-02-10  0:36 ` [PATCH net-next 01/11] net: ping6: remove a pr_debug() statement Jakub Kicinski
2022-02-10  5:30   ` David Ahern
2022-02-10  0:36 ` [PATCH net-next 02/11] net: ping6: support packet timestamping Jakub Kicinski
2022-02-10  0:44   ` Maciej Żenczykowski
2022-02-10  2:17     ` Jakub Kicinski
2022-02-10  5:30   ` David Ahern
2022-02-10  0:36 ` [PATCH net-next 03/11] net: ping6: support setting socket options via cmsg Jakub Kicinski
2022-02-10  5:30   ` David Ahern
2022-02-10  0:36 ` [PATCH net-next 04/11] selftests: net: rename cmsg_so_mark Jakub Kicinski
2022-02-10  0:36 ` [PATCH net-next 05/11] selftests: net: make cmsg_so_mark ready for more options Jakub Kicinski
2022-02-10  0:36 ` [PATCH net-next 06/11] selftests: net: cmsg_sender: support icmp and raw sockets Jakub Kicinski
2022-02-10  0:36 ` [PATCH net-next 07/11] selftests: net: cmsg_so_mark: test ICMP and RAW sockets Jakub Kicinski
2022-02-10  0:36 ` [PATCH net-next 08/11] selftests: net: cmsg_so_mark: test with SO_MARK set by setsockopt Jakub Kicinski
2022-02-10  0:36 ` Jakub Kicinski [this message]
2022-02-10  0:36 ` [PATCH net-next 10/11] selftests: net: cmsg_sender: support Tx timestamping Jakub Kicinski
2022-02-10  0:36 ` [PATCH net-next 11/11] selftests: net: test standard socket cmsgs across UDP and ICMP sockets Jakub Kicinski
2022-02-10 15:30 ` [PATCH net-next 00/11] net: ping6: support basic socket cmsgs patchwork-bot+netdevbpf
2022-02-10 16:08 ` Willem de Bruijn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220210003649.3120861-10-kuba@kernel.org \
    --to=kuba@kernel.org \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=lorenzo@google.com \
    --cc=maze@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=shuah@kernel.org \
    --cc=willemb@google.com \
    --cc=yoshfuji@linux-ipv6.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).