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 class BPFModule { 58 private: 59 static const std::string FN_PREFIX; 60 int init_engine(); 61 int parse(llvm::Module *mod); 62 int finalize(); 63 int annotate(); 64 void annotate_light(); 65 std::unique_ptr<llvm::ExecutionEngine> finalize_rw(std::unique_ptr<llvm::Module> mod); 66 std::string make_reader(llvm::Module *mod, llvm::Type *type); 67 std::string make_writer(llvm::Module *mod, llvm::Type *type); 68 void dump_ir(llvm::Module &mod); 69 int load_file_module(std::unique_ptr<llvm::Module> *mod, const std::string &file, bool in_memory); 70 int load_includes(const std::string &text); 71 int load_cfile(const std::string &file, bool in_memory, const char *cflags[], int ncflags); 72 int kbuild_flags(const char *uname_release, std::vector<std::string> *cflags); 73 int run_pass_manager(llvm::Module &mod); 74 StatusTuple sscanf(std::string fn_name, const char *str, void *val); 75 StatusTuple snprintf(std::string fn_name, char *str, size_t sz, 76 const void *val); 77 78 public: 79 BPFModule(unsigned flags, TableStorage *ts = nullptr, bool rw_engine_enabled = true, 80 const std::string &maps_ns = ""); 81 ~BPFModule(); 82 int load_b(const std::string &filename, const std::string &proto_filename); 83 int load_c(const std::string &filename, const char *cflags[], int ncflags); 84 int load_string(const std::string &text, const char *cflags[], int ncflags); id()85 std::string id() const { return id_; } maps_ns()86 std::string maps_ns() const { return maps_ns_; } 87 size_t num_functions() const; 88 uint8_t * function_start(size_t id) const; 89 uint8_t * function_start(const std::string &name) const; 90 const char * function_source(const std::string &name) const; 91 const char * function_source_rewritten(const std::string &name) const; 92 int annotate_prog_tag(const std::string &name, int fd, 93 struct bpf_insn *insn, int prog_len); 94 const char * function_name(size_t id) const; 95 size_t function_size(size_t id) const; 96 size_t function_size(const std::string &name) const; 97 size_t num_tables() const; 98 size_t table_id(const std::string &name) const; 99 int table_fd(size_t id) const; 100 int table_fd(const std::string &name) const; 101 const char * table_name(size_t id) const; 102 int table_type(const std::string &name) const; 103 int table_type(size_t id) const; 104 size_t table_max_entries(const std::string &name) const; 105 size_t table_max_entries(size_t id) const; 106 int table_flags(const std::string &name) const; 107 int table_flags(size_t id) const; 108 const char * table_key_desc(size_t id) const; 109 const char * table_key_desc(const std::string &name) const; 110 size_t table_key_size(size_t id) const; 111 size_t table_key_size(const std::string &name) const; 112 int table_key_printf(size_t id, char *buf, size_t buflen, const void *key); 113 int table_key_scanf(size_t id, const char *buf, void *key); 114 const char * table_leaf_desc(size_t id) const; 115 const char * table_leaf_desc(const std::string &name) const; 116 size_t table_leaf_size(size_t id) const; 117 size_t table_leaf_size(const std::string &name) const; 118 int table_leaf_printf(size_t id, char *buf, size_t buflen, const void *leaf); 119 int table_leaf_scanf(size_t id, const char *buf, void *leaf); 120 char * license() const; 121 unsigned kern_version() const; table_storage()122 TableStorage &table_storage() { return *ts_; } 123 124 private: 125 unsigned flags_; // 0x1 for printing 126 bool rw_engine_enabled_; 127 bool used_b_loader_; 128 std::string filename_; 129 std::string proto_filename_; 130 std::unique_ptr<llvm::LLVMContext> ctx_; 131 std::unique_ptr<llvm::ExecutionEngine> engine_; 132 std::unique_ptr<llvm::ExecutionEngine> rw_engine_; 133 std::unique_ptr<llvm::Module> mod_; 134 std::unique_ptr<FuncSource> func_src_; 135 std::map<std::string, std::tuple<uint8_t *, uintptr_t>> sections_; 136 std::vector<TableDesc *> tables_; 137 std::map<std::string, size_t> table_names_; 138 std::vector<std::string> function_names_; 139 std::map<llvm::Type *, std::string> readers_; 140 std::map<llvm::Type *, std::string> writers_; 141 std::string id_; 142 std::string maps_ns_; 143 std::string mod_src_; 144 std::map<std::string, std::string> src_dbg_fmap_; 145 TableStorage *ts_; 146 std::unique_ptr<TableStorage> local_ts_; 147 }; 148 149 } // namespace ebpf 150