HTTP/HTTP2/HTTPS / Technology · 2017年5月9日

两种GRE隧道

之前改过GRE Kernel的代码,合并到不同的Kernel版本上。不曾想,当合并到Kernel版本4.4.29上的时候,Jeff突然问了我一个问题:

If we are doing WCCP Tunnel back we don’t call __gre_xmit? Why skipping __gre_xmit in gre_tap_xmit?

这不是我写的代码,但是突然一想,问的对啊,这个gre_tap_xmit是干嘛的?

于是追代码,发现我们一直关注的GRE Tunnel的代码是ipgre_xmit->__gre_xmit,ipgre_xmit绑定在ipgre_netdev_ops上,而gre_tap_xmit绑定在gre_tap_netdev_ops上。

那么问题来了:难道有两种GRE设备?


带着这个问题我Google了很多文章,但是都没有太清楚的说明。知道我找到这篇文章:《Two types of GRE tunneling configurations

A GRE tunnel can be created between two computers using an ip command. Either IP/GRE or (IP)/Ethernet/GRE tunnel can be created by slightly modifying the commands.

GRE (Generic Routing Encapsulation) is a layer 3 (L3) tunneling protocol, which was standardized by the IETF. The following command list is an example of a script that creates IP/GRE tunnel over IP.

# addgreip REMOTE_IP LOCAL_IP GRE_KEY LOCAL_SLICE_IP
ip tunnel add gre0 mode gre remote ${1} local ${2} key ${3}
ip addr add ${4} dev gre0
ip link set gre0 up
ip link set gre0 mtu 1460

As described in the first line, the command arguments are the destination IP address (i.e., the end of the GRE tunnel), the source (my) IP address (i.e., the beginning of the GRE tunnel), the GRE key, and the source (my) IP address on the tunnel (i.e., the IP address of the virtual network) and the subnet length. If the subnet length is not specified, an error occurs.

If no GRE key is specified by the ip tunnel command, the packet size becomes smaller; it must be specified for the above script. An example of the command usage follows.

./addgreip 10.0.32.129 10.0.32.15 1 192.168.10.2/24

The next script is for IP/Ethernet/GRE/IP.

# addgreether REMOTE_IP LOCAL_IP GRE_KEY LOCAL_SLICE_IP
ip link add gre1 type gretap remote ${1} local ${2} key ${3}
ip addr add ${4} dev gre1
ip link set gre1 up
ip link set gre1 mtu 1450

The command arguments are similar to those for the IP/GRE/IP commands. The MAC address specified in the inserted MAC header is filled by the computer if is is not specified by the user. An example of the command usage follows.

./addgreether 10.0.32.129 10.0.32.15 1 192.168.10.2/24

The ip command may refuse the above commnad (type gretap) when using older Linux. In such a case, a package called “iproute2” should be upgraded.

When IP/GRE/IP is used, an ip tunnel command is used for configuration. However, when IP/Ethernet/GRE/IP is used, an ip link command is used. As for command keywords, a “mode” parameter is used for GRE when IP/GRE is used. However, “type gretap” is used when IP/Ethernet/GRE is used. I am skeptical why these commands are so different.


简单说,对于GRE Tunnel来说,确实是有两种设备。一种是IP_GRE,一种是GRE_TAP。第一种主要服务于本地和目的地为不同网络的情况,而第二种更像是在局域网内构建的GRE Tunnel。

这也就是为什么文章中称第一种为IP/GRE,而第二种为(IP)/Ethernet/GRE。

再换一种说法,也是关键点,第一种的device type是gre,而第二种的Device type是gretap。

第一种的用法,我想应该就是我们的产品那样,跨网络使用。

而第二种的用法,我想应该是用在VPN的情况下,毕竟同局域网直接使用L2就行了。