Tests in Mininet Env
parent
0109446686
commit
7d24a53db9
@ -0,0 +1,63 @@
|
|||||||
|
from mininet.net import Mininet
|
||||||
|
from mininet.node import Controller, OVSSwitch
|
||||||
|
from mininet.cli import CLI
|
||||||
|
from mininet.log import setLogLevel
|
||||||
|
|
||||||
|
class CustomController( Controller ):
|
||||||
|
"Open vSwitch controller"
|
||||||
|
def __init__( self, name, **kwargs ):
|
||||||
|
kwargs.setdefault( 'command', self.isAvailable() or
|
||||||
|
'ovs-controller' )
|
||||||
|
Controller.__init__( self, name, **kwargs )
|
||||||
|
|
||||||
|
|
||||||
|
# class CustomController(Controller):
|
||||||
|
# def _handle_ConnectionUp(self, event):
|
||||||
|
# # Handle new connection
|
||||||
|
# dpid_str = dpid_to_str(event.dpid)
|
||||||
|
# log.info("Switch %s connected", dpid_str)
|
||||||
|
# self.connection = event.connection
|
||||||
|
# event.connection.addListeners(self)
|
||||||
|
|
||||||
|
# def _handle_PacketIn(self, event):
|
||||||
|
# # Handle incoming packet
|
||||||
|
# packet = event.parsed
|
||||||
|
# if packet.type == ethernet.IP_TYPE:
|
||||||
|
# ip_packet = packet.payload
|
||||||
|
# src_ip = ip_packet.srcip
|
||||||
|
# dst_ip = ip_packet.dstip
|
||||||
|
# log.info("Source IP: %s, Destination IP: %s", src_ip, dst_ip)
|
||||||
|
|
||||||
|
# # Call the parent handler to continue processing other events
|
||||||
|
# super(CustomController, self)._handle_PacketIn(event)
|
||||||
|
|
||||||
|
def create_topology():
|
||||||
|
net = Mininet(controller=Controller, switch=OVSSwitch)
|
||||||
|
|
||||||
|
# Create network nodes
|
||||||
|
h1 = net.addHost('h1')
|
||||||
|
h2 = net.addHost('h2')
|
||||||
|
s1 = net.addSwitch('s1', cls=OVSSwitch)
|
||||||
|
|
||||||
|
# Create links
|
||||||
|
net.addLink(h1, s1)
|
||||||
|
net.addLink(h2, s1)
|
||||||
|
|
||||||
|
# Start the network
|
||||||
|
net.start()
|
||||||
|
|
||||||
|
# Create a custom controller instance
|
||||||
|
controller = net.addController('c1', controller=CustomController)
|
||||||
|
|
||||||
|
# Connect the switch to the controller
|
||||||
|
s1.start([controller])
|
||||||
|
|
||||||
|
# Enter command line mode
|
||||||
|
CLI(net)
|
||||||
|
|
||||||
|
# Stop the network
|
||||||
|
net.stop()
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
setLogLevel('info')
|
||||||
|
create_topology()
|
@ -1,109 +1,84 @@
|
|||||||
import heapq
|
|
||||||
import random
|
|
||||||
from mininet.net import Mininet
|
from mininet.net import Mininet
|
||||||
from mininet.node import OVSSwitch, Controller
|
from mininet.node import Controller
|
||||||
from mininet.link import TCLink
|
|
||||||
from mininet.net import Mininet
|
|
||||||
from mininet.topo import Topo
|
|
||||||
from mininet.node import Node
|
|
||||||
from mininet.cli import CLI
|
|
||||||
from mininet.link import TCLink
|
|
||||||
from mininet.log import setLogLevel
|
from mininet.log import setLogLevel
|
||||||
|
from mininet.cli import CLI
|
||||||
|
from pox.lib import of
|
||||||
|
import time
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
|
||||||
|
# Create empty lists to store the timestamp and latency values
|
||||||
|
timestamps = []
|
||||||
|
latencies = []
|
||||||
|
|
||||||
|
class PacketCopyController(Controller):
|
||||||
|
def __init__(self, name, target_host, **kwargs):
|
||||||
|
Controller.__init__(self, name, **kwargs)
|
||||||
|
self.target_host = target_host
|
||||||
|
|
||||||
|
def _handle_PacketIn(self, event):
|
||||||
|
packet = event.parsed
|
||||||
|
self.packet_out(event.port, packet)
|
||||||
|
self.packet_out_to_host(packet)
|
||||||
|
# Measure latency and store timestamp and latency values
|
||||||
|
latency = time.time() - event.created
|
||||||
|
timestamps.append(time.time())
|
||||||
|
latencies.append(latency)
|
||||||
|
|
||||||
|
def packet_out(self, out_port, packet):
|
||||||
|
msg = of.ofp_packet_out()
|
||||||
|
msg.data = packet.pack()
|
||||||
|
action = of.ofp_action_output(port=out_port)
|
||||||
|
msg.actions.append(action)
|
||||||
|
self.connection.send(msg)
|
||||||
|
|
||||||
|
def packet_out_to_host(self, packet):
|
||||||
|
host = self.net.get(self.target_host)
|
||||||
|
if host:
|
||||||
|
host.sendMsg(packet)
|
||||||
|
|
||||||
class MyRouter (Node):
|
if __name__ == '__main__':
|
||||||
def config(self, **params):
|
setLogLevel('info')
|
||||||
super(MyRouter, self).config(**params)
|
|
||||||
self.cmd('sysctl net.ipv4.ip_forward=1') #Enable forwarding on the router
|
# Creazione della rete Mininet
|
||||||
def terminate(self):
|
net = Mininet(controller=PacketCopyController)
|
||||||
self.cmd('sysctl net.ipv4.ip_forward=0') #Disable forwarding on the router
|
|
||||||
super(MyRouter, self).terminate
|
# Creazione degli host e degli switch
|
||||||
|
h1 = net.addHost('h1')
|
||||||
def create_topology():
|
h2 = net.addHost('h2')
|
||||||
net = Topo()
|
h3 = net.addHost('h3')
|
||||||
|
s1 = net.addSwitch('s1')
|
||||||
# Create the SD-WAN sites
|
|
||||||
site1 = net.addHost('site1')
|
# Creazione dei collegamenti
|
||||||
site2 = net.addHost('site2')
|
net.addLink(h1, s1)
|
||||||
site3 = net.addHost('site3')
|
net.addLink(h2, s1)
|
||||||
|
net.addLink(h3, s1)
|
||||||
# Create the virtual SD-WAN devices
|
|
||||||
sdwan1 = net.addHost('sdwan1')
|
# Create the plot
|
||||||
sdwan2 = net.addHost('sdwan2')
|
plt.figure()
|
||||||
|
plt.xlabel('Time')
|
||||||
# Create switches
|
plt.ylabel('Latency (seconds)')
|
||||||
switch1 = net.addSwitch('s1')
|
plt.title('Network Latency')
|
||||||
|
|
||||||
# Connect the hosts and switches
|
# Avvio della rete e assegnazione del controller
|
||||||
net.addLink(site1, switch1, bw=10, delay='10ms')
|
# Add the POX controller
|
||||||
net.addLink(site2, switch1, bw=5, delay='20ms')
|
controller = net.addController(name='controller', target_host='h3', controller=PacketCopyController, ip='127.0.0.1', port=6633)
|
||||||
net.addLink(site3, switch1, bw=8, delay='15ms')
|
net.start()
|
||||||
net.addLink(sdwan1, switch1, bw=100, delay='1ms')
|
controller.start()
|
||||||
net.addLink(sdwan2, switch1, bw=100, delay='1ms')
|
# controller = net.controllers[0]
|
||||||
|
# controller.net = net
|
||||||
return net
|
|
||||||
|
# Start the plot animation
|
||||||
def dynamic_path_selection(net, link_qos): # Function to select the best path based on real-time conditions using Dijkstra algorithm with QoS metrics
|
plt.ion()
|
||||||
|
plt.show()
|
||||||
# Add a random factor to the QoS metric
|
|
||||||
random_range = 4
|
try:
|
||||||
for link in link_qos:
|
while True:
|
||||||
link_qos[link] += random.randint(-random_range, random_range)
|
# Update the plot with new latency data
|
||||||
|
plt.plot(timestamps, latencies, 'b-')
|
||||||
# Perform Dijkstra algorithm to find the best path based on QoS metrics
|
plt.draw()
|
||||||
def dijkstra(source):
|
plt.pause(0.1)
|
||||||
distance = {node: float('inf') for node in net}
|
except KeyboardInterrupt:
|
||||||
distance[source] = 0
|
pass
|
||||||
queue = [(0, source)]
|
|
||||||
while queue:
|
|
||||||
dist, node = heapq.heappop(queue)
|
|
||||||
if dist > distance[node]:
|
|
||||||
continue
|
|
||||||
for neighbor, _, link_info in net[node].connectionsTo(net):
|
|
||||||
qos = link_qos.get(link_info[0].intf1.name)
|
|
||||||
new_dist = dist + qos
|
|
||||||
if new_dist < distance[neighbor]:
|
|
||||||
distance[neighbor] = new_dist
|
|
||||||
heapq.heappush(queue, (new_dist, neighbor))
|
|
||||||
return distance
|
|
||||||
|
|
||||||
# Run Dijkstra algorithm from each site to determine the best path
|
|
||||||
site1_distance = dijkstra('site1')
|
|
||||||
site2_distance = dijkstra('site2')
|
|
||||||
site3_distance = dijkstra('site3')
|
|
||||||
sdwan1_distance = dijkstra('sdwan1')
|
|
||||||
sdwan2_distance = dijkstra('sdwan2')
|
|
||||||
|
|
||||||
# Select the best path based on minimum total QoS distance
|
|
||||||
best_path = min(site1_distance, site2_distance, site3_distance, sdwan1_distance, sdwan2_distance,
|
|
||||||
key=lambda x: sum(x.values()))
|
|
||||||
print("Best path based on QoS metrics:")
|
|
||||||
for node, distance in best_path.items():
|
|
||||||
print(f"Node: {node}, Total QoS Distance: {distance}")
|
|
||||||
|
|
||||||
def run_topology():
|
|
||||||
setLogLevel('info') #Different logging levels are 'info' 'warning' 'error' 'debug'
|
|
||||||
topo = create_topology()
|
|
||||||
net = Mininet(topo=topo, link=TCLink)
|
|
||||||
# net.addController('c0', controller=Controller) #non serve metterlo esplicitamente
|
|
||||||
|
|
||||||
net.start() #Starting the network
|
|
||||||
# Define QoS metrics for each link (example values)
|
|
||||||
link_qos = {
|
|
||||||
'site1-eth0': 8, # QoS value for link site1 -> switch1
|
|
||||||
'site2-eth0': 6, # QoS value for link site2 -> switch1
|
|
||||||
'site3-eth0': 9, # QoS value for link site3 -> switch1
|
|
||||||
'sdwan1-eth0': 10, # QoS value for link sdwan1 -> switch1
|
|
||||||
'sdwan2-eth0': 7 # QoS value for link sdwan2 -> switch1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Add a random factor to the QoS metric
|
|
||||||
random_range = 4
|
|
||||||
for link in link_qos:
|
|
||||||
link_qos[link] += random.randint(-random_range, random_range)
|
|
||||||
|
|
||||||
dynamic_path_selection(net, link_qos)
|
|
||||||
CLI(net)
|
CLI(net)
|
||||||
net.stop() #Stopping the network
|
net.stop()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
run_topology()
|
|
||||||
|
@ -0,0 +1,46 @@
|
|||||||
|
######### Set Network Nodes #########
|
||||||
|
N_host steffe0
|
||||||
|
N_host steffe1
|
||||||
|
N_host steffe2
|
||||||
|
N_host steffe3
|
||||||
|
N_host steffe4
|
||||||
|
N_host steffe5
|
||||||
|
N_host steffe6
|
||||||
|
N_host steffe7
|
||||||
|
N_host steffe8
|
||||||
|
N_host steffe9
|
||||||
|
N_host steffe10
|
||||||
|
N_host steffe11
|
||||||
|
N_host steffe12
|
||||||
|
N_host steffe13
|
||||||
|
N_host steffe14
|
||||||
|
N_host steffe15
|
||||||
|
N_host steffe16
|
||||||
|
N_host steffe17
|
||||||
|
N_host steffe18
|
||||||
|
N_host steffe19
|
||||||
|
N_host steffe20
|
||||||
|
N_switch s0
|
||||||
|
|
||||||
|
############################################ Set Network Links ############################################
|
||||||
|
SN_link s0 steffe0 1000
|
||||||
|
SN_link s0 steffe1 1000
|
||||||
|
SN_link s0 steffe2 1000
|
||||||
|
SN_link s0 steffe3 1000
|
||||||
|
SN_link s0 steffe4 1000
|
||||||
|
SN_link s0 steffe5 1000
|
||||||
|
SN_link s0 steffe6 1000
|
||||||
|
SN_link s0 steffe7 1000
|
||||||
|
SN_link s0 steffe8 1000
|
||||||
|
SN_link s0 steffe9 1000
|
||||||
|
SN_link s0 steffe10 1000
|
||||||
|
SN_link s0 steffe11 1000
|
||||||
|
SN_link s0 steffe12 1000
|
||||||
|
SN_link s0 steffe13 1000
|
||||||
|
SN_link s0 steffe14 1000
|
||||||
|
SN_link s0 steffe15 1000
|
||||||
|
SN_link s0 steffe16 1000
|
||||||
|
SN_link s0 steffe17 1000
|
||||||
|
SN_link s0 steffe18 1000
|
||||||
|
SN_link s0 steffe19 1000
|
||||||
|
SN_link s0 steffe20 1000
|
Loading…
Reference in New Issue