Craig Gallek | ef45614 | 2016-01-04 17:41:45 -0500 | [diff] [blame] | 1 | /* |
| 2 | * To speed up listener socket lookup, create an array to store all sockets |
| 3 | * listening on the same port. This allows a decision to be made after finding |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 4 | * the first socket. An optional BPF program can also be configured for |
| 5 | * selecting the socket index from the array of available sockets. |
Craig Gallek | ef45614 | 2016-01-04 17:41:45 -0500 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <net/sock_reuseport.h> |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 9 | #include <linux/bpf.h> |
Craig Gallek | ef45614 | 2016-01-04 17:41:45 -0500 | [diff] [blame] | 10 | #include <linux/rcupdate.h> |
| 11 | |
| 12 | #define INIT_SOCKS 128 |
| 13 | |
| 14 | static DEFINE_SPINLOCK(reuseport_lock); |
| 15 | |
| 16 | static struct sock_reuseport *__reuseport_alloc(u16 max_socks) |
| 17 | { |
| 18 | size_t size = sizeof(struct sock_reuseport) + |
| 19 | sizeof(struct sock *) * max_socks; |
| 20 | struct sock_reuseport *reuse = kzalloc(size, GFP_ATOMIC); |
| 21 | |
| 22 | if (!reuse) |
| 23 | return NULL; |
| 24 | |
| 25 | reuse->max_socks = max_socks; |
| 26 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 27 | RCU_INIT_POINTER(reuse->prog, NULL); |
Craig Gallek | ef45614 | 2016-01-04 17:41:45 -0500 | [diff] [blame] | 28 | return reuse; |
| 29 | } |
| 30 | |
| 31 | int reuseport_alloc(struct sock *sk) |
| 32 | { |
| 33 | struct sock_reuseport *reuse; |
| 34 | |
| 35 | /* bh lock used since this function call may precede hlist lock in |
| 36 | * soft irq of receive path or setsockopt from process context |
| 37 | */ |
| 38 | spin_lock_bh(&reuseport_lock); |
| 39 | WARN_ONCE(rcu_dereference_protected(sk->sk_reuseport_cb, |
| 40 | lockdep_is_held(&reuseport_lock)), |
| 41 | "multiple allocations for the same socket"); |
| 42 | reuse = __reuseport_alloc(INIT_SOCKS); |
| 43 | if (!reuse) { |
| 44 | spin_unlock_bh(&reuseport_lock); |
| 45 | return -ENOMEM; |
| 46 | } |
| 47 | |
| 48 | reuse->socks[0] = sk; |
| 49 | reuse->num_socks = 1; |
| 50 | rcu_assign_pointer(sk->sk_reuseport_cb, reuse); |
| 51 | |
| 52 | spin_unlock_bh(&reuseport_lock); |
| 53 | |
| 54 | return 0; |
| 55 | } |
| 56 | EXPORT_SYMBOL(reuseport_alloc); |
| 57 | |
| 58 | static struct sock_reuseport *reuseport_grow(struct sock_reuseport *reuse) |
| 59 | { |
| 60 | struct sock_reuseport *more_reuse; |
| 61 | u32 more_socks_size, i; |
| 62 | |
| 63 | more_socks_size = reuse->max_socks * 2U; |
| 64 | if (more_socks_size > U16_MAX) |
| 65 | return NULL; |
| 66 | |
| 67 | more_reuse = __reuseport_alloc(more_socks_size); |
| 68 | if (!more_reuse) |
| 69 | return NULL; |
| 70 | |
| 71 | more_reuse->max_socks = more_socks_size; |
| 72 | more_reuse->num_socks = reuse->num_socks; |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 73 | more_reuse->prog = reuse->prog; |
Craig Gallek | ef45614 | 2016-01-04 17:41:45 -0500 | [diff] [blame] | 74 | |
| 75 | memcpy(more_reuse->socks, reuse->socks, |
| 76 | reuse->num_socks * sizeof(struct sock *)); |
| 77 | |
| 78 | for (i = 0; i < reuse->num_socks; ++i) |
| 79 | rcu_assign_pointer(reuse->socks[i]->sk_reuseport_cb, |
| 80 | more_reuse); |
| 81 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 82 | /* Note: we use kfree_rcu here instead of reuseport_free_rcu so |
| 83 | * that reuse and more_reuse can temporarily share a reference |
| 84 | * to prog. |
| 85 | */ |
Craig Gallek | ef45614 | 2016-01-04 17:41:45 -0500 | [diff] [blame] | 86 | kfree_rcu(reuse, rcu); |
| 87 | return more_reuse; |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * reuseport_add_sock - Add a socket to the reuseport group of another. |
| 92 | * @sk: New socket to add to the group. |
| 93 | * @sk2: Socket belonging to the existing reuseport group. |
| 94 | * May return ENOMEM and not add socket to group under memory pressure. |
| 95 | */ |
| 96 | int reuseport_add_sock(struct sock *sk, const struct sock *sk2) |
| 97 | { |
| 98 | struct sock_reuseport *reuse; |
| 99 | |
| 100 | spin_lock_bh(&reuseport_lock); |
| 101 | reuse = rcu_dereference_protected(sk2->sk_reuseport_cb, |
| 102 | lockdep_is_held(&reuseport_lock)), |
| 103 | WARN_ONCE(rcu_dereference_protected(sk->sk_reuseport_cb, |
| 104 | lockdep_is_held(&reuseport_lock)), |
| 105 | "socket already in reuseport group"); |
| 106 | |
| 107 | if (reuse->num_socks == reuse->max_socks) { |
| 108 | reuse = reuseport_grow(reuse); |
| 109 | if (!reuse) { |
| 110 | spin_unlock_bh(&reuseport_lock); |
| 111 | return -ENOMEM; |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | reuse->socks[reuse->num_socks] = sk; |
| 116 | /* paired with smp_rmb() in reuseport_select_sock() */ |
| 117 | smp_wmb(); |
| 118 | reuse->num_socks++; |
| 119 | rcu_assign_pointer(sk->sk_reuseport_cb, reuse); |
| 120 | |
| 121 | spin_unlock_bh(&reuseport_lock); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | EXPORT_SYMBOL(reuseport_add_sock); |
| 126 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 127 | static void reuseport_free_rcu(struct rcu_head *head) |
| 128 | { |
| 129 | struct sock_reuseport *reuse; |
| 130 | |
| 131 | reuse = container_of(head, struct sock_reuseport, rcu); |
| 132 | if (reuse->prog) |
| 133 | bpf_prog_destroy(reuse->prog); |
| 134 | kfree(reuse); |
| 135 | } |
| 136 | |
Craig Gallek | ef45614 | 2016-01-04 17:41:45 -0500 | [diff] [blame] | 137 | void reuseport_detach_sock(struct sock *sk) |
| 138 | { |
| 139 | struct sock_reuseport *reuse; |
| 140 | int i; |
| 141 | |
| 142 | spin_lock_bh(&reuseport_lock); |
| 143 | reuse = rcu_dereference_protected(sk->sk_reuseport_cb, |
| 144 | lockdep_is_held(&reuseport_lock)); |
| 145 | rcu_assign_pointer(sk->sk_reuseport_cb, NULL); |
| 146 | |
| 147 | for (i = 0; i < reuse->num_socks; i++) { |
| 148 | if (reuse->socks[i] == sk) { |
| 149 | reuse->socks[i] = reuse->socks[reuse->num_socks - 1]; |
| 150 | reuse->num_socks--; |
| 151 | if (reuse->num_socks == 0) |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 152 | call_rcu(&reuse->rcu, reuseport_free_rcu); |
Craig Gallek | ef45614 | 2016-01-04 17:41:45 -0500 | [diff] [blame] | 153 | break; |
| 154 | } |
| 155 | } |
| 156 | spin_unlock_bh(&reuseport_lock); |
| 157 | } |
| 158 | EXPORT_SYMBOL(reuseport_detach_sock); |
| 159 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 160 | static struct sock *run_bpf(struct sock_reuseport *reuse, u16 socks, |
| 161 | struct bpf_prog *prog, struct sk_buff *skb, |
| 162 | int hdr_len) |
| 163 | { |
| 164 | struct sk_buff *nskb = NULL; |
| 165 | u32 index; |
| 166 | |
| 167 | if (skb_shared(skb)) { |
| 168 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 169 | if (!nskb) |
| 170 | return NULL; |
| 171 | skb = nskb; |
| 172 | } |
| 173 | |
| 174 | /* temporarily advance data past protocol header */ |
| 175 | if (!pskb_pull(skb, hdr_len)) { |
Craig Gallek | 00ce3a1 | 2016-01-05 10:57:13 -0500 | [diff] [blame] | 176 | kfree_skb(nskb); |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 177 | return NULL; |
| 178 | } |
| 179 | index = bpf_prog_run_save_cb(prog, skb); |
| 180 | __skb_push(skb, hdr_len); |
| 181 | |
| 182 | consume_skb(nskb); |
| 183 | |
| 184 | if (index >= socks) |
| 185 | return NULL; |
| 186 | |
| 187 | return reuse->socks[index]; |
| 188 | } |
| 189 | |
Craig Gallek | ef45614 | 2016-01-04 17:41:45 -0500 | [diff] [blame] | 190 | /** |
| 191 | * reuseport_select_sock - Select a socket from an SO_REUSEPORT group. |
| 192 | * @sk: First socket in the group. |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 193 | * @hash: When no BPF filter is available, use this hash to select. |
| 194 | * @skb: skb to run through BPF filter. |
| 195 | * @hdr_len: BPF filter expects skb data pointer at payload data. If |
| 196 | * the skb does not yet point at the payload, this parameter represents |
| 197 | * how far the pointer needs to advance to reach the payload. |
Craig Gallek | ef45614 | 2016-01-04 17:41:45 -0500 | [diff] [blame] | 198 | * Returns a socket that should receive the packet (or NULL on error). |
| 199 | */ |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 200 | struct sock *reuseport_select_sock(struct sock *sk, |
| 201 | u32 hash, |
| 202 | struct sk_buff *skb, |
| 203 | int hdr_len) |
Craig Gallek | ef45614 | 2016-01-04 17:41:45 -0500 | [diff] [blame] | 204 | { |
| 205 | struct sock_reuseport *reuse; |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 206 | struct bpf_prog *prog; |
Craig Gallek | ef45614 | 2016-01-04 17:41:45 -0500 | [diff] [blame] | 207 | struct sock *sk2 = NULL; |
| 208 | u16 socks; |
| 209 | |
| 210 | rcu_read_lock(); |
| 211 | reuse = rcu_dereference(sk->sk_reuseport_cb); |
| 212 | |
| 213 | /* if memory allocation failed or add call is not yet complete */ |
| 214 | if (!reuse) |
| 215 | goto out; |
| 216 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 217 | prog = rcu_dereference(reuse->prog); |
Craig Gallek | ef45614 | 2016-01-04 17:41:45 -0500 | [diff] [blame] | 218 | socks = READ_ONCE(reuse->num_socks); |
| 219 | if (likely(socks)) { |
| 220 | /* paired with smp_wmb() in reuseport_add_sock() */ |
| 221 | smp_rmb(); |
| 222 | |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 223 | if (prog && skb) |
| 224 | sk2 = run_bpf(reuse, socks, prog, skb, hdr_len); |
| 225 | else |
| 226 | sk2 = reuse->socks[reciprocal_scale(hash, socks)]; |
Craig Gallek | ef45614 | 2016-01-04 17:41:45 -0500 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | out: |
| 230 | rcu_read_unlock(); |
| 231 | return sk2; |
| 232 | } |
| 233 | EXPORT_SYMBOL(reuseport_select_sock); |
Craig Gallek | 538950a | 2016-01-04 17:41:47 -0500 | [diff] [blame] | 234 | |
| 235 | struct bpf_prog * |
| 236 | reuseport_attach_prog(struct sock *sk, struct bpf_prog *prog) |
| 237 | { |
| 238 | struct sock_reuseport *reuse; |
| 239 | struct bpf_prog *old_prog; |
| 240 | |
| 241 | spin_lock_bh(&reuseport_lock); |
| 242 | reuse = rcu_dereference_protected(sk->sk_reuseport_cb, |
| 243 | lockdep_is_held(&reuseport_lock)); |
| 244 | old_prog = rcu_dereference_protected(reuse->prog, |
| 245 | lockdep_is_held(&reuseport_lock)); |
| 246 | rcu_assign_pointer(reuse->prog, prog); |
| 247 | spin_unlock_bh(&reuseport_lock); |
| 248 | |
| 249 | return old_prog; |
| 250 | } |
| 251 | EXPORT_SYMBOL(reuseport_attach_prog); |