VOM: logging, populate and stats fixes
[vpp.git] / src / vpp-api / vom / interface.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/interface.hpp"
17 #include "vom/interface_cmds.hpp"
18 #include "vom/interface_factory.hpp"
19 #include "vom/l3_binding_cmds.hpp"
20 #include "vom/logger.hpp"
21 #include "vom/prefix.hpp"
22
23 namespace VOM {
24 /**
25  * A DB of all the interfaces, key on the name
26  */
27 singular_db<interface::key_t, interface> interface::m_db;
28
29 /**
30  * A DB of all the interfaces, key on VPP's handle
31  */
32 std::map<handle_t, std::weak_ptr<interface>> interface::m_hdl_db;
33
34 interface::event_handler interface::m_evh;
35
36 /**
37  * Construct a new object matching the desried state
38  */
39 interface::interface(const std::string& name,
40                      interface::type_t itf_type,
41                      interface::admin_state_t itf_state)
42   : m_hdl(handle_t::INVALID)
43   , m_name(name)
44   , m_type(itf_type)
45   , m_state(itf_state)
46   , m_table_id(route::DEFAULT_TABLE)
47   , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
48   , m_oper(oper_state_t::DOWN)
49 {
50 }
51
52 interface::interface(const std::string& name,
53                      interface::type_t itf_type,
54                      interface::admin_state_t itf_state,
55                      const route_domain& rd)
56   : m_hdl(handle_t::INVALID)
57   , m_name(name)
58   , m_type(itf_type)
59   , m_rd(rd.singular())
60   , m_state(itf_state)
61   , m_table_id(m_rd->table_id())
62   , m_l2_address(l2_address_t::ZERO, rc_t::UNSET)
63   , m_oper(oper_state_t::DOWN)
64 {
65 }
66
67 interface::interface(const interface& o)
68   : m_hdl(o.m_hdl)
69   , m_name(o.m_name)
70   , m_type(o.m_type)
71   , m_rd(o.m_rd)
72   , m_state(o.m_state)
73   , m_table_id(o.m_table_id)
74   , m_l2_address(o.m_l2_address)
75   , m_oper(o.m_oper)
76 {
77 }
78
79 bool
80 interface::operator==(const interface& i) const
81 {
82   return ((key() == i.key()) &&
83           (m_l2_address.data() == i.m_l2_address.data()) &&
84           (m_state == i.m_state) && (m_rd == i.m_rd) && (m_type == i.m_type) &&
85           (m_oper == i.m_oper));
86 }
87
88 interface::event_listener::event_listener()
89   : m_status(rc_t::NOOP)
90 {
91 }
92
93 HW::item<bool>&
94 interface::event_listener::status()
95 {
96   return (m_status);
97 }
98
99 interface::stat_listener::stat_listener()
100   : m_status(rc_t::NOOP)
101 {
102 }
103
104 HW::item<bool>&
105 interface::stat_listener::status()
106 {
107   return (m_status);
108 }
109
110 /**
111  * Return the interface type
112  */
113 const interface::type_t&
114 interface::type() const
115 {
116   return (m_type);
117 }
118
119 const handle_t&
120 interface::handle() const
121 {
122   return (singular()->handle_i());
123 }
124
125 const handle_t&
126 interface::handle_i() const
127 {
128   return (m_hdl.data());
129 }
130
131 const l2_address_t&
132 interface::l2_address() const
133 {
134   return (m_l2_address.data());
135 }
136
137 interface::const_iterator_t
138 interface::cbegin()
139 {
140   return m_db.cbegin();
141 }
142
143 interface::const_iterator_t
144 interface::cend()
145 {
146   return m_db.cend();
147 }
148
149 void
150 interface::sweep()
151 {
152   if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
153     m_table_id.data() = route::DEFAULT_TABLE;
154     HW::enqueue(
155       new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
156     HW::enqueue(
157       new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
158   }
159
160   if (m_stats) {
161     HW::enqueue(new interface_cmds::stats_disable_cmd(m_hdl.data()));
162     m_stats.reset();
163   }
164
165   // If the interface is up, bring it down
166   if (m_state && interface::admin_state_t::UP == m_state.data()) {
167     m_state.data() = interface::admin_state_t::DOWN;
168     HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
169   }
170
171   if (m_hdl) {
172     std::queue<cmd*> cmds;
173     HW::enqueue(mk_delete_cmd(cmds));
174   }
175   HW::write();
176 }
177
178 void
179 interface::replay()
180 {
181   if (m_hdl) {
182     std::queue<cmd*> cmds;
183     HW::enqueue(mk_create_cmd(cmds));
184   }
185
186   if (m_state && interface::admin_state_t::UP == m_state.data()) {
187     HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
188   }
189
190   if (m_table_id && (m_table_id.data() != route::DEFAULT_TABLE)) {
191     HW::enqueue(
192       new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
193     HW::enqueue(
194       new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
195   }
196 }
197
198 interface::~interface()
199 {
200   sweep();
201   release();
202 }
203
204 void
205 interface::release()
206 {
207   // not in the DB anymore.
208   m_db.release(m_name, this);
209 }
210
211 std::string
212 interface::to_string() const
213 {
214   std::ostringstream s;
215   s << "interface:[" << m_name << " type:" << m_type.to_string()
216     << " hdl:" << m_hdl.to_string() << " l2-address:["
217     << m_l2_address.to_string() << "]";
218
219   if (m_rd) {
220     s << " rd:" << m_rd->to_string();
221   }
222
223   s << " admin-state:" << m_state.to_string()
224     << " oper-state:" << m_oper.to_string() << "]";
225
226   return (s.str());
227 }
228
229 const std::string&
230 interface::name() const
231 {
232   return (m_name);
233 }
234
235 const interface::key_t&
236 interface::key() const
237 {
238   return (name());
239 }
240
241 std::queue<cmd*>&
242 interface::mk_create_cmd(std::queue<cmd*>& q)
243 {
244   if (type_t::LOOPBACK == m_type) {
245     q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
246   } else if (type_t::BVI == m_type) {
247     q.push(new interface_cmds::loopback_create_cmd(m_hdl, m_name));
248     q.push(new interface_cmds::set_tag(m_hdl, m_name));
249   } else if (type_t::AFPACKET == m_type) {
250     q.push(new interface_cmds::af_packet_create_cmd(m_hdl, m_name));
251   } else if (type_t::TAP == m_type) {
252     q.push(new interface_cmds::tap_create_cmd(m_hdl, m_name));
253   }
254
255   return (q);
256 }
257
258 std::queue<cmd*>&
259 interface::mk_delete_cmd(std::queue<cmd*>& q)
260 {
261   if ((type_t::LOOPBACK == m_type) || (type_t::BVI == m_type)) {
262     q.push(new interface_cmds::loopback_delete_cmd(m_hdl));
263   } else if (type_t::AFPACKET == m_type) {
264     q.push(new interface_cmds::af_packet_delete_cmd(m_hdl, m_name));
265   } else if (type_t::TAP == m_type) {
266     q.push(new interface_cmds::tap_delete_cmd(m_hdl));
267   }
268
269   return (q);
270 }
271
272 void
273 interface::update(const interface& desired)
274 {
275   /*
276    * the desired state is always that the interface should be created
277    */
278   if (rc_t::OK != m_hdl.rc()) {
279     std::queue<cmd*> cmds;
280     HW::enqueue(mk_create_cmd(cmds));
281     /*
282      * interface create now, so we can barf early if it fails
283      */
284     HW::write();
285   }
286
287   /*
288    * If the interface is not created do other commands should be issued
289    */
290   if (rc_t::OK != m_hdl.rc())
291     return;
292
293   /*
294    * change the interface state to that which is deisred
295    */
296   if (m_state.update(desired.m_state)) {
297     HW::enqueue(new interface_cmds::state_change_cmd(m_state, m_hdl));
298   }
299
300   /*
301    * change the interface state to that which is deisred
302    */
303   if (m_l2_address.update(desired.m_l2_address)) {
304     HW::enqueue(new interface_cmds::set_mac_cmd(m_l2_address, m_hdl));
305   }
306
307   /*
308    * If the interface is mapped into a route domain, set VPP's
309    * table ID
310    */
311   if (m_rd != desired.m_rd) {
312     /*
313      * changing route domains. need to remove all L3 bindings, swap the table
314      * then reapply the bindings.
315      */
316     auto it = l3_binding::cbegin();
317
318     while (it != l3_binding::cend()) {
319       if (it->second.lock()->itf().key() == key())
320         it->second.lock()->sweep();
321       ++it;
322     }
323     m_rd = desired.m_rd;
324     m_table_id.update(m_rd ? m_rd->table_id() : route::DEFAULT_TABLE);
325     HW::enqueue(
326       new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
327     HW::enqueue(
328       new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
329     HW::write();
330
331     it = l3_binding::cbegin();
332     while (it != l3_binding::cend()) {
333       if (it->second.lock()->itf().key() == key())
334         it->second.lock()->replay(); //(*it->second.lock());
335       ++it;
336     }
337   } else if (!m_table_id && m_rd) {
338     HW::enqueue(
339       new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV4, m_hdl));
340     HW::enqueue(
341       new interface_cmds::set_table_cmd(m_table_id, l3_proto_t::IPV6, m_hdl));
342   }
343 }
344
345 void
346 interface::set(const l2_address_t& addr)
347 {
348   assert(rc_t::UNSET == m_l2_address.rc());
349   m_l2_address.set(rc_t::NOOP);
350   m_l2_address.update(addr);
351 }
352
353 void
354 interface::set(const handle_t& hdl)
355 {
356   m_hdl = hdl;
357 }
358
359 void
360 interface::set(const oper_state_t& state)
361 {
362   m_oper = state;
363 }
364
365 void
366 interface::enable_stats_i(interface::stat_listener& el)
367 {
368   m_stats.reset(new interface_cmds::stats_enable_cmd(el, handle_i()));
369   HW::enqueue(m_stats);
370   HW::write();
371 }
372
373 void
374 interface::enable_stats(interface::stat_listener& el)
375 {
376   singular()->enable_stats_i(el);
377 }
378
379 std::shared_ptr<interface>
380 interface::singular_i() const
381 {
382   return (m_db.find_or_add(key(), *this));
383 }
384
385 std::shared_ptr<interface>
386 interface::singular() const
387 {
388   return singular_i();
389 }
390
391 std::shared_ptr<interface>
392 interface::find(const key_t& k)
393 {
394   return (m_db.find(k));
395 }
396
397 std::shared_ptr<interface>
398 interface::find(const handle_t& handle)
399 {
400   return (m_hdl_db[handle].lock());
401 }
402
403 void
404 interface::add(const key_t& key, const HW::item<handle_t>& item)
405 {
406   std::shared_ptr<interface> sp = find(key);
407
408   if (sp && item) {
409     m_hdl_db[item.data()] = sp;
410   }
411 }
412
413 void
414 interface::remove(const HW::item<handle_t>& item)
415 {
416   m_hdl_db.erase(item.data());
417 }
418
419 void
420 interface::dump(std::ostream& os)
421 {
422   m_db.dump(os);
423 }
424
425 void
426 interface::event_handler::handle_populate(const client_db::key_t& key)
427 {
428   /*
429    * dump VPP current states
430    */
431   std::shared_ptr<interface_cmds::dump_cmd> cmd =
432     std::make_shared<interface_cmds::dump_cmd>();
433
434   HW::enqueue(cmd);
435   HW::write();
436
437   for (auto& itf_record : *cmd) {
438     std::shared_ptr<interface> itf =
439       interface_factory::new_interface(itf_record.get_payload());
440
441     if (itf && interface::type_t::LOCAL != itf->type()) {
442       VOM_LOG(log_level_t::DEBUG) << "dump: " << itf->to_string();
443       /*
444        * Write each of the discovered interfaces into the OM,
445        * but disable the HW Command q whilst we do, so that no
446        * commands are sent to VPP
447        */
448       OM::commit(key, *itf);
449
450       /**
451        * Get the address configured on the interface
452        */
453       std::shared_ptr<l3_binding_cmds::dump_v4_cmd> dcmd =
454         std::make_shared<l3_binding_cmds::dump_v4_cmd>(
455           l3_binding_cmds::dump_v4_cmd(itf->handle()));
456
457       HW::enqueue(dcmd);
458       HW::write();
459
460       for (auto& l3_record : *dcmd) {
461         auto& payload = l3_record.get_payload();
462         const route::prefix_t pfx(payload.is_ipv6, payload.ip,
463                                   payload.prefix_length);
464
465         VOM_LOG(log_level_t::DEBUG) << "dump: " << pfx.to_string();
466
467         l3_binding l3(*itf, pfx);
468         OM::commit(key, l3);
469       }
470     }
471   }
472 }
473
474 interface::event_handler::event_handler()
475 {
476   OM::register_listener(this);
477   inspect::register_handler({ "interface", "intf" }, "interfaces", this);
478 }
479
480 void
481 interface::event_handler::handle_replay()
482 {
483   m_db.replay();
484 }
485
486 dependency_t
487 interface::event_handler::order() const
488 {
489   return (dependency_t::INTERFACE);
490 }
491
492 void
493 interface::event_handler::show(std::ostream& os)
494 {
495   m_db.dump(os);
496 }
497
498 } // namespace VOM
499
500 /*
501  * fd.io coding-style-patch-verification: ON
502  *
503  * Local Variables:
504  * eval: (c-set-style "mozilla")
505  * End:
506  */