FD.io VPP  v18.07.1-19-g511ce25
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/logger.hpp"
20 
21 namespace VOM {
22 const std::string VXLAN_TUNNEL_NAME = "vxlan-tunnel-itf";
23 
24 vxlan_tunnel::event_handler vxlan_tunnel::m_evh;
25 
26 /**
27  * A DB of all vxlan_tunnels
28  * this does not register as a listener for replay events, since the tunnels
29  * are also in the base-class interface DB and so will be poked from there.
30  */
32 
34  const boost::asio::ip::address& dst,
35  uint32_t vni)
36  : src(src)
37  , dst(dst)
38  , vni(vni)
39 {
40 }
41 
43  : src()
44  , dst()
45  , vni(0)
46 {
47 }
48 
49 bool
51 {
52  return ((src == other.src) && (dst == other.dst) && (vni == other.vni));
53 }
54 
55 bool
57 {
58  if (src < o.src)
59  return true;
60  if (dst < o.dst)
61  return true;
62  if (vni < o.vni)
63  return true;
64 
65  return false;
66 }
67 
68 std::string
70 {
71  std::ostringstream s;
72 
73  s << "ep:["
74  << "src:" << src.to_string() << " dst:" << dst.to_string() << " vni:" << vni
75  << "]";
76 
77  return (s.str());
78 }
79 
80 std::ostream&
81 operator<<(std::ostream& os, const vxlan_tunnel::endpoint_t& ep)
82 {
83  os << ep.to_string();
84 
85  return (os);
86 }
87 
88 std::string
89 vxlan_tunnel::mk_name(const boost::asio::ip::address& src,
91  uint32_t vni)
92 {
93  std::ostringstream s;
94 
95  s << VXLAN_TUNNEL_NAME << "-" << src << "-" << dst << ":" << vni;
96 
97  return (s.str());
98 }
99 
101  const boost::asio::ip::address& dst,
102  uint32_t vni)
103  : interface(mk_name(src, dst, vni),
104  interface::type_t::VXLAN,
106  , m_tep(src, dst, vni)
107 {
108 }
109 
111  : interface(o)
112  , m_tep(o.m_tep)
113 {
114 }
115 
116 const handle_t&
118 {
119  return (m_hdl.data());
120 }
121 
122 void
123 vxlan_tunnel::sweep()
124 {
125  if (m_hdl) {
127  }
128  HW::write();
129 }
130 
131 void
132 vxlan_tunnel::replay()
133 {
134  if (m_hdl) {
136  }
137 }
138 
140 {
141  sweep();
142 
143  /*
144  * release from both DBs
145  */
146  release();
147  m_db.release(m_tep, this);
148 }
149 
150 std::string
152 {
153  std::ostringstream s;
154  s << "vxlan-tunnel: " << m_hdl.to_string() << " " << m_tep.to_string();
155 
156  return (s.str());
157 }
158 
159 void
160 vxlan_tunnel::update(const vxlan_tunnel& desired)
161 {
162  /*
163  * the desired state is always that the interface should be created
164  */
165  if (!m_hdl) {
167  }
168 }
169 
170 std::shared_ptr<vxlan_tunnel>
171 vxlan_tunnel::find_or_add(const vxlan_tunnel& temp)
172 {
173  /*
174  * a VXLAN tunnel needs to be in both the interface-find-by-name
175  * and the vxlan_tunnel-find-by-endpoint singular databases
176  */
177  std::shared_ptr<vxlan_tunnel> sp;
178 
179  sp = m_db.find_or_add(temp.m_tep, temp);
180 
181  interface::m_db.add(temp.name(), sp);
182 
183  return (sp);
184 }
185 
186 std::shared_ptr<vxlan_tunnel>
188 {
189  return (find_or_add(*this));
190 }
191 
192 std::shared_ptr<interface>
193 vxlan_tunnel::singular_i() const
194 {
195  return find_or_add(*this);
196 }
197 
198 void
199 vxlan_tunnel::dump(std::ostream& os)
200 {
201  db_dump(m_db, os);
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  std::shared_ptr<vxlan_tunnel_cmds::dump_cmd> cmd =
211  std::make_shared<vxlan_tunnel_cmds::dump_cmd>();
212 
213  HW::enqueue(cmd);
214  HW::write();
215 
216  for (auto& record : *cmd) {
217  auto& payload = record.get_payload();
218  handle_t hdl(payload.sw_if_index);
220  from_bytes(payload.is_ipv6, payload.src_address);
222  from_bytes(payload.is_ipv6, payload.dst_address);
223 
224  std::shared_ptr<vxlan_tunnel> vt =
225  vxlan_tunnel(src, dst, payload.vni).singular();
226  vt->set(hdl);
227 
228  VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
229 
230  OM::commit(key, *vt);
231  }
232 }
233 
235 {
236  OM::register_listener(this);
237  inspect::register_handler({ "vxlan" }, "VXLAN Tunnels", this);
238 }
239 
240 void
241 vxlan_tunnel::event_handler::handle_replay()
242 {
243  // replay is handled from the interface DB
244 }
245 
247 vxlan_tunnel::event_handler::order() const
248 {
250 }
251 
252 void
253 vxlan_tunnel::event_handler::show(std::ostream& os)
254 {
255  // dumped by the interface handler
256 }
257 
258 }; // namespace VOM
259 
260 /*
261  * fd.io coding-style-patch-verification: ON
262  *
263  * Local Variables:
264  * eval: (c-set-style "mozilla")
265  * End:
266  */
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:234
typedef address
Definition: ip_types.api:35
const std::string VXLAN_TUNNEL_NAME
#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.
virtual std::string to_string() const
Debug rpint function.
Combaintion of attributes that are a unique key for a VXLAN 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
HW::item< handle_t > m_hdl
The SW interface handle VPP has asigned to the interface.
Definition: interface.hpp:487
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 void dump(std::ostream &os)
Dump all L3Configs into the stream provided.
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:236
static const log_level_t DEBUG
Definition: logger.hpp:32
A Command class that creates an VXLAN tunnel.
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:160
T & data()
Return the data read/written.
Definition: hw.hpp:108
static singular_db< key_t, interface > m_db
A map of all interfaces key against the interface&#39;s name.
Definition: interface.hpp:519
A functor class that creates an VXLAN tunnel.
std::shared_ptr< vxlan_tunnel > singular() const
Return the matching &#39;singular instance&#39;.
A Database to store the unique &#39;singular&#39; instances of a single object type.
Definition: singular_db.hpp:33
The admin state of the interface.
Definition: interface.hpp:142
A representation of an interface in VPP.
Definition: interface.hpp:41
A type declaration of an interface handle in VPP.
Definition: types.hpp:228
boost::asio::ip::address from_bytes(uint8_t is_ip6, uint8_t *bytes)
Convert a VPP byte stinrg into a boost addresss.
Definition: prefix.cpp:193
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:311
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:265
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:194
An interface type.
Definition: interface.hpp:67
std::ostream & operator<<(std::ostream &os, const std::pair< direction_t, interface::key_t > &key)
vxlan_tunnel(const boost::asio::ip::address &src, const boost::asio::ip::address &dst, uint32_t vni)
Construct a new object matching the desried state.
bool operator<(const endpoint_t &o) const
less-than operator for map storage
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
bool operator==(const endpoint_t &o) const
Comparison operator.
uint32_t vni
The VNI of the endpoint.
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:271
const handle_t & handle() const
Return VPP&#39;s handle to this object.