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