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