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 #define GTUNE_USE_EVPORTS        (1<<14)
76 #define GTUNE_STRICT_LIMITS      (1<<15)
77 
78 /* SSL server verify mode */
79 enum {
80 	SSL_SERVER_VERIFY_NONE = 0,
81 	SSL_SERVER_VERIFY_REQUIRED = 1,
82 };
83 
84 /* FIXME : this will have to be redefined correctly */
85 struct global {
86 	int uid;
87 	int gid;
88 	int external_check;
89 	int nbproc;
90 	int nbthread;
91 	unsigned int hard_stop_after;	/* maximum time allowed to perform a soft-stop */
92 	int maxconn, hardmaxconn;
93 	int maxsslconn;
94 	int ssl_session_max_cost;   /* how many bytes an SSL session may cost */
95 	int ssl_handshake_max_cost; /* how many bytes an SSL handshake may use */
96 	int ssl_used_frontend;      /* non-zero if SSL is used in a frontend */
97 	int ssl_used_backend;       /* non-zero if SSL is used in a backend */
98 	int ssl_used_async_engines; /* number of used async engines */
99 	unsigned int ssl_server_verify; /* default verify mode on servers side */
100 	struct freq_ctr conn_per_sec;
101 	struct freq_ctr sess_per_sec;
102 	struct freq_ctr ssl_per_sec;
103 	struct freq_ctr ssl_fe_keys_per_sec;
104 	struct freq_ctr ssl_be_keys_per_sec;
105 	struct freq_ctr comp_bps_in;	/* bytes per second, before http compression */
106 	struct freq_ctr comp_bps_out;	/* bytes per second, after http compression */
107 	struct freq_ctr out_32bps;      /* #of 32-byte blocks emitted per second */
108 	unsigned long long out_bytes;   /* total #of bytes emitted */
109 	int cps_lim, cps_max;
110 	int sps_lim, sps_max;
111 	int ssl_lim, ssl_max;
112 	int ssl_fe_keys_max, ssl_be_keys_max;
113 	unsigned int shctx_lookups, shctx_misses;
114 	int comp_rate_lim;           /* HTTP compression rate limit */
115 	int maxpipes;		/* max # of pipes */
116 	int maxsock;		/* max # of sockets */
117 	int rlimit_nofile;	/* default ulimit-n value : 0=unset */
118 	int rlimit_memmax_all;	/* default all-process memory limit in megs ; 0=unset */
119 	int rlimit_memmax;	/* default per-process memory limit in megs ; 0=unset */
120 	long maxzlibmem;        /* max RAM for zlib in bytes */
121 	int mode;
122 	unsigned int req_count; /* request counter (HTTP or TCP session) for logs and unique_id */
123 	int last_checks;
124 	int spread_checks;
125 	int max_spread_checks;
126 	int max_syslog_len;
127 	char *chroot;
128 	char *pidfile;
129 	char *node, *desc;		/* node name & description */
130 	struct buffer log_tag;           /* name for syslog */
131 	struct list logsrvs;
132 	char *log_send_hostname;   /* set hostname in syslog header */
133 	char *server_state_base;   /* path to a directory where server state files can be found */
134 	char *server_state_file;   /* path to the file where server states are loaded from */
135 	struct {
136 		int maxpollevents; /* max number of poll events at once */
137 		int maxaccept;     /* max number of consecutive accept() */
138 		int options;       /* various tuning options */
139 		int runqueue_depth;/* max number of tasks to run at once */
140 		int recv_enough;   /* how many input bytes at once are "enough" */
141 		int bufsize;       /* buffer size in bytes, defaults to BUFSIZE */
142 		int maxrewrite;    /* buffer max rewrite size in bytes, defaults to MAXREWRITE */
143 		int reserved_bufs; /* how many buffers can only be allocated for response */
144 		int buf_limit;     /* if not null, how many total buffers may only be allocated */
145 		int client_sndbuf; /* set client sndbuf to this value if not null */
146 		int client_rcvbuf; /* set client rcvbuf to this value if not null */
147 		int server_sndbuf; /* set server sndbuf to this value if not null */
148 		int server_rcvbuf; /* set server rcvbuf to this value if not null */
149 		int chksize;       /* check buffer size in bytes, defaults to BUFSIZE */
150 		int pipesize;      /* pipe size in bytes, system defaults if zero */
151 		int max_http_hdr;  /* max number of HTTP headers, use MAX_HTTP_HDR if zero */
152 		int requri_len;    /* max len of request URI, use REQURI_LEN if zero */
153 		int cookie_len;    /* max length of cookie captures */
154 		int pattern_cache; /* max number of entries in the pattern cache. */
155 		int sslcachesize;  /* SSL cache size in session, defaults to 20000 */
156 		int comp_maxlevel;    /* max HTTP compression level */
157 		int pool_low_ratio;   /* max ratio of FDs used before we stop using new idle connections */
158 		int pool_high_ratio;  /* max ratio of FDs used before we start killing idle connections when creating new connections */
159 		int pool_low_count;   /* max number of opened fd before we stop using new idle connections */
160 		int pool_high_count;  /* max number of opened fd before we start killing idle connections when creating new connections */
161 		unsigned short idle_timer; /* how long before an empty buffer is considered idle (ms) */
162 	} tune;
163 	struct {
164 		char *prefix;           /* path prefix of unix bind socket */
165 		struct {                /* UNIX socket permissions */
166 			uid_t uid;      /* -1 to leave unchanged */
167 			gid_t gid;      /* -1 to leave unchanged */
168 			mode_t mode;    /* 0 to leave unchanged */
169 		} ux;
170 	} unix_bind;
171 	struct proxy *stats_fe;     /* the frontend holding the stats settings */
172 	struct vars   vars;         /* list of variables for the process scope. */
173 #ifdef USE_CPU_AFFINITY
174 	struct {
175 		unsigned long proc[MAX_PROCS];      /* list of CPU masks for the 32/64 first processes */
176 		unsigned long proc_t1[MAX_PROCS];   /* list of CPU masks for the 1st thread of each process */
177 		unsigned long thread[MAX_THREADS];  /* list of CPU masks for the 32/64 first threads of the 1st process */
178 	} cpu_map;
179 #endif
180 	struct {
181 		struct {
182 			int status;                      /* cache on or off */
183 
184 			uint64_t dict_size;              /* max memory used by dict, in bytes */
185 			uint64_t data_size;              /* max memory used by data, in bytes */
186 
187 			int dict_cleaner;                /* the number of entries checked once */
188 			int data_cleaner;                /* the number of data checked once */
189 			int disk_cleaner;                /* the number of files checked once */
190 			int disk_loader;                 /* the number of files load once */
191 			int disk_saver;                  /* the number of entries checked once for persist_async */
192 
193 			struct ist root;                 /* disk root directory */
194 
195 			struct nst_memory *memory;       /* memory */
196 		} cache;
197 		struct {
198 			int status;                      /* enable nosql on or off */
199 
200 			uint64_t dict_size;              /* max memory used by dict, in bytes */
201 			uint64_t data_size;              /* max memory used by data, in bytes */
202 
203 			int dict_cleaner;                /* the number of entries checked once */
204 			int data_cleaner;                /* the number of data checked once */
205 			int disk_cleaner;                /* the number of files checked once */
206 			int disk_loader;                 /* the number of files load once */
207 			int disk_saver;                  /* the number of entries checked once for persist_async */
208 
209 			struct ist root;                 /* disk root directory */
210 
211 			struct nst_memory *memory;       /* memory */
212 		} nosql;
213 
214 		struct {
215 			int          status;                      /* enable nosql on or off */
216 			struct ist   purge_method;
217 			struct ist   uri;                        /* the uri used for stats and manager */
218 		} manager;
219 
220 		struct nst_stats  *stats;
221 
222 		struct nst_memory *memory;                     /* for common usage */
223 	} nuster;
224 };
225 
226 /* options for mworker_proc */
227 
228 #define PROC_O_TYPE_MASTER           0x00000001
229 #define PROC_O_TYPE_WORKER           0x00000002
230 #define PROC_O_TYPE_PROG             0x00000004
231 /* 0x00000008 unused */
232 #define PROC_O_LEAVING               0x00000010  /* this process should be leaving */
233 /* 0x00000020 to 0x00000080 unused */
234 #define PROC_O_START_RELOAD          0x00000100  /* Start the process even if the master was re-executed */
235 
236 /*
237  * Structure used to describe the processes in master worker mode
238  */
239 struct mworker_proc {
240 	int pid;
241 	int options;
242 	char *id;
243 	char **command;
244 	char *path;
245 	char *version;
246 	int ipc_fd[2]; /* 0 is master side, 1 is worker side */
247 	int relative_pid;
248 	int reloads;
249 	int timestamp;
250 	struct server *srv; /* the server entry in the master proxy */
251 	struct list list;
252 	int uid;
253 	int gid;
254 };
255 
256 extern struct global global;
257 extern int  pid;                /* current process id */
258 extern int  relative_pid;       /* process id starting at 1 */
259 extern unsigned long pid_bit;   /* bit corresponding to the process id */
260 extern unsigned long all_proc_mask; /* mask of all processes */
261 extern int  actconn;            /* # of active sessions */
262 extern int  listeners;
263 extern int  jobs;               /* # of active jobs (listeners, sessions, open devices) */
264 extern int  unstoppable_jobs;   /* # of active jobs that can't be stopped during a soft stop */
265 extern int  active_peers;       /* # of active peers (connection attempts and successes) */
266 extern int  connected_peers;    /* # of really connected peers */
267 extern THREAD_LOCAL struct buffer trash;
268 extern int nb_oldpids;          /* contains the number of old pids found */
269 extern const int zero;
270 extern const int one;
271 extern const struct linger nolinger;
272 extern int stopping;	/* non zero means stopping in progress */
273 extern int killed;	/* >0 means a hard-stop is triggered, >1 means hard-stop immediately */
274 extern char hostname[MAX_HOSTNAME_LEN];
275 extern char localpeer[MAX_HOSTNAME_LEN];
276 extern struct mt_list global_listener_queue; /* list of the temporarily limited listeners */
277 extern struct task *global_listener_queue_task;
278 extern unsigned int warned;     /* bitfield of a few warnings to emit just once */
279 extern volatile unsigned long sleeping_thread_mask;
280 extern struct list proc_list; /* list of process in mworker mode */
281 extern struct mworker_proc *proc_self; /* process structure of current process */
282 extern int master; /* 1 if in master, 0 otherwise */
283 extern unsigned int rlim_fd_cur_at_boot;
284 extern unsigned int rlim_fd_max_at_boot;
285 extern int atexit_flag;
286 extern unsigned char boot_seed[20];  // per-boot random seed (160 bits initially)
287 
288 /* bit values to go with "warned" above */
289 /* unassigned : 0x00000001 (previously: WARN_BLOCK_DEPRECATED) */
290 /* unassigned : 0x00000002 */
291 /* unassigned : 0x00000004 (previously: WARN_REDISPATCH_DEPRECATED) */
292 /* unassigned : 0x00000008 (previously: WARN_CLITO_DEPRECATED) */
293 /* unassigned : 0x00000010 (previously: WARN_SRVTO_DEPRECATED) */
294 /* unassigned : 0x00000020 (previously: WARN_CONTO_DEPRECATED) */
295 #define WARN_FORCECLOSE_DEPRECATED  0x00000040
296 
297 
298 /* to be used with warned and WARN_* */
already_warned(unsigned int warning)299 static inline int already_warned(unsigned int warning)
300 {
301 	if (warned & warning)
302 		return 1;
303 	warned |= warning;
304 	return 0;
305 }
306 
307 /* returns a mask if set, otherwise all_proc_mask */
proc_mask(unsigned long mask)308 static inline unsigned long proc_mask(unsigned long mask)
309 {
310 	return mask ? mask : all_proc_mask;
311 }
312 
313 /* returns a mask if set, otherwise all_threads_mask */
thread_mask(unsigned long mask)314 static inline unsigned long thread_mask(unsigned long mask)
315 {
316 	return mask ? mask : all_threads_mask;
317 }
318 
319 int tell_old_pids(int sig);
320 int delete_oldpid(int pid);
321 
322 int main(int argc, char **argv);
323 void deinit(void);
324 void hap_register_build_opts(const char *str, int must_free);
325 void hap_register_post_check(int (*fct)());
326 void hap_register_post_proxy_check(int (*fct)(struct proxy *));
327 void hap_register_post_server_check(int (*fct)(struct server *));
328 void hap_register_post_deinit(void (*fct)());
329 void hap_register_proxy_deinit(void (*fct)(struct proxy *));
330 void hap_register_server_deinit(void (*fct)(struct server *));
331 
332 void hap_register_per_thread_alloc(int (*fct)());
333 void hap_register_per_thread_init(int (*fct)());
334 void hap_register_per_thread_deinit(void (*fct)());
335 void hap_register_per_thread_free(int (*fct)());
336 
337 void mworker_accept_wrapper(int fd);
338 void mworker_reload();
339 
340 /* simplified way to declare static build options in a file */
341 #define REGISTER_BUILD_OPTS(str) \
342 	INITCALL2(STG_REGISTER, hap_register_build_opts, (str), 0)
343 
344 /* simplified way to declare a post-check callback in a file */
345 #define REGISTER_POST_CHECK(fct) \
346 	INITCALL1(STG_REGISTER, hap_register_post_check, (fct))
347 
348 /* simplified way to declare a post-proxy-check callback in a file */
349 #define REGISTER_POST_PROXY_CHECK(fct) \
350 	INITCALL1(STG_REGISTER, hap_register_post_proxy_check, (fct))
351 
352 /* simplified way to declare a post-server-check callback in a file */
353 #define REGISTER_POST_SERVER_CHECK(fct) \
354 	INITCALL1(STG_REGISTER, hap_register_post_server_check, (fct))
355 
356 /* simplified way to declare a post-deinit callback in a file */
357 #define REGISTER_POST_DEINIT(fct) \
358 	INITCALL1(STG_REGISTER, hap_register_post_deinit, (fct))
359 
360 /* simplified way to declare a proxy-deinit callback in a file */
361 #define REGISTER_PROXY_DEINIT(fct) \
362 	INITCALL1(STG_REGISTER, hap_register_proxy_deinit, (fct))
363 
364 /* simplified way to declare a proxy-deinit callback in a file */
365 #define REGISTER_SERVER_DEINIT(fct) \
366 	INITCALL1(STG_REGISTER, hap_register_server_deinit, (fct))
367 
368 /* simplified way to declare a per-thread allocation callback in a file */
369 #define REGISTER_PER_THREAD_ALLOC(fct) \
370 	INITCALL1(STG_REGISTER, hap_register_per_thread_alloc, (fct))
371 
372 /* simplified way to declare a per-thread init callback in a file */
373 #define REGISTER_PER_THREAD_INIT(fct) \
374 	INITCALL1(STG_REGISTER, hap_register_per_thread_init, (fct))
375 
376 /* simplified way to declare a per-thread deinit callback in a file */
377 #define REGISTER_PER_THREAD_DEINIT(fct) \
378 	INITCALL1(STG_REGISTER, hap_register_per_thread_deinit, (fct))
379 
380 /* simplified way to declare a per-thread free callback in a file */
381 #define REGISTER_PER_THREAD_FREE(fct) \
382 	INITCALL1(STG_REGISTER, hap_register_per_thread_free, (fct))
383 
384 #endif /* _TYPES_GLOBAL_H */
385 
386 /*
387  * Local variables:
388  *  c-indent-level: 8
389  *  c-basic-offset: 8
390  * End:
391  */
392