[PATCH net-next RFC v2 repost 1/3] devlink: Wrap trap related

From: Aya Levin
Date: Tue Sep 22 2020 - 07:35:42 EST


Bundle the trap related lists: trap_list, trap_group_list and
trap_policer_list in a dedicated struct. This will be handy in the
coming patches in the set introducing traps in devlink port context.
With trap_lists, code reuse is much simpler.

Signed-off-by: Aya Levin <ayal@xxxxxxxxxx>
---
Changelog:
v1->v2:
Patch 1: Encapsulate only the traps lists for future code reuse. Don't
try to reuse the traps ops.

include/net/devlink.h | 10 +++--
net/core/devlink.c | 109 ++++++++++++++++++++++++++------------------------
2 files changed, 63 insertions(+), 56 deletions(-)

diff --git a/include/net/devlink.h b/include/net/devlink.h
index eaec0a8cc5ef..f11e09097e44 100644
--- a/include/net/devlink.h
+++ b/include/net/devlink.h
@@ -22,6 +22,12 @@

struct devlink_ops;

+struct devlink_trap_lists {
+ struct list_head trap_list;
+ struct list_head trap_group_list;
+ struct list_head trap_policer_list;
+};
+
struct devlink {
struct list_head list;
struct list_head port_list;
@@ -33,9 +39,7 @@ struct devlink {
struct list_head reporter_list;
struct mutex reporters_lock; /* protects reporter_list */
struct devlink_dpipe_headers *dpipe_headers;
- struct list_head trap_list;
- struct list_head trap_group_list;
- struct list_head trap_policer_list;
+ struct devlink_trap_lists trap_lists;
const struct devlink_ops *ops;
struct xarray snapshot_ids;
struct device *dev;
diff --git a/net/core/devlink.c b/net/core/devlink.c
index 19037f114307..fde6f2c5c409 100644
--- a/net/core/devlink.c
+++ b/net/core/devlink.c
@@ -6158,11 +6158,11 @@ struct devlink_trap_item {
};

static struct devlink_trap_policer_item *
-devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
+devlink_trap_policer_item_lookup(struct devlink_trap_lists *trap_lists, u32 id)
{
struct devlink_trap_policer_item *policer_item;

- list_for_each_entry(policer_item, &devlink->trap_policer_list, list) {
+ list_for_each_entry(policer_item, &trap_lists->trap_policer_list, list) {
if (policer_item->policer->id == id)
return policer_item;
}
@@ -6171,11 +6171,11 @@ devlink_trap_policer_item_lookup(struct devlink *devlink, u32 id)
}

static struct devlink_trap_item *
-devlink_trap_item_lookup(struct devlink *devlink, const char *name)
+devlink_trap_item_lookup(struct devlink_trap_lists *trap_lists, const char *name)
{
struct devlink_trap_item *trap_item;

- list_for_each_entry(trap_item, &devlink->trap_list, list) {
+ list_for_each_entry(trap_item, &trap_lists->trap_list, list) {
if (!strcmp(trap_item->trap->name, name))
return trap_item;
}
@@ -6184,7 +6184,7 @@ devlink_trap_item_lookup(struct devlink *devlink, const char *name)
}

static struct devlink_trap_item *
-devlink_trap_item_get_from_info(struct devlink *devlink,
+devlink_trap_item_get_from_info(struct devlink_trap_lists *trap_lists,
struct genl_info *info)
{
struct nlattr *attr;
@@ -6193,7 +6193,7 @@ devlink_trap_item_get_from_info(struct devlink *devlink,
return NULL;
attr = info->attrs[DEVLINK_ATTR_TRAP_NAME];

- return devlink_trap_item_lookup(devlink, nla_data(attr));
+ return devlink_trap_item_lookup(trap_lists, nla_data(attr));
}

static int
@@ -6352,10 +6352,10 @@ static int devlink_nl_cmd_trap_get_doit(struct sk_buff *skb,
struct sk_buff *msg;
int err;

- if (list_empty(&devlink->trap_list))
+ if (list_empty(&devlink->trap_lists.trap_list))
return -EOPNOTSUPP;

- trap_item = devlink_trap_item_get_from_info(devlink, info);
+ trap_item = devlink_trap_item_get_from_info(&devlink->trap_lists, info);
if (!trap_item) {
NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
return -ENOENT;
@@ -6392,7 +6392,7 @@ static int devlink_nl_cmd_trap_get_dumpit(struct sk_buff *msg,
if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
continue;
mutex_lock(&devlink->lock);
- list_for_each_entry(trap_item, &devlink->trap_list, list) {
+ list_for_each_entry(trap_item, &devlink->trap_lists.trap_list, list) {
if (idx < start) {
idx++;
continue;
@@ -6468,10 +6468,10 @@ static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
struct devlink_trap_item *trap_item;
int err;

- if (list_empty(&devlink->trap_list))
+ if (list_empty(&devlink->trap_lists.trap_list))
return -EOPNOTSUPP;

- trap_item = devlink_trap_item_get_from_info(devlink, info);
+ trap_item = devlink_trap_item_get_from_info(&devlink->trap_lists, info);
if (!trap_item) {
NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap");
return -ENOENT;
@@ -6485,11 +6485,11 @@ static int devlink_nl_cmd_trap_set_doit(struct sk_buff *skb,
}

static struct devlink_trap_group_item *
-devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
+devlink_trap_group_item_lookup(struct devlink_trap_lists *trap_lists, const char *name)
{
struct devlink_trap_group_item *group_item;

- list_for_each_entry(group_item, &devlink->trap_group_list, list) {
+ list_for_each_entry(group_item, &trap_lists->trap_group_list, list) {
if (!strcmp(group_item->group->name, name))
return group_item;
}
@@ -6498,11 +6498,11 @@ devlink_trap_group_item_lookup(struct devlink *devlink, const char *name)
}

static struct devlink_trap_group_item *
-devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
+devlink_trap_group_item_lookup_by_id(struct devlink_trap_lists *trap_lists, u16 id)
{
struct devlink_trap_group_item *group_item;

- list_for_each_entry(group_item, &devlink->trap_group_list, list) {
+ list_for_each_entry(group_item, &trap_lists->trap_group_list, list) {
if (group_item->group->id == id)
return group_item;
}
@@ -6511,7 +6511,7 @@ devlink_trap_group_item_lookup_by_id(struct devlink *devlink, u16 id)
}

static struct devlink_trap_group_item *
-devlink_trap_group_item_get_from_info(struct devlink *devlink,
+devlink_trap_group_item_get_from_info(struct devlink_trap_lists *trap_lists,
struct genl_info *info)
{
char *name;
@@ -6520,7 +6520,7 @@ devlink_trap_group_item_get_from_info(struct devlink *devlink,
return NULL;
name = nla_data(info->attrs[DEVLINK_ATTR_TRAP_GROUP_NAME]);

- return devlink_trap_group_item_lookup(devlink, name);
+ return devlink_trap_group_item_lookup(trap_lists, name);
}

static int
@@ -6574,10 +6574,10 @@ static int devlink_nl_cmd_trap_group_get_doit(struct sk_buff *skb,
struct sk_buff *msg;
int err;

- if (list_empty(&devlink->trap_group_list))
+ if (list_empty(&devlink->trap_lists.trap_group_list))
return -EOPNOTSUPP;

- group_item = devlink_trap_group_item_get_from_info(devlink, info);
+ group_item = devlink_trap_group_item_get_from_info(&devlink->trap_lists, info);
if (!group_item) {
NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
return -ENOENT;
@@ -6616,7 +6616,7 @@ static int devlink_nl_cmd_trap_group_get_dumpit(struct sk_buff *msg,
if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
continue;
mutex_lock(&devlink->lock);
- list_for_each_entry(group_item, &devlink->trap_group_list,
+ list_for_each_entry(group_item, &devlink->trap_lists.trap_group_list,
list) {
if (idx < start) {
idx++;
@@ -6652,7 +6652,7 @@ __devlink_trap_group_action_set(struct devlink *devlink,
struct devlink_trap_item *trap_item;
int err;

- list_for_each_entry(trap_item, &devlink->trap_list, list) {
+ list_for_each_entry(trap_item, &devlink->trap_lists.trap_list, list) {
if (strcmp(trap_item->group_item->group->name, group_name))
continue;
err = __devlink_trap_action_set(devlink, trap_item,
@@ -6712,7 +6712,7 @@ static int devlink_trap_group_set(struct devlink *devlink,
u32 policer_id;

policer_id = nla_get_u32(attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);
- policer_item = devlink_trap_policer_item_lookup(devlink,
+ policer_item = devlink_trap_policer_item_lookup(&devlink->trap_lists,
policer_id);
if (policer_id && !policer_item) {
NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
@@ -6740,10 +6740,10 @@ static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
bool modified = false;
int err;

- if (list_empty(&devlink->trap_group_list))
+ if (list_empty(&devlink->trap_lists.trap_group_list))
return -EOPNOTSUPP;

- group_item = devlink_trap_group_item_get_from_info(devlink, info);
+ group_item = devlink_trap_group_item_get_from_info(&devlink->trap_lists, info);
if (!group_item) {
NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap group");
return -ENOENT;
@@ -6767,7 +6767,7 @@ static int devlink_nl_cmd_trap_group_set_doit(struct sk_buff *skb,
}

static struct devlink_trap_policer_item *
-devlink_trap_policer_item_get_from_info(struct devlink *devlink,
+devlink_trap_policer_item_get_from_info(struct devlink_trap_lists *trap_lists,
struct genl_info *info)
{
u32 id;
@@ -6776,7 +6776,7 @@ devlink_trap_policer_item_get_from_info(struct devlink *devlink,
return NULL;
id = nla_get_u32(info->attrs[DEVLINK_ATTR_TRAP_POLICER_ID]);

- return devlink_trap_policer_item_lookup(devlink, id);
+ return devlink_trap_policer_item_lookup(trap_lists, id);
}

static int
@@ -6862,10 +6862,10 @@ static int devlink_nl_cmd_trap_policer_get_doit(struct sk_buff *skb,
struct sk_buff *msg;
int err;

- if (list_empty(&devlink->trap_policer_list))
+ if (list_empty(&devlink->trap_lists.trap_policer_list))
return -EOPNOTSUPP;

- policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
+ policer_item = devlink_trap_policer_item_get_from_info(&devlink->trap_lists, info);
if (!policer_item) {
NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
return -ENOENT;
@@ -6904,7 +6904,7 @@ static int devlink_nl_cmd_trap_policer_get_dumpit(struct sk_buff *msg,
if (!net_eq(devlink_net(devlink), sock_net(msg->sk)))
continue;
mutex_lock(&devlink->lock);
- list_for_each_entry(policer_item, &devlink->trap_policer_list,
+ list_for_each_entry(policer_item, &devlink->trap_lists.trap_policer_list,
list) {
if (idx < start) {
idx++;
@@ -6987,13 +6987,13 @@ static int devlink_nl_cmd_trap_policer_set_doit(struct sk_buff *skb,
struct netlink_ext_ack *extack = info->extack;
struct devlink *devlink = info->user_ptr[0];

- if (list_empty(&devlink->trap_policer_list))
+ if (list_empty(&devlink->trap_lists.trap_policer_list))
return -EOPNOTSUPP;

if (!devlink->ops->trap_policer_set)
return -EOPNOTSUPP;

- policer_item = devlink_trap_policer_item_get_from_info(devlink, info);
+ policer_item = devlink_trap_policer_item_get_from_info(&devlink->trap_lists, info);
if (!policer_item) {
NL_SET_ERR_MSG_MOD(extack, "Device did not register this trap policer");
return -ENOENT;
@@ -7401,9 +7401,9 @@ struct devlink *devlink_alloc(const struct devlink_ops *ops, size_t priv_size)
INIT_LIST_HEAD(&devlink->param_list);
INIT_LIST_HEAD(&devlink->region_list);
INIT_LIST_HEAD(&devlink->reporter_list);
- INIT_LIST_HEAD(&devlink->trap_list);
- INIT_LIST_HEAD(&devlink->trap_group_list);
- INIT_LIST_HEAD(&devlink->trap_policer_list);
+ INIT_LIST_HEAD(&devlink->trap_lists.trap_list);
+ INIT_LIST_HEAD(&devlink->trap_lists.trap_group_list);
+ INIT_LIST_HEAD(&devlink->trap_lists.trap_policer_list);
mutex_init(&devlink->lock);
mutex_init(&devlink->reporters_lock);
return devlink;
@@ -7488,9 +7488,9 @@ void devlink_free(struct devlink *devlink)
{
mutex_destroy(&devlink->reporters_lock);
mutex_destroy(&devlink->lock);
- WARN_ON(!list_empty(&devlink->trap_policer_list));
- WARN_ON(!list_empty(&devlink->trap_group_list));
- WARN_ON(!list_empty(&devlink->trap_list));
+ WARN_ON(!list_empty(&devlink->trap_lists.trap_policer_list));
+ WARN_ON(!list_empty(&devlink->trap_lists.trap_group_list));
+ WARN_ON(!list_empty(&devlink->trap_lists.trap_list));
WARN_ON(!list_empty(&devlink->reporter_list));
WARN_ON(!list_empty(&devlink->region_list));
WARN_ON(!list_empty(&devlink->param_list));
@@ -8984,13 +8984,13 @@ devlink_trap_group_notify(struct devlink *devlink,
}

static int
-devlink_trap_item_group_link(struct devlink *devlink,
+devlink_trap_item_group_link(struct devlink_trap_lists *trap_lists,
struct devlink_trap_item *trap_item)
{
u16 group_id = trap_item->trap->init_group_id;
struct devlink_trap_group_item *group_item;

- group_item = devlink_trap_group_item_lookup_by_id(devlink, group_id);
+ group_item = devlink_trap_group_item_lookup_by_id(trap_lists, group_id);
if (WARN_ON_ONCE(!group_item))
return -EINVAL;

@@ -9027,10 +9027,11 @@ static int
devlink_trap_register(struct devlink *devlink,
const struct devlink_trap *trap, void *priv)
{
+ struct devlink_trap_lists *trap_lists = &devlink->trap_lists;
struct devlink_trap_item *trap_item;
int err;

- if (devlink_trap_item_lookup(devlink, trap->name))
+ if (devlink_trap_item_lookup(trap_lists, trap->name))
return -EEXIST;

trap_item = kzalloc(sizeof(*trap_item), GFP_KERNEL);
@@ -9047,7 +9048,7 @@ devlink_trap_register(struct devlink *devlink,
trap_item->action = trap->init_action;
trap_item->priv = priv;

- err = devlink_trap_item_group_link(devlink, trap_item);
+ err = devlink_trap_item_group_link(trap_lists, trap_item);
if (err)
goto err_group_link;

@@ -9055,7 +9056,7 @@ devlink_trap_register(struct devlink *devlink,
if (err)
goto err_trap_init;

- list_add_tail(&trap_item->list, &devlink->trap_list);
+ list_add_tail(&trap_item->list, &trap_lists->trap_list);
devlink_trap_notify(devlink, trap_item, DEVLINK_CMD_TRAP_NEW);

return 0;
@@ -9073,7 +9074,7 @@ static void devlink_trap_unregister(struct devlink *devlink,
{
struct devlink_trap_item *trap_item;

- trap_item = devlink_trap_item_lookup(devlink, trap->name);
+ trap_item = devlink_trap_item_lookup(&devlink->trap_lists, trap->name);
if (WARN_ON_ONCE(!trap_item))
return;

@@ -9090,7 +9091,7 @@ static void devlink_trap_disable(struct devlink *devlink,
{
struct devlink_trap_item *trap_item;

- trap_item = devlink_trap_item_lookup(devlink, trap->name);
+ trap_item = devlink_trap_item_lookup(&devlink->trap_lists, trap->name);
if (WARN_ON_ONCE(!trap_item))
return;

@@ -9245,7 +9246,7 @@ void *devlink_trap_ctx_priv(void *trap_ctx)
EXPORT_SYMBOL_GPL(devlink_trap_ctx_priv);

static int
-devlink_trap_group_item_policer_link(struct devlink *devlink,
+devlink_trap_group_item_policer_link(struct devlink_trap_lists *trap_lists,
struct devlink_trap_group_item *group_item)
{
u32 policer_id = group_item->group->init_policer_id;
@@ -9254,7 +9255,7 @@ devlink_trap_group_item_policer_link(struct devlink *devlink,
if (policer_id == 0)
return 0;

- policer_item = devlink_trap_policer_item_lookup(devlink, policer_id);
+ policer_item = devlink_trap_policer_item_lookup(trap_lists, policer_id);
if (WARN_ON_ONCE(!policer_item))
return -EINVAL;

@@ -9267,10 +9268,11 @@ static int
devlink_trap_group_register(struct devlink *devlink,
const struct devlink_trap_group *group)
{
+ struct devlink_trap_lists *trap_lists = &devlink->trap_lists;
struct devlink_trap_group_item *group_item;
int err;

- if (devlink_trap_group_item_lookup(devlink, group->name))
+ if (devlink_trap_group_item_lookup(trap_lists, group->name))
return -EEXIST;

group_item = kzalloc(sizeof(*group_item), GFP_KERNEL);
@@ -9285,7 +9287,7 @@ devlink_trap_group_register(struct devlink *devlink,

group_item->group = group;

- err = devlink_trap_group_item_policer_link(devlink, group_item);
+ err = devlink_trap_group_item_policer_link(trap_lists, group_item);
if (err)
goto err_policer_link;

@@ -9295,7 +9297,7 @@ devlink_trap_group_register(struct devlink *devlink,
goto err_group_init;
}

- list_add_tail(&group_item->list, &devlink->trap_group_list);
+ list_add_tail(&group_item->list, &trap_lists->trap_group_list);
devlink_trap_group_notify(devlink, group_item,
DEVLINK_CMD_TRAP_GROUP_NEW);

@@ -9315,7 +9317,7 @@ devlink_trap_group_unregister(struct devlink *devlink,
{
struct devlink_trap_group_item *group_item;

- group_item = devlink_trap_group_item_lookup(devlink, group->name);
+ group_item = devlink_trap_group_item_lookup(&devlink->trap_lists, group->name);
if (WARN_ON_ONCE(!group_item))
return;

@@ -9414,10 +9416,11 @@ static int
devlink_trap_policer_register(struct devlink *devlink,
const struct devlink_trap_policer *policer)
{
+ struct devlink_trap_lists *trap_lists = &devlink->trap_lists;
struct devlink_trap_policer_item *policer_item;
int err;

- if (devlink_trap_policer_item_lookup(devlink, policer->id))
+ if (devlink_trap_policer_item_lookup(trap_lists, policer->id))
return -EEXIST;

policer_item = kzalloc(sizeof(*policer_item), GFP_KERNEL);
@@ -9434,7 +9437,7 @@ devlink_trap_policer_register(struct devlink *devlink,
goto err_policer_init;
}

- list_add_tail(&policer_item->list, &devlink->trap_policer_list);
+ list_add_tail(&policer_item->list, &trap_lists->trap_policer_list);
devlink_trap_policer_notify(devlink, policer_item,
DEVLINK_CMD_TRAP_POLICER_NEW);

@@ -9451,7 +9454,7 @@ devlink_trap_policer_unregister(struct devlink *devlink,
{
struct devlink_trap_policer_item *policer_item;

- policer_item = devlink_trap_policer_item_lookup(devlink, policer->id);
+ policer_item = devlink_trap_policer_item_lookup(&devlink->trap_lists, policer->id);
if (WARN_ON_ONCE(!policer_item))
return;

--
2.14.1