blob: 8ae0f2be1b4df965d15caa00b084edfb40d168a0 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreaue3ba5f02006-06-29 18:54:54 +02002 include/types/fd.h
Willy Tarreaubaaee002006-06-26 02:48:02 +02003 File descriptors states.
4
Willy Tarreau0c303ee2008-07-07 00:09:58 +02005 Copyright (C) 2000-2008 Willy Tarreau - w@1wt.eu
Willy Tarreaub5654f62008-12-07 16:45:10 +01006
Willy Tarreaubaaee002006-06-26 02:48:02 +02007 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
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 Tarreau54469402006-07-29 16:59:06 +020032#include <types/buffers.h>
Willy Tarreaudd815982007-10-16 12:25:14 +020033#include <types/protocols.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020034
35/* different possible states for the fd */
36#define FD_STCLOSE 0
37#define FD_STLISTEN 1
38#define FD_STCONN 2
39#define FD_STREADY 3
40#define FD_STERROR 4
41
Willy Tarreau54469402006-07-29 16:59:06 +020042enum {
43 DIR_RD=0,
44 DIR_WR=1,
45 DIR_SIZE
46};
Willy Tarreaubaaee002006-06-26 02:48:02 +020047
Willy Tarreaud6f087e2008-01-18 17:20:13 +010048/*
49 * FD_POLL_IN remains set as long as some data is pending for read.
50 * FD_POLL_OUT remains set as long as the fd accepts to write data.
51 * FD_POLL_ERR and FD_POLL_ERR remain set forever (until processed).
52 */
Willy Tarreau3d32d3a2007-04-15 11:31:05 +020053#define FD_POLL_IN 0x01
54#define FD_POLL_PRI 0x02
55#define FD_POLL_OUT 0x04
56#define FD_POLL_ERR 0x08
57#define FD_POLL_HUP 0x10
Willy Tarreau3d32d3a2007-04-15 11:31:05 +020058
Willy Tarreaud6f087e2008-01-18 17:20:13 +010059#define FD_POLL_DATA (FD_POLL_IN | FD_POLL_OUT)
60#define FD_POLL_STICKY (FD_POLL_ERR | FD_POLL_HUP)
Willy Tarreau3d32d3a2007-04-15 11:31:05 +020061
Willy Tarreaubaaee002006-06-26 02:48:02 +020062/* info about one given fd */
63struct fdtab {
Willy Tarreau54469402006-07-29 16:59:06 +020064 struct {
65 int (*f)(int fd); /* read/write function */
66 struct buffer *b; /* read/write buffer */
67 } cb[DIR_SIZE];
Willy Tarreaueabf3132008-08-29 23:36:51 +020068 void *owner; /* the session (or proxy) associated with this fd */
Willy Tarreau3d32d3a2007-04-15 11:31:05 +020069 unsigned char state; /* the state of this fd */
70 unsigned char ev; /* event seen in return of poll() : FD_POLL_* */
Willy Tarreaue94ebd02007-10-09 17:14:37 +020071 struct sockaddr *peeraddr; /* pointer to peer's network address, or NULL if unset */
72 socklen_t peerlen; /* peer's address length, or 0 if unset */
Willy Tarreauc6f4ce82009-06-10 11:09:37 +020073 int local_port; /* optional local port */
74 struct port_range *port_range; /* optional port range to bind to */
Willy Tarreaubaaee002006-06-26 02:48:02 +020075};
76
Willy Tarreau4f60f162007-04-08 16:39:58 +020077/*
78 * Poller descriptors.
79 * - <name> is initialized by the poller's register() function, and should not
80 * be allocated, just linked to.
81 * - <pref> is initialized by the poller's register() function. It is set to 0
82 * by default, meaning the poller is disabled. init() should set it to 0 in
83 * case of failure. term() must set it to 0. A generic unoptimized select()
84 * poller should set it to 100.
85 * - <private> is initialized by the poller's init() function, and cleaned by
86 * the term() function.
Willy Tarreau97129b52007-04-09 00:54:46 +020087 * - cond_s() checks if fd was not set then sets it and returns 1. Otherwise
88 * it returns 0. It may be the same as set().
89 * - cond_c() checks if fd was set then clears it and returns 1. Otherwise
90 * it returns 0. It may be the same as clr().
Willy Tarreau4f60f162007-04-08 16:39:58 +020091 * - clo() should be used to do indicate the poller that fd will be closed. It
92 * may be the same as rem() on some pollers.
Willy Tarreaud825eef2007-05-12 22:35:00 +020093 * - poll() calls the poller, expiring at <exp>
Willy Tarreau4f60f162007-04-08 16:39:58 +020094 */
95struct poller {
96 void *private; /* any private data for the poller */
Willy Tarreau5b702422007-04-16 01:33:26 +020097 int REGPRM2 (*is_set)(const int fd, int dir); /* check if <fd> is being polled for dir <dir> */
98 int REGPRM2 (*set)(const int fd, int dir); /* set polling on <fd> for <dir> */
99 int REGPRM2 (*clr)(const int fd, int dir); /* clear polling on <fd> for <dir> */
100 int REGPRM2 (*cond_s)(const int fd, int dir); /* set polling on <fd> for <dir> if unset */
101 int REGPRM2 (*cond_c)(const int fd, int dir); /* clear polling on <fd> for <dir> if set */
102 void REGPRM1 (*rem)(const int fd); /* remove any polling on <fd> */
103 void REGPRM1 (*clo)(const int fd); /* mark <fd> as closed */
Willy Tarreau0c303ee2008-07-07 00:09:58 +0200104 void REGPRM2 (*poll)(struct poller *p, int exp); /* the poller itself */
Willy Tarreau5b702422007-04-16 01:33:26 +0200105 int REGPRM1 (*init)(struct poller *p); /* poller initialization */
106 void REGPRM1 (*term)(struct poller *p); /* termination of this poller */
107 int REGPRM1 (*test)(struct poller *p); /* pre-init check of the poller */
108 int REGPRM1 (*fork)(struct poller *p); /* post-fork re-opening */
Willy Tarreau4f60f162007-04-08 16:39:58 +0200109 const char *name; /* poller name */
110 int pref; /* try pollers with higher preference first */
111};
112
113extern struct poller cur_poller; /* the current poller */
114extern int nbpollers;
115#define MAX_POLLERS 10
116extern struct poller pollers[MAX_POLLERS]; /* all registered pollers */
117
Willy Tarreaubaaee002006-06-26 02:48:02 +0200118extern struct fdtab *fdtab; /* array of all the file descriptors */
119extern int maxfd; /* # of the highest fd + 1 */
120extern int totalconn; /* total # of terminated sessions */
121extern int actconn; /* # of active sessions */
122
123#endif /* _TYPES_FD_H */
124
125/*
126 * Local variables:
127 * c-indent-level: 8
128 * c-basic-offset: 8
129 * End:
130 */