Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 2 | * include/types/fd.h |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 3 | * File descriptors states - check src/fd.c for explanations. |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 4 | * |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 5 | * Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 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 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | |
| 22 | #ifndef _TYPES_FD_H |
| 23 | #define _TYPES_FD_H |
| 24 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 25 | #include <common/config.h> |
Christopher Faulet | d4604ad | 2017-05-29 10:40:41 +0200 | [diff] [blame] | 26 | #include <common/hathreads.h> |
Willy Tarreau | 931d8b7 | 2019-08-27 11:08:17 +0200 | [diff] [blame] | 27 | #include <common/ist.h> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 28 | #include <types/port_range.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 30 | /* Direction for each FD event update */ |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 31 | enum { |
| 32 | DIR_RD=0, |
| 33 | DIR_WR=1, |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 34 | }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 36 | /* Polling status flags returned in fdtab[].ev : |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 37 | * 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 Tarreau | 3d32d3a | 2007-04-15 11:31:05 +0200 | [diff] [blame] | 41 | #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 Tarreau | 3d32d3a | 2007-04-15 11:31:05 +0200 | [diff] [blame] | 46 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 47 | #define FD_POLL_DATA (FD_POLL_IN | FD_POLL_OUT) |
| 48 | #define FD_POLL_STICKY (FD_POLL_ERR | FD_POLL_HUP) |
Willy Tarreau | 3d32d3a | 2007-04-15 11:31:05 +0200 | [diff] [blame] | 49 | |
Willy Tarreau | 5bee3e2 | 2019-09-04 09:52:57 +0200 | [diff] [blame] | 50 | /* FD bits used for different polling states in each direction */ |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 51 | #define FD_EV_ACTIVE 1U |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 52 | #define FD_EV_READY 2U |
Willy Tarreau | 77abb43 | 2019-09-06 18:27:02 +0200 | [diff] [blame] | 53 | #define FD_EV_SHUT 4U |
| 54 | #define FD_EV_ERR 8U |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 55 | |
Willy Tarreau | 1dad384 | 2019-07-08 23:09:03 +0200 | [diff] [blame] | 56 | /* bits positions for a few flags */ |
Willy Tarreau | f8ecc7f | 2019-09-04 13:22:50 +0200 | [diff] [blame] | 57 | #define FD_EV_ACTIVE_R_BIT 0 |
Willy Tarreau | 77abb43 | 2019-09-06 18:27:02 +0200 | [diff] [blame] | 58 | #define FD_EV_READY_R_BIT 1 |
| 59 | #define FD_EV_SHUT_R_BIT 2 |
| 60 | #define FD_EV_ERR_R_BIT 3 |
Willy Tarreau | f8ecc7f | 2019-09-04 13:22:50 +0200 | [diff] [blame] | 61 | |
| 62 | #define FD_EV_ACTIVE_W_BIT 4 |
Willy Tarreau | 77abb43 | 2019-09-06 18:27:02 +0200 | [diff] [blame] | 63 | #define FD_EV_READY_W_BIT 5 |
| 64 | #define FD_EV_SHUT_W_BIT 6 |
| 65 | #define FD_EV_ERR_W_BIT 7 |
Willy Tarreau | 1dad384 | 2019-07-08 23:09:03 +0200 | [diff] [blame] | 66 | |
Willy Tarreau | 77abb43 | 2019-09-06 18:27:02 +0200 | [diff] [blame] | 67 | #define FD_EV_STATUS (FD_EV_ACTIVE | FD_EV_READY | FD_EV_SHUT | FD_EV_ERR) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 68 | #define FD_EV_STATUS_R (FD_EV_STATUS) |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 69 | #define FD_EV_STATUS_W (FD_EV_STATUS << 4) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 70 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 71 | #define FD_EV_ACTIVE_R (FD_EV_ACTIVE) |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 72 | #define FD_EV_ACTIVE_W (FD_EV_ACTIVE << 4) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 73 | #define FD_EV_ACTIVE_RW (FD_EV_ACTIVE_R | FD_EV_ACTIVE_W) |
| 74 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 75 | #define FD_EV_READY_R (FD_EV_READY) |
| 76 | #define FD_EV_READY_W (FD_EV_READY << 4) |
| 77 | #define FD_EV_READY_RW (FD_EV_READY_R | FD_EV_READY_W) |
| 78 | |
Willy Tarreau | 77abb43 | 2019-09-06 18:27:02 +0200 | [diff] [blame] | 79 | /* note that when FD_EV_SHUT is set, ACTIVE and READY are cleared */ |
| 80 | #define FD_EV_SHUT_R (FD_EV_SHUT) |
| 81 | #define FD_EV_SHUT_W (FD_EV_SHUT << 4) |
| 82 | #define FD_EV_SHUT_RW (FD_EV_SHUT_R | FD_EV_SHUT_W) |
| 83 | |
| 84 | /* note that when FD_EV_ERR is set, SHUT is also set */ |
| 85 | #define FD_EV_ERR_R (FD_EV_ERR) |
| 86 | #define FD_EV_ERR_W (FD_EV_ERR << 4) |
| 87 | #define FD_EV_ERR_RW (FD_EV_ERR_R | FD_EV_ERR_W) |
| 88 | |
Willy Tarreau | 733b132 | 2016-11-17 14:22:52 +0100 | [diff] [blame] | 89 | |
| 90 | /* This is the value used to mark a file descriptor as dead. This value is |
| 91 | * negative, this is important so that tests on fd < 0 properly match. It |
Joseph Herlant | f69b807 | 2018-11-25 13:34:43 -0800 | [diff] [blame] | 92 | * also has the nice property of being highly negative but neither overflowing |
| 93 | * nor changing sign on 32-bit machines when multiplied by sizeof(fdtab). |
Willy Tarreau | 733b132 | 2016-11-17 14:22:52 +0100 | [diff] [blame] | 94 | * This ensures that any unexpected dereference of such an uninitialized |
| 95 | * file descriptor will lead to so large a dereference that it will crash |
| 96 | * the process at the exact location of the bug with a clean stack trace |
| 97 | * instead of causing silent manipulation of other FDs. And it's readable |
| 98 | * when found in a dump. |
| 99 | */ |
| 100 | #define DEAD_FD_MAGIC 0xFDDEADFD |
| 101 | |
Olivier Houchard | 1256836 | 2018-01-31 18:07:29 +0100 | [diff] [blame] | 102 | /* fdlist_entry: entry used by the fd cache. |
| 103 | * >= 0 means we're in the cache and gives the FD of the next in the cache, |
| 104 | * -1 means we're in the cache and the last element, |
| 105 | * -2 means the entry is locked, |
| 106 | * <= -3 means not in the cache, and next element is -4-fd |
| 107 | * |
| 108 | * It must remain 8-aligned so that aligned CAS operations may be done on both |
| 109 | * entries at once. |
| 110 | */ |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 111 | struct fdlist_entry { |
Olivier Houchard | 1256836 | 2018-01-31 18:07:29 +0100 | [diff] [blame] | 112 | int next; |
| 113 | int prev; |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 114 | } __attribute__ ((aligned(8))); |
| 115 | |
Olivier Houchard | 1256836 | 2018-01-31 18:07:29 +0100 | [diff] [blame] | 116 | /* head of the fd cache */ |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 117 | struct fdlist { |
Olivier Houchard | 1256836 | 2018-01-31 18:07:29 +0100 | [diff] [blame] | 118 | int first; |
| 119 | int last; |
Olivier Houchard | 4815c8c | 2018-01-24 18:17:56 +0100 | [diff] [blame] | 120 | } __attribute__ ((aligned(8))); |
| 121 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 122 | /* info about one given fd */ |
| 123 | struct fdtab { |
Willy Tarreau | 5809052 | 2017-11-26 10:41:47 +0100 | [diff] [blame] | 124 | __decl_hathreads(HA_SPINLOCK_T lock); |
| 125 | unsigned long thread_mask; /* mask of thread IDs authorized to process the task */ |
Willy Tarreau | ebc78d7 | 2018-01-20 23:53:50 +0100 | [diff] [blame] | 126 | unsigned long update_mask; /* mask of thread IDs having an update for fd */ |
Olivier Houchard | 6b96f72 | 2018-04-25 16:58:25 +0200 | [diff] [blame] | 127 | struct fdlist_entry update; /* Entry in the global update list */ |
Willy Tarreau | 7a798e5 | 2016-04-14 11:13:20 +0200 | [diff] [blame] | 128 | void (*iocb)(int fd); /* I/O handler */ |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 129 | void *owner; /* the connection or listener associated with this fd, NULL if closed */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 130 | unsigned char state; /* FD state for read and write directions (2*3 bits) */ |
Willy Tarreau | 3d32d3a | 2007-04-15 11:31:05 +0200 | [diff] [blame] | 131 | unsigned char ev; /* event seen in return of poll() : FD_POLL_* */ |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 132 | unsigned char linger_risk:1; /* 1 if we must kill lingering before closing */ |
Conrad Hoffmann | 041751c | 2014-05-20 14:28:24 +0200 | [diff] [blame] | 133 | unsigned char cloned:1; /* 1 if a cloned socket, requires EPOLL_CTL_DEL on close */ |
Willy Tarreau | d660990 | 2019-08-30 14:36:10 +0200 | [diff] [blame] | 134 | unsigned char initialized:1; /* 1 if init phase was done on this fd (e.g. set non-blocking) */ |
Willy Tarreau | 76913d3 | 2019-08-30 14:33:11 +0200 | [diff] [blame] | 135 | } |
| 136 | #ifdef USE_THREAD |
| 137 | /* only align on cache lines when using threads; 32-bit small archs |
| 138 | * can put everything in 32-bytes when threads are disabled. |
| 139 | */ |
| 140 | __attribute__((aligned(64))) |
| 141 | #endif |
| 142 | ; |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 143 | |
| 144 | /* less often used information */ |
| 145 | struct fdinfo { |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 146 | struct port_range *port_range; /* optional port range to bind to */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 147 | int local_port; /* optional local port */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 148 | }; |
| 149 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 150 | /* |
| 151 | * Poller descriptors. |
| 152 | * - <name> is initialized by the poller's register() function, and should not |
| 153 | * be allocated, just linked to. |
| 154 | * - <pref> is initialized by the poller's register() function. It is set to 0 |
| 155 | * by default, meaning the poller is disabled. init() should set it to 0 in |
| 156 | * case of failure. term() must set it to 0. A generic unoptimized select() |
| 157 | * poller should set it to 100. |
| 158 | * - <private> is initialized by the poller's init() function, and cleaned by |
| 159 | * the term() function. |
Willy Tarreau | 70c6fd8 | 2012-11-11 21:02:34 +0100 | [diff] [blame] | 160 | * - clo() should be used to do indicate the poller that fd will be closed. |
Willy Tarreau | 2ae84e4 | 2019-05-28 16:44:05 +0200 | [diff] [blame] | 161 | * - poll() calls the poller, expiring at <exp>, or immediately if <wake> is set |
Willy Tarreau | 5a76769 | 2017-03-13 11:38:28 +0100 | [diff] [blame] | 162 | * - flags indicate what the poller supports (HAP_POLL_F_*) |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 163 | */ |
Willy Tarreau | 5a76769 | 2017-03-13 11:38:28 +0100 | [diff] [blame] | 164 | |
| 165 | #define HAP_POLL_F_RDHUP 0x00000001 /* the poller notifies of HUP with reads */ |
| 166 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 167 | struct poller { |
| 168 | void *private; /* any private data for the poller */ |
Willy Tarreau | 70c6fd8 | 2012-11-11 21:02:34 +0100 | [diff] [blame] | 169 | void REGPRM1 (*clo)(const int fd); /* mark <fd> as closed */ |
Willy Tarreau | 2ae84e4 | 2019-05-28 16:44:05 +0200 | [diff] [blame] | 170 | void REGPRM3 (*poll)(struct poller *p, int exp, int wake); /* the poller itself */ |
Willy Tarreau | 5b70242 | 2007-04-16 01:33:26 +0200 | [diff] [blame] | 171 | int REGPRM1 (*init)(struct poller *p); /* poller initialization */ |
| 172 | void REGPRM1 (*term)(struct poller *p); /* termination of this poller */ |
| 173 | int REGPRM1 (*test)(struct poller *p); /* pre-init check of the poller */ |
| 174 | int REGPRM1 (*fork)(struct poller *p); /* post-fork re-opening */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 175 | const char *name; /* poller name */ |
Willy Tarreau | 5a76769 | 2017-03-13 11:38:28 +0100 | [diff] [blame] | 176 | unsigned int flags; /* HAP_POLL_F_* */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 177 | int pref; /* try pollers with higher preference first */ |
| 178 | }; |
| 179 | |
| 180 | extern struct poller cur_poller; /* the current poller */ |
| 181 | extern int nbpollers; |
| 182 | #define MAX_POLLERS 10 |
| 183 | extern struct poller pollers[MAX_POLLERS]; /* all registered pollers */ |
| 184 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 185 | extern struct fdtab *fdtab; /* array of all the file descriptors */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 186 | extern struct fdinfo *fdinfo; /* less-often used infos for file descriptors */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 187 | extern int totalconn; /* total # of terminated sessions */ |
| 188 | extern int actconn; /* # of active sessions */ |
| 189 | |
| 190 | #endif /* _TYPES_FD_H */ |
| 191 | |
| 192 | /* |
| 193 | * Local variables: |
| 194 | * c-indent-level: 8 |
| 195 | * c-basic-offset: 8 |
| 196 | * End: |
| 197 | */ |