bmv2
Designing your own switch target with bmv2
simple_pre.h
Go to the documentation of this file.
1 /* Copyright 2013-present Barefoot Networks, Inc.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /*
17  * Srikrishna Gopu (krishna@barefootnetworks.com)
18  * Antonin Bas (antonin@barefootnetworks.com)
19  *
20  */
21 
23 
24 #ifndef BM_BM_SIM_SIMPLE_PRE_H_
25 #define BM_BM_SIM_SIMPLE_PRE_H_
26 
27 #include <string>
28 #include <unordered_map>
29 #include <vector>
30 
31 #include <boost/thread/shared_mutex.hpp>
32 
33 #include "handle_mgr.h"
34 #include "pre.h"
35 
36 // forward declaration of Json::Value
37 namespace Json {
38 
39 class Value;
40 
41 } // namespace Json
42 
43 namespace bm {
44 
47 class McSimplePre {
48  public:
50  using mgrp_t = unsigned int;
52  using rid_t = unsigned int;
54  using egress_port_t = unsigned int;
55 
56  using mgrp_hdl_t = uintptr_t;
57  using l1_hdl_t = uintptr_t;
58  using l2_hdl_t = uintptr_t;
59 
60  public:
61  enum McReturnCode {
62  SUCCESS = 0,
63  TABLE_FULL,
64  INVALID_MGID,
65  INVALID_MGRP_HANDLE,
66  INVALID_L1_HANDLE,
67  ERROR
68  };
69 
71  struct McIn {
74  };
75 
77  struct McOut {
82  };
83 
84  public:
85  static constexpr size_t PORT_MAP_SIZE = 512;
86  using PortMap = McPre::Set<PORT_MAP_SIZE>;
87 
88  static constexpr size_t LAG_MAP_SIZE = 512;
89  using LagMap = McPre::Set<LAG_MAP_SIZE>;
90 
91  McSimplePre() {}
92  McReturnCode mc_mgrp_create(const mgrp_t, mgrp_hdl_t *);
93  McReturnCode mc_mgrp_destroy(const mgrp_hdl_t);
94  McReturnCode mc_node_create(const rid_t,
95  const PortMap &port_map,
96  l1_hdl_t *l1_hdl);
97  McReturnCode mc_node_associate(const mgrp_hdl_t, const l1_hdl_t);
98  McReturnCode mc_node_dissociate(const mgrp_hdl_t, const l1_hdl_t);
99  McReturnCode mc_node_destroy(const l1_hdl_t);
100  McReturnCode mc_node_update(const l1_hdl_t l1_hdl,
101  const PortMap &port_map);
102 
103  std::string mc_get_entries() const;
104 
105  void reset_state();
106 
129  std::vector<McOut> replicate(const McIn) const;
130 
132  McSimplePre(const McSimplePre &other) = delete;
134  McSimplePre &operator=(const McSimplePre &other) = delete;
135 
137  McSimplePre(McSimplePre &&other) = delete;
139  McSimplePre &operator=(McSimplePre &&other) = delete;
140 
141  protected:
142  static constexpr int MGID_TABLE_SIZE = 4096;
143  static constexpr int L1_MAX_ENTRIES = 4096;
144  static constexpr int L2_MAX_ENTRIES = 8192;
145 
146  struct MgidEntry {
147  mgrp_t mgid{};
148  std::vector<l1_hdl_t> l1_list{};
149 
150  MgidEntry() {}
151  explicit MgidEntry(mgrp_t mgid)
152  : mgid(mgid) {}
153  };
154 
155  struct L1Entry {
156  mgrp_hdl_t mgrp_hdl{};
157  rid_t rid{};
158  l2_hdl_t l2_hdl{};
159  bool is_associated{false};
160 
161  L1Entry() {}
162  explicit L1Entry(rid_t rid)
163  : rid(rid) {}
164  };
165 
166  struct L2Entry {
167  l1_hdl_t l1_hdl{};
168  PortMap port_map{};
169  LagMap lag_map{};
170 
171  L2Entry() {}
172  L2Entry(l1_hdl_t l1_hdl, const PortMap &port_map)
173  : l1_hdl(l1_hdl), port_map(port_map) {}
174 
175  L2Entry(l1_hdl_t l1_hdl, const PortMap &port_map, const LagMap &lag_map)
176  : l1_hdl(l1_hdl), port_map(port_map), lag_map(lag_map) {}
177  };
178 
179  // internal version, which does not acquire the lock
180  void reset_state_();
181 
182  // does not acquire lock
183  void get_entries_common(Json::Value *root) const;
184 
185  // does not acquire lock
186  void node_dissociate(MgidEntry *mgid_entry, l1_hdl_t l1_hdl);
187 
188  std::unordered_map<mgrp_hdl_t, MgidEntry> mgid_entries{};
189  std::unordered_map<l1_hdl_t, L1Entry> l1_entries{};
190  std::unordered_map<l2_hdl_t, L2Entry> l2_entries{};
191  HandleMgr l1_handles{};
192  HandleMgr l2_handles{};
193  mutable boost::shared_mutex mutex{};
194 };
195 
196 } // namespace bm
197 
198 #endif // BM_BM_SIM_SIMPLE_PRE_H_
bm::McSimplePre::rid_t
unsigned int rid_t
NC.
Definition: simple_pre.h:52
bm::McSimplePre::McOut
Output of replicate() method.
Definition: simple_pre.h:77
bm::McSimplePre
Definition: simple_pre.h:47
bm::McSimplePre::McIn
Input to replicate() method.
Definition: simple_pre.h:71
bm::McSimplePre::mgrp_t
unsigned int mgrp_t
NC.
Definition: simple_pre.h:50
bm::McSimplePre::McOut::rid
rid_t rid
replication id of multicast copy
Definition: simple_pre.h:79
bm::McSimplePre::egress_port_t
unsigned int egress_port_t
NC.
Definition: simple_pre.h:54
bm::McSimplePre::McIn::mgid
mgrp_t mgid
Multicast group id to use for replication.
Definition: simple_pre.h:73
bm::McSimplePre::replicate
std::vector< McOut > replicate(const McIn) const
bm::McSimplePre::McOut::egress_port
egress_port_t egress_port
egress port of multicast copy
Definition: simple_pre.h:81
bm::McSimplePre::operator=
McSimplePre & operator=(const McSimplePre &other)=delete
Deleted move assignment operator.