rdma: add option to specify inteface name
[vpp.git] / src / plugins / rdma / cli.c
1 /*
2  *------------------------------------------------------------------
3  * Copyright (c) 2018 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 <vlib/pci/pci.h>
25 #include <vnet/ethernet/ethernet.h>
26
27 #include <rdma/rdma.h>
28
29 static clib_error_t *
30 rdma_create_command_fn (vlib_main_t * vm, unformat_input_t * input,
31                         vlib_cli_command_t * cmd)
32 {
33   unformat_input_t _line_input, *line_input = &_line_input;
34   rdma_create_if_args_t args;
35
36   clib_memset (&args, 0, sizeof (rdma_create_if_args_t));
37
38   /* Get a line of input. */
39   if (!unformat_user (input, unformat_line_input, line_input))
40     return 0;
41
42   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
43     {
44       if (unformat (line_input, "host-if %s", &args.ifname))
45         ;
46       else if (unformat (line_input, "name %s", &args.name))
47         ;
48       else
49         return clib_error_return (0, "unknown input `%U'",
50                                   format_unformat_error, input);
51     }
52   unformat_free (line_input);
53
54   rdma_create_if (vm, &args);
55
56   vec_free (args.ifname);
57   vec_free (args.name);
58
59   return args.error;
60 }
61
62 /* *INDENT-OFF* */
63 VLIB_CLI_COMMAND (rdma_create_command, static) = {
64   .path = "create interface rdma",
65   .short_help = "create interface rdma <host-if ifname> [name <name>]",
66   .function = rdma_create_command_fn,
67 };
68 /* *INDENT-ON* */
69
70 static clib_error_t *
71 rdma_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
72                         vlib_cli_command_t * cmd)
73 {
74   unformat_input_t _line_input, *line_input = &_line_input;
75   u32 sw_if_index = ~0;
76   vnet_hw_interface_t *hw;
77   rdma_main_t *rm = &rdma_main;
78   rdma_device_t *rd;
79   vnet_main_t *vnm = vnet_get_main ();
80
81   /* Get a line of input. */
82   if (!unformat_user (input, unformat_line_input, line_input))
83     return 0;
84
85   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
86     {
87       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
88         ;
89       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
90                          vnm, &sw_if_index))
91         ;
92       else
93         return clib_error_return (0, "unknown input `%U'",
94                                   format_unformat_error, input);
95     }
96   unformat_free (line_input);
97
98   if (sw_if_index == ~0)
99     return clib_error_return (0,
100                               "please specify interface name or sw_if_index");
101
102   hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
103   if (hw == NULL || rdma_device_class.index != hw->dev_class_index)
104     return clib_error_return (0, "not an AVF interface");
105
106   rd = pool_elt_at_index (rm->devices, hw->dev_instance);
107
108   rdma_delete_if (vm, rd);
109
110   return 0;
111 }
112
113 /* *INDENT-OFF* */
114 VLIB_CLI_COMMAND (rdma_delete_command, static) = {
115   .path = "delete interface rdma",
116   .short_help = "delete interface rdma "
117     "{<interface> | sw_if_index <sw_idx>}",
118   .function = rdma_delete_command_fn,
119 };
120 /* *INDENT-ON* */
121
122 clib_error_t *
123 rdma_cli_init (vlib_main_t * vm)
124 {
125   return 0;
126 }
127
128 VLIB_INIT_FUNCTION (rdma_cli_init);
129
130 /*
131  * fd.io coding-style-patch-verification: ON
132  *
133  * Local Variables:
134  * eval: (c-set-style "gnu")
135  * End:
136  */