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.hpp" |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 17 | #include "vom/bridge_domain_cmds.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 18 | #include "vom/interface.hpp" |
| 19 | #include "vom/l2_binding.hpp" |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 20 | |
| 21 | namespace VOM { |
| 22 | /** |
| 23 | * A DB of al the interfaces, key on the name |
| 24 | */ |
| 25 | singular_db<uint32_t, bridge_domain> bridge_domain::m_db; |
| 26 | |
| 27 | bridge_domain::event_handler bridge_domain::m_evh; |
| 28 | |
| 29 | /** |
| 30 | * Construct a new object matching the desried state |
| 31 | */ |
| 32 | bridge_domain::bridge_domain(uint32_t id) |
| 33 | : m_id(id) |
| 34 | { |
| 35 | } |
| 36 | |
| 37 | bridge_domain::bridge_domain(const bridge_domain& o) |
| 38 | : m_id(o.m_id) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | uint32_t |
| 43 | bridge_domain::id() const |
| 44 | { |
| 45 | return (m_id.data()); |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | bridge_domain::sweep() |
| 50 | { |
| 51 | if (rc_t::OK == m_id.rc()) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 52 | HW::enqueue(new bridge_domain_cmds::delete_cmd(m_id)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 53 | } |
| 54 | HW::write(); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | bridge_domain::replay() |
| 59 | { |
| 60 | if (rc_t::OK == m_id.rc()) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 61 | HW::enqueue(new bridge_domain_cmds::create_cmd(m_id)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | |
| 65 | bridge_domain::~bridge_domain() |
| 66 | { |
| 67 | sweep(); |
| 68 | |
| 69 | // not in the DB anymore. |
| 70 | m_db.release(m_id.data(), this); |
| 71 | } |
| 72 | |
| 73 | std::string |
| 74 | bridge_domain::to_string() const |
| 75 | { |
| 76 | std::ostringstream s; |
| 77 | s << "bridge-domain:[" << m_id.to_string() << "]"; |
| 78 | |
| 79 | return (s.str()); |
| 80 | } |
| 81 | |
| 82 | std::shared_ptr<bridge_domain> |
| 83 | bridge_domain::find(uint32_t id) |
| 84 | { |
| 85 | /* |
| 86 | * Loop throught the entire map looking for matching interface. |
| 87 | * not the most efficient algorithm, but it will do for now. The |
| 88 | * number of L3 configs is low and this is only called during bootup |
| 89 | */ |
| 90 | std::shared_ptr<bridge_domain> bd; |
| 91 | |
| 92 | auto it = m_db.cbegin(); |
| 93 | |
| 94 | while (it != m_db.cend()) { |
| 95 | /* |
| 96 | * The key in the DB is a pair of the interface's name and prefix. |
| 97 | * If the keys match, save the L3-config |
| 98 | */ |
| 99 | auto key = it->first; |
| 100 | |
| 101 | if (id == key) { |
| 102 | bd = it->second.lock(); |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | ++it; |
| 107 | } |
| 108 | |
| 109 | return (bd); |
| 110 | } |
| 111 | |
| 112 | void |
| 113 | bridge_domain::update(const bridge_domain& desired) |
| 114 | { |
| 115 | /* |
| 116 | * the desired state is always that the interface should be created |
| 117 | */ |
| 118 | if (rc_t::OK != m_id.rc()) { |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 119 | HW::enqueue(new bridge_domain_cmds::create_cmd(m_id)); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | std::shared_ptr<bridge_domain> |
| 124 | bridge_domain::find_or_add(const bridge_domain& temp) |
| 125 | { |
| 126 | return (m_db.find_or_add(temp.m_id.data(), temp)); |
| 127 | } |
| 128 | |
| 129 | std::shared_ptr<bridge_domain> |
| 130 | bridge_domain::singular() const |
| 131 | { |
| 132 | return find_or_add(*this); |
| 133 | } |
| 134 | |
| 135 | void |
| 136 | bridge_domain::dump(std::ostream& os) |
| 137 | { |
| 138 | m_db.dump(os); |
| 139 | } |
| 140 | |
| 141 | void |
| 142 | bridge_domain::event_handler::handle_populate(const client_db::key_t& key) |
| 143 | { |
| 144 | /* |
| 145 | * dump VPP Bridge domains |
| 146 | */ |
Neale Ranns | 9ef1c0a | 2017-11-03 04:39:05 -0700 | [diff] [blame] | 147 | std::shared_ptr<bridge_domain_cmds::dump_cmd> cmd( |
| 148 | new bridge_domain_cmds::dump_cmd()); |
Neale Ranns | 812ed39 | 2017-10-16 04:20:13 -0700 | [diff] [blame] | 149 | |
| 150 | HW::enqueue(cmd); |
| 151 | HW::write(); |
| 152 | |
| 153 | for (auto& record : *cmd) { |
| 154 | auto& payload = record.get_payload(); |
| 155 | |
| 156 | bridge_domain bd(payload.bd_id); |
| 157 | |
| 158 | VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string(); |
| 159 | |
| 160 | /* |
| 161 | * Write each of the discovered interfaces into the OM, |
| 162 | * but disable the HW Command q whilst we do, so that no |
| 163 | * commands are sent to VPP |
| 164 | */ |
| 165 | OM::commit(key, bd); |
| 166 | |
| 167 | /** |
| 168 | * For each interface in the BD construct an l2_binding |
| 169 | */ |
| 170 | for (unsigned int ii = 0; ii < payload.n_sw_ifs; ii++) { |
| 171 | std::shared_ptr<interface> itf = |
| 172 | interface::find(payload.sw_if_details[ii].sw_if_index); |
| 173 | l2_binding l2(*itf, bd); |
| 174 | OM::commit(key, l2); |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | bridge_domain::event_handler::event_handler() |
| 180 | { |
| 181 | OM::register_listener(this); |
| 182 | inspect::register_handler({ "bd", "bridge" }, "Bridge Domains", this); |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | bridge_domain::event_handler::handle_replay() |
| 187 | { |
| 188 | m_db.replay(); |
| 189 | } |
| 190 | |
| 191 | dependency_t |
| 192 | bridge_domain::event_handler::order() const |
| 193 | { |
| 194 | return (dependency_t::TABLE); |
| 195 | } |
| 196 | |
| 197 | void |
| 198 | bridge_domain::event_handler::show(std::ostream& os) |
| 199 | { |
| 200 | m_db.dump(os); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * fd.io coding-style-patch-verification: ON |
| 206 | * |
| 207 | * Local Variables: |
| 208 | * eval: (c-set-style "mozilla") |
| 209 | * End: |
| 210 | */ |