138 lines · c
1// SPDX-License-Identifier: GPL-2.02/* Test program for SIOC{G,S}HWTSTAMP3 * Copyright 2013 Solarflare Communications4 * Author: Ben Hutchings5 */6 7#include <errno.h>8#include <stdio.h>9#include <stdlib.h>10#include <string.h>11 12#include <sys/socket.h>13#include <sys/ioctl.h>14 15#include <linux/if.h>16#include <linux/net_tstamp.h>17#include <linux/sockios.h>18 19#include "kselftest.h"20 21static int22lookup_value(const char **names, int size, const char *name)23{24 int value;25 26 for (value = 0; value < size; value++)27 if (names[value] && strcasecmp(names[value], name) == 0)28 return value;29 30 return -1;31}32 33static const char *34lookup_name(const char **names, int size, int value)35{36 return (value >= 0 && value < size) ? names[value] : NULL;37}38 39static void list_names(FILE *f, const char **names, int size)40{41 int value;42 43 for (value = 0; value < size; value++)44 if (names[value])45 fprintf(f, " %s\n", names[value]);46}47 48static const char *tx_types[] = {49#define TX_TYPE(name) [HWTSTAMP_TX_ ## name] = #name50 TX_TYPE(OFF),51 TX_TYPE(ON),52 TX_TYPE(ONESTEP_SYNC)53#undef TX_TYPE54};55#define N_TX_TYPES ((int)(ARRAY_SIZE(tx_types)))56 57static const char *rx_filters[] = {58#define RX_FILTER(name) [HWTSTAMP_FILTER_ ## name] = #name59 RX_FILTER(NONE),60 RX_FILTER(ALL),61 RX_FILTER(SOME),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),74#undef RX_FILTER75};76#define N_RX_FILTERS ((int)(ARRAY_SIZE(rx_filters)))77 78static void usage(void)79{80 fputs("Usage: hwtstamp_config if_name [tx_type rx_filter]\n"81 "tx_type is any of (case-insensitive):\n",82 stderr);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);86}87 88int main(int argc, char **argv)89{90 struct ifreq ifr;91 struct hwtstamp_config config;92 const char *name;93 int sock;94 95 if ((argc != 2 && argc != 4) || (strlen(argv[1]) >= IFNAMSIZ)) {96 usage();97 return 2;98 }99 100 if (argc == 4) {101 config.flags = 0;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) {105 usage();106 return 2;107 }108 }109 110 sock = socket(AF_INET, SOCK_DGRAM, 0);111 if (sock < 0) {112 perror("socket");113 return 1;114 }115 116 strcpy(ifr.ifr_name, argv[1]);117 ifr.ifr_data = (caddr_t)&config;118 119 if (ioctl(sock, (argc == 2) ? SIOCGHWTSTAMP : SIOCSHWTSTAMP, &ifr)) {120 perror("ioctl");121 return 1;122 }123 124 printf("flags = %#x\n", config.flags);125 name = lookup_name(tx_types, N_TX_TYPES, config.tx_type);126 if (name)127 printf("tx_type = %s\n", name);128 else129 printf("tx_type = %d\n", config.tx_type);130 name = lookup_name(rx_filters, N_RX_FILTERS, config.rx_filter);131 if (name)132 printf("rx_filter = %s\n", name);133 else134 printf("rx_filter = %d\n", config.rx_filter);135 136 return 0;137}138