Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Utilities Changes for DHCP DoS Mitigation Feature #3301

Open
wants to merge 107 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
107 commits
Select commit Hold shift + click to select a range
d71e6fc
Add dhcp-mitigation-rate add + del + show commands
asraza07 Apr 30, 2024
002ea71
Added support for DHCP rate limit in DB Migrator with default rate li…
asraza07 May 1, 2024
347850f
Remove portchannel DHCP rate limit from DB Migrator
asraza07 May 1, 2024
47e4d89
Remove portchannel behaviour from DHCP rate limit CLI commands
asraza07 May 1, 2024
7c6b911
Fix for pre-commit failure
asraza07 May 3, 2024
1ffc299
Fixed pre-commit failures
asraza07 May 3, 2024
95765f9
Fix for pre-commit check
asraza07 May 3, 2024
46aacbb
Fix for trailing whitespace
asraza07 May 3, 2024
81b73ba
Add test cases for DHCP mitigation rate feature
asraza07 May 3, 2024
790d8f9
Fix for pre-commit
asraza07 May 6, 2024
fe99f6f
Fix for line length
asraza07 May 6, 2024
6db5369
Fix for blank line
asraza07 May 6, 2024
7235c25
Fix for space in blank line
asraza07 May 6, 2024
271a0ed
Fix for test case failures
asraza07 May 6, 2024
6193bda
Fix for test case show command and teardown class method
asraza07 May 6, 2024
7756dde
Fix for test case show command db object
asraza07 May 6, 2024
e2ae103
Fix for test case show command
asraza07 May 6, 2024
7c16a89
Fix logical error in show command
asraza07 May 6, 2024
fe661f4
Fix for test case show command db object
asraza07 May 6, 2024
0f36f95
Fix for test case failure
asraza07 May 6, 2024
594476b
Remove failing test case
asraza07 May 7, 2024
5a6b56a
Add support for scenarios where dhcp_rate_limit attribute is not present
asraza07 May 7, 2024
e4cadde
Fix for faulty config_db entry
ridahanif96 May 7, 2024
3adaf98
Fix for db injection
ridahanif96 May 7, 2024
4ed287a
Cherry-pick commit "Add support for scenarios where dhcp_rate_limit a…
asraza07 May 7, 2024
e7450ec
Fix for config_db write
asraza07 May 7, 2024
9c2f81e
Fix for typo
asraza07 May 7, 2024
b0aab53
Merge branch 'sonic-net:master' into dhcp_dos_mitigation_cli
asraza07 May 9, 2024
8afda8e
Add test case
asraza07 May 9, 2024
6a43338
Empty line fix
asraza07 May 9, 2024
a71524b
Edit setup_class
asraza07 May 9, 2024
b6c336d
Empty line
asraza07 May 9, 2024
0811009
Edit setup teardown
asraza07 May 9, 2024
87948cb
Fix for pre-commit
asraza07 May 9, 2024
d748f55
Remove test case
asraza07 May 9, 2024
86808ae
Fix for pre-commit
asraza07 May 9, 2024
7ac1926
Added test case
asraza07 May 9, 2024
6cd94ca
Fixing dhcp show error
ridahanif96 May 9, 2024
2b9868c
Fixing dhcp show error with db obj
ridahanif96 May 9, 2024
4cc58a3
Fixed for Show DHCP Output
ridahanif96 May 10, 2024
614cc5c
Updated dhcp show
ridahanif96 May 13, 2024
6e136a0
Modified shpw command
ridahanif96 May 14, 2024
5ff01ea
DHCP Show Command Modify
ridahanif96 May 15, 2024
8dfb309
Removed show command to test coverage
ridahanif96 May 16, 2024
82f498d
Added more testcases
ridahanif96 May 20, 2024
f8c2ab0
Fix for patch
ridahanif96 May 20, 2024
c85d89c
Fixing errors
ridahanif96 May 20, 2024
81a86d2
Fix for pytest
ridahanif96 May 20, 2024
f0d2fce
Added show
ridahanif96 May 20, 2024
1047c6f
FIxed Show command
ridahanif96 May 20, 2024
d9767b2
Added show commad
ridahanif96 May 24, 2024
42e43b1
Fix pre-commit errors
asraza07 May 31, 2024
86402db
Fix for show command
asraza07 May 31, 2024
ee95cfe
Fix for db object variable
asraza07 May 31, 2024
35b98ab
Merge branch 'sonic-net:master' into dhcp_dos_mitigation_cli
asraza07 Jun 20, 2024
b06482e
Modifed show command for fixes
ridahanif96 Jun 20, 2024
41b51e2
test_show_dhcp_mitigation_rate output
ridahanif96 Jun 20, 2024
a10976c
Fixes
ridahanif96 Jun 20, 2024
62a6782
Fix for show command test case
asraza07 Jun 20, 2024
31a1248
Fix for show command test
asraza07 Jun 20, 2024
de19535
Show command fix
ridahanif96 Jun 21, 2024
1d54e22
Fix
ridahanif96 Jun 21, 2024
98b7ddd
COde fix
ridahanif96 Jun 21, 2024
271dd49
Fixing Show case
ridahanif96 Jun 21, 2024
7adbbf8
Show test
ridahanif96 Jun 21, 2024
d3421b7
Fix for show test case output
asraza07 Jul 10, 2024
6866f2f
Fix for show test
asraza07 Jul 10, 2024
4bab065
Fix for show command context
asraza07 Jul 11, 2024
59d020c
Fix for show command test output
asraza07 Jul 11, 2024
8532980
Added test cases for DHCP rate limit and interface alias mode
KanzaLatif Jul 15, 2024
a3ebe65
Fixing the Precommit
KanzaLatif Jul 15, 2024
d85648c
Fixing the indents
KanzaLatif Jul 15, 2024
21cab1a
Fixing the indents
KanzaLatif Jul 15, 2024
7bfe23a
Fixing rate
ridahanif96 Jul 15, 2024
b0fe8f2
printing outputs to see if the test cases are working
KanzaLatif Jul 18, 2024
f709832
Merge branch 'dhcp_dos_mitigation_cli' of https://github.com/asraza07…
KanzaLatif Jul 18, 2024
7ad873a
checking the testcases
KanzaLatif Jul 18, 2024
85f10e4
checking the testcases
KanzaLatif Jul 18, 2024
4d89516
Fix for show command in alias mode
asraza07 Jul 18, 2024
32f932d
correcting interfaces test
KanzaLatif Jul 18, 2024
3a6c4a6
Merge branch 'dhcp_dos_mitigation_cli' of https://github.com/asraza07…
KanzaLatif Jul 18, 2024
9a10c79
printing outputs to see the error
KanzaLatif Jul 18, 2024
f3cd62d
correcting the output discrepancies
KanzaLatif Jul 18, 2024
b75d973
Fix for test cases error output
asraza07 Jul 19, 2024
d577a8c
Fix for formatted string
asraza07 Jul 19, 2024
8c5fb8f
Add testcase for interface with no existing rate limit
asraza07 Jul 19, 2024
169a75e
Fix for output table format
asraza07 Jul 19, 2024
5113a36
Add testcase for value error in dhcp rate config
asraza07 Jul 19, 2024
5522530
Fix for object variable
asraza07 Jul 19, 2024
66260c7
Add unit test patch to invoke value error for test case
asraza07 Jul 19, 2024
baed48a
Add patch to invoke value error in test case
asraza07 Jul 19, 2024
7f01ed2
Raise value error via pytest
asraza07 Jul 19, 2024
6637b21
Pre-commit test
KanzaLatif Jul 22, 2024
3d581d7
Pre-commit test
KanzaLatif Jul 22, 2024
c8f6a9b
Pre-commit test
KanzaLatif Jul 22, 2024
a156ebc
Fix for interfaces output
asraza07 Jul 22, 2024
624caf3
Fix for assertion error
KanzaLatif Jul 22, 2024
0c6c857
Fix for assertion error
KanzaLatif Jul 22, 2024
49532cd
Merge branch 'dhcp_dos_mitigation_cli' of https://github.com/asraza07…
KanzaLatif Jul 22, 2024
e81892c
Fix for pre-commit
KanzaLatif Jul 22, 2024
345ccec
Fix for pre-commit
KanzaLatif Jul 22, 2024
628a770
Fix for config_db instance to correct value error behavior
asraza07 Jul 22, 2024
1c06171
Fix for pre-commit
asraza07 Jul 22, 2024
7d7562c
Testcase db object corrections
asraza07 Jul 22, 2024
277dc74
Remove whitespaces
asraza07 Jul 22, 2024
0148cda
testing variable 266 intfcs_test.py
muhammadalihussnain Sep 9, 2024
1a69d16
removed variable, added mistaken
muhammadalihussnain Sep 9, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5018,6 +5018,105 @@ def loopback_action(ctx, interface_name, action):
table_name = get_interface_table_name(interface_name)
config_db.mod_entry(table_name, interface_name, {"loopback_action": action})

#
# 'dhcp-mitigation-rate' subgroup ('config interface dhcp-mitigation-rate ...')
#


@interface.group(cls=clicommon.AbbreviationGroup, name='dhcp-mitigation-rate')
@click.pass_context
def dhcp_mitigation_rate(ctx):
"""Set interface DHCP rate limit attribute"""
pass

#
# 'add' subcommand
#


@dhcp_mitigation_rate.command(name='add')
@click.argument('interface_name', metavar='<interface_name>', required=True)
@click.argument('packet_rate', metavar='<DHCP packet rate>', required=True, type=int)
@click.pass_context
@clicommon.pass_db
def add_dhcp_mitigation_rate(db, ctx, interface_name, packet_rate):
"""Add a new DHCP mitigation rate on an interface"""
# Get the config_db connector
config_db = ValidatedConfigDBConnector(db.cfgdb)

if clicommon.get_interface_naming_mode() == "alias":
interface_name = interface_alias_to_name(config_db, interface_name)

if clicommon.is_valid_port(config_db, interface_name):
pass
elif clicommon.is_valid_portchannel(config_db, interface_name):
ctx.fail("{} is a PortChannel!".format(interface_name))
else:
ctx.fail("{} does not exist".format(interface_name))

if packet_rate <= 0:
ctx.fail("DHCP rate limit is not valid. \nIt must be greater than 0.")

port_data = config_db.get_entry('PORT', interface_name)

if 'dhcp_rate_limit' in port_data:
rate = port_data["dhcp_rate_limit"]
else:
rate = '0'

if rate != '0':
ctx.fail("{} has DHCP rate limit configured. \nRemove it to add new DHCP rate limit.".format(interface_name))

try:
config_db.mod_entry('PORT', interface_name, {"dhcp_rate_limit": "{}".format(str(packet_rate))})
except ValueError as e:
ctx.fail("{} invalid or does not exist. Error: {}".format(interface_name, e))

#
# 'del' subcommand
#


@dhcp_mitigation_rate.command(name='del')
@click.argument('interface_name', metavar='<interface_name>', required=True)
@click.argument('packet_rate', metavar='<DHCP packet rate>', required=True, type=int)
@click.pass_context
@clicommon.pass_db
def del_dhcp_mitigation_rate(db, ctx, interface_name, packet_rate):
"""Delete an existing DHCP mitigation rate on an interface"""
# Get the config_db connector
config_db = ValidatedConfigDBConnector(db.cfgdb)

if clicommon.get_interface_naming_mode() == "alias":
interface_name = interface_alias_to_name(config_db, interface_name)

if clicommon.is_valid_port(config_db, interface_name):
pass
elif clicommon.is_valid_portchannel(config_db, interface_name):
ctx.fail("{} is a PortChannel!".format(interface_name))
else:
ctx.fail("{} does not exist".format(interface_name))

if packet_rate <= 0:
ctx.fail("DHCP rate limit is not valid. \nIt must be greater than 0.")

port_data = config_db.get_entry('PORT', interface_name)

if 'dhcp_rate_limit' in port_data:
rate = port_data["dhcp_rate_limit"]
else:
rate = '0'

if rate != str(packet_rate):
ctx.fail("{} DHCP rate limit does not exist on {}.".format(packet_rate, interface_name))

port_data["dhcp_rate_limit"] = "0"

try:
config_db.mod_entry('PORT', interface_name, {"dhcp_rate_limit": "0"})
except ValueError as e:
ctx.fail("{} invalid or does not exist. Error: {}".format(interface_name, e))

#
# buffer commands and utilities
#
Expand Down
13 changes: 13 additions & 0 deletions scripts/db_migrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,18 @@ def migrate_config_db_port_table_for_auto_neg(self):
elif value['autoneg'] == '0':
self.configDB.set(self.configDB.CONFIG_DB, '{}|{}'.format(table_name, key), 'autoneg', 'off')

def migrate_config_db_port_table_for_dhcp_rate_limit(self):
port_table_name = 'PORT'
port_table = self.configDB.get_table(port_table_name)

for p_key, p_value in port_table.items():
if 'dhcp_rate_limit' in p_value:
self.configDB.set(self.configDB.CONFIG_DB, '{}|{}'.format(port_table_name, p_key),
'dhcp_rate_limit', p_value['dhcp_rate_limit'])
else:
self.configDB.set(self.configDB.CONFIG_DB, '{}|{}'.format(port_table_name, p_key),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @yaqiangz , as per our HLD document, we plan to depreciate the default COPP DHCP rate limit and replace it with our new rate-limiting mechanism since TC holds and advantage over COPP in the event of a DoS attack (it can rate limit at an interface level). We kept the default value 300 packets/sec to ensure backward compatibility with the depreciated COPP limit. We have added a sonic-buildimage PR to do this which is linked to our main HLD PR.

'dhcp_rate_limit', '300')

def migrate_qos_db_fieldval_reference_remove(self, table_list, db, db_num, db_delimeter):
for pair in table_list:
table_name, fields_list = pair
Expand Down Expand Up @@ -1029,6 +1041,7 @@ def version_3_0_0(self):
"""
log.log_info('Handling version_3_0_0')
self.migrate_config_db_port_table_for_auto_neg()
self.migrate_config_db_port_table_for_dhcp_rate_limit()
self.set_version('version_3_0_1')
return 'version_3_0_1'

Expand Down
Loading
Loading