1
#pragma once
2

            
3
#include <cstdint>
4
#include <string>
5

            
6
#include "source/common/common/logger.h"
7

            
8
union bpf_attr;
9

            
10
namespace Envoy {
11
namespace Cilium {
12

            
13
/**
14
 * Bpf system call interface.
15
 */
16
class Bpf : public Logger::Loggable<Logger::Id::filter> {
17
public:
18
  /**
19
   * Create a bpf map object without actually creating or opening a bpf map yet.
20
   * @param map_type the type of a bpf map to be opened or created, e.g.,
21
   * BPF_MAP_TYPE_HASH
22
   * @param key_size the size of the bpf map entry lookup key.
23
   * @param min_value_size the minimum acceptable size of the bpf map entry value.
24
   * @param max_value_size the maximum size of the bpf map entry value the caller is buffering for.
25
   *        Defaults to same as min_value_size.
26
   */
27
  Bpf(uint32_t map_type, uint32_t key_size, uint32_t min_value_size, uint32_t max_value_size = 0);
28
  virtual ~Bpf();
29

            
30
  /**
31
   * Close the bpf file descriptor, if open.
32
   */
33
  void close();
34

            
35
  /**
36
   * Open an existing bpf map. The bpf map must have the map type and key and
37
   * value sizes that match with the ones given to the constructor.
38
   * @param path the file system path to the pinned bpf map.
39
   * @returns boolean for success of the operation.
40
   */
41
  bool open(const std::string& path);
42

            
43
  /**
44
   * Lookup an entry from the bpf map identified with the key, storing the found
45
   * value, if any.
46
   * @param key pointer to the key identifying the entry to be found.
47
   * @param value pointer at which the value is copied to if the entry is found.
48
   *        Enough space must be provided for 'max_value_size_' bytes.
49
   *        The caller should only examine the first 'min_value_size_' bytes.
50
   * @returns boolean for success of the operation.
51
   */
52
  bool lookup(const void* key, void* value);
53

            
54
protected:
55
  std::string path_;
56
  int fd_;
57

            
58
public:
59
  uint32_t map_type_;
60
  uint32_t key_size_;
61
  uint32_t min_value_size_;
62
  uint32_t max_value_size_;
63
  uint32_t real_value_size_;
64
};
65

            
66
} // namespace Cilium
67
} // namespace Envoy