From cd966c7e198eb838cd1e838cd1c7b9eb764e7add Mon Sep 17 00:00:00 2001
From: Daniel Borkmann <daniel@iogearbox.net>
Date: Thu, 19 Mar 2020 23:36:15 +0100
Subject: [PATCH 05/10] bpf: re-add a proper types.h mapper

Commit a1d93e044c1f ("bpf: don't use fixed size integer types from
stdint.h") removed the types.h mapper to avoid dependency on stdint.h
and instead relied on -I/usr/include/$(shell uname -m)-linux-gnu/ to
pull in linux/types.h from the system headers.

This is problematic in that for the docker runtime base image we want
to get rid of libc6-dev-i386 package which pulls in gcc-multilib and
latter pulls linux-libc-dev which includes all linux headers as well.

Cilium's bpf/ code however already ships all linux header dependencies
by itself, so linux-libc-dev is actually not needed. It's just that we
need to have a proper type mapper for BPF. Implement one with basic
types and also remove the -I/usr/include/$(shell uname -m)-linux-gnu/
inclusion.

Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
(cherry picked from commit 8b3435f91af72dfbc2eef13f463b95ec08faec55)
---
 bpf/Makefile.bpf               |  2 +-
 bpf/include/bpf/types_mapper.h | 33 +++++++++++++++++++++++++++++++++
 bpf/include/linux/types.h      |  9 +++++++++
 bpf/init.sh                    |  3 ---
 pkg/datapath/loader/compile.go | 24 ------------------------
 test/bpf/Makefile              |  4 ++--
 6 files changed, 45 insertions(+), 30 deletions(-)
 create mode 100644 bpf/include/bpf/types_mapper.h
 create mode 100644 bpf/include/linux/types.h

diff --git a/bpf/Makefile.bpf b/bpf/Makefile.bpf
index 14e17a6eb..fee4b8322 100644
--- a/bpf/Makefile.bpf
+++ b/bpf/Makefile.bpf
@@ -1,4 +1,4 @@
-FLAGS := -I/usr/include/$(shell uname -m)-linux-gnu -I$(ROOT_DIR)/bpf/include -I$(ROOT_DIR)/bpf -D__NR_CPUS__=$(shell nproc --all) -O2 -g
+FLAGS := -I$(ROOT_DIR)/bpf/include -I$(ROOT_DIR)/bpf -D__NR_CPUS__=$(shell nproc --all) -O2 -g
 
 CLANG_FLAGS := ${FLAGS} -target bpf -emit-llvm
 # eBPF verifier enforces unaligned access checks where necessary, so don't
diff --git a/bpf/include/bpf/types_mapper.h b/bpf/include/bpf/types_mapper.h
new file mode 100644
index 000000000..aca87d645
--- /dev/null
+++ b/bpf/include/bpf/types_mapper.h
@@ -0,0 +1,33 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2020 Authors of Cilium */
+
+#ifndef __BPF_TYPES_MAPPER__
+#define __BPF_TYPES_MAPPER__
+
+typedef __signed__ char __s8;
+typedef unsigned char __u8;
+
+typedef __signed__ short __s16;
+typedef unsigned short __u16;
+
+typedef __signed__ int __s32;
+typedef unsigned int __u32;
+
+typedef __signed__ long long __s64;
+typedef unsigned long long __u64;
+
+typedef __u16 __le16;
+typedef __u16 __be16;
+
+typedef __u32 __le32;
+typedef __u32 __be32;
+
+typedef __u64 __le64;
+typedef __u64 __be64;
+
+typedef __u16 __sum16;
+typedef __u32 __wsum;
+
+typedef __u64 __aligned_u64;
+
+#endif /* __BPF_TYPES_MAPPER__ */
diff --git a/bpf/include/linux/types.h b/bpf/include/linux/types.h
new file mode 100644
index 000000000..abed7f764
--- /dev/null
+++ b/bpf/include/linux/types.h
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* Copyright (C) 2020 Authors of Cilium */
+
+#ifndef _LINUX_TYPES_H
+#define _LINUX_TYPES_H
+
+#include "../bpf/types_mapper.h"
+
+#endif /* _LINUX_TYPES_H */
diff --git a/bpf/init.sh b/bpf/init.sh
index ae815c99e..4ed5328c8 100755
--- a/bpf/init.sh
+++ b/bpf/init.sh
@@ -58,8 +58,6 @@ rm $RUNDIR/encap.state 2> /dev/null || true
 # This directory was created by the daemon and contains the per container header file
 DIR="$PWD/globals"
 
-MACHINE=$(uname -m)
-
 function setup_dev()
 {
 	local -r NAME=$1
@@ -273,7 +271,6 @@ function bpf_compile()
 
 	clang -O2 -g -target bpf -emit-llvm				\
 	      -Wno-address-of-packed-member -Wno-unknown-warning-option	\
-	      -I/usr/include/${MACHINE}-linux-gnu                       \
 	      -I. -I$DIR -I$LIB -I$LIB/include				\
 	      -D__NR_CPUS__=$NR_CPUS					\
 	      -DENABLE_ARP_RESPONDER					\
diff --git a/pkg/datapath/loader/compile.go b/pkg/datapath/loader/compile.go
index dfc9b473f..df20ea261 100644
--- a/pkg/datapath/loader/compile.go
+++ b/pkg/datapath/loader/compile.go
@@ -23,7 +23,6 @@ import (
 	"io/ioutil"
 	"os"
 	"path"
-	"sync"
 
 	"github.com/cilium/cilium/common"
 	"github.com/cilium/cilium/pkg/command/exec"
@@ -31,7 +30,6 @@ import (
 	"github.com/cilium/cilium/pkg/option"
 
 	"github.com/sirupsen/logrus"
-	"golang.org/x/sys/unix"
 )
 
 // OutputType determines the type to be generated by the compilation steps.
@@ -194,27 +192,6 @@ func compileAndLink(ctx context.Context, prog *progInfo, dir *directoryInfo, deb
 	return err
 }
 
-var (
-	unameOnce sync.Once
-
-	// default fallback
-	machineName = "x86_64"
-)
-
-// getMachineName returns the machine hardware name of this host.
-func getMachineName() string {
-	unameOnce.Do(func() {
-		var uts unix.Utsname
-		err := unix.Uname(&uts)
-		if err != nil {
-			log.WithError(err).Warning("Failed to get machine hardware name")
-		} else {
-			machineName = string(uts.Machine[:bytes.IndexByte(uts.Machine[:], 0)])
-		}
-	})
-	return machineName
-}
-
 // progCFlags determines the compiler flags for the specified prog and paths.
 func progCFlags(prog *progInfo, dir *directoryInfo) []string {
 	var output string
@@ -226,7 +203,6 @@ func progCFlags(prog *progInfo, dir *directoryInfo) []string {
 	}
 
 	return append(testIncludes,
-		fmt.Sprintf("-I/usr/include/%s-linux-gnu", getMachineName()),
 		fmt.Sprintf("-I%s", path.Join(dir.Runtime, "globals")),
 		fmt.Sprintf("-I%s", dir.State),
 		fmt.Sprintf("-I%s", dir.Library),
diff --git a/test/bpf/Makefile b/test/bpf/Makefile
index 4919d32bc..e4f0dfed2 100644
--- a/test/bpf/Makefile
+++ b/test/bpf/Makefile
@@ -1,8 +1,8 @@
 include ../../Makefile.defs
 
-FLAGS := -I/usr/include/$(shell uname -m)-linux-gnu  -I../../bpf/ -I../../bpf/include -I. -D__NR_CPUS__=$(shell nproc --all) -O2
+FLAGS := -I../../bpf/ -I../../bpf/include -I. -D__NR_CPUS__=$(shell nproc --all) -O2
 BPF_CC_FLAGS :=  ${FLAGS} -target bpf -emit-llvm
-BPF_LLC_FLAGS   := -march=bpf -mcpu=probe -filetype=obj
+BPF_LLC_FLAGS := -march=bpf -mcpu=probe -filetype=obj
 
 LIB := $(shell find ../../bpf/ -name '*.h')
 
-- 
2.27.0

