Skip to content
This repository has been archived by the owner on Apr 20, 2021. It is now read-only.

Commit

Permalink
Merge pull request #17 from kinvolk/nhlfr/remove-kernel-source-dep
Browse files Browse the repository at this point in the history
Remove kernel source tree dependency
  • Loading branch information
robertgzr authored Aug 2, 2017
2 parents a30f98f + 8938856 commit 967821a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
3 changes: 1 addition & 2 deletions bpf/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
OUTDIR=out
OUTFILE=$(OUTDIR)/cgnet.o

LINUX_HEADERS="/usr/lib/modules/$(shell uname -r)/build"

.PHONY: all build clean

Expand All @@ -11,8 +10,8 @@ $(OUTFILE):
mkdir -p $(OUTDIR)
clang -D__KERNEL__ -D__ASM_SYSREG_H \
-Wno-unused-value -Wno-pointer-sign -Wno-compare-distinct-pointer-types \
-fno-stack-protector \
-O2 -emit-llvm -c "src/cgnet.c" \
$(foreach path,$(LINUX_HEADERS), -I $(path)/arch/x86/include -I $(path)/arch/x86/include/generated -I $(path)/include -I $(path)/include/generated/uapi -I $(path)/arch/x86/include/uapi -I $(path)/include/uapi) \
-o - | llc -march=bpf -filetype=obj -o $(OUTFILE)

bindata.go:
Expand Down
21 changes: 11 additions & 10 deletions bpf/src/cgnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
* limitations under the License.
*/

#include <linux/kconfig.h>
#include <linux/bpf.h>
#include <uapi/linux/tcp.h>
#include <uapi/linux/if_ether.h>
#include <uapi/linux/ip.h>
#include <linux/tcp.h>
#include <linux/if_ether.h>
#include <linux/ip.h>
#include <arpa/inet.h>
#include <stddef.h>
#include "bpf_helpers.h"

struct bpf_map_def SEC("maps/count") count_map = {
Expand All @@ -29,23 +30,23 @@ struct bpf_map_def SEC("maps/count") count_map = {
SEC("cgroup/skb")
int count_packets(struct __sk_buff *skb) {
int packets_key = 0, bytes_key = 1;
u64 *packets = NULL;
u64 *bytes = NULL;
__u64 *packets = 0;
__u64 *bytes = 0;

packets = bpf_map_lookup_elem(&count_map, &packets_key);
if (packets == NULL)
if (packets == 0)
return 0;

*packets += 1;

bytes = bpf_map_lookup_elem(&count_map, &bytes_key);
if (bytes == NULL)
if (bytes == 0)
return 0;

u16 dest = 0;
__u16 dest = 0;
bpf_skb_load_bytes(skb, sizeof(struct iphdr) + offsetof(struct tcphdr, dest), &dest, sizeof(dest));

if (dest == ntohs(80))
if (dest == __constant_ntohs(80))
*bytes += skb->len;

return 1;
Expand Down

0 comments on commit 967821a

Please sign in to comment.