Line data Source code
1 : /*
2 : *------------------------------------------------------------------
3 : * Copyright (c) 2016 Cisco and/or its affiliates.
4 : * Licensed under the Apache License, Version 2.0 (the "License");
5 : * you may not use this file except in compliance with the License.
6 : * You may obtain a copy of the License at:
7 : *
8 : * http://www.apache.org/licenses/LICENSE-2.0
9 : *
10 : * Unless required by applicable law or agreed to in writing, software
11 : * distributed under the License is distributed on an "AS IS" BASIS,
12 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : * See the License for the specific language governing permissions and
14 : * limitations under the License.
15 : *------------------------------------------------------------------
16 : */
17 : #include <stdint.h>
18 : #include <net/if.h>
19 : #include <sys/ioctl.h>
20 : #include <inttypes.h>
21 :
22 : #include <vlib/vlib.h>
23 : #include <vlib/unix/unix.h>
24 : #include <vnet/ethernet/ethernet.h>
25 : #include <vnet/ip/ip4_packet.h>
26 : #include <vnet/ip/ip6_packet.h>
27 : #include <vnet/ip/format.h>
28 : #include <vnet/devices/virtio/virtio.h>
29 : #include <vnet/devices/tap/tap.h>
30 :
31 : static clib_error_t *
32 0 : tap_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
33 : vlib_cli_command_t * cmd)
34 : {
35 0 : unformat_input_t _line_input, *line_input = &_line_input;
36 0 : tap_create_if_args_t args = { 0 };
37 0 : int ip_addr_set = 0;
38 : u32 tmp;
39 :
40 0 : args.id = ~0;
41 0 : args.tap_flags = 0;
42 0 : args.rv = -1;
43 0 : args.num_rx_queues = 1;
44 0 : args.num_tx_queues = 1;
45 :
46 : /* Get a line of input. */
47 0 : if (unformat_user (input, unformat_line_input, line_input))
48 : {
49 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
50 : {
51 0 : if (unformat (line_input, "id %u", &args.id))
52 : ;
53 : else
54 0 : if (unformat (line_input, "host-if-name %s", &args.host_if_name))
55 : ;
56 0 : else if (unformat (line_input, "host-ns %s", &args.host_namespace))
57 : ;
58 0 : else if (unformat (line_input, "host-mac-addr %U",
59 : unformat_ethernet_address,
60 : args.host_mac_addr.bytes))
61 : ;
62 0 : else if (unformat (line_input, "host-bridge %s", &args.host_bridge))
63 : ;
64 0 : else if (unformat (line_input, "host-ip4-addr %U/%d",
65 : unformat_ip4_address, &args.host_ip4_addr,
66 : &args.host_ip4_prefix_len))
67 0 : ip_addr_set = 1;
68 0 : else if (unformat (line_input, "host-ip4-gw %U",
69 : unformat_ip4_address, &args.host_ip4_gw))
70 0 : args.host_ip4_gw_set = 1;
71 0 : else if (unformat (line_input, "host-ip6-addr %U/%d",
72 : unformat_ip6_address, &args.host_ip6_addr,
73 : &args.host_ip6_prefix_len))
74 0 : ip_addr_set = 1;
75 0 : else if (unformat (line_input, "host-ip6-gw %U",
76 : unformat_ip6_address, &args.host_ip6_gw))
77 0 : args.host_ip6_gw_set = 1;
78 0 : else if (unformat (line_input, "num-rx-queues %d", &tmp))
79 0 : args.num_rx_queues = tmp;
80 0 : else if (unformat (line_input, "num-tx-queues %d", &tmp))
81 0 : args.num_tx_queues = tmp;
82 0 : else if (unformat (line_input, "rx-ring-size %d", &tmp))
83 0 : args.rx_ring_sz = tmp;
84 0 : else if (unformat (line_input, "tx-ring-size %d", &tmp))
85 0 : args.tx_ring_sz = tmp;
86 : else
87 0 : if (unformat
88 : (line_input, "host-mtu-size %d", &args.host_mtu_size))
89 0 : args.host_mtu_set = 1;
90 0 : else if (unformat (line_input, "no-gso"))
91 0 : args.tap_flags &= ~TAP_FLAG_GSO;
92 0 : else if (unformat (line_input, "gso"))
93 0 : args.tap_flags |= TAP_FLAG_GSO;
94 0 : else if (unformat (line_input, "gro-coalesce"))
95 0 : args.tap_flags |= TAP_FLAG_GRO_COALESCE;
96 0 : else if (unformat (line_input, "csum-offload"))
97 0 : args.tap_flags |= TAP_FLAG_CSUM_OFFLOAD;
98 0 : else if (unformat (line_input, "persist"))
99 0 : args.tap_flags |= TAP_FLAG_PERSIST;
100 0 : else if (unformat (line_input, "attach"))
101 0 : args.tap_flags |= TAP_FLAG_ATTACH;
102 0 : else if (unformat (line_input, "tun"))
103 0 : args.tap_flags |= TAP_FLAG_TUN;
104 0 : else if (unformat (line_input, "packed"))
105 0 : args.tap_flags |= TAP_FLAG_PACKED;
106 0 : else if (unformat (line_input, "in-order"))
107 0 : args.tap_flags |= TAP_FLAG_IN_ORDER;
108 0 : else if (unformat (line_input, "hw-addr %U",
109 : unformat_ethernet_address, args.mac_addr.bytes))
110 0 : args.mac_addr_set = 1;
111 : else
112 : {
113 0 : unformat_free (line_input);
114 0 : return clib_error_return (0, "unknown input `%U'",
115 : format_unformat_error, input);
116 : }
117 : }
118 0 : unformat_free (line_input);
119 : }
120 :
121 0 : if (ip_addr_set && args.host_bridge)
122 0 : return clib_error_return (0, "Please specify either host ip address or "
123 : "host bridge");
124 :
125 0 : tap_create_if (vm, &args);
126 :
127 0 : if (!args.rv)
128 0 : vlib_cli_output (vm, "%U\n", format_vnet_sw_if_index_name,
129 : vnet_get_main (), args.sw_if_index);
130 :
131 0 : vec_free (args.host_if_name);
132 0 : vec_free (args.host_namespace);
133 0 : vec_free (args.host_bridge);
134 :
135 0 : return args.error;
136 :
137 : }
138 :
139 : /* *INDENT-OFF* */
140 285289 : VLIB_CLI_COMMAND (tap_create_command, static) = {
141 : .path = "create tap",
142 : .short_help =
143 : "create tap {id <if-id>} [hw-addr <mac-address>] "
144 : "[num-rx-queues <n>] [num-tx-queues <n>] [rx-ring-size <size>] "
145 : "[tx-ring-size <size>] [host-ns <netns>] [host-bridge <bridge-name>] "
146 : "[host-ip4-addr <ip4addr/mask>] [host-ip6-addr <ip6-addr>] "
147 : "[host-ip4-gw <ip4-addr>] [host-ip6-gw <ip6-addr>] "
148 : "[host-mac-addr <host-mac-address>] [host-if-name <name>] "
149 : "[host-mtu-size <size>] [no-gso|gso [gro-coalesce]|csum-offload] "
150 : "[persist] [attach] [tun] [packed] [in-order]",
151 : .function = tap_create_command_fn,
152 : };
153 : /* *INDENT-ON* */
154 :
155 : static clib_error_t *
156 0 : tap_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
157 : vlib_cli_command_t * cmd)
158 : {
159 0 : unformat_input_t _line_input, *line_input = &_line_input;
160 0 : u32 sw_if_index = ~0;
161 0 : vnet_main_t *vnm = vnet_get_main ();
162 : int rv;
163 :
164 : /* Get a line of input. */
165 0 : if (!unformat_user (input, unformat_line_input, line_input))
166 0 : return clib_error_return (0, "Missing <interface>");
167 :
168 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
169 : {
170 0 : if (unformat (line_input, "sw_if_index %d", &sw_if_index))
171 : ;
172 0 : else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
173 : vnm, &sw_if_index))
174 : ;
175 : else
176 0 : return clib_error_return (0, "unknown input `%U'",
177 : format_unformat_error, input);
178 : }
179 0 : unformat_free (line_input);
180 :
181 0 : if (sw_if_index == ~0)
182 0 : return clib_error_return (0,
183 : "please specify interface name or sw_if_index");
184 :
185 0 : rv = tap_delete_if (vm, sw_if_index);
186 0 : if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
187 0 : return clib_error_return (0, "not a tap interface");
188 0 : else if (rv != 0)
189 0 : return clib_error_return (0, "error on deleting tap interface");
190 :
191 0 : return 0;
192 : }
193 :
194 : /* *INDENT-OFF* */
195 285289 : VLIB_CLI_COMMAND (tap_delete__command, static) =
196 : {
197 : .path = "delete tap",
198 : .short_help = "delete tap {<interface> | sw_if_index <sw_idx>}",
199 : .function = tap_delete_command_fn,
200 : };
201 : /* *INDENT-ON* */
202 :
203 : static clib_error_t *
204 0 : tap_offload_command_fn (vlib_main_t * vm, unformat_input_t * input,
205 : vlib_cli_command_t * cmd)
206 : {
207 0 : unformat_input_t _line_input, *line_input = &_line_input;
208 0 : u32 sw_if_index = ~0;
209 0 : vnet_main_t *vnm = vnet_get_main ();
210 0 : int gso_enable = 0, gso_disable = 0, is_gro_coalesce = 0;
211 0 : int csum_offload_enable = 0, csum_offload_disable = 0;
212 0 : int rv = 0;
213 :
214 : /* Get a line of input. */
215 0 : if (!unformat_user (input, unformat_line_input, line_input))
216 0 : return clib_error_return (0, "Missing <interface>");
217 :
218 0 : while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
219 : {
220 0 : if (unformat (line_input, "sw_if_index %d", &sw_if_index))
221 : ;
222 0 : else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
223 : vnm, &sw_if_index))
224 : ;
225 0 : else if (unformat (line_input, "gso-enable"))
226 : {
227 0 : gso_enable = 1;
228 0 : if (unformat (line_input, "gro-coalesce"))
229 0 : is_gro_coalesce = 1;
230 : }
231 0 : else if (unformat (line_input, "gso-disable"))
232 0 : gso_disable = 1;
233 0 : else if (unformat (line_input, "csum-offload-enable"))
234 0 : csum_offload_enable = 1;
235 0 : else if (unformat (line_input, "csum-offload-disable"))
236 0 : csum_offload_disable = 1;
237 : else
238 0 : return clib_error_return (0, "unknown input `%U'",
239 : format_unformat_error, input);
240 : }
241 0 : unformat_free (line_input);
242 :
243 0 : if (sw_if_index == ~0)
244 0 : return clib_error_return (0,
245 : "please specify interface name or sw_if_index");
246 :
247 0 : if (gso_enable)
248 0 : rv = tap_gso_enable_disable (vm, sw_if_index, 1, is_gro_coalesce);
249 0 : else if (csum_offload_enable)
250 0 : rv = tap_csum_offload_enable_disable (vm, sw_if_index, 1);
251 0 : else if (gso_disable)
252 0 : rv = tap_gso_enable_disable (vm, sw_if_index, 0, 0);
253 0 : else if (csum_offload_disable)
254 0 : rv = tap_csum_offload_enable_disable (vm, sw_if_index, 0);
255 :
256 0 : if (rv == VNET_API_ERROR_INVALID_SW_IF_INDEX)
257 0 : return clib_error_return (0, "not a tap interface");
258 0 : else if (rv != 0)
259 0 : return clib_error_return (0, "error on configuring GSO on tap interface");
260 :
261 0 : return 0;
262 : }
263 :
264 : /* *INDENT-OFF* */
265 285289 : VLIB_CLI_COMMAND (tap_offload_command, static) =
266 : {
267 : .path = "set tap offload",
268 : .short_help = "set tap offload {<interface> | sw_if_index <sw_idx>}"
269 : " <gso-enable [gro-coalesce] | gso-disable | csum-offload-enable |"
270 : "csum-offload-disable>",
271 : .function = tap_offload_command_fn,
272 : };
273 : /* *INDENT-ON* */
274 :
275 : static clib_error_t *
276 0 : tap_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
277 : vlib_cli_command_t * cmd)
278 : {
279 0 : virtio_main_t *mm = &virtio_main;
280 : virtio_if_t *vif;
281 0 : vnet_main_t *vnm = vnet_get_main ();
282 0 : int show_descr = 0;
283 0 : clib_error_t *error = 0;
284 0 : u32 hw_if_index, *hw_if_indices = 0;
285 :
286 0 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
287 : {
288 0 : if (unformat
289 : (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
290 0 : vec_add1 (hw_if_indices, hw_if_index);
291 0 : else if (unformat (input, "descriptors"))
292 0 : show_descr = 1;
293 : else
294 : {
295 0 : error = clib_error_return (0, "unknown input `%U'",
296 : format_unformat_error, input);
297 0 : goto done;
298 : }
299 : }
300 :
301 0 : if (vec_len (hw_if_indices) == 0)
302 : {
303 : /* *INDENT-OFF* */
304 0 : pool_foreach (vif, mm->interfaces)
305 0 : vec_add1 (hw_if_indices, vif->hw_if_index);
306 : /* *INDENT-ON* */
307 : }
308 :
309 0 : virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_TAP);
310 :
311 0 : done:
312 0 : vec_free (hw_if_indices);
313 0 : return error;
314 : }
315 :
316 : /* *INDENT-OFF* */
317 285289 : VLIB_CLI_COMMAND (tap_show_command, static) = {
318 : .path = "show tap",
319 : .short_help = "show tap {<interface>] [descriptors]",
320 : .function = tap_show_command_fn,
321 : };
322 : /* *INDENT-ON* */
323 :
324 : static clib_error_t *
325 0 : tun_show_command_fn (vlib_main_t * vm, unformat_input_t * input,
326 : vlib_cli_command_t * cmd)
327 : {
328 0 : virtio_main_t *mm = &virtio_main;
329 : virtio_if_t *vif;
330 0 : vnet_main_t *vnm = vnet_get_main ();
331 0 : int show_descr = 0;
332 0 : clib_error_t *error = 0;
333 0 : u32 hw_if_index, *hw_if_indices = 0;
334 :
335 0 : while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
336 : {
337 0 : if (unformat
338 : (input, "%U", unformat_vnet_hw_interface, vnm, &hw_if_index))
339 0 : vec_add1 (hw_if_indices, hw_if_index);
340 0 : else if (unformat (input, "descriptors"))
341 0 : show_descr = 1;
342 : else
343 : {
344 0 : error = clib_error_return (0, "unknown input `%U'",
345 : format_unformat_error, input);
346 0 : goto done;
347 : }
348 : }
349 :
350 0 : if (vec_len (hw_if_indices) == 0)
351 : {
352 : /* *INDENT-OFF* */
353 0 : pool_foreach (vif, mm->interfaces)
354 0 : vec_add1 (hw_if_indices, vif->hw_if_index);
355 : /* *INDENT-ON* */
356 : }
357 :
358 0 : virtio_show (vm, hw_if_indices, show_descr, VIRTIO_IF_TYPE_TUN);
359 :
360 0 : done:
361 0 : vec_free (hw_if_indices);
362 0 : return error;
363 : }
364 :
365 : /* *INDENT-OFF* */
366 285289 : VLIB_CLI_COMMAND (tun_show_command, static) = {
367 : .path = "show tun",
368 : .short_help = "show tun {<interface>] [descriptors]",
369 : .function = tun_show_command_fn,
370 : };
371 : /* *INDENT-ON* */
372 :
373 : clib_error_t *
374 575 : tap_cli_init (vlib_main_t * vm)
375 : {
376 575 : return 0;
377 : }
378 :
379 76607 : VLIB_INIT_FUNCTION (tap_cli_init);
380 :
381 : /*
382 : * fd.io coding-style-patch-verification: ON
383 : *
384 : * Local Variables:
385 : * eval: (c-set-style "gnu")
386 : * End:
387 : */
|