6e994e4d2a58416138e2a92875d441ba034598fc
[vpp.git] / src / vpp-api / vom / acl_binding.hpp
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 #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/singular_db.hpp"
29
30 namespace VOM {
31 namespace ACL {
32 /**
33  * A binding between an ACL and an interface.
34  * A representation of the application of the ACL to the interface.
35  */
36 template <typename LIST>
37 class binding : public object_base
38 {
39 public:
40   /**
41    * The key for a binding is the direction and the interface
42    */
43   typedef std::pair<direction_t, interface::key_t> key_t;
44
45   /**
46    * Construct a new object matching the desried state
47    */
48   binding(const direction_t& direction, const interface& itf, const LIST& acl)
49     : m_direction(direction)
50     , m_itf(itf.singular())
51     , m_acl(acl.singular())
52     , m_binding(false)
53   {
54     m_evh.order();
55   }
56
57   /**
58    * Copy Constructor
59    */
60   binding(const binding& o)
61     : m_direction(o.m_direction)
62     , m_itf(o.m_itf)
63     , m_acl(o.m_acl)
64     , m_binding(o.m_binding)
65   {
66   }
67
68   /**
69    * Destructor
70    */
71   ~binding()
72   {
73     sweep();
74     m_db.release(std::make_pair(m_direction, m_itf->key()), this);
75   }
76
77   /**
78    * Return the 'singular instance' of the L2 config that matches this
79    * object
80    */
81   std::shared_ptr<binding> singular() const { return find_or_add(*this); }
82
83   /**
84    * convert to string format for debug purposes
85    */
86   std::string to_string() const
87   {
88     std::ostringstream s;
89     s << "acl-binding:[" << m_direction.to_string() << " " << m_itf->to_string()
90       << " " << m_acl->to_string() << " " << m_binding.to_string() << "]";
91
92     return (s.str());
93   }
94
95   /**
96    * Dump all bindings into the stream provided
97    */
98   static void dump(std::ostream& os) { m_db.dump(os); }
99
100   static dependency_t order() { return m_evh.order(); }
101
102 private:
103   /**
104    * Class definition for listeners to OM events
105    */
106   class event_handler : public OM::listener, public inspect::command_handler
107   {
108   public:
109     event_handler();
110
111     virtual ~event_handler() = default;
112
113     /**
114      * Handle a populate event
115      */
116     void handle_populate(const client_db::key_t& key);
117
118     /**
119      * Handle a replay event
120      */
121     void handle_replay() { m_db.replay(); }
122
123     /**
124      * Show the object in the Singular DB
125      */
126     void show(std::ostream& os) { m_db.dump(os); }
127
128     /**
129      * Get the sortable Id of the listener
130      */
131     dependency_t order() const { return (dependency_t::BINDING); }
132   };
133
134   /**
135    * event_handler to register with OM
136    */
137   static event_handler m_evh;
138
139   /**
140    * Enquue commonds to the VPP command Q for the update
141    */
142   void update(const binding& obj);
143
144   /**
145    * Find or Add the instance in the DB
146    */
147   static std::shared_ptr<binding> find_or_add(const binding& temp)
148   {
149     return (m_db.find_or_add(
150       std::make_pair(temp.m_direction, temp.m_itf->key()), temp));
151   }
152
153   /*
154    * It's the OM class that calls singular()
155    */
156   friend class VOM::OM;
157
158   /**
159    * It's the singular_db class that calls replay()
160    */
161   friend class singular_db<key_t, binding>;
162
163   /**
164    * Sweep/reap the object if still stale
165    */
166   void sweep(void);
167
168   /**
169    * Replay the objects state to HW
170    */
171   void replay(void);
172
173   /**
174    * The direction the of the packets on which to apply the ACL
175    * input or output
176    */
177   const direction_t m_direction;
178
179   /**
180    * A reference counting pointer the interface that this L3 layer
181    * represents. By holding the reference here, we can guarantee that
182    * this object will outlive the interface
183    */
184   const std::shared_ptr<interface> m_itf;
185
186   /**
187    * A reference counting pointer the ACL that this
188    * interface is bound to. By holding the reference here, we can
189    * guarantee that this object will outlive the BD.
190    */
191   const std::shared_ptr<LIST> m_acl;
192
193   /**
194    * HW configuration for the binding. The bool representing the
195    * do/don't bind.
196    */
197   HW::item<bool> m_binding;
198
199   /**
200    * A map of all L2 interfaces key against the interface's handle_t
201    */
202   static singular_db<key_t, binding> m_db;
203 };
204
205 /**
206  * Typedef the L3 binding type
207  */
208 typedef binding<l3_list> l3_binding;
209
210 /**
211  * Typedef the L2 binding type
212  */
213 typedef binding<l2_list> l2_binding;
214
215 /**
216  * Definition of the static Singular DB for ACL bindings
217  */
218 template <typename LIST>
219 singular_db<typename ACL::binding<LIST>::key_t, ACL::binding<LIST>>
220   binding<LIST>::m_db;
221
222 template <typename LIST>
223 typename ACL::binding<LIST>::event_handler binding<LIST>::m_evh;
224
225 namespace {
226 const static dependency_t __attribute__((unused)) l2o = l2_binding::order();
227 const static dependency_t __attribute__((unused)) l3o = l3_binding::order();
228 };
229 };
230
231 std::ostream& operator<<(std::ostream& os,
232                          const std::pair<direction_t, interface::key_t>& key);
233 };
234
235 /*
236  * fd.io coding-style-patch-verification: ON
237  *
238  * Local Variables:
239  * eval: (c-set-style "mozilla")
240  * End:
241  */
242
243 #endif