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