helpers.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/rcupdate.h>
  14. /* If kernel subsystem is allowing eBPF programs to call this function,
  15. * inside its own verifier_ops->get_func_proto() callback it should return
  16. * bpf_map_lookup_elem_proto, so that verifier can properly check the arguments
  17. *
  18. * Different map implementations will rely on rcu in map methods
  19. * lookup/update/delete, therefore eBPF programs must run under rcu lock
  20. * if program is allowed to access maps, so check rcu_read_lock_held in
  21. * all three functions.
  22. */
  23. static u64 bpf_map_lookup_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  24. {
  25. /* verifier checked that R1 contains a valid pointer to bpf_map
  26. * and R2 points to a program stack and map->key_size bytes were
  27. * initialized
  28. */
  29. struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
  30. void *key = (void *) (unsigned long) r2;
  31. void *value;
  32. WARN_ON_ONCE(!rcu_read_lock_held());
  33. value = map->ops->map_lookup_elem(map, key);
  34. /* lookup() returns either pointer to element value or NULL
  35. * which is the meaning of PTR_TO_MAP_VALUE_OR_NULL type
  36. */
  37. return (unsigned long) value;
  38. }
  39. struct bpf_func_proto bpf_map_lookup_elem_proto = {
  40. .func = bpf_map_lookup_elem,
  41. .gpl_only = false,
  42. .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
  43. .arg1_type = ARG_CONST_MAP_PTR,
  44. .arg2_type = ARG_PTR_TO_MAP_KEY,
  45. };
  46. static u64 bpf_map_update_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  47. {
  48. struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
  49. void *key = (void *) (unsigned long) r2;
  50. void *value = (void *) (unsigned long) r3;
  51. WARN_ON_ONCE(!rcu_read_lock_held());
  52. return map->ops->map_update_elem(map, key, value, r4);
  53. }
  54. struct bpf_func_proto bpf_map_update_elem_proto = {
  55. .func = bpf_map_update_elem,
  56. .gpl_only = false,
  57. .ret_type = RET_INTEGER,
  58. .arg1_type = ARG_CONST_MAP_PTR,
  59. .arg2_type = ARG_PTR_TO_MAP_KEY,
  60. .arg3_type = ARG_PTR_TO_MAP_VALUE,
  61. .arg4_type = ARG_ANYTHING,
  62. };
  63. static u64 bpf_map_delete_elem(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5)
  64. {
  65. struct bpf_map *map = (struct bpf_map *) (unsigned long) r1;
  66. void *key = (void *) (unsigned long) r2;
  67. WARN_ON_ONCE(!rcu_read_lock_held());
  68. return map->ops->map_delete_elem(map, key);
  69. }
  70. struct bpf_func_proto bpf_map_delete_elem_proto = {
  71. .func = bpf_map_delete_elem,
  72. .gpl_only = false,
  73. .ret_type = RET_INTEGER,
  74. .arg1_type = ARG_CONST_MAP_PTR,
  75. .arg2_type = ARG_PTR_TO_MAP_KEY,
  76. };