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