584d68002d36267e2ee44d324aebd7232c5eaa2a
[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
40   /* Get a line of input. */
41   if (! unformat_user (input, unformat_line_input, line_input))
42     return 0;
43
44   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
45     {
46       if (unformat (line_input, "name %s", &host_if_name))
47         ;
48       else if (unformat (line_input, "hw-addr %U", unformat_ethernet_address, hwaddr))
49         hw_addr_ptr = hwaddr;
50       else if (unformat (line_input, "pipe"))
51         is_pipe = 1;
52       else if (unformat (line_input, "master"))
53         is_master = 1;
54       else if (unformat (line_input, "slave"))
55         is_master = 0;
56       else
57         return clib_error_return (0, "unknown input `%U'", format_unformat_error, input);
58     }
59   unformat_free (line_input);
60
61   if (host_if_name == NULL)
62       return clib_error_return (0, "missing host interface name");
63
64   r = netmap_create_if(vm, host_if_name, hw_addr_ptr, is_pipe, is_master);
65
66   if (r == VNET_API_ERROR_SYSCALL_ERROR_1)
67     return clib_error_return(0, "%s (errno %d)", strerror (errno), errno);
68
69   if (r == VNET_API_ERROR_INVALID_INTERFACE)
70     return clib_error_return(0, "Invalid interface name");
71
72   if (r == VNET_API_ERROR_SUBIF_ALREADY_EXISTS)
73     return clib_error_return(0, "Interface already exists");
74
75   return 0;
76 }
77
78 VLIB_CLI_COMMAND (netmap_create_command, static) = {
79   .path = "create netmap",
80   .short_help = "create netmap name [<intf name>|valeXXX:YYY] "
81     "[hw-addr <mac>] [pipe] [master|slave]",
82   .function = netmap_create_command_fn,
83 };
84
85 static clib_error_t *
86 netmap_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
87                              vlib_cli_command_t * cmd)
88 {
89   unformat_input_t _line_input, * line_input = &_line_input;
90   u8 * host_if_name = NULL;
91
92   /* Get a line of input. */
93   if (! unformat_user (input, unformat_line_input, line_input))
94     return 0;
95
96   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
97     {
98       if (unformat (line_input, "name %s", &host_if_name))
99         ;
100       else
101         return clib_error_return (0, "unknown input `%U'", format_unformat_error, input);
102     }
103   unformat_free (line_input);
104
105   if (host_if_name == NULL)
106       return clib_error_return (0, "missing host interface name");
107
108   netmap_delete_if(vm, host_if_name);
109
110   return 0;
111 }
112
113 VLIB_CLI_COMMAND (netmap_delete_command, static) = {
114   .path = "delete netmap",
115   .short_help = "delete netmap name <interface name>",
116   .function = netmap_delete_command_fn,
117 };
118
119 clib_error_t *
120 netmap_cli_init (vlib_main_t * vm)
121 {
122   return 0;
123 }
124
125 VLIB_INIT_FUNCTION (netmap_cli_init);