From nobody Fri Apr 18 00:31:38 2025 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 4ZdwgK5HPQz5t3FM; Fri, 18 Apr 2025 00:31:41 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from smtp.freebsd.org (smtp.freebsd.org [IPv6:2610:1c1:1:606c::24b:4]) (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 "smtp.freebsd.org", Issuer "R11" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4ZdwgK4Spqz3t5H; Fri, 18 Apr 2025 00:31:41 +0000 (UTC) (envelope-from kevans@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1744936301; 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: in-reply-to:in-reply-to:references:references; bh=lWQbIXLWS8FaTCY8AcbbVRhYOkBqFw3VeNox4hJdkFk=; b=xc70EgfN+e8LRpH0UiBwHrG/AI7hjkuzfi2GdcSwJ4dBENvZzqNiKgsyViMWnUNPmq68hz T8ceYN5WcpSNuNjanaWNPn8SwV/eJCttNthW+/plVuMX6GFXwCRxqyr3Pe9hIxOkyjku6N Rir/Zemkw5FxS5qLiI8vSOrWxbXXKT42Hj6CAA5ueQ2A2+MnasyxKN12XqaN2tdyIe/ET5 CRHrv5C7uCJGj0PjA4VPOsfBIds4ni0MMfSYUitSNKKBHVyjuPf7dc1bylKd5bdfijZpSW 4tOGj2nnycqvhNQ/hjCboXkRuazEdbIp+ya4aQUzjRHBoMljpQNnN2rR3Y7s8g== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1744936301; a=rsa-sha256; cv=none; b=Ic1EzOu4Pzim0zg5XVdSWX8jVqXiVV2ARljieXrg1tXKIsIR+C4ZTlF2yJSvzagtNGf1al x1pcdiWl7a9QfEl6kiW6Rehf/v3vrvusQbSqGilbrOJqfFpROGI4nF+AESYtteFIz3Imq1 ixhzGxoi/WolvJm6jvkA0hZCA3Ux4Q1hlP+4Dub0fMs+4qTU+YwTh7kEiiPdxSsOW6t1Jf ORYS1ZF2YjEACUlbx3lqNGm0TAuGLINs9CVq2i0eZ/9ul/rSEUA+fl5SwYxwfAN4gcIrp3 n1g0+7SRd6RjVlsL/Wq+Ge65ha3lxAs5dEFRC317o8ZD7OlYHInGJ2Gh5vyrAw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1744936301; 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: in-reply-to:in-reply-to:references:references; bh=lWQbIXLWS8FaTCY8AcbbVRhYOkBqFw3VeNox4hJdkFk=; b=csbNAnwEQtd+7h9xpRKcAyaAXGvmXiKzpV/6x0rVlJSWPd9hUy+AUTMHN0ZYnRTrC2VpIy mzUJoCvyz01aZ3uYPAkgz8YO6Y/GCPCE/dQMfvimX0FzKtn0SYYs1lFADJ6aA3jb7tBUfy G8Sk/tEpKfXzA6GVvWfYh2YLb2YxApX0TAVZgBq7zl1Oa9wkPXWVmVX4YXPqXs0j/UjP3J 6AnLGQAdAiCDU9Yy2yTx29HkeeP6ngOaasekjktsEwl2xsk9RXi0I/BscTMy6In0uYpaIf bRdwEBqy0QvWbdseOs3YYGaFnoL0xOhnDD7oh55Z+KQTxrkKJXNUxzUzSTJtuQ== Received: from [10.9.4.95] (unknown [209.182.120.176]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) (Authenticated sender: kevans/mail) by smtp.freebsd.org (Postfix) with ESMTPSA id 4ZdwgJ5vvDzZLD; Fri, 18 Apr 2025 00:31:40 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Message-ID: <84d62d7d-7f94-4308-a629-cfe58d3c43d5@FreeBSD.org> Date: Thu, 17 Apr 2025 19:31:38 -0500 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: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: git: 7121e9414f29 - main - wg: Improve wg_peer_alloc() to simplify the calling From: Kyle Evans To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org References: <202504180030.53I0UXR0092012@gitrepo.freebsd.org> Content-Language: en-US In-Reply-To: <202504180030.53I0UXR0092012@gitrepo.freebsd.org> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit On 4/17/25 19:30, Kyle Evans wrote: > The branch main has been updated by kevans: > > URL: https://cgit.FreeBSD.org/src/commit/?id=7121e9414f294d116caeadd07ebd969136d3a631 > > commit 7121e9414f294d116caeadd07ebd969136d3a631 > Author: Aaron LI > AuthorDate: 2025-04-18 00:30:11 +0000 > Commit: Kyle Evans > CommitDate: 2025-04-18 00:30:11 +0000 > > wg: Improve wg_peer_alloc() to simplify the calling > > Move the necessary extra logics (i.e., noise_remote_enable() and > TAILQ_INSERT_TAIL()) from wg_ioctl_set() to wg_peer_alloc(), and thus > make it easier to be called. Actually, the updated version is more > asymmetric to wg_peer_destroy() and thus less likely to be misused. > Meanwhile, rename it to wg_peer_create() to look more consistent with > wg_peer_destroy(). > > Reviewed by: aly_aaronly.me (diff), markj > Obtained from: DragonflyBSD 902964ab24ba (with some changes) > --- > sys/dev/wg/if_wg.c | 42 ++++++++++++++++++++++++++---------------- > 1 file changed, 26 insertions(+), 16 deletions(-) > Sigh, sorry, forgot to tag this one: Differential Revision: https://reviews.freebsd.org/D49796 Thanks, Kyle Evans > diff --git a/sys/dev/wg/if_wg.c b/sys/dev/wg/if_wg.c > index 83e5d9e5ceb3..5a3b60e45b7a 100644 > --- a/sys/dev/wg/if_wg.c > +++ b/sys/dev/wg/if_wg.c > @@ -315,7 +315,8 @@ static void wg_timers_run_persistent_keepalive(void *); > static int wg_aip_add(struct wg_softc *, struct wg_peer *, sa_family_t, const void *, uint8_t); > static struct wg_peer *wg_aip_lookup(struct wg_softc *, sa_family_t, void *); > static void wg_aip_remove_all(struct wg_softc *, struct wg_peer *); > -static struct wg_peer *wg_peer_alloc(struct wg_softc *, const uint8_t [WG_KEY_SIZE]); > +static struct wg_peer *wg_peer_create(struct wg_softc *, > + const uint8_t [WG_KEY_SIZE], int *); > static void wg_peer_free_deferred(struct noise_remote *); > static void wg_peer_destroy(struct wg_peer *); > static void wg_peer_destroy_all(struct wg_softc *); > @@ -378,18 +379,26 @@ static void wg_module_deinit(void); > > /* TODO Peer */ > static struct wg_peer * > -wg_peer_alloc(struct wg_softc *sc, const uint8_t pub_key[WG_KEY_SIZE]) > +wg_peer_create(struct wg_softc *sc, const uint8_t pub_key[WG_KEY_SIZE], > + int *errp) > { > struct wg_peer *peer; > > sx_assert(&sc->sc_lock, SX_XLOCKED); > > peer = malloc(sizeof(*peer), M_WG, M_WAITOK | M_ZERO); > + > peer->p_remote = noise_remote_alloc(sc->sc_local, peer, pub_key); > - peer->p_tx_bytes = counter_u64_alloc(M_WAITOK); > - peer->p_rx_bytes = counter_u64_alloc(M_WAITOK); > + if ((*errp = noise_remote_enable(peer->p_remote)) != 0) { > + noise_remote_free(peer->p_remote, NULL); > + free(peer, M_WG); > + return (NULL); > + } > + > peer->p_id = peer_counter++; > peer->p_sc = sc; > + peer->p_tx_bytes = counter_u64_alloc(M_WAITOK); > + peer->p_rx_bytes = counter_u64_alloc(M_WAITOK); > > cookie_maker_init(&peer->p_cookie, pub_key); > > @@ -420,6 +429,13 @@ wg_peer_alloc(struct wg_softc *sc, const uint8_t pub_key[WG_KEY_SIZE]) > LIST_INIT(&peer->p_aips); > peer->p_aips_num = 0; > > + TAILQ_INSERT_TAIL(&sc->sc_peers, peer, p_entry); > + sc->sc_peers_num++; > + > + if (if_getlinkstate(sc->sc_ifp) == LINK_STATE_UP) > + wg_timers_enable(peer); > + > + DPRINTF(sc, "Peer %" PRIu64 " created\n", peer->p_id); > return (peer); > } > > @@ -2376,7 +2392,7 @@ wg_peer_add(struct wg_softc *sc, const nvlist_t *nvl) > size_t size; > struct noise_remote *remote; > struct wg_peer *peer = NULL; > - bool need_insert = false; > + bool need_cleanup = false; > > sx_assert(&sc->sc_lock, SX_XLOCKED); > > @@ -2408,8 +2424,10 @@ wg_peer_add(struct wg_softc *sc, const nvlist_t *nvl) > wg_aip_remove_all(sc, peer); > } > if (peer == NULL) { > - peer = wg_peer_alloc(sc, pub_key); > - need_insert = true; > + peer = wg_peer_create(sc, pub_key, &err); > + if (peer == NULL) > + goto out; > + need_cleanup = true; > } > if (nvlist_exists_binary(nvl, "endpoint")) { > endpoint = nvlist_get_binary(nvl, "endpoint", &size); > @@ -2467,19 +2485,11 @@ wg_peer_add(struct wg_softc *sc, const nvlist_t *nvl) > } > } > } > - if (need_insert) { > - if ((err = noise_remote_enable(peer->p_remote)) != 0) > - goto out; > - TAILQ_INSERT_TAIL(&sc->sc_peers, peer, p_entry); > - sc->sc_peers_num++; > - if (if_getlinkstate(sc->sc_ifp) == LINK_STATE_UP) > - wg_timers_enable(peer); > - } > if (remote != NULL) > noise_remote_put(remote); > return (0); > out: > - if (need_insert) /* If we fail, only destroy if it was new. */ > + if (need_cleanup) /* If we fail, only destroy if it was new. */ > wg_peer_destroy(peer); > if (remote != NULL) > noise_remote_put(remote);