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