Replies: 9 comments
-
With meta_key = x.get(('NFTA_RULE_EXPRESSIONS', 'NFTA_EXPR_DATA', 'NFTA_META_KEY'))` I hope this helps. If not, don't hesitate to ask. |
Beta Was this translation helpful? Give feedback.
-
Thank you for your help, but I can't.
I need find all rules handle with This record When I do
Next step I try do
|
Beta Was this translation helpful? Give feedback.
-
What library version do you use? |
Beta Was this translation helpful? Give feedback.
-
@svinota |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot. Let me reproduce your config on a VM and I will return to you tomorrow. |
Beta Was this translation helpful? Give feedback.
-
Thank you! If you need preconfigured VM for testing just let me know. |
Beta Was this translation helpful? Give feedback.
-
Sorry for the wait. The issue was that there are many expressions within one rule. Here is an example code: import struct
from pyroute2.nftables.main import NFTables
from pyroute2 import NDB
def get_cmp_eq(rule, ndb):
nft_reg = None # register to look
nft_key = None # the key type
nft_data = None # the cmp data
expr_name = None # expression type -- meta, cmp, counter, immediate...
# iterate all the expression in a rule
for expression in rule.get('NFTA_RULE_EXPRESSIONS'):
expr_name = expression.get('NFTA_EXPR_NAME')
if expr_name == 'meta':
nft_key = expression.get(('NFTA_EXPR_DATA', 'NFTA_META_KEY'))
nft_reg = expression.get(('NFTA_EXPR_DATA', 'NFTA_META_DREG'))
if expr_name == 'cmp':
# load only NFT_CMP_EQ ( == ) operations data
if (
expression.get(('NFTA_EXPR_DATA', 'NFTA_CMP_SREG')) == nft_reg
and expression.get(('NFTA_EXPR_DATA', 'NFTA_CMP_OP')) == 'NFT_CMP_EQ'
):
nft_data = expression.get(
('NFTA_EXPR_DATA', 'NFTA_CMP_DATA', 'NFTA_DATA_VALUE')
)
# nft returns interface index in NFT_META_IIF and NFT_META_OIF
if nft_key in ('NFT_META_IIF', 'NFT_META_OIF'):
# decode the index
if_index = struct.unpack('I', nft_data)[0]
# lookup the NDB
nft_data = ndb.interfaces[if_index]['ifname']
return (nft_key, nft_data)
with NDB() as ndb, NFTables() as nft:
for rule in nft.get_rules():
print(get_cmp_eq(rule, ndb)) And here is how it works:
on a slightly simplified ruleset:
|
Beta Was this translation helpful? Give feedback.
-
Hi! Sorry for long time answer. We should was do some tests. We see two problems:
|
Beta Was this translation helpful? Give feedback.
-
Indeed it might be a good idea to provide a pre-configured VM, so we will be in synced environments. So if you can share a qemu/kvm image with me, I would use it for prototyping. Thus we will avoid inconsistent test results. Regarding the performance — it is possible to use specific parsers for particular requests, that will match binary protocol data instead of decoding & comparing, and then the code will decode only particular fields. |
Beta Was this translation helpful? Give feedback.
-
Hi everybody.
I have this rule:
When I do
rules = nft.get_rules()
, I get:{'nfgen_family': 1, 'version': 0, 'res_id': 236, 'attrs': [('NFTA_RULE_TABLE', 'prerouting'), ('NFTA_RULE_CHAIN', 'POLICER'), ('NFTA_RULE_HANDLE', 4), ('NFTA_RULE_EXPRESSIONS', [{'attrs': [('NFTA_EXPR_NAME', 'meta'), ('NFTA_EXPR_DATA', {'attrs': [('NFTA_META_KEY', 'NFT_META_IIF'), ('NFTA_META_DREG', 'NFT_REG_1')]})]}, {'attrs': [('NFTA_EXPR_NAME', 'cmp'), ('NFTA_EXPR_DATA', {'attrs': [('NFTA_CMP_SREG', 'NFT_REG_1'), ('NFTA_CMP_OP', 'NFT_CMP_EQ'), ('NFTA_CMP_DATA', {'attrs': [('NFTA_DATA_VALUE', b'\x03\x00\x00\x00')]})]})]}, {'attrs': [('NFTA_EXPR_NAME', 'counter'), ('NFTA_EXPR_DATA', {'attrs': [('NFTA_COUNTER_BYTES', 3904398), ('NFTA_COUNTER_PACKETS', 48244)]})]}, {'attrs': [('NFTA_EXPR_NAME', 'immediate'), ('NFTA_EXPR_DATA', {'attrs': [('NFTA_IMMEDIATE_DREG', 'NFT_REG_VERDICT'), ('NFTA_IMMEDIATE_DATA', {'attrs': [('NFTA_DATA_VERDICT', {'attrs': [('NFTA_VERDICT_CODE', 'NF_ACCEPT')]})]})]})]}])], 'header': {'length': 236, 'type': 2566, 'flags': 2050, 'sequence_number': 255, 'pid': 3497, 'error': None, 'target': 'localhost', 'stats': Stats(qsize=0, delta=0, delay=0)}}
How can I get a meta oif? I want to get br100 is a result.
Beta Was this translation helpful? Give feedback.
All reactions