def five_line(): # parameters Sim.scheduler.reset() # setup network net = Network('../networks/five-nodes-line.txt', DVRouter) for n in net.nodes: net.nodes[n].add_protocol(protocol="dvrouting", handler=RoutingApp(net.nodes[n])) Sim.scheduler.add(delay=11, event=None, handler=end_simulation) # send packet from n2 to n5 and from n5 to n2 p = Packet(destination_address=net.nodes['n5'].get_address('n4'), length=100) Sim.scheduler.add(10.1, event=p, handler=net.nodes['n2'].send_packet) p = Packet(destination_address=net.nodes['n2'].get_address('n3'), length=100) Sim.scheduler.add(10.2, event=p, handler=net.nodes['n5'].send_packet) # send packet from n2 to n4 and from n4 to n2 p = Packet(destination_address=net.nodes['n4'].get_address('n3'), length=100) Sim.scheduler.add(10.3, event=p, handler=net.nodes['n2'].send_packet) p = Packet(destination_address=net.nodes['n2'].get_address('n3'), length=100) Sim.scheduler.add(10.4, event=p, handler=net.nodes['n4'].send_packet) # run the simulation Sim.scheduler.run()
def lan(): # parameters Sim.scheduler.reset() h1 = Host('h1') h1.add_protocol('arp', ARPApp(h1)) h2 = Host('h2') h2.add_protocol('arp', ARPApp(h2)) h1.add_link( Link(mac_address='AA-AA-AA-AA', address=IPAddress('1.1.1.2'), prefix_len=28, startpoint=h1, endpoint=h2)) h2.add_link( Link(mac_address='BB-BB-BB-BB', address=IPAddress('1.1.1.5'), prefix_len=28, startpoint=h2, endpoint=h1)) # send packet from h1 to h2 p = Packet(destination_address=h2.get_address('h1'), length=100) Sim.scheduler.add(1.1, event=p, handler=h1.send_packet) p = Packet(destination_address=h1.get_address('h2'), length=100) Sim.scheduler.add(1.2, event=p, handler=h2.send_packet) # run the simulation Sim.scheduler.run()
def main(): # parameters Sim.scheduler.reset() # setup network net = Network('../networks/five-nodes.txt') # get nodes n1 = net.get_node('n1') n2 = net.get_node('n2') n3 = net.get_node('n3') n4 = net.get_node('n4') n5 = net.get_node('n5') # setup broadcast application b1 = BroadcastApp(n1) n1.add_protocol(protocol="broadcast", handler=b1) b2 = BroadcastApp(n2) n2.add_protocol(protocol="broadcast", handler=b2) b3 = BroadcastApp(n3) n3.add_protocol(protocol="broadcast", handler=b3) b4 = BroadcastApp(n4) n4.add_protocol(protocol="broadcast", handler=b4) b5 = BroadcastApp(n5) n5.add_protocol(protocol="broadcast", handler=b5) # send a broadcast packet from 1 with TTL 2, so everyone should get it p = Packet(source_address=n1.get_address('n2'), destination_address=BROADCAST_IP_ADDRESS, ident=1, ttl=2, protocol='broadcast', length=100) Sim.scheduler.add(delay=0, event=p, handler=n1.send_packet) # send a broadcast packet from 1 with TTL 1, so just nodes 2 and 3 # should get it p = Packet(source_address=n1.get_address('n2'), destination_address=BROADCAST_IP_ADDRESS, ident=2, ttl=1, protocol='broadcast', length=100) Sim.scheduler.add(delay=1, event=p, handler=n1.send_packet) # send a broadcast packet from 3 with TTL 1, so just nodes 1, 4, and 5 # should get it p = Packet(source_address=n3.get_address('n1'), destination_address=BROADCAST_IP_ADDRESS, ident=3, ttl=1, protocol='broadcast', length=100) Sim.scheduler.add(delay=2, event=p, handler=n3.send_packet) # run the simulation Sim.scheduler.run()
def run1(filename): Sim.scheduler.reset() net = Network(filename) # setup routes a = net.get_node('a') b = net.get_node('b') c = net.get_node('c') a.add_forwarding_entry(c.get_address('b'), a.links[0]) b.add_forwarding_entry(c.get_address('b'), b.links[1]) # setup app PH = PacketHandler() net.nodes['c'].add_protocol(protocol="delay", handler=PH) amount_data = 8 * 1000000 packet_size = 8 * 1000 packet2send = amount_data / packet_size delay_amount = packet_size / a.links[0].bandwidth count = 0 for i in range(int(packet2send)): count += 1 p = Packet(destination_address=c.get_address('b'), protocol='delay', ident=i + 1, length=1000) Sim.scheduler.add(delay=(i * delay_amount), event=p, handler=a.send_packet) Sim.scheduler.run()
def generic_3node(file_name, time_to_send_packet): # parameters Sim.scheduler.reset() # setup network net = Network(file_name) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n3 = net.get_node('n3') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n1.add_forwarding_entry(address=n3.get_address('n2'), link=n1.links[0]) n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0]) n2.add_forwarding_entry(address=n3.get_address('n2'), link=n2.links[1]) n3.add_forwarding_entry(address=n2.get_address('n3'), link=n2.links[0]) # setup app d = DelayHandler() net.nodes['n3'].add_protocol(protocol="delay", handler=d) # send 10,000 packets for i in range(1, 10001): p = Packet(destination_address=n3.get_address('n2'), ident=i, protocol='delay', length=1000) calc_delay = (i - 1) * time_to_send_packet Sim.scheduler.add(delay=calc_delay, event=p, handler=n1.send_packet) # run the simulation Sim.scheduler.run()
def main(): # parameters Sim.scheduler.reset() # setup network net = Network('../networks/one-hop.txt') # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0]) # setup app d = DelayHandler() net.nodes['n2'].add_protocol(protocol="delay", handler=d) # send one packet p = Packet(destination_address=n2.get_address('n1'), ident=1, protocol='delay', length=1000) Sim.scheduler.add(delay=0, event=p, handler=n1.send_packet) # take the link down Sim.scheduler.add(delay=1, event=None, handler=n1.get_link('n2').down) # send one packet (it won't go through) p = Packet(destination_address=n2.get_address('n1'), ident=1, protocol='delay', length=1000) Sim.scheduler.add(delay=1.1, event=p, handler=n1.send_packet) # bring the link up Sim.scheduler.add(delay=2, event=None, handler=n1.get_link('n2').up) # send one packet (and now it goes through) p = Packet(destination_address=n2.get_address('n1'), ident=1, protocol='delay', length=1000) Sim.scheduler.add(delay=2.1, event=p, handler=n1.send_packet) # run the simulation Sim.scheduler.run()
def five_ring(): # parameters Sim.scheduler.reset() logging.getLogger('bene.node').setLevel(logging.INFO) # setup network net = Network('../networks/five-nodes-ring.txt', DVRouter) for n in net.nodes: net.nodes[n].add_protocol(protocol="dvrouting", handler=RoutingApp(net.nodes[n])) Sim.scheduler.add(delay=20, event=None, handler=end_simulation) # send packet from n2 to n5 and from n5 to n2 p = Packet(destination_address=net.nodes['n5'].get_address('n4'), length=100) Sim.scheduler.add(10.1, event=p, handler=net.nodes['n2'].send_packet) p = Packet(destination_address=net.nodes['n2'].get_address('n3'), length=100) Sim.scheduler.add(10.2, event=p, handler=net.nodes['n5'].send_packet) # send packet from n4 to n2 and from n4 to n2 p = Packet(destination_address=net.nodes['n4'].get_address('n3'), length=100) Sim.scheduler.add(10.3, event=p, handler=net.nodes['n2'].send_packet) p = Packet(destination_address=net.nodes['n2'].get_address('n3'), length=100) Sim.scheduler.add(10.4, event=p, handler=net.nodes['n4'].send_packet) # at t = 11, bring down (both sides of) link between n5 and n1 Sim.scheduler.add(11, event=None, handler=net.nodes['n5'].get_link('n1').down) Sim.scheduler.add(11, event=None, handler=net.nodes['n1'].get_link('n5').down) # send packet from n2 to n5 and from n5 to n2 - should fail p = Packet(destination_address=net.nodes['n5'].get_address('n4'), length=100) Sim.scheduler.add(12.1, event=p, handler=net.nodes['n2'].send_packet) p = Packet(destination_address=net.nodes['n2'].get_address('n3'), length=100) Sim.scheduler.add(12.2, event=p, handler=net.nodes['n5'].send_packet) # send packet from n2 to n5 and from n5 to n2 - should take different path p = Packet(destination_address=net.nodes['n5'].get_address('n4'), length=100) Sim.scheduler.add(15.1, event=p, handler=net.nodes['n2'].send_packet) p = Packet(destination_address=net.nodes['n2'].get_address('n3'), length=100) Sim.scheduler.add(15.2, event=p, handler=net.nodes['n5'].send_packet) # run the simulation Sim.scheduler.run()
def handle(self, event): # quit if done now = Sim.scheduler.current_time() if (now - self.start) > self.duration: return # generate a packet self.ident += 1 p = Packet(destination_address=self.destination, ident=self.ident, protocol='delay', length=1000) Sim.scheduler.add(delay=0, event=p, handler=self.node.send_packet) # schedule the next time we should generate a packet Sim.scheduler.add(delay=random.expovariate(self.load), event='generate', handler=self.handle)
def generic_4packets(file_name): # parameters Sim.scheduler.reset() # setup network net = Network(file_name) # setup routes n1 = net.get_node('n1') n2 = net.get_node('n2') n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0]) n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0]) # setup app d = DelayHandler() net.nodes['n2'].add_protocol(protocol="delay", handler=d) # send three packets # packet 1 p = Packet(destination_address=n2.get_address('n1'), ident=1, protocol='delay', length=1000) Sim.scheduler.add(delay=0, event=p, handler=n1.send_packet) # packet 2 p = Packet(destination_address=n2.get_address('n1'), ident=2, protocol='delay', length=1000) Sim.scheduler.add(delay=0, event=p, handler=n1.send_packet) # packet 3 p = Packet(destination_address=n2.get_address('n1'), ident=3, protocol='delay', length=1000) Sim.scheduler.add(delay=0, event=p, handler=n1.send_packet) # send 4th delayed packet p = Packet(destination_address=n2.get_address('n1'), ident=4, protocol='delay', length=1000) Sim.scheduler.add(delay=2, event=p, handler=n1.send_packet) # run the simulation Sim.scheduler.run()
def router(): # parameters Sim.scheduler.reset() h1 = Host('h1', default_gateway=IPAddress('1.1.1.1')) h1.add_protocol('arp', ARPApp(h1)) h2 = Host('h2', default_gateway=IPAddress('1.1.1.1')) h2.add_protocol('arp', ARPApp(h2)) h3 = Host('h3', default_gateway=IPAddress('2.2.2.1')) h3.add_protocol('arp', ARPApp(h3)) h4 = Host('h4', default_gateway=IPAddress('2.2.2.1')) h4.add_protocol('arp', ARPApp(h4)) s1 = Switch('s1') s2 = Switch('s2') n1 = Node('n1') n1.add_protocol('arp', ARPApp(n1)) n2 = Node('n2') n2.add_protocol('arp', ARPApp(n2)) h1.add_link( Link(mac_address='AA-AA-AA-AA', address=IPAddress('1.1.1.2'), prefix_len=28, startpoint=h1, endpoint=s1)) s1.add_link(Link(startpoint=s1, endpoint=h1)) h2.add_link( Link(mac_address='BB-BB-BB-BB', address=IPAddress('1.1.1.5'), prefix_len=28, startpoint=h2, endpoint=s1)) s1.add_link(Link(startpoint=s1, endpoint=h2)) n1.add_link( Link(mac_address='CC-CC-CC-CC', address=IPAddress('1.1.1.1'), prefix_len=28, startpoint=n1, endpoint=s1)) s1.add_link(Link(startpoint=s1, endpoint=n1)) h3.add_link( Link(mac_address='DD-DD-DD-DD', address=IPAddress('2.2.2.2'), prefix_len=28, startpoint=h3, endpoint=s2)) s2.add_link(Link(startpoint=s2, endpoint=h3)) h4.add_link( Link(mac_address='EE-EE-EE-EE', address=IPAddress('2.2.2.5'), prefix_len=28, startpoint=h4, endpoint=s2)) s2.add_link(Link(startpoint=s2, endpoint=h4)) n2.add_link( Link(mac_address='00-00-00-00', address=IPAddress('2.2.2.1'), prefix_len=28, startpoint=n2, endpoint=s2)) s2.add_link(Link(startpoint=s2, endpoint=n2)) n1.add_link( Link(mac_address='11-11-11-11', address=IPAddress('3.3.3.1'), prefix_len=30, startpoint=n1, endpoint=n2)) n2.add_link( Link(mac_address='22-22-22-22', address=IPAddress('3.3.3.2'), prefix_len=30, startpoint=n2, endpoint=n1)) n1.add_forwarding_entry(Subnet(IPAddress('2.2.2.0'), 28), n1.get_link('n2'), IPAddress('3.3.3.2')) n2.add_forwarding_entry(Subnet(IPAddress('1.1.1.0'), 28), n2.get_link('n1'), IPAddress('3.3.3.1')) p = Packet(destination_address=h2.get_address('s1'), length=100) Sim.scheduler.add(1.1, event=p, handler=h1.send_packet) p = Packet(destination_address=h3.get_address('s2'), length=100) Sim.scheduler.add(1.2, event=p, handler=h1.send_packet) # run the simulation Sim.scheduler.run()
def fifteen(): # parameters Sim.scheduler.reset() # setup network net = Network('../networks/fifteen-nodes.txt', DVRouter) for n in net.nodes: net.nodes[n].add_protocol(protocol="dvrouting", handler=RoutingApp(net.nodes[n])) Sim.scheduler.add(delay=40, event=None, handler=end_simulation) # send packet from n9 to n10 and from n10 to n9 p = Packet(destination_address=net.nodes['n10'].get_address('n1'), length=100) Sim.scheduler.add(20.0, event=p, handler=net.nodes['n9'].send_packet) p = Packet(destination_address=net.nodes['n9'].get_address('n6'), length=100) Sim.scheduler.add(20.1, event=p, handler=net.nodes['n10'].send_packet) # send packet from n9 to n11 and from n11 to n9 p = Packet(destination_address=net.nodes['n11'].get_address('n4'), length=100) Sim.scheduler.add(20.2, event=p, handler=net.nodes['n9'].send_packet) p = Packet(destination_address=net.nodes['n9'].get_address('n6'), length=100) Sim.scheduler.add(20.3, event=p, handler=net.nodes['n11'].send_packet) # send packet from n9 to n12 and from n12 to n9 p = Packet(destination_address=net.nodes['n12'].get_address('n5'), length=100) Sim.scheduler.add(20.4, event=p, handler=net.nodes['n9'].send_packet) p = Packet(destination_address=net.nodes['n9'].get_address('n6'), length=100) Sim.scheduler.add(20.5, event=p, handler=net.nodes['n12'].send_packet) # send packet from n9 to n13 and from n13 to n9 p = Packet(destination_address=net.nodes['n13'].get_address('n5'), length=100) Sim.scheduler.add(20.6, event=p, handler=net.nodes['n9'].send_packet) p = Packet(destination_address=net.nodes['n9'].get_address('n6'), length=100) Sim.scheduler.add(20.7, event=p, handler=net.nodes['n13'].send_packet) # send packet from n9 to n14 and from n14 to n9 p = Packet(destination_address=net.nodes['n14'].get_address('n2'), length=100) Sim.scheduler.add(20.8, event=p, handler=net.nodes['n9'].send_packet) p = Packet(destination_address=net.nodes['n9'].get_address('n6'), length=100) Sim.scheduler.add(20.9, event=p, handler=net.nodes['n14'].send_packet) # send packet from n7 to n15 and from n15 to n7 p = Packet(destination_address=net.nodes['n15'].get_address('n14'), length=100) Sim.scheduler.add(21.0, event=p, handler=net.nodes['n7'].send_packet) p = Packet(destination_address=net.nodes['n7'].get_address('n8'), length=100) Sim.scheduler.add(21.1, event=p, handler=net.nodes['n15'].send_packet) # at t = 22, bring down link between n2 and n8 Sim.scheduler.add(22, event=None, handler=net.nodes['n8'].get_link('n2').down) Sim.scheduler.add(22, event=None, handler=net.nodes['n2'].get_link('n8').down) # send packet from n7 to n15 and from n15 to n7 - these should fail p = Packet(destination_address=net.nodes['n15'].get_address('n14'), length=100) Sim.scheduler.add(23.1, event=p, handler=net.nodes['n7'].send_packet) p = Packet(destination_address=net.nodes['n7'].get_address('n8'), length=100) Sim.scheduler.add(23.2, event=p, handler=net.nodes['n15'].send_packet) # send packet from n7 to n15 and from n15 to n7 - these should get there through new paths p = Packet(destination_address=net.nodes['n15'].get_address('n14'), length=100) Sim.scheduler.add(30.1, event=p, handler=net.nodes['n7'].send_packet) p = Packet(destination_address=net.nodes['n7'].get_address('n8'), length=100) Sim.scheduler.add(30.2, event=p, handler=net.nodes['n15'].send_packet) # run the simulation Sim.scheduler.run()