api: string type to convert to vector
[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 #include <vlibapi/api_types_inlines.h>
22
23 uword unformat_sw_if_index (unformat_input_t * input, va_list * args);
24
25 /* Declare message IDs */
26 #include <http_static/http_static_msg_enum.h>
27
28 /* define message structures */
29 #define vl_typedefs
30 #include <http_static/http_static_all_api_h.h>
31 #undef vl_typedefs
32
33 /* declare message handlers for each api */
34
35 #define vl_endianfun            /* define message structures */
36 #include <http_static/http_static_all_api_h.h>
37 #undef vl_endianfun
38
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...)
41 #define vl_printfun
42 #include <http_static/http_static_all_api_h.h>
43 #undef vl_printfun
44
45 /* Get the API version number. */
46 #define vl_api_version(n,v) static u32 api_version=(v);
47 #include <http_static/http_static_all_api_h.h>
48 #undef vl_api_version
49
50
51 typedef struct
52 {
53   /* API message ID base */
54   u16 msg_id_base;
55   vat_main_t *vat_main;
56 } http_static_test_main_t;
57
58 http_static_test_main_t http_static_test_main;
59
60 #define __plugin_msg_base http_static_test_main.msg_id_base
61 #include <vlibapi/vat_helper_macros.h>
62
63 #define foreach_standard_reply_retval_handler   \
64 _(http_static_enable_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 = http_static_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 _(HTTP_STATIC_ENABLE_REPLY, http_static_enable_reply)
88
89
90 static int
91 api_http_static_enable (vat_main_t * vam)
92 {
93   unformat_input_t *line_input = vam->input;
94   vl_api_http_static_enable_t *mp;
95   u64 tmp;
96   u8 *www_root = 0;
97   u8 *uri = 0;
98   u32 prealloc_fifos = 0;
99   u32 private_segment_size = 0;
100   u32 fifo_size = 8 << 10;
101   u32 cache_size_limit = 1 << 20;
102   int ret;
103
104   /* Parse args required to build the message */
105   while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT)
106     {
107       if (unformat (line_input, "www-root %s", &www_root))
108         ;
109       else if (unformat (line_input, "prealloc-fifos %d", &prealloc_fifos))
110         ;
111       else if (unformat (line_input, "private-segment-size %U",
112                          unformat_memory_size, &tmp))
113         {
114           if (tmp >= 0x100000000ULL)
115             {
116               errmsg ("private segment size %llu, too large", tmp);
117               return -99;
118             }
119           private_segment_size = (u32) tmp;
120         }
121       else if (unformat (line_input, "fifo-size %U", unformat_memory_size,
122                          &tmp))
123         {
124           fifo_size = (u32) tmp;
125         }
126       else if (unformat (line_input, "cache-size %U", unformat_memory_size,
127                          &tmp))
128         {
129           if (tmp < (128ULL << 10))
130             {
131               errmsg ("cache-size must be at least 128kb");
132               return -99;
133             }
134           cache_size_limit = (u32) tmp;
135         }
136
137       else if (unformat (line_input, "uri %s", &uri))
138         ;
139       else
140         {
141           errmsg ("unknown input `%U'", format_unformat_error, line_input);
142           return -99;
143         }
144     }
145
146   if (www_root == 0)
147     {
148       errmsg ("Must specify www-root");
149       return -99;
150     }
151
152   if (uri == 0)
153     uri = format (0, "tcp://0.0.0.0/80%c", 0);
154
155
156
157   /* Construct the API message */
158   M (HTTP_STATIC_ENABLE, mp);
159   vl_api_to_api_string (strnlen ((const char *) www_root, 256),
160                         (const char *) www_root,
161                         (vl_api_string_t *) & mp->www_root);
162   vl_api_to_api_string (strnlen ((const char *) uri, 256), (const char *) uri,
163                         (vl_api_string_t *) & mp->uri);
164   mp->fifo_size = ntohl (fifo_size);
165   mp->cache_size_limit = ntohl (cache_size_limit);
166   mp->prealloc_fifos = ntohl (prealloc_fifos);
167   mp->private_segment_size = ntohl (private_segment_size);
168
169   /* send it... */
170   S (mp);
171
172   /* Wait for a reply... */
173   W (ret);
174   return ret;
175 }
176
177 /*
178  * List of messages that the api test plugin sends,
179  * and that the data plane plugin processes
180  */
181 #define foreach_vpe_api_msg                                             \
182 _(http_static_enable, "www-root <path> [prealloc-fios <nn>]\n"          \
183 "[private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]\n")
184
185 static void
186 http_static_api_hookup (vat_main_t * vam)
187 {
188   http_static_test_main_t *htmp = &http_static_test_main;
189   /* Hook up handlers for replies from the data plane plug-in */
190 #define _(N,n)                                                  \
191     vl_msg_api_set_handlers((VL_API_##N + htmp->msg_id_base),     \
192                            #n,                                  \
193                            vl_api_##n##_t_handler,              \
194                            vl_noop_handler,                     \
195                            vl_api_##n##_t_endian,               \
196                            vl_api_##n##_t_print,                \
197                            sizeof(vl_api_##n##_t), 1);
198   foreach_vpe_api_reply_msg;
199 #undef _
200
201   /* API messages we can send */
202 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
203   foreach_vpe_api_msg;
204 #undef _
205
206   /* Help strings */
207 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
208   foreach_vpe_api_msg;
209 #undef _
210 }
211
212 clib_error_t *
213 vat_plugin_register (vat_main_t * vam)
214 {
215   http_static_test_main_t *htmp = &http_static_test_main;
216   u8 *name;
217
218   htmp->vat_main = vam;
219
220   /* Ask the vpp engine for the first assigned message-id */
221   name = format (0, "http_static_%08x%c", api_version, 0);
222   htmp->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
223
224   if (htmp->msg_id_base != (u16) ~ 0)
225     http_static_api_hookup (vam);
226
227   vec_free (name);
228
229   return 0;
230 }
231
232 /*
233  * fd.io coding-style-patch-verification: ON
234  *
235  * Local Variables:
236  * eval: (c-set-style "gnu")
237  * End:
238  */