Commit ad7e1043 authored by cedgar's avatar cedgar
Browse files

rsvp solution out

parent 4a60099a
"""
Inspired in the mininet CLI.
"""
import subprocess
from cmd import Cmd
from os import isatty
from select import poll, POLLIN
import select
import sys
import os
import atexit
class RSVPCLI( Cmd ):
"Simple command-line interface to talk to nodes."
prompt = 'rsvp-menu> '
def __init__( self, controller, stdin=sys.stdin, script=None,
*args, **kwargs ):
self.controller = controller
# Local variable bindings for py command
self.locals = { 'controller': controller }
# Attempt to handle input
self.inPoller = poll()
self.inPoller.register( stdin )
self.inputFile = script
Cmd.__init__( self, *args, stdin=stdin, **kwargs )
self.hello_msg()
if self.inputFile:
self.do_source( self.inputFile )
return
self.initReadline()
self.run()
readlineInited = False
def hello_msg(self):
"""
"""
print '======================================================================'
print 'Welcome to the RSVP CLI'
print '======================================================================'
print 'You can now make reservations for your hosts in the network.'
print 'To add a reservation run:'
print 'add_reservation <src> <dst> <duration> <bw> <priority>'
print ''
print 'To delete a reservation run: '
print 'del_reservation <src> <dst>'
print ''
@classmethod
def initReadline( cls ):
"Set up history if readline is available"
# Only set up readline once to prevent multiplying the history file
if cls.readlineInited:
return
cls.readlineInited = True
try:
from readline import ( read_history_file, write_history_file,
set_history_length )
except ImportError:
pass
else:
history_path = os.path.expanduser( '~/.rsvp_controller_history' )
if os.path.isfile( history_path ):
read_history_file( history_path )
set_history_length( 1000 )
atexit.register( lambda: write_history_file( history_path ) )
def run( self ):
"Run our cmdloop(), catching KeyboardInterrupt"
while True:
try:
if self.isatty():
subprocess.call( 'stty echo sane intr ^C',shell=True)
self.cmdloop()
break
except KeyboardInterrupt:
# Output a message - unless it's also interrupted
# pylint: disable=broad-except
try:
print( '\nInterrupt\n' )
except Exception:
pass
# pylint: enable=broad-except
def emptyline( self ):
"Don't repeat last command when you hit return."
pass
def getLocals( self ):
"Local variable bindings for py command"
self.locals.update( self.mn )
return self.locals
helpStr = (
'To add a reservation run:\n'
'add_reservation <src> <dst> <duration> <bw> <priority>\n'
'\n'
'To delete a reservation run: \n'
'del_reservation <src> <dst>\n'
''
)
def do_help( self, line ):
"Describe available CLI commands."
Cmd.do_help( self, line )
if line == '':
print( self.helpStr )
def do_exit( self, _line ):
"Exit"
assert self # satisfy pylint and allow override
return 'exited by user command'
def do_quit( self, line ):
"Exit"
return self.do_exit( line )
def do_EOF( self, line ):
"Exit"
print( '\n' )
return self.do_exit( line )
def isatty( self ):
"Is our standard input a tty?"
return isatty( self.stdin.fileno() )
"""
RSVP COMMANDS
"""
def do_add_reservation(self, line=""):
"""Adds a reservation using mpls.
add_reservation <src> <dst> <duration> <bw> <priority>
"""
# geta rguments
args = line.split()
# defaults
duration = 9999
bw = 1
priority = 1
if len(args) < 2:
print("Not enough args!")
return
elif len(args) == 2:
src, dst = args
elif len(args) == 3:
src, dst, duration = args
elif len(args) == 4:
src, dst, duration, bw = args
elif len(args) == 5:
src, dst, duration, bw, priority = args
else:
print("Too many args!")
return
# casts
duration = float(duration)
bw = float(bw)
priority = int(priority)
# add entry
res = self.controller.add_reservation(src, dst, duration, bw, priority)
def do_del_reservation(self, line=""):
"""Deletes a reservation"""
# gets arguments
args = line.split()
if len(args) < 2:
print("Not enough args!")
return
elif len(args) == 2:
src, dst = args[:2]
else:
print("Too many args!")
return
# add entry
res = self.controller.del_reservation(src, dst)
def do_del_all_reservations(self, line =""):
"""Deletes all the reservations"""
res = self.controller.del_all_reservations()
def do_print_reservations(self, line = ""):
"""Prints current reservations"""
print("Current Reservations:")
print("---------------------")
for i, ((src, dst), data) in enumerate(self.controller.current_reservations.items()):
print("{:>3} {}->{} : {}, bw:{}, priority:{}, timeout:{}".format(i, src, dst,
"->".join(data['path']), data['bw'], data['priority'], data["timeout"] ))
def do_print_link_capacity(self, line=""):
"""Prints current link capacities"""
print("Current Link Capacities:")
print("---------------------")
for edge, bw in self.controller.links_capacity.items():
print("{} -> {}".format(edge, bw))
\ No newline at end of file
{
"program": "rsvp.p4",
"switch": "simple_switch",
"compiler": "p4c",
"options": "--target bmv2 --arch v1model --std p4-16",
"switch_cli": "simple_switch_CLI",
"cli": true,
"pcap_dump": false,
"enable_log": false,
"topo_module": {
"file_path": "",
"module_name": "p4utils.mininetlib.apptopo",
"object_name": "AppTopoStrategies"
},
"controller_module": null,
"topodb_module": {
"file_path": "",
"module_name": "p4utils.utils.topology",
"object_name": "Topology"
},
"mininet_module": {
"file_path": "",
"module_name": "p4utils.mininetlib.p4net",
"object_name": "P4Mininet"
},
"topology": {
"assignment_strategy": "l3",
"default_bw": 10,
"links": [["h1", "s1"],
["h2", "s1"],
["s1", "s2"],
["s1", "s3"],
["s2", "s3"],
["h3", "s2"],
["h4", "s2"],
["h5", "s3"],
["h6", "s3"]],
"hosts": {
"h1": {
},
"h2": {
},
"h3": {
},
"h4": {
},
"h5": {
},
"h6": {
}
},
"switches": {
"s1": {
},
"s2": {
},
"s3": {
}
}
}
}
{
"program": "rsvp.p4",
"switch": "simple_switch",
"compiler": "p4c",
"options": "--target bmv2 --arch v1model --std p4-16",
"switch_cli": "simple_switch_CLI",
"cli": true,
"pcap_dump": false,
"enable_log": true,
"topo_module": {
"file_path": "",
"module_name": "p4utils.mininetlib.apptopo",
"object_name": "AppTopoStrategies"
},
"controller_module": null,
"topodb_module": {
"file_path": "",
"module_name": "p4utils.utils.topology",
"object_name": "Topology"
},
"mininet_module": {
"file_path": "",
"module_name": "p4utils.mininetlib.p4net",
"object_name": "P4Mininet"
},
"topology": {
"assignment_strategy": "l3",
"default_bw" : 10,
"links": [["h1", "s1"],
["h2", "s1"],
["s1", "s2"],
["s1", "s3"],
["s2", "s4"],
["s3", "s4"],
["s2", "s5"],
["s3", "s6"],
["s4", "s5"],
["s4", "s6"],
["s5", "s7"],
["s6", "s7"],
["s7", "h5"],
["s7", "h6"],
["h3", "s3"],
["h4", "s5"]
],
"hosts": {
"h1": {
},
"h2": {
},
"h3": {
},
"h4": {
},
"h5": {
},
"h6": {
}
},
"switches": {
"s1": {
},
"s2": {
},
"s3": {
},
"s4": {
},
"s5": {
},
"s6": {
},
"s7": {
}
}
}
}
/* -*- P4_16 -*- */
#include <core.p4>
#include <v1model.p4>
const bit<16> TYPE_IPV4 = 0x0800;
const bit<16> TYPE_MPLS = 0x8847;
#define CONST_MAX_LABELS 128
#define CONST_MAX_MPLS_HOPS 8
/*************************************************************************
*********************** H E A D E R S ***********************************
*************************************************************************/
typedef bit<9> egressSpec_t;
typedef bit<48> macAddr_t;
typedef bit<32> ip4Addr_t;
typedef bit<20> label_t;
header ethernet_t {
macAddr_t dstAddr;
macAddr_t srcAddr;
bit<16> etherType;
}
header mpls_t {
bit<20> label;
bit<3> exp;
bit<1> s;
bit<8> ttl;
}
header ipv4_t {
bit<4> version;
bit<4> ihl;
bit<8> diffserv;
bit<16> totalLen;
bit<16> identification;
bit<3> flags;
bit<13> fragOffset;
bit<8> ttl;
bit<8> protocol;
bit<16> hdrChecksum;
ip4Addr_t srcAddr;
ip4Addr_t dstAddr;
}
struct metadata {
bit<2> meter_color;
}
struct headers {
ethernet_t ethernet;
mpls_t[CONST_MAX_MPLS_HOPS] mpls;
ipv4_t ipv4;
}
/*************************************************************************
*********************** P A R S E R ***********************************
*************************************************************************/
parser MyParser(packet_in packet,
out headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
state start {
transition parse_ethernet;
}
state parse_ethernet {
packet.extract(hdr.ethernet);
transition select(hdr.ethernet.etherType) {
TYPE_MPLS: parse_mpls;
TYPE_IPV4: parse_ipv4;
default: accept;
}
}
state parse_mpls {
packet.extract(hdr.mpls.next);
transition select(hdr.mpls.last.s) {
1: parse_ipv4;
default: parse_mpls;
}
}
state parse_ipv4 {
packet.extract(hdr.ipv4);
transition accept;
}
}
/*************************************************************************
************ C H E C K S U M V E R I F I C A T I O N *************
*************************************************************************/
control MyVerifyChecksum(inout headers hdr, inout metadata meta) {
apply { }
}
/*************************************************************************
************** I N G R E S S P R O C E S S I N G *******************
*************************************************************************/
control MyIngress(inout headers hdr,
inout metadata meta,
inout standard_metadata_t standard_metadata) {
/* Direct meter*/
direct_meter<bit<2>>(MeterType.bytes) rsvp_meter;
action drop() {
mark_to_drop(standard_metadata);
}
action ipv4_forward(macAddr_t dstAddr, egressSpec_t port) {
hdr.ethernet.srcAddr = hdr.ethernet.dstAddr;
hdr.ethernet.dstAddr = dstAddr;
standard_metadata.egress_spec = port;
hdr.ipv4.ttl = hdr.ipv4.ttl - 1;
}
action mpls_ingress_1_hop(label_t label_1) {
rsvp_meter.read(meta.meter_color);
hdr.ethernet.etherType = TYPE_MPLS;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_1;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 1;
}
action mpls_ingress_2_hop(label_t label_1, label_t label_2) {
rsvp_meter.read(meta.meter_color);
hdr.ethernet.etherType = TYPE_MPLS;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_1;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 1;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_2;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 0;
}
action mpls_ingress_3_hop(label_t label_1, label_t label_2, label_t label_3) {
rsvp_meter.read(meta.meter_color);
hdr.ethernet.etherType = TYPE_MPLS;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_1;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 1;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_2;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 0;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_3;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 0;
}
action mpls_ingress_4_hop(label_t label_1, label_t label_2, label_t label_3, label_t label_4) {
rsvp_meter.read(meta.meter_color);
hdr.ethernet.etherType = TYPE_MPLS;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_1;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 1;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_2;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 0;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_3;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 0;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_4;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 0;
}
action mpls_ingress_5_hop(label_t label_1, label_t label_2, label_t label_3, label_t label_4, label_t label_5) {
rsvp_meter.read(meta.meter_color);
hdr.ethernet.etherType = TYPE_MPLS;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_1;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 1;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_2;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 0;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_3;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 0;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_4;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 0;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_5;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 0;
}
action mpls_ingress_6_hop(label_t label_1, label_t label_2, label_t label_3, label_t label_4, label_t label_5, label_t label_6) {
rsvp_meter.read(meta.meter_color);
hdr.ethernet.etherType = TYPE_MPLS;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_1;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 1;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_2;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 0;
hdr.mpls.push_front(1);
hdr.mpls[0].setValid();
hdr.mpls[0].label = label_3;
hdr.mpls[0].ttl = hdr.ipv4.ttl - 1;
hdr.mpls[0].s = 0;
hdr.mpls.push_front(1);