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