Skip to content

Commit d23b8db

Browse files
sonic-buildimage: Bind linecard database instance to midplane IP
On a linecard, the database container is bound to localhost (127.0.0.1) by default. This prevents other components in the chassis, such as the supervisor card, from accessing the linecard's database over the midplane network. This change exposes the database instance to the midplane, allowing for remote access from the supervisor. Signed-off-by: manish <[email protected]>
1 parent 0f6ebe3 commit d23b8db

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ if [[ $DATABASE_TYPE == "chassisdb" ]]; then
8282
VAR_LIB_REDIS_CHASSIS_DIR="/var/lib/redis_chassis"
8383
mkdir -p $VAR_LIB_REDIS_CHASSIS_DIR
8484
update_chassisdb_config -j $db_cfg_file_tmp -k -p $chassis_db_port
85+
# Set protected mode based on the hostname
86+
additional_data_json=$(jq -c '{INSTANCES: .INSTANCES | map_values({is_protected_mode: (.hostname == "127.0.0.1")})}' "$db_cfg_file_tmp")
8587
# generate all redis server supervisord configuration file
86-
sonic-cfggen -j $db_cfg_file_tmp \
88+
sonic-cfggen -j $db_cfg_file_tmp -a "$additional_data_json" \
8789
-t /usr/share/sonic/templates/supervisord.conf.j2,/etc/supervisor/conf.d/supervisord.conf \
8890
-t /usr/share/sonic/templates/critical_processes.j2,/etc/supervisor/critical_processes
8991
rm $db_cfg_file_tmp
@@ -104,7 +106,13 @@ then
104106
fi
105107
# delete chassisdb config to generate supervisord config
106108
update_chassisdb_config -j $db_cfg_file_tmp -d
107-
sonic-cfggen -j $db_cfg_file_tmp \
109+
# Set protected mode based on the hostname
110+
additional_data_json=$(jq -c '{INSTANCES: .INSTANCES | map_values({is_protected_mode: (.hostname == "127.0.0.1")})}' "$db_cfg_file_tmp")
111+
# For Linecard databases, disable Redis protected mode to expose them to the midplane.
112+
if [ -f "$chassisdb_config" ] && [[ "$start_chassis_db" != "1" ]]; then
113+
additional_data_json=$(jq -c '{INSTANCES: .INSTANCES | map_values({is_protected_mode: false})}' "$db_cfg_file_tmp")
114+
fi
115+
sonic-cfggen -j "$db_cfg_file_tmp" -a "$additional_data_json" \
108116
-t /usr/share/sonic/templates/supervisord.conf.j2,/etc/supervisor/conf.d/supervisord.conf \
109117
-t /usr/share/sonic/templates/critical_processes.j2,/etc/supervisor/critical_processes
110118

dockers/docker-database/supervisord.conf.j2

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ dependent_startup=true
3535
{%- if redis_inst != 'remote_redis' %}
3636
[program:{{ redis_inst }}]
3737
{% if redis_items['hostname'] != '127.0.0.1' %}
38-
{%- set ADDITIONAL_OPTS = '--protected-mode no' %}
3938
{%- if redis_inst != 'redis_chassis' %}
4039
{%- set LOOPBACK_IP = '127.0.0.1' -%}
4140
{%- endif -%}
4241
{%- else -%}
4342
{%- set LOOPBACK_IP = '' -%}
43+
{%- endif -%}
44+
{%- if not redis_items['is_protected_mode'] %}
45+
{%- set ADDITIONAL_OPTS = '--protected-mode no' %}
46+
{%- else %}
4447
{%- set ADDITIONAL_OPTS = '' %}
4548
{%- endif -%}
4649
command=/bin/bash -c "{ [[ -s /var/lib/{{ redis_inst }}/dump.rdb ]] || rm -f /var/lib/{{ redis_inst }}/dump.rdb; } && mkdir -p /var/lib/{{ redis_inst }} && exec /usr/bin/redis-server /etc/redis/redis.conf --bind {{ LOOPBACK_IP }} {{ redis_items['hostname'] }} --port {{ redis_items['port'] }} --unixsocket {{ redis_items['unix_socket_path'] }} --pidfile /var/run/redis/{{ redis_inst }}.pid --dir /var/lib/{{ redis_inst }} {{ ADDITIONAL_OPTS }}"

files/build_templates/docker_image_ctl.j2

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ function postStartAction()
173173
{%- if docker_container_name == "database" %}
174174
midplane_ip=""
175175
CHASSISDB_CONF="/usr/share/sonic/device/$PLATFORM/chassisdb.conf"
176+
if [[ -f $CHASSISDB_CONF && $DATABASE_TYPE != "dpudb" ]]; then
177+
slot_id=$(python3 -c 'import sonic_platform.platform; platform_chassis = sonic_platform.platform.Platform().get_chassis(); print(platform_chassis.get_my_slot())' 2>/dev/null)
178+
supervisor_slot_id=$(python3 -c 'import sonic_platform.platform; platform_chassis = sonic_platform.platform.Platform().get_chassis(); print(platform_chassis.get_supervisor_slot())' 2>/dev/null)
179+
fi
176180
[ -f $CHASSISDB_CONF ] && source $CHASSISDB_CONF
177181
if [[ "$DEV" && $DATABASE_TYPE != "dpudb" ]]; then
178182
# Enable the forwarding on eth0 interface in namespace.
@@ -188,9 +192,6 @@ function postStartAction()
188192
ip netns exec "$NET_NS" ip addr add 127.0.0.1/16 dev lo
189193
ip netns exec "$NET_NS" ip addr del 127.0.0.1/8 dev lo
190194

191-
slot_id=$(python3 -c 'import sonic_platform.platform; platform_chassis = sonic_platform.platform.Platform().get_chassis(); print(platform_chassis.get_my_slot())' 2>/dev/null)
192-
supervisor_slot_id=$(python3 -c 'import sonic_platform.platform; platform_chassis = sonic_platform.platform.Platform().get_chassis(); print(platform_chassis.get_supervisor_slot())' 2>/dev/null)
193-
194195
# Create eth1 in database instance
195196
if [[ "${slot_id}" == "${supervisor_slot_id}" ]]; then
196197
ip link add name ns-eth1"$NET_NS" type veth peer name eth1@"$NET_NS"
@@ -235,6 +236,12 @@ function postStartAction()
235236
fi
236237
fi
237238
fi
239+
240+
# midplane ip for the Linecard database container
241+
if [[ -z "$DEV" && "$DATABASE_TYPE" != "dpudb" && -f $CHASSISDB_CONF && "${slot_id}" != "${supervisor_slot_id}" ]]; then
242+
midplane_ip=$(docker exec -i ${DOCKERNAME} ip addr show eth1-midplane | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1)
243+
fi
244+
238245
# Setup ebtables configuration
239246
{%- if sonic_asic_platform != "vs" %}
240247
ebtables_config
@@ -330,8 +337,8 @@ function postStartAction()
330337
REDIS_SOCK="/var/run/redis-chassis/redis_chassis.sock"
331338
fi
332339
chgrp -f redis $REDIS_SOCK && chmod -f 0760 $REDIS_SOCK
333-
334-
if [[ $DEV && $midplane_ip ]]; then
340+
# Binding the midplane ip to the redisdb
341+
if [[ -n "$midplane_ip" ]]; then
335342
IFS=_ read ip port < <(jq -r '.INSTANCES | [.redis.hostname, .redis.port] | join("_")' /var/run/redis$DEV/sonic-db/database_config.json)
336343
bound_ips=$(redis-cli --raw -h $ip -p $port config get bind | sed -n '2,2 p')
337344
redis-cli -h $ip -p $port config set bind "$bound_ips $midplane_ip"

0 commit comments

Comments
 (0)