From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=JzHzEqaw; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTPS id B17435A0289 for ; Mon, 23 Jun 2025 13:06:47 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1750676806; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d09ir1FT+18IRPi15RJGN7QAkeCxKacmrZ2eEliyutU=; b=JzHzEqawTPIYNMzusND6UX/t+p8K88mFBUnxD3PggDOLTcjNGEzS2J+2cgWQscOuB183Ni vdfzabGCChnIwOgXwOl5uYphLkMabF8jpGKJueHWD1ugURQgXNu9ETqxz/e8KXmHGWpTM0 LG3rMe15Er0ojEfRn4LotVRodTpCTc0= Received: from mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (ec2-35-165-154-97.us-west-2.compute.amazonaws.com [35.165.154.97]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-399-uiB9npWqNTWFARJk0jmlPQ-1; Mon, 23 Jun 2025 07:06:45 -0400 X-MC-Unique: uiB9npWqNTWFARJk0jmlPQ-1 X-Mimecast-MFC-AGG-ID: uiB9npWqNTWFARJk0jmlPQ_1750676804 Received: from mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.40]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-08.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 7882B1800343 for ; Mon, 23 Jun 2025 11:06:44 +0000 (UTC) Received: from lenovo-t14s.redhat.com (unknown [10.45.224.132]) by mx-prod-int-04.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 65A6F19560AB; Mon, 23 Jun 2025 11:06:43 +0000 (UTC) From: Laurent Vivier To: passt-dev@passt.top Subject: [PATCH v7 03/31] iov: Introduce iov_tail_clone() and iov_tail_drop(). Date: Mon, 23 Jun 2025 13:06:07 +0200 Message-ID: <20250623110635.1478625-4-lvivier@redhat.com> In-Reply-To: <20250623110635.1478625-1-lvivier@redhat.com> References: <20250623110635.1478625-1-lvivier@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.0 on 10.30.177.40 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: IEpjc0gpd3liy4-HmQfIjxQMaXesVja982VRfZtYTjY_1750676804 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: 73ZBG7TTXUH2U24E4TK6R6LL3GCZYGBI X-Message-ID-Hash: 73ZBG7TTXUH2U24E4TK6R6LL3GCZYGBI X-MailFrom: lvivier@redhat.com 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 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: These utilities enhance iov_tail manipulation, useful for efficient packet processing by enabling iovec array cloning and header stripping without data copies. - iov_tail_drop(): Discards a specified number of bytes from the beginning of an iov_tail by advancing its internal offset and pruning consumed elements. - iov_tail_clone(): Clone an iov_tail into an iovec array, adjusting the first iovec entry to remove the iov_tail offset. Signed-off-by: Laurent Vivier --- iov.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ iov.h | 3 +++ 2 files changed, 55 insertions(+) diff --git a/iov.c b/iov.c index f72145b30d1b..6ba0ada949b8 100644 --- a/iov.c +++ b/iov.c @@ -192,6 +192,21 @@ size_t iov_tail_size(struct iov_tail *tail) return iov_size(tail->iov, tail->cnt) - tail->off; } +/** + * iov_tail_drop() - Discard a header from an IOV tail + * @tail: IO vector tail + * @len: length to move the head of the tail + * + * Return: true if the item still contains any bytes, otherwise false + */ +/* cppcheck-suppress unusedFunction */ +bool iov_tail_drop(struct iov_tail *tail, size_t len) +{ + tail->off = tail->off + len; + + return iov_tail_prune(tail); +} + /** * iov_peek_header_() - Get pointer to a header from an IOV tail * @tail: IOV tail to get header from @@ -248,3 +263,40 @@ void *iov_remove_header_(struct iov_tail *tail, size_t len, size_t align) tail->off = tail->off + len; return p; } + +/** + * iov_tail_clone() - Assign iov references referencing a subset of the data + * in an iov_tail + * + * @dst_iov: Pointer to the destination array of struct iovec describing + * the scatter/gather I/O vector to shallow copy to. + * @dst_iov_cnt: Maximum number of elements in the destination iov array. + * @tail: Pointer to the source iov_tail + * + * Return: the number of elements successfully referenced from the destination + * iov array, a negative value if there is not enough room in the + * destination iov array + */ +/* cppcheck-suppress unusedFunction */ +ssize_t iov_tail_clone(struct iovec *dst_iov, size_t dst_iov_cnt, + struct iov_tail *tail) +{ + const struct iovec *iov = &tail->iov[0]; + size_t iov_cnt = tail->cnt; + size_t offset = tail->off; + unsigned int i, j; + + i = iov_skip_bytes(iov, iov_cnt, offset, &offset); + + /* assign iov references referencing a subset of the source one */ + for (j = 0; i < iov_cnt && j < dst_iov_cnt; i++, j++) { + dst_iov[j].iov_base = (char *)iov[i].iov_base + offset; + dst_iov[j].iov_len = iov[i].iov_len - offset; + offset = 0; + } + + if (j == dst_iov_cnt && i != iov_cnt) + return -1; + + return j; +} diff --git a/iov.h b/iov.h index 3fc96ab9755a..bf9820ac52ab 100644 --- a/iov.h +++ b/iov.h @@ -72,8 +72,11 @@ struct iov_tail { bool iov_tail_prune(struct iov_tail *tail); size_t iov_tail_size(struct iov_tail *tail); +bool iov_tail_drop(struct iov_tail *tail, size_t len); void *iov_peek_header_(struct iov_tail *tail, size_t len, size_t align); void *iov_remove_header_(struct iov_tail *tail, size_t len, size_t align); +ssize_t iov_tail_clone(struct iovec *dst_iov, size_t dst_iov_cnt, + struct iov_tail *tail); /** * IOV_PEEK_HEADER() - Get typed pointer to a header from an IOV tail -- 2.49.0