1 /* Test program for SIOC{G,S}HWTSTAMP
2 * Copyright 2013 Solarflare Communications
3 * Author: Ben Hutchings
11 #include <sys/socket.h>
12 #include <sys/ioctl.h>
15 #include <linux/net_tstamp.h>
16 #include <linux/sockios.h>
19 lookup_value(const char **names
, int size
, const char *name
)
23 for (value
= 0; value
< size
; value
++)
24 if (names
[value
] && strcasecmp(names
[value
], name
) == 0)
31 lookup_name(const char **names
, int size
, int value
)
33 return (value
>= 0 && value
< size
) ? names
[value
] : NULL
;
36 static void list_names(FILE *f
, const char **names
, int size
)
40 for (value
= 0; value
< size
; value
++)
42 fprintf(f
, " %s\n", names
[value
]);
45 static const char *tx_types
[] = {
46 #define TX_TYPE(name) [HWTSTAMP_TX_ ## name] = #name
52 #define N_TX_TYPES ((int)(sizeof(tx_types) / sizeof(tx_types[0])))
54 static const char *rx_filters
[] = {
55 #define RX_FILTER(name) [HWTSTAMP_FILTER_ ## name] = #name
59 RX_FILTER(PTP_V1_L4_EVENT
),
60 RX_FILTER(PTP_V1_L4_SYNC
),
61 RX_FILTER(PTP_V1_L4_DELAY_REQ
),
62 RX_FILTER(PTP_V2_L4_EVENT
),
63 RX_FILTER(PTP_V2_L4_SYNC
),
64 RX_FILTER(PTP_V2_L4_DELAY_REQ
),
65 RX_FILTER(PTP_V2_L2_EVENT
),
66 RX_FILTER(PTP_V2_L2_SYNC
),
67 RX_FILTER(PTP_V2_L2_DELAY_REQ
),
68 RX_FILTER(PTP_V2_EVENT
),
69 RX_FILTER(PTP_V2_SYNC
),
70 RX_FILTER(PTP_V2_DELAY_REQ
),
73 #define N_RX_FILTERS ((int)(sizeof(rx_filters) / sizeof(rx_filters[0])))
75 static void usage(void)
77 fputs("Usage: hwtstamp_config if_name [tx_type rx_filter]\n"
78 "tx_type is any of (case-insensitive):\n",
80 list_names(stderr
, tx_types
, N_TX_TYPES
);
81 fputs("rx_filter is any of (case-insensitive):\n", stderr
);
82 list_names(stderr
, rx_filters
, N_RX_FILTERS
);
85 int main(int argc
, char **argv
)
88 struct hwtstamp_config config
;
92 if ((argc
!= 2 && argc
!= 4) || (strlen(argv
[1]) >= IFNAMSIZ
)) {
99 config
.tx_type
= lookup_value(tx_types
, N_TX_TYPES
, argv
[2]);
100 config
.rx_filter
= lookup_value(rx_filters
, N_RX_FILTERS
, argv
[3]);
101 if (config
.tx_type
< 0 || config
.rx_filter
< 0) {
107 sock
= socket(AF_INET
, SOCK_DGRAM
, 0);
113 strcpy(ifr
.ifr_name
, argv
[1]);
114 ifr
.ifr_data
= (caddr_t
)&config
;
116 if (ioctl(sock
, (argc
== 2) ? SIOCGHWTSTAMP
: SIOCSHWTSTAMP
, &ifr
)) {
121 printf("flags = %#x\n", config
.flags
);
122 name
= lookup_name(tx_types
, N_TX_TYPES
, config
.tx_type
);
124 printf("tx_type = %s\n", name
);
126 printf("tx_type = %d\n", config
.tx_type
);
127 name
= lookup_name(rx_filters
, N_RX_FILTERS
, config
.rx_filter
);
129 printf("rx_filter = %s\n", name
);
131 printf("rx_filter = %d\n", config
.rx_filter
);