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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #ifndef __VOM_NAT_BINDING_H__
17 #define __VOM_NAT_BINDING_H__
20 #include "vom/interface.hpp"
21 #include "vom/object_base.hpp"
23 #include "vom/rpc_cmd.hpp"
24 #include "vom/singular_db.hpp"
26 #include <vapi/nat.api.vapi.hpp>
30 * A Clas representing the binding of an L2 interface to a bridge-domain
31 * and the properties of that binding.
33 class nat_binding : public object_base
39 struct zone_t : public enum_base<zone_t>
44 zone_t(int v, const std::string s);
54 const static zone_t INSIDE;
59 const static zone_t OUTSIDE;
63 * The key for a NAT Binding.
64 * The zoe is not included, since the same interface is never inside
67 typedef std::tuple<interface::key_type, direction_t, l3_proto_t> key_t;
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
76 nat_binding(const interface& itf,
77 const direction_t& dir,
78 const l3_proto_t& proto,
84 nat_binding(const nat_binding& o);
92 * Return the 'singular instance' of the L2 config that matches this
95 std::shared_ptr<nat_binding> singular() const;
98 * convert to string format for debug purposes
100 std::string to_string() const;
103 * Dump all nat_bindings into the stream provided
105 static void dump(std::ostream& os);
108 * A functor class that binds L2 configuration to an interface
110 class bind_44_input_cmd
111 : public rpc_cmd<HW::item<bool>,
113 vapi::Nat44_interface_add_del_feature>
119 bind_44_input_cmd(HW::item<bool>& item,
124 * Issue the command to VPP/HW
126 rc_t issue(connection& con);
128 * convert to string format for debug purposes
130 std::string to_string() const;
133 * Comparison operator - only used for UT
135 bool operator==(const bind_44_input_cmd& i) const;
139 * The interface to bind
141 const handle_t m_itf;
144 * The zone the interface is in
150 * A cmd class that Unbinds L2 configuration from an interface
152 class unbind_44_input_cmd
153 : public rpc_cmd<HW::item<bool>,
155 vapi::Nat44_interface_add_del_feature>
161 unbind_44_input_cmd(HW::item<bool>& item,
166 * Issue the command to VPP/HW
168 rc_t issue(connection& con);
170 * convert to string format for debug purposes
172 std::string to_string() const;
175 * Comparison operator - only used for UT
177 bool operator==(const unbind_44_input_cmd& i) const;
181 * The interface to bind
183 const handle_t m_itf;
186 * The zone the interface is in
192 * A cmd class that Dumps all the nat_statics
194 class dump_44_cmd : public dump_cmd<vapi::Nat44_interface_dump>
201 dump_44_cmd(const dump_44_cmd& d);
204 * Issue the command to VPP/HW
206 rc_t issue(connection& con);
208 * convert to string format for debug purposes
210 std::string to_string() const;
213 * Comparison operator - only used for UT
215 bool operator==(const dump_44_cmd& i) const;
226 * Class definition for listeners to OM events
228 class event_handler : public OM::listener, public inspect::command_handler
232 virtual ~event_handler() = default;
235 * Handle a populate event
237 void handle_populate(const client_db::key_t& key);
240 * Handle a replay event
242 void handle_replay();
245 * Show the object in the Singular DB
247 void show(std::ostream& os);
250 * Get the sortable Id of the listener
252 dependency_t order() const;
256 * event_handler to register with OM
258 static event_handler m_evh;
261 * Enquue commonds to the VPP command Q for the update
263 void update(const nat_binding& obj);
266 * Find or Add the singular instance in the DB
268 static std::shared_ptr<nat_binding> find_or_add(const nat_binding& temp);
271 * It's the OM class that calls singular()
276 * It's the singular_db class that calls replay()
278 friend class singular_db<const key_t, nat_binding>;
281 * Sweep/reap the object if still stale
286 * replay the object to create it in hardware
291 * HW configuration for the binding. The bool representing the
294 HW::item<bool> m_binding;
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
301 const std::shared_ptr<interface> m_itf;
304 * The direction in which the feature applies
309 * The L3 protocol used on the inside
314 * The NAT zone the interface is in
319 * A map of all L2 interfaces key against the interface's handle_t
321 static singular_db<const key_t, nat_binding> m_db;
324 std::ostream& operator<<(std::ostream& os, const nat_binding::key_t& key);
328 * fd.io coding-style-patch-verification: ON
331 * eval: (c-set-style "mozilla")