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