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

Update ebpf comment format #4624

Merged
merged 8 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 14 additions & 15 deletions backends/ebpf/codeGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ bool CodeGenInspector::preorder(const IR::BlockStatement *s) {
return false;
}

// This is correct only after inlining
/// This is correct only after inlining.
bool CodeGenInspector::preorder(const IR::ExitStatement *) {
builder->append("return");
builder->endOfStatement();
Expand Down Expand Up @@ -590,25 +590,24 @@ void CodeGenInspector::emitTCAssignmentEndianessConversion(const IR::Expression
return;
}
if (rByteOrder == "NETWORK") {
/*
If left side of assignment is not annotated field i.e host endian and right expression
is annotated field i.e network endian, we need to convert rexp to host order.
Example -
select_0 = hdr.ipv4.diffserv
select_0 = bntoh(hdr.ipv4.diffserv)
*/
// If left side of assignment is not annotated field i.e host endian and right expression
// is annotated field i.e network endian, we need to convert rexp to host order.
// Example -
// select_0 = hdr.ipv4.diffserv
// select_0 = bntoh(hdr.ipv4.diffserv)
//
emitAndConvertByteOrder(rexpr, "HOST");
}
if (lByteOrder == "NETWORK") {
/*
If left side of assignment is annotated field i.e network endian, we need to convert
right expression to network order.
Example -
hdr.opv4.diffserv = 0x1;
hdr.opv4.diffserv = bhton(0x1)
*/
// If left side of assignment is annotated field i.e network endian, we need to convert
// right expression to network order.
// Example -
// hdr.opv4.diffserv = 0x1;
// hdr.opv4.diffserv = bhton(0x1)
//
emitAndConvertByteOrder(rexpr, "NETWORK");
}

return;
}

Expand Down
16 changes: 8 additions & 8 deletions backends/ebpf/codeGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,20 @@ class CodeBuilder : public Util::SourceCodeBuilder {
explicit CodeBuilder(const Target *target) : target(target) {}
};

// Visitor for generating C for EBPF
// This visitor is invoked on various subtrees
/// Visitor for generating C for EBPF.
/// This visitor is invoked on various subtrees.
class CodeGenInspector : public Inspector {
protected:
CodeBuilder *builder;
P4::ReferenceMap *refMap;
P4::TypeMap *typeMap;
std::map<const IR::Parameter *, const IR::Parameter *> substitution;
// asPointerVariables stores the list of string expressions that
// should be emitted as pointer variables.
/// asPointerVariables stores the list of string expressions that
/// should be emitted as pointer variables.
std::set<cstring> asPointerVariables;

// Since CodeGenInspector also generates C comments,
// this variable keeps track of the current comment depth.
/// Since CodeGenInspector also generates C comments,
/// this variable keeps track of the current comment depth.
int commentDescriptionDepth = 0;

public:
Expand Down Expand Up @@ -134,10 +134,10 @@ class CodeGenInspector : public Inspector {

class EBPFInitializerUtils {
public:
// return *real* number of bits required by type
/// return *real* number of bits required by type
static unsigned ebpfTypeWidth(P4::TypeMap *typeMap, const IR::Expression *expr);

// Generate hex string and prepend it with zeroes when shorter than required width
/// Generate hex string and prepend it with zeroes when shorter than required width.
static cstring genHexStr(const big_int &value, unsigned width, const IR::Expression *expr);
};

Expand Down
4 changes: 2 additions & 2 deletions backends/ebpf/ebpfControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class ControlBodyTranslator : public virtual CodeGenInspector {
public:
explicit ControlBodyTranslator(const EBPFControl *control);

// handle the packet_out.emit method
/// Handle the packet_out.emit method.
virtual void compileEmitField(const IR::Expression *expr, cstring field, unsigned alignment,
EBPFType *type);
virtual void compileEmit(const IR::Vector<IR::Argument> *args);
Expand Down Expand Up @@ -63,7 +63,7 @@ class EBPFControl : public EBPFObject {
const IR::Parameter *xdpInputMeta; // only for xdp progs
const IR::Parameter *xdpOutputMeta; // only for xdp progs
const IR::Parameter *parserHeaders;
// replace references to headers with references to parserHeaders
/// Replace references to headers with references to parserHeaders
cstring hitVariable;
ControlBodyTranslator *codeGen;
const bool emitExterns;
Expand Down
12 changes: 6 additions & 6 deletions backends/ebpf/ebpfDeparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace EBPF {

class EBPFDeparser;

// this translator emits deparser externs
/// This translator emits deparser externs.
class DeparserBodyTranslator : public ControlBodyTranslator {
protected:
const EBPFDeparser *deparser;
Expand All @@ -34,7 +34,7 @@ class DeparserBodyTranslator : public ControlBodyTranslator {
bool preorder(const IR::MethodCallExpression *expression) override;
};

// this translator emits buffer preparation (eg. which headers will be emitted)
/// This translator emits buffer preparation (eg. which headers will be emitted)
class DeparserPrepareBufferTranslator : public ControlBodyTranslator {
protected:
const EBPFDeparser *deparser;
Expand All @@ -47,7 +47,7 @@ class DeparserPrepareBufferTranslator : public ControlBodyTranslator {
bool preorder(const IR::MethodCallExpression *expression) override;
};

// this translator emits headers
/// This translator emits headers
class DeparserHdrEmitTranslator : public DeparserPrepareBufferTranslator {
protected:
const EBPFDeparser *deparser;
Expand Down Expand Up @@ -79,9 +79,9 @@ class EBPFDeparser : public EBPFControl {

bool build() override;
void emit(CodeBuilder *builder) override;
// A "PreDeparser" is emitted just before a sequence of hdr.emit() functions.
// It is useful in the case of resubmit or clone operation, as these operations
// require to have an original packet.
/// A "PreDeparser" is emitted just before a sequence of hdr.emit() functions.
/// It is useful in the case of resubmit or clone operation, as these operations
/// require to have an original packet.
virtual void emitPreDeparser(CodeBuilder *builder) { (void)builder; }

virtual void emitDeparserExternCalls(CodeBuilder *builder) { (void)builder; }
Expand Down
4 changes: 2 additions & 2 deletions backends/ebpf/ebpfModel.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct Filter_Model : public ::Model::Elem {
::Model::Elem filter;
};

// Keep this in sync with ebpf_model.p4 and xdp_model.p4
/// Keep this in sync with ebpf_model.p4 and xdp_model.p4
class EBPFModel : public ::Model::Model {
protected:
EBPFModel()
Expand All @@ -87,7 +87,7 @@ class EBPFModel : public ::Model::Model {
::Model::Elem CPacketName;
::Model::Param_Model packet;
ModelArchitecture arch;
// Only one of these should be used, depending on arch value.
/// Only one of these should be used, depending on arch value.
Filter_Model filter;
Xdp_Model xdp;

Expand Down
2 changes: 1 addition & 1 deletion backends/ebpf/ebpfObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ limitations under the License.

namespace EBPF {

// Base class for EBPF objects
/// Base class for EBPF objects.
class EBPFObject : public ICastable {
public:
virtual ~EBPFObject() {}
Expand Down
16 changes: 8 additions & 8 deletions backends/ebpf/ebpfOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ enum XDP2TC { XDP2TC_NONE, XDP2TC_META, XDP2TC_HEAD, XDP2TC_CPUMAP };

class EbpfOptions : public CompilerOptions {
public:
// file to output to
/// file to output to
cstring outputFile = nullptr;
// read from json
/// read from json
bool loadIRFromJson = false;
// Externs generation
/// Externs generation
bool emitExterns = false;
// tracing eBPF code execution
/// tracing eBPF code execution
bool emitTraceMessages = false;
// generate program to XDP layer
/// generate program to XDP layer
bool generateToXDP = false;
// XDP2TC mode for PSA-eBPF
//// XDP2TC mode for PSA-eBPF
enum XDP2TC xdp2tcMode = XDP2TC_NONE;
// maximum number of unique ternary masks
/// maximum number of unique ternary masks
unsigned int maxTernaryMasks = 128;
// Enable table cache for LPM and ternary tables
/// Enable table cache for LPM and ternary tables
bool enableTableCache = false;

EbpfOptions();
Expand Down
2 changes: 1 addition & 1 deletion backends/ebpf/ebpfParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class EBPFParserState;

class StateTranslationVisitor : public CodeGenInspector {
protected:
// stores the result of evaluating the select argument
/// Stores the result of evaluating the select argument.
cstring selectValue;

P4::P4CoreLibrary &p4lib;
Expand Down
6 changes: 3 additions & 3 deletions backends/ebpf/ebpfProgram.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,17 @@ class EBPFProgram : public EBPFObject {
EBPFParser *parser;
EBPFControl *control;
EBPFModel &model;
// Deparser may be NULL if not supported (e.g. ebpfFilter package)
/// Deparser may be NULL if not supported (e.g. ebpfFilter package).
EBPFDeparser *deparser;

cstring endLabel, offsetVar, lengthVar, headerStartVar;
cstring zeroKey, functionName, errorVar;
cstring packetStartVar, packetEndVar, byteVar;
cstring errorEnum;
cstring license = "GPL"; // TODO: this should be a compiler option probably
cstring license = "GPL"; /// TODO: this should be a compiler option probably
cstring arrayIndexType = "u32";

virtual bool build(); // return 'true' on success
virtual bool build(); /// return 'true' on success

EBPFProgram(const EbpfOptions &options, const IR::P4Program *program, P4::ReferenceMap *refMap,
P4::TypeMap *typeMap, const IR::ToplevelBlock *toplevel)
Expand Down
13 changes: 7 additions & 6 deletions backends/ebpf/ebpfTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ void EBPFTable::initKey() {
}
}

// Performs the following validations:
// - Validates if LPM key is the last one from match keys in an LPM table (ignores selector fields).
// - Validates if match fields in ternary tables are sorted by size
// in descending order (ignores selector fields).
/// Performs the following validations:
/// - Validates if LPM key is the last one from match keys in an LPM table (ignores selector
/// fields).
/// - Validates if match fields in ternary tables are sorted by size
/// in descending order (ignores selector fields).
void EBPFTable::validateKeys() const {
if (keyGenerator == nullptr) return;

Expand Down Expand Up @@ -862,8 +863,8 @@ cstring EBPFTable::p4ActionToActionIDName(const IR::P4Action *action) const {
return Util::printf_format("%s_ACT_%s", tableInstance.toUpper(), actionName.toUpper());
}

// As ternary has precedence over lpm, this function checks if any
// field is key field is lpm and none of key fields is of type ternary.
/// As ternary has precedence over lpm, this function checks if any
/// field is key field is lpm and none of key fields is of type ternary.
bool EBPFTable::isLPMTable() const {
bool isLPM = false;
if (keyGenerator != nullptr) {
Expand Down
12 changes: 6 additions & 6 deletions backends/ebpf/ebpfTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class ActionTranslationVisitor : public virtual CodeGenInspector {
bool isActionParameter(const IR::PathExpression *expression) const;
}; // ActionTranslationVisitor

// Also used to represent counters
/// Also used to represent counters
class EBPFTableBase : public EBPFObject {
public:
const EBPFProgram *program;
Expand Down Expand Up @@ -94,8 +94,8 @@ class EBPFTable : public EBPFTableBase {
cstring defaultActionMapName;
std::map<const IR::KeyElement *, cstring> keyFieldNames;
std::map<const IR::KeyElement *, EBPFType *> keyTypes;
// Use 1024 by default.
// TODO: make it configurable using compiler options.
/// Use 1024 by default.
/// TODO: make it configurable using compiler options.
size_t size = 1024;
const cstring prefixFieldName = "prefixlen";

Expand All @@ -112,7 +112,7 @@ class EBPFTable : public EBPFTableBase {
virtual void emitValueType(CodeBuilder *builder);
virtual void emitValueActionIDNames(CodeBuilder *builder);
virtual void emitValueStructStructure(CodeBuilder *builder);
// Emits value types used by direct externs.
/// Emits value types used by direct externs.
virtual void emitDirectValueTypes(CodeBuilder *builder) { (void)builder; }
virtual void emitAction(CodeBuilder *builder, cstring valueName, cstring actionRunVariable);
virtual void emitInitializer(CodeBuilder *builder);
Expand All @@ -128,8 +128,8 @@ class EBPFTable : public EBPFTableBase {
matchType->name.name == P4::P4CoreLibrary::instance().ternaryMatch.name ||
matchType->name.name == P4::P4CoreLibrary::instance().lpmMatch.name;
}
// Whether to drop packet if no match entry found.
// Some table implementations may want to continue processing.
/// Whether to drop packet if no match entry found.
/// Some table implementations may want to continue processing.
virtual bool dropOnNoMatchingEntryFound() const { return true; }

virtual bool cacheEnabled() { return false; }
Expand Down
12 changes: 6 additions & 6 deletions backends/ebpf/ebpfType.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ limitations under the License.

namespace EBPF {

// Base class for EBPF types
/// Base class for EBPF types
class EBPFType : public EBPFObject {
protected:
explicit EBPFType(const IR::Type *type) : type(type) {}
Expand All @@ -46,10 +46,10 @@ class EBPFType : public EBPFObject {
class IHasWidth : public ICastable {
public:
virtual ~IHasWidth() {}
// P4 width
/// P4 width
virtual unsigned widthInBits() const = 0;
// Width in the target implementation.
// Currently a multiple of 8.
/// Width in the target implementation.
/// Currently a multiple of 8.
virtual unsigned implementationWidthInBits() const = 0;

DECLARE_TYPEINFO(IHasWidth);
Expand Down Expand Up @@ -125,7 +125,7 @@ class EBPFScalarType : public EBPFType, public IHasWidth {
DECLARE_TYPEINFO(EBPFScalarType, EBPFType, IHasWidth);
};

// This should not always implement IHasWidth, but it may...
/// This should not always implement IHasWidth, but it may...
class EBPFTypeName : public EBPFType, public IHasWidth {
const IR::Type_Name *type;
EBPFType *canonical;
Expand All @@ -149,7 +149,7 @@ class EBPFTypeName : public EBPFType, public IHasWidth {
DECLARE_TYPEINFO(EBPFTypeName, EBPFType, IHasWidth);
};

// Also represents headers and unions
/// Also represents headers and unions
class EBPFStructType : public EBPFType, public IHasWidth {
class EBPFField {
public:
Expand Down
6 changes: 2 additions & 4 deletions backends/ebpf/lower.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ limitations under the License.

namespace EBPF {

/**
This pass rewrites expressions which are not supported natively on EBPF.
*/
/// This pass rewrites expressions which are not supported natively on EBPF.
class LowerExpressions : public Transform {
P4::TypeMap *typeMap;
// Cannot shift with a value larger than 5 bits
/// Cannot shift with a value larger than 5 bits.
const int maxShiftWidth = 5;
const IR::Expression *shift(const IR::Operation_Binary *expression) const;

Expand Down
2 changes: 1 addition & 1 deletion backends/ebpf/midend.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class MidEnd {
P4::TypeMap typeMap;

void addDebugHook(DebugHook hook) { hooks.push_back(hook); }
// If p4c is run with option '--listMidendPasses', outStream is used for printing passes names
/// If p4c is run with option '--listMidendPasses', outStream is used for printing passes names.
const IR::ToplevelBlock *run(EbpfOptions &options, const IR::P4Program *program,
std::ostream *outStream = nullptr);
};
Expand Down
2 changes: 1 addition & 1 deletion backends/ebpf/psa/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class PSASwitchBackend {
void convert(const IR::ToplevelBlock *tlb);
void codegen(std::ostream &cstream) const {
CodeBuilder c(target);
// instead of generating two files, put all the code in a single file
// Instead of generating two files, put all the code in a single file.
ebpf_program->emit(&c);
cstream << c.toString();
}
Expand Down
Loading
Loading