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