test_map_in_map_kern.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (c) 2017 Facebook
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. */
  8. #define KBUILD_MODNAME "foo"
  9. #include <linux/ptrace.h>
  10. #include <linux/version.h>
  11. #include <uapi/linux/bpf.h>
  12. #include <uapi/linux/in6.h>
  13. #include "bpf_helpers.h"
  14. #define MAX_NR_PORTS 65536
  15. /* map #0 */
  16. struct bpf_map_def SEC("maps") port_a = {
  17. .type = BPF_MAP_TYPE_ARRAY,
  18. .key_size = sizeof(u32),
  19. .value_size = sizeof(int),
  20. .max_entries = MAX_NR_PORTS,
  21. };
  22. /* map #1 */
  23. struct bpf_map_def SEC("maps") port_h = {
  24. .type = BPF_MAP_TYPE_HASH,
  25. .key_size = sizeof(u32),
  26. .value_size = sizeof(int),
  27. .max_entries = 1,
  28. };
  29. /* map #2 */
  30. struct bpf_map_def SEC("maps") reg_result_h = {
  31. .type = BPF_MAP_TYPE_HASH,
  32. .key_size = sizeof(u32),
  33. .value_size = sizeof(int),
  34. .max_entries = 1,
  35. };
  36. /* map #3 */
  37. struct bpf_map_def SEC("maps") inline_result_h = {
  38. .type = BPF_MAP_TYPE_HASH,
  39. .key_size = sizeof(u32),
  40. .value_size = sizeof(int),
  41. .max_entries = 1,
  42. };
  43. /* map #4 */ /* Test case #0 */
  44. struct bpf_map_def SEC("maps") a_of_port_a = {
  45. .type = BPF_MAP_TYPE_ARRAY_OF_MAPS,
  46. .key_size = sizeof(u32),
  47. .inner_map_idx = 0, /* map_fd[0] is port_a */
  48. .max_entries = MAX_NR_PORTS,
  49. };
  50. /* map #5 */ /* Test case #1 */
  51. struct bpf_map_def SEC("maps") h_of_port_a = {
  52. .type = BPF_MAP_TYPE_HASH_OF_MAPS,
  53. .key_size = sizeof(u32),
  54. .inner_map_idx = 0, /* map_fd[0] is port_a */
  55. .max_entries = 1,
  56. };
  57. /* map #6 */ /* Test case #2 */
  58. struct bpf_map_def SEC("maps") h_of_port_h = {
  59. .type = BPF_MAP_TYPE_HASH_OF_MAPS,
  60. .key_size = sizeof(u32),
  61. .inner_map_idx = 1, /* map_fd[1] is port_h */
  62. .max_entries = 1,
  63. };
  64. static __always_inline int do_reg_lookup(void *inner_map, u32 port)
  65. {
  66. int *result;
  67. result = bpf_map_lookup_elem(inner_map, &port);
  68. return result ? *result : -ENOENT;
  69. }
  70. static __always_inline int do_inline_array_lookup(void *inner_map, u32 port)
  71. {
  72. int *result;
  73. if (inner_map != &port_a)
  74. return -EINVAL;
  75. result = bpf_map_lookup_elem(&port_a, &port);
  76. return result ? *result : -ENOENT;
  77. }
  78. static __always_inline int do_inline_hash_lookup(void *inner_map, u32 port)
  79. {
  80. int *result;
  81. if (inner_map != &port_h)
  82. return -EINVAL;
  83. result = bpf_map_lookup_elem(&port_h, &port);
  84. return result ? *result : -ENOENT;
  85. }
  86. SEC("kprobe/sys_connect")
  87. int trace_sys_connect(struct pt_regs *ctx)
  88. {
  89. struct sockaddr_in6 *in6;
  90. u16 test_case, port, dst6[8];
  91. int addrlen, ret, inline_ret, ret_key = 0;
  92. u32 port_key;
  93. void *outer_map, *inner_map;
  94. bool inline_hash = false;
  95. in6 = (struct sockaddr_in6 *)PT_REGS_PARM2(ctx);
  96. addrlen = (int)PT_REGS_PARM3(ctx);
  97. if (addrlen != sizeof(*in6))
  98. return 0;
  99. ret = bpf_probe_read(dst6, sizeof(dst6), &in6->sin6_addr);
  100. if (ret) {
  101. inline_ret = ret;
  102. goto done;
  103. }
  104. if (dst6[0] != 0xdead || dst6[1] != 0xbeef)
  105. return 0;
  106. test_case = dst6[7];
  107. ret = bpf_probe_read(&port, sizeof(port), &in6->sin6_port);
  108. if (ret) {
  109. inline_ret = ret;
  110. goto done;
  111. }
  112. port_key = port;
  113. ret = -ENOENT;
  114. if (test_case == 0) {
  115. outer_map = &a_of_port_a;
  116. } else if (test_case == 1) {
  117. outer_map = &h_of_port_a;
  118. } else if (test_case == 2) {
  119. outer_map = &h_of_port_h;
  120. } else {
  121. ret = __LINE__;
  122. inline_ret = ret;
  123. goto done;
  124. }
  125. inner_map = bpf_map_lookup_elem(outer_map, &port_key);
  126. if (!inner_map) {
  127. ret = __LINE__;
  128. inline_ret = ret;
  129. goto done;
  130. }
  131. ret = do_reg_lookup(inner_map, port_key);
  132. if (test_case == 0 || test_case == 1)
  133. inline_ret = do_inline_array_lookup(inner_map, port_key);
  134. else
  135. inline_ret = do_inline_hash_lookup(inner_map, port_key);
  136. done:
  137. bpf_map_update_elem(&reg_result_h, &ret_key, &ret, BPF_ANY);
  138. bpf_map_update_elem(&inline_result_h, &ret_key, &inline_ret, BPF_ANY);
  139. return 0;
  140. }
  141. char _license[] SEC("license") = "GPL";
  142. u32 _version SEC("version") = LINUX_VERSION_CODE;