blob: 6e871f9aa91dd2d7610614bd55e66503086078f8 [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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/rpc_cmd.hpp"
24#include "vom/singular_db.hpp"
25
26#include <vapi/nat.api.vapi.hpp>
27
28namespace VOM {
29/**
30 * A Clas representing the binding of an L2 interface to a bridge-domain
31 * and the properties of that binding.
32 */
33class nat_binding : public object_base
34{
35public:
36 /**
37 * NAT Zoness
38 */
39 struct zone_t : public enum_base<zone_t>
40 {
41 /**
42 * Constructor
43 */
44 zone_t(int v, const std::string s);
45
46 /**
47 * Destructor
48 */
49 ~zone_t() = default;
50
51 /**
52 * Permit Zone
53 */
54 const static zone_t INSIDE;
55
56 /**
57 * Deny Zone
58 */
59 const static zone_t OUTSIDE;
60 };
61
62 /**
63 * The key for a NAT Binding.
64 * The zoe is not included, since the same interface is never inside
65 * and outside.
66 */
67 typedef std::tuple<interface::key_type, direction_t, l3_proto_t> key_t;
68
69 /**
70 * Construct a new object matching the desried state
71 * @param itf The interface onto which we bind/apply the feature
72 * @param dir The direction (input/output)
73 * @param proto The L3 proto used inside.
74 * @param zone The NAT zone for the link
75 */
76 nat_binding(const interface& itf,
77 const direction_t& dir,
78 const l3_proto_t& proto,
79 const zone_t& zone);
80
81 /**
82 * Copy Constructor
83 */
84 nat_binding(const nat_binding& o);
85
86 /**
87 * Destructor
88 */
89 ~nat_binding();
90
91 /**
92 * Return the 'singular instance' of the L2 config that matches this
93 * object
94 */
95 std::shared_ptr<nat_binding> singular() const;
96
97 /**
98 * convert to string format for debug purposes
99 */
100 std::string to_string() const;
101
102 /**
103 * Dump all nat_bindings into the stream provided
104 */
105 static void dump(std::ostream& os);
106
107 /**
108 * A functor class that binds L2 configuration to an interface
109 */
110 class bind_44_input_cmd
111 : public rpc_cmd<HW::item<bool>,
112 rc_t,
113 vapi::Nat44_interface_add_del_feature>
114 {
115 public:
116 /**
117 * Constructor
118 */
119 bind_44_input_cmd(HW::item<bool>& item,
120 const handle_t& itf,
121 const zone_t& zone);
122
123 /**
124 * Issue the command to VPP/HW
125 */
126 rc_t issue(connection& con);
127 /**
128 * convert to string format for debug purposes
129 */
130 std::string to_string() const;
131
132 /**
133 * Comparison operator - only used for UT
134 */
135 bool operator==(const bind_44_input_cmd& i) const;
136
137 private:
138 /**
139 * The interface to bind
140 */
141 const handle_t m_itf;
142
143 /**
144 * The zone the interface is in
145 */
146 const zone_t m_zone;
147 };
148
149 /**
150 * A cmd class that Unbinds L2 configuration from an interface
151 */
152 class unbind_44_input_cmd
153 : public rpc_cmd<HW::item<bool>,
154 rc_t,
155 vapi::Nat44_interface_add_del_feature>
156 {
157 public:
158 /**
159 * Constructor
160 */
161 unbind_44_input_cmd(HW::item<bool>& item,
162 const handle_t& itf,
163 const zone_t& zone);
164
165 /**
166 * Issue the command to VPP/HW
167 */
168 rc_t issue(connection& con);
169 /**
170 * convert to string format for debug purposes
171 */
172 std::string to_string() const;
173
174 /**
175 * Comparison operator - only used for UT
176 */
177 bool operator==(const unbind_44_input_cmd& i) const;
178
179 private:
180 /**
181 * The interface to bind
182 */
183 const handle_t m_itf;
184
185 /**
186 * The zone the interface is in
187 */
188 const zone_t m_zone;
189 };
190
191 /**
192 * A cmd class that Dumps all the nat_statics
193 */
194 class dump_44_cmd : public dump_cmd<vapi::Nat44_interface_dump>
195 {
196 public:
197 /**
198 * Constructor
199 */
200 dump_44_cmd();
201 dump_44_cmd(const dump_44_cmd& d);
202
203 /**
204 * Issue the command to VPP/HW
205 */
206 rc_t issue(connection& con);
207 /**
208 * convert to string format for debug purposes
209 */
210 std::string to_string() const;
211
212 /**
213 * Comparison operator - only used for UT
214 */
215 bool operator==(const dump_44_cmd& i) const;
216
217 private:
218 /**
219 * HW reutrn code
220 */
221 HW::item<bool> item;
222 };
223
224private:
225 /**
226 * Class definition for listeners to OM events
227 */
228 class event_handler : public OM::listener, public inspect::command_handler
229 {
230 public:
231 event_handler();
232 virtual ~event_handler() = default;
233
234 /**
235 * Handle a populate event
236 */
237 void handle_populate(const client_db::key_t& key);
238
239 /**
240 * Handle a replay event
241 */
242 void handle_replay();
243
244 /**
245 * Show the object in the Singular DB
246 */
247 void show(std::ostream& os);
248
249 /**
250 * Get the sortable Id of the listener
251 */
252 dependency_t order() const;
253 };
254
255 /**
256 * event_handler to register with OM
257 */
258 static event_handler m_evh;
259
260 /**
261 * Enquue commonds to the VPP command Q for the update
262 */
263 void update(const nat_binding& obj);
264
265 /**
266 * Find or Add the singular instance in the DB
267 */
268 static std::shared_ptr<nat_binding> find_or_add(const nat_binding& temp);
269
270 /*
271 * It's the OM class that calls singular()
272 */
273 friend class OM;
274
275 /**
276 * It's the singular_db class that calls replay()
277 */
278 friend class singular_db<const key_t, nat_binding>;
279
280 /**
281 * Sweep/reap the object if still stale
282 */
283 void sweep(void);
284
285 /**
286 * replay the object to create it in hardware
287 */
288 void replay(void);
289
290 /**
291 * HW configuration for the binding. The bool representing the
292 * do/don't bind.
293 */
294 HW::item<bool> m_binding;
295
296 /**
297 * A reference counting pointer the interface that this NAT binding
298 * represents. By holding the reference here, we can guarantee that
299 * this object will outlive the interface
300 */
301 const std::shared_ptr<interface> m_itf;
302
303 /**
304 * The direction in which the feature applies
305 */
306 direction_t m_dir;
307
308 /**
309 * The L3 protocol used on the inside
310 */
311 l3_proto_t m_proto;
312
313 /**
314 * The NAT zone the interface is in
315 */
316 zone_t m_zone;
317
318 /**
319 * A map of all L2 interfaces key against the interface's handle_t
320 */
321 static singular_db<const key_t, nat_binding> m_db;
322};
323
324std::ostream& operator<<(std::ostream& os, const nat_binding::key_t& key);
325};
326
327/*
328 * fd.io coding-style-patch-verification: ON
329 *
330 * Local Variables:
331 * eval: (c-set-style "mozilla")
332 * End:
333 */
334
335#endif