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         type(that.type),
49         key_size(that.key_size),
50         leaf_size(that.leaf_size),
51         max_entries(that.max_entries),
52         flags(that.flags),
53         key_desc(that.key_desc),
54         leaf_desc(that.leaf_desc),
55         key_sscanf(that.key_sscanf),
56         leaf_sscanf(that.leaf_sscanf),
57         key_snprintf(that.key_snprintf),
58         leaf_snprintf(that.leaf_snprintf),
59         is_shared(that.is_shared),
60         is_extern(that.is_extern) {}
61 
62  public:
TableDesc()63   TableDesc()
64       : type(0),
65         key_size(0),
66         leaf_size(0),
67         max_entries(0),
68         flags(0),
69         is_shared(false),
70         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)71   TableDesc(const std::string &name, FileDesc &&fd, int type, size_t key_size,
72             size_t leaf_size, size_t max_entries, int flags)
73       : name(name),
74         fd(std::move(fd)),
75         type(type),
76         key_size(key_size),
77         leaf_size(leaf_size),
78         max_entries(max_entries),
79         flags(flags),
80         is_shared(false),
81         is_extern(false) {}
82   TableDesc(TableDesc &&that) = default;
83 
84   TableDesc &operator=(TableDesc &&that) = default;
85   TableDesc &operator=(const TableDesc &that) = delete;
86 
dup()87   TableDesc dup() const { return TableDesc(*this); }
88 
89   std::string name;
90   FileDesc fd;
91   int type;
92   size_t key_size;  // sizes are in bytes
93   size_t leaf_size;
94   size_t max_entries;
95   int flags;
96   std::string key_desc;
97   std::string leaf_desc;
98   sscanf_fn key_sscanf;
99   sscanf_fn leaf_sscanf;
100   snprintf_fn key_snprintf;
101   snprintf_fn leaf_snprintf;
102   bool is_shared;
103   bool is_extern;
104 };
105 
106 /// MapTypesVisitor gets notified of new bpf tables, and has a chance to parse
107 /// the key and leaf types for their own usage. Subclass this abstract class and
108 /// implement the Visit method, then add an instance of this class to the
109 /// StorageTable instance to be notified of each new key/leaf type.
110 class MapTypesVisitor {
111  public:
~MapTypesVisitor()112   virtual ~MapTypesVisitor() {}
113   virtual void Visit(TableDesc &desc, clang::ASTContext &C, clang::QualType key_type,
114                      clang::QualType leaf_type) = 0;
115 };
116 
117 std::unique_ptr<MapTypesVisitor> createJsonMapTypesVisitor();
118 
119 }  // namespace ebpf
120