Reduce DHCP requests to ISP

From BubbaWiki
Jump to: navigation, search

Background

The B3 when running in “router, firewall and server” mode asks, in most cases, the ISP (Internet Service Provider) for an IP address using the DHCP protocol and when running in “server only” mode it asks, in most cases, the local DHCP server for an IP address. The responses to the DHCP requests will update a large number of files on the file system and hence wake up the harddisk.

If you would like to have the harddisk in sleep as often as possible the DHCP request intervals should be as long as possible.

Software baseline

This description assumes and is verified on Bubba sw release 2.5.0.2

Configuration Information

You need to save the command and options that is used to start the DHCP server since we need to restart it when the modifications are complete. Login to the B3 and execute the following commands.

ps -ef|grep dhclient

On my B3 this results in the following result

root 698 1 0 Aug27 ? 00:00:00 dhclient -v -pf /var/run/dhclient.eth0.pid -lf /var/lib/dhcp/dhclient.eth0.leases eth0
morgan 29147 639 0 06:08 pts/1 00:00:00 grep dhclient

and it is the first line that we are interested in. 698 is the process id of the dhclient, this we will need later. We will also need the whole command for starting the dhclient.

Packages

To make all the improvements we need to modify the source code for the dhclient and hence we need to fetch it and the packages needed to compile it and verify the changes. Login to the B3 and execute the following commands

su
change_distribution -sunc elvin
apt-get update
apt-get install build-essential tcpdump

Setup

Since a DHCP server in some cases have a default lease time that is significantly shorter than the maximum allowed one we need to explicitly request the DHCP server for a long lease time. This is done by enhancing the dhclient configuration file

Login to the B3 and become the root user

su

Go to the dhclient configuration folder, make a backup and open the file

cd /etc/dhcp/
cp dhclient.conf{,.orig}
nano dhclient.conf

Add the following text into the file

interface "eth0" {
send dhcp-lease-time 172800;
}

If there already is a interface “eth0” block defined add the “send dh...” row to the existing block. 172800 is the requested lease time in seconds and corresponds to 48 hours.

To ensure that the client and the server have the same understanding on the renewal and rebinding timers we also add “ dhcp-rebinding-time, dhcp-renewal-time” to the request statement in the same file. On my B3 this results in that the request statement have the following content.

request subnet-mask, broadcast-address, time-offset, routers,
domain-name, domain-name-servers, host-name,
netbios-name-servers, netbios-scope,
dhcp-rebinding-time, dhcp-renewal-time;

Modifying the dhclient

For some DHCP servers the above changes is enough and the modification of the dhclient source code is not necessary. Since it depends on your ISPs DHCP settings you could first try without modifying the dhclient and do it later if required by your ISP to get the optimal performance.

First exit the root user and perform the modifications as a “normal” user.

exit

fetch the source code of dhclient in a new folder.

cd ~
mkdir development
cd development
apt-get source isc-dhcp-client

go into the client folder

cd isc-dhcp-4.1.1-P1/client

modify the dhclient.c file so that the function dhcpack have the new syntax when it comes to renewal logic.

Original code

/* Now introduce some randomness to the renewal time: */
if (client->new->renewal <= ((TIME_MAX / 3) - 3))
client->new->renewal = (((client->new->renewal * 3) + 3) / 4) +
(((random() % client->new->renewal) + 3) / 4);

New code

/* Now introduce some randomness to the renewal time: */
if (client->new->renewal <= ((TIME_MAX / 3) - 3))
client->new->renewal = client->new->renewal +
(random() % client->new->renewal) / 10);

This will “move” the randomness from 75-100% of the renewal time to 100-110% of the renewal time, e.g. we do not ask for a new IP address until the server renewal time have expired. The reason for this is that some DHCP servers to not update the renewal time if requests comes before it have expired.

Now we need to configure the build environment and build the binaries

cd ..
./configure
make

To install the new client we simply copy it to the correct location (as root user)

su
cp /sbin/dhclient{,.orig}
chmod go-w client/dhclient
cp client/dhclient /sbin/.

Restart dhclient

To stop the dhclient we must know its process id. As seen above my process id was 698, yours will be different. It is vital that you use the correct process id, if you use a wrong one you might in worst case stop a vital process on B3 and be forced to pull the power cord (which is not good) to get it usable again. To stop the dhclient execute the command below and replace NNN with the process id of dhclient.

kill NNN

Now we need to start the process again. To allow the process to continue to live after we have closed the shell we used to start it we must use the “nohup” command to start it and we should also divert output to /dev/null to avoid having a file with the output of the command. NOTE: If your dhclient have other options than in this example it is vital that you start dhclient with your parameters and not the ones below.

nohup dhclient -v -pf /var/run/dhclient.eth0.pid -lf /var/lib/dhcp/dhclient.eth0.leases eth0 >/dev/null

Do not forget “nohup” and “>/dev/null”

Test

To test the updates we capture the relevant traffic on the eth0 interface. This log can be viewed later with the Wireshark application (Available for Win, Mac, Ubuntu, …)

cd ~
su
tcpdump -i eth0 -w DHCP.pcap port 67

This command will capture all traffic that have a source or destination port of 67, which all DHCP traffic have.

The tcpdump command should be running for at least 24 hours so that you get a capture of an update. To get a log from the initial DHCP request the tcpdump command could be started in a separate command window before the dhclient is restarted.

When 24 hours have passed stop the tcpdump application by pressing Ctrl-C. The DHCP.pcap file can now be opened by Wireshark. To filter out DHCP broadcast traffic add a filter in wireshark that have the following syntax ip.addr=<your eth0 IP address>

Your eth0 IP address can be found by the following commands

su
ifconfig eth0

In the DHCP ACK packets you can expand the “bootstrap protocol” and the option “IP Address Lease Time” will tell you the time your ISP is allowing you to lease an IP address. If everything is working as it should this time should be the same in all DHCP ACK packets. If you have not updated the dhclient the value can be smaller in every second packet. If this is the case and you would like to squeeze the harddisk sleep times you need to modify the dhclient application as described above.

NOTE: It is a good idea to perform the test before any changes so that you know the initial status of you DHCP traffic. Maybe you already have really good performance, e.g. long intervals.

Remove packages

The configuration of the distribution as well as the installed packages are not needed once the modifications are done. The commands below will remove the packages.

su
apt-get purge build-essential tcpdump
change_distribution -unc elvin
apt-get update

New SW for B3

When the Bubba software is updated it is likely that this procedure have to be redone since the edited files are included in the releases from Excito.