Skip to content

Commit 31131d7

Browse files
authored
Refactor syncd init to share code between systemd and supervisord (sonic-net#184)
- systemd init script and supervisor start script now share code via syncd_init_common.sh - Use sonic-cfggen to obtain ASIC type (requires syncd dockers inherit from docker-config-engine. I will make this change once this PR is merged and I update the submodule in sonic-buildimage. - Added Cavium-specific startup steps so that platform can share the same init files
1 parent 4e80690 commit 31131d7

File tree

5 files changed

+148
-120
lines changed

5 files changed

+148
-120
lines changed

debian/rules

+9-8
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,13 @@ binary: binary-syncd binary-syncd-rpc
2626

2727
binary-syncd:
2828
$(shell echo > /tmp/syncd-build)
29-
dh clean --with autotools-dev
29+
dh clean --with autotools-dev
3030
dh build -N syncd-rpc -N syncd-rpc-dbg --with autotools-dev
3131
dh binary -N syncd-rpc -N syncd-rpc-dbg --with autotools-dev
3232

3333
binary-syncd-rpc:
3434
$(shell echo '--enable-rpcserver=yes' > /tmp/syncd-build)
35-
dh clean --with autotools-dev
35+
dh clean --with autotools-dev
3636
dh build -N syncd -N syncd-dbg --with autotools-dev
3737
dh binary -N syncd -N syncd-dbg --with autotools-dev
3838

@@ -43,15 +43,16 @@ binary-syncd-rpc:
4343
# -DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH)
4444

4545
override_dh_auto_configure:
46-
./autogen.sh
46+
./autogen.sh
4747
dh_auto_configure -- $(shell cat /tmp/syncd-build)
4848

49+
override_dh_install:
50+
dh_install
51+
ifeq ($(shell cat /tmp/syncd-build), --enable-rpcserver=yes)
52+
sed -i 's/ENABLE_SAITHRIFT=0/ENABLE_SAITHRIFT=1/' debian/syncd-rpc/usr/bin/syncd_init_common.sh
53+
endif
54+
4955
override_dh_installinit:
50-
ifeq ($(shell cat /tmp/syncd-build), --enable-rpcserver=yes)
51-
sed -i 's/ENABLE_SAITHRIFT=0/ENABLE_SAITHRIFT=1/' debian/syncd-rpc.init
52-
else
53-
sed -i 's/ENABLE_SAITHRIFT=1/ENABLE_SAITHRIFT=0/' debian/syncd.init
54-
endif
5556
dh_installinit --init-script=syncd
5657

5758
override_dh_shlibdeps:

debian/syncd.init

+9-112
Original file line numberDiff line numberDiff line change
@@ -10,125 +10,21 @@
1010
# Description: Enable service syncd.
1111
### END INIT INFO
1212

13-
DAEMON_SYNCD=/usr/bin/syncd
13+
# Source the file that holds common code for systemd and supervisord
14+
. /usr/bin/syncd_init_common.sh
1415

15-
# dsserve: domain socket server for stdio
16-
DAEMON_DSSERVE=/usr/bin/dsserve
17-
DAEMON_DSSERVE_ARGS="-d $DAEMON_SYNCD --diag"
18-
ENABLE_SAITHRIFT=0
19-
PLATFORM_DIR=/usr/share/sonic/platform
20-
HWSKU_DIR=/usr/share/sonic/hwsku
21-
22-
if [ -x $DAEMON_DSSERVE ]; then
23-
DAEMON=$DAEMON_DSSERVE
24-
DAEMON_ARGS=$DAEMON_DSSERVE_ARGS
25-
else
26-
DAEMON=$DAEMON_SYNCD
16+
# When starting as a service, we want our process to run as a daemon, in the background
17+
if [ $CMD == $CMD_DSSERVE ]; then
18+
CMD_ARGS="-d $CMD_ARGS"
2719
fi
2820

29-
parse_yaml() {
30-
local prefix=$2
31-
local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
32-
sed -ne "s|^\($s\)\($w\)$s:$s\"\(.*\)\"$s\$|\1$fs\2$fs\3|p" \
33-
-e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p" $1 |
34-
awk -F$fs '{
35-
indent = length($1)/2;
36-
vname[indent] = $2;
37-
for (i in vname) {if (i > indent) {delete vname[i]}}
38-
if (length($3) > 0) {
39-
vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
40-
printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
41-
}
42-
}'
43-
}
44-
45-
start_bcm()
46-
{
47-
[ -e /dev/linux-bcm-knet ] || mknod /dev/linux-bcm-knet c 122 0
48-
[ -e /dev/linux-user-bde ] || mknod /dev/linux-user-bde c 126 0
49-
[ -e /dev/linux-kernel-bde ] || mknod /dev/linux-kernel-bde c 127 0
50-
}
51-
52-
start_mlnx()
53-
{
54-
[ -e /dev/sxdevs/sxcdev ] || ( mkdir -p /dev/sxdevs && mknod /dev/sxdevs/sxcdev c 231 193 )
55-
56-
# Read MAC address and align the last 6 bits.
57-
MAC_ADDRESS=`ip link show eth0 | grep ether | awk '{print $2}'`
58-
last_byte=`python -c "print '$MAC_ADDRESS'[-2:]"`
59-
aligned_last_byte=`python -c "print format(int(int('$last_byte', 16) & 0b11000000), '02x')"` # put mask and take away the 0x prefix
60-
ALIGNED_MAC_ADDRESS=`python -c "print '$MAC_ADDRESS'[:-2] + '$aligned_last_byte'"` # put aligned byte into the end of MAC
61-
62-
# Write MAC address into /tmp/profile file.
63-
cat $HWSKU_DIR/sai.profile > /tmp/sai.profile
64-
echo "DEVICE_MAC_ADDRESS=$ALIGNED_MAC_ADDRESS" >> /tmp/sai.profile
65-
}
66-
67-
start_centec()
68-
{
69-
[ -e /dev/linux_dal ] || mknod /dev/linux_dal c 198 0
70-
[ -e /dev/net/tun ] || ( mkdir -p /dev/net && mknod /dev/net/tun c 10 200 )
71-
72-
# Read MAC address and align the last 6 bits.
73-
MAC_ADDRESS=`ip link show eth0 | grep ether | awk '{print $2}'`
74-
last_byte=`python -c "print '$MAC_ADDRESS'[-2:]"`
75-
aligned_last_byte=`python -c "print format(int(int('$last_byte', 16) & 0b11000000), '02x')"` # put mask and take away the 0x prefix
76-
ALIGNED_MAC_ADDRESS=`python -c "print '$MAC_ADDRESS'[:-2] + '$aligned_last_byte'"` # put aligned byte into the end of MAC
77-
78-
# Write MAC address into /tmp/profile file.
79-
cat $HWSKU_DIR/sai.profile > /tmp/sai.profile
80-
echo "DEVICE_MAC_ADDRESS=$ALIGNED_MAC_ADDRESS" >> /tmp/sai.profile
81-
}
82-
83-
start_cavium()
84-
{
85-
export XP_ROOT=/usr/bin/
86-
}
87-
88-
case "$(cat /proc/cmdline)" in
89-
*fast-reboot*)
90-
FAST_REBOOT='yes'
91-
;;
92-
*)
93-
FAST_REBOOT='no'
94-
;;
95-
esac
96-
9721
case "$1" in
9822
start)
99-
eval $(parse_yaml /etc/sonic/sonic_version.yml "sonic_")
100-
101-
if [ $sonic_asic_type == "broadcom" ]; then
102-
start_bcm
103-
DAEMON_ARGS+=" -p $HWSKU_DIR/sai.profile "
104-
if [ $FAST_REBOOT == "yes" ];
105-
then
106-
DAEMON_ARGS+=" -t fast "
107-
fi
108-
elif [ $sonic_asic_type == "mellanox" ]; then
109-
start_mlnx
110-
DAEMON_ARGS+=" -p /tmp/sai.profile "
111-
elif [ $sonic_asic_type == "cavium" ]; then
112-
DAEMON_ARGS+=" -p $HWSKU_DIR/sai.profile "
113-
start_cavium
114-
elif [ $sonic_asic_type == "centec" ]; then
115-
start_centec
116-
DAEMON_ARGS+=" -p /tmp/sai.profile "
117-
else
118-
echo "Unknown asic type $sonic_asic_type"
119-
exit 1
120-
fi
121-
122-
if [ ${ENABLE_SAITHRIFT} == 1 ]; then
123-
DAEMON_ARGS+=" -r -m $HWSKU_DIR/port_config.ini"
124-
fi
125-
126-
[ -r $PLATFORM_DIR/syncd.conf ] && . $PLATFORM_DIR/syncd.conf
127-
128-
start-stop-daemon --start --quiet --exec $DAEMON -- $DAEMON_ARGS
23+
config_syncd
24+
start-stop-daemon --start --quiet --exec $CMD -- $CMD_ARGS
12925
;;
13026
stop)
131-
start-stop-daemon --stop --quiet --exec $DAEMON
27+
start-stop-daemon --stop --quiet --exec $CMD
13228
;;
13329
*)
13430
echo "Usage: service syncd {start|stop}"
@@ -137,3 +33,4 @@ stop)
13733
esac
13834

13935
exit 0
36+

debian/syncd.install

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
usr/bin/saidump
22
usr/bin/saiplayer
33
usr/bin/syncd*
4+
syncd/scripts/* usr/bin

syncd/scripts/syncd_init_common.sh

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env bash
2+
3+
CMD_SYNCD=/usr/bin/syncd
4+
5+
# dsserve: domain socket server for stdio
6+
CMD_DSSERVE=/usr/bin/dsserve
7+
CMD_DSSERVE_ARGS="$CMD_SYNCD --diag"
8+
9+
ENABLE_SAITHRIFT=0
10+
11+
PLATFORM_DIR=/usr/share/sonic/platform
12+
HWSKU_DIR=/usr/share/sonic/hwsku
13+
14+
SONIC_ASIC_TYPE=$(sonic-cfggen -y /etc/sonic/sonic_version.yml -v asic_type)
15+
16+
if [ -x $CMD_DSSERVE ]; then
17+
CMD=$CMD_DSSERVE
18+
CMD_ARGS=$CMD_DSSERVE_ARGS
19+
else
20+
CMD=$CMD_SYNCD
21+
fi
22+
23+
case "$(cat /proc/cmdline)" in
24+
*fast-reboot*)
25+
FAST_REBOOT='yes'
26+
;;
27+
*)
28+
FAST_REBOOT='no'
29+
;;
30+
esac
31+
32+
33+
config_syncd_bcm()
34+
{
35+
CMD_ARGS+=" -p $HWSKU_DIR/sai.profile"
36+
37+
[ -e /dev/linux-bcm-knet ] || mknod /dev/linux-bcm-knet c 122 0
38+
[ -e /dev/linux-user-bde ] || mknod /dev/linux-user-bde c 126 0
39+
[ -e /dev/linux-kernel-bde ] || mknod /dev/linux-kernel-bde c 127 0
40+
41+
if [ $FAST_REBOOT == "yes" ]; then
42+
CMD_ARGS+=" -t fast"
43+
fi
44+
}
45+
46+
config_syncd_mlnx()
47+
{
48+
CMD_ARGS+=" -p /tmp/sai.profile"
49+
50+
[ -e /dev/sxdevs/sxcdev ] || ( mkdir -p /dev/sxdevs && mknod /dev/sxdevs/sxcdev c 231 193 )
51+
52+
# Read MAC address and align the last 6 bits.
53+
MAC_ADDRESS=$(ip link show eth0 | grep ether | awk '{print $2}')
54+
last_byte=$(python -c "print '$MAC_ADDRESS'[-2:]")
55+
aligned_last_byte=$(python -c "print format(int(int('$last_byte', 16) & 0b11000000), '02x')") # put mask and take away the 0x prefix
56+
ALIGNED_MAC_ADDRESS=$(python -c "print '$MAC_ADDRESS'[:-2] + '$aligned_last_byte'") # put aligned byte into the end of MAC
57+
58+
# Write MAC address into /tmp/profile file.
59+
cat $HWSKU_DIR/sai.profile > /tmp/sai.profile
60+
echo "DEVICE_MAC_ADDRESS=$ALIGNED_MAC_ADDRESS" >> /tmp/sai.profile
61+
}
62+
63+
config_syncd_centec()
64+
{
65+
CMD_ARGS+=" -p /tmp/sai.profile"
66+
67+
[ -e /dev/linux_dal ] || mknod /dev/linux_dal c 198 0
68+
[ -e /dev/net/tun ] || ( mkdir -p /dev/net && mknod /dev/net/tun c 10 200 )
69+
70+
# Read MAC address and align the last 6 bits.
71+
MAC_ADDRESS=$(ip link show eth0 | grep ether | awk '{print $2}')
72+
last_byte=$(python -c "print '$MAC_ADDRESS'[-2:]")
73+
aligned_last_byte=$(python -c "print format(int(int('$last_byte', 16) & 0b11000000), '02x')") # put mask and take away the 0x prefix
74+
ALIGNED_MAC_ADDRESS=$(python -c "print '$MAC_ADDRESS'[:-2] + '$aligned_last_byte'") # put aligned byte into the end of MAC
75+
76+
# Write MAC address into /tmp/profile file.
77+
cat $HWSKU_DIR/sai.profile > /tmp/sai.profile
78+
echo "DEVICE_MAC_ADDRESS=$ALIGNED_MAC_ADDRESS" >> /tmp/sai.profile
79+
}
80+
81+
config_syncd_cavium()
82+
{
83+
CMD_ARGS+=" -p /etc/ssw/AS7512/profile.ini -N"
84+
85+
export XP_ROOT=/usr/bin/
86+
87+
# Wait until redis-server starts
88+
until [ $(redis-cli ping | grep -c PONG) -gt 0 ]; do
89+
sleep 1
90+
done
91+
92+
redis-cli FLUSHALL
93+
}
94+
95+
config_syncd()
96+
{
97+
if [ "$SONIC_ASIC_TYPE" == "broadcom" ]; then
98+
config_syncd_bcm
99+
elif [ "$SONIC_ASIC_TYPE" == "mellanox" ]; then
100+
config_syncd_mlnx
101+
elif [ "$SONIC_ASIC_TYPE" == "cavium" ]; then
102+
config_syncd_cavium
103+
elif [ "$SONIC_ASIC_TYPE" == "centec" ]; then
104+
config_syncd_centec
105+
else
106+
echo "Unknown ASIC type $SONIC_ASIC_TYPE"
107+
exit 1
108+
fi
109+
110+
if [ ${ENABLE_SAITHRIFT} == 1 ]; then
111+
CMD_ARGS+=" -r -m $HWSKU_DIR/port_config.ini"
112+
fi
113+
114+
[ -r $PLATFORM_DIR/syncd.conf ] && . $PLATFORM_DIR/syncd.conf
115+
}
116+

syncd/scripts/syncd_start.sh

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/usr/bin/env bash
2+
#
3+
# Script to start syncd using supervisord
4+
#
5+
6+
# Source the file that holds common code for systemd and supervisord
7+
. /usr/bin/syncd_init_common.sh
8+
9+
10+
config_syncd
11+
12+
exec ${CMD} ${CMD_ARGS}
13+

0 commit comments

Comments
 (0)