From: Jiri Pirko <jiri@resnulli.us>
To: netdev@vger.kernel.org
Cc: davem@davemloft.net, jakub.kicinski@netronome.com,
andrew@lunn.ch, mlxsw@mellanox.com
Subject: [patch net-next v3 3/3] devlink: add format requirement for devlink object names
Date: Mon, 21 Oct 2019 16:26:13 +0200 [thread overview]
Message-ID: <20191021142613.26657-4-jiri@resnulli.us> (raw)
In-Reply-To: <20191021142613.26657-1-jiri@resnulli.us>
From: Jiri Pirko <jiri@mellanox.com>
Currently, the name format is not required by the code, however it is
required during patch review. All params added until now are in-lined
with the following format:
1) lowercase characters, digits and underscored are allowed
2) underscore is neither at the beginning nor at the end and
there is no more than one in a row.
Add checker to the code to require this format from drivers and warn if
they don't follow. This applies to params, resources, reporters,
traps, trap groups, dpipe tables and dpipe fields.
Signed-off-by: Jiri Pirko <jiri@mellanox.com>
---
v2->v3:
- loosen the checks to allow uppercase as well
- also check resources, reporters,
traps, trap groups, dpipe tables and dpipe fields.
v1->v2:
- removed empty line after comment
- added check for empty string
- converted i and len to size_t and put on a single line
- s/valid_name/name_valid/ to be aligned with the rest
---
net/core/devlink.c | 79 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 79 insertions(+)
diff --git a/net/core/devlink.c b/net/core/devlink.c
index 45b6a9a964f6..e7d714699579 100644
--- a/net/core/devlink.c
+++ b/net/core/devlink.c
@@ -20,6 +20,7 @@
#include <linux/workqueue.h>
#include <linux/u64_stats_sync.h>
#include <linux/timekeeping.h>
+#include <linux/ctype.h>
#include <rdma/ib_verbs.h>
#include <net/netlink.h>
#include <net/genetlink.h>
@@ -31,6 +32,30 @@
#define CREATE_TRACE_POINTS
#include <trace/events/devlink.h>
+static bool devlink_obj_name_valid(const char *name)
+{
+ size_t i, len = strlen(name);
+
+ if (!len)
+ return false;
+
+ /* Name can contain lowercase/uppercase characters or digits.
+ * Underscores are also allowed, but not at the beginning
+ * or end of the name and not more than one in a row.
+ */
+ for (i = 0; i < len; i++) {
+ if (isalnum(name[i]))
+ continue;
+ if (name[i] != '_')
+ return false;
+ if (i == 0 || i + 1 == len)
+ return false;
+ if (name[i - 1] == '_')
+ return false;
+ }
+ return true;
+}
+
static struct devlink_dpipe_field devlink_dpipe_fields_ethernet[] = {
{
.name = "destination_mac",
@@ -4782,6 +4807,9 @@ devlink_health_reporter_create(struct devlink *devlink,
goto unlock;
}
+ if (WARN_ON(!devlink_obj_name_valid(ops->name)))
+ return ERR_PTR(-EINVAL);
+
if (WARN_ON(auto_recover && !ops->recover) ||
WARN_ON(graceful_period && !ops->recover)) {
reporter = ERR_PTR(-EINVAL);
@@ -6702,6 +6730,36 @@ void devlink_sb_unregister(struct devlink *devlink, unsigned int sb_index)
}
EXPORT_SYMBOL_GPL(devlink_sb_unregister);
+static int
+devlink_dpipe_header_verify(struct devlink_dpipe_header *dpipe_header)
+{
+ int i;
+
+ if (WARN_ON(!devlink_obj_name_valid(dpipe_header->name)))
+ return -EINVAL;
+
+ for (i = 0; i < dpipe_header->fields_count; i++) {
+ const char *name = dpipe_header->fields[i].name;
+
+ if (WARN_ON(!devlink_obj_name_valid(name)))
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static int
+devlink_dpipe_headers_verify(struct devlink_dpipe_headers *dpipe_headers)
+{
+ int i, err;
+
+ for (i = 0; i < dpipe_headers->headers_count; i++) {
+ err = devlink_dpipe_header_verify(dpipe_headers->headers[i]);
+ if (err)
+ return err;
+ }
+ return 0;
+}
+
/**
* devlink_dpipe_headers_register - register dpipe headers
*
@@ -6713,6 +6771,11 @@ EXPORT_SYMBOL_GPL(devlink_sb_unregister);
int devlink_dpipe_headers_register(struct devlink *devlink,
struct devlink_dpipe_headers *dpipe_headers)
{
+ int err;
+
+ err = devlink_dpipe_headers_verify(dpipe_headers);
+ if (err)
+ return err;
mutex_lock(&devlink->lock);
devlink->dpipe_headers = dpipe_headers;
mutex_unlock(&devlink->lock);
@@ -6785,6 +6848,9 @@ int devlink_dpipe_table_register(struct devlink *devlink,
if (devlink_dpipe_table_find(&devlink->dpipe_table_list, table_name))
return -EEXIST;
+ if (WARN_ON(!devlink_obj_name_valid(table_name)))
+ return -EINVAL;
+
if (WARN_ON(!table_ops->size_get))
return -EINVAL;
@@ -6853,6 +6919,9 @@ int devlink_resource_register(struct devlink *devlink,
top_hierarchy = parent_resource_id == DEVLINK_RESOURCE_ID_PARENT_TOP;
+ if (WARN_ON(!devlink_obj_name_valid(resource_name)))
+ return -EINVAL;
+
mutex_lock(&devlink->lock);
resource = devlink_resource_find(devlink, NULL, resource_id);
if (resource) {
@@ -7044,6 +7113,10 @@ static int devlink_param_verify(const struct devlink_param *param)
{
if (!param || !param->name || !param->supported_cmodes)
return -EINVAL;
+
+ if (WARN_ON(!devlink_obj_name_valid(param->name)))
+ return -EINVAL;
+
if (param->generic)
return devlink_param_generic_verify(param);
else
@@ -7650,6 +7723,9 @@ static int devlink_trap_verify(const struct devlink_trap *trap)
if (!trap || !trap->name || !trap->group.name)
return -EINVAL;
+ if (WARN_ON(!devlink_obj_name_valid(trap->name)))
+ return -EINVAL;
+
if (trap->generic)
return devlink_trap_generic_verify(trap);
else
@@ -7686,6 +7762,9 @@ devlink_trap_group_driver_verify(const struct devlink_trap_group *group)
static int devlink_trap_group_verify(const struct devlink_trap_group *group)
{
+ if (WARN_ON(!devlink_obj_name_valid(group->name)))
+ return -EINVAL;
+
if (group->generic)
return devlink_trap_group_generic_verify(group);
else
--
2.21.0
next prev parent reply other threads:[~2019-10-21 14:26 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-10-21 14:26 [patch net-next v3 0/3] devlink: add format requirement for devlink object names Jiri Pirko
2019-10-21 14:26 ` [patch net-next v3 1/3] netdevsim: change name of fib rules resource Jiri Pirko
2019-10-21 14:26 ` [patch net-next v3 2/3] devlink: replace spaces in dpipe field names Jiri Pirko
2019-10-21 15:17 ` David Ahern
2019-10-21 14:26 ` Jiri Pirko [this message]
2019-10-21 15:20 ` [patch net-next v3 3/3] devlink: add format requirement for devlink object names David Ahern
2019-10-21 15:28 ` Andrew Lunn
2019-10-21 15:56 ` Jiri Pirko
2019-10-21 16:11 ` David Ahern
2019-10-22 5:59 ` Jiri Pirko
2019-10-23 22:16 ` David Ahern
2019-10-24 6:06 ` 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=20191021142613.26657-4-jiri@resnulli.us \
--to=jiri@resnulli.us \
--cc=andrew@lunn.ch \
--cc=davem@davemloft.net \
--cc=jakub.kicinski@netronome.com \
--cc=mlxsw@mellanox.com \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
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;
as well as URLs for NNTP newsgroup(s).