[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20191021142613.26657-4-jiri@resnulli.us>
Date: Mon, 21 Oct 2019 16:26:13 +0200
From: Jiri Pirko <jiri@...nulli.us>
To: netdev@...r.kernel.org
Cc: davem@...emloft.net, jakub.kicinski@...ronome.com, andrew@...n.ch,
mlxsw@...lanox.com
Subject: [patch net-next v3 3/3] devlink: add format requirement for devlink object names
From: Jiri Pirko <jiri@...lanox.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@...lanox.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
Powered by blists - more mailing lists