实验8:数据平面可编程实践——P4

实验8:数据平面可编程实践——P4

一、实验目的

  1. 掌握V1Model框架下P4_16的程序结构和基本语法
  2. 能够运用 P4 进行简单数据平面编程

二、实验环境

  1. 下载虚拟机软件Oracle VisualBox或VMware;
  2. 在虚拟机中安装Ubuntu 16.04 Desktop amd64,并安装完整Mininet和P4开发环境;
  3. 提供P4镜像P4-Suite2018.ova,提取码:egwf

三、实验要求

学习P4官方示例教程,链接:https://github.com/p4lang/tutorials,了解P4-16版本的基本语法、基于V1Model的P4代码结构,完成如下练习:

(一)基本要求

熟悉使用P4实现交换机IPv4的基本转发原理,编写P4程序,在下面的拓扑中实现IPV4 隧道转发。

(二)进阶要求

在熟悉隧道转发原理的前提下,编写实现P4Runtime的Python代码,将流规则下发到上图拓扑中的交换机,实现依据此规则的隧道转发。

首先是引入了一堆库,和需要用到的p4runtime_lib

#!/usr/bin/env python2
import argparse
import grpc
import os
import sys
from time import sleep

# Import P4Runtime lib from parent utils dir
# Probably there's a better way of doing this.
sys.path.append(
    os.path.join(os.path.dirname(os.path.abspath(__file__)),
                 '../../utils/'))
import p4runtime_lib.bmv2
from p4runtime_lib.error_utils import printGrpcError
from p4runtime_lib.switch import ShutdownAllSwitchConnections
import p4runtime_lib.helper

SWITCH_TO_HOST_PORT = 1
SWITCH_TO_SWITCH_PORT = 2        //指定了交换机端口号

之后是定义写隧道规则

def writeTunnelRules(p4info_helper, ingress_sw, egress_sw, tunnel_id, dst_eth_addr, dst_ip_addr):

第一个规则

# 1) Tunnel Ingress Rule
table_entry = p4info_helper.buildTableEntry(
        table_name="MyIngress.ipv4_lpm",
        match_fields={
            "hdr.ipv4.dstAddr": (dst_ip_addr, 32)
        },
        action_name="MyIngress.myTunnel_ingress",
        action_params={
            "dst_id": tunnel_id,
        })
    ingress_sw.WriteTableEntry(table_entry)
    print "Installed ingress tunnel rule on %s" % ingress_sw.name

readTableRules

def readTableRules(p4info_helper, sw):
    """
    Reads the table entries from all tables on the switch.

    :param p4info_helper: the P4Info helper
    :param sw: the switch connection
    """
    print '\n----- Reading tables rules for %s -----' % sw.name
    for response in sw.ReadTableEntries():
        for entity in response.entities:
            entry = entity.table_entry
            # TODO For extra credit, you can use the p4info_helper to translate
            #      the IDs in the entry to names
            print entry
            print '-----'

从交换机中读具体的索引对应的计数器。在我们的程序中,这个索引是隧道ID号。如果这个索引是0,就会从计数器中返回所有的值

def printCounter(p4info_helper, sw, counter_name, index):
    """
    Reads the specified counter at the specified index from the switch. In our
    program, the index is the tunnel ID. If the index is 0, it will return all
    values from the counter.

    :param p4info_helper: the P4Info helper
    :param sw:  the switch connection
    :param counter_name: the name of the counter from the P4 program
    :param index: the counter index (in our case, the tunnel ID)
    """
    for response in sw.ReadCounters(p4info_helper.get_counters_id(counter_name), index):
        for entity in response.entities:
            counter = entity.counter_entry
            print "%s %s %d: %d packets (%d bytes)" % (
                sw.name, counter_name, index,
                counter.data.packet_count, counter.data.byte_count
            )

 

 

 

 

 

 

 

pingall成功:

 

 

 

 

 

 

 

 

 实验总结:

在安装中clone grpc的子模块时卡住了,并没有按照.gitmodules上更改后的路径进行下载。发现停止shell脚本运行,手动clone相关模块。进入grpc/third_party中,逐个克隆子模块。此时要打开grpc 文件夹下的.gitmodules查看所有需要用到的模块。遇到错误和解决方法。在遇到error:* You must fix these errors before submitting* - "Review URL" is required.solve:p4 change NUM需要增加post-review URL。P4作为一种SDN中诞生不久的面向数据平面的编程语言进一步提高了SDN的可编程性与灵活性

 

 

posted @ 2021-11-01 21:30    阅读(137)  评论(0)    收藏  举报