summaryrefslogtreecommitdiff
path: root/platform/include/internal/proxy.h
blob: e0978fce8463d00ec4af1187d07282d1d84942c0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#pragma once

#include <tfe_stream.h>
#include <event2/event.h>
#include <sender_scm.h>
#include <ssl_stream_core.h>

struct ssl_mgr;
struct key_keeper;
struct acceptor_kni_v2;
struct watchdog_kni;
struct breakpad_instance;

enum TFE_STAT_FIELD
{
	STAT_SIGPIPE,
	/* FDs */
	STAT_FD_OPEN_BY_KNI_ACCEPT,
	STAT_FD_CLOSE_BY_KNI_ACCEPT_FAIL,
	STAT_FD_CLOSE,

	/* Stream */
	STAT_STREAM_OPEN,
	STAT_STREAM_CLS,
	STAT_STREAM_CLS_DOWN_EOF,
	STAT_STREAM_CLS_UP_EOF,
	STAT_STREAM_CLS_DOWN_ERR,
	STAT_STREAM_CLS_UP_ERR,
	STAT_STREAM_CLS_KILL,

	/* Action */
	STAT_STREAM_INTERCEPT,
	STAT_STREAM_BYPASS,
	STAT_STREAM_INCPT_BYTES,
	STAT_STREAM_INCPT_DOWN_BYTES,
	STAT_STREAM_INCPT_UP_BYTES,

	/* Protocol */
	STAT_STREAM_TCP_PLAIN,
	STAT_STREAM_TCP_SSL,
	TFE_STAT_MAX
};

struct tfe_proxy_tcp_options
{
	/* TCP OPTIONS */
	int sz_rcv_buffer;
	int sz_snd_buffer;

    /* TRACE FOR DEBUG */
    int enable_overwrite;
    int tcp_nodelay;
	int so_keepalive;
    int tcp_keepidle;
    int tcp_keepintvl;
    int tcp_keepcnt;
    int tcp_user_timeout;

    int tcp_ttl_upstream;
    int tcp_ttl_downstream;
};

struct tfe_proxy_rate_limit_options
{
	unsigned int read_rate;
	unsigned int read_burst;
	unsigned int write_rate;
	unsigned int write_burst;
};

struct tfe_proxy_accept_para
{
	/* Both upstream and downstream FDs */
	evutil_socket_t upstream_fd;
	evutil_socket_t downstream_fd;

	/* Session Type */
	bool is_set_session_type;
	enum tfe_stream_proto session_type;
	bool passthrough;

	/* addition info */
	unsigned int keyring_id;
};

enum tfe_load_balance_algo
{
	LEAST_CONN = 0,
	ROUND_ROBIN = 1,
};

struct tfe_proxy
{
	char name[TFE_SYMBOL_MAX];
	struct event_base * evbase;
	struct event * sev[8];
	struct event * gcev;

	void * logger;
	void * fs_handle;
	unsigned int nr_work_threads;
	struct tfe_thread_ctx * work_threads[TFE_THREAD_MAX];
	int make_work_thread_sleep;

	unsigned int nr_modules;
	struct tfe_plugin * modules;

	struct ssl_mgr * ssl_mgr_handler;
	struct ssl_policy_enforcer* ssl_ply_enforcer;
	struct key_keeper * key_keeper_handler;

	unsigned int en_kni_v1_acceptor;
    unsigned int en_kni_v2_acceptor;

    struct acceptor_kni_v1 * kni_v1_acceptor;
    struct acceptor_kni_v2 * kni_v2_acceptor;
    struct sender_scm * scm_sender;
    struct watchdog_kni * watchdog_kni;

	/* DEBUG OPTIONS */
	unsigned int tcp_all_passthrough;
	struct tfe_proxy_tcp_options tcp_options;

	/* GLOBAL RATELIMIT */
	unsigned int en_rate_limit;
	struct tfe_proxy_rate_limit_options rate_limit_options;

	/* PERFOMANCE MONIOTR VARIABLES*/
	long long stat_val[TFE_STAT_MAX];
	int fs_id[TFE_STAT_MAX];

	/* Crash Report */
	struct breakpad_instance * breakpad;

	/* cpu affinity */
	unsigned int enable_cpu_affinity;
	unsigned int cpu_affinity_mask[TFE_THREAD_MAX];

	/* load balancing */
	enum tfe_load_balance_algo load_balance;
};

extern struct tfe_proxy * g_default_proxy;

#define TFE_PROXY_STAT_INCREASE(field, val)			\
do { __atomic_fetch_add(&g_default_proxy->stat_val[field], val, __ATOMIC_RELAXED); } while(0)

struct tfe_thread_ctx * tfe_proxy_thread_ctx_acquire(struct tfe_proxy * ctx);
void tfe_proxy_thread_ctx_release(struct tfe_thread_ctx * thread_ctx);

struct tfe_proxy * tfe_proxy_new(const char * profile);
int tfe_proxy_fds_accept(struct tfe_proxy * ctx, int fd_downstream, int fd_upstream, struct tfe_cmsg * cmsg);
void tfe_proxy_run(struct tfe_proxy * proxy);
int tfe_thread_set_affinity(int core_id);