a47710ee1c35c8562633439f746a5ce87750d8ad
[vpp.git] / src / examples / 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 #include <vlibapi/vat_helper_macros.h>
27
28 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
29
30 /* Declare message IDs */
31 #include <sample/sample_msg_enum.h>
32
33 /* define message structures */
34 #define vl_typedefs
35 #include <sample/sample_all_api_h.h> 
36 #undef vl_typedefs
37
38 /* declare message handlers for each api */
39
40 #define vl_endianfun             /* define message structures */
41 #include <sample/sample_all_api_h.h> 
42 #undef vl_endianfun
43
44 /* instantiate all the print functions we know about */
45 #define vl_print(handle, ...)
46 #define vl_printfun
47 #include <sample/sample_all_api_h.h> 
48 #undef vl_printfun
49
50 /* Get the API version number. */
51 #define vl_api_version(n,v) static u32 api_version=(v);
52 #include <sample/sample_all_api_h.h>
53 #undef vl_api_version
54
55
56 typedef struct {
57     /* API message ID base */
58     u16 msg_id_base;
59     vat_main_t *vat_main;
60 } sample_test_main_t;
61
62 sample_test_main_t sample_test_main;
63
64 #define foreach_standard_reply_retval_handler   \
65 _(sample_macswap_enable_disable_reply)
66
67 #define _(n)                                            \
68     static void vl_api_##n##_t_handler                  \
69     (vl_api_##n##_t * mp)                               \
70     {                                                   \
71         vat_main_t * vam = sample_test_main.vat_main;   \
72         i32 retval = ntohl(mp->retval);                 \
73         if (vam->async_mode) {                          \
74             vam->async_errors += (retval < 0);          \
75         } else {                                        \
76             vam->retval = retval;                       \
77             vam->result_ready = 1;                      \
78         }                                               \
79     }
80 foreach_standard_reply_retval_handler;
81 #undef _
82
83 /* 
84  * Table of message reply handlers, must include boilerplate handlers
85  * we just generated
86  */
87 #define foreach_vpe_api_reply_msg                                       \
88 _(SAMPLE_MACSWAP_ENABLE_DISABLE_REPLY, sample_macswap_enable_disable_reply)
89
90
91 static int api_sample_macswap_enable_disable (vat_main_t * vam)
92 {
93     sample_test_main_t * sm = &sample_test_main;
94     unformat_input_t * i = vam->input;
95     f64 timeout;
96     int enable_disable = 1;
97     u32 sw_if_index = ~0;
98     vl_api_sample_macswap_enable_disable_t * mp;
99
100     /* Parse args required to build the message */
101     while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) {
102         if (unformat (i, "%U", unformat_sw_if_index, vam, &sw_if_index))
103             ;
104         else if (unformat (i, "sw_if_index %d", &sw_if_index))
105             ;
106         else if (unformat (i, "disable"))
107             enable_disable = 0;
108         else
109             break;
110     }
111     
112     if (sw_if_index == ~0) {
113         errmsg ("missing interface name / explicit sw_if_index number \n");
114         return -99;
115     }
116     
117     /* Construct the API message */
118     M(SAMPLE_MACSWAP_ENABLE_DISABLE, sample_macswap_enable_disable);
119     mp->sw_if_index = ntohl (sw_if_index);
120     mp->enable_disable = enable_disable;
121
122     /* send it... */
123     S;
124
125     /* Wait for a reply... */
126     W;
127 }
128
129 /* 
130  * List of messages that the api test plugin sends,
131  * and that the data plane plugin processes
132  */
133 #define foreach_vpe_api_msg \
134 _(sample_macswap_enable_disable, "<intfc> [disable]")
135
136 void vat_api_hookup (vat_main_t *vam)
137 {
138     sample_test_main_t * sm = &sample_test_main;
139     /* Hook up handlers for replies from the data plane plug-in */
140 #define _(N,n)                                                  \
141     vl_msg_api_set_handlers((VL_API_##N + sm->msg_id_base),     \
142                            #n,                                  \
143                            vl_api_##n##_t_handler,              \
144                            vl_noop_handler,                     \
145                            vl_api_##n##_t_endian,               \
146                            vl_api_##n##_t_print,                \
147                            sizeof(vl_api_##n##_t), 1); 
148     foreach_vpe_api_reply_msg;
149 #undef _
150
151     /* API messages we can send */
152 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
153     foreach_vpe_api_msg;
154 #undef _    
155     
156     /* Help strings */
157 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
158     foreach_vpe_api_msg;
159 #undef _
160 }
161
162 clib_error_t * vat_plugin_register (vat_main_t *vam)
163 {
164   sample_test_main_t * sm = &sample_test_main;
165   u8 * name;
166
167   sm->vat_main = vam;
168
169   name = format (0, "sample_%08x%c", api_version, 0);
170   sm->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
171
172   if (sm->msg_id_base != (u16) ~0)
173     vat_api_hookup (vam);
174   
175   vec_free(name);
176   
177   return 0;
178 }