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)
39 vxlan_tunnel::endpoint_t::endpoint_t(const boost::asio::ip::address& src,
40 const boost::asio::ip::address& dst,
48 vxlan_tunnel::endpoint_t::endpoint_t()
56 vxlan_tunnel::endpoint_t::operator==(const endpoint_t& other) const
58 return ((src == other.src) && (dst == other.dst) && (vni == other.vni));
62 vxlan_tunnel::endpoint_t::to_string() const
67 << "src:" << src.to_string() << " dst:" << dst.to_string() << " vni:" << vni
74 vxlan_tunnel::mk_name(const boost::asio::ip::address& src,
75 const boost::asio::ip::address& dst,
81 s << VXLAN_TUNNEL_NAME << "-" << mode.to_string() << "-" << src << "-" << dst
87 vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
88 const boost::asio::ip::address& dst,
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)
98 , m_table_id(route::DEFAULT_TABLE)
102 vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
103 const boost::asio::ip::address& dst,
105 const interface& mcast_itf,
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)
112 , m_mcast_itf(mcast_itf.singular())
114 , m_table_id(route::DEFAULT_TABLE)
118 vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
119 const boost::asio::ip::address& dst,
121 const route_domain& rd,
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)
129 , m_rd(rd.singular())
130 , m_table_id(m_rd->table_id())
134 vxlan_tunnel::vxlan_tunnel(const vxlan_tunnel& o)
138 , m_mcast_itf(o.m_mcast_itf)
140 , m_table_id(o.m_table_id)
145 vxlan_tunnel::operator==(const vxlan_tunnel& other) const
147 return ((m_tep == other.m_tep) && (m_mode == other.m_mode) &&
148 (m_mcast_itf == other.m_mcast_itf));
152 vxlan_tunnel::handle() const
154 return (m_hdl.data());
157 std::shared_ptr<vxlan_tunnel>
158 vxlan_tunnel::find(const interface::key_t& k)
160 return std::dynamic_pointer_cast<vxlan_tunnel>(m_db.find(k));
164 vxlan_tunnel::sweep()
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));
176 vxlan_tunnel::replay()
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)));
192 if (m_rd && (m_rd->table_id() != route::DEFAULT_TABLE)) {
194 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
196 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
200 vxlan_tunnel::~vxlan_tunnel()
207 vxlan_tunnel::to_string() const
209 std::ostringstream s;
210 s << "vxlan-tunnel: " << m_hdl.to_string() << " " << m_mode.to_string() << " "
211 << m_tep.to_string();
213 s << " " << m_mcast_itf->to_string();
219 vxlan_tunnel::update(const vxlan_tunnel& desired)
222 * the desired state is always that the interface should be created
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)));
238 if (!m_table_id && m_rd) {
240 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
242 new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
246 std::shared_ptr<vxlan_tunnel>
247 vxlan_tunnel::singular() const
249 return std::dynamic_pointer_cast<vxlan_tunnel>(singular_i());
252 std::shared_ptr<interface>
253 vxlan_tunnel::singular_i() const
255 return m_db.find_or_add(key(), *this);
259 vxlan_tunnel::event_handler::handle_populate(const client_db::key_t& key)
262 * dump VPP current states
265 std::shared_ptr<vxlan_tunnel_cmds::dump_cmd> cmd =
266 std::make_shared<vxlan_tunnel_cmds::dump_cmd>();
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);
279 std::shared_ptr<vxlan_tunnel> vt =
280 vxlan_tunnel(src, dst, payload.vni).singular();
283 VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
285 OM::commit(key, *vt);
289 std::shared_ptr<vxlan_gbp_tunnel_cmds::dump_cmd> cmd =
290 std::make_shared<vxlan_gbp_tunnel_cmds::dump_cmd>();
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);
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
309 VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
311 OM::commit(key, *vt);
316 vxlan_tunnel::event_handler::event_handler()
318 OM::register_listener(this);
319 inspect::register_handler({ "vxlan" }, "VXLAN Tunnels", this);
323 vxlan_tunnel::event_handler::handle_replay()
325 // replay is handled from the interface DB
329 vxlan_tunnel::event_handler::order() const
331 return (dependency_t::VIRTUAL_INTERFACE);
335 vxlan_tunnel::event_handler::show(std::ostream& os)
337 // dumped by the interface handler
343 * fd.io coding-style-patch-verification: ON
346 * eval: (c-set-style "mozilla")