From f4764186c671616c32050cac489c58329ec6c4e5 Mon Sep 17 00:00:00 2001 From: Tom Barbette Date: Wed, 8 May 2024 10:14:11 +0200 Subject: [PATCH 1/3] Jinja support --- npf/section.py | 18 ++++++++++-------- npf/test.py | 8 ++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/npf/section.py b/npf/section.py index 9dbef56b..4a39affa 100644 --- a/npf/section.py +++ b/npf/section.py @@ -41,12 +41,13 @@ def suggest(self, w, max = None): class SectionFactory: varPattern = "([a-zA-Z0-9_:-]+)[=](" + Variable.VALUE_REGEX + ")?" namePattern = re.compile( - "^(?P" + Variable.TAGS_REGEX + "[:])?(?Pinfo|config|variables|exit|pypost|pyexit|late_variables|include (?P[a-zA-Z0-9_./-]+)(?P([ \t]+" + - varPattern + ")+)?|(init-)?file(:?[@](?P[a-zA-Z0-9]+))? (?P[a-zA-Z0-9_.${}-]+)(:? (?Pnoparse))?|require|" - "import(:?[@](?P[a-zA-Z0-9]+)(:?[-](?P[*0-9]+))?)?[ \t]+(?P" + Variable.VALUE_REGEX + ")(?P([ \t]+" + - varPattern + ")+)?|" + - "sendfile(:?[@](?P[a-zA-Z0-9]+))?[ \t]+(?P.*)|" + - "(:?script|init|exit)(:?[@](?P[a-zA-Z0-9]+)(:?[-](?P[*0-9]+))?)?(?P([ \t]+" + varPattern + ")*))$") + "^(?P" + Variable.TAGS_REGEX + "[:])?(?Pinfo|config|variables|exit|pypost|pyexit|late_variables|" + "include (?P[a-zA-Z0-9_./-]+)(?P([ \t]+" + varPattern + ")+)?|" + "(init-)?file(:?[@](?P[a-zA-Z0-9]+))? (?P[a-zA-Z0-9_.${}-]+)(:? (?Pnoparse))?(:? (?Pjinja))?|" + "require|" + "import(:?[@](?P[a-zA-Z0-9]+)(:?[-](?P[*0-9]+))?)?[ \t]+(?P" + Variable.VALUE_REGEX + ")(?P([ \t]+" + varPattern + ")+)?|" + "sendfile(:?[@](?P[a-zA-Z0-9]+))?[ \t]+(?P.*)|" + + "(:?script|init|exit)(:?[@](?P[a-zA-Z0-9]+)(:?[-](?P[*0-9]+))?)?(?P([ \t]+" + varPattern + ")*))$") @staticmethod def build(test, data): @@ -100,7 +101,7 @@ def build(test, data): if sectionName.startswith('file'): s = SectionFile(matcher.group('fileName').strip(), role=matcher.group('fileRole'), - noparse=matcher.group('fileNoparse')) + noparse=matcher.group('fileNoparse'), jinja=matcher.group('fileJinja')) return s if sectionName.startswith('init-file'): s = SectionInitFile(matcher.group('fileName').strip(), role=matcher.group('fileRole'), @@ -257,12 +258,13 @@ def finish(self, test): class SectionFile(Section): - def __init__(self, filename, role=None, noparse=False): + def __init__(self, filename, role=None, noparse=False, jinja=False): super().__init__('file') self.content = '' self.filename = filename self._role = role self.noparse = noparse + self.jinja = jinja def get_role(self): return self._role diff --git a/npf/test.py b/npf/test.py index 2b49e86d..cec69886 100755 --- a/npf/test.py +++ b/npf/test.py @@ -405,6 +405,14 @@ def build_file_list(self, v, self_role=None, files=None) -> List[Tuple[str, str, if not s.noparse: s.filename = SectionVariable.replace_variables(v, s.filename, role, default_role_map = self.config.get_dict("default_role_map")) p = SectionVariable.replace_variables(v, s.content, role,default_role_map = self.config.get_dict("default_role_map")) + if s.jinja: + from jinja2 import Environment, PackageLoader, select_autoescape + env = Environment( + loader=PackageLoader("npf"), + autoescape=select_autoescape() + ) + template = env.from_string(p) + p = template.render(**v) else: p = s.content create_list.append((s.filename, p, role)) From f5317b1aff6d4716890ee0ce872491f38a0fb2c7 Mon Sep 17 00:00:00 2001 From: Tom Barbette Date: Fri, 10 May 2024 07:45:43 +0200 Subject: [PATCH 2/3] Add requirement --- npf/test.py | 13 +++++-------- setup.py | 3 ++- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/npf/test.py b/npf/test.py index cec69886..82581b62 100755 --- a/npf/test.py +++ b/npf/test.py @@ -405,16 +405,13 @@ def build_file_list(self, v, self_role=None, files=None) -> List[Tuple[str, str, if not s.noparse: s.filename = SectionVariable.replace_variables(v, s.filename, role, default_role_map = self.config.get_dict("default_role_map")) p = SectionVariable.replace_variables(v, s.content, role,default_role_map = self.config.get_dict("default_role_map")) - if s.jinja: - from jinja2 import Environment, PackageLoader, select_autoescape - env = Environment( - loader=PackageLoader("npf"), - autoescape=select_autoescape() - ) - template = env.from_string(p) - p = template.render(**v) else: p = s.content + if s.jinja: + from jinja2 import Environment, BaseLoader + env = Environment(loader=BaseLoader) + template = env.from_string(p) + p = template.render(**v) create_list.append((s.filename, p, role)) return create_list diff --git a/setup.py b/setup.py index 84cd69c5..3ba701bc 100644 --- a/setup.py +++ b/setup.py @@ -28,7 +28,8 @@ 'pygtrie', 'packaging', 'importlib_metadata', - 'npf-web-extension >= 0.6.4' + 'npf-web-extension >= 0.6.4', + 'jinja2' ] setuptools.setup( From 74450e153320764771e934e00d373832a6f113d4 Mon Sep 17 00:00:00 2001 From: Tom Barbette Date: Fri, 10 May 2024 07:54:36 +0200 Subject: [PATCH 3/3] Generator: Add support for GEN_FNT and change PAUSE to GEN_PAUSE GEN_FNT enables to insert some element in the generator pipeline --- modules/fastclick-play-single-mt.npf | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/modules/fastclick-play-single-mt.npf b/modules/fastclick-play-single-mt.npf index b5f74761..d520524d 100644 --- a/modules/fastclick-play-single-mt.npf +++ b/modules/fastclick-play-single-mt.npf @@ -38,7 +38,7 @@ GEN_BURST=32 PROMISC=false promisc:PROMISC=true IGNORE?=0 -PAUSE=none +GEN_PAUSE=none GEN_DESC=0 END_AFTER=0 GEN_PROGRESSIVE=0 @@ -68,6 +68,7 @@ GEN_NGEN=EXPAND( $((1 if $GEN_PIPELINE else $GEN_THREADS)) ) GEN_IPV6?=0 GEN_NANO?=0 GEN_PREFIX?= +GEN_FNT?= UNQUEUE= udpgen:UNQUEUE=Unqueue() @@ -163,6 +164,7 @@ Idle->[1]output; elementclass Numberise { $magic | input-> Strip(14) $GEN_NUMBER + $GEN_FNT $(( "" if $GEN_IPV6 else " -> ResetIPChecksum() " )) -> Unstrip(14) -> output } @@ -195,7 +197,7 @@ $(( " StaticThreadSched(fdIN0 0/0, unqueue0 0/0);" if $GEN_PIPELINE else "" )) $(( " StaticThreadSched(tdIN 0/%d);" % ($GEN_THREADS + 1) if $GEN_TX_PIPELINE else "" )) -receiveIN :: FromDPDKDevice($port, VERBOSE $rxverbose, MAC $INsrcmac, PROMISC $PROMISC, PAUSE $PAUSE, NDESC $GEN_DESC, MAXTHREADS $GEN_THREADS, NUMA false, ACTIVE $GEN_RX) +receiveIN :: FromDPDKDevice($port, VERBOSE $rxverbose, MAC $INsrcmac, PROMISC $PROMISC, PAUSE $GEN_PAUSE, NDESC $GEN_DESC, MAXTHREADS $GEN_THREADS, NUMA false, ACTIVE $GEN_RX) elementclass Receiver { $mac, $dir | input[0]