Search code examples
pythonmultithreadingmultiprocessingsdnmininet

Multithreading or multiprocessing in mininet and scapy


As a part of university project I created a topology using Mininet and tried to generate traffic using Scapy. To generate TCP attack on topology a python script named "My_SYNfloodattack.py" is called. Following is a part of main script that shows how it calls.

...
def attack():
attack_hosts = ['h2','h3','h4','h5','h6','h7','h8','h9','h10','h11','h12','h13','h14','h15',
               'h16','h17','h18','h19','h20','h21','h22','h23','h24','h25','h26','h27','h28']
    for item in attack_hosts:
        h1 = net.get(item)
        i = int(item[1:])
        h1.cmd('sudo python /home/pox/ext/My_SYNflood_attack.py 10.0.0.%s 10.0.0.253'%i)
...

when I call attack function, first time 'My_SYNflood_attack.py' is called by "h2", 100 packet is sent completely, then this process happens to "h3" and so on. The problem is that I want to call the function simultaneously, all hosts in list ('h3' to 'h28') start to send packet together. I searched and read about multithreading and multiprocessing but how can I use it in mininet?
Thanks in advance.


Solution

  • You should formulate the part of the task you want to run asynchronously as a function, or (better to my mind, but others may disagree) as the run method of a threading.Thread subclass. Then start a new thread/process for each host and then wait for them all to finish. The code would look something like the following (which is untested):

    from threading import Thread
    ...
    class FloodThread(Thread):
        def __init__(self, hn):
            self.hostnum = hn
            Thread.__init__(self)
        def run(self):
            i = int(self.hn[1:])
            hn.cmd('sudo python /home/pox/ext/My_SYNflood_attack.py 10.0.0.%s 10.0.0.253' % self.i)
    
    ...
    
    def attack():
        threads = []
        attack_hosts = ['h2','h3','h4','h5','h6','h7','h8','h9','h10','h11','h12','h13','h14','h15',
                   'h16','h17','h18','h19','h20','h21','h22','h23','h24','h25','h26','h27','h28']
        for item in attack_hosts:
            hn = net.get(item)
            thread = FloodThread(hn)
            threads.append(thread)
            thread.start()
        for thread in threads:
            thread.wait()
    ...