1. ## Kubernetes CNI Cilium
  2. # 时间:20210829-[14:00-17:00]
  3. # 主题:eBPF XDP TC
  4. # 文档:https://www.yuque.com/docs/share/2791eeef-26b8-4f62-9dda-9bef4f112ee4?# 《20210903-eBPF XDP TC介绍》
  5. # 作业:eBPF XDP TC
  1. # FAQ:

eBPF Overview

  1. # eBPF 是一项革命性技术,它能在内核中运行沙箱程序(sandbox programs), 而无需修改内核源码或者加载内核模块。
  2. eBPF的一个重要特性是能够使用高级语言(如C)来实现程序。LLVM有一个eBPF后端,用于编辑包含eBPF指令的ELF文件,前端(如clang)可以用于生成程序。
  3. 在一个后端转换为字节码后,使用bpf()系统调用加载bpf程序,并校验安全性。JIT会将字节码编译进CPU架构中,并将该程序附加到内核对象上,当这些对象发生事件时会触发程序的执行(例如,当从一个网络接口发送报文时)。

20210903-eBPF XDP TC介绍 - 图1

  1. # eBPF work flow:
  2. 1.我们可通过诸如 C, Go ,Python 等等高级语言BPF相关的逻辑。
  3. 2.Clang/LLVM 为eBPF提供了一个 编译后端,能从C 源码直接生成eBPF 字节码(bytecode)
  4. 3.由于此字节码需要注入到内核空间,所以就需要对其做逻辑检查,避免出现恶意,和过于复杂的逻辑,和能导致内核Creash的逻辑
  5. 4.代码及时编译到cpu架构
  6. 5.注入到期望的hook中,然后就可以使用此bpf 的逻辑处理进入后边协议栈之前的数据包。

eBPF Map

image.png

  1. # eBPF Map:
  2. eBPF使用的主要的数据结构是eBPF map,这是一个通用的数据结构,用于在内核或内核和用户空间传递数据。其名称"map"也意味着数据的存储和检索需要用到key。
  3. 我们可以在CIlium中查看这些Map信息:
  4. # 为什么需要BPF Map
  5. 通过消息传递来触发程序中的行为是软件工程中广泛使用的技术。一个程序可以通过发送消息来修改另一个程序的行为,这也允许这些程序之间通过这个方式来传递信息。
  6. 关于BPF最吸引人的一个方面,就是运行在内核上的程序可以在运行时使用消息传递相互通信,我称之为「communication on air」。
  7. 而BPF Map就是用户空间和内核空间之间的数据交换、信息传递的桥梁。
  8. # BPF Map是什么
  9. BPF Map本质上是以「键/值」方式存储在内核中的数据结构,它们可以被任何知道它们的BPF程序访问。在内核空间的程序创建BPF Map并返回对应的文件描述符,在用户空间运行的程序就可以通过这个文件描述符来访问并操作BPF Map,这就是为什么BPF Map在BPF世界中是桥梁的存在了。
  10. # BPF Map类型
  11. 根据申请内存方式的不同,BPF Map有很多种类型(具体的列表可以看之前的博客文章),常用的类型是BPF_MAP_TYPE_HASH和BPF_MAP_TYPE_ARRAY,它们背后的内存管理方式跟我们熟悉的哈希表和数组基本一致,此外还有包括BPF_MAP_TYPE_PROG_ARRAY、BPF_MAP_TYPE_PERF_EVENT_ARRAY等10余种Map类型,具体可以查看之前的博文。随着多CPU架构的成熟发展,BPF Map也引入了per-cpu类型,如BPF_MAP_TYPE_PERCPU_HASH、BPF_MAP_TYPE_PERCPU_ARRAY等,当你使用这种类型的BPF Map时,每个CPU都会存储并看到它自己的Map数据,从属于不同CPU之间的数据是互相隔离的,这样做的好处是,在进行查找和聚合操作时更加高效,性能更好,尤其是你的BPF程序主要是在做收集时间序列型数据,如流量数据或指标等。

网络数据收发原理

  1. 网络相关的(子系统),但对于存储和其他系统,问题都是类似的。我在图 中列出了对这些子系统进行操作所需的工具。例如,
  2. 配置以太网驱动或者网络设备需要使用 ethtool 命令
  3. 配置路由使用 ip 命令
  4. 配置过滤使用 seccom 命令
  5. 配置 IP 防火墙使用 iptables 命令,但如果你使用的是 raw sockets,那有很多地方都 会 bypass,因此这并不是一个完整的防火墙
  6. 配置流量整形使用 tc 命令
  7. 抓包使用 tcpdump 命令,但同样的,它并没有展示出全部信息,因为它只关注了一层
  8. 如果有虚拟交换机,那使用 brctl 或 ovsctl
  9. 所以我们看到,每个子系统都有自己的 API,这意味着如果要自动化这些东西,必须单独的 使用这些工具。有一些工具这样做了,但这种方式意味着我们需要了解其中的每一层。

处理过程Overview

image.png

  1. 该图描述的一个Linux中数据转发原理层-原理图:
  2. ---------------------------------------
  3. 用户态 |----> 应用层 ----> HTTP FTP
  4. ---------------------------------------
  5. 内核态 |----> 传输层 ----> TCP UDP
  6. 协议栈 |----> 网络层 ----> IP ICMP IGMP
  7. 驱动 |----> 链路层 ----> 网络设备驱动
  8. ---------------------------------------
  9. 硬件 |----> 物理层 ----> 网卡 网线
  10. # 具体的逻辑为:
  11. # 接收流程:
  12. 1.数据包达到物理网卡(RX FIFO),通过DMA到内存中。内存指的是网卡的接收的Ring Buffer。
  13. 2.并且拷贝成一个一个的sk_buffer.
  14. 3.触发硬中断,通知CPU,已经有数据来了,CPU根据注册的中断函数,中断函数调用驱动程序,驱动先禁用网卡的中断,目的下次再来数据就直接处理就可以,就不再通知CPU的硬中断。
  15. 4.弥补硬中断处理时间问题,需要启用一个软中断。(主要是弥补硬中断处理时间不及的问题)
  16. 5.数据单元的sk_buffer然后再交给我们的协议栈处理。实际上就是交给网络层和传输层来处理。[被ip层协议和传输层协议处理]
  17. 6.去处掉网络层和传输层的头以后,CPU就把数据Copy到用户空间的应用程序。

内核单元层级关系

image.png
image.png

  1. # 这里主要是需要弄清楚netfilter所处的位置。
  2. BPF XDP ---> sk_buffer ---> TC[network stack Dividing Line] ---> IPv4 And IPv6 ---> Netfilter ---> TCP UDP RAW
  3. # 该链路对应我们一下的逻辑分析。所以非常重要。分析XDP 和 TC的过程很重要。

XDP & TC

20210903-eBPF XDP TC介绍 - 图6

  1. #
  2. Here are some of the operations an XDP program can perform with the packets it receives, once it is connected to a network interface:
  3. # 1.XDP_DROP – Drops and does not process the packet. eBPF programs can analyze traffic patterns and use filters to update the XDP application in real time to drop specific types of packets (for example, malicious traffic).
  4. # 2.XDP_PASS – Indicates that the packet should be forwarded to the normal network stack for further processing. The XDP program can modify the content of the package before this happens. < XDP ---> TC >
  5. # 3.XDP_TX – Forwards the packet (which may have been modified) to the same network interface that received it.
  6. # 4.XDP_REDIRECT – Bypasses the normal network stack and redirects the packet via another NIC to the network.
  7. ||
  8. \/
  9. XDP_DROP:在驱动层丢弃报文,通常用于实现DDos或防火墙
  10. XDP_PASS:允许报文上送到内核网络栈,同时处理该报文的CPU会分配并填充一个skb,将其传递到GRO引擎。之后的处理与没有XDP程序的过程相同。
  11. XDP_TX:BPF程序通过该选项可以将网络报文从接收到该报文的NIC上发送出去。例如当集群中的部分机器实现了防火墙和负载均衡时,这些机器就可以作为hairpinned模式的负载均衡,在接收到报文,经过XDP BPF修改后将该报文原路发送出去。
  12. XDP_REDIRECT:与XDP_TX类似,但是通过另一个网卡将包发出去。另外, XDP_REDIRECT 还可以将包重定向到一个 BPF cpumap,即,当前执行 XDP 程序的 CPU 可以将这个包交给某个远端 CPU,由后者将这个包送到更上层的内核栈,当前 CPU 则继续在这个网卡执行接收和处理包的任务。这和 XDP_PASS 类似,但当前 CPU 不用去做将包送到内核协议栈的准备工作(分配 skb,初始化等等),这部分开销还是很大的。
  13. XDP_ABORTED:表示程序产生了异常,其行为和 XDP_DROP相同,但 XDP_ABORTED 会经过 trace_xdp_exception tracepoint,因此可以通过 tracing 工具来监控这种非正常行为。
  14. # DPDK
  15. # DPDK 完全ByPass掉原本处在内核中的协议栈,但是数据包封装又不得不使用协议栈,所以DPDK把这部分全部在用户空间中实现。这样就不再涉及到内核空间和用户空间之间切换等问题。从而能大大的提高了数据转发的效率。且DPDK使用PMD技术,也对性能提升帮助非常之大。
  16. # 具体性能测试对比:
  17. # https://github.com/OSH-2019/x-xdp-on-android/blob/master/docs/research.md

XDP TC.pdf

数据包处理逻辑

image.png

  1. # BPF XDP ---> sk_buffer ---> TC[network stack Dividing Line] ---> IPv4 And IPv6 ---> Netfilter ---> TCP UDP RAW
  2. # 数据从网卡进来以后,DMA到Linux kernel,然后送到network statck,然后再送给应用。

image.png

  1. # BPF XDP ---> sk_buffer ---> TC[network stack Dividing Line] ---> IPv4 And IPv6 ---> Netfilter ---> TCP UDP RAW
  2. # 送内核中的Netfilter处理。此时已经错过了 XDP TC

image.png

  1. # BPF XDP ---> sk_buffer ---> TC[network stack Dividing Line] ---> IPv4 And IPv6 ---> Netfilter ---> TCP UDP RAW
  2. 我们使用Netfilter处理数据包,实际上我们还可以再往前一下,比如这里使用TC 的Hook来处理。

image.png

  1. # BPF XDP ---> sk_buffer ---> TC[network stack Dividing Line] ---> IPv4 And IPv6 ---> Netfilter ---> TCP UDP RAW
  2. 我们最终可以在XDP处完成数据的Drop。
  3. # http://wiki.dreamrunner.org/public_html/Linux/Networks/sk_buff-structure-analysis.html

XDP应用

  1. Use Cases for XDP
  2. Here are a few common use cases for XDP in eBPF.
  3. DDoS Mitigation and Firewalling
  4. One of the basic functions of XDP in eBPF is to use XDP_DROP, which tells the driver to drop packets at an early stage. This lets you apply a variety of efficient network strategies, while keeping the cost of each packet very low.
  5. This is great for situations where you need to deal with any type of DDoS attack, but more generally, using XDP, eBPF can implement any type of firewall policy with very little overhead. XDP can handle these scenarios, for example, by scrubbing illegitimate traffic and forwarding legitimate packets to their destination using XDP_TX.
  6. XDP can either be deployed in a standalone network appliance, or distributed to multiple nodes that protect the host. The latter scenario can be implemented using XDP_PASS or cpumap XDP_REDIRECT. To boost performance, you can use offloaded XDP, which shifts the already small cost of each data packet entirely to the NIC, which is processed at wire speed.
  7. Forwarding and Load Balancing
  8. Another major use case for XDP is the use of XDP_TX or XDP_REDIRECT operations for packet forwarding and load balancing. Data packets can be manipulated by BPF programs running on the XDP layer. BPF helpers—functions used by BPF programs to interact with the system or with the context in which they operate—can be used to increase or decrease the headroom of data packets, to encapsulate and decapsulate data packets before sending them back.
  9. There are two common ways to implement a load balancer:
  10. You can use XDP_TX to forward packets using the same NIC by which it was received
  11. You can use XDP_REDIRECT to forward packets to a different network interface
  12. Monitoring and Flow Sampling
  13. XDP is commonly used for packet monitoring, sampling, and other forms of network analysis. It can be used, for example, to monitor traffic on an intermediate node in the path of an end host, or in conjunction with any of the above use cases.
  14. For complex packet analysis, XDP maps network packets (either truncated or full payloads) and custom metadata to eBPF programs. This can also support situations where only initial data in the flow is analyzed, and then bypasses monitoring when it determines the traffic is legitimate.
  15. The flexibility provided by BPF with XDP lets you implement any type of custom monitoring or sampling.