Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

366
Views
How to enter network namespace by name?

My program needs to connect to two TAP interfaces that each live in their own network namespace. Because setns() works at the pthread level I plan to have a dedicated pthread for each interface. (My program itself runs in the "root" namespace.)

However, the setns() function requires me to pass a file descriptor to the network namespace I want to enter. Obtaining this descriptor requires the pid of a process that already exists in that namespace. Once I have that PID I can do a call to open() to get the file descriptor to the network namespace:

int fd = open("/proc/<pid>/ns/net");

But how do I obtain that PID?

One way would be to create a "dummy" process in the desired namespace:

ip netns exec tap101-ns sleep 100 & 
[1] 30645

And then use that PID in my program: int fd = open("/proc/30645/ns/net")

However, this way of working seems a bit silly...

Is there a cleaner way to enter a network namespace when all I have is the name of the namespace I want to enter?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I stumbled upon a solution shortly after posting my question.

The path /var/run/netns/ can also be used to obtain a file descriptor. According to the ip-netns documentation:

The file descriptor resulting from opening /var/run/netns/NAME refers to the specified network namespace. Holding that file descriptor open keeps the network namespace alive. The file descriptor can be used with the setns(2) system call to change the network namespace associated with a task.

So I can use this code to switch to a namespace inside my pthread:

int namespace_fd = open("/var/run/netns/tap101-ns");
setns(namespace_fd, CLONE_NEWNET);
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!