ibumad_kern.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
  2. /**
  3. * ibumad BPF sample kernel side
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * Copyright(c) 2018 Ira Weiny, Intel Corporation
  10. */
  11. #define KBUILD_MODNAME "ibumad_count_pkts_by_class"
  12. #include <uapi/linux/bpf.h>
  13. #include <bpf/bpf_helpers.h>
  14. struct bpf_map_def SEC("maps") read_count = {
  15. .type = BPF_MAP_TYPE_ARRAY,
  16. .key_size = sizeof(u32), /* class; u32 required */
  17. .value_size = sizeof(u64), /* count of mads read */
  18. .max_entries = 256, /* Room for all Classes */
  19. };
  20. struct bpf_map_def SEC("maps") write_count = {
  21. .type = BPF_MAP_TYPE_ARRAY,
  22. .key_size = sizeof(u32), /* class; u32 required */
  23. .value_size = sizeof(u64), /* count of mads written */
  24. .max_entries = 256, /* Room for all Classes */
  25. };
  26. #undef DEBUG
  27. #ifndef DEBUG
  28. #undef bpf_printk
  29. #define bpf_printk(fmt, ...)
  30. #endif
  31. /* Taken from the current format defined in
  32. * include/trace/events/ib_umad.h
  33. * and
  34. * /sys/kernel/debug/tracing/events/ib_umad/ib_umad_read/format
  35. * /sys/kernel/debug/tracing/events/ib_umad/ib_umad_write/format
  36. */
  37. struct ib_umad_rw_args {
  38. u64 pad;
  39. u8 port_num;
  40. u8 sl;
  41. u8 path_bits;
  42. u8 grh_present;
  43. u32 id;
  44. u32 status;
  45. u32 timeout_ms;
  46. u32 retires;
  47. u32 length;
  48. u32 qpn;
  49. u32 qkey;
  50. u8 gid_index;
  51. u8 hop_limit;
  52. u16 lid;
  53. u16 attr_id;
  54. u16 pkey_index;
  55. u8 base_version;
  56. u8 mgmt_class;
  57. u8 class_version;
  58. u8 method;
  59. u32 flow_label;
  60. u16 mad_status;
  61. u16 class_specific;
  62. u32 attr_mod;
  63. u64 tid;
  64. u8 gid[16];
  65. u32 dev_index;
  66. u8 traffic_class;
  67. };
  68. SEC("tracepoint/ib_umad/ib_umad_read_recv")
  69. int on_ib_umad_read_recv(struct ib_umad_rw_args *ctx)
  70. {
  71. u64 zero = 0, *val;
  72. u8 class = ctx->mgmt_class;
  73. bpf_printk("ib_umad read recv : class 0x%x\n", class);
  74. val = bpf_map_lookup_elem(&read_count, &class);
  75. if (!val) {
  76. bpf_map_update_elem(&read_count, &class, &zero, BPF_NOEXIST);
  77. val = bpf_map_lookup_elem(&read_count, &class);
  78. if (!val)
  79. return 0;
  80. }
  81. (*val) += 1;
  82. return 0;
  83. }
  84. SEC("tracepoint/ib_umad/ib_umad_read_send")
  85. int on_ib_umad_read_send(struct ib_umad_rw_args *ctx)
  86. {
  87. u64 zero = 0, *val;
  88. u8 class = ctx->mgmt_class;
  89. bpf_printk("ib_umad read send : class 0x%x\n", class);
  90. val = bpf_map_lookup_elem(&read_count, &class);
  91. if (!val) {
  92. bpf_map_update_elem(&read_count, &class, &zero, BPF_NOEXIST);
  93. val = bpf_map_lookup_elem(&read_count, &class);
  94. if (!val)
  95. return 0;
  96. }
  97. (*val) += 1;
  98. return 0;
  99. }
  100. SEC("tracepoint/ib_umad/ib_umad_write")
  101. int on_ib_umad_write(struct ib_umad_rw_args *ctx)
  102. {
  103. u64 zero = 0, *val;
  104. u8 class = ctx->mgmt_class;
  105. bpf_printk("ib_umad write : class 0x%x\n", class);
  106. val = bpf_map_lookup_elem(&write_count, &class);
  107. if (!val) {
  108. bpf_map_update_elem(&write_count, &class, &zero, BPF_NOEXIST);
  109. val = bpf_map_lookup_elem(&write_count, &class);
  110. if (!val)
  111. return 0;
  112. }
  113. (*val) += 1;
  114. return 0;
  115. }
  116. char _license[] SEC("license") = "GPL";