Skip to content

Commit

Permalink
pppoe-server: T6234: PPPoE-server pado-delay refactoring
Browse files Browse the repository at this point in the history
(cherry picked from commit 107ee09)
  • Loading branch information
natali-rs1985 authored and mergify[bot] committed May 1, 2024
1 parent f69604d commit 0891817
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 3 deletions.
2 changes: 1 addition & 1 deletion data/templates/accel-ppp/pppoe.config.j2
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ service-name={{ service_name | join(',') }}
{% set delay_without_sessions = pado_delay.delays_without_sessions[0] | default('0') %}
{% set pado_delay_param = namespace(value=delay_without_sessions) %}
{% for delay, sessions in pado_delay.delays_with_sessions | sort(attribute='1') %}
{% if not loop.last %}
{% if not delay == 'disable' %}
{% set pado_delay_param.value = pado_delay_param.value + ',' + delay + ':' + sessions | string %}
{% else %}
{% set pado_delay_param.value = pado_delay_param.value + ',-1:' + sessions | string %}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- include start from include/version/pppoe-server-version.xml.i -->
<syntaxVersion component='pppoe-server' version='9'></syntaxVersion>
<syntaxVersion component='pppoe-server' version='10'></syntaxVersion>
<!-- include end -->
8 changes: 8 additions & 0 deletions interface-definitions/service_pppoe-server.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,20 @@
<tagNode name="pado-delay">
<properties>
<help>PADO delays</help>
<valueHelp>
<format>disable</format>
<description>Disable new connections</description>
</valueHelp>
<completionHelp>
<list>disable</list>
</completionHelp>
<valueHelp>
<format>u32:1-999999</format>
<description>Number in ms</description>
</valueHelp>
<constraint>
<validator name="numeric" argument="--range 1-999999"/>
<regex>disable</regex>
</constraint>
<constraintErrorMessage>Invalid PADO delay</constraintErrorMessage>
</properties>
Expand Down
9 changes: 8 additions & 1 deletion smoketest/scripts/cli/test_service_pppoe-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,14 @@ def test_pppoe_server_pado_delay(self):
conf = ConfigParser(allow_no_value=True, delimiters='=')
conf.read(self._config_file)

self.assertEqual(conf['pppoe']['pado-delay'], '10,20:200,-1:300')
self.assertEqual(conf['pppoe']['pado-delay'], '10,20:200,30:300')

self.set(['pado-delay', 'disable', 'sessions', '400'])
self.cli_commit()

conf = ConfigParser(allow_no_value=True, delimiters='=')
conf.read(self._config_file)
self.assertEqual(conf['pppoe']['pado-delay'], '10,20:200,30:300,-1:400')


if __name__ == '__main__':
Expand Down
17 changes: 17 additions & 0 deletions src/conf_mode/service_pppoe-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,29 @@ def verify_pado_delay(pppoe):
pado_delay = pppoe['pado_delay']

delays_without_sessions = pado_delay['delays_without_sessions']
if 'disable' in delays_without_sessions:
raise ConfigError(
'Number of sessions must be specified for "pado-delay disable"'
)

if len(delays_without_sessions) > 1:
raise ConfigError(
f'Cannot add more then ONE pado-delay without sessions, '
f'but {len(delays_without_sessions)} were set'
)

if 'disable' in [delay[0] for delay in pado_delay['delays_with_sessions']]:
# need to sort delays by sessions to verify if there is no delay
# for sessions after disabling
sorted_pado_delay = sorted(pado_delay['delays_with_sessions'], key=lambda k_v: k_v[1])
last_delay = sorted_pado_delay[-1]

if last_delay[0] != 'disable':
raise ConfigError(
f'Cannot add pado-delay after disabled sessions, but '
f'"pado-delay {last_delay[0]} sessions {last_delay[1]}" was set'
)

def verify(pppoe):
if not pppoe:
return None
Expand Down
56 changes: 56 additions & 0 deletions src/migration-scripts/pppoe-server/9-to-10
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env python3
#
# Copyright (C) 2024 VyOS maintainers and contributors
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 or later as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

# Migration of pado-delay options

from sys import argv
from sys import exit
from vyos.configtree import ConfigTree

if len(argv) < 2:
print("Must specify file name!")
exit(1)

file_name = argv[1]

with open(file_name, 'r') as f:
config_file = f.read()

config = ConfigTree(config_file)
base = ['service', 'pppoe-server', 'pado-delay']
if not config.exists(base):
exit(0)

pado_delay = {}
for delay in config.list_nodes(base):
sessions = config.return_value(base + [delay, 'sessions'])
pado_delay[delay] = sessions

# need to define delay for latest sessions
sorted_delays = dict(sorted(pado_delay.items(), key=lambda k_v: int(k_v[1])))
last_delay = list(sorted_delays)[-1]

# Rename last delay -> disable
tmp = base + [last_delay]
if config.exists(tmp):
config.rename(tmp, 'disable')

try:
with open(file_name, 'w') as f:
f.write(config.to_string())
except OSError as e:
print("Failed to save the modified config: {}".format(e))
exit(1)

0 comments on commit 0891817

Please sign in to comment.