上个实验中,我们发现新安装的VPP中没有接口,一般是需要像颜老师那样通过DPDK来纳管主机的接口的,但也有一种方式是创建VETH。
在 Linux 中,有一种接口叫做“veth”,像是Windows里面的虚拟网卡,但veth设备是成对出现的,一端连接内核的协议栈,另一端相连VPP的接口。所以,也可以将“veth”接口视为具有两端(而不是一端)的接口。
首先,在内核创建一个veth接口,一端命名为ethvpp,另一端命名为ethhost。
[root@localhost ~]# ip link add name ethvpp type veth peer name ethhost
[root@localhost ~]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:13:d9:6c brd ff:ff:ff:ff:ff:ff
3: ethhost@ethvpp: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether 02:91:21:16:e5:e9 brd ff:ff:ff:ff:ff:ff
4: ethvpp@ethhost: <BROADCAST,MULTICAST,M-DOWN> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether 3e:35:0f:15:74:a3 brd ff:ff:ff:ff:ff:ff
这里我们可以看到接口3和4的名称分别是ethhost@ethvpp和ethvpp@ethhost,说明两个接口是有依赖关系的。
开启veth接口的两端。
ip link set dev ethvpp up
ip link set dev ethhost up
[root@localhost ~]# ip link set dev ethvpp up
[root@localhost ~]# ip link set dev ethhost up
[root@localhost ~]# ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT gr oup default qlen 1000
link/ether 00:0c:29:13:d9:6c brd ff:ff:ff:ff:ff:ff
3: ethhost@ethvpp: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mo de DEFAULT group default qlen 1000
link/ether 02:91:21:16:e5:e9 brd ff:ff:ff:ff:ff:ff
4: ethvpp@ethhost: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP mo de DEFAULT group default qlen 1000
link/ether 3e:35:0f:15:74:a3 brd ff:ff:ff:ff:ff:ff
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default q len 1000
link/ether 00:0c:29:13:d9:6c brd ff:ff:ff:ff:ff:ff
inet 192.168.1.131/24 brd 192.168.1.255 scope global noprefixroute ens192
valid_lft forever preferred_lft forever
inet6 2408:8207:1918:7790:b931:4b6f:5bda:8e59/64 scope global noprefixroute dynamic
valid_lft 258934sec preferred_lft 172534sec
inet6 fe80::f797:19ba:c532:71a4/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: ethhost@ethvpp: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP gr oup default qlen 1000
link/ether 02:91:21:16:e5:e9 brd ff:ff:ff:ff:ff:ff
inet6 fe80::91:21ff:fe16:e5e9/64 scope link
valid_lft forever preferred_lft forever
4: ethvpp@ethhost: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP gr oup default qlen 1000
link/ether 3e:35:0f:15:74:a3 brd ff:ff:ff:ff:ff:ff
inet6 fe80::3c35:fff:fe15:74a3/64 scope link
valid_lft forever preferred_lft forever
可以看到,此时链路和接口均已经UO起来了,但是还没有地址,接下来先在内核中给ethhost分配一个IP地址。
ip addr add 10.1.1.1/24 dev ethhost
ip addr show ethhost
[root@localhost ~]# ip addr add 10.1.1.1/24 dev ethhost
[root@localhost ~]# ip addr show ethhost
3: ethhost@ethvpp: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 02:91:21:16:e5:e9 brd ff:ff:ff:ff:ff:ff
inet 10.1.1.1/24 scope global ethhost
valid_lft forever preferred_lft forever
inet6 fe80::91:21ff:fe16:e5e9/64 scope link
valid_lft forever preferred_lft forever
使用命令vppctl进入VPP shell。
可以看到,VPP中还是没有接口的。接下来,创建一个连接到ethvpp的主机接口。
create host-interface name ethvpp
可以看到,接口ethvpp创建成功,并且硬件状态是UP,但协议状态是DOWN。需要手工使能接口ethvpp。
set interface state host-ethvpp up
接口UP以后,再给接口分配IP地址。
set int ip address host-ethvpp 10.1.1.2/24
确认接口IP地址。
然后ping测试一下。
什么鬼,没有ping命令。再一看,好像命令少的有些可怜。
原来是我只装了基础包的原因。
先装一下plugins插件。
然后重启VPP。
再次进入VPP就有ping命令了,还多了一些其他命令。
正好到这了,捎带看一下安装的扩展包情况。
但是重启之后新增加的接口不见了,重新配一下吧。
神奇不,不能ping通自己,却能ping通veth对端接口地址,查看邻居信息。
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 00:0c:29:13:d9:6c brd ff:ff:ff:ff:ff:ff
inet 192.168.1.131/24 brd 192.168.1.255 scope global noprefixroute ens192
valid_lft forever preferred_lft forever
inet6 2408:8207:1918:7790:b931:4b6f:5bda:8e59/64 scope global noprefixroute dynamic
valid_lft 259187sec preferred_lft 172787sec
inet6 fe80::f797:19ba:c532:71a4/64 scope link noprefixroute
valid_lft forever preferred_lft forever
3: ethhost@ethvpp: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 02:91:21:16:e5:e9 brd ff:ff:ff:ff:ff:ff
inet 10.1.1.1/24 scope global ethhost
valid_lft forever preferred_lft forever
inet6 fe80::91:21ff:fe16:e5e9/64 scope link
valid_lft forever preferred_lft forever
4: ethvpp@ethhost: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether 3e:35:0f:15:74:a3 brd ff:ff:ff:ff:ff:ff
inet6 fe80::3c35:fff:fe15:74a3/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# ping 10.1.1.1
PING 10.1.1.1 (10.1.1.1) 56(84) bytes of data.
64 bytes from 10.1.1.1: icmp_seq=1 ttl=64 time=0.085 ms
64 bytes from 10.1.1.1: icmp_seq=2 ttl=64 time=0.044 ms
64 bytes from 10.1.1.1: icmp_seq=3 ttl=64 time=0.047 ms
--- 10.1.1.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.044/0.058/0.085/0.020 ms
[root@localhost ~]# ping 10.1.1.2
PING 10.1.1.2 (10.1.1.2) 56(84) bytes of data.
64 bytes from 10.1.1.2: icmp_seq=1 ttl=64 time=0.197 ms
64 bytes from 10.1.1.2: icmp_seq=2 ttl=64 time=0.142 ms
64 bytes from 10.1.1.2: icmp_seq=3 ttl=64 time=0.136 ms
--- 10.1.1.2 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.136/0.158/0.197/0.029 ms
而在内核就简单了,两个地址都能通。
再尝试ping一下内核的业务地址,则提示需要指定出接口,但即使带源接口仍不行。
其实就是没有路由而已。
增加一条路由。
ip route add 192.168.1.0/24 via 10.1.1.1
这不就通了吗。