blob: eca3f9041f0b1b3c805e6118fc13d7fe8e1a9d09 [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_binding.hpp"
17#include "vom/cmd.hpp"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070018#include "vom/nat_binding_cmds.hpp"
Neale Ranns756cd942018-04-06 09:18:11 -070019#include "vom/singular_db_funcs.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070020
21namespace VOM {
22singular_db<const nat_binding::key_t, nat_binding> nat_binding::m_db;
23
24nat_binding::event_handler nat_binding::m_evh;
25
26const nat_binding::zone_t nat_binding::zone_t::INSIDE(0, "inside");
27const nat_binding::zone_t nat_binding::zone_t::OUTSIDE(0, "outside");
28
29nat_binding::zone_t::zone_t(int v, const std::string s)
30 : enum_base(v, s)
31{
32}
Neale Ranns041fa502017-12-20 08:49:51 -080033const nat_binding::zone_t&
34nat_binding::zone_t::from_vpp(u8 is_inside)
35{
36 if (is_inside)
37 return zone_t::INSIDE;
38 return zone_t::OUTSIDE;
39}
Neale Ranns812ed392017-10-16 04:20:13 -070040
41/**
42 * Construct a new object matching the desried state
43 */
44nat_binding::nat_binding(const interface& itf,
45 const direction_t& dir,
46 const l3_proto_t& proto,
47 const zone_t& zone)
48 : m_binding(false)
49 , m_itf(itf.singular())
50 , m_dir(dir)
51 , m_proto(proto)
52 , m_zone(zone)
53{
54}
55
56nat_binding::nat_binding(const nat_binding& o)
57 : m_binding(o.m_binding)
58 , m_itf(o.m_itf)
59 , m_dir(o.m_dir)
60 , m_proto(o.m_proto)
61 , m_zone(o.m_zone)
62{
63}
64
65nat_binding::~nat_binding()
66{
67 sweep();
Neale Ranns041fa502017-12-20 08:49:51 -080068 m_db.release(key(), this);
69}
70
71const nat_binding::key_t
72nat_binding::key() const
73{
74 return (make_tuple(m_itf->key(), m_dir, m_proto));
75}
76
77bool
78nat_binding::operator==(const nat_binding& n) const
79{
80 return ((key() == n.key()) && (m_zone == n.m_zone));
Neale Ranns812ed392017-10-16 04:20:13 -070081}
82
83void
84nat_binding::sweep()
85{
86 if (m_binding) {
87 if (direction_t::INPUT == m_dir) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070088 HW::enqueue(new nat_binding_cmds::unbind_44_input_cmd(
89 m_binding, m_itf->handle(), m_zone));
Neale Ranns812ed392017-10-16 04:20:13 -070090 } else {
Neale Ranns041fa502017-12-20 08:49:51 -080091 HW::enqueue(new nat_binding_cmds::unbind_44_output_cmd(
92 m_binding, m_itf->handle(), m_zone));
Neale Ranns812ed392017-10-16 04:20:13 -070093 }
94 }
95 HW::write();
96}
97
98void
99nat_binding::replay()
100{
101 if (m_binding) {
102 if (direction_t::INPUT == m_dir) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700103 HW::enqueue(new nat_binding_cmds::bind_44_input_cmd(
104 m_binding, m_itf->handle(), m_zone));
Neale Ranns812ed392017-10-16 04:20:13 -0700105 } else {
Neale Ranns041fa502017-12-20 08:49:51 -0800106 HW::enqueue(new nat_binding_cmds::bind_44_output_cmd(
107 m_binding, m_itf->handle(), m_zone));
Neale Ranns812ed392017-10-16 04:20:13 -0700108 }
109 }
110}
111
112void
113nat_binding::update(const nat_binding& desired)
114{
115 /*
116 * the desired state is always that the interface should be created
117 */
118 if (!m_binding) {
119 if (direction_t::INPUT == m_dir) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700120 HW::enqueue(new nat_binding_cmds::bind_44_input_cmd(
121 m_binding, m_itf->handle(), m_zone));
Neale Ranns812ed392017-10-16 04:20:13 -0700122 } else {
Neale Ranns842eb222018-01-04 01:11:44 -0800123 HW::enqueue(new nat_binding_cmds::bind_44_output_cmd(
Neale Ranns041fa502017-12-20 08:49:51 -0800124 m_binding, m_itf->handle(), m_zone));
Neale Ranns812ed392017-10-16 04:20:13 -0700125 }
126 }
127}
128
129std::string
130nat_binding::to_string() const
131{
132 std::ostringstream s;
Neale Ranns041fa502017-12-20 08:49:51 -0800133 s << "nat-binding:[" << m_itf->to_string()
134 << " direction:" << m_dir.to_string() << " proto:" << m_proto.to_string()
135 << " zone:" << m_zone.to_string() << "]";
Neale Ranns812ed392017-10-16 04:20:13 -0700136
137 return (s.str());
138}
139
140std::shared_ptr<nat_binding>
141nat_binding::find_or_add(const nat_binding& temp)
142{
Neale Ranns041fa502017-12-20 08:49:51 -0800143 return (m_db.find_or_add(temp.key(), temp));
144}
145
146std::shared_ptr<nat_binding>
147nat_binding::find(const key_t& key)
148{
149 return (m_db.find(key));
Neale Ranns812ed392017-10-16 04:20:13 -0700150}
151
152std::shared_ptr<nat_binding>
153nat_binding::singular() const
154{
155 return find_or_add(*this);
156}
157
158void
159nat_binding::dump(std::ostream& os)
160{
Neale Ranns756cd942018-04-06 09:18:11 -0700161 db_dump(m_db, os);
Neale Ranns812ed392017-10-16 04:20:13 -0700162}
163
164std::ostream&
165operator<<(std::ostream& os, const nat_binding::key_t& key)
166{
167 os << "[" << std::get<0>(key) << ", " << std::get<1>(key) << ", "
168 << std::get<2>(key) << "]";
169
170 return (os);
171}
172
173nat_binding::event_handler::event_handler()
174{
175 OM::register_listener(this);
176 inspect::register_handler({ "nat-binding" }, "NAT bindings", this);
177}
178
179void
180nat_binding::event_handler::handle_replay()
181{
182 m_db.replay();
183}
184
185void
186nat_binding::event_handler::handle_populate(const client_db::key_t& key)
187{
Neale Ranns041fa502017-12-20 08:49:51 -0800188 std::shared_ptr<nat_binding_cmds::dump_input_44_cmd> icmd =
189 std::make_shared<nat_binding_cmds::dump_input_44_cmd>();
190
191 HW::enqueue(icmd);
192 HW::write();
193
194 for (auto& record : *icmd) {
195 auto& payload = record.get_payload();
196
197 std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
198 nat_binding nb(*itf, direction_t::INPUT, l3_proto_t::IPV4,
199 zone_t::from_vpp(payload.is_inside));
200 OM::commit(key, nb);
201 }
202
203 std::shared_ptr<nat_binding_cmds::dump_output_44_cmd> ocmd =
204 std::make_shared<nat_binding_cmds::dump_output_44_cmd>();
205
206 HW::enqueue(ocmd);
207 HW::write();
208
209 for (auto& record : *ocmd) {
210 auto& payload = record.get_payload();
211
212 std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
213 nat_binding nb(*itf, direction_t::OUTPUT, l3_proto_t::IPV4,
214 zone_t::from_vpp(payload.is_inside));
215 OM::commit(key, nb);
216 }
Neale Ranns812ed392017-10-16 04:20:13 -0700217}
218
219dependency_t
220nat_binding::event_handler::order() const
221{
222 return (dependency_t::BINDING);
223}
224
225void
226nat_binding::event_handler::show(std::ostream& os)
227{
Neale Ranns756cd942018-04-06 09:18:11 -0700228 db_dump(m_db, os);
Neale Ranns812ed392017-10-16 04:20:13 -0700229}
230}
231
232/*
233 * fd.io coding-style-patch-verification: ON
234 *
235 * Local Variables:
236 * eval: (c-set-style "mozilla")
237 * End:
238 */