Re: [PATCH net-next v4 06/15] quic: add stream management
From: Paolo Abeni <pabeni@redhat.com>
Date: 2025-11-04 11:05:53
Also in:
linux-cifs
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.
+ 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 quic_stream_create() helper using an additonal
argument for the relevant table.{send,recv}
- replace the above 2 functions with a single invocation to such helper.
It looks like there are more de-dup opportunity below.
+
+/* 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. /P