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_static.hpp" |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 17 | #include "vom/nat_static_cmds.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 18 | |
| 19 | namespace VOM { |
| 20 | singular_db<nat_static::key_t, nat_static> nat_static::m_db; |
| 21 | nat_static::event_handler nat_static::m_evh; |
| 22 | |
| 23 | nat_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 | |
| 32 | nat_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 | |
| 42 | nat_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 | |
| 50 | nat_static::~nat_static() |
| 51 | { |
| 52 | sweep(); |
| 53 | |
| 54 | // not in the DB anymore. |
Neale Ranns | 041fa50 | 2017-12-20 08:49:51 -0800 | [diff] [blame] | 55 | m_db.release(key(), this); |
| 56 | } |
| 57 | |
| 58 | const nat_static::key_t |
| 59 | nat_static::key() const |
| 60 | { |
| 61 | return (std::make_pair(m_rd->key(), m_outside)); |
| 62 | } |
| 63 | |
| 64 | bool |
| 65 | nat_static::operator==(const nat_static& n) const |
| 66 | { |
| 67 | return ((key() == n.key()) && (m_inside == n.m_inside)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | void |
| 71 | nat_static::sweep() |
| 72 | { |
| 73 | if (m_hw) { |
| 74 | if (m_inside.is_v4()) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 75 | HW::enqueue(new nat_static_cmds::delete_44_cmd( |
| 76 | m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 77 | } |
| 78 | } |
| 79 | HW::write(); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | nat_static::replay() |
| 84 | { |
| 85 | if (m_hw) { |
| 86 | if (m_inside.is_v4()) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 87 | HW::enqueue(new nat_static_cmds::create_44_cmd( |
| 88 | m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | nat_static::update(const nat_static& r) |
| 95 | { |
| 96 | /* |
| 97 | * create the table if it is not yet created |
| 98 | */ |
| 99 | if (rc_t::OK != m_hw.rc()) { |
| 100 | if (m_inside.is_v4()) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 101 | HW::enqueue(new nat_static_cmds::create_44_cmd( |
| 102 | m_hw, m_rd->table_id(), m_inside.to_v4(), m_outside)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | std::string |
| 108 | nat_static::to_string() const |
| 109 | { |
| 110 | std::ostringstream s; |
| 111 | s << "nat-static:[" |
Neale Ranns | 041fa50 | 2017-12-20 08:49:51 -0800 | [diff] [blame] | 112 | << "table:" << m_rd->to_string() << " inside:" << m_inside.to_string() |
| 113 | << " outside:" << m_outside.to_string() << "]"; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 114 | |
| 115 | return (s.str()); |
| 116 | } |
| 117 | |
| 118 | std::shared_ptr<nat_static> |
| 119 | nat_static::find_or_add(const nat_static& temp) |
| 120 | { |
Neale Ranns | 041fa50 | 2017-12-20 08:49:51 -0800 | [diff] [blame] | 121 | return (m_db.find_or_add(temp.key(), temp)); |
| 122 | } |
| 123 | |
| 124 | std::shared_ptr<nat_static> |
| 125 | nat_static::find(const key_t& key) |
| 126 | { |
| 127 | return (m_db.find(key)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | std::shared_ptr<nat_static> |
| 131 | nat_static::singular() const |
| 132 | { |
| 133 | return find_or_add(*this); |
| 134 | } |
| 135 | |
| 136 | void |
| 137 | nat_static::dump(std::ostream& os) |
| 138 | { |
| 139 | m_db.dump(os); |
| 140 | } |
| 141 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 142 | nat_static::event_handler::event_handler() |
| 143 | { |
| 144 | OM::register_listener(this); |
| 145 | inspect::register_handler({ "nat-static" }, "NAT Statics", this); |
| 146 | } |
| 147 | |
| 148 | void |
| 149 | nat_static::event_handler::handle_replay() |
| 150 | { |
| 151 | m_db.replay(); |
| 152 | } |
| 153 | |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 154 | void |
| 155 | nat_static::event_handler::handle_populate(const client_db::key_t& key) |
| 156 | { |
Neale Ranns | 041fa50 | 2017-12-20 08:49:51 -0800 | [diff] [blame] | 157 | /* |
| 158 | * dump VPP current states |
| 159 | */ |
| 160 | std::shared_ptr<nat_static_cmds::dump_44_cmd> cmd = |
| 161 | std::make_shared<nat_static_cmds::dump_44_cmd>(); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 162 | |
Neale Ranns | 041fa50 | 2017-12-20 08:49:51 -0800 | [diff] [blame] | 163 | HW::enqueue(cmd); |
| 164 | HW::write(); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 165 | |
Neale Ranns | 041fa50 | 2017-12-20 08:49:51 -0800 | [diff] [blame] | 166 | for (auto& record : *cmd) { |
| 167 | |
| 168 | auto& payload = record.get_payload(); |
| 169 | |
| 170 | boost::asio::ip::address inside = from_bytes(0, payload.local_ip_address); |
| 171 | boost::asio::ip::address outside = |
| 172 | from_bytes(0, payload.external_ip_address); |
| 173 | nat_static n(route_domain(payload.vrf_id), inside, outside.to_v4()); |
| 174 | |
| 175 | /* |
| 176 | * Write each of the discovered mappings into the OM, |
| 177 | * but disable the HW Command q whilst we do, so that no |
| 178 | * commands are sent to VPP |
| 179 | */ |
| 180 | OM::commit(key, n); |
| 181 | } |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | dependency_t |
| 185 | nat_static::event_handler::order() const |
| 186 | { |
| 187 | return (dependency_t::ENTRY); |
| 188 | } |
| 189 | |
| 190 | void |
| 191 | nat_static::event_handler::show(std::ostream& os) |
| 192 | { |
| 193 | m_db.dump(os); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * fd.io coding-style-patch-verification: ON |
| 199 | * |
| 200 | * Local Variables: |
| 201 | * eval: (c-set-style "mozilla") |
| 202 | * End: |
| 203 | */ |