blob: 0835cea5d4858eeba9dda408e73fce1604d4d692 [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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#ifndef __VOM_ACL_LIST_H__
17#define __VOM_ACL_LIST_H__
18
19#include <set>
20
21#include "vom/acl_l2_rule.hpp"
22#include "vom/acl_l3_rule.hpp"
23#include "vom/acl_types.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070024#include "vom/hw.hpp"
25#include "vom/inspect.hpp"
26#include "vom/om.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070027#include "vom/singular_db.hpp"
28
Neale Ranns812ed392017-10-16 04:20:13 -070029namespace VOM {
30namespace ACL {
31/**
32 * An ACL list comprises a set of match actions rules to be applied to
33 * packets.
34 * A list is bound to a given interface.
35 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070036template <typename RULE>
Neale Ranns812ed392017-10-16 04:20:13 -070037class list : public object_base
38{
39public:
40 /**
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
44 */
45 typedef std::string key_t;
46
47 /**
48 * The rule container type
49 */
50 typedef std::multiset<RULE> rules_t;
51
52 /**
53 * Construct a new object matching the desried state
54 */
55 list(const key_t& key)
56 : m_key(key)
57 {
58 }
59
60 list(const handle_t& hdl, const key_t& key)
61 : m_hdl(hdl)
62 , m_key(key)
63 {
64 }
65
66 list(const key_t& key, const rules_t& rules)
67 : m_key(key)
68 , m_rules(rules)
69 {
70 m_evh.order();
71 }
72
73 /**
74 * Copy Constructor
75 */
76 list(const list& o)
77 : m_hdl(o.m_hdl)
78 , m_key(o.m_key)
79 , m_rules(o.m_rules)
80 {
81 }
82
83 /**
84 * Destructor
85 */
86 ~list()
87 {
88 sweep();
89 m_db.release(m_key, this);
90 }
91
92 /**
93 * Return the 'sigular instance' of the ACL that matches this object
94 */
95 std::shared_ptr<list> singular() const { return find_or_add(*this); }
96
97 /**
98 * Dump all ACLs into the stream provided
99 */
100 static void dump(std::ostream& os) { m_db.dump(os); }
101
102 /**
103 * convert to string format for debug purposes
104 */
105 std::string to_string() const
106 {
107 std::ostringstream s;
108 s << "acl-list:[" << m_key << " " << m_hdl.to_string() << " rules:[";
109
110 for (auto rule : m_rules) {
111 s << rule.to_string() << " ";
112 }
113
114 s << "]]";
115
116 return (s.str());
117 }
118
119 /**
120 * Insert priority sorted a rule into the list
121 */
122 void insert(const RULE& rule) { m_rules.insert(rule); }
123
124 /**
125 * Remove a rule from the list
126 */
127 void remove(const RULE& rule) { m_rules.erase(rule); }
128
129 /**
130 * Return the VPP assign handle
131 */
132 const handle_t& handle() const { return m_hdl.data(); }
133
Neale Ranns812ed392017-10-16 04:20:13 -0700134 static std::shared_ptr<list> find(const handle_t& handle)
135 {
136 return (m_hdl_db[handle].lock());
137 }
138
139 static std::shared_ptr<list> find(const key_t& key)
140 {
141 return (m_db.find(key));
142 }
143
144 static void add(const handle_t& handle, std::shared_ptr<list> sp)
145 {
146 m_hdl_db[handle] = sp;
147 }
148
149 static void remove(const handle_t& handle) { m_hdl_db.erase(handle); }
150
151private:
152 /**
153 * Class definition for listeners to OM events
154 */
155 class event_handler : public OM::listener, public inspect::command_handler
156 {
157 public:
158 event_handler()
159 {
160 OM::register_listener(this);
161 inspect::register_handler({ "acl" }, "ACL lists", this);
162 }
163 virtual ~event_handler() = default;
164
165 /**
166 * Handle a populate event
167 */
168 void handle_populate(const client_db::key_t& key);
169
170 /**
171 * Handle a replay event
172 */
173 void handle_replay() { m_db.replay(); }
174
175 /**
176 * Show the object in the Singular DB
177 */
178 void show(std::ostream& os) { m_db.dump(os); }
179
180 /**
181 * Get the sortable Id of the listener
182 */
183 dependency_t order() const { return (dependency_t::ACL); }
184 };
185
186 /**
187 * event_handler to register with OM
188 */
189 static event_handler m_evh;
190
191 /**
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700192 * Enqueue commands to the VPP command Q for the update
Neale Ranns812ed392017-10-16 04:20:13 -0700193 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700194 void update(const list& obj);
Neale Ranns812ed392017-10-16 04:20:13 -0700195
196 /**
197 * HW assigned handle
198 */
199 HW::item<handle_t> m_hdl;
200
201 /**
202 * Find or add the sigular instance in the DB
203 */
204 static std::shared_ptr<list> find_or_add(const list& temp)
205 {
206 return (m_db.find_or_add(temp.m_key, temp));
207 }
208
209 /*
210 * It's the VOM::OM class that updates call update
211 */
212 friend class VOM::OM;
213
214 /**
215 * It's the VOM::singular_db class that calls replay()
216 */
217 friend class singular_db<key_t, list>;
218
219 /**
220 * Sweep/reap the object if still stale
221 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700222 void sweep(void);
Neale Ranns812ed392017-10-16 04:20:13 -0700223
224 /**
225 * Replay the objects state to HW
226 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700227 void replay(void);
Neale Ranns812ed392017-10-16 04:20:13 -0700228
229 /**
230 * A map of all ACL's against the client's key
231 */
232 static singular_db<key_t, list> m_db;
233
234 /**
235 * A map of all ACLs keyed against VPP's handle
236 */
237 static std::map<const handle_t, std::weak_ptr<list>> m_hdl_db;
238
239 /**
240 * The Key is a user defined identifer for this ACL
241 */
242 const key_t m_key;
243
244 /**
245 * A sorted list of the rules
246 */
247 rules_t m_rules;
248};
249
250/**
251 * Typedef the L3 ACL type
252 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700253typedef list<l3_rule> l3_list;
Neale Ranns812ed392017-10-16 04:20:13 -0700254
255/**
256 * Typedef the L2 ACL type
257 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700258typedef list<l2_rule> l2_list;
Neale Ranns812ed392017-10-16 04:20:13 -0700259
260/**
261 * Definition of the static singular_db for ACL Lists
262 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700263template <typename RULE>
264singular_db<typename ACL::list<RULE>::key_t, ACL::list<RULE>> list<RULE>::m_db;
Neale Ranns812ed392017-10-16 04:20:13 -0700265
266/**
267 * Definition of the static per-handle DB for ACL Lists
268 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700269template <typename RULE>
270std::map<const handle_t, std::weak_ptr<ACL::list<RULE>>> list<RULE>::m_hdl_db;
Neale Ranns812ed392017-10-16 04:20:13 -0700271
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700272template <typename RULE>
273typename ACL::list<RULE>::event_handler list<RULE>::m_evh;
Neale Ranns812ed392017-10-16 04:20:13 -0700274};
275};
276
277/*
278 * fd.io coding-style-patch-verification: ON
279 *
280 * Local Variables:
281 * eval: (c-set-style "mozilla")
282 * End:
283 */
284
285#endif