acl: revert acl: api cleanup
[vpp.git] / extras / vom / vom / acl_l3_list.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/acl_l3_list.hpp"
17 #include "vom/acl_list_cmds.hpp"
18 #include "vom/logger.hpp"
19 #include "vom/singular_db_funcs.hpp"
20
21 namespace VOM {
22 namespace ACL {
23
24 /**
25  * Definition of the static singular_db for ACL Lists
26  */
27 singular_db<l3_list::key_t, l3_list> l3_list::m_db;
28
29 /**
30  * Definition of the static per-handle DB for ACL Lists
31  */
32 std::map<handle_t, std::weak_ptr<l3_list>> l3_list::m_hdl_db;
33
34 l3_list::event_handler l3_list::m_evh;
35
36 l3_list::event_handler::event_handler()
37 {
38   OM::register_listener(this);
39   inspect::register_handler({ "l3-acl-list" }, "L3 ACL lists", this);
40 }
41
42 l3_list::l3_list(const key_t& key)
43   : m_hdl(handle_t::INVALID)
44   , m_key(key)
45 {}
46
47 l3_list::l3_list(const handle_t& hdl, const key_t& key)
48   : m_hdl(hdl)
49   , m_key(key)
50 {}
51
52 l3_list::l3_list(const key_t& key, const rules_t& rules)
53   : m_hdl(handle_t::INVALID)
54   , m_key(key)
55   , m_rules(rules)
56 {}
57
58 l3_list::l3_list(const l3_list& o)
59   : m_hdl(o.m_hdl)
60   , m_key(o.m_key)
61   , m_rules(o.m_rules)
62 {}
63
64 l3_list::~l3_list()
65 {
66   sweep();
67   m_db.release(m_key, this);
68 }
69
70 std::shared_ptr<l3_list>
71 l3_list::singular() const
72 {
73   return find_or_add(*this);
74 }
75
76 /**
77  * Dump all ACLs into the stream provided
78  */
79 void
80 l3_list::dump(std::ostream& os)
81 {
82   db_dump(m_db, os);
83 }
84
85 /**
86  * convert to string format for debug purposes
87  */
88 std::string
89 l3_list::to_string() const
90 {
91   std::ostringstream s;
92   s << "acl-list:[" << m_key << " " << m_hdl.to_string() << " rules:[";
93
94   for (auto rule : m_rules) {
95     s << rule.to_string() << " ";
96   }
97
98   s << "]]";
99
100   return (s.str());
101 }
102
103 void
104 l3_list::insert(const l3_rule& rule)
105 {
106   m_rules.insert(rule);
107 }
108
109 void
110 l3_list::remove(const l3_rule& rule)
111 {
112   m_rules.erase(rule);
113 }
114
115 const handle_t&
116 l3_list::handle() const
117 {
118   return (singular()->handle_i());
119 }
120
121 std::shared_ptr<l3_list>
122 l3_list::find(const handle_t& handle)
123 {
124   return (m_hdl_db[handle].lock());
125 }
126
127 std::shared_ptr<l3_list>
128 l3_list::find(const key_t& key)
129 {
130   return (m_db.find(key));
131 }
132
133 std::shared_ptr<l3_list>
134 l3_list::find_or_add(const l3_list& temp)
135 {
136   return (m_db.find_or_add(temp.key(), temp));
137 }
138
139 const handle_t&
140 l3_list::handle_i() const
141 {
142   return (m_hdl.data());
143 }
144
145 void
146 l3_list::add(const key_t& key, const HW::item<handle_t>& item)
147 {
148   std::shared_ptr<l3_list> sp = find(key);
149
150   if (sp && item) {
151     m_hdl_db[item.data()] = sp;
152   }
153 }
154
155 void
156 l3_list::remove(const HW::item<handle_t>& item)
157 {
158   m_hdl_db.erase(item.data());
159 }
160
161 const l3_list::key_t&
162 l3_list::key() const
163 {
164   return m_key;
165 }
166
167 const l3_list::rules_t&
168 l3_list::rules() const
169 {
170   return m_rules;
171 }
172
173 bool
174 l3_list::operator==(const l3_list& l) const
175 {
176   return (key() == l.key() && rules() == l.rules());
177 }
178
179 void
180 l3_list::event_handler::handle_populate(const client_db::key_t& key)
181 {
182   /*
183    * dump L3 ACLs Bridge domains
184    */
185   std::shared_ptr<list_cmds::l3_dump_cmd> cmd =
186     std::make_shared<list_cmds::l3_dump_cmd>();
187
188   HW::enqueue(cmd);
189   HW::write();
190
191   for (auto& record : *cmd) {
192     auto& payload = record.get_payload();
193
194     const handle_t hdl(payload.acl_index);
195     l3_list acl(hdl, std::string(reinterpret_cast<const char*>(payload.tag)));
196
197     for (unsigned int ii = 0; ii < payload.count; ii++) {
198       const route::prefix_t src(payload.r[ii].is_ipv6,
199                                 payload.r[ii].src_ip_addr,
200                                 payload.r[ii].src_ip_prefix_len);
201       const route::prefix_t dst(payload.r[ii].is_ipv6,
202                                 payload.r[ii].dst_ip_addr,
203                                 payload.r[ii].dst_ip_prefix_len);
204       l3_rule rule(ii, action_t::from_int(payload.r[ii].is_permit), src, dst);
205
206       rule.set_proto(payload.r[ii].proto);
207       rule.set_src_from_port(payload.r[ii].srcport_or_icmptype_first);
208       rule.set_src_to_port(payload.r[ii].srcport_or_icmptype_last);
209       rule.set_dst_from_port(payload.r[ii].dstport_or_icmpcode_first);
210       rule.set_dst_to_port(payload.r[ii].dstport_or_icmpcode_last);
211       rule.set_tcp_flags_mask(payload.r[ii].tcp_flags_mask);
212       rule.set_tcp_flags_value(payload.r[ii].tcp_flags_value);
213
214       acl.insert(rule);
215     }
216     VOM_LOG(log_level_t::DEBUG) << "dump: " << acl.to_string();
217
218     /*
219      * Write each of the discovered ACLs into the OM,
220      * but disable the HW Command q whilst we do, so that no
221      * commands are sent to VPP
222      */
223     OM::commit(key, acl);
224   }
225 }
226
227 void
228 l3_list::event_handler::show(std::ostream& os)
229 {
230   db_dump(m_db, os);
231 }
232
233 dependency_t
234 l3_list::event_handler::order() const
235 {
236   return (dependency_t::ACL);
237 }
238
239 void
240 l3_list::event_handler::handle_replay()
241 {
242   m_db.replay();
243 }
244
245 void
246 l3_list::update(const l3_list& obj)
247 {
248   /*
249    * always update the instance with the latest rule set
250    */
251   if (rc_t::OK != m_hdl.rc() || obj.m_rules != m_rules) {
252     HW::enqueue(new list_cmds::l3_update_cmd(m_hdl, m_key, m_rules));
253   }
254   /*
255    * We don't, can't, read the priority from VPP,
256    * so the is equals check above does not include the priorty.
257    * but we save it now.
258    */
259   m_rules = obj.m_rules;
260 }
261
262 /**
263  * Sweep/reap the object if still stale
264  */
265 void
266 l3_list::sweep(void)
267 {
268   if (m_hdl) {
269     HW::enqueue(new list_cmds::l3_delete_cmd(m_hdl));
270   }
271   HW::write();
272 }
273
274 /**
275  * Replay the objects state to HW
276  */
277 void
278 l3_list::replay(void)
279 {
280   if (m_hdl) {
281     m_hdl.data().reset();
282     HW::enqueue(new list_cmds::l3_update_cmd(m_hdl, m_key, m_rules));
283   }
284 }
285
286 }; // namespace ACL
287 }; // namespace VOM
288
289 /*
290  * fd.io coding-style-patch-verification: ON
291  *
292  * Local Variables:
293  * eval: (c-set-style "mozilla")
294  * End:
295  */