blob: b5d448f17116c6500a43c3fcce3c380ae27b7569 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
Willy Tarreau49801602020-06-04 22:50:02 +02002 * include/haproxy/backend-t.h
Willy Tarreau5b4c2b52009-10-03 11:21:53 +02003 * This file assembles definitions for backends
4 *
Willy Tarreau3ebb1162012-02-13 16:57:44 +01005 * Copyright (C) 2000-2012 Willy Tarreau - w@1wt.eu
Willy Tarreau5b4c2b52009-10-03 11:21:53 +02006 *
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 Tarreaubaaee002006-06-26 02:48:02 +020021
Willy Tarreau49801602020-06-04 22:50:02 +020022#ifndef _HAPROXY_BACKEND_T_H
23#define _HAPROXY_BACKEND_T_H
Willy Tarreaubaaee002006-06-26 02:48:02 +020024
Willy Tarreaub2551052020-06-09 09:07:15 +020025#include <haproxy/api-t.h>
Willy Tarreaufbe8da32020-06-04 14:34:27 +020026#include <haproxy/lb_chash-t.h>
Willy Tarreaub5fc3bf2020-06-04 14:37:38 +020027#include <haproxy/lb_fas-t.h>
Willy Tarreau02549412020-06-04 14:41:04 +020028#include <haproxy/lb_fwlc-t.h>
Willy Tarreau546ba422020-06-04 14:45:03 +020029#include <haproxy/lb_fwrr-t.h>
Willy Tarreau28671592020-06-04 20:22:59 +020030#include <haproxy/lb_map-t.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020031#include <haproxy/server-t.h>
Willy Tarreau49801602020-06-04 22:50:02 +020032#include <haproxy/thread-t.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020033
Willy Tarreauf3e49f92009-10-03 12:21:20 +020034/* Parameters for lbprm.algo */
35
36/* Lower bits define the kind of load balancing method, which means the type of
37 * algorithm, and which criterion it is based on. For this reason, those bits
38 * also include information about dependencies, so that the config parser can
39 * detect incompatibilities.
Willy Tarreaudf366142007-11-30 16:23:20 +010040 */
41
Willy Tarreau3ebb1162012-02-13 16:57:44 +010042/* LB parameters are on the lower 8 bits. Depends on the LB kind. */
43
44/* BE_LB_HASH_* is used with BE_LB_KIND_HI */
Willy Tarreauf3e49f92009-10-03 12:21:20 +020045#define BE_LB_HASH_SRC 0x00000 /* hash source IP */
46#define BE_LB_HASH_URI 0x00001 /* hash HTTP URI */
47#define BE_LB_HASH_PRM 0x00002 /* hash HTTP URL parameter */
48#define BE_LB_HASH_HDR 0x00003 /* hash HTTP header value */
49#define BE_LB_HASH_RDP 0x00004 /* hash RDP cookie value */
Willy Tarreau760e81d2018-05-03 07:20:40 +020050#define BE_LB_HASH_RND 0x00008 /* hash a random value */
Willy Tarreau9757a382009-10-03 12:56:50 +020051
Willy Tarreau3ebb1162012-02-13 16:57:44 +010052/* BE_LB_RR_* is used with BE_LB_KIND_RR */
Willy Tarreau9757a382009-10-03 12:56:50 +020053#define BE_LB_RR_DYN 0x00000 /* dynamic round robin (default) */
54#define BE_LB_RR_STATIC 0x00001 /* static round robin */
Willy Tarreau760e81d2018-05-03 07:20:40 +020055#define BE_LB_RR_RANDOM 0x00002 /* random round robin */
Willy Tarreau3ebb1162012-02-13 16:57:44 +010056
57/* BE_LB_CB_* is used with BE_LB_KIND_CB */
58#define BE_LB_CB_LC 0x00000 /* least-connections */
Willy Tarreauf09c6602012-02-13 17:12:08 +010059#define BE_LB_CB_FAS 0x00001 /* first available server (opposite of leastconn) */
Willy Tarreau3ebb1162012-02-13 16:57:44 +010060
Willy Tarreauf3e49f92009-10-03 12:21:20 +020061#define BE_LB_PARM 0x000FF /* mask to get/clear the LB param */
62
63/* Required input(s) */
64#define BE_LB_NEED_NONE 0x00000 /* no input needed */
65#define BE_LB_NEED_ADDR 0x00100 /* only source address needed */
66#define BE_LB_NEED_DATA 0x00200 /* some payload is needed */
67#define BE_LB_NEED_HTTP 0x00400 /* an HTTP request is needed */
68/* not used: 0x0800 */
69#define BE_LB_NEED 0x00F00 /* mask to get/clear dependencies */
70
71/* Algorithm */
72#define BE_LB_KIND_NONE 0x00000 /* algorithm not set */
73#define BE_LB_KIND_RR 0x01000 /* round-robin */
Willy Tarreau3ebb1162012-02-13 16:57:44 +010074#define BE_LB_KIND_CB 0x02000 /* connection-based */
Willy Tarreau6b2e11b2009-10-01 07:52:15 +020075#define BE_LB_KIND_HI 0x03000 /* hash of input (see hash inputs above) */
Willy Tarreauf3e49f92009-10-03 12:21:20 +020076#define BE_LB_KIND 0x07000 /* mask to get/clear LB algorithm */
77
78/* All known variants of load balancing algorithms. These can be cleared using
79 * the BE_LB_ALGO mask. For a check, using BE_LB_KIND is preferred.
80 */
81#define BE_LB_ALGO_NONE (BE_LB_KIND_NONE | BE_LB_NEED_NONE) /* not defined */
82#define BE_LB_ALGO_RR (BE_LB_KIND_RR | BE_LB_NEED_NONE) /* round robin */
Willy Tarreau760e81d2018-05-03 07:20:40 +020083#define BE_LB_ALGO_RND (BE_LB_KIND_RR | BE_LB_NEED_NONE | BE_LB_RR_RANDOM) /* random value */
Willy Tarreau3ebb1162012-02-13 16:57:44 +010084#define BE_LB_ALGO_LC (BE_LB_KIND_CB | BE_LB_NEED_NONE | BE_LB_CB_LC) /* least connections */
Willy Tarreauf09c6602012-02-13 17:12:08 +010085#define BE_LB_ALGO_FAS (BE_LB_KIND_CB | BE_LB_NEED_NONE | BE_LB_CB_FAS) /* first available server */
Willy Tarreau9757a382009-10-03 12:56:50 +020086#define BE_LB_ALGO_SRR (BE_LB_KIND_RR | BE_LB_NEED_NONE | BE_LB_RR_STATIC) /* static round robin */
Willy Tarreauf3e49f92009-10-03 12:21:20 +020087#define BE_LB_ALGO_SH (BE_LB_KIND_HI | BE_LB_NEED_ADDR | BE_LB_HASH_SRC) /* hash: source IP */
88#define BE_LB_ALGO_UH (BE_LB_KIND_HI | BE_LB_NEED_HTTP | BE_LB_HASH_URI) /* hash: HTTP URI */
89#define BE_LB_ALGO_PH (BE_LB_KIND_HI | BE_LB_NEED_HTTP | BE_LB_HASH_PRM) /* hash: HTTP URL parameter */
90#define BE_LB_ALGO_HH (BE_LB_KIND_HI | BE_LB_NEED_HTTP | BE_LB_HASH_HDR) /* hash: HTTP header value */
91#define BE_LB_ALGO_RCH (BE_LB_KIND_HI | BE_LB_NEED_DATA | BE_LB_HASH_RDP) /* hash: RDP cookie value */
92#define BE_LB_ALGO (BE_LB_KIND | BE_LB_NEED | BE_LB_PARM ) /* mask to clear algo */
93
94/* Higher bits define how a given criterion is mapped to a server. In fact it
95 * designates the LB function by itself. The dynamic algorithms will also have
96 * the DYN bit set. These flags are automatically set at the end of the parsing.
97 */
98#define BE_LB_LKUP_NONE 0x00000 /* not defined */
99#define BE_LB_LKUP_MAP 0x10000 /* static map based lookup */
100#define BE_LB_LKUP_RRTREE 0x20000 /* FWRR tree lookup */
101#define BE_LB_LKUP_LCTREE 0x30000 /* FWLC tree lookup */
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200102#define BE_LB_LKUP_CHTREE 0x40000 /* consistent hash */
Willy Tarreauf09c6602012-02-13 17:12:08 +0100103#define BE_LB_LKUP_FSTREE 0x50000 /* FAS tree lookup */
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200104#define BE_LB_LKUP 0x70000 /* mask to get just the LKUP value */
105
106/* additional properties */
107#define BE_LB_PROP_DYN 0x80000 /* bit to indicate a dynamic algorithm */
108
Willy Tarreau6b2e11b2009-10-01 07:52:15 +0200109/* hash types */
110#define BE_LB_HASH_MAP 0x000000 /* map-based hash (default) */
111#define BE_LB_HASH_CONS 0x100000 /* consistent hashbit to indicate a dynamic algorithm */
Bhaskar Maddalab6c0ac92013-11-05 11:54:02 -0500112#define BE_LB_HASH_TYPE 0x100000 /* get/clear hash types */
113
114/* additional modifier on top of the hash function (only avalanche right now) */
115#define BE_LB_HMOD_AVAL 0x200000 /* avalanche modifier */
116#define BE_LB_HASH_MOD 0x200000 /* get/clear hash modifier */
Willy Tarreaub625a082007-11-26 01:15:43 +0100117
Bhaskar98634f02013-10-29 23:30:51 -0400118/* BE_LB_HFCN_* is the hash function, to be used with BE_LB_HASH_FUNC */
119#define BE_LB_HFCN_SDBM 0x000000 /* sdbm hash */
120#define BE_LB_HFCN_DJB2 0x400000 /* djb2 hash */
Willy Tarreaua0f42712013-11-14 14:30:35 +0100121#define BE_LB_HFCN_WT6 0x800000 /* wt6 hash */
Willy Tarreau324f07f2015-01-20 19:44:50 +0100122#define BE_LB_HFCN_CRC32 0xC00000 /* crc32 hash */
Bhaskar98634f02013-10-29 23:30:51 -0400123#define BE_LB_HASH_FUNC 0xC00000 /* get/clear hash function */
124
125
Willy Tarreaub625a082007-11-26 01:15:43 +0100126/* various constants */
Willy Tarreaub698f0f2007-12-02 11:01:23 +0100127
matt.farnsworth@nokia.com1c2ab962008-04-14 20:47:37 +0200128/* The scale factor between user weight and effective weight allows smooth
Willy Tarreaub698f0f2007-12-02 11:01:23 +0100129 * weight modulation even with small weights (eg: 1). It should not be too high
130 * though because it limits the number of servers in FWRR mode in order to
131 * prevent any integer overflow. The max number of servers per backend is
Godbacha34bdc02013-07-22 07:44:53 +0800132 * limited to about (2^32-1)/256^2/scale ~= 65535.9999/scale. A scale of 16
133 * looks like a good value, as it allows 4095 servers per backend while leaving
Willy Tarreaub698f0f2007-12-02 11:01:23 +0100134 * modulation steps of about 6% for servers with the lowest weight (1).
135 */
136#define BE_WEIGHT_SCALE 16
Willy Tarreaub625a082007-11-26 01:15:43 +0100137
Willy Tarreau5b4c2b52009-10-03 11:21:53 +0200138/* LB parameters for all algorithms */
139struct lbprm {
Willy Tarreau0cac26c2019-01-14 16:55:42 +0100140 union { /* LB parameters depending on the algo type */
141 struct lb_map map;
142 struct lb_fwrr fwrr;
143 struct lb_fwlc fwlc;
144 struct lb_chash chash;
145 struct lb_fas fas;
146 };
Willy Tarreauf3e49f92009-10-03 12:21:20 +0200147 int algo; /* load balancing algorithm and variants: BE_LB_* */
Willy Tarreau5b4c2b52009-10-03 11:21:53 +0200148 int tot_wact, tot_wbck; /* total effective weights of active and backup servers */
149 int tot_weight; /* total effective weight of servers participating to LB */
Willy Tarreaubd715102020-10-23 22:44:30 +0200150 int tot_uweight; /* total user weight of servers participating to LB (for reporting) */
Willy Tarreau5b4c2b52009-10-03 11:21:53 +0200151 int tot_used; /* total number of servers used for LB */
152 int wmult; /* ratio between user weight and effective weight */
153 int wdiv; /* ratio between effective weight and user weight */
Willy Tarreau76e84f52019-01-14 16:50:58 +0100154 int hash_balance_factor; /* load balancing factor * 100, 0 if disabled */
Willy Tarreau484ff072019-01-14 15:28:53 +0100155 char *arg_str; /* name of the URL parameter/header/cookie used for hashing */
Willy Tarreau4c03d1c2019-01-14 15:23:54 +0100156 int arg_len; /* strlen(arg_str), computed only once */
Willy Tarreau20e68372019-01-14 16:04:01 +0100157 int arg_opt1; /* extra option 1 for the LB algo (algo-specific) */
158 int arg_opt2; /* extra option 2 for the LB algo (algo-specific) */
159 int arg_opt3; /* extra option 3 for the LB algo (algo-specific) */
vend_natalie.chen732edc92019-12-09 14:43:49 +0800160 int arg_opt4; /* extra option 4 for the LB algo (algo-specific) */
Willy Tarreaucd10def2020-10-17 18:48:47 +0200161 __decl_thread(HA_RWLOCK_T lock);
Willy Tarreaubd715102020-10-23 22:44:30 +0200162 struct server *fbck; /* first backup server when !PR_O_USE_ALL_BK, or NULL */
Christopher Faulet9dcf9b62017-11-13 10:34:01 +0100163
Willy Tarreau59b0fec2021-02-17 16:01:37 +0100164 /* Call backs for some actions. Any of them may be NULL (thus should be ignored).
165 * Those marked "srvlock" will need to be called with the server lock held.
166 * The other ones might take it themselves if needed, based on indications.
167 */
168 void (*update_server_eweight)(struct server *); /* to be called after eweight change // srvlock */
169 void (*set_server_status_up)(struct server *); /* to be called after status changes to UP // srvlock */
170 void (*set_server_status_down)(struct server *); /* to be called after status changes to DOWN // srvlock */
171 void (*server_take_conn)(struct server *, int locked); /* to be called when connection is assigned */
172 void (*server_drop_conn)(struct server *, int locked); /* to be called when connection is dropped */
Willy Tarreau5b4c2b52009-10-03 11:21:53 +0200173};
174
Willy Tarreau49801602020-06-04 22:50:02 +0200175#endif /* _HAPROXY_BACKEND_T_H */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200176
177/*
178 * Local variables:
179 * c-indent-level: 8
180 * c-basic-offset: 8
181 * End:
182 */