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