Skip to content

Commit

Permalink
Merge pull request #4 from nttcom/refactor/xdp
Browse files Browse the repository at this point in the history
Refactor XDP files
  • Loading branch information
Motok1 authored Jul 20, 2023
2 parents e4c4fce + 161e083 commit 3581efc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 26 deletions.
9 changes: 4 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
# Copyright (c) 2023 NTT Communications Corporation
# Copyright (c) 2023 Takeru Hayasaka
# Copyright (c) 2023 Takeru Hayasaka

GOCMD=go
BINARY_NAME=fluvia
CLANG ?= clang
CFLAGS := -O2 -g -Wall $(CFLAGS)
DIFF_FROM_BRANCH_NAME ?= origin/main

GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
CYAN := $(shell tput -Txterm setaf 6)
RESET := $(shell tput -Txterm sgr0)

.PHONY: all build clean
.PHONY: all build clean

all: go-gen build

build:
build:
mkdir -p out/bin
$(GOCMD) build -o out/bin/$(BINARY_NAME) ./cmd/$(BINARY_NAME)/main.go

Expand All @@ -36,6 +35,6 @@ help:
@echo ''
@echo 'Targets:'
@awk 'BEGIN {FS = ":.*?## "} { \
if (/^[a-zA-Z_-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \
if (/^[a-zA-Z_0-9-]+:.*?##.*$$/) {printf " ${YELLOW}%-20s${GREEN}%s${RESET}\n", $$1, $$2} \
else if (/^## .*$$/) {printf " ${CYAN}%s${RESET}\n", substr($$1,4)} \
}' $(MAKEFILE_LIST)
28 changes: 15 additions & 13 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2023 NTT Communications Corporation
* Copyright (c) 2023 Takeru Hayasaka
* Copyright (c) 2023 Takeru Hayasaka
*/

#define KBUILD_MODNAME "xdp_probe"
Expand All @@ -18,12 +18,13 @@

#include "xdp_map.h"

SEC("xdp")
int xdp_prog(struct xdp_md *ctx) {
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
__u32 probe_key = XDP_PASS;
struct ethhdr *eth = data;
SEC("xdp")
int xdp_prog(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
void *data = (void *)(long)ctx->data;
__u32 probe_key = XDP_PASS;
struct ethhdr *eth = data;

if ((void *)(eth + 1) > data_end)
return XDP_PASS;
Expand Down Expand Up @@ -62,27 +63,28 @@ int xdp_prog(struct xdp_md *ctx) {
key.flags = srh->flags;
key.tag = srh->tag;

for(int i=0; i<MAX_SEGMENTLIST_ENTRIES; i++ )
{
if (!(i < key.lastEntry + 1) )
for (int i = 0; i < MAX_SEGMENTLIST_ENTRIES; i++)
{
if (!(i < key.lastEntry + 1))
break;

if ((void *)(data + sizeof(struct ethhdr) + sizeof(struct ipv6hdr) + sizeof(struct srhhdr) + sizeof(struct in6_addr) * (i + 1) + 1) > data_end)
break;

__builtin_memcpy(&key.segments[i], &srh->segments[i], sizeof(struct in6_addr));
}

value = bpf_map_lookup_elem(&ipfix_probe_map, &key);
if (!value) {
if (!value)
{
bpf_map_update_elem(&ipfix_probe_map, &key, &zero, BPF_NOEXIST);
value = bpf_map_lookup_elem(&ipfix_probe_map, &key);
if (!value)
return XDP_PASS;
}
(*value)++;

return XDP_PASS;
return XDP_PASS;
}

char _license[] SEC("license") = "MIT";
2 changes: 1 addition & 1 deletion src/xdp_consts.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2023 NTT Communications Corporation
* Copyright (c) 2023 Takeru Hayasaka
* Copyright (c) 2023 Takeru Hayasaka
*/

#ifndef __XDP_CONSTS_H
Expand Down
5 changes: 3 additions & 2 deletions src/xdp_map.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2023 NTT Communications Corporation
* Copyright (c) 2023 Takeru Hayasaka
* Copyright (c) 2023 Takeru Hayasaka
*/

#ifndef __XDP_MAPS_H
Expand All @@ -12,7 +12,8 @@
#include <bpf/bpf_endian.h>
#include "xdp_consts.h"
#include "xdp_struct.h"
struct {
struct
{
__uint(type, BPF_MAP_TYPE_LRU_HASH);
__uint(max_entries, MAX_MAP_ENTRIES);
__type(key, struct probe_data);
Expand Down
11 changes: 6 additions & 5 deletions src/xdp_struct.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (c) 2023 NTT Communications Corporation
* Copyright (c) 2023 Takeru Hayasaka
* Copyright (c) 2023 Takeru Hayasaka
*/

#ifndef __XDP_STRUCTS_H
Expand All @@ -25,10 +25,11 @@ struct srhhdr
struct in6_addr segments[0];
};

struct probe_data {
__u8 h_dest[ETH_ALEN];
__u8 h_source[ETH_ALEN];
__be16 h_proto;
struct probe_data
{
__u8 h_dest[ETH_ALEN];
__u8 h_source[ETH_ALEN];
__be16 h_proto;
struct in6_addr v6_srcaddr;
struct in6_addr v6_dstaddr;
__u8 nextHdr;
Expand Down

0 comments on commit 3581efc

Please sign in to comment.