From 3cbc04bea02fc60471dfe0c671ede3ca42c118c3 Mon Sep 17 00:00:00 2001 From: Florin Coras Date: Mon, 2 Oct 2017 00:18:51 -0700 Subject: udp: refactor udp code Change-Id: I44d5c9df7c49b8d4d5677c6d319033b2da3e6b80 Signed-off-by: Florin Coras --- src/scripts/vnet/uri/dummy_app.py | 91 ++++++++++++++++++++++++++++----------- src/scripts/vnet/uri/udp | 6 +++ 2 files changed, 72 insertions(+), 25 deletions(-) mode change 100644 => 100755 src/scripts/vnet/uri/dummy_app.py (limited to 'src/scripts') diff --git a/src/scripts/vnet/uri/dummy_app.py b/src/scripts/vnet/uri/dummy_app.py old mode 100644 new mode 100755 index ff00f2fc8c6..85ea4dcd474 --- a/src/scripts/vnet/uri/dummy_app.py +++ b/src/scripts/vnet/uri/dummy_app.py @@ -3,6 +3,7 @@ import socket import sys import time +import argparse # action can be reflect or drop action = "drop" @@ -32,37 +33,52 @@ def handle_connection (connection, client_address): connection.sendall(data) finally: connection.close() - -def run_server(ip, port): - print("Starting server {}:{}".format(repr(ip), repr(port))) +def run_tcp_server(ip, port): + print("Starting TCP server {}:{}".format(repr(ip), repr(port))) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) server_address = (ip, int(port)) sock.bind(server_address) sock.listen(1) - while True: connection, client_address = sock.accept() handle_connection (connection, client_address) +def run_udp_server(ip, port): + print("Starting UDP server {}:{}".format(repr(ip), repr(port))) + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + server_address = (ip, int(port)) + sock.bind(server_address) + while True: + data, addr = sock.recvfrom(4096) + if (action != "drop"): + #snd_sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + sock.sendto (data, addr) -def prepare_data(): +def run_server(ip, port, proto): + if (proto == "tcp"): + run_tcp_server(ip, port) + elif (proto == "udp"): + run_udp_server(ip, port) + +def prepare_data(power): buf = [] - for i in range (0, pow(2, 16)): + for i in range (0, pow(2, power)): buf.append(i & 0xff) return bytearray(buf) -def run_client(ip, port): - print("Starting client {}:{}".format(repr(ip), repr(port))) +def run_tcp_client(ip, port): + print("Starting TCP client {}:{}".format(repr(ip), repr(port))) sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) - server_address = (ip, port) + server_address = (ip, int(port)) sock.connect(server_address) - - data = prepare_data() + + data = prepare_data(16) n_rcvd = 0 n_sent = len (data) try: sock.sendall(data) - + timeout = time.time() + 2 while n_rcvd < n_sent and time.time() < timeout: tmp = sock.recv(1500) @@ -73,28 +89,53 @@ def run_client(ip, port): print("Difference at byte {}. Sent {} got {}" .format(n_rcvd + i, data[n_rcvd + i], tmp[i])) n_rcvd += n_read - + if (n_rcvd < n_sent or n_rcvd > n_sent): print("Sent {} and got back {}".format(n_sent, n_rcvd)) else: print("Got back what we've sent!!"); - + finally: sock.close() - -def run(mode, ip, port): +def run_udp_client(ip, port): + print("Starting UDP client {}:{}".format(repr(ip), repr(port))) + n_packets = 100 + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) + server_address = (ip, int(port)) + data = prepare_data(10) + try: + for i in range (0, n_packets): + sock.sendto(data, server_address) + finally: + sock.close() +def run_client(ip, port, proto): + if (proto == "tcp"): + run_tcp_client(ip, port) + elif (proto == "udp"): + run_udp_client(ip, port) +def run(mode, ip, port, proto): if (mode == "server"): - run_server (ip, port) + run_server (ip, port, proto) elif (mode == "client"): - run_client (ip, port) + run_client (ip, port, proto) else: raise Exception("Unknown mode. Only client and server supported") if __name__ == "__main__": - if (len(sys.argv)) < 4: - raise Exception("Usage: ./dummy_app [ ]") - if (len(sys.argv) == 6): - action = sys.argv[4] - test = int(sys.argv[5]) - - run (sys.argv[1], sys.argv[2], int(sys.argv[3])) + parser = argparse.ArgumentParser() + parser.add_argument('-m', action='store', dest='mode') + parser.add_argument('-i', action='store', dest='ip') + parser.add_argument('-p', action='store', dest='port') + parser.add_argument('-proto', action='store', dest='proto') + parser.add_argument('-a', action='store', dest='action') + parser.add_argument('-t', action='store', dest='test') + results = parser.parse_args() + action = results.action + test = results.test + run(results.mode, results.ip, results.port, results.proto) + #if (len(sys.argv)) < 4: + # raise Exception("Usage: ./dummy_app [ ]") + #if (len(sys.argv) == 6): + # action = sys.argv[4] + # test = int(sys.argv[5]) + #run (sys.argv[1], sys.argv[2], int(sys.argv[3])) diff --git a/src/scripts/vnet/uri/udp b/src/scripts/vnet/uri/udp index c7628f49b13..3ab4292fce6 100644 --- a/src/scripts/vnet/uri/udp +++ b/src/scripts/vnet/uri/udp @@ -1,6 +1,12 @@ loop create set int ip address loop0 6.0.0.1/32 set int state loop0 up +set int state GigabitEthernet1b/0/0 up +set int ip address GigabitEthernet1b/0/0 192.168.1.1/24 + +create host-interface name vpp1 +set int state host-vpp1 up +set int ip address host-vpp1 6.0.1.1/24 packet-generator new { name udp -- cgit 1.2.3-korg