VOM: interface event struct
[vpp.git] / extras / vom / vom / interface_cmds.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_cmds.hpp"
17 #include "vom/cmd.hpp"
18
19 DEFINE_VAPI_MSG_IDS_VPE_API_JSON;
20 DEFINE_VAPI_MSG_IDS_INTERFACE_API_JSON;
21 DEFINE_VAPI_MSG_IDS_AF_PACKET_API_JSON;
22 DEFINE_VAPI_MSG_IDS_VHOST_USER_API_JSON;
23 DEFINE_VAPI_MSG_IDS_STATS_API_JSON;
24
25 namespace VOM {
26 namespace interface_cmds {
27 loopback_create_cmd::loopback_create_cmd(HW::item<handle_t>& item,
28                                          const std::string& name)
29   : create_cmd(item, name)
30 {
31 }
32
33 rc_t
34 loopback_create_cmd::issue(connection& con)
35 {
36   msg_t req(con.ctx(), std::ref(*this));
37
38   VAPI_CALL(req.execute());
39
40   wait();
41
42   if (m_hw_item.rc() == rc_t::OK) {
43     insert_interface();
44   }
45
46   return rc_t::OK;
47 }
48 std::string
49 loopback_create_cmd::to_string() const
50 {
51   std::ostringstream s;
52   s << "loopback-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
53
54   return (s.str());
55 }
56
57 af_packet_create_cmd::af_packet_create_cmd(HW::item<handle_t>& item,
58                                            const std::string& name)
59   : create_cmd(item, name)
60 {
61 }
62
63 rc_t
64 af_packet_create_cmd::issue(connection& con)
65 {
66   msg_t req(con.ctx(), std::ref(*this));
67
68   auto& payload = req.get_request().get_payload();
69
70   payload.use_random_hw_addr = 1;
71   memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
72   memcpy(payload.host_if_name, m_name.c_str(),
73          std::min(m_name.length(), sizeof(payload.host_if_name)));
74
75   VAPI_CALL(req.execute());
76
77   wait();
78
79   if (m_hw_item.rc() == rc_t::OK) {
80     insert_interface();
81   }
82
83   return rc_t::OK;
84 }
85 std::string
86 af_packet_create_cmd::to_string() const
87 {
88   std::ostringstream s;
89   s << "af-packet-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
90
91   return (s.str());
92 }
93
94 vhost_create_cmd::vhost_create_cmd(HW::item<handle_t>& item,
95                                    const std::string& name,
96                                    const std::string& tag)
97   : create_cmd(item, name)
98   , m_tag(tag)
99 {
100 }
101
102 rc_t
103 vhost_create_cmd::issue(connection& con)
104 {
105   msg_t req(con.ctx(), std::ref(*this));
106
107   auto& payload = req.get_request().get_payload();
108   memset(payload.sock_filename, 0, sizeof(payload.sock_filename));
109   memcpy(payload.sock_filename, m_name.c_str(),
110          std::min(m_name.length(), sizeof(payload.sock_filename)));
111   memset(payload.tag, 0, sizeof(payload.tag));
112
113   if (!m_tag.empty())
114     memcpy(payload.tag, m_tag.c_str(),
115            std::min(m_tag.length(), sizeof(payload.tag)));
116
117   payload.is_server = 0;
118   payload.use_custom_mac = 0;
119   payload.renumber = 0;
120
121   VAPI_CALL(req.execute());
122
123   wait();
124
125   if (m_hw_item.rc() == rc_t::OK) {
126     insert_interface();
127   }
128
129   return rc_t::OK;
130 }
131
132 std::string
133 vhost_create_cmd::to_string() const
134 {
135   std::ostringstream s;
136   s << "vhost-intf-create: " << m_hw_item.to_string() << " name:" << m_name
137     << " tag:" << m_tag;
138
139   return (s.str());
140 }
141
142 loopback_delete_cmd::loopback_delete_cmd(HW::item<handle_t>& item)
143   : delete_cmd(item)
144 {
145 }
146
147 rc_t
148 loopback_delete_cmd::issue(connection& con)
149 {
150   msg_t req(con.ctx(), std::ref(*this));
151
152   auto& payload = req.get_request().get_payload();
153   payload.sw_if_index = m_hw_item.data().value();
154
155   VAPI_CALL(req.execute());
156
157   wait();
158   m_hw_item.set(rc_t::NOOP);
159
160   remove_interface();
161   return rc_t::OK;
162 }
163
164 std::string
165 loopback_delete_cmd::to_string() const
166 {
167   std::ostringstream s;
168   s << "loopback-itf-delete: " << m_hw_item.to_string();
169
170   return (s.str());
171 }
172
173 af_packet_delete_cmd::af_packet_delete_cmd(HW::item<handle_t>& item,
174                                            const std::string& name)
175   : delete_cmd(item, name)
176 {
177 }
178
179 rc_t
180 af_packet_delete_cmd::issue(connection& con)
181 {
182   msg_t req(con.ctx(), std::ref(*this));
183
184   auto& payload = req.get_request().get_payload();
185   memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
186   memcpy(payload.host_if_name, m_name.c_str(),
187          std::min(m_name.length(), sizeof(payload.host_if_name)));
188
189   VAPI_CALL(req.execute());
190
191   wait();
192   m_hw_item.set(rc_t::NOOP);
193
194   remove_interface();
195   return rc_t::OK;
196 }
197 std::string
198 af_packet_delete_cmd::to_string() const
199 {
200   std::ostringstream s;
201   s << "af_packet-itf-delete: " << m_hw_item.to_string();
202
203   return (s.str());
204 }
205
206 vhost_delete_cmd::vhost_delete_cmd(HW::item<handle_t>& item,
207                                    const std::string& name)
208   : delete_cmd(item, name)
209 {
210 }
211
212 rc_t
213 vhost_delete_cmd::issue(connection& con)
214 {
215   msg_t req(con.ctx(), std::ref(*this));
216
217   auto& payload = req.get_request().get_payload();
218   payload.sw_if_index = m_hw_item.data().value();
219
220   VAPI_CALL(req.execute());
221
222   wait();
223   remove_interface();
224
225   return rc_t::OK;
226 }
227 std::string
228 vhost_delete_cmd::to_string() const
229 {
230   std::ostringstream s;
231   s << "vhost-itf-delete: " << m_hw_item.to_string() << " name:" << m_name;
232
233   return (s.str());
234 }
235
236 state_change_cmd::state_change_cmd(HW::item<interface::admin_state_t>& state,
237                                    const HW::item<handle_t>& hdl)
238   : rpc_cmd(state)
239   , m_hdl(hdl)
240 {
241 }
242
243 bool
244 state_change_cmd::operator==(const state_change_cmd& other) const
245 {
246   return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
247 }
248
249 rc_t
250 state_change_cmd::issue(connection& con)
251 {
252   msg_t req(con.ctx(), std::ref(*this));
253
254   auto& payload = req.get_request().get_payload();
255   payload.sw_if_index = m_hdl.data().value();
256   payload.admin_up_down = m_hw_item.data().value();
257
258   VAPI_CALL(req.execute());
259
260   return (wait());
261 }
262
263 std::string
264 state_change_cmd::to_string() const
265 {
266   std::ostringstream s;
267   s << "itf-state-change: " << m_hw_item.to_string()
268     << " hdl:" << m_hdl.to_string();
269   return (s.str());
270 }
271
272 set_table_cmd::set_table_cmd(HW::item<route::table_id_t>& table,
273                              const l3_proto_t& proto,
274                              const HW::item<handle_t>& hdl)
275   : rpc_cmd(table)
276   , m_hdl(hdl)
277   , m_proto(proto)
278 {
279 }
280
281 bool
282 set_table_cmd::operator==(const set_table_cmd& other) const
283 {
284   return ((m_hdl == other.m_hdl) && (m_proto == other.m_proto) &&
285           (m_hw_item == other.m_hw_item));
286 }
287
288 rc_t
289 set_table_cmd::issue(connection& con)
290 {
291   msg_t req(con.ctx(), std::ref(*this));
292
293   auto& payload = req.get_request().get_payload();
294   payload.sw_if_index = m_hdl.data().value();
295   payload.is_ipv6 = m_proto.is_ipv6();
296   payload.vrf_id = m_hw_item.data();
297
298   VAPI_CALL(req.execute());
299
300   return (wait());
301 }
302
303 std::string
304 set_table_cmd::to_string() const
305 {
306   std::ostringstream s;
307   s << "itf-set-table: " << m_hw_item.to_string()
308     << " proto:" << m_proto.to_string() << " hdl:" << m_hdl.to_string();
309   return (s.str());
310 }
311
312 set_mac_cmd::set_mac_cmd(HW::item<l2_address_t>& mac,
313                          const HW::item<handle_t>& hdl)
314   : rpc_cmd(mac)
315   , m_hdl(hdl)
316 {
317 }
318
319 bool
320 set_mac_cmd::operator==(const set_mac_cmd& other) const
321 {
322   return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
323 }
324
325 rc_t
326 set_mac_cmd::issue(connection& con)
327 {
328   msg_t req(con.ctx(), std::ref(*this));
329
330   auto& payload = req.get_request().get_payload();
331   payload.sw_if_index = m_hdl.data().value();
332   m_hw_item.data().to_mac().to_bytes(payload.mac_address,
333                                      sizeof(payload.mac_address));
334
335   VAPI_CALL(req.execute());
336
337   return (wait());
338 }
339
340 std::string
341 set_mac_cmd::to_string() const
342 {
343   std::ostringstream s;
344   s << "itf-set-mac: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
345   return (s.str());
346 }
347
348 collect_detail_stats_change_cmd::collect_detail_stats_change_cmd(
349   HW::item<interface::stats_type_t>& item,
350   const handle_t& hdl,
351   bool enable)
352   : rpc_cmd(item)
353   , m_hdl(hdl)
354   , m_enable(enable)
355 {
356 }
357
358 bool
359 collect_detail_stats_change_cmd::operator==(
360   const collect_detail_stats_change_cmd& other) const
361 {
362   return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item) &&
363           (m_enable == other.m_enable));
364 }
365
366 rc_t
367 collect_detail_stats_change_cmd::issue(connection& con)
368 {
369   msg_t req(con.ctx(), std::ref(*this));
370
371   auto& payload = req.get_request().get_payload();
372   payload.sw_if_index = m_hdl.value();
373   payload.enable_disable = m_enable;
374
375   VAPI_CALL(req.execute());
376
377   return (wait());
378 }
379
380 std::string
381 collect_detail_stats_change_cmd::to_string() const
382 {
383   std::ostringstream s;
384   s << "itf-stats: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
385   return (s.str());
386 }
387
388 events_cmd::events_cmd(interface::event_listener& el)
389   : event_cmd(el.status())
390   , m_listener(el)
391 {
392 }
393
394 bool
395 events_cmd::operator==(const events_cmd& other) const
396 {
397   return (true);
398 }
399
400 rc_t
401 events_cmd::issue(connection& con)
402 {
403   /*
404    * First set the call back to handle the interface events
405    */
406   m_reg.reset(new reg_t(con.ctx(), std::ref(*(static_cast<event_cmd*>(this)))));
407
408   /*
409    * then send the request to enable them
410    */
411   msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
412
413   auto& payload = req.get_request().get_payload();
414   payload.enable_disable = 1;
415   payload.pid = getpid();
416
417   VAPI_CALL(req.execute());
418
419   wait();
420
421   return (rc_t::OK);
422 }
423
424 void
425 events_cmd::retire(connection& con)
426 {
427   /*
428    * disable interface events.
429    */
430   msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
431
432   auto& payload = req.get_request().get_payload();
433   payload.enable_disable = 0;
434   payload.pid = getpid();
435
436   VAPI_CALL(req.execute());
437
438   wait();
439 }
440
441 void
442 events_cmd::notify()
443 {
444   std::lock_guard<interface_cmds::events_cmd> lg(*this);
445   std::vector<interface::event> events;
446
447   for (auto& msg : *this) {
448     auto& payload = msg.get_payload();
449
450     handle_t handle(payload.sw_if_index);
451     std::shared_ptr<interface> sp = interface::find(handle);
452
453     if (sp) {
454       interface::oper_state_t oper_state =
455         interface::oper_state_t::from_int(payload.link_up_down);
456
457       VOM_LOG(log_level_t::DEBUG) << "Interface Event: " << sp->to_string()
458                                   << " state: " << oper_state.to_string();
459
460       sp->set(oper_state);
461       events.push_back({ *sp, oper_state });
462     }
463   }
464
465   flush();
466
467   m_listener.handle_interface_event(events);
468 }
469
470 std::string
471 events_cmd::to_string() const
472 {
473   return ("itf-events");
474 }
475
476 /**
477  * Interface statistics
478  */
479 stats_enable_cmd::stats_enable_cmd(interface::stat_listener& el,
480                                    const handle_t& handle)
481   : event_cmd(el.status())
482   , m_listener(el)
483   , m_swifindex(handle)
484 {
485 }
486
487 bool
488 stats_enable_cmd::operator==(const stats_enable_cmd& other) const
489 {
490   return (true);
491 }
492
493 rc_t
494 stats_enable_cmd::issue(connection& con)
495 {
496   /*
497    * First set the call back to handle the interface stats
498    */
499   m_reg.reset(new reg_t(con.ctx(), std::ref(*(static_cast<event_cmd*>(this)))));
500
501   /*
502    * then send the request to enable them
503    */
504   msg_t req(con.ctx(), 1, std::ref(*(static_cast<rpc_cmd*>(this))));
505
506   auto& payload = req.get_request().get_payload();
507   payload.enable_disable = 1;
508   payload.pid = getpid();
509   payload.num = 1;
510
511   payload.sw_ifs[0] = m_swifindex.value();
512
513   VAPI_CALL(req.execute());
514
515   wait();
516
517   return (rc_t::OK);
518 }
519
520 void
521 stats_enable_cmd::retire(connection& con)
522 {
523   /*
524    * disable interface stats.
525    */
526   msg_t req(con.ctx(), 1, std::ref(*(static_cast<rpc_cmd*>(this))));
527
528   auto& payload = req.get_request().get_payload();
529   payload.enable_disable = 0;
530   payload.pid = getpid();
531   payload.num = 1;
532   payload.sw_ifs[0] = m_swifindex.value();
533
534   VAPI_CALL(req.execute());
535
536   wait();
537 }
538
539 interface::stat_listener&
540 stats_enable_cmd::listener() const
541 {
542   return m_listener;
543 }
544
545 void
546 stats_enable_cmd::set(const rc_t& rc)
547 {
548   m_listener.status().set(rc);
549 }
550
551 void
552 stats_enable_cmd::notify()
553 {
554   m_listener.handle_interface_stat(this);
555 }
556
557 std::string
558 stats_enable_cmd::to_string() const
559 {
560   std::ostringstream s;
561   s << "itf-stats-enable itf:" << m_swifindex.to_string();
562   return (s.str());
563 }
564
565 stats_disable_cmd::stats_disable_cmd(const handle_t& handle)
566   : rpc_cmd(m_res)
567   , m_swifindex(handle)
568 {
569 }
570
571 bool
572 stats_disable_cmd::operator==(const stats_disable_cmd& other) const
573 {
574   return (true);
575 }
576
577 rc_t
578 stats_disable_cmd::issue(connection& con)
579 {
580   /*
581    * then send the request to enable them
582    */
583   msg_t req(con.ctx(), 1, std::ref(*this));
584
585   auto& payload = req.get_request().get_payload();
586   payload.enable_disable = 0;
587   payload.pid = getpid();
588   payload.num = 1;
589
590   payload.sw_ifs[0] = m_swifindex.value();
591
592   VAPI_CALL(req.execute());
593
594   wait();
595
596   return (rc_t::OK);
597 }
598
599 std::string
600 stats_disable_cmd::to_string() const
601 {
602   std::ostringstream s;
603   s << "itf-stats-disable itf:" << m_swifindex.to_string();
604   return (s.str());
605 }
606
607 dump_cmd::dump_cmd()
608 {
609 }
610
611 bool
612 dump_cmd::operator==(const dump_cmd& other) const
613 {
614   return (true);
615 }
616
617 rc_t
618 dump_cmd::issue(connection& con)
619 {
620   m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
621
622   auto& payload = m_dump->get_request().get_payload();
623   payload.name_filter_valid = 0;
624
625   VAPI_CALL(m_dump->execute());
626
627   wait();
628
629   return rc_t::OK;
630 }
631
632 std::string
633 dump_cmd::to_string() const
634 {
635   return ("itf-dump");
636 }
637
638 vhost_dump_cmd::vhost_dump_cmd()
639 {
640 }
641
642 bool
643 vhost_dump_cmd::operator==(const vhost_dump_cmd& other) const
644 {
645   return (true);
646 }
647
648 rc_t
649 vhost_dump_cmd::issue(connection& con)
650 {
651   m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
652
653   VAPI_CALL(m_dump->execute());
654
655   wait();
656
657   return rc_t::OK;
658 }
659
660 std::string
661 vhost_dump_cmd::to_string() const
662 {
663   return ("vhost-itf-dump");
664 }
665
666 bool
667 af_packet_dump_cmd::operator==(const af_packet_dump_cmd& other) const
668 {
669   return (true);
670 }
671
672 rc_t
673 af_packet_dump_cmd::issue(connection& con)
674 {
675   m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
676
677   VAPI_CALL(m_dump->execute());
678
679   wait();
680
681   return rc_t::OK;
682 }
683
684 std::string
685 af_packet_dump_cmd::to_string() const
686 {
687   return ("af-packet-itf-dump");
688 }
689
690 set_tag::set_tag(HW::item<handle_t>& item, const std::string& name)
691   : rpc_cmd(item)
692   , m_name(name)
693 {
694 }
695
696 rc_t
697 set_tag::issue(connection& con)
698 {
699   msg_t req(con.ctx(), std::ref(*this));
700
701   auto& payload = req.get_request().get_payload();
702   payload.is_add = 1;
703   payload.sw_if_index = m_hw_item.data().value();
704   memset(payload.tag, 0, sizeof(payload.tag));
705   memcpy(payload.tag, m_name.c_str(), m_name.length());
706
707   VAPI_CALL(req.execute());
708
709   return (wait());
710 }
711 std::string
712 set_tag::to_string() const
713 {
714   std::ostringstream s;
715   s << "itf-set-tag: " << m_hw_item.to_string() << " name:" << m_name;
716
717   return (s.str());
718 }
719
720 bool
721 set_tag::operator==(const set_tag& o) const
722 {
723   return ((m_name == o.m_name) && (m_hw_item.data() == o.m_hw_item.data()));
724 }
725 }; // namespace interface_cmds
726 }; // namespace VOM
727
728 /*
729  * fd.io coding-style-patch-verification: ON
730  *
731  * Local Variables:
732  * eval: (c-set-style "mozilla")
733  * End:
734  */