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_span.hpp" |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 17 | #include "vom/interface_span_cmds.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 18 | |
| 19 | namespace VOM { |
| 20 | /** |
| 21 | * A DB of all interface_span config |
| 22 | */ |
Neale Ranns | fd92060 | 2017-11-23 12:15:00 -0800 | [diff] [blame] | 23 | singular_db<interface_span::key_t, interface_span> interface_span::m_db; |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 24 | |
| 25 | interface_span::event_handler interface_span::m_evh; |
| 26 | |
| 27 | interface_span::interface_span(const interface& itf_from, |
| 28 | const interface& itf_to, |
| 29 | interface_span::state_t state) |
| 30 | : m_itf_from(itf_from.singular()) |
| 31 | , m_itf_to(itf_to.singular()) |
| 32 | , m_state(state) |
| 33 | , m_config(true) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | interface_span::interface_span(const interface_span& o) |
| 38 | : m_itf_from(o.m_itf_from) |
| 39 | , m_itf_to(o.m_itf_to) |
| 40 | , m_state(o.m_state) |
| 41 | , m_config(o.m_config) |
| 42 | { |
| 43 | } |
| 44 | |
| 45 | interface_span::~interface_span() |
| 46 | { |
| 47 | sweep(); |
| 48 | |
| 49 | // not in the DB anymore. |
| 50 | m_db.release(make_pair(m_itf_from->key(), m_itf_to->key()), this); |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | interface_span::sweep() |
| 55 | { |
| 56 | if (m_config) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 57 | HW::enqueue(new interface_span_cmds::unconfig_cmd( |
| 58 | m_config, m_itf_from->handle(), m_itf_to->handle())); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 59 | } |
| 60 | HW::write(); |
| 61 | } |
| 62 | |
| 63 | void |
| 64 | interface_span::dump(std::ostream& os) |
| 65 | { |
| 66 | m_db.dump(os); |
| 67 | } |
| 68 | |
| 69 | void |
| 70 | interface_span::replay() |
| 71 | { |
| 72 | if (m_config) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 73 | HW::enqueue(new interface_span_cmds::config_cmd( |
| 74 | m_config, m_itf_from->handle(), m_itf_to->handle(), m_state)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | std::string |
| 79 | interface_span::to_string() const |
| 80 | { |
| 81 | std::ostringstream s; |
| 82 | s << "Itf Span-config:" |
| 83 | << " itf-from:" << m_itf_from->to_string() |
| 84 | << " itf-to:" << m_itf_to->to_string() << " state:" << m_state.to_string(); |
| 85 | |
| 86 | return (s.str()); |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | interface_span::update(const interface_span& desired) |
| 91 | { |
| 92 | if (!m_config) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 93 | HW::enqueue(new interface_span_cmds::config_cmd( |
| 94 | m_config, m_itf_from->handle(), m_itf_to->handle(), m_state)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | std::ostream& |
Neale Ranns | fd92060 | 2017-11-23 12:15:00 -0800 | [diff] [blame] | 99 | operator<<(std::ostream& os, const interface_span::key_t& key) |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 100 | { |
| 101 | os << "[" << key.first << ", " << key.second << "]"; |
| 102 | |
| 103 | return (os); |
| 104 | } |
| 105 | |
| 106 | std::shared_ptr<interface_span> |
| 107 | interface_span::find_or_add(const interface_span& temp) |
| 108 | { |
| 109 | return (m_db.find_or_add( |
| 110 | make_pair(temp.m_itf_from->key(), temp.m_itf_to->key()), temp)); |
| 111 | } |
| 112 | |
| 113 | std::shared_ptr<interface_span> |
| 114 | interface_span::singular() const |
| 115 | { |
| 116 | return find_or_add(*this); |
| 117 | } |
| 118 | |
| 119 | interface_span::event_handler::event_handler() |
| 120 | { |
| 121 | OM::register_listener(this); |
| 122 | inspect::register_handler({ "itf-span" }, "interface span configurations", |
| 123 | this); |
| 124 | } |
| 125 | |
| 126 | void |
| 127 | interface_span::event_handler::handle_replay() |
| 128 | { |
| 129 | m_db.replay(); |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | interface_span::event_handler::handle_populate(const client_db::key_t& key) |
| 134 | { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 135 | std::shared_ptr<interface_span_cmds::dump_cmd> cmd( |
| 136 | new interface_span_cmds::dump_cmd()); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 137 | |
| 138 | HW::enqueue(cmd); |
| 139 | HW::write(); |
| 140 | |
| 141 | for (auto& record : *cmd) { |
| 142 | auto& payload = record.get_payload(); |
| 143 | |
| 144 | std::shared_ptr<interface> itf_from = |
| 145 | interface::find(payload.sw_if_index_from); |
| 146 | std::shared_ptr<interface> itf_to = interface::find(payload.sw_if_index_to); |
| 147 | |
| 148 | interface_span itf_span(*itf_from, *itf_to, |
| 149 | state_t::from_int(payload.state)); |
| 150 | |
| 151 | VOM_LOG(log_level_t::DEBUG) << "span-dump: " << itf_from->to_string() |
| 152 | << itf_to->to_string() |
| 153 | << state_t::from_int(payload.state).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, itf_span); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | dependency_t |
| 165 | interface_span::event_handler::order() const |
| 166 | { |
| 167 | return (dependency_t::BINDING); |
| 168 | } |
| 169 | |
| 170 | void |
| 171 | interface_span::event_handler::show(std::ostream& os) |
| 172 | { |
| 173 | m_db.dump(os); |
| 174 | } |
| 175 | |
| 176 | const interface_span::state_t interface_span::state_t::DISABLED(0, "disable"); |
| 177 | const interface_span::state_t interface_span::state_t::RX_ENABLED(1, |
| 178 | "rx-enable"); |
| 179 | const interface_span::state_t interface_span::state_t::TX_ENABLED(2, |
| 180 | "tx-enable"); |
| 181 | const interface_span::state_t interface_span::state_t::TX_RX_ENABLED( |
| 182 | 3, |
| 183 | "tx-rx-enable"); |
| 184 | |
| 185 | interface_span::state_t::state_t(int v, const std::string& s) |
| 186 | : enum_base<interface_span::state_t>(v, s) |
| 187 | { |
| 188 | } |
| 189 | |
| 190 | interface_span::state_t |
| 191 | interface_span::state_t::from_int(uint8_t i) |
| 192 | { |
| 193 | switch (i) { |
| 194 | case 0: |
| 195 | return interface_span::state_t::DISABLED; |
| 196 | break; |
| 197 | case 1: |
| 198 | return interface_span::state_t::RX_ENABLED; |
| 199 | break; |
| 200 | case 2: |
| 201 | return interface_span::state_t::TX_ENABLED; |
| 202 | break; |
| 203 | case 3: |
| 204 | default: |
| 205 | break; |
| 206 | } |
| 207 | |
| 208 | return interface_span::state_t::TX_RX_ENABLED; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * fd.io coding-style-patch-verification: ON |
| 214 | * |
| 215 | * Local Variables: |
| 216 | * eval: (c-set-style "mozilla") |
| 217 | * End: |
| 218 | */ |