http: add http protocol plugin
[vpp.git] / src / plugins / http / http.h
1 /*
2  * Copyright (c) 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
16 #ifndef SRC_PLUGINS_HTTP_HTTP_H_
17 #define SRC_PLUGINS_HTTP_HTTP_H_
18
19 #include <vnet/plugin/plugin.h>
20 #include <vpp/app/version.h>
21
22 #include <vppinfra/time_range.h>
23
24 #include <vnet/session/application_interface.h>
25 #include <vnet/session/application.h>
26
27 #define HTTP_DEBUG 0
28
29 #if HTTP_DEBUG
30 #define HTTP_DBG(_lvl, _fmt, _args...)                                        \
31   if (_lvl <= HTTP_DEBUG)                                                     \
32   clib_warning (_fmt, ##_args)
33 #else
34 #define HTTP_DBG(_lvl, _fmt, _args...)
35 #endif
36
37 typedef struct http_conn_id_
38 {
39   union
40   {
41     session_handle_t app_session_handle;
42     u32 parent_app_api_ctx;
43   };
44   session_handle_t tc_session_handle;
45   u32 parent_app_wrk_index;
46 } http_conn_id_t;
47
48 STATIC_ASSERT (sizeof (http_conn_id_t) <= TRANSPORT_CONN_ID_LEN,
49                "ctx id must be less than TRANSPORT_CONN_ID_LEN");
50
51 typedef enum http_state_
52 {
53   HTTP_CONN_STATE_LISTEN,
54   HTTP_CONN_STATE_CONNECTING,
55   HTTP_CONN_STATE_ESTABLISHED,
56   HTTP_CONN_STATE_TRANSPORT_CLOSED,
57   HTTP_CONN_STATE_APP_CLOSED,
58   HTTP_CONN_STATE_CLOSED
59 } http_conn_state_t;
60
61 typedef enum http_req_state_
62 {
63   HTTP_REQ_STATE_WAIT_METHOD,
64   HTTP_REQ_STATE_WAIT_APP,
65   HTTP_REQ_STATE_SEND_MORE_DATA,
66   HTTP_REQ_N_STATES,
67 } http_req_state_t;
68
69 typedef enum http_req_method_
70 {
71   HTTP_REQ_GET = 0,
72   HTTP_REQ_POST,
73 } http_req_method_t;
74
75 typedef enum http_msg_type_
76 {
77   HTTP_MSG_REQUEST,
78   HTTP_MSG_REPLY
79 } http_msg_type_t;
80
81 #define foreach_http_content_type                                             \
82   _ (TEXT_HTML, "text/html")                                                  \
83   _ (TEXT_CSS, "text/css")                                                    \
84   _ (TEXT_JS, "text/javascript")                                              \
85   _ (TEXT_JSON, "application/json")
86
87 typedef enum http_content_type_
88 {
89 #define _(s, str) HTTP_CONTENT_##s,
90   foreach_http_content_type
91 #undef _
92 } http_content_type_t;
93
94 #define foreach_http_status_code                                              \
95   _ (200, OK, "200 OK")                                                       \
96   _ (400, BAD_REQUEST, "400 Bad Request")                                     \
97   _ (405, METHOD_NOT_ALLOWED, "405 Method Not Allowed")                       \
98   _ (500, INTERNAL_ERROR, "500 Internal Server Error")
99
100 typedef enum http_status_code_
101 {
102 #define _(c, s, str) HTTP_STATUS_##s,
103   foreach_http_status_code
104 #undef _
105     HTTP_N_STATUS
106 } http_status_code_t;
107
108 typedef struct http_msg_data_
109 {
110   http_content_type_t content_type;
111   u32 len;
112   u32 offset;
113   u8 data[0];
114 } http_msg_data_t;
115
116 typedef struct http_msg_
117 {
118   http_msg_type_t type;
119   union
120   {
121     http_req_method_t method_type;
122     http_status_code_t code;
123   };
124   http_msg_data_t data;
125 } http_msg_t;
126
127 typedef struct http_buffer_
128 {
129   svm_fifo_t *src;
130   svm_fifo_seg_t *segs;
131   u32 len;
132   u32 cur_seg;
133   u32 offset;
134 } http_buffer_t;
135
136 typedef struct http_tc_
137 {
138   union
139   {
140     transport_connection_t connection;
141     http_conn_id_t c_http_conn_id;
142   };
143 #define h_tc_session_handle c_http_conn_id.tc_session_handle
144 #define h_pa_wrk_index      c_http_conn_id.parent_app_wrk_index
145 #define h_pa_session_handle c_http_conn_id.app_session_handle
146 #define h_hc_index          connection.c_index
147
148   http_conn_state_t state;
149   u32 timer_handle;
150
151   /*
152    * Current request
153    */
154   http_req_state_t req_state;
155   http_req_method_t method;
156   u8 *rx_buf;
157   u32 rx_buf_offset;
158   http_buffer_t tx_buf;
159 } http_conn_t;
160
161 typedef struct http_worker_
162 {
163   http_conn_t *conn_pool;
164 } http_worker_t;
165
166 typedef struct http_main_
167 {
168   http_worker_t *wrk;
169   http_conn_t *listener_ctx_pool;
170   u32 app_index;
171
172   clib_timebase_t timebase;
173
174   /*
175    * Runtime config
176    */
177   u8 debug_level;
178
179   /*
180    * Config
181    */
182   u64 first_seg_size;
183   u32 fifo_size;
184 } http_main_t;
185
186 #endif /* SRC_PLUGINS_HTTP_HTTP_H_ */
187
188 /*
189  * fd.io coding-style-patch-verification: ON
190  *
191  * Local Variables:
192  * eval: (c-set-style "gnu")
193  * End:
194  */