Linux USB实现网络共享

usb 网络共享

两个Linux设备之间实现USB网络共享,类似Android手机开启USB网络共享的功能。其中一台设备为USB Host,另外一台为USB Device。Device使用的USB接口必须为USB Slave,否则无法正常工作。使用RNDIS驱动,还能够通过USB与Windows实现网络共享。使用的协议主要是Ethernet over USB

参考链接:

https://lishiwen4.github.io/network/cdc-and-rndis

https://www.cnblogs.com/zhuangquan/p/15428253.html

https://developer.ridgerun.com/wiki/index.php/How_to_use_USB_device_networking

https://www.51cto.com/article/533323.html

1. USB Host配置

请添加图片描述

重新编译烧录内核。

2. USB Device配置

请添加图片描述

重新编译烧录内核。

启动之后,会有一张usb0网卡。

3. 网络测试

这里是用imx6ull做Device,hi3518ev200做Host,通过USB连接线连接两个设备。注意:从机设备要使用USB OTG口,否则无法识别。 连接后,主机设备也将出现usb0网卡。并出现内核打印,表明使用的是EEM驱动。

usb 1-1.1: new high-speed USB device number 3 using hiusb-ehci
cdc_eem 1-1.1:1.0: usb0: register 'cdc_eem' at usb-hiusb-ehci-1.1, CDC EEM Device, 06:86:fc:8d:90:41

这里设置主机和从机在同一网段。

Host:

ifconfig usb0 192.168.50.100 up

Device:

ifconfig usb0 192.168.50.101 up 

测试:

# ping 192.168.50.100
PING 192.168.50.100 (192.168.50.100): 56 data bytes
64 bytes from 192.168.50.100: seq=0 ttl=64 time=1.509 ms
64 bytes from 192.168.50.100: seq=1 ttl=64 time=0.756 ms
64 bytes from 192.168.50.100: seq=2 ttl=64 time=0.793 ms
64 bytes from 192.168.50.100: seq=3 ttl=64 time=0.644 ms
^C
--- 192.168.50.100 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 0.644/0.925/1.509 ms
# ping 192.168.50.101
PING 192.168.50.101 (192.168.50.101) 56(84) bytes of data.
64 bytes from 192.168.50.101: icmp_seq=1 ttl=64 time=0.687 ms
64 bytes from 192.168.50.101: icmp_seq=2 ttl=64 time=0.644 ms
64 bytes from 192.168.50.101: icmp_seq=3 ttl=64 time=0.753 ms
64 bytes from 192.168.50.101: icmp_seq=4 ttl=64 time=0.645 ms
64 bytes from 192.168.50.101: icmp_seq=5 ttl=64 time=0.621 ms

--- 192.168.50.101 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 4003ms
rtt min/avg/max/mdev = 0.621/0.670/0.753/0.046 ms

3.1 取消Device的EEM

重新编译Device的内核。Host在连接USB后,内核将出现以下打印,表明使用的CDC Ethernet驱动。

usb 1-1.1: new high-speed USB device number 5 using hiusb-ehci
cdc_ether 1-1.1:1.0: usb0: register 'cdc_ether' at usb-hiusb-ehci-1.1, CDC Ethernet Device, e6:58:40:39:1f:4d

配置Host和Device的usb0网卡,并进行ping测试,网络也是OK的。

3.2 修改Device Gadget Driver

做如下修改,最后Host端识别到的还是CDC Ethernet,网络也是通的。

请添加图片描述

3.3 Host勾选CDC Ethernet Subset请添加图片描述

勾选这个驱动,Host端识别到设备是Linux Device,虽然Host和Device都能生成usb0网卡,但是在配置IP后,ping测试不通。

 usb 1-1.1: new high-speed USB device number 6 using hiusb-ehci
cdc_subset 1-1.1:1.0: usb0: register 'cdc_subset' at usb-hiusb-ehci-1.1, Linux Device, 2a:15:5c:f7:a6:e2

遗憾的是,还是没有将RNDIS使用起来。在内核中,无论是配置RNDIS Host,还是RNDIS Device,驱动的依赖都是CDC Ethernet,因此默认都是使用CDC Ethernet。估计只有让Windows来作为Host才能使用上RNDIS了。

IPv6: ADDRCONF(NETDEV_UP): eth1: link is not ready
MAC Address = e0:e1:a9:97:ad:be
IPv6: ADDRCONF(NETDEV_UP): wlan0: link is not ready
fec 20b4000.ethernet eth0: Link is Up - 100Mbps/Full - flow control rx/tx
IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
random: nonblocking pool is initialized
R8188EU: ERROR assoc success
IPv6: ADDRCONF(NETDEV_CHANGE): wlan0: link becomes ready
g_ether gadget: high-speed config #2: RNDIS
g_ether gadget: high-speed config #2: RNDIS

果然,使用USB线连接Windows后,Device使用的驱动是RNDIS。但是由于Windows上没有安装对应的驱动,但是没有出现网络适配器。奇怪的是,设备管理器出现的是串口设备,并没有出现网络设备。

猜你喜欢

转载自blog.csdn.net/duapple/article/details/128679984
今日推荐