Static http server
[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 (www_root == 0)
152     {
153       errmsg ("Must specify www-root");
154       return -99;
155     }
156
157   if (uri == 0)
158     uri = format (0, "tcp://0.0.0.0/80%c", 0);
159
160
161
162   /* Construct the API message */
163   M (HTTP_STATIC_ENABLE, mp);
164   clib_strncpy ((char *) mp->www_root, (char *) www_root,
165                 ARRAY_LEN (mp->www_root) - 1);
166   clib_strncpy ((char *) mp->uri, (char *) uri, ARRAY_LEN (mp->uri) - 1);
167   mp->fifo_size = ntohl (fifo_size);
168   mp->cache_size_limit = ntohl (cache_size_limit);
169   mp->prealloc_fifos = ntohl (prealloc_fifos);
170   mp->private_segment_size = ntohl (private_segment_size);
171
172   /* send it... */
173   S (mp);
174
175   /* Wait for a reply... */
176   W (ret);
177   return ret;
178 }
179
180 /*
181  * List of messages that the api test plugin sends,
182  * and that the data plane plugin processes
183  */
184 #define foreach_vpe_api_msg                                             \
185 _(http_static_enable, "www-root <path> [prealloc-fios <nn>]\n"          \
186 "[private-segment-size <nnMG>] [fifo-size <nbytes>] [uri <uri>]\n")
187
188 static void
189 http_static_api_hookup (vat_main_t * vam)
190 {
191   http_static_test_main_t *htmp = &http_static_test_main;
192   /* Hook up handlers for replies from the data plane plug-in */
193 #define _(N,n)                                                  \
194     vl_msg_api_set_handlers((VL_API_##N + htmp->msg_id_base),     \
195                            #n,                                  \
196                            vl_api_##n##_t_handler,              \
197                            vl_noop_handler,                     \
198                            vl_api_##n##_t_endian,               \
199                            vl_api_##n##_t_print,                \
200                            sizeof(vl_api_##n##_t), 1);
201   foreach_vpe_api_reply_msg;
202 #undef _
203
204   /* API messages we can send */
205 #define _(n,h) hash_set_mem (vam->function_by_name, #n, api_##n);
206   foreach_vpe_api_msg;
207 #undef _
208
209   /* Help strings */
210 #define _(n,h) hash_set_mem (vam->help_by_name, #n, h);
211   foreach_vpe_api_msg;
212 #undef _
213 }
214
215 clib_error_t *
216 vat_plugin_register (vat_main_t * vam)
217 {
218   http_static_test_main_t *htmp = &http_static_test_main;
219   u8 *name;
220
221   htmp->vat_main = vam;
222
223   /* Ask the vpp engine for the first assigned message-id */
224   name = format (0, "http_static_%08x%c", api_version, 0);
225   htmp->msg_id_base = vl_client_get_first_plugin_msg_id ((char *) name);
226
227   if (htmp->msg_id_base != (u16) ~ 0)
228     http_static_api_hookup (vam);
229
230   vec_free (name);
231
232   return 0;
233 }
234
235 /*
236  * fd.io coding-style-patch-verification: ON
237  *
238  * Local Variables:
239  * eval: (c-set-style "gnu")
240  * End:
241  */