Skip to content

Commit 9f08f88

Browse files
authored
[dpu]: Add DPU database service (#17161)
Sub PRs: sonic-net/sonic-host-services#84 #17191 Why I did it According to the design, the database instances of DPU will be kept in the NPU host. Microsoft ADO (number only): 25072889 How I did it To follow the multiple ASIC design, I assume a new platform environment variable NUM_DPU will be defined in the /usr/share/sonic/device/$PLATFORM/platform_env.conf. Based on this number, NPU host will launch a corresponding number of instances for the DPU database. Signed-off-by: Ze Gan <[email protected]>
1 parent 00a9412 commit 9f08f88

File tree

6 files changed

+119
-15
lines changed

6 files changed

+119
-15
lines changed

dockers/docker-database/Dockerfile.j2

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ RUN apt-get clean -y && \
3232
s/^# unixsocket/unixsocket/; \
3333
s/redis-server.sock/redis.sock/g; \
3434
s/^client-output-buffer-limit pubsub [0-9]+mb [0-9]+mb [0-9]+/client-output-buffer-limit pubsub 0 0 0/; \
35-
s/^notify-keyspace-events ""$/notify-keyspace-events AKE/ \
35+
s/^notify-keyspace-events ""$/notify-keyspace-events AKE/; \
36+
s/^databases [0-9]+$/databases 100/ \
3637
' /etc/redis/redis.conf
3738

3839
COPY ["supervisord.conf.j2", "/usr/share/sonic/templates/"]

dockers/docker-database/database_config.json.j2

+29-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
"INSTANCES": {
33
"redis":{
44
"hostname" : "{{HOST_IP}}",
5-
"port" : 6379,
6-
"unix_socket_path" : "/var/run/redis{{NAMESPACE_ID}}/redis.sock",
5+
"port" : {{REDIS_PORT}},
6+
"unix_socket_path" : "/var/run/redis{{DEV}}/redis.sock",
77
"persistence_for_warm_boot" : "yes"
8+
{% if DATABASE_TYPE is defined and DATABASE_TYPE != "" %}
9+
,"database_type": "{{DATABASE_TYPE}}"
10+
{% endif %}
811
},
912
"redis_chassis":{
1013
"hostname" : "redis_chassis.server",
@@ -94,6 +97,30 @@
9497
"separator": ":",
9598
"instance" : "redis"
9699
}
100+
{% if DATABASE_TYPE is defined and DATABASE_TYPE == "dpudb" %}
101+
,
102+
"DPU_APPL_DB" : {
103+
"id" : 15,
104+
"separator": ":",
105+
"instance" : "redis",
106+
"format": "proto"
107+
},
108+
"DPU_APPL_STATE_DB" : {
109+
"id" : 16,
110+
"separator": "|",
111+
"instance" : "redis"
112+
},
113+
"DPU_STATE_DB" : {
114+
"id" : 17,
115+
"separator": "|",
116+
"instance" : "redis"
117+
},
118+
"DPU_COUNTERS_DB" : {
119+
"id" : 18,
120+
"separator": ":",
121+
"instance" : "redis"
122+
}
123+
{% endif %}
97124
},
98125
"VERSION" : "1.0"
99126
}

dockers/docker-database/database_global.json.j2

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
{% set namespace_cnt = NAMESPACE_COUNT|int %}
2+
{% if NUM_DPU is defined %}
3+
{% set dpu_cnt = NUM_DPU | int %}
4+
{% else %}
5+
{% set dpu_cnt = 0 %}
6+
{% endif %}
27
{
38
"INCLUDES" : [
49
{
510
"include" : "../../redis/sonic-db/database_config.json"
611
},
12+
713
{% if namespace_cnt > 1 %}
814
{% for ns in range(namespace_cnt) %}
915
{
@@ -15,7 +21,22 @@
1521
},
1622
{% endif %}
1723
{% endfor %}
24+
{% endif %}
25+
26+
{% if dpu_cnt > 0 %}
27+
{% for dpu in range(dpu_cnt) %}
28+
{
29+
"database_type" : "dpudb",
30+
"include" : "../../redisdpu{{dpu}}/sonic-db/database_config.json"
31+
{% if dpu == dpu_cnt-1 %}
32+
}
33+
{% else %}
34+
},
35+
{% endif %}
36+
{% endfor %}
37+
{% endif %}
38+
1839
],
1940
"VERSION" : "1.0"
2041
}
21-
{% endif %}
42+

dockers/docker-database/docker-database-init.sh

+14-2
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,26 @@ then
1717
host_ip=127.0.0.1
1818
fi
1919

20+
redis_port=6379
21+
22+
if [[ $DATABASE_TYPE == "dpudb" ]]; then
23+
host_ip="169.254.200.254"
24+
if ! ip -4 -o addr | awk '{print $4}' | grep $host_ip; then
25+
host_ip=127.0.0.1
26+
fi
27+
DPU_ID=`echo $DEV | tr -dc '0-9'`
28+
redis_port=`expr 6381 + $DPU_ID`
29+
fi
30+
31+
2032
REDIS_DIR=/var/run/redis$NAMESPACE_ID
2133
mkdir -p $REDIS_DIR/sonic-db
2234
mkdir -p /etc/supervisor/conf.d/
2335

2436
if [ -f /etc/sonic/database_config$NAMESPACE_ID.json ]; then
2537
cp /etc/sonic/database_config$NAMESPACE_ID.json $REDIS_DIR/sonic-db/database_config.json
2638
else
27-
HOST_IP=$host_ip j2 /usr/share/sonic/templates/database_config.json.j2 > $REDIS_DIR/sonic-db/database_config.json
39+
HOST_IP=$host_ip REDIS_PORT=$redis_port DATABASE_TYPE=$DATABASE_TYPE j2 /usr/share/sonic/templates/database_config.json.j2 > $REDIS_DIR/sonic-db/database_config.json
2840
fi
2941

3042
# on VoQ system, we only publish redis_chassis instance and CHASSIS_APP_DB when
@@ -59,7 +71,7 @@ if [[ $DATABASE_TYPE == "chassisdb" ]]; then
5971
fi
6072

6173
# copy/generate the database_global.json file if this is global database service in multi asic platform.
62-
if [[ $NAMESPACE_ID == "" ]] && [[ $NAMESPACE_COUNT -gt 1 ]]
74+
if [[ $DATABASE_TYPE == "" ]] && [[ $NAMESPACE_COUNT -gt 1 || $NUM_DPU -gt 1 ]]
6375
then
6476
if [ -f /etc/sonic/database_global.json ]; then
6577
cp /etc/sonic/database_global.json $REDIS_DIR/sonic-db/database_global.json

files/build_templates/docker_image_ctl.j2

+26-9
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function updateSyslogConf()
5858
}
5959
function ebtables_config()
6060
{
61-
if [ "$DEV" ]; then
61+
if [[ "$DEV" && $DATABASE_TYPE != "dpudb" ]]; then
6262
# Install ebtables filter in namespaces on multi-asic.
6363
ip netns exec $NET_NS ebtables-restore < /etc/ebtables.filter.cfg
6464
else
@@ -169,7 +169,7 @@ function postStartAction()
169169
{%- if docker_container_name == "database" %}
170170
CHASSISDB_CONF="/usr/share/sonic/device/$PLATFORM/chassisdb.conf"
171171
[ -f $CHASSISDB_CONF ] && source $CHASSISDB_CONF
172-
if [ "$DEV" ]; then
172+
if [[ "$DEV" && $DATABASE_TYPE != "dpudb" ]]; then
173173
# Enable the forwarding on eth0 interface in namespace.
174174
SYSCTL_NET_CONFIG="/etc/sysctl.d/sysctl-net.conf"
175175
docker exec -i database$DEV sed -i -e "s/^net.ipv4.conf.eth0.forwarding=0/net.ipv4.conf.eth0.forwarding=1/;
@@ -223,7 +223,7 @@ function postStartAction()
223223
# databases are not availbale until database container is ready.
224224
# also chassisdb doesn't support warm/fast reboot, its dump.rdb is deleted
225225
# at service startup time, nothing need to be done here.
226-
if [ "$DATABASE_TYPE" != "chassisdb" ]; then
226+
if [[ "$DATABASE_TYPE" != "chassisdb" ]]; then
227227
# Wait until supervisord and redis starts. This change is needed
228228
# because now database_config.json is jinja2 templated based
229229
# and by the time file gets generated if we do redis ping
@@ -503,7 +503,7 @@ start() {
503503
fi
504504
{%- endif %}
505505

506-
if [ -z "$DEV" ]; then
506+
if [[ -z "$DEV" || $DATABASE_TYPE == "dpudb" ]]; then
507507
NET="host"
508508

509509
# For Multi-ASIC platform we have to mount the redis paths for database instances running in different
@@ -528,6 +528,11 @@ start() {
528528
DB_OPT=$DB_OPT" --env DATABASE_TYPE=$DATABASE_TYPE"
529529
else
530530
DB_OPT=$DB_OPT" -v /var/run/redis$DEV:/var/run/redis:rw "
531+
DB_OPT=$DB_OPT" --env DATABASE_TYPE=$DATABASE_TYPE "
532+
DB_OPT=$DB_OPT" --env NUM_DPU=$NUM_DPU "
533+
if [[ "$DEV" ]]; then
534+
DB_OPT=$DB_OPT" -v /var/run/redis$DEV:/var/run/redis$DEV:rw "
535+
fi
531536
fi
532537
{%- endif %}
533538
else
@@ -557,6 +562,12 @@ start() {
557562
fi
558563
fi
559564
{%- endif %}
565+
566+
NAMESPACE_ID="$DEV"
567+
if [[ $DATABASE_TYPE == "dpudb" ]]; then
568+
NAMESPACE_ID=""
569+
fi
570+
560571
{%- if sonic_asic_platform == "mellanox" %}
561572
# TODO: Mellanox will remove the --tmpfs exception after SDK socket path changed in new SDK version
562573
{%- endif %}
@@ -630,9 +641,10 @@ start() {
630641
{%- if mount_default_tmpfs|default("n") == "y" %}
631642
--tmpfs /var/tmp \
632643
{%- endif %}
633-
--env "NAMESPACE_ID"="$DEV" \
644+
--env "NAMESPACE_ID"="$NAMESPACE_ID" \
634645
--env "NAMESPACE_PREFIX"="$NAMESPACE_PREFIX" \
635-
--env "NAMESPACE_COUNT"=$NUM_ASIC \
646+
--env "NAMESPACE_COUNT"="$NUM_ASIC" \
647+
--env "DEV"="$DEV" \
636648
--env "CONTAINER_NAME"=$DOCKERNAME \
637649
--name=$DOCKERNAME \
638650
{%- if docker_container_name == "gbsyncd" %}
@@ -668,7 +680,7 @@ wait() {
668680
stop() {
669681
{%- if docker_container_name == "database" %}
670682
docker stop $DOCKERNAME
671-
if [ "$DEV" ]; then
683+
if [[ "$DEV" && $DATABASE_TYPE != "dpudb" ]]; then
672684
ip netns delete "$NET_NS"
673685
fi
674686
{%- elif docker_container_name == "teamd" %}
@@ -684,7 +696,7 @@ stop() {
684696
kill() {
685697
{%- if docker_container_name == "database" %}
686698
docker kill $DOCKERNAME
687-
if [ "$DEV" ]; then
699+
if [[ "$DEV" && $DATABASE_TYPE != "dpudb" ]]; then
688700
ip netns delete "$NET_NS"
689701
fi
690702
{%- else %}
@@ -701,11 +713,16 @@ if [ "$DEV" == "chassisdb" ]; then
701713
DOCKERNAME=$DOCKERNAME"-chassis"
702714
unset DEV
703715
fi
716+
717+
if [[ "$DEV" == *"dpu"* ]]; then
718+
DATABASE_TYPE="dpudb"
719+
fi
720+
704721
{%- endif %}
705722
NAMESPACE_PREFIX="asic"
706723
DOCKERNAME=$DOCKERNAME$DEV
707724
CONTAINER_EXISTS="no"
708-
if [ "$DEV" ]; then
725+
if [[ "$DEV" && $DATABASE_TYPE != "dpudb" ]]; then
709726
NET_NS="$NAMESPACE_PREFIX$DEV" #name of the network namespace
710727

711728
SONIC_CFGGEN="sonic-cfggen -n $NET_NS"

src/sonic-py-common/sonic_py_common/device_info.py

+26
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
CHASSIS_INFO_MODEL_FIELD = 'model'
4646
CHASSIS_INFO_REV_FIELD = 'revision'
4747

48+
# DPU constants
49+
DPU_NAME_PREFIX = "dpu"
50+
4851
# Cacheable Objects
4952
sonic_ver_info = {}
5053
hw_info_dict = {}
@@ -841,3 +844,26 @@ def is_frontend_port_present_in_host():
841844
if not namespace_id:
842845
return False
843846
return True
847+
848+
849+
def get_num_dpus():
850+
# Todo: we should use platform api to get the dpu number
851+
# instead of rely on the platform env config.
852+
num_dpus = 0
853+
platform_env_conf_file_path = get_platform_env_conf_file_path()
854+
855+
# platform_env.conf file not present for platform
856+
if platform_env_conf_file_path is None:
857+
return num_dpus
858+
859+
# Else open the file check for keyword - num_dpu -
860+
with open(platform_env_conf_file_path) as platform_env_conf_file:
861+
for line in platform_env_conf_file:
862+
tokens = line.split('=')
863+
if len(tokens) < 2:
864+
continue
865+
if tokens[0].lower() == 'num_dpu':
866+
num_dpus = tokens[1].strip()
867+
break
868+
return int(num_dpus)
869+

0 commit comments

Comments
 (0)