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/logger.hpp"
20 const std::string VXLAN_TUNNEL_NAME = "vxlan-tunnel-itf";
22 vxlan_tunnel::event_handler vxlan_tunnel::m_evh;
25 * A DB of all vxlan_tunnels
26 * this does not register as a listener for replay events, since the tunnels
27 * are also in the base-class interface DB and so will be poked from there.
29 singular_db<vxlan_tunnel::endpoint_t, vxlan_tunnel> vxlan_tunnel::m_db;
31 vxlan_tunnel::endpoint_t::endpoint_t(const boost::asio::ip::address& src,
32 const boost::asio::ip::address& dst,
40 vxlan_tunnel::endpoint_t::endpoint_t()
48 vxlan_tunnel::endpoint_t::operator==(const endpoint_t& other) const
50 return ((src == other.src) && (dst == other.dst) && (vni == other.vni));
54 vxlan_tunnel::endpoint_t::operator<(const vxlan_tunnel::endpoint_t& o) const
67 vxlan_tunnel::endpoint_t::to_string() const
72 << "src:" << src.to_string() << " dst:" << dst.to_string() << " vni:" << vni
79 operator<<(std::ostream& os, const vxlan_tunnel::endpoint_t& ep)
87 vxlan_tunnel::mk_name(const boost::asio::ip::address& src,
88 const boost::asio::ip::address& dst,
93 s << VXLAN_TUNNEL_NAME << "-" << src << "-" << dst << ":" << vni;
98 vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
99 const boost::asio::ip::address& dst,
101 : interface(mk_name(src, dst, vni),
102 interface::type_t::VXLAN,
103 interface::admin_state_t::UP)
104 , m_tep(src, dst, vni)
108 vxlan_tunnel::vxlan_tunnel(const handle_t& hdl,
109 const boost::asio::ip::address& src,
110 const boost::asio::ip::address& dst,
114 mk_name(src, dst, vni),
115 interface::type_t::VXLAN,
116 interface::admin_state_t::UP)
117 , m_tep(src, dst, vni)
121 vxlan_tunnel::vxlan_tunnel(const vxlan_tunnel& o)
128 vxlan_tunnel::handle() const
130 return (m_hdl.data());
134 vxlan_tunnel::sweep()
137 HW::enqueue(new delete_cmd(m_hdl, m_tep));
143 vxlan_tunnel::replay()
146 HW::enqueue(new create_cmd(m_hdl, name(), m_tep));
150 vxlan_tunnel::~vxlan_tunnel()
155 * release from both DBs
158 m_db.release(m_tep, this);
162 vxlan_tunnel::to_string() const
164 std::ostringstream s;
165 s << "vxlan-tunnel: " << m_hdl.to_string() << " " << m_tep.to_string();
171 vxlan_tunnel::update(const vxlan_tunnel& desired)
174 * the desired state is always that the interface should be created
177 HW::enqueue(new create_cmd(m_hdl, name(), m_tep));
181 std::shared_ptr<vxlan_tunnel>
182 vxlan_tunnel::find_or_add(const vxlan_tunnel& temp)
185 * a VXLAN tunnel needs to be in both the interface-find-by-name
186 * and the vxlan_tunnel-find-by-endpoint singular databases
188 std::shared_ptr<vxlan_tunnel> sp;
190 sp = m_db.find_or_add(temp.m_tep, temp);
192 interface::m_db.add(temp.name(), sp);
197 std::shared_ptr<vxlan_tunnel>
198 vxlan_tunnel::singular() const
200 return (find_or_add(*this));
203 std::shared_ptr<interface>
204 vxlan_tunnel::singular_i() const
206 return find_or_add(*this);
210 vxlan_tunnel::dump(std::ostream& os)
216 vxlan_tunnel::event_handler::handle_populate(const client_db::key_t& key)
219 * dump VPP current states
221 std::shared_ptr<vxlan_tunnel::dump_cmd> cmd(new vxlan_tunnel::dump_cmd());
226 for (auto& record : *cmd) {
227 auto& payload = record.get_payload();
228 handle_t hdl(payload.sw_if_index);
229 boost::asio::ip::address src =
230 from_bytes(payload.is_ipv6, payload.src_address);
231 boost::asio::ip::address dst =
232 from_bytes(payload.is_ipv6, payload.dst_address);
234 vxlan_tunnel vt(hdl, src, dst, payload.vni);
236 VOM_LOG(log_level_t::DEBUG) << "dump: " << vt.to_string();
242 vxlan_tunnel::event_handler::event_handler()
244 OM::register_listener(this);
245 inspect::register_handler({ "vxlan" }, "VXLAN Tunnels", this);
249 vxlan_tunnel::event_handler::handle_replay()
251 // replay is handled from the interface DB
255 vxlan_tunnel::event_handler::order() const
257 return (dependency_t::TUNNEL);
261 vxlan_tunnel::event_handler::show(std::ostream& os)
267 * fd.io coding-style-patch-verification: ON
270 * eval: (c-set-style "mozilla")