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> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 26 | #include <types/port_range.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 27 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 28 | /* Direction for each FD event update */ |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 29 | enum { |
| 30 | DIR_RD=0, |
| 31 | DIR_WR=1, |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 32 | }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 34 | /* Polling status flags returned in fdtab[].ev : |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 35 | * FD_POLL_IN remains set as long as some data is pending for read. |
| 36 | * FD_POLL_OUT remains set as long as the fd accepts to write data. |
| 37 | * FD_POLL_ERR and FD_POLL_ERR remain set forever (until processed). |
| 38 | */ |
Willy Tarreau | 3d32d3a | 2007-04-15 11:31:05 +0200 | [diff] [blame] | 39 | #define FD_POLL_IN 0x01 |
| 40 | #define FD_POLL_PRI 0x02 |
| 41 | #define FD_POLL_OUT 0x04 |
| 42 | #define FD_POLL_ERR 0x08 |
| 43 | #define FD_POLL_HUP 0x10 |
Willy Tarreau | 3d32d3a | 2007-04-15 11:31:05 +0200 | [diff] [blame] | 44 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 45 | #define FD_POLL_DATA (FD_POLL_IN | FD_POLL_OUT) |
| 46 | #define FD_POLL_STICKY (FD_POLL_ERR | FD_POLL_HUP) |
Willy Tarreau | 3d32d3a | 2007-04-15 11:31:05 +0200 | [diff] [blame] | 47 | |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 48 | #define FD_EV_ACTIVE 1U |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 49 | #define FD_EV_READY 2U |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 50 | #define FD_EV_POLLED 4U |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 51 | |
| 52 | #define FD_EV_STATUS (FD_EV_ACTIVE | FD_EV_POLLED | FD_EV_READY) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 53 | #define FD_EV_STATUS_R (FD_EV_STATUS) |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 54 | #define FD_EV_STATUS_W (FD_EV_STATUS << 4) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 55 | |
| 56 | #define FD_EV_POLLED_R (FD_EV_POLLED) |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 57 | #define FD_EV_POLLED_W (FD_EV_POLLED << 4) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 58 | #define FD_EV_POLLED_RW (FD_EV_POLLED_R | FD_EV_POLLED_W) |
| 59 | |
| 60 | #define FD_EV_ACTIVE_R (FD_EV_ACTIVE) |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 61 | #define FD_EV_ACTIVE_W (FD_EV_ACTIVE << 4) |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 62 | #define FD_EV_ACTIVE_RW (FD_EV_ACTIVE_R | FD_EV_ACTIVE_W) |
| 63 | |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 64 | #define FD_EV_READY_R (FD_EV_READY) |
| 65 | #define FD_EV_READY_W (FD_EV_READY << 4) |
| 66 | #define FD_EV_READY_RW (FD_EV_READY_R | FD_EV_READY_W) |
| 67 | |
| 68 | enum fd_states { |
| 69 | FD_ST_DISABLED = 0, |
| 70 | FD_ST_MUSTPOLL, |
| 71 | FD_ST_STOPPED, |
| 72 | FD_ST_ACTIVE, |
| 73 | FD_ST_ABORT, |
| 74 | FD_ST_POLLED, |
| 75 | FD_ST_PAUSED, |
| 76 | FD_ST_READY |
| 77 | }; |
Willy Tarreau | 7be79a4 | 2012-11-11 15:02:54 +0100 | [diff] [blame] | 78 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 79 | /* info about one given fd */ |
| 80 | struct fdtab { |
Willy Tarreau | 0aa1e33 | 2016-04-14 11:13:20 +0200 | [diff] [blame^] | 81 | void (*iocb)(int fd); /* I/O handler */ |
Willy Tarreau | 8018471 | 2012-07-06 14:54:49 +0200 | [diff] [blame] | 82 | void *owner; /* the connection or listener associated with this fd, NULL if closed */ |
Willy Tarreau | 15a4dec | 2014-01-20 11:09:39 +0100 | [diff] [blame] | 83 | unsigned int cache; /* position+1 in the FD cache. 0=not in cache. */ |
Willy Tarreau | f817e9f | 2014-01-10 16:58:45 +0100 | [diff] [blame] | 84 | 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] | 85 | unsigned char ev; /* event seen in return of poll() : FD_POLL_* */ |
Willy Tarreau | 037d2c1 | 2012-11-06 02:34:46 +0100 | [diff] [blame] | 86 | unsigned char new:1; /* 1 if this fd has just been created */ |
| 87 | unsigned char updated:1; /* 1 if this fd is already in the update list */ |
Willy Tarreau | ad38ace | 2013-12-15 14:19:38 +0100 | [diff] [blame] | 88 | 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] | 89 | unsigned char cloned:1; /* 1 if a cloned socket, requires EPOLL_CTL_DEL on close */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | /* less often used information */ |
| 93 | struct fdinfo { |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 94 | struct port_range *port_range; /* optional port range to bind to */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 95 | int local_port; /* optional local port */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 96 | }; |
| 97 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 98 | /* |
| 99 | * Poller descriptors. |
| 100 | * - <name> is initialized by the poller's register() function, and should not |
| 101 | * be allocated, just linked to. |
| 102 | * - <pref> is initialized by the poller's register() function. It is set to 0 |
| 103 | * by default, meaning the poller is disabled. init() should set it to 0 in |
| 104 | * case of failure. term() must set it to 0. A generic unoptimized select() |
| 105 | * poller should set it to 100. |
| 106 | * - <private> is initialized by the poller's init() function, and cleaned by |
| 107 | * the term() function. |
Willy Tarreau | 70c6fd8 | 2012-11-11 21:02:34 +0100 | [diff] [blame] | 108 | * - clo() should be used to do indicate the poller that fd will be closed. |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 109 | * - poll() calls the poller, expiring at <exp> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 110 | */ |
| 111 | struct poller { |
| 112 | void *private; /* any private data for the poller */ |
Willy Tarreau | 70c6fd8 | 2012-11-11 21:02:34 +0100 | [diff] [blame] | 113 | void REGPRM1 (*clo)(const int fd); /* mark <fd> as closed */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 114 | void REGPRM2 (*poll)(struct poller *p, int exp); /* the poller itself */ |
Willy Tarreau | 5b70242 | 2007-04-16 01:33:26 +0200 | [diff] [blame] | 115 | int REGPRM1 (*init)(struct poller *p); /* poller initialization */ |
| 116 | void REGPRM1 (*term)(struct poller *p); /* termination of this poller */ |
| 117 | int REGPRM1 (*test)(struct poller *p); /* pre-init check of the poller */ |
| 118 | int REGPRM1 (*fork)(struct poller *p); /* post-fork re-opening */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 119 | const char *name; /* poller name */ |
| 120 | int pref; /* try pollers with higher preference first */ |
| 121 | }; |
| 122 | |
| 123 | extern struct poller cur_poller; /* the current poller */ |
| 124 | extern int nbpollers; |
| 125 | #define MAX_POLLERS 10 |
| 126 | extern struct poller pollers[MAX_POLLERS]; /* all registered pollers */ |
| 127 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 128 | extern struct fdtab *fdtab; /* array of all the file descriptors */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 129 | extern struct fdinfo *fdinfo; /* less-often used infos for file descriptors */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 130 | extern int maxfd; /* # of the highest fd + 1 */ |
| 131 | extern int totalconn; /* total # of terminated sessions */ |
| 132 | extern int actconn; /* # of active sessions */ |
| 133 | |
| 134 | #endif /* _TYPES_FD_H */ |
| 135 | |
| 136 | /* |
| 137 | * Local variables: |
| 138 | * c-indent-level: 8 |
| 139 | * c-basic-offset: 8 |
| 140 | * End: |
| 141 | */ |