blob: e62e2a4f3afaff651b5ae15efe02e6801d6e7513 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02002 * include/types/fd.h
Willy Tarreauf817e9f2014-01-10 16:58:45 +01003 * File descriptors states - check src/fd.c for explanations.
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02004 *
Willy Tarreauf817e9f2014-01-10 16:58:45 +01005 * Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02006 *
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 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#ifndef _TYPES_FD_H
23#define _TYPES_FD_H
24
Willy Tarreau2dd0d472006-06-29 17:53:05 +020025#include <common/config.h>
Christopher Fauletd4604ad2017-05-29 10:40:41 +020026#include <common/hathreads.h>
Willy Tarreau931d8b72019-08-27 11:08:17 +020027#include <common/ist.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020028#include <types/port_range.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreau7be79a42012-11-11 15:02:54 +010030/* Direction for each FD event update */
Willy Tarreau54469402006-07-29 16:59:06 +020031enum {
32 DIR_RD=0,
33 DIR_WR=1,
Willy Tarreau54469402006-07-29 16:59:06 +020034};
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
Willy Tarreau7be79a42012-11-11 15:02:54 +010036/* Polling status flags returned in fdtab[].ev :
Willy Tarreaud6f087e2008-01-18 17:20:13 +010037 * FD_POLL_IN remains set as long as some data is pending for read.
38 * FD_POLL_OUT remains set as long as the fd accepts to write data.
39 * FD_POLL_ERR and FD_POLL_ERR remain set forever (until processed).
40 */
Willy Tarreau3d32d3a2007-04-15 11:31:05 +020041#define FD_POLL_IN 0x01
42#define FD_POLL_PRI 0x02
43#define FD_POLL_OUT 0x04
44#define FD_POLL_ERR 0x08
45#define FD_POLL_HUP 0x10
Willy Tarreau3d32d3a2007-04-15 11:31:05 +020046
Willy Tarreaud6f087e2008-01-18 17:20:13 +010047#define FD_POLL_DATA (FD_POLL_IN | FD_POLL_OUT)
48#define FD_POLL_STICKY (FD_POLL_ERR | FD_POLL_HUP)
Willy Tarreau3d32d3a2007-04-15 11:31:05 +020049
Willy Tarreaua135ea62020-02-21 16:26:19 +010050/* FD_EV_* are the values used in fdtab[].state to define the polling states in
51 * each direction. Most of them are manipulated using test-and-set operations
52 * which require the bit position in the mask, which is given in the _BIT
53 * variant.
54 */
Willy Tarreauf817e9f2014-01-10 16:58:45 +010055
Willy Tarreau1dad3842019-07-08 23:09:03 +020056/* bits positions for a few flags */
Willy Tarreauf8ecc7f2019-09-04 13:22:50 +020057#define FD_EV_ACTIVE_R_BIT 0
Willy Tarreau77abb432019-09-06 18:27:02 +020058#define FD_EV_READY_R_BIT 1
59#define FD_EV_SHUT_R_BIT 2
Willy Tarreau1ed37812020-02-26 16:12:45 +010060/* unused: 3 */
Willy Tarreauf8ecc7f2019-09-04 13:22:50 +020061
62#define FD_EV_ACTIVE_W_BIT 4
Willy Tarreau77abb432019-09-06 18:27:02 +020063#define FD_EV_READY_W_BIT 5
64#define FD_EV_SHUT_W_BIT 6
Willy Tarreau1ed37812020-02-26 16:12:45 +010065#define FD_EV_ERR_RW_BIT 7
Willy Tarreau1dad3842019-07-08 23:09:03 +020066
Willy Tarreaua135ea62020-02-21 16:26:19 +010067/* and flag values */
68#define FD_EV_ACTIVE_R (1U << FD_EV_ACTIVE_R_BIT)
69#define FD_EV_ACTIVE_W (1U << FD_EV_ACTIVE_W_BIT)
Willy Tarreau7be79a42012-11-11 15:02:54 +010070#define FD_EV_ACTIVE_RW (FD_EV_ACTIVE_R | FD_EV_ACTIVE_W)
71
Willy Tarreaua135ea62020-02-21 16:26:19 +010072#define FD_EV_READY_R (1U << FD_EV_READY_R_BIT)
73#define FD_EV_READY_W (1U << FD_EV_READY_W_BIT)
Willy Tarreauf817e9f2014-01-10 16:58:45 +010074#define FD_EV_READY_RW (FD_EV_READY_R | FD_EV_READY_W)
75
Willy Tarreau77abb432019-09-06 18:27:02 +020076/* note that when FD_EV_SHUT is set, ACTIVE and READY are cleared */
Willy Tarreaua135ea62020-02-21 16:26:19 +010077#define FD_EV_SHUT_R (1U << FD_EV_SHUT_R_BIT)
78#define FD_EV_SHUT_W (1U << FD_EV_SHUT_W_BIT)
Willy Tarreau77abb432019-09-06 18:27:02 +020079#define FD_EV_SHUT_RW (FD_EV_SHUT_R | FD_EV_SHUT_W)
80
Willy Tarreau1ed37812020-02-26 16:12:45 +010081/* note that when FD_EV_ERR is set, SHUT is also set. Also, ERR is for both
82 * directions at once (write error, socket dead, etc).
83 */
84#define FD_EV_ERR_RW (1U << FD_EV_ERR_RW_BIT)
Willy Tarreau77abb432019-09-06 18:27:02 +020085
Willy Tarreau733b1322016-11-17 14:22:52 +010086
87/* This is the value used to mark a file descriptor as dead. This value is
88 * negative, this is important so that tests on fd < 0 properly match. It
Joseph Herlantf69b8072018-11-25 13:34:43 -080089 * also has the nice property of being highly negative but neither overflowing
90 * nor changing sign on 32-bit machines when multiplied by sizeof(fdtab).
Willy Tarreau733b1322016-11-17 14:22:52 +010091 * This ensures that any unexpected dereference of such an uninitialized
92 * file descriptor will lead to so large a dereference that it will crash
93 * the process at the exact location of the bug with a clean stack trace
94 * instead of causing silent manipulation of other FDs. And it's readable
95 * when found in a dump.
96 */
97#define DEAD_FD_MAGIC 0xFDDEADFD
98
Olivier Houchard12568362018-01-31 18:07:29 +010099/* fdlist_entry: entry used by the fd cache.
100 * >= 0 means we're in the cache and gives the FD of the next in the cache,
101 * -1 means we're in the cache and the last element,
102 * -2 means the entry is locked,
103 * <= -3 means not in the cache, and next element is -4-fd
104 *
105 * It must remain 8-aligned so that aligned CAS operations may be done on both
106 * entries at once.
107 */
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100108struct fdlist_entry {
Olivier Houchard12568362018-01-31 18:07:29 +0100109 int next;
110 int prev;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100111} __attribute__ ((aligned(8)));
112
Olivier Houchard12568362018-01-31 18:07:29 +0100113/* head of the fd cache */
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100114struct fdlist {
Olivier Houchard12568362018-01-31 18:07:29 +0100115 int first;
116 int last;
Olivier Houchard4815c8c2018-01-24 18:17:56 +0100117} __attribute__ ((aligned(8)));
118
Willy Tarreaubaaee002006-06-26 02:48:02 +0200119/* info about one given fd */
120struct fdtab {
Willy Tarreau58090522017-11-26 10:41:47 +0100121 __decl_hathreads(HA_SPINLOCK_T lock);
122 unsigned long thread_mask; /* mask of thread IDs authorized to process the task */
Willy Tarreauebc78d72018-01-20 23:53:50 +0100123 unsigned long update_mask; /* mask of thread IDs having an update for fd */
Olivier Houchard6b96f722018-04-25 16:58:25 +0200124 struct fdlist_entry update; /* Entry in the global update list */
Willy Tarreau7a798e52016-04-14 11:13:20 +0200125 void (*iocb)(int fd); /* I/O handler */
Willy Tarreau80184712012-07-06 14:54:49 +0200126 void *owner; /* the connection or listener associated with this fd, NULL if closed */
Willy Tarreaua135ea62020-02-21 16:26:19 +0100127 unsigned char state; /* FD state for read and write directions (FD_EV_*) */
Willy Tarreau3d32d3a2007-04-15 11:31:05 +0200128 unsigned char ev; /* event seen in return of poll() : FD_POLL_* */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100129 unsigned char linger_risk:1; /* 1 if we must kill lingering before closing */
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200130 unsigned char cloned:1; /* 1 if a cloned socket, requires EPOLL_CTL_DEL on close */
Willy Tarreaud6609902019-08-30 14:36:10 +0200131 unsigned char initialized:1; /* 1 if init phase was done on this fd (e.g. set non-blocking) */
Willy Tarreau76913d32019-08-30 14:33:11 +0200132}
133#ifdef USE_THREAD
134/* only align on cache lines when using threads; 32-bit small archs
135 * can put everything in 32-bytes when threads are disabled.
136 */
137__attribute__((aligned(64)))
138#endif
139;
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200140
141/* less often used information */
142struct fdinfo {
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200143 struct port_range *port_range; /* optional port range to bind to */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200144 int local_port; /* optional local port */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200145};
146
Willy Tarreau4f60f162007-04-08 16:39:58 +0200147/*
148 * Poller descriptors.
149 * - <name> is initialized by the poller's register() function, and should not
150 * be allocated, just linked to.
151 * - <pref> is initialized by the poller's register() function. It is set to 0
152 * by default, meaning the poller is disabled. init() should set it to 0 in
153 * case of failure. term() must set it to 0. A generic unoptimized select()
154 * poller should set it to 100.
155 * - <private> is initialized by the poller's init() function, and cleaned by
156 * the term() function.
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100157 * - clo() should be used to do indicate the poller that fd will be closed.
Willy Tarreau2ae84e42019-05-28 16:44:05 +0200158 * - poll() calls the poller, expiring at <exp>, or immediately if <wake> is set
Willy Tarreau5a767692017-03-13 11:38:28 +0100159 * - flags indicate what the poller supports (HAP_POLL_F_*)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200160 */
Willy Tarreau5a767692017-03-13 11:38:28 +0100161
Willy Tarreau11ef0832019-11-28 18:17:33 +0100162#define HAP_POLL_F_RDHUP 0x00000001 /* the poller notifies of HUP with reads */
163#define HAP_POLL_F_ERRHUP 0x00000002 /* the poller reports ERR and HUP */
Willy Tarreau5a767692017-03-13 11:38:28 +0100164
Willy Tarreau4f60f162007-04-08 16:39:58 +0200165struct poller {
166 void *private; /* any private data for the poller */
Willy Tarreau03e78532020-02-25 07:38:05 +0100167 void (*clo)(const int fd); /* mark <fd> as closed */
168 void (*poll)(struct poller *p, int exp, int wake); /* the poller itself */
169 int (*init)(struct poller *p); /* poller initialization */
170 void (*term)(struct poller *p); /* termination of this poller */
171 int (*test)(struct poller *p); /* pre-init check of the poller */
172 int (*fork)(struct poller *p); /* post-fork re-opening */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200173 const char *name; /* poller name */
Willy Tarreau5a767692017-03-13 11:38:28 +0100174 unsigned int flags; /* HAP_POLL_F_* */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200175 int pref; /* try pollers with higher preference first */
176};
177
178extern struct poller cur_poller; /* the current poller */
179extern int nbpollers;
180#define MAX_POLLERS 10
181extern struct poller pollers[MAX_POLLERS]; /* all registered pollers */
182
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183extern struct fdtab *fdtab; /* array of all the file descriptors */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200184extern struct fdinfo *fdinfo; /* less-often used infos for file descriptors */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185extern int totalconn; /* total # of terminated sessions */
186extern int actconn; /* # of active sessions */
187
188#endif /* _TYPES_FD_H */
189
190/*
191 * Local variables:
192 * c-indent-level: 8
193 * c-basic-offset: 8
194 * End:
195 */