How to Create Virtual Ethernet Devices in Linux

How do I create virtual ethernet devices in linux?

You can use VDE2, a virtual switch.

For example (you will need a few terms):

# Install vde2 (assumes Debian/Ubuntu)
sudo aptitude install vde2
# Create the switch and two tap interfaces attached to it
sudo vde_switch -tap tap0 -tap tap1
# Configure the interfaces
sudo ip addr add 10.0.31.10 dev tap0
sudo ip addr add 10.0.31.11 dev tap1
# Start a server
socat - TCP-LISTEN:4234,bind=10.0.31.10
# Alternatively, an echo server:
#socat PIPE TCP-LISTEN:4234,bind=10.0.31.10
# Start a client
socat - TCP:10.0.31.10:4234,bind=10.0.31.11

Type on one side, it will appear on the other.

How can I create a virtual Ethernet device in C?

You cannot create any virtual Ethernet device in portable C11 (or portable C99) because the C standard does not have any notion of Ethernet devices.

You need some operating system support for that, and your question becomes operating system specific.

If you are targeting the Linux operating system, you could use strace(1) to understand the system calls executed by your ip commands.

However, you could simply write some application-specific shell script (running the above commands) and carefully use system(3) or popen(3) to run it (or even use directly fork(2), execve(2), pipe(2), poll(2) etc... read Advanced Linux Programming for more). Be scared of potential code injection, and if you build the command string, check any input data entering it.

As ysdx commented, look inside rtnetlink(7)

How to Create a virtual network interface and connect it to a bridge

Setting up another virtual interface is not necessary since one can assign an IP to the bridge:

ip addr add dev br0 <ip>

This can then be used to communicate with the other devices on the bridge if these have IPs in the same subnet

Create new ethernet usb network interface on Linux

this is ethernet-over-usb

https://en.wikipedia.org/wiki/Ethernet_over_USB

without external hardware you can try the CDCether kernel module and ethtool ( then you can only connect to a usb device that operates in usb device mode )

( https://developer.ridgerun.com/wiki/index.php/How_to_use_USB_device_networking , http://tldp.org/HOWTO/Motorola-Surfboard-Modem/usb.html, http://www.linux-usb.org/usbnet/ )

else you need a physical adapter for this. the adapter translates between the protocols and the different hardware interfaces.

in usb protocol can only be one host in a network, therefore you need at least a host-to-host cable ( http://www.linux-usb.org/usbnet/ ) if you want to connect two usb host devices, i.e. two pcs

required kernel module ( driver ) when using a physical adapter is either usbnet ( with its minidrivers ) or usb-eth

Make virtual network interface in Linux?

You want the tun/tap device:

http://en.wikipedia.org/wiki/TUN/TAP



Related Topics



Leave a reply



Submit