VOM: vxlan-gbp
[vpp.git] / extras / vom / vom / vxlan_tunnel.cpp
1 /*
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/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"
22
23 namespace VOM {
24 const std::string VXLAN_TUNNEL_NAME = "vxlan-tunnel-itf";
25
26 vxlan_tunnel::event_handler vxlan_tunnel::m_evh;
27
28 const vxlan_tunnel::mode_t vxlan_tunnel::mode_t::STANDARD(0, "standard");
29 const vxlan_tunnel::mode_t vxlan_tunnel::mode_t::GBP(0, "GBP");
30 const vxlan_tunnel::mode_t vxlan_tunnel::mode_t::GPE(0, "GPE");
31
32 vxlan_tunnel::mode_t::mode_t(int v, const std::string s)
33   : enum_base<vxlan_tunnel::mode_t>(v, s)
34 {
35 }
36
37 vxlan_tunnel::endpoint_t::endpoint_t(const boost::asio::ip::address& src,
38                                      const boost::asio::ip::address& dst,
39                                      uint32_t vni)
40   : src(src)
41   , dst(dst)
42   , vni(vni)
43 {
44 }
45
46 vxlan_tunnel::endpoint_t::endpoint_t()
47   : src()
48   , dst()
49   , vni(0)
50 {
51 }
52
53 bool
54 vxlan_tunnel::endpoint_t::operator==(const endpoint_t& other) const
55 {
56   return ((src == other.src) && (dst == other.dst) && (vni == other.vni));
57 }
58
59 std::string
60 vxlan_tunnel::endpoint_t::to_string() const
61 {
62   std::ostringstream s;
63
64   s << "ep:["
65     << "src:" << src.to_string() << " dst:" << dst.to_string() << " vni:" << vni
66     << "]";
67
68   return (s.str());
69 }
70
71 std::string
72 vxlan_tunnel::mk_name(const boost::asio::ip::address& src,
73                       const boost::asio::ip::address& dst,
74                       const mode_t& mode,
75                       uint32_t vni)
76 {
77   std::ostringstream s;
78
79   s << VXLAN_TUNNEL_NAME << "-" << mode.to_string() << "-" << src << "-" << dst
80     << ":" << vni;
81
82   return (s.str());
83 }
84
85 vxlan_tunnel::vxlan_tunnel(const boost::asio::ip::address& src,
86                            const boost::asio::ip::address& dst,
87                            uint32_t vni,
88                            const mode_t& mode)
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)
93   , m_mode(mode)
94 {
95 }
96
97 vxlan_tunnel::vxlan_tunnel(const vxlan_tunnel& o)
98   : interface(o)
99   , m_tep(o.m_tep)
100   , m_mode(o.m_mode)
101 {
102 }
103
104 const handle_t&
105 vxlan_tunnel::handle() const
106 {
107   return (m_hdl.data());
108 }
109
110 std::shared_ptr<vxlan_tunnel>
111 vxlan_tunnel::find(const interface::key_t& k)
112 {
113   return std::dynamic_pointer_cast<vxlan_tunnel>(m_db.find(k));
114 }
115
116 void
117 vxlan_tunnel::sweep()
118 {
119   if (m_hdl) {
120     if (mode_t::STANDARD == m_mode)
121       HW::enqueue(new vxlan_tunnel_cmds::delete_cmd(m_hdl, m_tep));
122     else if (mode_t::GBP == m_mode)
123       HW::enqueue(new vxlan_gbp_tunnel_cmds::delete_cmd(m_hdl, m_tep));
124   }
125   HW::write();
126 }
127
128 void
129 vxlan_tunnel::replay()
130 {
131   if (m_hdl) {
132     if (mode_t::STANDARD == m_mode)
133       HW::enqueue(new vxlan_tunnel_cmds::create_cmd(m_hdl, name(), m_tep));
134     else if (mode_t::GBP == m_mode)
135       HW::enqueue(new vxlan_gbp_tunnel_cmds::create_cmd(m_hdl, name(), m_tep));
136   }
137 }
138
139 vxlan_tunnel::~vxlan_tunnel()
140 {
141   sweep();
142   release();
143 }
144
145 std::string
146 vxlan_tunnel::to_string() const
147 {
148   std::ostringstream s;
149   s << "vxlan-tunnel: " << m_hdl.to_string() << " " << m_mode.to_string() << " "
150     << m_tep.to_string();
151
152   return (s.str());
153 }
154
155 void
156 vxlan_tunnel::update(const vxlan_tunnel& desired)
157 {
158   /*
159    * the desired state is always that the interface should be created
160    */
161   if (!m_hdl) {
162     if (mode_t::STANDARD == m_mode)
163       HW::enqueue(new vxlan_tunnel_cmds::create_cmd(m_hdl, name(), m_tep));
164     else if (mode_t::GBP == m_mode)
165       HW::enqueue(new vxlan_gbp_tunnel_cmds::create_cmd(m_hdl, name(), m_tep));
166   }
167 }
168
169 std::shared_ptr<vxlan_tunnel>
170 vxlan_tunnel::singular() const
171 {
172   return std::dynamic_pointer_cast<vxlan_tunnel>(singular_i());
173 }
174
175 std::shared_ptr<interface>
176 vxlan_tunnel::singular_i() const
177 {
178   return m_db.find_or_add(key(), *this);
179 }
180
181 void
182 vxlan_tunnel::event_handler::handle_populate(const client_db::key_t& key)
183 {
184   /*
185    * dump VPP current states
186    */
187   {
188     std::shared_ptr<vxlan_tunnel_cmds::dump_cmd> cmd =
189       std::make_shared<vxlan_tunnel_cmds::dump_cmd>();
190
191     HW::enqueue(cmd);
192     HW::write();
193
194     for (auto& record : *cmd) {
195       auto& payload = record.get_payload();
196       handle_t hdl(payload.sw_if_index);
197       boost::asio::ip::address src =
198         from_bytes(payload.is_ipv6, payload.src_address);
199       boost::asio::ip::address dst =
200         from_bytes(payload.is_ipv6, payload.dst_address);
201
202       std::shared_ptr<vxlan_tunnel> vt =
203         vxlan_tunnel(src, dst, payload.vni).singular();
204       vt->set(hdl);
205
206       VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
207
208       OM::commit(key, *vt);
209     }
210   }
211   {
212     std::shared_ptr<vxlan_gbp_tunnel_cmds::dump_cmd> cmd =
213       std::make_shared<vxlan_gbp_tunnel_cmds::dump_cmd>();
214
215     HW::enqueue(cmd);
216     HW::write();
217
218     for (auto& record : *cmd) {
219       auto& payload = record.get_payload();
220       handle_t hdl(payload.tunnel.sw_if_index);
221       boost::asio::ip::address src = from_api(payload.tunnel.src);
222       boost::asio::ip::address dst = from_api(payload.tunnel.dst);
223
224       std::shared_ptr<vxlan_tunnel> vt =
225         vxlan_tunnel(src, dst, payload.tunnel.vni, mode_t::GBP).singular();
226       vt->set(hdl);
227
228       VOM_LOG(log_level_t::DEBUG) << "dump: " << vt->to_string();
229
230       OM::commit(key, *vt);
231     }
232   }
233 }
234
235 vxlan_tunnel::event_handler::event_handler()
236 {
237   OM::register_listener(this);
238   inspect::register_handler({ "vxlan" }, "VXLAN Tunnels", this);
239 }
240
241 void
242 vxlan_tunnel::event_handler::handle_replay()
243 {
244   // replay is handled from the interface DB
245 }
246
247 dependency_t
248 vxlan_tunnel::event_handler::order() const
249 {
250   return (dependency_t::VIRTUAL_INTERFACE);
251 }
252
253 void
254 vxlan_tunnel::event_handler::show(std::ostream& os)
255 {
256   // dumped by the interface handler
257 }
258
259 }; // namespace VOM
260
261 /*
262  * fd.io coding-style-patch-verification: ON
263  *
264  * Local Variables:
265  * eval: (c-set-style "mozilla")
266  * End:
267  */