Skip to content

Commit

Permalink
Merge pull request #5 from hawkv6/bpf-refactoring
Browse files Browse the repository at this point in the history
Bpf refactoring
  • Loading branch information
jklaiber authored Sep 5, 2023
2 parents 1a61d43 + 599d75d commit 35a95f6
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 19 deletions.
3 changes: 3 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
---
Language: Cpp
BasedOnStyle: LLVM

# Use tabs for indentation
UseTab: Always
TabWidth: 4
Expand Down
9 changes: 5 additions & 4 deletions src/main.c → bpf/bpf_dns.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>

#include "dns.h"
#include "xdp_map.h"
#include "lib/dns.h"
#include "lib/consts.h"
#include "lib/maps.h"

#define memcpy __builtin_memcpy

Expand Down Expand Up @@ -167,14 +168,14 @@ static int parse_dns_query(struct xdp_md *ctx, void *query_start,
// Fill dns_query.name with zero bytes
// Not doing so will make the verifier complain when dns_query is used as a
// key in bpf_map_lookup
for (i = 0; i < MAX_DNS_NAME_LENGTH; i++) {
for (i = 0; i < MAX_DNS_NAME_LEN; i++) {
q->name[i] = 0;
}
// Fill record_type and class with default values to satisfy verifier
q->record_type = 0;
q->record_class = 0;

for (i = 0; i < MAX_DNS_NAME_LENGTH; i++) {
for (i = 0; i < MAX_DNS_NAME_LEN; i++) {
// Boundary check of cursor. Verifier requires a +1 here.
// Probably because we are advancing the pointer at the end of the loop
if (cursor + 1 > data_end) {
Expand Down
4 changes: 2 additions & 2 deletions src/xdp_struct.h → bpf/lib/client.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef __XDP_STRUCTS_H
#define __XDP_STRUCTS_H
#ifndef __LIB_CLIENT_H
#define __LIB_CLIENT_H

#include <linux/in6.h>
#include <linux/types.h>
Expand Down
6 changes: 3 additions & 3 deletions src/xdp_consts.h → bpf/lib/consts.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef __XDP_CONSTS_H
#define __XDP_CONSTS_H
#ifndef __LIB_CONSTS_H
#define __LIB_CONSTS_H

#define MAX_MAP_ENTRIES 1024
#define MAX_SEGMENTLIST_ENTRIES 10
#define MAX_DOMAIN_NAME_LEN 256
#define MAX_DNS_NAME_LEN 256

#define UDP_P_DNS 53

Expand Down
8 changes: 4 additions & 4 deletions src/dns.h → bpf/lib/dns.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#ifndef __DNS_H
#define __DNS_H
#ifndef __LIB_DNS_H
#define __LIB_DNS_H

#include <linux/in6.h>

#define MAX_DNS_NAME_LENGTH 256
#include "consts.h"

struct dns_hdr {
__u16 transaction_id;
Expand All @@ -26,7 +26,7 @@ struct dns_hdr {
struct dns_query {
__u16 record_type;
__u16 record_class;
char name[MAX_DNS_NAME_LENGTH];
char name[MAX_DNS_NAME_LEN];
} __attribute__((packed));

struct dns_answer {
Expand Down
10 changes: 5 additions & 5 deletions src/xdp_map.h → bpf/lib/maps.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef __XDP_MAPS_H
#define __XDP_MAPS_H
#include "xdp_consts.h"
#include "xdp_struct.h"
#ifndef __LIB_MAPS_H
#define __LIB_MAPS_H
#include "client.h"
#include "consts.h"
#include <bpf/bpf_endian.h>
#include <bpf/bpf_helpers.h>
#include <linux/bpf.h>
Expand All @@ -10,7 +10,7 @@
struct {
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, MAX_MAP_ENTRIES);
__type(key, char[MAX_DOMAIN_NAME_LEN]);
__type(key, char[MAX_DNS_NAME_LEN]);
__type(value, struct client_data);
} client_map SEC(".maps");

Expand Down
Binary file modified out/bin/hawkwing
Binary file not shown.
2 changes: 1 addition & 1 deletion pkg/bpf/bpf.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/cilium/ebpf"
)

//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -no-global-types -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf ../../src/main.c -- -I../../src
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -no-global-types -cc $BPF_CLANG -cflags $BPF_CFLAGS bpf ../../bpf/bpf_dns.c -- -I../../bpf

type ClientData struct {
DstPort uint16
Expand Down
Binary file modified pkg/bpf/bpf_bpfeb.o
Binary file not shown.
Binary file modified pkg/bpf/bpf_bpfel.o
Binary file not shown.

0 comments on commit 35a95f6

Please sign in to comment.