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
124#define SO_PASSPIDFD 76124#define SO_PASSPIDFD 76
125#define SO_PEERPIDFD 77125#define SO_PEERPIDFD 77
126 126
127#define SCM_TS_OPT_ID 78
128
127#if !defined(__KERNEL__)129#if !defined(__KERNEL__)
128 130
129#if __BITS_PER_LONG == 64 || (defined(__x86_64__) && defined(__ILP32__))131#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
77static bool cfg_do_listen;77static bool cfg_do_listen;
78static uint16_t dest_port = 9000;78static uint16_t dest_port = 9000;
79static bool cfg_print_nsec;79static bool cfg_print_nsec;
80static uint32_t ts_opt_id;
81static bool cfg_use_cmsg_opt_id;
80 82
81static struct sockaddr_in daddr;83static struct sockaddr_in daddr;
82static struct sockaddr_in6 daddr6;84static struct sockaddr_in6 daddr6;
@@ -136,12 +138,13 @@ static void validate_key(int tskey, int tstype) · 53 unmodified lines
136 /* compare key for each subsequent request138 /* compare key for each subsequent request
137 * must only test for one type, the first one requested139 * must only test for one type, the first one requested
138 */140 */
139 if (saved_tskey == -1)141 if (saved_tskey == -1 || cfg_use_cmsg_opt_id)
140 saved_tskey_type = tstype;142 saved_tskey_type = tstype;
141 else if (saved_tskey_type != tstype)143 else if (saved_tskey_type != tstype)
142 return;144 return;
143 145
144 stepsize = cfg_proto == SOCK_STREAM ? cfg_payload_len : 1;146 stepsize = cfg_proto == SOCK_STREAM ? cfg_payload_len : 1;
147 stepsize = cfg_use_cmsg_opt_id ? 0 : stepsize;
145 if (tskey != saved_tskey + stepsize) {148 if (tskey != saved_tskey + stepsize) {
146 fprintf(stderr, "ERROR: key %d, expected %d\n",149 fprintf(stderr, "ERROR: key %d, expected %d\n",
147 tskey, saved_tskey + stepsize);150 tskey, saved_tskey + stepsize);
@@ -484,7 +487,7 @@ static void fill_header_udp(void *p, bool is_ipv4) · 336 unmodified lines
484 487
485static void do_test(int family, unsigned int report_opt)488static void do_test(int family, unsigned int report_opt)
486{489{
487 char control[CMSG_SPACE(sizeof(uint32_t))];490 char control[2 * CMSG_SPACE(sizeof(uint32_t))];
488 struct sockaddr_ll laddr;491 struct sockaddr_ll laddr;
489 unsigned int sock_opt;492 unsigned int sock_opt;
490 struct cmsghdr *cmsg;493 struct cmsghdr *cmsg;
@@ -624,18 +627,32 @@ static void do_test(int family, unsigned int report_opt) · 133 unmodified lines
624 msg.msg_iov = &iov;627 msg.msg_iov = &iov;
625 msg.msg_iovlen = 1;628 msg.msg_iovlen = 1;
626 629
627 if (cfg_use_cmsg) {630 if (cfg_use_cmsg || cfg_use_cmsg_opt_id) {
628 memset(control, 0, sizeof(control));631 memset(control, 0, sizeof(control));
629 632
630 msg.msg_control = control;633 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));
632 636
633 cmsg = CMSG_FIRSTHDR(&msg);637 cmsg = NULL;
634 cmsg->cmsg_level = SOL_SOCKET;638 if (cfg_use_cmsg) {
635 cmsg->cmsg_type = SO_TIMESTAMPING;639 cmsg = CMSG_FIRSTHDR(&msg);
636 cmsg->cmsg_len = CMSG_LEN(sizeof(uint32_t));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 }
637 655
638 *((uint32_t *) CMSG_DATA(cmsg)) = report_opt;
639 }656 }
640 657
641 val = sendmsg(fd, &msg, 0);658 val = sendmsg(fd, &msg, 0);
@@ -685,6 +702,7 @@ static void __attribute__((noreturn)) usage(const char *filepath) · 43 unmodified lines
685 " -L listen on hostname and port\n"702 " -L listen on hostname and port\n"
686 " -n: set no-payload option\n"703 " -n: set no-payload option\n"
687 " -N: print timestamps and durations in nsec (instead of usec)\n"704 " -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"
688 " -p N: connect to port N\n"706 " -p N: connect to port N\n"
689 " -P: use PF_PACKET\n"707 " -P: use PF_PACKET\n"
690 " -r: use raw\n"708 " -r: use raw\n"
@@ -705,7 +723,7 @@ static void parse_opt(int argc, char **argv) · 14 unmodified lines
705 int c;723 int c;
706 724
707 while ((c = getopt(argc, argv,725 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) {
709 switch (c) {727 switch (c) {
710 case '4':728 case '4':
711 do_ipv6 = 0;729 do_ipv6 = 0;
@@ -746,6 +764,10 @@ static void parse_opt(int argc, char **argv) · 34 unmodified lines
746 case 'N':764 case 'N':
747 cfg_print_nsec = true;765 cfg_print_nsec = true;
748 break;766 break;
767 case 'o':
768 ts_opt_id = strtoul(optarg, NULL, 10);
769 cfg_use_cmsg_opt_id = true;
770 break;
749 case 'p':771 case 'p':
750 dest_port = strtoul(optarg, NULL, 10);772 dest_port = strtoul(optarg, NULL, 10);
751 break;773 break;
@@ -803,6 +825,8 @@ static void parse_opt(int argc, char **argv) · 51 unmodified lines
803 error(1, 0, "cannot ask for pktinfo over pf_packet");825 error(1, 0, "cannot ask for pktinfo over pf_packet");
804 if (cfg_busy_poll && cfg_use_epoll)826 if (cfg_busy_poll && cfg_use_epoll)
805 error(1, 0, "pass epoll or busy_poll, not both");827 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");
806 830
807 if (optind != argc - 1)831 if (optind != argc - 1)
808 error(1, 0, "missing required hostname argument");832 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
37run_test_tcpudpraw() {37run_test_tcpudpraw() {
38 local -r args=$@38 local -r args=$@
39 39
40 run_test_v4v6 ${args} # tcp40 run_test_v4v6 ${args} # tcp
41 run_test_v4v6 ${args} -u # udp41 run_test_v4v6 ${args} -u # udp
42 run_test_v4v6 ${args} -r # raw42 run_test_v4v6 ${args} -u -o 42 # udp with fixed tskey
43 run_test_v4v6 ${args} -R # raw (IPPROTO_RAW)43 run_test_v4v6 ${args} -r # raw
44 run_test_v4v6 ${args} -P # pf_packet44 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
45}47}
46 48
47run_test_all() {49run_test_all() {