maps.rs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //! Map struct and type bindings.
  2. use core::mem;
  3. use crate::thiserror::{self, Error};
  4. use alloc::vec::Vec;
  5. /// Invalid map type encontered
  6. pub struct InvalidMapTypeError {
  7. /// The map type
  8. pub map_type: u32,
  9. }
  10. impl TryFrom<u32> for crate::generated::bpf_map_type {
  11. type Error = InvalidMapTypeError;
  12. fn try_from(map_type: u32) -> Result<Self, Self::Error> {
  13. use crate::generated::bpf_map_type::*;
  14. Ok(match map_type {
  15. x if x == BPF_MAP_TYPE_UNSPEC as u32 => BPF_MAP_TYPE_UNSPEC,
  16. x if x == BPF_MAP_TYPE_HASH as u32 => BPF_MAP_TYPE_HASH,
  17. x if x == BPF_MAP_TYPE_ARRAY as u32 => BPF_MAP_TYPE_ARRAY,
  18. x if x == BPF_MAP_TYPE_PROG_ARRAY as u32 => BPF_MAP_TYPE_PROG_ARRAY,
  19. x if x == BPF_MAP_TYPE_PERF_EVENT_ARRAY as u32 => BPF_MAP_TYPE_PERF_EVENT_ARRAY,
  20. x if x == BPF_MAP_TYPE_PERCPU_HASH as u32 => BPF_MAP_TYPE_PERCPU_HASH,
  21. x if x == BPF_MAP_TYPE_PERCPU_ARRAY as u32 => BPF_MAP_TYPE_PERCPU_ARRAY,
  22. x if x == BPF_MAP_TYPE_STACK_TRACE as u32 => BPF_MAP_TYPE_STACK_TRACE,
  23. x if x == BPF_MAP_TYPE_CGROUP_ARRAY as u32 => BPF_MAP_TYPE_CGROUP_ARRAY,
  24. x if x == BPF_MAP_TYPE_LRU_HASH as u32 => BPF_MAP_TYPE_LRU_HASH,
  25. x if x == BPF_MAP_TYPE_LRU_PERCPU_HASH as u32 => BPF_MAP_TYPE_LRU_PERCPU_HASH,
  26. x if x == BPF_MAP_TYPE_LPM_TRIE as u32 => BPF_MAP_TYPE_LPM_TRIE,
  27. x if x == BPF_MAP_TYPE_BLOOM_FILTER as u32 => BPF_MAP_TYPE_BLOOM_FILTER,
  28. x if x == BPF_MAP_TYPE_ARRAY_OF_MAPS as u32 => BPF_MAP_TYPE_ARRAY_OF_MAPS,
  29. x if x == BPF_MAP_TYPE_HASH_OF_MAPS as u32 => BPF_MAP_TYPE_HASH_OF_MAPS,
  30. x if x == BPF_MAP_TYPE_DEVMAP as u32 => BPF_MAP_TYPE_DEVMAP,
  31. x if x == BPF_MAP_TYPE_SOCKMAP as u32 => BPF_MAP_TYPE_SOCKMAP,
  32. x if x == BPF_MAP_TYPE_CPUMAP as u32 => BPF_MAP_TYPE_CPUMAP,
  33. x if x == BPF_MAP_TYPE_XSKMAP as u32 => BPF_MAP_TYPE_XSKMAP,
  34. x if x == BPF_MAP_TYPE_SOCKHASH as u32 => BPF_MAP_TYPE_SOCKHASH,
  35. x if x == BPF_MAP_TYPE_CGROUP_STORAGE as u32 => BPF_MAP_TYPE_CGROUP_STORAGE,
  36. x if x == BPF_MAP_TYPE_REUSEPORT_SOCKARRAY as u32 => BPF_MAP_TYPE_REUSEPORT_SOCKARRAY,
  37. x if x == BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE as u32 => {
  38. BPF_MAP_TYPE_PERCPU_CGROUP_STORAGE
  39. }
  40. x if x == BPF_MAP_TYPE_QUEUE as u32 => BPF_MAP_TYPE_QUEUE,
  41. x if x == BPF_MAP_TYPE_STACK as u32 => BPF_MAP_TYPE_STACK,
  42. x if x == BPF_MAP_TYPE_SK_STORAGE as u32 => BPF_MAP_TYPE_SK_STORAGE,
  43. x if x == BPF_MAP_TYPE_DEVMAP_HASH as u32 => BPF_MAP_TYPE_DEVMAP_HASH,
  44. x if x == BPF_MAP_TYPE_STRUCT_OPS as u32 => BPF_MAP_TYPE_STRUCT_OPS,
  45. x if x == BPF_MAP_TYPE_RINGBUF as u32 => BPF_MAP_TYPE_RINGBUF,
  46. x if x == BPF_MAP_TYPE_INODE_STORAGE as u32 => BPF_MAP_TYPE_INODE_STORAGE,
  47. x if x == BPF_MAP_TYPE_TASK_STORAGE as u32 => BPF_MAP_TYPE_TASK_STORAGE,
  48. _ => return Err(InvalidMapTypeError { map_type }),
  49. })
  50. }
  51. }
  52. /// BTF definition of a map
  53. #[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
  54. pub struct BtfMapDef {
  55. pub(crate) map_type: u32,
  56. pub(crate) key_size: u32,
  57. pub(crate) value_size: u32,
  58. pub(crate) max_entries: u32,
  59. pub(crate) map_flags: u32,
  60. pub(crate) pinning: PinningType,
  61. /// BTF type id of the map key
  62. pub btf_key_type_id: u32,
  63. /// BTF type id of the map value
  64. pub btf_value_type_id: u32,
  65. }
  66. /// The pinning type
  67. ///
  68. /// Upon pinning a map, a file representation is created for the map,
  69. /// so that the map can be alive and retrievable across sessions.
  70. #[repr(u32)]
  71. #[derive(Copy, Clone, Debug, PartialEq, Eq)]
  72. pub enum PinningType {
  73. /// No pinning
  74. None = 0,
  75. /// Pin by the name
  76. ByName = 1,
  77. }
  78. /// The error type returned when failing to parse a [PinningType]
  79. #[derive(Debug, Error)]
  80. pub enum PinningError {
  81. /// Unsupported pinning type
  82. #[error("unsupported pinning type `{pinning_type}`")]
  83. Unsupported {
  84. /// The unsupported pinning type
  85. pinning_type: u32,
  86. },
  87. }
  88. impl TryFrom<u32> for PinningType {
  89. type Error = PinningError;
  90. fn try_from(value: u32) -> Result<Self, Self::Error> {
  91. match value {
  92. 0 => Ok(PinningType::None),
  93. 1 => Ok(PinningType::ByName),
  94. pinning_type => Err(PinningError::Unsupported { pinning_type }),
  95. }
  96. }
  97. }
  98. impl Default for PinningType {
  99. fn default() -> Self {
  100. PinningType::None
  101. }
  102. }
  103. /// Map definition in legacy BPF map declaration style
  104. #[allow(non_camel_case_types)]
  105. #[repr(C)]
  106. #[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
  107. pub struct bpf_map_def {
  108. // minimum features required by old BPF programs
  109. /// The map type
  110. pub map_type: u32,
  111. /// The key_size
  112. pub key_size: u32,
  113. /// The value size
  114. pub value_size: u32,
  115. /// Max entry number
  116. pub max_entries: u32,
  117. /// Map flags
  118. pub map_flags: u32,
  119. // optional features
  120. /// Id
  121. pub id: u32,
  122. /// Pinning type
  123. pub pinning: PinningType,
  124. }
  125. /// The first five __u32 of `bpf_map_def` must be defined.
  126. pub(crate) const MINIMUM_MAP_SIZE: usize = mem::size_of::<u32>() * 5;
  127. /// Kinds of maps
  128. #[derive(Debug, Copy, Clone, PartialEq, Eq)]
  129. pub enum MapKind {
  130. /// A map holding `.bss` section data
  131. Bss,
  132. /// A map holding `.data` section data
  133. Data,
  134. /// A map holding `.rodata` section data
  135. Rodata,
  136. /// Other maps
  137. Other,
  138. }
  139. impl From<&str> for MapKind {
  140. fn from(s: &str) -> Self {
  141. if s == ".bss" {
  142. MapKind::Bss
  143. } else if s.starts_with(".data") {
  144. MapKind::Data
  145. } else if s.starts_with(".rodata") {
  146. MapKind::Rodata
  147. } else {
  148. MapKind::Other
  149. }
  150. }
  151. }
  152. /// Map data defined in `maps` or `.maps` sections
  153. #[derive(Debug, Clone)]
  154. pub enum Map {
  155. /// A map defined in the `maps` section
  156. Legacy(LegacyMap),
  157. /// A map defined in the `.maps` section
  158. Btf(BtfMap),
  159. }
  160. impl Map {
  161. /// Returns the map type
  162. pub fn map_type(&self) -> u32 {
  163. match self {
  164. Map::Legacy(m) => m.def.map_type,
  165. Map::Btf(m) => m.def.map_type,
  166. }
  167. }
  168. /// Returns the key size in bytes
  169. pub fn key_size(&self) -> u32 {
  170. match self {
  171. Map::Legacy(m) => m.def.key_size,
  172. Map::Btf(m) => m.def.key_size,
  173. }
  174. }
  175. /// Returns the value size in bytes
  176. pub fn value_size(&self) -> u32 {
  177. match self {
  178. Map::Legacy(m) => m.def.value_size,
  179. Map::Btf(m) => m.def.value_size,
  180. }
  181. }
  182. /// Returns the max entry number
  183. pub fn max_entries(&self) -> u32 {
  184. match self {
  185. Map::Legacy(m) => m.def.max_entries,
  186. Map::Btf(m) => m.def.max_entries,
  187. }
  188. }
  189. /// Sets the max entry number
  190. pub fn set_max_entries(&mut self, v: u32) {
  191. match self {
  192. Map::Legacy(m) => m.def.max_entries = v,
  193. Map::Btf(m) => m.def.max_entries = v,
  194. }
  195. }
  196. /// Returns the map flags
  197. pub fn map_flags(&self) -> u32 {
  198. match self {
  199. Map::Legacy(m) => m.def.map_flags,
  200. Map::Btf(m) => m.def.map_flags,
  201. }
  202. }
  203. /// Returns the pinning type of the map
  204. pub fn pinning(&self) -> PinningType {
  205. match self {
  206. Map::Legacy(m) => m.def.pinning,
  207. Map::Btf(m) => m.def.pinning,
  208. }
  209. }
  210. /// Returns the map data
  211. pub fn data(&self) -> &[u8] {
  212. match self {
  213. Map::Legacy(m) => &m.data,
  214. Map::Btf(m) => &m.data,
  215. }
  216. }
  217. /// Returns the map data as mutable
  218. pub fn data_mut(&mut self) -> &mut Vec<u8> {
  219. match self {
  220. Map::Legacy(m) => m.data.as_mut(),
  221. Map::Btf(m) => m.data.as_mut(),
  222. }
  223. }
  224. /// Returns the map kind
  225. pub fn kind(&self) -> MapKind {
  226. match self {
  227. Map::Legacy(m) => m.kind,
  228. Map::Btf(m) => m.kind,
  229. }
  230. }
  231. /// Returns the section index
  232. pub fn section_index(&self) -> usize {
  233. match self {
  234. Map::Legacy(m) => m.section_index,
  235. Map::Btf(m) => m.section_index,
  236. }
  237. }
  238. /// Returns the symbol index
  239. pub fn symbol_index(&self) -> usize {
  240. match self {
  241. Map::Legacy(m) => m.symbol_index,
  242. Map::Btf(m) => m.symbol_index,
  243. }
  244. }
  245. }
  246. /// A map declared with legacy BPF map declaration style, most likely from a `maps` section.
  247. ///
  248. /// See [Drop support for legacy BPF map declaration syntax - Libbpf: the road to v1.0](https://github.com/libbpf/libbpf/wiki/Libbpf:-the-road-to-v1.0#drop-support-for-legacy-bpf-map-declaration-syntax)
  249. /// for more info.
  250. #[derive(Debug, Clone)]
  251. pub struct LegacyMap {
  252. /// The definition of the map
  253. pub def: bpf_map_def,
  254. /// The section index
  255. pub section_index: usize,
  256. /// The symbol index
  257. pub symbol_index: usize,
  258. /// The map data
  259. pub data: Vec<u8>,
  260. /// The map kind
  261. pub kind: MapKind,
  262. }
  263. /// A BTF-defined map, most likely from a `.maps` section.
  264. #[derive(Debug, Clone)]
  265. pub struct BtfMap {
  266. /// The definition of the map
  267. pub def: BtfMapDef,
  268. pub(crate) section_index: usize,
  269. pub(crate) symbol_index: usize,
  270. pub(crate) kind: MapKind,
  271. pub(crate) data: Vec<u8>,
  272. }