Optimizing shaper-hashing filters (htb)

Source: Internet
Author: User

I have a very nice Shaper in my Linux box

How the configurator works-it's another question, here I will try to describe how one cocould configure her shaper with hash-based filtering.

So, this page about processing ing Linux kernel traffic shaper, which uses filter Hashing for massive IP processing.

  1. Introduction
  2. Idea
  3. Task
  4. Processing ing
  5. Real case
  6. Notes
Introduction

In a couple of words::

Shaper, in some sense, is a queueing discipline, which processes Queue (s) of packets. discipline may beClasslessOrClassful.ClassfulMeans that certain traffic may be shaped by certain class.

For certain traffic to be shaped by certain class, there is certainFilterShocould be configured.

So, in some sense, classful discipline consists of two trees-tree of classes and tree of filters. Filters filter packets to classes, classes shape traffic.

Anyway, these topics discovered in a lot of manuals

Why hashes.

«Filters tree» is a defined sequence of filters, with possible jumps between branches. But if tree has a lot of filters, it takes a lot of time to reach mathing one.

If while filters tree is a sequence of check an IP address for a match, it is far more than tive to useHashes. Hash is a table of keys «matching» their «values», so for every key there may be only one value, and hash searching (» which value corresponds
To this key ?») Is very fast.

In our case key is an IP address and value for a key is a filter, which filters a packet to a certain shaping class.

Surely, see also A lartc howto chapter for this.

Idea

So, if we create a table with 256 cells for/24 network and look for neede cell using IP address as a key, every needed filter we will find by one step. it wocould be 128 steps (in average) with no hash table.

If we need to manage two/24 networks, we can create three tables, two of them for 256 cells (for/24's) and one with two cells. first select «proper» large table by «network» key and then find proper filter by IP (host part participant ly ).

Like this:

Lets take two/24,192.168 .1.0/24 and 192.168.2.0/24.

Third bytes are 00000001 and 00000010 (in binary format) respectively.

So, kernel with the very first filter chooses a cell in Two-cells table-by third byte in IP address. in that cell there is a filter, which points to one of two 256-cells table, and uses fourth byte as a key. with this filter kernel can find the last (possibly)
Filter in Particle Cell of particle table. (» posibly»-because that filter can point to elsewhere, be chained to other filter (s )). the last filter filters a packet to a class, which shapes a packet.

The task

We have:

  • Five networks/24: 192.168.1.0/24,192.168 .2.0/24 .. 192.168.5.0/24
  • Interface $ Dev, directed to these networks

We need:

  • To shape packets depending on destination IP address
  • Unclassified traffic shoshould be shaped on a special class
Processing ing

We will want to have a script, which will configure our kernel... Or to configure kernel directly:

# it may be useful to distinct between# configuring and creating script:#tc="/sbin/tc"tc="/bin/echo /sbin/tc"

Create root qdisc (class 90-For unclassified traffic ):

$tc qdisc add dev $DEV root handle 1: htb default 90

Root class:

$tc class add dev $DEV parent 1:0 classid 1:1 htb rate 100Mbit

Now we need to create «root filter»-Other filters need this:

$tc filter add dev $DEV parent 1:1 prio 10 protocol ip u32

Create a table with five cells, one cell for every/24:

# divisor 5 --- table for 5 cells:$tc filter add dev $DEV parent 1:1 protocol ip prio 10 handle 8: u32 divisor 5

Now let's create five tables with 256 cells each:

for i in 1 2 3 4 5; do    $tc filter add dev $DEV parent 1:1 prio 10 handle ${i}: protocol ip u32 divisor 256done

Now let's fillUpperTable (with 5 cells), it must contain jumps to particle 256-cells table:

for i in 1 2 3 4 5; do    $tc filter add dev $DEV parent 1:1 protocol ip prio 10 \        u32 ht 8:$[i-1]: \        match ip dst 192.168.${i}.0/24 \        hashkey mask 0x000000ff at 16 \        link $i:done

This means: Put in cell $ [I-1] of table 8 (ht 8:$[i-1]:) A filter, which takes a fourth byte
(hashkey mask 0x000000ff) Of destination IP address (match ip dst 192.168.${i}.0/24), And uses it as a key for searching in table
$ I (link $i:).

Now-a «master filter», which uses tree bits of third byte as a key for searching in a main table:

$tc filter add dev $DEV parent 1:0 protocol ip prio 100 u32 ht 800:: \    match ip dst 192.168.0.0/21 \    hashkey mask 0x00000700 at 16 link 8:

It means-root filter (ht 800::) Must check destination IP address and, if it matches one our networks ,(match ip dst 192.168.0.0/21)
Use last three bits of third byte of address (hashkey mask 0x00000700 at 16) As a key for searching in table 8 (link 8:).

Now we may create classes/filters for our clients.

This is a very individual process (every administrator decides in which format she will store configuration data-classids, rates etc .), so for the sake of demonstration we will create one clients class and a filter for it:

# class 1: 320 --- parent for clients' classes:$tc class add dev $DEV parent 1:1 classid 1:230 htb rate 30Mbit ceil 50Mbit quantum 1500 . . .## particular client:$tc class add dev $DEV parent 1:230 classid 1:431 htb rate 2Mbit ceil 10Mbit quantum 1500 burst . . .$tc filter add dev $DEV protocol ip parent 1:0 prio 100 u32 ht 3:4: match ip dst 192.168.3.4 flowid 1:431

Last Command means-in a wrong th cell (numbered from zero !) Of third table (for 192.168.3.0/24) out a filter, which will filter packets in class :431.

So in a loop we can create classes and filters for every client-and we actually fill our 256-cells tables with these filters.

Real case

In a real case the Administrator may want to keep configuration data in a SQL database, real Scripts may differ a lotPerform the same job. Such shapers scale quite well, we may double (tripe, quadruple etc ....) Number of networks, but Filters
Will work very fast.

Probably you will configure your shaper at a bridge-HtbWorks quite nice at bridged interfaces, with no
IP address assigned.

Notes
  • When I worked on this configuration, I noticed that it is necessary to create a «whole» Number of 256-cells tables: It shocould be equal to (2 ^ n-1 ), where N is a number of/24 networks. so, I have to create more
    Tables than I actually need, but otherwise it doesn' t work
  • Cells are numbered starting with zero; cells numbers must be in Hex. So, in the example above-u32 HT3:4: match-Four (4) is in Hex, too
  • This is the first beta draft
Author: feiskyer published on 21:25:51 Original article link reading: 16 comment: 0 view comment

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.