dd1b0215751b0274b265dd587a4f5795d1a319fc
[vpp.git] / plugins / sample-plugin / sample / sample_test.c
1 /*
2  * Copyright (c) 2015 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 /*
16  *------------------------------------------------------------------
17  * sample_test.c - test harness plugin
18  *------------------------------------------------------------------
19  */
20
21 #include <vat/vat.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vlibsocket/api.h>
25 #include <vppinfra/error.h>
26
27 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
28
29 /* Declare message IDs */
30 #include <sample/sample_msg_enum.h>
31
32 /* define message structures */
33 #define vl_typedefs
34 #include <sample/sample_all_api_h.h> 
35 #undef vl_typedefs
36
37 /* declare message handlers for each api */
38
39 #define vl_endianfun             /* define message structures */
40 #include <sample/sample_all_api_h.h> 
41 #undef vl_endianfun
42
43 /* instantiate all the print functions we know about */
44 #define vl_print(handle, ...)
45 #define vl_printfun
46 #include <sample/sample_all_api_h.h> 
47 #undef vl_printfun
48
49 /* Get the API version number. */
50 #define vl_api_version(n,v) static u32 api_version=(v);
51 #include <sample/sample_all_api_h.h>
52 #undef vl_api_version
53
54
55 typedef struct {
56     /* API message ID base */
57     u16 msg_id_base;
58     vat_main_t *vat_main;
59 } sample_test_main_t;
60
61 sample_test_main_t sample_test_main;
62
63 #define foreach_standard_reply_retval_handler   \
64 _(sample_macswap_enable_disable_reply)
65
66 #define _(n)                                            \
67     static void vl_api_##n##_t_handler                  \
68     (vl_api_##n##_t * mp)                               \
69     {                                                   \
70         vat_main_t * vam = sample_test_main.vat_main;   \
71         i32 retval = ntohl(mp->retval);                 \
72         if (vam->async_mode) {                          \
73             vam->async_errors += (retval < 0);          \
74         } else {                                        \
75             vam->retval = retval;                       \
76             vam->result_ready = 1;                      \
77         }                                               \
78     }
79 foreach_standard_reply_retval_handler;
80 #undef _
81
82 /* 
83  * Table of message reply handlers, must include boilerplate handlers
84  * we just generated
85  */
86 #define foreach_vpe_api_reply_msg                                       \
87 _(SAMPLE_MACSWAP_ENABLE_DISABLE_REPLY, sample_macswap_enable_disable_reply)
88
89
90 /* M: construct, but don't yet send a message */
91
92 #define M(T,t)                                                  \
93 do {                                                            \
94     vam->result_ready = 0;                                      \
95     mp = vl_msg_api_alloc(sizeof(*mp));                         \
96     memset (mp, 0, sizeof (*mp));                               \
97     mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
98     mp->client_index = vam->my_client_index;                    \
99 } while(0);
100
101 #define M2(T,t,n)                                               \
102 do {                                                            \
103     vam->result_ready = 0;                                      \
104     mp = vl_msg_api_alloc(sizeof(*mp)+(n));                     \
105     memset (mp, 0, sizeof (*mp));                               \
106     mp->_vl_msg_id = ntohs (VL_API_##T + sm->msg_id_base);      \
107     mp->client_index = vam->my_client_index;                    \
108 } while(0);
109
110 /* S: send a message */
111 #define S (vl_msg_api_send_shmem (vam->vl_input_queue, (u8 *)&mp))
112
113 /* W: wait for results, with timeout */
114 #define W                                       \
115 do {                                            \
116     timeout = vat_time_now (vam) + 1.0;         \
117                                                 \
118     while (vat_time_now (vam) < timeout) {      \
119         if (vam->result_ready == 1) {           \
120             return (vam->retval);               \
121         }                                       \
122     }                                           \
123     return -99;                                 \
124 } while(0);
125
126 static int api_sample_macswap_enable_disable (vat_main_t * vam)
127 {
128     sample_test_main_t * sm = &sample_test_main;
129     unformat_input_t * i = vam->input;
130     f64 timeout;
131     int enable_disable = 1;
132     u32 sw_if_index = ~0;
133     vl_api_sample_macswap_enable_disable_t * mp;
134
135     /* Parse args required to build the message */
136     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
137         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
138             ;
139         else if (unformat (i, "sw_if_index %d", &sw_if_index))
140             ;
141         else if (unformat (i, "disable"))
142             enable_disable = 0;
143         else
144             break;
145     }
146     
147     if (sw_if_index == ~0) {
148         errmsg ("missing interface name / explicit sw_if_index number \n");
149         return -99;
150     }
151     
152     /* Construct the API message */
153     M(SAMPLE_MACSWAP_ENABLE_DISABLE, sample_macswap_enable_disable);
154     mp->sw_if_index = ntohl (sw_if_index);
155     mp->enable_disable = enable_disable;
156
157     /* send it... */
158     S;
159
160     /* Wait for a reply... */
161     W;
162 }
163
164 /* 
165  * List of messages that the api test plugin sends,
166  * and that the data plane plugin processes
167  */
168 #define foreach_vpe_api_msg \
169 _(sample_macswap_enable_disable, "<intfc> [disable]")
170
171 void vat_api_hookup (vat_main_t *vam)
172 {
173     sample_test_main_t * sm = &sample_test_main;
174     /* Hook up handlers for replies from the data plane plug-in */
175 #define _(N,n)                                                  \
176     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
177                            #n,                                  \
178                            vl_api_##n##_t_handler,              \
179                            vl_noop_handler,                     \
180                            vl_api_##n##_t_endian,               \
181                            vl_api_##n##_t_print,                \
182                            sizeof(vl_api_##n##_t), 1); 
183     foreach_vpe_api_reply_msg;
184 #undef _
185
186     /* API messages we can send */
187 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
188     foreach_vpe_api_msg;
189 #undef _    
190     
191     /* Help strings */
192 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
193     foreach_vpe_api_msg;
194 #undef _
195 }
196
197 clib_error_t * vat_plugin_register (vat_main_t *vam)
198 {
199   sample_test_main_t * sm = &sample_test_main;
200   u8 * name;
201
202   sm->vat_main = vam;
203
204   name = format (0, "sample_%08x%c", api_version, 0);
205   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
206
207   if (sm->msg_id_base != (u16) ~0)
208     vat_api_hookup (vam);
209   
210   vec_free(name);
211   
212   return 0;
213 }