Skip to content

Commit d612463

Browse files
author
Brian Rak
committed
Do not route workload and frontend networks via the gateway
If a Gateway does not allow hairpin traffic, it may drop our packets. Instead, if the remote is accessible via link via ARP, (which it is on the workload network and the frontend networks) route those packets via the device instead.
1 parent 0b4927c commit d612463

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

ansible/roles/cloudinit/files/var/lib/vmware/ovf-to-cloud-init.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,10 @@ writeRouteTableConfig() {
330330
mac=$(getMacForNetwork "$network")
331331
ip=$(ovf-rpctool get.ovf "${3}")
332332
if [ "$ip" != "" ] && [ "$ip" != "null" ]; then
333+
# Default Gateway
333334
echo "${id},${network},${mac},${ip},${gateway}" >> "/etc/vmware/route-tables.cfg"
335+
# Link-scoped route
336+
echo "${id},${network},${mac},${ip}" >> "/etc/vmware/route-tables.cfg"
334337
fi
335338
}
336339

ansible/roles/vmware/files/var/lib/vmware/routetablectl.sh

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,30 @@ function up_routes() {
138138
cfg_table_name="${line_parts[1]}"
139139
cfg_mac_addr="${line_parts[2]}"
140140
cfg_cidr="${line_parts[3]}"
141-
cfg_gateway="${line_parts[4]}"
141+
cfg_gateway=""
142+
143+
if [[ ${#line_parts[@]} == 5 ]]; then
144+
cfg_gateway="${line_parts[4]}"
145+
fi
146+
142147
cfg_dev="$(dev_from_mac "${cfg_mac_addr}")"
143148
route_table_name="${RT_TABLE_NAME_PREFIX}${cfg_table_name}"
144149

145150
# Create a new route table.
146151
echo2 "create new route table id=${cfg_table_id} name=${route_table_name}"
147152
printf '%d\t%s\n' "${cfg_table_id}" "${route_table_name}" >>"${RT_TABLES_FILE}"
148153

149-
# Create default route for new route table.
150-
echo2 "create default route for ${route_table_name}"
151-
ip route add table "${route_table_name}" default via "${cfg_gateway}" dev "${cfg_dev}" proto static
154+
if [[ "${cfg_gateway}" == "" ]]; then
155+
cfg_destination=$(python3 -c "import sys; import ipaddress; print(ipaddress.ip_network(sys.argv[1], strict=False))" "${cfg_cidr}")
156+
host="$(echo "${cfg_cidr}" | cut -d/ -f 1)"
157+
cmd="ip route add table ${route_table_name} ${cfg_destination} dev ${cfg_dev} proto kernel scope link src ${host}"
158+
echo2 "create route with cmd: ${cmd}"
159+
eval "${cmd}"
160+
else
161+
# Create default route for new route table.
162+
echo2 "create default route for ${route_table_name}"
163+
ip route add table "${route_table_name}" default via "${cfg_gateway}" dev "${cfg_dev}" proto static
164+
fi
152165

153166
# Create IP rule for new route table.
154167
echo2 "create IP rule for ${route_table_name}"

hack/test-route-programs.sh

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,10 @@ function net_mac_addr() {
102102
docker network inspect --format='{{range .Containers}}{{.MacAddress}}{{end}}' "${1}"
103103
}
104104

105+
function net_ip() {
106+
docker network inspect --format='{{range .Containers}}{{.IPv4Address}}{{end}}' "${1}"
107+
}
108+
105109
# Creates a Docker network if it does not exist.
106110
function net_create() {
107111
if [ -z "$(docker network ls -qf "Name=${1}")" ]; then
@@ -189,6 +193,9 @@ function start_haproxy() {
189193
#DOCKER_NET_1_MAC="$(net_mac_addr "${DOCKER_NET_1}")"
190194
DOCKER_NET_2_MAC="$(net_mac_addr "${DOCKER_NET_2}")"
191195
DOCKER_NET_3_MAC="$(net_mac_addr "${DOCKER_NET_3}")"
196+
197+
DOCKER_IP_NET_2="$(net_ip "${DOCKER_NET_2}")"
198+
DOCKER_IP_NET_3="$(net_ip "${DOCKER_NET_3}")"
192199
fi
193200
}
194201

@@ -311,7 +318,7 @@ function test_routetablectl() {
311318
test_prereqs
312319

313320
# Create the config file.
314-
# <TableID>,<TableName>,<MACAddress>,<NetworkCIDR>,<Gateway4>
321+
# <TableID>,<TableName>,<MACAddress>,<Network IP (CIDR format)>,<Gateway4>
315322
TEMP_TEST=".$(date "+%s")"
316323
cat <<EOF >"${TEMP_TEST}"
317324
2,frontend,${DOCKER_NET_2_MAC},${DOCKER_NET_2_CIDR},${DOCKER_NET_2_GATEWAY}
@@ -331,8 +338,10 @@ set -o pipefail
331338
# Create the config file.
332339
# <TableID>,<TableName>,<MACAddress>,<NetworkCIDR>,<Gateway4>
333340
cat <<EOD >/etc/vmware/route-tables.cfg
334-
2,frontend,${DOCKER_NET_2_MAC},${DOCKER_NET_2_CIDR},${DOCKER_NET_2_GATEWAY}
335-
3,workload,${DOCKER_NET_3_MAC},${DOCKER_NET_3_CIDR},${DOCKER_NET_3_GATEWAY}
341+
2,frontend,${DOCKER_NET_2_MAC},${DOCKER_IP_NET_2},${DOCKER_NET_2_GATEWAY}
342+
3,workload,${DOCKER_NET_3_MAC},${DOCKER_IP_NET_3},${DOCKER_NET_3_GATEWAY}
343+
2,frontend,${DOCKER_NET_2_MAC},${DOCKER_IP_NET_2}
344+
3,workload,${DOCKER_NET_3_MAC},${DOCKER_IP_NET_3}
336345
EOD
337346
338347
# Run the program with a populated config file and expect no errors.

0 commit comments

Comments
 (0)