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 <llvm/Config/llvm-config.h>
26 
27 #include "bcc_exception.h"
28 #include "table_storage.h"
29 
30 namespace llvm {
31 class ExecutionEngine;
32 class Function;
33 class LLVMContext;
34 class Module;
35 class Type;
36 }
37 
38 struct bpf_insn;
39 
40 namespace ebpf {
41 
42 typedef std::map<std::string, std::tuple<uint8_t *, uintptr_t, unsigned>> sec_map_def;
43 
44 // Options to enable different debug logging.
45 enum {
46   // Debug output compiled LLVM IR.
47   DEBUG_LLVM_IR = 0x1,
48   // Debug output loaded BPF bytecode and register state on branches.
49   DEBUG_BPF = 0x2,
50   // Debug output pre-processor result.
51   DEBUG_PREPROCESSOR = 0x4,
52   // Debug output ASM instructions embedded with source.
53   DEBUG_SOURCE = 0x8,
54   // Debug output register state on all instructions in addition to DEBUG_BPF.
55   DEBUG_BPF_REGISTER_STATE = 0x10,
56   // Debug BTF.
57   DEBUG_BTF = 0x20,
58 };
59 
60 class TableDesc;
61 class TableStorage;
62 class BLoader;
63 class ClangLoader;
64 class ProgFuncInfo;
65 class BTF;
66 
67 bool bpf_module_rw_engine_enabled(void);
68 
69 class BPFModule {
70  private:
71   int init_engine();
72   void initialize_rw_engine();
73   void cleanup_rw_engine();
74   int parse(llvm::Module *mod);
75   int finalize();
76   int annotate();
77   void annotate_light();
78   void finalize_prog_func_info();
79   std::unique_ptr<llvm::ExecutionEngine> finalize_rw(std::unique_ptr<llvm::Module> mod);
80   std::string make_reader(llvm::Module *mod, llvm::Type *type);
81   std::string make_writer(llvm::Module *mod, llvm::Type *type);
82   void dump_ir(llvm::Module &mod);
83   int load_file_module(std::unique_ptr<llvm::Module> *mod, const std::string &file, bool in_memory);
84   int load_includes(const std::string &text);
85   int load_cfile(const std::string &file, bool in_memory, const char *cflags[], int ncflags);
86   int kbuild_flags(const char *uname_release, std::vector<std::string> *cflags);
87   int run_pass_manager(llvm::Module &mod);
88   StatusTuple sscanf(std::string fn_name, const char *str, void *val);
89   StatusTuple snprintf(std::string fn_name, char *str, size_t sz,
90                        const void *val);
91   void load_btf(sec_map_def &sections);
92   int load_maps(sec_map_def &sections);
93   int create_maps(std::map<std::string, std::pair<int, int>> &map_tids,
94                   std::map<int, int> &map_fds,
95                   std::map<std::string, int> &inner_map_fds,
96                   bool for_inner_map);
97 
98  public:
99   BPFModule(unsigned flags, TableStorage *ts = nullptr, bool rw_engine_enabled = true,
100             const std::string &maps_ns = "", bool allow_rlimit = true,
101             const char *dev_name = nullptr);
102   ~BPFModule();
103   int free_bcc_memory();
104   int load_c(const std::string &filename, const char *cflags[], int ncflags);
105   int load_string(const std::string &text, const char *cflags[], int ncflags);
id()106   std::string id() const { return id_; }
maps_ns()107   std::string maps_ns() const { return maps_ns_; }
108   size_t num_functions() const;
109   uint8_t * function_start(size_t id) const;
110   uint8_t * function_start(const std::string &name) const;
111   const char * function_source(const std::string &name) const;
112   const char * function_source_rewritten(const std::string &name) const;
113   int annotate_prog_tag(const std::string &name, int fd,
114 			struct bpf_insn *insn, int prog_len);
115   const char * function_name(size_t id) const;
116   size_t function_size(size_t id) const;
117   size_t function_size(const std::string &name) const;
118   size_t num_tables() const;
119   size_t table_id(const std::string &name) const;
120   int table_fd(size_t id) const;
121   int table_fd(const std::string &name) const;
122   const char * table_name(size_t id) const;
123   int table_type(const std::string &name) const;
124   int table_type(size_t id) const;
125   size_t table_max_entries(const std::string &name) const;
126   size_t table_max_entries(size_t id) const;
127   int table_flags(const std::string &name) const;
128   int table_flags(size_t id) const;
129   const char * table_key_desc(size_t id) const;
130   const char * table_key_desc(const std::string &name) const;
131   size_t table_key_size(size_t id) const;
132   size_t table_key_size(const std::string &name) const;
133   int table_key_printf(size_t id, char *buf, size_t buflen, const void *key);
134   int table_key_scanf(size_t id, const char *buf, void *key);
135   const char * table_leaf_desc(size_t id) const;
136   const char * table_leaf_desc(const std::string &name) const;
137   size_t table_leaf_size(size_t id) const;
138   size_t table_leaf_size(const std::string &name) const;
139   int table_leaf_printf(size_t id, char *buf, size_t buflen, const void *leaf);
140   int table_leaf_scanf(size_t id, const char *buf, void *leaf);
141   char * license() const;
142   unsigned kern_version() const;
table_storage()143   TableStorage &table_storage() { return *ts_; }
144   int bcc_func_load(int prog_type, const char *name,
145                     const struct bpf_insn *insns, int prog_len,
146                     const char *license, unsigned kern_version,
147                     int log_level, char *log_buf, unsigned log_buf_size,
148                     const char *dev_name = nullptr,
149                     unsigned flags = 0, int attach_type = -1);
150   int bcc_func_attach(int prog_fd, int attachable_fd,
151                       int attach_type, unsigned int flags);
152   int bcc_func_detach(int prog_fd, int attachable_fd, int attach_type);
153   size_t perf_event_fields(const char *) const;
154   const char * perf_event_field(const char *, size_t i) const;
155 
156  private:
157   unsigned flags_;  // 0x1 for printing
158   bool rw_engine_enabled_;
159   bool used_b_loader_;
160   bool allow_rlimit_;
161   std::string filename_;
162   std::string proto_filename_;
163   std::unique_ptr<llvm::LLVMContext> ctx_;
164   std::unique_ptr<llvm::ExecutionEngine> engine_;
165   std::unique_ptr<llvm::ExecutionEngine> rw_engine_;
166   std::unique_ptr<llvm::Module> mod_;
167   std::unique_ptr<ProgFuncInfo> prog_func_info_;
168   sec_map_def sections_;
169   std::vector<TableDesc *> tables_;
170   std::map<std::string, size_t> table_names_;
171   std::map<llvm::Type *, std::string> readers_;
172   std::map<llvm::Type *, std::string> writers_;
173   std::string id_;
174   std::string maps_ns_;
175   std::string mod_src_;
176   std::map<std::string, std::string> src_dbg_fmap_;
177   TableStorage *ts_;
178   std::unique_ptr<TableStorage> local_ts_;
179   BTF *btf_;
180   fake_fd_map_def fake_fd_map_;
181   unsigned int ifindex_;
182 
183   // map of events -- key: event name, value: event fields
184   std::map<std::string, std::vector<std::string>> perf_events_;
185 };
186 
187 }  // namespace ebpf
188