blob: faf9b245cae6d9b7c668a6d7cc7ac3c88f3c1994 [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_BINDING_H__
17#define __VOM_ACL_BINDING_H__
18
19#include <ostream>
20
21#include "vom/acl_list.hpp"
22#include "vom/acl_types.hpp"
23#include "vom/hw.hpp"
24#include "vom/inspect.hpp"
25#include "vom/interface.hpp"
26#include "vom/object_base.hpp"
27#include "vom/om.hpp"
28#include "vom/rpc_cmd.hpp"
29#include "vom/singular_db.hpp"
30
31namespace VOM {
32namespace ACL {
33/**
34 * A binding between an ACL and an interface.
35 * A representation of the application of the ACL to the interface.
36 */
37template <typename LIST, typename BIND, typename DUMP>
38class binding : public object_base
39{
40public:
41 /**
42 * The key for a binding is the direction and the interface
43 */
44 typedef std::pair<direction_t, interface::key_type> key_t;
45
46 /**
47 * Construct a new object matching the desried state
48 */
49 binding(const direction_t& direction, const interface& itf, const LIST& acl)
50 : m_direction(direction)
51 , m_itf(itf.singular())
52 , m_acl(acl.singular())
53 , m_binding(0)
54 {
55 m_evh.order();
56 }
57
58 /**
59 * Copy Constructor
60 */
61 binding(const binding& o)
62 : m_direction(o.m_direction)
63 , m_itf(o.m_itf)
64 , m_acl(o.m_acl)
65 , m_binding(0)
66 {
67 }
68
69 /**
70 * Destructor
71 */
72 ~binding()
73 {
74 sweep();
75 m_db.release(std::make_pair(m_direction, m_itf->key()), this);
76 }
77
78 /**
79 * Return the 'singular instance' of the L2 config that matches this
80 * object
81 */
82 std::shared_ptr<binding> singular() const { return find_or_add(*this); }
83
84 /**
85 * convert to string format for debug purposes
86 */
87 std::string to_string() const
88 {
89 std::ostringstream s;
90 s << "acl-binding:[" << m_direction.to_string() << " " << m_itf->to_string()
91 << " " << m_acl->to_string() << " " << m_binding.to_string() << "]";
92
93 return (s.str());
94 }
95
96 /**
97 * Dump all bindings into the stream provided
98 */
99 static void dump(std::ostream& os) { m_db.dump(os); }
100
101 /**
102 * A command class that binds the ACL to the interface
103 */
104 class bind_cmd : public rpc_cmd<HW::item<bool>, rc_t, BIND>
105 {
106 public:
107 /**
108 * Constructor
109 */
110 bind_cmd(HW::item<bool>& item,
111 const direction_t& direction,
112 const handle_t& itf,
113 const handle_t& acl)
114 : rpc_cmd<HW::item<bool>, rc_t, BIND>(item)
115 , m_direction(direction)
116 , m_itf(itf)
117 , m_acl(acl)
118 {
119 }
120
121 /**
122 * Issue the command to VPP/HW
123 */
124 rc_t issue(connection& con);
125
126 /**
127 * convert to string format for debug purposes
128 */
129 std::string to_string() const
130 {
131 std::ostringstream s;
132 s << "acl-bind:[" << m_direction.to_string()
133 << " itf:" << m_itf.to_string() << " acl:" << m_acl.to_string() << "]";
134
135 return (s.str());
136 }
137
138 /**
139 * Comparison operator - only used for UT
140 */
141 bool operator==(const bind_cmd& other) const
142 {
143 return ((m_itf == other.m_itf) && (m_acl == m_acl));
144 }
145
146 private:
147 /**
148 * The direction of the binding
149 */
150 const direction_t m_direction;
151
152 /**
153 * The interface to bind to
154 */
155 const handle_t m_itf;
156
157 /**
158 * The ACL to bind
159 */
160 const handle_t m_acl;
161 };
162
163 /**
164 * A command class that binds the ACL to the interface
165 */
166 class unbind_cmd : public rpc_cmd<HW::item<bool>, rc_t, BIND>
167 {
168 public:
169 /**
170 * Constructor
171 */
172 unbind_cmd(HW::item<bool>& item,
173 const direction_t& direction,
174 const handle_t& itf,
175 const handle_t& acl)
176 : rpc_cmd<HW::item<bool>, rc_t, BIND>(item)
177 , m_direction(direction)
178 , m_itf(itf)
179 , m_acl(acl)
180 {
181 }
182
183 /**
184 * Issue the command to VPP/HW
185 */
186 rc_t issue(connection& con);
187
188 /**
189 * convert to string format for debug purposes
190 */
191 std::string to_string() const
192 {
193 std::ostringstream s;
194 s << "acl-unbind:[" << m_direction.to_string()
195 << " itf:" << m_itf.to_string() << " acl:" << m_acl.to_string() << "]";
196
197 return (s.str());
198 }
199
200 /**
201 * Comparison operator - only used for UT
202 */
203 bool operator==(const unbind_cmd& other) const
204 {
205 return ((m_itf == other.m_itf) && (m_acl == m_acl));
206 }
207
208 private:
209 /**
210 * The direction of the binding
211 */
212 const direction_t m_direction;
213
214 /**
215 * The interface to bind to
216 */
217 const handle_t m_itf;
218
219 /**
220 * The ACL to bind
221 */
222 const handle_t m_acl;
223 };
224
225 /**
226 * A cmd class that Dumps all the ACLs
227 */
228 class dump_cmd : public VOM::dump_cmd<DUMP>
229 {
230 public:
231 /**
232 * Constructor
233 */
234 dump_cmd() = default;
235 dump_cmd(const dump_cmd& d) = default;
236
237 /**
238 * Issue the command to VPP/HW
239 */
240 rc_t issue(connection& con);
241
242 /**
243 * convert to string format for debug purposes
244 */
245 std::string to_string() const { return ("acl-bind-dump"); }
246
247 private:
248 /**
249 * HW reutrn code
250 */
251 HW::item<bool> item;
252 };
253
254private:
255 /**
256 * Class definition for listeners to OM events
257 */
258 class event_handler : public OM::listener, public inspect::command_handler
259 {
260 public:
261 event_handler()
262 {
263 OM::register_listener(this);
264 inspect::register_handler({ "acl-binding" }, "ACL bindings", this);
265 }
266 virtual ~event_handler() = default;
267
268 /**
269 * Handle a populate event
270 */
271 void handle_populate(const client_db::key_t& key);
272
273 /**
274 * Handle a replay event
275 */
276 void handle_replay() { m_db.replay(); }
277
278 /**
279 * Show the object in the Singular DB
280 */
281 void show(std::ostream& os) { m_db.dump(os); }
282
283 /**
284 * Get the sortable Id of the listener
285 */
286 dependency_t order() const { return (dependency_t::BINDING); }
287 };
288
289 /**
290 * event_handler to register with OM
291 */
292 static event_handler m_evh;
293
294 /**
295 * Enquue commonds to the VPP command Q for the update
296 */
297 void update(const binding& obj)
298 {
299 if (!m_binding) {
300 HW::enqueue(
301 new bind_cmd(m_binding, m_direction, m_itf->handle(), m_acl->handle()));
302 }
303 HW::write();
304 }
305
306 /**
307 * Find or Add the instance in the DB
308 */
309 static std::shared_ptr<binding> find_or_add(const binding& temp)
310 {
311 return (m_db.find_or_add(
312 std::make_pair(temp.m_direction, temp.m_itf->key()), temp));
313 }
314
315 /*
316 * It's the OM class that calls singular()
317 */
318 friend class VOM::OM;
319
320 /**
321 * It's the singular_db class that calls replay()
322 */
323 friend class singular_db<key_t, binding>;
324
325 /**
326 * Sweep/reap the object if still stale
327 */
328 void sweep(void)
329 {
330 if (m_binding) {
331 HW::enqueue(new unbind_cmd(m_binding, m_direction, m_itf->handle(),
332 m_acl->handle()));
333 }
334 HW::write();
335 }
336
337 /**
338 * Replay the objects state to HW
339 */
340 void replay(void)
341 {
342 if (m_binding) {
343 HW::enqueue(
344 new bind_cmd(m_binding, m_direction, m_itf->handle(), m_acl->handle()));
345 }
346 }
347
348 /**
349 * The direction the of the packets on which to apply the ACL
350 * input or output
351 */
352 const direction_t m_direction;
353
354 /**
355 * A reference counting pointer the interface that this L3 layer
356 * represents. By holding the reference here, we can guarantee that
357 * this object will outlive the interface
358 */
359 const std::shared_ptr<interface> m_itf;
360
361 /**
362 * A reference counting pointer the ACL that this
363 * interface is bound to. By holding the reference here, we can
364 * guarantee that this object will outlive the BD.
365 */
366 const std::shared_ptr<LIST> m_acl;
367
368 /**
369 * HW configuration for the binding. The bool representing the
370 * do/don't bind.
371 */
372 HW::item<bool> m_binding;
373
374 /**
375 * A map of all L2 interfaces key against the interface's handle_t
376 */
377 static singular_db<key_t, binding> m_db;
378};
379
380/**
381 * Typedef the L3 binding type
382 */
383typedef binding<l3_list,
384 vapi::Acl_interface_add_del,
385 vapi::Acl_interface_list_dump>
386 l3_binding;
387
388/**
389 * Typedef the L2 binding type
390 */
391typedef binding<l2_list,
392 vapi::Macip_acl_interface_add_del,
393 vapi::Macip_acl_interface_list_dump>
394 l2_binding;
395
396/**
397 * Definition of the static Singular DB for ACL bindings
398 */
399template <typename LIST, typename BIND, typename DUMP>
400singular_db<typename ACL::binding<LIST, BIND, DUMP>::key_t,
401 ACL::binding<LIST, BIND, DUMP>>
402 binding<LIST, BIND, DUMP>::m_db;
403
404template <typename LIST, typename BIND, typename DUMP>
405typename ACL::binding<LIST, BIND, DUMP>::event_handler
406 binding<LIST, BIND, DUMP>::m_evh;
407};
408
409std::ostream& operator<<(
410 std::ostream& os,
411 const std::pair<direction_t, interface::key_type>& key);
412};
413
414/*
415 * fd.io coding-style-patch-verification: ON
416 *
417 * Local Variables:
418 * eval: (c-set-style "mozilla")
419 * End:
420 */
421
422#endif