face: congestion detection in TCP, UDP, and Unix socket transports

refs #4362

Change-Id: Idaa5d65e1f33663d95bad56de42640183b2cda6d
diff --git a/daemon/face/tcp-transport.cpp b/daemon/face/tcp-transport.cpp
index c14cab2..c95f693 100644
--- a/daemon/face/tcp-transport.cpp
+++ b/daemon/face/tcp-transport.cpp
@@ -1,6 +1,6 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
-/**
- * Copyright (c) 2014-2017,  Regents of the University of California,
+/*
+ * Copyright (c) 2014-2018,  Regents of the University of California,
  *                           Arizona Board of Regents,
  *                           Colorado State University,
  *                           University Pierre & Marie Curie, Sorbonne University,
@@ -25,6 +25,11 @@
 
 #include "tcp-transport.hpp"
 
+#if defined(__linux__)
+#include <linux/sockios.h>
+#include <sys/ioctl.h>
+#endif
+
 namespace nfd {
 namespace face {
 
@@ -55,6 +60,28 @@
   NFD_LOG_FACE_INFO("Creating transport");
 }
 
+ssize_t
+TcpTransport::getSendQueueLength()
+{
+  int queueLength = getSendQueueBytes();
+
+  // We want to obtain the amount of "not sent" bytes instead of the amount of "not sent" + "not
+  // acked" bytes. On Linux, we use SIOCOUTQNSD for this reason. However, macOS does not provide an
+  // efficient mechanism to obtain this value (SO_NWRITE includes both "not sent" and "not acked").
+#if defined(__linux__)
+  int nsd;
+  if (ioctl(m_socket.native_handle(), SIOCOUTQNSD, &nsd) < 0) {
+    NFD_LOG_FACE_WARN("Failed to obtain send queue length from socket: " << std::strerror(errno));
+  }
+  else if (nsd > 0) {
+    NFD_LOG_FACE_TRACE("SIOCOUTQNSD=" << nsd);
+    queueLength += nsd;
+  }
+#endif
+
+  return queueLength;
+}
+
 bool
 TcpTransport::canChangePersistencyToImpl(ndn::nfd::FacePersistency newPersistency) const
 {