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