github.com/cilium/cilium@v1.16.2/bpf/tests/xdp_nodeport_lb4_dsr_lb.c (about) 1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) 2 /* Copyright Authors of Cilium */ 3 4 #include "common.h" 5 6 #include <bpf/ctx/xdp.h> 7 #include "pktgen.h" 8 9 /* Set ETH_HLEN to 14 to indicate that the packet has a 14 byte ethernet header */ 10 #define ETH_HLEN 14 11 12 /* Enable code paths under test */ 13 #define ENABLE_IPV4 14 #define ENABLE_NODEPORT 15 #define ENABLE_NODEPORT_ACCELERATION 16 #define ENABLE_DSR 17 18 #define DISABLE_LOOPBACK_LB 19 20 /* Skip ingress policy checks, not needed to validate hairpin flow */ 21 #define USE_BPF_PROG_FOR_INGRESS_POLICY 22 #undef FORCE_LOCAL_POLICY_EVAL_AT_SOURCE 23 24 #define CLIENT_IP v4_ext_one 25 #define CLIENT_PORT __bpf_htons(111) 26 27 #define FRONTEND_IP v4_svc_two 28 #define FRONTEND_PORT tcp_svc_one 29 30 #define LB_IP v4_node_one 31 #define IPV4_DIRECT_ROUTING LB_IP 32 33 #define BACKEND_IP v4_pod_two 34 #define BACKEND_PORT __bpf_htons(8080) 35 36 #define fib_lookup mock_fib_lookup 37 38 static volatile const __u8 *client_mac = mac_one; 39 /* this matches the default node_config.h: */ 40 static volatile const __u8 lb_mac[ETH_ALEN] = { 0xce, 0x72, 0xa7, 0x03, 0x88, 0x56 }; 41 static volatile const __u8 *remote_backend_mac = mac_five; 42 43 long mock_fib_lookup(__maybe_unused void *ctx, struct bpf_fib_lookup *params, 44 __maybe_unused int plen, __maybe_unused __u32 flags) 45 { 46 params->ifindex = 0; 47 48 if (params->ipv4_dst == BACKEND_IP) { 49 __bpf_memcpy_builtin(params->smac, (__u8 *)lb_mac, ETH_ALEN); 50 __bpf_memcpy_builtin(params->dmac, (__u8 *)remote_backend_mac, ETH_ALEN); 51 } else { 52 __bpf_memcpy_builtin(params->smac, (__u8 *)lb_mac, ETH_ALEN); 53 __bpf_memcpy_builtin(params->dmac, (__u8 *)client_mac, ETH_ALEN); 54 } 55 56 return 0; 57 } 58 59 #include <bpf_xdp.c> 60 61 #include "lib/ipcache.h" 62 #include "lib/lb.h" 63 64 #define FROM_NETDEV 0 65 66 struct { 67 __uint(type, BPF_MAP_TYPE_PROG_ARRAY); 68 __uint(key_size, sizeof(__u32)); 69 __uint(max_entries, 1); 70 __array(values, int()); 71 } entry_call_map __section(".maps") = { 72 .values = { 73 [FROM_NETDEV] = &cil_xdp_entry, 74 }, 75 }; 76 77 /* Test that a SVC request that is LBed to a DSR remote backend 78 * - gets DNATed, 79 * - has IP Option inserted, 80 * - gets redirected back out by XDP 81 */ 82 PKTGEN("xdp", "xdp_nodeport_dsr_fwd") 83 int nodeport_dsr_fwd_pktgen(struct __ctx_buff *ctx) 84 { 85 struct pktgen builder; 86 struct tcphdr *l4; 87 void *data; 88 89 /* Init packet builder */ 90 pktgen__init(&builder, ctx); 91 92 l4 = pktgen__push_ipv4_tcp_packet(&builder, 93 (__u8 *)client_mac, (__u8 *)lb_mac, 94 CLIENT_IP, FRONTEND_IP, 95 CLIENT_PORT, FRONTEND_PORT); 96 if (!l4) 97 return TEST_ERROR; 98 99 data = pktgen__push_data(&builder, default_data, sizeof(default_data)); 100 if (!data) 101 return TEST_ERROR; 102 103 /* Calc lengths, set protocol fields and calc checksums */ 104 pktgen__finish(&builder); 105 106 return 0; 107 } 108 109 SETUP("xdp", "xdp_nodeport_dsr_fwd") 110 int nodeport_dsr_fwd_setup(struct __ctx_buff *ctx) 111 { 112 __u16 revnat_id = 1; 113 114 lb_v4_add_service(FRONTEND_IP, FRONTEND_PORT, 1, revnat_id); 115 lb_v4_add_backend(FRONTEND_IP, FRONTEND_PORT, 1, 124, 116 BACKEND_IP, BACKEND_PORT, IPPROTO_TCP, 0); 117 118 ipcache_v4_add_entry(BACKEND_IP, 0, 112233, 0, 0); 119 120 /* Jump into the entrypoint */ 121 tail_call_static(ctx, entry_call_map, FROM_NETDEV); 122 /* Fail if we didn't jump */ 123 return TEST_ERROR; 124 } 125 126 CHECK("xdp", "xdp_nodeport_dsr_fwd") 127 int nodeport_dsr_fwd_check(__maybe_unused const struct __ctx_buff *ctx) 128 { 129 struct dsr_opt_v4 *opt; 130 void *data, *data_end; 131 __u32 *status_code; 132 struct tcphdr *l4; 133 struct ethhdr *l2; 134 struct iphdr *l3; 135 136 test_init(); 137 138 data = (void *)(long)ctx_data(ctx); 139 data_end = (void *)(long)ctx->data_end; 140 141 if (data + sizeof(__u32) > data_end) 142 test_fatal("status code out of bounds"); 143 144 status_code = data; 145 146 assert(fib_ok(*status_code)); 147 148 l2 = data + sizeof(__u32); 149 if ((void *)l2 + sizeof(struct ethhdr) > data_end) 150 test_fatal("l2 out of bounds"); 151 152 l3 = (void *)l2 + sizeof(struct ethhdr); 153 if ((void *)l3 + sizeof(struct iphdr) > data_end) 154 test_fatal("l3 out of bounds"); 155 156 opt = (void *)l3 + sizeof(struct iphdr); 157 if ((void *)opt + 2 * sizeof(__u32) > data_end) 158 test_fatal("l3 DSR option out of bounds"); 159 160 l4 = (void *)opt + sizeof(*opt); 161 if ((void *)l4 + sizeof(struct tcphdr) > data_end) 162 test_fatal("l4 out of bounds"); 163 164 if (memcmp(l2->h_source, (__u8 *)lb_mac, ETH_ALEN) != 0) 165 test_fatal("src MAC is not the LB MAC") 166 if (memcmp(l2->h_dest, (__u8 *)remote_backend_mac, ETH_ALEN) != 0) 167 test_fatal("dst MAC is not the backend MAC") 168 169 if (l3->saddr != CLIENT_IP) 170 test_fatal("src IP has changed"); 171 172 if (l3->daddr != BACKEND_IP) 173 test_fatal("dst IP hasn't been NATed to remote backend IP"); 174 175 if (l3->check != bpf_htons(0x434a)) 176 test_fatal("L3 checksum is invalid: %d", bpf_htons(l3->check)); 177 178 if (opt->type != DSR_IPV4_OPT_TYPE) 179 test_fatal("type in DSR IP option is bad") 180 if (opt->len != 8) 181 test_fatal("length in DSR IP option is bad") 182 if (opt->port != __bpf_ntohs(FRONTEND_PORT)) 183 test_fatal("port in DSR IP option is bad") 184 if (opt->addr != __bpf_ntohl(FRONTEND_IP)) 185 test_fatal("addr in DSR IP option is bad") 186 187 if (l4->source != CLIENT_PORT) 188 test_fatal("src port has changed"); 189 190 if (l4->dest != BACKEND_PORT) 191 test_fatal("dst port hasn't been NATed to backend port"); 192 193 test_finish(); 194 }