blob: 58fcb83ab5903273de58a2bbcaf3ca3b659de803 [file] [log] [blame]
khenaidoo89b0e942018-10-21 21:11:33 -04001/*
Joey Armstrong7a9af442024-01-03 19:26:36 -05002 * Copyright 2018-2024 Open Networking Foundation (ONF) and the ONF Contributors
khenaidoo89b0e942018-10-21 21:11:33 -04003 *
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
Joey Armstrong393daca2023-07-06 08:47:54 -040053// 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
Akash Reddy Kankanala929cc002025-04-08 15:05:21 +053073// nolint: unparam
Elia Battiston509fdc72022-01-04 13:28:09 +010074func (fd *FlowDecomposer) updateOutputPortForControllerBoundFlowForParentDevice(ctx context.Context, dr *fu.DeviceRules) (*fu.DeviceRules, error) {
khenaidoo68c930b2019-05-13 11:46:51 -040075 EAPOL := fu.EthType(0x888e)
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -030076 PPPoED := fu.EthType(0x8863)
khenaidoo68c930b2019-05-13 11:46:51 -040077 IGMP := fu.IpProto(2)
78 UDP := fu.IpProto(17)
khenaidoo19d7b632018-10-30 10:49:50 -040079
80 newDeviceRules := dr.Copy()
Akash Reddy Kankanala929cc002025-04-08 15:05:21 +053081 // Check whether we are dealing with a parent device
npujar1d86a522019-11-14 17:11:16 +053082 for deviceID, fg := range dr.GetRules() {
Kent Hagerman6031aad2020-07-29 16:36:33 -040083 if device, err := fd.getDevice(ctx, deviceID); err == nil && device.Root {
npujar1d86a522019-11-14 17:11:16 +053084 newDeviceRules.ClearFlows(deviceID)
khenaidoo19d7b632018-10-30 10:49:50 -040085 for i := 0; i < fg.Flows.Len(); i++ {
86 f := fg.GetFlow(i)
87 UpdateOutPortNo := false
khenaidoo68c930b2019-05-13 11:46:51 -040088 for _, field := range fu.GetOfbFields(f) {
khenaidoo19d7b632018-10-30 10:49:50 -040089 UpdateOutPortNo = (field.String() == EAPOL.String())
Marcos Aurelio Carrero (Furukawa)a61a72c2021-01-28 13:48:20 -030090 UpdateOutPortNo = UpdateOutPortNo || (field.String() == PPPoED.String())
khenaidoo19d7b632018-10-30 10:49:50 -040091 UpdateOutPortNo = UpdateOutPortNo || (field.String() == IGMP.String())
92 UpdateOutPortNo = UpdateOutPortNo || (field.String() == UDP.String())
93 if UpdateOutPortNo {
94 break
95 }
96 }
97 if UpdateOutPortNo {
khenaidoo68c930b2019-05-13 11:46:51 -040098 f = fu.UpdateOutputPortByActionType(f, uint32(ofp.OfpInstructionType_OFPIT_APPLY_ACTIONS),
khenaidoo19d7b632018-10-30 10:49:50 -040099 uint32(ofp.OfpPortNo_OFPP_CONTROLLER))
100 }
npujar1d86a522019-11-14 17:11:16 +0530101 newDeviceRules.AddFlow(deviceID, (proto.Clone(f)).(*ofp.OfpFlowStats))
khenaidoo19d7b632018-10-30 10:49:50 -0400102 }
103 }
104 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400105
Scott Bakerfdea1e32020-02-21 15:35:41 -0800106 return newDeviceRules, nil
khenaidoo19d7b632018-10-30 10:49:50 -0400107}
108
Joey Armstrong393daca2023-07-06 08:47:54 -0400109// processControllerBoundFlow decomposes trap flows
Kent Hagerman6031aad2020-07-29 16:36:33 -0400110func (fd *FlowDecomposer) processControllerBoundFlow(ctx context.Context, agent LogicalDeviceAgent, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800111 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400112
Rohan Agrawal31f21802020-06-12 05:38:46 +0000113 logger.Debugw(ctx, "trap-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo, "flow": flow})
khenaidood20a5852018-10-22 22:09:55 -0400114 deviceRules := fu.NewDeviceRules()
npujar1d86a522019-11-14 17:11:16 +0530115 meterID := fu.GetMeterIdFromFlow(flow)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000116 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
khenaidood20a5852018-10-22 22:09:55 -0400117
khenaidoo820197c2020-02-13 16:35:33 -0500118 ingressHop := path[0]
119 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400120
Akash Reddy Kankanala929cc002025-04-08 15:05:21 +0530121 // case of packet_in from NNI port rule
khenaidoo820197c2020-02-13 16:35:33 -0500122 if agent.GetDeviceRoutes().IsRootPort(inPortNo) {
Humera Kouser4ff89012019-08-25 19:01:51 -0400123 // Trap flow for NNI port
Rohan Agrawal31f21802020-06-12 05:38:46 +0000124 logger.Debug(ctx, "trap-nni")
Humera Kouser4ff89012019-08-25 19:01:51 -0400125
npujar1d86a522019-11-14 17:11:16 +0530126 fa := &fu.FlowArgs{
Humera Kouser4ff89012019-08-25 19:01:51 -0400127 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie},
128 MatchFields: []*ofp.OfpOxmOfbField{
129 fu.InPort(egressHop.Egress),
130 },
131 Actions: fu.GetActions(flow),
132 }
133 // Augment the matchfields with the ofpfields from the flow
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400134 fg := fu.NewFlowsAndGroups()
Humera Kouser4ff89012019-08-25 19:01:51 -0400135 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800136 fs, err := fu.MkFlowStat(fa)
137 if err != nil {
138 return nil, err
139 }
140 fg.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400141 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400142 } else {
143 // Trap flow for UNI port
Rohan Agrawal31f21802020-06-12 05:38:46 +0000144 logger.Debug(ctx, "trap-uni")
Girish Gowdra9a50f032020-09-16 13:21:10 -0700145 var setVid, setPcp uint32
146 var setVidOk, setPcpOk bool
Akash Reddy Kankanala929cc002025-04-08 15:05:21 +0530147 // 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 -0400148 var inPorts = map[uint32]struct{}{inPortNo: {}}
khenaidoo89b0e942018-10-21 21:11:33 -0400149 if inPortNo == 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000150 inPorts = agent.GetWildcardInputPorts(ctx, egressHop.Egress) // exclude egress_hop.egress_port.port_no
khenaidoo89b0e942018-10-21 21:11:33 -0400151 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400152 for inputPort := range inPorts {
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400153 // Upstream flow on parent (olt) device
yasin sapli5458a1c2021-06-14 22:24:38 +0000154 // Olt meters for upstream trap flows are carried on writeMetadata for Multi UNI
155 oltMeterId := fu.GetMeterIdFromWriteMetadata(ctx, flow)
156 if oltMeterId == 0 {
157 oltMeterId = meterID
158 } else {
159 fu.SetMeterIdToFlow(flow, oltMeterId)
160 }
npujar1d86a522019-11-14 17:11:16 +0530161 faParent := &fu.FlowArgs{
yasin sapli5458a1c2021-06-14 22:24:38 +0000162 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(oltMeterId), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400163 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400164 fu.InPort(egressHop.Ingress),
khenaidoo68c930b2019-05-13 11:46:51 -0400165 fu.TunnelId(uint64(inputPort)),
khenaidoo89b0e942018-10-21 21:11:33 -0400166 },
167 Actions: []*ofp.OfpAction{
khenaidoo68c930b2019-05-13 11:46:51 -0400168 fu.Output(egressHop.Egress),
khenaidoo89b0e942018-10-21 21:11:33 -0400169 },
170 }
Girish Gowdra9a50f032020-09-16 13:21:10 -0700171 // Augment the parent device flow matchfields with the ofpfields from the flow
172 faParent.MatchFields = append(faParent.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT, fu.VLAN_VID, fu.VLAN_PCP)...)
173 // Augment the parent device flow matchfields with vlan vid and vlan pcp from action field.
174 // The child device is going to set the vlan and pcp and parent device has to match on them
175 if setVid, setVidOk = fu.GetSetActionField(ctx, flow, fu.VLAN_VID); setVidOk {
176 faParent.MatchFields = append(faParent.MatchFields, fu.VlanVid(setVid))
177 if setPcp, setPcpOk = fu.GetSetActionField(ctx, flow, fu.VLAN_PCP); setPcpOk {
178 faParent.MatchFields = append(faParent.MatchFields, fu.VlanPcp(setPcp))
179 }
180 }
181
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400182 fgParent := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800183 fs, err := fu.MkFlowStat(faParent)
184 if err != nil {
185 return nil, err
186 }
187 fgParent.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400188 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fgParent)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000189 logger.Debugw(ctx, "parent-trap-flow-set", log.Fields{"flow": faParent})
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400190
191 // Upstream flow on child (onu) device
192 var actions []*ofp.OfpAction
Girish Gowdra9a50f032020-09-16 13:21:10 -0700193 if setVidOk {
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400194 // have this child push the vlan the parent is matching/trapping on above
195 actions = []*ofp.OfpAction{
196 fu.PushVlan(0x8100),
Girish Gowdra9a50f032020-09-16 13:21:10 -0700197 fu.SetField(fu.VlanVid(setVid)),
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400198 fu.Output(ingressHop.Egress),
199 }
Girish Gowdra9a50f032020-09-16 13:21:10 -0700200 if setPcpOk {
201 actions = append(actions, fu.SetField(fu.VlanPcp(setPcp)))
202 }
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400203 } else {
204 // otherwise just set the egress port
205 actions = []*ofp.OfpAction{
206 fu.Output(ingressHop.Egress),
207 }
208 }
npujar1d86a522019-11-14 17:11:16 +0530209 faChild := &fu.FlowArgs{
210 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400211 MatchFields: []*ofp.OfpOxmOfbField{
212 fu.InPort(ingressHop.Ingress),
213 fu.TunnelId(uint64(inputPort)),
214 },
215 Actions: actions,
216 }
217 // Augment the matchfields with the ofpfields from the flow.
218 // If the parent has a match vid and the child is setting that match vid exclude the the match vlan
219 // 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 -0700220 faChild.MatchFields = append(faChild.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400221 fgChild := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800222 fs, err = fu.MkFlowStat(faChild)
223 if err != nil {
224 return nil, err
225 }
226 fgChild.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400227 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fgChild)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000228 logger.Debugw(ctx, "child-trap-flow-set", log.Fields{"flow": faChild})
khenaidoo89b0e942018-10-21 21:11:33 -0400229 }
230 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400231
Scott Bakerfdea1e32020-02-21 15:35:41 -0800232 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400233}
234
235// processUpstreamNonControllerBoundFlow processes non-controller bound flow. We assume that anything that is
236// upstream needs to get Q-in-Q treatment and that this is expressed via two flow rules, the first using the
237// goto-statement. We also assume that the inner tag is applied at the ONU, while the outer tag is
238// applied at the OLT
khenaidoo820197c2020-02-13 16:35:33 -0500239func (fd *FlowDecomposer) processUpstreamNonControllerBoundFlow(ctx context.Context,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800240 path []route.Hop, inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400241
Rohan Agrawal31f21802020-06-12 05:38:46 +0000242 logger.Debugw(ctx, "upstream-non-controller-bound-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400243 deviceRules := fu.NewDeviceRules()
244
npujar1d86a522019-11-14 17:11:16 +0530245 meterID := fu.GetMeterIdFromFlow(flow)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000246 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400247
khenaidoo820197c2020-02-13 16:35:33 -0500248 ingressHop := path[0]
249 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400250
Manikkaraj kb1a10922019-07-29 12:10:34 -0400251 if flow.TableId == 0 && fu.HasNextTable(flow) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000252 logger.Debugw(ctx, "decomposing-onu-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
khenaidoo89b0e942018-10-21 21:11:33 -0400253 if outPortNo != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000254 logger.Warnw(ctx, "outPort-should-not-be-specified", log.Fields{"outPortNo": outPortNo})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800255 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400256 }
npujar1d86a522019-11-14 17:11:16 +0530257 fa := &fu.FlowArgs{
258 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400259 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400260 fu.InPort(ingressHop.Ingress),
261 fu.TunnelId(uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400262 },
khenaidoo68c930b2019-05-13 11:46:51 -0400263 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400264 }
265 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400266 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400267
268 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400269 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400270
khenaidoo89b0e942018-10-21 21:11:33 -0400271 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800272 fs, err := fu.MkFlowStat(fa)
273 if err != nil {
274 return nil, err
275 }
276 fg.AddFlow(fs)
khenaidood20a5852018-10-22 22:09:55 -0400277 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400278 } else if flow.TableId == 1 && outPortNo != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000279 logger.Debugw(ctx, "decomposing-olt-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
npujar1d86a522019-11-14 17:11:16 +0530280 fa := &fu.FlowArgs{
281 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Manikkaraj kb1a10922019-07-29 12:10:34 -0400282 MatchFields: []*ofp.OfpOxmOfbField{
283 fu.InPort(egressHop.Ingress),
284 fu.TunnelId(uint64(inPortNo)),
285 },
khenaidoo89b0e942018-10-21 21:11:33 -0400286 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400287 // Augment the matchfields with the ofpfields from the flow
288 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidoo89b0e942018-10-21 21:11:33 -0400289
Akash Reddy Kankanala929cc002025-04-08 15:05:21 +0530290 // Augment the actions
Manikkaraj kb1a10922019-07-29 12:10:34 -0400291 filteredAction := fu.GetActions(flow, fu.OUTPUT)
292 filteredAction = append(filteredAction, fu.Output(egressHop.Egress))
293 fa.Actions = filteredAction
khenaidood20a5852018-10-22 22:09:55 -0400294
Manikkaraj kb1a10922019-07-29 12:10:34 -0400295 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800296 fs, err := fu.MkFlowStat(fa)
297 if err != nil {
298 return nil, err
299 }
300 fg.AddFlow(fs)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400301 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400302 }
Scott Bakerfdea1e32020-02-21 15:35:41 -0800303 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400304}
305
khenaidood20a5852018-10-22 22:09:55 -0400306// processDownstreamFlowWithNextTable decomposes downstream flows containing next table ID instructions
Kent Hagerman6031aad2020-07-29 16:36:33 -0400307func (fd *FlowDecomposer) processDownstreamFlowWithNextTable(ctx context.Context, agent LogicalDeviceAgent, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800308 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000309 logger.Debugw(ctx, "decomposing-olt-flow-in-downstream-flow-with-next-table", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400310 deviceRules := fu.NewDeviceRules()
npujar1d86a522019-11-14 17:11:16 +0530311 meterID := fu.GetMeterIdFromFlow(flow)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000312 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
khenaidood20a5852018-10-22 22:09:55 -0400313
khenaidoo89b0e942018-10-21 21:11:33 -0400314 if outPortNo != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000315 logger.Warnw(ctx, "outPort-should-not-be-specified", log.Fields{"outPortNo": outPortNo})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800316 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400317 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400318
khenaidoo820197c2020-02-13 16:35:33 -0500319 ingressHop := path[0]
320 egressHop := path[1]
Manikkaraj kb1a10922019-07-29 12:10:34 -0400321 if metadataFromwriteMetadata != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000322 logger.Debugw(ctx, "creating-metadata-flow", log.Fields{"flow": flow})
323 portNumber := fu.GetEgressPortNumberFromWriteMetadata(ctx, flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400324 if portNumber != 0 {
khenaidoo820197c2020-02-13 16:35:33 -0500325 recalculatedRoute, err := agent.GetRoute(ctx, inPortNo, portNumber)
326 if err != nil {
Girish Gowdra63a90a52022-02-25 16:51:13 -0800327 logger.Errorw(ctx, "no-route", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo, "metadata": metadataFromwriteMetadata, "error": err})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800328 return deviceRules, nil
khenaidoo820197c2020-02-13 16:35:33 -0500329 }
khenaidoo89b0e942018-10-21 21:11:33 -0400330 switch len(recalculatedRoute) {
331 case 0:
Girish Gowdra63a90a52022-02-25 16:51:13 -0800332 logger.Errorw(ctx, "no-route", log.Fields{"inPortNo": inPortNo, "outPortNo": portNumber, "comment": "deleting-flow", "metadata": metadataFromwriteMetadata})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400333 //TODO: Delete flow
Scott Bakerfdea1e32020-02-21 15:35:41 -0800334 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400335 case 2:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000336 logger.Debugw(ctx, "route-found", log.Fields{"ingressHop": ingressHop, "egressHop": egressHop})
khenaidoo89b0e942018-10-21 21:11:33 -0400337 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000338 logger.Errorw(ctx, "invalid-route-length", log.Fields{"routeLen": len(path)})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800339 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400340 }
341 ingressHop = recalculatedRoute[0]
342 }
Girish Gowdra63a90a52022-02-25 16:51:13 -0800343
npujar1d86a522019-11-14 17:11:16 +0530344 fa := &fu.FlowArgs{
345 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400346 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400347 fu.InPort(ingressHop.Ingress),
khenaidoo68c930b2019-05-13 11:46:51 -0400348 fu.TunnelId(uint64(portNumber)),
khenaidoo89b0e942018-10-21 21:11:33 -0400349 },
khenaidoo68c930b2019-05-13 11:46:51 -0400350 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400351 }
Girish Gowdra63a90a52022-02-25 16:51:13 -0800352
353 // Augment the metadata with innerTag if it is valid
354 innerTag := fu.GetInnerTagFromMetaData(ctx, flow)
355 if innerTag != 0 {
356 fa.MatchFields = append(fa.MatchFields, []*ofp.OfpOxmOfbField{fu.Metadata_ofp(uint64(innerTag))}...)
357 }
khenaidoo89b0e942018-10-21 21:11:33 -0400358 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400359 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT, fu.METADATA)...)
khenaidood20a5852018-10-22 22:09:55 -0400360
361 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400362 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400363
khenaidoo89b0e942018-10-21 21:11:33 -0400364 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800365 fs, err := fu.MkFlowStat(fa)
366 if err != nil {
367 return nil, err
368 }
369 fg.AddFlow(fs)
khenaidoo89b0e942018-10-21 21:11:33 -0400370 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
371 } else { // Create standard flow
Rohan Agrawal31f21802020-06-12 05:38:46 +0000372 logger.Debugw(ctx, "creating-standard-flow", log.Fields{"flow": flow})
npujar1d86a522019-11-14 17:11:16 +0530373 fa := &fu.FlowArgs{
374 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400375 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400376 fu.InPort(ingressHop.Ingress),
377 fu.TunnelId(uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400378 },
khenaidoo68c930b2019-05-13 11:46:51 -0400379 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400380 }
381 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400382 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400383
384 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400385 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400386
khenaidoo89b0e942018-10-21 21:11:33 -0400387 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800388 fs, err := fu.MkFlowStat(fa)
389 if err != nil {
390 return nil, err
391 }
392 fg.AddFlow(fs)
khenaidoo89b0e942018-10-21 21:11:33 -0400393 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
394 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400395
Scott Bakerfdea1e32020-02-21 15:35:41 -0800396 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400397}
398
khenaidood20a5852018-10-22 22:09:55 -0400399// processUnicastFlow decomposes unicast flows
khenaidoo820197c2020-02-13 16:35:33 -0500400func (fd *FlowDecomposer) processUnicastFlow(ctx context.Context, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800401 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400402
Rohan Agrawal31f21802020-06-12 05:38:46 +0000403 logger.Debugw(ctx, "decomposing-onu-flow-in-downstream-unicast-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400404 deviceRules := fu.NewDeviceRules()
405
khenaidoo820197c2020-02-13 16:35:33 -0500406 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400407
npujar1d86a522019-11-14 17:11:16 +0530408 meterID := fu.GetMeterIdFromFlow(flow)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000409 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
npujar1d86a522019-11-14 17:11:16 +0530410 fa := &fu.FlowArgs{
411 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Manikkaraj kb1a10922019-07-29 12:10:34 -0400412 MatchFields: []*ofp.OfpOxmOfbField{
413 fu.InPort(egressHop.Ingress),
414 },
khenaidoo89b0e942018-10-21 21:11:33 -0400415 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400416 // Augment the matchfields with the ofpfields from the flow
417 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400418
Manikkaraj kb1a10922019-07-29 12:10:34 -0400419 // Augment the Actions
420 filteredAction := fu.GetActions(flow, fu.OUTPUT)
421 filteredAction = append(filteredAction, fu.Output(egressHop.Egress))
422 fa.Actions = filteredAction
khenaidoo89b0e942018-10-21 21:11:33 -0400423
Manikkaraj kb1a10922019-07-29 12:10:34 -0400424 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800425 fs, err := fu.MkFlowStat(fa)
426 if err != nil {
427 return nil, err
428 }
429 fg.AddFlow(fs)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400430 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800431 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400432}
433
khenaidood20a5852018-10-22 22:09:55 -0400434// processMulticastFlow decompose multicast flows
khenaidoo820197c2020-02-13 16:35:33 -0500435func (fd *FlowDecomposer) processMulticastFlow(ctx context.Context, path []route.Hop,
npujar1d86a522019-11-14 17:11:16 +0530436 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats, grpID uint32,
khenaidood20a5852018-10-22 22:09:55 -0400437 groupMap map[uint32]*ofp.OfpGroupEntry) *fu.DeviceRules {
438
Rohan Agrawal31f21802020-06-12 05:38:46 +0000439 logger.Debugw(ctx, "multicast-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400440 deviceRules := fu.NewDeviceRules()
khenaidoo89b0e942018-10-21 21:11:33 -0400441
Akash Reddy Kankanala929cc002025-04-08 15:05:21 +0530442 // having no Group yet is the same as having a Group with no buckets
khenaidoo89b0e942018-10-21 21:11:33 -0400443 var grp *ofp.OfpGroupEntry
444 var ok bool
npujar1d86a522019-11-14 17:11:16 +0530445 if grp, ok = groupMap[grpID]; !ok {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000446 logger.Warnw(ctx, "Group-id-not-present-in-map", log.Fields{"grpId": grpID, "groupMap": groupMap})
khenaidoo89b0e942018-10-21 21:11:33 -0400447 return deviceRules
448 }
449 if grp == nil || grp.Desc == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000450 logger.Warnw(ctx, "Group-or-desc-nil", log.Fields{"grpId": grpID, "grp": grp})
khenaidoo89b0e942018-10-21 21:11:33 -0400451 return deviceRules
452 }
khenaidoo89b0e942018-10-21 21:11:33 -0400453
khenaidoo820197c2020-02-13 16:35:33 -0500454 deviceRules.CreateEntryIfNotExist(path[0].DeviceID)
Esin Karaman09959ae2019-11-29 13:59:58 +0000455 fg := fu.NewFlowsAndGroups()
456 fg.AddFlow(flow)
Akash Reddy Kankanala929cc002025-04-08 15:05:21 +0530457 // return the multicast flow without decomposing it
khenaidoo820197c2020-02-13 16:35:33 -0500458 deviceRules.AddFlowsAndGroup(path[0].DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400459 return deviceRules
460}
461
khenaidood20a5852018-10-22 22:09:55 -0400462// decomposeFlow decomposes a flow for a logical device into flows for each physical device
Kent Hagerman6031aad2020-07-29 16:36:33 -0400463func (fd *FlowDecomposer) decomposeFlow(ctx context.Context, agent LogicalDeviceAgent, flow *ofp.OfpFlowStats,
khenaidoo820197c2020-02-13 16:35:33 -0500464 groupMap map[uint32]*ofp.OfpGroupEntry) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400465
khenaidoo68c930b2019-05-13 11:46:51 -0400466 inPortNo := fu.GetInPort(flow)
Esin Karaman09959ae2019-11-29 13:59:58 +0000467 if fu.HasGroup(flow) && inPortNo == 0 {
Akash Reddy Kankanala929cc002025-04-08 15:05:21 +0530468 // if no in-port specified for a multicast flow, put NNI port as in-port
469 // so that a valid path can be found for the flow
Esin Karaman09959ae2019-11-29 13:59:58 +0000470 nniPorts := agent.GetNNIPorts()
471 if len(nniPorts) > 0 {
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400472 for port := range nniPorts {
473 inPortNo = port
474 break
475 }
Rohan Agrawal31f21802020-06-12 05:38:46 +0000476 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 +0000477 }
478 }
khenaidoo68c930b2019-05-13 11:46:51 -0400479 outPortNo := fu.GetOutPort(flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400480 deviceRules := fu.NewDeviceRules()
khenaidoo820197c2020-02-13 16:35:33 -0500481 path, err := agent.GetRoute(ctx, inPortNo, outPortNo)
482 if err != nil {
khenaidoo820197c2020-02-13 16:35:33 -0500483 return deviceRules, err
484 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400485
khenaidoo820197c2020-02-13 16:35:33 -0500486 switch len(path) {
khenaidoo89b0e942018-10-21 21:11:33 -0400487 case 0:
khenaidoo787224a2020-04-16 18:08:47 -0400488 return deviceRules, fmt.Errorf("no route from:%d to:%d :%w", inPortNo, outPortNo, route.ErrNoRoute)
khenaidoo89b0e942018-10-21 21:11:33 -0400489 case 2:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000490 logger.Debugw(ctx, "route-found", log.Fields{"ingressHop": path[0], "egressHop": path[1]})
khenaidoo89b0e942018-10-21 21:11:33 -0400491 default:
khenaidoo787224a2020-04-16 18:08:47 -0400492 return deviceRules, fmt.Errorf("invalid route length %d :%w", len(path), route.ErrNoRoute)
khenaidoo89b0e942018-10-21 21:11:33 -0400493 }
494
khenaidoo89b0e942018-10-21 21:11:33 -0400495 // Process controller bound flow
496 if outPortNo != 0 && (outPortNo&0x7fffffff) == uint32(ofp.OfpPortNo_OFPP_CONTROLLER) {
Scott Bakerfdea1e32020-02-21 15:35:41 -0800497 deviceRules, err = fd.processControllerBoundFlow(ctx, agent, path, inPortNo, outPortNo, flow)
498 if err != nil {
499 return nil, err
500 }
khenaidoo89b0e942018-10-21 21:11:33 -0400501 } else {
khenaidoo297cd252019-02-07 22:10:23 -0500502 var ingressDevice *voltha.Device
Kent Hagerman6031aad2020-07-29 16:36:33 -0400503 if ingressDevice, err = fd.getDevice(ctx, path[0].DeviceID); err != nil {
khenaidoo787224a2020-04-16 18:08:47 -0400504 // This can happen in a race condition where a device is deleted right after we obtain a
505 // route involving the device (GetRoute() above). Handle it as a no route event as well.
506 return deviceRules, fmt.Errorf("get-device-error :%v :%w", err, route.ErrNoRoute)
khenaidoo297cd252019-02-07 22:10:23 -0500507 }
508 isUpstream := !ingressDevice.Root
Manikkaraj kb1a10922019-07-29 12:10:34 -0400509 if isUpstream { // Unicast OLT and ONU UL
ssiddiqui21e54c32021-07-27 11:30:46 +0530510 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 -0800511 deviceRules, err = fd.processUpstreamNonControllerBoundFlow(ctx, path, inPortNo, outPortNo, flow)
512 if err != nil {
513 return nil, err
514 }
ssiddiqui21e54c32021-07-27 11:30:46 +0530515 } else if fu.HasNextTable(flow) && (flow.GetTableId() == 0 || flow.GetTableId() == 1) { // Unicast OLT Flow
516 // For 'Non-MPLS' flows, this condition will only be true for table-id 0 as only table-id 0 will have the
517 // 'go-to-next-table' instruction
518 // For 'MPLS' flows, this condition will be true for table-id 0 and table-id 1.
519 // So the flow here shall always be an OLT flow
520 logger.Debugw(ctx, "process-olt-downstream-non-controller-bound-flow-with-next-table", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800521 deviceRules, err = fd.processDownstreamFlowWithNextTable(ctx, agent, path, inPortNo, outPortNo, flow)
522 if err != nil {
523 return nil, err
524 }
ssiddiqui21e54c32021-07-27 11:30:46 +0530525 } else if (flow.GetTableId() == 1 || flow.GetTableId() == 2) && outPortNo != 0 { // Unicast ONU flow DL
526 // If this is an MPLS OLT flow (table-id 1, transition-to-table 2), the condition above will already be hit.
527 // So if we are reaching this point, the flow shall always be an ONU flow
528 logger.Debugw(ctx, "process-onu-downstream-uni-cast-flow", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800529 deviceRules, err = fd.processUnicastFlow(ctx, path, inPortNo, outPortNo, flow)
530 if err != nil {
531 return nil, err
532 }
Akash Reddy Kankanala929cc002025-04-08 15:05:21 +0530533 } else if grpID := fu.GetGroup(flow); grpID != 0 && flow.TableId == 0 { // Multicast
Rohan Agrawal31f21802020-06-12 05:38:46 +0000534 logger.Debugw(ctx, "process-multicast-flow", log.Fields{"flows": flow})
khenaidoo820197c2020-02-13 16:35:33 -0500535 deviceRules = fd.processMulticastFlow(ctx, path, inPortNo, outPortNo, flow, grpID, groupMap)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400536 } else {
khenaidoo820197c2020-02-13 16:35:33 -0500537 return deviceRules, status.Errorf(codes.Aborted, "unknown downstream flow %v", *flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400538 }
539 }
Elia Battiston509fdc72022-01-04 13:28:09 +0100540 deviceRules, err = fd.updateOutputPortForControllerBoundFlowForParentDevice(ctx, deviceRules)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800541 return deviceRules, err
khenaidoo89b0e942018-10-21 21:11:33 -0400542}