ecaae04496d07e2364e0e614017c5703d5ad5421
[vpp.git] / extras / vom / vom / bridge_domain.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_BRIDGE_DOMAIN_H__
17 #define __VOM_BRIDGE_DOMAIN_H__
18
19 #include "vom/enum_base.hpp"
20 #include "vom/hw.hpp"
21 #include "vom/inspect.hpp"
22 #include "vom/object_base.hpp"
23 #include "vom/om.hpp"
24 #include "vom/singular_db.hpp"
25
26 namespace VOM {
27 /**
28  * A base class for all object_base in the VPP object_base-Model.
29  *  provides the abstract interface.
30  */
31 class bridge_domain : public object_base
32 {
33 public:
34   /**
35    * Key Type for Bridge Domains in the sigular DB
36    */
37   typedef uint32_t key_t;
38
39   /**
40    * Bridge Domain Learning mode
41    */
42   struct learning_mode_t : enum_base<learning_mode_t>
43   {
44     const static learning_mode_t ON;
45     const static learning_mode_t OFF;
46
47   private:
48     /**
49      * Private constructor taking the value and the string name
50      */
51     learning_mode_t(int v, const std::string& s);
52   };
53
54   /**
55    * Bridge Domain ARP termination mode
56    */
57   struct arp_term_mode_t : enum_base<arp_term_mode_t>
58   {
59     const static arp_term_mode_t ON;
60     const static arp_term_mode_t OFF;
61
62   private:
63     /**
64      * Private constructor taking the value and the string name
65      */
66     arp_term_mode_t(int v, const std::string& s);
67   };
68
69   /**
70    * Bridge Domain ARP Unicast Forward mode
71    */
72   struct arp_ufwd_mode_t : enum_base<arp_ufwd_mode_t>
73   {
74     const static arp_ufwd_mode_t ON;
75     const static arp_ufwd_mode_t OFF;
76
77   private:
78     /**
79      * Private constructor taking the value and the string name
80      */
81     arp_ufwd_mode_t(int v, const std::string& s);
82   };
83
84   /**
85    * Bridge Domain MAC aging mode
86    */
87   struct mac_age_mode_t : enum_base<mac_age_mode_t>
88   {
89     const static mac_age_mode_t ON;
90     const static mac_age_mode_t OFF;
91
92   private:
93     /**
94      * Private constructor taking the value and the string name
95      */
96     mac_age_mode_t(int v, const std::string& s);
97   };
98
99   /**
100    * Bridge Domain Learning mode
101    */
102   struct flood_mode_t : enum_base<flood_mode_t>
103   {
104     const static flood_mode_t ON;
105     const static flood_mode_t OFF;
106
107   private:
108     /**
109      * Private constructor taking the value and the string name
110      */
111     flood_mode_t(int v, const std::string& s);
112   };
113
114   /**
115    * The value of the defaultbridge domain
116    */
117   const static uint32_t DEFAULT_TABLE = 0;
118
119   /**
120    * Construct a new object matching the desried state
121    */
122   bridge_domain(uint32_t id,
123                 const learning_mode_t& lmode = learning_mode_t::ON,
124                 const arp_term_mode_t& amode = arp_term_mode_t::ON,
125                 const arp_ufwd_mode_t& aumode = arp_ufwd_mode_t::OFF,
126                 const flood_mode_t& fmode = flood_mode_t::ON,
127                 const mac_age_mode_t& mmode = mac_age_mode_t::OFF);
128
129   /**
130    * Copy Constructor
131    */
132   bridge_domain(const bridge_domain& o);
133
134   /**
135    * Destructor
136    */
137   ~bridge_domain();
138
139   /**
140    * Comparison operator - for UT
141    */
142   bool operator==(const bridge_domain& b) const;
143
144   /**
145    * Return the bridge domain's VPP ID
146    */
147   uint32_t id() const;
148
149   /**
150    * Return the bridge domain's key
151    */
152   const key_t& key() const;
153
154   /**
155    * Return the matchin 'singular' instance of the bridge-domain
156    */
157   std::shared_ptr<bridge_domain> singular() const;
158
159   /**
160    * convert to string format for debug purposes
161    */
162   std::string to_string(void) const;
163
164   /**
165    * Static function to find the bridge_domain in the model
166    */
167   static std::shared_ptr<bridge_domain> find(const key_t& key);
168
169   /**
170    * Dump all bridge-doamin into the stream provided
171    */
172   static void dump(std::ostream& os);
173
174 private:
175   /**
176    * Class definition for listeners to OM events
177    */
178   class event_handler : public OM::listener, public inspect::command_handler
179   {
180   public:
181     event_handler();
182     virtual ~event_handler() = default;
183
184     /**
185      * Handle a populate event
186      */
187     void handle_populate(const client_db::key_t& key);
188
189     /**
190      * Handle a replay event
191      */
192     void handle_replay();
193
194     /**
195      * Show the object in the Singular DB
196      */
197     void show(std::ostream& os);
198
199     /**
200      * Get the sortable Id of the listener
201      */
202     dependency_t order() const;
203   };
204
205   /**
206    * Instance of the event handler to register with OM
207    */
208   static event_handler m_evh;
209
210   /**
211    * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
212    */
213   void update(const bridge_domain& obj);
214
215   /**
216    * Find or add an singular of a Bridge-Domain in the object_base Model
217    */
218   static std::shared_ptr<bridge_domain> find_or_add(const bridge_domain& temp);
219
220   /*
221    * It's the OM class that calls singular()
222    */
223   friend class OM;
224
225   /**
226    * It's the singular_db class that calls replay()
227    */
228   friend class singular_db<key_t, bridge_domain>;
229
230   /**
231    * Sweep/reap the object if still stale
232    */
233   void sweep(void);
234
235   /**
236    * replay the object to create it in hardware
237    */
238   void replay(void);
239
240   /**
241    * The ID we assign to this BD and the HW result in VPP
242    */
243   HW::item<uint32_t> m_id;
244
245   /**
246    * The learning mode of the bridge
247    */
248   learning_mode_t m_learning_mode;
249
250   /**
251    * The ARP termination mode of the bridge
252    */
253   arp_term_mode_t m_arp_term_mode;
254
255   /**
256    * The ARP Unicast Forward mode of the bridge
257    */
258   arp_ufwd_mode_t m_arp_ufwd_mode;
259
260   /**
261    * The flood mode of the bridge
262    */
263   flood_mode_t m_flood_mode;
264
265   /**
266    * The MAC aging mode of the bridge
267    */
268   mac_age_mode_t m_mac_age_mode;
269
270   /**
271    * A map of all interfaces key against the interface's name
272    */
273   static singular_db<key_t, bridge_domain> m_db;
274 };
275 };
276
277 /*
278  * fd.io coding-style-patch-verification: ON
279  *
280  * Local Variables:
281  * eval: (c-set-style "mozilla")
282  * End:
283  */
284
285 #endif