FD.io VPP  v18.11-rc0-18-g2a3fb1a
Vector Packet Processing
tap_interface_cmds.cpp
Go to the documentation of this file.
1 /*
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:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 
17 
18 #include <vapi/tap.api.vapi.hpp>
19 #include <vapi/tapv2.api.vapi.hpp>
20 
23 
24 namespace VOM {
25 namespace tap_interface_cmds {
27  const std::string& name,
29  const l2_address_t& l2_address)
30  : interface::create_cmd<vapi::Tap_connect>(item, name)
31  , m_prefix(prefix)
32  , m_l2_address(l2_address)
33 {
34 }
35 
36 rc_t
38 {
39  msg_t req(con.ctx(), std::ref(*this));
40 
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);
49  } else {
50  m_prefix.to_vpp(&payload.ip4_address_set, payload.ip4_address,
51  &payload.ip4_mask_width);
52  payload.ip4_address_set = 1;
53  }
54  }
55 
56  if (m_l2_address != l2_address_t::ZERO) {
57  m_l2_address.to_bytes(payload.mac_address, 6);
58  } else {
59  payload.use_random_mac = 1;
60  }
61 
62  VAPI_CALL(req.execute());
63 
64  wait();
65  if (m_hw_item.rc() == rc_t::OK) {
67  }
68 
69  return (m_hw_item.rc());
70 }
71 
72 std::string
74 {
75  std::ostringstream s;
76  s << "tap-intf-create: " << m_hw_item.to_string()
77  << " ip-prefix:" << m_prefix.to_string();
78 
79  return (s.str());
80 }
81 
83  : interface::delete_cmd<vapi::Tap_delete>(item)
84 {
85 }
86 
87 rc_t
89 {
90  msg_t req(con.ctx(), std::ref(*this));
91 
92  auto& payload = req.get_request().get_payload();
93  payload.sw_if_index = m_hw_item.data().value();
94 
95  VAPI_CALL(req.execute());
96 
97  wait();
99 
101  return rc_t::OK;
102 }
103 
104 std::string
106 {
107  std::ostringstream s;
108  s << "tap-itf-delete: " << m_hw_item.to_string();
109 
110  return (s.str());
111 }
112 
114 {
115 }
116 
117 bool
119 {
120  return (true);
121 }
122 
123 rc_t
125 {
126  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
127 
128  VAPI_CALL(m_dump->execute());
129 
130  wait();
131 
132  return rc_t::OK;
133 }
134 
135 std::string
137 {
138  return ("tap-itf-dump");
139 }
140 
141 /*
142  * TAPV2
143  */
145  const std::string& name,
147  const l2_address_t& l2_address)
148  : interface::create_cmd<vapi::Tap_create_v2>(item, name)
149  , m_prefix(prefix)
150  , m_l2_address(l2_address)
151 {
152 }
153 
154 rc_t
156 {
157  msg_t req(con.ctx(), std::ref(*this));
158 
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;
164 
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);
169  } else {
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;
173  }
174  }
175 
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;
179  }
180 
181  payload.id = 0xffffffff;
182  payload.use_random_mac = 1;
183  payload.tx_ring_sz = 1024;
184  payload.rx_ring_sz = 1024;
185 
186  VAPI_CALL(req.execute());
187 
188  m_hw_item = wait();
189  if (m_hw_item.rc() == rc_t::OK) {
191  }
192 
193  return rc_t::OK;
194 }
195 
196 std::string
198 {
199  std::ostringstream s;
200  s << "tapv2-intf-create: " << m_hw_item.to_string()
201  << " ip-prefix:" << m_prefix.to_string();
202 
203  return (s.str());
204 }
205 
207  : interface::delete_cmd<vapi::Tap_delete_v2>(item)
208 {
209 }
210 
211 rc_t
213 {
214 
215  msg_t req(con.ctx(), std::ref(*this));
216 
217  auto& payload = req.get_request().get_payload();
218 
219  payload.sw_if_index = m_hw_item.data().value();
220 
221  VAPI_CALL(req.execute());
222 
223  wait();
225 
227  return rc_t::OK;
228 }
229 std::string
231 {
232  std::ostringstream s;
233  s << "tapv2-itf-delete: " << m_hw_item.to_string();
234 
235  return (s.str());
236 }
237 
239 {
240 }
241 
242 bool
244 {
245  return (true);
246 }
247 
248 rc_t
250 {
251  m_dump.reset(new msg_t(con.ctx(), std::ref(*this)));
252 
253  VAPI_CALL(m_dump->execute());
254 
255  wait();
256 
257  return rc_t::OK;
258 }
259 
260 std::string
262 {
263  return ("tapv2-itf-dump");
264 }
265 
266 } // namespace tap_interface_cmds
267 } // namespace VOM
268 
269 /*
270  * fd.io coding-style-patch-verification: ON
271  *
272  * Local Variables:
273  * eval: (c-set-style "mozilla")
274  * End:
275  */
static const rc_t NOOP
The HW write/update action was/has not been attempted.
Definition: types.hpp:99
std::string to_string() const
convert to string format for debug purposes
void remove_interface()
remove the deleted interface from the DB
Definition: interface.hpp:386
uint32_t value() const
get the value of the handle
Definition: types.cpp:93
void to_bytes(uint8_t *array, uint8_t len) const
Convert to byte array.
Definition: types.cpp:208
rc_t issue(connection &con)
Issue the command to VPP/HW.
Error codes that VPP will return during a HW write.
Definition: types.hpp:84
static const l2_address_t ZERO
An all 0&#39;s L2 address.
Definition: types.hpp:353
std::string to_string() const
convert to string format for debug purposes
Definition: hw.hpp:160
Type def of a L2 address as read from VPP.
Definition: types.hpp:334
rc_t wait()
Wait on the commands promise.
Definition: rpc_cmd.hpp:80
T & data()
Return the data read/written.
Definition: hw.hpp:108
Forward declarations.
Definition: vapi.hpp:44
std::string to_string() const
convert to string format for debug purposes
rc_t issue(connection &con)
Issue the command to VPP/HW.
A cmd class that Dumps all the Vpp Interfaces.
tap_create_cmd(HW::item< handle_t > &item, const std::string &name, route::prefix_t &prefix, const l2_address_t &l2_address)
rc_t rc() const
Get the HW return code.
Definition: hw.hpp:118
rc_t issue(connection &con)
Issue the command to VPP/HW.
tapv2_create_cmd(HW::item< handle_t > &item, const std::string &name, route::prefix_t &prefix, const l2_address_t &l2_address)
std::string to_string() const
convert to string format for debug purposes
const std::string & m_name
The name of the interface to be created.
Definition: interface.hpp:343
A representation of the connection to VPP.
Definition: connection.hpp:33
rc_t issue(connection &con)
Issue the command to VPP/HW.
std::string to_string() const
convert to string format for debug purposes
Definition: prefix.cpp:183
bool operator==(const tapv2_dump_cmd &i) const
Comparison operator - only used for UT.
#define VAPI_CALL(_stmt)
Convenince wrapper macro for error handling in VAPI sends.
Definition: types.hpp:29
void to_vpp(uint8_t *is_ip6, uint8_t *addr, uint8_t *len) const
Convert the prefix into VPP API parameters.
Definition: prefix.cpp:251
A representation of an interface in VPP.
Definition: interface.hpp:41
DEFINE_VAPI_MSG_IDS_TAP_API_JSON
static const rc_t OK
The HW write was successfull.
Definition: types.hpp:104
DEFINE_VAPI_MSG_IDS_TAPV2_API_JSON
tap_delete_cmd(HW::item< handle_t > &item)
A cmd class that Dumps all the Vpp Interfaces.
vapi::Connection & ctx()
Retrun the VAPI context the commands will use.
Definition: connection.cpp:49
std::string to_string() const
convert to string format for debug purposes
void insert_interface()
add the created interface to the DB
Definition: interface.hpp:318
The VPP Object Model (VOM) library.
Definition: acl_binding.cpp:19
rc_t issue(connection &con)
Issue the command to VPP/HW.
void set(const rc_t &rc)
Set the HW return code - should only be called from the family of Command objects.
Definition: hw.hpp:124
rc_t issue(connection &con)
Issue the command to VPP/HW.
static const prefix_t ZERO
The all Zeros prefix.
Definition: prefix.hpp:165
typedef prefix
Definition: ip_types.api:40
std::string to_string() const
convert to string format for debug purposes
std::string to_string() const
convert to string format for debug purposes
const boost::asio::ip::address & address() const
Get the address.
Definition: prefix.cpp:149
A prefix defintion.
Definition: prefix.hpp:93
HW::item< handle_t > & m_hw_item
A reference to an object&#39;s HW::item that the command will update.
Definition: rpc_cmd.hpp:132
bool operator==(const tap_dump_cmd &i) const
Comparison operator - only used for UT.
HW::item< handle_t > & item()
return the HW item the command updates
Definition: rpc_cmd.hpp:64
vapi::Tap_connect msg_t
convenient typedef
Definition: rpc_cmd.hpp:44