P4 tutorials实验 - Qos, multicast

P4 tutorials实验 - Qos, multicast

QoS

背景知识

  • Differentiated Services,区分服务,是基于类的、实现QoS的一种技术,diffserv对网络流量进行分类和管理

实验部分

实验链接:https://github.com/p4lang/tutorials/tree/master/exercises/qos

TODO1:将tos字段划分为6bit的diffserv字段和2bit的ecn字段

TODO 2:为不同的流类型,实现不同的action

/* TODO: Implement actions for different traffic classes */
    action default_forwarding(){
    	hdr.ipv4.diffserv = 0;//default forwarding type
    }
    action tcp_forwarding(){
    	hdr.ipv4.diffserv = 44;//Expedited Forwarding type
    }
    action udp_forwarding(){
    	hdr.ipv4.diffserv = 46;//Voice Admit type
    }
//还可以有其他的action,详见tips

TODO 3:在协议的基础上,设置hdr.ipv4.diffserv

/* TODO: set hdr.ipv4.diffserv on the basis of protocol */
    apply {
        if (hdr.ipv4.isValid()) {
            if(hdr.ipv4.protocol == IP_PROTOCOLS_TCP){
            	tcp_forwarding();
            }
            else if (hdr.ipv4.protocol == IP_PROTOCOLS_UDP){
            	udp_forwarding();
            }
            ipv4_lpm.apply();
        }
    }

TODO 4:将tos替换成diffserv和ecn

hdr.ipv4.diffserv,
hdr.ipv4.ecn,

重新编译运行,在h2处可以看到IP数据包中的tos值发生变化

UDP:tos字段为0xb9

TCP:tos字段为0xb1

tips

关于solution中各种action及其diffserv字段取值的来源:区分服务的各种类型以及对应数值

multicast

背景知识

  • multicast,多播,将单个消息发送至所有的多播组成员
  • 在多播组中,如果出现了ingress端口,那么数据包会在该端口的egress pipeline中被丢弃

实验部分

TODO 1:定义multicast操作,以将数据包多播至多播组1

    // TODO: define `multicast` action to multicast packets to group 1
    // Hint: Check v1model for multicast group
    action multicast(){
    	standard_metadata.mcast_grp = 1;
    }

分析:查阅v1model.p4代码,在100行处,可查看到关于多播组id所使用的元数据:

    /// multicast group id (key for the mcast replication table)
    @alias("intrinsic_metadata.mcast_grp")
    bit<16> mcast_grp;

同时,查看/exercises/multicast/sig-topo下的s1-runtime.json文件,可以看到原始对多播组的设置,即多播组id为1,组内成员的出端口分别为1,2,3

  "multicast_group_entries" : [
    {
      "multicast_group_id" : 1,
      "replicas" : [
        {
          "egress_port" : 1,
          "instance" : 1
        },
        {
          "egress_port" : 2,
          "instance" : 1
        },
        {
          "egress_port" : 3,
          "instance" : 1
        }
      ]
    }
  ]

TODO 2:在可用操作中添加multicast操作,TODO 3:将默认的丢包操作替换为multicast操作

		actions = {
            // TODO: add `multicast` action to the list of available actions
            multicast;
            mac_forward;
            drop;
        }
        size = 1024;
        // TODO : replace default drop action by multicast
        default_action = multicast;

此时执行pingall操作可以看到h1, h2, h3之间能互相通信,但无法与h4通信

TODO 4:在sig-topo/s1-runtime.json文件中,将4端口添加至多播组中

        {
          "egress_port" : 4,
          "instance" : 1
        }//注意上方需要添加“,”符号

再次编译运行,可以看到h4也可以进行通信:

tips

查看v1model的standard_metadata:https://github.com/p4lang/p4c/blob/main/p4include/v1model.p4

further observation

default_action被设置为multicast之后,原本的mac_forward对主机通信不起作用。具体验证方法为,将multicast.p4文件中的mac_forward操作删除,同时将mac_lookup表中,action部分的mac_forward;也删除,最后将sig-topo/s1-runtime.json文件中的"table_entries"内容全部清空后,重新编译运行,可以发现四台主机之间的通信不受影响。

posted @ 2023-03-11 16:19  瑞图恩灵  阅读(402)  评论(0编辑  收藏  举报