fbb125cee9de52d320641dd66a334ed4ed377d07
[vpp.git] / src / plugins / avf / avf_test.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
18 #include <vlib/vlib.h>
19 #include <vlib/unix/unix.h>
20 #include <vlib/pci/pci.h>
21 #include <vnet/ethernet/ethernet.h>
22
23 #include <vat/vat.h>
24 #include <vlibapi/api.h>
25 #include <vlibmemory/api.h>
26
27 #include <vppinfra/error.h>
28 #include <avf/avf.h>
29
30 #define __plugin_msg_base avf_test_main.msg_id_base
31 #include <vlibapi/vat_helper_macros.h>
32
33 /* declare message IDs */
34 #include <avf/avf_msg_enum.h>
35
36 /* define message structures */
37 #define vl_typedefs
38 #include <avf/avf_all_api_h.h>
39 #undef vl_typedefs
40
41 /* declare message handlers for each api */
42 #define vl_endianfun
43 #include <avf/avf_all_api_h.h>
44 #undef vl_endianfun
45
46 /* instantiate all the print functions we know about */
47 #define vl_print(handle, ...)
48 #define vl_printfun
49 #include <avf/avf_all_api_h.h>
50 #undef vl_printfun
51
52 /* get API version number */
53 #define vl_api_version(n,v) static u32 api_version=(v);
54 #include <avf/avf_all_api_h.h>
55 #undef vp_api_version
56
57 typedef struct
58 {
59   /* API message ID base */
60   u16 msg_id_base;
61   vat_main_t *vat_main;
62 } avf_test_main_t;
63
64 avf_test_main_t avf_test_main;
65
66 #define foreach_standard_reply_retval_handler           \
67 _(avf_delete_reply)
68
69 #define _(n)                                            \
70     static void vl_api_##n##_t_handler                  \
71     (vl_api_##n##_t * mp)                               \
72     {                                                   \
73         vat_main_t * vam = avf_test_main.vat_main;      \
74         i32 retval = ntohl(mp->retval);                 \
75         if (vam->async_mode) {                          \
76             vam->async_errors += (retval < 0);          \
77         } else {                                        \
78             vam->retval = retval;                       \
79             vam->result_ready = 1;                      \
80         }                                               \
81     }
82 foreach_standard_reply_retval_handler;
83 #undef _
84
85 #define foreach_vpe_api_reply_msg                       \
86 _(AVF_CREATE_REPLY, avf_create_reply)                   \
87 _(AVF_DELETE_REPLY, avf_delete_reply)
88
89 /* avf create API */
90 static int
91 api_avf_create (vat_main_t * vam)
92 {
93   unformat_input_t *i = vam->input;
94   vl_api_avf_create_t *mp;
95   avf_create_if_args_t args;
96   uint32_t tmp;
97   int ret;
98   u32 x[4];
99
100   memset (&args, 0, sizeof (avf_create_if_args_t));
101
102   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
103     {
104       if (unformat (i, "%x:%x:%x.%x", &x[0], &x[1], &x[2], &x[3]))
105         {
106           args.addr.domain = x[0];
107           args.addr.bus = x[1];
108           args.addr.slot = x[2];
109           args.addr.function = x[3];
110         }
111       else if (unformat (i, "elog"))
112         args.enable_elog = 1;
113       else if (unformat (i, "rx-queue-size %u", &tmp))
114         args.rxq_size = tmp;
115       else if (unformat (i, "tx-queue-size %u", &tmp))
116         args.txq_size = tmp;
117       else if (unformat (i, "num-rx-queues %u", &tmp))
118         args.rxq_num = tmp;
119       else
120         {
121           clib_warning ("unknown input '%U'", format_unformat_error, i);
122           return -99;
123         }
124     }
125
126   M (AVF_CREATE, mp);
127
128   mp->pci_addr = clib_host_to_net_u32 (args.addr.as_u32);
129   mp->enable_elog = clib_host_to_net_u16 (args.enable_elog);
130   mp->rxq_num = clib_host_to_net_u16 (args.rxq_num);
131   mp->rxq_size = clib_host_to_net_u16 (args.rxq_size);
132   mp->txq_size = clib_host_to_net_u16 (args.txq_size);
133
134   S (mp);
135   W (ret);
136
137   return ret;
138 }
139
140 /* avf-create reply handler */
141 static void
142 vl_api_avf_create_reply_t_handler (vl_api_avf_create_reply_t * mp)
143 {
144   vat_main_t *vam = avf_test_main.vat_main;
145   i32 retval = ntohl (mp->retval);
146
147   if (retval == 0)
148     {
149       fformat (vam->ofp, "created avf with sw_if_index %d\n",
150                ntohl (mp->sw_if_index));
151     }
152
153   vam->retval = retval;
154   vam->result_ready = 1;
155   vam->regenerate_interface_table = 1;
156 }
157
158 /* avf delete API */
159 static int
160 api_avf_delete (vat_main_t * vam)
161 {
162   unformat_input_t *i = vam->input;
163   vl_api_avf_delete_t *mp;
164   u32 sw_if_index = 0;
165   u8 index_defined = 0;
166   int ret;
167
168   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
169     {
170       if (unformat (i, "sw_if_index %u", &sw_if_index))
171         index_defined = 1;
172       else
173         {
174           clib_warning ("unknown input '%U'", format_unformat_error, i);
175           return -99;
176         }
177     }
178
179   if (!index_defined)
180     {
181       errmsg ("missing sw_if_index\n");
182       return -99;
183     }
184
185   M (AVF_DELETE, mp);
186
187   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
188
189   S (mp);
190   W (ret);
191
192   return ret;
193 }
194
195 /*
196  * List of messages that the api test plugin sends,
197  * and that the data plane plugin processes
198  */
199 #define foreach_vpe_api_msg                                     \
200 _(avf_create, "<pci-address> [rx-queue-size <size>] "           \
201               "[tx-queue-size <size>] [num-rx-queues <size>]")  \
202 _(avf_delete, "<sw_if_index>")
203
204 static void
205 avf_vat_api_hookup (vat_main_t * vam)
206 {
207   avf_test_main_t *avm __attribute__ ((unused)) = &avf_test_main;
208 #define _(N,n)                                                  \
209   vl_msg_api_set_handlers((VL_API_##N + avm->msg_id_base),       \
210                           #n,                                   \
211                           vl_api_##n##_t_handler,               \
212                           vl_noop_handler,                      \
213                           vl_api_##n##_t_endian,                \
214                           vl_api_##n##_t_print,                 \
215                           sizeof(vl_api_##n##_t), 1);
216   foreach_vpe_api_reply_msg;
217 #undef _
218
219 #define _(n,h)                                                  \
220   hash_set_mem (vam->function_by_name, #n, api_##n);
221   foreach_vpe_api_msg;
222 #undef _
223
224 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
225   foreach_vpe_api_msg;
226 #undef _
227 }
228
229 clib_error_t *
230 vat_plugin_register (vat_main_t * vam)
231 {
232   avf_test_main_t *avm = &avf_test_main;
233   u8 *name;
234
235   avm->vat_main = vam;
236
237   name = format (0, "avf_%08x%c", api_version, 0);
238   avm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
239
240   if (avm->msg_id_base != (u16) ~ 0)
241     avf_vat_api_hookup (vam);
242
243   vec_free (name);
244
245   return 0;
246 }
247
248 /*
249  * fd.io coding-style-patch-verification: ON
250  *
251  * Local Variables:
252  * eval: (c-set-style "gnu")
253  * End:
254  */