<div dir="ltr"><div>Hey James,</div><div><br></div><div>Thanks! Responses below<br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Dec 1, 2021 at 1:12 PM James Feeney <<a href="mailto:james@nurealm.net" target="_blank">james@nurealm.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On 12/1/21 07:20, Brian Hutchinson wrote:<br>
> ...<br>
> In .system file I tried all I know to ensure the required interfaces were created before starting ptp4l in attempt to give bonding enough time to finish but binding to things like sys-subsystem-net-devices-bond1.device wasn't enough.<br>
> <br>
> Is it also possible to use carrier state in .service file?<br>
> <br>
> I see sys/devices/virtual/net/bond1/carrier but not sure how to only attempt to start my ptp4l service after carrier state is "1".<br>
> <br>
> I welcome your ideas and suggestions on how to start a service after a bond interface is really up.<br>
<br>
With systemd, the proper way to setup network bonding is to establish ordering with the use of "target" files, which can be added to /etc/systemd/system.<br>
<br>
The target files themselves need not contain anything, though I have these with simply:<br>
<br>
[Unit]<br>
Documentation= man:systemd.target(5)<br>
<br>
My configuration provides automatic bonding and bridging for removable/pluggable and fixed hardwired, wireless, and virtual interfaces, using hardlinked template files and a separate network configuration file, as /etc/conf.d/network, though you are only looking for bonding here. The big advantage with using systemd as the network configuration system, compared to alternatives, is that it "just works", and doesn't break after someone else's "upgrade".<br></blockquote><div><br></div><div>Your hardware situation is certainly more interesting than mine with hotplug stuff ... in the old days I had to do udev rules for stuff like that but with this project I decided to finally go with systemd. For the most part it does "just work" until it "doesn't" and I've ran into that quite a few times now and this is one of those cases (note I'm on linux-fslc-imx 5.10.69 and I understand some bonding issues have been fixed in 5.11 but I don't think that fix pertains to what I'm seeing here). There are hooks to guarantee the "network is online" before going on ... and they don't work right in this case. You can see from my serial console log bond1 isn't up until after the login prompt and all the systemd targets have finished! And systemd was told not to start ptp4l until after the network is up and you can clearly see it being started before bond1 is up.</div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
The essential idea with configuring virtual network interfaces using systemd target files derives from noting that network service clients and servers must run After bridge and bond master interfaces are working, which implies After configuration of their respective slave interfaces, and that hardware devices can only be enslaved After the master interfaces have been created. These constraints imply the following ordering:<br>
<br>
1) master interfaces<br>
2) enslaved interfaces<br>
3) network services<br>
<br>
The systemd target files are then inferred between these three stages:<br>
<br>
a) master interfaces<br>
b) "go.target"<br>
c) enslaved interfaces<br>
d) "ll.target"<br>
e) network services<br>
<br>
The target file naming is arbitrary, of course. I use these names from arbitrarily choosing the point of view from the template file used to configure each slave device to each master, where finally "ip link set %P master %I".<br>
<br>
You could use the terminology "director" and "executive", from corporate structure lingo, instead of "master" and "slave", if preferred, but the ip command still uses the the terms "master" and "slave".<br>
<br>
A hardware network device Requires go.target and the master interface service file "master@.service" runs Before go.target:<br>
<br>
Requires= go.target<br>
Before= go.target<br>
<br>
Plugging network hardware, then, will trigger the entire chain of configuration events.<br>
<br>
BindsTo= sys-subsystem-net-devices-%i.device<br>
<br>
Similarly, for the enslaved interface service file "enslaved@.service":<br>
<br>
Requires= go.target<br>
After= go.target<br>
Before= ll.target<br>
<br>
And finally, for the various network services service template files:<br>
<br>
PartOf= ll.target<br>
Requires= ll.target<br>
After= ll.target<br>
<br>
That's the basic idea. Of course, there are plenty of "housekeeping" details in practice. In particular, "Requisite" fails to recognize device units, and instead,<br>
<br>
ConditionPathExists= /sys/class/net/%I<br>
<br>
is necessary. This appears to me to be an unjustified bug with "Requisite", but - you know - Lennart.<br>
<br>
Altogether, to trigger configuration of both master and slave devices from "enslaved@.service":<br>
<br>
BindsTo= sys-subsystem-net-devices-%p.device<br>
ConditionPathExists= /sys/class/net/%P<br>
BindsTo= sys-subsystem-net-devices-%i.device<br>
<br>
It is useful to impose an arbitrary but strict naming convention with these files, to allow use of systemd specifiers and template files. In your case, you might simply hard-code what you want, if you are not looking for a generic solution, and all you want is bonding on a couple of interfaces.<br>
<br>
Still, when properly setup, you can individually "start" and "stop" any of the target units or network service units and get correct behavior.<br></blockquote><div><br></div><div>Maybe I'm missing something here but I don't see any way for me to "add targets" to this problem to solve it unless I abandon the systemd way of setting up the bond and wrap my "command line" way of creating the bond (with echo and ip commands) with .target and .service files ... which is going back to init scripts basically. <br></div><div><br></div><div>I guess I could make a .service that calls an ExecCondition= script that could see if<span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"> <span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">/sys/devices/virtual/net/bond1</span>/carrier = 1<br></span>AND (<span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">/sys/bus/i2c/devices/0-005f/net/lan1/carrier = 1</span></span> OR /sys/bus/i2c/devices/0-005f/net/lan2/carrier = 1)</span></span></div><div><br></div><div>... and start my ptp4l service after that.</div><div><br></div><div>But even that would probably need to Restart=on-failure like I have now if those interfaces aren't up yet.</div><div><br></div><div>I guess I'm just having a bit of buyer's remorse for believing I could rely on network-online.target before going on ... and I can't. <a href="https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/">https://www.freedesktop.org/wiki/Software/systemd/NetworkTarget/</a> "If you are a developer, instead of wondering what to do about <code>network.target</code>,
please just fix your program to be friendly to dynamically changing
network configuration. That way you will make your users happy because
things just start to work, and you will get fewer bug reports as your
stuff is just rock solid. You also make the boot faster for your users,
as they don't have to delay arbitrary services for the network anymore" ... I guess the systemd bonding implementor didn't abide by all that. ;)<br></div><div><br></div><div>I've read more and I've usurped the default action of <span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">systemd-networkd-wait-online.service to be more specific on which interfaces to wait on and what states they need to be in before moving on:</span></span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)"># SPDX-License-Identifier: LGPL-2.1+
</span><br>#
<br># This file is part of systemd.
<br>#
<br># systemd is free software; you can redistribute it and/or modify it
<br># under the terms of the GNU Lesser General Public License as published by
<br># the Free Software Foundation; either version 2.1 of the License, or
<br># (at your option) any later version.
<br>
<br>[Unit]
<br>Description=Wait for Network to be Configured
<br>Documentation=man:systemd-networkd-wait-online.service(8)
<br>DefaultDependencies=no
<br>Conflicts=shutdown.target
<br>Requires=systemd-networkd.service
<br>After=systemd-networkd.service
<br>Before=network-online.target shutdown.target
<br>
<br>[Service]
<br>Type=oneshot
<br>ExecStart=/lib/systemd/systemd-networkd-wait-online --interface bond1:degraded-carrier:carrier --interface lan1:carrier
<br>RemainAfterExit=yes
<br>
<br>[Install]
<br>WantedBy=network-online.target<br></span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace">... and it still doesn't work ... you can clearly see "Sync Microchip PHC with PTP Grand Master Clock" (my ptp4l.service) being called before the bond1 is online ... which doesn't happen until after the login prompt:</span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace"><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[ OK ] Reached target </span><span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Network</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">.
</span><br>[ 4.096782] imx-sdma 302c0000.dma-controller: firmware found.
<br>[ OK ] Reached targe[ 4.104764] imx-sdma 302c0000.dma-controller: loaded firmware 4.5
<br>t <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Network is Online</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">.[ 4.109828] caam-snvs 30370000.caam-snvs: violation handlers armed - init state
</span><br>
<br>[ OK ] Reached target <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Host and Network Name Lookups</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">.
</span><br> Starting <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Avahi mDNS/DNS-SD Stack</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">...
</span><br> Starting <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Enable ksz9567</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">...
</span><br> Starting <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">The NGINX HTTP and reverse proxy server</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">...
</span><br> Starting <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Sync M[ 4.189072] imx-sdma 302b0000.dma-controller: firmware found.</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">
</span><br><span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">icrochip PH…with PTP Grand Master Clock</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">...
</span><br>[ OK ] Started <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Enable ksz9567</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">.
</span><br>[<span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">FAILED</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">] Failed to start </span><span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Sync Micro…C with PTP Grand Master Clock</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">.
</span><br>See 'systemctl status ptp4l.service' for details.
<br>[ OK ] Started <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">The NGINX HTTP and reverse proxy server</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">.
</span><br>[ 4.254479] imx-sdma 30bd0000.dma-controller: firmware found.
<br>[ OK ] Started <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Avahi mDNS/DNS-SD Stack</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">.
</span><br>[ 4.413378] ksz9477-switch 0-005f lan1: configuring for phy/gmii link mode
<br>[ 4.427011] bond1: (slave lan1): Enslaving as a backup interface with a down link
<br>[ 4.501283] ksz9477-switch 0-005f lan2: configuring for phy/gmii link mode
<br>[ 4.511903] bond1: (slave lan2): Enslaving as a backup interface with a down link
<br> Starting <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Save/Restore Sound Card State</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">...
</span><br>[ OK ] Started <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Save/Restore Sound Card State</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">.
</span><br>[ OK ] Reached target <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Sound Card</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">.
</span><br>[ 5.009993] random: crng init done
<br>[ 5.013414] random: 7 urandom warning(s) missed due to ratelimiting
<br>[ OK ] Started <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Load/Save Random Seed</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">.
</span><br>[ OK ] Started <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">System Logger Daemon "default" instance</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">.
</span><br>[ OK ] Reached target <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Multi-User System</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">.
</span><br> Starting <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Update UTMP about System Runlevel Changes</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">...
</span><br>[ OK ] Started <span style="font-weight:bold;color:rgb(0,0,0);background-color:rgb(255,255,255)">Update UTMP about System Runlevel Changes</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">.
</span><br>
<br>Poky (Yocto Project Reference Distro) 3.1.7 imx8mmevk ttymxc1
<br>
<br>imx8mmevk login: [ 7.531146] ksz9477-switch 0-005f lan1: Link is Up - 1Gbps/Full - flow control rx/tx
<br>[ 8.873069] bond1: (slave lan1): link status definitely up, 1000 Mbps full duplex
<br>[ 8.882016] bond1: (slave lan1): making interface the new active one
<br>[ 8.892488] device eth0 entered promiscuous mode
<br>[ 8.897180] audit: type=1700 audit(1600598644.664:2): dev=eth0 prom=256 old_prom=0 auid=4294967295 uid=0 gid=0 ses=4294967295
<br>[ 8.913688] bond1: active interface up!
<br>[ 8.917595] IPv6: ADDRCONF(NETDEV_CHANGE): bond1: link becomes ready<br></span></span></div><div><span style="font-family:monospace"></span></div><div><span style="font-family:monospace"></span></div><div><br></div><div>systemctl status ptp4l</div><div><br></div><div><span style="font-family:monospace"><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;31m*</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0m ptp4l.service - Sync Microchip PHC with PTP Grand Master Clock
</span><br> Loaded: loaded (/etc/systemd/system/ptp4l.service; enabled; vendor preset: disabled)
<br> Active: <span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;31mfailed</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0m (Result: exit-code) since Sun 2020-09-20 10:44:01 UTC; 40s ago
</span><br> Process: 332 ExecStart=/usr/bin/ptp4l -f /etc/linuxptp/ptp4l.conf_e2e_one_step_g8275.2 -s -i bond1 <span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;31m(code=exited, status=255/EXCEPTION)</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0m
</span><br> Main PID: 332 (code=exited, status=255/EXCEPTION)
<br>
<br>Sep 20 10:44:01 imx8mmevk systemd[1]: Starting Sync Microchip PHC with PTP Grand Master Clock...
<br>Sep 20 10:44:01 imx8mmevk ptp4l[332]: <span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;31m</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;39m</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;31m[5.601] interface 'bond1' does not support requested timestamping mode</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0m
</span><br>Sep 20 10:44:01 imx8mmevk ptp4l[332]: failed to create a clock
<br>Sep 20 10:44:01 imx8mmevk systemd[1]: <span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;39m</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;31m</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;39mptp4l.service: Main process exited, code=exited, status=255/EXCEPTION</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0m
</span><br>Sep 20 10:44:01 imx8mmevk systemd[1]: <span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;38;5;185m</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;39m</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;38;5;185mptp4l.service: Failed with result 'exit-code'.</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0m
</span><br>Sep 20 10:44:01 imx8mmevk systemd[1]: <span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;31m</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;39m</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0;1;31mFailed to start Sync Microchip PHC with PTP Grand Master Clock.</span><span style="color:rgb(255,255,255);background-color:rgb(0,0,0)">[</span><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">[0m</span><br></span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace"><span style="font-family:monospace"><span style="color:rgb(0,0,0);background-color:rgb(255,255,255)">cat ptp4l.service </span><br>[Unit]
<br>Description=Sync Microchip PHC with PTP Grand Master Clock
<br>#Requires=network-online.target multi-user.target
<br>#BindsTo=sys-subsystem-net-devices-bond1.device sys-subsystem-net-devices-lan1.device sys-subsystem-net-devices-lan2.device multi-user.target
<br>#After=sys-subsystem-net-devices-bond1.device sys-subsystem-net-devices-lan1.device sys-subsystem-net-devices-lan2.device multi-user.target
<br>After=network-online.target
<br>Wants=network-online.target
<br>
<br>[Service]
<br>Type=exec
<br>#NotifyAccess=all
<br>ExecStart=/usr/bin/ptp4l -f /etc/linuxptp/ptp4l.conf_e2e_one_step_g8275.2 -s -i bond1
<br>#Restart=on-failure
<br>#RestartSec=1
<br>
<br>[Install]
<br>WantedBy=multi-user.target<br></span></span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace">...but after logging in and running systemctl restart ptp4l everything works. This is a straight up race condition during startup ... and I don't know how to fix it the "systemd" way. Am I doing something wrong or is something in systemd bonding broken???<br></span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace">Regards,</span></div><div><span style="font-family:monospace"><br></span></div><div><span style="font-family:monospace">Brian<br></span></div><div><br></div></div></div>