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 {
26 loopback_create_cmd::loopback_create_cmd(HW::item<handle_t>& item,
27 const std::string& name)
28 : create_cmd(item, name)
33 loopback_create_cmd::issue(connection& con)
35 msg_t req(con.ctx(), std::ref(*this));
37 VAPI_CALL(req.execute());
41 if (m_hw_item.rc() == rc_t::OK) {
48 loopback_create_cmd::to_string() const
51 s << "loopback-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
56 af_packet_create_cmd::af_packet_create_cmd(HW::item<handle_t>& item,
57 const std::string& name)
58 : create_cmd(item, name)
63 af_packet_create_cmd::issue(connection& con)
65 msg_t req(con.ctx(), std::ref(*this));
67 auto& payload = req.get_request().get_payload();
69 payload.use_random_hw_addr = 1;
70 memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
71 memcpy(payload.host_if_name, m_name.c_str(),
72 std::min(m_name.length(), sizeof(payload.host_if_name)));
74 VAPI_CALL(req.execute());
78 if (m_hw_item.rc() == rc_t::OK) {
85 af_packet_create_cmd::to_string() const
88 s << "af-packet-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
93 vhost_create_cmd::vhost_create_cmd(HW::item<handle_t>& item,
94 const std::string& name,
95 const std::string& tag)
96 : create_cmd(item, name)
102 vhost_create_cmd::issue(connection& con)
104 msg_t req(con.ctx(), std::ref(*this));
106 auto& payload = req.get_request().get_payload();
107 memset(payload.sock_filename, 0, sizeof(payload.sock_filename));
108 memcpy(payload.sock_filename, m_name.c_str(),
109 std::min(m_name.length(), sizeof(payload.sock_filename)));
110 memset(payload.tag, 0, sizeof(payload.tag));
113 memcpy(payload.tag, m_tag.c_str(),
114 std::min(m_tag.length(), sizeof(payload.tag)));
116 payload.is_server = 0;
117 payload.use_custom_mac = 0;
118 payload.renumber = 0;
120 VAPI_CALL(req.execute());
124 if (m_hw_item.rc() == rc_t::OK) {
132 vhost_create_cmd::to_string() const
134 std::ostringstream s;
135 s << "vhost-intf-create: " << m_hw_item.to_string() << " name:" << m_name
141 loopback_delete_cmd::loopback_delete_cmd(HW::item<handle_t>& item)
147 loopback_delete_cmd::issue(connection& con)
149 msg_t req(con.ctx(), std::ref(*this));
151 auto& payload = req.get_request().get_payload();
152 payload.sw_if_index = m_hw_item.data().value();
154 VAPI_CALL(req.execute());
157 m_hw_item.set(rc_t::NOOP);
164 loopback_delete_cmd::to_string() const
166 std::ostringstream s;
167 s << "loopback-itf-delete: " << m_hw_item.to_string();
172 af_packet_delete_cmd::af_packet_delete_cmd(HW::item<handle_t>& item,
173 const std::string& name)
174 : delete_cmd(item, name)
179 af_packet_delete_cmd::issue(connection& con)
181 msg_t req(con.ctx(), std::ref(*this));
183 auto& payload = req.get_request().get_payload();
184 memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
185 memcpy(payload.host_if_name, m_name.c_str(),
186 std::min(m_name.length(), sizeof(payload.host_if_name)));
188 VAPI_CALL(req.execute());
191 m_hw_item.set(rc_t::NOOP);
197 af_packet_delete_cmd::to_string() const
199 std::ostringstream s;
200 s << "af_packet-itf-delete: " << m_hw_item.to_string();
205 vhost_delete_cmd::vhost_delete_cmd(HW::item<handle_t>& item,
206 const std::string& name)
207 : delete_cmd(item, name)
212 vhost_delete_cmd::issue(connection& con)
214 msg_t req(con.ctx(), std::ref(*this));
216 auto& payload = req.get_request().get_payload();
217 payload.sw_if_index = m_hw_item.data().value();
219 VAPI_CALL(req.execute());
227 vhost_delete_cmd::to_string() const
229 std::ostringstream s;
230 s << "vhost-itf-delete: " << m_hw_item.to_string() << " name:" << m_name;
235 state_change_cmd::state_change_cmd(HW::item<interface::admin_state_t>& state,
236 const HW::item<handle_t>& hdl)
243 state_change_cmd::operator==(const state_change_cmd& other) const
245 return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
249 state_change_cmd::issue(connection& con)
251 msg_t req(con.ctx(), std::ref(*this));
253 auto& payload = req.get_request().get_payload();
254 payload.sw_if_index = m_hdl.data().value();
255 payload.admin_up_down = m_hw_item.data().value();
257 VAPI_CALL(req.execute());
263 state_change_cmd::to_string() const
265 std::ostringstream s;
266 s << "itf-state-change: " << m_hw_item.to_string()
267 << " hdl:" << m_hdl.to_string();
271 set_table_cmd::set_table_cmd(HW::item<route::table_id_t>& table,
272 const l3_proto_t& proto,
273 const HW::item<handle_t>& hdl)
281 set_table_cmd::operator==(const set_table_cmd& other) const
283 return ((m_hdl == other.m_hdl) && (m_proto == other.m_proto) &&
284 (m_hw_item == other.m_hw_item));
288 set_table_cmd::issue(connection& con)
290 msg_t req(con.ctx(), std::ref(*this));
292 auto& payload = req.get_request().get_payload();
293 payload.sw_if_index = m_hdl.data().value();
294 payload.is_ipv6 = m_proto.is_ipv6();
295 payload.vrf_id = m_hw_item.data();
297 VAPI_CALL(req.execute());
303 set_table_cmd::to_string() const
305 std::ostringstream s;
306 s << "itf-set-table: " << m_hw_item.to_string()
307 << " proto:" << m_proto.to_string() << " hdl:" << m_hdl.to_string();
311 set_mac_cmd::set_mac_cmd(HW::item<l2_address_t>& mac,
312 const HW::item<handle_t>& hdl)
319 set_mac_cmd::operator==(const set_mac_cmd& other) const
321 return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
325 set_mac_cmd::issue(connection& con)
327 msg_t req(con.ctx(), std::ref(*this));
329 auto& payload = req.get_request().get_payload();
330 payload.sw_if_index = m_hdl.data().value();
331 m_hw_item.data().to_mac().to_bytes(payload.mac_address,
332 sizeof(payload.mac_address));
334 VAPI_CALL(req.execute());
340 set_mac_cmd::to_string() const
342 std::ostringstream s;
343 s << "itf-set-mac: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
347 collect_detail_stats_change_cmd::collect_detail_stats_change_cmd(
348 HW::item<interface::stats_type_t>& item,
358 collect_detail_stats_change_cmd::operator==(
359 const collect_detail_stats_change_cmd& other) const
361 return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item) &&
362 (m_enable == other.m_enable));
366 collect_detail_stats_change_cmd::issue(connection& con)
368 msg_t req(con.ctx(), std::ref(*this));
370 auto& payload = req.get_request().get_payload();
371 payload.sw_if_index = m_hdl.value();
372 payload.enable_disable = m_enable;
374 VAPI_CALL(req.execute());
380 collect_detail_stats_change_cmd::to_string() const
382 std::ostringstream s;
383 s << "itf-stats: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
387 events_cmd::events_cmd(interface::event_listener& el)
388 : event_cmd(el.status())
394 events_cmd::operator==(const events_cmd& other) const
400 events_cmd::issue(connection& con)
403 * First set the call back to handle the interface events
405 m_reg.reset(new reg_t(con.ctx(), std::ref(*(static_cast<event_cmd*>(this)))));
408 * then send the request to enable them
410 msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
412 auto& payload = req.get_request().get_payload();
413 payload.enable_disable = 1;
414 payload.pid = getpid();
416 VAPI_CALL(req.execute());
424 events_cmd::retire(connection& con)
427 * disable interface events.
429 msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
431 auto& payload = req.get_request().get_payload();
432 payload.enable_disable = 0;
433 payload.pid = getpid();
435 VAPI_CALL(req.execute());
443 std::lock_guard<interface_cmds::events_cmd> lg(*this);
444 std::vector<interface::event> events;
446 for (auto& msg : *this) {
447 auto& payload = msg.get_payload();
449 handle_t handle(payload.sw_if_index);
450 std::shared_ptr<interface> sp = interface::find(handle);
453 interface::oper_state_t oper_state =
454 interface::oper_state_t::from_int(payload.link_up_down);
456 VOM_LOG(log_level_t::DEBUG) << "Interface Event: " << sp->to_string()
457 << " state: " << oper_state.to_string();
460 events.push_back({ *sp, oper_state });
466 m_listener.handle_interface_event(events);
470 events_cmd::to_string() const
472 return ("itf-events");
480 dump_cmd::operator==(const dump_cmd& other) const
486 dump_cmd::issue(connection& con)
488 m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
490 auto& payload = m_dump->get_request().get_payload();
491 payload.name_filter_valid = 0;
493 VAPI_CALL(m_dump->execute());
501 dump_cmd::to_string() const
506 vhost_dump_cmd::vhost_dump_cmd()
511 vhost_dump_cmd::operator==(const vhost_dump_cmd& other) const
517 vhost_dump_cmd::issue(connection& con)
519 m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
521 VAPI_CALL(m_dump->execute());
529 vhost_dump_cmd::to_string() const
531 return ("vhost-itf-dump");
535 af_packet_dump_cmd::operator==(const af_packet_dump_cmd& other) const
541 af_packet_dump_cmd::issue(connection& con)
543 m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
545 VAPI_CALL(m_dump->execute());
553 af_packet_dump_cmd::to_string() const
555 return ("af-packet-itf-dump");
558 set_tag::set_tag(HW::item<handle_t>& item, const std::string& name)
565 set_tag::issue(connection& con)
567 msg_t req(con.ctx(), std::ref(*this));
569 auto& payload = req.get_request().get_payload();
571 payload.sw_if_index = m_hw_item.data().value();
572 memset(payload.tag, 0, sizeof(payload.tag));
573 memcpy(payload.tag, m_name.c_str(), m_name.length());
575 VAPI_CALL(req.execute());
580 set_tag::to_string() const
582 std::ostringstream s;
583 s << "itf-set-tag: " << m_hw_item.to_string() << " name:" << m_name;
589 set_tag::operator==(const set_tag& o) const
591 return ((m_name == o.m_name) && (m_hw_item.data() == o.m_hw_item.data()));
593 }; // namespace interface_cmds
597 * fd.io coding-style-patch-verification: ON
600 * eval: (c-set-style "mozilla")