blob: 619cc3b6602f88536e359231cbc2e89cda86cc1f [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070017#include "vom/nat_static_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070018
19namespace VOM {
20singular_db<nat_static::key_t, nat_static> nat_static::m_db;
21nat_static::event_handler nat_static::m_evh;
22
23nat_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
32nat_static::nat_static(const route_domain& rd,
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
42nat_static::nat_static(const nat_static& ns)
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
50nat_static::~nat_static()
51{
52 sweep();
53
54 // not in the DB anymore.
55 m_db.release(std::make_pair(m_rd->key(), m_outside), this);
56}
57
58void
59nat_static::sweep()
60{
61 if (m_hw) {
62 if (m_inside.is_v4()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070063 HW::enqueue(new nat_static_cmds::delete_44_cmd(
64 m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside));
Neale Ranns812ed392017-10-16 04:20:13 -070065 }
66 }
67 HW::write();
68}
69
70void
71nat_static::replay()
72{
73 if (m_hw) {
74 if (m_inside.is_v4()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070075 HW::enqueue(new nat_static_cmds::create_44_cmd(
76 m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside));
Neale Ranns812ed392017-10-16 04:20:13 -070077 }
78 }
79}
80
81void
82nat_static::update(const nat_static& r)
83{
84 /*
85 * create the table if it is not yet created
86 */
87 if (rc_t::OK != m_hw.rc()) {
88 if (m_inside.is_v4()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070089 HW::enqueue(new nat_static_cmds::create_44_cmd(
90 m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside));
Neale Ranns812ed392017-10-16 04:20:13 -070091 }
92 }
93}
94
95std::string
96nat_static::to_string() const
97{
98 std::ostringstream s;
99 s << "nat-static:["
100 << "table:" << m_rd->to_string() << " inside: " << m_inside.to_string()
101 << " outside " << m_outside.to_string() << "]";
102
103 return (s.str());
104}
105
106std::shared_ptr<nat_static>
107nat_static::find_or_add(const nat_static& temp)
108{
109 return (
110 m_db.find_or_add(std::make_pair(temp.m_rd->key(), temp.m_outside), temp));
111}
112
113std::shared_ptr<nat_static>
114nat_static::singular() const
115{
116 return find_or_add(*this);
117}
118
119void
120nat_static::dump(std::ostream& os)
121{
122 m_db.dump(os);
123}
124
125std::ostream&
126operator<<(std::ostream& os, const nat_static::key_t& key)
127{
128 os << "[" << key.first << ", " << key.second << "]";
129
130 return (os);
131}
132
133nat_static::event_handler::event_handler()
134{
135 OM::register_listener(this);
136 inspect::register_handler({ "nat-static" }, "NAT Statics", this);
137}
138
139void
140nat_static::event_handler::handle_replay()
141{
142 m_db.replay();
143}
144
145/* void nat_static::populate_i(const client_db::key_t &key, */
146/* std::shared_ptr<interface> itf, */
147/* const l3_proto_t &proto) */
148/* { */
149/* /\* */
150/* * dump VPP current states */
151/* *\/ */
152/* std::shared_ptr<nat_static::dump_cmd> cmd = */
153/* std::make_shared<nat_static::dump_cmd>(nat_static::dump_cmd(itf->handle(),
154 * proto)); */
155
156/* HW::enqueue(cmd); */
157/* HW::write(); */
158
159/* for (auto & record : *cmd) */
160/* { */
161/* /\* */
162/* * construct a nat_static from each recieved record. */
163/* *\/ */
164/* auto &payload = record.get_payload(); */
165
166/* mac_address_t mac(payload.mac_address); */
167/* boost::asio::ip::address ip_addr = from_bytes(payload.is_ipv6, */
168/* payload.ip_address);
169 */
170/* nat_static n(*itf, mac, ip_addr); */
171
172/* VOM_LOG(log_level_t::DEBUG) << "nat_static-dump: " */
173/* << itf->to_string() */
174/* << mac.to_string() */
175/* << ip_addr.to_string(); */
176
177/* /\* */
178/* * Write each of the discovered interfaces into the OM, */
179/* * but disable the HW Command q whilst we do, so that no */
180/* * commands are sent to VPP */
181/* *\/ */
182/* OM::commit(key, n); */
183/* } */
184/* } */
185
186void
187nat_static::event_handler::handle_populate(const client_db::key_t& key)
188{
189 /* auto it = interface::cbegin(); */
190
191 /* while (it != interface::cend()) */
192 /* { */
193 /* nat_static::populate_i(key, it->second.lock(), l3_proto_t::IPV4);
194 */
195 /* nat_static::populate_i(key, it->second.lock(), l3_proto_t::IPV6);
196 */
197
198 /* ++it; */
199 /* } */
200}
201
202dependency_t
203nat_static::event_handler::order() const
204{
205 return (dependency_t::ENTRY);
206}
207
208void
209nat_static::event_handler::show(std::ostream& os)
210{
211 m_db.dump(os);
212}
213}
214
215/*
216 * fd.io coding-style-patch-verification: ON
217 *
218 * Local Variables:
219 * eval: (c-set-style "mozilla")
220 * End:
221 */