GBP: add allowed ethertypes to contracts
[vpp.git] / extras / vom / 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     const static zone_t& from_vpp(u8 is_inside);
59   };
60
61   /**
62    * The key for a NAT Binding.
63    *  The zoe is not included, since the same interface is never inside
64    * and outside.
65    */
66   typedef std::tuple<interface::key_t, direction_t, l3_proto_t> key_t;
67
68   /**
69    * Construct a new object matching the desried state
70    *  @param itf The interface onto which we bind/apply the feature
71    *  @param dir The direction (input/output)
72    *  @param proto The L3 proto used inside.
73    *  @param zone The NAT zone for the link
74    */
75   nat_binding(const interface& itf,
76               const direction_t& dir,
77               const l3_proto_t& proto,
78               const zone_t& zone);
79
80   /**
81    * Copy Constructor
82    */
83   nat_binding(const nat_binding& o);
84
85   /**
86    * Destructor
87    */
88   ~nat_binding();
89
90   /**
91    * Comparison operator - for UT
92    */
93   bool operator==(const nat_binding& n) const;
94
95   /**
96    * Return the binding's key
97    */
98   const key_t key() const;
99
100   /**
101    * Return the 'singular instance' of the L2 config that matches this
102    * object
103    */
104   std::shared_ptr<nat_binding> singular() const;
105
106   /**
107    * convert to string format for debug purposes
108    */
109   std::string to_string() const;
110
111   /**
112    * Static function to find the bridge_domain in the model
113    */
114   static std::shared_ptr<nat_binding> find(const key_t& key);
115
116   /**
117    * Dump all nat_bindings into the stream provided
118    */
119   static void dump(std::ostream& os);
120
121 private:
122   /**
123    * Class definition for listeners to OM events
124    */
125   class event_handler : public OM::listener, public inspect::command_handler
126   {
127   public:
128     event_handler();
129     virtual ~event_handler() = default;
130
131     /**
132      * Handle a populate event
133      */
134     void handle_populate(const client_db::key_t& key);
135
136     /**
137      * Handle a replay event
138      */
139     void handle_replay();
140
141     /**
142      * Show the object in the Singular DB
143      */
144     void show(std::ostream& os);
145
146     /**
147      * Get the sortable Id of the listener
148      */
149     dependency_t order() const;
150   };
151
152   /**
153    * event_handler to register with OM
154    */
155   static event_handler m_evh;
156
157   /**
158    * Enquue commonds to the VPP command Q for the update
159    */
160   void update(const nat_binding& obj);
161
162   /**
163    * Find or Add the singular instance in the DB
164    */
165   static std::shared_ptr<nat_binding> find_or_add(const nat_binding& temp);
166
167   /*
168    * It's the OM class that calls singular()
169    */
170   friend class OM;
171
172   /**
173    * It's the singular_db class that calls replay()
174    */
175   friend class singular_db<const key_t, nat_binding>;
176
177   /**
178    * Sweep/reap the object if still stale
179    */
180   void sweep(void);
181
182   /**
183    * replay the object to create it in hardware
184    */
185   void replay(void);
186
187   /**
188    * HW configuration for the binding. The bool representing the
189    * do/don't bind.
190    */
191   HW::item<bool> m_binding;
192
193   /**
194    * A reference counting pointer the interface that this NAT binding
195    * represents. By holding the reference here, we can guarantee that
196    * this object will outlive the interface
197    */
198   const std::shared_ptr<interface> m_itf;
199
200   /**
201    * The direction in which the feature applies
202    */
203   direction_t m_dir;
204
205   /**
206    * The L3 protocol used on the inside
207    */
208   l3_proto_t m_proto;
209
210   /**
211    * The NAT zone the interface is in
212    */
213   zone_t m_zone;
214
215   /**
216    * A map of all L2 interfaces key against the interface's handle_t
217    */
218   static singular_db<const key_t, nat_binding> m_db;
219 };
220
221 std::ostream& operator<<(std::ostream& os, const nat_binding::key_t& key);
222 };
223
224 /*
225  * fd.io coding-style-patch-verification: ON
226  *
227  * Local Variables:
228  * eval: (c-set-style "mozilla")
229  * End:
230  */
231
232 #endif