netdev.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <ynl.h>
  5. #include <net/if.h>
  6. #include "netdev-user.h"
  7. /* netdev genetlink family code sample
  8. * This sample shows off basics of the netdev family but also notification
  9. * handling, hence the somewhat odd UI. We subscribe to notifications first
  10. * then wait for ifc selection, so the socket may already accumulate
  11. * notifications as we wait. This allows us to test that YNL can handle
  12. * requests and notifications getting interleaved.
  13. */
  14. static void netdev_print_device(struct netdev_dev_get_rsp *d, unsigned int op)
  15. {
  16. char ifname[IF_NAMESIZE];
  17. const char *name;
  18. if (!d->_present.ifindex)
  19. return;
  20. name = if_indextoname(d->ifindex, ifname);
  21. if (name)
  22. printf("%8s", name);
  23. printf("[%d]\t", d->ifindex);
  24. if (!d->_present.xdp_features)
  25. return;
  26. printf("xdp-features (%llx):", d->xdp_features);
  27. for (int i = 0; d->xdp_features >= 1U << i; i++) {
  28. if (d->xdp_features & (1U << i))
  29. printf(" %s", netdev_xdp_act_str(1 << i));
  30. }
  31. printf(" xdp-rx-metadata-features (%llx):", d->xdp_rx_metadata_features);
  32. for (int i = 0; d->xdp_rx_metadata_features >= 1U << i; i++) {
  33. if (d->xdp_rx_metadata_features & (1U << i))
  34. printf(" %s", netdev_xdp_rx_metadata_str(1 << i));
  35. }
  36. printf(" xsk-features (%llx):", d->xsk_features);
  37. for (int i = 0; d->xsk_features >= 1U << i; i++) {
  38. if (d->xsk_features & (1U << i))
  39. printf(" %s", netdev_xsk_flags_str(1 << i));
  40. }
  41. printf(" xdp-zc-max-segs=%u", d->xdp_zc_max_segs);
  42. name = netdev_op_str(op);
  43. if (name)
  44. printf(" (ntf: %s)", name);
  45. printf("\n");
  46. }
  47. int main(int argc, char **argv)
  48. {
  49. struct netdev_dev_get_list *devs;
  50. struct ynl_ntf_base_type *ntf;
  51. struct ynl_error yerr;
  52. struct ynl_sock *ys;
  53. int ifindex = 0;
  54. if (argc > 1)
  55. ifindex = strtol(argv[1], NULL, 0);
  56. ys = ynl_sock_create(&ynl_netdev_family, &yerr);
  57. if (!ys) {
  58. fprintf(stderr, "YNL: %s\n", yerr.msg);
  59. return 1;
  60. }
  61. if (ynl_subscribe(ys, "mgmt"))
  62. goto err_close;
  63. printf("Select ifc ($ifindex; or 0 = dump; or -2 ntf check): ");
  64. if (scanf("%d", &ifindex) != 1) {
  65. fprintf(stderr, "Error: unable to parse input\n");
  66. goto err_destroy;
  67. }
  68. if (ifindex > 0) {
  69. struct netdev_dev_get_req *req;
  70. struct netdev_dev_get_rsp *d;
  71. req = netdev_dev_get_req_alloc();
  72. netdev_dev_get_req_set_ifindex(req, ifindex);
  73. d = netdev_dev_get(ys, req);
  74. netdev_dev_get_req_free(req);
  75. if (!d)
  76. goto err_close;
  77. netdev_print_device(d, 0);
  78. netdev_dev_get_rsp_free(d);
  79. } else if (!ifindex) {
  80. devs = netdev_dev_get_dump(ys);
  81. if (!devs)
  82. goto err_close;
  83. if (ynl_dump_empty(devs))
  84. fprintf(stderr, "Error: no devices reported\n");
  85. ynl_dump_foreach(devs, d)
  86. netdev_print_device(d, 0);
  87. netdev_dev_get_list_free(devs);
  88. } else if (ifindex == -2) {
  89. ynl_ntf_check(ys);
  90. }
  91. while ((ntf = ynl_ntf_dequeue(ys))) {
  92. netdev_print_device((struct netdev_dev_get_rsp *)&ntf->data,
  93. ntf->cmd);
  94. ynl_ntf_free(ntf);
  95. }
  96. ynl_sock_destroy(ys);
  97. return 0;
  98. err_close:
  99. fprintf(stderr, "YNL: %s\n", ys->err.msg);
  100. err_destroy:
  101. ynl_sock_destroy(ys);
  102. return 2;
  103. }