• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2015 PLUMgrid, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <stdint.h>
20 #include <map>
21 #include <memory>
22 #include <string>
23 #include <vector>
24 
25 #include "bcc_exception.h"
26 #include "table_storage.h"
27 
28 namespace llvm {
29 class ExecutionEngine;
30 class Function;
31 class LLVMContext;
32 class Module;
33 class Type;
34 }
35 
36 struct bpf_insn;
37 
38 namespace ebpf {
39 
40 typedef std::map<std::string, std::tuple<uint8_t *, uintptr_t, unsigned>> sec_map_def;
41 
42 // Options to enable different debug logging.
43 enum {
44   // Debug output compiled LLVM IR.
45   DEBUG_LLVM_IR = 0x1,
46   // Debug output loaded BPF bytecode and register state on branches.
47   DEBUG_BPF = 0x2,
48   // Debug output pre-processor result.
49   DEBUG_PREPROCESSOR = 0x4,
50   // Debug output ASM instructions embedded with source.
51   DEBUG_SOURCE = 0x8,
52   // Debug output register state on all instructions in addition to DEBUG_BPF.
53   DEBUG_BPF_REGISTER_STATE = 0x10,
54   // Debug BTF.
55   DEBUG_BTF = 0x20,
56 };
57 
58 class TableDesc;
59 class TableStorage;
60 class BLoader;
61 class ClangLoader;
62 class ProgFuncInfo;
63 class BTF;
64 
65 bool bpf_module_rw_engine_enabled(void);
66 
67 class BPFModule {
68  private:
69   int init_engine();
70   void initialize_rw_engine();
71   void cleanup_rw_engine();
72   int parse(llvm::Module *mod);
73   int finalize();
74   int annotate();
75   void annotate_light();
76   void finalize_prog_func_info();
77   std::unique_ptr<llvm::ExecutionEngine> finalize_rw(std::unique_ptr<llvm::Module> mod);
78   std::string make_reader(llvm::Module *mod, llvm::Type *type);
79   std::string make_writer(llvm::Module *mod, llvm::Type *type);
80   void dump_ir(llvm::Module &mod);
81   int load_file_module(std::unique_ptr<llvm::Module> *mod, const std::string &file, bool in_memory);
82   int load_includes(const std::string &text);
83   int load_cfile(const std::string &file, bool in_memory, const char *cflags[], int ncflags);
84   int kbuild_flags(const char *uname_release, std::vector<std::string> *cflags);
85   int run_pass_manager(llvm::Module &mod);
86   StatusTuple sscanf(std::string fn_name, const char *str, void *val);
87   StatusTuple snprintf(std::string fn_name, char *str, size_t sz,
88                        const void *val);
89   void load_btf(sec_map_def &sections);
90   int load_maps(sec_map_def &sections);
91   int create_maps(std::map<std::string, std::pair<int, int>> &map_tids,
92                   std::map<int, int> &map_fds,
93                   std::map<std::string, int> &inner_map_fds,
94                   bool for_inner_map);
95 
96  public:
97   BPFModule(unsigned flags, TableStorage *ts = nullptr, bool rw_engine_enabled = true,
98             const std::string &maps_ns = "", bool allow_rlimit = true,
99             const char *dev_name = nullptr);
100   ~BPFModule();
101   int free_bcc_memory();
102   int load_c(const std::string &filename, const char *cflags[], int ncflags);
103   int load_string(const std::string &text, const char *cflags[], int ncflags);
id()104   std::string id() const { return id_; }
maps_ns()105   std::string maps_ns() const { return maps_ns_; }
106   size_t num_functions() const;
107   uint8_t * function_start(size_t id) const;
108   uint8_t * function_start(const std::string &name) const;
109   const char * function_source(const std::string &name) const;
110   const char * function_source_rewritten(const std::string &name) const;
111   int annotate_prog_tag(const std::string &name, int fd,
112 			struct bpf_insn *insn, int prog_len);
113   const char * function_name(size_t id) const;
114   size_t function_size(size_t id) const;
115   size_t function_size(const std::string &name) const;
116   size_t num_tables() const;
117   size_t table_id(const std::string &name) const;
118   int table_fd(size_t id) const;
119   int table_fd(const std::string &name) const;
120   const char * table_name(size_t id) const;
121   int table_type(const std::string &name) const;
122   int table_type(size_t id) const;
123   size_t table_max_entries(const std::string &name) const;
124   size_t table_max_entries(size_t id) const;
125   int table_flags(const std::string &name) const;
126   int table_flags(size_t id) const;
127   const char * table_key_desc(size_t id) const;
128   const char * table_key_desc(const std::string &name) const;
129   size_t table_key_size(size_t id) const;
130   size_t table_key_size(const std::string &name) const;
131   int table_key_printf(size_t id, char *buf, size_t buflen, const void *key);
132   int table_key_scanf(size_t id, const char *buf, void *key);
133   const char * table_leaf_desc(size_t id) const;
134   const char * table_leaf_desc(const std::string &name) const;
135   size_t table_leaf_size(size_t id) const;
136   size_t table_leaf_size(const std::string &name) const;
137   int table_leaf_printf(size_t id, char *buf, size_t buflen, const void *leaf);
138   int table_leaf_scanf(size_t id, const char *buf, void *leaf);
139   char * license() const;
140   unsigned kern_version() const;
table_storage()141   TableStorage &table_storage() { return *ts_; }
142   int bcc_func_load(int prog_type, const char *name,
143                     const struct bpf_insn *insns, int prog_len,
144                     const char *license, unsigned kern_version,
145                     int log_level, char *log_buf, unsigned log_buf_size,
146                     const char *dev_name = nullptr,
147                     unsigned flags = 0);
148   int bcc_func_attach(int prog_fd, int attachable_fd,
149                       int attach_type, unsigned int flags);
150   int bcc_func_detach(int prog_fd, int attachable_fd, int attach_type);
151   size_t perf_event_fields(const char *) const;
152   const char * perf_event_field(const char *, size_t i) const;
153 
154  private:
155   unsigned flags_;  // 0x1 for printing
156   bool rw_engine_enabled_;
157   bool used_b_loader_;
158   bool allow_rlimit_;
159   std::string filename_;
160   std::string proto_filename_;
161   std::unique_ptr<llvm::LLVMContext> ctx_;
162   std::unique_ptr<llvm::ExecutionEngine> engine_;
163   std::unique_ptr<llvm::ExecutionEngine> rw_engine_;
164   std::unique_ptr<llvm::Module> mod_;
165   std::unique_ptr<ProgFuncInfo> prog_func_info_;
166   sec_map_def sections_;
167   std::vector<TableDesc *> tables_;
168   std::map<std::string, size_t> table_names_;
169   std::map<llvm::Type *, std::string> readers_;
170   std::map<llvm::Type *, std::string> writers_;
171   std::string id_;
172   std::string maps_ns_;
173   std::string mod_src_;
174   std::map<std::string, std::string> src_dbg_fmap_;
175   TableStorage *ts_;
176   std::unique_ptr<TableStorage> local_ts_;
177   BTF *btf_;
178   fake_fd_map_def fake_fd_map_;
179   unsigned int ifindex_;
180 
181   // map of events -- key: event name, value: event fields
182   std::map<std::string, std::vector<std::string>> perf_events_;
183 };
184 
185 }  // namespace ebpf
186