2019-06-12 19:06:18 +02:00

139 lines
4.8 KiB
Executable file

#!/usr/bin/env python
# Copyright (c) 2017 Stephen Ibanez
# All rights reserved.
# This software was developed by Stanford University and the University of Cambridge Computer Laboratory
# under National Science Foundation under Grant No. CNS-0855268,
# the University of Cambridge Computer Laboratory under EPSRC INTERNET Project EP/H040536/1 and
# by the University of Cambridge Computer Laboratory under DARPA/AFRL contract FA8750-11-C-0249 ("MRC2"),
# as part of the DARPA MRC research programme.
# Licensed to NetFPGA C.I.C. (NetFPGA) under one or more contributor
# license agreements. See the NOTICE file distributed with this work for
# additional information regarding copyright ownership. NetFPGA licenses this
# file to you under the NetFPGA Hardware-Software License, Version 1.0 (the
# "License"); you may not use this file except in compliance with the
# License. You may obtain a copy of the License at:
# Unless required by applicable law or agreed to in writing, Work 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 os, sys, re, cmd, subprocess, shlex, time
from threading import Thread
from switch_calc_headers import *
from nf_sim_tools import *
IFACE = "nf0"
ETH_SRC = "08:11:11:11:11:08"
ETH_DST = "08:22:22:22:22:08"
os.system('sudo ifconfig {0} netmask'.format(IFACE))
TCPDUMP = subprocess.Popen(shlex.split("tcpdump -i {0} -w /dev/null".format(IFACE)))
class SwitchCalcTester(cmd.Cmd):
"""The HW testing tool for the switch_calc design"""
prompt = "testing> "
intro = "The HW testing tool for the switch_calc design\n type help to see all commands"
def _to_int(self, op):
val = int(op, 0)
assert(val >= 0)
return val
print >> sys.stderr, "ERROR: operands must be valid positive integers"
return -1
Submit packet to the switch and print the results
def _submit_pkt(self, pkt):
rcvd_pkt = srp1(pkt, iface=IFACE)
width = 10
n = 9
fmat_string = "| ETHERNET | OP1:{0:<{width}} OP_CODE:{1:<{width}} OP2:{2:<{width}} RESULT:{3:<{width}} |"
print "Sent pkt: "
print "{0:-<{width}}".format("-", width=n*width)
print fmat_string.format(pkt[Calc].op1, pkt[Calc].opCode, pkt[Calc].op2, pkt[Calc].result, width=width)
print "{0:-<{width}}\n".format("-", width=n*width)
print "Received pkt: "
print "{0:-<{width}}".format("-", width=n*width)
print fmat_string.format(rcvd_pkt[Calc].op1, rcvd_pkt[Calc].opCode, rcvd_pkt[Calc].op2, rcvd_pkt[Calc].result, width=width)
print "{0:-<{width}}\n".format("-", width=n*width)
def _parse_line(self, line):
args = line.split()
if (len(args) != 3):
print >> sys.stderr, "ERROR: usage..."
op1 = self._to_int(args[0])
operation = args[1]
op2 = self._to_int(args[2])
if op1 == -1 or op2 == -1:
if (operation not in OPCODE_MAP.keys()):
print >> sys.stderr, "ERROR: {0} is not a supported operation".format(operation)
opCode = OPCODE_MAP[operation]
pkt = Ether(dst=ETH_DST, src=ETH_SRC) / Calc(op1=op1, opCode=opCode, op2=op2, result=0)
pkt = pad_pkt(pkt, PKT_SIZE) # pad pkt to desired size
return pkt
def do_run_test(self, line):
pkt = self._parse_line(line)
def help_run_test(self):
print """
run_test <op1> <operation> <op2>
DESCRIPTION: Create a single test packet to test the functionality of the switch_calc implementation
<operation> : must be one of the following [ADD, +, SUB, -, LOOKUP, ADD_REG, SET_REG]
LOOKUP : <op1> = the key to lookup, <op2> = unused
ADD_REG / SET_REG : <op1> = register index, <op2> = value
def do_exit(self, line):
if (TCPDUMP.poll() is None):
def do_EOF(self, line):
print ""
if (TCPDUMP.poll() is None):
return True
if __name__ == '__main__':
if len(sys.argv) > 1:
SwitchCalcTester().onecmd(' '.join(sys.argv[1:]))
if (TCPDUMP.poll() is None):