Linux 内核发包工具 pktgen

本文来自 我的博客。博客的文章保持更新,此文可能不是最新状态。

什么是 pktgen

pktgen 是一款 Linux 发包工具,可在内核高速发包。

Linux packet generator is a tool to generate packets at very high speed in the kernel.

使能 pktgen

内核配置文件开启 CONFIG_NET_PKTGEN,通过查看是否有 /proc/net/pktgen/ 目录确认是否编译 OK。

使用 pktgen 发包

  • 添加设备
echo "add_device eth3" > /proc/net/pktgen/kpktgend_0
  • 配置报文

配置方法,样例仅给出部分配置

echo "pkt_size 64" > /proc/net/pktgen/eth3
echo "count 1000000" > /proc/net/pktgen/eth3
echo "dst_mac aa:bb:cc:dd:ee:ff" > /proc/net/pktgen/eth3

查看配置结果:

cat /proc/net/pktgen/eth3
  • 发送报文
echo "start" > /proc/net/pktgen/pgctrl

查看发送结果:

cat /proc/net/pktgen/eth3

发包样例

[1209-20:29:14:517] root@ubuntu:/proc/net/pktgen# echo "pkt_size 1500" > /proc/net/pktgen/eth3   
[1209-20:29:26:980] root@ubuntu:/proc/net/pktgen# echo "count 1000000" > /proc/net/pktgen/eth3         
[1209-20:29:29:862] root@ubuntu:/proc/net/pktgen# echo "start" > /proc/net/pktgen/pgctrl      
[1209-20:29:48:407] root@ubuntu:/proc/net/pktgen# cat /proc/net/pktgen/eth3                   
[1209-20:29:48:430] Params: count 1000000  min_pkt_size: 1500  max_pkt_size: 1500
[1209-20:29:48:435]      frags: 0  delay: 0  clone_skb: 0  ifname: eth3
[1209-20:29:48:437]      flows: 0 flowlen: 0
[1209-20:29:48:441]      queue_map_min: 0  queue_map_max: 0
[1209-20:29:48:444]      dst_min:   dst_max: 
[1209-20:29:48:447]         src_min:   src_max: 
[1209-20:29:48:452]      src_mac: 02:10:18:2c:f8:0e dst_mac: 00:00:00:00:00:00
[1209-20:29:48:458]      udp_src_min: 9  udp_src_max: 9  udp_dst_min: 9  udp_dst_max: 9
[1209-20:29:48:460]      src_mac_count: 0  dst_mac_count: 0
[1209-20:29:48:464]      Flags: 
[1209-20:29:48:464] Current:
[1209-20:29:48:467]      pkts-sofar: 1000000  errors: 0
[1209-20:29:48:472]      started: 3219370012us  stopped: 3233111900us idle: 299798us
[1209-20:29:48:477]      seq_num: 1000001  cur_dst_mac_offset: 0  cur_src_mac_offset: 0
[1209-20:29:48:481]      cur_saddr: 192.168.2.1  cur_daddr: 0.0.0.0
[1209-20:29:48:486]      cur_udp_dst: 9  cur_udp_src: 9
[1209-20:29:48:490]      cur_queue_map: 0
[1209-20:29:48:491]      flows: 0
[1209-20:29:48:496] Result: OK: 13741888(c13442090+d299798) usec, 1000000 (1500byte,0frags)
[1209-20:29:48:499]   72770pps 873Mb/sec (873240000bps) errors: 0
[1209-20:30:05:839] root@ubuntu:/proc/net/pktgen# 

完整说明

From linuxfoundation.org pktgen

Setup

Enable CONFIG_NET_PKTGEN to compile and build pktgen.o either in kernel or as module. Module is preferred. insmod pktgen if needed. Once running pktgen creates a thread on each CPU where each thread has affinty it's CPU. Monitoring and controlling is done via /proc. Easiest to select a suitable a sample script and configure.

On a dual CPU:

 ps aux | grep pkt
 root       129  0.3  0.0     0    0 ?        SW    2003 523:20 [pktgen/0]
 root       130  0.3  0.0     0    0 ?        SW    2003 509:50 [pktgen/1]

For montoring and control pktgen creates:

  • /proc/net/pktgen/pgctrl
  • /proc/net/pktgen/kpktgend_X
 # cat /proc/net/pktgen/kpktgend_0 
 Name: kpktgend_0  max_before_softirq: 10000
 Running: 
 Stopped: eth1 
 Result: OK: max_before_softirq=10000

Most important the devices assigned to thread. Note! A device can only belong to one thread.
*/proc/net/pktgen/ethX
Param section holds configured info. Current hold running stats. Result is printed after run or after interruption. Example:

 # cat /proc/net/pktgen/eth1
 Params: count 10000000  min_pkt_size: 60  max_pkt_size: 60
      frags: 0  delay: 0  clone_skb: 1000000  ifname: eth1
      flows: 0 flowlen: 0
      dst_min: 10.10.11.2  dst_max: 
      src_min:   src_max: 
      src_mac: 00:00:00:00:00:00  dst_mac: 00:04:23:AC:FD:82
      udp_src_min: 9  udp_src_max: 9  udp_dst_min: 9  udp_dst_max: 9
      src_mac_count: 0  dst_mac_count: 0 
      Flags: 
 Current:
      pkts-sofar: 10000000  errors: 39664
      started: 1103053986245187us  stopped: 1103053999346329us idle: 880401us
      seq_num: 10000011  cur_dst_mac_offset: 0  cur_src_mac_offset: 0
      cur_saddr: 0x10a0a0a  cur_daddr: 0x20b0a0a
      cur_udp_dst: 9  cur_udp_src: 9
      flows: 0
 Result: OK: 13101142(c12220741+d880401) usec, 10000000 (60byte,0frags)
   763292pps 390Mb/sec (390805504bps) errors: 39664

Confguring

This is done via the /proc interface easiest done via pgset in the scripts

Examples:

 pgset "clone_skb 1"     sets the number of copies of the same packet
 pgset "clone_skb 0"     use single SKB for all transmits
 pgset "pkt_size 9014"   sets packet size to 9014
 pgset "frags 5"         packet will consist of 5 fragments
 pgset "count 200000"    sets number of packets to send, set to zero
                        for continious sends untill explicitl stopped.
 pgset "delay 5000"      adds delay to hard_start_xmit(). nanoseconds
 pgset "dst 10.0.0.1"    sets IP destination addres
                        (BEWARE! This generator is very aggressive!)
 pgset "dst_min 10.0.0.1"            Same as dst
 pgset "dst_max 10.0.0.254"          Set the maximum destination IP.
 pgset "src_min 10.0.0.1"            Set the minimum (or only) source IP.
 pgset "src_max 10.0.0.254"          Set the maximum source IP.
 pgset "dst6 fec0::1"     IPV6 destination address
 pgset "src6 fec0::2"     IPV6 source address
 pgset "dstmac 00:00:00:00:00:00"    sets MAC destination address
 pgset "srcmac 00:00:00:00:00:00"    sets MAC source address
 pgset "src_mac_count 1" Sets the number of MACs we'll range through.  
                        The 'minimum' MAC is what you set with srcmac.
 pgset "dst_mac_count 1" Sets the number of MACs we'll range through.
                        The 'minimum' MAC is what you set with dstmac.
 pgset "flag [name]"     Set a flag to determine behaviour.  Current flags
                        are: IPSRC_RND #IP Source is random (between min/max),
                             IPDST_RND, UDPSRC_RND,
                             UDPDST_RND, MACSRC_RND, MACDST_RND 
  pgset "udp_src_min 9"   set UDP source port min, If < udp_src_max, then
                        cycle through the port range.
  pgset "udp_src_max 9"   set UDP source port max.
  pgset "udp_dst_min 9"   set UDP destination port min, If < udp_dst_max, then
                        cycle through the port range.
  pgset "udp_dst_max 9"   set UDP destination port max.
  pgset stop                  aborts injection. Also, ^C aborts generator.

Examples

A collection of small tutorial scripts for pktgen is in expamples dir.

  • pktgen.conf-1-1 # 1 CPU 1 dev
  • pktgen.conf-1-2 # 1 CPU 2 dev
  • pktgen.conf-2-1 # 2 CPU's 1 dev
  • pktgen.conf-2-2 # 2 CPU's 2 dev
  • pktgen.conf-1-1-rdos # 1 CPU 1 dev w. route DoS
  • pktgen.conf-1-1-ip6 # 1 CPU 1 dev ipv6
  • pktgen.conf-1-1-ip6-rdos # 1 CPU 1 dev ipv6 w. route DoS
  • pktgen.conf-1-1-flows # 1 CPU 1 dev multiple flows.
    Run in shell: ./pktgen.conf-X-Y It does all the setup including sending.

Interrupt affinity

Note when adding devices to a specific CPU there good idea to also assign /proc/irq/XX/smp_affinity so the TX-interrupts gets bound to the same CPU. as this reduces cache bouncing when freeing skb's.

Commands

Pgcontrol commands:

  • start
  • stop
    Thread commands:
  • add_device
  • rem_device_all
  • max_before_softirq
    Device commands:
  • count
  • clone_skb
  • debug
  • frags
  • delay
  • src_mac_count
  • dst_mac_count
  • pkt_size
  • min_pkt_size
  • max_pkt_size
  • udp_src_min
  • udp_src_max
  • udp_dst_min
  • udp_dst_max
  • flag
    • IPSRC_RND
    • TXSIZE_RND
    • IPDST_RND
    • UDPSRC_RND
    • UDPDST_RND
    • MACSRC_RND
    • MACDST_RND
  • dst_min
  • dst_max
  • src_min
  • src_max
  • dst_mac
  • src_mac
  • clear_counters
  • dst6
  • src6
  • flows
  • flowlen
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 161,780评论 4 369
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 68,424评论 1 305
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 111,397评论 0 254
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,576评论 0 218
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,997评论 3 295
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,945评论 1 224
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 32,107评论 2 317
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,850评论 0 208
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,625评论 1 250
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,804评论 2 253
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,285评论 1 265
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,613评论 3 261
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,291评论 3 242
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,164评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,963评论 0 201
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 36,096评论 2 285
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,886评论 2 278

推荐阅读更多精彩内容

  • ping error: Destination off-link but no route作者 codercjg ...
    codercjg阅读 978评论 0 1
  • No.6 无定乡 奥特曼的降临是有条件的,首先,需要他指定的人,我们称之为“引导者”。那人可以家财万贯,可以一贫如...
    贝龙阅读 19,912评论 16 10
  • 设计稿是否 兼容iPad字体方案(px/rem),字体过小采取浏览器最小显示单、多行省略图标转icon多行line...
    I_am_Cynthia阅读 586评论 0 1
  • 科伦坡的热空气席卷了全身。换钱的工作人员手招向我们,吐着拙略的普通话:换钱,这里。卖dialog卡的工作人员也不标...
    meters阅读 353评论 0 1
  • 老师问我是哪两张单子,我说是在院系领导那里签字。明显,老师想获得的信息和我给出的信息不一致。 回答别人的问题,首先...
    HD_coding阅读 1,222评论 0 0