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