blob: c07ac5948b69a94ad2fabbd019a58bb53bf43854 [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())
54 /*
55 var maxLen uint16
56 maxLen = loxiOutputAction.GetMaxLen()
57 output.MaxLen = uint32(maxLen)
58
59 */
60/*
61 output.MaxLen = 0
62 outputAction.Output = &output
63 ofpAction.Action = &outputAction
64 ofpAction.Type = openflow_13.OfpActionType_OFPAT_OUTPUT
65 case ofp.OFPATCopyTtlOut: //CopyTtltOut
66 case ofp.OFPATCopyTtlIn: //CopyTtlIn
67 case ofp.OFPATSetMplsTtl: //SetMplsTtl
68 case ofp.OFPATDecMplsTtl: //DecMplsTtl
69 case ofp.OFPATPushVLAN: //PushVlan
70 var pushVlan openflow_13.OfpAction_Push
71 loxiPushAction := action.(*ofp.ActionPushVlan)
72 var push openflow_13.OfpActionPush
73 push.Ethertype = uint32(loxiPushAction.Ethertype) //TODO This should be available in the fields
74 pushVlan.Push = &push
75 ofpAction.Type = openflow_13.OfpActionType_OFPAT_PUSH_VLAN
76 ofpAction.Action = &pushVlan
77 case ofp.OFPATPopVLAN: //PopVlan
78 ofpAction.Type = openflow_13.OfpActionType_OFPAT_POP_VLAN
79 case ofp.OFPATPushMpls: //PushMpls
80 case ofp.OFPATPopMpls: //PopMpls
81 case ofp.OFPATSetQueue: //SetQueue
82 case ofp.OFPATGroup: //ActionGroup
83 case ofp.OFPATSetNwTtl: //SetNwTtl
84 case ofp.OFPATDecNwTtl: //DecNwTtl
85 case ofp.OFPATSetField: //SetField
86 ofpAction.Type = openflow_13.OfpActionType_OFPAT_SET_FIELD
87 var ofpActionForSetField openflow_13.OfpAction_SetField
88 var ofpActionSetField openflow_13.OfpActionSetField
89 var ofpOxmField openflow_13.OfpOxmField
90 ofpOxmField.OxmClass = openflow_13.OfpOxmClass_OFPXMC_OPENFLOW_BASIC
91 var ofpOxmFieldForOfbField openflow_13.OfpOxmField_OfbField
92 var ofpOxmOfbField openflow_13.OfpOxmOfbField
93 loxiSetField := action.(*ofp.ActionSetField)
94 oxmName := loxiSetField.Field.GetOXMName()
95 switch oxmName {
96 //TODO handle set field sith other fields
97 case "vlan_vid":
98 ofpOxmOfbField.Type = openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID
99 var vlanVid openflow_13.OfpOxmOfbField_VlanVid
100 var VlanVid = loxiSetField.Field.GetOXMValue().(uint16)
101 vlanVid.VlanVid = uint32(VlanVid)
102
103 ofpOxmOfbField.Value = &vlanVid
104 }
105 ofpOxmFieldForOfbField.OfbField = &ofpOxmOfbField
106 ofpOxmField.Field = &ofpOxmFieldForOfbField
107 ofpActionSetField.Field = &ofpOxmField
108 ofpActionForSetField.SetField = &ofpActionSetField
109 ofpAction.Action = &ofpActionForSetField
110
111 case ofp.OFPATPushPbb: //PushPbb
112 case ofp.OFPATPopPbb: //PopPbb
113 case ofp.OFPATExperimenter: //Experimenter
114
115 }
116 return &ofpAction
117
118}
119
120// parseOxm for parsing OxmOfb field
121func parseOxm(ofbField *openflow_13.OfpOxmOfbField, DeviceID string) (goloxi.IOxm, uint16) {
122 switch ofbField.Type {
123 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_IN_PORT:
124 ofpInPort := ofp.NewOxmInPort()
125 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_Port)
126 ofpInPort.Value = ofp.Port(val.Port)
127 return ofpInPort, 4
128 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_ETH_TYPE:
129 ofpEthType := ofp.NewOxmEthType()
130 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_EthType)
131 ofpEthType.Value = ofp.EthernetType(val.EthType)
132 return ofpEthType, 2
133 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_IN_PHY_PORT:
134 ofpInPhyPort := ofp.NewOxmInPhyPort()
135 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_PhysicalPort)
136 ofpInPhyPort.Value = ofp.Port(val.PhysicalPort)
137 return ofpInPhyPort, 4
138 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_IP_PROTO:
139 ofpIPProto := ofp.NewOxmIpProto()
140 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_IpProto)
141 ofpIPProto.Value = ofp.IpPrototype(val.IpProto)
142 return ofpIPProto, 1
143 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_UDP_SRC:
144 ofpUDPSrc := ofp.NewOxmUdpSrc()
145 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_UdpSrc)
146 ofpUDPSrc.Value = uint16(val.UdpSrc)
147 return ofpUDPSrc, 2
148 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_UDP_DST:
149 ofpUDPDst := ofp.NewOxmUdpDst()
150 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_UdpDst)
151 ofpUDPDst.Value = uint16(val.UdpDst)
152 return ofpUDPDst, 2
153 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID:
154 ofpVlanVid := ofp.NewOxmVlanVid()
155 val := ofbField.GetValue()
156 if val == nil {
157 ofpVlanVid.Value = uint16(0)
158 return ofpVlanVid, 2
159 }
160 vlanID := val.(*openflow_13.OfpOxmOfbField_VlanVid)
161 if ofbField.HasMask {
162 ofpVlanVidMasked := ofp.NewOxmVlanVidMasked()
163 valMask := ofbField.GetMask()
164 vlanMask := valMask.(*openflow_13.OfpOxmOfbField_VlanVidMask)
165 if vlanID.VlanVid == 4096 && vlanMask.VlanVidMask == 4096 {
166 ofpVlanVidMasked.Value = uint16(vlanID.VlanVid)
167 ofpVlanVidMasked.ValueMask = uint16(vlanMask.VlanVidMask)
168 } else {
169 ofpVlanVidMasked.Value = uint16(vlanID.VlanVid) | 0x1000
170 ofpVlanVidMasked.ValueMask = uint16(vlanMask.VlanVidMask)
171
172 }
173 return ofpVlanVidMasked, 4
174 }
175 ofpVlanVid.Value = uint16(vlanID.VlanVid) | 0x1000
176 return ofpVlanVid, 2
177 case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_METADATA:
178 ofpMetadata := ofp.NewOxmMetadata()
179 val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_TableMetadata)
180 ofpMetadata.Value = val.TableMetadata
181 return ofpMetadata, 8
182 default:
183 }
184 return nil, 0
185}
186
187// parseInstructions for parsing of instructions
188func parseInstructions(ofpInstruction *openflow_13.OfpInstruction, DeviceID string) (ofp.IInstruction, uint16) {
189 instType := ofpInstruction.Type
190 data := ofpInstruction.GetData()
191 switch instType {
192 case ofp.OFPITWriteMetadata:
193 instruction := ofp.NewInstructionWriteMetadata()
194 instruction.Len = 24
195 metadata := data.(*openflow_13.OfpInstruction_WriteMetadata).WriteMetadata
196 instruction.Metadata = uint64(metadata.Metadata)
197 return instruction, 24
198 case ofp.OFPITMeter:
199 instruction := ofp.NewInstructionMeter()
200 instruction.Len = 8
201 meter := data.(*openflow_13.OfpInstruction_Meter).Meter
202 instruction.MeterId = meter.MeterId
203 return instruction, 8
204 case ofp.OFPITGotoTable:
205 instruction := ofp.NewInstructionGotoTable()
206 instruction.Len = 8
207 gotoTable := data.(*openflow_13.OfpInstruction_GotoTable).GotoTable
208 instruction.TableId = uint8(gotoTable.TableId)
209 return instruction, 8
210 case ofp.OFPITApplyActions:
211 instruction := ofp.NewInstructionApplyActions()
212 var instructionSize uint16
213 instructionSize = 8
214 //ofpActions := ofpInstruction.GetActions().Actions
215 var actions []goloxi.IAction
216 for _, ofpAction := range ofpInstruction.GetActions().Actions {
217 action, actionSize := parseAction(ofpAction, DeviceID)
218 actions = append(actions, action)
219 instructionSize += actionSize
220
221 }
222 instruction.Actions = actions
223 instruction.SetLen(instructionSize)
224 return instruction, instructionSize
225 }
226 //shouldn't have reached here :<
227 return nil, 0
228}
229
230// parseAction for parsing of actions
231func parseAction(ofpAction *openflow_13.OfpAction, DeviceID string) (goloxi.IAction, uint16) {
232 switch ofpAction.Type {
233 case openflow_13.OfpActionType_OFPAT_OUTPUT:
234 ofpOutputAction := ofpAction.GetOutput()
235 outputAction := ofp.NewActionOutput()
236 outputAction.Port = ofp.Port(ofpOutputAction.Port)
237 outputAction.MaxLen = uint16(ofpOutputAction.MaxLen)
238 outputAction.Len = 16
239 return outputAction, 16
240 case openflow_13.OfpActionType_OFPAT_PUSH_VLAN:
241 ofpPushVlanAction := ofp.NewActionPushVlan()
242 ofpPushVlanAction.Ethertype = uint16(ofpAction.GetPush().Ethertype)
243 ofpPushVlanAction.Len = 8
244 return ofpPushVlanAction, 8
245 case openflow_13.OfpActionType_OFPAT_POP_VLAN:
246 ofpPopVlanAction := ofp.NewActionPopVlan()
247 ofpPopVlanAction.Len = 8
248 return ofpPopVlanAction, 8
249 case openflow_13.OfpActionType_OFPAT_SET_FIELD:
250 ofpActionSetField := ofpAction.GetSetField()
251 setFieldAction := ofp.NewActionSetField()
252
253 iOxm, _ := parseOxm(ofpActionSetField.GetField().GetOfbField(), DeviceID)
254 setFieldAction.Field = iOxm
255 setFieldAction.Len = 16
256 return setFieldAction, 16
257 default:
258 }
259 return nil, 0
260}
261
262// parsePortStats for parsing of port stats
263func parsePortStats(port *voltha.LogicalPort) *ofp.PortStatsEntry {
264 stats := port.OfpPortStats
265 port.OfpPort.GetPortNo()
266 var entry ofp.PortStatsEntry
267 entry.SetPortNo(ofp.Port(port.OfpPort.GetPortNo()))
268 entry.SetRxPackets(stats.GetRxPackets())
269 entry.SetTxPackets(stats.GetTxPackets())
270 entry.SetRxBytes(stats.GetRxBytes())
271 entry.SetTxBytes(stats.GetTxBytes())
272 entry.SetRxDropped(stats.GetRxDropped())
273 entry.SetTxDropped(stats.GetTxDropped())
274 entry.SetRxErrors(stats.GetRxErrors())
275 entry.SetTxErrors(stats.GetTxErrors())
276 entry.SetRxFrameErr(stats.GetRxFrameErr())
277 entry.SetRxOverErr(stats.GetRxOverErr())
278 entry.SetRxCrcErr(stats.GetRxCrcErr())
279 entry.SetCollisions(stats.GetCollisions())
280 entry.SetDurationSec(stats.GetDurationSec())
281 entry.SetDurationNsec(stats.GetDurationNsec())
282 return &entry
283}*/