Skip to content

Commit

Permalink
iproute: Merge pull request #1124 from liske/fix-set-newnsid
Browse files Browse the repository at this point in the history
iproute: fix set_netnsid implementation to work with NetNS

Bug-Url: #1124
Bug-Url: #1121
Bug-Url: #1123
  • Loading branch information
svinota authored Oct 27, 2023
2 parents 32f5fa0 + 8f95be8 commit f61f392
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions pyroute2/iproute/linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from itertools import chain
from socket import AF_INET, AF_INET6, AF_UNSPEC

import pyroute2.netns
from pyroute2 import config
from pyroute2.common import AF_MPLS, basestring
from pyroute2.config import AF_BRIDGE
Expand Down Expand Up @@ -743,33 +742,26 @@ def get_netns_info(self, list_proc=False):
except OSError:
pass

def set_netnsid(self, name=None, fd=None, nsid=None):
def set_netnsid(self, nsid=None, pid=None, fd=None):
'''Assigns an id to a peer netns using RTM_NEWNSID query.
The kernel chooses an unique id if nsid is omitted.
This corresponds to the "ip netns set" command.
'''
msg = nsidmsg()
fh = None

try:
if name is not None:
netns_path = pyroute2.netns._get_netnspath(name)
fh = open(netns_path, 'r')
fd = fh.fileno()
if nsid is None or nsid < 0:
# kernel auto select
msg['attrs'].append(('NETNSA_NSID', 4294967295))
else:
msg['attrs'].append(('NETNSA_NSID', nsid))

if pid is not None:
msg['attrs'].append(('NETNSA_PID', pid))

if fd is not None:
msg['attrs'].append(('NETNSA_FD', fd))
if nsid is None or nsid < 0:
# kernel auto select
msg['attrs'].append(('NETNSA_NSID', 4294967295))
else:
msg['attrs'].append(('NETNSA_NSID', nsid))

return self.nlm_request(
msg, RTM_NEWNSID, NLM_F_REQUEST | NLM_F_ACK
)
finally:
if fh is not None:
fh.close()
return self.nlm_request(msg, RTM_NEWNSID, NLM_F_REQUEST | NLM_F_ACK)

# 8<---------------------------------------------------------------

Expand Down

0 comments on commit f61f392

Please sign in to comment.