net/unix: SO_REUSEPORT for AF_UNIX
Subsystems (3)
RFC PATCH 1/1 net/unix: SO_REUSEPORT for AF_UNIX
Support the SO_REUSEPORT option for AF_UNIX (aka AF_LOCAL) sockets. Note that unlike the IP implementations, the semantics for AF_UNIX sockets are those of the original BSD implementation, i.e. each socket that successfully reuses a port completely takes over from the previous listener.
The vast majority of software does an unlink() before bind() on UNIX sockets. This also effectively takes over the socket from the previous listener (given sufficient permissions), but leads to a short window of time where connections are refused because the socket doesn't exist. One can now achieve the same behaviour without dropping a single connection by using SO_REUSEPORT and not calling unlink() before bind().
The restrictions on this are the same as for the IP implementation:
listening socket on the given path must exist, also have SO_REUSEPORT set and have the same uid.
--- net/unix/af_unix.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-)
net/unix/af_unix.c +29 -2
@@ -326,6 +326,29 @@ found:return s; } +static bool unix_port_reusable (struct sock *sk,const char *sun_path, +struct path *path) +{ +struct sock *owner; +bool ret; + +if (!sk->sk_reuseport) +return false; + +if (kern_path (sun_path,LOOKUP_FOLLOW , path)) +return false; + + owner =unix_find_socket_byinode (d_backing_inode (path->dentry)); +if (!owner) +return false; + + ret = owner->sk_reuseport && + owner->sk_type == sk->sk_type && +uid_eq (sock_i_uid (sk),sock_i_uid (owner)); +sock_put (owner); +return ret; +} +static inline int unix_writable (struct sock *sk) {return (atomic_read (&sk->sk_wmem_alloc) <<2 ) <= sk->sk_sndbuf;
@@ -914,9 +937,13 @@ static int unix_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)umode_t mode =S_IFSOCK | (SOCK_INODE (sock)->i_mode & ~current_umask ()); err =unix_mknod (sun_path, mode, &path); -if (err) { -if (err == -EEXIST ) +if (err == -EEXIST ) { +if (unix_port_reusable (sk, sun_path, &path)) + err =0 ; +else err = -EADDRINUSE ; + } +if (err) {unix_release_addr (addr); goto out_up; }
Signature · 1 line
2.4.4
Quoted 5 lines · Conrad Hoffmann
Support the
SO_REUSEPORToption forAF_UNIX(akaAF_LOCAL) sockets. Note that unlike the IP implementations, the semantics forAF_UNIXsockets are those of the original BSD implementation, i.e. each socket that successfully reuses a port completely takes over from the previous listener.
This is a really weird corner case from a user's perspective. I think sharing it is more reasonable, as you can always signal the other process out of band.
Signature · 1 line
Alex Gartrell
Hi,
Quoted 10 lines · Alex Gartrell
Quoted 5 lines · Conrad Hoffmann
Support the
SO_REUSEPORToption forAF_UNIX(akaAF_LOCAL) sockets. Note that unlike the IP implementations, the semantics forAF_UNIXsockets are those of the original BSD implementation, i.e. each socket that successfully reuses a port completely takes over from the previous listener.This is a really weird corner case from a user's perspective. I think sharing it is more reasonable, as you can always signal the other process out of band.
Thanks for the feedback. While I don't think the use case is that weird I understand that the shared solution would accomodate for both this and other use case, thus being the superior one. I'll see what I can do.
Conrad