Skip to content

Commit 38e12f9

Browse files
lmbAlexei Starovoitov
authored andcommitted
bpf: sockmap: Merge sockmap and sockhash update functions
Merge the two very similar functions sock_map_update_elem and sock_hash_update_elem into one. Signed-off-by: Lorenz Bauer <lmb@cloudflare.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Acked-by: John Fastabend <john.fastabend@gmail.com> Acked-by: Yonghong Song <yhs@fb.com> Link: https://lore.kernel.org/bpf/20200821102948.21918-3-lmb@cloudflare.com
1 parent 7b219da commit 38e12f9

1 file changed

Lines changed: 7 additions & 42 deletions

File tree

‎net/core/sock_map.c‎

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -559,10 +559,12 @@ static bool sock_map_sk_state_allowed(const struct sock *sk)
559559
return false;
560560
}
561561

562+
static int sock_hash_update_common(struct bpf_map *map, void *key,
563+
struct sock *sk, u64 flags);
564+
562565
static int sock_map_update_elem(struct bpf_map *map, void *key,
563566
void *value, u64 flags)
564567
{
565-
u32 idx = *(u32 *)key;
566568
struct socket *sock;
567569
struct sock *sk;
568570
int ret;
@@ -591,8 +593,10 @@ static int sock_map_update_elem(struct bpf_map *map, void *key,
591593
sock_map_sk_acquire(sk);
592594
if (!sock_map_sk_state_allowed(sk))
593595
ret = -EOPNOTSUPP;
596+
else if (map->map_type == BPF_MAP_TYPE_SOCKMAP)
597+
ret = sock_map_update_common(map, *(u32 *)key, sk, flags);
594598
else
595-
ret = sock_map_update_common(map, idx, sk, flags);
599+
ret = sock_hash_update_common(map, key, sk, flags);
596600
sock_map_sk_release(sk);
597601
out:
598602
fput(sock->file);
@@ -909,45 +913,6 @@ static int sock_hash_update_common(struct bpf_map *map, void *key,
909913
return ret;
910914
}
911915

912-
static int sock_hash_update_elem(struct bpf_map *map, void *key,
913-
void *value, u64 flags)
914-
{
915-
struct socket *sock;
916-
struct sock *sk;
917-
int ret;
918-
u64 ufd;
919-
920-
if (map->value_size == sizeof(u64))
921-
ufd = *(u64 *)value;
922-
else
923-
ufd = *(u32 *)value;
924-
if (ufd > S32_MAX)
925-
return -EINVAL;
926-
927-
sock = sockfd_lookup(ufd, &ret);
928-
if (!sock)
929-
return ret;
930-
sk = sock->sk;
931-
if (!sk) {
932-
ret = -EINVAL;
933-
goto out;
934-
}
935-
if (!sock_map_sk_is_suitable(sk)) {
936-
ret = -EOPNOTSUPP;
937-
goto out;
938-
}
939-
940-
sock_map_sk_acquire(sk);
941-
if (!sock_map_sk_state_allowed(sk))
942-
ret = -EOPNOTSUPP;
943-
else
944-
ret = sock_hash_update_common(map, key, sk, flags);
945-
sock_map_sk_release(sk);
946-
out:
947-
fput(sock->file);
948-
return ret;
949-
}
950-
951916
static int sock_hash_get_next_key(struct bpf_map *map, void *key,
952917
void *key_next)
953918
{
@@ -1216,7 +1181,7 @@ const struct bpf_map_ops sock_hash_ops = {
12161181
.map_alloc = sock_hash_alloc,
12171182
.map_free = sock_hash_free,
12181183
.map_get_next_key = sock_hash_get_next_key,
1219-
.map_update_elem = sock_hash_update_elem,
1184+
.map_update_elem = sock_map_update_elem,
12201185
.map_delete_elem = sock_hash_delete_elem,
12211186
.map_lookup_elem = sock_hash_lookup,
12221187
.map_lookup_elem_sys_only = sock_hash_lookup_sys,

0 commit comments

Comments
 (0)