32c19f46590a77573238b7c43cb878bfcecba6d8
[vpp.git] / src / plugins / avf / 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 <avf/avf.h>
28
29 static clib_error_t *
30 avf_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   avf_create_if_args_t args;
35   u32 tmp;
36
37   clib_memset (&args, 0, sizeof (avf_create_if_args_t));
38
39   /* Get a line of input. */
40   if (!unformat_user (input, unformat_line_input, line_input))
41     return 0;
42
43   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
44     {
45       if (unformat (line_input, "%U", unformat_vlib_pci_addr, &args.addr))
46         ;
47       else if (unformat (line_input, "elog"))
48         args.enable_elog = 1;
49       else if (unformat (line_input, "rx-queue-size %u", &tmp))
50         args.rxq_size = tmp;
51       else if (unformat (line_input, "tx-queue-size %u", &tmp))
52         args.txq_size = tmp;
53       else if (unformat (line_input, "num-rx-queues %u", &tmp))
54         args.rxq_num = tmp;
55       else if (unformat (line_input, "name %s", &args.name))
56         ;
57       else
58         return clib_error_return (0, "unknown input `%U'",
59                                   format_unformat_error, input);
60     }
61   unformat_free (line_input);
62
63   avf_create_if (vm, &args);
64
65   vec_free (args.name);
66
67   return args.error;
68 }
69
70 /* *INDENT-OFF* */
71 VLIB_CLI_COMMAND (avf_create_command, static) = {
72   .path = "create interface avf",
73   .short_help = "create interface avf <pci-address> "
74                 "[rx-queue-size <size>] [tx-queue-size <size>] "
75                 "[num-rx-queues <size>]",
76   .function = avf_create_command_fn,
77 };
78 /* *INDENT-ON* */
79
80 static clib_error_t *
81 avf_delete_command_fn (vlib_main_t * vm, unformat_input_t * input,
82                        vlib_cli_command_t * cmd)
83 {
84   unformat_input_t _line_input, *line_input = &_line_input;
85   u32 sw_if_index = ~0;
86   vnet_hw_interface_t *hw;
87   vnet_main_t *vnm = vnet_get_main ();
88
89   /* Get a line of input. */
90   if (!unformat_user (input, unformat_line_input, line_input))
91     return 0;
92
93   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
94     {
95       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
96         ;
97       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
98                          vnm, &sw_if_index))
99         ;
100       else
101         return clib_error_return (0, "unknown input `%U'",
102                                   format_unformat_error, input);
103     }
104   unformat_free (line_input);
105
106   if (sw_if_index == ~0)
107     return clib_error_return (0,
108                               "please specify interface name or sw_if_index");
109
110   hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
111   if (hw == NULL || avf_device_class.index != hw->dev_class_index)
112     return clib_error_return (0, "not an AVF interface");
113
114   vlib_process_signal_event (vm, avf_process_node.index,
115                              AVF_PROCESS_EVENT_DELETE_IF, hw->dev_instance);
116
117   return 0;
118 }
119
120 /* *INDENT-OFF* */
121 VLIB_CLI_COMMAND (avf_delete_command, static) = {
122   .path = "delete interface avf",
123   .short_help = "delete interface avf "
124     "{<interface> | sw_if_index <sw_idx>}",
125   .function = avf_delete_command_fn,
126   .is_mp_safe = 1,
127 };
128 /* *INDENT-ON* */
129
130 static clib_error_t *
131 avf_test_command_fn (vlib_main_t * vm, unformat_input_t * input,
132                      vlib_cli_command_t * cmd)
133 {
134   unformat_input_t _line_input, *line_input = &_line_input;
135   u32 sw_if_index = ~0;
136   vnet_hw_interface_t *hw;
137   avf_device_t *ad;
138   vnet_main_t *vnm = vnet_get_main ();
139   int test_irq = 0, enable_elog = 0, disable_elog = 0;
140
141   /* Get a line of input. */
142   if (!unformat_user (input, unformat_line_input, line_input))
143     return 0;
144
145   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
146     {
147       if (unformat (line_input, "sw_if_index %d", &sw_if_index))
148         ;
149       else if (unformat (line_input, "irq"))
150         test_irq = 1;
151       else if (unformat (line_input, "elog-on"))
152         enable_elog = 1;
153       else if (unformat (line_input, "elog-off"))
154         disable_elog = 1;
155       else if (unformat (line_input, "%U", unformat_vnet_sw_interface,
156                          vnm, &sw_if_index))
157         ;
158       else
159         return clib_error_return (0, "unknown input `%U'",
160                                   format_unformat_error, input);
161     }
162   unformat_free (line_input);
163
164   if (sw_if_index == ~0)
165     return clib_error_return (0,
166                               "please specify interface name or sw_if_index");
167
168   hw = vnet_get_sup_hw_interface_api_visible_or_null (vnm, sw_if_index);
169   if (hw == NULL || avf_device_class.index != hw->dev_class_index)
170     return clib_error_return (0, "not a AVF interface");
171
172   ad = avf_get_device (hw->dev_instance);
173
174   if (enable_elog)
175     ad->flags |= AVF_DEVICE_F_ELOG;
176
177   if (disable_elog)
178     ad->flags &= ~AVF_DEVICE_F_ELOG;
179
180   if (test_irq)
181     avf_reg_write (ad, AVFINT_DYN_CTL0, (1 << 0) | (3 << 3) | (1 << 2));
182
183   return 0;
184 }
185
186 /* *INDENT-OFF* */
187 VLIB_CLI_COMMAND (avf_test_command, static) = {
188   .path = "test avf",
189   .short_help = "test avf [<interface> | sw_if_index <sw_idx>] [irq] "
190     "[elog-on] [elog-off]",
191   .function = avf_test_command_fn,
192 };
193 /* *INDENT-ON* */
194
195 clib_error_t *
196 avf_cli_init (vlib_main_t * vm)
197 {
198   return 0;
199 }
200
201 VLIB_INIT_FUNCTION (avf_cli_init);
202
203 /*
204  * fd.io coding-style-patch-verification: ON
205  *
206  * Local Variables:
207  * eval: (c-set-style "gnu")
208  * End:
209  */