开发者

How do I create virtual ethernet devices in linux?

开发者 https://www.devze.com 2022-12-16 12:26 出处:网络
I am testing an implementation of a protocol that talks between two computers using ethernet (not IP). In order to not actually have to have two physical computers, I want to create two virtual ethern

I am testing an implementation of a protocol that talks between two computers using ethernet (not IP). In order to not actually have to have two physical computers, I want to create two virtual ethernet interfaces. These would only be able to talk to each other, so 开发者_开发知识库one endpoint program would bind to one interface and the other endpoint would bind to the other.

Is this possible and how do I do it?


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.


You can use the "tap" virtual ethernet driver which lets a userspace program pretend to be an ethernet interface. This is a standard kernel feature for some time now (it might not be enabled in your kernel though).


You can use ns3 to emulate a complicated network between two tap devices if you need it: http://www.nsnam.org/

I've had it emulating two switches, a wireless client, and an AP, between two virtualbox instances.


if you want your own subnet and don't want to bother to use vde.

look at this. In short:

# tunctl -t eth0
Set 'eth0' persistent and owned by uid 0
# ifconfig eth0
eth0      Link encap:Ethernet  HWaddr a6:9b:fe:d8:d9:5e  
      BROADCAST MULTICAST  MTU:1500  Metric:1
      RX packets:0 errors:0 dropped:0 overruns:0 frame:0
      TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
      collisions:0 txqueuelen:500 
      RX bytes:0 (0.0 B)  TX bytes:0 (0.0 B)

Or with ip:

# ip tuntap add dev eth0 mode tap
# ip link ls dev eth0
  7: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT qlen 500
  link/ether 0e:55:9b:6f:57:6c brd ff:ff:ff:ff:ff:ff


man interfaces man ifconfig

just add a new stanza in /etc/network/interfaces

my example configuration:

iface eth0 inet static
   address 192.168.2.150
   netmask 255.255.255.0
   network 192.168.2.0
   broadcast 192.168.2.255
   gateway 192.168.2.253
   # dns-* options are implemented by the resolvconf package, if installed
   dns-nameservers 8.8.4.4


iface eth0:1 inet static
    address 192.168.2.2
    netmask 255.255.255.0
    network 192.168.2.0
    broadcast 192.168.2.255
    gateway 192.168.2.253
    # dns-* options are implemented by the resolvconf package, if installed
    dns-nameservers 8.8.4.4

--

eth0 has ip 192.168.2.150 while eth0:1 has 192.168.2.2


You can use the vconfig command example:

vconfig add eth0 10 #virtual interface eth0.10 will be created
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号