Skip to content

Commit e55a7d7

Browse files
nikos-githublguohan
authored andcommitted
[baseimage]: Initial changes for dhcp to support eth0 in a mgmt vrf (#2348)
* Initial changes to support eth0 in a mgmt vrf
1 parent b7330dd commit e55a7d7

File tree

3 files changed

+239
-7
lines changed

3 files changed

+239
-7
lines changed

build_debian.sh

+5-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ sudo LANG=C chroot $FILESYSTEM_ROOT apt-get -y install \
200200
## Note: don't install python-apt by pip, older than Debian repo one
201201
sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y install \
202202
file \
203-
ifupdown \
203+
ifupdown2 \
204204
iproute2 \
205205
bridge-utils \
206206
isc-dhcp-client \
@@ -232,6 +232,7 @@ sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y in
232232
unzip \
233233
gdisk \
234234
sysfsutils \
235+
squashfs-tools \
235236
grub2-common \
236237
rsyslog \
237238
ethtool \
@@ -340,6 +341,8 @@ set /files/etc/sysctl.conf/net.ipv6.conf.eth0.keep_addr_on_down 1
340341
341342
set /files/etc/sysctl.conf/net.ipv6.conf.eth0.accept_ra_defrtr 0
342343
344+
set /files/etc/sysctl.conf/net.ipv4.tcp_l3mdev_accept 1
345+
343346
set /files/etc/sysctl.conf/net.core.rmem_max 2097152
344347
set /files/etc/sysctl.conf/net.core.wmem_max 2097152
345348
" -r $FILESYSTEM_ROOT
@@ -368,6 +371,7 @@ sudo cp files/dhcp/rfc3442-classless-routes $FILESYSTEM_ROOT/etc/dhcp/dhclient-e
368371
sudo cp files/dhcp/sethostname $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
369372
sudo cp files/dhcp/graphserviceurl $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
370373
sudo cp files/dhcp/snmpcommunity $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
374+
sudo cp files/dhcp/vrf $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
371375
sudo cp files/dhcp/dhclient.conf $FILESYSTEM_ROOT/etc/dhcp/
372376

373377
## Version file

files/build_templates/sonic_debian_extension.j2

-6
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,6 @@ sudo dpkg --root=$FILESYSTEM_ROOT -P {{ debname }}
260260

261261
sudo rm -f $FILESYSTEM_ROOT/usr/sbin/policy-rc.d
262262

263-
## Revise /etc/init.d/networking and /lib/systemd/system/networking.service for Arista switches
264-
if [ "$image_type" = "aboot" ]; then
265-
sudo sed -i 's/udevadm settle/udevadm settle -E \/sys\/class\/net\/eth0/' $FILESYSTEM_ROOT/etc/init.d/networking
266-
sudo sed -i 's/udevadm settle/udevadm settle -E \/sys\/class\/net\/eth0/' $FILESYSTEM_ROOT/lib/systemd/system/networking.service
267-
fi
268-
269263
## copy platform rc.local
270264
sudo cp $IMAGE_CONFIGS/platform/rc.local $FILESYSTEM_ROOT/etc/
271265

files/dhcp/vrf

+234
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
1+
#
2+
# DHCLIENT exit hook for vrf support.
3+
#
4+
# Code ported from https://github.com/CumulusNetworks/vrf under GPLv2 license
5+
# (see https://github.com/CumulusNetworks/vrf/blob/master/debian/copyright).
6+
#
7+
8+
# Get table_id for device enslaved to a vrf.
9+
vrf_get_table_dev()
10+
{
11+
local table_id
12+
13+
# If vrf_slave is not in the output, device is not enslaved.
14+
table_id=$(ip -o -d link show dev ${1} 2>/dev/null |\
15+
egrep ' vrf_slave table [0-9]*' |\
16+
sed -e 's/.*vrf_slave table \([0-9]*\) .*/\1/')
17+
18+
[ -z "${table_id}" ] && return 1
19+
20+
echo ${table_id}
21+
22+
return 0
23+
}
24+
25+
# Get table_id for vrf device.
26+
vrf_get_table()
27+
{
28+
local table_id
29+
30+
table_id=$(ip -o -d link show dev ${1} 2>/dev/null |\
31+
egrep ' vrf table [0-9]*' |\
32+
sed -e 's/.*vrf table \([0-9]*\) .*/\1/')
33+
34+
[ -z "${table_id}" ] && return 1
35+
36+
echo ${table_id}
37+
38+
return 0
39+
}
40+
41+
vrf_exists()
42+
{
43+
local vrf=${1}
44+
local n
45+
46+
[ "$vrf" = "default" ] && return 0
47+
48+
# ip link show dev <name> type vrf happily returns 0 even though
49+
# <name> is not of type vrf. Hence the wc -l.
50+
n=$(ip -br link show dev ${vrf} type vrf 2>/dev/null | wc -l)
51+
[ ${n} -eq 1 ] && return 0
52+
53+
return $?
54+
}
55+
56+
# Check vrf device contains only alphanumeric characters.
57+
get_vrf_arg()
58+
{
59+
local vrf
60+
61+
vrf=$(echo $1 | tr -cd [:alnum:])
62+
if [ "$vrf" != "$1" ]; then
63+
echo "Invalid VRF" >&2
64+
return 1
65+
fi
66+
67+
echo $vrf
68+
}
69+
70+
vrf_table()
71+
{
72+
local table_id
73+
local vrf
74+
75+
vrf=$(get_vrf_arg ${1})
76+
[ $? -ne 0 ] && return 1
77+
78+
vrf_exists $vrf
79+
if [ $? -eq 0 ]; then
80+
vrf_get_table $vrf
81+
return 0
82+
fi
83+
84+
# Maybe this is a device, not a vrf.
85+
table_id=$(vrf_get_table_dev $vrf)
86+
if [ $? -eq 0 ]; then
87+
echo ${table_id}
88+
return 0
89+
fi
90+
91+
return 1
92+
}
93+
94+
table_id=$(vrf_table ${interface})
95+
96+
if [ -n "${table_id}" ]; then
97+
98+
case "$reason" in
99+
BOUND|RENEW|REBIND|REBOOT)
100+
if [ -z "$old_ip_address" ] ||
101+
[ "$old_ip_address" != "$new_ip_address" ] ||
102+
[ "$reason" = "BOUND" ] || [ "$reason" = "REBOOT" ]; then
103+
# If we have $new_rfc3442_classless_static_routes then we have to
104+
# ignore $new_routers entirely.
105+
if [ ! "$new_rfc3442_classless_static_routes" ]; then
106+
# Set if_metric if IF_METRIC is set or there's more than one router.
107+
if_metric="$IF_METRIC"
108+
if [ "${new_routers%% *}" != "${new_routers}" ]; then
109+
if_metric=${if_metric:-1}
110+
fi
111+
112+
for router in $new_routers; do
113+
if [ "$new_subnet_mask" = "255.255.255.255" ]; then
114+
# Set explicit route for p2p connection.
115+
ip -4 route add table ${table_id} ${router} dev $interface >/dev/null 2>&1
116+
fi
117+
118+
# Remove old default route should it remain from dhclient-script.
119+
ip -4 route del default via ${router} dev ${interface} \
120+
${if_metric:+metric $if_metric} >/dev/null 2>&1
121+
122+
# Set default route.
123+
ip -4 route add table ${table_id} default via ${router} dev ${interface} \
124+
${if_metric:+metric $if_metric} >/dev/null 2>&1
125+
126+
if [ -n "$if_metric" ]; then
127+
if_metric=$((if_metric+1))
128+
fi
129+
done
130+
else
131+
set -- $new_rfc3442_classless_static_routes
132+
133+
while [ $# -gt 0 ]; do
134+
net_length=$1
135+
via_arg=''
136+
137+
case $net_length in
138+
32|31|30|29|28|27|26|25)
139+
if [ $# -lt 9 ]; then
140+
return 1
141+
fi
142+
net_address="${2}.${3}.${4}.${5}"
143+
gateway="${6}.${7}.${8}.${9}"
144+
shift 9
145+
;;
146+
24|23|22|21|20|19|18|17)
147+
if [ $# -lt 8 ]; then
148+
return 1
149+
fi
150+
net_address="${2}.${3}.${4}.0"
151+
gateway="${5}.${6}.${7}.${8}"
152+
shift 8
153+
;;
154+
16|15|14|13|12|11|10|9)
155+
if [ $# -lt 7 ]; then
156+
return 1
157+
fi
158+
net_address="${2}.${3}.0.0"
159+
gateway="${4}.${5}.${6}.${7}"
160+
shift 7
161+
;;
162+
8|7|6|5|4|3|2|1)
163+
if [ $# -lt 6 ]; then
164+
return 1
165+
fi
166+
net_address="${2}.0.0.0"
167+
gateway="${3}.${4}.${5}.${6}"
168+
shift 6
169+
;;
170+
0) # default route
171+
if [ $# -lt 5 ]; then
172+
return 1
173+
fi
174+
net_address="0.0.0.0"
175+
gateway="${2}.${3}.${4}.${5}"
176+
shift 5
177+
;;
178+
*) # error
179+
return 1
180+
;;
181+
esac
182+
183+
# Take care of link-local routes.
184+
if [ "${gateway}" != '0.0.0.0' ]; then
185+
via_arg="via ${gateway}"
186+
fi
187+
188+
# Set route (ip detects host routes automatically).
189+
ip -4 route add table ${table_id} "${net_address}/${net_length}" \
190+
${via_arg} dev "${interface}" >/dev/null 2>&1
191+
done
192+
fi
193+
fi
194+
195+
if [ -n "$alias_ip_address" ] &&
196+
[ "$new_ip_address" != "$alias_ip_address" ]; then
197+
ip -4 route add table ${table_id} ${alias_ip_address} dev ${interface} >/dev/null 2>&1
198+
fi
199+
;;
200+
201+
EXPIRE|FAIL|RELEASE|STOP)
202+
if [ -n "$alias_ip_address" ]; then
203+
ip -4 route add table ${table_id} ${alias_ip_address} dev ${interface} >/dev/null 2>&1
204+
fi
205+
206+
;;
207+
208+
TIMEOUT)
209+
# If there is no router recorded in the lease or the 1st router answers pings.
210+
if [ -z "$new_routers" ] || ping -q -c 1 "${new_routers%% *}"; then
211+
# If we have $new_rfc3442_classless_static_routes then we have to
212+
# ignore $new_routers entirely.
213+
if [ ! "$new_rfc3442_classless_static_routes" ]; then
214+
if [ -n "$alias_ip_address" ] &&
215+
[ "$new_ip_address" != "$alias_ip_address" ]; then
216+
ip -4 route add table ${table_id} ${alias_ip_address} dev ${interface} >/dev/null 2>&1
217+
fi
218+
219+
# Set default route.
220+
for router in $new_routers; do
221+
ip -4 route add table ${table_id} default via ${router} dev ${interface} \
222+
${if_metric:+metric $if_metric} >/dev/null 2>&1
223+
224+
if [ -n "$if_metric" ]; then
225+
if_metric=$((if_metric+1))
226+
fi
227+
done
228+
fi
229+
fi
230+
231+
;;
232+
esac
233+
234+
fi

0 commit comments

Comments
 (0)