public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH v2 05/10] flow: Introduce struct flowside, space for uniform tracking of addresses
Date: Mon, 28 Aug 2023 15:41:41 +1000	[thread overview]
Message-ID: <20230828054146.48673-6-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20230828054146.48673-1-david@gibson.dropbear.id.au>

Handling of each protocol needs some degree of tracking of the addresses
and ports at the end of each connection or flow.  Sometimes that's explicit
(as in the guest visible addresses for TCP connections), sometimes implicit
(the bound and connected addresses of sockets).

To allow more general abd robust handling, and more consistency across
protocols we want to uniformly track the address and port at each end of
the connection.  Furthermore, because we allow port remapping, and we
sometimes need to apply NAT, the addresses and ports can be different as
seen by the guest/namespace and as by the host.

Introduce 'struct flowside' to keep track of the address and ports of a
flow from a single "side" (guest or host).  Store two of these in the
common fields of a flow to track that information for both sides.

For now we just introduce the structure and fields themselves, along with
some simple helpers.  Later patches will actually use these to store useful
information.

Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
 flow.c | 22 ++++++++++++++++++++++
 flow.h | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 70 insertions(+)

diff --git a/flow.c b/flow.c
index 12ca8db..a93cf8c 100644
--- a/flow.c
+++ b/flow.c
@@ -7,6 +7,7 @@
 
 #include <unistd.h>
 #include <string.h>
+#include <arpa/inet.h>
 
 #include "util.h"
 #include "passt.h"
@@ -24,6 +25,27 @@ const char *flow_type_str[] = {
 /* Global Flow Table */
 union flow flowtab[FLOW_MAX];
 
+/** flowside_fmt - Format a flowside as a string
+ * @fs:		flowside to format
+ * @buf:	Buffer into which to store the formatted version
+ * @size:	Size of @buf
+ *
+ * Return: pointer to formatted string describing @fs, or NULL on error
+ */
+/* cppcheck-suppress unusedFunction */
+const char *flowside_fmt(const struct flowside *fs, char *buf, size_t size)
+{
+	char ebuf[INET6_ADDRSTRLEN], fbuf[INET6_ADDRSTRLEN];
+
+	if (!inet_ntop(AF_INET6, &fs->eaddr, ebuf, sizeof(ebuf))
+	    || !inet_ntop(AF_INET6, &fs->faddr, fbuf, sizeof(fbuf)))
+		return NULL;
+
+	snprintf(buf, size, "[%s]:%hu <-> [%s]:%hu", fbuf, fs->fport,
+		 ebuf, fs->eport);
+	return (const char *)buf;
+}
+
 /**
  * flow_table_compact() - Perform compaction on flow table
  * @c:		Execution context
diff --git a/flow.h b/flow.h
index e212796..9891fcb 100644
--- a/flow.h
+++ b/flow.h
@@ -18,11 +18,59 @@ extern const char *flow_type_str[];
 #define FLOW_TYPE(f)							\
         ((f)->type <= FLOW_MAX ? flow_type_str[(f)->type] : "?")
 
+/**
+ * struct flowside - Describes a logical packet flow as seen from one "side"
+ * @eaddr:	Endpoint address (remote address from passt's PoV)
+ * @faddr:	Forwarding address (local address from passt's PoV)
+ * @eport:	Endpoint port
+ * @fport:	Forwarding port
+ */
+struct flowside {
+	union inany_addr faddr;
+	union inany_addr eaddr;
+	in_port_t fport, eport;
+};
+
+/** flowside_from_af - Initialize a flowside from addresses
+ * @fs:		flowside to initialize
+ * @af:		Address family (AF_INET or AF_INET6)
+ * @faddr:	Forwarding address (pointer to in_addr or in6_addr)
+ * @fport:	Forwarding port
+ * @eaddr:	Endpoint address (pointer to in_addr or in6_addr)
+ * @eport:	Endpoint port
+ */
+static inline void flowside_from_af(struct flowside *fs, int af,
+				    const void *faddr, in_port_t fport,
+				    const void *eaddr, in_port_t eport)
+{
+	inany_from_af(&fs->faddr, af, faddr);
+	inany_from_af(&fs->eaddr, af, eaddr);
+	fs->fport = fport;
+	fs->eport = eport;
+}
+
+/** flowside_complete - Check if flowside is fully initialized
+ * @fs:		flowside to check
+ */
+static inline bool flowside_complete(const struct flowside *fs)
+{
+	return !IN6_IS_ADDR_UNSPECIFIED(&fs->faddr) &&
+		!IN6_IS_ADDR_UNSPECIFIED(&fs->eaddr) &&
+		fs->fport != 0 && fs->eport != 0;
+}
+
+#define FLOWSIDE_STRLEN		(2*(INET6_ADDRSTRLEN+8) + 6)
+
+const char *flowside_fmt(const struct flowside *fs, char *buf, size_t size);
+
 /**
  * struct flow_common - Common fields for packet flows
+ * @side[]:	Information on the flow for each side.  Flow types can have
+ *		their own conventions about which side is which
  * @type:	Type of packet flow
  */
 struct flow_common {
+	struct flowside		side[2];
 	enum flow_type		type;
 };
 
-- 
@@ -18,11 +18,59 @@ extern const char *flow_type_str[];
 #define FLOW_TYPE(f)							\
         ((f)->type <= FLOW_MAX ? flow_type_str[(f)->type] : "?")
 
+/**
+ * struct flowside - Describes a logical packet flow as seen from one "side"
+ * @eaddr:	Endpoint address (remote address from passt's PoV)
+ * @faddr:	Forwarding address (local address from passt's PoV)
+ * @eport:	Endpoint port
+ * @fport:	Forwarding port
+ */
+struct flowside {
+	union inany_addr faddr;
+	union inany_addr eaddr;
+	in_port_t fport, eport;
+};
+
+/** flowside_from_af - Initialize a flowside from addresses
+ * @fs:		flowside to initialize
+ * @af:		Address family (AF_INET or AF_INET6)
+ * @faddr:	Forwarding address (pointer to in_addr or in6_addr)
+ * @fport:	Forwarding port
+ * @eaddr:	Endpoint address (pointer to in_addr or in6_addr)
+ * @eport:	Endpoint port
+ */
+static inline void flowside_from_af(struct flowside *fs, int af,
+				    const void *faddr, in_port_t fport,
+				    const void *eaddr, in_port_t eport)
+{
+	inany_from_af(&fs->faddr, af, faddr);
+	inany_from_af(&fs->eaddr, af, eaddr);
+	fs->fport = fport;
+	fs->eport = eport;
+}
+
+/** flowside_complete - Check if flowside is fully initialized
+ * @fs:		flowside to check
+ */
+static inline bool flowside_complete(const struct flowside *fs)
+{
+	return !IN6_IS_ADDR_UNSPECIFIED(&fs->faddr) &&
+		!IN6_IS_ADDR_UNSPECIFIED(&fs->eaddr) &&
+		fs->fport != 0 && fs->eport != 0;
+}
+
+#define FLOWSIDE_STRLEN		(2*(INET6_ADDRSTRLEN+8) + 6)
+
+const char *flowside_fmt(const struct flowside *fs, char *buf, size_t size);
+
 /**
  * struct flow_common - Common fields for packet flows
+ * @side[]:	Information on the flow for each side.  Flow types can have
+ *		their own conventions about which side is which
  * @type:	Type of packet flow
  */
 struct flow_common {
+	struct flowside		side[2];
 	enum flow_type		type;
 };
 
-- 
2.41.0


  parent reply	other threads:[~2023-08-28  5:42 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-28  5:41 [PATCH v2 00/10] RFC: Convert TCP connection table to generalisable flow table David Gibson
2023-08-28  5:41 ` [PATCH v2 01/10] flow, tcp: Generalise connection types David Gibson
2023-08-28  5:41 ` [PATCH v2 02/10] flow, tcp: Move TCP connection table to unified flow table David Gibson
2023-08-28  5:41 ` [PATCH v2 03/10] flow, tcp: Consolidate flow pointer<->index helpers David Gibson
2023-09-07  1:01   ` Stefano Brivio
2023-09-07  3:48     ` David Gibson
2023-08-28  5:41 ` [PATCH v2 04/10] flow: Make unified version of flow table compaction David Gibson
2023-08-28  5:41 ` David Gibson [this message]
2023-09-07  1:01   ` [PATCH v2 05/10] flow: Introduce struct flowside, space for uniform tracking of addresses Stefano Brivio
2023-09-07  4:05     ` David Gibson
2023-09-07  7:55       ` Stefano Brivio
2023-08-28  5:41 ` [PATCH v2 06/10] tcp: Move guest side address tracking to flow/flowside David Gibson
2023-08-28  5:41 ` [PATCH v2 07/10] tcp, flow: Perform TCP hash calculations based on flowside David Gibson
2023-08-28  5:41 ` [PATCH v2 08/10] tcp: Re-use flowside_hash for initial sequence number generation David Gibson
2023-08-28  5:41 ` [PATCH v2 09/10] tcp: Maintain host flowside for connections David Gibson
2023-08-28  5:41 ` [PATCH v2 10/10] tcp_splice: Fill out flowside information for spliced connections David Gibson
2023-09-07  1:02   ` Stefano Brivio
2023-09-07  4:14     ` David Gibson
2023-09-07  7:55       ` Stefano Brivio

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230828054146.48673-6-david@gibson.dropbear.id.au \
    --to=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    --cc=sbrivio@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).