VPP-81: Print interface name after creating an interface with CLI
[vpp.git] / vnet / vnet / devices / netmap / 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
21 #include <vlib/vlib.h>
22 #include <vlib/unix/unix.h>
23 #include <vnet/ethernet/ethernet.h>
24
25 #include <vnet/devices/netmap/net_netmap.h>
26 #include <vnet/devices/netmap/netmap.h>
27
28 static clib_error_t *
29 netmap_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
30                              vlib_cli_command_t * cmd)
31 {
32   unformat_input_t _line_input, * line_input = &_line_input;
33   u8 * host_if_name = NULL;
34   u8 hwaddr [6];
35   u8 * hw_addr_ptr = 0;
36   int r;
37   u8 is_pipe = 0;
38   u8 is_master = 0;
39   u32 sw_if_index = ~0;
40
41   /* Get a line of input. */
42   if (! unformat_user (input, unformat_line_input, line_input))
43     return 0;
44
45   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
46     {
47       if (unformat (line_input, "name %s", &host_if_name))
48         ;
49       else if (unformat (line_input, "hw-addr %U", unformat_ethernet_address, hwaddr))
50         hw_addr_ptr = hwaddr;
51       else if (unformat (line_input, "pipe"))
52         is_pipe = 1;
53       else if (unformat (line_input, "master"))
54         is_master = 1;
55       else if (unformat (line_input, "slave"))
56         is_master = 0;
57       else
58         return clib_error_return (0, "unknown input `%U'", format_unformat_error, input);
59     }
60   unformat_free (line_input);
61
62   if (host_if_name == NULL)
63       return clib_error_return (0, "missing host interface name");
64
65   r = netmap_create_if(vm, host_if_name, hw_addr_ptr, is_pipe, is_master, &sw_if_index);
66
67   if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
68     return clib_error_return(0, "%s (errno %d)", strerror (errno), errno);
69
70   if (r == VNET_API_ERROR_INVALID_INTERFACE)
71     return clib_error_return(0, "Invalid interface name");
72
73   if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
74     return clib_error_return(0, "Interface already exists");
75
76   vlib_cli_output(vm, "%U\n", format_vnet_sw_if_index_name, vnet_get_main(), sw_if_index);
77   return 0;
78 }
79
80 VLIB_CLI_COMMAND (netmap_create_command, static) = {
81   .path = "create netmap",
82   .short_help = "create netmap name [<intf name>|valeXXX:YYY] "
83     "[hw-addr <mac>] [pipe] [master|slave]",
84   .function = netmap_create_command_fn,
85 };
86
87 static clib_error_t *
88 netmap_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
89                              vlib_cli_command_t * cmd)
90 {
91   unformat_input_t _line_input, * line_input = &_line_input;
92   u8 * host_if_name = NULL;
93
94   /* Get a line of input. */
95   if (! unformat_user (input, unformat_line_input, line_input))
96     return 0;
97
98   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
99     {
100       if (unformat (line_input, "name %s", &host_if_name))
101         ;
102       else
103         return clib_error_return (0, "unknown input `%U'", format_unformat_error, input);
104     }
105   unformat_free (line_input);
106
107   if (host_if_name == NULL)
108       return clib_error_return (0, "missing host interface name");
109
110   netmap_delete_if(vm, host_if_name);
111
112   return 0;
113 }
114
115 VLIB_CLI_COMMAND (netmap_delete_command, static) = {
116   .path = "delete netmap",
117   .short_help = "delete netmap name <interface name>",
118   .function = netmap_delete_command_fn,
119 };
120
121 clib_error_t *
122 netmap_cli_init (vlib_main_t * vm)
123 {
124   return 0;
125 }
126
127 VLIB_INIT_FUNCTION (netmap_cli_init);