title: Are UDP packets sent over a single socket guaranteed to be sent in order?
body: <p><strong><em>I realize it's a bad idea to rely on the UDP protocol to provide any sort of ordering guarantees and TCP should be used instead. Please refrain from answers suggesting I use TCP.</em></strong></p>
  
  <p>I am debugging some legacy networking code and was wondering what are the ordering guarantees provided by the socket interface. The setup I have consists of a linux box running Debian talking to an embedded device over a direct ethernet cable. The embedded device cannot fit an entire TCP stack and even if it did, the legacy stack is too old to refactor.</p>
  
  <p>Specifically, if I have a NIC configured with the default single <a href="http://www.tldp.org/HOWTO/Traffic-Control-HOWTO/classless-qdiscs.html#qs-pfifo_fast" rel="nofollow noreferrer">pfifo_fast</a>, and I am sending packets over a single socket, using a single thread, and they all have the same <a href="https://en.wikipedia.org/wiki/Type_of_service" rel="nofollow noreferrer">ToS</a>, under these conditions, am I guaranteed that all my packets will be sent over the wire in the order I send them?</p>
  
  <p>This is the behavior I observe in practice but I haven't found any standard, POSIX or otherwise that guarantees this behavior and I would like to make sure this is in fact supported behavior under the environments and assumptions I listed above.</p>
  
  <p>In contrast, if I send my packets over two separate sockets I observe that they are <strong>not</strong> sent over the NIC in the same order I sent them from the application code. It's clear that the kernel is reserving the right to re-order packets sent over separate sockets, but refrains from doing so when using a single socket.</p>
  
  <p>As I understand it, calling <code>send()</code> on a socket places the packet into the appropriate queue for the NIC synchronously. The existence of a <em>queue</em> suggests to me a notion of order (otherwise a <em>list</em> would be a more appropriate name for the data structure). Whether or not such ordering guarantees exist, I am interested in some sort of documentation or specification stating so.</p>