History

selftests: txtimestamp: add SCM_TS_OPT_ID test

Extend txtimestamp test to run with fixed tskey using SCM_TS_OPT_ID control message for all types of sockets.

Vadim Fedorenko committed 2 years ago

Commit a89568e1 parent 822b5bc3 files · +43 −15Browse files

Unified · Split

3 files
  1. tools/include/uapi/asm-generic/socket.h +2 −0
  2. tools/testing/selftests/net/txtimestamp.c +34 −10
  3. tools/testing/selftests/net/txtimestamp.sh +7 −5
tools/include/uapi/asm-generic/socket.h+2−0
@@ -124,6 +124,8 @@ · 123 unmodified lines
124124 #define SO_PASSPIDFD 76
125125 #define SO_PEERPIDFD 77
126126
127+#define SCM_TS_OPT_ID 78
128+
127129 #if !defined(__KERNEL__)
128130
129131 #if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))
tools/testing/selftests/net/txtimestamp.c+34−10
@@ -77,6 +77,8 @@ static bool cfg_epollet; · 76 unmodified lines
7777 static bool cfg_do_listen;
7878 static uint16_t dest_port = 9000;
7979 static bool cfg_print_nsec;
80+static uint32_t ts_opt_id;
81+static bool cfg_use_cmsg_opt_id;
8082
8183 static struct sockaddr_in daddr;
8284 static struct sockaddr_in6 daddr6;
@@ -136,12 +138,13 @@ static void validate_key(int tskey, int tstype) · 53 unmodified lines
136138 /* compare key for each subsequent request
137139 * must only test for one type, the first one requested
138140 */
139− if (saved_tskey == -1)
141+ if (saved_tskey == -1 || cfg_use_cmsg_opt_id)
140142 saved_tskey_type = tstype;
141143 else if (saved_tskey_type != tstype)
142144 return;
143145
144146 stepsize = cfg_proto == SOCK_STREAM ? cfg_payload_len : 1;
147+ stepsize = cfg_use_cmsg_opt_id ? 0 : stepsize;
145148 if (tskey != saved_tskey + stepsize) {
146149 fprintf(stderr, "ERROR: key %d, expected %d\n",
147150 tskey, saved_tskey + stepsize);
@@ -484,7 +487,7 @@ static void fill_header_udp(void *p, bool is_ipv4) · 336 unmodified lines
484487
485488 static void do_test(int family, unsigned int report_opt)
486489 {
487− char control[CMSG_SPACE(sizeof(uint32_t))];
490+ char control[2 * CMSG_SPACE(sizeof(uint32_t))];
488491 struct sockaddr_ll laddr;
489492 unsigned int sock_opt;
490493 struct cmsghdr *cmsg;
@@ -624,18 +627,32 @@ static void do_test(int family, unsigned int report_opt) · 133 unmodified lines
624627 msg.msg_iov = &iov;
625628 msg.msg_iovlen = 1;
626629
627− if (cfg_use_cmsg) {
630+ if (cfg_use_cmsg || cfg_use_cmsg_opt_id) {
628631 memset(control, 0, sizeof(control));
629632
630633 msg.msg_control = control;
631− msg.msg_controllen = sizeof(control);
634+ msg.msg_controllen = cfg_use_cmsg * CMSG_SPACE(sizeof(uint32_t));
635+ msg.msg_controllen += cfg_use_cmsg_opt_id * CMSG_SPACE(sizeof(uint32_t));
632636
633− cmsg = CMSG_FIRSTHDR(&msg);
634− cmsg->cmsg_level = SOL_SOCKET;
635− cmsg->cmsg_type = SO_TIMESTAMPING;
636− cmsg->cmsg_len = CMSG_LEN(sizeof(uint32_t));
637+ cmsg = NULL;
638+ if (cfg_use_cmsg) {
639+ cmsg = CMSG_FIRSTHDR(&msg);
640+ cmsg->cmsg_level = SOL_SOCKET;
641+ cmsg->cmsg_type = SO_TIMESTAMPING;
642+ cmsg->cmsg_len = CMSG_LEN(sizeof(uint32_t));
643+
644+ *((uint32_t *)CMSG_DATA(cmsg)) = report_opt;
645+ }
646+ if (cfg_use_cmsg_opt_id) {
647+ cmsg = cmsg ? CMSG_NXTHDR(&msg, cmsg) : CMSG_FIRSTHDR(&msg);
648+ cmsg->cmsg_level = SOL_SOCKET;
649+ cmsg->cmsg_type = SCM_TS_OPT_ID;
650+ cmsg->cmsg_len = CMSG_LEN(sizeof(uint32_t));
651+
652+ *((uint32_t *)CMSG_DATA(cmsg)) = ts_opt_id;
653+ saved_tskey = ts_opt_id;
654+ }
637655
638− *((uint32_t *) CMSG_DATA(cmsg)) = report_opt;
639656 }
640657
641658 val = sendmsg(fd, &msg, 0);
@@ -685,6 +702,7 @@ static void __attribute__((noreturn)) usage(const char *filepath) · 43 unmodified lines
685702 " -L listen on hostname and port\n"
686703 " -n: set no-payload option\n"
687704 " -N: print timestamps and durations in nsec (instead of usec)\n"
705+ " -o N: use SCM_TS_OPT_ID control message to provide N as tskey\n"
688706 " -p N: connect to port N\n"
689707 " -P: use PF_PACKET\n"
690708 " -r: use raw\n"
@@ -705,7 +723,7 @@ static void parse_opt(int argc, char **argv) · 14 unmodified lines
705723 int c;
706724
707725 while ((c = getopt(argc, argv,
708− "46bc:CeEFhIl:LnNp:PrRS:t:uv:V:x")) != -1) {
726+ "46bc:CeEFhIl:LnNo:p:PrRS:t:uv:V:x")) != -1) {
709727 switch (c) {
710728 case '4':
711729 do_ipv6 = 0;
@@ -746,6 +764,10 @@ static void parse_opt(int argc, char **argv) · 34 unmodified lines
746764 case 'N':
747765 cfg_print_nsec = true;
748766 break;
767+ case 'o':
768+ ts_opt_id = strtoul(optarg, NULL, 10);
769+ cfg_use_cmsg_opt_id = true;
770+ break;
749771 case 'p':
750772 dest_port = strtoul(optarg, NULL, 10);
751773 break;
@@ -803,6 +825,8 @@ static void parse_opt(int argc, char **argv) · 51 unmodified lines
803825 error(1, 0, "cannot ask for pktinfo over pf_packet");
804826 if (cfg_busy_poll && cfg_use_epoll)
805827 error(1, 0, "pass epoll or busy_poll, not both");
828+ if (cfg_proto == SOCK_STREAM && cfg_use_cmsg_opt_id)
829+ error(1, 0, "TCP sockets don't support SCM_TS_OPT_ID");
806830
807831 if (optind != argc - 1)
808832 error(1, 0, "missing required hostname argument");
tools/testing/selftests/net/txtimestamp.sh+7−5
@@ -37,11 +37,13 @@ run_test_v4v6() { · 36 unmodified lines
3737 run_test_tcpudpraw() {
3838 local -r args=$@
3939
40− run_test_v4v6 ${args} # tcp
41− run_test_v4v6 ${args} -u # udp
42− run_test_v4v6 ${args} -r # raw
43− run_test_v4v6 ${args} -R # raw (IPPROTO_RAW)
44− run_test_v4v6 ${args} -P # pf_packet
40+ run_test_v4v6 ${args} # tcp
41+ run_test_v4v6 ${args} -u # udp
42+ run_test_v4v6 ${args} -u -o 42 # udp with fixed tskey
43+ run_test_v4v6 ${args} -r # raw
44+ run_test_v4v6 ${args} -r -o 42 # raw
45+ run_test_v4v6 ${args} -R # raw (IPPROTO_RAW)
46+ run_test_v4v6 ${args} -P # pf_packet
4547 }
4648
4749 run_test_all() {