Upgrade to Pro — share decks privately, control downloads, hide ads and more …

eBPF-based Container Networking

eBPF-based Container Networking

議程內容將會從 Kernel Networking, Kernel Modules of Networking, Kernel-Bypass Networking 及目前最具有革命性的 Early point of Kernel Networking 以上這幾種不同的 Linux Kernel 網路技術介紹,此外還會深入介紹 eBPF 的過去發展演變、現今如何開發 eBPF 程式並注入到 Linux Kernel 改變 Kernel 原生行為,更進一步做 Linux Kernel Networking Subsystem 注入點 (Hooks) 的介紹,最後將會介紹如何引入 eBPF 幫助現在 Kubernetes networking 所存在的問題。

Che-Wei Lin

July 29, 2022
Tweet

More Decks by Che-Wei Lin

Other Decks in Technology

Transcript

  1. Agenda • Looking Back on SDN & CNI • So2ware-defined

    Networking & Container Network Interface • Introduc<on to eBPF • eBPF Development • eBPF Program Types • Summary & Future Scope • Slides: hHps:/ /<nyurl.com/ebpf-linux COSCUP 2022 3
  2. SDN & CNI • Linux Kernel Networking • Netfilter, Rou-ng,

    Traffic Control (TC) • Kernel Modules of Networking • Open vSwitch (OvS) • Kernel-Bypass Networking • DPDK • Early Point of Kernel Networking (XDP) • XDP with eBPF COSCUP 2022 4
  3. CNI Fundamental Requirements • Kubernetes 對任何網路實現都提出了以下基本要求 (i.e., 任何 CNI Plugin

    實作必須⽀援) • Reachability - Pod 可以在沒有 NAT 的情況下與任何其他節點 (Node) 上的其他 Pod 直接通訊 • Connec.vity - 節點 (Node) 上的程式 (e.g., system daemons, kubelet) 可以與該節點上的所有 Pod 通訊 • 次要⽀援⽬標 • IP 地址管理 (IPAM) • 來源地址轉換 (SNAT) • 端⼝映射 (Port mappings) • 頻寬管理 (QoS) COSCUP 2022 5
  4. Linux Kernel Networking • 典型CNI⽤例: Flannel, Calico • 常⾒操縱 datapath

    作法 (透過 Kernel Networking Tools) • ip-netns: 容器網路管理 • iptables (netfilter): 做 SNAT/ipMasq1, DNAT • iproute2: 配置路由表 Linux bridge 創建及 Default gateway 2設定,及串接 netns 3管理 • ARP 代理,創建 IPIP/VXLAN tunnel 3 plugins/plugins/main/bridge/bridge.go#L336 2 plugins/plugins/main/bridge/bridge.go#L388 1 plugins/pkg/ip/ipmasq_linux.go#L24-L26 COSCUP 2022 8
  5. Linux Kernel Networking • Pros: • 開箱即⽤ • 簡單易⽤,定位排查問題相對容易 (tcpdump)

    • Cons: • 慢 • 受限於 Kernel network stack 所提供之 功能,進階 datapath 操縱不容易達成 COSCUP 2022 9
  6. Kernel Modules for Networking • 典型 CNI ⽤例: OVN-Kubernetes, Kube-OVN,

    WeaveNet • ⼜稱 Open vSwitch datapath 其依賴 openvswitch.ko; 根據其特性⼜分為 ovs-kernel, ovs-dpdk • 須⾃⾏編譯 Kernel module 後掛載,⽬前⼤多數 Linux 發⾏版已 內建(Kernel 4.6+),但仍可以找最新的穩定版本編譯替換 • 常⾒操縱 datapath 作法 • OFPP_NORMAL 維持交換器傳統的 L2/L3 功能做轉發 • 使⽤ OVN 或⾃⾏開發 SDN controller 透過 OpenFlow Protocol 下發 flows 進 Open vSwitch • OVN 作為 SDN Controller 提供非常豐富進階網路路由功能 • Distributed virtual routers, logical switches, Network ACLs, DHCP, DNS Server COSCUP 2022 11
  7. Kernel Modules for Networking 以 Open vSwitch 為例 • Pros:

    • ⽤⼾可透過程式化(Programmable)的控制器 (Controller),與 vSwitch 交互完全⾃訂義路由 • Cons: • Kernel modules 需另外編譯,安全性需要注意 • 對於更進階的 datapath 操作需修改 Kernel modules 開發難度⾼ • 對比於 Kernel networking ⽅式 forwarding/rou@ng 彈性許多 COSCUP 2022 12
  8. Kernel-Bypass Networking • 典型 CNI ⽤例: userspace-cni-network-plugin • User-space networking,

    datapath 常⾒依賴 DPDK • 對應 Kernel networking 的另⼀種在 User-space 網路選項 • DPDK 技術可以讓進到主機的封包,直接跳過 Linux Kernel, 送到 User-space 的 App. 做處理 (i.e., 是⼀種有效地將封包送入⽤⼾空間的機制) • Bypass Kernel 所以將失去以下功能必須重新實作 • Linux 原⽣網路設定 (iproute2, ifconfig)、debuging, troubleshooGng ⼯具 (tcpdump) • Packet processing, Packet handling: ARP, bridging • IP Stack (TCP/IP, rouGng, filGng) COSCUP 2022 14
  9. Kernel-Bypass Networking 常⾒操縱 datapath 作法: 相較於純 DPDK 更常⾒的作法是搭配 Open vSwitch(ovs-dpdk)

    或 VPP(vpp-dpdk) ⼀起使⽤,兩 種都是可以在 User-space 做 forwarding/rou>ng 的⼯ 具 • ovs-dpdk: 讓 SDN Controller/OVN 決定封包轉發/ 處理的邏輯 • vpp-dpdk: VPP 4提供 packets processing, switch/ router 等功能 4 Vector Packet Processing COSCUP 2022 15
  10. Kernel-Bypass Networking 以 DPDK 為例 • Pros: • 性能:快 System

    Call, Context switch (user to kernel), copy data from (user to kernel/kernel to user) 等花費更低 • 價值:彈性⾼ packet processing, packet handling, protocol ⽀援豐富 • Cons: • 開發困難:僅提供網路 Dataplan 開發⼯具,對使⽤者要求具 備備專業網路及硬體知識 • 部署複雜:獨佔設備的特性,硬體及軟體部署難度⾼ • 獨佔裝置:需搭配特定的硬體網卡及驅動程式 COSCUP 2022 16
  11. Early Point of Kernel Networking (XDP) • 典型 CNI ⽤例:

    Cilium, Calico eBPF Dataplane • 透過 eBPF 技術,提早對進去 networking stack 前的封包做 處理,也可以對 Kernel 動態注入影響原來 Kernel networking 所提供的功能 • 常⾒操縱 datapath 作法 (注入點) • XDP (eXpress Data Path): DSR (Direct Server Return), SNAT, DNAT • tc ingress/egress hook: DSR, SNAT, DNAT • cgroup socket hook: ACLs • socket map: Socket redirects, SOCKMAP • socket opKon hook: Socket level LB, DNAT COSCUP 2022 18
  12. Early Point of Kernel Networking (XDP) • 典型 CNI ⽤例:

    Cilium, Calico eBPF Dataplane • 透過 eBPF 技術,提早對進去 networking stack 前的封包做 處理,也可以對 Kernel 動態注入影響原來 Kernel networking 所提供的功能 • 常⾒操縱 datapath 作法 (注入點) • XDP (eXpress Data Path): DSR (Direct Server Return), SNAT, DNAT • tc ingress/egress hook: DSR, SNAT, DNAT • cgroup socket hook: ACLs • socket map: Socket redirects, SOCKMAP • socket opKon hook: Socket level LB, DNAT COSCUP 2022 19
  13. Early Point of Kernel Networking (XDP) • XDP (eXpress Data

    Path) 的特型 • 是被設計為⾼性能、可程式化 (Programmability) • 不是 Kernel bypass、不是⽤來取代 TCP/IP stack、 某些條件下不需要專⽤硬體 • XDP Ac?ons • XDP_ABORTED • XDP_DROP • XDP_PASS • XDP_TX • XDP_REDIRECT COSCUP 2022 20
  14. Early Point of Kernel Networking (XDP) • Pros: • Kernel

    安全性⾼:由 eBPF verifier 做確保 • 開發難度較低:相較於 Kernel modules, Kernel bypass 開發難度低 • Datapath 保有適當的彈性,透過載入 BPF 變 更 • Cons: • 只能通過 BPF Helper 函數呼叫,不能引⽤所 有 Kernel 函數 COSCUP 2022 21
  15. Recap • Linux Kernel Networking • Kernel Modules of Networking

    • Kernel-Bypass Networking • Early Point of Kernel Networking (XDP) COSCUP 2022 22
  16. Quotes If the tradi+onal kernel network stack is a freeway,

    kernel bypass is a proposal to build an infrastructure of high speed trains and XDP is a proposal for adding carpool lanes to the freeway. — Tom Herbert, Alexei Starovoitov5 5 eXpress Data Path (XDP) COSCUP 2022 23
  17. Introduc)on to eBPF • eBPF 技術涵蓋4⼤範疇 • 安全功能 • 網路功能

    • 追蹤 (Tracing)、性能分析 (Profiling) • 可觀測性 (Observability)、監控 (Monitoring) • Linux Kernel ⼀直都是實現監控、可觀測性、網路、安全功能最理想的地⽅ • 不過很多情況下並非容易的事,因為這些⼯作需要修改 Kernel 源碼或加載 Kernel modules • eBPF 技術提供⼀個機會讓 Kernel 可以透過動態注入⽤⼾的程式 (eBPF program) 改變原⽣ Kernel 的⾏ 為 COSCUP 2022 24
  18. Classical BPF (cBPF) • Berkeley Packet Filter6 • 特殊⽬的 virtual

    machine. ⽀援應⽤快速地 從 stream 過濾封包 • network tap: 從網路設備驅動程式收集封 包的副本 • packet filter: 過濾器決定是否應該接受⼀ 個封包 • 廣泛使⽤在 tcpdump 6 The BSD Packet Filter: A New Architecture for User-level Packet Capture, 1992 COSCUP 2022 25
  19. Extended BPF (eBPF) • 原始 BPF 被稱為 classical BPF,隨著 eBPF

    推出已被 棄⽤ • 2014/09, 第⼀個 eBPF 補丁集合合併到 Kernel 3.18 7 • 將 eBPF 的解釋器 (interpreter) 從 networking subsystem 拆出來 • BPF Maps: ⽤於在 Kernel 和 user-space 之間共享 數據的通⽤儲存 (generic store); 有29種 types • user-space 透過 syscall 和 eBPF 程式可以同時 存取 BPF Maps 7 BPF syscall, maps, verifier, samples, 2014/06; The BPF system call API, version 14, 2014/09 COSCUP 2022 26
  20. eBPF Load and Verify8 1. Compile: eBPF 相比於 cBPF 最有⽤的演變之⼀是基於

    clang 和 LLVM 提供的編譯後端,能從 C 源碼編譯成 eBPF bytecode • clang 和 LLVM 為 eBPF 開發⽣態不可或缺的⼯具 2. Load: 透過 Loader 使⽤ syscall, bpf() 將 bytecode 載入 Kernel 3. Verify: Kernel 使⽤ Verify 保證執⾏的 bytecode 安全 性、避免 Kernel 發⽣錯誤 4. 等待觸發執⾏: 在 Kernel 對應的事件(Event)觸發時,執 ⾏該 eBPF bytecode,並且可以通過 BPF Map 與 User- space 程式交互 8 Kernel v3.18, kernel/bpf/syscall.c COSCUP 2022 27
  21. eBPF Development 典型的開發及載入 BPF 流程 1. 使⽤ C 開發 BPF

    程式,並使⽤ LLVM 編譯成 ELF bytecode 2. User-space 透過 BPF ELF Loader (e.g., iproute2) 解析 ELF 後,通過 bpf(), syscall 將解析後的 BPF Program 注入 Kernel 3. Verifier 驗證 BPF 指令,然後對其進⾏即時編 譯(JIT),返回程式的⼀個 file descriptor 4. 利⽤ file descriptor aHach 到 Kernel networking subsystem 開發⼯具選擇 • C: libbpf/libbpf; Lives in the kernel repo • Python: iovisor/bcc; Focused on tracing and profiling; Heavy run>me • Go: iovisor/gobpf; Go bindings for the bcc framework; uses CGo • Go: cilium/ebpf; Pure Go eBPF Library; maintained by Cloudflare and Cilium • Go: dropbox/goebpf; Support less eBPF features; Simple and easy to use COSCUP 2022 28
  22. eBPF Development XDP/BPF program xdp_drop.c #include <linux/bpf.h> #include <linux/bpf_helpers.h> SEC("xdp_drop")

    int xdp_drop(struct xdp_md *ctx) { return XDP_DROP; } char __license[] SEC("license") = "GPL"; # Compile clang -target bpf -Wall -O2 -c xdp_drop.c -o xdp_drop.o # Attach xdp/bpf program ip link set dev eth0 xdp obj xdp_drop.o # Detach xdp/bpf program ip link set dev eth0 xdp off COSCUP 2022 29
  23. eBPF Program Types • Program Types Introduc4on • Socket Filter

    Programs • XDP Programs • Traffic Classifer (tc) Programs • Cgroup Socket Programs • Socket Op4on Programs • Socket Map Programs COSCUP 2022 30
  24. eBPF Program Types Intro. • 截⾄ Kernel 5.10 約有31種 BPF

    Program Types • 對於所有 BPF Program Types9 有2⼤分類 • Tracing • Networking • 本次分享將會著重在 Networking 領域 9 Kernel 5.10; linux/include/uapi/linux/bpf.h#L170 COSCUP 2022 31
  25. Socket Filter Intro. • Socket Filter Programs; BPF_PROG_TYPE_SOCKET_FILTER • 最早出現在

    Kernel 裡⾯的 BPF program type • Socket Filter 不允許修改封包內容, 亦不允許變更封包地址,僅作可觀測性 (Observability) ⽤途 • 使⽤場景 • 流量過濾及複製(對原始流量只讀) • 可觀測性:流量統計 COSCUP 2022 32
  26. Bo#om half of the Network Stack • Networking Subsystem 下半部分

    • XDP/BPF hooks • tc/BPF hooks COSCUP 2022 33
  27. XDP/BPF Intro. • XDP Programs; BPF_PROG_TYPE_XDP • Na.ve XDP (Driver):

    預設原⽣模式 • Offload XDP (Hardware): BPF program offload 在網卡上 • Generic XDP (Network Stack): 4.12+, virtual interface, 通⽤模式(即使驅動不⽀援仍可使⽤) • 使⽤場景: 流量清洗、DDoS防禦10、緩解11、 L4負載均衡12 12 Facebook L4 LB: Katran 11 Cloudflare: L4Drop: XDP DDoS Mi6ga6ons 10 Cloudflare: How to drop 10 million packets per second COSCUP 2022 34
  28. XDP/BPF Example Program xdp_pass.c #include <linux/bpf.h> #include <bpf/bpf_helpers.h> SEC("xdp") int

    xdp_dummy_prog(struct xdp_md *ctx) { return XDP_PASS; } char _license[] SEC("license") = "GPL"; # Compile clang -target bpf -Wall -O2 -c xdp_pass.c -o xdp_pass.o # Attach xdp/bpf program ip link set dev eth0 xdp obj xdp_pass.o # Detach xdp/bpf program ip link set dev eth0 xdp off COSCUP 2022 35
  29. tc/BPF Intro. • tc: qdisc(排隊規則), class(類別), filter(過濾器) • 傳統 filter

    + ac/on 模式的限制: 匹配+執⾏動作 • Traffic Classifier Programs; BPF_PROG_TYPE_SCHED_CLS • Kernel 4.1 tc 指令⽀援 eBPF,可以直接將 BPF 程式作 為 tc classifier (filter) • Kernel 4.4 iproute2 引入 direct-action,classifier 的返回將直接視為 ac/on 的返回 • Kernel 4.5 添加了⼀個新的 qdisc Class clsact • clsact 能夠以 direct-action 模式 aEach eBPF 程式 • clsact 可以在 ingress/egress 上以 direct- action 模式 aEach eBPF 程式 COSCUP 2022 36
  30. tc/BPF Example Program tc_process.c #include <linux/bpf.h> #include <linux/if_ether.h> #include <linux/pkt_cls.h>

    #include <linux/swab.h> int classifier(struct __sk_buff *skb) { void *data_end = (void *)(unsigned long long)skb->data_end; void *data = (void *)(unsigned long long)skb->data; struct ethhdr *eth = data; if (data + sizeof(struct ethhdr) > data_end) return TC_ACT_SHOT; if (eth->h_proto == ___constant_swab16(ETH_P_IP)) // Packet processing is not implemented in this sample. return process_packet(skb); else return TC_ACT_OK; } # Create a qdisc class clsact tc qdisc add dev eth0 clsact # Attach tc/bpf program tc filter add dev eth0 ingress bpf direct-action obj tc_process.o # Detach tc/bpf program tc qdisc del dev eth0 clsact COSCUP 2022 37
  31. Difference between tc/BPF & XDP/BPF • XDP/BPF hook 為最早期觸發點(創建skb 之前),性能最⾼

    • tc/BPF 擁有更好的封包修改能⼒ (input context: sk_buff vs. xdp_buff) • XDP/BPF hook 觸發點只可以在 ingress, tc/BPF hook 觸發點可以在 ingress 及 egress • XDP/BPF 依賴驅動⽀援 tc/BPF 則不依賴 COSCUP 2022 38
  32. tc/BPF & XDP/BPF for Container Networking • tc, XDP BPF

    datapath 常⾒⽤於操作南北向流量(N-S Traffic)在 Cluster Network 上 Service Type: NodePort, ExternalIPs, LoadBalancer • Cilium 1.6 的 kube-proxy 取代 • tc/BPF 實現 SNAT, DNAT 取代 kube-proxy (iptables) Service Type: NodePort • Cilium 1.7 的 Direct Server Return (DSR) • tc/BPF 實現 DSR • 直接回覆外部 Client 不需要額外跳轉 • 保留 Client Source IP • Cilium 1.8 kube-proxy 的取代在 XDP 層實現其包含 DSR, SNAT 功能 • 以 XDP 實現網路性能更⾼ COSCUP 2022 39
  33. Top half of the Network Stack • Networking Subsystem 上半部分

    • cgroup socket hooks • socket op6on hooks • socket map COSCUP 2022 40
  34. Cgroup Socket Intro. • Cgroup Programs; BPF_PROG_TYPE_CGROUP_SKB • 任何封包要從 Kernel

    傳遞進去任何⾏程(Process)在相同的 cgroup 會經過這個 filter (i.e., ⽤ cgroup 控制進去 procress 的網路流量) • 與 Socket Filter ⼗分類似但主要差別在 • cgroup 的級別上放⾏或丟棄封包(return 1 for allow, else for DROP) • cgroup 可以控制所有在相同 cgroup 裡的全部⾏程 (Processes) • 使⽤場景 • 可以被加載到 IP/TCP/UDP 的 ingress/egress 處理邏輯上,作為網路安全策略 (Network Policy) 的實現 COSCUP 2022 41
  35. Cgroup Socket Program cgroup_socket_blocked.c #include <linux/bpf.h> #include <netinet/ip.h> #include <bpf/bpf_helpers.h>

    // Ingress hook - handle incoming packets SEC("cgroup_skb/ingress") int ingress(struct __sk_buff *skb) { return 0; } // Egress hook - handle outgoing packets SEC("cgroup_skb/egress") int egress(struct __sk_buff *skb) { return 0; } char __license[] __section("license") = "GPL"; COSCUP 2022 42
  36. Socket Op*on Intro. • Socket Op+on Programs; BPF_PROG_TYPE_SOCK_OPS; SOCK_OPS •

    Hook 位置在多個地⽅ BPF_SOCK_OPS_*13 • 使⽤場景 • 動態跟蹤監聽 Socket 事件 • 動態修改 socket connec+on op+ons, e.g., tcp connec+on establishment, retransmit, +meout • 在 socket 事件發⽣,變更⽬的地 IP 或 Port 實現, e.g., LB, 透明代理(Cilium Kubernetes Service) • Socket Level LB: Socket redirec+on (需搭配 BPF_PROG_TYPE_SK_SKB) 13 include/uapi/linux/bpf.h COSCUP 2022 43
  37. Socket Map Intro. • Socket Map Programs; BPF_PROG_TYPE_SK_SKB • 可以存取

    socket maps(SOCKMAP)14 及做 socket redirects • 使⽤場景: Socket Level LB: Socket redirec9on; 1. 創建 socket map (⼀種 BPF Map 的類型); 2. BPF_PROG_TYPE_SOCK_OPS: 攔截所有 socket 事 件,然後從 struct bpf_sock_ops 中讀取 socket 訊息存到 sockmap; 3. BPF_PROG_TYPE_SK_SKB: 攔截 sendmsg() 的 syscall,然後從 sockmap 查詢對端 socket 訊息, 最後透過 bpf_sk_redirect_map() 做重定向 14 SOCKMAP - TCP splicing of the future COSCUP 2022 44
  38. Cgroup Socket & Socket Op/on, Map/BPF for Container Networking •

    Cgroup Socket, Socket Op0on, Socket Map 常⾒⽤於操作東⻄向流量(E-W Traffic)在 Cluster Network 上; Service Type: ClusterIP • Cilium ⼤量使⽤ eBPF 做 Datapath 操縱的代表性 CNI • Cilium 1.6 的 kube-proxy 取代 • Socket Op0on, Map/BPF 實現 DNAT 取代 kube-proxy (iptables) Service Type: ClusterIP • 透明代理 • ⾼性能 Socket 層負載均衡 (traffic 沒經過 netfilter 逕⾏重定向) • Cilium BPF datapath 的核⼼設計理念 • 對於東⻄向流量(E-W Traffic),儘量靠近 Socket 層處理 • 對於南北向流量(N-S Traffic),儘量靠近網卡驅動層處理 COSCUP 2022 45
  39. Summary & Future Scope • eBPF 會是接下來在 Kubernetes Networking 上的

    game changer • Cilium 為⽬前最出⾊的 eBPF CNI 專案 • Egress Gateway (Cilium 1.12 stable release) • Integrated Ingress Controller (Cilium 1.12) • Envoy 作為 edge proxy 能感知 L7 協定亦能做 L4LB 並具有可觀測性、熔斷、⾃動重試、限流等 功能 • Cilium Service Mesh • Sidecarless model, eBPF acceleraMon & Per-Node Proxy • Reduce Total Cost of Service Mesh & Envoy mulM-tenancy problems COSCUP 2022 46
  40. We're Hiring Site Reliability Engineers Taipei | LINE Taiwan |

    Engineering System | Remote | Full-;me h=ps:/ /careers.linecorp.com/jobs/1330 COSCUP 2022 47