Linux – How to set network interface affinity under Ubuntu Linux

affinityinterfacelinuxprocessrouting

I have a multihomed linux box with 6 network interfaces that I'm using to run some tests of router hardware. I would like to be able to set "NIC affinity" (for lack of a better term) on an arbitrary process so that even though a destination IP might correspond to a local interface, all network traffic for that process will be routed out via another interface (and looped back in by the external device). Ideally, one process's affinity setting will not affect another's.

As a sample use case, say I have eth0=192.168.0.1 and eth1=10.0.0.1. and an external router connected to eth0 and eth1 with the IPs of 192.168.0.2 and 10.0.0.2, respectively. If I open up my shell and ping 10.0.0.1, no traffic will traverse the external connection as the destination corresponds to the local machine, but if I were to somehow set the shell process's affinity to eth0, ICMP traffic would flow out via eth0, into the external router, and back again to eth1. It's the traversal of the external router that I want to guarantee, and as there will be more than one, I need to be able to bind to arbitrary NICs.

Is this possible? Given my searches thus far, I see no answers but the absence of mentions of what I'm trying to do leads me to suspect I'm missing an important consideration, as this doesn't seem like a original question (just an odd one).

FWIW, this is an Ubuntu Linux 11.10 box. Any pointers to similar work or suggestions for tools would be greatly appreciated. Thanks!

Best Answer

This seems like a situation where you might need to take one step back and let us know in a more general way what you are trying to accomplish, rather then how to implement a particular solution.

That being said, in order to set a process bound to a specific interface in Linux you need to hack the bind system call (man 2 bind). You can do this by using alternative libraries and pre-loading them, see How to use different network interfaces for different processes over on Super User.