Skip to content

Instantly share code, notes, and snippets.

@mkocabas
Created April 16, 2018 14:07
Show Gist options
  • Save mkocabas/0af22cc2a21a371f6b5c1af7d5e40e3e to your computer and use it in GitHub Desktop.
Save mkocabas/0af22cc2a21a371f6b5c1af7d5e40e3e to your computer and use it in GitHub Desktop.
from mininet.net import Mininet
from random import randint
from scipy import spatial
import numpy as np
import scipy as scp
import math
## Mininet must run as ROOT !!!
# add function for nodes to move randomly when invoked
# check 0mq code and how to integrate it here
# write table of hosts and their corresponding IPs to a file
## number of hosts
nmb_hosts=100
## Playground Dimensions
min_x=0
min_y=0
max_x=100
max_y=100
max_r=2000 ## max transmission radius
# Generate a random position in the x axis
def gen_x_pos():
return randint(min_x,max_x);
# Generate a random position in the y axis
def gen_y_pos():
return randint(min_y,max_y);
#Create mininet hosts
#Give them random locations
def define_hosts(net):
hosts=[]
host_post=np.zeros((nmb_hosts,2))
for i in range(nmb_hosts):
#We might need to change this later on and define an interface first then assign an IP
#hosts.append(net.addHost('h%s'%(i+1), ip='192.168.0.%s/24'%(i+10)))
hosts.append(net.addHost('h%s'%(i+1) ))
#h[i].setIP('10.0.0.%s/24'%(i+2))
host_post[i,0]=gen_x_pos()
host_post[i,1]=gen_y_pos()
switch = net.addSwitch( 's0' )
return hosts,host_post,switch;
# Create Links
def define_links(net,hosts,host_post):
# Link Properties
#linkopts = dict(bw=10, delay='5ms', loss=10, max_queue_size=1000, use_htb=True)
links=[]
for i in range((nmb_hosts-1)):
for j in range((i+1,nmb_hosts)):
if(scp.spatial.distance.sqeuclidean(host_post[i],host_post[j]) <= max_r):
#if(dist(h_post[i],h_post[j]) <= max_r):
links.append(net.addLink( hosts[i], hosts[j] ))
return links;
# Create Links between all hosts and the single switch
def define_switch_links(net,hosts,switch):
# Link Properties
#linkopts = dict(bw=10, delay='5ms', loss=10, max_queue_size=1000, use_htb=True)
links=[]
for i in range((nmb_hosts)):
links.append(net.addLink( hosts[i], switch ))
hosts[i].setIP('192.168.0.%s/24'%(i+10))
return links;
def hosts_run_script(hosts):
for i in range((nmb_hosts-1)):
## We pass the id of the host when we run its own script
hosts[i].sendCmd("python myScript.py %s %s &",i , nmb_hosts)
return;
def print_host_config(hosts):
for i in range((nmb_hosts-1)):
hosts[i].cmd( 'ifconfig' )
return;
def create_adjacency(host_post):
adj_mat = np.zeros((nmb_hosts,nmb_hosts))
for i in range((nmb_hosts)):
for j in range((nmb_hosts)):
adj_mat[i,j] = scp.spatial.distance.sqeuclidean(host_post[i],host_post[j])
return adj_mat;
def get_IP_list(hosts):
IP_list=[]
for i in range(nmb_hosts):
IP_list.append(hosts[i].IP())
return IP_list;
# Define the network
def define_network():
net = Mininet()
hosts,host_post,switch=define_hosts(net)
#links = define_links(net,hosts,host_post)
links = define_switch_links(net,hosts,switch)
net.start()
#
# print_host_config(hosts)
#
# hosts_run_script(hosts)
np.save("./location_matrix.npy",host_post)
adj_mat = create_adjacency(host_post)
np.save("./adjacency_matrix.npy",adj_mat)
IP_list = get_IP_list(hosts)
np.save("./IP_list.npy",np.asarray(IP_list))
net.stop()
## Not needed anymore, links do not exist in mininet anymore
# print "Testing network connectivity"
# net.pingAll()
## Might be useful in the future
# print "Testing bandwidth between h1 and h4"
# h1, h4 = net.get( 'h1', 'h4' )
# net.iperf( (h1, h4) )
# print h1.cmd( 'ping -c1', h2.IP() )
return net
#Squared Euclidean Distance
def dist(h1,h2):
return math.sqrt(((h1[0]-h2[0])**2)+((h1[1]-h2[1])**2));
def main():
net=define_network();
return ;
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment