1 /*
2  * include/types/global.h
3  * Global variables.
4  *
5  * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation, version 2.1
10  * exclusively.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #ifndef _TYPES_GLOBAL_H
23 #define _TYPES_GLOBAL_H
24 
25 #include <netinet/in.h>
26 
27 #include <common/config.h>
28 #include <common/initcall.h>
29 #include <common/hathreads.h>
30 #include <common/standard.h>
31 
32 #include <types/listener.h>
33 #include <types/proxy.h>
34 #include <types/task.h>
35 #include <types/vars.h>
36 
37 #ifndef UNIX_MAX_PATH
38 #define UNIX_MAX_PATH 108
39 #endif
40 
41 /* modes of operation (global.mode) */
42 #define	MODE_DEBUG	0x01
43 #define	MODE_DAEMON	0x02
44 #define	MODE_QUIET	0x04
45 #define	MODE_CHECK	0x08
46 #define	MODE_VERBOSE	0x10
47 #define	MODE_STARTING	0x20
48 #define	MODE_FOREGROUND	0x40
49 #define	MODE_MWORKER	0x80    /* Master Worker */
50 #define	MODE_MWORKER_WAIT	0x100    /* Master Worker wait mode */
51 
52 /* list of last checks to perform, depending on config options */
53 #define LSTCHK_CAP_BIND	0x00000001	/* check that we can bind to any port */
54 #define LSTCHK_NETADM	0x00000002	/* check that we have CAP_NET_ADMIN */
55 
56 /* Global tuning options */
57 /* available polling mechanisms */
58 #define GTUNE_USE_SELECT         (1<<0)
59 #define GTUNE_USE_POLL           (1<<1)
60 #define GTUNE_USE_EPOLL          (1<<2)
61 #define GTUNE_USE_KQUEUE         (1<<3)
62 /* platform-specific options */
63 #define GTUNE_USE_SPLICE         (1<<4)
64 #define GTUNE_USE_GAI            (1<<5)
65 #define GTUNE_USE_REUSEPORT      (1<<6)
66 #define GTUNE_RESOLVE_DONTFAIL   (1<<7)
67 
68 #define GTUNE_SOCKET_TRANSFER	 (1<<8)
69 #define GTUNE_NOEXIT_ONFAILURE   (1<<9)
70 #define GTUNE_USE_SYSTEMD        (1<<10)
71 
72 #define GTUNE_BUSY_POLLING       (1<<11)
73 #define GTUNE_LISTENER_MQ        (1<<12)
74 #define GTUNE_SET_DUMPABLE       (1<<13)
75 
76 #define GTUNE_USE_EVPORTS        (1<<14)
77 
78 /* Access level for a stats socket */
79 #define ACCESS_LVL_NONE     0
80 #define ACCESS_LVL_USER     1
81 #define ACCESS_LVL_OPER     2
82 #define ACCESS_LVL_ADMIN    3
83 #define ACCESS_LVL_MASK     0x3
84 
85 #define ACCESS_FD_LISTENERS 0x4  /* expose listeners FDs on stats socket */
86 #define ACCESS_MASTER       0x8  /* works with the master (and every other processes) */
87 #define ACCESS_MASTER_ONLY  0x10 /* only works with the worker */
88 
89 /* SSL server verify mode */
90 enum {
91 	SSL_SERVER_VERIFY_NONE = 0,
92 	SSL_SERVER_VERIFY_REQUIRED = 1,
93 };
94 
95 /* FIXME : this will have to be redefined correctly */
96 struct global {
97 	int uid;
98 	int gid;
99 	int external_check;
100 	int nbproc;
101 	int nbthread;
102 	unsigned int hard_stop_after;	/* maximum time allowed to perform a soft-stop */
103 	int maxconn, hardmaxconn;
104 	int maxsslconn;
105 	int ssl_session_max_cost;   /* how many bytes an SSL session may cost */
106 	int ssl_handshake_max_cost; /* how many bytes an SSL handshake may use */
107 	int ssl_used_frontend;      /* non-zero if SSL is used in a frontend */
108 	int ssl_used_backend;       /* non-zero if SSL is used in a backend */
109 	int ssl_used_async_engines; /* number of used async engines */
110 	unsigned int ssl_server_verify; /* default verify mode on servers side */
111 	struct freq_ctr conn_per_sec;
112 	struct freq_ctr sess_per_sec;
113 	struct freq_ctr ssl_per_sec;
114 	struct freq_ctr ssl_fe_keys_per_sec;
115 	struct freq_ctr ssl_be_keys_per_sec;
116 	struct freq_ctr comp_bps_in;	/* bytes per second, before http compression */
117 	struct freq_ctr comp_bps_out;	/* bytes per second, after http compression */
118 	struct freq_ctr out_32bps;      /* #of 32-byte blocks emitted per second */
119 	unsigned long long out_bytes;   /* total #of bytes emitted */
120 	int cps_lim, cps_max;
121 	int sps_lim, sps_max;
122 	int ssl_lim, ssl_max;
123 	int ssl_fe_keys_max, ssl_be_keys_max;
124 	unsigned int shctx_lookups, shctx_misses;
125 	int comp_rate_lim;           /* HTTP compression rate limit */
126 	int maxpipes;		/* max # of pipes */
127 	int maxsock;		/* max # of sockets */
128 	int rlimit_nofile;	/* default ulimit-n value : 0=unset */
129 	int rlimit_memmax_all;	/* default all-process memory limit in megs ; 0=unset */
130 	int rlimit_memmax;	/* default per-process memory limit in megs ; 0=unset */
131 	long maxzlibmem;        /* max RAM for zlib in bytes */
132 	int mode;
133 	unsigned int req_count; /* request counter (HTTP or TCP session) for logs and unique_id */
134 	int last_checks;
135 	int spread_checks;
136 	int max_spread_checks;
137 	int max_syslog_len;
138 	char *chroot;
139 	char *pidfile;
140 	char *node, *desc;		/* node name & description */
141 	struct buffer log_tag;           /* name for syslog */
142 	struct list logsrvs;
143 	char *log_send_hostname;   /* set hostname in syslog header */
144 	char *server_state_base;   /* path to a directory where server state files can be found */
145 	char *server_state_file;   /* path to the file where server states are loaded from */
146 	struct {
147 		int maxpollevents; /* max number of poll events at once */
148 		int maxaccept;     /* max number of consecutive accept() */
149 		int options;       /* various tuning options */
150 		int runqueue_depth;/* max number of tasks to run at once */
151 		int recv_enough;   /* how many input bytes at once are "enough" */
152 		int bufsize;       /* buffer size in bytes, defaults to BUFSIZE */
153 		int maxrewrite;    /* buffer max rewrite size in bytes, defaults to MAXREWRITE */
154 		int reserved_bufs; /* how many buffers can only be allocated for response */
155 		int buf_limit;     /* if not null, how many total buffers may only be allocated */
156 		int client_sndbuf; /* set client sndbuf to this value if not null */
157 		int client_rcvbuf; /* set client rcvbuf to this value if not null */
158 		int server_sndbuf; /* set server sndbuf to this value if not null */
159 		int server_rcvbuf; /* set server rcvbuf to this value if not null */
160 		int chksize;       /* check buffer size in bytes, defaults to BUFSIZE */
161 		int pipesize;      /* pipe size in bytes, system defaults if zero */
162 		int max_http_hdr;  /* max number of HTTP headers, use MAX_HTTP_HDR if zero */
163 		int requri_len;    /* max len of request URI, use REQURI_LEN if zero */
164 		int cookie_len;    /* max length of cookie captures */
165 		int pattern_cache; /* max number of entries in the pattern cache. */
166 		int sslcachesize;  /* SSL cache size in session, defaults to 20000 */
167 		int comp_maxlevel;    /* max HTTP compression level */
168 		int pool_low_ratio;   /* max ratio of FDs used before we stop using new idle connections */
169 		int pool_high_ratio;  /* max ratio of FDs used before we start killing idle connections when creating new connections */
170 		int pool_low_count;   /* max number of opened fd before we stop using new idle connections */
171 		int pool_high_count;  /* max number of opened fd before we start killing idle connections when creating new connections */
172 		unsigned short idle_timer; /* how long before an empty buffer is considered idle (ms) */
173 	} tune;
174 	struct {
175 		char *prefix;           /* path prefix of unix bind socket */
176 		struct {                /* UNIX socket permissions */
177 			uid_t uid;      /* -1 to leave unchanged */
178 			gid_t gid;      /* -1 to leave unchanged */
179 			mode_t mode;    /* 0 to leave unchanged */
180 		} ux;
181 	} unix_bind;
182 	struct proxy *stats_fe;     /* the frontend holding the stats settings */
183 	struct vars   vars;         /* list of variables for the process scope. */
184 #ifdef USE_CPU_AFFINITY
185 	struct {
186 		unsigned long proc[MAX_PROCS];      /* list of CPU masks for the 32/64 first processes */
187 		unsigned long proc_t1[MAX_PROCS];   /* list of CPU masks for the 1st thread of each process */
188 		unsigned long thread[MAX_THREADS];  /* list of CPU masks for the 32/64 first threads of the 1st process */
189 	} cpu_map;
190 #endif
191 };
192 
193 /* options for mworker_proc */
194 
195 #define PROC_O_TYPE_MASTER           0x00000001
196 #define PROC_O_TYPE_WORKER           0x00000002
197 #define PROC_O_TYPE_PROG             0x00000004
198 /* 0x00000008 unused */
199 #define PROC_O_LEAVING               0x00000010  /* this process should be leaving */
200 /* 0x00000020 to 0x00000080 unused */
201 #define PROC_O_START_RELOAD          0x00000100  /* Start the process even if the master was re-executed */
202 
203 /*
204  * Structure used to describe the processes in master worker mode
205  */
206 struct mworker_proc {
207 	int pid;
208 	int options;
209 	char *id;
210 	char **command;
211 	char *path;
212 	char *version;
213 	int ipc_fd[2]; /* 0 is master side, 1 is worker side */
214 	int relative_pid;
215 	int reloads;
216 	int timestamp;
217 	struct server *srv; /* the server entry in the master proxy */
218 	struct list list;
219 };
220 
221 extern struct global global;
222 extern int  pid;                /* current process id */
223 extern int  relative_pid;       /* process id starting at 1 */
224 extern unsigned long pid_bit;   /* bit corresponding to the process id */
225 extern unsigned long all_proc_mask; /* mask of all processes */
226 extern int  actconn;            /* # of active sessions */
227 extern int  listeners;
228 extern int  jobs;               /* # of active jobs (listeners, sessions, open devices) */
229 extern int  unstoppable_jobs;   /* # of active jobs that can't be stopped during a soft stop */
230 extern int  active_peers;       /* # of active peers (connection attempts and successes) */
231 extern int  connected_peers;    /* # of really connected peers */
232 extern THREAD_LOCAL struct buffer trash;
233 extern int nb_oldpids;          /* contains the number of old pids found */
234 extern const int zero;
235 extern const int one;
236 extern const struct linger nolinger;
237 extern int stopping;	/* non zero means stopping in progress */
238 extern int killed;	/* >0 means a hard-stop is triggered, >1 means hard-stop immediately */
239 extern char hostname[MAX_HOSTNAME_LEN];
240 extern char localpeer[MAX_HOSTNAME_LEN];
241 extern struct list global_listener_queue; /* list of the temporarily limited listeners */
242 extern struct task *global_listener_queue_task;
243 extern unsigned int warned;     /* bitfield of a few warnings to emit just once */
244 extern volatile unsigned long sleeping_thread_mask;
245 extern struct list proc_list; /* list of process in mworker mode */
246 extern struct mworker_proc *proc_self; /* process structure of current process */
247 extern int master; /* 1 if in master, 0 otherwise */
248 extern unsigned int rlim_fd_cur_at_boot;
249 extern unsigned int rlim_fd_max_at_boot;
250 extern int atexit_flag;
251 extern unsigned char boot_seed[20];  // per-boot random seed (160 bits initially)
252 
253 /* bit values to go with "warned" above */
254 #define WARN_BLOCK_DEPRECATED       0x00000001
255 /* unassigned : 0x00000002 */
256 #define WARN_REDISPATCH_DEPRECATED  0x00000004
257 #define WARN_CLITO_DEPRECATED       0x00000008
258 #define WARN_SRVTO_DEPRECATED       0x00000010
259 #define WARN_CONTO_DEPRECATED       0x00000020
260 #define WARN_FORCECLOSE_DEPRECATED  0x00000040
261 
262 #define WARN_REQREP_DEPRECATED      0x00000080
263 #define WARN_REQDEL_DEPRECATED      0x00000100
264 #define WARN_REQDENY_DEPRECATED     0x00000200
265 #define WARN_REQPASS_DEPRECATED     0x00000400
266 #define WARN_REQALLOW_DEPRECATED    0x00000800
267 #define WARN_REQTARPIT_DEPRECATED   0x00001000
268 #define WARN_REQADD_DEPRECATED      0x00002000
269 #define WARN_RSPREP_DEPRECATED      0x00004000
270 #define WARN_RSPDEL_DEPRECATED      0x00008000
271 #define WARN_RSPDENY_DEPRECATED     0x00010000
272 #define WARN_RSPADD_DEPRECATED      0x00020000
273 
274 /* to be used with warned and WARN_* */
already_warned(unsigned int warning)275 static inline int already_warned(unsigned int warning)
276 {
277 	if (warned & warning)
278 		return 1;
279 	warned |= warning;
280 	return 0;
281 }
282 
283 /* returns a mask if set, otherwise all_proc_mask */
proc_mask(unsigned long mask)284 static inline unsigned long proc_mask(unsigned long mask)
285 {
286 	return mask ? mask : all_proc_mask;
287 }
288 
289 /* returns a mask if set, otherwise all_threads_mask */
thread_mask(unsigned long mask)290 static inline unsigned long thread_mask(unsigned long mask)
291 {
292 	return mask ? mask : all_threads_mask;
293 }
294 
295 int tell_old_pids(int sig);
296 int delete_oldpid(int pid);
297 
298 int main(int argc, char **argv);
299 void deinit(void);
300 void hap_register_build_opts(const char *str, int must_free);
301 void hap_register_post_check(int (*fct)());
302 void hap_register_post_deinit(void (*fct)());
303 
304 void hap_register_per_thread_alloc(int (*fct)());
305 void hap_register_per_thread_init(int (*fct)());
306 void hap_register_per_thread_deinit(void (*fct)());
307 void hap_register_per_thread_free(int (*fct)());
308 
309 void mworker_accept_wrapper(int fd);
310 void mworker_reload();
311 
312 /* simplified way to declare static build options in a file */
313 #define REGISTER_BUILD_OPTS(str) \
314 	INITCALL2(STG_REGISTER, hap_register_build_opts, (str), 0)
315 
316 /* simplified way to declare a post-check callback in a file */
317 #define REGISTER_POST_CHECK(fct) \
318 	INITCALL1(STG_REGISTER, hap_register_post_check, (fct))
319 
320 /* simplified way to declare a post-deinit callback in a file */
321 #define REGISTER_POST_DEINIT(fct) \
322 	INITCALL1(STG_REGISTER, hap_register_post_deinit, (fct))
323 
324 /* simplified way to declare a per-thread allocation callback in a file */
325 #define REGISTER_PER_THREAD_ALLOC(fct) \
326 	INITCALL1(STG_REGISTER, hap_register_per_thread_alloc, (fct))
327 
328 /* simplified way to declare a per-thread init callback in a file */
329 #define REGISTER_PER_THREAD_INIT(fct) \
330 	INITCALL1(STG_REGISTER, hap_register_per_thread_init, (fct))
331 
332 /* simplified way to declare a per-thread deinit callback in a file */
333 #define REGISTER_PER_THREAD_DEINIT(fct) \
334 	INITCALL1(STG_REGISTER, hap_register_per_thread_deinit, (fct))
335 
336 /* simplified way to declare a per-thread free callback in a file */
337 #define REGISTER_PER_THREAD_FREE(fct) \
338 	INITCALL1(STG_REGISTER, hap_register_per_thread_free, (fct))
339 
340 #endif /* _TYPES_GLOBAL_H */
341 
342 /*
343  * Local variables:
344  *  c-indent-level: 8
345  *  c-basic-offset: 8
346  * End:
347  */
348