The core port allocator is currently private to connection-oriented sockets. Virtio datagrams need the core bound table and port allocation, while VMCI datagrams must keep their own endpoint creation and port namespace.
Export vsock_bind_common() with the bound-table locking inside the helper, and use it for stream and SEQPACKET binds. Keep datagram binds delegated to the transport so a transport can opt into the core allocator without changing other transports' binding behavior. Keep datagram and connection-oriented sockets in separate logical port spaces within the core bound table. Filter bind conflicts and bound-socket lookups by class, including wildcard CIDs and automatic port allocation, so datagram and stream/SEQPACKET sockets can share a CID and port. Stream and SEQPACKET sockets continue to conflict with each other. Signed-off-by: Sergio Lopez <[email protected]> --- include/net/af_vsock.h | 3 ++ net/vmw_vsock/af_vsock.c | 98 +++++++++++++++++++++++++++++++----------------- 2 files changed, 66 insertions(+), 35 deletions(-) diff --git a/include/net/af_vsock.h b/include/net/af_vsock.h index e8481f12312e..ac061115ed30 100644 --- a/include/net/af_vsock.h +++ b/include/net/af_vsock.h @@ -220,6 +220,7 @@ void vsock_remove_pending(struct sock *listener, struct sock *pending); void vsock_enqueue_accept(struct sock *listener, struct sock *connected); void vsock_pending_to_accept(struct sock *listener, struct sock *pending); void vsock_insert_connected(struct vsock_sock *vsk); +int vsock_bind_common(struct vsock_sock *vsk, struct sockaddr_vm *addr); void vsock_remove_bound(struct vsock_sock *vsk); void vsock_remove_connected(struct vsock_sock *vsk); struct sock *vsock_find_bound_socket(struct sockaddr_vm *addr); @@ -227,6 +228,8 @@ struct sock *vsock_find_connected_socket(struct sockaddr_vm *src, struct sockaddr_vm *dst); struct sock *vsock_find_bound_socket_net(struct sockaddr_vm *addr, struct net *net); +struct sock *vsock_find_bound_dgram_socket_net(struct sockaddr_vm *addr, + struct net *net); struct sock *vsock_find_connected_socket_net(struct sockaddr_vm *src, struct sockaddr_vm *dst, struct net *net); diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c index d0508887a2fb..d5cf240d4ece 100644 --- a/net/vmw_vsock/af_vsock.c +++ b/net/vmw_vsock/af_vsock.c @@ -14,14 +14,10 @@ * specified an address that they are responsible for) and one for connected * sockets (sockets that have established a connection with another socket). * These tables are "global" in that all sockets on the system are placed - * within them. - Note, though, that the bound table contains an extra entry - * for a list of unbound sockets and SOCK_DGRAM sockets will always remain in - * that list. The bound table is used solely for lookup of sockets when packets - * are received and that's not necessary for SOCK_DGRAM sockets since we create - * a datagram handle for each and need not perform a lookup. Keeping SOCK_DGRAM - * sockets out of the bound hash buckets will reduce the chance of collisions - * when looking for SOCK_STREAM sockets and prevents us from having to check the - * socket type in the hash table lookups. + * within them. The bound table has an extra entry for unbound sockets. + * Transports with their own datagram endpoint table can leave their datagram + * sockets there; virtio datagrams use the bound hash buckets. Datagram and + * connection-oriented sockets have separate logical port spaces within them. * * - Sockets created by user action will either be "client" sockets that * initiate a connection or "server" sockets that listen for connections; we do @@ -295,12 +291,16 @@ static void __vsock_remove_connected(struct vsock_sock *vsk) } static struct sock *__vsock_find_bound_socket_net(struct sockaddr_vm *addr, - struct net *net) + struct net *net, bool dgram) { struct vsock_sock *vsk; list_for_each_entry(vsk, vsock_bound_sockets(addr), bound_table) { struct sock *sk = sk_vsock(vsk); + bool socket_dgram = sk->sk_type == SOCK_DGRAM; + + if (socket_dgram != dgram) + continue; if (vsock_addr_equals_addr(addr, &vsk->local_addr) && vsock_net_check_mode(sock_net(sk), net)) @@ -372,28 +372,41 @@ void vsock_remove_connected(struct vsock_sock *vsk) } EXPORT_SYMBOL_GPL(vsock_remove_connected); -/* Find a bound socket, filtering by namespace and namespace mode. - * - * Use this in transports that are namespace-aware and can provide the - * network namespace context. - */ -struct sock *vsock_find_bound_socket_net(struct sockaddr_vm *addr, - struct net *net) +static struct sock *vsock_find_bound_socket_type_net(struct sockaddr_vm *addr, + struct net *net, bool dgram) { struct sock *sk; spin_lock_bh(&vsock_table_lock); - sk = __vsock_find_bound_socket_net(addr, net); + sk = __vsock_find_bound_socket_net(addr, net, dgram); if (sk) sock_hold(sk); - spin_unlock_bh(&vsock_table_lock); return sk; } + +/* Find a connection-oriented bound socket, filtering by namespace and mode. + * + * Use this in transports that are namespace-aware and can provide the + * network namespace context. + */ +struct sock *vsock_find_bound_socket_net(struct sockaddr_vm *addr, + struct net *net) +{ + return vsock_find_bound_socket_type_net(addr, net, false); +} EXPORT_SYMBOL_GPL(vsock_find_bound_socket_net); -/* Find a bound socket without namespace filtering. +/* Find a datagram bound socket, filtering by namespace and mode. */ +struct sock *vsock_find_bound_dgram_socket_net(struct sockaddr_vm *addr, + struct net *net) +{ + return vsock_find_bound_socket_type_net(addr, net, true); +} +EXPORT_SYMBOL_GPL(vsock_find_bound_dgram_socket_net); + +/* Find a connection-oriented bound socket without namespace filtering. * * Use this in transports that lack namespace context. All sockets are * treated as if in global mode. @@ -832,11 +845,13 @@ static void vsock_pending_work(struct work_struct *work) /**** SOCKET OPERATIONS ****/ -static int __vsock_bind_connectible(struct vsock_sock *vsk, - struct sockaddr_vm *addr) +static int __vsock_bind_common(struct vsock_sock *vsk, + struct sockaddr_vm *addr) { - struct net *net = sock_net(sk_vsock(vsk)); + struct sock *sk = sk_vsock(vsk); + struct net *net = sock_net(sk); struct sockaddr_vm new_addr; + bool dgram = sk->sk_type == SOCK_DGRAM; if (!net->vsock.port) net->vsock.port = get_random_u32_above(LAST_RESERVED_PORT); @@ -854,7 +869,8 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk, new_addr.svm_port = net->vsock.port++; - if (!__vsock_find_bound_socket_net(&new_addr, net)) { + if (!__vsock_find_bound_socket_net(&new_addr, net, + dgram)) { found = true; break; } @@ -871,16 +887,15 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk, return -EACCES; } - if (__vsock_find_bound_socket_net(&new_addr, net)) + if (__vsock_find_bound_socket_net(&new_addr, net, dgram)) return -EADDRINUSE; } vsock_addr_init(&vsk->local_addr, new_addr.svm_cid, new_addr.svm_port); - /* Remove connection oriented sockets from the unbound list and add them - * to the hash table for easy lookup by its address. The unbound list - * is simply an extra entry at the end of the hash table, a trick used - * by AF_UNIX. + /* Move the socket from the unbound list to the hash table for lookup + * by its address. The unbound list is an extra entry at the end of + * the hash table, a trick used by AF_UNIX. */ __vsock_remove_bound(vsk); __vsock_insert_bound(vsock_bound_sockets(&vsk->local_addr), vsk); @@ -888,11 +903,26 @@ static int __vsock_bind_connectible(struct vsock_sock *vsk, return 0; } -static int __vsock_bind_dgram(struct vsock_sock *vsk, - struct sockaddr_vm *addr) +/** + * vsock_bind_common - bind a socket using the core port allocator + * @vsk: socket to bind + * @addr: local address to bind to + * + * The caller must hold the socket lock. + * + * Return: 0 on success or a negative error code on failure. + */ +int vsock_bind_common(struct vsock_sock *vsk, struct sockaddr_vm *addr) { - return vsk->transport->dgram_bind(vsk, addr); + int ret; + + spin_lock_bh(&vsock_table_lock); + ret = __vsock_bind_common(vsk, addr); + spin_unlock_bh(&vsock_table_lock); + + return ret; } +EXPORT_SYMBOL_GPL(vsock_bind_common); static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr) { @@ -914,13 +944,11 @@ static int __vsock_bind(struct sock *sk, struct sockaddr_vm *addr) switch (sk->sk_socket->type) { case SOCK_STREAM: case SOCK_SEQPACKET: - spin_lock_bh(&vsock_table_lock); - retval = __vsock_bind_connectible(vsk, addr); - spin_unlock_bh(&vsock_table_lock); + retval = vsock_bind_common(vsk, addr); break; case SOCK_DGRAM: - retval = __vsock_bind_dgram(vsk, addr); + retval = vsk->transport->dgram_bind(vsk, addr); break; default: -- 2.56.0
