mgmt: add Mtu to faces/create and FaceStatus

refs #4005

Change-Id: I26c02aa088dc59aecd331cd579036177fef0f64c
diff --git a/src/encoding/tlv-nfd.hpp b/src/encoding/tlv-nfd.hpp
index 12ef55b..dcaed7e 100644
--- a/src/encoding/tlv-nfd.hpp
+++ b/src/encoding/tlv-nfd.hpp
@@ -69,6 +69,7 @@
   LinkType                      = 134,
   BaseCongestionMarkingInterval = 135,
   DefaultCongestionThreshold    = 136,
+  Mtu                           = 137,
   FaceQueryFilter               = 150,
   FaceEventNotification         = 192,
   FaceEventKind                 = 193,
diff --git a/src/mgmt/nfd/control-command.cpp b/src/mgmt/nfd/control-command.cpp
index 07e8b17..f851bf1 100644
--- a/src/mgmt/nfd/control-command.cpp
+++ b/src/mgmt/nfd/control-command.cpp
@@ -105,6 +105,7 @@
     .optional(CONTROL_PARAMETER_FACE_PERSISTENCY)
     .optional(CONTROL_PARAMETER_BASE_CONGESTION_MARKING_INTERVAL)
     .optional(CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD)
+    .optional(CONTROL_PARAMETER_MTU)
     .optional(CONTROL_PARAMETER_FLAGS)
     .optional(CONTROL_PARAMETER_MASK);
   m_responseValidator
@@ -114,6 +115,7 @@
     .required(CONTROL_PARAMETER_FACE_PERSISTENCY)
     .optional(CONTROL_PARAMETER_BASE_CONGESTION_MARKING_INTERVAL)
     .optional(CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD)
+    .optional(CONTROL_PARAMETER_MTU)
     .required(CONTROL_PARAMETER_FLAGS);
 }
 
diff --git a/src/mgmt/nfd/control-parameters.cpp b/src/mgmt/nfd/control-parameters.cpp
index 65360a5..4d826ef 100644
--- a/src/mgmt/nfd/control-parameters.cpp
+++ b/src/mgmt/nfd/control-parameters.cpp
@@ -51,6 +51,9 @@
 {
   size_t totalLength = 0;
 
+  if (this->hasMtu()) {
+    totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Mtu, m_mtu);
+  }
   if (this->hasDefaultCongestionThreshold()) {
     totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::DefaultCongestionThreshold,
                                                   m_defaultCongestionThreshold);
@@ -228,6 +231,12 @@
   if (this->hasDefaultCongestionThreshold()) {
     m_defaultCongestionThreshold = readNonNegativeInteger(*val);
   }
+
+  val = m_wire.find(tlv::nfd::Mtu);
+  m_hasFields[CONTROL_PARAMETER_MTU] = val != m_wire.elements_end();
+  if (this->hasMtu()) {
+    m_mtu = readNonNegativeInteger(*val);
+  }
 }
 
 bool
@@ -368,6 +377,10 @@
     os << "DefaultCongestionThreshold: " << parameters.getDefaultCongestionThreshold() << ", ";
   }
 
+  if (parameters.hasMtu()) {
+    os << "Mtu: " << parameters.getMtu() << ", ";
+  }
+
   os << ")";
   return os;
 }
diff --git a/src/mgmt/nfd/control-parameters.hpp b/src/mgmt/nfd/control-parameters.hpp
index c325e0b..2291096 100644
--- a/src/mgmt/nfd/control-parameters.hpp
+++ b/src/mgmt/nfd/control-parameters.hpp
@@ -49,6 +49,7 @@
   CONTROL_PARAMETER_FACE_PERSISTENCY,
   CONTROL_PARAMETER_BASE_CONGESTION_MARKING_INTERVAL,
   CONTROL_PARAMETER_DEFAULT_CONGESTION_THRESHOLD,
+  CONTROL_PARAMETER_MTU,
   CONTROL_PARAMETER_UBOUND
 };
 
@@ -67,7 +68,8 @@
   "ExpirationPeriod",
   "FacePersistency",
   "BaseCongestionMarkingInterval",
-  "DefaultCongestionThreshold"
+  "DefaultCongestionThreshold",
+  "Mtu"
 };
 
 /**
@@ -559,6 +561,44 @@
     return *this;
   }
 
+  bool
+  hasMtu() const
+  {
+    return m_hasFields[CONTROL_PARAMETER_MTU];
+  }
+
+  /** \brief get MTU (measured in bytes)
+   *
+   *  This value is capped at MAX_NDN_PACKET_SIZE, even if the MTU of the face is unlimited.
+   */
+  uint64_t
+  getMtu() const
+  {
+    BOOST_ASSERT(this->hasMtu());
+    return m_mtu;
+  }
+
+  /** \brief set MTU (measured in bytes)
+   *
+   *  This value is capped at MAX_NDN_PACKET_SIZE, even if the MTU of the face is unlimited.
+   */
+  ControlParameters&
+  setMtu(uint64_t mtu)
+  {
+    m_wire.reset();
+    m_mtu = mtu;
+    m_hasFields[CONTROL_PARAMETER_MTU] = true;
+    return *this;
+  }
+
+  ControlParameters&
+  unsetMtu()
+  {
+    m_wire.reset();
+    m_hasFields[CONTROL_PARAMETER_MTU] = false;
+    return *this;
+  }
+
   const std::vector<bool>&
   getPresentFields() const
   {
@@ -615,6 +655,7 @@
   FacePersistency     m_facePersistency;
   time::nanoseconds   m_baseCongestionMarkingInterval;
   uint64_t            m_defaultCongestionThreshold;
+  uint64_t            m_mtu;
 
 private:
   mutable Block m_wire;
diff --git a/src/mgmt/nfd/face-status.cpp b/src/mgmt/nfd/face-status.cpp
index 0c127ae..dc74eb1 100644
--- a/src/mgmt/nfd/face-status.cpp
+++ b/src/mgmt/nfd/face-status.cpp
@@ -63,6 +63,9 @@
   totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInNacks, m_nInNacks);
   totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInData, m_nInData);
   totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::NInInterests, m_nInInterests);
+  if (m_mtu) {
+    totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::Mtu, *m_mtu);
+  }
   if (m_defaultCongestionThreshold) {
     totalLength += prependNonNegativeIntegerBlock(encoder, tlv::nfd::DefaultCongestionThreshold,
                                                   *m_defaultCongestionThreshold);
@@ -187,6 +190,14 @@
     m_defaultCongestionThreshold = nullopt;
   }
 
+  if (val != m_wire.elements_end() && val->type() == tlv::nfd::Mtu) {
+    m_mtu = readNonNegativeInteger(*val);
+    ++val;
+  }
+  else {
+    m_mtu = nullopt;
+  }
+
   if (val != m_wire.elements_end() && val->type() == tlv::nfd::NInInterests) {
     m_nInInterests = readNonNegativeInteger(*val);
     ++val;
@@ -309,6 +320,22 @@
 }
 
 FaceStatus&
+FaceStatus::setMtu(uint64_t mtu)
+{
+  m_wire.reset();
+  m_mtu = mtu;
+  return *this;
+}
+
+FaceStatus&
+FaceStatus::unsetMtu()
+{
+  m_wire.reset();
+  m_mtu = nullopt;
+  return *this;
+}
+
+FaceStatus&
 FaceStatus::setNInInterests(uint64_t nInInterests)
 {
   m_wire.reset();
@@ -390,6 +417,8 @@
       a.hasDefaultCongestionThreshold() == b.hasDefaultCongestionThreshold() &&
       (!a.hasDefaultCongestionThreshold() ||
        a.getDefaultCongestionThreshold() == b.getDefaultCongestionThreshold()) &&
+      a.hasMtu() == b.hasMtu() &&
+      (!a.hasMtu() || a.getMtu() == b.getMtu()) &&
       a.getNInInterests() == b.getNInInterests() &&
       a.getNInData() == b.getNInData() &&
       a.getNInNacks() == b.getNInNacks() &&
@@ -426,6 +455,10 @@
     os << "     DefaultCongestionThreshold: " << status.getDefaultCongestionThreshold() << " bytes,\n";
   }
 
+  if (status.hasMtu()) {
+    os << "     Mtu: " << status.getMtu() << " bytes,\n";
+  }
+
   os << "     Flags: " << AsHex{status.getFlags()} << ",\n"
      << "     Counters: {Interests: {in: " << status.getNInInterests() << ", "
      << "out: " << status.getNOutInterests() << "},\n"
diff --git a/src/mgmt/nfd/face-status.hpp b/src/mgmt/nfd/face-status.hpp
index 30a97f5..3de21fe 100644
--- a/src/mgmt/nfd/face-status.hpp
+++ b/src/mgmt/nfd/face-status.hpp
@@ -119,6 +119,33 @@
   FaceStatus&
   unsetDefaultCongestionThreshold();
 
+  bool
+  hasMtu() const
+  {
+    return !!m_mtu;
+  }
+
+  /** \brief get MTU (measured in bytes)
+   *
+   *  This value is capped at MAX_NDN_PACKET_SIZE, even if the MTU of the face is unlimited.
+   */
+  uint64_t
+  getMtu() const
+  {
+    BOOST_ASSERT(hasMtu());
+    return *m_mtu;
+  }
+
+  /** \brief set MTU (measured in bytes)
+   *
+   *  This value is capped at MAX_NDN_PACKET_SIZE, even if the MTU of the face is unlimited.
+   */
+  FaceStatus&
+  setMtu(uint64_t mtu);
+
+  FaceStatus&
+  unsetMtu();
+
   uint64_t
   getNInInterests() const
   {
@@ -195,6 +222,7 @@
   optional<time::milliseconds> m_expirationPeriod;
   optional<time::nanoseconds> m_baseCongestionMarkingInterval;
   optional<uint64_t> m_defaultCongestionThreshold;
+  optional<uint64_t> m_mtu;
   uint64_t m_nInInterests;
   uint64_t m_nInData;
   uint64_t m_nInNacks;
diff --git a/tests/unit-tests/mgmt/nfd/control-command.t.cpp b/tests/unit-tests/mgmt/nfd/control-command.t.cpp
index 3d826cc..4db3752 100644
--- a/tests/unit-tests/mgmt/nfd/control-command.t.cpp
+++ b/tests/unit-tests/mgmt/nfd/control-command.t.cpp
@@ -47,6 +47,7 @@
     .setFacePersistency(FACE_PERSISTENCY_PERMANENT)
     .setBaseCongestionMarkingInterval(100_ms)
     .setDefaultCongestionThreshold(10000)
+    .setMtu(8192)
     .setFlags(0x3)
     .setMask(0x1);
   BOOST_CHECK_NO_THROW(command.validateRequest(p1));
@@ -82,6 +83,7 @@
     .setFacePersistency(FACE_PERSISTENCY_PERMANENT)
     .setBaseCongestionMarkingInterval(500_ns)
     .setDefaultCongestionThreshold(12345)
+    .setMtu(2048)
     .setFlags(0x3);
   BOOST_CHECK_NO_THROW(command.validateResponse(p1));
 
@@ -161,6 +163,12 @@
   BOOST_CHECK_NO_THROW(command.validateRequest(p5));
   BOOST_CHECK_THROW(command.validateResponse(p5), ControlCommand::ArgumentError);
   BOOST_CHECK_EQUAL(p5.getFaceId(), 0);
+
+  ControlParameters p6;
+  p6.setFaceId(1)
+    .setMtu(1024);
+  BOOST_CHECK_THROW(command.validateRequest(p6), ControlCommand::ArgumentError);
+  BOOST_CHECK_THROW(command.validateResponse(p6), ControlCommand::ArgumentError);
 }
 
 BOOST_AUTO_TEST_CASE(FaceDestroy)
diff --git a/tests/unit-tests/mgmt/nfd/face-status.t.cpp b/tests/unit-tests/mgmt/nfd/face-status.t.cpp
index 3cae12a..0ed7aca 100644
--- a/tests/unit-tests/mgmt/nfd/face-status.t.cpp
+++ b/tests/unit-tests/mgmt/nfd/face-status.t.cpp
@@ -45,6 +45,7 @@
       .setExpirationPeriod(10_s)
       .setBaseCongestionMarkingInterval(5_ns)
       .setDefaultCongestionThreshold(7)
+      .setMtu(9)
       .setNInInterests(10)
       .setNInData(200)
       .setNInNacks(1)
@@ -67,17 +68,17 @@
   //   printf("0x%02x, ", *it);
   // }
   static const uint8_t expected[] = {
-    0x80, 0x67, 0x69, 0x01, 0x64, 0x72, 0x15, 0x74, 0x63, 0x70,
+    0x80, 0x6a, 0x69, 0x01, 0x64, 0x72, 0x15, 0x74, 0x63, 0x70,
     0x34, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32, 0x2e, 0x30, 0x2e,
     0x32, 0x2e, 0x31, 0x3a, 0x36, 0x33, 0x36, 0x33, 0x81, 0x16,
     0x74, 0x63, 0x70, 0x34, 0x3a, 0x2f, 0x2f, 0x31, 0x39, 0x32,
     0x2e, 0x30, 0x2e, 0x32, 0x2e, 0x32, 0x3a, 0x35, 0x35, 0x35,
     0x35, 0x35, 0x6d, 0x02, 0x27, 0x10, 0x84, 0x01, 0x01, 0x85,
     0x01, 0x01, 0x86, 0x01, 0x01, 0x87, 0x01, 0x05, 0x88, 0x01,
-    0x07, 0x90, 0x01, 0x0a, 0x91, 0x01, 0xc8, 0x97, 0x01, 0x01,
-    0x92, 0x02, 0x0b, 0xb8, 0x93, 0x01, 0x04, 0x98, 0x01, 0x02,
-    0x94, 0x04, 0x4f, 0x41, 0xe7, 0x7b, 0x95, 0x04, 0x3b, 0x8d,
-    0x37, 0x30, 0x6c, 0x01, 0x07,
+    0x07, 0x89, 0x01, 0x09, 0x90, 0x01, 0x0a, 0x91, 0x01, 0xc8,
+    0x97, 0x01, 0x01, 0x92, 0x02, 0x0b, 0xb8, 0x93, 0x01, 0x04,
+    0x98, 0x01, 0x02, 0x94, 0x04, 0x4f, 0x41, 0xe7, 0x7b, 0x95,
+    0x04, 0x3b, 0x8d, 0x37, 0x30, 0x6c, 0x01, 0x07,
   };
   BOOST_CHECK_EQUAL_COLLECTIONS(expected, expected + sizeof(expected),
                                 wire.begin(), wire.end());
@@ -127,6 +128,7 @@
                     "     LinkType: multi-access,\n"
                     "     BaseCongestionMarkingInterval: 5 nanoseconds,\n"
                     "     DefaultCongestionThreshold: 7 bytes,\n"
+                    "     Mtu: 9 bytes,\n"
                     "     Flags: 0x7,\n"
                     "     Counters: {Interests: {in: 10, out: 3000},\n"
                     "                Data: {in: 200, out: 4},\n"