blob: 36642faade224de4c09916f09e5542871c8955ec [file] [log] [blame]
Don Newton98fd8812019-09-23 15:15:02 -04001/*
David K. Bainbridge157bdab2020-01-16 14:38:05 -08002 Copyright 2020 the original author or authors.
Don Newton98fd8812019-09-23 15:15:02 -04003
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17package openflow
18
19import (
20 "fmt"
Jonathan Hart828908c2020-04-15 14:23:45 -070021 ofp "github.com/opencord/goloxi/of13"
Maninder12b909f2020-10-23 14:23:36 +053022 "github.com/opencord/voltha-protos/v4/go/openflow_13"
ssiddiquidf20bf72021-08-23 14:00:56 +053023 "net"
David K. Bainbridge157bdab2020-01-16 14:38:05 -080024 "strings"
25 "sync"
Don Newton98fd8812019-09-23 15:15:02 -040026)
27
28var mu sync.Mutex
29var xid uint32 = 1
30
31func GetXid() uint32 {
32 mu.Lock()
33 defer mu.Unlock()
34 xid++
35 return xid
36}
37func PadString(value string, padSize int) string {
38 size := len(value)
39 nullsNeeded := padSize - size
40 null := fmt.Sprintf("%c", '\000')
41 padded := strings.Repeat(null, nullsNeeded)
42 return fmt.Sprintf("%s%s", value, padded)
43}
Don Newtone0d34a82019-11-14 10:58:06 -050044
45func extractAction(action ofp.IAction) *openflow_13.OfpAction {
46 var ofpAction openflow_13.OfpAction
47 switch action.GetType() {
48 case ofp.OFPATOutput:
49 var outputAction openflow_13.OfpAction_Output
50 loxiOutputAction := action.(*ofp.ActionOutput)
51 var output openflow_13.OfpActionOutput
Don Newtonb437c6f2019-12-18 11:51:57 -050052 output.Port = uint32(loxiOutputAction.GetPort())
53 /*
54 var maxLen uint16
55 maxLen = loxiOutputAction.GetMaxLen()
56 output.MaxLen = uint32(maxLen)
57
58 */
59 output.MaxLen = 0
Don Newtone0d34a82019-11-14 10:58:06 -050060 outputAction.Output = &output
61 ofpAction.Action = &outputAction
62 ofpAction.Type = openflow_13.OfpActionType_OFPAT_OUTPUT
63 case ofp.OFPATCopyTtlOut: //CopyTtltOut
64 case ofp.OFPATCopyTtlIn: //CopyTtlIn
65 case ofp.OFPATSetMplsTtl: //SetMplsTtl
ssiddiquidf20bf72021-08-23 14:00:56 +053066 mplsTtl := action.(*ofp.ActionSetMplsTtl)
67 setMplsTtl := openflow_13.OfpAction_MplsTtl{
68 MplsTtl: &openflow_13.OfpActionMplsTtl{
69 MplsTtl: uint32(mplsTtl.MplsTtl),
70 },
71 }
72 ofpAction.Type = openflow_13.OfpActionType_OFPAT_SET_MPLS_TTL
73 ofpAction.Action = &setMplsTtl
Don Newtone0d34a82019-11-14 10:58:06 -050074 case ofp.OFPATDecMplsTtl: //DecMplsTtl
75 case ofp.OFPATPushVLAN: //PushVlan
76 var pushVlan openflow_13.OfpAction_Push
77 loxiPushAction := action.(*ofp.ActionPushVlan)
Don Newtone0d34a82019-11-14 10:58:06 -050078 var push openflow_13.OfpActionPush
79 push.Ethertype = uint32(loxiPushAction.Ethertype) //TODO This should be available in the fields
80 pushVlan.Push = &push
81 ofpAction.Type = openflow_13.OfpActionType_OFPAT_PUSH_VLAN
82 ofpAction.Action = &pushVlan
83 case ofp.OFPATPopVLAN: //PopVlan
84 ofpAction.Type = openflow_13.OfpActionType_OFPAT_POP_VLAN
85 case ofp.OFPATPushMpls: //PushMpls
ssiddiquidf20bf72021-08-23 14:00:56 +053086 var pushMpls openflow_13.OfpAction_Push
87 mplsPushAction := action.(*ofp.ActionPushMpls)
88 var push openflow_13.OfpActionPush
89 push.Ethertype = uint32(mplsPushAction.Ethertype)
90 pushMpls.Push = &push
91 ofpAction.Type = openflow_13.OfpActionType_OFPAT_PUSH_MPLS
92 ofpAction.Action = &pushMpls
Don Newtone0d34a82019-11-14 10:58:06 -050093 case ofp.OFPATPopMpls: //PopMpls
ssiddiquidf20bf72021-08-23 14:00:56 +053094 ofpAction.Type = openflow_13.OfpActionType_OFPAT_POP_MPLS
Don Newtone0d34a82019-11-14 10:58:06 -050095 case ofp.OFPATSetQueue: //SetQueue
96 case ofp.OFPATGroup: //ActionGroup
Jonathan Hart60c5d772020-03-30 18:28:40 -070097 ofpAction.Type = openflow_13.OfpActionType_OFPAT_GROUP
98 group := action.(*ofp.ActionGroup)
99 ofpAction.Action = &openflow_13.OfpAction_Group{
100 Group: &openflow_13.OfpActionGroup{
101 GroupId: group.GroupId,
102 },
103 }
Don Newtone0d34a82019-11-14 10:58:06 -0500104 case ofp.OFPATSetNwTtl: //SetNwTtl
105 case ofp.OFPATDecNwTtl: //DecNwTtl
106 case ofp.OFPATSetField: //SetField
107 ofpAction.Type = openflow_13.OfpActionType_OFPAT_SET_FIELD
108 var ofpAction_SetField openflow_13.OfpAction_SetField
109 var ofpActionSetField openflow_13.OfpActionSetField
110 var ofpOxmField openflow_13.OfpOxmField
111 ofpOxmField.OxmClass = openflow_13.OfpOxmClass_OFPXMC_OPENFLOW_BASIC
112 var ofpOxmField_OfbField openflow_13.OfpOxmField_OfbField
113 var ofpOxmOfbField openflow_13.OfpOxmOfbField
114 loxiSetField := action.(*ofp.ActionSetField)
115 oxmName := loxiSetField.Field.GetOXMName()
116 switch oxmName {
Don Newton7577f072020-01-06 12:41:11 -0500117 //TODO handle set field sith other fields
Don Newtone0d34a82019-11-14 10:58:06 -0500118 case "vlan_vid":
119 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID
120 var vlanVid openflow_13.OfpOxmOfbField_VlanVid
121 var VlanVid = loxiSetField.Field.GetOXMValue().(uint16)
122 vlanVid.VlanVid = uint32(VlanVid)
Don Newtone0d34a82019-11-14 10:58:06 -0500123 ofpOxmOfbField.Value = &vlanVid
Gamze Abaka3e2b2ce2020-05-09 10:21:40 +0000124 case "vlan_pcp":
125 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_PCP
126 var vlanPcp openflow_13.OfpOxmOfbField_VlanPcp
127 var VlanPcp = loxiSetField.Field.GetOXMValue().(uint8)
128 vlanPcp.VlanPcp = uint32(VlanPcp)
129 ofpOxmOfbField.Value = &vlanPcp
ssiddiquidf20bf72021-08-23 14:00:56 +0530130 case "mpls_label":
131 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_MPLS_LABEL
132 var mplsLabel openflow_13.OfpOxmOfbField_MplsLabel
133 label := loxiSetField.Field.GetOXMValue().(uint32)
134 mplsLabel.MplsLabel = label
135 ofpOxmOfbField.Value = &mplsLabel
136 case "mpls_bos":
137 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_MPLS_BOS
138 var mplsBos openflow_13.OfpOxmOfbField_MplsBos
139 bos := loxiSetField.Field.GetOXMValue().(uint8)
140 mplsBos.MplsBos = uint32(bos)
141 ofpOxmOfbField.Value = &mplsBos
142 case "eth_src":
143 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_ETH_SRC
144 var ethSrc openflow_13.OfpOxmOfbField_EthSrc
145 src := loxiSetField.Field.GetOXMValue().(net.HardwareAddr)
146 ethSrc.EthSrc = src
147 ofpOxmOfbField.Value = &ethSrc
148 case "eth_dst":
149 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_ETH_DST
150 var ethDst openflow_13.OfpOxmOfbField_EthDst
151 dst := loxiSetField.Field.GetOXMValue().(net.HardwareAddr)
152 ethDst.EthDst = dst
153 ofpOxmOfbField.Value = &ethDst
Don Newtone0d34a82019-11-14 10:58:06 -0500154 }
155 ofpOxmField_OfbField.OfbField = &ofpOxmOfbField
156 ofpOxmField.Field = &ofpOxmField_OfbField
157 ofpActionSetField.Field = &ofpOxmField
158 ofpAction_SetField.SetField = &ofpActionSetField
159 ofpAction.Action = &ofpAction_SetField
Don Newtonb437c6f2019-12-18 11:51:57 -0500160
Don Newtone0d34a82019-11-14 10:58:06 -0500161 case ofp.OFPATPushPbb: //PushPbb
162 case ofp.OFPATPopPbb: //PopPbb
163 case ofp.OFPATExperimenter: //Experimenter
164
165 }
166 return &ofpAction
167
168}