1#!/bin/bash 2# Copyright (c) PLUMgrid, Inc. 3# Licensed under the Apache License, Version 2.0 (the "License") 4 5#set -x 6 7name=$1; shift 8kind=$1; shift 9cmd=$1; shift 10 11PYTHONPATH=@CMAKE_BINARY_DIR@/src/python 12LD_LIBRARY_PATH=@CMAKE_BINARY_DIR@:@CMAKE_BINARY_DIR@/src/cc 13 14ns=$name 15 16function cleanup() { 17 trap - EXIT 18 if [[ "$kind" = "namespace" ]]; then 19 sudo ip netns delete $ns 20 fi 21} 22 23trap cleanup EXIT 24 25function ns_run() { 26 sudo ip netns add $ns 27 sudo ip link add $ns.in type veth peer name $ns.out 28 sudo ip link set $ns.in netns $ns 29 sudo ip netns exec $ns ip link set $ns.in name eth0 30 sudo ip netns exec $ns ip addr add dev eth0 172.16.1.2/24 31 sudo ip netns exec $ns ip link set eth0 up 32 sudo ip netns exec $ns ethtool -K eth0 tx off 33 sudo ip addr add dev $ns.out 172.16.1.1/24 34 sudo ip link set $ns.out up 35 sudo bash -c "PYTHONPATH=$PYTHONPATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH ip netns exec $ns $cmd $1 $2" 36 return $? 37} 38function sudo_run() { 39 sudo bash -c "PYTHONPATH=$PYTHONPATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH $cmd $1 $2" 40 return $? 41} 42function simple_run() { 43 PYTHONPATH=$PYTHONPATH LD_LIBRARY_PATH=$LD_LIBRARY_PATH $cmd $1 $2 44 return $? 45} 46 47case $kind in 48 namespace) 49 ns_run $@ 50 ;; 51 sudo) 52 sudo_run $@ 53 ;; 54 simple) 55 simple_run $@ 56 ;; 57 *) 58 echo "Invalid kind $kind" 59 exit 1 60 ;; 61esac 62 63[[ $? -ne 0 ]] && { echo "Failed"; exit 1; } 64 65exit 0 66