plugins: http_static. Migrate to use api string type.
[vpp.git] / src / plugins / http_static / http_static_test.c
1 /*
2  * http_static.c - skeleton vpp-api-test plug-in
3  *
4  * Copyright (c) <current-year> <your-organization>
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 #include <vat/vat.h>
18 #include <vlibapi/api.h>
19 #include <vlibmemory/api.h>
20 #include <vppinfra/error.h>
21
22 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
23
24 /* Declare message IDs */
25 #include <http_static/http_static_msg_enum.h>
26
27 /* define message structures */
28 #define vl_typedefs
29 #include <http_static/http_static_all_api_h.h>
30 #undef vl_typedefs
31
32 /* declare message handlers for each api */
33
34 #define vl_endianfun            /* define message structures */
35 #include <http_static/http_static_all_api_h.h>
36 #undef vl_endianfun
37
38 /* instantiate all the print functions we know about */
39 #define vl_print(handle, ...)
40 #define vl_printfun
41 #include <http_static/http_static_all_api_h.h>
42 #undef vl_printfun
43
44 /* Get the API version number. */
45 #define vl_api_version(n,v) static u32 api_version=(v);
46 #include <http_static/http_static_all_api_h.h>
47 #undef vl_api_version
48
49
50 typedef struct
51 {
52   /* API message ID base */
53   u16 msg_id_base;
54   vat_main_t *vat_main;
55 } http_static_test_main_t;
56
57 http_static_test_main_t http_static_test_main;
58
59 #define __plugin_msg_base http_static_test_main.msg_id_base
60 #include <vlibapi/vat_helper_macros.h>
61
62 #define foreach_standard_reply_retval_handler   \
63 _(http_static_enable_reply)
64
65 #define _(n)                                            \
66     static void vl_api_##n##_t_handler                  \
67     (vl_api_##n##_t * mp)                               \
68     {                                                   \
69         vat_main_t * vam = http_static_test_main.vat_main;   \
70         i32 retval = ntohl(mp->retval);                 \
71         if (vam->async_mode) {                          \
72             vam->async_errors += (retval < 0);          \
73         } else {                                        \
74             vam->retval = retval;                       \
75             vam->result_ready = 1;                      \
76         }                                               \
77     }
78 foreach_standard_reply_retval_handler;
79 #undef _
80
81 /*
82  * Table of message reply handlers, must include boilerplate handlers
83  * we just generated
84  */
85 #define foreach_vpe_api_reply_msg                       \
86 _(HTTP_STATIC_ENABLE_REPLY, http_static_enable_reply)
87
88
89 static int
90 api_http_static_enable (vat_main_t * vam)
91 {
92   unformat_input_t *line_input = vam->input;
93   vl_api_http_static_enable_t *mp;
94   u64 tmp;
95   u8 *www_root = 0;
96   u8 *uri = 0;
97   u32 prealloc_fifos = 0;
98   u32 private_segment_size = 0;
99   u32 fifo_size = 8 << 10;
100   u32 cache_size_limit = 1 << 20;
101   int ret;
102
103   /* Parse args required to build the message */
104   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
105     {
106       if (unformat (line_input, "www-root %s", &www_root))
107         ;
108       else if (unformat (line_input, "prealloc-fifos %d", &prealloc_fifos))
109         ;
110       else if (unformat (line_input, "private-segment-size %U",
111                          unformat_memory_size, &tmp))
112         {
113           if (tmp >= 0x100000000ULL)
114             {
115               errmsg ("private segment size %llu, too large", tmp);
116               return -99;
117             }
118           private_segment_size = (u32) tmp;
119         }
120       else if (unformat (line_input, "fifo-size %U", unformat_memory_size,
121                          &tmp))
122         {
123           fifo_size = (u32) tmp;
124         }
125       else if (unformat (line_input, "cache-size %U", unformat_memory_size,
126                          &tmp))
127         {
128           if (tmp < (128ULL << 10))
129             {
130               errmsg ("cache-size must be at least 128kb");
131               return -99;
132             }
133           cache_size_limit = (u32) tmp;
134         }
135
136       else if (unformat (line_input, "uri %s", &uri))
137         ;
138       else
139         {
140           errmsg ("unknown input `%U'", format_unformat_error, line_input);
141           return -99;
142         }
143     }
144
145   if (www_root == 0)
146     {
147       errmsg ("Must specify www-root");
148       return -99;
149     }
150
151   if (uri == 0)
152     uri = format (0, "tcp://0.0.0.0/80%c", 0);
153
154
155
156   /* Construct the API message */
157   M (HTTP_STATIC_ENABLE, mp);
158   vl_api_to_api_string (strnlen ((const char *) www_root, 256),
159                         (const char *) www_root,
160                         (vl_api_string_t *) & mp->www_root);
161   vl_api_to_api_string (strnlen ((const char *) uri, 256), (const char *) uri,
162                         (vl_api_string_t *) & mp->uri);
163   mp->fifo_size = ntohl (fifo_size);
164   mp->cache_size_limit = ntohl (cache_size_limit);
165   mp->prealloc_fifos = ntohl (prealloc_fifos);
166   mp->private_segment_size = ntohl (private_segment_size);
167
168   /* send it... */
169   S (mp);
170
171   /* Wait for a reply... */
172   W (ret);
173   return ret;
174 }
175
176 /*
177  * List of messages that the api test plugin sends,
178  * and that the data plane plugin processes
179  */
180 #define foreach_vpe_api_msg                                             \
181 _(http_static_enable, "www-root <path> [prealloc-fios <nn>]\n"          \
182 "[private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]\n")
183
184 static void
185 http_static_api_hookup (vat_main_t * vam)
186 {
187   http_static_test_main_t *htmp = &http_static_test_main;
188   /* Hook up handlers for replies from the data plane plug-in */
189 #define _(N,n)                                                  \
190     vl_msg_api_set_handlers((VL_API_##N + htmp->msg_id_base),     \
191                            #n,                                  \
192                            vl_api_##n##_t_handler,              \
193                            vl_noop_handler,                     \
194                            vl_api_##n##_t_endian,               \
195                            vl_api_##n##_t_print,                \
196                            sizeof(vl_api_##n##_t), 1);
197   foreach_vpe_api_reply_msg;
198 #undef _
199
200   /* API messages we can send */
201 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
202   foreach_vpe_api_msg;
203 #undef _
204
205   /* Help strings */
206 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
207   foreach_vpe_api_msg;
208 #undef _
209 }
210
211 clib_error_t *
212 vat_plugin_register (vat_main_t * vam)
213 {
214   http_static_test_main_t *htmp = &http_static_test_main;
215   u8 *name;
216
217   htmp->vat_main = vam;
218
219   /* Ask the vpp engine for the first assigned message-id */
220   name = format (0, "http_static_%08x%c", api_version, 0);
221   htmp->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
222
223   if (htmp->msg_id_base != (u16) ~ 0)
224     http_static_api_hookup (vam);
225
226   vec_free (name);
227
228   return 0;
229 }
230
231 /*
232  * fd.io coding-style-patch-verification: ON
233  *
234  * Local Variables:
235  * eval: (c-set-style "gnu")
236  * End:
237  */