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