test_stub.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #include <linux/kernel.h>
  8. #include <linux/types.h>
  9. #include <linux/slab.h>
  10. #include <linux/err.h>
  11. #include <linux/bpf.h>
  12. /* test stubs for BPF_MAP_TYPE_UNSPEC and for BPF_PROG_TYPE_UNSPEC
  13. * to be used by user space verifier testsuite
  14. */
  15. struct bpf_context {
  16. u64 arg1;
  17. u64 arg2;
  18. };
  19. static const struct bpf_func_proto *test_func_proto(enum bpf_func_id func_id)
  20. {
  21. switch (func_id) {
  22. case BPF_FUNC_map_lookup_elem:
  23. return &bpf_map_lookup_elem_proto;
  24. case BPF_FUNC_map_update_elem:
  25. return &bpf_map_update_elem_proto;
  26. case BPF_FUNC_map_delete_elem:
  27. return &bpf_map_delete_elem_proto;
  28. default:
  29. return NULL;
  30. }
  31. }
  32. static const struct bpf_context_access {
  33. int size;
  34. enum bpf_access_type type;
  35. } test_ctx_access[] = {
  36. [offsetof(struct bpf_context, arg1)] = {
  37. FIELD_SIZEOF(struct bpf_context, arg1),
  38. BPF_READ
  39. },
  40. [offsetof(struct bpf_context, arg2)] = {
  41. FIELD_SIZEOF(struct bpf_context, arg2),
  42. BPF_READ
  43. },
  44. };
  45. static bool test_is_valid_access(int off, int size, enum bpf_access_type type)
  46. {
  47. const struct bpf_context_access *access;
  48. if (off < 0 || off >= ARRAY_SIZE(test_ctx_access))
  49. return false;
  50. access = &test_ctx_access[off];
  51. if (access->size == size && (access->type & type))
  52. return true;
  53. return false;
  54. }
  55. static struct bpf_verifier_ops test_ops = {
  56. .get_func_proto = test_func_proto,
  57. .is_valid_access = test_is_valid_access,
  58. };
  59. static struct bpf_prog_type_list tl_prog = {
  60. .ops = &test_ops,
  61. .type = BPF_PROG_TYPE_UNSPEC,
  62. };
  63. static int __init register_test_ops(void)
  64. {
  65. bpf_register_prog_type(&tl_prog);
  66. return 0;
  67. }
  68. late_initcall(register_test_ops);