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, all with different QoS specifications, you may find that the kernel spends a lot of time matching all those rules.

By default, all filters reside in one big chain which is matched in descending order of priority. If you have 1000 rules, 1000 checks may be needed to determine what to do with a packet.

Matching would go much quicker if you would have 256 chains with each four rules - if you could divide packets over those 256 chains, so that the right rule will be there.

Hashing makes this possible. Let‘s say you have 1024 cable modem customers in your network, with IP addresses ranging from 1.2.0.0 to 1.2.3.255, and each has to go in another bin, for example ‘lite‘, ‘regular‘ and ‘premium‘. You would then have 1024 rules like this:

# tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src   1.2.0.0 classid 1:1
# tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src   1.2.0.1 classid 1:1
...
# tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src   1.2.3.254 classid 1:3
# tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src   1.2.3.255 classid 1:2

To speed this up, we can use the last part of the IP address as a ‘hash key‘. We then get 256 tables, the first of which looks like this:

# tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src   1.2.0.0 classid 1:1
# tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src   1.2.1.0 classid 1:1
# tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src   1.2.2.0 classid 1:3
# tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src   1.2.3.0 classid 1:2

The next one starts like this:

# tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src   1.2.0.1 classid 1:1
...

This way, only four checks are needed at most, two on average.

Configuration is pretty complicated, but very worth it by the time you have this many rules. First we make a filter root, then we create a table with 256 entries:

# tc filter add dev eth1 parent 1:0 prio 5 protocol ip u32
# tc filter add dev eth1 parent 1:0 prio 5 handle 2: protocol ip u32 divisor 256

Now we add some rules to entries in the created table:

# tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 ht 2:7b:         match ip src 1.2.0.123 flowid 1:1
# tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 ht 2:7b:         match ip src 1.2.1.123 flowid 1:2
# tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 ht 2:7b:         match ip src 1.2.3.123 flowid 1:3
# tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 ht 2:7b:         match ip src 1.2.4.123 flowid 1:2

This is entry 123, which contains matches for 1.2.0.123, 1.2.1.123, 1.2.2.123, 1.2.3.123, and sends them to 1:1, 1:2, 1:3 and 1:2 respectively. Note that we need to specify our hash bucket in hex, 0x7b is 123.

Next create a ‘hashing filter‘ that directs traffic to the right entry in the hashing table:

# tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 ht 800::         match ip src 1.2.0.0/16         hashkey mask 0x000000ff at 12         link 2:

Ok, some numbers need explaining. The default hash table is called 800:: and all filtering starts there. Then we select the source address, which lives as position 12, 13, 14 and 15 in the IP header, and indicate that we are only interested in the last part. This will be sent to hash table 2:, which we created earlier.

It is quite complicated, but it does work in practice and performance will be staggering. Note that this example could be improved to the ideal case where each chain contains 1 filter!

时间: 2024-08-24 17:27:45

Hashing filters for very fast massive filtering的相关文章

Gradle Goodness: Copy Files with Filtering

Gradle Goodness: Copy Files with Filtering Gradle's copy task is very powerful and includes filtering capabilities. This means we can change the contents of the files that are copied before they reach their new destination. We use the filter() method

编写 capture filters

编写 capture filters 如有转载,请在转载前给我提一些建议.谢谢. 百度查不到资料,为无能的百度搜索增加点营养的料. 读 http://www.n-cg.net/CaptureFilters.htm 使用TCPdump语法编写 Syntax Description host host host is either the ip address or host name src host host Capture all packets where host is the sourc

Computer Graphics Research Software

Helping you avoid re-inventing the wheel since 2009! Last updated December 5, 2012.Try searching this page for keywords like 'segmentation' or 'PLY'.If you would like to contribute links, please e-mail them to [email protected]. Papers & Archives Gra

计算机图形学研究常用工具软件和代码

Computer Graphics Research Software Helping you avoid re-inventing the wheel since 2009! Last updated December 5, 2012.Try searching this page for keywords like 'segmentation' or 'PLY'.If you would like to contribute links, please e-mail them to [ema

数字图像处理经典论文汇总

Colorization and Color Transfer(图像上色和颜色迁移) Semantic Colorization with Internet Images, Chia et al. SIGGRAPH ASIA 2011 Color Harmonization, Cohen-Or, Sorkine, Gal, Leyvand, and Xu. Web Page Computing the alpha-Channel with Probabilistic Segmentation f

如何科研

原文链接 https://mmcheng.net/paperreading/ 关于文献阅读和科研选题 2014年04月01日 星期二2018年03月30日 星期五 MM Cheng 26 Comments ? 自从2007年一月去我即将读研的清华大学计算机图形学组做本科毕业设计开始,我就陷入了一个困扰我许久的问题之中:如何阅读文献,如何寻找科研题目?之后长达三年的时间,我一直被这个问题深深困扰,直至2009年底首次以第二作者发表论文(Sketch2Photo和Resizing)才稍微有缓和.在之

memcached全面剖析--4

memcached的分布式算法   memcached的分布式 正如第1次中介绍的那样, memcached虽然称为“分布式”缓存服务器,但服务器端并没有“分布式”功能. 服务器端仅包括 第2次. 第3次 前坂介绍的内存存储功能,其实现非常简单. 至于memcached的分布式,则是完全由客户端程序库实现的. 这种分布式是memcached的最大特点. memcached的分布式是什么意思? 这里多次使用了“分布式”这个词,但并未做详细解释. 现在开始简单地介绍一下其原理,各个客户端的实现基本相

史上最全的maven pom.xml文件教程详解

<project xmlns=http://maven.apache.org/POM/4.0.0 xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:schemaLocation=http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd >; <!-- 父项目的坐标.如果项目中没有规定某个元素的值,那么父项目中的对应值即为项目的默认值.

maven的pom.xml文件标签含义

pom作为项目对象模型.通过xml表示maven项目,使用pom.xml来实现.主要描述了项目:包括配置文件:开发者需要遵循的规则,缺陷管理系统,组织和licenses,项目的url,项目的依赖性,以及其他所有的项目相关因素. [xml] view plain copy print? <span style="padding:0px; margin:0px"><project xmlns="http://maven.apache.org/POM/4.0.0&