Iproute2 Basics – Linux Tools for TCP / IP Networking, Traffic and Control

By | 25/02/2013

Iproute2 (or Iproute) consists of a variety of utilities for controlling TCP / IP networking and traffic control in Linux. It is actively maintained and frequently updated, and now considered the preferred package for modern network technologies which includes important network tools such as ip and tc, used for IPv4 and IPv6 configuration and traffic control. It currently replaces the outdated Net-Tools package encompassing commands such as ifconfig, route, arp, etc, which are now known to be mostly inadequate as they provide limited features.

To give you an idea just how obsolete the Net-Tools package is, it should be noted the last verified update was back in April, 2001. A common view among many developers and users is that these former Net-Tools utilities should be deprecated, however, most Linux distros still include them as old habits are hard to break. It is suggested, now is the time to familiarize yourself with some of the most common commands of the newer, feature packed Iproute2 utilities package to be more current with the times.

 

Iproute2 Basics

 

Common Iproute2 Commands – IP Basic Usage and Syntax

The “ip” command consists of the some following subsets (only some of the most common are listed for this tutorial) and shown with the older [Net-Tools equivalent] in brackets:

ip address [ifconfig], ip link, ip route [route or netstat -rn], ip neighbour [arp]

As you will see, these ip commands can be abbreviated, for example, ip address to ip addr, and so on.

Syntax (Source – IP man page):

ip [ OPTIONS ] OBJECT { COMMAND | help }

OBJECT := { link | addr | addrlabel | route | rule | neigh | ntable | tunnel |
tuntap | maddr | mroute | mrule | monitor | xfrm | netns | l2tp }

OPTIONS := { -V[ersion] | -s[tatistics] | -r[esolve] | -f[amily] { inet | inet6 |
ipx | dnet | link } | -o[neline] }

OBJECT
address – protocol (IP or IPv6) address on a device.
addrlabel – label configuration for protocol address selection.
l2tp – tunnel ethernet over IP (L2TPv3).
link – network device.
maddress – multicast address.
monitor – watch for netlink messages.
mroute – multicast routing cache entry.
mrule – rule in multicast routing policy database.
neighbour – manage ARP or NDISC cache entries.
netns – manage network namespaces.
ntable – manage the neighbor cache’s operation.
route – routing table entry.
rule – rule in routing policy database.
tunnel – tunnel over IP.
tuntap – manage TUN/TAP devices.
xfrm – manage IPSec policies.

The names of all objects may be written in full or abbreviated form, f.e.
address is abbreviated as addr or just a.

COMMAND
Specifies the action to perform on the object. The set of possible actions
depends on the object type. As a rule, it is possible to add, delete and show
(or list ) objects, but some objects do not allow all of these operations or have
some additional commands. The help command is available for all objects. It prints
out a list of available commands and argument syntax conventions.

Note: When experimenting with these commands for the purpose of this tutorial, please execute them in ‘root’, as they will both ‘set’ the given configuration and ‘list’ it.

 

Usage and Examples

We will now run through an actual example of starting off with one NIC (Network Interface Card), then adding another. Upon installing a new Linux distribution or NIC, the first task which comes to mind is to display a comprehensive list of all the the NICs on your system, the Label names, IP addresses, Broadcasting addresses, MAC addresses, Link status, State status, and much more.

Let us assume the computer we are using initially has one pci based NIC connected to a router with internet.

To list the protocol addresses with all relevant information:

# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:26:22:35:59:5f brd ff:ff:ff:ff:ff:ff
    inet 192.168.44.9/24 brd 192.168.44.255 scope global eth0
    inet6 fe80::226:22ff:fe35:595f/64 scope link
       valid_lft forever preferred_lft forever
4: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 70:1a:04:01:f9:62 brd ff:ff:ff:ff:ff:ff

Note the link status of the device eth0 is UP, and so is the state as there is an active internet connection.

To display the contents of the routing tables or the route(s):

# ip route show
default via 192.168.44.1 dev eth0
192.168.44.0/24 dev eth0  proto kernel  scope link  src 192.168.44.9

To display the current neighbour table in the kernel with extra statistics:

# ip -s neighbor show
192.168.44.1 dev eth0 lladdr 00:30:05:1b:cd:7c ref 1 used 84/8/3 probes 1 REACHABLE

Now, we physically add an additional usb based NIC to the computer.

Display the protocol addresses and info again:

# ip addr show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
    link/ether 00:26:22:35:59:5f brd ff:ff:ff:ff:ff:ff
    inet 192.168.44.9/24 brd 192.168.44.255 scope global eth0
    inet6 fe80::226:22ff:fe35:595f/64 scope link
       valid_lft forever preferred_lft forever
4: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 70:1a:04:01:f9:62 brd ff:ff:ff:ff:ff:ff
5: eth1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN qlen 1000
    link/ether 00:60:6e:6f:02:56 brd ff:ff:ff:ff:ff:ff

Our new NIC labelled eth1 is now visible, however, there are two noticeable items: The device needs to be enabled to UP, and both an IP and Broadcasting address needs to be assigned. This can be done easily in the next steps.

Set the link ‘UP’:

# ip link set eth1 up

It can be brought down by using the same link command followed by ‘down’. Let’s now assign this device both an IP and Broadcasting address.

Now display the dev protocol info for eth1:

# ip addr show dev eth1
5: eth1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:60:6e:6f:02:56 brd ff:ff:ff:ff:ff:ff

Notice after ‘MULTICAST’ our new device eth1 is currently ‘UP’.

Assign the ip and broadcasting address:

# ip addr add 192.168.45.2/24 brd + dev eth1

We use ‘add’ and include the new address of the interface. The format of the address depends on the protocol. It is a dotted quad for IP and a sequence of hexadecimal halfwords separated by colons for IPv6. The ADDRESS may be followed by a slash and a decimal number which encodes the network prefix length, in this case “/24″, etc. Then a Broadcast address is added with the abbreviated “brd” followed by an actual IP address. In this particular example, it is convenient to use the special symbols ‘+’ and ‘-’ instead of the broadcast address. In this case, the broadcast address is derived by setting/resetting the host bits of the interface prefix.

Keep in mind, it is possible to have several different addresses attached to one device using ‘add’. It is possible of course to remove ip and broadcasting addresses by using ‘del’ (delete) instead of ‘add’.

Let’s display our device information once again:

# ip addr show dev eth1
5: eth1: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
    link/ether 00:60:6e:6f:02:56 brd ff:ff:ff:ff:ff:ff
    inet 192.168.45.2/24 brd 192.168.45.255 scope global eth1

There you go, the new eth1 device has been set up and ready to go.

Using some additional route commands, it is also possible to add or delete addresses to a router by using the following commands, given there is a network:

# ip route add default via 192.168.12.55 dev eth1

This adds a default route (for all addresses) via the local gateway 192.168.12.55 that can be reached on device eth1.

or use:

# ip route add 192.168.45.5/24 via 192.168.12.55 dev eth1

This adds a specific address via the local gateway.

 

Traffic control through Iproute2 TC Tool

One other more complex Iproute2 tool that will only be touched upon and briefly described is the ‘tc’ (traffic control) command. This is for the more advanced network administrator, and used to configure Traffic Control in the Linux kernel. Traffic Control consists of the following:

SHAPING – When traffic is shaped, its rate of transmission is under control. Shaping may be more than lowering the available bandwidth – it is also used to smooth out bursts in traffic for better network behaviour. Shaping occurs on egress.

SCHEDULING – By scheduling the transmission of packets it is possible to improve interactivity for traffic that needs it while still guaranteeing bandwidth to bulk transfers. Reordering is also called prioritizing, and happens only on egress.

POLICING – Where shaping deals with transmission of traffic, policing pertains to traffic arriving. Policing thus occurs on ingress.

DROPPING – Traffic exceeding a set bandwidth may also be dropped forthwith, both on ingress and on egress.

 

Processing of traffic is controlled by three kinds of objects: qdiscs, classes and filters.

QDISCS – qdisc is short for ‘queueing discipline’ and it is elementary to understanding traffic control.

CLASSES – Some qdiscs can contain classes, which contain further qdiscs – traffic may then be enqueued in any of the inner qdiscs, which are within the classes.

FILTERS – A filter is used by a classful qdisc to determine in which class a packet will be enqueued.

 

A basic example to display ‘qdisc’ information:

# tc -s -d qdisc show dev eth0
qdisc pfifo_fast 0: root refcnt 2 bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
Sent 27370146 bytes 195120 pkt (dropped 0, overlimits 0 requeues 0)
backlog 0b 0p requeues 0

Conclusion

This Iproute2 tutorial only touched upon some of the numerous commands and options that are available to perform almost any custom task involving networking, traffic, and control. With this introduction, you should now possess the skills to perform the basic tasks presented. For more complex options, you may want to consult the man pages or have a look at the “Guide to IP Layer Network Administration with Linux”, at http://linux-ip.net/. It is comprehensive with detailed examples. Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *