1 /* 2 * Copyright (c) 2017 VMware, 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 <cstddef> 20 #include <iterator> 21 #include <map> 22 #include <memory> 23 #include <string> 24 #include <vector> 25 26 #include "table_desc.h" 27 28 namespace ebpf { 29 30 typedef std::map<int, std::tuple<int, std::string, int, int, int, int, int, std::string, std::string>> 31 fake_fd_map_def; 32 33 class TableStorageImpl; 34 class TableStorageIteratorImpl; 35 36 class Path { 37 public: 38 static const std::string DELIM; 39 Path() = default; 40 Path(const Path &other) = default; 41 Path &operator=(const Path &other) = default; Path(std::initializer_list<std::string> parts)42 Path(std::initializer_list<std::string> parts) { 43 size_t len = parts.size() * DELIM.size(); 44 for (const auto &s : parts) 45 len += s.size(); 46 path_.reserve(len); 47 for (const auto &s : parts) 48 path_ += DELIM + s; 49 } to_string()50 const std::string &to_string() const { return path_; } 51 52 private: 53 std::string path_; 54 }; 55 56 class TableStorage { 57 public: 58 /// iterator is an abstract class for traversing the map entries in a table 59 /// storage object. 60 class iterator { 61 private: 62 friend class TableStorage; 63 iterator(const iterator &); 64 65 public: 66 typedef std::pair<const std::string, TableDesc> value_type; 67 typedef std::ptrdiff_t difference_type; 68 typedef value_type *pointer; 69 typedef value_type &reference; 70 typedef std::forward_iterator_tag iterator_category; 71 typedef iterator self_type; 72 73 iterator(); 74 iterator(std::unique_ptr<TableStorageIteratorImpl>); 75 ~iterator(); 76 iterator(iterator &&); 77 iterator &operator=(iterator &&); 78 self_type &operator++(); 79 self_type operator++(int); 80 bool operator==(const self_type &) const; 81 bool operator!=(const self_type &) const; 82 value_type &operator*() const; 83 pointer operator->() const; 84 85 private: 86 std::unique_ptr<TableStorageIteratorImpl> impl_; 87 }; 88 89 TableStorage(); 90 ~TableStorage(); 91 void Init(std::unique_ptr<TableStorageImpl>); 92 93 bool Find(const Path &path, TableStorage::iterator &result) const; 94 bool Insert(const Path &path, TableDesc &&desc); 95 bool Delete(const Path &path); 96 size_t DeletePrefix(const Path &path); 97 98 void AddMapTypesVisitor(std::unique_ptr<MapTypesVisitor>); 99 void VisitMapType(TableDesc &desc, clang::ASTContext &C, clang::QualType key_type, 100 clang::QualType leaf_type); 101 iterator begin(); 102 iterator end(); 103 iterator lower_bound(const Path &p); 104 iterator upper_bound(const Path &p); 105 106 private: 107 std::unique_ptr<TableStorageImpl> impl_; 108 std::vector<std::unique_ptr<MapTypesVisitor>> visitors_; 109 }; 110 111 std::unique_ptr<TableStorage> createSharedTableStorage(); 112 std::unique_ptr<TableStorage> createBpfFsTableStorage(); 113 } 114