Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 1 | /* |
Florin Coras | c5df8c7 | 2019-04-08 07:42:30 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2017-2019 Cisco and/or its affiliates. |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 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 | |
| 16 | #include <vnet/vnet.h> |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 17 | #include <vnet/session/application.h> |
| 18 | #include <vnet/session/application_interface.h> |
Florin Coras | 54a51fd | 2019-02-07 15:34:52 -0800 | [diff] [blame] | 19 | #include <vnet/session/session.h> |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 20 | #include <vppinfra/tw_timer_2t_1w_2048sl.h> |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 21 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 22 | typedef enum |
| 23 | { |
| 24 | EVENT_WAKEUP = 1, |
| 25 | } http_process_event_t; |
| 26 | |
| 27 | typedef struct |
| 28 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 29 | u32 hs_index; |
| 30 | u32 thread_index; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 31 | u64 node_index; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 32 | } http_server_args; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 33 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 34 | typedef enum |
| 35 | { |
| 36 | HTTP_STATE_CLOSED, |
| 37 | HTTP_STATE_ESTABLISHED, |
| 38 | HTTP_STATE_OK_SENT, |
| 39 | } http_session_state_t; |
| 40 | typedef struct |
| 41 | { |
| 42 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
| 43 | #define _(type, name) type name; |
| 44 | foreach_app_session_field |
| 45 | #undef _ |
| 46 | u32 thread_index; |
| 47 | u8 *rx_buf; |
| 48 | u32 vpp_session_index; |
| 49 | u64 vpp_session_handle; |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 50 | u32 timer_handle; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 51 | } http_session_t; |
| 52 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 53 | typedef struct |
| 54 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 55 | http_session_t **sessions; |
| 56 | clib_rwlock_t sessions_lock; |
| 57 | u32 **session_to_http_session; |
| 58 | |
Florin Coras | 3c2fed5 | 2018-07-04 04:15:05 -0700 | [diff] [blame] | 59 | svm_msg_q_t **vpp_queue; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 60 | |
| 61 | uword *handler_by_get_request; |
| 62 | |
| 63 | u32 *free_http_cli_process_node_indices; |
| 64 | |
| 65 | /* Sever's event queue */ |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 66 | svm_queue_t *vl_input_queue; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 67 | |
| 68 | /* API client handle */ |
| 69 | u32 my_client_index; |
| 70 | |
| 71 | u32 app_index; |
| 72 | |
| 73 | /* process node index for evnt scheduling */ |
| 74 | u32 node_index; |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 75 | |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 76 | tw_timer_wheel_2t_1w_2048sl_t tw; |
| 77 | clib_spinlock_t tw_lock; |
| 78 | |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 79 | u32 prealloc_fifos; |
| 80 | u32 private_segment_size; |
| 81 | u32 fifo_size; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 82 | u8 *uri; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 83 | u32 is_static; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 84 | vlib_main_t *vlib_main; |
| 85 | } http_server_main_t; |
| 86 | |
| 87 | http_server_main_t http_server_main; |
| 88 | |
| 89 | static void |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 90 | http_server_sessions_reader_lock (void) |
| 91 | { |
| 92 | clib_rwlock_reader_lock (&http_server_main.sessions_lock); |
| 93 | } |
| 94 | |
| 95 | static void |
| 96 | http_server_sessions_reader_unlock (void) |
| 97 | { |
| 98 | clib_rwlock_reader_unlock (&http_server_main.sessions_lock); |
| 99 | } |
| 100 | |
| 101 | static void |
| 102 | http_server_sessions_writer_lock (void) |
| 103 | { |
| 104 | clib_rwlock_writer_lock (&http_server_main.sessions_lock); |
| 105 | } |
| 106 | |
| 107 | static void |
| 108 | http_server_sessions_writer_unlock (void) |
| 109 | { |
| 110 | clib_rwlock_writer_unlock (&http_server_main.sessions_lock); |
| 111 | } |
| 112 | |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 113 | static http_session_t * |
| 114 | http_server_session_alloc (u32 thread_index) |
| 115 | { |
| 116 | http_server_main_t *hsm = &http_server_main; |
| 117 | http_session_t *hs; |
| 118 | pool_get (hsm->sessions[thread_index], hs); |
| 119 | memset (hs, 0, sizeof (*hs)); |
| 120 | hs->session_index = hs - hsm->sessions[thread_index]; |
| 121 | hs->thread_index = thread_index; |
| 122 | hs->timer_handle = ~0; |
| 123 | return hs; |
| 124 | } |
| 125 | |
| 126 | static http_session_t * |
| 127 | http_server_session_get (u32 thread_index, u32 hs_index) |
| 128 | { |
| 129 | http_server_main_t *hsm = &http_server_main; |
| 130 | if (pool_is_free_index (hsm->sessions[thread_index], hs_index)) |
| 131 | return 0; |
| 132 | return pool_elt_at_index (hsm->sessions[thread_index], hs_index); |
| 133 | } |
| 134 | |
| 135 | static void |
| 136 | http_server_session_free (http_session_t * hs) |
| 137 | { |
| 138 | http_server_main_t *hsm = &http_server_main; |
| 139 | pool_put (hsm->sessions[hs->thread_index], hs); |
| 140 | if (CLIB_DEBUG) |
| 141 | memset (hs, 0xfa, sizeof (*hs)); |
| 142 | } |
| 143 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 144 | static void |
| 145 | http_server_session_lookup_add (u32 thread_index, u32 s_index, u32 hs_index) |
| 146 | { |
| 147 | http_server_main_t *hsm = &http_server_main; |
| 148 | vec_validate (hsm->session_to_http_session[thread_index], s_index); |
| 149 | hsm->session_to_http_session[thread_index][s_index] = hs_index; |
| 150 | } |
| 151 | |
| 152 | static void |
| 153 | http_server_session_lookup_del (u32 thread_index, u32 s_index) |
| 154 | { |
| 155 | http_server_main_t *hsm = &http_server_main; |
| 156 | hsm->session_to_http_session[thread_index][s_index] = ~0; |
| 157 | } |
| 158 | |
| 159 | static http_session_t * |
| 160 | http_server_session_lookup (u32 thread_index, u32 s_index) |
| 161 | { |
| 162 | http_server_main_t *hsm = &http_server_main; |
| 163 | u32 hs_index; |
| 164 | |
| 165 | if (s_index < vec_len (hsm->session_to_http_session[thread_index])) |
| 166 | { |
| 167 | hs_index = hsm->session_to_http_session[thread_index][s_index]; |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 168 | return http_server_session_get (thread_index, hs_index); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 173 | |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 174 | static void |
| 175 | http_server_session_timer_start (http_session_t * hs) |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 176 | { |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 177 | u32 hs_handle; |
| 178 | hs_handle = hs->thread_index << 24 | hs->session_index; |
| 179 | clib_spinlock_lock (&http_server_main.tw_lock); |
| 180 | hs->timer_handle = tw_timer_start_2t_1w_2048sl (&http_server_main.tw, |
| 181 | hs_handle, 0, 60); |
| 182 | clib_spinlock_unlock (&http_server_main.tw_lock); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static void |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 186 | http_server_session_timer_stop (http_session_t * hs) |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 187 | { |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 188 | if (hs->timer_handle == ~0) |
| 189 | return; |
| 190 | clib_spinlock_lock (&http_server_main.tw_lock); |
| 191 | tw_timer_stop_2t_1w_2048sl (&http_server_main.tw, hs->timer_handle); |
| 192 | clib_spinlock_unlock (&http_server_main.tw_lock); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | static void |
| 196 | http_server_session_cleanup (http_session_t * hs) |
| 197 | { |
| 198 | if (!hs) |
| 199 | return; |
| 200 | http_server_session_lookup_del (hs->thread_index, hs->vpp_session_index); |
| 201 | vec_free (hs->rx_buf); |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 202 | http_server_session_timer_stop (hs); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 203 | http_server_session_free (hs); |
| 204 | } |
| 205 | |
| 206 | static void |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 207 | http_server_session_disconnect (http_session_t * hs) |
| 208 | { |
| 209 | vnet_disconnect_args_t _a = { 0 }, *a = &_a; |
| 210 | a->handle = hs->vpp_session_handle; |
| 211 | a->app_index = http_server_main.app_index; |
| 212 | vnet_disconnect_session (a); |
| 213 | } |
| 214 | |
| 215 | static void |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 216 | http_process_free (http_server_args * args) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 217 | { |
| 218 | vlib_node_runtime_t *rt; |
| 219 | vlib_main_t *vm = &vlib_global_main; |
| 220 | http_server_main_t *hsm = &http_server_main; |
| 221 | vlib_node_t *n; |
| 222 | u32 node_index; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 223 | http_server_args **save_args; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 224 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 225 | node_index = args->node_index; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 226 | ASSERT (node_index != 0); |
| 227 | |
| 228 | n = vlib_get_node (vm, node_index); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 229 | rt = vlib_node_get_runtime (vm, n->index); |
| 230 | save_args = vlib_node_get_runtime_data (vm, n->index); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 231 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 232 | /* Reset process session pointer */ |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 233 | clib_mem_free (*save_args); |
| 234 | *save_args = 0; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 235 | |
| 236 | /* Turn off the process node */ |
| 237 | vlib_node_set_state (vm, rt->node_index, VLIB_NODE_STATE_DISABLED); |
| 238 | |
| 239 | /* add node index to the freelist */ |
| 240 | vec_add1 (hsm->free_http_cli_process_node_indices, node_index); |
| 241 | } |
| 242 | |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 243 | /* *INDENT-OFF* */ |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 244 | static const char *http_ok = |
| 245 | "HTTP/1.1 200 OK\r\n"; |
| 246 | |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 247 | static const char *http_response = |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 248 | "Content-Type: text/html\r\n" |
| 249 | "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n" |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 250 | "Connection: close \r\n" |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 251 | "Pragma: no-cache\r\n" |
| 252 | "Content-Length: %d\r\n\r\n%s"; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 253 | |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 254 | static const char *http_error_template = |
| 255 | "HTTP/1.1 %s\r\n" |
| 256 | "Content-Type: text/html\r\n" |
| 257 | "Expires: Mon, 11 Jan 1970 10:10:10 GMT\r\n" |
| 258 | "Connection: close\r\n" |
| 259 | "Pragma: no-cache\r\n" |
| 260 | "Content-Length: 0\r\n\r\n"; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 261 | |
| 262 | /* Header, including incantation to suppress favicon.ico requests */ |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 263 | static const char *html_header_template = |
| 264 | "<html><head><title>%v</title></head>" |
| 265 | "<link rel=\"icon\" href=\"data:,\">" |
| 266 | "<body><pre>"; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 267 | |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 268 | static const char *html_footer = |
| 269 | "</pre></body></html>\r\n"; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 270 | |
Florin Coras | 898cd8f | 2018-06-08 02:02:13 -0700 | [diff] [blame] | 271 | static const char *html_header_static = |
| 272 | "<html><head><title>static reply</title></head>" |
| 273 | "<link rel=\"icon\" href=\"data:,\">" |
| 274 | "<body><pre>hello</pre></body></html>\r\n"; |
| 275 | /* *INDENT-ON* */ |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 276 | |
| 277 | static u8 *static_http; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 278 | static u8 *static_ok; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 279 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 280 | static void |
| 281 | http_cli_output (uword arg, u8 * buffer, uword buffer_bytes) |
| 282 | { |
| 283 | u8 **output_vecp = (u8 **) arg; |
| 284 | u8 *output_vec; |
| 285 | u32 offset; |
| 286 | |
| 287 | output_vec = *output_vecp; |
| 288 | |
| 289 | offset = vec_len (output_vec); |
| 290 | vec_validate (output_vec, offset + buffer_bytes - 1); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 291 | clib_memcpy_fast (output_vec + offset, buffer, buffer_bytes); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 292 | |
| 293 | *output_vecp = output_vec; |
| 294 | } |
| 295 | |
| 296 | void |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 297 | send_data (http_session_t * hs, u8 * data) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 298 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 299 | http_server_main_t *hsm = &http_server_main; |
| 300 | vnet_disconnect_args_t _a = { 0 }, *a = &_a; |
| 301 | vlib_main_t *vm = vlib_get_main (); |
| 302 | f64 last_sent_timer = vlib_time_now (vm); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 303 | u32 offset, bytes_to_send; |
| 304 | f64 delay = 10e-3; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 305 | |
| 306 | bytes_to_send = vec_len (data); |
| 307 | offset = 0; |
| 308 | |
| 309 | while (bytes_to_send > 0) |
| 310 | { |
| 311 | int actual_transfer; |
| 312 | |
| 313 | actual_transfer = svm_fifo_enqueue_nowait |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 314 | (hs->tx_fifo, bytes_to_send, data + offset); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 315 | |
| 316 | /* Made any progress? */ |
| 317 | if (actual_transfer <= 0) |
| 318 | { |
Dave Barach | 26dc58b | 2019-03-29 14:08:45 -0400 | [diff] [blame] | 319 | http_server_sessions_reader_unlock (); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 320 | vlib_process_suspend (vm, delay); |
Dave Barach | 26dc58b | 2019-03-29 14:08:45 -0400 | [diff] [blame] | 321 | http_server_sessions_reader_lock (); |
| 322 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 323 | /* 10s deadman timer */ |
| 324 | if (vlib_time_now (vm) > last_sent_timer + 10.0) |
| 325 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 326 | a->handle = hs->vpp_session_handle; |
| 327 | a->app_index = hsm->app_index; |
| 328 | vnet_disconnect_session (a); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 329 | break; |
| 330 | } |
| 331 | /* Exponential backoff, within reason */ |
| 332 | if (delay < 1.0) |
| 333 | delay = delay * 2.0; |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | last_sent_timer = vlib_time_now (vm); |
| 338 | offset += actual_transfer; |
| 339 | bytes_to_send -= actual_transfer; |
| 340 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 341 | if (svm_fifo_set_event (hs->tx_fifo)) |
| 342 | session_send_io_evt_to_thread (hs->tx_fifo, |
| 343 | SESSION_IO_EVT_TX_FLUSH); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 344 | delay = 10e-3; |
| 345 | } |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | static void |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 350 | send_error (http_session_t * hs, char *str) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 351 | { |
| 352 | u8 *data; |
| 353 | |
| 354 | data = format (0, http_error_template, str); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 355 | send_data (hs, data); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 356 | vec_free (data); |
| 357 | } |
| 358 | |
| 359 | static uword |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 360 | http_cli_process (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 361 | vlib_frame_t * f) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 362 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 363 | u8 *request = 0, *reply = 0, *http = 0, *html = 0; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 364 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 365 | http_server_args **save_args; |
| 366 | http_server_args *args; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 367 | unformat_input_t input; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 368 | http_session_t *hs; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 369 | int i; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 370 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 371 | save_args = vlib_node_get_runtime_data (hsm->vlib_main, rt->node_index); |
| 372 | args = *save_args; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 373 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 374 | http_server_sessions_reader_lock (); |
| 375 | |
| 376 | hs = http_server_session_get (args->thread_index, args->hs_index); |
| 377 | ASSERT (hs); |
| 378 | |
| 379 | request = hs->rx_buf; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 380 | if (vec_len (request) < 7) |
| 381 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 382 | send_error (hs, "400 Bad Request"); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 383 | goto out; |
| 384 | } |
| 385 | |
| 386 | for (i = 0; i < vec_len (request) - 4; i++) |
| 387 | { |
| 388 | if (request[i] == 'G' && |
| 389 | request[i + 1] == 'E' && |
| 390 | request[i + 2] == 'T' && request[i + 3] == ' ') |
| 391 | goto found; |
| 392 | } |
| 393 | bad_request: |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 394 | send_error (hs, "400 Bad Request"); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 395 | goto out; |
| 396 | |
| 397 | found: |
| 398 | /* Lose "GET " */ |
| 399 | vec_delete (request, i + 5, 0); |
| 400 | |
| 401 | /* Replace slashes with spaces, stop at the end of the path */ |
| 402 | i = 0; |
| 403 | while (1) |
| 404 | { |
| 405 | if (request[i] == '/') |
| 406 | request[i] = ' '; |
| 407 | else if (request[i] == ' ') |
| 408 | { |
| 409 | /* vlib_cli_input is vector-based, no need for a NULL */ |
| 410 | _vec_len (request) = i; |
| 411 | break; |
| 412 | } |
| 413 | i++; |
| 414 | /* Should never happen */ |
| 415 | if (i == vec_len (request)) |
| 416 | goto bad_request; |
| 417 | } |
| 418 | |
| 419 | /* Generate the html header */ |
| 420 | html = format (0, html_header_template, request /* title */ ); |
| 421 | |
| 422 | /* Run the command */ |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 423 | unformat_init_vector (&input, vec_dup (request)); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 424 | vlib_cli_input (vm, &input, http_cli_output, (uword) & reply); |
| 425 | unformat_free (&input); |
| 426 | request = 0; |
| 427 | |
| 428 | /* Generate the html page */ |
| 429 | html = format (html, "%v", reply); |
| 430 | html = format (html, html_footer); |
| 431 | /* And the http reply */ |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 432 | http = format (0, http_ok, vec_len (http_ok)); |
| 433 | http = format (http, http_response, vec_len (html), html); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 434 | |
| 435 | /* Send it */ |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 436 | send_data (hs, http); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 437 | |
| 438 | out: |
| 439 | /* Cleanup */ |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 440 | http_server_sessions_reader_unlock (); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 441 | vec_free (reply); |
| 442 | vec_free (html); |
| 443 | vec_free (http); |
| 444 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 445 | http_process_free (args); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 446 | return (0); |
| 447 | } |
| 448 | |
| 449 | static void |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 450 | alloc_http_process (http_server_args * args) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 451 | { |
| 452 | char *name; |
| 453 | vlib_node_t *n; |
| 454 | http_server_main_t *hsm = &http_server_main; |
| 455 | vlib_main_t *vm = hsm->vlib_main; |
| 456 | uword l = vec_len (hsm->free_http_cli_process_node_indices); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 457 | http_server_args **save_args; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 458 | |
| 459 | if (vec_len (hsm->free_http_cli_process_node_indices) > 0) |
| 460 | { |
| 461 | n = vlib_get_node (vm, hsm->free_http_cli_process_node_indices[l - 1]); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 462 | vlib_node_set_state (vm, n->index, VLIB_NODE_STATE_POLLING); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 463 | _vec_len (hsm->free_http_cli_process_node_indices) = l - 1; |
| 464 | } |
| 465 | else |
| 466 | { |
| 467 | static vlib_node_registration_t r = { |
| 468 | .function = http_cli_process, |
| 469 | .type = VLIB_NODE_TYPE_PROCESS, |
| 470 | .process_log2_n_stack_bytes = 16, |
| 471 | .runtime_data_bytes = sizeof (void *), |
| 472 | }; |
| 473 | |
| 474 | name = (char *) format (0, "http-cli-%d", l); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 475 | r.name = name; |
| 476 | vlib_register_node (vm, &r); |
| 477 | vec_free (name); |
| 478 | |
| 479 | n = vlib_get_node (vm, r.index); |
| 480 | } |
| 481 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 482 | /* Save the node index in the args. It won't be zero. */ |
| 483 | args->node_index = n->index; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 484 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 485 | /* Save the args (pointer) in the node runtime */ |
| 486 | save_args = vlib_node_get_runtime_data (vm, n->index); |
Florin Coras | a154017 | 2019-04-04 08:27:58 -0700 | [diff] [blame] | 487 | *save_args = clib_mem_alloc (sizeof (*args)); |
| 488 | clib_memcpy_fast (*save_args, args, sizeof (*args)); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 489 | |
| 490 | vlib_start_process (vm, n->runtime_index); |
| 491 | } |
| 492 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 493 | static void |
| 494 | alloc_http_process_callback (void *cb_args) |
| 495 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 496 | alloc_http_process ((http_server_args *) cb_args); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | static int |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 500 | session_rx_request (http_session_t * hs) |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 501 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 502 | u32 max_dequeue, cursize; |
| 503 | int n_read; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 504 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 505 | cursize = vec_len (hs->rx_buf); |
| 506 | max_dequeue = svm_fifo_max_dequeue (hs->rx_fifo); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 507 | if (PREDICT_FALSE (max_dequeue == 0)) |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 508 | return -1; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 509 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 510 | vec_validate (hs->rx_buf, cursize + max_dequeue - 1); |
| 511 | n_read = app_recv_stream_raw (hs->rx_fifo, hs->rx_buf + cursize, |
| 512 | max_dequeue, 0, 0 /* peek */ ); |
| 513 | ASSERT (n_read == max_dequeue); |
| 514 | if (svm_fifo_is_empty (hs->rx_fifo)) |
| 515 | svm_fifo_unset_event (hs->rx_fifo); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 516 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 517 | _vec_len (hs->rx_buf) = cursize + n_read; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 518 | return 0; |
| 519 | } |
| 520 | |
| 521 | static int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 522 | http_server_rx_callback (session_t * s) |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 523 | { |
Florin Coras | a154017 | 2019-04-04 08:27:58 -0700 | [diff] [blame] | 524 | http_server_args args; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 525 | http_session_t *hs; |
JingLiuZTE | 7cafe76 | 2017-11-08 15:01:27 +0800 | [diff] [blame] | 526 | int rv; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 527 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 528 | http_server_sessions_reader_lock (); |
| 529 | |
| 530 | hs = http_server_session_lookup (s->thread_index, s->session_index); |
| 531 | if (!hs || hs->session_state != HTTP_STATE_ESTABLISHED) |
| 532 | return -1; |
| 533 | |
| 534 | rv = session_rx_request (hs); |
JingLiuZTE | 7cafe76 | 2017-11-08 15:01:27 +0800 | [diff] [blame] | 535 | if (rv) |
| 536 | return rv; |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 537 | |
| 538 | /* send the command to a new/recycled vlib process */ |
Florin Coras | a154017 | 2019-04-04 08:27:58 -0700 | [diff] [blame] | 539 | args.hs_index = hs->session_index; |
| 540 | args.thread_index = hs->thread_index; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 541 | |
| 542 | http_server_sessions_reader_unlock (); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 543 | |
Florin Coras | a154017 | 2019-04-04 08:27:58 -0700 | [diff] [blame] | 544 | /* Send RPC request to main thread */ |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 545 | if (vlib_get_thread_index () != 0) |
Florin Coras | a154017 | 2019-04-04 08:27:58 -0700 | [diff] [blame] | 546 | vlib_rpc_call_main_thread (alloc_http_process_callback, (u8 *) & args, |
| 547 | sizeof (args)); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 548 | else |
Florin Coras | a154017 | 2019-04-04 08:27:58 -0700 | [diff] [blame] | 549 | alloc_http_process (&args); |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 553 | static int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 554 | http_server_rx_callback_static (session_t * s) |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 555 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 556 | http_session_t *hs; |
| 557 | u32 request_len; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 558 | u8 *request = 0; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 559 | int i, rv; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 560 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 561 | hs = http_server_session_lookup (s->thread_index, s->session_index); |
| 562 | if (!hs || hs->session_state == HTTP_STATE_CLOSED) |
| 563 | return 0; |
| 564 | |
| 565 | /* ok 200 was sent */ |
| 566 | if (hs->session_state == HTTP_STATE_OK_SENT) |
| 567 | goto send_data; |
| 568 | |
| 569 | rv = session_rx_request (hs); |
JingLiuZTE | 7cafe76 | 2017-11-08 15:01:27 +0800 | [diff] [blame] | 570 | if (rv) |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 571 | goto wait_for_data; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 572 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 573 | request = hs->rx_buf; |
| 574 | request_len = vec_len (request); |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 575 | if (vec_len (request) < 7) |
| 576 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 577 | send_error (hs, "400 Bad Request"); |
| 578 | goto close_session; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 581 | for (i = 0; i < request_len - 4; i++) |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 582 | { |
| 583 | if (request[i] == 'G' && |
| 584 | request[i + 1] == 'E' && |
| 585 | request[i + 2] == 'T' && request[i + 3] == ' ') |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 586 | goto find_end; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 587 | } |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 588 | send_error (hs, "400 Bad Request"); |
| 589 | goto close_session; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 590 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 591 | find_end: |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 592 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 593 | /* check for the end sequence: /r/n/r/n */ |
| 594 | if (request[request_len - 1] != 0xa || request[request_len - 3] != 0xa |
| 595 | || request[request_len - 2] != 0xd || request[request_len - 4] != 0xd) |
| 596 | goto wait_for_data; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 597 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 598 | /* send 200 OK first */ |
| 599 | send_data (hs, static_ok); |
| 600 | hs->session_state = HTTP_STATE_OK_SENT; |
| 601 | goto postpone; |
| 602 | |
| 603 | send_data: |
| 604 | send_data (hs, static_http); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 605 | |
| 606 | close_session: |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 607 | http_server_session_disconnect (hs); |
| 608 | http_server_session_cleanup (hs); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 609 | return 0; |
| 610 | |
| 611 | postpone: |
Florin Coras | 78b5fa6 | 2019-02-21 20:04:15 -0800 | [diff] [blame] | 612 | (void) svm_fifo_set_event (hs->rx_fifo); |
Florin Coras | f6c4313 | 2019-03-01 12:41:21 -0800 | [diff] [blame] | 613 | session_send_io_evt_to_thread (hs->rx_fifo, SESSION_IO_EVT_BUILTIN_RX); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 614 | return 0; |
| 615 | |
| 616 | wait_for_data: |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 617 | return 0; |
| 618 | } |
| 619 | |
| 620 | static int |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 621 | http_server_session_accept_callback (session_t * s) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 622 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 623 | http_server_main_t *hsm = &http_server_main; |
| 624 | http_session_t *hs; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 625 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 626 | hsm->vpp_queue[s->thread_index] = |
Florin Coras | 31c9955 | 2019-03-01 13:00:58 -0800 | [diff] [blame] | 627 | session_main_get_vpp_event_queue (s->thread_index); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 628 | |
| 629 | if (!hsm->is_static) |
| 630 | http_server_sessions_writer_lock (); |
| 631 | |
| 632 | hs = http_server_session_alloc (s->thread_index); |
| 633 | http_server_session_lookup_add (s->thread_index, s->session_index, |
| 634 | hs->session_index); |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 635 | hs->rx_fifo = s->rx_fifo; |
| 636 | hs->tx_fifo = s->tx_fifo; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 637 | hs->vpp_session_index = s->session_index; |
| 638 | hs->vpp_session_handle = session_handle (s); |
| 639 | hs->session_state = HTTP_STATE_ESTABLISHED; |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 640 | http_server_session_timer_start (hs); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 641 | |
| 642 | if (!hsm->is_static) |
| 643 | http_server_sessions_writer_unlock (); |
| 644 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 645 | s->session_state = SESSION_STATE_READY; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | static void |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 650 | http_server_session_disconnect_callback (session_t * s) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 651 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 652 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | 4af830c | 2018-12-04 09:21:36 -0800 | [diff] [blame] | 653 | vnet_disconnect_args_t _a = { 0 }, *a = &_a; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 654 | http_session_t *hs; |
| 655 | |
| 656 | if (!hsm->is_static) |
| 657 | http_server_sessions_writer_lock (); |
| 658 | |
| 659 | hs = http_server_session_lookup (s->thread_index, s->session_index); |
| 660 | http_server_session_cleanup (hs); |
| 661 | |
| 662 | if (!hsm->is_static) |
| 663 | http_server_sessions_writer_unlock (); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 664 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 665 | a->handle = session_handle (s); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 666 | a->app_index = hsm->app_index; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 667 | vnet_disconnect_session (a); |
| 668 | } |
| 669 | |
| 670 | static void |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 671 | http_server_session_reset_callback (session_t * s) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 672 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 673 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | 4af830c | 2018-12-04 09:21:36 -0800 | [diff] [blame] | 674 | vnet_disconnect_args_t _a = { 0 }, *a = &_a; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 675 | http_session_t *hs; |
| 676 | |
| 677 | if (!hsm->is_static) |
| 678 | http_server_sessions_writer_lock (); |
| 679 | |
| 680 | hs = http_server_session_lookup (s->thread_index, s->session_index); |
| 681 | http_server_session_cleanup (hs); |
| 682 | |
| 683 | if (!hsm->is_static) |
| 684 | http_server_sessions_writer_unlock (); |
| 685 | |
Florin Coras | 4af830c | 2018-12-04 09:21:36 -0800 | [diff] [blame] | 686 | a->handle = session_handle (s); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 687 | a->app_index = hsm->app_index; |
Florin Coras | 4af830c | 2018-12-04 09:21:36 -0800 | [diff] [blame] | 688 | vnet_disconnect_session (a); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 689 | } |
| 690 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 691 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 692 | http_server_session_connected_callback (u32 app_index, u32 api_context, |
Florin Coras | 288eaab | 2019-02-03 15:26:14 -0800 | [diff] [blame] | 693 | session_t * s, u8 is_fail) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 694 | { |
| 695 | clib_warning ("called..."); |
| 696 | return -1; |
| 697 | } |
| 698 | |
| 699 | static int |
Florin Coras | fa76a76 | 2018-11-29 12:40:10 -0800 | [diff] [blame] | 700 | http_server_add_segment_callback (u32 client_index, u64 segment_handle) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 701 | { |
| 702 | clib_warning ("called..."); |
| 703 | return -1; |
| 704 | } |
| 705 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 706 | static session_cb_vft_t http_server_session_cb_vft = { |
| 707 | .session_accept_callback = http_server_session_accept_callback, |
| 708 | .session_disconnect_callback = http_server_session_disconnect_callback, |
| 709 | .session_connected_callback = http_server_session_connected_callback, |
| 710 | .add_segment_callback = http_server_add_segment_callback, |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 711 | .builtin_app_rx_callback = http_server_rx_callback, |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 712 | .session_reset_callback = http_server_session_reset_callback |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 713 | }; |
| 714 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 715 | static int |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 716 | http_server_attach () |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 717 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 718 | vnet_app_add_tls_cert_args_t _a_cert, *a_cert = &_a_cert; |
| 719 | vnet_app_add_tls_key_args_t _a_key, *a_key = &_a_key; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 720 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 721 | u64 options[APP_OPTIONS_N_OPTIONS]; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 722 | vnet_app_attach_args_t _a, *a = &_a; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 723 | u32 segment_size = 128 << 20; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 724 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 725 | clib_memset (a, 0, sizeof (*a)); |
| 726 | clib_memset (options, 0, sizeof (options)); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 727 | |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 728 | if (hsm->private_segment_size) |
| 729 | segment_size = hsm->private_segment_size; |
| 730 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 731 | a->api_client_index = ~0; |
| 732 | a->name = format (0, "test_http_server"); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 733 | a->session_cb_vft = &http_server_session_cb_vft; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 734 | a->options = options; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 735 | a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size; |
| 736 | a->options[APP_OPTIONS_RX_FIFO_SIZE] = |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 737 | hsm->fifo_size ? hsm->fifo_size : 8 << 10; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 738 | a->options[APP_OPTIONS_TX_FIFO_SIZE] = |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 739 | hsm->fifo_size ? hsm->fifo_size : 32 << 10; |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 740 | a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 741 | a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = hsm->prealloc_fifos; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 742 | |
| 743 | if (vnet_application_attach (a)) |
| 744 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 745 | vec_free (a->name); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 746 | clib_warning ("failed to attach server"); |
| 747 | return -1; |
| 748 | } |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 749 | vec_free (a->name); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 750 | hsm->app_index = a->app_index; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 751 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 752 | clib_memset (a_cert, 0, sizeof (*a_cert)); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 753 | a_cert->app_index = a->app_index; |
| 754 | vec_validate (a_cert->cert, test_srv_crt_rsa_len); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 755 | clib_memcpy_fast (a_cert->cert, test_srv_crt_rsa, test_srv_crt_rsa_len); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 756 | vnet_app_add_tls_cert (a_cert); |
| 757 | |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 758 | clib_memset (a_key, 0, sizeof (*a_key)); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 759 | a_key->app_index = a->app_index; |
| 760 | vec_validate (a_key->key, test_srv_key_rsa_len); |
Dave Barach | 178cf49 | 2018-11-13 16:34:13 -0500 | [diff] [blame] | 761 | clib_memcpy_fast (a_key->key, test_srv_key_rsa, test_srv_key_rsa_len); |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 762 | vnet_app_add_tls_key (a_key); |
| 763 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 764 | return 0; |
| 765 | } |
| 766 | |
| 767 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 768 | http_server_listen () |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 769 | { |
| 770 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | c9940fc | 2019-02-05 20:55:11 -0800 | [diff] [blame] | 771 | vnet_listen_args_t _a, *a = &_a; |
Dave Barach | b7b9299 | 2018-10-17 10:38:51 -0400 | [diff] [blame] | 772 | clib_memset (a, 0, sizeof (*a)); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 773 | a->app_index = hsm->app_index; |
| 774 | a->uri = "tcp://0.0.0.0/80"; |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 775 | if (hsm->uri) |
| 776 | a->uri = (char *) hsm->uri; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 777 | return vnet_bind_uri (a); |
| 778 | } |
| 779 | |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 780 | static void |
| 781 | http_server_session_cleanup_cb (void *hs_handlep) |
| 782 | { |
| 783 | http_session_t *hs; |
| 784 | uword hs_handle; |
| 785 | hs_handle = pointer_to_uword (hs_handlep); |
| 786 | hs = http_server_session_get (hs_handle >> 24, hs_handle & 0x00FFFFFF); |
| 787 | if (!hs) |
| 788 | return; |
| 789 | hs->timer_handle = ~0; |
| 790 | http_server_session_disconnect (hs); |
| 791 | http_server_session_cleanup (hs); |
| 792 | } |
| 793 | |
| 794 | static void |
| 795 | http_expired_timers_dispatch (u32 * expired_timers) |
| 796 | { |
| 797 | u32 hs_handle; |
| 798 | int i; |
| 799 | |
| 800 | for (i = 0; i < vec_len (expired_timers); i++) |
| 801 | { |
| 802 | /* Get session handle. The first bit is the timer id */ |
| 803 | hs_handle = expired_timers[i] & 0x7FFFFFFF; |
| 804 | session_send_rpc_evt_to_thread (hs_handle >> 24, |
| 805 | http_server_session_cleanup_cb, |
| 806 | uword_to_pointer (hs_handle, void *)); |
| 807 | } |
| 808 | } |
| 809 | |
| 810 | static uword |
| 811 | http_server_process (vlib_main_t * vm, vlib_node_runtime_t * rt, |
| 812 | vlib_frame_t * f) |
| 813 | { |
| 814 | http_server_main_t *hsm = &http_server_main; |
| 815 | f64 now, timeout = 1.0; |
| 816 | uword *event_data = 0; |
| 817 | uword __clib_unused event_type; |
| 818 | |
| 819 | while (1) |
| 820 | { |
| 821 | vlib_process_wait_for_event_or_clock (vm, timeout); |
| 822 | now = vlib_time_now (vm); |
| 823 | event_type = vlib_process_get_events (vm, (uword **) & event_data); |
| 824 | |
| 825 | /* expire timers */ |
| 826 | clib_spinlock_lock (&http_server_main.tw_lock); |
| 827 | tw_timer_expire_timers_2t_1w_2048sl (&hsm->tw, now); |
| 828 | clib_spinlock_unlock (&http_server_main.tw_lock); |
| 829 | |
| 830 | vec_reset_length (event_data); |
| 831 | } |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | /* *INDENT-OFF* */ |
| 836 | VLIB_REGISTER_NODE (http_server_process_node) = |
| 837 | { |
| 838 | .function = http_server_process, |
| 839 | .type = VLIB_NODE_TYPE_PROCESS, |
| 840 | .name = "http-server-process", |
| 841 | .state = VLIB_NODE_STATE_DISABLED, |
| 842 | }; |
| 843 | /* *INDENT-ON* */ |
| 844 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 845 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 846 | http_server_create (vlib_main_t * vm) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 847 | { |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 848 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 849 | http_server_main_t *hsm = &http_server_main; |
| 850 | u32 num_threads; |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 851 | vlib_node_t *n; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 852 | |
| 853 | num_threads = 1 /* main thread */ + vtm->n_threads; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 854 | vec_validate (hsm->vpp_queue, num_threads - 1); |
| 855 | vec_validate (hsm->sessions, num_threads - 1); |
| 856 | vec_validate (hsm->session_to_http_session, num_threads - 1); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 857 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 858 | clib_rwlock_init (&hsm->sessions_lock); |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 859 | clib_spinlock_init (&hsm->tw_lock); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 860 | |
| 861 | if (http_server_attach ()) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 862 | { |
| 863 | clib_warning ("failed to attach server"); |
| 864 | return -1; |
| 865 | } |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 866 | if (http_server_listen ()) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 867 | { |
| 868 | clib_warning ("failed to start listening"); |
| 869 | return -1; |
| 870 | } |
Florin Coras | dc43bcd | 2019-04-05 18:23:08 -0700 | [diff] [blame] | 871 | |
| 872 | /* Init timer wheel and process */ |
| 873 | tw_timer_wheel_init_2t_1w_2048sl (&hsm->tw, http_expired_timers_dispatch, |
| 874 | 1 /* timer interval */ , ~0); |
| 875 | vlib_node_set_state (vm, http_server_process_node.index, |
| 876 | VLIB_NODE_STATE_POLLING); |
| 877 | n = vlib_get_node (vm, http_server_process_node.index); |
| 878 | vlib_start_process (vm, n->runtime_index); |
| 879 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 880 | return 0; |
| 881 | } |
| 882 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 883 | static clib_error_t * |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 884 | http_server_create_command_fn (vlib_main_t * vm, |
| 885 | unformat_input_t * input, |
| 886 | vlib_cli_command_t * cmd) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 887 | { |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 888 | http_server_main_t *hsm = &http_server_main; |
Dave Barach | 26dc58b | 2019-03-29 14:08:45 -0400 | [diff] [blame] | 889 | unformat_input_t _line_input, *line_input = &_line_input; |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 890 | u64 seg_size; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 891 | u8 *html; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 892 | int rv; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 893 | |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 894 | hsm->prealloc_fifos = 0; |
| 895 | hsm->private_segment_size = 0; |
| 896 | hsm->fifo_size = 0; |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 897 | hsm->is_static = 0; |
Dave Barach | 26dc58b | 2019-03-29 14:08:45 -0400 | [diff] [blame] | 898 | |
| 899 | /* Get a line of input. */ |
| 900 | if (!unformat_user (input, unformat_line_input, line_input)) |
| 901 | goto start_server; |
| 902 | |
| 903 | while (unformat_check_input (line_input) != UNFORMAT_END_OF_INPUT) |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 904 | { |
Dave Barach | 26dc58b | 2019-03-29 14:08:45 -0400 | [diff] [blame] | 905 | if (unformat (line_input, "static")) |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 906 | hsm->is_static = 1; |
Dave Barach | 26dc58b | 2019-03-29 14:08:45 -0400 | [diff] [blame] | 907 | else |
| 908 | if (unformat (line_input, "prealloc-fifos %d", &hsm->prealloc_fifos)) |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 909 | ; |
Dave Barach | 26dc58b | 2019-03-29 14:08:45 -0400 | [diff] [blame] | 910 | else if (unformat (line_input, "private-segment-size %U", |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 911 | unformat_memory_size, &seg_size)) |
| 912 | { |
| 913 | if (seg_size >= 0x100000000ULL) |
| 914 | { |
| 915 | vlib_cli_output (vm, "private segment size %llu, too large", |
| 916 | seg_size); |
| 917 | return 0; |
| 918 | } |
| 919 | hsm->private_segment_size = seg_size; |
| 920 | } |
Dave Barach | 26dc58b | 2019-03-29 14:08:45 -0400 | [diff] [blame] | 921 | else if (unformat (line_input, "fifo-size %d", &hsm->fifo_size)) |
Florin Coras | 1d6d085 | 2017-11-17 14:26:01 -0800 | [diff] [blame] | 922 | hsm->fifo_size <<= 10; |
Dave Barach | 26dc58b | 2019-03-29 14:08:45 -0400 | [diff] [blame] | 923 | else if (unformat (line_input, "uri %s", &hsm->uri)) |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 924 | ; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 925 | else |
| 926 | return clib_error_return (0, "unknown input `%U'", |
Dave Barach | 26dc58b | 2019-03-29 14:08:45 -0400 | [diff] [blame] | 927 | format_unformat_error, line_input); |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 928 | } |
Dave Barach | 26dc58b | 2019-03-29 14:08:45 -0400 | [diff] [blame] | 929 | unformat_free (line_input); |
| 930 | |
| 931 | start_server: |
| 932 | |
Florin Coras | 0e9c33b | 2017-08-14 22:33:41 -0700 | [diff] [blame] | 933 | if (hsm->my_client_index != (u32) ~ 0) |
| 934 | return clib_error_return (0, "test http server is already running"); |
| 935 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 936 | vnet_session_enable_disable (vm, 1 /* turn on TCP, etc. */ ); |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 937 | |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 938 | if (hsm->is_static) |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 939 | { |
Florin Coras | 371ca50 | 2018-02-21 12:07:41 -0800 | [diff] [blame] | 940 | http_server_session_cb_vft.builtin_app_rx_callback = |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 941 | http_server_rx_callback_static; |
| 942 | html = format (0, html_header_static); |
| 943 | static_http = format (0, http_response, vec_len (html), html); |
Florin Coras | 844a36d | 2018-12-20 09:50:50 -0800 | [diff] [blame] | 944 | static_ok = format (0, http_ok); |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 945 | } |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 946 | rv = http_server_create (vm); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 947 | switch (rv) |
| 948 | { |
| 949 | case 0: |
| 950 | break; |
| 951 | default: |
| 952 | return clib_error_return (0, "server_create returned %d", rv); |
| 953 | } |
| 954 | return 0; |
| 955 | } |
| 956 | |
| 957 | /* *INDENT-OFF* */ |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 958 | VLIB_CLI_COMMAND (http_server_create_command, static) = |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 959 | { |
| 960 | .path = "test http server", |
| 961 | .short_help = "test http server", |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 962 | .function = http_server_create_command_fn, |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 963 | }; |
| 964 | /* *INDENT-ON* */ |
| 965 | |
| 966 | static clib_error_t * |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 967 | http_server_main_init (vlib_main_t * vm) |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 968 | { |
| 969 | http_server_main_t *hsm = &http_server_main; |
Florin Coras | 149d62f | 2017-11-01 15:05:49 -0700 | [diff] [blame] | 970 | |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 971 | hsm->my_client_index = ~0; |
| 972 | hsm->vlib_main = vm; |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 973 | return 0; |
| 974 | } |
| 975 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 976 | VLIB_INIT_FUNCTION (http_server_main_init); |
Dave Barach | 1015a1e | 2017-05-08 19:15:03 -0400 | [diff] [blame] | 977 | |
| 978 | /* |
| 979 | * fd.io coding-style-patch-verification: ON |
| 980 | * |
| 981 | * Local Variables: |
| 982 | * eval: (c-set-style "gnu") |
| 983 | * End: |
| 984 | */ |