blob: 9ac5ceae87cda844cfea7591db17986b0e6ff503 [file] [log] [blame]
Neale Ranns812ed392017-10-16 04:20:13 -07001/*
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 Ranns9ef1c0a2017-11-03 04:39:05 -070017#include "vom/bridge_domain_entry_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070018
19namespace VOM {
20singular_db<bridge_domain_entry::key_t, bridge_domain_entry>
21 bridge_domain_entry::m_db;
22
23bridge_domain_entry::event_handler bridge_domain_entry::m_evh;
24
25bridge_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
35bridge_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 Rannsf29e85f2017-11-01 03:29:13 -070043 * the entry goes in the default bridge-domain
44 */
Neale Ranns812ed392017-10-16 04:20:13 -070045 bridge_domain bd(bridge_domain::DEFAULT_TABLE);
46
47 m_bd = bd.singular();
48}
49
50bridge_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 Rannsfd920602017-11-23 12:15:00 -080058const bridge_domain_entry::key_t
59bridge_domain_entry::key() const
60{
61 return (std::make_pair(m_bd->key(), m_mac));
62}
63
64bool
65bridge_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 Ranns812ed392017-10-16 04:20:13 -070070bridge_domain_entry::~bridge_domain_entry()
71{
72 sweep();
73
74 // not in the DB anymore.
Neale Rannsfd920602017-11-23 12:15:00 -080075 m_db.release(key(), this);
Neale Ranns812ed392017-10-16 04:20:13 -070076}
77
78void
79bridge_domain_entry::sweep()
80{
81 if (m_hw) {
Mohsin Kazmie2e9ce52017-12-01 15:12:57 +010082 HW::enqueue(new bridge_domain_entry_cmds::delete_cmd(
83 m_hw, m_mac, m_bd->id(), interface::type_t::BVI == m_tx_itf->type()));
Neale Ranns812ed392017-10-16 04:20:13 -070084 }
85 HW::write();
86}
87
88void
89bridge_domain_entry::replay()
90{
91 if (m_hw) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070092 HW::enqueue(new bridge_domain_entry_cmds::create_cmd(
Mohsin Kazmie2e9ce52017-12-01 15:12:57 +010093 m_hw, m_mac, m_bd->id(), m_tx_itf->handle(),
94 interface::type_t::BVI == m_tx_itf->type()));
Neale Ranns812ed392017-10-16 04:20:13 -070095 }
96}
97std::string
98bridge_domain_entry::to_string() const
99{
100 std::ostringstream s;
101 s << "bridge-domain-entry:[" << m_bd->to_string() << ", " << m_mac.to_string()
102 << ", tx:" << m_tx_itf->name() << "]";
103
104 return (s.str());
105}
106
107void
108bridge_domain_entry::update(const bridge_domain_entry& r)
109{
110 /*
Neale Rannsf29e85f2017-11-01 03:29:13 -0700111 * create the table if it is not yet created
112 */
Neale Ranns812ed392017-10-16 04:20:13 -0700113 if (rc_t::OK != m_hw.rc()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700114 HW::enqueue(new bridge_domain_entry_cmds::create_cmd(
Mohsin Kazmie2e9ce52017-12-01 15:12:57 +0100115 m_hw, m_mac, m_bd->id(), m_tx_itf->handle(),
116 interface::type_t::BVI == m_tx_itf->type()));
Neale Ranns812ed392017-10-16 04:20:13 -0700117 }
118}
119
120std::shared_ptr<bridge_domain_entry>
121bridge_domain_entry::find_or_add(const bridge_domain_entry& temp)
122{
Neale Rannsfd920602017-11-23 12:15:00 -0800123 return (m_db.find_or_add(temp.key(), temp));
124}
125
126std::shared_ptr<bridge_domain_entry>
127bridge_domain_entry::find(const key_t& k)
128{
129 return (m_db.find(k));
Neale Ranns812ed392017-10-16 04:20:13 -0700130}
131
132std::shared_ptr<bridge_domain_entry>
133bridge_domain_entry::singular() const
134{
135 return find_or_add(*this);
136}
137
138void
139bridge_domain_entry::dump(std::ostream& os)
140{
141 m_db.dump(os);
142}
143
144bridge_domain_entry::event_handler::event_handler()
145{
146 OM::register_listener(this);
147 inspect::register_handler({ "bd-entry" },
148 "bridge domain entry configurations", this);
149}
150
151void
152bridge_domain_entry::event_handler::handle_replay()
153{
154 m_db.replay();
155}
156
157void
158bridge_domain_entry::event_handler::handle_populate(const client_db::key_t& key)
159{
Neale Ranns1d781552017-11-27 04:52:35 -0800160 std::shared_ptr<bridge_domain_entry_cmds::dump_cmd> cmd =
161 std::make_shared<bridge_domain_entry_cmds::dump_cmd>();
Neale Ranns812ed392017-10-16 04:20:13 -0700162
163 HW::enqueue(cmd);
164 HW::write();
165
166 for (auto& record : *cmd) {
167 auto& payload = record.get_payload();
168
169 std::shared_ptr<interface> itf = interface::find(payload.sw_if_index);
170 std::shared_ptr<bridge_domain> bd = bridge_domain::find(payload.bd_id);
Neale Rannsa2ee0292017-11-28 22:29:13 -0800171
172 if (!bd || !itf) {
173 VOM_LOG(log_level_t::ERROR) << "bridge-domain-entry dump:"
174 << " itf:" << payload.sw_if_index
175 << " bd:" << payload.bd_id;
176 continue;
177 }
178
Neale Ranns812ed392017-10-16 04:20:13 -0700179 mac_address_t mac(payload.mac);
180 bridge_domain_entry bd_e(*bd, mac, *itf);
181
Neale Rannsa2ee0292017-11-28 22:29:13 -0800182 VOM_LOG(log_level_t::DEBUG) << "bridge-domain-entry dump:"
183 << " " << bd->to_string() << " "
184 << itf->to_string() << " mac:["
185 << mac.to_string() << "]";
Neale Ranns812ed392017-10-16 04:20:13 -0700186
187 /*
Neale Rannsf29e85f2017-11-01 03:29:13 -0700188 * Write each of the discovered interfaces into the OM,
189 * but disable the HW Command q whilst we do, so that no
190 * commands are sent to VPP
191 */
Neale Ranns812ed392017-10-16 04:20:13 -0700192 OM::commit(key, bd_e);
193 }
194}
195
196dependency_t
197bridge_domain_entry::event_handler::order() const
198{
199 return (dependency_t::ENTRY);
200}
201
202void
203bridge_domain_entry::event_handler::show(std::ostream& os)
204{
205 m_db.dump(os);
206}
207
208std::ostream&
209operator<<(std::ostream& os, const bridge_domain_entry::key_t& key)
210{
211 os << "[" << key.first << ", " << key.second.to_string() << "]";
212
213 return (os);
214}
215}
216
217/*
218 * fd.io coding-style-patch-verification: ON
219 *
220 * Local Variables:
221 * eval: (c-set-style "mozilla")
222 * End:
223 */