blob: 89db0eea219125dd5bc4ed1ddf0aa96e37e4e7e8 [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"
Neale Ranns812ed392017-10-16 04:20:13 -070028#include "vom/singular_db.hpp"
Neale Ranns756cd942018-04-06 09:18:11 -070029#include "vom/singular_db_funcs.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070030
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 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070037template <typename LIST>
Neale Ranns812ed392017-10-16 04:20:13 -070038class binding : public object_base
39{
40public:
41 /**
42 * The key for a binding is the direction and the interface
43 */
Neale Rannsfd920602017-11-23 12:15:00 -080044 typedef std::pair<direction_t, interface::key_t> key_t;
Neale Ranns812ed392017-10-16 04:20:13 -070045
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())
Mohsin Kazmib5eb3b12018-02-26 18:36:17 +010053 , m_binding(false)
Neale Ranns812ed392017-10-16 04:20:13 -070054 {
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)
Mohsin Kazmib5eb3b12018-02-26 18:36:17 +010065 , m_binding(o.m_binding)
Neale Ranns812ed392017-10-16 04:20:13 -070066 {
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
Mohsin Kazmib5eb3b12018-02-26 18:36:17 +0100101 static dependency_t order() { return m_evh.order(); }
102
Neale Ranns812ed392017-10-16 04:20:13 -0700103private:
104 /**
105 * Class definition for listeners to OM events
106 */
107 class event_handler : public OM::listener, public inspect::command_handler
108 {
109 public:
Mohsin Kazmib5eb3b12018-02-26 18:36:17 +0100110 event_handler();
111
Neale Ranns812ed392017-10-16 04:20:13 -0700112 virtual ~event_handler() = default;
113
114 /**
115 * Handle a populate event
116 */
117 void handle_populate(const client_db::key_t& key);
118
119 /**
120 * Handle a replay event
121 */
122 void handle_replay() { m_db.replay(); }
123
124 /**
125 * Show the object in the Singular DB
126 */
Neale Ranns756cd942018-04-06 09:18:11 -0700127 void show(std::ostream& os) { db_dump(m_db, os); }
Neale Ranns812ed392017-10-16 04:20:13 -0700128
129 /**
130 * Get the sortable Id of the listener
131 */
132 dependency_t order() const { return (dependency_t::BINDING); }
133 };
134
135 /**
136 * event_handler to register with OM
137 */
138 static event_handler m_evh;
139
140 /**
141 * Enquue commonds to the VPP command Q for the update
142 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700143 void update(const binding& obj);
Neale Ranns812ed392017-10-16 04:20:13 -0700144
145 /**
146 * Find or Add the instance in the DB
147 */
148 static std::shared_ptr<binding> find_or_add(const binding& temp)
149 {
150 return (m_db.find_or_add(
151 std::make_pair(temp.m_direction, temp.m_itf->key()), temp));
152 }
153
154 /*
155 * It's the OM class that calls singular()
156 */
157 friend class VOM::OM;
158
159 /**
160 * It's the singular_db class that calls replay()
161 */
162 friend class singular_db<key_t, binding>;
163
164 /**
165 * Sweep/reap the object if still stale
166 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700167 void sweep(void);
Neale Ranns812ed392017-10-16 04:20:13 -0700168
169 /**
170 * Replay the objects state to HW
171 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700172 void replay(void);
Neale Ranns812ed392017-10-16 04:20:13 -0700173
174 /**
175 * The direction the of the packets on which to apply the ACL
176 * input or output
177 */
178 const direction_t m_direction;
179
180 /**
181 * A reference counting pointer the interface that this L3 layer
182 * represents. By holding the reference here, we can guarantee that
183 * this object will outlive the interface
184 */
185 const std::shared_ptr<interface> m_itf;
186
187 /**
188 * A reference counting pointer the ACL that this
189 * interface is bound to. By holding the reference here, we can
190 * guarantee that this object will outlive the BD.
191 */
192 const std::shared_ptr<LIST> m_acl;
193
194 /**
195 * HW configuration for the binding. The bool representing the
196 * do/don't bind.
197 */
198 HW::item<bool> m_binding;
199
200 /**
201 * A map of all L2 interfaces key against the interface's handle_t
202 */
203 static singular_db<key_t, binding> m_db;
204};
205
206/**
207 * Typedef the L3 binding type
208 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700209typedef binding<l3_list> l3_binding;
Neale Ranns812ed392017-10-16 04:20:13 -0700210
211/**
212 * Typedef the L2 binding type
213 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700214typedef binding<l2_list> l2_binding;
Neale Ranns812ed392017-10-16 04:20:13 -0700215
216/**
217 * Definition of the static Singular DB for ACL bindings
218 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700219template <typename LIST>
220singular_db<typename ACL::binding<LIST>::key_t, ACL::binding<LIST>>
221 binding<LIST>::m_db;
Neale Ranns812ed392017-10-16 04:20:13 -0700222
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700223template <typename LIST>
224typename ACL::binding<LIST>::event_handler binding<LIST>::m_evh;
Mohsin Kazmib5eb3b12018-02-26 18:36:17 +0100225
226namespace {
227const static dependency_t __attribute__((unused)) l2o = l2_binding::order();
228const static dependency_t __attribute__((unused)) l3o = l3_binding::order();
229};
Neale Ranns812ed392017-10-16 04:20:13 -0700230};
231
Neale Rannsfd920602017-11-23 12:15:00 -0800232std::ostream& operator<<(std::ostream& os,
233 const std::pair<direction_t, interface::key_t>& key);
Neale Ranns812ed392017-10-16 04:20:13 -0700234};
235
236/*
237 * fd.io coding-style-patch-verification: ON
238 *
239 * Local Variables:
240 * eval: (c-set-style "mozilla")
241 * End:
242 */
243
244#endif