From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by passt.top (Postfix, from userid 1000) id EDDC85A061C; Wed, 29 Jan 2025 00:39:40 +0100 (CET) From: Stefano Brivio To: passt-dev@passt.top Subject: [PATCH v2 8/8] flow, tcp: Basic pre-migration source handler to dump sequence numbers Date: Wed, 29 Jan 2025 00:39:40 +0100 Message-ID: <20250128233940.1235855-9-sbrivio@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20250128233940.1235855-1-sbrivio@redhat.com> References: <20250128233940.1235855-1-sbrivio@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: S7MXLNQJHPGPH2HXJP2N7FHBZQCVEOMX X-Message-ID-Hash: S7MXLNQJHPGPH2HXJP2N7FHBZQCVEOMX X-MailFrom: sbrivio@passt.top X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Laurent Vivier , David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Very much draft quality, but it works. Ask passt-repair to switch TCP sockets to repair mode and dump their current sequence numbers to the flow table, which will be transferred and used by the target in the next step. Signed-off-by: Stefano Brivio --- flow.c | 43 +++++++++++++++++++++++++++++++++++++++++ flow.h | 1 + migrate.c | 1 + tcp.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tcp_conn.h | 5 +++++ 5 files changed, 106 insertions(+) diff --git a/flow.c b/flow.c index ee1221b..e7148b2 100644 --- a/flow.c +++ b/flow.c @@ -19,6 +19,7 @@ #include "inany.h" #include "flow.h" #include "flow_table.h" +#include "repair.h" const char *flow_state_str[] = { [FLOW_STATE_FREE] = "FREE", @@ -874,6 +875,48 @@ void flow_defer_handler(const struct ctx *c, const struct timespec *now) *last_next = FLOW_MAX; } +/** + * flow_migrate_source_pre() - Prepare all source flows for migration + * @c: Execution context + * @m: Migration metadata + * + * Return: 0 on success + */ +int flow_migrate_source_pre(struct ctx *c, struct migrate_meta *m) +{ + unsigned i; + int rc; + + (void)m; + + for (i = 0; i < FLOW_MAX; i++) { /* TODO: iterator with skip */ + union flow *flow = &flowtab[i]; + + if (flow->f.state == FLOW_STATE_FREE) + i += flow->free.n - 1; + else if (flow->f.state == FLOW_STATE_ACTIVE && + flow->f.type == FLOW_TCP) + rc = tcp_flow_repair_on(c, &flow->tcp); + + if (rc) + return rc; /* TODO: rollback */ + } + + repair_flush(c); /* TODO: move to TCP logic */ + + for (i = 0; i < FLOW_MAX; i++) { /* TODO: iterator with skip */ + union flow *flow = &flowtab[i]; + + if (flow->f.state == FLOW_STATE_FREE) + i += flow->free.n - 1; + else if (flow->f.state == FLOW_STATE_ACTIVE && + flow->f.type == FLOW_TCP) + tcp_flow_dump_seq(c, &flow->tcp); + } + + return 0; +} + /** * flow_init() - Initialise flow related data structures */ diff --git a/flow.h b/flow.h index 8eb5964..ff390a6 100644 --- a/flow.h +++ b/flow.h @@ -255,6 +255,7 @@ union flow; void flow_init(void); void flow_defer_handler(const struct ctx *c, const struct timespec *now); +int flow_migrate_source_pre(struct ctx *c, struct migrate_meta *m); void flow_log_(const struct flow_common *f, int pri, const char *fmt, ...) __attribute__((format(printf, 3, 4))); diff --git a/migrate.c b/migrate.c index b8b79e0..6707c02 100644 --- a/migrate.c +++ b/migrate.c @@ -56,6 +56,7 @@ static struct migrate_data data_versions[] = { /* Handlers to call in source before sending data */ struct migrate_handler handlers_source_pre[] = { + { flow_migrate_source_pre }, { 0 }, }; diff --git a/tcp.c b/tcp.c index c89f323..3a3038b 100644 --- a/tcp.c +++ b/tcp.c @@ -299,6 +299,7 @@ #include "log.h" #include "inany.h" #include "flow.h" +#include "repair.h" #include "linux_dep.h" #include "flow_table.h" @@ -868,6 +869,61 @@ void tcp_defer_handler(struct ctx *c) tcp_payload_flush(c); } +/** + * tcp_flow_repair_on() - Enable repair mode for a single TCP flow + * @c: Execution context + * @conn: Pointer to the TCP connection structure + * + * Return: 0 on success, negative error code on failure + */ +int tcp_flow_repair_on(struct ctx *c, const struct tcp_tap_conn *conn) +{ + int rc = 0; + + if ((rc = repair_set(c, conn->sock, TCP_REPAIR_ON))) + err("Failed to set TCP_REPAIR for socket %i", conn->sock); + + return rc; +} + +/** + * tcp_flow_dump_seq() - Dump sequences for send and receive queues + * @c: Execution context + * @conn: Pointer to the TCP connection structure + * + * Return: 0 on success, negative error code on failure + */ +int tcp_flow_dump_seq(struct ctx *c, struct tcp_tap_conn *conn) +{ + int v, s = conn->sock; + socklen_t vlen; + + (void)c; + + vlen = sizeof(v); + + v = TCP_SEND_QUEUE; + /* TODO: proper error management and prints */ + if (setsockopt(s, SOL_TCP, TCP_REPAIR_QUEUE, &v, vlen)) + return -errno; + + if (getsockopt(s, SOL_TCP, TCP_QUEUE_SEQ, &conn->sock_seq_snd, &vlen)) + return -errno; + + debug("Send queue sequence %u for socket %i", conn->sock_seq_snd, s); + + v = TCP_RECV_QUEUE; + if (setsockopt(s, SOL_TCP, TCP_REPAIR_QUEUE, &v, vlen)) + return -errno; + + if (getsockopt(s, SOL_TCP, TCP_QUEUE_SEQ, &conn->sock_seq_rcv, &vlen)) + return -errno; + + debug("Receive queue sequence %u for socket %i", conn->sock_seq_rcv, s); + + return 0; +} + /** * tcp_fill_header() - Fill the TCP header fields for a given TCP segment. * diff --git a/tcp_conn.h b/tcp_conn.h index d342680..0c3e197 100644 --- a/tcp_conn.h +++ b/tcp_conn.h @@ -94,6 +94,9 @@ struct tcp_tap_conn { uint32_t seq_from_tap; uint32_t seq_ack_to_tap; uint32_t seq_init_from_tap; + + uint32_t sock_seq_snd; + uint32_t sock_seq_rcv; }; /** @@ -140,6 +143,8 @@ extern int init_sock_pool4 [TCP_SOCK_POOL_SIZE]; extern int init_sock_pool6 [TCP_SOCK_POOL_SIZE]; bool tcp_flow_defer(const struct tcp_tap_conn *conn); +int tcp_flow_repair_on(struct ctx *c, const struct tcp_tap_conn *conn); +int tcp_flow_dump_seq(struct ctx *c, struct tcp_tap_conn *conn); bool tcp_splice_flow_defer(struct tcp_splice_conn *conn); void tcp_splice_timer(const struct ctx *c, struct tcp_splice_conn *conn); int tcp_conn_pool_sock(int pool[]); -- 2.43.0