예제 #1
0
def config_network():

    program = "simple.p4"

    topo = LinearTopo(3)
    link = custom(TCLink, bw=10)
    net = P4Mininet(program=program, topo=topo, link=link)
    net.start()

    print "Switch: Configuration"
    sw = net.get("s1")

    print "Configuring switch: " + "s1"
    sw.command("table_set_default ipv4_lpm drop")
    sw.command(
        "table_add ipv4_lpm ipv4_forward 10.0.0.1/32 => 00:00:0a:00:01:01 1")
    sw.command(
        "table_add ipv4_lpm ipv4_forward 10.0.0.2/32 => 00:00:00:00:02:02 2")
    sw.command(
        "table_add ipv4_lpm ipv4_forward 10.0.0.3/32 => 00:00:00:00:03:03 3")

    #   eth0     1  2     eth0
    # |h1|-------|s1|-------|h2|

    return net
예제 #2
0
def config_network():

    program = "simple_int.p4"

    topo = LinearTopo()
    link = custom(TCLink, bw=10)
    net = P4Mininet(program=program, topo=topo, link=link)
    net.start()

    print "Switch: Configuration"

    for i in range(1, 3):

        sw = net.get("s%d" % i)
        fname = "cfg/" + "s%d" % i + "-commands.txt"
        print("Configuring switch: " + "s%d" % i)
        print("Config filename: " + fname)
        with open(fname, "r") as file:
            cmd = file.read()

        # sw.command('table_add ipv4_lpm set_nhop 10.0.0.10/32 => 10.0.0.10 1')
        # sw.command('set_crc16_parameters calc 0x002d 0x0 0x0 false false')
        sw.command(cmd)


    return net
예제 #3
0
def config_network():

    program = "simple.p4"

    topo = LinearTopo(4)
    link = custom(TCLink, bw=10)
    net = P4Mininet(program=program, topo=topo, link=link)
    net.start()

    print "Switch: Configuration"
    sw = net.get("s1")

    print "Configuring switch: " + "s1"
    sw.command("table_set_default ipv4_class drop")
    sw.command("table_add ipv4_class ipv4_forward 0 => 00:00:00:00:02:02 2")
    sw.command("table_add ipv4_class ipv4_forward 1 => 00:00:00:00:03:03 3")
    sw.command("table_add ipv4_class ipv4_forward 2 => 00:00:00:00:04:04 4")

    return net
예제 #4
0
def config_network():

    program = "simple.p4"

    topo = LinearTopo(3)
    link = custom(TCLink, bw=10)
    net = P4Mininet(program=program, topo=topo, link=link)
    net.start()

    print "Switch: Configuration"
    sw = net.get("s1")

    print "Configuring switch: " + "s1"
    sw.command("table_set_default ipv4_lpm drop")
    sw.command("table_add ipv4_lpm ipv4_forward 0->7  => 2 1")
    sw.command("table_add ipv4_lpm ipv4_forward 8->20  => 3 2")
    #sw.command("table_add ipv4_lpm ipv4_forward 8->20 => 00:00:00:00:03:03 3")

    return net
예제 #5
0
def config_network():

    program = "simple.p4"

    topo = LinearTopo(2, [1, 8])
    link = custom(TCLink, bw=10)
    net = P4Mininet(program=program, topo=topo, link=link)
    net.start()

    print "Switch: Configuration"
    sw = net.get("s1")

    print "Configuring switch: " + "s1"

    fname = "cfg/s1-command.txt"
    with open(fname, "r") as file:
        cmd = file.read()

    sw.command(cmd)

    return net
예제 #6
0
def config_network():

    program = "simple.p4"

    topo = LinearTopo(2, [1, 8])
    link = custom(TCLink, bw=10)
    net = P4Mininet(program=program, topo=topo, link=link)
    net.start()

    print "Switch: Configuration"
    sw = net.get("s1")

    print "Configuring switch: " + "s1"
    #                                               ether      proto       flags      totalLen      dstPort    ctrl       ecn      UdpdstPort
    #sw.command("table_add table action  0 -> 132   0 -> 255  0 -> 3     0 -> 1500     0 -> 23    0 -> 63    0 -> 3   0 -> 0    =>  2 1")
    fname = "cfg/s1-command.txt"
    with open(fname, "r") as file:
        cmd = file.read()

    sw.command(cmd)

    return net
예제 #7
0
def config_network():

    program = "simple.p4"

    topo = LinearTopo(2, [1, 8])
    link = custom(TCLink, bw=10)
    net = P4Mininet(program=program, topo=topo, link=link)
    net.start()

    print "Switch: Configuration"
    sw = net.get("s1")

    print "Configuring switch: " + "s1"
    sw.command("table_set_default ipv4_class drop")
    sw.command("table_add ipv4_class ipv4_forward 0 => 2")
    sw.command("table_add ipv4_class ipv4_forward 1 => 3")
    sw.command("table_add ipv4_class ipv4_forward 2 => 4")
    sw.command("table_add ipv4_class ipv4_forward 3 => 5")
    sw.command("table_add ipv4_class ipv4_forward 4 => 6")
    sw.command("table_add ipv4_class ipv4_forward 5 => 7")
    sw.command("table_add ipv4_class ipv4_forward 6 => 8")
    sw.command("table_add ipv4_class ipv4_forward 7 => 9")

    return net
예제 #8
0
from p4app import P4Mininet
from my_topo import SingleSwitchTopo
from controller import MacLearningController

# Add three hosts. Port 1 (h1) is reserved for the CPU.
N = 3

topo = SingleSwitchTopo(N)
net = P4Mininet(program='l2switch.p4', topo=topo, auto_arp=False)
net.start()

# Add a mcast group for all ports (except for the CPU port)
bcast_mgid = 1
sw = net.get('s1')
sw.addMulticastGroup(mgid=bcast_mgid, ports=range(2, N + 1))

# Send MAC bcast packets to the bcast multicast group
sw.insertTableEntry(
    table_name='MyIngress.fwd_l2',
    match_fields={'hdr.ethernet.dstAddr': ["ff:ff:ff:ff:ff:ff"]},
    action_name='MyIngress.set_mgid',
    action_params={'mgid': bcast_mgid})

# Start the MAC learning controller
cpu = MacLearningController(sw)
cpu.start()

h2, h3 = net.get('h2'), net.get('h3')

print h2.cmd('arping -c1 10.0.0.3')
예제 #9
0
파일: main.py 프로젝트: lazy-val-b/pwospf
from p4app import P4Mininet
from mininet.topo import Topo
from myTopo import MyTopo
from controller import PWOSPFController
import time


N = 3 # Number of switches
K = 3 # Number of hosts per switch
mgid = 10 # Multicast ID

topo = MyTopo(N, K)
net = P4Mininet(program='PWOSPF.p4', topo=topo)
net.start()

s1, s2, s3 = net.get('s1'), net.get('s2'), net.get('s3')
h1, h2, h3, h4, c1, c2, c3 = net.get('h1'), net.get('h2'), net.get('h3'), net.get('h4'), net.get('c1'), net.get('c2'), net.get('c3')

def printLinkInfos():
        for link in topo.links():
                print('###############')
                print('LINK')
                print(link)
                print(link[0] + '<---->' + link[1])
                print(topo.linkInfo(link[0], link[1]))
                print(link[0])
                print(topo.nodeInfo(link[0]))
                print(link[1])
                print(topo.nodeInfo(link[1]))

# Start a controller for each switch
예제 #10
0
from p4app import P4Mininet
from my_topo import SingleSwitchTopo, TriangleSwitchesTopo, SquareSwitchesTopo
from controller import RouterController
import time

topo = SquareSwitchesTopo()
net = P4Mininet(program='router.p4', topo=topo, auto_arp=False)
net.start()

sw1, r1, h1 = net.get('s1'), net.get('cpu1'), net.get('h1')
sw2, r2, h2 = net.get('s2'), net.get('cpu2'), net.get('h2')
sw3, r3, h3 = net.get('s3'), net.get('cpu3'), net.get('h3')
sw4, r4, h4 = net.get('s4'), net.get('cpu4'), net.get('h4')

# Define interfaces
s1_intfs = [('100.0.1.1','255.255.255.0',3,1),
            ('100.0.1.2','255.255.255.0',3,2),
            ('100.0.1.3','255.255.255.0',3,3),
            ('100.0.1.4','255.255.255.0',3,4),
            ('100.0.1.5','255.255.255.0',3,5)]
s2_intfs = [('100.0.2.1','255.255.255.0',3,1),
            ('100.0.2.2','255.255.255.0',3,2),
            ('100.0.2.3','255.255.255.0',3,3),
            ('100.0.2.4','255.255.255.0',3,4)]
s3_intfs = [('100.0.3.1','255.255.255.0',3,1),
            ('100.0.3.2','255.255.255.0',3,2),
            ('100.0.3.3','255.255.255.0',3,3),
            ('100.0.3.4','255.255.255.0',3,4),
            ('100.0.3.5','255.255.255.0',3,5)]
s4_intfs = [('100.0.4.1','255.255.255.0',3,1),
            ('100.0.4.2','255.255.255.0',3,2),
예제 #11
0
from mininet.cli import CLI
from p4app import P4Mininet
from mininet.topo import SingleSwitchTopo

if len(sys.argv) > 1:
    if sys.argv[1] == 'compile':
        try:
            P4Program('substring_match.p4').compile()
        except Exception as e:
            print(e)
            sys.exit(1)
        sys.exit(0)


topo = SingleSwitchTopo(1)
net = P4Mininet(program='substring_match.p4', topo=topo)
net.start()



sw = net.get('s1')

for i in range(0,8):
    sw.insertTableEntry(table_name='MyIngress.get_strA_char',
                        match_fields={'hdr.internal_header.iterator_r': i},
                        action_name='MyIngress.get_strA_char'+str(i)
                        )

for i in range(0,8):
    sw.insertTableEntry(table_name='MyIngress.get_strB_char',
                        match_fields={'hdr.internal_header.iterator_l': i},
예제 #12
0
def inlineTest():
    opo = DoubleSwitchTopo(N)
    net = P4Mininet(program='switch.p4', topo=topo, auto_arp=False)
    # net = P4Mininet(program='switch.p4', topo=topo, auto_arp=False)
    net.start()

    # Add a mcast group for all ports (except for the CPU port)
    bcast_mgid = 1
    sw1 = net.get('s1a')
    sw2 = net.get('s2a')
    sw3 = net.get('s3a')
    sw1.addMulticastGroup(mgid=bcast_mgid, ports=range(2, N+1))
    sw2.addMulticastGroup(mgid=bcast_mgid, ports=range(2, N+1))
    sw3.addMulticastGroup(mgid=bcast_mgid, ports=range(2, N+1))


    # Send MAC bcast packets to the bcast multicast group
    sw1.insertTableEntry(table_name='MyIngress.fwd_l2',
                        match_fields={'hdr.ethernet.dstAddr': [
                            "ff:ff:ff:ff:ff:ff"]},
                        action_name='MyIngress.set_mgid',
                        action_params={'mgid': bcast_mgid})
    sw2.insertTableEntry(table_name='MyIngress.fwd_l2',
                        match_fields={'hdr.ethernet.dstAddr': [
                            "ff:ff:ff:ff:ff:ff"]},
                        action_name='MyIngress.set_mgid',
                        action_params={'mgid': bcast_mgid})
    sw3.insertTableEntry(table_name='MyIngress.fwd_l2',
                        match_fields={'hdr.ethernet.dstAddr': [
                            "ff:ff:ff:ff:ff:ff"]},
                        action_name='MyIngress.set_mgid',
                        action_params={'mgid': bcast_mgid})
    # Adding boradcast for PWOSPF
    sw1.insertTableEntry(table_name='MyIngress.ipv4_lpm',
                        match_fields={'hdr.ipv4.dstAddr': ["224.0.0.5", 32]},
                        action_name='MyIngress.set_mgid',
                        action_params={'mgid': bcast_mgid})

    sw2.insertTableEntry(table_name='MyIngress.ipv4_lpm',
                        match_fields={'hdr.ipv4.dstAddr': ["224.0.0.5", 32]},
                        action_name='MyIngress.set_mgid',
                        action_params={'mgid': bcast_mgid})
    sw3.insertTableEntry(table_name='MyIngress.ipv4_lpm',
                        match_fields={'hdr.ipv4.dstAddr': ["224.0.0.5", 32]},
                        action_name='MyIngress.set_mgid',
                        action_params={'mgid': bcast_mgid})

    # Start the MAC learning controller
    cpu1 = MacLearningController(sw1, "10.0.0.0", "192.168.0.0")
    cpu2 = MacLearningController(sw2, "10.0.1.0", "192.168.0.0")
    cpu3 = MacLearningController(sw3, "10.0.2.0", "192.168.0.0")
    cpu1.start()
    cpu2.start()
    cpu3.start()

    h2, h3, h4, h5 = net.get('h2'), net.get('h3'), net.get('h4'), net.get('h5')

    # time.sleep(1)
    print h2.cmd('arping -c1 10.0.0.3')
    print h3.cmd('arping -c1 10.0.1.2')

    time.sleep(5)
    # print h3.cmd('arping -c1 10.0.0.3')
    print h3.cmd('ping -c1 10.0.0.3')


    cpu1.join()
    cpu2.join()
    cpu3.join()


    # print h2.cmd('arping -c1 10.0.0.3')
    # print h2.cmd('arping -c1 10.0.0.3')
    # print h3.cmd('arping -c1 10.0.1.2')

    # print h3.cmd('ping -t 1 -c1 10.0.0.2')
    # print h2.cmd('ping -c1 10.0.0.3')

    # These table entries were added by the CPU:
    sw1.printTableEntries()
    sw2.printTableEntries()
    sw3.printTableEntries()
    print("Packet Counts")
    packet1, byte = sw1.readCounter("packet_counter", 0)
    packet2, byte = sw2.readCounter("packet_counter", 0)
    packet3, byte = sw3.readCounter("packet_counter", 0)
    print("IP = SW1: %d , SW2: %d , SW3: %d" % (packet1, packet2, packet3))
    packet1, byte = sw1.readCounter("packet_counter", 1)
    packet2, byte = sw2.readCounter("packet_counter", 1)
    packet3, byte = sw3.readCounter("packet_counter", 1)
    print("ARP = SW1: %d , SW2: %d , SW3: %d" % (packet1, packet2, packet3))
    packet1, byte = sw1.readCounter("packet_counter", 2)
    packet2, byte = sw2.readCounter("packet_counter", 2)
    packet3, byte = sw3.readCounter("packet_counter", 2)
    print("CPU = SW1: %d , SW2: %d , SW3: %d" % (packet1, packet2, packet3))
예제 #13
0
from p4app import P4Mininet

from mytopo import CustomTopo

topo = CustomTopo()
net = P4Mininet(program="router.p4", topo=topo)
net.start()

h1, h2, h3, h4, h5 = net.get("h1"), net.get("h2"), net.get("h3"), net.get("h4"), net.get("h5")
s1, s2, s3, s4 = net.get("s1"), net.get("s2"), net.get("s3"), net.get("s4")

def addForwardingRule(sw, host, port):
    sw.insertTableEntry(table_name="MyIngress.ipv4_lpm",
                        match_fields={'hdr.ipv4.dstAddr': ["10.0.0.%d" % host, 32]},
                        action_name="MyIngress.ipv4_forward",
                        action_params={'dstAddr': "00:00:00:00:00:0%d" % host,
                                       'port': port}
                        )

def addFirewallRule(sw, inPort, outPort, direction):
    sw.insertTableEntry(table_name ="MyIngress.check_ports",
                        match_fields = { "standard_metadata.ingress_port": inPort, 
                                        "standard_metadata.egress_spec": outPort},
                        action_name = "MyIngress.set_direction",
                        action_params = {"dir": direction}
                        )

# Set Forwarding for S1
for host, port in [(1,1), (2,2), (3,2), (4,3), (5, 3)]:
    addForwardingRule(s1, host, port)
예제 #14
0
# p4app Driver code, for starting a vanilla topology for debugging
# usage: p4app run AES.p4app
import os
os.environ["GC_INITIAL_HEAP_SIZE"]="20G"
os.environ["GC_MAX_HEAP_SIZE"]="50G"

from p4app import P4Mininet
from mininet.topo import SingleSwitchTopo

topo = SingleSwitchTopo(2)
net = P4Mininet(program='AES.p4', topo=topo)
net.start()

print("Minined started")

from mininet.cli import CLI
CLI(net)
예제 #15
0
파일: main.py 프로젝트: p4lang/hackathons
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#     http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import random

from p4app import P4Mininet
from mininet.topo import SingleSwitchTopo

topo = SingleSwitchTopo(2)
net = P4Mininet(program='registers.p4', topo=topo)
net.start()

h1 = net.get('h1')
h2 = net.get('h2')

out = h1.cmd('./send.py 10.0.0.2 write fooo')
print('sent a write packet with payload *fooo* and received: %s' % out)
assert out.strip() == "fooo"

out = h1.cmd('./send.py 10.0.0.2 read barr')
print('sent a read packet with payload *barr* and received: %s' % out)
assert out.strip() == "fooo"

out = h1.cmd('./send.py 10.0.0.2 read bazz')
print('sent a write packet with payload *bazz* and received: %s' % out)
예제 #16
0
from camus_topo import SingleSwitchTopo, FatTreeTopo, getPort
import subprocess
import sys
import time

K = 4

itch_app = CamusApp('spec.p4', ingress_name='MyIngress')
itch_app.generateQueryPipeline('itch_camus.p4')

topo = FatTreeTopo(K)
topo.subscribe('h_0_0_0', 'add_order.shares = 1')
topo.subscribe('h_0_0_1', 'add_order.price = 2')
topo.subscribe('h_2_1_0', 'add_order.stock = "GOOGL"')

net = P4Mininet(program='itch.p4', topo=topo)
net.start()

# Core switches should forward all traffic down
for core in range(topo.core_count):
    sw = net.get('cs_%d' % core)
    sw.insertTableEntry(table_name='MyIngress.packet_direction',
                        default_action=True,
                        action_name='MyIngress.direction_is_down')

non_core_switches = [(t, pod, x) for t in ['as', 'es']
                     for pod in range(topo.pod_count)
                     for x in range(topo.edge_count / topo.pod_count)]

# Add rules for detecting whether a packet is going up or down the tree
for sw_type, pod, x in non_core_switches:
예제 #17
0
from p4app import P4Mininet
from mininet.topo import SingleSwitchTopo
import sys
import time

topo = SingleSwitchTopo(2)
net = P4Mininet(program='cache.p4', topo=topo)
net.start()

s1, h1, h2 = net.get('s1'), net.get('h1'), net.get('h2')

# TODO Populate IPv4 forwarding table

# TODO Populate the cache table


# Now, we can test that everything works

# Start the server with some key-values
server = h1.popen('./server.py 1=11 2=22', stdout=sys.stdout, stderr=sys.stdout)
time.sleep(0.4) # wait for the server to be listenning

out = h2.cmd('./client.py 10.0.0.1 1') # expect a resp from server
assert out.strip() == "11"
out = h2.cmd('./client.py 10.0.0.1 1') # expect a value from switch cache (registers)
assert out.strip() == "11"
out = h2.cmd('./client.py 10.0.0.1 2') # resp from server
assert out.strip() == "22"
out = h2.cmd('./client.py 10.0.0.1 3') # from switch cache (table)
assert out.strip() == "33"
out = h2.cmd('./client.py 10.0.0.1 123') # resp not found from server
예제 #18
0
def finalTest():
    MAX_PORT = 10
    total_sim_time = 20

    topo = FiveSwitchTopo()
    net = P4Mininet(program='switch.p4', topo=topo, auto_arp=False)
    net.start()

    bcast_mgid = 1
    switches = []
    cpus = []
    hosts = []
    for i in range(5):
        print("SWITCH %d" % (i+1))
        switches.append(net.get("s%da" % (i+1)))
        cpus.append(MacLearningController(switches[i],"10.0.%d.0" % (i), "192.168.0.0", "%02d:00:00:00:00:00" % (i+1)))
        
        switches[i].addMulticastGroup(mgid=bcast_mgid, ports=range(2, MAX_PORT+1))
        switches[i].insertTableEntry(table_name='MyIngress.fwd_l2', match_fields={'hdr.ethernet.dstAddr': [
                                     "ff:ff:ff:ff:ff:ff"]}, action_name='MyIngress.set_mgid', action_params={'mgid': bcast_mgid})
        switches[i].insertTableEntry(table_name='MyIngress.ipv4_lpm', match_fields={'hdr.ipv4.dstAddr': [
                                     "224.0.0.5", 32]}, action_name='MyIngress.set_mgid', action_params={'mgid': bcast_mgid})
        
        cpus[i].start()
        print("STARTED SWITCH %d" % (i+1))

    print("STARTED CPUS")
    for i in range(10):
        hosts.append(net.get('h%d' % (i + 1)))
    print("GOT HOSTS")
    for i in range(5):
        print (hosts[i*2]).cmd("arping -c1 10.0.%d.%d" % (i,((i+1)*2)))
    print("ARPPED")
    time.sleep(total_sim_time)
    print("PINGS SHOULD SUCCEED")
    print (hosts[0]).cmd("ping -c1 10.0.1.3")
    print (hosts[0]).cmd("ping -c1 10.0.2.5")
    print (hosts[0]).cmd("ping -c1 10.0.3.7")
    print (hosts[0]).cmd("ping -c1 10.0.4.9")
    print (hosts[4]).cmd("ping -c1 10.0.0.1")
    print("PINGS SHOULD FAIL")
    print (hosts[0]).cmd("ping -c1 10.0.1.2")
    print (hosts[0]).cmd("ping -c1 10.0.5.3")
    print (hosts[0]).cmd('ping -t 1 -c1 10.0.0.2')
    cpu_temp = cpus.pop(0)

    cpu_temp.join()
    print("TESTING ROUTER FAILURE")
    time.sleep(30)

    print("FINISHED SLEEPING")
    for i in cpus:
        i.join()

    for i in switches:
        i.printTableEntries()

    print("Packet Counts For Switches")
    count = 0
    for i in switches:
        print("Switch %d" % (count + 1))
        packet1, byte = i.readCounter("packet_counter", 0)
        packet2, byte = i.readCounter("packet_counter", 1)
        packet3, byte = i.readCounter("packet_counter", 2)

        print("IP = %d , ARP =  %d , CPU = %d" % (packet1, packet2, packet3))
        count += 1