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

Fix hostapd's place in systemd dependency tree. #45464

Merged
merged 1 commit into from Oct 17, 2018

Conversation

clefru
Copy link
Contributor

@clefru clefru commented Aug 22, 2018

after clause:

  • Remove unrelated services. They don't need to be there for a link-level service
  • Only retain sys-subsystem-net-devices-${if}.device as this is only what hostapd needs

requiredBy clause:

  • Add hostapd as a requirement for the ${if} link to be brought up.

wantedBy clause:

  • Remove general network.target, as this is already implied from dependencies
    stacked above hostapd. And if it's not implied than starting hostapd is not required
    for this particular network configuration.
Motivation for this change

bridge setup races with hostapd, and if it wins, the bridge fails to start, see: #16090

Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option sandbox in nix.conf on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Determined the impact on package closure size (by running nix path-info -S before and after)
  • Fits CONTRIBUTING.md.

@clefru
Copy link
Contributor Author

clefru commented Aug 22, 2018

Here is the dependency graph that I get with this PR. Notice that br0-netdev.service depends on network-link-wlp4s0.service which depends on hostapd.service which depends on sys-subsystem-net-devices-wlp4s0.device.

You can generate this graph yourself with:

systemd-analyze dot br0-netdev.service hostapd.service network.target | dot -Tpdf -o /tmp/foo.pdf


after = [ "${cfg.interface}-cfg.service" "nat.service" "bind.service" "dhcpd.service" "sys-subsystem-net-devices-${cfg.interface}.device" ];
after = [ "sys-subsystem-net-devices-${cfg.interface}.device" ];
requiredBy = [ "network-link-${cfg.interface}.service" ];
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it should also add BindsTo=sys-subsystem-net-devices-wlan0.device?

Copy link
Member

Choose a reason for hiding this comment

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

Arch does After=network.target, but I guess they just cannot add more specific requirements because they don't know which interface will be used.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, BindsTo seems appropriate. I'll switch after to BindsTo as BindsTo is already implies the effects of after.

after=network.target is not correct, as this potentially would hoist hostapd to the top of the dependency tree and causes the race to reappear. To get a visual idea of that, please look at the pdf attached in my PR comment. Also after=network.target gives a cyclic after chain of hostapd->network->br0-netdev->network-link-wl4s0->hostapd. Somebody in #16090 said they had the same bridge race with Arch. Maybe that's the reason. I'll ping this PR to them.

@clefru clefru force-pushed the hostapd-dep-fix-on-master branch 2 times, most recently from e68f914 to b8d1638 Compare August 22, 2018 12:10
@clefru
Copy link
Contributor Author

clefru commented Aug 28, 2018

@qknight could you check if this fixes your bridge problems that you posted in #16090 ?

@qknight
Copy link
Member

qknight commented Sep 28, 2018

@clefru your patch does not work here

probosed solution

merge our patches as this:

-         wantedBy = [ "network.target" ];
-         after = [ "${cfg.interface}-cfg.service" "nat.service" "bind.service" "dhcpd.service" "sys-subsystem-net-devices-${cfg.interface}.device" ];
+         after = [ "sys-subsystem-net-devices-${cfg.interface}.device" ];
+         bindTo = [ "sys-subsystem-net-devices-${cfg.interface}.device" ];
+         requiredBy = [ "network-link-${cfg.interface}.service" ];

i've tried with requires instead of after but it failed. i also tried requriedBy instead of after which also fails. so both might be wrong here.

these configs seem to use a combination of after and bindTo, see:

with only your patch

patch:

-        wantedBy = [ "network.target" ];

-         after = [ "${cfg.interface}-cfg.service" "nat.service" "bind.service" "dhcpd.service" "sys-subsystem-net-devices-${cfg.interface}.device" ];
+         bindsTo = [ "sys-subsystem-net-devices-${cfg.interface}.device" ];
+         requiredBy = [ "network-link-${cfg.interface}.service" ];

outcome:

[  OK  ] Reached target Basic System.
         Starting Link configuration of enp1s0...
         Starting NTP Daemon...
         Starting Name Service Cache Daemon...
         Starting Address configuration of enp1s0...
         Starting Cron Daemon...
[  OK  ] Started hostapd wireless AP.
         Starting Kernel Auditing...
[  OK  ] Started nixcloud.monitoring rule up… timer for mail.lastlog.de-BACKUP.
[  OK  ] Started Kernel Log Daemon.
[  OK  ] Started nixcloud.monitoring rule upload timer for apu-nixi-BACKUP.
[  OK  ] Reached target Timers.
[  OK  ] Started Create Volatile Files and Directories.
[  OK  ] Started Link configuration of enp1s0.
[  OK  ] Started Address configuration of enp1s0.
[  OK  ] Started Cron Daemon.
[  OK  ] Started Kernel Auditing.
[  OK  ] Started NTP Daemon.
[  OK  ] Reached target System Time Synchronized.
[  OK  ] Started Name Service Cache Daemon.
[  OK  ] Reached target User and Group Name Lookups.
         Starting Login Service...
[  OK  ] Reached target Host and Network Name Lookups.
[  OK  ] Stopped hostapd wireless AP.
[  OK  ] Started hostapd wireless AP.
[  OK  ] Started D-Bus System Message Bus.
[  OK  ] Found device QCA986x/988x 802.11ac Wireless Network Adapter.
[  OK  ] Started Login Service.
         Starting Link configuration of wlp4s0...
         Starting Address configuration of wlp4s0...
         Starting Load/Save RF Kill Switch Status...
[  OK  ] Started Load/Save RF Kill Switch Status.
[  OK  ] Started Address configuration of wlp4s0.
[  OK  ] Stopped hostapd wireless AP.
[  OK  ] Started hostapd wireless AP.
[  OK  ] Started Link configuration of wlp4s0.
         Starting Bridge Interface br0...
[  OK  ] Reached target All Network Interfaces (deprecated).
[  OK  ] Started Bridge Interface br0.
         Starting Networking Setup...
[  OK  ] Started Networking Setup

journalctl -u hostapd

-- Reboot --
Sep 28 14:34:49 apu-nixi systemd[1]: Started hostapd wireless AP.
Sep 28 14:34:49 apu-nixi hostapd[731]: Configuration file: /nix/store/n44xdhcw27a4g5lyvkw292bpfbncayzd-hostapd.conf
Sep 28 14:34:49 apu-nixi hostapd[731]: Could not read interface wlp4s0 flags: No such device
Sep 28 14:34:49 apu-nixi hostapd[731]: nl80211: Driver does not support authentication/association or connect commands
Sep 28 14:34:49 apu-nixi hostapd[731]: nl80211: deinit ifname=wlp4s0 disabled_11b_rates=0
Sep 28 14:34:49 apu-nixi hostapd[731]: Could not read interface wlp4s0 flags: No such device
Sep 28 14:34:49 apu-nixi hostapd[731]: nl80211 driver initialization failed.
Sep 28 14:34:49 apu-nixi hostapd[731]: wlp4s0: interface state UNINITIALIZED->DISABLED
Sep 28 14:34:49 apu-nixi hostapd[731]: wlp4s0: AP-DISABLED
Sep 28 14:34:49 apu-nixi hostapd[731]: hostapd_free_hapd_data: Interface wlp4s0 wasn't started
Sep 28 14:34:49 apu-nixi systemd[1]: hostapd.service: Main process exited, code=exited, status=1/FAILURE
Sep 28 14:34:49 apu-nixi systemd[1]: hostapd.service: Failed with result 'exit-code'.
Sep 28 14:34:49 apu-nixi systemd[1]: hostapd.service: Service hold-off time over, scheduling restart.
Sep 28 14:34:49 apu-nixi systemd[1]: hostapd.service: Scheduled restart job, restart counter is at 1.
Sep 28 14:34:49 apu-nixi systemd[1]: Stopped hostapd wireless AP.
Sep 28 14:34:49 apu-nixi systemd[1]: Started hostapd wireless AP.
Sep 28 14:34:49 apu-nixi hostapd[784]: Configuration file: /nix/store/n44xdhcw27a4g5lyvkw292bpfbncayzd-hostapd.conf
Sep 28 14:34:49 apu-nixi hostapd[784]: Could not read interface wlp4s0 flags: No such device
Sep 28 14:34:49 apu-nixi hostapd[784]: nl80211: Driver does not support authentication/association or connect commands
Sep 28 14:34:49 apu-nixi hostapd[784]: nl80211: deinit ifname=wlp4s0 disabled_11b_rates=0
Sep 28 14:34:49 apu-nixi hostapd[784]: Could not read interface wlp4s0 flags: No such device
Sep 28 14:34:49 apu-nixi hostapd[784]: nl80211 driver initialization failed.
Sep 28 14:34:49 apu-nixi hostapd[784]: wlp4s0: interface state UNINITIALIZED->DISABLED
Sep 28 14:34:49 apu-nixi hostapd[784]: wlp4s0: AP-DISABLED
Sep 28 14:34:49 apu-nixi hostapd[784]: hostapd_free_hapd_data: Interface wlp4s0 wasn't started
Sep 28 14:34:54 apu-nixi systemd[1]: hostapd.service: Main process exited, code=exited, status=1/FAILURE
Sep 28 14:34:54 apu-nixi systemd[1]: hostapd.service: Failed with result 'exit-code'.
Sep 28 14:34:55 apu-nixi systemd[1]: hostapd.service: Service hold-off time over, scheduling restart.
Sep 28 14:34:55 apu-nixi systemd[1]: hostapd.service: Scheduled restart job, restart counter is at 2.
Sep 28 14:34:55 apu-nixi systemd[1]: Stopped hostapd wireless AP.
Sep 28 14:34:55 apu-nixi systemd[1]: Started hostapd wireless AP.
Sep 28 14:34:55 apu-nixi hostapd[816]: Configuration file: /nix/store/n44xdhcw27a4g5lyvkw292bpfbncayzd-hostapd.conf
Sep 28 14:34:57 apu-nixi hostapd[816]: Using interface wlp4s0 with hwaddr 04:f0:21:1b:60:cb and ssid "flux"
Sep 28 14:34:57 apu-nixi hostapd[816]: random: Only 8/20 bytes of strong random data available from /dev/random
Sep 28 14:34:57 apu-nixi hostapd[816]: random: Not enough entropy pool available for secure operations
Sep 28 14:34:57 apu-nixi hostapd[816]: WPA: Not enough entropy in random pool for secure operations - update keys later when the first station connects
Sep 28 14:34:57 apu-nixi hostapd[816]: wlp4s0: interface state UNINITIALIZED->ENABLED
Sep 28 14:34:57 apu-nixi hostapd[816]: wlp4s0: AP-ENABLED
Sep 28 14:35:06 apu-nixi hostapd[816]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: authenticated
Sep 28 14:35:06 apu-nixi hostapd[816]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: authenticated
Sep 28 14:35:06 apu-nixi hostapd[816]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: associated (aid 1)
Sep 28 14:35:06 apu-nixi hostapd[816]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: associated (aid 1)
Sep 28 14:35:06 apu-nixi hostapd[816]: random: Cannot read from /dev/random: Resource temporarily unavailable
Sep 28 14:35:06 apu-nixi hostapd[816]: random: Only 8/20 bytes of strong random data available from /dev/random
Sep 28 14:35:06 apu-nixi hostapd[816]: random: Not enough entropy pool available for secure operations
Sep 28 14:35:06 apu-nixi hostapd[816]: WPA: Not enough entropy in random pool to proceed - reject first 4-way handshake
Sep 28 14:35:11 apu-nixi hostapd[816]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: deauthenticated due to local deauth request
Sep 28 14:35:11 apu-nixi hostapd[816]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: deauthenticated due to local deauth request
Sep 28 14:35:21 apu-nixi hostapd[816]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: authenticated
Sep 28 14:35:21 apu-nixi hostapd[816]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: authenticated
Sep 28 14:35:21 apu-nixi hostapd[816]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: associated (aid 1)
Sep 28 14:35:21 apu-nixi hostapd[816]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: associated (aid 1)
Sep 28 14:35:21 apu-nixi hostapd[816]: random: Cannot read from /dev/random: Resource temporarily unavailable
Sep 28 14:35:21 apu-nixi hostapd[816]: random: Only 8/20 bytes of strong random data available from /dev/random

then it works

with our patches combined

-        wantedBy = [ "network.target" ];

-          after = [ "${cfg.interface}-cfg.service" "nat.service" "bind.service" "dhcpd.service" "sys-subsystem-net-devices-${cfg.interface}.device" ];
+         after = [ "sys-subsystem-net-devices-${cfg.interface}.device" ];
+         requiredBy = [ "network-link-${cfg.interface}.service" ];

boot log:

[  OK  ] Started Create Volatile Files and Directories.
[  OK  ] Found device I210 Gigabit Network Connection.
[  OK  ] Started Cron Daemon.
         Starting Address configuration of enp1s0...
         Starting Link configuration of enp1s0...
[  OK  ] Started Kernel Auditing.
[  OK  ] Started NTP Daemon.
[  OK  ] Reached target System Time Synchronized.
[  OK  ] Started Address configuration of enp1s0.
[  OK  ] Started Link configuration of enp1s0.
[  OK  ] Started Name Service Cache Daemon.
[  OK  ] Reached target User and Group Name Lookups.
         Starting Login Service...
[  OK  ] Reached target Host and Network Name Lookups.
[  OK  ] Started D-Bus System Message Bus.
[  OK  ] Found device QCA986x/988x 802.11ac Wireless Network Adapter.
[  OK  ] Started Login Service.
         Starting Load/Save RF Kill Switch Status...
         Starting Link configuration of wlp4s0...
         Starting Address configuration of wlp4s0...
[  OK  ] Started hostapd wireless AP.
[  OK  ] Started Load/Save RF Kill Switch Status.
[  OK  ] Started Address configuration of wlp4s0.
[  OK  ] Started Link configuration of wlp4s0.
         Starting Bridge Interface br0...
[  OK  ] Reached target All Network Interfaces (deprecated).
[  OK  ] Started Bridge Interface br0.
         Starting Networking Setup...

journalctl -u hostapd

-- Reboot --
Sep 28 14:39:43 apu-nixi systemd[1]: Started hostapd wireless AP.
Sep 28 14:39:43 apu-nixi hostapd[798]: Configuration file: /nix/store/n44xdhcw27a4g5lyvkw292bpfbncayzd-hostapd.conf
Sep 28 14:39:46 apu-nixi hostapd[798]: Using interface wlp4s0 with hwaddr 04:f0:21:1b:60:cb and ssid "flux"
Sep 28 14:39:46 apu-nixi hostapd[798]: random: Only 6/20 bytes of strong random data available from /dev/random
Sep 28 14:39:46 apu-nixi hostapd[798]: random: Not enough entropy pool available for secure operations
Sep 28 14:39:46 apu-nixi hostapd[798]: WPA: Not enough entropy in random pool for secure operations - update keys later when the first station connects
Sep 28 14:39:46 apu-nixi hostapd[798]: wlp4s0: interface state UNINITIALIZED->ENABLED
Sep 28 14:39:46 apu-nixi hostapd[798]: wlp4s0: AP-ENABLED
Sep 28 14:39:54 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: authenticated
Sep 28 14:39:54 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: authenticated
Sep 28 14:39:54 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: associated (aid 1)
Sep 28 14:39:54 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: associated (aid 1)
Sep 28 14:39:54 apu-nixi hostapd[798]: random: Cannot read from /dev/random: Resource temporarily unavailable
Sep 28 14:39:54 apu-nixi hostapd[798]: random: Only 6/20 bytes of strong random data available from /dev/random
Sep 28 14:39:54 apu-nixi hostapd[798]: random: Not enough entropy pool available for secure operations
Sep 28 14:39:54 apu-nixi hostapd[798]: WPA: Not enough entropy in random pool to proceed - reject first 4-way handshake
Sep 28 14:39:59 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: deauthenticated due to local deauth request
Sep 28 14:39:59 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: deauthenticated due to local deauth request
Sep 28 14:40:07 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: authenticated
Sep 28 14:40:07 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: authenticated
Sep 28 14:40:07 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: associated (aid 1)
Sep 28 14:40:07 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: associated (aid 1)
Sep 28 14:40:07 apu-nixi hostapd[798]: random: Cannot read from /dev/random: Resource temporarily unavailable
Sep 28 14:40:07 apu-nixi hostapd[798]: random: Only 6/20 bytes of strong random data available from /dev/random
Sep 28 14:40:07 apu-nixi hostapd[798]: random: Not enough entropy pool available for secure operations
Sep 28 14:40:07 apu-nixi hostapd[798]: WPA: Not enough entropy in random pool to proceed - reject first 4-way handshake
Sep 28 14:40:12 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: deauthenticated due to local deauth request
Sep 28 14:40:12 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: deauthenticated due to local deauth request
Sep 28 14:40:32 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: authenticated
Sep 28 14:40:32 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: authenticated
Sep 28 14:40:32 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: associated (aid 1)
Sep 28 14:40:32 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 IEEE 802.11: associated (aid 1)
Sep 28 14:40:32 apu-nixi hostapd[798]: random: Cannot read from /dev/random: Resource temporarily unavailable
Sep 28 14:40:32 apu-nixi hostapd[798]: random: Only 12/20 bytes of strong random data available from /dev/random
Sep 28 14:40:32 apu-nixi hostapd[798]: random: Allow operation to proceed based on internal entropy
Sep 28 14:40:32 apu-nixi hostapd[798]: wlp4s0: AP-STA-CONNECTED 3c:a9:f4:18:fe:08
Sep 28 14:40:32 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 RADIUS: starting accounting session 4D390B8406A0049D
Sep 28 14:40:32 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 WPA: pairwise key handshake completed (WPA)
Sep 28 14:40:32 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 RADIUS: starting accounting session 4D390B8406A0049D
Sep 28 14:40:32 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 WPA: pairwise key handshake completed (WPA)
Sep 28 14:40:32 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 WPA: group key handshake completed (WPA)
Sep 28 14:40:32 apu-nixi hostapd[798]: wlp4s0: STA 3c:a9:f4:18:fe:08 WPA: group key handshake completed (WPA)

* nat/bind/dhcp.service:
  Remove. Those services have nothing to do with a link-level service.

* sys-subsystem-net-devices-${if}.device:
  Add as BindsTo dependency as this will make hostapd stop when the
  device is unplugged.

* network-link-${if}.service:
  Add hostapd as dependency for this service via requiredBy clause,
  so that the network link is only considered to be established
  only after hostapd has started.

* network.target:
  Remove this from wantedBy clause as this is already implied from
  dependencies stacked above hostapd. And if it's not implied than
  starting hostapd is not required for this particular network
  configuration.
@clefru
Copy link
Contributor Author

clefru commented Oct 10, 2018

@qknight I have updated the PR as you suggested (retaining the after clause). Thanks for you analysis. (Also I reread the systemd.unit manpage, and my assertation above that "BindsTo" effects subsume those of "after" is wrong, hence it's still needed)

@qknight
Copy link
Member

qknight commented Oct 10, 2018

@clefru ok, when you think the PR is ready i evaluate it once more, then we merge

@clefru
Copy link
Contributor Author

clefru commented Oct 13, 2018

@qknight Please re-evaluate the PR as I updated it with your suggestions. Thank you!

@qknight qknight merged commit 725fcde into NixOS:master Oct 17, 2018
clefru added a commit to clefru/nixpkgs that referenced this pull request Oct 17, 2018
* nat/bind/dhcp.service:
  Remove. Those services have nothing to do with a link-level service.

* sys-subsystem-net-devices-${if}.device:
  Add as BindsTo dependency as this will make hostapd stop when the
  device is unplugged.

* network-link-${if}.service:
  Add hostapd as dependency for this service via requiredBy clause,
  so that the network link is only considered to be established
  only after hostapd has started.

* network.target:
  Remove this from wantedBy clause as this is already implied from
  dependencies stacked above hostapd. And if it's not implied than
  starting hostapd is not required for this particular network
  configuration.

(cherry picked from commit 725fcde)
Mic92 added a commit that referenced this pull request Oct 17, 2018
Fix hostapd's place in systemd dependency tree. (#45464)
@lopsided98
Copy link
Contributor

This broke hostapd when using systemd-networkd, because network-link-${cfg.interface}.service does not exist, so the hostapd never starts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants