]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: connection: add helper conn_append_debug_info()
authorWilly Tarreau <w@1wt.eu>
Wed, 16 Jun 2021 15:35:20 +0000 (17:35 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 16 Jun 2021 16:30:42 +0000 (18:30 +0200)
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.

include/haproxy/connection.h
src/connection.c

index aeef840a6ece4a0373d7cb22097f5bf937cb95d6..d4843462f80e0a4d1f019a7eae06c43b1ad73f20 100644 (file)
@@ -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);
 
index 73a326dd570e45e02dc01d2573b4c30012e0cce5..0ba29ed662f16edf6f8bfe7638998bda3d318dbe 100644 (file)
@@ -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 <buf>. 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 <pfx> 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.
  *