Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/gnrc_netif: add bus for interface events, gnrc_netif_up()/gnrc_netif_down() functions #17902

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

benpicco
Copy link
Contributor

@benpicco benpicco commented Apr 4, 2022

Contribution description

It can be useful to know when an interface is up / down, e.g. to (re)start or stop the DHCP client.

This adds a bus to gnrc netif where users interested in interface events (up/down) can subscripe.

It also adds convenience functions gnrc_netif_up(), gnrc_netif_down() that block until the interface event is generated (or timeout).

Testing procedure

Issues/PRs references

includes #17893

@benpicco benpicco added the State: waiting for other PR State: The PR requires another PR to be merged first label Apr 4, 2022
@github-actions github-actions bot added Area: network Area: Networking Area: sys Area: System labels Apr 4, 2022
@benpicco benpicco requested a review from jia200x April 4, 2022 15:32
@jia200x
Copy link
Member

jia200x commented Apr 5, 2022

The approach makes sense to me and can be easily extended to other netif link layers (e.g 802.15.4).
Let's get #17893 first and then proceed with this one

@benpicco benpicco force-pushed the gnrc_netif_updown-event branch from f8afd7c to b9ff0ca Compare September 18, 2022 14:03
@benpicco benpicco removed the State: waiting for other PR State: The PR requires another PR to be merged first label Sep 18, 2022
@benpicco benpicco force-pushed the gnrc_netif_updown-event branch from 86a9be4 to c17dc4c Compare September 18, 2022 15:48
Copy link
Contributor

@fabian18 fabian18 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

first round of comments :)

sys/net/gnrc/netif/gnrc_netif.c Outdated Show resolved Hide resolved
msg_bus_subscribe(&sub, GNRC_IPV6_EVENT_ADDR_VALID);
#endif

while (gnrc_netapi_set(netif->pid, NETOPT_STATE, 0, &state, sizeof(state)) == -EBUSY) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Must it be NETOPT_STATE or can it be NETOPT_LINK? Because our ifconfig <i> {up | down} command generates NETOPT_LINK.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uh interesting.
My use case was setting STATE_SLEEP which should also generate a link down event…

NETOPT_LINK is read-only on all 3 drivers that implement it.

sys/net/gnrc/netif/gnrc_netif.c Outdated Show resolved Hide resolved
sys/net/gnrc/netif/gnrc_netif.c Show resolved Hide resolved
sys/net/gnrc/netif/gnrc_netif.c Outdated Show resolved Hide resolved
@benpicco benpicco force-pushed the gnrc_netif_updown-event branch from 5a25d0c to b54778a Compare September 21, 2022 16:49
@benpicco benpicco force-pushed the gnrc_netif_updown-event branch from b54778a to bb2697b Compare October 11, 2022 21:27
@benpicco
Copy link
Contributor Author

benpicco commented Oct 18, 2022

With 8de8d05 we can now also make use of the bus mechanism in gnrc_ipv6_nib.

This makes the gnrc_netif_bus module mandatory and drops the GNRC_IPV6_NIB_IFACE_UP/DOWN events as they are no longer needed. This unfortunately adds ~400 bytes to the ROM.

To register with the interface events, we have to wait for the interfaces to be initialized. Especially on esp32 this can take surprisingly long.

If you don't agree with this, I can drop the commit.

@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Oct 20, 2022
@riot-ci
Copy link

riot-ci commented Oct 20, 2022

Murdock results

FAILED

8de8d05 gnrc_ipv6_nib: make use of gnrc_netif_bus for interface events

Success Failures Total Runtime
371 0 2006 01m:37s

Artifacts

This only reflects a subset of all builds from https://ci-prod.riot-os.org. Please refer to https://ci.riot-os.org for a complete build for now.

@benpicco benpicco added CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Dec 13, 2022
Copy link
Member

@maribu maribu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm. Some comments inline.

I think the opportunistic approach to wait for interfaces to become ready is a dragon that at least should be documented.

if ((state == NETOPT_STATE_SLEEP) || (state == NETOPT_STATE_OFF) ||
(state == NETOPT_STATE_STANDBY)) {
event = GNRC_NETIF_EVENT_LINK_STATE_CHANGED_DOWN;
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else {
}
else {

Comment on lines +189 to +192
DEBUG_PUTS("ipv6: wait for interfaces to be ready");
while (gnrc_netif_iter(NULL) == NULL) {
ztimer_sleep(ZTIMER_MSEC, 250);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fragile. But I also do not know any way to reliably wait for interface registration to complete off the top of my head or even if such feature exists :/

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The interfaces could send a message when they are initialized, then we can handle the registration here.

gnrc_ipv6_nib_iface_up(msg.content.ptr);
break;
case GNRC_IPV6_NIB_IFACE_DOWN:
case GNRC_NETIF_EVENT_LINK_STATE_CHANGED_DOWN:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Care to fix the indent here, since you touch the code anyway?

@benpicco benpicco added the State: waiting for author State: Action by the author of the PR is required label Feb 2, 2023
Comment on lines +114 to +128
typedef enum {
/**
* @brief Link state changed to UP
*
* The event is generated when the link state on the interface became online.
*/
GNRC_NETIF_EVENT_LINK_STATE_CHANGED_DOWN,

/**
* @brief Link state changed to DOWN
*
* The event is generated when the link state on the interface become offline.
*/
GNRC_NETIF_EVENT_LINK_STATE_CHANGED_UP,
} gnrc_netif_event_t;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation seems to be swapped

int res = 0;

netopt_state_t state_prev;
while (gnrc_netapi_get(netif->pid, NETOPT_STATE, 0, &state_prev, sizeof(state_prev)) == -EBUSY) {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line and the one below are a bit too long (warning of > 100 characters)

@benpicco benpicco requested a review from HendrikVE December 18, 2023 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: network Area: Networking Area: sys Area: System CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR State: waiting for author State: Action by the author of the PR is required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants