Skip to content

Commit d1c7232

Browse files
Support attaching multiple network interfaces to the same network card
Prior to this commit, the code had two assumptions: 1. Single network card instances can have only one network interface a. therefore, when there are more than one network interface, the code made a IMDS call to retrieve device index (network interface index), which is only available on instances with multiple network cards. Having a secondary network interface on single network card instance failed the code and caused instance launch failures. 2. Each network card can have only one network interface a. therefore, the route table is unique to each network card. Having multiple network interfaces on a network card confused the code and generated wrong route tables. To fix (1), this commit uses a fallback value 0 when retrieval of device index fails. To fix (2), this commit names the route tables in a way that is unique to network interface and network card. FYI: 1. Network card is the physical card. Network interface is the virtual concept. Each network card can have multiple network interfaces (which is AWS is 1 or 2) 2. "Network interface" is synonym to "device" Signed-off-by: Hanwen <[email protected]>
1 parent 9fe7e20 commit d1c7232

File tree

6 files changed

+37
-18
lines changed

6 files changed

+37
-18
lines changed

cookbooks/aws-parallelcluster-environment/files/amazon-2023/network_interfaces/configure_nw_interface.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ set -ex
44

55
if
66
[ -z "${DEVICE_NAME}" ] || # name of the device
7-
[ -z "${DEVICE_NUMBER}" ] || # number of the device
7+
[ -z "${DEVICE_NUMBER}" ] || # index of the device
8+
[ -z "${NETWORK_CARD_INDEX}" ] || # index of the network card
89
[ -z "${DEVICE_IP_ADDRESS}" ] || # ip of the device
910
[ -z "${MAC}" ] || # mac address of the device
1011
[ -z "${CIDR_BLOCK}" ] # CIDR block of the subnet
1112
then
1213
echo 'One or more environment variables missing'
1314
exit 1
1415
fi
15-
echo "Configuring NIC, Device name: ${DEVICE_NAME}, Device number: ${DEVICE_NUMBER}"
16+
echo "Configuring NIC, Device name: ${DEVICE_NAME}, Device number: ${DEVICE_NUMBER}, Network card index:${NETWORK_CARD_INDEX}"
1617

1718
configuration_directory="/etc/systemd/network"
1819
file_name="70-${DEVICE_NAME}.network"
@@ -23,12 +24,13 @@ fi
2324

2425
cd "$configuration_directory"
2526

26-
ROUTE_TABLE=100${DEVICE_NUMBER}
27+
SUFFIX=$(printf "%03d" $NETWORK_CARD_INDEX)$(printf "%03d" $DEVICE_NUMBER)
28+
ROUTE_TABLE=1${SUFFIX}
2729

2830
ln -s /usr/lib/systemd/network/80-ec2.network ${file_name} # Use default EC2 configuration. This include MTU, etc.
2931

3032
/bin/cat <<EOF > ${sub_directory}/eni.conf
31-
# Configuration for ${DEVICE_NUMBER} generated by ParallelCluster
33+
# Configuration for network card: ${NETWORK_CARD_INDEX}, device number: ${DEVICE_NUMBER} generated by ParallelCluster
3234
# This is inspired by https://github.com/amazonlinux/amazon-ec2-net-utils/blob/v2.4.1/lib/lib.sh
3335
[Match]
3436
MACAddress=${MAC}

cookbooks/aws-parallelcluster-environment/files/default/network_interfaces/configure_nw_interface.sh

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ set -e
1010

1111
if
1212
[ -z "${DEVICE_NAME}" ] || # name of the device
13-
[ -z "${DEVICE_NUMBER}" ] || # number of the device
13+
[ -z "${DEVICE_NUMBER}" ] || # index of the device
14+
[ -z "${NETWORK_CARD_INDEX}" ] || # index of the network card
1415
[ -z "${GW_IP_ADDRESS}" ] || # gateway ip address
1516
[ -z "${DEVICE_IP_ADDRESS}" ] || # ip address to assign to the interface
1617
[ -z "${CIDR_PREFIX_LENGTH}" ] || # the prefix length of the device IP cidr block
@@ -20,9 +21,11 @@ then
2021
exit 1
2122
fi
2223

23-
ROUTE_TABLE="100${DEVICE_NUMBER}"
24+
SUFFIX=$(printf "%03d" $NETWORK_CARD_INDEX)$(printf "%03d" $DEVICE_NUMBER)
2425

25-
echo "Configuring ${DEVICE_NAME} with IP:${DEVICE_IP_ADDRESS} CIDR_PREFIX:${CIDR_PREFIX_LENGTH} NETMASK:${NETMASK} GW:${GW_IP_ADDRESS} ROUTING_TABLE:${ROUTE_TABLE}"
26+
ROUTE_TABLE="1${SUFFIX}"
27+
28+
echo "Configuring device name: ${DEVICE_NAME} with IP:${DEVICE_IP_ADDRESS} CIDR_PREFIX:${CIDR_PREFIX_LENGTH} NETMASK:${NETMASK} GW:${GW_IP_ADDRESS} ROUTING_TABLE:${ROUTE_TABLE}"
2629

2730
# config file
2831
FILE="/etc/sysconfig/network-scripts/ifcfg-${DEVICE_NAME}"

cookbooks/aws-parallelcluster-environment/files/redhat-8.network_interfaces/configure_nw_interface.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ set -e
1313

1414
if
1515
[ -z "${DEVICE_NAME}" ] || # name of the device
16-
[ -z "${DEVICE_NUMBER}" ] || # number of the device
16+
[ -z "${DEVICE_NUMBER}" ] || # index of the device
17+
[ -z "${NETWORK_CARD_INDEX}" ] || # index of the network card
1718
[ -z "${GW_IP_ADDRESS}" ] || # gateway ip address
1819
[ -z "${DEVICE_IP_ADDRESS}" ] || # ip address to assign to the interface
1920
[ -z "${CIDR_PREFIX_LENGTH}" ] # the prefix length of the device IP cidr block
@@ -23,9 +24,10 @@ then
2324
fi
2425

2526
con_name="System ${DEVICE_NAME}"
26-
route_table="100${DEVICE_NUMBER}"
27-
priority="100${DEVICE_NUMBER}"
28-
metric="100${DEVICE_NUMBER}"
27+
SUFFIX=$(printf "%03d" $NETWORK_CARD_INDEX)$(printf "%03d" $DEVICE_NUMBER)
28+
route_table="1${SUFFIX}"
29+
priority="1${SUFFIX}"
30+
metric="1${SUFFIX}"
2931

3032
# Rename connection
3133
original_con_name=`nmcli -t -f GENERAL.CONNECTION device show ${DEVICE_NAME} | cut -f2 -d':'`

cookbooks/aws-parallelcluster-environment/files/rocky/network_interfaces/configure_nw_interface.sh

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ set -e
1313

1414
if
1515
[ -z "${DEVICE_NAME}" ] || # name of the device
16-
[ -z "${DEVICE_NUMBER}" ] || # number of the device
16+
[ -z "${DEVICE_NUMBER}" ] || # index of the device
17+
[ -z "${NETWORK_CARD_INDEX}" ] || # index of the network card
1718
[ -z "${GW_IP_ADDRESS}" ] || # gateway ip address
1819
[ -z "${DEVICE_IP_ADDRESS}" ] || # ip address to assign to the interface
1920
[ -z "${CIDR_PREFIX_LENGTH}" ] # the prefix length of the device IP cidr block
@@ -23,9 +24,10 @@ then
2324
fi
2425

2526
con_name="System ${DEVICE_NAME}"
26-
route_table="100${DEVICE_NUMBER}"
27-
priority="100${DEVICE_NUMBER}"
28-
metric="100${DEVICE_NUMBER}"
27+
SUFFIX=$(printf "%03d" $NETWORK_CARD_INDEX)$(printf "%03d" $DEVICE_NUMBER)
28+
route_table="1${SUFFIX}"
29+
priority="1${SUFFIX}"
30+
metric="1${SUFFIX}"
2931

3032
# Rename connection
3133
original_con_name=`nmcli -t -f GENERAL.CONNECTION device show ${DEVICE_NAME} | cut -f2 -d':'`

cookbooks/aws-parallelcluster-environment/files/ubuntu/network_interfaces/configure_nw_interface.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ set -e
1010

1111
if
1212
[ -z "${DEVICE_NAME}" ] || # name of the device
13-
[ -z "${DEVICE_NUMBER}" ] || # number of the device
13+
[ -z "${DEVICE_NUMBER}" ] || # index of the device
14+
[ -z "${NETWORK_CARD_INDEX}" ] || # index of the network card
1415
[ -z "${GW_IP_ADDRESS}" ] || # gateway ip address
1516
[ -z "${DEVICE_IP_ADDRESS}" ] || # ip address to assign to the interface
1617
[ -z "${CIDR_PREFIX_LENGTH}" ] || # the prefix length of the device IP cidr block
@@ -40,7 +41,8 @@ if [ "${STATIC_IP_CONFIG}" = "" ]
4041
fi
4142

4243
FILE="/etc/netplan/${DEVICE_NAME}.yaml"
43-
ROUTE_TABLE="100${DEVICE_NUMBER}"
44+
SUFFIX=$(printf "%03d" $NETWORK_CARD_INDEX)$(printf "%03d" $DEVICE_NUMBER)
45+
ROUTE_TABLE="1${SUFFIX}"
4446

4547
echo "Configuring ${DEVICE_NAME} with IP:${DEVICE_IP_ADDRESS} CIDR_PREFIX:${CIDR_PREFIX_LENGTH} NETMASK:${NETMASK} GW:${GW_IP_ADDRESS} ROUTING_TABLE:${ROUTE_TABLE}"
4648

cookbooks/aws-parallelcluster-environment/recipes/config/network_interfaces.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@
1616
return if on_docker?
1717

1818
def network_card_index(mac, token)
19+
# This IMDS call is not available on single NIC instance, therefore fallback to 0
1920
uri = URI("http://169.254.169.254/latest/meta-data/network/interfaces/macs/#{mac}/network-card")
21+
get_metadata_with_token(token, uri) || 0
22+
end
23+
24+
def device_number(mac, token)
25+
uri = URI("http://169.254.169.254/latest/meta-data/network/interfaces/macs/#{mac}/device-number")
2026
get_metadata_with_token(token, uri)
2127
end
2228

@@ -69,6 +75,7 @@ def cidr_to_netmask(cidr)
6975
# Configure nw interfaces
7076
macs.each do |mac|
7177
device_name = device_name(mac)
78+
device_number = device_number(mac, token)
7279
network_card_index = network_card_index(mac, token)
7380
gw_ip_address = gateway_address
7481
device_ip_address = device_ip(mac, token)
@@ -84,7 +91,8 @@ def cidr_to_netmask(cidr)
8491
environment(
8592
# TODO: The variables are a superset of what's required by individual scripts. Consider simplification.
8693
'DEVICE_NAME' => device_name,
87-
'DEVICE_NUMBER' => "#{network_card_index}", # in configure_nw_interface DEVICE_NUMBER actually means network card index
94+
'DEVICE_NUMBER' => "#{device_number}",
95+
'NETWORK_CARD_INDEX' => "#{network_card_index}",
8896
'GW_IP_ADDRESS' => gw_ip_address,
8997
'DEVICE_IP_ADDRESS' => device_ip_address,
9098
'CIDR_PREFIX_LENGTH' => cidr_prefix_length,

0 commit comments

Comments
 (0)