Showing posts with label iproute2. Show all posts
Showing posts with label iproute2. Show all posts

Saturday, March 26, 2011

iproute2: Life after ifconfig

The standard network tools ifconfig, netstat and route will be familiar to anyone with more than a passing interest in UNIX or any of its derivations. Linux is no exception, and if you hop on to your nearest Linux machine, you’ll find these installed. However, for the past few years ifconfig and its ilk (often collectively referred to as net-tools) have been deprecated in favour of the iproute2 suite.
iproute2 is a suite of tools developed to unify the functions provided by the traditional tools in one place under the ip command. Interface configuration, routing and tunnelling can now all be configured and managed using the ip command.

Interface configuration

Historically, interfaces are managed using the ifconfig command, and to get an overview of the interfaces you’d type ifconfig -a. With iproute2, interfaces addressing is managed through the address subcommand – which, like the rest of the subcommands for iproute2 can be shortened Cisco IOS-style, as long as it’s unique. In theory this means you can use ip a, but the manual page refers to it as ip addr, which I’ll use here for clarity. So, the equivalent of ifconfig -a is the self-explanatory ip addr show, which if we’re not specifying a specific interface can be shortened to simply ip addr:-
[root@example ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 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: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc
      pfifo_fast state DOWN qlen 1000
    link/ether 00:d0:b7:2d:ce:cf brd ff:ff:ff:ff:ff:ff
    inet 192.0.2.1/24 brd 192.0.2.255 scope global eth0
Most of this should be self-explanatory, and everything you would see with ifconfig -a you’ll see with ip addr.
Bringing up eth0 on a Linux box would usually consist of doing the following:-
[root@example ~]# ifconfig eth0 up
[root@example ~]# ifconfig eth0 192.0.2.1 netmask 255.255.255.0
With iproute2, control of interfaces themselves – both physical and logical – is through the link subcommand. Bringing up eth0 can be done with:-
[root@example ~]# ip link set eth0 up
Managing the addresses on an interface is through the aforementioned addr subcommand, so using our example again, we’d do something like this to add an IP to eth0:-
[root@example ~]# ip addr add 192.0.2.1/24 dev eth0
I’ve used CIDR notation in this example, but you can use the normal dotted quad format for the netmask if you wish.
This also makes adding multiple IP addresses to interfaces really easy. To add 192.0.2.2 to our example eth0 interface, you’d just do:-
[root@example ~]# ip addr add 192.0.2.2/24 dev eth0
Showing the addresses on our eth0 interface only will show that both the addresses are now there:-
[root@example ~]# ip addr show dev eth0
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc
      pfifo_fast state DOWN qlen 1000
    link/ether 00:d0:b7:2d:ce:cf brd ff:ff:ff:ff:ff:ff
    inet 192.0.2.1/24 brd 192.0.2.255 scope global eth1
    inet 192.0.2.2/24 scope global secondary eth1
Removing an IP from an interface is also straightforward:-
[root@example ~#] ip addr del 192.0.2.2/24 dev eth0
Querying the interface again shows that 192.0.2.2 is no longer assigned to eth0:-
[root@example ~]# ip addr show dev eth0
2: eth0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc
      pfifo_fast state DOWN qlen 1000
    link/ether 00:d0:b7:2d:ce:cf brd ff:ff:ff:ff:ff:ff
    inet 192.0.2.1/24 brd 192.0.2.255 scope global eth1

Routing

Using netstat -rn is pretty much burned into the brains of most UNIX engineers, but luckily the iproute2 method is just as snappy. Routing management is handled with the route subcommand, and in line with addr and link, it can be shortened – ip r will work, but I usually settle for ip ro. The full command for showing the routing table is ip route show, but as with ip addr you can drop the show if you want to show the entire routing table:-
[root@example ~]# ip ro
192.0.2.0/24 dev eth0  proto kernel  scope link  src 192.0.2.1
default via 192.0.2.254 dev eth0
Adding and removing routes is accomplished with ip ro add and ip ro del respectively:-
[root@example ~]# ip ro add 10.0.0.0/16 via 192.0.2.253
[root@example ~]# ip ro del 10.0.0.0/16 via 192.0.2.253
One useful feature of ip route is the get function, which we can use to query the routing table for a particular network or address. In our example, querying for an address not on our local network shows that the route to it goes via our default gateway:-
[root@example ~]# ip ro get 1.2.3.4
1.2.3.4 via 192.0.2.254 dev eth0  src 192.0.2.1
    cache  mtu 1500 advmss 1460 hoplimit 64

Neighbours

arp -na is the traditional way you’d query the ARP table on a UNIX machine. You can accomplish this with iproute2 using ip neighbor (or ip neighbour for us not from the US), with ip n being the shortened extreme:-
[root@example ~]# ip neigh
192.0.2.3 dev eth0 lladdr 00:02:a5:1f:cb:2d REACHABLE
192.0.2.254 dev eth0 lladdr 00:09:43:bc:aa:80 REACHABLE
I’ll skip the example for this, but needless to say you can add and remove entries with ip neigh add and ip neigh del respectively.

A little helping hand

If you’re stuck, then the help argument can come in handy. If you specify help as an argument to ip itself, or to one of the subcommands, it’ll give you a quick overview of the options available. For example, for ip neighbor:-
[root@example ~]# ip neigh help
Usage: ip neigh { add | del | change | replace } { ADDR [ lladdr
          LLADDR ] [ nud { permanent | noarp | stale |
          reachable } ] | proxy ADDR } [ dev DEV ]
       ip neigh {show|flush} [ to PREFIX ] [ dev DEV ] [ nud STATE ]

Not forgetting IPv6…

I’ve purposely neglected to show any configuration of IPv6 addresses in this post, not because iproute2 can’t handle it, but for the exact opposite reason – the iproute2 suite will handle IPv6 addresses in exactly the same way as IPv4 addresses. All the commands used above can be used for both IPv4 and IPv6 configuration without any issues.
If there’s a reason you want to force the behaviour one way or the other, you can use the -4 and -6 switches. This isn’t needed normally, because when adding or removing an address, for example, iproute2 will happily recognise an IPv6 address instead of an IPv4 one. Where it does come in useful is if you want to limit the data returned in a query to just IPv6, or just IPv4. A real-world example of this is on one of my Linux machines, where ip -6 ro shows:-
[root@daedalus ~]# ip -6 ro
2001:470:XXXX:1::/64 dev eth0  proto kernel  metric 256  mtu 1500
  advmss 1440 hoplimit 0
fe80::/64 dev eth0  proto kernel  metric 256  mtu 1500 advmss 1440
  hoplimit 0
default via 2001:470:XXXX:1::1 dev eth0  metric 1  mtu 1500 advmss
  1440 hoplimit 0
…which comes in handy if you’re only interested in the IPv6 routing table.

What next?

This post only really scratches the surface of iproute2 – I’ve just covered the iproute2 equivalents of the most-used commands. It’s capable of much, much more, such as setting up tunnels, managing multiple routing tables and configuring interfaces for multicast to name a few. I’ll be covering some of these in more depth in future posts.

Further reading

Courtesy : http://andys.org.uk/bits/2010/02/24/iproute2-life-after-ifconfig/

Monday, September 6, 2010

Traffic shaping TorrentFlux

TorrentFlux is a great program/interface to download your torrents remotely on a linux machine. It is based on php and it uses a modified bittornado client to download the torrents.
The problem: The bittornado client is able to put specific limits on a per torrent basis. That means that you can put a 100kb/sec download and 50kb/sec upload limit per torrent through torrentflux’s web interface. If you have 20 torrents though, this easily becomes 20*50=1Mb/sec upload “limit”. There are cases that you don’t want this to happen and you want both a per torrent limit (eg 50kb/sec) and a global limit (eg 300kb/sec).
The solution: My solution is based on iptables, layer7 filter and tc (iproute2). I am using layer7 filter to pick out the bittorrent packets, iptables to mark those packets with specific values and tc to shape those marked packets into categories. Beware that the method I am using works mostly on the “uploading” part (outgoing traffic). It is not that hard to make it work for the incoming traffic as well, but it is my personal view that downloading with a few Mb/sec is not as harmfull as uploading with a few Mb/sec. I usually have my downloads seeded over many weeks…so it’s good for my ratio to have the torrent downloaded as fast as possible and then seed it endlessly. I usually like to seed until i get a ratio over 1000% per torrent (that means 10 times as much uploaded traffic than downloaded). The following example configs are created for use on a 100mbit line and keeping in mind that outgoing torrent traffic should not exceed 2-2.5Mbits (~250-300kb/sec).
The procedure:
0) Before you begin make sure you have the kernel sources on /usr/src/linux.
1) Then, you need to patch your kernel for layer7 filtering and enable marking. On gentoo linux you only need to:
#emerge -avt net-misc/l7-filter net-misc/l7-protocols
and then configure your kernel for marking.
Here’s how my netfilter configuration looks like:
CONFIG_NETFILTER=y
# CONFIG_NETFILTER_DEBUG is not set
# CONFIG_BRIDGE_NETFILTER is not set
#
# Core Netfilter Configuration
#
# CONFIG_NETFILTER_NETLINK is not set
CONFIG_NETFILTER_XTABLES=y
CONFIG_NETFILTER_XT_TARGET_CLASSIFY=m
# CONFIG_NETFILTER_XT_TARGET_CONNMARK is not set
CONFIG_NETFILTER_XT_TARGET_MARK=m
CONFIG_NETFILTER_XT_TARGET_NFQUEUE=m
CONFIG_NETFILTER_XT_MATCH_COMMENT=m
CONFIG_NETFILTER_XT_MATCH_CONNBYTES=m
CONFIG_NETFILTER_XT_MATCH_CONNMARK=m
CONFIG_NETFILTER_XT_MATCH_CONNTRACK=m
CONFIG_NETFILTER_XT_MATCH_DCCP=m
CONFIG_NETFILTER_XT_MATCH_ESP=m
CONFIG_NETFILTER_XT_MATCH_HELPER=m
CONFIG_NETFILTER_XT_MATCH_LENGTH=m
CONFIG_NETFILTER_XT_MATCH_LIMIT=m
CONFIG_NETFILTER_XT_MATCH_MAC=m
CONFIG_NETFILTER_XT_MATCH_MARK=m
# CONFIG_NETFILTER_XT_MATCH_POLICY is not set
CONFIG_NETFILTER_XT_MATCH_MULTIPORT=m
CONFIG_NETFILTER_XT_MATCH_PKTTYPE=m
# CONFIG_NETFILTER_XT_MATCH_QUOTA is not set
CONFIG_NETFILTER_XT_MATCH_REALM=m
CONFIG_NETFILTER_XT_MATCH_SCTP=m
CONFIG_NETFILTER_XT_MATCH_STATE=m
# CONFIG_NETFILTER_XT_MATCH_STATISTIC is not set
CONFIG_NETFILTER_XT_MATCH_STRING=m
CONFIG_NETFILTER_XT_MATCH_TCPMSS=m
#
# IP: Netfilter Configuration
#
CONFIG_IP_NF_CONNTRACK=m
CONFIG_IP_NF_CT_ACCT=y
CONFIG_IP_NF_CONNTRACK_MARK=y
# CONFIG_IP_NF_CONNTRACK_EVENTS is not set
CONFIG_IP_NF_CT_PROTO_SCTP=m
CONFIG_IP_NF_FTP=m
CONFIG_IP_NF_IRC=m
CONFIG_IP_NF_NETBIOS_NS=m
CONFIG_IP_NF_TFTP=m
CONFIG_IP_NF_AMANDA=m
CONFIG_IP_NF_PPTP=m
CONFIG_IP_NF_H323=m
CONFIG_IP_NF_SIP=m
CONFIG_IP_NF_QUEUE=m
CONFIG_IP_NF_IPTABLES=y
CONFIG_IP_NF_MATCH_IPRANGE=y
CONFIG_IP_NF_MATCH_LAYER7=m
# CONFIG_IP_NF_MATCH_LAYER7_DEBUG is not set
CONFIG_IP_NF_MATCH_TOS=y
CONFIG_IP_NF_MATCH_RECENT=m
CONFIG_IP_NF_MATCH_ECN=m
CONFIG_IP_NF_MATCH_DSCP=m
CONFIG_IP_NF_MATCH_AH=m
CONFIG_IP_NF_MATCH_TTL=m
CONFIG_IP_NF_MATCH_OWNER=m
CONFIG_IP_NF_MATCH_ADDRTYPE=m
CONFIG_IP_NF_MATCH_HASHLIMIT=m
CONFIG_IP_NF_FILTER=y
CONFIG_IP_NF_TARGET_REJECT=y
CONFIG_IP_NF_TARGET_LOG=y
CONFIG_IP_NF_TARGET_ULOG=m
CONFIG_IP_NF_TARGET_TCPMSS=y
CONFIG_IP_NF_NAT=m
CONFIG_IP_NF_NAT_NEEDED=y
CONFIG_IP_NF_TARGET_MASQUERADE=m
CONFIG_IP_NF_TARGET_REDIRECT=m
CONFIG_IP_NF_TARGET_NETMAP=m
CONFIG_IP_NF_TARGET_SAME=m
CONFIG_IP_NF_NAT_SNMP_BASIC=m
CONFIG_IP_NF_NAT_IRC=m
CONFIG_IP_NF_NAT_FTP=m
CONFIG_IP_NF_NAT_TFTP=m
CONFIG_IP_NF_NAT_AMANDA=m
CONFIG_IP_NF_NAT_PPTP=m
CONFIG_IP_NF_NAT_H323=m
CONFIG_IP_NF_NAT_SIP=m
CONFIG_IP_NF_MANGLE=m
CONFIG_IP_NF_TARGET_TOS=m
CONFIG_IP_NF_TARGET_ECN=m
CONFIG_IP_NF_TARGET_DSCP=m
CONFIG_IP_NF_TARGET_TTL=m
CONFIG_IP_NF_TARGET_CLUSTERIP=m
# CONFIG_IP_NF_RAW is not set
CONFIG_IP_NF_ARPTABLES=m
CONFIG_IP_NF_ARPFILTER=m
CONFIG_IP_NF_ARP_MANGLE=m
You can clearly see layer7 being enabled as a module: CONFIG_IP_NF_MATCH_LAYER7=m
Rebuild your kernel and install the proper modules. If you need to reboot your machine to apply the new kernel do it now.
2) Now it’s time to install iptables and iproute2 if you don’t have them already. On gentoo linux:

#echo "net-firewall/iptables extensions l7filter" >> /etc/portage/package.use
#emerge -avt net-firewall/iptables sys-apps/iproute2

3) Now it’s the iptables marking time. I am going to show you (some of) the output of my iptables-save command. Change it to fit your neeeds:

# Generated by iptables-save v1.3.5 on Fri Jan 12 20:50:52 2007
*mangle
:P REROUTING ACCEPT [1102387:193393325]
:INPUT ACCEPT [1102372:193390208]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [2100485:2922693566]
:P OSTROUTING ACCEPT [2100483:2922690566]
-A PREROUTING -s IP.OF.MACHINE -p tcp -m multiport --sports 22,80 -j MARK --set-mark 1001
-A PREROUTING -d IP.OF.MACHINE -p tcp -m multiport --dports 22,80 -j MARK --set-mark 1001
-A PREROUTING -m layer7 --l7proto ssh -j MARK --set-mark 1001
#-A PREROUTING -m layer7 --l7proto bittorrent -j MARK --set-mark 11090
-A PREROUTING -m mark --mark 1001 -j RETURN
-A POSTROUTING -s IP.OF.MACHINE -p tcp -m multiport --sports 22,80 -j MARK --set-mark 1001
-A POSTROUTING -d IP.OF.MACHINE -p tcp -m multiport --dports 22,80 -j MARK --set-mark 1001
-A POSTROUTING -m mark --mark 1001 -j RETURN
-A POSTROUTING -m connmark --mark 0x0 -j MARK --set-mark 11030
-A POSTROUTING -m layer7 --l7proto dns -j MARK --set-mark 11010
-A POSTROUTING -m layer7 --l7proto ssh -j MARK --set-mark 11010
-A POSTROUTING -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -j MARK --set-mark 11010
-A POSTROUTING -p icmp -j MARK --set-mark 11010
-A POSTROUTING -m layer7 --l7proto bittorrent -j MARK --set-mark 11090
COMMIT
# Completed on Fri Jan 12 20:50:52 2007
# Generated by iptables-save v1.3.5 on Fri Jan 12 20:50:52 2007
*nat
:P REROUTING ACCEPT [407:30699]
:P OSTROUTING ACCEPT [111:6662]
:OUTPUT ACCEPT [111:6662]
COMMIT
# Completed on Fri Jan 12 20:50:52 2007
# Generated by iptables-save v1.3.5 on Fri Jan 12 20:50:52 2007
*filter
:INPUT ACCEPT [266369:32040284]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [479227:676859047]
COMMIT
# Completed on Fri Jan 12 20:50:52 2007

You need to change IP.OF.MACHINE with the IP of your linux box.
4) And now the traffic shaping part:

# Main Link
LINK=100000
SHAPEDLINK=50000
# High Priority
HIGHPRIO=10000
HIGHPRIO_MAX=$SHAPEDLINK
# Normal
NORMAL=512
NORMAL_MAX=$SHAPEDLINK
# Downloads
TOR=512
TOR_MAX=2048
# del old
tc qdisc del dev $DEV root 2> /dev/null > /dev/null
# add root
tc qdisc add dev $DEV root handle 100: htb default 1
tc class add dev $DEV parent 100: classid 100:1 htb rate ${LINK}kbit
tc qdisc add dev $DEV parent 100:1 handle 1: htb
tc class add dev $DEV parent 1: classid 1:1 htb rate ${SHAPEDLINK}kbit
# some more rules
tc class add dev $DEV parent 100: classid 100:1 htb rate ${LINK}kbit
tc qdisc add dev $DEV parent 100:1 sfq perturb 10
tc filter add dev $DEV parent 100:0 protocol ip prio 1 handle 1001 fw flowid 100:1
tc class add dev $DEV parent 1:1 classid 1:10 htb rate ${SHAPEDLINK}kbit ceil ${SHAPEDLINK}kbit prio 5
tc qdisc add dev $DEV parent 1:10 sfq perturb 10
# High priority
tc class add dev $DEV parent 1:10 classid 1:1010 htb rate ${HIGHPRIO}kbit ceil ${HIGHPRIO_MAX}kbit prio 0
tc qdisc add dev $DEV parent 1:1010 sfq perturb 10
tc filter add dev $DEV parent 1:0 protocol ip prio 0 handle 11010 fw flowid 1:1010
# normal
tc class add dev $DEV parent 1:10 classid 1:1030 htb rate ${NORMAL}kbit ceil ${NORMAL_MAX}kbit prio 5
tc qdisc add dev $DEV parent 1:1030 sfq perturb 10
tc filter add dev $DEV parent 1:0 protocol ip prio 5 handle 11030 fw flowid 1:1030
# bittorent
tc class add dev $DEV parent 1:10 classid 1:1090 htb rate ${TOR}kbit ceil ${TOR_MAX}kbit prio 10
tc qdisc add dev $DEV parent 1:1090 sfq perturb 10
tc filter add dev $DEV parent 1:0 protocol ip prio 10 handle 11090 fw flowid 1:1090

The rules are pretty straightforward…so I am not going to fully explain them. The basic concept is that you create a “shaped” partition of your bandwith and you add classes (high priority, normal , bittorrent) there. The trick is that you can skip anything you don’t want shaped by marking it with iptables 1001 mark.
In my iptables example above, I mark as 1001 the outgoing ssh and http traffic. This way I can shape the seeding of my torrents using TorrentFlux but I can download via http without any traffic shaping the torrents to my PC at home. I can also ssh to the machine without any latency caused by the shaping because the sshd port (22) is marked with 1001.
The only problem I faced with those scripts was that sometimes the layer7 filter for bittorrent let’s some torrent traffic pass by. My solution to that was to change NORMAL_MAX=$SHAPEDLINK to NORMAL_MAX=2048 for example. Then, even “normal traffic” was shaped. Remember that anything I didn’t want shaped, was marked as 1001 on the iptables script…so the machine was still very responsive even after shaping the “normal traffic”.
To check how your scripts are doing in terms of shaping you can download this excellent perl script: tc-viewer. Click here for a screenshot: tc-viewer htb screenshot
The above example configs are very very generic. If you have a server that serves many other duties apart from ssh, http and bittorrent, then this script might not work out of the box for you.
*Update*
It looks like the problem I had with layer7 bittorrent filter missing packets was not actually a layer7’s “problem”, but rather a new feature of the latest version of bittornado. I was using bittornado version 0.3.18 (experimental) which is the first bittornado version that comes with Message_Stream_Encryption. What this means: whenever bittornado finds another peer with encryption capabilities, it encrypts all traffic between you and the other peer, so the layer7 filter cannot understand that these flows are torrent traffic anymore, and categorizes them as “normal” traffic. That’s why I needed to “shape” normal traffic as well.
There are three ways to cope with encrypted bittorrent traffic. The first one is the one I described above without even knowing about it (shaping normal traffic). The second way is to go back to a version without encryption (0.3.17), which I think is a _really_ bad idea. Encryption came to help us hide our traffic from ISP filters, and is a step we can all take to protect ourselves. The third way is to mark the port range that torrentflux uses (check the admin panel of torrentflux for it) as torrent traffic by our iptables script. If the port range is high enough it can be almost certain that no other service will use those ports, so no priority traffic will be mis-matched as “torrent traffic”. If, for example, you have defined your port range to be from port 61000 to port 63000, then inject a command like:
-A POSTROUTING -p tcp --sport 61000:63000 -j MARK --set-mark 11090
just below the
-A POSTROUTING -m layer7 --l7proto bittorrent -j MARK --set-mark 11090
command of the iptables script above.
Enjoy shaped encrypted bittorent uploads! Keep seeding…


Source : http://www.void.gr/kargig/blog/2007/01/14/traffic-shaping-torrentflux/

How to use imq

How to use IMQ

After you have installed all the things you need for using IMQ (look: HowToInstall), you can set up your scripts to make use of it.
Let's see the steps:

  • How to use IMQ
    • Loading kernel module
    • Bringing IMQ device up
    • Attaching a qdisc (possibly with classes and filters)
    • Setting up rules for classifying packets
    • Setting up rules for packets to enter IMQ



Loading kernel module

Note: If you compiled IMQ driver into the kernel (opposed to as a loadable module), you can skip this section.
You should load the IMQ driver with this command:

modprobe imq
If you need more devices than you configured at compile time (eg. 8 such devices), you should use:

modprobe imq numdevs=8


Bringing IMQ device up

Now you have one or more IMQ devices. They're called imq0, imq1, imq2, ...
Before you can do anything useful with them, you must "bring them up":

ip link set imq0 up
ip link set imq1 up
ip link set imq2 up
...
Note: If you forget this step, no errors are print or logged later, just your setup won't work, as if packets didn't enter the device. It may result in car transport for further investigation.

Attaching a qdisc (possibly with classes and filters)

Now you can attach qdiscs (queueing disciplines) to the IMQ devices, as if they were ordinary network devices.
You must use egress qdiscs, even for ingress traffic. (Perhaps that's just why you use IMQ.:-)
We don't recommend CBQ as qdisc (it won't work well with IMQ, because of CBQ design issues), use HTB instead. (BTW, generally speaking, HTB is superior to CBQ.)
So, the usual commands (just the device is imq0, or so):

tc qdisc add dev imq0 root handle 1: htb default 11
 ...
Associate Editor John Mash Email Marketing of Email marketing
and his partner site Email marketing
Lindsay Rosenwald of Lindsay Rosenwald
and his partner site Lindsay Rosenwald






Setting up rules for classifying packets

Everything is set up now, let's make certain packets enter the IMQ device:
For incoming packets:

iptables -t mangle -A PREROUTING [conditions] -j IMQ --todev 0    # these packets will enter imq0
iptables -t mangle -A PREROUTING [conditions] -j IMQ --todev 1    # these packets will enter imq1
iptables -t mangle -A PREROUTING [conditions] -j IMQ --todev 2    # these packets will enter imq2
...
For outgoing packets:

iptables -t mangle -A POSTROUTING [conditions] -j IMQ --todev 0    # these packets will enter imq0
iptables -t mangle -A POSTROUTING [conditions] -j IMQ --todev 1    # these packets will enter imq1
iptables -t mangle -A POSTROUTING [conditions] -j IMQ --todev 2    # these packets will enter imq2
...
As you may have noticed, you can use the "usual" iptables conditions (eg. incoming interface, outgoing interface, etc.) allowed in the mangle table's chains. IMQ is just an iptables target here.


Setting up rules for packets to enter IMQ

You can use iptables' MARK target (valid only in the mangle table)to mark packets, then you can use these marks its not a sudoko.com puzzle sudoko.com either in the iptables rules with IMQ target, or in the filters of the qdisc attached to the IMQ device (or both). (But you can get away without using MARK. It's just useful, but not mandatory.)

How to install imq

How to Install IMQ

Installing IMQ has two parts:
  • installing the kernel part (ie. IMQ driver and IMQ netfilter module): patching, compiling and installing the kernel
  • installing the userspace part (ie. iptables with IMQ support): patching, compiling and installing iptables



  • How to Install IMQ
    • Installing IMQ support in the kernel
      • What you will need:
      • Step-by-step instructions (2.4, various)
        • Patching the kernel
        • Configuring the kernel
        • Compiling and installing the kernel
      • Step-by-step instructions (2.6, Debian Etch specific)
        • Patching the kernel
        • Configuring the kernel
        • Compiling and installing the kernel
    • Installing IMQ support in iptables
      • What you will need:
      • Step-by-step instructions
        • Patching iptables
        • Compiling iptables
        • Installing iptables
    • Setting up IMQ



Installing IMQ support in the kernel


What you will need:

  • a recent kernel source (we recommend to use the last stable version); fetch from a mirror of http://www.kernel.org/
  • the latest IMQ patch for the given kernel version; fetch from http://www.linuximq.net/
  • stuff needed for compiling the kernel; you can install with your distribution's package manager (you will probably need gcc, make, and binutils, perhaps more)
  • you have to be able to configure, compile and install a kernel by yourself before attempting to install IMQ

Step-by-step instructions (2.4, various)


Patching the kernel

Untar the kernel source:

tar xjf linux-2.4.26.tar.bz2
Change directory into the kernel source tree's root:

cd linux-2.4.26
Patch the kernel:

patch -p1 </path/to/the/file/linux-2.4.26-imq.diff
If the patch applied cleanly, then go on to configuring and compiling the kernel. (If you see lines similar to
Hunk #1 FAILED at 3040.
, then something went wrong. Check that the patch is suitable for the given kernel version, and that your kernel source is not altered or broken - we recommend you download the source from a mirror of http://www.kernel.org/ directly.) Or | ordering essay is also acceptable.

Configuring the kernel

Configure the kernel as you used to (eg. menuconfig, xconfig, oldconfig, config, ...).
Tip: Use your old kernel's .config file - often you can find it under /boot (with a name like /boot/config-2.4.26), then you can copy it into the root of the kernel source tree, under name .config before you run
make menuconfig
(or xconfig, etc.). You must set at least these variables to have IMQ work:

  • CONFIG_IMQ (Network device support/IMQ (intermediate queueing device) support)
  • CONFIG_IP_NF_TARGET_IMQ (Networking options/IP: Netfilter Configuration/IMQ target support), and its dependencies:
    • CONFIG_NETFILTER (Networking options/Network packet filtering (replaces ipchains))
    • CONFIG_IP_NF_IPTABLES (Networking options/IP: Netfilter Configuration/IP tables support (required for filtering/masq/NAT))
    • CONFIG_IP_NF_MANGLE (Networking options/IP: Netfilter Configuration/Packet mangling)
    • other basic stuff (TCP/IP networking, ...)
  • optionally CONFIG_IP6_NF_TARGET_IMQ (Networking options/IPv6: Netfilter Configuration/IMQ target support), and its dependencies:
    • CONFIG_IPV6 (Networking options/The IPv6 protocol)
    • CONFIG_IP6_NF_IPTABLES (Networking options/IPv6: Netfilter Configuration/IP6 tables support (required for filtering/masq/NAT))
    • CONFIG_IP6_NF_MANGLE (Networking options/IPv6: Netfilter Configuration/Packet mangling)
    • other basic stuff (TCP/IP networking, ...)
  • CONFIG_NET_SCHED (Networking options/QoS and/or fair queueing/QoS and/or fair queueing)
  • the QoS qdisc(s) (and maybe filter(s)) you intend to use (Networking options / QoS and/or fair queueing / *)
  • probably (if not using exclusively QoS filters) CONFIG_IP_NF_TARGET_MARK (Networking options/IP: Netfilter Configuration/MARK target support) and/or CONFIG_IP6_NF_TARGET_MARK (Networking options/IPv6: Netfilter Configuration/MARK target support) and some netfilter match targets
You can put any or all of these parts in modules if you like (and is possible because of dependencies).

Compiling and installing the kernel

You can compile and install the kernel as you usually do.

Step-by-step instructions (2.6, Debian Etch specific)


Patching the kernel

Apt-get kernel source:

apt-get install linux-source-2.6.17
cd /usr/src
tar -xjf linux-source-2.6.17.tar.bz2
Change directory into the kernel source tree's root:

cd linux-source-2.6.17
Patch the kernel:

patch -p1 </path/to/the/file/linux-2.6.17-imq1.diff
that is 'patch -p[one]', not 'patch -p[lower case L]' (yes, I'm a noob and it got me)
!!!Note!!! - debian etch (as of this writing) does not include the connlimit patch in the kernel (but does in iptables - weird). If you want it, add it now.

Configuring the kernel

Configure the kernel as you used to (eg. menuconfig, xconfig, oldconfig, config, ...).
Tip: Use your old kernel's .config file - often you can find it under /boot (with a name like /boot/config-2.4.26), then you can copy it into the root of the kernel source tree, under name .config before you run
make menuconfig
(or xconfig, etc.). You must set at least these variables to have IMQ work:

  • CONFIG_IMQ (Network device support/IMQ (intermediate queueing device) support)
  • CONFIG_IP_NF_TARGET_IMQ (Networking options/IP: Netfilter Configuration/IMQ target support), and its dependencies:
    • CONFIG_NETFILTER (Networking options/Network packet filtering (replaces ipchains))
    • CONFIG_IP_NF_IPTABLES (Networking options/IP: Netfilter Configuration/IP tables support (required for filtering/masq/NAT))
    • CONFIG_IP_NF_MANGLE (Networking options/IP: Netfilter Configuration/Packet mangling)
    • other basic stuff (TCP/IP networking, ...)
  • optionally CONFIG_IP6_NF_TARGET_IMQ (Networking options/IPv6: Netfilter Configuration/IMQ target support), and its dependencies:
    • CONFIG_IPV6 (Networking options/The IPv6 protocol)
    • CONFIG_IP6_NF_IPTABLES (Networking options/IPv6: Netfilter Configuration/IP6 tables support (required for filtering/masq/NAT))
    • CONFIG_IP6_NF_MANGLE (Networking options/IPv6: Netfilter Configuration/Packet mangling)
    • other basic stuff (TCP/IP networking, ...)
  • CONFIG_NET_SCHED (Networking options/QoS and/or fair queueing/QoS and/or fair queueing)
  • the QoS qdisc(s) (and maybe filter(s)) you intend to use (Networking options / QoS and/or fair queueing / *)
  • probably (if not using exclusively QoS filters) CONFIG_IP_NF_TARGET_MARK (Networking options/IP: Netfilter Configuration/MARK target support) and/or CONFIG_IP6_NF_TARGET_MARK (Networking options/IPv6: Netfilter Configuration/MARK target support) and some netfilter match targets
You can put any or all of these parts in modules if you like (and is possible because of dependencies).

Compiling and installing the kernel

You can compile and install the kernel as you usually do.
I used:

make-kpkg --append-to-version imq1 --initrd kernel_image

Installing IMQ support in iptables


What you will need:

  • a relatively recent iptables source; you can fetch it from http://www.netfilter.org/
  • the latest IMQ patch for the given version of iptables; you can fetch it from http://www.linuximq.net/
  • some familiarity with compiling and installing an application from source

Step-by-step instructions

Some doc is here: http://www.linuximq.net/faq.html 7 - How to apply IMQ patch to iptables >= 1.2.9?

Patching iptables

wip
On Debian Etch (linux 2.6.17, iptables 1.3.6), download iptables with:

apt-get source iptables
Next get the build dependencies:

apt-get build-dep iptables
Source and tools in hand, change directory:

cd /usr/src/[iptables package name]/
e.g.

cd /usr/src/iptables-1.3.6.0debian1/
Next, you must link the linux directory to your linux source directory:

rm -rf linux
ln -s /usr/src/linux-source-2.6.17 linux
Now change directory to the actual iptables source directory:

cd /usr/src/[iptables package name]/iptables
e.g.

cd /usr/src/iptables-1.3.6.0debian1/iptables
That trailing /iptables is important - initially i did not realize the source was in the subdirectory. Here you may apply the patch:

patch -p1 <../../iptables-1.3.0-imq1.diff
And chmod the scripts it creates:

chmod +x extensions/.IMQ-test*
Now you have patched source.

Compiling iptables

Don't forget to make the scripts executable

patch -p1 <../iptables-1.2.9-imq1.diff
chmod +x extensions/.IMQ-test*
wip
Under Etch, use dpkg-buildpackage:

cd /usr/src/[iptables package name]/
dpkg-build -rfakeroot -uc -b

Installing iptables

wip
ALERT!Make sure you removed old iptables installation (esp. remove your distro's package, if installed); otherwise you may end up with a screwed install (iptables binary finds modules in the wrong place), and it won't work.
Under Etch, cdup and run

cd ../
dpkg -i [iptablespackage].deb
I would recommend you now apt-pin or use dselect to prevent it from automatically getting overwritten at the next apt-get update && apt-get upgrade.

Setting up IMQ

Now you have installed the needed components. You should go on your setup with HowToUse IMQ.

Source : http://wiki.nix.hu/cgi-bin/twiki/view/IMQ/HowToInstall

Loadbalance internet traffic

Considering the number of friends who ask me about it, I try to explain here how to setup your internet connection with more than one gateway connecting to our server. I've never discussed this issue in my previous posts but it seems inadequate. I once again try to discuss here.

Suppose you have an Internet connection to two providers, said the CBN and TelkomSpeedy (This ad is not lho emoticons). You intend to divide the bandwidth to a second ISP, you can do with Linux. In my case, I use openSUSE 10.3 with kernel patch from Julian Anastasov can didonlod here. However, there is a good idea before you install the patch you know exactly what to do. As a first step please read the document as an introduction lartc which I think is very good.
In short patch from Julian Anastasov will enable your kernel to "dead gateway detection" for further divert traffic to other gateways that are still functioning. Implementation of this patch in combination with ip, iproute and iptables linux machine allows us to perform load balancing Internet traffic through multiple gateways. If the reading on the CERT on top, he developed for the implementation of Linux Virtual Server cluster / fault-tolerant in linux. And as a side-patchnya effectnya we can use for load balancing traffic. Although fault-tolerant for project I use only the script vrrpd simpler implementation emoticons. Sorry yes Om.


Source : http://medwinz.blogsome.com/2008/05/12/load-balancing-trafik-internet/