devlink: Move devlink health get and set code to health file
authorMoshe Shemesh <moshe@nvidia.com>
Tue, 14 Feb 2023 16:37:59 +0000 (18:37 +0200)
committerJakub Kicinski <kuba@kernel.org>
Thu, 16 Feb 2023 03:15:44 +0000 (19:15 -0800)
Move devlink health get and set callbacks and related code from
leftover.c to health.c. No functional change in this patch.

Signed-off-by: Moshe Shemesh <moshe@nvidia.com>
Reviewed-by: Jiri Pirko <jiri@nvidia.com>
Reviewed-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
net/devlink/devl_internal.h
net/devlink/health.c
net/devlink/leftover.c

index 49fe9e2..085f80b 100644 (file)
@@ -176,6 +176,8 @@ int devlink_port_netdevice_event(struct notifier_block *nb,
 
 struct devlink_port *
 devlink_port_get_from_info(struct devlink *devlink, struct genl_info *info);
+struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
+                                                struct nlattr **attrs);
 
 /* Reload */
 bool devlink_reload_actions_valid(const struct devlink_ops *ops);
@@ -224,6 +226,18 @@ devlink_health_reporter_find_by_name(struct devlink *devlink,
 struct devlink_health_reporter *
 devlink_port_health_reporter_find_by_name(struct devlink_port *devlink_port,
                                          const char *reporter_name);
+struct devlink_health_reporter *
+devlink_health_reporter_get_from_attrs(struct devlink *devlink,
+                                      struct nlattr **attrs);
+struct devlink_health_reporter *
+devlink_health_reporter_get_from_info(struct devlink *devlink,
+                                     struct genl_info *info);
+int
+devlink_nl_health_reporter_fill(struct sk_buff *msg,
+                               struct devlink_health_reporter *reporter,
+                               enum devlink_command cmd, u32 portid,
+                               u32 seq, int flags);
+
 void devlink_fmsg_free(struct devlink_fmsg *fmsg);
 
 /* Line cards */
@@ -249,3 +263,7 @@ int devlink_nl_cmd_info_get_doit(struct sk_buff *skb, struct genl_info *info);
 int devlink_nl_cmd_flash_update(struct sk_buff *skb, struct genl_info *info);
 int devlink_nl_cmd_selftests_get_doit(struct sk_buff *skb, struct genl_info *info);
 int devlink_nl_cmd_selftests_run(struct sk_buff *skb, struct genl_info *info);
+int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
+                                           struct genl_info *info);
+int devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
+                                           struct genl_info *info);
index 18d1f38..1c92f36 100644 (file)
@@ -194,3 +194,217 @@ devlink_health_reporter_destroy(struct devlink_health_reporter *reporter)
        devl_unlock(devlink);
 }
 EXPORT_SYMBOL_GPL(devlink_health_reporter_destroy);
+
+int
+devlink_nl_health_reporter_fill(struct sk_buff *msg,
+                               struct devlink_health_reporter *reporter,
+                               enum devlink_command cmd, u32 portid,
+                               u32 seq, int flags)
+{
+       struct devlink *devlink = reporter->devlink;
+       struct nlattr *reporter_attr;
+       void *hdr;
+
+       hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
+       if (!hdr)
+               return -EMSGSIZE;
+
+       if (devlink_nl_put_handle(msg, devlink))
+               goto genlmsg_cancel;
+
+       if (reporter->devlink_port) {
+               if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
+                       goto genlmsg_cancel;
+       }
+       reporter_attr = nla_nest_start_noflag(msg,
+                                             DEVLINK_ATTR_HEALTH_REPORTER);
+       if (!reporter_attr)
+               goto genlmsg_cancel;
+       if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
+                          reporter->ops->name))
+               goto reporter_nest_cancel;
+       if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
+                      reporter->health_state))
+               goto reporter_nest_cancel;
+       if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
+                             reporter->error_count, DEVLINK_ATTR_PAD))
+               goto reporter_nest_cancel;
+       if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
+                             reporter->recovery_count, DEVLINK_ATTR_PAD))
+               goto reporter_nest_cancel;
+       if (reporter->ops->recover &&
+           nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
+                             reporter->graceful_period,
+                             DEVLINK_ATTR_PAD))
+               goto reporter_nest_cancel;
+       if (reporter->ops->recover &&
+           nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
+                      reporter->auto_recover))
+               goto reporter_nest_cancel;
+       if (reporter->dump_fmsg &&
+           nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
+                             jiffies_to_msecs(reporter->dump_ts),
+                             DEVLINK_ATTR_PAD))
+               goto reporter_nest_cancel;
+       if (reporter->dump_fmsg &&
+           nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
+                             reporter->dump_real_ts, DEVLINK_ATTR_PAD))
+               goto reporter_nest_cancel;
+       if (reporter->ops->dump &&
+           nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
+                      reporter->auto_dump))
+               goto reporter_nest_cancel;
+
+       nla_nest_end(msg, reporter_attr);
+       genlmsg_end(msg, hdr);
+       return 0;
+
+reporter_nest_cancel:
+       nla_nest_cancel(msg, reporter_attr);
+genlmsg_cancel:
+       genlmsg_cancel(msg, hdr);
+       return -EMSGSIZE;
+}
+
+struct devlink_health_reporter *
+devlink_health_reporter_get_from_attrs(struct devlink *devlink,
+                                      struct nlattr **attrs)
+{
+       struct devlink_port *devlink_port;
+       char *reporter_name;
+
+       if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
+               return NULL;
+
+       reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
+       devlink_port = devlink_port_get_from_attrs(devlink, attrs);
+       if (IS_ERR(devlink_port))
+               return devlink_health_reporter_find_by_name(devlink,
+                                                           reporter_name);
+       else
+               return devlink_port_health_reporter_find_by_name(devlink_port,
+                                                                reporter_name);
+}
+
+struct devlink_health_reporter *
+devlink_health_reporter_get_from_info(struct devlink *devlink,
+                                     struct genl_info *info)
+{
+       return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
+}
+
+int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
+                                           struct genl_info *info)
+{
+       struct devlink *devlink = info->user_ptr[0];
+       struct devlink_health_reporter *reporter;
+       struct sk_buff *msg;
+       int err;
+
+       reporter = devlink_health_reporter_get_from_info(devlink, info);
+       if (!reporter)
+               return -EINVAL;
+
+       msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
+       if (!msg)
+               return -ENOMEM;
+
+       err = devlink_nl_health_reporter_fill(msg, reporter,
+                                             DEVLINK_CMD_HEALTH_REPORTER_GET,
+                                             info->snd_portid, info->snd_seq,
+                                             0);
+       if (err) {
+               nlmsg_free(msg);
+               return err;
+       }
+
+       return genlmsg_reply(msg, info);
+}
+
+static int
+devlink_nl_cmd_health_reporter_get_dump_one(struct sk_buff *msg,
+                                           struct devlink *devlink,
+                                           struct netlink_callback *cb)
+{
+       struct devlink_nl_dump_state *state = devlink_dump_state(cb);
+       struct devlink_health_reporter *reporter;
+       struct devlink_port *port;
+       unsigned long port_index;
+       int idx = 0;
+       int err;
+
+       list_for_each_entry(reporter, &devlink->reporter_list, list) {
+               if (idx < state->idx) {
+                       idx++;
+                       continue;
+               }
+               err = devlink_nl_health_reporter_fill(msg, reporter,
+                                                     DEVLINK_CMD_HEALTH_REPORTER_GET,
+                                                     NETLINK_CB(cb->skb).portid,
+                                                     cb->nlh->nlmsg_seq,
+                                                     NLM_F_MULTI);
+               if (err) {
+                       state->idx = idx;
+                       return err;
+               }
+               idx++;
+       }
+       xa_for_each(&devlink->ports, port_index, port) {
+               list_for_each_entry(reporter, &port->reporter_list, list) {
+                       if (idx < state->idx) {
+                               idx++;
+                               continue;
+                       }
+                       err = devlink_nl_health_reporter_fill(msg, reporter,
+                                                             DEVLINK_CMD_HEALTH_REPORTER_GET,
+                                                             NETLINK_CB(cb->skb).portid,
+                                                             cb->nlh->nlmsg_seq,
+                                                             NLM_F_MULTI);
+                       if (err) {
+                               state->idx = idx;
+                               return err;
+                       }
+                       idx++;
+               }
+       }
+
+       return 0;
+}
+
+const struct devlink_cmd devl_cmd_health_reporter_get = {
+       .dump_one               = devlink_nl_cmd_health_reporter_get_dump_one,
+};
+
+int devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
+                                           struct genl_info *info)
+{
+       struct devlink *devlink = info->user_ptr[0];
+       struct devlink_health_reporter *reporter;
+
+       reporter = devlink_health_reporter_get_from_info(devlink, info);
+       if (!reporter)
+               return -EINVAL;
+
+       if (!reporter->ops->recover &&
+           (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
+            info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]))
+               return -EOPNOTSUPP;
+
+       if (!reporter->ops->dump &&
+           info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
+               return -EOPNOTSUPP;
+
+       if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
+               reporter->graceful_period =
+                       nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
+
+       if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
+               reporter->auto_recover =
+                       nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
+
+       if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
+               reporter->auto_dump =
+               nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
+
+       return 0;
+}
index 90f95f0..0b1c5e0 100644 (file)
@@ -156,8 +156,8 @@ static struct devlink_port *devlink_port_get_by_index(struct devlink *devlink,
        return xa_load(&devlink->ports, port_index);
 }
 
-static struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
-                                                       struct nlattr **attrs)
+struct devlink_port *devlink_port_get_from_attrs(struct devlink *devlink,
+                                                struct nlattr **attrs)
 {
        if (attrs[DEVLINK_ATTR_PORT_INDEX]) {
                u32 port_index = nla_get_u32(attrs[DEVLINK_ATTR_PORT_INDEX]);
@@ -5963,77 +5963,6 @@ nla_put_failure:
        return err;
 }
 
-static int
-devlink_nl_health_reporter_fill(struct sk_buff *msg,
-                               struct devlink_health_reporter *reporter,
-                               enum devlink_command cmd, u32 portid,
-                               u32 seq, int flags)
-{
-       struct devlink *devlink = reporter->devlink;
-       struct nlattr *reporter_attr;
-       void *hdr;
-
-       hdr = genlmsg_put(msg, portid, seq, &devlink_nl_family, flags, cmd);
-       if (!hdr)
-               return -EMSGSIZE;
-
-       if (devlink_nl_put_handle(msg, devlink))
-               goto genlmsg_cancel;
-
-       if (reporter->devlink_port) {
-               if (nla_put_u32(msg, DEVLINK_ATTR_PORT_INDEX, reporter->devlink_port->index))
-                       goto genlmsg_cancel;
-       }
-       reporter_attr = nla_nest_start_noflag(msg,
-                                             DEVLINK_ATTR_HEALTH_REPORTER);
-       if (!reporter_attr)
-               goto genlmsg_cancel;
-       if (nla_put_string(msg, DEVLINK_ATTR_HEALTH_REPORTER_NAME,
-                          reporter->ops->name))
-               goto reporter_nest_cancel;
-       if (nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_STATE,
-                      reporter->health_state))
-               goto reporter_nest_cancel;
-       if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_ERR_COUNT,
-                             reporter->error_count, DEVLINK_ATTR_PAD))
-               goto reporter_nest_cancel;
-       if (nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_RECOVER_COUNT,
-                             reporter->recovery_count, DEVLINK_ATTR_PAD))
-               goto reporter_nest_cancel;
-       if (reporter->ops->recover &&
-           nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD,
-                             reporter->graceful_period,
-                             DEVLINK_ATTR_PAD))
-               goto reporter_nest_cancel;
-       if (reporter->ops->recover &&
-           nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER,
-                      reporter->auto_recover))
-               goto reporter_nest_cancel;
-       if (reporter->dump_fmsg &&
-           nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS,
-                             jiffies_to_msecs(reporter->dump_ts),
-                             DEVLINK_ATTR_PAD))
-               goto reporter_nest_cancel;
-       if (reporter->dump_fmsg &&
-           nla_put_u64_64bit(msg, DEVLINK_ATTR_HEALTH_REPORTER_DUMP_TS_NS,
-                             reporter->dump_real_ts, DEVLINK_ATTR_PAD))
-               goto reporter_nest_cancel;
-       if (reporter->ops->dump &&
-           nla_put_u8(msg, DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP,
-                      reporter->auto_dump))
-               goto reporter_nest_cancel;
-
-       nla_nest_end(msg, reporter_attr);
-       genlmsg_end(msg, hdr);
-       return 0;
-
-reporter_nest_cancel:
-       nla_nest_cancel(msg, reporter_attr);
-genlmsg_cancel:
-       genlmsg_cancel(msg, hdr);
-       return -EMSGSIZE;
-}
-
 static void devlink_recover_notify(struct devlink_health_reporter *reporter,
                                   enum devlink_command cmd)
 {
@@ -6189,33 +6118,6 @@ int devlink_health_report(struct devlink_health_reporter *reporter,
 EXPORT_SYMBOL_GPL(devlink_health_report);
 
 static struct devlink_health_reporter *
-devlink_health_reporter_get_from_attrs(struct devlink *devlink,
-                                      struct nlattr **attrs)
-{
-       struct devlink_port *devlink_port;
-       char *reporter_name;
-
-       if (!attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME])
-               return NULL;
-
-       reporter_name = nla_data(attrs[DEVLINK_ATTR_HEALTH_REPORTER_NAME]);
-       devlink_port = devlink_port_get_from_attrs(devlink, attrs);
-       if (IS_ERR(devlink_port))
-               return devlink_health_reporter_find_by_name(devlink,
-                                                           reporter_name);
-       else
-               return devlink_port_health_reporter_find_by_name(devlink_port,
-                                                                reporter_name);
-}
-
-static struct devlink_health_reporter *
-devlink_health_reporter_get_from_info(struct devlink *devlink,
-                                     struct genl_info *info)
-{
-       return devlink_health_reporter_get_from_attrs(devlink, info->attrs);
-}
-
-static struct devlink_health_reporter *
 devlink_health_reporter_get_from_cb(struct netlink_callback *cb)
 {
        const struct genl_dumpit_info *info = genl_dumpit_info(cb);
@@ -6251,123 +6153,6 @@ devlink_health_reporter_state_update(struct devlink_health_reporter *reporter,
 }
 EXPORT_SYMBOL_GPL(devlink_health_reporter_state_update);
 
-static int devlink_nl_cmd_health_reporter_get_doit(struct sk_buff *skb,
-                                                  struct genl_info *info)
-{
-       struct devlink *devlink = info->user_ptr[0];
-       struct devlink_health_reporter *reporter;
-       struct sk_buff *msg;
-       int err;
-
-       reporter = devlink_health_reporter_get_from_info(devlink, info);
-       if (!reporter)
-               return -EINVAL;
-
-       msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
-       if (!msg)
-               return -ENOMEM;
-
-       err = devlink_nl_health_reporter_fill(msg, reporter,
-                                             DEVLINK_CMD_HEALTH_REPORTER_GET,
-                                             info->snd_portid, info->snd_seq,
-                                             0);
-       if (err) {
-               nlmsg_free(msg);
-               return err;
-       }
-
-       return genlmsg_reply(msg, info);
-}
-
-static int
-devlink_nl_cmd_health_reporter_get_dump_one(struct sk_buff *msg,
-                                           struct devlink *devlink,
-                                           struct netlink_callback *cb)
-{
-       struct devlink_nl_dump_state *state = devlink_dump_state(cb);
-       struct devlink_health_reporter *reporter;
-       struct devlink_port *port;
-       unsigned long port_index;
-       int idx = 0;
-       int err;
-
-       list_for_each_entry(reporter, &devlink->reporter_list, list) {
-               if (idx < state->idx) {
-                       idx++;
-                       continue;
-               }
-               err = devlink_nl_health_reporter_fill(msg, reporter,
-                                                     DEVLINK_CMD_HEALTH_REPORTER_GET,
-                                                     NETLINK_CB(cb->skb).portid,
-                                                     cb->nlh->nlmsg_seq,
-                                                     NLM_F_MULTI);
-               if (err) {
-                       state->idx = idx;
-                       return err;
-               }
-               idx++;
-       }
-       xa_for_each(&devlink->ports, port_index, port) {
-               list_for_each_entry(reporter, &port->reporter_list, list) {
-                       if (idx < state->idx) {
-                               idx++;
-                               continue;
-                       }
-                       err = devlink_nl_health_reporter_fill(msg, reporter,
-                                                             DEVLINK_CMD_HEALTH_REPORTER_GET,
-                                                             NETLINK_CB(cb->skb).portid,
-                                                             cb->nlh->nlmsg_seq,
-                                                             NLM_F_MULTI);
-                       if (err) {
-                               state->idx = idx;
-                               return err;
-                       }
-                       idx++;
-               }
-       }
-
-       return 0;
-}
-
-const struct devlink_cmd devl_cmd_health_reporter_get = {
-       .dump_one               = devlink_nl_cmd_health_reporter_get_dump_one,
-};
-
-static int
-devlink_nl_cmd_health_reporter_set_doit(struct sk_buff *skb,
-                                       struct genl_info *info)
-{
-       struct devlink *devlink = info->user_ptr[0];
-       struct devlink_health_reporter *reporter;
-
-       reporter = devlink_health_reporter_get_from_info(devlink, info);
-       if (!reporter)
-               return -EINVAL;
-
-       if (!reporter->ops->recover &&
-           (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD] ||
-            info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]))
-               return -EOPNOTSUPP;
-
-       if (!reporter->ops->dump &&
-           info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
-               return -EOPNOTSUPP;
-
-       if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD])
-               reporter->graceful_period =
-                       nla_get_u64(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_GRACEFUL_PERIOD]);
-
-       if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER])
-               reporter->auto_recover =
-                       nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_RECOVER]);
-
-       if (info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP])
-               reporter->auto_dump =
-               nla_get_u8(info->attrs[DEVLINK_ATTR_HEALTH_REPORTER_AUTO_DUMP]);
-
-       return 0;
-}
-
 static int devlink_nl_cmd_health_reporter_recover_doit(struct sk_buff *skb,
                                                       struct genl_info *info)
 {