From 1f0684784622deb25b3037842a4fa1be03f75e12 Mon Sep 17 00:00:00 2001 From: haskal Date: Wed, 29 Jan 2020 16:23:07 -0500 Subject: [PATCH] C o n t a i n e r i z a t i o n --- .gitignore | 1 + Makefile | 18 +++++++++++++ iputil.rkt | 54 +++++++++++++++++++++++++++++++++++++++ router | 19 ++++++++++++++ router.rkt | 16 ------------ scripts/nspawn-support.sh | 10 ++++++++ scripts/run-tests.sh | 4 +++ 7 files changed, 106 insertions(+), 16 deletions(-) create mode 100644 Makefile create mode 100644 iputil.rkt create mode 100755 router delete mode 100644 router.rkt create mode 100755 scripts/nspawn-support.sh create mode 100755 scripts/run-tests.sh diff --git a/.gitignore b/.gitignore index b2f4ba5..c3526aa 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.rkt~ +/build diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7d38355 --- /dev/null +++ b/Makefile @@ -0,0 +1,18 @@ +.PHONY: all test clean + +TESTS_DIR=build +TEST_SIM_PKG=https://course.ccs.neu.edu/cs3700sp20/archive/bgp-sim.tar.gz + +all: + +$(TESTS_DIR): + mkdir -p $(TESTS_DIR) + sudo ./scripts/nspawn-support.sh create $(TESTS_DIR)/container + cd $(TESTS_DIR) && wget -O bgp-sim.tar.gz $(TEST_SIM_PKG) + cd $(TESTS_DIR) && tar xf bgp-sim.tar.gz + +clean: + sudo $(RM) -r $(TESTS_DIR) + +test: $(TESTS_DIR) + sudo ./scripts/nspawn-support.sh boot $(TESTS_DIR)/container diff --git a/iputil.rkt b/iputil.rkt new file mode 100644 index 0000000..1d6c11d --- /dev/null +++ b/iputil.rkt @@ -0,0 +1,54 @@ +#lang racket + +(require racket/struct) + +(provide peer subnet + string->ip + ip->string + string->subnet + subnet->string + string->peer) + +(struct peer [ip type] + #:methods gen:custom-write + [(define write-proc + (make-constructor-style-printer + (lambda (x) 'peer) + (lambda (x) (list (ip->string (peer-ip x)) (peer-type x)))))]) + +(struct subnet [ip mask] + #:methods gen:custom-write + [(define write-proc + (make-constructor-style-printer + (lambda (x) 'subnet) + (lambda (x) (list (subnet->string x)))))]) + +(define (string->ip str) + (let ([parts (reverse (string-split str "."))]) + (for/sum ([i (in-range 4)] + [part parts]) + (arithmetic-shift (string->number part) (* i 8))))) + +(define (ip->string ip) + (string-join + (reverse + (for/list ([i (in-range 4)]) + (number->string (bitwise-and + (arithmetic-shift ip (* i -8)) 255)))) + ".")) + +(define (string->subnet str) + (match (string-split str "/") + [(list ipstr maskstr) (subnet (string->ip ipstr) + (string->number maskstr))])) + +(define (subnet->string sub) + (string-append + (ip->string (subnet-ip sub)) + "/" + (number->string (subnet-mask sub)))) + +(define (string->peer str) + (match (string-split str "-") + [(list ip type) + (peer (string->ip ip) (string->symbol type))])) diff --git a/router b/router new file mode 100755 index 0000000..1c17f11 --- /dev/null +++ b/router @@ -0,0 +1,19 @@ +#!/usr/bin/env racket +#lang racket + +(require "iputil.rkt") + +;; Str [Listof Peer] -> +;; Router main +(define (run-router asn peers) + (displayln asn) + (map displayln peers) + (void)) + +(module+ main + (command-line + #:program "router" + #:args + (asn . peers) + ;; Run the router + (run-router asn (map string->peer peers)))) diff --git a/router.rkt b/router.rkt deleted file mode 100644 index 977dbab..0000000 --- a/router.rkt +++ /dev/null @@ -1,16 +0,0 @@ -#lang racket - -;; Str [Listof Str] -> -;; Router main -(define (run-router asn ip-addrs) - (displayln asn) - (map displayln ip-addrs) - (void)) - -(module+ main - (command-line - #:program "router" - #:args - (asn . ip-addrs) - ;; Run the router - (run-router asn ip-addrs))) diff --git a/scripts/nspawn-support.sh b/scripts/nspawn-support.sh new file mode 100755 index 0000000..5807c1e --- /dev/null +++ b/scripts/nspawn-support.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +cmd="$1" +dir="$2" + +if [ "$cmd" == "create" ]; then + debootstrap --include=systemd-container --include=libpython3.6 --include=racket --components=main,universe bionic "$dir" http://archive.ubuntu.com/ubuntu/ +elif [ "$cmd" == "boot" ]; then + systemd-nspawn -D "$dir" --bind "$PWD:/router" -- /bin/bash -c "cd /router && scripts/run-tests.sh /router/build; bash" +fi diff --git a/scripts/run-tests.sh b/scripts/run-tests.sh new file mode 100755 index 0000000..ebff23f --- /dev/null +++ b/scripts/run-tests.sh @@ -0,0 +1,4 @@ +#!/bin/bash + +cd $1 +./sim --router ../router milestone