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_VHOST_USER_API_JSON;
25 namespace interface_cmds {
27 bvi_create_cmd::bvi_create_cmd(HW::item<handle_t>& item,
28 const std::string& name)
29 : create_cmd(item, name)
34 bvi_create_cmd::issue(connection& con)
36 msg_t req(con.ctx(), std::ref(*this));
38 auto& payload = req.get_request().get_payload();
40 payload.user_instance = ~0;
42 VAPI_CALL(req.execute());
46 if (m_hw_item.rc() == rc_t::OK) {
53 bvi_create_cmd::to_string() const
56 s << "bvi-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
61 loopback_create_cmd::loopback_create_cmd(HW::item<handle_t>& item,
62 const std::string& name)
63 : create_cmd(item, name)
68 loopback_create_cmd::issue(connection& con)
70 msg_t req(con.ctx(), std::ref(*this));
72 VAPI_CALL(req.execute());
76 if (m_hw_item.rc() == rc_t::OK) {
83 loopback_create_cmd::to_string() const
86 s << "loopback-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
91 af_packet_create_cmd::af_packet_create_cmd(HW::item<handle_t>& item,
92 const std::string& name)
93 : create_cmd(item, name)
98 af_packet_create_cmd::issue(connection& con)
100 msg_t req(con.ctx(), std::ref(*this));
102 auto& payload = req.get_request().get_payload();
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)));
109 VAPI_CALL(req.execute());
113 if (m_hw_item.rc() == rc_t::OK) {
120 af_packet_create_cmd::to_string() const
122 std::ostringstream s;
123 s << "af-packet-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
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)
137 vhost_create_cmd::issue(connection& con)
139 msg_t req(con.ctx(), std::ref(*this));
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));
148 memcpy(payload.tag, m_tag.c_str(),
149 std::min(m_tag.length(), sizeof(payload.tag)));
151 payload.is_server = 0;
152 payload.use_custom_mac = 0;
153 payload.renumber = 0;
155 VAPI_CALL(req.execute());
159 if (m_hw_item.rc() == rc_t::OK) {
167 vhost_create_cmd::to_string() const
169 std::ostringstream s;
170 s << "vhost-intf-create: " << m_hw_item.to_string() << " name:" << m_name
176 bvi_delete_cmd::bvi_delete_cmd(HW::item<handle_t>& item)
182 bvi_delete_cmd::issue(connection& con)
184 msg_t req(con.ctx(), std::ref(*this));
186 auto& payload = req.get_request().get_payload();
187 payload.sw_if_index = m_hw_item.data().value();
189 VAPI_CALL(req.execute());
192 m_hw_item.set(rc_t::NOOP);
199 bvi_delete_cmd::to_string() const
201 std::ostringstream s;
202 s << "bvi-itf-delete: " << m_hw_item.to_string();
207 loopback_delete_cmd::loopback_delete_cmd(HW::item<handle_t>& item)
213 loopback_delete_cmd::issue(connection& con)
215 msg_t req(con.ctx(), std::ref(*this));
217 auto& payload = req.get_request().get_payload();
218 payload.sw_if_index = m_hw_item.data().value();
220 VAPI_CALL(req.execute());
223 m_hw_item.set(rc_t::NOOP);
230 loopback_delete_cmd::to_string() const
232 std::ostringstream s;
233 s << "loopback-itf-delete: " << m_hw_item.to_string();
238 af_packet_delete_cmd::af_packet_delete_cmd(HW::item<handle_t>& item,
239 const std::string& name)
240 : delete_cmd(item, name)
245 af_packet_delete_cmd::issue(connection& con)
247 msg_t req(con.ctx(), std::ref(*this));
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)));
254 VAPI_CALL(req.execute());
257 m_hw_item.set(rc_t::NOOP);
263 af_packet_delete_cmd::to_string() const
265 std::ostringstream s;
266 s << "af_packet-itf-delete: " << m_hw_item.to_string();
271 vhost_delete_cmd::vhost_delete_cmd(HW::item<handle_t>& item,
272 const std::string& name)
273 : delete_cmd(item, name)
278 vhost_delete_cmd::issue(connection& con)
280 msg_t req(con.ctx(), std::ref(*this));
282 auto& payload = req.get_request().get_payload();
283 payload.sw_if_index = m_hw_item.data().value();
285 VAPI_CALL(req.execute());
293 vhost_delete_cmd::to_string() const
295 std::ostringstream s;
296 s << "vhost-itf-delete: " << m_hw_item.to_string() << " name:" << m_name;
301 state_change_cmd::state_change_cmd(HW::item<interface::admin_state_t>& state,
302 const HW::item<handle_t>& hdl)
309 state_change_cmd::operator==(const state_change_cmd& other) const
311 return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
315 state_change_cmd::issue(connection& con)
317 msg_t req(con.ctx(), std::ref(*this));
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();
323 VAPI_CALL(req.execute());
329 state_change_cmd::to_string() const
331 std::ostringstream s;
332 s << "itf-state-change: " << m_hw_item.to_string()
333 << " hdl:" << m_hdl.to_string();
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)
347 set_table_cmd::operator==(const set_table_cmd& other) const
349 return ((m_hdl == other.m_hdl) && (m_proto == other.m_proto) &&
350 (m_hw_item == other.m_hw_item));
354 set_table_cmd::issue(connection& con)
356 msg_t req(con.ctx(), std::ref(*this));
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();
363 VAPI_CALL(req.execute());
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());
406 set_mac_cmd::to_string() const
408 std::ostringstream s;
409 s << "itf-set-mac: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
413 collect_detail_stats_change_cmd::collect_detail_stats_change_cmd(
414 HW::item<interface::stats_type_t>& item,
424 collect_detail_stats_change_cmd::operator==(
425 const collect_detail_stats_change_cmd& other) const
427 return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item) &&
428 (m_enable == other.m_enable));
432 collect_detail_stats_change_cmd::issue(connection& con)
434 msg_t req(con.ctx(), std::ref(*this));
436 auto& payload = req.get_request().get_payload();
437 payload.sw_if_index = m_hdl.value();
438 payload.enable_disable = m_enable;
440 VAPI_CALL(req.execute());
446 collect_detail_stats_change_cmd::to_string() const
448 std::ostringstream s;
449 s << "itf-stats: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
453 events_cmd::events_cmd(interface::event_listener& el)
454 : event_cmd(el.status())
460 events_cmd::operator==(const events_cmd& other) const
466 events_cmd::issue(connection& con)
469 * First set the call back to handle the interface events
471 m_reg.reset(new reg_t(con.ctx(), std::ref(*(static_cast<event_cmd*>(this)))));
474 * then send the request to enable them
476 msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
478 auto& payload = req.get_request().get_payload();
479 payload.enable_disable = 1;
480 payload.pid = getpid();
482 VAPI_CALL(req.execute());
490 events_cmd::retire(connection& con)
493 * disable interface events.
495 msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
497 auto& payload = req.get_request().get_payload();
498 payload.enable_disable = 0;
499 payload.pid = getpid();
501 VAPI_CALL(req.execute());
509 std::lock_guard<interface_cmds::events_cmd> lg(*this);
510 std::vector<interface::event> events;
512 for (auto& msg : *this) {
513 auto& payload = msg.get_payload();
515 handle_t handle(payload.sw_if_index);
516 std::shared_ptr<interface> sp = interface::find(handle);
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);
522 VOM_LOG(log_level_t::DEBUG) << "Interface Event: " << sp->to_string()
523 << " state: " << oper_state.to_string();
526 events.push_back({ *sp, oper_state });
532 m_listener.handle_interface_event(events);
536 events_cmd::to_string() const
538 return ("itf-events");
546 dump_cmd::operator==(const dump_cmd& other) const
552 dump_cmd::issue(connection& con)
554 m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
556 auto& payload = m_dump->get_request().get_payload();
557 payload.name_filter_valid = 0;
559 VAPI_CALL(m_dump->execute());
567 dump_cmd::to_string() const
572 vhost_dump_cmd::vhost_dump_cmd()
577 vhost_dump_cmd::operator==(const vhost_dump_cmd& other) const
583 vhost_dump_cmd::issue(connection& con)
585 m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
587 VAPI_CALL(m_dump->execute());
595 vhost_dump_cmd::to_string() const
597 return ("vhost-itf-dump");
601 af_packet_dump_cmd::operator==(const af_packet_dump_cmd& other) const
607 af_packet_dump_cmd::issue(connection& con)
609 m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
611 VAPI_CALL(m_dump->execute());
619 af_packet_dump_cmd::to_string() const
621 return ("af-packet-itf-dump");
624 set_tag::set_tag(HW::item<handle_t>& item, const std::string& name)
631 set_tag::issue(connection& con)
633 msg_t req(con.ctx(), std::ref(*this));
635 auto& payload = req.get_request().get_payload();
637 payload.sw_if_index = m_hw_item.data().value();
638 memset(payload.tag, 0, sizeof(payload.tag));
639 memcpy(payload.tag, m_name.c_str(), m_name.length());
641 VAPI_CALL(req.execute());
646 set_tag::to_string() const
648 std::ostringstream s;
649 s << "itf-set-tag: " << m_hw_item.to_string() << " name:" << m_name;
655 set_tag::operator==(const set_tag& o) const
657 return ((m_name == o.m_name) && (m_hw_item.data() == o.m_hw_item.data()));
659 }; // namespace interface_cmds
663 * fd.io coding-style-patch-verification: ON
666 * eval: (c-set-style "mozilla")