1 // SPDX-License-Identifier: GPL-2.0
3 * Check if we can migrate child sockets.
5 * 1. If reuse_md->migrating_sk is NULL (SYN packet),
6 * return SK_PASS without selecting a listener.
7 * 2. If reuse_md->migrating_sk is not NULL (socket migration),
8 * select a listener (reuseport_map[migrate_map[cookie]])
10 * Author: Kuniyuki Iwashima <kuniyu@amazon.co.jp>
15 #include <linux/bpf.h>
16 #include <linux/if_ether.h>
18 #include <linux/ipv6.h>
19 #include <linux/tcp.h>
21 #include <bpf/bpf_endian.h>
22 #include <bpf/bpf_helpers.h>
25 __uint(type, BPF_MAP_TYPE_REUSEPORT_SOCKARRAY);
26 __uint(max_entries, 256);
29 } reuseport_map SEC(".maps");
32 __uint(type, BPF_MAP_TYPE_HASH);
33 __uint(max_entries, 256);
36 } migrate_map SEC(".maps");
38 int migrated_at_close = 0;
39 int migrated_at_close_fastopen = 0;
40 int migrated_at_send_synack = 0;
41 int migrated_at_recv_ack = 0;
45 int drop_ack(struct xdp_md *xdp)
47 void *data_end = (void *)(long)xdp->data_end;
48 void *data = (void *)(long)xdp->data;
49 struct ethhdr *eth = data;
50 struct tcphdr *tcp = NULL;
52 if (eth + 1 > data_end)
55 switch (bpf_ntohs(eth->h_proto)) {
57 struct iphdr *ip = (struct iphdr *)(eth + 1);
59 if (ip + 1 > data_end)
62 if (ip->protocol != IPPROTO_TCP)
65 tcp = (struct tcphdr *)((void *)ip + ip->ihl * 4);
69 struct ipv6hdr *ipv6 = (struct ipv6hdr *)(eth + 1);
71 if (ipv6 + 1 > data_end)
74 if (ipv6->nexthdr != IPPROTO_TCP)
77 tcp = (struct tcphdr *)(ipv6 + 1);
84 if (tcp + 1 > data_end)
87 if (tcp->dest != server_port)
90 if (!tcp->syn && tcp->ack)
97 SEC("sk_reuseport/migrate")
98 int migrate_reuseport(struct sk_reuseport_md *reuse_md)
100 int *key, flags = 0, state, err;
103 if (!reuse_md->migrating_sk)
106 state = reuse_md->migrating_sk->state;
107 cookie = bpf_get_socket_cookie(reuse_md->sk);
109 key = bpf_map_lookup_elem(&migrate_map, &cookie);
113 err = bpf_sk_select_reuseport(reuse_md, &reuseport_map, key, flags);
118 case BPF_TCP_ESTABLISHED:
119 __sync_fetch_and_add(&migrated_at_close, 1);
121 case BPF_TCP_SYN_RECV:
122 __sync_fetch_and_add(&migrated_at_close_fastopen, 1);
124 case BPF_TCP_NEW_SYN_RECV:
126 __sync_fetch_and_add(&migrated_at_send_synack, 1);
128 __sync_fetch_and_add(&migrated_at_recv_ack, 1);
135 char _license[] SEC("license") = "GPL";