Skip to content

Commit d021125

Browse files
rajendra-dendukuripphuchar
authored andcommitted
ZTP infrastructure changes to support DHCP discovery provisioning data (sonic-net#3298)
* ZTP infrastructure changes to support DHCP discovery provisioning data - Dynamically generate DHCP client configuration based on current ZTP state - Added support to request and process hostname when using DHCPv6 - Do not process graphservice url dhcp option if ZTP is enabled, ZTP service will process it - Generate /e/n/i file with all active interfaces seeking address assignment via DHCP. Only interfaces that are created in Linux will be added to /e/n/i. Also DHCP is started only on linked up in-band interfaces. Signed-off-by: Rajendra Dendukuri <rajendra.dendukuri@broadcom.com>
1 parent 0d222bb commit d021125

11 files changed

Lines changed: 174 additions & 42 deletions

build_debian.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,6 @@ set /files/etc/sysctl.conf/net.ipv6.conf.default.keep_addr_on_down 1
396396
set /files/etc/sysctl.conf/net.ipv6.conf.all.keep_addr_on_down 1
397397
set /files/etc/sysctl.conf/net.ipv6.conf.eth0.keep_addr_on_down 1
398398
399-
set /files/etc/sysctl.conf/net.ipv6.conf.eth0.accept_ra_defrtr 0
400-
set /files/etc/sysctl.conf/net.ipv6.conf.eth0.accept_ra 0
401-
402399
set /files/etc/sysctl.conf/net.ipv4.tcp_l3mdev_accept 1
403400
set /files/etc/sysctl.conf/net.ipv4.udp_l3mdev_accept 1
404401
@@ -433,10 +430,10 @@ EOF
433430

434431
sudo cp files/dhcp/rfc3442-classless-routes $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d
435432
sudo cp files/dhcp/sethostname $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
433+
sudo cp files/dhcp/sethostname6 $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
436434
sudo cp files/dhcp/graphserviceurl $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
437435
sudo cp files/dhcp/snmpcommunity $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
438436
sudo cp files/dhcp/vrf $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
439-
sudo cp files/dhcp/dhclient.conf $FILESYSTEM_ROOT/etc/dhcp/
440437
if [ -f files/image_config/ntp/ntp ]; then
441438
sudo cp ./files/image_config/ntp/ntp $FILESYSTEM_ROOT/etc/init.d/
442439
fi

files/build_templates/sonic_debian_extension.j2

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,12 @@ sudo cp $IMAGE_CONFIGS/interfaces/interfaces-config.sh $FILESYSTEM_ROOT/usr/bin/
219219
sudo cp $IMAGE_CONFIGS/interfaces/*.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/
220220
echo "interfaces-config.service" | sudo tee -a $GENERATED_SERVICE_FILE
221221

222+
# Copy dhcp client configuration template and create an initial configuration
223+
sudo cp files/dhcp/dhclient.conf.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/
224+
j2 files/dhcp/dhclient.conf.j2 | sudo tee $FILESYSTEM_ROOT/etc/dhcp/dhclient.conf
225+
sudo cp files/dhcp/ifupdown2_policy.json $FILESYSTEM_ROOT/etc/network/ifupdown2/policy.d
226+
sudo cp files/dhcp/90-dhcp6-systcl.conf.j2 $FILESYSTEM_ROOT_USR_SHARE_SONIC_TEMPLATES/
227+
222228
# Copy initial interfaces configuration file, will be overwritten on first boot
223229
sudo cp $IMAGE_CONFIGS/interfaces/init_interfaces $FILESYSTEM_ROOT/etc/network/interfaces
224230
sudo mkdir -p $FILESYSTEM_ROOT/etc/network/interfaces.d

files/dhcp/90-dhcp6-systcl.conf.j2

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{% if MGMT_INTERFACE %}
2+
net.ipv6.conf.eth0.accept_ra_defrtr = 0
3+
net.ipv6.conf.eth0.accept_ra = 0
4+
{% else %}
5+
net.ipv6.conf.eth0.accept_ra_defrtr = 1
6+
net.ipv6.conf.eth0.accept_ra = 1
7+
{% endif %}

files/dhcp/dhclient.conf

Lines changed: 0 additions & 24 deletions
This file was deleted.

files/dhcp/dhclient.conf.j2

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{% block banner %}
2+
# =============== Managed by SONiC Config Engine DO NOT EDIT! ===============
3+
# generated from /usr/share/sonic/templates/dhclient.conf.j2 using sonic-cfggen
4+
# file: /etc/dhcp/dhclient.conf
5+
#
6+
{% endblock banner %}
7+
# Configuration file for /sbin/dhclient, which is included in Debian's
8+
# dhcp3-client package.
9+
#
10+
# This is a sample configuration file for dhclient. See dhclient.conf's
11+
# man page for more information about the syntax of this file
12+
# and a more comprehensive list of the parameters understood by
13+
# dhclient.
14+
#
15+
# Normally, if the DHCP server provides reasonable information and does
16+
# not leave anything out (like the domain name, for example), then
17+
# few changes must be made to this file, if any.
18+
#
19+
20+
option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;
21+
option snmp-community code 224 = text;
22+
option minigraph-url code 225 = text;
23+
option acl-url code 226 = text;
24+
option tftp-server-name code 66 = text;
25+
option bootfile-name code 67 = text;
26+
option user-class code 77 = text;
27+
option provisioning-script-url code 239 = text;
28+
option dhcp6.user-class code 15 = text;
29+
option dhcp6.provisioning-script-url code 239 = text;
30+
option dhcp6.boot-file-url code 59 = text;
31+
32+
send host-name = gethostname();
33+
request subnet-mask, broadcast-address, time-offset, routers,
34+
domain-name, domain-name-servers, domain-search, host-name,
35+
dhcp6.name-servers, dhcp6.domain-search, interface-mtu, dhcp6.fqdn,
36+
rfc3442-classless-static-routes, ntp-servers, log-servers,
37+
{%- if ZTP is defined and ZTP_DHCP_DISABLED is not defined -%}bootfile-name, provisioning-script-url, tftp-server-name,
38+
dhcp6.provisioning-script-url, dhcp6.boot-file-url,{%- endif -%}
39+
snmp-community, minigraph-url, acl-url;
40+
{% if ZTP is defined and ZTP_DHCP_DISABLED is not defined %}
41+
send user-class "SONiC-ZTP";
42+
send dhcp6.user-class "SONiC-ZTP";
43+
send dhcp-client-identifier "SONiC##{{ ZTP['mode']['product-name'] }}##{{ ZTP['mode']['serial-no'] }}";
44+
retry 60;
45+
{% endif %}

files/dhcp/graphserviceurl

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
case $reason in
2-
BOUND|RENEW|REBIND|REBOOT)
3-
if [ -n "$new_minigraph_url" ]; then
4-
echo $new_minigraph_url > /tmp/dhcp_graph_url
5-
else
6-
echo "N/A" > /tmp/dhcp_graph_url
7-
fi
8-
if [ -n "$new_acl_url" ]; then
9-
echo $new_acl_url > /tmp/dhcp_acl_url
10-
fi
11-
;;
12-
esac
1+
if [ ! -e /usr/bin/ztp ] || [ "$(ztp status -c)" = "0:DISABLED" ]; then
2+
case $reason in
3+
BOUND|RENEW|REBIND|REBOOT)
4+
if [ -n "$new_minigraph_url" ]; then
5+
echo $new_minigraph_url > /tmp/dhcp_graph_url
6+
else
7+
echo "N/A" > /tmp/dhcp_graph_url
8+
fi
9+
if [ -n "$new_acl_url" ]; then
10+
echo $new_acl_url > /tmp/dhcp_acl_url
11+
fi
12+
;;
13+
esac
14+
fi

files/dhcp/ifupdown2_policy.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"dhcp" : {
3+
"defaults" : {
4+
"dhcp-wait" : "no"
5+
},
6+
"iface_defaults" : {
7+
"eth0" : {
8+
"dhcp6-duid" : "LL"
9+
}
10+
}
11+
}
12+
}

files/dhcp/rfc3442-classless-routes

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,13 @@ if [ "$RUN" = "yes" ]; then
5555
fi
5656

5757
# set route (ip detects host routes automatically)
58-
ip -4 route add "${net_address}/${net_length}" \
58+
if echo $interface | grep -v Ethernet ; then
59+
ip -4 route add "${net_address}/${net_length}" \
5960
${via_arg} dev "${interface}" table default >/dev/null 2>&1
61+
else
62+
ip -4 route add "${net_address}/${net_length}" \
63+
${via_arg} dev "${interface}" >/dev/null 2>&1
64+
fi
6065
done
6166
fi
6267
fi

files/dhcp/sethostname6

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
case $reason in
2+
BOUND6|RENEW6|REBIND6|REBOOT)
3+
current_dhcp6_fqdn=`hostname`
4+
if [ "$current_dhcp6_fqdn" != "$new_dhcp6_fqdn" ] && [ -n "$new_dhcp6_fqdn" ]
5+
then
6+
echo $new_dhcp6_fqdn > /etc/hostname
7+
hostname -F /etc/hostname
8+
sed -i "/\s$current_dhcp6_fqdn$/d" /etc/hosts
9+
sed -i "/\s$new_dhcp6_fqdn$/d" /etc/hosts
10+
echo "127.0.0.1 $new_dhcp6_fqdn" >> /etc/hosts
11+
echo ":: $new_dhcp6_fqdn" >> /etc/hosts
12+
fi
13+
;;
14+
esac

files/image_config/interfaces/interfaces-config.sh

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,40 @@
22

33
ifdown --force eth0
44

5-
sonic-cfggen -d -t /usr/share/sonic/templates/interfaces.j2 > /etc/network/interfaces
5+
# Check if ZTP DHCP policy has been installed
6+
if [ -e /etc/network/ifupdown2/policy.d/ztp_dhcp.json ]; then
7+
# Obtain port operational state information
8+
redis-dump -d 0 -k "PORT_TABLE:Ethernet*" -y > /tmp/ztp_port_data.json
9+
10+
if [ $? -ne 0 ] || [ ! -e /tmp/ztp_port_data.json ] || [ "$(cat /tmp/ztp_port_data.json)" = "" ]; then
11+
echo "{}" > /tmp/ztp_port_data.json
12+
fi
13+
14+
# Create an input file with ztp input information
15+
echo "{ \"PORT_DATA\" : $(cat /tmp/ztp_port_data.json) }" > \
16+
/tmp/ztp_input.json
17+
else
18+
echo "{ \"ZTP_DHCP_DISABLED\" : \"true\" }" > /tmp/ztp_input.json
19+
fi
20+
21+
# Create /e/n/i file for existing and active interfaces
22+
sonic-cfggen -d -j /tmp/ztp_input.json -t /usr/share/sonic/templates/interfaces.j2 > /etc/network/interfaces
623

724
[ -f /var/run/dhclient.eth0.pid ] && kill `cat /var/run/dhclient.eth0.pid` && rm -f /var/run/dhclient.eth0.pid
25+
[ -f /var/run/dhclient6.eth0.pid ] && kill `cat /var/run/dhclient6.eth0.pid` && rm -f /var/run/dhclient6.eth0.pid
26+
27+
for intf_pid in $(ls -1 /var/run/dhclient*.Ethernet*.pid 2> /dev/null); do
28+
[ -f ${intf_pid} ] && kill `cat ${intf_pid}` && rm -f ${intf_pid}
29+
done
830

31+
sonic-cfggen -d -j /tmp/ztp_input.json -t /usr/share/sonic/templates/90-dhcp6-systcl.conf.j2 > /etc/sysctl.d/90-dhcp6-systcl.conf
32+
# Read sysctl conf files again
33+
sysctl -p /etc/sysctl.d/90-dhcp6-systcl.conf
34+
35+
sonic-cfggen -d -j /tmp/ztp_input.json -t /usr/share/sonic/templates/dhclient.conf.j2 > /etc/dhcp/dhclient.conf
936
systemctl restart networking
37+
38+
# Clean-up created files
39+
rm -f /tmp/ztp_input.json /tmp/ztp_port_data.json
40+
41+
ifdown lo && ifup lo

0 commit comments

Comments
 (0)