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