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