MAJOR: polling: rework the whole polling system

This commit heavily changes the polling system in order to definitely
fix the frequent breakage of SSL which needs to remember the last
EAGAIN before deciding whether to poll or not. Now we have a state per
direction for each FD, as opposed to a previous and current state
previously. An FD can have up to 8 different states for each direction,
each of which being the result of a 3-bit combination. These 3 bits
indicate a wish to access the FD, the readiness of the FD and the
subscription of the FD to the polling system.

This means that it will now be possible to remember the state of a
file descriptor across disable/enable sequences that generally happen
during forwarding, where enabling reading on a previously disabled FD
would result in forgetting the EAGAIN flag it met last time.

Several new state manipulation functions have been introduced or
adapted :
  - fd_want_{recv,send} : enable receiving/sending on the FD regardless
    of its state (sets the ACTIVE flag) ;

  - fd_stop_{recv,send} : stop receiving/sending on the FD regardless
    of its state (clears the ACTIVE flag) ;

  - fd_cant_{recv,send} : report a failure to receive/send on the FD
    corresponding to EAGAIN (clears the READY flag) ;

  - fd_may_{recv,send}  : report the ability to receive/send on the FD
    as reported by poll() (sets the READY flag) ;

Some functions are used to report the current FD status :

  - fd_{recv,send}_active
  - fd_{recv,send}_ready
  - fd_{recv,send}_polled

Some functions were removed :
  - fd_ev_clr(), fd_ev_set(), fd_ev_rem(), fd_ev_wai()

The POLLHUP/POLLERR flags are now reported as ready so that the I/O layers
knows it can try to access the file descriptor to get this information.

In order to simplify the conditions to add/remove cache entries, a new
function fd_alloc_or_release_cache_entry() was created to be used from
pollers while scanning for updates.

The following pollers have been updated :

   ev_select() : done, built, tested on Linux 3.10
   ev_poll()   : done, built, tested on Linux 3.10
   ev_epoll()  : done, built, tested on Linux 3.10 & 3.13
   ev_kqueue() : done, built, tested on OpenBSD 5.2
diff --git a/include/proto/connection.h b/include/proto/connection.h
index af995cd..d44aeec 100644
--- a/include/proto/connection.h
+++ b/include/proto/connection.h
@@ -96,6 +96,9 @@
 		int fd = conn->t.sock.fd;
 
 		fd_insert(fd);
+		/* mark the fd as ready so as not to needlessly poll at the beginning */
+		fd_may_recv(fd);
+		fd_may_send(fd);
 		fdtab[fd].owner = conn;
 		fdtab[fd].iocb = conn_fd_handler;
 		conn->flags |= CO_FL_CTRL_READY;
@@ -166,9 +169,9 @@
 	if ((conn->flags & CO_FL_CTRL_READY) && conn->ctrl) {
 		unsigned int flags = conn->flags & ~(CO_FL_CURR_RD_ENA | CO_FL_CURR_WR_ENA);
 
-		if (fd_ev_is_set(conn->t.sock.fd, DIR_RD))
+		if (fd_recv_active(conn->t.sock.fd))
 			flags |= CO_FL_CURR_RD_ENA;
-		if (fd_ev_is_set(conn->t.sock.fd, DIR_WR))
+		if (fd_send_active(conn->t.sock.fd))
 			flags |= CO_FL_CURR_WR_ENA;
 		conn->flags = flags;
 	}
diff --git a/include/proto/fd.h b/include/proto/fd.h
index 1ca9b35..c87dc3d 100644
--- a/include/proto/fd.h
+++ b/include/proto/fd.h
@@ -2,7 +2,7 @@
  * include/proto/fd.h
  * File descriptors states.
  *
- * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
+ * Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -127,99 +127,165 @@
 	}
 }
 
+/* Automatically allocates or releases a cache entry for fd <fd> depending on
+ * its new state. This is meant to be used by pollers while processing updates.
+ */
+static inline void fd_alloc_or_release_cache_entry(int fd, int new_state)
+{
+	/* READY and ACTIVE states (the two with both flags set) require a cache entry */
+
+	if (((new_state & (FD_EV_READY_R | FD_EV_ACTIVE_R)) == (FD_EV_READY_R | FD_EV_ACTIVE_R)) ||
+	    ((new_state & (FD_EV_READY_W | FD_EV_ACTIVE_W)) == (FD_EV_READY_W | FD_EV_ACTIVE_W))) {
+		fd_alloc_cache_entry(fd);
+	}
+	else {
+		fd_release_cache_entry(fd);
+	}
+}
+
 /*
- * Returns non-zero if <fd> is already monitored for events in direction <dir>.
+ * returns the FD's recv state (FD_EV_*)
  */
-static inline int fd_ev_is_set(const int fd, int dir)
+static inline int fd_recv_state(const int fd)
 {
-	return ((unsigned)fdtab[fd].state >> dir) & FD_EV_STATUS;
+	return ((unsigned)fdtab[fd].state >> (4 * DIR_RD)) & FD_EV_STATUS;
 }
 
-/* Disable processing of events on fd <fd> for direction <dir>. Note: this
- * function was optimized to be used with a constant for <dir>.
+/*
+ * returns true if the FD is active for recv
  */
-static inline void fd_ev_clr(const int fd, int dir)
+static inline int fd_recv_active(const int fd)
 {
-	unsigned int i = ((unsigned int)fdtab[fd].state) & (FD_EV_STATUS << dir);
-	if (i == 0)
-		return; /* already disabled */
-	fdtab[fd].state ^= i;
-	updt_fd(fd); /* need an update entry to change the state */
+	return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_R;
 }
 
-/* Enable polling for events on fd <fd> for direction <dir>. Note: this
- * function was optimized to be used with a constant for <dir>.
+/*
+ * returns true if the FD is ready for recv
  */
-static inline void fd_ev_wai(const int fd, int dir)
+static inline int fd_recv_ready(const int fd)
 {
-	unsigned int i = ((unsigned int)fdtab[fd].state) & (FD_EV_STATUS << dir);
-	if (i == (FD_EV_POLLED << dir))
-		return; /* already in desired state */
-	fdtab[fd].state ^= i ^ (FD_EV_POLLED << dir);
-	updt_fd(fd); /* need an update entry to change the state */
+	return (unsigned)fdtab[fd].state & FD_EV_READY_R;
+}
+
+/*
+ * returns true if the FD is polled for recv
+ */
+static inline int fd_recv_polled(const int fd)
+{
+	return (unsigned)fdtab[fd].state & FD_EV_POLLED_R;
+}
+
+/*
+ * returns the FD's send state (FD_EV_*)
+ */
+static inline int fd_send_state(const int fd)
+{
+	return ((unsigned)fdtab[fd].state >> (4 * DIR_WR)) & FD_EV_STATUS;
+}
+
+/*
+ * returns true if the FD is active for send
+ */
+static inline int fd_send_active(const int fd)
+{
+	return (unsigned)fdtab[fd].state & FD_EV_ACTIVE_W;
 }
 
-/* Enable processing of events on fd <fd> for direction <dir>. Note: this
- * function was optimized to be used with a constant for <dir>.
+/*
+ * returns true if the FD is ready for send
  */
-static inline void fd_ev_set(int fd, int dir)
+static inline int fd_send_ready(const int fd)
 {
-	unsigned int i = ((unsigned int)fdtab[fd].state) & (FD_EV_STATUS << dir);
+	return (unsigned)fdtab[fd].state & FD_EV_READY_W;
+}
 
-	/* note that we don't care about disabling the polled state when
-	 * enabling the active state, since it brings no benefit but costs
-	 * some syscalls.
-	 */
-	if (i & (FD_EV_ACTIVE << dir))
-		return; /* already in desired state */
-	fdtab[fd].state |= (FD_EV_ACTIVE << dir);
+/*
+ * returns true if the FD is polled for send
+ */
+static inline int fd_send_polled(const int fd)
+{
+	return (unsigned)fdtab[fd].state & FD_EV_POLLED_W;
+}
+
+/* Disable processing recv events on fd <fd> */
+static inline void fd_stop_recv(int fd)
+{
+	if (!((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_R))
+		return; /* already disabled */
+	fdtab[fd].state &= ~FD_EV_ACTIVE_R;
 	updt_fd(fd); /* need an update entry to change the state */
 }
 
-/* Disable processing of events on fd <fd> for both directions. */
-static inline void fd_ev_rem(const int fd)
+/* Disable processing send events on fd <fd> */
+static inline void fd_stop_send(int fd)
 {
-	unsigned int i = ((unsigned int)fdtab[fd].state) & FD_EV_CURR_MASK;
-	if (i == 0)
+	if (!((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_W))
 		return; /* already disabled */
-	fdtab[fd].state ^= i;
+	fdtab[fd].state &= ~FD_EV_ACTIVE_W;
 	updt_fd(fd); /* need an update entry to change the state */
 }
 
-/* event manipulation primitives for use by I/O callbacks */
-static inline void fd_want_recv(int fd)
+/* Disable processing of events on fd <fd> for both directions. */
+static inline void fd_stop_both(int fd)
 {
-	return fd_ev_set(fd, DIR_RD);
+	if (!((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_RW))
+		return; /* already disabled */
+	fdtab[fd].state &= ~FD_EV_ACTIVE_RW;
+	updt_fd(fd); /* need an update entry to change the state */
 }
 
-static inline void fd_stop_recv(int fd)
+/* Report that FD <fd> cannot receive anymore without polling (EAGAIN detected). */
+static inline void fd_cant_recv(const int fd)
 {
-	return fd_ev_clr(fd, DIR_RD);
+	if (!(((unsigned int)fdtab[fd].state) & FD_EV_READY_R))
+		return; /* already marked as blocked */
+	fdtab[fd].state &= ~FD_EV_READY_R;
+	updt_fd(fd);
 }
 
-static inline void fd_poll_recv(int fd)
+/* Report that FD <fd> can receive anymore without polling. */
+static inline void fd_may_recv(const int fd)
 {
-	return fd_ev_wai(fd, DIR_RD);
+	if (((unsigned int)fdtab[fd].state) & FD_EV_READY_R)
+		return; /* already marked as blocked */
+	fdtab[fd].state |= FD_EV_READY_R;
+	updt_fd(fd);
 }
 
-static inline void fd_want_send(int fd)
+/* Report that FD <fd> cannot send anymore without polling (EAGAIN detected). */
+static inline void fd_cant_send(const int fd)
 {
-	return fd_ev_set(fd, DIR_WR);
+	if (!(((unsigned int)fdtab[fd].state) & FD_EV_READY_W))
+		return; /* already marked as blocked */
+	fdtab[fd].state &= ~FD_EV_READY_W;
+	updt_fd(fd);
 }
 
-static inline void fd_stop_send(int fd)
+/* Report that FD <fd> can send anymore without polling (EAGAIN detected). */
+static inline void fd_may_send(const int fd)
 {
-	return fd_ev_clr(fd, DIR_WR);
+	if (((unsigned int)fdtab[fd].state) & FD_EV_READY_W)
+		return; /* already marked as blocked */
+	fdtab[fd].state |= FD_EV_READY_W;
+	updt_fd(fd);
 }
 
-static inline void fd_poll_send(int fd)
+/* Prepare FD <fd> to try to receive */
+static inline void fd_want_recv(int fd)
 {
-	return fd_ev_wai(fd, DIR_WR);
+	if (((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_R))
+		return; /* already enabled */
+	fdtab[fd].state |= FD_EV_ACTIVE_R;
+	updt_fd(fd); /* need an update entry to change the state */
 }
 
-static inline void fd_stop_both(int fd)
+/* Prepare FD <fd> to try to send */
+static inline void fd_want_send(int fd)
 {
-	return fd_ev_rem(fd);
+	if (((unsigned int)fdtab[fd].state & FD_EV_ACTIVE_W))
+		return; /* already enabled */
+	fdtab[fd].state |= FD_EV_ACTIVE_W;
+	updt_fd(fd); /* need an update entry to change the state */
 }
 
 /* Prepares <fd> for being polled */
diff --git a/include/types/fd.h b/include/types/fd.h
index 3bfe48f..1c2c7c8 100644
--- a/include/types/fd.h
+++ b/include/types/fd.h
@@ -1,8 +1,8 @@
 /*
  * include/types/fd.h
- * File descriptors states.
+ * File descriptors states - check src/fd.c for explanations.
  *
- * Copyright (C) 2000-2009 Willy Tarreau - w@1wt.eu
+ * Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -45,32 +45,43 @@
 #define FD_POLL_DATA    (FD_POLL_IN  | FD_POLL_OUT)
 #define FD_POLL_STICKY  (FD_POLL_ERR | FD_POLL_HUP)
 
-/* Event state for an FD in each direction, as found in the 4 lower bits of
- * fdtab[].state, and in the 4 next bits.
- */
 #define FD_EV_ACTIVE    1U
+#define FD_EV_READY     2U
 #define FD_EV_POLLED    4U
-#define FD_EV_STATUS    (FD_EV_ACTIVE | FD_EV_POLLED)
+
+#define FD_EV_STATUS    (FD_EV_ACTIVE | FD_EV_POLLED | FD_EV_READY)
 #define FD_EV_STATUS_R  (FD_EV_STATUS)
-#define FD_EV_STATUS_W  (FD_EV_STATUS << 1)
+#define FD_EV_STATUS_W  (FD_EV_STATUS << 4)
 
 #define FD_EV_POLLED_R  (FD_EV_POLLED)
-#define FD_EV_POLLED_W  (FD_EV_POLLED << 1)
+#define FD_EV_POLLED_W  (FD_EV_POLLED << 4)
 #define FD_EV_POLLED_RW (FD_EV_POLLED_R | FD_EV_POLLED_W)
 
 #define FD_EV_ACTIVE_R  (FD_EV_ACTIVE)
-#define FD_EV_ACTIVE_W  (FD_EV_ACTIVE << 1)
+#define FD_EV_ACTIVE_W  (FD_EV_ACTIVE << 4)
 #define FD_EV_ACTIVE_RW (FD_EV_ACTIVE_R | FD_EV_ACTIVE_W)
 
-#define FD_EV_CURR_MASK 0x0FU
-#define FD_EV_PREV_MASK 0xF0U
+#define FD_EV_READY_R   (FD_EV_READY)
+#define FD_EV_READY_W   (FD_EV_READY << 4)
+#define FD_EV_READY_RW  (FD_EV_READY_R | FD_EV_READY_W)
+
+enum fd_states {
+	FD_ST_DISABLED = 0,
+	FD_ST_MUSTPOLL,
+	FD_ST_STOPPED,
+	FD_ST_ACTIVE,
+	FD_ST_ABORT,
+	FD_ST_POLLED,
+	FD_ST_PAUSED,
+	FD_ST_READY
+};
 
 /* info about one given fd */
 struct fdtab {
 	int (*iocb)(int fd);                 /* I/O handler, returns FD_WAIT_* */
 	void *owner;                         /* the connection or listener associated with this fd, NULL if closed */
 	unsigned int  cache;                 /* position+1 in the FD cache. 0=not in cache. */
-	unsigned char state;                 /* FD state for read and write directions (4+4 bits) */
+	unsigned char state;                 /* FD state for read and write directions (2*3 bits) */
 	unsigned char ev;                    /* event seen in return of poll() : FD_POLL_* */
 	unsigned char new:1;                 /* 1 if this fd has just been created */
 	unsigned char updated:1;             /* 1 if this fd is already in the update list */