Python脚本创建mininet网络拓扑与iperf测量

2024-01-13 14:08

本文主要是介绍Python脚本创建mininet网络拓扑与iperf测量,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

1.新建Python文件example.py,并保存在mininet/custom目录下

1.1 __init__()

1.2 addController

1.3 addSwitch

1.4 addHost设置主机CPU

1.6 ping互通测试

1.6 iperf测试TCP吞吐量、UDP丢包和延迟抖动[2]

1.7 buildFromTopo() 

2.修改文件example.py为可执行文件和可读写文件

3.运行脚本 

参考文献


1.新建Python文件example.py,并保存在mininet/custom目录下

""" this file is a custom topology using Python.
reference link:
https://blog.csdn.net/weixin_39616367/article/details/111450445
"""from mininet.net import Mininet
from mininet.node import CPULimitedHost
from mininet.link import TCLink
from mininet.cli import CLInet = Mininet(host=CPULimitedHost, link=TCLink)#def addController( self, name='c0', controller=None, **params ):
c0 = net.addController()# def addSwitch( self, name, cls=None, **params ):
s1 = net.addSwitch('s1')#def addHost( self, name, cls=None, **params ):
h1 = net.addHost('h1')
h2 = net.addHost('h2', cpu=0.5)
h3 = net.addHost('h3', cpu=0.5)#def addLink( self, node1, node2, port1=None, port2=None, cls=None, **params ):
net.addLink(s1, h1, bw=10, delay='5ms', max_queue_size=1000, loss=10, use_htb=True)
net.addLink(s1, h2)
net.addLink(s1, h3)#def ping( self, hosts=None, timeout=None ):
net.pingAll()net.start()
CLI(net)    #Waiting for command

其中,mininet.net中的Mininet类定义了一个网络的类,包括拓扑、交换机、主机、控制器、链路、接口等,具体定义如下:

1.1 __init__()

def __init__( self, topo=None, switch=OVSKernelSwitch, host=Host,controller=DefaultController, link=Link, intf=Intf,build=True, xterms=False, cleanup=False, ipBase='10.0.0.0/8',inNamespace=False,autoSetMacs=False, autoStaticArp=False, autoPinCpus=False,listenPort=None, waitConnected=False ):"""Create Mininet object.topo: Topo (topology) object or Noneswitch: default Switch classhost: default Host class/constructorcontroller: default Controller class/constructorlink: default Link class/constructorintf: default Intf class/constructoripBase: base IP address for hosts,build: build now from topo?xterms: if build now, spawn xterms?cleanup: if build now, cleanup before creating?inNamespace: spawn switches and controller in net namespaces?autoSetMacs: set MAC addrs automatically like IP addresses?autoStaticArp: set all-pairs static MAC addrs?autoPinCpus: pin hosts to (real) cores (requires CPULimitedHost)?listenPort: base listening port to open; will be incremented foreach additional switch in the net if inNamespace=FalsewaitConnected: wait for switches to Connect?(False; True/None=wait indefinitely; time(s)=timed wait)"""

1.2 addController

def addController( self, name='c0', controller=None, **params ):"""Add controller.controller: Controller class"""

1.3 addSwitch

def addSwitch( self, name, cls=None, **params ):"""Add switch.name: name of switch to addcls: custom switch class/constructor (optional)returns: added switchside effect: increments listenPort ivar ."""

1.4 addHost设置主机CPU

def addHost( self, name, cls=None, **params ):"""Add host.name: name of host to addcls: custom host class/constructor (optional)params: parameters for hostreturns: added host"""
如:h1 = net.addHost('h1', cpu=0.5)
name需要加入节点的名字
clscustom host class,可选参数
params可选参数,具体见mininet/mininet/node.py中的config()
cpu:desired overall system CPU fraction

cores: (real) core(s) this host can run on

  mininet/mininet/node.py中的config()

def config( self, cpu=-1, cores=None, **params ):"""cpu: desired overall system CPU fractioncores: (real) core(s) this host can run onparams: parameters for Node.config()"""
def addLink( self, node1, node2, port1=None, port2=None, cls=None, **params ):""""Add a link from node1 to node2node1: source node (or name)node2: dest node (or name)port1: source port (optional)port2: dest port (optional)cls: link class (optional)params: additional link params (optional)returns: link object"""

如:self.addLink( node1, node2, bw=10, delay='5ms', max_queue_size=1000, loss=10, use_htb=True)

使用htb速率限制器和netem延迟/丢失模拟器,添加具有带宽、延迟和丢失特性,最大排队长度为1000的双向链路。

node1,node2节点1,2
port1source port源端口,可选参数
port2destination port目的端口,可选参数
clscustom link class,可选参数
params可选参数,具体见mininet/mininet/link.pyconfig()
bw: bandwidth in b/s (e.g. '10M')单位是Mbit
delay: transmit delay (e.g. '5ms', '100us', '1s')
jitter: jitter (e.g. '1ms')
loss: loss (e.g. '1%' )百分比

内核队列调度算法:

use_tbf: use TBF scheduling

use_hfsc: use HFSC scheduling,HFSC:Hierarchical Fair Service Curve’s(层次公平服务曲线) 

use_htb: use HTB scheduling, HTB:Hierarchical Token Bucket(层级令牌桶)

max_queue_size: queue limit parameter for netem以数据包表示

  mininet/mininet/link.py的config()

def config( self, bw=None, delay=None, jitter=None, loss=None,gro=False, txo=True, rxo=True,speedup=0, use_hfsc=False, use_tbf=False,latency_ms=None, enable_ecn=False, enable_red=False,max_queue_size=None, **params ):"""Configure the port and set its properties.bw: bandwidth in b/s (e.g. '10m')delay: transmit delay (e.g. '1ms' )jitter: jitter (e.g. '1ms')loss: loss (e.g. '1%' )gro: enable GRO (False)txo: enable transmit checksum offload (True)rxo: enable receive checksum offload (True)speedup: experimental switch-side bw optionuse_hfsc: use HFSC schedulinguse_tbf: use TBF schedulinglatency_ms: TBF latency parameterenable_ecn: enable ECN (False)enable_red: enable RED (False)max_queue_size: queue limit parameter for netem"""

1.6 ping互通测试

def ping( self, hosts=None, timeout=None ):"""Ping between all specified hosts.hosts: list of hoststimeout: time to wait for a response, as stringreturns: ploss packet loss percentage"""

1.6 iperf测试TCP吞吐量、UDP丢包和延迟抖动[2]

def iperf( self, hosts=None, l4Type='TCP', udpBw='10M', fmt=None,seconds=5, port=5001):"""Run iperf between two hosts.hosts: list of hosts; if None, uses first and last hostsl4Type: string, one of [ TCP, UDP ]udpBw: bandwidth target for UDP testfmt: iperf format argument if anyseconds: iperf time to transmitport: iperf portreturns: two-element array of [ server, client ] speedsnote: send() is buffered, so client rate can be much higher thanthe actual transmission rate; on an unloaded system, serverrate should be much closer to the actual receive rate"""

iperf是一款常用的网络带宽测试工具,分为客户端和服务端。服务端首先开启监听,客户端发送流量,最后测得带宽的大小。以h1为服务端,以h2为客户端。打开服务端和客户端终端命令为:

xterm h1 h2

1、Iperf测试TCP网络带宽利用率  

h1服务端iperf -s 
h2客户端

Iperf默认的运行时间在10秒左右,通过“-t”和“-i”参数来改变Iperf运行的时间和输出频率。如:iperf -c 10.0.0.1 -t 60 -i 10

通过“-n”参数指定要传输的数据量,指定“-n”参数后,“-t”参数失效,Iperf在传输完毕指定大小的数据包后,自动结束。如:iperf -c 10.0.0.1 -n 100M -i 10
通过“-P”参数测试多线程TCP吞吐量。如:iperf -c 10.0.0.1 -n 100M -i 10 -P 2

2、Iperf测试UDP数据包丢包率和延迟指标

h1服务端通过“-p”指定服务器端与客户端所连接的端口,iperf -s -u -p 6633 -t 60 -i 10
h2客户端通过Iperf的“-u”参数即可测试UDP应用的传输性能,“-b”参数测试客户端传输100M的UDP数据包,如:iperf -c 10.0.0.1 -u -p 6633 -t 60 -i 10 -b 100M

“Jitter”列表示抖动时间,也称为传输延迟。

“Lost/Total Datagrams”列表示丢失的数据包和数据包数量,0%是平均丢包率。

1.7 buildFromTopo() 

Mininet类最主要的部分是 build() 函数,依次执行:根据拓扑创建网络,配置网络名字空间,配置主机的 IP、MAC 等信息,检查是否启动 xterm,是否配置自动静态 arp 等。

def buildFromTopo( self, topo=None ):"""Build mininet from a topology objectAt the end of this function, everything should be connectedand up."""

2.修改文件example.py为可执行文件和可读写文件

chmod +x example.py
chmod 666 example.py

3.运行脚本 

sudo python3 example.py

参考文献

1.mininet搭建自定义网络拓扑.http://www.muzixing.com/pages/2013/12/06/yuan-chuang-mininetda-jian-zi-ding-yi-wang-luo-tuo-bu-by-muzi.html.

2.性能测试工具Iperf 验证SDN网络.https://www.sdnlab.com/15088.html.

这篇关于Python脚本创建mininet网络拓扑与iperf测量的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/601727

相关文章

Python的Darts库实现时间序列预测

《Python的Darts库实现时间序列预测》Darts一个集统计、机器学习与深度学习模型于一体的Python时间序列预测库,本文主要介绍了Python的Darts库实现时间序列预测,感兴趣的可以了解... 目录目录一、什么是 Darts?二、安装与基本配置安装 Darts导入基础模块三、时间序列数据结构与

Python正则表达式匹配和替换的操作指南

《Python正则表达式匹配和替换的操作指南》正则表达式是处理文本的强大工具,Python通过re模块提供了完整的正则表达式功能,本文将通过代码示例详细介绍Python中的正则匹配和替换操作,需要的朋... 目录基础语法导入re模块基本元字符常用匹配方法1. re.match() - 从字符串开头匹配2.

Python使用FastAPI实现大文件分片上传与断点续传功能

《Python使用FastAPI实现大文件分片上传与断点续传功能》大文件直传常遇到超时、网络抖动失败、失败后只能重传的问题,分片上传+断点续传可以把大文件拆成若干小块逐个上传,并在中断后从已完成分片继... 目录一、接口设计二、服务端实现(FastAPI)2.1 运行环境2.2 目录结构建议2.3 serv

通过Docker容器部署Python环境的全流程

《通过Docker容器部署Python环境的全流程》在现代化开发流程中,Docker因其轻量化、环境隔离和跨平台一致性的特性,已成为部署Python应用的标准工具,本文将详细演示如何通过Docker容... 目录引言一、docker与python的协同优势二、核心步骤详解三、进阶配置技巧四、生产环境最佳实践

Python一次性将指定版本所有包上传PyPI镜像解决方案

《Python一次性将指定版本所有包上传PyPI镜像解决方案》本文主要介绍了一个安全、完整、可离线部署的解决方案,用于一次性准备指定Python版本的所有包,然后导出到内网环境,感兴趣的小伙伴可以跟随... 目录为什么需要这个方案完整解决方案1. 项目目录结构2. 创建智能下载脚本3. 创建包清单生成脚本4

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

python获取指定名字的程序的文件路径的两种方法

《python获取指定名字的程序的文件路径的两种方法》本文主要介绍了python获取指定名字的程序的文件路径的两种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 最近在做项目,需要用到给定一个程序名字就可以自动获取到这个程序在Windows系统下的绝对路径,以下

Linux下MySQL数据库定时备份脚本与Crontab配置教学

《Linux下MySQL数据库定时备份脚本与Crontab配置教学》在生产环境中,数据库是核心资产之一,定期备份数据库可以有效防止意外数据丢失,本文将分享一份MySQL定时备份脚本,并讲解如何通过cr... 目录备份脚本详解脚本功能说明授权与可执行权限使用 Crontab 定时执行编辑 Crontab添加定

使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解

《使用Python批量将.ncm格式的音频文件转换为.mp3格式的实战详解》本文详细介绍了如何使用Python通过ncmdump工具批量将.ncm音频转换为.mp3的步骤,包括安装、配置ffmpeg环... 目录1. 前言2. 安装 ncmdump3. 实现 .ncm 转 .mp34. 执行过程5. 执行结

Python实现批量CSV转Excel的高性能处理方案

《Python实现批量CSV转Excel的高性能处理方案》在日常办公中,我们经常需要将CSV格式的数据转换为Excel文件,本文将介绍一个基于Python的高性能解决方案,感兴趣的小伙伴可以跟随小编一... 目录一、场景需求二、技术方案三、核心代码四、批量处理方案五、性能优化六、使用示例完整代码七、小结一、