Skip to content

Commit

Permalink
lacp: add initial port state configuration
Browse files Browse the repository at this point in the history
Signed-off-by: Jonas Johansson <[email protected]>
  • Loading branch information
Jonas Johansson authored and Mika Juvonen committed Oct 11, 2021
1 parent 4384454 commit 5b70f28
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/team.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ int team_port_remove(struct team_handle *th, uint32_t port_ifindex);
bool team_is_our_port(struct team_handle *th, uint32_t port_ifindex);
int team_carrier_set(struct team_handle *th, bool carrier_up);
int team_carrier_get(struct team_handle *th, bool *carrier_up);
int team_link_set(struct team_handle *th, int ifindex, bool link_up);
int team_hwaddr_set(struct team_handle *th, uint32_t ifindex,
const char *addr, unsigned int addr_len);
int team_hwaddr_get(struct team_handle *th, uint32_t ifindex,
Expand Down
41 changes: 41 additions & 0 deletions libteam/libteam.c
Original file line number Diff line number Diff line change
Expand Up @@ -1673,6 +1673,47 @@ int team_carrier_get(struct team_handle *th, bool *carrier_up)
#endif
}

/**
* @param th libteam library context
* @param ifindex interface index
* @param link_up link state to be set
*
* @details Sets link status for a network interface
*
* @return Zero on success or negative number in case of an error.
**/
TEAM_EXPORT
int team_link_set(struct team_handle *th, int ifindex, bool link_up)
{
#ifdef HAVE_RTNL_LINK_SET_CARRIER
struct rtnl_link *link;
int err;

link = rtnl_link_alloc();
if (!link)
return -ENOMEM;

rtnl_link_set_ifindex(link, ifindex);
if (link_up)
rtnl_link_set_flags(link, IFF_UP);
else
rtnl_link_unset_flags(link, IFF_UP);

err = rtnl_link_change(th->nl_cli.sock, link, link, 0);
err = -nl2syserr(err);

rtnl_link_put(link);
if (err == -EINVAL) {
warn(th, "Failed to set link status.");
return 0;
}
return err;
#else
return -EOPNOTSUPP;
#endif
}


/**
* @param th libteam library context
* @param ifindex interface index
Expand Down
10 changes: 10 additions & 0 deletions teamd/teamd_runner_lacp.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ struct lacp_port {
#define LACP_PORT_CFG_DFLT_LACP_KEY 0
bool sticky;
#define LACP_PORT_CFG_DFLT_STICKY false
bool link_up;
#define LACP_PORT_CFG_DFLT_STATE true
} cfg;
};

Expand Down Expand Up @@ -1251,6 +1253,13 @@ static int lacp_port_load_config(struct teamd_context *ctx,
lacp_port->cfg.sticky = LACP_PORT_CFG_DFLT_STICKY;
teamd_log_dbg(ctx, "%s: Using sticky \"%d\".", port_name,
lacp_port->cfg.sticky);

err = teamd_config_bool_get(ctx, &lacp_port->cfg.link_up,
"$.ports.%s.link_up", port_name);
if (err)
lacp_port->cfg.link_up = LACP_PORT_CFG_DFLT_STATE;
teamd_log_dbg(ctx, "%s: Using link_up \"%d\".", port_name,
lacp_port->cfg.link_up);
return 0;
}

Expand Down Expand Up @@ -1330,6 +1339,7 @@ static int lacp_port_added(struct teamd_context *ctx,
if (!TEAMD_ENOENT(err))
goto timeout_callback_del;
}
team_link_set(ctx->th, tdport->ifindex, lacp_port->cfg.link_up);

err = lacp_port_set_mac(ctx, tdport);
if (err)
Expand Down
52 changes: 49 additions & 3 deletions teamd/teamd_runner_loadbalance.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,43 @@ struct lb {
struct teamd_balancer *tb;
};

static int lb_event_watch_port_added(struct teamd_context *ctx,
struct teamd_port *tdport, void *priv)
struct lb_port {
struct teamd_port *tdport;
struct {
bool link_up;
#define LB_DFLT_PORT_STATE true
} cfg;
};

static int lb_port_load_config(struct teamd_context *ctx,
struct lb_port *lb_port)
{
struct lb *lb = priv;
const char *port_name = lb_port->tdport->ifname;
int err;

err = teamd_config_bool_get(ctx, &lb_port->cfg.link_up,
"$.ports.%s.link_up", port_name);
if (err)
lb_port->cfg.link_up = LB_DFLT_PORT_STATE;
teamd_log_dbg(ctx, "%s: Using link_up \"%d\".", port_name,
lb_port->cfg.link_up);
return 0;
}

static int lb_port_added(struct teamd_context *ctx,
struct teamd_port *tdport,
void *priv, void *creator_priv)
{
struct lb_port *lb_port = priv;
struct lb *lb = creator_priv;
int err;

lb_port->tdport = tdport;
err = lb_port_load_config(ctx, lb_port);
if (err) {
teamd_log_err("Failed to load port config.");
return err;
}
err = team_hwaddr_set(ctx->th, tdport->ifindex, ctx->hwaddr,
ctx->hwaddr_len);
if (err) {
Expand All @@ -49,9 +80,24 @@ static int lb_event_watch_port_added(struct teamd_context *ctx,
return TEAMD_ENOENT(err) ? 0 : err;
}
}
team_link_set(ctx->th, tdport->ifindex, lb_port->cfg.link_up);

return teamd_balancer_port_added(lb->tb, tdport);
}

static const struct teamd_port_priv lb_port_priv = {
.init = lb_port_added,
.priv_size = sizeof(struct lb_port),
};

static int lb_event_watch_port_added(struct teamd_context *ctx,
struct teamd_port *tdport, void *priv)
{
struct lb *lb = priv;

return teamd_port_priv_create(tdport, &lb_port_priv, lb);
}

static void lb_event_watch_port_removed(struct teamd_context *ctx,
struct teamd_port *tdport, void *priv)
{
Expand Down

0 comments on commit 5b70f28

Please sign in to comment.