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