blob: 8e012b5d951afdc5dffa071c9bd11faf33e7daf1 [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.hpp"
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070017#include "vom/bridge_domain_cmds.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070018#include "vom/interface.hpp"
19#include "vom/l2_binding.hpp"
Neale Ranns812ed392017-10-16 04:20:13 -070020
21namespace VOM {
22/**
23 * A DB of al the interfaces, key on the name
24 */
25singular_db<uint32_t, bridge_domain> bridge_domain::m_db;
26
27bridge_domain::event_handler bridge_domain::m_evh;
28
29/**
30 * Construct a new object matching the desried state
31 */
32bridge_domain::bridge_domain(uint32_t id)
33 : m_id(id)
34{
35}
36
37bridge_domain::bridge_domain(const bridge_domain& o)
38 : m_id(o.m_id)
39{
40}
41
42uint32_t
43bridge_domain::id() const
44{
45 return (m_id.data());
46}
47
48void
49bridge_domain::sweep()
50{
51 if (rc_t::OK == m_id.rc()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070052 HW::enqueue(new bridge_domain_cmds::delete_cmd(m_id));
Neale Ranns812ed392017-10-16 04:20:13 -070053 }
54 HW::write();
55}
56
57void
58bridge_domain::replay()
59{
60 if (rc_t::OK == m_id.rc()) {
Neale Ranns9ef1c0a2017-11-03 04:39:05 -070061 HW::enqueue(new bridge_domain_cmds::create_cmd(m_id));
Neale Ranns812ed392017-10-16 04:20:13 -070062 }
63}
64
65bridge_domain::~bridge_domain()
66{
67 sweep();
68
69 // not in the DB anymore.
70 m_db.release(m_id.data(), this);
71}
72
73std::string
74bridge_domain::to_string() const
75{
76 std::ostringstream s;
77 s << "bridge-domain:[" << m_id.to_string() << "]";
78
79 return (s.str());
80}
81
82std::shared_ptr<bridge_domain>
83bridge_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
112void
113bridge_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 Ranns9ef1c0a2017-11-03 04:39:05 -0700119 HW::enqueue(new bridge_domain_cmds::create_cmd(m_id));
Neale Ranns812ed392017-10-16 04:20:13 -0700120 }
121}
122
123std::shared_ptr<bridge_domain>
124bridge_domain::find_or_add(const bridge_domain& temp)
125{
126 return (m_db.find_or_add(temp.m_id.data(), temp));
127}
128
129std::shared_ptr<bridge_domain>
130bridge_domain::singular() const
131{
132 return find_or_add(*this);
133}
134
135void
136bridge_domain::dump(std::ostream& os)
137{
138 m_db.dump(os);
139}
140
141void
142bridge_domain::event_handler::handle_populate(const client_db::key_t& key)
143{
144 /*
145 * dump VPP Bridge domains
146 */
Neale Ranns9ef1c0a2017-11-03 04:39:05 -0700147 std::shared_ptr<bridge_domain_cmds::dump_cmd> cmd(
148 new bridge_domain_cmds::dump_cmd());
Neale Ranns812ed392017-10-16 04:20:13 -0700149
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
179bridge_domain::event_handler::event_handler()
180{
181 OM::register_listener(this);
182 inspect::register_handler({ "bd", "bridge" }, "Bridge Domains", this);
183}
184
185void
186bridge_domain::event_handler::handle_replay()
187{
188 m_db.replay();
189}
190
191dependency_t
192bridge_domain::event_handler::order() const
193{
194 return (dependency_t::TABLE);
195}
196
197void
198bridge_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 */