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/tap_interface.hpp" |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 17 | #include "vom/tap_interface_cmds.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 18 | |
| 19 | #include <vapi/vpe.api.vapi.hpp> |
| 20 | |
| 21 | namespace VOM { |
| 22 | tap_interface::event_handler tap_interface::m_evh; |
| 23 | |
| 24 | /** |
| 25 | * Construct a new object matching the desried state |
| 26 | */ |
| 27 | tap_interface::tap_interface(const std::string& name, |
| 28 | admin_state_t state, |
| 29 | route::prefix_t prefix) |
| 30 | : interface(name, type_t::TAP, state) |
| 31 | , m_prefix(prefix) |
| 32 | , m_l2_address(l2_address_t::ZERO) |
| 33 | { |
| 34 | } |
| 35 | |
| 36 | tap_interface::tap_interface(const std::string& name, |
| 37 | admin_state_t state, |
| 38 | route::prefix_t prefix, |
| 39 | const l2_address_t& l2_address) |
| 40 | : interface(name, type_t::TAP, state) |
| 41 | , m_prefix(prefix) |
| 42 | , m_l2_address(l2_address) |
| 43 | { |
| 44 | } |
| 45 | |
| 46 | tap_interface::tap_interface(const handle_t& hdl, |
| 47 | const std::string& name, |
| 48 | admin_state_t state, |
| 49 | route::prefix_t prefix) |
| 50 | : interface(hdl, l2_address_t::ZERO, name, type_t::TAP, state) |
| 51 | , m_prefix(prefix) |
| 52 | , m_l2_address(l2_address_t::ZERO) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | tap_interface::~tap_interface() |
| 57 | { |
| 58 | sweep(); |
| 59 | release(); |
| 60 | } |
| 61 | |
| 62 | tap_interface::tap_interface(const tap_interface& o) |
| 63 | : interface(o) |
| 64 | , m_prefix(o.m_prefix) |
| 65 | , m_l2_address(o.m_l2_address) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | std::queue<cmd*>& |
| 70 | tap_interface::mk_create_cmd(std::queue<cmd*>& q) |
| 71 | { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 72 | q.push( |
| 73 | new tap_interface_cmds::create_cmd(m_hdl, name(), m_prefix, m_l2_address)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 74 | |
| 75 | return (q); |
| 76 | } |
| 77 | |
| 78 | std::queue<cmd*>& |
| 79 | tap_interface::mk_delete_cmd(std::queue<cmd*>& q) |
| 80 | { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 81 | q.push(new tap_interface_cmds::delete_cmd(m_hdl)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 82 | |
| 83 | return (q); |
| 84 | } |
| 85 | |
| 86 | std::shared_ptr<tap_interface> |
| 87 | tap_interface::singular() const |
| 88 | { |
| 89 | return std::dynamic_pointer_cast<tap_interface>(singular_i()); |
| 90 | } |
| 91 | |
| 92 | std::shared_ptr<interface> |
| 93 | tap_interface::singular_i() const |
| 94 | { |
| 95 | return m_db.find_or_add(name(), *this); |
| 96 | } |
| 97 | |
| 98 | void |
| 99 | tap_interface::event_handler::handle_populate(const client_db::key_t& key) |
| 100 | { |
| 101 | /* |
| 102 | * dump VPP current states |
| 103 | */ |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 104 | std::shared_ptr<tap_interface_cmds::dump_cmd> cmd( |
| 105 | new tap_interface_cmds::dump_cmd()); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 106 | |
| 107 | HW::enqueue(cmd); |
| 108 | HW::write(); |
| 109 | |
| 110 | for (auto& record : *cmd) { |
| 111 | auto& payload = record.get_payload(); |
| 112 | |
| 113 | std::string name = reinterpret_cast<const char*>(payload.dev_name); |
| 114 | |
| 115 | tap_interface itf(name, interface::admin_state_t::UP, |
| 116 | route::prefix_t::ZERO); |
| 117 | |
| 118 | VOM_LOG(log_level_t::DEBUG) << "tap-dump: " << itf.to_string(); |
| 119 | |
| 120 | /* |
| 121 | * Write each of the discovered interfaces into the OM, |
| 122 | * but disable the HW Command q whilst we do, so that no |
| 123 | * commands are sent to VPP |
| 124 | */ |
| 125 | OM::commit(key, itf); |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | tap_interface::event_handler::event_handler() |
| 130 | { |
| 131 | OM::register_listener(this); |
| 132 | inspect::register_handler({ "tap" }, "tap_interfaces", this); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | tap_interface::event_handler::handle_replay() |
| 137 | { |
| 138 | m_db.replay(); |
| 139 | } |
| 140 | |
| 141 | dependency_t |
| 142 | tap_interface::event_handler::order() const |
| 143 | { |
| 144 | return (dependency_t::INTERFACE); |
| 145 | } |
| 146 | |
| 147 | void |
| 148 | tap_interface::event_handler::show(std::ostream& os) |
| 149 | { |
| 150 | m_db.dump(os); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * fd.io coding-style-patch-verification: ON |
| 156 | * |
| 157 | * Local Variables: |
| 158 | * eval: (c-set-style "mozilla") |
| 159 | * End: |
| 160 | */ |