/* * Copyright (c) 2016 PLUMgrid, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include "common.h" #include "compat/linux/bpf.h" #include "table_storage.h" #include "table_storage_impl.h" namespace ebpf { using std::string; using std::unique_ptr; /// A process-wide singleton of shared tables class SharedTableStorage : public TableStorageImpl { public: class iterator : public TableStorageIteratorImpl { std::map::iterator it_; public: explicit iterator(const std::map::iterator &it) : it_(it) {} virtual ~iterator() {} virtual unique_ptr clone() const override { return make_unique(it_); } virtual self_type &operator++() override { ++it_; return *this; } virtual value_type &operator*() const override { return *it_; } virtual pointer operator->() const override { return &*it_; } }; virtual ~SharedTableStorage() {} virtual bool Find(const string &name, TableStorage::iterator &result) const override; virtual bool Insert(const string &name, TableDesc &&desc) override; virtual bool Delete(const string &name) override; virtual unique_ptr begin() override; virtual unique_ptr end() override; virtual unique_ptr lower_bound(const string &k) override; virtual unique_ptr upper_bound(const string &k) override; virtual unique_ptr erase(const TableStorageIteratorImpl &it) override; private: static std::map tables_; }; bool SharedTableStorage::Find(const string &name, TableStorage::iterator &result) const { auto it = tables_.find(name); if (it == tables_.end()) return false; result = TableStorage::iterator(make_unique(it)); return true; } bool SharedTableStorage::Insert(const string &name, TableDesc &&desc) { auto it = tables_.find(name); if (it != tables_.end()) return false; tables_[name] = std::move(desc); return true; } bool SharedTableStorage::Delete(const string &name) { auto it = tables_.find(name); if (it == tables_.end()) return false; tables_.erase(it); return true; } unique_ptr SharedTableStorage::begin() { return make_unique(tables_.begin()); } unique_ptr SharedTableStorage::end() { return make_unique(tables_.end()); } unique_ptr SharedTableStorage::lower_bound(const string &k) { return make_unique(tables_.lower_bound(k)); } unique_ptr SharedTableStorage::upper_bound(const string &k) { return make_unique(tables_.upper_bound(k)); } unique_ptr SharedTableStorage::erase(const TableStorageIteratorImpl &it) { auto i = tables_.find((*it).first); if (i == tables_.end()) return unique_ptr(); return make_unique(tables_.erase(i)); } // All maps for this process are kept in global static storage. std::map SharedTableStorage::tables_; unique_ptr createSharedTableStorage() { auto t = make_unique(); t->Init(make_unique()); t->AddMapTypesVisitor(createJsonMapTypesVisitor()); return t; } }