利用zephyr搭建ipv6实验环境

Creative Commons
本作品采用知识共享署名

本文介绍如何基于zephyr提供的模拟net环境建立ipv6实验环境。

zephyr提供了net-tools和qemu通讯,建立tap0网卡,可以很方便的用于用于ipv6的实验

net-tools

安装

1
2
3
git clone https://github.com/zephyrproject-rtos/net-tools
cd net-tools
make

问题处理:
make时报错“monitor_15_4.c:34:23: fatal error: pcap/pcap.h: No such file or directory”,未安装libpcap,执行

1
sudo apt-get install libpcap-dev

运行

  • 开启Terminal为#1执行
    1
    ./loop-socat.sh

问题处理:
会一直报“./loop-socat.sh: 31: ./loop-socat.sh: socat: not found”,未安装socat,执行

1
sudo apt-get install socat

  • 开启Terminal为#2执行
    1
    sudo ./loop-slip-tap.sh

此时在Terminal可以看到如下信息,说明虚拟网卡tap0已经建立成功

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
********SLIP started on ``/tmp/slip.dev''
slipfd and inslip reopened
ip neigh flush dev tap0
Cannot find device "tap0"
opened tap device ``/dev/tap0''
ifconfig tap0 up
ip -6 route add 2001:db8::/64 dev tap0
ip -6 addr add 2001:db8::2/64 dev tap0
ip route add 192.0.2.0/24 dev tap0
ip addr add 192.0.2.2/24 dev tap0
ifconfig tap0

tap0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.0.2.2 netmask 255.255.255.0 broadcast 0.0.0.0
inet6 2001:db8::2 prefixlen 64 scopeid 0x0<global>
inet6 fe80::8c6b:33ff:fe41:d663 prefixlen 64 scopeid 0x20<link>
ether 8e:6b:33:41:d6:63 txqueuelen 1000 (以太网)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

can't open siodev ``/tmp/slip.dev''
********SLIP started on ``/tmp/slip.dev''
slipfd and inslip reopened
ip neigh flush dev tap0

Qemu运行

在zephyr设置好的情况下,执行下面命令

1
2
3
4
cd $ZEPHYR_BASE/samples/net/http_server
mkdir build && cd build
cmake -GNinja -DBOARD=qemu_x86 ..
ninja run

启动的Qemu将运行起zephyr,在zephyr shell中执行net iface就可以看见Qemu的ipv6和ipv4地址了,如下
net

  • 在终端当中执行如下命令能正常ping通

    1
    ping6 -I 2001:db8::1
  • 在chrome中输入

    1
    http://[2001:db8::1]/index.html

能看见
ipv6

以上过程中,用wireshark对网卡tap0进行抓包,可以分析ipv6数据,例如ping6的抓包:
ping6

参考

https://docs.zephyrproject.org/latest/subsystems/networking/qemu_setup.html#networking-with-qemu
https://docs.zephyrproject.org/latest/samples/net/http_server/README.html