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