blob: fc2981ea4dddbdc9084a269d12cdf874d91f585a [file] [log] [blame]
/*
// Copyright (c) 2019 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/
#pragma once
#ifndef BMCWEB_ENABLE_REDFISH_ONE_CHASSIS
#include "async_resp.hpp"
#include "bmcweb_config.h"
#include "chassis_utils.hpp"
#include "dbus_utility.hpp"
#include "dbus_utils.hpp"
#include "error_messages.hpp"
#include "managed_store.hpp"
#include "managed_store_types.hpp"
#include <boost/system/error_code.hpp>
#include <sdbusplus/asio/property.hpp>
#include <array>
#include <charconv>
#include <cstdint>
#include <string_view>
#ifdef UNIT_TEST_BUILD
#include "test/g3/mock_managed_store.hpp" // NOLINT
#endif
namespace redfish
{
enum NetworkProtocolUnitStructFields : std::uint8_t
{
NET_PROTO_UNIT_NAME,
NET_PROTO_UNIT_DESC,
NET_PROTO_UNIT_LOAD_STATE,
NET_PROTO_UNIT_ACTIVE_STATE,
NET_PROTO_UNIT_SUB_STATE,
NET_PROTO_UNIT_DEVICE,
NET_PROTO_UNIT_OBJ_PATH,
NET_PROTO_UNIT_ALWAYS_0,
NET_PROTO_UNIT_ALWAYS_EMPTY,
NET_PROTO_UNIT_ALWAYS_ROOT_PATH
};
enum NetworkProtocolListenResponseElements : std::uint8_t
{
NET_PROTO_LISTEN_TYPE,
NET_PROTO_LISTEN_STREAM
};
template <typename CallbackFunc>
void getMainChassisId(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
CallbackFunc&& callback)
{
if (strlen(bmcwebMainChassisId) != 0) {
callback(bmcwebMainChassisId, asyncResp);
return;
}
// Find managed chassis
constexpr std::array<std::string_view, 2> interfaces = {
"xyz.openbmc_project.Inventory.Item.Board",
"xyz.openbmc_project.Inventory.Item.Chassis"};
managedStore::ManagedObjectStoreContext requestContext(asyncResp);
managedStore::GetManagedObjectStore()->getSubTree(
"/xyz/openbmc_project/inventory", 0, interfaces, requestContext,
[callback,
asyncResp, requestContext](const boost::system::error_code& ec,
const dbus::utility::MapperGetSubTreeResponse& subtree) {
if (ec)
{
BMCWEB_LOG_ERROR << ec;
return;
}
if (subtree.empty())
{
BMCWEB_LOG_DEBUG << "Can't find chassis!";
return;
}
// We pick the very first chassis object path in subtree and start
// walking up in the topology to find root chassis.
std::string chassisObjectPath = subtree[0].first;
chassis_utils::getRootChassisPath(
{}, asyncResp, {chassisObjectPath},
[callback, asyncResp](const std::string& objectPath) {
std::size_t idPos = objectPath.rfind('/');
if (idPos == std::string::npos || (idPos + 1) >= objectPath.size())
{
messages::internalError(asyncResp->res);
BMCWEB_LOG_DEBUG << "Can't parse chassis ID!";
return;
}
std::string chassisId = objectPath.substr(idPos + 1);
BMCWEB_LOG_DEBUG << "chassisId = " << chassisId;
callback(chassisId, asyncResp);
});
});
}
template <typename CallbackFunc>
void getBmcChassisId(const std::shared_ptr<bmcweb::AsyncResp>& asyncResp,
CallbackFunc&& callback)
{
// Find BMC chassis
constexpr std::array<std::string_view, 1> interfaces = {
"xyz.openbmc_project.Configuration.Bmc"};
managedStore::ManagedObjectStoreContext context(asyncResp);
dbus_utils::getSubTree(
"/xyz/openbmc_project/inventory", 0, interfaces, context,
[callback,
asyncResp](const boost::system::error_code& ec,
const dbus::utility::MapperGetSubTreeResponse& subtree) {
if (ec)
{
BMCWEB_LOG_ERROR << ec;
return;
}
if (subtree.empty())
{
BMCWEB_LOG_ERROR << "Can't find chassis!";
return;
}
if (subtree.size() > 1)
{
BMCWEB_LOG_ERROR << "Unexpectedly find multiple chassis!";
return;
}
std::filesystem::path chassisObjectPath(subtree[0].first);
auto parentPath = chassisObjectPath.parent_path();
if (parentPath.empty())
{
messages::internalError(asyncResp->res);
BMCWEB_LOG_ERROR << "Can't parse chassis ID!";
return;
}
std::string chassisId = parentPath.filename().string();
BMCWEB_LOG_DEBUG << "chassisId = " << chassisId;
callback(chassisId, asyncResp);
});
}
template <typename CallbackFunc>
void getPortStatusAndPath(
std::span<const std::pair<std::string_view, std::string_view>>
protocolToDBus,
const managedStore::ManagedObjectStoreContext& context,
CallbackFunc&& callback)
{
managedStore::GetManagedObjectStore()->listUnits(
context,
[protocolToDBus, callback{std::forward<CallbackFunc>(callback)}](
const boost::system::error_code& ec,
const dbus::utility::SystemdListUnits& r) {
std::vector<std::tuple<std::string, std::string, bool>> socketData;
if (ec)
{
BMCWEB_LOG_ERROR << ec;
// return error code
callback(ec, socketData);
return;
}
// save all service output into vector
for (const dbus::utility::SystemdUnitStruct& unit : r)
{
// Only traverse through <xyz>.socket units
const std::string& unitName = std::get<NET_PROTO_UNIT_NAME>(unit);
// find "." into unitsName
size_t lastCharPos = unitName.rfind('.');
if (lastCharPos == std::string::npos)
{
continue;
}
// is unitsName end with ".socket"
std::string unitNameEnd = unitName.substr(lastCharPos);
if (unitNameEnd != ".socket")
{
continue;
}
// find "@" into unitsName
if (size_t atCharPos = unitName.rfind('@');
atCharPos != std::string::npos)
{
lastCharPos = atCharPos;
}
// unitsName without "@eth(x).socket", only <xyz>
// unitsName without ".socket", only <xyz>
std::string unitNameStr = unitName.substr(0, lastCharPos);
for (const auto& kv : protocolToDBus)
{
// We are interested in services, which starts with
// mapped service name
if (unitNameStr != kv.second)
{
continue;
}
const std::string& socketPath =
std::get<NET_PROTO_UNIT_OBJ_PATH>(unit);
const std::string& unitState =
std::get<NET_PROTO_UNIT_SUB_STATE>(unit);
bool isProtocolEnabled =
((unitState == "running") || (unitState == "listening"));
socketData.emplace_back(socketPath, std::string(kv.first),
isProtocolEnabled);
// We found service, return from inner loop.
break;
}
}
callback(ec, socketData);
});
}
template <typename CallbackFunc>
void getPortNumber(
const std::string& socketPath,
const managedStore::ManagedObjectStoreContext& requestContext,
CallbackFunc&& callback)
{
dbus_utils::getProperty<std::vector<std::tuple<std::string, std::string>>>(
"org.freedesktop.systemd1", socketPath,
"org.freedesktop.systemd1.Socket", "Listen", requestContext,
[callback{std::forward<CallbackFunc>(callback)}](
const boost::system::error_code& ec,
const std::vector<std::tuple<std::string, std::string>>& resp) {
if (ec)
{
BMCWEB_LOG_ERROR << ec;
callback(ec, 0);
return;
}
if (resp.empty())
{
// Network Protocol Listen Response Elements is empty
boost::system::error_code ec1 =
boost::system::errc::make_error_code(
boost::system::errc::bad_message);
// return error code
callback(ec1, 0);
BMCWEB_LOG_ERROR << ec1;
return;
}
const std::string& listenStream =
std::get<NET_PROTO_LISTEN_STREAM>(resp[0]);
const char* pa = &listenStream[listenStream.rfind(':') + 1];
int port{0};
if (auto [p, ec2] = std::from_chars(pa, nullptr, port);
ec2 != std::errc())
{
// there is only two possibility invalid_argument and
// result_out_of_range
boost::system::error_code ec3 =
boost::system::errc::make_error_code(
boost::system::errc::invalid_argument);
if (ec2 == std::errc::result_out_of_range)
{
ec3 = boost::system::errc::make_error_code(
boost::system::errc::result_out_of_range);
}
// return error code
callback(ec3, 0);
BMCWEB_LOG_ERROR << ec3;
}
callback(ec, port);
});
}
} // namespace redfish
#endif