8204edfe66acc9ce1344b6e7a3c7cb6265584159
[vpp.git] / src / plugins / dpdk / api / dpdk_test.c
1
2 /*
3  * dpdk_test.c - skeleton vpp-api-test plug-in
4  *
5  * Copyright (c) <current-year> <your-organization>
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #include <vat/vat.h>
19 #include <vlibapi/api.h>
20 #include <vlibmemory/api.h>
21 #include <vppinfra/error.h>
22 #include <vnet/ip/ip.h>
23
24 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
25
26 /* Declare message IDs */
27 #include <dpdk/api/dpdk_msg_enum.h>
28
29 /* define message structures */
30 #define vl_typedefs
31 #include <dpdk/api/dpdk.api.h>
32 #undef vl_typedefs
33
34 /* declare message handlers for each api */
35
36 #define vl_endianfun             /* define message structures */
37 #include <dpdk/api/dpdk.api.h>
38 #undef vl_endianfun
39
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...)
42 #define vl_printfun
43 #include <dpdk/api/dpdk.api.h>
44 #undef vl_printfun
45
46 /* Get the API version number. */
47 #define vl_api_version(n,v) static u32 api_version=(v);
48 #include <dpdk/api/dpdk.api.h>
49 #undef vl_api_version
50
51 typedef struct {
52     /* API message ID base */
53     u16 msg_id_base;
54     vat_main_t *vat_main;
55 } dpdk_test_main_t;
56
57 dpdk_test_main_t dpdk_test_main;
58
59 #define foreach_standard_reply_retval_handler         \
60 _(sw_interface_set_dpdk_hqos_pipe_reply)              \
61 _(sw_interface_set_dpdk_hqos_subport_reply)           \
62 _(sw_interface_set_dpdk_hqos_tctbl_reply)
63
64 #define _(n)                                          \
65     static void vl_api_##n##_t_handler                \
66     (vl_api_##n##_t * mp)                             \
67     {                                                 \
68         vat_main_t * vam = dpdk_test_main.vat_main;  \
69         i32 retval = ntohl(mp->retval);               \
70         if (vam->async_mode) {                        \
71             vam->async_errors += (retval < 0);        \
72         } else {                                      \
73             vam->retval = retval;                     \
74             vam->result_ready = 1;                    \
75         }                                             \
76     }
77 foreach_standard_reply_retval_handler;
78 #undef _
79
80 /* 
81  * Table of message reply handlers, must include boilerplate handlers
82  * we just generated
83  */
84 #define foreach_vpe_api_reply_msg                               \
85 _(SW_INTERFACE_SET_DPDK_HQOS_PIPE_REPLY,                        \
86   sw_interface_set_dpdk_hqos_pipe_reply)                        \
87 _(SW_INTERFACE_SET_DPDK_HQOS_SUBPORT_REPLY,                     \
88   sw_interface_set_dpdk_hqos_subport_reply)                     \
89 _(SW_INTERFACE_SET_DPDK_HQOS_TCTBL_REPLY,                       \
90   sw_interface_set_dpdk_hqos_tctbl_reply)
91
92 /* M: construct, but don't yet send a message */
93 #define M(T,t)                                                  \
94 do {                                                            \
95     vam->result_ready = 0;                                      \
96     mp = vl_msg_api_alloc(sizeof(*mp));                         \
97     memset (mp, 0, sizeof (*mp));                               \
98     mp->_vl_msg_id = ntohs (VL_API_##T + dm->msg_id_base);      \
99     mp->client_index = vam->my_client_index;                    \
100 } while(0);
101
102 #define M2(T,t,n)                                               \
103 do {                                                            \
104     vam->result_ready = 0;                                      \
105     mp = vl_msg_api_alloc(sizeof(*mp)+(n));                     \
106     memset (mp, 0, sizeof (*mp));                               \
107     mp->_vl_msg_id = ntohs (VL_API_##T + dm->msg_id_base);      \
108     mp->client_index = vam->my_client_index;                    \
109 } while(0);
110
111 /* S: send a message */
112 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
113
114 /* W: wait for results, with timeout */
115 #define W                                       \
116 do {                                            \
117     timeout = vat_time_now (vam) + 1.0;         \
118                                                 \
119     while (vat_time_now (vam) < timeout) {      \
120         if (vam->result_ready == 1) {           \
121             return (vam->retval);               \
122         }                                       \
123     }                                           \
124     return -99;                                 \
125 } while(0);
126
127 static int
128 api_sw_interface_set_dpdk_hqos_pipe (vat_main_t * vam)
129 {
130   dpdk_test_main_t * dm = &dpdk_test_main;
131   unformat_input_t *i = vam->input;
132   vl_api_sw_interface_set_dpdk_hqos_pipe_t *mp;
133   f64 timeout;
134   u32 sw_if_index;
135   u8 sw_if_index_set = 0;
136   u32 subport;
137   u8 subport_set = 0;
138   u32 pipe;
139   u8 pipe_set = 0;
140   u32 profile;
141   u8 profile_set = 0;
142
143   /* Parse args required to build the message */
144   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
145     {
146         if (unformat (i, "rx sw_if_index %u", &sw_if_index))
147   sw_if_index_set = 1;
148       else if (unformat (i, "subport %u", &subport))
149   subport_set = 1;
150       else if (unformat (i, "pipe %u", &pipe))
151   pipe_set = 1;
152       else if (unformat (i, "profile %u", &profile))
153   profile_set = 1;
154       else
155   break;
156     }
157
158   if (sw_if_index_set == 0)
159     {
160       errmsg ("missing interface name or sw_if_index");
161       return -99;
162     }
163
164   if (subport_set == 0)
165     {
166       errmsg ("missing subport ");
167       return -99;
168     }
169
170   if (pipe_set == 0)
171     {
172       errmsg ("missing pipe");
173       return -99;
174     }
175
176   if (profile_set == 0)
177     {
178       errmsg ("missing profile");
179       return -99;
180     }
181
182   M (SW_INTERFACE_SET_DPDK_HQOS_PIPE, sw_interface_set_dpdk_hqos_pipe);
183
184   mp->sw_if_index = ntohl (sw_if_index);
185   mp->subport = ntohl (subport);
186   mp->pipe = ntohl (pipe);
187   mp->profile = ntohl (profile);
188
189
190   S;
191   W;
192   /* NOTREACHED */
193   return 0;
194 }
195
196 static int
197 api_sw_interface_set_dpdk_hqos_subport (vat_main_t * vam)
198 {
199   dpdk_test_main_t * dm = &dpdk_test_main;
200   unformat_input_t *i = vam->input;
201   vl_api_sw_interface_set_dpdk_hqos_subport_t *mp;
202   f64 timeout;
203   u32 sw_if_index;
204   u8 sw_if_index_set = 0;
205   u32 subport;
206   u8 subport_set = 0;
207   u32 tb_rate = 1250000000; /* 10GbE */
208   u32 tb_size = 1000000;
209   u32 tc_rate[] = { 1250000000, 1250000000, 1250000000, 1250000000 };
210   u32 tc_period = 10;
211
212   /* Parse args required to build the message */
213   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
214     {
215       if (unformat (i, "rx sw_if_index %u", &sw_if_index))
216   sw_if_index_set = 1;
217       else if (unformat (i, "subport %u", &subport))
218   subport_set = 1;
219       else if (unformat (i, "rate %u", &tb_rate))
220   {
221     u32 tc_id;
222
223     for (tc_id = 0; tc_id < (sizeof (tc_rate) / sizeof (tc_rate[0]));
224          tc_id++)
225       tc_rate[tc_id] = tb_rate;
226   }
227       else if (unformat (i, "bktsize %u", &tb_size))
228   ;
229       else if (unformat (i, "tc0 %u", &tc_rate[0]))
230   ;
231       else if (unformat (i, "tc1 %u", &tc_rate[1]))
232   ;
233       else if (unformat (i, "tc2 %u", &tc_rate[2]))
234   ;
235       else if (unformat (i, "tc3 %u", &tc_rate[3]))
236   ;
237       else if (unformat (i, "period %u", &tc_period))
238   ;
239       else
240   break;
241     }
242
243   if (sw_if_index_set == 0)
244     {
245       errmsg ("missing interface name or sw_if_index");
246       return -99;
247     }
248
249   if (subport_set == 0)
250     {
251       errmsg ("missing subport ");
252       return -99;
253     }
254
255   M (SW_INTERFACE_SET_DPDK_HQOS_SUBPORT, sw_interface_set_dpdk_hqos_subport);
256
257   mp->sw_if_index = ntohl (sw_if_index);
258   mp->subport = ntohl (subport);
259   mp->tb_rate = ntohl (tb_rate);
260   mp->tb_size = ntohl (tb_size);
261   mp->tc_rate[0] = ntohl (tc_rate[0]);
262   mp->tc_rate[1] = ntohl (tc_rate[1]);
263   mp->tc_rate[2] = ntohl (tc_rate[2]);
264   mp->tc_rate[3] = ntohl (tc_rate[3]);
265   mp->tc_period = ntohl (tc_period);
266
267   S;
268   W;
269   /* NOTREACHED */
270   return 0;
271 }
272
273 static int
274 api_sw_interface_set_dpdk_hqos_tctbl (vat_main_t * vam)
275 {
276   dpdk_test_main_t * dm = &dpdk_test_main;
277   unformat_input_t *i = vam->input;
278   vl_api_sw_interface_set_dpdk_hqos_tctbl_t *mp;
279   f64 timeout;
280   u32 sw_if_index;
281   u8 sw_if_index_set = 0;
282   u8 entry_set = 0;
283   u8 tc_set = 0;
284   u8 queue_set = 0;
285   u32 entry, tc, queue;
286
287   /* Parse args required to build the message */
288   while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT)
289     {
290       if (unformat (i, "rx sw_if_index %u", &sw_if_index))
291   sw_if_index_set = 1;
292       else if (unformat (i, "entry %d", &entry))
293   entry_set = 1;
294       else if (unformat (i, "tc %d", &tc))
295   tc_set = 1;
296       else if (unformat (i, "queue %d", &queue))
297   queue_set = 1;
298       else
299   break;
300     }
301
302   if (sw_if_index_set == 0)
303     {
304       errmsg ("missing interface name or sw_if_index");
305       return -99;
306     }
307
308   if (entry_set == 0)
309     {
310       errmsg ("missing entry ");
311       return -99;
312     }
313
314   if (tc_set == 0)
315     {
316       errmsg ("missing traffic class ");
317       return -99;
318     }
319
320   if (queue_set == 0)
321     {
322       errmsg ("missing queue ");
323       return -99;
324     }
325
326   M (SW_INTERFACE_SET_DPDK_HQOS_TCTBL, sw_interface_set_dpdk_hqos_tctbl);
327
328   mp->sw_if_index = ntohl (sw_if_index);
329   mp->entry = ntohl (entry);
330   mp->tc = ntohl (tc);
331   mp->queue = ntohl (queue);
332
333   S;
334   W;
335   /* NOTREACHED */
336   return 0;
337 }
338
339 /* 
340  * List of messages that the api test plugin sends,
341  * and that the data plane plugin processes
342  */
343 #define foreach_vpe_api_msg                                               \
344 _(sw_interface_set_dpdk_hqos_pipe,                                        \
345   "rx sw_if_index <id> subport <subport-id> pipe <pipe-id>\n"   \
346   "profile <profile-id>\n")                                               \
347 _(sw_interface_set_dpdk_hqos_subport,                                     \
348   "rx sw_if_index <id> subport <subport-id> [rate <n>]\n"       \
349   "[bktsize <n>] [tc0 <n>] [tc1 <n>] [tc2 <n>] [tc3 <n>] [period <n>]\n") \
350 _(sw_interface_set_dpdk_hqos_tctbl,                                       \
351   "rx sw_if_index <id> entry <n> tc <n> queue <n>\n")
352
353 static void dpdk_api_hookup (vat_main_t *vam)
354 {
355   dpdk_test_main_t * dm __attribute__((unused)) = &dpdk_test_main;
356   /* Hook up handlers for replies from the data plane plug-in */
357 #define _(N,n)                                                  \
358   vl_msg_api_set_handlers((VL_API_##N + dm->msg_id_base),       \
359                           #n,                                   \
360                           vl_api_##n##_t_handler,               \
361                           vl_noop_handler,                      \
362                           vl_api_##n##_t_endian,                \
363                           vl_api_##n##_t_print,                 \
364                           sizeof(vl_api_##n##_t), 1); 
365   foreach_vpe_api_reply_msg;
366 #undef _
367
368   /* API messages we can send */
369 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
370   foreach_vpe_api_msg;
371 #undef _    
372     
373   /* Help strings */
374 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
375   foreach_vpe_api_msg;
376 #undef _
377 }
378
379 clib_error_t * vat_plugin_register (vat_main_t *vam)
380 {
381   dpdk_test_main_t * dm = &dpdk_test_main;
382   u8 * name;
383
384   dm->vat_main = vam;
385
386   /* Ask the vpp engine for the first assigned message-id */
387   name = format (0, "dpdk_%08x%c", api_version, 0);
388   dm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
389
390   if (dm->msg_id_base != (u16) ~0)
391     dpdk_api_hookup (vam);
392   
393   vec_free(name);
394   
395   return 0;
396 }