summaryrefslogtreecommitdiffstats
path: root/dev/flags
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 12:18:05 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-13 12:18:05 +0000
commitb46aad6df449445a9fc4aa7b32bd40005438e3f7 (patch)
tree751aa858ca01f35de800164516b298887382919d /dev/flags
parentInitial commit. (diff)
downloadhaproxy-b46aad6df449445a9fc4aa7b32bd40005438e3f7.tar.xz
haproxy-b46aad6df449445a9fc4aa7b32bd40005438e3f7.zip
Adding upstream version 2.9.5.upstream/2.9.5
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r--dev/flags/README12
-rw-r--r--dev/flags/flags.c157
-rwxr-xr-xdev/flags/show-fd-to-flags.sh2
-rwxr-xr-xdev/flags/show-sess-to-flags.sh209
4 files changed, 380 insertions, 0 deletions
diff --git a/dev/flags/README b/dev/flags/README
new file mode 100644
index 0000000..f3730c7
--- /dev/null
+++ b/dev/flags/README
@@ -0,0 +1,12 @@
+This needs to be built from the top makefile, for example :
+
+ make dev/flags/flags
+
+Then the executable is usable either one value at a time from the
+command line, either with values coming from stdin with "-" passed
+alone instead of the value.
+
+It is possible to restrict the decoding to certain fields only by
+specifying one of "ana", "chn", "conn", "sc", "si", "sierr", "strm",
+"task", or "txn" before the value.
+
diff --git a/dev/flags/flags.c b/dev/flags/flags.c
new file mode 100644
index 0000000..65af237
--- /dev/null
+++ b/dev/flags/flags.c
@@ -0,0 +1,157 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+/* make the include files below expose their flags */
+#define HA_EXPOSE_FLAGS
+
+#include <haproxy/channel-t.h>
+#include <haproxy/connection-t.h>
+#include <haproxy/fd-t.h>
+#include <haproxy/http_ana-t.h>
+#include <haproxy/htx-t.h>
+#include <haproxy/mux_fcgi-t.h>
+#include <haproxy/mux_h2-t.h>
+#include <haproxy/mux_h1-t.h>
+#include <haproxy/stconn-t.h>
+#include <haproxy/stream-t.h>
+#include <haproxy/task-t.h>
+
+// 1 bit per flag, no hole permitted here
+#define SHOW_AS_ANA 0x00000001
+#define SHOW_AS_CHN 0x00000002
+#define SHOW_AS_CONN 0x00000004
+#define SHOW_AS_SC 0x00000008
+#define SHOW_AS_SET 0x00000010
+#define SHOW_AS_STRM 0x00000020
+#define SHOW_AS_TASK 0x00000040
+#define SHOW_AS_TXN 0x00000080
+#define SHOW_AS_SD 0x00000100
+#define SHOW_AS_HSL 0x00000200
+#define SHOW_AS_HTX 0x00000400
+#define SHOW_AS_HMSG 0x00000800
+#define SHOW_AS_FD 0x00001000
+#define SHOW_AS_H2C 0x00002000
+#define SHOW_AS_H2S 0x00004000
+#define SHOW_AS_H1C 0x00008000
+#define SHOW_AS_H1S 0x00010000
+#define SHOW_AS_FCONN 0x00020000
+#define SHOW_AS_FSTRM 0x00040000
+
+// command line names, must be in exact same order as the SHOW_AS_* flags above
+// so that show_as_words[i] matches flag 1U<<i.
+const char *show_as_words[] = { "ana", "chn", "conn", "sc", "stet", "strm", "task", "txn", "sd", "hsl", "htx", "hmsg", "fd", "h2c", "h2s", "h1c", "h1s", "fconn", "fstrm"};
+
+/* will be sufficient for even largest flag names */
+static char buf[4096];
+static size_t bsz = sizeof(buf);
+
+unsigned int get_show_as(const char *word)
+{
+ int w = 0;
+
+ while (1) {
+ if (w == sizeof(show_as_words) / sizeof(*show_as_words))
+ return 0;
+ if (strcmp(word, show_as_words[w]) == 0)
+ return 1U << w;
+ w++;
+ }
+}
+
+void usage_exit(const char *name)
+{
+ int word, nbword;
+
+ fprintf(stderr, "Usage: %s [", name);
+
+ nbword = sizeof(show_as_words) / sizeof(*show_as_words);
+ for (word = 0; word < nbword; word++)
+ fprintf(stderr, "%s%s", word ? "|" : "", show_as_words[word]);
+ fprintf(stderr, "]* { [+-][0x]value* | - }\n");
+ exit(1);
+}
+
+int main(int argc, char **argv)
+{
+ unsigned int flags;
+ unsigned int show_as = 0;
+ unsigned int f;
+ const char *name = argv[0];
+ char line[20];
+ char *value;
+ int multi = 0;
+ int use_stdin = 0;
+ char *err;
+
+ while (argc > 0) {
+ argv++; argc--;
+ if (argc < 1)
+ usage_exit(name);
+
+ f = get_show_as(argv[0]);
+ if (!f)
+ break;
+ show_as |= f;
+ }
+
+ if (!show_as)
+ show_as = ~0U;
+
+ if (argc > 1)
+ multi = 1;
+
+ if (strcmp(argv[0], "-") == 0)
+ use_stdin = 1;
+
+ while (argc > 0) {
+ if (use_stdin) {
+ value = fgets(line, sizeof(line), stdin);
+ if (!value)
+ break;
+
+ /* skip common leading delimiters that slip from copy-paste */
+ while (*value == ' ' || *value == '\t' || *value == ':' || *value == '=')
+ value++;
+
+ /* stop at the end of the number and trim any C suffix like "UL" */
+ err = value;
+ while (*err == '-' || *err == '+' ||
+ (isalnum((unsigned char)*err) && toupper((unsigned char)*err) != 'U' && toupper((unsigned char)*err) != 'L'))
+ err++;
+ *err = 0;
+ } else {
+ value = argv[0];
+ argv++; argc--;
+ }
+
+ flags = strtoul(value, &err, 0);
+ if (!*value || *err) {
+ fprintf(stderr, "Unparsable value: <%s>\n", value);
+ usage_exit(name);
+ }
+
+ if (multi || use_stdin)
+ printf("### 0x%08x:\n", flags);
+
+ if (show_as & SHOW_AS_ANA) printf("chn->ana = %s\n", (chn_show_analysers(buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_CHN) printf("chn->flags = %s\n", (chn_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_CONN) printf("conn->flags = %s\n", (conn_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_SC) printf("sc->flags = %s\n", (sc_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_SD) printf("sd->flags = %s\n", (se_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_SET) printf("strm->et = %s\n", (strm_et_show_flags(buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_STRM) printf("strm->flags = %s\n", (strm_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_TASK) printf("task->state = %s\n", (task_show_state (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_TXN) printf("txn->flags = %s\n", (txn_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_HSL) printf("sl->flags = %s\n", (hsl_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_HTX) printf("htx->flags = %s\n", (htx_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_HMSG) printf("hmsg->flags = %s\n", (hmsg_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_FD) printf("fd->flags = %s\n", (fd_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_H2C) printf("h2c->flags = %s\n", (h2c_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_H2S) printf("h2s->flags = %s\n", (h2s_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_H1C) printf("h1c->flags = %s\n", (h1c_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_H1S) printf("h1s->flags = %s\n", (h1s_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_FCONN) printf("fconn->flags = %s\n",(fconn_show_flags (buf, bsz, " | ", flags), buf));
+ if (show_as & SHOW_AS_FSTRM) printf("fstrm->flags = %s\n",(fstrm_show_flags (buf, bsz, " | ", flags), buf));
+ }
+ return 0;
+}
diff --git a/dev/flags/show-fd-to-flags.sh b/dev/flags/show-fd-to-flags.sh
new file mode 100755
index 0000000..29757c3
--- /dev/null
+++ b/dev/flags/show-fd-to-flags.sh
@@ -0,0 +1,2 @@
+#!/bin/sh
+awk '{print $12}' | grep cflg= | sort | uniq -c | sort -nr | while read a b; do c=${b##*=}; d=$(${0%/*}/flags conn $c);d=${d##*= }; printf "%6d %s %s\n" $a "$b" "$d";done
diff --git a/dev/flags/show-sess-to-flags.sh b/dev/flags/show-sess-to-flags.sh
new file mode 100755
index 0000000..79003a4
--- /dev/null
+++ b/dev/flags/show-sess-to-flags.sh
@@ -0,0 +1,209 @@
+#!/usr/bin/env bash
+
+# This script is used to resolve various flags that appear on "show sess all".
+# All identified ones will be appended at the end, with a short name and their
+# value, followed by either the value resolved by "flags" when it's found, or
+# by the copy-pastable command to use to resolve them. The path to FLAGS is
+# searched in this order: 1) $FLAGS, 2) in the path, 3) dev/flags/flags, 4)
+# in the same directory as the script.
+#
+# This script is horrendous, but it's not a reason for making it even more
+# disgusting. The big regex flag mapping mess at the end is readable on a
+# large screen and it's easier to spot mistakes using this aligned format,
+# so please preserve this as much as possible and avoid multi-line formats.
+#
+# The append_* functions provide different variants that are still commented
+# out. It's mostly a matter of taste, they're equivalent.
+#
+# Usage: socat /path/to/socket - <<< "show sess all" | ./$0 > output
+#
+# options:
+# --color=never, --no-color: never colorize output
+# --color=always: always colorize output (default: only on terminal)
+
+# look for "flags in path then in dev/flags/flags then next to the script"
+FLAGS="${FLAGS:-$(command -v flags)}"
+if [ -z "$FLAGS" ]; then
+ if [ -e dev/flags/flags ]; then
+ FLAGS=dev/flags/flags;
+ elif [ -e "${0%/*}/flags" ]; then
+ FLAGS="${0%/*}/flags"
+ else
+ # OK still not found,let's write a copy-pastable command
+ FLAGS="echo ./flags"
+ fi
+fi
+
+HTTP_METH=( "OPTIONS" "GET" "HEAD" "POST" "PUT" "DELETE" "TRACE" "CONNECT" "OTHER" )
+out=( )
+decode=( )
+
+# returns str $2 and $3 concatenated with enough spaces in between so that the
+# total size doesn't exceed $1 chars, but always inserts at least one space.
+justify() {
+ local pad=" "
+ local str
+
+ while str="${2}${pad}${3}" && [ ${#str} -le $1 ]; do
+ pad="${pad} "
+ done
+ echo -n "$str"
+}
+
+# remove spaces at the beginning and end in "$1"
+trim() {
+ while [ -n "$1" -a -z "${1## *}" ]; do
+ set -- "${1# }"
+ done
+ while [ -n "$1" -a -z "${1%%* }" ]; do
+ set -- "${1% }"
+ done
+ echo -n "$1"
+}
+
+# pass $1=ctx name, $2=argname, $3=value, append the decoded line to decode[]
+append_flag() {
+ set -- "$1" "$2" "$(printf "%#x" $3)"
+ #decode[${#decode[@]}]="$1=$3 [ $(set -- $($FLAGS $2 $3 | cut -f2- -d=); echo $*) ]"
+ #decode[${#decode[@]}]="$(printf "%-14s %10s %s" $1 $3 "$(set -- $($FLAGS $2 $3 | cut -f2- -d=); echo $*)")"
+ #decode[${#decode[@]}]="$(justify 22 "$1" "$3") $(set -- $($FLAGS $2 $3 | cut -f2- -d=); echo $*)"
+ decode[${#decode[@]}]="$(justify 22 "$1" "$3") $(set -- $($FLAGS $2 $3 | cut -f2- -d= | tr -d '|'); echo "$*")"
+ #decode[${#decode[@]}]="$(justify 22 "$1" "$3") $(set -- $($FLAGS $2 $(printf "%#x" $3) | cut -f2- -d= | tr -d '|'); echo "$*")"
+ #decode[${#decode[@]}]="$(justify 22 "$1" "$3") $(trim "$($FLAGS $2 $3 | cut -f2- -d= | tr -d '|')")"
+ #decode[${#decode[@]}]="$(justify 22 "$1" "$3") $(trim "$($FLAGS $2 $3 | cut -f2- -d= | tr -d ' ')")"
+}
+
+# pass $1=ctx name, $2=value, $3=decoded value
+append_str() {
+ #decode[${#decode[@]}]="$1=$2 [ $3 ]"
+ #decode[${#decode[@]}]="$(printf "%-14s %10s %s" $1 $2 $3)"
+ decode[${#decode[@]}]="$(justify 22 "$1" "$2") $(trim $3)"
+}
+
+# dump and reset the buffers
+dump_and_reset() {
+ local line
+
+ line=0
+ while [ $line -lt ${#out[@]} ]; do
+ if [ -n "$COLOR" ]; then
+ # highlight name=value for values made of upper case letters
+ echo "${out[$line]}" | \
+ sed -e 's,\(^0x.*\),\x1b[1;37m\1\x1b[0m,g' \
+ -e 's,\([^ ,=]*\)=\([A-Z][^:, ]*\),\x1b[1;36m\1\x1b[0m=\x1b[1;33m\2\x1b[0m,g'
+
+ else
+ echo "${out[$line]}"
+ fi
+ ((line++))
+ done
+
+ [ ${#decode[@]} -eq 0 ] || echo " -----------------------------------"
+
+ line=0
+ while [ $line -lt ${#decode[@]} ]; do
+ echo " ${decode[$line]}"
+ ((line++, total++))
+ done
+
+ [ ${#decode[@]} -eq 0 ] || echo " -----------------------------------"
+
+ decode=( )
+ out=( )
+}
+
+### main entry point
+
+if [ -t 1 ]; then
+ # terminal on stdout, enable color by default
+ COLOR=1
+else
+ COLOR=
+fi
+
+if [ "$1" == "--no-color" -o "$1" == "--color=never" ]; then
+ shift
+ COLOR=
+elif [ "$1" == "--color=always" ]; then
+ shift
+ COLOR=1
+fi
+
+ctx=strm
+while read -r; do
+ [ "$REPLY" != "EOF" ] || break # for debugging
+
+ if [[ "$REPLY" =~ ^[[:blank:]]*task= ]]; then
+ ctx=task;
+ elif [[ "$REPLY" =~ ^[[:blank:]]*txn= ]]; then
+ ctx=txn;
+ elif [[ "$REPLY" =~ ^[[:blank:]]*scf= ]]; then
+ ctx=scf;
+ elif [[ "$REPLY" =~ ^[[:blank:]]*co0= ]]; then
+ ctx=cof;
+ elif [[ "$REPLY" =~ ^[[:blank:]]*app0= ]]; then
+ ctx=appf;
+ elif [[ "$REPLY" =~ ^[[:blank:]]*req= ]]; then
+ ctx=req;
+ elif [[ "$REPLY" =~ ^[[:blank:]]*scb= ]]; then
+ ctx=scb;
+ elif [[ "$REPLY" =~ ^[[:blank:]]*co1= ]]; then
+ ctx=cob;
+ elif [[ "$REPLY" =~ ^[[:blank:]]*app1= ]]; then
+ ctx=appb;
+ elif [[ "$REPLY" =~ ^[[:blank:]]*res= ]]; then
+ ctx=res;
+ elif [[ "$REPLY" =~ ^0x ]]; then
+ # here we dump what we have and we reset
+ dump_and_reset
+ ctx=strm;
+ fi
+
+ if [ $ctx = strm ]; then
+ ! [[ "$REPLY" =~ [[:blank:]]flags=([0-9a-fx]*) ]] || append_flag strm.flg strm "${BASH_REMATCH[1]}"
+ elif [ $ctx = task ]; then
+ ! [[ "$REPLY" =~ \(state=([0-9a-fx]*) ]] || append_flag task.state task "${BASH_REMATCH[1]}"
+ elif [ $ctx = txn ]; then
+ ! [[ "$REPLY" =~ [[:blank:]]meth=([^[:blank:]]*) ]] || append_str txn.meth "${BASH_REMATCH[1]}" "${HTTP_METH[$((${BASH_REMATCH[1]}))]}"
+ ! [[ "$REPLY" =~ [[:blank:]]flags=([0-9a-fx]*) ]] || append_flag txn.flg txn "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]req\.f=([0-9a-fx]*) ]] || append_flag txn.req.flg hmsg "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]rsp\.f=([0-9a-fx]*) ]] || append_flag txn.rsp.flg hmsg "${BASH_REMATCH[1]}"
+ elif [ $ctx = scf ]; then
+ ! [[ "$REPLY" =~ [[:blank:]]flags=([0-9a-fx]*) ]] || append_flag f.sc.flg sc "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]endp=[[:alnum:]]*,[[:alnum:]]*,([0-9a-fx]*) ]] || append_flag f.sc.sd.flg sd "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]h1s.*\.sd\.flg=([0-9a-fx]*) ]] || append_flag f.h1s.sd.flg sd "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]h1s\.flg=([0-9a-fx]*) ]] || append_flag f.h1s.flg h1s "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]h1c\.flg=([0-9a-fx]*) ]] || append_flag f.h1c.flg h1c "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ ^[[:blank:]]*\.sc=.*\.flg=.*\.app=.*\.sd=[^=]*\.flg=([0-9a-fx]*) ]] || append_flag f.h2s.sd.flg sd "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]h2s.*\.flg=([0-9a-fx]*) ]] || append_flag f.h2s.flg h2s "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]h2c.*\.flg=([0-9a-fx]*) ]] || append_flag f.h2c.flg h2c "${BASH_REMATCH[1]}"
+ elif [ $ctx = cof ]; then
+ ! [[ "$REPLY" =~ [[:blank:]]flags=([0-9a-fx]*) ]] || append_flag f.co.flg conn "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]fd.state=([0-9a-fx]*) ]] || append_flag f.co.fd.st fd 0x"${BASH_REMATCH[1]#0x}"
+ elif [ $ctx = req ]; then
+ ! [[ "$REPLY" =~ [[:blank:]]\(f=([0-9a-fx]*) ]] || append_flag req.flg chn "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]an=([0-9a-fx]*) ]] || append_flag req.ana ana "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]htx.*flags=([0-9a-fx]*) ]] || append_flag req.htx.flg htx "${BASH_REMATCH[1]}"
+ elif [ $ctx = scb ]; then
+ ! [[ "$REPLY" =~ [[:blank:]]flags=([0-9a-fx]*) ]] || append_flag b.sc.flg sc "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]endp=[[:alnum:]]*,[[:alnum:]]*,([0-9a-fx]*) ]] || append_flag b.sc.sd.flg sd "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]h1s.*\.sd\.flg=([0-9a-fx]*) ]] || append_flag b.h1s.sd.flg sd "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]h1s\.flg=([0-9a-fx]*) ]] || append_flag b.h1s.flg h1s "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]h1c\.flg=([0-9a-fx]*) ]] || append_flag b.h1c.flg h1c "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ ^[[:blank:]]*\.sc=.*\.flg=.*\.app=.*\.sd=[^=]*\.flg=([0-9a-fx]*) ]] || append_flag b.h2s.sd.flg sd "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]h2s.*\.flg=([0-9a-fx]*) ]] || append_flag b.h2s.flg h2s "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]h2c.*\.flg=([0-9a-fx]*) ]] || append_flag b.h2c.flg h2c "${BASH_REMATCH[1]}"
+ elif [ $ctx = cob ]; then
+ ! [[ "$REPLY" =~ [[:blank:]]flags=([0-9a-fx]*) ]] || append_flag b.co.flg conn "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]fd.state=([0-9a-fx]*) ]] || append_flag b.co.fd.st fd "${BASH_REMATCH[1]}"
+ elif [ $ctx = res ]; then
+ ! [[ "$REPLY" =~ [[:blank:]]\(f=([0-9a-fx]*) ]] || append_flag res.flg chn "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]an=([0-9a-fx]*) ]] || append_flag res.ana ana "${BASH_REMATCH[1]}"
+ ! [[ "$REPLY" =~ [[:blank:]]htx.*flags=([0-9a-fx]*) ]] || append_flag res.htx.flg htx "${BASH_REMATCH[1]}"
+ fi
+
+ out[${#out[@]}]="$REPLY"
+done
+
+# dump the last stream
+dump_and_reset