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 |
| 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 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 | e94ebd0 | 2007-10-09 17:14:37 +0200 | [diff] [blame] | 25 | #include <sys/socket.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | #include <sys/time.h> |
| 27 | #include <sys/types.h> |
| 28 | #include <unistd.h> |
| 29 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 30 | #include <common/config.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 31 | #include <types/task.h> |
Willy Tarreau | dd81598 | 2007-10-16 12:25:14 +0200 | [diff] [blame] | 32 | #include <types/protocols.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 33 | |
| 34 | /* different possible states for the fd */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | #define FD_STLISTEN 1 |
| 36 | #define FD_STCONN 2 |
| 37 | #define FD_STREADY 3 |
| 38 | #define FD_STERROR 4 |
| 39 | |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 40 | enum { |
| 41 | DIR_RD=0, |
| 42 | DIR_WR=1, |
| 43 | DIR_SIZE |
| 44 | }; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 45 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 46 | /* |
| 47 | * FD_POLL_IN remains set as long as some data is pending for read. |
| 48 | * FD_POLL_OUT remains set as long as the fd accepts to write data. |
| 49 | * FD_POLL_ERR and FD_POLL_ERR remain set forever (until processed). |
| 50 | */ |
Willy Tarreau | 3d32d3a | 2007-04-15 11:31:05 +0200 | [diff] [blame] | 51 | #define FD_POLL_IN 0x01 |
| 52 | #define FD_POLL_PRI 0x02 |
| 53 | #define FD_POLL_OUT 0x04 |
| 54 | #define FD_POLL_ERR 0x08 |
| 55 | #define FD_POLL_HUP 0x10 |
Willy Tarreau | 3d32d3a | 2007-04-15 11:31:05 +0200 | [diff] [blame] | 56 | |
Willy Tarreau | d6f087e | 2008-01-18 17:20:13 +0100 | [diff] [blame] | 57 | #define FD_POLL_DATA (FD_POLL_IN | FD_POLL_OUT) |
| 58 | #define FD_POLL_STICKY (FD_POLL_ERR | FD_POLL_HUP) |
Willy Tarreau | 3d32d3a | 2007-04-15 11:31:05 +0200 | [diff] [blame] | 59 | |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 60 | /* bit values for fdtab[fd]->flags. Most of them are used to hold a value |
| 61 | * consecutive to a behaviour change. |
| 62 | */ |
| 63 | #define FD_FL_TCP 0x0001 /* socket is TCP */ |
| 64 | #define FD_FL_TCP_NODELAY 0x0002 |
Willy Tarreau | 6db06d3 | 2009-08-19 11:14:11 +0200 | [diff] [blame] | 65 | #define FD_FL_TCP_NOLING 0x0004 /* lingering disabled */ |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 66 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 67 | /* info about one given fd */ |
| 68 | struct fdtab { |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 69 | struct { |
| 70 | int (*f)(int fd); /* read/write function */ |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 71 | } cb[DIR_SIZE]; |
Willy Tarreau | db3b326 | 2012-07-05 23:19:22 +0200 | [diff] [blame^] | 72 | void *owner; /* the session (or proxy) associated with this fd, NULL if closed */ |
Willy Tarreau | b48b323 | 2009-10-17 22:54:17 +0200 | [diff] [blame] | 73 | struct { /* used by pollers which support speculative polling */ |
| 74 | unsigned char e; /* read and write events status. 4 bits, may be merged into flags' lower bits */ |
| 75 | unsigned int s1; /* Position in spec list+1. 0=not in list. */ |
| 76 | } spec; |
Willy Tarreau | fb14edc | 2009-06-14 15:24:37 +0200 | [diff] [blame] | 77 | unsigned short flags; /* various flags precising the exact status of this fd */ |
Willy Tarreau | 3d32d3a | 2007-04-15 11:31:05 +0200 | [diff] [blame] | 78 | unsigned char state; /* the state of this fd */ |
| 79 | unsigned char ev; /* event seen in return of poll() : FD_POLL_* */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /* less often used information */ |
| 83 | struct fdinfo { |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 84 | struct port_range *port_range; /* optional port range to bind to */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 85 | int local_port; /* optional local port */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 86 | }; |
| 87 | |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 88 | /* |
| 89 | * Poller descriptors. |
| 90 | * - <name> is initialized by the poller's register() function, and should not |
| 91 | * be allocated, just linked to. |
| 92 | * - <pref> is initialized by the poller's register() function. It is set to 0 |
| 93 | * by default, meaning the poller is disabled. init() should set it to 0 in |
| 94 | * case of failure. term() must set it to 0. A generic unoptimized select() |
| 95 | * poller should set it to 100. |
| 96 | * - <private> is initialized by the poller's init() function, and cleaned by |
| 97 | * the term() function. |
Willy Tarreau | 97129b5 | 2007-04-09 00:54:46 +0200 | [diff] [blame] | 98 | * - cond_s() checks if fd was not set then sets it and returns 1. Otherwise |
| 99 | * it returns 0. It may be the same as set(). |
| 100 | * - cond_c() checks if fd was set then clears it and returns 1. Otherwise |
| 101 | * it returns 0. It may be the same as clr(). |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 102 | * - clo() should be used to do indicate the poller that fd will be closed. It |
| 103 | * may be the same as rem() on some pollers. |
Willy Tarreau | d825eef | 2007-05-12 22:35:00 +0200 | [diff] [blame] | 104 | * - poll() calls the poller, expiring at <exp> |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 105 | */ |
| 106 | struct poller { |
| 107 | void *private; /* any private data for the poller */ |
Willy Tarreau | 5b70242 | 2007-04-16 01:33:26 +0200 | [diff] [blame] | 108 | int REGPRM2 (*is_set)(const int fd, int dir); /* check if <fd> is being polled for dir <dir> */ |
| 109 | int REGPRM2 (*set)(const int fd, int dir); /* set polling on <fd> for <dir> */ |
| 110 | int REGPRM2 (*clr)(const int fd, int dir); /* clear polling on <fd> for <dir> */ |
| 111 | int REGPRM2 (*cond_s)(const int fd, int dir); /* set polling on <fd> for <dir> if unset */ |
| 112 | int REGPRM2 (*cond_c)(const int fd, int dir); /* clear polling on <fd> for <dir> if set */ |
| 113 | void REGPRM1 (*rem)(const int fd); /* remove any polling on <fd> */ |
| 114 | void REGPRM1 (*clo)(const int fd); /* mark <fd> as closed */ |
Willy Tarreau | 0c303ee | 2008-07-07 00:09:58 +0200 | [diff] [blame] | 115 | void REGPRM2 (*poll)(struct poller *p, int exp); /* the poller itself */ |
Willy Tarreau | 5b70242 | 2007-04-16 01:33:26 +0200 | [diff] [blame] | 116 | int REGPRM1 (*init)(struct poller *p); /* poller initialization */ |
| 117 | void REGPRM1 (*term)(struct poller *p); /* termination of this poller */ |
| 118 | int REGPRM1 (*test)(struct poller *p); /* pre-init check of the poller */ |
| 119 | int REGPRM1 (*fork)(struct poller *p); /* post-fork re-opening */ |
Willy Tarreau | 4f60f16 | 2007-04-08 16:39:58 +0200 | [diff] [blame] | 120 | const char *name; /* poller name */ |
| 121 | int pref; /* try pollers with higher preference first */ |
| 122 | }; |
| 123 | |
| 124 | extern struct poller cur_poller; /* the current poller */ |
| 125 | extern int nbpollers; |
| 126 | #define MAX_POLLERS 10 |
| 127 | extern struct poller pollers[MAX_POLLERS]; /* all registered pollers */ |
| 128 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 129 | extern struct fdtab *fdtab; /* array of all the file descriptors */ |
Willy Tarreau | 8d5d77e | 2009-10-18 07:25:52 +0200 | [diff] [blame] | 130 | extern struct fdinfo *fdinfo; /* less-often used infos for file descriptors */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 131 | extern int maxfd; /* # of the highest fd + 1 */ |
| 132 | extern int totalconn; /* total # of terminated sessions */ |
| 133 | extern int actconn; /* # of active sessions */ |
| 134 | |
| 135 | #endif /* _TYPES_FD_H */ |
| 136 | |
| 137 | /* |
| 138 | * Local variables: |
| 139 | * c-indent-level: 8 |
| 140 | * c-basic-offset: 8 |
| 141 | * End: |
| 142 | */ |