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