blob: f6f3cf586e181a5b0e53056cea8872976ec68027 [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
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.
14 */
15
16#include "vom/vxlan_tunnel.hpp"
17#include "vom/logger.hpp"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070018#include "vom/vxlan_tunnel_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070019
20namespace VOM {
21const std::string VXLAN_TUNNEL_NAME = "vxlan-tunnel-itf";
22
23vxlan_tunnel::event_handler vxlan_tunnel::m_evh;
24
25/**
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.
29 */
30singular_db<vxlan_tunnel::endpoint_t, vxlan_tunnel> vxlan_tunnel::m_db;
31
32vxlan_tunnel::endpoint_t::endpoint_t(const boost::asio::ip::address& src,
33 const boost::asio::ip::address& dst,
34 uint32_t vni)
35 : src(src)
36 , dst(dst)
37 , vni(vni)
38{
39}
40
41vxlan_tunnel::endpoint_t::endpoint_t()
42 : src()
43 , dst()
44 , vni(0)
45{
46}
47
48bool
49vxlan_tunnel::endpoint_t::operator==(const endpoint_t& other) const
50{
51 return ((src == other.src) && (dst == other.dst) && (vni == other.vni));
52}
53
54bool
55vxlan_tunnel::endpoint_t::operator<(const vxlan_tunnel::endpoint_t& o) const
56{
57 if (src < o.src)
58 return true;
59 if (dst < o.dst)
60 return true;
61 if (vni < o.vni)
62 return true;
63
64 return false;
65}
66
67std::string
68vxlan_tunnel::endpoint_t::to_string() const
69{
70 std::ostringstream s;
71
72 s << "ep:["
73 << "src:" << src.to_string() << " dst:" << dst.to_string() << " vni:" << vni
74 << "]";
75
76 return (s.str());
77}
78
79std::ostream&
80operator<<(std::ostream& os, const vxlan_tunnel::endpoint_t& ep)
81{
82 os << ep.to_string();
83
84 return (os);
85}
86
87std::string
88vxlan_tunnel::mk_name(const boost::asio::ip::address& src,
89 const boost::asio::ip::address& dst,
90 uint32_t vni)
91{
92 std::ostringstream s;
93
94 s << VXLAN_TUNNEL_NAME << "-" << src << "-" << dst << ":" << vni;
95
96 return (s.str());
97}
98
99vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
100 const boost::asio::ip::address& dst,
101 uint32_t vni)
102 : interface(mk_name(src, dst, vni),
103 interface::type_t::VXLAN,
104 interface::admin_state_t::UP)
105 , m_tep(src, dst, vni)
106{
107}
108
Neale Ranns812ed392017-10-16 04:20:13 -0700109vxlan_tunnel::vxlan_tunnel(const vxlan_tunnel& o)
110 : interface(o)
111 , m_tep(o.m_tep)
112{
113}
114
115const handle_t&
116vxlan_tunnel::handle() const
117{
118 return (m_hdl.data());
119}
120
121void
122vxlan_tunnel::sweep()
123{
124 if (m_hdl) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700125 HW::enqueue(new vxlan_tunnel_cmds::delete_cmd(m_hdl, m_tep));
Neale Ranns812ed392017-10-16 04:20:13 -0700126 }
127 HW::write();
128}
129
130void
131vxlan_tunnel::replay()
132{
133 if (m_hdl) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700134 HW::enqueue(new vxlan_tunnel_cmds::create_cmd(m_hdl, name(), m_tep));
Neale Ranns812ed392017-10-16 04:20:13 -0700135 }
136}
137
138vxlan_tunnel::~vxlan_tunnel()
139{
140 sweep();
141
142 /*
143 * release from both DBs
144 */
145 release();
146 m_db.release(m_tep, this);
147}
148
149std::string
150vxlan_tunnel::to_string() const
151{
152 std::ostringstream s;
153 s << "vxlan-tunnel: " << m_hdl.to_string() << " " << m_tep.to_string();
154
155 return (s.str());
156}
157
158void
159vxlan_tunnel::update(const vxlan_tunnel& desired)
160{
161 /*
162 * the desired state is always that the interface should be created
163 */
164 if (!m_hdl) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700165 HW::enqueue(new vxlan_tunnel_cmds::create_cmd(m_hdl, name(), m_tep));
Neale Ranns812ed392017-10-16 04:20:13 -0700166 }
167}
168
169std::shared_ptr<vxlan_tunnel>
170vxlan_tunnel::find_or_add(const vxlan_tunnel& temp)
171{
172 /*
173 * a VXLAN tunnel needs to be in both the interface-find-by-name
174 * and the vxlan_tunnel-find-by-endpoint singular databases
175 */
176 std::shared_ptr<vxlan_tunnel> sp;
177
178 sp = m_db.find_or_add(temp.m_tep, temp);
179
180 interface::m_db.add(temp.name(), sp);
181
182 return (sp);
183}
184
185std::shared_ptr<vxlan_tunnel>
186vxlan_tunnel::singular() const
187{
188 return (find_or_add(*this));
189}
190
191std::shared_ptr<interface>
192vxlan_tunnel::singular_i() const
193{
194 return find_or_add(*this);
195}
196
197void
198vxlan_tunnel::dump(std::ostream& os)
199{
200 m_db.dump(os);
201}
202
203void
204vxlan_tunnel::event_handler::handle_populate(const client_db::key_t& key)
205{
206 /*
Neale Rannsfd920602017-11-23 12:15:00 -0800207 * dump VPP current states
208 */
Neale Ranns1d781552017-11-27 04:52:35 -0800209 std::shared_ptr<vxlan_tunnel_cmds::dump_cmd> cmd =
210 std::make_shared<vxlan_tunnel_cmds::dump_cmd>();
Neale Ranns812ed392017-10-16 04:20:13 -0700211
212 HW::enqueue(cmd);
213 HW::write();
214
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);
222
Neale Rannsa2ee0292017-11-28 22:29:13 -0800223 std::shared_ptr<vxlan_tunnel> vt =
224 vxlan_tunnel(src, dst, payload.vni).singular();
225 vt->set(hdl);
Neale Ranns812ed392017-10-16 04:20:13 -0700226
Neale Rannsa2ee0292017-11-28 22:29:13 -0800227 VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
Neale Ranns812ed392017-10-16 04:20:13 -0700228
Neale Rannsa2ee0292017-11-28 22:29:13 -0800229 OM::commit(key, *vt);
Neale Ranns812ed392017-10-16 04:20:13 -0700230 }
231}
232
233vxlan_tunnel::event_handler::event_handler()
234{
235 OM::register_listener(this);
236 inspect::register_handler({ "vxlan" }, "VXLAN Tunnels", this);
237}
238
239void
240vxlan_tunnel::event_handler::handle_replay()
241{
242 // replay is handled from the interface DB
243}
244
245dependency_t
246vxlan_tunnel::event_handler::order() const
247{
248 return (dependency_t::TUNNEL);
249}
250
251void
252vxlan_tunnel::event_handler::show(std::ostream& os)
253{
Neale Rannsfd920602017-11-23 12:15:00 -0800254 // dumped by the interface handler
Neale Ranns812ed392017-10-16 04:20:13 -0700255}
Neale Rannsfd920602017-11-23 12:15:00 -0800256
257}; // namespace VOM
258
Neale Ranns812ed392017-10-16 04:20:13 -0700259/*
260 * fd.io coding-style-patch-verification: ON
261 *
262 * Local Variables:
263 * eval: (c-set-style "mozilla")
264 * End:
265 */