1 /*
2  * include/types/fd.h
3  * File descriptors states - check src/fd.c for explanations.
4  *
5  * Copyright (C) 2000-2014 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_FD_H
23 #define _TYPES_FD_H
24 
25 #include <common/config.h>
26 #include <common/hathreads.h>
27 #include <types/port_range.h>
28 
29 /* Direction for each FD event update */
30 enum {
31 	DIR_RD=0,
32 	DIR_WR=1,
33 };
34 
35 /* Polling status flags returned in fdtab[].ev :
36  * FD_POLL_IN remains set as long as some data is pending for read.
37  * FD_POLL_OUT remains set as long as the fd accepts to write data.
38  * FD_POLL_ERR and FD_POLL_ERR remain set forever (until processed).
39  */
40 #define FD_POLL_IN	0x01
41 #define FD_POLL_PRI	0x02
42 #define FD_POLL_OUT	0x04
43 #define FD_POLL_ERR	0x08
44 #define FD_POLL_HUP	0x10
45 
46 #define FD_POLL_DATA    (FD_POLL_IN  | FD_POLL_OUT)
47 #define FD_POLL_STICKY  (FD_POLL_ERR | FD_POLL_HUP)
48 
49 #define FD_EV_ACTIVE    1U
50 #define FD_EV_READY     2U
51 #define FD_EV_POLLED    4U
52 
53 /* bits positions for a few flags */
54 #define FD_EV_READY_R_BIT 1
55 #define FD_EV_READY_W_BIT 5
56 
57 #define FD_EV_STATUS    (FD_EV_ACTIVE | FD_EV_POLLED | FD_EV_READY)
58 #define FD_EV_STATUS_R  (FD_EV_STATUS)
59 #define FD_EV_STATUS_W  (FD_EV_STATUS << 4)
60 
61 #define FD_EV_POLLED_R  (FD_EV_POLLED)
62 #define FD_EV_POLLED_W  (FD_EV_POLLED << 4)
63 #define FD_EV_POLLED_RW (FD_EV_POLLED_R | FD_EV_POLLED_W)
64 
65 #define FD_EV_ACTIVE_R  (FD_EV_ACTIVE)
66 #define FD_EV_ACTIVE_W  (FD_EV_ACTIVE << 4)
67 #define FD_EV_ACTIVE_RW (FD_EV_ACTIVE_R | FD_EV_ACTIVE_W)
68 
69 #define FD_EV_READY_R   (FD_EV_READY)
70 #define FD_EV_READY_W   (FD_EV_READY << 4)
71 #define FD_EV_READY_RW  (FD_EV_READY_R | FD_EV_READY_W)
72 
73 enum fd_states {
74 	FD_ST_DISABLED = 0,
75 	FD_ST_MUSTPOLL,
76 	FD_ST_STOPPED,
77 	FD_ST_ACTIVE,
78 	FD_ST_ABORT,
79 	FD_ST_POLLED,
80 	FD_ST_PAUSED,
81 	FD_ST_READY
82 };
83 
84 
85 /* This is the value used to mark a file descriptor as dead. This value is
86  * negative, this is important so that tests on fd < 0 properly match. It
87  * also has the nice property of being highly negative but neither overflowing
88  * nor changing sign on 32-bit machines when multiplied by sizeof(fdtab).
89  * This ensures that any unexpected dereference of such an uninitialized
90  * file descriptor will lead to so large a dereference that it will crash
91  * the process at the exact location of the bug with a clean stack trace
92  * instead of causing silent manipulation of other FDs. And it's readable
93  * when found in a dump.
94  */
95 #define DEAD_FD_MAGIC 0xFDDEADFD
96 
97 /* fdlist_entry: entry used by the fd cache.
98  *    >= 0 means we're in the cache and gives the FD of the next in the cache,
99  *      -1 means we're in the cache and the last element,
100  *      -2 means the entry is locked,
101  *   <= -3 means not in the cache, and next element is -4-fd
102  *
103  * It must remain 8-aligned so that aligned CAS operations may be done on both
104  * entries at once.
105  */
106 struct fdlist_entry {
107 	int next;
108 	int prev;
109 } __attribute__ ((aligned(8)));
110 
111 /* head of the fd cache */
112 struct fdlist {
113 	int first;
114 	int last;
115 } __attribute__ ((aligned(8)));
116 
117 /* info about one given fd */
118 struct fdtab {
119 	__decl_hathreads(HA_SPINLOCK_T lock);
120 	unsigned long thread_mask;           /* mask of thread IDs authorized to process the task */
121 	unsigned long update_mask;           /* mask of thread IDs having an update for fd */
122 	struct fdlist_entry cache;           /* Entry in the fdcache */
123 	struct fdlist_entry update;          /* Entry in the global update list */
124 	void (*iocb)(int fd);                /* I/O handler */
125 	void *owner;                         /* the connection or listener associated with this fd, NULL if closed */
126 	unsigned char state;                 /* FD state for read and write directions (2*3 bits) */
127 	unsigned char ev;                    /* event seen in return of poll() : FD_POLL_* */
128 	unsigned char linger_risk:1;         /* 1 if we must kill lingering before closing */
129 	unsigned char cloned:1;              /* 1 if a cloned socket, requires EPOLL_CTL_DEL on close */
130 };
131 
132 /* less often used information */
133 struct fdinfo {
134 	struct port_range *port_range;       /* optional port range to bind to */
135 	int local_port;                      /* optional local port */
136 };
137 
138 /*
139  * Poller descriptors.
140  *  - <name> is initialized by the poller's register() function, and should not
141  *    be allocated, just linked to.
142  *  - <pref> is initialized by the poller's register() function. It is set to 0
143  *    by default, meaning the poller is disabled. init() should set it to 0 in
144  *    case of failure. term() must set it to 0. A generic unoptimized select()
145  *    poller should set it to 100.
146  *  - <private> is initialized by the poller's init() function, and cleaned by
147  *    the term() function.
148  *  - clo() should be used to do indicate the poller that fd will be closed.
149  *  - poll() calls the poller, expiring at <exp>, or immediately if <wake> is set
150  *  - flags indicate what the poller supports (HAP_POLL_F_*)
151  */
152 
153 #define HAP_POLL_F_RDHUP 0x00000001                          /* the poller notifies of HUP with reads */
154 
155 struct poller {
156 	void   *private;                                     /* any private data for the poller */
157 	void REGPRM1   (*clo)(const int fd);                 /* mark <fd> as closed */
158 	void REGPRM3   (*poll)(struct poller *p, int exp, int wake);  /* the poller itself */
159 	int  REGPRM1   (*init)(struct poller *p);            /* poller initialization */
160 	void REGPRM1   (*term)(struct poller *p);            /* termination of this poller */
161 	int  REGPRM1   (*test)(struct poller *p);            /* pre-init check of the poller */
162 	int  REGPRM1   (*fork)(struct poller *p);            /* post-fork re-opening */
163 	const char   *name;                                  /* poller name */
164 	unsigned int flags;                                  /* HAP_POLL_F_* */
165 	int    pref;                                         /* try pollers with higher preference first */
166 };
167 
168 extern struct poller cur_poller; /* the current poller */
169 extern int nbpollers;
170 #define MAX_POLLERS	10
171 extern struct poller pollers[MAX_POLLERS];   /* all registered pollers */
172 
173 extern struct fdtab *fdtab;             /* array of all the file descriptors */
174 extern struct fdinfo *fdinfo;           /* less-often used infos for file descriptors */
175 extern int totalconn;                   /* total # of terminated sessions */
176 extern int actconn;                     /* # of active sessions */
177 
178 #endif /* _TYPES_FD_H */
179 
180 /*
181  * Local variables:
182  *  c-indent-level: 8
183  *  c-basic-offset: 8
184  * End:
185  */
186