pyroute2 icon indicating copy to clipboard operation
pyroute2 copied to clipboard

Add an option to set "NLM_F_ECHO" flag

Open ffourcot opened this issue 2 years ago • 3 comments

Hello,

I recently saw a discussion on kernel mailing-list about the "NLM_F_ECHO" flag. It can help to get real kernel result after a command. Some examples when it's enabled:

In [1]: from pyroute2 import IPRoute

In [2]: ipr = IPRoute()

In [3]: ipr.route("add", dst="8.8.8.8/32", gateway="192.168.1.254", oif=6)
Out[3]: 
({'family': 2,
  'dst_len': 32,
  'src_len': 0,
  'tos': 0,
  'table': 254,
  'proto': 4,
  'scope': 0,
  'type': 1,
  'flags': 0,
  'attrs': [('RTA_TABLE', 254),
   ('RTA_DST', '8.8.8.8'),
   ('RTA_GATEWAY', '192.168.1.254'),
   ('RTA_OIF', 6)],
  'header': {'length': 60,
   'type': 24,
   'flags': 1536,
   'sequence_number': 255,
   'pid': 3118269,
   'error': None,
   'target': 'localhost',
   'stats': Stats(qsize=0, delta=0, delay=0)},
  'event': 'RTM_NEWROUTE'},)

In [4]: ipr.route("del", dst="8.8.8.8/32")
Out[4]: 
({'family': 2,
  'dst_len': 32,
  'src_len': 0,
  'tos': 0,
  'table': 254,
  'proto': 4,
  'scope': 0,
  'type': 1,
  'flags': 0,
  'attrs': [('RTA_TABLE', 254),
   ('RTA_DST', '8.8.8.8'),
   ('RTA_GATEWAY', '192.168.1.254'),
   ('RTA_OIF', 6)],
  'header': {'length': 60,
   'type': 25,
   'flags': 0,
   'sequence_number': 256,
   'pid': 3118269,
   'error': None,
   'target': 'localhost',
   'stats': Stats(qsize=0, delta=0, delay=0)},
  'event': 'RTM_DELROUTE'},)

However, enabling it is currently very hard in pyroute2, since I did not find another way than patching this code:

          commands = {
-             'add': (RTM_NEWROUTE, flags_make),
+             'add': (RTM_NEWROUTE, flags_make | NLM_F_ECHO),
              'set': (RTM_NEWROUTE, flags_replace),
              'replace': (RTM_NEWROUTE, flags_replace),
              'change': (RTM_NEWROUTE, flags_change),
              'append': (RTM_NEWROUTE, flags_append),
-             'del': (RTM_DELROUTE, flags_base),
+             'del': (RTM_DELROUTE, flags_base | NLM_F_ECHO),
              'remove': (RTM_DELROUTE, flags_base),

Since it's a "generic" option (not so much actually, but available on several commands) I think that pyroute2 should provide a simple api, so giving a "echo=True" argument should be enough to get it

ffourcot avatar Sep 22 '22 12:09 ffourcot

Yep. I'm to fix that asap

svinota avatar Sep 22 '22 13:09 svinota

ok, it's already partly usable on the branch (see https://github.com/svinota/pyroute2/pull/1032)

until Monday adding some tests, moving related methods to the common socket base class, and then merging

usage: ipr = IPRoute(nlm_echo=True)

svinota avatar Sep 24 '22 15:09 svinota

@svinota thanks! I will try asap

ffourcot avatar Sep 27 '22 06:09 ffourcot