blob: d573c585eb7568d8ba87953967c331ba1004d2bb [file] [log] [blame]
Willy Tarreaufa7e1022008-10-19 07:30:41 +02001/*
Willy Tarreau4c283dc2009-12-29 14:36:34 +01002 * include/types/stream_interface.h
3 * This file describes the stream_interface struct and associated constants.
4 *
Willy Tarreaub24281b2011-02-13 13:16:36 +01005 * Copyright (C) 2000-2011 Willy Tarreau - w@1wt.eu
Willy Tarreau4c283dc2009-12-29 14:36:34 +01006 *
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 Tarreaufa7e1022008-10-19 07:30:41 +020021
22#ifndef _TYPES_STREAM_INTERFACE_H
23#define _TYPES_STREAM_INTERFACE_H
24
25#include <stdlib.h>
Emeric Brun129c5902010-01-04 14:47:58 +010026#include <sys/socket.h>
Willy Tarreaufa7e1022008-10-19 07:30:41 +020027
Willy Tarreau48adac52008-08-30 04:58:38 +020028#include <types/buffers.h>
Willy Tarreaufa7e1022008-10-19 07:30:41 +020029#include <common/config.h>
30
31/* A stream interface must have its own errors independantly of the buffer's,
32 * so that applications can rely on what the buffer reports while the stream
Willy Tarreau74ab2ac2008-11-23 17:23:07 +010033 * interface is performing some retries (eg: connection error). Some states are
34 * transient and do not last beyond process_session().
Willy Tarreaufa7e1022008-10-19 07:30:41 +020035 */
36enum {
Willy Tarreauefb453c2008-10-26 20:49:47 +010037 SI_ST_INI = 0, /* interface not sollicitated yet */
Willy Tarreau74ab2ac2008-11-23 17:23:07 +010038 SI_ST_REQ, /* [transient] connection initiation desired and not started yet */
Willy Tarreaufa7e1022008-10-19 07:30:41 +020039 SI_ST_QUE, /* interface waiting in queue */
40 SI_ST_TAR, /* interface in turn-around state after failed connect attempt */
41 SI_ST_ASS, /* server just assigned to this interface */
42 SI_ST_CON, /* initiated connection request (resource exists) */
Willy Tarreau74ab2ac2008-11-23 17:23:07 +010043 SI_ST_CER, /* [transient] previous connection attempt failed (resource released) */
Willy Tarreaufa7e1022008-10-19 07:30:41 +020044 SI_ST_EST, /* connection established (resource exists) */
Willy Tarreau74ab2ac2008-11-23 17:23:07 +010045 SI_ST_DIS, /* [transient] disconnected from other side, but cleanup not done yet */
Willy Tarreaucff64112008-11-03 06:26:53 +010046 SI_ST_CLO, /* stream intf closed, might not existing anymore. Buffers shut. */
Willy Tarreaufa7e1022008-10-19 07:30:41 +020047};
48
49/* error types reported on the streams interface for more accurate reporting */
50enum {
Willy Tarreaue5ed4062008-08-30 03:17:31 +020051 SI_ET_NONE = 0x0000, /* no error yet, leave it to zero */
52 SI_ET_QUEUE_TO = 0x0001, /* queue timeout */
53 SI_ET_QUEUE_ERR = 0x0002, /* queue error (eg: full) */
54 SI_ET_QUEUE_ABRT = 0x0004, /* aborted in queue by external cause */
55 SI_ET_CONN_TO = 0x0008, /* connection timeout */
56 SI_ET_CONN_ERR = 0x0010, /* connection error (eg: no server available) */
57 SI_ET_CONN_ABRT = 0x0020, /* connection aborted by external cause (eg: abort) */
58 SI_ET_CONN_OTHER = 0x0040, /* connection aborted for other reason (eg: 500) */
59 SI_ET_DATA_TO = 0x0080, /* timeout during data phase */
60 SI_ET_DATA_ERR = 0x0100, /* error during data phase */
61 SI_ET_DATA_ABRT = 0x0200, /* data phase aborted by external cause */
Willy Tarreaufa7e1022008-10-19 07:30:41 +020062};
63
Willy Tarreaud7704b52008-09-04 11:51:16 +020064/* flags set after I/O */
65enum {
66 SI_FL_NONE = 0x0000, /* nothing */
67 SI_FL_EXP = 0x0001, /* timeout has expired */
Willy Tarreaucff64112008-11-03 06:26:53 +010068 SI_FL_ERR = 0x0002, /* a non-recoverable error has occurred */
Willy Tarreaub0ef7352008-12-14 13:26:20 +010069 SI_FL_WAIT_ROOM = 0x0004, /* waiting for space to store incoming data */
70 SI_FL_WAIT_DATA = 0x0008, /* waiting for more data to send */
Willy Tarreaudc340a92009-06-28 23:10:19 +020071 SI_FL_CAP_SPLTCP = 0x0010, /* splicing possible from/to TCP */
Willy Tarreau89f7ef22009-09-05 20:57:35 +020072 SI_FL_DONT_WAKE = 0x0020, /* resync in progress, don't wake up */
Willy Tarreauf27b5ea2009-10-03 22:01:18 +020073 SI_FL_INDEP_STR = 0x0040, /* independant streams = don't update rex on write */
Willy Tarreau4c283dc2009-12-29 14:36:34 +010074 SI_FL_NOLINGER = 0x0080, /* may close without lingering. One-shot. */
Willy Tarreau7bb68ab2012-05-13 14:48:59 +020075 SI_FL_NOHALF = 0x0100, /* no half close, close both sides at once */
William Lallemandb7ff6a32012-03-02 14:35:21 +010076 SI_FL_SRC_ADDR = 0x1000, /* get the source ip/port with getsockname */
Aman Gupta0bc0c242012-04-02 18:57:55 -070077 SI_FL_TO_SET = 0x2000, /* addr.to is set */
78 SI_FL_FROM_SET = 0x4000, /* addr.from is set */
Willy Tarreaud7704b52008-09-04 11:51:16 +020079};
80
Willy Tarreauac825402011-03-04 22:04:29 +010081/* target types */
82enum {
83 TARG_TYPE_NONE = 0, /* no target set, pointer is NULL by definition */
Willy Tarreau1539a012012-05-11 14:47:34 +020084 TARG_TYPE_CLIENT, /* target is a client, pointer is NULL by definition */
Willy Tarreauac825402011-03-04 22:04:29 +010085 TARG_TYPE_PROXY, /* target is a proxy ; use address with the proxy's settings */
86 TARG_TYPE_SERVER, /* target is a server ; use address with server's and its proxy's settings */
87 TARG_TYPE_APPLET, /* target is an applet ; use only the applet */
Willy Tarreau7c0a1512011-03-10 11:17:02 +010088 TARG_TYPE_TASK, /* target is a task running an external applet */
Willy Tarreauac825402011-03-04 22:04:29 +010089};
90
Willy Tarreaudc340a92009-06-28 23:10:19 +020091#define SI_FL_CAP_SPLICE (SI_FL_CAP_SPLTCP)
92
Willy Tarreau9650f372009-08-16 14:02:45 +020093struct server;
94struct proxy;
Willy Tarreaub24281b2011-02-13 13:16:36 +010095struct si_applet;
Willy Tarreau060781f2012-05-07 16:50:03 +020096struct stream_interface;
Willy Tarreau9650f372009-08-16 14:02:45 +020097
Willy Tarreau73b013b2012-05-21 16:31:45 +020098/* This structure describes a connection with its methods and data.
99 * A connection may be performed to proxy or server via a local or remote
100 * socket, and can also be made to an internal applet. It can support
101 * several data schemes (applet, raw, ssl, ...). It can support several
102 * connection control schemes, generally a protocol for socket-oriented
103 * connections, but other methods for applets.
104 */
105struct connection {
106 const struct sock_ops *data; /* operations at the data layer */
107 const struct protocol *ctrl; /* operations at the control layer, generally a protocol */
108};
109
Willy Tarreauac825402011-03-04 22:04:29 +0100110struct target {
111 int type;
112 union {
113 void *v; /* pointer value, for any type */
114 struct proxy *p; /* when type is TARG_TYPE_PROXY */
115 struct server *s; /* when type is TARG_TYPE_SERVER */
116 struct si_applet *a; /* when type is TARG_TYPE_APPLET */
Willy Tarreau7c0a1512011-03-10 11:17:02 +0100117 struct task *t; /* when type is TARG_TYPE_TASK */
Willy Tarreauac825402011-03-04 22:04:29 +0100118 } ptr;
119};
120
Willy Tarreau060781f2012-05-07 16:50:03 +0200121struct sock_ops {
122 void (*update)(struct stream_interface *); /* I/O update function */
123 void (*shutr)(struct stream_interface *); /* shutr function */
124 void (*shutw)(struct stream_interface *); /* shutw function */
125 void (*chk_rcv)(struct stream_interface *); /* chk_rcv function */
126 void (*chk_snd)(struct stream_interface *); /* chk_snd function */
127 int (*read)(int fd); /* read callback after poll() */
128 int (*write)(int fd); /* wrtie callback after poll() */
129};
130
Willy Tarreaube5ea192011-03-03 17:08:11 +0100131/* A stream interface has 3 parts :
132 * - the buffer side, which interfaces to the buffers.
133 * - the remote side, which describes the state and address of the other side.
134 * - the functions, which are used by the buffer side to communicate with the
135 * remote side from the buffer side.
136 */
137
Willy Tarreaub24281b2011-02-13 13:16:36 +0100138/* Note that if an applet is registered, the update function will not be called
139 * by the session handler, so it may be used to resync flags at the end of the
140 * applet handler. See stream_int_update_embedded() for reference.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200141 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200142struct stream_interface {
Willy Tarreaube5ea192011-03-03 17:08:11 +0100143 /* struct members used by the "buffer" side */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200144 unsigned int state; /* SI_ST* */
Willy Tarreaue5ed4062008-08-30 03:17:31 +0200145 unsigned int prev_state;/* SI_ST*, copy of previous state */
Willy Tarreaube5ea192011-03-03 17:08:11 +0100146 unsigned int flags; /* SI_FL_* */
147 struct buffer *ib, *ob; /* input and output buffers */
Willy Tarreau35374672008-09-03 18:11:02 +0200148 unsigned int exp; /* wake up time for connect, queue, turn-around, ... */
Willy Tarreaube5ea192011-03-03 17:08:11 +0100149 void *owner; /* generally a (struct task*) */
150 unsigned int err_type; /* first error detected, one of SI_ET_* */
151 void *err_loc; /* commonly the server, NULL when SI_ET_NONE */
152
Willy Tarreau73b013b2012-05-21 16:31:45 +0200153 struct connection conn; /* descriptor for a connection */
Willy Tarreaube5ea192011-03-03 17:08:11 +0100154
Willy Tarreau060781f2012-05-07 16:50:03 +0200155 void (*release)(struct stream_interface *); /* handler to call after the last close() */
156
Willy Tarreaube5ea192011-03-03 17:08:11 +0100157 /* struct members below are the "remote" part, as seen from the buffer side */
Willy Tarreauac825402011-03-04 22:04:29 +0100158 struct target target; /* the target to connect to (server, proxy, applet, ...) */
Willy Tarreauee28de02010-06-01 09:51:00 +0200159 int conn_retries; /* number of connect retries left */
Willy Tarreaub22e55b2011-03-20 10:16:46 +0100160 int send_proxy_ofs; /* <0 = offset to (re)send from the end, >0 = send all */
Willy Tarreaube5ea192011-03-03 17:08:11 +0100161 int fd; /* file descriptor for a stream driver when known */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100162 struct {
Willy Tarreau295a8372011-03-10 11:25:07 +0100163 int state; /* applet state, initialized to zero */
Willy Tarreaubc4af052011-02-13 13:25:14 +0100164 void *private; /* may be used by any function above */
165 unsigned int st0, st1; /* may be used by any function above */
Willy Tarreau295a8372011-03-10 11:25:07 +0100166 union {
167 struct {
168 struct proxy *px;
169 struct server *sv;
170 struct listener *l;
171 int px_st; /* STAT_PX_ST* */
172 unsigned int flags; /* STAT_* */
173 int iid, type, sid; /* proxy id, type and service id if bounding of stats is enabled */
Cyril Bonté19979e12012-04-04 12:57:21 +0200174 int st_code; /* the status code returned by an action */
Willy Tarreau295a8372011-03-10 11:25:07 +0100175 } stats;
176 struct {
177 struct bref bref; /* back-reference from the session being dumped */
178 void *target; /* session we want to dump, or NULL for all */
179 unsigned int uid; /* if non-null, the uniq_id of the session being dumped */
180 int section; /* section of the session being dumped */
181 int pos; /* last position of the current session's buffer */
182 } sess;
183 struct {
184 int iid; /* if >= 0, ID of the proxy to filter on */
185 struct proxy *px; /* current proxy being dumped, NULL = not started yet. */
186 unsigned int buf; /* buffer being dumped, 0 = req, 1 = rep */
187 unsigned int sid; /* session ID of error being dumped */
188 int ptr; /* <0: headers, >=0 : text pointer to restart from */
189 int bol; /* pointer to beginning of current line */
190 } errors;
191 struct {
192 void *target; /* table we want to dump, or NULL for all */
193 struct proxy *proxy; /* table being currently dumped (first if NULL) */
194 struct stksess *entry; /* last entry we were trying to dump (or first if NULL) */
195 long long value; /* value to compare against */
196 signed char data_type; /* type of data to compare, or -1 if none */
197 signed char data_op; /* operator (STD_OP_*) when data_type set */
198 } table;
199 struct {
200 const char *msg; /* pointer to a persistent message to be returned in PRINT state */
201 } cli;
202 } ctx; /* used by stats I/O handlers to dump the stats */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100203 } applet;
Willy Tarreau6471afb2011-09-23 10:54:59 +0200204 struct {
205 struct sockaddr_storage from; /* client address, or address to spoof when connecting to the server */
206 struct sockaddr_storage to; /* address reached by the client if SN_FRT_ADDR_SET is set, or address to connect to */
207 } addr; /* addresses of the remote side, client for producer and server for consumer */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200208};
209
Willy Tarreaub24281b2011-02-13 13:16:36 +0100210/* An applet designed to run in a stream interface */
211struct si_applet {
212 char *name; /* applet's name to report in logs */
213 void (*fct)(struct stream_interface *); /* internal I/O handler, may never be NULL */
Aman Gupta9a13e842012-04-02 18:57:53 -0700214 void (*release)(struct stream_interface *); /* callback to release resources, may be NULL */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100215};
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200216
217#endif /* _TYPES_STREAM_INTERFACE_H */
218
219/*
220 * Local variables:
221 * c-indent-level: 8
222 * c-basic-offset: 8
223 * End:
224 */