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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "vom/gbp_bridge_domain.hpp"
17 #include "vom/gbp_bridge_domain_cmds.hpp"
18 #include "vom/interface.hpp"
19 #include "vom/l2_binding.hpp"
20 #include "vom/singular_db_funcs.hpp"
24 const gbp_bridge_domain::flags_t gbp_bridge_domain::flags_t::NONE(0, "none");
25 const gbp_bridge_domain::flags_t gbp_bridge_domain::flags_t::DO_NOT_LEARN(
29 gbp_bridge_domain::flags_t::flags_t(int v, const std::string& s)
30 : enum_base<gbp_bridge_domain::flags_t>(v, s)
35 * A DB of al the interfaces, key on the name
37 singular_db<uint32_t, gbp_bridge_domain> gbp_bridge_domain::m_db;
39 gbp_bridge_domain::event_handler gbp_bridge_domain::m_evh;
42 * Construct a new object matching the desried state
44 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
49 , m_bvi(bvi.singular())
56 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
58 const interface& uu_fwd,
59 const interface& bm_flood,
63 , m_bvi(bvi.singular())
64 , m_uu_fwd(uu_fwd.singular())
65 , m_bm_flood(bm_flood.singular())
70 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
71 const std::shared_ptr<interface> bvi,
72 const std::shared_ptr<interface> uu_fwd,
73 const std::shared_ptr<interface> bm_flood,
79 , m_bm_flood(bm_flood)
84 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
86 const std::shared_ptr<interface> uu_fwd,
87 const std::shared_ptr<interface> bm_flood,
91 , m_bvi(bvi.singular())
93 , m_bm_flood(bm_flood)
98 gbp_bridge_domain::gbp_bridge_domain(const gbp_bridge_domain& bd)
102 , m_uu_fwd(bd.m_uu_fwd)
103 , m_bm_flood(bd.m_bm_flood)
104 , m_flags(bd.m_flags)
108 const gbp_bridge_domain::key_t
109 gbp_bridge_domain::key() const
111 return (m_bd->key());
115 gbp_bridge_domain::id() const
120 const std::shared_ptr<bridge_domain>
121 gbp_bridge_domain::get_bridge_domain() const
126 const std::shared_ptr<interface>
127 gbp_bridge_domain::get_bvi() const
133 gbp_bridge_domain::operator==(const gbp_bridge_domain& b) const
137 if (m_bvi && b.m_bvi)
138 equal &= (m_bvi->key() == b.m_bvi->key());
139 else if (!m_bvi && !b.m_bvi)
144 if (m_uu_fwd && b.m_uu_fwd)
145 equal &= (m_uu_fwd->key() == b.m_uu_fwd->key());
146 else if (!m_uu_fwd && !b.m_uu_fwd)
151 if (m_bm_flood && b.m_bm_flood)
152 equal &= (m_bm_flood->key() == b.m_bm_flood->key());
153 else if (!m_bm_flood && !b.m_bm_flood)
158 return ((m_bd->key() == b.m_bd->key()) && equal);
162 gbp_bridge_domain::sweep()
164 if (rc_t::OK == m_id.rc()) {
165 HW::enqueue(new gbp_bridge_domain_cmds::delete_cmd(m_id));
171 gbp_bridge_domain::replay()
173 if (rc_t::OK == m_id.rc()) {
174 HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(
175 m_id, (m_bvi ? m_bvi->handle() : handle_t::INVALID),
176 (m_uu_fwd ? m_uu_fwd->handle() : handle_t::INVALID),
177 (m_bm_flood ? m_bm_flood->handle() : handle_t::INVALID), m_flags));
181 gbp_bridge_domain::~gbp_bridge_domain()
185 // not in the DB anymore.
186 m_db.release(m_id.data(), this);
190 gbp_bridge_domain::to_string() const
192 std::ostringstream s;
193 s << "gbp-bridge-domain:[" << m_bd->to_string();
196 s << " bvi:" << m_bvi->to_string();
198 s << " uu-fwd:" << m_uu_fwd->to_string();
205 std::shared_ptr<gbp_bridge_domain>
206 gbp_bridge_domain::find(const key_t& key)
208 return (m_db.find(key));
212 gbp_bridge_domain::update(const gbp_bridge_domain& desired)
215 * the desired state is always that the interface should be created
217 if (rc_t::OK != m_id.rc()) {
218 HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(
219 m_id, (m_bvi ? m_bvi->handle() : handle_t::INVALID),
220 (m_uu_fwd ? m_uu_fwd->handle() : handle_t::INVALID),
221 (m_bm_flood ? m_bm_flood->handle() : handle_t::INVALID), m_flags));
225 std::shared_ptr<gbp_bridge_domain>
226 gbp_bridge_domain::find_or_add(const gbp_bridge_domain& temp)
228 return (m_db.find_or_add(temp.m_id.data(), temp));
231 std::shared_ptr<gbp_bridge_domain>
232 gbp_bridge_domain::singular() const
234 return find_or_add(*this);
238 gbp_bridge_domain::dump(std::ostream& os)
244 gbp_bridge_domain::event_handler::handle_populate(const client_db::key_t& key)
247 * dump GBP Bridge domains
249 std::shared_ptr<gbp_bridge_domain_cmds::dump_cmd> cmd =
250 std::make_shared<gbp_bridge_domain_cmds::dump_cmd>();
255 for (auto& record : *cmd) {
256 auto& payload = record.get_payload();
258 std::shared_ptr<interface> uu_fwd =
259 interface::find(payload.bd.uu_fwd_sw_if_index);
260 std::shared_ptr<interface> bm_flood =
261 interface::find(payload.bd.bm_flood_sw_if_index);
262 std::shared_ptr<interface> bvi =
263 interface::find(payload.bd.bvi_sw_if_index);
265 if (uu_fwd && bm_flood && bvi) {
266 gbp_bridge_domain bd(payload.bd.bd_id, bvi, uu_fwd, bm_flood);
268 VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
270 gbp_bridge_domain bd(payload.bd.bd_id, *bvi);
272 VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
274 VOM_LOG(log_level_t::ERROR)
275 << "no BVI:" << payload.bd.bvi_sw_if_index
276 << " nor uu-fwd:" << payload.bd.uu_fwd_sw_if_index;
281 gbp_bridge_domain::event_handler::event_handler()
283 OM::register_listener(this);
284 inspect::register_handler({ "gbd", "gbridge" }, "GBP Bridge Domains", this);
288 gbp_bridge_domain::event_handler::handle_replay()
294 gbp_bridge_domain::event_handler::order() const
296 return (dependency_t::VIRTUAL_TABLE);
300 gbp_bridge_domain::event_handler::show(std::ostream& os)
307 * fd.io coding-style-patch-verification: ON
310 * eval: (c-set-style "mozilla")