FD.io VPP  v19.01.2-3-gf61a1a8
Vector Packet Processing
vxlan_tunnel.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/vxlan_tunnel.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/logger.hpp"
22 
23 namespace VOM {
24 const std::string VXLAN_TUNNEL_NAME = "vxlan-tunnel-itf";
25 
26 vxlan_tunnel::event_handler vxlan_tunnel::m_evh;
27 
31 
32 vxlan_tunnel::mode_t::mode_t(int v, const std::string s)
33  : enum_base<vxlan_tunnel::mode_t>(v, s)
34 {
35 }
36 
39  uint32_t vni)
40  : src(src)
41  , dst(dst)
42  , vni(vni)
43 {
44 }
45 
47  : src()
48  , dst()
49  , vni(0)
50 {
51 }
52 
53 bool
55 {
56  return ((src == other.src) && (dst == other.dst) && (vni == other.vni));
57 }
58 
59 std::string
61 {
62  std::ostringstream s;
63 
64  s << "ep:["
65  << "src:" << src.to_string() << " dst:" << dst.to_string() << " vni:" << vni
66  << "]";
67 
68  return (s.str());
69 }
70 
71 std::string
72 vxlan_tunnel::mk_name(const boost::asio::ip::address& src,
74  const mode_t& mode,
75  uint32_t vni)
76 {
77  std::ostringstream s;
78 
79  s << VXLAN_TUNNEL_NAME << "-" << mode.to_string() << "-" << src << "-" << dst
80  << ":" << vni;
81 
82  return (s.str());
83 }
84 
86  const boost::asio::ip::address& dst,
87  uint32_t vni,
88  const mode_t& mode)
89  : interface(mk_name(src, dst, mode, vni),
90  interface::type_t::VXLAN,
92  , m_tep(src, dst, vni)
93  , m_mode(mode)
94  , m_mcast_itf()
95 {
96 }
97 
99  const boost::asio::ip::address& dst,
100  uint32_t vni,
101  const interface& mcast_itf,
102  const mode_t& mode)
103  : interface(mk_name(src, dst, mode, vni),
104  interface::type_t::VXLAN,
106  , m_tep(src, dst, vni)
107  , m_mode(mode)
108  , m_mcast_itf(mcast_itf.singular())
109 {
110 }
111 
113  : interface(o)
114  , m_tep(o.m_tep)
115  , m_mode(o.m_mode)
116 {
117 }
118 
119 const handle_t&
121 {
122  return (m_hdl.data());
123 }
124 
125 std::shared_ptr<vxlan_tunnel>
127 {
128  return std::dynamic_pointer_cast<vxlan_tunnel>(m_db.find(k));
129 }
130 
131 void
132 vxlan_tunnel::sweep()
133 {
134  if (m_hdl) {
135  if (mode_t::STANDARD == m_mode)
137  else if (mode_t::GBP == m_mode)
139  }
140  HW::write();
141 }
142 
143 void
144 vxlan_tunnel::replay()
145 {
146  if (m_hdl) {
147  if (mode_t::STANDARD == m_mode)
149  m_hdl, name(), m_tep,
150  (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
151  else if (mode_t::GBP == m_mode)
153  m_hdl, name(), m_tep,
154  (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
155  }
156 }
157 
159 {
160  sweep();
161  release();
162 }
163 
164 std::string
166 {
167  std::ostringstream s;
168  s << "vxlan-tunnel: " << m_hdl.to_string() << " " << m_mode.to_string() << " "
169  << m_tep.to_string();
170 
171  return (s.str());
172 }
173 
174 void
175 vxlan_tunnel::update(const vxlan_tunnel& desired)
176 {
177  /*
178  * the desired state is always that the interface should be created
179  */
180  if (!m_hdl) {
181  if (mode_t::STANDARD == m_mode)
183  m_hdl, name(), m_tep,
184  (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
185  else if (mode_t::GBP == m_mode)
187  m_hdl, name(), m_tep,
188  (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
189  }
190 }
191 
192 std::shared_ptr<vxlan_tunnel>
194 {
195  return std::dynamic_pointer_cast<vxlan_tunnel>(singular_i());
196 }
197 
198 std::shared_ptr<interface>
199 vxlan_tunnel::singular_i() const
200 {
201  return m_db.find_or_add(key(), *this);
202 }
203 
204 void
205 vxlan_tunnel::event_handler::handle_populate(const client_db::key_t& key)
206 {
207  /*
208  * dump VPP current states
209  */
210  {
211  std::shared_ptr<vxlan_tunnel_cmds::dump_cmd> cmd =
212  std::make_shared<vxlan_tunnel_cmds::dump_cmd>();
213 
214  HW::enqueue(cmd);
215  HW::write();
216 
217  for (auto& record : *cmd) {
218  auto& payload = record.get_payload();
219  handle_t hdl(payload.sw_if_index);
221  from_bytes(payload.is_ipv6, payload.src_address);
223  from_bytes(payload.is_ipv6, payload.dst_address);
224 
225  std::shared_ptr<vxlan_tunnel> vt =
226  vxlan_tunnel(src, dst, payload.vni).singular();
227  vt->set(hdl);
228 
229  VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
230 
231  OM::commit(key, *vt);
232  }
233  }
234  {
235  std::shared_ptr<vxlan_gbp_tunnel_cmds::dump_cmd> cmd =
236  std::make_shared<vxlan_gbp_tunnel_cmds::dump_cmd>();
237 
238  HW::enqueue(cmd);
239  HW::write();
240 
241  for (auto& record : *cmd) {
242  auto& payload = record.get_payload();
243  handle_t hdl(payload.tunnel.sw_if_index);
244  boost::asio::ip::address src = from_api(payload.tunnel.src);
245  boost::asio::ip::address dst = from_api(payload.tunnel.dst);
246 
247  std::shared_ptr<vxlan_tunnel> vt =
248  vxlan_tunnel(src, dst, payload.tunnel.vni, mode_t::GBP).singular();
249  vt->set(hdl);
250 
251  VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
252 
253  OM::commit(key, *vt);
254  }
255  }
256 }
257 
259 {
260  OM::register_listener(this);
261  inspect::register_handler({ "vxlan" }, "VXLAN Tunnels", this);
262 }
263 
264 void
265 vxlan_tunnel::event_handler::handle_replay()
266 {
267  // replay is handled from the interface DB
268 }
269 
271 vxlan_tunnel::event_handler::order() const
272 {
274 }
275 
276 void
277 vxlan_tunnel::event_handler::show(std::ostream& os)
278 {
279  // dumped by the interface handler
280 }
281 
282 }; // namespace VOM
283 
284 /*
285  * fd.io coding-style-patch-verification: ON
286  *
287  * Local Variables:
288  * eval: (c-set-style "mozilla")
289  * End:
290  */
A Command class that creates an VXLAN tunnel.
std::string to_string() const
Debug print function.
A representation of a VXLAN Tunnel in VPP.
void release()
release/remove an interface form the singular store
Definition: interface.cpp:233
typedef address
Definition: ip_types.api:30
vl_api_address_t src
Definition: vxlan_gbp.api:32
const std::string VXLAN_TUNNEL_NAME
static const mode_t GPE
#define VOM_LOG(lvl)
Definition: logger.hpp:181
static std::shared_ptr< vxlan_tunnel > find(const interface::key_t &k)
Fond the singular instance of the interface in the DB by key.
A template base class for all enum types.
Definition: enum_base.hpp:30
virtual std::string to_string() const
Debug rpint function.
Combaintion of attributes that are a unique key for a VXLAN tunnel.
mode for the tunnel
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
virtual interfaces - those that depend on some real interface
static const mode_t GBP
HW::item< handle_t > m_hdl
The SW interface handle VPP has asigned to the interface.
Definition: interface.hpp:532
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
boost::asio::ip::address dst
The destination IP address of the endpoint.
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:255
static const log_level_t DEBUG
Definition: logger.hpp:32
ip_address_t from_api(const vapi_type_address &v)
Definition: api_types.cpp:53
static const handle_t INVALID
A value of an interface handle_t that means the itf does not exist.
Definition: types.hpp:268
A Command class that creates an VXLAN tunnel.
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:161
T & data()
Return the data read/written.
Definition: hw.hpp:109
static singular_db< key_t, interface > m_db
A map of all interfaces key against the interface&#39;s name.
Definition: interface.hpp:564
A functor class that creates an VXLAN tunnel.
std::shared_ptr< vxlan_tunnel > singular() const
Return the matching &#39;singular instance&#39;.
vl_api_address_t dst
Definition: vxlan_gbp.api:33
The admin state of the interface.
Definition: interface.hpp:138
A representation of an interface in VPP.
Definition: interface.hpp:41
A type declaration of an interface handle in VPP.
Definition: types.hpp:233
endpoint_t()
Default constructor.
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:340
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
const std::string & name() const
Return the interface type.
Definition: interface.cpp:264
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:212
boost::asio::ip::address from_bytes(uint8_t is_ip6, const uint8_t *bytes)
Convert a VPP byte stinrg into a boost addresss.
Definition: prefix.cpp:193
An interface type.
Definition: interface.hpp:67
static const mode_t STANDARD
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
A representation of a method call to VPP.
Definition: cmd.hpp:32
A functor class that creates an VXLAN tunnel.
u32 vni
Definition: vxlan_gbp.api:36
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
bool operator==(const endpoint_t &o) const
Comparison operator.
uint32_t vni
The VNI of the endpoint.
vl_api_gbp_vxlan_tunnel_mode_t mode
Definition: gbp.api:349
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
std::string key_t
The key for interface&#39;s key.
Definition: interface.hpp:56
vxlan_tunnel(const boost::asio::ip::address &src, const boost::asio::ip::address &dst, uint32_t vni, const mode_t &mode=mode_t::STANDARD)
Construct a new object matching the desried state.
boost::asio::ip::address src
The src IP address of the endpoint.
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
const key_t & key() const
Return the interface type.
Definition: interface.cpp:270
const handle_t & handle() const
Return VPP&#39;s handle to this object.