blob: 0d0c28aec6bf463bc14c3bfc3ab68a335a944b97 [file] [log] [blame]
Willy Tarreau8987e7a2020-08-28 11:37:21 +02001/*
2 * AF_INET/AF_INET6 SOCK_STREAM protocol layer (tcp)
3 *
4 * Copyright 2000-2013 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13/* this is to have tcp_info defined on systems using musl
14 * library, such as Alpine Linux.
15 */
16#define _GNU_SOURCE
17
18#include <ctype.h>
19#include <errno.h>
Willy Tarreau8987e7a2020-08-28 11:37:21 +020020#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <time.h>
24
25#include <sys/param.h>
26#include <sys/socket.h>
27#include <sys/types.h>
28
29#include <netinet/tcp.h>
30#include <netinet/in.h>
31
32#include <haproxy/api.h>
33#include <haproxy/arg.h>
34#include <haproxy/connection.h>
Christopher Faulet8da67aa2022-03-29 17:53:09 +020035#include <haproxy/cs_utils.h>
Willy Tarreau6cd007d2021-10-06 19:01:21 +020036#include <haproxy/errors.h>
Willy Tarreau8987e7a2020-08-28 11:37:21 +020037#include <haproxy/global.h>
38#include <haproxy/listener-t.h>
39#include <haproxy/namespace.h>
40#include <haproxy/proxy-t.h>
41#include <haproxy/sample.h>
Christopher Fauletc03be1a2021-10-25 08:01:20 +020042#include <haproxy/session.h>
Willy Tarreau8987e7a2020-08-28 11:37:21 +020043#include <haproxy/tools.h>
44
Christopher Faulet7d081f02021-04-15 09:38:37 +020045/* Fetch the connection's source IPv4/IPv6 address. Depending on the keyword, it
46 * may be the frontend or the backend connection.
Willy Tarreau8987e7a2020-08-28 11:37:21 +020047 */
48static int
49smp_fetch_src(const struct arg *args, struct sample *smp, const char *kw, void *private)
50{
Christopher Fauletc03be1a2021-10-25 08:01:20 +020051 const struct sockaddr_storage *src = NULL;
Christopher Faulet003df1c2021-04-15 09:39:38 +020052
Christopher Faulet888cd702021-10-25 16:58:50 +020053 if (kw[0] == 'b') { /* bc_src */
Christopher Fauletc03be1a2021-10-25 08:01:20 +020054 struct connection *conn = ((obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
55 ? cs_conn(__objt_check(smp->sess->origin)->cs)
Christopher Faulet95a61e82021-12-22 14:22:03 +010056 : (smp->strm ? cs_conn(smp->strm->csb): NULL));
Christopher Fauletc03be1a2021-10-25 08:01:20 +020057 if (conn && conn_get_src(conn))
58 src = conn_src(conn);
59 }
Christopher Faulet888cd702021-10-25 16:58:50 +020060 else if (kw[0] == 'f') { /* fc_src */
61 struct connection *conn = objt_conn(smp->sess->origin);
62
63 if (conn && conn_get_src(conn))
64 src = conn_src(conn);
65 }
66 else /* src */
Christopher Faulet8da67aa2022-03-29 17:53:09 +020067 src = (smp->strm ? cs_src(smp->strm->csf) : sess_src(smp->sess));
Willy Tarreau8987e7a2020-08-28 11:37:21 +020068
Christopher Fauletc03be1a2021-10-25 08:01:20 +020069 if (!src)
Willy Tarreau8987e7a2020-08-28 11:37:21 +020070 return 0;
71
Christopher Fauletc03be1a2021-10-25 08:01:20 +020072 switch (src->ss_family) {
Willy Tarreau8987e7a2020-08-28 11:37:21 +020073 case AF_INET:
Christopher Fauletc03be1a2021-10-25 08:01:20 +020074 smp->data.u.ipv4 = ((struct sockaddr_in *)src)->sin_addr;
Willy Tarreau8987e7a2020-08-28 11:37:21 +020075 smp->data.type = SMP_T_IPV4;
76 break;
77 case AF_INET6:
Christopher Fauletc03be1a2021-10-25 08:01:20 +020078 smp->data.u.ipv6 = ((struct sockaddr_in6 *)src)->sin6_addr;
Willy Tarreau8987e7a2020-08-28 11:37:21 +020079 smp->data.type = SMP_T_IPV6;
80 break;
81 default:
82 return 0;
83 }
84
85 smp->flags = 0;
86 return 1;
87}
88
Christopher Faulet7d081f02021-04-15 09:38:37 +020089/* set temp integer to the connection's source port. Depending on the
90 * keyword, it may be the frontend or the backend connection.
91 */
Willy Tarreau8987e7a2020-08-28 11:37:21 +020092static int
Christopher Faulet7d081f02021-04-15 09:38:37 +020093smp_fetch_sport(const struct arg *args, struct sample *smp, const char *kw, void *private)
Willy Tarreau8987e7a2020-08-28 11:37:21 +020094{
Christopher Fauletc03be1a2021-10-25 08:01:20 +020095 const struct sockaddr_storage *src = NULL;
Christopher Faulet003df1c2021-04-15 09:39:38 +020096
Christopher Faulet888cd702021-10-25 16:58:50 +020097 if (kw[0] == 'b') { /* bc_src_port */
Christopher Fauletc03be1a2021-10-25 08:01:20 +020098 struct connection *conn = ((obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
99 ? cs_conn(__objt_check(smp->sess->origin)->cs)
Christopher Faulet95a61e82021-12-22 14:22:03 +0100100 : (smp->strm ? cs_conn(smp->strm->csb): NULL));
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200101 if (conn && conn_get_src(conn))
102 src = conn_src(conn);
103 }
Christopher Faulet888cd702021-10-25 16:58:50 +0200104 else if (kw[0] == 'f') { /* fc_src_port */
105 struct connection *conn = objt_conn(smp->sess->origin);
106
107 if (conn && conn_get_src(conn))
108 src = conn_src(conn);
109 }
110 else /* src_port */
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200111 src = (smp->strm ? cs_src(smp->strm->csf) : sess_src(smp->sess));
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200112
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200113 if (!src)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200114 return 0;
115
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200116 smp->data.type = SMP_T_SINT;
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200117 if (!(smp->data.u.sint = get_host_port(src)))
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200118 return 0;
119
120 smp->flags = 0;
121 return 1;
122}
123
Christopher Faulet7d081f02021-04-15 09:38:37 +0200124/* fetch the connection's destination IPv4/IPv6 address. Depending on the
125 * keyword, it may be the frontend or the backend connection.
126 */
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200127static int
128smp_fetch_dst(const struct arg *args, struct sample *smp, const char *kw, void *private)
129{
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200130 const struct sockaddr_storage *dst = NULL;
Christopher Faulet003df1c2021-04-15 09:39:38 +0200131
Christopher Faulet888cd702021-10-25 16:58:50 +0200132 if (kw[0] == 'b') { /* bc_dst */
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200133 struct connection *conn = ((obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
134 ? cs_conn(__objt_check(smp->sess->origin)->cs)
Christopher Faulet95a61e82021-12-22 14:22:03 +0100135 : (smp->strm ? cs_conn(smp->strm->csb): NULL));
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200136 if (conn && conn_get_dst(conn))
137 dst = conn_dst(conn);
138 }
Christopher Faulet888cd702021-10-25 16:58:50 +0200139 else if (kw[0] == 'f') { /* fc_dst */
140 struct connection *conn = objt_conn(smp->sess->origin);
141
142 if (conn && conn_get_dst(conn))
143 dst = conn_dst(conn);
144 }
145 else /* dst */
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200146 dst = (smp->strm ? cs_dst(smp->strm->csf) : sess_dst(smp->sess));
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200147
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200148 if (!dst)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200149 return 0;
150
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200151 switch (dst->ss_family) {
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200152 case AF_INET:
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200153 smp->data.u.ipv4 = ((struct sockaddr_in *)dst)->sin_addr;
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200154 smp->data.type = SMP_T_IPV4;
155 break;
156 case AF_INET6:
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200157 smp->data.u.ipv6 = ((struct sockaddr_in6 *)dst)->sin6_addr;
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200158 smp->data.type = SMP_T_IPV6;
159 break;
160 default:
161 return 0;
162 }
163
164 smp->flags = 0;
165 return 1;
166}
167
168/* check if the destination address of the front connection is local to the
169 * system or if it was intercepted.
170 */
171int smp_fetch_dst_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
172{
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200173 struct listener *li = smp->sess->listener;
Christopher Faulet888cd702021-10-25 16:58:50 +0200174 const struct sockaddr_storage *dst = NULL;
175
176 if (kw[0] == 'f') { /* fc_dst_is_local */
177 struct connection *conn = objt_conn(smp->sess->origin);
178
179 if (conn && conn_get_src(conn))
180 dst = conn_dst(conn);
181 }
182 else /* dst_is_local */
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200183 dst = (smp->strm ? cs_dst(smp->strm->csf) : sess_dst(smp->sess));
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200184
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200185 if (!dst)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200186 return 0;
187
188 smp->data.type = SMP_T_BOOL;
189 smp->flags = 0;
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200190 smp->data.u.sint = addr_is_local(li->rx.settings->netns, dst);
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200191 return smp->data.u.sint >= 0;
192}
193
194/* check if the source address of the front connection is local to the system
195 * or not.
196 */
197int smp_fetch_src_is_local(const struct arg *args, struct sample *smp, const char *kw, void *private)
198{
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200199 struct listener *li = smp->sess->listener;
Christopher Faulet888cd702021-10-25 16:58:50 +0200200 const struct sockaddr_storage *src = NULL;
201
202 if (kw[0] == 'f') { /* fc_src_is_local */
203 struct connection *conn = objt_conn(smp->sess->origin);
204
205 if (conn && conn_get_src(conn))
206 src = conn_src(conn);
207 }
208 else /* src_is_local */
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200209 src = (smp->strm ? cs_src(smp->strm->csf) : sess_src(smp->sess));
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200210
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200211 if (!src)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200212 return 0;
213
214 smp->data.type = SMP_T_BOOL;
215 smp->flags = 0;
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200216 smp->data.u.sint = addr_is_local(li->rx.settings->netns, src);
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200217 return smp->data.u.sint >= 0;
218}
219
Christopher Faulet7d081f02021-04-15 09:38:37 +0200220/* set temp integer to the connexion's destination port. Depending on the
221 * keyword, it may be the frontend or the backend connection.
222 */
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200223static int
224smp_fetch_dport(const struct arg *args, struct sample *smp, const char *kw, void *private)
225{
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200226 const struct sockaddr_storage *dst = NULL;
Christopher Faulet003df1c2021-04-15 09:39:38 +0200227
Christopher Faulet888cd702021-10-25 16:58:50 +0200228 if (kw[0] == 'b') { /* bc_dst_port */
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200229 struct connection *conn = ((obj_type(smp->sess->origin) == OBJ_TYPE_CHECK)
230 ? cs_conn(__objt_check(smp->sess->origin)->cs)
Christopher Faulet95a61e82021-12-22 14:22:03 +0100231 : (smp->strm ? cs_conn(smp->strm->csb): NULL));
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200232 if (conn && conn_get_dst(conn))
233 dst = conn_dst(conn);
234 }
Christopher Faulet888cd702021-10-25 16:58:50 +0200235 else if (kw[0] == 'f') { /* fc_dst_post */
236 struct connection *conn = objt_conn(smp->sess->origin);
237
238 if (conn && conn_get_src(conn))
239 dst = conn_dst(conn);
240 }
241 else /* dst_port */
Christopher Faulet8da67aa2022-03-29 17:53:09 +0200242 dst = (smp->strm ? cs_dst(smp->strm->csf) : sess_dst(smp->sess));
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200243
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200244 if (!dst)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200245 return 0;
246
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200247 smp->data.type = SMP_T_SINT;
Christopher Fauletc03be1a2021-10-25 08:01:20 +0200248 if (!(smp->data.u.sint = get_host_port(dst)))
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200249 return 0;
250
251 smp->flags = 0;
252 return 1;
253}
254
255#ifdef TCP_INFO
256
257
258/* Validates the arguments passed to "fc_*" fetch keywords returning a time
259 * value. These keywords support an optional string representing the unit of the
260 * result: "us" for microseconds and "ms" for milliseconds". Returns 0 on error
261 * and non-zero if OK.
262 */
263static int val_fc_time_value(struct arg *args, char **err)
264{
265 if (args[0].type == ARGT_STR) {
266 if (strcmp(args[0].data.str.area, "us") == 0) {
267 chunk_destroy(&args[0].data.str);
268 args[0].type = ARGT_SINT;
269 args[0].data.sint = TIME_UNIT_US;
270 }
271 else if (strcmp(args[0].data.str.area, "ms") == 0) {
272 chunk_destroy(&args[0].data.str);
273 args[0].type = ARGT_SINT;
274 args[0].data.sint = TIME_UNIT_MS;
275 }
276 else {
277 memprintf(err, "expects 'us' or 'ms', got '%s'",
278 args[0].data.str.area);
279 return 0;
280 }
281 }
282 else {
283 memprintf(err, "Unexpected arg type");
284 return 0;
285 }
286
287 return 1;
288}
289
290/* Validates the arguments passed to "fc_*" fetch keywords returning a
291 * counter. These keywords should be used without any keyword, but because of a
292 * bug in previous versions, an optional string argument may be passed. In such
293 * case, the argument is ignored and a warning is emitted. Returns 0 on error
294 * and non-zero if OK.
295 */
296static int var_fc_counter(struct arg *args, char **err)
297{
298 if (args[0].type != ARGT_STOP) {
299 ha_warning("no argument supported for 'fc_*' sample expressions returning counters.\n");
300 if (args[0].type == ARGT_STR)
301 chunk_destroy(&args[0].data.str);
302 args[0].type = ARGT_STOP;
303 }
304
305 return 1;
306}
307
308/* Returns some tcp_info data if it's available. "dir" must be set to 0 if
309 * the client connection is required, otherwise it is set to 1. "val" represents
310 * the required value.
311 * If the function fails it returns 0, otherwise it returns 1 and "result" is filled.
312 */
313static inline int get_tcp_info(const struct arg *args, struct sample *smp,
314 int dir, int val)
315{
316 struct connection *conn;
317 struct tcp_info info;
318 socklen_t optlen;
319
320 /* strm can be null. */
321 if (!smp->strm)
322 return 0;
323
Christopher Faulet6b0a0fb2022-04-04 11:29:28 +0200324 /* get the object associated with the conn-stream.The
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200325 * object can be other thing than a connection. For example,
326 * it be a appctx. */
Christopher Faulet95a61e82021-12-22 14:22:03 +0100327 conn = (dir == 0 ? cs_conn(smp->strm->csf) : cs_conn(smp->strm->csb));
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200328 if (!conn)
329 return 0;
330
331 /* The fd may not be available for the tcp_info struct, and the
332 syscal can fail. */
333 optlen = sizeof(info);
Willy Tarreaue2226792022-04-11 18:04:33 +0200334 if ((conn->flags & CO_FL_FDLESS) ||
335 getsockopt(conn->handle.fd, IPPROTO_TCP, TCP_INFO, &info, &optlen) == -1)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200336 return 0;
337
338 /* extract the value. */
339 smp->data.type = SMP_T_SINT;
340 switch (val) {
David CARLIER7747d462022-04-11 12:53:11 +0100341#if defined(__APPLE__)
342 case 0: smp->data.u.sint = info.tcpi_rttcur; break;
343 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
344 case 2: smp->data.u.sint = info.tcpi_tfo_syn_data_acked; break;
345 case 4: smp->data.u.sint = info.tcpi_tfo_syn_loss; break;
346 case 5: smp->data.u.sint = info.tcpi_rto; break;
347#else
348 /* all other platforms supporting TCP_INFO have these ones */
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200349 case 0: smp->data.u.sint = info.tcpi_rtt; break;
350 case 1: smp->data.u.sint = info.tcpi_rttvar; break;
David CARLIER7747d462022-04-11 12:53:11 +0100351# if defined(__linux__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200352 /* these ones are common to all Linux versions */
353 case 2: smp->data.u.sint = info.tcpi_unacked; break;
354 case 3: smp->data.u.sint = info.tcpi_sacked; break;
355 case 4: smp->data.u.sint = info.tcpi_lost; break;
356 case 5: smp->data.u.sint = info.tcpi_retrans; break;
357 case 6: smp->data.u.sint = info.tcpi_fackets; break;
358 case 7: smp->data.u.sint = info.tcpi_reordering; break;
David CARLIER7747d462022-04-11 12:53:11 +0100359# elif defined(__FreeBSD__) || defined(__NetBSD__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200360 /* the ones are found on FreeBSD and NetBSD featuring TCP_INFO */
361 case 2: smp->data.u.sint = info.__tcpi_unacked; break;
362 case 3: smp->data.u.sint = info.__tcpi_sacked; break;
363 case 4: smp->data.u.sint = info.__tcpi_lost; break;
364 case 5: smp->data.u.sint = info.__tcpi_retrans; break;
365 case 6: smp->data.u.sint = info.__tcpi_fackets; break;
366 case 7: smp->data.u.sint = info.__tcpi_reordering; break;
David CARLIER7747d462022-04-11 12:53:11 +0100367# endif
368#endif // apple
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200369 default: return 0;
370 }
371
372 return 1;
373}
374
David CARLIER7747d462022-04-11 12:53:11 +0100375#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200376/* get the mean rtt of a client connection */
377static int
378smp_fetch_fc_rtt(const struct arg *args, struct sample *smp, const char *kw, void *private)
379{
380 if (!get_tcp_info(args, smp, 0, 0))
381 return 0;
382
383 /* By default or if explicitly specified, convert rtt to ms */
384 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
385 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
386
387 return 1;
388}
David CARLIER5c83e3a2022-04-11 12:41:24 +0100389#endif
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200390
David CARLIER7747d462022-04-11 12:53:11 +0100391#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200392/* get the variance of the mean rtt of a client connection */
393static int
394smp_fetch_fc_rttvar(const struct arg *args, struct sample *smp, const char *kw, void *private)
395{
396 if (!get_tcp_info(args, smp, 0, 1))
397 return 0;
398
399 /* By default or if explicitly specified, convert rttvar to ms */
400 if (!args || args[0].type == ARGT_STOP || args[0].data.sint == TIME_UNIT_MS)
401 smp->data.u.sint = (smp->data.u.sint + 500) / 1000;
402
403 return 1;
404}
David CARLIER5c83e3a2022-04-11 12:41:24 +0100405#endif
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200406
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200407
David CARLIER7747d462022-04-11 12:53:11 +0100408#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200409/* get the unacked counter on a client connection */
410static int
411smp_fetch_fc_unacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
412{
413 if (!get_tcp_info(args, smp, 0, 2))
414 return 0;
415 return 1;
416}
David CARLIER5c83e3a2022-04-11 12:41:24 +0100417#endif
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200418
David CARLIER5c83e3a2022-04-11 12:41:24 +0100419#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200420/* get the sacked counter on a client connection */
421static int
422smp_fetch_fc_sacked(const struct arg *args, struct sample *smp, const char *kw, void *private)
423{
424 if (!get_tcp_info(args, smp, 0, 3))
425 return 0;
426 return 1;
427}
David CARLIER5c83e3a2022-04-11 12:41:24 +0100428#endif
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200429
David CARLIER7747d462022-04-11 12:53:11 +0100430#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200431/* get the lost counter on a client connection */
432static int
433smp_fetch_fc_lost(const struct arg *args, struct sample *smp, const char *kw, void *private)
434{
435 if (!get_tcp_info(args, smp, 0, 4))
436 return 0;
437 return 1;
438}
David CARLIER5c83e3a2022-04-11 12:41:24 +0100439#endif
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200440
David CARLIER7747d462022-04-11 12:53:11 +0100441#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200442/* get the retrans counter on a client connection */
443static int
444smp_fetch_fc_retrans(const struct arg *args, struct sample *smp, const char *kw, void *private)
445{
446 if (!get_tcp_info(args, smp, 0, 5))
447 return 0;
448 return 1;
449}
David CARLIER5c83e3a2022-04-11 12:41:24 +0100450#endif
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200451
David CARLIER5c83e3a2022-04-11 12:41:24 +0100452#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200453/* get the fackets counter on a client connection */
454static int
455smp_fetch_fc_fackets(const struct arg *args, struct sample *smp, const char *kw, void *private)
456{
457 if (!get_tcp_info(args, smp, 0, 6))
458 return 0;
459 return 1;
460}
David CARLIER5c83e3a2022-04-11 12:41:24 +0100461#endif
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200462
David CARLIER5c83e3a2022-04-11 12:41:24 +0100463#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200464/* get the reordering counter on a client connection */
465static int
466smp_fetch_fc_reordering(const struct arg *args, struct sample *smp, const char *kw, void *private)
467{
468 if (!get_tcp_info(args, smp, 0, 7))
469 return 0;
470 return 1;
471}
David CARLIER5c83e3a2022-04-11 12:41:24 +0100472#endif
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200473#endif // TCP_INFO
474
475/* Note: must not be declared <const> as its list will be overwritten.
476 * Note: fetches that may return multiple types must be declared as the lowest
477 * common denominator, the type that can be casted into all other ones. For
478 * instance v4/v6 must be declared v4.
479 */
480static struct sample_fetch_kw_list sample_fetch_keywords = {ILH, {
Christopher Faulet7d081f02021-04-15 09:38:37 +0200481 { "bc_dst", smp_fetch_dst, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
482 { "bc_dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
483 { "bc_src", smp_fetch_src, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
484 { "bc_src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4SRV },
485
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200486 { "dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
487 { "dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
488 { "dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
Christopher Faulet888cd702021-10-25 16:58:50 +0200489
490 { "fc_dst", smp_fetch_dst, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
491 { "fc_dst_is_local", smp_fetch_dst_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
492 { "fc_dst_port", smp_fetch_dport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
493
494 { "fc_src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
495 { "fc_src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
496 { "fc_src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
497
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200498 { "src", smp_fetch_src, 0, NULL, SMP_T_IPV4, SMP_USE_L4CLI },
499 { "src_is_local", smp_fetch_src_is_local, 0, NULL, SMP_T_BOOL, SMP_USE_L4CLI },
500 { "src_port", smp_fetch_sport, 0, NULL, SMP_T_SINT, SMP_USE_L4CLI },
501#ifdef TCP_INFO
502 { "fc_rtt", smp_fetch_fc_rtt, ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
503 { "fc_rttvar", smp_fetch_fc_rttvar, ARG1(0,STR), val_fc_time_value, SMP_T_SINT, SMP_USE_L4CLI },
David CARLIER7747d462022-04-11 12:53:11 +0100504#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200505 { "fc_unacked", smp_fetch_fc_unacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
David CARLIER5c83e3a2022-04-11 12:41:24 +0100506#endif
507#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200508 { "fc_sacked", smp_fetch_fc_sacked, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
David CARLIER5c83e3a2022-04-11 12:41:24 +0100509#endif
David CARLIER7747d462022-04-11 12:53:11 +0100510#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200511 { "fc_retrans", smp_fetch_fc_retrans, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
David CARLIER5c83e3a2022-04-11 12:41:24 +0100512#endif
513#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200514 { "fc_fackets", smp_fetch_fc_fackets, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
David CARLIER5c83e3a2022-04-11 12:41:24 +0100515#endif
David CARLIER7747d462022-04-11 12:53:11 +0100516#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__) || defined(__APPLE__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200517 { "fc_lost", smp_fetch_fc_lost, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
David CARLIER5c83e3a2022-04-11 12:41:24 +0100518#endif
519#if defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200520 { "fc_reordering", smp_fetch_fc_reordering, ARG1(0,STR), var_fc_counter, SMP_T_SINT, SMP_USE_L4CLI },
David CARLIER5c83e3a2022-04-11 12:41:24 +0100521#endif
Willy Tarreau8987e7a2020-08-28 11:37:21 +0200522#endif // TCP_INFO
523 { /* END */ },
524}};
525
526INITCALL1(STG_REGISTER, sample_register_fetches, &sample_fetch_keywords);
527
528
529/*
530 * Local variables:
531 * c-indent-level: 8
532 * c-basic-offset: 8
533 * End:
534 */