From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qt1-f172.google.com (mail-qt1-f172.google.com [209.85.160.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C880B2E62B4 for ; Wed, 25 Mar 2026 03:49:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.160.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774410591; cv=none; b=dLnXCs9P2NVEuHVf/DtFZhLaov88moErNkGLn8A4M7Kjdu86dUu4q6McfIahwn4O0HiTeZqk1OgOT9W36tyRSgvF7W+npW3VyNDp85tJ0kPWdvn3jjneSaKQNvZKZ8YhZ0A5nk4baYYuv7YfCl5hzWrUo3AItXxkwWkreM+jWVw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1774410591; c=relaxed/simple; bh=ngFPwXkkurbSJi167Sr4VLCpev1Q2dqSQoazmHQB9Qs=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=feKQQsjPfcsDLJ68/oLmkNy5EjpxJPFWoevjXuivON2SwvVerKS8j4VzN2M3diw9srUFlG7xtOjouNypfBdyVWuqeITtcV8HhkOD7NAQturvTv0vzZ0scW8da+l4ZxP5qsDMEU3K0ljxmCcjlpDTqRUnJpneDOrlpK/I6mtgMCQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=HTEGNMbE; arc=none smtp.client-ip=209.85.160.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HTEGNMbE" Received: by mail-qt1-f172.google.com with SMTP id d75a77b69052e-5094e1d17d3so49598111cf.3 for ; Tue, 24 Mar 2026 20:49:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774410588; x=1775015388; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=sWXysmmFoumyN1OfpS1Dlk5sIIO7cNibhRi90Ty/B1E=; b=HTEGNMbEYloU7RpsGvhMpbKYgonoC+4/4iwz/EvAZOK0Q9xw/b3RRbCiAbWDEqDr4T Zyl+yhcmVdD2Fe6qJT2dxwAHaMEZjW3fCMEDDyrleYKBI1beDTxQzPyOQrpw73uWEANI OYlEEPeSjuZd/lb5qleE9E9UwyxzcrIEbwZnr88I1yp8/Sd1R5erT1SW2Gad+bQiv88q JOySD3wwalrlitRFVX7QUlzwUJxFvDhjVtkYq6NOecyNcSBPAQYqXyaasWQLE1X0YQFn cEmyBpbJ8Ekq3/BuNmDtfrrBQS5ASpvln3fwh33tOvPJu0OwYHAKcdmwwCwQkPBC45qU +MqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774410588; x=1775015388; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=sWXysmmFoumyN1OfpS1Dlk5sIIO7cNibhRi90Ty/B1E=; b=EUVTExgdeJbOepMrHTP6vkRYCOvk6+5BepjBLhbTl1Ta5OKVsVDKmBlno+DDth9HJQ XchUjdrOJhMVaQlFuHkhamjznHMh0VzRMJ2pr/5TqNuDkuJHYKrvGMb8+b/L1IpCQCKZ /jRu2g2kZeLJI939cKQFWufnI2ukBb+mMb4KScb9YgfcoZgaUsN+3qcdwXaKJ9QGhKcq fFEMcqH1kYXzg3jotxIZUVh8gL+e5QNe/Asg06/1TgUdYjHLbJgvZ39isxRdl9I5i1yh saeORt0ISwE44FiyfPLG+CQqKoSoMmlaCNjMPvxjPQfZZYMvh2aSxUfOE3s9M9Axd1gz kPhA== X-Gm-Message-State: AOJu0Yzb4aeEgAAijGdWrzrkpxVTtJKnSYt2FQgu5Jrgih9oZnPjCYmh 8+HriJaf2pKcXrrVvm87ual9KKAT/S9/p8umEX/QidoXIFKYpFDlTo5tdH5nRRP9Va0= X-Gm-Gg: ATEYQzxd0Rbjns0G0CndPTJQ9uexhPBpP7nTDGc0V7jZoZF+h71TIgkJlS4Vb2hgowc 5pGQLyB5W6d0y1jZa+W5WECu539q/bn9M9Ydt/sfJLQh37F990i2o3lXKRzxkH1tEi1yw0MfUM5 y+XnP0pBm0QvEiaQSeXTthxsT0ulZEzxHPcLj8R574YniU/e5CLjQkOuSH37J2+9tkFIdTqbSny kd5tbTNFHlMniHsuHUzEvZLtN8vm8pfNaicAxgfKqtxl5iYsnbTJWuoguyY/4+AOu0hmccchEhy wQDMH+DuXDa0xzIifPFat4+fQouoOVzJ74TflGtdQpBSLLHSUQTWdPxObH4EV2ZUiiP5UJAQURv 4IEHErdKmqNhlVRtqzbWehGcf7tEbB+8y6D0DN7rRBwnOkq3dEtIcPVZnIP3It995JiuKl6S0Jj eCybBdbeSkZHxbSKhRO+3ylA+1WLnUpPUUedIbm1e4HSOiOV29sufyp+mDVxC1agLbbWlH32VyA 8c8v65gkzQIc/XXidY5tR2AbYJkm7sZa+dvb6f+4Kr48mcqcICGAokwzxMvDWSEUg== X-Received: by 2002:a05:622a:22a9:b0:50b:4b2f:1606 with SMTP id d75a77b69052e-50b80c9ad0amr29393041cf.15.1774410588361; Tue, 24 Mar 2026 20:49:48 -0700 (PDT) Received: from wsfd-netdev58.anl.eng.rdu2.dc.redhat.com ([66.187.232.140]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-50b36cb2e29sm150093001cf.1.2026.03.24.20.49.45 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Mar 2026 20:49:47 -0700 (PDT) From: Xin Long To: network dev , quic@lists.linux.dev Cc: davem@davemloft.net, kuba@kernel.org, Eric Dumazet , Paolo Abeni , Simon Horman , Stefan Metzmacher , Moritz Buhl , Tyler Fanelli , Pengtao He , Thomas Dreibholz , linux-cifs@vger.kernel.org, Steve French , Namjae Jeon , Paulo Alcantara , Tom Talpey , kernel-tls-handshake@lists.linux.dev, Chuck Lever , Jeff Layton , Steve Dickson , Hannes Reinecke , Alexander Aring , David Howells , Matthieu Baerts , John Ericson , Cong Wang , "D . Wythe" , Jason Baron , illiliti , Sabrina Dubroca , Marcelo Ricardo Leitner , Daniel Stenberg , Andy Gospodarek , "Marc E . Fiuczynski" Subject: [PATCH net-next v11 10/15] quic: add packet number space Date: Tue, 24 Mar 2026 23:47:15 -0400 Message-ID: <72c4d87ed7fce4ed7e06d8f76a3ceaf7869e91e3.1774410440.git.lucien.xin@gmail.com> X-Mailer: git-send-email 2.47.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: netdev@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit This patch introduces 'quic_pnspace', which manages per packet number space members. It maintains the next packet number to assign, tracks the total length of frames currently in flight, and records the time when the next packet may be considered lost. It also keeps track of the largest acknowledged packet number, the time it was acknowledged, and when the most recent ack eliciting packet was sent. These fields are useful for loss detection, RTT estimation, and congestion control. To support ACK frame generation, quic_pnspace includes a packet number acknowledgment map (pn_ack_map) that tracks received packet numbers. Supporting functions are provided to validate and mark received packet numbers and compute the number of gap blocks needed during ACK frame construction. - quic_pnspace_check(): Validates a received packet number. - quic_pnspace_mark(): Marks a received packet number in the ACK map. - quic_pnspace_num_gabs(): Returns the gap ACK blocks for constructing ACK frames. Note QUIC uses separate packet number spaces for each encryption level (APP, INITIAL, HANDSHAKE, EARLY) except EARLY and all generations of APP keys use the same packet number space, as describe in rfc9002#section-4.1. Signed-off-by: Xin Long Acked-by: Paolo Abeni --- v5: - Change timestamp variables from u32 to u64 and use quic_ktime_get_us() to set max_pn_acked_time, as jiffies_to_usecs() is not accurate enough. - Reorder some members in quic_pnspace to reduce 32-bit holes (noted by Paolo). v6: - Note for AI reviews: it's safe to do cast (u16)(pn - space->base_pn) in quic_pnspace_mark(), as the pn < base_pn + QUIC_PN_MAP_SIZE (4096) validation is always done in quic_pnspace_check(), which will always be called before quic_pnspace_mark() in a later patchset. - Note for AI reviews: failures in quic_pnspace_init() do not result in a pn_map leak in quic_init_sock(), because quic_destroy_sock() is always called to free it in err path, either via inet/6_create() or through quic_accept() in a later patchset. v8: - Replace bitfields with plain u8 in struct quic_pnspace. v10: - Fix a grammar error in the comment of quic_pnspace_check(). v11: - Note for AI reviews: RFC 9000 does not define integer IDs for packet number spaces. In this implementation, App=0, Initial=1, Handshake=2, and Early maps to 0 (3 % 3). - Set maximum line length to 80 characters. - clear space->pn_map pointer after free in quic_pnspace_free(). - Change quic_pnspace_grow() to return negative errno on failure or 0 on success. - Change return type of quic_pnspace_next_gap_ack() and quic_pnspace_set_ecn_count() to bool. - Return -EINVAL instead of -1 on failure in quic_pnspace_check(). --- net/quic/Makefile | 2 +- net/quic/pnspace.c | 250 +++++++++++++++++++++++++++++++++++++++++++++ net/quic/pnspace.h | 157 ++++++++++++++++++++++++++++ net/quic/socket.c | 12 +++ net/quic/socket.h | 7 ++ 5 files changed, 427 insertions(+), 1 deletion(-) create mode 100644 net/quic/pnspace.c create mode 100644 net/quic/pnspace.h diff --git a/net/quic/Makefile b/net/quic/Makefile index 4d4a42c6d565..9d8e18297911 100644 --- a/net/quic/Makefile +++ b/net/quic/Makefile @@ -6,4 +6,4 @@ obj-$(CONFIG_IP_QUIC) += quic.o quic-y := common.o family.o protocol.o socket.o stream.o connid.o path.o \ - cong.o + cong.o pnspace.o diff --git a/net/quic/pnspace.c b/net/quic/pnspace.c new file mode 100644 index 000000000000..d8f19f4e0fc6 --- /dev/null +++ b/net/quic/pnspace.c @@ -0,0 +1,250 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* QUIC kernel implementation + * (C) Copyright Red Hat Corp. 2023 + * + * This file is part of the QUIC kernel implementation + * + * Initialization/cleanup for QUIC protocol support. + * + * Written or modified by: + * Xin Long + */ + +#include + +#include "common.h" +#include "pnspace.h" + +int quic_pnspace_init(struct quic_pnspace *space) +{ + if (!space->pn_map) { + space->pn_map = kzalloc(BITS_TO_BYTES(QUIC_PN_MAP_INITIAL), + GFP_KERNEL); + if (!space->pn_map) + return -ENOMEM; + space->pn_map_len = QUIC_PN_MAP_INITIAL; + } else { + bitmap_zero(space->pn_map, space->pn_map_len); + } + + space->max_time_limit = QUIC_PNSPACE_TIME_LIMIT; + space->next_pn = QUIC_PNSPACE_NEXT_PN; + space->base_pn = -1; + return 0; +} + +void quic_pnspace_free(struct quic_pnspace *space) +{ + space->pn_map_len = 0; + kfree(space->pn_map); + space->pn_map = NULL; +} + +/* Expand the bitmap tracking received packet numbers. Ensures the pn_map + * bitmap can cover at least @size packet numbers. Allocates a larger bitmap, + * copies existing data, and updates metadata. + * + * Return: 0 on success, or a negative errno value on failure. + */ +static int quic_pnspace_grow(struct quic_pnspace *space, u16 size) +{ + u16 len, inc, offset; + unsigned long *new; + + if (size > QUIC_PN_MAP_SIZE) + return -EINVAL; + + inc = ALIGN((size - space->pn_map_len), BITS_PER_LONG) + + QUIC_PN_MAP_INCREMENT; + len = (u16)min(space->pn_map_len + inc, QUIC_PN_MAP_SIZE); + + new = kzalloc(BITS_TO_BYTES(len), GFP_ATOMIC); + if (!new) + return -ENOMEM; + + offset = (u16)(space->max_pn_seen + 1 - space->base_pn); + bitmap_copy(new, space->pn_map, offset); + kfree(space->pn_map); + space->pn_map = new; + space->pn_map_len = len; + + return 0; +} + +/* Check if a packet number has been received. + * + * Returns: 0 if the packet number has not been received. 1 if it has already + * been received. -EINVAL if the packet number is too old or too far in the + * future to track. + */ +int quic_pnspace_check(struct quic_pnspace *space, s64 pn) +{ + if (space->base_pn == -1) /* No packet number received yet. */ + return 0; + + if (pn < space->min_pn_seen || pn >= space->base_pn + QUIC_PN_MAP_SIZE) + return -EINVAL; + + if (pn < space->base_pn) + return 1; + if (pn - space->base_pn < space->pn_map_len && + test_bit(pn - space->base_pn, space->pn_map)) + return 1; + + return 0; +} + +/* Advance base_pn past contiguous received packet numbers. Finds the next gap + * (unreceived packet) beyond @pn, shifts the bitmap, and updates base_pn + * accordingly. + */ +static void quic_pnspace_move(struct quic_pnspace *space, s64 pn) +{ + u16 offset; + + offset = (u16)(pn + 1 - space->base_pn); + offset = (u16)find_next_zero_bit(space->pn_map, space->pn_map_len, + offset); + space->base_pn += offset; + bitmap_shift_right(space->pn_map, space->pn_map, offset, + space->pn_map_len); +} + +/* Mark a packet number as received. Updates the packet number map to record + * reception of @pn. Advances base_pn if possible, and updates max/min/last + * seen fields as needed. + * + * Returns: 0 on success or if the packet was already marked, or a negative + * error returned by bitmap growth when expanding the map. + */ +int quic_pnspace_mark(struct quic_pnspace *space, s64 pn) +{ + s64 last_max_pn_seen; + u64 last_max_pn_time; + u16 gap; + int err; + + if (space->base_pn == -1) { + /* Initialize base_pn based on the peer's first packet number + * since peer's packet numbers may start at a non-zero value. + */ + quic_pnspace_set_base_pn(space, pn + 1); + return 0; + } + + /* Ignore packets with number less than current base (already + * processed). + */ + if (pn < space->base_pn) + return 0; + + /* If gap is beyond current map length, try to grow the bitmap to + * accommodate. + */ + gap = (u16)(pn - space->base_pn); + if (gap >= space->pn_map_len) { + err = quic_pnspace_grow(space, gap + 1); + if (err) + return err; + } + + if (space->max_pn_seen < pn) { + space->max_pn_seen = pn; + space->max_pn_time = space->time; + } + + if (space->base_pn == pn) { /* PN is next expected packet. */ + if (quic_pnspace_has_gap(space)) /* Advance to next gap. */ + quic_pnspace_move(space, pn); + else /* Fast path: increment base_pn if no gaps. */ + space->base_pn++; + } else { /* Mark this packet as received in the bitmap. */ + set_bit(gap, space->pn_map); + } + + /* Only update min and last_max_pn_seen if this packet is the current + * max_pn. + */ + if (space->max_pn_seen != pn) + return 0; + + /* Check if enough time has elapsed or enough packets have been + * received to update tracking. + */ + last_max_pn_seen = min_t(s64, space->last_max_pn_seen, space->base_pn); + last_max_pn_time = space->last_max_pn_time; + if (space->max_pn_time < last_max_pn_time + space->max_time_limit && + space->max_pn_seen <= last_max_pn_seen + QUIC_PN_MAP_LIMIT) + return 0; + + /* Advance base_pn if last_max_pn_seen is ahead of current base_pn. + * This is needed because QUIC doesn't retransmit packets; + * retransmitted frames are carried in new packets, so we move forward. + */ + if (space->last_max_pn_seen + 1 > space->base_pn) + quic_pnspace_move(space, space->last_max_pn_seen); + + space->min_pn_seen = space->last_max_pn_seen; + space->last_max_pn_seen = space->max_pn_seen; + space->last_max_pn_time = space->max_pn_time; + return 0; +} + +/* Find the next gap in received packet numbers. Scans pn_map for a gap + * starting from *@iter. A gap is a contiguous block of unreceived packets + * between received ones. + * + * Returns: true if a gap was found, false if no more gaps exist or are + * relevant. + */ +static bool quic_pnspace_next_gap_ack(const struct quic_pnspace *space, + s64 *iter, u16 *start, u16 *end) +{ + u16 start_ = 0, end_ = 0, offset = (u16)(*iter - space->base_pn); + + start_ = (u16)find_next_zero_bit(space->pn_map, space->pn_map_len, + offset); + if (space->max_pn_seen <= space->base_pn + start_) + return false; + + end_ = (u16)find_next_bit(space->pn_map, space->pn_map_len, start_); + if (space->max_pn_seen <= space->base_pn + end_ - 1) + return false; + + *start = start_ + 1; + *end = end_; + *iter = space->base_pn + *end; + return true; +} + +/* Generate gap acknowledgment blocks (GABs). GABs describe ranges of + * unacknowledged packets between received ones, and are used in ACK frames. + * + * Returns: Number of generated GABs (up to QUIC_PN_MAP_MAX_GABS). + */ +u16 quic_pnspace_num_gabs(struct quic_pnspace *space, + struct quic_gap_ack_block *gabs) +{ + u16 start, end, ngaps = 0; + s64 iter; + + if (!quic_pnspace_has_gap(space)) + return 0; + + iter = space->base_pn; + /* Loop through all gaps until the end of the window or max allowed + * gaps. + */ + while (quic_pnspace_next_gap_ack(space, &iter, &start, &end)) { + gabs[ngaps].start = start; + if (ngaps == QUIC_PN_MAP_MAX_GABS - 1) { + gabs[ngaps].end = + (u16)(space->max_pn_seen - space->base_pn); + ngaps++; + break; + } + gabs[ngaps].end = end; + ngaps++; + } + return ngaps; +} diff --git a/net/quic/pnspace.h b/net/quic/pnspace.h new file mode 100644 index 000000000000..0961add68401 --- /dev/null +++ b/net/quic/pnspace.h @@ -0,0 +1,157 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* QUIC kernel implementation + * (C) Copyright Red Hat Corp. 2023 + * + * This file is part of the QUIC kernel implementation + * + * Written or modified by: + * Xin Long + */ + +#define QUIC_PN_MAP_MAX_GABS 32 + +#define QUIC_PN_MAP_INITIAL 64 +#define QUIC_PN_MAP_INCREMENT QUIC_PN_MAP_INITIAL +#define QUIC_PN_MAP_SIZE 4096 +#define QUIC_PN_MAP_LIMIT (QUIC_PN_MAP_SIZE * 3 / 4) + +#define QUIC_PNSPACE_MAX (QUIC_CRYPTO_MAX - 1) +#define QUIC_PNSPACE_NEXT_PN 0 +#define QUIC_PNSPACE_TIME_LIMIT (333000 * 3) + +enum { + QUIC_ECN_ECT1, + QUIC_ECN_ECT0, + QUIC_ECN_CE, + QUIC_ECN_MAX +}; + +enum { + QUIC_ECN_LOCAL, /* ECN bits from incoming IP headers */ + QUIC_ECN_PEER, /* ECN bits reported by peer in ACK frames */ + QUIC_ECN_DIR_MAX +}; + +/* Represents a gap (range of missing packets) in the ACK map. The values are + * offsets from base_pn, with both 'start' and 'end' being +1. + */ +struct quic_gap_ack_block { + u16 start; + u16 end; +}; + +/* Packet Number Map (pn_map) Layout: + * + * min_pn_seen -->++-----------------------+---------------------+--- + * base_pn -----^ last_max_pn_seen --^ max_pn_seen --^ + * + * Map Advancement Logic: + * - min_pn_seen = last_max_pn_seen; + * - base_pn = first zero bit after last_max_pn_seen; + * - last_max_pn_seen = max_pn_seen; + * - last_max_pn_time = current time; + * + * Conditions to Advance pn_map: + * - (max_pn_time - last_max_pn_time) >= max_time_limit, or + * - (max_pn_seen - last_max_pn_seen) > QUIC_PN_MAP_LIMIT + * + * Gap Search Range: + * - From (base_pn - 1) to max_pn_seen + */ +struct quic_pnspace { + /* ECN counters indexed by dir and ECN codepoint (ECT1, ECT0, CE) */ + u64 ecn_count[QUIC_ECN_DIR_MAX][QUIC_ECN_MAX]; + unsigned long *pn_map; /* Received PN bitmap for ACK generation */ + u16 pn_map_len; /* Length of the PN bit map (in bits) */ + u8 need_sack; /* Flag indicating a SACK frame should be sent */ + u8 sack_path; /* Path used for sending the SACK frame */ + + s64 last_max_pn_seen; /* Largest PN seen before pn_map advance */ + u64 last_max_pn_time; /* Timestamp last_max_pn_seen was received */ + s64 min_pn_seen; /* Smallest PN received */ + s64 max_pn_seen; /* Largest PN received */ + u64 max_pn_time; /* Timestamp max_pn_seen was received */ + s64 base_pn; /* PN corresponding to the start of the pn_map */ + u64 time; /* Cached now, or latest socket accept timestamp */ + + s64 max_pn_acked_seen; /* Largest PN ACKed by peer */ + u64 max_pn_acked_time; /* Timestamp max_pn_acked_seen was ACKed */ + u64 last_sent_time; /* Timestamp last ack-eliciting packet sent */ + u64 loss_time; /* Timestamp the packet can be declared lost */ + s64 next_pn; /* Next PN to send */ + + u32 max_time_limit; /* Time threshold to trigger pn_map advance */ + u32 inflight; /* Ack-eliciting bytes in flight */ +}; + +static inline void +quic_pnspace_set_max_pn_acked_seen(struct quic_pnspace *space, + s64 max_pn_acked_seen) +{ + if (space->max_pn_acked_seen >= max_pn_acked_seen) + return; + space->max_pn_acked_seen = max_pn_acked_seen; + space->max_pn_acked_time = quic_ktime_get_us(); +} + +static inline void quic_pnspace_set_base_pn(struct quic_pnspace *space, s64 pn) +{ + space->base_pn = pn; + space->max_pn_seen = space->base_pn - 1; + space->last_max_pn_seen = space->max_pn_seen; + space->min_pn_seen = space->max_pn_seen; + + space->max_pn_time = space->time; + space->last_max_pn_time = space->max_pn_time; +} + +static inline bool quic_pnspace_has_gap(const struct quic_pnspace *space) +{ + return space->base_pn != space->max_pn_seen + 1; +} + +static inline void quic_pnspace_inc_ecn_count(struct quic_pnspace *space, + u8 ecn) +{ + if (!ecn) + return; + space->ecn_count[QUIC_ECN_LOCAL][ecn - 1]++; +} + +/* Check if any ECN-marked packets were received. */ +static inline bool quic_pnspace_has_ecn_count(struct quic_pnspace *space) +{ + return space->ecn_count[QUIC_ECN_LOCAL][QUIC_ECN_ECT0] || + space->ecn_count[QUIC_ECN_LOCAL][QUIC_ECN_ECT1] || + space->ecn_count[QUIC_ECN_LOCAL][QUIC_ECN_CE]; +} + +/* Updates the stored ECN counters based on values received in the peer's ACK + * frame. Each counter is updated only if the new value is higher. + * + * Returns: true if CE count was increased (congestion indicated), false + * otherwise. + */ +static inline bool quic_pnspace_set_ecn_count(struct quic_pnspace *space, + u64 *ecn_count) +{ + u64 *count = space->ecn_count[QUIC_ECN_PEER]; + + if (count[QUIC_ECN_ECT0] < ecn_count[QUIC_ECN_ECT0]) + count[QUIC_ECN_ECT0] = ecn_count[QUIC_ECN_ECT0]; + if (count[QUIC_ECN_ECT1] < ecn_count[QUIC_ECN_ECT1]) + count[QUIC_ECN_ECT1] = ecn_count[QUIC_ECN_ECT1]; + if (count[QUIC_ECN_CE] < ecn_count[QUIC_ECN_CE]) { + count[QUIC_ECN_CE] = ecn_count[QUIC_ECN_CE]; + return true; + } + return false; +} + +u16 quic_pnspace_num_gabs(struct quic_pnspace *space, + struct quic_gap_ack_block *gabs); +int quic_pnspace_check(struct quic_pnspace *space, s64 pn); +int quic_pnspace_mark(struct quic_pnspace *space, s64 pn); + +void quic_pnspace_free(struct quic_pnspace *space); +int quic_pnspace_init(struct quic_pnspace *space); diff --git a/net/quic/socket.c b/net/quic/socket.c index 7a4a00498b54..3b66cf8a942a 100644 --- a/net/quic/socket.c +++ b/net/quic/socket.c @@ -38,6 +38,8 @@ static void quic_write_space(struct sock *sk) static int quic_init_sock(struct sock *sk) { + u8 i; + sk->sk_destruct = inet_sock_destruct; sk->sk_write_space = quic_write_space; sock_set_flag(sk, SOCK_USE_WRITE_QUEUE); @@ -52,11 +54,21 @@ static int quic_init_sock(struct sock *sk) if (quic_stream_init(quic_streams(sk))) return -ENOMEM; + for (i = 0; i < QUIC_PNSPACE_MAX; i++) { + if (quic_pnspace_init(quic_pnspace(sk, i))) + return -ENOMEM; + } + return 0; } static void quic_destroy_sock(struct sock *sk) { + u8 i; + + for (i = 0; i < QUIC_PNSPACE_MAX; i++) + quic_pnspace_free(quic_pnspace(sk, i)); + quic_path_unbind(sk, quic_paths(sk), 0); quic_path_unbind(sk, quic_paths(sk), 1); diff --git a/net/quic/socket.h b/net/quic/socket.h index 9201ca3edad0..68c7b22d1e88 100644 --- a/net/quic/socket.h +++ b/net/quic/socket.h @@ -12,6 +12,7 @@ #include #include "common.h" +#include "pnspace.h" #include "family.h" #include "stream.h" #include "connid.h" @@ -43,6 +44,7 @@ struct quic_sock { struct quic_conn_id_set dest; struct quic_path_group paths; struct quic_cong cong; + struct quic_pnspace space[QUIC_PNSPACE_MAX]; }; struct quic6_sock { @@ -105,6 +107,11 @@ static inline struct quic_cong *quic_cong(const struct sock *sk) return &quic_sk(sk)->cong; } +static inline struct quic_pnspace *quic_pnspace(const struct sock *sk, u8 level) +{ + return &quic_sk(sk)->space[level % QUIC_CRYPTO_EARLY]; +} + static inline bool quic_is_establishing(struct sock *sk) { return sk->sk_state == QUIC_SS_ESTABLISHING; -- 2.47.1