-
Notifications
You must be signed in to change notification settings - Fork 6
/
ovn-port.sh
executable file
·148 lines (121 loc) · 2.92 KB
/
ovn-port.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
usage () {
cat << EOF
usage: ovn-port COMMAND
These commands need to be run on the host where you plan
to spawn your namespaces.
Commands:
add-port [--db=tcp:IP:6640] LSWITCH LPORT_NAME IP/MASK GATEWAY
del-port [--db=tcp:IP:6640] LPORT_NAME
EOF
}
add_port () {
case $1 in
--db=*)
DB=`expr X"$1" : 'X[^=]*=\(.*\)'`
shift
;;
esac
LSWITCH=$1
if [ -z "$LSWITCH" ]; then
echo "No switch name given" >& 2
exit 1
fi
if [ -n "$DB" ]; then
exists=`ovn-nbctl --db=$DB get logical_switch $LSWITCH name`
else
exists=`ovn-nbctl get logical_switch $LSWITCH name`
fi
if [ -z "$exists" ]; then
echo "$LSWITCH switch does not exist in NB"
exit 1
fi
LPORT_NAME=$2
if [ -z "$LPORT_NAME" ]; then
echo "No lport name given" >& 2
exit 1
fi
IP=$3
if [ -z "$IP" ]; then
echo "No IP given" >& 2
exit 1
fi
GATEWAY=$4
if [ -z "$GATEWAY" ]; then
echo "No GATEWAY given" >& 2
exit 1
fi
x=`shuf -i 1-99 -n 1`
y=`shuf -i 1-99 -n 1`
z=`shuf -i 1-99 -n 1`
MAC="00:02:03:$x:$y:$z"
if [ -n "$DB" ]; then
ovn-nbctl --db=$DB lsp-add $LSWITCH $LPORT_NAME
else
ovn-nbctl lsp-add $LSWITCH $LPORT_NAME
fi
IP_ONLY=`echo $IP | awk -F \/ '{print $1}'`
if [ -n "$DB" ]; then
ovn-nbctl --db=$DB lsp-set-addresses $LPORT_NAME "$MAC $IP_ONLY"
else
ovn-nbctl lsp-set-addresses $LPORT_NAME "$MAC $IP_ONLY"
fi
ip netns add $LPORT_NAME
ip link add "${LPORT_NAME}_l" type veth peer name "${LPORT_NAME}_c"
BRIDGE="br-int"
if ovs-vsctl --may-exist add-port "$BRIDGE" "${LPORT_NAME}_l" \
-- set interface "${LPORT_NAME}_l" \
external_ids:iface-id="$LPORT_NAME"; then : ; else
echo >&2 "$UTIL: Failed to add "${LPORT_NAME}_l" port to bridge $BRIDGE"
ip link delete "${LPORT_NAME}_l"
exit 1
fi
ip link set "${LPORT_NAME}_l" up
ip link set "${LPORT_NAME}_c" netns "${LPORT_NAME}"
ip netns exec "${LPORT_NAME}" ip link set dev "${LPORT_NAME}_c" name eth0
ip netns exec "${LPORT_NAME}" ip link set eth0 up
ip netns exec "${LPORT_NAME}" ip link set dev eth0 mtu 1440
ip netns exec "${LPORT_NAME}" ip addr add $IP dev eth0
ip netns exec "${LPORT_NAME}" ip link set dev eth0 address "$MAC"
ip netns exec "${LPORT_NAME}" ip route add default via "$GATEWAY"
}
del_port () {
case $1 in
--db=*)
DB=`expr X"$1" : 'X[^=]*=\(.*\)'`
shift
;;
esac
LPORT_NAME=$1
if [ -z "$LPORT_NAME" ]; then
echo "No lport name given" >& 2
exit 1
fi
if [ -n "$DB" ]; then
ovn-nbctl --db=$DB lport-del $LPORT_NAME
else
ovn-nbctl lport-del $LPORT_NAME
fi
ip netns delete $LPORT_NAME
ovs-vsctl del-port ${LPORT_NAME}_l
ip link delete ${LPORT_NAME}_l
}
case $1 in
"add-port")
shift
add_port "$@"
exit 0
;;
"del-port")
shift
del_port "$@"
exit 0
;;
-h | --help)
usage
exit 0
;;
*)
echo >&2 "$UTIL: unknown command \"$1\" (use --help for help)"
exit 1
;;
esac