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/singular_db_funcs.hpp"
19 #include "vom/vxlan_tunnel_cmds.hpp"
22 const std::string VXLAN_TUNNEL_NAME = "vxlan-tunnel-itf";
24 vxlan_tunnel::event_handler vxlan_tunnel::m_evh;
27 * A DB of all vxlan_tunnels
28 * this does not register as a listener for replay events, since the tunnels
29 * are also in the base-class interface DB and so will be poked from there.
31 singular_db<vxlan_tunnel::endpoint_t, vxlan_tunnel> vxlan_tunnel::m_db;
33 vxlan_tunnel::endpoint_t::endpoint_t(const boost::asio::ip::address& src,
34 const boost::asio::ip::address& dst,
42 vxlan_tunnel::endpoint_t::endpoint_t()
50 vxlan_tunnel::endpoint_t::operator==(const endpoint_t& other) const
52 return ((src == other.src) && (dst == other.dst) && (vni == other.vni));
56 vxlan_tunnel::endpoint_t::operator<(const vxlan_tunnel::endpoint_t& o) const
69 vxlan_tunnel::endpoint_t::to_string() const
74 << "src:" << src.to_string() << " dst:" << dst.to_string() << " vni:" << vni
81 operator<<(std::ostream& os, const vxlan_tunnel::endpoint_t& ep)
89 vxlan_tunnel::mk_name(const boost::asio::ip::address& src,
90 const boost::asio::ip::address& dst,
95 s << VXLAN_TUNNEL_NAME << "-" << src << "-" << dst << ":" << vni;
100 vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
101 const boost::asio::ip::address& dst,
103 : interface(mk_name(src, dst, vni),
104 interface::type_t::VXLAN,
105 interface::admin_state_t::UP)
106 , m_tep(src, dst, vni)
110 vxlan_tunnel::vxlan_tunnel(const vxlan_tunnel& o)
117 vxlan_tunnel::handle() const
119 return (m_hdl.data());
123 vxlan_tunnel::sweep()
126 HW::enqueue(new vxlan_tunnel_cmds::delete_cmd(m_hdl, m_tep));
132 vxlan_tunnel::replay()
135 HW::enqueue(new vxlan_tunnel_cmds::create_cmd(m_hdl, name(), m_tep));
139 vxlan_tunnel::~vxlan_tunnel()
144 * release from both DBs
147 m_db.release(m_tep, this);
151 vxlan_tunnel::to_string() const
153 std::ostringstream s;
154 s << "vxlan-tunnel: " << m_hdl.to_string() << " " << m_tep.to_string();
160 vxlan_tunnel::update(const vxlan_tunnel& desired)
163 * the desired state is always that the interface should be created
166 HW::enqueue(new vxlan_tunnel_cmds::create_cmd(m_hdl, name(), m_tep));
170 std::shared_ptr<vxlan_tunnel>
171 vxlan_tunnel::find_or_add(const vxlan_tunnel& temp)
174 * a VXLAN tunnel needs to be in both the interface-find-by-name
175 * and the vxlan_tunnel-find-by-endpoint singular databases
177 std::shared_ptr<vxlan_tunnel> sp;
179 sp = m_db.find_or_add(temp.m_tep, temp);
181 interface::m_db.add(temp.name(), sp);
186 std::shared_ptr<vxlan_tunnel>
187 vxlan_tunnel::singular() const
189 return (find_or_add(*this));
192 std::shared_ptr<interface>
193 vxlan_tunnel::singular_i() const
195 return find_or_add(*this);
199 vxlan_tunnel::dump(std::ostream& os)
205 vxlan_tunnel::event_handler::handle_populate(const client_db::key_t& key)
208 * dump VPP current states
210 std::shared_ptr<vxlan_tunnel_cmds::dump_cmd> cmd =
211 std::make_shared<vxlan_tunnel_cmds::dump_cmd>();
216 for (auto& record : *cmd) {
217 auto& payload = record.get_payload();
218 handle_t hdl(payload.sw_if_index);
219 boost::asio::ip::address src =
220 from_bytes(payload.is_ipv6, payload.src_address);
221 boost::asio::ip::address dst =
222 from_bytes(payload.is_ipv6, payload.dst_address);
224 std::shared_ptr<vxlan_tunnel> vt =
225 vxlan_tunnel(src, dst, payload.vni).singular();
228 VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
230 OM::commit(key, *vt);
234 vxlan_tunnel::event_handler::event_handler()
236 OM::register_listener(this);
237 inspect::register_handler({ "vxlan" }, "VXLAN Tunnels", this);
241 vxlan_tunnel::event_handler::handle_replay()
243 // replay is handled from the interface DB
247 vxlan_tunnel::event_handler::order() const
249 return (dependency_t::TUNNEL);
253 vxlan_tunnel::event_handler::show(std::ostream& os)
255 // dumped by the interface handler
261 * fd.io coding-style-patch-verification: ON
264 * eval: (c-set-style "mozilla")