From: Sergio Lopez <slp@redhat.com>
To: "Stefano Garzarella" <sgarzare@redhat.com>,
"Bobby Eshleman" <bobbyeshleman@meta.com>,
"Alexei Starovoitov" <ast@kernel.org>,
"David S. Miller" <davem@davemloft.net>,
"Eric Dumazet" <edumazet@kernel.org>,
"Jakub Kicinski" <kuba@kernel.org>,
"Paolo Abeni" <pabeni@redhat.com>,
"Simon Horman" <horms@kernel.org>,
"Bryan Tan" <bryan-bt.tan@broadcom.com>,
"Vishnu Dasa" <vishnu.dasa@broadcom.com>,
"Broadcom internal kernel review list"
<bcm-kernel-feedback-list@broadcom.com>,
"Stefan Hajnoczi" <stefanha@redhat.com>,
"Michael S. Tsirkin" <mst@redhat.com>,
"Jason Wang" <jasowangio@gmail.com>,
"Eugenio Pérez" <eperezma@redhat.com>,
"Xuan Zhuo" <xuanzhuo@linux.alibaba.com>,
"Haiyang Zhang" <haiyangz@microsoft.com>,
"Wei Liu" <wei.liu@kernel.org>,
"Dexuan Cui" <decui@microsoft.com>,
"Shuah Khan" <skhan@linuxfoundation.org>,
"Randy Dunlap" <rdunlap@infradead.org>
Cc: virtualization@lists.linux.dev, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
bpf@vger.kernel.org, linux-hyperv@vger.kernel.org,
Jonathan Corbet <corbet@lwn.net>,
linux-doc@vger.kernel.org, Sergio Lopez <slp@redhat.com>
Subject: [PATCH RFC v2 1/8] vsock: expose the core port allocator to transports
Date: Fri, 13 Nov 2026 13:47:34 +0100 [thread overview]
Message-ID: <20261113-vsock-dgram-support-v2-1-bacfc85b71ce@redhat.com> (raw)
In-Reply-To: <20261113-vsock-dgram-support-v2-0-bacfc85b71ce@redhat.com>
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 <slp@redhat.com>
---
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
next prev parent reply other threads:[~2026-11-13 12:49 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-13 12:47 [PATCH RFC v2 0/8] vsock: add datagram support to virtio and vhost Sergio Lopez
2026-11-13 12:47 ` Sergio Lopez [this message]
2026-11-13 12:57 ` [PATCH RFC v2 1/8] vsock: expose the core port allocator to transports sashiko-bot
2026-11-13 12:47 ` [PATCH RFC v2 2/8] vsock: report datagram receive and send queue state Sergio Lopez
2026-11-13 12:59 ` sashiko-bot
2026-11-13 12:47 ` [PATCH RFC v2 3/8] vsock/virtio: implement the datagram data path Sergio Lopez
2026-11-13 13:16 ` sashiko-bot
2026-11-13 12:47 ` [PATCH RFC v2 4/8] vsock/virtio: negotiate datagram support on the guest Sergio Lopez
2026-11-13 13:08 ` sashiko-bot
2026-11-13 12:47 ` [PATCH RFC v2 5/8] vhost/vsock: negotiate datagram support with guests Sergio Lopez
2026-11-13 13:17 ` sashiko-bot
2026-11-13 12:47 ` [PATCH RFC v2 6/8] vsock: allow datagrams to use directional transports Sergio Lopez
2026-11-13 13:39 ` sashiko-bot
2026-11-13 12:47 ` [PATCH RFC v2 7/8] vsock: fragment datagrams across receive buffers Sergio Lopez
2026-11-13 13:07 ` sashiko-bot
2026-11-13 12:47 ` [PATCH RFC v2 8/8] vsock: add datagram tests to vsock_test Sergio Lopez
2026-11-13 13:14 ` sashiko-bot
2026-11-13 13:05 ` [PATCH RFC v2 0/8] vsock: add datagram support to virtio and vhost Michael S. Tsirkin
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261113-vsock-dgram-support-v2-1-bacfc85b71ce@redhat.com \
--to=slp@redhat.com \
--cc=ast@kernel.org \
--cc=bcm-kernel-feedback-list@broadcom.com \
--cc=bobbyeshleman@meta.com \
--cc=bpf@vger.kernel.org \
--cc=bryan-bt.tan@broadcom.com \
--cc=corbet@lwn.net \
--cc=davem@davemloft.net \
--cc=decui@microsoft.com \
--cc=edumazet@kernel.org \
--cc=eperezma@redhat.com \
--cc=haiyangz@microsoft.com \
--cc=horms@kernel.org \
--cc=jasowangio@gmail.com \
--cc=kuba@kernel.org \
--cc=kvm@vger.kernel.org \
--cc=linux-doc@vger.kernel.org \
--cc=linux-hyperv@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mst@redhat.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=rdunlap@infradead.org \
--cc=sgarzare@redhat.com \
--cc=skhan@linuxfoundation.org \
--cc=stefanha@redhat.com \
--cc=virtualization@lists.linux.dev \
--cc=vishnu.dasa@broadcom.com \
--cc=wei.liu@kernel.org \
--cc=xuanzhuo@linux.alibaba.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox