From 140f759c00f0885abd3cff7856549408489f7dc7 Mon Sep 17 00:00:00 2001 From: Shaun Crampton Date: Mon, 24 Feb 2020 14:09:47 +0000 Subject: [PATCH] Post-merge build fix-ups. --- bpf-gpl/Makefile | 26 +++++++-- bpf-gpl/calculate-flags | 2 +- bpf-gpl/connect_balancer_v6.c | 19 ++++++- bpf-gpl/list-objs | 3 +- bpf/nat/connecttime.go | 79 +-------------------------- bpf/ut/bpf_prog_test.go | 2 +- fv/test-connection/test-connection.go | 4 +- 7 files changed, 45 insertions(+), 90 deletions(-) diff --git a/bpf-gpl/Makefile b/bpf-gpl/Makefile index df5d81d1de..6e52afdf94 100644 --- a/bpf-gpl/Makefile +++ b/bpf-gpl/Makefile @@ -44,8 +44,11 @@ C_FILES:=tc.c connect_balancer.c all: $(OBJS) ut-objs: $(UT_OBJS) -connect_time%.ll: connect_balancer.c connect_balancer.d - $(CC) $(CFLAGS) `./calculate-flags $@` -c $< -o $@ +COMPILE=$(CC) $(CFLAGS) `./calculate-flags $@` -c $< -o $@ +connect_time_%v4.ll: connect_balancer.c connect_balancer.d calculate-flags + $(COMPILE) +connect_time_%v6.ll: connect_balancer_v6.c connect_balancer_v6.d calculate-flags + $(COMPILE) UT_CFLAGS=\ -D__BPFTOOL_LOADER__ \ @@ -62,11 +65,24 @@ ut/%.ll: ut/%.c ut/ut.h tc.c tc.d $(CC) $(CFLAGS) $(UT_CFLAGS) -c $< -o $@ # Production and UT versions of the main binaries. -%.ll: tc.c tc.d - $(CC) $(CFLAGS) `./calculate-flags $@` -c $< -o $@ +# Combining the targets into one rule causes make to fail to rebuild the .ll files. Not sure why. +to%.ll: tc.c tc.d calculate-flags + $(COMPILE) +from%.ll: tc.c tc.d calculate-flags + $(COMPILE) +test%.ll: tc.c tc.d calculate-flags + $(COMPILE) LINK=$(LD) -march=bpf -filetype=obj -o $@ $< -bin/%.o: %.ll | bin +bin/to%.o: to%.ll | bin + $(LINK) +bin/from%.o: from%.ll | bin + $(LINK) +bin/test%.o: test%.ll | bin + $(LINK) +bin/connect_time_%v4.o: connect_time_%v4.ll | bin + $(LINK) +bin/connect_time_%v6.o: connect_time_%v6.ll | bin $(LINK) ut/%.o: ut/%.ll $(LINK) diff --git a/bpf-gpl/calculate-flags b/bpf-gpl/calculate-flags index e16be0fd41..45a042a59e 100755 --- a/bpf-gpl/calculate-flags +++ b/bpf-gpl/calculate-flags @@ -51,7 +51,7 @@ if [[ "${filename}" =~ .*host([0-9a-fA-Fx]+).* ]]; then args+=("-DCALI_HOST_IP=${BASH_REMATCH[1]}") fi -if [[ "${filename}" =~ ut_.* ]]; then +if [[ "${filename}" =~ test_.* ]]; then args+=("-D__BPFTOOL_LOADER__") args+=("-DCALI_VXLAN_PORT=5665") args+=("-DCALI_NAT_TUNNEL_MTU=700") diff --git a/bpf-gpl/connect_balancer_v6.c b/bpf-gpl/connect_balancer_v6.c index c2ba2f09fc..2fd4148abe 100644 --- a/bpf-gpl/connect_balancer_v6.c +++ b/bpf-gpl/connect_balancer_v6.c @@ -1,10 +1,25 @@ +// Project Calico BPF dataplane programs. // Copyright (c) 2020 Tigera, Inc. All rights reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this program; if not, write to the Free Software Foundation, Inc., +// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include #include -#include "../include/bpf.h" -#include "../include/log.h" +#include "bpf.h" +#include "log.h" #include "sendrecv.h" diff --git a/bpf-gpl/list-objs b/bpf-gpl/list-objs index 1d60727506..f3ffb0c071 100755 --- a/bpf-gpl/list-objs +++ b/bpf-gpl/list-objs @@ -22,7 +22,8 @@ # # WARNING: naming and set of cases must be kept in sync with tc.ProgFilename() in Felix's compiler.go. for log_level in debug info no_log; do - echo "bin/connect_time_$log_level.o" + echo "bin/connect_time_${log_level}_v4.o" + echo "bin/connect_time_${log_level}_v6.o" for host_drop in "" "host_drop_"; do if [ "${host_drop}" = "host_drop_" ]; then # The workload-to-host drop setting only applies to the from-workload hook. diff --git a/bpf/nat/connecttime.go b/bpf/nat/connecttime.go index cf7e614898..2b228e722d 100644 --- a/bpf/nat/connecttime.go +++ b/bpf/nat/connecttime.go @@ -15,7 +15,6 @@ package nat import ( - "bufio" "encoding/json" "fmt" "os" @@ -23,13 +22,11 @@ import ( "path" "strconv" "strings" - "sync" "github.com/pkg/errors" log "github.com/sirupsen/logrus" "github.com/projectcalico/felix/bpf" - "github.com/projectcalico/felix/bpf/tc" ) type cgroupProgs struct { @@ -188,7 +185,7 @@ func ProgFileName(logLevel string, ipver int) string { switch ipver { case 4: - return fmt.Sprintf("connect_time_%s.o", logLevel) + return fmt.Sprintf("connect_time_%s_v4.o", logLevel) case 6: return fmt.Sprintf("connect_time_%s_v6.o", logLevel) } @@ -197,80 +194,6 @@ func ProgFileName(logLevel string, ipver int) string { return "" } -func compileConnectTimeLoadBalancer(logLevel, inFile, outFile string) error { - args := []string{ - "-x", - "c", - "-D__KERNEL__", - "-D__ASM_SYSREG_H", - "-D__BPFTOOL_LOADER__", - "-DCALI_LOG_LEVEL=CALI_LOG_LEVEL_" + strings.ToUpper(logLevel), - fmt.Sprintf("-DCALI_COMPILE_FLAGS=%d", tc.CompileFlagCgroup), - "-DCALI_LOG_PFX=CALI", - "-Wno-unused-value", - "-Wno-pointer-sign", - "-Wno-compare-distinct-pointer-types", - "-Wunused", - "-Wall", - "-Werror", - "-fno-stack-protector", - "-O2", - "-emit-llvm", - "-c", inFile, - "-o", "-", - } - - clang := exec.Command("clang", args...) - clangStdout, err := clang.StdoutPipe() - if err != nil { - return err - } - clangStderr, err := clang.StderrPipe() - if err != nil { - return err - } - err = clang.Start() - if err != nil { - log.WithError(err).Panic("Failed to start clang.") - return err - } - var wg sync.WaitGroup - wg.Add(1) - go func() { - defer wg.Done() - scanner := bufio.NewScanner(clangStderr) - for scanner.Scan() { - log.Warnf("clang stderr: %s", scanner.Text()) - } - if err != nil { - log.WithError(err).Error("Error while reading clang stderr") - } - }() - llc := exec.Command("llc", "-march=bpf", "-filetype=obj", "-o", outFile) - llc.Stdin = clangStdout - out, err := llc.CombinedOutput() - if err != nil { - log.WithError(err).WithField("out", string(out)).Error("Failed to compile C program (llc step)") - return err - } - err = clang.Wait() - if err != nil { - log.WithError(err).Error("Clang failed.") - return err - } - wg.Wait() - - return nil -} - -func CompileConnectTimeLoadBalancer(logLevel string, outFile string) error { - return compileConnectTimeLoadBalancer(logLevel, "bpf/cgroup/connect_balancer.c", outFile) -} - -func CompileConnectTimeLoadBalancerV6(logLevel string, outFile string) error { - return compileConnectTimeLoadBalancer(logLevel, "bpf/cgroup/connect_balancer_v6.c", outFile) -} - func ensureCgroupPath(cgroupv2 string) (string, error) { cgroupRoot, err := bpf.MaybeMountCgroupV2() if err != nil { diff --git a/bpf/ut/bpf_prog_test.go b/bpf/ut/bpf_prog_test.go index eed6af59eb..3d50318ac2 100644 --- a/bpf/ut/bpf_prog_test.go +++ b/bpf/ut/bpf_prog_test.go @@ -128,7 +128,7 @@ func runBpfTest(t *testing.T, section string, rules [][][]*proto.Rule, testFn fu Expect(err).NotTo(HaveOccurred()) defer os.RemoveAll(bpfFsDir) - obj := "../../bpf-gpl/bin/ut_" + obj := "../../bpf-gpl/bin/test_" if strings.Contains(section, "from") { obj += "from_" } else { diff --git a/fv/test-connection/test-connection.go b/fv/test-connection/test-connection.go index 5b2c0fcb88..b55c600fd3 100644 --- a/fv/test-connection/test-connection.go +++ b/fv/test-connection/test-connection.go @@ -248,7 +248,7 @@ func tryConnect(remoteIpAddr, remotePort, sourceIpAddr, sourcePort, protocol, lo defer conn.Close() for { - req := conncheck.NewRequest() + req := connectivity.NewRequest() data, err := json.Marshal(req) if err != nil { log.WithError(err).Fatal("Failed to marshal data") @@ -270,7 +270,7 @@ func tryConnect(remoteIpAddr, remotePort, sourceIpAddr, sourcePort, protocol, lo log.Fatalf("From address %+v does not match remoteAddr %+v", from, remoteAddrResolved) } - var resp conncheck.Response + var resp connectivity.Response err = json.Unmarshal(bufIn[:n], &resp) if err != nil { log.WithError(err).Fatal("Failed to read response")