---
# System prepended metadata

title: P4 08
tags: [P4]

---

#  P4 08
### 龍帆軒
## controller實作
從mycontroller.py裡的main開始看

![](https://i.imgur.com/ODxMYLR.png)    
>把switch1 跟 switch2 的data dump到給定的txt檔

![](https://i.imgur.com/kugBZ2u.png)    
> 傳送master arbitration來設定這個controller為master
> 可以去utils/p4runtime_lib/switch.py找定義
>![](https://i.imgur.com/jwb1SI7.png)    
> ``` request=p4runtime_pb2.StreamMessageRequest```    
> 在 p4lang/p4runtime/blob/master/proto/p4/v1/p4runtime.proto 可以看到定義
> ![](https://i.imgur.com/MOHVk5U.png)
> oneof 的用法是只有一個在update裡的資料會被改動
> ![](https://i.imgur.com/I9msfUi.png)
> 
> `request.arbitration.device_id=self.device.id`
> 設定目前要被arbitrate的client

![](https://i.imgur.com/YZI8CjK.png)    
> 這幾行是在安裝p4 program到每個switch上
> `SetForwardingPipelineConfig()`可以在switch.py找定義
> ![](https://i.imgur.com/07gAUZt.png)
> 去p4runtime.proto找 `SetFowardingPipelineConfigRequest()`
> ![](https://i.imgur.com/wAt0leK.png)
> 可以看到是在定義每個action代表的動作

![](https://i.imgur.com/9Y11eHb.png)    
> 把tunnel的traffic rules寫進去
> `writeTunnelRules()`可以在mycontroller.py上面一點找到
> `writeTunnelRules()`是在做3件事 
> 1.tunnel ingress rule : encapsulates traffic into a tunnel with the specified ID
> 2.tunnel transit rule : forwards traffic based on the specified ID
> 3.tunnel egress rule : decapsulates traffic with the specified ID and sends it to the host

![](https://i.imgur.com/NmbrMMt.png)    
> 從switch1 跟 switch2 讀取table entry
> ![](https://i.imgur.com/Wp0iwhv.png)
> 更詳細的在report 07

![](https://i.imgur.com/elDwZG1.png)    
> 每兩秒印出tunnel counters
> ![](https://i.imgur.com/QwClpij.png)
> `sw.ReadCounters()`可以在p4runtime_lib/switch.py裡找到
> ![](https://i.imgur.com/gcuxMlm.png)
> `ReadRequest()`在p4runtime.proto可以找到
>![](https://i.imgur.com/RjXGux4.png)
>Entity 可以從p4runtime.proto上方找到
>![](https://i.imgur.com/f6y2r7z.png)
>可以看到我們設定entities為2 就是要讀table entry
> `yield`是在迴圈裡遇到這行statement便會暫停跳出去 
p4runtime_pb2 找不到


###### tags: `P4`















