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
proposal: net: Allow a socket to be created in a Linux network namespace #62581
Comments
Can you expand on what use cases are not handled by setting the namespace yourself? It's not obvious to me from glancing at the issues you mention. Thanks. I agree that creating a new socket in a different namespace is not simple, but it is also something that very few programs need to do. As long as it is possible to do this, it seems to me that this is a better fit for an external third party package than for the standard library. https://go.dev/doc/faq#x_in_std |
Namespace discoverer here: it's actually much more involved, not least considering proper error handling and the multiple forms that namespace references might take on before they turn into some fd. And don't get us started on talking about the correct lifecycle handling of open fds... I can only second @ianlancetaylor that this isn't something for the std lib, especially as it is very, very Linux-specific. Also, why just supporting network namespaces and dial, and not the other namespace types? I basically know of these two 3rd party libs that act as the namespace-switching building block to wrap the dial part:
|
The use case is to dial or listen in another network namespace by using the standard library and hooks provided by the standard library. Is this supported today? Sort of. You can do the following:
You can further optimize a bit on this: You can install a Control object in the dialer/listener. As soon as it is indicated to the Control object that a socket has been created, you can go ahead and revert the namespace and call runtime.UnlockOSThread. I see two problems with it, though:
Is this good enough? Maybe, but I do not think it is ideal. I do agree with @thediveo that the standard library should not support Linux namespaces directly. It should provide hooks so that you can support them reliably with mechanisms like the one outlined above. |
@cwmos which hooks (extension points) would you expect? |
I guess I do not have any super good suggestions for how to change the API. So I will close this issue. |
In Linux, a socket can be created in a network namespace as follows:
There seems to be no good way to do this with the Go runtime:
It has been proposed to set the network namespace before calling Dial #46932, #44922. However, that seems like a somewhat fragile approach and it does not support all use cases as also pointed out in these issues.
It is not immediately clear to me what a good way to implement this feature would be. Perhaps syscall.RawConn could have a function to set the file descriptor?
The text was updated successfully, but these errors were encountered: