Use Python's mininet library for traffic control and monitoring
Use Python's mininet library for traffic control and monitoring
Overview:
Mininet is a convenient simulation network environment creation tool that can control the creation of network topology and traffic control through Python programming language.This article will introduce examples of how to use the MINET class library for traffic control and monitoring.
Preparation:
Before the beginning, the Python class library of MININet needs to be installed.You can use the following command to install it:
pip install mininet
Example code for flow control and monitoring:
Next, we will demonstrate how to use the MININT class library for traffic control and monitoring through a simple example.In this example, we will create a simple network topology and control and monitor traffic by setting the traffic mode and monitor.
python
from mininet.net import Mininet
from mininet.node import Host, Switch, Controller
from mininet.link import TCIntf
from mininet.cli import CLI
from mininet.log import setLogLevel
import time
def create_network():
net = Mininet()
# Create switches and hosts
h1 = net.addHost('h1')
h2 = net.addHost('h2')
s1 = net.addSwitch('s1')
# Add connection
net.addLink(h1, s1)
net.addLink(h2, s1)
# Add controller
net.addController('c0')
# 网
net.start()
# Configure traffic and monitor
s1.cmd('tc qdisc add dev s1-eth1 root handle 1: htb default 11')
s1.cmd('tc class add dev s1-eth1 parent 1: classid 1:1 htb rate 100mbps')
s1.cmd('tc class add dev s1-eth1 parent 1:1 classid 1:11 htb rate 50mbps')
s1.cmd('tc filter add dev s1-eth1 protocol ip parent 1:0 prio 1 u32 match ip dst 10.0.0.2 flowid 1:11')
# Start monitoring traffic
s1.cmd('tcpdump -i s1-eth1 -w /tmp/traffic.pcap &')
# 运 CLI
CLI(net)
# Termination network
net.stop()
if __name__ == '__main__':
setLogLevel('info')
create_network()
Code explanation:
First of all, we introduced various components of the Mininet library, including Mininet, Host, Switch, Controller, etc.We then define a function called Create_network to create networks and configuration traffic and monitors.
In the Create_network function, we first created a Mininet object and added two hosts (H1 and H2) and a switch (S1).Then, we connect the host to the switch through the addlink function and add a controller with the AddController function.
Next, we used the TC command to configure the traffic control rules of the switch S1.These rules classify and control network traffic in accordance with certain strategies.For example, we can set up overall bandwidth, bandwidth limit, and flow filtering rules.
Then, we used the TCPDUmp command to start a monitor on the S1-ETH1 port of the switch S1 to capture the data packet of the network traffic and save it as /tmp/traffic.pcap file.
Finally, we hand over the control of the control to the user through the CLI (Net), and can manually operate the network topology on the command line.
Summarize:
This article shows examples of how to control and monitor traffic control and monitoring with Python's MINET class libraries.By creating network topology, configuration traffic and monitor, we can flexibly control and monitor network traffic.This is very helpful for network performance optimization and failure exclusion.It is hoped that readers can understand the basic usage of the MINET class library through this article and be able to use it flexibly in practical applications.