From 3b3b377f416344f63ed6efe5e1642da1e3bedda8 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Wed, 16 Feb 2022 18:37:55 +0100 Subject: [PATCH 01/53] First commit --- Makefile | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/Makefile b/Makefile index 50454790c..69f908274 100644 --- a/Makefile +++ b/Makefile @@ -99,6 +99,18 @@ test: .coverage -v \ ./pfcpiface +internal/p4constants/p4info.go: conf/p4/bin/p4info.txt + +P4INFO_GO := internal/p4constants/p4info.go +p4info-go: $(P4INFO_GO) +$(P4INFO_GO): + $(info *** Generating go constants: $< -> $@) + @docker run --rm -v $(CURDIR):/tassen -w /tassen \ + --entrypoint ./util/go-gen-p4-const.py $(PTF_IMG) \ + --output $@ --p4info $< + @docker run --rm -v $(CURDIR):/tassen -w /tassen \ + ${GOLANG_IMG} gofmt -w $@ + fmt: @go fmt ./... From d5188dcd2716e508b36b81cb05e475c9a999e744 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 17 Feb 2022 15:44:45 +0100 Subject: [PATCH 02/53] Add generation constants --- Makefile | 18 +-- internal/p4constants/p4constants.go | 118 +++++++++++++++++ utils/go_gen_p4_const.py | 193 ++++++++++++++++++++++++++++ 3 files changed, 318 insertions(+), 11 deletions(-) create mode 100644 internal/p4constants/p4constants.go create mode 100755 utils/go_gen_p4_const.py diff --git a/Makefile b/Makefile index 69f908274..0d37d325c 100644 --- a/Makefile +++ b/Makefile @@ -99,17 +99,13 @@ test: .coverage -v \ ./pfcpiface -internal/p4constants/p4info.go: conf/p4/bin/p4info.txt - -P4INFO_GO := internal/p4constants/p4info.go -p4info-go: $(P4INFO_GO) -$(P4INFO_GO): - $(info *** Generating go constants: $< -> $@) - @docker run --rm -v $(CURDIR):/tassen -w /tassen \ - --entrypoint ./util/go-gen-p4-const.py $(PTF_IMG) \ - --output $@ --p4info $< - @docker run --rm -v $(CURDIR):/tassen -w /tassen \ - ${GOLANG_IMG} gofmt -w $@ +p4info-go: + $(info *** Generating go constants...) + @docker run --rm -v $(CURDIR):/app -w /app \ + opennetworking/up4-ptf:latest python ./utils/go_gen_p4_const.py \ + --output internal/p4constants/p4constants.go --p4info conf/p4/bin/p4info.txt + @docker run --rm -v $(CURDIR):/app -w /app \ + golang:1.16-alpine gofmt -w internal/p4constants/p4constants.go fmt: @go fmt ./... diff --git a/internal/p4constants/p4constants.go b/internal/p4constants/p4constants.go new file mode 100644 index 000000000..a44284741 --- /dev/null +++ b/internal/p4constants/p4constants.go @@ -0,0 +1,118 @@ +/* + * Copyright 2022-present Open Networking Foundation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +package p4constants + +//noinspection GoSnakeCaseUsage +const ( + // Header field IDs + Hdr_PreQosPipeTunnelPeers_TunnelPeerId uint32 = 1 + Hdr_PreQosPipeApplications_AppL4Port uint32 = 2 + Hdr_PreQosPipeApplications_AppIpProto uint32 = 3 + Hdr_PreQosPipeApplications_AppIpAddr uint32 = 1 + Hdr_PreQosPipeAclAcls_SrcIface uint32 = 2 + Hdr_PreQosPipeAclAcls_L4Sport uint32 = 9 + Hdr_PreQosPipeAclAcls_Ipv4Dst uint32 = 7 + Hdr_PreQosPipeAclAcls_Ipv4Src uint32 = 6 + Hdr_PreQosPipeAclAcls_L4Dport uint32 = 10 + Hdr_PreQosPipeAclAcls_Inport uint32 = 1 + Hdr_PreQosPipeAclAcls_Ipv4Proto uint32 = 8 + Hdr_PreQosPipeAclAcls_EthType uint32 = 5 + Hdr_PreQosPipeAclAcls_EthSrc uint32 = 3 + Hdr_PreQosPipeAclAcls_EthDst uint32 = 4 + Hdr_PreQosPipeTerminationsDownlink_AppId uint32 = 2 + Hdr_PreQosPipeTerminationsDownlink_UeAddress uint32 = 1 + Hdr_PreQosPipeInterfaces_Ipv4DstPrefix uint32 = 1 + Hdr_PreQosPipeMyStation_DstMac uint32 = 1 + Hdr_PreQosPipeRoutingRoutesV4_DstPrefix uint32 = 1 + Hdr_PreQosPipeSessionsDownlink_UeAddress uint32 = 1 + Hdr_PreQosPipeSessionsUplink_N3Address uint32 = 1 + Hdr_PreQosPipeSessionsUplink_Teid uint32 = 2 + Hdr_PreQosPipeTerminationsUplink_AppId uint32 = 2 + Hdr_PreQosPipeTerminationsUplink_UeAddress uint32 = 1 + // Table IDs + Table_PreQosPipeTunnelPeers uint32 = 49497304 + Table_PreQosPipeAclAcls uint32 = 47204971 + Table_PreQosPipeTerminationsUplink uint32 = 37595532 + Table_PreQosPipeRoutingRoutesV4 uint32 = 39015874 + Table_PreQosPipeSessionsUplink uint32 = 44976597 + Table_PreQosPipeMyStation uint32 = 40931612 + Table_PreQosPipeSessionsDownlink uint32 = 34742049 + Table_PreQosPipeApplications uint32 = 46868458 + Table_PreQosPipeInterfaces uint32 = 33923840 + Table_PreQosPipeTerminationsDownlink uint32 = 34778590 + // Indirect Counter IDs + Counter_PreQosPipePreQosCounter uint32 = 315693181 + Counter_PostQosPipePostQosCounter uint32 = 302958180 + // Direct Counter IDs + DirectCounter_Acls uint32 = 325583051 + // Action IDs + Action_PreQosPipeRoutingDrop uint32 = 31448256 + Action_PreQosPipeSetSessionDownlinkBuff uint32 = 20249483 + Action_PreQosPipeUplinkTermFwd uint32 = 28305359 + Action_PreQosPipeDownlinkTermFwd uint32 = 32699713 + Action_PreQosPipeAclPunt uint32 = 26495283 + Action_NoAction uint32 = 21257015 + Action_PreQosPipeSetSessionDownlinkDrop uint32 = 20229579 + Action_PreQosPipeAclSetPort uint32 = 30494847 + Action_PreQosPipeDoGtpuTunnelWithPsc uint32 = 31713420 + Action_PreQosPipeUplinkTermDrop uint32 = 20977365 + Action_PreQosPipeDoDrop uint32 = 28401267 + Action_PreQosPipeAclDrop uint32 = 18812293 + Action_PreQosPipeSetSourceIface uint32 = 26090030 + Action_PreQosPipeDoGtpuTunnel uint32 = 29247910 + Action_PreQosPipeSetSessionUplinkDrop uint32 = 22196934 + Action_PreQosPipeSetSessionDownlink uint32 = 21848329 + Action_PreQosPipeLoadTunnelParam uint32 = 32742981 + Action_PreQosPipeInitializeMetadata uint32 = 23766285 + Action_PreQosPipeUplinkTermFwdNoTc uint32 = 21760615 + Action_PreQosPipeRoutingRoute uint32 = 23965128 + Action_PreQosPipeSetAppId uint32 = 23010411 + Action_PreQosPipeDownlinkTermFwdNoTc uint32 = 26185804 + Action_PreQosPipeSetSessionUplink uint32 = 19461580 + Action_PreQosPipeDownlinkTermDrop uint32 = 31264233 + Action_PreQosPipeAclCloneToCpu uint32 = 21596798 + // Action Param IDs + ActionParam_PreQosPipeSetSessionDownlink_TunnelPeerId uint32 = 1 + ActionParam_PreQosPipeSetSessionDownlink_SessionMeterIdx uint32 = 2 + ActionParam_PreQosPipeRoutingRoute_SrcMac uint32 = 1 + ActionParam_PreQosPipeRoutingRoute_EgressPort uint32 = 3 + ActionParam_PreQosPipeRoutingRoute_DstMac uint32 = 2 + ActionParam_PreQosPipeUplinkTermFwdNoTc_AppMeterIdx uint32 = 2 + ActionParam_PreQosPipeUplinkTermFwdNoTc_CtrIdx uint32 = 1 + ActionParam_PreQosPipeSetSessionUplink_SessionMeterIdx uint32 = 1 + ActionParam_PreQosPipeUplinkTermDrop_CtrIdx uint32 = 1 + ActionParam_PreQosPipeSetAppId_AppId uint32 = 1 + ActionParam_PreQosPipeAclSetPort_Port uint32 = 1 + ActionParam_PreQosPipeSetSessionDownlinkBuff_SessionMeterIdx uint32 = 1 + ActionParam_PreQosPipeDownlinkTermDrop_CtrIdx uint32 = 1 + ActionParam_PreQosPipeDownlinkTermFwd_Tc uint32 = 4 + ActionParam_PreQosPipeDownlinkTermFwd_Teid uint32 = 2 + ActionParam_PreQosPipeDownlinkTermFwd_AppMeterIdx uint32 = 5 + ActionParam_PreQosPipeDownlinkTermFwd_CtrIdx uint32 = 1 + ActionParam_PreQosPipeDownlinkTermFwd_Qfi uint32 = 3 + ActionParam_PreQosPipeUplinkTermFwd_AppMeterIdx uint32 = 3 + ActionParam_PreQosPipeUplinkTermFwd_Tc uint32 = 2 + ActionParam_PreQosPipeUplinkTermFwd_CtrIdx uint32 = 1 + ActionParam_PreQosPipeLoadTunnelParam_DstAddr uint32 = 2 + ActionParam_PreQosPipeLoadTunnelParam_SrcAddr uint32 = 1 + ActionParam_PreQosPipeLoadTunnelParam_Sport uint32 = 3 + ActionParam_PreQosPipeSetSourceIface_SliceId uint32 = 3 + ActionParam_PreQosPipeSetSourceIface_SrcIface uint32 = 1 + ActionParam_PreQosPipeSetSourceIface_Direction uint32 = 2 + ActionParam_PreQosPipeDownlinkTermFwdNoTc_AppMeterIdx uint32 = 4 + ActionParam_PreQosPipeDownlinkTermFwdNoTc_Teid uint32 = 2 + ActionParam_PreQosPipeDownlinkTermFwdNoTc_CtrIdx uint32 = 1 + ActionParam_PreQosPipeDownlinkTermFwdNoTc_Qfi uint32 = 3 + // Action Profile IDs + ActionProfile_HashedSelector uint32 = 297808402 + // Packet Metadata IDs + PacketMeta_IngressPort uint32 = 1 + PacketMeta_Reserved uint32 = 1 + // Meter IDs + Meter_PreQosPipeAppMeter uint32 = 338231090 + Meter_PreQosPipeSessionMeter uint32 = 347593234 +) diff --git a/utils/go_gen_p4_const.py b/utils/go_gen_p4_const.py new file mode 100755 index 000000000..c34b773b4 --- /dev/null +++ b/utils/go_gen_p4_const.py @@ -0,0 +1,193 @@ +# Copyright 2022-present Open Networking Foundation +# +# SPDX-License-Identifier: Apache-2.0 + +import argparse +import google.protobuf.text_format as tf +import re +from p4.config.v1 import p4info_pb2 + +copyright = '''/* + * Copyright 2022-present Open Networking Foundation + * + * SPDX-License-Identifier: Apache-2.0 + */ +''' + +PKG_FMT = 'package %s' + +CONST_OPEN = '''//noinspection GoSnakeCaseUsage + const (''' +CONST_CLOSE = ')' + + +UINT32 = 'uint32' +EMPTY_STR = '' + +HF_VAR_PREFIX = 'Hdr_' +TBL_VAR_PREFIX = 'Table_' +CTR_VAR_PREFIX = 'Counter_' +DIRCTR_VAR_PREFIX = 'DirectCounter_' +ACT_VAR_PREFIX = "Action_" +ACTPARAM_VAR_PREFIX = "ActionParam_" +ACTPROF_VAR_PREFIX = "ActionProfile_" +PACKETMETA_VAR_PREFIX = "PacketMeta_" +MTR_VAR_PREFIX = "Meter_" + +class ConstantClassGenerator(object): + header_fields = dict() + tables = set() + counters = set() + direct_counters = set() + actions = set() + action_params = dict() + action_profiles = set() + packet_metadata = set() + meters = set() + + # https://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-snake-case + def convert_camel_to_all_caps(self, name): + s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) + s1 = re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).title() + return s1.replace('_', '').replace('.', '') + + def __init__(self, pkg_name): + self.package_name = PKG_FMT % (pkg_name, ) + + def parse(self, p4info): + for tbl in p4info.tables: + for mf in tbl.match_fields: + try: + self.header_fields[tbl.preamble.name] + except KeyError: + self.header_fields[tbl.preamble.name] = set() + finally: + self.header_fields[tbl.preamble.name].add((mf.name, mf.id)) + + self.tables.add((tbl.preamble.name, tbl.preamble.id)) + + for ctr in p4info.counters: + self.counters.add((ctr.preamble.name, ctr.preamble.id)) + + for dir_ctr in p4info.direct_counters: + self.direct_counters.add((dir_ctr.preamble.name, dir_ctr.preamble.id)) + + for act in p4info.actions: + self.actions.add((act.preamble.name, act.preamble.id)) + + for param in act.params: + try: + self.action_params[act.preamble.name] + except KeyError as e: + self.action_params[act.preamble.name] = set() + finally: + self.action_params[act.preamble.name].add((param.name, param.id)) + + for act_prof in p4info.action_profiles: + self.action_profiles.add((act_prof.preamble.name, act_prof.preamble.id)) + + for cpm in p4info.controller_packet_metadata: + for mta in cpm.metadata: + self.packet_metadata.add((mta.name, mta.id)) + for mtr in p4info.meters: + self.meters.add((mtr.preamble.name, mtr.preamble.id)) + + def const_line(self, obj_type, name, id, var_type=UINT32, table_prefix=None): + var_name = self.convert_camel_to_all_caps(name) + if table_prefix: + var_name = self.convert_camel_to_all_caps(table_prefix) + "_" + var_name + var_name = obj_type+var_name + base_line = "\t%s %s = %i" + line = base_line % (var_name, var_type, id) + return line + + def generate_go(self): + lines = list() + lines.append(copyright) + lines.append(self.package_name) + lines.append(CONST_OPEN) + + if len(self.header_fields) != 0: + lines.append(' // Header field IDs') + for (tbl, hfs) in self.header_fields.items(): + for hf in hfs: + lines.append(self.const_line(HF_VAR_PREFIX, hf[0], hf[1], table_prefix=tbl)) + + if len(self.tables) != 0: + lines.append(' // Table IDs') + for tbl in self.tables: + lines.append(self.const_line(TBL_VAR_PREFIX, tbl[0], tbl[1])) + + if len(self.counters) != 0: + lines.append(' // Indirect Counter IDs') + for ctr in self.counters: + lines.append(self.const_line(CTR_VAR_PREFIX, ctr[0], ctr[1])) + + if len(self.direct_counters) != 0: + lines.append(' // Direct Counter IDs') + for dctr in self.direct_counters: + lines.append(self.const_line(DIRCTR_VAR_PREFIX, dctr[0], dctr[1])) + + if len(self.actions) != 0: + lines.append(' // Action IDs') + for act in self.actions: + lines.append(self.const_line(ACT_VAR_PREFIX, act[0], act[1])) + + if len(self.action_params) != 0: + lines.append(' // Action Param IDs') + for (tbl, act_prms) in self.action_params.items(): + for act_prm in act_prms: + lines.append(self.const_line(ACTPARAM_VAR_PREFIX, act_prm[0], act_prm[1], table_prefix=tbl)) + + if len(self.action_profiles) != 0: + lines.append(' // Action Profile IDs') + for act_prof in self.action_profiles: + lines.append(self.const_line(ACTPROF_VAR_PREFIX, act_prof[0], act_prof[1])) + + if len(self.packet_metadata) != 0: + lines.append(' // Packet Metadata IDs') + for pmeta in self.packet_metadata: + if not pmeta[0].startswith("_"): + lines.append(self.const_line(PACKETMETA_VAR_PREFIX, pmeta[0], pmeta[1])) + + if len(self.meters) != 0: + lines.append(' // Meter IDs') + for mtr in self.meters: + lines.append(self.const_line(MTR_VAR_PREFIX, mtr[0], mtr[1])) + lines.append(CONST_CLOSE) + # end of class + + return '\n'.join(lines) + +def main(): + parser = argparse.ArgumentParser(prog='go-gen-p4-const', + description='P4Info to Go constant generator.') + parser.add_argument('-o', '--output', help='path to output file', default='-') + parser.add_argument('-p', '--p4info', help='path to p4info file (text format)') + args = parser.parse_args() + + p4info_file = args.p4info + output_file = args.output + + pieces = args.output.split('/') + pkg_name = pieces[-2] if len(pieces) > 1 else 'undefined' + + p4info = p4info_pb2.P4Info() + with open(p4info_file, 'r') as input_file: + s = input_file.read() + tf.Merge(s, p4info) + + gen = ConstantClassGenerator(pkg_name) + gen.parse(p4info) + go_code = gen.generate_go() + + if output_file == '-': + # std output + print(go_code) + else: + with open(output_file, 'w') as output_file: + output_file.write(go_code) + + +if __name__ == '__main__': + main() From 8f87dbe8f9922bd889e4844eadcdc84d4a3f109c Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 17 Feb 2022 15:46:46 +0100 Subject: [PATCH 03/53] Refactor --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 0d37d325c..8ad2024ac 100644 --- a/Makefile +++ b/Makefile @@ -99,7 +99,7 @@ test: .coverage -v \ ./pfcpiface -p4info-go: +p4-constants: $(info *** Generating go constants...) @docker run --rm -v $(CURDIR):/app -w /app \ opennetworking/up4-ptf:latest python ./utils/go_gen_p4_const.py \ From 524fdb9eb298eb6f78ba3f5d1351879cf4461fb1 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 17 Feb 2022 17:22:10 +0100 Subject: [PATCH 04/53] Use p4 constants for tables and actions --- pfcpiface/p4rt_translator.go | 93 ++++++++---------------------------- pfcpiface/up4.go | 31 ++++-------- 2 files changed, 30 insertions(+), 94 deletions(-) diff --git a/pfcpiface/p4rt_translator.go b/pfcpiface/p4rt_translator.go index 817f5573a..7bb7a0156 100644 --- a/pfcpiface/p4rt_translator.go +++ b/pfcpiface/p4rt_translator.go @@ -10,6 +10,7 @@ import ( "math/bits" "net" + "github.com/omec-project/upf-epc/internal/p4constants" p4ConfigV1 "github.com/p4lang/p4runtime/go/p4/config/v1" p4 "github.com/p4lang/p4runtime/go/p4/v1" log "github.com/sirupsen/logrus" @@ -41,27 +42,6 @@ const ( FieldSessionMeterIndex = "session_meter_idx" FieldAppMeterIndex = "app_meter_idx" - TableInterfaces = "PreQosPipe.interfaces" - TableTunnelPeers = "PreQosPipe.tunnel_peers" - TableDownlinkTerminations = "PreQosPipe.terminations_downlink" - TableUplinkTerminations = "PreQosPipe.terminations_uplink" - TableDownlinkSessions = "PreQosPipe.sessions_downlink" - TableUplinkSessions = "PreQosPipe.sessions_uplink" - TableApplications = "PreQosPipe.applications" - - ActSetSourceIface = "PreQosPipe.set_source_iface" - ActSetUplinkSession = "PreQosPipe.set_session_uplink" - ActSetDownlinkSession = "PreQosPipe.set_session_downlink" - ActSetDownlinkSessionBuff = "PreQosPipe.set_session_downlink_buff" - ActUplinkTermDrop = "PreQosPipe.uplink_term_drop" - ActUplinkTermFwd = "PreQosPipe.uplink_term_fwd" - ActUplinkTermFwdNoTC = "PreQosPipe.uplink_term_fwd_no_tc" - ActDownlinkTermDrop = "PreQosPipe.downlink_term_drop" - ActDownlinkTermFwd = "PreQosPipe.downlink_term_fwd" - ActDownlinkTermFwdNoTC = "PreQosPipe.downlink_term_fwd_no_tc" - ActLoadTunnelParams = "PreQosPipe.load_tunnel_param" - ActSetAppID = "PreQosPipe.set_app_id" - DefaultPriority = 0 DefaultApplicationID = 0 // NoTC assuming bit<2> type of TC in UP4, @@ -132,16 +112,6 @@ func convertValueToBinary(value interface{}) ([]byte, error) { } } -func (t *P4rtTranslator) tableID(name string) uint32 { - for _, table := range t.p4Info.Tables { - if table.Preamble.Name == name { - return table.Preamble.Id - } - } - - return invalidID -} - func (t *P4rtTranslator) meterID(name string) uint32 { for _, meter := range t.p4Info.Meters { if meter.Preamble.Name == name { @@ -152,16 +122,6 @@ func (t *P4rtTranslator) meterID(name string) uint32 { return invalidID } -func (t *P4rtTranslator) actionID(name string) uint32 { - for _, action := range t.p4Info.Actions { - if action.Preamble.Name == name { - return action.Preamble.Id - } - } - - return invalidID -} - func (t *P4rtTranslator) getActionByID(actionID uint32) (*p4ConfigV1.Action, error) { for _, action := range t.p4Info.Actions { if action.Preamble.Id == actionID { @@ -494,10 +454,8 @@ func (t *P4rtTranslator) getLPMMatchFieldValue(tableEntry *p4.TableEntry, name s } func (t *P4rtTranslator) BuildInterfaceTableEntryNoAction() *p4.TableEntry { - tableID := t.tableID(TableInterfaces) - entry := &p4.TableEntry{ - TableId: tableID, + TableId: p4constants.Table_PreQosPipeInterfaces, Priority: DefaultPriority, } @@ -505,8 +463,6 @@ func (t *P4rtTranslator) BuildInterfaceTableEntryNoAction() *p4.TableEntry { } func (t *P4rtTranslator) BuildInterfaceTableEntry(ipNet *net.IPNet, isCore bool) (*p4.TableEntry, error) { - tableID := t.tableID(TableInterfaces) - srcIface := access direction := DirectionUplink @@ -516,7 +472,7 @@ func (t *P4rtTranslator) BuildInterfaceTableEntry(ipNet *net.IPNet, isCore bool) } entry := &p4.TableEntry{ - TableId: tableID, + TableId: p4constants.Table_PreQosPipeInterfaces, Priority: DefaultPriority, } @@ -526,7 +482,7 @@ func (t *P4rtTranslator) BuildInterfaceTableEntry(ipNet *net.IPNet, isCore bool) } action := &p4.Action{ - ActionId: t.actionID(ActSetSourceIface), + ActionId: p4constants.Action_PreQosPipeSetSourceIface, } if err := t.withActionParam(action, FieldSrcIface, srcIface); err != nil { @@ -555,9 +511,8 @@ func (t *P4rtTranslator) BuildApplicationsTableEntry(pdr pdr, internalAppID uint }) applicationsBuilderLog.Trace("Building P4rt table entry for applications table") - tableID := t.tableID(TableApplications) entry := &p4.TableEntry{ - TableId: tableID, + TableId: p4constants.Table_PreQosPipeApplications, // priority for UP4 cannot be greater than 65535 Priority: int32(math.MaxUint16 - pdr.precedence), } @@ -597,7 +552,7 @@ func (t *P4rtTranslator) BuildApplicationsTableEntry(pdr pdr, internalAppID uint } action := &p4.Action{ - ActionId: t.actionID(ActSetAppID), + ActionId: p4constants.Action_PreQosPipeSetAppId, } if err := t.withActionParam(action, FieldApplicationID, internalAppID); err != nil { @@ -621,10 +576,8 @@ func (t *P4rtTranslator) buildUplinkSessionsEntry(pdr pdr, sessMeterIdx uint32) }) uplinkBuilderLog.Trace("Building P4rt table entry for sessions_uplink table") - tableID := t.tableID(TableUplinkSessions) - entry := &p4.TableEntry{ - TableId: tableID, + TableId: p4constants.Table_PreQosPipeSessionsUplink, Priority: DefaultPriority, } @@ -637,7 +590,7 @@ func (t *P4rtTranslator) buildUplinkSessionsEntry(pdr pdr, sessMeterIdx uint32) } action := &p4.Action{ - ActionId: t.actionID(ActSetUplinkSession), + ActionId: p4constants.Action_PreQosPipeSetSessionUplink, } if err := t.withActionParam(action, FieldSessionMeterIndex, sessMeterIdx); err != nil { @@ -662,9 +615,8 @@ func (t *P4rtTranslator) buildDownlinkSessionsEntry(pdr pdr, sessMeterIdx uint32 }) builderLog.Trace("Building P4rt table entry for sessions_downlink table") - tableID := t.tableID(TableDownlinkSessions) entry := &p4.TableEntry{ - TableId: tableID, + TableId: p4constants.Table_PreQosPipeSessionsDownlink, Priority: DefaultPriority, } @@ -675,11 +627,11 @@ func (t *P4rtTranslator) buildDownlinkSessionsEntry(pdr pdr, sessMeterIdx uint32 var action *p4.Action if needsBuffering { action = &p4.Action{ - ActionId: t.actionID(ActSetDownlinkSessionBuff), + ActionId: p4constants.Action_PreQosPipeSetSessionDownlinkBuff, } } else { action = &p4.Action{ - ActionId: t.actionID(ActSetDownlinkSession), + ActionId: p4constants.Action_PreQosPipeSetSessionDownlink, } if err := t.withActionParam(action, FieldTunnelPeerID, tunnelPeerID); err != nil { return nil, err @@ -718,9 +670,8 @@ func (t *P4rtTranslator) buildUplinkTerminationsEntry(pdr pdr, appMeterIdx uint3 }) builderLog.Debug("Building P4rt table entry for UP4 terminations_uplink table") - tableID := t.tableID(TableUplinkTerminations) entry := &p4.TableEntry{ - TableId: tableID, + TableId: p4constants.Table_PreQosPipeTerminationsUplink, Priority: DefaultPriority, } @@ -735,11 +686,11 @@ func (t *P4rtTranslator) buildUplinkTerminationsEntry(pdr pdr, appMeterIdx uint3 var action *p4.Action if shouldDrop { action = &p4.Action{ - ActionId: t.actionID(ActUplinkTermDrop), + ActionId: p4constants.Action_PreQosPipeUplinkTermDrop, } } else if !shouldDrop && tc != NoTC { action = &p4.Action{ - ActionId: t.actionID(ActUplinkTermFwd), + ActionId: p4constants.Action_PreQosPipeUplinkTermFwd, } if err := t.withActionParam(action, FieldTrafficClass, tc); err != nil { @@ -750,7 +701,7 @@ func (t *P4rtTranslator) buildUplinkTerminationsEntry(pdr pdr, appMeterIdx uint3 } } else { action = &p4.Action{ - ActionId: t.actionID(ActUplinkTermFwdNoTC), + ActionId: p4constants.Action_PreQosPipeUplinkTermFwdNoTc, } if err := t.withActionParam(action, FieldAppMeterIndex, appMeterIdx); err != nil { return nil, err @@ -780,9 +731,8 @@ func (t *P4rtTranslator) buildDownlinkTerminationsEntry(pdr pdr, appMeterIdx uin }) builderLog.Debug("Building P4rt table entry for UP4 terminations_downlink table") - tableID := t.tableID(TableDownlinkTerminations) entry := &p4.TableEntry{ - TableId: tableID, + TableId: p4constants.Table_PreQosPipeTerminationsDownlink, Priority: DefaultPriority, } @@ -797,11 +747,11 @@ func (t *P4rtTranslator) buildDownlinkTerminationsEntry(pdr pdr, appMeterIdx uin var action *p4.Action if relatedFAR.Drops() { action = &p4.Action{ - ActionId: t.actionID(ActDownlinkTermDrop), + ActionId: p4constants.Action_PreQosPipeDownlinkTermDrop, } } else if !relatedFAR.Drops() && tc != NoTC { action = &p4.Action{ - ActionId: t.actionID(ActDownlinkTermFwd), + ActionId: p4constants.Action_PreQosPipeDownlinkTermFwd, } if err := t.withActionParam(action, FieldTEID, relatedFAR.tunnelTEID); err != nil { @@ -821,7 +771,7 @@ func (t *P4rtTranslator) buildDownlinkTerminationsEntry(pdr pdr, appMeterIdx uin } } else { action = &p4.Action{ - ActionId: t.actionID(ActDownlinkTermFwdNoTC), + ActionId: p4constants.Action_PreQosPipeDownlinkTermFwdNoTc, } if err := t.withActionParam(action, FieldTEID, relatedFAR.tunnelTEID); err != nil { @@ -868,14 +818,13 @@ func (t *P4rtTranslator) BuildGTPTunnelPeerTableEntry(tunnelPeerID uint8, tunnel }) builderLog.Trace("Building P4rt table entry for GTP Tunnel Peers table") - tableID := t.tableID(TableTunnelPeers) entry := &p4.TableEntry{ - TableId: tableID, + TableId: p4constants.Table_PreQosPipeTunnelPeers, Priority: DefaultPriority, Action: &p4.TableAction{ Type: &p4.TableAction_Action{ Action: &p4.Action{ - ActionId: t.actionID(ActLoadTunnelParams), + ActionId: p4constants.Action_PreQosPipeLoadTunnelParam, }, }, }, diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index 47572e0e8..d4ca83153 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -12,6 +12,7 @@ import ( "net" "time" + "github.com/omec-project/upf-epc/internal/p4constants" "google.golang.org/grpc/codes" p4 "github.com/p4lang/p4runtime/go/p4/v1" @@ -372,30 +373,16 @@ func (up4 *UP4) setUpfInfo(u *upf, conf *Conf) { } func (up4 *UP4) clearAllTables() error { - tables := []string{TableUplinkSessions, TableDownlinkSessions, TableUplinkTerminations, TableDownlinkTerminations, TableTunnelPeers, TableApplications} - tableIDs := make([]uint32, len(tables)) - - for _, table := range tables { - tableID, err := up4.p4RtTranslator.getTableIDByName(table) - if err != nil { - return err - } - - tableIDs = append(tableIDs, tableID) - } - - err := up4.p4client.ClearTables(tableIDs) - if err != nil { - return err + tableIDs := []uint32{ + p4constants.Table_PreQosPipeSessionsUplink, + p4constants.Table_PreQosPipeSessionsDownlink, + p4constants.Table_PreQosPipeTerminationsUplink, + p4constants.Table_PreQosPipeTerminationsDownlink, + p4constants.Table_PreQosPipeTunnelPeers, + p4constants.Table_PreQosPipeInterfaces, } - interfacesTableID, err := up4.p4RtTranslator.getTableIDByName(TableInterfaces) - if err != nil { - return err - } - - err = up4.p4client.ClearTable(interfacesTableID) - if err != nil { + if err := up4.p4client.ClearTables(tableIDs); err != nil { return err } From f9b0df03c2605ec50058fefc99edaaaf2fc86851 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 17 Feb 2022 16:55:44 +0100 Subject: [PATCH 05/53] Remove unused methods --- pfcpiface/p4rt_translator.go | 47 ++++++++++++++---------------------- pfcpiface/up4.go | 23 +++++++++--------- 2 files changed, 29 insertions(+), 41 deletions(-) diff --git a/pfcpiface/p4rt_translator.go b/pfcpiface/p4rt_translator.go index 7bb7a0156..e9a33f7e8 100644 --- a/pfcpiface/p4rt_translator.go +++ b/pfcpiface/p4rt_translator.go @@ -112,16 +112,6 @@ func convertValueToBinary(value interface{}) ([]byte, error) { } } -func (t *P4rtTranslator) meterID(name string) uint32 { - for _, meter := range t.p4Info.Meters { - if meter.Preamble.Name == name { - return meter.Preamble.Id - } - } - - return invalidID -} - func (t *P4rtTranslator) getActionByID(actionID uint32) (*p4ConfigV1.Action, error) { for _, action := range t.p4Info.Actions { if action.Preamble.Id == actionID { @@ -142,34 +132,24 @@ func (t *P4rtTranslator) getTableByID(tableID uint32) (*p4ConfigV1.Table, error) return nil, ErrNotFoundWithParam("table", "ID", tableID) } -func (t *P4rtTranslator) getTableIDByName(name string) (uint32, error) { - for _, table := range t.p4Info.Tables { - if table.Preamble.Name == name { - return table.Preamble.Id, nil - } - } - - return 0, ErrNotFoundWithParam("table", "name", name) -} - -func (t *P4rtTranslator) getCounterByName(name string) (*p4ConfigV1.Counter, error) { +func (t *P4rtTranslator) getCounterByID(ID uint32) (*p4ConfigV1.Counter, error) { for _, ctr := range t.p4Info.Counters { - if ctr.Preamble.Name == name { + if ctr.Preamble.Id == ID { return ctr, nil } } - return nil, ErrNotFoundWithParam("counter", "name", name) + return nil, ErrNotFoundWithParam("counter", "ID", ID) } -func (t *P4rtTranslator) getMeterByName(name string) (*p4ConfigV1.Meter, error) { +func (t *P4rtTranslator) getMeterByID(ID uint32) (*p4ConfigV1.Meter, error) { for _, mtr := range t.p4Info.Meters { - if mtr.Preamble.Name == name { + if mtr.Preamble.Id == ID { return mtr, nil } } - return nil, ErrNotFoundWithParam("meter", "name", name) + return nil, ErrNotFoundWithParam("meter", "ID", ID) } //nolint:unused @@ -851,14 +831,23 @@ func (t *P4rtTranslator) BuildGTPTunnelPeerTableEntry(tunnelPeerID uint8, tunnel return entry, nil } -func (t *P4rtTranslator) BuildMeterEntry(meter string, cellID uint32, config *p4.MeterConfig) *p4.MeterEntry { +func (t *P4rtTranslator) BuildMeterEntry(meterID uint32, cellID uint32, config *p4.MeterConfig) *p4.MeterEntry { + meterName := "" + + switch meterID { + case p4constants.Meter_PreQosPipeAppMeter: + meterName = applicationMeter + break + case p4constants.Meter_PreQosPipeSessionMeter: + meterName = sessionMeter + } + builderLog := log.WithFields(log.Fields{ - "Meter": meter, + "Meter": meterName, "Cell ID": cellID, }) builderLog.Trace("Building Meter entry") - meterID := t.meterID(meter) entry := &p4.MeterEntry{ MeterId: meterID, Index: &p4.Index{Index: int64(cellID)}, diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index d4ca83153..c0e676976 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -134,7 +134,7 @@ func (up4 *UP4) portStats(uc *upfCollector, ch chan<- prometheus.Metric) { } func (up4 *UP4) initCounter(counterID uint8, name string) error { - ctr, err := up4.p4RtTranslator.getCounterByName(name) + ctr, err := up4.p4RtTranslator.getCounterByID(uint32(counterID)) if err != nil { return err } @@ -231,7 +231,7 @@ func (up4 *UP4) initAllCounters() error { func (up4 *UP4) initMetersPools() error { log.Debug("Initializing P4 Meters pools for UP4") - appMeter, err := up4.p4RtTranslator.getMeterByName(applicationMeter) + appMeter, err := up4.p4RtTranslator.getMeterByID(p4constants.Meter_PreQosPipeAppMeter) if err != nil { return err } @@ -248,7 +248,7 @@ func (up4 *UP4) initMetersPools() error { log.Trace("Application meter IDs pool initialized: ", up4.appMeterCellIDsPool.String()) - sessMeter, err := up4.p4RtTranslator.getMeterByName(sessionMeter) + sessMeter, err := up4.p4RtTranslator.getMeterByID(p4constants.Meter_PreQosPipeSessionMeter) if err != nil { return err } @@ -857,7 +857,7 @@ func (up4 *UP4) configureApplicationMeter(q qer, bidirectional bool) (meter, err // according to the SD-Core/SD-Fabric contract, UL and DL MBRs/GBRs are always equal for Application QERs. meterConfig := getMeterConfigurationFromQER(q.ulMbr, q.ulGbr) - meterEntry := up4.p4RtTranslator.BuildMeterEntry(applicationMeter, appMeter.uplinkCellID, meterConfig) + meterEntry := up4.p4RtTranslator.BuildMeterEntry(p4constants.Meter_PreQosPipeAppMeter, appMeter.uplinkCellID, meterConfig) entries = append(entries, meterEntry) } @@ -866,7 +866,7 @@ func (up4 *UP4) configureApplicationMeter(q qer, bidirectional bool) (meter, err // according to the SD-Core/SD-Fabric contract, UL and DL MBRs/GBRs are always equal for Application QERs. meterConfig := getMeterConfigurationFromQER(q.dlMbr, q.dlGbr) - meterEntry := up4.p4RtTranslator.BuildMeterEntry(applicationMeter, appMeter.downlinkCellID, meterConfig) + meterEntry := up4.p4RtTranslator.BuildMeterEntry(p4constants.Meter_PreQosPipeAppMeter, appMeter.downlinkCellID, meterConfig) entries = append(entries, meterEntry) } @@ -907,10 +907,10 @@ func (up4 *UP4) configureSessionMeter(q qer) (meter, error) { logger.Debug("Configuring Session Meter from QER") uplinkMeterConfig := getMeterConfigurationFromQER(q.ulMbr, q.ulGbr) - uplinkMeterEntry := up4.p4RtTranslator.BuildMeterEntry(sessionMeter, uplinkCellID, uplinkMeterConfig) + uplinkMeterEntry := up4.p4RtTranslator.BuildMeterEntry(p4constants.Meter_PreQosPipeSessionMeter, uplinkCellID, uplinkMeterConfig) downlinkMeterConfig := getMeterConfigurationFromQER(q.dlMbr, q.dlGbr) - downlinkMeterEntry := up4.p4RtTranslator.BuildMeterEntry(sessionMeter, downlinkCellID, downlinkMeterConfig) + downlinkMeterEntry := up4.p4RtTranslator.BuildMeterEntry(p4constants.Meter_PreQosPipeSessionMeter, downlinkCellID, downlinkMeterConfig) logger = logger.WithFields(log.Fields{ "uplink meter entry": uplinkMeterEntry, @@ -993,8 +993,7 @@ func verifyPDR(pdr pdr) error { return nil } -func (up4 *UP4) resetMeter(name string, meter meter) { - meterID := up4.p4RtTranslator.meterID(name) +func (up4 *UP4) resetMeter(meterID uint32, meter meter) { entries := make([]*p4.MeterEntry, 0, 2) entry := &p4.MeterEntry{ @@ -1014,7 +1013,7 @@ func (up4 *UP4) resetMeter(name string, meter meter) { err := up4.p4client.ApplyMeterEntries(p4.Update_MODIFY, entries...) if err != nil { - log.Errorf("Failed to reset %v meter entries: %v", name, err) + log.Errorf("Failed to reset %v meter entries: %v", meterID, err) } } @@ -1039,14 +1038,14 @@ func (up4 *UP4) resetMeters(qers []qer) { } if meter.meterType == meterTypeApplication { - up4.resetMeter(applicationMeter, meter) + up4.resetMeter(p4constants.Meter_PreQosPipeAppMeter, meter) up4.releaseAppMeterCellID(meter.uplinkCellID) if meter.downlinkCellID != meter.uplinkCellID { up4.releaseAppMeterCellID(meter.downlinkCellID) } } else if meter.meterType == meterTypeSession { - up4.resetMeter(sessionMeter, meter) + up4.resetMeter(p4constants.Meter_PreQosPipeSessionMeter, meter) up4.releaseSessionMeterCellID(meter.uplinkCellID) up4.releaseSessionMeterCellID(meter.downlinkCellID) } From 52ce4dd949868a0d841e18b3dc428410c265bd23 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 17 Feb 2022 17:18:14 +0100 Subject: [PATCH 06/53] Fix golint --- pfcpiface/p4rt_translator.go | 1 - 1 file changed, 1 deletion(-) diff --git a/pfcpiface/p4rt_translator.go b/pfcpiface/p4rt_translator.go index e9a33f7e8..ec0f86346 100644 --- a/pfcpiface/p4rt_translator.go +++ b/pfcpiface/p4rt_translator.go @@ -837,7 +837,6 @@ func (t *P4rtTranslator) BuildMeterEntry(meterID uint32, cellID uint32, config * switch meterID { case p4constants.Meter_PreQosPipeAppMeter: meterName = applicationMeter - break case p4constants.Meter_PreQosPipeSessionMeter: meterName = sessionMeter } From a9c9a5e5dfa1ef3da82bac8e8fa732c1640e77d1 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 17 Feb 2022 17:20:00 +0100 Subject: [PATCH 07/53] Add go.sum --- go.sum | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.sum b/go.sum index f3d12ed39..9dfcc730c 100644 --- a/go.sum +++ b/go.sum @@ -71,8 +71,6 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/alexflint/go-filemutex v0.0.0-20171022225611-72bdc8eae2ae/go.mod h1:CgnQgUtFrFz9mxFNtED3jI5tLDjKlOM+oUF/sTk6ps0= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= -github.com/antoninbas/p4runtime-go-client v0.0.0-20220127195713-1c9d91589da9 h1:7EfrltzBDB2zoM78xFB6X/0mVUt3xNULCEv8eMA3JRE= -github.com/antoninbas/p4runtime-go-client v0.0.0-20220127195713-1c9d91589da9/go.mod h1:NzHBH6Ig6nPZ/yd7HG/d+2J2r9afJYdLzi1VBekDJPo= github.com/antoninbas/p4runtime-go-client v0.0.0-20220204221603-49eba9f248c1 h1:K/xygi8gBo3tFCs2577fIYuEZMtPDebQQz5T7f5teqE= github.com/antoninbas/p4runtime-go-client v0.0.0-20220204221603-49eba9f248c1/go.mod h1:NzHBH6Ig6nPZ/yd7HG/d+2J2r9afJYdLzi1VBekDJPo= github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8= From fd46518acdaca5eaa07686016fb8b9fc62dc006c Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 17 Feb 2022 19:24:18 +0100 Subject: [PATCH 08/53] Move script to scripts package --- Makefile | 2 +- {utils => scripts}/go_gen_p4_const.py | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename {utils => scripts}/go_gen_p4_const.py (100%) diff --git a/Makefile b/Makefile index 8ad2024ac..43a7a1a01 100644 --- a/Makefile +++ b/Makefile @@ -102,7 +102,7 @@ test: .coverage p4-constants: $(info *** Generating go constants...) @docker run --rm -v $(CURDIR):/app -w /app \ - opennetworking/up4-ptf:latest python ./utils/go_gen_p4_const.py \ + opennetworking/up4-ptf:latest python ./scripts/go_gen_p4_const.py \ --output internal/p4constants/p4constants.go --p4info conf/p4/bin/p4info.txt @docker run --rm -v $(CURDIR):/app -w /app \ golang:1.16-alpine gofmt -w internal/p4constants/p4constants.go diff --git a/utils/go_gen_p4_const.py b/scripts/go_gen_p4_const.py similarity index 100% rename from utils/go_gen_p4_const.py rename to scripts/go_gen_p4_const.py From d76240c9b210a8f7769f8a5cb2f4731655cb1431 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Fri, 18 Feb 2022 22:47:49 +0100 Subject: [PATCH 09/53] Sync with latest changes --- pfcpiface/p4rt_translator.go | 44 ++++++++++++++++++------------------ pfcpiface/up4.go | 28 +++++++++++------------ 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/pfcpiface/p4rt_translator.go b/pfcpiface/p4rt_translator.go index ec0f86346..b699a8155 100644 --- a/pfcpiface/p4rt_translator.go +++ b/pfcpiface/p4rt_translator.go @@ -435,7 +435,7 @@ func (t *P4rtTranslator) getLPMMatchFieldValue(tableEntry *p4.TableEntry, name s func (t *P4rtTranslator) BuildInterfaceTableEntryNoAction() *p4.TableEntry { entry := &p4.TableEntry{ - TableId: p4constants.Table_PreQosPipeInterfaces, + TableId: p4constants.TablePreQosPipeInterfaces, Priority: DefaultPriority, } @@ -452,7 +452,7 @@ func (t *P4rtTranslator) BuildInterfaceTableEntry(ipNet *net.IPNet, isCore bool) } entry := &p4.TableEntry{ - TableId: p4constants.Table_PreQosPipeInterfaces, + TableId: p4constants.TablePreQosPipeInterfaces, Priority: DefaultPriority, } @@ -462,7 +462,7 @@ func (t *P4rtTranslator) BuildInterfaceTableEntry(ipNet *net.IPNet, isCore bool) } action := &p4.Action{ - ActionId: p4constants.Action_PreQosPipeSetSourceIface, + ActionId: p4constants.ActionPreQosPipeSetSourceIface, } if err := t.withActionParam(action, FieldSrcIface, srcIface); err != nil { @@ -492,7 +492,7 @@ func (t *P4rtTranslator) BuildApplicationsTableEntry(pdr pdr, internalAppID uint applicationsBuilderLog.Trace("Building P4rt table entry for applications table") entry := &p4.TableEntry{ - TableId: p4constants.Table_PreQosPipeApplications, + TableId: p4constants.TablePreQosPipeApplications, // priority for UP4 cannot be greater than 65535 Priority: int32(math.MaxUint16 - pdr.precedence), } @@ -532,7 +532,7 @@ func (t *P4rtTranslator) BuildApplicationsTableEntry(pdr pdr, internalAppID uint } action := &p4.Action{ - ActionId: p4constants.Action_PreQosPipeSetAppId, + ActionId: p4constants.ActionPreQosPipeSetAppId, } if err := t.withActionParam(action, FieldApplicationID, internalAppID); err != nil { @@ -557,7 +557,7 @@ func (t *P4rtTranslator) buildUplinkSessionsEntry(pdr pdr, sessMeterIdx uint32) uplinkBuilderLog.Trace("Building P4rt table entry for sessions_uplink table") entry := &p4.TableEntry{ - TableId: p4constants.Table_PreQosPipeSessionsUplink, + TableId: p4constants.TablePreQosPipeSessionsUplink, Priority: DefaultPriority, } @@ -570,7 +570,7 @@ func (t *P4rtTranslator) buildUplinkSessionsEntry(pdr pdr, sessMeterIdx uint32) } action := &p4.Action{ - ActionId: p4constants.Action_PreQosPipeSetSessionUplink, + ActionId: p4constants.ActionPreQosPipeSetSessionUplink, } if err := t.withActionParam(action, FieldSessionMeterIndex, sessMeterIdx); err != nil { @@ -596,7 +596,7 @@ func (t *P4rtTranslator) buildDownlinkSessionsEntry(pdr pdr, sessMeterIdx uint32 builderLog.Trace("Building P4rt table entry for sessions_downlink table") entry := &p4.TableEntry{ - TableId: p4constants.Table_PreQosPipeSessionsDownlink, + TableId: p4constants.TablePreQosPipeSessionsDownlink, Priority: DefaultPriority, } @@ -607,11 +607,11 @@ func (t *P4rtTranslator) buildDownlinkSessionsEntry(pdr pdr, sessMeterIdx uint32 var action *p4.Action if needsBuffering { action = &p4.Action{ - ActionId: p4constants.Action_PreQosPipeSetSessionDownlinkBuff, + ActionId: p4constants.ActionPreQosPipeSetSessionDownlinkBuff, } } else { action = &p4.Action{ - ActionId: p4constants.Action_PreQosPipeSetSessionDownlink, + ActionId: p4constants.ActionPreQosPipeSetSessionDownlink, } if err := t.withActionParam(action, FieldTunnelPeerID, tunnelPeerID); err != nil { return nil, err @@ -651,7 +651,7 @@ func (t *P4rtTranslator) buildUplinkTerminationsEntry(pdr pdr, appMeterIdx uint3 builderLog.Debug("Building P4rt table entry for UP4 terminations_uplink table") entry := &p4.TableEntry{ - TableId: p4constants.Table_PreQosPipeTerminationsUplink, + TableId: p4constants.TablePreQosPipeTerminationsUplink, Priority: DefaultPriority, } @@ -666,11 +666,11 @@ func (t *P4rtTranslator) buildUplinkTerminationsEntry(pdr pdr, appMeterIdx uint3 var action *p4.Action if shouldDrop { action = &p4.Action{ - ActionId: p4constants.Action_PreQosPipeUplinkTermDrop, + ActionId: p4constants.ActionPreQosPipeUplinkTermDrop, } } else if !shouldDrop && tc != NoTC { action = &p4.Action{ - ActionId: p4constants.Action_PreQosPipeUplinkTermFwd, + ActionId: p4constants.ActionPreQosPipeUplinkTermDrop, } if err := t.withActionParam(action, FieldTrafficClass, tc); err != nil { @@ -681,7 +681,7 @@ func (t *P4rtTranslator) buildUplinkTerminationsEntry(pdr pdr, appMeterIdx uint3 } } else { action = &p4.Action{ - ActionId: p4constants.Action_PreQosPipeUplinkTermFwdNoTc, + ActionId: p4constants.ActionPreQosPipeDownlinkTermFwdNoTc, } if err := t.withActionParam(action, FieldAppMeterIndex, appMeterIdx); err != nil { return nil, err @@ -712,7 +712,7 @@ func (t *P4rtTranslator) buildDownlinkTerminationsEntry(pdr pdr, appMeterIdx uin builderLog.Debug("Building P4rt table entry for UP4 terminations_downlink table") entry := &p4.TableEntry{ - TableId: p4constants.Table_PreQosPipeTerminationsDownlink, + TableId: p4constants.TablePreQosPipeTerminationsDownlink, Priority: DefaultPriority, } @@ -727,11 +727,11 @@ func (t *P4rtTranslator) buildDownlinkTerminationsEntry(pdr pdr, appMeterIdx uin var action *p4.Action if relatedFAR.Drops() { action = &p4.Action{ - ActionId: p4constants.Action_PreQosPipeDownlinkTermDrop, + ActionId: p4constants.ActionPreQosPipeDownlinkTermDrop, } } else if !relatedFAR.Drops() && tc != NoTC { action = &p4.Action{ - ActionId: p4constants.Action_PreQosPipeDownlinkTermFwd, + ActionId: p4constants.ActionPreQosPipeDownlinkTermFwd, } if err := t.withActionParam(action, FieldTEID, relatedFAR.tunnelTEID); err != nil { @@ -751,7 +751,7 @@ func (t *P4rtTranslator) buildDownlinkTerminationsEntry(pdr pdr, appMeterIdx uin } } else { action = &p4.Action{ - ActionId: p4constants.Action_PreQosPipeDownlinkTermFwdNoTc, + ActionId: p4constants.ActionPreQosPipeDownlinkTermFwdNoTc, } if err := t.withActionParam(action, FieldTEID, relatedFAR.tunnelTEID); err != nil { @@ -799,12 +799,12 @@ func (t *P4rtTranslator) BuildGTPTunnelPeerTableEntry(tunnelPeerID uint8, tunnel builderLog.Trace("Building P4rt table entry for GTP Tunnel Peers table") entry := &p4.TableEntry{ - TableId: p4constants.Table_PreQosPipeTunnelPeers, + TableId: p4constants.TablePreQosPipeTunnelPeers, Priority: DefaultPriority, Action: &p4.TableAction{ Type: &p4.TableAction_Action{ Action: &p4.Action{ - ActionId: p4constants.Action_PreQosPipeLoadTunnelParam, + ActionId: p4constants.ActionPreQosPipeLoadTunnelParam, }, }, }, @@ -835,9 +835,9 @@ func (t *P4rtTranslator) BuildMeterEntry(meterID uint32, cellID uint32, config * meterName := "" switch meterID { - case p4constants.Meter_PreQosPipeAppMeter: + case p4constants.MeterPreQosPipeAppMeter: meterName = applicationMeter - case p4constants.Meter_PreQosPipeSessionMeter: + case p4constants.MeterPreQosPipeSessionMeter: meterName = sessionMeter } diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index a1b3b4599..da7370910 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -236,7 +236,7 @@ func (up4 *UP4) initAllCounters() error { func (up4 *UP4) initMetersPools() error { log.Debug("Initializing P4 Meters pools for UP4") - appMeter, err := up4.p4RtTranslator.getMeterByID(p4constants.Meter_PreQosPipeAppMeter) + appMeter, err := up4.p4RtTranslator.getMeterByID(p4constants.MeterPreQosPipeAppMeter) if err != nil { return err } @@ -253,7 +253,7 @@ func (up4 *UP4) initMetersPools() error { log.Trace("Application meter IDs pool initialized: ", up4.appMeterCellIDsPool.String()) - sessMeter, err := up4.p4RtTranslator.getMeterByID(p4constants.Meter_PreQosPipeSessionMeter) + sessMeter, err := up4.p4RtTranslator.getMeterByID(p4constants.MeterPreQosPipeSessionMeter) if err != nil { return err } @@ -379,12 +379,12 @@ func (up4 *UP4) setUpfInfo(u *upf, conf *Conf) { func (up4 *UP4) clearAllTables() error { tableIDs := []uint32{ - p4constants.Table_PreQosPipeSessionsUplink, - p4constants.Table_PreQosPipeSessionsDownlink, - p4constants.Table_PreQosPipeTerminationsUplink, - p4constants.Table_PreQosPipeTerminationsDownlink, - p4constants.Table_PreQosPipeTunnelPeers, - p4constants.Table_PreQosPipeInterfaces, + p4constants.TablePreQosPipeSessionsUplink, + p4constants.TablePreQosPipeSessionsDownlink, + p4constants.TablePreQosPipeTerminationsUplink, + p4constants.TablePreQosPipeTerminationsDownlink, + p4constants.TablePreQosPipeTunnelPeers, + p4constants.TablePreQosPipeInterfaces, } if err := up4.p4client.ClearTables(tableIDs); err != nil { @@ -862,7 +862,7 @@ func (up4 *UP4) configureApplicationMeter(q qer, bidirectional bool) (meter, err // according to the SD-Core/SD-Fabric contract, UL and DL MBRs/GBRs are always equal for Application QERs. meterConfig := getMeterConfigurationFromQER(q.ulMbr, q.ulGbr) - meterEntry := up4.p4RtTranslator.BuildMeterEntry(p4constants.Meter_PreQosPipeAppMeter, appMeter.uplinkCellID, meterConfig) + meterEntry := up4.p4RtTranslator.BuildMeterEntry(p4constants.MeterPreQosPipeAppMeter, appMeter.uplinkCellID, meterConfig) entries = append(entries, meterEntry) } @@ -871,7 +871,7 @@ func (up4 *UP4) configureApplicationMeter(q qer, bidirectional bool) (meter, err // according to the SD-Core/SD-Fabric contract, UL and DL MBRs/GBRs are always equal for Application QERs. meterConfig := getMeterConfigurationFromQER(q.dlMbr, q.dlGbr) - meterEntry := up4.p4RtTranslator.BuildMeterEntry(p4constants.Meter_PreQosPipeAppMeter, appMeter.downlinkCellID, meterConfig) + meterEntry := up4.p4RtTranslator.BuildMeterEntry(p4constants.MeterPreQosPipeAppMeter, appMeter.downlinkCellID, meterConfig) entries = append(entries, meterEntry) } @@ -912,10 +912,10 @@ func (up4 *UP4) configureSessionMeter(q qer) (meter, error) { logger.Debug("Configuring Session Meter from QER") uplinkMeterConfig := getMeterConfigurationFromQER(q.ulMbr, q.ulGbr) - uplinkMeterEntry := up4.p4RtTranslator.BuildMeterEntry(p4constants.Meter_PreQosPipeSessionMeter, uplinkCellID, uplinkMeterConfig) + uplinkMeterEntry := up4.p4RtTranslator.BuildMeterEntry(p4constants.MeterPreQosPipeSessionMeter, uplinkCellID, uplinkMeterConfig) downlinkMeterConfig := getMeterConfigurationFromQER(q.dlMbr, q.dlGbr) - downlinkMeterEntry := up4.p4RtTranslator.BuildMeterEntry(p4constants.Meter_PreQosPipeSessionMeter, downlinkCellID, downlinkMeterConfig) + downlinkMeterEntry := up4.p4RtTranslator.BuildMeterEntry(p4constants.MeterPreQosPipeSessionMeter, downlinkCellID, downlinkMeterConfig) logger = logger.WithFields(log.Fields{ "uplink meter entry": uplinkMeterEntry, @@ -1043,14 +1043,14 @@ func (up4 *UP4) resetMeters(qers []qer) { } if meter.meterType == meterTypeApplication { - up4.resetMeter(p4constants.Meter_PreQosPipeAppMeter, meter) + up4.resetMeter(p4constants.MeterPreQosPipeAppMeter, meter) up4.releaseAppMeterCellID(meter.uplinkCellID) if meter.downlinkCellID != meter.uplinkCellID { up4.releaseAppMeterCellID(meter.downlinkCellID) } } else if meter.meterType == meterTypeSession { - up4.resetMeter(p4constants.Meter_PreQosPipeSessionMeter, meter) + up4.resetMeter(p4constants.MeterPreQosPipeSessionMeter, meter) up4.releaseSessionMeterCellID(meter.uplinkCellID) up4.releaseSessionMeterCellID(meter.downlinkCellID) } From 969f8535f5a5bc6e833894cdcb67636162e42459 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Fri, 18 Feb 2022 22:49:15 +0100 Subject: [PATCH 10/53] Remove old script --- scripts/go_gen_p4_const.py | 193 ------------------------------------- 1 file changed, 193 deletions(-) delete mode 100755 scripts/go_gen_p4_const.py diff --git a/scripts/go_gen_p4_const.py b/scripts/go_gen_p4_const.py deleted file mode 100755 index c34b773b4..000000000 --- a/scripts/go_gen_p4_const.py +++ /dev/null @@ -1,193 +0,0 @@ -# Copyright 2022-present Open Networking Foundation -# -# SPDX-License-Identifier: Apache-2.0 - -import argparse -import google.protobuf.text_format as tf -import re -from p4.config.v1 import p4info_pb2 - -copyright = '''/* - * Copyright 2022-present Open Networking Foundation - * - * SPDX-License-Identifier: Apache-2.0 - */ -''' - -PKG_FMT = 'package %s' - -CONST_OPEN = '''//noinspection GoSnakeCaseUsage - const (''' -CONST_CLOSE = ')' - - -UINT32 = 'uint32' -EMPTY_STR = '' - -HF_VAR_PREFIX = 'Hdr_' -TBL_VAR_PREFIX = 'Table_' -CTR_VAR_PREFIX = 'Counter_' -DIRCTR_VAR_PREFIX = 'DirectCounter_' -ACT_VAR_PREFIX = "Action_" -ACTPARAM_VAR_PREFIX = "ActionParam_" -ACTPROF_VAR_PREFIX = "ActionProfile_" -PACKETMETA_VAR_PREFIX = "PacketMeta_" -MTR_VAR_PREFIX = "Meter_" - -class ConstantClassGenerator(object): - header_fields = dict() - tables = set() - counters = set() - direct_counters = set() - actions = set() - action_params = dict() - action_profiles = set() - packet_metadata = set() - meters = set() - - # https://stackoverflow.com/questions/1175208/elegant-python-function-to-convert-camelcase-to-snake-case - def convert_camel_to_all_caps(self, name): - s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name) - s1 = re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).title() - return s1.replace('_', '').replace('.', '') - - def __init__(self, pkg_name): - self.package_name = PKG_FMT % (pkg_name, ) - - def parse(self, p4info): - for tbl in p4info.tables: - for mf in tbl.match_fields: - try: - self.header_fields[tbl.preamble.name] - except KeyError: - self.header_fields[tbl.preamble.name] = set() - finally: - self.header_fields[tbl.preamble.name].add((mf.name, mf.id)) - - self.tables.add((tbl.preamble.name, tbl.preamble.id)) - - for ctr in p4info.counters: - self.counters.add((ctr.preamble.name, ctr.preamble.id)) - - for dir_ctr in p4info.direct_counters: - self.direct_counters.add((dir_ctr.preamble.name, dir_ctr.preamble.id)) - - for act in p4info.actions: - self.actions.add((act.preamble.name, act.preamble.id)) - - for param in act.params: - try: - self.action_params[act.preamble.name] - except KeyError as e: - self.action_params[act.preamble.name] = set() - finally: - self.action_params[act.preamble.name].add((param.name, param.id)) - - for act_prof in p4info.action_profiles: - self.action_profiles.add((act_prof.preamble.name, act_prof.preamble.id)) - - for cpm in p4info.controller_packet_metadata: - for mta in cpm.metadata: - self.packet_metadata.add((mta.name, mta.id)) - for mtr in p4info.meters: - self.meters.add((mtr.preamble.name, mtr.preamble.id)) - - def const_line(self, obj_type, name, id, var_type=UINT32, table_prefix=None): - var_name = self.convert_camel_to_all_caps(name) - if table_prefix: - var_name = self.convert_camel_to_all_caps(table_prefix) + "_" + var_name - var_name = obj_type+var_name - base_line = "\t%s %s = %i" - line = base_line % (var_name, var_type, id) - return line - - def generate_go(self): - lines = list() - lines.append(copyright) - lines.append(self.package_name) - lines.append(CONST_OPEN) - - if len(self.header_fields) != 0: - lines.append(' // Header field IDs') - for (tbl, hfs) in self.header_fields.items(): - for hf in hfs: - lines.append(self.const_line(HF_VAR_PREFIX, hf[0], hf[1], table_prefix=tbl)) - - if len(self.tables) != 0: - lines.append(' // Table IDs') - for tbl in self.tables: - lines.append(self.const_line(TBL_VAR_PREFIX, tbl[0], tbl[1])) - - if len(self.counters) != 0: - lines.append(' // Indirect Counter IDs') - for ctr in self.counters: - lines.append(self.const_line(CTR_VAR_PREFIX, ctr[0], ctr[1])) - - if len(self.direct_counters) != 0: - lines.append(' // Direct Counter IDs') - for dctr in self.direct_counters: - lines.append(self.const_line(DIRCTR_VAR_PREFIX, dctr[0], dctr[1])) - - if len(self.actions) != 0: - lines.append(' // Action IDs') - for act in self.actions: - lines.append(self.const_line(ACT_VAR_PREFIX, act[0], act[1])) - - if len(self.action_params) != 0: - lines.append(' // Action Param IDs') - for (tbl, act_prms) in self.action_params.items(): - for act_prm in act_prms: - lines.append(self.const_line(ACTPARAM_VAR_PREFIX, act_prm[0], act_prm[1], table_prefix=tbl)) - - if len(self.action_profiles) != 0: - lines.append(' // Action Profile IDs') - for act_prof in self.action_profiles: - lines.append(self.const_line(ACTPROF_VAR_PREFIX, act_prof[0], act_prof[1])) - - if len(self.packet_metadata) != 0: - lines.append(' // Packet Metadata IDs') - for pmeta in self.packet_metadata: - if not pmeta[0].startswith("_"): - lines.append(self.const_line(PACKETMETA_VAR_PREFIX, pmeta[0], pmeta[1])) - - if len(self.meters) != 0: - lines.append(' // Meter IDs') - for mtr in self.meters: - lines.append(self.const_line(MTR_VAR_PREFIX, mtr[0], mtr[1])) - lines.append(CONST_CLOSE) - # end of class - - return '\n'.join(lines) - -def main(): - parser = argparse.ArgumentParser(prog='go-gen-p4-const', - description='P4Info to Go constant generator.') - parser.add_argument('-o', '--output', help='path to output file', default='-') - parser.add_argument('-p', '--p4info', help='path to p4info file (text format)') - args = parser.parse_args() - - p4info_file = args.p4info - output_file = args.output - - pieces = args.output.split('/') - pkg_name = pieces[-2] if len(pieces) > 1 else 'undefined' - - p4info = p4info_pb2.P4Info() - with open(p4info_file, 'r') as input_file: - s = input_file.read() - tf.Merge(s, p4info) - - gen = ConstantClassGenerator(pkg_name) - gen.parse(p4info) - go_code = gen.generate_go() - - if output_file == '-': - # std output - print(go_code) - else: - with open(output_file, 'w') as output_file: - output_file.write(go_code) - - -if __name__ == '__main__': - main() From 513b0aee7616e4ed332b9579d91314a922d88cb3 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Mon, 21 Feb 2022 17:01:15 +0100 Subject: [PATCH 11/53] * make consts private * build maps and lists of IDs --- scripts/go_gen_p4_const.go | 183 ++++++++++++++++++++++++++----------- 1 file changed, 132 insertions(+), 51 deletions(-) diff --git a/scripts/go_gen_p4_const.go b/scripts/go_gen_p4_const.go index a18f9d49b..16532ad93 100644 --- a/scripts/go_gen_p4_const.go +++ b/scripts/go_gen_p4_const.go @@ -16,109 +16,190 @@ import ( ) const ( - P4infoPath = "conf/p4/bin/p4info.txt" + p4infoPath = "conf/p4/bin/p4info.txt" - DefaultPackageName = "p4constants" - // CopyrightHeader uses raw strings to avoid issues with reuse - CopyrightHeader = `// SPDX-License-Identifier: Apache-2.0 + defaultPackageName = "p4constants" + // copyrightHeader uses raw strings to avoid issues with reuse + copyrightHeader = `// SPDX-License-Identifier: Apache-2.0 // Copyright 2022-present Open Networking Foundation ` - ConstOpen = "//noinspection GoSnakeCaseUsage\nconst (\n" - ConstClose = ")" - - IdTypeString = "uint32" - - HfVarPrefix = "Hdr_" - TblVarPrefix = "Table_" - CtrVarPrefix = "Counter_" - DirctrVarPrefix = "DirectCounter_" - ActVarPrefix = "Action_" - ActparamVarPrefix = "ActionParam_" - ActprofVarPrefix = "ActionProfile_" - PacketmetaVarPrefix = "PacketMeta_" - MtrVarPrefix = "Meter_" + constOpen = "//noinspection GoSnakeCaseUsage\nconst (\n" + varOpen = "var (\n" + mapFormatString = "%v:\"%v\",\n" + listFormatString = "%v,\n" + constOrVarClose = ")\n" + + idTypeString = "uint32" + sizeTypeString = "uint64" + + hfVarPrefix = "Hdr_" + tblVarPrefix = "Table_" + ctrVarPrefix = "Counter_" + ctrSizeVarPrefix = "CounterSize_" + dirCtrVarPrefix = "DirectCounter_" + actVarPrefix = "Action_" + actparamVarPrefix = "ActionParam_" + actprofVarPrefix = "ActionProfile_" + packetmetaVarPrefix = "PacketMeta_" + mtrVarPrefix = "Meter_" + mtrSizeVarPrefix = "MeterSize_" + + tableMapDeclaration = "TableIDToNameMap = map[uint32]string {\n" + tableListDeclaration = "TableIDList = []uint32 {\n" + actionMapDeclaration = "ActionIDToNameMap = map[uint32]string {\n" + actionListDeclaration = "ActionIDList = []uint32 {\n" + counterMapDeclaration = "CounterIDToNameMap = map[uint32]string {\n" + counterListDeclaration = "CounterIDList = []uint32 {\n" + directCounterMapDeclaration = "DirectCounterIDToNameMap = map[uint32]string {\n" + directCounterListDeclaration = "DirectCounterIDList = []uint32 {\n" + meterMapDeclaration = "MeterIDToNameMap = map[uint32]string {\n" + meterListDeclaration = "MeterIDList = []uint32 {\n" + packetMetadataMapDeclaration = "PktMetadataIDToNameMap = map[uint32]string {\n" + packetMetadataListDeclaration = "PktMetadataIDList = []uint32 {\n" ) +var () + func emitEntityConstant(p4EntityName string, id uint32) string { // see: https://go.dev/ref/spec#Identifiers p4EntityName = strings.Replace(p4EntityName, ".", "_", -1) p4EntityName = strcase.ToPascal(p4EntityName) - return fmt.Sprintf("%s \t %s = %v\n", p4EntityName, IdTypeString, id) + return fmt.Sprintf("%s \t %s = %v\n", p4EntityName, idTypeString, id) +} + +func emitEntitySizeConstant(p4EntityName string, id int64) string { + // see: https://go.dev/ref/spec#Identifiers + p4EntityName = strings.Replace(p4EntityName, ".", "_", -1) + p4EntityName = strcase.ToPascal(p4EntityName) + return fmt.Sprintf("%s \t %s = %v\n", p4EntityName, sizeTypeString, id) } func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { - builder := strings.Builder{} + constBuilder := strings.Builder{} // constBuilder used to create consts + mapBuilder := strings.Builder{} + listBuilder := strings.Builder{} - builder.WriteString(CopyrightHeader + "\n") + constBuilder.WriteString(copyrightHeader + "\n") - builder.WriteString(fmt.Sprintf("package %s\n", packageName)) - builder.WriteString(ConstOpen + "\n") + constBuilder.WriteString(fmt.Sprintf("package %s\n", packageName)) + constBuilder.WriteString(constOpen + "\n") + mapBuilder.WriteString(varOpen) //HeaderField IDs - builder.WriteString("// HeaderFields\n") + constBuilder.WriteString("// HeaderFields\n") for _, element := range p4info.GetTables() { for _, matchField := range element.MatchFields { tableName := element.GetPreamble().GetName() name := matchField.GetName() - builder.WriteString(emitEntityConstant(HfVarPrefix+tableName+name, matchField.GetId())) + constBuilder.WriteString(emitEntityConstant(hfVarPrefix+tableName+name, matchField.GetId())) } } // Tables - builder.WriteString("// Tables\n") + constBuilder.WriteString("// Tables\n") + mapBuilder.WriteString(tableMapDeclaration) + listBuilder.WriteString(tableListDeclaration) for _, element := range p4info.GetTables() { - name := element.GetPreamble().GetName() - builder.WriteString(emitEntityConstant(TblVarPrefix+name, element.GetPreamble().GetId())) + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + constBuilder.WriteString(emitEntityConstant(tblVarPrefix+name, ID)) + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } + mapBuilder.WriteString("}\n") //Close declaration + listBuilder.WriteString("}\n") + // Actions - builder.WriteString("// Actions\n") + constBuilder.WriteString("// Actions\n") + mapBuilder.WriteString(actionMapDeclaration) + listBuilder.WriteString(actionListDeclaration) for _, element := range p4info.GetActions() { - name := element.GetPreamble().GetName() - builder.WriteString(emitEntityConstant(ActVarPrefix+name, element.GetPreamble().GetId())) + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + constBuilder.WriteString(emitEntityConstant(actVarPrefix+name, ID)) + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } + mapBuilder.WriteString("}\n") + listBuilder.WriteString("}\n") //Close declarations + // Action Param IDs - builder.WriteString("// ActionParams\n") + constBuilder.WriteString("// ActionParams\n") for _, element := range p4info.GetActions() { for _, actionParam := range element.GetParams() { actionName := element.GetPreamble().GetName() name := actionParam.GetName() - builder.WriteString(emitEntityConstant(ActparamVarPrefix+actionName+name, actionParam.GetId())) + constBuilder.WriteString(emitEntityConstant(actparamVarPrefix+actionName+name, actionParam.GetId())) } } // Indirect Counters - builder.WriteString("// IndirectCounters\n") + constBuilder.WriteString("// IndirectCounters\n") + mapBuilder.WriteString(counterMapDeclaration) + listBuilder.WriteString(counterListDeclaration) for _, element := range p4info.GetCounters() { - name := element.GetPreamble().GetName() - builder.WriteString(emitEntityConstant(CtrVarPrefix+name, element.GetPreamble().GetId())) + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + constBuilder.WriteString(emitEntityConstant(ctrVarPrefix+name, ID)) + constBuilder.WriteString(emitEntitySizeConstant(ctrSizeVarPrefix+name, element.GetSize())) + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } + mapBuilder.WriteString("}\n") + listBuilder.WriteString("}\n") //Close declarations + // Direct Counters - builder.WriteString("// DirectCounters\n") + constBuilder.WriteString("// DirectCounters\n") + mapBuilder.WriteString(directCounterMapDeclaration) + listBuilder.WriteString(directCounterListDeclaration) for _, element := range p4info.GetDirectCounters() { - name := element.GetPreamble().GetName() - builder.WriteString(emitEntityConstant(DirctrVarPrefix+name, element.GetPreamble().GetId())) + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + constBuilder.WriteString(emitEntityConstant(dirCtrVarPrefix+name, element.GetPreamble().GetId())) + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } + mapBuilder.WriteString("}\n") + listBuilder.WriteString("}\n") //Close declarations + // Action profiles - builder.WriteString("// ActionProfiles\n") + constBuilder.WriteString("// ActionProfiles\n") for _, element := range p4info.GetActionProfiles() { name := element.GetPreamble().GetName() - builder.WriteString(emitEntityConstant(ActprofVarPrefix+name, element.GetPreamble().GetId())) + constBuilder.WriteString(emitEntityConstant(actprofVarPrefix+name, element.GetPreamble().GetId())) } // Packet metadata - builder.WriteString("// PacketMetadata\n") + constBuilder.WriteString("// PacketMetadata\n") + mapBuilder.WriteString(packetMetadataMapDeclaration) + listBuilder.WriteString(packetMetadataListDeclaration) for _, element := range p4info.GetControllerPacketMetadata() { - name := element.GetPreamble().GetName() - builder.WriteString(emitEntityConstant(PacketmetaVarPrefix+name, element.GetPreamble().GetId())) + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + constBuilder.WriteString(emitEntityConstant(packetmetaVarPrefix+name, ID)) + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } + mapBuilder.WriteString("}\n") + listBuilder.WriteString("}\n") //Close declarations + // Meters - builder.WriteString("// Meters\n") + constBuilder.WriteString("// Meters\n") + mapBuilder.WriteString(meterMapDeclaration) + listBuilder.WriteString(meterListDeclaration) for _, element := range p4info.GetMeters() { - name := element.GetPreamble().GetName() - builder.WriteString(emitEntityConstant(MtrVarPrefix+name, element.GetPreamble().GetId())) + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + constBuilder.WriteString(emitEntityConstant(mtrVarPrefix+name, ID)) + constBuilder.WriteString(emitEntitySizeConstant(mtrSizeVarPrefix+name, element.GetSize())) + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } + mapBuilder.WriteString("}\n") + listBuilder.WriteString("}\n") //Close declarations - builder.WriteString(ConstClose + "\n") + constBuilder.WriteString(constOrVarClose + "\n") + listBuilder.WriteString(constOrVarClose) - return builder.String() + return constBuilder.String() + mapBuilder.String() + listBuilder.String() } func getP4Config(p4infopath string) *p4ConfigV1.P4Info { @@ -138,9 +219,9 @@ func getP4Config(p4infopath string) *p4ConfigV1.P4Info { } func main() { - p4infoPath := flag.String("p4info", P4infoPath, "Path of the p4info file") + p4infoPath := flag.String("p4info", p4infoPath, "Path of the p4info file") outputPath := flag.String("output", "-", "Default will print to Stdout") - packageName := flag.String("package", DefaultPackageName, "Set the package name") + packageName := flag.String("package", defaultPackageName, "Set the package name") flag.Parse() From 7791972bc220fef7ee998a507bd4f67c1ab8528d Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Mon, 21 Feb 2022 17:08:42 +0100 Subject: [PATCH 12/53] Refactor --- internal/p4constants/p4constants.go | 123 +++++++++++++++++++++++++++- scripts/go_gen_p4_const.go | 4 +- 2 files changed, 120 insertions(+), 7 deletions(-) diff --git a/internal/p4constants/p4constants.go b/internal/p4constants/p4constants.go index de427b359..e22d8c6db 100644 --- a/internal/p4constants/p4constants.go +++ b/internal/p4constants/p4constants.go @@ -101,8 +101,10 @@ const ( ActionParamPreQosPipeLoadTunnelParamdstAddr uint32 = 2 ActionParamPreQosPipeLoadTunnelParamsport uint32 = 3 // IndirectCounters - CounterPreQosPipePreQosCounter uint32 = 315693181 - CounterPostQosPipePostQosCounter uint32 = 302958180 + CounterPreQosPipePreQosCounter uint32 = 315693181 + CounterSizePreQosPipePreQosCounter uint64 = 1024 + CounterPostQosPipePostQosCounter uint32 = 302958180 + CounterSizePostQosPipePostQosCounter uint64 = 1024 // DirectCounters DirectCounterAcls uint32 = 325583051 // ActionProfiles @@ -111,6 +113,119 @@ const ( PacketMetaPacketOut uint32 = 75327753 PacketMetaPacketIn uint32 = 80671331 // Meters - MeterPreQosPipeAppMeter uint32 = 338231090 - MeterPreQosPipeSessionMeter uint32 = 347593234 + MeterPreQosPipeAppMeter uint32 = 338231090 + MeterSizePreQosPipeAppMeter uint64 = 1024 + MeterPreQosPipeSessionMeter uint32 = 347593234 + MeterSizePreQosPipeSessionMeter uint64 = 1024 +) + +var ( + TableIDToNameMap = map[uint32]string{ + 39015874: "PreQosPipe.Routing.routes_v4", + 47204971: "PreQosPipe.Acl.acls", + 40931612: "PreQosPipe.my_station", + 33923840: "PreQosPipe.interfaces", + 44976597: "PreQosPipe.sessions_uplink", + 34742049: "PreQosPipe.sessions_downlink", + 37595532: "PreQosPipe.terminations_uplink", + 34778590: "PreQosPipe.terminations_downlink", + 46868458: "PreQosPipe.applications", + 49497304: "PreQosPipe.tunnel_peers", + } + ActionIDToNameMap = map[uint32]string{ + 21257015: "NoAction", + 31448256: "PreQosPipe.Routing.drop", + 23965128: "PreQosPipe.Routing.route", + 30494847: "PreQosPipe.Acl.set_port", + 26495283: "PreQosPipe.Acl.punt", + 21596798: "PreQosPipe.Acl.clone_to_cpu", + 18812293: "PreQosPipe.Acl.drop", + 23766285: "PreQosPipe._initialize_metadata", + 26090030: "PreQosPipe.set_source_iface", + 28401267: "PreQosPipe.do_drop", + 19461580: "PreQosPipe.set_session_uplink", + 22196934: "PreQosPipe.set_session_uplink_drop", + 21848329: "PreQosPipe.set_session_downlink", + 20229579: "PreQosPipe.set_session_downlink_drop", + 20249483: "PreQosPipe.set_session_downlink_buff", + 21760615: "PreQosPipe.uplink_term_fwd_no_tc", + 28305359: "PreQosPipe.uplink_term_fwd", + 20977365: "PreQosPipe.uplink_term_drop", + 26185804: "PreQosPipe.downlink_term_fwd_no_tc", + 32699713: "PreQosPipe.downlink_term_fwd", + 31264233: "PreQosPipe.downlink_term_drop", + 23010411: "PreQosPipe.set_app_id", + 32742981: "PreQosPipe.load_tunnel_param", + 29247910: "PreQosPipe.do_gtpu_tunnel", + 31713420: "PreQosPipe.do_gtpu_tunnel_with_psc", + } + CounterIDToNameMap = map[uint32]string{ + 315693181: "PreQosPipe.pre_qos_counter", + 302958180: "PostQosPipe.post_qos_counter", + } + DirectCounterIDToNameMap = map[uint32]string{ + 325583051: "acls", + } + PktMetadataIDToNameMap = map[uint32]string{ + 75327753: "packet_out", + 80671331: "packet_in", + } + MeterIDToNameMap = map[uint32]string{ + 338231090: "PreQosPipe.app_meter", + 347593234: "PreQosPipe.session_meter", + } + TableIDList = []uint32{ + 39015874, + 47204971, + 40931612, + 33923840, + 44976597, + 34742049, + 37595532, + 34778590, + 46868458, + 49497304, + } + ActionIDList = []uint32{ + 21257015, + 31448256, + 23965128, + 30494847, + 26495283, + 21596798, + 18812293, + 23766285, + 26090030, + 28401267, + 19461580, + 22196934, + 21848329, + 20229579, + 20249483, + 21760615, + 28305359, + 20977365, + 26185804, + 32699713, + 31264233, + 23010411, + 32742981, + 29247910, + 31713420, + } + CounterIDList = []uint32{ + 315693181, + 302958180, + } + DirectCounterIDList = []uint32{ + 325583051, + } + PktMetadataIDList = []uint32{ + 75327753, + 80671331, + } + MeterIDList = []uint32{ + 338231090, + 347593234, + } ) diff --git a/scripts/go_gen_p4_const.go b/scripts/go_gen_p4_const.go index 16532ad93..f68ff078d 100644 --- a/scripts/go_gen_p4_const.go +++ b/scripts/go_gen_p4_const.go @@ -76,9 +76,7 @@ func emitEntitySizeConstant(p4EntityName string, id int64) string { } func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { - constBuilder := strings.Builder{} // constBuilder used to create consts - mapBuilder := strings.Builder{} - listBuilder := strings.Builder{} + constBuilder, mapBuilder, listBuilder := strings.Builder{}, strings.Builder{}, strings.Builder{} constBuilder.WriteString(copyrightHeader + "\n") From 20d29bd182e32859d664396ab35317598e9a8c2a Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Mon, 21 Feb 2022 17:22:26 +0100 Subject: [PATCH 13/53] Refactor --- internal/p4constants/p4constants.go | 7 ++++++- scripts/go_gen_p4_const.go | 16 +++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/internal/p4constants/p4constants.go b/internal/p4constants/p4constants.go index e22d8c6db..613677780 100644 --- a/internal/p4constants/p4constants.go +++ b/internal/p4constants/p4constants.go @@ -3,7 +3,6 @@ package p4constants -//noinspection GoSnakeCaseUsage const ( // HeaderFields @@ -166,6 +165,9 @@ var ( DirectCounterIDToNameMap = map[uint32]string{ 325583051: "acls", } + ActionProfileIDToNameMap = map[uint32]string{ + 297808402: "hashed_selector", + } PktMetadataIDToNameMap = map[uint32]string{ 75327753: "packet_out", 80671331: "packet_in", @@ -220,6 +222,9 @@ var ( DirectCounterIDList = []uint32{ 325583051, } + ActionProfileIDList = []uint32{ + 297808402, + } PktMetadataIDList = []uint32{ 75327753, 80671331, diff --git a/scripts/go_gen_p4_const.go b/scripts/go_gen_p4_const.go index f68ff078d..271bf2011 100644 --- a/scripts/go_gen_p4_const.go +++ b/scripts/go_gen_p4_const.go @@ -24,7 +24,7 @@ const ( // Copyright 2022-present Open Networking Foundation ` - constOpen = "//noinspection GoSnakeCaseUsage\nconst (\n" + constOpen = "const (\n" varOpen = "var (\n" mapFormatString = "%v:\"%v\",\n" listFormatString = "%v,\n" @@ -49,6 +49,8 @@ const ( tableListDeclaration = "TableIDList = []uint32 {\n" actionMapDeclaration = "ActionIDToNameMap = map[uint32]string {\n" actionListDeclaration = "ActionIDList = []uint32 {\n" + actionProfileMapDeclaration = "ActionProfileIDToNameMap = map[uint32]string {\n" + actionProfileListDeclaration = "ActionProfileIDList = []uint32 {\n" counterMapDeclaration = "CounterIDToNameMap = map[uint32]string {\n" counterListDeclaration = "CounterIDList = []uint32 {\n" directCounterMapDeclaration = "DirectCounterIDToNameMap = map[uint32]string {\n" @@ -161,10 +163,18 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { // Action profiles constBuilder.WriteString("// ActionProfiles\n") + mapBuilder.WriteString(actionProfileMapDeclaration) + listBuilder.WriteString(actionProfileListDeclaration) for _, element := range p4info.GetActionProfiles() { - name := element.GetPreamble().GetName() - constBuilder.WriteString(emitEntityConstant(actprofVarPrefix+name, element.GetPreamble().GetId())) + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + constBuilder.WriteString(emitEntityConstant(actprofVarPrefix+name, ID)) + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } + mapBuilder.WriteString("}\n") + listBuilder.WriteString("}\n") //Close declarations + // Packet metadata constBuilder.WriteString("// PacketMetadata\n") mapBuilder.WriteString(packetMetadataMapDeclaration) From bb18debe40a0c200b7c18a007903f2b62f268007 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Mon, 21 Feb 2022 17:34:06 +0100 Subject: [PATCH 14/53] Refactor --- internal/p4constants/p4constants.go | 1 - scripts/go_gen_p4_const.go | 11 +++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/internal/p4constants/p4constants.go b/internal/p4constants/p4constants.go index 613677780..1351446eb 100644 --- a/internal/p4constants/p4constants.go +++ b/internal/p4constants/p4constants.go @@ -4,7 +4,6 @@ package p4constants const ( - // HeaderFields HdrPreQosPipeRoutingRoutesV4dstPrefix uint32 = 1 HdrPreQosPipeAclAclsinport uint32 = 1 diff --git a/scripts/go_gen_p4_const.go b/scripts/go_gen_p4_const.go index 271bf2011..d7b6f01e5 100644 --- a/scripts/go_gen_p4_const.go +++ b/scripts/go_gen_p4_const.go @@ -61,8 +61,6 @@ const ( packetMetadataListDeclaration = "PktMetadataIDList = []uint32 {\n" ) -var () - func emitEntityConstant(p4EntityName string, id uint32) string { // see: https://go.dev/ref/spec#Identifiers p4EntityName = strings.Replace(p4EntityName, ".", "_", -1) @@ -83,7 +81,7 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { constBuilder.WriteString(copyrightHeader + "\n") constBuilder.WriteString(fmt.Sprintf("package %s\n", packageName)) - constBuilder.WriteString(constOpen + "\n") + constBuilder.WriteString(constOpen) mapBuilder.WriteString(varOpen) //HeaderField IDs @@ -106,8 +104,8 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } - mapBuilder.WriteString("}\n") //Close declaration - listBuilder.WriteString("}\n") + mapBuilder.WriteString("}\n") + listBuilder.WriteString("}\n") //Close declaration // Actions constBuilder.WriteString("// Actions\n") @@ -132,6 +130,7 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { constBuilder.WriteString(emitEntityConstant(actparamVarPrefix+actionName+name, actionParam.GetId())) } } + // Indirect Counters constBuilder.WriteString("// IndirectCounters\n") mapBuilder.WriteString(counterMapDeclaration) @@ -205,7 +204,7 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { listBuilder.WriteString("}\n") //Close declarations constBuilder.WriteString(constOrVarClose + "\n") - listBuilder.WriteString(constOrVarClose) + listBuilder.WriteString(constOrVarClose) // last builder closes 'var' declaration return constBuilder.String() + mapBuilder.String() + listBuilder.String() } From c24ac19e41f23367dcd68dd09e7016deec4975d2 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Mon, 21 Feb 2022 17:43:49 +0100 Subject: [PATCH 15/53] Refactor --- scripts/go_gen_p4_const.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/scripts/go_gen_p4_const.go b/scripts/go_gen_p4_const.go index d7b6f01e5..7ec5a1a7f 100644 --- a/scripts/go_gen_p4_const.go +++ b/scripts/go_gen_p4_const.go @@ -88,8 +88,8 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { constBuilder.WriteString("// HeaderFields\n") for _, element := range p4info.GetTables() { for _, matchField := range element.MatchFields { - tableName := element.GetPreamble().GetName() - name := matchField.GetName() + tableName, name := element.GetPreamble().GetName(), matchField.GetName() + constBuilder.WriteString(emitEntityConstant(hfVarPrefix+tableName+name, matchField.GetId())) } } @@ -125,8 +125,8 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { constBuilder.WriteString("// ActionParams\n") for _, element := range p4info.GetActions() { for _, actionParam := range element.GetParams() { - actionName := element.GetPreamble().GetName() - name := actionParam.GetName() + actionName, name := element.GetPreamble().GetName(), actionParam.GetName() + constBuilder.WriteString(emitEntityConstant(actparamVarPrefix+actionName+name, actionParam.GetId())) } } From 2fe9a2221e1c5d8931c25bd5c0c2bcc1402f5aef Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Mon, 21 Feb 2022 17:47:24 +0100 Subject: [PATCH 16/53] Refactor. Make list builder and map builder independent using different var() sections --- internal/p4constants/p4constants.go | 3 +++ scripts/go_gen_p4_const.go | 4 +++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/internal/p4constants/p4constants.go b/internal/p4constants/p4constants.go index 1351446eb..9fee6fce2 100644 --- a/internal/p4constants/p4constants.go +++ b/internal/p4constants/p4constants.go @@ -175,6 +175,9 @@ var ( 338231090: "PreQosPipe.app_meter", 347593234: "PreQosPipe.session_meter", } +) + +var ( TableIDList = []uint32{ 39015874, 47204971, diff --git a/scripts/go_gen_p4_const.go b/scripts/go_gen_p4_const.go index 7ec5a1a7f..be4e1925e 100644 --- a/scripts/go_gen_p4_const.go +++ b/scripts/go_gen_p4_const.go @@ -83,6 +83,7 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { constBuilder.WriteString(fmt.Sprintf("package %s\n", packageName)) constBuilder.WriteString(constOpen) mapBuilder.WriteString(varOpen) + listBuilder.WriteString(varOpen) //HeaderField IDs constBuilder.WriteString("// HeaderFields\n") @@ -204,7 +205,8 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { listBuilder.WriteString("}\n") //Close declarations constBuilder.WriteString(constOrVarClose + "\n") - listBuilder.WriteString(constOrVarClose) // last builder closes 'var' declaration + mapBuilder.WriteString(constOrVarClose + "\n") + listBuilder.WriteString(constOrVarClose) return constBuilder.String() + mapBuilder.String() + listBuilder.String() } From e8d7cf144d8d3cf01778fdf61adedcd851084a0f Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Mon, 21 Feb 2022 18:06:08 +0100 Subject: [PATCH 17/53] Add CI verification step --- .github/workflows/pull_request.yml | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index c839e8aaf..9915ec35c 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -53,6 +53,19 @@ jobs: run: | git --no-pager diff + - name: Build P4 constants + id: check + run: | + make p4-constants + echo "::set-output name=PORCELAIN::`git status --porcelain`" + # Verify P4 constants + - name: Check P4 constants are updated + if: ${{ steps.check.outputs.PORCELAIN != '' }} + uses: actions/github-script@v3 + with: + script: | + core.setFailed('Please run make p4-constants and commit changes') + # Build again and commit - name: Build the BESS-UPF Docker image (after format) run: | From 295e7cef620e589a08970e9a3678f8cf43f6461d Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Tue, 22 Feb 2022 14:18:47 +0100 Subject: [PATCH 18/53] Refactor workflow --- .github/workflows/pull_request.yml | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 9915ec35c..c5c2c58cb 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -52,19 +52,15 @@ jobs: - name: Show all CI changes run: | git --no-pager diff - + # Verify P4 constants - name: Build P4 constants id: check run: | make p4-constants - echo "::set-output name=PORCELAIN::`git status --porcelain`" - # Verify P4 constants - - name: Check P4 constants are updated - if: ${{ steps.check.outputs.PORCELAIN != '' }} - uses: actions/github-script@v3 - with: - script: | - core.setFailed('Please run make p4-constants and commit changes') + git update-index --refresh + - name: Log on step failure + if: steps.check.outcome == 'failure' + run: echo 'Please run make p4-constants and commit changes' # Build again and commit - name: Build the BESS-UPF Docker image (after format) From 793f29cb593aee6cf6fe89d9d202744c69d77acd Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Tue, 22 Feb 2022 14:38:55 +0100 Subject: [PATCH 19/53] Make maps and slices immutable --- internal/p4constants/p4constants.go | 72 +++++++++++++++++------ scripts/go_gen_p4_const.go | 89 ++++++++++++++--------------- 2 files changed, 96 insertions(+), 65 deletions(-) diff --git a/internal/p4constants/p4constants.go b/internal/p4constants/p4constants.go index 9fee6fce2..689e4fc6a 100644 --- a/internal/p4constants/p4constants.go +++ b/internal/p4constants/p4constants.go @@ -117,8 +117,8 @@ const ( MeterSizePreQosPipeSessionMeter uint64 = 1024 ) -var ( - TableIDToNameMap = map[uint32]string{ +func GetTableIDToNameMap() map[uint32]string { + return map[uint32]string{ 39015874: "PreQosPipe.Routing.routes_v4", 47204971: "PreQosPipe.Acl.acls", 40931612: "PreQosPipe.my_station", @@ -130,7 +130,10 @@ var ( 46868458: "PreQosPipe.applications", 49497304: "PreQosPipe.tunnel_peers", } - ActionIDToNameMap = map[uint32]string{ +} + +func GetActionIDToNameMap() map[uint32]string { + return map[uint32]string{ 21257015: "NoAction", 31448256: "PreQosPipe.Routing.drop", 23965128: "PreQosPipe.Routing.route", @@ -157,28 +160,43 @@ var ( 29247910: "PreQosPipe.do_gtpu_tunnel", 31713420: "PreQosPipe.do_gtpu_tunnel_with_psc", } - CounterIDToNameMap = map[uint32]string{ +} + +func GetCounterIDToNameMap() map[uint32]string { + return map[uint32]string{ 315693181: "PreQosPipe.pre_qos_counter", 302958180: "PostQosPipe.post_qos_counter", } - DirectCounterIDToNameMap = map[uint32]string{ +} + +func GetDirectCounterIDToNameMap() map[uint32]string { + return map[uint32]string{ 325583051: "acls", } - ActionProfileIDToNameMap = map[uint32]string{ +} + +func GetActionProfileIDToNameMap() map[uint32]string { + return map[uint32]string{ 297808402: "hashed_selector", } - PktMetadataIDToNameMap = map[uint32]string{ +} + +func GetPacketMetadataIDToNameMap() map[uint32]string { + return map[uint32]string{ 75327753: "packet_out", 80671331: "packet_in", } - MeterIDToNameMap = map[uint32]string{ +} + +func GetMeterIDToNameMap() map[uint32]string { + return map[uint32]string{ 338231090: "PreQosPipe.app_meter", 347593234: "PreQosPipe.session_meter", } -) +} -var ( - TableIDList = []uint32{ +func GetTableIDList() []uint32 { + return []uint32{ 39015874, 47204971, 40931612, @@ -190,7 +208,10 @@ var ( 46868458, 49497304, } - ActionIDList = []uint32{ +} + +func GetActionIDList() []uint32 { + return []uint32{ 21257015, 31448256, 23965128, @@ -217,22 +238,37 @@ var ( 29247910, 31713420, } - CounterIDList = []uint32{ +} + +func GetCounterIDList() []uint32 { + return []uint32{ 315693181, 302958180, } - DirectCounterIDList = []uint32{ +} + +func GetDirectCounterIDList() []uint32 { + return []uint32{ 325583051, } - ActionProfileIDList = []uint32{ +} + +func GetActionProfileIDList() []uint32 { + return []uint32{ 297808402, } - PktMetadataIDList = []uint32{ +} + +func GetPacketMetadataIDList() []uint32 { + return []uint32{ 75327753, 80671331, } - MeterIDList = []uint32{ +} + +func GetMeterIDList() []uint32 { + return []uint32{ 338231090, 347593234, } -) +} diff --git a/scripts/go_gen_p4_const.go b/scripts/go_gen_p4_const.go index be4e1925e..b0d92efa0 100644 --- a/scripts/go_gen_p4_const.go +++ b/scripts/go_gen_p4_const.go @@ -25,7 +25,6 @@ const ( ` constOpen = "const (\n" - varOpen = "var (\n" mapFormatString = "%v:\"%v\",\n" listFormatString = "%v,\n" constOrVarClose = ")\n" @@ -45,20 +44,20 @@ const ( mtrVarPrefix = "Meter_" mtrSizeVarPrefix = "MeterSize_" - tableMapDeclaration = "TableIDToNameMap = map[uint32]string {\n" - tableListDeclaration = "TableIDList = []uint32 {\n" - actionMapDeclaration = "ActionIDToNameMap = map[uint32]string {\n" - actionListDeclaration = "ActionIDList = []uint32 {\n" - actionProfileMapDeclaration = "ActionProfileIDToNameMap = map[uint32]string {\n" - actionProfileListDeclaration = "ActionProfileIDList = []uint32 {\n" - counterMapDeclaration = "CounterIDToNameMap = map[uint32]string {\n" - counterListDeclaration = "CounterIDList = []uint32 {\n" - directCounterMapDeclaration = "DirectCounterIDToNameMap = map[uint32]string {\n" - directCounterListDeclaration = "DirectCounterIDList = []uint32 {\n" - meterMapDeclaration = "MeterIDToNameMap = map[uint32]string {\n" - meterListDeclaration = "MeterIDList = []uint32 {\n" - packetMetadataMapDeclaration = "PktMetadataIDToNameMap = map[uint32]string {\n" - packetMetadataListDeclaration = "PktMetadataIDList = []uint32 {\n" + tableMapFunc = "func GetTableIDToNameMap() map[uint32]string {\n return map[uint32]string {\n" + tableListFunc = "func GetTableIDList() []uint32 {\n return []uint32 {\n" + actionMapFunc = "func GetActionIDToNameMap() map[uint32]string {\n return map[uint32]string {\n" + actionListFunc = "func GetActionIDList() []uint32 {\n return []uint32 {\n" + counterMapFunc = "func GetCounterIDToNameMap() map[uint32]string {\n return map[uint32]string {\n" + counterListFunc = "func GetCounterIDList() []uint32 {\n return []uint32 {\n" + directCounterMapFunc = "func GetDirectCounterIDToNameMap() map[uint32]string {\n return map[uint32]string {\n" + directCounterListFunc = "func GetDirectCounterIDList() []uint32 {\n return []uint32 {\n" + actionProfileMapFunc = "func GetActionProfileIDToNameMap() map[uint32]string {\n return map[uint32]string {\n" + actionProfileListFunc = "func GetActionProfileIDList() []uint32 {\n return []uint32 {\n" + pktMetadataMapFunc = "func GetPacketMetadataIDToNameMap() map[uint32]string {\n return map[uint32]string {\n" + pktMetadataListFunc = "func GetPacketMetadataIDList() []uint32 {\n return []uint32 {\n" + metersMapFunc = "func GetMeterIDToNameMap() map[uint32]string {\n return map[uint32]string {\n" + metersListFunc = "func GetMeterIDList() []uint32 {\n return []uint32 {\n" ) func emitEntityConstant(p4EntityName string, id uint32) string { @@ -82,8 +81,6 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { constBuilder.WriteString(fmt.Sprintf("package %s\n", packageName)) constBuilder.WriteString(constOpen) - mapBuilder.WriteString(varOpen) - listBuilder.WriteString(varOpen) //HeaderField IDs constBuilder.WriteString("// HeaderFields\n") @@ -96,8 +93,8 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { } // Tables constBuilder.WriteString("// Tables\n") - mapBuilder.WriteString(tableMapDeclaration) - listBuilder.WriteString(tableListDeclaration) + mapBuilder.WriteString(tableMapFunc) + listBuilder.WriteString(tableListFunc) for _, element := range p4info.GetTables() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() @@ -105,13 +102,13 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } - mapBuilder.WriteString("}\n") - listBuilder.WriteString("}\n") //Close declaration + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close func declaration // Actions constBuilder.WriteString("// Actions\n") - mapBuilder.WriteString(actionMapDeclaration) - listBuilder.WriteString(actionListDeclaration) + mapBuilder.WriteString(actionMapFunc) + listBuilder.WriteString(actionListFunc) for _, element := range p4info.GetActions() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() @@ -119,8 +116,8 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } - mapBuilder.WriteString("}\n") - listBuilder.WriteString("}\n") //Close declarations + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close func declarations // Action Param IDs constBuilder.WriteString("// ActionParams\n") @@ -134,8 +131,8 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { // Indirect Counters constBuilder.WriteString("// IndirectCounters\n") - mapBuilder.WriteString(counterMapDeclaration) - listBuilder.WriteString(counterListDeclaration) + mapBuilder.WriteString(counterMapFunc) + listBuilder.WriteString(counterListFunc) for _, element := range p4info.GetCounters() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() @@ -144,13 +141,13 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } - mapBuilder.WriteString("}\n") - listBuilder.WriteString("}\n") //Close declarations + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close func declarations // Direct Counters constBuilder.WriteString("// DirectCounters\n") - mapBuilder.WriteString(directCounterMapDeclaration) - listBuilder.WriteString(directCounterListDeclaration) + mapBuilder.WriteString(directCounterMapFunc) + listBuilder.WriteString(directCounterListFunc) for _, element := range p4info.GetDirectCounters() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() @@ -158,13 +155,13 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } - mapBuilder.WriteString("}\n") - listBuilder.WriteString("}\n") //Close declarations + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close declarations // Action profiles constBuilder.WriteString("// ActionProfiles\n") - mapBuilder.WriteString(actionProfileMapDeclaration) - listBuilder.WriteString(actionProfileListDeclaration) + mapBuilder.WriteString(actionProfileMapFunc) + listBuilder.WriteString(actionProfileListFunc) for _, element := range p4info.GetActionProfiles() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() @@ -172,13 +169,13 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } - mapBuilder.WriteString("}\n") - listBuilder.WriteString("}\n") //Close declarations + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close declarations // Packet metadata constBuilder.WriteString("// PacketMetadata\n") - mapBuilder.WriteString(packetMetadataMapDeclaration) - listBuilder.WriteString(packetMetadataListDeclaration) + mapBuilder.WriteString(pktMetadataMapFunc) + listBuilder.WriteString(pktMetadataListFunc) for _, element := range p4info.GetControllerPacketMetadata() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() @@ -186,13 +183,13 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } - mapBuilder.WriteString("}\n") - listBuilder.WriteString("}\n") //Close declarations + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close declarations // Meters constBuilder.WriteString("// Meters\n") - mapBuilder.WriteString(meterMapDeclaration) - listBuilder.WriteString(meterListDeclaration) + mapBuilder.WriteString(metersMapFunc) + listBuilder.WriteString(metersListFunc) for _, element := range p4info.GetMeters() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() @@ -201,12 +198,10 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } - mapBuilder.WriteString("}\n") - listBuilder.WriteString("}\n") //Close declarations + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close declarations constBuilder.WriteString(constOrVarClose + "\n") - mapBuilder.WriteString(constOrVarClose + "\n") - listBuilder.WriteString(constOrVarClose) return constBuilder.String() + mapBuilder.String() + listBuilder.String() } From 28d88509349385e7321517884d132a6b71a1b3a6 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Tue, 22 Feb 2022 14:41:59 +0100 Subject: [PATCH 20/53] Refactor workflow --- .github/workflows/pull_request.yml | 3 --- 1 file changed, 3 deletions(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index c5c2c58cb..ef217134f 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -58,9 +58,6 @@ jobs: run: | make p4-constants git update-index --refresh - - name: Log on step failure - if: steps.check.outcome == 'failure' - run: echo 'Please run make p4-constants and commit changes' # Build again and commit - name: Build the BESS-UPF Docker image (after format) From e327b9d1d6f4c822a0ff5be1c47d39c1efcbcc2a Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Tue, 22 Feb 2022 16:03:57 +0100 Subject: [PATCH 21/53] Use latest version of p4constants --- pfcpiface/p4rt_translator.go | 20 ------------- pfcpiface/up4.go | 55 ++++++++++++++---------------------- 2 files changed, 21 insertions(+), 54 deletions(-) diff --git a/pfcpiface/p4rt_translator.go b/pfcpiface/p4rt_translator.go index b699a8155..778336bde 100644 --- a/pfcpiface/p4rt_translator.go +++ b/pfcpiface/p4rt_translator.go @@ -132,26 +132,6 @@ func (t *P4rtTranslator) getTableByID(tableID uint32) (*p4ConfigV1.Table, error) return nil, ErrNotFoundWithParam("table", "ID", tableID) } -func (t *P4rtTranslator) getCounterByID(ID uint32) (*p4ConfigV1.Counter, error) { - for _, ctr := range t.p4Info.Counters { - if ctr.Preamble.Id == ID { - return ctr, nil - } - } - - return nil, ErrNotFoundWithParam("counter", "ID", ID) -} - -func (t *P4rtTranslator) getMeterByID(ID uint32) (*p4ConfigV1.Meter, error) { - for _, mtr := range t.p4Info.Meters { - if mtr.Preamble.Id == ID { - return mtr, nil - } - } - - return nil, ErrNotFoundWithParam("meter", "ID", ID) -} - //nolint:unused func (t *P4rtTranslator) getMatchFieldIDByName(table *p4ConfigV1.Table, fieldName string) uint32 { for _, field := range table.MatchFields { diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index da7370910..eb05f95ad 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -138,20 +138,15 @@ func (up4 *UP4) sessionStats(*PfcpNodeCollector, chan<- prometheus.Metric) error func (up4 *UP4) portStats(uc *upfCollector, ch chan<- prometheus.Metric) { } -func (up4 *UP4) initCounter(counterID uint8, name string) error { - ctr, err := up4.p4RtTranslator.getCounterByID(uint32(counterID)) - if err != nil { - return err - } - - up4.counters[counterID].maxSize = uint64(ctr.Size) - up4.counters[counterID].counterID = uint64(ctr.Preamble.Id) +func (up4 *UP4) initCounter(counterID uint8, name string, counterSize uint64) error { + up4.counters[counterID].maxSize = counterSize + up4.counters[counterID].counterID = uint64(counterID) log.WithFields(log.Fields{ "counterID": counterID, "name": name, - "max-size": ctr.Size, - "UP4 counter ID": ctr.Preamble.Id, + "max-size": counterSize, + "UP4 counter ID": counterID, }).Debug("Counter initialized successfully") return nil @@ -219,13 +214,18 @@ func (up4 *UP4) setupChannel() error { func (up4 *UP4) initAllCounters() error { log.Debug("Initializing counter for UP4") + preQosCounterIdentifier, preQoSCounterSize := p4constants.CounterPreQosPipePreQosCounter, p4constants.CounterSizePreQosPipePreQosCounter + preQosCounterName := p4constants.GetCounterIDToNameMap()[preQosCounterIdentifier] - err := up4.initCounter(preQosCounterID, "PreQosPipe.pre_qos_counter") + err := up4.initCounter(preQosCounterID, preQosCounterName, preQoSCounterSize) if err != nil { return ErrOperationFailedWithReason("init preQosCounterID counter", err.Error()) } - err = up4.initCounter(postQosCounterID, "PostQosPipe.post_qos_counter") + postQosCounterIdentifier, postQoSCounterSize := p4constants.CounterPreQosPipePreQosCounter, p4constants.CounterSizePreQosPipePreQosCounter + postQosCounterName := p4constants.GetCounterIDToNameMap()[postQosCounterIdentifier] + + err = up4.initCounter(postQosCounterID, postQosCounterName, postQoSCounterSize) if err != nil { return ErrOperationFailedWithReason("init postQosCounterID counter", err.Error()) } @@ -236,35 +236,31 @@ func (up4 *UP4) initAllCounters() error { func (up4 *UP4) initMetersPools() error { log.Debug("Initializing P4 Meters pools for UP4") - appMeter, err := up4.p4RtTranslator.getMeterByID(p4constants.MeterPreQosPipeAppMeter) - if err != nil { - return err - } + appMeterID, appMeterSize := p4constants.MeterPreQosPipeAppMeter, p4constants.MeterSizePreQosPipeAppMeter + appMeterName := p4constants.GetMeterIDToNameMap()[appMeterID] log.WithFields(log.Fields{ "name": applicationMeter, - "meter": appMeter, + "meter": appMeterName, }).Trace("Found P4 meter by name") up4.appMeterCellIDsPool = set.NewSet() - for i := 1; i < int(appMeter.Size); i++ { + for i := 1; i < int(appMeterSize); i++ { up4.appMeterCellIDsPool.Add(uint32(i)) } log.Trace("Application meter IDs pool initialized: ", up4.appMeterCellIDsPool.String()) - sessMeter, err := up4.p4RtTranslator.getMeterByID(p4constants.MeterPreQosPipeSessionMeter) - if err != nil { - return err - } + sessMeterID, sessMeterSize := p4constants.MeterPreQosPipeSessionMeter, p4constants.MeterSizePreQosPipeSessionMeter + sessMeterName := p4constants.GetMeterIDToNameMap()[sessMeterID] log.WithFields(log.Fields{ "name": sessionMeter, - "meter": sessMeter, + "meter": sessMeterName, }).Trace("Found P4 meter by name") up4.sessMeterCellIDsPool = set.NewSet() - for i := 1; i < int(sessMeter.Size); i++ { + for i := 1; i < int(sessMeterSize); i++ { up4.sessMeterCellIDsPool.Add(uint32(i)) } @@ -378,16 +374,7 @@ func (up4 *UP4) setUpfInfo(u *upf, conf *Conf) { } func (up4 *UP4) clearAllTables() error { - tableIDs := []uint32{ - p4constants.TablePreQosPipeSessionsUplink, - p4constants.TablePreQosPipeSessionsDownlink, - p4constants.TablePreQosPipeTerminationsUplink, - p4constants.TablePreQosPipeTerminationsDownlink, - p4constants.TablePreQosPipeTunnelPeers, - p4constants.TablePreQosPipeInterfaces, - } - - if err := up4.p4client.ClearTables(tableIDs); err != nil { + if err := up4.p4client.ClearTables(p4constants.GetTableIDList()); err != nil { return err } From 09a7a2b8263e68970c6c4b6e9cc2a1c50f7fb1bd Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Tue, 22 Feb 2022 16:10:06 +0100 Subject: [PATCH 22/53] Refactor --- pfcpiface/p4rt_translator.go | 4 ++-- pfcpiface/up4.go | 9 ++------- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/pfcpiface/p4rt_translator.go b/pfcpiface/p4rt_translator.go index 778336bde..29905afec 100644 --- a/pfcpiface/p4rt_translator.go +++ b/pfcpiface/p4rt_translator.go @@ -816,9 +816,9 @@ func (t *P4rtTranslator) BuildMeterEntry(meterID uint32, cellID uint32, config * switch meterID { case p4constants.MeterPreQosPipeAppMeter: - meterName = applicationMeter + meterName = p4constants.GetMeterIDToNameMap()[p4constants.MeterPreQosPipeAppMeter] case p4constants.MeterPreQosPipeSessionMeter: - meterName = sessionMeter + meterName = p4constants.GetMeterIDToNameMap()[p4constants.MeterPreQosPipeSessionMeter] } builderLog := log.WithFields(log.Fields{ diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index eb05f95ad..61d6da2d9 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -36,9 +36,6 @@ const ( maxGTPTunnelPeerIDs = 253 maxApplicationIDs = 254 - applicationMeter = "PreQosPipe.app_meter" - sessionMeter = "PreQosPipe.session_meter" - meterTypeApplication uint8 = 1 meterTypeSession uint8 = 2 @@ -240,8 +237,7 @@ func (up4 *UP4) initMetersPools() error { appMeterName := p4constants.GetMeterIDToNameMap()[appMeterID] log.WithFields(log.Fields{ - "name": applicationMeter, - "meter": appMeterName, + "name": appMeterName, }).Trace("Found P4 meter by name") up4.appMeterCellIDsPool = set.NewSet() @@ -255,8 +251,7 @@ func (up4 *UP4) initMetersPools() error { sessMeterName := p4constants.GetMeterIDToNameMap()[sessMeterID] log.WithFields(log.Fields{ - "name": sessionMeter, - "meter": sessMeterName, + "name": sessMeterName, }).Trace("Found P4 meter by name") up4.sessMeterCellIDsPool = set.NewSet() From 07b8f7afb0cd55acf2088e2940ab752893f64d47 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Tue, 22 Feb 2022 16:15:08 +0100 Subject: [PATCH 23/53] Refactor --- pfcpiface/p4rt_translator.go | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/pfcpiface/p4rt_translator.go b/pfcpiface/p4rt_translator.go index 29905afec..4b90768d8 100644 --- a/pfcpiface/p4rt_translator.go +++ b/pfcpiface/p4rt_translator.go @@ -812,14 +812,7 @@ func (t *P4rtTranslator) BuildGTPTunnelPeerTableEntry(tunnelPeerID uint8, tunnel } func (t *P4rtTranslator) BuildMeterEntry(meterID uint32, cellID uint32, config *p4.MeterConfig) *p4.MeterEntry { - meterName := "" - - switch meterID { - case p4constants.MeterPreQosPipeAppMeter: - meterName = p4constants.GetMeterIDToNameMap()[p4constants.MeterPreQosPipeAppMeter] - case p4constants.MeterPreQosPipeSessionMeter: - meterName = p4constants.GetMeterIDToNameMap()[p4constants.MeterPreQosPipeSessionMeter] - } + meterName := p4constants.GetMeterIDToNameMap()[meterID] builderLog := log.WithFields(log.Fields{ "Meter": meterName, From fa8a58c6e2750a72ca53d2220f6e1d5f126d0d6b Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Tue, 22 Feb 2022 16:27:32 +0100 Subject: [PATCH 24/53] Fix golint --- pfcpiface/up4.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index 61d6da2d9..4833c30db 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -211,18 +211,19 @@ func (up4 *UP4) setupChannel() error { func (up4 *UP4) initAllCounters() error { log.Debug("Initializing counter for UP4") - preQosCounterIdentifier, preQoSCounterSize := p4constants.CounterPreQosPipePreQosCounter, p4constants.CounterSizePreQosPipePreQosCounter - preQosCounterName := p4constants.GetCounterIDToNameMap()[preQosCounterIdentifier] - err := up4.initCounter(preQosCounterID, preQosCounterName, preQoSCounterSize) + counterID, counterSize := p4constants.CounterPreQosPipePreQosCounter, p4constants.CounterSizePreQosPipePreQosCounter + counterName := p4constants.GetCounterIDToNameMap()[counterID] + + err := up4.initCounter(preQosCounterID, counterName, counterSize) if err != nil { return ErrOperationFailedWithReason("init preQosCounterID counter", err.Error()) } - postQosCounterIdentifier, postQoSCounterSize := p4constants.CounterPreQosPipePreQosCounter, p4constants.CounterSizePreQosPipePreQosCounter - postQosCounterName := p4constants.GetCounterIDToNameMap()[postQosCounterIdentifier] + counterID, counterSize = p4constants.CounterPostQosPipePostQosCounter, p4constants.CounterSizePostQosPipePostQosCounter + counterName = p4constants.GetCounterIDToNameMap()[counterID] - err = up4.initCounter(postQosCounterID, postQosCounterName, postQoSCounterSize) + err = up4.initCounter(postQosCounterID, counterName, counterSize) if err != nil { return ErrOperationFailedWithReason("init postQosCounterID counter", err.Error()) } From bc1db35de1b5cd88a1568e0f1413ac761b05aa26 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Wed, 23 Feb 2022 21:58:46 +0100 Subject: [PATCH 25/53] Refactor. Fix case in autogenerated const names --- internal/p4constants/p4constants.go | 110 ++++++++++++++-------------- scripts/go_gen_p4_const.go | 28 +++---- 2 files changed, 70 insertions(+), 68 deletions(-) diff --git a/internal/p4constants/p4constants.go b/internal/p4constants/p4constants.go index 689e4fc6a..1f0fa4627 100644 --- a/internal/p4constants/p4constants.go +++ b/internal/p4constants/p4constants.go @@ -5,30 +5,30 @@ package p4constants const ( // HeaderFields - HdrPreQosPipeRoutingRoutesV4dstPrefix uint32 = 1 - HdrPreQosPipeAclAclsinport uint32 = 1 - HdrPreQosPipeAclAclssrcIface uint32 = 2 - HdrPreQosPipeAclAclsethSrc uint32 = 3 - HdrPreQosPipeAclAclsethDst uint32 = 4 - HdrPreQosPipeAclAclsethType uint32 = 5 - HdrPreQosPipeAclAclsipv4Src uint32 = 6 - HdrPreQosPipeAclAclsipv4Dst uint32 = 7 - HdrPreQosPipeAclAclsipv4Proto uint32 = 8 - HdrPreQosPipeAclAclsl4Sport uint32 = 9 - HdrPreQosPipeAclAclsl4Dport uint32 = 10 - HdrPreQosPipeMyStationdstMac uint32 = 1 - HdrPreQosPipeInterfacesipv4DstPrefix uint32 = 1 - HdrPreQosPipeSessionsUplinkn3Address uint32 = 1 - HdrPreQosPipeSessionsUplinkteid uint32 = 2 - HdrPreQosPipeSessionsDownlinkueAddress uint32 = 1 - HdrPreQosPipeTerminationsUplinkueAddress uint32 = 1 - HdrPreQosPipeTerminationsUplinkappId uint32 = 2 - HdrPreQosPipeTerminationsDownlinkueAddress uint32 = 1 - HdrPreQosPipeTerminationsDownlinkappId uint32 = 2 - HdrPreQosPipeApplicationsappIpAddr uint32 = 1 - HdrPreQosPipeApplicationsappL4Port uint32 = 2 - HdrPreQosPipeApplicationsappIpProto uint32 = 3 - HdrPreQosPipeTunnelPeerstunnelPeerId uint32 = 1 + HdrPreQosPipeRoutingRoutesV4DstPrefix uint32 = 1 + HdrPreQosPipeAclAclsInport uint32 = 1 + HdrPreQosPipeAclAclsSrcIface uint32 = 2 + HdrPreQosPipeAclAclsEthSrc uint32 = 3 + HdrPreQosPipeAclAclsEthDst uint32 = 4 + HdrPreQosPipeAclAclsEthType uint32 = 5 + HdrPreQosPipeAclAclsIpv4Src uint32 = 6 + HdrPreQosPipeAclAclsIpv4Dst uint32 = 7 + HdrPreQosPipeAclAclsIpv4Proto uint32 = 8 + HdrPreQosPipeAclAclsL4Sport uint32 = 9 + HdrPreQosPipeAclAclsL4Dport uint32 = 10 + HdrPreQosPipeMyStationDstMac uint32 = 1 + HdrPreQosPipeInterfacesIpv4DstPrefix uint32 = 1 + HdrPreQosPipeSessionsUplinkN3Address uint32 = 1 + HdrPreQosPipeSessionsUplinkTeid uint32 = 2 + HdrPreQosPipeSessionsDownlinkUeAddress uint32 = 1 + HdrPreQosPipeTerminationsUplinkUeAddress uint32 = 1 + HdrPreQosPipeTerminationsUplinkAppId uint32 = 2 + HdrPreQosPipeTerminationsDownlinkUeAddress uint32 = 1 + HdrPreQosPipeTerminationsDownlinkAppId uint32 = 2 + HdrPreQosPipeApplicationsAppIpAddr uint32 = 1 + HdrPreQosPipeApplicationsAppL4Port uint32 = 2 + HdrPreQosPipeApplicationsAppIpProto uint32 = 3 + HdrPreQosPipeTunnelPeersTunnelPeerId uint32 = 1 // Tables TablePreQosPipeRoutingRoutesV4 uint32 = 39015874 TablePreQosPipeAclAcls uint32 = 47204971 @@ -67,37 +67,37 @@ const ( ActionPreQosPipeDoGtpuTunnel uint32 = 29247910 ActionPreQosPipeDoGtpuTunnelWithPsc uint32 = 31713420 // ActionParams - ActionParamPreQosPipeRoutingRoutesrcMac uint32 = 1 - ActionParamPreQosPipeRoutingRoutedstMac uint32 = 2 - ActionParamPreQosPipeRoutingRouteegressPort uint32 = 3 - ActionParamPreQosPipeAclSetPortport uint32 = 1 - ActionParamPreQosPipeSetSourceIfacesrcIface uint32 = 1 - ActionParamPreQosPipeSetSourceIfacedirection uint32 = 2 - ActionParamPreQosPipeSetSourceIfacesliceId uint32 = 3 - ActionParamPreQosPipeSetSessionUplinksessionMeterIdx uint32 = 1 - ActionParamPreQosPipeSetSessionDownlinktunnelPeerId uint32 = 1 - ActionParamPreQosPipeSetSessionDownlinksessionMeterIdx uint32 = 2 - ActionParamPreQosPipeSetSessionDownlinkBuffsessionMeterIdx uint32 = 1 - ActionParamPreQosPipeUplinkTermFwdNoTcctrIdx uint32 = 1 - ActionParamPreQosPipeUplinkTermFwdNoTcappMeterIdx uint32 = 2 - ActionParamPreQosPipeUplinkTermFwdctrIdx uint32 = 1 - ActionParamPreQosPipeUplinkTermFwdtc uint32 = 2 - ActionParamPreQosPipeUplinkTermFwdappMeterIdx uint32 = 3 - ActionParamPreQosPipeUplinkTermDropctrIdx uint32 = 1 - ActionParamPreQosPipeDownlinkTermFwdNoTcctrIdx uint32 = 1 - ActionParamPreQosPipeDownlinkTermFwdNoTcteid uint32 = 2 - ActionParamPreQosPipeDownlinkTermFwdNoTcqfi uint32 = 3 - ActionParamPreQosPipeDownlinkTermFwdNoTcappMeterIdx uint32 = 4 - ActionParamPreQosPipeDownlinkTermFwdctrIdx uint32 = 1 - ActionParamPreQosPipeDownlinkTermFwdteid uint32 = 2 - ActionParamPreQosPipeDownlinkTermFwdqfi uint32 = 3 - ActionParamPreQosPipeDownlinkTermFwdtc uint32 = 4 - ActionParamPreQosPipeDownlinkTermFwdappMeterIdx uint32 = 5 - ActionParamPreQosPipeDownlinkTermDropctrIdx uint32 = 1 - ActionParamPreQosPipeSetAppIdappId uint32 = 1 - ActionParamPreQosPipeLoadTunnelParamsrcAddr uint32 = 1 - ActionParamPreQosPipeLoadTunnelParamdstAddr uint32 = 2 - ActionParamPreQosPipeLoadTunnelParamsport uint32 = 3 + ActionParamPreQosPipeRoutingRouteSrcMac uint32 = 1 + ActionParamPreQosPipeRoutingRouteDstMac uint32 = 2 + ActionParamPreQosPipeRoutingRouteEgressPort uint32 = 3 + ActionParamPreQosPipeAclSetPortPort uint32 = 1 + ActionParamPreQosPipeSetSourceIfaceSrcIface uint32 = 1 + ActionParamPreQosPipeSetSourceIfaceDirection uint32 = 2 + ActionParamPreQosPipeSetSourceIfaceSliceId uint32 = 3 + ActionParamPreQosPipeSetSessionUplinkSessionMeterIdx uint32 = 1 + ActionParamPreQosPipeSetSessionDownlinkTunnelPeerId uint32 = 1 + ActionParamPreQosPipeSetSessionDownlinkSessionMeterIdx uint32 = 2 + ActionParamPreQosPipeSetSessionDownlinkBuffSessionMeterIdx uint32 = 1 + ActionParamPreQosPipeUplinkTermFwdNoTcCtrIdx uint32 = 1 + ActionParamPreQosPipeUplinkTermFwdNoTcAppMeterIdx uint32 = 2 + ActionParamPreQosPipeUplinkTermFwdCtrIdx uint32 = 1 + ActionParamPreQosPipeUplinkTermFwdTc uint32 = 2 + ActionParamPreQosPipeUplinkTermFwdAppMeterIdx uint32 = 3 + ActionParamPreQosPipeUplinkTermDropCtrIdx uint32 = 1 + ActionParamPreQosPipeDownlinkTermFwdNoTcCtrIdx uint32 = 1 + ActionParamPreQosPipeDownlinkTermFwdNoTcTeid uint32 = 2 + ActionParamPreQosPipeDownlinkTermFwdNoTcQfi uint32 = 3 + ActionParamPreQosPipeDownlinkTermFwdNoTcAppMeterIdx uint32 = 4 + ActionParamPreQosPipeDownlinkTermFwdCtrIdx uint32 = 1 + ActionParamPreQosPipeDownlinkTermFwdTeid uint32 = 2 + ActionParamPreQosPipeDownlinkTermFwdQfi uint32 = 3 + ActionParamPreQosPipeDownlinkTermFwdTc uint32 = 4 + ActionParamPreQosPipeDownlinkTermFwdAppMeterIdx uint32 = 5 + ActionParamPreQosPipeDownlinkTermDropCtrIdx uint32 = 1 + ActionParamPreQosPipeSetAppIdAppId uint32 = 1 + ActionParamPreQosPipeLoadTunnelParamSrcAddr uint32 = 1 + ActionParamPreQosPipeLoadTunnelParamDstAddr uint32 = 2 + ActionParamPreQosPipeLoadTunnelParamSport uint32 = 3 // IndirectCounters CounterPreQosPipePreQosCounter uint32 = 315693181 CounterSizePreQosPipePreQosCounter uint64 = 1024 diff --git a/scripts/go_gen_p4_const.go b/scripts/go_gen_p4_const.go index b0d92efa0..0f48af029 100644 --- a/scripts/go_gen_p4_const.go +++ b/scripts/go_gen_p4_const.go @@ -60,15 +60,17 @@ const ( metersListFunc = "func GetMeterIDList() []uint32 {\n return []uint32 {\n" ) -func emitEntityConstant(p4EntityName string, id uint32) string { +func emitEntityConstant(prefix string, p4EntityName string, id uint32) string { // see: https://go.dev/ref/spec#Identifiers + p4EntityName = prefix + "_" + p4EntityName p4EntityName = strings.Replace(p4EntityName, ".", "_", -1) p4EntityName = strcase.ToPascal(p4EntityName) return fmt.Sprintf("%s \t %s = %v\n", p4EntityName, idTypeString, id) } -func emitEntitySizeConstant(p4EntityName string, id int64) string { +func emitEntitySizeConstant(prefix string, p4EntityName string, id int64) string { // see: https://go.dev/ref/spec#Identifiers + p4EntityName = prefix + "_" + p4EntityName p4EntityName = strings.Replace(p4EntityName, ".", "_", -1) p4EntityName = strcase.ToPascal(p4EntityName) return fmt.Sprintf("%s \t %s = %v\n", p4EntityName, sizeTypeString, id) @@ -88,7 +90,7 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { for _, matchField := range element.MatchFields { tableName, name := element.GetPreamble().GetName(), matchField.GetName() - constBuilder.WriteString(emitEntityConstant(hfVarPrefix+tableName+name, matchField.GetId())) + constBuilder.WriteString(emitEntityConstant(hfVarPrefix+tableName, name, matchField.GetId())) } } // Tables @@ -98,7 +100,7 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { for _, element := range p4info.GetTables() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - constBuilder.WriteString(emitEntityConstant(tblVarPrefix+name, ID)) + constBuilder.WriteString(emitEntityConstant(tblVarPrefix, name, ID)) mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } @@ -112,7 +114,7 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { for _, element := range p4info.GetActions() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - constBuilder.WriteString(emitEntityConstant(actVarPrefix+name, ID)) + constBuilder.WriteString(emitEntityConstant(actVarPrefix, name, ID)) mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } @@ -125,7 +127,7 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { for _, actionParam := range element.GetParams() { actionName, name := element.GetPreamble().GetName(), actionParam.GetName() - constBuilder.WriteString(emitEntityConstant(actparamVarPrefix+actionName+name, actionParam.GetId())) + constBuilder.WriteString(emitEntityConstant(actparamVarPrefix+actionName, name, actionParam.GetId())) } } @@ -136,8 +138,8 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { for _, element := range p4info.GetCounters() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - constBuilder.WriteString(emitEntityConstant(ctrVarPrefix+name, ID)) - constBuilder.WriteString(emitEntitySizeConstant(ctrSizeVarPrefix+name, element.GetSize())) + constBuilder.WriteString(emitEntityConstant(ctrVarPrefix, name, ID)) + constBuilder.WriteString(emitEntitySizeConstant(ctrSizeVarPrefix, name, element.GetSize())) mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } @@ -151,7 +153,7 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { for _, element := range p4info.GetDirectCounters() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - constBuilder.WriteString(emitEntityConstant(dirCtrVarPrefix+name, element.GetPreamble().GetId())) + constBuilder.WriteString(emitEntityConstant(dirCtrVarPrefix, name, element.GetPreamble().GetId())) mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } @@ -165,7 +167,7 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { for _, element := range p4info.GetActionProfiles() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - constBuilder.WriteString(emitEntityConstant(actprofVarPrefix+name, ID)) + constBuilder.WriteString(emitEntityConstant(actprofVarPrefix, name, ID)) mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } @@ -179,7 +181,7 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { for _, element := range p4info.GetControllerPacketMetadata() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - constBuilder.WriteString(emitEntityConstant(packetmetaVarPrefix+name, ID)) + constBuilder.WriteString(emitEntityConstant(packetmetaVarPrefix, name, ID)) mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } @@ -193,8 +195,8 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { for _, element := range p4info.GetMeters() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - constBuilder.WriteString(emitEntityConstant(mtrVarPrefix+name, ID)) - constBuilder.WriteString(emitEntitySizeConstant(mtrSizeVarPrefix+name, element.GetSize())) + constBuilder.WriteString(emitEntityConstant(mtrVarPrefix, name, ID)) + constBuilder.WriteString(emitEntitySizeConstant(mtrSizeVarPrefix, name, element.GetSize())) mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } From 0e535164136d13de3ad891e1c19ccd0db8a175de Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 24 Feb 2022 09:54:10 +0100 Subject: [PATCH 26/53] Add interpreter to evaluate script output --- go.mod | 1 + go.sum | 2 ++ scripts/go_gen_p4_const_test.go | 17 +++++++++++++++++ 3 files changed, 20 insertions(+) create mode 100644 scripts/go_gen_p4_const_test.go diff --git a/go.mod b/go.mod index bf175a330..6515d2992 100644 --- a/go.mod +++ b/go.mod @@ -22,6 +22,7 @@ require ( github.com/prometheus/client_golang v1.11.0 github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.7.0 + github.com/traefik/yaegi v0.11.2 github.com/wmnsk/go-pfcp v0.0.14 golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a diff --git a/go.sum b/go.sum index 66a7a14d7..58c31796c 100644 --- a/go.sum +++ b/go.sum @@ -620,6 +620,8 @@ github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/traefik/yaegi v0.11.2 h1:zosveTf5iIa60fAeQpaH4719b+bnlgsOvO7Nb/OTMTo= +github.com/traefik/yaegi v0.11.2/go.mod h1:RuCwD8/wsX7b6KoQHOaIFUfuH3gQIK4KWnFFmJMw5VA= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= diff --git a/scripts/go_gen_p4_const_test.go b/scripts/go_gen_p4_const_test.go new file mode 100644 index 000000000..5a684fe83 --- /dev/null +++ b/scripts/go_gen_p4_const_test.go @@ -0,0 +1,17 @@ +package main + +import ( + "testing" + + "github.com/stretchr/testify/require" + "github.com/traefik/yaegi/interp" + "github.com/traefik/yaegi/stdlib" +) + +func Test_generate(t *testing.T) { + interpreter := interp.New(interp.Options{}) + + err := interpreter.Use(stdlib.Symbols) + require.NoError(t, err) + +} From 3c9cec0b0c27290025c7572e11a8b9f8bb52cddc Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 24 Feb 2022 11:00:03 +0100 Subject: [PATCH 27/53] Split generator in multiple functions --- internal/p4constants/p4constants.go | 102 +++++++-------- scripts/go_gen_p4_const.go | 188 ++++++++++++++++++++-------- 2 files changed, 190 insertions(+), 100 deletions(-) diff --git a/internal/p4constants/p4constants.go b/internal/p4constants/p4constants.go index 1f0fa4627..455d27c8b 100644 --- a/internal/p4constants/p4constants.go +++ b/internal/p4constants/p4constants.go @@ -132,6 +132,21 @@ func GetTableIDToNameMap() map[uint32]string { } } +func GetTableIDList() []uint32 { + return []uint32{ + 39015874, + 47204971, + 40931612, + 33923840, + 44976597, + 34742049, + 37595532, + 34778590, + 46868458, + 49497304, + } +} + func GetActionIDToNameMap() map[uint32]string { return map[uint32]string{ 21257015: "NoAction", @@ -162,54 +177,6 @@ func GetActionIDToNameMap() map[uint32]string { } } -func GetCounterIDToNameMap() map[uint32]string { - return map[uint32]string{ - 315693181: "PreQosPipe.pre_qos_counter", - 302958180: "PostQosPipe.post_qos_counter", - } -} - -func GetDirectCounterIDToNameMap() map[uint32]string { - return map[uint32]string{ - 325583051: "acls", - } -} - -func GetActionProfileIDToNameMap() map[uint32]string { - return map[uint32]string{ - 297808402: "hashed_selector", - } -} - -func GetPacketMetadataIDToNameMap() map[uint32]string { - return map[uint32]string{ - 75327753: "packet_out", - 80671331: "packet_in", - } -} - -func GetMeterIDToNameMap() map[uint32]string { - return map[uint32]string{ - 338231090: "PreQosPipe.app_meter", - 347593234: "PreQosPipe.session_meter", - } -} - -func GetTableIDList() []uint32 { - return []uint32{ - 39015874, - 47204971, - 40931612, - 33923840, - 44976597, - 34742049, - 37595532, - 34778590, - 46868458, - 49497304, - } -} - func GetActionIDList() []uint32 { return []uint32{ 21257015, @@ -240,6 +207,25 @@ func GetActionIDList() []uint32 { } } +func GetDirectCounterIDToNameMap() map[uint32]string { + return map[uint32]string{ + 325583051: "acls", + } +} + +func GetDirectCounterIDList() []uint32 { + return []uint32{ + 325583051, + } +} + +func GetCounterIDToNameMap() map[uint32]string { + return map[uint32]string{ + 315693181: "PreQosPipe.pre_qos_counter", + 302958180: "PostQosPipe.post_qos_counter", + } +} + func GetCounterIDList() []uint32 { return []uint32{ 315693181, @@ -247,9 +233,9 @@ func GetCounterIDList() []uint32 { } } -func GetDirectCounterIDList() []uint32 { - return []uint32{ - 325583051, +func GetActionProfileIDToNameMap() map[uint32]string { + return map[uint32]string{ + 297808402: "hashed_selector", } } @@ -259,6 +245,13 @@ func GetActionProfileIDList() []uint32 { } } +func GetPacketMetadataIDToNameMap() map[uint32]string { + return map[uint32]string{ + 75327753: "packet_out", + 80671331: "packet_in", + } +} + func GetPacketMetadataIDList() []uint32 { return []uint32{ 75327753, @@ -266,6 +259,13 @@ func GetPacketMetadataIDList() []uint32 { } } +func GetMeterIDToNameMap() map[uint32]string { + return map[uint32]string{ + 338231090: "PreQosPipe.app_meter", + 347593234: "PreQosPipe.session_meter", + } +} + func GetMeterIDList() []uint32 { return []uint32{ 338231090, diff --git a/scripts/go_gen_p4_const.go b/scripts/go_gen_p4_const.go index 0f48af029..cdf58ab4b 100644 --- a/scripts/go_gen_p4_const.go +++ b/scripts/go_gen_p4_const.go @@ -76,12 +76,129 @@ func emitEntitySizeConstant(prefix string, p4EntityName string, id int64) string return fmt.Sprintf("%s \t %s = %v\n", p4EntityName, sizeTypeString, id) } -func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { - constBuilder, mapBuilder, listBuilder := strings.Builder{}, strings.Builder{}, strings.Builder{} +func generateTables(p4info *p4ConfigV1.P4Info) string { + mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} - constBuilder.WriteString(copyrightHeader + "\n") + mapBuilder.WriteString(tableMapFunc) + listBuilder.WriteString(tableListFunc) + for _, element := range p4info.GetTables() { + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) + } + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close func declaration + + return mapBuilder.String() + listBuilder.String() +} + +func generateActions(p4info *p4ConfigV1.P4Info) string { + mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} + + mapBuilder.WriteString(actionMapFunc) + listBuilder.WriteString(actionListFunc) + for _, element := range p4info.GetActions() { + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) + } + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close func declarations + + return mapBuilder.String() + listBuilder.String() +} + +func generateIndirectCounters(p4info *p4ConfigV1.P4Info) string { + mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} + + mapBuilder.WriteString(counterMapFunc) + listBuilder.WriteString(counterListFunc) + for _, element := range p4info.GetCounters() { + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) + } + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close func declarations + + return mapBuilder.String() + listBuilder.String() +} + +func generateDirectCounters(p4info *p4ConfigV1.P4Info) string { + mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} + + mapBuilder.WriteString(directCounterMapFunc) + listBuilder.WriteString(directCounterListFunc) + for _, element := range p4info.GetDirectCounters() { + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) + } + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close declarations + + return mapBuilder.String() + listBuilder.String() +} + +func generateMeters(p4info *p4ConfigV1.P4Info) string { + mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} + + // Meters + mapBuilder.WriteString(metersMapFunc) + listBuilder.WriteString(metersListFunc) + for _, element := range p4info.GetMeters() { + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) + } + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close declarations + + return mapBuilder.String() + listBuilder.String() +} + +func generateActionProfiles(p4info *p4ConfigV1.P4Info) string { + mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} + + mapBuilder.WriteString(actionProfileMapFunc) + listBuilder.WriteString(actionProfileListFunc) + for _, element := range p4info.GetActionProfiles() { + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) + } + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close declarations + + return mapBuilder.String() + listBuilder.String() +} + +func generatePacketMetadata(p4info *p4ConfigV1.P4Info) string { + mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} + + mapBuilder.WriteString(pktMetadataMapFunc) + listBuilder.WriteString(pktMetadataListFunc) + for _, element := range p4info.GetControllerPacketMetadata() { + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) + } + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close declarations + + return mapBuilder.String() + listBuilder.String() +} + +func generateConstants(p4info *p4ConfigV1.P4Info) string { + constBuilder := strings.Builder{} - constBuilder.WriteString(fmt.Sprintf("package %s\n", packageName)) constBuilder.WriteString(constOpen) //HeaderField IDs @@ -95,31 +212,19 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { } // Tables constBuilder.WriteString("// Tables\n") - mapBuilder.WriteString(tableMapFunc) - listBuilder.WriteString(tableListFunc) for _, element := range p4info.GetTables() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() constBuilder.WriteString(emitEntityConstant(tblVarPrefix, name, ID)) - mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) - listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } - mapBuilder.WriteString("}\n}\n\n") - listBuilder.WriteString("}\n}\n\n") //Close func declaration // Actions constBuilder.WriteString("// Actions\n") - mapBuilder.WriteString(actionMapFunc) - listBuilder.WriteString(actionListFunc) for _, element := range p4info.GetActions() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() constBuilder.WriteString(emitEntityConstant(actVarPrefix, name, ID)) - mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) - listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } - mapBuilder.WriteString("}\n}\n\n") - listBuilder.WriteString("}\n}\n\n") //Close func declarations // Action Param IDs constBuilder.WriteString("// ActionParams\n") @@ -133,79 +238,49 @@ func generateP4Constants(p4info *p4ConfigV1.P4Info, packageName string) string { // Indirect Counters constBuilder.WriteString("// IndirectCounters\n") - mapBuilder.WriteString(counterMapFunc) - listBuilder.WriteString(counterListFunc) for _, element := range p4info.GetCounters() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() constBuilder.WriteString(emitEntityConstant(ctrVarPrefix, name, ID)) constBuilder.WriteString(emitEntitySizeConstant(ctrSizeVarPrefix, name, element.GetSize())) - mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) - listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } - mapBuilder.WriteString("}\n}\n\n") - listBuilder.WriteString("}\n}\n\n") //Close func declarations // Direct Counters constBuilder.WriteString("// DirectCounters\n") - mapBuilder.WriteString(directCounterMapFunc) - listBuilder.WriteString(directCounterListFunc) for _, element := range p4info.GetDirectCounters() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - constBuilder.WriteString(emitEntityConstant(dirCtrVarPrefix, name, element.GetPreamble().GetId())) - mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) - listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) + constBuilder.WriteString(emitEntityConstant(dirCtrVarPrefix, name, ID)) } - mapBuilder.WriteString("}\n}\n\n") - listBuilder.WriteString("}\n}\n\n") //Close declarations // Action profiles constBuilder.WriteString("// ActionProfiles\n") - mapBuilder.WriteString(actionProfileMapFunc) - listBuilder.WriteString(actionProfileListFunc) for _, element := range p4info.GetActionProfiles() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() constBuilder.WriteString(emitEntityConstant(actprofVarPrefix, name, ID)) - mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) - listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } - mapBuilder.WriteString("}\n}\n\n") - listBuilder.WriteString("}\n}\n\n") //Close declarations // Packet metadata constBuilder.WriteString("// PacketMetadata\n") - mapBuilder.WriteString(pktMetadataMapFunc) - listBuilder.WriteString(pktMetadataListFunc) for _, element := range p4info.GetControllerPacketMetadata() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() constBuilder.WriteString(emitEntityConstant(packetmetaVarPrefix, name, ID)) - mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) - listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } - mapBuilder.WriteString("}\n}\n\n") - listBuilder.WriteString("}\n}\n\n") //Close declarations // Meters constBuilder.WriteString("// Meters\n") - mapBuilder.WriteString(metersMapFunc) - listBuilder.WriteString(metersListFunc) for _, element := range p4info.GetMeters() { name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() constBuilder.WriteString(emitEntityConstant(mtrVarPrefix, name, ID)) constBuilder.WriteString(emitEntitySizeConstant(mtrSizeVarPrefix, name, element.GetSize())) - mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) - listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) } - mapBuilder.WriteString("}\n}\n\n") - listBuilder.WriteString("}\n}\n\n") //Close declarations constBuilder.WriteString(constOrVarClose + "\n") - return constBuilder.String() + mapBuilder.String() + listBuilder.String() + return constBuilder.String() } func getP4Config(p4infopath string) *p4ConfigV1.P4Info { @@ -233,7 +308,22 @@ func main() { p4config := getP4Config(*p4infoPath) - result := generateP4Constants(p4config, *packageName) + headerBuilder := strings.Builder{} + + headerBuilder.WriteString(copyrightHeader + "\n") + headerBuilder.WriteString(fmt.Sprintf("package %s\n", *packageName)) + + headerBuilder.WriteString(generateConstants(p4config)) + + headerBuilder.WriteString(generateTables(p4config)) + headerBuilder.WriteString(generateActions(p4config)) + headerBuilder.WriteString(generateDirectCounters(p4config)) + headerBuilder.WriteString(generateIndirectCounters(p4config)) + headerBuilder.WriteString(generateActionProfiles(p4config)) + headerBuilder.WriteString(generatePacketMetadata(p4config)) + headerBuilder.WriteString(generateMeters(p4config)) + + result := headerBuilder.String() if *outputPath == "-" { fmt.Println(result) From aa536e9eadac83e34fbfc835e5a85688a5d252be Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 24 Feb 2022 15:15:06 +0100 Subject: [PATCH 28/53] Add test for constants and table generation --- go.mod | 1 - go.sum | 2 - scripts/dummy_p4info.txt | 829 ++++++++++++++++++++++++++++++++ scripts/go_gen_p4_const_test.go | 25 +- 4 files changed, 849 insertions(+), 8 deletions(-) create mode 100644 scripts/dummy_p4info.txt diff --git a/go.mod b/go.mod index 6515d2992..bf175a330 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,6 @@ require ( github.com/prometheus/client_golang v1.11.0 github.com/sirupsen/logrus v1.8.1 github.com/stretchr/testify v1.7.0 - github.com/traefik/yaegi v0.11.2 github.com/wmnsk/go-pfcp v0.0.14 golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a diff --git a/go.sum b/go.sum index 58c31796c..66a7a14d7 100644 --- a/go.sum +++ b/go.sum @@ -620,8 +620,6 @@ github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG github.com/tchap/go-patricia v2.2.6+incompatible/go.mod h1:bmLyhP68RS6kStMGxByiQ23RP/odRBOTVjwp2cDyi6I= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= -github.com/traefik/yaegi v0.11.2 h1:zosveTf5iIa60fAeQpaH4719b+bnlgsOvO7Nb/OTMTo= -github.com/traefik/yaegi v0.11.2/go.mod h1:RuCwD8/wsX7b6KoQHOaIFUfuH3gQIK4KWnFFmJMw5VA= github.com/ugorji/go v1.1.4/go.mod h1:uQMGLiO92mf5W77hV/PUCpI3pbzQx3CRekS0kk+RGrc= github.com/urfave/cli v0.0.0-20171014202726-7bc6a0acffa5/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA= diff --git a/scripts/dummy_p4info.txt b/scripts/dummy_p4info.txt new file mode 100644 index 000000000..f1f6ee44a --- /dev/null +++ b/scripts/dummy_p4info.txt @@ -0,0 +1,829 @@ +pkg_info { + arch: "v1model" +} +tables { + preamble { + id: 39015874 + name: "PreQosPipe.Routing.routes_v4" + alias: "routes_v4" + } + match_fields { + id: 1 + name: "dst_prefix" + bitwidth: 32 + match_type: LPM + } + action_refs { + id: 23965128 + } + action_refs { + id: 21257015 + annotations: "@defaultonly" + scope: DEFAULT_ONLY + } + implementation_id: 297808402 + size: 1024 +} +tables { + preamble { + id: 47204971 + name: "PreQosPipe.Acl.acls" + alias: "Acl.acls" + } + match_fields { + id: 1 + name: "inport" + bitwidth: 9 + match_type: TERNARY + } + match_fields { + id: 2 + name: "src_iface" + bitwidth: 8 + match_type: TERNARY + } + match_fields { + id: 3 + name: "eth_src" + bitwidth: 48 + match_type: TERNARY + } + match_fields { + id: 4 + name: "eth_dst" + bitwidth: 48 + match_type: TERNARY + } + match_fields { + id: 5 + name: "eth_type" + bitwidth: 16 + match_type: TERNARY + } + match_fields { + id: 6 + name: "ipv4_src" + bitwidth: 32 + match_type: TERNARY + } + match_fields { + id: 7 + name: "ipv4_dst" + bitwidth: 32 + match_type: TERNARY + } + match_fields { + id: 8 + name: "ipv4_proto" + bitwidth: 8 + match_type: TERNARY + } + match_fields { + id: 9 + name: "l4_sport" + bitwidth: 16 + match_type: TERNARY + } + match_fields { + id: 10 + name: "l4_dport" + bitwidth: 16 + match_type: TERNARY + } + action_refs { + id: 30494847 + } + action_refs { + id: 26495283 + } + action_refs { + id: 21596798 + } + action_refs { + id: 18812293 + } + action_refs { + id: 21257015 + } + const_default_action_id: 21257015 + direct_resource_ids: 325583051 + size: 1024 +} +tables { + preamble { + id: 40931612 + name: "PreQosPipe.my_station" + alias: "my_station" + } + match_fields { + id: 1 + name: "dst_mac" + bitwidth: 48 + match_type: EXACT + } + action_refs { + id: 21257015 + } + size: 1024 +} +tables { + preamble { + id: 33923840 + name: "PreQosPipe.interfaces" + alias: "interfaces" + } + match_fields { + id: 1 + name: "ipv4_dst_prefix" + bitwidth: 32 + match_type: LPM + } + action_refs { + id: 26090030 + } + const_default_action_id: 26090030 + size: 1024 +} +tables { + preamble { + id: 44976597 + name: "PreQosPipe.sessions_uplink" + alias: "sessions_uplink" + } + match_fields { + id: 1 + name: "n3_address" + bitwidth: 32 + match_type: EXACT + } + match_fields { + id: 2 + name: "teid" + bitwidth: 32 + match_type: EXACT + } + action_refs { + id: 19461580 + } + action_refs { + id: 22196934 + } + action_refs { + id: 28401267 + annotations: "@defaultonly" + scope: DEFAULT_ONLY + } + const_default_action_id: 28401267 + size: 1024 +} +tables { + preamble { + id: 34742049 + name: "PreQosPipe.sessions_downlink" + alias: "sessions_downlink" + } + match_fields { + id: 1 + name: "ue_address" + bitwidth: 32 + match_type: EXACT + } + action_refs { + id: 21848329 + } + action_refs { + id: 20229579 + } + action_refs { + id: 20249483 + } + action_refs { + id: 28401267 + annotations: "@defaultonly" + scope: DEFAULT_ONLY + } + const_default_action_id: 28401267 + size: 1024 +} +tables { + preamble { + id: 37595532 + name: "PreQosPipe.terminations_uplink" + alias: "terminations_uplink" + } + match_fields { + id: 1 + name: "ue_address" + bitwidth: 32 + match_type: EXACT + } + match_fields { + id: 2 + name: "app_id" + bitwidth: 8 + match_type: EXACT + } + action_refs { + id: 28305359 + } + action_refs { + id: 21760615 + } + action_refs { + id: 20977365 + } + action_refs { + id: 28401267 + annotations: "@defaultonly" + scope: DEFAULT_ONLY + } + const_default_action_id: 28401267 + size: 1024 +} +tables { + preamble { + id: 34778590 + name: "PreQosPipe.terminations_downlink" + alias: "terminations_downlink" + } + match_fields { + id: 1 + name: "ue_address" + bitwidth: 32 + match_type: EXACT + } + match_fields { + id: 2 + name: "app_id" + bitwidth: 8 + match_type: EXACT + } + action_refs { + id: 32699713 + } + action_refs { + id: 31264233 + } + action_refs { + id: 26185804 + } + action_refs { + id: 28401267 + annotations: "@defaultonly" + scope: DEFAULT_ONLY + } + const_default_action_id: 28401267 + size: 1024 +} +tables { + preamble { + id: 46868458 + name: "PreQosPipe.applications" + alias: "applications" + } + match_fields { + id: 1 + name: "app_ip_addr" + bitwidth: 32 + match_type: LPM + } + match_fields { + id: 2 + name: "app_l4_port" + bitwidth: 16 + match_type: RANGE + } + match_fields { + id: 3 + name: "app_ip_proto" + bitwidth: 8 + match_type: TERNARY + } + action_refs { + id: 23010411 + } + const_default_action_id: 23010411 + size: 1024 +} +tables { + preamble { + id: 49497304 + name: "PreQosPipe.tunnel_peers" + alias: "tunnel_peers" + } + match_fields { + id: 1 + name: "tunnel_peer_id" + bitwidth: 8 + match_type: EXACT + } + action_refs { + id: 32742981 + } + action_refs { + id: 21257015 + annotations: "@defaultonly" + scope: DEFAULT_ONLY + } + size: 1024 +} +actions { + preamble { + id: 21257015 + name: "NoAction" + alias: "NoAction" + annotations: "@noWarn(\"unused\")" + } +} +actions { + preamble { + id: 31448256 + name: "PreQosPipe.Routing.drop" + alias: "Routing.drop" + } +} +actions { + preamble { + id: 23965128 + name: "PreQosPipe.Routing.route" + alias: "route" + } + params { + id: 1 + name: "src_mac" + bitwidth: 48 + } + params { + id: 2 + name: "dst_mac" + bitwidth: 48 + } + params { + id: 3 + name: "egress_port" + bitwidth: 9 + } +} +actions { + preamble { + id: 30494847 + name: "PreQosPipe.Acl.set_port" + alias: "set_port" + } + params { + id: 1 + name: "port" + bitwidth: 9 + } +} +actions { + preamble { + id: 26495283 + name: "PreQosPipe.Acl.punt" + alias: "punt" + } +} +actions { + preamble { + id: 21596798 + name: "PreQosPipe.Acl.clone_to_cpu" + alias: "clone_to_cpu" + } +} +actions { + preamble { + id: 18812293 + name: "PreQosPipe.Acl.drop" + alias: "Acl.drop" + } +} +actions { + preamble { + id: 23766285 + name: "PreQosPipe._initialize_metadata" + alias: "_initialize_metadata" + } +} +actions { + preamble { + id: 26090030 + name: "PreQosPipe.set_source_iface" + alias: "set_source_iface" + } + params { + id: 1 + name: "src_iface" + bitwidth: 8 + } + params { + id: 2 + name: "direction" + bitwidth: 8 + } + params { + id: 3 + name: "slice_id" + bitwidth: 4 + } +} +actions { + preamble { + id: 28401267 + name: "PreQosPipe.do_drop" + alias: "do_drop" + } +} +actions { + preamble { + id: 19461580 + name: "PreQosPipe.set_session_uplink" + alias: "set_session_uplink" + } + params { + id: 1 + name: "session_meter_idx" + bitwidth: 32 + } +} +actions { + preamble { + id: 22196934 + name: "PreQosPipe.set_session_uplink_drop" + alias: "set_session_uplink_drop" + } +} +actions { + preamble { + id: 21848329 + name: "PreQosPipe.set_session_downlink" + alias: "set_session_downlink" + } + params { + id: 1 + name: "tunnel_peer_id" + bitwidth: 8 + } + params { + id: 2 + name: "session_meter_idx" + bitwidth: 32 + } +} +actions { + preamble { + id: 20229579 + name: "PreQosPipe.set_session_downlink_drop" + alias: "set_session_downlink_drop" + } +} +actions { + preamble { + id: 20249483 + name: "PreQosPipe.set_session_downlink_buff" + alias: "set_session_downlink_buff" + } + params { + id: 1 + name: "session_meter_idx" + bitwidth: 32 + } +} +actions { + preamble { + id: 21760615 + name: "PreQosPipe.uplink_term_fwd_no_tc" + alias: "uplink_term_fwd_no_tc" + } + params { + id: 1 + name: "ctr_idx" + bitwidth: 32 + } + params { + id: 2 + name: "app_meter_idx" + bitwidth: 32 + } +} +actions { + preamble { + id: 28305359 + name: "PreQosPipe.uplink_term_fwd" + alias: "uplink_term_fwd" + } + params { + id: 1 + name: "ctr_idx" + bitwidth: 32 + } + params { + id: 2 + name: "tc" + bitwidth: 2 + } + params { + id: 3 + name: "app_meter_idx" + bitwidth: 32 + } +} +actions { + preamble { + id: 20977365 + name: "PreQosPipe.uplink_term_drop" + alias: "uplink_term_drop" + } + params { + id: 1 + name: "ctr_idx" + bitwidth: 32 + } +} +actions { + preamble { + id: 26185804 + name: "PreQosPipe.downlink_term_fwd_no_tc" + alias: "downlink_term_fwd_no_tc" + } + params { + id: 1 + name: "ctr_idx" + bitwidth: 32 + } + params { + id: 2 + name: "teid" + bitwidth: 32 + } + params { + id: 3 + name: "qfi" + bitwidth: 6 + } + params { + id: 4 + name: "app_meter_idx" + bitwidth: 32 + } +} +actions { + preamble { + id: 32699713 + name: "PreQosPipe.downlink_term_fwd" + alias: "downlink_term_fwd" + } + params { + id: 1 + name: "ctr_idx" + bitwidth: 32 + } + params { + id: 2 + name: "teid" + bitwidth: 32 + } + params { + id: 3 + name: "qfi" + bitwidth: 6 + } + params { + id: 4 + name: "tc" + bitwidth: 2 + } + params { + id: 5 + name: "app_meter_idx" + bitwidth: 32 + } +} +actions { + preamble { + id: 31264233 + name: "PreQosPipe.downlink_term_drop" + alias: "downlink_term_drop" + } + params { + id: 1 + name: "ctr_idx" + bitwidth: 32 + } +} +actions { + preamble { + id: 23010411 + name: "PreQosPipe.set_app_id" + alias: "set_app_id" + } + params { + id: 1 + name: "app_id" + bitwidth: 8 + } +} +actions { + preamble { + id: 32742981 + name: "PreQosPipe.load_tunnel_param" + alias: "load_tunnel_param" + } + params { + id: 1 + name: "src_addr" + bitwidth: 32 + } + params { + id: 2 + name: "dst_addr" + bitwidth: 32 + } + params { + id: 3 + name: "sport" + bitwidth: 16 + } +} +actions { + preamble { + id: 29247910 + name: "PreQosPipe.do_gtpu_tunnel" + alias: "do_gtpu_tunnel" + } +} +actions { + preamble { + id: 31713420 + name: "PreQosPipe.do_gtpu_tunnel_with_psc" + alias: "do_gtpu_tunnel_with_psc" + } +} +action_profiles { + preamble { + id: 297808402 + name: "hashed_selector" + alias: "hashed_selector" + } + table_ids: 39015874 + with_selector: true + size: 1024 +} +counters { + preamble { + id: 315693181 + name: "PreQosPipe.pre_qos_counter" + alias: "pre_qos_counter" + } + spec { + unit: BOTH + } + size: 1024 +} +counters { + preamble { + id: 302958180 + name: "PostQosPipe.post_qos_counter" + alias: "post_qos_counter" + } + spec { + unit: BOTH + } + size: 1024 +} +direct_counters { + preamble { + id: 325583051 + name: "acls" + alias: "acls" + } + spec { + unit: BOTH + } + direct_table_id: 47204971 +} +meters { + preamble { + id: 338231090 + name: "PreQosPipe.app_meter" + alias: "app_meter" + } + spec { + unit: BYTES + } + size: 1024 +} +meters { + preamble { + id: 347593234 + name: "PreQosPipe.session_meter" + alias: "session_meter" + } + spec { + unit: BYTES + } + size: 1024 +} +controller_packet_metadata { + preamble { + id: 75327753 + name: "packet_out" + alias: "packet_out" + annotations: "@controller_header(\"packet_out\")" + } + metadata { + id: 1 + name: "reserved" + bitwidth: 8 + } +} +controller_packet_metadata { + preamble { + id: 80671331 + name: "packet_in" + alias: "packet_in" + annotations: "@controller_header(\"packet_in\")" + } + metadata { + id: 1 + name: "ingress_port" + bitwidth: 9 + } + metadata { + id: 2 + name: "_pad" + bitwidth: 7 + } +} +digests { + preamble { + id: 396224266 + name: "ddn_digest_t" + alias: "ddn_digest_t" + } + type_spec { + struct { + name: "ddn_digest_t" + } + } +} +type_info { + structs { + key: "ddn_digest_t" + value { + members { + name: "ue_address" + type_spec { + bitstring { + bit { + bitwidth: 32 + } + } + } + } + } + } + serializable_enums { + key: "Direction" + value { + underlying_type { + bitwidth: 8 + } + members { + name: "UNKNOWN" + value: "\000" + } + members { + name: "UPLINK" + value: "\001" + } + members { + name: "DOWNLINK" + value: "\002" + } + members { + name: "OTHER" + value: "\003" + } + } + } + serializable_enums { + key: "InterfaceType" + value { + underlying_type { + bitwidth: 8 + } + members { + name: "UNKNOWN" + value: "\000" + } + members { + name: "ACCESS" + value: "\001" + } + members { + name: "CORE" + value: "\002" + } + } + } +} diff --git a/scripts/go_gen_p4_const_test.go b/scripts/go_gen_p4_const_test.go index 5a684fe83..535a135cd 100644 --- a/scripts/go_gen_p4_const_test.go +++ b/scripts/go_gen_p4_const_test.go @@ -1,17 +1,32 @@ package main import ( + "strings" "testing" "github.com/stretchr/testify/require" - "github.com/traefik/yaegi/interp" - "github.com/traefik/yaegi/stdlib" ) +const dummy_p4info = "dummy_p4info.txt" + func Test_generate(t *testing.T) { - interpreter := interp.New(interp.Options{}) + p4config := getP4Config(dummy_p4info) + + test := generateConstants(p4config) + + require.True(t, strings.Contains(test, "HdrPreQosPipeSessionsUplinkTeid \t uint32 = 2")) + require.True(t, strings.Contains(test, "TablePreQosPipeSessionsDownlink \t uint32 = 34742049")) + require.True(t, strings.Contains(test, "ActionParamPreQosPipeSetSessionUplinkSessionMeterIdx \t uint32 = 1")) + require.True(t, strings.Contains(test, "DirectCounterAcls \t uint32 = 325583051")) + require.True(t, strings.Contains(test, "PacketMetaPacketOut \t uint32 = 75327753")) + require.True(t, strings.Contains(test, "MeterPreQosPipeAppMeter \t uint32 = 338231090")) +} + +func Test_generateTables(t *testing.T) { + p4config := getP4Config(dummy_p4info) - err := interpreter.Use(stdlib.Symbols) - require.NoError(t, err) + test := generateTables(p4config) + require.True(t, strings.Contains(test, "39015874:\"PreQosPipe.Routing.routes_v4\",")) + require.True(t, strings.Contains(test, "34778590:\"PreQosPipe.terminations_downlink\",")) } From 10c76c398d5c5d5e62766832a67fdca7c6e07be6 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 24 Feb 2022 15:53:23 +0100 Subject: [PATCH 29/53] Refactor --- scripts/go_gen_p4_const_test.go | 75 ++++++++++++++++++++++++++------- 1 file changed, 59 insertions(+), 16 deletions(-) diff --git a/scripts/go_gen_p4_const_test.go b/scripts/go_gen_p4_const_test.go index 535a135cd..f5b8b9145 100644 --- a/scripts/go_gen_p4_const_test.go +++ b/scripts/go_gen_p4_const_test.go @@ -1,32 +1,75 @@ package main import ( + "strconv" "strings" "testing" + p4ConfigV1 "github.com/p4lang/p4runtime/go/p4/config/v1" "github.com/stretchr/testify/require" ) -const dummy_p4info = "dummy_p4info.txt" +const dummyP4info = "dummy_p4info.txt" func Test_generate(t *testing.T) { - p4config := getP4Config(dummy_p4info) + type args struct { + p4config *p4ConfigV1.P4Info + } - test := generateConstants(p4config) + type want struct { + ID int + name string + } - require.True(t, strings.Contains(test, "HdrPreQosPipeSessionsUplinkTeid \t uint32 = 2")) - require.True(t, strings.Contains(test, "TablePreQosPipeSessionsDownlink \t uint32 = 34742049")) - require.True(t, strings.Contains(test, "ActionParamPreQosPipeSetSessionUplinkSessionMeterIdx \t uint32 = 1")) - require.True(t, strings.Contains(test, "DirectCounterAcls \t uint32 = 325583051")) - require.True(t, strings.Contains(test, "PacketMetaPacketOut \t uint32 = 75327753")) - require.True(t, strings.Contains(test, "MeterPreQosPipeAppMeter \t uint32 = 338231090")) -} - -func Test_generateTables(t *testing.T) { - p4config := getP4Config(dummy_p4info) + tests := []struct { + name string + args *args + want *want + wantErr bool + }{ + { + name: "verify table const", + args: &args{ + p4config: getP4Config(dummyP4info), + }, + want: &want{ + ID: 40931612, + name: "TablePreQosPipeMyStation", + }, + }, + { + name: "verify action const", + args: &args{ + p4config: getP4Config(dummyP4info), + }, + want: &want{ + ID: 23766285, + name: "ActionPreQosPipeInitializeMetadata", + }, + }, + { + name: "non existing const", + args: &args{ + p4config: getP4Config(dummyP4info), + }, + want: &want{ + ID: 111111, + name: "test", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := generateConstants(tt.args.p4config) - test := generateTables(p4config) + idx := strings.Index(result, tt.want.name) + if idx == -1 && tt.wantErr { + return + } - require.True(t, strings.Contains(test, "39015874:\"PreQosPipe.Routing.routes_v4\",")) - require.True(t, strings.Contains(test, "34778590:\"PreQosPipe.terminations_downlink\",")) + line := strings.SplitN(result[idx:], "\n", 1) + require.True(t, strings.Contains(strings.Join(line, " "), strconv.Itoa(tt.want.ID))) + }) + } } From 8c2c3939f9b7baed937028c9da412f132e5d282e Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 24 Feb 2022 15:57:37 +0100 Subject: [PATCH 30/53] Refactor --- scripts/go_gen_p4_const_test.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/scripts/go_gen_p4_const_test.go b/scripts/go_gen_p4_const_test.go index f5b8b9145..33ba0edc0 100644 --- a/scripts/go_gen_p4_const_test.go +++ b/scripts/go_gen_p4_const_test.go @@ -59,6 +59,7 @@ func Test_generate(t *testing.T) { wantErr: true, }, } + for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := generateConstants(tt.args.p4config) @@ -68,6 +69,11 @@ func Test_generate(t *testing.T) { return } + if idx == -1 && !tt.wantErr { + // Avoid panics + t.Fail() + } + line := strings.SplitN(result[idx:], "\n", 1) require.True(t, strings.Contains(strings.Join(line, " "), strconv.Itoa(tt.want.ID))) }) From 0dbebe1e63dd510f179ca602c67af4b8dfc8edc9 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 24 Feb 2022 16:09:23 +0100 Subject: [PATCH 31/53] Add generate Tables test --- scripts/go_gen_p4_const_test.go | 73 ++++++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/scripts/go_gen_p4_const_test.go b/scripts/go_gen_p4_const_test.go index 33ba0edc0..3dc362ed1 100644 --- a/scripts/go_gen_p4_const_test.go +++ b/scripts/go_gen_p4_const_test.go @@ -11,7 +11,7 @@ import ( const dummyP4info = "dummy_p4info.txt" -func Test_generate(t *testing.T) { +func Test_generateConstants(t *testing.T) { type args struct { p4config *p4ConfigV1.P4Info } @@ -58,6 +58,17 @@ func Test_generate(t *testing.T) { }, wantErr: true, }, + { + name: "verify meter size", + args: &args{ + p4config: getP4Config(dummyP4info), + }, + want: &want{ + ID: 1024, + name: "MeterSizePreQosPipeAppMeter", + }, + wantErr: false, + }, } for _, tt := range tests { @@ -69,12 +80,70 @@ func Test_generate(t *testing.T) { return } + if idx != -1 && tt.wantErr { + t.Fail() + } + + line := strings.SplitN(result[idx:], "\n", 1) + require.True(t, strings.Contains(strings.Join(line, " "), strconv.Itoa(tt.want.ID))) + }) + } +} + +func Test_generateTables(t *testing.T) { + type args struct { + p4config *p4ConfigV1.P4Info + } + + type want struct { + ID int + name string + } + + tests := []struct { + name string + args *args + want *want + wantErr bool + }{ + { + name: "verify table map", + args: &args{ + p4config: getP4Config(dummyP4info), + }, + want: &want{ + ID: 44976597, + name: "PreQosPipe.sessions_uplink", + }, + }, + { + name: "non existing element", + args: &args{ + p4config: getP4Config(dummyP4info), + }, + want: &want{ + ID: 1111, + name: "test", + }, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := generateTables(tt.args.p4config) + + idx := strings.Index(result, tt.want.name) + if idx == -1 && tt.wantErr { + return + } + if idx == -1 && !tt.wantErr { // Avoid panics t.Fail() } - line := strings.SplitN(result[idx:], "\n", 1) + line := strings.SplitN(result[idx:], ",", 1) require.True(t, strings.Contains(strings.Join(line, " "), strconv.Itoa(tt.want.ID))) }) } From 66bd763136a99b383c05a91a13ce68011cae4117 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 24 Feb 2022 16:16:23 +0100 Subject: [PATCH 32/53] Refactor --- scripts/dummy_p4info.txt | 2 +- scripts/go_gen_p4_const_test.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/scripts/dummy_p4info.txt b/scripts/dummy_p4info.txt index f1f6ee44a..0edbc79bd 100644 --- a/scripts/dummy_p4info.txt +++ b/scripts/dummy_p4info.txt @@ -111,7 +111,7 @@ tables { } tables { preamble { - id: 40931612 + id: 12345678 name: "PreQosPipe.my_station" alias: "my_station" } diff --git a/scripts/go_gen_p4_const_test.go b/scripts/go_gen_p4_const_test.go index 3dc362ed1..e53a88455 100644 --- a/scripts/go_gen_p4_const_test.go +++ b/scripts/go_gen_p4_const_test.go @@ -33,7 +33,7 @@ func Test_generateConstants(t *testing.T) { p4config: getP4Config(dummyP4info), }, want: &want{ - ID: 40931612, + ID: 12345678, name: "TablePreQosPipeMyStation", }, }, From 598219aa262b5d65325a460e0c4cf2a8b58a6dfd Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 24 Feb 2022 16:20:12 +0100 Subject: [PATCH 33/53] Add dummy action --- scripts/dummy_p4info.txt | 7 +++++++ scripts/go_gen_p4_const_test.go | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/scripts/dummy_p4info.txt b/scripts/dummy_p4info.txt index 0edbc79bd..25508f088 100644 --- a/scripts/dummy_p4info.txt +++ b/scripts/dummy_p4info.txt @@ -342,6 +342,13 @@ actions { alias: "Routing.drop" } } +actions { + preamble { + id: 76544321 + name: "my.dummy.action" + alias: "dummy.action" + } +} actions { preamble { id: 23965128 diff --git a/scripts/go_gen_p4_const_test.go b/scripts/go_gen_p4_const_test.go index e53a88455..e5e17a244 100644 --- a/scripts/go_gen_p4_const_test.go +++ b/scripts/go_gen_p4_const_test.go @@ -69,6 +69,17 @@ func Test_generateConstants(t *testing.T) { }, wantErr: false, }, + { + name: "verify dummy action", + args: &args{ + p4config: getP4Config(dummyP4info), + }, + want: &want{ + ID: 76544321, + name: "MyDummyAction", + }, + wantErr: false, + }, } for _, tt := range tests { From e31c2f74c5c7a00eaad97cc7ec2bb0f32efd301d Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 24 Feb 2022 16:52:52 +0100 Subject: [PATCH 34/53] Refactor. One big table driven test --- scripts/go_gen_p4_const_test.go | 108 +++++++++++++++++++------------- 1 file changed, 65 insertions(+), 43 deletions(-) diff --git a/scripts/go_gen_p4_const_test.go b/scripts/go_gen_p4_const_test.go index e5e17a244..f6df2e07a 100644 --- a/scripts/go_gen_p4_const_test.go +++ b/scripts/go_gen_p4_const_test.go @@ -11,9 +11,19 @@ import ( const dummyP4info = "dummy_p4info.txt" +type generatorType int + +const ( + constant generatorType = iota + table + action + meter +) + func Test_generateConstants(t *testing.T) { type args struct { p4config *p4ConfigV1.P4Info + genType generatorType } type want struct { @@ -31,6 +41,7 @@ func Test_generateConstants(t *testing.T) { name: "verify table const", args: &args{ p4config: getP4Config(dummyP4info), + genType: constant, }, want: &want{ ID: 12345678, @@ -41,6 +52,7 @@ func Test_generateConstants(t *testing.T) { name: "verify action const", args: &args{ p4config: getP4Config(dummyP4info), + genType: constant, }, want: &want{ ID: 23766285, @@ -51,6 +63,7 @@ func Test_generateConstants(t *testing.T) { name: "non existing const", args: &args{ p4config: getP4Config(dummyP4info), + genType: constant, }, want: &want{ ID: 111111, @@ -62,65 +75,29 @@ func Test_generateConstants(t *testing.T) { name: "verify meter size", args: &args{ p4config: getP4Config(dummyP4info), + genType: constant, }, want: &want{ ID: 1024, name: "MeterSizePreQosPipeAppMeter", }, - wantErr: false, }, { name: "verify dummy action", args: &args{ p4config: getP4Config(dummyP4info), + genType: constant, }, want: &want{ ID: 76544321, name: "MyDummyAction", }, - wantErr: false, }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - result := generateConstants(tt.args.p4config) - - idx := strings.Index(result, tt.want.name) - if idx == -1 && tt.wantErr { - return - } - - if idx != -1 && tt.wantErr { - t.Fail() - } - - line := strings.SplitN(result[idx:], "\n", 1) - require.True(t, strings.Contains(strings.Join(line, " "), strconv.Itoa(tt.want.ID))) - }) - } -} - -func Test_generateTables(t *testing.T) { - type args struct { - p4config *p4ConfigV1.P4Info - } - - type want struct { - ID int - name string - } - - tests := []struct { - name string - args *args - want *want - wantErr bool - }{ { name: "verify table map", args: &args{ p4config: getP4Config(dummyP4info), + genType: table, }, want: &want{ ID: 44976597, @@ -131,6 +108,7 @@ func Test_generateTables(t *testing.T) { name: "non existing element", args: &args{ p4config: getP4Config(dummyP4info), + genType: table, }, want: &want{ ID: 1111, @@ -138,23 +116,67 @@ func Test_generateTables(t *testing.T) { }, wantErr: true, }, + { + name: "verify meter map", + args: &args{ + p4config: getP4Config(dummyP4info), + genType: meter, + }, + want: &want{ + ID: 338231090, + name: "PreQosPipe.app_meter", + }, + }, + { + name: "verify action map", + args: &args{ + p4config: getP4Config(dummyP4info), + genType: action, + }, + want: &want{ + ID: 30494847, + name: "PreQosPipe.Acl.set_port", + }, + }, + { + name: "non existing action", + args: &args{ + p4config: getP4Config(dummyP4info), + genType: action, + }, + want: &want{ + ID: 1, + name: "test", + }, + wantErr: true, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - result := generateTables(tt.args.p4config) + result := "" + + switch tt.args.genType { + case constant: + result = generateConstants(tt.args.p4config) + case table: + result = generateTables(tt.args.p4config) + case action: + result = generateActions(tt.args.p4config) + case meter: + result = generateMeters(tt.args.p4config) + } idx := strings.Index(result, tt.want.name) if idx == -1 && tt.wantErr { return } - if idx == -1 && !tt.wantErr { - // Avoid panics + if idx != -1 && tt.wantErr { t.Fail() } - line := strings.SplitN(result[idx:], ",", 1) + line := strings.SplitN(result[idx:], "\n", 1) require.True(t, strings.Contains(strings.Join(line, " "), strconv.Itoa(tt.want.ID))) }) } From 16c4da6e1c1175dab444fa7d07cf0cd0ef938f7c Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Thu, 24 Feb 2022 17:01:53 +0100 Subject: [PATCH 35/53] Refactor --- scripts/go_gen_p4_const_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/go_gen_p4_const_test.go b/scripts/go_gen_p4_const_test.go index f6df2e07a..d2fbbd604 100644 --- a/scripts/go_gen_p4_const_test.go +++ b/scripts/go_gen_p4_const_test.go @@ -20,7 +20,7 @@ const ( meter ) -func Test_generateConstants(t *testing.T) { +func Test_generator(t *testing.T) { type args struct { p4config *p4ConfigV1.P4Info genType generatorType From d25c1aab476140cccc5ced465aa12e791ef10ebc Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Fri, 25 Feb 2022 15:02:48 +0100 Subject: [PATCH 36/53] Add direct and indirect counters --- scripts/dummy_p4info.txt | 11 +++++++++ scripts/go_gen_p4_const_test.go | 40 +++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/scripts/dummy_p4info.txt b/scripts/dummy_p4info.txt index 25508f088..df251a104 100644 --- a/scripts/dummy_p4info.txt +++ b/scripts/dummy_p4info.txt @@ -708,6 +708,17 @@ direct_counters { } direct_table_id: 47204971 } +direct_counters { + preamble { + id: 12345 + name: "MyDummyCounter" + alias: "mydummycounter" + } + spec { + unit: BOTH + } + direct_table_id: 1234 +} meters { preamble { id: 338231090 diff --git a/scripts/go_gen_p4_const_test.go b/scripts/go_gen_p4_const_test.go index d2fbbd604..30592d338 100644 --- a/scripts/go_gen_p4_const_test.go +++ b/scripts/go_gen_p4_const_test.go @@ -17,6 +17,8 @@ const ( constant generatorType = iota table action + indirectCounter + directCounter meter ) @@ -150,6 +152,40 @@ func Test_generator(t *testing.T) { }, wantErr: true, }, + { + name: "verify indirect counter map", + args: &args{ + p4config: getP4Config(dummyP4info), + genType: indirectCounter, + }, + want: &want{ + ID: 315693181, + name: "PreQosPipe.pre_qos_counter", + }, + }, + { + name: "non existing indirect counter", + args: &args{ + p4config: getP4Config(dummyP4info), + genType: indirectCounter, + }, + want: &want{ + ID: 111, + name: "test", + }, + wantErr: true, + }, + { + name: "verify dummy direct counter", + args: &args{ + p4config: getP4Config(dummyP4info), + genType: directCounter, + }, + want: &want{ + ID: 12345, + name: "MyDummyCounter", + }, + }, } for _, tt := range tests { @@ -165,6 +201,10 @@ func Test_generator(t *testing.T) { result = generateActions(tt.args.p4config) case meter: result = generateMeters(tt.args.p4config) + case indirectCounter: + result = generateIndirectCounters(tt.args.p4config) + case directCounter: + result = generateDirectCounters(tt.args.p4config) } idx := strings.Index(result, tt.want.name) From dc262e45376bb08f73ddd26548a6afe968060884 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Fri, 25 Feb 2022 19:22:29 +0100 Subject: [PATCH 37/53] Add scripts folder when running tests and sync with latest changes --- Makefile | 2 +- internal/p4constants/p4constants.go | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index a066da7fa..486fda667 100644 --- a/Makefile +++ b/Makefile @@ -97,7 +97,7 @@ test: .coverage -coverprofile=.coverage/coverage-unit.txt \ -covermode=atomic \ -v \ - ./pfcpiface + ./pfcpiface ./scripts p4-constants: $(info *** Generating go constants...) diff --git a/internal/p4constants/p4constants.go b/internal/p4constants/p4constants.go index 455d27c8b..a611d5143 100644 --- a/internal/p4constants/p4constants.go +++ b/internal/p4constants/p4constants.go @@ -25,9 +25,10 @@ const ( HdrPreQosPipeTerminationsUplinkAppId uint32 = 2 HdrPreQosPipeTerminationsDownlinkUeAddress uint32 = 1 HdrPreQosPipeTerminationsDownlinkAppId uint32 = 2 - HdrPreQosPipeApplicationsAppIpAddr uint32 = 1 - HdrPreQosPipeApplicationsAppL4Port uint32 = 2 - HdrPreQosPipeApplicationsAppIpProto uint32 = 3 + HdrPreQosPipeApplicationsSliceId uint32 = 1 + HdrPreQosPipeApplicationsAppIpAddr uint32 = 2 + HdrPreQosPipeApplicationsAppL4Port uint32 = 3 + HdrPreQosPipeApplicationsAppIpProto uint32 = 4 HdrPreQosPipeTunnelPeersTunnelPeerId uint32 = 1 // Tables TablePreQosPipeRoutingRoutesV4 uint32 = 39015874 From f13534933cf0b83098cbef360b0cebc1c72cb3de Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Fri, 25 Feb 2022 19:32:52 +0100 Subject: [PATCH 38/53] Restore tableID slice when clearing all tables --- pfcpiface/up4.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index c9649f201..a8cc2fbf7 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -370,7 +370,16 @@ func (up4 *UP4) setUpfInfo(u *upf, conf *Conf) { } func (up4 *UP4) clearAllTables() error { - if err := up4.p4client.ClearTables(p4constants.GetTableIDList()); err != nil { + tableIDs := []uint32{ + p4constants.TablePreQosPipeSessionsUplink, + p4constants.TablePreQosPipeSessionsDownlink, + p4constants.TablePreQosPipeTerminationsUplink, + p4constants.TablePreQosPipeTerminationsDownlink, + p4constants.TablePreQosPipeTunnelPeers, + p4constants.TablePreQosPipeInterfaces, + } + + if err := up4.p4client.ClearTables(tableIDs); err != nil { return err } From 542d3211ea2cb3c969c2718aeffbf7b90b9382cc Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Fri, 25 Feb 2022 21:29:49 +0100 Subject: [PATCH 39/53] Refactor clearAllTables in clearTables --- pfcpiface/up4.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index a8cc2fbf7..2b00ea528 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -369,7 +369,7 @@ func (up4 *UP4) setUpfInfo(u *upf, conf *Conf) { } } -func (up4 *UP4) clearAllTables() error { +func (up4 *UP4) clearTables() error { tableIDs := []uint32{ p4constants.TablePreQosPipeSessionsUplink, p4constants.TablePreQosPipeSessionsDownlink, @@ -441,7 +441,7 @@ func (up4 *UP4) tryConnect() error { up4.p4RtTranslator = newP4RtTranslator(up4.p4client.P4Info) - err = up4.clearAllTables() + err = up4.clearTables() if err != nil { log.Warningf("failed to clear tables: %v", err) } From 393ba4adae70244ff7dc9973c8c48d252772a0fa Mon Sep 17 00:00:00 2001 From: pudelkoM Date: Fri, 25 Feb 2022 12:37:41 -0800 Subject: [PATCH 40/53] Cleanup - move code to `cmd` - inline test p4info - consolidate duplicate code --- Makefile | 4 +- cmd/p4info_code_gen/p4info_code_gen.go | 278 ++++++ .../p4info_code_gen/p4info_code_gen_test.go | 157 +++- internal/p4constants/p4constants.go | 56 +- scripts/dummy_p4info.txt | 847 ------------------ scripts/go_gen_p4_const.go | 335 ------- 6 files changed, 424 insertions(+), 1253 deletions(-) create mode 100644 cmd/p4info_code_gen/p4info_code_gen.go rename scripts/go_gen_p4_const_test.go => cmd/p4info_code_gen/p4info_code_gen_test.go (50%) delete mode 100644 scripts/dummy_p4info.txt delete mode 100644 scripts/go_gen_p4_const.go diff --git a/Makefile b/Makefile index 486fda667..77ec3d5ea 100644 --- a/Makefile +++ b/Makefile @@ -97,12 +97,12 @@ test: .coverage -coverprofile=.coverage/coverage-unit.txt \ -covermode=atomic \ -v \ - ./pfcpiface ./scripts + ./pfcpiface ./cmd/... p4-constants: $(info *** Generating go constants...) @docker run --rm -v $(CURDIR):/app -w /app \ - golang:latest go run ./scripts/go_gen_p4_const.go \ + golang:latest go run ./cmd/p4info_code_gen/p4info_code_gen.go \ -output internal/p4constants/p4constants.go -p4info conf/p4/bin/p4info.txt @docker run --rm -v $(CURDIR):/app -w /app \ golang:latest gofmt -w internal/p4constants/p4constants.go diff --git a/cmd/p4info_code_gen/p4info_code_gen.go b/cmd/p4info_code_gen/p4info_code_gen.go new file mode 100644 index 000000000..02df3d2e3 --- /dev/null +++ b/cmd/p4info_code_gen/p4info_code_gen.go @@ -0,0 +1,278 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2022-present Open Networking Foundation + +package main + +import ( + "flag" + "fmt" + "io/ioutil" + "os" + "strings" + + "github.com/ettle/strcase" + "github.com/golang/protobuf/proto" + p4ConfigV1 "github.com/p4lang/p4runtime/go/p4/config/v1" +) + +const ( + p4infoPath = "conf/p4/bin/p4info.txt" + + defaultPackageName = "p4constants" + // copyrightHeader uses raw strings to avoid issues with reuse + copyrightHeader = `// SPDX-License-Identifier: Apache-2.0 +// Copyright 2022-present Open Networking Foundation +` + + constOpen = "const (\n" + mapFormatString = "%v:\"%v\",\n" + listFormatString = "%v,\n" + constOrVarClose = ")\n" + + idTypeString = "uint32" + sizeTypeString = "uint64" + + hfVarPrefix = "Hdr_" + tblVarPrefix = "Table_" + ctrVarPrefix = "Counter_" + ctrSizeVarPrefix = "CounterSize_" + dirCtrVarPrefix = "DirectCounter_" + actVarPrefix = "Action_" + actparamVarPrefix = "ActionParam_" + actprofVarPrefix = "ActionProfile_" + packetmetaVarPrefix = "PacketMeta_" + mtrVarPrefix = "Meter_" + mtrSizeVarPrefix = "MeterSize_" +) + +func emitEntityConstant(prefix string, p4EntityName string, id uint32) string { + // see: https://go.dev/ref/spec#Identifiers + p4EntityName = prefix + "_" + p4EntityName + p4EntityName = strings.Replace(p4EntityName, ".", "_", -1) + p4EntityName = strcase.ToPascal(p4EntityName) + return fmt.Sprintf("%s \t %s = %v\n", p4EntityName, idTypeString, id) +} + +// TODO: collapse with emitEntityConstant +func emitEntitySizeConstant(prefix string, p4EntityName string, id int64) string { + // see: https://go.dev/ref/spec#Identifiers + p4EntityName = prefix + "_" + p4EntityName + p4EntityName = strings.Replace(p4EntityName, ".", "_", -1) + p4EntityName = strcase.ToPascal(p4EntityName) + return fmt.Sprintf("%s \t %s = %v\n", p4EntityName, sizeTypeString, id) +} + +func getPreambles(info *p4ConfigV1.P4Info, p4Type string) (preambles []*p4ConfigV1.Preamble) { + switch p4Type { + case "Table": + for _, e := range info.GetTables() { + preambles = append(preambles, e.GetPreamble()) + } + case "Action": + for _, e := range info.GetActions() { + preambles = append(preambles, e.GetPreamble()) + } + case "ActionProfile": + for _, e := range info.GetActionProfiles() { + preambles = append(preambles, e.GetPreamble()) + } + case "Counter": + for _, e := range info.GetCounters() { + preambles = append(preambles, e.GetPreamble()) + } + case "DirectCounter": + for _, e := range info.GetDirectCounters() { + preambles = append(preambles, e.GetPreamble()) + } + case "Meter": + for _, e := range info.GetMeters() { + preambles = append(preambles, e.GetPreamble()) + } + case "DirectMeter": + for _, e := range info.GetDirectMeters() { + preambles = append(preambles, e.GetPreamble()) + } + case "ControllerPacketMetadata": + for _, e := range info.GetControllerPacketMetadata() { + preambles = append(preambles, e.GetPreamble()) + } + case "ValueSet": + for _, e := range info.GetValueSets() { + preambles = append(preambles, e.GetPreamble()) + } + case "Register": + for _, e := range info.GetRegisters() { + preambles = append(preambles, e.GetPreamble()) + } + case "Digest": + for _, e := range info.GetDigests() { + preambles = append(preambles, e.GetPreamble()) + } + default: + panic("unknown p4 type " + p4Type) + } + + return +} + +func generateP4DataFunctions(info *p4ConfigV1.P4Info, p4Type string) string { + const mapFuncTemplate = "func Get%sIDToNameMap() map[%s]string {\n return map[%s]string {\n" + const listFuncTemplate = "func Get%sIDList() []%s {\n return []%s {\n" + + mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} + mapBuilder.WriteString(fmt.Sprintf(mapFuncTemplate, p4Type, idTypeString, idTypeString)) + listBuilder.WriteString(fmt.Sprintf(listFuncTemplate, p4Type, idTypeString, idTypeString)) + + preambles := getPreambles(info, p4Type) + + for _, element := range preambles { + name, ID := element.GetName(), element.GetId() + + mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) + listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) + } + mapBuilder.WriteString("}\n}\n\n") + listBuilder.WriteString("}\n}\n\n") //Close declarations + + return mapBuilder.String() + listBuilder.String() +} + +func generateConstants(p4info *p4ConfigV1.P4Info) string { + constBuilder := strings.Builder{} + + constBuilder.WriteString(constOpen) + + //HeaderField IDs + constBuilder.WriteString("// HeaderFields\n") + for _, element := range p4info.GetTables() { + for _, matchField := range element.MatchFields { + tableName, name := element.GetPreamble().GetName(), matchField.GetName() + + constBuilder.WriteString(emitEntityConstant(hfVarPrefix+tableName, name, matchField.GetId())) + } + } + + // Tables + constBuilder.WriteString("// Tables\n") + for _, element := range p4info.GetTables() { + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + constBuilder.WriteString(emitEntityConstant(tblVarPrefix, name, ID)) + } + + // Actions + constBuilder.WriteString("// Actions\n") + for _, element := range p4info.GetActions() { + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + constBuilder.WriteString(emitEntityConstant(actVarPrefix, name, ID)) + } + + // Action Param IDs + constBuilder.WriteString("// ActionParams\n") + for _, element := range p4info.GetActions() { + for _, actionParam := range element.GetParams() { + actionName, name := element.GetPreamble().GetName(), actionParam.GetName() + + constBuilder.WriteString(emitEntityConstant(actparamVarPrefix+actionName, name, actionParam.GetId())) + } + } + + // Indirect Counters + constBuilder.WriteString("// IndirectCounters\n") + for _, element := range p4info.GetCounters() { + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + constBuilder.WriteString(emitEntityConstant(ctrVarPrefix, name, ID)) + constBuilder.WriteString(emitEntitySizeConstant(ctrSizeVarPrefix, name, element.GetSize())) + } + + // Direct Counters + constBuilder.WriteString("// DirectCounters\n") + for _, element := range p4info.GetDirectCounters() { + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + constBuilder.WriteString(emitEntityConstant(dirCtrVarPrefix, name, ID)) + } + + // Action profiles + constBuilder.WriteString("// ActionProfiles\n") + for _, element := range p4info.GetActionProfiles() { + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + constBuilder.WriteString(emitEntityConstant(actprofVarPrefix, name, ID)) + } + + // Packet metadata + constBuilder.WriteString("// PacketMetadata\n") + for _, element := range p4info.GetControllerPacketMetadata() { + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + constBuilder.WriteString(emitEntityConstant(packetmetaVarPrefix, name, ID)) + } + + // Meters + constBuilder.WriteString("// Meters\n") + for _, element := range p4info.GetMeters() { + name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() + + constBuilder.WriteString(emitEntityConstant(mtrVarPrefix, name, ID)) + constBuilder.WriteString(emitEntitySizeConstant(mtrSizeVarPrefix, name, element.GetSize())) + } + + constBuilder.WriteString(constOrVarClose + "\n") + + return constBuilder.String() +} + +func mustGetP4Config(p4infopath string) *p4ConfigV1.P4Info { + p4infoBytes, err := ioutil.ReadFile(p4infopath) + if err != nil { + panic(fmt.Sprintf("Could not read P4Info file: %v", err)) + } + + var p4info p4ConfigV1.P4Info + + err = proto.UnmarshalText(string(p4infoBytes), &p4info) + if err != nil { + panic("Could not parse P4Info file") + } + + return &p4info +} + +func main() { + p4infoPath := flag.String("p4info", p4infoPath, "Path of the p4info file") + outputPath := flag.String("output", "-", "Default will print to Stdout") + packageName := flag.String("package", defaultPackageName, "Set the package name") + + flag.Parse() + + p4info := mustGetP4Config(*p4infoPath) + + sb := strings.Builder{} + + sb.WriteString(copyrightHeader + "\n") + sb.WriteString(fmt.Sprintf("package %s\n", *packageName)) + + sb.WriteString(generateConstants(p4info)) + sb.WriteString(generateP4DataFunctions(p4info, "Table")) + sb.WriteString(generateP4DataFunctions(p4info, "Action")) + sb.WriteString(generateP4DataFunctions(p4info, "ActionProfile")) + sb.WriteString(generateP4DataFunctions(p4info, "Counter")) + sb.WriteString(generateP4DataFunctions(p4info, "DirectCounter")) + sb.WriteString(generateP4DataFunctions(p4info, "Meter")) + sb.WriteString(generateP4DataFunctions(p4info, "DirectMeter")) + sb.WriteString(generateP4DataFunctions(p4info, "ControllerPacketMetadata")) + sb.WriteString(generateP4DataFunctions(p4info, "Register")) + + result := sb.String() + + if *outputPath == "-" { + fmt.Println(result) + } else { + if err := os.WriteFile(*outputPath, []byte(result), 0644); err != nil { + panic(fmt.Sprintf("Error while creating File: %v", err)) + } + } +} diff --git a/scripts/go_gen_p4_const_test.go b/cmd/p4info_code_gen/p4info_code_gen_test.go similarity index 50% rename from scripts/go_gen_p4_const_test.go rename to cmd/p4info_code_gen/p4info_code_gen_test.go index 30592d338..94c7af924 100644 --- a/scripts/go_gen_p4_const_test.go +++ b/cmd/p4info_code_gen/p4info_code_gen_test.go @@ -1,6 +1,8 @@ package main import ( + "io/fs" + "io/ioutil" "strconv" "strings" "testing" @@ -9,8 +11,6 @@ import ( "github.com/stretchr/testify/require" ) -const dummyP4info = "dummy_p4info.txt" - type generatorType int const ( @@ -22,7 +22,84 @@ const ( meter ) +const testP4InfoString = ` +pkg_info { + arch: "v1model" +} +tables { + preamble { + id: 12345678 + name: "PreQosPipe.my_station" + alias: "my_station" + } + match_fields { + id: 1 + name: "dst_mac" + bitwidth: 48 + match_type: EXACT + } + action_refs { + id: 21257015 + } + size: 1024 +} +actions { + preamble { + id: 26090030 + name: "PreQosPipe.set_source_iface" + alias: "set_source_iface" + } + params { + id: 1 + name: "src_iface" + bitwidth: 8 + } + params { + id: 2 + name: "direction" + bitwidth: 8 + } + params { + id: 3 + name: "slice_id" + bitwidth: 4 + } +} +meters { + preamble { + id: 338231090 + name: "PreQosPipe.app_meter" + alias: "app_meter" + } + spec { + unit: BYTES + } + size: 1024 +} +counters { + preamble { + id: 315693181 + name: "PreQosPipe.pre_qos_counter" + alias: "pre_qos_counter" + } + spec { + unit: BOTH + } + size: 1024 +} +` + +func mustWriteStringToDisk(s string, path string) { + err := ioutil.WriteFile(path, []byte(s), fs.ModePerm) + if err != nil { + panic(err) + } +} + func Test_generator(t *testing.T) { + p4infoPath := t.TempDir() + "/dummy_p4info.pb.txt" + mustWriteStringToDisk(testP4InfoString, p4infoPath) + type args struct { p4config *p4ConfigV1.P4Info genType generatorType @@ -42,7 +119,7 @@ func Test_generator(t *testing.T) { { name: "verify table const", args: &args{ - p4config: getP4Config(dummyP4info), + p4config: mustGetP4Config(p4infoPath), genType: constant, }, want: &want{ @@ -53,18 +130,18 @@ func Test_generator(t *testing.T) { { name: "verify action const", args: &args{ - p4config: getP4Config(dummyP4info), + p4config: mustGetP4Config(p4infoPath), genType: constant, }, want: &want{ - ID: 23766285, - name: "ActionPreQosPipeInitializeMetadata", + ID: 26090030, + name: "ActionPreQosPipeSetSourceIface", }, }, { name: "non existing const", args: &args{ - p4config: getP4Config(dummyP4info), + p4config: mustGetP4Config(p4infoPath), genType: constant, }, want: &want{ @@ -76,7 +153,7 @@ func Test_generator(t *testing.T) { { name: "verify meter size", args: &args{ - p4config: getP4Config(dummyP4info), + p4config: mustGetP4Config(p4infoPath), genType: constant, }, want: &want{ @@ -84,32 +161,21 @@ func Test_generator(t *testing.T) { name: "MeterSizePreQosPipeAppMeter", }, }, - { - name: "verify dummy action", - args: &args{ - p4config: getP4Config(dummyP4info), - genType: constant, - }, - want: &want{ - ID: 76544321, - name: "MyDummyAction", - }, - }, { name: "verify table map", args: &args{ - p4config: getP4Config(dummyP4info), + p4config: mustGetP4Config(p4infoPath), genType: table, }, want: &want{ - ID: 44976597, - name: "PreQosPipe.sessions_uplink", + ID: 12345678, + name: "PreQosPipe.my_station", }, }, { name: "non existing element", args: &args{ - p4config: getP4Config(dummyP4info), + p4config: mustGetP4Config(p4infoPath), genType: table, }, want: &want{ @@ -121,7 +187,7 @@ func Test_generator(t *testing.T) { { name: "verify meter map", args: &args{ - p4config: getP4Config(dummyP4info), + p4config: mustGetP4Config(p4infoPath), genType: meter, }, want: &want{ @@ -132,18 +198,18 @@ func Test_generator(t *testing.T) { { name: "verify action map", args: &args{ - p4config: getP4Config(dummyP4info), + p4config: mustGetP4Config(p4infoPath), genType: action, }, want: &want{ - ID: 30494847, - name: "PreQosPipe.Acl.set_port", + ID: 26090030, + name: "PreQosPipe.set_source_iface", }, }, { name: "non existing action", args: &args{ - p4config: getP4Config(dummyP4info), + p4config: mustGetP4Config(p4infoPath), genType: action, }, want: &want{ @@ -155,7 +221,7 @@ func Test_generator(t *testing.T) { { name: "verify indirect counter map", args: &args{ - p4config: getP4Config(dummyP4info), + p4config: mustGetP4Config(p4infoPath), genType: indirectCounter, }, want: &want{ @@ -166,7 +232,7 @@ func Test_generator(t *testing.T) { { name: "non existing indirect counter", args: &args{ - p4config: getP4Config(dummyP4info), + p4config: mustGetP4Config(p4infoPath), genType: indirectCounter, }, want: &want{ @@ -175,17 +241,6 @@ func Test_generator(t *testing.T) { }, wantErr: true, }, - { - name: "verify dummy direct counter", - args: &args{ - p4config: getP4Config(dummyP4info), - genType: directCounter, - }, - want: &want{ - ID: 12345, - name: "MyDummyCounter", - }, - }, } for _, tt := range tests { @@ -196,15 +251,15 @@ func Test_generator(t *testing.T) { case constant: result = generateConstants(tt.args.p4config) case table: - result = generateTables(tt.args.p4config) + result = generateP4DataFunctions(tt.args.p4config, "Table") case action: - result = generateActions(tt.args.p4config) + result = generateP4DataFunctions(tt.args.p4config, "Action") case meter: - result = generateMeters(tt.args.p4config) + result = generateP4DataFunctions(tt.args.p4config, "Meter") case indirectCounter: - result = generateIndirectCounters(tt.args.p4config) + result = generateP4DataFunctions(tt.args.p4config, "Counter") case directCounter: - result = generateDirectCounters(tt.args.p4config) + result = generateP4DataFunctions(tt.args.p4config, "DirectCounter") } idx := strings.Index(result, tt.want.name) @@ -213,11 +268,15 @@ func Test_generator(t *testing.T) { } if idx != -1 && tt.wantErr { - t.Fail() + t.Fatalf("Found unexpected entity name %s in generated code %s", tt.want.name, result) + } + + if idx == -1 { + t.Fatalf("Did not find expected entity name '%s' in generated code: %s", tt.want.name, result) } - line := strings.SplitN(result[idx:], "\n", 1) - require.True(t, strings.Contains(strings.Join(line, " "), strconv.Itoa(tt.want.ID))) + line := strings.Join(strings.SplitN(result[idx:], "\n", 1), " ") + require.Contains(t, line, strconv.Itoa(tt.want.ID), "ID not found") }) } } diff --git a/internal/p4constants/p4constants.go b/internal/p4constants/p4constants.go index a611d5143..1fb669156 100644 --- a/internal/p4constants/p4constants.go +++ b/internal/p4constants/p4constants.go @@ -208,15 +208,15 @@ func GetActionIDList() []uint32 { } } -func GetDirectCounterIDToNameMap() map[uint32]string { +func GetActionProfileIDToNameMap() map[uint32]string { return map[uint32]string{ - 325583051: "acls", + 297808402: "hashed_selector", } } -func GetDirectCounterIDList() []uint32 { +func GetActionProfileIDList() []uint32 { return []uint32{ - 325583051, + 297808402, } } @@ -234,42 +234,58 @@ func GetCounterIDList() []uint32 { } } -func GetActionProfileIDToNameMap() map[uint32]string { +func GetDirectCounterIDToNameMap() map[uint32]string { return map[uint32]string{ - 297808402: "hashed_selector", + 325583051: "acls", } } -func GetActionProfileIDList() []uint32 { +func GetDirectCounterIDList() []uint32 { return []uint32{ - 297808402, + 325583051, } } -func GetPacketMetadataIDToNameMap() map[uint32]string { +func GetMeterIDToNameMap() map[uint32]string { return map[uint32]string{ - 75327753: "packet_out", - 80671331: "packet_in", + 338231090: "PreQosPipe.app_meter", + 347593234: "PreQosPipe.session_meter", } } -func GetPacketMetadataIDList() []uint32 { +func GetMeterIDList() []uint32 { return []uint32{ - 75327753, - 80671331, + 338231090, + 347593234, } } -func GetMeterIDToNameMap() map[uint32]string { +func GetDirectMeterIDToNameMap() map[uint32]string { + return map[uint32]string{} +} + +func GetDirectMeterIDList() []uint32 { + return []uint32{} +} + +func GetControllerPacketMetadataIDToNameMap() map[uint32]string { return map[uint32]string{ - 338231090: "PreQosPipe.app_meter", - 347593234: "PreQosPipe.session_meter", + 75327753: "packet_out", + 80671331: "packet_in", } } -func GetMeterIDList() []uint32 { +func GetControllerPacketMetadataIDList() []uint32 { return []uint32{ - 338231090, - 347593234, + 75327753, + 80671331, } } + +func GetRegisterIDToNameMap() map[uint32]string { + return map[uint32]string{} +} + +func GetRegisterIDList() []uint32 { + return []uint32{} +} diff --git a/scripts/dummy_p4info.txt b/scripts/dummy_p4info.txt deleted file mode 100644 index df251a104..000000000 --- a/scripts/dummy_p4info.txt +++ /dev/null @@ -1,847 +0,0 @@ -pkg_info { - arch: "v1model" -} -tables { - preamble { - id: 39015874 - name: "PreQosPipe.Routing.routes_v4" - alias: "routes_v4" - } - match_fields { - id: 1 - name: "dst_prefix" - bitwidth: 32 - match_type: LPM - } - action_refs { - id: 23965128 - } - action_refs { - id: 21257015 - annotations: "@defaultonly" - scope: DEFAULT_ONLY - } - implementation_id: 297808402 - size: 1024 -} -tables { - preamble { - id: 47204971 - name: "PreQosPipe.Acl.acls" - alias: "Acl.acls" - } - match_fields { - id: 1 - name: "inport" - bitwidth: 9 - match_type: TERNARY - } - match_fields { - id: 2 - name: "src_iface" - bitwidth: 8 - match_type: TERNARY - } - match_fields { - id: 3 - name: "eth_src" - bitwidth: 48 - match_type: TERNARY - } - match_fields { - id: 4 - name: "eth_dst" - bitwidth: 48 - match_type: TERNARY - } - match_fields { - id: 5 - name: "eth_type" - bitwidth: 16 - match_type: TERNARY - } - match_fields { - id: 6 - name: "ipv4_src" - bitwidth: 32 - match_type: TERNARY - } - match_fields { - id: 7 - name: "ipv4_dst" - bitwidth: 32 - match_type: TERNARY - } - match_fields { - id: 8 - name: "ipv4_proto" - bitwidth: 8 - match_type: TERNARY - } - match_fields { - id: 9 - name: "l4_sport" - bitwidth: 16 - match_type: TERNARY - } - match_fields { - id: 10 - name: "l4_dport" - bitwidth: 16 - match_type: TERNARY - } - action_refs { - id: 30494847 - } - action_refs { - id: 26495283 - } - action_refs { - id: 21596798 - } - action_refs { - id: 18812293 - } - action_refs { - id: 21257015 - } - const_default_action_id: 21257015 - direct_resource_ids: 325583051 - size: 1024 -} -tables { - preamble { - id: 12345678 - name: "PreQosPipe.my_station" - alias: "my_station" - } - match_fields { - id: 1 - name: "dst_mac" - bitwidth: 48 - match_type: EXACT - } - action_refs { - id: 21257015 - } - size: 1024 -} -tables { - preamble { - id: 33923840 - name: "PreQosPipe.interfaces" - alias: "interfaces" - } - match_fields { - id: 1 - name: "ipv4_dst_prefix" - bitwidth: 32 - match_type: LPM - } - action_refs { - id: 26090030 - } - const_default_action_id: 26090030 - size: 1024 -} -tables { - preamble { - id: 44976597 - name: "PreQosPipe.sessions_uplink" - alias: "sessions_uplink" - } - match_fields { - id: 1 - name: "n3_address" - bitwidth: 32 - match_type: EXACT - } - match_fields { - id: 2 - name: "teid" - bitwidth: 32 - match_type: EXACT - } - action_refs { - id: 19461580 - } - action_refs { - id: 22196934 - } - action_refs { - id: 28401267 - annotations: "@defaultonly" - scope: DEFAULT_ONLY - } - const_default_action_id: 28401267 - size: 1024 -} -tables { - preamble { - id: 34742049 - name: "PreQosPipe.sessions_downlink" - alias: "sessions_downlink" - } - match_fields { - id: 1 - name: "ue_address" - bitwidth: 32 - match_type: EXACT - } - action_refs { - id: 21848329 - } - action_refs { - id: 20229579 - } - action_refs { - id: 20249483 - } - action_refs { - id: 28401267 - annotations: "@defaultonly" - scope: DEFAULT_ONLY - } - const_default_action_id: 28401267 - size: 1024 -} -tables { - preamble { - id: 37595532 - name: "PreQosPipe.terminations_uplink" - alias: "terminations_uplink" - } - match_fields { - id: 1 - name: "ue_address" - bitwidth: 32 - match_type: EXACT - } - match_fields { - id: 2 - name: "app_id" - bitwidth: 8 - match_type: EXACT - } - action_refs { - id: 28305359 - } - action_refs { - id: 21760615 - } - action_refs { - id: 20977365 - } - action_refs { - id: 28401267 - annotations: "@defaultonly" - scope: DEFAULT_ONLY - } - const_default_action_id: 28401267 - size: 1024 -} -tables { - preamble { - id: 34778590 - name: "PreQosPipe.terminations_downlink" - alias: "terminations_downlink" - } - match_fields { - id: 1 - name: "ue_address" - bitwidth: 32 - match_type: EXACT - } - match_fields { - id: 2 - name: "app_id" - bitwidth: 8 - match_type: EXACT - } - action_refs { - id: 32699713 - } - action_refs { - id: 31264233 - } - action_refs { - id: 26185804 - } - action_refs { - id: 28401267 - annotations: "@defaultonly" - scope: DEFAULT_ONLY - } - const_default_action_id: 28401267 - size: 1024 -} -tables { - preamble { - id: 46868458 - name: "PreQosPipe.applications" - alias: "applications" - } - match_fields { - id: 1 - name: "app_ip_addr" - bitwidth: 32 - match_type: LPM - } - match_fields { - id: 2 - name: "app_l4_port" - bitwidth: 16 - match_type: RANGE - } - match_fields { - id: 3 - name: "app_ip_proto" - bitwidth: 8 - match_type: TERNARY - } - action_refs { - id: 23010411 - } - const_default_action_id: 23010411 - size: 1024 -} -tables { - preamble { - id: 49497304 - name: "PreQosPipe.tunnel_peers" - alias: "tunnel_peers" - } - match_fields { - id: 1 - name: "tunnel_peer_id" - bitwidth: 8 - match_type: EXACT - } - action_refs { - id: 32742981 - } - action_refs { - id: 21257015 - annotations: "@defaultonly" - scope: DEFAULT_ONLY - } - size: 1024 -} -actions { - preamble { - id: 21257015 - name: "NoAction" - alias: "NoAction" - annotations: "@noWarn(\"unused\")" - } -} -actions { - preamble { - id: 31448256 - name: "PreQosPipe.Routing.drop" - alias: "Routing.drop" - } -} -actions { - preamble { - id: 76544321 - name: "my.dummy.action" - alias: "dummy.action" - } -} -actions { - preamble { - id: 23965128 - name: "PreQosPipe.Routing.route" - alias: "route" - } - params { - id: 1 - name: "src_mac" - bitwidth: 48 - } - params { - id: 2 - name: "dst_mac" - bitwidth: 48 - } - params { - id: 3 - name: "egress_port" - bitwidth: 9 - } -} -actions { - preamble { - id: 30494847 - name: "PreQosPipe.Acl.set_port" - alias: "set_port" - } - params { - id: 1 - name: "port" - bitwidth: 9 - } -} -actions { - preamble { - id: 26495283 - name: "PreQosPipe.Acl.punt" - alias: "punt" - } -} -actions { - preamble { - id: 21596798 - name: "PreQosPipe.Acl.clone_to_cpu" - alias: "clone_to_cpu" - } -} -actions { - preamble { - id: 18812293 - name: "PreQosPipe.Acl.drop" - alias: "Acl.drop" - } -} -actions { - preamble { - id: 23766285 - name: "PreQosPipe._initialize_metadata" - alias: "_initialize_metadata" - } -} -actions { - preamble { - id: 26090030 - name: "PreQosPipe.set_source_iface" - alias: "set_source_iface" - } - params { - id: 1 - name: "src_iface" - bitwidth: 8 - } - params { - id: 2 - name: "direction" - bitwidth: 8 - } - params { - id: 3 - name: "slice_id" - bitwidth: 4 - } -} -actions { - preamble { - id: 28401267 - name: "PreQosPipe.do_drop" - alias: "do_drop" - } -} -actions { - preamble { - id: 19461580 - name: "PreQosPipe.set_session_uplink" - alias: "set_session_uplink" - } - params { - id: 1 - name: "session_meter_idx" - bitwidth: 32 - } -} -actions { - preamble { - id: 22196934 - name: "PreQosPipe.set_session_uplink_drop" - alias: "set_session_uplink_drop" - } -} -actions { - preamble { - id: 21848329 - name: "PreQosPipe.set_session_downlink" - alias: "set_session_downlink" - } - params { - id: 1 - name: "tunnel_peer_id" - bitwidth: 8 - } - params { - id: 2 - name: "session_meter_idx" - bitwidth: 32 - } -} -actions { - preamble { - id: 20229579 - name: "PreQosPipe.set_session_downlink_drop" - alias: "set_session_downlink_drop" - } -} -actions { - preamble { - id: 20249483 - name: "PreQosPipe.set_session_downlink_buff" - alias: "set_session_downlink_buff" - } - params { - id: 1 - name: "session_meter_idx" - bitwidth: 32 - } -} -actions { - preamble { - id: 21760615 - name: "PreQosPipe.uplink_term_fwd_no_tc" - alias: "uplink_term_fwd_no_tc" - } - params { - id: 1 - name: "ctr_idx" - bitwidth: 32 - } - params { - id: 2 - name: "app_meter_idx" - bitwidth: 32 - } -} -actions { - preamble { - id: 28305359 - name: "PreQosPipe.uplink_term_fwd" - alias: "uplink_term_fwd" - } - params { - id: 1 - name: "ctr_idx" - bitwidth: 32 - } - params { - id: 2 - name: "tc" - bitwidth: 2 - } - params { - id: 3 - name: "app_meter_idx" - bitwidth: 32 - } -} -actions { - preamble { - id: 20977365 - name: "PreQosPipe.uplink_term_drop" - alias: "uplink_term_drop" - } - params { - id: 1 - name: "ctr_idx" - bitwidth: 32 - } -} -actions { - preamble { - id: 26185804 - name: "PreQosPipe.downlink_term_fwd_no_tc" - alias: "downlink_term_fwd_no_tc" - } - params { - id: 1 - name: "ctr_idx" - bitwidth: 32 - } - params { - id: 2 - name: "teid" - bitwidth: 32 - } - params { - id: 3 - name: "qfi" - bitwidth: 6 - } - params { - id: 4 - name: "app_meter_idx" - bitwidth: 32 - } -} -actions { - preamble { - id: 32699713 - name: "PreQosPipe.downlink_term_fwd" - alias: "downlink_term_fwd" - } - params { - id: 1 - name: "ctr_idx" - bitwidth: 32 - } - params { - id: 2 - name: "teid" - bitwidth: 32 - } - params { - id: 3 - name: "qfi" - bitwidth: 6 - } - params { - id: 4 - name: "tc" - bitwidth: 2 - } - params { - id: 5 - name: "app_meter_idx" - bitwidth: 32 - } -} -actions { - preamble { - id: 31264233 - name: "PreQosPipe.downlink_term_drop" - alias: "downlink_term_drop" - } - params { - id: 1 - name: "ctr_idx" - bitwidth: 32 - } -} -actions { - preamble { - id: 23010411 - name: "PreQosPipe.set_app_id" - alias: "set_app_id" - } - params { - id: 1 - name: "app_id" - bitwidth: 8 - } -} -actions { - preamble { - id: 32742981 - name: "PreQosPipe.load_tunnel_param" - alias: "load_tunnel_param" - } - params { - id: 1 - name: "src_addr" - bitwidth: 32 - } - params { - id: 2 - name: "dst_addr" - bitwidth: 32 - } - params { - id: 3 - name: "sport" - bitwidth: 16 - } -} -actions { - preamble { - id: 29247910 - name: "PreQosPipe.do_gtpu_tunnel" - alias: "do_gtpu_tunnel" - } -} -actions { - preamble { - id: 31713420 - name: "PreQosPipe.do_gtpu_tunnel_with_psc" - alias: "do_gtpu_tunnel_with_psc" - } -} -action_profiles { - preamble { - id: 297808402 - name: "hashed_selector" - alias: "hashed_selector" - } - table_ids: 39015874 - with_selector: true - size: 1024 -} -counters { - preamble { - id: 315693181 - name: "PreQosPipe.pre_qos_counter" - alias: "pre_qos_counter" - } - spec { - unit: BOTH - } - size: 1024 -} -counters { - preamble { - id: 302958180 - name: "PostQosPipe.post_qos_counter" - alias: "post_qos_counter" - } - spec { - unit: BOTH - } - size: 1024 -} -direct_counters { - preamble { - id: 325583051 - name: "acls" - alias: "acls" - } - spec { - unit: BOTH - } - direct_table_id: 47204971 -} -direct_counters { - preamble { - id: 12345 - name: "MyDummyCounter" - alias: "mydummycounter" - } - spec { - unit: BOTH - } - direct_table_id: 1234 -} -meters { - preamble { - id: 338231090 - name: "PreQosPipe.app_meter" - alias: "app_meter" - } - spec { - unit: BYTES - } - size: 1024 -} -meters { - preamble { - id: 347593234 - name: "PreQosPipe.session_meter" - alias: "session_meter" - } - spec { - unit: BYTES - } - size: 1024 -} -controller_packet_metadata { - preamble { - id: 75327753 - name: "packet_out" - alias: "packet_out" - annotations: "@controller_header(\"packet_out\")" - } - metadata { - id: 1 - name: "reserved" - bitwidth: 8 - } -} -controller_packet_metadata { - preamble { - id: 80671331 - name: "packet_in" - alias: "packet_in" - annotations: "@controller_header(\"packet_in\")" - } - metadata { - id: 1 - name: "ingress_port" - bitwidth: 9 - } - metadata { - id: 2 - name: "_pad" - bitwidth: 7 - } -} -digests { - preamble { - id: 396224266 - name: "ddn_digest_t" - alias: "ddn_digest_t" - } - type_spec { - struct { - name: "ddn_digest_t" - } - } -} -type_info { - structs { - key: "ddn_digest_t" - value { - members { - name: "ue_address" - type_spec { - bitstring { - bit { - bitwidth: 32 - } - } - } - } - } - } - serializable_enums { - key: "Direction" - value { - underlying_type { - bitwidth: 8 - } - members { - name: "UNKNOWN" - value: "\000" - } - members { - name: "UPLINK" - value: "\001" - } - members { - name: "DOWNLINK" - value: "\002" - } - members { - name: "OTHER" - value: "\003" - } - } - } - serializable_enums { - key: "InterfaceType" - value { - underlying_type { - bitwidth: 8 - } - members { - name: "UNKNOWN" - value: "\000" - } - members { - name: "ACCESS" - value: "\001" - } - members { - name: "CORE" - value: "\002" - } - } - } -} diff --git a/scripts/go_gen_p4_const.go b/scripts/go_gen_p4_const.go deleted file mode 100644 index cdf58ab4b..000000000 --- a/scripts/go_gen_p4_const.go +++ /dev/null @@ -1,335 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -// Copyright 2022-present Open Networking Foundation - -package main - -import ( - "flag" - "fmt" - "io/ioutil" - "os" - "strings" - - "github.com/ettle/strcase" - "github.com/golang/protobuf/proto" - p4ConfigV1 "github.com/p4lang/p4runtime/go/p4/config/v1" -) - -const ( - p4infoPath = "conf/p4/bin/p4info.txt" - - defaultPackageName = "p4constants" - // copyrightHeader uses raw strings to avoid issues with reuse - copyrightHeader = `// SPDX-License-Identifier: Apache-2.0 -// Copyright 2022-present Open Networking Foundation -` - - constOpen = "const (\n" - mapFormatString = "%v:\"%v\",\n" - listFormatString = "%v,\n" - constOrVarClose = ")\n" - - idTypeString = "uint32" - sizeTypeString = "uint64" - - hfVarPrefix = "Hdr_" - tblVarPrefix = "Table_" - ctrVarPrefix = "Counter_" - ctrSizeVarPrefix = "CounterSize_" - dirCtrVarPrefix = "DirectCounter_" - actVarPrefix = "Action_" - actparamVarPrefix = "ActionParam_" - actprofVarPrefix = "ActionProfile_" - packetmetaVarPrefix = "PacketMeta_" - mtrVarPrefix = "Meter_" - mtrSizeVarPrefix = "MeterSize_" - - tableMapFunc = "func GetTableIDToNameMap() map[uint32]string {\n return map[uint32]string {\n" - tableListFunc = "func GetTableIDList() []uint32 {\n return []uint32 {\n" - actionMapFunc = "func GetActionIDToNameMap() map[uint32]string {\n return map[uint32]string {\n" - actionListFunc = "func GetActionIDList() []uint32 {\n return []uint32 {\n" - counterMapFunc = "func GetCounterIDToNameMap() map[uint32]string {\n return map[uint32]string {\n" - counterListFunc = "func GetCounterIDList() []uint32 {\n return []uint32 {\n" - directCounterMapFunc = "func GetDirectCounterIDToNameMap() map[uint32]string {\n return map[uint32]string {\n" - directCounterListFunc = "func GetDirectCounterIDList() []uint32 {\n return []uint32 {\n" - actionProfileMapFunc = "func GetActionProfileIDToNameMap() map[uint32]string {\n return map[uint32]string {\n" - actionProfileListFunc = "func GetActionProfileIDList() []uint32 {\n return []uint32 {\n" - pktMetadataMapFunc = "func GetPacketMetadataIDToNameMap() map[uint32]string {\n return map[uint32]string {\n" - pktMetadataListFunc = "func GetPacketMetadataIDList() []uint32 {\n return []uint32 {\n" - metersMapFunc = "func GetMeterIDToNameMap() map[uint32]string {\n return map[uint32]string {\n" - metersListFunc = "func GetMeterIDList() []uint32 {\n return []uint32 {\n" -) - -func emitEntityConstant(prefix string, p4EntityName string, id uint32) string { - // see: https://go.dev/ref/spec#Identifiers - p4EntityName = prefix + "_" + p4EntityName - p4EntityName = strings.Replace(p4EntityName, ".", "_", -1) - p4EntityName = strcase.ToPascal(p4EntityName) - return fmt.Sprintf("%s \t %s = %v\n", p4EntityName, idTypeString, id) -} - -func emitEntitySizeConstant(prefix string, p4EntityName string, id int64) string { - // see: https://go.dev/ref/spec#Identifiers - p4EntityName = prefix + "_" + p4EntityName - p4EntityName = strings.Replace(p4EntityName, ".", "_", -1) - p4EntityName = strcase.ToPascal(p4EntityName) - return fmt.Sprintf("%s \t %s = %v\n", p4EntityName, sizeTypeString, id) -} - -func generateTables(p4info *p4ConfigV1.P4Info) string { - mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} - - mapBuilder.WriteString(tableMapFunc) - listBuilder.WriteString(tableListFunc) - for _, element := range p4info.GetTables() { - name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - - mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) - listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) - } - mapBuilder.WriteString("}\n}\n\n") - listBuilder.WriteString("}\n}\n\n") //Close func declaration - - return mapBuilder.String() + listBuilder.String() -} - -func generateActions(p4info *p4ConfigV1.P4Info) string { - mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} - - mapBuilder.WriteString(actionMapFunc) - listBuilder.WriteString(actionListFunc) - for _, element := range p4info.GetActions() { - name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - - mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) - listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) - } - mapBuilder.WriteString("}\n}\n\n") - listBuilder.WriteString("}\n}\n\n") //Close func declarations - - return mapBuilder.String() + listBuilder.String() -} - -func generateIndirectCounters(p4info *p4ConfigV1.P4Info) string { - mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} - - mapBuilder.WriteString(counterMapFunc) - listBuilder.WriteString(counterListFunc) - for _, element := range p4info.GetCounters() { - name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - - mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) - listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) - } - mapBuilder.WriteString("}\n}\n\n") - listBuilder.WriteString("}\n}\n\n") //Close func declarations - - return mapBuilder.String() + listBuilder.String() -} - -func generateDirectCounters(p4info *p4ConfigV1.P4Info) string { - mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} - - mapBuilder.WriteString(directCounterMapFunc) - listBuilder.WriteString(directCounterListFunc) - for _, element := range p4info.GetDirectCounters() { - name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - - mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) - listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) - } - mapBuilder.WriteString("}\n}\n\n") - listBuilder.WriteString("}\n}\n\n") //Close declarations - - return mapBuilder.String() + listBuilder.String() -} - -func generateMeters(p4info *p4ConfigV1.P4Info) string { - mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} - - // Meters - mapBuilder.WriteString(metersMapFunc) - listBuilder.WriteString(metersListFunc) - for _, element := range p4info.GetMeters() { - name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - - mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) - listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) - } - mapBuilder.WriteString("}\n}\n\n") - listBuilder.WriteString("}\n}\n\n") //Close declarations - - return mapBuilder.String() + listBuilder.String() -} - -func generateActionProfiles(p4info *p4ConfigV1.P4Info) string { - mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} - - mapBuilder.WriteString(actionProfileMapFunc) - listBuilder.WriteString(actionProfileListFunc) - for _, element := range p4info.GetActionProfiles() { - name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - - mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) - listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) - } - mapBuilder.WriteString("}\n}\n\n") - listBuilder.WriteString("}\n}\n\n") //Close declarations - - return mapBuilder.String() + listBuilder.String() -} - -func generatePacketMetadata(p4info *p4ConfigV1.P4Info) string { - mapBuilder, listBuilder := strings.Builder{}, strings.Builder{} - - mapBuilder.WriteString(pktMetadataMapFunc) - listBuilder.WriteString(pktMetadataListFunc) - for _, element := range p4info.GetControllerPacketMetadata() { - name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - - mapBuilder.WriteString(fmt.Sprintf(mapFormatString, ID, name)) - listBuilder.WriteString(fmt.Sprintf(listFormatString, ID)) - } - mapBuilder.WriteString("}\n}\n\n") - listBuilder.WriteString("}\n}\n\n") //Close declarations - - return mapBuilder.String() + listBuilder.String() -} - -func generateConstants(p4info *p4ConfigV1.P4Info) string { - constBuilder := strings.Builder{} - - constBuilder.WriteString(constOpen) - - //HeaderField IDs - constBuilder.WriteString("// HeaderFields\n") - for _, element := range p4info.GetTables() { - for _, matchField := range element.MatchFields { - tableName, name := element.GetPreamble().GetName(), matchField.GetName() - - constBuilder.WriteString(emitEntityConstant(hfVarPrefix+tableName, name, matchField.GetId())) - } - } - // Tables - constBuilder.WriteString("// Tables\n") - for _, element := range p4info.GetTables() { - name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - - constBuilder.WriteString(emitEntityConstant(tblVarPrefix, name, ID)) - } - - // Actions - constBuilder.WriteString("// Actions\n") - for _, element := range p4info.GetActions() { - name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - - constBuilder.WriteString(emitEntityConstant(actVarPrefix, name, ID)) - } - - // Action Param IDs - constBuilder.WriteString("// ActionParams\n") - for _, element := range p4info.GetActions() { - for _, actionParam := range element.GetParams() { - actionName, name := element.GetPreamble().GetName(), actionParam.GetName() - - constBuilder.WriteString(emitEntityConstant(actparamVarPrefix+actionName, name, actionParam.GetId())) - } - } - - // Indirect Counters - constBuilder.WriteString("// IndirectCounters\n") - for _, element := range p4info.GetCounters() { - name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - - constBuilder.WriteString(emitEntityConstant(ctrVarPrefix, name, ID)) - constBuilder.WriteString(emitEntitySizeConstant(ctrSizeVarPrefix, name, element.GetSize())) - } - - // Direct Counters - constBuilder.WriteString("// DirectCounters\n") - for _, element := range p4info.GetDirectCounters() { - name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - - constBuilder.WriteString(emitEntityConstant(dirCtrVarPrefix, name, ID)) - } - - // Action profiles - constBuilder.WriteString("// ActionProfiles\n") - for _, element := range p4info.GetActionProfiles() { - name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - - constBuilder.WriteString(emitEntityConstant(actprofVarPrefix, name, ID)) - } - - // Packet metadata - constBuilder.WriteString("// PacketMetadata\n") - for _, element := range p4info.GetControllerPacketMetadata() { - name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - - constBuilder.WriteString(emitEntityConstant(packetmetaVarPrefix, name, ID)) - } - - // Meters - constBuilder.WriteString("// Meters\n") - for _, element := range p4info.GetMeters() { - name, ID := element.GetPreamble().GetName(), element.GetPreamble().GetId() - - constBuilder.WriteString(emitEntityConstant(mtrVarPrefix, name, ID)) - constBuilder.WriteString(emitEntitySizeConstant(mtrSizeVarPrefix, name, element.GetSize())) - } - - constBuilder.WriteString(constOrVarClose + "\n") - - return constBuilder.String() -} - -func getP4Config(p4infopath string) *p4ConfigV1.P4Info { - p4infoBytes, err := ioutil.ReadFile(p4infopath) - if err != nil { - panic(fmt.Sprintf("Could not read P4Info file: %v", err)) - } - - var p4info p4ConfigV1.P4Info - - err = proto.UnmarshalText(string(p4infoBytes), &p4info) - if err != nil { - panic("Could not parse P4Info file") - } - - return &p4info -} - -func main() { - p4infoPath := flag.String("p4info", p4infoPath, "Path of the p4info file") - outputPath := flag.String("output", "-", "Default will print to Stdout") - packageName := flag.String("package", defaultPackageName, "Set the package name") - - flag.Parse() - - p4config := getP4Config(*p4infoPath) - - headerBuilder := strings.Builder{} - - headerBuilder.WriteString(copyrightHeader + "\n") - headerBuilder.WriteString(fmt.Sprintf("package %s\n", *packageName)) - - headerBuilder.WriteString(generateConstants(p4config)) - - headerBuilder.WriteString(generateTables(p4config)) - headerBuilder.WriteString(generateActions(p4config)) - headerBuilder.WriteString(generateDirectCounters(p4config)) - headerBuilder.WriteString(generateIndirectCounters(p4config)) - headerBuilder.WriteString(generateActionProfiles(p4config)) - headerBuilder.WriteString(generatePacketMetadata(p4config)) - headerBuilder.WriteString(generateMeters(p4config)) - - result := headerBuilder.String() - - if *outputPath == "-" { - fmt.Println(result) - } else { - if err := os.WriteFile(*outputPath, []byte(result), 0644); err != nil { - panic(fmt.Sprintf("Error while creating File: %v", err)) - } - } -} From 2da4ab3e3c7cb967a15ba1d955392b8e6fc2f148 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Mon, 28 Feb 2022 14:22:15 +0100 Subject: [PATCH 41/53] Add copyright header --- cmd/p4info_code_gen/p4info_code_gen_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cmd/p4info_code_gen/p4info_code_gen_test.go b/cmd/p4info_code_gen/p4info_code_gen_test.go index 94c7af924..63c5fc17c 100644 --- a/cmd/p4info_code_gen/p4info_code_gen_test.go +++ b/cmd/p4info_code_gen/p4info_code_gen_test.go @@ -1,3 +1,6 @@ +// SPDX-License-Identifier: Apache-2.0 +// Copyright 2022-present Open Networking Foundation + package main import ( From 239baefeac175857a077786add2ed9550ea977e7 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Mon, 28 Feb 2022 16:46:15 +0100 Subject: [PATCH 42/53] Fix golint --- pfcpiface/p4rt_translator.go | 1 - pfcpiface/up4.go | 33 +++++++-------------------------- 2 files changed, 7 insertions(+), 27 deletions(-) diff --git a/pfcpiface/p4rt_translator.go b/pfcpiface/p4rt_translator.go index 8d1b151e6..50fc5bdc7 100644 --- a/pfcpiface/p4rt_translator.go +++ b/pfcpiface/p4rt_translator.go @@ -423,7 +423,6 @@ func (t *P4rtTranslator) BuildInterfaceTableEntryNoAction() *p4.TableEntry { } func (t *P4rtTranslator) BuildInterfaceTableEntry(ipNet *net.IPNet, sliceID uint8, isCore bool) (*p4.TableEntry, error) { - srcIface := access direction := DirectionUplink diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index 110d76d20..42411d07d 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -135,7 +135,7 @@ func (up4 *UP4) sessionStats(*PfcpNodeCollector, chan<- prometheus.Metric) error func (up4 *UP4) portStats(uc *upfCollector, ch chan<- prometheus.Metric) { } -func (up4 *UP4) initCounter(counterID uint8, name string, counterSize uint64) error { +func (up4 *UP4) initCounter(counterID uint8, name string, counterSize uint64) { up4.counters[counterID].maxSize = counterSize up4.counters[counterID].counterID = uint64(counterID) @@ -145,8 +145,6 @@ func (up4 *UP4) initCounter(counterID uint8, name string, counterSize uint64) er "max-size": counterSize, "UP4 counter ID": counterID, }).Debug("Counter initialized successfully") - - return nil } func resetCounterVal(p *UP4, counterID uint8, val uint64) { @@ -209,29 +207,21 @@ func (up4 *UP4) setupChannel() error { return nil } -func (up4 *UP4) initAllCounters() error { +func (up4 *UP4) initAllCounters() { log.Debug("Initializing counter for UP4") counterID, counterSize := p4constants.CounterPreQosPipePreQosCounter, p4constants.CounterSizePreQosPipePreQosCounter counterName := p4constants.GetCounterIDToNameMap()[counterID] - err := up4.initCounter(preQosCounterID, counterName, counterSize) - if err != nil { - return ErrOperationFailedWithReason("init preQosCounterID counter", err.Error()) - } + up4.initCounter(preQosCounterID, counterName, counterSize) counterID, counterSize = p4constants.CounterPostQosPipePostQosCounter, p4constants.CounterSizePostQosPipePostQosCounter counterName = p4constants.GetCounterIDToNameMap()[counterID] - err = up4.initCounter(postQosCounterID, counterName, counterSize) - if err != nil { - return ErrOperationFailedWithReason("init postQosCounterID counter", err.Error()) - } - - return nil + up4.initCounter(postQosCounterID, counterName, counterSize) } -func (up4 *UP4) initMetersPools() error { +func (up4 *UP4) initMetersPools() { log.Debug("Initializing P4 Meters pools for UP4") appMeterID, appMeterSize := p4constants.MeterPreQosPipeAppMeter, p4constants.MeterSizePreQosPipeAppMeter @@ -266,8 +256,6 @@ func (up4 *UP4) initMetersPools() error { "applicationMeter pool size": up4.appMeterCellIDsPool.Cardinality(), "sessMeter pool size": up4.sessMeterCellIDsPool.Cardinality(), }).Debug("P4 Meters pools initialized successfully") - - return nil } func (up4 *UP4) initTunnelPeerIDs() { @@ -446,15 +434,8 @@ func (up4 *UP4) tryConnect() error { log.Warningf("failed to clear tables: %v", err) } - err = up4.initAllCounters() - if err != nil { - return ErrOperationFailedWithReason("counters initialization", err.Error()) - } - - err = up4.initMetersPools() - if err != nil { - return ErrOperationFailedWithReason("meters pools initialization", err.Error()) - } + up4.initAllCounters() + up4.initMetersPools() go up4.listenToDDNs() From 00030feccac8ea91fb2570fa580e74e64ddf4e02 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Tue, 1 Mar 2022 21:20:54 +0100 Subject: [PATCH 43/53] Fix incorrect actions --- pfcpiface/p4rt_translator.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pfcpiface/p4rt_translator.go b/pfcpiface/p4rt_translator.go index 50fc5bdc7..9f37f22f2 100644 --- a/pfcpiface/p4rt_translator.go +++ b/pfcpiface/p4rt_translator.go @@ -653,7 +653,7 @@ func (t *P4rtTranslator) buildUplinkTerminationsEntry(pdr pdr, appMeterIdx uint3 } } else if !shouldDrop && tc != NoTC { action = &p4.Action{ - ActionId: p4constants.ActionPreQosPipeUplinkTermDrop, + ActionId: p4constants.ActionPreQosPipeUplinkTermFwd, } if err := t.withActionParam(action, FieldTrafficClass, tc); err != nil { @@ -664,7 +664,7 @@ func (t *P4rtTranslator) buildUplinkTerminationsEntry(pdr pdr, appMeterIdx uint3 } } else { action = &p4.Action{ - ActionId: p4constants.ActionPreQosPipeDownlinkTermFwdNoTc, + ActionId: p4constants.ActionPreQosPipeUplinkTermFwdNoTc, } if err := t.withActionParam(action, FieldAppMeterIndex, appMeterIdx); err != nil { return nil, err From d39316e483e1c7d626103ce5c28c5fda14e5219c Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Wed, 2 Mar 2022 17:24:48 +0100 Subject: [PATCH 44/53] Add applications table when clearing tables --- pfcpiface/up4.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index 722f82006..d2a4d2716 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -365,6 +365,7 @@ func (up4 *UP4) clearTables() error { p4constants.TablePreQosPipeTerminationsDownlink, p4constants.TablePreQosPipeTunnelPeers, p4constants.TablePreQosPipeInterfaces, + p4constants.TablePreQosPipeApplications, } if err := up4.p4client.ClearTables(tableIDs); err != nil { From 7df6ee7d7094d697f32fa8029cdf304543ccf57a Mon Sep 17 00:00:00 2001 From: tosinski Date: Mon, 7 Mar 2022 16:27:47 +0100 Subject: [PATCH 45/53] Don't set UP4 forwarding pipeline config --- Dockerfile | 1 - conf/p4/bin/p4info.bin | Bin 4005 -> 0 bytes pfcpiface/p4rtc.go | 13 ++++++++++++- pfcpiface/up4.go | 11 +---------- test/integration/basic_test.go | 7 +++++++ test/integration/framework.go | 16 ++++++++++++++++ 6 files changed, 36 insertions(+), 12 deletions(-) delete mode 100644 conf/p4/bin/p4info.bin diff --git a/Dockerfile b/Dockerfile index a4642f686..bdf09350b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -131,7 +131,6 @@ RUN CGO_ENABLED=0 go build $GOFLAGS -o /bin/pfcpiface ./cmd/pfcpiface # Stage pfcpiface: runtime image of pfcpiface toward SMF/SPGW-C FROM alpine AS pfcpiface COPY conf /opt/bess/bessctl/conf -COPY conf/p4/bin/p4info.bin conf/p4/bin/p4info.txt conf/p4/bin/bmv2.json /bin/ COPY --from=pfcpiface-build /bin/pfcpiface /bin ENTRYPOINT [ "/bin/pfcpiface" ] diff --git a/conf/p4/bin/p4info.bin b/conf/p4/bin/p4info.bin deleted file mode 100644 index 8335fc7967a213813270d2a601757444a4711dee..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 4005 zcma)9U2GIp6n3`T_U`Sr-JZ4-t|F6_)IvZQ4oD4g+?S$>jFf$8c zVyrPfASenAF~J9r$b&zGC?W)c(g-2^1_+4>i9U!i!lOUL1i^Ff>~3dvx0IKed(Yf^ z?l<2(_dDm(#Pr(D&kfoJ&%#F9Nk+c71n|U4m#?zDm8QcxSKB$?wE8<;d~;9RJc}iY zO#q?*H9TK)T;6BCuBshXteTv=d9;qTlJAawL?OAr;C*^7>)Td#>%`cMUAKllP!Q_o~QMhY!xz%^=2%duTwO?1SeiNqk`xf#vu(=%KpXlf)? zP|N)R4bAEdX@~~~#3l_XQVm3m@V7c#P2rFfsR3d-n`g;Ms@GVsI(Xb3sUf4-eY_P|2%E<}4}p1F&dHkAM%Gwj2tvb} zsp*E{;#lM}21(qyxX(>P^bq#NI|mZ@I{Vvsit+O2$usq(@uCW&Kry7y*<}BzbE!yh z78f<_EtbU5RAvg0Ze9}*w%@&;7!Sg*Lc7V1>;E*tlOZCI+d8~5V=Ni&;h|K3 zc=(kIM&cMU&B#!lWXO>lC?p1rRe=%DVgArnLko$6@TQp#%2EEfBG5a>?om}dL>ToW zpr5u~YKs8!bC$)kn!~y4v1GUh6A(~C$TGT7XHH&j97E)AA=ZftNdY9i_QH%j!^Kv* zX@O<0*Ilb4owITtH`3GO_#cixXovtSXs_Yg4ogOQuhMp@voJBlY+-~HXjUy>P%q>{ z=%thk?8ZXsfoh6W@_und11b}2Hj0SQuLu+&#~w$aM;T_M&;p9i<8liUEKOA@cx8g_ z#U`sb%4$yLi8fo#GiV0&J__UG?b-EEmDue2?-6Dp$yaI za^F>g$Gj>W)@&+G2Wkf)q=v1@->5A#*3dapAz^Z8lw3W~-h%W(xlM|DK?=oxa~C5Q z^D|paUzB4!QEptO~H4n7Pb)j~icREGx_q=mIqvf^2-aI6q?3@UuK^!6zL(39TRB(}rAMAFJ$1d>Wf!1LauAcSMeZEU5|k)%vy=nq_N#hP75S z2PZb;=eJBsV7(Bw@IG$9q%3kpBq^7;V~r(-QqKelkNFu@$ZQpqtl z@tP{Nl&r$u!jKc$A`)`N9V?-vZHx~kPEmM@r354peAT8tQ?0@Oh?oDzn^-!C*F;s}HiKGLx*oiO3(Kl2cV#vk*}-=KFQXes#VYW{jK2suvLB1#s6ol1By7`&QR?+!n+qJ)+0_y0QjTlpS z;bWn8T3wK_E#I}XSaPT!pH`Lkp4FgK F{ST>!2)zIR diff --git a/pfcpiface/p4rtc.go b/pfcpiface/p4rtc.go index 37282a554..88f01e8a3 100644 --- a/pfcpiface/p4rtc.go +++ b/pfcpiface/p4rtc.go @@ -433,7 +433,11 @@ func (c *P4rtClient) WriteBatchReq(updates []*p4.Update) error { // GetForwardingPipelineConfig ... Get Pipeline config from switch. func (c *P4rtClient) GetForwardingPipelineConfig() (err error) { - log.Println("GetForwardingPipelineConfig") + getLog := log.WithFields(log.Fields{ + "device ID": c.deviceID, + "conn": c.conn.Target(), + }) + getLog.Info("Getting ForwardingPipelineConfig from P4Rt device") pipeline, err := GetPipelineConfig(c.client, c.deviceID) if err != nil { @@ -441,8 +445,15 @@ func (c *P4rtClient) GetForwardingPipelineConfig() (err error) { return } + if pipeline.GetConfig() == nil { + return ErrOperationFailedWithReason("GetForwardingPipelineConfig", + "Operation successful, but no P4 config provided.") + } + c.P4Info = *pipeline.Config.P4Info + getLog.Info("Got ForwardingPipelineConfig from P4Rt device") + return } diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index dc56e89e1..ac26024e8 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -22,11 +22,6 @@ import ( "github.com/wmnsk/go-pfcp/ie" ) -const ( - p4InfoPath = "/bin/p4info.txt" - deviceConfigPath = "/bin/bmv2.json" -) - const ( preQosCounterID = iota postQosCounterID @@ -206,11 +201,7 @@ func (up4 *UP4) setupChannel() error { err = up4.p4client.GetForwardingPipelineConfig() if err != nil { - err = up4.p4client.SetForwardingPipelineConfig(p4InfoPath, deviceConfigPath) - if err != nil { - log.Errorf("set forwarding pipeling config failed: %v", err) - return err - } + return err } return nil diff --git a/test/integration/basic_test.go b/test/integration/basic_test.go index 7eaafa3e1..e9ea161cf 100644 --- a/test/integration/basic_test.go +++ b/test/integration/basic_test.go @@ -5,6 +5,7 @@ package integration import ( "net" + "os" "testing" "time" @@ -14,6 +15,12 @@ import ( "github.com/wmnsk/go-pfcp/ie" ) +func init() { + if os.Getenv(EnvFastpath) == FastpathUP4 { + initForwardingPipelineConfig() + } +} + func TestUPFBasedUeIPAllocation(t *testing.T) { setup(t, ConfigUPFBasedIPAllocation) defer teardown(t) diff --git a/test/integration/framework.go b/test/integration/framework.go index b88ecb3cb..4eb8125c2 100644 --- a/test/integration/framework.go +++ b/test/integration/framework.go @@ -51,6 +51,9 @@ const ( directionUplink = 0x1 directionDownlink = 0x2 + + p4InfoPath = "../../conf/p4/bin/p4info.txt" + deviceConfigPath = "../../conf/p4/bin/bmv2.json" ) type UEState uint8 @@ -225,6 +228,19 @@ func isFastpathBESS() bool { return os.Getenv(EnvFastpath) == FastpathBESS } +func initForwardingPipelineConfig() { + p4rtClient, err := providers.ConnectP4rt("127.0.0.1:50001", ReaderElectionID) + if err != nil { + panic("Cannot init forwarding pipeline config: " + err.Error()) + } + defer providers.DisconnectP4rt() + + _, err = p4rtClient.SetFwdPipe(deviceConfigPath, p4InfoPath, 0) + if err != nil { + panic("Cannot init forwarding pipeline config: " + err.Error()) + } +} + func setup(t *testing.T, configType uint32) { // TODO: we currently need to reset the DefaultRegisterer between tests, as some leave the // the registry in a bad state. Use custom registries to avoid global state. From 2ea3a8aa87ccfb460519708fd9f937f724648116 Mon Sep 17 00:00:00 2001 From: tosinski Date: Mon, 7 Mar 2022 16:28:39 +0100 Subject: [PATCH 46/53] Fix golint --- pfcpiface/p4rtc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pfcpiface/p4rtc.go b/pfcpiface/p4rtc.go index 88f01e8a3..75a44eaa1 100644 --- a/pfcpiface/p4rtc.go +++ b/pfcpiface/p4rtc.go @@ -435,7 +435,7 @@ func (c *P4rtClient) WriteBatchReq(updates []*p4.Update) error { func (c *P4rtClient) GetForwardingPipelineConfig() (err error) { getLog := log.WithFields(log.Fields{ "device ID": c.deviceID, - "conn": c.conn.Target(), + "conn": c.conn.Target(), }) getLog.Info("Getting ForwardingPipelineConfig from P4Rt device") From 7409a228b09fa535c0cb130128aef43d479c408f Mon Sep 17 00:00:00 2001 From: tosinski Date: Mon, 7 Mar 2022 16:35:35 +0100 Subject: [PATCH 47/53] Clean test code and add comment --- pfcpiface/p4rtc.go | 2 ++ test/integration/basic_test.go | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/pfcpiface/p4rtc.go b/pfcpiface/p4rtc.go index 75a44eaa1..35a54e1bb 100644 --- a/pfcpiface/p4rtc.go +++ b/pfcpiface/p4rtc.go @@ -445,6 +445,8 @@ func (c *P4rtClient) GetForwardingPipelineConfig() (err error) { return } + // P4 spec allows for sending successful response to GetForwardingPipelineConfig + // without config. We fail in such a case, because the response without config is useless. if pipeline.GetConfig() == nil { return ErrOperationFailedWithReason("GetForwardingPipelineConfig", "Operation successful, but no P4 config provided.") diff --git a/test/integration/basic_test.go b/test/integration/basic_test.go index e9ea161cf..8933b6b0f 100644 --- a/test/integration/basic_test.go +++ b/test/integration/basic_test.go @@ -5,7 +5,6 @@ package integration import ( "net" - "os" "testing" "time" @@ -16,7 +15,7 @@ import ( ) func init() { - if os.Getenv(EnvFastpath) == FastpathUP4 { + if isFastpathUP4() && isModeDocker() { initForwardingPipelineConfig() } } From 5fc015dffc977bbc9789724bebd823bf3f851155 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomasz=20Osi=C5=84ski?= Date: Mon, 7 Mar 2022 19:38:11 +0100 Subject: [PATCH 48/53] Update logger Co-authored-by: Maximilian Pudelko --- pfcpiface/p4rtc.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pfcpiface/p4rtc.go b/pfcpiface/p4rtc.go index 35a54e1bb..9245db5d3 100644 --- a/pfcpiface/p4rtc.go +++ b/pfcpiface/p4rtc.go @@ -441,7 +441,7 @@ func (c *P4rtClient) GetForwardingPipelineConfig() (err error) { pipeline, err := GetPipelineConfig(c.client, c.deviceID) if err != nil { - log.Println("set pipeline config error ", err) + getLog.Println("set pipeline config error ", err) return } From 71c75c773c46c96447349453329cd4a1f312e215 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Wed, 9 Mar 2022 17:46:09 +0100 Subject: [PATCH 49/53] Add getMeterByID method --- pfcpiface/p4rt_translator.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/pfcpiface/p4rt_translator.go b/pfcpiface/p4rt_translator.go index 9f37f22f2..fbbf82686 100644 --- a/pfcpiface/p4rt_translator.go +++ b/pfcpiface/p4rt_translator.go @@ -122,6 +122,16 @@ func (t *P4rtTranslator) getActionByID(actionID uint32) (*p4ConfigV1.Action, err return nil, ErrNotFoundWithParam("action", "ID", actionID) } +func (t *P4rtTranslator) getMeterByID(meterID uint32) (*p4ConfigV1.Meter, error) { + for _, meter := range t.p4Info.Meters { + if meter.Preamble.Id == meterID { + return meter, nil + } + } + + return nil, ErrNotFoundWithParam("meter", "ID", meterID) +} + func (t *P4rtTranslator) getTableByID(tableID uint32) (*p4ConfigV1.Table, error) { for _, table := range t.p4Info.Tables { if table.Preamble.Id == tableID { From fb93525a446f4c306f4354a07d1b3c92937370e5 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Fri, 11 Mar 2022 11:30:18 +0100 Subject: [PATCH 50/53] Add getMeterSizeByID method --- pfcpiface/p4rt_translator.go | 6 +++--- pfcpiface/up4.go | 12 ++++++++++-- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/pfcpiface/p4rt_translator.go b/pfcpiface/p4rt_translator.go index fbbf82686..fb2964a71 100644 --- a/pfcpiface/p4rt_translator.go +++ b/pfcpiface/p4rt_translator.go @@ -122,14 +122,14 @@ func (t *P4rtTranslator) getActionByID(actionID uint32) (*p4ConfigV1.Action, err return nil, ErrNotFoundWithParam("action", "ID", actionID) } -func (t *P4rtTranslator) getMeterByID(meterID uint32) (*p4ConfigV1.Meter, error) { +func (t *P4rtTranslator) getMeterSizeByID(meterID uint32) (int64, error) { for _, meter := range t.p4Info.Meters { if meter.Preamble.Id == meterID { - return meter, nil + return meter.GetSize(), nil } } - return nil, ErrNotFoundWithParam("meter", "ID", meterID) + return 0, ErrNotFoundWithParam("meter", "ID", meterID) } func (t *P4rtTranslator) getTableByID(tableID uint32) (*p4ConfigV1.Table, error) { diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index 7f9cb0ab9..e31a962f4 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -215,8 +215,12 @@ func (up4 *UP4) initAllCounters() { func (up4 *UP4) initMetersPools() { log.Debug("Initializing P4 Meters pools for UP4") - appMeterID, appMeterSize := p4constants.MeterPreQosPipeAppMeter, p4constants.MeterSizePreQosPipeAppMeter + appMeterID := p4constants.MeterPreQosPipeAppMeter appMeterName := p4constants.GetMeterIDToNameMap()[appMeterID] + appMeterSize, err := up4.p4RtTranslator.getMeterSizeByID(appMeterID) + if err != nil { + log.Errorf("Could not find meter size of %v", appMeterName) + } log.WithFields(log.Fields{ "name": appMeterName, @@ -229,8 +233,12 @@ func (up4 *UP4) initMetersPools() { log.Trace("Application meter IDs pool initialized: ", up4.appMeterCellIDsPool.String()) - sessMeterID, sessMeterSize := p4constants.MeterPreQosPipeSessionMeter, p4constants.MeterSizePreQosPipeSessionMeter + sessMeterID := p4constants.MeterPreQosPipeSessionMeter sessMeterName := p4constants.GetMeterIDToNameMap()[sessMeterID] + sessMeterSize, err := up4.p4RtTranslator.getMeterSizeByID(sessMeterID) + if err != nil { + log.Errorf("Could not find meter size of %v", sessMeterName) + } log.WithFields(log.Fields{ "name": sessMeterName, From 2ea39a1be7d4b38d9a42c2d5eb6f210c9b8a0689 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Fri, 11 Mar 2022 13:28:38 +0100 Subject: [PATCH 51/53] Sync with latest changes --- pfcpiface/p4rt_translator.go | 10 +++++ pfcpiface/up4.go | 81 ++++++++++++++++++++---------------- 2 files changed, 56 insertions(+), 35 deletions(-) diff --git a/pfcpiface/p4rt_translator.go b/pfcpiface/p4rt_translator.go index 80abc3b54..7dcd2f08f 100644 --- a/pfcpiface/p4rt_translator.go +++ b/pfcpiface/p4rt_translator.go @@ -133,6 +133,16 @@ func (t *P4rtTranslator) getMeterSizeByID(meterID uint32) (int64, error) { return 0, ErrNotFoundWithParam("meter", "ID", meterID) } +func (t *P4rtTranslator) getCounterSizeByID(counterID uint32) (int64, error) { + for _, counter := range t.p4Info.Counters { + if counter.Preamble.Id == counterID { + return counter.GetSize(), nil + } + } + + return 0, ErrNotFoundWithParam("counter", "ID", counterID) +} + func (t *P4rtTranslator) getTableByID(tableID uint32) (*p4ConfigV1.Table, error) { for _, table := range t.p4Info.Tables { if table.Preamble.Id == tableID { diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index 1e1685e93..e564ad7af 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -199,58 +199,69 @@ func (up4 *UP4) setupChannel() error { } func (up4 *UP4) initAllCounters() { - log.Debug("Initializing counter for UP4") + log.Debug("Initializing counters for UP4") - counterID, counterSize := p4constants.CounterPreQosPipePreQosCounter, p4constants.CounterSizePreQosPipePreQosCounter - counterName := p4constants.GetCounterIDToNameMap()[counterID] + counters := []uint32{ + p4constants.CounterPreQosPipePreQosCounter, + p4constants.CounterPostQosPipePostQosCounter, + } - up4.initCounter(preQosCounterID, counterName, counterSize) + for _, counterID := range counters { + counterName := p4constants.GetCounterIDToNameMap()[counterID] - counterID, counterSize = p4constants.CounterPostQosPipePostQosCounter, p4constants.CounterSizePostQosPipePostQosCounter - counterName = p4constants.GetCounterIDToNameMap()[counterID] + counterSize, err := up4.p4RtTranslator.getCounterSizeByID(counterID) + if err != nil { + log.Error(err) + } - up4.initCounter(postQosCounterID, counterName, counterSize) + switch counterID { //FIXME initCounter should accept a uint32 but doing so will break integration tests (PFCP Agent cannot connect). Need to investigate on why + case p4constants.CounterPreQosPipePreQosCounter: + up4.initCounter(preQosCounterID, counterName, uint64(counterSize)) + case p4constants.CounterPostQosPipePostQosCounter: + up4.initCounter(postQosCounterID, counterName, uint64(counterSize)) + } + } } func (up4 *UP4) initMetersPools() { log.Debug("Initializing P4 Meters pools for UP4") - appMeterID := p4constants.MeterPreQosPipeAppMeter - appMeterName := p4constants.GetMeterIDToNameMap()[appMeterID] - appMeterSize, err := up4.p4RtTranslator.getMeterSizeByID(appMeterID) - if err != nil { - log.Errorf("Could not find meter size of %v", appMeterName) + meters := []uint32{ + p4constants.MeterPreQosPipeAppMeter, + p4constants.MeterPreQosPipeSessionMeter, } - log.WithFields(log.Fields{ - "name": appMeterName, - }).Trace("Found P4 meter by name") - - up4.appMeterCellIDsPool = set.NewSet() - for i := 1; i < int(appMeterSize); i++ { - up4.appMeterCellIDsPool.Add(uint32(i)) - } + for _, meterID := range meters { + meterName, exists := p4constants.GetMeterIDToNameMap()[meterID] + if exists { + log.WithFields(log.Fields{ + "name": meterName, + }).Trace("Found P4 meter") + } - log.Trace("Application meter IDs pool initialized: ", up4.appMeterCellIDsPool.String()) + meterSize, err := up4.p4RtTranslator.getMeterSizeByID(meterID) + if err != nil { + log.Errorf("Could not find meter size of %v", meterName) + } - sessMeterID := p4constants.MeterPreQosPipeSessionMeter - sessMeterName := p4constants.GetMeterIDToNameMap()[sessMeterID] - sessMeterSize, err := up4.p4RtTranslator.getMeterSizeByID(sessMeterID) - if err != nil { - log.Errorf("Could not find meter size of %v", sessMeterName) - } + switch meterID { + case p4constants.MeterPreQosPipeAppMeter: + up4.appMeterCellIDsPool = set.NewSet() + for i := 1; i < int(meterSize); i++ { + up4.appMeterCellIDsPool.Add(uint32(i)) + } - log.WithFields(log.Fields{ - "name": sessMeterName, - }).Trace("Found P4 meter by name") + log.Trace("Application meter IDs pool initialized: ", up4.appMeterCellIDsPool.String()) + case p4constants.MeterPreQosPipeSessionMeter: + up4.sessMeterCellIDsPool = set.NewSet() + for i := 1; i < int(meterSize); i++ { + up4.sessMeterCellIDsPool.Add(uint32(i)) + } - up4.sessMeterCellIDsPool = set.NewSet() - for i := 1; i < int(sessMeterSize); i++ { - up4.sessMeterCellIDsPool.Add(uint32(i)) + log.Trace("Session meter IDs pool initialized: ", up4.sessMeterCellIDsPool.String()) + } } - log.Trace("Session meter IDs pool initialized: ", up4.sessMeterCellIDsPool.String()) - log.WithFields(log.Fields{ "applicationMeter pool size": up4.appMeterCellIDsPool.Cardinality(), "sessMeter pool size": up4.sessMeterCellIDsPool.Cardinality(), From d4eb1ab4f4459e5a50366d27cca291542b3e0ea6 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Fri, 11 Mar 2022 14:59:50 +0100 Subject: [PATCH 52/53] Fix counter panic in integration tests --- pfcpiface/up4.go | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index e564ad7af..a1fb7adb4 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -214,8 +214,9 @@ func (up4 *UP4) initAllCounters() { log.Error(err) } - switch counterID { //FIXME initCounter should accept a uint32 but doing so will break integration tests (PFCP Agent cannot connect). Need to investigate on why + switch counterID { case p4constants.CounterPreQosPipePreQosCounter: + //FIXME to fully exploit p4constants, counters should be a map instead of a slice up4.initCounter(preQosCounterID, counterName, uint64(counterSize)) case p4constants.CounterPostQosPipePostQosCounter: up4.initCounter(postQosCounterID, counterName, uint64(counterSize)) @@ -232,12 +233,7 @@ func (up4 *UP4) initMetersPools() { } for _, meterID := range meters { - meterName, exists := p4constants.GetMeterIDToNameMap()[meterID] - if exists { - log.WithFields(log.Fields{ - "name": meterName, - }).Trace("Found P4 meter") - } + meterName := p4constants.GetMeterIDToNameMap()[meterID] meterSize, err := up4.p4RtTranslator.getMeterSizeByID(meterID) if err != nil { From 9ec6143d76186b778cc34e87cd959cc1eed7f3b9 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Fri, 11 Mar 2022 16:22:55 +0100 Subject: [PATCH 53/53] Address review comments --- pfcpiface/up4.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pfcpiface/up4.go b/pfcpiface/up4.go index a1fb7adb4..856be7369 100644 --- a/pfcpiface/up4.go +++ b/pfcpiface/up4.go @@ -216,7 +216,6 @@ func (up4 *UP4) initAllCounters() { switch counterID { case p4constants.CounterPreQosPipePreQosCounter: - //FIXME to fully exploit p4constants, counters should be a map instead of a slice up4.initCounter(preQosCounterID, counterName, uint64(counterSize)) case p4constants.CounterPostQosPipePostQosCounter: up4.initCounter(postQosCounterID, counterName, uint64(counterSize)) @@ -1001,7 +1000,7 @@ func (up4 *UP4) resetMeter(meterID uint32, meter meter) { err := up4.p4client.ApplyMeterEntries(p4.Update_MODIFY, entries...) if err != nil { - log.Errorf("Failed to reset %v meter entries: %v", meterID, err) + log.Errorf("Failed to reset %v meter entries: %v", p4constants.GetMeterIDToNameMap()[meterID], err) } }