P4編程入門
潘衛平、龔志敏
相關主題
商品描述
目錄大綱
目 錄
第1章 可編程交換芯片概述..........................................................1
1.1 可編程交換芯片產生的背景...............................................................................1
1.1.1 可編程交換芯片是SDN發展過程的自然產物 ............................................1
1.1.2 可編程交換芯片的發展是學界與業界互相促進的結果 ..............................3
1.2 可編程交換芯片的實現原理...............................................................................4
1.2.1 傳統交換芯片存在的問題 ..............................................................................4
1.2.2 可編程交換芯片的設計目標 ..........................................................................6
1.2.3 可編程交換芯片的參考實現——RMT架構 ................................................7
1.2.4 可編程交換芯片與傳統交換芯片的比較 ....................................................13
1.3 可編程交換芯片的應用場景.............................................................................14
1.3.1 傳統交換設備功能的增強 ............................................................................14
1.3.2 網關類應用 ....................................................................................................15
1.3.3 新型網絡應用 ................................................................................................16
1.4 本章小結..........................................................................................................16
第2章 P4語言概述.................................................................17
2.1 P4語言的特點.................................................................................................17
2.2 P4語言規範....................................................................................................19
2.3 P4編程架構....................................................................................................19
2.4 P4報文路徑....................................................................................................22
2.5 P4 “hello, world” 實例程序..............................................................................24
2.6 P4學習資料....................................................................................................30
2.7 P4語言的發展前景..........................................................................................31
2.8 本章小結..........................................................................................................31
第3章 P4語言詳解.................................................................32
3.1 P4語言概述....................................................................................................32
3.1.1 P4語言的關鍵字 ...........................................................................................32
3.1.2 P4語言的數據類型 .......................................................................................32
3.2 P4語言基本數據類型及其表達式....................................................................33
3.2.1 無符號整型 ....................................................................................................33
3.2.2 有符號整型 ....................................................................................................36
3.2.3 整型常量 ........................................................................................................38
3.2.4 varbit類型 ......................................................................................................38
3.2.5 布爾類型 ........................................................................................................39
3.2.6 error類型 .......................................................................................................40
3.3 復合數據類型及其表達式................................................................................40
3.3.1 枚舉類型 ........................................................................................................41
3.3.2 結構類型 ........................................................................................................44
3.3.3 元組類型 ........................................................................................................45
3.3.4 header類型 ....................................................................................................46
3.3.5 header stack類型 ...........................................................................................49
3.3.6 header union類型 ..........................................................................................50
3.3.7 集合類型 ........................................................................................................51
3.3.8 extern類型 .....................................................................................................53
3.4 有關數據類型的其他主題................................................................................53
3.4.1 類型默認值 ....................................................................................................53
3.4.2 未初始化的值 ................................................................................................54
3.4.3 類型轉換 ........................................................................................................54
3.4.4 類型別名 ........................................................................................................55
3.4.5 類型嵌套規則 ................................................................................................56
3.4.6 運算符的優先級 ............................................................................................56
3.4.7 表達式的求值順序 ........................................................................................58
3.4.8 P4中非法算術表達式舉例 ...........................................................................58
3.5 函數.................................................................................................................59
3.6 語句.................................................................................................................60
3.6.1 賦值語句 ........................................................................................................60
3.6.2 條件語句 ........................................................................................................60
3.6.3 switch語句 .....................................................................................................61
前言
V
3.6.4 return語句 ......................................................................................................62
3.6.5 exit語句 .........................................................................................................62
3.7 control.............................................................................................................62
3.7.1 control的定義 ................................................................................................63
3.7.2 action ..............................................................................................................64
3.7.3 table ................................................................................................................65
3.7.4 control調用的方法 ........................................................................................69
3.8 parser..............................................................................................................70
3.8.1 parser的定義 .................................................................................................70
3.8.2 parser中的語句 .............................................................................................71
3.8.3 parser value set ...............................................................................................73
3.9 deparser..........................................................................................................74
3.9.1 將數據插入報文 ............................................................................................74
3.9.2 計算checksum ...............................................................................................75
3.10 package..........................................................................................................76
3.11 本章小結..........................................................................................................76
第4章 P4開發環境搭建............................................................77
4.1 使用虛擬機搭建P4開發環境..........................................................................78
4.1.1 安裝Ubuntu 20.04 .........................................................................................78
4.1.2 安裝P4開發環境 ..........................................................................................78
4.2 BMv2網絡拓撲的搭建.....................................................................................79
4.3 P4程序的編譯和運行......................................................................................80
4.3.1 編譯P4“hello,world”實例程序 .................................................................80
4.3.2 運行P4“hello,world”實例程序 .................................................................80
4.4 simple_switch_CLI使用方法介紹....................................................................84
4.4.1 simple_switch_CLI命令概覽 ........................................................................84
4.4.2 通過simple_switch_CLI進行表項配置 .......................................................85
4.5 本章小結..........................................................................................................87
第5章 P4編程實例.................................................................88
5.1 可編程parser實例..........................................................................................88
5.1.1 parser實例的主要功能 .................................................................................89
5.1.2 parser實例的代碼清單 .................................................................................89
5.1.3 parser實例代碼的詳細解釋 .........................................................................93
5.1.4 parser實例的運行 .......................................................................................101
5.1.5 parser實例小結 ...........................................................................................104
5.2 最長前綴匹配算法lpm實例..........................................................................104
5.2.1 lpm實例的主要功能 ...................................................................................104
5.2.2 lpm實例的代碼清單 ...................................................................................105
5.2.3 lpm實例代碼的詳細解釋 ...........................................................................108
5.2.4 lpm實例的運行 ...........................................................................................109
5.2.5 lpm實例小結 ...............................................................................................112
5.3 三態匹配ternary實例....................................................................................112
5.3.1 ternary實例的主要功能 ..............................................................................113
5.3.2 ternary實例的代碼清單 ..............................................................................113
5.3.3 ternary實例代碼的詳細解釋 ......................................................................116
5.3.4 ternary實例的運行 ......................................................................................117
5.3.5 ternary實例小結 ..........................................................................................120
5.4 範圍匹配range實例......................................................................................120
5.4.1 range實例的主要功能 ................................................................................120
5.4.2 range實例的代碼清單 ................................................................................121
5.4.3 range實例代碼的詳細解釋 ........................................................................123
5.4.4 range實例的運行 ........................................................................................124
5.4.5 range實例小結 ............................................................................................126
5.5 可編程deparser實例....................................................................................127
5.5.1 deparser實例的主要功能 ............................................................................127
5.5.2 deparser實例的代碼清單 ............................................................................127
5.5.3 deparser實例代碼的詳細解釋 ....................................................................131
5.5.4 deparser實例的運行 ....................................................................................133
5.5.5 deparser實例小結 ........................................................................................135
5.6 selector實例.................................................................................................135
5.6.1 selector實例的主要功能 .............................................................................137
5.6.2 selector實例的代碼清單 .............................................................................137
5.6.3 selector實例代碼的詳細解釋 .....................................................................138
5.6.4 selector實例的運行 .....................................................................................140
5.6.5 selector實例小結 .........................................................................................142
5.7 register實例..................................................................................................143
5.7.1 register實例的主要功能 .............................................................................143
5.7.2 register實例的代碼清單 .............................................................................144
5.7.3 register實例代碼的詳細解釋 .....................................................................147
5.7.4 register實例的運行 .....................................................................................150
5.7.5 register實例小結 .........................................................................................153
5.8 counter實例..................................................................................................154
5.8.1 counter實例的主要功能 .............................................................................154
5.8.2 counter實例的代碼清單 .............................................................................154
5.8.3 counter實例代碼的詳細解釋 .....................................................................156
5.8.4 counter實例的運行 .....................................................................................158
5.8.5 counter實例小結 .........................................................................................161
5.9 meter實例.....................................................................................................161
5.9.1 meter實例的主要功能 ................................................................................161
5.9.2 meter實例的代碼清單 ................................................................................161
5.9.3 meter實例代碼的詳細解釋 ........................................................................162
5.9.4 meter實例的運行 ........................................................................................164
5.9.5 meter實例小結 ............................................................................................166
5.10 resubmit/recirculate實例.............................................................................167
5.10.1 resubmit實例的主要功能 .......................................................................168
5.10.2 resubmit實例的代碼清單 .......................................................................168
5.10.3 resubmit實例代碼的詳細解釋 ...............................................................169
5.10.4 resubmit實例的運行 ...............................................................................172
5.10.5 resubmit實例小結 ...................................................................................175
5.11 clone實例...................................................................................................175
5.11.1 clone實例的主要功能 .............................................................................176
5.11.2 clone實例的代碼清單 .............................................................................176
5.11.3 clone實例代碼的詳細解釋 .....................................................................177
5.11.4 clone實例的運行 .....................................................................................178
5.11.5 clone實例小結 .........................................................................................181
5.12 vnic實例......................................................................................................181
5.12.1 vnic實例的主要功能 ..............................................................................183
5.12.2 vnic實例的代碼清單 ..............................................................................183
5.12.3 vnic實例代碼的詳細解釋 ......................................................................185
5.12.4 vnic實例的運行 ......................................................................................186
5.12.5 vnic實例小結 ..........................................................................................189
5.13 P4Runtime實例..........................................................................................189
5.13.1 P4Runtime實例的主要功能 ...................................................................191
5.13.2 P4Runtime實例的代碼清單 ...................................................................192
5.13.3 P4Runtime實例代碼的詳細解釋 ...........................................................194
5.13.4 P4Runtime控制面代碼 ...........................................................................197
5.13.5 P4Runtime實例的運行 ...........................................................................200
5.13.6 P4Runtime實例小結 ...............................................................................203
第6章 P4項目實戰...............................................................204
6.1 P4項目立項與軟硬件平臺選型......................................................................204
6.1.1 P4項目立項需要考慮的問題 .....................................................................204
6.1.2 P4硬件平臺選型 .........................................................................................205
6.1.3 P4軟件平臺選型 .........................................................................................205
6.2 基於P4和可編程芯片的虛擬路由器.............................................................206
6.2.1 虛擬路由器的功能 ......................................................................................207
6.2.2 虛擬路由器適合用P4和可編程芯片實現嗎 ............................................212
6.2.3 基於P4和可編程芯片的虛擬路由器的需求定義 ....................................214
6.2.4 基於P4和可編程芯片的虛擬路由器的概要設計 ....................................214
6.3 基於P4和可編程芯片的分流器.....................................................................218
6.3.1 網關設備的部署模式 ..................................................................................218
6.3.2 網關集群化部署模式的缺點和問題 ..........................................................222
6.3.3 分流器設計 ..................................................................................................223
6.4 本章小結........................................................................................................230
參考文獻................................................................................231