-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This program binds a program to a cgroup and then matches hard coded IP addresses and adds these to a sockmap. This will receive messages from the backend and send them to the client. client:X <---> frontend:10000 client:X <---> backend:10001 To keep things simple this is only designed for 1:1 connections using hard coded values. A more complete example would allow many backends and clients. To run, # sockmap <cgroup2_dir> Signed-off-by: John Fastabend <[email protected]> Signed-off-by: David S. Miller <[email protected]>
- Loading branch information
Showing
8 changed files
with
547 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# kbuild trick to avoid linker error. Can be omitted if a module is built. | ||
obj- := dummy.o | ||
|
||
# List of programs to build | ||
hostprogs-y := sockmap | ||
|
||
# Libbpf dependencies | ||
LIBBPF := ../../tools/lib/bpf/bpf.o | ||
|
||
HOSTCFLAGS += -I$(objtree)/usr/include | ||
HOSTCFLAGS += -I$(srctree)/tools/lib/ | ||
HOSTCFLAGS += -I$(srctree)/tools/testing/selftests/bpf/ | ||
HOSTCFLAGS += -I$(srctree)/tools/lib/ -I$(srctree)/tools/include | ||
HOSTCFLAGS += -I$(srctree)/tools/perf | ||
|
||
sockmap-objs := ../bpf/bpf_load.o $(LIBBPF) sockmap_user.o | ||
|
||
# Tell kbuild to always build the programs | ||
always := $(hostprogs-y) | ||
always += sockmap_kern.o | ||
|
||
HOSTLOADLIBES_sockmap += -lelf -lpthread | ||
|
||
# Allows pointing LLC/CLANG to a LLVM backend with bpf support, redefine on cmdline: | ||
# make samples/bpf/ LLC=~/git/llvm/build/bin/llc CLANG=~/git/llvm/build/bin/clang | ||
LLC ?= llc | ||
CLANG ?= clang | ||
|
||
# Trick to allow make to be run from this directory | ||
all: | ||
$(MAKE) -C ../../ $(CURDIR)/ | ||
|
||
clean: | ||
$(MAKE) -C ../../ M=$(CURDIR) clean | ||
@rm -f *~ | ||
|
||
$(obj)/syscall_nrs.s: $(src)/syscall_nrs.c | ||
$(call if_changed_dep,cc_s_c) | ||
|
||
$(obj)/syscall_nrs.h: $(obj)/syscall_nrs.s FORCE | ||
$(call filechk,offsets,__SYSCALL_NRS_H__) | ||
|
||
clean-files += syscall_nrs.h | ||
|
||
FORCE: | ||
|
||
|
||
# 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 | ||
|
||
$(src)/*.c: 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. | ||
$(obj)/%.o: $(src)/%.c | ||
$(CLANG) $(NOSTDINC_FLAGS) $(LINUXINCLUDE) $(EXTRA_CFLAGS) -I$(obj) \ | ||
-D__KERNEL__ -D__ASM_SYSREG_H -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 \ | ||
-O2 -emit-llvm -c $< -o -| $(LLC) -march=bpf -filetype=obj -o $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of version 2 of the GNU General Public | ||
* License as published by the Free Software Foundation. | ||
* | ||
* 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 <uapi/linux/bpf.h> | ||
#include <uapi/linux/if_ether.h> | ||
#include <uapi/linux/if_packet.h> | ||
#include <uapi/linux/ip.h> | ||
#include "../../tools/testing/selftests/bpf/bpf_helpers.h" | ||
#include "../../tools/testing/selftests/bpf/bpf_endian.h" | ||
|
||
/* Sockmap sample program connects a client and a backend together | ||
* using cgroups. | ||
* | ||
* client:X <---> frontend:80 client:X <---> backend:80 | ||
* | ||
* For simplicity we hard code values here and bind 1:1. The hard | ||
* coded values are part of the setup in sockmap.sh script that | ||
* is associated with this BPF program. | ||
* | ||
* The bpf_printk is verbose and prints information as connections | ||
* are established and verdicts are decided. | ||
*/ | ||
|
||
#define bpf_printk(fmt, ...) \ | ||
({ \ | ||
char ____fmt[] = fmt; \ | ||
bpf_trace_printk(____fmt, sizeof(____fmt), \ | ||
##__VA_ARGS__); \ | ||
}) | ||
|
||
struct bpf_map_def SEC("maps") sock_map = { | ||
.type = BPF_MAP_TYPE_SOCKMAP, | ||
.key_size = sizeof(int), | ||
.value_size = sizeof(int), | ||
.max_entries = 20, | ||
}; | ||
|
||
SEC("sk_skb1") | ||
int bpf_prog1(struct __sk_buff *skb) | ||
{ | ||
return skb->len; | ||
} | ||
|
||
SEC("sk_skb2") | ||
int bpf_prog2(struct __sk_buff *skb) | ||
{ | ||
__u32 lport = skb->local_port; | ||
__u32 rport = skb->remote_port; | ||
int ret = 0; | ||
|
||
if (lport == 10000) | ||
ret = 10; | ||
else | ||
ret = 1; | ||
|
||
bpf_printk("sockmap: %d -> %d @ %d\n", lport, bpf_ntohl(rport), ret); | ||
return bpf_sk_redirect_map(&sock_map, ret, 0); | ||
} | ||
|
||
SEC("sockops") | ||
int bpf_sockmap(struct bpf_sock_ops *skops) | ||
{ | ||
__u32 lport, rport; | ||
int op, err = 0, index, key, ret; | ||
|
||
|
||
op = (int) skops->op; | ||
|
||
switch (op) { | ||
case BPF_SOCK_OPS_PASSIVE_ESTABLISHED_CB: | ||
lport = skops->local_port; | ||
rport = skops->remote_port; | ||
|
||
if (lport == 10000) { | ||
ret = 1; | ||
err = bpf_sock_map_update(skops, &sock_map, &ret, | ||
BPF_NOEXIST, | ||
BPF_SOCKMAP_STRPARSER); | ||
bpf_printk("passive(%i -> %i) map ctx update err: %d\n", | ||
lport, bpf_ntohl(rport), err); | ||
} | ||
break; | ||
case BPF_SOCK_OPS_ACTIVE_ESTABLISHED_CB: | ||
lport = skops->local_port; | ||
rport = skops->remote_port; | ||
|
||
if (bpf_ntohl(rport) == 10001) { | ||
ret = 10; | ||
err = bpf_sock_map_update(skops, &sock_map, &ret, | ||
BPF_NOEXIST, | ||
BPF_SOCKMAP_STRPARSER); | ||
bpf_printk("active(%i -> %i) map ctx update err: %d\n", | ||
lport, bpf_ntohl(rport), err); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
return 0; | ||
} | ||
char _license[] SEC("license") = "GPL"; |
Oops, something went wrong.