helpers.c 3.4 KB

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