|
| 1 | +/* SPDX-License-Identifier: GPL-2.0 */ |
| 2 | +#include <linux/bpf.h> |
| 3 | +#include <linux/in.h> |
| 4 | +#include <bpf/bpf_helpers.h> |
| 5 | +#include <bpf/bpf_endian.h> |
| 6 | + |
| 7 | +// The parsing helper functions from the packet01 lesson have moved here |
| 8 | +#include "../common/parsing_helpers.h" |
| 9 | +#include "../common/rewrite_helpers.h" |
| 10 | + |
| 11 | +/* Defines xdp_stats_map */ |
| 12 | +#include "../common/xdp_stats_kern_user.h" |
| 13 | +#include "../common/xdp_stats_kern.h" |
| 14 | + |
| 15 | +struct my_timestamp { |
| 16 | + __u16 magic; |
| 17 | + __u64 time; |
| 18 | +} __attribute__((packed)); |
| 19 | + |
| 20 | +SEC("xdp_tailgrow_parse") |
| 21 | +int grow_parse(struct xdp_md *ctx) |
| 22 | +{ |
| 23 | + void *data_end; |
| 24 | + void *data; |
| 25 | + int action = XDP_PASS; |
| 26 | + int eth_type, ip_type; |
| 27 | + struct hdr_cursor nh; |
| 28 | + struct iphdr *iphdr; |
| 29 | + struct ethhdr *eth; |
| 30 | + __u16 ip_tot_len; |
| 31 | + |
| 32 | + struct my_timestamp *ts; |
| 33 | + |
| 34 | + /* Increase packet size (at tail) and reload data pointers */ |
| 35 | + __u8 offset = sizeof(*ts); |
| 36 | + if (bpf_xdp_adjust_tail(ctx, offset)) |
| 37 | + goto out; |
| 38 | + data_end = (void *)(long)ctx->data_end; |
| 39 | + data = (void *)(long)ctx->data; |
| 40 | + |
| 41 | + /* These keep track of the next header type and iterator pointer */ |
| 42 | + nh.pos = data; |
| 43 | + |
| 44 | + eth_type = parse_ethhdr(&nh, data_end, ð); |
| 45 | + if (eth_type < 0) { |
| 46 | + action = XDP_ABORTED; |
| 47 | + goto out; |
| 48 | + } |
| 49 | + |
| 50 | + if (eth_type == bpf_htons(ETH_P_IP)) { |
| 51 | + ip_type = parse_iphdr(&nh, data_end, &iphdr); |
| 52 | + } else { |
| 53 | + action = XDP_PASS; |
| 54 | + goto out; |
| 55 | + } |
| 56 | + |
| 57 | + /* Demo use-case: Add timestamp in extended tailroom to ICMP packets, |
| 58 | + * before sending to network-stack via XDP_PASS. This can be |
| 59 | + * captured via tcpdump, and provide earlier (XDP layer) timestamp. |
| 60 | + */ |
| 61 | + if (ip_type == IPPROTO_ICMP) { |
| 62 | + |
| 63 | + /* Packet size in bytes, including IP header and data */ |
| 64 | + ip_tot_len = bpf_ntohs(iphdr->tot_len); |
| 65 | + |
| 66 | + /* |
| 67 | + * Tricks to get pass the verifier. Being allowed to use |
| 68 | + * packet value iphdr->tot_len, involves bounding possible |
| 69 | + * values to please verifier. |
| 70 | + */ |
| 71 | + if (ip_tot_len < 2) { |
| 72 | + /* This check seems strange on unsigned ip_tot_len, |
| 73 | + * but is needed, else verifier complains: |
| 74 | + * "unbounded min value is not allowed" |
| 75 | + */ |
| 76 | + goto out; |
| 77 | + } |
| 78 | + ip_tot_len &= 0xFFF; /* Max 4095 */ |
| 79 | + |
| 80 | + /* Finding end of packet + offset, and bound access */ |
| 81 | + if ((void *)iphdr + ip_tot_len + offset > data_end) { |
| 82 | + action = XDP_ABORTED; |
| 83 | + goto out; |
| 84 | + } |
| 85 | + |
| 86 | + /* Point ts to end-of-packet, that have been offset extended */ |
| 87 | + ts = (void *)iphdr + ip_tot_len; |
| 88 | + ts->magic = 0x5354; /* String "TS" in network-byte-order */ |
| 89 | + ts->time = bpf_ktime_get_ns(); |
| 90 | + } |
| 91 | +out: |
| 92 | + return xdp_stats_record_action(ctx, action); |
| 93 | +} |
| 94 | + |
| 95 | +SEC("xdp_tailgrow") |
| 96 | +int tailgrow_pass(struct xdp_md *ctx) |
| 97 | +{ |
| 98 | + int offset; |
| 99 | + |
| 100 | + offset = 10; |
| 101 | + bpf_xdp_adjust_tail(ctx, offset); |
| 102 | + return xdp_stats_record_action(ctx, XDP_PASS); |
| 103 | +} |
| 104 | + |
| 105 | +SEC("xdp_pass") |
| 106 | +int xdp_pass_func(struct xdp_md *ctx) |
| 107 | +{ |
| 108 | + return xdp_stats_record_action(ctx, XDP_PASS); |
| 109 | +} |
| 110 | + |
| 111 | +/* For benchmarking tail grow overhead (does a memset)*/ |
| 112 | +SEC("xdp_tailgrow_tx") |
| 113 | +int tailgrow_tx(struct xdp_md *ctx) |
| 114 | +{ |
| 115 | + int offset; |
| 116 | + |
| 117 | + offset = 32; |
| 118 | + bpf_xdp_adjust_tail(ctx, offset); |
| 119 | + return xdp_stats_record_action(ctx, XDP_TX); |
| 120 | +} |
| 121 | + |
| 122 | +/* Baseline benchmark of XDP_TX */ |
| 123 | +SEC("xdp_tx") |
| 124 | +int xdp_tx_rec(struct xdp_md *ctx) |
| 125 | +{ |
| 126 | + return xdp_stats_record_action(ctx, XDP_TX); |
| 127 | +} |
| 128 | + |
| 129 | +char _license[] SEC("license") = "GPL"; |
0 commit comments