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