這一次要講的是 Counter、Meter 以及 Register 這三個部分。

這三個東西比較算是廣域的,也就是說不會因為一個封包處理完就結束。

在 P4 當中,他們將這三個東西視為”Cell”,也就是在 P4 的定義中,他們需要先被定義成一組陣列才可以使用。

Counter


Counter 的定義方式如下:

counter counter_name {
    type : packets | bytes;
    [direct : table_name;]
    [static : table_name;]
    [instance_count : const_value;]
    [min_width : const_value;]
    [staturating;]
}

type 顧名思義就是 Counter 的類型,依據設定使用不同的方式來統計,在 P4 當中可以使用 packet 以及 byte 兩種。

在一個 counter 中 direct 以及 static 這兩個只會出現一個:

direct:指定的 table 中所有的 entry 都會套用這一個 counter,也就是說這一個 counter 不能被 count 這一個 action 呼叫,否則會出現錯誤,

static:指定的 table 中可以使用 count 這一個 action 來增加 counter 中的內容,但是如果被其他的 table 呼叫,則會出現錯誤。

註:若沒有指定 static 或是 direct,則任何一個 table 都可以透過 count 這個 action 來增加此 counter 的數值。

instance_count:用來配置這一個 counter 的實體數量,要注意的是,當一個 Counter 具有 direct 屬性時,則這一個屬性將不會存在,否則他就是必要的屬性。

min_width:指定這一個 counter 的最小大小(單位:bits)

staturating:若有寫這一個屬性,則在 counter 抵達上限時時候將會停止計數,否則將會歸零。

Meter


Meter 寫法如下:

meter meter_name {
    type : bytes | packets;
    result : field_ref;
    [direct : table_name;]
    [static : table_name;]
    [instance_count : const_value;]
}

type、direct、static 以及 instance_count 與 counter 相同,唯一不同的是 result。

result:指定一個 field(如 metadata 中的資料)來儲存資料。

Register


register 的定義方式如下:

register register_name {
    width : const_value;
    layout : header_type_name;
    [direct : table_name;]
    [static : table_name;]
    [instance_count : const_value;]
    [attribute_list;]
}

width 以及 layout 兩個只會出現一個,width 與前兩個相同,而 layout 則是直接套用一個定義好的 header 結構。

direct、static、instance_count 也是跟前兩個相同。

attribute_list 的定義如下

attributes : entry , entry , ……..

每一個 entry 可以是 signed 或是 staturing。

註:目前(ver 1.0.2) P4 Spec 上並未針對 attribute_list 做出解釋,待補充。

register 的使用方式如下:

register_name[ const_value ]

或是

register_name[ const_value ].field_name

 

下回應該會針對 P4 software switch 所提供的 Switch API 以及 Switch SAI 去做分析

4 thoughts on “簡單介紹 P4 語言(五)- Counter, Meter & Register

  1. 一直不明白 Meter 應用的場景是什麼?
    我知道 count 可以用來統計,register 可以保存狀態信息。

    1. Spec 並沒有提到太多有關於 meter 相關的訊息,個人推測僅供計算流量或是速率

      1. meter 的输出结果只有红、黄、绿三种颜色。
        应该是用来对某些变量做监视。

        1. 對,只不過具體的作法,依據 bmv2 的實作(meters.cpp/h),他是計算 rate,然後給予 color

Share Your Thought