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/logger.hpp"
19 #include "vom/singular_db_funcs.hpp"
20 #include "vom/vxlan_gbp_tunnel_cmds.hpp"
21 #include "vom/vxlan_tunnel_cmds.hpp"
24 const std::string VXLAN_TUNNEL_NAME = "vxlan-tunnel-itf";
26 vxlan_tunnel::event_handler vxlan_tunnel::m_evh;
28 const vxlan_tunnel::mode_t vxlan_tunnel::mode_t::STANDARD(0, "standard");
29 const vxlan_tunnel::mode_t vxlan_tunnel::mode_t::GBP(1, "GBP");
30 const vxlan_tunnel::mode_t vxlan_tunnel::mode_t::GPE(2, "GPE");
32 vxlan_tunnel::mode_t::mode_t(int v, const std::string s)
33 : enum_base<vxlan_tunnel::mode_t>(v, s)
37 vxlan_tunnel::endpoint_t::endpoint_t(const boost::asio::ip::address& src,
38 const boost::asio::ip::address& dst,
46 vxlan_tunnel::endpoint_t::endpoint_t()
54 vxlan_tunnel::endpoint_t::operator==(const endpoint_t& other) const
56 return ((src == other.src) && (dst == other.dst) && (vni == other.vni));
60 vxlan_tunnel::endpoint_t::to_string() const
65 << "src:" << src.to_string() << " dst:" << dst.to_string() << " vni:" << vni
72 vxlan_tunnel::mk_name(const boost::asio::ip::address& src,
73 const boost::asio::ip::address& dst,
79 s << VXLAN_TUNNEL_NAME << "-" << mode.to_string() << "-" << src << "-" << dst
85 vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
86 const boost::asio::ip::address& dst,
89 : interface(mk_name(src, dst, mode, vni),
90 interface::type_t::VXLAN,
91 interface::admin_state_t::UP)
92 , m_tep(src, dst, vni)
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())
112 vxlan_tunnel::vxlan_tunnel(const vxlan_tunnel& o)
116 , m_mcast_itf(o.m_mcast_itf)
121 vxlan_tunnel::operator==(const vxlan_tunnel& other) const
123 return ((m_tep == other.m_tep) && (m_mode == other.m_mode) &&
124 (m_mcast_itf == other.m_mcast_itf));
128 vxlan_tunnel::handle() const
130 return (m_hdl.data());
133 std::shared_ptr<vxlan_tunnel>
134 vxlan_tunnel::find(const interface::key_t& k)
136 return std::dynamic_pointer_cast<vxlan_tunnel>(m_db.find(k));
140 vxlan_tunnel::sweep()
143 if (mode_t::STANDARD == m_mode)
144 HW::enqueue(new vxlan_tunnel_cmds::delete_cmd(m_hdl, m_tep));
145 else if (mode_t::GBP == m_mode)
146 HW::enqueue(new vxlan_gbp_tunnel_cmds::delete_cmd(m_hdl, m_tep));
152 vxlan_tunnel::replay()
155 if (mode_t::STANDARD == m_mode)
156 HW::enqueue(new vxlan_tunnel_cmds::create_cmd(
157 m_hdl, name(), m_tep,
158 (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
159 else if (mode_t::GBP == m_mode)
160 HW::enqueue(new vxlan_gbp_tunnel_cmds::create_cmd(
161 m_hdl, name(), m_tep,
162 (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
166 vxlan_tunnel::~vxlan_tunnel()
173 vxlan_tunnel::to_string() const
175 std::ostringstream s;
176 s << "vxlan-tunnel: " << m_hdl.to_string() << " " << m_mode.to_string() << " "
177 << m_tep.to_string();
179 s << " " << m_mcast_itf->to_string();
185 vxlan_tunnel::update(const vxlan_tunnel& desired)
188 * the desired state is always that the interface should be created
190 if (rc_t::OK != m_hdl.rc()) {
191 if (mode_t::STANDARD == m_mode)
192 HW::enqueue(new vxlan_tunnel_cmds::create_cmd(
193 m_hdl, name(), m_tep,
194 (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
195 else if (mode_t::GBP == m_mode)
196 HW::enqueue(new vxlan_gbp_tunnel_cmds::create_cmd(
197 m_hdl, name(), m_tep,
198 (m_mcast_itf ? m_mcast_itf->handle() : handle_t::INVALID)));
202 std::shared_ptr<vxlan_tunnel>
203 vxlan_tunnel::singular() const
205 return std::dynamic_pointer_cast<vxlan_tunnel>(singular_i());
208 std::shared_ptr<interface>
209 vxlan_tunnel::singular_i() const
211 return m_db.find_or_add(key(), *this);
215 vxlan_tunnel::event_handler::handle_populate(const client_db::key_t& key)
218 * dump VPP current states
221 std::shared_ptr<vxlan_tunnel_cmds::dump_cmd> cmd =
222 std::make_shared<vxlan_tunnel_cmds::dump_cmd>();
227 for (auto& record : *cmd) {
228 auto& payload = record.get_payload();
229 handle_t hdl(payload.sw_if_index);
230 boost::asio::ip::address src =
231 from_bytes(payload.is_ipv6, payload.src_address);
232 boost::asio::ip::address dst =
233 from_bytes(payload.is_ipv6, payload.dst_address);
235 std::shared_ptr<vxlan_tunnel> vt =
236 vxlan_tunnel(src, dst, payload.vni).singular();
239 VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
241 OM::commit(key, *vt);
245 std::shared_ptr<vxlan_gbp_tunnel_cmds::dump_cmd> cmd =
246 std::make_shared<vxlan_gbp_tunnel_cmds::dump_cmd>();
251 for (auto& record : *cmd) {
252 auto& payload = record.get_payload();
253 handle_t hdl(payload.tunnel.sw_if_index);
254 boost::asio::ip::address src = from_api(payload.tunnel.src);
255 boost::asio::ip::address dst = from_api(payload.tunnel.dst);
257 std::shared_ptr<vxlan_tunnel> vt =
258 vxlan_tunnel(src, dst, payload.tunnel.vni, mode_t::GBP).singular();
261 VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
263 OM::commit(key, *vt);
268 vxlan_tunnel::event_handler::event_handler()
270 OM::register_listener(this);
271 inspect::register_handler({ "vxlan" }, "VXLAN Tunnels", this);
275 vxlan_tunnel::event_handler::handle_replay()
277 // replay is handled from the interface DB
281 vxlan_tunnel::event_handler::order() const
283 return (dependency_t::VIRTUAL_INTERFACE);
287 vxlan_tunnel::event_handler::show(std::ostream& os)
289 // dumped by the interface handler
295 * fd.io coding-style-patch-verification: ON
298 * eval: (c-set-style "mozilla")