p4/tutorials/utils/run_exercise.py源码解析

该博客详细介绍了如何利用Makefile来管理和执行P4编程任务,包括编译P4源代码,生成P4Runtime文件,以及通过Python脚本运行BMv2软件交换机。Makefile中包含了对警告的处理,以及运行时的网络配置和日志记录。此外,还解析了Mininet网络拓扑,用于创建和配置虚拟网络环境。
摘要由CSDN通过智能技术生成

makefile

wildcard函数 $(wildcard *.c) : 获取工作目录下的所有.c文件列表

makefile符号表示
https://blog.csdn.net/dlf1769/article/details/78997967

  • makefile 代码解析

在这里插入图片描述

ifndef的作用是 如果没有传值,则中间内容是定义的默认值 ifdef ;
ifdef 如果变量;的值非空,那到表达式为真。否则,表达式为假。当然,;同样可以是一个函数的返回值。注意,ifdef只是测试一个变量是否有值,其并不会把变量扩展到当前位置。

#新建三个文件夹 分别为
mkdir -p build pcaps logs 
#对应makefile中的 %.json:%.p4下的语句
p4c-bm2-ss --p4v 16 --p4runtime-files build/basic.p4.p4info.txt -o build/basic.json basic.p4
basic.p4(81): [--Wwarn=unused] warning: Unused action parameter dstAddr
    action ipv4_forward(macAddr_t dstAddr, egressSpec_t port) {
   
                                  ^^^^^^^
basic.p4(81): [--Wwarn=unused] warning: Unused action parameter port
    action ipv4_forward(macAddr_t dstAddr, egressSpec_t port) {
   
# 运行 build的下方语句                                                        ^^^^
sudo python ../../utils/run_exercise.py -t pod-topo/topology.json -j build/basic.json -b simple_switch_grpc
mkdir -p build pcaps logs

p4c-bm2-ss --p4v 16 --p4runtime-files build/basic.p4.p4info.txt -o build/basic.json basic.p4

p4c-bm2-ss --p4v 16 --p4runtime-files build/firewall.p4.p4info.txt -o build/firewall.json firewall.p4
firewall.p4(124): [--Wwarn=unused] warning: bloom_filter_1: unused instance
    register<bit<1>>(4096) bloom_filter_1;
                           ^^^^^^^^^^^^^^
firewall.p4(125): [--Wwarn=unused] warning: bloom_filter_2: unused instance
    register<bit<1>>(4096) bloom_filter_2;
                           ^^^^^^^^^^^^^^
sudo python ../../utils/run_exercise.py -t pod-topo/topology.json -j build/basic.json -b simple_switch_grpc
#!/usr/bin/env python2
# Copyright 2013-present Barefoot Networks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Adapted by Robert MacDavid (macdavid@cs.princeton.edu) from scripts found in
# the p4app repository (https://github.com/p4lang/p4app)
#
# We encourage you to dissect this script to better understand the BMv2/Mininet
# environment used by the P4 tutorial.
#
import os, sys, json, subprocess, re, argparse
from time import sleep

from p4_mininet import P4Switch, P4Host

from mininet.net import Mininet
from mininet.topo import Topo
from mininet.link import TCLink
from mininet.cli import CLI

from p4runtime_switch import P4RuntimeSwitch
import p4runtime_lib.simple_controller

def configureP4Switch(**switch_args):
    """ Helper class that is called by mininet to initialize
        the virtual P4 switches. The purpose is to ensure each
        switch's thrift server is using a unique port.
    """
    if "sw_path" in switch_args and 'grpc' in switch_args['sw_path']:
        # If grpc appears in the BMv2 switch target, we assume will start P4Runtime
        class ConfiguredP4RuntimeSwitch(P4RuntimeSwitch):
            def __init__(self, *opts, **kwargs):
                kwargs.update(switch_args)
                P4RuntimeSwitch.__init__(self, *opts, **kwargs)

            def describe(self):
                print "%s -> gRPC port: %d" % (self.name, self.grpc_port)

        return ConfiguredP4RuntimeSwitch
    else:
        class ConfiguredP4Switch(P4Switch):
            next_thrift_port = 9090
            def __init__(self, *opts, **kwargs):
                global next_thrift_port
                kwargs.update(switch_args)
                kwargs['thrift_port'] = ConfiguredP4Switch.next_thrift_port
                ConfiguredP4Switch.next_thrift_port += 1
                P4Switch.__init__(self, *opts, **kwargs)

            def describe(self):
                print "%s -> Thrift port: %d" % (self.name, self.thrift_port)

        return ConfiguredP4Switch


class ExerciseTopo(Topo): # 该类在create_network()中用到
    """ The mininet topology class for the P4 tutorial exercises.
    """
    def __init__(self, hosts, switches, links, log_dir, bmv2_exe, pcap_dir, **opts):
        Topo.__init__(self, **opts)
        host_links = []
        switch_links = [
好的,以下是网页的中文翻译: # 序列模型和长短时记忆网络 ## 序列数据和循环神经网络 自然语言处理中的大多数任务都涉及到对序列数据的处理。序列数据是指按照一定顺序排列的数据,比如一句话中的单词、一篇文章中的句子等。 传统的深度学习模型很难处理序列数据,因为它们无法考虑到序列中前后单元之间的关系。为了解决这个问题,循环神经网络 (Recurrent Neural Networks, RNNs) 被提出。 循环神经网络是一种具有记忆功能的神经网络,它可以处理序列数据,同时考虑到了序列中前后单元之间的关系。RNNs 在自然语言处理中得到了广泛应用,比如文本分类、语音识别、机器翻译等。 ## 长短时记忆网络 尽管 RNNs 在处理序列数据方面表现很好,但是它们存在消失梯度 (Vanishing Gradient) 问题,当序列长度较长时,这个问题会变得尤为严重。长短时记忆网络 (Long Short-Term Memory, LSTM) 是一种能够缓解消失梯度问题的循环神经网络。 LSTM 通过引入门 (gate) 机制,控制了信息的流动,从而保留了序列中的长期依赖关系。LSTM 在自然语言处理中的表现非常优秀,比如情感分析、命名实体识别等任务。 ## PyTorch 中的 LSTM 在 PyTorch 中,我们可以使用 torch.nn.LSTM 类来构建 LSTM 模型。LSTM 类的参数说明如下: ```python torch.nn.LSTM(input_size, hidden_size, num_layers, bias, batch_first, dropout, bidirectional) ``` 其中, - `input_size`:输入数据的特征维度。 - `hidden_size`:隐状态的特征维度。 - `num_layers`:循环神经网络的层数。 - `bias`:是否使用偏置项。 - `batch_first`:是否将 batch 放在第一维。 - `dropout`:是否进行 dropout。 - `bidirectional`:是否使用双向 LSTM。 下面是一个使用 LSTM 进行文本分类的示例:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值