1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4NR_FILES=32768
5readonly NETNS="ns-$(mktemp -u XXXXXX)"
6
7# default values
8port=443
9addr_v6="2001:0db8:0:f101::1"
10addr_v4="10.8.8.8"
11use_v6=true
12addr=""
13
14usage() {
15    echo "Usage: $0 [-6 | -4] [-p port] [-a address]"
16    echo -e "\t6: use ipv6"
17    echo -e "\t4: use ipv4"
18    echo -e "\tport: Port number"
19    echo -e "\taddress: ip address"
20}
21
22while getopts "ha:p:64" opt; do
23    case ${opt} in
24	h)
25	    usage $0
26	    exit 0
27	    ;;
28	a)  addr=$OPTARG;;
29	p)
30	    port=$OPTARG;;
31	6)
32	    use_v6=true;;
33	4)
34	    use_v6=false;;
35    esac
36done
37
38setup() {
39    ip netns add "${NETNS}"
40    ip -netns "${NETNS}" link add veth0 type veth peer name veth1
41    ip -netns "${NETNS}" link set lo up
42    ip -netns "${NETNS}" link set veth0 up
43    ip -netns "${NETNS}" link set veth1 up
44
45    if [[ "$use_v6" == true ]]; then
46        ip -netns "${NETNS}" addr add $addr_v6 nodad dev veth0
47    else
48        ip -netns "${NETNS}" addr add $addr_v4 dev lo
49    fi
50}
51
52cleanup() {
53    ip netns del "${NETNS}"
54}
55
56if [[ "$addr" != "" ]]; then
57    addr_v4=$addr;
58    addr_v6=$addr;
59fi
60setup
61if [[ "$use_v6" == true ]] ; then
62    ip netns exec "${NETNS}" sh -c \
63        "ulimit -n ${NR_FILES};./bind_bhash ${port} ipv6 ${addr_v6}"
64else
65    ip netns exec "${NETNS}" sh -c \
66        "ulimit -n ${NR_FILES};./bind_bhash ${port} ipv4 ${addr_v4}"
67fi
68cleanup
69