FD.io VPP  v18.10-34-gcce845e
Vector Packet Processing
gbp_subnet.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018 Cisco and/or its affiliates.
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 #include "vom/gbp_subnet.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/gbp_subnet_cmds.hpp"
20 
21 namespace VOM {
22 
23 gbp_subnet::type_t::type_t(int v, const std::string s)
24  : enum_base<gbp_subnet::type_t>(v, s)
25 {
26 }
27 
28 const gbp_subnet::type_t gbp_subnet::type_t::INTERNAL(0, "internal");
29 const gbp_subnet::type_t gbp_subnet::type_t::EXTERNAL(1, "external");
30 
31 singular_db<gbp_subnet::key_t, gbp_subnet> gbp_subnet::m_db;
32 
33 gbp_subnet::event_handler gbp_subnet::m_evh;
34 
36  : m_hw(false)
37  , m_rd(rd.singular())
38  , m_prefix(prefix)
39  , m_type(type_t::INTERNAL)
40  , m_recirc(nullptr)
41  , m_epg(nullptr)
42 {
43 }
44 
46  const route::prefix_t& prefix,
47  const gbp_recirc& recirc,
48  const gbp_endpoint_group& epg)
49  : m_hw(false)
50  , m_rd(rd.singular())
51  , m_prefix(prefix)
52  , m_type(type_t::EXTERNAL)
53  , m_recirc(recirc.singular())
54  , m_epg(epg.singular())
55 {
56 }
57 
59  : m_hw(o.m_hw)
60  , m_rd(o.m_rd)
61  , m_prefix(o.m_prefix)
62  , m_type(o.m_type)
63  , m_recirc(o.m_recirc)
64  , m_epg(o.m_epg)
65 {
66 }
67 
69 {
70  sweep();
71  m_db.release(key(), this);
72 }
73 
76 {
77  return (std::make_pair(m_rd->key(), m_prefix));
78 }
79 
80 bool
82 {
83  return ((key() == gs.key()) && (m_type == gs.m_type) &&
84  (m_recirc == gs.m_recirc) && (m_epg == gs.m_epg));
85 }
86 
87 void
88 gbp_subnet::sweep()
89 {
90  if (m_hw) {
92  new gbp_subnet_cmds::delete_cmd(m_hw, m_rd->table_id(), m_prefix));
93  }
94  HW::write();
95 }
96 
97 void
99 {
100  if (m_hw) {
102  m_hw, m_rd->table_id(), m_prefix, (m_type == type_t::INTERNAL),
103  (m_recirc ? m_recirc->handle() : handle_t::INVALID),
104  (m_epg ? m_epg->id() : ~0)));
105  }
106 }
107 
108 std::string
110 {
111  std::ostringstream s;
112  s << "gbp-subnet:[" << m_type.to_string() << ", " << m_rd->to_string() << ":"
113  << m_prefix.to_string();
114  if (m_recirc)
115  s << ", " << m_recirc->to_string();
116  if (m_epg)
117  s << ", " << m_epg->to_string();
118 
119  s << "]";
120 
121  return (s.str());
122 }
123 
124 void
125 gbp_subnet::update(const gbp_subnet& r)
126 {
127  if (rc_t::OK != m_hw.rc()) {
129  m_hw, m_rd->table_id(), m_prefix, (m_type == type_t::INTERNAL),
130  (m_recirc ? m_recirc->handle() : handle_t::INVALID),
131  (m_epg ? m_epg->id() : ~0)));
132  } else {
133  if (m_type != r.m_type) {
134  m_epg = r.m_epg;
135  m_recirc = r.m_recirc;
136  m_type = r.m_type;
137 
139  m_hw, m_rd->table_id(), m_prefix, (m_type == type_t::INTERNAL),
140  (m_recirc ? m_recirc->handle() : handle_t::INVALID),
141  (m_epg ? m_epg->id() : ~0)));
142  }
143  }
144 }
145 
146 std::shared_ptr<gbp_subnet>
147 gbp_subnet::find_or_add(const gbp_subnet& temp)
148 {
149  return (m_db.find_or_add(temp.key(), temp));
150 }
151 
152 std::shared_ptr<gbp_subnet>
154 {
155  return (m_db.find(k));
156 }
157 
158 std::shared_ptr<gbp_subnet>
160 {
161  return find_or_add(*this);
162 }
163 
164 void
165 gbp_subnet::dump(std::ostream& os)
166 {
167  db_dump(m_db, os);
168 }
169 
171 {
172  OM::register_listener(this);
173  inspect::register_handler({ "gbp-subnet" }, "GBP Subnets", this);
174 }
175 
176 void
177 gbp_subnet::event_handler::handle_replay()
178 {
179  m_db.replay();
180 }
181 
182 void
183 gbp_subnet::event_handler::handle_populate(const client_db::key_t& key)
184 {
185  std::shared_ptr<gbp_subnet_cmds::dump_cmd> cmd =
186  std::make_shared<gbp_subnet_cmds::dump_cmd>();
187 
188  HW::enqueue(cmd);
189  HW::write();
190 
191  for (auto& record : *cmd) {
192  auto& payload = record.get_payload();
193 
194  route::prefix_t pfx = from_api(payload.subnet.prefix);
195  std::shared_ptr<route_domain> rd =
196  route_domain::find(payload.subnet.table_id);
197 
198  if (rd) {
199  if (payload.subnet.is_internal) {
200  gbp_subnet gs(*rd, pfx);
201  OM::commit(key, gs);
202  VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
203  } else {
204  std::shared_ptr<interface> itf =
205  interface::find(payload.subnet.sw_if_index);
206  std::shared_ptr<gbp_endpoint_group> epg =
207  gbp_endpoint_group::find(payload.subnet.epg_id);
208 
209  if (itf && epg) {
210  std::shared_ptr<gbp_recirc> recirc = gbp_recirc::find(itf->key());
211 
212  if (recirc) {
213  gbp_subnet gs(*rd, pfx, *recirc, *epg);
214  OM::commit(key, gs);
215  VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
216  }
217  }
218  }
219  }
220  }
221 }
222 
224 gbp_subnet::event_handler::order() const
225 {
226  return (dependency_t::ENTRY);
227 }
228 
229 void
230 gbp_subnet::event_handler::show(std::ostream& os)
231 {
232  db_dump(m_db, os);
233 }
234 } // namespace VOM
235 
236 /*
237  * fd.io coding-style-patch-verification: ON
238  *
239  * Local Variables:
240  * eval: (c-set-style "mozilla")
241  * End:
242  */
static std::shared_ptr< route_domain > find(const key_t &temp)
Find the instnace of the route domain in the OM.
#define VOM_LOG(lvl)
Definition: logger.hpp:181
void db_dump(const DB &db, std::ostream &os)
Print each of the objects in the DB into the stream provided.
const std::string key_t
In the opflex world each entity is known by a URI which can be converted into a string.
Definition: client_db.hpp:51
static std::shared_ptr< interface > find(const handle_t &h)
The the singular instance of the interface in the DB by handle.
Definition: interface.cpp:461
std::shared_ptr< gbp_subnet > singular() const
Return the matching &#39;singular instance&#39;.
Definition: gbp_subnet.cpp:159
static void register_handler(const std::vector< std::string > &cmds, const std::string &help, command_handler *ch)
Register a command handler for inspection.
Definition: inspect.cpp:85
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:236
static const log_level_t DEBUG
Definition: logger.hpp:32
ip_address_t from_api(const vapi_type_address &v)
Definition: api_types.cpp:65
static const handle_t INVALID
A value of an interface handle_t that means the itf does not exist.
Definition: types.hpp:263
A route-domain is a VRF.
std::string to_string() const
Convert to string for debugging.
Definition: gbp_subnet.cpp:109
rc_t rc() const
Get the HW return code.
Definition: hw.hpp:118
void replay(void)
replay the object to create it in hardware
Definition: gbp_subnet.cpp:98
~gbp_subnet()
Destructor.
Definition: gbp_subnet.cpp:68
std::string to_string() const
convert to string format for debug purposes
Definition: prefix.cpp:183
#define v
Definition: acl.c:496
static void dump(std::ostream &os)
Dump all bridge_domain-doamin into the stream provided.
Definition: gbp_subnet.cpp:165
static std::shared_ptr< gbp_subnet > find(const key_t &k)
Find the instnace of the bridge_domain domain in the OM.
Definition: gbp_subnet.cpp:153
A recirculation interface for GBP use pre/post NAT.
Definition: gbp_recirc.hpp:27
A GBP Enpoint (i.e.
Definition: gbp_subnet.hpp:28
bool operator==(const gbp_subnet &bdae) const
comparison operator
Definition: gbp_subnet.cpp:81
gbp_subnet(const route_domain &rd, const route::prefix_t &prefix)
Construct an internal GBP subnet.
Definition: gbp_subnet.cpp:35
const key_t key() const
Return the object&#39;s key.
Definition: gbp_subnet.cpp:75
A cmd class that deletes a GBP subnet.
A entry in the ARP termination table of a Bridge Domain.
static rc_t commit(const client_db::key_t &key, const OBJ &obj)
Make the State in VPP reflect the expressed desired state.
Definition: om.hpp:202
void event_handler(void *tls_async)
Definition: tls_async.c:339
dependency_t
There needs to be a strict order in which object types are read from VPP (at boot time) and replayed ...
Definition: types.hpp:43
static const rc_t OK
The HW write was successfull.
Definition: types.hpp:104
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:194
A command class that creates or updates the GBP subnet.
static std::shared_ptr< gbp_recirc > find(const key_t &k)
Find the instnace of the recirc interface in the OM.
Definition: gbp_recirc.cpp:121
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
A representation of a method call to VPP.
Definition: cmd.hpp:32
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
typedef prefix
Definition: ip_types.api:40
std::pair< route_domain::key_t, route::prefix_t > key_t
The key for a GBP subnet; table and prefix.
Definition: gbp_subnet.hpp:34
Entries in Tables.
static std::shared_ptr< gbp_endpoint_group > find(const key_t &k)
Find the instnace of the bridge_domain domain in the OM.
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
A prefix defintion.
Definition: prefix.hpp:92