VOM: vxlan-gbp
[vpp.git] / extras / vom / vom / vxlan_tunnel.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_VXLAN_TUNNEL_H__
17 #define __VOM_VXLAN_TUNNEL_H__
18
19 #include "vom/hw.hpp"
20 #include "vom/inspect.hpp"
21 #include "vom/interface.hpp"
22 #include "vom/object_base.hpp"
23 #include "vom/om.hpp"
24 #include "vom/prefix.hpp"
25 #include "vom/route_domain.hpp"
26 #include "vom/singular_db.hpp"
27
28 namespace VOM {
29 /**
30  * A representation of a VXLAN Tunnel in VPP
31  */
32 class vxlan_tunnel : public interface
33 {
34 public:
35   /**
36    * Combaintion of attributes that are a unique key
37    * for a VXLAN tunnel
38    */
39   struct endpoint_t
40   {
41     /**
42      * Default constructor
43      */
44     endpoint_t();
45     /**
46      * Constructor taking endpoint values
47      */
48     endpoint_t(const boost::asio::ip::address& src,
49                const boost::asio::ip::address& dst,
50                uint32_t vni);
51
52     /**
53      * Comparison operator
54      */
55     bool operator==(const endpoint_t& o) const;
56
57     /**
58      * Debug print function
59      */
60     std::string to_string() const;
61
62     /**
63      * The src IP address of the endpoint
64      */
65     boost::asio::ip::address src;
66
67     /**
68      * The destination IP address of the endpoint
69      */
70     boost::asio::ip::address dst;
71
72     /**
73      * The VNI of the endpoint
74      */
75     uint32_t vni;
76   };
77
78   /**
79    * mode for the tunnel
80    */
81   struct mode_t : public enum_base<mode_t>
82   {
83     ~mode_t() = default;
84     const static mode_t STANDARD;
85     const static mode_t GBP;
86     const static mode_t GPE;
87
88   private:
89     mode_t(int v, const std::string s);
90     mode_t() = default;
91   };
92
93   /**
94    * Construct a new object matching the desried state
95    */
96   vxlan_tunnel(const boost::asio::ip::address& src,
97                const boost::asio::ip::address& dst,
98                uint32_t vni,
99                const mode_t& mode = mode_t::STANDARD);
100
101   /**
102    * Construct a new object matching the desried state with a handle
103    * read from VPP
104    */
105   vxlan_tunnel(const handle_t& hdl,
106                const boost::asio::ip::address& src,
107                const boost::asio::ip::address& dst,
108                uint32_t vni,
109                const mode_t& mode = mode_t::STANDARD);
110
111   /*
112    * Destructor
113    */
114   ~vxlan_tunnel();
115
116   /**
117    * Copy constructor
118    */
119   vxlan_tunnel(const vxlan_tunnel& o);
120
121   /**
122    * Return the matching 'singular instance'
123    */
124   std::shared_ptr<vxlan_tunnel> singular() const;
125
126   /**
127    * Debug rpint function
128    */
129   virtual std::string to_string() const;
130
131   /**
132    * Return VPP's handle to this object
133    */
134   const handle_t& handle() const;
135
136   /**
137    * Fond the singular instance of the interface in the DB by key
138    */
139   static std::shared_ptr<vxlan_tunnel> find(const interface::key_t& k);
140
141 private:
142   /**
143    * Class definition for listeners to OM events
144    */
145   class event_handler : public OM::listener, public inspect::command_handler
146   {
147   public:
148     event_handler();
149     virtual ~event_handler() = default;
150
151     /**
152      * Handle a populate event
153      */
154     void handle_populate(const client_db::key_t& key);
155
156     /**
157      * Handle a replay event
158      */
159     void handle_replay();
160
161     /**
162      * Show the object in the Singular DB
163      */
164     void show(std::ostream& os);
165
166     /**
167      * Get the sortable Id of the listener
168      */
169     dependency_t order() const;
170   };
171
172   /**
173    * Event handle to register with OM
174    */
175   static event_handler m_evh;
176
177   /**
178    * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
179    */
180   void update(const vxlan_tunnel& obj);
181
182   /**
183    * Return the matching 'instance' of the sub-interface
184    *  over-ride from the base class
185    */
186   std::shared_ptr<interface> singular_i() const;
187
188   /**
189    * Find the VXLAN tunnel in the OM
190    */
191   static std::shared_ptr<vxlan_tunnel> find_or_add(const vxlan_tunnel& temp);
192
193   /*
194    * It's the VPPHW class that updates the objects in HW
195    */
196   friend class OM;
197
198   /**
199    * It's the singular_db class that calls replay()
200    */
201   friend class singular_db<endpoint_t, vxlan_tunnel>;
202
203   /**
204    * Sweep/reap the object if still stale
205    */
206   void sweep(void);
207
208   /**
209    * replay the object to create it in hardware
210    */
211   void replay(void);
212
213   /**
214    * Tunnel enpoint/key
215    */
216   endpoint_t m_tep;
217
218   /**
219    * The tunnel mode
220    */
221   mode_t m_mode;
222
223   /**
224    * Construct a unique name for the tunnel
225    */
226   static std::string mk_name(const boost::asio::ip::address& src,
227                              const boost::asio::ip::address& dst,
228                              const mode_t& mode,
229                              uint32_t vni);
230 };
231
232 /**
233  * Ostream output for a tunnel endpoint
234  */
235 std::ostream& operator<<(std::ostream& os, const vxlan_tunnel::endpoint_t& ep);
236
237 }; // namespace VOM
238
239 /*
240  * fd.io coding-style-patch-verification: ON
241  *
242  * Local Variables:
243  * eval: (c-set-style "mozilla")
244  * End:
245  */
246
247 #endif