blob: 6d518e25d857bebce22859848de8b202ffbf51e6 [file] [log] [blame]
Sean M. Collins2a242512016-05-03 09:03:09 -04001#!/bin/bash
2# Subnet IP version
3IP_VERSION=${IP_VERSION:-"4+6"}
4# Validate IP_VERSION
5if [[ $IP_VERSION != "4" ]] && [[ $IP_VERSION != "6" ]] && [[ $IP_VERSION != "4+6" ]]; then
6 die $LINENO "IP_VERSION must be either 4, 6, or 4+6"
7fi
8# Specify if the initial private and external networks should be created
9NEUTRON_CREATE_INITIAL_NETWORKS=${NEUTRON_CREATE_INITIAL_NETWORKS:-True}
10
11## Provider Network Information
12PROVIDER_SUBNET_NAME=${PROVIDER_SUBNET_NAME:-"provider_net"}
13IPV6_PROVIDER_SUBNET_NAME=${IPV6_PROVIDER_SUBNET_NAME:-"provider_net_v6"}
14IPV6_PROVIDER_FIXED_RANGE=${IPV6_PROVIDER_FIXED_RANGE:-}
15IPV6_PROVIDER_NETWORK_GATEWAY=${IPV6_PROVIDER_NETWORK_GATEWAY:-}
16
17PUBLIC_BRIDGE=${PUBLIC_BRIDGE:-br-ex}
Ihar Hrachyshka7b5c7dc2016-07-15 20:17:13 +020018PUBLIC_BRIDGE_MTU=${PUBLIC_BRIDGE_MTU:-1500}
Sean M. Collins2a242512016-05-03 09:03:09 -040019
Kevin Benton1554ade2016-07-22 09:40:19 -070020# If Q_ASSIGN_GATEWAY_TO_PUBLIC_BRIDGE=True, assign the gateway IP of the public
21# subnet to the public bridge interface even if Q_USE_PROVIDERNET_FOR_PUBLIC is
22# used.
23Q_ASSIGN_GATEWAY_TO_PUBLIC_BRIDGE=${Q_ASSIGN_GATEWAY_TO_PUBLIC_BRIDGE:-True}
24
YAMAMOTO Takashi1aa43682016-07-21 19:37:04 +090025# The name of the default router
26Q_ROUTER_NAME=${Q_ROUTER_NAME:-router1}
27
Sean M. Collins2a242512016-05-03 09:03:09 -040028# If Q_USE_PUBLIC_VETH=True, create and use a veth pair instead of
29# PUBLIC_BRIDGE. This is intended to be used with
30# Q_USE_PROVIDERNET_FOR_PUBLIC=True.
31Q_USE_PUBLIC_VETH=${Q_USE_PUBLIC_VETH:-False}
32Q_PUBLIC_VETH_EX=${Q_PUBLIC_VETH_EX:-veth-pub-ex}
33Q_PUBLIC_VETH_INT=${Q_PUBLIC_VETH_INT:-veth-pub-int}
34
vsaienkod8942212016-05-13 12:51:30 +030035# The next variable is configured by plugin
Sean M. Collins2a242512016-05-03 09:03:09 -040036# e.g. _configure_neutron_l3_agent or lib/neutron_plugins/*
37#
Sean M. Collins2a242512016-05-03 09:03:09 -040038# L3 routers exist per tenant
39Q_L3_ROUTER_PER_TENANT=${Q_L3_ROUTER_PER_TENANT:-True}
40
41
42# Use flat providernet for public network
43#
44# If Q_USE_PROVIDERNET_FOR_PUBLIC=True, use a flat provider network
45# for external interface of neutron l3-agent. In that case,
46# PUBLIC_PHYSICAL_NETWORK specifies provider:physical_network value
47# used for the network. In case of ofagent, you should add the
48# corresponding entry to your OFAGENT_PHYSICAL_INTERFACE_MAPPINGS.
49# For openvswitch agent, you should add the corresponding entry to
50# your OVS_BRIDGE_MAPPINGS.
51#
52# eg. (ofagent)
53# Q_USE_PROVIDERNET_FOR_PUBLIC=True
54# Q_USE_PUBLIC_VETH=True
55# PUBLIC_PHYSICAL_NETWORK=public
56# OFAGENT_PHYSICAL_INTERFACE_MAPPINGS=public:veth-pub-int
57#
58# eg. (openvswitch agent)
59# Q_USE_PROVIDERNET_FOR_PUBLIC=True
60# PUBLIC_PHYSICAL_NETWORK=public
61# OVS_BRIDGE_MAPPINGS=public:br-ex
Kevin Benton1554ade2016-07-22 09:40:19 -070062Q_USE_PROVIDERNET_FOR_PUBLIC=${Q_USE_PROVIDERNET_FOR_PUBLIC:-True}
Sean M. Collins2a242512016-05-03 09:03:09 -040063PUBLIC_PHYSICAL_NETWORK=${PUBLIC_PHYSICAL_NETWORK:-public}
64
65# Generate 40-bit IPv6 Global ID to comply with RFC 4193
66IPV6_GLOBAL_ID=`uuidgen | sed s/-//g | cut -c 23- | sed -e "s/\(..\)\(....\)\(....\)/\1:\2:\3/"`
67
68# IPv6 gateway and subnet defaults, in case they are not customized in localrc
69IPV6_RA_MODE=${IPV6_RA_MODE:-slaac}
70IPV6_ADDRESS_MODE=${IPV6_ADDRESS_MODE:-slaac}
71IPV6_PUBLIC_SUBNET_NAME=${IPV6_PUBLIC_SUBNET_NAME:-ipv6-public-subnet}
72IPV6_PRIVATE_SUBNET_NAME=${IPV6_PRIVATE_SUBNET_NAME:-ipv6-private-subnet}
Kevin Benton4bfbc292016-11-15 17:26:05 -080073IPV6_ADDRS_SAFE_TO_USE=${IPV6_ADDRS_SAFE_TO_USE:-fd$IPV6_GLOBAL_ID::/56}
74# if we got larger than a /64 safe to use, we only use the first /64 to
75# avoid side effects outlined in rfc7421
Clark Boylana5afa7d2016-11-18 12:32:19 -080076FIXED_RANGE_V6=${FIXED_RANGE_V6:-$(echo $IPV6_ADDRS_SAFE_TO_USE | awk -F '/' '{ print $1"/"($2>63 ? $2 : 64) }')}
Brian Haley31813e92016-08-22 15:39:22 -040077IPV6_PRIVATE_NETWORK_GATEWAY=${IPV6_PRIVATE_NETWORK_GATEWAY:-}
Sean M. Collins2a242512016-05-03 09:03:09 -040078IPV6_PUBLIC_RANGE=${IPV6_PUBLIC_RANGE:-2001:db8::/64}
79IPV6_PUBLIC_NETWORK_GATEWAY=${IPV6_PUBLIC_NETWORK_GATEWAY:-2001:db8::2}
80IPV6_ROUTER_GW_IP=${IPV6_ROUTER_GW_IP:-2001:db8::1}
81
82# Gateway and subnet defaults, in case they are not customized in localrc
Brian Haley31813e92016-08-22 15:39:22 -040083NETWORK_GATEWAY=${NETWORK_GATEWAY:-}
84PUBLIC_NETWORK_GATEWAY=${PUBLIC_NETWORK_GATEWAY:-}
Sean M. Collins2a242512016-05-03 09:03:09 -040085PRIVATE_SUBNET_NAME=${PRIVATE_SUBNET_NAME:-"private-subnet"}
86PUBLIC_SUBNET_NAME=${PUBLIC_SUBNET_NAME:-"public-subnet"}
87
88# Subnetpool defaults
rajinirc58a1552016-09-27 17:14:59 -050089USE_SUBNETPOOL=${USE_SUBNETPOOL:-True}
Sean M. Collins2a242512016-05-03 09:03:09 -040090SUBNETPOOL_NAME=${SUBNETPOOL_NAME:-"shared-default-subnetpool"}
91
Kevin Benton4bfbc292016-11-15 17:26:05 -080092SUBNETPOOL_PREFIX_V4=${SUBNETPOOL_PREFIX_V4:-$IPV4_ADDRS_SAFE_TO_USE}
93SUBNETPOOL_PREFIX_V6=${SUBNETPOOL_PREFIX_V6:-$IPV6_ADDRS_SAFE_TO_USE}
Sean M. Collins2a242512016-05-03 09:03:09 -040094
Kevin Benton4bfbc292016-11-15 17:26:05 -080095SUBNETPOOL_SIZE_V4=${SUBNETPOOL_SIZE_V4:-26}
Sean M. Collins2a242512016-05-03 09:03:09 -040096SUBNETPOOL_SIZE_V6=${SUBNETPOOL_SIZE_V6:-64}
97
Henry Gessau734f1442016-09-17 19:28:53 -040098default_v4_route_devs=$(ip -4 route | grep ^default | awk '{print $5}')
99die_if_not_set $LINENO default_v4_route_devs "Failure retrieving default IPv4 route devices"
100
101default_v6_route_devs=$(ip -6 route | grep ^default | awk '{print $5}')
Monty Taylorc12d1d92016-08-23 19:07:57 -0500102
Sean M. Collins2a242512016-05-03 09:03:09 -0400103function _determine_config_l3 {
Angus Leesa1c70f22016-05-31 14:43:14 +1000104 local opts="--config-file $NEUTRON_CONF --config-file $Q_L3_CONF_FILE"
Sean M. Collins2a242512016-05-03 09:03:09 -0400105 echo "$opts"
106}
107
108function _configure_neutron_l3_agent {
Sean M. Collins2a242512016-05-03 09:03:09 -0400109
110 cp $NEUTRON_DIR/etc/l3_agent.ini.sample $Q_L3_CONF_FILE
111
Sean M. Collins2a242512016-05-03 09:03:09 -0400112 iniset $Q_L3_CONF_FILE DEFAULT debug $ENABLE_DEBUG_LOG_LEVEL
Sean M. Collinsa2ed0552016-05-11 15:35:10 -0400113 iniset $Q_L3_CONF_FILE AGENT root_helper "$Q_RR_COMMAND"
Sean M. Collins2a242512016-05-03 09:03:09 -0400114 if [[ "$Q_USE_ROOTWRAP_DAEMON" == "True" ]]; then
Sean M. Collinsa2ed0552016-05-11 15:35:10 -0400115 iniset $Q_L3_CONF_FILE AGENT root_helper_daemon "$Q_RR_DAEMON_COMMAND"
Sean M. Collins2a242512016-05-03 09:03:09 -0400116 fi
117
118 _neutron_setup_interface_driver $Q_L3_CONF_FILE
119
Stephen Finucane24e29f22016-06-15 14:31:51 +0100120 neutron_plugin_configure_l3_agent $Q_L3_CONF_FILE
Sean M. Collins2a242512016-05-03 09:03:09 -0400121
Sean Dague6a008fa2016-08-03 15:09:01 -0400122 # If we've given a PUBLIC_INTERFACE to take over, then we assume
123 # that we can own the whole thing, and privot it into the OVS
124 # bridge. If we are not, we're probably on a single interface
125 # machine, and we just setup NAT so that fixed guests can get out.
126 if [[ -n "$PUBLIC_INTERFACE" ]]; then
127 _move_neutron_addresses_route "$PUBLIC_INTERFACE" "$OVS_PHYSICAL_BRIDGE" True False "inet"
Sean M. Collins2a242512016-05-03 09:03:09 -0400128
Sean Dague6a008fa2016-08-03 15:09:01 -0400129 if [[ $(ip -f inet6 a s dev "$PUBLIC_INTERFACE" | grep -c 'global') != 0 ]]; then
130 _move_neutron_addresses_route "$PUBLIC_INTERFACE" "$OVS_PHYSICAL_BRIDGE" False False "inet6"
131 fi
132 else
Henry Gessau734f1442016-09-17 19:28:53 -0400133 for d in $default_v4_route_devs; do
134 sudo iptables -t nat -A POSTROUTING -o $d -s $FLOATING_RANGE -j MASQUERADE
135 done
Sean M. Collins2a242512016-05-03 09:03:09 -0400136 fi
137}
138
139# Explicitly set router id in l3 agent configuration
140function _neutron_set_router_id {
141 if [[ "$Q_L3_ROUTER_PER_TENANT" == "False" ]]; then
142 iniset $Q_L3_CONF_FILE DEFAULT router_id $ROUTER_ID
143 fi
144}
145
146# Get ext_gw_interface depending on value of Q_USE_PUBLIC_VETH
147function _neutron_get_ext_gw_interface {
148 if [[ "$Q_USE_PUBLIC_VETH" == "True" ]]; then
149 echo $Q_PUBLIC_VETH_EX
150 else
151 # Disable in-band as we are going to use local port
152 # to communicate with VMs
153 sudo ovs-vsctl set Bridge $PUBLIC_BRIDGE \
154 other_config:disable-in-band=true
155 echo $PUBLIC_BRIDGE
156 fi
157}
158
159function create_neutron_initial_network {
Gary Kotton88f85582016-08-14 06:55:42 -0700160 if ! is_service_enabled q-svc && ! is_service_enabled neutron-api; then
161 echo "Controller services not enabled. No networks configured!"
162 return
163 fi
164 if [[ "$NEUTRON_CREATE_INITIAL_NETWORKS" == "False" ]]; then
165 echo "Network creation disabled!"
166 return
167 fi
Sean M. Collins2a242512016-05-03 09:03:09 -0400168 local project_id
169 project_id=$(openstack project list | grep " demo " | get_field 1)
170 die_if_not_set $LINENO project_id "Failure retrieving project_id for demo"
171
172 # Allow drivers that need to create an initial network to do so here
173 if type -p neutron_plugin_create_initial_network_profile > /dev/null; then
174 neutron_plugin_create_initial_network_profile $PHYSICAL_NETWORK
175 fi
176
Matt Van Dijkd7a3f5c2016-08-16 15:46:58 +0000177 if is_networking_extension_supported "auto-allocated-topology"; then
rajinirc58a1552016-09-27 17:14:59 -0500178 if [[ "$USE_SUBNETPOOL" == "True" ]]; then
179 if [[ "$IP_VERSION" =~ 4.* ]]; then
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700180 SUBNETPOOL_V4_ID=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" subnet pool create $SUBNETPOOL_NAME --default-prefix-length $SUBNETPOOL_SIZE_V4 --pool-prefix $SUBNETPOOL_PREFIX_V4 --share --default | grep ' id ' | get_field 2)
rajinirc58a1552016-09-27 17:14:59 -0500181 fi
182 if [[ "$IP_VERSION" =~ .*6 ]]; then
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700183 SUBNETPOOL_V6_ID=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" subnet pool create $SUBNETPOOL_NAME --default-prefix-length $SUBNETPOOL_SIZE_V6 --pool-prefix $SUBNETPOOL_PREFIX_V6 --share --default | grep ' id ' | get_field 2)
rajinirc58a1552016-09-27 17:14:59 -0500184 fi
Matt Van Dijkd7a3f5c2016-08-16 15:46:58 +0000185 fi
186 fi
187
Sean M. Collins2a242512016-05-03 09:03:09 -0400188 if is_provider_network; then
189 die_if_not_set $LINENO PHYSICAL_NETWORK "You must specify the PHYSICAL_NETWORK"
190 die_if_not_set $LINENO PROVIDER_NETWORK_TYPE "You must specify the PROVIDER_NETWORK_TYPE"
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700191 NET_ID=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" network create $PHYSICAL_NETWORK --project $project_id --provider-network-type $PROVIDER_NETWORK_TYPE --provider-physical-network "$PHYSICAL_NETWORK" ${SEGMENTATION_ID:+--provider-segment $SEGMENTATION_ID} --share | grep ' id ' | get_field 2)
Sean M. Collins2a242512016-05-03 09:03:09 -0400192 die_if_not_set $LINENO NET_ID "Failure creating NET_ID for $PHYSICAL_NETWORK $project_id"
193
194 if [[ "$IP_VERSION" =~ 4.* ]]; then
Matt Van Dijkd7a3f5c2016-08-16 15:46:58 +0000195 if [ -z $SUBNETPOOL_V4_ID ]; then
196 fixed_range_v4=$FIXED_RANGE
197 fi
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700198 SUBNET_ID=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" subnet create --project $project_id --ip-version 4 ${ALLOCATION_POOL:+--allocation-pool $ALLOCATION_POOL} $PROVIDER_SUBNET_NAME --gateway $NETWORK_GATEWAY ${SUBNETPOOL_V4_ID:+--subnet-pool $SUBNETPOOL_V4_ID} --network $NET_ID --subnet-range $fixed_range_v4 | grep ' id ' | get_field 2)
Sean M. Collins2a242512016-05-03 09:03:09 -0400199 die_if_not_set $LINENO SUBNET_ID "Failure creating SUBNET_ID for $PROVIDER_SUBNET_NAME $project_id"
200 fi
201
Sean M. Collinse34ec992016-06-07 12:36:50 -0400202 if [[ "$IP_VERSION" =~ .*6 ]]; then
203 die_if_not_set $LINENO IPV6_PROVIDER_FIXED_RANGE "IPV6_PROVIDER_FIXED_RANGE has not been set, but Q_USE_PROVIDERNET_FOR_PUBLIC is true and IP_VERSION includes 6"
204 die_if_not_set $LINENO IPV6_PROVIDER_NETWORK_GATEWAY "IPV6_PROVIDER_NETWORK_GATEWAY has not been set, but Q_USE_PROVIDERNET_FOR_PUBLIC is true and IP_VERSION includes 6"
Matt Van Dijkd7a3f5c2016-08-16 15:46:58 +0000205 if [ -z $SUBNETPOOL_V6_ID ]; then
206 fixed_range_v6=$IPV6_PROVIDER_FIXED_RANGE
207 fi
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700208 SUBNET_V6_ID=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" subnet create --project $project_id --ip-version 6 --ipv6-address-mode $IPV6_ADDRESS_MODE --gateway $IPV6_PROVIDER_NETWORK_GATEWAY $IPV6_PROVIDER_SUBNET_NAME ${SUBNETPOOL_V6_ID:+--subnet-pool $SUBNETPOOL_V6_ID} --network $NET_ID $fixed_range_v6 | grep 'id' | get_field 2)
Sean M. Collins2a242512016-05-03 09:03:09 -0400209 die_if_not_set $LINENO SUBNET_V6_ID "Failure creating SUBNET_V6_ID for $IPV6_PROVIDER_SUBNET_NAME $project_id"
210 fi
211
212 if [[ $Q_AGENT == "openvswitch" ]]; then
213 sudo ip link set $OVS_PHYSICAL_BRIDGE up
214 sudo ip link set br-int up
215 sudo ip link set $PUBLIC_INTERFACE up
216 fi
217 else
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700218 NET_ID=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" network create --project $project_id "$PRIVATE_NETWORK_NAME" | grep ' id ' | get_field 2)
Sean M. Collins2a242512016-05-03 09:03:09 -0400219 die_if_not_set $LINENO NET_ID "Failure creating NET_ID for $PRIVATE_NETWORK_NAME $project_id"
220
221 if [[ "$IP_VERSION" =~ 4.* ]]; then
222 # Create IPv4 private subnet
223 SUBNET_ID=$(_neutron_create_private_subnet_v4 $project_id)
224 fi
225
226 if [[ "$IP_VERSION" =~ .*6 ]]; then
227 # Create IPv6 private subnet
228 IPV6_SUBNET_ID=$(_neutron_create_private_subnet_v6 $project_id)
229 fi
230 fi
231
Sean M. Collinsc35110e2016-05-18 10:38:51 -0400232 if is_networking_extension_supported "router" && is_networking_extension_supported "external-net"; then
Sean M. Collins2a242512016-05-03 09:03:09 -0400233 # Create a router, and add the private subnet as one of its interfaces
234 if [[ "$Q_L3_ROUTER_PER_TENANT" == "True" ]]; then
235 # create a tenant-owned router.
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700236 ROUTER_ID=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" router create --project $project_id $Q_ROUTER_NAME | grep ' id ' | get_field 2)
Sean M. Collins2a242512016-05-03 09:03:09 -0400237 die_if_not_set $LINENO ROUTER_ID "Failure creating ROUTER_ID for $project_id $Q_ROUTER_NAME"
238 else
239 # Plugin only supports creating a single router, which should be admin owned.
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700240 ROUTER_ID=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" router create $Q_ROUTER_NAME | grep ' id ' | get_field 2)
Sean M. Collins2a242512016-05-03 09:03:09 -0400241 die_if_not_set $LINENO ROUTER_ID "Failure creating ROUTER_ID for $Q_ROUTER_NAME"
242 fi
243
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700244 EXTERNAL_NETWORK_FLAGS="--external"
Matt Van Dijkd7a3f5c2016-08-16 15:46:58 +0000245 if is_networking_extension_supported "auto-allocated-topology"; then
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700246 EXTERNAL_NETWORK_FLAGS="$EXTERNAL_NETWORK_FLAGS --default"
Sean M. Collins2a242512016-05-03 09:03:09 -0400247 fi
248 # Create an external network, and a subnet. Configure the external network as router gw
249 if [ "$Q_USE_PROVIDERNET_FOR_PUBLIC" = "True" ]; then
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700250 EXT_NET_ID=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" network create "$PUBLIC_NETWORK_NAME" $EXTERNAL_NETWORK_FLAGS --provider-network-type flat --provider-physical-network ${PUBLIC_PHYSICAL_NETWORK} | grep ' id ' | get_field 2)
Sean M. Collins2a242512016-05-03 09:03:09 -0400251 else
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700252 EXT_NET_ID=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" network create "$PUBLIC_NETWORK_NAME" $EXTERNAL_NETWORK_FLAGS | grep ' id ' | get_field 2)
Sean M. Collins2a242512016-05-03 09:03:09 -0400253 fi
254 die_if_not_set $LINENO EXT_NET_ID "Failure creating EXT_NET_ID for $PUBLIC_NETWORK_NAME"
255
256 if [[ "$IP_VERSION" =~ 4.* ]]; then
257 # Configure router for IPv4 public access
258 _neutron_configure_router_v4
259 fi
260
261 if [[ "$IP_VERSION" =~ .*6 ]]; then
262 # Configure router for IPv6 public access
263 _neutron_configure_router_v6
264 fi
265 fi
266}
267
268# Create private IPv4 subnet
269function _neutron_create_private_subnet_v4 {
270 local project_id=$1
Matt Van Dijkd7a3f5c2016-08-16 15:46:58 +0000271 if [ -z $SUBNETPOOL_V4_ID ]; then
272 fixed_range_v4=$FIXED_RANGE
273 fi
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700274 local subnet_params="--project $project_id "
275 subnet_params+="--ip-version 4 "
Brian Haley31813e92016-08-22 15:39:22 -0400276 if [[ -n "$NETWORK_GATEWAY" ]]; then
277 subnet_params+="--gateway $NETWORK_GATEWAY "
278 fi
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700279 subnet_params+="${SUBNETPOOL_V4_ID:+--subnet-pool $SUBNETPOOL_V4_ID} "
280 subnet_params+="${fixed_range_v4:+--subnet-range $fixed_range_v4} "
281 subnet_params+="--network $NET_ID $PRIVATE_SUBNET_NAME"
Sean M. Collins2a242512016-05-03 09:03:09 -0400282 local subnet_id
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700283 subnet_id=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" subnet create $subnet_params | grep ' id ' | get_field 2)
Sean M. Collins2a242512016-05-03 09:03:09 -0400284 die_if_not_set $LINENO subnet_id "Failure creating private IPv4 subnet for $project_id"
285 echo $subnet_id
286}
287
288# Create private IPv6 subnet
289function _neutron_create_private_subnet_v6 {
290 local project_id=$1
291 die_if_not_set $LINENO IPV6_RA_MODE "IPV6 RA Mode not set"
292 die_if_not_set $LINENO IPV6_ADDRESS_MODE "IPV6 Address Mode not set"
293 local ipv6_modes="--ipv6-ra-mode $IPV6_RA_MODE --ipv6-address-mode $IPV6_ADDRESS_MODE"
Matt Van Dijkd7a3f5c2016-08-16 15:46:58 +0000294 if [ -z $SUBNETPOOL_V6_ID ]; then
295 fixed_range_v6=$FIXED_RANGE_V6
296 fi
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700297 local subnet_params="--project $project_id "
298 subnet_params+="--ip-version 6 "
Brian Haley31813e92016-08-22 15:39:22 -0400299 if [[ -n "$IPV6_PRIVATE_NETWORK_GATEWAY" ]]; then
300 subnet_params+="--gateway $IPV6_PRIVATE_NETWORK_GATEWAY "
301 fi
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700302 subnet_params+="${SUBNETPOOL_V6_ID:+--subnet-pool $SUBNETPOOL_V6_ID} "
303 subnet_params+="${fixed_range_v6:+--subnet-range $fixed_range_v6 $ipv6_modes} "
304 subnet_params+="--network $NET_ID $IPV6_PRIVATE_SUBNET_NAME "
Sean M. Collins2a242512016-05-03 09:03:09 -0400305 local ipv6_subnet_id
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700306 ipv6_subnet_id=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" subnet create $subnet_params | grep ' id ' | get_field 2)
Sean M. Collins2a242512016-05-03 09:03:09 -0400307 die_if_not_set $LINENO ipv6_subnet_id "Failure creating private IPv6 subnet for $project_id"
308 echo $ipv6_subnet_id
309}
310
311# Create public IPv4 subnet
312function _neutron_create_public_subnet_v4 {
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700313 local subnet_params="--ip-version 4 "
Sean M. Collins2a242512016-05-03 09:03:09 -0400314 subnet_params+="${Q_FLOATING_ALLOCATION_POOL:+--allocation-pool $Q_FLOATING_ALLOCATION_POOL} "
Brian Haley31813e92016-08-22 15:39:22 -0400315 if [[ -n "$PUBLIC_NETWORK_GATEWAY" ]]; then
316 subnet_params+="--gateway $PUBLIC_NETWORK_GATEWAY "
317 fi
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700318 subnet_params+="--network $EXT_NET_ID --subnet-range $FLOATING_RANGE --no-dhcp "
319 subnet_params+="$PUBLIC_SUBNET_NAME"
Sean M. Collins2a242512016-05-03 09:03:09 -0400320 local id_and_ext_gw_ip
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700321 id_and_ext_gw_ip=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" subnet create $subnet_params | grep -e 'gateway_ip' -e ' id ')
Sean M. Collins2a242512016-05-03 09:03:09 -0400322 die_if_not_set $LINENO id_and_ext_gw_ip "Failure creating public IPv4 subnet"
323 echo $id_and_ext_gw_ip
324}
325
326# Create public IPv6 subnet
327function _neutron_create_public_subnet_v6 {
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700328 local subnet_params="--ip-version 6 "
Sean M. Collins2a242512016-05-03 09:03:09 -0400329 subnet_params+="--gateway $IPV6_PUBLIC_NETWORK_GATEWAY "
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700330 subnet_params+="--network $EXT_NET_ID --subnet-range $IPV6_PUBLIC_RANGE --no-dhcp "
331 subnet_params+="$IPV6_PUBLIC_SUBNET_NAME"
Sean M. Collins2a242512016-05-03 09:03:09 -0400332 local ipv6_id_and_ext_gw_ip
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700333 ipv6_id_and_ext_gw_ip=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" subnet create $subnet_params | grep -e 'gateway_ip' -e ' id ')
Sean M. Collins2a242512016-05-03 09:03:09 -0400334 die_if_not_set $LINENO ipv6_id_and_ext_gw_ip "Failure creating an IPv6 public subnet"
335 echo $ipv6_id_and_ext_gw_ip
336}
337
338# Configure neutron router for IPv4 public access
339function _neutron_configure_router_v4 {
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700340 openstack --os-cloud devstack-admin --os-region "$REGION_NAME" router add subnet $ROUTER_ID $SUBNET_ID
Sean M. Collins2a242512016-05-03 09:03:09 -0400341 # Create a public subnet on the external network
342 local id_and_ext_gw_ip
343 id_and_ext_gw_ip=$(_neutron_create_public_subnet_v4 $EXT_NET_ID)
344 local ext_gw_ip
345 ext_gw_ip=$(echo $id_and_ext_gw_ip | get_field 2)
346 PUB_SUBNET_ID=$(echo $id_and_ext_gw_ip | get_field 5)
347 # Configure the external network as the default router gateway
348 neutron --os-cloud devstack-admin --os-region "$REGION_NAME" router-gateway-set $ROUTER_ID $EXT_NET_ID
349
350 # This logic is specific to using the l3-agent for layer 3
Sean M. Collinsd00cbb72016-06-20 13:53:44 -0400351 if is_service_enabled q-l3 || is_service_enabled neutron-l3; then
Sean M. Collins2a242512016-05-03 09:03:09 -0400352 # Configure and enable public bridge
353 local ext_gw_interface="none"
354 if is_neutron_ovs_base_plugin; then
355 ext_gw_interface=$(_neutron_get_ext_gw_interface)
356 elif [[ "$Q_AGENT" = "linuxbridge" ]]; then
Kevin Benton6a42a852016-07-21 11:11:54 -0700357 # Get the device the neutron router and network for $FIXED_RANGE
Sean M. Collins2a242512016-05-03 09:03:09 -0400358 # will be using.
Kevin Benton6a42a852016-07-21 11:11:54 -0700359 if [ "$Q_USE_PROVIDERNET_FOR_PUBLIC" = "True" ]; then
360 # in provider nets a bridge mapping uses the public bridge directly
361 ext_gw_interface=$PUBLIC_BRIDGE
362 else
363 # e.x. brq3592e767-da for NET_ID 3592e767-da66-4bcb-9bec-cdb03cd96102
364 ext_gw_interface=brq${EXT_NET_ID:0:11}
365 fi
Sean M. Collins2a242512016-05-03 09:03:09 -0400366 fi
367 if [[ "$ext_gw_interface" != "none" ]]; then
368 local cidr_len=${FLOATING_RANGE#*/}
369 local testcmd="ip -o link | grep -q $ext_gw_interface"
370 test_with_retry "$testcmd" "$ext_gw_interface creation failed"
Kevin Benton1554ade2016-07-22 09:40:19 -0700371 if [[ $(ip addr show dev $ext_gw_interface | grep -c $ext_gw_ip) == 0 && ( $Q_USE_PROVIDERNET_FOR_PUBLIC == "False" || $Q_USE_PUBLIC_VETH == "True" || $Q_ASSIGN_GATEWAY_TO_PUBLIC_BRIDGE == "True" ) ]]; then
Sean M. Collins2a242512016-05-03 09:03:09 -0400372 sudo ip addr add $ext_gw_ip/$cidr_len dev $ext_gw_interface
373 sudo ip link set $ext_gw_interface up
374 fi
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700375 ROUTER_GW_IP=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" port list -c 'Fixed IP Addresses' --device-owner network:router_gateway | awk -F'ip_address' '{ print $2 }' | cut -f2 -d\' | tr '\n' ' ')
Sean M. Collins2a242512016-05-03 09:03:09 -0400376 die_if_not_set $LINENO ROUTER_GW_IP "Failure retrieving ROUTER_GW_IP"
Sean M. Collins2a242512016-05-03 09:03:09 -0400377 fi
378 _neutron_set_router_id
379 fi
380}
381
382# Configure neutron router for IPv6 public access
383function _neutron_configure_router_v6 {
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700384 openstack --os-cloud devstack-admin --os-region "$REGION_NAME" router add subnet $ROUTER_ID $IPV6_SUBNET_ID
Sean M. Collins2a242512016-05-03 09:03:09 -0400385 # Create a public subnet on the external network
386 local ipv6_id_and_ext_gw_ip
387 ipv6_id_and_ext_gw_ip=$(_neutron_create_public_subnet_v6 $EXT_NET_ID)
388 local ipv6_ext_gw_ip
389 ipv6_ext_gw_ip=$(echo $ipv6_id_and_ext_gw_ip | get_field 2)
390 local ipv6_pub_subnet_id
391 ipv6_pub_subnet_id=$(echo $ipv6_id_and_ext_gw_ip | get_field 5)
392
393 # If the external network has not already been set as the default router
394 # gateway when configuring an IPv4 public subnet, do so now
395 if [[ "$IP_VERSION" == "6" ]]; then
396 neutron --os-cloud devstack-admin --os-region "$REGION_NAME" router-gateway-set $ROUTER_ID $EXT_NET_ID
397 fi
398
399 # This logic is specific to using the l3-agent for layer 3
Sean M. Collinsd00cbb72016-06-20 13:53:44 -0400400 if is_service_enabled q-l3 || is_service_enabled neutron-l3; then
Henry Gessau734f1442016-09-17 19:28:53 -0400401 # Ensure IPv6 RAs are accepted on interfaces with a default route.
Monty Taylorc12d1d92016-08-23 19:07:57 -0500402 # This is needed for neutron-based devstack clouds to work in
403 # IPv6-only clouds in the gate. Please do not remove this without
404 # talking to folks in Infra.
Henry Gessau734f1442016-09-17 19:28:53 -0400405 for d in $default_v6_route_devs; do
Drago Rossonb34d4592016-09-26 13:23:23 -0500406 # Slashes must be used in this sysctl command because route devices
407 # can have dots in their names. If dots were used, dots in the
408 # device name would be reinterpreted as a slash, causing an error.
409 sudo sysctl -w net/ipv6/conf/$d/accept_ra=2
Henry Gessau734f1442016-09-17 19:28:53 -0400410 done
Sean M. Collins2a242512016-05-03 09:03:09 -0400411 # Ensure IPv6 forwarding is enabled on the host
412 sudo sysctl -w net.ipv6.conf.all.forwarding=1
413 # Configure and enable public bridge
414 # Override global IPV6_ROUTER_GW_IP with the true value from neutron
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700415 IPV6_ROUTER_GW_IP=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" port list -c 'Fixed IP Addresses' | grep $ipv6_pub_subnet_id | awk -F'ip_address' '{ print $2 }' | cut -f2 -d\' | tr '\n' ' ')
Sean M. Collins2a242512016-05-03 09:03:09 -0400416 die_if_not_set $LINENO IPV6_ROUTER_GW_IP "Failure retrieving IPV6_ROUTER_GW_IP"
417
418 if is_neutron_ovs_base_plugin; then
419 local ext_gw_interface
420 ext_gw_interface=$(_neutron_get_ext_gw_interface)
421 local ipv6_cidr_len=${IPV6_PUBLIC_RANGE#*/}
422
423 # Configure interface for public bridge
Yi Zhaoa464ea72016-05-12 10:32:58 +0800424 sudo ip -6 addr replace $ipv6_ext_gw_ip/$ipv6_cidr_len dev $ext_gw_interface
Matt Van Dijkd7a3f5c2016-08-16 15:46:58 +0000425 local replace_range=${SUBNETPOOL_PREFIX_V6}
426 if [[ -z "${SUBNETPOOL_V6_ID}" ]]; then
427 replace_range=${FIXED_RANGE_V6}
428 fi
429 sudo ip -6 route replace $replace_range via $IPV6_ROUTER_GW_IP dev $ext_gw_interface
Sean M. Collins2a242512016-05-03 09:03:09 -0400430 fi
431 _neutron_set_router_id
432 fi
433}
watanabe.isao4f4d95a2016-05-12 20:35:20 +0900434
435function is_provider_network {
vsaienkod8942212016-05-13 12:51:30 +0300436 if [ "$Q_USE_PROVIDER_NETWORKING" == "True" ]; then
watanabe.isao4f4d95a2016-05-12 20:35:20 +0900437 return 0
438 fi
439 return 1
440}
Sean M. Collinsc35110e2016-05-18 10:38:51 -0400441
442function is_networking_extension_supported {
443 local extension=$1
444 # TODO(sc68cal) cache this instead of calling every time
Armando Migliaccio4f11ff32016-10-27 06:15:23 -0700445 EXT_LIST=$(openstack --os-cloud devstack-admin --os-region "$REGION_NAME" extension list --network -c Alias -f value)
Sean M. Collinsc35110e2016-05-18 10:38:51 -0400446 [[ $EXT_LIST =~ $extension ]] && return 0
447}