Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
Full support for building wireguard for the UBNT e300 (ER-4/ER-6P/ER-12)

Git submodules:
  * WireGuard 0.0.20190601
  * libmnl 1.0.4
  * musl 1.1.22
  * vyatta-wireguard package and scripts v2.0 branch

Git LFS objects:
  * Cavium Octeon gcc 4.7 toolchain (OCTEON-SDK-5.1-tools.tar.xz) from
    OCTEON-SDK-5.1.tbz in https://github.com/Cavium-Open-Source-Distributions/OCTEON-SDK/
    I repackaged only the toolchain into a tar.xz to save space and avoid
    slow bzip2 decompression.
  * Kernel source (e300_kernel_5174690-gbd11043d0ccc.tgz) from the
    EdgeMAX v2.0.1 GPL release https://dl.ubnt.com/firmwares/edgemax/v2.0.x/GPL.ER-e300.v2.0.1.5174690.tar.bz2
    I repackaged just the kernel source tarball to save space and avoid
    slow bzip2 decompression.

Other:
  * only-use-__vmalloc-for-now.patch from https://gist.github.com/Lochnair/805bf9ab96742d0fe1c25e4130268307
    See Lochnair/vyatta-wireguard#97 for
    context and history
  • Loading branch information
aswild committed Jun 9, 2019
0 parents commit 6e298b2
Show file tree
Hide file tree
Showing 12 changed files with 267 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.tgz filter=lfs diff=lfs merge=lfs -text
*.tar* filter=lfs diff=lfs merge=lfs -text
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/OCTEON-SDK-5.1-tools/
/sysroot/
/wg
/wireguard.ko
*.deb
13 changes: 13 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[submodule "WireGuard"]
path = src/WireGuard
url = https://git.zx2c4.com/WireGuard
[submodule "musl"]
path = src/musl
url = git://git.musl-libc.org/musl
[submodule "libmnl"]
path = src/libmnl
url = git://git.netfilter.org/libmnl
[submodule "vyatta-wireguard"]
path = src/vyatta-wireguard
url = https://github.com/Lochnair/vyatta-wireguard
branch = v2.0
203 changes: 203 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,203 @@
#!/bin/bash

set -eo pipefail

THISDIR="$(readlink -f "$(dirname "$0")")"
SRCDIR="$THISDIR/src"
SYSROOT="$THISDIR/sysroot"

ER_BOARD="e300"
ER_KERNEL_TAR="$(ls "$SRCDIR"/${ER_BOARD}_kernel_*.tgz)"
ER_KERNEL_RELEASE="4.9.79-UBNT"
KERNEL_DIR="$SRCDIR/kernel"

TOOLCHAIN_TAR="$SRCDIR/OCTEON-SDK-5.1-tools.tar.xz"
TOOLCHAIN_DIR="$THISDIR/OCTEON-SDK-5.1-tools"

# Toolchain and directory configurations
TARGET=mips64-octeon-linux-gnu
MUSL_CC="$SYSROOT/bin/musl-gcc"

export PATH="$TOOLCHAIN_DIR/bin:$PATH"
export CROSS_COMPILE="${TARGET}-"
export CC="${CROSS_COMPILE}gcc"
export CFLAGS="-O2 -mabi=64"
export ARCH="mips"
export PKG_CONFIG_PATH="$SYSROOT/lib/pkgconfig"

if [[ -z "$MAKEFLAGS" ]] && type nproc &>/dev/null; then
export MAKEFLAGS="-j$(nproc)"
fi

# set terminal colors
if [[ -t 1 ]]; then
CYAN=$'\033[1;36m'
BLUE=$'\033[1;34m'
NC=$'\033[0m'
else
CYAN=
BLUE=
NC=
fi

cd "$THISDIR"

# utility functions
msg() {
echo "${CYAN}>>> ${*} <<<${NC}"
}

run() {
echo "${BLUE}+ ${*}${NC}"
"$@"
}

# Build step functions, organized in the order a full build would run
submodules_prepare() {
msg "Initializing submodules"
run git submodule update --init
}

submodules_build() {
:
}

prepare_toolchain() {
if [[ ! -f "$TOOLCHAIN_DIR/bin/$CC" ]]; then
msg "Extract toolchain"
run rm -rf "$TOOLCHAIN_DIR"
run tar xf "$TOOLCHAIN_TAR"
else
msg "Toolchain already extracted"
fi
}

build_toolchain() {
:
}

prepare_kernel() {
msg "Extracting kernel source"
run rm -rf "$KERNEL_DIR"
run tar xf "$ER_KERNEL_TAR" -C "$SRCDIR"
}

build_kernel() {
msg "Configure kernel source"
pushd "$KERNEL_DIR"
run make ubnt_er_${ER_BOARD}_defconfig
run make modules_prepare
msg "Install kernel headers"
run make INSTALL_HDR_PATH="$SYSROOT" headers_install
popd
}

prepare_musl() {
pushd "$SRCDIR/musl"
msg "Clean musl"
run git reset --hard
run git clean -dxfq
popd
}

build_musl() {
pushd "$SRCDIR/musl"
msg "Configure musl"
./configure --host=x86_64 --target=$TARGET --prefix=$SYSROOT --enable-static --disable-shared
msg "Build musl"
run make
msg "Install musl"
run make install
popd
}

prepare_libmnl() {
pushd "$SRCDIR/libmnl"
msg "Clean libmnl"
run git reset --hard
run git clean -dxfq
popd
}

build_libmnl() {
pushd "$SRCDIR/libmnl"
msg "Configure libmnl"
run ./autogen.sh
run ./configure CC=$MUSL_CC --prefix=$SYSROOT --host=$TARGET --enable-static --disable-shared
msg "Build libmnl"
run make
msg "Install libmnl"
run make install
popd
}

prepare_wireguard() {
pushd "$SRCDIR/WireGuard"
msg "Clean WireGuard"
run git reset --hard
run git clean -dxfq
msg "Patch WireGuard with __vmalloc fix"
# https://gist.github.com/Lochnair/805bf9ab96742d0fe1c25e4130268307
run git apply "$SRCDIR/only-use-__vmalloc-for-now.patch"
popd
}

build_wireguard() {
# "make module" in the WireGuard directory will update its version.h
# and add "-dirty" since we patched the source. Avoid this by building
# the module directly with the kernel Makefiles.
run make -C "$KERNEL_DIR" M="$SRCDIR/WireGuard/src" modules
run install -m644 "$SRCDIR/WireGuard/src/wireguard.ko" "$THISDIR"

run make -C "$SRCDIR/WireGuard/src/tools" CC="$MUSL_CC"
run cp -f "$SRCDIR/WireGuard/src/tools/wg" .
run ${CROSS_COMPILE}strip wg
}

prepare_package() {
pushd "$SRCDIR/vyatta-wireguard"
msg "Clean deb package"
run git reset --hard
run git clean -dxfq
popd
}

build_package() {
local wireguard_ver="$(git -C "$SRCDIR/WireGuard" describe --dirty=)"
if [[ -z "$wireguard_ver" ]]; then
msg "ERROR: Unable to get WireGuard version"
return 1
fi

pushd "$SRCDIR/vyatta-wireguard"
msg "Build deb package"
run install -m644 "$THISDIR/wireguard.ko" "$ER_BOARD/lib/modules/$ER_KERNEL_RELEASE/kernel/net/"
run install -m755 "$THISDIR/wg" "$SRCDIR/vyatta-wireguard/$ER_BOARD/usr/bin/"
run sed -i "s/^Version:.*/Version: ${wireguard_ver}-1/" "$SRCDIR/vyatta-wireguard/debian/control"
run make -j1 deb-${ER_BOARD}
run install -m644 package/*.deb "$THISDIR"
popd
}

clean_all() {
run rm -rf "$TOOLCHAIN_DIR" "$KERNEL_DIR" sysroot wg wireguard.ko *.deb
run git submodule foreach 'git reset --hard; git clean -dxfq'
}

if (( $# == 0 )); then
set -- submodules toolchain kernel musl libmnl wireguard package
fi
for x in "$@"; do
case $x in
build_*|prepare_*)
eval $x
;;
clean)
clean_all
;;
*)
eval prepare_$x
eval build_$x
;;
esac
done
1 change: 1 addition & 0 deletions src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/kernel/
3 changes: 3 additions & 0 deletions src/OCTEON-SDK-5.1-tools.tar.xz
Git LFS file not shown
1 change: 1 addition & 0 deletions src/WireGuard
Submodule WireGuard added at 14cb37
3 changes: 3 additions & 0 deletions src/e300_kernel_5174690-gbd11043d0ccc.tgz
Git LFS file not shown
1 change: 1 addition & 0 deletions src/libmnl
Submodule libmnl added at 0930a6
1 change: 1 addition & 0 deletions src/musl
Submodule musl added at e97681
33 changes: 33 additions & 0 deletions src/only-use-__vmalloc-for-now.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
From 00a970f9ce4a97e8f46f24c90c0242a658143457 Mon Sep 17 00:00:00 2001
From: Nils Andreas Svee <me@lochnair.net>
Date: Sun, 3 Mar 2019 23:44:16 +0100
Subject: [PATCH] Only use __vmalloc for now

---
src/compat/compat.h | 10 ----------
1 file changed, 10 deletions(-)

diff --git a/src/compat/compat.h b/src/compat/compat.h
index 7a61e4c..f2b3d5e 100644
--- a/src/compat/compat.h
+++ b/src/compat/compat.h
@@ -464,16 +464,6 @@ static inline __be32 our_inet_confirm_addr(struct net *net, struct in_device *in
#include <linux/slab.h>
static inline void *kvmalloc_ours(size_t size, gfp_t flags)
{
- gfp_t kmalloc_flags = flags;
- void *ret;
- if (size > PAGE_SIZE) {
- kmalloc_flags |= __GFP_NOWARN;
- if (!(kmalloc_flags & __GFP_REPEAT) || (size <= PAGE_SIZE << PAGE_ALLOC_COSTLY_ORDER))
- kmalloc_flags |= __GFP_NORETRY;
- }
- ret = kmalloc(size, kmalloc_flags);
- if (ret || size <= PAGE_SIZE)
- return ret;
return __vmalloc(size, flags, PAGE_KERNEL);
}
static inline void *kvzalloc_ours(size_t size, gfp_t flags)
--
2.21.0

1 change: 1 addition & 0 deletions src/vyatta-wireguard
Submodule vyatta-wireguard added at 44a252

0 comments on commit 6e298b2

Please sign in to comment.