hwtstamp_config.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Test program for SIOC{G,S}HWTSTAMP
  3. * Copyright 2013 Solarflare Communications
  4. * Author: Ben Hutchings
  5. */
  6. #include <errno.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <sys/socket.h>
  11. #include <sys/ioctl.h>
  12. #include <linux/if.h>
  13. #include <linux/net_tstamp.h>
  14. #include <linux/sockios.h>
  15. static int
  16. lookup_value(const char **names, int size, const char *name)
  17. {
  18. int value;
  19. for (value = 0; value < size; value++)
  20. if (names[value] && strcasecmp(names[value], name) == 0)
  21. return value;
  22. return -1;
  23. }
  24. static const char *
  25. lookup_name(const char **names, int size, int value)
  26. {
  27. return (value >= 0 && value < size) ? names[value] : NULL;
  28. }
  29. static void list_names(FILE *f, const char **names, int size)
  30. {
  31. int value;
  32. for (value = 0; value < size; value++)
  33. if (names[value])
  34. fprintf(f, " %s\n", names[value]);
  35. }
  36. static const char *tx_types[] = {
  37. #define TX_TYPE(name) [HWTSTAMP_TX_ ## name] = #name
  38. TX_TYPE(OFF),
  39. TX_TYPE(ON),
  40. TX_TYPE(ONESTEP_SYNC)
  41. #undef TX_TYPE
  42. };
  43. #define N_TX_TYPES ((int)(sizeof(tx_types) / sizeof(tx_types[0])))
  44. static const char *rx_filters[] = {
  45. #define RX_FILTER(name) [HWTSTAMP_FILTER_ ## name] = #name
  46. RX_FILTER(NONE),
  47. RX_FILTER(ALL),
  48. RX_FILTER(SOME),
  49. RX_FILTER(PTP_V1_L4_EVENT),
  50. RX_FILTER(PTP_V1_L4_SYNC),
  51. RX_FILTER(PTP_V1_L4_DELAY_REQ),
  52. RX_FILTER(PTP_V2_L4_EVENT),
  53. RX_FILTER(PTP_V2_L4_SYNC),
  54. RX_FILTER(PTP_V2_L4_DELAY_REQ),
  55. RX_FILTER(PTP_V2_L2_EVENT),
  56. RX_FILTER(PTP_V2_L2_SYNC),
  57. RX_FILTER(PTP_V2_L2_DELAY_REQ),
  58. RX_FILTER(PTP_V2_EVENT),
  59. RX_FILTER(PTP_V2_SYNC),
  60. RX_FILTER(PTP_V2_DELAY_REQ),
  61. #undef RX_FILTER
  62. };
  63. #define N_RX_FILTERS ((int)(sizeof(rx_filters) / sizeof(rx_filters[0])))
  64. static void usage(void)
  65. {
  66. fputs("Usage: hwtstamp_config if_name [tx_type rx_filter]\n"
  67. "tx_type is any of (case-insensitive):\n",
  68. stderr);
  69. list_names(stderr, tx_types, N_TX_TYPES);
  70. fputs("rx_filter is any of (case-insensitive):\n", stderr);
  71. list_names(stderr, rx_filters, N_RX_FILTERS);
  72. }
  73. int main(int argc, char **argv)
  74. {
  75. struct ifreq ifr;
  76. struct hwtstamp_config config;
  77. const char *name;
  78. int sock;
  79. if ((argc != 2 && argc != 4) || (strlen(argv[1]) >= IFNAMSIZ)) {
  80. usage();
  81. return 2;
  82. }
  83. if (argc == 4) {
  84. config.flags = 0;
  85. config.tx_type = lookup_value(tx_types, N_TX_TYPES, argv[2]);
  86. config.rx_filter = lookup_value(rx_filters, N_RX_FILTERS, argv[3]);
  87. if (config.tx_type < 0 || config.rx_filter < 0) {
  88. usage();
  89. return 2;
  90. }
  91. }
  92. sock = socket(AF_INET, SOCK_DGRAM, 0);
  93. if (sock < 0) {
  94. perror("socket");
  95. return 1;
  96. }
  97. strcpy(ifr.ifr_name, argv[1]);
  98. ifr.ifr_data = (caddr_t)&config;
  99. if (ioctl(sock, (argc == 2) ? SIOCGHWTSTAMP : SIOCSHWTSTAMP, &ifr)) {
  100. perror("ioctl");
  101. return 1;
  102. }
  103. printf("flags = %#x\n", config.flags);
  104. name = lookup_name(tx_types, N_TX_TYPES, config.tx_type);
  105. if (name)
  106. printf("tx_type = %s\n", name);
  107. else
  108. printf("tx_type = %d\n", config.tx_type);
  109. name = lookup_name(rx_filters, N_RX_FILTERS, config.rx_filter);
  110. if (name)
  111. printf("rx_filter = %s\n", name);
  112. else
  113. printf("rx_filter = %d\n", config.rx_filter);
  114. return 0;
  115. }