Skip to content

Commit

Permalink
Merge pull request #14 from wide-vsix/slankdev-less-dependency
Browse files Browse the repository at this point in the history
Less Dependency
  • Loading branch information
slankdev authored Oct 25, 2022
2 parents cc93618 + 80d0954 commit 4b17c69
Show file tree
Hide file tree
Showing 42 changed files with 9,722 additions and 14 deletions.
41 changes: 41 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
ARG CILIUM_LLVM_IMAGE=quay.io/cilium/cilium-llvm:3408daa17f6490a464dfc746961e28ae31964c66
ARG UBUNTU_IMAGE=docker.io/library/ubuntu:22.04

# STAGE(llvm-dist)
FROM ${CILIUM_LLVM_IMAGE} as llvm-dist

# STAGE(iproute2-dist)
FROM ${UBUNTU_IMAGE} as iproute2-dist
RUN DEBIAN_FRONTEND=noninteractive apt update && apt install -y \
vim curl git gcc make flex bison clang-12 libbsd-dev libbfd-dev \
libcap-dev libelf-dev gcc-multilib pkg-config linux-tools-`uname -r`
ARG LIBBPF_VERSION="0.8.0"
ARG IPROUTE2_VERSION="5.18.0"
ADD https://github.com/libbpf/libbpf/archive/refs/tags/v${LIBBPF_VERSION}.tar.gz .
RUN tar xvf v${LIBBPF_VERSION}.tar.gz
RUN cd libbpf-${LIBBPF_VERSION}/src && make install BUILD_STATIC_ONLY=1 && make install_pkgconfig
ADD https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/snapshot/iproute2-${IPROUTE2_VERSION}.tar.gz .
RUN tar xvf iproute2-${IPROUTE2_VERSION}.tar.gz
RUN cd iproute2-${IPROUTE2_VERSION} && ./configure --libbpf_force=on --libbpf_dir=/ && make install

# STAGE(flowctl-dist)
FROM golang:1.17 as flowctl-dist
WORKDIR /opt
COPY ./ ./
RUN CGO_ENABLED=0 go build -o ./bin/flowctl ./cmd/flowctl/main.go

# STAGE(rootfs)
FROM ${UBUNTU_IMAGE} as rootfs
RUN DEBIAN_FRONTEND=noninteractive apt update && apt install -y --no-install-recommends \
curl libelf1 libmnl0 bash-completion iptables ipset kmod ca-certificates \
libelf-dev libbsd-dev jq
COPY ./bpf /usr/include/bpf
COPY --from=llvm-dist /usr/local/bin/clang /usr/local/bin/llc /usr/local/bin/
COPY --from=iproute2-dist /usr/sbin/ip /usr/sbin/ss /usr/sbin/tc /usr/sbin/
COPY --from=flowctl-dist /opt/bin/flowctl /usr/bin/
RUN echo "source /etc/bash_completion" >> /root/.bashrc
RUN echo ". <(flowctl completion bash)" >> /root/.bashrc

# FINAL STAGE
FROM scratch
COPY --from=rootfs / /
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ sudo flowctl meter attach --netns ns0 -n eth3
```

### flowctl usage

```shell
docker run --rm --name tmp -it --privileged --net=host -v /usr/include/asm:/usr/include/asm -v /var/run/netns:/var/run/netns ghcr.io/wide-vsix/linux-flow-exporter:branch-master bash
flowctl meter status
```

```shell
## How to check current flow cache
$ sudo flowctl dump
Expand Down Expand Up @@ -179,3 +185,13 @@ It may be updated by LINE Corporation

```
```

## Licence

The user space components are licensed under the
[Apache License, Version 2.0](https://github.com/wide-vsix/linux-flow-exporter/blob/main/LICENSE).
The BPF code templates are dual-licensed under the
[General Public License](https://github.com/wide-vsix/linux-flow-exporter/blob/main/bpf/LICENSE.GPL-2.0),
Version 2.0 (only) and the
[2-Clause BSD License](https://github.com/wide-vsix/linux-flow-exporter/blob/main/bpf/LICENSE.BSD-2-Clause)
(you can use the terms of either license, at your option).
23 changes: 23 additions & 0 deletions bpf/LICENSE.BSD-2-Clause
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright Authors of Cilium. All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
341 changes: 341 additions & 0 deletions bpf/LICENSE.GPL-2.0

Large diffs are not rendered by default.

37 changes: 37 additions & 0 deletions bpf/access.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
/* Copyright Authors of Cilium */

#ifndef __BPF_ACCESS_H_
#define __BPF_ACCESS_H_

#include "compiler.h"

#if defined(__bpf__)
static __always_inline __maybe_unused __u32
map_array_get_32(const __u32 *array, __u32 index, const __u32 limit)
{
__u32 datum = 0;

if (__builtin_constant_p(index) ||
!__builtin_constant_p(limit))
__throw_build_bug();

/* LLVM tends to optimize code away that is needed for the verifier to
* understand dynamic map access. Input constraint is that index < limit
* for this util function, so we never fail here, and returned datum is
* always valid.
*/
asm volatile("%[index] <<= 2\n\t"
"if %[index] > %[limit] goto +1\n\t"
"%[array] += %[index]\n\t"
"%[datum] = *(u32 *)(%[array] + 0)\n\t"
: [datum]"=r"(datum)
: [limit]"i"(limit), [array]"r"(array), [index]"r"(index)
: /* no clobbers */ );

return datum;
}
#else
# define map_array_get_32(array, index, limit) __throw_build_bug()
#endif /* __bpf__ */
#endif /* __BPF_ACCESS_H_ */
22 changes: 22 additions & 0 deletions bpf/api.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
/* Copyright Authors of Cilium */

#ifndef __BPF_API__
#define __BPF_API__

#include <linux/types.h>
#include <linux/byteorder.h>
#include <linux/bpf.h>
#include <linux/if_packet.h>

#include "compiler.h"
#include "section.h"
#include "helpers.h"
#include "builtins.h"
#include "tailcall.h"
#include "errno.h"
#include "loader.h"
#include "csum.h"
#include "access.h"

#endif /* __BPF_API__ */
Loading

0 comments on commit 4b17c69

Please sign in to comment.