blob: e5e955adfc25b7e8cbd6ad148d83605198c2dd2c [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"
khenaidoo820197c2020-02-13 16:35:33 -050024 "github.com/opencord/voltha-go/rw_core/route"
khenaidood948f772021-08-11 17:49:24 -040025 fu "github.com/opencord/voltha-lib-go/v7/pkg/flows"
26 "github.com/opencord/voltha-lib-go/v7/pkg/log"
27 ofp "github.com/opencord/voltha-protos/v5/go/openflow_13"
28 "github.com/opencord/voltha-protos/v5/go/voltha"
khenaidoo820197c2020-02-13 16:35:33 -050029 "google.golang.org/grpc/codes"
30 "google.golang.org/grpc/status"
khenaidoo89b0e942018-10-21 21:11:33 -040031)
32
npujar1d86a522019-11-14 17:11:16 +053033// FlowDecomposer represent flow decomposer attribute
khenaidoo89b0e942018-10-21 21:11:33 -040034type FlowDecomposer struct {
Kent Hagerman6031aad2020-07-29 16:36:33 -040035 getDevice GetDeviceFunc
36}
37
38// DeviceManager represents a generic device manager
39type GetDeviceFunc func(context.Context, string) (*voltha.Device, error)
40
41type LogicalDeviceAgent interface {
42 GetDeviceRoutes() *route.DeviceRoutes
43 GetWildcardInputPorts(ctx context.Context, excludePort uint32) map[uint32]struct{}
44 GetRoute(ctx context.Context, ingressPortNo uint32, egressPortNo uint32) ([]route.Hop, error)
45 GetNNIPorts() map[uint32]struct{}
khenaidoo89b0e942018-10-21 21:11:33 -040046}
47
npujar1d86a522019-11-14 17:11:16 +053048// NewFlowDecomposer creates flow decomposer instance
Kent Hagerman6031aad2020-07-29 16:36:33 -040049func NewFlowDecomposer(getDevice GetDeviceFunc) *FlowDecomposer {
50 return &FlowDecomposer{getDevice: getDevice}
khenaidoo89b0e942018-10-21 21:11:33 -040051}
52
53//DecomposeRules decomposes per-device flows and flow-groups from the flows and groups defined on a logical device
Kent Hagerman6031aad2020-07-29 16:36:33 -040054func (fd *FlowDecomposer) DecomposeRules(ctx context.Context, agent LogicalDeviceAgent, flows map[uint64]*ofp.OfpFlowStats, groups map[uint32]*ofp.OfpGroupEntry) (*fu.DeviceRules, error) {
khenaidoo3306c992019-05-24 16:57:35 -040055 deviceRules := *fu.NewDeviceRules()
khenaidoo2c6a0992019-04-29 13:46:56 -040056 devicesToUpdate := make(map[string]string)
khenaidoo89b0e942018-10-21 21:11:33 -040057
Kent Hagerman433a31a2020-05-20 19:04:48 -040058 for _, flow := range flows {
59 decomposedRules, err := fd.decomposeFlow(ctx, agent, flow, groups)
khenaidoo820197c2020-02-13 16:35:33 -050060 if err != nil {
61 return nil, err
62 }
npujar1d86a522019-11-14 17:11:16 +053063 for deviceID, flowAndGroups := range decomposedRules.Rules {
64 deviceRules.CreateEntryIfNotExist(deviceID)
65 deviceRules.Rules[deviceID].AddFrom(flowAndGroups)
66 devicesToUpdate[deviceID] = deviceID
khenaidoo89b0e942018-10-21 21:11:33 -040067 }
68 }
khenaidoo820197c2020-02-13 16:35:33 -050069 return deviceRules.FilterRules(devicesToUpdate), nil
khenaidoo89b0e942018-10-21 21:11:33 -040070}
71
khenaidoo19d7b632018-10-30 10:49:50 -040072// Handles special case of any controller-bound flow for a parent device
Elia Battiston509fdc72022-01-04 13:28:09 +010073func (fd *FlowDecomposer) updateOutputPortForControllerBoundFlowForParentDevice(ctx context.Context, dr *fu.DeviceRules) (*fu.DeviceRules, error) {
khenaidoo68c930b2019-05-13 11:46:51 -040074 EAPOL := fu.EthType(0x888e)
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -030075 PPPoED := fu.EthType(0x8863)
khenaidoo68c930b2019-05-13 11:46:51 -040076 IGMP := fu.IpProto(2)
77 UDP := fu.IpProto(17)
khenaidoo19d7b632018-10-30 10:49:50 -040078
79 newDeviceRules := dr.Copy()
80 // Check whether we are dealing with a parent device
npujar1d86a522019-11-14 17:11:16 +053081 for deviceID, fg := range dr.GetRules() {
Kent Hagerman6031aad2020-07-29 16:36:33 -040082 if device, err := fd.getDevice(ctx, deviceID); err == nil && device.Root {
npujar1d86a522019-11-14 17:11:16 +053083 newDeviceRules.ClearFlows(deviceID)
khenaidoo19d7b632018-10-30 10:49:50 -040084 for i := 0; i < fg.Flows.Len(); i++ {
85 f := fg.GetFlow(i)
86 UpdateOutPortNo := false
khenaidoo68c930b2019-05-13 11:46:51 -040087 for _, field := range fu.GetOfbFields(f) {
khenaidoo19d7b632018-10-30 10:49:50 -040088 UpdateOutPortNo = (field.String() == EAPOL.String())
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -030089 UpdateOutPortNo = UpdateOutPortNo || (field.String() == PPPoED.String())
khenaidoo19d7b632018-10-30 10:49:50 -040090 UpdateOutPortNo = UpdateOutPortNo || (field.String() == IGMP.String())
91 UpdateOutPortNo = UpdateOutPortNo || (field.String() == UDP.String())
92 if UpdateOutPortNo {
93 break
94 }
95 }
96 if UpdateOutPortNo {
khenaidoo68c930b2019-05-13 11:46:51 -040097 f = fu.UpdateOutputPortByActionType(f, uint32(ofp.OfpInstructionType_OFPIT_APPLY_ACTIONS),
khenaidoo19d7b632018-10-30 10:49:50 -040098 uint32(ofp.OfpPortNo_OFPP_CONTROLLER))
99 }
100 // Update flow Id as a change in the instruction field will result in a new flow ID
Mahir Gunyeladdb66a2020-04-29 18:08:50 -0700101 //var err error
102 //if f.Id, err = fu.HashFlowStats(f); err != nil {
103 //return nil, err
104 //}
npujar1d86a522019-11-14 17:11:16 +0530105 newDeviceRules.AddFlow(deviceID, (proto.Clone(f)).(*ofp.OfpFlowStats))
khenaidoo19d7b632018-10-30 10:49:50 -0400106 }
107 }
108 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400109
Scott Bakerfdea1e32020-02-21 15:35:41 -0800110 return newDeviceRules, nil
khenaidoo19d7b632018-10-30 10:49:50 -0400111}
112
khenaidood20a5852018-10-22 22:09:55 -0400113//processControllerBoundFlow decomposes trap flows
Kent Hagerman6031aad2020-07-29 16:36:33 -0400114func (fd *FlowDecomposer) processControllerBoundFlow(ctx context.Context, agent LogicalDeviceAgent, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800115 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400116
Rohan Agrawal31f21802020-06-12 05:38:46 +0000117 logger.Debugw(ctx, "trap-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo, "flow": flow})
khenaidood20a5852018-10-22 22:09:55 -0400118 deviceRules := fu.NewDeviceRules()
npujar1d86a522019-11-14 17:11:16 +0530119 meterID := fu.GetMeterIdFromFlow(flow)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000120 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
khenaidood20a5852018-10-22 22:09:55 -0400121
khenaidoo820197c2020-02-13 16:35:33 -0500122 ingressHop := path[0]
123 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400124
Humera Kouser4ff89012019-08-25 19:01:51 -0400125 //case of packet_in from NNI port rule
khenaidoo820197c2020-02-13 16:35:33 -0500126 if agent.GetDeviceRoutes().IsRootPort(inPortNo) {
Humera Kouser4ff89012019-08-25 19:01:51 -0400127 // Trap flow for NNI port
Rohan Agrawal31f21802020-06-12 05:38:46 +0000128 logger.Debug(ctx, "trap-nni")
Humera Kouser4ff89012019-08-25 19:01:51 -0400129
npujar1d86a522019-11-14 17:11:16 +0530130 fa := &fu.FlowArgs{
Humera Kouser4ff89012019-08-25 19:01:51 -0400131 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie},
132 MatchFields: []*ofp.OfpOxmOfbField{
133 fu.InPort(egressHop.Egress),
134 },
135 Actions: fu.GetActions(flow),
136 }
137 // Augment the matchfields with the ofpfields from the flow
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400138 fg := fu.NewFlowsAndGroups()
Humera Kouser4ff89012019-08-25 19:01:51 -0400139 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800140 fs, err := fu.MkFlowStat(fa)
141 if err != nil {
142 return nil, err
143 }
144 fg.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400145 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400146 } else {
147 // Trap flow for UNI port
Rohan Agrawal31f21802020-06-12 05:38:46 +0000148 logger.Debug(ctx, "trap-uni")
Girish Gowdra9a50f032020-09-16 13:21:10 -0700149 var setVid, setPcp uint32
150 var setVidOk, setPcpOk bool
Matt Jeannerete75f2842020-03-14 15:45:12 -0400151 //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 -0400152 var inPorts = map[uint32]struct{}{inPortNo: {}}
khenaidoo89b0e942018-10-21 21:11:33 -0400153 if inPortNo == 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000154 inPorts = agent.GetWildcardInputPorts(ctx, egressHop.Egress) // exclude egress_hop.egress_port.port_no
khenaidoo89b0e942018-10-21 21:11:33 -0400155 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400156 for inputPort := range inPorts {
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400157 // Upstream flow on parent (olt) device
yasin sapli5458a1c2021-06-14 22:24:38 +0000158 // Olt meters for upstream trap flows are carried on writeMetadata for Multi UNI
159 oltMeterId := fu.GetMeterIdFromWriteMetadata(ctx, flow)
160 if oltMeterId == 0 {
161 oltMeterId = meterID
162 } else {
163 fu.SetMeterIdToFlow(flow, oltMeterId)
164 }
npujar1d86a522019-11-14 17:11:16 +0530165 faParent := &fu.FlowArgs{
yasin sapli5458a1c2021-06-14 22:24:38 +0000166 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(oltMeterId), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400167 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400168 fu.InPort(egressHop.Ingress),
khenaidoo68c930b2019-05-13 11:46:51 -0400169 fu.TunnelId(uint64(inputPort)),
khenaidoo89b0e942018-10-21 21:11:33 -0400170 },
171 Actions: []*ofp.OfpAction{
khenaidoo68c930b2019-05-13 11:46:51 -0400172 fu.Output(egressHop.Egress),
khenaidoo89b0e942018-10-21 21:11:33 -0400173 },
174 }
Girish Gowdra9a50f032020-09-16 13:21:10 -0700175 // Augment the parent device flow matchfields with the ofpfields from the flow
176 faParent.MatchFields = append(faParent.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT, fu.VLAN_VID, fu.VLAN_PCP)...)
177 // Augment the parent device flow matchfields with vlan vid and vlan pcp from action field.
178 // The child device is going to set the vlan and pcp and parent device has to match on them
179 if setVid, setVidOk = fu.GetSetActionField(ctx, flow, fu.VLAN_VID); setVidOk {
180 faParent.MatchFields = append(faParent.MatchFields, fu.VlanVid(setVid))
181 if setPcp, setPcpOk = fu.GetSetActionField(ctx, flow, fu.VLAN_PCP); setPcpOk {
182 faParent.MatchFields = append(faParent.MatchFields, fu.VlanPcp(setPcp))
183 }
184 }
185
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400186 fgParent := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800187 fs, err := fu.MkFlowStat(faParent)
188 if err != nil {
189 return nil, err
190 }
191 fgParent.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400192 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fgParent)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000193 logger.Debugw(ctx, "parent-trap-flow-set", log.Fields{"flow": faParent})
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400194
195 // Upstream flow on child (onu) device
196 var actions []*ofp.OfpAction
Girish Gowdra9a50f032020-09-16 13:21:10 -0700197 if setVidOk {
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400198 // have this child push the vlan the parent is matching/trapping on above
199 actions = []*ofp.OfpAction{
200 fu.PushVlan(0x8100),
Girish Gowdra9a50f032020-09-16 13:21:10 -0700201 fu.SetField(fu.VlanVid(setVid)),
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400202 fu.Output(ingressHop.Egress),
203 }
Girish Gowdra9a50f032020-09-16 13:21:10 -0700204 if setPcpOk {
205 actions = append(actions, fu.SetField(fu.VlanPcp(setPcp)))
206 }
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400207 } else {
208 // otherwise just set the egress port
209 actions = []*ofp.OfpAction{
210 fu.Output(ingressHop.Egress),
211 }
212 }
npujar1d86a522019-11-14 17:11:16 +0530213 faChild := &fu.FlowArgs{
214 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400215 MatchFields: []*ofp.OfpOxmOfbField{
216 fu.InPort(ingressHop.Ingress),
217 fu.TunnelId(uint64(inputPort)),
218 },
219 Actions: actions,
220 }
221 // Augment the matchfields with the ofpfields from the flow.
222 // If the parent has a match vid and the child is setting that match vid exclude the the match vlan
223 // for the child given it will be setting that vlan and the parent will be matching on it
Girish Gowdra9a50f032020-09-16 13:21:10 -0700224 faChild.MatchFields = append(faChild.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400225 fgChild := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800226 fs, err = fu.MkFlowStat(faChild)
227 if err != nil {
228 return nil, err
229 }
230 fgChild.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400231 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fgChild)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000232 logger.Debugw(ctx, "child-trap-flow-set", log.Fields{"flow": faChild})
khenaidoo89b0e942018-10-21 21:11:33 -0400233 }
234 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400235
Scott Bakerfdea1e32020-02-21 15:35:41 -0800236 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400237}
238
239// processUpstreamNonControllerBoundFlow processes non-controller bound flow. We assume that anything that is
240// upstream needs to get Q-in-Q treatment and that this is expressed via two flow rules, the first using the
241// goto-statement. We also assume that the inner tag is applied at the ONU, while the outer tag is
242// applied at the OLT
khenaidoo820197c2020-02-13 16:35:33 -0500243func (fd *FlowDecomposer) processUpstreamNonControllerBoundFlow(ctx context.Context,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800244 path []route.Hop, inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400245
Rohan Agrawal31f21802020-06-12 05:38:46 +0000246 logger.Debugw(ctx, "upstream-non-controller-bound-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400247 deviceRules := fu.NewDeviceRules()
248
npujar1d86a522019-11-14 17:11:16 +0530249 meterID := fu.GetMeterIdFromFlow(flow)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000250 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400251
khenaidoo820197c2020-02-13 16:35:33 -0500252 ingressHop := path[0]
253 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400254
Manikkaraj kb1a10922019-07-29 12:10:34 -0400255 if flow.TableId == 0 && fu.HasNextTable(flow) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000256 logger.Debugw(ctx, "decomposing-onu-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
khenaidoo89b0e942018-10-21 21:11:33 -0400257 if outPortNo != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000258 logger.Warnw(ctx, "outPort-should-not-be-specified", log.Fields{"outPortNo": outPortNo})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800259 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400260 }
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},
khenaidoo89b0e942018-10-21 21:11:33 -0400263 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400264 fu.InPort(ingressHop.Ingress),
265 fu.TunnelId(uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400266 },
khenaidoo68c930b2019-05-13 11:46:51 -0400267 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400268 }
269 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400270 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400271
272 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400273 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400274
khenaidoo89b0e942018-10-21 21:11:33 -0400275 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800276 fs, err := fu.MkFlowStat(fa)
277 if err != nil {
278 return nil, err
279 }
280 fg.AddFlow(fs)
khenaidood20a5852018-10-22 22:09:55 -0400281 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400282 } else if flow.TableId == 1 && outPortNo != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000283 logger.Debugw(ctx, "decomposing-olt-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
npujar1d86a522019-11-14 17:11:16 +0530284 fa := &fu.FlowArgs{
285 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Manikkaraj kb1a10922019-07-29 12:10:34 -0400286 MatchFields: []*ofp.OfpOxmOfbField{
287 fu.InPort(egressHop.Ingress),
288 fu.TunnelId(uint64(inPortNo)),
289 },
khenaidoo89b0e942018-10-21 21:11:33 -0400290 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400291 // Augment the matchfields with the ofpfields from the flow
292 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidoo89b0e942018-10-21 21:11:33 -0400293
Manikkaraj kb1a10922019-07-29 12:10:34 -0400294 //Augment the actions
295 filteredAction := fu.GetActions(flow, fu.OUTPUT)
296 filteredAction = append(filteredAction, fu.Output(egressHop.Egress))
297 fa.Actions = filteredAction
khenaidood20a5852018-10-22 22:09:55 -0400298
Manikkaraj kb1a10922019-07-29 12:10:34 -0400299 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800300 fs, err := fu.MkFlowStat(fa)
301 if err != nil {
302 return nil, err
303 }
304 fg.AddFlow(fs)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400305 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400306 }
Scott Bakerfdea1e32020-02-21 15:35:41 -0800307 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400308}
309
khenaidood20a5852018-10-22 22:09:55 -0400310// processDownstreamFlowWithNextTable decomposes downstream flows containing next table ID instructions
Kent Hagerman6031aad2020-07-29 16:36:33 -0400311func (fd *FlowDecomposer) processDownstreamFlowWithNextTable(ctx context.Context, agent LogicalDeviceAgent, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800312 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000313 logger.Debugw(ctx, "decomposing-olt-flow-in-downstream-flow-with-next-table", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400314 deviceRules := fu.NewDeviceRules()
npujar1d86a522019-11-14 17:11:16 +0530315 meterID := fu.GetMeterIdFromFlow(flow)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000316 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
khenaidood20a5852018-10-22 22:09:55 -0400317
khenaidoo89b0e942018-10-21 21:11:33 -0400318 if outPortNo != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000319 logger.Warnw(ctx, "outPort-should-not-be-specified", log.Fields{"outPortNo": outPortNo})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800320 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400321 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400322
khenaidoo820197c2020-02-13 16:35:33 -0500323 ingressHop := path[0]
324 egressHop := path[1]
Manikkaraj kb1a10922019-07-29 12:10:34 -0400325 if metadataFromwriteMetadata != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000326 logger.Debugw(ctx, "creating-metadata-flow", log.Fields{"flow": flow})
327 portNumber := fu.GetEgressPortNumberFromWriteMetadata(ctx, flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400328 if portNumber != 0 {
khenaidoo820197c2020-02-13 16:35:33 -0500329 recalculatedRoute, err := agent.GetRoute(ctx, inPortNo, portNumber)
330 if err != nil {
Girish Gowdra63a90a52022-02-25 16:51:13 -0800331 logger.Errorw(ctx, "no-route", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo, "metadata": metadataFromwriteMetadata, "error": err})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800332 return deviceRules, nil
khenaidoo820197c2020-02-13 16:35:33 -0500333 }
khenaidoo89b0e942018-10-21 21:11:33 -0400334 switch len(recalculatedRoute) {
335 case 0:
Girish Gowdra63a90a52022-02-25 16:51:13 -0800336 logger.Errorw(ctx, "no-route", log.Fields{"inPortNo": inPortNo, "outPortNo": portNumber, "comment": "deleting-flow", "metadata": metadataFromwriteMetadata})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400337 //TODO: Delete flow
Scott Bakerfdea1e32020-02-21 15:35:41 -0800338 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400339 case 2:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000340 logger.Debugw(ctx, "route-found", log.Fields{"ingressHop": ingressHop, "egressHop": egressHop})
khenaidoo89b0e942018-10-21 21:11:33 -0400341 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000342 logger.Errorw(ctx, "invalid-route-length", log.Fields{"routeLen": len(path)})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800343 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400344 }
345 ingressHop = recalculatedRoute[0]
346 }
Girish Gowdra63a90a52022-02-25 16:51:13 -0800347
npujar1d86a522019-11-14 17:11:16 +0530348 fa := &fu.FlowArgs{
349 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400350 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400351 fu.InPort(ingressHop.Ingress),
khenaidoo68c930b2019-05-13 11:46:51 -0400352 fu.TunnelId(uint64(portNumber)),
khenaidoo89b0e942018-10-21 21:11:33 -0400353 },
khenaidoo68c930b2019-05-13 11:46:51 -0400354 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400355 }
Girish Gowdra63a90a52022-02-25 16:51:13 -0800356
357 // Augment the metadata with innerTag if it is valid
358 innerTag := fu.GetInnerTagFromMetaData(ctx, flow)
359 if innerTag != 0 {
360 fa.MatchFields = append(fa.MatchFields, []*ofp.OfpOxmOfbField{fu.Metadata_ofp(uint64(innerTag))}...)
361 }
362
khenaidoo89b0e942018-10-21 21:11:33 -0400363 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400364 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT, fu.METADATA)...)
khenaidood20a5852018-10-22 22:09:55 -0400365
366 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400367 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400368
khenaidoo89b0e942018-10-21 21:11:33 -0400369 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800370 fs, err := fu.MkFlowStat(fa)
371 if err != nil {
372 return nil, err
373 }
374 fg.AddFlow(fs)
khenaidoo89b0e942018-10-21 21:11:33 -0400375 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
376 } else { // Create standard flow
Rohan Agrawal31f21802020-06-12 05:38:46 +0000377 logger.Debugw(ctx, "creating-standard-flow", log.Fields{"flow": flow})
npujar1d86a522019-11-14 17:11:16 +0530378 fa := &fu.FlowArgs{
379 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400380 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400381 fu.InPort(ingressHop.Ingress),
382 fu.TunnelId(uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400383 },
khenaidoo68c930b2019-05-13 11:46:51 -0400384 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400385 }
386 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400387 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400388
389 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400390 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400391
khenaidoo89b0e942018-10-21 21:11:33 -0400392 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800393 fs, err := fu.MkFlowStat(fa)
394 if err != nil {
395 return nil, err
396 }
397 fg.AddFlow(fs)
khenaidoo89b0e942018-10-21 21:11:33 -0400398 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
399 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400400
Scott Bakerfdea1e32020-02-21 15:35:41 -0800401 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400402}
403
khenaidood20a5852018-10-22 22:09:55 -0400404// processUnicastFlow decomposes unicast flows
khenaidoo820197c2020-02-13 16:35:33 -0500405func (fd *FlowDecomposer) processUnicastFlow(ctx context.Context, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800406 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400407
Rohan Agrawal31f21802020-06-12 05:38:46 +0000408 logger.Debugw(ctx, "decomposing-onu-flow-in-downstream-unicast-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400409 deviceRules := fu.NewDeviceRules()
410
khenaidoo820197c2020-02-13 16:35:33 -0500411 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400412
npujar1d86a522019-11-14 17:11:16 +0530413 meterID := fu.GetMeterIdFromFlow(flow)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000414 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
npujar1d86a522019-11-14 17:11:16 +0530415 fa := &fu.FlowArgs{
416 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Manikkaraj kb1a10922019-07-29 12:10:34 -0400417 MatchFields: []*ofp.OfpOxmOfbField{
418 fu.InPort(egressHop.Ingress),
419 },
khenaidoo89b0e942018-10-21 21:11:33 -0400420 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400421 // Augment the matchfields with the ofpfields from the flow
422 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400423
Manikkaraj kb1a10922019-07-29 12:10:34 -0400424 // Augment the Actions
425 filteredAction := fu.GetActions(flow, fu.OUTPUT)
426 filteredAction = append(filteredAction, fu.Output(egressHop.Egress))
427 fa.Actions = filteredAction
khenaidoo89b0e942018-10-21 21:11:33 -0400428
Manikkaraj kb1a10922019-07-29 12:10:34 -0400429 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800430 fs, err := fu.MkFlowStat(fa)
431 if err != nil {
432 return nil, err
433 }
434 fg.AddFlow(fs)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400435 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800436 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400437}
438
khenaidood20a5852018-10-22 22:09:55 -0400439// processMulticastFlow decompose multicast flows
khenaidoo820197c2020-02-13 16:35:33 -0500440func (fd *FlowDecomposer) processMulticastFlow(ctx context.Context, path []route.Hop,
npujar1d86a522019-11-14 17:11:16 +0530441 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats, grpID uint32,
khenaidood20a5852018-10-22 22:09:55 -0400442 groupMap map[uint32]*ofp.OfpGroupEntry) *fu.DeviceRules {
443
Rohan Agrawal31f21802020-06-12 05:38:46 +0000444 logger.Debugw(ctx, "multicast-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400445 deviceRules := fu.NewDeviceRules()
khenaidoo89b0e942018-10-21 21:11:33 -0400446
447 //having no Group yet is the same as having a Group with no buckets
448 var grp *ofp.OfpGroupEntry
449 var ok bool
npujar1d86a522019-11-14 17:11:16 +0530450 if grp, ok = groupMap[grpID]; !ok {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000451 logger.Warnw(ctx, "Group-id-not-present-in-map", log.Fields{"grpId": grpID, "groupMap": groupMap})
khenaidoo89b0e942018-10-21 21:11:33 -0400452 return deviceRules
453 }
454 if grp == nil || grp.Desc == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000455 logger.Warnw(ctx, "Group-or-desc-nil", log.Fields{"grpId": grpID, "grp": grp})
khenaidoo89b0e942018-10-21 21:11:33 -0400456 return deviceRules
457 }
khenaidoo89b0e942018-10-21 21:11:33 -0400458
khenaidoo820197c2020-02-13 16:35:33 -0500459 deviceRules.CreateEntryIfNotExist(path[0].DeviceID)
Esin Karaman09959ae2019-11-29 13:59:58 +0000460 fg := fu.NewFlowsAndGroups()
461 fg.AddFlow(flow)
462 //return the multicast flow without decomposing it
khenaidoo820197c2020-02-13 16:35:33 -0500463 deviceRules.AddFlowsAndGroup(path[0].DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400464 return deviceRules
465}
466
khenaidood20a5852018-10-22 22:09:55 -0400467// decomposeFlow decomposes a flow for a logical device into flows for each physical device
Kent Hagerman6031aad2020-07-29 16:36:33 -0400468func (fd *FlowDecomposer) decomposeFlow(ctx context.Context, agent LogicalDeviceAgent, flow *ofp.OfpFlowStats,
khenaidoo820197c2020-02-13 16:35:33 -0500469 groupMap map[uint32]*ofp.OfpGroupEntry) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400470
khenaidoo68c930b2019-05-13 11:46:51 -0400471 inPortNo := fu.GetInPort(flow)
Esin Karaman09959ae2019-11-29 13:59:58 +0000472 if fu.HasGroup(flow) && inPortNo == 0 {
473 //if no in-port specified for a multicast flow, put NNI port as in-port
khenaidoo820197c2020-02-13 16:35:33 -0500474 //so that a valid path can be found for the flow
Esin Karaman09959ae2019-11-29 13:59:58 +0000475 nniPorts := agent.GetNNIPorts()
476 if len(nniPorts) > 0 {
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400477 for port := range nniPorts {
478 inPortNo = port
479 break
480 }
Rohan Agrawal31f21802020-06-12 05:38:46 +0000481 logger.Debugw(ctx, "assigning-nni-port-as-in-port-for-multicast-flow", log.Fields{"nni": inPortNo, "flow:": flow})
Esin Karaman09959ae2019-11-29 13:59:58 +0000482 }
483 }
khenaidoo68c930b2019-05-13 11:46:51 -0400484 outPortNo := fu.GetOutPort(flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400485 deviceRules := fu.NewDeviceRules()
khenaidoo820197c2020-02-13 16:35:33 -0500486 path, err := agent.GetRoute(ctx, inPortNo, outPortNo)
487 if err != nil {
khenaidoo820197c2020-02-13 16:35:33 -0500488 return deviceRules, err
489 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400490
khenaidoo820197c2020-02-13 16:35:33 -0500491 switch len(path) {
khenaidoo89b0e942018-10-21 21:11:33 -0400492 case 0:
khenaidoo787224a2020-04-16 18:08:47 -0400493 return deviceRules, fmt.Errorf("no route from:%d to:%d :%w", inPortNo, outPortNo, route.ErrNoRoute)
khenaidoo89b0e942018-10-21 21:11:33 -0400494 case 2:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000495 logger.Debugw(ctx, "route-found", log.Fields{"ingressHop": path[0], "egressHop": path[1]})
khenaidoo89b0e942018-10-21 21:11:33 -0400496 default:
khenaidoo787224a2020-04-16 18:08:47 -0400497 return deviceRules, fmt.Errorf("invalid route length %d :%w", len(path), route.ErrNoRoute)
khenaidoo89b0e942018-10-21 21:11:33 -0400498 }
499
khenaidoo89b0e942018-10-21 21:11:33 -0400500 // Process controller bound flow
501 if outPortNo != 0 && (outPortNo&0x7fffffff) == uint32(ofp.OfpPortNo_OFPP_CONTROLLER) {
Scott Bakerfdea1e32020-02-21 15:35:41 -0800502 deviceRules, err = fd.processControllerBoundFlow(ctx, agent, path, inPortNo, outPortNo, flow)
503 if err != nil {
504 return nil, err
505 }
khenaidoo89b0e942018-10-21 21:11:33 -0400506 } else {
khenaidoo297cd252019-02-07 22:10:23 -0500507 var ingressDevice *voltha.Device
508 var err error
Kent Hagerman6031aad2020-07-29 16:36:33 -0400509 if ingressDevice, err = fd.getDevice(ctx, path[0].DeviceID); err != nil {
khenaidoo787224a2020-04-16 18:08:47 -0400510 // This can happen in a race condition where a device is deleted right after we obtain a
511 // route involving the device (GetRoute() above). Handle it as a no route event as well.
512 return deviceRules, fmt.Errorf("get-device-error :%v :%w", err, route.ErrNoRoute)
khenaidoo297cd252019-02-07 22:10:23 -0500513 }
514 isUpstream := !ingressDevice.Root
Manikkaraj kb1a10922019-07-29 12:10:34 -0400515 if isUpstream { // Unicast OLT and ONU UL
ssiddiqui21e54c32021-07-27 11:30:46 +0530516 logger.Debug(ctx, "process-olt-and-onu-upstream-non-controller-bound-uni-cast-flows", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800517 deviceRules, err = fd.processUpstreamNonControllerBoundFlow(ctx, path, inPortNo, outPortNo, flow)
518 if err != nil {
519 return nil, err
520 }
ssiddiqui21e54c32021-07-27 11:30:46 +0530521 } else if fu.HasNextTable(flow) && (flow.GetTableId() == 0 || flow.GetTableId() == 1) { // Unicast OLT Flow
522 // For 'Non-MPLS' flows, this condition will only be true for table-id 0 as only table-id 0 will have the
523 // 'go-to-next-table' instruction
524 // For 'MPLS' flows, this condition will be true for table-id 0 and table-id 1.
525 // So the flow here shall always be an OLT flow
526 logger.Debugw(ctx, "process-olt-downstream-non-controller-bound-flow-with-next-table", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800527 deviceRules, err = fd.processDownstreamFlowWithNextTable(ctx, agent, path, inPortNo, outPortNo, flow)
528 if err != nil {
529 return nil, err
530 }
ssiddiqui21e54c32021-07-27 11:30:46 +0530531 } else if (flow.GetTableId() == 1 || flow.GetTableId() == 2) && outPortNo != 0 { // Unicast ONU flow DL
532 // If this is an MPLS OLT flow (table-id 1, transition-to-table 2), the condition above will already be hit.
533 // So if we are reaching this point, the flow shall always be an ONU flow
534 logger.Debugw(ctx, "process-onu-downstream-uni-cast-flow", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800535 deviceRules, err = fd.processUnicastFlow(ctx, path, inPortNo, outPortNo, flow)
536 if err != nil {
537 return nil, err
538 }
npujar1d86a522019-11-14 17:11:16 +0530539 } else if grpID := fu.GetGroup(flow); grpID != 0 && flow.TableId == 0 { //Multicast
Rohan Agrawal31f21802020-06-12 05:38:46 +0000540 logger.Debugw(ctx, "process-multicast-flow", log.Fields{"flows": flow})
khenaidoo820197c2020-02-13 16:35:33 -0500541 deviceRules = fd.processMulticastFlow(ctx, path, inPortNo, outPortNo, flow, grpID, groupMap)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400542 } else {
khenaidoo820197c2020-02-13 16:35:33 -0500543 return deviceRules, status.Errorf(codes.Aborted, "unknown downstream flow %v", *flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400544 }
545 }
Elia Battiston509fdc72022-01-04 13:28:09 +0100546 deviceRules, err = fd.updateOutputPortForControllerBoundFlowForParentDevice(ctx, deviceRules)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800547 return deviceRules, err
khenaidoo89b0e942018-10-21 21:11:33 -0400548}