Thiyagarajan Subramani | b83b043 | 2020-01-08 13:43:28 +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 core |
| 18 | |
| 19 | import ( |
| 20 | "errors" |
| 21 | |
| 22 | "github.com/opencord/voltha-lib-go/v2/pkg/log" |
| 23 | oop "github.com/opencord/voltha-protos/v2/go/openolt" |
| 24 | tp_pb "github.com/opencord/voltha-protos/v2/go/tech_profile" |
| 25 | "golang.org/x/net/context" |
| 26 | "google.golang.org/grpc/codes" |
| 27 | "google.golang.org/grpc/status" |
| 28 | ) |
| 29 | |
| 30 | const ( |
| 31 | //Constants utilised while forming HSIA Flow |
| 32 | HsiaFlow = "HSIA_FLOW" |
| 33 | DownStreamHsiaActionOVid = 4108 //Used in Downstream HSIA |
| 34 | |
| 35 | //Constants utilised while forming DHCP Flow |
| 36 | DhcpFlow = "DHCP_FLOW" |
| 37 | IPv4EthType = 0x800 //2048 |
| 38 | DhcpIPProto = 17 |
| 39 | DhcpSrcPort = 68 |
| 40 | DhcpDstPort = 67 |
| 41 | |
| 42 | //Constants utilised while forming EAPOL Flow |
| 43 | EapolFlow = "EAPOL_FLOW" |
| 44 | EapEthType = 0x888e //34958 |
| 45 | |
| 46 | //Direction constant |
| 47 | Upstream = "upstream" |
| 48 | Downstream = "downstream" |
| 49 | |
| 50 | //PacketTagType constant |
| 51 | PacketTagType = "pkt_tag_type" |
| 52 | Untagged = "untagged" |
| 53 | SingleTag = "single_tag" |
| 54 | DoubleTag = "double_tag" |
| 55 | ) |
| 56 | |
| 57 | func getTrafficSched(subs *Subscriber, direction tp_pb.Direction) []*tp_pb.TrafficScheduler { |
| 58 | var SchedCfg *tp_pb.SchedulerConfig |
| 59 | |
| 60 | if direction == tp_pb.Direction_DOWNSTREAM { |
| 61 | SchedCfg = subs.RsrMgr.ResourceMgrs[subs.PonIntf].TechProfileMgr. |
| 62 | GetDsScheduler(subs.TpInstance[subs.TestConfig.TpIDList[0]]) |
| 63 | |
| 64 | } else { |
| 65 | SchedCfg = subs.RsrMgr.ResourceMgrs[subs.PonIntf].TechProfileMgr. |
| 66 | GetUsScheduler(subs.TpInstance[subs.TestConfig.TpIDList[0]]) |
| 67 | } |
| 68 | |
| 69 | // hard-code for now |
| 70 | cir := 16000 |
| 71 | cbs := 5000 |
| 72 | eir := 16000 |
| 73 | ebs := 5000 |
| 74 | pir := cir + eir |
| 75 | pbs := cbs + ebs |
| 76 | |
| 77 | TrafficShaping := &tp_pb.TrafficShapingInfo{Cir: uint32(cir), Cbs: uint32(cbs), Pir: uint32(pir), Pbs: uint32(pbs)} |
| 78 | |
| 79 | TrafficSched := []*tp_pb.TrafficScheduler{subs.RsrMgr.ResourceMgrs[subs.PonIntf].TechProfileMgr. |
| 80 | GetTrafficScheduler(subs.TpInstance[subs.TestConfig.TpIDList[0]], SchedCfg, TrafficShaping)} |
| 81 | |
| 82 | return TrafficSched |
| 83 | } |
| 84 | |
| 85 | func getTrafficQueues(subs *Subscriber, direction tp_pb.Direction) []*tp_pb.TrafficQueue { |
| 86 | |
| 87 | trafficQueues := subs.RsrMgr.ResourceMgrs[subs.PonIntf].TechProfileMgr. |
| 88 | GetTrafficQueues(subs.TpInstance[subs.TestConfig.TpIDList[0]], direction) |
| 89 | |
| 90 | return trafficQueues |
| 91 | } |
| 92 | |
| 93 | func FormatClassfierAction(flowType string, direction string, subs *Subscriber) (oop.Classifier, oop.Action) { |
| 94 | var flowClassifier oop.Classifier |
| 95 | var actionCmd oop.ActionCmd |
| 96 | var actionInfo oop.Action |
| 97 | |
| 98 | if direction == Upstream { |
| 99 | switch flowType { |
| 100 | case EapolFlow: |
| 101 | flowClassifier.EthType = EapEthType |
| 102 | flowClassifier.OVid = subs.Ctag |
| 103 | flowClassifier.PktTagType = SingleTag |
| 104 | actionCmd.TrapToHost = true |
| 105 | actionInfo.Cmd = &actionCmd |
| 106 | case DhcpFlow: |
| 107 | flowClassifier.EthType = IPv4EthType |
| 108 | flowClassifier.IpProto = DhcpIPProto |
| 109 | flowClassifier.SrcPort = DhcpSrcPort |
| 110 | flowClassifier.DstPort = DhcpDstPort |
| 111 | flowClassifier.PktTagType = SingleTag |
| 112 | actionCmd.TrapToHost = true |
| 113 | actionInfo.Cmd = &actionCmd |
| 114 | case HsiaFlow: |
| 115 | flowClassifier.OVid = subs.Ctag |
| 116 | flowClassifier.PktTagType = SingleTag |
| 117 | actionCmd.AddOuterTag = true |
| 118 | actionInfo.Cmd = &actionCmd |
| 119 | actionInfo.OVid = subs.Stag |
| 120 | default: |
| 121 | log.Errorw("Unsupported flow type", log.Fields{"flowtype": flowType, |
| 122 | "direction": direction}) |
| 123 | } |
| 124 | } else if direction == Downstream { |
| 125 | switch flowType { |
| 126 | case EapolFlow: |
| 127 | log.Errorw("Downstream EAP flows are not required instead controller "+ |
| 128 | "packet outs EAP response directly to onu in downstream", log.Fields{"flowtype": flowType, |
| 129 | "direction": direction}) |
| 130 | case DhcpFlow: |
| 131 | log.Errorw("Downstream DHCP flows are not required instead we have "+ |
| 132 | "NNI trap flows already installed", log.Fields{"flowtype": flowType, |
| 133 | "direction": direction}) |
| 134 | case HsiaFlow: |
| 135 | flowClassifier.OVid = subs.Stag |
| 136 | flowClassifier.IVid = subs.Ctag |
| 137 | flowClassifier.PktTagType = DoubleTag |
| 138 | actionCmd.RemoveOuterTag = true |
| 139 | actionInfo.Cmd = &actionCmd |
| 140 | actionInfo.OVid = DownStreamHsiaActionOVid |
| 141 | default: |
| 142 | log.Errorw("Unsupported flow type", log.Fields{"flowtype": flowType, |
| 143 | "direction": direction}) |
| 144 | } |
| 145 | } |
| 146 | return flowClassifier, actionInfo |
| 147 | } |
| 148 | |
| 149 | func AddFlow(subs *Subscriber, flowType string, direction string, flowID uint32, |
| 150 | allocID uint32, gemID uint32) error { |
| 151 | log.Infow("add-flow", log.Fields{"WorkFlow": subs.TestConfig.WorkflowName, "FlowType": flowType, |
| 152 | "direction": direction, "flowID": flowID}) |
| 153 | var err error |
| 154 | |
| 155 | flowClassifier, actionInfo := FormatClassfierAction(flowType, direction, subs) |
| 156 | flow := oop.Flow{AccessIntfId: int32(subs.PonIntf), OnuId: int32(subs.OnuID), |
| 157 | UniId: int32(subs.UniID), FlowId: flowID, |
| 158 | FlowType: direction, AllocId: int32(allocID), GemportId: int32(gemID), |
| 159 | Classifier: &flowClassifier, Action: &actionInfo, |
| 160 | Priority: 1000, PortNo: subs.UniPortNo} |
| 161 | |
| 162 | _, err = subs.OpenOltClient.FlowAdd(context.Background(), &flow) |
| 163 | |
| 164 | st, _ := status.FromError(err) |
| 165 | if st.Code() == codes.AlreadyExists { |
| 166 | log.Debugw("Flow already exists", log.Fields{"err": err, "deviceFlow": flow}) |
| 167 | return nil |
| 168 | } |
| 169 | |
| 170 | if err != nil { |
| 171 | log.Errorw("Failed to Add flow to device", log.Fields{"err": err, "deviceFlow": flow}) |
| 172 | return errors.New(ReasonCodeToReasonString(FLOW_ADD_FAILED)) |
| 173 | } |
| 174 | log.Debugw("Flow added to device successfully ", log.Fields{"flow": flow}) |
| 175 | |
| 176 | return nil |
| 177 | } |