Merge pull request #19476 from yuwata/network-can

network: several CAN device related updates
This commit is contained in:
Yu Watanabe 2021-05-01 15:03:22 +09:00 committed by GitHub
commit 0c9cdcb9d4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 73 deletions

View file

@ -52,7 +52,7 @@ int config_parse_can_bitrate(
return 0;
}
static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
static int link_set_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
int r;
assert(link);
@ -60,53 +60,24 @@ static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link)
if (IN_SET(link->state, LINK_STATE_FAILED, LINK_STATE_LINGER))
return 1;
r = sd_netlink_message_get_errno(m);
if (r < 0)
/* we warn but don't fail the link, as it may be brought up later */
log_link_message_warning_errno(link, m, r, "Could not bring up interface");
return 1;
}
static int link_up_can(Link *link) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
int r;
assert(link);
log_link_debug(link, "Bringing CAN link up");
r = sd_rtnl_message_new_link(link->manager->rtnl, &req, RTM_SETLINK, link->ifindex);
if (r < 0)
return log_link_error_errno(link, r, "Could not allocate RTM_SETLINK message: %m");
r = sd_rtnl_message_link_set_flags(req, IFF_UP, IFF_UP);
if (r < 0)
return log_link_error_errno(link, r, "Could not set link flags: %m");
r = netlink_call_async(link->manager->rtnl, NULL, req, link_up_handler,
link_netlink_destroy_callback, link);
if (r < 0)
return log_link_error_errno(link, r, "Could not send rtnetlink message: %m");
link_ref(link);
return 0;
}
static int link_set_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link) {
int r;
assert(link);
log_link_debug(link, "Set link");
r = sd_netlink_message_get_errno(m);
if (r < 0 && r != -EEXIST) {
log_link_message_warning_errno(link, m, r, "Failed to configure CAN link");
link_enter_failed(link);
return 1;
}
log_link_debug(link, "Link set");
r = link_activate(link);
if (r < 0) {
link_enter_failed(link);
return 1;
}
link->can_configured = true;
link_check_ready(link);
return 1;
}
@ -255,9 +226,6 @@ static int link_set_can(Link *link) {
link_ref(link);
if (!(link->flags & IFF_UP))
return link_up_can(link);
return 0;
}
@ -290,30 +258,21 @@ int link_configure_can(Link *link) {
if (streq_ptr(link->kind, "can")) {
/* The CAN interface must be down to configure bitrate, etc... */
if ((link->flags & IFF_UP)) {
if ((link->flags & IFF_UP))
r = link_down(link, link_down_handler);
if (r < 0) {
link_enter_failed(link);
return r;
}
} else {
else
r = link_set_can(link);
if (r < 0) {
link_enter_failed(link);
return r;
}
}
return 0;
}
if (!(link->flags & IFF_UP)) {
r = link_up_can(link);
if (r < 0) {
if (r < 0)
link_enter_failed(link);
return r;
}
return r;
}
r = link_activate(link);
if (r < 0)
return r;
link->can_configured = true;
link_check_ready(link);
return 0;
}

View file

@ -764,6 +764,15 @@ void link_check_ready(Link *link) {
if (!link->network)
return;
if (link->iftype == ARPHRD_CAN) {
/* let's shortcut things for CAN which doesn't need most of checks below. */
if (!link->can_configured)
return (void) log_link_debug(link, "%s(): CAN device is not configured.", __func__);
link_enter_configured(link);
return;
}
if (!link->addresses_configured)
return (void) log_link_debug(link, "%s(): static addresses are not configured.", __func__);
@ -1376,7 +1385,7 @@ static int link_up_handler(sd_netlink *rtnl, sd_netlink_message *m, Link *link)
return 1;
}
static int link_up(Link *link) {
int link_up(Link *link) {
_cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL;
int r;
@ -1771,7 +1780,7 @@ static void link_drop(Link *link) {
link_detach_from_manager(link);
}
static int link_joined(Link *link) {
int link_activate(Link *link) {
int r;
assert(link);
@ -1789,10 +1798,8 @@ static int link_joined(Link *link) {
_fallthrough_;
case ACTIVATION_POLICY_ALWAYS_UP:
r = link_up(link);
if (r < 0) {
link_enter_failed(link);
if (r < 0)
return r;
}
break;
case ACTIVATION_POLICY_DOWN:
if (link->activated)
@ -1800,16 +1807,27 @@ static int link_joined(Link *link) {
_fallthrough_;
case ACTIVATION_POLICY_ALWAYS_DOWN:
r = link_down(link, NULL);
if (r < 0) {
link_enter_failed(link);
if (r < 0)
return r;
}
break;
default:
break;
}
link->activated = true;
return 0;
}
static int link_joined(Link *link) {
int r;
assert(link);
assert(link->network);
r = link_activate(link);
if (r < 0)
return r;
if (link->network->bridge) {
r = link_set_bridge(link);
if (r < 0)
@ -2076,6 +2094,7 @@ int link_configure(Link *link) {
return r;
if (link->iftype == ARPHRD_CAN)
/* let's shortcut things for CAN which doesn't need most of what's done below. */
return link_configure_can(link);
r = link_set_sysctl(link);
@ -2567,6 +2586,10 @@ static int link_carrier_gained(Link *link) {
assert(link);
if (link->iftype == ARPHRD_CAN)
/* let's shortcut things for CAN which doesn't need most of what's done below. */
return link_handle_bound_by_list(link);
r = wifi_get_info(link);
if (r < 0)
return r;
@ -2625,6 +2648,10 @@ static int link_carrier_lost(Link *link) {
if (link->network && link->network->ignore_carrier_loss)
return 0;
if (link->iftype == ARPHRD_CAN)
/* let's shortcut things for CAN which doesn't need most of what's done below. */
return link_handle_bound_by_list(link);
/* Some devices reset itself while setting the MTU. This causes the DHCP client fall into a loop.
* setting_mtu keep track whether the device got reset because of setting MTU and does not drop the
* configuration and stop the clients as well. */
@ -2712,6 +2739,10 @@ static int link_admin_state_down(Link *link) {
return 0;
if (link->network->activation_policy == ACTIVATION_POLICY_ALWAYS_UP) {
if (streq_ptr(link->kind, "can") && !link->can_configured)
/* CAN device needs to be down on configure. */
return 0;
log_link_info(link, "ActivationPolicy is \"always-on\", forcing link up");
return link_up(link);
}

View file

@ -131,6 +131,7 @@ typedef struct Link {
bool setting_genmode:1;
bool ipv6_mtu_set:1;
bool bridge_mdb_configured:1;
bool can_configured:1;
bool activated:1;
sd_dhcp_server *dhcp_server;
@ -207,7 +208,9 @@ DEFINE_TRIVIAL_DESTRUCTOR(link_netlink_destroy_callback, Link, link_unref);
int link_get(Manager *m, int ifindex, Link **ret);
int link_up(Link *link);
int link_down(Link *link, link_netlink_message_handler_t callback);
int link_activate(Link *link);
void link_enter_failed(Link *link);