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