# Kubernetes CNI SRIOV Kernel# 时间:20210825-[20:00-22:00]# 主题:MUTUS SRIOV Kernel# 文档:https://www.yuque.com/docs/share/319c7ebe-26af-42f2-a36a-88fa6eba8eb4?# 《20210825-SRIOV Kernel》# 作业:MULTUS SRIOV Kernel
sr-iov-with-kubernetes-networking-and-observability-technology-guide.pdf
kubernetes-application-note.pdf
SRIOV CNI And SRIOV-Network-Device-Plugin
拓扑逻辑:
SRIOV 基础介绍
# SRIOV CNI:提供SRIOV的能力。SR-IOV 全称 Single Root I/O Virtualization,是 Intel 在 2007年提出的一种基于硬件的虚拟化解决方案。This plugin enables the configuration and usage of SR-IOV VF networks in containers and orchestrators like Kubernetes. //主要的作用//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.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.# SR-IOV 使用 physical functions (PF) 和 virtual functions (VF) 为 SR-IOV 设备管理全局功能。PF 包含SR-IOV 功能的完整PCIe设备,PF 作为普通的PCIe 设备被发现、管理和配置 。PF 通过分配VF 来配置和管理 SR-IOV 功能。禁用SR-IOV后,主机将在一个物理网卡上创建一个 PF。VF 是轻量级 PCIe 功能(I/O 处理)的 PCIe 设备,每个 VF 都是通过 PF 来生成管理的,VF 的具体数量限制受限于 PCIe 设备自身配置及驱动程序的支持,启用SR-IOV后,主机将在一个物理NIC上创建单个PF和多个VF。 VF的数量取决于配置和驱动程序支持。每个 SR-IOV 设备都可有一个 PF(Physical Functions),并且每个 PF 最多可有64,000个与其关联的 VF(Virtual Function)。PF 可以通过寄存器创建 VF,这些寄存器设计有专用于此目的的属性。一旦在 PF 中启用了 SR-IOV,就可以通过 PF 的总线、设备和功能编号(路由 ID)访问各个 VF 的 PCI 配置空间。每个 VF 都具有一个 PCI 内存空间,用于映射其寄存器集。VF设备驱动程序对寄存器集进行操作以启用其功能,并且显示为实际存在的PCI设备。创建 VF 后,可以直接将其指定给虚拟机或各个应用程序。此功能使得虚拟功能可以共享物理设备,并在没有CPU和虚拟机管理程序软件开销的情况下执行 I/O。
SRIOV Kernel Mode
# 所有worker 节点均需要此操作。# 1.安装SRIOV CNI前提准备:# SRI-IOV WorkerNode PrerequisitesIn 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.1.Enable** VT-D** at BIOS level.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.3.Upgrade Kernel boot line to enable intel_iommu=on and iommu=pt$ grubby --update-kernel=ALL --args="iommu=pt intel_iommu=on"$ reboot# Huge page SupportMost network applications use huge pages, so you may want to enable that. Please edit /etc/default/grub and add huge-pagesGRUB_CMDLINE_LINUX="nofb nomodeset vga=normal iommu=pt intel_iommu=on default_hugepagesz=1G hugepagesz=1G hugepages=16"#Rebuild grub.cfggrub2-mkconfig -o /boot/grub2/grub.cfg && reboot# 2. 安装步骤:# git clone https://github.com/k8snetworkplumbingwg/sriov-cni.git# kubectl apply -f images/k8s-v1.16/sriov-cni-daemonset.yaml # 此运行的Pod即为把sriov的bin文件cp到 /opt/cni/bin/下kube-system kube-sriov-cni-ds-amd64-cr2wg 1/1 Running 0 11s 10.244.200.196 k8s-2 <none> <none>kube-system kube-sriov-cni-ds-amd64-k5tln 1/1 Running 0 11s 10.244.231.202 k8s-1 <none> <none>[root@k8s-1 bin]# lltotal 243852-rwxr-xr-x 1 root root 4159518 Aug 22 16:29 bandwidth-rwxr-xr-x. 1 root root 3581192 Sep 10 2020 bridge-rwxr-xr-x 1 root root 36564992 Aug 22 16:29 calico-rwxr-xr-x 1 root root 36564992 Aug 22 16:29 calico-ipam-rwxr-xr-x. 1 root root 9837552 Sep 10 2020 dhcp-rwxr-xr-x. 1 root root 4699824 Sep 10 2020 firewall-rwxr-xr-x 1 root root 3069556 Aug 22 16:29 flannel-rwxr-xr-x. 1 root root 3274160 Sep 10 2020 host-device-rwxr-xr-x 1 root root 3614480 Aug 22 16:29 host-local-rwxr-xr-x 1 root root 36564992 Aug 22 16:29 install-rwxr-xr-x. 1 root root 3377272 Sep 10 2020 ipvlan-rwxr-xr-x 1 root root 3209463 Aug 22 16:29 loopback-rwxr-xr-x. 1 root root 3440168 Sep 10 2020 macvlan-rwxr-xr-x 1 root root 36250611 Aug 22 16:29 multus-rwxr-xr-x 1 root root 3939867 Aug 22 16:29 portmap-rwxr-xr-x. 1 root root 3528800 Sep 10 2020 ptp-rwxr-xr-x. 1 root root 2849328 Sep 10 2020 sbr-rwxr-xr-x 1 root root 3886233 Aug 22 21:13 sriov # this one-rwxr-xr-x. 1 root root 2503512 Sep 10 2020 static-rwxr-xr-x 1 root root 3356587 Aug 22 16:29 tuning-rwxr-xr-x. 1 root root 3377120 Sep 10 2020 vlan-rwxr-xr-x 1 root root 38016417 Aug 22 16:30 whereabouts[root@k8s-1 bin]## 3. 配置SRIVO的VF:# https://github.com/k8snetworkplumbingwg/sriov-network-device-plugin/blob/master/docs/vf-setup.mdecho 0 > /sys/class/net/${PF_NAME}/device/sriov_numvfsecho 8 > /sys/class/net/${PF_NAME}/device/sriov_numvfs# Like:X710 NIC:echo 0 > /sys/class/net/eth2/device/sriov_numvfsecho 32 > /sys/class/net/eth2/device/sriov_numvfs# 4.查看vf:ip link show
SRIOV-Network-Device-Plugin安装
# 1.安装SRIOV-Network-Device-Plugin:准备# https://github.com/k8snetworkplumbingwg/sriov-network-device-pluginSR-IOV Network Device PluginThe SR-IOV Network Device Plugin is Kubernetes device plugin for discovering and advertising SR-IOV virtual functions (VFs) available on a Kubernetes host.Features~ Handles SR-IOV capable/not-capable devices (NICs and Accelerators alike)~ Supports devices with both Kernel and userspace (UIO and VFIO) drivers~ Allows resource grouping using "Selector"~ User configurable resourceName~ Detects Kubelet restarts and auto-re-register~ Detects Link status (for Linux network devices) and updates associated VFs health accordingly~ Extensible to support new device types with minimal effort if not already supported~ Works within virtual deployments of Kubernetes that do not have virtualized-iommu support (VFIO No-IOMMU support)To deploy workloads with SR-IOV VF this plugin needs to work together with the following two CNI components:# Any CNI meta plugin supporting Device Plugin based network provisioning (Multus CNI, or DANM)Retrieves allocated network device information of a Pod# SR-IOV CNIDuring Pod creation, plumbs allocated SR-IOV VF to a Pods network namespace using VF information given by the meta pluginOn Pod deletion, reset and release the VF from the PodPlease follow the Quick Start for multi network interface support in Kubernetes.# Supported SR-IOV NICsThe following NICs were tested with this implementation. However, other SR-IOV capable NICs should work as well.Intel® E800 SeriesIntel® X700 SeriesIntel® 82599ESMellanox ConnectX-4®Mellanox Connectx-4® Lx EN AdapterMellanox ConnectX-5®Mellanox ConnectX-5® ExMellanox ConnectX-6®Mellanox ConnectX-6® Dx# 2.安装SRIOV-Network-Device-Plugin:# git clone https://github.com/k8snetworkplumbingwg/sriov-network-device-plugin.git# Deploy config mapCreate a ConfigMap that defines SR-IOV resource pool configurationMake 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.$ kubectl create -f deployments/configMap.yaml# 现在这里给出一个configMap的YAML文件:]# cat configMap.yamlapiVersion: v1kind: ConfigMapmetadata:name: sriovdp-confignamespace: kube-systemdata:config.json: |{"resourceList": [{"resourceName": "sriov_kern_1","selectors":{"vendors": ["8086"],"devices": ["154c"],"drivers": ["i40evf"],"pfNames": ["eth4"]}},{"resourceName": "sriov_kern_2","selectors":{"vendors": ["8086"],"devices": ["154c"],"drivers": ["i40evf"],"pfNames": ["eth5"]}},{"resourceName": "sriov_kern_3","selectors":{"vendors": ["8086"],"devices": ["154c"],"drivers": ["i40evf"],"pfNames": ["eth6"]}},{"resourceName": "sriov_kern_4","selectors":{"vendors": ["8086"],"devices": ["154c"],"drivers": ["i40evf"],"pfNames": ["eth7"]}}]}## Deploy daemonset$ kubectl create -f deployments/k8s-v1.16/sriovdp-daemonset.yamlFor K8s version v1.15 or earlier use deployments/k8s-v1.10-v1.15/sriovdp-daemonset.yaml instead.[root@ovs k8s-v1.16]# kubectl apply -f sriovdp-daemonset.yamlserviceaccount/sriov-device-plugin createddaemonset.apps/kube-sriov-device-plugin-amd64 createddaemonset.apps/kube-sriov-device-plugin-ppc64le createddaemonset.apps/kube-sriov-device-plugin-arm64 created[root@ovs k8s-v1.16]#kube-system kube-sriov-device-plugin-amd64-75vdw 1/1 Running 0 4m10s 172.12.1.11 k8s-1 <none> <none>kube-system kube-sriov-device-plugin-amd64-gdr87 1/1 Running 0 41s 172.12.1.12 k8s-2 <none> <none># 3. 查看节点的能力:# kubectl describe nodes k8s-1Capacity:cpu: 88ephemeral-storage: 3900337424Kihugepages-1Gi: 64Giintel.com/sriov_kern_3: 16intel.com/sriov_kern_4: 16intel.com/sriov_kern_1: 32intel.com/sriov_kern_2: 32memory: 791009936Kipods: 200Allocatable:cpu: 80ephemeral-storage: 3594550964007hugepages-1Gi: 64Giintel.com/sriov_kern_3: 16 # 这里的数量由此决定:echo 16 > /sys/class/net/${PF_NAME}/device/sriov_numvfsintel.com/sriov_kern_4: 16 # 这里的数量由此决定:echo 32 > /sys/class/net/${PF_NAME}/device/sriov_numvfsintel.com/sriov_kern_1: 32intel.com/sriov_kern_2: 32memory: 723798672Kipods: 200#此时该节点就拥有了SRIOV Kernel的能力,并且有自己的VF的数量的显示。接下来就是如何使用该Resources去创建Pod了。
Multus加载SRIOV Kernel
[root@k8s-1 sriov]# cat *.yaml# Network attachment defination template:[下边会专门针对SRIOV的NAD做介绍]apiVersion: "k8s.cni.cncf.io/v1"kind: NetworkAttachmentDefinitionmetadata:name: sriov-kernelnet0annotations:k8s.v1.cni.cncf.io/resourceName: intel.com/sriov_kern_1spec:config: '{"type": "sriov","cniVersion": "0.3.1","name": "sriov-kernelnet0","spoofchk": "off","type": "sriov","vlan": 2310,"ipam": {"type": "whereabouts","range": "172.172.5.0/24","range_start": "172.172.5.20","range_end": "172.172.5.30"}}'# Pod resource template:apiVersion: v1kind: Podmetadata:name: pod-demo-sriov-kernelannotations:k8s.v1.cni.cncf.io/networks: sriov-kernelnet0spec:containers:- name: pod0-case-03image: centos/tools:v1imagePullPolicy: IfNotPresentcommand:- /sbin/initresources:requests:cpu: "4"memory: "8Gi"intel.com/sriov_kern_1: '1'limits:cpu: "4"memory: "8Gi"intel.com/sriov_kern_1: '1'[root@k8s-1 sriov]## Pod的信息:# kubectl exec -it pod-demo-sriov-kernel bashkubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.[root@pod-demo-sriov-kernel /]# ifconfigeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1440inet 10.20.45.179 netmask 255.255.255.255 broadcast 0.0.0.0ether 86:ee:aa:75:19:1a txqueuelen 0 (Ethernet)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0loop txqueuelen 1000 (Local Loopback)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0net1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.172.5.20 netmask 255.255.255.0 broadcast 172.172.5.255ether 06:07:38:e1:8e:11 txqueuelen 1000 (Ethernet)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0[root@pod-demo-sriov-kernel /]#
SRIOV NAD(network-attach-defination)
# 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 networktype(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 requiresvlanfield to be set to a non-zero value. Otherwise, the error will be returned.mac(string, optional): MAC address to assign for the VFspoofchk(string, optional): turn packet spoof checking on or off for the VFtrust(string, optional): turn trust setting on or off for the VFlink_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,i40eis known to work butigbdoesn’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:
{"cniVersion": "0.3.1","name": "sriov-dpdk","type": "sriovi-net","deviceID": "0000:03:02.0","vlan": 1000,"mac": "CA:FE:C0:FF:EE:00","vlanQoS": 4,"min_tx_rate": 100,"max_tx_rate": 200,"spoofchk": "off","trust": "on","link_state": "enable"}
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:
apiVersion: v1kind: Podmetadata:name: samplepodannotations:k8s.v1.cni.cncf.io/networks: '[{"name": "sriov-net","mac": "CA:FE:C0:FF:EE:00"}]'spec:containers:- name: runTimeConfigcommand: ["/bin/bash", "-c", "sleep 300"]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.
