http_static: add dynamic GET / POST method hooks
[vpp.git] / src / plugins / http_static / http_static.h
1
2 /*
3  * http_static.h - skeleton vpp engine plug-in header file
4  *
5  * Copyright (c) <current-year> <your-organization>
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #ifndef __included_http_static_h__
19 #define __included_http_static_h__
20
21 #include <vnet/vnet.h>
22 #include <vnet/session/application.h>
23 #include <vnet/session/application_interface.h>
24 #include <vnet/session/session.h>
25 #include <vnet/ip/ip.h>
26 #include <vnet/ethernet/ethernet.h>
27
28 #include <vppinfra/hash.h>
29 #include <vppinfra/error.h>
30 #include <vppinfra/time_range.h>
31 #include <vppinfra/tw_timer_2t_1w_2048sl.h>
32 #include <vppinfra/bihash_vec8_8.h>
33
34 /** @file Static http server definitions
35 */
36
37 typedef struct
38 {
39   /* API message ID base */
40   u16 msg_id_base;
41
42   /* convenience */
43   vlib_main_t *vlib_main;
44   vnet_main_t *vnet_main;
45 } http_static_main_t;
46
47 extern http_static_main_t http_static_main;
48
49 /** \brief Session States
50  */
51
52 typedef enum
53 {
54   /** Session is closed */
55   HTTP_STATE_CLOSED,
56   /** Session is established */
57   HTTP_STATE_ESTABLISHED,
58   /** Session has sent an OK response */
59   HTTP_STATE_OK_SENT,
60   /** Session has sent an HTML response */
61   HTTP_STATE_SEND_MORE_DATA,
62   /** Number of states */
63   HTTP_STATE_N_STATES,
64 } http_session_state_t;
65
66 typedef enum
67 {
68   CALLED_FROM_RX,
69   CALLED_FROM_TX,
70   CALLED_FROM_TIMER,
71 } http_state_machine_called_from_t;
72
73 typedef enum
74 {
75   HTTP_BUILTIN_METHOD_GET = 0,
76   HTTP_BUILTIN_METHOD_POST,
77 } http_builtin_method_type_t;
78
79
80 /** \brief Application session
81  */
82 typedef struct
83 {
84   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
85   /** Base class instance variables */
86 #define _(type, name) type name;
87   foreach_app_session_field
88 #undef _
89   /** rx thread index */
90   u32 thread_index;
91   /** rx buffer */
92   u8 *rx_buf;
93   /** vpp session index, handle */
94   u32 vpp_session_index;
95   u64 vpp_session_handle;
96   /** Timeout timer handle */
97   u32 timer_handle;
98   /** Fully-resolved file path */
99   u8 *path;
100   /** File data, a vector */
101   u8 *data;
102   /** Current data send offset */
103   u32 data_offset;
104   /** Need to free data in detach_cache_entry */
105   int free_data;
106
107   /** File cache pool index */
108   u32 cache_pool_index;
109   /** state machine called from... */
110   http_state_machine_called_from_t called_from;
111 } http_session_t;
112
113 /** \brief In-memory file data cache entry
114  */
115 typedef struct
116 {
117   /** Name of the file */
118   u8 *filename;
119   /** Contents of the file, as a u8 * vector */
120   u8 *data;
121   /** Last time the cache entry was used */
122   f64 last_used;
123   /** Cache LRU links */
124   u32 next_index;
125   u32 prev_index;
126   /** Reference count, so we don't recycle while referenced */
127   int inuse;
128 } file_data_cache_t;
129
130 /** \brief Main data structure
131  */
132
133 typedef struct
134 {
135   /** Per thread vector of session pools */
136   http_session_t **sessions;
137   /** Session pool reader writer lock */
138   clib_rwlock_t sessions_lock;
139   /** vpp session to http session index map */
140   u32 **session_to_http_session;
141
142   /** Enable debug messages */
143   int debug_level;
144
145   /** vpp message/event queue */
146   svm_msg_q_t **vpp_queue;
147
148   /** Unified file data cache pool */
149   file_data_cache_t *cache_pool;
150   /** Hash table which maps file name to file data */
151     BVT (clib_bihash) name_to_data;
152
153   /** Hash tables for built-in GET and POST handlers */
154   uword *get_url_handlers;
155   uword *post_url_handlers;
156
157   /** Current cache size */
158   u64 cache_size;
159   /** Max cache size in bytes */
160   u64 cache_limit;
161   /** Number of cache evictions */
162   u64 cache_evictions;
163
164   /** Cache LRU listheads */
165   u32 first_index;
166   u32 last_index;
167
168   /** root path to be served */
169   u8 *www_root;
170
171   /** Server's event queue */
172   svm_queue_t *vl_input_queue;
173
174   /** API client handle */
175   u32 my_client_index;
176
177   /** Application index */
178   u32 app_index;
179
180   /** Process node index for event scheduling */
181   u32 node_index;
182
183   /** Session cleanup timer wheel */
184   tw_timer_wheel_2t_1w_2048sl_t tw;
185   clib_spinlock_t tw_lock;
186
187   /** Time base, so we can generate browser cache control http spew */
188   clib_timebase_t timebase;
189
190   /** Number of preallocated fifos, usually 0 */
191   u32 prealloc_fifos;
192   /** Private segment size, usually 0 */
193   u32 private_segment_size;
194   /** Size of the allocated rx, tx fifos, roughly 8K or so */
195   u32 fifo_size;
196   /** The bind URI, defaults to tcp://0.0.0.0/80 */
197   u8 *uri;
198   vlib_main_t *vlib_main;
199 } http_static_server_main_t;
200
201 extern http_static_server_main_t http_static_server_main;
202
203 int http_static_server_enable_api (u32 fifo_size, u32 cache_limit,
204                                    u32 prealloc_fifos,
205                                    u32 private_segment_size,
206                                    u8 * www_root, u8 * uri);
207
208 void http_static_server_register_builtin_handler
209   (void *fp, char *url, int type);
210
211 #endif /* __included_http_static_h__ */
212
213 /*
214  * fd.io coding-style-patch-verification: ON
215  *
216  * Local Variables:
217  * eval: (c-set-style "gnu")
218  * End:
219  */