Skip to content

Commit

Permalink
Update packet examples to compile with libxdp
Browse files Browse the repository at this point in the history
Signed-off-by: Donald Hunter <[email protected]>
  • Loading branch information
donaldh committed Mar 3, 2023
1 parent f7fef8b commit e2bea69
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 41 deletions.
12 changes: 6 additions & 6 deletions common/xdp_stats_kern.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
#endif

/* Keeps stats per (enum) xdp_action */
struct bpf_map_def SEC("maps") xdp_stats_map = {
.type = BPF_MAP_TYPE_PERCPU_ARRAY,
.key_size = sizeof(__u32),
.value_size = sizeof(struct datarec),
.max_entries = XDP_ACTION_MAX,
};
struct {
__uint(type, BPF_MAP_TYPE_PERCPU_ARRAY);
__type(key, __u32);
__type(value, struct datarec);
__uint(max_entries, XDP_ACTION_MAX);
} xdp_stats_map SEC(".maps");

static __always_inline
__u32 xdp_stats_record_action(struct xdp_md *ctx, __u32 action)
Expand Down
27 changes: 14 additions & 13 deletions packet-solutions/xdp_prog_kern_03.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,20 @@
#define memcpy(dest, src, n) __builtin_memcpy((dest), (src), (n))
#endif

struct bpf_map_def SEC("maps") tx_port = {
.type = BPF_MAP_TYPE_DEVMAP,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 256,
};

struct bpf_map_def SEC("maps") redirect_params = {
.type = BPF_MAP_TYPE_HASH,
.key_size = ETH_ALEN,
.value_size = ETH_ALEN,
.max_entries = 1,
};
struct {
__uint(type, BPF_MAP_TYPE_DEVMAP);
__type(key, int);
__type(value, int);
__uint(max_entries, 256);
} tx_port SEC(".maps");


struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, unsigned char[ETH_ALEN]);
__type(value, unsigned char[ETH_ALEN]);
__uint(max_entries, 1);
} redirect_params SEC(".maps");

static __always_inline __u16 csum_fold_helper(__u32 csum)
{
Expand Down
2 changes: 1 addition & 1 deletion packet01-parsing/xdp_prog_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static __always_inline int parse_ethhdr(struct hdr_cursor *nh,
{
}*/

SEC("xdp_packet_parser")
SEC("xdp")
int xdp_parser_func(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
Expand Down
6 changes: 3 additions & 3 deletions packet02-rewriting/xdp_prog_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static __always_inline int vlan_tag_push(struct xdp_md *ctx,
}

/* Implement assignment 1 in this section */
SEC("xdp_port_rewrite")
SEC("xdp")
int xdp_port_rewrite_func(struct xdp_md *ctx)
{
return XDP_PASS;
Expand All @@ -63,7 +63,7 @@ int xdp_port_rewrite_func(struct xdp_md *ctx)
/* VLAN swapper; will pop outermost VLAN tag if it exists, otherwise push a new
* one with ID 1. Use this for assignments 2 and 3.
*/
SEC("xdp_vlan_swap")
SEC("xdp")
int xdp_vlan_swap_func(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
Expand Down Expand Up @@ -91,7 +91,7 @@ int xdp_vlan_swap_func(struct xdp_md *ctx)
/* Solution to the parsing exercise in lesson packet01. Handles VLANs and legacy
* IP (via the helpers in parsing_helpers.h).
*/
SEC("xdp_packet_parser")
SEC("xdp")
int xdp_parser_func(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
Expand Down
40 changes: 22 additions & 18 deletions packet03-redirecting/xdp_prog_kern.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@
#define memcpy(dest, src, n) __builtin_memcpy((dest), (src), (n))
#endif

struct bpf_map_def SEC("maps") tx_port = {
.type = BPF_MAP_TYPE_DEVMAP,
.key_size = sizeof(int),
.value_size = sizeof(int),
.max_entries = 256,
};

struct bpf_map_def SEC("maps") redirect_params = {
.type = BPF_MAP_TYPE_HASH,
.key_size = ETH_ALEN,
.value_size = ETH_ALEN,
.max_entries = 1,
};
struct {
__uint(type, BPF_MAP_TYPE_DEVMAP);
__type(key, int);
__type(value, int);
__uint(max_entries, 256);
} tx_port SEC(".maps");


struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, unsigned char[ETH_ALEN]);
__type(value, unsigned char[ETH_ALEN]);
__uint(max_entries, 1);
} redirect_params SEC(".maps");

static __always_inline void swap_src_dst_mac(struct ethhdr *eth)
{
/* Assignment 1: swap source and destination addresses in the eth.
Expand All @@ -45,7 +47,7 @@ static __always_inline void swap_src_dst_ipv4(struct iphdr *iphdr)
}

/* Implement packet03/assignment-1 in this section */
SEC("xdp_icmp_echo")
SEC("xdp")
int xdp_icmp_echo_func(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
Expand Down Expand Up @@ -104,14 +106,16 @@ int xdp_icmp_echo_func(struct xdp_md *ctx)
/* Assignment 1: patch the packet and update the checksum. You can use
* the echo_reply variable defined above to fix the ICMP Type field. */

bpf_printk("echo_reply: %d", echo_reply);

action = XDP_TX;

out:
return xdp_stats_record_action(ctx, action);
}

/* Assignment 2 */
SEC("xdp_redirect")
SEC("xdp")
int xdp_redirect_func(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
Expand Down Expand Up @@ -139,7 +143,7 @@ int xdp_redirect_func(struct xdp_md *ctx)
}

/* Assignment 3: nothing to do here, patch the xdp_prog_user.c program */
SEC("xdp_redirect_map")
SEC("xdp")
int xdp_redirect_map_func(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
Expand Down Expand Up @@ -179,7 +183,7 @@ static __always_inline int ip_decrease_ttl(struct iphdr *iph)
}

/* Assignment 4: Complete this router program */
SEC("xdp_router")
SEC("xdp")
int xdp_router_func(struct xdp_md *ctx)
{
void *data_end = (void *)(long)ctx->data_end;
Expand Down Expand Up @@ -265,7 +269,7 @@ int xdp_router_func(struct xdp_md *ctx)
return xdp_stats_record_action(ctx, action);
}

SEC("xdp_pass")
SEC("xdp")
int xdp_pass_func(struct xdp_md *ctx)
{
return XDP_PASS;
Expand Down

0 comments on commit e2bea69

Please sign in to comment.