From: Y7n05h Date: Wed, 8 Feb 2023 07:22:05 +0000 (+0800) Subject: remove duplicate code in xdp X-Git-Tag: dnsdist-1.8.0-rc1~41^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=12a686143b0d62dfd607efad2e6643aec09e96c0;p=thirdparty%2Fpdns.git remove duplicate code in xdp --- diff --git a/contrib/xdp-filter.ebpf.src b/contrib/xdp-filter.ebpf.src index 879baee3a4..3ead6e151b 100644 --- a/contrib/xdp-filter.ebpf.src +++ b/contrib/xdp-filter.ebpf.src @@ -1,118 +1,4 @@ -#include -#include -#include -#include - -#define DNS_PORT 53 - -// do not use libc includes because this causes clang -// to include 32bit headers on 64bit ( only ) systems. -typedef __u8 uint8_t; -typedef __u16 uint16_t; -typedef __u32 uint32_t; -typedef __u64 uint64_t; -#define memcpy __builtin_memcpy - -/* - * Helper pointer to parse the incoming packets - * Copyright 2020, NLnet Labs, All rights reserved. - */ -struct cursor { - void *pos; - void *end; -}; - -/* - * Store the VLAN header - * Copyright 2020, NLnet Labs, All rights reserved. - */ -struct vlanhdr { - uint16_t tci; - uint16_t encap_proto; -}; - -/* - * Store the DNS header - * Copyright 2020, NLnet Labs, All rights reserved. - */ -struct dnshdr { - uint16_t id; - union { - struct { -#if BYTE_ORDER == LITTLE_ENDIAN - uint8_t rd : 1; - uint8_t tc : 1; - uint8_t aa : 1; - uint8_t opcode : 4; - uint8_t qr : 1; - - uint8_t rcode : 4; - uint8_t cd : 1; - uint8_t ad : 1; - uint8_t z : 1; - uint8_t ra : 1; -#elif BYTE_ORDER == BIG_ENDIAN || BYTE_ORDER == PDP_ENDIAN - uint8_t qr : 1; - uint8_t opcode : 4; - uint8_t aa : 1; - uint8_t tc : 1; - uint8_t rd : 1; - - uint8_t ra : 1; - uint8_t z : 1; - uint8_t ad : 1; - uint8_t cd : 1; - uint8_t rcode : 4; -#endif - } as_bits_and_pieces; - uint16_t as_value; - } flags; - uint16_t qdcount; - uint16_t ancount; - uint16_t nscount; - uint16_t arcount; -}; - -/* - * Store the qname and qtype - */ -struct dns_qname -{ - uint8_t qname[255]; - uint16_t qtype; -}; - -/* - * The possible actions to perform on the packet - * PASS: XDP_PASS - * DROP: XDP_DROP - * TC: set TC bit and XDP_TX - */ -enum dns_action : uint8_t { - PASS = 0, - DROP = 1, - TC = 2 -}; - -struct CIDR4 -{ - uint32_t cidr; - uint32_t addr; -}; -struct CIDR6 -{ - uint32_t cidr; - struct in6_addr addr; -}; - -/* - * Store the matching counter and the associated action for a blocked element - */ -struct map_value -{ - uint64_t counter; - enum dns_action action; -}; +#include "xdp.h" BPF_TABLE_PINNED("hash", uint32_t, struct map_value, v4filter, 1024, "/sys/fs/bpf/dnsdist/addr-v4"); BPF_TABLE_PINNED("hash", struct in6_addr, struct map_value, v6filter, 1024, "/sys/fs/bpf/dnsdist/addr-v6"); @@ -131,68 +17,6 @@ BPF_TABLE_PINNED("prog", int, int, progsarray, 2, "/sys/fs/bpf/dnsdist/progs"); BPF_TABLE_PINNED7("lpm_trie", struct CIDR4, struct map_value, cidr4filter, 1024, "/sys/fs/bpf/dnsdist/cidr4", BPF_F_NO_PREALLOC); BPF_TABLE_PINNED7("lpm_trie", struct CIDR6, struct map_value, cidr6filter, 1024, "/sys/fs/bpf/dnsdist/cidr6", BPF_F_NO_PREALLOC); -/* - * Initializer of a cursor pointer - * Copyright 2020, NLnet Labs, All rights reserved. - */ -static inline void cursor_init(struct cursor *c, struct xdp_md *ctx) -{ - c->end = (void *)(long)ctx->data_end; - c->pos = (void *)(long)ctx->data; -} - -/* - * Header parser functions - * Copyright 2020, NLnet Labs, All rights reserved. - */ -#define PARSE_FUNC_DECLARATION(STRUCT) \ -static inline struct STRUCT *parse_ ## STRUCT (struct cursor *c) \ -{ \ - struct STRUCT *ret = c->pos; \ - if (c->pos + sizeof(struct STRUCT) > c->end) \ - return 0; \ - c->pos += sizeof(struct STRUCT); \ - return ret; \ -} - -PARSE_FUNC_DECLARATION(ethhdr) -PARSE_FUNC_DECLARATION(vlanhdr) -PARSE_FUNC_DECLARATION(iphdr) -PARSE_FUNC_DECLARATION(ipv6hdr) -PARSE_FUNC_DECLARATION(udphdr) -PARSE_FUNC_DECLARATION(dnshdr) - -/* - * Parse ethernet frame and fill the struct - * Copyright 2020, NLnet Labs, All rights reserved. - */ -static inline struct ethhdr *parse_eth(struct cursor *c, uint16_t *eth_proto) -{ - struct ethhdr *eth; - - if (!(eth = parse_ethhdr(c))) - return 0; - - *eth_proto = eth->h_proto; - if (*eth_proto == bpf_htons(ETH_P_8021Q) - || *eth_proto == bpf_htons(ETH_P_8021AD)) { - struct vlanhdr *vlan; - - if (!(vlan = parse_vlanhdr(c))) - return 0; - - *eth_proto = vlan->encap_proto; - if (*eth_proto == bpf_htons(ETH_P_8021Q) - || *eth_proto == bpf_htons(ETH_P_8021AD)) { - if (!(vlan = parse_vlanhdr(c))) - return 0; - - *eth_proto = vlan->encap_proto; - } - } - return eth; -} - /* * Recalculate the checksum * Copyright 2020, NLnet Labs, All rights reserved. diff --git a/contrib/xdp-logging-middleware.ebpf.src b/contrib/xdp-logging-middleware.ebpf.src index 8a355a44ee..66c6fd87d8 100644 --- a/contrib/xdp-logging-middleware.ebpf.src +++ b/contrib/xdp-logging-middleware.ebpf.src @@ -1,160 +1,8 @@ -#include -#include -#include -#include - -#define DNS_PORT 53 - -// do not use libc includes because this causes clang -// to include 32bit headers on 64bit ( only ) systems. -typedef __u8 uint8_t; -typedef __u16 uint16_t; -typedef __u32 uint32_t; -typedef __u64 uint64_t; -#define memcpy __builtin_memcpy - -/* - * Helper pointer to parse the incoming packets - * Copyright 2020, NLnet Labs, All rights reserved. - */ -struct cursor { - void* pos; - void* end; -}; - -/* - * Store the VLAN header - * Copyright 2020, NLnet Labs, All rights reserved. - */ -struct vlanhdr { - uint16_t tci; - uint16_t encap_proto; -}; - -/* - * Store the DNS header - * Copyright 2020, NLnet Labs, All rights reserved. - */ -struct dnshdr { - uint16_t id; - union { - struct { -#if BYTE_ORDER == LITTLE_ENDIAN - uint8_t rd : 1; - uint8_t tc : 1; - uint8_t aa : 1; - uint8_t opcode : 4; - uint8_t qr : 1; - - uint8_t rcode : 4; - uint8_t cd : 1; - uint8_t ad : 1; - uint8_t z : 1; - uint8_t ra : 1; -#elif BYTE_ORDER == BIG_ENDIAN || BYTE_ORDER == PDP_ENDIAN - uint8_t qr : 1; - uint8_t opcode : 4; - uint8_t aa : 1; - uint8_t tc : 1; - uint8_t rd : 1; - - uint8_t ra : 1; - uint8_t z : 1; - uint8_t ad : 1; - uint8_t cd : 1; - uint8_t rcode : 4; -#endif - } as_bits_and_pieces; - uint16_t as_value; - } flags; - uint16_t qdcount; - uint16_t ancount; - uint16_t nscount; - uint16_t arcount; -}; - -/* - * Store the qname and qtype - */ -struct dns_qname { - uint8_t qname[255]; - uint16_t qtype; -}; - -/* - * The possible actions to perform on the packet - * PASS: XDP_PASS - * DROP: XDP_DROP - * TC: set TC bit and XDP_TX - */ -enum dns_action { - PASS = 0, - DROP = 1, - TC = 2 -}; +#include "xdp.h" BPF_TABLE_PINNED("prog", int, int, progsarray, 2, "/sys/fs/bpf/dnsdist/progs"); BPF_PERF_OUTPUT(events); -/* - * Initializer of a cursor pointer - * Copyright 2020, NLnet Labs, All rights reserved. - */ -static inline void cursor_init(struct cursor* c, struct xdp_md* ctx) { - c->end = (void*)(long)ctx->data_end; - c->pos = (void*)(long)ctx->data; -} - -/* - * Header parser functions - * Copyright 2020, NLnet Labs, All rights reserved. - */ -#define PARSE_FUNC_DECLARATION(STRUCT) \ - static inline struct STRUCT* parse_##STRUCT(struct cursor* c) { \ - struct STRUCT* ret = c->pos; \ - if (c->pos + sizeof(struct STRUCT) > c->end) \ - return 0; \ - c->pos += sizeof(struct STRUCT); \ - return ret; \ - } - -PARSE_FUNC_DECLARATION(ethhdr) -PARSE_FUNC_DECLARATION(vlanhdr) -PARSE_FUNC_DECLARATION(iphdr) -PARSE_FUNC_DECLARATION(ipv6hdr) -PARSE_FUNC_DECLARATION(udphdr) -PARSE_FUNC_DECLARATION(dnshdr) - -/* - * Parse ethernet frame and fill the struct - * Copyright 2020, NLnet Labs, All rights reserved. - */ -static inline struct ethhdr* parse_eth(struct cursor* c, uint16_t* eth_proto) { - struct ethhdr* eth; - - if (!(eth = parse_ethhdr(c))) - return 0; - - *eth_proto = eth->h_proto; - if (*eth_proto == bpf_htons(ETH_P_8021Q) - || *eth_proto == bpf_htons(ETH_P_8021AD)) { - struct vlanhdr* vlan; - - if (!(vlan = parse_vlanhdr(c))) - return 0; - - *eth_proto = vlan->encap_proto; - if (*eth_proto == bpf_htons(ETH_P_8021Q) - || *eth_proto == bpf_htons(ETH_P_8021AD)) { - if (!(vlan = parse_vlanhdr(c))) - return 0; - - *eth_proto = vlan->encap_proto; - } - } - return eth; -} - /* * Parse DNS QName and fill the struct */ diff --git a/contrib/xdp.h b/contrib/xdp.h new file mode 100644 index 0000000000..87fef3a776 --- /dev/null +++ b/contrib/xdp.h @@ -0,0 +1,183 @@ +#ifndef __XDP_H__ +#define __XDP_H__ + +#include +#include +#include +#include + +#define DNS_PORT 53 + +// do not use libc includes because this causes clang +// to include 32bit headers on 64bit ( only ) systems. +typedef __u8 uint8_t; +typedef __u16 uint16_t; +typedef __u32 uint32_t; +typedef __u64 uint64_t; +#define memcpy __builtin_memcpy + +/* + * Helper pointer to parse the incoming packets + * Copyright 2020, NLnet Labs, All rights reserved. + */ +struct cursor { + void *pos; + void *end; +}; + +/* + * Store the VLAN header + * Copyright 2020, NLnet Labs, All rights reserved. + */ +struct vlanhdr { + uint16_t tci; + uint16_t encap_proto; +}; + +/* + * Store the DNS header + * Copyright 2020, NLnet Labs, All rights reserved. + */ +struct dnshdr { + uint16_t id; + union { + struct { +#if BYTE_ORDER == LITTLE_ENDIAN + uint8_t rd : 1; + uint8_t tc : 1; + uint8_t aa : 1; + uint8_t opcode : 4; + uint8_t qr : 1; + + uint8_t rcode : 4; + uint8_t cd : 1; + uint8_t ad : 1; + uint8_t z : 1; + uint8_t ra : 1; +#elif BYTE_ORDER == BIG_ENDIAN || BYTE_ORDER == PDP_ENDIAN + uint8_t qr : 1; + uint8_t opcode : 4; + uint8_t aa : 1; + uint8_t tc : 1; + uint8_t rd : 1; + + uint8_t ra : 1; + uint8_t z : 1; + uint8_t ad : 1; + uint8_t cd : 1; + uint8_t rcode : 4; +#endif + } as_bits_and_pieces; + uint16_t as_value; + } flags; + uint16_t qdcount; + uint16_t ancount; + uint16_t nscount; + uint16_t arcount; +}; + +/* + * Store the qname and qtype + */ +struct dns_qname +{ + uint8_t qname[255]; + uint16_t qtype; +}; + +/* + * The possible actions to perform on the packet + * PASS: XDP_PASS + * DROP: XDP_DROP + * TC: set TC bit and XDP_TX + */ +enum dns_action : uint8_t { + PASS = 0, + DROP = 1, + TC = 2 +}; + +struct CIDR4 +{ + uint32_t cidr; + uint32_t addr; +}; +struct CIDR6 +{ + uint32_t cidr; + struct in6_addr addr; +}; + +/* + * Store the matching counter and the associated action for a blocked element + */ +struct map_value +{ + uint64_t counter; + enum dns_action action; +}; + + +/* + * Initializer of a cursor pointer + * Copyright 2020, NLnet Labs, All rights reserved. + */ +static inline void cursor_init(struct cursor *c, struct xdp_md *ctx) +{ + c->end = (void *)(long)ctx->data_end; + c->pos = (void *)(long)ctx->data; +} + +/* + * Header parser functions + * Copyright 2020, NLnet Labs, All rights reserved. + */ +#define PARSE_FUNC_DECLARATION(STRUCT) \ +static inline struct STRUCT *parse_ ## STRUCT (struct cursor *c) \ +{ \ + struct STRUCT *ret = c->pos; \ + if (c->pos + sizeof(struct STRUCT) > c->end) \ + return 0; \ + c->pos += sizeof(struct STRUCT); \ + return ret; \ +} + +PARSE_FUNC_DECLARATION(ethhdr) +PARSE_FUNC_DECLARATION(vlanhdr) +PARSE_FUNC_DECLARATION(iphdr) +PARSE_FUNC_DECLARATION(ipv6hdr) +PARSE_FUNC_DECLARATION(udphdr) +PARSE_FUNC_DECLARATION(dnshdr) + +/* + * Parse ethernet frame and fill the struct + * Copyright 2020, NLnet Labs, All rights reserved. + */ +static inline struct ethhdr *parse_eth(struct cursor *c, uint16_t *eth_proto) +{ + struct ethhdr *eth; + + if (!(eth = parse_ethhdr(c))) + return 0; + + *eth_proto = eth->h_proto; + if (*eth_proto == bpf_htons(ETH_P_8021Q) + || *eth_proto == bpf_htons(ETH_P_8021AD)) { + struct vlanhdr *vlan; + + if (!(vlan = parse_vlanhdr(c))) + return 0; + + *eth_proto = vlan->encap_proto; + if (*eth_proto == bpf_htons(ETH_P_8021Q) + || *eth_proto == bpf_htons(ETH_P_8021AD)) { + if (!(vlan = parse_vlanhdr(c))) + return 0; + + *eth_proto = vlan->encap_proto; + } + } + return eth; +} + +#endif