From 455421111dd80bb9b0601f8e1055bbe341b3490b Mon Sep 17 00:00:00 2001 From: Kalimuthu Velappan Date: Fri, 4 Dec 2020 00:39:12 -0800 Subject: [PATCH] eBPF library support for SONiC applications It provides a generic library support for filtering the netlink message using eBPF based packet filtering. Linux netlink messaging system is a broadcast domain, every netlink message is broadcasted to all the subscribed appplications regardless whether the application is interested in it or not. In a scaled scenario, more than 1 million message is being broadcasted across applications. This framework helps to filter the unwanted netlink message at the kernel space itself and eliminates the unnecessary passing of message to the user space application and then it get dropped. This improves the system performance and scale the system to higher level. --- dockers/docker-base-stretch/Dockerfile.j2 | 3 + files/build_templates/docker_image_ctl.j2 | 2 + rules/libebpf.dep | 10 + rules/libebpf.mk | 13 + sonic-slave-stretch/Dockerfile.j2 | 5 + src/libebpf/Makefile | 14 + src/libebpf/src/Makefile.am | 3 + src/libebpf/src/autogen.sh | 6 + src/libebpf/src/config/config.guess | 1462 +++++++++++++++++ src/libebpf/src/configure.ac | 48 + src/libebpf/src/debian/changelog | 6 + src/libebpf/src/debian/compat | 1 + src/libebpf/src/debian/control | 12 + src/libebpf/src/debian/rules | 25 + src/libebpf/src/libutil/Makefile.am | 23 + src/libebpf/src/libutil/bpf_load.c | 441 +++++ src/libebpf/src/libutil/ebpf/bpf_helpers.h | 163 ++ src/libebpf/src/libutil/ebpf/bpf_load.h | 33 + src/libebpf/src/libutil/ebpf/libbpf.h | 209 +++ src/libebpf/src/libutil/ebpf/libebpf.h | 33 + src/libebpf/src/libutil/ebpftest.c | 100 ++ src/libebpf/src/libutil/libbpf.c | 155 ++ src/libebpf/src/libutil/libebpf.c | 99 ++ .../src/samples/ebpf-kern-mod/Makefile | 63 + .../src/samples/ebpf-kern-mod/ebpf_timer.c | 40 + .../src/samples/ebpf-kern-mod/ebpftest.c | 172 ++ .../src/samples/ebpf-kern-mod/ebpftest.h | 99 ++ .../src/samples/ebpf-user-mod/Makefile | 84 + .../src/samples/ebpf-user-mod/ebpftest.c | 174 ++ .../src/samples/ebpf-user-mod/ebpftest.h | 101 ++ 30 files changed, 3599 insertions(+) create mode 100644 rules/libebpf.dep create mode 100644 rules/libebpf.mk create mode 100644 src/libebpf/Makefile create mode 100644 src/libebpf/src/Makefile.am create mode 100755 src/libebpf/src/autogen.sh create mode 100755 src/libebpf/src/config/config.guess create mode 100644 src/libebpf/src/configure.ac create mode 100644 src/libebpf/src/debian/changelog create mode 100644 src/libebpf/src/debian/compat create mode 100644 src/libebpf/src/debian/control create mode 100755 src/libebpf/src/debian/rules create mode 100644 src/libebpf/src/libutil/Makefile.am create mode 100755 src/libebpf/src/libutil/bpf_load.c create mode 100755 src/libebpf/src/libutil/ebpf/bpf_helpers.h create mode 100755 src/libebpf/src/libutil/ebpf/bpf_load.h create mode 100755 src/libebpf/src/libutil/ebpf/libbpf.h create mode 100644 src/libebpf/src/libutil/ebpf/libebpf.h create mode 100644 src/libebpf/src/libutil/ebpftest.c create mode 100755 src/libebpf/src/libutil/libbpf.c create mode 100644 src/libebpf/src/libutil/libebpf.c create mode 100755 src/libebpf/src/samples/ebpf-kern-mod/Makefile create mode 100644 src/libebpf/src/samples/ebpf-kern-mod/ebpf_timer.c create mode 100755 src/libebpf/src/samples/ebpf-kern-mod/ebpftest.c create mode 100755 src/libebpf/src/samples/ebpf-kern-mod/ebpftest.h create mode 100644 src/libebpf/src/samples/ebpf-user-mod/Makefile create mode 100755 src/libebpf/src/samples/ebpf-user-mod/ebpftest.c create mode 100755 src/libebpf/src/samples/ebpf-user-mod/ebpftest.h diff --git a/dockers/docker-base-stretch/Dockerfile.j2 b/dockers/docker-base-stretch/Dockerfile.j2 index aa6eda8a955c..3bd6ecf8c77e 100644 --- a/dockers/docker-base-stretch/Dockerfile.j2 +++ b/dockers/docker-base-stretch/Dockerfile.j2 @@ -111,6 +111,9 @@ RUN apt-get -y purge \ {{ install_debian_packages(docker_base_stretch_debs.split(' ')) }} {%- endif %} +# Base packages for bpf +RUN apt-get install -f -y libbpf-dev libelf-dev + # Clean up apt # Remove /var/lib/apt/lists/*, could be obsoleted for derived images RUN apt-get clean -y && \ diff --git a/files/build_templates/docker_image_ctl.j2 b/files/build_templates/docker_image_ctl.j2 index 9c7afa84b252..a3311293deaa 100644 --- a/files/build_templates/docker_image_ctl.j2 +++ b/files/build_templates/docker_image_ctl.j2 @@ -390,6 +390,8 @@ start() { {%- if sonic_asic_platform != "mellanox" %} --tmpfs /tmp \ {%- endif %} + -v /sys/kernel/debug:/sys/kernel/debug \ + --ulimit memlock=67108864:67108864 \ --tmpfs /var/tmp \ --env "NAMESPACE_ID"="$DEV" \ --env "NAMESPACE_PREFIX"="$NAMESPACE_PREFIX" \ diff --git a/rules/libebpf.dep b/rules/libebpf.dep new file mode 100644 index 000000000000..fb2b83a4b8f0 --- /dev/null +++ b/rules/libebpf.dep @@ -0,0 +1,10 @@ + +SPATH := $($(LIBEBPF)_SRC_PATH) +DEP_FILES := $(SONIC_COMMON_FILES_LIST) rules/libebpf.mk rules/libebpf.dep +DEP_FILES += $(SONIC_COMMON_BASE_FILES_LIST) +DEP_FILES += $(shell git ls-files $(SPATH)) + +$(LIBEBPF)_CACHE_MODE := GIT_CONTENT_SHA +$(LIBEBPF)_DEP_FLAGS := $(SONIC_COMMON_FLAGS_LIST) +$(LIBEBPF)_DEP_FILES := $(DEP_FILES) + diff --git a/rules/libebpf.mk b/rules/libebpf.mk new file mode 100644 index 000000000000..f4c229a69f73 --- /dev/null +++ b/rules/libebpf.mk @@ -0,0 +1,13 @@ +# BPF package +BPF_VERSION := 1.0.0 +LIBEBPF = libebpf_$(BPF_VERSION)_$(CONFIGURED_ARCH).deb +$(LIBEBPF)_SRC_PATH = $(SRC_PATH)/libebpf +$(LIBEBPF)_DEPENDS += $(LINUX_HEADERS) $(LINUX_COMMON_HEADERS) +SONIC_MAKE_DEBS += $(LIBEBPF) + +LIBEBPF_DBG = libebpf-dbgsym_$(BPF_VERSION)_$(CONFIGURED_ARCH).deb +$(LIBEBPF_DBG)_DEPENDS += $(LIBEBPF) +$(eval $(call add_derived_package,$(LIBEBPF),$(LIBEBPF_DBG))) + +export LIBEBPF LIBEBPF_DBG BPF_VERSION + diff --git a/sonic-slave-stretch/Dockerfile.j2 b/sonic-slave-stretch/Dockerfile.j2 index c1e9d10951e0..cdc3a9a68c53 100644 --- a/sonic-slave-stretch/Dockerfile.j2 +++ b/sonic-slave-stretch/Dockerfile.j2 @@ -291,6 +291,11 @@ RUN apt-get update && apt-get install -y \ # For iptables libnetfilter-conntrack-dev \ libnftnl-dev \ +# eBPF tools + clang-6.0 \ + llvm-6.0 \ + libbpf-dev \ + libelf-dev \ # For SAI3.7 protobuf-compiler \ libprotobuf-dev \ diff --git a/src/libebpf/Makefile b/src/libebpf/Makefile new file mode 100644 index 000000000000..3af7747b43a5 --- /dev/null +++ b/src/libebpf/Makefile @@ -0,0 +1,14 @@ +SHELL = /bin/bash +.ONESHELL: +.SHELLFLAGS += -e + +MAIN_TARGET := libebpf_$(EBPF_VERSION)_$(CONFIGURED_ARCH).deb + +$(addprefix $(DEST)/, $(MAIN_TARGET)): $(DEST)/% : + + pushd src + [ ! -f ./autogen.sh ] || ./autogen.sh + dpkg-buildpackage -us -uc -b -j$(SONIC_CONFIG_MAKE_JOBS) + popd + + mv $(LIBEBPF_DBG) $* $(DEST)/ diff --git a/src/libebpf/src/Makefile.am b/src/libebpf/src/Makefile.am new file mode 100644 index 000000000000..d0a61d338b5a --- /dev/null +++ b/src/libebpf/src/Makefile.am @@ -0,0 +1,3 @@ +ACLOCAL_AMFLAGS = -I m4 + +SUBDIRS = libutil samples/ebpf-kern-mod samples/ebpf-user-mod diff --git a/src/libebpf/src/autogen.sh b/src/libebpf/src/autogen.sh new file mode 100755 index 000000000000..c8d0bbe4a251 --- /dev/null +++ b/src/libebpf/src/autogen.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +libtoolize --force --copy && +autoreconf --force --install -I m4 +rm -Rf autom4te.cache + diff --git a/src/libebpf/src/config/config.guess b/src/libebpf/src/config/config.guess new file mode 100755 index 000000000000..2e9ad7fe8189 --- /dev/null +++ b/src/libebpf/src/config/config.guess @@ -0,0 +1,1462 @@ +#! /bin/sh +# Attempt to guess a canonical system name. +# Copyright 1992-2016 Free Software Foundation, Inc. + +timestamp='2016-10-02' + +# This file 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 3 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, see . +# +# As a special exception to the GNU General Public License, if you +# distribute this file as part of a program that contains a +# configuration script generated by Autoconf, you may include it under +# the same distribution terms that you use for the rest of that +# program. This Exception is an additional permission under section 7 +# of the GNU General Public License, version 3 ("GPLv3"). +# +# Originally written by Per Bothner; maintained since 2000 by Ben Elliston. +# +# You can get the latest version of this script from: +# http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess +# +# Please send patches to . + + +me=`echo "$0" | sed -e 's,.*/,,'` + +usage="\ +Usage: $0 [OPTION] + +Output the configuration name of the system \`$me' is run on. + +Operation modes: + -h, --help print this help, then exit + -t, --time-stamp print date of last modification, then exit + -v, --version print version number, then exit + +Report bugs and patches to ." + +version="\ +GNU config.guess ($timestamp) + +Originally written by Per Bothner. +Copyright 1992-2016 Free Software Foundation, Inc. + +This is free software; see the source for copying conditions. There is NO +warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE." + +help=" +Try \`$me --help' for more information." + +# Parse command line +while test $# -gt 0 ; do + case $1 in + --time-stamp | --time* | -t ) + echo "$timestamp" ; exit ;; + --version | -v ) + echo "$version" ; exit ;; + --help | --h* | -h ) + echo "$usage"; exit ;; + -- ) # Stop option processing + shift; break ;; + - ) # Use stdin as input. + break ;; + -* ) + echo "$me: invalid option $1$help" >&2 + exit 1 ;; + * ) + break ;; + esac +done + +if test $# != 0; then + echo "$me: too many arguments$help" >&2 + exit 1 +fi + +trap 'exit 1' 1 2 15 + +# CC_FOR_BUILD -- compiler used by this script. Note that the use of a +# compiler to aid in system detection is discouraged as it requires +# temporary files to be created and, as you can see below, it is a +# headache to deal with in a portable fashion. + +# Historically, `CC_FOR_BUILD' used to be named `HOST_CC'. We still +# use `HOST_CC' if defined, but it is deprecated. + +# Portable tmp directory creation inspired by the Autoconf team. + +set_cc_for_build=' +trap "exitcode=\$?; (rm -f \$tmpfiles 2>/dev/null; rmdir \$tmp 2>/dev/null) && exit \$exitcode" 0 ; +trap "rm -f \$tmpfiles 2>/dev/null; rmdir \$tmp 2>/dev/null; exit 1" 1 2 13 15 ; +: ${TMPDIR=/tmp} ; + { tmp=`(umask 077 && mktemp -d "$TMPDIR/cgXXXXXX") 2>/dev/null` && test -n "$tmp" && test -d "$tmp" ; } || + { test -n "$RANDOM" && tmp=$TMPDIR/cg$$-$RANDOM && (umask 077 && mkdir $tmp) ; } || + { tmp=$TMPDIR/cg-$$ && (umask 077 && mkdir $tmp) && echo "Warning: creating insecure temp directory" >&2 ; } || + { echo "$me: cannot create a temporary directory in $TMPDIR" >&2 ; exit 1 ; } ; +dummy=$tmp/dummy ; +tmpfiles="$dummy.c $dummy.o $dummy.rel $dummy" ; +case $CC_FOR_BUILD,$HOST_CC,$CC in + ,,) echo "int x;" > $dummy.c ; + for c in cc gcc c89 c99 ; do + if ($c -c -o $dummy.o $dummy.c) >/dev/null 2>&1 ; then + CC_FOR_BUILD="$c"; break ; + fi ; + done ; + if test x"$CC_FOR_BUILD" = x ; then + CC_FOR_BUILD=no_compiler_found ; + fi + ;; + ,,*) CC_FOR_BUILD=$CC ;; + ,*,*) CC_FOR_BUILD=$HOST_CC ;; +esac ; set_cc_for_build= ;' + +# This is needed to find uname on a Pyramid OSx when run in the BSD universe. +# (ghazi@noc.rutgers.edu 1994-08-24) +if (test -f /.attbin/uname) >/dev/null 2>&1 ; then + PATH=$PATH:/.attbin ; export PATH +fi + +UNAME_MACHINE=`(uname -m) 2>/dev/null` || UNAME_MACHINE=unknown +UNAME_RELEASE=`(uname -r) 2>/dev/null` || UNAME_RELEASE=unknown +UNAME_SYSTEM=`(uname -s) 2>/dev/null` || UNAME_SYSTEM=unknown +UNAME_VERSION=`(uname -v) 2>/dev/null` || UNAME_VERSION=unknown + +case "${UNAME_SYSTEM}" in +Linux|GNU|GNU/*) + # If the system lacks a compiler, then just pick glibc. + # We could probably try harder. + LIBC=gnu + + eval $set_cc_for_build + cat <<-EOF > $dummy.c + #include + #if defined(__UCLIBC__) + LIBC=uclibc + #elif defined(__dietlibc__) + LIBC=dietlibc + #else + LIBC=gnu + #endif + EOF + eval `$CC_FOR_BUILD -E $dummy.c 2>/dev/null | grep '^LIBC' | sed 's, ,,g'` + ;; +esac + +# Note: order is significant - the case branches are not exclusive. + +case "${UNAME_MACHINE}:${UNAME_SYSTEM}:${UNAME_RELEASE}:${UNAME_VERSION}" in + *:NetBSD:*:*) + # NetBSD (nbsd) targets should (where applicable) match one or + # more of the tuples: *-*-netbsdelf*, *-*-netbsdaout*, + # *-*-netbsdecoff* and *-*-netbsd*. For targets that recently + # switched to ELF, *-*-netbsd* would select the old + # object file format. This provides both forward + # compatibility and a consistent mechanism for selecting the + # object file format. + # + # Note: NetBSD doesn't particularly care about the vendor + # portion of the name. We always set it to "unknown". + sysctl="sysctl -n hw.machine_arch" + UNAME_MACHINE_ARCH=`(uname -p 2>/dev/null || \ + /sbin/$sysctl 2>/dev/null || \ + /usr/sbin/$sysctl 2>/dev/null || \ + echo unknown)` + case "${UNAME_MACHINE_ARCH}" in + armeb) machine=armeb-unknown ;; + arm*) machine=arm-unknown ;; + sh3el) machine=shl-unknown ;; + sh3eb) machine=sh-unknown ;; + sh5el) machine=sh5le-unknown ;; + earmv*) + arch=`echo ${UNAME_MACHINE_ARCH} | sed -e 's,^e\(armv[0-9]\).*$,\1,'` + endian=`echo ${UNAME_MACHINE_ARCH} | sed -ne 's,^.*\(eb\)$,\1,p'` + machine=${arch}${endian}-unknown + ;; + *) machine=${UNAME_MACHINE_ARCH}-unknown ;; + esac + # The Operating System including object format, if it has switched + # to ELF recently (or will in the future) and ABI. + case "${UNAME_MACHINE_ARCH}" in + earm*) + os=netbsdelf + ;; + arm*|i386|m68k|ns32k|sh3*|sparc|vax) + eval $set_cc_for_build + if echo __ELF__ | $CC_FOR_BUILD -E - 2>/dev/null \ + | grep -q __ELF__ + then + # Once all utilities can be ECOFF (netbsdecoff) or a.out (netbsdaout). + # Return netbsd for either. FIX? + os=netbsd + else + os=netbsdelf + fi + ;; + *) + os=netbsd + ;; + esac + # Determine ABI tags. + case "${UNAME_MACHINE_ARCH}" in + earm*) + expr='s/^earmv[0-9]/-eabi/;s/eb$//' + abi=`echo ${UNAME_MACHINE_ARCH} | sed -e "$expr"` + ;; + esac + # The OS release + # Debian GNU/NetBSD machines have a different userland, and + # thus, need a distinct triplet. However, they do not need + # kernel version information, so it can be replaced with a + # suitable tag, in the style of linux-gnu. + case "${UNAME_VERSION}" in + Debian*) + release='-gnu' + ;; + *) + release=`echo ${UNAME_RELEASE} | sed -e 's/[-_].*//' | cut -d. -f1,2` + ;; + esac + # Since CPU_TYPE-MANUFACTURER-KERNEL-OPERATING_SYSTEM: + # contains redundant information, the shorter form: + # CPU_TYPE-MANUFACTURER-OPERATING_SYSTEM is used. + echo "${machine}-${os}${release}${abi}" + exit ;; + *:Bitrig:*:*) + UNAME_MACHINE_ARCH=`arch | sed 's/Bitrig.//'` + echo ${UNAME_MACHINE_ARCH}-unknown-bitrig${UNAME_RELEASE} + exit ;; + *:OpenBSD:*:*) + UNAME_MACHINE_ARCH=`arch | sed 's/OpenBSD.//'` + echo ${UNAME_MACHINE_ARCH}-unknown-openbsd${UNAME_RELEASE} + exit ;; + *:LibertyBSD:*:*) + UNAME_MACHINE_ARCH=`arch | sed 's/^.*BSD\.//'` + echo ${UNAME_MACHINE_ARCH}-unknown-libertybsd${UNAME_RELEASE} + exit ;; + *:ekkoBSD:*:*) + echo ${UNAME_MACHINE}-unknown-ekkobsd${UNAME_RELEASE} + exit ;; + *:SolidBSD:*:*) + echo ${UNAME_MACHINE}-unknown-solidbsd${UNAME_RELEASE} + exit ;; + macppc:MirBSD:*:*) + echo powerpc-unknown-mirbsd${UNAME_RELEASE} + exit ;; + *:MirBSD:*:*) + echo ${UNAME_MACHINE}-unknown-mirbsd${UNAME_RELEASE} + exit ;; + *:Sortix:*:*) + echo ${UNAME_MACHINE}-unknown-sortix + exit ;; + alpha:OSF1:*:*) + case $UNAME_RELEASE in + *4.0) + UNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $3}'` + ;; + *5.*) + UNAME_RELEASE=`/usr/sbin/sizer -v | awk '{print $4}'` + ;; + esac + # According to Compaq, /usr/sbin/psrinfo has been available on + # OSF/1 and Tru64 systems produced since 1995. I hope that + # covers most systems running today. This code pipes the CPU + # types through head -n 1, so we only detect the type of CPU 0. + ALPHA_CPU_TYPE=`/usr/sbin/psrinfo -v | sed -n -e 's/^ The alpha \(.*\) processor.*$/\1/p' | head -n 1` + case "$ALPHA_CPU_TYPE" in + "EV4 (21064)") + UNAME_MACHINE=alpha ;; + "EV4.5 (21064)") + UNAME_MACHINE=alpha ;; + "LCA4 (21066/21068)") + UNAME_MACHINE=alpha ;; + "EV5 (21164)") + UNAME_MACHINE=alphaev5 ;; + "EV5.6 (21164A)") + UNAME_MACHINE=alphaev56 ;; + "EV5.6 (21164PC)") + UNAME_MACHINE=alphapca56 ;; + "EV5.7 (21164PC)") + UNAME_MACHINE=alphapca57 ;; + "EV6 (21264)") + UNAME_MACHINE=alphaev6 ;; + "EV6.7 (21264A)") + UNAME_MACHINE=alphaev67 ;; + "EV6.8CB (21264C)") + UNAME_MACHINE=alphaev68 ;; + "EV6.8AL (21264B)") + UNAME_MACHINE=alphaev68 ;; + "EV6.8CX (21264D)") + UNAME_MACHINE=alphaev68 ;; + "EV6.9A (21264/EV69A)") + UNAME_MACHINE=alphaev69 ;; + "EV7 (21364)") + UNAME_MACHINE=alphaev7 ;; + "EV7.9 (21364A)") + UNAME_MACHINE=alphaev79 ;; + esac + # A Pn.n version is a patched version. + # A Vn.n version is a released version. + # A Tn.n version is a released field test version. + # A Xn.n version is an unreleased experimental baselevel. + # 1.2 uses "1.2" for uname -r. + echo ${UNAME_MACHINE}-dec-osf`echo ${UNAME_RELEASE} | sed -e 's/^[PVTX]//' | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz` + # Reset EXIT trap before exiting to avoid spurious non-zero exit code. + exitcode=$? + trap '' 0 + exit $exitcode ;; + Alpha\ *:Windows_NT*:*) + # How do we know it's Interix rather than the generic POSIX subsystem? + # Should we change UNAME_MACHINE based on the output of uname instead + # of the specific Alpha model? + echo alpha-pc-interix + exit ;; + 21064:Windows_NT:50:3) + echo alpha-dec-winnt3.5 + exit ;; + Amiga*:UNIX_System_V:4.0:*) + echo m68k-unknown-sysv4 + exit ;; + *:[Aa]miga[Oo][Ss]:*:*) + echo ${UNAME_MACHINE}-unknown-amigaos + exit ;; + *:[Mm]orph[Oo][Ss]:*:*) + echo ${UNAME_MACHINE}-unknown-morphos + exit ;; + *:OS/390:*:*) + echo i370-ibm-openedition + exit ;; + *:z/VM:*:*) + echo s390-ibm-zvmoe + exit ;; + *:OS400:*:*) + echo powerpc-ibm-os400 + exit ;; + arm:RISC*:1.[012]*:*|arm:riscix:1.[012]*:*) + echo arm-acorn-riscix${UNAME_RELEASE} + exit ;; + arm*:riscos:*:*|arm*:RISCOS:*:*) + echo arm-unknown-riscos + exit ;; + SR2?01:HI-UX/MPP:*:* | SR8000:HI-UX/MPP:*:*) + echo hppa1.1-hitachi-hiuxmpp + exit ;; + Pyramid*:OSx*:*:* | MIS*:OSx*:*:* | MIS*:SMP_DC-OSx*:*:*) + # akee@wpdis03.wpafb.af.mil (Earle F. Ake) contributed MIS and NILE. + if test "`(/bin/universe) 2>/dev/null`" = att ; then + echo pyramid-pyramid-sysv3 + else + echo pyramid-pyramid-bsd + fi + exit ;; + NILE*:*:*:dcosx) + echo pyramid-pyramid-svr4 + exit ;; + DRS?6000:unix:4.0:6*) + echo sparc-icl-nx6 + exit ;; + DRS?6000:UNIX_SV:4.2*:7* | DRS?6000:isis:4.2*:7*) + case `/usr/bin/uname -p` in + sparc) echo sparc-icl-nx7; exit ;; + esac ;; + s390x:SunOS:*:*) + echo ${UNAME_MACHINE}-ibm-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` + exit ;; + sun4H:SunOS:5.*:*) + echo sparc-hal-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` + exit ;; + sun4*:SunOS:5.*:* | tadpole*:SunOS:5.*:*) + echo sparc-sun-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` + exit ;; + i86pc:AuroraUX:5.*:* | i86xen:AuroraUX:5.*:*) + echo i386-pc-auroraux${UNAME_RELEASE} + exit ;; + i86pc:SunOS:5.*:* | i86xen:SunOS:5.*:*) + eval $set_cc_for_build + SUN_ARCH=i386 + # If there is a compiler, see if it is configured for 64-bit objects. + # Note that the Sun cc does not turn __LP64__ into 1 like gcc does. + # This test works for both compilers. + if [ "$CC_FOR_BUILD" != no_compiler_found ]; then + if (echo '#ifdef __amd64'; echo IS_64BIT_ARCH; echo '#endif') | \ + (CCOPTS="" $CC_FOR_BUILD -E - 2>/dev/null) | \ + grep IS_64BIT_ARCH >/dev/null + then + SUN_ARCH=x86_64 + fi + fi + echo ${SUN_ARCH}-pc-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` + exit ;; + sun4*:SunOS:6*:*) + # According to config.sub, this is the proper way to canonicalize + # SunOS6. Hard to guess exactly what SunOS6 will be like, but + # it's likely to be more like Solaris than SunOS4. + echo sparc-sun-solaris3`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` + exit ;; + sun4*:SunOS:*:*) + case "`/usr/bin/arch -k`" in + Series*|S4*) + UNAME_RELEASE=`uname -v` + ;; + esac + # Japanese Language versions have a version number like `4.1.3-JL'. + echo sparc-sun-sunos`echo ${UNAME_RELEASE}|sed -e 's/-/_/'` + exit ;; + sun3*:SunOS:*:*) + echo m68k-sun-sunos${UNAME_RELEASE} + exit ;; + sun*:*:4.2BSD:*) + UNAME_RELEASE=`(sed 1q /etc/motd | awk '{print substr($5,1,3)}') 2>/dev/null` + test "x${UNAME_RELEASE}" = x && UNAME_RELEASE=3 + case "`/bin/arch`" in + sun3) + echo m68k-sun-sunos${UNAME_RELEASE} + ;; + sun4) + echo sparc-sun-sunos${UNAME_RELEASE} + ;; + esac + exit ;; + aushp:SunOS:*:*) + echo sparc-auspex-sunos${UNAME_RELEASE} + exit ;; + # The situation for MiNT is a little confusing. The machine name + # can be virtually everything (everything which is not + # "atarist" or "atariste" at least should have a processor + # > m68000). The system name ranges from "MiNT" over "FreeMiNT" + # to the lowercase version "mint" (or "freemint"). Finally + # the system name "TOS" denotes a system which is actually not + # MiNT. But MiNT is downward compatible to TOS, so this should + # be no problem. + atarist[e]:*MiNT:*:* | atarist[e]:*mint:*:* | atarist[e]:*TOS:*:*) + echo m68k-atari-mint${UNAME_RELEASE} + exit ;; + atari*:*MiNT:*:* | atari*:*mint:*:* | atarist[e]:*TOS:*:*) + echo m68k-atari-mint${UNAME_RELEASE} + exit ;; + *falcon*:*MiNT:*:* | *falcon*:*mint:*:* | *falcon*:*TOS:*:*) + echo m68k-atari-mint${UNAME_RELEASE} + exit ;; + milan*:*MiNT:*:* | milan*:*mint:*:* | *milan*:*TOS:*:*) + echo m68k-milan-mint${UNAME_RELEASE} + exit ;; + hades*:*MiNT:*:* | hades*:*mint:*:* | *hades*:*TOS:*:*) + echo m68k-hades-mint${UNAME_RELEASE} + exit ;; + *:*MiNT:*:* | *:*mint:*:* | *:*TOS:*:*) + echo m68k-unknown-mint${UNAME_RELEASE} + exit ;; + m68k:machten:*:*) + echo m68k-apple-machten${UNAME_RELEASE} + exit ;; + powerpc:machten:*:*) + echo powerpc-apple-machten${UNAME_RELEASE} + exit ;; + RISC*:Mach:*:*) + echo mips-dec-mach_bsd4.3 + exit ;; + RISC*:ULTRIX:*:*) + echo mips-dec-ultrix${UNAME_RELEASE} + exit ;; + VAX*:ULTRIX*:*:*) + echo vax-dec-ultrix${UNAME_RELEASE} + exit ;; + 2020:CLIX:*:* | 2430:CLIX:*:*) + echo clipper-intergraph-clix${UNAME_RELEASE} + exit ;; + mips:*:*:UMIPS | mips:*:*:RISCos) + eval $set_cc_for_build + sed 's/^ //' << EOF >$dummy.c +#ifdef __cplusplus +#include /* for printf() prototype */ + int main (int argc, char *argv[]) { +#else + int main (argc, argv) int argc; char *argv[]; { +#endif + #if defined (host_mips) && defined (MIPSEB) + #if defined (SYSTYPE_SYSV) + printf ("mips-mips-riscos%ssysv\n", argv[1]); exit (0); + #endif + #if defined (SYSTYPE_SVR4) + printf ("mips-mips-riscos%ssvr4\n", argv[1]); exit (0); + #endif + #if defined (SYSTYPE_BSD43) || defined(SYSTYPE_BSD) + printf ("mips-mips-riscos%sbsd\n", argv[1]); exit (0); + #endif + #endif + exit (-1); + } +EOF + $CC_FOR_BUILD -o $dummy $dummy.c && + dummyarg=`echo "${UNAME_RELEASE}" | sed -n 's/\([0-9]*\).*/\1/p'` && + SYSTEM_NAME=`$dummy $dummyarg` && + { echo "$SYSTEM_NAME"; exit; } + echo mips-mips-riscos${UNAME_RELEASE} + exit ;; + Motorola:PowerMAX_OS:*:*) + echo powerpc-motorola-powermax + exit ;; + Motorola:*:4.3:PL8-*) + echo powerpc-harris-powermax + exit ;; + Night_Hawk:*:*:PowerMAX_OS | Synergy:PowerMAX_OS:*:*) + echo powerpc-harris-powermax + exit ;; + Night_Hawk:Power_UNIX:*:*) + echo powerpc-harris-powerunix + exit ;; + m88k:CX/UX:7*:*) + echo m88k-harris-cxux7 + exit ;; + m88k:*:4*:R4*) + echo m88k-motorola-sysv4 + exit ;; + m88k:*:3*:R3*) + echo m88k-motorola-sysv3 + exit ;; + AViiON:dgux:*:*) + # DG/UX returns AViiON for all architectures + UNAME_PROCESSOR=`/usr/bin/uname -p` + if [ $UNAME_PROCESSOR = mc88100 ] || [ $UNAME_PROCESSOR = mc88110 ] + then + if [ ${TARGET_BINARY_INTERFACE}x = m88kdguxelfx ] || \ + [ ${TARGET_BINARY_INTERFACE}x = x ] + then + echo m88k-dg-dgux${UNAME_RELEASE} + else + echo m88k-dg-dguxbcs${UNAME_RELEASE} + fi + else + echo i586-dg-dgux${UNAME_RELEASE} + fi + exit ;; + M88*:DolphinOS:*:*) # DolphinOS (SVR3) + echo m88k-dolphin-sysv3 + exit ;; + M88*:*:R3*:*) + # Delta 88k system running SVR3 + echo m88k-motorola-sysv3 + exit ;; + XD88*:*:*:*) # Tektronix XD88 system running UTekV (SVR3) + echo m88k-tektronix-sysv3 + exit ;; + Tek43[0-9][0-9]:UTek:*:*) # Tektronix 4300 system running UTek (BSD) + echo m68k-tektronix-bsd + exit ;; + *:IRIX*:*:*) + echo mips-sgi-irix`echo ${UNAME_RELEASE}|sed -e 's/-/_/g'` + exit ;; + ????????:AIX?:[12].1:2) # AIX 2.2.1 or AIX 2.1.1 is RT/PC AIX. + echo romp-ibm-aix # uname -m gives an 8 hex-code CPU id + exit ;; # Note that: echo "'`uname -s`'" gives 'AIX ' + i*86:AIX:*:*) + echo i386-ibm-aix + exit ;; + ia64:AIX:*:*) + if [ -x /usr/bin/oslevel ] ; then + IBM_REV=`/usr/bin/oslevel` + else + IBM_REV=${UNAME_VERSION}.${UNAME_RELEASE} + fi + echo ${UNAME_MACHINE}-ibm-aix${IBM_REV} + exit ;; + *:AIX:2:3) + if grep bos325 /usr/include/stdio.h >/dev/null 2>&1; then + eval $set_cc_for_build + sed 's/^ //' << EOF >$dummy.c + #include + + main() + { + if (!__power_pc()) + exit(1); + puts("powerpc-ibm-aix3.2.5"); + exit(0); + } +EOF + if $CC_FOR_BUILD -o $dummy $dummy.c && SYSTEM_NAME=`$dummy` + then + echo "$SYSTEM_NAME" + else + echo rs6000-ibm-aix3.2.5 + fi + elif grep bos324 /usr/include/stdio.h >/dev/null 2>&1; then + echo rs6000-ibm-aix3.2.4 + else + echo rs6000-ibm-aix3.2 + fi + exit ;; + *:AIX:*:[4567]) + IBM_CPU_ID=`/usr/sbin/lsdev -C -c processor -S available | sed 1q | awk '{ print $1 }'` + if /usr/sbin/lsattr -El ${IBM_CPU_ID} | grep ' POWER' >/dev/null 2>&1; then + IBM_ARCH=rs6000 + else + IBM_ARCH=powerpc + fi + if [ -x /usr/bin/lslpp ] ; then + IBM_REV=`/usr/bin/lslpp -Lqc bos.rte.libc | + awk -F: '{ print $3 }' | sed s/[0-9]*$/0/` + else + IBM_REV=${UNAME_VERSION}.${UNAME_RELEASE} + fi + echo ${IBM_ARCH}-ibm-aix${IBM_REV} + exit ;; + *:AIX:*:*) + echo rs6000-ibm-aix + exit ;; + ibmrt:4.4BSD:*|romp-ibm:BSD:*) + echo romp-ibm-bsd4.4 + exit ;; + ibmrt:*BSD:*|romp-ibm:BSD:*) # covers RT/PC BSD and + echo romp-ibm-bsd${UNAME_RELEASE} # 4.3 with uname added to + exit ;; # report: romp-ibm BSD 4.3 + *:BOSX:*:*) + echo rs6000-bull-bosx + exit ;; + DPX/2?00:B.O.S.:*:*) + echo m68k-bull-sysv3 + exit ;; + 9000/[34]??:4.3bsd:1.*:*) + echo m68k-hp-bsd + exit ;; + hp300:4.4BSD:*:* | 9000/[34]??:4.3bsd:2.*:*) + echo m68k-hp-bsd4.4 + exit ;; + 9000/[34678]??:HP-UX:*:*) + HPUX_REV=`echo ${UNAME_RELEASE}|sed -e 's/[^.]*.[0B]*//'` + case "${UNAME_MACHINE}" in + 9000/31? ) HP_ARCH=m68000 ;; + 9000/[34]?? ) HP_ARCH=m68k ;; + 9000/[678][0-9][0-9]) + if [ -x /usr/bin/getconf ]; then + sc_cpu_version=`/usr/bin/getconf SC_CPU_VERSION 2>/dev/null` + sc_kernel_bits=`/usr/bin/getconf SC_KERNEL_BITS 2>/dev/null` + case "${sc_cpu_version}" in + 523) HP_ARCH=hppa1.0 ;; # CPU_PA_RISC1_0 + 528) HP_ARCH=hppa1.1 ;; # CPU_PA_RISC1_1 + 532) # CPU_PA_RISC2_0 + case "${sc_kernel_bits}" in + 32) HP_ARCH=hppa2.0n ;; + 64) HP_ARCH=hppa2.0w ;; + '') HP_ARCH=hppa2.0 ;; # HP-UX 10.20 + esac ;; + esac + fi + if [ "${HP_ARCH}" = "" ]; then + eval $set_cc_for_build + sed 's/^ //' << EOF >$dummy.c + + #define _HPUX_SOURCE + #include + #include + + int main () + { + #if defined(_SC_KERNEL_BITS) + long bits = sysconf(_SC_KERNEL_BITS); + #endif + long cpu = sysconf (_SC_CPU_VERSION); + + switch (cpu) + { + case CPU_PA_RISC1_0: puts ("hppa1.0"); break; + case CPU_PA_RISC1_1: puts ("hppa1.1"); break; + case CPU_PA_RISC2_0: + #if defined(_SC_KERNEL_BITS) + switch (bits) + { + case 64: puts ("hppa2.0w"); break; + case 32: puts ("hppa2.0n"); break; + default: puts ("hppa2.0"); break; + } break; + #else /* !defined(_SC_KERNEL_BITS) */ + puts ("hppa2.0"); break; + #endif + default: puts ("hppa1.0"); break; + } + exit (0); + } +EOF + (CCOPTS="" $CC_FOR_BUILD -o $dummy $dummy.c 2>/dev/null) && HP_ARCH=`$dummy` + test -z "$HP_ARCH" && HP_ARCH=hppa + fi ;; + esac + if [ ${HP_ARCH} = hppa2.0w ] + then + eval $set_cc_for_build + + # hppa2.0w-hp-hpux* has a 64-bit kernel and a compiler generating + # 32-bit code. hppa64-hp-hpux* has the same kernel and a compiler + # generating 64-bit code. GNU and HP use different nomenclature: + # + # $ CC_FOR_BUILD=cc ./config.guess + # => hppa2.0w-hp-hpux11.23 + # $ CC_FOR_BUILD="cc +DA2.0w" ./config.guess + # => hppa64-hp-hpux11.23 + + if echo __LP64__ | (CCOPTS="" $CC_FOR_BUILD -E - 2>/dev/null) | + grep -q __LP64__ + then + HP_ARCH=hppa2.0w + else + HP_ARCH=hppa64 + fi + fi + echo ${HP_ARCH}-hp-hpux${HPUX_REV} + exit ;; + ia64:HP-UX:*:*) + HPUX_REV=`echo ${UNAME_RELEASE}|sed -e 's/[^.]*.[0B]*//'` + echo ia64-hp-hpux${HPUX_REV} + exit ;; + 3050*:HI-UX:*:*) + eval $set_cc_for_build + sed 's/^ //' << EOF >$dummy.c + #include + int + main () + { + long cpu = sysconf (_SC_CPU_VERSION); + /* The order matters, because CPU_IS_HP_MC68K erroneously returns + true for CPU_PA_RISC1_0. CPU_IS_PA_RISC returns correct + results, however. */ + if (CPU_IS_PA_RISC (cpu)) + { + switch (cpu) + { + case CPU_PA_RISC1_0: puts ("hppa1.0-hitachi-hiuxwe2"); break; + case CPU_PA_RISC1_1: puts ("hppa1.1-hitachi-hiuxwe2"); break; + case CPU_PA_RISC2_0: puts ("hppa2.0-hitachi-hiuxwe2"); break; + default: puts ("hppa-hitachi-hiuxwe2"); break; + } + } + else if (CPU_IS_HP_MC68K (cpu)) + puts ("m68k-hitachi-hiuxwe2"); + else puts ("unknown-hitachi-hiuxwe2"); + exit (0); + } +EOF + $CC_FOR_BUILD -o $dummy $dummy.c && SYSTEM_NAME=`$dummy` && + { echo "$SYSTEM_NAME"; exit; } + echo unknown-hitachi-hiuxwe2 + exit ;; + 9000/7??:4.3bsd:*:* | 9000/8?[79]:4.3bsd:*:* ) + echo hppa1.1-hp-bsd + exit ;; + 9000/8??:4.3bsd:*:*) + echo hppa1.0-hp-bsd + exit ;; + *9??*:MPE/iX:*:* | *3000*:MPE/iX:*:*) + echo hppa1.0-hp-mpeix + exit ;; + hp7??:OSF1:*:* | hp8?[79]:OSF1:*:* ) + echo hppa1.1-hp-osf + exit ;; + hp8??:OSF1:*:*) + echo hppa1.0-hp-osf + exit ;; + i*86:OSF1:*:*) + if [ -x /usr/sbin/sysversion ] ; then + echo ${UNAME_MACHINE}-unknown-osf1mk + else + echo ${UNAME_MACHINE}-unknown-osf1 + fi + exit ;; + parisc*:Lites*:*:*) + echo hppa1.1-hp-lites + exit ;; + C1*:ConvexOS:*:* | convex:ConvexOS:C1*:*) + echo c1-convex-bsd + exit ;; + C2*:ConvexOS:*:* | convex:ConvexOS:C2*:*) + if getsysinfo -f scalar_acc + then echo c32-convex-bsd + else echo c2-convex-bsd + fi + exit ;; + C34*:ConvexOS:*:* | convex:ConvexOS:C34*:*) + echo c34-convex-bsd + exit ;; + C38*:ConvexOS:*:* | convex:ConvexOS:C38*:*) + echo c38-convex-bsd + exit ;; + C4*:ConvexOS:*:* | convex:ConvexOS:C4*:*) + echo c4-convex-bsd + exit ;; + CRAY*Y-MP:*:*:*) + echo ymp-cray-unicos${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' + exit ;; + CRAY*[A-Z]90:*:*:*) + echo ${UNAME_MACHINE}-cray-unicos${UNAME_RELEASE} \ + | sed -e 's/CRAY.*\([A-Z]90\)/\1/' \ + -e y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/ \ + -e 's/\.[^.]*$/.X/' + exit ;; + CRAY*TS:*:*:*) + echo t90-cray-unicos${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' + exit ;; + CRAY*T3E:*:*:*) + echo alphaev5-cray-unicosmk${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' + exit ;; + CRAY*SV1:*:*:*) + echo sv1-cray-unicos${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' + exit ;; + *:UNICOS/mp:*:*) + echo craynv-cray-unicosmp${UNAME_RELEASE} | sed -e 's/\.[^.]*$/.X/' + exit ;; + F30[01]:UNIX_System_V:*:* | F700:UNIX_System_V:*:*) + FUJITSU_PROC=`uname -m | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz` + FUJITSU_SYS=`uname -p | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/\///'` + FUJITSU_REL=`echo ${UNAME_RELEASE} | sed -e 's/ /_/'` + echo "${FUJITSU_PROC}-fujitsu-${FUJITSU_SYS}${FUJITSU_REL}" + exit ;; + 5000:UNIX_System_V:4.*:*) + FUJITSU_SYS=`uname -p | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/\///'` + FUJITSU_REL=`echo ${UNAME_RELEASE} | tr ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz | sed -e 's/ /_/'` + echo "sparc-fujitsu-${FUJITSU_SYS}${FUJITSU_REL}" + exit ;; + i*86:BSD/386:*:* | i*86:BSD/OS:*:* | *:Ascend\ Embedded/OS:*:*) + echo ${UNAME_MACHINE}-pc-bsdi${UNAME_RELEASE} + exit ;; + sparc*:BSD/OS:*:*) + echo sparc-unknown-bsdi${UNAME_RELEASE} + exit ;; + *:BSD/OS:*:*) + echo ${UNAME_MACHINE}-unknown-bsdi${UNAME_RELEASE} + exit ;; + *:FreeBSD:*:*) + UNAME_PROCESSOR=`/usr/bin/uname -p` + case ${UNAME_PROCESSOR} in + amd64) + echo x86_64-unknown-freebsd`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'` ;; + *) + echo ${UNAME_PROCESSOR}-unknown-freebsd`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'` ;; + esac + exit ;; + i*:CYGWIN*:*) + echo ${UNAME_MACHINE}-pc-cygwin + exit ;; + *:MINGW64*:*) + echo ${UNAME_MACHINE}-pc-mingw64 + exit ;; + *:MINGW*:*) + echo ${UNAME_MACHINE}-pc-mingw32 + exit ;; + *:MSYS*:*) + echo ${UNAME_MACHINE}-pc-msys + exit ;; + i*:windows32*:*) + # uname -m includes "-pc" on this system. + echo ${UNAME_MACHINE}-mingw32 + exit ;; + i*:PW*:*) + echo ${UNAME_MACHINE}-pc-pw32 + exit ;; + *:Interix*:*) + case ${UNAME_MACHINE} in + x86) + echo i586-pc-interix${UNAME_RELEASE} + exit ;; + authenticamd | genuineintel | EM64T) + echo x86_64-unknown-interix${UNAME_RELEASE} + exit ;; + IA64) + echo ia64-unknown-interix${UNAME_RELEASE} + exit ;; + esac ;; + [345]86:Windows_95:* | [345]86:Windows_98:* | [345]86:Windows_NT:*) + echo i${UNAME_MACHINE}-pc-mks + exit ;; + 8664:Windows_NT:*) + echo x86_64-pc-mks + exit ;; + i*:Windows_NT*:* | Pentium*:Windows_NT*:*) + # How do we know it's Interix rather than the generic POSIX subsystem? + # It also conflicts with pre-2.0 versions of AT&T UWIN. Should we + # UNAME_MACHINE based on the output of uname instead of i386? + echo i586-pc-interix + exit ;; + i*:UWIN*:*) + echo ${UNAME_MACHINE}-pc-uwin + exit ;; + amd64:CYGWIN*:*:* | x86_64:CYGWIN*:*:*) + echo x86_64-unknown-cygwin + exit ;; + p*:CYGWIN*:*) + echo powerpcle-unknown-cygwin + exit ;; + prep*:SunOS:5.*:*) + echo powerpcle-unknown-solaris2`echo ${UNAME_RELEASE}|sed -e 's/[^.]*//'` + exit ;; + *:GNU:*:*) + # the GNU system + echo `echo ${UNAME_MACHINE}|sed -e 's,[-/].*$,,'`-unknown-${LIBC}`echo ${UNAME_RELEASE}|sed -e 's,/.*$,,'` + exit ;; + *:GNU/*:*:*) + # other systems with GNU libc and userland + echo ${UNAME_MACHINE}-unknown-`echo ${UNAME_SYSTEM} | sed 's,^[^/]*/,,' | tr "[:upper:]" "[:lower:]"``echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'`-${LIBC} + exit ;; + i*86:Minix:*:*) + echo ${UNAME_MACHINE}-pc-minix + exit ;; + aarch64:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + aarch64_be:Linux:*:*) + UNAME_MACHINE=aarch64_be + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + alpha:Linux:*:*) + case `sed -n '/^cpu model/s/^.*: \(.*\)/\1/p' < /proc/cpuinfo` in + EV5) UNAME_MACHINE=alphaev5 ;; + EV56) UNAME_MACHINE=alphaev56 ;; + PCA56) UNAME_MACHINE=alphapca56 ;; + PCA57) UNAME_MACHINE=alphapca56 ;; + EV6) UNAME_MACHINE=alphaev6 ;; + EV67) UNAME_MACHINE=alphaev67 ;; + EV68*) UNAME_MACHINE=alphaev68 ;; + esac + objdump --private-headers /bin/sh | grep -q ld.so.1 + if test "$?" = 0 ; then LIBC=gnulibc1 ; fi + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + arc:Linux:*:* | arceb:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + arm*:Linux:*:*) + eval $set_cc_for_build + if echo __ARM_EABI__ | $CC_FOR_BUILD -E - 2>/dev/null \ + | grep -q __ARM_EABI__ + then + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + else + if echo __ARM_PCS_VFP | $CC_FOR_BUILD -E - 2>/dev/null \ + | grep -q __ARM_PCS_VFP + then + echo ${UNAME_MACHINE}-unknown-linux-${LIBC}eabi + else + echo ${UNAME_MACHINE}-unknown-linux-${LIBC}eabihf + fi + fi + exit ;; + avr32*:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + cris:Linux:*:*) + echo ${UNAME_MACHINE}-axis-linux-${LIBC} + exit ;; + crisv32:Linux:*:*) + echo ${UNAME_MACHINE}-axis-linux-${LIBC} + exit ;; + e2k:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + frv:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + hexagon:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + i*86:Linux:*:*) + echo ${UNAME_MACHINE}-pc-linux-${LIBC} + exit ;; + ia64:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + k1om:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + m32r*:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + m68*:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + mips:Linux:*:* | mips64:Linux:*:*) + eval $set_cc_for_build + sed 's/^ //' << EOF >$dummy.c + #undef CPU + #undef ${UNAME_MACHINE} + #undef ${UNAME_MACHINE}el + #if defined(__MIPSEL__) || defined(__MIPSEL) || defined(_MIPSEL) || defined(MIPSEL) + CPU=${UNAME_MACHINE}el + #else + #if defined(__MIPSEB__) || defined(__MIPSEB) || defined(_MIPSEB) || defined(MIPSEB) + CPU=${UNAME_MACHINE} + #else + CPU= + #endif + #endif +EOF + eval `$CC_FOR_BUILD -E $dummy.c 2>/dev/null | grep '^CPU'` + test x"${CPU}" != x && { echo "${CPU}-unknown-linux-${LIBC}"; exit; } + ;; + mips64el:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + openrisc*:Linux:*:*) + echo or1k-unknown-linux-${LIBC} + exit ;; + or32:Linux:*:* | or1k*:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + padre:Linux:*:*) + echo sparc-unknown-linux-${LIBC} + exit ;; + parisc64:Linux:*:* | hppa64:Linux:*:*) + echo hppa64-unknown-linux-${LIBC} + exit ;; + parisc:Linux:*:* | hppa:Linux:*:*) + # Look for CPU level + case `grep '^cpu[^a-z]*:' /proc/cpuinfo 2>/dev/null | cut -d' ' -f2` in + PA7*) echo hppa1.1-unknown-linux-${LIBC} ;; + PA8*) echo hppa2.0-unknown-linux-${LIBC} ;; + *) echo hppa-unknown-linux-${LIBC} ;; + esac + exit ;; + ppc64:Linux:*:*) + echo powerpc64-unknown-linux-${LIBC} + exit ;; + ppc:Linux:*:*) + echo powerpc-unknown-linux-${LIBC} + exit ;; + ppc64le:Linux:*:*) + echo powerpc64le-unknown-linux-${LIBC} + exit ;; + ppcle:Linux:*:*) + echo powerpcle-unknown-linux-${LIBC} + exit ;; + riscv32:Linux:*:* | riscv64:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + s390:Linux:*:* | s390x:Linux:*:*) + echo ${UNAME_MACHINE}-ibm-linux-${LIBC} + exit ;; + sh64*:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + sh*:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + sparc:Linux:*:* | sparc64:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + tile*:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + vax:Linux:*:*) + echo ${UNAME_MACHINE}-dec-linux-${LIBC} + exit ;; + x86_64:Linux:*:*) + echo ${UNAME_MACHINE}-pc-linux-${LIBC} + exit ;; + xtensa*:Linux:*:*) + echo ${UNAME_MACHINE}-unknown-linux-${LIBC} + exit ;; + i*86:DYNIX/ptx:4*:*) + # ptx 4.0 does uname -s correctly, with DYNIX/ptx in there. + # earlier versions are messed up and put the nodename in both + # sysname and nodename. + echo i386-sequent-sysv4 + exit ;; + i*86:UNIX_SV:4.2MP:2.*) + # Unixware is an offshoot of SVR4, but it has its own version + # number series starting with 2... + # I am not positive that other SVR4 systems won't match this, + # I just have to hope. -- rms. + # Use sysv4.2uw... so that sysv4* matches it. + echo ${UNAME_MACHINE}-pc-sysv4.2uw${UNAME_VERSION} + exit ;; + i*86:OS/2:*:*) + # If we were able to find `uname', then EMX Unix compatibility + # is probably installed. + echo ${UNAME_MACHINE}-pc-os2-emx + exit ;; + i*86:XTS-300:*:STOP) + echo ${UNAME_MACHINE}-unknown-stop + exit ;; + i*86:atheos:*:*) + echo ${UNAME_MACHINE}-unknown-atheos + exit ;; + i*86:syllable:*:*) + echo ${UNAME_MACHINE}-pc-syllable + exit ;; + i*86:LynxOS:2.*:* | i*86:LynxOS:3.[01]*:* | i*86:LynxOS:4.[02]*:*) + echo i386-unknown-lynxos${UNAME_RELEASE} + exit ;; + i*86:*DOS:*:*) + echo ${UNAME_MACHINE}-pc-msdosdjgpp + exit ;; + i*86:*:4.*:* | i*86:SYSTEM_V:4.*:*) + UNAME_REL=`echo ${UNAME_RELEASE} | sed 's/\/MP$//'` + if grep Novell /usr/include/link.h >/dev/null 2>/dev/null; then + echo ${UNAME_MACHINE}-univel-sysv${UNAME_REL} + else + echo ${UNAME_MACHINE}-pc-sysv${UNAME_REL} + fi + exit ;; + i*86:*:5:[678]*) + # UnixWare 7.x, OpenUNIX and OpenServer 6. + case `/bin/uname -X | grep "^Machine"` in + *486*) UNAME_MACHINE=i486 ;; + *Pentium) UNAME_MACHINE=i586 ;; + *Pent*|*Celeron) UNAME_MACHINE=i686 ;; + esac + echo ${UNAME_MACHINE}-unknown-sysv${UNAME_RELEASE}${UNAME_SYSTEM}${UNAME_VERSION} + exit ;; + i*86:*:3.2:*) + if test -f /usr/options/cb.name; then + UNAME_REL=`sed -n 's/.*Version //p' /dev/null >/dev/null ; then + UNAME_REL=`(/bin/uname -X|grep Release|sed -e 's/.*= //')` + (/bin/uname -X|grep i80486 >/dev/null) && UNAME_MACHINE=i486 + (/bin/uname -X|grep '^Machine.*Pentium' >/dev/null) \ + && UNAME_MACHINE=i586 + (/bin/uname -X|grep '^Machine.*Pent *II' >/dev/null) \ + && UNAME_MACHINE=i686 + (/bin/uname -X|grep '^Machine.*Pentium Pro' >/dev/null) \ + && UNAME_MACHINE=i686 + echo ${UNAME_MACHINE}-pc-sco$UNAME_REL + else + echo ${UNAME_MACHINE}-pc-sysv32 + fi + exit ;; + pc:*:*:*) + # Left here for compatibility: + # uname -m prints for DJGPP always 'pc', but it prints nothing about + # the processor, so we play safe by assuming i586. + # Note: whatever this is, it MUST be the same as what config.sub + # prints for the "djgpp" host, or else GDB configure will decide that + # this is a cross-build. + echo i586-pc-msdosdjgpp + exit ;; + Intel:Mach:3*:*) + echo i386-pc-mach3 + exit ;; + paragon:*:*:*) + echo i860-intel-osf1 + exit ;; + i860:*:4.*:*) # i860-SVR4 + if grep Stardent /usr/include/sys/uadmin.h >/dev/null 2>&1 ; then + echo i860-stardent-sysv${UNAME_RELEASE} # Stardent Vistra i860-SVR4 + else # Add other i860-SVR4 vendors below as they are discovered. + echo i860-unknown-sysv${UNAME_RELEASE} # Unknown i860-SVR4 + fi + exit ;; + mini*:CTIX:SYS*5:*) + # "miniframe" + echo m68010-convergent-sysv + exit ;; + mc68k:UNIX:SYSTEM5:3.51m) + echo m68k-convergent-sysv + exit ;; + M680?0:D-NIX:5.3:*) + echo m68k-diab-dnix + exit ;; + M68*:*:R3V[5678]*:*) + test -r /sysV68 && { echo 'm68k-motorola-sysv'; exit; } ;; + 3[345]??:*:4.0:3.0 | 3[34]??A:*:4.0:3.0 | 3[34]??,*:*:4.0:3.0 | 3[34]??/*:*:4.0:3.0 | 4400:*:4.0:3.0 | 4850:*:4.0:3.0 | SKA40:*:4.0:3.0 | SDS2:*:4.0:3.0 | SHG2:*:4.0:3.0 | S7501*:*:4.0:3.0) + OS_REL='' + test -r /etc/.relid \ + && OS_REL=.`sed -n 's/[^ ]* [^ ]* \([0-9][0-9]\).*/\1/p' < /etc/.relid` + /bin/uname -p 2>/dev/null | grep 86 >/dev/null \ + && { echo i486-ncr-sysv4.3${OS_REL}; exit; } + /bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \ + && { echo i586-ncr-sysv4.3${OS_REL}; exit; } ;; + 3[34]??:*:4.0:* | 3[34]??,*:*:4.0:*) + /bin/uname -p 2>/dev/null | grep 86 >/dev/null \ + && { echo i486-ncr-sysv4; exit; } ;; + NCR*:*:4.2:* | MPRAS*:*:4.2:*) + OS_REL='.3' + test -r /etc/.relid \ + && OS_REL=.`sed -n 's/[^ ]* [^ ]* \([0-9][0-9]\).*/\1/p' < /etc/.relid` + /bin/uname -p 2>/dev/null | grep 86 >/dev/null \ + && { echo i486-ncr-sysv4.3${OS_REL}; exit; } + /bin/uname -p 2>/dev/null | /bin/grep entium >/dev/null \ + && { echo i586-ncr-sysv4.3${OS_REL}; exit; } + /bin/uname -p 2>/dev/null | /bin/grep pteron >/dev/null \ + && { echo i586-ncr-sysv4.3${OS_REL}; exit; } ;; + m68*:LynxOS:2.*:* | m68*:LynxOS:3.0*:*) + echo m68k-unknown-lynxos${UNAME_RELEASE} + exit ;; + mc68030:UNIX_System_V:4.*:*) + echo m68k-atari-sysv4 + exit ;; + TSUNAMI:LynxOS:2.*:*) + echo sparc-unknown-lynxos${UNAME_RELEASE} + exit ;; + rs6000:LynxOS:2.*:*) + echo rs6000-unknown-lynxos${UNAME_RELEASE} + exit ;; + PowerPC:LynxOS:2.*:* | PowerPC:LynxOS:3.[01]*:* | PowerPC:LynxOS:4.[02]*:*) + echo powerpc-unknown-lynxos${UNAME_RELEASE} + exit ;; + SM[BE]S:UNIX_SV:*:*) + echo mips-dde-sysv${UNAME_RELEASE} + exit ;; + RM*:ReliantUNIX-*:*:*) + echo mips-sni-sysv4 + exit ;; + RM*:SINIX-*:*:*) + echo mips-sni-sysv4 + exit ;; + *:SINIX-*:*:*) + if uname -p 2>/dev/null >/dev/null ; then + UNAME_MACHINE=`(uname -p) 2>/dev/null` + echo ${UNAME_MACHINE}-sni-sysv4 + else + echo ns32k-sni-sysv + fi + exit ;; + PENTIUM:*:4.0*:*) # Unisys `ClearPath HMP IX 4000' SVR4/MP effort + # says + echo i586-unisys-sysv4 + exit ;; + *:UNIX_System_V:4*:FTX*) + # From Gerald Hewes . + # How about differentiating between stratus architectures? -djm + echo hppa1.1-stratus-sysv4 + exit ;; + *:*:*:FTX*) + # From seanf@swdc.stratus.com. + echo i860-stratus-sysv4 + exit ;; + i*86:VOS:*:*) + # From Paul.Green@stratus.com. + echo ${UNAME_MACHINE}-stratus-vos + exit ;; + *:VOS:*:*) + # From Paul.Green@stratus.com. + echo hppa1.1-stratus-vos + exit ;; + mc68*:A/UX:*:*) + echo m68k-apple-aux${UNAME_RELEASE} + exit ;; + news*:NEWS-OS:6*:*) + echo mips-sony-newsos6 + exit ;; + R[34]000:*System_V*:*:* | R4000:UNIX_SYSV:*:* | R*000:UNIX_SV:*:*) + if [ -d /usr/nec ]; then + echo mips-nec-sysv${UNAME_RELEASE} + else + echo mips-unknown-sysv${UNAME_RELEASE} + fi + exit ;; + BeBox:BeOS:*:*) # BeOS running on hardware made by Be, PPC only. + echo powerpc-be-beos + exit ;; + BeMac:BeOS:*:*) # BeOS running on Mac or Mac clone, PPC only. + echo powerpc-apple-beos + exit ;; + BePC:BeOS:*:*) # BeOS running on Intel PC compatible. + echo i586-pc-beos + exit ;; + BePC:Haiku:*:*) # Haiku running on Intel PC compatible. + echo i586-pc-haiku + exit ;; + x86_64:Haiku:*:*) + echo x86_64-unknown-haiku + exit ;; + SX-4:SUPER-UX:*:*) + echo sx4-nec-superux${UNAME_RELEASE} + exit ;; + SX-5:SUPER-UX:*:*) + echo sx5-nec-superux${UNAME_RELEASE} + exit ;; + SX-6:SUPER-UX:*:*) + echo sx6-nec-superux${UNAME_RELEASE} + exit ;; + SX-7:SUPER-UX:*:*) + echo sx7-nec-superux${UNAME_RELEASE} + exit ;; + SX-8:SUPER-UX:*:*) + echo sx8-nec-superux${UNAME_RELEASE} + exit ;; + SX-8R:SUPER-UX:*:*) + echo sx8r-nec-superux${UNAME_RELEASE} + exit ;; + SX-ACE:SUPER-UX:*:*) + echo sxace-nec-superux${UNAME_RELEASE} + exit ;; + Power*:Rhapsody:*:*) + echo powerpc-apple-rhapsody${UNAME_RELEASE} + exit ;; + *:Rhapsody:*:*) + echo ${UNAME_MACHINE}-apple-rhapsody${UNAME_RELEASE} + exit ;; + *:Darwin:*:*) + UNAME_PROCESSOR=`uname -p` || UNAME_PROCESSOR=unknown + eval $set_cc_for_build + if test "$UNAME_PROCESSOR" = unknown ; then + UNAME_PROCESSOR=powerpc + fi + if test `echo "$UNAME_RELEASE" | sed -e 's/\..*//'` -le 10 ; then + if [ "$CC_FOR_BUILD" != no_compiler_found ]; then + if (echo '#ifdef __LP64__'; echo IS_64BIT_ARCH; echo '#endif') | \ + (CCOPTS="" $CC_FOR_BUILD -E - 2>/dev/null) | \ + grep IS_64BIT_ARCH >/dev/null + then + case $UNAME_PROCESSOR in + i386) UNAME_PROCESSOR=x86_64 ;; + powerpc) UNAME_PROCESSOR=powerpc64 ;; + esac + fi + fi + elif test "$UNAME_PROCESSOR" = i386 ; then + # Avoid executing cc on OS X 10.9, as it ships with a stub + # that puts up a graphical alert prompting to install + # developer tools. Any system running Mac OS X 10.7 or + # later (Darwin 11 and later) is required to have a 64-bit + # processor. This is not true of the ARM version of Darwin + # that Apple uses in portable devices. + UNAME_PROCESSOR=x86_64 + fi + echo ${UNAME_PROCESSOR}-apple-darwin${UNAME_RELEASE} + exit ;; + *:procnto*:*:* | *:QNX:[0123456789]*:*) + UNAME_PROCESSOR=`uname -p` + if test "$UNAME_PROCESSOR" = x86; then + UNAME_PROCESSOR=i386 + UNAME_MACHINE=pc + fi + echo ${UNAME_PROCESSOR}-${UNAME_MACHINE}-nto-qnx${UNAME_RELEASE} + exit ;; + *:QNX:*:4*) + echo i386-pc-qnx + exit ;; + NEO-?:NONSTOP_KERNEL:*:*) + echo neo-tandem-nsk${UNAME_RELEASE} + exit ;; + NSE-*:NONSTOP_KERNEL:*:*) + echo nse-tandem-nsk${UNAME_RELEASE} + exit ;; + NSR-?:NONSTOP_KERNEL:*:*) + echo nsr-tandem-nsk${UNAME_RELEASE} + exit ;; + *:NonStop-UX:*:*) + echo mips-compaq-nonstopux + exit ;; + BS2000:POSIX*:*:*) + echo bs2000-siemens-sysv + exit ;; + DS/*:UNIX_System_V:*:*) + echo ${UNAME_MACHINE}-${UNAME_SYSTEM}-${UNAME_RELEASE} + exit ;; + *:Plan9:*:*) + # "uname -m" is not consistent, so use $cputype instead. 386 + # is converted to i386 for consistency with other x86 + # operating systems. + if test "$cputype" = 386; then + UNAME_MACHINE=i386 + else + UNAME_MACHINE="$cputype" + fi + echo ${UNAME_MACHINE}-unknown-plan9 + exit ;; + *:TOPS-10:*:*) + echo pdp10-unknown-tops10 + exit ;; + *:TENEX:*:*) + echo pdp10-unknown-tenex + exit ;; + KS10:TOPS-20:*:* | KL10:TOPS-20:*:* | TYPE4:TOPS-20:*:*) + echo pdp10-dec-tops20 + exit ;; + XKL-1:TOPS-20:*:* | TYPE5:TOPS-20:*:*) + echo pdp10-xkl-tops20 + exit ;; + *:TOPS-20:*:*) + echo pdp10-unknown-tops20 + exit ;; + *:ITS:*:*) + echo pdp10-unknown-its + exit ;; + SEI:*:*:SEIUX) + echo mips-sei-seiux${UNAME_RELEASE} + exit ;; + *:DragonFly:*:*) + echo ${UNAME_MACHINE}-unknown-dragonfly`echo ${UNAME_RELEASE}|sed -e 's/[-(].*//'` + exit ;; + *:*VMS:*:*) + UNAME_MACHINE=`(uname -p) 2>/dev/null` + case "${UNAME_MACHINE}" in + A*) echo alpha-dec-vms ; exit ;; + I*) echo ia64-dec-vms ; exit ;; + V*) echo vax-dec-vms ; exit ;; + esac ;; + *:XENIX:*:SysV) + echo i386-pc-xenix + exit ;; + i*86:skyos:*:*) + echo ${UNAME_MACHINE}-pc-skyos`echo ${UNAME_RELEASE} | sed -e 's/ .*$//'` + exit ;; + i*86:rdos:*:*) + echo ${UNAME_MACHINE}-pc-rdos + exit ;; + i*86:AROS:*:*) + echo ${UNAME_MACHINE}-pc-aros + exit ;; + x86_64:VMkernel:*:*) + echo ${UNAME_MACHINE}-unknown-esx + exit ;; + amd64:Isilon\ OneFS:*:*) + echo x86_64-unknown-onefs + exit ;; +esac + +cat >&2 </dev/null || echo unknown` +uname -r = `(uname -r) 2>/dev/null || echo unknown` +uname -s = `(uname -s) 2>/dev/null || echo unknown` +uname -v = `(uname -v) 2>/dev/null || echo unknown` + +/usr/bin/uname -p = `(/usr/bin/uname -p) 2>/dev/null` +/bin/uname -X = `(/bin/uname -X) 2>/dev/null` + +hostinfo = `(hostinfo) 2>/dev/null` +/bin/universe = `(/bin/universe) 2>/dev/null` +/usr/bin/arch -k = `(/usr/bin/arch -k) 2>/dev/null` +/bin/arch = `(/bin/arch) 2>/dev/null` +/usr/bin/oslevel = `(/usr/bin/oslevel) 2>/dev/null` +/usr/convex/getsysinfo = `(/usr/convex/getsysinfo) 2>/dev/null` + +UNAME_MACHINE = ${UNAME_MACHINE} +UNAME_RELEASE = ${UNAME_RELEASE} +UNAME_SYSTEM = ${UNAME_SYSTEM} +UNAME_VERSION = ${UNAME_VERSION} +EOF + +exit 1 + +# Local variables: +# eval: (add-hook 'write-file-hooks 'time-stamp) +# time-stamp-start: "timestamp='" +# time-stamp-format: "%:y-%02m-%02d" +# time-stamp-end: "'" +# End: diff --git a/src/libebpf/src/configure.ac b/src/libebpf/src/configure.ac new file mode 100644 index 000000000000..edbe188aa381 --- /dev/null +++ b/src/libebpf/src/configure.ac @@ -0,0 +1,48 @@ +AC_INIT([sonic-libebpf],[1.0.0]) +AC_CONFIG_SRCDIR([]) +AC_CONFIG_AUX_DIR(config) +AM_CONFIG_HEADER(config.h) +AM_INIT_AUTOMAKE([foreign]) +AC_LANG_C +AC_LANG([C++]) +AC_PROG_CC +AC_PROG_CXX +AC_PROG_LIBTOOL +AC_HEADER_STDC + +#AC_CHECK_LIB([hiredis], [redisConnect],, +# AC_MSG_ERROR([libhiredis is not installed.])) + +#AC_CHECK_LIB([nl-genl-3], [genl_connect]) + +AC_ARG_ENABLE(debug, +[ --enable-debug Compile with debugging flags], +[case "${enableval}" in + yes) debug=true ;; + no) debug=false ;; + *) AC_MSG_ERROR(bad value ${enableval} for --enable-debug) ;; +esac],[debug=false]) +AM_CONDITIONAL(DEBUG, test x$debug = xtrue) + +AC_ARG_ENABLE(gtest, +[ --enable-gtest Compile with googletest flags], +[case "${enableval}" in + yes) gtest=true ;; + no) gtest=false ;; + *) AC_MSG_ERROR(bad value ${enableval} for --enable-gtest) ;; +esac],[gtest=false]) +AM_CONDITIONAL(GTEST, test x$gtest = xtrue) + +CFLAGS_COMMON="-std=c++11 -Wall -fPIC -Wno-write-strings -I/usr/include" + +CFLAGS_COMMON+=" -Werror" + +AC_SUBST(CFLAGS_COMMON) + +AC_CONFIG_FILES([ + libutil/Makefile + Makefile +]) + +AC_OUTPUT + diff --git a/src/libebpf/src/debian/changelog b/src/libebpf/src/debian/changelog new file mode 100644 index 000000000000..d77860cc2f7f --- /dev/null +++ b/src/libebpf/src/debian/changelog @@ -0,0 +1,6 @@ +sonic (1.0.0) stable; urgency=medium + + * Initial release. + + -- EBPF Fri, 26 Nov 2019 12:00:00 -0800 + diff --git a/src/libebpf/src/debian/compat b/src/libebpf/src/debian/compat new file mode 100644 index 000000000000..ec635144f600 --- /dev/null +++ b/src/libebpf/src/debian/compat @@ -0,0 +1 @@ +9 diff --git a/src/libebpf/src/debian/control b/src/libebpf/src/debian/control new file mode 100644 index 000000000000..0afac0888485 --- /dev/null +++ b/src/libebpf/src/debian/control @@ -0,0 +1,12 @@ +Source: sonic +Maintainer: Broadcom +Section: net +Priority: optional +Build-Depends: dh-exec (>=0.3), debhelper (>= 9), autotools-dev +Standards-Version: 1.0.0 + +Package: libebpf +Architecture: any +Depends: +Description: This package contains eBPF library for SONiC project. + diff --git a/src/libebpf/src/debian/rules b/src/libebpf/src/debian/rules new file mode 100755 index 000000000000..ba827ff91418 --- /dev/null +++ b/src/libebpf/src/debian/rules @@ -0,0 +1,25 @@ +#!/usr/bin/make -f + +export DH_VERBOSE = 1 +export DH_BUILD_DDEBS=1 +#export DEB_BUILD_OPTIONS=nostrip +export DEB_BUILD_OPTIONS=autodbgsym +#export DEB_BUILD_MAINT_OPTIONS = hardening=+all +#export DEB_CFLAGS_MAINT_APPEND = -Wall -pedantic +#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed + + +DPKG_EXPORT_BUILDFLAGS = 1 +include /usr/share/dpkg/default.mk + +# main packaging script based on dh7 syntax +%: + dh $@ --with autotools-dev + +override_dh_auto_install: + dh_auto_install --destdir=debian/libebpf + +.PHONY: override_dh_strip +override_dh_strip: + dh_strip --ddebs --automatic-dbgsym + diff --git a/src/libebpf/src/libutil/Makefile.am b/src/libebpf/src/libutil/Makefile.am new file mode 100644 index 000000000000..702b5156f65c --- /dev/null +++ b/src/libebpf/src/libutil/Makefile.am @@ -0,0 +1,23 @@ + +KBUILD_PATH ?= /lib/modules/$(KVERSION)/build +KBUILD_SRC_PATH ?= /lib/modules/$(KVERSION)/source + + +INCLUDES = -I$(KBUILD_SRC_PATH)/include/uapi -I$(KBUILD_SRC_PATH)/include +INCLUDES += -I $(top_srcdir)/utils + +nobase_include_HEADERS = ebpf/bpf_load.h ebpf/bpf_helpers.h ebpf/libebpf.h ebpf/libbpf.h +lib_LIBRARIES = libebpf.a +bin_PROGRAMS = ebpftest + +if DEBUG +DBGFLAGS = -ggdb -DDEBUG -O0 +else +DBGFLAGS = -g -DNDEBUG -O0 +endif + +libebpf_a_CFLAGS = -Wno-unused-variable -D__EXPORTED_HEADERS__ $(COV_CFLAGS) $(ASAN_CFLAGS) $(DBGFLAGS) +libebpf_a_SOURCES = bpf_load.c libbpf.c libebpf.c +ebpftest_CFLAGS = -Wno-unused-variable -D__EXPORTED_HEADERS__ $(COV_CFLAGS) $(ASAN_CFLAGS) $(DBGFLAGS) +ebpftest_SOURCES = ebpftest.c +ebpftest_LDFLAGS = -L. -lebpf -lbpf -lebpf -lelf diff --git a/src/libebpf/src/libutil/bpf_load.c b/src/libebpf/src/libutil/bpf_load.c new file mode 100755 index 000000000000..1bb7a2150e1f --- /dev/null +++ b/src/libebpf/src/libutil/bpf_load.c @@ -0,0 +1,441 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ebpf/libbpf.h" +#include "ebpf/bpf_helpers.h" +#include "ebpf/bpf_load.h" + +#define DEBUGFS "/sys/kernel/debug/tracing/" + +static char license[128]; +static int kern_version; +static bool processed_sec[128]; +int map_fd[MAX_MAPS]; +int prog_fd[MAX_PROGS]; +int event_fd[MAX_PROGS]; +int prog_cnt; +int prog_array_fd = -1; + +static int populate_prog_array(const char *event, int prog_fd) +{ + int ind = atoi(event), err; + + err = bpf_update_elem(prog_array_fd, &ind, &prog_fd, BPF_ANY); + if (err < 0) { + printf("failed to store prog_fd in prog_array\n"); + return -1; + } + return 0; +} + +static int load_and_attach(const char *event, struct bpf_insn *prog, int size) +{ + bool is_socket = strncmp(event, "socket", 6) == 0; + bool is_kprobe = strncmp(event, "kprobe/", 7) == 0; + bool is_kretprobe = strncmp(event, "kretprobe/", 10) == 0; + bool is_tracepoint = strncmp(event, "tracepoint/", 11) == 0; + bool is_xdp = strncmp(event, "xdp", 3) == 0; + bool is_perf_event = strncmp(event, "perf_event", 10) == 0; + enum bpf_prog_type prog_type; + char buf[256]; + int fd, efd, err, id; + struct perf_event_attr attr = {}; + + attr.type = PERF_TYPE_TRACEPOINT; + attr.sample_type = PERF_SAMPLE_RAW; + attr.sample_period = 1; + attr.wakeup_events = 1; + + if (is_socket) { + prog_type = BPF_PROG_TYPE_SOCKET_FILTER; + } else if (is_kprobe || is_kretprobe) { + prog_type = BPF_PROG_TYPE_KPROBE; + } else if (is_tracepoint) { + prog_type = BPF_PROG_TYPE_TRACEPOINT; + } else if (is_xdp) { + prog_type = BPF_PROG_TYPE_XDP; + } else if (is_perf_event) { + prog_type = BPF_PROG_TYPE_PERF_EVENT; + } else { + printf("Unknown event '%s'\n", event); + return -1; + } + + fd = bpf_prog_load(prog_type, prog, size, license, kern_version); + if (fd < 0) { + printf("bpf_prog_load() err=%d\n%s", errno, bpf_log_buf); + return -1; + } + + prog_fd[prog_cnt++] = fd; + + if (is_xdp || is_perf_event) + return 0; + + if (is_socket) { + event += 6; + if (*event != '/') + return 0; + event++; + if (!isdigit(*event)) { + printf("invalid prog number\n"); + return -1; + } + return populate_prog_array(event, fd); + } + + if (is_kprobe || is_kretprobe) { + if (is_kprobe) + event += 7; + else + event += 10; + + if (*event == 0) { + printf("event name cannot be empty\n"); + return -1; + } + + if (isdigit(*event)) + return populate_prog_array(event, fd); + + snprintf(buf, sizeof(buf), + "echo '%c:%s %s' >> /sys/kernel/debug/tracing/kprobe_events", + is_kprobe ? 'p' : 'r', event, event); + err = system(buf); + if (err < 0) { + printf("failed to create kprobe '%s' error '%s'\n", + event, strerror(errno)); + return -1; + } + + strcpy(buf, DEBUGFS); + strcat(buf, "events/kprobes/"); + strcat(buf, event); + strcat(buf, "/id"); + } else if (is_tracepoint) { + event += 11; + + if (*event == 0) { + printf("event name cannot be empty\n"); + return -1; + } + strcpy(buf, DEBUGFS); + strcat(buf, "events/"); + strcat(buf, event); + strcat(buf, "/id"); + } + + efd = open(buf, O_RDONLY, 0); + if (efd < 0) { + printf("failed to open event %s\n", event); + return -1; + } + + err = read(efd, buf, sizeof(buf)); + if (err < 0 || err >= sizeof(buf)) { + printf("read from '%s' failed '%s'\n", event, strerror(errno)); + return -1; + } + + close(efd); + + buf[err] = 0; + id = atoi(buf); + attr.config = id; + + efd = perf_event_open(&attr, -1/*pid*/, 0/*cpu*/, -1/*group_fd*/, 0); + if (efd < 0) { + printf("event %d fd %d err %s\n", id, efd, strerror(errno)); + return -1; + } + event_fd[prog_cnt - 1] = efd; + ioctl(efd, PERF_EVENT_IOC_ENABLE, 0); + ioctl(efd, PERF_EVENT_IOC_SET_BPF, fd); + + return 0; +} + +static int load_maps(struct bpf_map_def *maps, int len) +{ + int i; + + for (i = 0; i < len / sizeof(struct bpf_map_def); i++) { + + map_fd[i] = bpf_create_map(maps[i].type, + maps[i].key_size, + maps[i].value_size, + maps[i].max_entries, + maps[i].map_flags); + if (map_fd[i] < 0) { + printf("failed to create a map: %d %s\n", + errno, strerror(errno)); + return 1; + } + + if (maps[i].type == BPF_MAP_TYPE_PROG_ARRAY) + prog_array_fd = map_fd[i]; + } + return 0; +} + +static int get_sec(Elf *elf, int i, GElf_Ehdr *ehdr, char **shname, + GElf_Shdr *shdr, Elf_Data **data) +{ + Elf_Scn *scn; + + scn = elf_getscn(elf, i); + if (!scn) + return 1; + + if (gelf_getshdr(scn, shdr) != shdr) + return 2; + + *shname = elf_strptr(elf, ehdr->e_shstrndx, shdr->sh_name); + if (!*shname || !shdr->sh_size) + return 3; + + *data = elf_getdata(scn, 0); + if (!*data || elf_getdata(scn, *data) != NULL) + return 4; + + return 0; +} + +static int parse_relo_and_apply(Elf_Data *data, Elf_Data *symbols, + GElf_Shdr *shdr, struct bpf_insn *insn) +{ + int i, nrels; + + nrels = shdr->sh_size / shdr->sh_entsize; + + for (i = 0; i < nrels; i++) { + GElf_Sym sym; + GElf_Rel rel; + unsigned int insn_idx; + + gelf_getrel(data, i, &rel); + + insn_idx = rel.r_offset / sizeof(struct bpf_insn); + + gelf_getsym(symbols, GELF_R_SYM(rel.r_info), &sym); + + if (insn[insn_idx].code != (BPF_LD | BPF_IMM | BPF_DW)) { + printf("invalid relo for insn[%d].code 0x%x\n", + insn_idx, insn[insn_idx].code); + return 1; + } + insn[insn_idx].src_reg = BPF_PSEUDO_MAP_FD; + insn[insn_idx].imm = map_fd[sym.st_value / sizeof(struct bpf_map_def)]; + } + + return 0; +} + +int load_bpf_file(char *path) +{ + int fd, i; + Elf *elf; + GElf_Ehdr ehdr; + GElf_Shdr shdr, shdr_prog; + Elf_Data *data, *data_prog, *symbols = NULL; + char *shname, *shname_prog; + + if (elf_version(EV_CURRENT) == EV_NONE) + return 1; + + fd = open(path, O_RDONLY, 0); + if (fd < 0) + return 1; + + elf = elf_begin(fd, ELF_C_READ, NULL); + + if (!elf) + return 1; + + if (gelf_getehdr(elf, &ehdr) != &ehdr) + return 1; + + /* clear all kprobes */ + i = system("echo \"\" > /sys/kernel/debug/tracing/kprobe_events"); + + /* scan over all elf sections to get license and map info */ + for (i = 1; i < ehdr.e_shnum; i++) { + + if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) + continue; + + if (0) /* helpful for llvm debugging */ + printf("section %d:%s data %p size %zd link %d flags %d\n", + i, shname, data->d_buf, data->d_size, + shdr.sh_link, (int) shdr.sh_flags); + + if (strcmp(shname, "license") == 0) { + processed_sec[i] = true; + memcpy(license, data->d_buf, data->d_size); + } else if (strcmp(shname, "version") == 0) { + processed_sec[i] = true; + if (data->d_size != sizeof(int)) { + printf("invalid size of version section %zd\n", + data->d_size); + return 1; + } + memcpy(&kern_version, data->d_buf, sizeof(int)); + } else if (strcmp(shname, "maps") == 0) { + processed_sec[i] = true; + if (load_maps(data->d_buf, data->d_size)) + return 1; + } else if (shdr.sh_type == SHT_SYMTAB) { + symbols = data; + } + } + + /* load programs that need map fixup (relocations) */ + for (i = 1; i < ehdr.e_shnum; i++) { + + if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) + continue; + if (shdr.sh_type == SHT_REL) { + struct bpf_insn *insns; + + if (get_sec(elf, shdr.sh_info, &ehdr, &shname_prog, + &shdr_prog, &data_prog)) + continue; + + insns = (struct bpf_insn *) data_prog->d_buf; + + processed_sec[shdr.sh_info] = true; + processed_sec[i] = true; + + if (parse_relo_and_apply(data, symbols, &shdr, insns)) + continue; + + if (memcmp(shname_prog, "kprobe/", 7) == 0 || + memcmp(shname_prog, "kretprobe/", 10) == 0 || + memcmp(shname_prog, "tracepoint/", 11) == 0 || + memcmp(shname_prog, "xdp", 3) == 0 || + memcmp(shname_prog, "perf_event", 10) == 0 || + memcmp(shname_prog, "socket", 6) == 0) + load_and_attach(shname_prog, insns, data_prog->d_size); + } + } + + /* load programs that don't use maps */ + for (i = 1; i < ehdr.e_shnum; i++) { + + if (processed_sec[i]) + continue; + + if (get_sec(elf, i, &ehdr, &shname, &shdr, &data)) + continue; + + if (memcmp(shname, "kprobe/", 7) == 0 || + memcmp(shname, "kretprobe/", 10) == 0 || + memcmp(shname, "tracepoint/", 11) == 0 || + memcmp(shname, "xdp", 3) == 0 || + memcmp(shname, "perf_event", 10) == 0 || + memcmp(shname, "socket", 6) == 0) + load_and_attach(shname, data->d_buf, data->d_size); + } + + close(fd); + return 0; +} + +void read_trace_pipe(void) +{ + int trace_fd; + + trace_fd = open(DEBUGFS "trace_pipe", O_RDONLY, 0); + if (trace_fd < 0) + return; + + while (1) { + static char buf[4096]; + ssize_t sz; + + sz = read(trace_fd, buf, sizeof(buf) - 1); + if (sz > 0) { + buf[sz] = 0; + puts(buf); + } + } +} + +#define MAX_SYMS 300000 +static struct ksym syms[MAX_SYMS]; +static int sym_cnt; + +static int ksym_cmp(const void *p1, const void *p2) +{ + return ((struct ksym *)p1)->addr - ((struct ksym *)p2)->addr; +} + +int load_kallsyms(void) +{ + FILE *f = fopen("/proc/kallsyms", "r"); + char func[256], buf[256]; + char symbol; + void *addr; + int i = 0; + + if (!f) + return -ENOENT; + + while (!feof(f)) { + if (!fgets(buf, sizeof(buf), f)) + break; + if (sscanf(buf, "%p %c %s", &addr, &symbol, func) != 3) + break; + if (!addr) + continue; + syms[i].addr = (long) addr; + syms[i].name = strdup(func); + i++; + } + sym_cnt = i; + qsort(syms, sym_cnt, sizeof(struct ksym), ksym_cmp); + return 0; +} + +struct ksym *ksym_search(long key) +{ + int start = 0, end = sym_cnt; + int result; + + while (start < end) { + size_t mid = start + (end - start) / 2; + + result = key - syms[mid].addr; + if (result < 0) + end = mid; + else if (result > 0) + start = mid + 1; + else + return &syms[mid]; + } + + if (start >= 1 && syms[start - 1].addr < key && + key < syms[start].addr) + /* valid ksym */ + return &syms[start - 1]; + + /* out of range. return _stext */ + return &syms[0]; +} diff --git a/src/libebpf/src/libutil/ebpf/bpf_helpers.h b/src/libebpf/src/libutil/ebpf/bpf_helpers.h new file mode 100755 index 000000000000..bf7b3dda6c9c --- /dev/null +++ b/src/libebpf/src/libutil/ebpf/bpf_helpers.h @@ -0,0 +1,163 @@ +#ifndef __BPF_HELPERS_H +#define __BPF_HELPERS_H +//#define _LINUX_BPF_H 1 +#include + +/* helper macro to place programs, maps, license in + * different sections in elf_bpf file. Section names + * are interpreted by elf_bpf loader + */ +#define SEC(NAME) __attribute__((section(NAME), used)) + +/* helper functions called from eBPF programs written in C */ +static void *(*bpf_map_lookup_elem)(void *map, void *key) = + (void *) BPF_FUNC_map_lookup_elem; +static int (*bpf_map_update_elem)(void *map, void *key, void *value, + unsigned long long flags) = + (void *) BPF_FUNC_map_update_elem; +static int (*bpf_map_delete_elem)(void *map, void *key) = + (void *) BPF_FUNC_map_delete_elem; +static int (*bpf_probe_read)(void *dst, int size, void *unsafe_ptr) = + (void *) BPF_FUNC_probe_read; +static unsigned long long (*bpf_ktime_get_ns)(void) = + (void *) BPF_FUNC_ktime_get_ns; +static int (*bpf_trace_printk)(const char *fmt, int fmt_size, ...) = + (void *) BPF_FUNC_trace_printk; +//static void (*bpf_tail_call)(void *ctx, void *map, int index) = +// (void *) BPF_FUNC_tail_call; +static unsigned long long (*bpf_get_smp_processor_id)(void) = + (void *) BPF_FUNC_get_smp_processor_id; +static unsigned long long (*bpf_get_current_pid_tgid)(void) = + (void *) BPF_FUNC_get_current_pid_tgid; +static unsigned long long (*bpf_get_current_uid_gid)(void) = + (void *) BPF_FUNC_get_current_uid_gid; +static int (*bpf_get_current_comm)(void *buf, int buf_size) = + (void *) BPF_FUNC_get_current_comm; +static int (*bpf_perf_event_read)(void *map, int index) = + (void *) BPF_FUNC_perf_event_read; +static int (*bpf_clone_redirect)(void *ctx, int ifindex, int flags) = + (void *) BPF_FUNC_clone_redirect; +static int (*bpf_redirect)(int ifindex, int flags) = + (void *) BPF_FUNC_redirect; +static int (*bpf_perf_event_output)(void *ctx, void *map, + unsigned long long flags, void *data, + int size) = + (void *) BPF_FUNC_perf_event_output; +//static int (*bpf_get_stackid)(void *ctx, void *map, int flags) = +// (void *) BPF_FUNC_get_stackid; +static int (*bpf_probe_write_user)(void *dst, void *src, int size) = + (void *) BPF_FUNC_probe_write_user; +static int (*bpf_current_task_under_cgroup)(void *map, int index) = + (void *) BPF_FUNC_current_task_under_cgroup; +static int (*bpf_skb_get_tunnel_key)(void *ctx, void *key, int size, int flags) = + (void *) BPF_FUNC_skb_get_tunnel_key; +static int (*bpf_skb_set_tunnel_key)(void *ctx, void *key, int size, int flags) = + (void *) BPF_FUNC_skb_set_tunnel_key; +static int (*bpf_skb_get_tunnel_opt)(void *ctx, void *md, int size) = + (void *) BPF_FUNC_skb_get_tunnel_opt; +static int (*bpf_skb_set_tunnel_opt)(void *ctx, void *md, int size) = + (void *) BPF_FUNC_skb_set_tunnel_opt; +static unsigned long long (*bpf_get_prandom_u32)(void) = + (void *) BPF_FUNC_get_prandom_u32; + +static int (*bpf_find_nlattr)(void *skb, int off, int attr) = + (void *) (BPF_FUNC_skb_get_nlattr); + +static int (*bpf_find_nlattr_nest)(void *skb, int off, int attr) = + (void *) (BPF_FUNC_skb_get_nlattr_nest); + +/* llvm builtin functions that eBPF C program may use to + * emit BPF_LD_ABS and BPF_LD_IND instructions + */ +struct sk_buff; +unsigned long long load_byte(void *skb, + unsigned long long off) asm("llvm.bpf.load.byte"); +unsigned long long load_half(void *skb, + unsigned long long off) asm("llvm.bpf.load.half"); +unsigned long long load_word(void *skb, + unsigned long long off) asm("llvm.bpf.load.word"); + +/* a helper structure used by eBPF C program + * to describe map attributes to elf_bpf loader + */ +struct bpf_map_def { + unsigned int type; + unsigned int key_size; + unsigned int value_size; + unsigned int max_entries; + unsigned int map_flags; +}; + +static int (*bpf_skb_store_bytes)(void *ctx, int off, void *from, int len, int flags) = + (void *) BPF_FUNC_skb_store_bytes; +static int (*bpf_l3_csum_replace)(void *ctx, int off, int from, int to, int flags) = + (void *) BPF_FUNC_l3_csum_replace; +static int (*bpf_l4_csum_replace)(void *ctx, int off, int from, int to, int flags) = + (void *) BPF_FUNC_l4_csum_replace; +static int (*bpf_skb_under_cgroup)(void *ctx, void *map, int index) = + (void *) BPF_FUNC_skb_under_cgroup; + +#if defined(__x86_64__) + +#define PT_REGS_PARM1(x) ((x)->di) +#define PT_REGS_PARM2(x) ((x)->si) +#define PT_REGS_PARM3(x) ((x)->dx) +#define PT_REGS_PARM4(x) ((x)->cx) +#define PT_REGS_PARM5(x) ((x)->r8) +#define PT_REGS_RET(x) ((x)->sp) +#define PT_REGS_FP(x) ((x)->bp) +#define PT_REGS_RC(x) ((x)->ax) +#define PT_REGS_SP(x) ((x)->sp) +#define PT_REGS_IP(x) ((x)->ip) + +#elif defined(__s390x__) + +#define PT_REGS_PARM1(x) ((x)->gprs[2]) +#define PT_REGS_PARM2(x) ((x)->gprs[3]) +#define PT_REGS_PARM3(x) ((x)->gprs[4]) +#define PT_REGS_PARM4(x) ((x)->gprs[5]) +#define PT_REGS_PARM5(x) ((x)->gprs[6]) +#define PT_REGS_RET(x) ((x)->gprs[14]) +#define PT_REGS_FP(x) ((x)->gprs[11]) /* Works only with CONFIG_FRAME_POINTER */ +#define PT_REGS_RC(x) ((x)->gprs[2]) +#define PT_REGS_SP(x) ((x)->gprs[15]) +#define PT_REGS_IP(x) ((x)->psw.addr) + +#elif defined(__aarch64__) + +#define PT_REGS_PARM1(x) ((x)->regs[0]) +#define PT_REGS_PARM2(x) ((x)->regs[1]) +#define PT_REGS_PARM3(x) ((x)->regs[2]) +#define PT_REGS_PARM4(x) ((x)->regs[3]) +#define PT_REGS_PARM5(x) ((x)->regs[4]) +#define PT_REGS_RET(x) ((x)->regs[30]) +#define PT_REGS_FP(x) ((x)->regs[29]) /* Works only with CONFIG_FRAME_POINTER */ +#define PT_REGS_RC(x) ((x)->regs[0]) +#define PT_REGS_SP(x) ((x)->sp) +#define PT_REGS_IP(x) ((x)->pc) + +#elif defined(__powerpc__) + +#define PT_REGS_PARM1(x) ((x)->gpr[3]) +#define PT_REGS_PARM2(x) ((x)->gpr[4]) +#define PT_REGS_PARM3(x) ((x)->gpr[5]) +#define PT_REGS_PARM4(x) ((x)->gpr[6]) +#define PT_REGS_PARM5(x) ((x)->gpr[7]) +#define PT_REGS_RC(x) ((x)->gpr[3]) +#define PT_REGS_SP(x) ((x)->sp) +#define PT_REGS_IP(x) ((x)->nip) + +#endif + +#ifdef __powerpc__ +#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ (ip) = (ctx)->link; }) +#define BPF_KRETPROBE_READ_RET_IP BPF_KPROBE_READ_RET_IP +#else +#define BPF_KPROBE_READ_RET_IP(ip, ctx) ({ \ + bpf_probe_read(&(ip), sizeof(ip), (void *)PT_REGS_RET(ctx)); }) +#define BPF_KRETPROBE_READ_RET_IP(ip, ctx) ({ \ + bpf_probe_read(&(ip), sizeof(ip), \ + (void *)(PT_REGS_FP(ctx) + sizeof(ip))); }) +#endif + +#endif diff --git a/src/libebpf/src/libutil/ebpf/bpf_load.h b/src/libebpf/src/libutil/ebpf/bpf_load.h new file mode 100755 index 000000000000..dfa57fe65c8e --- /dev/null +++ b/src/libebpf/src/libutil/ebpf/bpf_load.h @@ -0,0 +1,33 @@ +#ifndef __BPF_LOAD_H +#define __BPF_LOAD_H + +#define MAX_MAPS 32 +#define MAX_PROGS 32 + +extern int map_fd[MAX_MAPS]; +extern int prog_fd[MAX_PROGS]; +extern int event_fd[MAX_PROGS]; + +/* parses elf file compiled by llvm .c->.o + * . parses 'maps' section and creates maps via BPF syscall + * . parses 'license' section and passes it to syscall + * . parses elf relocations for BPF maps and adjusts BPF_LD_IMM64 insns by + * storing map_fd into insn->imm and marking such insns as BPF_PSEUDO_MAP_FD + * . loads eBPF programs via BPF syscall + * + * One ELF file can contain multiple BPF programs which will be loaded + * and their FDs stored stored in prog_fd array + * + * returns zero on success + */ +int load_bpf_file(char *path); + +void read_trace_pipe(void); +struct ksym { + long addr; + char *name; +}; + +int load_kallsyms(void); +struct ksym *ksym_search(long key); +#endif diff --git a/src/libebpf/src/libutil/ebpf/libbpf.h b/src/libebpf/src/libutil/ebpf/libbpf.h new file mode 100755 index 000000000000..ac6edb61b64a --- /dev/null +++ b/src/libebpf/src/libutil/ebpf/libbpf.h @@ -0,0 +1,209 @@ +/* eBPF mini library */ +#ifndef __LIBBPF_H +#define __LIBBPF_H + +struct bpf_insn; + +int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, + int max_entries, int map_flags); +int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags); +int bpf_lookup_elem(int fd, void *key, void *value); +int bpf_delete_elem(int fd, void *key); +int bpf_get_next_key(int fd, void *key, void *next_key); + +int bpf_prog_load(enum bpf_prog_type prog_type, + const struct bpf_insn *insns, int insn_len, + const char *license, int kern_version); + +int bpf_obj_pin(int fd, const char *pathname); +int bpf_obj_get(const char *pathname); + +#define LOG_BUF_SIZE 65536 +extern char bpf_log_buf[LOG_BUF_SIZE]; + +/* ALU ops on registers, bpf_add|sub|...: dst_reg += src_reg */ + +#define BPF_ALU64_REG(OP, DST, SRC) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_ALU32_REG(OP, DST, SRC) \ + ((struct bpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* ALU ops on immediates, bpf_add|sub|...: dst_reg += imm32 */ + +#define BPF_ALU64_IMM(OP, DST, IMM) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +#define BPF_ALU32_IMM(OP, DST, IMM) \ + ((struct bpf_insn) { \ + .code = BPF_ALU | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Short form of mov, dst_reg = src_reg */ + +#define BPF_MOV64_REG(DST, SRC) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +#define BPF_MOV32_REG(DST, SRC) \ + ((struct bpf_insn) { \ + .code = BPF_ALU | BPF_MOV | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = 0 }) + +/* Short form of mov, dst_reg = imm32 */ + +#define BPF_MOV64_IMM(DST, IMM) \ + ((struct bpf_insn) { \ + .code = BPF_ALU64 | BPF_MOV | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +#define BPF_MOV32_IMM(DST, IMM) \ + ((struct bpf_insn) { \ + .code = BPF_ALU | BPF_MOV | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* BPF_LD_IMM64 macro encodes single 'load 64-bit immediate' insn */ +#define BPF_LD_IMM64(DST, IMM) \ + BPF_LD_IMM64_RAW(DST, 0, IMM) + +#define BPF_LD_IMM64_RAW(DST, SRC, IMM) \ + ((struct bpf_insn) { \ + .code = BPF_LD | BPF_DW | BPF_IMM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = 0, \ + .imm = (__u32) (IMM) }), \ + ((struct bpf_insn) { \ + .code = 0, /* zero is reserved opcode */ \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = ((__u64) (IMM)) >> 32 }) + +#ifndef BPF_PSEUDO_MAP_FD +# define BPF_PSEUDO_MAP_FD 1 +#endif + +/* pseudo BPF_LD_IMM64 insn used to refer to process-local map_fd */ +#define BPF_LD_MAP_FD(DST, MAP_FD) \ + BPF_LD_IMM64_RAW(DST, BPF_PSEUDO_MAP_FD, MAP_FD) + + +/* Direct packet access, R0 = *(uint *) (skb->data + imm32) */ + +#define BPF_LD_ABS(SIZE, IMM) \ + ((struct bpf_insn) { \ + .code = BPF_LD | BPF_SIZE(SIZE) | BPF_ABS, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = IMM }) + +/* Memory load, dst_reg = *(uint *) (src_reg + off16) */ + +#define BPF_LDX_MEM(SIZE, DST, SRC, OFF) \ + ((struct bpf_insn) { \ + .code = BPF_LDX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Memory store, *(uint *) (dst_reg + off16) = src_reg */ + +#define BPF_STX_MEM(SIZE, DST, SRC, OFF) \ + ((struct bpf_insn) { \ + .code = BPF_STX | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Memory store, *(uint *) (dst_reg + off16) = imm32 */ + +#define BPF_ST_MEM(SIZE, DST, OFF, IMM) \ + ((struct bpf_insn) { \ + .code = BPF_ST | BPF_SIZE(SIZE) | BPF_MEM, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = OFF, \ + .imm = IMM }) + +/* Conditional jumps against registers, if (dst_reg 'op' src_reg) goto pc + off16 */ + +#define BPF_JMP_REG(OP, DST, SRC, OFF) \ + ((struct bpf_insn) { \ + .code = BPF_JMP | BPF_OP(OP) | BPF_X, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = 0 }) + +/* Conditional jumps against immediates, if (dst_reg 'op' imm32) goto pc + off16 */ + +#define BPF_JMP_IMM(OP, DST, IMM, OFF) \ + ((struct bpf_insn) { \ + .code = BPF_JMP | BPF_OP(OP) | BPF_K, \ + .dst_reg = DST, \ + .src_reg = 0, \ + .off = OFF, \ + .imm = IMM }) + +/* Raw code statement block */ + +#define BPF_RAW_INSN(CODE, DST, SRC, OFF, IMM) \ + ((struct bpf_insn) { \ + .code = CODE, \ + .dst_reg = DST, \ + .src_reg = SRC, \ + .off = OFF, \ + .imm = IMM }) + +/* Program exit */ + +#define BPF_EXIT_INSN() \ + ((struct bpf_insn) { \ + .code = BPF_JMP | BPF_EXIT, \ + .dst_reg = 0, \ + .src_reg = 0, \ + .off = 0, \ + .imm = 0 }) + +/* create RAW socket and bind to interface 'name' */ +int open_raw_sock(const char *name); + +struct perf_event_attr; +int perf_event_open(struct perf_event_attr *attr, int pid, int cpu, + int group_fd, unsigned long flags); +#endif diff --git a/src/libebpf/src/libutil/ebpf/libebpf.h b/src/libebpf/src/libutil/ebpf/libebpf.h new file mode 100644 index 000000000000..f285395b8523 --- /dev/null +++ b/src/libebpf/src/libutil/ebpf/libebpf.h @@ -0,0 +1,33 @@ +#ifndef __LIBEBPF_H__ +#define __LIBEBPF_H__ + +/* compiler workaround */ +#define _htonl __builtin_bswap32 + +#define DEBUG_MODE_ON +#define ACCEPT_PKT 0xFFFF +#define DROP_PKT 0 + + +#ifdef DEBUG_MODE_ON +#define BPF_TR(_fmt,...) \ +{ \ + char fmt[]=_fmt; \ + bpf_trace_printk(fmt, sizeof(fmt), ##__VA_ARGS__); \ +} +#else +#define BPF_TR(_fmt,...) {}; +#endif + +#ifdef __cplusplus +extern "C" { +#endif + + int attach_ebpf_filter(int nl_fd, char *); + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/src/libebpf/src/libutil/ebpftest.c b/src/libebpf/src/libutil/ebpftest.c new file mode 100644 index 000000000000..c2c5c5fe8cee --- /dev/null +++ b/src/libebpf/src/libutil/ebpftest.c @@ -0,0 +1,100 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ebpf/libebpf.h" +#include "ebpf/libbpf.h" +#include "ebpf/bpf_load.h" + + +extern char bpf_log_buf[65536]; + +char bpf_log_buf[65536]; +int socket_create() +{ + /*int sock = socket(AF_UNIX, SOCK_DGRAM, 0);*/ + int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); + return sock; +} + +int create_netlink_socket() +{ + int nl_fd = 0; + + struct sockaddr_nl sa = { + .nl_family = AF_NETLINK, + .nl_pad = 0, + .nl_pid = 0, + .nl_groups = RTMGRP_LINK + }; + + + nl_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); + if (nl_fd == -1) + { + printf("nl_fd CREATE Failed : %s", strerror(errno)); + return -1; + } + + if(bind(nl_fd, (struct sockaddr *) &sa, sizeof(sa)) == -1) + { + printf("nl_fd BIND Failed : %s", strerror(errno)); + return -1; + } + + return nl_fd; +} + + + +int timer_test() +{ + + int sock_fd = -1; + char filename[256]; + sock_fd = socket_create(); + + snprintf(filename, sizeof(filename), "ebpd_timer.bpf"); + + if (load_bpf_file(filename)) { + printf("%s", bpf_log_buf); + return 1; + } + + assert(setsockopt(sock_fd, SOL_SOCKET, SO_ATTACH_BPF, prog_fd, + sizeof(prog_fd[0])) == 0); + + return 0; +} +int main(int argc, char *argv[]) +{ + if(argc <2){ + printf("Usage: %s \n",argv[0] ); + exit(0); + } + + int sd=create_netlink_socket(); + if (sd == -1) + { + printf("sock CREATE Failed : %s", strerror(errno)); + return -1; + } + attach_ebpf_filter(sd,argv[1]); +} diff --git a/src/libebpf/src/libutil/libbpf.c b/src/libebpf/src/libutil/libbpf.c new file mode 100755 index 000000000000..18fd7bf5f62f --- /dev/null +++ b/src/libebpf/src/libutil/libbpf.c @@ -0,0 +1,155 @@ +/* eBPF mini library */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ebpf/libbpf.h" + +static __u64 ptr_to_u64(void *ptr) +{ + return (__u64) (unsigned long) ptr; +} + +int bpf_create_map(enum bpf_map_type map_type, int key_size, int value_size, + int max_entries, int map_flags) +{ + union bpf_attr attr = { + .map_type = map_type, + .key_size = key_size, + .value_size = value_size, + .max_entries = max_entries, + .map_flags = map_flags, + }; + + return syscall(__NR_bpf, BPF_MAP_CREATE, &attr, sizeof(attr)); +} + +int bpf_update_elem(int fd, void *key, void *value, unsigned long long flags) +{ + union bpf_attr attr = { + .map_fd = fd, + .key = ptr_to_u64(key), + .value = ptr_to_u64(value), + .flags = flags, + }; + + return syscall(__NR_bpf, BPF_MAP_UPDATE_ELEM, &attr, sizeof(attr)); +} + +int bpf_lookup_elem(int fd, void *key, void *value) +{ + union bpf_attr attr = { + .map_fd = fd, + .key = ptr_to_u64(key), + .value = ptr_to_u64(value), + }; + + return syscall(__NR_bpf, BPF_MAP_LOOKUP_ELEM, &attr, sizeof(attr)); +} + +int bpf_delete_elem(int fd, void *key) +{ + union bpf_attr attr = { + .map_fd = fd, + .key = ptr_to_u64(key), + }; + + return syscall(__NR_bpf, BPF_MAP_DELETE_ELEM, &attr, sizeof(attr)); +} + +int bpf_get_next_key(int fd, void *key, void *next_key) +{ + union bpf_attr attr = { + .map_fd = fd, + .key = ptr_to_u64(key), + .next_key = ptr_to_u64(next_key), + }; + + return syscall(__NR_bpf, BPF_MAP_GET_NEXT_KEY, &attr, sizeof(attr)); +} + +#define ROUND_UP(x, n) (((x) + (n) - 1u) & ~((n) - 1u)) + +char bpf_log_buf[LOG_BUF_SIZE]; + +int bpf_prog_load(enum bpf_prog_type prog_type, + const struct bpf_insn *insns, int prog_len, + const char *license, int kern_version) +{ + union bpf_attr attr = { + .prog_type = prog_type, + .insns = ptr_to_u64((void *) insns), + .insn_cnt = prog_len / sizeof(struct bpf_insn), + .license = ptr_to_u64((void *) license), + .log_buf = ptr_to_u64(bpf_log_buf), + .log_size = LOG_BUF_SIZE, + .log_level = 1, + }; + + /* assign one field outside of struct init to make sure any + * padding is zero initialized + */ + attr.kern_version = kern_version; + + bpf_log_buf[0] = 0; + + return syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); +} + +int bpf_obj_pin(int fd, const char *pathname) +{ + union bpf_attr attr = { + .pathname = ptr_to_u64((void *)pathname), + .bpf_fd = fd, + }; + + return syscall(__NR_bpf, BPF_OBJ_PIN, &attr, sizeof(attr)); +} + +int bpf_obj_get(const char *pathname) +{ + union bpf_attr attr = { + .pathname = ptr_to_u64((void *)pathname), + }; + + return syscall(__NR_bpf, BPF_OBJ_GET, &attr, sizeof(attr)); +} + +int open_raw_sock(const char *name) +{ + struct sockaddr_ll sll; + int sock; + + sock = socket(PF_PACKET, SOCK_RAW | SOCK_NONBLOCK | SOCK_CLOEXEC, htons(ETH_P_ALL)); + if (sock < 0) { + printf("cannot create raw socket\n"); + return -1; + } + + memset(&sll, 0, sizeof(sll)); + sll.sll_family = AF_PACKET; + sll.sll_ifindex = if_nametoindex(name); + sll.sll_protocol = htons(ETH_P_ALL); + if (bind(sock, (struct sockaddr *)&sll, sizeof(sll)) < 0) { + printf("bind to %s: %s\n", name, strerror(errno)); + close(sock); + return -1; + } + + return sock; +} + +int perf_event_open(struct perf_event_attr *attr, int pid, int cpu, + int group_fd, unsigned long flags) +{ + return syscall(__NR_perf_event_open, attr, pid, cpu, + group_fd, flags); +} diff --git a/src/libebpf/src/libutil/libebpf.c b/src/libebpf/src/libutil/libebpf.c new file mode 100644 index 000000000000..dcab634421a0 --- /dev/null +++ b/src/libebpf/src/libutil/libebpf.c @@ -0,0 +1,99 @@ +/* + * Copyright 2019 Broadcom. All rights reserved. + * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries. + * + * Author: Kalimuthu Velappan + * Email : kalimuthu.velappan@broadcom.com + * + * 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. + * + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ebpf/libebpf.h" +#include "ebpf/libbpf.h" +#include "ebpf/bpf_load.h" + + +void open_syslog(char *filename) +{ + char filectl[128]=""; + sprintf(filectl, "libebpf#%s",filename); + + setlogmask (LOG_UPTO (LOG_NOTICE)); + openlog (filectl, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); +} +void close_syslog() +{ + closelog(); +} + + +__attribute__ ((visibility("default"))) int attach_ebpf_filter(int nl_fd, char *filename) +{ + char filectl[128]=""; + + + sprintf(filectl, "%s.enable",filename); + if( access( filectl, F_OK ) == -1 ) + { + syslog(LOG_INFO, "Filter %s is disabled\n", filename); + return 0; + } + + if (load_bpf_file(filename)) { + syslog(LOG_ERR, "%s", bpf_log_buf); + syslog(LOG_ERR, "Filter load is failed for %s socket \n", filename); + return 0; + } + + if(setsockopt(nl_fd, SOL_SOCKET, SO_ATTACH_BPF, prog_fd, sizeof(prog_fd[0])) != 0) + { + perror("Filter attach failed: "); + syslog(LOG_ERR, "Filter attach is failed for %s socket \n", filename); + return 0; + } + syslog(LOG_INFO, "Filter is attached successfully for %s socket \n", filename); + return 0; +} + +#ifdef MAIN +int main(int argc, char *argv[]) +{ + printf("\n"); + return 0; +} +#endif + + + + diff --git a/src/libebpf/src/samples/ebpf-kern-mod/Makefile b/src/libebpf/src/samples/ebpf-kern-mod/Makefile new file mode 100755 index 000000000000..79e348721d5f --- /dev/null +++ b/src/libebpf/src/samples/ebpf-kern-mod/Makefile @@ -0,0 +1,63 @@ +KBUILD_PATH ?= /lib/modules/$(KVERSION)/build +KBUILD_SRC_PATH ?= /lib/modules/$(KVERSION)/source +LLC ?= llc-6.0 +CLANG ?= clang-6.0 + + +SRCS := ebpftest.c ebpf_timer.c +EBPFOBJS := $(SRCS:.c=.bpf) +BYTEOBJS := $(SRCS:.c=.o) +EXTRA_CFLAGS += -I$(PWD)/../../libutil \ + -I$(KBUILD_SRC_PATH)/include/uapi -I$(KBUILD_SRC_PATH)/include \ + -I$(objtree)/usr/include -Wno-unused-variable + + +# Tell kbuild to always build the programs +always := $(hostprogs-y) +always += $(EBPFOBJS) + + +# Trick to allow make to be run from this directory +all: + $(MAKE) -C $(KBUILD_PATH) M=$$PWD/ + +clean: + $(MAKE) -C $(KBUILD_PATH) M=$$PWD clean + @rm -f *~ $(BYTEOBJS) $(EBPFOBJS) + +distclean: clean + +# Verify LLVM compiler tools are available and bpf target is supported by llc +.PHONY: verify_cmds verify_target_bpf $(CLANG) $(LLC) + +verify_cmds: $(CLANG) $(LLC) + @for TOOL in $^ ; do \ + if ! (which -- "$${TOOL}" > /dev/null 2>&1); then \ + echo "*** ERROR: Cannot find LLVM tool $${TOOL}" ;\ + exit 1; \ + else true; fi; \ + done + +verify_target_bpf: verify_cmds + @if ! (${LLC} -march=bpf -mattr=help > /dev/null 2>&1); then \ + echo "*** ERROR: LLVM (${LLC}) does not support 'bpf' target" ;\ + echo " NOTICE: LLVM version >= 3.7.1 required" ;\ + exit 2; \ + else true; fi + +$(SRCS): verify_target_bpf + +# asm/sysreg.h - inline assembly used by it is incompatible with llvm. +# But, there is no easy way to fix it, so just exclude it since it is +# useless for BPF samples. +$(addprefix $(obj)/,$(BYTEOBJS)):%.o:%.c + $(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(EXTRA_CFLAGS) \ + -D__KERNEL__ -D__ASM_SYSREG_H -Wno-unused-value -Wno-pointer-sign \ + -Wno-compare-distinct-pointer-types \ + -O2 -emit-llvm -c $< -o $@ + +$(addprefix $(obj)/,$(EBPFOBJS)):%.bpf:%.o + cat $^ | $(LLC) -march=bpf -filetype=obj -o $@ + +install: + cp *.bpf $(DESTDIR)/usr/lib diff --git a/src/libebpf/src/samples/ebpf-kern-mod/ebpf_timer.c b/src/libebpf/src/samples/ebpf-kern-mod/ebpf_timer.c new file mode 100644 index 000000000000..3545261d2af5 --- /dev/null +++ b/src/libebpf/src/samples/ebpf-kern-mod/ebpf_timer.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ebpf/bpf_helpers.h" +#include "ebpf/bpf_helpers.h" +#include "ebpftest.h" +#include +/*#include */ +/*#include "ebpf/bpf_helpers.h"*/ +/*#define SEC(NAME) __attribute__((section(NAME), used))*/ + +#define __uint(name, val) int (*name)[val] +#define __type(name, val) val *name + + +struct { + __uint(type, BPF_MAP_TYPE_PERF_EVENT_ARRAY); + __uint(key_size, sizeof(int)); + __uint(value_size, sizeof(int)); +} perf_buf_map SEC(".maps"); + +SEC("kprobe/sys_nanosleep") +int nanosec_timer(struct pt_regs *ctx) +{ + int cpu = bpf_get_smp_processor_id(); + + bpf_perf_event_output(ctx, &perf_buf_map, BPF_F_CURRENT_CPU, + &cpu, sizeof(cpu)); + bpf_printk("%s\n","RDX"); + return 0; +} + +char _license[] SEC("license") = "GPL"; +__u32 _version SEC("version") = 1; diff --git a/src/libebpf/src/samples/ebpf-kern-mod/ebpftest.c b/src/libebpf/src/samples/ebpf-kern-mod/ebpftest.c new file mode 100755 index 000000000000..87f73bc004a7 --- /dev/null +++ b/src/libebpf/src/samples/ebpf-kern-mod/ebpftest.c @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ebpf/bpf_helpers.h" +#include "ebpf/bpf_helpers.h" +#include "ebpftest.h" + + +struct bpf_map_def SEC("maps") my_map = { + .type = BPF_MAP_TYPE_HASH, + .key_size = sizeof(KEY), + .value_size = sizeof(VALUE), + .max_entries = 256, +}; + +SEC("socket") +int bpf_prog1(struct __sk_buff *skb) +{ + + uint16_t nlmsg_flags = load_half(skb, offsetof(struct nlmsghdr, nlmsg_flags)); + nlmsg_flags = htons(nlmsg_flags); + + // accept all pkts with NLM_F_MULTI flag set + if ( nlmsg_flags & NLM_F_MULTI) + BPF_MAP_ACCEPT_PKT(&my_map); + + uint16_t nlmsg_type = load_half(skb, offsetof(struct nlmsghdr, nlmsg_type)); + nlmsg_type = htons(nlmsg_type); + + //accept pkt for end of Multi msg + if (nlmsg_type == NLMSG_DONE) + BPF_MAP_ACCEPT_PKT(&my_map); + + if ( (nlmsg_type != RTM_NEWLINK) + && (nlmsg_type != RTM_DELLINK)) + { + BPF_TR(" TEAMD : [%d] >> Unknown Type : pkt: %d\n", INVALID_IFINDEX, nlmsg_type); + BPF_MAP_DROP_PKT(&my_map); + } + + uint8_t ifi_family = load_byte(skb, sizeof(struct nlmsghdr) + offsetof(struct ifinfomsg, ifi_family)); + if (ifi_family != AF_UNSPEC) + { + BPF_TR(" TEAMD : [%d] >> Invalid family : %d\n", INVALID_IFINDEX, ifi_family); + BPF_MAP_DROP_PKT(&my_map); + } + + int ifname_hex = 0; + KEY m_key = {0, 0}; + VALUE new_val = {0}; + + int kif_index = load_word(skb, sizeof(struct nlmsghdr) + offsetof(struct ifinfomsg, ifi_index)); + kif_index = _htonl(kif_index); + m_key.ifidx = kif_index; + + int tlv_off_st = (sizeof(struct nlmsghdr) + sizeof(struct ifinfomsg)); + int tlv_off = bpf_find_nlattr(skb, tlv_off_st, IFLA_IFNAME); + if(tlv_off != 0) + { + BPF_TR(" TEAMD : [%d] >> found IFLA_IFNAME @ off : %d\n", m_key.ifidx, tlv_off); + ifname_hex = load_word(skb, tlv_off+sizeof(struct nlattr)); + } + + if ( (ifname_hex != ETH_ASCII) + && (ifname_hex != PO_ASCII) ) + { + BPF_TR(" TEAMD : [%d] >> Not of Ethernet or PortChannel, ifname : %x\n", m_key.ifidx, ifname_hex); + BPF_MAP_DROP_PKT(&my_map); + } + + + BPF_TR(" TEAMD : [%d] >> Recv Pkt : %d\n", m_key.ifidx, nlmsg_type); + + if (nlmsg_type == RTM_DELLINK) + { + bpf_map_delete_elem(&my_map, &m_key); + BPF_MAP_ACCEPT_PKT(&my_map); + } + + int t_flags = load_word(skb, sizeof(struct nlmsghdr) + offsetof(struct ifinfomsg, ifi_flags)); + new_val.team_info.flags |= (t_flags & _htonl(IFF_RUNNING)); + new_val.team_info.flags |= (t_flags & _htonl(IFF_UP)); + + tlv_off = bpf_find_nlattr(skb, tlv_off_st, IFLA_MASTER); + if(tlv_off != 0) + { + BPF_TR(" TEAMD : [%d] >> found IFLA_MASTER @ off : %d\n", m_key.ifidx, tlv_off); + new_val.team_info.master = load_word(skb, tlv_off+sizeof(struct nlattr)); + } + + tlv_off = bpf_find_nlattr(skb, tlv_off_st, IFLA_ADDRESS); + if(tlv_off != 0) + { + BPF_TR(" TEAMD : [%d] >> found IFLA_ADDRESS @ off : %d\n", m_key.ifidx, tlv_off); + new_val.team_info.mac_addr[0] = load_byte(skb, 0+tlv_off+sizeof(struct nlattr)); + new_val.team_info.mac_addr[1] = load_byte(skb, 1+tlv_off+sizeof(struct nlattr)); + new_val.team_info.mac_addr[2] = load_byte(skb, 2+tlv_off+sizeof(struct nlattr)); + new_val.team_info.mac_addr[3] = load_byte(skb, 3+tlv_off+sizeof(struct nlattr)); + new_val.team_info.mac_addr[4] = load_byte(skb, 4+tlv_off+sizeof(struct nlattr)); + new_val.team_info.mac_addr[5] = load_byte(skb, 5+tlv_off+sizeof(struct nlattr)); + } + + tlv_off = bpf_find_nlattr(skb, tlv_off_st, IFLA_PHYS_PORT_ID); + if(tlv_off != 0) + { + BPF_TR(" TEAMD : [%d] >> found IFLA_PHYS_PORT_ID @ off : %d\n", m_key.ifidx, tlv_off); + new_val.team_info.ppid.id[0] = load_byte(skb, 0+tlv_off+sizeof(struct nlattr)); + new_val.team_info.ppid.id[1] = load_byte(skb, 1+tlv_off+sizeof(struct nlattr)); + new_val.team_info.ppid.id[2] = load_byte(skb, 2+tlv_off+sizeof(struct nlattr)); + new_val.team_info.ppid.id[3] = load_byte(skb, 3+tlv_off+sizeof(struct nlattr)); + new_val.team_info.ppid.id[4] = load_byte(skb, 4+tlv_off+sizeof(struct nlattr)); + new_val.team_info.ppid.id[5] = load_byte(skb, 5+tlv_off+sizeof(struct nlattr)); + } + + + tlv_off = bpf_find_nlattr(skb, tlv_off_st, IFLA_LINKINFO); + if(tlv_off != 0) + { + BPF_TR(" TEAMD : [%d] >> found IFLA_LINKINFO @ off : %d\n", m_key.ifidx,tlv_off); + tlv_off = bpf_find_nlattr_nest(skb, tlv_off, IFLA_INFO_SLAVE_KIND); + if(tlv_off != 0) + { + new_val.team_info.slave_info = load_word(skb, tlv_off+sizeof(struct nlattr)); + BPF_TR(" TEAMD : [%d] >> found IFLA_INFO_SLAVE @ off : %d\n slave-info : %x",tlv_off, new_val.team_info.slave_info); + } + else + BPF_TR(" TEAMD : [%d] >> NOT found SLAVE_IFLA_LINKINFO @ off : %d\n", m_key.ifidx,tlv_off); + } + else + BPF_TR(" TEAMD : [%d] >> NOT found IFLA_LINKINFO @ off : %d\n", m_key.ifidx,tlv_off); + + + VALUE *old_val = 0; + uint8_t update_flag = 0; + + old_val = bpf_map_lookup_elem(&my_map, &m_key); + if (old_val) + { + + + if ( (old_val->team_info.slave_info != new_val.team_info.slave_info) + || (old_val->team_info.master != new_val.team_info.master) + || (old_val->team_info.flags != new_val.team_info.flags) + || (*((uint32_t *)&old_val->team_info.mac_addr[0]) != *((uint32_t *)&new_val.team_info.mac_addr[0])) + || (*((uint16_t *)&old_val->team_info.mac_addr[4]) != *((uint16_t *)&new_val.team_info.mac_addr[4])) + || (*((uint32_t *)&old_val->team_info.ppid.id[0]) != *((uint32_t *)&new_val.team_info.ppid.id[0])) + || (*((uint16_t *)&old_val->team_info.ppid.id[4]) != *((uint16_t *)&new_val.team_info.ppid.id[4])) + ) + { + new_val.count = old_val->count + 1; + bpf_map_update_elem(&my_map, &m_key, &new_val, BPF_EXIST); + } + else + { + BPF_TR(" TEAMD : [%d] >> No Change Drop pkt\n", m_key.ifidx); + BPF_MAP_DROP_PKT(&my_map); + } + } + else { + bpf_map_update_elem(&my_map, &m_key, &new_val, BPF_NOEXIST); + } + + BPF_TR("TEAMD: [%d] >> ACCEPT\n\n", m_key.ifidx); + BPF_MAP_ACCEPT_PKT(&my_map); +} +char _license[] SEC("license") = "GPL"; diff --git a/src/libebpf/src/samples/ebpf-kern-mod/ebpftest.h b/src/libebpf/src/samples/ebpf-kern-mod/ebpftest.h new file mode 100755 index 000000000000..7ec0563d1f49 --- /dev/null +++ b/src/libebpf/src/samples/ebpf-kern-mod/ebpftest.h @@ -0,0 +1,99 @@ +#ifndef __EBPFTEST_H__ +#define __EBPFTEST_H__ + +#pragma pack(push,1) +typedef struct { + int ifidx; + int pad; +}KEY; + + + +#define MAX_PHYS_ITEM_ID_LEN 32 + +/* This structure holds a unique identifier to identify some + * * physical item (port for example) used by a netdevice. + * */ +struct netdev_phys_item_id { + unsigned char id[MAX_PHYS_ITEM_ID_LEN]; + unsigned char id_len; +}; + +#define KEY_VALUE_FORMAT "K:%d, V:(S[%x] M[%x] F[%x] MC[%x:%x:%x:%x:%x:%x] P[%x:%x:%x:%x:%x:%x])" +typedef struct { + // i think name can never change for a given kernel ifindex + // not sure why teamd code checks for this as well. + // To reduce complexity of filter logic i'm commenting this out for now. + /*char ifname[IFNAMSIZ];*/ + int slave_info; + int master; + int flags; + char mac_addr[6]; + struct netdev_phys_item_id ppid; +}TEAMD_INFO; + +typedef struct { + int count; + int accept; + TEAMD_INFO team_info; +}VALUE; +#pragma pack(pop) + +/* compiler workaround */ +#define _htonl __builtin_bswap32 + +#define DEBUG_MODE_ON +#define ACCEPT_PKT 0xFFFF +#define DROP_PKT 0 +#define INVALID_IFINDEX 0 + +#define ETH_ASCII 0x45746865 // Ascii equivalent for "Ethe" to filter out Ethernet nl-msgs +#define PO_ASCII 0x506F7274 // Ascii equivalent for "Port" to filter out Portchannel nl-msgs +#define TEAM_ASCII 0x7465616d // Ascii equivalent for "team" to filter out Ethernet nl-msgs + +#ifdef DEBUG_MODE_ON +#define BPF_TR(_fmt,...) \ +{ \ + char fmt[]=_fmt; \ + bpf_trace_printk(fmt, sizeof(fmt), ##__VA_ARGS__); \ +} +#else +#define BPF_TR(_fmt,...) {}; +#endif + + + +/*if(drop_counter_val->count %100 == 0) BPF_TR("DROP COUNT: %d\n",drop_counter_val->count); */ +#define BPF_MAP_DROP_PKT(map) \ +{ \ + KEY counter_key = {0, 50000}; \ + VALUE *counter_val = (VALUE*)bpf_map_lookup_elem(map, &counter_key); \ + if ( counter_val ){ \ + counter_val->count++; \ + BPF_TR("TEAMD: [%d] >> DROP [ %d ] \n\n", 0, counter_val->count); \ + }else{ \ + VALUE init_counter_val ={0}; \ + bpf_map_update_elem(map, &counter_key, &init_counter_val, BPF_NOEXIST); \ + BPF_TR("TEAMD: [%d] >> INIT DROP [ %d ] \n\n", 0, init_counter_val.count); \ + } \ + return DROP_PKT; \ +}\ + + + +#define BPF_MAP_ACCEPT_PKT(map) \ +{ \ + KEY counter_key = {0, 50000}; \ + VALUE *counter_val = (VALUE*)bpf_map_lookup_elem(map, &counter_key); \ + if ( counter_val ){ \ + counter_val->accept++; \ + BPF_TR("TEAMD: [%d] >> ACCEPT [ %d ] \n\n", 0, counter_val->accept); \ + }else{ \ + VALUE init_counter_val ={0}; \ + bpf_map_update_elem(map, &counter_key, &init_counter_val, BPF_NOEXIST); \ + BPF_TR("TEAMD: [%d] >> INIT ACCEPT [ %d ] \n\n", 0, init_counter_val.accept); \ + } \ + return ACCEPT_PKT; \ +}\ + +#endif diff --git a/src/libebpf/src/samples/ebpf-user-mod/Makefile b/src/libebpf/src/samples/ebpf-user-mod/Makefile new file mode 100644 index 000000000000..7615887ac508 --- /dev/null +++ b/src/libebpf/src/samples/ebpf-user-mod/Makefile @@ -0,0 +1,84 @@ +KBUILD_PATH ?= /lib/modules/$(KVERSION)/build +KBUILD_SRC_PATH ?= /lib/modules/$(KVERSION)/source +LLC ?= llc-6.0 +CLANG ?= clang-6.0 +LLVM_OBJDUMP ?= llvm-objdump-6.0 +INC_FLAGS = -nostdinc -isystem $(shell $(CLANG) -print-file-name=include) +EXTRA_CFLAGS ?= -O2 -Wall -emit-llvm -DEBPF_MAPS=shared + +# In case up-to-date headers are not installed locally in /usr/include, +# use source build. + +LINUXINCLUDE = \ + -I../../libutil/ \ + -I$(KBUILD_PATH)/include \ + -I$(KBUILD_PATH)/arch/x86/include \ + -I$(KBUILD_SRC_PATH)/include \ + -I$(KBUILD_SRC_PATH)/arch/x86/include/ \ + -I$(KBUILD_PATH)/include/uapi \ + -I$(KBUILD_SRC_PATH)/include/uapi \ + -I$(KBUILD_SRC_PATH)/arch/x86/include/uapi/ \ + -I$(KBUILD_PATH)/include/generated/uapi \ + -I$(KBUILD_PATH)/arch/x86/include/uapi \ + -I$(KBUILD_PATH)/arch/x86/include/generated/ \ + -I$(KBUILD_PATH)/arch/x86/include/generated/uapi \ + -I/usr/include \ + + +KERNELBPF = ebpftest.bpf +KERNELOBJ = $(KERNELBPF:%.bpf=%.obj) +KERNELOBJDUMPS = $(KERNELBPF:%.bpf=%.objdump) + +prefix ?= $(DESTDIR) +INSTALLPATH = $(prefix)/lib/ebpf + +all: $(KERNELBPF) + +debug: DEBUG_FLAGS = -DBPFDEBUG +debug: all + +.PHONY: clean + +clean: + rm -f $(KERNELBPF) $(KERNELOBJ) $(KERNELOBJDUMPS) + +distclean: clean + +test: + + +install_PROGRAM = install +install_DIR = install -dv + + +$(KERNELOBJ): %.obj:%.c + + $(CLANG) $(INC_FLAGS) \ + -D__KERNEL__ -D__ASM_SYSREG_H \ + $(DEBUG_FLAGS) \ + -Wno-unused-value -Wno-pointer-sign \ + -Wno-compare-distinct-pointer-types \ + -Wno-gnu-variable-sized-type-not-at-end \ + -Wno-address-of-packed-member -Wno-tautological-compare \ + -Wno-unknown-warning-option \ + -I../include $(LINUXINCLUDE) \ + $(EXTRA_CFLAGS) -c $< -o $@ + +$(KERNELBPF): %.bpf:%.obj + cat $^ | $(LLC) -march=bpf -filetype=obj -o $@ + + +$(KERNELOBJDUMPS): %.objdump:%.bpf + $(LLVM_OBJDUMP) -S -no-show-raw-insn $< > $@ + +objdump: $(KERNELOBJDUMPS) + +install: $(KERNELBPF) + $(install_DIR) -d $(INSTALLPATH) ; \ + $(install_PROGRAM) $^ -t $(INSTALLPATH) + #Comment the below line if the filter has to be disabled + touch $(INSTALLPATH)/$^.enable + + +uninstall: $(KERNELBPF) + rm -rf $(INSTALLPATH) diff --git a/src/libebpf/src/samples/ebpf-user-mod/ebpftest.c b/src/libebpf/src/samples/ebpf-user-mod/ebpftest.c new file mode 100755 index 000000000000..f5ac88e78baa --- /dev/null +++ b/src/libebpf/src/samples/ebpf-user-mod/ebpftest.c @@ -0,0 +1,174 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "ebpf/bpf_helpers.h" +#include "ebpf/bpf_helpers.h" +#include "ebpftest.h" +#include + + +struct bpf_map_def SEC("maps") my_map = { + .type = BPF_MAP_TYPE_HASH, + .key_size = sizeof(KEY), + .value_size = sizeof(VALUE), + .max_entries = 256, +}; + +SEC("socket") +int bpf_prog1(struct __sk_buff *skb) +{ + + uint16_t nlmsg_flags = load_half(skb, offsetof(struct nlmsghdr, nlmsg_flags)); + nlmsg_flags = htons(nlmsg_flags); + + // accept all pkts with NLM_F_MULTI flag set + if ( nlmsg_flags & NLM_F_MULTI) + BPF_MAP_ACCEPT_PKT(&my_map); + + uint16_t nlmsg_type = load_half(skb, offsetof(struct nlmsghdr, nlmsg_type)); + nlmsg_type = htons(nlmsg_type); + + //accept pkt for end of Multi msg + if (nlmsg_type == NLMSG_DONE) + BPF_MAP_ACCEPT_PKT(&my_map); + + if ( (nlmsg_type != RTM_NEWLINK) + && (nlmsg_type != RTM_DELLINK)) + { + BPF_TR(" TEAMD : [%d] >> Unknown Type : pkt: %d\n", INVALID_IFINDEX, nlmsg_type); + BPF_MAP_DROP_PKT(&my_map); + } + + uint8_t ifi_family = load_byte(skb, sizeof(struct nlmsghdr) + offsetof(struct ifinfomsg, ifi_family)); + if (ifi_family != AF_UNSPEC) + { + BPF_TR(" TEAMD : [%d] >> Invalid family : %d\n", INVALID_IFINDEX, ifi_family); + BPF_MAP_DROP_PKT(&my_map); + } + + int ifname_hex = 0; + KEY m_key = {0, 0}; + VALUE new_val = {0}; + + int kif_index = load_word(skb, sizeof(struct nlmsghdr) + offsetof(struct ifinfomsg, ifi_index)); + kif_index = _htonl(kif_index); + m_key.ifidx = kif_index; + + int tlv_off_st = (sizeof(struct nlmsghdr) + sizeof(struct ifinfomsg)); + int tlv_off = bpf_find_nlattr(skb, tlv_off_st, IFLA_IFNAME); + if(tlv_off != 0) + { + BPF_TR(" TEAMD : [%d] >> found IFLA_IFNAME @ off : %d\n", m_key.ifidx, tlv_off); + ifname_hex = load_word(skb, tlv_off+sizeof(struct nlattr)); + } + + if ( (ifname_hex != ETH_ASCII) + && (ifname_hex != PO_ASCII) ) + { + BPF_TR(" TEAMD : [%d] >> Not of Ethernet or PortChannel, ifname : %x\n", m_key.ifidx, ifname_hex); + BPF_MAP_DROP_PKT(&my_map); + } + + + BPF_TR(" TEAMD : [%d] >> Recv Pkt : %d\n", m_key.ifidx, nlmsg_type); + + if (nlmsg_type == RTM_DELLINK) + { + bpf_map_delete_elem(&my_map, &m_key); + BPF_MAP_ACCEPT_PKT(&my_map); + } + + int t_flags = load_word(skb, sizeof(struct nlmsghdr) + offsetof(struct ifinfomsg, ifi_flags)); + new_val.team_info.flags |= (t_flags & _htonl(IFF_RUNNING)); + new_val.team_info.flags |= (t_flags & _htonl(IFF_UP)); + + tlv_off = bpf_find_nlattr(skb, tlv_off_st, IFLA_MASTER); + if(tlv_off != 0) + { + BPF_TR(" TEAMD : [%d] >> found IFLA_MASTER @ off : %d\n", m_key.ifidx, tlv_off); + new_val.team_info.master = load_word(skb, tlv_off+sizeof(struct nlattr)); + } + + tlv_off = bpf_find_nlattr(skb, tlv_off_st, IFLA_ADDRESS); + if(tlv_off != 0) + { + BPF_TR(" TEAMD : [%d] >> found IFLA_ADDRESS @ off : %d\n", m_key.ifidx, tlv_off); + new_val.team_info.mac_addr[0] = load_byte(skb, 0+tlv_off+sizeof(struct nlattr)); + new_val.team_info.mac_addr[1] = load_byte(skb, 1+tlv_off+sizeof(struct nlattr)); + new_val.team_info.mac_addr[2] = load_byte(skb, 2+tlv_off+sizeof(struct nlattr)); + new_val.team_info.mac_addr[3] = load_byte(skb, 3+tlv_off+sizeof(struct nlattr)); + new_val.team_info.mac_addr[4] = load_byte(skb, 4+tlv_off+sizeof(struct nlattr)); + new_val.team_info.mac_addr[5] = load_byte(skb, 5+tlv_off+sizeof(struct nlattr)); + } + + tlv_off = bpf_find_nlattr(skb, tlv_off_st, IFLA_PHYS_PORT_ID); + if(tlv_off != 0) + { + BPF_TR(" TEAMD : [%d] >> found IFLA_PHYS_PORT_ID @ off : %d\n", m_key.ifidx, tlv_off); + new_val.team_info.ppid.id[0] = load_byte(skb, 0+tlv_off+sizeof(struct nlattr)); + new_val.team_info.ppid.id[1] = load_byte(skb, 1+tlv_off+sizeof(struct nlattr)); + new_val.team_info.ppid.id[2] = load_byte(skb, 2+tlv_off+sizeof(struct nlattr)); + new_val.team_info.ppid.id[3] = load_byte(skb, 3+tlv_off+sizeof(struct nlattr)); + new_val.team_info.ppid.id[4] = load_byte(skb, 4+tlv_off+sizeof(struct nlattr)); + new_val.team_info.ppid.id[5] = load_byte(skb, 5+tlv_off+sizeof(struct nlattr)); + } + + + tlv_off = bpf_find_nlattr(skb, tlv_off_st, IFLA_LINKINFO); + if(tlv_off != 0) + { + BPF_TR(" TEAMD : [%d] >> found IFLA_LINKINFO @ off : %d\n", m_key.ifidx,tlv_off); + tlv_off = bpf_find_nlattr_nest(skb, tlv_off, IFLA_INFO_SLAVE_KIND); + if(tlv_off != 0) + { + new_val.team_info.slave_info = load_word(skb, tlv_off+sizeof(struct nlattr)); + BPF_TR(" TEAMD : [%d] >> found IFLA_INFO_SLAVE @ off : %d\n slave-info : %x",tlv_off, new_val.team_info.slave_info); + } + else + BPF_TR(" TEAMD : [%d] >> NOT found SLAVE_IFLA_LINKINFO @ off : %d\n", m_key.ifidx,tlv_off); + } + else + BPF_TR(" TEAMD : [%d] >> NOT found IFLA_LINKINFO @ off : %d\n", m_key.ifidx,tlv_off); + + + VALUE *old_val = 0; + uint8_t update_flag = 0; + + old_val = bpf_map_lookup_elem(&my_map, &m_key); + if (old_val) + { + + /*if (memcmp(&old_val->team_info, &new_val.team_info, sizeof(TEAMD_INFO)) != 0)*/ + if ( (old_val->team_info.slave_info != new_val.team_info.slave_info) + || (old_val->team_info.master != new_val.team_info.master) + || (old_val->team_info.flags != new_val.team_info.flags) + || (*((uint32_t *)&old_val->team_info.mac_addr[0]) != *((uint32_t *)&new_val.team_info.mac_addr[0])) + || (*((uint16_t *)&old_val->team_info.mac_addr[4]) != *((uint16_t *)&new_val.team_info.mac_addr[4])) + || (*((uint32_t *)&old_val->team_info.ppid.id[0]) != *((uint32_t *)&new_val.team_info.ppid.id[0])) + || (*((uint16_t *)&old_val->team_info.ppid.id[4]) != *((uint16_t *)&new_val.team_info.ppid.id[4])) + ) + { + new_val.count = old_val->count + 1; + bpf_map_update_elem(&my_map, &m_key, &new_val, BPF_EXIST); + } + else + { + BPF_TR(" TEAMD : [%d] >> No Change Drop pkt\n", m_key.ifidx); + BPF_MAP_DROP_PKT(&my_map); + } + } + else { + bpf_map_update_elem(&my_map, &m_key, &new_val, BPF_NOEXIST); + } + + BPF_TR("TEAMD: [%d] >> ACCEPT\n\n", m_key.ifidx); + BPF_MAP_ACCEPT_PKT(&my_map); +} +char _license[] SEC("license") = "GPL"; diff --git a/src/libebpf/src/samples/ebpf-user-mod/ebpftest.h b/src/libebpf/src/samples/ebpf-user-mod/ebpftest.h new file mode 100755 index 000000000000..46d0e83d1b4c --- /dev/null +++ b/src/libebpf/src/samples/ebpf-user-mod/ebpftest.h @@ -0,0 +1,101 @@ +#ifndef __EBPFTEST_H__ +#define __EBPFTEST_H__ + +#pragma pack(push,1) +typedef struct { + int ifidx; + int pad; +}KEY; + + + +#define MAX_PHYS_ITEM_ID_LEN 32 + +/* This structure holds a unique identifier to identify some + * * physical item (port for example) used by a netdevice. + * */ +#if 0 +struct netdev_phys_item_id { + unsigned char id[MAX_PHYS_ITEM_ID_LEN]; + unsigned char id_len; +}; +#endif + +#define KEY_VALUE_FORMAT "K:%d, V:(S[%x] M[%x] F[%x] MC[%x:%x:%x:%x:%x:%x] P[%x:%x:%x:%x:%x:%x])" +typedef struct { + // i think name can never change for a given kernel ifindex + // not sure why teamd code checks for this as well. + // To reduce complexity of filter logic i'm commenting this out for now. + /*char ifname[IFNAMSIZ];*/ + int slave_info; + int master; + int flags; + char mac_addr[6]; + struct netdev_phys_item_id ppid; +}TEAMD_INFO; + +typedef struct { + int count; + int accept; + TEAMD_INFO team_info; +}VALUE; +#pragma pack(pop) + +/* compiler workaround */ +#define _htonl __builtin_bswap32 + +#define DEBUG_MODE_ON +#define ACCEPT_PKT 0xFFFF +#define DROP_PKT 0 +#define INVALID_IFINDEX 0 + +#define ETH_ASCII 0x45746865 // Ascii equivalent for "Ethe" to filter out Ethernet nl-msgs +#define PO_ASCII 0x506F7274 // Ascii equivalent for "Port" to filter out Portchannel nl-msgs +#define TEAM_ASCII 0x7465616d // Ascii equivalent for "team" to filter out Ethernet nl-msgs + +#ifdef DEBUG_MODE_ON +#define BPF_TR(_fmt,...) \ +{ \ + char fmt[]=_fmt; \ + bpf_trace_printk(fmt, sizeof(fmt), ##__VA_ARGS__); \ +} +#else +#define BPF_TR(_fmt,...) {}; +#endif + + + +/*if(drop_counter_val->count %100 == 0) BPF_TR("DROP COUNT: %d\n",drop_counter_val->count); */ +#define BPF_MAP_DROP_PKT(map) \ +{ \ + KEY counter_key = {0, 50000}; \ + VALUE *counter_val = (VALUE*)bpf_map_lookup_elem(map, &counter_key); \ + if ( counter_val ){ \ + counter_val->count++; \ + BPF_TR("TEAMD: [%d] >> DROP [ %d ] \n\n", 0, counter_val->count); \ + }else{ \ + VALUE init_counter_val ={0}; \ + bpf_map_update_elem(map, &counter_key, &init_counter_val, BPF_NOEXIST); \ + BPF_TR("TEAMD: [%d] >> INIT DROP [ %d ] \n\n", 0, init_counter_val.count); \ + } \ + return DROP_PKT; \ +}\ + + + +#define BPF_MAP_ACCEPT_PKT(map) \ +{ \ + KEY counter_key = {0, 50000}; \ + VALUE *counter_val = (VALUE*)bpf_map_lookup_elem(map, &counter_key); \ + if ( counter_val ){ \ + counter_val->accept++; \ + BPF_TR("TEAMD: [%d] >> ACCEPT [ %d ] \n\n", 0, counter_val->accept); \ + }else{ \ + VALUE init_counter_val ={0}; \ + bpf_map_update_elem(map, &counter_key, &init_counter_val, BPF_NOEXIST); \ + BPF_TR("TEAMD: [%d] >> INIT ACCEPT [ %d ] \n\n", 0, init_counter_val.accept); \ + } \ + return ACCEPT_PKT; \ +}\ + +#endif