forked from sonic-net/sonic-mgmt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopp_utils.py
More file actions
368 lines (285 loc) · 13 KB
/
Copy pathcopp_utils.py
File metadata and controls
368 lines (285 loc) · 13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
"""
Helpful utilities for writing tests for the COPP feature.
Todo:
Refactor ptfadapter so it can be leveraged in these test cases.
"""
import re
import logging
import json
from tests.common.config_reload import config_reload
DEFAULT_NN_TARGET_PORT = 3
_REMOVE_IP_SCRIPT = "scripts/remove_ip.sh"
_ADD_IP_SCRIPT = "scripts/add_ip.sh"
_ADD_IP_BACKEND_SCRIPT = "scripts/add_ip_backend.sh"
_UPDATE_COPP_SCRIPT = "copp/scripts/update_copp_config.py"
_BASE_COPP_CONFIG = "/tmp/base_copp_config.json"
_APP_DB_COPP_CONFIG = ":/etc/swss/config.d/00-copp.config.json"
_CONFIG_DB_COPP_CONFIG = "/etc/sonic/copp_cfg.json"
_TEMP_COPP_CONFIG = "/tmp/copp_config.json"
_TEMP_COPP_TEMPLATE = "/tmp/copp.json.j2"
_COPP_TEMPLATE_PATH = "/usr/share/sonic/templates/copp.json.j2"
_SWSS_COPP_TEMPLATE = ":" + _COPP_TEMPLATE_PATH
_DEFAULT_COPP_TEMPLATE = "/usr/share/sonic/templates/copp_cfg.j2"
_BASE_COPP_TEMPLATE = "/home/admin/copp_cfg_base.j2"
_PTF_NN_TEMPLATE = "templates/ptf_nn_agent.conf.ptf.j2"
_PTF_NN_DEST = "/etc/supervisor/conf.d/ptf_nn_agent.conf"
_SYNCD_NN_TEMPLATE = "templates/ptf_nn_agent.conf.dut.j2"
_SYNCD_NN_DEST = "/tmp/ptf_nn_agent.conf"
_SYNCD_NN_FILE = "ptf_nn_agent.conf"
_CONFIG_DB = "/etc/sonic/config_db.json"
_TEMP_CONFIG_DB = "/home/admin/config_db_copp_backup.json"
def limit_policer(dut, pps_limit, nn_target_namespace):
"""
Updates the COPP configuration in the SWSS container to respect a given rate limit.
Note:
The SWSS container must be restarted for the config change to take effect.
Args:
dut (SonicHost): The target device.
pps_limit (int): The rate limit for COPP to enforce on ALL trap groups.
"""
asichost = dut.asic_instance_from_namespace(nn_target_namespace)
swss_docker_name = asichost.get_docker_name("swss")
if "201811" in dut.os_version or "201911" in dut.os_version:
dut.command("docker cp {} {}".format(swss_docker_name + _APP_DB_COPP_CONFIG, _BASE_COPP_CONFIG))
config_format = "app_db"
else:
dut.command("cp {} {}".format(_DEFAULT_COPP_TEMPLATE, _BASE_COPP_TEMPLATE))
dut.command("cp {} {}".format(_CONFIG_DB_COPP_CONFIG, _BASE_COPP_CONFIG))
config_format = "config_db"
dut.script(
cmd="{} {} {} {} {}".format(_UPDATE_COPP_SCRIPT,
pps_limit,
_BASE_COPP_CONFIG,
_TEMP_COPP_CONFIG,
config_format)
)
if config_format == "app_db":
dut.command("docker cp {} {}".format(_TEMP_COPP_CONFIG, swss_docker_name + _APP_DB_COPP_CONFIG))
# As copp config is regenerated each time swss starts need to replace the template with
# config updated above. But before doing that need store the original template in a
# temporary file for restore after test.
dut.command("docker cp {} {}".format(swss_docker_name + _SWSS_COPP_TEMPLATE, _TEMP_COPP_TEMPLATE))
dut.command("docker cp {} {}".format(_TEMP_COPP_CONFIG, swss_docker_name + _SWSS_COPP_TEMPLATE))
else:
dut.command("cp {} {}".format(_TEMP_COPP_CONFIG, _DEFAULT_COPP_TEMPLATE))
def restore_policer(dut, nn_target_namespace):
"""
Reloads the default COPP configuration in the SWSS container.
Notes:
This method should only be used after limit_policer.
The SWSS container must be restarted for the config change to take effect.
"""
asichost = dut.asic_instance_from_namespace(nn_target_namespace)
swss_docker_name = asichost.get_docker_name("swss")
# Restore the copp template in swss
if "201811" in dut.os_version or "201911" in dut.os_version:
dut.command("docker cp {} {}".format(_BASE_COPP_CONFIG, swss_docker_name + _APP_DB_COPP_CONFIG))
dut.command("docker cp {} {}".format(_TEMP_COPP_TEMPLATE, swss_docker_name + _SWSS_COPP_TEMPLATE))
else:
dut.command("cp {} {}".format(_BASE_COPP_TEMPLATE, _DEFAULT_COPP_TEMPLATE))
def configure_ptf(ptf, test_params, is_backend_topology=False):
"""
Configures the PTF to run the NN agent on the specified port.
Args:
ptf (PTFHost): The target PTF.
test_params (_COPPTestParameters): test parameters set.
is_backend_topology (bool): Whether it's a backend topology testbed.
"""
ptf.script(cmd=_REMOVE_IP_SCRIPT)
if is_backend_topology:
ip_command = "ip address add %s/31 dev \"eth%s.%s\"" % (test_params.myip, test_params.nn_target_port, test_params.nn_target_vlanid)
else:
ip_command = "ip address add %s/31 dev eth%s" % (test_params.myip, test_params.nn_target_port)
logging.debug("ip_command is: %s" % ip_command)
ptf.command(ip_command)
facts = {
"nn_target_port": test_params.nn_target_port,
"nn_target_vlanid": test_params.nn_target_vlanid
}
ptf.host.options["variable_manager"].extra_vars.update(facts)
ptf.template(src=_PTF_NN_TEMPLATE, dest=_PTF_NN_DEST)
ptf.supervisorctl(name="ptf_nn_agent", state="restarted")
def restore_ptf(ptf):
"""
Restores the PTF and the NN agent to default settings.
Args:
ptf (PTFHost): The target PTF.
"""
ptf.script(cmd=_REMOVE_IP_SCRIPT)
facts = {
"nn_target_port": DEFAULT_NN_TARGET_PORT,
"nn_target_vlanid": None
}
ptf.host.options["variable_manager"].extra_vars.update(facts)
ptf.template(src=_PTF_NN_TEMPLATE, dest=_PTF_NN_DEST)
ptf.supervisorctl(name="ptf_nn_agent", state="restarted")
def configure_syncd(dut, nn_target_port, nn_target_interface, nn_target_namespace, nn_target_vlanid, creds):
"""
Configures syncd to run the NN agent on the specified port.
Note:
The DUT must be running an RPC syncd image in order for the
NN agent to be available.
Args:
dut (SonicHost): The target device.
nn_target_port (int): The port to run NN agent on.
nn_target_interface (str): The Interface remote NN agents listen to
nn_target_namespace (str): The namespace remote NN agents listens
nn_target_vlanid (str): The vlan id of the port to run NN agent on
creds (dict): Credential information according to the dut inventory
"""
facts = {
"nn_target_port": nn_target_port,
"nn_target_interface": nn_target_interface,
"nn_target_vlanid": nn_target_vlanid
}
dut.host.options["variable_manager"].extra_vars.update(facts)
asichost = dut.asic_instance_from_namespace(nn_target_namespace)
syncd_docker_name = asichost.get_docker_name("syncd")
_install_nano(dut, creds, syncd_docker_name)
dut.template(src=_SYNCD_NN_TEMPLATE, dest=_SYNCD_NN_DEST)
dut.command("docker cp {} {}:/etc/supervisor/conf.d/".format(_SYNCD_NN_DEST, syncd_docker_name))
dut.command("docker exec {} supervisorctl reread".format(syncd_docker_name))
dut.command("docker exec {} supervisorctl update".format(syncd_docker_name))
def restore_syncd(dut, nn_target_namespace):
asichost = dut.asic_instance_from_namespace(nn_target_namespace)
syncd_docker_name = asichost.get_docker_name("syncd")
dut.command("docker exec {} rm -rf /etc/supervisor/conf.d/{}".format(syncd_docker_name, _SYNCD_NN_FILE))
dut.command("docker exec {} supervisorctl reread".format(syncd_docker_name))
dut.command("docker exec {} supervisorctl update".format(syncd_docker_name))
def _install_nano(dut, creds, syncd_docker_name):
"""
Install nanomsg package to syncd container.
Args:
dut (SonicHost): The target device.
creds (dict): Credential information according to the dut inventory
"""
output = dut.command("docker exec {} bash -c '[ -d /usr/local/include/nanomsg ] || echo copp'".format(syncd_docker_name))
if output["stdout"] == "copp":
http_proxy = creds.get('proxy_env', {}).get('http_proxy', '')
https_proxy = creds.get('proxy_env', {}).get('https_proxy', '')
cmd = '''docker exec -e http_proxy={} -e https_proxy={} {} bash -c " \
rm -rf /var/lib/apt/lists/* \
&& apt-get update \
&& apt-get install -y python-pip build-essential libssl-dev libffi-dev python-dev python-setuptools wget cmake \
&& wget https://github.com/nanomsg/nanomsg/archive/1.0.0.tar.gz \
&& tar xzf 1.0.0.tar.gz && cd nanomsg-1.0.0 \
&& mkdir -p build && cmake . && make install && ldconfig && cd .. && rm -rf nanomsg-1.0.0 \
&& rm -f 1.0.0.tar.gz && pip2 install cffi==1.7.0 && pip2 install --upgrade cffi==1.7.0 && pip2 install nnpy \
&& mkdir -p /opt && cd /opt && wget https://raw.githubusercontent.com/p4lang/ptf/master/ptf_nn/ptf_nn_agent.py \
&& mkdir ptf && cd ptf && wget https://raw.githubusercontent.com/p4lang/ptf/master/src/ptf/afpacket.py && touch __init__.py \
" '''.format(http_proxy, https_proxy, syncd_docker_name)
dut.command(cmd)
def _map_port_number_to_interface(dut, nn_target_port):
"""
Retrieves the correct interface for a given port number.
"""
interfaces = dut.command("portstat")["stdout_lines"][2:]
return interfaces[nn_target_port].split()[0]
def _get_http_and_https_proxy_ip(creds):
"""
Get the http and https proxy ip.
Args:
creds (dict): Credential information according to the dut inventory
"""
return (re.findall(r'[0-9]+(?:\.[0-9]+){3}', creds.get('proxy_env', {}).get('http_proxy', ''))[0],
re.findall(r'[0-9]+(?:\.[0-9]+){3}', creds.get('proxy_env', {}).get('https_proxy', ''))[0])
def configure_always_enabled_for_trap(dut, trap_id, always_enabled):
"""
Configure the always_enabled to true or false for the specified trap id.
Args:
dut (SonicHost): The target device
trap_id (str): The trap id (e.g. bgp)
always_enabled (str): true or false
"""
copp_trap_config_json = "/tmp/copp_{}.json".format(trap_id)
cmd_copp_trap_always_enabled_config = """
cat << EOF > %s
{
"COPP_TRAP": {
"%s": {
"always_enabled": "%s"
}
}
}
EOF
""" % (copp_trap_config_json, trap_id, always_enabled)
dut.shell(cmd_copp_trap_always_enabled_config)
dut.command("sudo config load {} -y".format(copp_trap_config_json))
def get_config_db_json_obj(dut):
"""
Get config_db content from dut
Args:
dut (SonicHost): The target device
"""
config_db_json = dut.shell("sudo sonic-cfggen -d --print-data")["stdout"]
return json.loads(config_db_json)
def remove_feature_entry(dut, feature_name):
"""
Remove feature entry from dut
Args:
dut (SonicHost): The target device
feature_name (str): feature name (e.g bgp)
"""
dut.command('redis-cli -n 4 del "FEATURE|{}"'.format(feature_name))
def disable_feature_entry(dut, feature_name):
"""
Disable feature entry on dut
Args:
dut (SonicHost): The target device
feature_name (str): feature name (e.g bgp)
"""
dut.command(' sudo config feature state {} disabled'.format(feature_name))
def enable_feature_entry(dut, feature_name):
"""
Enabled feature entry dut
Args:
dut (SonicHost): The target device
feature_name (str): feature name (e.g bgp)
"""
dut.command(' sudo config feature state {} enabled'.format(feature_name))
def backup_config_db(dut):
"""
Backup config db to /home/admin/
Args:
dut (SonicHost): The target device
"""
dut.command("sudo cp {} {}".format(_CONFIG_DB, _TEMP_CONFIG_DB))
def restore_config_db(dut):
"""
Restore config db
Args:
dut (SonicHost): The target device
"""
dut.command("sudo cp {} {}".format(_TEMP_CONFIG_DB, _CONFIG_DB))
dut.command("sudo rm -f {}".format(_TEMP_CONFIG_DB))
config_reload(dut)
def uninstall_trap(dut, feature_name, trap_id):
"""
Uninstall trap by disabling feature and set always_enable to false
Args:
dut (SonicHost): The target device
feature_name (str): feature name corresponding to the trap
trap_id (str): trap id
"""
disable_feature_entry(dut, feature_name)
configure_always_enabled_for_trap(dut, trap_id, "false")
def verify_always_enable_value(dut, trap_id, always_enable_value):
"""
Verify the value of always_enable for the specified trap is expected one
Args:
dut (SonicHost): The target device
trap_id (str): trap id
always_enable_value (str): true or false
"""
config_db_json = get_config_db_json_obj(dut)
assert config_db_json["COPP_TRAP"][trap_id]["always_enabled"] == always_enable_value, \
"The value of always_enable not match. The expected value is:{}, the actual value is :{}".format(
always_enable_value, config_db_json["COPP_TRAP"][trap_id]["always_enabled"])
def install_trap(dut, feature_name):
"""
Install trap by enabling feature status
Args:
dut (SonicHost): The target device
feature_name (str): feature name
"""
enable_feature_entry(dut, feature_name)