All of lore.kernel.org
 help / color / mirror / Atom feed
* UDP recvfrom/recv question on connected/unconnected sockets
@ 2024-04-18 17:07 Bob McMahon
  2024-04-18 17:44 ` Kuniyuki Iwashima
  0 siblings, 1 reply; 2+ messages in thread
From: Bob McMahon @ 2024-04-18 17:07 UTC (permalink / raw)
  To: netdev


[-- Attachment #1.1: Type: text/plain, Size: 1250 bytes --]

Hi All,

I have a question about the OS routing UDP packets to threads and connect
vs unconnected sockets. Same src/dst IP and same dst port, different src
port.

If there are two UDP sockets listening on the same port, each serviced by
its own thread and they both hang a recvfrom() or recv() (for the connected
socket,) will the OS route packets only to the thread with a connected
socket vs the thread with th unconnected socket? If not, what will happen?

Bob

-- 
This electronic communication and the information and any files transmitted 
with it, or attached to it, are confidential and are intended solely for 
the use of the individual or entity to whom it is addressed and may contain 
information that is confidential, legally privileged, protected by privacy 
laws, or otherwise restricted from disclosure to anyone else. If you are 
not the intended recipient or the person responsible for delivering the 
e-mail to the intended recipient, you are hereby notified that any use, 
copying, distributing, dissemination, forwarding, printing, or copying of 
this e-mail is strictly prohibited. If you received this e-mail in error, 
please return the e-mail to the sender, delete it from your computer, and 
destroy any printed copy of it.

[-- Attachment #1.2: Type: text/html, Size: 1395 bytes --]

[-- Attachment #2: S/MIME Cryptographic Signature --]
[-- Type: application/pkcs7-signature, Size: 4206 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: UDP recvfrom/recv question on connected/unconnected sockets
  2024-04-18 17:07 UDP recvfrom/recv question on connected/unconnected sockets Bob McMahon
@ 2024-04-18 17:44 ` Kuniyuki Iwashima
  0 siblings, 0 replies; 2+ messages in thread
From: Kuniyuki Iwashima @ 2024-04-18 17:44 UTC (permalink / raw)
  To: bob.mcmahon; +Cc: netdev, kuniyu

From: Bob McMahon <bob.mcmahon@broadcom.com>
Date: Thu, 18 Apr 2024 10:07:51 -0700
> Hi All,
> 
> I have a question about the OS routing UDP packets to threads and connect
> vs unconnected sockets. Same src/dst IP and same dst port, different src
> port.
> 
> If there are two UDP sockets listening on the same port, each serviced by
> its own thread and they both hang a recvfrom() or recv() (for the connected
> socket,) will the OS route packets only to the thread with a connected
> socket vs the thread with th unconnected socket? If not, what will happen?

Connected sockets receive packets matching 4-tuple, and unconnected sockets
receive packets that no connected socket matches.

  $ python3
  >>> from socket import *
  >>> 
  >>> s1 = socket(AF_INET, SOCK_DGRAM)
  >>> s1.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
  >>> s1.bind(('0', 0))
  >>> 
  >>> s2 = socket(AF_INET, SOCK_DGRAM)
  >>> s2.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
  >>> s2.bind(s1.getsockname())
  >>> 
  >>> s1.connect(('10.0.0.53', 8000))
  >>> s1
  <socket.socket fd=3, family=AddressFamily.AF_INET, type=SocketKind.SOCK_DGRAM, proto=0, laddr=('10.0.0.53', 28947), raddr=('10.0.0.53', 8000)>
  >>> s2
  <socket.socket fd=4, family=AddressFamily.AF_INET, type=SocketKind.SOCK_DGRAM, proto=0, laddr=('0.0.0.0', 28947)>
  >>> 
  >>> s3 = socket(AF_INET, SOCK_DGRAM)
  >>> s3.bind(('10.0.0.53', 8000))
  >>> 
  >>> s4 = socket(AF_INET, SOCK_DGRAM)
  >>> s4.bind(('10.0.0.53', 8080))
  >>> 
  >>> s3.sendto(b'hello', s1.getsockname())
  5
  >>> s4.sendto(b'world', s1.getsockname())
  5
  >>> 
  >>> s1.recv(10)
  b'hello'
  >>> s2.recv(10)
  b'world'

Then, if you create a new unconnected socket, the old unconnected socket will
no longer receive packets until the new one is close()d.

  >>> s5 = socket(AF_INET, SOCK_DGRAM)
  >>> s5.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
  >>> s5.bind(s1.getsockname())
  >>> 
  >>> s4.sendto(b'test', s1.getsockname())
  4
  >>> s2.recv(10)
  ^CTraceback (most recent call last):
    File "<stdin>", line 1, in <module>
  KeyboardInterrupt
  >>> s5.recv(10)
  b'test'

SO_REUSEADDR allows the newer socket to take over the port from the old
ones.

If you want to blance the loads across unconnected sockets, use SO_REUSPORT
instead.

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-04-18 17:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-18 17:07 UDP recvfrom/recv question on connected/unconnected sockets Bob McMahon
2024-04-18 17:44 ` Kuniyuki Iwashima

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.