vxlan: vxlan/vxlan.api API cleanup
[vpp.git] / extras / vom / vom / vxlan_tunnel.cpp
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/interface_cmds.hpp"
19 #include "vom/logger.hpp"
20 #include "vom/singular_db_funcs.hpp"
21 #include "vom/vxlan_gbp_tunnel_cmds.hpp"
22 #include "vom/vxlan_tunnel_cmds.hpp"
23
24 namespace VOM {
25 const std::string VXLAN_TUNNEL_NAME = "vxlan-tunnel-itf";
26
27 vxlan_tunnel::event_handler vxlan_tunnel::m_evh;
28
29 const vxlan_tunnel::mode_t vxlan_tunnel::mode_t::STANDARD(0, "standard");
30 const vxlan_tunnel::mode_t vxlan_tunnel::mode_t::GBP_L2(1, "GBP-L2");
31 const vxlan_tunnel::mode_t vxlan_tunnel::mode_t::GBP_L3(2, "GBP-L3");
32 const vxlan_tunnel::mode_t vxlan_tunnel::mode_t::GPE(3, "GPE");
33
34 vxlan_tunnel::mode_t::mode_t(int v, const std::string s)
35   : enum_base<vxlan_tunnel::mode_t>(v, s)
36 {}
37
38 vxlan_tunnel::endpoint_t::endpoint_t(const boost::asio::ip::address& src,
39                                      const boost::asio::ip::address& dst,
40                                      uint32_t vni)
41   : src(src)
42   , dst(dst)
43   , vni(vni)
44 {}
45
46 vxlan_tunnel::endpoint_t::endpoint_t()
47   : src()
48   , dst()
49   , vni(0)
50 {}
51
52 bool
53 vxlan_tunnel::endpoint_t::operator==(const endpoint_t& other) const
54 {
55   return ((src == other.src) && (dst == other.dst) && (vni == other.vni));
56 }
57
58 std::string
59 vxlan_tunnel::endpoint_t::to_string() const
60 {
61   std::ostringstream s;
62
63   s << "ep:["
64     << "src:" << src.to_string() << " dst:" << dst.to_string() << " vni:" << vni
65     << "]";
66
67   return (s.str());
68 }
69
70 std::string
71 vxlan_tunnel::mk_name(const boost::asio::ip::address& src,
72                       const boost::asio::ip::address& dst,
73                       const mode_t& mode,
74                       uint32_t vni)
75 {
76   std::ostringstream s;
77
78   s << VXLAN_TUNNEL_NAME << "-" << mode.to_string() << "-" << src << "-" << dst
79     << ":" << vni;
80
81   return (s.str());
82 }
83
84 vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
85                            const boost::asio::ip::address& dst,
86                            uint32_t vni,
87                            const mode_t& mode)
88   : interface(mk_name(src, dst, mode, vni),
89               interface::type_t::VXLAN,
90               interface::admin_state_t::UP)
91   , m_tep(src, dst, vni)
92   , m_mode(mode)
93   , m_mcast_itf()
94   , m_rd()
95   , m_table_id(route::DEFAULT_TABLE)
96 {}
97
98 vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
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,
105               interface::admin_state_t::UP)
106   , m_tep(src, dst, vni)
107   , m_mode(mode)
108   , m_mcast_itf(mcast_itf.singular())
109   , m_rd()
110   , m_table_id(route::DEFAULT_TABLE)
111 {}
112
113 vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
114                            const boost::asio::ip::address& dst,
115                            uint32_t vni,
116                            const route_domain& rd,
117                            const mode_t& mode)
118   : interface(mk_name(src, dst, mode, vni),
119               interface::type_t::VXLAN,
120               interface::admin_state_t::UP)
121   , m_tep(src, dst, vni)
122   , m_mode(mode)
123   , m_mcast_itf()
124   , m_rd(rd.singular())
125   , m_table_id(m_rd->table_id())
126 {}
127
128 vxlan_tunnel::vxlan_tunnel(const vxlan_tunnel& o)
129   : interface(o)
130   , m_tep(o.m_tep)
131   , m_mode(o.m_mode)
132   , m_mcast_itf(o.m_mcast_itf)
133   , m_rd(o.m_rd)
134   , m_table_id(o.m_table_id)
135 {}
136
137 bool
138 vxlan_tunnel::operator==(const vxlan_tunnel& other) const
139 {
140   return ((m_tep == other.m_tep) && (m_mode == other.m_mode) &&
141           (m_mcast_itf == other.m_mcast_itf));
142 }
143
144 const handle_t&
145 vxlan_tunnel::handle() const
146 {
147   return (m_hdl.data());
148 }
149
150 std::shared_ptr<vxlan_tunnel>
151 vxlan_tunnel::find(const interface::key_t& k)
152 {
153   return std::dynamic_pointer_cast<vxlan_tunnel>(m_db.find(k));
154 }
155
156 void
157 vxlan_tunnel::sweep()
158 {
159   if (m_hdl) {
160     if (mode_t::STANDARD == m_mode)
161       HW::enqueue(new vxlan_tunnel_cmds::delete_cmd(m_hdl, m_tep));
162     else if (mode_t::GBP_L2 == m_mode || mode_t::GBP_L3 == m_mode)
163       HW::enqueue(new vxlan_gbp_tunnel_cmds::delete_cmd(m_hdl, m_tep));
164   }
165   HW::write();
166 }
167
168 void
169 vxlan_tunnel::replay()
170 {
171   if (m_hdl) {
172     if (mode_t::STANDARD == m_mode)
173       HW::enqueue(new vxlan_tunnel_cmds::create_cmd(
174         m_hdl,
175         name(),
176         m_tep,
177         (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
178     else if (mode_t::GBP_L2 == m_mode)
179       HW::enqueue(new vxlan_gbp_tunnel_cmds::create_cmd(
180         m_hdl,
181         name(),
182         m_tep,
183         true,
184         (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
185     else if (mode_t::GBP_L3 == m_mode)
186       HW::enqueue(new vxlan_gbp_tunnel_cmds::create_cmd(
187         m_hdl,
188         name(),
189         m_tep,
190         false,
191         (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
192   }
193   if (m_rd && (m_rd->table_id() != route::DEFAULT_TABLE)) {
194     HW::enqueue(
195       new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
196     HW::enqueue(
197       new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
198   }
199 }
200
201 vxlan_tunnel::~vxlan_tunnel()
202 {
203   sweep();
204   release();
205 }
206
207 std::string
208 vxlan_tunnel::to_string() const
209 {
210   std::ostringstream s;
211   s << "vxlan-tunnel: " << m_hdl.to_string() << " " << m_mode.to_string() << " "
212     << m_tep.to_string();
213   if (m_mcast_itf)
214     s << " " << m_mcast_itf->to_string();
215
216   return (s.str());
217 }
218
219 void
220 vxlan_tunnel::update(const vxlan_tunnel& desired)
221 {
222   /*
223    * the desired state is always that the interface should be created
224    */
225   if (rc_t::OK != m_hdl.rc()) {
226     if (mode_t::STANDARD == m_mode)
227       HW::enqueue(new vxlan_tunnel_cmds::create_cmd(
228         m_hdl,
229         name(),
230         m_tep,
231         (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
232     else if (mode_t::GBP_L2 == m_mode)
233       HW::enqueue(new vxlan_gbp_tunnel_cmds::create_cmd(
234         m_hdl,
235         name(),
236         m_tep,
237         true,
238         (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
239     else if (mode_t::GBP_L3 == m_mode)
240       HW::enqueue(new vxlan_gbp_tunnel_cmds::create_cmd(
241         m_hdl,
242         name(),
243         m_tep,
244         false,
245         (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
246   }
247   if (!m_table_id && m_rd) {
248     HW::enqueue(
249       new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
250     HW::enqueue(
251       new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
252   }
253 }
254
255 std::shared_ptr<vxlan_tunnel>
256 vxlan_tunnel::singular() const
257 {
258   return std::dynamic_pointer_cast<vxlan_tunnel>(singular_i());
259 }
260
261 std::shared_ptr<interface>
262 vxlan_tunnel::singular_i() const
263 {
264   return m_db.find_or_add(key(), *this);
265 }
266
267 void
268 vxlan_tunnel::event_handler::handle_populate(const client_db::key_t& key)
269 {
270   /*
271    * dump VPP current states
272    */
273   {
274     std::shared_ptr<vxlan_tunnel_cmds::dump_cmd> cmd =
275       std::make_shared<vxlan_tunnel_cmds::dump_cmd>();
276
277     HW::enqueue(cmd);
278     HW::write();
279
280     for (auto& record : *cmd) {
281       auto& payload = record.get_payload();
282       handle_t hdl(payload.sw_if_index);
283       boost::asio::ip::address src =
284         from_bytes(payload.src_address.af, (uint8_t*)&payload.src_address.un);
285       boost::asio::ip::address dst =
286         from_bytes(payload.dst_address.af, (uint8_t*)&payload.dst_address.un);
287
288       std::shared_ptr<vxlan_tunnel> vt =
289         vxlan_tunnel(src, dst, payload.vni).singular();
290       vt->set(hdl);
291
292       VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
293
294       OM::commit(key, *vt);
295     }
296   }
297   {
298     std::shared_ptr<vxlan_gbp_tunnel_cmds::dump_cmd> cmd =
299       std::make_shared<vxlan_gbp_tunnel_cmds::dump_cmd>();
300
301     HW::enqueue(cmd);
302     HW::write();
303
304     for (auto& record : *cmd) {
305       auto& payload = record.get_payload();
306       handle_t hdl(payload.tunnel.sw_if_index);
307       boost::asio::ip::address src = from_api(payload.tunnel.src);
308       boost::asio::ip::address dst = from_api(payload.tunnel.dst);
309
310       std::shared_ptr<vxlan_tunnel> vt =
311         vxlan_tunnel(src,
312                      dst,
313                      payload.tunnel.vni,
314                      (payload.tunnel.mode == VXLAN_GBP_API_TUNNEL_MODE_L2
315                         ? mode_t::GBP_L2
316                         : mode_t::GBP_L3))
317           .singular();
318       vt->set(hdl);
319
320       VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
321
322       OM::commit(key, *vt);
323     }
324   }
325 }
326
327 vxlan_tunnel::event_handler::event_handler()
328 {
329   OM::register_listener(this);
330   inspect::register_handler({ "vxlan" }, "VXLAN Tunnels", this);
331 }
332
333 void
334 vxlan_tunnel::event_handler::handle_replay()
335 {
336   // replay is handled from the interface DB
337 }
338
339 dependency_t
340 vxlan_tunnel::event_handler::order() const
341 {
342   return (dependency_t::VIRTUAL_INTERFACE);
343 }
344
345 void
346 vxlan_tunnel::event_handler::show(std::ostream& os)
347 {
348   // dumped by the interface handler
349 }
350
351 }; // namespace VOM
352
353 /*
354  * fd.io coding-style-patch-verification: ON
355  *
356  * Local Variables:
357  * eval: (c-set-style "mozilla")
358  * End:
359  */