linux/tools/testing/selftests/networking/timestamping/rxtimestamp.c
<<
>>
Prefs
   1#include <errno.h>
   2#include <error.h>
   3#include <getopt.h>
   4#include <stdbool.h>
   5#include <stdio.h>
   6#include <stdlib.h>
   7#include <string.h>
   8#include <unistd.h>
   9
  10#include <sys/time.h>
  11#include <sys/socket.h>
  12#include <sys/select.h>
  13#include <sys/ioctl.h>
  14#include <arpa/inet.h>
  15#include <net/if.h>
  16
  17#include <asm/types.h>
  18#include <linux/net_tstamp.h>
  19#include <linux/errqueue.h>
  20
  21#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
  22
  23struct options {
  24        int so_timestamp;
  25        int so_timestampns;
  26        int so_timestamping;
  27};
  28
  29struct tstamps {
  30        bool tstamp;
  31        bool tstampns;
  32        bool swtstamp;
  33        bool hwtstamp;
  34};
  35
  36struct socket_type {
  37        char *friendly_name;
  38        int type;
  39        int protocol;
  40        bool enabled;
  41};
  42
  43struct test_case {
  44        struct options sockopt;
  45        struct tstamps expected;
  46        bool enabled;
  47};
  48
  49struct sof_flag {
  50        int mask;
  51        char *name;
  52};
  53
  54static struct sof_flag sof_flags[] = {
  55#define SOF_FLAG(f) { f, #f }
  56        SOF_FLAG(SOF_TIMESTAMPING_SOFTWARE),
  57        SOF_FLAG(SOF_TIMESTAMPING_RX_SOFTWARE),
  58        SOF_FLAG(SOF_TIMESTAMPING_RX_HARDWARE),
  59};
  60
  61static struct socket_type socket_types[] = {
  62        { "ip",         SOCK_RAW,       IPPROTO_EGP },
  63        { "udp",        SOCK_DGRAM,     IPPROTO_UDP },
  64        { "tcp",        SOCK_STREAM,    IPPROTO_TCP },
  65};
  66
  67static struct test_case test_cases[] = {
  68        { {}, {} },
  69        {
  70                { so_timestamp: 1 },
  71                { tstamp: true }
  72        },
  73        {
  74                { so_timestampns: 1 },
  75                { tstampns: true }
  76        },
  77        {
  78                { so_timestamp: 1, so_timestampns: 1 },
  79                { tstampns: true }
  80        },
  81        {
  82                { so_timestamping: SOF_TIMESTAMPING_RX_SOFTWARE },
  83                {}
  84        },
  85        {
  86                /* Loopback device does not support hw timestamps. */
  87                { so_timestamping: SOF_TIMESTAMPING_RX_HARDWARE },
  88                {}
  89        },
  90        {
  91                { so_timestamping: SOF_TIMESTAMPING_SOFTWARE },
  92                {}
  93        },
  94        {
  95                { so_timestamping: SOF_TIMESTAMPING_RX_SOFTWARE
  96                        | SOF_TIMESTAMPING_RX_HARDWARE },
  97                {}
  98        },
  99        {
 100                { so_timestamping: SOF_TIMESTAMPING_SOFTWARE
 101                        | SOF_TIMESTAMPING_RX_SOFTWARE },
 102                { swtstamp: true }
 103        },
 104        {
 105                { so_timestamp: 1, so_timestamping: SOF_TIMESTAMPING_SOFTWARE
 106                        | SOF_TIMESTAMPING_RX_SOFTWARE },
 107                { tstamp: true, swtstamp: true }
 108        },
 109};
 110
 111static struct option long_options[] = {
 112        { "list_tests", no_argument, 0, 'l' },
 113        { "test_num", required_argument, 0, 'n' },
 114        { "op_size", required_argument, 0, 's' },
 115        { "tcp", no_argument, 0, 't' },
 116        { "udp", no_argument, 0, 'u' },
 117        { "ip", no_argument, 0, 'i' },
 118};
 119
 120static int next_port = 19999;
 121static int op_size = 10 * 1024;
 122
 123void print_test_case(struct test_case *t)
 124{
 125        int f = 0;
 126
 127        printf("sockopts {");
 128        if (t->sockopt.so_timestamp)
 129                printf(" SO_TIMESTAMP ");
 130        if (t->sockopt.so_timestampns)
 131                printf(" SO_TIMESTAMPNS ");
 132        if (t->sockopt.so_timestamping) {
 133                printf(" SO_TIMESTAMPING: {");
 134                for (f = 0; f < ARRAY_SIZE(sof_flags); f++)
 135                        if (t->sockopt.so_timestamping & sof_flags[f].mask)
 136                                printf(" %s |", sof_flags[f].name);
 137                printf("}");
 138        }
 139        printf("} expected cmsgs: {");
 140        if (t->expected.tstamp)
 141                printf(" SCM_TIMESTAMP ");
 142        if (t->expected.tstampns)
 143                printf(" SCM_TIMESTAMPNS ");
 144        if (t->expected.swtstamp || t->expected.hwtstamp) {
 145                printf(" SCM_TIMESTAMPING {");
 146                if (t->expected.swtstamp)
 147                        printf("0");
 148                if (t->expected.swtstamp && t->expected.hwtstamp)
 149                        printf(",");
 150                if (t->expected.hwtstamp)
 151                        printf("2");
 152                printf("}");
 153        }
 154        printf("}\n");
 155}
 156
 157void do_send(int src)
 158{
 159        int r;
 160        char *buf = malloc(op_size);
 161
 162        memset(buf, 'z', op_size);
 163        r = write(src, buf, op_size);
 164        if (r < 0)
 165                error(1, errno, "Failed to sendmsg");
 166
 167        free(buf);
 168}
 169
 170bool do_recv(int rcv, int read_size, struct tstamps expected)
 171{
 172        const int CMSG_SIZE = 1024;
 173
 174        struct scm_timestamping *ts;
 175        struct tstamps actual = {};
 176        char cmsg_buf[CMSG_SIZE];
 177        struct iovec recv_iov;
 178        struct cmsghdr *cmsg;
 179        bool failed = false;
 180        struct msghdr hdr;
 181        int flags = 0;
 182        int r;
 183
 184        memset(&hdr, 0, sizeof(hdr));
 185        hdr.msg_iov = &recv_iov;
 186        hdr.msg_iovlen = 1;
 187        recv_iov.iov_base = malloc(read_size);
 188        recv_iov.iov_len = read_size;
 189
 190        hdr.msg_control = cmsg_buf;
 191        hdr.msg_controllen = sizeof(cmsg_buf);
 192
 193        r = recvmsg(rcv, &hdr, flags);
 194        if (r < 0)
 195                error(1, errno, "Failed to recvmsg");
 196        if (r != read_size)
 197                error(1, 0, "Only received %d bytes of payload.", r);
 198
 199        if (hdr.msg_flags & (MSG_TRUNC | MSG_CTRUNC))
 200                error(1, 0, "Message was truncated.");
 201
 202        for (cmsg = CMSG_FIRSTHDR(&hdr); cmsg != NULL;
 203             cmsg = CMSG_NXTHDR(&hdr, cmsg)) {
 204                if (cmsg->cmsg_level != SOL_SOCKET)
 205                        error(1, 0, "Unexpected cmsg_level %d",
 206                              cmsg->cmsg_level);
 207                switch (cmsg->cmsg_type) {
 208                case SCM_TIMESTAMP:
 209                        actual.tstamp = true;
 210                        break;
 211                case SCM_TIMESTAMPNS:
 212                        actual.tstampns = true;
 213                        break;
 214                case SCM_TIMESTAMPING:
 215                        ts = (struct scm_timestamping *)CMSG_DATA(cmsg);
 216                        actual.swtstamp = !!ts->ts[0].tv_sec;
 217                        if (ts->ts[1].tv_sec != 0)
 218                                error(0, 0, "ts[1] should not be set.");
 219                        actual.hwtstamp = !!ts->ts[2].tv_sec;
 220                        break;
 221                default:
 222                        error(1, 0, "Unexpected cmsg_type %d", cmsg->cmsg_type);
 223                }
 224        }
 225
 226#define VALIDATE(field) \
 227        do { \
 228                if (expected.field != actual.field) { \
 229                        if (expected.field) \
 230                                error(0, 0, "Expected " #field " to be set."); \
 231                        else \
 232                                error(0, 0, \
 233                                      "Expected " #field " to not be set."); \
 234                        failed = true; \
 235                } \
 236        } while (0)
 237
 238        VALIDATE(tstamp);
 239        VALIDATE(tstampns);
 240        VALIDATE(swtstamp);
 241        VALIDATE(hwtstamp);
 242#undef VALIDATE
 243
 244        free(recv_iov.iov_base);
 245
 246        return failed;
 247}
 248
 249void config_so_flags(int rcv, struct options o)
 250{
 251        int on = 1;
 252
 253        if (setsockopt(rcv, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) < 0)
 254                error(1, errno, "Failed to enable SO_REUSEADDR");
 255
 256        if (o.so_timestamp &&
 257            setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMP,
 258                       &o.so_timestamp, sizeof(o.so_timestamp)) < 0)
 259                error(1, errno, "Failed to enable SO_TIMESTAMP");
 260
 261        if (o.so_timestampns &&
 262            setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMPNS,
 263                       &o.so_timestampns, sizeof(o.so_timestampns)) < 0)
 264                error(1, errno, "Failed to enable SO_TIMESTAMPNS");
 265
 266        if (o.so_timestamping &&
 267            setsockopt(rcv, SOL_SOCKET, SO_TIMESTAMPING,
 268                       &o.so_timestamping, sizeof(o.so_timestamping)) < 0)
 269                error(1, errno, "Failed to set SO_TIMESTAMPING");
 270}
 271
 272bool run_test_case(struct socket_type s, struct test_case t)
 273{
 274        int port = (s.type == SOCK_RAW) ? 0 : next_port++;
 275        int read_size = op_size;
 276        struct sockaddr_in addr;
 277        bool failed = false;
 278        int src, dst, rcv;
 279
 280        src = socket(AF_INET, s.type, s.protocol);
 281        if (src < 0)
 282                error(1, errno, "Failed to open src socket");
 283
 284        dst = socket(AF_INET, s.type, s.protocol);
 285        if (dst < 0)
 286                error(1, errno, "Failed to open dst socket");
 287
 288        memset(&addr, 0, sizeof(addr));
 289        addr.sin_family = AF_INET;
 290        addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
 291        addr.sin_port = htons(port);
 292
 293        if (bind(dst, (struct sockaddr *)&addr, sizeof(addr)) < 0)
 294                error(1, errno, "Failed to bind to port %d", port);
 295
 296        if (s.type == SOCK_STREAM && (listen(dst, 1) < 0))
 297                error(1, errno, "Failed to listen");
 298
 299        if (connect(src, (struct sockaddr *)&addr, sizeof(addr)) < 0)
 300                error(1, errno, "Failed to connect");
 301
 302        if (s.type == SOCK_STREAM) {
 303                rcv = accept(dst, NULL, NULL);
 304                if (rcv < 0)
 305                        error(1, errno, "Failed to accept");
 306                close(dst);
 307        } else {
 308                rcv = dst;
 309        }
 310
 311        config_so_flags(rcv, t.sockopt);
 312        usleep(20000); /* setsockopt for SO_TIMESTAMPING is asynchronous */
 313        do_send(src);
 314
 315        if (s.type == SOCK_RAW)
 316                read_size += 20;  /* for IP header */
 317        failed = do_recv(rcv, read_size, t.expected);
 318
 319        close(rcv);
 320        close(src);
 321
 322        return failed;
 323}
 324
 325int main(int argc, char **argv)
 326{
 327        bool all_protocols = true;
 328        bool all_tests = true;
 329        int arg_index = 0;
 330        int failures = 0;
 331        int s, t;
 332        char opt;
 333
 334        while ((opt = getopt_long(argc, argv, "", long_options,
 335                                  &arg_index)) != -1) {
 336                switch (opt) {
 337                case 'l':
 338                        for (t = 0; t < ARRAY_SIZE(test_cases); t++) {
 339                                printf("%d\t", t);
 340                                print_test_case(&test_cases[t]);
 341                        }
 342                        return 0;
 343                case 'n':
 344                        t = atoi(optarg);
 345                        if (t >= ARRAY_SIZE(test_cases))
 346                                error(1, 0, "Invalid test case: %d", t);
 347                        all_tests = false;
 348                        test_cases[t].enabled = true;
 349                        break;
 350                case 's':
 351                        op_size = atoi(optarg);
 352                        break;
 353                case 't':
 354                        all_protocols = false;
 355                        socket_types[2].enabled = true;
 356                        break;
 357                case 'u':
 358                        all_protocols = false;
 359                        socket_types[1].enabled = true;
 360                        break;
 361                case 'i':
 362                        all_protocols = false;
 363                        socket_types[0].enabled = true;
 364                        break;
 365                default:
 366                        error(1, 0, "Failed to parse parameters.");
 367                }
 368        }
 369
 370        for (s = 0; s < ARRAY_SIZE(socket_types); s++) {
 371                if (!all_protocols && !socket_types[s].enabled)
 372                        continue;
 373
 374                printf("Testing %s...\n", socket_types[s].friendly_name);
 375                for (t = 0; t < ARRAY_SIZE(test_cases); t++) {
 376                        if (!all_tests && !test_cases[t].enabled)
 377                                continue;
 378
 379                        printf("Starting testcase %d...\n", t);
 380                        if (run_test_case(socket_types[s], test_cases[t])) {
 381                                failures++;
 382                                printf("FAILURE in test case ");
 383                                print_test_case(&test_cases[t]);
 384                        }
 385                }
 386        }
 387        if (!failures)
 388                printf("PASSED.\n");
 389        return failures;
 390}
 391