Skip to content

Commit 16eaece

Browse files
authored
Update p4rt configuration to match SONiC upstream schema. (sonic-net#10725)
*The initial commit for the P4RT docker hard coded all the flags which makes it difficult to configure at runtime. Reading them from the CONFIG_DB allows for more flexibility.
1 parent 59d570b commit 16eaece

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

dockers/docker-sonic-p4rt/Dockerfile.j2

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ RUN apt-get clean -y && \
2424
rm -rf /debs
2525

2626
COPY ["start.sh", "p4rt.sh", "/usr/bin/"]
27+
COPY ["p4rt_vars.j2", "/usr/share/sonic/templates/"]
2728
COPY ["supervisord.conf", "/etc/supervisor/conf.d/"]
2829
COPY ["files/supervisor-proc-exit-listener", "/usr/bin"]
2930
COPY ["critical_processes", "/etc/supervisor"]

dockers/docker-sonic-p4rt/p4rt.sh

+90
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,95 @@
11
#!/usr/bin/env bash
22

3+
EXIT_P4RT_VARS_FILE_NOT_FOUND=1
4+
readonly P4RT_VARS_FILE=/usr/share/sonic/templates/p4rt_vars.j2
5+
6+
if [ ! -f "${P4RT_VARS_FILE}" ]; then
7+
echo "P4rt vars template file not found"
8+
exit ${EXIT_P4RT_VARS_FILE_NOT_FOUND}
9+
fi
10+
11+
# Try to read p4rt and certs config from ConfigDB.
12+
# Use default value if no valid config exists
13+
P4RT_VARS=$(sonic-cfggen -d -t ${P4RT_VARS_FILE})
14+
readonly P4RT_VARS=${P4RT_VARS//[\']/\"}
15+
readonly X509=$(echo ${P4RT_VARS} | jq -r '.x509')
16+
readonly P4RT=$(echo ${P4RT_VARS} | jq -r '.p4rt')
17+
readonly CERTS=$(echo ${P4RT_VARS} | jq -r '.certs')
18+
319
P4RT_ARGS=" --alsologtostderr --logbuflevel=-1"
420

21+
if [ -n "${CERTS}" ]; then
22+
readonly SERVER_CRT=$(echo ${CERTS} | jq -r '.server_crt // empty')
23+
readonly SERVER_KEY=$(echo ${CERTS} | jq -r '.server_key // empty')
24+
if [ -z "${SERVER_CRT}" ] || [ -z "${SERVER_KEY}" ]; then
25+
P4RT_ARGS+=" --use_insecure_server_credentials"
26+
else
27+
P4RT_ARGS+=" --server_certificate_file=${SERVER_CRT} --server_key_file=${SERVER_KEY}"
28+
fi
29+
30+
readonly CA_CRT=$(echo ${CERTS} | jq -r '.ca_crt // empty')
31+
if [ ! -z "${CA_CRT}" ]; then
32+
P4RT_ARGS+=" --ca_certificate_file=${CA_CRT}"
33+
readonly CRL=$(echo ${CERTS} | jq -r '.cert_crl_dir // empty')
34+
if [ ! -z "$CRL" ]; then
35+
P4RT_ARGS+=" --cert_crl_dir=${CRL}"
36+
fi
37+
fi
38+
elif [ -n "${X509}" ]; then
39+
readonly SERVER_CRT=$(echo ${X509} | jq -r '.server_crt // empty')
40+
readonly SERVER_KEY=$(echo ${X509} | jq -r '.server_key // empty')
41+
if [ -z "${SERVER_CRT}" ] || [ -z "${SERVER_KEY}" ]; then
42+
P4RT_ARGS+=" --use_insecure_server_credentials"
43+
else
44+
P4RT_ARGS+=" --server_certificate_file=${SERVER_CRT} --server_key_file=${SERVER_KEY}"
45+
fi
46+
47+
readonly CA_CRT=$(echo ${X509} | jq -r '.ca_crt // empty')
48+
if [ ! -z "${CA_CRT}" ]; then
49+
P4RT_ARGS+=" --ca_certificate_file=${CA_CRT}"
50+
readonly CRL=$(echo ${X509} | jq -r '.cert_crl_dir // empty')
51+
if [ ! -z "$CRL" ]; then
52+
P4RT_ARGS+=" --cert_crl_dir=${CRL}"
53+
fi
54+
fi
55+
else
56+
P4RT_ARGS+=" --use_insecure_server_credentials"
57+
fi
58+
59+
# Try to read P4RT authorization config from ConfigDB.
60+
readonly AUTHZ_FILE=$(echo ${P4RT} | jq -r '.authz_policy // empty')
61+
if [ ! -z "${AUTHZ_FILE}" ]; then
62+
P4RT_ARGS+=" --authz_policy_enabled --authorization_policy_file=${AUTHZ_FILE}"
63+
fi
64+
65+
# Try to read P4RT port config from ConfigDB.
66+
readonly PORT=$(echo ${P4RT} | jq -r '.port // empty')
67+
if [ ! -z "${PORT}" ]; then
68+
P4RT_ARGS+=" --p4rt_grpc_port=${PORT}"
69+
fi
70+
71+
# Try to read P4RT genetlink config from ConfigDB.
72+
readonly GENETLINK=$(echo ${P4RT} | jq -r '.use_genetlink // empty')
73+
if [ ! -z "${GENETLINK}" ]; then
74+
P4RT_ARGS+=" --use_genetlink=${GENETLINK}"
75+
fi
76+
77+
# Try to read P4RT port ID config from ConfigDB.
78+
readonly PORT_ID=$(echo ${P4RT} | jq -r '.use_port_ids // empty')
79+
if [ ! -z "${PORT_ID}" ]; then
80+
P4RT_ARGS+=" --use_port_ids=${PORT_ID}"
81+
fi
82+
83+
# Try to read P4RT save forwarding config from ConfigDB.
84+
readonly SAVE_FORWARDING_CONFIG=$(echo ${P4RT} | jq -r '.save_forwarding_config_file // empty')
85+
if [ ! -z "${SAVE_FORWARDING_CONFIG}" ]; then
86+
P4RT_ARGS+=" --save_forwarding_config_file=${SAVE_FORWARDING_CONFIG}"
87+
fi
88+
89+
# Try to read P4RT unix socket config from ConfigDB.
90+
readonly UNIX_SOCKET=$(echo ${P4RT} | jq -r '.p4rt_unix_socket // empty')
91+
if [ ! -z "${UNIX_SOCKET}" ]; then
92+
P4RT_ARGS+=" --p4rt_unix_socket=${UNIX_SOCKET}"
93+
fi
94+
595
exec /usr/local/bin/p4rt ${P4RT_ARGS}
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"certs": {% if P4RT %}{% if "certs" in P4RT.keys() %}{{ P4RT["certs"] }}{% else %}""{% endif %}{% else %}""{% endif %},
3+
"p4rt" : {% if P4RT %}{% if "p4rt_app" in P4RT.keys() %}{{ P4RT["p4rt_app"] }}{% else %}""{% endif %}{% else %}""{% endif %},
4+
"x509" : {% if DEVICE_METADATA %}{% if "x509" in DEVICE_METADATA.keys() %}{{ DEVICE_METADATA["x509"] }}{% else %}""{% endif %}{% else %}""{% endif %}
5+
}

0 commit comments

Comments
 (0)