FD.io VPP  v18.01-8-g0eacf49
Vector Packet Processing
nat_binding.cpp
Go to the documentation of this file.
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 #include "vom/nat_binding.hpp"
17 #include "vom/cmd.hpp"
18 #include "vom/nat_binding_cmds.hpp"
19 
20 namespace VOM {
21 singular_db<const nat_binding::key_t, nat_binding> nat_binding::m_db;
22 
23 nat_binding::event_handler nat_binding::m_evh;
24 
25 const nat_binding::zone_t nat_binding::zone_t::INSIDE(0, "inside");
26 const nat_binding::zone_t nat_binding::zone_t::OUTSIDE(0, "outside");
27 
28 nat_binding::zone_t::zone_t(int v, const std::string s)
29  : enum_base(v, s)
30 {
31 }
34 {
35  if (is_inside)
36  return zone_t::INSIDE;
37  return zone_t::OUTSIDE;
38 }
39 
40 /**
41  * Construct a new object matching the desried state
42  */
44  const direction_t& dir,
45  const l3_proto_t& proto,
46  const zone_t& zone)
47  : m_binding(false)
48  , m_itf(itf.singular())
49  , m_dir(dir)
50  , m_proto(proto)
51  , m_zone(zone)
52 {
53 }
54 
56  : m_binding(o.m_binding)
57  , m_itf(o.m_itf)
58  , m_dir(o.m_dir)
59  , m_proto(o.m_proto)
60  , m_zone(o.m_zone)
61 {
62 }
63 
65 {
66  sweep();
67  m_db.release(key(), this);
68 }
69 
72 {
73  return (make_tuple(m_itf->key(), m_dir, m_proto));
74 }
75 
76 bool
78 {
79  return ((key() == n.key()) && (m_zone == n.m_zone));
80 }
81 
82 void
83 nat_binding::sweep()
84 {
85  if (m_binding) {
86  if (direction_t::INPUT == m_dir) {
88  m_binding, m_itf->handle(), m_zone));
89  } else {
91  m_binding, m_itf->handle(), m_zone));
92  }
93  }
94  HW::write();
95 }
96 
97 void
98 nat_binding::replay()
99 {
100  if (m_binding) {
101  if (direction_t::INPUT == m_dir) {
103  m_binding, m_itf->handle(), m_zone));
104  } else {
106  m_binding, m_itf->handle(), m_zone));
107  }
108  }
109 }
110 
111 void
112 nat_binding::update(const nat_binding& desired)
113 {
114  /*
115  * the desired state is always that the interface should be created
116  */
117  if (!m_binding) {
118  if (direction_t::INPUT == m_dir) {
120  m_binding, m_itf->handle(), m_zone));
121  } else {
123  m_binding, m_itf->handle(), m_zone));
124  }
125  }
126 }
127 
128 std::string
130 {
131  std::ostringstream s;
132  s << "nat-binding:[" << m_itf->to_string()
133  << " direction:" << m_dir.to_string() << " proto:" << m_proto.to_string()
134  << " zone:" << m_zone.to_string() << "]";
135 
136  return (s.str());
137 }
138 
139 std::shared_ptr<nat_binding>
140 nat_binding::find_or_add(const nat_binding& temp)
141 {
142  return (m_db.find_or_add(temp.key(), temp));
143 }
144 
145 std::shared_ptr<nat_binding>
147 {
148  return (m_db.find(key));
149 }
150 
151 std::shared_ptr<nat_binding>
153 {
154  return find_or_add(*this);
155 }
156 
157 void
158 nat_binding::dump(std::ostream& os)
159 {
160  m_db.dump(os);
161 }
162 
163 std::ostream&
164 operator<<(std::ostream& os, const nat_binding::key_t& key)
165 {
166  os << "[" << std::get<0>(key) << ", " << std::get<1>(key) << ", "
167  << std::get<2>(key) << "]";
168 
169  return (os);
170 }
171 
172 nat_binding::event_handler::event_handler()
173 {
174  OM::register_listener(this);
175  inspect::register_handler({ "nat-binding" }, "NAT bindings", this);
176 }
177 
178 void
179 nat_binding::event_handler::handle_replay()
180 {
181  m_db.replay();
182 }
183 
184 void
185 nat_binding::event_handler::handle_populate(const client_db::key_t& key)
186 {
187  std::shared_ptr<nat_binding_cmds::dump_input_44_cmd> icmd =
188  std::make_shared<nat_binding_cmds::dump_input_44_cmd>();
189 
190  HW::enqueue(icmd);
191  HW::write();
192 
193  for (auto& record : *icmd) {
194  auto& payload = record.get_payload();
195 
196  std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
198  zone_t::from_vpp(payload.is_inside));
199  OM::commit(key, nb);
200  }
201 
202  std::shared_ptr<nat_binding_cmds::dump_output_44_cmd> ocmd =
203  std::make_shared<nat_binding_cmds::dump_output_44_cmd>();
204 
205  HW::enqueue(ocmd);
206  HW::write();
207 
208  for (auto& record : *ocmd) {
209  auto& payload = record.get_payload();
210 
211  std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
213  zone_t::from_vpp(payload.is_inside));
214  OM::commit(key, nb);
215  }
216 }
217 
219 nat_binding::event_handler::order() const
220 {
221  return (dependency_t::BINDING);
222 }
223 
224 void
225 nat_binding::event_handler::show(std::ostream& os)
226 {
227  m_db.dump(os);
228 }
229 }
230 
231 /*
232  * fd.io coding-style-patch-verification: ON
233  *
234  * Local Variables:
235  * eval: (c-set-style "mozilla")
236  * End:
237  */
~nat_binding()
Destructor.
Definition: nat_binding.cpp:64
static const zone_t OUTSIDE
Deny Zone.
Definition: nat_binding.hpp:56
A cmd class that unbinds a NAT configuration from an input interface.
A template base class for all enum types.
Definition: enum_base.hpp:30
const std::string key_t
In the opflex world each entity is known by a URI which can be converted into a string.
Definition: client_db.hpp:51
static std::shared_ptr< interface > find(const handle_t &h)
The the singular instance of the interface in the DB by handle.
Definition: interface.cpp:402
A functor class that binds a NAT configuration to an output interface.
static void register_handler(const std::vector< std::string > &cmds, const std::string &help, command_handler *ch)
Register a command handler for inspection.
Definition: inspect.cpp:85
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:225
Types belonging to Routing.
Definition: prefix.hpp:32
A Class representing the binding of an L2 interface to a bridge-domain and the properties of that bin...
Definition: nat_binding.hpp:30
static const direction_t INPUT
Permit Direction.
Definition: types.hpp:148
nat_binding(const interface &itf, const direction_t &dir, const l3_proto_t &proto, const zone_t &zone)
Construct a new object matching the desried state.
Definition: nat_binding.cpp:43
Feature Directions.
Definition: types.hpp:133
static const l3_proto_t IPV4
Definition: prefix.hpp:35
static const zone_t INSIDE
Permit Zone.
Definition: nat_binding.hpp:51
bool operator==(const nat_binding &n) const
Comparison operator - for UT.
Definition: nat_binding.cpp:77
const key_t key() const
Return the binding&#39;s key.
Definition: nat_binding.cpp:71
#define v
Definition: acl.c:341
zone_t(int v, const std::string s)
Constructor.
Definition: nat_binding.cpp:28
static void dump(std::ostream &os)
Dump all nat_bindings into the stream provided.
A functor class that binds a NAT configuration to an input interface.
A representation of an interface in VPP.
Definition: interface.hpp:41
std::string to_string() const
convert to string format for debug purposes
std::shared_ptr< nat_binding > singular() const
Return the &#39;singular instance&#39; of the L2 config that matches this object.
static rc_t commit(const client_db::key_t &key, const OBJ &obj)
Make the State in VPP reflect the expressed desired state.
Definition: om.hpp:202
dependency_t
There needs to be a strict order in which object types are read from VPP (at boot time) and replayed ...
Definition: types.hpp:43
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:189
std::ostream & operator<<(std::ostream &os, const std::pair< direction_t, interface::key_t > &key)
Then L2/objects that bind to interfaces, BD, ACLS, etc.
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
static std::shared_ptr< nat_binding > find(const key_t &key)
Static function to find the bridge_domain in the model.
unsigned char u8
Definition: types.h:56
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
static const direction_t OUTPUT
Deny Direction.
Definition: types.hpp:153
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
std::tuple< interface::key_t, direction_t, l3_proto_t > key_t
The key for a NAT Binding.
Definition: nat_binding.hpp:66
A cmd class that unbinds a NAT configuration from an output interface.
static const zone_t & from_vpp(u8 is_inside)
Definition: nat_binding.cpp:33
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:124