From 1f5c9854e426f565c17969a6c80e985161d21516 Mon Sep 17 00:00:00 2001 From: Ze Gan Date: Tue, 19 Dec 2023 13:41:15 +0800 Subject: [PATCH 1/6] Add remote database logic for dpu Signed-off-by: Ze Gan --- dockers/docker-database/database_config.json.j2 | 17 +++++++++++++---- dockers/docker-database/docker-database-init.sh | 13 +++++++++++++ 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/dockers/docker-database/database_config.json.j2 b/dockers/docker-database/database_config.json.j2 index bacc1d356649..c685d15b4471 100644 --- a/dockers/docker-database/database_config.json.j2 +++ b/dockers/docker-database/database_config.json.j2 @@ -1,3 +1,4 @@ +{% set include_remote_db = (REMOTE_DB_IP is defined and REMOTE_DB_PORT is defined) %} { "INSTANCES": { "redis":{ @@ -15,6 +16,14 @@ "unix_socket_path": "/var/run/redis-chassis/redis_chassis.sock", "persistence_for_warm_boot" : "yes" } +{% if include_remote_db %} + ,"remote_redis":{ + "hostname" : "{{REMOTE_DB_IP}}", + "port" : {{REMOTE_DB_PORT}}, + "unix_socket_path": "" + "persistence_for_warm_boot" : "yes" + } +{% endif %} }, "DATABASES" : { "APPL_DB" : { @@ -102,23 +111,23 @@ "DPU_APPL_DB" : { "id" : 15, "separator": ":", - "instance" : "redis", + "instance" : {% if include_remote_db%} "remote_redis" {% else %} "redis" {% endif %}, "format": "proto" }, "DPU_APPL_STATE_DB" : { "id" : 16, "separator": "|", - "instance" : "redis" + "instance" : {% if include_remote_db%} "remote_redis" {% else %} "redis" {% endif %} }, "DPU_STATE_DB" : { "id" : 17, "separator": "|", - "instance" : "redis" + "instance" : {% if include_remote_db%} "remote_redis" {% else %} "redis" {% endif %} }, "DPU_COUNTERS_DB" : { "id" : 18, "separator": ":", - "instance" : "redis" + "instance" : {% if include_remote_db%} "remote_redis" {% else %} "redis" {% endif %} } {% endif %} }, diff --git a/dockers/docker-database/docker-database-init.sh b/dockers/docker-database/docker-database-init.sh index 9fe5ae242245..82670f62b02c 100755 --- a/dockers/docker-database/docker-database-init.sh +++ b/dockers/docker-database/docker-database-init.sh @@ -28,6 +28,19 @@ if [[ $DATABASE_TYPE == "dpudb" ]]; then redis_port=`expr 6381 + $DPU_ID` fi +# TODO: Check the current platform is DPU +midplane_ip=$(ip -4 -o addr | grep -v "169.254.200.254" | grep "169.254.200" | awk '{print $4}' | cut -d'/' -f1 | head -1 ) +if [[ $midplane_ip != "" ]] +then + export DATABASE_TYPE="dpudb" + export REMOTE_DB_IP="169.254.200.254" + IFS=. read -r a b c d <<< "169.254.200.1" + midplane_base_ip_num=$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d)) + # Determine the DB PORT from midplane IP + IFS=. read -r a b c d <<< $midplane_ip + midplane_ip_num=$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d)) + export REMOTE_DB_PORT=$((6381 + $midplane_ip_num - $midplane_base_ip_num)) +fi REDIS_DIR=/var/run/redis$NAMESPACE_ID mkdir -p $REDIS_DIR/sonic-db From e7ea7a94c4d94b3e01028ac1323cb2ab139754f0 Mon Sep 17 00:00:00 2001 From: Ze Gan Date: Thu, 1 Feb 2024 13:23:17 +0800 Subject: [PATCH 2/6] Rename database_name to container_name Signed-off-by: Ze Gan --- dockers/docker-database/database_global.json.j2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dockers/docker-database/database_global.json.j2 b/dockers/docker-database/database_global.json.j2 index 032f5e722e12..1760a96f0286 100644 --- a/dockers/docker-database/database_global.json.j2 +++ b/dockers/docker-database/database_global.json.j2 @@ -26,7 +26,7 @@ {% if dpu_cnt > 0 %} {% for dpu in range(dpu_cnt) %} { - "database_name" : "dpu{{dpu}}", + "container_name" : "dpu{{dpu}}", "include" : "../../redisdpu{{dpu}}/sonic-db/database_config.json" {% if dpu == dpu_cnt-1 %} } From ac8f06fc5aa780cf7ade17a28373d55cd2ad7a13 Mon Sep 17 00:00:00 2001 From: Ze Gan Date: Wed, 3 Apr 2024 16:40:01 +0800 Subject: [PATCH 3/6] Get NUM_DPU from platform.json Signed-off-by: Ze Gan --- files/build_templates/docker_image_ctl.j2 | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/files/build_templates/docker_image_ctl.j2 b/files/build_templates/docker_image_ctl.j2 index 95c2e1fa52a8..15d89d41517c 100644 --- a/files/build_templates/docker_image_ctl.j2 +++ b/files/build_templates/docker_image_ctl.j2 @@ -341,6 +341,12 @@ start() { source $PLATFORM_ENV_CONF fi + # Parse the platform.json file to get the platform specific information + PLATFORM_JSON=/usr/share/sonic/device/$PLATFORM/platform.json + if [ -f "$PLATFORM_JSON" ]; then + NUM_DPU=$(cat $PLATFORM_JSON | jq -r '.DPUS | length') + fi + {%- if sonic_asic_platform == "broadcom" %} {%- if docker_container_name == "syncd" %} # Set the SYNCD_SHM_SIZE if this variable not defined From 9b7d70ce4ee533822bb63be1e89a804bba598171 Mon Sep 17 00:00:00 2001 From: Ze Gan Date: Wed, 3 Apr 2024 18:09:57 +0800 Subject: [PATCH 4/6] Refactor remote db Signed-off-by: Ze Gan --- .../docker-database/docker-database-init.sh | 21 +++++++++---------- files/build_templates/docker_image_ctl.j2 | 9 +++++++- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/dockers/docker-database/docker-database-init.sh b/dockers/docker-database/docker-database-init.sh index 8de6d0a2b825..467f33c212d1 100755 --- a/dockers/docker-database/docker-database-init.sh +++ b/dockers/docker-database/docker-database-init.sh @@ -28,18 +28,17 @@ if [[ $DATABASE_TYPE == "dpudb" ]]; then redis_port=`expr 6381 + $DPU_ID` fi -# TODO: Check the current platform is DPU -midplane_ip=$(ip -4 -o addr | grep -v "169.254.200.254" | grep "169.254.200" | awk '{print $4}' | cut -d'/' -f1 | head -1 ) -if [[ $midplane_ip != "" ]] +if [[ $IS_DPU_DEVICE == "true" ]] then - export DATABASE_TYPE="dpudb" - export REMOTE_DB_IP="169.254.200.254" - IFS=. read -r a b c d <<< "169.254.200.1" - midplane_base_ip_num=$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d)) - # Determine the DB PORT from midplane IP - IFS=. read -r a b c d <<< $midplane_ip - midplane_ip_num=$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d)) - export REMOTE_DB_PORT=$((6381 + $midplane_ip_num - $midplane_base_ip_num)) + midplane_ip=$( ip -4 -o addr show eth0-midplane | awk '{print $4}' | cut -d'/' -f1 ) + if [[ $midplane_ip != "" ]] + then + export DATABASE_TYPE="dpudb" + export REMOTE_DB_IP="169.254.200.254" + # Determine the DB PORT from midplane IP + IFS=. read -r a b c d <<< $midplane_ip + export REMOTE_DB_PORT=$((6380 + $d)) + fi fi REDIS_DIR=/var/run/redis$NAMESPACE_ID diff --git a/files/build_templates/docker_image_ctl.j2 b/files/build_templates/docker_image_ctl.j2 index 15d89d41517c..235480c80fd3 100644 --- a/files/build_templates/docker_image_ctl.j2 +++ b/files/build_templates/docker_image_ctl.j2 @@ -344,7 +344,13 @@ start() { # Parse the platform.json file to get the platform specific information PLATFORM_JSON=/usr/share/sonic/device/$PLATFORM/platform.json if [ -f "$PLATFORM_JSON" ]; then - NUM_DPU=$(cat $PLATFORM_JSON | jq -r '.DPUS | length') + NUM_DPU=$(jq -r '.DPUS | length' $PLATFORM_JSON 2>/dev/null) + jq -e '.DPU' $PLATFORM_JSON >/dev/null + if [[ $? -eq 0 ]]; then + IS_DPU_DEVICE="true" + else + IS_DPU_DEVICE="false" + fi fi {%- if sonic_asic_platform == "broadcom" %} @@ -539,6 +545,7 @@ start() { DB_OPT=$DB_OPT" -v /var/run/redis$DEV:/var/run/redis:rw " DB_OPT=$DB_OPT" --env DATABASE_TYPE=$DATABASE_TYPE " DB_OPT=$DB_OPT" --env NUM_DPU=$NUM_DPU " + DB_OPT=$DB_OPT" --env IS_DPU_DEVICE=$IS_DPU_DEVICE " if [[ "$DEV" ]]; then DB_OPT=$DB_OPT" -v /var/run/redis$DEV:/var/run/redis$DEV:rw " fi From dc04804771296d25966eff185ad959d9808cd37c Mon Sep 17 00:00:00 2001 From: Ze Gan Date: Mon, 8 Apr 2024 14:52:26 +0800 Subject: [PATCH 5/6] Mount all database to other containers Signed-off-by: Ze Gan --- files/build_templates/docker_image_ctl.j2 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/files/build_templates/docker_image_ctl.j2 b/files/build_templates/docker_image_ctl.j2 index 235480c80fd3..694fdd1f85a9 100644 --- a/files/build_templates/docker_image_ctl.j2 +++ b/files/build_templates/docker_image_ctl.j2 @@ -502,7 +502,7 @@ start() { redis_dir_list=`/usr/bin/python -c "import sys; import os; import json; f=open(sys.argv[1]); \ global_db_dir = os.path.dirname(sys.argv[1]); data=json.load(f); \ print(\" \".join([os.path.normpath(global_db_dir+'/'+elem['include']).partition('sonic-db')[0]\ - for elem in data['INCLUDES'] if 'namespace' in elem])); f.close()" $SONIC_DB_GLOBAL_JSON` + for elem in data['INCLUDES'] if 'namespace' in elem or 'container_name' in elem ])); f.close()" $SONIC_DB_GLOBAL_JSON` fi {%- if docker_container_name == "database" %} From 10fd6e543a534f827d4aeb849a171f1da0d6ecbf Mon Sep 17 00:00:00 2001 From: Ze Gan Date: Tue, 9 Apr 2024 10:06:16 +0800 Subject: [PATCH 6/6] fix indent Signed-off-by: Ze Gan --- dockers/docker-database/database_config.json.j2 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dockers/docker-database/database_config.json.j2 b/dockers/docker-database/database_config.json.j2 index c685d15b4471..b5c231a93f41 100644 --- a/dockers/docker-database/database_config.json.j2 +++ b/dockers/docker-database/database_config.json.j2 @@ -111,23 +111,23 @@ "DPU_APPL_DB" : { "id" : 15, "separator": ":", - "instance" : {% if include_remote_db%} "remote_redis" {% else %} "redis" {% endif %}, + "instance" : {% if include_remote_db %} "remote_redis" {% else %} "redis" {% endif %}, "format": "proto" }, "DPU_APPL_STATE_DB" : { "id" : 16, "separator": "|", - "instance" : {% if include_remote_db%} "remote_redis" {% else %} "redis" {% endif %} + "instance" : {% if include_remote_db %} "remote_redis" {% else %} "redis" {% endif %} }, "DPU_STATE_DB" : { "id" : 17, "separator": "|", - "instance" : {% if include_remote_db%} "remote_redis" {% else %} "redis" {% endif %} + "instance" : {% if include_remote_db %} "remote_redis" {% else %} "redis" {% endif %} }, "DPU_COUNTERS_DB" : { "id" : 18, "separator": ":", - "instance" : {% if include_remote_db%} "remote_redis" {% else %} "redis" {% endif %} + "instance" : {% if include_remote_db %} "remote_redis" {% else %} "redis" {% endif %} } {% endif %} },