PLDM : Change the dbus timeout to 5 seconds

Dbus timeout value for every dbus call in PLDM is set to 5 seconds
instead of default 25 seconds so that all dbus calls that expect a
reply exists after 5 seconds if there is no response from the client.
While BMC is still waiting for a response on any dbus calls it is
possible that host can timeout and return an error.
Another reason is that memory address from where BMC would read data
sent by host may get deallocated after 20 seconds and it is possible
for this to happen even before BMC has read the data. This has lead
to DMA issues.

Tested by adding a sleep for 10 seconds inside main function of
phospher-host-postd and executed get dbus property on snoopd
interface from pldm. PLDM dbus call timed out after 5 seconds.

Change-Id: I1aa1b4ad7fceff2224a3ccc3bcf6abd5705963bc
Signed-off-by: Varsha Kaverappa <vkaverap@in.ibm.com>
diff --git a/common/utils.cpp b/common/utils.cpp
index fca6985..03cfd9e 100644
--- a/common/utils.cpp
+++ b/common/utils.cpp
@@ -228,7 +228,9 @@
                                       mapperInterface, "GetObject");
     mapper.append(path, DbusInterfaceList({interface}));
 
-    auto mapperResponseMsg = bus.call(mapper);
+    auto mapperResponseMsg = bus.call(
+        mapper,
+        std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
     mapperResponseMsg.read(mapperResponse);
     return mapperResponse.begin()->first;
 }
@@ -241,7 +243,9 @@
     auto method = bus.new_method_call(mapperBusName, mapperPath,
                                       mapperInterface, "GetSubTree");
     method.append(searchPath, depth, ifaceList);
-    auto reply = bus.call(method);
+    auto reply = bus.call(
+        method,
+        std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
     GetSubTreeResponse response;
     reply.read(response);
     return response;
@@ -355,7 +359,9 @@
                                       "Get");
     method.append(dbusInterface, dbusProp);
     PropertyValue value{};
-    auto reply = bus.call(method);
+    auto reply = bus.call(
+        method,
+        std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
     reply.read(value);
     return value;
 }
diff --git a/common/utils.hpp b/common/utils.hpp
index f1357db..cc6db01 100644
--- a/common/utils.hpp
+++ b/common/utils.hpp
@@ -21,6 +21,9 @@
 #include <variant>
 #include <vector>
 
+using microsec = std::chrono::microseconds;
+using sec = std::chrono::seconds;
+
 namespace pldm
 {
 namespace utils
diff --git a/libpldmresponder/bios_config.cpp b/libpldmresponder/bios_config.cpp
index 69a98f4..1e74081 100644
--- a/libpldmresponder/bios_config.cpp
+++ b/libpldmresponder/bios_config.cpp
@@ -491,7 +491,9 @@
                                           "org.freedesktop.DBus.Properties",
                                           "Get");
         method.append(biosInterface, "BaseBIOSTable");
-        auto reply = bus.call(method);
+        auto reply = bus.call(
+            method,
+            std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
         std::variant<BaseBIOSTable> varBiosTable{};
         reply.read(varBiosTable);
         biosTable = std::get<BaseBIOSTable>(varBiosTable);
diff --git a/libpldmresponder/fru.cpp b/libpldmresponder/fru.cpp
index 7ccc4a9..f4311b8 100644
--- a/libpldmresponder/fru.cpp
+++ b/libpldmresponder/fru.cpp
@@ -37,7 +37,9 @@
         auto method = bus.new_method_call(
             std::get<0>(dbusInfo).c_str(), std::get<1>(dbusInfo).c_str(),
             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
-        auto reply = bus.call(method);
+        auto reply = bus.call(
+            method,
+            std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
         reply.read(objects);
     }
     catch (const std::exception& e)
@@ -143,7 +145,9 @@
                                           pldm::utils::dbusProperties, "Get");
         method.append("xyz.openbmc_project.Association", "endpoints");
         std::variant<std::vector<std::string>> paths;
-        auto reply = bus.call(method);
+        auto reply = bus.call(
+            method,
+            std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
         reply.read(paths);
         auto fwRunningVersion = std::get<std::vector<std::string>>(paths)[0];
         constexpr auto versionIntf = "xyz.openbmc_project.Software.Version";
diff --git a/meson.build b/meson.build
index 7a596ed..c846121 100644
--- a/meson.build
+++ b/meson.build
@@ -36,6 +36,7 @@
 conf_data.set('HEARTBEAT_TIMEOUT', get_option('heartbeat-timeout-seconds'))
 conf_data.set('TERMINUS_ID', get_option('terminus-id'))
 conf_data.set('TERMINUS_HANDLE',get_option('terminus-handle'))
+conf_data.set('DBUS_TIMEOUT', get_option('dbus-timeout-value'))
 add_project_arguments('-DLIBPLDMRESPONDER', language : ['c','cpp'])
 endif
 if get_option('softoff').enabled()
diff --git a/meson_options.txt b/meson_options.txt
index 72ec1ec..9cff3fd 100644
--- a/meson_options.txt
+++ b/meson_options.txt
@@ -15,6 +15,8 @@
 option('instance-id-expiration-interval', type: 'integer', min: 5, max: 6, description: 'Instance ID expiration interval in seconds', value: 5)
 # Default response-time-out set to 2 seconds to facilitate a minimum retry of the request of 2.
 option('response-time-out', type: 'integer', min: 300, max: 4800, description: 'The amount of time a requester has to wait for a response message in milliseconds', value: 2000)
+# Time taken to wait for the reply for any PLDM dbus call is set to 5 seconds. After 5 seconds the dbus method will exit.
+option('dbus-timeout-value', type: 'integer', min: 3, max: 10, description: 'The amount of time pldm waits to get a response for a dbus message before timing out', value: 5)
 
 option('heartbeat-timeout-seconds', type: 'integer', description: ' The amount of time host waits for BMC to respond to pings from host, as part of host-bmc surveillance', value: 120)
 
diff --git a/oem/ibm/libpldmresponder/file_io_type_dump.cpp b/oem/ibm/libpldmresponder/file_io_type_dump.cpp
index 2c6b02a..9005f66 100644
--- a/oem/ibm/libpldmresponder/file_io_type_dump.cpp
+++ b/oem/ibm/libpldmresponder/file_io_type_dump.cpp
@@ -69,7 +69,9 @@
             bus.new_method_call(DUMP_MANAGER_BUSNAME, DUMP_MANAGER_PATH,
                                 OBJECT_MANAGER_INTERFACE, "GetManagedObjects");
 
-        auto reply = bus.call(method);
+        auto reply = bus.call(
+            method,
+            std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
         reply.read(objects);
     }
     catch (const sdbusplus::exception_t& e)
@@ -285,7 +287,9 @@
                 auto method = bus.new_method_call(
                     "xyz.openbmc_project.Dump.Manager", path.c_str(),
                     "xyz.openbmc_project.Object.Delete", "Delete");
-                bus.call(method);
+                bus.call(method,
+                         std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT))
+                             .count());
             }
             catch (const std::exception& e)
             {
diff --git a/oem/ibm/libpldmresponder/file_io_type_pel.cpp b/oem/ibm/libpldmresponder/file_io_type_pel.cpp
index c329bac..ba94f4e 100644
--- a/oem/ibm/libpldmresponder/file_io_type_pel.cpp
+++ b/oem/ibm/libpldmresponder/file_io_type_pel.cpp
@@ -108,7 +108,9 @@
         auto method = bus.new_method_call(service.c_str(), logObjPath,
                                           logInterface, "GetPEL");
         method.append(fileHandle);
-        auto reply = bus.call(method);
+        auto reply = bus.call(
+            method,
+            std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
         sdbusplus::message::unix_fd fd{};
         reply.read(fd);
         auto rc = transferFileData(fd, true, offset, length, address);
@@ -139,7 +141,9 @@
         auto method = bus.new_method_call(service.c_str(), logObjPath,
                                           logInterface, "GetPEL");
         method.append(fileHandle);
-        auto reply = bus.call(method);
+        auto reply = bus.call(
+            method,
+            std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
         sdbusplus::message::unix_fd fd{};
         reply.read(fd);
 
diff --git a/oem/ibm/libpldmresponder/inband_code_update.cpp b/oem/ibm/libpldmresponder/inband_code_update.cpp
index c69c1bc..1e87824 100644
--- a/oem/ibm/libpldmresponder/inband_code_update.cpp
+++ b/oem/ibm/libpldmresponder/inband_code_update.cpp
@@ -164,7 +164,9 @@
         method.append("xyz.openbmc_project.Association", "endpoints");
         std::variant<std::vector<std::string>> paths;
 
-        auto reply = bus.call(method);
+        auto reply = bus.call(
+            method,
+            std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
         reply.read(paths);
 
         runningVersion = std::get<std::vector<std::string>>(paths)[0];
@@ -173,7 +175,9 @@
                                            propIntf, "Get");
         method1.append("xyz.openbmc_project.Association", "endpoints");
 
-        auto reply1 = bus.call(method1);
+        auto reply1 = bus.call(
+            method1,
+            std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
         reply1.read(paths);
         for (const auto& path : std::get<std::vector<std::string>>(paths))
         {
diff --git a/pldmtool/pldm_cmd_helper.cpp b/pldmtool/pldm_cmd_helper.cpp
index 3bf4295..c8ce8df 100644
--- a/pldmtool/pldm_cmd_helper.cpp
+++ b/pldmtool/pldm_cmd_helper.cpp
@@ -1,3 +1,5 @@
+#include "config.h"
+
 #include "pldm_cmd_helper.hpp"
 
 #include "xyz/openbmc_project/Common/error.hpp"
@@ -14,7 +16,6 @@
 
 namespace pldmtool
 {
-
 namespace helper
 {
 /*
@@ -144,7 +145,9 @@
         auto method = bus.new_method_call(service.c_str(), pldmObjPath,
                                           pldmRequester, "GetInstanceId");
         method.append(mctp_eid);
-        auto reply = bus.call(method);
+        auto reply = bus.call(
+            method,
+            std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
         reply.read(instanceId);
     }
     catch (const std::exception& e)
diff --git a/requester/mctp_endpoint_discovery.cpp b/requester/mctp_endpoint_discovery.cpp
index f94bea6..f0da39a 100644
--- a/requester/mctp_endpoint_discovery.cpp
+++ b/requester/mctp_endpoint_discovery.cpp
@@ -13,7 +13,6 @@
 
 namespace pldm
 {
-
 MctpDiscovery::MctpDiscovery(sdbusplus::bus_t& bus,
                              fw_update::Manager* fwManager) :
     bus(bus),
@@ -30,7 +29,9 @@
         auto method = bus.new_method_call(
             "xyz.openbmc_project.MCTP.Control", "/xyz/openbmc_project/mctp",
             "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
-        auto reply = bus.call(method);
+        auto reply = bus.call(
+            method,
+            std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
         reply.read(objects);
     }
     catch (const std::exception& e)
diff --git a/softoff/softoff.cpp b/softoff/softoff.cpp
index 80abbf7..b5d9e8c 100644
--- a/softoff/softoff.cpp
+++ b/softoff/softoff.cpp
@@ -145,7 +145,9 @@
         VMMMethod.append(TID, entityType,
                          (uint16_t)PLDM_STATE_SET_SW_TERMINATION_STATUS);
 
-        auto VMMResponseMsg = bus.call(VMMMethod);
+        auto VMMResponseMsg = bus.call(
+            VMMMethod,
+            std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
 
         VMMResponseMsg.read(VMMResponse);
         if (VMMResponse.size() != 0)
@@ -187,7 +189,9 @@
         sysFwMethod.append(TID, entityType,
                            (uint16_t)PLDM_STATE_SET_SW_TERMINATION_STATUS);
 
-        auto sysFwResponseMsg = bus.call(sysFwMethod);
+        auto sysFwResponseMsg = bus.call(
+            sysFwMethod,
+            std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
 
         sysFwResponseMsg.read(sysFwResponse);
 
@@ -236,7 +240,9 @@
         method.append(TID, entityType,
                       (uint16_t)PLDM_STATE_SET_SW_TERMINATION_STATUS);
 
-        auto ResponseMsg = bus.call(method);
+        auto ResponseMsg = bus.call(
+            method,
+            std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
 
         ResponseMsg.read(Response);
 
@@ -306,7 +312,9 @@
             "xyz.openbmc_project.PLDM.Requester", "GetInstanceId");
         method.append(mctpEID);
 
-        auto ResponseMsg = bus.call(method);
+        auto ResponseMsg = bus.call(
+            method,
+            std::chrono::duration_cast<microsec>(sec(DBUS_TIMEOUT)).count());
 
         ResponseMsg.read(instanceID);
     }