avoid using thread local storage for thread index
[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_create_reply)                                     \
68 _(avf_delete_reply)
69
70 #define _(n)                                            \
71     static void vl_api_##n##_t_handler                  \
72     (vl_api_##n##_t * mp)                               \
73     {                                                   \
74         vat_main_t * vam = avf_test_main.vat_main;      \
75         i32 retval = ntohl(mp->retval);                 \
76         if (vam->async_mode) {                          \
77             vam->async_errors += (retval < 0);          \
78         } else {                                        \
79             vam->retval = retval;                       \
80             vam->result_ready = 1;                      \
81         }                                               \
82     }
83 foreach_standard_reply_retval_handler;
84 #undef _
85
86 #define foreach_vpe_api_reply_msg                       \
87 _(AVF_CREATE_REPLY, avf_create_reply)                   \
88 _(AVF_DELETE_REPLY, avf_delete_reply)
89
90 /* avf create API */
91 static int
92 api_avf_create (vat_main_t * vam)
93 {
94   unformat_input_t *i = vam->input;
95   vl_api_avf_create_t *mp;
96   avf_create_if_args_t args;
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", &args.rxq_size))
114         ;
115       else if (unformat (i, "tx-queue-size %u", &args.txq_size))
116         ;
117       else
118         {
119           clib_warning ("unknown input '%U'", format_unformat_error, i);
120           return -99;
121         }
122     }
123
124   M (AVF_CREATE, mp);
125
126   mp->pci_addr = clib_host_to_net_u32 (args.addr.as_u32);
127   mp->enable_elog = clib_host_to_net_u16 (args.enable_elog);
128   mp->rxq_size = clib_host_to_net_u16 (args.rxq_size);
129   mp->txq_size = clib_host_to_net_u16 (args.txq_size);
130
131   S (mp);
132   W (ret);
133
134   return ret;
135 }
136
137 /* avf delete API */
138 static int
139 api_avf_delete (vat_main_t * vam)
140 {
141   unformat_input_t *i = vam->input;
142   vl_api_avf_delete_t *mp;
143   u32 sw_if_index = 0;
144   u8 index_defined = 0;
145   int ret;
146
147   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
148     {
149       if (unformat (i, "sw_if_index %u", &sw_if_index))
150         index_defined = 1;
151       else
152         {
153           clib_warning ("unknown input '%U'", format_unformat_error, i);
154           return -99;
155         }
156     }
157
158   if (!index_defined)
159     {
160       errmsg ("missing sw_if_index\n");
161       return -99;
162     }
163
164   M (AVF_DELETE, mp);
165
166   mp->sw_if_index = clib_host_to_net_u32 (sw_if_index);
167
168   S (mp);
169   W (ret);
170
171   return ret;
172 }
173
174 /*
175  * List of messages that the api test plugin sends,
176  * and that the data plane plugin processes
177  */
178 #define foreach_vpe_api_msg                                     \
179 _(avf_create, "<pci-address> [rx-queue-size <size>] "           \
180               "[tx-queue-size <size>]")                         \
181 _(avf_delete, "<sw_if_index>")
182
183 static void
184 avf_vat_api_hookup (vat_main_t * vam)
185 {
186   avf_test_main_t *avm __attribute__ ((unused)) = &avf_test_main;
187 #define _(N,n)                                                  \
188   vl_msg_api_set_handlers((VL_API_##N + avm->msg_id_base),       \
189                           #n,                                   \
190                           vl_api_##n##_t_handler,               \
191                           vl_noop_handler,                      \
192                           vl_api_##n##_t_endian,                \
193                           vl_api_##n##_t_print,                 \
194                           sizeof(vl_api_##n##_t), 1);
195   foreach_vpe_api_reply_msg;
196 #undef _
197
198 #define _(n,h)                                                  \
199   hash_set_mem (vam->function_by_name, #n, api_##n);
200   foreach_vpe_api_msg;
201 #undef _
202
203 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
204   foreach_vpe_api_msg;
205 #undef _
206 }
207
208 clib_error_t *
209 vat_plugin_register (vat_main_t * vam)
210 {
211   avf_test_main_t *avm = &avf_test_main;
212   u8 *name;
213
214   avm->vat_main = vam;
215
216   name = format (0, "avf_%08x%c", api_version, 0);
217   avm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
218
219   if (avm->msg_id_base != (u16) ~ 0)
220     avf_vat_api_hookup (vam);
221
222   vec_free (name);
223
224   return 0;
225 }
226
227 /*
228  * fd.io coding-style-patch-verification: ON
229  *
230  * Local Variables:
231  * eval: (c-set-style "gnu")
232  * End:
233  */