linux下在多vlan环境下配置网络
2014-06-13 12:20:35 阿炯

公司从第三方租用了服务器和网络资源,对方提供了三个网段,由于我们购买了百兆的独立带宽,只能将它们划为三个vlan,默认为ip数量最多网段。这样就可以尽可能多的使用百兆的带宽,而不用拆分使用或另外接线。

vlan id分别为301 302 303,默认为302。

服务器分为内外网,外网即要通过这三个网段出去。如果是在虚拟机上使用,为网口指定特定的vlan id是比较容易的,在实体机上,就需要另外设定了。

302为默认的vlan段,其它两个段直接配置到网口上时,不能正常使用,也无法上网。需要使用'vconfig'指令来配置,记得要先载入'8021q'的内核模块。

# vconfig -h
Expecting argc to be 3-5, inclusive.  Was: 2

Usage: add             [interface-name] [vlan_id]
       rem             [vlan-name]
       set_flag        [interface-name] [flag-num]       [0 | 1]
       set_egress_map  [vlan-name]      [skb_priority]   [vlan_qos]
       set_ingress_map [vlan-name]      [skb_priority]   [vlan_qos]
       set_name_type   [name-type]

* The [interface-name] is the name of the ethernet card that hosts
  the VLAN you are talking about.
* The vlan_id is the identifier (0-4095) of the VLAN you are operating on.
* skb_priority is the priority in the socket buffer (sk_buff).
* vlan_qos is the 3 bit priority in the VLAN header

* name-type:  VLAN_PLUS_VID (vlan0005), VLAN_PLUS_VID_NO_PAD (vlan5),
              DEV_PLUS_VID (eth0.0005), DEV_PLUS_VID_NO_PAD (eth0.5)
* bind-type:  PER_DEVICE  # Allows vlan 5 on eth0 and eth1 to be unique.
              PER_KERNEL  # Forces vlan 5 to be unique across all devices.
* FLAGS:  1 REORDER_HDR  When this is set, the VLAN device will move the
            ethernet header around to make it look exactly like a real
            ethernet device.  This may help programs such as DHCPd which
            read the raw ethernet packet and make assumptions about the
            location of bytes.  If you don't need it, don't turn it on, because
            there will be at least a small performance degradation.  Default
            is OFF.
            
vlan协议数据帧格式


Linux 的内核也是有支援VLAN 功能,就是在网口的讯框加上指定的VLAN Tag,设定使用上还是很简单的。

模块加载
modprobe 8021q
lsmod |grep -i 8021q

# vconfig add [interface-name] [vlan_id]

为指定的网口设定vid,需要注意的是被设定网路装置[interface-name]必须是active 的,即通过过'ifconfig'能查询到的。

所有设定好的VLAN可以通过/proc/net/vlan/config 来查询到,不过这样的设定下次重新开机就会失效,还需要将保存在/etc/rc.local,可在开机时候就会生效。

立即移除时可使用指令:
# vconfig rem [ vlan-name]

下面是在脚本中的片段
modprobe 8021q
vconfig add em1 303
ifup em1.303
ifconfig em1.303 61.189.249.130 netmask 255.255.255.224
ifconfig em1.303:1 61.189.249.131 netmask 255.255.255.224
ifconfig em1.303:2 61.189.249.132 netmask 255.255.255.224
ifconfig em1.303:3 61.189.249.133 netmask 255.255.255.224
ifconfig em1.303:4 61.189.249.134 netmask 255.255.255.224
ifconfig em1.303:5 61.189.249.135 netmask 255.255.255.224
ifconfig em1.303:6 61.189.249.136 netmask 255.255.255.224
ifconfig em1.303:7 61.189.249.137 netmask 255.255.255.224
ifconfig em1.303:8 61.189.249.138 netmask 255.255.255.224
ifconfig em1.303:9 61.189.249.139 netmask 255.255.255.224

route add default gw 61.189.249.129
route del -net 0.0.0.0 gw 192.168.30.251

将为外网口配置10个公网ip地址,同时将系统的默认网关由原先的风网改为外网所提供的网关地址。这是在ip比较多的情况下,将这些设置保存为脚本文件并赋于权限,在加到rc.local中,这样下次开机后即可。

如果所用的ip只有一、两个,可以在系统的网络配置目录新建接口文件。
cd /etc/sysconfig/network-scripts

cp ifcfg-eth0 ifcfg-eth0.303

vi ifcfg-eth0.303 将eth0改成eth0.303

将关于eth0的ip删除掉,这样下次启动时可自动启动接口。


开机启动802.1q

在/etc/sysconfig/network中声明
VLAN=yes


保留vlan配置

在/etc/rc.local

添加如下内容:
modprobe 8021q
vconfig add eth0 303
ifup eth0.303


对vlan的理解

通过VLan 的功能能将一个实体的switch 将他切割成多个logical broadcast domain (所有的广播封包broadcast package 只会在此doamin 中传送),他的做法也很简单就是在原先的讯框(Frame)指定tag,而这 tag 的数字即是VLAN ID,VLAN 已经被IEEE 列为标准协定的一员802.1q,默认的VLAN ID 皆为1。vlan_id的范围为:0-4095。

参考来源

linux 下VLAN数据包格式及vconfig命令测试

Linux 设置 多ip,多vlan