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 27 namespace llvm { 28 class ExecutionEngine; 29 class Function; 30 class LLVMContext; 31 class Module; 32 class Type; 33 } 34 35 namespace ebpf { 36 37 // Options to enable different debug logging. 38 enum { 39 // Debug output compiled LLVM IR. 40 DEBUG_LLVM_IR = 0x1, 41 // Debug output loaded BPF bytecode and register state on branches. 42 DEBUG_BPF = 0x2, 43 // Debug output pre-processor result. 44 DEBUG_PREPROCESSOR = 0x4, 45 // Debug output ASM instructions embedded with source. 46 DEBUG_SOURCE = 0x8, 47 // Debug output register state on all instructions in addition to DEBUG_BPF. 48 DEBUG_BPF_REGISTER_STATE = 0x10, 49 }; 50 51 class TableDesc; 52 class TableStorage; 53 class BLoader; 54 class ClangLoader; 55 class FuncSource; 56 57 bool bpf_module_rw_engine_enabled(void); 58 59 class BPFModule { 60 private: 61 static const std::string FN_PREFIX; 62 int init_engine(); 63 void initialize_rw_engine(); 64 void cleanup_rw_engine(); 65 int parse(llvm::Module *mod); 66 int finalize(); 67 int annotate(); 68 void annotate_light(); 69 std::unique_ptr<llvm::ExecutionEngine> finalize_rw(std::unique_ptr<llvm::Module> mod); 70 std::string make_reader(llvm::Module *mod, llvm::Type *type); 71 std::string make_writer(llvm::Module *mod, llvm::Type *type); 72 void dump_ir(llvm::Module &mod); 73 int load_file_module(std::unique_ptr<llvm::Module> *mod, const std::string &file, bool in_memory); 74 int load_includes(const std::string &text); 75 int load_cfile(const std::string &file, bool in_memory, const char *cflags[], int ncflags); 76 int kbuild_flags(const char *uname_release, std::vector<std::string> *cflags); 77 int run_pass_manager(llvm::Module &mod); 78 StatusTuple sscanf(std::string fn_name, const char *str, void *val); 79 StatusTuple snprintf(std::string fn_name, char *str, size_t sz, 80 const void *val); 81 82 public: 83 BPFModule(unsigned flags, TableStorage *ts = nullptr, bool rw_engine_enabled = true, 84 const std::string &maps_ns = ""); 85 ~BPFModule(); 86 int load_b(const std::string &filename, const std::string &proto_filename); 87 int load_c(const std::string &filename, const char *cflags[], int ncflags); 88 int load_string(const std::string &text, const char *cflags[], int ncflags); id()89 std::string id() const { return id_; } maps_ns()90 std::string maps_ns() const { return maps_ns_; } 91 size_t num_functions() const; 92 uint8_t * function_start(size_t id) const; 93 uint8_t * function_start(const std::string &name) const; 94 const char * function_source(const std::string &name) const; 95 const char * function_source_rewritten(const std::string &name) const; 96 int annotate_prog_tag(const std::string &name, int fd, 97 struct bpf_insn *insn, int prog_len); 98 const char * function_name(size_t id) const; 99 size_t function_size(size_t id) const; 100 size_t function_size(const std::string &name) const; 101 size_t num_tables() const; 102 size_t table_id(const std::string &name) const; 103 int table_fd(size_t id) const; 104 int table_fd(const std::string &name) const; 105 const char * table_name(size_t id) const; 106 int table_type(const std::string &name) const; 107 int table_type(size_t id) const; 108 size_t table_max_entries(const std::string &name) const; 109 size_t table_max_entries(size_t id) const; 110 int table_flags(const std::string &name) const; 111 int table_flags(size_t id) const; 112 const char * table_key_desc(size_t id) const; 113 const char * table_key_desc(const std::string &name) const; 114 size_t table_key_size(size_t id) const; 115 size_t table_key_size(const std::string &name) const; 116 int table_key_printf(size_t id, char *buf, size_t buflen, const void *key); 117 int table_key_scanf(size_t id, const char *buf, void *key); 118 const char * table_leaf_desc(size_t id) const; 119 const char * table_leaf_desc(const std::string &name) const; 120 size_t table_leaf_size(size_t id) const; 121 size_t table_leaf_size(const std::string &name) const; 122 int table_leaf_printf(size_t id, char *buf, size_t buflen, const void *leaf); 123 int table_leaf_scanf(size_t id, const char *buf, void *leaf); 124 char * license() const; 125 unsigned kern_version() const; table_storage()126 TableStorage &table_storage() { return *ts_; } 127 128 private: 129 unsigned flags_; // 0x1 for printing 130 bool rw_engine_enabled_; 131 bool used_b_loader_; 132 std::string filename_; 133 std::string proto_filename_; 134 std::unique_ptr<llvm::LLVMContext> ctx_; 135 std::unique_ptr<llvm::ExecutionEngine> engine_; 136 std::unique_ptr<llvm::ExecutionEngine> rw_engine_; 137 std::unique_ptr<llvm::Module> mod_; 138 std::unique_ptr<FuncSource> func_src_; 139 std::map<std::string, std::tuple<uint8_t *, uintptr_t>> sections_; 140 std::vector<TableDesc *> tables_; 141 std::map<std::string, size_t> table_names_; 142 std::vector<std::string> function_names_; 143 std::map<llvm::Type *, std::string> readers_; 144 std::map<llvm::Type *, std::string> writers_; 145 std::string id_; 146 std::string maps_ns_; 147 std::string mod_src_; 148 std::map<std::string, std::string> src_dbg_fmap_; 149 TableStorage *ts_; 150 std::unique_ptr<TableStorage> local_ts_; 151 }; 152 153 } // namespace ebpf 154