face: reserved FaceIds and NullFace

refs #1620, #1622

Change-Id: I315ae262c7e0813a64b69d3e7d67de61b7b88c55
diff --git a/daemon/face/face.hpp b/daemon/face/face.hpp
index 2db2c6a..52c0894 100644
--- a/daemon/face/face.hpp
+++ b/daemon/face/face.hpp
@@ -40,8 +40,20 @@
  */
 typedef int FaceId;
 
+/// indicates an invalid FaceId
 const FaceId INVALID_FACEID = -1;
 
+/// identifies the InternalFace used in management
+const FaceId FACEID_INTERNAL_FACE = 1;
+/// identifies a packet comes from the ContentStore, in LocalControlHeader incomingFaceId
+const FaceId FACEID_CONTENT_STORE = 254;
+/// identifies the NullFace that drops every packet
+const FaceId FACEID_NULL = 255;
+/// upper bound of reserved FaceIds
+const FaceId FACEID_RESERVED_MAX = 255;
+
+
+/// pratical limit of packet size in octets
 const size_t MAX_NDN_PACKET_SIZE = 8800;
 
 
diff --git a/daemon/face/null-face.cpp b/daemon/face/null-face.cpp
new file mode 100644
index 0000000..5d55693
--- /dev/null
+++ b/daemon/face/null-face.cpp
@@ -0,0 +1,54 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "null-face.hpp"
+
+namespace nfd {
+
+// FIB might restrict creating a nexthop record toward non-local face in /localhost namespace.
+// NullFace has isLocal=true to enable creating a "blackhole" FIB entry under /localhost.
+
+NullFace::NullFace(const FaceUri& uri)
+  : Face(uri, uri, true)
+{
+}
+
+void
+NullFace::sendInterest(const Interest& interest)
+{
+}
+
+void
+NullFace::sendData(const Data& data)
+{
+}
+
+void
+NullFace::close()
+{
+  this->fail("close");
+}
+
+} // namespace nfd
diff --git a/daemon/face/null-face.hpp b/daemon/face/null-face.hpp
new file mode 100644
index 0000000..3dae571
--- /dev/null
+++ b/daemon/face/null-face.hpp
@@ -0,0 +1,60 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef NFD_DAEMON_FACE_NULL_FACE_HPP
+#define NFD_DAEMON_FACE_NULL_FACE_HPP
+
+#include "face.hpp"
+
+namespace nfd {
+
+/**
+ * \brief a Face that has no underlying transport and drops every packet
+ */
+class NullFace : public Face
+{
+public:
+  explicit
+  NullFace(const FaceUri& uri = FaceUri("null://"));
+
+  virtual void
+  sendInterest(const Interest& interest);
+
+  /// send a Data
+  virtual void
+  sendData(const Data& data);
+
+  /** \brief Close the face
+   *
+   *  This terminates all communication on the face and cause
+   *  onFail() method event to be invoked
+   */
+  virtual void
+  close();
+};
+
+} // namespace nfd
+
+#endif // NFD_DAEMON_FACE_NULL_FACE_HPP
diff --git a/daemon/fw/face-table.cpp b/daemon/fw/face-table.cpp
index 10dbdfd..a95c562 100644
--- a/daemon/fw/face-table.cpp
+++ b/daemon/fw/face-table.cpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "face-table.hpp"
 #include "forwarder.hpp"
@@ -32,7 +33,7 @@
 
 FaceTable::FaceTable(Forwarder& forwarder)
   : m_forwarder(forwarder)
-  , m_lastFaceId(0)
+  , m_lastFaceId(FACEID_RESERVED_MAX)
 {
 }
 
@@ -44,18 +45,32 @@
 void
 FaceTable::add(shared_ptr<Face> face)
 {
-  if (face->getId() != INVALID_FACEID &&
-      m_faces.count(face->getId()) > 0)
-    {
-      NFD_LOG_DEBUG("Trying to add existing face id=" << face->getId() << " to the face table");
-      return;
-    }
+  if (face->getId() != INVALID_FACEID && m_faces.count(face->getId()) > 0) {
+    NFD_LOG_WARN("Trying to add existing face id=" << face->getId() << " to the face table");
+    return;
+  }
 
   FaceId faceId = ++m_lastFaceId;
+  BOOST_ASSERT(faceId > FACEID_RESERVED_MAX);
+  this->addImpl(face, faceId);
+}
+
+void
+FaceTable::addReserved(shared_ptr<Face> face, FaceId faceId)
+{
+  BOOST_ASSERT(face->getId() == INVALID_FACEID);
+  BOOST_ASSERT(m_faces.count(face->getId()) == 0);
+  BOOST_ASSERT(faceId <= FACEID_RESERVED_MAX);
+  this->addImpl(face, faceId);
+}
+
+void
+FaceTable::addImpl(shared_ptr<Face> face, FaceId faceId)
+{
   face->setId(faceId);
   m_faces[faceId] = face;
-  NFD_LOG_INFO("Added face id=" << faceId << " remote=" << face->getRemoteUri() <<
-                                              " local=" << face->getLocalUri());
+  NFD_LOG_INFO("Added face id=" << faceId << " remote=" << face->getRemoteUri()
+                                          << " local=" << face->getLocalUri());
 
   face->onReceiveInterest += bind(&Forwarder::onInterest,
                                   &m_forwarder, ref(*face), _1);
diff --git a/daemon/fw/face-table.hpp b/daemon/fw/face-table.hpp
index 8f7dcb2..a255cfe 100644
--- a/daemon/fw/face-table.hpp
+++ b/daemon/fw/face-table.hpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #ifndef NFD_DAEMON_FW_FACE_TABLE_HPP
 #define NFD_DAEMON_FW_FACE_TABLE_HPP
@@ -35,7 +36,7 @@
 
 /** \brief container of all Faces
  */
-class FaceTable
+class FaceTable : noncopyable
 {
 public:
   explicit
@@ -47,6 +48,10 @@
   VIRTUAL_WITH_TESTS void
   add(shared_ptr<Face> face);
 
+  /// add a special Face with a reserved FaceId
+  VIRTUAL_WITH_TESTS void
+  addReserved(shared_ptr<Face> face, FaceId faceId);
+
   VIRTUAL_WITH_TESTS shared_ptr<Face>
   get(FaceId id) const;
 
@@ -88,8 +93,11 @@
   EventEmitter<shared_ptr<Face> > onRemove;
 
 private:
+  void
+  addImpl(shared_ptr<Face> face, FaceId faceId);
+
   // remove is private because it's a subscriber of face.onFail event.
-  // face->close() closes a face and would trigger .remove(face)
+  // face->close() closes a face and triggers .remove(face)
   void
   remove(shared_ptr<Face> face);
 
diff --git a/daemon/fw/forwarder.cpp b/daemon/fw/forwarder.cpp
index a57a756..d3fb541 100644
--- a/daemon/fw/forwarder.cpp
+++ b/daemon/fw/forwarder.cpp
@@ -92,6 +92,7 @@
     // CS lookup
     const Data* csMatch = m_cs.find(interest);
     if (csMatch != 0) {
+      const_cast<Data*>(csMatch)->setIncomingFaceId(FACEID_CONTENT_STORE);
       // XXX should we lookup PIT for other Interests that also match csMatch?
 
       // goto outgoing Data pipeline
diff --git a/daemon/main.cpp b/daemon/main.cpp
index 70717c6..2509307 100644
--- a/daemon/main.cpp
+++ b/daemon/main.cpp
@@ -31,6 +31,7 @@
 #include "core/global-io.hpp"
 #include "core/privilege-helper.hpp"
 #include "fw/forwarder.hpp"
+#include "face/null-face.hpp"
 #include "mgmt/internal-face.hpp"
 #include "mgmt/fib-manager.hpp"
 #include "mgmt/face-manager.hpp"
@@ -78,6 +79,10 @@
 
     initializeManagement();
 
+    m_forwarder->getFaceTable().addReserved(make_shared<NullFace>(), FACEID_NULL);
+    m_forwarder->getFaceTable().addReserved(
+      make_shared<NullFace>(FaceUri("contentstore://")), FACEID_CONTENT_STORE);
+
     PrivilegeHelper::drop();
   }
 
@@ -125,19 +130,14 @@
     m_fibManager = make_shared<FibManager>(ref(m_forwarder->getFib()),
                                            bind(&Forwarder::getFace, m_forwarder.get(), _1),
                                            m_internalFace);
-
     m_faceManager = make_shared<FaceManager>(ref(m_forwarder->getFaceTable()),
                                              m_internalFace);
+    m_strategyChoiceManager = make_shared<StrategyChoiceManager>(
+                                ref(m_forwarder->getStrategyChoice()), m_internalFace);
 
-    m_strategyChoiceManager =
-      make_shared<StrategyChoiceManager>(ref(m_forwarder->getStrategyChoice()),
-                                         m_internalFace);
-
-    m_statusServer = make_shared<StatusServer>(m_internalFace,
-                                               ref(*m_forwarder));
+    m_statusServer = make_shared<StatusServer>(m_internalFace, ref(*m_forwarder));
 
     ConfigFile config((IgnoreRibAndLogSections()));
-
     general::setConfigFile(config);
 
     TablesConfigSection tablesConfig(m_forwarder->getCs(),
@@ -145,12 +145,11 @@
                                      m_forwarder->getFib(),
                                      m_forwarder->getStrategyChoice(),
                                      m_forwarder->getMeasurements());
-
     tablesConfig.setConfigFile(config);
 
     m_internalFace->getValidator().setConfigFile(config);
 
-    m_forwarder->addFace(m_internalFace);
+    m_forwarder->getFaceTable().addReserved(m_internalFace, FACEID_INTERNAL_FACE);
 
     m_faceManager->setConfigFile(config);
 
diff --git a/tests/daemon/face/null.cpp b/tests/daemon/face/null.cpp
new file mode 100644
index 0000000..db90410
--- /dev/null
+++ b/tests/daemon/face/null.cpp
@@ -0,0 +1,51 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
+ *
+ * This file is part of NFD (Named Data Networking Forwarding Daemon).
+ * See AUTHORS.md for complete list of NFD authors and contributors.
+ *
+ * NFD is free software: you can redistribute it and/or modify it under the terms
+ * of the GNU General Public License as published by the Free Software Foundation,
+ * either version 3 of the License, or (at your option) any later version.
+ *
+ * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
+ * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+ * PURPOSE.  See the GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along with
+ * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "face/null-face.hpp"
+
+#include "tests/test-common.hpp"
+
+namespace nfd {
+namespace tests {
+
+BOOST_FIXTURE_TEST_SUITE(FaceNull, BaseFixture)
+
+BOOST_AUTO_TEST_CASE(Send)
+{
+  shared_ptr<NullFace> face = make_shared<NullFace>();
+
+  shared_ptr<Interest> interest = makeInterest("/A");
+  BOOST_CHECK_NO_THROW(face->sendInterest(*interest));
+
+  shared_ptr<Data> data = makeData("/B");
+  BOOST_CHECK_NO_THROW(face->sendData(*data));
+
+  BOOST_CHECK_NO_THROW(face->close());
+}
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nfd
diff --git a/tests/daemon/fw/face-table.cpp b/tests/daemon/fw/face-table.cpp
index 3f49525..0dd8432 100644
--- a/tests/daemon/fw/face-table.cpp
+++ b/tests/daemon/fw/face-table.cpp
@@ -1,11 +1,12 @@
 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
 /**
- * Copyright (c) 2014  Regents of the University of California,
- *                     Arizona Board of Regents,
- *                     Colorado State University,
- *                     University Pierre & Marie Curie, Sorbonne University,
- *                     Washington University in St. Louis,
- *                     Beijing Institute of Technology
+ * Copyright (c) 2014,  Regents of the University of California,
+ *                      Arizona Board of Regents,
+ *                      Colorado State University,
+ *                      University Pierre & Marie Curie, Sorbonne University,
+ *                      Washington University in St. Louis,
+ *                      Beijing Institute of Technology,
+ *                      The University of Memphis
  *
  * This file is part of NFD (Named Data Networking Forwarding Daemon).
  * See AUTHORS.md for complete list of NFD authors and contributors.
@@ -20,7 +21,7 @@
  *
  * You should have received a copy of the GNU General Public License along with
  * NFD, e.g., in COPYING.md file.  If not, see <http://www.gnu.org/licenses/>.
- **/
+ */
 
 #include "fw/face-table.hpp"
 #include "fw/forwarder.hpp"
@@ -61,6 +62,8 @@
   BOOST_CHECK_NE(face1->getId(), INVALID_FACEID);
   BOOST_CHECK_NE(face2->getId(), INVALID_FACEID);
   BOOST_CHECK_NE(face1->getId(), face2->getId());
+  BOOST_CHECK_GT(face1->getId(), FACEID_RESERVED_MAX);
+  BOOST_CHECK_GT(face2->getId(), FACEID_RESERVED_MAX);
 
   FaceId oldId1 = face1->getId();
   faceTable.add(face1);
@@ -79,6 +82,18 @@
   BOOST_CHECK_EQUAL(onRemoveHistory[0], onAddHistory[0]);
 }
 
+BOOST_AUTO_TEST_CASE(AddReserved)
+{
+  Forwarder forwarder;
+  FaceTable& faceTable = forwarder.getFaceTable();
+
+  shared_ptr<Face> face1 = make_shared<DummyFace>();
+  BOOST_CHECK_EQUAL(face1->getId(), INVALID_FACEID);
+
+  faceTable.addReserved(face1, 5);
+  BOOST_CHECK_EQUAL(face1->getId(), 5);
+}
+
 BOOST_AUTO_TEST_CASE(Enumerate)
 {
   Forwarder forwarder;
diff --git a/tests/daemon/mgmt/face-manager.cpp b/tests/daemon/mgmt/face-manager.cpp
index 92ec987..602db76 100644
--- a/tests/daemon/mgmt/face-manager.cpp
+++ b/tests/daemon/mgmt/face-manager.cpp
@@ -1587,13 +1587,13 @@
 
   ControlParameters resultParameters;
   resultParameters.setUri("dummy://");
-  resultParameters.setFaceId(1);
+  resultParameters.setFaceId(FACEID_RESERVED_MAX + 1);
 
   shared_ptr<DummyFace> dummy(make_shared<DummyFace>());
 
   ndn::nfd::FaceEventNotification expectedFaceEvent;
   expectedFaceEvent.setKind(ndn::nfd::FACE_EVENT_CREATED)
-                   .setFaceId(1)
+                   .setFaceId(FACEID_RESERVED_MAX + 1)
                    .setRemoteUri(dummy->getRemoteUri().toString())
                    .setLocalUri(dummy->getLocalUri().toString())
                    .setFlags(0);