netns.sh revision bee3dc86
1#!/usr/bin/env bash
2
3# Copyright (c) 2016 Cisco and/or its affiliates.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at:
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16if [ $USER != "root" ] ; then
17    #echo "Restarting script with sudo..."
18    sudo $0 ${*}
19    exit
20fi
21
22C1_IP=$1
23C1_GW=$2
24C2_IP=$3
25C2_GW=$4
26
27# Restart VPP so there is no config
28service vpp restart
29
30# delete previous incarnations if they exist
31ip link del dev veth_link1 &>/dev/null
32ip link del dev veth_link2 &>/dev/null
33ip netns del c1 &>/dev/null
34ip netns del c2 &>/dev/null
35
36#create namespaces
37ip netns add c1 &>/dev/null
38ip netns add c2 &>/dev/null
39
40# create and configure 1st veth pair
41ip link add name veth_link1 type veth peer name link1
42ip link set dev link1 up
43ip link set dev veth_link1 up netns c1
44
45ip netns exec c1 \
46     bash -c "
47    ip link set dev lo up
48    ip addr add ${C1_IP} dev veth_link1
49    ip route add default via ${C1_GW} dev veth_link1
50"
51
52# create and configure 2nd veth pair
53ip link add name veth_link2 type veth peer name link2
54ip link set dev link2 up
55ip link set dev veth_link2 up netns c2
56
57ip netns exec c2 \
58     bash -c "
59    ip link set dev lo up
60    ip addr add ${C2_IP} dev veth_link2
61    ip route add default via ${C2_GW} dev veth_link2
62"
63