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 #include "vom/nat_binding.hpp"
17 #include "vom/cmd.hpp"
18 #include "vom/nat_binding_cmds.hpp"
19 #include "vom/singular_db_funcs.hpp"
22 singular_db<const nat_binding::key_t, nat_binding> nat_binding::m_db;
24 nat_binding::event_handler nat_binding::m_evh;
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");
29 nat_binding::zone_t::zone_t(int v, const std::string s)
33 const nat_binding::zone_t&
34 nat_binding::zone_t::from_vpp(u8 is_inside)
37 return zone_t::INSIDE;
38 return zone_t::OUTSIDE;
42 * Construct a new object matching the desried state
44 nat_binding::nat_binding(const interface& itf,
45 const direction_t& dir,
46 const l3_proto_t& proto,
49 , m_itf(itf.singular())
56 nat_binding::nat_binding(const nat_binding& o)
57 : m_binding(o.m_binding)
65 nat_binding::~nat_binding()
68 m_db.release(key(), this);
71 const nat_binding::key_t
72 nat_binding::key() const
74 return (make_tuple(m_itf->key(), m_dir, m_proto));
78 nat_binding::operator==(const nat_binding& n) const
80 return ((key() == n.key()) && (m_zone == n.m_zone));
87 if (direction_t::INPUT == m_dir) {
88 if (l3_proto_t::IPV4 == m_proto) {
89 HW::enqueue(new nat_binding_cmds::unbind_44_input_cmd(
90 m_binding, m_itf->handle(), m_zone));
92 HW::enqueue(new nat_binding_cmds::unbind_66_input_cmd(
93 m_binding, m_itf->handle(), m_zone));
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));
100 VOM_LOG(log_level_t::ERROR) << "NAT 66 output feature not supported";
108 nat_binding::replay()
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));
116 HW::enqueue(new nat_binding_cmds::bind_66_input_cmd(
117 m_binding, m_itf->handle(), m_zone));
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));
124 VOM_LOG(log_level_t::ERROR) << "NAT 66 output feature not supported";
131 nat_binding::update(const nat_binding& desired)
134 * the desired state is always that the interface should be created
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));
142 HW::enqueue(new nat_binding_cmds::bind_66_input_cmd(
143 m_binding, m_itf->handle(), m_zone));
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));
150 VOM_LOG(log_level_t::ERROR) << "NAT 66 output feature not supported";
157 nat_binding::to_string() const
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() << "]";
167 std::shared_ptr<nat_binding>
168 nat_binding::find_or_add(const nat_binding& temp)
170 return (m_db.find_or_add(temp.key(), temp));
173 std::shared_ptr<nat_binding>
174 nat_binding::find(const key_t& key)
176 return (m_db.find(key));
179 std::shared_ptr<nat_binding>
180 nat_binding::singular() const
182 return find_or_add(*this);
186 nat_binding::dump(std::ostream& os)
192 operator<<(std::ostream& os, const nat_binding::key_t& key)
194 os << "[" << std::get<0>(key) << ", " << std::get<1>(key) << ", "
195 << std::get<2>(key) << "]";
200 nat_binding::event_handler::event_handler()
202 OM::register_listener(this);
203 inspect::register_handler({ "nat-binding" }, "NAT bindings", this);
207 nat_binding::event_handler::handle_replay()
213 nat_binding::event_handler::handle_populate(const client_db::key_t& key)
215 std::shared_ptr<nat_binding_cmds::dump_input_44_cmd> icmd =
216 std::make_shared<nat_binding_cmds::dump_input_44_cmd>();
221 for (auto& record : *icmd) {
222 auto& payload = record.get_payload();
224 std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
227 nat_binding nb(*itf, direction_t::INPUT, l3_proto_t::IPV4,
228 zone_t::from_vpp(payload.is_inside));
231 VOM_LOG(log_level_t::ERROR) << "nat-binding-input-44 no sw_if_index: "
232 << payload.sw_if_index;
236 std::shared_ptr<nat_binding_cmds::dump_output_44_cmd> ocmd =
237 std::make_shared<nat_binding_cmds::dump_output_44_cmd>();
242 for (auto& record : *ocmd) {
243 auto& payload = record.get_payload();
245 std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
247 nat_binding nb(*itf, direction_t::OUTPUT, l3_proto_t::IPV4,
248 zone_t::from_vpp(payload.is_inside));
251 VOM_LOG(log_level_t::ERROR) << "nat-binding-output-44 no sw_if_index: "
252 << payload.sw_if_index;
256 std::shared_ptr<nat_binding_cmds::dump_input_66_cmd> i6cmd =
257 std::make_shared<nat_binding_cmds::dump_input_66_cmd>();
262 for (auto& record : *i6cmd) {
263 auto& payload = record.get_payload();
265 std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
267 nat_binding nb(*itf, direction_t::INPUT, l3_proto_t::IPV6,
268 zone_t::from_vpp(payload.is_inside));
271 VOM_LOG(log_level_t::ERROR) << "nat-binding-input-66 no sw_if_index: "
272 << payload.sw_if_index;
278 nat_binding::event_handler::order() const
280 return (dependency_t::BINDING);
284 nat_binding::event_handler::show(std::ostream& os)
291 * fd.io coding-style-patch-verification: ON
294 * eval: (c-set-style "mozilla")