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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "vom/interface_cmds.hpp"
17 #include "vom/cmd.hpp"
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_TAP_API_JSON;
23 DEFINE_VAPI_MSG_IDS_VHOST_USER_API_JSON;
24 DEFINE_VAPI_MSG_IDS_STATS_API_JSON;
27 namespace interface_cmds {
28 loopback_create_cmd::loopback_create_cmd(HW::item<handle_t>& item,
29 const std::string& name)
30 : create_cmd(item, name)
35 loopback_create_cmd::issue(connection& con)
37 msg_t req(con.ctx(), std::ref(*this));
39 VAPI_CALL(req.execute());
43 if (m_hw_item.rc() == rc_t::OK) {
50 loopback_create_cmd::to_string() const
53 s << "loopback-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
58 af_packet_create_cmd::af_packet_create_cmd(HW::item<handle_t>& item,
59 const std::string& name)
60 : create_cmd(item, name)
65 af_packet_create_cmd::issue(connection& con)
67 msg_t req(con.ctx(), std::ref(*this));
69 auto& payload = req.get_request().get_payload();
71 payload.use_random_hw_addr = 1;
72 memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
73 memcpy(payload.host_if_name, m_name.c_str(),
74 std::min(m_name.length(), sizeof(payload.host_if_name)));
76 VAPI_CALL(req.execute());
80 if (m_hw_item.rc() == rc_t::OK) {
87 af_packet_create_cmd::to_string() const
90 s << "af-packet-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
95 tap_create_cmd::tap_create_cmd(HW::item<handle_t>& item,
96 const std::string& name)
97 : create_cmd(item, name)
102 tap_create_cmd::issue(connection& con)
104 msg_t req(con.ctx(), std::ref(*this));
106 auto& payload = req.get_request().get_payload();
108 memset(payload.tap_name, 0, sizeof(payload.tap_name));
109 memcpy(payload.tap_name, m_name.c_str(),
110 std::min(m_name.length(), sizeof(payload.tap_name)));
111 payload.use_random_mac = 1;
113 VAPI_CALL(req.execute());
117 if (m_hw_item.rc() == rc_t::OK) {
125 tap_create_cmd::to_string() const
127 std::ostringstream s;
128 s << "tap-intf-create: " << m_hw_item.to_string() << " name:" << m_name;
133 vhost_create_cmd::vhost_create_cmd(HW::item<handle_t>& item,
134 const std::string& name,
135 const std::string& tag)
136 : create_cmd(item, name)
142 vhost_create_cmd::issue(connection& con)
144 msg_t req(con.ctx(), std::ref(*this));
146 auto& payload = req.get_request().get_payload();
147 memset(payload.sock_filename, 0, sizeof(payload.sock_filename));
148 memcpy(payload.sock_filename, m_name.c_str(),
149 std::min(m_name.length(), sizeof(payload.sock_filename)));
150 memset(payload.tag, 0, sizeof(payload.tag));
153 memcpy(payload.tag, m_tag.c_str(),
154 std::min(m_tag.length(), sizeof(payload.tag)));
156 payload.is_server = 0;
157 payload.use_custom_mac = 0;
158 payload.renumber = 0;
160 VAPI_CALL(req.execute());
164 if (m_hw_item.rc() == rc_t::OK) {
172 vhost_create_cmd::to_string() const
174 std::ostringstream s;
175 s << "vhost-intf-create: " << m_hw_item.to_string() << " name:" << m_name
181 loopback_delete_cmd::loopback_delete_cmd(HW::item<handle_t>& item)
187 loopback_delete_cmd::issue(connection& con)
189 msg_t req(con.ctx(), std::ref(*this));
191 auto& payload = req.get_request().get_payload();
192 payload.sw_if_index = m_hw_item.data().value();
194 VAPI_CALL(req.execute());
197 m_hw_item.set(rc_t::NOOP);
204 loopback_delete_cmd::to_string() const
206 std::ostringstream s;
207 s << "loopback-itf-delete: " << m_hw_item.to_string();
212 af_packet_delete_cmd::af_packet_delete_cmd(HW::item<handle_t>& item,
213 const std::string& name)
214 : delete_cmd(item, name)
219 af_packet_delete_cmd::issue(connection& con)
221 msg_t req(con.ctx(), std::ref(*this));
223 auto& payload = req.get_request().get_payload();
224 memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
225 memcpy(payload.host_if_name, m_name.c_str(),
226 std::min(m_name.length(), sizeof(payload.host_if_name)));
228 VAPI_CALL(req.execute());
231 m_hw_item.set(rc_t::NOOP);
237 af_packet_delete_cmd::to_string() const
239 std::ostringstream s;
240 s << "af_packet-itf-delete: " << m_hw_item.to_string();
245 tap_delete_cmd::tap_delete_cmd(HW::item<handle_t>& item)
251 tap_delete_cmd::issue(connection& con)
253 // finally... call VPP
259 tap_delete_cmd::to_string() const
261 std::ostringstream s;
262 s << "tap-itf-delete: " << m_hw_item.to_string();
267 vhost_delete_cmd::vhost_delete_cmd(HW::item<handle_t>& item,
268 const std::string& name)
269 : delete_cmd(item, name)
274 vhost_delete_cmd::issue(connection& con)
276 msg_t req(con.ctx(), std::ref(*this));
278 auto& payload = req.get_request().get_payload();
279 payload.sw_if_index = m_hw_item.data().value();
281 VAPI_CALL(req.execute());
289 vhost_delete_cmd::to_string() const
291 std::ostringstream s;
292 s << "vhost-itf-delete: " << m_hw_item.to_string() << " name:" << m_name;
297 state_change_cmd::state_change_cmd(HW::item<interface::admin_state_t>& state,
298 const HW::item<handle_t>& hdl)
305 state_change_cmd::operator==(const state_change_cmd& other) const
307 return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
311 state_change_cmd::issue(connection& con)
313 msg_t req(con.ctx(), std::ref(*this));
315 auto& payload = req.get_request().get_payload();
316 payload.sw_if_index = m_hdl.data().value();
317 payload.admin_up_down = m_hw_item.data().value();
319 VAPI_CALL(req.execute());
321 m_hw_item.set(wait());
327 state_change_cmd::to_string() const
329 std::ostringstream s;
330 s << "itf-state-change: " << m_hw_item.to_string()
331 << " hdl:" << m_hdl.to_string();
335 set_table_cmd::set_table_cmd(HW::item<route::table_id_t>& table,
336 const l3_proto_t& proto,
337 const HW::item<handle_t>& hdl)
345 set_table_cmd::operator==(const set_table_cmd& other) const
347 return ((m_hdl == other.m_hdl) && (m_proto == other.m_proto) &&
348 (m_hw_item == other.m_hw_item));
352 set_table_cmd::issue(connection& con)
354 msg_t req(con.ctx(), std::ref(*this));
356 auto& payload = req.get_request().get_payload();
357 payload.sw_if_index = m_hdl.data().value();
358 payload.is_ipv6 = m_proto.is_ipv6();
359 payload.vrf_id = m_hw_item.data();
361 VAPI_CALL(req.execute());
363 m_hw_item.set(wait());
369 set_table_cmd::to_string() const
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();
377 set_mac_cmd::set_mac_cmd(HW::item<l2_address_t>& mac,
378 const HW::item<handle_t>& hdl)
385 set_mac_cmd::operator==(const set_mac_cmd& other) const
387 return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
391 set_mac_cmd::issue(connection& con)
393 msg_t req(con.ctx(), std::ref(*this));
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));
400 VAPI_CALL(req.execute());
402 m_hw_item.set(wait());
408 set_mac_cmd::to_string() const
410 std::ostringstream s;
411 s << "itf-set-mac: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
415 collect_detail_stats_change_cmd::collect_detail_stats_change_cmd(
416 HW::item<interface::stats_type_t>& item,
426 collect_detail_stats_change_cmd::operator==(
427 const collect_detail_stats_change_cmd& other) const
429 return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item) &&
430 (m_enable == other.m_enable));
434 collect_detail_stats_change_cmd::issue(connection& con)
436 msg_t req(con.ctx(), std::ref(*this));
438 auto& payload = req.get_request().get_payload();
439 payload.sw_if_index = m_hdl.value();
440 payload.enable_disable = m_enable;
442 VAPI_CALL(req.execute());
444 m_hw_item.set(wait());
450 collect_detail_stats_change_cmd::to_string() const
452 std::ostringstream s;
453 s << "itf-stats: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
457 events_cmd::events_cmd(interface::event_listener& el)
458 : event_cmd(el.status())
464 events_cmd::operator==(const events_cmd& other) const
470 events_cmd::issue(connection& con)
473 * First set the call back to handle the interface events
475 m_reg.reset(new reg_t(con.ctx(), std::ref(*(static_cast<event_cmd*>(this)))));
478 * then send the request to enable them
480 msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
482 auto& payload = req.get_request().get_payload();
483 payload.enable_disable = 1;
484 payload.pid = getpid();
486 VAPI_CALL(req.execute());
494 events_cmd::retire(connection& con)
497 * disable interface events.
499 msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
501 auto& payload = req.get_request().get_payload();
502 payload.enable_disable = 0;
503 payload.pid = getpid();
505 VAPI_CALL(req.execute());
513 m_listener.handle_interface_event(this);
517 events_cmd::to_string() const
519 return ("itf-events");
523 * Interface statistics
525 stats_enable_cmd::stats_enable_cmd(interface::stat_listener& el,
526 const handle_t& handle)
527 : event_cmd(el.status())
529 , m_swifindex(handle)
534 stats_enable_cmd::operator==(const stats_enable_cmd& other) const
540 stats_enable_cmd::issue(connection& con)
543 * First set the call back to handle the interface stats
545 m_reg.reset(new reg_t(con.ctx(), std::ref(*(static_cast<event_cmd*>(this)))));
548 * then send the request to enable them
550 msg_t req(con.ctx(), 1, std::ref(*(static_cast<rpc_cmd*>(this))));
552 auto& payload = req.get_request().get_payload();
553 payload.enable_disable = 1;
554 payload.pid = getpid();
557 payload.sw_ifs[0] = m_swifindex.value();
559 VAPI_CALL(req.execute());
567 stats_enable_cmd::retire(connection& con)
570 * disable interface stats.
572 msg_t req(con.ctx(), 1, std::ref(*(static_cast<rpc_cmd*>(this))));
574 auto& payload = req.get_request().get_payload();
575 payload.enable_disable = 0;
576 payload.pid = getpid();
578 payload.sw_ifs[0] = m_swifindex.value();
580 VAPI_CALL(req.execute());
585 interface::stat_listener&
586 stats_enable_cmd::listener() const
592 stats_enable_cmd::set(const rc_t& rc)
594 m_listener.status().set(rc);
598 stats_enable_cmd::notify()
600 m_listener.handle_interface_stat(this);
604 stats_enable_cmd::to_string() const
606 std::ostringstream s;
607 s << "itf-stats-enable itf:" << m_swifindex.to_string();
611 stats_disable_cmd::stats_disable_cmd(const handle_t& handle)
613 , m_swifindex(handle)
618 stats_disable_cmd::operator==(const stats_disable_cmd& other) const
624 stats_disable_cmd::issue(connection& con)
627 * then send the request to enable them
629 msg_t req(con.ctx(), 1, std::ref(*this));
631 auto& payload = req.get_request().get_payload();
632 payload.enable_disable = 0;
633 payload.pid = getpid();
636 payload.sw_ifs[0] = m_swifindex.value();
638 VAPI_CALL(req.execute());
646 stats_disable_cmd::to_string() const
648 std::ostringstream s;
649 s << "itf-stats-disable itf:" << m_swifindex.to_string();
658 dump_cmd::operator==(const dump_cmd& other) const
664 dump_cmd::issue(connection& con)
666 m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
668 auto& payload = m_dump->get_request().get_payload();
669 payload.name_filter_valid = 0;
671 VAPI_CALL(m_dump->execute());
679 dump_cmd::to_string() const
684 vhost_dump_cmd::vhost_dump_cmd()
689 vhost_dump_cmd::operator==(const vhost_dump_cmd& other) const
695 vhost_dump_cmd::issue(connection& con)
697 m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
699 VAPI_CALL(m_dump->execute());
707 vhost_dump_cmd::to_string() const
709 return ("vhost-itf-dump");
712 set_tag::set_tag(HW::item<handle_t>& item, const std::string& name)
719 set_tag::issue(connection& con)
721 msg_t req(con.ctx(), std::ref(*this));
723 auto& payload = req.get_request().get_payload();
725 payload.sw_if_index = m_hw_item.data().value();
726 memset(payload.tag, 0, sizeof(payload.tag));
727 memcpy(payload.tag, m_name.c_str(), m_name.length());
729 VAPI_CALL(req.execute());
736 set_tag::to_string() const
738 std::ostringstream s;
739 s << "itf-set-tag: " << m_hw_item.to_string() << " name:" << m_name;
745 set_tag::operator==(const set_tag& o) const
747 return ((m_name == o.m_name) && (m_hw_item.data() == o.m_hw_item.data()));
749 }; // namespace interface_cmds
753 * fd.io coding-style-patch-verification: ON
756 * eval: (c-set-style "mozilla")