blob: eb338dbf9400f9e9a34406961f4a7cbdeaac2a2f [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
khenaidoo89b0e942018-10-21 21:11:33 -0400143 var inPorts []uint32
144 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 } else {
147 inPorts = []uint32{inPortNo}
148 }
149 for _, inputPort := range inPorts {
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400150 // Upstream flow on parent (olt) device
npujar1d86a522019-11-14 17:11:16 +0530151 faParent := &fu.FlowArgs{
152 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400153 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400154 fu.InPort(egressHop.Ingress),
khenaidoo68c930b2019-05-13 11:46:51 -0400155 fu.TunnelId(uint64(inputPort)),
khenaidoo89b0e942018-10-21 21:11:33 -0400156 },
157 Actions: []*ofp.OfpAction{
khenaidoo68c930b2019-05-13 11:46:51 -0400158 fu.Output(egressHop.Egress),
khenaidoo89b0e942018-10-21 21:11:33 -0400159 },
160 }
161 // Augment the matchfields with the ofpfields from the flow
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400162 faParent.MatchFields = append(faParent.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
163 fgParent := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800164 fs, err := fu.MkFlowStat(faParent)
165 if err != nil {
166 return nil, err
167 }
168 fgParent.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400169 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fgParent)
Girish Kumarf56a4682020-03-20 20:07:46 +0000170 logger.Debugw("parent-trap-flow-set", log.Fields{"flow": faParent})
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400171
172 // Upstream flow on child (onu) device
173 var actions []*ofp.OfpAction
174 setvid := fu.GetVlanVid(flow)
175 if setvid != nil {
176 // have this child push the vlan the parent is matching/trapping on above
177 actions = []*ofp.OfpAction{
178 fu.PushVlan(0x8100),
179 fu.SetField(fu.VlanVid(*setvid)),
180 fu.Output(ingressHop.Egress),
181 }
182 } else {
183 // otherwise just set the egress port
184 actions = []*ofp.OfpAction{
185 fu.Output(ingressHop.Egress),
186 }
187 }
npujar1d86a522019-11-14 17:11:16 +0530188 faChild := &fu.FlowArgs{
189 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400190 MatchFields: []*ofp.OfpOxmOfbField{
191 fu.InPort(ingressHop.Ingress),
192 fu.TunnelId(uint64(inputPort)),
193 },
194 Actions: actions,
195 }
196 // Augment the matchfields with the ofpfields from the flow.
197 // If the parent has a match vid and the child is setting that match vid exclude the the match vlan
198 // for the child given it will be setting that vlan and the parent will be matching on it
199 if setvid != nil {
200 faChild.MatchFields = append(faChild.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT, fu.VLAN_VID)...)
201 } else {
202 faChild.MatchFields = append(faChild.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
203 }
204 fgChild := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800205 fs, err = fu.MkFlowStat(faChild)
206 if err != nil {
207 return nil, err
208 }
209 fgChild.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400210 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fgChild)
Girish Kumarf56a4682020-03-20 20:07:46 +0000211 logger.Debugw("child-trap-flow-set", log.Fields{"flow": faChild})
khenaidoo89b0e942018-10-21 21:11:33 -0400212 }
213 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400214
Scott Bakerfdea1e32020-02-21 15:35:41 -0800215 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400216}
217
218// processUpstreamNonControllerBoundFlow processes non-controller bound flow. We assume that anything that is
219// upstream needs to get Q-in-Q treatment and that this is expressed via two flow rules, the first using the
220// goto-statement. We also assume that the inner tag is applied at the ONU, while the outer tag is
221// applied at the OLT
khenaidoo820197c2020-02-13 16:35:33 -0500222func (fd *FlowDecomposer) processUpstreamNonControllerBoundFlow(ctx context.Context,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800223 path []route.Hop, inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400224
Girish Kumarf56a4682020-03-20 20:07:46 +0000225 logger.Debugw("upstream-non-controller-bound-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400226 deviceRules := fu.NewDeviceRules()
227
npujar1d86a522019-11-14 17:11:16 +0530228 meterID := fu.GetMeterIdFromFlow(flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400229 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(flow)
230
khenaidoo820197c2020-02-13 16:35:33 -0500231 ingressHop := path[0]
232 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400233
Manikkaraj kb1a10922019-07-29 12:10:34 -0400234 if flow.TableId == 0 && fu.HasNextTable(flow) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000235 logger.Debugw("decomposing-onu-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
khenaidoo89b0e942018-10-21 21:11:33 -0400236 if outPortNo != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000237 logger.Warnw("outPort-should-not-be-specified", log.Fields{"outPortNo": outPortNo})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800238 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400239 }
npujar1d86a522019-11-14 17:11:16 +0530240 fa := &fu.FlowArgs{
241 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400242 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400243 fu.InPort(ingressHop.Ingress),
244 fu.TunnelId(uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400245 },
khenaidoo68c930b2019-05-13 11:46:51 -0400246 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400247 }
248 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400249 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400250
251 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400252 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400253
khenaidoo89b0e942018-10-21 21:11:33 -0400254 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800255 fs, err := fu.MkFlowStat(fa)
256 if err != nil {
257 return nil, err
258 }
259 fg.AddFlow(fs)
khenaidood20a5852018-10-22 22:09:55 -0400260 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400261 } else if flow.TableId == 1 && outPortNo != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000262 logger.Debugw("decomposing-olt-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
npujar1d86a522019-11-14 17:11:16 +0530263 fa := &fu.FlowArgs{
264 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Manikkaraj kb1a10922019-07-29 12:10:34 -0400265 MatchFields: []*ofp.OfpOxmOfbField{
266 fu.InPort(egressHop.Ingress),
267 fu.TunnelId(uint64(inPortNo)),
268 },
khenaidoo89b0e942018-10-21 21:11:33 -0400269 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400270 // Augment the matchfields with the ofpfields from the flow
271 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidoo89b0e942018-10-21 21:11:33 -0400272
Manikkaraj kb1a10922019-07-29 12:10:34 -0400273 //Augment the actions
274 filteredAction := fu.GetActions(flow, fu.OUTPUT)
275 filteredAction = append(filteredAction, fu.Output(egressHop.Egress))
276 fa.Actions = filteredAction
khenaidood20a5852018-10-22 22:09:55 -0400277
Manikkaraj kb1a10922019-07-29 12:10:34 -0400278 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800279 fs, err := fu.MkFlowStat(fa)
280 if err != nil {
281 return nil, err
282 }
283 fg.AddFlow(fs)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400284 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400285 }
Scott Bakerfdea1e32020-02-21 15:35:41 -0800286 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400287}
288
khenaidood20a5852018-10-22 22:09:55 -0400289// processDownstreamFlowWithNextTable decomposes downstream flows containing next table ID instructions
khenaidoo820197c2020-02-13 16:35:33 -0500290func (fd *FlowDecomposer) processDownstreamFlowWithNextTable(ctx context.Context, agent coreif.LogicalDeviceAgent, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800291 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000292 logger.Debugw("decomposing-olt-flow-in-downstream-flow-with-next-table", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400293 deviceRules := fu.NewDeviceRules()
npujar1d86a522019-11-14 17:11:16 +0530294 meterID := fu.GetMeterIdFromFlow(flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400295 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(flow)
khenaidood20a5852018-10-22 22:09:55 -0400296
khenaidoo89b0e942018-10-21 21:11:33 -0400297 if outPortNo != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000298 logger.Warnw("outPort-should-not-be-specified", log.Fields{"outPortNo": outPortNo})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800299 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400300 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400301
302 if flow.TableId != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000303 logger.Warnw("This is not olt pipeline table, so skipping", log.Fields{"tableId": flow.TableId})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800304 return deviceRules, nil
Manikkaraj kb1a10922019-07-29 12:10:34 -0400305 }
306
khenaidoo820197c2020-02-13 16:35:33 -0500307 ingressHop := path[0]
308 egressHop := path[1]
Manikkaraj kb1a10922019-07-29 12:10:34 -0400309 if metadataFromwriteMetadata != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000310 logger.Debugw("creating-metadata-flow", log.Fields{"flow": flow})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400311 portNumber := fu.GetEgressPortNumberFromWriteMetadata(flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400312 if portNumber != 0 {
khenaidoo820197c2020-02-13 16:35:33 -0500313 recalculatedRoute, err := agent.GetRoute(ctx, inPortNo, portNumber)
314 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000315 logger.Errorw("no-route-double-tag", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo, "metadata": metadataFromwriteMetadata, "error": err})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800316 return deviceRules, nil
khenaidoo820197c2020-02-13 16:35:33 -0500317 }
khenaidoo89b0e942018-10-21 21:11:33 -0400318 switch len(recalculatedRoute) {
319 case 0:
Girish Kumarf56a4682020-03-20 20:07:46 +0000320 logger.Errorw("no-route-double-tag", log.Fields{"inPortNo": inPortNo, "outPortNo": portNumber, "comment": "deleting-flow", "metadata": metadataFromwriteMetadata})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400321 //TODO: Delete flow
Scott Bakerfdea1e32020-02-21 15:35:41 -0800322 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400323 case 2:
Girish Kumarf56a4682020-03-20 20:07:46 +0000324 logger.Debugw("route-found", log.Fields{"ingressHop": ingressHop, "egressHop": egressHop})
khenaidoo89b0e942018-10-21 21:11:33 -0400325 default:
Girish Kumarf56a4682020-03-20 20:07:46 +0000326 logger.Errorw("invalid-route-length", log.Fields{"routeLen": len(path)})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800327 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400328 }
329 ingressHop = recalculatedRoute[0]
330 }
khenaidoo68c930b2019-05-13 11:46:51 -0400331 innerTag := fu.GetInnerTagFromMetaData(flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400332 if innerTag == 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000333 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 -0400334 //TODO: Delete flow
Scott Bakerfdea1e32020-02-21 15:35:41 -0800335 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400336 }
npujar1d86a522019-11-14 17:11:16 +0530337 fa := &fu.FlowArgs{
338 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400339 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400340 fu.InPort(ingressHop.Ingress),
Manikkaraj kb1a10922019-07-29 12:10:34 -0400341 fu.Metadata_ofp(uint64(innerTag)),
khenaidoo68c930b2019-05-13 11:46:51 -0400342 fu.TunnelId(uint64(portNumber)),
khenaidoo89b0e942018-10-21 21:11:33 -0400343 },
khenaidoo68c930b2019-05-13 11:46:51 -0400344 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400345 }
346 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400347 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT, fu.METADATA)...)
khenaidood20a5852018-10-22 22:09:55 -0400348
349 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400350 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400351
khenaidoo89b0e942018-10-21 21:11:33 -0400352 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800353 fs, err := fu.MkFlowStat(fa)
354 if err != nil {
355 return nil, err
356 }
357 fg.AddFlow(fs)
khenaidoo89b0e942018-10-21 21:11:33 -0400358 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
359 } else { // Create standard flow
Girish Kumarf56a4682020-03-20 20:07:46 +0000360 logger.Debugw("creating-standard-flow", log.Fields{"flow": flow})
npujar1d86a522019-11-14 17:11:16 +0530361 fa := &fu.FlowArgs{
362 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400363 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400364 fu.InPort(ingressHop.Ingress),
365 fu.TunnelId(uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400366 },
khenaidoo68c930b2019-05-13 11:46:51 -0400367 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400368 }
369 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400370 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400371
372 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400373 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400374
khenaidoo89b0e942018-10-21 21:11:33 -0400375 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800376 fs, err := fu.MkFlowStat(fa)
377 if err != nil {
378 return nil, err
379 }
380 fg.AddFlow(fs)
khenaidoo89b0e942018-10-21 21:11:33 -0400381 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
382 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400383
Scott Bakerfdea1e32020-02-21 15:35:41 -0800384 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400385}
386
khenaidood20a5852018-10-22 22:09:55 -0400387// processUnicastFlow decomposes unicast flows
khenaidoo820197c2020-02-13 16:35:33 -0500388func (fd *FlowDecomposer) processUnicastFlow(ctx context.Context, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800389 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400390
Girish Kumarf56a4682020-03-20 20:07:46 +0000391 logger.Debugw("decomposing-onu-flow-in-downstream-unicast-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400392 deviceRules := fu.NewDeviceRules()
393
khenaidoo820197c2020-02-13 16:35:33 -0500394 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400395
npujar1d86a522019-11-14 17:11:16 +0530396 meterID := fu.GetMeterIdFromFlow(flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400397 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(flow)
npujar1d86a522019-11-14 17:11:16 +0530398 fa := &fu.FlowArgs{
399 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Manikkaraj kb1a10922019-07-29 12:10:34 -0400400 MatchFields: []*ofp.OfpOxmOfbField{
401 fu.InPort(egressHop.Ingress),
402 },
khenaidoo89b0e942018-10-21 21:11:33 -0400403 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400404 // Augment the matchfields with the ofpfields from the flow
405 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400406
Manikkaraj kb1a10922019-07-29 12:10:34 -0400407 // Augment the Actions
408 filteredAction := fu.GetActions(flow, fu.OUTPUT)
409 filteredAction = append(filteredAction, fu.Output(egressHop.Egress))
410 fa.Actions = filteredAction
khenaidoo89b0e942018-10-21 21:11:33 -0400411
Manikkaraj kb1a10922019-07-29 12:10:34 -0400412 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800413 fs, err := fu.MkFlowStat(fa)
414 if err != nil {
415 return nil, err
416 }
417 fg.AddFlow(fs)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400418 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800419 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400420}
421
khenaidood20a5852018-10-22 22:09:55 -0400422// processMulticastFlow decompose multicast flows
khenaidoo820197c2020-02-13 16:35:33 -0500423func (fd *FlowDecomposer) processMulticastFlow(ctx context.Context, path []route.Hop,
npujar1d86a522019-11-14 17:11:16 +0530424 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats, grpID uint32,
khenaidood20a5852018-10-22 22:09:55 -0400425 groupMap map[uint32]*ofp.OfpGroupEntry) *fu.DeviceRules {
426
Girish Kumarf56a4682020-03-20 20:07:46 +0000427 logger.Debugw("multicast-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400428 deviceRules := fu.NewDeviceRules()
khenaidoo89b0e942018-10-21 21:11:33 -0400429
430 //having no Group yet is the same as having a Group with no buckets
431 var grp *ofp.OfpGroupEntry
432 var ok bool
npujar1d86a522019-11-14 17:11:16 +0530433 if grp, ok = groupMap[grpID]; !ok {
Girish Kumarf56a4682020-03-20 20:07:46 +0000434 logger.Warnw("Group-id-not-present-in-map", log.Fields{"grpId": grpID, "groupMap": groupMap})
khenaidoo89b0e942018-10-21 21:11:33 -0400435 return deviceRules
436 }
437 if grp == nil || grp.Desc == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000438 logger.Warnw("Group-or-desc-nil", log.Fields{"grpId": grpID, "grp": grp})
khenaidoo89b0e942018-10-21 21:11:33 -0400439 return deviceRules
440 }
khenaidoo89b0e942018-10-21 21:11:33 -0400441
khenaidoo820197c2020-02-13 16:35:33 -0500442 deviceRules.CreateEntryIfNotExist(path[0].DeviceID)
Esin Karaman09959ae2019-11-29 13:59:58 +0000443 fg := fu.NewFlowsAndGroups()
444 fg.AddFlow(flow)
445 //return the multicast flow without decomposing it
khenaidoo820197c2020-02-13 16:35:33 -0500446 deviceRules.AddFlowsAndGroup(path[0].DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400447 return deviceRules
448}
449
khenaidood20a5852018-10-22 22:09:55 -0400450// decomposeFlow decomposes a flow for a logical device into flows for each physical device
npujar467fe752020-01-16 20:17:45 +0530451func (fd *FlowDecomposer) decomposeFlow(ctx context.Context, agent coreif.LogicalDeviceAgent, flow *ofp.OfpFlowStats,
khenaidoo820197c2020-02-13 16:35:33 -0500452 groupMap map[uint32]*ofp.OfpGroupEntry) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400453
khenaidoo68c930b2019-05-13 11:46:51 -0400454 inPortNo := fu.GetInPort(flow)
Esin Karaman09959ae2019-11-29 13:59:58 +0000455 if fu.HasGroup(flow) && inPortNo == 0 {
456 //if no in-port specified for a multicast flow, put NNI port as in-port
khenaidoo820197c2020-02-13 16:35:33 -0500457 //so that a valid path can be found for the flow
Esin Karaman09959ae2019-11-29 13:59:58 +0000458 nniPorts := agent.GetNNIPorts()
459 if len(nniPorts) > 0 {
460 inPortNo = nniPorts[0]
Girish Kumarf56a4682020-03-20 20:07:46 +0000461 logger.Debugw("assigning-nni-port-as-in-port-for-multicast-flow", log.Fields{"nni": nniPorts[0], "flow:": flow})
Esin Karaman09959ae2019-11-29 13:59:58 +0000462 }
463 }
khenaidoo68c930b2019-05-13 11:46:51 -0400464 outPortNo := fu.GetOutPort(flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400465 deviceRules := fu.NewDeviceRules()
khenaidoo820197c2020-02-13 16:35:33 -0500466 path, err := agent.GetRoute(ctx, inPortNo, outPortNo)
467 if err != nil {
khenaidoo820197c2020-02-13 16:35:33 -0500468 return deviceRules, err
469 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400470
khenaidoo820197c2020-02-13 16:35:33 -0500471 switch len(path) {
khenaidoo89b0e942018-10-21 21:11:33 -0400472 case 0:
khenaidoo787224a2020-04-16 18:08:47 -0400473 return deviceRules, fmt.Errorf("no route from:%d to:%d :%w", inPortNo, outPortNo, route.ErrNoRoute)
khenaidoo89b0e942018-10-21 21:11:33 -0400474 case 2:
Girish Kumarf56a4682020-03-20 20:07:46 +0000475 logger.Debugw("route-found", log.Fields{"ingressHop": path[0], "egressHop": path[1]})
khenaidoo89b0e942018-10-21 21:11:33 -0400476 default:
khenaidoo787224a2020-04-16 18:08:47 -0400477 return deviceRules, fmt.Errorf("invalid route length %d :%w", len(path), route.ErrNoRoute)
khenaidoo89b0e942018-10-21 21:11:33 -0400478 }
479
khenaidoo89b0e942018-10-21 21:11:33 -0400480 // Process controller bound flow
481 if outPortNo != 0 && (outPortNo&0x7fffffff) == uint32(ofp.OfpPortNo_OFPP_CONTROLLER) {
Scott Bakerfdea1e32020-02-21 15:35:41 -0800482 deviceRules, err = fd.processControllerBoundFlow(ctx, agent, path, inPortNo, outPortNo, flow)
483 if err != nil {
484 return nil, err
485 }
khenaidoo89b0e942018-10-21 21:11:33 -0400486 } else {
khenaidoo297cd252019-02-07 22:10:23 -0500487 var ingressDevice *voltha.Device
488 var err error
Kent Hagerman45a13e42020-04-13 12:23:50 -0400489 if ingressDevice, err = fd.deviceMgr.GetDevice(ctx, &voltha.ID{Id: path[0].DeviceID}); err != nil {
khenaidoo787224a2020-04-16 18:08:47 -0400490 // This can happen in a race condition where a device is deleted right after we obtain a
491 // route involving the device (GetRoute() above). Handle it as a no route event as well.
492 return deviceRules, fmt.Errorf("get-device-error :%v :%w", err, route.ErrNoRoute)
khenaidoo297cd252019-02-07 22:10:23 -0500493 }
494 isUpstream := !ingressDevice.Root
Manikkaraj kb1a10922019-07-29 12:10:34 -0400495 if isUpstream { // Unicast OLT and ONU UL
Girish Kumarf56a4682020-03-20 20:07:46 +0000496 logger.Debug("process-olt-nd-onu-upstream-noncontrollerbound-unicast-flows", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800497 deviceRules, err = fd.processUpstreamNonControllerBoundFlow(ctx, path, inPortNo, outPortNo, flow)
498 if err != nil {
499 return nil, err
500 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400501 } else if fu.HasNextTable(flow) && flow.TableId == 0 { // Unicast OLT flow DL
Girish Kumarf56a4682020-03-20 20:07:46 +0000502 logger.Debugw("process-olt-downstream-noncontrollerbound-flow-with-nexttable", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800503 deviceRules, err = fd.processDownstreamFlowWithNextTable(ctx, agent, path, inPortNo, outPortNo, flow)
504 if err != nil {
505 return nil, err
506 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400507 } else if flow.TableId == 1 && outPortNo != 0 { // Unicast ONU flow DL
Girish Kumarf56a4682020-03-20 20:07:46 +0000508 logger.Debugw("process-onu-downstream-unicast-flow", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800509 deviceRules, err = fd.processUnicastFlow(ctx, path, inPortNo, outPortNo, flow)
510 if err != nil {
511 return nil, err
512 }
npujar1d86a522019-11-14 17:11:16 +0530513 } else if grpID := fu.GetGroup(flow); grpID != 0 && flow.TableId == 0 { //Multicast
Girish Kumarf56a4682020-03-20 20:07:46 +0000514 logger.Debugw("process-multicast-flow", log.Fields{"flows": flow})
khenaidoo820197c2020-02-13 16:35:33 -0500515 deviceRules = fd.processMulticastFlow(ctx, path, inPortNo, outPortNo, flow, grpID, groupMap)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400516 } else {
khenaidoo820197c2020-02-13 16:35:33 -0500517 return deviceRules, status.Errorf(codes.Aborted, "unknown downstream flow %v", *flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400518 }
519 }
Scott Bakerfdea1e32020-02-21 15:35:41 -0800520 deviceRules, err = fd.updateOutputPortForControllerBoundFlowForParentDevide(flow, deviceRules)
521 return deviceRules, err
khenaidoo89b0e942018-10-21 21:11:33 -0400522}