d3fa29b068dfc56dd8d8f032f10c6d85237e8f8c
[vpp.git] / src / plugins / http_static / http_static.h
1 /*
2  * Copyright (c) 2017-2022 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef __included_http_static_h__
16 #define __included_http_static_h__
17
18 #include <vnet/session/application_interface.h>
19 #include <vnet/session/session.h>
20 #include <http/http.h>
21
22 #include <vppinfra/hash.h>
23 #include <vppinfra/error.h>
24 #include <vppinfra/bihash_vec8_8.h>
25
26 /** @file http_static.h
27  * Static http server definitions
28  */
29
30 /** \brief Application session
31  */
32 typedef struct
33 {
34   CLIB_CACHE_LINE_ALIGN_MARK (cacheline0);
35   u32 session_index;
36   /** rx thread index */
37   u32 thread_index;
38   /** vpp session index, handle */
39   u32 vpp_session_index;
40   session_handle_t vpp_session_handle;
41   /** Fully-resolved file path */
42   u8 *path;
43   /** Data to send */
44   u8 *data;
45   /** Data length */
46   u32 data_len;
47   /** Current data send offset */
48   u32 data_offset;
49   /** Need to free data in detach_cache_entry */
50   int free_data;
51   /** File cache pool index */
52   u32 cache_pool_index;
53 } hss_session_t;
54
55 typedef struct hss_session_handle_
56 {
57   union
58   {
59     struct
60     {
61       u32 session_index;
62       u32 thread_index;
63     };
64     u64 as_u64;
65   };
66 } hss_session_handle_t;
67
68 STATIC_ASSERT_SIZEOF (hss_session_handle_t, sizeof (u64));
69
70 /** \brief In-memory file data cache entry
71  */
72 typedef struct
73 {
74   /** Name of the file */
75   u8 *filename;
76   /** Contents of the file, as a u8 * vector */
77   u8 *data;
78   /** Last time the cache entry was used */
79   f64 last_used;
80   /** Cache LRU links */
81   u32 next_index;
82   u32 prev_index;
83   /** Reference count, so we don't recycle while referenced */
84   int inuse;
85 } hss_cache_entry_t;
86
87 typedef struct hss_url_handler_args_
88 {
89   hss_session_handle_t sh;
90
91   union
92   {
93     /* Request args */
94     struct
95     {
96       u8 *request;
97       http_req_method_t reqtype;
98     };
99
100     /* Reply args */
101     struct
102     {
103       u8 *data;
104       uword data_len;
105       u8 free_vec_data;
106       http_status_code_t sc;
107     };
108   };
109 } hss_url_handler_args_t;
110
111 typedef enum hss_url_handler_rc_
112 {
113   HSS_URL_HANDLER_OK,
114   HSS_URL_HANDLER_ERROR,
115   HSS_URL_HANDLER_ASYNC,
116 } hss_url_handler_rc_t;
117
118 typedef hss_url_handler_rc_t (*hss_url_handler_fn) (hss_url_handler_args_t *);
119 typedef void (*hss_register_url_fn) (hss_url_handler_fn, char *, int);
120 typedef void (*hss_session_send_fn) (hss_url_handler_args_t *args);
121
122 /** \brief Main data structure
123  */
124 typedef struct
125 {
126   /** Per thread vector of session pools */
127   hss_session_t **sessions;
128   /** Session pool reader writer lock */
129   clib_spinlock_t cache_lock;
130
131   /** Unified file data cache pool */
132   hss_cache_entry_t *cache_pool;
133   /** Hash table which maps file name to file data */
134     BVT (clib_bihash) name_to_data;
135
136   /** Hash tables for built-in GET and POST handlers */
137   uword *get_url_handlers;
138   uword *post_url_handlers;
139
140   /** Current cache size */
141   u64 cache_size;
142   /** Max cache size in bytes */
143   u64 cache_limit;
144   /** Number of cache evictions */
145   u64 cache_evictions;
146
147   /** Cache LRU listheads */
148   u32 first_index;
149   u32 last_index;
150
151   /** root path to be served */
152   u8 *www_root;
153
154   /** Application index */
155   u32 app_index;
156
157   /** Cert and key pair for tls */
158   u32 ckpair_index;
159
160   /* API message ID base */
161   u16 msg_id_base;
162
163   vlib_main_t *vlib_main;
164
165   /*
166    * Config
167    */
168
169   /** Enable debug messages */
170   int debug_level;
171   /** Number of preallocated fifos, usually 0 */
172   u32 prealloc_fifos;
173   /** Private segment size, usually 0 */
174   u64 private_segment_size;
175   /** Size of the allocated rx, tx fifos, roughly 8K or so */
176   u32 fifo_size;
177   /** The bind URI, defaults to tcp://0.0.0.0/80 */
178   u8 *uri;
179   /** Threshold for switching to ptr data in http msgs */
180   u32 use_ptr_thresh;
181   /** Enable the use of builtinurls */
182   u8 enable_url_handlers;
183 } hss_main_t;
184
185 extern hss_main_t hss_main;
186
187 int hss_create (vlib_main_t *vm);
188
189 /**
190  * Register a GET or POST URL handler
191  */
192 void hss_register_url_handler (hss_url_handler_fn fp, const char *url,
193                                http_req_method_t type);
194 void hss_session_send_data (hss_url_handler_args_t *args);
195 void hss_builtinurl_json_handlers_init (void);
196
197 #endif /* __included_http_static_h__ */
198
199 /*
200  * fd.io coding-style-patch-verification: ON
201  *
202  * Local Variables:
203  * eval: (c-set-style "gnu")
204  * End:
205  */