Skip to content

Commit

Permalink
teamd: move teamd_update_port_config and teamd_get_actual_config
Browse files Browse the repository at this point in the history
Signed-off-by: Jiri Pirko <jiri@resnulli.us>
  • Loading branch information
jpirko committed Apr 5, 2013
1 parent f4e27d4 commit e68ec8b
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 120 deletions.
114 changes: 0 additions & 114 deletions teamd/teamd.c
Expand Up @@ -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;
Expand Down
7 changes: 3 additions & 4 deletions teamd/teamd.h
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
114 changes: 114 additions & 0 deletions teamd/teamd_config.c
Expand Up @@ -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)
{
Expand Down
4 changes: 2 additions & 2 deletions teamd/teamd_ctl_methods.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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.");
Expand Down

0 comments on commit e68ec8b

Please sign in to comment.