This makes it more consistent with nettools' lease_to_ip4_config(). The benefit of having a self pointer, is that it provides the necessary context for logging. Without it, these functions cannot correctly log. At this point, it's clearer to get the necessary data directly from the DHCP client instance, instead of having the caller passing them on (redundantly). |
||
|---|---|---|
| .. | ||
| tests | ||
| meson.build | ||
| nm-dhcp-client-logging.h | ||
| nm-dhcp-client.c | ||
| nm-dhcp-client.h | ||
| nm-dhcp-dhclient-utils.c | ||
| nm-dhcp-dhclient-utils.h | ||
| nm-dhcp-dhclient.c | ||
| nm-dhcp-dhcpcanon.c | ||
| nm-dhcp-dhcpcd.c | ||
| nm-dhcp-helper-api.h | ||
| nm-dhcp-helper.c | ||
| nm-dhcp-listener.c | ||
| nm-dhcp-listener.h | ||
| nm-dhcp-manager.c | ||
| nm-dhcp-manager.h | ||
| nm-dhcp-nettools.c | ||
| nm-dhcp-options.c | ||
| nm-dhcp-options.h | ||
| nm-dhcp-systemd.c | ||
| nm-dhcp-utils.c | ||
| nm-dhcp-utils.h | ||
| README.next.md | ||
NMDhcpClient
Using NMDhcpClient still requires a lot of logic in NMDevice. The main goal
is to simplify NMDevice, so NMDhcpClient must become more complicated to
provide a simpler (but robust) API.
NMDevice has basically two timeouts (talking about IPv4, but it applies
similarly to IPv6): ipv4.dhcp-timeout and ipv4.required-timeout. They
control how long NMDevice is willing to try, before failing the activation
altogether. Note that with ipv4.may-fail=yes, we may very well never want to
fail the activation entirely, regardless how DHCP is doing. In that case we
want to stay up, but also constantly retrying whether we cannot get a lease and
recover.
Currently, if NMDhcpClient signals a failure, then it's basically up to
NMDevice to schedule and retry. That is complicated, and we should move the
complexity out of NMDevice.
NMDhcpClient should have a simpler API:
-
nm_dhcp_manager_start_ip[46](): creates (and starts) aNMDhcpClientinstance. The difference is, this function tries really hard not to fail to create anNMDhcpClient. There is no explicitstart(), but note that the instance must not emit any signals before the next maincontext iteration. That is, it only will call back the user after a timeout/idle or some other IO event, which happens during a future iteration of the maincontext. -
nm_dhcp_client_stop(): whenNMDeviceis done with theNMDhcpClientinstance, it will stop it and throw it away. This method exists becauseNMDhcpClientis aGObjectand ref-counted. Thus, we don't want to rely on the last unref to stop the instance, but have an explicit stop. After stop, the instance is defunct and won't emit any signals anymore. The class does not need to support restarting a stopped instance. IfNMDevicewants to restart DHCP, it should create a new one.NMDevicewould only want to do that, if the parameters change, hence a new instance is in order (and no need for the complexity of restart inNMDhcpClient). -
as already now,
NMDhcpClientis not very configurable. You provide most (all) parameters duringnm_dhcp_manager_start_ip[46](), and then it keeps running until stop. -
NMDhcpClientexposes a simple state to the user:-
"no lease, but good". When starting, there is no lease, but we are optimistic to get one. This is the inital state, but we can also get back to this state after we had a lease (which might expire).
-
"has a lease". Here there is no need to distinguish whether the current lease was the first we received, or whether this was an update. In this state, the instance has a lease and we are good.
-
"no lease, but bad".
NMDhcpClienttries really hard, and "bad" does not mean that it gave up. It will keep retrying, it's just that there is little hope of getting a new lease. This happens, when you try to run DHCP on a Layer3 link (WireGuard). There is little hope to succeed, butNMDhcpClient(theoretically) will retry and may recover from this. Another example is when we fail to start dhclient because it's not installed. In that case, we are not optimistic to recover, howeverNMDhcpDhclientwill retry (with backoff timeout) and might still recover from this. For most cases,NMDevicewill treat the no-lease cases the same, but in case of "bad" it might give up earlier.
-
When a lease expires, that does not necessarily mean that we are now in a bad
state. It might mean that the DHCP server is temporarily down, but we might
recover from that easily. "bad" really means, something is wrong on our side
which prevents us from getting a lease. Also, imagine dhclient dies (we would
try to restart, but assume that fails too), but we still have a valid lease,
then possibly NMDhcpClient should still pretend all is good and we still have
a lease until it expires. It may be we can recover before that happens. The
point of all of this, is to hide errors as much as possibly and automatically
recover. NMDevice will decide to tear down, if we didn't get a lease after
ipv4.dhcp-timeout. That's the main criteria, and it might not even
distinguish between "no lease, but good" and "no lease, but bad".
-
NMDhcpClientwill also take care of theipv4.dhcp-timeoutgrace period. That timeout is provided during start, and starts ticking whenever there is no lease. When it expires, a timeout signal gets emitted. That's it. This is independent from the 3 states above, and only savesNMDevicefrom scheduling this timer themselves. This is NM_DHCP_CLIENT_NOTIFY_TYPE_NO_LEASE_TIMEOUT notification. -
for nettools,
nm_dhcp_client_can_accept()indicates that when we receive a lease, we need to accept/decline it first. In that case,NMDeviceoptionally does ACD first, then configures the IP address first and callsnm_dhcp_client_accept(). In case of ACD conflict, it will callnm_dhcp_client_decline()(which optimally causesNMDhcpClientto get a different lease). With this, the above state "has a lease" has actually three flavors: "has a lease but not yet ACD probed" and "has a lease but accepted/declined" (butNM_DHCP_CLIENT_SIGNAL_STATE_CHANGEDgets only emitted when we get the lease, not when we accept/decline it). Withdhclient, when we receive a lease, it means "has a lease but accepted" right away. -
for IPv6 prefix delegation, there is also
needed_prefixesandNM_DHCP_CLIENT_NOTIFY_TYPE_PREFIX_DELEGATED. Currentlyneeded_prefixesneeds to be specified during start (which simplifies things). Maybeneeded_prefixesshould be changable at runtime. Otherwise, whether we have prefixes is similar to whether we have a lease, and the simple 3 states apply.
When NetworkManager quits, it may want to leave the interface up. In that case,
we still always want to stop the DHCP client, but possibly not deconfiguring
the interface. I don't think that this concerns NMDhcpClient, because NMDhcpClient
only provides the lease information and NMDevice is responsible to configure it.