arraymap.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* Copyright (c) 2011-2014 PLUMgrid, http://plumgrid.com
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * General Public License for more details.
  11. */
  12. #include <linux/bpf.h>
  13. #include <linux/err.h>
  14. #include <linux/vmalloc.h>
  15. #include <linux/slab.h>
  16. #include <linux/mm.h>
  17. struct bpf_array {
  18. struct bpf_map map;
  19. u32 elem_size;
  20. char value[0] __aligned(8);
  21. };
  22. /* Called from syscall */
  23. static struct bpf_map *array_map_alloc(union bpf_attr *attr)
  24. {
  25. struct bpf_array *array;
  26. u32 elem_size, array_size;
  27. /* check sanity of attributes */
  28. if (attr->max_entries == 0 || attr->key_size != 4 ||
  29. attr->value_size == 0)
  30. return ERR_PTR(-EINVAL);
  31. elem_size = round_up(attr->value_size, 8);
  32. /* check round_up into zero and u32 overflow */
  33. if (elem_size == 0 ||
  34. attr->max_entries > (U32_MAX - sizeof(*array)) / elem_size)
  35. return ERR_PTR(-ENOMEM);
  36. array_size = sizeof(*array) + attr->max_entries * elem_size;
  37. /* allocate all map elements and zero-initialize them */
  38. array = kzalloc(array_size, GFP_USER | __GFP_NOWARN);
  39. if (!array) {
  40. array = vzalloc(array_size);
  41. if (!array)
  42. return ERR_PTR(-ENOMEM);
  43. }
  44. /* copy mandatory map attributes */
  45. array->map.key_size = attr->key_size;
  46. array->map.value_size = attr->value_size;
  47. array->map.max_entries = attr->max_entries;
  48. array->elem_size = elem_size;
  49. return &array->map;
  50. }
  51. /* Called from syscall or from eBPF program */
  52. static void *array_map_lookup_elem(struct bpf_map *map, void *key)
  53. {
  54. struct bpf_array *array = container_of(map, struct bpf_array, map);
  55. u32 index = *(u32 *)key;
  56. if (index >= array->map.max_entries)
  57. return NULL;
  58. return array->value + array->elem_size * index;
  59. }
  60. /* Called from syscall */
  61. static int array_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
  62. {
  63. struct bpf_array *array = container_of(map, struct bpf_array, map);
  64. u32 index = *(u32 *)key;
  65. u32 *next = (u32 *)next_key;
  66. if (index >= array->map.max_entries) {
  67. *next = 0;
  68. return 0;
  69. }
  70. if (index == array->map.max_entries - 1)
  71. return -ENOENT;
  72. *next = index + 1;
  73. return 0;
  74. }
  75. /* Called from syscall or from eBPF program */
  76. static int array_map_update_elem(struct bpf_map *map, void *key, void *value,
  77. u64 map_flags)
  78. {
  79. struct bpf_array *array = container_of(map, struct bpf_array, map);
  80. u32 index = *(u32 *)key;
  81. if (map_flags > BPF_EXIST)
  82. /* unknown flags */
  83. return -EINVAL;
  84. if (index >= array->map.max_entries)
  85. /* all elements were pre-allocated, cannot insert a new one */
  86. return -E2BIG;
  87. if (map_flags == BPF_NOEXIST)
  88. /* all elements already exist */
  89. return -EEXIST;
  90. memcpy(array->value + array->elem_size * index, value, array->elem_size);
  91. return 0;
  92. }
  93. /* Called from syscall or from eBPF program */
  94. static int array_map_delete_elem(struct bpf_map *map, void *key)
  95. {
  96. return -EINVAL;
  97. }
  98. /* Called when map->refcnt goes to zero, either from workqueue or from syscall */
  99. static void array_map_free(struct bpf_map *map)
  100. {
  101. struct bpf_array *array = container_of(map, struct bpf_array, map);
  102. /* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
  103. * so the programs (can be more than one that used this map) were
  104. * disconnected from events. Wait for outstanding programs to complete
  105. * and free the array
  106. */
  107. synchronize_rcu();
  108. kvfree(array);
  109. }
  110. static struct bpf_map_ops array_ops = {
  111. .map_alloc = array_map_alloc,
  112. .map_free = array_map_free,
  113. .map_get_next_key = array_map_get_next_key,
  114. .map_lookup_elem = array_map_lookup_elem,
  115. .map_update_elem = array_map_update_elem,
  116. .map_delete_elem = array_map_delete_elem,
  117. };
  118. static struct bpf_map_type_list tl = {
  119. .ops = &array_ops,
  120. .type = BPF_MAP_TYPE_ARRAY,
  121. };
  122. static int __init register_array_map(void)
  123. {
  124. bpf_register_map_type(&tl);
  125. return 0;
  126. }
  127. late_initcall(register_array_map);