免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 34517 | 回复: 80
打印 上一主题 下一主题

[网络管理] 发布一个速度匹配的内核模块(hashspeed) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-02 12:04 |只看该作者 |倒序浏览
本模块是在hashlimit基础上修改的,除了可以进行包的匹配以外,还可以进行字节的匹配,可以达到针对IP来进行限速和限制包的发送速度。

hashspeed-0.1.tar.gz (11.47 KB, 下载次数: 640)

2008.10.12 更新源代码,添加了对高版本kernel和iptables的支持,目前能够在linux-2.6.25 linux-2.6.26和iptables-1.4.1正常编译(gcc-4.1和gcc-4.3)

更多信息可以查看压缩包中的README.zh_CN-utf8(中文UTF-8编码)

我是在2.6.22 和 2.6.23内核上编译通过的,iptables-1.3.8, gcc-4,1

如果是更旧的内核,可能会没有linux/mutex.h

如果没有x_table.h,那么可以把ipt_hashspeed.c中的
#include <linux/netfilter/x_tables.h>
去掉,不会有任何影响

如果没有mutex.h,就得自己想办法了。(主要是用到mutex_lock这个函数)
在一些老的内核版本中,有 fs/xfs/linux-2.6/mutex.h这个头文件,有能力的兄弟可以自己参考参考,做一些修改


因为对linux编程不是很熟悉,这个只是在hashlimit模块的基础上,按照需要进行了适当的修改(而hashlimit模块是在比较新的内核中才出现的),因此,对于那些遇到编译问题的,本人无法做出一一解答。

不过,我会针对大家提供的一些编译错误,在源代码上做一些修改,并投放在我的网站上,因此,直接从我的网站上下载的源码包可能与这儿的附件有些微差别。

如果哪位兄弟在自己的实际环境中,成功编译并运行(通过修改源代码),请张贴您的修改和编译环境。


linux不同版本的内核,结构定义不一样,这个真是烦人,要想让它能够在不同版本的内核下都能够编译成功,真不是一件容易的事,只能一点点完善了。

我会不断地更新我的网站上的文件,也许,你下载到最新的源代码,就有可能解决以前不能成功编译的问题了。

[ 本帖最后由 springwind426 于 2008-10-12 10:14 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2008-01-02 14:18 |只看该作者
wow~~~~~~~~~~测试下,精确吗?同时支持 -m mac --mac-source吗?

论坛徽章:
0
3 [报告]
发表于 2008-01-02 14:34 |只看该作者
这只是一个匹配模块,因此,是可以与其他模块一起使用的

我自己测试的精确度还行

比如:
iptables -A FORWARD -i eth1 -p tcp -m hashspeed \! --hashspeed-bytes 256K --hashspeed-mode srcip --hashspeed-name slimit -j DROP

可以将eth1连接的内网的所有主机的TCP上传速度限制在256K/S


iptables -A FORWARD -o eth1 -p tcp -m hashspeed \! --hashspeed-bytes 256K --hashspeed-mode dstip --hashspeed-name dlimit -j DROP
可以将eth1连接的内网的所有主机的TCP下载速度限制在256K/S

论坛徽章:
0
4 [报告]
发表于 2008-01-02 17:20 |只看该作者
iptables 1.3.5 内核2.6.19.7 编译不过去

论坛徽章:
0
5 [报告]
发表于 2008-01-02 17:21 |只看该作者
[root@proxy hashspeed-0.1]# make
make -C /lib/modules/2.6.9-Custom/build M=/root/hashspeed-0.1 modules
make[1]: Entering directory `/usr/src/linux-2.6.9'
  CC [M]  /root/hashspeed-0.1/ipt_hashspeed.o
/root/hashspeed-0.1/ipt_hashspeed.c:27:38: linux/netfilter/x_tables.h: No such file or directory
/root/hashspeed-0.1/ipt_hashspeed.c:107: error: syntax error before "__read_mostly"
/root/hashspeed-0.1/ipt_hashspeed.c:107: warning: type defaults to `int' in declaration of `__read_mostly'
/root/hashspeed-0.1/ipt_hashspeed.c:107: warning: data definition has no type or storage class
/root/hashspeed-0.1/ipt_hashspeed.c: In function `dsthash_alloc_init':
/root/hashspeed-0.1/ipt_hashspeed.c:158: error: `hashspeed_cachep' undeclared (first use in this function)
/root/hashspeed-0.1/ipt_hashspeed.c:158: error: (Each undeclared identifier is reported only once
/root/hashspeed-0.1/ipt_hashspeed.c:158: error: for each function it appears in.)
/root/hashspeed-0.1/ipt_hashspeed.c: In function `dsthash_free':
/root/hashspeed-0.1/ipt_hashspeed.c:176: error: `hashspeed_cachep' undeclared (first use in this function)
/root/hashspeed-0.1/ipt_hashspeed.c: In function `htable_create':
/root/hashspeed-0.1/ipt_hashspeed.c:243: warning: assignment discards qualifiers from pointer target type
/root/hashspeed-0.1/ipt_hashspeed.c:259: warning: implicit declaration of function `setup_timer'
/root/hashspeed-0.1/ipt_hashspeed.c: In function `hashspeed_checkentry':
/root/hashspeed-0.1/ipt_hashspeed.c:542: error: `match' undeclared (first use in this function)
/root/hashspeed-0.1/ipt_hashspeed.c: At top level:
/root/hashspeed-0.1/ipt_hashspeed.c:555: warning: "struct xt_match" declared inside parameter list
/root/hashspeed-0.1/ipt_hashspeed.c:555: warning: its scope is only this definition or declaration, which is probably not what you want
/root/hashspeed-0.1/ipt_hashspeed.c:606: warning: initialization from incompatible pointer type
/root/hashspeed-0.1/ipt_hashspeed.c: In function `ipt_hashspeed_init':
/root/hashspeed-0.1/ipt_hashspeed.c:728: warning: implicit declaration of function `ipt_register_matches'
/root/hashspeed-0.1/ipt_hashspeed.c:734: error: `hashspeed_cachep' undeclared (first use in this function)
/root/hashspeed-0.1/ipt_hashspeed.c:754: warning: implicit declaration of function `xt_unregister_matches'
/root/hashspeed-0.1/ipt_hashspeed.c: In function `ipt_hashspeed_fini':
/root/hashspeed-0.1/ipt_hashspeed.c:763: error: `hashspeed_cachep' undeclared (first use in this function)
/root/hashspeed-0.1/ipt_hashspeed.c:767: warning: implicit declaration of function `ipt_unregister_matches'
make[2]: *** [/root/hashspeed-0.1/ipt_hashspeed.o] Error 1
make[1]: *** [_module_/root/hashspeed-0.1] Error 2
make[1]: Leaving directory `/usr/src/linux-2.6.9'
make: *** [ipt_hashspeed.ko] Error 2

论坛徽章:
0
6 [报告]
发表于 2008-01-02 17:21 |只看该作者
原帖由 springwind426 于 2008-1-2 14:34 发表
这只是一个匹配模块,因此,是可以与其他模块一起使用的

我自己测试的精确度还行

比如:
iptables -A FORWARD -i eth1 -p tcp -m hashspeed \! --hashspeed-bytes 256K --hashspeed-mode srcip --hashsp ...



请问限速是 per ip的 还是per net的?

论坛徽章:
0
7 [报告]
发表于 2008-01-02 17:50 |只看该作者
我也是这个错误

[root@wj hashspeed-0.1]# make
Makefile:23: You need to install iptables sources and maybe set IPTABLES_SRC
make -C /lib/modules/2.6.9-5.EL/build M=/tmp/hashspeed-0.1 modules
make[1]: Entering directory `/usr/src/kernels/2.6.9-5.EL-i686'
  CC [M]  /tmp/hashspeed-0.1/ipt_hashspeed.o
/tmp/hashspeed-0.1/ipt_hashspeed.c:27:38: linux/netfilter/x_tables.h: No such file or directory
/tmp/hashspeed-0.1/ipt_hashspeed.c:29:25: linux/mutex.h: No such file or directory

是不是路径的问题?  要在源码路径下编译吗?

论坛徽章:
0
8 [报告]
发表于 2008-01-02 18:40 |只看该作者
原帖由 5iwww 于 2008-1-2 17:21 发表



请问限速是 per ip的 还是per net的?



是per ip

论坛徽章:
0
9 [报告]
发表于 2008-01-02 19:08 |只看该作者
需要编绎内核不?我是MAKE成功了,但再现iptables: Unknown error 4294967295

论坛徽章:
0
10 [报告]
发表于 2008-01-02 19:46 |只看该作者
不知道可不可以和IPRANGE一起匹配使用,我试试先!

iptables -I FORWARD 18 -m iprange --src-range 192.168.1.12-192.168.80 -m hashspeed \! --hashspeed-bytes 256K --hashspeed-mode dstip --hashspeed-name dlimit -j DROP

我的编译成功了,但是运行上面这一句提示:iptables: No chain/target/match by that name
可是用iptables -m hashspeed --help 显示
hashspeed v1.3.8 options:
required parameter: <hashspeed-name>  <hashspeed-mode>  <hashspeed-bytes|hashspeed-packets>
only one revert parameter is accepted, meaning ! hashspeed-bytes or ! hashspeed-packets or ! <hashspeed-bytes || hashspeed-packets>
[--hashspeed-bytes <avg>]       Bytes per second unless followed by K M postfixes
[--hashspeed-bytes-burst <num>] max bytes a burst, default avg*0.1
[--hashspeed-packets <num>]             [Packets per second]
[--hashspeed-packets-burst <num>]       max packets a burst, default 5
--hashspeed-mode <mode>         mode is a comma-separated list of
                                        dstip,srcip
--hashspeed-name <name>         name for /proc/net/ipt_hashspeed/
[--hashspeed-htable-size <num>] number of hashtable buckets
[--hashspeed-htable-max <num>]  number of hashtable entries
[--hashspeed-htable-gcinterval] interval between garbage collection runs
[--hashspeed-htable-expire]     after which time are idle entries expired?
这应该是正常安装上去了呀!
下面是安装记录
[root@mack2050 hashspeed-0.1]# make
make -C /lib/modules/2.6.22.8/build M=/root/hashspeed-0.1 modules
make[1]: Entering directory `/usr/src/linux-2.6.22.8'
  CC [M]  /root/hashspeed-0.1/ipt_hashspeed.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /root/hashspeed-0.1/ipt_hashspeed.mod.o
  LD [M]  /root/hashspeed-0.1/ipt_hashspeed.ko
make[1]: Leaving directory `/usr/src/linux-2.6.22.8'
cc -O2 -Wall -DIPTABLES_VERSION=\"1.3.8\" -I/usr/src/iptables-1.3.8/include -fPIC -c libipt_hashspeed.c
cc -shared -o libipt_hashspeed.so libipt_hashspeed.o
[root@mack2050 hashspeed-0.1]# make install
cp ipt_hashspeed.ko /lib/modules/2.6.22.8/kernel/net/ipv4/netfilter/
cp libipt_hashspeed.so /lib/iptables/

请大家指点,谢谢!

[ 本帖最后由 mack2050 于 2008-1-2 20:01 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP