Tofino可编程交换机实现网络聚合的快速使用

一、网络拓扑

在这里插入图片描述

Tofino交换机内网IP及ssh port用户名密码备注
switch00172.16.50.241:22root
switch01172.16.50.242:22root
switch02172.16.50.243:22root
switch03172.16.50.244:22root
注:需通过外网访问computer26,然后访问交换机。
服务器内网IP及ssh port实验用IP用户名密码备注*1
sever01172.16.50.1:6001172.16.200.1sdna b d
sever02172.16.50.2:6002172.16.200.2sdna b d
sever08172.16.50.8:6008172.16.200.8sdna b f
sever11172.16.50.31:22172.16.200.31sdna b f
sever12172.16.50.32:22172.16.200.32sdna b
sever13172.16.50.33:22172.16.200.33sdna b python安装问题,无法ping baidu
sever14172.16.50.34:22sdna b
sever15172.16.50.35:22sdna b e

注1:a)实验python环境OK;b)网卡与交换机连接OK,可sendp;c)ARP表设置完全;d)代码是最新版本;e)ovs+odl OK;f)cuda OK
注2:部分交换机重启后没有设置反代理,需通过外网访问computer26,然后访问。

2、172网段连接

服务器内网IP及ssh port远程链接IP用户名密码备注
sever26172.16.150.226:22(默认端口)xxx.xxx.xxx.xxx:PORT_NUM# (隐藏)xxx
# Access the switch00 through external internet.
# ssh to computer26
ssh USR_NAME@xxx.xxx.xxx.xxx -p PORT_NUM # 隐藏
USR_NAME@xxx.xxx.xxx.xxx''s password:
# ssh to switch00
sdn@computer26:~$ ssh root@172.16.50.241 # 241~244
root@172.16.150.252's password:
root@localhost:~

3、172网段网卡连接情况

已测试的每个server共同部分的网卡(X为server号,1、2、8…)

网卡名ip连接情况
eno1172.16.50.X和其他server的eno1、Tofino控制口联通
eno2172.16.150.X仅和其他server的eno2联通
eno5172.16.160.X仅和其他server的eno5联通

4、故障排除

1)ECDSA host key “ip地址” for has changed and you have requested strict checking

SSH连接相同的ip地址时因有连接记录直接使用失效的协议信息去验证该ip服务器,所以报错,使用下面命令清除known_hosts里旧缓存文件即可

ssh-keygen -R "你的远程服务器ip地址"

二、可编程交换机

1、进入$sde目录

root@localhost:~$ cd bf-sde-9.3.1/
root@localhost:~/bf-sde-9.3.1#. ./set_sde.bash # 不要忘记前⾯的点,这是设置当
前⽬录为sde
Using bf-sde-9.3.1 in /root/bf-sde-9.3.1

2、P4程序编写

参考PUBLIC_Tofino-Native-Arch-Document.pdf文档,https://github.com/Fangjin98/distributed_PS_ML此代码只要数据包数据链路层(MAC)和网络层(IPv4)按要求即可,数据包后面内容不影响转发,不需要更高层协议。根据网络层dst_IP直接转发到交换机对应端口。

1)对寄存器的操作

在这里插入图片描述

3、P4程序编译

以对$sde/bc/ngaa文件夹内跑文件编译为例,在$sde目录下:

root@localhost:~/bf-sde-9.3.1# ./p4_build-9.0.0.sh -p bc/mapreduce/p4switch_bc_switch00/ngaa/ngaa.p4
Using SDE          /root/bf-sde-9.3.1
Using SDE_INSTALL /root/bf-sde-9.3.1/install
Using SDE version bf-sde-9.3.1

OS Name:  "Open Network Linux OS ONL-bf-9.1.0, 2020-02-11.16
This system has 8GB of RAM and 8 CPU(s)
Parallelization:  Recommended: -j4   Actual: -j4

Compiling for p4_16/tna
P4 compiler path:    /root/bf-sde-9.3.1/install/bin/bf-p4c
P4 compiler version: 9.3.1 (SHA: 18fd024) (p4c-based)
Build Dir: /root/bf-sde-9.3.1/build/p4-build/ngaa
 Logs Dir: /root/bf-sde-9.3.1/logs/p4-build/ngaa

  Building ngaa        CLEAR CONFIGURE MAKE INSTALL ... DONE

编译生成的二进制文件存储在$sde/build/p4-build/目录下

4、交换机的运行

需要在三个shell里的$sde目录下分别依次执行下列“1)、2)、3)”三步

1)运行P4程序

注意运行run_switchd.sh时直接用p4文件名即可(无需路径),会自动定位$sde/build/p4-build/目录下的ngaa文件

root@localhost:~/bf-sde-9.3.1# ./run_switchd.sh -p ngaa
Using SDE /root/bf-sde-9.3.1
Using SDE_INSTALL /root/bf-sde-9.3.1/install
Setting up DMA Memory Pool
Using TARGET_CONFIG_FILE /root/bf-sde-9.3.1/install/share/p4/targets/tofino/ngaa.conf
Using PATH /root/bf-sde-9.3.1/install/bin:/root/bf-sde-9.3.1/install/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/lib/platform-config/current/onl/bin:/lib/platform-config/current/onl/sbin:/lib/platform-config/current/onl/lib/bin:/lib/platform-config/current/onl/lib/sbin
Using LD_LIBRARY_PATH /usr/local/lib:/root/bf-sde-9.3.1/install/lib:
bf_sysfs_fname /sys/class/bf/bf0/device/dev_add
Install dir: /root/bf-sde-9.3.1/install (0x5619cbf96980)
bf_switchd: system services initialized
bf_switchd: loading conf_file /root/bf-sde-9.3.1/install/share/p4/targets/tofino/ngaa.conf...
bf_switchd: processing device configuration...
Configuration for dev_id 0
  Family        : tofino
  pci_sysfs_str : /sys/devices/pci0000:00/0000:00:03.0/0000:05:00.0
  pci_domain    : 0
  pci_bus       : 5
  pci_fn        : 0
  pci_dev       : 0
  pci_int_mode  : 1
  sbus_master_fw: /root/bf-sde-9.3.1/install/
  pcie_fw       : /root/bf-sde-9.3.1/install/
  serdes_fw     : /root/bf-sde-9.3.1/install/
  sds_fw_path   : /root/bf-sde-9.3.1/install/
  microp_fw_path: 
bf_switchd: processing P4 configuration...
P4 profile for dev_id 0
num P4 programs 1
  p4_name: ngaa
  p4_pipeline_name: pipe
    libpd: 
    libpdthrift: 
    context: /root/bf-sde-9.3.1/install/share/tofinopd/ngaa/pipe/context.json
    config: /root/bf-sde-9.3.1/install/share/tofinopd/ngaa/pipe/tofino.bin
  Pipes in scope [0 1 2 3 ]
  diag: 
  accton diag: 
  Agent[0]: /root/bf-sde-9.3.1/install/lib/libpltfm_mgr.so
  non_default_port_ppgs: 0
  SAI default initialize: 1 
bf_switchd: library /root/bf-sde-9.3.1/install/lib/libpltfm_mgr.so loaded
bf_switchd: agent[0] initialized
Tcl server started..
Tcl server: listen socket created
Tcl server: bind done on port 8008, listening...
Tcl server: waiting for incoming connections...
Health monitor started 
Operational mode set to ASIC
Initialized the device types using platforms infra API
ASIC detected at PCI /sys/class/bf/bf0/device
ASIC pci device id is 16
Starting PD-API RPC server on port 9090
bf_switchd: drivers initialized
detecting.. IOMMU not enabled on the platform
Setting core_pll_ctrl0=cd44cbfe
-
bf_switchd: dev_id 0 initialized

bf_switchd: initialized 1 devices
Adding Thrift service for bf-platforms to server
bf_switchd: thrift initialized for agent : 0
bf_switchd: spawning cli server thread
bf_switchd: spawning driver shell
bf_switchd: server started - listening on port 9999
bfruntime gRPC server started on 0.0.0.0:50052

        ********************************************
        *      WARNING: Authorised Access Only     *
        ********************************************
    
bfshell> 

2)设置可编程交换机端口

Tofino交换机端口

可编程交换机共有32个端口,每个端口可以以10Gbps/25Gbps/40Gbps/100Gbps的速率运行,我们需要以特定速率启用特定端口,才能使交换机接收到来自主机的包。

可以在bfshell下进入ucli再进入port manager来逐步设置端口

bfshell> ucli
Starting UCLI from bf-shell
Cannot read termcap database;
using dumb terminal settings.
bf-sde> pm
bf-sde.pm>

也可以使用port.bfsh文件批量设置端口

root@localhost:~/bf-sde-9.3.1# ./run_bfshell.sh -f bc/mapreduce/p4switch_bc_switch00/ngaa/port.bfsh
Using SDE /root/bf-sde-9.3.1
Using SDE_INSTALL /root/bf-sde-9.3.1/install
Connecting to localhost port 7777 to check status on these devices: [0]
Waiting for device 0 to be ready
/root/bf-sde-9.3.1/install/bin/bfshell bc/ngaa/port.bfsh
ucli
pm
port-add 1/- 10G NONE
an-set 1/- 2
port-enb 1/-
port-add 32/0 100G NONE
port-enb 32/0
show


        ********************************************
        *      WARNING: Authorised Access Only     *
        ********************************************
    
bfshell> ucli
Cannot read termcap database;
using dumb terminal settings.
bf-sde.pm> pm
error: unknown command 'pm'
bf-sde.pm> port-add 1/- 10G NONE
bf-sde.pm> an-set 1/- 2
bf-sde.pm> port-enb 1/-
bf-sde.pm> port-add 32/0 100G NONE
bf-sde.pm> port-enb 32/0
bf-sde.pm> show
-----+----+---+----+-------+----+--+--+---+---+---+--------+----------------+----------------+-
PORT |MAC |D_P|P/PT|SPEED  |FEC |AN|KR|RDY|ADM|OPR|LPBK    |FRAMES RX       |FRAMES TX       |E
-----+----+---+----+-------+----+--+--+---+---+---+--------+----------------+----------------+-
1/0  |23/0|132|3/ 4|100G   |NONE|Ds|Au|YES|ENB|DWN|  NONE  |               0|               0| 
2/0  |22/0|140|3/12|100G   |NONE|En|Au|YES|ENB|DWN|  NONE  |               0|               0| 
32/0 |25/0|136|3/ 8|100G   |NONE|Au|Au|YES|ENB|DWN|  NONE  |               0|               0| 
bf-sde.pm>

其中port.bfsh文件内容及说明如下

ucli
pm
port-add 1/- 10G NONE
an-set 1/- 2
port-enb 1/-
port-add 32/0 100G NONE
port-enb 32/0
show

添加端口:port-add <conn_id/chnl> <speed (1G, 10G, 25G, 40G, 40G_NB, 50G, 100G, 40G_NON_BREAKABLE)> <fec (NONE, FC, RS)>
删除端口:port-del <conn_id/chnl>
启用端口:port-enb <conn_id/chnl>
禁用端口:port-dis <conn_id/chnl>
显示端口信息:show -a -p <conn_id/chnl> [-d]
端口自动协商:an-set <conn_id/chnl_id> <AN_setting>

AN_setting 是以下之一:
0 接受SDE 确定的默认值;
1 强制启用 AN;
2 强制禁用 AN。

** 通配符“-”**代表全部端口或通道,例如bf-sde.bf_pltfm.pm> port-enb 1/- 表示启动端口1的全部通道

3)写入P4程序表项

在P4程序中我们定义了一些table,这些table需要接收key来执行对应的action。我们需要对照自己的P4程序编写相应的setup.py文件并在新的终端运行。

root@localhost:~/bf-sde-9.3.1# ./run_bfshell.sh -b bc/mapreduce/p4switch_bc_switch00/ngaa/setup.py

在这里插入图片描述
其中setup.py代码与p4文件的table相对应,具体如下:
setup文件说明
该左侧代码add_with_ipv4_forward实现功能:交换机收到一个包,解析出目的ip地址,若为ip01,发往端口port01
其中左侧代码port01、port02…为可编程交换机的物理端口,该型号交换机端口可通过bf-sde.pm> show查看,如下图(由于设置为100G带宽,33个端口都仅有通道0)
在这里插入图片描述

至此P4程序就算成功运行了,接下来的任务就是主机端向对应的网口发定义好的包,然后交换机会根据P4程序对包进⾏解析处理转发等⼯作。

5、故障排除

1)提示IP冲突

由于已经运行一个p4进程导致的冲突,kill进程重启bf_switchd即可

ps -ax | grep switch #查找到正在运行p4进程的pid
sudo kill xxx # xxx为进程号

2)提示“Device mmap failed for dev_id 0”

root@Switch:~$ cd /root/bf-sde-*
root@Switch:~$ . ./set_sde.bash
root@Switch:~$ ./install/bin/bf_kdrv_mod_load $SDE_INSTALL

6、其他

1)scp传输

在跳板机computer26,将当前目录下的ngaa文件夹复制到172.16.50.241交换机的bc
scp -r ./ngaa root@172.16.50.241:/root/bf-sde-9.3.1/bc
在跳板机computer26,将当前目录下的ngaa文件夹复制到172.16.50.1服务器的bc
scp -r -P 6001 ./ngaa sdn@172.16.50.1:/home/sdn/bc

2)ssh port

vim /etc/ssh/sshd_config

若“#port 22”则为默认端口22,ssh链接时无需“-p”明确端口,若无井号注释则需

ssh sdn@172.xx.xx.xx -p 60xx # ssh链接
scp -r -P 60xx ./localfilename root@172.xx.xx.xx:/root/remotefiles #scp传输

三、非可编程交换机端(使用主机OvS模拟)

可ssh登录交换机使用CLI控制转发(尚未研究)
目前使用主机Open vSwitch构建虚拟非可编程交换机,以根据mac地址转发转发数据包
参考资料:https://www.cnblogs.com/goldsunshine/p/10331606.html
https://github.com/openvswitch/ovs

1、环境配置

1)安装OVS

server好多已经有ovs环境了,暂略此部分

2)安装opendaylight控制器

下载OpenDaylight源文件“distribution-karaf-0.6.4-Carbon.tar.gz”(目前下载过慢,使用scp)

wget https://nexus.opendaylight.org/content/groups/public/org/opendaylight/integration/distribution-karaf/0.6.4-Carbon/distribution-karaf-0.6.4-Carbon.tar.gz

解压并进入文件

tar zvxf distribution-karaf-0.6.4-Carbon.tar.gz
cd distribution-karaf-0.6.4-Carbon

安装Java

apt-get install openjdk-8-jdk # 安装Java

vim /etc/environment 进入环境变量配置文件,在第二行加入java的环境变量(具体路径查看/usr/lib/jvm目录下的文件名称)

JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"

vim bin/setenv,在最后一行增加(具体路径查看/usr/lib/jvm目录下的文件名称)

JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"

vim etc/org.apache.karaf.management.cfg
简单配置ODL的运行文件

在这里插入图片描述
开启ODL控制器

./bin/karaf

在这里插入图片描述
安装必要的插件

opendaylight-user@root>feature:install odl-restconf
opendaylight-user@root>feature:install odl-l2switch-switch-ui
opendaylight-user@root>feature:install odl-openflowplugin-flow-services-ui
opendaylight-user@root>feature:install odl-mdsal-apidocs
opendaylight-user@root>feature:install odl-dluxapps-applications
opendaylight-user@root>feature:install odl-faas-all

查看端口验证ODL是否启动成功

sudo netstat -anput | grep 6633

当ODL启动成功之后会监听在6633端口,监听交换机的连接。新建一个窗口,如果6633端口有监听,那就说明ODL启动是正常的
在这里插入图片描述

2、设置OVS

注意:添加网桥端口(绑定物理网卡)后该网卡将不能正常使用,勿使用ssh连接的网卡

ovs-vsctl show # 查看网桥(虚拟交换机)
ovs-vsctl add-br  br-test # 添加网桥 br-test
ovs-vsctl add-port br-test netward_name # 添加网桥端口(绑定物理网卡)
ovs-vsctl del-port br-test enp0s3  # 删除网桥端口
ovs-vsctl del-br br-test # 删除网桥
ovs-vsctl set-controller br-test tcp:127.0.0.1:6633 # 网桥链接控制器(需先./karaf开启ODL控制器

ovs-ofctl dump-flows br-test # 查看网桥的流表
ovs-ofctl show br-test # 查看端口
ovs-ofctl add-flow br-test in_port=1,actions=output:2 # 手动下发流表,端口1进全部转发到2(上一步可看端口1、2对应哪个物理网卡)

四、主机端

1、环境配置

1)安装网卡驱动

· https://blog.csdn.net/qq_37960243/article/details/123066359 ,需要python2
· 接下来,目前网卡型号为 NVIDIA Mellanox ConnectX-6,默认使用Infiniband模式,需要转换为Ethernet模式https://blog.csdn.net/qq_44777969/article/details/108033334
· 接下来配置ip并重启端口

sudo vim /etc/network/interfaces # 新增网卡ens3f0、ens3f1配置
# 方案1
sudo ifdown ens3f0
sudo ifdown ens3f1
sudo ifup ens3f0
sudo ifup ens3f1
# 方案2
service networking restart # 但会报错“Job for network.service failed because the control process exited with error code”
journalctl -xe # 查看日志(若是eno5网卡报错,忽略即可)

2)检验网卡与交换机连接

先在可编程交换机运行代码(3步,端口需要打开与该网卡连接的端口,速度设置100G),可以看到OPR列已经UP了,说明连接OK,严谨起见,继续下面步骤
在这里插入图片描述
接着在网卡所在主机执行ibstat,达到下图State: Active且Physical state: LinkUp即可
在这里插入图片描述
最后在网卡所在主机ping任意ip(或者发任意Ethernet包)

ping -I ens3f0 172.16.200.8

此时在交换机端口面板使用show命令刷新,对应端口的“FRAMES RX”列不为0即可,如下图
在这里插入图片描述
此时测试通过
注意:必须启动可编程交换机(三个窗口),另外如果想通过ping测试两台主机连接,必须两台主机全部在自己ARP表中增加对方的IP-MAC对应

3)卸载已有python环境

https://blog.csdn.net/weixin_39592789/article/details/116732213

4)安装python 3.7.8

https://zhuanlan.zhihu.com/p/47868341
软连接时若提示failed to create symbolic link ‘/usr/bin/python’: File exists,在/usr/bin/下删除python文件
同时需要链接到/usr/bin/python3,使用时用python3 xxx.py即可

5)按需求安装依赖

首先安装CUDA11.3(含显卡驱动)
https://blog.csdn.net/Edwardshenbc/article/details/125047459?spm=1001.2014.3001.5502
其次执行下面步骤

sudo apt install libjpeg-dev zlib1g-dev libssl-dev libffi-dev python-dev build-essential libxml2-dev libxslt1-dev
pip3 install pulp numpy tensorboard

其次安装pytorch相关
https://blog.csdn.net/Edwardshenbc/article/details/125047459?spm=1001.2014.3001.5502

若报错见下面5的解决方案

6)安装paramiko

若报错是因为python3不支持,使用命令备份(或删除)lsb_release即可
在这里插入图片描述

pip3 install paramiko -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
mv /usr/bin/lsb_release /usr/bin/lsb_release.bak # 备份lsb_release

7)解决 ModuleNotFoundError: No module named ‘_bz2’

git clone https://github.com/shenbc/tempfiles.git
cd tempfiles
mv _bz2.cpython-37m-x86_64-linux-gnu.so /service/python3/lib/python3.7/site-packages
# 若仍不能解决问题,继续以下步骤
cd /service/python3/lib/python3.7/site-packages
chmod +x _bz2.cpython-37m-x86_64-linux-gnu.so

8)解决 ModuleNotFoundError: No module named ‘_lzma’

apt-get install liblzma-dev -y
pip install backports.lzma # 失败则 pip3 install backports.lzma

git clone https://github.com/shenbc/tempfiles
mv /service/python3/lib/python3.7/lzma.py /service/python3/lib/python3.7/lzma.py.bak # 备份
cd tempfiles
mv lzma.py /service/python3/lib/python3.7/lzma.py

9)解决 ModuleNotFoundError: No module named ‘scapy’

sudo pip3 install scapy

10)解决下载pytorch模型时 urllib.error.URLError: <urlopen error [Errno -2] Name or service not known>

在/etc/resolv.conf中添加 nameserver 8.8.8.8

2、运行

1)配置arp表

由于200网段的主机之间使用可编程交换机Tofino相连接,而Tofino尚未实现ARP协议。主机在执行socket.socket.sendto函数发送UDP数据时(utils.DataManager.py使用此函数),会先查找本机APR表,然而因为没有172.16.200.X的IP对应的MAC,会发送3条ARP包至交换机。这也是为什么如果不执行此步骤直接运行会出现Tofino端口FRAMES RX仅增加3,RX无变化的情况。
解决办法:
在主机手动增加ARP表项

arp # 查看ARP表
arp -i eth0 -s 172.16.200.2 08:c0:eb:28:9c:60 # 在eth0网卡的arp表上,添加IP到MAC的映射

2)在server上运行

sudo su
cd /home/sdn/bc/mapreduce
python server.py

3、主机端故障排除

1)bash: data/log/client_0_log.txt: Permission denied

Execute cmd.
 cd /home/sdn/bc/mapreduce; sudo /service/python3/bin/python3 -u client.py  --master_ip 127.0.0.1 --master_port 53300 --master_nic_ip 127.0.0.1 --client_ip 172.16.50.8 --client_nic_ip 172.16.50.8 --idx 0 --dataset CIFAR10 --model resnet50 --epoch 100 --batch_size 128 --ratio 1.0 --lr 0.05 --decay_rate 0.98 --algorithm proposed --step_size 1.0 --write_to_file False > data/log/client_0_log.txt 2>&1
[172.16.50.8] OUT:
***密码***
bash: data/log/client_0_log.txt: Permission denied

['PASSWORD\r\nbash: data/log/client_0_log.txt: Permission denied\r\n']
Files already downloaded and verified
Files already downloaded and verified
Recv buff: 425984
Get data from nic 127.0.0.1...
Try to connect socket and send init config.

权限问题导致client无法运行client.py,改权限即可

chmod -R 777 mapreduce

2)ibstat失败(仍未解决)

错误提示如下所示
在这里插入图片描述
原因在于网卡在ETH模式时重新安装了网卡驱动,导致失败
将网卡模式从EHT改为IB再改为ETH即可

4、其他

1)查看进程

ps aux # 查看进程
ps aux|grep python # 过滤出python进程
# a:显示当前终端下的所有进程信息,包括其他用户的进程。
# u:使用以用户为主的格式输出进程信息。
# x:显示当前用户在所有终端下的进程。

返回结果表头:
USER: 启动该进程的用户账号名称
PID: 该进程的ID号,在当前系统中是唯一的
%CPU: CPU占用的百分比
%MEM: 内存占用的百分比
VSZ: 占用虚拟内存(swap空间)的大小
RSS: 占用常驻内存(物理内存)的大小
TTY: 该进程在哪个终端上运行。“?”表未知或不需要终端
STAT: 显示了进程当前的状态,如S(休眠)、R(运行)、Z(僵死)、<(高优先级)、N(低优先级)、s(父进程)、+(前台进程)。对处于僵死状态的进程应予以手动终止。
START: 启动该进程的时间
TIME: 该进程占用CPU时间
COMMAND: 启动该进程的命令的名称

2)查看端口占用

https://www.runoob.com/w3cnote/linux-check-port-usage.html

五、参考资料

1、https://github.com/Fangjin98/distributed_PS_ML
2、https://gitlab.tongyuejun.cn/zhangjx/p4_doc/-/blob/main/Wedge100BF_User_Manual.org

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值