Skip to content

Commit

Permalink
Fix compilation issues (openbmc#55)
Browse files Browse the repository at this point in the history
* Fix compilation issue

This PR is to fix the compilation issue caused by not
declaring a variable.

Reference:
[1] https://sys-openbmc-dev-jenkins.swg-devops.com/job/OpenBMC-Dev/job/build/job/github-private/job/openbmc-build-matrix-x86/label=x86-docker-builder,target=p10bmc/112/console

Signed-off-by: Asmitha Karunanithi <[email protected]>

* Fix compilation issues

This PR fixes compilation issues introduced by the following
PRs:
[1] ibm-openbmc#51

Signed-off-by: Asmitha Karunanithi <[email protected]>

Signed-off-by: Asmitha Karunanithi <[email protected]>
  • Loading branch information
asmithakarun authored and rfrandse committed Feb 16, 2023
1 parent 2e19d0b commit 63af40a
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 47 deletions.
60 changes: 42 additions & 18 deletions src/ibm/hypervisor-network-mgr-src/hyp_ethernet_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,33 +495,57 @@ ObjectPath HypEthInterface::ip(HypIP::Protocol protType, std::string ipaddress,

HypIP::AddressOrigin origin = HypIP::AddressOrigin::Static;

if (!isValidIP(AF_INET, ipaddress) && !isValidIP(AF_INET6, ipaddress))
InAddrAny addr;
try
{
log<level::ERR>("Not a valid IP address"),
entry("ADDRESS=%s", ipaddress.c_str());
elog<InvalidArgument>(Argument::ARGUMENT_NAME("Address"),
Argument::ARGUMENT_VALUE(ipaddress.c_str()));
return sdbusplus::message::details::string_path_wrapper();
switch (protType)
{
case HypIP::Protocol::IPv4:
addr = ToAddr<in_addr>{}(ipaddress);
break;
case HypIP::Protocol::IPv6:
addr = ToAddr<in6_addr>{}(ipaddress);
break;
default:
throw std::logic_error("Exhausted protocols");
}
}

if (!isValidIP(AF_INET, gateway) && !isValidIP(AF_INET6, gateway))
catch (const std::exception& e)
{
log<level::ERR>("Not a valid gateway"),
entry("ADDRESS=%s", gateway.c_str());
elog<InvalidArgument>(Argument::ARGUMENT_NAME("Gateway"),
auto msg = fmt::format("Invalid IP `{}`: {}\n", ipaddress, e.what());
log<level::ERR>(msg.c_str(), entry("ADDRESS=%s", ipaddress.c_str()));
elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipaddress"),
Argument::ARGUMENT_VALUE(ipaddress.c_str()));
return sdbusplus::message::details::string_path_wrapper();
}

if (!isValidPrefix(AF_INET, prefixLength) &&
!isValidPrefix(AF_INET6, prefixLength))
IfAddr ifaddr;
try
{
ifaddr = {addr, prefixLength};
}
catch (const std::exception& e)
{
log<level::ERR>("Prefix length is not correct "),
entry("PREFIXLENGTH=%" PRIu8, prefixLength);
auto msg = fmt::format("Invalid prefix length `{}`: {}\n", prefixLength,
e.what());
log<level::ERR>(msg.c_str(),
entry("PREFIXLENGTH=%" PRIu8, prefixLength));
elog<InvalidArgument>(
Argument::ARGUMENT_NAME("prefixLength"),
Argument::ARGUMENT_VALUE(std::to_string(prefixLength).c_str()));
return sdbusplus::message::details::string_path_wrapper();
}

try
{
if (!gateway.empty())
{
gateway = std::to_string(ToAddr<in_addr>{}(gateway));
}
}
catch (const std::exception& e)
{
auto msg = fmt::format("Invalid v4 GW `{}`: {}", gateway, e.what());
log<level::ERR>(msg.c_str(), entry("GATEWAY=%s", gateway.c_str()));
elog<InvalidArgument>(Argument::ARGUMENT_NAME("GATEWAY"),
Argument::ARGUMENT_VALUE(gateway.c_str()));
}

const std::string intfLabel = getIntfLabel();
Expand Down
72 changes: 52 additions & 20 deletions src/ibm/hypervisor-network-mgr-src/hyp_ip_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,29 @@ void HypIPAddress::resetBaseBiosTableAttrs()
updateBaseBiosTable(mapDbusToBiosAttr("prefixLength"), 0);
}

InAddrAny HypIPAddress::getIpAddress(std::string ip)
{
try
{
switch (HypIP::type())
{
case HypIP::Protocol::IPv4:
return ToAddr<in_addr>{}(HypIP::address());
case HypIP::Protocol::IPv6:
return ToAddr<in6_addr>{}(HypIP::address());
default:
throw std::logic_error("Exhausted protocols");
}
}
catch (const std::exception& e)
{
auto msg = fmt::format("Invalid IP `{}`: {}\n", ip, e.what());
log<level::ERR>(msg.c_str(), entry("ADDRESS=%s", ip.c_str()));
elog<InvalidArgument>(Argument::ARGUMENT_NAME("ipaddress"),
Argument::ARGUMENT_VALUE(ip.c_str()));
}
}

std::string HypIPAddress::address(std::string ipAddress)
{
std::string ip = HypIP::address();
Expand All @@ -214,14 +237,7 @@ std::string HypIPAddress::address(std::string ipAddress)
return ip;
}

int addressFamily =
(HypIP::type() == HypIP::Protocol::IPv4) ? AF_INET : AF_INET6;
if (!isValidIP(addressFamily, ipAddress))
{
log<level::ERR>("Not a valid IP address"),
entry("ADDRESS=%s", ipAddress.c_str());
elog<NotAllowed>(NotAllowedArgument::REASON("Invalid Ip"));
}
InAddrAny addr = getIpAddress(ipAddress);

ipAddress = HypIP::address(ipAddress);

Expand Down Expand Up @@ -251,14 +267,23 @@ uint8_t HypIPAddress::prefixLength(uint8_t value)
{
return length;
}
int addressFamily =
(HypIP::type() == HypIP::Protocol::IPv4) ? AF_INET : AF_INET6;
if (!isValidPrefix(addressFamily, value))

InAddrAny addr = getIpAddress(HypIP::address());
IfAddr ifaddr;
try
{
log<level::ERR>("PrefixLength is not correct "),
entry("PREFIXLENGTH=%" PRIu8, value);
elog<NotAllowed>(NotAllowedArgument::REASON("Invalid Prefixlength"));
ifaddr = {addr, value};
}
catch (const std::exception& e)
{
auto msg =
fmt::format("Invalid prefix length `{}`: {}\n", value, e.what());
log<level::ERR>(msg.c_str(), entry("PREFIXLENGTH=%" PRIu8, value));
elog<InvalidArgument>(
Argument::ARGUMENT_NAME("prefixLength"),
Argument::ARGUMENT_VALUE(std::to_string(value).c_str()));
}

value = HypIP::prefixLength(value);

// update parent biosTableAttrs
Expand Down Expand Up @@ -286,13 +311,20 @@ std::string HypIPAddress::gateway(std::string gateway)
// value is already existing
return gw;
}
int addressFamily =
(HypIP::type() == IP::Protocol::IPv4) ? AF_INET : AF_INET6;
if (!isValidIP(addressFamily, gateway))

try
{
if (!gateway.empty())
{
gateway = std::to_string(ToAddr<in_addr>{}(gateway));
}
}
catch (const std::exception& e)
{
log<level::ERR>("Not a valid gateway"),
entry("ADDRESS=%s", gateway.c_str());
elog<NotAllowed>(NotAllowedArgument::REASON("Invalid Gateway"));
auto msg = fmt::format("Invalid v4 GW `{}`: {}", gateway, e.what());
log<level::ERR>(msg.c_str(), entry("GATEWAY=%s", gateway.c_str()));
elog<InvalidArgument>(Argument::ARGUMENT_NAME("GATEWAY"),
Argument::ARGUMENT_VALUE(gateway.c_str()));
}

gateway = HypIP::gateway(gateway);
Expand Down
6 changes: 6 additions & 0 deletions src/ibm/hypervisor-network-mgr-src/hyp_ip_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ class HypIPAddress : public HypIPIfaces
*/
bool enabled(bool value) override;

/** @brief Method to convert ip address to InAddrAny type
* @param[in] ip - ip address of type string
* @returns ip address of type InAddrAny
*/
InAddrAny getIpAddress(std::string ip);

using HypEnableIntf::enabled;

using HypIP::address;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,48 @@ class MockHypEthernetInterface : public HypEthInterface

HypIP::AddressOrigin origin = HypIP::AddressOrigin::Static;

if (!isValidIP(AF_INET, ipaddress) && !isValidIP(AF_INET6, ipaddress))
InAddrAny addr;
try
{
// Not a valid IP address
return false;
switch (protType)
{
case HypIP::Protocol::IPv4:
addr = ToAddr<in_addr>{}(ipaddress);
break;
case HypIP::Protocol::IPv6:
addr = ToAddr<in6_addr>{}(ipaddress);
break;
default:
return false;
}
}
catch (const std::exception& e)
{
// Invalid ip address
}

if (!isValidIP(AF_INET, gateway) && !isValidIP(AF_INET6, gateway))
IfAddr ifaddr;
try
{
ifaddr = {addr, prefixLength};
}
catch (const std::exception& e)
{
// Not a valid gateway
// Invalid prefix length
return false;
}

if (!isValidPrefix(AF_INET, prefixLength) &&
!isValidPrefix(AF_INET6, prefixLength))
std::string gw;
try
{
if (!gateway.empty())
{
gw = std::to_string(ToAddr<in_addr>{}(gateway));
}
}
catch (const std::exception& e)
{
// PrefixLength is not correct
// Invalid gateway
return false;
}

Expand All @@ -89,7 +115,7 @@ class MockHypEthernetInterface : public HypEthInterface

addrs[intfLabel] = std::make_unique<HypIPAddress>(
bus, (objPath).c_str(), *this, protType, ipaddress, origin,
prefixLength, gateway, "if0");
prefixLength, gw, "if0");
return true;
}

Expand Down

0 comments on commit 63af40a

Please sign in to comment.