Cache system GUID in netipmid

GUID is used in the IPMI session setup steps RAKP12 and RAKP34.
The GUID is read from the DBUS property. It is observed that
when the host is booting, reading the GUID takes a significant
delay and the session setup fails with ipmitool. Since GUID doesn't
change for a machine, it is safe to cache GUID in the netipmid and
speed up the session setup.

Resolves openbmc/openbmc#1812
Resolves openbmc/openbmc#2245
Resolves openbmc/openbmc#2246

Change-Id: I78c993b3e5ef8b6764457c8fdb3ecb985b965c0c
Signed-off-by: Tom Joseph <tomjoseph@in.ibm.com>
diff --git a/command/guid.cpp b/command/guid.cpp
index 556484d..0943846 100644
--- a/command/guid.cpp
+++ b/command/guid.cpp
@@ -7,21 +7,28 @@
 #include <host-ipmid/ipmid-api.h>
 #include <mapper.h>
 
+namespace cache
+{
+
+command::Guid guid;
+
+} // namespace cache
+
 namespace command
 {
 
-std::array<uint8_t, BMC_GUID_LEN> getSystemGUID()
+std::unique_ptr<sdbusplus::bus::match_t> matchPtr(nullptr);
+
+static constexpr auto guidObjPath = "/org/openbmc/control/chassis0";
+static constexpr auto propInterface = "org.freedesktop.DBus.Properties";
+
+Guid getSystemGUID()
 {
     // Canned System GUID for QEMU where the Chassis DBUS object is not
     // populated
-    std::array<uint8_t, BMC_GUID_LEN> guid = { 0x01, 0x02, 0x03, 0x04,
-                                               0x05, 0x06, 0x07, 0x08,
-                                               0x09, 0x0A, 0x0B, 0x0C,
-                                               0x0D, 0x0E, 0x0F, 0x10
-                                             };
+    Guid guid = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
+                  0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10 };
 
-    constexpr auto objname = "/org/openbmc/control/chassis0";
-    constexpr auto interface = "org.freedesktop.DBus.Properties";
     constexpr auto chassisIntf = "org.openbmc.control.Chassis";
 
     sd_bus_message* reply = nullptr;
@@ -33,16 +40,16 @@
 
     do
     {
-        rc = mapper_get_service(bus, objname, &busname);
+        rc = mapper_get_service(bus, guidObjPath, &busname);
         if (rc < 0)
         {
-            std::cerr << "Failed to get " << objname << " bus name: "
+            std::cerr << "Failed to get " << guidObjPath << " bus name: "
                       << strerror(-rc) << "\n";
             break;
         }
 
-        rc = sd_bus_call_method(bus, busname, objname, interface, "Get", &error,
-                                &reply, "ss", chassisIntf, "uuid");
+        rc = sd_bus_call_method(bus, busname, guidObjPath, propInterface, "Get",
+                                &error, &reply, "ss", chassisIntf, "uuid");
         if (rc < 0)
         {
             std::cerr << "Failed to call Get Method:" << strerror(-rc) << "\n";
@@ -76,4 +83,21 @@
     return guid;
 }
 
+void registerGUIDChangeCallback()
+{
+    if(matchPtr == nullptr)
+    {
+        using namespace sdbusplus::bus::match::rules;
+        sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
+
+        matchPtr = std::make_unique<sdbusplus::bus::match_t>(
+            bus,
+            path_namespace(guidObjPath) +
+            type::signal() +
+            member("PropertiesChanged") +
+            interface(propInterface),
+            [](sdbusplus::message::message&){cache::guid = getSystemGUID();});
+    }
+}
+
 } // namespace command
diff --git a/command/guid.hpp b/command/guid.hpp
index 622b330..5941ef8 100644
--- a/command/guid.hpp
+++ b/command/guid.hpp
@@ -2,7 +2,7 @@
 
 #include <cstddef>
 #include <vector>
-
+#include <sdbusplus/bus/match.hpp>
 #include "comm_module.hpp"
 
 namespace command
@@ -10,12 +10,26 @@
 
 constexpr size_t BMC_GUID_LEN = 16;
 
+using Guid = std::array<uint8_t, BMC_GUID_LEN>;
+
 /**
  * @brief Get System GUID
  *
  * @return If UUID is successfully read from the Chassis DBUS object, then the
  *         GUID is returned, else a canned GUID is returned
  */
-std::array<uint8_t, BMC_GUID_LEN> getSystemGUID();
+Guid getSystemGUID();
+
+/**
+ *  @brief Register the callback to update the cache when the GUID changes
+ */
+void registerGUIDChangeCallback();
 
 } // namespace command
+
+namespace cache
+{
+
+extern command::Guid guid;
+
+} //namespace cache
diff --git a/command/rakp12.cpp b/command/rakp12.cpp
index 19dc160..e5ea3a7 100644
--- a/command/rakp12.cpp
+++ b/command/rakp12.cpp
@@ -113,8 +113,7 @@
     std::advance(iter, cipher::rakp_auth::BMC_RANDOM_NUMBER_LEN);
 
     // Managed System GUID
-    auto guid = getSystemGUID();
-    std::copy_n(guid.data(), guid.size(), iter);
+    std::copy_n(cache::guid.data(), cache::guid.size(), iter);
     std::advance(iter, BMC_GUID_LEN);
 
     // Requested Privilege Level
@@ -144,7 +143,9 @@
               response->managed_system_random_number);
 
     // Copy System GUID to the Response
-    std::copy_n(guid.data(), guid.size(), response->managed_system_guid);
+    std::copy_n(cache::guid.data(),
+                cache::guid.size(),
+                response->managed_system_guid);
 
     // Insert the HMAC output into the payload
     outPayload.insert(outPayload.end(), output.begin(), output.end());
diff --git a/command/rakp34.cpp b/command/rakp34.cpp
index 95b8af7..8c95e95 100644
--- a/command/rakp34.cpp
+++ b/command/rakp34.cpp
@@ -233,8 +233,7 @@
     std::advance(iter, sizeof(bmcSessionID));
 
     // Managed System GUID
-    auto guid = getSystemGUID();
-    std::copy_n(guid.data(), guid.size(), iter);
+    std::copy_n(cache::guid.data(), cache::guid.size(), iter);
 
     // Integrity Check Value
     auto icv = authAlgo->generateICV(input);
diff --git a/main.cpp b/main.cpp
index 842f098..ef4a349 100644
--- a/main.cpp
+++ b/main.cpp
@@ -12,6 +12,7 @@
 #include <systemd/sd-event.h>
 
 #include <host-ipmid/ipmid-api.h>
+#include "command/guid.hpp"
 #include "comm_module.hpp"
 #include "command_table.hpp"
 #include "message.hpp"
@@ -66,6 +67,11 @@
         goto finish;
     }
 
+    // Register callback to update cache for a GUID change and cache the GUID
+    command::registerGUIDChangeCallback();
+    cache::guid = command::getSystemGUID();
+
+
     // Register all the IPMI provider libraries applicable for net-ipmid
     provider::registerCallbackHandlers(NET_IPMID_LIB_PATH);
 
diff --git a/main.hpp b/main.hpp
index 2b5f509..954ebae 100644
--- a/main.hpp
+++ b/main.hpp
@@ -3,6 +3,7 @@
 #include <tuple>
 
 #include <command_table.hpp>
+#include "command/guid.hpp"
 #include <sessions_manager.hpp>
 #include "sol/sol_manager.hpp"
 #include "sd_event_loop.hpp"