blob: 179730e547210300d564fc6b1add7f4c409997b8 [file] [log] [blame]
Naveen Sampath04696f72022-06-13 15:19:14 +05301/*
2* Copyright 2022-present Open Networking Foundation
3* Licensed under the Apache License, Version 2.0 (the "License");
4* you may not use this file except in compliance with the License.
5* You may obtain a copy of the License at
6*
7* http://www.apache.org/licenses/LICENSE-2.0
8*
9* Unless required by applicable law or agreed to in writing, software
10* distributed under the License is distributed on an "AS IS" BASIS,
11* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12* See the License for the specific language governing permissions and
13* limitations under the License.
14 */
15
16package controller
17
18import (
19 "fmt"
20 "strings"
21 "sync"
22)
23
24var mu sync.Mutex
25var xid uint32 = 1
26
27// GetXid to get xid
28func GetXid() uint32 {
29 mu.Lock()
30 defer mu.Unlock()
31 xid++
32 return xid
33}
34
35// PadString for padding of string
36func PadString(value string, padSize int) string {
37 size := len(value)
38 nullsNeeded := padSize - size
39 null := fmt.Sprintf("%c", '\000')
40 padded := strings.Repeat(null, nullsNeeded)
41 return fmt.Sprintf("%s%s", value, padded)
42}
43
44/*
45// extractAction for extract action
46func extractAction(action ofp.IAction) *openflow_13.OfpAction {
47 var ofpAction openflow_13.OfpAction
48 switch action.GetType() {
49 case ofp.OFPATOutput:
50 var outputAction openflow_13.OfpAction_Output
51 loxiOutputAction := action.(*ofp.ActionOutput)
52 var output openflow_13.OfpActionOutput
53 output.Port = uint32(loxiOutputAction.GetPort())
Naveen Sampath04696f72022-06-13 15:19:14 +053054 var maxLen uint16
55 maxLen = loxiOutputAction.GetMaxLen()
56 output.MaxLen = uint32(maxLen)
Naveen Sampath04696f72022-06-13 15:19:14 +053057 output.MaxLen = 0
58 outputAction.Output = &output
59 ofpAction.Action = &outputAction
60 ofpAction.Type = openflow_13.OfpActionType_OFPAT_OUTPUT
61 case ofp.OFPATCopyTtlOut: //CopyTtltOut
62 case ofp.OFPATCopyTtlIn: //CopyTtlIn
63 case ofp.OFPATSetMplsTtl: //SetMplsTtl
64 case ofp.OFPATDecMplsTtl: //DecMplsTtl
65 case ofp.OFPATPushVLAN: //PushVlan
66 var pushVlan openflow_13.OfpAction_Push
67 loxiPushAction := action.(*ofp.ActionPushVlan)
68 var push openflow_13.OfpActionPush
69 push.Ethertype = uint32(loxiPushAction.Ethertype) //TODO This should be available in the fields
70 pushVlan.Push = &push
71 ofpAction.Type = openflow_13.OfpActionType_OFPAT_PUSH_VLAN
72 ofpAction.Action = &pushVlan
73 case ofp.OFPATPopVLAN: //PopVlan
74 ofpAction.Type = openflow_13.OfpActionType_OFPAT_POP_VLAN
75 case ofp.OFPATPushMpls: //PushMpls
76 case ofp.OFPATPopMpls: //PopMpls
77 case ofp.OFPATSetQueue: //SetQueue
78 case ofp.OFPATGroup: //ActionGroup
79 case ofp.OFPATSetNwTtl: //SetNwTtl
80 case ofp.OFPATDecNwTtl: //DecNwTtl
81 case ofp.OFPATSetField: //SetField
82 ofpAction.Type = openflow_13.OfpActionType_OFPAT_SET_FIELD
83 var ofpActionForSetField openflow_13.OfpAction_SetField
84 var ofpActionSetField openflow_13.OfpActionSetField
85 var ofpOxmField openflow_13.OfpOxmField
86 ofpOxmField.OxmClass = openflow_13.OfpOxmClass_OFPXMC_OPENFLOW_BASIC
87 var ofpOxmFieldForOfbField openflow_13.OfpOxmField_OfbField
88 var ofpOxmOfbField openflow_13.OfpOxmOfbField
89 loxiSetField := action.(*ofp.ActionSetField)
90 oxmName := loxiSetField.Field.GetOXMName()
91 switch oxmName {
92 //TODO handle set field sith other fields
93 case "vlan_vid":
94 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID
95 var vlanVid openflow_13.OfpOxmOfbField_VlanVid
96 var VlanVid = loxiSetField.Field.GetOXMValue().(uint16)
97 vlanVid.VlanVid = uint32(VlanVid)
98
99 ofpOxmOfbField.Value = &vlanVid
100 }
101 ofpOxmFieldForOfbField.OfbField = &ofpOxmOfbField
102 ofpOxmField.Field = &ofpOxmFieldForOfbField
103 ofpActionSetField.Field = &ofpOxmField
104 ofpActionForSetField.SetField = &ofpActionSetField
105 ofpAction.Action = &ofpActionForSetField
106
107 case ofp.OFPATPushPbb: //PushPbb
108 case ofp.OFPATPopPbb: //PopPbb
109 case ofp.OFPATExperimenter: //Experimenter
110
111 }
112 return &ofpAction
113
114}
115
116// parseOxm for parsing OxmOfb field
117func parseOxm(ofbField *openflow_13.OfpOxmOfbField, DeviceID string) (goloxi.IOxm, uint16) {
118 switch ofbField.Type {
119 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_IN_PORT:
120 ofpInPort := ofp.NewOxmInPort()
121 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_Port)
122 ofpInPort.Value = ofp.Port(val.Port)
123 return ofpInPort, 4
124 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_ETH_TYPE:
125 ofpEthType := ofp.NewOxmEthType()
126 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_EthType)
127 ofpEthType.Value = ofp.EthernetType(val.EthType)
128 return ofpEthType, 2
129 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_IN_PHY_PORT:
130 ofpInPhyPort := ofp.NewOxmInPhyPort()
131 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_PhysicalPort)
132 ofpInPhyPort.Value = ofp.Port(val.PhysicalPort)
133 return ofpInPhyPort, 4
134 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_IP_PROTO:
135 ofpIPProto := ofp.NewOxmIpProto()
136 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_IpProto)
137 ofpIPProto.Value = ofp.IpPrototype(val.IpProto)
138 return ofpIPProto, 1
139 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_UDP_SRC:
140 ofpUDPSrc := ofp.NewOxmUdpSrc()
141 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_UdpSrc)
142 ofpUDPSrc.Value = uint16(val.UdpSrc)
143 return ofpUDPSrc, 2
144 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_UDP_DST:
145 ofpUDPDst := ofp.NewOxmUdpDst()
146 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_UdpDst)
147 ofpUDPDst.Value = uint16(val.UdpDst)
148 return ofpUDPDst, 2
149 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID:
150 ofpVlanVid := ofp.NewOxmVlanVid()
151 val := ofbField.GetValue()
152 if val == nil {
153 ofpVlanVid.Value = uint16(0)
154 return ofpVlanVid, 2
155 }
156 vlanID := val.(*openflow_13.OfpOxmOfbField_VlanVid)
157 if ofbField.HasMask {
158 ofpVlanVidMasked := ofp.NewOxmVlanVidMasked()
159 valMask := ofbField.GetMask()
160 vlanMask := valMask.(*openflow_13.OfpOxmOfbField_VlanVidMask)
161 if vlanID.VlanVid == 4096 && vlanMask.VlanVidMask == 4096 {
162 ofpVlanVidMasked.Value = uint16(vlanID.VlanVid)
163 ofpVlanVidMasked.ValueMask = uint16(vlanMask.VlanVidMask)
164 } else {
165 ofpVlanVidMasked.Value = uint16(vlanID.VlanVid) | 0x1000
166 ofpVlanVidMasked.ValueMask = uint16(vlanMask.VlanVidMask)
167
168 }
169 return ofpVlanVidMasked, 4
170 }
171 ofpVlanVid.Value = uint16(vlanID.VlanVid) | 0x1000
172 return ofpVlanVid, 2
173 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_METADATA:
174 ofpMetadata := ofp.NewOxmMetadata()
175 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_TableMetadata)
176 ofpMetadata.Value = val.TableMetadata
177 return ofpMetadata, 8
178 default:
179 }
180 return nil, 0
181}
182
183// parseInstructions for parsing of instructions
184func parseInstructions(ofpInstruction *openflow_13.OfpInstruction, DeviceID string) (ofp.IInstruction, uint16) {
185 instType := ofpInstruction.Type
186 data := ofpInstruction.GetData()
187 switch instType {
188 case ofp.OFPITWriteMetadata:
189 instruction := ofp.NewInstructionWriteMetadata()
190 instruction.Len = 24
191 metadata := data.(*openflow_13.OfpInstruction_WriteMetadata).WriteMetadata
192 instruction.Metadata = uint64(metadata.Metadata)
193 return instruction, 24
194 case ofp.OFPITMeter:
195 instruction := ofp.NewInstructionMeter()
196 instruction.Len = 8
197 meter := data.(*openflow_13.OfpInstruction_Meter).Meter
198 instruction.MeterId = meter.MeterId
199 return instruction, 8
200 case ofp.OFPITGotoTable:
201 instruction := ofp.NewInstructionGotoTable()
202 instruction.Len = 8
203 gotoTable := data.(*openflow_13.OfpInstruction_GotoTable).GotoTable
204 instruction.TableId = uint8(gotoTable.TableId)
205 return instruction, 8
206 case ofp.OFPITApplyActions:
207 instruction := ofp.NewInstructionApplyActions()
208 var instructionSize uint16
209 instructionSize = 8
210 //ofpActions := ofpInstruction.GetActions().Actions
211 var actions []goloxi.IAction
212 for _, ofpAction := range ofpInstruction.GetActions().Actions {
213 action, actionSize := parseAction(ofpAction, DeviceID)
214 actions = append(actions, action)
215 instructionSize += actionSize
216
217 }
218 instruction.Actions = actions
219 instruction.SetLen(instructionSize)
220 return instruction, instructionSize
221 }
222 //shouldn't have reached here :<
223 return nil, 0
224}
225
226// parseAction for parsing of actions
227func parseAction(ofpAction *openflow_13.OfpAction, DeviceID string) (goloxi.IAction, uint16) {
228 switch ofpAction.Type {
229 case openflow_13.OfpActionType_OFPAT_OUTPUT:
230 ofpOutputAction := ofpAction.GetOutput()
231 outputAction := ofp.NewActionOutput()
232 outputAction.Port = ofp.Port(ofpOutputAction.Port)
233 outputAction.MaxLen = uint16(ofpOutputAction.MaxLen)
234 outputAction.Len = 16
235 return outputAction, 16
236 case openflow_13.OfpActionType_OFPAT_PUSH_VLAN:
237 ofpPushVlanAction := ofp.NewActionPushVlan()
238 ofpPushVlanAction.Ethertype = uint16(ofpAction.GetPush().Ethertype)
239 ofpPushVlanAction.Len = 8
240 return ofpPushVlanAction, 8
241 case openflow_13.OfpActionType_OFPAT_POP_VLAN:
242 ofpPopVlanAction := ofp.NewActionPopVlan()
243 ofpPopVlanAction.Len = 8
244 return ofpPopVlanAction, 8
245 case openflow_13.OfpActionType_OFPAT_SET_FIELD:
246 ofpActionSetField := ofpAction.GetSetField()
247 setFieldAction := ofp.NewActionSetField()
248
249 iOxm, _ := parseOxm(ofpActionSetField.GetField().GetOfbField(), DeviceID)
250 setFieldAction.Field = iOxm
251 setFieldAction.Len = 16
252 return setFieldAction, 16
253 default:
254 }
255 return nil, 0
256}
257
258// parsePortStats for parsing of port stats
259func parsePortStats(port *voltha.LogicalPort) *ofp.PortStatsEntry {
260 stats := port.OfpPortStats
261 port.OfpPort.GetPortNo()
262 var entry ofp.PortStatsEntry
263 entry.SetPortNo(ofp.Port(port.OfpPort.GetPortNo()))
264 entry.SetRxPackets(stats.GetRxPackets())
265 entry.SetTxPackets(stats.GetTxPackets())
266 entry.SetRxBytes(stats.GetRxBytes())
267 entry.SetTxBytes(stats.GetTxBytes())
268 entry.SetRxDropped(stats.GetRxDropped())
269 entry.SetTxDropped(stats.GetTxDropped())
270 entry.SetRxErrors(stats.GetRxErrors())
271 entry.SetTxErrors(stats.GetTxErrors())
272 entry.SetRxFrameErr(stats.GetRxFrameErr())
273 entry.SetRxOverErr(stats.GetRxOverErr())
274 entry.SetRxCrcErr(stats.GetRxCrcErr())
275 entry.SetCollisions(stats.GetCollisions())
276 entry.SetDurationSec(stats.GetDurationSec())
277 entry.SetDurationNsec(stats.GetDurationNsec())
278 return &entry
279}*/