【玩转开源】BananaPi R2 —— 第二篇 Openwrt 网口配置分析
上次和大家分享了如何烧录和安装Openwrt到BananaPi R2,运行Openwrt的R2目前就具备路由器的功能了,这次我们来看看R2运行Openwrt的性能如何,同时也会讲解一些常用的网络知识。
首先看一下硬件上的设计:
可以看出硬件上,R2设计了一个wan口和4个lan口;
这里可能有人会有疑问,什么是wan口,什么是lan口?
wan(Wide Area Network)表示广域网,lan(Local Area Network)表示局域网;简单的讲就是wan口是连接Internet上网用的,lan口是R2的局域网口。
接下来,我们再来看一下软件,执行命令“ifconfig”
root@LEDE:/# ifconfig br-lan Link encap:Ethernet HWaddr 6A:2D:BB:A5:19:51 inet addr:192.168.1.1 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) eth0 Link encap:Ethernet HWaddr 6A:2D:BB:A5:19:51 inet6 addr: fe80::682d:bbff:fea5:1951/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:17 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:1834 (1.7 KiB) Interrupt:211 eth1 Link encap:Ethernet HWaddr DA:65:D4:6A:B6:96 inet6 addr: fe80::d865:d4ff:fe6a:b696/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:187 errors:0 dropped:0 overruns:0 frame:0 TX packets:44 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:61769 (60.3 KiB) TX bytes:4958 (4.8 KiB) Interrupt:211 lan0 Link encap:Ethernet HWaddr 6A:2D:BB:A5:19:51 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) lan1 Link encap:Ethernet HWaddr 6A:2D:BB:A5:19:51 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) lan2 Link encap:Ethernet HWaddr 6A:2D:BB:A5:19:51 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) lan3 Link encap:Ethernet HWaddr 6A:2D:BB:A5:19:51 UP BROADCAST MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:0 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:0 (0.0 B) TX bytes:0 (0.0 B) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 inet6 addr: ::1/128 Scope:Host UP LOOPBACK RUNNING MTU:65536 Metric:1 RX packets:28 errors:0 dropped:0 overruns:0 frame:0 TX packets:28 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1 RX bytes:2364 (2.3 KiB) TX bytes:2364 (2.3 KiB) wan Link encap:Ethernet HWaddr DA:65:D4:6A:B6:96 inet addr:192.168.2.144 Bcast:192.168.2.255 Mask:255.255.255.0 inet6 addr: fe80::d865:d4ff:fe6a:b696/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:187 errors:0 dropped:0 overruns:0 frame:0 TX packets:37 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:58403 (57.0 KiB) TX bytes:3774 (3.6 KiB)
命令执行完后可以看到R2有1个wan,4个lan,1个网桥br-lan,2个网卡eth0,eth1,1个本地环回lo网口;
这里注意看每个网口的HWaddr即mac地址,可以发现wan的HWaddr与eth1是一样的,br-lan,lan0~lan3的HWaddr与eth0是一样的,由此可初步断定,wan口是由网卡eth1生成的虚拟网卡,br-lan和lan0~lan3则是由eth0生成的;接下来再查看一下网口配置文件 "/etc/config/network" 来确定网口是如何配置的,是否和我们的初步判断是一致的;
root@LEDE:/# cat /etc/config/network config interface 'loopback' option ifname 'lo' option proto 'static' option ipaddr '127.0.0.1' option netmask '255.0.0.0' config interface 'lan' option type 'bridge' option ifname 'lan0 lan1 lan2 lan3' option proto 'static' option ipaddr '192.168.1.1' option netmask '255.255.255.0' option ip6assign '60' config interface 'wan' option ifname 'wan' option proto 'dhcp'
从配置文件可以看出,一共定义了3个网口 loopback,lan,wan;lan的类型(option type)是网桥(bridge),一共定义了4个虚拟网口(option ifname 'lan0 lan1 lan2 lan3'),采用静态IP(option proto 'static');wan口就是wan,IP采用动态分配(dhcp)的方式。
如果修改了/etc/config/network文件,可以执行:“/etc/init.d/network reload” 使其生效。
如果想了解更多/etc/config/network,可查看这里:https://openwrt.org/docs/guide-user/base-system/basic-networking
这里留个问题,这里的网络配置是如何与网卡驱动对应起来的呢?思考一下这个问题,后续分析代码的时候会讲解到。
接下来我们来评测一下R2性能的好坏,那么如何去检测呢?这里引申一个概念benchmark,那么什么是benchmark,顾名思义就是基准的意思,而Linux有丰富的工具来测试benchmark,这里我们测试网络就需要用到其中一个工具“iperf”;不解释过多,先简单感受一下;用“iperf”测试网口通信性能,需要两台设备处于同一个局域网,一台作为server,一台作为client,这里我用PC作为server端,R2作为client端,测试R2 wan的网口性能:
- 创建server端
jack@jack: iperf3 -s #创建服务器端 ----------------------------------------------------------- Server listening on 5201 -----------------------------------------------------------
- 在R2上创建client端,测试TCP的传输速率
root@LEDE:/# iperf3 -c 192.168.2.148 #PC server的IP为192.168.2.148 Connecting to host 192.168.2.148, port 5201 [ 5] local 192.168.2.166 port 49034 connected to 192.168.2.148 port 5201 [ ID] Interval Transfer Bitrate Retr Cwnd [ 5] 0.00-1.00 sec 66.5 MBytes 557 Mbits/sec 0 331 KBytes [ 5] 1.00-2.00 sec 65.2 MBytes 547 Mbits/sec 0 331 KBytes [ 5] 2.00-3.00 sec 65.6 MBytes 550 Mbits/sec 0 331 KBytes [ 5] 3.00-4.00 sec 65.4 MBytes 548 Mbits/sec 0 331 KBytes [ 5] 4.00-5.00 sec 65.2 MBytes 547 Mbits/sec 0 331 KBytes [ 5] 5.00-6.00 sec 65.3 MBytes 548 Mbits/sec 0 331 KBytes [ 5] 6.00-7.00 sec 65.4 MBytes 548 Mbits/sec 0 331 KBytes [ 5] 7.00-8.00 sec 65.7 MBytes 551 Mbits/sec 0 331 KBytes [ 5] 8.00-9.00 sec 65.4 MBytes 549 Mbits/sec 0 331 KBytes [ 5] 9.00-10.00 sec 65.3 MBytes 548 Mbits/sec 0 331 KBytes - - - - - - - - - - - - - - - - - - - - - - - - - [ ID] Interval Transfer Bitrate Retr [ 5] 0.00-10.00 sec 655 MBytes 549 Mbits/sec 0 sender [ 5] 0.00-10.00 sec 654 MBytes 549 Mbits/sec receiver
- 在R2上创建client端,测试UDP的传输速率
root@LEDE:/# iperf3 -u -c 192.168.2.148 Connecting to host 192.168.2.148, port 5201 [ 5] local 192.168.2.166 port 43646 connected to 192.168.2.148 port 5201 [ ID] Interval Transfer Bitrate Total Datagrams [ 5] 0.00-1.00 sec 129 KBytes 1.05 Mbits/sec 91 [ 5] 1.00-2.00 sec 127 KBytes 1.04 Mbits/sec 90 [ 5] 2.00-3.00 sec 129 KBytes 1.05 Mbits/sec 91 [ 5] 3.00-4.00 sec 127 KBytes 1.04 Mbits/sec 90 [ 5] 4.00-5.00 sec 129 KBytes 1.05 Mbits/sec 91 [ 5] 5.00-6.00 sec 129 KBytes 1.05 Mbits/sec 91 [ 5] 6.00-7.00 sec 127 KBytes 1.04 Mbits/sec 90 [ 5] 7.00-8.00 sec 129 KBytes 1.05 Mbits/sec 91 [ 5] 8.00-9.00 sec 127 KBytes 1.04 Mbits/sec 90 [ 5] 9.00-10.00 sec 129 KBytes 1.05 Mbits/sec 91 - - - - - - - - - - - - - - - - - - - - - - - - - [ ID] Interval Transfer Bitrate Jitter Lost/Total Datagrams [ 5] 0.00-10.00 sec 1.25 MBytes 1.05 Mbits/sec 0.000 ms 0/906 (0%) sender [ 5] 0.00-10.00 sec 1.25 MBytes 1.05 Mbits/sec 0.016 ms 0/906 (0%) receiver
这里看出R2的wan口,TCP的速率大约为549Mbits/s,速度性能初步去看不能算特别好,后续分析一下代码的时候,我们再回来看这个问题,是什么原因导致的。
Openwrt相关资源:https://openwrt.org/
欢迎大家批评指正,转载请注明出处,多谢。