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 could configure her shaper with hash-based filtering.

So, this page about configuring linux kernel traffic shaper, which uses filter hashing for massive IP processing.

  1. Introduction
  2. Idea
  3. Task
  4. Configuring
  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 be classless or classfulClassful means that certain traffic may be shaped by certain class.

For certain traffic to be shaped by certain class, there is certain filter should 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 effective to use hashes. 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 would 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 particularly).

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 particular cell of particular 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 should be shaped on a special class

Configuring

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 256
done

Now let’s fill upper table (with 5 cells), it must contain jumps to particular 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 fifth cell (numbered from zero!) of third table (for 192.168.3.0/24) out a filter, which will filter packets in class 1: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 lot but perform 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 — HTB works 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 should 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 ht 3:4: match — four (4) is in hex, too :-)
  • This is the first beta draft :-)

Optimizing shaper — hashing filters (HTB)的更多相关文章

  1. Hashing filters for very fast massive filtering

    If you have a need for thousands of rules, for example if you have a lot of clients or computers, al ...

  2. 【Ansible 文档】【译文】模版(Jinja2)

    Templating (Jinja2) 正如在 variables 部分描述的那样, Ansible 使用Jinja2模版来启用动态表达式和访问变量. Ansible 扩展了许多 filtes 和 t ...

  3. 【Ansible 文档】【译文】常见问题

    http://docs.ansible.com/ansible/latest/faq.html 如何为一个task或者整个Playbook设置PATH或者任意其他环境变量? 通过environment ...

  4. 布隆过滤器(Bloom Filters)的原理及代码实现(Python + Java)

    本文介绍了布隆过滤器的概念及变体,这种描述非常适合代码模拟实现.重点在于标准布隆过滤器和计算布隆过滤器,其他的大都在此基础上优化.文末附上了标准布隆过滤器和计算布隆过滤器的代码实现(Java版和Pyt ...

  5. ABP(现代ASP.NET样板开发框架)系列之13、ABP领域层——数据过滤器(Data filters)

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之13.ABP领域层——数据过滤器(Data filters) ABP是“ASP.NET Boilerplate P ...

  6. ASP.NET MVC Filters 4种默认过滤器的使用【附示例】

    过滤器(Filters)的出现使得我们可以在ASP.NET MVC程序里更好的控制浏览器请求过来的URL,不是每个请求都会响应内容,只响应特定内容给那些有特定权限的用户,过滤器理论上有以下功能: 判断 ...

  7. Unity性能优化(3)-官方教程Optimizing garbage collection in Unity games翻译

    本文是Unity官方教程,性能优化系列的第三篇<Optimizing garbage collection in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...

  8. Unity性能优化(4)-官方教程Optimizing graphics rendering in Unity games翻译

    本文是Unity官方教程,性能优化系列的第四篇<Optimizing graphics rendering in Unity games>的翻译. 相关文章: Unity性能优化(1)-官 ...

  9. [Algorithm] 局部敏感哈希算法(Locality Sensitive Hashing)

    局部敏感哈希(Locality Sensitive Hashing,LSH)算法是我在前一段时间找工作时接触到的一种衡量文本相似度的算法.局部敏感哈希是近似最近邻搜索算法中最流行的一种,它有坚实的理论 ...

随机推荐

  1. 二分图 最大权匹配 km算法

    这个算法的本质还是不断的找增广路: KM算法的正确性基于以下定理:若由二分图中所有满足A[i]+B[j]=w[i,j]的边(i,j)构成的子图(称做相等子图)有完备匹配,那么这个完备匹配就是二分图的最 ...

  2. Web API系列

    ASP.NET Web API 是一种框架,用于轻松构建可以访问多种客户端(包括浏览器和移动设备)的 HTTP 服务. ASP.NET Web API 是一种用于在 .NET Framework 上构 ...

  3. C#.NET SQL数据库备份与还原解决方案

    C#.NET SQL数据库备份与还原解决方案http://www.csframework.com/archive/1/arc-1-20110924-1841.htm 开发框架V2.2(快速开发版)系统 ...

  4. 求x^0+x^1+x^2+.......x^n mod p; x,n,p<=10^9

    方法一:快速幂.但是肯定还是超时. 方法二:利用等比数列公式,但是有除法,做不下去了. 方法三:有点分治的味道.. n为偶数时,x^0+x^1+x^2+.......x^n=(x^0+x^1+x^2+ ...

  5. 谷歌 不支持 activeX插件

    因为Chrome浏览器42以上版本已经陆续不再支持NPAPI插件,也就是说,目前的迅雷插件.FLASH插件.支付宝插件.阿里旺旺插件.百度贴吧.网银等网站都受到一定程度的影响,本文分享给大家如何让谷歌 ...

  6. C++-sizeof和strlen的区别

    一.sizeof    sizeof(...)是运算符,在头文件中typedef为unsigned int,其值在编译时即计算好了,参数可以是数组.指针.类型.对象.函数等.    它的功能是:获得保 ...

  7. bzoj 1036 Tree Count

    题目大意:给出一棵树,每个点有一个权值,要求三种操作:1.修改某个点的权值,2.询问x到y路径上各点的权值最大值,3.询问x到y路径上各点的权值之和. #include <cstdio> ...

  8. java接收键盘输入

    System.out.print("Please input String to check:");//提示输入 Scanner sc=new Scanner(System.in) ...

  9. [Swift2.0系列]Defer/Guard 基础语法

    1.Defer Swift2.0中加入了defer新语法声明.defer译为延缓.推迟之意.那么在Swift2.0中它将被应用于什么位置呢?比如,读取某目录下的文件内容并处理数据,你需要首先定位到文件 ...

  10. jQuery 通用表单方法

    表单验证一直是一个麻烦的事情,让很多人望而退步,之前想过一个验证的好方法,但是有bug,昨晚请教了juyling.com的王员外,顺利解决. 以下是js代码     function mySubmit ...