blob: a2e71b3a085566eee94963f2f546a150a8de613c [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 Ranns812ed392017-10-16 04:20:13 -070019
20namespace VOM {
21singular_db<const nat_binding::key_t, nat_binding> nat_binding::m_db;
22
23nat_binding::event_handler nat_binding::m_evh;
24
25const nat_binding::zone_t nat_binding::zone_t::INSIDE(0, "inside");
26const nat_binding::zone_t nat_binding::zone_t::OUTSIDE(0, "outside");
27
28nat_binding::zone_t::zone_t(int v, const std::string s)
29 : enum_base(v, s)
30{
31}
Neale Ranns041fa502017-12-20 08:49:51 -080032const nat_binding::zone_t&
33nat_binding::zone_t::from_vpp(u8 is_inside)
34{
35 if (is_inside)
36 return zone_t::INSIDE;
37 return zone_t::OUTSIDE;
38}
Neale Ranns812ed392017-10-16 04:20:13 -070039
40/**
41 * Construct a new object matching the desried state
42 */
43nat_binding::nat_binding(const interface& itf,
44 const direction_t& dir,
45 const l3_proto_t& proto,
46 const zone_t& zone)
47 : m_binding(false)
48 , m_itf(itf.singular())
49 , m_dir(dir)
50 , m_proto(proto)
51 , m_zone(zone)
52{
53}
54
55nat_binding::nat_binding(const nat_binding& o)
56 : m_binding(o.m_binding)
57 , m_itf(o.m_itf)
58 , m_dir(o.m_dir)
59 , m_proto(o.m_proto)
60 , m_zone(o.m_zone)
61{
62}
63
64nat_binding::~nat_binding()
65{
66 sweep();
Neale Ranns041fa502017-12-20 08:49:51 -080067 m_db.release(key(), this);
68}
69
70const nat_binding::key_t
71nat_binding::key() const
72{
73 return (make_tuple(m_itf->key(), m_dir, m_proto));
74}
75
76bool
77nat_binding::operator==(const nat_binding& n) const
78{
79 return ((key() == n.key()) && (m_zone == n.m_zone));
Neale Ranns812ed392017-10-16 04:20:13 -070080}
81
82void
83nat_binding::sweep()
84{
85 if (m_binding) {
86 if (direction_t::INPUT == m_dir) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070087 HW::enqueue(new nat_binding_cmds::unbind_44_input_cmd(
88 m_binding, m_itf->handle(), m_zone));
Neale Ranns812ed392017-10-16 04:20:13 -070089 } else {
Neale Ranns041fa502017-12-20 08:49:51 -080090 HW::enqueue(new nat_binding_cmds::unbind_44_output_cmd(
91 m_binding, m_itf->handle(), m_zone));
Neale Ranns812ed392017-10-16 04:20:13 -070092 }
93 }
94 HW::write();
95}
96
97void
98nat_binding::replay()
99{
100 if (m_binding) {
101 if (direction_t::INPUT == m_dir) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700102 HW::enqueue(new nat_binding_cmds::bind_44_input_cmd(
103 m_binding, m_itf->handle(), m_zone));
Neale Ranns812ed392017-10-16 04:20:13 -0700104 } else {
Neale Ranns041fa502017-12-20 08:49:51 -0800105 HW::enqueue(new nat_binding_cmds::bind_44_output_cmd(
106 m_binding, m_itf->handle(), m_zone));
Neale Ranns812ed392017-10-16 04:20:13 -0700107 }
108 }
109}
110
111void
112nat_binding::update(const nat_binding& desired)
113{
114 /*
115 * the desired state is always that the interface should be created
116 */
117 if (!m_binding) {
118 if (direction_t::INPUT == m_dir) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700119 HW::enqueue(new nat_binding_cmds::bind_44_input_cmd(
120 m_binding, m_itf->handle(), m_zone));
Neale Ranns812ed392017-10-16 04:20:13 -0700121 } else {
Neale Ranns041fa502017-12-20 08:49:51 -0800122 HW::enqueue(new nat_binding_cmds::bind_44_input_cmd(
123 m_binding, m_itf->handle(), m_zone));
Neale Ranns812ed392017-10-16 04:20:13 -0700124 }
125 }
126}
127
128std::string
129nat_binding::to_string() const
130{
131 std::ostringstream s;
Neale Ranns041fa502017-12-20 08:49:51 -0800132 s << "nat-binding:[" << m_itf->to_string()
133 << " direction:" << m_dir.to_string() << " proto:" << m_proto.to_string()
134 << " zone:" << m_zone.to_string() << "]";
Neale Ranns812ed392017-10-16 04:20:13 -0700135
136 return (s.str());
137}
138
139std::shared_ptr<nat_binding>
140nat_binding::find_or_add(const nat_binding& temp)
141{
Neale Ranns041fa502017-12-20 08:49:51 -0800142 return (m_db.find_or_add(temp.key(), temp));
143}
144
145std::shared_ptr<nat_binding>
146nat_binding::find(const key_t& key)
147{
148 return (m_db.find(key));
Neale Ranns812ed392017-10-16 04:20:13 -0700149}
150
151std::shared_ptr<nat_binding>
152nat_binding::singular() const
153{
154 return find_or_add(*this);
155}
156
157void
158nat_binding::dump(std::ostream& os)
159{
160 m_db.dump(os);
161}
162
163std::ostream&
164operator<<(std::ostream& os, const nat_binding::key_t& key)
165{
166 os << "[" << std::get<0>(key) << ", " << std::get<1>(key) << ", "
167 << std::get<2>(key) << "]";
168
169 return (os);
170}
171
172nat_binding::event_handler::event_handler()
173{
174 OM::register_listener(this);
175 inspect::register_handler({ "nat-binding" }, "NAT bindings", this);
176}
177
178void
179nat_binding::event_handler::handle_replay()
180{
181 m_db.replay();
182}
183
184void
185nat_binding::event_handler::handle_populate(const client_db::key_t& key)
186{
Neale Ranns041fa502017-12-20 08:49:51 -0800187 std::shared_ptr<nat_binding_cmds::dump_input_44_cmd> icmd =
188 std::make_shared<nat_binding_cmds::dump_input_44_cmd>();
189
190 HW::enqueue(icmd);
191 HW::write();
192
193 for (auto& record : *icmd) {
194 auto& payload = record.get_payload();
195
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));
199 OM::commit(key, nb);
200 }
201
202 std::shared_ptr<nat_binding_cmds::dump_output_44_cmd> ocmd =
203 std::make_shared<nat_binding_cmds::dump_output_44_cmd>();
204
205 HW::enqueue(ocmd);
206 HW::write();
207
208 for (auto& record : *ocmd) {
209 auto& payload = record.get_payload();
210
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));
214 OM::commit(key, nb);
215 }
Neale Ranns812ed392017-10-16 04:20:13 -0700216}
217
218dependency_t
219nat_binding::event_handler::order() const
220{
221 return (dependency_t::BINDING);
222}
223
224void
225nat_binding::event_handler::show(std::ostream& os)
226{
227 m_db.dump(os);
228}
229}
230
231/*
232 * fd.io coding-style-patch-verification: ON
233 *
234 * Local Variables:
235 * eval: (c-set-style "mozilla")
236 * End:
237 */