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