最近参加一个内核项目,需要测试linux内核tcp/ip网络代码,最终选择google的开源工具packetdrill,网上搜索了packetdrill的资料有一些,但是并不是很多,结合自己使用过程中遇到的问题和心得写一篇packetdrill使用总结,总体上来说packetdrill工具很容易上手,但是使用过程还是遇到一些问题,总共分三部门,第一部分是packetdrill介绍,第二部分是packetdrill语法,第三部分是安装使用过程中遇到的问题,工具开源代码git地址https://github.com/google/packetdrill。
一、Packetdrill测试框架
https://blog.youkuaiyun.com/dog250/article/details/51934338
二、packetdrill语法
脚本中可以包含四种语句:数据包、系统调用、shell命令、python语句。
Packets语法说明:
0.100 < S 0:0(0) win 32792 <mss 1460,sackOK,nop,nop,nop,wscale 7>
0.100 > S. 0:0(0) ack 1 <...>
第一行是packetdrill工具模拟的注入报文,发送给测试的内核系统,第二行是检查点,是被测内核期望输出的检查项,S表示建连的SYN报文,0:0(0)表示报文序列号从0开始到0结束,长度为0,win表示接收窗口大小,<mss 1460,sackOK,nop,nop,nop,wscale 7>表示TCP报文中可选项配置,packets语法类似tcpdump。
时间戳说明:
每条语句都必须以时间戳开头,指明它的执行时间,或者预期事件的发生时间。
时间戳可以使用多种格式:
Absolute(绝对时间):0.75
Relative(相对时间):+0.2
Wildcard(任意时间):*
Range(绝对时间区间):0.750~0.900
Relative Range(相对时间区间):+0.1~+0.2
Loose(允许误差值):--tolerance_usecs=800
Blocking(阻塞时间区间):0.750...0.900
三、安装使用步骤
安装过程:
1、下载代码https://github.com/google/packetdrill到设备
2、安装编译@ubuntu:~/xxx/packetdrill-master/gtests/net/packetdrill$ ./configure & make
3、执行用例./packetdrill ./tests/linux/listen/xxx.pkt
四、遇到的问题
问题一:
现象:
@ubuntu:~/xxx/packetdrill-master/gtests/net/packetdrill/tests/linux/listen$ ../../../packetdrill listen-incoming-ack.pkt
lockall(MCL_CURRENT | MCL_FUTURE): Cannot allocate memory
解决:
su root切换到root下即可运行
问题二:
现象:
root@ubuntu:/home/xxx/packetdrill-master/gtests/net/packetdrill# ./packetdrill mytest.pkt
mytest.pkt:17: error handling packet: bad value outbound TCP option 3
script packet: 0.200000 S. 0:0(0) ack 1 <mss 1460,nop,nop,sackOK,nop,wscale 6>
解决:
修改测试脚本的wscale值,保证接收和发送的wscale值一致,tcp可选项校验可以使用<..>模糊校验
问题三:
现象:
root@ubuntu:/home/xxx/packetdrill-master/gtests/net/packetdrill# ./packetdrill timewait+lastack/timewait_case1.pkt
timewait+lastack/timewait_case1.pkt:15: runtime error in code: can't find getsockopt to get sk_meminfo
解决:
packetdrill-master/gtests/net/packetdrill/code.c里面如下错误
packetdrill-master/gtests/net/packetdrill/platforms.h修改该文件中宏值
#define HAVE_SO_MEMINFO 1 修改完重新编译一次,才可以生效
问题四:
现象:
Packetdrill测试框架和内核代码版本匹配不一致
解决:
/usr/include/linux/tcp.h和测试框架中packetdrill中的tcp.h中结构体有差异,需要修改packetdrill中结构体为测试内核结构体,保持一致
root@ubuntu:/home/xxx/packetdrill-master/gtests/net/packetdrill# ./configure && make
修改完依然报错
code.c:193:43: error: 'const struct _tcp_info' has no member named 'tcpi_delivery_rate'
emit_var(code, "tcpi_delivery_rate", info->tcpi_delivery_rate);
配套的测试框架代码也要注释掉
问题五:
现象:
root@ubuntu:/home/xxx/packetdrill-master/gtests/net/packetdrill# ./packetdrill timewait+lastack/timewait_case2.pkt
timewait+lastack/timewait_case2.pkt:46: error handling packet: timing error: expected outbound packet at 0.900000 sec but happened at 0.800012 sec; tolerance 0.004000 sec
script packet: 0.900000 F. 1001:1001(0) ack 1002
actual packet: 0.800012 F. 1001:1001(0) ack 1002 win 244
解决:
脚本中时间设置不正确,修改脚本时间值为实际时间值0.800012
问题六:
现象:
root@ubuntu:/home/xxx/packetdrill-master/gtests/net/packetdrill# ./packetdrill timewait+lastack/timewait_case2.pkt
timewait+lastack/timewait_case2.pkt:45: runtime error in code: can't find getsockopt to get TCP info
解决:
因为脚本中调用close函数,无法获取tcp状态信息,可以使用`netstat | grep LAST_ACK`进行tcp状态确认
问题七:
现象:
root@ubuntu:/home/xxx/packetdrill-master/gtests/net/packetdrill# ./packetdrill ./tests/linux/timewait+lastack/lastack_incoming_data_seq.pkt
parse_command_line_options argc=2
argv[0] = './packetdrill'
argv[1] = './tests/linux/timewait+lastack/lastack_incoming_data_seq.pkt'
parse_and_run_script: ./tests/linux/timewait+lastack/lastack_incoming_data_seq.pkt
read_script(./tests/linux/timewait+lastack/lastack_incoming_data_seq.pkt)
read_script: 1358 bytes
parse_and_finalize_config
parse_command_line_options argc=2
argv[0] = './packetdrill'
argv[1] = './tests/linux/timewait+lastack/lastack_incoming_data_seq.pkt'
': syntax errorimewait+lastack/lastack_incoming_data_seq.pkt:3: parse error at '
root@ubuntu:/home/xxx/packetdrill-master/gtests/net/packetdrill#
解决:
文件需要是linux的,不可是windows的,可以用bycompare看出文件类型,dos2unix转换下
参考文档:
https://www.usenix.org/system/files/conference/atc13/atc13-cardwell.pdf
https://blog.youkuaiyun.com/zhangskd/article/details/25329579