GBP Endpoint Learning
[vpp.git] / extras / vom / vom / gbp_subnet.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_GBP_SUBNET_H__
17 #define __VOM_GBP_SUBNET_H__
18
19 #include <ostream>
20
21 #include "vom/gbp_endpoint_group.hpp"
22 #include "vom/gbp_recirc.hpp"
23 #include "vom/gbp_route_domain.hpp"
24 #include "vom/singular_db.hpp"
25
26 namespace VOM {
27 /**
28  * A GBP Enpoint (i.e. a VM)
29  */
30 class gbp_subnet : public object_base
31 {
32 public:
33   /**
34    * The key for a GBP subnet; table and prefix
35    */
36   typedef std::pair<gbp_route_domain::key_t, route::prefix_t> key_t;
37
38   struct type_t : public enum_base<type_t>
39   {
40     /**
41      * Internal subnet is reachable through the source EPG's
42      * uplink interface.
43      */
44     const static type_t STITCHED_INTERNAL;
45
46     /**
47      * External subnet requires NAT translation before egress.
48      */
49     const static type_t STITCHED_EXTERNAL;
50
51     /**
52      * A transport subnet, sent via the RD's UU-fwd interface
53      */
54     const static type_t TRANSPORT;
55
56   private:
57     type_t(int v, const std::string s);
58   };
59
60   /**
61   * Construct an internal GBP subnet
62   */
63   gbp_subnet(const gbp_route_domain& rd,
64              const route::prefix_t& prefix,
65              const type_t& type);
66
67   /**
68    * Construct an external GBP subnet
69    */
70   gbp_subnet(const gbp_route_domain& rd,
71              const route::prefix_t& prefix,
72              const gbp_recirc& recirc,
73              const gbp_endpoint_group& epg);
74
75   /**
76    * Copy Construct
77    */
78   gbp_subnet(const gbp_subnet& r);
79
80   /**
81    * Destructor
82    */
83   ~gbp_subnet();
84
85   /**
86    * Return the object's key
87    */
88   const key_t key() const;
89
90   /**
91    * comparison operator
92    */
93   bool operator==(const gbp_subnet& bdae) const;
94
95   /**
96    * Return the matching 'singular instance'
97    */
98   std::shared_ptr<gbp_subnet> singular() const;
99
100   /**
101    * Find the instnace of the bridge_domain domain in the OM
102    */
103   static std::shared_ptr<gbp_subnet> find(const key_t& k);
104
105   /**
106    * Dump all bridge_domain-doamin into the stream provided
107    */
108   static void dump(std::ostream& os);
109
110   /**
111    * replay the object to create it in hardware
112    */
113   void replay(void);
114
115   /**
116    * Convert to string for debugging
117    */
118   std::string to_string() const;
119
120 private:
121   /**
122    * Class definition for listeners to OM events
123    */
124   class event_handler : public OM::listener, public inspect::command_handler
125   {
126   public:
127     event_handler();
128     virtual ~event_handler() = default;
129
130     /**
131      * Handle a populate event
132      */
133     void handle_populate(const client_db::key_t& key);
134
135     /**
136      * Handle a replay event
137      */
138     void handle_replay();
139
140     /**
141      * Show the object in the Singular DB
142      */
143     void show(std::ostream& os);
144
145     /**
146      * Get the sortable Id of the listener
147      */
148     dependency_t order() const;
149   };
150
151   /**
152    * event_handler to register with OM
153    */
154   static event_handler m_evh;
155
156   /**
157    * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
158    */
159   void update(const gbp_subnet& obj);
160
161   /**
162    * Find or add the instnace of the bridge_domain domain in the OM
163    */
164   static std::shared_ptr<gbp_subnet> find_or_add(const gbp_subnet& temp);
165
166   /*
167    * It's the VPPHW class that updates the objects in HW
168    */
169   friend class OM;
170
171   /**
172    * It's the singular_db class that calls replay()
173    */
174   friend class singular_db<key_t, gbp_subnet>;
175
176   /**
177    * Sweep/reap the object if still stale
178    */
179   void sweep(void);
180
181   /**
182    * HW configuration for the result of creating the subnet
183    */
184   HW::item<bool> m_hw;
185
186   /**
187    * the route domain the prefix is in
188    */
189   const std::shared_ptr<gbp_route_domain> m_rd;
190
191   /**
192    * prefix to match
193    */
194   const route::prefix_t m_prefix;
195
196   /*
197    * Subnet type
198    */
199   type_t m_type;
200
201   /**
202    * The interface the prefix is reachable through
203    */
204   std::shared_ptr<gbp_recirc> m_recirc;
205
206   /**
207    * The EPG the subnet is in
208    */
209   std::shared_ptr<gbp_endpoint_group> m_epg;
210
211   /**
212    * A map of all bridge_domains
213    */
214   static singular_db<key_t, gbp_subnet> m_db;
215 };
216
217 std::ostream& operator<<(std::ostream& os, const gbp_subnet::key_t& key);
218
219 }; // namespace
220
221 /*
222  * fd.io coding-style-patch-verification: ON
223  *
224  * Local Variables:
225  * eval: (c-set-style "mozilla")
226  * End:
227  */
228
229 #endif