1 // SPDX-License-Identifier: GPL-2.0
2 /* Test program for SIOC{G,S}HWTSTAMP
3 * Copyright 2013 Solarflare Communications
4 * Author: Ben Hutchings
12 #include <sys/socket.h>
13 #include <sys/ioctl.h>
16 #include <linux/net_tstamp.h>
17 #include <linux/sockios.h>
19 #include "kselftest.h"
22 lookup_value(const char **names, int size, const char *name)
26 for (value = 0; value < size; value++)
27 if (names[value] && strcasecmp(names[value], name) == 0)
34 lookup_name(const char **names, int size, int value)
36 return (value >= 0 && value < size) ? names[value] : NULL;
39 static void list_names(FILE *f, const char **names, int size)
43 for (value = 0; value < size; value++)
45 fprintf(f, " %s\n", names[value]);
48 static const char *tx_types[] = {
49 #define TX_TYPE(name) [HWTSTAMP_TX_ ## name] = #name
55 #define N_TX_TYPES ((int)(ARRAY_SIZE(tx_types)))
57 static const char *rx_filters[] = {
58 #define RX_FILTER(name) [HWTSTAMP_FILTER_ ## name] = #name
62 RX_FILTER(PTP_V1_L4_EVENT),
63 RX_FILTER(PTP_V1_L4_SYNC),
64 RX_FILTER(PTP_V1_L4_DELAY_REQ),
65 RX_FILTER(PTP_V2_L4_EVENT),
66 RX_FILTER(PTP_V2_L4_SYNC),
67 RX_FILTER(PTP_V2_L4_DELAY_REQ),
68 RX_FILTER(PTP_V2_L2_EVENT),
69 RX_FILTER(PTP_V2_L2_SYNC),
70 RX_FILTER(PTP_V2_L2_DELAY_REQ),
71 RX_FILTER(PTP_V2_EVENT),
72 RX_FILTER(PTP_V2_SYNC),
73 RX_FILTER(PTP_V2_DELAY_REQ),
76 #define N_RX_FILTERS ((int)(ARRAY_SIZE(rx_filters)))
78 static void usage(void)
80 fputs("Usage: hwtstamp_config if_name [tx_type rx_filter]\n"
81 "tx_type is any of (case-insensitive):\n",
83 list_names(stderr, tx_types, N_TX_TYPES);
84 fputs("rx_filter is any of (case-insensitive):\n", stderr);
85 list_names(stderr, rx_filters, N_RX_FILTERS);
88 int main(int argc, char **argv)
91 struct hwtstamp_config config;
95 if ((argc != 2 && argc != 4) || (strlen(argv[1]) >= IFNAMSIZ)) {
102 config.tx_type = lookup_value(tx_types, N_TX_TYPES, argv[2]);
103 config.rx_filter = lookup_value(rx_filters, N_RX_FILTERS, argv[3]);
104 if (config.tx_type < 0 || config.rx_filter < 0) {
110 sock = socket(AF_INET, SOCK_DGRAM, 0);
116 strcpy(ifr.ifr_name, argv[1]);
117 ifr.ifr_data = (caddr_t)&config;
119 if (ioctl(sock, (argc == 2) ? SIOCGHWTSTAMP : SIOCSHWTSTAMP, &ifr)) {
124 printf("flags = %#x\n", config.flags);
125 name = lookup_name(tx_types, N_TX_TYPES, config.tx_type);
127 printf("tx_type = %s\n", name);
129 printf("tx_type = %d\n", config.tx_type);
130 name = lookup_name(rx_filters, N_RX_FILTERS, config.rx_filter);
132 printf("rx_filter = %s\n", name);
134 printf("rx_filter = %d\n", config.rx_filter);