35422e2a6d4dea9a273f5b87bd9dc2b0b57a7255
[vpp.git] / extras / vom / vom / gbp_subnet.cpp
1 /*
2  * Copyright (c) 2018 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/gbp_subnet.hpp"
17 #include "vom/api_types.hpp"
18 #include "vom/gbp_subnet_cmds.hpp"
19 #include "vom/singular_db_funcs.hpp"
20
21 namespace VOM {
22
23 gbp_subnet::type_t::type_t(int v, const std::string s)
24   : enum_base<gbp_subnet::type_t>(v, s)
25 {
26 }
27
28 const gbp_subnet::type_t gbp_subnet::type_t::STITCHED_INTERNAL(
29   0,
30   "stitched-internal");
31 const gbp_subnet::type_t gbp_subnet::type_t::STITCHED_EXTERNAL(
32   1,
33   "stitched-external");
34 const gbp_subnet::type_t gbp_subnet::type_t::TRANSPORT(2, "transport");
35 const gbp_subnet::type_t gbp_subnet::type_t::L3_OUT(3, "l3-out");
36
37 singular_db<gbp_subnet::key_t, gbp_subnet> gbp_subnet::m_db;
38
39 gbp_subnet::event_handler gbp_subnet::m_evh;
40
41 gbp_subnet::gbp_subnet(const gbp_route_domain& rd,
42                        const route::prefix_t& prefix,
43                        const type_t& type)
44   : m_hw(false)
45   , m_rd(rd.singular())
46   , m_prefix(prefix)
47   , m_type(type)
48   , m_recirc(nullptr)
49   , m_epg(nullptr)
50   , m_sclass(~0)
51 {
52 }
53
54 gbp_subnet::gbp_subnet(const gbp_route_domain& rd,
55                        const route::prefix_t& prefix,
56                        const gbp_recirc& recirc,
57                        const gbp_endpoint_group& epg)
58   : m_hw(false)
59   , m_rd(rd.singular())
60   , m_prefix(prefix)
61   , m_type(type_t::STITCHED_EXTERNAL)
62   , m_recirc(recirc.singular())
63   , m_epg(epg.singular())
64   , m_sclass(~0)
65 {
66 }
67
68 gbp_subnet::gbp_subnet(const gbp_route_domain& rd,
69                        const route::prefix_t& prefix,
70                        sclass_t sclass)
71   : m_hw(false)
72   , m_rd(rd.singular())
73   , m_prefix(prefix)
74   , m_type(type_t::L3_OUT)
75   , m_recirc(nullptr)
76   , m_epg()
77   , m_sclass(sclass)
78 {
79 }
80
81 gbp_subnet::gbp_subnet(const gbp_subnet& o)
82   : m_hw(o.m_hw)
83   , m_rd(o.m_rd)
84   , m_prefix(o.m_prefix)
85   , m_type(o.m_type)
86   , m_recirc(o.m_recirc)
87   , m_epg(o.m_epg)
88   , m_sclass(o.m_sclass)
89 {
90 }
91
92 gbp_subnet::~gbp_subnet()
93 {
94   sweep();
95   m_db.release(key(), this);
96 }
97
98 const gbp_subnet::key_t
99 gbp_subnet::key() const
100 {
101   return (std::make_pair(m_rd->key(), m_prefix));
102 }
103
104 bool
105 gbp_subnet::operator==(const gbp_subnet& gs) const
106 {
107   return ((key() == gs.key()) && (m_type == gs.m_type) &&
108           (m_recirc == gs.m_recirc) && (m_epg == gs.m_epg) &&
109           (m_sclass == gs.m_sclass));
110 }
111
112 void
113 gbp_subnet::sweep()
114 {
115   if (m_hw) {
116     HW::enqueue(new gbp_subnet_cmds::delete_cmd(m_hw, m_rd->id(), m_prefix));
117   }
118   HW::write();
119 }
120
121 void
122 gbp_subnet::replay()
123 {
124   if (m_hw) {
125     HW::enqueue(new gbp_subnet_cmds::create_cmd(
126       m_hw, m_rd->id(), m_prefix, m_type,
127       (m_recirc ? m_recirc->handle() : handle_t::INVALID),
128       (m_epg ? m_epg->sclass() : m_sclass)));
129   }
130 }
131
132 std::string
133 gbp_subnet::to_string() const
134 {
135   std::ostringstream s;
136   s << "gbp-subnet:[" << m_type.to_string() << ", " << m_rd->to_string() << ":"
137     << m_prefix.to_string();
138   if (m_recirc)
139     s << ", " << m_recirc->to_string();
140   if (m_epg)
141     s << ", " << m_epg->to_string();
142
143   s << "]";
144
145   return (s.str());
146 }
147
148 void
149 gbp_subnet::update(const gbp_subnet& r)
150 {
151   if (rc_t::OK != m_hw.rc()) {
152     HW::enqueue(new gbp_subnet_cmds::create_cmd(
153       m_hw, m_rd->id(), m_prefix, m_type,
154       (m_recirc ? m_recirc->handle() : handle_t::INVALID),
155       (m_epg ? m_epg->sclass() : m_sclass)));
156   } else {
157     if (m_type != r.m_type) {
158       m_epg = r.m_epg;
159       m_recirc = r.m_recirc;
160       m_type = r.m_type;
161
162       HW::enqueue(new gbp_subnet_cmds::create_cmd(
163         m_hw, m_rd->id(), m_prefix, m_type,
164         (m_recirc ? m_recirc->handle() : handle_t::INVALID),
165         (m_epg ? m_epg->sclass() : m_sclass)));
166     }
167   }
168 }
169
170 std::shared_ptr<gbp_subnet>
171 gbp_subnet::find_or_add(const gbp_subnet& temp)
172 {
173   return (m_db.find_or_add(temp.key(), temp));
174 }
175
176 std::shared_ptr<gbp_subnet>
177 gbp_subnet::find(const key_t& k)
178 {
179   return (m_db.find(k));
180 }
181
182 std::shared_ptr<gbp_subnet>
183 gbp_subnet::singular() const
184 {
185   return find_or_add(*this);
186 }
187
188 void
189 gbp_subnet::dump(std::ostream& os)
190 {
191   db_dump(m_db, os);
192 }
193
194 gbp_subnet::event_handler::event_handler()
195 {
196   OM::register_listener(this);
197   inspect::register_handler({ "gbp-subnet" }, "GBP Subnets", this);
198 }
199
200 void
201 gbp_subnet::event_handler::handle_replay()
202 {
203   m_db.replay();
204 }
205
206 void
207 gbp_subnet::event_handler::handle_populate(const client_db::key_t& key)
208 {
209   std::shared_ptr<gbp_subnet_cmds::dump_cmd> cmd =
210     std::make_shared<gbp_subnet_cmds::dump_cmd>();
211
212   HW::enqueue(cmd);
213   HW::write();
214
215   for (auto& record : *cmd) {
216     auto& payload = record.get_payload();
217
218     route::prefix_t pfx = from_api(payload.subnet.prefix);
219     std::shared_ptr<gbp_route_domain> rd =
220       gbp_route_domain::find(payload.subnet.rd_id);
221
222     if (rd) {
223       switch (payload.subnet.type) {
224         case GBP_API_SUBNET_TRANSPORT: {
225           gbp_subnet gs(*rd, pfx, type_t::TRANSPORT);
226           OM::commit(key, gs);
227           VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
228           break;
229         }
230         case GBP_API_SUBNET_STITCHED_INTERNAL: {
231           gbp_subnet gs(*rd, pfx, type_t::STITCHED_INTERNAL);
232           OM::commit(key, gs);
233           VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
234           break;
235         }
236         case GBP_API_SUBNET_L3_OUT: {
237           gbp_subnet gs(*rd, pfx, payload.subnet.sclass);
238           OM::commit(key, gs);
239           VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
240           break;
241         }
242         case GBP_API_SUBNET_STITCHED_EXTERNAL: {
243           std::shared_ptr<interface> itf =
244             interface::find(payload.subnet.sw_if_index);
245           std::shared_ptr<gbp_endpoint_group> epg =
246             gbp_endpoint_group::find(payload.subnet.sclass);
247
248           if (itf && epg) {
249             std::shared_ptr<gbp_recirc> recirc = gbp_recirc::find(itf->key());
250
251             if (recirc) {
252               gbp_subnet gs(*rd, pfx, *recirc, *epg);
253               OM::commit(key, gs);
254               VOM_LOG(log_level_t::DEBUG) << "read: " << gs.to_string();
255             }
256           }
257         }
258       }
259     }
260   }
261 }
262
263 dependency_t
264 gbp_subnet::event_handler::order() const
265 {
266   return (dependency_t::ENTRY);
267 }
268
269 void
270 gbp_subnet::event_handler::show(std::ostream& os)
271 {
272   db_dump(m_db, os);
273 }
274
275 std::ostream&
276 operator<<(std::ostream& os, const gbp_subnet::key_t& key)
277 {
278   os << "[" << key.first << ", " << key.second << "]";
279
280   return os;
281 }
282
283 } // namespace VOM
284
285 /*
286  * fd.io coding-style-patch-verification: ON
287  *
288  * Local Variables:
289  * eval: (c-set-style "mozilla")
290  * End:
291  */