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