From: Willy Tarreau Date: Wed, 16 Jun 2021 15:35:20 +0000 (+0200) Subject: MINOR: connection: add helper conn_append_debug_info() X-Git-Tag: v2.5-dev1~107 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d943a044aa1ff4808b7b06cbd7a4c2fef659035c;p=thirdparty%2Fhaproxy.git MINOR: connection: add helper conn_append_debug_info() This function appends to a buffer some information from a connection. This will be used by traces and possibly some debugging as well. A frontend/backend/server, transport/control layers, source/destination ip:port, connection pointer and direction are reported depending on the available information. --- diff --git a/include/haproxy/connection.h b/include/haproxy/connection.h index aeef840a6e..d4843462f8 100644 --- a/include/haproxy/connection.h +++ b/include/haproxy/connection.h @@ -55,6 +55,8 @@ int make_proxy_line(char *buf, int buf_len, struct server *srv, struct connectio int make_proxy_line_v1(char *buf, int buf_len, struct sockaddr_storage *src, struct sockaddr_storage *dst); int make_proxy_line_v2(char *buf, int buf_len, struct server *srv, struct connection *remote, struct stream *strm); +int conn_append_debug_info(struct buffer *buf, const struct connection *conn, const char *pfx); + int conn_subscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es); int conn_unsubscribe(struct connection *conn, void *xprt_ctx, int event_type, struct wait_event *es); diff --git a/src/connection.c b/src/connection.c index 73a326dd57..0ba29ed662 100644 --- a/src/connection.c +++ b/src/connection.c @@ -1357,6 +1357,45 @@ static int cfg_parse_pp2_never_send_local(char **args, int section_type, struct return 0; } +/* extracts some info from the connection and appends them to buffer . The + * connection's pointer, its direction, target (fe/be/srv), xprt/ctrl, source + * when set, destination when set, are printed in a compact human-readable format + * fitting on a single line. This is handy to complete traces or debug output. + * It is permitted to pass a NULL conn pointer. The number of characters emitted + * is returned. A prefix might be prepended before the first field if not + * NULL. + */ +int conn_append_debug_info(struct buffer *buf, const struct connection *conn, const char *pfx) +{ + const struct listener *li; + const struct server *sv; + const struct proxy *px; + char addr[40]; + int old_len = buf->data; + + if (!conn) + return 0; + + chunk_appendf(buf, "%sconn=%p(%s)", pfx ? pfx : "", conn, conn_is_back(conn) ? "OUT" : "IN"); + + if ((li = objt_listener(conn->target))) + chunk_appendf(buf, " fe=%s", li->bind_conf->frontend->id); + else if ((sv = objt_server(conn->target))) + chunk_appendf(buf, " sv=%s/%s", sv->proxy->id, sv->id); + else if ((px = objt_proxy(conn->target))) + chunk_appendf(buf, " be=%s", px->id); + + chunk_appendf(buf, " %s/%s", conn_get_xprt_name(conn), conn_get_ctrl_name(conn)); + + if (conn->flags & CO_FL_ADDR_FROM_SET && addr_to_str(conn->src, addr, sizeof(addr))) + chunk_appendf(buf, " src=%s:%d", addr, get_host_port(conn->src)); + + if (conn->flags & CO_FL_ADDR_TO_SET && addr_to_str(conn->dst, addr, sizeof(addr))) + chunk_appendf(buf, " dst=%s:%d", addr, get_host_port(conn->dst)); + + return buf->data - old_len; +} + /* return the major HTTP version as 1 or 2 depending on how the request arrived * before being processed. *