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