test_map_in_map.bpf.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 "vmlinux.h"
  10. #include <linux/version.h>
  11. #include <bpf/bpf_helpers.h>
  12. #include <bpf/bpf_tracing.h>
  13. #include <bpf/bpf_core_read.h>
  14. #define MAX_NR_PORTS 65536
  15. #define EINVAL 22
  16. #define ENOENT 2
  17. /* map #0 */
  18. struct inner_a {
  19. __uint(type, BPF_MAP_TYPE_ARRAY);
  20. __type(key, u32);
  21. __type(value, int);
  22. __uint(max_entries, MAX_NR_PORTS);
  23. } port_a SEC(".maps");
  24. /* map #1 */
  25. struct inner_h {
  26. __uint(type, BPF_MAP_TYPE_HASH);
  27. __type(key, u32);
  28. __type(value, int);
  29. __uint(max_entries, 1);
  30. } port_h SEC(".maps");
  31. /* map #2 */
  32. struct {
  33. __uint(type, BPF_MAP_TYPE_HASH);
  34. __type(key, u32);
  35. __type(value, int);
  36. __uint(max_entries, 1);
  37. } reg_result_h SEC(".maps");
  38. /* map #3 */
  39. struct {
  40. __uint(type, BPF_MAP_TYPE_HASH);
  41. __type(key, u32);
  42. __type(value, int);
  43. __uint(max_entries, 1);
  44. } inline_result_h SEC(".maps");
  45. /* map #4 */ /* Test case #0 */
  46. struct {
  47. __uint(type, BPF_MAP_TYPE_ARRAY_OF_MAPS);
  48. __uint(max_entries, MAX_NR_PORTS);
  49. __uint(key_size, sizeof(u32));
  50. __array(values, struct inner_a); /* use inner_a as inner map */
  51. } a_of_port_a SEC(".maps");
  52. /* map #5 */ /* Test case #1 */
  53. struct {
  54. __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
  55. __uint(max_entries, 1);
  56. __uint(key_size, sizeof(u32));
  57. __array(values, struct inner_a); /* use inner_a as inner map */
  58. } h_of_port_a SEC(".maps");
  59. /* map #6 */ /* Test case #2 */
  60. struct {
  61. __uint(type, BPF_MAP_TYPE_HASH_OF_MAPS);
  62. __uint(max_entries, 1);
  63. __uint(key_size, sizeof(u32));
  64. __array(values, struct inner_h); /* use inner_h as inner map */
  65. } h_of_port_h SEC(".maps");
  66. static __always_inline int do_reg_lookup(void *inner_map, u32 port)
  67. {
  68. int *result;
  69. result = bpf_map_lookup_elem(inner_map, &port);
  70. return result ? *result : -ENOENT;
  71. }
  72. static __always_inline int do_inline_array_lookup(void *inner_map, u32 port)
  73. {
  74. int *result;
  75. if (inner_map != &port_a)
  76. return -EINVAL;
  77. result = bpf_map_lookup_elem(&port_a, &port);
  78. return result ? *result : -ENOENT;
  79. }
  80. static __always_inline int do_inline_hash_lookup(void *inner_map, u32 port)
  81. {
  82. int *result;
  83. if (inner_map != &port_h)
  84. return -EINVAL;
  85. result = bpf_map_lookup_elem(&port_h, &port);
  86. return result ? *result : -ENOENT;
  87. }
  88. SEC("ksyscall/connect")
  89. int BPF_KSYSCALL(trace_sys_connect, unsigned int fd, struct sockaddr_in6 *in6, int addrlen)
  90. {
  91. u16 test_case, port, dst6[8];
  92. int ret, inline_ret, ret_key = 0;
  93. u32 port_key;
  94. void *outer_map, *inner_map;
  95. bool inline_hash = false;
  96. if (addrlen != sizeof(*in6))
  97. return 0;
  98. ret = bpf_probe_read_user(dst6, sizeof(dst6), &in6->sin6_addr);
  99. if (ret) {
  100. inline_ret = ret;
  101. goto done;
  102. }
  103. if (dst6[0] != 0xdead || dst6[1] != 0xbeef)
  104. return 0;
  105. test_case = dst6[7];
  106. ret = bpf_probe_read_user(&port, sizeof(port), &in6->sin6_port);
  107. if (ret) {
  108. inline_ret = ret;
  109. goto done;
  110. }
  111. port_key = port;
  112. ret = -ENOENT;
  113. if (test_case == 0) {
  114. outer_map = &a_of_port_a;
  115. } else if (test_case == 1) {
  116. outer_map = &h_of_port_a;
  117. } else if (test_case == 2) {
  118. outer_map = &h_of_port_h;
  119. } else {
  120. ret = __LINE__;
  121. inline_ret = ret;
  122. goto done;
  123. }
  124. inner_map = bpf_map_lookup_elem(outer_map, &port_key);
  125. if (!inner_map) {
  126. ret = __LINE__;
  127. inline_ret = ret;
  128. goto done;
  129. }
  130. ret = do_reg_lookup(inner_map, port_key);
  131. if (test_case == 0 || test_case == 1)
  132. inline_ret = do_inline_array_lookup(inner_map, port_key);
  133. else
  134. inline_ret = do_inline_hash_lookup(inner_map, port_key);
  135. done:
  136. bpf_map_update_elem(&reg_result_h, &ret_key, &ret, BPF_ANY);
  137. bpf_map_update_elem(&inline_result_h, &ret_key, &inline_ret, BPF_ANY);
  138. return 0;
  139. }
  140. char _license[] SEC("license") = "GPL";
  141. u32 _version SEC("version") = LINUX_VERSION_CODE;