Break up vpe.api
[vpp.git] / src / vpp-api / vom / nat_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_NAT_BINDING_H__
17 #define __VOM_NAT_BINDING_H__
18
19 #include "vom/hw.hpp"
20 #include "vom/interface.hpp"
21 #include "vom/object_base.hpp"
22 #include "vom/om.hpp"
23 #include "vom/singular_db.hpp"
24
25 namespace VOM {
26 /**
27  * A Class representing the binding of an L2 interface to a bridge-domain
28  * and the properties of that binding.
29  */
30 class nat_binding : public object_base
31 {
32 public:
33   /**
34    * NAT Zoness
35    */
36   struct zone_t : public enum_base<zone_t>
37   {
38     /**
39      * Constructor
40      */
41     zone_t(int v, const std::string s);
42
43     /**
44      * Destructor
45      */
46     ~zone_t() = default;
47
48     /**
49      * Permit Zone
50      */
51     const static zone_t INSIDE;
52
53     /**
54      * Deny Zone
55      */
56     const static zone_t OUTSIDE;
57   };
58
59   /**
60    * The key for a NAT Binding.
61    *  The zoe is not included, since the same interface is never inside
62    * and outside.
63    */
64   typedef std::tuple<interface::key_type, direction_t, l3_proto_t> key_t;
65
66   /**
67    * Construct a new object matching the desried state
68    *  @param itf The interface onto which we bind/apply the feature
69    *  @param dir The direction (input/output)
70    *  @param proto The L3 proto used inside.
71    *  @param zone The NAT zone for the link
72    */
73   nat_binding(const interface& itf,
74               const direction_t& dir,
75               const l3_proto_t& proto,
76               const zone_t& zone);
77
78   /**
79    * Copy Constructor
80    */
81   nat_binding(const nat_binding& o);
82
83   /**
84    * Destructor
85    */
86   ~nat_binding();
87
88   /**
89    * Return the 'singular instance' of the L2 config that matches this
90    * object
91    */
92   std::shared_ptr<nat_binding> singular() const;
93
94   /**
95    * convert to string format for debug purposes
96    */
97   std::string to_string() const;
98
99   /**
100    * Dump all nat_bindings into the stream provided
101    */
102   static void dump(std::ostream& os);
103
104 private:
105   /**
106    * Class definition for listeners to OM events
107    */
108   class event_handler : public OM::listener, public inspect::command_handler
109   {
110   public:
111     event_handler();
112     virtual ~event_handler() = default;
113
114     /**
115      * Handle a populate event
116      */
117     void handle_populate(const client_db::key_t& key);
118
119     /**
120      * Handle a replay event
121      */
122     void handle_replay();
123
124     /**
125      * Show the object in the Singular DB
126      */
127     void show(std::ostream& os);
128
129     /**
130      * Get the sortable Id of the listener
131      */
132     dependency_t order() const;
133   };
134
135   /**
136    * event_handler to register with OM
137    */
138   static event_handler m_evh;
139
140   /**
141    * Enquue commonds to the VPP command Q for the update
142    */
143   void update(const nat_binding& obj);
144
145   /**
146    * Find or Add the singular instance in the DB
147    */
148   static std::shared_ptr<nat_binding> find_or_add(const nat_binding& temp);
149
150   /*
151    * It's the OM class that calls singular()
152    */
153   friend class OM;
154
155   /**
156    * It's the singular_db class that calls replay()
157    */
158   friend class singular_db<const key_t, nat_binding>;
159
160   /**
161    * Sweep/reap the object if still stale
162    */
163   void sweep(void);
164
165   /**
166    * replay the object to create it in hardware
167    */
168   void replay(void);
169
170   /**
171    * HW configuration for the binding. The bool representing the
172    * do/don't bind.
173    */
174   HW::item<bool> m_binding;
175
176   /**
177    * A reference counting pointer the interface that this NAT binding
178    * represents. By holding the reference here, we can guarantee that
179    * this object will outlive the interface
180    */
181   const std::shared_ptr<interface> m_itf;
182
183   /**
184    * The direction in which the feature applies
185    */
186   direction_t m_dir;
187
188   /**
189    * The L3 protocol used on the inside
190    */
191   l3_proto_t m_proto;
192
193   /**
194    * The NAT zone the interface is in
195    */
196   zone_t m_zone;
197
198   /**
199    * A map of all L2 interfaces key against the interface's handle_t
200    */
201   static singular_db<const key_t, nat_binding> m_db;
202 };
203
204 std::ostream& operator<<(std::ostream& os, const nat_binding::key_t& key);
205 };
206
207 /*
208  * fd.io coding-style-patch-verification: ON
209  *
210  * Local Variables:
211  * eval: (c-set-style "mozilla")
212  * End:
213  */
214
215 #endif