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/tap_interface_cmds.hpp"
18 #include <vapi/tap.api.vapi.hpp>
19 #include <vapi/tapv2.api.vapi.hpp>
21 DEFINE_VAPI_MSG_IDS_TAP_API_JSON;
22 DEFINE_VAPI_MSG_IDS_TAPV2_API_JSON;
25 namespace tap_interface_cmds {
26 tap_create_cmd::tap_create_cmd(HW::item<handle_t>& item,
27 const std::string& name,
28 route::prefix_t& prefix,
29 const l2_address_t& l2_address)
30 : interface::create_cmd<vapi::Tap_connect>(item, name)
32 , m_l2_address(l2_address)
37 tap_create_cmd::issue(connection& con)
39 msg_t req(con.ctx(), std::ref(*this));
41 auto& payload = req.get_request().get_payload();
42 memset(payload.tap_name, 0, sizeof(payload.tap_name));
43 memcpy(payload.tap_name, m_name.c_str(),
44 std::min(m_name.length(), sizeof(payload.tap_name)));
45 if (m_prefix != route::prefix_t::ZERO) {
46 if (m_prefix.address().is_v6()) {
47 m_prefix.to_vpp(&payload.ip6_address_set, payload.ip6_address,
48 &payload.ip6_mask_width);
50 m_prefix.to_vpp(&payload.ip4_address_set, payload.ip4_address,
51 &payload.ip4_mask_width);
52 payload.ip4_address_set = 1;
56 if (m_l2_address != l2_address_t::ZERO) {
57 m_l2_address.to_bytes(payload.mac_address, 6);
59 payload.use_random_mac = 1;
62 VAPI_CALL(req.execute());
65 if (m_hw_item.rc() == rc_t::OK) {
69 return (m_hw_item.rc());
73 tap_create_cmd::to_string() const
76 s << "tap-intf-create: " << m_hw_item.to_string()
77 << " ip-prefix:" << m_prefix.to_string();
82 tap_delete_cmd::tap_delete_cmd(HW::item<handle_t>& item)
83 : interface::delete_cmd<vapi::Tap_delete>(item)
88 tap_delete_cmd::issue(connection& con)
90 msg_t req(con.ctx(), std::ref(*this));
92 auto& payload = req.get_request().get_payload();
93 payload.sw_if_index = m_hw_item.data().value();
95 VAPI_CALL(req.execute());
98 m_hw_item.set(rc_t::NOOP);
105 tap_delete_cmd::to_string() const
107 std::ostringstream s;
108 s << "tap-itf-delete: " << m_hw_item.to_string();
113 tap_dump_cmd::tap_dump_cmd()
118 tap_dump_cmd::operator==(const tap_dump_cmd& other) const
124 tap_dump_cmd::issue(connection& con)
126 m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
128 VAPI_CALL(m_dump->execute());
136 tap_dump_cmd::to_string() const
138 return ("tap-itf-dump");
144 tapv2_create_cmd::tapv2_create_cmd(HW::item<handle_t>& item,
145 const std::string& name,
146 route::prefix_t& prefix,
147 const l2_address_t& l2_address)
148 : interface::create_cmd<vapi::Tap_create_v2>(item, name)
150 , m_l2_address(l2_address)
155 tapv2_create_cmd::issue(connection& con)
157 msg_t req(con.ctx(), std::ref(*this));
159 auto& payload = req.get_request().get_payload();
160 memset(payload.host_if_name, 0, sizeof(payload.host_if_name));
161 memcpy(payload.host_if_name, m_name.c_str(),
162 std::min(m_name.length(), sizeof(payload.host_if_name)));
163 payload.host_if_name_set = 1;
165 if (m_prefix != route::prefix_t::ZERO) {
166 if (m_prefix.address().is_v6()) {
167 m_prefix.to_vpp(&payload.host_ip6_addr_set, payload.host_ip6_addr,
168 &payload.host_ip6_prefix_len);
170 m_prefix.to_vpp(&payload.host_ip4_addr_set, payload.host_ip4_addr,
171 &payload.host_ip4_prefix_len);
172 payload.host_ip4_addr_set = 1;
176 if (m_l2_address != l2_address_t::ZERO) {
177 m_l2_address.to_bytes(payload.host_mac_addr, 6);
178 payload.host_mac_addr_set = 1;
181 payload.id = 0xffffffff;
182 payload.use_random_mac = 1;
183 payload.tx_ring_sz = 1024;
184 payload.rx_ring_sz = 1024;
186 VAPI_CALL(req.execute());
189 if (m_hw_item.rc() == rc_t::OK) {
197 tapv2_create_cmd::to_string() const
199 std::ostringstream s;
200 s << "tapv2-intf-create: " << m_hw_item.to_string()
201 << " ip-prefix:" << m_prefix.to_string();
206 tapv2_delete_cmd::tapv2_delete_cmd(HW::item<handle_t>& item)
207 : interface::delete_cmd<vapi::Tap_delete_v2>(item)
212 tapv2_delete_cmd::issue(connection& con)
215 msg_t req(con.ctx(), std::ref(*this));
217 auto& payload = req.get_request().get_payload();
219 payload.sw_if_index = m_hw_item.data().value();
221 VAPI_CALL(req.execute());
224 m_hw_item.set(rc_t::NOOP);
230 tapv2_delete_cmd::to_string() const
232 std::ostringstream s;
233 s << "tapv2-itf-delete: " << m_hw_item.to_string();
238 tapv2_dump_cmd::tapv2_dump_cmd()
243 tapv2_dump_cmd::operator==(const tapv2_dump_cmd& other) const
249 tapv2_dump_cmd::issue(connection& con)
251 m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
253 VAPI_CALL(m_dump->execute());
261 tapv2_dump_cmd::to_string() const
263 return ("tapv2-itf-dump");
266 } // namespace tap_interface_cmds
270 * fd.io coding-style-patch-verification: ON
273 * eval: (c-set-style "mozilla")