hwtstamp_config.c 3.0 KB

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