Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
9 changes: 5 additions & 4 deletions config/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,16 @@
asic_type = None


SAMPLE_RATE_MIN = 256
SAMPLE_RATE_MAX = 8388608
SAMPLE_RATE_MIN = 2
SAMPLE_RATE_MAX = 0xFFFFFFFF # uint32 max
TRUNCATE_SIZE_MIN = 64
TRUNCATE_SIZE_MAX = 9216


def validate_sample_rate(ctx, param, value):
if value != 0 and (value < SAMPLE_RATE_MIN or value > SAMPLE_RATE_MAX):
raise click.BadParameter(f"must be 0 or in range {SAMPLE_RATE_MIN}..{SAMPLE_RATE_MAX}")
raise click.BadParameter(
f"must be 0 or in range {SAMPLE_RATE_MIN}..{SAMPLE_RATE_MAX} (uint32 max)")
return value


Expand Down Expand Up @@ -3216,7 +3217,7 @@ def erspan(ctx):
@click.argument('direction', metavar='[direction]', required=False)
@click.option('--policer')
@click.option('--sample_rate', type=int, default=0, callback=validate_sample_rate,
help="Sampling rate (1-in-N), 256..8388608. 0 disables sampling")
help="Sampling rate (1-in-N), 2..4294967295. 0 disables sampling")
@click.option('--truncate_size', type=int, default=0, callback=validate_truncate_size,
help="Truncation size in bytes, 64..9216. 0 disables truncation")
def erspan_add(session_name, src_ip, dst_ip, dscp, ttl, gre_type, queue, policer, src_port, direction,
Expand Down
4 changes: 2 additions & 2 deletions doc/Command-Reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -9327,8 +9327,8 @@ While adding a new ERSPAN session, users need to configure the following fields
7) optional - Policer which will be used to control the rate at which frames are mirrored.
8) optional - List of source ports which can have both Ethernet and LAG ports.
9) optional - Direction - Mirror session direction when configured along with Source port. (Supported rx/tx/both. default direction is both)
10) optional - Sample rate for sampled mirroring. N means mirror 1-in-N packets. When not specified, full mirroring is used. Valid range: 256..8388608.
11) optional - Truncate size in bytes for mirrored packets. When not specified, no truncation is applied. Valid range: 64..9216.
10) optional - Sample rate for sampled mirroring. N means mirror 1-in-N packets. When not specified (or set to 0), full mirroring is used. Valid values: 0 or 2..4294967295 (uint32 max).
11) optional - Truncate size in bytes for mirrored packets. When not specified (or set to 0), no truncation is applied. Valid values: 0 or 64..9216.

- Usage:
```
Expand Down
34 changes: 17 additions & 17 deletions tests/config_mirror_session_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ def test_mirror_session_erspan_add_with_invalid_sample_rate():
["test_session", "1.1.1.1", "2.2.2.2", "8", "64",
"--sample_rate", "-1"])
assert result.exit_code != 0
assert 'must be 0 or in range 256..8388608' in result.output
assert 'must be 0 or in range 2..4294967295' in result.output

# Verify invalid truncate_size (negative)
result = runner.invoke(
Expand All @@ -824,45 +824,45 @@ def test_mirror_session_erspan_add_with_invalid_sample_rate():
def test_mirror_session_erspan_add_sample_rate_boundary():
runner = CliRunner()

# sample_rate=1 (in the 1-255 hole, should fail)
# sample_rate=1 (below minimum of 2, should fail)
result = runner.invoke(
config.config.commands["mirror_session"].commands["erspan"].commands["add"],
["test_session", "1.1.1.1", "2.2.2.2", "8", "64",
"--sample_rate", "1"])
assert result.exit_code != 0
assert 'must be 0 or in range 256..8388608' in result.output
assert 'must be 0 or in range 2..4294967295' in result.output

# sample_rate=255 (upper boundary of hole, should fail)
result = runner.invoke(
config.config.commands["mirror_session"].commands["erspan"].commands["add"],
["test_session", "1.1.1.1", "2.2.2.2", "8", "64",
"--sample_rate", "255"])
assert result.exit_code != 0
assert 'must be 0 or in range 256..8388608' in result.output
# sample_rate=2 (minimum valid, should pass)
with mock.patch('config.main.add_erspan') as _:
result = runner.invoke(
config.config.commands["mirror_session"].commands["erspan"].commands["add"],
["test_session", "1.1.1.1", "2.2.2.2", "8", "64",
"--sample_rate", "2"])
assert result.exit_code == 0

# sample_rate=256 (minimum valid, should pass)
# sample_rate=100 (in the valid range, should pass)
with mock.patch('config.main.add_erspan') as _:
result = runner.invoke(
config.config.commands["mirror_session"].commands["erspan"].commands["add"],
["test_session", "1.1.1.1", "2.2.2.2", "8", "64",
"--sample_rate", "256"])
"--sample_rate", "100"])
assert result.exit_code == 0

# sample_rate=8388608 (maximum valid, should pass)
# sample_rate=4294967295 (maximum valid, should pass)
with mock.patch('config.main.add_erspan') as _:
result = runner.invoke(
config.config.commands["mirror_session"].commands["erspan"].commands["add"],
["test_session", "1.1.1.1", "2.2.2.2", "8", "64",
"--sample_rate", "8388608"])
"--sample_rate", "4294967295"])
assert result.exit_code == 0

# sample_rate=8388609 (above maximum, should fail)
# sample_rate=4294967296 (above maximum, should fail)
result = runner.invoke(
config.config.commands["mirror_session"].commands["erspan"].commands["add"],
["test_session", "1.1.1.1", "2.2.2.2", "8", "64",
"--sample_rate", "8388609"])
"--sample_rate", "4294967296"])
assert result.exit_code != 0
assert 'must be 0 or in range 256..8388608' in result.output
assert 'must be 0 or in range 2..4294967295' in result.output


def test_mirror_session_erspan_add_truncate_size_boundary():
Expand Down
Loading