blob: f69886354b6abccb2aa8e3972fb8896edc109fbb [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau8d5d77e2009-10-18 07:25:52 +02002 * include/types/fd.h
3 * File descriptors states.
4 *
5 * Copyright (C) 2000-2009 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 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020021
22#ifndef _TYPES_FD_H
23#define _TYPES_FD_H
24
Willy Tarreaue94ebd02007-10-09 17:14:37 +020025#include <sys/socket.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020026#include <sys/time.h>
27#include <sys/types.h>
28#include <unistd.h>
29
Willy Tarreau2dd0d472006-06-29 17:53:05 +020030#include <common/config.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020031#include <types/task.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020032#include <types/protocols.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020033
34/* different possible states for the fd */
35#define FD_STCLOSE 0
36#define FD_STLISTEN 1
37#define FD_STCONN 2
38#define FD_STREADY 3
39#define FD_STERROR 4
40
Willy Tarreau54469402006-07-29 16:59:06 +020041enum {
42 DIR_RD=0,
43 DIR_WR=1,
44 DIR_SIZE
45};
Willy Tarreaubaaee002006-06-26 02:48:02 +020046
Willy Tarreaud6f087e2008-01-18 17:20:13 +010047/*
48 * FD_POLL_IN remains set as long as some data is pending for read.
49 * FD_POLL_OUT remains set as long as the fd accepts to write data.
50 * FD_POLL_ERR and FD_POLL_ERR remain set forever (until processed).
51 */
Willy Tarreau3d32d3a2007-04-15 11:31:05 +020052#define FD_POLL_IN 0x01
53#define FD_POLL_PRI 0x02
54#define FD_POLL_OUT 0x04
55#define FD_POLL_ERR 0x08
56#define FD_POLL_HUP 0x10
Willy Tarreau3d32d3a2007-04-15 11:31:05 +020057
Willy Tarreaud6f087e2008-01-18 17:20:13 +010058#define FD_POLL_DATA (FD_POLL_IN | FD_POLL_OUT)
59#define FD_POLL_STICKY (FD_POLL_ERR | FD_POLL_HUP)
Willy Tarreau3d32d3a2007-04-15 11:31:05 +020060
Willy Tarreaufb14edc2009-06-14 15:24:37 +020061/* bit values for fdtab[fd]->flags. Most of them are used to hold a value
62 * consecutive to a behaviour change.
63 */
64#define FD_FL_TCP 0x0001 /* socket is TCP */
65#define FD_FL_TCP_NODELAY 0x0002
Willy Tarreau6db06d32009-08-19 11:14:11 +020066#define FD_FL_TCP_NOLING 0x0004 /* lingering disabled */
Willy Tarreaufb14edc2009-06-14 15:24:37 +020067
Willy Tarreaubaaee002006-06-26 02:48:02 +020068/* info about one given fd */
69struct fdtab {
Willy Tarreau54469402006-07-29 16:59:06 +020070 struct {
71 int (*f)(int fd); /* read/write function */
72 struct buffer *b; /* read/write buffer */
73 } cb[DIR_SIZE];
Willy Tarreaueabf3132008-08-29 23:36:51 +020074 void *owner; /* the session (or proxy) associated with this fd */
Willy Tarreaub48b3232009-10-17 22:54:17 +020075 struct { /* used by pollers which support speculative polling */
76 unsigned char e; /* read and write events status. 4 bits, may be merged into flags' lower bits */
77 unsigned int s1; /* Position in spec list+1. 0=not in list. */
78 } spec;
Willy Tarreaufb14edc2009-06-14 15:24:37 +020079 unsigned short flags; /* various flags precising the exact status of this fd */
Willy Tarreau3d32d3a2007-04-15 11:31:05 +020080 unsigned char state; /* the state of this fd */
81 unsigned char ev; /* event seen in return of poll() : FD_POLL_* */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +020082};
83
84/* less often used information */
85struct fdinfo {
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020086 struct port_range *port_range; /* optional port range to bind to */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +020087 int local_port; /* optional local port */
88 struct sockaddr *peeraddr; /* pointer to peer's network address, or NULL if unset */
89 socklen_t peerlen; /* peer's address length, or 0 if unset */
Willy Tarreaubaaee002006-06-26 02:48:02 +020090};
91
Willy Tarreau4f60f162007-04-08 16:39:58 +020092/*
93 * Poller descriptors.
94 * - <name> is initialized by the poller's register() function, and should not
95 * be allocated, just linked to.
96 * - <pref> is initialized by the poller's register() function. It is set to 0
97 * by default, meaning the poller is disabled. init() should set it to 0 in
98 * case of failure. term() must set it to 0. A generic unoptimized select()
99 * poller should set it to 100.
100 * - <private> is initialized by the poller's init() function, and cleaned by
101 * the term() function.
Willy Tarreau97129b52007-04-09 00:54:46 +0200102 * - cond_s() checks if fd was not set then sets it and returns 1. Otherwise
103 * it returns 0. It may be the same as set().
104 * - cond_c() checks if fd was set then clears it and returns 1. Otherwise
105 * it returns 0. It may be the same as clr().
Willy Tarreau4f60f162007-04-08 16:39:58 +0200106 * - clo() should be used to do indicate the poller that fd will be closed. It
107 * may be the same as rem() on some pollers.
Willy Tarreaud825eef2007-05-12 22:35:00 +0200108 * - poll() calls the poller, expiring at <exp>
Willy Tarreau4f60f162007-04-08 16:39:58 +0200109 */
110struct poller {
111 void *private; /* any private data for the poller */
Willy Tarreau5b702422007-04-16 01:33:26 +0200112 int REGPRM2 (*is_set)(const int fd, int dir); /* check if <fd> is being polled for dir <dir> */
113 int REGPRM2 (*set)(const int fd, int dir); /* set polling on <fd> for <dir> */
114 int REGPRM2 (*clr)(const int fd, int dir); /* clear polling on <fd> for <dir> */
115 int REGPRM2 (*cond_s)(const int fd, int dir); /* set polling on <fd> for <dir> if unset */
116 int REGPRM2 (*cond_c)(const int fd, int dir); /* clear polling on <fd> for <dir> if set */
117 void REGPRM1 (*rem)(const int fd); /* remove any polling on <fd> */
118 void REGPRM1 (*clo)(const int fd); /* mark <fd> as closed */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200119 void REGPRM2 (*poll)(struct poller *p, int exp); /* the poller itself */
Willy Tarreau5b702422007-04-16 01:33:26 +0200120 int REGPRM1 (*init)(struct poller *p); /* poller initialization */
121 void REGPRM1 (*term)(struct poller *p); /* termination of this poller */
122 int REGPRM1 (*test)(struct poller *p); /* pre-init check of the poller */
123 int REGPRM1 (*fork)(struct poller *p); /* post-fork re-opening */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200124 const char *name; /* poller name */
125 int pref; /* try pollers with higher preference first */
126};
127
128extern struct poller cur_poller; /* the current poller */
129extern int nbpollers;
130#define MAX_POLLERS 10
131extern struct poller pollers[MAX_POLLERS]; /* all registered pollers */
132
Willy Tarreaubaaee002006-06-26 02:48:02 +0200133extern struct fdtab *fdtab; /* array of all the file descriptors */
Willy Tarreau8d5d77e2009-10-18 07:25:52 +0200134extern struct fdinfo *fdinfo; /* less-often used infos for file descriptors */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200135extern int maxfd; /* # of the highest fd + 1 */
136extern int totalconn; /* total # of terminated sessions */
137extern int actconn; /* # of active sessions */
138
139#endif /* _TYPES_FD_H */
140
141/*
142 * Local variables:
143 * c-indent-level: 8
144 * c-basic-offset: 8
145 * End:
146 */