1. 软件需求
(1) C/C++编译器
hyperscan使用C开发,且需要C99和C11支持,目前支持的编译器有
GCC, v4.8.1 or higher
Clang, v3.4 or higher (with libstdc++ or libc++)
Intel C++ Compiler v15 or higher
(2) 第三方依赖库
依赖项 版本 说明
-------------------------------------------------
CMake >=2.8.11
Ragel 6.9
Python 2.7
Boost >=1.57 仅需要regex
Pcap >=0.8 Optional: 仅用于示例程序
注1:boost不需要编译安装,要下载源码包,解压后执行类似
# ln -s /root/zhn/software/boost_1_58_0/boost ../hyperscan-5.0.0/include/boost
的命令创建符号链接,注意boost路径需要是绝对路径,否则可能找不到
注2:pcap库会依赖flex和bison
2. 编译过程
# cd hyperscan-5.0.0
# mkdir build
# cd build
# cmake .. -DBUILD_STATIC_AND_SHARED=on -DCMAKE_BUILD_TYPE=Release
# make -j 20
# ll lib
total 22452
-rw-r--r--. 1 root root 174930 May 8 16:22 libcorpusomatic.a
-rw-r--r--. 1 root root 13598 May 8 15:50 libcrosscompileutil.a
-rw-r--r--. 1 root root 12624 May 8 15:50 libdatabaseutil.a
-rw-r--r--. 1 root root 38770 May 8 15:56 libexpressionutil.a
-rw-r--r--. 1 root root 17232358 May 8 16:25 libhs.a
-rw-r--r--. 1 root root 5502018 May 8 16:17 libhs_runtime.a
# make install
Install the project...
-- Install configuration: "RELWITHDEBINFO"
-- Installing: /usr/local/lib64/pkgconfig/libhs.pc
-- Installing: /usr/local/include/hs/hs.h
-- Installing: /usr/local/include/hs/hs_common.h
-- Installing: /usr/local/include/hs/hs_compile.h
-- Installing: /usr/local/include/hs/hs_runtime.h
-- Installing: /usr/local/lib64/libhs_runtime.a
-- Installing: /usr/local/lib64/libhs.a
-- Installing: /usr/local/share/doc/hyperscan/examples/simplegrep.c
-- Installing: /usr/local/share/doc/hyperscan/examples/pcapscan.cc
-- Installing: /usr/local/share/doc/hyperscan/examples/patbench.cc
-- Installing: /usr/local/share/doc/hyperscan/examples/README.md
3. 出现问题
(1) 汇编程序太老,无法汇编gcc发出的指令
/tmp/ccmMtaRQ.s:12011: Error: no such instruction: `shrx %r8d,%eax,%eax'
/tmp/ccmMtaRQ.s:12064: Error: no such instruction: `shlx %rax,%rsi,%rax'
/tmp/ccmMtaRQ.s:12134: Error: no such instruction: `shlx %rsi,%rax,%rax'
...
当前指令集:
# gcc -march=native -Q --help=target | grep march
需要安装binutils
# wget https://ftp.gnu.org/gnu/binutils/binutils-2.27.tar.gz
# tar -zxvf binutils-2.27.tar.gz
# cd binutils-2.27
# ./configure --prefix=/usr
# make
# make install
4. demo程序
(1) simplegrep
# pwd
/root/zhn/software/hyperscan-5.0.0/examples
# ../build/bin/simplegrep int simplegrep.c
Scanning 8051 bytes with Hyperscan
Match for pattern "int" at offset 1774
Match for pattern "int" at offset 2244
Match for pattern "int" at offset 2707
Match for pattern "int" at offset 2730
Match for pattern "int" at offset 2756
Match for pattern "int" at offset 2845
Match for pattern "int" at offset 2875
Match for pattern "int" at offset 3152
Match for pattern "int" at offset 3228
Match for pattern "int" at offset 3524
Match for pattern "int" at offset 3732
Match for pattern "int" at offset 3889
Match for pattern "int" at offset 4101
Match for pattern "int" at offset 4249
Match for pattern "int" at offset 4347
Match for pattern "int" at offset 4528
Match for pattern "int" at offset 4873
Match for pattern "int" at offset 5048
Match for pattern "int" at offset 5086
Match for pattern "int" at offset 5095
Match for pattern "int" at offset 5153
Match for pattern "int" at offset 5869
Match for pattern "int" at offset 6090
Match for pattern "int" at offset 6121
Match for pattern "int" at offset 7099
Match for pattern "int" at offset 7146
Match for pattern "int" at offset 7322
Match for pattern "int" at offset 7484
Match for pattern "int" at offset 7661
(2) pcapscan
准备一个规则文件matchFile和pcap文件baidu_image.pcap
matchFile内容
123:/baidu/
456:/[f|F]ile/
执行
# ./pcapscan matchFile baidu_image.pcap
Pattern file: matchFile
Compiling Hyperscan databases with 2 patterns.
Hyperscan streaming mode database compiled in 0.00116827 seconds.
Hyperscan block mode database compiled in 0.000731925 seconds.
PCAP input file: baidu_image.pcap
449 packets in 130 streams, totalling 364977 bytes.
Average packet length: 812 bytes.
Average stream length: 2807 bytes.
Streaming mode Hyperscan database size : 4904 bytes.
Block mode Hyperscan database size : 4904 bytes.
Streaming mode Hyperscan stream state size: 22 bytes (per stream).
Streaming mode:
Total matches: 266
Match rate: 0.7463 matches/kilobyte
Throughput (with stream overhead): 12860.81 megabits/sec
Throughput (no stream overhead): 14072.15 megabits/sec
Block mode:
Total matches: 264
Match rate: 0.7407 matches/kilobyte
Throughput: 17829.85 megabits/sec
WARNING: Input PCAP file is less than 2MB in size.
This test may have been too short to calculate accurate results.
(3) patbench
# ./patbench matchFile baidu_image.pcap
Base signatures: matchFile PCAP input file: baidu_image.pcap Repeat count: 1 Mode: streaming
Scan time 0.008 sec, Scanned 364977 bytes, Throughput 381.597 Mbps, Matches 266
Number of signatures: 2
Base performance: 12379.131 Megabits/s
Cutting signatures cumulatively for 1 generations
Generation 0 . Performance: 23840.098 Megabits/s (1.926x) after cutting:
456:/[f|F]ile/