|
| 1 | +from mininet.topo import Topo |
| 2 | +from mininet.link import Link |
| 3 | +from mininet import util |
| 4 | +from mininet.node import OVSSwitch |
| 5 | +import time |
| 6 | +import subprocess |
| 7 | +import os |
| 8 | +import atexit |
| 9 | + |
| 10 | + |
| 11 | +class QKLink(Link): |
| 12 | + @classmethod |
| 13 | + def makeIntfPair(cls, intfname1, intfname2, addr1=None, addr2=None, |
| 14 | + node1=None, node2=None, deleteIntfs=True): |
| 15 | + """Create pair of interfaces |
| 16 | + intfname1: name for interface 1 |
| 17 | + intfname2: name for interface 2 |
| 18 | + addr1: MAC address for interface 1 (optional) |
| 19 | + addr2: MAC address for interface 2 (optional) |
| 20 | + node1: home node for interface 1 (optional) |
| 21 | + node2: home node for interface 2 (optional) |
| 22 | + (override this method [and possibly delete()] |
| 23 | + to change link type)""" |
| 24 | + # Leave this as a class method for now |
| 25 | + assert cls |
| 26 | + return makeIntfPair(intfname1, intfname2, addr1, addr2, node1, node2, |
| 27 | + deleteIntfs) |
| 28 | + |
| 29 | + def stop(self): |
| 30 | + for p in QKLink.processes: |
| 31 | + p.kill() |
| 32 | + self.delete() |
| 33 | + |
| 34 | + |
| 35 | +QKLink.processes = [] |
| 36 | + |
| 37 | + |
| 38 | +def makeIntfPair(intf1, intf2, addr1=None, addr2=None, node1=None, node2=None, |
| 39 | + deleteIntfs=True, runCmd=None): |
| 40 | + """Make a veth pair connnecting new interfaces intf1 and intf2 |
| 41 | + intf1: name for interface 1 |
| 42 | + intf2: name for interface 2 |
| 43 | + addr1: MAC address for interface 1 (optional) |
| 44 | + addr2: MAC address for interface 2 (optional) |
| 45 | + node1: home node for interface 1 (optional) |
| 46 | + node2: home node for interface 2 (optional) |
| 47 | + deleteIntfs: delete intfs before creating them |
| 48 | + runCmd: function to run shell commands (quietRun) |
| 49 | + raises Exception on failure""" |
| 50 | + if not runCmd: |
| 51 | + runCmd = util.quietRun if not node1 else node1.cmd |
| 52 | + runCmd2 = util.quietRun if not node2 else node2.cmd |
| 53 | + if deleteIntfs: |
| 54 | + # Delete any old interfaces with the same names |
| 55 | + runCmd('ip link del ' + intf1) |
| 56 | + runCmd2('ip link del ' + intf2) |
| 57 | + """cmdOutput = util.run('ip tuntap add %s mode tap' % |
| 58 | + (intf1)) |
| 59 | + if cmdOutput: |
| 60 | + raise Exception("Error creating interface pair (%s,%s): %s " % |
| 61 | + (intf1, intf2, cmdOutput)) |
| 62 | + cmdOutput = util.run('ip tuntap add %s mode tap' % |
| 63 | + (intf2)) |
| 64 | + if cmdOutput: |
| 65 | + raise Exception("Error creating interface pair (%s,%s): %s " % |
| 66 | + (intf1, intf2, cmdOutput))""" |
| 67 | + # Create new pair |
| 68 | + netns = 1 if not node2 else node2.pid |
| 69 | + print( |
| 70 | + '\nctapudp -s 0.0.0.0 -p %i -t 127.0.0.1 -k %i -i %s -a 1 -q 127.0.0.1 -r 55554 -e 100\n' % ( |
| 71 | + makeIntfPair.portscount, makeIntfPair.portscount + 1, intf1)) |
| 72 | + print( |
| 73 | + '\nctapudp -s 0.0.0.0 -p %i -t 127.0.0.1 -k %i -i %s -a 1 -q 127.0.0.1 -r 55554 -e 100\n' % ( |
| 74 | + makeIntfPair.portscount + 1, makeIntfPair.portscount, intf2)) |
| 75 | + process = subprocess.Popen( |
| 76 | + ['ctapudp', '-s', '127.0.0.1', '-p', str(makeIntfPair.portscount), '-t', '127.0.0.1', |
| 77 | + '-k', |
| 78 | + str(makeIntfPair.portscount + 1), '-i', intf1, '-a', '1', '-q', '127.0.0.1', '-r', '55554', '-e', |
| 79 | + '100'""", '-d', '1'"""], preexec_fn=os.setpgrp) |
| 80 | + QKLink.processes.append(process) |
| 81 | + process = subprocess.Popen( |
| 82 | + ['ctapudp', '-s', '127.0.0.1', '-p', str(makeIntfPair.portscount + 1), '-t', '127.0.0.1', |
| 83 | + '-k', |
| 84 | + str(makeIntfPair.portscount), '-i', intf2, '-a', '1', '-q', '127.0.0.1', '-r', '55554', '-e', |
| 85 | + '100'""", '-d', '1'"""], preexec_fn=os.setpgrp) |
| 86 | + QKLink.processes.append(process) |
| 87 | + time.sleep(0.5) |
| 88 | + makeIntfPair.portscount = makeIntfPair.portscount + 2 |
| 89 | + if addr1 is None: |
| 90 | + cmdOutput = util.run('ip link set %s ' |
| 91 | + 'netns %s' % |
| 92 | + (intf1, node1.pid)) |
| 93 | + else: |
| 94 | + cmdOutput = util.run('ip link set %s address %s ' |
| 95 | + 'netns %s' % |
| 96 | + (intf1, addr1, node1.pid)) |
| 97 | + |
| 98 | + if cmdOutput: |
| 99 | + for p in QKLink.processes: |
| 100 | + p.kill() |
| 101 | + raise Exception("Error creating interface pair (%s,%s): %s " % |
| 102 | + (intf1, intf2, cmdOutput)) |
| 103 | + if addr2 is None: |
| 104 | + cmdOutput = util.run('ip link set %s ' |
| 105 | + 'netns %s' % |
| 106 | + (intf2, netns)) |
| 107 | + else: |
| 108 | + cmdOutput = util.run('ip link set %s address %s ' |
| 109 | + 'netns %s' % |
| 110 | + (intf2, addr2, netns)) |
| 111 | + |
| 112 | + if cmdOutput: |
| 113 | + for p in QKLink.processes: |
| 114 | + p.kill() |
| 115 | + raise Exception("Error creating interface pair (%s,%s): %s " % |
| 116 | + (intf1, intf2, cmdOutput)) |
| 117 | + |
| 118 | + |
| 119 | +makeIntfPair.portscount = 3333 |
| 120 | + |
| 121 | + |
| 122 | +class QTopo(Topo): |
| 123 | + "2 switch 2 host connetcted by QKLink custom topology" |
| 124 | + |
| 125 | + def __init__(self): |
| 126 | + "Create custom topo." |
| 127 | + |
| 128 | + # Initialize topology |
| 129 | + Topo.__init__(self) |
| 130 | + |
| 131 | + # Add hosts and switches |
| 132 | + leftHost = self.addHost('h1') |
| 133 | + rightHost = self.addHost('h2') |
| 134 | + leftSwitch = self.addSwitch('s1', cls = OVSSwitch, protocols='OpenFlow13', dpid='000016ccbbe0d642') |
| 135 | + rightSwitch = self.addSwitch('s2', cls = OVSSwitch, protocols='OpenFlow13', dpid='00001ad87e868d45') |
| 136 | + |
| 137 | + # Add links |
| 138 | + self.addLink(leftHost, leftSwitch, cls=QKLink) |
| 139 | + self.addLink(rightHost, rightSwitch, cls=QKLink) |
| 140 | + self.addLink(leftSwitch, rightSwitch) |
| 141 | + self.addLink(leftSwitch, rightSwitch, cls=QKLink) |
| 142 | + |
| 143 | + |
| 144 | +process = subprocess.Popen( |
| 145 | + ['keyworker', '-n', '/opt/bal/var/db1', '-w', '2'""", '-d', '1'"""], |
| 146 | + preexec_fn=os.setpgrp) |
| 147 | + |
| 148 | + |
| 149 | +def exit_handler(): |
| 150 | + process.kill() |
| 151 | + |
| 152 | + |
| 153 | +atexit.register(exit_handler) |
| 154 | +topos = {'qtopo': (lambda: QTopo())} |
0 commit comments