P4語法(3)Table,Action


Table

table是p4的匹配——動作表,定義了匹配字段(key)、動作(action)和一些其他相關屬性。

其處理數據包的流程:

  1. Key construction.建立其匹配字段
  2. Key lookup in a lookup table.The result of key lookup is an "action". 數據包中去匹配table中的key中的字段,並獲得要執行的"action"。
  3. Action execution.執行動作。

key

key由一個個表單對組成(e:m),其中e是對應數據包中匹配的字段,而m是一個match_kind常數用來表示匹配的算法。

例如:

1 key = {
2     hdr.ipv4.dstAddr:lpm;
3 }

這個就是以ipv4頭的目的地址作為匹配字段,采用的是lpm(最長前綴字段)匹配方式。

p416 core現在提供三種默認的match_kind。 

1 match_kind{
2     lpm,//最長前綴字段
3     ternary,//三元匹配
4     exact//完全匹配
5 }

 

Action

table中的action list是列舉了該table支持的action類型。用於action的匹配。

其他屬性

p416提供了一些預設的其他屬性:

  • default_action:當table miss的時候執行的動作。
  • counters:計數器
  • size:table大小
  • implementation:指定table實際運作方式,這部分通常取決於架構,例如v1model中actionprofile提供了通過hash的方式隨機選擇一個actionprofilemember去執行。
  • const entries:預設的table entry,在編譯階段會寫到編譯好的檔案中。

Action

關於action,p4中在table里可以利用action去對封包做出處理,action非常類似於其他高級語言中所示的函數,抽象程度可以很高,並且表現出協議無關的特性。這也能體現一部分p4的擴展性。

action可以讀取控制平面(control plane)提供的數據進行操作,然后根據action的代碼內容影響數據平面(data plane)的工作。

對於action的定義:

1 action action_name(parameter1,parameter2,……){
2     //語句塊
3 }

 

而且p4有提供不少基本操作(Primitive Actions),這些action高度抽象,在p416中,大部分的基本操作被移動到了一些函數庫中(arch.p4或者vendor.p4),部分操作依然保留在了core.p4中。更多詳細的內容在spec中。

例如ipv4轉發的code:

1 action ipv4_forward(bit<48> dstAddr,bit<9> port){
2     standard_metadata.egress_spec = port;
3     hdr.ethernet.srcAddr = hdr.ethernet.dstAddr;
4     hdr.ethernet.dstAddr = dstAddr;
5     hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
6 }

 


樣例

這里用tutorial中的ipv4轉發代碼做樣例。

 1 action drop(){
 2         mark_to_drop();//將要丟棄的包標記為丟棄
 3     }
 4     
 5     action ipv4_forward(bit<48> dstAddr,bit<9> port){
 6         standard_metadata.egress_spec = port;
 7         hdr.ethernet.srcAddr = hdr.ethernet.dstAddr;
 8         hdr.ethernet.dstAddr = dstAddr;
 9         hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
10     }
11 table ipv4_lpm{
12         key = {
13             hdr.ipv4.dstAddr: lpm;
14         }
15         
16         actions = {
17             ipv6_forward;
18             drop;
19             NoAction;
20         }
21         
22         size = 1024;
23         
24         default_action = drop();
25     }

 

然后給一個switch中定義的流表項:

 1 {
 2       "table": "MyIngress.ipv4_lpm",   
 3       "match": {
 4         "hdr.ipv4.dstAddr": ["10.0.1.1", 32]
 5       },
 6       "action_name": "MyIngress.ipv4_forward",
 7       "action_params": {
 8         "dstAddr": "00:00:00:00:01:01",
 9         "port": 1
10       }
11 }

 

這個table就是對應ipv4.lpm的,匹配的dstAddr為10.0.1.1,執行的action為ipv4.forward,傳入的兩個參數為mac地址00:00:00:00:01:01,端口1。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM