microsoft / ebpf-for-windows-demo

This repository contains the demo material built on top of ebpf-for-windows platform.
MIT License
38 stars 12 forks source link

Fix cilium XDP demo #18

Closed saxena-anurag closed 1 year ago

saxena-anurag commented 2 years ago

It looks like after moving to nuget package, the XDP demo does not work on the final test setup. Compilation of Cilium XDP program fails due to

  1. missing header files
  2. Some redefinitions.
shpalani commented 1 year ago

@saxena-anurag and @shpalani are assigned and working on it.

shpalani commented 1 year ago

Status: Reproduced.

Section: Compiling / Loading eBPF Program in https://github.com/microsoft/ebpf-for-windows-demo/blob/main/cilium/load_balancer/docs/CiliumL4LBSetup.md

PS C:\ebpf-for-windows-demo\Debug> .\cilium_based_agent.exe --bpf-lb-mode=snat --device=ethernet
Initializing daemon with mode = snat
Using interface:
  name         = ethernet
  mtu          = 1500
  ifindex      = 8
  IPv4 address = 172.29.163.18

Cleaning up previous configuration ...
Compiling XDP eBPF program for SNAT ...
In file included from bpf_xdp.c:7:
In file included from ..\include\bpf_helpers.h:13:
..\include/ebpf_structs.h:14:10: fatal error: 'stdbool.h' file not found
#include <stdbool.h>
         ^~~~~~~~~~~
1 error generated.
Failed to compile the XDP program with error 0
Verifying the program ...
bpf_object__open failed with error 0
Initialization complete.

>$

Visual Studio Version

ebpf-for-windows-demo.sln has VisualStudioVersion = 16.0.32002.261 Vs ebpf-for-windows.sln has VisualStudioVersion = 17.4.33205.214

ClCompile:

In latest ebpf-for-windows repo, ebpf-for-windows.sln has C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.35.32215\bin\HostX64\x64\CL.exe Vs In latest ebfp-for-windows-demo repo, ebpf-for-windows-demo.sln has C:\Program Files\Microsoft Visual Studio\2022\Enterprise\VC\Tools\MSVC\14.35.32215\bin\HostX64\x64\CL.exe

clang version

In dev machine: C:\ebpf-2023\ebpf-for-windows-demo>clang --version clang version 11.0.1 Target: x86_64-pc-windows-msvc Thread model: posix InstalledDir: C:\Program Files\LLVM\bin Vs In native VM, clang version 11.1.0 Target: x86_64-pc-windows-msvc Thread model: posix InstalledDir: C:\Program Files\LLVM\bin

shpalani commented 1 year ago

Analysis: With clang 11 version,

  1. Looking at the file from the below ebfp_structs,h, there is no NO_CRT defined...

if !defined(NO_CRT)

include

include

else

typedef unsigned char uint8_t; typedef unsigned int uint32_t; typedef unsigned long long uint64_t;

endif

C:\ebpf-2023\ebpf-for-windows-demo\packages\eBPF-for-Windows.0.9.0\build\native\include\bpf_helpers.h C:\ebpf-2023\ebpf-for-windows-demo\x64\Debug\include\bpf_helpers.h C:\ebpf-2023\ebpf-for-windows-demo\packages\eBPF-for-Windows.0.9.0\build\native\include\ebpf_structs.h C:\ebpf-2023\ebpf-for-windows-demo\x64\Debug\include\ebpf_structs.h

  1. Even C:\ebpf-2023\ebpf-for-windows-demo\cilium\load_balancer\stdbool.h found has just 1 line of pragma statement.
    
    // Copyright (c) Microsoft Corporation
    // SPDX-License-Identifier: MIT

pragma once


----------------------------------------
One possible fix:
In cilium\load_balancer\daemon\daemon.cpp, add 
 #define NO_CRT
shpalani commented 1 year ago

With the above fix of adding #define NO_CRT, there are redefinition errors....

C:\ebpf-for-windows-demo\Debug> .\cilium_based_agent.exe --bpf-lb-mode=snat --device=ethernet
Initializing daemon with mode = snat
Using interface:
  name         = ethernet
  mtu          = 1500
  ifindex      = 8
  IPv4 address = 172.29.163.18

Cleaning up previous configuration ...
Compiling XDP eBPF program for SNAT ...
In file included from bpf_xdp.c:7:
In file included from ..\include\bpf_helpers.h:14:
..\include/ebpf_structs.h:17:23: error: redefinition of typedef 'uint8_t' is a C11 feature
      [-Werror,-Wtypedef-redefinition]
typedef unsigned char uint8_t;
                      ^
..\include\stdint.h:8:23: note: previous definition is here
typedef unsigned char uint8_t;
                      ^
In file included from bpf_xdp.c:7:
In file included from ..\include\bpf_helpers.h:14:
..\include/ebpf_structs.h:18:22: error: redefinition of typedef 'uint32_t' is a C11 feature
      [-Werror,-Wtypedef-redefinition]
typedef unsigned int uint32_t;
                     ^
..\include\stdint.h:9:22: note: previous definition is here
typedef unsigned int uint32_t;
                     ^
In file included from bpf_xdp.c:7:
In file included from ..\include\bpf_helpers.h:14:
..\include/ebpf_structs.h:19:28: error: redefinition of typedef 'uint64_t' is a C11 feature
      [-Werror,-Wtypedef-redefinition]
typedef unsigned long long uint64_t;
                           ^
..\include\stdint.h:7:28: note: previous definition is here
typedef unsigned long long uint64_t;
                           ^
3 errors generated.
Failed to compile the XDP program with error 183
Failed to compile XDP program for SNAT mode.
Failed to compile and load XDP program. error=183
PS C:\ebpf-for-windows-demo\Debug>
shpalani commented 1 year ago

Root cause analysis: stdbool.h was missing in the x64\Debug\include installation. Seems day-1 issue.

CI/CD had the stdbool.h included correctly. clang -g -O2 -target bpf -mcpu=v1 -std=gnu89 -nostdinc -D__NR_CPUS__=4 -Wextra -Werror -Wshadow -Wno-address-of-packed-member -Wno-unknown-warning-option -Wno-gnu-variable-sized-type-not-at-end -Wdeclaration-after-statement -DSECLABEL=2 -DNODE_MAC={.addr={0x0,0x15,0x5d,0x6e,0x85,0x4}} -DCALLS_MAP=cilium_calls_xdp -Dcapture_enabled=0 -DDISABLE_LOOPBACK_LB -DNATIVE_DEV_IFINDEX=2 -I$(SolutionDir)cilium\load_balancer\bpf -I$(SolutionDir)external\cilium\bpf -I$(SolutionDir)external\cilium\bpf\include -I$(SolutionDir)cilium\load_balancer $(ClangIncludes) -c $(SolutionDir)external\cilium\bpf\bpf_xdp.c -o $(SolutionDir)$(Platform)\$(Configuration)\cilium\object\bpf_xdp_snat.o -DEBPF_FOR_WINDOWS -DMODE_SNAT

saxena-anurag commented 1 year ago

Re-opening the issue, as this is not completely fixed yet.

shpalani commented 1 year ago

ebpf program is not loading: Root cause identified. Working on the fix.

Test:

PS C:\Program Files\ebpf-for-windows\drivers> netsh ebpf show programs

    ID  Pins  Links  Mode       Type           Name
======  ====  =====  =========  =============  ====================
 65537     0      0  JIT        xdp            name

PS C:\Program Files\ebpf-for-windows\drivers>
shpalani commented 1 year ago

Fixed. Now the programs are loading.