linux虚拟网络设备之vlan配置详解

作者:daisy 时间:2023-08-07 04:38:22 

简介

VLAN是网络栈的一个附加功能,且位于下两层。首先来学习Linux中网络栈下两层的实现,再去看如何把VLAN这个功能附加上去。下两层涉及到具体的硬件设备,日趋完善的Linux内核已经做到了很好的代码隔离,对网络设备驱动也是如此,如下图所示:

linux虚拟网络设备之vlan配置详解

这里要注意的是,Linux下的网络设备net_dev并不一定都对应实际的硬件设备,只要注册一个struct net_device{}结构体(netdevice.h)到内核中,那么这个网络设备就存在了。该结构体很庞大,其中包含设备的协议地址(对于IP即IP地址),这样它就能被网络层识别,并参与路由系统,最有名的当数loopback设备。不同的设备(包括硬件和非硬件)的ops操作方法各不相同,由驱动自己实现。一些通用性的、与设备无关的操作流程(如设备锁定等)则被Linux提炼出来,我们称为驱动框架。

linux虚拟网络设备之vlan配置

我们通过一个网桥两个设备对,来连接两个网络名字空间,每个名字空间中创建两个vlan

linux虚拟网络设备之vlan配置详解

借助vconfig来配置vlan:


#创建网桥
brctl addbr br-test-vlan
#创建veth对儿
ip link add veth01 type veth peer name veth10
ip link add veth02 type veth peer name veth20
#将veth对儿的一段添加到网桥
brctl addif br-test-vlan veth01
brctl addif br-test-vlan veth02
#启动设备
ip link set dev br-test-vlan up
ip link set dev veth01 up
ip link set dev veth02 up
ip link set dev veth10 up
ip link set dev veth20 up
#创建网络名字空间
ip netns add test-vlan-vm01
ip netns add test-vlan-vm02
#将设备对儿的另一端添加到另个名字空间(其实在一个名字空间也能玩,只是两个名字空间更加形象)
ip link set veth10 netns test-vlan-vm01
ip link set veth20 netns test-vlan-vm02
#分别进入两个名字空间创建vlan和配置ip
#配置名字空间test-vlan-vm01
ip netns exec test-vlan-vm01 bash
#配置vlan 3001 和 vlan 3002
vconfig add veth10 3001
vconfig add veth10 3002
#启动两个vlan的设备
ip link set veth10.3001 up
ip link set veth10.3002 up
#分别在两个vlan上配置ip (这里简单起见,使用了同一个网段了IP,缺点是,需要了解一点儿路由的知识)
ip a add 172.16.30.1/24 dev veth10.3001
ip a add 172.16.30.2/24 dev veth10.3002
#添加路由
route add 172.16.30.21 dev veth10.3001
route add 172.16.30.22 dev veth10.3002
#配置名字空间test-vlan-vm02
ip netns exec test-vlan-vm02 bash
#配置vlan 3001 和 vlan 3002
vconfig add veth20 3001
vconfig add veth20 3002
#启动两个vlan的设备
ip link set veth20.3001 up
ip link set veth20.3002 up
#分别在两个vlan上配置ip (这里简单起见,使用了同一个网段了IP,缺点是,需要了解一点儿路由的知识)
ip a add 172.16.30.21/24 dev veth20.3001
ip a add 172.16.30.22/24 dev veth20.3002
#添加路由
route add 172.16.30.1 dev veth20.3001
route add 172.16.30.2 dev veth20.3002

查看一下vlan配置:


# cat /proc/net/vlan/config
VLAN Dev name | VLAN ID
Name-Type: VLAN_NAME_TYPE_RAW_PLUS_VID_NO_PAD
veth10.3001 | 3001 | veth10
veth10.3002 | 3002 | veth10

现在,我们可以分别在两个名字空间来ping另外一个名字空间的两个IP,虽然两个IP都能ping通,但是使用的源IP是不同的,走的vlan也是不同的,我们可以在veth01/veth10/veth02/veth20/br-test-vlan 任意一个上抓包,会看到vlan信息:


# tcpdump -i veth10 -nn -e
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on veth10, link-type EN10MB (Ethernet), capture size 262144 bytes
15:38:18.381010 82:f7:0e:2d:3f:62 > 9e:58:72:fa:11:15, ethertype 802.1Q (0x8100), length 102: vlan <span style="color: #ff0000;">3001</span>, p 0, ethertype IPv4, <strong><span style="color: #ff0000;">172.16.30.1 > 172.16.30.21</span></strong>: ICMP echo request, id 19466, seq 1, length 64
15:38:18.381183 9e:58:72:fa:11:15 > 82:f7:0e:2d:3f:62, ethertype 802.1Q (0x8100), length 102: vlan <span style="color: #ff0000;"><strong>3001</strong></span>, p 0, ethertype IPv4, 172.16.30.21 > 172.16.30.1: ICMP echo reply, id 19466, seq 1, length 64
15:38:19.396796 82:f7:0e:2d:3f:62 > 9e:58:72:fa:11:15, ethertype 802.1Q (0x8100), length 102: vlan 3001, p 0, ethertype IPv4, 172.16.30.1 > 172.16.30.21: ICMP echo request, id 19466, seq 2, length 64
15:38:19.396859 9e:58:72:fa:11:15 > 82:f7:0e:2d:3f:62, ethertype 802.1Q (0x8100), length 102: vlan 3001, p 0, ethertype IPv4, 172.16.30.21 > 172.16.30.1: ICMP echo reply, id 19466, seq 2, length 64
15:38:23.162052 82:f7:0e:2d:3f:62 > 9e:58:72:fa:11:15, ethertype 802.1Q (0x8100), length 102: vlan 3002, p 0, ethertype IPv4, 172.16.30.2 > <strong><span style="color: #ff0000;">172.16.30.22</span></strong>: ICMP echo request, id 19473, seq 1, length 64
15:38:23.162107 9e:58:72:fa:11:15 > 82:f7:0e:2d:3f:62, ethertype 802.1Q (0x8100), length 102: vlan 3002, p 0, ethertype IPv4, <strong><span style="color: #ff0000;">172.16.30.22 > 172.16.30.2</span></strong>: ICMP echo reply, id 19473, seq 1, length 64

如果试图从veth10.3001 去ping 172.16.30.22 是不能通的,因为是不同的vlan呀:


# ping -I veth10.3001 172.16.30.22
PING 172.16.30.22 (172.16.30.22) from 172.16.30.1 veth10.3001: 56(84) bytes of data.
^C
--- 172.16.30.22 ping statistics ---
9 packets transmitted, 0 received, 100% packet loss, time 8231ms

不适用vconfig的解法:


ip link add link veth10 name veth10.3001 type vlan id 3001

另: vlan 一般以  设备名.vlanid 来命名,不过并非强制,如下命名为 vlan3003也是没问题的


# ip link add link veth10 name vlan3003 type vlan id 3003

注意:一个主设备上相同vlan好的子设备最多只能有一个


# ip link add link veth10 name vlan3001 type vlan id 3001
RTNETLINK answers: File exists

所以,正常来讲,一般是这样的:

linux虚拟网络设备之vlan配置详解

参考: http://network.51cto.com/art/201504/473419.htm

来源:https://phpor.net/blog/post/7109

标签:linux,虚拟网络设备,vlan配置
0
投稿

猜你喜欢

  • 我的四年做地方站经历!论坛运营篇

    2007-11-23 12:04:00
  • 对HTTP协议的头信息详解

    2010-05-02 18:04:00
  • 韩国IPTV用户破百万 2012年市场收益将扩大10倍

    2009-10-14 10:46:00
  • 内网IP建ftp服务器教程

    2009-02-12 13:30:00
  • 500万以上的四川赈灾英雄榜(附部分明星捐款)

    2008-05-17 09:53:00
  • 购机走出概念模糊 谈虚拟主机的三种限制

    2009-02-01 08:59:00
  • VMware虚拟机安装CentOS 6.9图文教程

    2023-11-02 14:14:47
  • 识别常见的Web应用安全漏洞

    2008-04-15 11:18:00
  • SNS网站发展之势:在取舍中凸显自我价值

    2008-12-23 10:37:00
  • 怎么做好网站技术重构

    2008-12-08 10:50:00
  • 手把手教你使用 virtualBox 让虚拟机连接网络的教程

    2021-01-08 00:43:32
  • Godaddy:如何设置电子邮件转发

    2010-05-04 13:05:00
  • Linux Autofs自动挂载服务安装部署教程

    2023-08-05 13:50:43
  • IE8与IE7共存的两种方法

    2008-03-17 13:23:00
  • 关于DEDECMS的URL优化的一些心得

    2010-04-24 20:39:00
  • Linux下使用pip安装SpeechRecognition连接超时解决办法

    2023-07-20 22:05:31
  • 新手必读:我的第一笔GG收款105.69美元

    2009-06-02 10:04:00
  • 中小型企业如何打好综合网络营销持久战

    2009-04-07 10:13:00
  • 我家久久:基于意见领袖的口碑营销

    2009-04-24 14:40:00
  • 开源框架 Matrix-Dendrite 搭建聊天服务器的详细过程

    2023-06-04 03:18:09
  • asp之家 网站运营 m.aspxhome.com