VOM: GBP bridge domain flags
[vpp.git] / extras / vom / vom / gbp_bridge_domain.cpp
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/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"
21
22 namespace VOM {
23
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(
26   0,
27   "do-not-learn");
28
29 gbp_bridge_domain::flags_t::flags_t(int v, const std::string& s)
30   : enum_base<gbp_bridge_domain::flags_t>(v, s)
31 {
32 }
33
34 /**
35  * A DB of al the interfaces, key on the name
36  */
37 singular_db<uint32_t, gbp_bridge_domain> gbp_bridge_domain::m_db;
38
39 gbp_bridge_domain::event_handler gbp_bridge_domain::m_evh;
40
41 /**
42  * Construct a new object matching the desried state
43  */
44 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
45                                      const interface& bvi,
46                                      const flags_t& flags)
47   : m_id(bd.id())
48   , m_bd(bd.singular())
49   , m_bvi(bvi.singular())
50   , m_flags(flags)
51 {
52 }
53
54 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
55                                      const interface& bvi,
56                                      const interface& uu_fwd,
57                                      const flags_t& flags)
58   : m_id(bd.id())
59   , m_bd(bd.singular())
60   , m_bvi(bvi.singular())
61   , m_uu_fwd(uu_fwd.singular())
62   , m_flags(flags)
63 {
64 }
65
66 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
67                                      const std::shared_ptr<interface> bvi,
68                                      const std::shared_ptr<interface> uu_fwd,
69                                      const flags_t& flags)
70   : m_id(bd.id())
71   , m_bd(bd.singular())
72   , m_bvi(bvi->singular())
73   , m_uu_fwd(uu_fwd->singular())
74   , m_flags(flags)
75 {
76 }
77
78 gbp_bridge_domain::gbp_bridge_domain(const bridge_domain& bd,
79                                      const interface& bvi,
80                                      const std::shared_ptr<interface> uu_fwd,
81                                      const flags_t& flags)
82   : m_id(bd.id())
83   , m_bd(bd.singular())
84   , m_bvi(bvi.singular())
85   , m_uu_fwd(uu_fwd->singular())
86   , m_flags(flags)
87 {
88 }
89
90 gbp_bridge_domain::gbp_bridge_domain(const gbp_bridge_domain& bd)
91   : m_id(bd.id())
92   , m_bd(bd.m_bd)
93   , m_bvi(bd.m_bvi)
94   , m_uu_fwd(bd.m_uu_fwd)
95   , m_flags(bd.m_flags)
96 {
97 }
98
99 const gbp_bridge_domain::key_t
100 gbp_bridge_domain::key() const
101 {
102   return (m_bd->key());
103 }
104
105 uint32_t
106 gbp_bridge_domain::id() const
107 {
108   return (m_bd->id());
109 }
110
111 const std::shared_ptr<bridge_domain>
112 gbp_bridge_domain::get_bridge_domain() const
113 {
114   return m_bd;
115 }
116
117 const std::shared_ptr<interface>
118 gbp_bridge_domain::get_bvi() const
119 {
120   return m_bvi;
121 }
122
123 bool
124 gbp_bridge_domain::operator==(const gbp_bridge_domain& b) const
125 {
126   bool equal = true;
127
128   if (m_bvi && b.m_bvi)
129     equal &= (m_bvi->key() == b.m_bvi->key());
130   else if (!m_bvi && !b.m_bvi)
131     ;
132   else
133     equal = false;
134
135   if (m_uu_fwd && b.m_uu_fwd)
136     equal &= (m_uu_fwd->key() == b.m_uu_fwd->key());
137   else if (!m_uu_fwd && !b.m_uu_fwd)
138     ;
139   else
140     equal = false;
141
142   return ((m_bd->key() == b.m_bd->key()) && equal);
143 }
144
145 void
146 gbp_bridge_domain::sweep()
147 {
148   if (rc_t::OK == m_id.rc()) {
149     HW::enqueue(new gbp_bridge_domain_cmds::delete_cmd(m_id));
150   }
151   HW::write();
152 }
153
154 void
155 gbp_bridge_domain::replay()
156 {
157   if (rc_t::OK == m_id.rc()) {
158     HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(
159       m_id, (m_bvi ? m_bvi->handle() : handle_t::INVALID),
160       (m_uu_fwd ? m_uu_fwd->handle() : handle_t::INVALID), m_flags));
161   }
162 }
163
164 gbp_bridge_domain::~gbp_bridge_domain()
165 {
166   sweep();
167
168   // not in the DB anymore.
169   m_db.release(m_id.data(), this);
170 }
171
172 std::string
173 gbp_bridge_domain::to_string() const
174 {
175   std::ostringstream s;
176   s << "gbp-bridge-domain:[" << m_bd->to_string();
177
178   if (m_bvi)
179     s << " bvi:" << m_bvi->to_string();
180   if (m_uu_fwd)
181     s << " uu-fwd:" << m_uu_fwd->to_string();
182
183   s << "]";
184
185   return (s.str());
186 }
187
188 std::shared_ptr<gbp_bridge_domain>
189 gbp_bridge_domain::find(const key_t& key)
190 {
191   return (m_db.find(key));
192 }
193
194 void
195 gbp_bridge_domain::update(const gbp_bridge_domain& desired)
196 {
197   /*
198    * the desired state is always that the interface should be created
199    */
200   if (rc_t::OK != m_id.rc()) {
201     HW::enqueue(new gbp_bridge_domain_cmds::create_cmd(
202       m_id, (m_bvi ? m_bvi->handle() : handle_t::INVALID),
203       (m_uu_fwd ? m_uu_fwd->handle() : handle_t::INVALID), m_flags));
204   }
205 }
206
207 std::shared_ptr<gbp_bridge_domain>
208 gbp_bridge_domain::find_or_add(const gbp_bridge_domain& temp)
209 {
210   return (m_db.find_or_add(temp.m_id.data(), temp));
211 }
212
213 std::shared_ptr<gbp_bridge_domain>
214 gbp_bridge_domain::singular() const
215 {
216   return find_or_add(*this);
217 }
218
219 void
220 gbp_bridge_domain::dump(std::ostream& os)
221 {
222   db_dump(m_db, os);
223 }
224
225 void
226 gbp_bridge_domain::event_handler::handle_populate(const client_db::key_t& key)
227 {
228   /*
229    * dump GBP Bridge domains
230    */
231   std::shared_ptr<gbp_bridge_domain_cmds::dump_cmd> cmd =
232     std::make_shared<gbp_bridge_domain_cmds::dump_cmd>();
233
234   HW::enqueue(cmd);
235   HW::write();
236
237   for (auto& record : *cmd) {
238     auto& payload = record.get_payload();
239
240     std::shared_ptr<interface> uu_fwd =
241       interface::find(payload.bd.uu_fwd_sw_if_index);
242     std::shared_ptr<interface> bvi =
243       interface::find(payload.bd.bvi_sw_if_index);
244
245     if (uu_fwd && bvi) {
246       gbp_bridge_domain bd(payload.bd.bd_id, bvi, uu_fwd);
247       OM::commit(key, bd);
248       VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
249     } else if (bvi) {
250       gbp_bridge_domain bd(payload.bd.bd_id, *bvi);
251       OM::commit(key, bd);
252       VOM_LOG(log_level_t::DEBUG) << "dump: " << bd.to_string();
253     } else {
254       VOM_LOG(log_level_t::ERROR)
255         << "no BVI:" << payload.bd.bvi_sw_if_index
256         << " nor uu-fwd:" << payload.bd.uu_fwd_sw_if_index;
257     }
258   }
259 }
260
261 gbp_bridge_domain::event_handler::event_handler()
262 {
263   OM::register_listener(this);
264   inspect::register_handler({ "gbd", "gbridge" }, "GBP Bridge Domains", this);
265 }
266
267 void
268 gbp_bridge_domain::event_handler::handle_replay()
269 {
270   m_db.replay();
271 }
272
273 dependency_t
274 gbp_bridge_domain::event_handler::order() const
275 {
276   return (dependency_t::VIRTUAL_TABLE);
277 }
278
279 void
280 gbp_bridge_domain::event_handler::show(std::ostream& os)
281 {
282   db_dump(m_db, os);
283 }
284 }
285
286 /*
287  * fd.io coding-style-patch-verification: ON
288  *
289  * Local Variables:
290  * eval: (c-set-style "mozilla")
291  * End:
292  */