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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __VOM_ACL_LIST_H__
17 #define __VOM_ACL_LIST_H__
21 #include "vom/acl_l2_rule.hpp"
22 #include "vom/acl_l3_rule.hpp"
23 #include "vom/acl_types.hpp"
25 #include "vom/inspect.hpp"
27 #include "vom/singular_db.hpp"
32 * An ACL list comprises a set of match actions rules to be applied to
34 * A list is bound to a given interface.
36 template <typename RULE>
37 class list : public object_base
41 * The KEY can be used to uniquely identify the ACL.
42 * (other choices for keys, like the summation of the properties
43 * of the rules, are rather too cumbersome to use
45 typedef std::string key_t;
48 * The rule container type
50 typedef std::multiset<RULE> rules_t;
53 * Construct a new object matching the desried state
55 list(const key_t& key)
60 list(const handle_t& hdl, const key_t& key)
66 list(const key_t& key, const rules_t& rules)
89 m_db.release(m_key, this);
93 * Return the 'sigular instance' of the ACL that matches this object
95 std::shared_ptr<list> singular() const { return find_or_add(*this); }
98 * Dump all ACLs into the stream provided
100 static void dump(std::ostream& os) { m_db.dump(os); }
103 * convert to string format for debug purposes
105 std::string to_string() const
107 std::ostringstream s;
108 s << "acl-list:[" << m_key << " " << m_hdl.to_string() << " rules:[";
110 for (auto rule : m_rules) {
111 s << rule.to_string() << " ";
120 * Insert priority sorted a rule into the list
122 void insert(const RULE& rule) { m_rules.insert(rule); }
125 * Remove a rule from the list
127 void remove(const RULE& rule) { m_rules.erase(rule); }
130 * Return the VPP assign handle
132 const handle_t& handle() const { return m_hdl.data(); }
134 static std::shared_ptr<list> find(const handle_t& handle)
136 return (m_hdl_db[handle].lock());
139 static std::shared_ptr<list> find(const key_t& key)
141 return (m_db.find(key));
144 static void add(const handle_t& handle, std::shared_ptr<list> sp)
146 m_hdl_db[handle] = sp;
149 static void remove(const handle_t& handle) { m_hdl_db.erase(handle); }
153 * Class definition for listeners to OM events
155 class event_handler : public OM::listener, public inspect::command_handler
160 OM::register_listener(this);
161 inspect::register_handler({ "acl" }, "ACL lists", this);
163 virtual ~event_handler() = default;
166 * Handle a populate event
168 void handle_populate(const client_db::key_t& key);
171 * Handle a replay event
173 void handle_replay() { m_db.replay(); }
176 * Show the object in the Singular DB
178 void show(std::ostream& os) { m_db.dump(os); }
181 * Get the sortable Id of the listener
183 dependency_t order() const { return (dependency_t::ACL); }
187 * event_handler to register with OM
189 static event_handler m_evh;
192 * Enqueue commands to the VPP command Q for the update
194 void update(const list& obj);
199 HW::item<handle_t> m_hdl;
202 * Find or add the sigular instance in the DB
204 static std::shared_ptr<list> find_or_add(const list& temp)
206 return (m_db.find_or_add(temp.m_key, temp));
210 * It's the VOM::OM class that updates call update
212 friend class VOM::OM;
215 * It's the VOM::singular_db class that calls replay()
217 friend class singular_db<key_t, list>;
220 * Sweep/reap the object if still stale
225 * Replay the objects state to HW
230 * A map of all ACL's against the client's key
232 static singular_db<key_t, list> m_db;
235 * A map of all ACLs keyed against VPP's handle
237 static std::map<const handle_t, std::weak_ptr<list>> m_hdl_db;
240 * The Key is a user defined identifer for this ACL
245 * A sorted list of the rules
251 * Typedef the L3 ACL type
253 typedef list<l3_rule> l3_list;
256 * Typedef the L2 ACL type
258 typedef list<l2_rule> l2_list;
261 * Definition of the static singular_db for ACL Lists
263 template <typename RULE>
264 singular_db<typename ACL::list<RULE>::key_t, ACL::list<RULE>> list<RULE>::m_db;
267 * Definition of the static per-handle DB for ACL Lists
269 template <typename RULE>
270 std::map<const handle_t, std::weak_ptr<ACL::list<RULE>>> list<RULE>::m_hdl_db;
272 template <typename RULE>
273 typename ACL::list<RULE>::event_handler list<RULE>::m_evh;
278 * fd.io coding-style-patch-verification: ON
281 * eval: (c-set-style "mozilla")