From owner-dev-commits-src-branches@freebsd.org Mon Jun 14 16:35:33 2021 Return-Path: Delivered-To: dev-commits-src-branches@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id B88526521A7; Mon, 14 Jun 2021 16:35: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 4G3cWT4qCPz3nF9; Mon, 14 Jun 2021 16:35:33 +0000 (UTC) (envelope-from git@FreeBSD.org) 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 8DE55180F9; Mon, 14 Jun 2021 16:35:33 +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 15EGZXiU056096; Mon, 14 Jun 2021 16:35:33 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 15EGZXmh056095; Mon, 14 Jun 2021 16:35:33 GMT (envelope-from git) Date: Mon, 14 Jun 2021 16:35:33 GMT Message-Id: <202106141635.15EGZXmh056095@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Kristof Provost Subject: git: 9c84b01ac069 - stable/12 - pf: Convenience function for optional (numeric) arguments MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: kp X-Git-Repository: src X-Git-Refname: refs/heads/stable/12 X-Git-Reftype: branch X-Git-Commit: 9c84b01ac069ddf50dba58f2fcff965c525dc946 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-branches@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commits to the stable branches of the FreeBSD src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 14 Jun 2021 16:35:33 -0000 The branch stable/12 has been updated by kp: URL: https://cgit.FreeBSD.org/src/commit/?id=9c84b01ac069ddf50dba58f2fcff965c525dc946 commit 9c84b01ac069ddf50dba58f2fcff965c525dc946 Author: Kristof Provost AuthorDate: 2021-05-15 11:45:55 +0000 Commit: Kristof Provost CommitDate: 2021-06-14 16:25:16 +0000 pf: Convenience function for optional (numeric) arguments Add _opt() variants for the uint* functions. These functions set the provided default value if the nvlist doesn't contain the relevant value. This is helpful for optional values (e.g. when the API is extended to add new fields). While here simplify the header by also using macros to create the prototypes for the macro-generated function implementations. Reviewed by: scottl MFC after: 2 weeks Sponsored by: Rubicon Communications, LLC ("Netgate") Differential Revision: https://reviews.freebsd.org/D30510 (cherry picked from commit 7c4342890bf17b72f0d79ada1326d9cbf34e736c) --- sys/netpfil/pf/pf_nv.c | 15 +++++++++++++++ sys/netpfil/pf/pf_nv.h | 35 ++++++++++++++--------------------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/sys/netpfil/pf/pf_nv.c b/sys/netpfil/pf/pf_nv.c index dab72f04d138..31943ba69687 100644 --- a/sys/netpfil/pf/pf_nv.c +++ b/sys/netpfil/pf/pf_nv.c @@ -40,6 +40,21 @@ __FBSDID("$FreeBSD$"); #include #define PF_NV_IMPL_UINT(fnname, type, max) \ + int \ + pf_nv ## fnname ## _opt(const nvlist_t *nvl, const char *name, \ + type *val, type dflt) \ + { \ + uint64_t raw; \ + if (! nvlist_exists_number(nvl, name)) { \ + *val = dflt; \ + return (0); \ + } \ + raw = nvlist_get_number(nvl, name); \ + if (raw > max) \ + return (ERANGE); \ + *val = (type)raw; \ + return (0); \ + } \ int \ pf_nv ## fnname(const nvlist_t *nvl, const char *name, type *val) \ { \ diff --git a/sys/netpfil/pf/pf_nv.h b/sys/netpfil/pf/pf_nv.h index 321c0425fe7f..e53d19018ffe 100644 --- a/sys/netpfil/pf/pf_nv.h +++ b/sys/netpfil/pf/pf_nv.h @@ -56,29 +56,22 @@ SDT_PROBE_DECLARE(pf, ioctl, nvchk, error); goto errout; \ } while (0) +#define PF_NV_DEF_UINT(fnname, type, max) \ + int pf_nv ## fnname ## _opt(const nvlist_t *, const char *, \ + type *, type); \ + int pf_nv ## fnname(const nvlist_t *, const char *, type *); \ + int pf_nv ## fnname ## _array(const nvlist_t *, const char *, \ + type *,size_t, size_t *); \ + void pf_ ## fnname ## _array_nv(nvlist_t *, const char *, \ + const type *, size_t); + +PF_NV_DEF_UINT(uint8, uint8_t, UINT8_MAX); +PF_NV_DEF_UINT(uint16, uint16_t, UINT16_MAX); +PF_NV_DEF_UINT(uint32, uint32_t, UINT32_MAX); +PF_NV_DEF_UINT(uint64, uint64_t, UINT64_MAX); + int pf_nvbinary(const nvlist_t *, const char *, void *, size_t); int pf_nvint(const nvlist_t *, const char *, int *); -int pf_nvuint8(const nvlist_t *, const char *, uint8_t *); -int pf_nvuint8_array(const nvlist_t *, const char *, uint8_t *, - size_t, size_t *); -void pf_uint8_array_nv(nvlist_t *, const char *, const uint8_t *, - size_t); -int pf_nvuint16(const nvlist_t *, const char *, uint16_t *); -int pf_nvuint16_array(const nvlist_t *, const char *, uint16_t *, - size_t, size_t *); -void pf_uint16_array_nv(nvlist_t *, const char *, const uint16_t *, - size_t); -int pf_nvuint32(const nvlist_t *, const char *, uint32_t *); -int pf_nvuint32_array(const nvlist_t *, const char *, uint32_t *, - size_t, size_t *); -void pf_uint32_array_nv(nvlist_t *, const char *, const uint32_t *, - size_t); -int pf_nvuint64(const nvlist_t *, const char *, uint64_t *); -int pf_nvuint64_array(const nvlist_t *, const char *, uint64_t *, - size_t, size_t *); -void pf_uint64_array_nv(nvlist_t *, const char *, const uint64_t *, - size_t); - int pf_nvstring(const nvlist_t *, const char *, char *, size_t); /* Translation functions */