David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2020 the original author or authors. |
| 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 | |
| 17 | package openflow |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Andrea Campanella | 3034262 | 2020-03-05 20:48:26 +0100 | [diff] [blame] | 21 | "encoding/binary" |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 22 | "net" |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 23 | |
| 24 | ofp "github.com/opencord/goloxi/of13" |
| 25 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 26 | "github.com/opencord/voltha-protos/v5/go/openflow_13" |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 27 | ) |
| 28 | |
| 29 | var oxmMap = map[string]int32{ |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame] | 30 | "in_port": 0, |
| 31 | "in_phy_port": 1, |
| 32 | "metadata": 2, |
| 33 | "eth_dst": 3, |
| 34 | "eth_src": 4, |
| 35 | "eth_type": 5, |
| 36 | "vlan_vid": 6, |
| 37 | "vlan_pcp": 7, |
| 38 | "ip_dscp": 8, |
| 39 | "ip_ecn": 9, |
| 40 | "ip_proto": 10, |
| 41 | "ipv4_src": 11, |
| 42 | "ipv4_dst": 12, |
| 43 | "tcp_src": 13, |
| 44 | "tcp_dst": 14, |
| 45 | "udp_src": 15, |
| 46 | "udp_dst": 16, |
| 47 | "sctp_src": 17, |
| 48 | "sctp_dst": 18, |
| 49 | "icmpv4_type": 19, |
| 50 | "icmpv4_code": 20, |
| 51 | "arp_op": 21, |
| 52 | "arp_spa": 22, |
| 53 | "arp_tpa": 23, |
| 54 | "arp_sha": 24, |
| 55 | "arp_tha": 25, |
| 56 | "ipv6_src": 26, |
| 57 | "ipv6_dst": 27, |
| 58 | "ipv6_flabel": 28, |
| 59 | "icmpv6_type": 29, |
| 60 | "icmpv6_code": 30, |
| 61 | "ipv6_nd_target": 31, |
| 62 | "ipv6_nd_sll": 32, |
| 63 | "ipv6_nd_tll": 33, |
| 64 | "mpls_label": 34, |
| 65 | "mpls_tc": 35, |
| 66 | "mpls_bos": 36, |
| 67 | "pbb_isid": 37, |
| 68 | "tunnel_id": 38, |
| 69 | "ipv6_exthdr": 39, |
| 70 | "vlan_vid_masked": 200, //made up |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 73 | func (ofc *OFConnection) handleFlowAdd(ctx context.Context, flowAdd *ofp.FlowAdd) { |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 74 | span, ctx := log.CreateChildSpan(ctx, "openflow-flow-add") |
| 75 | defer span.Finish() |
| 76 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 77 | if logger.V(log.DebugLevel) { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 78 | logger.Debugw(ctx, "handleFlowAdd called", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 79 | log.Fields{ |
| 80 | "device-id": ofc.DeviceID, |
Matteo Scandolo | f3a7d57 | 2021-11-29 15:36:19 -0800 | [diff] [blame] | 81 | "flow": flowAdd}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 82 | } |
| 83 | |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 84 | volthaClient := ofc.VolthaClient.Get() |
| 85 | if volthaClient == nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 86 | logger.Errorw(ctx, "no-voltha-connection", |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 87 | log.Fields{"device-id": ofc.DeviceID}) |
| 88 | return |
| 89 | } |
| 90 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 91 | // Construct the match |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 92 | var oxmList []*openflow_13.OfpOxmField |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 93 | for _, oxmField := range flowAdd.Match.GetOxmList() { |
| 94 | name := oxmMap[oxmField.GetOXMName()] |
| 95 | val := oxmField.GetOXMValue() |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 96 | field := openflow_13.OfpOxmOfbField{Type: openflow_13.OxmOfbFieldTypes(name)} |
| 97 | ofpOxmField := openflow_13.OfpOxmField{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 98 | OxmClass: ofp.OFPXMCOpenflowBasic, |
| 99 | Field: &openflow_13.OfpOxmField_OfbField{OfbField: &field}, |
| 100 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 101 | switch openflow_13.OxmOfbFieldTypes(name) { |
| 102 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_IN_PORT: |
| 103 | field.Value = &openflow_13.OfpOxmOfbField_Port{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 104 | Port: uint32(val.(ofp.Port)), |
| 105 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 106 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_IN_PHY_PORT: |
| 107 | field.Value = &openflow_13.OfpOxmOfbField_PhysicalPort{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 108 | PhysicalPort: val.(uint32), |
| 109 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 110 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_METADATA: |
| 111 | field.Value = &openflow_13.OfpOxmOfbField_TableMetadata{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 112 | TableMetadata: val.(uint64), |
| 113 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 114 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_ETH_TYPE: |
| 115 | field.Value = &openflow_13.OfpOxmOfbField_EthType{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 116 | EthType: uint32(val.(ofp.EthernetType)), |
| 117 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 118 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_IP_PROTO: |
| 119 | field.Value = &openflow_13.OfpOxmOfbField_IpProto{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 120 | IpProto: uint32(val.(ofp.IpPrototype)), |
| 121 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 122 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_IPV4_DST: |
| 123 | field.Value = &openflow_13.OfpOxmOfbField_Ipv4Dst{ |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 124 | Ipv4Dst: binary.BigEndian.Uint32(val.(net.IP)), |
| 125 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 126 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_ETH_DST: |
| 127 | field.Value = &openflow_13.OfpOxmOfbField_EthDst{ |
Gamze Abaka | 3e2b2ce | 2020-05-09 10:21:40 +0000 | [diff] [blame] | 128 | EthDst: val.(net.HardwareAddr), |
| 129 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 130 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_ETH_SRC: |
| 131 | field.Value = &openflow_13.OfpOxmOfbField_EthSrc{ |
ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 132 | EthSrc: val.(net.HardwareAddr), |
| 133 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 134 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_UDP_SRC: |
| 135 | field.Value = &openflow_13.OfpOxmOfbField_UdpSrc{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 136 | UdpSrc: uint32(val.(uint16)), |
| 137 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 138 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_UDP_DST: |
| 139 | field.Value = &openflow_13.OfpOxmOfbField_UdpDst{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 140 | UdpDst: uint32(val.(uint16)), |
| 141 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 142 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID: |
| 143 | field.Value = &openflow_13.OfpOxmOfbField_VlanVid{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 144 | VlanVid: uint32((val.(uint16) & 0xfff) | 0x1000), |
| 145 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 146 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_PCP: |
| 147 | field.Value = &openflow_13.OfpOxmOfbField_VlanPcp{ |
Gamze Abaka | 3e2b2ce | 2020-05-09 10:21:40 +0000 | [diff] [blame] | 148 | VlanPcp: uint32(val.(uint8)), |
| 149 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 150 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_MPLS_LABEL: |
| 151 | field.Value = &openflow_13.OfpOxmOfbField_MplsLabel{ |
ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 152 | MplsLabel: val.(uint32), |
| 153 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 154 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_MPLS_BOS: |
| 155 | field.Value = &openflow_13.OfpOxmOfbField_MplsBos{ |
ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 156 | MplsBos: uint32(val.(uint8)), |
| 157 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 158 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_MPLS_TC: |
| 159 | field.Value = &openflow_13.OfpOxmOfbField_MplsTc{ |
ssiddiqui | df20bf7 | 2021-08-23 14:00:56 +0530 | [diff] [blame] | 160 | MplsTc: val.(uint32), |
| 161 | } |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame] | 162 | case 200: // voltha-protos doesn't actually have a type for vlan_mask |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 163 | field = openflow_13.OfpOxmOfbField{Type: openflow_13.OxmOfbFieldTypes(oxmMap["vlan_vid"])} |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame] | 164 | field.HasMask = true |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 165 | ofpOxmField = openflow_13.OfpOxmField{ |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame] | 166 | OxmClass: ofp.OFPXMCOpenflowBasic, |
| 167 | Field: &openflow_13.OfpOxmField_OfbField{OfbField: &field}, |
| 168 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 169 | field.Value = &openflow_13.OfpOxmOfbField_VlanVid{ |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame] | 170 | VlanVid: uint32(val.(uint16)), |
| 171 | } |
| 172 | vidMask := val.(uint16) |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 173 | field.Mask = &openflow_13.OfpOxmOfbField_VlanVidMask{ |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame] | 174 | VlanVidMask: uint32(vidMask), |
| 175 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 176 | } |
| 177 | oxmList = append(oxmList, &ofpOxmField) |
| 178 | } |
| 179 | |
| 180 | // Construct the instructions |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 181 | var instructions []*openflow_13.OfpInstruction |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 182 | for _, ofpInstruction := range flowAdd.GetInstructions() { |
| 183 | instructionType := ofpInstruction.GetType() |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 184 | instruction := openflow_13.OfpInstruction{Type: uint32(instructionType)} |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 185 | switch instructionType { |
| 186 | case ofp.OFPITGotoTable: |
| 187 | instruction.Data = &openflow_13.OfpInstruction_GotoTable{ |
| 188 | GotoTable: &openflow_13.OfpInstructionGotoTable{ |
| 189 | TableId: uint32(ofpInstruction.(ofp.IInstructionGotoTable).GetTableId()), |
| 190 | }, |
| 191 | } |
| 192 | case ofp.OFPITWriteMetadata: |
| 193 | instruction.Data = &openflow_13.OfpInstruction_WriteMetadata{ |
| 194 | WriteMetadata: &openflow_13.OfpInstructionWriteMetadata{ |
| 195 | Metadata: ofpInstruction.(ofp.IInstructionWriteMetadata).GetMetadata(), |
| 196 | MetadataMask: ofpInstruction.(ofp.IInstructionWriteMetadata).GetMetadataMask(), |
| 197 | }, |
| 198 | } |
| 199 | case ofp.OFPITWriteActions: |
| 200 | var ofpActions []*openflow_13.OfpAction |
| 201 | for _, action := range ofpInstruction.(ofp.IInstructionWriteActions).GetActions() { |
| 202 | ofpActions = append(ofpActions, extractAction(action)) |
| 203 | } |
| 204 | instruction.Data = &openflow_13.OfpInstruction_Actions{ |
| 205 | Actions: &openflow_13.OfpInstructionActions{ |
| 206 | Actions: ofpActions, |
| 207 | }, |
| 208 | } |
| 209 | case ofp.OFPITApplyActions: |
| 210 | var ofpActions []*openflow_13.OfpAction |
| 211 | for _, action := range ofpInstruction.(ofp.IInstructionApplyActions).GetActions() { |
| 212 | ofpActions = append(ofpActions, extractAction(action)) |
| 213 | } |
| 214 | instruction.Data = &openflow_13.OfpInstruction_Actions{ |
| 215 | Actions: &openflow_13.OfpInstructionActions{ |
| 216 | Actions: ofpActions, |
| 217 | }, |
| 218 | } |
| 219 | case ofp.OFPITMeter: |
| 220 | instruction.Data = &openflow_13.OfpInstruction_Meter{ |
| 221 | Meter: &openflow_13.OfpInstructionMeter{ |
| 222 | MeterId: ofpInstruction.(ofp.IInstructionMeter).GetMeterId(), |
| 223 | }, |
| 224 | } |
| 225 | } |
| 226 | instructions = append(instructions, &instruction) |
| 227 | } |
| 228 | |
| 229 | // Construct the request |
| 230 | flowUpdate := openflow_13.FlowTableUpdate{ |
| 231 | Id: ofc.DeviceID, |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 232 | FlowMod: &openflow_13.OfpFlowMod{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 233 | Cookie: flowAdd.Cookie, |
| 234 | CookieMask: flowAdd.CookieMask, |
| 235 | TableId: uint32(flowAdd.TableId), |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 236 | Command: openflow_13.OfpFlowModCommand_OFPFC_ADD, |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 237 | IdleTimeout: uint32(flowAdd.IdleTimeout), |
| 238 | HardTimeout: uint32(flowAdd.HardTimeout), |
| 239 | Priority: uint32(flowAdd.Priority), |
| 240 | BufferId: flowAdd.BufferId, |
| 241 | OutPort: uint32(flowAdd.OutPort), |
| 242 | OutGroup: uint32(flowAdd.OutGroup), |
| 243 | Flags: uint32(flowAdd.Flags), |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 244 | Match: &openflow_13.OfpMatch{ |
| 245 | Type: openflow_13.OfpMatchType(flowAdd.Match.GetType()), |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 246 | OxmFields: oxmList, |
| 247 | }, |
| 248 | |
| 249 | Instructions: instructions, |
| 250 | }, |
Maninder | e2af7e4 | 2020-12-04 11:46:26 +0530 | [diff] [blame] | 251 | Xid: flowAdd.Xid, |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 252 | } |
| 253 | if logger.V(log.DebugLevel) { |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 254 | logger.Debugw(ctx, "FlowAdd being sent to Voltha", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 255 | log.Fields{ |
David K. Bainbridge | e05cf0c | 2021-08-19 03:16:50 +0000 | [diff] [blame] | 256 | "device-id": ofc.DeviceID, |
| 257 | "flow-mod-object": flowUpdate, |
| 258 | }) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 259 | } |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 260 | if _, err := volthaClient.UpdateLogicalDeviceFlowTable(log.WithSpanFromContext(context.Background(), ctx), &flowUpdate); err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 261 | logger.Errorw(ctx, "Error calling FlowAdd ", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 262 | log.Fields{ |
| 263 | "device-id": ofc.DeviceID, |
| 264 | "error": err}) |
Andrea Campanella | 3034262 | 2020-03-05 20:48:26 +0100 | [diff] [blame] | 265 | // Report failure to controller |
| 266 | message := ofp.NewFlowModFailedErrorMsg() |
| 267 | message.SetXid(flowAdd.Xid) |
| 268 | message.SetCode(ofp.OFPFMFCBadCommand) |
| 269 | //OF 1.3 |
| 270 | message.SetVersion(4) |
| 271 | bs := make([]byte, 2) |
| 272 | //OF 1.3 |
| 273 | bs[0] = byte(4) |
| 274 | //Flow Mod |
| 275 | bs[1] = byte(14) |
| 276 | //Length of the message |
| 277 | length := make([]byte, 2) |
| 278 | binary.BigEndian.PutUint16(length, 56) |
| 279 | bs = append(bs, length...) |
| 280 | empty := []byte{0, 0, 0, 0} |
| 281 | bs = append(bs, empty...) |
| 282 | //Cookie of the Flow |
| 283 | cookie := make([]byte, 52) |
| 284 | binary.BigEndian.PutUint64(cookie, flowAdd.Cookie) |
| 285 | bs = append(bs, cookie...) |
| 286 | message.SetData(bs) |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 287 | err := ofc.SendMessage(ctx, message) |
Andrea Campanella | 3034262 | 2020-03-05 20:48:26 +0100 | [diff] [blame] | 288 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 289 | logger.Errorw(ctx, "Error reporting failure of FlowUpdate to controller", |
Andrea Campanella | 3034262 | 2020-03-05 20:48:26 +0100 | [diff] [blame] | 290 | log.Fields{ |
| 291 | "device-id": ofc.DeviceID, |
| 292 | "error": err}) |
| 293 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
| 296 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 297 | func (ofc *OFConnection) handleFlowMod(ctx context.Context, flowMod *ofp.FlowMod) { |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 298 | span, ctx := log.CreateChildSpan(ctx, "openflow-flow-modification") |
| 299 | defer span.Finish() |
| 300 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 301 | if logger.V(log.DebugLevel) { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 302 | logger.Debugw(ctx, "handleFlowMod called", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 303 | log.Fields{ |
| 304 | "device-id": ofc.DeviceID, |
Matteo Scandolo | f3a7d57 | 2021-11-29 15:36:19 -0800 | [diff] [blame] | 305 | "flow-mod": flowMod}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 306 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 307 | logger.Errorw(ctx, "handleFlowMod not implemented", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 308 | log.Fields{"device-id": ofc.DeviceID}) |
| 309 | } |
| 310 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 311 | func (ofc *OFConnection) handleFlowModStrict(ctx context.Context, flowModStrict *ofp.FlowModifyStrict) { |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 312 | span, ctx := log.CreateChildSpan(ctx, "openflow-flow-modification-strict") |
| 313 | defer span.Finish() |
| 314 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 315 | if logger.V(log.DebugLevel) { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 316 | logger.Debugw(ctx, "handleFlowModStrict called", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 317 | log.Fields{ |
| 318 | "device-id": ofc.DeviceID, |
Matteo Scandolo | f3a7d57 | 2021-11-29 15:36:19 -0800 | [diff] [blame] | 319 | "flow-mod-strict": flowModStrict}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 320 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 321 | logger.Error(ctx, "handleFlowModStrict not implemented", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 322 | log.Fields{"device-id": ofc.DeviceID}) |
| 323 | } |
| 324 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 325 | func (ofc *OFConnection) handleFlowDelete(ctx context.Context, flowDelete *ofp.FlowDelete) { |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 326 | span, ctx := log.CreateChildSpan(ctx, "openflow-flow-delete") |
| 327 | defer span.Finish() |
| 328 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 329 | if logger.V(log.DebugLevel) { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 330 | logger.Debugw(ctx, "handleFlowDelete called", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 331 | log.Fields{ |
| 332 | "device-id": ofc.DeviceID, |
Matteo Scandolo | f3a7d57 | 2021-11-29 15:36:19 -0800 | [diff] [blame] | 333 | "flow-delete": flowDelete}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 334 | } |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 335 | logger.Error(ctx, "handleFlowDelete not implemented", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 336 | log.Fields{"device-id": ofc.DeviceID}) |
| 337 | |
| 338 | } |
| 339 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 340 | func (ofc *OFConnection) handleFlowDeleteStrict(ctx context.Context, flowDeleteStrict *ofp.FlowDeleteStrict) { |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 341 | span, ctx := log.CreateChildSpan(ctx, "openflow-flow-delete-strict") |
| 342 | defer span.Finish() |
| 343 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 344 | if logger.V(log.DebugLevel) { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 345 | logger.Debugw(ctx, "handleFlowDeleteStrict called", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 346 | log.Fields{ |
Matteo Scandolo | f3a7d57 | 2021-11-29 15:36:19 -0800 | [diff] [blame] | 347 | "device-id": ofc.DeviceID, |
| 348 | "flow": flowDeleteStrict}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 349 | } |
| 350 | |
David Bainbridge | f8ce7d2 | 2020-04-08 12:49:41 -0700 | [diff] [blame] | 351 | volthaClient := ofc.VolthaClient.Get() |
| 352 | if volthaClient == nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 353 | logger.Errorw(ctx, "no-voltha-connection", |
David K. Bainbridge | 9cb404e | 2020-01-28 14:32:29 -0800 | [diff] [blame] | 354 | log.Fields{"device-id": ofc.DeviceID}) |
| 355 | return |
| 356 | } |
| 357 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 358 | // Construct match |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 359 | var oxmList []*openflow_13.OfpOxmField |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 360 | for _, oxmField := range flowDeleteStrict.Match.GetOxmList() { |
| 361 | name := oxmMap[oxmField.GetOXMName()] |
| 362 | val := oxmField.GetOXMValue() |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 363 | var ofpOxmField openflow_13.OfpOxmField |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 364 | ofpOxmField.OxmClass = ofp.OFPXMCOpenflowBasic |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 365 | var field openflow_13.OfpOxmOfbField |
| 366 | field.Type = openflow_13.OxmOfbFieldTypes(name) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 367 | |
| 368 | var x openflow_13.OfpOxmField_OfbField |
| 369 | x.OfbField = &field |
| 370 | ofpOxmField.Field = &x |
| 371 | |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 372 | switch openflow_13.OxmOfbFieldTypes(name) { |
| 373 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_IN_PORT: |
| 374 | field.Value = &openflow_13.OfpOxmOfbField_Port{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 375 | Port: uint32(val.(ofp.Port)), |
| 376 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 377 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_IN_PHY_PORT: |
| 378 | field.Value = &openflow_13.OfpOxmOfbField_PhysicalPort{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 379 | PhysicalPort: val.(uint32), |
| 380 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 381 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_METADATA: |
| 382 | field.Value = &openflow_13.OfpOxmOfbField_TableMetadata{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 383 | TableMetadata: val.(uint64), |
| 384 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 385 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_ETH_TYPE: |
| 386 | field.Value = &openflow_13.OfpOxmOfbField_EthType{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 387 | EthType: uint32(val.(ofp.EthernetType)), |
| 388 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 389 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_IP_PROTO: |
| 390 | field.Value = &openflow_13.OfpOxmOfbField_IpProto{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 391 | IpProto: uint32(val.(ofp.IpPrototype)), |
| 392 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 393 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_IPV4_DST: |
| 394 | field.Value = &openflow_13.OfpOxmOfbField_Ipv4Dst{ |
Jonathan Hart | 60c5d77 | 2020-03-30 18:28:40 -0700 | [diff] [blame] | 395 | Ipv4Dst: binary.BigEndian.Uint32(val.(net.IP)), |
| 396 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 397 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_ETH_DST: |
| 398 | field.Value = &openflow_13.OfpOxmOfbField_EthDst{ |
Esin Karaman | 082a701 | 2020-06-23 15:47:02 +0000 | [diff] [blame] | 399 | EthDst: val.(net.HardwareAddr), |
| 400 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 401 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_UDP_SRC: |
| 402 | field.Value = &openflow_13.OfpOxmOfbField_UdpSrc{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 403 | UdpSrc: uint32(val.(uint16)), |
| 404 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 405 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_UDP_DST: |
| 406 | field.Value = &openflow_13.OfpOxmOfbField_UdpDst{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 407 | UdpDst: uint32(val.(uint16)), |
| 408 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 409 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID: |
| 410 | field.Value = &openflow_13.OfpOxmOfbField_VlanVid{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 411 | VlanVid: uint32(val.(uint16)), |
| 412 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 413 | case openflow_13.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_PCP: |
| 414 | field.Value = &openflow_13.OfpOxmOfbField_VlanPcp{ |
Esin Karaman | 082a701 | 2020-06-23 15:47:02 +0000 | [diff] [blame] | 415 | VlanPcp: uint32(val.(uint8)), |
| 416 | } |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame] | 417 | case 200: // voltha-protos doesn't actually have a type for vlan_mask |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 418 | field = openflow_13.OfpOxmOfbField{Type: openflow_13.OxmOfbFieldTypes(oxmMap["vlan_vid"])} |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame] | 419 | field.HasMask = true |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 420 | ofpOxmField = openflow_13.OfpOxmField{ |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame] | 421 | OxmClass: ofp.OFPXMCOpenflowBasic, |
| 422 | Field: &openflow_13.OfpOxmField_OfbField{OfbField: &field}, |
| 423 | } |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 424 | field.Value = &openflow_13.OfpOxmOfbField_VlanVid{ |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame] | 425 | VlanVid: uint32(val.(uint16)), |
| 426 | } |
| 427 | vidMask := val.(uint16) |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 428 | field.Mask = &openflow_13.OfpOxmOfbField_VlanVidMask{ |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame] | 429 | VlanVidMask: uint32(vidMask), |
| 430 | } |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 431 | } |
Don Newton | 7fe70f7 | 2020-02-21 13:54:11 -0500 | [diff] [blame] | 432 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 433 | oxmList = append(oxmList, &ofpOxmField) |
| 434 | } |
| 435 | |
Matteo Scandolo | 322c308 | 2020-06-17 14:21:26 -0700 | [diff] [blame] | 436 | responseRequired := false |
| 437 | |
| 438 | if flowDeleteStrict.GetFlags() == ofp.OFPFFSendFlowRem { |
| 439 | responseRequired = true |
| 440 | } |
| 441 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 442 | // Construct request |
| 443 | flowUpdate := openflow_13.FlowTableUpdate{ |
| 444 | Id: ofc.DeviceID, |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 445 | FlowMod: &openflow_13.OfpFlowMod{ |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 446 | Cookie: flowDeleteStrict.Cookie, |
| 447 | CookieMask: flowDeleteStrict.CookieMask, |
| 448 | TableId: uint32(flowDeleteStrict.TableId), |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 449 | Command: openflow_13.OfpFlowModCommand_OFPFC_DELETE_STRICT, |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 450 | IdleTimeout: uint32(flowDeleteStrict.IdleTimeout), |
| 451 | HardTimeout: uint32(flowDeleteStrict.HardTimeout), |
| 452 | Priority: uint32(flowDeleteStrict.Priority), |
| 453 | BufferId: flowDeleteStrict.BufferId, |
| 454 | OutPort: uint32(flowDeleteStrict.OutPort), |
| 455 | OutGroup: uint32(flowDeleteStrict.OutGroup), |
| 456 | Flags: uint32(flowDeleteStrict.Flags), |
khenaidoo | fcf0b8d | 2021-10-19 17:57:30 -0400 | [diff] [blame] | 457 | Match: &openflow_13.OfpMatch{ |
| 458 | Type: openflow_13.OfpMatchType(flowDeleteStrict.Match.GetType()), |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 459 | OxmFields: oxmList, |
| 460 | }, |
| 461 | }, |
Maninder | e2af7e4 | 2020-12-04 11:46:26 +0530 | [diff] [blame] | 462 | Xid: flowDeleteStrict.Xid, |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | if logger.V(log.DebugLevel) { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 466 | logger.Debugf(ctx, "FlowDeleteStrict being sent to Voltha", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 467 | log.Fields{ |
| 468 | "device-id": ofc.DeviceID, |
Matteo Scandolo | f3a7d57 | 2021-11-29 15:36:19 -0800 | [diff] [blame] | 469 | "flow-update": flowUpdate}) |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 470 | } |
Matteo Scandolo | 936e2df | 2020-10-27 14:31:36 -0700 | [diff] [blame] | 471 | |
Girish Kumar | 01e0c63 | 2020-08-10 16:48:56 +0000 | [diff] [blame] | 472 | if _, err := volthaClient.UpdateLogicalDeviceFlowTable(log.WithSpanFromContext(context.Background(), ctx), &flowUpdate); err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 473 | logger.Errorw(ctx, "Error calling FlowDelete ", |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 474 | log.Fields{ |
| 475 | "device-id": ofc.DeviceID, |
| 476 | "error": err}) |
Matteo Scandolo | 322c308 | 2020-06-17 14:21:26 -0700 | [diff] [blame] | 477 | return |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 478 | } |
Matteo Scandolo | 322c308 | 2020-06-17 14:21:26 -0700 | [diff] [blame] | 479 | |
| 480 | if responseRequired { |
| 481 | response := ofp.NewFlowRemoved() |
| 482 | |
| 483 | response.Cookie = flowDeleteStrict.Cookie |
| 484 | response.Priority = flowDeleteStrict.Priority |
| 485 | response.Reason = ofp.OFPRRDelete |
| 486 | response.Match = flowDeleteStrict.Match |
| 487 | response.IdleTimeout = flowDeleteStrict.IdleTimeout |
| 488 | response.HardTimeout = flowDeleteStrict.HardTimeout |
| 489 | response.Xid = flowDeleteStrict.Xid |
| 490 | |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 491 | err := ofc.SendMessage(ctx, response) |
Matteo Scandolo | 322c308 | 2020-06-17 14:21:26 -0700 | [diff] [blame] | 492 | if err != nil { |
Rohan Agrawal | c32d993 | 2020-06-15 11:01:47 +0000 | [diff] [blame] | 493 | logger.Errorw(ctx, "Error sending FlowRemoved to ONOS", |
Matteo Scandolo | 322c308 | 2020-06-17 14:21:26 -0700 | [diff] [blame] | 494 | log.Fields{ |
| 495 | "device-id": ofc.DeviceID, |
| 496 | "error": err}) |
| 497 | } |
| 498 | } |
| 499 | |
David K. Bainbridge | 157bdab | 2020-01-16 14:38:05 -0800 | [diff] [blame] | 500 | } |