vxlan: vxlan/vxlan.api 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
48 l2_list::l2_list(const handle_t& hdl, const key_t& key)
49   : m_hdl(hdl)
50   , m_key(key)
51 {
52 }
53
54 l2_list::l2_list(const key_t& key, const rules_t& rules)
55   : m_hdl(handle_t::INVALID)
56   , m_key(key)
57   , m_rules(rules)
58 {
59 }
60
61 l2_list::l2_list(const l2_list& o)
62   : m_hdl(o.m_hdl)
63   , m_key(o.m_key)
64   , m_rules(o.m_rules)
65 {
66 }
67
68 l2_list::~l2_list()
69 {
70   sweep();
71   m_db.release(m_key, this);
72 }
73
74 std::shared_ptr<l2_list>
75 l2_list::singular() const
76 {
77   return find_or_add(*this);
78 }
79
80 /**
81  * Dump all ACLs into the stream provided
82  */
83 void
84 l2_list::dump(std::ostream& os)
85 {
86   db_dump(m_db, os);
87 }
88
89 /**
90  * convert to string format for debug purposes
91  */
92 std::string
93 l2_list::to_string() const
94 {
95   std::ostringstream s;
96   s << "acl-list:[" << m_key << " " << m_hdl.to_string() << " rules:[";
97
98   for (auto rule : m_rules) {
99     s << rule.to_string() << " ";
100   }
101
102   s << "]]";
103
104   return (s.str());
105 }
106
107 void
108 l2_list::insert(const l2_rule& rule)
109 {
110   m_rules.insert(rule);
111 }
112
113 void
114 l2_list::remove(const l2_rule& rule)
115 {
116   m_rules.erase(rule);
117 }
118
119 const handle_t&
120 l2_list::handle() const
121 {
122   return (singular()->handle_i());
123 }
124
125 std::shared_ptr<l2_list>
126 l2_list::find(const handle_t& handle)
127 {
128   return (m_hdl_db[handle].lock());
129 }
130
131 std::shared_ptr<l2_list>
132 l2_list::find(const key_t& key)
133 {
134   return (m_db.find(key));
135 }
136
137 std::shared_ptr<l2_list>
138 l2_list::find_or_add(const l2_list& temp)
139 {
140   return (m_db.find_or_add(temp.key(), temp));
141 }
142
143 const handle_t&
144 l2_list::handle_i() const
145 {
146   return (m_hdl.data());
147 }
148
149 void
150 l2_list::add(const key_t& key, const HW::item<handle_t>& item)
151 {
152   std::shared_ptr<l2_list> sp = find(key);
153
154   if (sp && item) {
155     m_hdl_db[item.data()] = sp;
156   }
157 }
158
159 void
160 l2_list::remove(const HW::item<handle_t>& item)
161 {
162   m_hdl_db.erase(item.data());
163 }
164
165 const l2_list::key_t&
166 l2_list::key() const
167 {
168   return m_key;
169 }
170
171 const l2_list::rules_t&
172 l2_list::rules() const
173 {
174   return m_rules;
175 }
176
177 bool
178 l2_list::operator==(const l2_list& l) const
179 {
180   return (key() == l.key() && rules() == l.rules());
181 }
182
183 void
184 l2_list::event_handler::handle_populate(const client_db::key_t& key)
185 {
186   /*
187    * dump L2 ACLs
188    */
189   std::shared_ptr<list_cmds::l2_dump_cmd> cmd =
190     std::make_shared<list_cmds::l2_dump_cmd>();
191
192   HW::enqueue(cmd);
193   HW::write();
194
195   for (auto& record : *cmd) {
196     auto& payload = record.get_payload();
197
198     const handle_t hdl(payload.acl_index);
199     l2_list acl(hdl, std::string(reinterpret_cast<const char*>(payload.tag)));
200
201     for (unsigned int ii = 0; ii < payload.count; ii++) {
202       const route::prefix_t pfx(payload.r[ii].is_ipv6,
203                                 payload.r[ii].src_ip_addr,
204                                 payload.r[ii].src_ip_prefix_len);
205       l2_rule rule(ii, action_t::from_int(payload.r[ii].is_permit), pfx,
206                    { payload.r[ii].src_mac }, { payload.r[ii].src_mac_mask });
207
208       acl.insert(rule);
209     }
210     VOM_LOG(log_level_t::DEBUG) << "dump: " << acl.to_string();
211
212     /*
213      * Write each of the discovered ACLs into the OM,
214      * but disable the HW Command q whilst we do, so that no
215      * commands are sent to VPP
216      */
217     OM::commit(key, acl);
218   }
219 }
220
221 void
222 l2_list::event_handler::show(std::ostream& os)
223 {
224   db_dump(m_db, os);
225 }
226
227 dependency_t
228 l2_list::event_handler::order() const
229 {
230   return (dependency_t::ACL);
231 }
232
233 void
234 l2_list::event_handler::handle_replay()
235 {
236   m_db.replay();
237 }
238
239 void
240 l2_list::update(const l2_list& obj)
241 {
242   /*
243    * always update the instance with the latest rule set
244    */
245   if (rc_t::OK != m_hdl.rc() || obj.m_rules != m_rules) {
246     HW::enqueue(new list_cmds::l2_update_cmd(m_hdl, m_key, m_rules));
247   }
248   /*
249    * We don't, can't, read the priority from VPP,
250    * so the is equals check above does not include the priorty.
251    * but we save it now.
252    */
253   m_rules = obj.m_rules;
254 }
255
256 void
257 l2_list::sweep(void)
258 {
259   if (m_hdl) {
260     HW::enqueue(new list_cmds::l2_delete_cmd(m_hdl));
261   }
262   HW::write();
263 }
264
265 void
266 l2_list::replay(void)
267 {
268   if (m_hdl) {
269     m_hdl.data().reset();
270     HW::enqueue(new list_cmds::l2_update_cmd(m_hdl, m_key, m_rules));
271   }
272 }
273
274 }; // namespace ACL
275 }; // namespace VOM
276
277 /*
278  * fd.io coding-style-patch-verification: ON
279  *
280  * Local Variables:
281  * eval: (c-set-style "mozilla")
282  * End:
283  */