From nobody Wed Mar 08 12:29:32 2023 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4PWs7x03tcz3wLC3; Wed, 8 Mar 2023 12:29:33 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4PWs7w6g48z4Jt7; Wed, 8 Mar 2023 12:29:32 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1678278572; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yexFzuc1rjBFQrYUPUrpReEXmPO259xjl/KV7EYkqGU=; b=LRkuHvlv1Fr9CVSKXnIMl4DX1ZqqDQg6xX/33FgA/b3oVeYqnvQ7oAmVu3ESu0T5R7P5kL x07Q0b8I9RC3MLvYihne1JLF2tL24OUJdghRz8QI3Vin8eLG5rkmpWjx5OB1kcGZQhAwWj S+4FF4FFDawiudiR6OOnApmXvlN1b63xuw9j1pcuijC+LtwOqKxUFeeUevC0G9bHTzVzDc YZwUAhhLewoiWKuKLT1cN7nX1zaIXmGARkg1BPgkT4q7stciN6THkPGOn2WUDdlIqVxHpr ppVDXO9pLX5ulIHLueOMcsS0a1/HImtbB8h4y0lD7hjwEjAczr2cfWAzpkkCBw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1678278572; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=yexFzuc1rjBFQrYUPUrpReEXmPO259xjl/KV7EYkqGU=; b=VGQUq0VBL4kpPxX5nKjFcipPz/2+V8BpPPCgjDOv9jMsjS11HRroozsgLOAjo+nMOcRoXT ppNa4VjfLPX4LVHzNzu45ntzP3+4i444DxIzwfhXQ0vpTq8WhoyfT7C/AA6xHFg2pVP3TH Ke7nKoBMsdP85U4Xfltyf3g1TArNf1Tv8J1jAAOnNgxQ/h4dYkwdD2CD3qxvA5x7Ii7a0n f6DbbRfXhE1HLVWuX4ECS3VxwXAyaWGuUuxRjY0J7FscZWumV/jXMj1qc2CnobysxybMUU srrzf9rAe1iOWwFfGTo1/riAZFqkQbF0md9vq2www6cwXb9is7eUFKcSWuuMew== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1678278572; a=rsa-sha256; cv=none; b=xwoGiQMqpiedjdWZZ+lCRBswY6nG/MN1o0RU5Tz2wwx3yWYol1Hl2c6H3q6kqEXxWVc4uD ad3QMpzIJK3T5ltdbSJbu8E6d7Vqe3j581KFeBdu0efH0I0CutLUuP6GsLUHu4s4mrhx67 1rEI6WmJuQMwIo9M9R9QIYUTHGozuM/5RhRJxuZYqPVEiC7VXYW8+MuX9IIp7AjpeS0rcA Cl4AzH9J62uTZzKJNFsvPlXJpnpAM/Wg3Vj8vqcthQJWmpuA9WGO3pfCvEKTX0LNCwwqtG nGFk6nkexUMMvGMICQb9UfqLiyWJnOXE1JOOuzqekPfxRffmWHWh02cCsk03ew== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4PWs7w5jZKzQXL; Wed, 8 Mar 2023 12:29:32 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 328CTWNu051912; Wed, 8 Mar 2023 12:29:32 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 328CTWms051911; Wed, 8 Mar 2023 12:29:32 GMT (envelope-from git) Date: Wed, 8 Mar 2023 12:29:32 GMT Message-Id: <202303081229.328CTWms051911@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: "Alexander V. Chernikov" Subject: git: 0679eb1f39aa - main - netlink: make snl(3) scratch buffer growable List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: melifaro X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 0679eb1f39aa798da6c4601bb06bab4c175c4b18 Auto-Submitted: auto-generated X-ThisMailContainsUnwantedMimeParts: N The branch main has been updated by melifaro: URL: https://cgit.FreeBSD.org/src/commit/?id=0679eb1f39aa798da6c4601bb06bab4c175c4b18 commit 0679eb1f39aa798da6c4601bb06bab4c175c4b18 Author: Alexander V. Chernikov AuthorDate: 2023-03-08 11:17:43 +0000 Commit: Alexander V. Chernikov CommitDate: 2023-03-08 12:28:55 +0000 netlink: make snl(3) scratch buffer growable Differential Revision: https://reviews.freebsd.org/D38946 MFC after: 2 weeks --- sys/netlink/netlink_snl.h | 90 ++++++++++++++++++++++++++++++++++++----------- 1 file changed, 69 insertions(+), 21 deletions(-) diff --git a/sys/netlink/netlink_snl.h b/sys/netlink/netlink_snl.h index d88808ea6d3b..3c63e3f95419 100644 --- a/sys/netlink/netlink_snl.h +++ b/sys/netlink/netlink_snl.h @@ -70,11 +70,31 @@ #define NL_ARRAY_LEN(_a) (sizeof(_a) / sizeof((_a)[0])) struct linear_buffer { - char *base; /* Base allocated memory pointer */ - uint32_t offset; /* Currently used offset */ - uint32_t size; /* Total buffer size */ + char *base; /* Base allocated memory pointer */ + uint32_t offset; /* Currently used offset */ + uint32_t size; /* Total buffer size */ + struct linear_buffer *next; /* Buffer chaining */ }; +static inline struct linear_buffer * +lb_init(uint32_t size) +{ + struct linear_buffer *lb = calloc(1, size); + + if (lb != NULL) { + lb->base = (char *)(lb + 1); + lb->size = size - sizeof(*lb); + } + + return (lb); +} + +static inline void +lb_free(struct linear_buffer *lb) +{ + free(lb); +} + static inline char * lb_allocz(struct linear_buffer *lb, int len) { @@ -101,7 +121,7 @@ struct snl_state { size_t datalen; uint32_t seq; bool init_done; - struct linear_buffer lb; + struct linear_buffer *lb; }; #define SCRATCH_BUFFER_SIZE 1024 @@ -145,6 +165,45 @@ static const struct snl_hdr_parser _name = { \ } +static inline void * +snl_allocz(struct snl_state *ss, int len) +{ + void *data = lb_allocz(ss->lb, len); + + if (data == NULL) { + uint32_t size = ss->lb->size * 2; + + while (size < len + sizeof(struct linear_buffer)) + size *= 2; + + struct linear_buffer *lb = lb_init(size); + + if (lb != NULL) { + lb->next = ss->lb; + ss->lb = lb; + data = lb_allocz(ss->lb, len); + } + } + + return (data); +} + +static inline void +snl_clear_lb(struct snl_state *ss) +{ + struct linear_buffer *lb = ss->lb; + + lb_clear(lb); + lb = lb->next; + ss->lb->next = NULL; + /* Remove all linear bufs except the largest one */ + while (lb != NULL) { + struct linear_buffer *lb_next = lb->next; + lb_free(lb); + lb = lb_next; + } +} + static void snl_free(struct snl_state *ss) { @@ -152,8 +211,10 @@ snl_free(struct snl_state *ss) close(ss->fd); if (ss->buf != NULL) free(ss->buf); - if (ss->lb.base != NULL) - free(ss->lb.base); + if (ss->lb != NULL) { + snl_clear_lb(ss); + lb_free(ss->lb); + } } } @@ -181,9 +242,8 @@ snl_init(struct snl_state *ss, int netlink_family) return (false); } - ss->lb.size = SCRATCH_BUFFER_SIZE; - ss->lb.base = calloc(1, ss->lb.size); - if (ss->lb.base == NULL) { + ss->lb = lb_init(SCRATCH_BUFFER_SIZE); + if (ss->lb == NULL) { snl_free(ss); return (false); } @@ -191,18 +251,6 @@ snl_init(struct snl_state *ss, int netlink_family) return (true); } -static inline void * -snl_allocz(struct snl_state *ss, int len) -{ - return (lb_allocz(&ss->lb, len)); -} - -static inline void -snl_clear_lb(struct snl_state *ss) -{ - lb_clear(&ss->lb); -} - static inline bool snl_send(struct snl_state *ss, void *data, int sz) {