From e68ec8b9f8afcd3b9d66a57e3a191c6f5d27e6e9 Mon Sep 17 00:00:00 2001 From: Jiri Pirko Date: Wed, 3 Apr 2013 16:34:34 +0200 Subject: [PATCH] teamd: move teamd_update_port_config and teamd_get_actual_config Signed-off-by: Jiri Pirko --- teamd/teamd.c | 114 -------------------------------------- teamd/teamd.h | 7 +-- teamd/teamd_config.c | 114 ++++++++++++++++++++++++++++++++++++++ teamd/teamd_ctl_methods.c | 4 +- 4 files changed, 119 insertions(+), 120 deletions(-) diff --git a/teamd/teamd.c b/teamd/teamd.c index c39669b..5a49e00 100644 --- a/teamd/teamd.c +++ b/teamd/teamd.c @@ -752,120 +752,6 @@ static int teamd_check_change_hwaddr(struct teamd_context *ctx) return err; } -static int get_port_obj(json_t **pport_obj, json_t *config_json, - const char *port_name) -{ - int err; - json_t *ports_obj; - json_t *port_obj; - - err = json_unpack(config_json, "{s:o}", "ports", &ports_obj); - if (err) { - ports_obj = json_object(); - if (!ports_obj) - return -ENOMEM; - err = json_object_set(config_json, "ports", ports_obj); - if (err) { - json_decref(ports_obj); - return -ENOMEM; - } - } - err = json_unpack(ports_obj, "{s:o}", port_name, &port_obj); - if (err) { - port_obj = json_object(); - if (!port_obj) - return -ENOMEM; - err = json_object_set(ports_obj, port_name, port_obj); - if (err) { - json_decref(port_obj); - return -ENOMEM; - } - } - if (pport_obj) - *pport_obj = port_obj; - return 0; -} - -int teamd_get_actual_config(struct teamd_context *ctx, json_t **pactual_json) -{ - json_t *actual_json; - struct teamd_port *tdport; - json_t *ports_obj; - void *iter; - int err; - - actual_json = json_deep_copy(ctx->config_json); - if (!actual_json) - return -ENOMEM; - - /* - * Create json objects for all present ports - */ - teamd_for_each_tdport(tdport, ctx) { - err = get_port_obj(NULL, actual_json, tdport->ifname); - if (err) - goto errout; - } - - /* - * Get rid of json object of ports which are not present - */ - err = json_unpack(actual_json, "{s:o}", "ports", &ports_obj); - if (err) { - err = -EINVAL; - goto errout; - } - iter = json_object_iter(ports_obj); - while (iter) { - const char *port_name = json_object_iter_key(iter); - - iter = json_object_iter_next(ports_obj, iter); - if (!teamd_get_port_by_ifname(ctx, (char *) port_name)) - json_object_del(ports_obj, port_name); - } - - *pactual_json = actual_json; - return 0; - -errout: - json_decref(actual_json); - return err; -} - -int teamd_update_port_config(struct teamd_context *ctx, const char *port_name, - const char *json_port_cfg_str) -{ - int err; - json_t *port_obj; - json_t *port_new_obj; - json_error_t jerror; - - port_new_obj = json_loads(json_port_cfg_str, JSON_REJECT_DUPLICATES, - &jerror); - if (!port_new_obj) { - teamd_log_err("%s: Failed to parse port config string: " - "%s on line %d, column %d", port_name, - jerror.text, jerror.line, jerror.column); - return -EIO; - } - err = get_port_obj(&port_obj, ctx->config_json, port_name); - if (err) { - teamd_log_err("%s: Failed to obtain port config object", - port_name); - goto new_port_decref; - } - - /* replace existing object content */ - json_object_clear(port_obj); - err = json_object_update(port_obj, port_new_obj); - if (err) - teamd_log_err("%s: Failed to update existing config " - "port object", port_name); -new_port_decref: - json_decref(port_new_obj); - return err; -} - int teamd_port_add(struct teamd_context *ctx, const char *port_name) { int err; diff --git a/teamd/teamd.h b/teamd/teamd.h index b9bf0ba..388eb44 100644 --- a/teamd/teamd.h +++ b/teamd/teamd.h @@ -199,10 +199,6 @@ int teamd_state_json_dump(struct teamd_context *ctx, json_t **pstate_json); int teamd_state_json_basics_init(struct teamd_context *ctx); void teamd_state_json_basics_fini(struct teamd_context *ctx); -int teamd_get_actual_config(struct teamd_context *ctx, json_t **pactual_json); -int teamd_update_port_config(struct teamd_context *ctx, const char *port_name, - const char *json_port_cfg_str); - /* Main loop callbacks */ #define TEAMD_LOOP_FD_EVENT_READ (1 << 0) #define TEAMD_LOOP_FD_EVENT_WRITE (1 << 1) @@ -362,6 +358,9 @@ static inline bool timespec_is_zero(struct timespec *ts) int teamd_config_load(struct teamd_context *ctx); void teamd_config_free(struct teamd_context *ctx); +int teamd_config_get_actual(struct teamd_context *ctx, json_t **pactual_json); +int teamd_config_port_update(struct teamd_context *ctx, const char *port_name, + const char *json_port_cfg_str); int teamd_config_string_get(struct teamd_context *ctx, const char **p_str_val, const char *fmt, ...); int teamd_config_int_get(struct teamd_context *ctx, int *p_int_val, diff --git a/teamd/teamd_config.c b/teamd/teamd_config.c index dd2dceb..05f9e14 100644 --- a/teamd/teamd_config.c +++ b/teamd/teamd_config.c @@ -60,6 +60,120 @@ void teamd_config_free(struct teamd_context *ctx) json_decref(ctx->config_json); } +static int get_port_obj(json_t **pport_obj, json_t *config_json, + const char *port_name) +{ + int err; + json_t *ports_obj; + json_t *port_obj; + + err = json_unpack(config_json, "{s:o}", "ports", &ports_obj); + if (err) { + ports_obj = json_object(); + if (!ports_obj) + return -ENOMEM; + err = json_object_set(config_json, "ports", ports_obj); + if (err) { + json_decref(ports_obj); + return -ENOMEM; + } + } + err = json_unpack(ports_obj, "{s:o}", port_name, &port_obj); + if (err) { + port_obj = json_object(); + if (!port_obj) + return -ENOMEM; + err = json_object_set(ports_obj, port_name, port_obj); + if (err) { + json_decref(port_obj); + return -ENOMEM; + } + } + if (pport_obj) + *pport_obj = port_obj; + return 0; +} + +int teamd_config_get_actual(struct teamd_context *ctx, json_t **pactual_json) +{ + json_t *actual_json; + struct teamd_port *tdport; + json_t *ports_obj; + void *iter; + int err; + + actual_json = json_deep_copy(ctx->config_json); + if (!actual_json) + return -ENOMEM; + + /* + * Create json objects for all present ports + */ + teamd_for_each_tdport(tdport, ctx) { + err = get_port_obj(NULL, actual_json, tdport->ifname); + if (err) + goto errout; + } + + /* + * Get rid of json object of ports which are not present + */ + err = json_unpack(actual_json, "{s:o}", "ports", &ports_obj); + if (err) { + err = -EINVAL; + goto errout; + } + iter = json_object_iter(ports_obj); + while (iter) { + const char *port_name = json_object_iter_key(iter); + + iter = json_object_iter_next(ports_obj, iter); + if (!teamd_get_port_by_ifname(ctx, (char *) port_name)) + json_object_del(ports_obj, port_name); + } + + *pactual_json = actual_json; + return 0; + +errout: + json_decref(actual_json); + return err; +} + +int teamd_config_port_update(struct teamd_context *ctx, const char *port_name, + const char *json_port_cfg_str) +{ + int err; + json_t *port_obj; + json_t *port_new_obj; + json_error_t jerror; + + port_new_obj = json_loads(json_port_cfg_str, JSON_REJECT_DUPLICATES, + &jerror); + if (!port_new_obj) { + teamd_log_err("%s: Failed to parse port config string: " + "%s on line %d, column %d", port_name, + jerror.text, jerror.line, jerror.column); + return -EIO; + } + err = get_port_obj(&port_obj, ctx->config_json, port_name); + if (err) { + teamd_log_err("%s: Failed to obtain port config object", + port_name); + goto new_port_decref; + } + + /* replace existing object content */ + json_object_clear(port_obj); + err = json_object_update(port_obj, port_new_obj); + if (err) + teamd_log_err("%s: Failed to update existing config " + "port object", port_name); +new_port_decref: + json_decref(port_new_obj); + return err; +} + static int __json_path_lite(json_t **p_json_obj, json_t *json_root, const char *fmt, va_list ap) { diff --git a/teamd/teamd_ctl_methods.c b/teamd/teamd_ctl_methods.c index f1a356b..ca1adaf 100644 --- a/teamd/teamd_ctl_methods.c +++ b/teamd/teamd_ctl_methods.c @@ -49,7 +49,7 @@ static int teamd_ctl_method_port_config_update(struct teamd_context *ctx, teamd_log_err("Device \"%s\" does not exist.", port_devname); return ops->reply_err(ops_priv, "NoSuchDev", "No such device."); } - err = teamd_update_port_config(ctx, port_devname, port_config); + err = teamd_config_port_update(ctx, port_devname, port_config); if (err) { teamd_log_err("Failed to update config for port \"%s\".", port_devname); @@ -133,7 +133,7 @@ static int teamd_ctl_method_config_dump_actual(struct teamd_context *ctx, json_t *actual_json; int err; - err = teamd_get_actual_config(ctx, &actual_json); + err = teamd_config_get_actual(ctx, &actual_json); if (err) { teamd_log_err("Failed to get actual config."); return ops->reply_err(ops_priv, "ConfigDumpActualFail", "Failed to get actual config.");