2ad4acfb0c1e46b2958d0ce56180a5b217419153
[vpp.git] / src / plugins / http_static / http_static.c
1 /*
2  * http_static.c - skeleton vpp engine 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
18 #include <vnet/vnet.h>
19 #include <vnet/plugin/plugin.h>
20 #include <http_static/http_static.h>
21
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vpp/app/version.h>
25
26 /* define message IDs */
27 #include <http_static/http_static_msg_enum.h>
28
29 #include <vpp/api/types.h>
30
31 /* define message structures */
32 #define vl_typedefs
33 #include <http_static/http_static_all_api_h.h>
34 #undef vl_typedefs
35
36 /* define generated endian-swappers */
37 #define vl_endianfun
38 #include <http_static/http_static_all_api_h.h>
39 #undef vl_endianfun
40
41 /* instantiate all the print functions we know about */
42 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
43 #define vl_printfun
44 #include <http_static/http_static_all_api_h.h>
45 #undef vl_printfun
46
47 /* Get the API version number */
48 #define vl_api_version(n,v) static u32 api_version=(v);
49 #include <http_static/http_static_all_api_h.h>
50 #undef vl_api_version
51
52 #define REPLY_MSG_ID_BASE hmp->msg_id_base
53 #include <vlibapi/api_helper_macros.h>
54
55 http_static_main_t http_static_main;
56
57 /* List of message types that this plugin understands */
58
59 #define foreach_http_static_plugin_api_msg                           \
60 _(HTTP_STATIC_ENABLE, http_static_enable)
61
62 /* API message handler */
63 static void vl_api_http_static_enable_t_handler
64   (vl_api_http_static_enable_t * mp)
65 {
66   vl_api_http_static_enable_reply_t *rmp;
67   http_static_main_t *hmp = &http_static_main;
68   int rv;
69   u8 *www_root = 0;
70   u8 *uri = 0;
71
72   char *p = (char *) &mp->www_root;
73   www_root = vl_api_from_api_to_vec ((vl_api_string_t *) p);
74   p += vl_api_string_len ((vl_api_string_t *) p) + sizeof (vl_api_string_t);
75   uri = vl_api_from_api_to_vec ((vl_api_string_t *) p);
76
77   rv = http_static_server_enable_api
78     (ntohl (mp->fifo_size),
79      ntohl (mp->cache_size_limit),
80      ntohl (mp->prealloc_fifos),
81      ntohl (mp->private_segment_size), www_root, uri);
82
83   vec_free (www_root);
84   vec_free (uri);
85   REPLY_MACRO (VL_API_HTTP_STATIC_ENABLE_REPLY);
86 }
87
88 /* Set up the API message handling tables */
89 static clib_error_t *
90 http_static_plugin_api_hookup (vlib_main_t * vm)
91 {
92   http_static_main_t *hmp = &http_static_main;
93 #define _(N,n)                                                  \
94     vl_msg_api_set_handlers((VL_API_##N + hmp->msg_id_base),     \
95                            #n,                                  \
96                            vl_api_##n##_t_handler,              \
97                            vl_noop_handler,                     \
98                            vl_api_##n##_t_endian,               \
99                            vl_api_##n##_t_print,                \
100                            sizeof(vl_api_##n##_t), 1);
101   foreach_http_static_plugin_api_msg;
102 #undef _
103
104   return 0;
105 }
106
107 #define vl_msg_name_crc_list
108 #include <http_static/http_static_all_api_h.h>
109 #undef vl_msg_name_crc_list
110
111 static void
112 setup_message_id_table (http_static_main_t * hmp, api_main_t * am)
113 {
114 #define _(id,n,crc)   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + hmp->msg_id_base);
115   foreach_vl_msg_name_crc_http_static;
116 #undef _
117 }
118
119 static clib_error_t *
120 http_static_init (vlib_main_t * vm)
121 {
122   http_static_main_t *hmp = &http_static_main;
123   clib_error_t *error = 0;
124   u8 *name;
125
126   hmp->vlib_main = vm;
127   hmp->vnet_main = vnet_get_main ();
128
129   name = format (0, "http_static_%08x%c", api_version, 0);
130
131   /* Ask for a correctly-sized block of API message decode slots */
132   hmp->msg_id_base = vl_msg_api_get_msg_ids
133     ((char *) name, VL_MSG_FIRST_AVAILABLE);
134
135   error = http_static_plugin_api_hookup (vm);
136
137   /* Add our API messages to the global name_crc hash table */
138   setup_message_id_table (hmp, &api_main);
139
140   vec_free (name);
141
142   return error;
143 }
144
145 VLIB_INIT_FUNCTION (http_static_init);
146
147 /* *INDENT-OFF* */
148 VLIB_PLUGIN_REGISTER () =
149 {
150   .version = VPP_BUILD_VER,
151   .description = "HTTP Static Server"
152 };
153 /* *INDENT-ON* */
154
155 /*
156  * fd.io coding-style-patch-verification: ON
157  *
158  * Local Variables:
159  * eval: (c-set-style "gnu")
160  * End:
161  */