blob: 80a9604a29d74609e4b32cb8fc685b3fa3c5ff2b [file] [log] [blame]
khenaidoo89b0e942018-10-21 21:11:33 -04001/*
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
npujar1d86a522019-11-14 17:11:16 +053017package flowdecomposition
khenaidoo89b0e942018-10-21 21:11:33 -040018
19import (
npujar467fe752020-01-16 20:17:45 +053020 "context"
khenaidoo787224a2020-04-16 18:08:47 -040021 "fmt"
Mahir Gunyeladdb66a2020-04-29 18:08:50 -070022
khenaidoo19d7b632018-10-30 10:49:50 -040023 "github.com/gogo/protobuf/proto"
npujar1d86a522019-11-14 17:11:16 +053024 "github.com/opencord/voltha-go/rw_core/coreif"
khenaidoo820197c2020-02-13 16:35:33 -050025 "github.com/opencord/voltha-go/rw_core/route"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080026 fu "github.com/opencord/voltha-lib-go/v3/pkg/flows"
27 "github.com/opencord/voltha-lib-go/v3/pkg/log"
28 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
29 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoo820197c2020-02-13 16:35:33 -050030 "google.golang.org/grpc/codes"
31 "google.golang.org/grpc/status"
khenaidoo89b0e942018-10-21 21:11:33 -040032)
33
npujar1d86a522019-11-14 17:11:16 +053034// FlowDecomposer represent flow decomposer attribute
khenaidoo89b0e942018-10-21 21:11:33 -040035type FlowDecomposer struct {
npujar1d86a522019-11-14 17:11:16 +053036 deviceMgr coreif.DeviceManager
khenaidoo89b0e942018-10-21 21:11:33 -040037}
38
npujar1d86a522019-11-14 17:11:16 +053039// NewFlowDecomposer creates flow decomposer instance
40func NewFlowDecomposer(deviceMgr coreif.DeviceManager) *FlowDecomposer {
khenaidoo89b0e942018-10-21 21:11:33 -040041 var decomposer FlowDecomposer
42 decomposer.deviceMgr = deviceMgr
43 return &decomposer
44}
45
46//DecomposeRules decomposes per-device flows and flow-groups from the flows and groups defined on a logical device
Kent Hagerman433a31a2020-05-20 19:04:48 -040047func (fd *FlowDecomposer) DecomposeRules(ctx context.Context, agent coreif.LogicalDeviceAgent, flows map[uint64]*ofp.OfpFlowStats, groups map[uint32]*ofp.OfpGroupEntry) (*fu.DeviceRules, error) {
khenaidoo3306c992019-05-24 16:57:35 -040048 deviceRules := *fu.NewDeviceRules()
khenaidoo2c6a0992019-04-29 13:46:56 -040049 devicesToUpdate := make(map[string]string)
khenaidoo89b0e942018-10-21 21:11:33 -040050
Kent Hagerman433a31a2020-05-20 19:04:48 -040051 for _, flow := range flows {
52 decomposedRules, err := fd.decomposeFlow(ctx, agent, flow, groups)
khenaidoo820197c2020-02-13 16:35:33 -050053 if err != nil {
54 return nil, err
55 }
npujar1d86a522019-11-14 17:11:16 +053056 for deviceID, flowAndGroups := range decomposedRules.Rules {
57 deviceRules.CreateEntryIfNotExist(deviceID)
58 deviceRules.Rules[deviceID].AddFrom(flowAndGroups)
59 devicesToUpdate[deviceID] = deviceID
khenaidoo89b0e942018-10-21 21:11:33 -040060 }
61 }
khenaidoo820197c2020-02-13 16:35:33 -050062 return deviceRules.FilterRules(devicesToUpdate), nil
khenaidoo89b0e942018-10-21 21:11:33 -040063}
64
khenaidoo19d7b632018-10-30 10:49:50 -040065// Handles special case of any controller-bound flow for a parent device
66func (fd *FlowDecomposer) updateOutputPortForControllerBoundFlowForParentDevide(flow *ofp.OfpFlowStats,
Scott Bakerfdea1e32020-02-21 15:35:41 -080067 dr *fu.DeviceRules) (*fu.DeviceRules, error) {
khenaidoo68c930b2019-05-13 11:46:51 -040068 EAPOL := fu.EthType(0x888e)
69 IGMP := fu.IpProto(2)
70 UDP := fu.IpProto(17)
khenaidoo19d7b632018-10-30 10:49:50 -040071
72 newDeviceRules := dr.Copy()
73 // Check whether we are dealing with a parent device
npujar1d86a522019-11-14 17:11:16 +053074 for deviceID, fg := range dr.GetRules() {
75 if root, _ := fd.deviceMgr.IsRootDevice(deviceID); root {
76 newDeviceRules.ClearFlows(deviceID)
khenaidoo19d7b632018-10-30 10:49:50 -040077 for i := 0; i < fg.Flows.Len(); i++ {
78 f := fg.GetFlow(i)
79 UpdateOutPortNo := false
khenaidoo68c930b2019-05-13 11:46:51 -040080 for _, field := range fu.GetOfbFields(f) {
khenaidoo19d7b632018-10-30 10:49:50 -040081 UpdateOutPortNo = (field.String() == EAPOL.String())
82 UpdateOutPortNo = UpdateOutPortNo || (field.String() == IGMP.String())
83 UpdateOutPortNo = UpdateOutPortNo || (field.String() == UDP.String())
84 if UpdateOutPortNo {
85 break
86 }
87 }
88 if UpdateOutPortNo {
khenaidoo68c930b2019-05-13 11:46:51 -040089 f = fu.UpdateOutputPortByActionType(f, uint32(ofp.OfpInstructionType_OFPIT_APPLY_ACTIONS),
khenaidoo19d7b632018-10-30 10:49:50 -040090 uint32(ofp.OfpPortNo_OFPP_CONTROLLER))
91 }
92 // Update flow Id as a change in the instruction field will result in a new flow ID
Mahir Gunyeladdb66a2020-04-29 18:08:50 -070093 //var err error
94 //if f.Id, err = fu.HashFlowStats(f); err != nil {
95 //return nil, err
96 //}
npujar1d86a522019-11-14 17:11:16 +053097 newDeviceRules.AddFlow(deviceID, (proto.Clone(f)).(*ofp.OfpFlowStats))
khenaidoo19d7b632018-10-30 10:49:50 -040098 }
99 }
100 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400101
Scott Bakerfdea1e32020-02-21 15:35:41 -0800102 return newDeviceRules, nil
khenaidoo19d7b632018-10-30 10:49:50 -0400103}
104
khenaidood20a5852018-10-22 22:09:55 -0400105//processControllerBoundFlow decomposes trap flows
khenaidoo820197c2020-02-13 16:35:33 -0500106func (fd *FlowDecomposer) processControllerBoundFlow(ctx context.Context, agent coreif.LogicalDeviceAgent, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800107 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400108
Girish Kumarf56a4682020-03-20 20:07:46 +0000109 logger.Debugw("trap-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo, "flow": flow})
khenaidood20a5852018-10-22 22:09:55 -0400110 deviceRules := fu.NewDeviceRules()
npujar1d86a522019-11-14 17:11:16 +0530111 meterID := fu.GetMeterIdFromFlow(flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400112 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(flow)
khenaidood20a5852018-10-22 22:09:55 -0400113
khenaidoo820197c2020-02-13 16:35:33 -0500114 ingressHop := path[0]
115 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400116
Humera Kouser4ff89012019-08-25 19:01:51 -0400117 //case of packet_in from NNI port rule
khenaidoo820197c2020-02-13 16:35:33 -0500118 if agent.GetDeviceRoutes().IsRootPort(inPortNo) {
Humera Kouser4ff89012019-08-25 19:01:51 -0400119 // Trap flow for NNI port
Girish Kumarf56a4682020-03-20 20:07:46 +0000120 logger.Debug("trap-nni")
Humera Kouser4ff89012019-08-25 19:01:51 -0400121
npujar1d86a522019-11-14 17:11:16 +0530122 fa := &fu.FlowArgs{
Humera Kouser4ff89012019-08-25 19:01:51 -0400123 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie},
124 MatchFields: []*ofp.OfpOxmOfbField{
125 fu.InPort(egressHop.Egress),
126 },
127 Actions: fu.GetActions(flow),
128 }
129 // Augment the matchfields with the ofpfields from the flow
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400130 fg := fu.NewFlowsAndGroups()
Humera Kouser4ff89012019-08-25 19:01:51 -0400131 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800132 fs, err := fu.MkFlowStat(fa)
133 if err != nil {
134 return nil, err
135 }
136 fg.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400137 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400138 } else {
139 // Trap flow for UNI port
Girish Kumarf56a4682020-03-20 20:07:46 +0000140 logger.Debug("trap-uni")
khenaidoo89b0e942018-10-21 21:11:33 -0400141
Matt Jeannerete75f2842020-03-14 15:45:12 -0400142 //inPortNo is 0 for wildcard input case, do not include upstream port for controller bound flow in input
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400143 var inPorts = map[uint32]struct{}{inPortNo: {}}
khenaidoo89b0e942018-10-21 21:11:33 -0400144 if inPortNo == 0 {
khenaidood20a5852018-10-22 22:09:55 -0400145 inPorts = agent.GetWildcardInputPorts(egressHop.Egress) // exclude egress_hop.egress_port.port_no
khenaidoo89b0e942018-10-21 21:11:33 -0400146 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400147 for inputPort := range inPorts {
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400148 // Upstream flow on parent (olt) device
npujar1d86a522019-11-14 17:11:16 +0530149 faParent := &fu.FlowArgs{
150 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400151 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400152 fu.InPort(egressHop.Ingress),
khenaidoo68c930b2019-05-13 11:46:51 -0400153 fu.TunnelId(uint64(inputPort)),
khenaidoo89b0e942018-10-21 21:11:33 -0400154 },
155 Actions: []*ofp.OfpAction{
khenaidoo68c930b2019-05-13 11:46:51 -0400156 fu.Output(egressHop.Egress),
khenaidoo89b0e942018-10-21 21:11:33 -0400157 },
158 }
159 // Augment the matchfields with the ofpfields from the flow
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400160 faParent.MatchFields = append(faParent.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
161 fgParent := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800162 fs, err := fu.MkFlowStat(faParent)
163 if err != nil {
164 return nil, err
165 }
166 fgParent.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400167 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fgParent)
Girish Kumarf56a4682020-03-20 20:07:46 +0000168 logger.Debugw("parent-trap-flow-set", log.Fields{"flow": faParent})
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400169
170 // Upstream flow on child (onu) device
171 var actions []*ofp.OfpAction
172 setvid := fu.GetVlanVid(flow)
173 if setvid != nil {
174 // have this child push the vlan the parent is matching/trapping on above
175 actions = []*ofp.OfpAction{
176 fu.PushVlan(0x8100),
177 fu.SetField(fu.VlanVid(*setvid)),
178 fu.Output(ingressHop.Egress),
179 }
180 } else {
181 // otherwise just set the egress port
182 actions = []*ofp.OfpAction{
183 fu.Output(ingressHop.Egress),
184 }
185 }
npujar1d86a522019-11-14 17:11:16 +0530186 faChild := &fu.FlowArgs{
187 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400188 MatchFields: []*ofp.OfpOxmOfbField{
189 fu.InPort(ingressHop.Ingress),
190 fu.TunnelId(uint64(inputPort)),
191 },
192 Actions: actions,
193 }
194 // Augment the matchfields with the ofpfields from the flow.
195 // If the parent has a match vid and the child is setting that match vid exclude the the match vlan
196 // for the child given it will be setting that vlan and the parent will be matching on it
197 if setvid != nil {
198 faChild.MatchFields = append(faChild.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT, fu.VLAN_VID)...)
199 } else {
200 faChild.MatchFields = append(faChild.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
201 }
202 fgChild := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800203 fs, err = fu.MkFlowStat(faChild)
204 if err != nil {
205 return nil, err
206 }
207 fgChild.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400208 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fgChild)
Girish Kumarf56a4682020-03-20 20:07:46 +0000209 logger.Debugw("child-trap-flow-set", log.Fields{"flow": faChild})
khenaidoo89b0e942018-10-21 21:11:33 -0400210 }
211 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400212
Scott Bakerfdea1e32020-02-21 15:35:41 -0800213 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400214}
215
216// processUpstreamNonControllerBoundFlow processes non-controller bound flow. We assume that anything that is
217// upstream needs to get Q-in-Q treatment and that this is expressed via two flow rules, the first using the
218// goto-statement. We also assume that the inner tag is applied at the ONU, while the outer tag is
219// applied at the OLT
khenaidoo820197c2020-02-13 16:35:33 -0500220func (fd *FlowDecomposer) processUpstreamNonControllerBoundFlow(ctx context.Context,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800221 path []route.Hop, inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400222
Girish Kumarf56a4682020-03-20 20:07:46 +0000223 logger.Debugw("upstream-non-controller-bound-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400224 deviceRules := fu.NewDeviceRules()
225
npujar1d86a522019-11-14 17:11:16 +0530226 meterID := fu.GetMeterIdFromFlow(flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400227 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(flow)
228
khenaidoo820197c2020-02-13 16:35:33 -0500229 ingressHop := path[0]
230 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400231
Manikkaraj kb1a10922019-07-29 12:10:34 -0400232 if flow.TableId == 0 && fu.HasNextTable(flow) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000233 logger.Debugw("decomposing-onu-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
khenaidoo89b0e942018-10-21 21:11:33 -0400234 if outPortNo != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000235 logger.Warnw("outPort-should-not-be-specified", log.Fields{"outPortNo": outPortNo})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800236 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400237 }
npujar1d86a522019-11-14 17:11:16 +0530238 fa := &fu.FlowArgs{
239 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400240 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400241 fu.InPort(ingressHop.Ingress),
242 fu.TunnelId(uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400243 },
khenaidoo68c930b2019-05-13 11:46:51 -0400244 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400245 }
246 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400247 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400248
249 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400250 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400251
khenaidoo89b0e942018-10-21 21:11:33 -0400252 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800253 fs, err := fu.MkFlowStat(fa)
254 if err != nil {
255 return nil, err
256 }
257 fg.AddFlow(fs)
khenaidood20a5852018-10-22 22:09:55 -0400258 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400259 } else if flow.TableId == 1 && outPortNo != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000260 logger.Debugw("decomposing-olt-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
npujar1d86a522019-11-14 17:11:16 +0530261 fa := &fu.FlowArgs{
262 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Manikkaraj kb1a10922019-07-29 12:10:34 -0400263 MatchFields: []*ofp.OfpOxmOfbField{
264 fu.InPort(egressHop.Ingress),
265 fu.TunnelId(uint64(inPortNo)),
266 },
khenaidoo89b0e942018-10-21 21:11:33 -0400267 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400268 // Augment the matchfields with the ofpfields from the flow
269 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidoo89b0e942018-10-21 21:11:33 -0400270
Manikkaraj kb1a10922019-07-29 12:10:34 -0400271 //Augment the actions
272 filteredAction := fu.GetActions(flow, fu.OUTPUT)
273 filteredAction = append(filteredAction, fu.Output(egressHop.Egress))
274 fa.Actions = filteredAction
khenaidood20a5852018-10-22 22:09:55 -0400275
Manikkaraj kb1a10922019-07-29 12:10:34 -0400276 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800277 fs, err := fu.MkFlowStat(fa)
278 if err != nil {
279 return nil, err
280 }
281 fg.AddFlow(fs)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400282 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400283 }
Scott Bakerfdea1e32020-02-21 15:35:41 -0800284 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400285}
286
khenaidood20a5852018-10-22 22:09:55 -0400287// processDownstreamFlowWithNextTable decomposes downstream flows containing next table ID instructions
khenaidoo820197c2020-02-13 16:35:33 -0500288func (fd *FlowDecomposer) processDownstreamFlowWithNextTable(ctx context.Context, agent coreif.LogicalDeviceAgent, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800289 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000290 logger.Debugw("decomposing-olt-flow-in-downstream-flow-with-next-table", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400291 deviceRules := fu.NewDeviceRules()
npujar1d86a522019-11-14 17:11:16 +0530292 meterID := fu.GetMeterIdFromFlow(flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400293 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(flow)
khenaidood20a5852018-10-22 22:09:55 -0400294
khenaidoo89b0e942018-10-21 21:11:33 -0400295 if outPortNo != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000296 logger.Warnw("outPort-should-not-be-specified", log.Fields{"outPortNo": outPortNo})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800297 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400298 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400299
300 if flow.TableId != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000301 logger.Warnw("This is not olt pipeline table, so skipping", log.Fields{"tableId": flow.TableId})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800302 return deviceRules, nil
Manikkaraj kb1a10922019-07-29 12:10:34 -0400303 }
304
khenaidoo820197c2020-02-13 16:35:33 -0500305 ingressHop := path[0]
306 egressHop := path[1]
Manikkaraj kb1a10922019-07-29 12:10:34 -0400307 if metadataFromwriteMetadata != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000308 logger.Debugw("creating-metadata-flow", log.Fields{"flow": flow})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400309 portNumber := fu.GetEgressPortNumberFromWriteMetadata(flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400310 if portNumber != 0 {
khenaidoo820197c2020-02-13 16:35:33 -0500311 recalculatedRoute, err := agent.GetRoute(ctx, inPortNo, portNumber)
312 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000313 logger.Errorw("no-route-double-tag", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo, "metadata": metadataFromwriteMetadata, "error": err})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800314 return deviceRules, nil
khenaidoo820197c2020-02-13 16:35:33 -0500315 }
khenaidoo89b0e942018-10-21 21:11:33 -0400316 switch len(recalculatedRoute) {
317 case 0:
Girish Kumarf56a4682020-03-20 20:07:46 +0000318 logger.Errorw("no-route-double-tag", log.Fields{"inPortNo": inPortNo, "outPortNo": portNumber, "comment": "deleting-flow", "metadata": metadataFromwriteMetadata})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400319 //TODO: Delete flow
Scott Bakerfdea1e32020-02-21 15:35:41 -0800320 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400321 case 2:
Girish Kumarf56a4682020-03-20 20:07:46 +0000322 logger.Debugw("route-found", log.Fields{"ingressHop": ingressHop, "egressHop": egressHop})
khenaidoo89b0e942018-10-21 21:11:33 -0400323 default:
Girish Kumarf56a4682020-03-20 20:07:46 +0000324 logger.Errorw("invalid-route-length", log.Fields{"routeLen": len(path)})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800325 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400326 }
327 ingressHop = recalculatedRoute[0]
328 }
khenaidoo68c930b2019-05-13 11:46:51 -0400329 innerTag := fu.GetInnerTagFromMetaData(flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400330 if innerTag == 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000331 logger.Errorw("no-inner-route-double-tag", log.Fields{"inPortNo": inPortNo, "outPortNo": portNumber, "comment": "deleting-flow", "metadata": metadataFromwriteMetadata})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400332 //TODO: Delete flow
Scott Bakerfdea1e32020-02-21 15:35:41 -0800333 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400334 }
npujar1d86a522019-11-14 17:11:16 +0530335 fa := &fu.FlowArgs{
336 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400337 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400338 fu.InPort(ingressHop.Ingress),
Manikkaraj kb1a10922019-07-29 12:10:34 -0400339 fu.Metadata_ofp(uint64(innerTag)),
khenaidoo68c930b2019-05-13 11:46:51 -0400340 fu.TunnelId(uint64(portNumber)),
khenaidoo89b0e942018-10-21 21:11:33 -0400341 },
khenaidoo68c930b2019-05-13 11:46:51 -0400342 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400343 }
344 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400345 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT, fu.METADATA)...)
khenaidood20a5852018-10-22 22:09:55 -0400346
347 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400348 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400349
khenaidoo89b0e942018-10-21 21:11:33 -0400350 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800351 fs, err := fu.MkFlowStat(fa)
352 if err != nil {
353 return nil, err
354 }
355 fg.AddFlow(fs)
khenaidoo89b0e942018-10-21 21:11:33 -0400356 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
357 } else { // Create standard flow
Girish Kumarf56a4682020-03-20 20:07:46 +0000358 logger.Debugw("creating-standard-flow", log.Fields{"flow": flow})
npujar1d86a522019-11-14 17:11:16 +0530359 fa := &fu.FlowArgs{
360 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400361 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400362 fu.InPort(ingressHop.Ingress),
363 fu.TunnelId(uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400364 },
khenaidoo68c930b2019-05-13 11:46:51 -0400365 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400366 }
367 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400368 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400369
370 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400371 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400372
khenaidoo89b0e942018-10-21 21:11:33 -0400373 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800374 fs, err := fu.MkFlowStat(fa)
375 if err != nil {
376 return nil, err
377 }
378 fg.AddFlow(fs)
khenaidoo89b0e942018-10-21 21:11:33 -0400379 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
380 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400381
Scott Bakerfdea1e32020-02-21 15:35:41 -0800382 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400383}
384
khenaidood20a5852018-10-22 22:09:55 -0400385// processUnicastFlow decomposes unicast flows
khenaidoo820197c2020-02-13 16:35:33 -0500386func (fd *FlowDecomposer) processUnicastFlow(ctx context.Context, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800387 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400388
Girish Kumarf56a4682020-03-20 20:07:46 +0000389 logger.Debugw("decomposing-onu-flow-in-downstream-unicast-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400390 deviceRules := fu.NewDeviceRules()
391
khenaidoo820197c2020-02-13 16:35:33 -0500392 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400393
npujar1d86a522019-11-14 17:11:16 +0530394 meterID := fu.GetMeterIdFromFlow(flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400395 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(flow)
npujar1d86a522019-11-14 17:11:16 +0530396 fa := &fu.FlowArgs{
397 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Manikkaraj kb1a10922019-07-29 12:10:34 -0400398 MatchFields: []*ofp.OfpOxmOfbField{
399 fu.InPort(egressHop.Ingress),
400 },
khenaidoo89b0e942018-10-21 21:11:33 -0400401 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400402 // Augment the matchfields with the ofpfields from the flow
403 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400404
Manikkaraj kb1a10922019-07-29 12:10:34 -0400405 // Augment the Actions
406 filteredAction := fu.GetActions(flow, fu.OUTPUT)
407 filteredAction = append(filteredAction, fu.Output(egressHop.Egress))
408 fa.Actions = filteredAction
khenaidoo89b0e942018-10-21 21:11:33 -0400409
Manikkaraj kb1a10922019-07-29 12:10:34 -0400410 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800411 fs, err := fu.MkFlowStat(fa)
412 if err != nil {
413 return nil, err
414 }
415 fg.AddFlow(fs)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400416 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800417 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400418}
419
khenaidood20a5852018-10-22 22:09:55 -0400420// processMulticastFlow decompose multicast flows
khenaidoo820197c2020-02-13 16:35:33 -0500421func (fd *FlowDecomposer) processMulticastFlow(ctx context.Context, path []route.Hop,
npujar1d86a522019-11-14 17:11:16 +0530422 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats, grpID uint32,
khenaidood20a5852018-10-22 22:09:55 -0400423 groupMap map[uint32]*ofp.OfpGroupEntry) *fu.DeviceRules {
424
Girish Kumarf56a4682020-03-20 20:07:46 +0000425 logger.Debugw("multicast-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400426 deviceRules := fu.NewDeviceRules()
khenaidoo89b0e942018-10-21 21:11:33 -0400427
428 //having no Group yet is the same as having a Group with no buckets
429 var grp *ofp.OfpGroupEntry
430 var ok bool
npujar1d86a522019-11-14 17:11:16 +0530431 if grp, ok = groupMap[grpID]; !ok {
Girish Kumarf56a4682020-03-20 20:07:46 +0000432 logger.Warnw("Group-id-not-present-in-map", log.Fields{"grpId": grpID, "groupMap": groupMap})
khenaidoo89b0e942018-10-21 21:11:33 -0400433 return deviceRules
434 }
435 if grp == nil || grp.Desc == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000436 logger.Warnw("Group-or-desc-nil", log.Fields{"grpId": grpID, "grp": grp})
khenaidoo89b0e942018-10-21 21:11:33 -0400437 return deviceRules
438 }
khenaidoo89b0e942018-10-21 21:11:33 -0400439
khenaidoo820197c2020-02-13 16:35:33 -0500440 deviceRules.CreateEntryIfNotExist(path[0].DeviceID)
Esin Karaman09959ae2019-11-29 13:59:58 +0000441 fg := fu.NewFlowsAndGroups()
442 fg.AddFlow(flow)
443 //return the multicast flow without decomposing it
khenaidoo820197c2020-02-13 16:35:33 -0500444 deviceRules.AddFlowsAndGroup(path[0].DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400445 return deviceRules
446}
447
khenaidood20a5852018-10-22 22:09:55 -0400448// decomposeFlow decomposes a flow for a logical device into flows for each physical device
npujar467fe752020-01-16 20:17:45 +0530449func (fd *FlowDecomposer) decomposeFlow(ctx context.Context, agent coreif.LogicalDeviceAgent, flow *ofp.OfpFlowStats,
khenaidoo820197c2020-02-13 16:35:33 -0500450 groupMap map[uint32]*ofp.OfpGroupEntry) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400451
khenaidoo68c930b2019-05-13 11:46:51 -0400452 inPortNo := fu.GetInPort(flow)
Esin Karaman09959ae2019-11-29 13:59:58 +0000453 if fu.HasGroup(flow) && inPortNo == 0 {
454 //if no in-port specified for a multicast flow, put NNI port as in-port
khenaidoo820197c2020-02-13 16:35:33 -0500455 //so that a valid path can be found for the flow
Esin Karaman09959ae2019-11-29 13:59:58 +0000456 nniPorts := agent.GetNNIPorts()
457 if len(nniPorts) > 0 {
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400458 for port := range nniPorts {
459 inPortNo = port
460 break
461 }
462 logger.Debugw("assigning-nni-port-as-in-port-for-multicast-flow", log.Fields{"nni": inPortNo, "flow:": flow})
Esin Karaman09959ae2019-11-29 13:59:58 +0000463 }
464 }
khenaidoo68c930b2019-05-13 11:46:51 -0400465 outPortNo := fu.GetOutPort(flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400466 deviceRules := fu.NewDeviceRules()
khenaidoo820197c2020-02-13 16:35:33 -0500467 path, err := agent.GetRoute(ctx, inPortNo, outPortNo)
468 if err != nil {
khenaidoo820197c2020-02-13 16:35:33 -0500469 return deviceRules, err
470 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400471
khenaidoo820197c2020-02-13 16:35:33 -0500472 switch len(path) {
khenaidoo89b0e942018-10-21 21:11:33 -0400473 case 0:
khenaidoo787224a2020-04-16 18:08:47 -0400474 return deviceRules, fmt.Errorf("no route from:%d to:%d :%w", inPortNo, outPortNo, route.ErrNoRoute)
khenaidoo89b0e942018-10-21 21:11:33 -0400475 case 2:
Girish Kumarf56a4682020-03-20 20:07:46 +0000476 logger.Debugw("route-found", log.Fields{"ingressHop": path[0], "egressHop": path[1]})
khenaidoo89b0e942018-10-21 21:11:33 -0400477 default:
khenaidoo787224a2020-04-16 18:08:47 -0400478 return deviceRules, fmt.Errorf("invalid route length %d :%w", len(path), route.ErrNoRoute)
khenaidoo89b0e942018-10-21 21:11:33 -0400479 }
480
khenaidoo89b0e942018-10-21 21:11:33 -0400481 // Process controller bound flow
482 if outPortNo != 0 && (outPortNo&0x7fffffff) == uint32(ofp.OfpPortNo_OFPP_CONTROLLER) {
Scott Bakerfdea1e32020-02-21 15:35:41 -0800483 deviceRules, err = fd.processControllerBoundFlow(ctx, agent, path, inPortNo, outPortNo, flow)
484 if err != nil {
485 return nil, err
486 }
khenaidoo89b0e942018-10-21 21:11:33 -0400487 } else {
khenaidoo297cd252019-02-07 22:10:23 -0500488 var ingressDevice *voltha.Device
489 var err error
Kent Hagerman45a13e42020-04-13 12:23:50 -0400490 if ingressDevice, err = fd.deviceMgr.GetDevice(ctx, &voltha.ID{Id: path[0].DeviceID}); err != nil {
khenaidoo787224a2020-04-16 18:08:47 -0400491 // This can happen in a race condition where a device is deleted right after we obtain a
492 // route involving the device (GetRoute() above). Handle it as a no route event as well.
493 return deviceRules, fmt.Errorf("get-device-error :%v :%w", err, route.ErrNoRoute)
khenaidoo297cd252019-02-07 22:10:23 -0500494 }
495 isUpstream := !ingressDevice.Root
Manikkaraj kb1a10922019-07-29 12:10:34 -0400496 if isUpstream { // Unicast OLT and ONU UL
Girish Kumarf56a4682020-03-20 20:07:46 +0000497 logger.Debug("process-olt-nd-onu-upstream-noncontrollerbound-unicast-flows", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800498 deviceRules, err = fd.processUpstreamNonControllerBoundFlow(ctx, path, inPortNo, outPortNo, flow)
499 if err != nil {
500 return nil, err
501 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400502 } else if fu.HasNextTable(flow) && flow.TableId == 0 { // Unicast OLT flow DL
Girish Kumarf56a4682020-03-20 20:07:46 +0000503 logger.Debugw("process-olt-downstream-noncontrollerbound-flow-with-nexttable", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800504 deviceRules, err = fd.processDownstreamFlowWithNextTable(ctx, agent, path, inPortNo, outPortNo, flow)
505 if err != nil {
506 return nil, err
507 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400508 } else if flow.TableId == 1 && outPortNo != 0 { // Unicast ONU flow DL
Girish Kumarf56a4682020-03-20 20:07:46 +0000509 logger.Debugw("process-onu-downstream-unicast-flow", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800510 deviceRules, err = fd.processUnicastFlow(ctx, path, inPortNo, outPortNo, flow)
511 if err != nil {
512 return nil, err
513 }
npujar1d86a522019-11-14 17:11:16 +0530514 } else if grpID := fu.GetGroup(flow); grpID != 0 && flow.TableId == 0 { //Multicast
Girish Kumarf56a4682020-03-20 20:07:46 +0000515 logger.Debugw("process-multicast-flow", log.Fields{"flows": flow})
khenaidoo820197c2020-02-13 16:35:33 -0500516 deviceRules = fd.processMulticastFlow(ctx, path, inPortNo, outPortNo, flow, grpID, groupMap)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400517 } else {
khenaidoo820197c2020-02-13 16:35:33 -0500518 return deviceRules, status.Errorf(codes.Aborted, "unknown downstream flow %v", *flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400519 }
520 }
Scott Bakerfdea1e32020-02-21 15:35:41 -0800521 deviceRules, err = fd.updateOutputPortForControllerBoundFlowForParentDevide(flow, deviceRules)
522 return deviceRules, err
khenaidoo89b0e942018-10-21 21:11:33 -0400523}