public inbox for quic@lists.linux.dev 
 help / color / mirror / Atom feed
From: Xin Long <lucien.xin@gmail.com>
To: Paolo Abeni <pabeni@redhat.com>
Cc: network dev <netdev@vger.kernel.org>,
	quic@lists.linux.dev, davem@davemloft.net,  kuba@kernel.org,
	Eric Dumazet <edumazet@google.com>,
	Simon Horman <horms@kernel.org>,
	 Stefan Metzmacher <metze@samba.org>,
	Moritz Buhl <mbuhl@openbsd.org>,
	Tyler Fanelli <tfanelli@redhat.com>,
	 Pengtao He <hepengtao@xiaomi.com>,
	Thomas Dreibholz <dreibh@simula.no>,
	linux-cifs@vger.kernel.org,  Steve French <smfrench@gmail.com>,
	Namjae Jeon <linkinjeon@kernel.org>,
	 Paulo Alcantara <pc@manguebit.com>, Tom Talpey <tom@talpey.com>,
	kernel-tls-handshake@lists.linux.dev,
	 Chuck Lever <chuck.lever@oracle.com>,
	Jeff Layton <jlayton@kernel.org>,
	 Steve Dickson <steved@redhat.com>,
	Hannes Reinecke <hare@suse.de>,
	Alexander Aring <aahringo@redhat.com>,
	 David Howells <dhowells@redhat.com>,
	Matthieu Baerts <matttbe@kernel.org>,
	 John Ericson <mail@johnericson.me>,
	Cong Wang <xiyou.wangcong@gmail.com>,
	 "D . Wythe" <alibuda@linux.alibaba.com>,
	Jason Baron <jbaron@akamai.com>,
	 illiliti <illiliti@protonmail.com>,
	Sabrina Dubroca <sd@queasysnail.net>,
	 Marcelo Ricardo Leitner <marcelo.leitner@gmail.com>,
	Daniel Stenberg <daniel@haxx.se>,
	 Andy Gospodarek <andrew.gospodarek@broadcom.com>
Subject: Re: [PATCH net-next v4 06/15] quic: add stream management
Date: Wed, 5 Nov 2025 20:27:19 -0500	[thread overview]
Message-ID: <CADvbK_c2gUNyDNYfgVrQ+Cm9rL6P_n+s0LJsrAPz0VK9FDDxyg@mail.gmail.com> (raw)
In-Reply-To: <ad38f56b-5c53-408e-abcc-4b061c2097a3@redhat.com>

On Tue, Nov 4, 2025 at 6:05 AM Paolo Abeni <pabeni@redhat.com> wrote:
>
> On 10/29/25 3:35 PM, Xin Long wrote:
> +/* Create and register new streams for sending. */
> > +static struct quic_stream *quic_stream_send_create(struct quic_stream_table *streams,
> > +                                                s64 max_stream_id, u8 is_serv)
> > +{
> > +     struct quic_stream *stream = NULL;
> > +     s64 stream_id;
> > +
> > +     stream_id = streams->send.next_bidi_stream_id;
> > +     if (quic_stream_id_uni(max_stream_id))
> > +             stream_id = streams->send.next_uni_stream_id;
> > +
> > +     /* rfc9000#section-2.1: A stream ID that is used out of order results in all streams
> > +      * of that type with lower-numbered stream IDs also being opened.
> > +      */
> > +     while (stream_id <= max_stream_id) {
> > +             stream = kzalloc(sizeof(*stream), GFP_KERNEL_ACCOUNT);
> > +             if (!stream)
> > +                     return NULL;
> > +
> > +             stream->id = stream_id;
> > +             if (quic_stream_id_uni(stream_id)) {
> > +                     stream->send.max_bytes = streams->send.max_stream_data_uni;
> > +
> > +                     if (streams->send.next_uni_stream_id < stream_id + QUIC_STREAM_ID_STEP)
> > +                             streams->send.next_uni_stream_id = stream_id + QUIC_STREAM_ID_STEP;
>
> It's unclear to me the goal the above 2 statements. Dealing with id
> wrap-arounds? If 'streams->send.next_uni_stream_id < stream_id +
> QUIC_STREAM_ID_STEP' is not true the next quic_stream_send_create() will
> reuse the same stream_id.
>
> I moving the above in a separate helper with some comments would help.
>
I will add a macro for this:

#define quic_stream_id_next_update(limits, type, id)    \
do {                                                    \
        if ((limits)->next_##type##_stream_id < (id) +
QUIC_STREAM_ID_STEP)     \
                (limits)->next_##type##_stream_id = (id) +
QUIC_STREAM_ID_STEP; \
        (limits)->streams_##type++;
         \
} while (0)

So that we can use it to update both next_uni_stream_id and next_bidi_stream_id.

>
> > +                     streams->send.streams_uni++;
> > +
> > +                     quic_stream_add(streams, stream);
> > +                     stream_id += QUIC_STREAM_ID_STEP;
> > +                     continue;
> > +             }
> > +
> > +             if (streams->send.next_bidi_stream_id < stream_id + QUIC_STREAM_ID_STEP)
> > +                     streams->send.next_bidi_stream_id = stream_id + QUIC_STREAM_ID_STEP;
> > +             streams->send.streams_bidi++;
> > +
> > +             if (quic_stream_id_local(stream_id, is_serv)) {
> > +                     stream->send.max_bytes = streams->send.max_stream_data_bidi_remote;
> > +                     stream->recv.max_bytes = streams->recv.max_stream_data_bidi_local;
> > +             } else {
> > +                     stream->send.max_bytes = streams->send.max_stream_data_bidi_local;
> > +                     stream->recv.max_bytes = streams->recv.max_stream_data_bidi_remote;
> > +             }
> > +             stream->recv.window = stream->recv.max_bytes;
> > +
> > +             quic_stream_add(streams, stream);
> > +             stream_id += QUIC_STREAM_ID_STEP;
> > +     }
> > +     return stream;
> > +}
> > +
> > +/* Create and register new streams for receiving. */
> > +static struct quic_stream *quic_stream_recv_create(struct quic_stream_table *streams,
> > +                                                s64 max_stream_id, u8 is_serv)
> > +{
> > +     struct quic_stream *stream = NULL;
> > +     s64 stream_id;
> > +
> > +     stream_id = streams->recv.next_bidi_stream_id;
> > +     if (quic_stream_id_uni(max_stream_id))
> > +             stream_id = streams->recv.next_uni_stream_id;
> > +
> > +     /* rfc9000#section-2.1: A stream ID that is used out of order results in all streams
> > +      * of that type with lower-numbered stream IDs also being opened.
> > +      */
> > +     while (stream_id <= max_stream_id) {
> > +             stream = kzalloc(sizeof(*stream), GFP_ATOMIC | __GFP_ACCOUNT);
> > +             if (!stream)
> > +                     return NULL;
> > +
> > +             stream->id = stream_id;
> > +             if (quic_stream_id_uni(stream_id)) {
> > +                     stream->recv.window = streams->recv.max_stream_data_uni;
> > +                     stream->recv.max_bytes = stream->recv.window;
> > +
> > +                     if (streams->recv.next_uni_stream_id < stream_id + QUIC_STREAM_ID_STEP)
> > +                             streams->recv.next_uni_stream_id = stream_id + QUIC_STREAM_ID_STEP;
> > +                     streams->recv.streams_uni++;
> > +
> > +                     quic_stream_add(streams, stream);
> > +                     stream_id += QUIC_STREAM_ID_STEP;
> > +                     continue;
> > +             }
> > +
> > +             if (streams->recv.next_bidi_stream_id < stream_id + QUIC_STREAM_ID_STEP)
> > +                     streams->recv.next_bidi_stream_id = stream_id + QUIC_STREAM_ID_STEP;
> > +             streams->recv.streams_bidi++;
> > +
> > +             if (quic_stream_id_local(stream_id, is_serv)) {
> > +                     stream->send.max_bytes = streams->send.max_stream_data_bidi_remote;
> > +                     stream->recv.max_bytes = streams->recv.max_stream_data_bidi_local;
> > +             } else {
> > +                     stream->send.max_bytes = streams->send.max_stream_data_bidi_local;
> > +                     stream->recv.max_bytes = streams->recv.max_stream_data_bidi_remote;
> > +             }
> > +             stream->recv.window = stream->recv.max_bytes;
> > +
> > +             quic_stream_add(streams, stream);
> > +             stream_id += QUIC_STREAM_ID_STEP;
> > +     }
> > +     return stream;
> > +}
>
> The above 2 functions has a lot of code in common. I think you could
> deduplicate it by:
> - defining a named type for quic_stream_table.{send,recv}
> - define a generic /() helper using an additonal
> argument for the relevant table.{send,recv}
> - replace the above 2 functions with a single invocation to such helper.
This is a very smart idea!

It will dedup not only quic_stream_recv_create(), but also
quic_stream_get_param() and quic_stream_set_param().

I will define a type named 'struct quic_stream_limits'.
Note that, since we must pass 'bool send' to quic_stream_create() for
setting the fields in a single 'stream' .

        if (quic_stream_id_uni(stream_id)) {
                if (send) {
                        stream->send.max_bytes = limits->max_stream_data_uni;
                } else {
                        stream->recv.max_bytes = limits->max_stream_data_uni;
                        stream->recv.window = stream->recv.max_bytes;
                }

I'm planning not to pass additional argument of table.{send,recv},
but do this in quic_stream_create():
        struct quic_stream_limits *limits = &streams->send;
        gfp_t gfp = GFP_KERNEL_ACCOUNT;

        if (!send) {
                limits = &streams->recv;
                gfp = GFP_ATOMIC | __GFP_ACCOUNT;
        }

>
> It looks like there are more de-dup opportunity below.
>
Yes, the difference is only the variable name _uni_ and _bidi_.
I'm planning to de-dup them with macros like:

#define quic_stream_id_below_next(streams, type, id, send)        \
    ((send) ? ((id) < (streams)->send.next_##type##_stream_id) :    \
          ((id) < (streams)->recv.next_##type##_stream_id))

/* Check if a send or receive stream ID is already closed. */
static bool quic_stream_id_closed(struct quic_stream_table *streams,
s64 stream_id, bool send)
{
    if (quic_stream_id_uni(stream_id))
        return quic_stream_id_below_next(streams, uni, stream_id, send);
    return quic_stream_id_below_next(streams, bidi, stream_id, send);
}

#define quic_stream_id_above_max(streams, type, id)            \
    (((id) > (streams)->send.max_##type##_stream_id) ? true :    \
        (quic_stream_id_to_streams((id) -
(streams)->send.next_##type##_stream_id) +    \
            (streams)->send.streams_##type >
(streams)->send.max_streams_##type))

/* Check if a stream ID would exceed local (recv) or peer (send) limits. */
bool quic_stream_id_exceeds(struct quic_stream_table *streams, s64
stream_id, bool send)
{
    if (!send) {
        if (quic_stream_id_uni(stream_id))
            return stream_id > streams->recv.max_uni_stream_id;
        return stream_id > streams->recv.max_bidi_stream_id;
    }

    if (quic_stream_id_uni(stream_id))
        return quic_stream_id_above_max(streams, uni, stream_id);
    return quic_stream_id_above_max(streams, bidi, stream_id);
}

Do you think it's worth it?

> > +
> > +/* Check if a send or receive stream ID is already closed. */
> > +static bool quic_stream_id_closed(struct quic_stream_table *streams, s64 stream_id, bool send)
> > +{
> > +     if (quic_stream_id_uni(stream_id)) {
> > +             if (send)
> > +                     return stream_id < streams->send.next_uni_stream_id;
> > +             return stream_id < streams->recv.next_uni_stream_id;
> > +     }
> > +     if (send)
> > +             return stream_id < streams->send.next_bidi_stream_id;
> > +     return stream_id < streams->recv.next_bidi_stream_id;
> > +}
> > +
> > +/* Check if a stream ID would exceed local (recv) or peer (send) limits. */
> > +bool quic_stream_id_exceeds(struct quic_stream_table *streams, s64 stream_id, bool send)
> > +{
> > +     u64 nstreams;
> > +
> > +     if (!send) {
> > +             if (quic_stream_id_uni(stream_id))
> > +                     return stream_id > streams->recv.max_uni_stream_id;
> > +             return stream_id > streams->recv.max_bidi_stream_id;
> > +     }
> > +
> > +     if (quic_stream_id_uni(stream_id)) {
> > +             if (stream_id > streams->send.max_uni_stream_id)
> > +                     return true;
> > +             stream_id -= streams->send.next_uni_stream_id;
> > +             nstreams = quic_stream_id_to_streams(stream_id);
> > +             return nstreams + streams->send.streams_uni > streams->send.max_streams_uni;
> > +     }
> > +
> > +     if (stream_id > streams->send.max_bidi_stream_id)
> > +             return true;
> > +     stream_id -= streams->send.next_bidi_stream_id;
> > +     nstreams = quic_stream_id_to_streams(stream_id);
> > +     return nstreams + streams->send.streams_bidi > streams->send.max_streams_bidi;
> > +}
> > +
> > +/* Get or create a send stream by ID. */
> > +struct quic_stream *quic_stream_send_get(struct quic_stream_table *streams, s64 stream_id,
> > +                                      u32 flags, bool is_serv)
> > +{
> > +     struct quic_stream *stream;
> > +
> > +     if (!quic_stream_id_valid(stream_id, is_serv, true))
> > +             return ERR_PTR(-EINVAL);
> > +
> > +     stream = quic_stream_find(streams, stream_id);
> > +     if (stream) {
>
> You should add some comments and possibly lockdep annotation/static
> check about the expected locking for the whole stream lifecycle.
>
sk is not seen in this file, so I will add some comments to describe this
will also be called under the sock lock.

Thanks.

  reply	other threads:[~2025-11-06  1:27 UTC|newest]

Thread overview: 48+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-10-29 14:35 [PATCH net-next v4 00/15] net: introduce QUIC infrastructure and core subcomponents Xin Long
2025-10-29 14:35 ` [PATCH net-next v4 01/15] net: define IPPROTO_QUIC and SOL_QUIC constants Xin Long
2025-11-04  9:20   ` Paolo Abeni
2025-10-29 14:35 ` [PATCH net-next v4 02/15] net: build socket infrastructure for QUIC protocol Xin Long
2025-10-29 16:22   ` Stefan Metzmacher
2025-10-29 19:57     ` Xin Long
2025-10-30 11:29       ` Stefan Metzmacher
2025-10-30 14:13         ` Xin Long
2025-10-30 14:17           ` Stefan Metzmacher
2025-10-30 14:28             ` Xin Long
2025-11-04  9:38   ` Paolo Abeni
2025-11-05 22:20     ` Xin Long
2025-10-29 14:35 ` [PATCH net-next v4 03/15] quic: provide common utilities and data structures Xin Long
2025-11-04  9:55   ` Paolo Abeni
2025-10-29 14:35 ` [PATCH net-next v4 04/15] quic: provide family ops for address and protocol Xin Long
2025-11-04 10:27   ` Paolo Abeni
2025-11-06  1:01     ` Xin Long
2025-10-29 14:35 ` [PATCH net-next v4 05/15] quic: provide quic.h header files for kernel and userspace Xin Long
2025-10-29 14:35 ` [PATCH net-next v4 06/15] quic: add stream management Xin Long
2025-11-04 11:05   ` Paolo Abeni
2025-11-06  1:27     ` Xin Long [this message]
2025-11-06  8:51       ` Paolo Abeni
2025-11-06 16:22         ` Xin Long
2025-10-29 14:35 ` [PATCH net-next v4 07/15] quic: add connection id management Xin Long
2025-10-29 14:35 ` [PATCH net-next v4 08/15] quic: add path management Xin Long
2025-11-04 11:50   ` Paolo Abeni
2025-11-06  1:28     ` Xin Long
2025-10-29 14:35 ` [PATCH net-next v4 09/15] quic: add congestion control Xin Long
2025-11-04 12:02   ` Paolo Abeni
2025-11-06 20:24     ` Xin Long
2025-10-29 14:35 ` [PATCH net-next v4 10/15] quic: add packet number space Xin Long
2025-11-04 12:17   ` Paolo Abeni
2025-11-06 16:40     ` Xin Long
2025-10-29 14:35 ` [PATCH net-next v4 11/15] quic: add crypto key derivation and installation Xin Long
2025-10-29 14:35 ` [PATCH net-next v4 12/15] quic: add crypto packet encryption and decryption Xin Long
2025-10-29 14:35 ` [PATCH net-next v4 13/15] quic: add timer management Xin Long
2025-11-04 12:33   ` Paolo Abeni
2025-11-06 16:49     ` Xin Long
2025-11-13 21:23       ` Xin Long
2025-10-29 14:35 ` [PATCH net-next v4 14/15] quic: add frame encoder and decoder base Xin Long
2025-11-04 12:47   ` Paolo Abeni
2025-11-06 17:22     ` Xin Long
2025-11-13 21:26       ` Xin Long
2025-10-29 14:35 ` [PATCH net-next v4 15/15] quic: add packet builder and parser base Xin Long
2025-11-04 14:44   ` Paolo Abeni
2025-11-06 19:24     ` Xin Long
2025-11-04  2:41 ` [PATCH net-next v4 00/15] net: introduce QUIC infrastructure and core subcomponents Jakub Kicinski
2025-11-05 22:19   ` Xin Long

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=CADvbK_c2gUNyDNYfgVrQ+Cm9rL6P_n+s0LJsrAPz0VK9FDDxyg@mail.gmail.com \
    --to=lucien.xin@gmail.com \
    --cc=aahringo@redhat.com \
    --cc=alibuda@linux.alibaba.com \
    --cc=andrew.gospodarek@broadcom.com \
    --cc=chuck.lever@oracle.com \
    --cc=daniel@haxx.se \
    --cc=davem@davemloft.net \
    --cc=dhowells@redhat.com \
    --cc=dreibh@simula.no \
    --cc=edumazet@google.com \
    --cc=hare@suse.de \
    --cc=hepengtao@xiaomi.com \
    --cc=horms@kernel.org \
    --cc=illiliti@protonmail.com \
    --cc=jbaron@akamai.com \
    --cc=jlayton@kernel.org \
    --cc=kernel-tls-handshake@lists.linux.dev \
    --cc=kuba@kernel.org \
    --cc=linkinjeon@kernel.org \
    --cc=linux-cifs@vger.kernel.org \
    --cc=mail@johnericson.me \
    --cc=marcelo.leitner@gmail.com \
    --cc=matttbe@kernel.org \
    --cc=mbuhl@openbsd.org \
    --cc=metze@samba.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pc@manguebit.com \
    --cc=quic@lists.linux.dev \
    --cc=sd@queasysnail.net \
    --cc=smfrench@gmail.com \
    --cc=steved@redhat.com \
    --cc=tfanelli@redhat.com \
    --cc=tom@talpey.com \
    --cc=xiyou.wangcong@gmail.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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox