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 = 1;
157 payload.use_custom_mac = 0;
158 payload.renumber = 0;
160 VAPI_CALL(req.execute());
168 vhost_create_cmd::to_string() const
170 std::ostringstream s;
171 s << "vhost-intf-create: " << m_hw_item.to_string() << " name:" << m_name
177 loopback_delete_cmd::loopback_delete_cmd(HW::item<handle_t>& item)
183 loopback_delete_cmd::issue(connection& con)
185 msg_t req(con.ctx(), std::ref(*this));
187 auto& payload = req.get_request().get_payload();
188 payload.sw_if_index = m_hw_item.data().value();
190 VAPI_CALL(req.execute());
193 m_hw_item.set(rc_t::NOOP);
200 loopback_delete_cmd::to_string() const
202 std::ostringstream s;
203 s << "loopback-itf-delete: " << m_hw_item.to_string();
208 af_packet_delete_cmd::af_packet_delete_cmd(HW::item<handle_t>& item,
209 const std::string& name)
210 : delete_cmd(item, name)
215 af_packet_delete_cmd::issue(connection& con)
217 msg_t req(con.ctx(), std::ref(*this));
219 auto& payload = req.get_request().get_payload();
220 memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
221 memcpy(payload.host_if_name, m_name.c_str(),
222 std::min(m_name.length(), sizeof(payload.host_if_name)));
224 VAPI_CALL(req.execute());
227 m_hw_item.set(rc_t::NOOP);
233 af_packet_delete_cmd::to_string() const
235 std::ostringstream s;
236 s << "af_packet-itf-delete: " << m_hw_item.to_string();
241 tap_delete_cmd::tap_delete_cmd(HW::item<handle_t>& item)
247 tap_delete_cmd::issue(connection& con)
249 // finally... call VPP
255 tap_delete_cmd::to_string() const
257 std::ostringstream s;
258 s << "tap-itf-delete: " << m_hw_item.to_string();
263 vhost_delete_cmd::vhost_delete_cmd(HW::item<handle_t>& item,
264 const std::string& name)
265 : delete_cmd(item, name)
270 vhost_delete_cmd::issue(connection& con)
272 msg_t req(con.ctx(), std::ref(*this));
274 auto& payload = req.get_request().get_payload();
275 payload.sw_if_index = m_hw_item.data().value();
277 VAPI_CALL(req.execute());
284 vhost_delete_cmd::to_string() const
286 std::ostringstream s;
287 s << "vhost-itf-delete: " << m_hw_item.to_string() << " name:" << m_name;
292 state_change_cmd::state_change_cmd(HW::item<interface::admin_state_t>& state,
293 const HW::item<handle_t>& hdl)
300 state_change_cmd::operator==(const state_change_cmd& other) const
302 return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
306 state_change_cmd::issue(connection& con)
308 msg_t req(con.ctx(), std::ref(*this));
310 auto& payload = req.get_request().get_payload();
311 payload.sw_if_index = m_hdl.data().value();
312 payload.admin_up_down = m_hw_item.data().value();
314 VAPI_CALL(req.execute());
316 m_hw_item.set(wait());
322 state_change_cmd::to_string() const
324 std::ostringstream s;
325 s << "itf-state-change: " << m_hw_item.to_string()
326 << " hdl:" << m_hdl.to_string();
330 set_table_cmd::set_table_cmd(HW::item<route::table_id_t>& table,
331 const l3_proto_t& proto,
332 const HW::item<handle_t>& hdl)
340 set_table_cmd::operator==(const set_table_cmd& other) const
342 return ((m_hdl == other.m_hdl) && (m_proto == other.m_proto) &&
343 (m_hw_item == other.m_hw_item));
347 set_table_cmd::issue(connection& con)
349 msg_t req(con.ctx(), std::ref(*this));
351 auto& payload = req.get_request().get_payload();
352 payload.sw_if_index = m_hdl.data().value();
353 payload.is_ipv6 = m_proto.is_ipv6();
354 payload.vrf_id = m_hw_item.data();
356 VAPI_CALL(req.execute());
358 m_hw_item.set(wait());
364 set_table_cmd::to_string() const
366 std::ostringstream s;
367 s << "itf-set-table: " << m_hw_item.to_string()
368 << " proto:" << m_proto.to_string() << " hdl:" << m_hdl.to_string();
372 set_mac_cmd::set_mac_cmd(HW::item<l2_address_t>& mac,
373 const HW::item<handle_t>& hdl)
380 set_mac_cmd::operator==(const set_mac_cmd& other) const
382 return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
386 set_mac_cmd::issue(connection& con)
388 msg_t req(con.ctx(), std::ref(*this));
390 auto& payload = req.get_request().get_payload();
391 payload.sw_if_index = m_hdl.data().value();
392 m_hw_item.data().to_mac().to_bytes(payload.mac_address,
393 sizeof(payload.mac_address));
395 VAPI_CALL(req.execute());
397 m_hw_item.set(wait());
403 set_mac_cmd::to_string() const
405 std::ostringstream s;
406 s << "itf-set-mac: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
410 events_cmd::events_cmd(interface::event_listener& el)
411 : event_cmd(el.status())
417 events_cmd::operator==(const events_cmd& other) const
423 events_cmd::issue(connection& con)
426 * First set the call back to handle the interface events
428 m_reg.reset(new reg_t(con.ctx(), std::ref(*(static_cast<event_cmd*>(this)))));
431 * then send the request to enable them
433 msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
435 auto& payload = req.get_request().get_payload();
436 payload.enable_disable = 1;
437 payload.pid = getpid();
439 VAPI_CALL(req.execute());
447 events_cmd::retire(connection& con)
450 * disable interface events.
452 msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
454 auto& payload = req.get_request().get_payload();
455 payload.enable_disable = 0;
456 payload.pid = getpid();
458 VAPI_CALL(req.execute());
466 m_listener.handle_interface_event(this);
470 events_cmd::to_string() const
472 return ("itf-events");
476 * Interface statistics
478 stats_enable_cmd::stats_enable_cmd(interface::stat_listener& el,
479 const handle_t& handle)
480 : event_cmd(el.status())
482 , m_swifindex(handle)
487 stats_enable_cmd::operator==(const stats_enable_cmd& other) const
493 stats_enable_cmd::issue(connection& con)
496 * First set the call back to handle the interface stats
498 m_reg.reset(new reg_t(con.ctx(), std::ref(*(static_cast<event_cmd*>(this)))));
501 * then send the request to enable them
503 msg_t req(con.ctx(), 1, std::ref(*(static_cast<rpc_cmd*>(this))));
505 auto& payload = req.get_request().get_payload();
506 payload.enable_disable = 1;
507 payload.pid = getpid();
510 payload.sw_ifs[0] = m_swifindex.value();
512 VAPI_CALL(req.execute());
520 stats_enable_cmd::retire(connection& con)
523 * disable interface stats.
525 msg_t req(con.ctx(), 1, std::ref(*(static_cast<rpc_cmd*>(this))));
527 auto& payload = req.get_request().get_payload();
528 payload.enable_disable = 0;
529 payload.pid = getpid();
531 payload.sw_ifs[0] = m_swifindex.value();
533 VAPI_CALL(req.execute());
539 stats_enable_cmd::notify()
541 m_listener.handle_interface_stat(this);
545 stats_enable_cmd::to_string() const
547 std::ostringstream s;
548 s << "itf-stats-enable itf:" << m_swifindex.to_string();
552 stats_disable_cmd::stats_disable_cmd(const handle_t& handle)
554 , m_swifindex(handle)
559 stats_disable_cmd::operator==(const stats_disable_cmd& other) const
565 stats_disable_cmd::issue(connection& con)
568 * then send the request to enable them
570 msg_t req(con.ctx(), 1, std::ref(*this));
572 auto& payload = req.get_request().get_payload();
573 payload.enable_disable = 0;
574 payload.pid = getpid();
577 payload.sw_ifs[0] = m_swifindex.value();
579 VAPI_CALL(req.execute());
587 stats_disable_cmd::to_string() const
589 std::ostringstream s;
590 s << "itf-stats-disable itf:" << m_swifindex.to_string();
599 dump_cmd::operator==(const dump_cmd& other) const
605 dump_cmd::issue(connection& con)
607 m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
609 auto& payload = m_dump->get_request().get_payload();
610 payload.name_filter_valid = 0;
612 VAPI_CALL(m_dump->execute());
620 dump_cmd::to_string() const
625 vhost_dump_cmd::vhost_dump_cmd()
630 vhost_dump_cmd::operator==(const vhost_dump_cmd& other) const
636 vhost_dump_cmd::issue(connection& con)
638 m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
640 VAPI_CALL(m_dump->execute());
648 vhost_dump_cmd::to_string() const
650 return ("vhost-itf-dump");
653 set_tag::set_tag(HW::item<handle_t>& item, const std::string& name)
660 set_tag::issue(connection& con)
662 msg_t req(con.ctx(), std::ref(*this));
664 auto& payload = req.get_request().get_payload();
666 payload.sw_if_index = m_hw_item.data().value();
667 memcpy(payload.tag, m_name.c_str(), m_name.length());
669 VAPI_CALL(req.execute());
676 set_tag::to_string() const
678 std::ostringstream s;
679 s << "itf-set-tag: " << m_hw_item.to_string() << " name:" << m_name;
685 set_tag::operator==(const set_tag& o) const
687 return ((m_name == o.m_name) && (m_hw_item.data() == o.m_hw_item.data()));
689 }; // namespace interface_cmds
693 * fd.io coding-style-patch-verification: ON
696 * eval: (c-set-style "mozilla")