1aa4a5e79c30cd3ec624ddbbc4019bc10c8b2fa7
[vpp.git] / src / vpp-api / vom / nat_static.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_NAT_STATIC_H__
17 #define __VOM_NAT_STATIC_H__
18
19 #include "vom/dump_cmd.hpp"
20 #include "vom/route.hpp"
21 #include "vom/singular_db.hpp"
22 #include "vom/types.hpp"
23
24 #include <vapi/nat.api.vapi.hpp>
25
26 namespace VOM {
27 /**
28  * A entry in the ARP termination table of a Bridge Domain
29  */
30 class nat_static : public object_base
31 {
32 public:
33   /**
34    * The key for a NAT static mapping.
35    *  So far only model the address only case. The address
36    * is the outside.
37    */
38   typedef std::pair<route::table_id_t, boost::asio::ip::address> key_t;
39
40   /**
41    * Construct an NAT Static binding with the outside address in default
42    * table
43    */
44   nat_static(const boost::asio::ip::address& inside,
45              const boost::asio::ip::address_v4& outside);
46
47   /**
48    * Construct an NAT Static binding with the outside address in
49    * route-domain specified
50    */
51   nat_static(const route_domain& rd,
52              const boost::asio::ip::address& inside,
53              const boost::asio::ip::address_v4& outside);
54
55   /**
56    * Copy Construct
57    */
58   nat_static(const nat_static& r);
59
60   /**
61    * Destructor
62    */
63   ~nat_static();
64
65   /**
66    * Return the matching 'singular instance'
67    */
68   std::shared_ptr<nat_static> singular() const;
69
70   /**
71    * Find the instnace of the bridge_domain domain in the OM
72    */
73   static std::shared_ptr<nat_static> find(const nat_static& temp);
74
75   /**
76    * Dump all bridge_domain-doamin into the stream provided
77    */
78   static void dump(std::ostream& os);
79
80   /**
81    * replay the object to create it in hardware
82    */
83   void replay(void);
84
85   /**
86    * Convert to string for debugging
87    */
88   std::string to_string() const;
89
90   /**
91    * A command class that creates NAT 44 static mapping
92    */
93   class create_44_cmd
94     : public rpc_cmd<HW::item<bool>, rc_t, vapi::Nat44_add_del_static_mapping>
95   {
96   public:
97     /**
98      * Constructor
99      */
100     create_44_cmd(HW::item<bool>& item,
101                   route::table_id_t id,
102                   const boost::asio::ip::address_v4& inside,
103                   const boost::asio::ip::address_v4& outside);
104
105     /**
106      * Issue the command to VPP/HW
107      */
108     rc_t issue(connection& con);
109
110     /**
111      * convert to string format for debug purposes
112      */
113     std::string to_string() const;
114
115     /**
116      * Comparison operator - only used for UT
117      */
118     bool operator==(const create_44_cmd& i) const;
119
120   private:
121     route::table_id_t m_id;
122     const boost::asio::ip::address_v4 m_inside;
123     const boost::asio::ip::address_v4 m_outside;
124   };
125
126   /**
127    * A cmd class that deletes a NAT 44 static mapping
128    */
129   class delete_44_cmd
130     : public rpc_cmd<HW::item<bool>, rc_t, vapi::Nat44_add_del_static_mapping>
131   {
132   public:
133     /**
134      * Constructor
135      */
136     delete_44_cmd(HW::item<bool>& item,
137                   route::table_id_t id,
138                   const boost::asio::ip::address_v4& inside,
139                   const boost::asio::ip::address_v4& outside);
140
141     /**
142      * Issue the command to VPP/HW
143      */
144     rc_t issue(connection& con);
145
146     /**
147      * convert to string format for debug purposes
148      */
149     std::string to_string() const;
150
151     /**
152      * Comparison operator - only used for UT
153      */
154     bool operator==(const delete_44_cmd& i) const;
155
156   private:
157     route::table_id_t m_id;
158     const boost::asio::ip::address_v4 m_inside;
159     const boost::asio::ip::address_v4 m_outside;
160   };
161
162   /**
163    * A cmd class that Dumps all the nat_statics
164    */
165   class dump_44_cmd : public dump_cmd<vapi::Nat44_static_mapping_dump>
166   {
167   public:
168     /**
169      * Constructor
170      */
171     dump_44_cmd();
172     dump_44_cmd(const dump_44_cmd& d);
173
174     /**
175      * Issue the command to VPP/HW
176      */
177     rc_t issue(connection& con);
178     /**
179      * convert to string format for debug purposes
180      */
181     std::string to_string() const;
182
183     /**
184      * Comparison operator - only used for UT
185      */
186     bool operator==(const dump_44_cmd& i) const;
187
188   private:
189     /**
190      * HW reutrn code
191      */
192     HW::item<bool> item;
193   };
194
195 private:
196   /**
197    * Class definition for listeners to OM events
198    */
199   class event_handler : public OM::listener, public inspect::command_handler
200   {
201   public:
202     event_handler();
203     virtual ~event_handler() = default;
204
205     /**
206      * Handle a populate event
207      */
208     void handle_populate(const client_db::key_t& key);
209
210     /**
211      * Handle a replay event
212      */
213     void handle_replay();
214
215     /**
216      * Show the object in the Singular DB
217      */
218     void show(std::ostream& os);
219
220     /**
221      * Get the sortable Id of the listener
222      */
223     dependency_t order() const;
224   };
225
226   /**
227    * event_handler to register with OM
228    */
229   static event_handler m_evh;
230
231   /**
232    * Commit the acculmulated changes into VPP. i.e. to a 'HW" write.
233    */
234   void update(const nat_static& obj);
235
236   /**
237    * Find or add the instnace of the bridge_domain domain in the OM
238    */
239   static std::shared_ptr<nat_static> find_or_add(const nat_static& temp);
240
241   /*
242    * It's the VPPHW class that updates the objects in HW
243    */
244   friend class OM;
245
246   /**
247    * It's the singular_db class that calls replay()
248    */
249   friend class singular_db<key_t, nat_static>;
250
251   /**
252    * Sweep/reap the object if still stale
253    */
254   void sweep(void);
255
256   /**
257    * HW configuration for the result of creating the bridge_domain
258    */
259   HW::item<bool> m_hw;
260
261   /**
262    * The table-ID the outside address resides in
263    */
264   std::shared_ptr<route_domain> m_rd;
265
266   /**
267    * The 'inside' IP address, could be v4 or v6
268    */
269   const boost::asio::ip::address& m_inside;
270
271   /**
272    * The 'outside' IP address - always v4
273    */
274   const boost::asio::ip::address_v4& m_outside;
275
276   /**
277    * A map of all NAT statics
278    */
279   static singular_db<key_t, nat_static> m_db;
280 };
281
282 std::ostream& operator<<(std::ostream& os, const nat_static::key_t& key);
283 };
284
285 /*
286  * fd.io coding-style-patch-verification: ON
287  *
288  * Local Variables:
289  * eval: (c-set-style "mozilla")
290  * End:
291  */
292
293 #endif