---
title: 网卡绑定CPU
description: "- 本节内容在每次服务器重启后，都需要重新执行。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/2300/armnative/kunpengcpskbox_20_0050.html
sourcePath: /source/zh/kunpengboostkithistory/2300/armnative/kunpengcpskbox_20_0050.html
indexId: b72b24e3015d38222e24dfc265685c02f11840b8625068121f76a6f91aef5e8876
---
# 网卡绑定CPU

- 本节内容在每次服务器重启后，都需要重新执行。
- 服务器上使用多张网卡时，每张网卡均需要进行如下步骤确认。


网络服务占用的CPU与容器绑定的CPU重叠时，会造成容器内CPU资源异常。为了避免这种情况出现，请将流量较大、负载较重的网卡绑定至空闲CPU。

1. 执行如下命令，查看网卡pci设备号。本文以网卡enp125s0f1为例进行说明。
  1 ethtool -i enp125s0f1 | grep bus-info | awk '{print $2}'

  回显如下所示，表示网卡enp125s0f1的pci设备号为0000:7d:00.1。

  1 0000:7d:00.1

2. 执行如下命令，查询网卡涉及的中断。
  命令中的${id_pci}为1中查到的网卡设备号。 1 cat /proc/interrupts | grep "${id_pci}" | awk -F: '{print $1}'

  回显如下所示，表示网卡enp125s0f1对应的中断为358、359。

  1 2 358 359

  若查询到网卡涉及的中断比较多，则需要判断中断是否分散地绑在不同CPU上，根据判断结果来确定是否修改中断绑定的CPU。

3. 查询中断绑定在哪个CPU上，命令中的${break_value}为查询到的网卡中断号。
```
cat /proc/irq/${break_value}/smp_affinity_list
```


  - 若中断是分散地绑在不同CPU上，且分散后网卡中断绑定的CPU与容器绑定的CPU不冲突，则可跳过本小节后续内容，不进行任何操作。
  - 若其中大部分集中在同一个CPU上或需要将占用CPU过高网卡中断绑定到空闲CPU上，请参见    4
和    5
，将其绑定在预留的CPU（优先网卡所属的NUMAnode CPU）上。
4. 根据网卡的pci设备号，查看网卡所属的NUMA node。
  命令中的${id_pci}为网卡设备号，可通过本节内容的1进行查看。执行命令，回显中的NUMA node参数对应的值即为网卡所属的NUMA node。 1 lspci -vvvs ${id_pci}

  回显如下所示，根据pci设备号查询得到的网卡enp125s0f1所属的NUMA node为0。

  1 2 3 4 5 6 7 8 7d:00.1 Ethernet controller: Huawei Technologies Co., Ltd. HNS GE/10GE/25GE Network Controller (rev 21) Control: I/O- Mem+ BusMaster+ SpecCycle- MemWINV- VGASnoop- ParErr- Stepping- SERR- FastB2B- DisINTx- Status: Cap+ 66MHz- UDF- FastB2B- ParErr- DEVSEL=fast >TAbort- <TAbort- <MAbort- >SERR- <PERR- INTx- Latency: 0 NUMA node: 0 Region 0: Memory at 121040000 (64-bit, prefetchable) [size=64K] Region 2: Memory at 120400000 (64-bit, prefetchable) [size=1M] Capabilities: [40] Express (v2) Endpoint, MSI 00

5. 网卡中断绑定至预留CPU（优先网卡所属的NUMA node CPU）上。
命令中的${break_1}、${break_2}依次为两个网卡中断的值。

  - 将中断${break_1}绑定至1 CPU。
    1 echo 1 > /proc/irq/${break_1}/smp_affinity_list

  - 将中断${break_2}绑定至2 CPU。
    1 echo 2 > /proc/irq/${break_2}/smp_affinity_list

以网卡enp125s0f1为例，它对应的中断为358、359，绑定命令依次为：
```
echo 1 > /proc/irq/358/smp_affinity_list
echo 2 > /proc/irq/359/smp_affinity_list
```

查询得到网卡所属的NUMA node后，NUMA node对应的core区间可执行如下命令查看。

```
lscpu
```

回显如下示例所示，网卡enp125s0f1所属的NUMA node为0，其对应的core区间为0~23。

```
Architecture:                    aarch64
CPU op-mode(s):                  64-bit
Byte Order:                      Little Endian
CPU(s):                          96
On-line CPU(s) list:             0-95
Thread(s) per core:              1
Core(s) per socket:              48
Socket(s):                       2
NUMA node(s):                    2
Vendor ID:                       0x48
Model:                           0
Stepping:                        0x1
BogoMIPS:                        200.00
L1d cache:                       6 MiB
L1i cache:                       6 MiB
L2 cache:                        48 MiB
L3 cache:                        128 MiB
NUMA node0 CPU(s):               0-23
NUMA node1 CPU(s):               24-47
NUMA node2 CPU(s):               48-71
NUMA node3 CPU(s):               72-95
```
