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>
20 lookup_value(const char **names, int size, const char *name)
24 for (value = 0; value < size; value++)
25 if (names[value] && strcasecmp(names[value], name) == 0)
32 lookup_name(const char **names, int size, int value)
34 return (value >= 0 && value < size) ? names[value] : NULL;
37 static void list_names(FILE *f, const char **names, int size)
41 for (value = 0; value < size; value++)
43 fprintf(f, " %s\n", names[value]);
46 static const char *tx_types[] = {
47 #define TX_TYPE(name) [HWTSTAMP_TX_ ## name] = #name
53 #define N_TX_TYPES ((int)(sizeof(tx_types) / sizeof(tx_types[0])))
55 static const char *rx_filters[] = {
56 #define RX_FILTER(name) [HWTSTAMP_FILTER_ ## name] = #name
60 RX_FILTER(PTP_V1_L4_EVENT),
61 RX_FILTER(PTP_V1_L4_SYNC),
62 RX_FILTER(PTP_V1_L4_DELAY_REQ),
63 RX_FILTER(PTP_V2_L4_EVENT),
64 RX_FILTER(PTP_V2_L4_SYNC),
65 RX_FILTER(PTP_V2_L4_DELAY_REQ),
66 RX_FILTER(PTP_V2_L2_EVENT),
67 RX_FILTER(PTP_V2_L2_SYNC),
68 RX_FILTER(PTP_V2_L2_DELAY_REQ),
69 RX_FILTER(PTP_V2_EVENT),
70 RX_FILTER(PTP_V2_SYNC),
71 RX_FILTER(PTP_V2_DELAY_REQ),
74 #define N_RX_FILTERS ((int)(sizeof(rx_filters) / sizeof(rx_filters[0])))
76 static void usage(void)
78 fputs("Usage: hwtstamp_config if_name [tx_type rx_filter]\n"
79 "tx_type is any of (case-insensitive):\n",
81 list_names(stderr, tx_types, N_TX_TYPES);
82 fputs("rx_filter is any of (case-insensitive):\n", stderr);
83 list_names(stderr, rx_filters, N_RX_FILTERS);
86 int main(int argc, char **argv)
89 struct hwtstamp_config config;
93 if ((argc != 2 && argc != 4) || (strlen(argv[1]) >= IFNAMSIZ)) {
100 config.tx_type = lookup_value(tx_types, N_TX_TYPES, argv[2]);
101 config.rx_filter = lookup_value(rx_filters, N_RX_FILTERS, argv[3]);
102 if (config.tx_type < 0 || config.rx_filter < 0) {
108 sock = socket(AF_INET, SOCK_DGRAM, 0);
114 strcpy(ifr.ifr_name, argv[1]);
115 ifr.ifr_data = (caddr_t)&config;
117 if (ioctl(sock, (argc == 2) ? SIOCGHWTSTAMP : SIOCSHWTSTAMP, &ifr)) {
122 printf("flags = %#x\n", config.flags);
123 name = lookup_name(tx_types, N_TX_TYPES, config.tx_type);
125 printf("tx_type = %s\n", name);
127 printf("tx_type = %d\n", config.tx_type);
128 name = lookup_name(rx_filters, N_RX_FILTERS, config.rx_filter);
130 printf("rx_filter = %s\n", name);
132 printf("rx_filter = %d\n", config.rx_filter);