hscraigchang
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # P4 lab ## Part 1 將目前使用者加到docker群組裡面,當docker service起來時,會以這個群組的成員來初始化相關服務 ``` sudo chmod 777 /var/run/docker.sock ``` 進入獨立的venv python環境 ``` python -m venv venv && . ./venv/bin/activate ``` 查詢ASCII ``` python -c "print(hex(ord('+')))" ``` ### Get P4 code Get the basic p4 codebase from ONOS. ![](https://i.imgur.com/pWEoWZS.jpg) ### Compile first P4 code ``` $ p4c-bm2-ss --arch v1model \ -o build/bmv2.json --p4runtime-files build/p4info.txt \ src/basic.p4 ``` 完成後會在build資料夾中生成以下檔案 ![](https://i.imgur.com/SmgPemb.jpg) 接著建立一python檔(topo.py),執行並建立基本網路拓樸。 ![](https://i.imgur.com/FJr8Brj.jpg) ### Network Topology Set up switches with p4runtime-shell, and then add flow rules to the switch through p4 program. 上圖可以看到所建立的拓樸有兩台switch,所以我們需要用p4 language去控制每一台switch。 ``` $ python -m p4runtime_sh --election-id 0,1 \ --grpc-addr localhost:50001 \ --config "build/p4info.txt,build/bmv2.json" ``` ![](https://i.imgur.com/DigQGoR.png) 接著輸入以下指令為讓封包進入這台switch由port 1進、port 2出。 ``` te = table_entry["ingress.table0_control.table0"]( action="ingress.table0_control.set_egress_port" ) te.priority = 1 te.match["standard_metadata.ingress_port"] = "2" te.action["port"] = "1" te.insert() te = table_entry["ingress.table0_control.table0"]( action="ingress.table0_control.set_egress_port" ) te.priority = 1 te.match["standard_metadata.ingress_port"] = "1" te.action["port"] = "2" te.insert() ``` ![](https://i.imgur.com/qTESNIN.jpg) 兩台switch設定完成後,回到mininet即可以讓h1與h2互通。 ![](https://i.imgur.com/xLxqJTl.jpg) ## Part 2 ### P4 architecture ![](https://i.imgur.com/EaxdpwJ.jpg) **Parser $\rightarrow$ Control Pipeline $\rightarrow$ Control Deparser** ### Parser & Deparser 開啟mininet ![](https://i.imgur.com/YerYqQn.jpg) 發送UDP封包 ``` $ docker exec -it p4mn_01 m h2 bash $ nc -u 10.0.1.1 3000 1 + 1 ``` 開啟wireshark (若遇到wireshark權限問題,待補) ![](https://i.imgur.com/opHYXo6.jpg) ![](https://i.imgur.com/o9iNkph.jpg) **實驗目標:** Parser:如果UDP port是3000,解析內容(兩個數字+一個運算符號)。 透過**header stack**來儲存內容。 **步驟** 編輯parsers.p4、headers.p4、custom_headers.p4 在parse_udp底下可以透過transition select來轉換到不同的state,在此預設的情況是不會往下送,當滿足以下條件則進行parse_math_udp $\cdot$ UDP data length > 0 $\cdot$ UDP port = 3000 ### Step 1 - 1: ![](https://i.imgur.com/H0SE7IC.jpg) 從維基百科上面的資料可以得知udp協定中的header為8個bytes,從剛剛wireshark的封包可以看到他的length為14也就是整體的長度,因此可以得知data length為6 bytes。所以我們要定義一個UDP data length,且他必須大於0。在header.p4中可以看到他定義的UDP整體長度為length_,所以我們在定義UDP data length為: **headers** ![](https://i.imgur.com/1bul7eE.jpg) 這個length_是指udp封包的整個length。 **custom headers** ![](https://i.imgur.com/Ig8ABnt.jpg) local_metadata主要是定義、儲存在parser.p4得出的結果 **parser** ![](https://i.imgur.com/L0Fqnln.jpg) ### Step 1 - 2: 接著需要寫buffer來儲存num1、space1、num2、operator、space2。要注意的是,它的結構是由struct包著header,再由header包著field才比較不會有bug。 **headers** ![](https://i.imgur.com/TQTSoEO.jpg) **custom headers** ![](https://i.imgur.com/FTdw5wV.jpg) ![](https://i.imgur.com/2HuxZBs.jpg) (打錯字,已更正) **parser** ![](https://i.imgur.com/iPxhdFR.jpg) 建立header stack來暫存數字及運算符號。需要注意的是定義的udp_buffer裡面的元素是char,-CHAR_0是為了將字元轉為數字。 ![](https://i.imgur.com/UcRuBPN.jpg) **define** ![](https://i.imgur.com/7htG15W.jpg) 定義字元0的ASCII為0X30。 ### Control Pipeline **實驗目標:** Table entry具有三個欄位:Key、Action、Action Data ![](https://i.imgur.com/zWCtKAU.jpg =70%x) 將前面的數字及運算符號進行運算,並把結果資料寫回UDP封包。 **步驟** ### Step 2 - 1: basic.p4 ![](https://i.imgur.com/7elHFZS.jpg) ![](https://i.imgur.com/EsMjAdX.jpg) ![](https://i.imgur.com/qJcpZcZ.jpg) define.p4 ![](https://i.imgur.com/4DZbBNI.jpg)

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully