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 <unistd.h> 20*387f9dfdSAndroid Build Coastguard Worker #include <cstdint> 21*387f9dfdSAndroid Build Coastguard Worker #include <functional> 22*387f9dfdSAndroid Build Coastguard Worker #include <memory> 23*387f9dfdSAndroid Build Coastguard Worker #include <string> 24*387f9dfdSAndroid Build Coastguard Worker 25*387f9dfdSAndroid Build Coastguard Worker #include "bcc_exception.h" 26*387f9dfdSAndroid Build Coastguard Worker #include "file_desc.h" 27*387f9dfdSAndroid Build Coastguard Worker 28*387f9dfdSAndroid Build Coastguard Worker namespace clang { 29*387f9dfdSAndroid Build Coastguard Worker class ASTContext; 30*387f9dfdSAndroid Build Coastguard Worker class QualType; 31*387f9dfdSAndroid Build Coastguard Worker } 32*387f9dfdSAndroid Build Coastguard Worker 33*387f9dfdSAndroid Build Coastguard Worker namespace ebpf { 34*387f9dfdSAndroid Build Coastguard Worker 35*387f9dfdSAndroid Build Coastguard Worker typedef std::function<StatusTuple(const char *, void *)> sscanf_fn; 36*387f9dfdSAndroid Build Coastguard Worker typedef std::function<StatusTuple(char *, size_t, const void *)> snprintf_fn; 37*387f9dfdSAndroid Build Coastguard Worker 38*387f9dfdSAndroid Build Coastguard Worker /// TableDesc uniquely stores all of the runtime state for an active bpf table. 39*387f9dfdSAndroid Build Coastguard Worker /// The copy constructor/assign operator are disabled since the file handles 40*387f9dfdSAndroid Build Coastguard Worker /// owned by this table are not implicitly copyable. One should call the dup() 41*387f9dfdSAndroid Build Coastguard Worker /// method if an explicit new handle is required. We define the move operators 42*387f9dfdSAndroid Build Coastguard Worker /// so that objects of this class can reside in stl containers. 43*387f9dfdSAndroid Build Coastguard Worker class TableDesc { 44*387f9dfdSAndroid Build Coastguard Worker private: TableDesc(const TableDesc & that)45*387f9dfdSAndroid Build Coastguard Worker TableDesc(const TableDesc &that) 46*387f9dfdSAndroid Build Coastguard Worker : name(that.name), 47*387f9dfdSAndroid Build Coastguard Worker fd(that.fd.dup()), 48*387f9dfdSAndroid Build Coastguard Worker fake_fd(that.fake_fd), 49*387f9dfdSAndroid Build Coastguard Worker type(that.type), 50*387f9dfdSAndroid Build Coastguard Worker key_size(that.key_size), 51*387f9dfdSAndroid Build Coastguard Worker leaf_size(that.leaf_size), 52*387f9dfdSAndroid Build Coastguard Worker max_entries(that.max_entries), 53*387f9dfdSAndroid Build Coastguard Worker flags(that.flags), 54*387f9dfdSAndroid Build Coastguard Worker key_desc(that.key_desc), 55*387f9dfdSAndroid Build Coastguard Worker leaf_desc(that.leaf_desc), 56*387f9dfdSAndroid Build Coastguard Worker key_sscanf(that.key_sscanf), 57*387f9dfdSAndroid Build Coastguard Worker leaf_sscanf(that.leaf_sscanf), 58*387f9dfdSAndroid Build Coastguard Worker key_snprintf(that.key_snprintf), 59*387f9dfdSAndroid Build Coastguard Worker leaf_snprintf(that.leaf_snprintf), 60*387f9dfdSAndroid Build Coastguard Worker is_shared(that.is_shared), 61*387f9dfdSAndroid Build Coastguard Worker is_extern(that.is_extern) {} 62*387f9dfdSAndroid Build Coastguard Worker 63*387f9dfdSAndroid Build Coastguard Worker public: TableDesc()64*387f9dfdSAndroid Build Coastguard Worker TableDesc() 65*387f9dfdSAndroid Build Coastguard Worker : fake_fd(0), 66*387f9dfdSAndroid Build Coastguard Worker type(0), 67*387f9dfdSAndroid Build Coastguard Worker key_size(0), 68*387f9dfdSAndroid Build Coastguard Worker leaf_size(0), 69*387f9dfdSAndroid Build Coastguard Worker max_entries(0), 70*387f9dfdSAndroid Build Coastguard Worker flags(0), 71*387f9dfdSAndroid Build Coastguard Worker is_shared(false), 72*387f9dfdSAndroid Build Coastguard Worker is_extern(false) {} TableDesc(const std::string & name,FileDesc && fd,int type,size_t key_size,size_t leaf_size,size_t max_entries,int flags)73*387f9dfdSAndroid Build Coastguard Worker TableDesc(const std::string &name, FileDesc &&fd, int type, size_t key_size, 74*387f9dfdSAndroid Build Coastguard Worker size_t leaf_size, size_t max_entries, int flags) 75*387f9dfdSAndroid Build Coastguard Worker : name(name), 76*387f9dfdSAndroid Build Coastguard Worker fd(std::move(fd)), 77*387f9dfdSAndroid Build Coastguard Worker type(type), 78*387f9dfdSAndroid Build Coastguard Worker key_size(key_size), 79*387f9dfdSAndroid Build Coastguard Worker leaf_size(leaf_size), 80*387f9dfdSAndroid Build Coastguard Worker max_entries(max_entries), 81*387f9dfdSAndroid Build Coastguard Worker flags(flags), 82*387f9dfdSAndroid Build Coastguard Worker is_shared(false), 83*387f9dfdSAndroid Build Coastguard Worker is_extern(false) {} 84*387f9dfdSAndroid Build Coastguard Worker TableDesc(TableDesc &&that) = default; 85*387f9dfdSAndroid Build Coastguard Worker 86*387f9dfdSAndroid Build Coastguard Worker TableDesc &operator=(TableDesc &&that) = default; 87*387f9dfdSAndroid Build Coastguard Worker TableDesc &operator=(const TableDesc &that) = delete; 88*387f9dfdSAndroid Build Coastguard Worker dup()89*387f9dfdSAndroid Build Coastguard Worker TableDesc dup() const { return TableDesc(*this); } 90*387f9dfdSAndroid Build Coastguard Worker 91*387f9dfdSAndroid Build Coastguard Worker std::string name; 92*387f9dfdSAndroid Build Coastguard Worker FileDesc fd; 93*387f9dfdSAndroid Build Coastguard Worker int fake_fd; 94*387f9dfdSAndroid Build Coastguard Worker int type; 95*387f9dfdSAndroid Build Coastguard Worker size_t key_size; // sizes are in bytes 96*387f9dfdSAndroid Build Coastguard Worker size_t leaf_size; 97*387f9dfdSAndroid Build Coastguard Worker size_t max_entries; 98*387f9dfdSAndroid Build Coastguard Worker int flags; 99*387f9dfdSAndroid Build Coastguard Worker std::string key_desc; 100*387f9dfdSAndroid Build Coastguard Worker std::string leaf_desc; 101*387f9dfdSAndroid Build Coastguard Worker sscanf_fn key_sscanf; 102*387f9dfdSAndroid Build Coastguard Worker sscanf_fn leaf_sscanf; 103*387f9dfdSAndroid Build Coastguard Worker snprintf_fn key_snprintf; 104*387f9dfdSAndroid Build Coastguard Worker snprintf_fn leaf_snprintf; 105*387f9dfdSAndroid Build Coastguard Worker bool is_shared; 106*387f9dfdSAndroid Build Coastguard Worker bool is_extern; 107*387f9dfdSAndroid Build Coastguard Worker }; 108*387f9dfdSAndroid Build Coastguard Worker 109*387f9dfdSAndroid Build Coastguard Worker /// MapTypesVisitor gets notified of new bpf tables, and has a chance to parse 110*387f9dfdSAndroid Build Coastguard Worker /// the key and leaf types for their own usage. Subclass this abstract class and 111*387f9dfdSAndroid Build Coastguard Worker /// implement the Visit method, then add an instance of this class to the 112*387f9dfdSAndroid Build Coastguard Worker /// StorageTable instance to be notified of each new key/leaf type. 113*387f9dfdSAndroid Build Coastguard Worker class MapTypesVisitor { 114*387f9dfdSAndroid Build Coastguard Worker public: ~MapTypesVisitor()115*387f9dfdSAndroid Build Coastguard Worker virtual ~MapTypesVisitor() {} 116*387f9dfdSAndroid Build Coastguard Worker virtual void Visit(TableDesc &desc, clang::ASTContext &C, clang::QualType key_type, 117*387f9dfdSAndroid Build Coastguard Worker clang::QualType leaf_type) = 0; 118*387f9dfdSAndroid Build Coastguard Worker }; 119*387f9dfdSAndroid Build Coastguard Worker 120*387f9dfdSAndroid Build Coastguard Worker std::unique_ptr<MapTypesVisitor> createJsonMapTypesVisitor(); 121*387f9dfdSAndroid Build Coastguard Worker 122*387f9dfdSAndroid Build Coastguard Worker } // namespace ebpf 123