Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Guard changes #2

Merged
merged 5 commits into from
Sep 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ GUARD Tool
Usage: guard [OPTIONS]
Options:
-h,--help Use the below listed functions.
Warning: Don't try guard on non guardable units
(sys, perv)
-c,--create TEXT Create GUARD record, expects physical path as input.
-d,--delete TEXT Delete GUARD record, expects physical path as input.
-l,--list Listing of GUARDed resources.
-r,--clearall Clears GUARD states for all resources.
-v,--version Version of GUARD tool.
-h,--help Guard CLI tool options
chiragsibm marked this conversation as resolved.
Show resolved Hide resolved
-c,--create TEXT Create Guard record, expects physical path as input
-i,--invalidate TEXT Invalidate a single Guard record, expects physical
path as input
-I,--invalidate-all Invalidates all the Guard records
-l,--list List all the GUARD'ed resources
-r,--reset Erase all the Guard records
-v,--version Version of GUARD tool
chiragsibm marked this conversation as resolved.
Show resolved Hide resolved
```
**Note:** Physical path can be fetched from device tree, using ATTR_PHYS_DEV_PATH
attribute of the corresponding target.
Expand All @@ -64,11 +65,15 @@ guard -l
ID | ERROR | Type | Path
00000001 | 00000000 | manual | physical:sys-0/node-0/proc-0/mc-0/mi-0/mcc-0
```
* To clear all the guard records
* To erase all the guard records
```
guard -r
```
* To clear a particular guard record
* To invalidate a single guard record
```
guard -i sys-0/node-0/proc-0/mc-0/mi-0/mcc-0
```
* To invalidate all the guard records
```
guard -d sys-0/node-0/proc-0/mc-0/mi-0/mcc-0
guard -I
```
129 changes: 91 additions & 38 deletions guard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,83 @@
#include "libguard/guard_interface.hpp"
#include "libguard/include/guard_record.hpp"

#include <config.h>

#include <CLI/CLI.hpp>

using namespace std;
using namespace openpower::guard;

void guardList()
void guardList(bool displayResolved)
{
auto records = getAll();
if (!records.size())
{
std::cout << "No Records to display" << std::endl;
return;
}
std::cout << "ID | ERROR | Type | Path " << std::endl;

bool isHeaderPrinted = false;
for (const auto& elem : records)
chiragsibm marked this conversation as resolved.
Show resolved Hide resolved
{
if (elem.errType != GARD_Reconfig)
// Not to print guard records with errorlog type set as GARD_Reconfig
chiragsibm marked this conversation as resolved.
Show resolved Hide resolved
// As guard below type records are for internal usage only.
if (elem.errType == GARD_Reconfig)
{
continue;
}
// To list resolved records as user wants to list resolved records
else if (displayResolved && (elem.recordId != GUARD_RESOLVED))
{
continue;
}
// To list unresolved records as user wants to list unresolved records
else if (!displayResolved && (elem.recordId == GUARD_RESOLVED))
{
continue;
}

// Don't print the header if already printed header since
// records mixed of resolved and unresolved records so if have
// only either one in retrieved records and user tried to see opposite
// one then we should not print header else user will get confused.
if (!isHeaderPrinted)
{
std::cout << "ID | ERROR | Type | Path " << std::endl;
isHeaderPrinted = true;
}

std::cout << std::hex << std::setw(8) << std::setfill('0')
<< elem.recordId;

std::cout << " | ";
std::cout << std::hex << std::setw(8) << std::setfill('0')
<< elem.elogId;

std::cout << " | ";
std::optional<std::string> gReasonToStr =
guardReasonToStr(elem.errType);
std::cout << *gReasonToStr;

std::cout << " | ";
std::optional<std::string> physicalPath =
getPhysicalPath(elem.targetId);
if (!physicalPath)
{
std::cout << "Unknown ";
}
else
{
std::cout << std::hex << std::setw(8) << std::setfill('0')
<< elem.recordId;
std::cout << " | ";
std::cout << std::hex << std::setw(8) << std::setfill('0')
<< elem.elogId;

std::cout << " | ";
std::optional<std::string> gReasonToStr =
guardReasonToStr(elem.errType);
std::cout << *gReasonToStr;
std::cout << " | ";
std::optional<std::string> physicalPath =
getPhysicalPath(elem.targetId);
if (!physicalPath)
{
std::cout << "Unknown ";
}
else
{
std::cout << *physicalPath;
}
std::cout << std::endl;
std::cout << *physicalPath;
}
std::cout << std::endl;
}

if (!isHeaderPrinted)
{
std::cout << "No "
<< (displayResolved == true ? "resolved" : "unresolved")
Copy link
Collaborator

@RameshIyyar RameshIyyar Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS: displayResolved == true ? -> displayResolved ?

<< " records to display" << std::endl;
}
}

Expand All @@ -62,6 +98,11 @@ void guardClear()
clearAll();
}

void guardInvalidateAll()
{
invalidateAll();
}

void guardCreate(const std::string& physicalPath)
{
std::optional<EntityPath> entityPath = getEntityPath(physicalPath);
Expand All @@ -84,25 +125,29 @@ int main(int argc, char** argv)
{
try
{
CLI::App app{"GUARD Tool"};
CLI::App app{"Guard Tool"};
std::optional<std::string> createGuardStr;
std::optional<std::string> deleteGuardStr;
bool listGuardRecords = false;
bool clearAll = false;
bool listResolvedGuardRecords = false;
bool invalidateAll = false;
bool gversion = false;

app.set_help_flag("-h, --help",
"Use the below listed functions.\n"
"Warning: Don't try guard on non guardable units "
"(sys, perv)");
app.set_help_flag("-h, --help", "Guard CLI tool options");
app.add_option("-c, --create", createGuardStr,
"Create GUARD record, expects physical path as input");
app.add_option("-d, --delete", deleteGuardStr,
"Delete GUARD record, expects physical path as input");
"Create Guard record, expects physical path as input");
app.add_option(
"-i, --invalidate", deleteGuardStr,
"Invalidate a single Guard record, expects physical path as input");
app.add_flag("-I, --invalidate-all", invalidateAll,
"Invalidates all the Guard records");
app.add_flag("-l, --list", listGuardRecords,
"Listing of GUARDed resources.");
app.add_flag("-r, --clearall", clearAll,
"Clears GUARD states for all resources");
"List all the Guard'ed resources");
app.add_flag("-r, --reset", clearAll, "Erase all the Guard records");
app.add_flag("-a, --listresolvedrecords", listResolvedGuardRecords,
"List all the resolved Guard'ed resources")
->group("");
app.add_flag("-v, --version", gversion, "Version of GUARD tool");

CLI11_PARSE(app, argc, argv);
Expand All @@ -123,11 +168,19 @@ int main(int argc, char** argv)
}
else if (listGuardRecords)
{
guardList();
guardList(false);
}
else if (listResolvedGuardRecords)
{
guardList(true);
}
else if (invalidateAll)
{
guardInvalidateAll();
}
else if (gversion)
{
std::cout << "GUARD Tool version is " << GUARD_VERSION << std::endl;
std::cout << "Guard tool " << GUARD_VERSION << std::endl;
}
else
{
Expand Down
21 changes: 11 additions & 10 deletions libguard/guard_common.hpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
// SPDX-License-Identifier: Apache-2.0
#pragma once

#include "guard_exception.hpp"
#include "guard_log.hpp"

#include <array>
#include <cstdint>
#include <stdexcept>
#include <optional>

namespace openpower
{
namespace guard
{

using namespace openpower::guard::exception;

/* From hostboot: src/include/usr/targeting/common/entitypath.H */
struct EntityPath
{
Expand Down Expand Up @@ -75,15 +78,15 @@ struct EntityPath
GUARD_ERROR,
"Size mismatch. Given buf size[%d] EntityPath sizeof[%d]",
rawData.size(), sizeof(EntityPath));
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath initializer_list constructor failed");
}

if (rawData.size() == 0)
{
openpower::guard::log::guard_log(GUARD_ERROR,
"Given raw data is empty");
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath initializer_list constructor failed");
}

Expand All @@ -93,7 +96,7 @@ struct EntityPath
{
openpower::guard::log::guard_log(
GUARD_ERROR, "PathElement size mismatch in given raw data");
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath initializer_list constructor failed");
}

Expand All @@ -107,7 +110,7 @@ struct EntityPath
openpower::guard::log::guard_log(
GUARD_ERROR,
"Insufficient data for PathElement in given raw data");
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath initializer_list constructor failed");
}

Expand All @@ -122,8 +125,7 @@ struct EntityPath
{
openpower::guard::log::guard_log(GUARD_ERROR,
"Given raw data is empty");
throw std::runtime_error(
"EntityPath conversion constructor failed");
throw InvalidEntityPath("EntityPath conversion constructor failed");
}

type_size = rawData[0];
Expand All @@ -135,8 +137,7 @@ struct EntityPath
GUARD_ERROR,
"Size mismatch. Given path elements size[%d] max[%d]",
pathElementsSize, maxPathElements);
throw std::runtime_error(
"EntityPath conversion constructor failed");
throw InvalidEntityPath("EntityPath conversion constructor failed");
}

for (int i = 0, j = 1; i < pathElementsSize;
Expand All @@ -147,7 +148,7 @@ struct EntityPath
openpower::guard::log::guard_log(
GUARD_ERROR,
"Insufficient data for PathElement in given raw data");
throw std::runtime_error(
throw InvalidEntityPath(
"EntityPath conversion constructor failed");
}

Expand Down
83 changes: 83 additions & 0 deletions libguard/guard_exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#pragma once

#include <exception>
#include <sstream>
#include <string>

chiragsibm marked this conversation as resolved.
Show resolved Hide resolved
namespace openpower
{
namespace guard
{
namespace exception
{
// TODO:Issue #3, to avoid multiple exception classes
class GuardException : public std::exception
chiragsibm marked this conversation as resolved.
Show resolved Hide resolved
{
public:
explicit GuardException(const std::string& message) : message(message){};

const char* what() const noexcept override
{
return message.c_str();
}

chiragsibm marked this conversation as resolved.
Show resolved Hide resolved
private:
std::string message;
};

class GuardFileOpenFailed : public GuardException
{
public:
explicit GuardFileOpenFailed(const std::string& msg) :
GuardException(msg){};
};

class GuardFileReadFailed : public GuardException
{
public:
explicit GuardFileReadFailed(const std::string& msg) :
GuardException(msg){};
};

class GuardFileWriteFailed : public GuardException
{
public:
explicit GuardFileWriteFailed(const std::string& msg) :
GuardException(msg){};
};

class GuardFileSeekFailed : public GuardException
{
public:
explicit GuardFileSeekFailed(const std::string& msg) :
GuardException(msg){};
};

class InvalidEntry : public GuardException
{
public:
explicit InvalidEntry(const std::string& msg) : GuardException(msg){};
};

class AlreadyGuarded : public GuardException
{
public:
explicit AlreadyGuarded(const std::string& msg) : GuardException(msg){};
};

class InvalidEntityPath : public GuardException
{
public:
explicit InvalidEntityPath(const std::string& msg) : GuardException(msg){};
};

class GuardFileOverFlowed : public GuardException
{
public:
explicit GuardFileOverFlowed(const std::string& msg) :
GuardException(msg){};
};

} // namespace exception
} // namespace guard
} // namespace openpower
Loading