builtinurl: initial working attempt
[vpp.git] / src / plugins / builtinurl / builtinurl.c
1 /*
2  * builtinurl.c - skeleton vpp engine plug-in
3  *
4  * Copyright (c) 2019 Cisco and/or its affiliates.
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 <builtinurl/builtinurl.h>
21
22 #include <vlibapi/api.h>
23 #include <vlibmemory/api.h>
24 #include <vpp/app/version.h>
25 #include <stdbool.h>
26
27 /* define message IDs */
28 #include <builtinurl/builtinurl_msg_enum.h>
29
30 /* define message structures */
31 #define vl_typedefs
32 #include <builtinurl/builtinurl_all_api_h.h>
33 #undef vl_typedefs
34
35 /* define generated endian-swappers */
36 #define vl_endianfun
37 #include <builtinurl/builtinurl_all_api_h.h>
38 #undef vl_endianfun
39
40 /* instantiate all the print functions we know about */
41 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
42 #define vl_printfun
43 #include <builtinurl/builtinurl_all_api_h.h>
44 #undef vl_printfun
45
46 /* Get the API version number */
47 #define vl_api_version(n,v) static u32 api_version=(v);
48 #include <builtinurl/builtinurl_all_api_h.h>
49 #undef vl_api_version
50
51 #define REPLY_MSG_ID_BASE bmp->msg_id_base
52 #include <vlibapi/api_helper_macros.h>
53
54 builtinurl_main_t builtinurl_main;
55
56 /* List of message types that this plugin understands */
57
58 #define foreach_builtinurl_plugin_api_msg                           \
59 _(BUILTINURL_ENABLE, builtinurl_enable)
60
61 /* Action function shared between message handler and debug CLI */
62
63 int
64 builtinurl_enable (builtinurl_main_t * bmp)
65 {
66   void (*fp) (void *, char *, int);
67
68   if (bmp->initialized)
69     return 0;
70
71   /* Look up the builtin URL registration handler */
72   fp = vlib_get_plugin_symbol
73     ("http_static_plugin.so", "http_static_server_register_builtin_handler");
74
75   /* Most likely, the http_static plugin isn't loaded. Done. */
76   if (fp == 0)
77     return VNET_API_ERROR_NO_SUCH_TABLE;
78
79   bmp->register_handler = fp;
80   builtinurl_handler_init (bmp);
81   bmp->initialized = 1;
82
83   return 0;
84 }
85
86 static clib_error_t *
87 builtinurl_enable_command_fn (vlib_main_t * vm,
88                               unformat_input_t * input,
89                               vlib_cli_command_t * cmd)
90 {
91   builtinurl_main_t *bmp = &builtinurl_main;
92
93   int rv;
94
95   rv = builtinurl_enable (bmp);
96
97   switch (rv)
98     {
99     case 0:
100       break;
101
102     case VNET_API_ERROR_NO_SUCH_TABLE:
103       return clib_error_return
104         (0, "http_static_server_register_builtin_handler undefined");
105       break;
106
107     default:
108       return clib_error_return (0, "builtinurl_enable returned %d", rv);
109     }
110   return 0;
111 }
112
113 /* *INDENT-OFF* */
114 VLIB_CLI_COMMAND (builtinurl_enable_command, static) =
115 {
116   .path = "builtinurl enable",
117   .short_help = "Turn on builtin http/https GET and POST urls",
118   .function = builtinurl_enable_command_fn,
119 };
120 /* *INDENT-ON* */
121
122 /* API message handler */
123 static void vl_api_builtinurl_enable_t_handler
124   (vl_api_builtinurl_enable_t * mp)
125 {
126   vl_api_builtinurl_enable_reply_t *rmp;
127   builtinurl_main_t *bmp = &builtinurl_main;
128   int rv;
129
130   rv = builtinurl_enable (bmp);
131
132   REPLY_MACRO (VL_API_BUILTINURL_ENABLE_REPLY);
133 }
134
135 /* Set up the API message handling tables */
136 static clib_error_t *
137 builtinurl_plugin_api_hookup (vlib_main_t * vm)
138 {
139   builtinurl_main_t *bmp = &builtinurl_main;
140 #define _(N,n)                                                  \
141     vl_msg_api_set_handlers((VL_API_##N + bmp->msg_id_base),     \
142                            #n,                                  \
143                            vl_api_##n##_t_handler,              \
144                            vl_noop_handler,                     \
145                            vl_api_##n##_t_endian,               \
146                            vl_api_##n##_t_print,                \
147                            sizeof(vl_api_##n##_t), 1);
148   foreach_builtinurl_plugin_api_msg;
149 #undef _
150
151   return 0;
152 }
153
154 #define vl_msg_name_crc_list
155 #include <builtinurl/builtinurl_all_api_h.h>
156 #undef vl_msg_name_crc_list
157
158 static void
159 setup_message_id_table (builtinurl_main_t * bmp, api_main_t * am)
160 {
161 #define _(id,n,crc)   vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id + bmp->msg_id_base);
162   foreach_vl_msg_name_crc_builtinurl;
163 #undef _
164 }
165
166 static clib_error_t *
167 builtinurl_init (vlib_main_t * vm)
168 {
169   builtinurl_main_t *bmp = &builtinurl_main;
170   clib_error_t *error = 0;
171   u8 *name;
172
173   bmp->vlib_main = vm;
174   bmp->vnet_main = vnet_get_main ();
175
176   name = format (0, "builtinurl_%08x%c", api_version, 0);
177
178   /* Ask for a correctly-sized block of API message decode slots */
179   bmp->msg_id_base = vl_msg_api_get_msg_ids
180     ((char *) name, VL_MSG_FIRST_AVAILABLE);
181
182   error = builtinurl_plugin_api_hookup (vm);
183
184   /* Add our API messages to the global name_crc hash table */
185   setup_message_id_table (bmp, &api_main);
186
187   vec_free (name);
188
189   return error;
190 }
191
192 VLIB_INIT_FUNCTION (builtinurl_init);
193
194 /* *INDENT-OFF* */
195 VLIB_PLUGIN_REGISTER () =
196 {
197   .version = VPP_BUILD_VER,
198   .description = "vpp built-in URL support",
199 };
200 /* *INDENT-ON* */
201
202 /*
203  * fd.io coding-style-patch-verification: ON
204  *
205  * Local Variables:
206  * eval: (c-set-style "gnu")
207  * End:
208  */