acl: revert acl: api cleanup
[vpp.git] / extras / vom / vom / acl_l2_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_l2_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<l2_list::key_t, l2_list> l2_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<l2_list>> l2_list::m_hdl_db;
33
34 l2_list::event_handler l2_list::m_evh;
35
36 l2_list::event_handler::event_handler()
37 {
38   OM::register_listener(this);
39   inspect::register_handler({ "l2-acl-list" }, "L2 ACL lists", this);
40 }
41
42 l2_list::l2_list(const key_t& key)
43   : m_hdl(handle_t::INVALID)
44   , m_key(key)
45 {}
46
47 l2_list::l2_list(const handle_t& hdl, const key_t& key)
48   : m_hdl(hdl)
49   , m_key(key)
50 {}
51
52 l2_list::l2_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 l2_list::l2_list(const l2_list& o)
59   : m_hdl(o.m_hdl)
60   , m_key(o.m_key)
61   , m_rules(o.m_rules)
62 {}
63
64 l2_list::~l2_list()
65 {
66   sweep();
67   m_db.release(m_key, this);
68 }
69
70 std::shared_ptr<l2_list>
71 l2_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 l2_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 l2_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 l2_list::insert(const l2_rule& rule)
105 {
106   m_rules.insert(rule);
107 }
108
109 void
110 l2_list::remove(const l2_rule& rule)
111 {
112   m_rules.erase(rule);
113 }
114
115 const handle_t&
116 l2_list::handle() const
117 {
118   return (singular()->handle_i());
119 }
120
121 std::shared_ptr<l2_list>
122 l2_list::find(const handle_t& handle)
123 {
124   return (m_hdl_db[handle].lock());
125 }
126
127 std::shared_ptr<l2_list>
128 l2_list::find(const key_t& key)
129 {
130   return (m_db.find(key));
131 }
132
133 std::shared_ptr<l2_list>
134 l2_list::find_or_add(const l2_list& temp)
135 {
136   return (m_db.find_or_add(temp.key(), temp));
137 }
138
139 const handle_t&
140 l2_list::handle_i() const
141 {
142   return (m_hdl.data());
143 }
144
145 void
146 l2_list::add(const key_t& key, const HW::item<handle_t>& item)
147 {
148   std::shared_ptr<l2_list> sp = find(key);
149
150   if (sp && item) {
151     m_hdl_db[item.data()] = sp;
152   }
153 }
154
155 void
156 l2_list::remove(const HW::item<handle_t>& item)
157 {
158   m_hdl_db.erase(item.data());
159 }
160
161 const l2_list::key_t&
162 l2_list::key() const
163 {
164   return m_key;
165 }
166
167 const l2_list::rules_t&
168 l2_list::rules() const
169 {
170   return m_rules;
171 }
172
173 bool
174 l2_list::operator==(const l2_list& l) const
175 {
176   return (key() == l.key() && rules() == l.rules());
177 }
178
179 void
180 l2_list::event_handler::handle_populate(const client_db::key_t& key)
181 {
182   /*
183    * dump L2 ACLs
184    */
185   std::shared_ptr<list_cmds::l2_dump_cmd> cmd =
186     std::make_shared<list_cmds::l2_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     l2_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 pfx(payload.r[ii].is_ipv6,
199                                 payload.r[ii].src_ip_addr,
200                                 payload.r[ii].src_ip_prefix_len);
201       l2_rule rule(ii,
202                    action_t::from_int(payload.r[ii].is_permit),
203                    pfx,
204                    { payload.r[ii].src_mac },
205                    { payload.r[ii].src_mac_mask });
206
207       acl.insert(rule);
208     }
209     VOM_LOG(log_level_t::DEBUG) << "dump: " << acl.to_string();
210
211     /*
212      * Write each of the discovered ACLs into the OM,
213      * but disable the HW Command q whilst we do, so that no
214      * commands are sent to VPP
215      */
216     OM::commit(key, acl);
217   }
218 }
219
220 void
221 l2_list::event_handler::show(std::ostream& os)
222 {
223   db_dump(m_db, os);
224 }
225
226 dependency_t
227 l2_list::event_handler::order() const
228 {
229   return (dependency_t::ACL);
230 }
231
232 void
233 l2_list::event_handler::handle_replay()
234 {
235   m_db.replay();
236 }
237
238 void
239 l2_list::update(const l2_list& obj)
240 {
241   /*
242    * always update the instance with the latest rule set
243    */
244   if (rc_t::OK != m_hdl.rc() || obj.m_rules != m_rules) {
245     HW::enqueue(new list_cmds::l2_update_cmd(m_hdl, m_key, m_rules));
246   }
247   /*
248    * We don't, can't, read the priority from VPP,
249    * so the is equals check above does not include the priorty.
250    * but we save it now.
251    */
252   m_rules = obj.m_rules;
253 }
254
255 void
256 l2_list::sweep(void)
257 {
258   if (m_hdl) {
259     HW::enqueue(new list_cmds::l2_delete_cmd(m_hdl));
260   }
261   HW::write();
262 }
263
264 void
265 l2_list::replay(void)
266 {
267   if (m_hdl) {
268     m_hdl.data().reset();
269     HW::enqueue(new list_cmds::l2_update_cmd(m_hdl, m_key, m_rules));
270   }
271 }
272
273 }; // namespace ACL
274 }; // namespace VOM
275
276 /*
277  * fd.io coding-style-patch-verification: ON
278  *
279  * Local Variables:
280  * eval: (c-set-style "mozilla")
281  * End:
282  */