Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify syncd init script for supporting yml #1411

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
225 changes: 164 additions & 61 deletions syncd/scripts/syncd_init_common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -119,74 +119,177 @@ config_syncd_cisco_8000()
fi
}

config_syncd_bcm()
function merge_config_bcm_files()
{
to_file=$1
from_file=$2
message=$3
override=false
echo "" >> $to_file
echo "# Start of $message" >> $to_file
while read line
do
line=$( echo $line | xargs )
if [ ! -z "$line" ];then
if [ "${line::1}" == '#' ];then
echo $line >> $to_file
elif [ "$line" == "[Overwrite Section]" ];then

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is an extra space at end, no match will happen, right? How to consider this scenario?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the script will skip empty lines

override=true
echo "# $line" >> $to_file
echo "Merge properties with override $override"
else
sedline=${line%=*}
if grep -q $sedline $to_file ;then

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@geans-pin If there is "a=1" in overwrite section and "aa=1" in to_file, the sedline is a, but grep -q $sedline $to_file, still return true for this case, right? Will aa=1 be overwritten by a=1?

Copy link
Contributor Author

@geans-pin geans-pin Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the config property, we grep for the property name. It's unlikely to have the a and aa case.
The property name will be coming along with a meaningful naming prefix.

if $override ;then
echo "Override the config $(grep $sedline $to_file) with $line in $to_file"
prop=${line:0:`expr index $line =`}
sed -i "/$prop/d" $to_file
echo $line >> $to_file
else
grepline=$(grep $sedline $to_file)
if [ "${grepline::1}" == '#' ];then
echo $line >> $to_file
else
echo "Keep the config $(grep $sedline $to_file) in $to_file"
fi
fi
else
echo $line >> $to_file
fi
fi
fi
done < $from_file
echo "# End of $message" >> $to_file
echo "Merged $from_file to $to_file"
}

if [ -f $PLATFORM_DIR/common_config_support ];then

PLATFORM_COMMON_DIR=/usr/share/sonic/device/x86_64-broadcom_common

cp -f $HWSKU_DIR/*.config.bcm /tmp
cp -f /etc/sai.d/sai.profile /tmp
CONFIG_BCM=$(find /tmp -name '*.bcm')
PLT_CONFIG_BCM=$(find $HWSKU_DIR -name '*.bcm')
SAI_PROFILE=$(find /tmp -name 'sai.profile')
sed -i 's+/usr/share/sonic/hwsku+/tmp+g' $SAI_PROFILE

#Get first three characters of chip id
readline=$(grep '0x14e4' /proc/linux-kernel-bde)
chip_id=${readline#*0x14e4:0x}
chip_id=${chip_id::3}
COMMON_CONFIG_BCM=$(find $PLATFORM_COMMON_DIR/x86_64-broadcom_${chip_id} -name '*.bcm')

if [ -f $PLATFORM_COMMON_DIR/x86_64-broadcom_${chip_id}/*.bcm ]; then
for file in $CONFIG_BCM; do
echo "" >> $file
echo "# Start of chip common properties" >> $file
while read line
do
line=$( echo $line | xargs )
if [ ! -z "$line" ];then
if [ "${line::1}" == '#' ];then
echo $line >> $file
function merge_config_yml_files()
{
to_file=$1
from_file=$2
message=$3
override=false
merged_cnt=0
echo "" >> $to_file
echo "# Start of $message" >> $to_file
while read line
do
line=$( echo $line | xargs )
if [ ! -z "$line" ];then
if [ "${line::1}" == '#' ];then
echo " $line" >> $to_file
elif [ "$line" == "[Overwrite Section]"];then

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@geans-pin Overwrite section should be after [Normal Section], if we move overwrite section before [Normal Section], the logic here will not work and will overwrite all properties, is it right? Do we have a comment to remind this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, good catch. Let's have a comment in the script and HLD

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Had added the comment in HLD for reminding this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also add comment in script

#Overwrite section in common config should
#be located after normal section
override=true
echo " # $line" >> $to_file
echo "Merge properties with override $override"
else
sedline=${line%:*}
if grep -q $sedline $to_file ;then
if $override ;then
echo "Override the config $(grep $sedline $to_file) with $line in $to_file"
prop=${line:0:`expr index "$line" :`}
sed -i "/$prop/d" $to_file
echo " $line" >> $to_file
merged_cnt+=1
else
sedline=${line%=*}
if grep -q $sedline $file ;then
echo "Keep the config $(grep $sedline $file) in $file"
else
echo $line >> $file
fi
grepline=$(grep $sedline $to_file)
grepline="${grepline#"${grepline%%[![:space:]]*}"}"
if [ "${grepline::1}" == '#' ];then
echo " $line" >> $to_file
merged_cnt+=1
else
echo "Keep the config $(grep $sedline $to_file) in $to_file"
fi
fi
fi
done < $COMMON_CONFIG_BCM
echo "# End of chip common properties" >> $file
done
echo "Merging $PLT_CONFIG_BCM with $COMMON_CONFIG_BCM, merge files stored in $CONFIG_BCM"
fi

#sync the file system
sync

# copy the final config.bcm and sai.profile to the shared folder for 'show tech'
cp -f /tmp/sai.profile /var/run/sswsyncd/
cp -f /tmp/*.bcm /var/run/sswsyncd/

if [ -f "/tmp/sai.profile" ]; then
CMD_ARGS+=" -p /tmp/sai.profile"
elif [ -f "/etc/sai.d/sai.profile" ]; then
CMD_ARGS+=" -p /etc/sai.d/sai.profile"
else
CMD_ARGS+=" -p $HWSKU_DIR/sai.profile"
fi
else
echo " $line" >> $to_file
merged_cnt+=1
fi
fi
fi
done < $from_file

else
if [ $merged_cnt -gt 0 ]; then
sed -i "/# Start of/a \ global:" $to_file
sed -i "/# Start of/a \ 0:" $to_file
sed -i "/# Start of/a \bcm_device:" $to_file
sed -i "/# Start of/a \---" $to_file
fi
echo "# End of $message" >> $to_file
if [ $merged_cnt -gt 0 ]; then
sed -i "/# End of/i \..." $to_file
fi
echo "Merged $from_file to $to_file"
}

if [ -f "/etc/sai.d/sai.profile" ]; then
CMD_ARGS+=" -p /etc/sai.d/sai.profile"
else
CMD_ARGS+=" -p $HWSKU_DIR/sai.profile"
fi
config_syncd_bcm()
{
PLATFORM_COMMON_DIR=/usr/share/sonic/device/x86_64-broadcom_common
PLT_CONFIG_BCM=$(find $HWSKU_DIR -name '*.bcm' -not -path "*/phy/*")
PLT_CONFIG_YML=$(find $HWSKU_DIR -name '*.yml')

if [ ! -z "$PLT_CONFIG_BCM" ] && [ -f $PLATFORM_DIR/common_config_support ] ; then
cp -f $HWSKU_DIR/*.config.bcm /tmp
cp -f /etc/sai.d/sai.profile /tmp
CONFIG_BCM=$(find /tmp -name '*.bcm')
SAI_PROFILE=$(find /tmp -name 'sai.profile')
sed -i 's+/usr/share/sonic/hwsku+/tmp+g' $SAI_PROFILE

#Get first three characters of chip id
readline=$(grep '0x14e4' /proc/linux-kernel-bde)
chip_id=${readline#*0x14e4:0x}
chip_id=${chip_id::3}
COMMON_CONFIG_BCM=$(find $PLATFORM_COMMON_DIR/x86_64-broadcom_${chip_id} -maxdepth 1 -name '*.bcm')

if [ -f $PLATFORM_COMMON_DIR/x86_64-broadcom_${chip_id}/*.bcm ]; then
for file in $CONFIG_BCM; do
merge_config_bcm_files $file $COMMON_CONFIG_BCM "chip common properties"
done
echo "Merging $PLT_CONFIG_BCM with $COMMON_CONFIG_BCM, merge files stored in $CONFIG_BCM"
fi
#sync the file system
sync

# copy the final config.bcm and sai.profile to the shared folder for 'show tech'
cp -f /tmp/sai.profile /var/run/sswsyncd/
cp -f /tmp/*.bcm /var/run/sswsyncd/
fi

if [ ! -z "$PLT_CONFIG_YML" ] && [ -f $PLATFORM_DIR/common_config_support ]; then
cp -f $HWSKU_DIR/*.config.yml /tmp
cp -f /etc/sai.d/sai.profile /tmp
CONFIG_YML=$(find /tmp -name '*.yml')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We use the file name .yml suffix to identify the file format. Can you change the sai.profile and *.config.bcm to *.yml as following BRCM SDK rule ?

SAI_PROFILE=$(find /tmp -name 'sai.profile')
sed -i 's+/usr/share/sonic/hwsku+/tmp+g' $SAI_PROFILE

#Get first three characters of chip id
readline=$(grep '0:14e4' /proc/linux_ngbde)
chip_id=${readline#*0:14e4:}
chip_id=${chip_id::3}
COMMON_CONFIG_BCM=$(find $PLATFORM_COMMON_DIR/x86_64-broadcom_${chip_id} -maxdepth 1 -name '*.bcm')

if [ -f $PLATFORM_COMMON_DIR/x86_64-broadcom_${chip_id}/*.bcm ]; then
for file in $CONFIG_YML; do
merge_config_yml_files $file $COMMON_CONFIG_BCM "chip common properties"
done
echo "Merging $PLT_CONFIG_YML with $COMMON_CONFIG_BCM, merge files stored in $CONFIG_YML "
fi
#sync the file system
sync

# copy the final config.bcm and sai.profile to the shared folder for 'show tech'
cp -f /tmp/sai.profile /var/run/sswsyncd/
cp -f /tmp/*.yml /var/run/sswsyncd/
fi

if [ -f "/tmp/sai.profile" ]; then
CMD_ARGS+=" -p /tmp/sai.profile"
elif [ -f "/etc/sai.d/sai.profile" ]; then
CMD_ARGS+=" -p /etc/sai.d/sai.profile"
else
CMD_ARGS+=" -p $HWSKU_DIR/sai.profile"
fi

if [ -f "$HWSKU_DIR/context_config.json" ]; then
Expand Down
Loading