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