public inbox for netdev@vger.kernel.org 
 help / color / mirror / Atom feed
From: Jiri Pirko <jiri@resnulli•us>
To: Jamal Hadi Salim <jhs@mojatatu•com>
Cc: davem@davemloft•net, netdev@vger•kernel.org,
	xiyou.wangcong@gmail•com, dsahern@gmail•com,
	eric.dumazet@gmail•com, mrv@mojatatu•com,
	simon.horman@netronome•com, alex.aring@gmail•com
Subject: Re: [PATCH net-next v11 1/4] net netlink: Add new type NLA_BITFIELD_32
Date: Mon, 24 Jul 2017 13:14:52 +0200	[thread overview]
Message-ID: <20170724111452.GA1868@nanopsycho> (raw)
In-Reply-To: <1500860146-26970-2-git-send-email-jhs@emojatatu.com>

Mon, Jul 24, 2017 at 03:35:43AM CEST, jhs@mojatatu•com wrote:
>From: Jamal Hadi Salim <jhs@mojatatu•com>
>
>Generic bitflags attribute content sent to the kernel by user.
>With this type the user can either set or unset a flag in the
>kernel.
>
>The nla_value is a bitmap that defines the values being set
>The nla_selector is a bitmask that defines which value is legit.
>
>A check is made to ensure the rules that a kernel subsystem always
>conforms to bitflags the kernel already knows about. i.e
>if the user tries to set a bit flag that is not understood then
>the _it will be rejected_.
>
>In the most basic form, the user specifies the attribute policy as:
>[ATTR_GOO] = { .type = NLA_BITFIELD_32, .validation_data = &myvalidflags },
>
>where myvalidflags is the bit mask of the flags the kernel understands.
>
>If the user _does not_ provide myvalidflags then the attribute will
>also be rejected.
>
>Examples:
>nla_value = 0x0, and nla_selector = 0x1
>implies we are selecting bit 1 and we want to set its value to 0.
>
>nla_value = 0x2, and nla_selector = 0x2
>implies we are selecting bit 2 and we want to set its value to 1.
>
>Signed-off-by: Jamal Hadi Salim <jhs@mojatatu•com>
>---
> include/net/netlink.h        |  4 ++++
> include/uapi/linux/netlink.h | 17 +++++++++++++++++
> lib/nlattr.c                 | 21 +++++++++++++++++++++
> 3 files changed, 42 insertions(+)
>
>diff --git a/include/net/netlink.h b/include/net/netlink.h
>index ef8e6c3..e33d1fb 100644
>--- a/include/net/netlink.h
>+++ b/include/net/netlink.h
>@@ -178,6 +178,7 @@ enum {
> 	NLA_S16,
> 	NLA_S32,
> 	NLA_S64,
>+	NLA_BITFIELD_32,
> 	__NLA_TYPE_MAX,
> };
> 
>@@ -206,6 +207,7 @@ enum {
>  *    NLA_MSECS            Leaving the length field zero will verify the
>  *                         given type fits, using it verifies minimum length
>  *                         just like "All other"
>+ *    NLA_BITFIELD_32      A 32-bit bitmap/bitselector attribute
>  *    All other            Minimum length of attribute payload
>  *
>  * Example:
>@@ -213,11 +215,13 @@ enum {
>  * 	[ATTR_FOO] = { .type = NLA_U16 },
>  *	[ATTR_BAR] = { .type = NLA_STRING, .len = BARSIZ },
>  *	[ATTR_BAZ] = { .len = sizeof(struct mystruct) },
>+ *	[ATTR_GOO] = { .type = NLA_BITFIELD_32, .validation_data = &myvalidflags },
>  * };
>  */
> struct nla_policy {
> 	u16		type;
> 	u16		len;
>+	void            *validation_data;
> };
> 
> /**
>diff --git a/include/uapi/linux/netlink.h b/include/uapi/linux/netlink.h
>index f86127a..0ac05a6 100644
>--- a/include/uapi/linux/netlink.h
>+++ b/include/uapi/linux/netlink.h
>@@ -226,5 +226,22 @@ struct nlattr {
> #define NLA_ALIGN(len)		(((len) + NLA_ALIGNTO - 1) & ~(NLA_ALIGNTO - 1))
> #define NLA_HDRLEN		((int) NLA_ALIGN(sizeof(struct nlattr)))
> 
>+/* Generic 32 bitflags attribute content sent to the kernel.
>+ *
>+ * The nla_value is a bitmap that defines the values being set
>+ * The nla_selector is a bitmask that defines which value is legit
>+ *
>+ * Examples:
>+ *  nla_value = 0x0, and nla_selector = 0x1
>+ *  implies we are selecting bit 1 and we want to set its value to 0.
>+ *
>+ *  nla_value = 0x2, and nla_selector = 0x2
>+ *  implies we are selecting bit 2 and we want to set its value to 1.
>+ *
>+ */
>+struct nla_bitfield_32 {
>+	__u32 nla_value;
>+	__u32 nla_selector;

I would just have "value" and "selector" here. "nla_" prefix indicates
netlink attrubute, like in struct nlattr - nla_type, nla_len,
and that is wrong indication. 



>+};
> 
> #endif /* _UAPI__LINUX_NETLINK_H */
>diff --git a/lib/nlattr.c b/lib/nlattr.c
>index fb52435..c8aad7e 100644
>--- a/lib/nlattr.c
>+++ b/lib/nlattr.c
>@@ -27,6 +27,20 @@
> 	[NLA_S64]	= sizeof(s64),
> };
> 
>+static int validate_nla_bitfield_32(const struct nlattr *nla, void *valid_data)

This should be:
static int validate_nla_bitfield_32(const struct nlattr *nla, u32 *valid_flags_allowed)


Other than this 2 nits, this looks good.



>+{
>+	const struct nla_bitfield_32 *nbf = nla_data(nla);
>+	u32 *valid_flags_mask = valid_data;
>+
>+	if (!valid_data)
>+		return -EINVAL;
>+
>+	if (nbf->nla_value & ~*valid_flags_mask)
>+		return -EINVAL;
>+
>+	return 0;
>+}
>+
> static int validate_nla(const struct nlattr *nla, int maxtype,
> 			const struct nla_policy *policy)
> {
>@@ -46,6 +60,13 @@ static int validate_nla(const struct nlattr *nla, int maxtype,
> 			return -ERANGE;
> 		break;
> 
>+	case NLA_BITFIELD_32:
>+		if (attrlen != sizeof(struct nla_bitfield_32))
>+			return -ERANGE;
>+
>+		return validate_nla_bitfield_32(nla, pt->validation_data);
>+		break;
>+
> 	case NLA_NUL_STRING:
> 		if (pt->len)
> 			minlen = min_t(int, attrlen, pt->len + 1);
>-- 
>1.9.1
>

  reply	other threads:[~2017-07-24 11:14 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-24  1:35 [PATCH net-next v11 0/4] net sched actions: improve dump performance Jamal Hadi Salim
2017-07-24  1:35 ` [PATCH net-next v11 1/4] net netlink: Add new type NLA_BITFIELD_32 Jamal Hadi Salim
2017-07-24 11:14   ` Jiri Pirko [this message]
2017-07-25 11:14     ` Jamal Hadi Salim
2017-07-24 11:18   ` Jiri Pirko
2017-07-25 11:15     ` Jamal Hadi Salim
2017-07-25 14:41   ` David Ahern
2017-07-28 13:51     ` Jamal Hadi Salim
2017-07-28 14:08       ` Jiri Pirko
2017-07-28 14:19       ` David Ahern
2017-07-28 14:55         ` Jiri Pirko
2017-07-28 15:04         ` Jamal Hadi Salim
2017-07-28 15:13           ` David Ahern
2017-07-28 21:55             ` Jamal Hadi Salim
2017-07-24  1:35 ` [PATCH net-next v11 2/4] net sched actions: Use proper root attribute table for actions Jamal Hadi Salim
2017-07-24  1:35 ` [PATCH net-next v11 3/4] net sched actions: dump more than TCA_ACT_MAX_PRIO actions per batch Jamal Hadi Salim
2017-07-24 11:27   ` Jiri Pirko
2017-07-25 11:22     ` Jamal Hadi Salim
2017-07-25 11:33       ` Jiri Pirko
2017-07-25 12:34         ` Jamal Hadi Salim
2017-07-25 12:37           ` Jiri Pirko
2017-07-28 13:41             ` Jamal Hadi Salim
2017-07-28 14:12               ` Jiri Pirko
2017-07-28 14:52                 ` Jamal Hadi Salim
2017-07-28 14:57                   ` Jiri Pirko
2017-07-28 15:08                   ` Jamal Hadi Salim
2017-07-28 15:45                     ` Jiri Pirko
2017-07-28 22:10                       ` Jamal Hadi Salim
2017-07-29  7:19                         ` Jiri Pirko
2017-07-29 11:21                           ` Jamal Hadi Salim
2017-07-24  1:35 ` [PATCH net-next v11 4/4] net sched actions: add time filter for action dumping Jamal Hadi Salim
2017-07-24 11:34   ` Jiri Pirko
2017-07-25 11:27     ` Jamal Hadi Salim
2017-07-25 11:34       ` Jiri Pirko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170724111452.GA1868@nanopsycho \
    --to=jiri@resnulli$(echo .)us \
    --cc=alex.aring@gmail$(echo .)com \
    --cc=davem@davemloft$(echo .)net \
    --cc=dsahern@gmail$(echo .)com \
    --cc=eric.dumazet@gmail$(echo .)com \
    --cc=jhs@mojatatu$(echo .)com \
    --cc=mrv@mojatatu$(echo .)com \
    --cc=netdev@vger$(echo .)kernel.org \
    --cc=simon.horman@netronome$(echo .)com \
    --cc=xiyou.wangcong@gmail$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox