test_map_in_map_user.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. #include <sys/resource.h>
  9. #include <sys/socket.h>
  10. #include <arpa/inet.h>
  11. #include <stdint.h>
  12. #include <assert.h>
  13. #include <errno.h>
  14. #include <stdlib.h>
  15. #include <stdio.h>
  16. #include <bpf/bpf.h>
  17. #include "bpf_load.h"
  18. #define PORT_A (map_fd[0])
  19. #define PORT_H (map_fd[1])
  20. #define REG_RESULT_H (map_fd[2])
  21. #define INLINE_RESULT_H (map_fd[3])
  22. #define A_OF_PORT_A (map_fd[4]) /* Test case #0 */
  23. #define H_OF_PORT_A (map_fd[5]) /* Test case #1 */
  24. #define H_OF_PORT_H (map_fd[6]) /* Test case #2 */
  25. static const char * const test_names[] = {
  26. "Array of Array",
  27. "Hash of Array",
  28. "Hash of Hash",
  29. };
  30. #define NR_TESTS (sizeof(test_names) / sizeof(*test_names))
  31. static void check_map_id(int inner_map_fd, int map_in_map_fd, uint32_t key)
  32. {
  33. struct bpf_map_info info = {};
  34. uint32_t info_len = sizeof(info);
  35. int ret, id;
  36. ret = bpf_obj_get_info_by_fd(inner_map_fd, &info, &info_len);
  37. assert(!ret);
  38. ret = bpf_map_lookup_elem(map_in_map_fd, &key, &id);
  39. assert(!ret);
  40. assert(id == info.id);
  41. }
  42. static void populate_map(uint32_t port_key, int magic_result)
  43. {
  44. int ret;
  45. ret = bpf_map_update_elem(PORT_A, &port_key, &magic_result, BPF_ANY);
  46. assert(!ret);
  47. ret = bpf_map_update_elem(PORT_H, &port_key, &magic_result,
  48. BPF_NOEXIST);
  49. assert(!ret);
  50. ret = bpf_map_update_elem(A_OF_PORT_A, &port_key, &PORT_A, BPF_ANY);
  51. assert(!ret);
  52. check_map_id(PORT_A, A_OF_PORT_A, port_key);
  53. ret = bpf_map_update_elem(H_OF_PORT_A, &port_key, &PORT_A, BPF_NOEXIST);
  54. assert(!ret);
  55. check_map_id(PORT_A, H_OF_PORT_A, port_key);
  56. ret = bpf_map_update_elem(H_OF_PORT_H, &port_key, &PORT_H, BPF_NOEXIST);
  57. assert(!ret);
  58. check_map_id(PORT_H, H_OF_PORT_H, port_key);
  59. }
  60. static void test_map_in_map(void)
  61. {
  62. struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
  63. uint32_t result_key = 0, port_key;
  64. int result, inline_result;
  65. int magic_result = 0xfaceb00c;
  66. int ret;
  67. int i;
  68. port_key = rand() & 0x00FF;
  69. populate_map(port_key, magic_result);
  70. in6.sin6_addr.s6_addr16[0] = 0xdead;
  71. in6.sin6_addr.s6_addr16[1] = 0xbeef;
  72. in6.sin6_port = port_key;
  73. for (i = 0; i < NR_TESTS; i++) {
  74. printf("%s: ", test_names[i]);
  75. in6.sin6_addr.s6_addr16[7] = i;
  76. ret = connect(-1, (struct sockaddr *)&in6, sizeof(in6));
  77. assert(ret == -1 && errno == EBADF);
  78. ret = bpf_map_lookup_elem(REG_RESULT_H, &result_key, &result);
  79. assert(!ret);
  80. ret = bpf_map_lookup_elem(INLINE_RESULT_H, &result_key,
  81. &inline_result);
  82. assert(!ret);
  83. if (result != magic_result || inline_result != magic_result) {
  84. printf("Error. result:%d inline_result:%d\n",
  85. result, inline_result);
  86. exit(1);
  87. }
  88. bpf_map_delete_elem(REG_RESULT_H, &result_key);
  89. bpf_map_delete_elem(INLINE_RESULT_H, &result_key);
  90. printf("Pass\n");
  91. }
  92. }
  93. int main(int argc, char **argv)
  94. {
  95. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  96. char filename[256];
  97. assert(!setrlimit(RLIMIT_MEMLOCK, &r));
  98. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  99. if (load_bpf_file(filename)) {
  100. printf("%s", bpf_log_buf);
  101. return 1;
  102. }
  103. test_map_in_map();
  104. return 0;
  105. }