From 513b0aee7616e4ed332b9579d91314a922d88cb3 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Mon, 21 Feb 2022 17:01:15 +0100 Subject: [PATCH 01/25] * 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 02/25] 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 03/25] 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 04/25] 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 05/25] 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 06/25] 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 07/25] 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 08/25] 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 09/25] 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 10/25] 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 bc1db35de1b5cd88a1568e0f1413ac761b05aa26 Mon Sep 17 00:00:00 2001 From: Emanuele Gallone Date: Wed, 23 Feb 2022 21:58:46 +0100 Subject: [PATCH 11/25] 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 12/25] 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 13/25] 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 14/25] 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 15/25] 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 16/25] 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 17/25] 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 18/25] 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 19/25] 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 20/25] 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 21/25] 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 22/25] 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 23/25] 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 393ba4adae70244ff7dc9973c8c48d252772a0fa Mon Sep 17 00:00:00 2001 From: pudelkoM Date: Fri, 25 Feb 2022 12:37:41 -0800 Subject: [PATCH 24/25] 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 25/25] 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 (