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_static.hpp"
17 #include "vom/nat_static_cmds.hpp"
18 #include "vom/singular_db_funcs.hpp"
21 singular_db<nat_static::key_t, nat_static> nat_static::m_db;
22 nat_static::event_handler nat_static::m_evh;
24 nat_static::nat_static(const boost::asio::ip::address& inside,
25 const boost::asio::ip::address& outside)
27 , m_rd(route_domain::get_default())
33 nat_static::nat_static(const route_domain& rd,
34 const boost::asio::ip::address& inside,
35 const boost::asio::ip::address& outside)
43 nat_static::nat_static(const nat_static& ns)
46 , m_inside(ns.m_inside)
47 , m_outside(ns.m_outside)
51 nat_static::~nat_static()
55 // not in the DB anymore.
56 m_db.release(key(), this);
59 const nat_static::key_t
60 nat_static::key() const
62 return (std::make_pair(m_rd->key(), m_outside));
66 nat_static::operator==(const nat_static& n) const
68 return ((key() == n.key()) && (m_inside == n.m_inside));
75 if (m_inside.is_v4()) {
76 HW::enqueue(new nat_static_cmds::delete_44_cmd(
77 m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside.to_v4()));
79 HW::enqueue(new nat_static_cmds::delete_66_cmd(
80 m_hw, m_rd->table_id(), m_inside.to_v6(), m_outside.to_v6()));
90 if (m_inside.is_v4()) {
91 HW::enqueue(new nat_static_cmds::create_44_cmd(
92 m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside.to_v4()));
94 HW::enqueue(new nat_static_cmds::create_66_cmd(
95 m_hw, m_rd->table_id(), m_inside.to_v6(), m_outside.to_v6()));
101 nat_static::update(const nat_static& r)
104 * create the table if it is not yet created
106 if (rc_t::OK != m_hw.rc()) {
107 if (m_inside.is_v4()) {
108 HW::enqueue(new nat_static_cmds::create_44_cmd(
109 m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside.to_v4()));
111 HW::enqueue(new nat_static_cmds::create_66_cmd(
112 m_hw, m_rd->table_id(), m_inside.to_v6(), m_outside.to_v6()));
118 nat_static::to_string() const
120 std::ostringstream s;
122 << "table:" << m_rd->to_string() << " inside:" << m_inside.to_string()
123 << " outside:" << m_outside.to_string() << "]";
128 std::shared_ptr<nat_static>
129 nat_static::find_or_add(const nat_static& temp)
131 return (m_db.find_or_add(temp.key(), temp));
134 std::shared_ptr<nat_static>
135 nat_static::find(const key_t& key)
137 return (m_db.find(key));
140 std::shared_ptr<nat_static>
141 nat_static::singular() const
143 return find_or_add(*this);
147 nat_static::dump(std::ostream& os)
152 nat_static::event_handler::event_handler()
154 OM::register_listener(this);
155 inspect::register_handler({ "nat-static" }, "NAT Statics", this);
159 nat_static::event_handler::handle_replay()
165 nat_static::event_handler::handle_populate(const client_db::key_t& key)
168 * dump VPP current states
170 std::shared_ptr<nat_static_cmds::dump_44_cmd> cmd44 =
171 std::make_shared<nat_static_cmds::dump_44_cmd>();
176 for (auto& record : *cmd44) {
178 auto& payload = record.get_payload();
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);
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
193 std::shared_ptr<nat_static_cmds::dump_66_cmd> cmd66 =
194 std::make_shared<nat_static_cmds::dump_66_cmd>();
199 for (auto& record : *cmd66) {
201 auto& payload = record.get_payload();
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);
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
218 nat_static::event_handler::order() const
220 return (dependency_t::ENTRY);
224 nat_static::event_handler::show(std::ostream& os)
231 * fd.io coding-style-patch-verification: ON
234 * eval: (c-set-style "mozilla")