bpf.h 4.2 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. #ifndef _UAPI__LINUX_BPF_H__
  8. #define _UAPI__LINUX_BPF_H__
  9. #include <linux/types.h>
  10. #include <linux/bpf_common.h>
  11. /* Extended instruction set based on top of classic BPF */
  12. /* instruction classes */
  13. #define BPF_ALU64 0x07 /* alu mode in double word width */
  14. /* ld/ldx fields */
  15. #define BPF_DW 0x18 /* double word */
  16. #define BPF_XADD 0xc0 /* exclusive add */
  17. /* alu/jmp fields */
  18. #define BPF_MOV 0xb0 /* mov reg to reg */
  19. #define BPF_ARSH 0xc0 /* sign extending arithmetic shift right */
  20. /* change endianness of a register */
  21. #define BPF_END 0xd0 /* flags for endianness conversion: */
  22. #define BPF_TO_LE 0x00 /* convert to little-endian */
  23. #define BPF_TO_BE 0x08 /* convert to big-endian */
  24. #define BPF_FROM_LE BPF_TO_LE
  25. #define BPF_FROM_BE BPF_TO_BE
  26. #define BPF_JNE 0x50 /* jump != */
  27. #define BPF_JSGT 0x60 /* SGT is signed '>', GT in x86 */
  28. #define BPF_JSGE 0x70 /* SGE is signed '>=', GE in x86 */
  29. #define BPF_CALL 0x80 /* function call */
  30. #define BPF_EXIT 0x90 /* function return */
  31. /* Register numbers */
  32. enum {
  33. BPF_REG_0 = 0,
  34. BPF_REG_1,
  35. BPF_REG_2,
  36. BPF_REG_3,
  37. BPF_REG_4,
  38. BPF_REG_5,
  39. BPF_REG_6,
  40. BPF_REG_7,
  41. BPF_REG_8,
  42. BPF_REG_9,
  43. BPF_REG_10,
  44. __MAX_BPF_REG,
  45. };
  46. /* BPF has 10 general purpose 64-bit registers and stack frame. */
  47. #define MAX_BPF_REG __MAX_BPF_REG
  48. struct bpf_insn {
  49. __u8 code; /* opcode */
  50. __u8 dst_reg:4; /* dest register */
  51. __u8 src_reg:4; /* source register */
  52. __s16 off; /* signed offset */
  53. __s32 imm; /* signed immediate constant */
  54. };
  55. /* BPF syscall commands */
  56. enum bpf_cmd {
  57. /* create a map with given type and attributes
  58. * fd = bpf(BPF_MAP_CREATE, union bpf_attr *, u32 size)
  59. * returns fd or negative error
  60. * map is deleted when fd is closed
  61. */
  62. BPF_MAP_CREATE,
  63. /* lookup key in a given map
  64. * err = bpf(BPF_MAP_LOOKUP_ELEM, union bpf_attr *attr, u32 size)
  65. * Using attr->map_fd, attr->key, attr->value
  66. * returns zero and stores found elem into value
  67. * or negative error
  68. */
  69. BPF_MAP_LOOKUP_ELEM,
  70. /* create or update key/value pair in a given map
  71. * err = bpf(BPF_MAP_UPDATE_ELEM, union bpf_attr *attr, u32 size)
  72. * Using attr->map_fd, attr->key, attr->value
  73. * returns zero or negative error
  74. */
  75. BPF_MAP_UPDATE_ELEM,
  76. /* find and delete elem by key in a given map
  77. * err = bpf(BPF_MAP_DELETE_ELEM, union bpf_attr *attr, u32 size)
  78. * Using attr->map_fd, attr->key
  79. * returns zero or negative error
  80. */
  81. BPF_MAP_DELETE_ELEM,
  82. /* lookup key in a given map and return next key
  83. * err = bpf(BPF_MAP_GET_NEXT_KEY, union bpf_attr *attr, u32 size)
  84. * Using attr->map_fd, attr->key, attr->next_key
  85. * returns zero and stores next key or negative error
  86. */
  87. BPF_MAP_GET_NEXT_KEY,
  88. /* verify and load eBPF program
  89. * prog_fd = bpf(BPF_PROG_LOAD, union bpf_attr *attr, u32 size)
  90. * Using attr->prog_type, attr->insns, attr->license
  91. * returns fd or negative error
  92. */
  93. BPF_PROG_LOAD,
  94. };
  95. enum bpf_map_type {
  96. BPF_MAP_TYPE_UNSPEC,
  97. };
  98. enum bpf_prog_type {
  99. BPF_PROG_TYPE_UNSPEC,
  100. };
  101. union bpf_attr {
  102. struct { /* anonymous struct used by BPF_MAP_CREATE command */
  103. __u32 map_type; /* one of enum bpf_map_type */
  104. __u32 key_size; /* size of key in bytes */
  105. __u32 value_size; /* size of value in bytes */
  106. __u32 max_entries; /* max number of entries in a map */
  107. };
  108. struct { /* anonymous struct used by BPF_MAP_*_ELEM commands */
  109. __u32 map_fd;
  110. __aligned_u64 key;
  111. union {
  112. __aligned_u64 value;
  113. __aligned_u64 next_key;
  114. };
  115. };
  116. struct { /* anonymous struct used by BPF_PROG_LOAD command */
  117. __u32 prog_type; /* one of enum bpf_prog_type */
  118. __u32 insn_cnt;
  119. __aligned_u64 insns;
  120. __aligned_u64 license;
  121. __u32 log_level; /* verbosity level of verifier */
  122. __u32 log_size; /* size of user buffer */
  123. __aligned_u64 log_buf; /* user supplied buffer */
  124. };
  125. } __attribute__((aligned(8)));
  126. /* integer value in 'imm' field of BPF_CALL instruction selects which helper
  127. * function eBPF program intends to call
  128. */
  129. enum bpf_func_id {
  130. BPF_FUNC_unspec,
  131. __BPF_FUNC_MAX_ID,
  132. };
  133. #endif /* _UAPI__LINUX_BPF_H__ */