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"
18 #include "vom/vxlan_tunnel_cmds.hpp"
21 const std::string VXLAN_TUNNEL_NAME = "vxlan-tunnel-itf";
23 vxlan_tunnel::event_handler vxlan_tunnel::m_evh;
26 * A DB of all vxlan_tunnels
27 * this does not register as a listener for replay events, since the tunnels
28 * are also in the base-class interface DB and so will be poked from there.
30 singular_db<vxlan_tunnel::endpoint_t, vxlan_tunnel> vxlan_tunnel::m_db;
32 vxlan_tunnel::endpoint_t::endpoint_t(const boost::asio::ip::address& src,
33 const boost::asio::ip::address& dst,
41 vxlan_tunnel::endpoint_t::endpoint_t()
49 vxlan_tunnel::endpoint_t::operator==(const endpoint_t& other) const
51 return ((src == other.src) && (dst == other.dst) && (vni == other.vni));
55 vxlan_tunnel::endpoint_t::operator<(const vxlan_tunnel::endpoint_t& o) const
68 vxlan_tunnel::endpoint_t::to_string() const
73 << "src:" << src.to_string() << " dst:" << dst.to_string() << " vni:" << vni
80 operator<<(std::ostream& os, const vxlan_tunnel::endpoint_t& ep)
88 vxlan_tunnel::mk_name(const boost::asio::ip::address& src,
89 const boost::asio::ip::address& dst,
94 s << VXLAN_TUNNEL_NAME << "-" << src << "-" << dst << ":" << vni;
99 vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
100 const boost::asio::ip::address& dst,
102 : interface(mk_name(src, dst, vni),
103 interface::type_t::VXLAN,
104 interface::admin_state_t::UP)
105 , m_tep(src, dst, vni)
109 vxlan_tunnel::vxlan_tunnel(const vxlan_tunnel& o)
116 vxlan_tunnel::handle() const
118 return (m_hdl.data());
122 vxlan_tunnel::sweep()
125 HW::enqueue(new vxlan_tunnel_cmds::delete_cmd(m_hdl, m_tep));
131 vxlan_tunnel::replay()
134 HW::enqueue(new vxlan_tunnel_cmds::create_cmd(m_hdl, name(), m_tep));
138 vxlan_tunnel::~vxlan_tunnel()
143 * release from both DBs
146 m_db.release(m_tep, this);
150 vxlan_tunnel::to_string() const
152 std::ostringstream s;
153 s << "vxlan-tunnel: " << m_hdl.to_string() << " " << m_tep.to_string();
159 vxlan_tunnel::update(const vxlan_tunnel& desired)
162 * the desired state is always that the interface should be created
165 HW::enqueue(new vxlan_tunnel_cmds::create_cmd(m_hdl, name(), m_tep));
169 std::shared_ptr<vxlan_tunnel>
170 vxlan_tunnel::find_or_add(const vxlan_tunnel& temp)
173 * a VXLAN tunnel needs to be in both the interface-find-by-name
174 * and the vxlan_tunnel-find-by-endpoint singular databases
176 std::shared_ptr<vxlan_tunnel> sp;
178 sp = m_db.find_or_add(temp.m_tep, temp);
180 interface::m_db.add(temp.name(), sp);
185 std::shared_ptr<vxlan_tunnel>
186 vxlan_tunnel::singular() const
188 return (find_or_add(*this));
191 std::shared_ptr<interface>
192 vxlan_tunnel::singular_i() const
194 return find_or_add(*this);
198 vxlan_tunnel::dump(std::ostream& os)
204 vxlan_tunnel::event_handler::handle_populate(const client_db::key_t& key)
207 * dump VPP current states
209 std::shared_ptr<vxlan_tunnel_cmds::dump_cmd> cmd =
210 std::make_shared<vxlan_tunnel_cmds::dump_cmd>();
215 for (auto& record : *cmd) {
216 auto& payload = record.get_payload();
217 handle_t hdl(payload.sw_if_index);
218 boost::asio::ip::address src =
219 from_bytes(payload.is_ipv6, payload.src_address);
220 boost::asio::ip::address dst =
221 from_bytes(payload.is_ipv6, payload.dst_address);
223 std::shared_ptr<vxlan_tunnel> vt =
224 vxlan_tunnel(src, dst, payload.vni).singular();
227 VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
229 OM::commit(key, *vt);
233 vxlan_tunnel::event_handler::event_handler()
235 OM::register_listener(this);
236 inspect::register_handler({ "vxlan" }, "VXLAN Tunnels", this);
240 vxlan_tunnel::event_handler::handle_replay()
242 // replay is handled from the interface DB
246 vxlan_tunnel::event_handler::order() const
248 return (dependency_t::TUNNEL);
252 vxlan_tunnel::event_handler::show(std::ostream& os)
254 // dumped by the interface handler
260 * fd.io coding-style-patch-verification: ON
263 * eval: (c-set-style "mozilla")