【ClickHouse為什么這么快?】Hyperscan 超掃描算法:用于現代CPU的“快速-多模式”正則表達式匹配器

A SIMD instruction executes the same operation on multiple data in parallel.

A SIMD Operation

A SIMD operation is performed on multiple lanes of two SIMD registers independently, and the results are stored in the third register. Modern CPU supports a number of SIMD instructions that can work on specialized vector registers (SSE, AVX, etc.). The latest AVX512 instructions support up to 512-bit operations simultaneously.

一次 SIMD 操作是對兩個 SIMD 寄存器的多個通道獨立進行,然后結果存儲在第三個寄存器中。現代 CPU 支持可以在專用向量寄存器(SSE、AVX 等)上運行的 SIMD 指令。最新的 AVX512 指令最多可同時支持 512 位操作。

Hyperscan 超掃描算法:用于現代CPU的“快速-多模式”正則表達式匹配器

Hyperscan: A Fast Multi-pattern Regex Matcher for Modern CPUs

Regular expression matching serves as a key functionality of modern network security applications. Unfortunately, it often becomes the performance bottleneck as it involves

compute-intensive scan of every byte of packet payload. With trends towards increasing network bandwidth and a large ruleset of complex patterns, the performance re-quirement gets ever more demanding. In this paper, we present Hyperscan, a high performance regular expression matcher for commodity server machines.?

Hyperscan employs two core techniques for efficient pattern matching.?

- First, it exploits graph decomposition that translates regular expression matching into a series of string and finite automata matching. Unlike existing solutions, string matching becomes a part of regular expression matching, eliminating duplicate operations. Decomposed regular expression components also increase the chance of fast DFA matching as they tend to be smaller than the original pattern.

- Second, Hyperscan accelerates both string and finite automata matching using SIMD operations, which brings substantial through-put improvement.?

Our evaluation shows that Hyperscan improves the performance of Snort by a factor of 8.7 for a real traffic trace.

Deep packet inspection (DPI) provides the fundamental functionality for many middlebox applications that deal with L7 protocols, such as intrusion detection systems (IDS)。

Despite continued efforts, the performance of regex matching on a commodity server still remains impractical to directly serve today’s large network bandwidth. Instead, the de-facto best practice of high-performance DPI generally employs multi-string pattern matching as a pre-condition for expensive regex matching.?

This hybrid approach (or prefiltering) is attractive as multi-string matching is known to outperform multi-regex matching by two orders of magnitude , and most input traffic is innocent, making it more efficient to defer a rigorous check. For example, popular IDSes like Snort and Suricata specify a string pattern per each regex for prefiltering, and launch the corresponding regex matching only if the string is found in the input stream.

盡管一直在努力,商品服務器上的正則表達式匹配的性能仍然不適合直接服務于當今的大網絡帶寬。相反,高性能DPI的實際最佳實踐,通常采用多字符串模式匹配作為昂貴的正則表達式匹配的先決條件。

這種混合方法(或預過濾)很有吸引力,因為眾所周知,多字符串匹配的性能比多正則表達式匹配高出兩個數量級,而且大多數輸入流量都是無辜的,這使得推遲嚴格檢查更加有效。例如,像Snort和Suricata這樣的流行IDSes,為每個正則表達式指定一個用于預過濾的字符串模式,并且,只有在輸入流中找到字符串時,才啟動相應的正則表達式匹配。

However, the current prefilter-based matching has a number of limitations.?

First, string keywords are often defined manually by humans. Manual choice does not scale as the ruleset expands over time, and improper keywords would waste CPU cycles on redundant regex matching.?

Second, string matching and regex matching are executed as two separate tasks, with the former leveraged only as a trigger for the latter. This results in duplicate matching of the string keywords when the corresponding regex matching is executed.?

Third, current regex matching typically translates an entire regex into a single finite automaton (FA). If the number of deterministic finite automaton (DFA) states becomes too large, one must resort to a slower non-deterministic finite automaton (NFA) for matching of the whole regex.

然而,當前基于前置濾波器的匹配有許多限制。

首先,字符串關鍵字,通常由人手工定義。手動選擇不會隨著規則集隨著時間的推移而擴展,不正確的關鍵字,會在冗余的正則表達式匹配上浪費CPU周期。

其次,字符串匹配和正則表達式匹配,作為兩個獨立的任務執行,前者僅作為后者的觸發器。當執行相應的正則表達式匹配時,這會導致字符串關鍵字的重復匹配

第三,當前正則表達式匹配,通常將整個正則表達式轉換為單個有限自動機(FA)。如果確定型有窮自動機(DFA)狀態的數目過大,則必須使用較慢的非確定型有窮自動機(NFA)來匹配整個正則表達式。

Hyperscan, a high performance regex matching system that exploits regex decomposition as the first principle. Regex decomposition splits a regex pattern into a series of disjoint string and FA components。

This translates regex matching into a sequence of decomposed subregex matching whose execution and matching order is controlled by fast string matching.

超掃描,一個高性能正則表達式匹配系統,利用正則表達式分解作為第一原則。正則表達式分解將正則表達式模式拆分為一系列不相交的字符串和FA組件

這將正則表達式匹配轉換為分解的子正則表達式匹配序列,其執行和匹配順序由快速字符串匹配控制。

This design brings a number of benefits.?

First, our regex decomposition identifies string components automatically by performing rigorous structural analyses on the NFA graph of a regex. Our algorithm ensures that the extracted strings are pre-requisite for the rest of regex matching.

Second, string matching is run as a part of regex matching rather than being employed only as a trigger. Unlike the prefilter-based design, Hyperscan keeps track of the state of string matching throughout regex matching and avoids any redundant operations.?

Third, FA component matching is executed only when all relevant string and FA components are matched. This eliminates unnecessary FA component matching, which allows efficient CPU utilization.?

Finally, most decomposed FA components tend to be small, so they are more likely to be able to be converted to a DFA and benefit from fast DFA matching.

這種設計帶來了許多好處。

首先,正則表達式分解,通過對正則表達式的NFA圖,執行嚴格的結構分析,來自動識別字符串組件。算法確保提取的字符串是正則表達式匹配其余部分的先決條件。

其次,字符串匹配,作為正則表達式匹配的一部分運行,而不是僅作為觸發器使用。與基于前置過濾器的設計不同,Hypercan在整個正則表達式匹配過程中,跟蹤字符串匹配的狀態,并避免任何冗余操作。

第三,FA組件匹配,僅在匹配所有相關字符串和FA組件時執行。這消除了不必要的FA組件匹配,從而允許高效的CPU利用率。

最后,大多數分解的FA組件往往很小,因此它們更有可能轉換為DFA,并受益于快速的DFA匹配。


Beyond the benefits of regex decomposition, Hyperscan also brings a significant performance boost with single-instruction-multiple-data (SIMD)?accelerated pattern matching algorithms.






Source code at https://github.com/intel/hyperscan


參考資料:

https://github.com/intel/hyperscan

Hyperscan is a high-performance multiple regex matching library. It follows the regular expression syntax of the commonly-used libpcre library, but is a standalone library with its own C API.

Hyperscan uses hybrid automata techniques to allow simultaneous matching of large numbers (up to tens of thousands) of regular expressions and for the matching of regular expressions across streams of data.

Hyperscan is typically used in a DPI library stack.

https://www.usenix.org/sites/default/files/conference/protected-files/nsdi19_slides_wang_xiang.pdf

https://www.youtube.com/watch?v=Le67mP-jIa8

https://www.usenix.org/conference/nsdi19/presentation/wang-xiang

https://www.usenix.org/system/files/nsdi19-wang-xiang.pdf

http://intel.github.io/hyperscan/dev-reference/getting_started.html#very-quick-start

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,837評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,196評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,688評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,654評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,456評論 6 406
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,955評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,044評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,195評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,725評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,608評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,802評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,318評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,048評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,422評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,673評論 1 281
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,424評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,762評論 2 372