Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 1 | /* |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 2 | Copyright 2020 the original author or authors. |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 3 | |
| 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 | package openflow |
| 17 | |
| 18 | import ( |
| 19 | "encoding/json" |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 20 | "github.com/donNewtonAlpha/goloxi" |
| 21 | ofp "github.com/donNewtonAlpha/goloxi/of13" |
David K. Bainbridge | aea73cd | 2020-01-27 10:44:50 -0800 | [diff] [blame] | 22 | "github.com/opencord/voltha-lib-go/v3/pkg/log" |
| 23 | "github.com/opencord/voltha-protos/v3/go/openflow_13" |
| 24 | "github.com/opencord/voltha-protos/v3/go/voltha" |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 25 | ) |
| 26 | |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 27 | func parseOxm(ofbField *openflow_13.OfpOxmOfbField, DeviceID string) (goloxi.IOxm, uint16) { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 28 | if logger.V(log.DebugLevel) { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 29 | js, _ := json.Marshal(ofbField) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 30 | logger.Debugw("parseOxm called", |
| 31 | log.Fields{ |
| 32 | "device-id": DeviceID, |
| 33 | "ofbField": js}) |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 34 | } |
| 35 | |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 36 | switch ofbField.Type { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 37 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_IN_PORT: |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 38 | ofpInPort := ofp.NewOxmInPort() |
| 39 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_Port) |
| 40 | ofpInPort.Value = ofp.Port(val.Port) |
| 41 | return ofpInPort, 4 |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 42 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_ETH_TYPE: |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 43 | ofpEthType := ofp.NewOxmEthType() |
| 44 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_EthType) |
| 45 | ofpEthType.Value = ofp.EthernetType(val.EthType) |
| 46 | return ofpEthType, 2 |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 47 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_IN_PHY_PORT: |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 48 | ofpInPhyPort := ofp.NewOxmInPhyPort() |
| 49 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_PhysicalPort) |
| 50 | ofpInPhyPort.Value = ofp.Port(val.PhysicalPort) |
| 51 | return ofpInPhyPort, 4 |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 52 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_IP_PROTO: |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 53 | ofpIpProto := ofp.NewOxmIpProto() |
| 54 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_IpProto) |
| 55 | ofpIpProto.Value = ofp.IpPrototype(val.IpProto) |
| 56 | return ofpIpProto, 1 |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 57 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_UDP_SRC: |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 58 | ofpUdpSrc := ofp.NewOxmUdpSrc() |
| 59 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_UdpSrc) |
| 60 | ofpUdpSrc.Value = uint16(val.UdpSrc) |
| 61 | return ofpUdpSrc, 2 |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 62 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_UDP_DST: |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 63 | ofpUdpDst := ofp.NewOxmUdpDst() |
| 64 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_UdpDst) |
| 65 | ofpUdpDst.Value = uint16(val.UdpDst) |
| 66 | return ofpUdpDst, 2 |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 67 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID: |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 68 | ofpVlanVid := ofp.NewOxmVlanVid() |
| 69 | val := ofbField.GetValue() |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame^] | 70 | if val == nil { |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 71 | ofpVlanVid.Value = uint16(0) |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame^] | 72 | return ofpVlanVid, 2 |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 73 | } |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame^] | 74 | vlanId := val.(*openflow_13.OfpOxmOfbField_VlanVid) |
| 75 | if ofbField.HasMask { |
| 76 | ofpVlanVidMasked := ofp.NewOxmVlanVidMasked() |
| 77 | valMask := ofbField.GetMask() |
| 78 | vlanMask := valMask.(*openflow_13.OfpOxmOfbField_VlanVidMask) |
| 79 | if vlanId.VlanVid == 4096 && vlanMask.VlanVidMask == 4096 { |
| 80 | ofpVlanVidMasked.Value = uint16(vlanId.VlanVid) |
| 81 | ofpVlanVidMasked.ValueMask = uint16(vlanMask.VlanVidMask) |
| 82 | } else { |
| 83 | ofpVlanVidMasked.Value = uint16(vlanId.VlanVid) | 0x1000 |
| 84 | ofpVlanVidMasked.ValueMask = uint16(vlanMask.VlanVidMask) |
| 85 | |
| 86 | } |
| 87 | return ofpVlanVidMasked, 4 |
| 88 | } |
| 89 | ofpVlanVid.Value = uint16(vlanId.VlanVid) | 0x1000 |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 90 | return ofpVlanVid, 2 |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 91 | case voltha.OxmOfbFieldTypes_OFPXMT_OFB_METADATA: |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 92 | ofpMetadata := ofp.NewOxmMetadata() |
| 93 | val := ofbField.GetValue().(*openflow_13.OfpOxmOfbField_TableMetadata) |
| 94 | ofpMetadata.Value = val.TableMetadata |
| 95 | return ofpMetadata, 8 |
| 96 | default: |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 97 | if logger.V(log.WarnLevel) { |
| 98 | js, _ := json.Marshal(ofbField) |
| 99 | logger.Warnw("ParseOXM Unhandled OxmField", |
| 100 | log.Fields{ |
| 101 | "device-id": DeviceID, |
| 102 | "OfbField": js}) |
| 103 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 104 | } |
| 105 | return nil, 0 |
| 106 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 107 | |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 108 | func parseInstructions(ofpInstruction *openflow_13.OfpInstruction, DeviceID string) (ofp.IInstruction, uint16) { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 109 | if logger.V(log.DebugLevel) { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 110 | js, _ := json.Marshal(ofpInstruction) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 111 | logger.Debugw("parseInstructions called", |
| 112 | log.Fields{ |
| 113 | "device-id": DeviceID, |
| 114 | "Instruction": js}) |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 115 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 116 | instType := ofpInstruction.Type |
| 117 | data := ofpInstruction.GetData() |
| 118 | switch instType { |
| 119 | case ofp.OFPITWriteMetadata: |
| 120 | instruction := ofp.NewInstructionWriteMetadata() |
| 121 | instruction.Len = 24 |
| 122 | metadata := data.(*openflow_13.OfpInstruction_WriteMetadata).WriteMetadata |
| 123 | instruction.Metadata = uint64(metadata.Metadata) |
| 124 | return instruction, 24 |
| 125 | case ofp.OFPITMeter: |
| 126 | instruction := ofp.NewInstructionMeter() |
| 127 | instruction.Len = 8 |
| 128 | meter := data.(*openflow_13.OfpInstruction_Meter).Meter |
| 129 | instruction.MeterId = meter.MeterId |
| 130 | return instruction, 8 |
| 131 | case ofp.OFPITGotoTable: |
| 132 | instruction := ofp.NewInstructionGotoTable() |
| 133 | instruction.Len = 8 |
| 134 | gotoTable := data.(*openflow_13.OfpInstruction_GotoTable).GotoTable |
| 135 | instruction.TableId = uint8(gotoTable.TableId) |
| 136 | return instruction, 8 |
| 137 | case ofp.OFPITApplyActions: |
| 138 | instruction := ofp.NewInstructionApplyActions() |
| 139 | var instructionSize uint16 |
| 140 | instructionSize = 8 |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 141 | //ofpActions := ofpInstruction.GetActions().Actions |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 142 | var actions []goloxi.IAction |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 143 | for _, ofpAction := range ofpInstruction.GetActions().Actions { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 144 | action, actionSize := parseAction(ofpAction, DeviceID) |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 145 | actions = append(actions, action) |
| 146 | instructionSize += actionSize |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 147 | |
| 148 | } |
| 149 | instruction.Actions = actions |
| 150 | instruction.SetLen(instructionSize) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 151 | if logger.V(log.DebugLevel) { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 152 | js, _ := json.Marshal(instruction) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 153 | logger.Debugw("parseInstructions returning", |
| 154 | log.Fields{ |
| 155 | "device-id": DeviceID, |
| 156 | "size": instructionSize, |
| 157 | "parsed-instruction": js}) |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 158 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 159 | return instruction, instructionSize |
| 160 | } |
| 161 | //shouldn't have reached here :< |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 162 | return nil, 0 |
| 163 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 164 | |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 165 | func parseAction(ofpAction *openflow_13.OfpAction, DeviceID string) (goloxi.IAction, uint16) { |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 166 | if logger.V(log.DebugLevel) { |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 167 | js, _ := json.Marshal(ofpAction) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 168 | logger.Debugw("parseAction called", |
| 169 | log.Fields{ |
| 170 | "device-id": DeviceID, |
| 171 | "action": js}) |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 172 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 173 | switch ofpAction.Type { |
| 174 | case openflow_13.OfpActionType_OFPAT_OUTPUT: |
| 175 | ofpOutputAction := ofpAction.GetOutput() |
| 176 | outputAction := ofp.NewActionOutput() |
| 177 | outputAction.Port = ofp.Port(ofpOutputAction.Port) |
| 178 | outputAction.MaxLen = uint16(ofpOutputAction.MaxLen) |
| 179 | outputAction.Len = 16 |
| 180 | return outputAction, 16 |
| 181 | case openflow_13.OfpActionType_OFPAT_PUSH_VLAN: |
| 182 | ofpPushVlanAction := ofp.NewActionPushVlan() |
| 183 | ofpPushVlanAction.Ethertype = uint16(ofpAction.GetPush().Ethertype) |
| 184 | ofpPushVlanAction.Len = 8 |
| 185 | return ofpPushVlanAction, 8 |
| 186 | case openflow_13.OfpActionType_OFPAT_POP_VLAN: |
| 187 | ofpPopVlanAction := ofp.NewActionPopVlan() |
| 188 | ofpPopVlanAction.Len = 8 |
| 189 | return ofpPopVlanAction, 8 |
| 190 | case openflow_13.OfpActionType_OFPAT_SET_FIELD: |
| 191 | ofpActionSetField := ofpAction.GetSetField() |
| 192 | setFieldAction := ofp.NewActionSetField() |
| 193 | |
Don Newton | 7577f07 | 2020-01-06 12:41:11 -0500 | [diff] [blame] | 194 | iOxm, _ := parseOxm(ofpActionSetField.GetField().GetOfbField(), DeviceID) |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 195 | setFieldAction.Field = iOxm |
Don Newton | b437c6f | 2019-12-18 11:51:57 -0500 | [diff] [blame] | 196 | setFieldAction.Len = 16 |
| 197 | return setFieldAction, 16 |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 198 | default: |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 199 | if logger.V(log.WarnLevel) { |
| 200 | js, _ := json.Marshal(ofpAction) |
| 201 | logger.Warnw("parseAction unknow action", |
| 202 | log.Fields{ |
| 203 | "device-id": DeviceID, |
| 204 | "action": js}) |
| 205 | } |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 206 | } |
| 207 | return nil, 0 |
| 208 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 209 | |
| 210 | func parsePortStats(port *voltha.LogicalPort) *ofp.PortStatsEntry { |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 211 | stats := port.OfpPortStats |
| 212 | port.OfpPort.GetPortNo() |
| 213 | var entry ofp.PortStatsEntry |
| 214 | entry.SetPortNo(ofp.Port(port.OfpPort.GetPortNo())) |
| 215 | entry.SetRxPackets(stats.GetRxPackets()) |
| 216 | entry.SetTxPackets(stats.GetTxPackets()) |
| 217 | entry.SetRxBytes(stats.GetRxBytes()) |
| 218 | entry.SetTxBytes(stats.GetTxBytes()) |
| 219 | entry.SetRxDropped(stats.GetRxDropped()) |
| 220 | entry.SetTxDropped(stats.GetTxDropped()) |
| 221 | entry.SetRxErrors(stats.GetRxErrors()) |
| 222 | entry.SetTxErrors(stats.GetTxErrors()) |
| 223 | entry.SetRxFrameErr(stats.GetRxFrameErr()) |
| 224 | entry.SetRxOverErr(stats.GetRxOverErr()) |
| 225 | entry.SetRxCrcErr(stats.GetRxCrcErr()) |
| 226 | entry.SetCollisions(stats.GetCollisions()) |
| 227 | entry.SetDurationSec(stats.GetDurationSec()) |
| 228 | entry.SetDurationNsec(stats.GetDurationNsec()) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 229 | return &entry |
Don Newton | e0d34a8 | 2019-11-14 10:58:06 -0500 | [diff] [blame] | 230 | } |