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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
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"
25 const std::string VXLAN_TUNNEL_NAME = "vxlan-tunnel-itf";
27 vxlan_tunnel::event_handler vxlan_tunnel::m_evh;
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");
34 vxlan_tunnel::mode_t::mode_t(int v, const std::string s)
35 : enum_base<vxlan_tunnel::mode_t>(v, s)
38 vxlan_tunnel::endpoint_t::endpoint_t(const boost::asio::ip::address& src,
39 const boost::asio::ip::address& dst,
46 vxlan_tunnel::endpoint_t::endpoint_t()
53 vxlan_tunnel::endpoint_t::operator==(const endpoint_t& other) const
55 return ((src == other.src) && (dst == other.dst) && (vni == other.vni));
59 vxlan_tunnel::endpoint_t::to_string() const
64 << "src:" << src.to_string() << " dst:" << dst.to_string() << " vni:" << vni
71 vxlan_tunnel::mk_name(const boost::asio::ip::address& src,
72 const boost::asio::ip::address& dst,
78 s << VXLAN_TUNNEL_NAME << "-" << mode.to_string() << "-" << src << "-" << dst
84 vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
85 const boost::asio::ip::address& dst,
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)
95 , m_table_id(route::DEFAULT_TABLE)
98 vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
99 const boost::asio::ip::address& dst,
101 const interface& mcast_itf,
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)
108 , m_mcast_itf(mcast_itf.singular())
110 , m_table_id(route::DEFAULT_TABLE)
113 vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
114 const boost::asio::ip::address& dst,
116 const route_domain& rd,
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)
124 , m_rd(rd.singular())
125 , m_table_id(m_rd->table_id())
128 vxlan_tunnel::vxlan_tunnel(const vxlan_tunnel& o)
132 , m_mcast_itf(o.m_mcast_itf)
134 , m_table_id(o.m_table_id)
138 vxlan_tunnel::operator==(const vxlan_tunnel& other) const
140 return ((m_tep == other.m_tep) && (m_mode == other.m_mode) &&
141 (m_mcast_itf == other.m_mcast_itf));
145 vxlan_tunnel::handle() const
147 return (m_hdl.data());
150 std::shared_ptr<vxlan_tunnel>
151 vxlan_tunnel::find(const interface::key_t& k)
153 return std::dynamic_pointer_cast<vxlan_tunnel>(m_db.find(k));
157 vxlan_tunnel::sweep()
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));
169 vxlan_tunnel::replay()
172 if (mode_t::STANDARD == m_mode)
173 HW::enqueue(new vxlan_tunnel_cmds::create_cmd(
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(
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(
191 (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
193 if (m_rd && (m_rd->table_id() != route::DEFAULT_TABLE)) {
195 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
197 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
201 vxlan_tunnel::~vxlan_tunnel()
208 vxlan_tunnel::to_string() const
210 std::ostringstream s;
211 s << "vxlan-tunnel: " << m_hdl.to_string() << " " << m_mode.to_string() << " "
212 << m_tep.to_string();
214 s << " " << m_mcast_itf->to_string();
220 vxlan_tunnel::update(const vxlan_tunnel& desired)
223 * the desired state is always that the interface should be created
225 if (rc_t::OK != m_hdl.rc()) {
226 if (mode_t::STANDARD == m_mode)
227 HW::enqueue(new vxlan_tunnel_cmds::create_cmd(
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(
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(
245 (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
247 if (!m_table_id && m_rd) {
249 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
251 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
255 std::shared_ptr<vxlan_tunnel>
256 vxlan_tunnel::singular() const
258 return std::dynamic_pointer_cast<vxlan_tunnel>(singular_i());
261 std::shared_ptr<interface>
262 vxlan_tunnel::singular_i() const
264 return m_db.find_or_add(key(), *this);
268 vxlan_tunnel::event_handler::handle_populate(const client_db::key_t& key)
271 * dump VPP current states
274 std::shared_ptr<vxlan_tunnel_cmds::dump_cmd> cmd =
275 std::make_shared<vxlan_tunnel_cmds::dump_cmd>();
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);
288 std::shared_ptr<vxlan_tunnel> vt =
289 vxlan_tunnel(src, dst, payload.vni).singular();
292 VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
294 OM::commit(key, *vt);
298 std::shared_ptr<vxlan_gbp_tunnel_cmds::dump_cmd> cmd =
299 std::make_shared<vxlan_gbp_tunnel_cmds::dump_cmd>();
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);
310 std::shared_ptr<vxlan_tunnel> vt =
314 (payload.tunnel.mode == VXLAN_GBP_API_TUNNEL_MODE_L2
320 VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
322 OM::commit(key, *vt);
327 vxlan_tunnel::event_handler::event_handler()
329 OM::register_listener(this);
330 inspect::register_handler({ "vxlan" }, "VXLAN Tunnels", this);
334 vxlan_tunnel::event_handler::handle_replay()
336 // replay is handled from the interface DB
340 vxlan_tunnel::event_handler::order() const
342 return (dependency_t::VIRTUAL_INTERFACE);
346 vxlan_tunnel::event_handler::show(std::ostream& os)
348 // dumped by the interface handler
354 * fd.io coding-style-patch-verification: ON
357 * eval: (c-set-style "mozilla")