redhat7 上安装dummynet

更多请访问 http://www.webpersonaldeveloper.cn

摘要:

在redhat 上部署dummynet 需要将ipfw 编译为内核模块,而ipfw需要调用linux kernel的一些函数。
linux kernel版本在不断提升,相关的数据结构也在变化。所以可能需要根据你要部署的系统内核版本,来调整参数

提醒

如果你要部署在Redhat6,CentOS6等内核版本比较低的平台,不必参考下面的部署流程。
这篇文章就可以满足的你要求
http://xmodulo.com/how-to-install-dummynet-on-centos.html

2016-08-18 update
不需要下载和系统同一版本的内核,因为并不是真正的编译内核。netfilter 默认就在内核中启动。
所以只用下面即可,可以跳过准备内核版本这步骤

yum install kernel-devel
yum group install "Development Tools

如果遇到问题,再返回这篇进一步学习

dummynet简介

dummynet 是linux下面(现在也可以部署在window下)一款开源的网络带宽控制工具,
在网站上线前,可能需要测试带宽对站点的影响。而dummynet就是为此而生。

部署流程

准备内核版本

  • 参看系统内核版本

    uname -r

我们需要将IPFW编译成内核模块,请确保
IPFW用到的内核源码版本同你LINUX系统运行内核版本一致。

我的linux系统版本redhat7.2,内核版本3.10.0-327.13.1.el7.x86_64

  • 下载此版本的内核源码包
    kernel-3.10.0-327.13.1.el7.src.rpm

http://vault.centos.org/7.2.1511/updates/Source/SPackages/

使用wget或者直接下载放在/usr/src/kernels 目录

  • 解压源码包

    `

    rpm2cpio kernel-3.10.0-327.13.1.el7.src.rpm |cpio -div

    tar xvf linux-3.10.0-327.13.1.el7.tar.xz
    cd 3.10.0-327.13.1.el7.x86_64
    make oldconfig
    make prepare
    make scripts
    `

编译dummynet

git clone https://github.com/FS1360472174/dummynet.git
cd dummynet
make KERNELPATH=/usr/src/kernels/3.10.0-327.13.1.el7.x86_64

加载ipfw 模块

cd kipfw-mod
insmod ipfw_mod.ko
cd dummy/ipfw
cp ipfw /sbin
chmod 700 /sbin/ipfw

验证ipfw

ipfw add pipe 2 in proto tcp

optional,将ipfw 设置为boot启动

 cp ipfw_mod.ko /lib/modules/3.10.0-327.13.1.el7.x86_64/kernel/net/netfilter
 depmod
 sh -c 'echo modprobe ipfw_mod >> /etc/rc.modules'
 chmod +x /etc/rc.modules

问题分析

1.insmod: ERROR: could not insert module ipfw_mod.ko: Invalid module format

解决:

modinfo ipfw_mod.ko 看下vermagic版本是不是uname -r的版本。
然后重新编译ipfw 模块

make clean
make KERNELPATH=/usr/src/kernels/3.10.0-327.13.1.el7.x86_64

如果仍然不行在/usr/src/kernels/$source 执行make,然后reboot

2.ipfw: getsockopt(IP_FW_ADD): Protocol not available

解决:

ipfw 模块未加载到内核
可以lsmod |grep ipfw看下
需要重述上述步骤,将ipfw编译进内核模块

3.编译模块时报错
类似于ipfw2_mod.c line 848 nf_hook_ops.hk struct have erors.

解决:
Hook structure 在各个版本的linux中定义不一样,所以如果是从dummynet 站点中下载的
老的dummynet 包或者从这个download下来的包可能就有错误。
https://github.com/luigirizzo/dummynet

建议你查看下当前所用系统的hook 结构

/usr/src/kernels/linux-3.10.0-327.4.5.el7/include/linux/netfilter.h
定义了nf_hook_ops,nf_hookfn的结构
struct nf_hook_ops {
struct list_head list;

    /* User fills in from here down. */
    nf_hookfn       *hook;
    struct module   *owner;
    void            *priv;
    u_int8_t        pf;
    unsigned int    hooknum;
    /* Hooks are ordered in ascending priority. */
    int             priority;

    /* Reserved for use in the future RHEL versions. Set to zero. */
    unsigned long   __rht_reserved1;
    unsigned long   __rht_reserved2;
    unsigned long   __rht_reserved3;
    unsigned long   __rht_reserved4;
    unsigned long   __rht_reserved5;
};

typedef unsigned int nf_hookfn(const struct nf_hook_ops *ops,
   struct sk_buff *skb,
   const struct net_device *in,
   const struct net_device *out,
#ifndef __GENKSYMS__
   const struct nf_hook_state *state
#else
   int (*okfn)(struct sk_buff *)
#endif
   );

参考:

http://vault.centos.org/7.2.1511/updates/Source/SPackages/kernel-3.10.0-327.4.5.el7.src.rpm
https://www.rpmfind.net/linux/RPM/centos/updates/7.2.1511/x86_64/Packages/kernel-3.10.0-327.13.1.el7.x86_64.html
http://xmodulo.com/how-to-install-dummynet-on-centos.html
https://access.redhat.com/articles/3078

原文地址:https://www.cnblogs.com/stoneFang/p/6715301.html