关于本文

此文为了解决我的无线网卡Intel(R) Dual Band Wireless AC 7265, REV=0x210的问题,WiFi经常断网,只要一出现断网就必须进行好几次的重启才可以让我的Arch Linux系统正常加载7265D网卡驱动。自从换了个笔记本这问题已经困扰我半年了,此问题差点导致我从一个Linux爱好者变为果粉(过分),没办法MacOS实在是太方便了,但是7月份我已经从大学毕业,进入工作岗位,那时在Mac上为了搭建Java项目的环境,只为做一个Mysql两个版本的兼容,折腾的还是回到了Arch Linux,不过无线网卡问题依旧,虽然已经解决我的多MySql兼容(使用Docker),这使我更加坚信作为一个Linux爱好者能为生产带来更高的效率以及便利。当然,可能有人不同意这个说法,但如果你使用Linux足够熟练,确实会发现在Linux环境下做开发比MacOS更适合!!

切换到Linux上来又有一个新的问题,那就是之前使用Arch Linux其实也永不到超过16G的运行内存,当时换内存的时候直接就删除了Swap分区(听网上说内存大就没必要使用Swap,等…),但进入工作岗位企业项目部署到我的Arch Linux 上后才发现系统非常的卡,卡的几乎不能控制鼠标!但是,还是太过相信网上的瞎结论,其次坚信Arch 非常强大,可能是中了什么挖矿脚本,于是在刚开启的时候就使用top命令监控程序行为。令我意外的是竟然真的出现了一个程序占满了CPU,一个名为kswapd0的程序出现在top监控范围!就刻意的搜索到了这样一篇文章kswapd0挖矿病毒的发现与清除,一看这个kswapd0程序的名字就知道,估计和Swap有关系,于是找资料查看此程序到底是个什么作用,在国外找到了一篇这样的文章kswapd0 is taking a lot of cpu最后才明白原来不是什么挖矿脚本,只是缺少了Swap分区导致,添加上Swap分区就解决了以上问题。

不过网卡问题依旧存在,折腾时间估计加起来能有两周了,一直在和问题做斗争,终于在第二周还是解决了这个最棘手的问题,因为过程中这个7265的网卡修好一次坏掉,修好一又坏掉!!反反复复折腾好几次,因为在Linux Wireless:iwlwifi网站上看到Intel官网已经不更新这个网卡在Linux上的驱动固件了,看国内文章没啥技术水平,只能去国外的Linux论坛上寻求问题的解决方案,但结果只有一些变通的方法,我一致认为7265D网卡只能认灾,只想骂Intel。当我只能硬改无线网卡或者买Linux系统支持的USB网卡 时(我已经买了!!!),通过Linux Wireless:Existing Linux Wireless drivers 文章根据厂商驱动挑选适配Linux的网卡设备,由于为什么要买后期会出专门一个这样的文章。正好由于网卡和Swap问题恰好在有了一次交集,就是我在修复我一致认为i的驱动问题的时候又不小心把休眠到Swap功能给搞没了(即:Linux电脑的休眠功能),又翻了一遍 Arch WiKi:Power management/Suspend and hibernate,在修复的过程中执行mkinitcpio -P出现了以往并不注意的==> WARNING: Possibly missing firmware for module: xxx,心理一想我的电脑问题最近开机报错挺多(网卡驱动报错也在其中),就想着搜索一下这些报错,并根据 Arch WiKi:mkinitcpio 提示安装了报错所需要的软件包,重启电脑,网卡竟然可以正常工作了,不过在测试过程中还是相对于之前没有WiFi问题的Arch笔记本来说还是存在一些小的Bug,不过还是解决了使用7265D网卡的大部分痛点,下文就是修复此网卡的所有过程了,尽情欣赏。

参考:

Network configuration/Wireless

udev

udev takes an inventory of your hardware and loads appropriate modules (drivers) for your corresponding hardware, which will in turn allow creation of a network interface.

列出硬件清单,并为所有硬件加载对应的驱动,过程将允许创建网络接口。

udev加载可能存在的问题

If the proper module is not loaded by udev on boot, simply load it manually. If udev loads more than one driver for a device, the resulting conflict may prevent successful configuration. Make sure to blacklist the unwanted module.

如果Arch启动时udev没有加载正确的模块,就需要手动加载。如果加载多个设备的驱动,可能会产生冲突并阻止加载。确保不需要加载的硬件放入黑名单。

检查设备状态

To check if the driver for your card has been loaded, check the output of the lspci -k or lsusb -v command, depending on if the card is connected by PCI(e) or USB.

如果你的驱动已加硬件,可通过lspci -k / lsusb -v命令显示:

  • lspci -k 显示pci设备加载情况
  • lsusb -v 显示usb设备加载情况(包:usbutils

If the card is a USB device, running dmesg | grep usbcore as root should give something like usbcore: registered new interface driver rtl8187 as output.

如果你的网卡为usb设备,dmesg | grep usbcore命令可以输出(例):

1
usbcore: registered new interface driver rtl8187

Also check the output of the ip link command to see if a wireless interface was created; usually the naming of the wireless network interfaces starts with the letter “w”, e.g. wlan0 or wlp2s0. Then bring the interface up with:

还要使用命令ip link 检查 无限接口是否被创建,通常无线接口名称为w开头。

可使用此命令,开启接口:

1
ip link set interface up

通常输出:

检查驱动是否被加载:

1
2
dmesg | grep firmware
[ 7.148259] iwlwifi 0000:02:00.0: loaded firmware version 39.30.4.1 build 35138 op_mode iwldvm

如果没有输出:

1
2
# 将会加载 iwlwifi 相关加载日志
dmesg | grep iwlwifi

linux-firmware

Many firmware images are provided by the linux-firmware package; however, proprietary firmware images are not included and have to be installed separately. This is described in #Installing driver/firmware.

大多数硬件linux-firmware已经提供,但专有硬件的驱动还需要自行安装。

安装驱动/固件

安装前建议查看,其中包括Linux无线设备驱动,和驱动支持的硬件: existing Linux wireless drivers

参考:

Linux Wireless:iwlwifi

NetSpot:无线安全协议: WEP, WPA, WPA2, 和 WPA3

关于我的设备

我的无线网卡设备是Intel(R) Dual Band Wireless AC **7265**, REV=0x210,上述链接找到iwlwifi驱动(Intel家的),且iwlwifi需要加载7265D的驱动 *-29.ucode,由于官网是这么说的

The revision number of a 7265D device is 0x210, if you see any other number, you have a 7265 device.

且非常重要的一句话是:

Those devices will not be supported by the newest firmware versions: the last firmware that was released for 3160, 7260 and 7265 is -17.ucode. Bug fixes will be ported to -17.ucode. 7265D, 3165 and 3168’s latest firmware version is -29.ucode.

这些设备将不被支持最新的固件版本,为3160,7260和7265发布的最后一个固件是-17. code。Bug修复将被移植到-17.ucode。7265D, 3165和3168的最新固件版本是-29.ucode。

等于说我的设备7265D不再支持更新驱动!!

所以我一致认为我的Wifi不停的断网是这个设备固件随着Linux更新而使得7265D这个设备固件越来越不兼容导致的问题。

网络接口管理

the wireless ones are controlled with ip from the iproute2 package.

Managing a wireless connection requires a basic set of tools. Either use a network manager or use one of the following directly:

管理无线连接需要一套基本的工具,使用网络 network manager或者使以下:

Software Package WEXT nl80211 WEP WPA/WPA2 Archiso [1]
wireless_tools1 wireless_tools Yes No Yes No Yes
iw iw No Yes Yes No Yes
wpa_supplicant wpa_supplicant Yes Yes No Yes Yes
iwd iwd No Yes No Yes Yes

iw and wireless_tools comparison

两个常用工具常用命令比较。

iw command wireless_tools command Description
iw dev wlan0 link iwconfig wlan0 Getting link status.
iw dev wlan0 scan iwlist wlan0 scan Scanning for available access points.
iw dev wlan0 set type ibss iwconfig wlan0 mode ad-hoc Setting the operation mode to ad-hoc.
iw dev wlan0 connect your_essid iwconfig wlan0 essid your_essid Connecting to open network.
iw dev wlan0 connect your_essid 2432 iwconfig wlan0 essid your_essid freq 2432M Connecting to open network specifying channel.
iw dev wlan0 connect your_essid key 0:your_key iwconfig wlan0 essid your_essid key your_key Connecting to WEP encrypted network using hexadecimal key.
iwconfig wlan0 essid your_essid key s:your_key Connecting to WEP encrypted network using ASCII key.
iw dev wlan0 set power_save on iwconfig wlan0 power on Enabling power save.

不过建议如果使用命令行工具,建议的顺序:iwd > iw > wireless_tools,我用的是前两个。

扫描接入点

If it displays Interface does not support scanning, then you probably forgot to install the firmware. In some cases this message is also displayed when not running iw as root.

如果不支持扫描,你可能忘记安装相应的固件。还有一些原因是没有使用root权限运行iw

1
iw dev interface scan | less

Tip: Depending on your location, you might need to set the correct regulatory domain in order to see all available networks.

取决于你的地区,你可能需要设置一下,才可以。

无线管理领域设置

设置管理领域作用在于,每个国家有每个国家对WiFi功率或者频段的一个标准,当然我们可以改变这个标准在我们的Linux机器上,比如设置无线设备的功率,这个将会扫描到更多的无线网络或开启热点时让更多的设备可以进行发现,提升无线网络的稳定性。

参考:

Arch Linux WiKi:Wireless

In the 2.4GHz band, the allowed channels are 1-11 for the US, 1-14 for Japan, and 1-13 for most of the rest of the world. In the 5GHz band, the rules for allowed channels are much more complex.

在2.4GHz频段,美国允许的频道为1-11,日本为1-14,世界其他大部分地区为1-13。在5GHz频段,允许的信道规则要复杂得多。

这段我个人觉得5G WiFi频段的功率要低于2.4G:

This is derived from transmit power/“tx power”, and is measured in dBm/mBm (1dBm=100mBm) or mW (log scale). In the 2.4GHz band, the maximum is 30dBm in the US and Canada, 20dBm in most of Europe, and 20dBm-30dBm for the rest of the world. In the 5GHz band, maximums are usually lower.

这是由发射功率/“发送功率”导出的,以dBm/mBm (1dBm=100mBm)或mW(对数尺度)测量。在2.4GHz频段,美国和加拿大的最大值为30dBm,欧洲大部分地区为20dBm,世界其他地区为20dBm-30dBm。在5GHz频段,最大值通常较低。

改变信道来在信道拥挤的情况下使用比如更稳定的2.4G频段,但是可能会影响其他无线设备,比如使用这个功能将CN使用的信道范围改为JP信道的使用范围。从而避免信道拥挤造成的2.4G不稳定

Misconfiguring the regdomain can be useful - for example, by allowing use of an unused channel when other channels are crowded, or by allowing an increase in tx power to widen transmitter range. However, this is not recommended as it could break local laws and cause interference with other radio devices.

错误配置regdomain可能是有用的-例如,允许在其他信道拥挤时使用未使用的信道,或允许增加tx功率以扩大发射机范围。然而,这是不建议的,因为它可能违反当地法律,并导致干扰其他无线电设备。

如果你想在Linux机器上方便的改变信道或者无线设备发送功率,你需要安装wireless-regdb包。

临时的改变无线领域:

1
iw reg set US

查看当前领域:

1
2
3
4
iw reg get
global
country US: DFS-FCC
...

查看信道 功率是否改变成功:

但我的显示还是CN的功率和信道,我估计没作用,主要还是在于敢于测试。

1
iw list | grep -A 15 Frequencies:

A more permanent configuration of the regdomain can be achieved through editing /etc/conf.d/wireless-regdom and uncommenting the appropriate domain.

可以通过编辑 /etc/conf.d/wireless-regdom 并取消注释相应的域来实现更永久的 regdomain 配置。

设置无线网卡工作模式

You might need to set the proper operating mode of the wireless card. More specifically, if you are going to connect an ad-hoc network, you need to set the operating mode to ibss:

你可能需要设置合适的操作模式为无线网卡,具体的说,你需要连接一个ad-hoc network(理解成游戏对战时候,各个支持ad-hoc的游戏机互相连接所使用的协议),你就需要设置工作模式为ibss:

1
iw dev interface set type ibss

故障排除

This section contains general troubleshooting tips, not strictly related to problems with drivers or firmware. For such topics, see next section #Troubleshooting drivers and firmware.

此部分包含一般的故障排除提示,与驱动或固件不相关,想看更多,请看下一个环节。

临时上网方式

你的Arch可能先要安装usb_modeswitch,装好后Arch将具备可以将连接手机的USB硬盘模式自动转换为网络解调器模式,才可以共享手机网络。

If you have problematic hardware and need internet access to, for example, download some software or get help in forums, you can make use of Android’s built-in feature for internet sharing via USB cable. See Android tethering#USB tethering for more information.

如果你有硬件问题并且需要接入互联网。例如,下载一些软件或者向论坛提供求助,你可以先使用Android设备USB共享网络给你的设备,详见Android tethering#USB tethering

查看日志

A good first measure to troubleshoot is to analyze the system’s logfiles first. In order not to manually parse through them all, it can help to open a second terminal/console window and watch the kernels messages with.

一种很好的排错方式就是分析系统的日志文件。帮助你打开第二个终端/窗口并查看内核消息:

1
dmesg -w

When using a tool for network management, the same can be done for systemd with

当你使用网络管理工具,你就可以用下面的方式输出:

1
journalctl -f 

Frequently a wireless error is accompanied by a deauthentication with a particular reason code, for example:

通常一个无线错误往往伴随着一个特殊的代码,例如:

1
wlan0: deauthenticating from XX:XX:XX:XX:XX:XX by local choice (reason=3)

Looking up the reason code might give a first hint. Maybe it also helps you to look at the control message flowchart, the journal messages will follow it.

查找原因代码可能会给出第一个提示。也许它还可以帮助您查看控制消息流程图,日志消息将遵循它。

发现问题

由于之前解决了没有Swap导致Arch Linux卡顿的问题,同时也实现了机器的休眠功能。在找WiFi网络问题的过程中,在我搜索的众多解决方案中看到一个改善此问题的变通方法,来自一篇论坛问题 askubuntu-garzanti:Intel Dual Band Wireless 7265 dropping connection ,大部分外国网友的解决方案其实都是出自Arch WiKi:Network configuration/Wireless,比如其中配置/etc/modprobe.d/iwlwifi.conf禁用掉无线802.11n协议,或者配置关闭蓝牙与WiFi兼容。然而这些配置对于我来说根本毫无作用。又由于之前Swap休眠功能不小心又被我玩坏了,查看一遍 Arch WiKi:Power management/Suspend and hibernate 才知道是搞反了两条命令的顺序,mkinitcpio -Pgrub-mkconfig -o /boot/grub/grub.cfg, 而在执行mkinitcpio -P命令时候过程报出6条WARNING: Possibly missing firmware for module: xxx,上网一查才知道原来是缺少驱动导致的问题,根据 Arch WiKi:mkinitcpio 提示安装了报错所需要的固件软件包,至此这样的一个顽固的问题才被解决。

以下内容只包含解决此标题的问题的操作,关于Swap休眠原理以及实现方式,详细查看 铸鼎_:Arch Linux “挖矿脚本kswapd0清除” 背后的秘密

由以下报错发现问题所在是缺少固件导致的问题:

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
27
28
29
30
31
32
33
34
35
36
37
38
➜  ~ mkinitcpio -P
==> Building image from preset: /etc/mkinitcpio.d/linux-lts.preset: 'default'
-> -k /boot/vmlinuz-linux-lts -c /etc/mkinitcpio.conf -g /boot/initramfs-linux-lts.img
==> Starting build: 5.15.61-1-lts
-> Running build hook: [base]
-> Running build hook: [udev]
-> Running build hook: [autodetect]
-> Running build hook: [modconf]
-> Running build hook: [block]
==> WARNING: Possibly missing firmware for module: xhci_pci
-> Running build hook: [keyboard]
-> Running build hook: [filesystems]
-> Running build hook: [resume]
-> Running build hook: [fsck]
==> Generating module dependencies
==> Creating zstd-compressed initcpio image: /boot/initramfs-linux-lts.img
==> Image generation successful
==> Building image from preset: /etc/mkinitcpio.d/linux-lts.preset: 'fallback'
-> -k /boot/vmlinuz-linux-lts -c /etc/mkinitcpio.conf -g /boot/initramfs-linux-lts-fallback.img -S autodetect
==> Starting build: 5.15.61-1-lts
-> Running build hook: [base]
-> Running build hook: [udev]
-> Running build hook: [modconf]
-> Running build hook: [block]
# ==> WARNING: Possibly missing firmware for module: wd719x
# ==> WARNING: Possibly missing firmware for module: qla2xxx
# ==> WARNING: Possibly missing firmware for module: bfa
# ==> WARNING: Possibly missing firmware for module: qla1280
# ==> WARNING: Possibly missing firmware for module: qed
# ==> WARNING: Possibly missing firmware for module: aic94xx
# ==> WARNING: Possibly missing firmware for module: xhci_pci
-> Running build hook: [keyboard]
-> Running build hook: [filesystems]
-> Running build hook: [resume]
-> Running build hook: [fsck]
==> Generating module dependencies
==> Creating zstd-compressed initcpio image: /boot/initramfs-linux-lts-fallback.img
==> Image generation successful

个人认为以上的缺少固件不应该只是WARNING报错,应该标记成ERROR,毕竟缺少固件确实有时候会导致非常令人头疼的问题,比如我的7265D网卡。

根据 Arch WiKi:mkinitcpio解决方案安装相应的固件包:

When initramfs are being rebuild after a kernel update, you might get warnings such as:

当内核更新后重建 initramfs 时可能会收到警告:

1
2
3
==> WARNING: Possibly missing firmware for module: wd719x
==> WARNING: Possibly missing firmware for module: aic94xx
==> WARNING: Possibly missing firmware for module: xhci_pci

If these or similar messages appear when generating a default initramfs image, then, as the warning says, installing additional firmware may be required. Most common firmware files can be acquired by installing the linux-firmware package. For other packages providing firmware see the table below or try searching for the module name in the official repositories or AUR.

如果在生成默认的initramfs映像时出现这些或类似的消息,那么,正如警告所说,可能需要安装额外的固件。大多数常见的固件文件可以通过安装linux-firmware包来获得。对于提供固件的其他包,请参阅下表,或尝试在官方存储库或AUR中搜索模块名称。

Otherwise, if the messages only appear when generating the fallback initramfs image you have the following options:

否则,如果消息仅在生成后备 initramfs 映像时出现,您有以下选项:

  • You can safely ignore the warnings, if you know that you do not use the affected hardware.

    如果您知道您没有使用受影响的硬件,那么您可以放心地忽略警告。

  • If you want to suppress the warnings, you can install the missing firmware. The meta-package mkinitcpio-firmwareAUR contains most optional firmwares. Alternatively, manually install the needed packages:

    如果您想消除警告,可以安装缺失的固件。元包mkinitcpio-firmwareAUR包含了大多数可选的固件。或者,手动安装所需的包:

Module Package
aic94xx aic94xx-firmwareAUR
bfa linux-firmware-qlogic
bnx2x linux-firmware-bnx2x
liquidio linux-firmware-liquidio
mlxsw_spectrum linux-firmware-mellanox
nfp linux-firmware-nfp
qat_4xxx Firmware is not yet available.
qed linux-firmware-qlogic
qla1280 linux-firmware-qlogic
qla2xxx linux-firmware-qlogic
wd719x wd719x-firmwareAUR
xhci_pci upd72020x-fwAUR

根据以上说明,一个是在生成default initramfs image阶段,一个是在生成fallback initramfs image阶段,当然这两个镜像的生成也是有先后顺序的,具体请查看 Arch WiKi:mkinitcpio

根据上述安装缺失的固件包:

  • default initramfs image阶段缺少 xhci_pci,根据==> WARNING: Possibly missing firmware for module: xhci_pci
  • fallback initramfs image阶段缺少 wd719x qla2xxx bfa qla1280 qed aic94xx xhci_pci

根据步骤,查询安装所需软件包:

1
yay -S upd72020x-fw linux-firmware-qlogic upd72020x-fw wd719x-firmware aic94xx-firmware

再生成一遍mkinitcpio:

1
mkinitcpio -P

以上未报错,直接Success.

为了保险起见,重新生成了一次grub配置:

1
grub-mkconfig -o /boot/grub/grub.cfg

重启,问题解决!!