test_map_in_map_user.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 "libbpf.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 populate_map(uint32_t port_key, int magic_result)
  32. {
  33. int ret;
  34. ret = bpf_map_update_elem(PORT_A, &port_key, &magic_result, BPF_ANY);
  35. assert(!ret);
  36. ret = bpf_map_update_elem(PORT_H, &port_key, &magic_result,
  37. BPF_NOEXIST);
  38. assert(!ret);
  39. ret = bpf_map_update_elem(A_OF_PORT_A, &port_key, &PORT_A, BPF_ANY);
  40. assert(!ret);
  41. ret = bpf_map_update_elem(H_OF_PORT_A, &port_key, &PORT_A, BPF_NOEXIST);
  42. assert(!ret);
  43. ret = bpf_map_update_elem(H_OF_PORT_H, &port_key, &PORT_H, BPF_NOEXIST);
  44. assert(!ret);
  45. }
  46. static void test_map_in_map(void)
  47. {
  48. struct sockaddr_in6 in6 = { .sin6_family = AF_INET6 };
  49. uint32_t result_key = 0, port_key;
  50. int result, inline_result;
  51. int magic_result = 0xfaceb00c;
  52. int ret;
  53. int i;
  54. port_key = rand() & 0x00FF;
  55. populate_map(port_key, magic_result);
  56. in6.sin6_addr.s6_addr16[0] = 0xdead;
  57. in6.sin6_addr.s6_addr16[1] = 0xbeef;
  58. in6.sin6_port = port_key;
  59. for (i = 0; i < NR_TESTS; i++) {
  60. printf("%s: ", test_names[i]);
  61. in6.sin6_addr.s6_addr16[7] = i;
  62. ret = connect(-1, (struct sockaddr *)&in6, sizeof(in6));
  63. assert(ret == -1 && errno == EBADF);
  64. ret = bpf_map_lookup_elem(REG_RESULT_H, &result_key, &result);
  65. assert(!ret);
  66. ret = bpf_map_lookup_elem(INLINE_RESULT_H, &result_key,
  67. &inline_result);
  68. assert(!ret);
  69. if (result != magic_result || inline_result != magic_result) {
  70. printf("Error. result:%d inline_result:%d\n",
  71. result, inline_result);
  72. exit(1);
  73. }
  74. bpf_map_delete_elem(REG_RESULT_H, &result_key);
  75. bpf_map_delete_elem(INLINE_RESULT_H, &result_key);
  76. printf("Pass\n");
  77. }
  78. }
  79. int main(int argc, char **argv)
  80. {
  81. struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
  82. char filename[256];
  83. assert(!setrlimit(RLIMIT_MEMLOCK, &r));
  84. snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
  85. if (load_bpf_file(filename)) {
  86. printf("%s", bpf_log_buf);
  87. return 1;
  88. }
  89. test_map_in_map();
  90. return 0;
  91. }