Unnecessary tests
parent
7d24a53db9
commit
9a4bc83c68
@ -1,63 +0,0 @@
|
|||||||
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,181 +0,0 @@
|
|||||||
import mininet
|
|
||||||
import time, socket, random
|
|
||||||
import numpy as np
|
|
||||||
from mininet.cli import CLI
|
|
||||||
from mininet.log import setLogLevel
|
|
||||||
from mininet.net import Mininet
|
|
||||||
from mininet.topo import Topo
|
|
||||||
from mininet.link import TCLink
|
|
||||||
import matplotlib.pyplot as plt
|
|
||||||
|
|
||||||
# Define custom topology
|
|
||||||
class MyTopology(Topo):
|
|
||||||
def build(self):
|
|
||||||
# Create switches
|
|
||||||
s1 = self.addSwitch('s1')
|
|
||||||
s2 = self.addSwitch('s2')
|
|
||||||
|
|
||||||
# Create hosts
|
|
||||||
h1 = self.addHost('h1')
|
|
||||||
h2 = self.addHost('h2')
|
|
||||||
|
|
||||||
# Add links
|
|
||||||
self.addLink(h1, s1, cls=TCLink, delay='10ms', bw=1)
|
|
||||||
self.addLink(s1, s2, cls=TCLink, delay='50ms', bw=0.5)
|
|
||||||
self.addLink(s2, h2, cls=TCLink, delay='10ms', bw=1)
|
|
||||||
|
|
||||||
# Define TCP agent
|
|
||||||
class MyTCPAgent:
|
|
||||||
def __init__(self):
|
|
||||||
# Initialize TCP agent
|
|
||||||
self.transmission_rounds = []
|
|
||||||
self.congestion_window_sizes = []
|
|
||||||
|
|
||||||
|
|
||||||
def handle_connection(self):
|
|
||||||
print("TCP connection establishment")
|
|
||||||
# Implement TCP connection establishment
|
|
||||||
|
|
||||||
# Example TCP connection establishment using a socket
|
|
||||||
# self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
||||||
# self.sock.connect(('localhost', 19191))
|
|
||||||
|
|
||||||
# Example TCP connection establishment simulation
|
|
||||||
time.sleep(1) # Simulating connection establishment delay
|
|
||||||
|
|
||||||
def handle_data_transfer(self, tunnel, window_size):
|
|
||||||
print("Performing data transfer with tunnel:", tunnel, "and window size:", window_size)
|
|
||||||
# Implement TCP data transfer with the given tunnel and window size using socket programming
|
|
||||||
|
|
||||||
# Implement TCP data transfer simulation
|
|
||||||
|
|
||||||
# Placeholder logic: Simulating data transfer
|
|
||||||
# time.sleep(0.1) # Simulating data transfer delay
|
|
||||||
|
|
||||||
# Simulate congestion control by waiting for a fixed amount of time
|
|
||||||
time.sleep(0.1)
|
|
||||||
|
|
||||||
# Record transmission round and final congestion window size
|
|
||||||
self.transmission_rounds.append(len(self.transmission_rounds) + 1)
|
|
||||||
self.congestion_window_sizes.append(window_size)
|
|
||||||
|
|
||||||
# Define RL agent for tunnel selection
|
|
||||||
class TunnelSelectionAgent:
|
|
||||||
def __init__(self, tunnels):
|
|
||||||
# Initialize your RL agent for tunnel selection
|
|
||||||
self.rewards = []
|
|
||||||
self.selected_tunnels = []
|
|
||||||
self.tunnels = tunnels
|
|
||||||
self.current_tunnel = 0
|
|
||||||
|
|
||||||
def select_tunnel(self):
|
|
||||||
# Implement tunnel selection based on RL policy
|
|
||||||
# Placeholder logic: Select a tunnel randomly or based on some criteria
|
|
||||||
selected_tunnel = self.tunnels[self.current_tunnel]
|
|
||||||
self.current_tunnel = (self.current_tunnel + 1) % len(self.tunnels)
|
|
||||||
return selected_tunnel
|
|
||||||
|
|
||||||
def update_policy(self, reward):
|
|
||||||
# Implement RL policy update based on rewards
|
|
||||||
self.rewards.append(reward)
|
|
||||||
self.selected_tunnels.append(self.select_tunnel())
|
|
||||||
|
|
||||||
# Define RL agent for window prediction
|
|
||||||
class WindowPredictionAgent:
|
|
||||||
def __init__(self):
|
|
||||||
# Initialize the RL agent for window prediction
|
|
||||||
self.window_sizes = [1, 2, 4, 8, 16, 32, 64] # Possible window sizes
|
|
||||||
self.alpha = 0.1 # Learning rate
|
|
||||||
self.gamma = 0.9 # Discount factor
|
|
||||||
self.q_table = {} # Q-table to store state-action values
|
|
||||||
|
|
||||||
def predict_window_size(self):
|
|
||||||
# Get the current state (e.g., network conditions)
|
|
||||||
state = self.get_state()
|
|
||||||
|
|
||||||
# Check if the state is in the Q-table
|
|
||||||
if state not in self.q_table:
|
|
||||||
# Initialize Q-values for all possible actions in the current state
|
|
||||||
self.q_table[state] = {window_size: 0 for window_size in self.window_sizes}
|
|
||||||
|
|
||||||
# Choose the action (window size) based on epsilon-greedy policy
|
|
||||||
if random.random() < 0.2: # Exploration (20% of the time)
|
|
||||||
action = random.choice(self.window_sizes)
|
|
||||||
else: # Exploitation (80% of the time)
|
|
||||||
action = self.get_best_action(state)
|
|
||||||
|
|
||||||
return action
|
|
||||||
|
|
||||||
def get_state(self):
|
|
||||||
# Implement the logic to determine the current state based on network conditions
|
|
||||||
# For example, you can consider factors such as round-trip time, packet loss rate, or congestion signals
|
|
||||||
|
|
||||||
# Placeholder logic: Return a random state
|
|
||||||
return random.randint(1, 10)
|
|
||||||
|
|
||||||
def get_best_action(self, state):
|
|
||||||
# Find the action (window size) with the highest Q-value for the given state
|
|
||||||
best_action = max(self.q_table[state], key=self.q_table[state].get)
|
|
||||||
return best_action
|
|
||||||
|
|
||||||
def update_policy(self, reward):
|
|
||||||
# Update the Q-value based on the reward received after taking an action
|
|
||||||
# Get the previous state and action
|
|
||||||
prev_state = self.get_state() # Replace with the actual previous state
|
|
||||||
prev_action = self.predict_window_size() # Replace with the actual previous action
|
|
||||||
|
|
||||||
# Get the current state
|
|
||||||
curr_state = self.get_state()
|
|
||||||
|
|
||||||
# Check if the current state is in the Q-table
|
|
||||||
if curr_state not in self.q_table:
|
|
||||||
# Initialize Q-values for all possible actions in the current state
|
|
||||||
self.q_table[curr_state] = {window_size: 0 for window_size in self.window_sizes}
|
|
||||||
|
|
||||||
# Update the Q-value using the Q-learning update rule
|
|
||||||
max_q_value = max(self.q_table[curr_state].values()) # Get the maximum Q-value for the current state
|
|
||||||
self.q_table[prev_state][prev_action] += self.alpha * (reward + self.gamma * max_q_value - self.q_table[prev_state][prev_action])
|
|
||||||
|
|
||||||
# Main function
|
|
||||||
if __name__ == '__main__':
|
|
||||||
setLogLevel('info')
|
|
||||||
|
|
||||||
# Create the Mininet network
|
|
||||||
topo = MyTopology()
|
|
||||||
net = Mininet(topo=topo)
|
|
||||||
net.start
|
|
||||||
|
|
||||||
# Instantiate TCP and RL agents
|
|
||||||
tcp_agent = MyTCPAgent()
|
|
||||||
tunnels = ['myPrivate1']
|
|
||||||
tunnel_agent = TunnelSelectionAgent(tunnels)
|
|
||||||
window_agent = WindowPredictionAgent()
|
|
||||||
|
|
||||||
# Perform TCP connection establishment
|
|
||||||
tcp_agent.handle_connection()
|
|
||||||
|
|
||||||
# Perform data transfer with RL-based tunnel selection and window prediction
|
|
||||||
start_time = time.time()
|
|
||||||
while True:
|
|
||||||
if time.time() - start_time > 10: # End packet exchange after <X> seconds
|
|
||||||
break
|
|
||||||
tunnel = tunnel_agent.select_tunnel()
|
|
||||||
window_size = window_agent.predict_window_size()
|
|
||||||
|
|
||||||
# Perform data transfer
|
|
||||||
tcp_agent.handle_data_transfer(tunnel, window_size)
|
|
||||||
|
|
||||||
# Update RL agents based on rewards
|
|
||||||
reward = -0.5 # Actual reward value
|
|
||||||
tunnel_agent.update_policy(reward)
|
|
||||||
# window_agent.update_policy(reward)
|
|
||||||
|
|
||||||
# Stop the Mininet network
|
|
||||||
net.stop()
|
|
||||||
|
|
||||||
# Plot Transmission Round and Congestion Window Size
|
|
||||||
plt.plot(tcp_agent.transmission_rounds, tcp_agent.congestion_window_sizes)
|
|
||||||
plt.xlabel('Transmission Round')
|
|
||||||
plt.ylabel('Congestion Window Size')
|
|
||||||
plt.title('TCP+RL Window Prediction')
|
|
||||||
plt.show()
|
|
@ -1,84 +0,0 @@
|
|||||||
from mininet.net import Mininet
|
|
||||||
from mininet.node import Controller
|
|
||||||
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)
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
setLogLevel('info')
|
|
||||||
|
|
||||||
# Creazione della rete Mininet
|
|
||||||
net = Mininet(controller=PacketCopyController)
|
|
||||||
|
|
||||||
# Creazione degli host e degli switch
|
|
||||||
h1 = net.addHost('h1')
|
|
||||||
h2 = net.addHost('h2')
|
|
||||||
h3 = net.addHost('h3')
|
|
||||||
s1 = net.addSwitch('s1')
|
|
||||||
|
|
||||||
# Creazione dei collegamenti
|
|
||||||
net.addLink(h1, s1)
|
|
||||||
net.addLink(h2, s1)
|
|
||||||
net.addLink(h3, s1)
|
|
||||||
|
|
||||||
# Create the plot
|
|
||||||
plt.figure()
|
|
||||||
plt.xlabel('Time')
|
|
||||||
plt.ylabel('Latency (seconds)')
|
|
||||||
plt.title('Network Latency')
|
|
||||||
|
|
||||||
# Avvio della rete e assegnazione del controller
|
|
||||||
# Add the POX controller
|
|
||||||
controller = net.addController(name='controller', target_host='h3', controller=PacketCopyController, ip='127.0.0.1', port=6633)
|
|
||||||
net.start()
|
|
||||||
controller.start()
|
|
||||||
# controller = net.controllers[0]
|
|
||||||
# controller.net = net
|
|
||||||
|
|
||||||
# Start the plot animation
|
|
||||||
plt.ion()
|
|
||||||
plt.show()
|
|
||||||
|
|
||||||
try:
|
|
||||||
while True:
|
|
||||||
# Update the plot with new latency data
|
|
||||||
plt.plot(timestamps, latencies, 'b-')
|
|
||||||
plt.draw()
|
|
||||||
plt.pause(0.1)
|
|
||||||
except KeyboardInterrupt:
|
|
||||||
pass
|
|
||||||
|
|
||||||
CLI(net)
|
|
||||||
net.stop()
|
|
@ -1,71 +0,0 @@
|
|||||||
import configparser
|
|
||||||
from colorama import Fore, Style
|
|
||||||
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
|
|
||||||
|
|
||||||
class MyRouter (Node):
|
|
||||||
def config(self, **params):
|
|
||||||
super(MyRouter, self).config(**params)
|
|
||||||
self.cmd('sysctl net.ipv4.ip_forward=1') #Enable forwarding on the router
|
|
||||||
def terminate(self):
|
|
||||||
self.cmd('sysctl net.ipv4.ip_forward=0') #Disable forwarding on the router
|
|
||||||
super(MyRouter, self).terminate
|
|
||||||
|
|
||||||
def build_topology(config_file):
|
|
||||||
topo = Topo()
|
|
||||||
elements = {} # Dictionary to store nodes
|
|
||||||
|
|
||||||
# h1 = topo.addHost('H1', ip='161.46.247.2/25', defaultRoute='via 161.46.247.1')
|
|
||||||
# h2 = topo.addHost('H2', ip='161.46.247.3/25', defaultRoute='via 161.46.247.1')
|
|
||||||
|
|
||||||
r0 = topo.addNode('R0', cls=MyRouter, ip='163.172.250.12/16')
|
|
||||||
r1 = topo.addNode('R1', cls=MyRouter, ip='163.172.250.12/28')
|
|
||||||
r2 = topo.addNode('R2', cls=MyRouter, ip='161.46.247.253/30')
|
|
||||||
|
|
||||||
topo.addLink(r0, r1, intfName2='R12', intfName1='R01', params2={'ip' : '163.172.250.12/28'}, params1={'ip' : '163.172.250.12/16'})
|
|
||||||
topo.addLink(r2, r1, intfName2='R13', intfName1='R21', params2={'ip' : '161.46.247.254/30'}, params1={'ip' : '161.46.247.253/30'})
|
|
||||||
|
|
||||||
# s1 = topo.addSwitch('S1')
|
|
||||||
|
|
||||||
# h4 = topo.addHost('H4', ip='161.46.247.131/26', defaultRoute='via 161.46.247.129')
|
|
||||||
# h3 = topo.addHost('H3', ip='161.46.247.196/27', defaultRoute='via 161.46.247.195')
|
|
||||||
|
|
||||||
|
|
||||||
### Ordine importante ###
|
|
||||||
|
|
||||||
# topo.addLink(r1, s1, intfName1='R11', params1={'ip' : '161.46.247.1/25'})
|
|
||||||
|
|
||||||
# topo.addLink(r2, h3, intfName2='H31', intfName1='R22', params1={'ip' : '161.46.247.195/27'})
|
|
||||||
# topo.addLink(r2, h4, intfName2='H41', intfName1='R23', params1={'ip' : '161.46.247.129/26'})
|
|
||||||
|
|
||||||
|
|
||||||
# topo.addLink(s1, h1, intfName2='H11')
|
|
||||||
# topo.addLink(s1, h2 ,intfName2='H21')
|
|
||||||
|
|
||||||
return topo
|
|
||||||
|
|
||||||
def run_topology(config_file):
|
|
||||||
setLogLevel('info') #Different logging levels are 'info' 'warning' 'error' 'debug'
|
|
||||||
topo = build_topology(config_file)
|
|
||||||
net = Mininet(topo=topo, link=TCLink)
|
|
||||||
net.start() #Starting the network
|
|
||||||
|
|
||||||
(net.getNodeByName('R1')).cmd('ip route add default via 161.172.250.1/26')
|
|
||||||
(net.getNodeByName('R1')).cmd('ip route add 161.46.247.128/27 via 161.46.247.253')
|
|
||||||
|
|
||||||
# (net.getNodeByName('R1')).cmd('ip route add 161.46.247.192/27 via 161.46.247.254 dev R13')
|
|
||||||
# (net.getNodeByName('R1')).cmd('ip route add 161.46.247.128/26 via 161.46.247.254 dev R13')
|
|
||||||
|
|
||||||
if net.pingAll():
|
|
||||||
print(Fore.RED + "Network has issues" + Style.RESET_ALL)
|
|
||||||
else:
|
|
||||||
print(Fore.GREEN + "Network working properly" + Style.RESET_ALL)
|
|
||||||
CLI(net)
|
|
||||||
net.stop() #Stopping the network
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
run_topology('MininetTopo.conf')
|
|
Loading…
Reference in New Issue