24

我正在测试使用以太网(不是 IP)在两台计算机之间进行通信的协议的实现。为了实际上不必拥有两台物理计算机,我想创建两个虚拟以太网接口。它们只能相互通信,因此一个端点程序将绑定到一个接口,而另一个端点将绑定到另一个。

这可能吗?我该怎么做?

4

6 回答 6

20

您可以使用 VDE2,一个虚拟交换机。

例如(您将需要一些术语):

# 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

在一侧输入,它将出现在另一侧。

于 2010-01-17T22:16:08.207 回答
5

您可以使用“tap”虚拟以太网驱动程序,它可以让用户空间程序伪装成以太网接口。这是一段时间以来的标准内核功能(尽管它可能未在您的内核中启用)。

于 2010-01-17T21:44:35.163 回答
1

如果需要,您可以使用 ns3 模拟两个 Tap 设备之间的复杂网络:http ://www.nsnam.org/

我已经让它在两个 virtualbox 实例之间模拟两个交换机、一个无线客户端和一个 AP。

于 2010-01-17T22:26:31.040 回答
1

如果您想要自己的子网并且不想费心使用 vde。

看看这个。简而言之:

# 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)

或者用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
于 2014-03-03T14:27:19.673 回答
0

人机界面 man ifconfig

只需在 /etc/network/interfaces 中添加一个新节

我的示例配置:

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 有 ip 192.168.2.150 而 eth0:1 有 192.168.2.2

于 2011-02-28T17:22:28.170 回答
-1

您可以使用 vconfig 命令示例:

vconfig add eth0 10 #virtual interface eth0.10 will be created
于 2014-02-13T23:55:54.780 回答