blob: 6bcc3eef39908ae389e30d83894915a1d4f568e0 [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"
manikkaraj kbf256be2019-03-25 00:13:48 +053025 "github.com/opencord/voltha-go/common/log"
26 tp "github.com/opencord/voltha-go/common/techprofile"
27 fd "github.com/opencord/voltha-go/rw_core/flow_decomposition"
Manikkaraj k884c1242019-04-11 16:26:42 +053028 rsrcMgr "github.com/opencord/voltha-openolt-adapter/adaptercore/resourcemanager"
manikkaraj kbf256be2019-03-25 00:13:48 +053029 ofp "github.com/opencord/voltha-protos/go/openflow_13"
Manikkaraj k884c1242019-04-11 16:26:42 +053030 "math/big"
31 // ic "github.com/opencord/voltha-protos/go/inter_container"
manikkaraj kbf256be2019-03-25 00:13:48 +053032 openolt_pb2 "github.com/opencord/voltha-protos/go/openolt"
33 voltha "github.com/opencord/voltha-protos/go/voltha"
34)
35
36const (
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
82type OpenOltFlowMgr struct {
83 techprofile []*tp.TechProfileMgr
84 deviceHandler *DeviceHandler
85 resourceMgr *rsrcMgr.OpenOltResourceMgr
86}
87
88func 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
101func (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
159func (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
225func (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
242func (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
Manikkaraj k884c1242019-04-11 16:26:42 +0530259func (f *OpenOltFlowMgr) addUpstreamDataFlow(intfId uint32, onuId uint32, uniId uint32,
260 portNo uint32, uplinkClassifier map[string]interface{},
261 uplinkAction map[string]interface{}, logicalFlow *ofp.OfpFlowStats,
262 allocId uint32, gemportId uint32) {
263 uplinkClassifier[PACKET_TAG_TYPE] = SINGLE_TAG
264 log.Debugw("Adding upstream data flow", log.Fields{"uplinkClassifier": uplinkClassifier, "uplinkAction": uplinkAction})
265 f.addHSIAFlow(intfId, onuId, uniId, portNo, uplinkClassifier, uplinkAction,
266 UPSTREAM, logicalFlow, allocId, gemportId)
267 /* TODO: Install Secondary EAP on the subscriber vlan */
manikkaraj kbf256be2019-03-25 00:13:48 +0530268}
269
Manikkaraj k884c1242019-04-11 16:26:42 +0530270func (f *OpenOltFlowMgr) addDownstreamDataFlow(intfId uint32, onuId uint32, uniId uint32,
271 portNo uint32, downlinkClassifier map[string]interface{},
272 downlinkAction map[string]interface{}, logicalFlow *ofp.OfpFlowStats,
273 allocId uint32, gemportId uint32) {
274 downlinkClassifier[PACKET_TAG_TYPE] = DOUBLE_TAG
275 log.Debugw("Adding downstream data flow", log.Fields{"downlinkClassifier": downlinkClassifier,
276 "downlinkAction": downlinkAction})
277 if uint32(downlinkClassifier[METADATA].(uint64)) == MkUniPortNum(intfId, onuId, uniId) &&
278 downlinkClassifier[VLAN_VID] == (uint32(ofp.OfpVlanId_OFPVID_PRESENT)|4000) {
279 log.Infow("EAPOL DL flow , Already added ,ignoring it", log.Fields{"downlinkClassifier": downlinkClassifier,
280 "downlinkAction": downlinkAction})
281 return
282 }
283 /* Already this info available classifier? */
284 downlinkAction[POP_VLAN] = true
285 downlinkAction[VLAN_VID] = downlinkClassifier[VLAN_VID]
286 f.addHSIAFlow(intfId, onuId, uniId, portNo, downlinkClassifier, downlinkAction,
287 DOWNSTREAM, logicalFlow, allocId, gemportId)
manikkaraj kbf256be2019-03-25 00:13:48 +0530288}
289
Manikkaraj k884c1242019-04-11 16:26:42 +0530290func (f *OpenOltFlowMgr) addHSIAFlow(intfId uint32, onuId uint32, uniId uint32, portNo uint32, classifier map[string]interface{},
291 action map[string]interface{}, direction string, logicalFlow *ofp.OfpFlowStats,
292 allocId uint32, gemPortId uint32) {
293 /* One of the OLT platform (Broadcom BAL) requires that symmetric
294 flows require the same flow_id to be used across UL and DL.
295 Since HSIA flow is the only symmetric flow currently, we need to
296 re-use the flow_id across both direction. The 'flow_category'
297 takes priority over flow_cookie to find any available HSIA_FLOW
298 id for the ONU.
299 */
300 log.Debugw("Adding HSIA flow", log.Fields{"intfId": intfId, "onuId": onuId, "uniId": uniId, "classifier": classifier,
301 "action": action, "direction": direction, "allocId": allocId, "gemPortId": gemPortId,
302 "logicalFlow": *logicalFlow})
303 flowStoreCookie := getFlowStoreCookie(classifier, gemPortId)
304 flowId, err := f.resourceMgr.GetFlowID(intfId, onuId, uniId, flowStoreCookie, "HSIA")
305 if err != nil {
306 log.Errorw("Flow id unavailable for HSIA flow", log.Fields{"direction": direction})
307 return
308 }
309 var classifierProto *openolt_pb2.Classifier
310 var actionProto *openolt_pb2.Action
311 if classifierProto = makeOpenOltClassifierField(classifier); classifierProto == nil {
312 log.Error("Error in making classifier protobuf for hsia flow")
313 return
314 }
315 log.Debugw("Created classifier proto", log.Fields{"classifier": *classifierProto})
316 if actionProto = makeOpenOltActionField(action); actionProto == nil {
317 log.Errorw("Error in making action protobuf for hsia flow", log.Fields{"direction": direction})
318 return
319 }
320 log.Debugw("Created action proto", log.Fields{"action": *actionProto})
321 flow := openolt_pb2.Flow{AccessIntfId: int32(intfId),
322 OnuId: int32(onuId),
323 UniId: int32(uniId),
324 FlowId: flowId,
325 FlowType: direction,
326 AllocId: int32(allocId),
327 NetworkIntfId: DEFAULT_NETWORK_INTERFACE_ID, // one NNI port is supported now
328 GemportId: int32(gemPortId),
329 Classifier: classifierProto,
330 Action: actionProto,
331 Priority: int32(logicalFlow.Priority),
332 Cookie: logicalFlow.Cookie,
333 PortNo: portNo}
334 if ok := f.addFlowToDevice(&flow); ok {
335 log.Debug("HSIA flow added to device successfully", log.Fields{"direction": direction})
336 flowsToKVStore := f.getUpdatedFlowInfo(&flow, flowStoreCookie, "HSIA")
337 if err := f.updateFlowInfoToKVStore(flow.AccessIntfId,
338 flow.OnuId,
339 flow.UniId,
340 flow.FlowId, flowsToKVStore); err != nil {
341 log.Errorw("Error uploading HSIA flow into KV store", log.Fields{"flow": flow, "direction": direction, "error": err})
342 return
343 }
344 }
345}
manikkaraj kbf256be2019-03-25 00:13:48 +0530346func (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) {
347 return
348}
349
350// Add EAPOL to device
351func (f *OpenOltFlowMgr) addEAPOLFlow(intfId uint32, onuId uint32, uniId uint32, portNo uint32, logicalFlow *ofp.OfpFlowStats, allocId uint32, gemPortId uint32, vlanId uint32) {
352 log.Debugw("Adding EAPOL to device", log.Fields{"intfId": intfId, "onuId": onuId, "portNo": portNo, "allocId": allocId, "gemPortId": gemPortId, "vlanId": vlanId, "flow": logicalFlow})
353
354 uplinkClassifier := make(map[string]interface{})
355 uplinkAction := make(map[string]interface{})
356 downlinkClassifier := make(map[string]interface{})
357 downlinkAction := make(map[string]interface{})
358 var upstreamFlow openolt_pb2.Flow
359 var downstreamFlow openolt_pb2.Flow
360
361 // Fill Classfier
362 uplinkClassifier[ETH_TYPE] = uint32(EAP_ETH_TYPE)
363 uplinkClassifier[PACKET_TAG_TYPE] = SINGLE_TAG
364 uplinkClassifier[VLAN_VID] = vlanId
365 // Fill action
366 uplinkAction[TRAP_TO_HOST] = true
367 flowStoreCookie := getFlowStoreCookie(uplinkClassifier, gemPortId)
368 //Add Uplink EAPOL Flow
369 uplinkFlowId, err := f.resourceMgr.GetFlowID(intfId, onuId, uniId, flowStoreCookie, "")
370 if err != nil {
Manikkaraj k884c1242019-04-11 16:26:42 +0530371 log.Errorw("flowId unavailable for UL EAPOL", log.Fields{"intfId": intfId, "onuId": onuId, "flowStoreCookie": flowStoreCookie})
372 return
manikkaraj kbf256be2019-03-25 00:13:48 +0530373 }
374 var classifierProto *openolt_pb2.Classifier
375 var actionProto *openolt_pb2.Action
376 log.Debugw("Creating UL EAPOL flow", log.Fields{"ul_classifier": uplinkClassifier, "ul_action": uplinkAction, "uplinkFlowId": uplinkFlowId})
377
378 if classifierProto = makeOpenOltClassifierField(uplinkClassifier); classifierProto == nil {
379 log.Error("Error in making classifier protobuf for ul flow")
380 return
381 }
382 log.Debugw("Created classifier proto", log.Fields{"classifier": *classifierProto})
383 if actionProto = makeOpenOltActionField(uplinkAction); actionProto == nil {
384 log.Error("Error in making action protobuf for ul flow")
385 return
386 }
387 log.Debugw("Created action proto", log.Fields{"action": *actionProto})
388 upstreamFlow = openolt_pb2.Flow{AccessIntfId: int32(intfId),
389 OnuId: int32(onuId),
390 UniId: int32(uniId),
391 FlowId: uplinkFlowId,
392 FlowType: UPSTREAM,
393 AllocId: int32(allocId),
394 NetworkIntfId: DEFAULT_NETWORK_INTERFACE_ID, // one NNI port is supported now
395 GemportId: int32(gemPortId),
396 Classifier: classifierProto,
397 Action: actionProto,
398 Priority: int32(logicalFlow.Priority),
399 Cookie: logicalFlow.Cookie,
400 PortNo: portNo}
401 if ok := f.addFlowToDevice(&upstreamFlow); ok {
402 log.Debug("EAPOL UL flow added to device successfully")
403 flowsToKVStore := f.getUpdatedFlowInfo(&upstreamFlow, flowStoreCookie, "EAPOL")
404 if err := f.updateFlowInfoToKVStore(upstreamFlow.AccessIntfId,
405 upstreamFlow.OnuId,
406 upstreamFlow.UniId,
407 upstreamFlow.FlowId, flowsToKVStore); err != nil {
408 log.Errorw("Error uploading EAPOL UL flow into KV store", log.Fields{"flow": upstreamFlow, "error": err})
409 return
410 }
411 }
412
413 if vlanId == DEFAULT_MGMT_VLAN {
414 /* Add Downstream EAPOL Flow, Only for first EAP flow (BAL
415 # requirement)
416 # On one of the platforms (Broadcom BAL), when same DL classifier
417 # vlan was used across multiple ONUs, eapol flow re-adds after
418 # flow delete (cases of onu reboot/disable) fails.
419 # In order to generate unique vlan, a combination of intf_id
420 # onu_id and uniId is used.
421 # uniId defaults to 0, so add 1 to it.
422 */
423 log.Debugw("Creating DL EAPOL flow with default vlan", log.Fields{"vlan": vlanId})
424 specialVlanDlFlow := 4090 - intfId*onuId*(uniId+1)
425 // Assert that we do not generate invalid vlans under no condition
426 if specialVlanDlFlow <= 2 {
427 log.Fatalw("invalid-vlan-generated", log.Fields{"vlan": specialVlanDlFlow})
428 return
429 }
430 log.Debugw("specialVlanEAPOLDlFlow:", log.Fields{"dl_vlan": specialVlanDlFlow})
431 // Fill Classfier
432 downlinkClassifier[PACKET_TAG_TYPE] = SINGLE_TAG
433 downlinkClassifier[VLAN_VID] = uint32(specialVlanDlFlow)
434 // Fill action
435 downlinkAction[PUSH_VLAN] = true
436 downlinkAction[VLAN_VID] = vlanId
437 flowStoreCookie := getFlowStoreCookie(downlinkClassifier, gemPortId)
438 downlinkFlowId, err := f.resourceMgr.GetFlowID(intfId, onuId, uniId, flowStoreCookie, "")
439 if err != nil {
Manikkaraj k884c1242019-04-11 16:26:42 +0530440 log.Errorw("flowId unavailable for DL EAPOL",
manikkaraj kbf256be2019-03-25 00:13:48 +0530441 log.Fields{"intfId": intfId, "onuId": onuId, "flowStoreCookie": flowStoreCookie})
442 return
443 }
444 log.Debugw("Creating DL EAPOL flow",
445 log.Fields{"dl_classifier": downlinkClassifier, "dl_action": downlinkAction, "downlinkFlowId": downlinkFlowId})
446 if classifierProto = makeOpenOltClassifierField(downlinkClassifier); classifierProto == nil {
447 log.Error("Error in making classifier protobuf for downlink flow")
448 return
449 }
450 if actionProto = makeOpenOltActionField(downlinkAction); actionProto == nil {
451 log.Error("Error in making action protobuf for dl flow")
452 return
453 }
454 // Downstream flow in grpc protobuf
455 downstreamFlow = openolt_pb2.Flow{AccessIntfId: int32(intfId),
456 OnuId: int32(onuId),
457 UniId: int32(uniId),
458 FlowId: downlinkFlowId,
459 FlowType: DOWNSTREAM,
460 AllocId: int32(allocId),
461 NetworkIntfId: DEFAULT_NETWORK_INTERFACE_ID,
462 GemportId: int32(gemPortId),
463 Classifier: classifierProto,
464 Action: actionProto,
465 Priority: int32(logicalFlow.Priority),
466 Cookie: logicalFlow.Cookie,
467 PortNo: portNo}
468 if ok := f.addFlowToDevice(&downstreamFlow); ok {
469 log.Debug("EAPOL DL flow added to device successfully")
470 flowsToKVStore := f.getUpdatedFlowInfo(&downstreamFlow, flowStoreCookie, "")
471 if err := f.updateFlowInfoToKVStore(downstreamFlow.AccessIntfId,
472 downstreamFlow.OnuId,
473 downstreamFlow.UniId,
474 downstreamFlow.FlowId, flowsToKVStore); err != nil {
475 log.Errorw("Error uploading EAPOL DL flow into KV store", log.Fields{"flow": upstreamFlow, "error": err})
476 return
477 }
478 }
479 } else {
480 log.Infow("EAPOL flow with non-default mgmt vlan is not supported", log.Fields{"vlanId": vlanId})
481 return
482 }
483 log.Debugw("Added EAPOL flows to device successfully", log.Fields{"flow": logicalFlow})
484}
485
486func makeOpenOltClassifierField(classifierInfo map[string]interface{}) *openolt_pb2.Classifier {
487 var classifier openolt_pb2.Classifier
488 if etherType, ok := classifierInfo[ETH_TYPE]; ok {
489 classifier.EthType = etherType.(uint32)
490 }
491 if ipProto, ok := classifierInfo[IP_PROTO]; ok {
492 classifier.IpProto = ipProto.(uint32)
493 }
494 if vlanId, ok := classifierInfo[VLAN_VID]; ok {
495 classifier.OVid = vlanId.(uint32)
496 }
Manikkaraj k884c1242019-04-11 16:26:42 +0530497 if metadata, ok := classifierInfo[METADATA]; ok { // TODO: Revisit
498 classifier.IVid = uint32(metadata.(uint64))
manikkaraj kbf256be2019-03-25 00:13:48 +0530499 }
500 if vlanPcp, ok := classifierInfo[VLAN_PCP]; ok {
501 classifier.OPbits = vlanPcp.(uint32)
502 }
503 if udpSrc, ok := classifierInfo[UDP_SRC]; ok {
504 classifier.SrcPort = udpSrc.(uint32)
505 }
506 if udpDst, ok := classifierInfo[UDP_DST]; ok {
507 classifier.DstPort = udpDst.(uint32)
508 }
509 if ipv4Dst, ok := classifierInfo[IPV4_DST]; ok {
510 classifier.DstIp = ipv4Dst.(uint32)
511 }
512 if ipv4Src, ok := classifierInfo[IPV4_SRC]; ok {
513 classifier.SrcIp = ipv4Src.(uint32)
514 }
515 if pktTagType, ok := classifierInfo[PACKET_TAG_TYPE]; ok {
516 if pktTagType.(string) == SINGLE_TAG {
517 classifier.PktTagType = SINGLE_TAG
518 } else if pktTagType.(string) == DOUBLE_TAG {
519 classifier.PktTagType = DOUBLE_TAG
520 } else if pktTagType.(string) == UNTAGGED {
521 classifier.PktTagType = UNTAGGED
522 } else {
523 log.Error("Invalid tag type in classifier") // should not hit
524 return nil
525 }
526 }
527 return &classifier
528}
529
530func makeOpenOltActionField(actionInfo map[string]interface{}) *openolt_pb2.Action {
531 var actionCmd openolt_pb2.ActionCmd
532 var action openolt_pb2.Action
533 action.Cmd = &actionCmd
534 if _, ok := actionInfo[POP_VLAN]; ok {
535 action.OVid = actionInfo[VLAN_VID].(uint32)
536 action.Cmd.RemoveOuterTag = true
537 } else if _, ok := actionInfo[PUSH_VLAN]; ok {
538 action.OVid = actionInfo[VLAN_VID].(uint32)
539 action.Cmd.AddOuterTag = true
540 } else if _, ok := actionInfo[TRAP_TO_HOST]; ok {
541 action.Cmd.TrapToHost = actionInfo[TRAP_TO_HOST].(bool)
542 } else {
543 log.Errorw("Invalid-action-field", log.Fields{"action": actionInfo})
544 return nil
545 }
546 return &action
547}
548
549func (f *OpenOltFlowMgr) getTPpath(intfId uint32, uni string) string {
550 /*
551 FIXME
552 Should get Table id form the flow, as of now hardcoded to DEFAULT_TECH_PROFILE_TABLE_ID (64)
553 'tp_path' contains the suffix part of the tech_profile_instance path. The prefix to the 'tp_path' should be set to
554 TechProfile.KV_STORE_TECH_PROFILE_PATH_PREFIX by the ONU adapter.
555 */
556 return f.techprofile[intfId].GetTechProfileInstanceKVPath(tp.DEFAULT_TECH_PROFILE_TABLE_ID, uni)
557}
558
559func getFlowStoreCookie(classifier map[string]interface{}, gemPortId uint32) uint64 {
560 if len(classifier) == 0 { // should never happen
561 log.Error("Invalid classfier object")
562 return 0
563 }
564 var jsonData []byte
565 var flowString string
566 var err error
567 // TODO: Do we need to marshall ??
568 if jsonData, err = json.Marshal(classifier); err != nil {
569 log.Error("Failed to encode classifier")
570 return 0
571 }
572 flowString = string(jsonData)
573 if gemPortId != 0 {
574 flowString = fmt.Sprintf("%s%s", string(jsonData), string(gemPortId))
575 }
576 h := md5.New()
577 h.Write([]byte(flowString))
578 hash := big.NewInt(0)
579 hash.SetBytes(h.Sum(nil))
580 return hash.Uint64()
581}
582
583func (f *OpenOltFlowMgr) getUpdatedFlowInfo(flow *openolt_pb2.Flow, flowStoreCookie uint64, flowCategory string) *[]openolt_pb2.Flow {
584 var flows []openolt_pb2.Flow
585 /* FIXME: To be removed and identify way to get same flow ID for HSIA DL and UL flows
586 To get existing flow matching flow catogery or cookie
587 */
588 /*flow.Category = flowCategory
589 flow.FlowStoreCookie = flowStoreCookie*/
590 flows = append(flows, *flow)
591 // Get existing flow for flowid from KV store
592 //existingFlows := f.resourceMgr.GetFlowIDInfo(uint32(flow.AccessIntfId),uint32(flow.OnuId),uint32(flow.UniId),flow.FlowId)
593 /*existingFlows := nil
594 if existingFlows != nil{
595 log.Debugw("Flow exists for given flowID, appending it",log.Fields{"flowID":flow.FlowId})
596 for _,f := range *existingFlows{
597 flows = append(flows,f)
598 }
599 }*/
600 log.Debugw("Updated flows for given flowID", log.Fields{"updatedflow": flows, "flowid": flow.FlowId})
601 return &flows
602}
603
604func (f *OpenOltFlowMgr) updateFlowInfoToKVStore(intfId int32, onuId int32, uniId int32, flowId uint32, flows *[]openolt_pb2.Flow) error {
605 log.Debugw("Storing flow into KV store", log.Fields{"flows": *flows})
606 /* FIXME: To implement API in resource mgr and invoke */
607 /*if err := f.resourceMgr.UpdateFlowIDInfo(intfId,onuId,uniId,flowId,flows); err != nil{
608 log.Debug("Error while Storing flow into KV store")
609 return err
610 }
611 log.Info("Stored flow into KV store successfully!")
612 */
613 return nil
614}
615
616func (f *OpenOltFlowMgr) addFlowToDevice(deviceFlow *openolt_pb2.Flow) bool {
617 log.Debugw("Sending flow to device via grpc", log.Fields{"flow": *deviceFlow})
618 _, err := f.deviceHandler.Client.FlowAdd(context.Background(), deviceFlow)
619 if err != nil {
620 log.Errorw("Failed to Add flow to device", log.Fields{"err": err, "deviceFlow": deviceFlow})
621 return false
622 }
623 log.Debugw("Flow added to device successfuly ", log.Fields{"flow": *deviceFlow})
624 return true
625}
626
627/*func register_flow(deviceFlow *openolt_pb2.Flow, logicalFlow *ofp.OfpFlowStats){
628 //update core flows_proxy : flows_proxy.update('/', flows)
629}
630
631func generateStoredId(flowId uint32, direction string)uint32{
632
633 if direction == UPSTREAM{
634 log.Debug("Upstream flow shifting flowid")
635 return ((0x1 << 15) | flowId)
636 }else if direction == DOWNSTREAM{
637 log.Debug("Downstream flow not shifting flowid")
638 return flowId
639 }else{
640 log.Errorw("Unrecognized direction",log.Fields{"direction": direction})
641 return flowId
642 }
643}
644
645*/
646
647func addLLDPFlow(flow *ofp.OfpFlowStats, portNo uint32) {
648 log.Info("Unimplemented")
649}
650func getUniPortPath(intfId uint32, onuId uint32, uniId uint32) string {
651 return fmt.Sprintf("pon-{%d}/onu-{%d}/uni-{%d}", intfId, onuId, uniId)
652}
653
Manikkaraj k884c1242019-04-11 16:26:42 +0530654func (f *OpenOltFlowMgr) getOnuChildDevice(intfId uint32, onuId uint32) (*voltha.Device, error) {
655 log.Debugw("GetChildDevice", log.Fields{"pon port": intfId, "onuId": onuId})
656 //parentPortNo := IntfIdToPortNo(intfId, voltha.Port_PON_OLT)
657 parentPortNo := intfId
658 onuDevice := f.deviceHandler.GetChildDevice(parentPortNo, onuId)
659 if onuDevice == nil {
660 log.Errorw("onu not found", log.Fields{"intfId": parentPortNo, "onuId": onuId})
661 return nil, errors.New("onu not found")
manikkaraj kbf256be2019-03-25 00:13:48 +0530662 }
Manikkaraj k884c1242019-04-11 16:26:42 +0530663 log.Debugw("Successfully received child device from core", log.Fields{"child_device": *onuDevice})
664 return onuDevice, nil
manikkaraj kbf256be2019-03-25 00:13:48 +0530665}
666
667func findNextFlow(flow *ofp.OfpFlowStats) *ofp.OfpFlowStats {
668 log.Info("Unimplemented")
669 return nil
670}
671
672func getSubscriberVlan(inPort uint32) uint32 {
673 /* For EAPOL case we will use default VLAN , so will implement later if required */
674 log.Info("Unimplemented")
675 return 0
676}
677
678func (f *OpenOltFlowMgr) clear_flows_and_scheduler_for_logical_port(childDevice *voltha.Device, logicalPort *voltha.LogicalPort) {
679 log.Info("Unimplemented")
680}
681
682func (f *OpenOltFlowMgr) AddFlow(flow *ofp.OfpFlowStats) {
683 classifierInfo := make(map[string]interface{}, 0)
684 actionInfo := make(map[string]interface{}, 0)
685 log.Debug("Adding Flow", log.Fields{"flow": flow})
686 for _, field := range fd.GetOfbFields(flow) {
687 if field.Type == fd.ETH_TYPE {
688 classifierInfo[ETH_TYPE] = field.GetEthType()
689 log.Debug("field-type-eth-type", log.Fields{"classifierInfo[ETH_TYPE]": classifierInfo[ETH_TYPE].(uint32)})
690 } else if field.Type == fd.IP_PROTO {
691 classifierInfo[IP_PROTO] = field.GetIpProto()
692 log.Debug("field-type-ip-proto", log.Fields{"classifierInfo[IP_PROTO]": classifierInfo[IP_PROTO].(uint32)})
693 } else if field.Type == fd.IN_PORT {
694 classifierInfo[IN_PORT] = field.GetPort()
695 log.Debug("field-type-in-port", log.Fields{"classifierInfo[IN_PORT]": classifierInfo[IN_PORT].(uint32)})
696 } else if field.Type == fd.VLAN_VID {
697 classifierInfo[VLAN_VID] = field.GetVlanVid()
698 log.Debug("field-type-vlan-vid", log.Fields{"classifierInfo[VLAN_VID]": classifierInfo[VLAN_VID].(uint32)})
699 } else if field.Type == fd.VLAN_PCP {
700 classifierInfo[VLAN_PCP] = field.GetVlanPcp()
701 log.Debug("field-type-vlan-pcp", log.Fields{"classifierInfo[VLAN_PCP]": classifierInfo[VLAN_PCP].(uint32)})
702 } else if field.Type == fd.UDP_DST {
703 classifierInfo[UDP_DST] = field.GetUdpDst()
704 log.Debug("field-type-udp-dst", log.Fields{"classifierInfo[UDP_DST]": classifierInfo[UDP_DST].(uint32)})
705 } else if field.Type == fd.UDP_SRC {
706 classifierInfo[UDP_SRC] = field.GetUdpSrc()
707 log.Debug("field-type-udp-src", log.Fields{"classifierInfo[UDP_SRC]": classifierInfo[UDP_SRC].(uint32)})
708 } else if field.Type == fd.IPV4_DST {
709 classifierInfo[IPV4_DST] = field.GetIpv4Dst()
710 log.Debug("field-type-ipv4-dst", log.Fields{"classifierInfo[IPV4_DST]": classifierInfo[IPV4_DST].(uint32)})
711 } else if field.Type == fd.IPV4_SRC {
712 classifierInfo[IPV4_SRC] = field.GetIpv4Src()
713 log.Debug("field-type-ipv4-src", log.Fields{"classifierInfo[IPV4_SRC]": classifierInfo[IPV4_SRC].(uint32)})
714 } else if field.Type == fd.METADATA {
715 classifierInfo[METADATA] = field.GetTableMetadata()
716 log.Debug("field-type-metadata", log.Fields{"classifierInfo[METADATA]": classifierInfo[METADATA].(uint64)})
717 } else {
718 log.Errorw("Un supported field type", log.Fields{"type": field.Type})
719 return
720 }
721 }
722 for _, action := range fd.GetActions(flow) {
723 if action.Type == fd.OUTPUT {
724 if out := action.GetOutput(); out != nil {
725 actionInfo[OUTPUT] = out.GetPort()
726 log.Debugw("action-type-output", log.Fields{"out_port": actionInfo[OUTPUT].(uint32)})
727 } else {
728 log.Error("Invalid output port in action")
729 return
730 }
731 } else if action.Type == fd.POP_VLAN {
Manikkaraj k884c1242019-04-11 16:26:42 +0530732 /*if gotoTable := fd.GetGotoTableId(flow); gotoTable == 0 {
manikkaraj kbf256be2019-03-25 00:13:48 +0530733 log.Infow("action-type-pop-vlan, being taken care of by ONU", log.Fields{"flow": flow})
734 actionInfo[POP_VLAN] = false
735 return
Manikkaraj k884c1242019-04-11 16:26:42 +0530736 }*/
manikkaraj kbf256be2019-03-25 00:13:48 +0530737 actionInfo[POP_VLAN] = true
738 log.Debugw("action-type-pop-vlan", log.Fields{"in_port": classifierInfo[IN_PORT].(uint32)})
739 } else if action.Type == fd.PUSH_VLAN {
740 if out := action.GetPush(); out != nil {
741 if tpid := out.GetEthertype(); tpid != 0x8100 {
742 log.Errorw("Invalid ethertype in push action", log.Fields{"ethertype": actionInfo[PUSH_VLAN].(int32)})
743 } else {
744 actionInfo[PUSH_VLAN] = true
745 actionInfo[TPID] = tpid
746 log.Debugw("action-type-push-vlan",
747 log.Fields{"push_tpid": actionInfo[TPID].(uint32), "in_port": classifierInfo[IN_PORT].(uint32)})
748 }
749 }
750 } else if action.Type == fd.SET_FIELD {
751 if out := action.GetSetField(); out != nil {
752 if field := out.GetField(); field != nil {
753 if ofClass := field.GetOxmClass(); ofClass != ofp.OfpOxmClass_OFPXMC_OPENFLOW_BASIC {
754 log.Errorw("Invalid openflow class", log.Fields{"class": ofClass})
755 return
756 }
757 /*log.Debugw("action-type-set-field",log.Fields{"field": field, "in_port": classifierInfo[IN_PORT].(uint32)})*/
758 if ofbField := field.GetOfbField(); ofbField != nil {
759 if fieldtype := ofbField.GetType(); fieldtype == ofp.OxmOfbFieldTypes_OFPXMT_OFB_VLAN_VID {
760 if vlan := ofbField.GetVlanVid(); vlan != 0 {
761 actionInfo[VLAN_VID] = vlan & 0xfff
762 log.Debugw("action-set-vlan-vid", log.Fields{"actionInfo[VLAN_VID]": actionInfo[VLAN_VID].(uint32)})
763 } else {
764 log.Error("No Invalid vlan id in set vlan-vid action")
765 }
766 } else {
767 log.Errorw("unsupported-action-set-field-type", log.Fields{"type": fieldtype})
768 }
769 }
770 }
771 }
772 } else {
773 log.Errorw("Un supported action type", log.Fields{"type": action.Type})
774 return
775 }
776 }
Manikkaraj k884c1242019-04-11 16:26:42 +0530777 /*if gotoTable := fd.GetGotoTableId(flow); gotoTable != 0 {
manikkaraj kbf256be2019-03-25 00:13:48 +0530778 if actionInfo[POP_VLAN] == nil {
779 log.Infow("Go to table - action-type-pop-vlan, being taken care of by ONU", log.Fields{"flow": flow})
780 return
781 }
Manikkaraj k884c1242019-04-11 16:26:42 +0530782 }*/
783 /* NOTE: This condition will be false when core decompose and provides flows to respective devices separately */
784 /* if _,ok := actionInfo[OUTPUT]; ok == false{
manikkaraj kbf256be2019-03-25 00:13:48 +0530785 log.Debug("action-go-to-table, get next flow for outport details")
786 if _, ok := classifierInfo[METADATA]; ok {
787 if next_flow := findNextFlow(flow); next_flow == nil {
788 log.Debugw("No next flow found ", log.Fields{"classifier": classifierInfo, "flow": flow})
789 return
790 } else {
791 actionInfo[OUTPUT] = fd.GetOutPort(next_flow)
792 log.Debugw("action-next-flow-outport", log.Fields{"actionInfo[OUTPUT]": actionInfo[OUTPUT].(uint32)})
793 for _, field := range fd.GetOfbFields(next_flow) {
794 if field.Type == fd.VLAN_VID {
795 classifierInfo[METADATA] = field.GetVlanVid() & 0xfff
796 log.Debugw("next-flow-classifier-metadata", log.Fields{"classifierInfo[METADATA]": classifierInfo[METADATA].(uint64)})
797 }
798 }
799 }
800 }
Manikkaraj k884c1242019-04-11 16:26:42 +0530801 }*/
manikkaraj kbf256be2019-03-25 00:13:48 +0530802 log.Infow("Flow ports", log.Fields{"classifierInfo_inport": classifierInfo[IN_PORT], "action_output": actionInfo[OUTPUT]})
803 portNo, intfId, onuId, uniId := ExtractAccessFromFlow(classifierInfo[IN_PORT].(uint32), actionInfo[OUTPUT].(uint32))
804 f.divideAndAddFlow(intfId, onuId, uniId, portNo, classifierInfo, actionInfo, flow)
805}
806
Manikkaraj k884c1242019-04-11 16:26:42 +0530807func (f *OpenOltFlowMgr) sendTPDownloadMsgToChild(intfId uint32, onuId uint32, uniId uint32, uni string) error {
manikkaraj kbf256be2019-03-25 00:13:48 +0530808
Manikkaraj k884c1242019-04-11 16:26:42 +0530809 onuDevice, err := f.getOnuChildDevice(intfId, onuId)
810 if err != nil {
811 log.Errorw("Error while fetching Child device from core", log.Fields{"onuId": onuId})
812 return err
manikkaraj kbf256be2019-03-25 00:13:48 +0530813 }
Manikkaraj k884c1242019-04-11 16:26:42 +0530814 log.Debugw("Got child device from OLT device handler", log.Fields{"device": *onuDevice})
815 /* TODO: uncomment once voltha-proto is ready with changes */
816 /*
817 tpPath := f.getTPpath(intfId, uni)
818 tpDownloadMsg := &ic.TechProfileDownload{UniId: uniId, Path: tpPath}
819 var tpDownloadMsg interface{}
820 log.Infow("Sending Load-tech-profile-request-to-brcm-onu-adapter",log.Fields{"msg": *tpDownloadMsg})
821 sendErr := f.deviceHandler.AdapterProxy.SendInterAdapterMessage(context.Background(),
822 tpDownloadMsg,
823 //ic.InterAdapterMessageType_TECH_PROFILE_DOWNLOAD_REQUEST,
824 ic.InterAdapterMessageType_OMCI_REQUEST,
825 f.deviceHandler.deviceType,
826 onuDevice.Type,
827 onuDevice.Id,
828 onuDevice.ProxyAddress.DeviceId, "")
829 if sendErr != nil {
830 log.Errorw("send techprofile-download request error", log.Fields{"fromAdapter": f.deviceHandler.deviceType,
831 "toAdapter": onuDevice.Type, "onuId": onuDevice.Id,
832 "proxyDeviceId": onuDevice.ProxyAddress.DeviceId})
833 return sendErr
834 }
835 log.Debugw("success Sending Load-tech-profile-request-to-brcm-onu-adapter",log.Fields{"msg":tpDownloadMsg})*/
836 return nil
manikkaraj kbf256be2019-03-25 00:13:48 +0530837}