Debian Codegraph @

devstack/plugin.sh.bagpipe_bgp

raw
1 #!/bin/bash
2
3 # Save trace setting
4 _XTRACE_BAGPIPE_BGP=$(set +o | grep xtrace)
5 set -o xtrace
6
7 # Pre-install gobgp requirements
8 function pre_install_gobgp {
9 # Install go language and configure environment variables
10 install_package golang-go
11
12 export GOPATH=${GOPATH:-$DEST/go}
13 if [[ ! -d $GOPATH ]]; then
14 mkdir -p $GOPATH
15 fi
16 }
17
18 # Install gobgp
19 function install_gobgp {
20 # we install from binary, because compiling from source requires golang-1.7 which is not in ubuntu xenial
21 mkdir -p $GOPATH/bin
22 cd $GOPATH/bin
23 wget $GOBGP_RELEASE -qO - | tar xz
24 }
25
26 # Set config files, create data dirs, etc
27 function configure_bagpipe {
28 # Put config files in ``/etc/bagpipe-bgp`` for everyone to find
29 if [[ ! -d $BAGPIPE_CONF_DIR ]]; then
30 sudo mkdir -p $BAGPIPE_CONF_DIR
31 fi
32 sudo chown $STACK_USER $BAGPIPE_CONF_DIR
33
34 # place rootwrap config files
35 # this is done here so that these files are in place, even when
36 # bagpipe-bgp is not configured, specifically for the functional and
37 # fullstack gate jobs
38 BAGPIPE_BGP_RW_CONF=/etc/bagpipe-bgp/rootwrap.conf
39 BAGPIPE_BGP_RW_D=/etc/bagpipe-bgp/rootwrap.d
40 sudo install -o root -g root -m 644 $NETWORKING_BAGPIPE_DIR/etc/bagpipe-bgp/rootwrap.conf $BAGPIPE_BGP_RW_CONF
41 sudo install -d -o root -m 755 $BAGPIPE_BGP_RW_D/
42 sudo install -o root -m 644 $NETWORKING_BAGPIPE_DIR/etc/bagpipe-bgp/rootwrap.d/* $BAGPIPE_BGP_RW_D/
43 sudo sed -e "s:^filters_path=.*$:filters_path=$BAGPIPE_BGP_RW_D:" -i $BAGPIPE_BGP_RW_CONF
44 sudo sed -e 's:^exec_dirs=\(.*\)$:exec_dirs=\1,/usr/local/bin:' -i $BAGPIPE_BGP_RW_CONF
45
46 # Specify ``rootwrap.conf`` as first parameter to neutron-rootwrap
47 ROOTWRAP_SUDOER_CMD="$NEUTRON_ROOTWRAP $BAGPIPE_BGP_RW_CONF *"
48 ROOTWRAP_DAEMON_SUDOER_CMD="$NEUTRON_ROOTWRAP-daemon $BAGPIPE_BGP_RW_CONF"
49
50 # configure sudo to allow bagpipe-bgp rootwrap config
51 TEMPFILE=`mktemp`
52 echo "$STACK_USER ALL=(root) NOPASSWD: $ROOTWRAP_SUDOER_CMD" >$TEMPFILE
53 echo "$STACK_USER ALL=(root) NOPASSWD: $ROOTWRAP_DAEMON_SUDOER_CMD" >>$TEMPFILE
54 chmod 0440 $TEMPFILE
55 sudo chown root:root $TEMPFILE
56 sudo mv $TEMPFILE /etc/sudoers.d/bagpipe-rootwrap
57
58 if is_service_enabled neutron-bagpipe-bgp; then
59 # build the config file from scratch
60 create_bagpipe_conf
61 fi
62 }
63
64 # Create a new bgp.conf file
65 function create_bagpipe_conf {
66 # (Re)create ``bgp.conf``
67 cp -p $NETWORKING_BAGPIPE_DIR/etc/bagpipe-bgp/bgp.conf.template $BAGPIPE_CONF
68
69 # build config
70 iniset $BAGPIPE_CONF DEFAULT debug $ENABLE_DEBUG_LOG_LEVEL
71
72 iniset $BAGPIPE_CONF BGP local_address ${BAGPIPE_HOST_IP:-$HOST_IP}
73 if [[ $BAGPIPE_BGP_PEERS == "-" ]]; then
74 iniset $BAGPIPE_CONF BGP peers ""
75 else
76 iniset $BAGPIPE_CONF BGP peers "$BAGPIPE_BGP_PEERS"
77 fi
78 iniset $BAGPIPE_CONF API host $BAGPIPE_SERVICE_HOST
79 iniset $BAGPIPE_CONF API port $BAGPIPE_SERVICE_PORT
80 iniset $BAGPIPE_CONF DATAPLANE_DRIVER_IPVPN dataplane_driver ${BAGPIPE_DATAPLANE_DRIVER_IPVPN:-dummy}
81 iniset $BAGPIPE_CONF DATAPLANE_DRIVER_IPVPN mpls_interface $BAGPIPE_MPLS_IFACE
82 iniset $BAGPIPE_CONF DATAPLANE_DRIVER_IPVPN ovs_bridge $BAGPIPE_MPLS_BR
83 iniset $BAGPIPE_CONF DATAPLANE_DRIVER_EVPN dataplane_driver ${BAGPIPE_DATAPLANE_DRIVER_EVPN:-dummy}
84 iniset $BAGPIPE_CONF DATAPLANE_DRIVER_IPVPN arp_responder ${BAGPIPE_ARP_RESPONDER:-False}
85 iniset $BAGPIPE_CONF DATAPLANE_DRIVER_IPVPN proxy_arp ${BAGPIPE_PROXY_ARP:-False}
86
87 BAGPIPE_BGP_RW_COMMAND="sudo $NEUTRON_ROOTWRAP $BAGPIPE_BGP_RW_CONF"
88 BAGPIPE_BGP_RW_DAEMON_COMMAND="sudo $NEUTRON_ROOTWRAP-daemon $BAGPIPE_BGP_RW_CONF"
89 iniset $BAGPIPE_CONF common root_helper "$BAGPIPE_BGP_RW_COMMAND"
90 iniset $BAGPIPE_CONF common root_helper_daemon "$BAGPIPE_BGP_RW_DAEMON_COMMAND"
91
92 setup_logging $BAGPIPE_CONF
93 }
94
95 # Initialize databases, etc.
96
97 function init_bagpipe {
98 if [[ $BAGPIPE_DATAPLANE_DRIVER_IPVPN == *"MPLSOVSDataplaneDriver"* || $BAGPIPE_DATAPLANE_DRIVER_IPVPN == "ovs" ]]; then
99 init_bagpipe_ovsmpls
100 else
101 echo "IP VPN driver not OVS, let's not init OVS MPLS bridge (driver is '$BAGPIPE_DATAPLANE_DRIVER_IPVPN')"
102 fi
103 }
104
105
106 function init_bagpipe_ovsmpls {
107
108 BAGPIPE_BR_RESET_SCRIPT=$(mktemp /var/tmp/bagpipe-bgp-br-reset.XXXXX)
109
110 :> $BAGPIPE_BR_RESET_SCRIPT
111
112 if [ -n "$BAGPIPE_MPLS_IFACE" ]; then
113
114 cat >> $BAGPIPE_BR_RESET_SCRIPT <<EOF
115
116 echo "Setting up $BAGPIPE_MPLS_BR OVS bridge based on current $BAGPIPE_MPLS_IFACE settings"
117 MPLS_IFACE_IP=\`ip addr show $BAGPIPE_MPLS_IFACE | grep 'inet ' | awk '{ print \$2 }'\`
118
119 if [ -z "\$MPLS_IFACE_IP" ]; then
120 echo "Failure retrieving IP config of BaGPipe MPLS interface ($BAGPIPE_MPLS_IFACE): perhaps $BAGPIPE_MPLS_BR was configured already ?"
121 echo "Try to setup $BAGPIPE_MPLS_IFACE before trying again.."
122 return -1
123 fi
124
125 if [ "\`ip route | grep default | awk '{ print \$5 }'\`" == "$BAGPIPE_MPLS_IFACE" ]; then
126 GW_IP=\`ip route | grep default | awk '{ print \$3 }'\`
127 fi
128
129 #echo "adding bridge $BAGPIPE_MPLS_BR with interface $BAGPIPE_MPLS_IFACE"
130 sudo ip addr flush dev $BAGPIPE_MPLS_IFACE
131 sudo ovs-vsctl del-br $BAGPIPE_MPLS_BR || true
132 sudo ovs-vsctl --may-exist add-br $BAGPIPE_MPLS_BR -- set-fail-mode $BAGPIPE_MPLS_BR secure
133 sudo ovs-vsctl --may-exist add-port $BAGPIPE_MPLS_BR $BAGPIPE_MPLS_IFACE
134 # when devstack is ran in a VM deployed on openstack, by default, openstac
135 # will forbid this VM to use another MAC address than the one it has allocated
136 # use a port with the same MAC as the one used by BAGPIPE_MPLS_IFACE
137 MAC=\`ip link show dev $BAGPIPE_MPLS_IFACE | grep link/ether | awk '{ print \$2 }'\`
138 sudo ip link set dev $BAGPIPE_MPLS_BR address \$MAC
139 #echo "setting ip and port on $BAGPIPE_MPLS_BR"
140 sudo ip addr add \$MPLS_IFACE_IP dev $BAGPIPE_MPLS_BR
141 sudo ip link set $BAGPIPE_MPLS_BR up
142 if [ -n "\$GW_IP" ]; then
143 #echo "adding route to $\GW_IP"
144 sudo ip route add default via \$GW_IP
145 fi
146
147 # map traffic to/from the bagpipe port from/to the physical interface,
148 # traffic from the phy interface only goes to internal port as a last
149 # resort (if it did not match an MPLS rule)
150 BAGPIPE_MPLS_IFACE_PORT_NUMBER=\`sudo ovs-ofctl show $BAGPIPE_MPLS_BR | grep "($BAGPIPE_MPLS_IFACE)" | awk '-F(' '{print \$1}' | tr -d ' '\`
151 BAGPIPE_INTERNAL_PORT_NUMBER=\`sudo ovs-ofctl show $BAGPIPE_MPLS_BR | grep "($BAGPIPE_MPLS_BR)" | awk '-F(' '{print \$1}' | tr -d ' '\`
152 sudo ovs-ofctl add-flow $BAGPIPE_MPLS_BR priority=0,in_port=\$BAGPIPE_MPLS_IFACE_PORT_NUMBER,action=output:\$BAGPIPE_INTERNAL_PORT_NUMBER
153 sudo ovs-ofctl add-flow $BAGPIPE_MPLS_BR in_port=\$BAGPIPE_INTERNAL_PORT_NUMBER,action=output:\$BAGPIPE_MPLS_IFACE_PORT_NUMBER
154
155 EOF
156
157 else
158
159 cat >> $BAGPIPE_BR_RESET_SCRIPT <<EOF
160
161 sudo ovs-vsctl del-br $BAGPIPE_MPLS_BR || true
162 sudo ovs-vsctl --may-exist add-br $BAGPIPE_MPLS_BR -- set-fail-mode $BAGPIPE_MPLS_BR secure
163
164 EOF
165
166 fi
167
168 source $BAGPIPE_BR_RESET_SCRIPT
169 }
170
171 # Start the BGP component
172 function start_bagpipe_bgp {
173 if is_service_enabled neutron-bagpipe-bgp ; then
174 run_process neutron-bagpipe-bgp "${NEUTRON_BIN_DIR}/bagpipe-bgp"
175
176 run_process neutron-bagpipe-bgp-lg "/bin/sh -c 'while true; do date ; sleep 10; bagpipe-looking-glass -r vpns instances ; echo --------------------------------------------------; done'"
177
178 echo "Waiting for bagpipe-bgp to start..."
179 if ! wait_for_service $SERVICE_TIMEOUT http://$BAGPIPE_SERVICE_HOST:$BAGPIPE_SERVICE_PORT; then
180 die $LINENO "bagpipe-bgp did not start"
181 fi
182 fi
183 }
184
185 # Start the FakeRR component
186 function start_bagpipe_fakerr {
187 if is_service_enabled neutron-bagpipe-fakerr ; then
188 run_process neutron-bagpipe-fakerr "echo 'from networking_bagpipe.bagpipe_bgp.fakerr import application' | sudo twistd -n -y /dev/stdin"
189 fi
190 }
191
192 function start_bagpipe {
193 start_bagpipe_bgp
194 start_bagpipe_fakerr
195 }
196
197 function stop_bagpipe_bgp {
198 stop_process neutron-bagpipe-bgp
199 }
200
201 function stop_bagpipe_fakerr {
202 stop_process neutron-bagpipe-fakerr
203 }
204
205 # Stop running processes
206 function stop_bagpipe {
207 stop_bagpipe_bgp
208 stop_bagpipe_fakerr
209 }
210
211 # Remove residual data files, anything left over from previous runs that a
212 # clean run would need to clean up
213 function cleanup_bagpipe {
214 if is_service_enabled neutron-bagpipe-bgp ; then
215 sudo bagpipe-bgp-cleanup
216
217 MPLS_IFACE_IP=`ip addr show $BAGPIPE_INTERNAL_PORT | grep 'inet ' | awk '{ print $2 }'`
218 GW_IP=`ip route | grep default | awk '{ print $3 }'`
219 sudo ovs-vsctl del-br $BAGPIPE_MPLS_BR
220 sudo ip addr add $MPLS_IFACE_IP dev $BAGPIPE_MPLS_IFACE
221 sudo ip route add default via $GW_IP
222 fi
223 }
224
225 if [[ "$1" == "source" ]]; then
226 # no-op
227 :
228 elif [[ "$1" == "stack" && "$2" == "pre-install" ]]; then
229 if is_service_enabled gobgp ; then
230 echo_summary "Installing golang package"
231 pre_install_gobgp
232 fi
233 elif [[ "$1" == "stack" && "$2" == "install" ]]; then
234 if is_service_enabled gobgp ; then
235 echo_summary "Installing GoBGP"
236 install_gobgp
237 fi
238 elif [[ "$1" == "stack" && "$2" == "post-config" ]]; then
239 echo_summary "Configuring Bagpipe"
240 configure_bagpipe
241 # Initializing before neutron starts is needed, so that we can move the single interface in br-mpls
242 init_bagpipe
243 elif [[ "$1" == "stack" && "$2" == "extra" ]]; then
244 echo_summary "Starting Bagpipe"
245 start_bagpipe
246 fi
247 if [[ "$1" == "unstack" ]]; then
248 echo_summary "Stopping Bagpipe"
249 stop_bagpipe
250 cleanup_bagpipe
251 fi
252 if [[ "$1" == "clean" ]]; then
253 cleanup_bagpipe
254 fi
255
256 # restore xtrace state
257 ${_XTRACE_BAGPIPE_BGP}
258