Skip to content

Commit

Permalink
Reapply "openpower-pels: Move to libpldm pldm_transport APIs" (#74)
Browse files Browse the repository at this point in the history
* Reapply "openpower-pels: Move to libpldm pldm_transport APIs"

This reverts commit eeb34e5.

* Fix formatting

Change-Id: Ia3a03cc59cbb03e4fa71995a52cd5fa2bb7ee89c
Signed-off-by: Eddie James <[email protected]>

---------

Signed-off-by: Eddie James <[email protected]>
Co-authored-by: Eddie James <[email protected]>
  • Loading branch information
eddiejames and Eddie James authored Oct 11, 2024
1 parent 149208c commit 1af06bb
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 46 deletions.
6 changes: 6 additions & 0 deletions config/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ conf_data = configuration_data()
conf_data.set('error_cap', get_option('error_cap'))
conf_data.set('error_info_cap', get_option('error_info_cap'))
conf_data.set('rsyslog_server_conf', get_option('rsyslog_server_conf'))

cxx = meson.get_compiler('cpp')
if cxx.has_header('poll.h')
add_project_arguments('-DPLDM_HAS_POLL=1', language: 'cpp')
endif

conf_h_dep = declare_dependency(
include_directories: include_directories('.'),
sources: configure_file(
Expand Down
6 changes: 4 additions & 2 deletions extensions/openpower-pels/host_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "data_interface.hpp"

#include <libpldm/transport.h>
#include <stdint.h>

#include <phosphor-logging/lg2.hpp>
Expand Down Expand Up @@ -195,9 +196,10 @@ class HostInterface
* invoked from.
* @param[in] fd - The file descriptor being used
* @param[in] revents - The event status bits
* @param[in] transport - The transport data pointer
*/
virtual void receive(sdeventplus::source::IO& io, int fd,
uint32_t revents) = 0;
virtual void receive(sdeventplus::source::IO& io, int fd, uint32_t revents,
pldm_transport* transport) = 0;

/**
* @brief An optional function to call on a successful command response.
Expand Down
106 changes: 78 additions & 28 deletions extensions/openpower-pels/pldm_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

#include <libpldm/base.h>
#include <libpldm/oem/ibm/file_io.h>
#include <libpldm/transport.h>
#include <libpldm/transport/mctp-demux.h>
#include <poll.h>
#include <unistd.h>

#include <phosphor-logging/lg2.hpp>
Expand All @@ -28,9 +31,11 @@ namespace openpower::pels

using namespace sdeventplus;
using namespace sdeventplus::source;
using TerminusID = uint8_t;

constexpr auto eidPath = "/usr/share/pldm/host_eid";
constexpr mctp_eid_t defaultEIDValue = 9;
constexpr TerminusID tid = defaultEIDValue;

constexpr uint16_t pelFileType = 0;

Expand All @@ -43,11 +48,10 @@ PLDMInterface::~PLDMInterface()

void PLDMInterface::closeFD()
{
if (_fd >= 0)
{
close(_fd);
_fd = -1;
}
pldm_transport_mctp_demux_destroy(mctpDemux);
mctpDemux = nullptr;
_fd = -1;
pldmTransport = nullptr;
}

void PLDMInterface::readEID()
Expand Down Expand Up @@ -76,14 +80,54 @@ void PLDMInterface::readEID()

void PLDMInterface::open()
{
_fd = pldm_open();
if (pldmTransport)
{
lg2::error("open: pldmTransport already setup!");
throw std::runtime_error{"open failed"};
}

_fd = openMctpDemuxTransport();
if (_fd < 0)
{
auto e = errno;
lg2::error("pldm_open failed. errno = {ERRNO}, rc = {RC}", "ERRNO", e,
"RC", _fd);
throw std::runtime_error{"pldm_open failed"};
lg2::error("Transport open failed. errno = {ERRNO}, rc = {RC}", "ERRNO",
e, "RC", _fd);
throw std::runtime_error{"Transport open failed"};
}
}

int PLDMInterface::openMctpDemuxTransport()
{
int rc = pldm_transport_mctp_demux_init(&mctpDemux);
if (rc)
{
lg2::error(
"openMctpDemuxTransport: Failed to init MCTP demux transport. rc = {RC}",
"RC", rc);
return rc;
}

rc = pldm_transport_mctp_demux_map_tid(mctpDemux, tid, tid);
if (rc)
{
lg2::error(
"openMctpDemuxTransport: Failed to setup tid to eid mapping. rc = {RC}",
"RC", rc);
cleanupCmd();
return rc;
}
pldmTransport = pldm_transport_mctp_demux_core(mctpDemux);

struct pollfd pollfd;
rc = pldm_transport_mctp_demux_init_pollfd(pldmTransport, &pollfd);
if (rc)
{
lg2::error("openMctpDemuxTransport: Failed to get pollfd. rc = {RC}",
"RC", rc);
cleanupCmd();
return rc;
}
return pollfd.fd;
}

void PLDMInterface::startCommand()
Expand Down Expand Up @@ -185,7 +229,7 @@ void PLDMInterface::registerReceiveCallback()
_event, _fd, EPOLLIN,
std::bind(std::mem_fn(&PLDMInterface::receive), this,
std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3));
std::placeholders::_3, pldmTransport));
}

void PLDMInterface::doSend()
Expand All @@ -203,45 +247,51 @@ void PLDMInterface::doSend()
lg2::error("encode_new_file_req failed, rc = {RC}", "RC", rc);
throw std::runtime_error{"encode_new_file_req failed"};
}

rc = pldm_send(_eid, _fd, requestMsg.data(), requestMsg.size());
pldm_tid_t pldmTID = static_cast<pldm_tid_t>(_eid);
rc = pldm_transport_send_msg(pldmTransport, pldmTID, requestMsg.data(),
requestMsg.size());
if (rc < 0)
{
auto e = errno;
lg2::error("pldm_send failed, rc = {RC}, errno = {ERRNO}", "RC", rc,
"ERRNO", e);
throw std::runtime_error{"pldm_send failed"};
lg2::error("pldm_transport_send_msg failed, rc = {RC}, errno = {ERRNO}",
"RC", rc, "ERRNO", e);
throw std::runtime_error{"pldm_transport_send_msg failed"};
}
}

void PLDMInterface::receive(IO& /*io*/, int fd, uint32_t revents)
void PLDMInterface::receive(IO& /*io*/, int /*fd*/, uint32_t revents,
pldm_transport* transport)

{
if (!(revents & EPOLLIN))
{
return;
}

uint8_t* responseMsg = nullptr;
void* responseMsg = nullptr;
size_t responseSize = 0;
ResponseStatus status = ResponseStatus::success;

auto rc = pldm_recv(_eid, fd, *_instanceID, &responseMsg, &responseSize);
if (rc < 0)
pldm_tid_t pldmTID;
auto rc = pldm_transport_recv_msg(transport, &pldmTID, &responseMsg,
&responseSize);
if (pldmTID != _eid)
{
if (rc == PLDM_REQUESTER_INSTANCE_ID_MISMATCH)
{
// We got a response to someone else's message. Ignore it.
return;
}
else if (rc == PLDM_REQUESTER_NOT_RESP_MSG)
// We got a response to someone else's message. Ignore it.
return;
}
if (rc)
{
if (rc == PLDM_REQUESTER_NOT_RESP_MSG)
{
// Due to the MCTP loopback, we may get notified of the message
// we just sent.
return;
}

auto e = errno;
lg2::error("pldm_recv failed, rc = {RC}, errno = {ERRNO}", "RC",
lg2::error("pldm_transport_recv_msg failed, rc = {RC}, errno = {ERRNO}",
"RC",
static_cast<std::underlying_type_t<pldm_requester_rc_t>>(rc),
"ERRNO", e);
status = ResponseStatus::failure;
Expand All @@ -259,8 +309,8 @@ void PLDMInterface::receive(IO& /*io*/, int fd, uint32_t revents)
uint8_t completionCode = 0;
auto response = reinterpret_cast<pldm_msg*>(responseMsg);

auto decodeRC = decode_new_file_resp(response, PLDM_NEW_FILE_RESP_BYTES,
&completionCode);
auto decodeRC =
decode_new_file_resp(response, responseSize, &completionCode);
if (decodeRC < 0)
{
lg2::error("decode_new_file_resp failed, rc = {RC}", "RC",
Expand Down
19 changes: 17 additions & 2 deletions extensions/openpower-pels/pldm_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <libpldm/instance-id.h>
#include <libpldm/pldm.h>
#include <libpldm/transport.h>
#include <libpldm/transport/mctp-demux.h>

#include <sdeventplus/clock.hpp>
#include <sdeventplus/source/io.hpp>
Expand Down Expand Up @@ -93,9 +95,10 @@ class PLDMInterface : public HostInterface
* @param[in] io - The event source object
* @param[in] fd - The FD used
* @param[in] revents - The event bits
* @param[in] transport - The transport data pointer
*/
void receive(sdeventplus::source::IO& io, int fd,
uint32_t revents) override;
void receive(sdeventplus::source::IO& io, int fd, uint32_t revents,
pldm_transport* transport) override;

/**
* @brief Function called when the receive timer expires.
Expand Down Expand Up @@ -123,6 +126,11 @@ class PLDMInterface : public HostInterface
*/
void open();

/** @brief Opens the MCTP socket for sending and receiving messages.
*
*/
int openMctpDemuxTransport();

/**
* @brief Encodes and sends the PLDM 'new file available' cmd
*/
Expand Down Expand Up @@ -200,6 +208,13 @@ class PLDMInterface : public HostInterface
* @brief The size of the PEL to notify the host of.
*/
uint32_t _pelSize = 0;

/**
* @brief pldm transport instance.
*/
pldm_transport* pldmTransport = nullptr;

pldm_transport_mctp_demux* mctpDemux = nullptr;
};

} // namespace openpower::pels
2 changes: 1 addition & 1 deletion phosphor-auditlog/alog_manager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ALManager : public ALObject
* @param[in] path - Path to attach at.
*/
ALManager(sdbusplus::bus_t& bus, const std::string& path) :
ALObject(bus, path.c_str()){};
ALObject(bus, path.c_str()) {};

/**
* @brief Parses all audit log events into JSON format.
Expand Down
12 changes: 6 additions & 6 deletions phosphor-auditlog/alog_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ bool ALParser::formatMsgReg(nlohmann::json& parsedEntry)
throw sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure();
}
parsedEntry["EventTimestamp"] = fullTimestamp->sec;
parsedEntry["ID"] = std::format("{}.{}:{}", fullTimestamp->sec,
fullTimestamp->milli,
fullTimestamp->serial);
parsedEntry["ID"] =
std::format("{}.{}:{}", fullTimestamp->sec, fullTimestamp->milli,
fullTimestamp->serial);

/* Fill varied args fields based on record type */
int recType = auparse_get_type(au);
Expand Down Expand Up @@ -259,9 +259,9 @@ bool ALParser::formatGeneral(nlohmann::json& parsedEntry)
throw sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure();
}
parsedEntry["EventTimestamp"] = fullTimestamp->sec;
parsedEntry["ID"] = std::format("{}.{}:{}", fullTimestamp->sec,
fullTimestamp->milli,
fullTimestamp->serial);
parsedEntry["ID"] =
std::format("{}.{}:{}", fullTimestamp->sec, fullTimestamp->milli,
fullTimestamp->serial);

auto recMsg = auparse_get_record_text(au);
parsedEntry["Event"] = recMsg;
Expand Down
4 changes: 2 additions & 2 deletions phosphor-auditlog/alog_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class ALParseFile
*/
ALParseFile()
{
std::string tempFile = std::filesystem::temp_directory_path() /
"auditLogJson-XXXXXX";
std::string tempFile =
std::filesystem::temp_directory_path() / "auditLogJson-XXXXXX";

lg2::debug("Constructing ALParseFile template={NAME}", "NAME",
tempFile);
Expand Down
10 changes: 5 additions & 5 deletions test/openpower-pels/mocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,10 @@ class MockHostInterface : public HostInterface
int fd = open(_fifo.c_str(), O_NONBLOCK | O_RDWR);
EXPECT_TRUE(fd >= 0) << "Unable to open FIFO";

auto callback = [this](sdeventplus::source::IO& source, int fd,
uint32_t events) {
this->receive(source, fd, events);
};
auto callback =
[this](sdeventplus::source::IO& source, int fd, uint32_t events) {
this->receive(source, fd, events, nullptr);
};

try
{
Expand Down Expand Up @@ -248,7 +248,7 @@ class MockHostInterface : public HostInterface
* @param[in] events - The event bits
*/
void receive(sdeventplus::source::IO& /*source*/, int /*fd*/,
uint32_t events) override
uint32_t events, pldm_transport* /*transport*/) override
{
if (!(events & EPOLLIN))
{
Expand Down

0 comments on commit 1af06bb

Please sign in to comment.