FD.io VPP  v21.06-3-gbb25fbf28
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"
20 
21 namespace VOM {
22 singular_db<const nat_binding::key_t, nat_binding> nat_binding::m_db;
23 
24 nat_binding::event_handler nat_binding::m_evh;
25 
26 const nat_binding::zone_t nat_binding::zone_t::INSIDE(0, "inside");
27 const nat_binding::zone_t nat_binding::zone_t::OUTSIDE(0, "outside");
28 
30  : enum_base(v, s)
31 {
32 }
35 {
36  if (is_inside)
37  return zone_t::INSIDE;
38  return zone_t::OUTSIDE;
39 }
40 
41 /**
42  * Construct a new object matching the desried state
43  */
45  const direction_t& dir,
46  const l3_proto_t& proto,
47  const zone_t& zone)
48  : m_binding(false)
49  , m_itf(itf.singular())
50  , m_dir(dir)
51  , m_proto(proto)
52  , m_zone(zone)
53 {
54 }
55 
57  : m_binding(o.m_binding)
58  , m_itf(o.m_itf)
59  , m_dir(o.m_dir)
60  , m_proto(o.m_proto)
61  , m_zone(o.m_zone)
62 {
63 }
64 
66 {
67  sweep();
68  m_db.release(key(), this);
69 }
70 
73 {
74  return (make_tuple(m_itf->key(), m_dir, m_proto));
75 }
76 
77 bool
79 {
80  return ((key() == n.key()) && (m_zone == n.m_zone));
81 }
82 
83 void
84 nat_binding::sweep()
85 {
86  if (m_binding) {
87  if (direction_t::INPUT == m_dir) {
88  if (l3_proto_t::IPV4 == m_proto) {
90  m_binding, m_itf->handle(), m_zone));
91  } else {
92  HW::enqueue(new nat_binding_cmds::unbind_66_input_cmd(
93  m_binding, m_itf->handle(), m_zone));
94  }
95  } else {
96  if (l3_proto_t::IPV4 == m_proto) {
97  HW::enqueue(new nat_binding_cmds::unbind_44_output_cmd(
98  m_binding, m_itf->handle(), m_zone));
99  } else {
100  VOM_LOG(log_level_t::ERROR) << "NAT 66 output feature not supported";
101  }
102  }
103  }
104  HW::write();
105 }
106 
107 void
108 nat_binding::replay()
109 {
110  if (m_binding) {
111  if (direction_t::INPUT == m_dir) {
112  if (l3_proto_t::IPV4 == m_proto) {
113  HW::enqueue(new nat_binding_cmds::bind_44_input_cmd(
114  m_binding, m_itf->handle(), m_zone));
115  } else {
116  HW::enqueue(new nat_binding_cmds::bind_66_input_cmd(
117  m_binding, m_itf->handle(), m_zone));
118  }
119  } else {
120  if (l3_proto_t::IPV4 == m_proto) {
121  HW::enqueue(new nat_binding_cmds::bind_44_output_cmd(
122  m_binding, m_itf->handle(), m_zone));
123  } else {
124  VOM_LOG(log_level_t::ERROR) << "NAT 66 output feature not supported";
125  }
126  }
127  }
128 }
129 
130 void
131 nat_binding::update(const nat_binding& desired)
132 {
133  /*
134  * the desired state is always that the interface should be created
135  */
136  if (!m_binding) {
137  if (direction_t::INPUT == m_dir) {
138  if (l3_proto_t::IPV4 == m_proto) {
139  HW::enqueue(new nat_binding_cmds::bind_44_input_cmd(
140  m_binding, m_itf->handle(), m_zone));
141  } else {
142  HW::enqueue(new nat_binding_cmds::bind_66_input_cmd(
143  m_binding, m_itf->handle(), m_zone));
144  }
145  } else {
146  if (l3_proto_t::IPV4 == m_proto) {
147  HW::enqueue(new nat_binding_cmds::bind_44_output_cmd(
148  m_binding, m_itf->handle(), m_zone));
149  } else {
150  VOM_LOG(log_level_t::ERROR) << "NAT 66 output feature not supported";
151  }
152  }
153  }
154 }
155 
158 {
159  std::ostringstream s;
160  s << "nat-binding:[" << m_itf->to_string()
161  << " direction:" << m_dir.to_string() << " proto:" << m_proto.to_string()
162  << " zone:" << m_zone.to_string() << "]";
163 
164  return (s.str());
165 }
166 
167 std::shared_ptr<nat_binding>
168 nat_binding::find_or_add(const nat_binding& temp)
169 {
170  return (m_db.find_or_add(temp.key(), temp));
171 }
172 
173 std::shared_ptr<nat_binding>
175 {
176  return (m_db.find(key));
177 }
178 
179 std::shared_ptr<nat_binding>
181 {
182  return find_or_add(*this);
183 }
184 
185 void
186 nat_binding::dump(std::ostream& os)
187 {
188  db_dump(m_db, os);
189 }
190 
191 std::ostream&
192 operator<<(std::ostream& os, const nat_binding::key_t& key)
193 {
194  os << "[" << std::get<0>(key) << ", " << std::get<1>(key) << ", "
195  << std::get<2>(key) << "]";
196 
197  return (os);
198 }
199 
201 {
202  OM::register_listener(this);
203  inspect::register_handler({ "nat-binding" }, "NAT bindings", this);
204 }
205 
206 void
207 nat_binding::event_handler::handle_replay()
208 {
209  m_db.replay();
210 }
211 
212 void
213 nat_binding::event_handler::handle_populate(const client_db::key_t& key)
214 {
215  std::shared_ptr<nat_binding_cmds::dump_input_44_cmd> icmd =
216  std::make_shared<nat_binding_cmds::dump_input_44_cmd>();
217 
218  HW::enqueue(icmd);
219  HW::write();
220 
221  for (auto& record : *icmd) {
222  auto& payload = record.get_payload();
223 
224  std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
225 
226  if (itf) {
227  nat_binding nb(*itf, direction_t::INPUT, l3_proto_t::IPV4,
228  zone_t::from_vpp(payload.flags & NAT_IS_INSIDE));
229  OM::commit(key, nb);
230  } else {
231  VOM_LOG(log_level_t::ERROR) << "nat-binding-input-44 no sw_if_index: "
232  << payload.sw_if_index;
233  }
234  }
235 
236  std::shared_ptr<nat_binding_cmds::dump_output_44_cmd> ocmd =
237  std::make_shared<nat_binding_cmds::dump_output_44_cmd>();
238 
239  HW::enqueue(ocmd);
240  HW::write();
241 
242  for (auto& record : *ocmd) {
243  auto& payload = record.get_payload();
244 
245  std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
246  if (itf) {
247  nat_binding nb(*itf, direction_t::OUTPUT, l3_proto_t::IPV4,
248  zone_t::from_vpp(payload.flags & NAT_IS_INSIDE));
249  OM::commit(key, nb);
250  } else {
251  VOM_LOG(log_level_t::ERROR) << "nat-binding-output-44 no sw_if_index: "
252  << payload.sw_if_index;
253  }
254  }
255 
256  std::shared_ptr<nat_binding_cmds::dump_input_66_cmd> i6cmd =
257  std::make_shared<nat_binding_cmds::dump_input_66_cmd>();
258 
259  HW::enqueue(i6cmd);
260  HW::write();
261 
262  for (auto& record : *i6cmd) {
263  auto& payload = record.get_payload();
264 
265  std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
266  if (itf) {
267  nat_binding nb(*itf, direction_t::INPUT, l3_proto_t::IPV6,
268  zone_t::from_vpp(payload.flags & NAT_IS_INSIDE));
269  OM::commit(key, nb);
270  } else {
271  VOM_LOG(log_level_t::ERROR) << "nat-binding-input-66 no sw_if_index: "
272  << payload.sw_if_index;
273  }
274  }
275 }
276 
278 nat_binding::event_handler::order() const
279 {
280  return (dependency_t::BINDING);
281 }
282 
283 void
284 nat_binding::event_handler::show(std::ostream& os)
285 {
286  db_dump(m_db, os);
287 }
288 }
289 
290 /*
291  * fd.io coding-style-patch-verification: OFF
292  *
293  * Local Variables:
294  * eval: (c-set-style "mozilla")
295  * End:
296  */
VOM::HW::write
static rc_t write()
Write/Execute all commands hitherto enqueued.
Definition: hw.cpp:255
VOM::nat_binding::dump
static void dump(std::ostream &os)
Dump all nat_bindings into the stream provided.
Definition: nat_binding.cpp:186
VOM::OM::commit
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
VOM_LOG
#define VOM_LOG(lvl)
Definition: logger.hpp:181
VOM
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
VOM::nat_binding::zone_t::OUTSIDE
const static zone_t OUTSIDE
Deny Zone.
Definition: nat_binding.hpp:56
VOM::inspect::register_handler
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
VOM::dependency_t
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
VOM::l3_proto_t::IPV4
const static l3_proto_t IPV4
Definition: prefix.hpp:55
VOM::nat_binding::zone_t::INSIDE
const static zone_t INSIDE
Permit Zone.
Definition: nat_binding.hpp:51
VOM::nat_binding::zone_t
NAT Zoness.
Definition: nat_binding.hpp:36
cmd.hpp
key
typedef key
Definition: ipsec_types.api:88
VOM::nat_binding::find
static std::shared_ptr< nat_binding > find(const key_t &key)
Static function to find the bridge_domain in the model.
Definition: nat_binding.cpp:174
VOM::db_dump
void db_dump(const DB &db, std::ostream &os)
Print each of the objects in the DB into the stream provided.
Definition: singular_db_funcs.hpp:35
VOM::nat_binding::key_t
std::tuple< interface::key_t, direction_t, l3_proto_t > key_t
The key for a NAT Binding.
Definition: nat_binding.hpp:66
VOM::OM::register_listener
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127
VOM::nat_binding
A Class representing the binding of an L2 interface to a bridge-domain and the properties of that bin...
Definition: nat_binding.hpp:30
VOM::nat_binding::to_string
std::string to_string() const
convert to string format for debug purposes
Definition: nat_binding.cpp:157
VOM::nat_binding::singular
std::shared_ptr< nat_binding > singular() const
Return the 'singular instance' of the L2 config that matches this object.
Definition: nat_binding.cpp:180
VOM::nat_binding::zone_t::from_vpp
const static zone_t & from_vpp(u8 is_inside)
Definition: nat_binding.cpp:34
VOM::enum_base
A template base class for all enum types.
Definition: enum_base.hpp:30
VOM::nat_binding::~nat_binding
~nat_binding()
Destructor.
Definition: nat_binding.cpp:65
VOM::direction_t::OUTPUT
const static direction_t OUTPUT
Deny Direction.
Definition: types.hpp:156
VOM::interface
A representation of an interface in VPP.
Definition: interface.hpp:41
VOM::client_db::key_t
const typedef 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
VOM::direction_t::INPUT
const static direction_t INPUT
Permit Direction.
Definition: types.hpp:151
VOM::HW::enqueue
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:212
VOM::operator<<
std::ostream & operator<<(std::ostream &os, const std::pair< direction_t, interface::key_t > &key)
Definition: acl_binding.cpp:201
false
#define false
Definition: cJSON.c:70
singular_db_funcs.hpp
VOM::enum_base::to_string
const std::string & to_string() const
convert to string format for debug purposes
Definition: enum_base.hpp:36
VOM::nat_binding::zone_t::zone_t
zone_t(int v, const std::string s)
Constructor.
Definition: nat_binding.cpp:29
VOM::l3_proto_t
An L3 protocol can be used to construct a prefix that is used to match packets are part of a route.
Definition: prefix.hpp:52
VOM::nat_binding_cmds::unbind_44_input_cmd
A cmd class that unbinds a NAT configuration from an input interface.
Definition: nat_binding_cmds.hpp:71
VOM::nat_binding::nat_binding
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:44
nat_binding_cmds.hpp
VOM::nat_binding::operator==
bool operator==(const nat_binding &n) const
Comparison operator - for UT.
Definition: nat_binding.cpp:78
VOM::nat_binding::key
const key_t key() const
Return the binding's key.
Definition: nat_binding.cpp:72
u8
unsigned char u8
Definition: types.h:56
VOM::direction_t
Feature Directions.
Definition: types.hpp:136
VOM::dependency_t::BINDING
@ BINDING
Then L2/objects that bind to interfaces, BD, ACLS, etc.
NAT_IS_INSIDE
@ NAT_IS_INSIDE
Definition: nat_types.api:44
show
void show(char *chroot_path, int verbose)
Definition: svmtool.c:104
event_handler
void event_handler(void *tls_async)
Definition: tls_async.c:334
proto
vl_api_ip_proto_t proto
Definition: acl_types.api:51
VOM::log_level_t::ERROR
const static log_level_t ERROR
Definition: logger.hpp:29
VOM::l3_proto_t::IPV6
const static l3_proto_t IPV6
Definition: prefix.hpp:56
nat_binding.hpp
VOM::interface::find
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:538
string
const char *const string
Definition: cJSON.h:172