1. # Kubernetes CNI SRIOV Kernel
  2. # 时间:20210825-[20:00-22:00]
  3. # 主题:MUTUS SRIOV Kernel
  4. # 文档:https://www.yuque.com/docs/share/319c7ebe-26af-42f2-a36a-88fa6eba8eb4?# 《20210825-SRIOV Kernel》
  5. # 作业:MULTUS SRIOV Kernel

sr-iov-with-kubernetes-networking-and-observability-technology-guide.pdf

kubernetes-application-note.pdf

SRIOV CNI And SRIOV-Network-Device-Plugin

拓扑逻辑:

DPDK-MULTUS-SRIOV.jpg

SRIOV 基础介绍

  1. # SRIOV CNI:提供SRIOV的能力。SR-IOV 全称 Single Root I/O Virtualization,是 Intel 在 2007年提出的一种基于硬件的虚拟化解决方案。
  2. This plugin enables the configuration and usage of SR-IOV VF networks in containers and orchestrators like Kubernetes. //主要的作用//
  3. Network Interface Cards (NICs) with SR-IOV capabilities are managed through physical functions (PFs) and virtual functions (VFs). A PF is used by the host and usually represents a single NIC port. VF configurations are applied through the PF. With SR-IOV CNI each VF can be treated as a separate network interface, assigned to a container, and configured with it's own MAC, VLAN IP and more.
  4. SR-IOV CNI plugin works with SR-IOV device plugin for VF allocation in Kubernetes. A metaplugin such as Multus gets the allocated VF's deviceID(PCI address) and is responsible for invoking the SR-IOV CNI plugin with that deviceID.
  5. # SR-IOV 使用 physical functions (PF) 和 virtual functions (VF) 为 SR-IOV 设备管理全局功能。
  6. PF 包含SR-IOV 功能的完整PCIe设备,PF 作为普通的PCIe 设备被发现、管理和配置 。PF 通过分配VF 来配置和管理 SR-IOV 功能。禁用SR-IOV后,主机将在一个物理网卡上创建一个 PF。
  7. VF 是轻量级 PCIe 功能(I/O 处理)的 PCIe 设备,每个 VF 都是通过 PF 来生成管理的,VF 的具体数量限制受限于 PCIe 设备自身配置及驱动程序的支持,启用SR-IOV后,主机将在一个物理NIC上创建单个PF和多个VF。 VF的数量取决于配置和驱动程序支持。
  8. 每个 SR-IOV 设备都可有一个 PF(Physical Functions),并且每个 PF 最多可有64,000个与其关联的 VF(Virtual Function)。PF 可以通过寄存器创建 VF,这些寄存器设计有专用于此目的的属性。一旦在 PF 中启用了 SR-IOV,就可以通过 PF 的总线、设备和功能编号(路由 ID)访问各个 VF 的 PCI 配置空间。
  9. 每个 VF 都具有一个 PCI 内存空间,用于映射其寄存器集。VF设备驱动程序对寄存器集进行操作以启用其功能,并且显示为实际存在的PCI设备。创建 VF 后,可以直接将其指定给虚拟机或各个应用程序。此功能使得虚拟功能可以共享物理设备,并在没有CPU和虚拟机管理程序软件开销的情况下执行 I/O。

SRIOV Kernel Mode

  1. # 所有worker 节点均需要此操作。
  2. # 1.安装SRIOV CNI前提准备:
  3. # SRI-IOV WorkerNode Prerequisites
  4. In order for the cni-sriov-plugin to start reading Virtual Functions resources as well HostNetworkTemplate objects can configure virtual functions you need to perform the following steps.
  5. 1.Enable** VT-D** at BIOS level.
  6. 2.Enable SRIOV feature globally and/or per nic according to the nic you are working with, for example Intel x710 should be enabled per nic at BIOS level.
  7. 3.Upgrade Kernel boot line to enable intel_iommu=on and iommu=pt
  8. $ grubby --update-kernel=ALL --args="iommu=pt intel_iommu=on"
  9. $ reboot
  10. # Huge page Support
  11. Most network applications use huge pages, so you may want to enable that. Please edit /etc/default/grub and add huge-pages
  12. GRUB_CMDLINE_LINUX="nofb nomodeset vga=normal iommu=pt intel_iommu=on default_hugepagesz=1G hugepagesz=1G hugepages=16"
  13. #Rebuild grub.cfg
  14. grub2-mkconfig -o /boot/grub2/grub.cfg && reboot
  15. # 2. 安装步骤:
  16. # git clone https://github.com/k8snetworkplumbingwg/sriov-cni.git
  17. # kubectl apply -f images/k8s-v1.16/sriov-cni-daemonset.yaml # 此运行的Pod即为把sriov的bin文件cp到 /opt/cni/bin/下
  18. kube-system kube-sriov-cni-ds-amd64-cr2wg 1/1 Running 0 11s 10.244.200.196 k8s-2 <none> <none>
  19. kube-system kube-sriov-cni-ds-amd64-k5tln 1/1 Running 0 11s 10.244.231.202 k8s-1 <none> <none>
  20. [root@k8s-1 bin]# ll
  21. total 243852
  22. -rwxr-xr-x 1 root root 4159518 Aug 22 16:29 bandwidth
  23. -rwxr-xr-x. 1 root root 3581192 Sep 10 2020 bridge
  24. -rwxr-xr-x 1 root root 36564992 Aug 22 16:29 calico
  25. -rwxr-xr-x 1 root root 36564992 Aug 22 16:29 calico-ipam
  26. -rwxr-xr-x. 1 root root 9837552 Sep 10 2020 dhcp
  27. -rwxr-xr-x. 1 root root 4699824 Sep 10 2020 firewall
  28. -rwxr-xr-x 1 root root 3069556 Aug 22 16:29 flannel
  29. -rwxr-xr-x. 1 root root 3274160 Sep 10 2020 host-device
  30. -rwxr-xr-x 1 root root 3614480 Aug 22 16:29 host-local
  31. -rwxr-xr-x 1 root root 36564992 Aug 22 16:29 install
  32. -rwxr-xr-x. 1 root root 3377272 Sep 10 2020 ipvlan
  33. -rwxr-xr-x 1 root root 3209463 Aug 22 16:29 loopback
  34. -rwxr-xr-x. 1 root root 3440168 Sep 10 2020 macvlan
  35. -rwxr-xr-x 1 root root 36250611 Aug 22 16:29 multus
  36. -rwxr-xr-x 1 root root 3939867 Aug 22 16:29 portmap
  37. -rwxr-xr-x. 1 root root 3528800 Sep 10 2020 ptp
  38. -rwxr-xr-x. 1 root root 2849328 Sep 10 2020 sbr
  39. -rwxr-xr-x 1 root root 3886233 Aug 22 21:13 sriov # this one
  40. -rwxr-xr-x. 1 root root 2503512 Sep 10 2020 static
  41. -rwxr-xr-x 1 root root 3356587 Aug 22 16:29 tuning
  42. -rwxr-xr-x. 1 root root 3377120 Sep 10 2020 vlan
  43. -rwxr-xr-x 1 root root 38016417 Aug 22 16:30 whereabouts
  44. [root@k8s-1 bin]#
  45. # 3. 配置SRIVO的VF:
  46. # https://github.com/k8snetworkplumbingwg/sriov-network-device-plugin/blob/master/docs/vf-setup.md
  47. echo 0 > /sys/class/net/${PF_NAME}/device/sriov_numvfs
  48. echo 8 > /sys/class/net/${PF_NAME}/device/sriov_numvfs
  49. # Like:X710 NIC:
  50. echo 0 > /sys/class/net/eth2/device/sriov_numvfs
  51. echo 32 > /sys/class/net/eth2/device/sriov_numvfs
  52. # 4.查看vf:
  53. ip link show

SRIOV-Network-Device-Plugin安装

  1. # 1.安装SRIOV-Network-Device-Plugin:准备
  2. # https://github.com/k8snetworkplumbingwg/sriov-network-device-plugin
  3. SR-IOV Network Device Plugin
  4. The SR-IOV Network Device Plugin is Kubernetes device plugin for discovering and advertising SR-IOV virtual functions (VFs) available on a Kubernetes host.
  5. Features
  6. ~ Handles SR-IOV capable/not-capable devices (NICs and Accelerators alike)
  7. ~ Supports devices with both Kernel and userspace (UIO and VFIO) drivers
  8. ~ Allows resource grouping using "Selector"
  9. ~ User configurable resourceName
  10. ~ Detects Kubelet restarts and auto-re-register
  11. ~ Detects Link status (for Linux network devices) and updates associated VFs health accordingly
  12. ~ Extensible to support new device types with minimal effort if not already supported
  13. ~ Works within virtual deployments of Kubernetes that do not have virtualized-iommu support (VFIO No-IOMMU support)
  14. To deploy workloads with SR-IOV VF this plugin needs to work together with the following two CNI components:
  15. # Any CNI meta plugin supporting Device Plugin based network provisioning (Multus CNI, or DANM)
  16. Retrieves allocated network device information of a Pod
  17. # SR-IOV CNI
  18. During Pod creation, plumbs allocated SR-IOV VF to a Pods network namespace using VF information given by the meta plugin
  19. On Pod deletion, reset and release the VF from the Pod
  20. Please follow the Quick Start for multi network interface support in Kubernetes.
  21. # Supported SR-IOV NICs
  22. The following NICs were tested with this implementation. However, other SR-IOV capable NICs should work as well.
  23. Intel® E800 Series
  24. Intel® X700 Series
  25. Intel® 82599ES
  26. Mellanox ConnectX-4®
  27. Mellanox Connectx-4® Lx EN Adapter
  28. Mellanox ConnectX-5®
  29. Mellanox ConnectX-5® Ex
  30. Mellanox ConnectX-6®
  31. Mellanox ConnectX-6® Dx
  32. # 2.安装SRIOV-Network-Device-Plugin:
  33. # git clone https://github.com/k8snetworkplumbingwg/sriov-network-device-plugin.git
  34. # Deploy config map
  35. Create a ConfigMap that defines SR-IOV resource pool configuration
  36. Make sure to update the 'config.json' entry in the configMap data to reflect your resource configuration for the device plugin. See Configurations section for supported configuration parameters.
  37. $ kubectl create -f deployments/configMap.yaml
  38. # 现在这里给出一个configMap的YAML文件:
  39. ]# cat configMap.yaml
  40. apiVersion: v1
  41. kind: ConfigMap
  42. metadata:
  43. name: sriovdp-config
  44. namespace: kube-system
  45. data:
  46. config.json: |
  47. {
  48. "resourceList": [
  49. {
  50. "resourceName": "sriov_kern_1",
  51. "selectors":
  52. {
  53. "vendors": ["8086"],
  54. "devices": ["154c"],
  55. "drivers": ["i40evf"],
  56. "pfNames": ["eth4"]
  57. }
  58. },
  59. {
  60. "resourceName": "sriov_kern_2",
  61. "selectors":
  62. {
  63. "vendors": ["8086"],
  64. "devices": ["154c"],
  65. "drivers": ["i40evf"],
  66. "pfNames": ["eth5"]
  67. }
  68. },
  69. {
  70. "resourceName": "sriov_kern_3",
  71. "selectors":
  72. {
  73. "vendors": ["8086"],
  74. "devices": ["154c"],
  75. "drivers": ["i40evf"],
  76. "pfNames": ["eth6"]
  77. }
  78. },
  79. {
  80. "resourceName": "sriov_kern_4",
  81. "selectors":
  82. {
  83. "vendors": ["8086"],
  84. "devices": ["154c"],
  85. "drivers": ["i40evf"],
  86. "pfNames": ["eth7"]
  87. }
  88. }
  89. ]
  90. }
  91. #
  92. # Deploy daemonset
  93. $ kubectl create -f deployments/k8s-v1.16/sriovdp-daemonset.yaml
  94. For K8s version v1.15 or earlier use deployments/k8s-v1.10-v1.15/sriovdp-daemonset.yaml instead.
  95. [root@ovs k8s-v1.16]# kubectl apply -f sriovdp-daemonset.yaml
  96. serviceaccount/sriov-device-plugin created
  97. daemonset.apps/kube-sriov-device-plugin-amd64 created
  98. daemonset.apps/kube-sriov-device-plugin-ppc64le created
  99. daemonset.apps/kube-sriov-device-plugin-arm64 created
  100. [root@ovs k8s-v1.16]#
  101. kube-system kube-sriov-device-plugin-amd64-75vdw 1/1 Running 0 4m10s 172.12.1.11 k8s-1 <none> <none>
  102. kube-system kube-sriov-device-plugin-amd64-gdr87 1/1 Running 0 41s 172.12.1.12 k8s-2 <none> <none>
  103. # 3. 查看节点的能力:
  104. # kubectl describe nodes k8s-1
  105. Capacity:
  106. cpu: 88
  107. ephemeral-storage: 3900337424Ki
  108. hugepages-1Gi: 64Gi
  109. intel.com/sriov_kern_3: 16
  110. intel.com/sriov_kern_4: 16
  111. intel.com/sriov_kern_1: 32
  112. intel.com/sriov_kern_2: 32
  113. memory: 791009936Ki
  114. pods: 200
  115. Allocatable:
  116. cpu: 80
  117. ephemeral-storage: 3594550964007
  118. hugepages-1Gi: 64Gi
  119. intel.com/sriov_kern_3: 16 # 这里的数量由此决定:echo 16 > /sys/class/net/${PF_NAME}/device/sriov_numvfs
  120. intel.com/sriov_kern_4: 16 # 这里的数量由此决定:echo 32 > /sys/class/net/${PF_NAME}/device/sriov_numvfs
  121. intel.com/sriov_kern_1: 32
  122. intel.com/sriov_kern_2: 32
  123. memory: 723798672Ki
  124. pods: 200
  125. #此时该节点就拥有了SRIOV Kernel的能力,并且有自己的VF的数量的显示。
  126. 接下来就是如何使用该Resources去创建Pod了。

Multus加载SRIOV Kernel

  1. [root@k8s-1 sriov]# cat *.yaml
  2. # Network attachment defination template:[下边会专门针对SRIOV的NAD做介绍]
  3. apiVersion: "k8s.cni.cncf.io/v1"
  4. kind: NetworkAttachmentDefinition
  5. metadata:
  6. name: sriov-kernelnet0
  7. annotations:
  8. k8s.v1.cni.cncf.io/resourceName: intel.com/sriov_kern_1
  9. spec:
  10. config: '{
  11. "type": "sriov",
  12. "cniVersion": "0.3.1",
  13. "name": "sriov-kernelnet0",
  14. "spoofchk": "off",
  15. "type": "sriov",
  16. "vlan": 2310,
  17. "ipam": {
  18. "type": "whereabouts",
  19. "range": "172.172.5.0/24",
  20. "range_start": "172.172.5.20",
  21. "range_end": "172.172.5.30"
  22. }
  23. }'
  24. # Pod resource template:
  25. apiVersion: v1
  26. kind: Pod
  27. metadata:
  28. name: pod-demo-sriov-kernel
  29. annotations:
  30. k8s.v1.cni.cncf.io/networks: sriov-kernelnet0
  31. spec:
  32. containers:
  33. - name: pod0-case-03
  34. image: centos/tools:v1
  35. imagePullPolicy: IfNotPresent
  36. command:
  37. - /sbin/init
  38. resources:
  39. requests:
  40. cpu: "4"
  41. memory: "8Gi"
  42. intel.com/sriov_kern_1: '1'
  43. limits:
  44. cpu: "4"
  45. memory: "8Gi"
  46. intel.com/sriov_kern_1: '1'
  47. [root@k8s-1 sriov]#
  48. # Pod的信息:
  49. # kubectl exec -it pod-demo-sriov-kernel bash
  50. kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
  51. [root@pod-demo-sriov-kernel /]# ifconfig
  52. eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1440
  53. inet 10.20.45.179 netmask 255.255.255.255 broadcast 0.0.0.0
  54. ether 86:ee:aa:75:19:1a txqueuelen 0 (Ethernet)
  55. RX packets 0 bytes 0 (0.0 B)
  56. RX errors 0 dropped 0 overruns 0 frame 0
  57. TX packets 0 bytes 0 (0.0 B)
  58. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  59. lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
  60. inet 127.0.0.1 netmask 255.0.0.0
  61. loop txqueuelen 1000 (Local Loopback)
  62. RX packets 0 bytes 0 (0.0 B)
  63. RX errors 0 dropped 0 overruns 0 frame 0
  64. TX packets 0 bytes 0 (0.0 B)
  65. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  66. net1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
  67. inet 172.172.5.20 netmask 255.255.255.0 broadcast 172.172.5.255
  68. ether 06:07:38:e1:8e:11 txqueuelen 1000 (Ethernet)
  69. RX packets 0 bytes 0 (0.0 B)
  70. RX errors 0 dropped 0 overruns 0 frame 0
  71. TX packets 0 bytes 0 (0.0 B)
  72. TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
  73. [root@pod-demo-sriov-kernel /]#

SRIOV NAD(network-attach-defination)

  1. # SRIOV NAD主要参数介绍:

Configuration reference - SR-IOV CNI

The SR-IOV CNI configures networks through a CNI spec configuration object. In a Kubernetes cluster set up with Multus this object is most often delivered as a Network Attachment Definition.

Parameters

  • name (string, required): the name of the network
  • type (string, required): “sriov”
  • ipam (dictionary, optional): IPAM configuration to be used for this network.
  • deviceID (string, required): A valid pci address of an SRIOV NIC’s VF. e.g. “0000:03:02.3”
  • vlan (int, optional): VLAN ID to assign for the VF. Value must be in the range 0-4094 (0 for disabled, 1-4094 for valid VLAN IDs).
  • vlanQoS (int, optional): VLAN QoS to assign for the VF. Value must be in the range 0-7. This option requires vlan field to be set to a non-zero value. Otherwise, the error will be returned.
  • mac (string, optional): MAC address to assign for the VF
  • spoofchk (string, optional): turn packet spoof checking on or off for the VF
  • trust (string, optional): turn trust setting on or off for the VF
  • link_state (string, optional): enforce link state for the VF. Allowed values: auto, enable, disable. Note that driver support may differ for this feature. For example, i40e is known to work but igb doesn’t.
  • min_tx_rate (int, optional): change the allowed minimum transmit bandwidth, in Mbps, for the VF. Setting this to 0 disables rate limiting. The min_tx_rate value should be <= max_tx_rate. Support of this feature depends on NICs and drivers.
  • max_tx_rate (int, optional): change the allowed maximum transmit bandwidth, in Mbps, for the VF.
    Setting this to 0 disables rate limiting.

An SR-IOV CNI config with each field filled out looks like:

  1. {
  2. "cniVersion": "0.3.1",
  3. "name": "sriov-dpdk",
  4. "type": "sriovi-net",
  5. "deviceID": "0000:03:02.0",
  6. "vlan": 1000,
  7. "mac": "CA:FE:C0:FF:EE:00",
  8. "vlanQoS": 4,
  9. "min_tx_rate": 100,
  10. "max_tx_rate": 200,
  11. "spoofchk": "off",
  12. "trust": "on",
  13. "link_state": "enable"
  14. }

Runtime Configuration

The SR-IOV CNI accepts a MAC address when passed as a runtime configuration - that is as part of a Kubernetes Pod spec. An example pod with a runtime configuration is:

  1. apiVersion: v1
  2. kind: Pod
  3. metadata:
  4. name: samplepod
  5. annotations:
  6. k8s.v1.cni.cncf.io/networks: '[
  7. {
  8. "name": "sriov-net",
  9. "mac": "CA:FE:C0:FF:EE:00"
  10. }
  11. ]'
  12. spec:
  13. containers:
  14. - name: runTimeConfig
  15. command: ["/bin/bash", "-c", "sleep 300"]
  16. image: centos/tools

The above config will configure a VF of type “sriov-net” with the MAC address configured as the value supplied under the ‘k8s.v1.cni.cncf.io/networks’. Where the MAC address supplied is invalid the container may be created with an unexpected address.

To avoid this it’s key to ensure the supplied MAC is valid for the specified interface. On some systems setting a Multicast MAC address (Where the least significant bit of the first octet is ‘1’) results in failure to set the MAC address.