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/interface.hpp" |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 17 | #include "vom/interface_cmds.hpp" |
| 18 | #include "vom/interface_factory.hpp" |
| 19 | #include "vom/l3_binding_cmds.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 20 | #include "vom/logger.hpp" |
| 21 | #include "vom/prefix.hpp" |
| 22 | |
| 23 | namespace VOM { |
| 24 | /** |
| 25 | * A DB of all the interfaces, key on the name |
| 26 | */ |
| 27 | singular_db<const std::string, interface> interface::m_db; |
| 28 | |
| 29 | /** |
| 30 | * A DB of all the interfaces, key on VPP's handle |
| 31 | */ |
| 32 | std::map<handle_t, std::weak_ptr<interface>> interface::m_hdl_db; |
| 33 | |
| 34 | interface::event_handler interface::m_evh; |
| 35 | |
| 36 | /** |
| 37 | * Construct a new object matching the desried state |
| 38 | */ |
| 39 | interface::interface(const std::string& name, |
| 40 | interface::type_t itf_type, |
| 41 | interface::admin_state_t itf_state) |
| 42 | : m_hdl(handle_t::INVALID) |
| 43 | , m_name(name) |
| 44 | , m_type(itf_type) |
| 45 | , m_state(itf_state) |
| 46 | , m_table_id(route::DEFAULT_TABLE) |
| 47 | , m_l2_address(l2_address_t::ZERO, rc_t::UNSET) |
| 48 | , m_oper(oper_state_t::DOWN) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | interface::interface(const handle_t& handle, |
| 53 | const l2_address_t& l2_address, |
| 54 | const std::string& name, |
| 55 | interface::type_t type, |
| 56 | interface::admin_state_t state) |
| 57 | : m_hdl(handle) |
| 58 | , m_name(name) |
| 59 | , m_type(type) |
| 60 | , m_state(state) |
| 61 | , m_table_id(route::DEFAULT_TABLE) |
| 62 | , m_l2_address(l2_address) |
| 63 | , m_oper(oper_state_t::DOWN) |
| 64 | { |
| 65 | } |
| 66 | |
| 67 | interface::interface(const std::string& name, |
| 68 | interface::type_t itf_type, |
| 69 | interface::admin_state_t itf_state, |
| 70 | const route_domain& rd) |
| 71 | : m_hdl(handle_t::INVALID) |
| 72 | , m_name(name) |
| 73 | , m_type(itf_type) |
| 74 | , m_rd(rd.singular()) |
| 75 | , m_state(itf_state) |
| 76 | , m_table_id(m_rd->table_id()) |
| 77 | , m_l2_address(l2_address_t::ZERO, rc_t::UNSET) |
| 78 | , m_oper(oper_state_t::DOWN) |
| 79 | { |
| 80 | } |
| 81 | |
| 82 | interface::interface(const interface& o) |
| 83 | : m_hdl(o.m_hdl) |
| 84 | , m_name(o.m_name) |
| 85 | , m_type(o.m_type) |
| 86 | , m_rd(o.m_rd) |
| 87 | , m_state(o.m_state) |
| 88 | , m_table_id(o.m_table_id) |
| 89 | , m_l2_address(o.m_l2_address) |
| 90 | , m_oper(o.m_oper) |
| 91 | { |
| 92 | } |
| 93 | |
| 94 | interface::event_listener::event_listener() |
| 95 | : m_status(rc_t::NOOP) |
| 96 | { |
| 97 | } |
| 98 | |
| 99 | HW::item<bool>& |
| 100 | interface::event_listener::status() |
| 101 | { |
| 102 | return (m_status); |
| 103 | } |
| 104 | |
| 105 | interface::stat_listener::stat_listener() |
| 106 | : m_status(rc_t::NOOP) |
| 107 | { |
| 108 | } |
| 109 | |
| 110 | HW::item<bool>& |
| 111 | interface::stat_listener::status() |
| 112 | { |
| 113 | return (m_status); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Return the interface type |
| 118 | */ |
| 119 | const interface::type_t& |
| 120 | interface::type() const |
| 121 | { |
| 122 | return (m_type); |
| 123 | } |
| 124 | |
| 125 | const handle_t& |
| 126 | interface::handle() const |
| 127 | { |
| 128 | return (m_hdl.data()); |
| 129 | } |
| 130 | |
| 131 | const l2_address_t& |
| 132 | interface::l2_address() const |
| 133 | { |
| 134 | return (m_l2_address.data()); |
| 135 | } |
| 136 | |
| 137 | interface::const_iterator_t |
| 138 | interface::cbegin() |
| 139 | { |
| 140 | return m_db.cbegin(); |
| 141 | } |
| 142 | |
| 143 | interface::const_iterator_t |
| 144 | interface::cend() |
| 145 | { |
| 146 | return m_db.cend(); |
| 147 | } |
| 148 | |
| 149 | void |
| 150 | interface::sweep() |
| 151 | { |
| 152 | if (m_table_id) { |
| 153 | m_table_id.data() = route::DEFAULT_TABLE; |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 154 | HW::enqueue( |
| 155 | new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl)); |
| 156 | HW::enqueue( |
| 157 | new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // If the interface is up, bring it down |
| 161 | if (m_state && interface::admin_state_t::UP == m_state.data()) { |
| 162 | m_state.data() = interface::admin_state_t::DOWN; |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 163 | HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 164 | } |
| 165 | if (m_hdl) { |
| 166 | std::queue<cmd*> cmds; |
| 167 | HW::enqueue(mk_delete_cmd(cmds)); |
| 168 | } |
| 169 | HW::write(); |
| 170 | } |
| 171 | |
| 172 | void |
| 173 | interface::replay() |
| 174 | { |
| 175 | if (m_hdl) { |
| 176 | std::queue<cmd*> cmds; |
| 177 | HW::enqueue(mk_create_cmd(cmds)); |
| 178 | } |
| 179 | |
| 180 | if (m_state && interface::admin_state_t::UP == m_state.data()) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 181 | HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | if (m_table_id) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 185 | HW::enqueue( |
| 186 | new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl)); |
| 187 | HW::enqueue( |
| 188 | new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | |
| 192 | interface::~interface() |
| 193 | { |
| 194 | sweep(); |
| 195 | release(); |
| 196 | } |
| 197 | |
| 198 | void |
| 199 | interface::release() |
| 200 | { |
| 201 | // not in the DB anymore. |
| 202 | m_db.release(m_name, this); |
| 203 | } |
| 204 | |
| 205 | std::string |
| 206 | interface::to_string() const |
| 207 | { |
| 208 | std::ostringstream s; |
| 209 | s << "interface:[" << m_name << " type:" << m_type.to_string() |
| 210 | << " hdl:" << m_hdl.to_string() |
| 211 | << " l2-address:" << m_l2_address.to_string(); |
| 212 | |
| 213 | if (m_rd) { |
| 214 | s << " rd:" << m_rd->to_string(); |
| 215 | } |
| 216 | |
| 217 | s << " admin-state:" << m_state.to_string() |
| 218 | << " oper-state:" << m_oper.to_string() << "]"; |
| 219 | |
| 220 | return (s.str()); |
| 221 | } |
| 222 | |
| 223 | const std::string& |
| 224 | interface::name() const |
| 225 | { |
| 226 | return (m_name); |
| 227 | } |
| 228 | |
| 229 | const interface::key_type& |
| 230 | interface::key() const |
| 231 | { |
| 232 | return (name()); |
| 233 | } |
| 234 | |
| 235 | std::queue<cmd*>& |
| 236 | interface::mk_create_cmd(std::queue<cmd*>& q) |
| 237 | { |
| 238 | if (type_t::LOOPBACK == m_type) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 239 | q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 240 | } else if (type_t::BVI == m_type) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 241 | q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name)); |
| 242 | q.push(new interface_cmds::set_tag(m_hdl, m_name)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 243 | } else if (type_t::AFPACKET == m_type) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 244 | q.push(new interface_cmds::af_packet_create_cmd(m_hdl, m_name)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 245 | } else if (type_t::TAP == m_type) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 246 | q.push(new interface_cmds::tap_create_cmd(m_hdl, m_name)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | return (q); |
| 250 | } |
| 251 | |
| 252 | std::queue<cmd*>& |
| 253 | interface::mk_delete_cmd(std::queue<cmd*>& q) |
| 254 | { |
| 255 | if ((type_t::LOOPBACK == m_type) || (type_t::BVI == m_type)) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 256 | q.push(new interface_cmds::loopback_delete_cmd(m_hdl)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 257 | } else if (type_t::AFPACKET == m_type) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 258 | q.push(new interface_cmds::af_packet_delete_cmd(m_hdl, m_name)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 259 | } else if (type_t::TAP == m_type) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 260 | q.push(new interface_cmds::tap_delete_cmd(m_hdl)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 261 | } |
| 262 | |
| 263 | return (q); |
| 264 | } |
| 265 | |
| 266 | void |
| 267 | interface::update(const interface& desired) |
| 268 | { |
| 269 | /* |
| 270 | * the desired state is always that the interface should be created |
| 271 | */ |
| 272 | if (rc_t::OK != m_hdl.rc()) { |
| 273 | std::queue<cmd*> cmds; |
| 274 | HW::enqueue(mk_create_cmd(cmds)); |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * change the interface state to that which is deisred |
| 279 | */ |
| 280 | if (m_state.update(desired.m_state)) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 281 | HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* |
| 285 | * change the interface state to that which is deisred |
| 286 | */ |
| 287 | if (m_l2_address.update(desired.m_l2_address)) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 288 | HW::enqueue(new interface_cmds::set_mac_cmd(m_l2_address, m_hdl)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | /* |
| 292 | * If the interface is mapped into a route domain, set VPP's |
| 293 | * table ID |
| 294 | */ |
| 295 | if (!m_table_id && m_rd) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 296 | HW::enqueue( |
| 297 | new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl)); |
| 298 | HW::enqueue( |
| 299 | new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 300 | } |
| 301 | } |
| 302 | |
| 303 | void |
| 304 | interface::set(const l2_address_t& addr) |
| 305 | { |
| 306 | assert(rc_t::UNSET == m_l2_address.rc()); |
| 307 | m_l2_address.set(rc_t::NOOP); |
| 308 | m_l2_address.update(addr); |
| 309 | } |
| 310 | |
| 311 | void |
| 312 | interface::set(const oper_state_t& state) |
| 313 | { |
| 314 | m_oper = state; |
| 315 | } |
| 316 | |
| 317 | std::shared_ptr<interface> |
| 318 | interface::singular_i() const |
| 319 | { |
| 320 | return (m_db.find_or_add(name(), *this)); |
| 321 | } |
| 322 | |
| 323 | std::shared_ptr<interface> |
| 324 | interface::singular() const |
| 325 | { |
| 326 | return singular_i(); |
| 327 | } |
| 328 | |
| 329 | std::shared_ptr<interface> |
| 330 | interface::find(const std::string& name) |
| 331 | { |
| 332 | return (m_db.find(name)); |
| 333 | } |
| 334 | |
| 335 | std::shared_ptr<interface> |
| 336 | interface::find(const handle_t& handle) |
| 337 | { |
| 338 | return (m_hdl_db[handle].lock()); |
| 339 | } |
| 340 | |
| 341 | void |
| 342 | interface::add(const std::string& name, const HW::item<handle_t>& item) |
| 343 | { |
| 344 | std::shared_ptr<interface> sp = find(name); |
| 345 | |
| 346 | if (sp && item) { |
| 347 | m_hdl_db[item.data()] = sp; |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | void |
| 352 | interface::remove(const HW::item<handle_t>& item) |
| 353 | { |
| 354 | m_hdl_db.erase(item.data()); |
| 355 | } |
| 356 | |
| 357 | void |
| 358 | interface::dump(std::ostream& os) |
| 359 | { |
| 360 | m_db.dump(os); |
| 361 | } |
| 362 | |
| 363 | void |
| 364 | interface::event_handler::handle_populate(const client_db::key_t& key) |
| 365 | { |
| 366 | /* |
| 367 | * dump VPP current states |
| 368 | */ |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 369 | std::shared_ptr<interface_cmds::dump_cmd> cmd(new interface_cmds::dump_cmd()); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 370 | |
| 371 | HW::enqueue(cmd); |
| 372 | HW::write(); |
| 373 | |
| 374 | for (auto& itf_record : *cmd) { |
| 375 | std::unique_ptr<interface> itf = |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 376 | interface_factory::new_interface(itf_record.get_payload()); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 377 | |
| 378 | if (itf && interface::type_t::LOCAL != itf->type()) { |
| 379 | VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string(); |
| 380 | /* |
| 381 | * Write each of the discovered interfaces into the OM, |
| 382 | * but disable the HW Command q whilst we do, so that no |
| 383 | * commands are sent to VPP |
| 384 | */ |
| 385 | OM::commit(key, *itf); |
| 386 | |
| 387 | /** |
| 388 | * Get the address configured on the interface |
| 389 | */ |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame^] | 390 | std::shared_ptr<l3_binding_cmds::dump_v4_cmd> dcmd = |
| 391 | std::make_shared<l3_binding_cmds::dump_v4_cmd>( |
| 392 | l3_binding_cmds::dump_v4_cmd(itf->handle())); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 393 | |
| 394 | HW::enqueue(dcmd); |
| 395 | HW::write(); |
| 396 | |
| 397 | for (auto& l3_record : *dcmd) { |
| 398 | auto& payload = l3_record.get_payload(); |
| 399 | const route::prefix_t pfx(payload.is_ipv6, payload.ip, |
| 400 | payload.prefix_length); |
| 401 | |
| 402 | VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string(); |
| 403 | |
| 404 | l3_binding l3(*itf, pfx); |
| 405 | OM::commit(key, l3); |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | interface::event_handler::event_handler() |
| 412 | { |
| 413 | OM::register_listener(this); |
| 414 | inspect::register_handler({ "interface", "intf" }, "interfaces", this); |
| 415 | } |
| 416 | |
| 417 | void |
| 418 | interface::event_handler::handle_replay() |
| 419 | { |
| 420 | m_db.replay(); |
| 421 | } |
| 422 | |
| 423 | dependency_t |
| 424 | interface::event_handler::order() const |
| 425 | { |
| 426 | return (dependency_t::INTERFACE); |
| 427 | } |
| 428 | |
| 429 | void |
| 430 | interface::event_handler::show(std::ostream& os) |
| 431 | { |
| 432 | m_db.dump(os); |
| 433 | } |
| 434 | } |
| 435 | /* |
| 436 | * fd.io coding-style-patch-verification: ON |
| 437 | * |
| 438 | * Local Variables: |
| 439 | * eval: (c-set-style "mozilla") |
| 440 | * End: |
| 441 | */ |