3 ms·
For more technical detail, https://linuxjournal.rubdos.be/ljarchive/LJ/298/12538.html https://linuxjournal.rubdos.be/ljarchive/LJ/298/12538.html goes into a TON
by btown 5y ago
For more technical detail, https://linuxjournal.rubdos.be/ljarchive/LJ/298/12538.html https://linuxjournal.rubdos.be/ljarchive/LJ/298/12538.html goes into a TON more detail on this, including simplified code for the implementation of the which-socket-should-receive-this-packet algorithm.
TL;DR:
- the OS maintains a hash map mapping port/IP/protocol/protocol-version to lists of sockets
- there's a scoring algorithm that determines which socket wins, based on specificity of how the socket(s) were bound
- if multiple sockets are bound with SO_REUSEPORT, things are routed based on a hash of the source/destination address/port, presumably so packets from a single client aren't split between different handlers - essentially, simple zero-backpressure load balancing
- read the article for nuances of when and how to use this, especially performance considerations about pre/post fork
- consider adding TL;DR segments like this, whether you're writing discovery fiction like the OP, or technical deep-dives like the link in this comment. Different readers will prefer different formats, but everyone benefits from having the option to consume an abstract before reading lengthy nonfiction prose!
- vbezhenar 5y agoHow does ICMP routing work? I always wondered about that. There's no port in ICMP, how does OS understand which ping executable receives ICMP echo replies.
- toyg 5y agoIf I read this [0] correctly, there is still a hashmap of sorts. [0] https://flylib.com/books/en/3.475.1.74/1/ https://flylib.com/books/en/3.475.1.74/1/
- zamadatix 5y agoICMP has an identifier field and a sequence number field, both exist to aid in matching requests with replies.
- vbezhenar 5y agoThanks. It seems that's not exactly correct. ICMP has type, code and checksum fields. Rest of data is type-specific. But echo-request and echo-reply types do have identifier and sequence fields indeed.
- sedatk 5y agoAppreciated!
- toomanydoubts 5y agoThanks for synthesizing.
- sharikous 5y agoOh thank you! With all respect to the OP reading the article just tingled my curiosity without satisfying it.