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