blob: 2bd7c07f2a17887d22fff3c25a366cee04602345 [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>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020026#include <types/port_range.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020027
Willy Tarreau7be79a42012-11-11 15:02:54 +010028/* Direction for each FD event update */
Willy Tarreau54469402006-07-29 16:59:06 +020029enum {
30 DIR_RD=0,
31 DIR_WR=1,
Willy Tarreau54469402006-07-29 16:59:06 +020032};
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
Willy Tarreau7be79a42012-11-11 15:02:54 +010034/* Polling status flags returned in fdtab[].ev :
Willy Tarreaud6f087e2008-01-18 17:20:13 +010035 * 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 Tarreau3d32d3a2007-04-15 11:31:05 +020039#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 Tarreau3d32d3a2007-04-15 11:31:05 +020044
Willy Tarreaud6f087e2008-01-18 17:20:13 +010045#define FD_POLL_DATA (FD_POLL_IN | FD_POLL_OUT)
46#define FD_POLL_STICKY (FD_POLL_ERR | FD_POLL_HUP)
Willy Tarreau3d32d3a2007-04-15 11:31:05 +020047
Willy Tarreau7be79a42012-11-11 15:02:54 +010048#define FD_EV_ACTIVE 1U
Willy Tarreauf817e9f2014-01-10 16:58:45 +010049#define FD_EV_READY 2U
Willy Tarreau7be79a42012-11-11 15:02:54 +010050#define FD_EV_POLLED 4U
Willy Tarreauf817e9f2014-01-10 16:58:45 +010051
52#define FD_EV_STATUS (FD_EV_ACTIVE | FD_EV_POLLED | FD_EV_READY)
Willy Tarreau7be79a42012-11-11 15:02:54 +010053#define FD_EV_STATUS_R (FD_EV_STATUS)
Willy Tarreauf817e9f2014-01-10 16:58:45 +010054#define FD_EV_STATUS_W (FD_EV_STATUS << 4)
Willy Tarreau7be79a42012-11-11 15:02:54 +010055
56#define FD_EV_POLLED_R (FD_EV_POLLED)
Willy Tarreauf817e9f2014-01-10 16:58:45 +010057#define FD_EV_POLLED_W (FD_EV_POLLED << 4)
Willy Tarreau7be79a42012-11-11 15:02:54 +010058#define FD_EV_POLLED_RW (FD_EV_POLLED_R | FD_EV_POLLED_W)
59
60#define FD_EV_ACTIVE_R (FD_EV_ACTIVE)
Willy Tarreauf817e9f2014-01-10 16:58:45 +010061#define FD_EV_ACTIVE_W (FD_EV_ACTIVE << 4)
Willy Tarreau7be79a42012-11-11 15:02:54 +010062#define FD_EV_ACTIVE_RW (FD_EV_ACTIVE_R | FD_EV_ACTIVE_W)
63
Willy Tarreauf817e9f2014-01-10 16:58:45 +010064#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
68enum 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 Tarreau7be79a42012-11-11 15:02:54 +010078
Willy Tarreau733b1322016-11-17 14:22:52 +010079
80/* This is the value used to mark a file descriptor as dead. This value is
81 * negative, this is important so that tests on fd < 0 properly match. It
82 * also has the nice property of being highly negative but not overflowing
83 * nor changing sign on 32-bit machines when multipled by sizeof(fdtab).
84 * This ensures that any unexpected dereference of such an uninitialized
85 * file descriptor will lead to so large a dereference that it will crash
86 * the process at the exact location of the bug with a clean stack trace
87 * instead of causing silent manipulation of other FDs. And it's readable
88 * when found in a dump.
89 */
90#define DEAD_FD_MAGIC 0xFDDEADFD
91
Willy Tarreaubaaee002006-06-26 02:48:02 +020092/* info about one given fd */
93struct fdtab {
Willy Tarreau7a798e52016-04-14 11:13:20 +020094 void (*iocb)(int fd); /* I/O handler */
Willy Tarreau80184712012-07-06 14:54:49 +020095 void *owner; /* the connection or listener associated with this fd, NULL if closed */
Willy Tarreau15a4dec2014-01-20 11:09:39 +010096 unsigned int cache; /* position+1 in the FD cache. 0=not in cache. */
Willy Tarreauf817e9f2014-01-10 16:58:45 +010097 unsigned char state; /* FD state for read and write directions (2*3 bits) */
Willy Tarreau3d32d3a2007-04-15 11:31:05 +020098 unsigned char ev; /* event seen in return of poll() : FD_POLL_* */
Willy Tarreau037d2c12012-11-06 02:34:46 +010099 unsigned char new:1; /* 1 if this fd has just been created */
100 unsigned char updated:1; /* 1 if this fd is already in the update list */
Willy Tarreauad38ace2013-12-15 14:19:38 +0100101 unsigned char linger_risk:1; /* 1 if we must kill lingering before closing */
Conrad Hoffmann041751c2014-05-20 14:28:24 +0200102 unsigned char cloned:1; /* 1 if a cloned socket, requires EPOLL_CTL_DEL on close */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200103};
104
105/* less often used information */
106struct fdinfo {
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200107 struct port_range *port_range; /* optional port range to bind to */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200108 int local_port; /* optional local port */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109};
110
Willy Tarreau4f60f162007-04-08 16:39:58 +0200111/*
112 * Poller descriptors.
113 * - <name> is initialized by the poller's register() function, and should not
114 * be allocated, just linked to.
115 * - <pref> is initialized by the poller's register() function. It is set to 0
116 * by default, meaning the poller is disabled. init() should set it to 0 in
117 * case of failure. term() must set it to 0. A generic unoptimized select()
118 * poller should set it to 100.
119 * - <private> is initialized by the poller's init() function, and cleaned by
120 * the term() function.
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100121 * - clo() should be used to do indicate the poller that fd will be closed.
Willy Tarreaud825eef2007-05-12 22:35:00 +0200122 * - poll() calls the poller, expiring at <exp>
Willy Tarreau5a767692017-03-13 11:38:28 +0100123 * - flags indicate what the poller supports (HAP_POLL_F_*)
Willy Tarreau4f60f162007-04-08 16:39:58 +0200124 */
Willy Tarreau5a767692017-03-13 11:38:28 +0100125
126#define HAP_POLL_F_RDHUP 0x00000001 /* the poller notifies of HUP with reads */
127
Willy Tarreau4f60f162007-04-08 16:39:58 +0200128struct poller {
129 void *private; /* any private data for the poller */
Willy Tarreau70c6fd82012-11-11 21:02:34 +0100130 void REGPRM1 (*clo)(const int fd); /* mark <fd> as closed */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200131 void REGPRM2 (*poll)(struct poller *p, int exp); /* the poller itself */
Willy Tarreau5b702422007-04-16 01:33:26 +0200132 int REGPRM1 (*init)(struct poller *p); /* poller initialization */
133 void REGPRM1 (*term)(struct poller *p); /* termination of this poller */
134 int REGPRM1 (*test)(struct poller *p); /* pre-init check of the poller */
135 int REGPRM1 (*fork)(struct poller *p); /* post-fork re-opening */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200136 const char *name; /* poller name */
Willy Tarreau5a767692017-03-13 11:38:28 +0100137 unsigned int flags; /* HAP_POLL_F_* */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200138 int pref; /* try pollers with higher preference first */
139};
140
141extern struct poller cur_poller; /* the current poller */
142extern int nbpollers;
143#define MAX_POLLERS 10
144extern struct poller pollers[MAX_POLLERS]; /* all registered pollers */
145
Willy Tarreaubaaee002006-06-26 02:48:02 +0200146extern struct fdtab *fdtab; /* array of all the file descriptors */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200147extern struct fdinfo *fdinfo; /* less-often used infos for file descriptors */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200148extern int maxfd; /* # of the highest fd + 1 */
149extern int totalconn; /* total # of terminated sessions */
150extern int actconn; /* # of active sessions */
151
152#endif /* _TYPES_FD_H */
153
154/*
155 * Local variables:
156 * c-indent-level: 8
157 * c-basic-offset: 8
158 * End:
159 */