FD.io VPP  v19.08.3-2-gbabecb413
Vector Packet Processing
nat_static.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_static.hpp"
17 #include "vom/nat_static_cmds.hpp"
19 
20 namespace VOM {
21 singular_db<nat_static::key_t, nat_static> nat_static::m_db;
22 nat_static::event_handler nat_static::m_evh;
23 
25  const boost::asio::ip::address& outside)
26  : m_hw(false)
27  , m_rd(route_domain::get_default())
28  , m_inside(inside)
29  , m_outside(outside)
30 {
31 }
32 
34  const boost::asio::ip::address& inside,
35  const boost::asio::ip::address& outside)
36  : m_hw(false)
37  , m_rd(rd.singular())
38  , m_inside(inside)
39  , m_outside(outside)
40 {
41 }
42 
44  : m_hw(ns.m_hw)
45  , m_rd(ns.m_rd)
46  , m_inside(ns.m_inside)
47  , m_outside(ns.m_outside)
48 {
49 }
50 
52 {
53  sweep();
54 
55  // not in the DB anymore.
56  m_db.release(key(), this);
57 }
58 
61 {
62  return (std::make_pair(m_rd->key(), m_outside));
63 }
64 
65 bool
67 {
68  return ((key() == n.key()) && (m_inside == n.m_inside));
69 }
70 
71 void
72 nat_static::sweep()
73 {
74  if (m_hw) {
75  if (m_inside.is_v4()) {
77  m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside.to_v4()));
78  } else {
80  m_hw, m_rd->table_id(), m_inside.to_v6(), m_outside.to_v6()));
81  }
82  }
83  HW::write();
84 }
85 
86 void
88 {
89  if (m_hw) {
90  if (m_inside.is_v4()) {
92  m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside.to_v4()));
93  } else {
95  m_hw, m_rd->table_id(), m_inside.to_v6(), m_outside.to_v6()));
96  }
97  }
98 }
99 
100 void
101 nat_static::update(const nat_static& r)
102 {
103  /*
104  * create the table if it is not yet created
105  */
106  if (rc_t::OK != m_hw.rc()) {
107  if (m_inside.is_v4()) {
109  m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside.to_v4()));
110  } else {
112  m_hw, m_rd->table_id(), m_inside.to_v6(), m_outside.to_v6()));
113  }
114  }
115 }
116 
117 std::string
119 {
120  std::ostringstream s;
121  s << "nat-static:["
122  << "table:" << m_rd->to_string() << " inside:" << m_inside.to_string()
123  << " outside:" << m_outside.to_string() << "]";
124 
125  return (s.str());
126 }
127 
128 std::shared_ptr<nat_static>
129 nat_static::find_or_add(const nat_static& temp)
130 {
131  return (m_db.find_or_add(temp.key(), temp));
132 }
133 
134 std::shared_ptr<nat_static>
136 {
137  return (m_db.find(key));
138 }
139 
140 std::shared_ptr<nat_static>
142 {
143  return find_or_add(*this);
144 }
145 
146 void
147 nat_static::dump(std::ostream& os)
148 {
149  db_dump(m_db, os);
150 }
151 
153 {
154  OM::register_listener(this);
155  inspect::register_handler({ "nat-static" }, "NAT Statics", this);
156 }
157 
158 void
159 nat_static::event_handler::handle_replay()
160 {
161  m_db.replay();
162 }
163 
164 void
165 nat_static::event_handler::handle_populate(const client_db::key_t& key)
166 {
167  /*
168  * dump VPP current states
169  */
170  std::shared_ptr<nat_static_cmds::dump_44_cmd> cmd44 =
171  std::make_shared<nat_static_cmds::dump_44_cmd>();
172 
173  HW::enqueue(cmd44);
174  HW::write();
175 
176  for (auto& record : *cmd44) {
177 
178  auto& payload = record.get_payload();
179 
180  boost::asio::ip::address inside = from_bytes(0, payload.local_ip_address);
181  boost::asio::ip::address outside =
182  from_bytes(0, payload.external_ip_address);
183  nat_static n(route_domain(payload.vrf_id), inside, outside);
184 
185  /*
186  * Write each of the discovered mappings into the OM,
187  * but disable the HW Command q whilst we do, so that no
188  * commands are sent to VPP
189  */
190  OM::commit(key, n);
191  }
192 
193  std::shared_ptr<nat_static_cmds::dump_66_cmd> cmd66 =
194  std::make_shared<nat_static_cmds::dump_66_cmd>();
195 
196  HW::enqueue(cmd66);
197  HW::write();
198 
199  for (auto& record : *cmd66) {
200 
201  auto& payload = record.get_payload();
202 
203  boost::asio::ip::address inside = from_bytes(1, payload.local_ip_address);
204  boost::asio::ip::address outside =
205  from_bytes(1, payload.external_ip_address);
206  nat_static n(route_domain(payload.vrf_id), inside, outside);
207 
208  /*
209  * Write each of the discovered mappings into the OM,
210  * but disable the HW Command q whilst we do, so that no
211  * commands are sent to VPP
212  */
213  OM::commit(key, n);
214  }
215 }
216 
218 nat_static::event_handler::order() const
219 {
220  return (dependency_t::ENTRY);
221 }
222 
223 void
224 nat_static::event_handler::show(std::ostream& os)
225 {
226  db_dump(m_db, os);
227 }
228 }
229 
230 /*
231  * fd.io coding-style-patch-verification: ON
232  *
233  * Local Variables:
234  * eval: (c-set-style "mozilla")
235  * End:
236  */
A cmd class that deletes a NAT 44 static mapping.
void replay(void)
replay the object to create it in hardware
Definition: nat_static.cpp:87
typedef address
Definition: ip_types.api:83
void db_dump(const DB &db, std::ostream &os)
Print each of the objects in the DB into the stream provided.
A command class that creates NAT 44 static mapping.
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
std::string to_string() const
Convert to string for debugging.
Definition: nat_static.cpp:118
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:255
rc_t rc() const
Get the HW return code.
Definition: hw.hpp:119
A route-domain is a VRF.
static void dump(std::ostream &os)
Dump all bridge_domain-doamin into the stream provided.
Definition: nat_static.cpp:147
A entry in the ARP termination table of a Bridge Domain.
Definition: nat_static.hpp:27
bool operator==(const nat_static &n) const
Comparison operator - for UT.
Definition: nat_static.cpp:66
nat_static(const boost::asio::ip::address &inside, const boost::asio::ip::address &outside)
Construct an NAT Static binding with the outside address in default table.
Definition: nat_static.cpp:24
std::shared_ptr< nat_static > singular() const
Return the matching &#39;singular instance&#39;.
Definition: nat_static.cpp:141
const key_t key() const
Return the object&#39;s key.
Definition: nat_static.cpp:60
A cmd class that deletes a NAT 66 static mapping.
std::pair< route::table_id_t, boost::asio::ip::address > key_t
The key for a NAT static mapping.
Definition: nat_static.hpp:35
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
void event_handler(void *tls_async)
Definition: tls_async.c:338
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 const rc_t OK
The HW write was successfull.
Definition: types.hpp:109
static void enqueue(cmd *f)
Enqueue A command for execution.
Definition: hw.cpp:212
boost::asio::ip::address from_bytes(uint8_t is_ip6, const uint8_t *bytes)
Convert a VPP byte stinrg into a boost addresss.
Definition: prefix.cpp:224
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
static std::shared_ptr< nat_static > find(const key_t &key)
Find the instnace of the bridge_domain domain in the OM.
Definition: nat_static.cpp:135
void show(char *chroot_path, int verbose)
Definition: svmtool.c:105
Entries in Tables.
A command class that creates NAT 66 static mapping.
~nat_static()
Destructor.
Definition: nat_static.cpp:51
static bool register_listener(listener *listener)
Register a listener of events.
Definition: om.cpp:127