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_STATS_API_JSON;
26 namespace interface_cmds {
27 loopback_create_cmd::loopback_create_cmd(HW::item<handle_t>& item,
28 const std::string& name)
29 : create_cmd(item, name)
34 loopback_create_cmd::issue(connection& con)
36 msg_t req(con.ctx(), std::ref(*this));
38 VAPI_CALL(req.execute());
42 if (m_hw_item.rc() == rc_t::OK) {
49 loopback_create_cmd::to_string() const
52 s << "loopback-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
57 af_packet_create_cmd::af_packet_create_cmd(HW::item<handle_t>& item,
58 const std::string& name)
59 : create_cmd(item, name)
64 af_packet_create_cmd::issue(connection& con)
66 msg_t req(con.ctx(), std::ref(*this));
68 auto& payload = req.get_request().get_payload();
70 payload.use_random_hw_addr = 1;
71 memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
72 memcpy(payload.host_if_name, m_name.c_str(),
73 std::min(m_name.length(), sizeof(payload.host_if_name)));
75 VAPI_CALL(req.execute());
79 if (m_hw_item.rc() == rc_t::OK) {
86 af_packet_create_cmd::to_string() const
89 s << "af-packet-itf-create: " << m_hw_item.to_string() << " name:" << m_name;
94 tap_create_cmd::tap_create_cmd(HW::item<handle_t>& item,
95 const std::string& name)
96 : create_cmd(item, name)
101 tap_create_cmd::issue(connection& con)
103 msg_t req(con.ctx(), std::ref(*this));
105 auto& payload = req.get_request().get_payload();
107 memset(payload.tap_name, 0, sizeof(payload.tap_name));
108 memcpy(payload.tap_name, m_name.c_str(),
109 std::min(m_name.length(), sizeof(payload.tap_name)));
110 payload.use_random_mac = 1;
112 VAPI_CALL(req.execute());
116 if (m_hw_item.rc() == rc_t::OK) {
124 tap_create_cmd::to_string() const
126 std::ostringstream s;
127 s << "tap-intf-create: " << m_hw_item.to_string() << " name:" << m_name;
132 loopback_delete_cmd::loopback_delete_cmd(HW::item<handle_t>& item)
138 loopback_delete_cmd::issue(connection& con)
140 msg_t req(con.ctx(), std::ref(*this));
142 auto& payload = req.get_request().get_payload();
143 payload.sw_if_index = m_hw_item.data().value();
145 VAPI_CALL(req.execute());
148 m_hw_item.set(rc_t::NOOP);
155 loopback_delete_cmd::to_string() const
157 std::ostringstream s;
158 s << "loopback-itf-delete: " << m_hw_item.to_string();
163 af_packet_delete_cmd::af_packet_delete_cmd(HW::item<handle_t>& item,
164 const std::string& name)
165 : delete_cmd(item, name)
170 af_packet_delete_cmd::issue(connection& con)
172 msg_t req(con.ctx(), std::ref(*this));
174 auto& payload = req.get_request().get_payload();
175 memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
176 memcpy(payload.host_if_name, m_name.c_str(),
177 std::min(m_name.length(), sizeof(payload.host_if_name)));
179 VAPI_CALL(req.execute());
182 m_hw_item.set(rc_t::NOOP);
188 af_packet_delete_cmd::to_string() const
190 std::ostringstream s;
191 s << "af_packet-itf-delete: " << m_hw_item.to_string();
196 tap_delete_cmd::tap_delete_cmd(HW::item<handle_t>& item)
202 tap_delete_cmd::issue(connection& con)
204 // finally... call VPP
210 tap_delete_cmd::to_string() const
212 std::ostringstream s;
213 s << "tap-itf-delete: " << m_hw_item.to_string();
218 state_change_cmd::state_change_cmd(HW::item<interface::admin_state_t>& state,
219 const HW::item<handle_t>& hdl)
226 state_change_cmd::operator==(const state_change_cmd& other) const
228 return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
232 state_change_cmd::issue(connection& con)
234 msg_t req(con.ctx(), std::ref(*this));
236 auto& payload = req.get_request().get_payload();
237 payload.sw_if_index = m_hdl.data().value();
238 payload.admin_up_down = m_hw_item.data().value();
240 VAPI_CALL(req.execute());
242 m_hw_item.set(wait());
248 state_change_cmd::to_string() const
250 std::ostringstream s;
251 s << "itf-state-change: " << m_hw_item.to_string()
252 << " hdl:" << m_hdl.to_string();
256 set_table_cmd::set_table_cmd(HW::item<route::table_id_t>& table,
257 const l3_proto_t& proto,
258 const HW::item<handle_t>& hdl)
266 set_table_cmd::operator==(const set_table_cmd& other) const
268 return ((m_hdl == other.m_hdl) && (m_proto == other.m_proto) &&
269 (m_hw_item == other.m_hw_item));
273 set_table_cmd::issue(connection& con)
275 msg_t req(con.ctx(), std::ref(*this));
277 auto& payload = req.get_request().get_payload();
278 payload.sw_if_index = m_hdl.data().value();
279 payload.is_ipv6 = m_proto.is_ipv6();
280 payload.vrf_id = m_hw_item.data();
282 VAPI_CALL(req.execute());
284 m_hw_item.set(wait());
290 set_table_cmd::to_string() const
292 std::ostringstream s;
293 s << "itf-set-table: " << m_hw_item.to_string()
294 << " proto:" << m_proto.to_string() << " hdl:" << m_hdl.to_string();
298 set_mac_cmd::set_mac_cmd(HW::item<l2_address_t>& mac,
299 const HW::item<handle_t>& hdl)
306 set_mac_cmd::operator==(const set_mac_cmd& other) const
308 return ((m_hdl == other.m_hdl) && (m_hw_item == other.m_hw_item));
312 set_mac_cmd::issue(connection& con)
314 msg_t req(con.ctx(), std::ref(*this));
316 auto& payload = req.get_request().get_payload();
317 payload.sw_if_index = m_hdl.data().value();
318 m_hw_item.data().to_mac().to_bytes(payload.mac_address,
319 sizeof(payload.mac_address));
321 VAPI_CALL(req.execute());
323 m_hw_item.set(wait());
329 set_mac_cmd::to_string() const
331 std::ostringstream s;
332 s << "itf-set-mac: " << m_hw_item.to_string() << " hdl:" << m_hdl.to_string();
336 events_cmd::events_cmd(interface::event_listener& el)
337 : event_cmd(el.status())
343 events_cmd::operator==(const events_cmd& other) const
349 events_cmd::issue(connection& con)
352 * First set the call back to handle the interface events
354 m_reg.reset(new reg_t(con.ctx(), std::ref(*(static_cast<event_cmd*>(this)))));
357 * then send the request to enable them
359 msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
361 auto& payload = req.get_request().get_payload();
362 payload.enable_disable = 1;
363 payload.pid = getpid();
365 VAPI_CALL(req.execute());
369 return (rc_t::INPROGRESS);
373 events_cmd::retire(connection& con)
376 * disable interface events.
378 msg_t req(con.ctx(), std::ref(*(static_cast<rpc_cmd*>(this))));
380 auto& payload = req.get_request().get_payload();
381 payload.enable_disable = 0;
382 payload.pid = getpid();
384 VAPI_CALL(req.execute());
392 m_listener.handle_interface_event(this);
396 events_cmd::to_string() const
398 return ("itf-events");
402 * Interface statistics
404 stats_cmd::stats_cmd(interface::stat_listener& el,
405 const std::vector<handle_t>& interfaces)
406 : event_cmd(el.status())
408 , m_swifindex(interfaces)
413 stats_cmd::operator==(const stats_cmd& other) const
419 stats_cmd::issue(connection& con)
422 * First set the clal back to handle the interface stats
424 m_reg.reset(new reg_t(con.ctx(), std::ref(*(static_cast<event_cmd*>(this)))));
428 * then send the request to enable them
430 msg_t req(con.ctx(), m_swifindex.size(),
431 std::ref(*(static_cast<rpc_cmd*>(this))));
433 auto& payload = req.get_request().get_payload();
434 payload.enable_disable = 1;
435 payload.pid = getpid();
436 payload.num = m_swifindex.size();
438 auto it = m_swifindex.cbegin();
440 while (it != m_swifindex.cend()) {
441 payload.sw_ifs[ii] = it->value();
446 VAPI_CALL(req.execute());
450 return (rc_t::INPROGRESS);
454 stats_cmd::retire(connection& con)
461 m_listener.handle_interface_stat(this);
465 stats_cmd::to_string() const
467 return ("itf-stats");
475 dump_cmd::operator==(const dump_cmd& other) const
481 dump_cmd::issue(connection& con)
483 m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
485 auto& payload = m_dump->get_request().get_payload();
486 payload.name_filter_valid = 0;
488 VAPI_CALL(m_dump->execute());
496 dump_cmd::to_string() const
501 set_tag::set_tag(HW::item<handle_t>& item, const std::string& name)
508 set_tag::issue(connection& con)
510 msg_t req(con.ctx(), std::ref(*this));
512 auto& payload = req.get_request().get_payload();
514 payload.sw_if_index = m_hw_item.data().value();
515 memcpy(payload.tag, m_name.c_str(), m_name.length());
517 VAPI_CALL(req.execute());
524 set_tag::to_string() const
526 std::ostringstream s;
527 s << "itf-set-tag: " << m_hw_item.to_string() << " name:" << m_name;
533 set_tag::operator==(const set_tag& o) const
535 return ((m_name == o.m_name) && (m_hw_item.data() == o.m_hw_item.data()));
537 }; // namespace interface_cmds
541 * fd.io coding-style-patch-verification: ON
544 * eval: (c-set-style "mozilla")