manikkaraj k | bf256be | 2019-03-25 00:13:48 +0530 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 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 adaptercore |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "crypto/md5" |
| 22 | "encoding/json" |
| 23 | "errors" |
| 24 | "fmt" |
| 25 | "math/big" |
| 26 | |
| 27 | rsrcMgr "github.com/opencord/voltha-go/adapters/openolt/adaptercore/resourcemanager" |
| 28 | "github.com/opencord/voltha-go/common/log" |
| 29 | tp "github.com/opencord/voltha-go/common/techprofile" |
| 30 | fd "github.com/opencord/voltha-go/rw_core/flow_decomposition" |
| 31 | ofp "github.com/opencord/voltha-protos/go/openflow_13" |
| 32 | openolt_pb2 "github.com/opencord/voltha-protos/go/openolt" |
| 33 | voltha "github.com/opencord/voltha-protos/go/voltha" |
| 34 | ) |
| 35 | |
| 36 | const ( |
| 37 | // Flow categories |
| 38 | HSIA_FLOW = "HSIA_FLOW" |
| 39 | EAPOL_FLOW = "EAPOL_FLOW" |
| 40 | |
| 41 | IP_PROTO_DHCP = 17 |
| 42 | |
| 43 | IP_PROTO_IGMP = 2 |
| 44 | |
| 45 | EAP_ETH_TYPE = 0x888e |
| 46 | LLDP_ETH_TYPE = 0x88cc |
| 47 | |
| 48 | IGMP_PROTO = 2 |
| 49 | |
| 50 | //FIXME - see also BRDCM_DEFAULT_VLAN in broadcom_onu.py |
| 51 | DEFAULT_MGMT_VLAN = 4091 |
| 52 | |
| 53 | DEFAULT_NETWORK_INTERFACE_ID = 0 |
| 54 | |
| 55 | // Openolt Flow |
| 56 | UPSTREAM = "upstream" |
| 57 | DOWNSTREAM = "downstream" |
| 58 | PACKET_TAG_TYPE = "pkt_tag_type" |
| 59 | UNTAGGED = "untagged" |
| 60 | SINGLE_TAG = "single_tag" |
| 61 | DOUBLE_TAG = "double_tag" |
| 62 | |
| 63 | // classifierInfo |
| 64 | ETH_TYPE = "eth_type" |
| 65 | TPID = "tpid" |
| 66 | IP_PROTO = "ip_proto" |
| 67 | IN_PORT = "in_port" |
| 68 | VLAN_VID = "vlan_vid" |
| 69 | VLAN_PCP = "vlan_pcp" |
| 70 | UDP_DST = "udp_dst" |
| 71 | UDP_SRC = "udp_src" |
| 72 | IPV4_DST = "ipv4_dst" |
| 73 | IPV4_SRC = "ipv4_src" |
| 74 | METADATA = "metadata" |
| 75 | OUTPUT = "output" |
| 76 | // Action |
| 77 | POP_VLAN = "pop_vlan" |
| 78 | PUSH_VLAN = "push_vlan" |
| 79 | TRAP_TO_HOST = "trap_to_host" |
| 80 | ) |
| 81 | |
| 82 | type OpenOltFlowMgr struct { |
| 83 | techprofile []*tp.TechProfileMgr |
| 84 | deviceHandler *DeviceHandler |
| 85 | resourceMgr *rsrcMgr.OpenOltResourceMgr |
| 86 | } |
| 87 | |
| 88 | func NewFlowManager(dh *DeviceHandler, rsrcMgr *rsrcMgr.OpenOltResourceMgr) *OpenOltFlowMgr { |
| 89 | log.Info("Initializing flow manager") |
| 90 | var flowMgr OpenOltFlowMgr |
| 91 | flowMgr.deviceHandler = dh |
| 92 | flowMgr.resourceMgr = rsrcMgr |
| 93 | if err := flowMgr.populateTechProfilePerPonPort(); err != nil { |
| 94 | log.Error("Error while populating tech profile mgr\n") |
| 95 | return nil |
| 96 | } |
| 97 | log.Info("Initialization of flow manager success!!") |
| 98 | return &flowMgr |
| 99 | } |
| 100 | |
| 101 | func (f *OpenOltFlowMgr) divideAndAddFlow(intfId uint32, onuId uint32, uniId uint32, portNo uint32, classifierInfo map[string]interface{}, actionInfo map[string]interface{}, flow *ofp.OfpFlowStats) { |
| 102 | var allocId []uint32 |
| 103 | var gemPorts []uint32 |
| 104 | |
| 105 | log.Infow("Dividing flow", log.Fields{"intfId": intfId, "onuId": onuId, "uniId": uniId, "portNo": portNo, "classifier": classifierInfo, "action": actionInfo}) |
| 106 | |
| 107 | log.Infow("sorting flow", log.Fields{"intfId": intfId, "onuId": onuId, "uniId": uniId, "portNo": portNo, |
| 108 | "classifierInfo": classifierInfo, "actionInfo": actionInfo}) |
| 109 | |
| 110 | uni := getUniPortPath(intfId, onuId, uniId) |
| 111 | log.Debugw("Uni port name", log.Fields{"uni": uni}) |
| 112 | allocId, gemPorts = f.createTcontGemports(intfId, onuId, uniId, uni, portNo, flow.GetTableId()) |
| 113 | if allocId == nil || gemPorts == nil { |
| 114 | log.Error("alloc-id-gem-ports-unavailable") |
| 115 | return |
| 116 | } |
| 117 | |
| 118 | /* Flows can't be added specific to gemport unless p-bits are received. |
| 119 | * Hence adding flows for all gemports |
| 120 | */ |
| 121 | for _, gemPort := range gemPorts { |
| 122 | if ipProto, ok := classifierInfo[IP_PROTO]; ok { |
| 123 | if ipProto.(uint32) == IP_PROTO_DHCP { |
| 124 | log.Info("Adding DHCP flow") |
| 125 | f.addDHCPTrapFlow(intfId, onuId, uniId, portNo, classifierInfo, actionInfo, flow, allocId[0], gemPort) |
| 126 | } else if ipProto == IP_PROTO_IGMP { |
| 127 | log.Info("igmp flow add ignored, not implemented yet") |
| 128 | } else { |
| 129 | log.Errorw("Invalid-Classifier-to-handle", log.Fields{"classifier": classifierInfo, "action": actionInfo}) |
| 130 | //return errors.New("Invalid-Classifier-to-handle") |
| 131 | } |
| 132 | } else if ethType, ok := classifierInfo[ETH_TYPE]; ok { |
| 133 | if ethType.(uint32) == EAP_ETH_TYPE { |
| 134 | log.Info("Adding EAPOL flow") |
| 135 | f.addEAPOLFlow(intfId, onuId, uniId, portNo, flow, allocId[0], gemPort, DEFAULT_MGMT_VLAN) |
| 136 | if vlan := getSubscriberVlan(fd.GetInPort(flow)); vlan != 0 { |
| 137 | f.addEAPOLFlow(intfId, onuId, uniId, portNo, flow, allocId[0], gemPort, vlan) |
| 138 | } |
| 139 | // Send Techprofile download event to child device in go routine as it takes time |
| 140 | go f.sendTPDownloadMsgToChild(intfId, onuId, uniId, uni) |
| 141 | } |
| 142 | if ethType == LLDP_ETH_TYPE { |
| 143 | log.Info("Adding LLDP flow") |
| 144 | addLLDPFlow(flow, portNo) |
| 145 | } |
| 146 | } else if _, ok := actionInfo[PUSH_VLAN]; ok { |
| 147 | log.Info("Adding upstream data rule") |
| 148 | f.addUpstreamDataFlow(intfId, onuId, uniId, portNo, classifierInfo, actionInfo, flow, allocId[0], gemPort) |
| 149 | } else if _, ok := actionInfo[POP_VLAN]; ok { |
| 150 | log.Info("Adding Downstream data rule") |
| 151 | f.addDownstreamDataFlow(intfId, onuId, uniId, portNo, classifierInfo, actionInfo, flow, allocId[0], gemPort) |
| 152 | } else { |
| 153 | log.Errorw("Invalid-flow-type-to-handle", log.Fields{"classifier": classifierInfo, "action": actionInfo, "flow": flow}) |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // This function allocates tconts and GEM ports for an ONU, currently one TCONT is supported per ONU |
| 159 | func (f *OpenOltFlowMgr) createTcontGemports(intfId uint32, onuId uint32, uniId uint32, uni string, uniPort uint32, tableID uint32) ([]uint32, []uint32) { |
| 160 | var allocID []uint32 |
| 161 | var gemPortIDs []uint32 |
| 162 | //If we already have allocated earlier for this onu, render them |
| 163 | if tcontId := f.resourceMgr.GetCurrentAllocIDForOnu(intfId, onuId); tcontId != 0 { |
| 164 | allocID = append(allocID, tcontId) |
| 165 | } |
| 166 | gemPortIDs = f.resourceMgr.GetCurrentGEMPortIDsForOnu(intfId, onuId, uniId) |
| 167 | if len(allocID) != 0 && len(gemPortIDs) != 0 { |
| 168 | log.Debug("Rendered Tcont and GEM ports from resource manager", log.Fields{"intfId": intfId, "onuId": onuId, "uniPort": uniId, |
| 169 | "allocID": allocID, "gemPortIDs": gemPortIDs}) |
| 170 | return allocID, gemPortIDs |
| 171 | } |
| 172 | log.Debug("Creating New TConts and Gem ports", log.Fields{"pon": intfId, "onu": onuId, "uni": uniId}) |
| 173 | |
| 174 | //FIXME: If table id is <= 63 using 64 as table id |
| 175 | if tableID < tp.DEFAULT_TECH_PROFILE_TABLE_ID { |
| 176 | tableID = tp.DEFAULT_TECH_PROFILE_TABLE_ID |
| 177 | } |
| 178 | tpPath := f.getTPpath(intfId, uni) |
| 179 | // Check tech profile instance already exists for derived port name |
| 180 | tech_profile_instance, err := f.techprofile[intfId].GetTPInstanceFromKVStore(tableID, tpPath) |
| 181 | if err != nil { // This should not happen, something wrong in KV backend transaction |
| 182 | log.Errorw("Error in fetching tech profile instance from KV store", log.Fields{"tableID": tableID, "path": tpPath}) |
| 183 | return nil, nil |
| 184 | } |
| 185 | if tech_profile_instance == nil { |
| 186 | log.Info("Creating tech profile instance", log.Fields{"path": tpPath}) |
| 187 | tech_profile_instance = f.techprofile[intfId].CreateTechProfInstance(tableID, uni, intfId) |
| 188 | if tech_profile_instance == nil { |
| 189 | log.Error("Tech-profile-instance-creation-failed") |
| 190 | return nil, nil |
| 191 | } |
| 192 | } else { |
| 193 | log.Debugw("Tech-profile-instance-already-exist-for-given port-name", log.Fields{"uni": uni}) |
| 194 | } |
| 195 | // Get upstream and downstream scheduler protos |
| 196 | us_scheduler := f.techprofile[intfId].GetUsScheduler(tech_profile_instance) |
| 197 | ds_scheduler := f.techprofile[intfId].GetDsScheduler(tech_profile_instance) |
| 198 | // Get TCONTS protos |
| 199 | tconts := f.techprofile[intfId].GetTconts(tech_profile_instance, us_scheduler, ds_scheduler) |
| 200 | if len(tconts) == 0 { |
| 201 | log.Error("TCONTS not found ") |
| 202 | return nil, nil |
| 203 | } |
| 204 | log.Debugw("Sending Create tcont to device", |
| 205 | log.Fields{"onu": onuId, "uni": uniId, "portNo": "", "tconts": tconts}) |
| 206 | if _, err := f.deviceHandler.Client.CreateTconts(context.Background(), |
| 207 | &openolt_pb2.Tconts{IntfId: intfId, |
| 208 | OnuId: onuId, |
| 209 | UniId: uniId, |
| 210 | PortNo: uniPort, |
| 211 | Tconts: tconts}); err != nil { |
| 212 | log.Error("Error while creating TCONT in device") |
| 213 | return nil, nil |
| 214 | } |
| 215 | allocID = append(allocID, tech_profile_instance.UsScheduler.AllocID) |
| 216 | for _, gem := range tech_profile_instance.UpstreamGemPortAttributeList { |
| 217 | gemPortIDs = append(gemPortIDs, gem.GemportID) |
| 218 | } |
| 219 | log.Debugw("Allocated Tcont and GEM ports", log.Fields{"allocID": allocID, "gemports": gemPortIDs}) |
| 220 | // Send Tconts and GEM ports to KV store |
| 221 | f.storeTcontsGEMPortsIntoKVStore(intfId, onuId, uniId, allocID, gemPortIDs) |
| 222 | return allocID, gemPortIDs |
| 223 | } |
| 224 | |
| 225 | func (f *OpenOltFlowMgr) storeTcontsGEMPortsIntoKVStore(intfId uint32, onuId uint32, uniId uint32, allocID []uint32, gemPortIDs []uint32) { |
| 226 | |
| 227 | log.Debugw("Storing allocated Tconts and GEM ports into KV store", |
| 228 | log.Fields{"intfId": intfId, "onuId": onuId, "uniId": uniId, "allocID": allocID, "gemPortIDs": gemPortIDs}) |
| 229 | /* Update the allocated alloc_id and gem_port_id for the ONU/UNI to KV store */ |
| 230 | if err := f.resourceMgr.UpdateAllocIdsForOnu(intfId, onuId, uniId, allocID); err != nil { |
| 231 | log.Error("Errow while uploading allocID to KV store") |
| 232 | } |
| 233 | if err := f.resourceMgr.UpdateGEMPortIDsForOnu(intfId, onuId, uniId, gemPortIDs); err != nil { |
| 234 | log.Error("Errow while uploading GEMports to KV store") |
| 235 | } |
| 236 | if err := f.resourceMgr.UpdateGEMportsPonportToOnuMapOnKVStore(gemPortIDs, intfId, onuId, uniId); err != nil { |
| 237 | log.Error("Errow while uploading gemtopon map to KV store") |
| 238 | } |
| 239 | log.Debug("Stored tconts and GEM into KV store successfully") |
| 240 | } |
| 241 | |
| 242 | func (f *OpenOltFlowMgr) populateTechProfilePerPonPort() error { |
| 243 | for _, techRange := range f.resourceMgr.DevInfo.Ranges { |
| 244 | for intfId := range techRange.IntfIds { |
| 245 | f.techprofile = append(f.techprofile, f.resourceMgr.ResourceMgrs[uint32(intfId)].TechProfileMgr) |
| 246 | } |
| 247 | } |
| 248 | //Make sure we have as many tech_profiles as there are pon ports on the device |
| 249 | if len(f.techprofile) != int(f.resourceMgr.DevInfo.GetPonPorts()) { |
| 250 | log.Errorw("Error while populating techprofile", |
| 251 | log.Fields{"numofTech": len(f.techprofile), "numPonPorts": f.resourceMgr.DevInfo.GetPonPorts()}) |
| 252 | return errors.New("Error while populating techprofile mgrs") |
| 253 | } |
| 254 | log.Infow("Populated techprofile per ponport successfully", |
| 255 | log.Fields{"numofTech": len(f.techprofile), "numPonPorts": f.resourceMgr.DevInfo.GetPonPorts()}) |
| 256 | return nil |
| 257 | } |
| 258 | |
| 259 | func (f *OpenOltFlowMgr) addDownstreamDataFlow(intfid uint32, onuid uint32, uniid uint32, portno uint32, classifier map[string]interface{}, action map[string]interface{}, logicalflow *ofp.OfpFlowStats, allocid uint32, gemportid uint32) { |
| 260 | |
| 261 | log.Info("Unimplemented") |
| 262 | |
| 263 | return |
| 264 | |
| 265 | } |
| 266 | |
| 267 | func (f *OpenOltFlowMgr) addUpstreamDataFlow(intfid uint32, onuid uint32, uniid uint32, portno uint32, classifier map[string]interface{}, action map[string]interface{}, logicalflow *ofp.OfpFlowStats, allocid uint32, gemportid uint32) { |
| 268 | |
| 269 | log.Info("Unimplemented") |
| 270 | return |
| 271 | |
| 272 | } |
| 273 | |
| 274 | func (f *OpenOltFlowMgr) addDHCPTrapFlow(intfId uint32, onuId uint32, uniId uint32, portNo uint32, classifier map[string]interface{}, action map[string]interface{}, logicalFlow *ofp.OfpFlowStats, allocId uint32, gemPortId uint32) { |
| 275 | return |
| 276 | } |
| 277 | |
| 278 | // Add EAPOL to device |
| 279 | func (f *OpenOltFlowMgr) addEAPOLFlow(intfId uint32, onuId uint32, uniId uint32, portNo uint32, logicalFlow *ofp.OfpFlowStats, allocId uint32, gemPortId uint32, vlanId uint32) { |
| 280 | log.Debugw("Adding EAPOL to device", log.Fields{"intfId": intfId, "onuId": onuId, "portNo": portNo, "allocId": allocId, "gemPortId": gemPortId, "vlanId": vlanId, "flow": logicalFlow}) |
| 281 | |
| 282 | uplinkClassifier := make(map[string]interface{}) |
| 283 | uplinkAction := make(map[string]interface{}) |
| 284 | downlinkClassifier := make(map[string]interface{}) |
| 285 | downlinkAction := make(map[string]interface{}) |
| 286 | var upstreamFlow openolt_pb2.Flow |
| 287 | var downstreamFlow openolt_pb2.Flow |
| 288 | |
| 289 | // Fill Classfier |
| 290 | uplinkClassifier[ETH_TYPE] = uint32(EAP_ETH_TYPE) |
| 291 | uplinkClassifier[PACKET_TAG_TYPE] = SINGLE_TAG |
| 292 | uplinkClassifier[VLAN_VID] = vlanId |
| 293 | // Fill action |
| 294 | uplinkAction[TRAP_TO_HOST] = true |
| 295 | flowStoreCookie := getFlowStoreCookie(uplinkClassifier, gemPortId) |
| 296 | //Add Uplink EAPOL Flow |
| 297 | uplinkFlowId, err := f.resourceMgr.GetFlowID(intfId, onuId, uniId, flowStoreCookie, "") |
| 298 | if err != nil { |
| 299 | log.Errorw("Error allocating flowID for UL EAPOL", log.Fields{"intfId": intfId, "onuId": onuId, "flowStoreCookie": flowStoreCookie}) |
| 300 | } |
| 301 | var classifierProto *openolt_pb2.Classifier |
| 302 | var actionProto *openolt_pb2.Action |
| 303 | log.Debugw("Creating UL EAPOL flow", log.Fields{"ul_classifier": uplinkClassifier, "ul_action": uplinkAction, "uplinkFlowId": uplinkFlowId}) |
| 304 | |
| 305 | if classifierProto = makeOpenOltClassifierField(uplinkClassifier); classifierProto == nil { |
| 306 | log.Error("Error in making classifier protobuf for ul flow") |
| 307 | return |
| 308 | } |
| 309 | log.Debugw("Created classifier proto", log.Fields{"classifier": *classifierProto}) |
| 310 | if actionProto = makeOpenOltActionField(uplinkAction); actionProto == nil { |
| 311 | log.Error("Error in making action protobuf for ul flow") |
| 312 | return |
| 313 | } |
| 314 | log.Debugw("Created action proto", log.Fields{"action": *actionProto}) |
| 315 | upstreamFlow = openolt_pb2.Flow{AccessIntfId: int32(intfId), |
| 316 | OnuId: int32(onuId), |
| 317 | UniId: int32(uniId), |
| 318 | FlowId: uplinkFlowId, |
| 319 | FlowType: UPSTREAM, |
| 320 | AllocId: int32(allocId), |
| 321 | NetworkIntfId: DEFAULT_NETWORK_INTERFACE_ID, // one NNI port is supported now |
| 322 | GemportId: int32(gemPortId), |
| 323 | Classifier: classifierProto, |
| 324 | Action: actionProto, |
| 325 | Priority: int32(logicalFlow.Priority), |
| 326 | Cookie: logicalFlow.Cookie, |
| 327 | PortNo: portNo} |
| 328 | if ok := f.addFlowToDevice(&upstreamFlow); ok { |
| 329 | log.Debug("EAPOL UL flow added to device successfully") |
| 330 | flowsToKVStore := f.getUpdatedFlowInfo(&upstreamFlow, flowStoreCookie, "EAPOL") |
| 331 | if err := f.updateFlowInfoToKVStore(upstreamFlow.AccessIntfId, |
| 332 | upstreamFlow.OnuId, |
| 333 | upstreamFlow.UniId, |
| 334 | upstreamFlow.FlowId, flowsToKVStore); err != nil { |
| 335 | log.Errorw("Error uploading EAPOL UL flow into KV store", log.Fields{"flow": upstreamFlow, "error": err}) |
| 336 | return |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | if vlanId == DEFAULT_MGMT_VLAN { |
| 341 | /* Add Downstream EAPOL Flow, Only for first EAP flow (BAL |
| 342 | # requirement) |
| 343 | # On one of the platforms (Broadcom BAL), when same DL classifier |
| 344 | # vlan was used across multiple ONUs, eapol flow re-adds after |
| 345 | # flow delete (cases of onu reboot/disable) fails. |
| 346 | # In order to generate unique vlan, a combination of intf_id |
| 347 | # onu_id and uniId is used. |
| 348 | # uniId defaults to 0, so add 1 to it. |
| 349 | */ |
| 350 | log.Debugw("Creating DL EAPOL flow with default vlan", log.Fields{"vlan": vlanId}) |
| 351 | specialVlanDlFlow := 4090 - intfId*onuId*(uniId+1) |
| 352 | // Assert that we do not generate invalid vlans under no condition |
| 353 | if specialVlanDlFlow <= 2 { |
| 354 | log.Fatalw("invalid-vlan-generated", log.Fields{"vlan": specialVlanDlFlow}) |
| 355 | return |
| 356 | } |
| 357 | log.Debugw("specialVlanEAPOLDlFlow:", log.Fields{"dl_vlan": specialVlanDlFlow}) |
| 358 | // Fill Classfier |
| 359 | downlinkClassifier[PACKET_TAG_TYPE] = SINGLE_TAG |
| 360 | downlinkClassifier[VLAN_VID] = uint32(specialVlanDlFlow) |
| 361 | // Fill action |
| 362 | downlinkAction[PUSH_VLAN] = true |
| 363 | downlinkAction[VLAN_VID] = vlanId |
| 364 | flowStoreCookie := getFlowStoreCookie(downlinkClassifier, gemPortId) |
| 365 | downlinkFlowId, err := f.resourceMgr.GetFlowID(intfId, onuId, uniId, flowStoreCookie, "") |
| 366 | if err != nil { |
| 367 | log.Errorw("Error allocating flowID for DL EAPOL", |
| 368 | log.Fields{"intfId": intfId, "onuId": onuId, "flowStoreCookie": flowStoreCookie}) |
| 369 | return |
| 370 | } |
| 371 | log.Debugw("Creating DL EAPOL flow", |
| 372 | log.Fields{"dl_classifier": downlinkClassifier, "dl_action": downlinkAction, "downlinkFlowId": downlinkFlowId}) |
| 373 | if classifierProto = makeOpenOltClassifierField(downlinkClassifier); classifierProto == nil { |
| 374 | log.Error("Error in making classifier protobuf for downlink flow") |
| 375 | return |
| 376 | } |
| 377 | if actionProto = makeOpenOltActionField(downlinkAction); actionProto == nil { |
| 378 | log.Error("Error in making action protobuf for dl flow") |
| 379 | return |
| 380 | } |
| 381 | // Downstream flow in grpc protobuf |
| 382 | downstreamFlow = openolt_pb2.Flow{AccessIntfId: int32(intfId), |
| 383 | OnuId: int32(onuId), |
| 384 | UniId: int32(uniId), |
| 385 | FlowId: downlinkFlowId, |
| 386 | FlowType: DOWNSTREAM, |
| 387 | AllocId: int32(allocId), |
| 388 | NetworkIntfId: DEFAULT_NETWORK_INTERFACE_ID, |
| 389 | GemportId: int32(gemPortId), |
| 390 | Classifier: classifierProto, |
| 391 | Action: actionProto, |
| 392 | Priority: int32(logicalFlow.Priority), |
| 393 | Cookie: logicalFlow.Cookie, |
| 394 | PortNo: portNo} |
| 395 | if ok := f.addFlowToDevice(&downstreamFlow); ok { |
| 396 | log.Debug("EAPOL DL flow added to device successfully") |
| 397 | flowsToKVStore := f.getUpdatedFlowInfo(&downstreamFlow, flowStoreCookie, "") |
| 398 | if err := f.updateFlowInfoToKVStore(downstreamFlow.AccessIntfId, |
| 399 | downstreamFlow.OnuId, |
| 400 | downstreamFlow.UniId, |
| 401 | downstreamFlow.FlowId, flowsToKVStore); err != nil { |
| 402 | log.Errorw("Error uploading EAPOL DL flow into KV store", log.Fields{"flow": upstreamFlow, "error": err}) |
| 403 | return |
| 404 | } |
| 405 | } |
| 406 | } else { |
| 407 | log.Infow("EAPOL flow with non-default mgmt vlan is not supported", log.Fields{"vlanId": vlanId}) |
| 408 | return |
| 409 | } |
| 410 | log.Debugw("Added EAPOL flows to device successfully", log.Fields{"flow": logicalFlow}) |
| 411 | } |
| 412 | |
| 413 | func makeOpenOltClassifierField(classifierInfo map[string]interface{}) *openolt_pb2.Classifier { |
| 414 | var classifier openolt_pb2.Classifier |
| 415 | if etherType, ok := classifierInfo[ETH_TYPE]; ok { |
| 416 | classifier.EthType = etherType.(uint32) |
| 417 | } |
| 418 | if ipProto, ok := classifierInfo[IP_PROTO]; ok { |
| 419 | classifier.IpProto = ipProto.(uint32) |
| 420 | } |
| 421 | if vlanId, ok := classifierInfo[VLAN_VID]; ok { |
| 422 | classifier.OVid = vlanId.(uint32) |
| 423 | } |
| 424 | if metadata, ok := classifierInfo[METADATA]; ok { |
| 425 | classifier.IVid = metadata.(uint32) |
| 426 | } |
| 427 | if vlanPcp, ok := classifierInfo[VLAN_PCP]; ok { |
| 428 | classifier.OPbits = vlanPcp.(uint32) |
| 429 | } |
| 430 | if udpSrc, ok := classifierInfo[UDP_SRC]; ok { |
| 431 | classifier.SrcPort = udpSrc.(uint32) |
| 432 | } |
| 433 | if udpDst, ok := classifierInfo[UDP_DST]; ok { |
| 434 | classifier.DstPort = udpDst.(uint32) |
| 435 | } |
| 436 | if ipv4Dst, ok := classifierInfo[IPV4_DST]; ok { |
| 437 | classifier.DstIp = ipv4Dst.(uint32) |
| 438 | } |
| 439 | if ipv4Src, ok := classifierInfo[IPV4_SRC]; ok { |
| 440 | classifier.SrcIp = ipv4Src.(uint32) |
| 441 | } |
| 442 | if pktTagType, ok := classifierInfo[PACKET_TAG_TYPE]; ok { |
| 443 | if pktTagType.(string) == SINGLE_TAG { |
| 444 | classifier.PktTagType = SINGLE_TAG |
| 445 | } else if pktTagType.(string) == DOUBLE_TAG { |
| 446 | classifier.PktTagType = DOUBLE_TAG |
| 447 | } else if pktTagType.(string) == UNTAGGED { |
| 448 | classifier.PktTagType = UNTAGGED |
| 449 | } else { |
| 450 | log.Error("Invalid tag type in classifier") // should not hit |
| 451 | return nil |
| 452 | } |
| 453 | } |
| 454 | return &classifier |
| 455 | } |
| 456 | |
| 457 | func makeOpenOltActionField(actionInfo map[string]interface{}) *openolt_pb2.Action { |
| 458 | var actionCmd openolt_pb2.ActionCmd |
| 459 | var action openolt_pb2.Action |
| 460 | action.Cmd = &actionCmd |
| 461 | if _, ok := actionInfo[POP_VLAN]; ok { |
| 462 | action.OVid = actionInfo[VLAN_VID].(uint32) |
| 463 | action.Cmd.RemoveOuterTag = true |
| 464 | } else if _, ok := actionInfo[PUSH_VLAN]; ok { |
| 465 | action.OVid = actionInfo[VLAN_VID].(uint32) |
| 466 | action.Cmd.AddOuterTag = true |
| 467 | } else if _, ok := actionInfo[TRAP_TO_HOST]; ok { |
| 468 | action.Cmd.TrapToHost = actionInfo[TRAP_TO_HOST].(bool) |
| 469 | } else { |
| 470 | log.Errorw("Invalid-action-field", log.Fields{"action": actionInfo}) |
| 471 | return nil |
| 472 | } |
| 473 | return &action |
| 474 | } |
| 475 | |
| 476 | func (f *OpenOltFlowMgr) getTPpath(intfId uint32, uni string) string { |
| 477 | /* |
| 478 | FIXME |
| 479 | Should get Table id form the flow, as of now hardcoded to DEFAULT_TECH_PROFILE_TABLE_ID (64) |
| 480 | 'tp_path' contains the suffix part of the tech_profile_instance path. The prefix to the 'tp_path' should be set to |
| 481 | TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX by the ONU adapter. |
| 482 | */ |
| 483 | return f.techprofile[intfId].GetTechProfileInstanceKVPath(tp.DEFAULT_TECH_PROFILE_TABLE_ID, uni) |
| 484 | } |
| 485 | |
| 486 | func getFlowStoreCookie(classifier map[string]interface{}, gemPortId uint32) uint64 { |
| 487 | if len(classifier) == 0 { // should never happen |
| 488 | log.Error("Invalid classfier object") |
| 489 | return 0 |
| 490 | } |
| 491 | var jsonData []byte |
| 492 | var flowString string |
| 493 | var err error |
| 494 | // TODO: Do we need to marshall ?? |
| 495 | if jsonData, err = json.Marshal(classifier); err != nil { |
| 496 | log.Error("Failed to encode classifier") |
| 497 | return 0 |
| 498 | } |
| 499 | flowString = string(jsonData) |
| 500 | if gemPortId != 0 { |
| 501 | flowString = fmt.Sprintf("%s%s", string(jsonData), string(gemPortId)) |
| 502 | } |
| 503 | h := md5.New() |
| 504 | h.Write([]byte(flowString)) |
| 505 | hash := big.NewInt(0) |
| 506 | hash.SetBytes(h.Sum(nil)) |
| 507 | return hash.Uint64() |
| 508 | } |
| 509 | |
| 510 | func (f *OpenOltFlowMgr) getUpdatedFlowInfo(flow *openolt_pb2.Flow, flowStoreCookie uint64, flowCategory string) *[]openolt_pb2.Flow { |
| 511 | var flows []openolt_pb2.Flow |
| 512 | /* FIXME: To be removed and identify way to get same flow ID for HSIA DL and UL flows |
| 513 | To get existing flow matching flow catogery or cookie |
| 514 | */ |
| 515 | /*flow.Category = flowCategory |
| 516 | flow.FlowStoreCookie = flowStoreCookie*/ |
| 517 | flows = append(flows, *flow) |
| 518 | // Get existing flow for flowid from KV store |
| 519 | //existingFlows := f.resourceMgr.GetFlowIDInfo(uint32(flow.AccessIntfId),uint32(flow.OnuId),uint32(flow.UniId),flow.FlowId) |
| 520 | /*existingFlows := nil |
| 521 | if existingFlows != nil{ |
| 522 | log.Debugw("Flow exists for given flowID, appending it",log.Fields{"flowID":flow.FlowId}) |
| 523 | for _,f := range *existingFlows{ |
| 524 | flows = append(flows,f) |
| 525 | } |
| 526 | }*/ |
| 527 | log.Debugw("Updated flows for given flowID", log.Fields{"updatedflow": flows, "flowid": flow.FlowId}) |
| 528 | return &flows |
| 529 | } |
| 530 | |
| 531 | func (f *OpenOltFlowMgr) updateFlowInfoToKVStore(intfId int32, onuId int32, uniId int32, flowId uint32, flows *[]openolt_pb2.Flow) error { |
| 532 | log.Debugw("Storing flow into KV store", log.Fields{"flows": *flows}) |
| 533 | /* FIXME: To implement API in resource mgr and invoke */ |
| 534 | /*if err := f.resourceMgr.UpdateFlowIDInfo(intfId,onuId,uniId,flowId,flows); err != nil{ |
| 535 | log.Debug("Error while Storing flow into KV store") |
| 536 | return err |
| 537 | } |
| 538 | log.Info("Stored flow into KV store successfully!") |
| 539 | */ |
| 540 | return nil |
| 541 | } |
| 542 | |
| 543 | func (f *OpenOltFlowMgr) addFlowToDevice(deviceFlow *openolt_pb2.Flow) bool { |
| 544 | log.Debugw("Sending flow to device via grpc", log.Fields{"flow": *deviceFlow}) |
| 545 | _, err := f.deviceHandler.Client.FlowAdd(context.Background(), deviceFlow) |
| 546 | if err != nil { |
| 547 | log.Errorw("Failed to Add flow to device", log.Fields{"err": err, "deviceFlow": deviceFlow}) |
| 548 | return false |
| 549 | } |
| 550 | log.Debugw("Flow added to device successfuly ", log.Fields{"flow": *deviceFlow}) |
| 551 | return true |
| 552 | } |
| 553 | |
| 554 | /*func register_flow(deviceFlow *openolt_pb2.Flow, logicalFlow *ofp.OfpFlowStats){ |
| 555 | //update core flows_proxy : flows_proxy.update('/', flows) |
| 556 | } |
| 557 | |
| 558 | func generateStoredId(flowId uint32, direction string)uint32{ |
| 559 | |
| 560 | if direction == UPSTREAM{ |
| 561 | log.Debug("Upstream flow shifting flowid") |
| 562 | return ((0x1 << 15) | flowId) |
| 563 | }else if direction == DOWNSTREAM{ |
| 564 | log.Debug("Downstream flow not shifting flowid") |
| 565 | return flowId |
| 566 | }else{ |
| 567 | log.Errorw("Unrecognized direction",log.Fields{"direction": direction}) |
| 568 | return flowId |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | */ |
| 573 | |
| 574 | func addLLDPFlow(flow *ofp.OfpFlowStats, portNo uint32) { |
| 575 | log.Info("Unimplemented") |
| 576 | } |
| 577 | func getUniPortPath(intfId uint32, onuId uint32, uniId uint32) string { |
| 578 | return fmt.Sprintf("pon-{%d}/onu-{%d}/uni-{%d}", intfId, onuId, uniId) |
| 579 | } |
| 580 | |
| 581 | func (f *OpenOltFlowMgr) getOnuChildDevice(intfId uint32, onuId uint32) *voltha.Device { |
| 582 | parentPortNo := IntfIdToPortNo(intfId, voltha.Port_PON_OLT) |
| 583 | ChildDevice := f.deviceHandler.GetChildDevice(parentPortNo, onuId) |
| 584 | if ChildDevice == nil { |
| 585 | log.Errorw("could-not-find-child-device", log.Fields{"parent_port_no": parentPortNo, "onuId": onuId}) |
| 586 | return nil |
| 587 | } |
| 588 | return ChildDevice |
| 589 | } |
| 590 | |
| 591 | func findNextFlow(flow *ofp.OfpFlowStats) *ofp.OfpFlowStats { |
| 592 | log.Info("Unimplemented") |
| 593 | return nil |
| 594 | } |
| 595 | |
| 596 | func getSubscriberVlan(inPort uint32) uint32 { |
| 597 | /* For EAPOL case we will use default VLAN , so will implement later if required */ |
| 598 | log.Info("Unimplemented") |
| 599 | return 0 |
| 600 | } |
| 601 | |
| 602 | func (f *OpenOltFlowMgr) clear_flows_and_scheduler_for_logical_port(childDevice *voltha.Device, logicalPort *voltha.LogicalPort) { |
| 603 | log.Info("Unimplemented") |
| 604 | } |
| 605 | |
| 606 | func (f *OpenOltFlowMgr) AddFlow(flow *ofp.OfpFlowStats) { |
| 607 | classifierInfo := make(map[string]interface{}, 0) |
| 608 | actionInfo := make(map[string]interface{}, 0) |
| 609 | log.Debug("Adding Flow", log.Fields{"flow": flow}) |
| 610 | for _, field := range fd.GetOfbFields(flow) { |
| 611 | if field.Type == fd.ETH_TYPE { |
| 612 | classifierInfo[ETH_TYPE] = field.GetEthType() |
| 613 | log.Debug("field-type-eth-type", log.Fields{"classifierInfo[ETH_TYPE]": classifierInfo[ETH_TYPE].(uint32)}) |
| 614 | } else if field.Type == fd.IP_PROTO { |
| 615 | classifierInfo[IP_PROTO] = field.GetIpProto() |
| 616 | log.Debug("field-type-ip-proto", log.Fields{"classifierInfo[IP_PROTO]": classifierInfo[IP_PROTO].(uint32)}) |
| 617 | } else if field.Type == fd.IN_PORT { |
| 618 | classifierInfo[IN_PORT] = field.GetPort() |
| 619 | log.Debug("field-type-in-port", log.Fields{"classifierInfo[IN_PORT]": classifierInfo[IN_PORT].(uint32)}) |
| 620 | } else if field.Type == fd.VLAN_VID { |
| 621 | classifierInfo[VLAN_VID] = field.GetVlanVid() |
| 622 | log.Debug("field-type-vlan-vid", log.Fields{"classifierInfo[VLAN_VID]": classifierInfo[VLAN_VID].(uint32)}) |
| 623 | } else if field.Type == fd.VLAN_PCP { |
| 624 | classifierInfo[VLAN_PCP] = field.GetVlanPcp() |
| 625 | log.Debug("field-type-vlan-pcp", log.Fields{"classifierInfo[VLAN_PCP]": classifierInfo[VLAN_PCP].(uint32)}) |
| 626 | } else if field.Type == fd.UDP_DST { |
| 627 | classifierInfo[UDP_DST] = field.GetUdpDst() |
| 628 | log.Debug("field-type-udp-dst", log.Fields{"classifierInfo[UDP_DST]": classifierInfo[UDP_DST].(uint32)}) |
| 629 | } else if field.Type == fd.UDP_SRC { |
| 630 | classifierInfo[UDP_SRC] = field.GetUdpSrc() |
| 631 | log.Debug("field-type-udp-src", log.Fields{"classifierInfo[UDP_SRC]": classifierInfo[UDP_SRC].(uint32)}) |
| 632 | } else if field.Type == fd.IPV4_DST { |
| 633 | classifierInfo[IPV4_DST] = field.GetIpv4Dst() |
| 634 | log.Debug("field-type-ipv4-dst", log.Fields{"classifierInfo[IPV4_DST]": classifierInfo[IPV4_DST].(uint32)}) |
| 635 | } else if field.Type == fd.IPV4_SRC { |
| 636 | classifierInfo[IPV4_SRC] = field.GetIpv4Src() |
| 637 | log.Debug("field-type-ipv4-src", log.Fields{"classifierInfo[IPV4_SRC]": classifierInfo[IPV4_SRC].(uint32)}) |
| 638 | } else if field.Type == fd.METADATA { |
| 639 | classifierInfo[METADATA] = field.GetTableMetadata() |
| 640 | log.Debug("field-type-metadata", log.Fields{"classifierInfo[METADATA]": classifierInfo[METADATA].(uint64)}) |
| 641 | } else { |
| 642 | log.Errorw("Un supported field type", log.Fields{"type": field.Type}) |
| 643 | return |
| 644 | } |
| 645 | } |
| 646 | for _, action := range fd.GetActions(flow) { |
| 647 | if action.Type == fd.OUTPUT { |
| 648 | if out := action.GetOutput(); out != nil { |
| 649 | actionInfo[OUTPUT] = out.GetPort() |
| 650 | log.Debugw("action-type-output", log.Fields{"out_port": actionInfo[OUTPUT].(uint32)}) |
| 651 | } else { |
| 652 | log.Error("Invalid output port in action") |
| 653 | return |
| 654 | } |
| 655 | } else if action.Type == fd.POP_VLAN { |
| 656 | if gotoTable := fd.GetGotoTableId(flow); gotoTable == 0 { |
| 657 | log.Infow("action-type-pop-vlan, being taken care of by ONU", log.Fields{"flow": flow}) |
| 658 | actionInfo[POP_VLAN] = false |
| 659 | return |
| 660 | } |
| 661 | actionInfo[POP_VLAN] = true |
| 662 | log.Debugw("action-type-pop-vlan", log.Fields{"in_port": classifierInfo[IN_PORT].(uint32)}) |
| 663 | } else if action.Type == fd.PUSH_VLAN { |
| 664 | if out := action.GetPush(); out != nil { |
| 665 | if tpid := out.GetEthertype(); tpid != 0x8100 { |
| 666 | log.Errorw("Invalid ethertype in push action", log.Fields{"ethertype": actionInfo[PUSH_VLAN].(int32)}) |
| 667 | } else { |
| 668 | actionInfo[PUSH_VLAN] = true |
| 669 | actionInfo[TPID] = tpid |
| 670 | log.Debugw("action-type-push-vlan", |
| 671 | log.Fields{"push_tpid": actionInfo[TPID].(uint32), "in_port": classifierInfo[IN_PORT].(uint32)}) |
| 672 | } |
| 673 | } |
| 674 | } else if action.Type == fd.SET_FIELD { |
| 675 | if out := action.GetSetField(); out != nil { |
| 676 | if field := out.GetField(); field != nil { |
| 677 | if ofClass := field.GetOxmClass(); ofClass != ofp.OfpOxmClass_OFPXMC_OPENFLOW_BASIC { |
| 678 | log.Errorw("Invalid openflow class", log.Fields{"class": ofClass}) |
| 679 | return |
| 680 | } |
| 681 | /*log.Debugw("action-type-set-field",log.Fields{"field": field, "in_port": classifierInfo[IN_PORT].(uint32)})*/ |
| 682 | if ofbField := field.GetOfbField(); ofbField != nil { |
| 683 | if fieldtype := ofbField.GetType(); fieldtype == ofp.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID { |
| 684 | if vlan := ofbField.GetVlanVid(); vlan != 0 { |
| 685 | actionInfo[VLAN_VID] = vlan & 0xfff |
| 686 | log.Debugw("action-set-vlan-vid", log.Fields{"actionInfo[VLAN_VID]": actionInfo[VLAN_VID].(uint32)}) |
| 687 | } else { |
| 688 | log.Error("No Invalid vlan id in set vlan-vid action") |
| 689 | } |
| 690 | } else { |
| 691 | log.Errorw("unsupported-action-set-field-type", log.Fields{"type": fieldtype}) |
| 692 | } |
| 693 | } |
| 694 | } |
| 695 | } |
| 696 | } else { |
| 697 | log.Errorw("Un supported action type", log.Fields{"type": action.Type}) |
| 698 | return |
| 699 | } |
| 700 | } |
| 701 | if gotoTable := fd.GetGotoTableId(flow); gotoTable != 0 { |
| 702 | if actionInfo[POP_VLAN] == nil { |
| 703 | log.Infow("Go to table - action-type-pop-vlan, being taken care of by ONU", log.Fields{"flow": flow}) |
| 704 | return |
| 705 | } |
| 706 | } |
| 707 | /* NOTE: This condition will be true when core decompose and provides flows to respective devices separately */ |
| 708 | if _, ok := actionInfo[OUTPUT]; ok == false { |
| 709 | log.Debug("action-go-to-table, get next flow for outport details") |
| 710 | if _, ok := classifierInfo[METADATA]; ok { |
| 711 | if next_flow := findNextFlow(flow); next_flow == nil { |
| 712 | log.Debugw("No next flow found ", log.Fields{"classifier": classifierInfo, "flow": flow}) |
| 713 | return |
| 714 | } else { |
| 715 | actionInfo[OUTPUT] = fd.GetOutPort(next_flow) |
| 716 | log.Debugw("action-next-flow-outport", log.Fields{"actionInfo[OUTPUT]": actionInfo[OUTPUT].(uint32)}) |
| 717 | for _, field := range fd.GetOfbFields(next_flow) { |
| 718 | if field.Type == fd.VLAN_VID { |
| 719 | classifierInfo[METADATA] = field.GetVlanVid() & 0xfff |
| 720 | log.Debugw("next-flow-classifier-metadata", log.Fields{"classifierInfo[METADATA]": classifierInfo[METADATA].(uint64)}) |
| 721 | } |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | } |
| 726 | log.Infow("Flow ports", log.Fields{"classifierInfo_inport": classifierInfo[IN_PORT], "action_output": actionInfo[OUTPUT]}) |
| 727 | portNo, intfId, onuId, uniId := ExtractAccessFromFlow(classifierInfo[IN_PORT].(uint32), actionInfo[OUTPUT].(uint32)) |
| 728 | f.divideAndAddFlow(intfId, onuId, uniId, portNo, classifierInfo, actionInfo, flow) |
| 729 | } |
| 730 | |
| 731 | func (f *OpenOltFlowMgr) sendTPDownloadMsgToChild(intfId uint32, onuId uint32, uniId uint32, uni string) bool { |
| 732 | |
| 733 | onuDevice := f.getOnuChildDevice(intfId, onuId) |
| 734 | if onuDevice == nil { |
| 735 | log.Error("Error while fetching Child device from core", log.Fields{"onuId": onuId}) |
| 736 | return false |
| 737 | } |
| 738 | log.Debugw("Retrived child device from OLT device handler", log.Fields{"device": *onuDevice}) |
| 739 | tpPath := f.getTPpath(intfId, uni) |
| 740 | log.Infow("Load-tech-profile-request-to-brcm-handler", log.Fields{"path": tpPath}) |
| 741 | /* TODO : "Send Event message to ONU device via proxy to download tech profile in go routine |
| 742 | msg = {'proxy_address': onuDevice.proxyAddress, 'uniId': uniId, |
| 743 | 'event': 'download_tech_profile', 'event_data': tp_path} |
| 744 | //Send the event message to the ONU adapter |
| 745 | self.adapter_agent.publish_inter_adapter_message(onu_device.id,msg) |
| 746 | */ |
| 747 | return true |
| 748 | |
| 749 | } |