Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 1 | /* |
| 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 Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 18 | #include "vom/nat_binding_cmds.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 19 | |
| 20 | namespace VOM { |
| 21 | singular_db<const nat_binding::key_t, nat_binding> nat_binding::m_db; |
| 22 | |
| 23 | nat_binding::event_handler nat_binding::m_evh; |
| 24 | |
| 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"); |
| 27 | |
| 28 | nat_binding::zone_t::zone_t(int v, const std::string s) |
| 29 | : enum_base(v, s) |
| 30 | { |
| 31 | } |
Neale Ranns | 041fa50 | 2017-12-20 08:49:51 -0800 | [diff] [blame] | 32 | const nat_binding::zone_t& |
| 33 | nat_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 Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 39 | |
| 40 | /** |
| 41 | * Construct a new object matching the desried state |
| 42 | */ |
| 43 | nat_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 | |
| 55 | nat_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 | |
| 64 | nat_binding::~nat_binding() |
| 65 | { |
| 66 | sweep(); |
Neale Ranns | 041fa50 | 2017-12-20 08:49:51 -0800 | [diff] [blame] | 67 | m_db.release(key(), this); |
| 68 | } |
| 69 | |
| 70 | const nat_binding::key_t |
| 71 | nat_binding::key() const |
| 72 | { |
| 73 | return (make_tuple(m_itf->key(), m_dir, m_proto)); |
| 74 | } |
| 75 | |
| 76 | bool |
| 77 | nat_binding::operator==(const nat_binding& n) const |
| 78 | { |
| 79 | return ((key() == n.key()) && (m_zone == n.m_zone)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void |
| 83 | nat_binding::sweep() |
| 84 | { |
| 85 | if (m_binding) { |
| 86 | if (direction_t::INPUT == m_dir) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 87 | HW::enqueue(new nat_binding_cmds::unbind_44_input_cmd( |
| 88 | m_binding, m_itf->handle(), m_zone)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 89 | } else { |
Neale Ranns | 041fa50 | 2017-12-20 08:49:51 -0800 | [diff] [blame] | 90 | HW::enqueue(new nat_binding_cmds::unbind_44_output_cmd( |
| 91 | m_binding, m_itf->handle(), m_zone)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | HW::write(); |
| 95 | } |
| 96 | |
| 97 | void |
| 98 | nat_binding::replay() |
| 99 | { |
| 100 | if (m_binding) { |
| 101 | if (direction_t::INPUT == m_dir) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 102 | HW::enqueue(new nat_binding_cmds::bind_44_input_cmd( |
| 103 | m_binding, m_itf->handle(), m_zone)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 104 | } else { |
Neale Ranns | 041fa50 | 2017-12-20 08:49:51 -0800 | [diff] [blame] | 105 | HW::enqueue(new nat_binding_cmds::bind_44_output_cmd( |
| 106 | m_binding, m_itf->handle(), m_zone)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | nat_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 Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 119 | HW::enqueue(new nat_binding_cmds::bind_44_input_cmd( |
| 120 | m_binding, m_itf->handle(), m_zone)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 121 | } else { |
Neale Ranns | 041fa50 | 2017-12-20 08:49:51 -0800 | [diff] [blame] | 122 | HW::enqueue(new nat_binding_cmds::bind_44_input_cmd( |
| 123 | m_binding, m_itf->handle(), m_zone)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | std::string |
| 129 | nat_binding::to_string() const |
| 130 | { |
| 131 | std::ostringstream s; |
Neale Ranns | 041fa50 | 2017-12-20 08:49:51 -0800 | [diff] [blame] | 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() << "]"; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 135 | |
| 136 | return (s.str()); |
| 137 | } |
| 138 | |
| 139 | std::shared_ptr<nat_binding> |
| 140 | nat_binding::find_or_add(const nat_binding& temp) |
| 141 | { |
Neale Ranns | 041fa50 | 2017-12-20 08:49:51 -0800 | [diff] [blame] | 142 | return (m_db.find_or_add(temp.key(), temp)); |
| 143 | } |
| 144 | |
| 145 | std::shared_ptr<nat_binding> |
| 146 | nat_binding::find(const key_t& key) |
| 147 | { |
| 148 | return (m_db.find(key)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | std::shared_ptr<nat_binding> |
| 152 | nat_binding::singular() const |
| 153 | { |
| 154 | return find_or_add(*this); |
| 155 | } |
| 156 | |
| 157 | void |
| 158 | nat_binding::dump(std::ostream& os) |
| 159 | { |
| 160 | m_db.dump(os); |
| 161 | } |
| 162 | |
| 163 | std::ostream& |
| 164 | operator<<(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 | |
| 172 | nat_binding::event_handler::event_handler() |
| 173 | { |
| 174 | OM::register_listener(this); |
| 175 | inspect::register_handler({ "nat-binding" }, "NAT bindings", this); |
| 176 | } |
| 177 | |
| 178 | void |
| 179 | nat_binding::event_handler::handle_replay() |
| 180 | { |
| 181 | m_db.replay(); |
| 182 | } |
| 183 | |
| 184 | void |
| 185 | nat_binding::event_handler::handle_populate(const client_db::key_t& key) |
| 186 | { |
Neale Ranns | 041fa50 | 2017-12-20 08:49:51 -0800 | [diff] [blame] | 187 | 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 Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | dependency_t |
| 219 | nat_binding::event_handler::order() const |
| 220 | { |
| 221 | return (dependency_t::BINDING); |
| 222 | } |
| 223 | |
| 224 | void |
| 225 | nat_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 | */ |