http: add http protocol plugin
[vpp.git] / src / plugins / http / http_timer.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_TIMER_H_
17 #define SRC_PLUGINS_HTTP_HTTP_TIMER_H_
18
19 #include <http/http.h>
20 #include <vppinfra/tw_timer_2t_1w_2048sl.h>
21
22 #define HTTP_CONN_TIMEOUT 60
23
24 typedef void (http_conn_timeout_fn) (void *);
25
26 typedef struct http_tw_ctx_
27 {
28   tw_timer_wheel_2t_1w_2048sl_t tw;
29   clib_spinlock_t tw_lock;
30   http_conn_timeout_fn *cb_fn;
31 } http_tw_ctx_t;
32
33 extern http_tw_ctx_t http_tw_ctx;
34
35 void http_timers_init (vlib_main_t *vm, http_conn_timeout_fn *cb_fn);
36
37 static inline void
38 http_conn_timer_start (http_conn_t *hc)
39 {
40   http_tw_ctx_t *twc = &http_tw_ctx;
41   u32 hs_handle;
42   u64 timeout;
43
44   timeout = HTTP_CONN_TIMEOUT;
45   hs_handle = hc->c_thread_index << 24 | hc->c_c_index;
46
47   clib_spinlock_lock (&twc->tw_lock);
48   hc->timer_handle =
49     tw_timer_start_2t_1w_2048sl (&twc->tw, hs_handle, 0, timeout);
50   clib_spinlock_unlock (&twc->tw_lock);
51 }
52
53 static inline void
54 http_conn_timer_stop (http_conn_t *hc)
55 {
56   http_tw_ctx_t *twc = &http_tw_ctx;
57
58   if (hc->timer_handle == ~0)
59     return;
60
61   clib_spinlock_lock (&twc->tw_lock);
62   tw_timer_stop_2t_1w_2048sl (&twc->tw, hc->timer_handle);
63   hc->timer_handle = ~0;
64   clib_spinlock_unlock (&twc->tw_lock);
65 }
66
67 static inline void
68 http_conn_timer_update (http_conn_t *hc)
69 {
70   http_tw_ctx_t *twc = &http_tw_ctx;
71   u64 timeout;
72
73   if (hc->timer_handle == ~0)
74     return;
75
76   timeout = HTTP_CONN_TIMEOUT;
77
78   clib_spinlock_lock (&twc->tw_lock);
79   tw_timer_update_2t_1w_2048sl (&twc->tw, hc->timer_handle, timeout);
80   clib_spinlock_unlock (&twc->tw_lock);
81 }
82
83 #endif /* SRC_PLUGINS_HTTP_HTTP_TIMER_H_ */
84
85 /*
86  * fd.io coding-style-patch-verification: ON
87  *
88  * Local Variables:
89  * eval: (c-set-style "gnu")
90  * End:
91  */