Wait for carrier in /etc/rc.d/defaultroute

Brooks Davis brooks at freebsd.org
Sat Sep 25 18:54:49 UTC 2010


On Fri, Sep 24, 2010 at 08:04:35PM -0400, Ed Maste wrote:
> /etc/rc.d/defaultroute currently bails immediately if all interfaces
> set to use DHCP have no carrier.  This caused grief at work as it takes
> some time for link to be established, and defaultroute ran before this 
> happened.  The rest of rc.d/ ran then before DHCP could assign an
> address and route.
> 
> The attached patch introduces a defaultroute_carrier_delay variable
> and a change to /etc/rc.d/defaultroute to wait that long before bailing
> out if there are no interfaces with carrier.  With the default settings
> defaultroute will wait for five seconds to see if any interface gets 
> carrier.  The original 30 second wait for a default route to appear is
> unchanged.
> 
> Any comments?  I'll commit it sometime next week if there's no concern.

This seems like a reasionable solution.  Just checking for carrier
didn't work as well as I'd hoped.  Have you tested this on a bridge or
similar interface?

It seems like in some senses there's probably something more the nic
could be telling us about it's state so we could do a better job here.
What we really want to know is if we have any dhcp interfaces that have
a chance of getting a lease any time soon so if the interface could say "I think I've got a
cable attached" that would be useful.

-- Brooks

> -Ed

> Index: etc/defaults/rc.conf
> ===================================================================
> --- etc/defaults/rc.conf	(revision 213127)
> +++ etc/defaults/rc.conf	(working copy)
> @@ -108,6 +108,7 @@
>  synchronous_dhclient="NO"	# Start dhclient directly on configured
>  				# interfaces during startup.
>  defaultroute_delay="30"		# Time to wait for a default route on a DHCP interface.
> +defaultroute_carrier_delay="5"	# Time to wait for carrier while waiting for a default route.
>  wpa_supplicant_program="/usr/sbin/wpa_supplicant"
>  wpa_supplicant_flags="-s"	# Extra flags to pass to wpa_supplicant
>  wpa_supplicant_conf_file="/etc/wpa_supplicant.conf"
> Index: etc/rc.d/defaultroute
> ===================================================================
> --- etc/rc.d/defaultroute	(revision 213127)
> +++ etc/rc.d/defaultroute	(working copy)
> @@ -1,6 +1,6 @@
>  #!/bin/sh
>  #
> -# Wait for the default route to be up
> +# Wait for the default route to be up if DHCP is in use
>  #
>  # $FreeBSD$
>  #
> @@ -16,9 +16,23 @@
>  start_cmd="defaultroute_start"
>  stop_cmd=":"
>  
> +# Does any interface have a carrier?
> +defaultroute_carrier()
> +{
> +	local carrier nocarrier
> +
> +	carrier=1
> +	for _if in ${dhcp_interfaces}; do
> +		output=`/sbin/ifconfig ${_if}`
> +		nocarrier=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'`
> +		[ -z "${nocarrier}" ] && carrier=0
> +	done
> +	return ${carrier}
> +}
> +
>  defaultroute_start()
>  {
> -	local output carrier nocarrier nl
> +	local nl waited
>  
>  	afexists inet || return 0
>  
> @@ -26,35 +40,30 @@
>  	# if none of the dhcp interfaces is plugged in.
>  	dhcp_interfaces=`list_net_interfaces dhcp`
>  	[ -z "${dhcp_interfaces}" ] && return
> -	carrier=false
> -	for _if in ${dhcp_interfaces}; do
> -		output=`/sbin/ifconfig ${_if}`
> -		nocarrier=`expr "${output}" : '.*[[:blank:]]status: \(no carrier\)'`
> -		[ -z "${nocarrier}" ] && carrier=true
> -	done
> -	if ! ${carrier}; then
> -		return
> -	fi
>  
>  	# Wait for a default route
> -	delay=${defaultroute_delay}
> -	while [ ${delay} -gt 0 ]; do
> +	waited=0
> +	while [ ${waited} -lt ${defaultroute_delay} ]; do
>  		defif=`get_default_if -inet`
>  		if [ -n "${defif}" ]; then
> -			if [ ${delay} -ne ${defaultroute_delay} ]; then
> +			if [ ${waited} -ne 0 ]; then
>  				echo -n "($defif)"
>  				nl=1
>  			fi
>  			break
>  		fi
> -		if [ ${delay} -eq ${defaultroute_delay} ]; then
> -			echo -n "Waiting ${delay}s for the default route interface: "
> +		if [ ${waited} -eq 0 ]; then
> +			echo -n "Waiting ${defaultroute_delay}s for the default route interface: "
>  		else
>  			echo -n .
>  		fi
> +		if [ ${waited} -eq ${defaultroute_carrier_delay} ] && ! defaultroute_carrier; then
> +			echo -n "(no carrier)"
> +			break
> +		fi
>  		nl=1
>  		sleep 1
> -		delay=$(($delay - 1))
> +		waited=$(($waited + 1))
>  	done
>  
>  	[ -n "$nl" ] && echo

> _______________________________________________
> freebsd-rc at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-rc
> To unsubscribe, send any mail to "freebsd-rc-unsubscribe at freebsd.org"

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 187 bytes
Desc: not available
Url : http://lists.freebsd.org/pipermail/freebsd-rc/attachments/20100925/5ada0d24/attachment.pgp


More information about the freebsd-rc mailing list