GBP: add allowed ethertypes to contracts
[vpp.git] / extras / vom / vom / l2_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_L2_BINDING_H__
17 #define __VOM_L2_BINDING_H__
18
19 #include "vom/bridge_domain.hpp"
20 #include "vom/hw.hpp"
21 #include "vom/inspect.hpp"
22 #include "vom/interface.hpp"
23 #include "vom/object_base.hpp"
24 #include "vom/om.hpp"
25 #include "vom/singular_db.hpp"
26
27 namespace VOM {
28 /**
29  * A Clas representing the binding of an L2 interface to a bridge-domain
30  * and the properties of that binding.
31  */
32 class l2_binding : public object_base
33 {
34 public:
35   /**
36    * Key type for an L2 binding in the singular DB
37    */
38   typedef interface::key_t key_t;
39
40   struct l2_vtr_op_t : public enum_base<l2_vtr_op_t>
41   {
42     l2_vtr_op_t(const l2_vtr_op_t& l) = default;
43     ~l2_vtr_op_t() = default;
44
45     const static l2_vtr_op_t L2_VTR_DISABLED;
46     const static l2_vtr_op_t L2_VTR_PUSH_1;
47     const static l2_vtr_op_t L2_VTR_PUSH_2;
48     const static l2_vtr_op_t L2_VTR_POP_1;
49     const static l2_vtr_op_t L2_VTR_POP_2;
50     const static l2_vtr_op_t L2_VTR_TRANSLATE_1_1;
51     const static l2_vtr_op_t L2_VTR_TRANSLATE_1_2;
52     const static l2_vtr_op_t L2_VTR_TRANSLATE_2_1;
53     const static l2_vtr_op_t L2_VTR_TRANSLATE_2_2;
54
55   private:
56     l2_vtr_op_t(int v, const std::string s);
57   };
58
59   struct l2_port_type_t : public enum_base<l2_port_type_t>
60   {
61     l2_port_type_t(const l2_port_type_t& l) = default;
62     ~l2_port_type_t() = default;
63
64     const static l2_port_type_t L2_PORT_TYPE_NORMAL;
65     const static l2_port_type_t L2_PORT_TYPE_BVI;
66     const static l2_port_type_t L2_PORT_TYPE_UU_FWD;
67
68   private:
69     l2_port_type_t(int v, const std::string s);
70   };
71
72   /**
73    * Construct a new object matching the desried state
74    */
75   l2_binding(const interface& itf, const bridge_domain& bd);
76   l2_binding(const interface& itf,
77              const bridge_domain& bd,
78              const l2_port_type_t& port_type);
79
80   /**
81    * Copy Constructor
82    */
83   l2_binding(const l2_binding& o);
84
85   /**
86    * Destructor
87    */
88   ~l2_binding();
89
90   /**
91    * Return the binding's key
92    */
93   const key_t& key() const;
94
95   /**
96    * Comparison operator - for UT
97    */
98   bool operator==(const l2_binding& l) const;
99
100   /**
101    * Return the 'singular instance' of the L2 config that matches this
102    * object
103    */
104   std::shared_ptr<l2_binding> singular() const;
105
106   /**
107    * convert to string format for debug purposes
108    */
109   std::string to_string() const;
110
111   /**
112    * Dump all l2_bindings into the stream provided
113    */
114   static void dump(std::ostream& os);
115
116   /**
117    * Set the VTR operation on the binding/interface
118    */
119   void set(const l2_vtr_op_t& op, uint16_t tag);
120
121   /**
122    * Static function to find the bridge_domain in the model
123    */
124   static std::shared_ptr<l2_binding> find(const key_t& key);
125
126 private:
127   /**
128    * Class definition for listeners to OM events
129    */
130   class event_handler : public OM::listener, public inspect::command_handler
131   {
132   public:
133     event_handler();
134     virtual ~event_handler() = default;
135
136     /**
137      * Handle a populate event
138      */
139     void handle_populate(const client_db::key_t& key);
140
141     /**
142      * Handle a replay event
143      */
144     void handle_replay();
145
146     /**
147      * Show the object in the Singular DB
148      */
149     void show(std::ostream& os);
150
151     /**
152      * Get the sortable Id of the listener
153      */
154     dependency_t order() const;
155   };
156
157   /**
158    * event_handler to register with OM
159    */
160   static event_handler m_evh;
161
162   /**
163    * Enquue commonds to the VPP command Q for the update
164    */
165   void update(const l2_binding& obj);
166
167   /**
168    * Find or Add the singular instance in the DB
169    */
170   static std::shared_ptr<l2_binding> find_or_add(const l2_binding& temp);
171
172   /*
173    * It's the OM class that calls singular()
174    */
175   friend class OM;
176
177   /**
178    * It's the singular_db class that calls replay()
179    */
180   friend class singular_db<key_t, l2_binding>;
181
182   /**
183    * Sweep/reap the object if still stale
184    */
185   void sweep(void);
186
187   /**
188    * replay the object to create it in hardware
189    */
190   void replay(void);
191
192   /**
193    * A reference counting pointer the interface that this L2 layer
194    * represents. By holding the reference here, we can guarantee that
195    * this object will outlive the interface
196    */
197   const std::shared_ptr<interface> m_itf;
198
199   /**
200    * A reference counting pointer the Bridge-Domain that this L2
201    * interface is bound to. By holding the reference here, we can
202    * guarantee that this object will outlive the BD.
203    */
204   std::shared_ptr<bridge_domain> m_bd;
205
206   /**
207    * l2 port type i.e. normal, bvi or unknown unicast
208    */
209   l2_port_type_t m_port_type;
210
211   /**
212    * HW configuration for the binding. The bool representing the
213    * do/don't bind.
214    */
215   HW::item<bool> m_binding;
216
217   /**
218    * HW configuration for the VTR option
219    */
220   HW::item<l2_vtr_op_t> m_vtr_op;
221
222   /**
223    * The Dot1q tag for the VTR operation
224    */
225   uint16_t m_vtr_op_tag;
226
227   /**
228    * A map of all L2 interfaces key against the interface's handle_t
229    */
230   static singular_db<key_t, l2_binding> m_db;
231 };
232 };
233
234 /*
235  * fd.io coding-style-patch-verification: ON
236  *
237  * Local Variables:
238  * eval: (c-set-style "mozilla")
239  * End:
240  */
241
242 #endif