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"
21 singular_db<const nat_binding::key_t, nat_binding> nat_binding::m_db;
23 nat_binding::event_handler nat_binding::m_evh;
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");
28 nat_binding::zone_t::zone_t(int v, const std::string s)
32 const nat_binding::zone_t&
33 nat_binding::zone_t::from_vpp(u8 is_inside)
36 return zone_t::INSIDE;
37 return zone_t::OUTSIDE;
41 * Construct a new object matching the desried state
43 nat_binding::nat_binding(const interface& itf,
44 const direction_t& dir,
45 const l3_proto_t& proto,
48 , m_itf(itf.singular())
55 nat_binding::nat_binding(const nat_binding& o)
56 : m_binding(o.m_binding)
64 nat_binding::~nat_binding()
67 m_db.release(key(), this);
70 const nat_binding::key_t
71 nat_binding::key() const
73 return (make_tuple(m_itf->key(), m_dir, m_proto));
77 nat_binding::operator==(const nat_binding& n) const
79 return ((key() == n.key()) && (m_zone == n.m_zone));
86 if (direction_t::INPUT == m_dir) {
87 HW::enqueue(new nat_binding_cmds::unbind_44_input_cmd(
88 m_binding, m_itf->handle(), m_zone));
90 HW::enqueue(new nat_binding_cmds::unbind_44_output_cmd(
91 m_binding, m_itf->handle(), m_zone));
101 if (direction_t::INPUT == m_dir) {
102 HW::enqueue(new nat_binding_cmds::bind_44_input_cmd(
103 m_binding, m_itf->handle(), m_zone));
105 HW::enqueue(new nat_binding_cmds::bind_44_output_cmd(
106 m_binding, m_itf->handle(), m_zone));
112 nat_binding::update(const nat_binding& desired)
115 * the desired state is always that the interface should be created
118 if (direction_t::INPUT == m_dir) {
119 HW::enqueue(new nat_binding_cmds::bind_44_input_cmd(
120 m_binding, m_itf->handle(), m_zone));
122 HW::enqueue(new nat_binding_cmds::bind_44_output_cmd(
123 m_binding, m_itf->handle(), m_zone));
129 nat_binding::to_string() const
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() << "]";
139 std::shared_ptr<nat_binding>
140 nat_binding::find_or_add(const nat_binding& temp)
142 return (m_db.find_or_add(temp.key(), temp));
145 std::shared_ptr<nat_binding>
146 nat_binding::find(const key_t& key)
148 return (m_db.find(key));
151 std::shared_ptr<nat_binding>
152 nat_binding::singular() const
154 return find_or_add(*this);
158 nat_binding::dump(std::ostream& os)
164 operator<<(std::ostream& os, const nat_binding::key_t& key)
166 os << "[" << std::get<0>(key) << ", " << std::get<1>(key) << ", "
167 << std::get<2>(key) << "]";
172 nat_binding::event_handler::event_handler()
174 OM::register_listener(this);
175 inspect::register_handler({ "nat-binding" }, "NAT bindings", this);
179 nat_binding::event_handler::handle_replay()
185 nat_binding::event_handler::handle_populate(const client_db::key_t& key)
187 std::shared_ptr<nat_binding_cmds::dump_input_44_cmd> icmd =
188 std::make_shared<nat_binding_cmds::dump_input_44_cmd>();
193 for (auto& record : *icmd) {
194 auto& payload = record.get_payload();
196 std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
197 nat_binding nb(*itf, direction_t::INPUT, l3_proto_t::IPV4,
198 zone_t::from_vpp(payload.is_inside));
202 std::shared_ptr<nat_binding_cmds::dump_output_44_cmd> ocmd =
203 std::make_shared<nat_binding_cmds::dump_output_44_cmd>();
208 for (auto& record : *ocmd) {
209 auto& payload = record.get_payload();
211 std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
212 nat_binding nb(*itf, direction_t::OUTPUT, l3_proto_t::IPV4,
213 zone_t::from_vpp(payload.is_inside));
219 nat_binding::event_handler::order() const
221 return (dependency_t::BINDING);
225 nat_binding::event_handler::show(std::ostream& os)
232 * fd.io coding-style-patch-verification: ON
235 * eval: (c-set-style "mozilla")