blob: ab0e750c4f92c8a0b816a608b3b8fb5df5fe1083 [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 Tarreauc7e42382012-08-24 19:22:53 +020028#include <types/channel.h>
Willy Tarreau56e9c5e2012-07-06 09:47:57 +020029#include <types/connection.h>
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +010030#ifdef USE_LUA
31#include <types/hlua.h>
32#endif
Willy Tarreau3fdb3662012-11-12 00:42:33 +010033#include <types/obj_type.h>
Willy Tarreaufa7e1022008-10-19 07:30:41 +020034#include <common/config.h>
35
Jamie Gloudon801a0a32012-08-25 00:18:33 -040036/* A stream interface must have its own errors independently of the buffer's,
Willy Tarreaufa7e1022008-10-19 07:30:41 +020037 * so that applications can rely on what the buffer reports while the stream
Willy Tarreau74ab2ac2008-11-23 17:23:07 +010038 * interface is performing some retries (eg: connection error). Some states are
39 * transient and do not last beyond process_session().
Willy Tarreaufa7e1022008-10-19 07:30:41 +020040 */
Willy Tarreau721854f2013-12-06 23:31:58 +010041enum si_state {
Willy Tarreauefb453c2008-10-26 20:49:47 +010042 SI_ST_INI = 0, /* interface not sollicitated yet */
Willy Tarreau74ab2ac2008-11-23 17:23:07 +010043 SI_ST_REQ, /* [transient] connection initiation desired and not started yet */
Willy Tarreaufa7e1022008-10-19 07:30:41 +020044 SI_ST_QUE, /* interface waiting in queue */
45 SI_ST_TAR, /* interface in turn-around state after failed connect attempt */
46 SI_ST_ASS, /* server just assigned to this interface */
47 SI_ST_CON, /* initiated connection request (resource exists) */
Willy Tarreau74ab2ac2008-11-23 17:23:07 +010048 SI_ST_CER, /* [transient] previous connection attempt failed (resource released) */
Willy Tarreaufa7e1022008-10-19 07:30:41 +020049 SI_ST_EST, /* connection established (resource exists) */
Willy Tarreau74ab2ac2008-11-23 17:23:07 +010050 SI_ST_DIS, /* [transient] disconnected from other side, but cleanup not done yet */
Willy Tarreaucff64112008-11-03 06:26:53 +010051 SI_ST_CLO, /* stream intf closed, might not existing anymore. Buffers shut. */
Willy Tarreau721854f2013-12-06 23:31:58 +010052} __attribute__((packed));
Willy Tarreaufa7e1022008-10-19 07:30:41 +020053
54/* error types reported on the streams interface for more accurate reporting */
55enum {
Willy Tarreaue5ed4062008-08-30 03:17:31 +020056 SI_ET_NONE = 0x0000, /* no error yet, leave it to zero */
57 SI_ET_QUEUE_TO = 0x0001, /* queue timeout */
58 SI_ET_QUEUE_ERR = 0x0002, /* queue error (eg: full) */
59 SI_ET_QUEUE_ABRT = 0x0004, /* aborted in queue by external cause */
60 SI_ET_CONN_TO = 0x0008, /* connection timeout */
61 SI_ET_CONN_ERR = 0x0010, /* connection error (eg: no server available) */
62 SI_ET_CONN_ABRT = 0x0020, /* connection aborted by external cause (eg: abort) */
Willy Tarreau2d400bb2012-05-14 12:11:47 +020063 SI_ET_CONN_RES = 0x0040, /* connection aborted due to lack of resources */
64 SI_ET_CONN_OTHER = 0x0080, /* connection aborted for other reason (eg: 500) */
65 SI_ET_DATA_TO = 0x0100, /* timeout during data phase */
66 SI_ET_DATA_ERR = 0x0200, /* error during data phase */
67 SI_ET_DATA_ABRT = 0x0400, /* data phase aborted by external cause */
Willy Tarreaufa7e1022008-10-19 07:30:41 +020068};
69
Willy Tarreau721854f2013-12-06 23:31:58 +010070/* flags set after I/O (16 bit) */
Willy Tarreaud7704b52008-09-04 11:51:16 +020071enum {
72 SI_FL_NONE = 0x0000, /* nothing */
73 SI_FL_EXP = 0x0001, /* timeout has expired */
Willy Tarreaucff64112008-11-03 06:26:53 +010074 SI_FL_ERR = 0x0002, /* a non-recoverable error has occurred */
Willy Tarreaub0ef7352008-12-14 13:26:20 +010075 SI_FL_WAIT_ROOM = 0x0004, /* waiting for space to store incoming data */
76 SI_FL_WAIT_DATA = 0x0008, /* waiting for more data to send */
Willy Tarreau93b0f4f2012-08-30 21:23:53 +020077 /* unused 0x0010 */
Willy Tarreau89f7ef22009-09-05 20:57:35 +020078 SI_FL_DONT_WAKE = 0x0020, /* resync in progress, don't wake up */
Jamie Gloudon801a0a32012-08-25 00:18:33 -040079 SI_FL_INDEP_STR = 0x0040, /* independent streams = don't update rex on write */
Willy Tarreau4c283dc2009-12-29 14:36:34 +010080 SI_FL_NOLINGER = 0x0080, /* may close without lingering. One-shot. */
Willy Tarreau7bb68ab2012-05-13 14:48:59 +020081 SI_FL_NOHALF = 0x0100, /* no half close, close both sides at once */
William Lallemandb7ff6a32012-03-02 14:35:21 +010082 SI_FL_SRC_ADDR = 0x1000, /* get the source ip/port with getsockname */
Willy Tarreaud7704b52008-09-04 11:51:16 +020083};
84
Willy Tarreau060781f2012-05-07 16:50:03 +020085struct stream_interface;
Willy Tarreau48099c72013-10-24 20:03:27 +020086struct si_applet;
Willy Tarreau9650f372009-08-16 14:02:45 +020087
Willy Tarreauc5788912012-08-24 18:12:41 +020088/* operations available on a stream-interface */
89struct si_ops {
Willy Tarreau060781f2012-05-07 16:50:03 +020090 void (*update)(struct stream_interface *); /* I/O update function */
Willy Tarreau060781f2012-05-07 16:50:03 +020091 void (*chk_rcv)(struct stream_interface *); /* chk_rcv function */
92 void (*chk_snd)(struct stream_interface *); /* chk_snd function */
Willy Tarreau6fe15412013-09-29 15:16:03 +020093 void (*shutr)(struct stream_interface *); /* shut read function */
94 void (*shutw)(struct stream_interface *); /* shut write function */
Willy Tarreau060781f2012-05-07 16:50:03 +020095};
96
Willy Tarreau452d3bb2013-11-24 02:28:49 +010097/* Context of a running applet. */
98struct appctx {
Willy Tarreau0788f472013-10-24 22:45:25 +020099 enum obj_type obj_type; /* OBJ_TYPE_APPCTX */
Willy Tarreau721854f2013-12-06 23:31:58 +0100100 /* 3 unused bytes here */
Willy Tarreau452d3bb2013-11-24 02:28:49 +0100101 unsigned int st0; /* CLI state for stats, session state for peers */
102 unsigned int st1; /* prompt for stats, session error for peers */
103 unsigned int st2; /* output state for stats, unused by peers */
Willy Tarreau48099c72013-10-24 20:03:27 +0200104 struct si_applet *applet; /* applet this context refers to */
Willy Tarreau452d3bb2013-11-24 02:28:49 +0100105
106 union {
107 struct {
108 struct proxy *px;
109 struct server *sv;
110 void *l;
111 int scope_str; /* limit scope to a frontend/backend substring */
112 int scope_len; /* length of the string above in the buffer */
113 int px_st; /* STAT_PX_ST* */
114 unsigned int flags; /* STAT_* */
115 int iid, type, sid; /* proxy id, type and service id if bounding of stats is enabled */
116 int st_code; /* the status code returned by an action */
117 } stats;
118 struct {
119 struct bref bref; /* back-reference from the session being dumped */
120 void *target; /* session we want to dump, or NULL for all */
121 unsigned int uid; /* if non-null, the uniq_id of the session being dumped */
122 int section; /* section of the session being dumped */
123 int pos; /* last position of the current session's buffer */
124 } sess;
125 struct {
126 int iid; /* if >= 0, ID of the proxy to filter on */
127 struct proxy *px; /* current proxy being dumped, NULL = not started yet. */
128 unsigned int buf; /* buffer being dumped, 0 = req, 1 = rep */
129 unsigned int sid; /* session ID of error being dumped */
130 int ptr; /* <0: headers, >=0 : text pointer to restart from */
131 int bol; /* pointer to beginning of current line */
132 } errors;
133 struct {
134 void *target; /* table we want to dump, or NULL for all */
135 struct proxy *proxy; /* table being currently dumped (first if NULL) */
136 struct stksess *entry; /* last entry we were trying to dump (or first if NULL) */
137 long long value; /* value to compare against */
138 signed char data_type; /* type of data to compare, or -1 if none */
139 signed char data_op; /* operator (STD_OP_*) when data_type set */
140 } table;
141 struct {
142 const char *msg; /* pointer to a persistent message to be returned in PRINT state */
Thierry FOURNIER364cfdf2014-01-29 19:08:49 +0100143 char *err; /* pointer to a 'must free' message to be returned in PRINT_FREE state */
Willy Tarreau452d3bb2013-11-24 02:28:49 +0100144 } cli;
145 struct {
146 void *ptr; /* multi-purpose pointer for peers */
147 } peers;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100148 struct {
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100149 unsigned int display_flags;
150 struct pat_ref *ref;
151 struct pat_ref_elt *elt;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100152 struct map_descriptor *desc;
Thierry FOURNIER1e00d382014-02-11 11:31:40 +0100153 struct pattern_expr *expr;
Thierry FOURNIERc0e0d7b2013-12-11 16:55:52 +0100154 struct chunk chunk;
155 } map;
Thierry FOURNIER7e7ac322015-02-16 19:27:16 +0100156#ifdef USE_LUA
157 struct {
158 int connected;
159 struct hlua_socket *socket;
160 struct list wake_on_read;
161 struct list wake_on_write;
162 } hlua;
163#endif
Willy Tarreau452d3bb2013-11-24 02:28:49 +0100164 } ctx; /* used by stats I/O handlers to dump the stats */
165};
166
Willy Tarreaube5ea192011-03-03 17:08:11 +0100167/* A stream interface has 3 parts :
168 * - the buffer side, which interfaces to the buffers.
169 * - the remote side, which describes the state and address of the other side.
170 * - the functions, which are used by the buffer side to communicate with the
171 * remote side from the buffer side.
172 */
173
Willy Tarreaub24281b2011-02-13 13:16:36 +0100174/* Note that if an applet is registered, the update function will not be called
175 * by the session handler, so it may be used to resync flags at the end of the
176 * applet handler. See stream_int_update_embedded() for reference.
Willy Tarreaufb90d942009-09-05 20:57:35 +0200177 */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200178struct stream_interface {
Willy Tarreaube5ea192011-03-03 17:08:11 +0100179 /* struct members used by the "buffer" side */
Willy Tarreau721854f2013-12-06 23:31:58 +0100180 enum si_state state; /* SI_ST* */
181 enum si_state prev_state;/* SI_ST*, copy of previous state */
182 unsigned short flags; /* SI_FL_* */
Willy Tarreau35374672008-09-03 18:11:02 +0200183 unsigned int exp; /* wake up time for connect, queue, turn-around, ... */
Willy Tarreaub8020ce2013-10-24 21:10:08 +0200184 struct channel *ib, *ob; /* input and output buffers */
Willy Tarreaube5ea192011-03-03 17:08:11 +0100185 void *owner; /* generally a (struct task*) */
Willy Tarreau7d67d7b2013-09-29 16:05:22 +0200186 enum obj_type *end; /* points to the end point (connection or appctx) */
Willy Tarreauc5788912012-08-24 18:12:41 +0200187 struct si_ops *ops; /* general operations at the stream interface layer */
Willy Tarreaube5ea192011-03-03 17:08:11 +0100188
189 /* struct members below are the "remote" part, as seen from the buffer side */
Willy Tarreaub8020ce2013-10-24 21:10:08 +0200190 unsigned int err_type; /* first error detected, one of SI_ET_* */
Willy Tarreauee28de02010-06-01 09:51:00 +0200191 int conn_retries; /* number of connect retries left */
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200192};
193
Willy Tarreaub24281b2011-02-13 13:16:36 +0100194/* An applet designed to run in a stream interface */
195struct si_applet {
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100196 enum obj_type obj_type; /* object type = OBJ_TYPE_APPLET */
Willy Tarreau721854f2013-12-06 23:31:58 +0100197 /* 3 unused bytes here */
Willy Tarreau3fdb3662012-11-12 00:42:33 +0100198 char *name; /* applet's name to report in logs */
199 void (*fct)(struct stream_interface *); /* internal I/O handler, may never be NULL */
Aman Gupta9a13e842012-04-02 18:57:53 -0700200 void (*release)(struct stream_interface *); /* callback to release resources, may be NULL */
Willy Tarreaub24281b2011-02-13 13:16:36 +0100201};
Willy Tarreaufa7e1022008-10-19 07:30:41 +0200202
203#endif /* _TYPES_STREAM_INTERFACE_H */
204
205/*
206 * Local variables:
207 * c-indent-level: 8
208 * c-basic-offset: 8
209 * End:
210 */