Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving interfaces to the outer namespace #306

Closed
jimfunk opened this issue Oct 25, 2016 · 5 comments
Closed

Moving interfaces to the outer namespace #306

jimfunk opened this issue Oct 25, 2016 · 5 comments

Comments

@jimfunk
Copy link

jimfunk commented Oct 25, 2016

I'm trying to get interfaces that I have moved into a namespace back out into the main namespace. However, none of the obvious methods appear to work. I've tried setting net_ns_pid and net_ns_fd and both fail in different way.

When I tried net_ns_pid, it would wait a while and then half-work:

Traceback (most recent call last):
  File "./nstest.py", line 21, in <module>
    interface.net_ns_pid = os.getpid()
  File "/usr/lib/python3.5/site-packages/pyroute2/ipdb/transactional.py", line 194, in __exit__
    self.commit()
  File "/usr/lib/python3.5/site-packages/pyroute2/ipdb/interface.py", line 905, in commit
    raise error
  File "/usr/lib/python3.5/site-packages/pyroute2/ipdb/interface.py", line 745, in commit
    transaction.wait_all_targets()
  File "/usr/lib/python3.5/site-packages/pyroute2/ipdb/transactional.py", line 468, in wait_all_targets
    raise CommitException('target %s is not set' % key)
pyroute2.ipdb.exceptions.CommitException: target net_ns_pid is not set

The really strange thing here is that the interface exists in both the outer and inner namespaces at this point. I didn't think that was even possible:

# ip link show fooint
73: fooint: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether 12:51:fa:28:2f:2e brd ff:ff:ff:ff:ff:ff
# ip netns exec foo ip link show fooint
7: fooint: <BROADCAST,NOARP> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
    link/ether 12:51:fa:28:2f:2e brd ff:ff:ff:ff:ff:ff

With net_ns_fd, it doesn't work at all:

Traceback (most recent call last):
  File "./nstest.py", line 22, in <module>
    interface.net_ns_fd = outer_ns_fd
  File "/usr/lib/python3.5/site-packages/pyroute2/ipdb/transactional.py", line 194, in __exit__
    self.commit()
  File "/usr/lib/python3.5/site-packages/pyroute2/ipdb/interface.py", line 905, in commit
    raise error
  File "/usr/lib/python3.5/site-packages/pyroute2/ipdb/interface.py", line 727, in commit
    run(nl.link, 'set', **request)
  File "/usr/lib/python3.5/site-packages/pyroute2/ipdb/interface.py", line 459, in _run
    raise error
  File "/usr/lib/python3.5/site-packages/pyroute2/ipdb/interface.py", line 454, in _run
    return cmd(*argv, **kwarg)
  File "/usr/lib/python3.5/site-packages/pyroute2/iproute.py", line 1239, in link
    msg_flags=msg_flags)
  File "/usr/lib/python3.5/site-packages/pyroute2/netlink/nlsocket.py", line 791, in nlm_request
    return do_try()
  File "/usr/lib/python3.5/site-packages/pyroute2/netlink/nlsocket.py", line 770, in do_try
    ret = self.get(msg_seq=msg_seq, terminate=terminate)
  File "/usr/lib/python3.5/site-packages/pyroute2/netlink/nlsocket.py", line 638, in get
    raise msg['header']['error']
pyroute2.netlink.exceptions.NetlinkError: (9, 'Bad file descriptor')

This is the script I'm using to test:

#!/usr/bin/env python3
import os
from pyroute2 import NetNS, IPDB


outer = IPDB()
netns = NetNS('foo')
inner = IPDB(netns)
outer_ns_fd = os.open('/proc/%s/ns/net' % os.getpid(), os.O_RDONLY)
#outer_ns_fd = os.open('/var/run/netns/foo', os.O_RDONLY)


for ipdb in (outer, inner):
    if 'fooint' in ipdb.interfaces:
        with ipdb.interfaces['fooint'] as interface:
            interface.remove()

inner.create(ifname='fooint', kind='dummy').commit()

with inner.interfaces['fooint'] as interface:
    #interface.net_ns_pid = os.getpid()
    interface.net_ns_fd = outer_ns_fd

assert 'fooint' not in inner.interfaces
assert 'fooint' in outer.interfaces
@svinota
Copy link
Owner

svinota commented Oct 27, 2016

I didn't think that was even possible

Ahhaha, me neither :)))) Thanks for the report, and give me some time to investigate :)

@svinota svinota added the bug label Oct 27, 2016
@svinota
Copy link
Owner

svinota commented Oct 27, 2016

Pls, write also you kernel and pyroute2 versions. Thanks!

svinota pushed a commit that referenced this issue Oct 27, 2016
@svinota
Copy link
Owner

svinota commented Oct 27, 2016

The net_ns_pid in IPDB is fixed, pls try it.

@jimfunk
Copy link
Author

jimfunk commented Oct 27, 2016

Yup. This works perfectly.

Thanks!

@svinota
Copy link
Owner

svinota commented Oct 27, 2016

Cool. So I close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants