FD.io VPP  v21.06-3-gbb25fbf28
Vector Packet Processing
gbp_vxlan.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 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_vxlan.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/gbp_vxlan_cmds.hpp"
19 #include "vom/interface.hpp"
21 
22 namespace VOM {
23 
24 const std::string GBP_VXLAN_NAME = "gbp-vxlan";
25 
26 /**
27  * A DB of al the interfaces, key on the name
28  */
30 
31 gbp_vxlan::event_handler gbp_vxlan::m_evh;
32 
34  const gbp_route_domain& grd,
35  const boost::asio::ip::address_v4& src)
36  : interface(mk_name(vni),
37  interface::type_t::UNKNOWN,
39  , m_vni(vni)
40  , m_gbd()
41  , m_grd(grd.singular())
42  , m_src(src)
43 {
44 }
46  const gbp_bridge_domain& gbd,
47  const boost::asio::ip::address_v4& src)
48  : interface(mk_name(vni),
49  interface::type_t::UNKNOWN,
51  , m_vni(vni)
52  , m_gbd(gbd.singular())
53  , m_grd()
54  , m_src(src)
55 {
56 }
57 
59  : interface(vt)
60  , m_vni(vt.m_vni)
61  , m_gbd(vt.m_gbd)
62  , m_grd(vt.m_grd)
63  , m_src(vt.m_src)
64 {
65 }
66 
68 gbp_vxlan::mk_name(uint32_t vni)
69 {
70  std::ostringstream s;
71 
72  s << GBP_VXLAN_NAME << "-" << vni;
73 
74  return (s.str());
75 }
76 
77 const gbp_vxlan::key_t
79 {
80  return (m_vni);
81 }
82 
83 bool
85 {
86  return (m_vni == vt.m_vni && m_src == vt.m_src);
87 }
88 
89 void
90 gbp_vxlan::sweep()
91 {
92  if (rc_t::OK == m_hdl) {
94  }
95  HW::write();
96 }
97 
98 void
99 gbp_vxlan::replay()
100 {
101  if (rc_t::OK == m_hdl) {
102  if (m_grd)
103  HW::enqueue(new gbp_vxlan_cmds::create_cmd(m_hdl, name(), m_src, m_vni,
104  false, m_grd->id()));
105  else if (m_gbd)
106  HW::enqueue(new gbp_vxlan_cmds::create_cmd(m_hdl, name(), m_src, m_vni,
107  true, m_gbd->id()));
108  }
109 }
110 
112 {
113  sweep();
114  m_db.release(key(), this);
115 }
116 
119 {
120  std::ostringstream s;
121  s << "gbp-vxlan:[" << m_vni << "]";
122 
123  return (s.str());
124 }
125 
126 std::shared_ptr<gbp_vxlan>
128 {
129  return (m_db.find(key));
130 }
131 
132 void
133 gbp_vxlan::update(const gbp_vxlan& desired)
134 {
135  /*
136  * the desired state is always that the interface should be created
137  */
138  if (rc_t::OK != m_hdl) {
139  if (m_grd)
140  HW::enqueue(new gbp_vxlan_cmds::create_cmd(m_hdl, name(), m_src, m_vni,
141  false, m_grd->id()));
142  else if (m_gbd)
143  HW::enqueue(new gbp_vxlan_cmds::create_cmd(m_hdl, name(), m_src, m_vni,
144  true, m_gbd->id()));
145  }
146 }
147 
148 std::shared_ptr<gbp_vxlan>
149 gbp_vxlan::find_or_add(const gbp_vxlan& temp)
150 {
151  return (m_db.find_or_add(temp.key(), temp));
152 }
153 
154 std::shared_ptr<gbp_vxlan>
156 {
157  return find_or_add(*this);
158 }
159 
160 std::shared_ptr<interface>
161 gbp_vxlan::singular_i() const
162 {
163  return find_or_add(*this);
164 }
165 
166 void
167 gbp_vxlan::dump(std::ostream& os)
168 {
169  db_dump(m_db, os);
170 }
171 
172 void
173 gbp_vxlan::event_handler::handle_populate(const client_db::key_t& key)
174 {
175  /*
176  * dump VPP Bridge domains
177  */
178  std::shared_ptr<gbp_vxlan_cmds::dump_cmd> cmd =
179  std::make_shared<gbp_vxlan_cmds::dump_cmd>();
180 
181  HW::enqueue(cmd);
182  HW::write();
183 
184  for (auto& record : *cmd) {
185  auto& payload = record.get_payload();
186 
187  boost::asio::ip::address_v4 src = from_api(payload.tunnel.src);
188 
189  if (GBP_VXLAN_TUNNEL_MODE_L3 == payload.tunnel.mode) {
190  auto rd = gbp_route_domain::find(payload.tunnel.bd_rd_id);
191 
192  if (rd) {
193  gbp_vxlan vt(payload.tunnel.vni, *rd, src);
194  OM::commit(key, vt);
195  VOM_LOG(log_level_t::DEBUG) << "dump: " << vt.to_string();
196  }
197  } else {
198  auto bd = gbp_bridge_domain::find(payload.tunnel.bd_rd_id);
199 
200  if (bd) {
201  gbp_vxlan vt(payload.tunnel.vni, *bd, src);
202  OM::commit(key, vt);
203  VOM_LOG(log_level_t::DEBUG) << "dump: " << vt.to_string();
204  }
205  }
206  }
207 }
208 
210 {
211  OM::register_listener(this);
212  inspect::register_handler({ "gvt", "gbp-vxlan-tunnel" }, "GBP VXLAN Tunnels",
213  this);
214 }
215 
216 void
217 gbp_vxlan::event_handler::handle_replay()
218 {
219  m_db.replay();
220 }
221 
223 gbp_vxlan::event_handler::order() const
224 {
225  return (dependency_t::BINDING);
226 }
227 
228 void
229 gbp_vxlan::event_handler::show(std::ostream& os)
230 {
231  db_dump(m_db, os);
232 }
233 }
234 
235 /*
236  * fd.io coding-style-patch-verification: OFF
237  *
238  * Local Variables:
239  * eval: (c-set-style "mozilla")
240  * End:
241  */
VOM::gbp_vxlan::gbp_vxlan
gbp_vxlan(uint32_t vni, const gbp_bridge_domain &gbd, const boost::asio::ip::address_v4 &src)
Construct a new object matching the desried state.
Definition: gbp_vxlan.cpp:45
VOM::HW::write
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:255
VOM::OM::commit
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
VOM_LOG
#define VOM_LOG(lvl)
Definition: logger.hpp:181
VOM
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
VOM::gbp_bridge_domain
A entry in the ARP termination table of a Bridge Domain.
Definition: gbp_bridge_domain.hpp:29
VOM::inspect::register_handler
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
VOM::dependency_t
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
VOM::rc_t::OK
const static rc_t OK
The HW write was successfull.
Definition: types.hpp:109
VOM::gbp_vxlan::dump
static void dump(std::ostream &os)
Dump all L3Configs into the stream provided.
Definition: gbp_vxlan.cpp:167
vni
u32 vni
Definition: flow_types.api:160
VOM::interface::m_hdl
HW::item< handle_t > m_hdl
The SW interface handle VPP has asigned to the interface.
Definition: interface.hpp:540
VOM::gbp_vxlan::operator==
bool operator==(const gbp_vxlan &vt) const
Definition: gbp_vxlan.cpp:84
VOM::gbp_vxlan::key_t
uint32_t key_t
The VNI is the key.
Definition: gbp_vxlan.hpp:36
VOM::gbp_vxlan::find
static std::shared_ptr< gbp_vxlan > find(const key_t k)
Find the GBP_VXLAN tunnel in the OM.
Definition: gbp_vxlan.cpp:127
VOM::gbp_vxlan::key
const key_t key() const
Return the object's key.
Definition: gbp_vxlan.cpp:78
VOM::interface::name
const std::string & name() const
Return the interface type.
Definition: interface.cpp:271
VOM::gbp_vxlan::to_string
virtual std::string to_string() const
Debug rpint function.
Definition: gbp_vxlan.cpp:118
key
typedef key
Definition: ipsec_types.api:88
VOM::GBP_VXLAN_NAME
const std::string GBP_VXLAN_NAME
Definition: gbp_vxlan.cpp:24
gbp_vxlan_cmds.hpp
VOM::db_dump
void db_dump(const DB &db, std::ostream &os)
Print each of the objects in the DB into the stream provided.
Definition: singular_db_funcs.hpp:35
VOM::OM::register_listener
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
VOM::gbp_bridge_domain::find
static std::shared_ptr< gbp_bridge_domain > find(const key_t &k)
Find the instnace of the bridge_domain domain in the OM.
Definition: gbp_bridge_domain.cpp:235
VOM::interface
A representation of an interface in VPP.
Definition: interface.hpp:41
VOM::client_db::key_t
const typedef 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
VOM::HW::enqueue
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:212
src
vl_api_address_t src
Definition: gre.api:54
VOM::gbp_vxlan::~gbp_vxlan
~gbp_vxlan()
Definition: gbp_vxlan.cpp:111
VOM::log_level_t::DEBUG
const static log_level_t DEBUG
Definition: logger.hpp:32
VOM::gbp_vxlan_cmds::create_cmd
A command class that creates an Bridge-Domain.
Definition: gbp_vxlan_cmds.hpp:30
singular_db_funcs.hpp
VOM::interface::type_t
An interface type.
Definition: interface.hpp:67
gbp_vxlan.hpp
VOM::interface::admin_state_t
The admin state of the interface.
Definition: interface.hpp:138
interface.hpp
VOM::gbp_route_domain
A entry in the ARP termination table of a Route Domain.
Definition: gbp_route_domain.hpp:30
VOM::gbp_vxlan_cmds::delete_cmd
A cmd class that Delete an Bridge-Domain.
Definition: gbp_vxlan_cmds.hpp:67
VOM::singular_db
A Database to store the unique 'singular' instances of a single object type.
Definition: singular_db.hpp:33
VOM::dependency_t::BINDING
@ BINDING
Then L2/objects that bind to interfaces, BD, ACLS, etc.
show
void show(char *chroot_path, int verbose)
Definition: svmtool.c:104
event_handler
void event_handler(void *tls_async)
Definition: tls_async.c:334
GBP_VXLAN_TUNNEL_MODE_L3
@ GBP_VXLAN_TUNNEL_MODE_L3
Definition: gbp.api:380
VOM::cmd
A representation of a method call to VPP.
Definition: cmd.hpp:32
VOM::from_api
const neighbour::flags_t from_api(vapi_enum_ip_neighbor_flags f)
Definition: api_types.cpp:36
api_types.hpp
VOM::gbp_vxlan
A representation of a GBP_VXLAN Tunnel in VPP.
Definition: gbp_vxlan.hpp:30
VOM::gbp_vxlan::singular
std::shared_ptr< gbp_vxlan > singular() const
Return the matching 'singular instance'.
Definition: gbp_vxlan.cpp:155
VOM::gbp_route_domain::find
static std::shared_ptr< gbp_route_domain > find(const key_t &k)
Find the instnace of the route_domain domain in the OM.
Definition: gbp_route_domain.cpp:179
string
const char *const string
Definition: cJSON.h:172