Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* XSKMAP used for AF_XDP sockets |
| 3 | * Copyright(c) 2018 Intel Corporation. |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/bpf.h> |
| 7 | #include <linux/capability.h> |
| 8 | #include <net/xdp_sock.h> |
| 9 | #include <linux/slab.h> |
| 10 | #include <linux/sched.h> |
| 11 | |
| 12 | struct xsk_map { |
| 13 | struct bpf_map map; |
| 14 | struct xdp_sock **xsk_map; |
| 15 | struct list_head __percpu *flush_list; |
| 16 | }; |
| 17 | |
| 18 | static struct bpf_map *xsk_map_alloc(union bpf_attr *attr) |
| 19 | { |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 20 | struct xsk_map *m; |
Colin Ian King | 6685699 | 2019-06-04 09:21:46 +0100 | [diff] [blame] | 21 | int cpu, err; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 22 | u64 cost; |
| 23 | |
| 24 | if (!capable(CAP_NET_ADMIN)) |
| 25 | return ERR_PTR(-EPERM); |
| 26 | |
| 27 | if (attr->max_entries == 0 || attr->key_size != 4 || |
| 28 | attr->value_size != 4 || |
| 29 | attr->map_flags & ~(BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)) |
| 30 | return ERR_PTR(-EINVAL); |
| 31 | |
| 32 | m = kzalloc(sizeof(*m), GFP_USER); |
| 33 | if (!m) |
| 34 | return ERR_PTR(-ENOMEM); |
| 35 | |
| 36 | bpf_map_init_from_attr(&m->map, attr); |
| 37 | |
| 38 | cost = (u64)m->map.max_entries * sizeof(struct xdp_sock *); |
| 39 | cost += sizeof(struct list_head) * num_possible_cpus(); |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 40 | |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 41 | /* Notice returns -EPERM on if map size is larger than memlock limit */ |
Roman Gushchin | c85d691 | 2019-05-29 18:03:59 -0700 | [diff] [blame] | 42 | err = bpf_map_charge_init(&m->map.memory, cost); |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 43 | if (err) |
| 44 | goto free_m; |
| 45 | |
Daniel Borkmann | e94fa1d | 2018-05-04 16:27:53 +0200 | [diff] [blame] | 46 | err = -ENOMEM; |
| 47 | |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 48 | m->flush_list = alloc_percpu(struct list_head); |
| 49 | if (!m->flush_list) |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 50 | goto free_charge; |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 51 | |
| 52 | for_each_possible_cpu(cpu) |
| 53 | INIT_LIST_HEAD(per_cpu_ptr(m->flush_list, cpu)); |
| 54 | |
| 55 | m->xsk_map = bpf_map_area_alloc(m->map.max_entries * |
| 56 | sizeof(struct xdp_sock *), |
| 57 | m->map.numa_node); |
| 58 | if (!m->xsk_map) |
| 59 | goto free_percpu; |
| 60 | return &m->map; |
| 61 | |
| 62 | free_percpu: |
| 63 | free_percpu(m->flush_list); |
Roman Gushchin | b936ca6 | 2019-05-29 18:03:58 -0700 | [diff] [blame] | 64 | free_charge: |
| 65 | bpf_map_charge_finish(&m->map.memory); |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 66 | free_m: |
| 67 | kfree(m); |
| 68 | return ERR_PTR(err); |
| 69 | } |
| 70 | |
| 71 | static void xsk_map_free(struct bpf_map *map) |
| 72 | { |
| 73 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
| 74 | int i; |
| 75 | |
Daniel Borkmann | f6069b9 | 2018-08-17 23:26:14 +0200 | [diff] [blame] | 76 | bpf_clear_redirect_map(map); |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 77 | synchronize_net(); |
| 78 | |
| 79 | for (i = 0; i < map->max_entries; i++) { |
| 80 | struct xdp_sock *xs; |
| 81 | |
| 82 | xs = m->xsk_map[i]; |
| 83 | if (!xs) |
| 84 | continue; |
| 85 | |
| 86 | sock_put((struct sock *)xs); |
| 87 | } |
| 88 | |
| 89 | free_percpu(m->flush_list); |
| 90 | bpf_map_area_free(m->xsk_map); |
| 91 | kfree(m); |
| 92 | } |
| 93 | |
| 94 | static int xsk_map_get_next_key(struct bpf_map *map, void *key, void *next_key) |
| 95 | { |
| 96 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
| 97 | u32 index = key ? *(u32 *)key : U32_MAX; |
| 98 | u32 *next = next_key; |
| 99 | |
| 100 | if (index >= m->map.max_entries) { |
| 101 | *next = 0; |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | if (index == m->map.max_entries - 1) |
| 106 | return -ENOENT; |
| 107 | *next = index + 1; |
| 108 | return 0; |
| 109 | } |
| 110 | |
| 111 | struct xdp_sock *__xsk_map_lookup_elem(struct bpf_map *map, u32 key) |
| 112 | { |
| 113 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
| 114 | struct xdp_sock *xs; |
| 115 | |
| 116 | if (key >= map->max_entries) |
| 117 | return NULL; |
| 118 | |
| 119 | xs = READ_ONCE(m->xsk_map[key]); |
| 120 | return xs; |
| 121 | } |
| 122 | |
| 123 | int __xsk_map_redirect(struct bpf_map *map, struct xdp_buff *xdp, |
| 124 | struct xdp_sock *xs) |
| 125 | { |
| 126 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
| 127 | struct list_head *flush_list = this_cpu_ptr(m->flush_list); |
| 128 | int err; |
| 129 | |
| 130 | err = xsk_rcv(xs, xdp); |
| 131 | if (err) |
| 132 | return err; |
| 133 | |
| 134 | if (!xs->flush_node.prev) |
| 135 | list_add(&xs->flush_node, flush_list); |
| 136 | |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | void __xsk_map_flush(struct bpf_map *map) |
| 141 | { |
| 142 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
| 143 | struct list_head *flush_list = this_cpu_ptr(m->flush_list); |
| 144 | struct xdp_sock *xs, *tmp; |
| 145 | |
| 146 | list_for_each_entry_safe(xs, tmp, flush_list, flush_node) { |
| 147 | xsk_flush(xs); |
| 148 | __list_del(xs->flush_node.prev, xs->flush_node.next); |
| 149 | xs->flush_node.prev = NULL; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static void *xsk_map_lookup_elem(struct bpf_map *map, void *key) |
| 154 | { |
Prashant Bhole | 3b4a63f | 2018-10-09 10:04:50 +0900 | [diff] [blame] | 155 | return ERR_PTR(-EOPNOTSUPP); |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static int xsk_map_update_elem(struct bpf_map *map, void *key, void *value, |
| 159 | u64 map_flags) |
| 160 | { |
| 161 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
| 162 | u32 i = *(u32 *)key, fd = *(u32 *)value; |
| 163 | struct xdp_sock *xs, *old_xs; |
| 164 | struct socket *sock; |
| 165 | int err; |
| 166 | |
| 167 | if (unlikely(map_flags > BPF_EXIST)) |
| 168 | return -EINVAL; |
| 169 | if (unlikely(i >= m->map.max_entries)) |
| 170 | return -E2BIG; |
| 171 | if (unlikely(map_flags == BPF_NOEXIST)) |
| 172 | return -EEXIST; |
| 173 | |
| 174 | sock = sockfd_lookup(fd, &err); |
| 175 | if (!sock) |
| 176 | return err; |
| 177 | |
| 178 | if (sock->sk->sk_family != PF_XDP) { |
| 179 | sockfd_put(sock); |
| 180 | return -EOPNOTSUPP; |
| 181 | } |
| 182 | |
| 183 | xs = (struct xdp_sock *)sock->sk; |
| 184 | |
| 185 | if (!xsk_is_setup_for_bpf_map(xs)) { |
| 186 | sockfd_put(sock); |
| 187 | return -EOPNOTSUPP; |
| 188 | } |
| 189 | |
| 190 | sock_hold(sock->sk); |
| 191 | |
| 192 | old_xs = xchg(&m->xsk_map[i], xs); |
Björn Töpel | cee2716 | 2018-10-08 19:40:16 +0200 | [diff] [blame] | 193 | if (old_xs) |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 194 | sock_put((struct sock *)old_xs); |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 195 | |
| 196 | sockfd_put(sock); |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | static int xsk_map_delete_elem(struct bpf_map *map, void *key) |
| 201 | { |
| 202 | struct xsk_map *m = container_of(map, struct xsk_map, map); |
| 203 | struct xdp_sock *old_xs; |
| 204 | int k = *(u32 *)key; |
| 205 | |
| 206 | if (k >= map->max_entries) |
| 207 | return -EINVAL; |
| 208 | |
| 209 | old_xs = xchg(&m->xsk_map[k], NULL); |
Björn Töpel | cee2716 | 2018-10-08 19:40:16 +0200 | [diff] [blame] | 210 | if (old_xs) |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 211 | sock_put((struct sock *)old_xs); |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | const struct bpf_map_ops xsk_map_ops = { |
| 217 | .map_alloc = xsk_map_alloc, |
| 218 | .map_free = xsk_map_free, |
| 219 | .map_get_next_key = xsk_map_get_next_key, |
| 220 | .map_lookup_elem = xsk_map_lookup_elem, |
| 221 | .map_update_elem = xsk_map_update_elem, |
| 222 | .map_delete_elem = xsk_map_delete_elem, |
Daniel Borkmann | e8d2bec | 2018-08-12 01:59:17 +0200 | [diff] [blame] | 223 | .map_check_btf = map_check_no_btf, |
Björn Töpel | fbfc504a | 2018-05-02 13:01:28 +0200 | [diff] [blame] | 224 | }; |