blob: 2b4f1941c75224ff9670d3aaae87b3e9d046ec03 [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"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080025 fu "github.com/opencord/voltha-lib-go/v3/pkg/flows"
26 "github.com/opencord/voltha-lib-go/v3/pkg/log"
27 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
28 "github.com/opencord/voltha-protos/v3/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
Kent Hagerman6031aad2020-07-29 16:36:33 -040073func (fd *FlowDecomposer) updateOutputPortForControllerBoundFlowForParentDevide(ctx context.Context, dr *fu.DeviceRules) (*fu.DeviceRules, error) {
khenaidoo68c930b2019-05-13 11:46:51 -040074 EAPOL := fu.EthType(0x888e)
75 IGMP := fu.IpProto(2)
76 UDP := fu.IpProto(17)
khenaidoo19d7b632018-10-30 10:49:50 -040077
78 newDeviceRules := dr.Copy()
79 // Check whether we are dealing with a parent device
npujar1d86a522019-11-14 17:11:16 +053080 for deviceID, fg := range dr.GetRules() {
Kent Hagerman6031aad2020-07-29 16:36:33 -040081 if device, err := fd.getDevice(ctx, deviceID); err == nil && device.Root {
npujar1d86a522019-11-14 17:11:16 +053082 newDeviceRules.ClearFlows(deviceID)
khenaidoo19d7b632018-10-30 10:49:50 -040083 for i := 0; i < fg.Flows.Len(); i++ {
84 f := fg.GetFlow(i)
85 UpdateOutPortNo := false
khenaidoo68c930b2019-05-13 11:46:51 -040086 for _, field := range fu.GetOfbFields(f) {
khenaidoo19d7b632018-10-30 10:49:50 -040087 UpdateOutPortNo = (field.String() == EAPOL.String())
88 UpdateOutPortNo = UpdateOutPortNo || (field.String() == IGMP.String())
89 UpdateOutPortNo = UpdateOutPortNo || (field.String() == UDP.String())
90 if UpdateOutPortNo {
91 break
92 }
93 }
94 if UpdateOutPortNo {
khenaidoo68c930b2019-05-13 11:46:51 -040095 f = fu.UpdateOutputPortByActionType(f, uint32(ofp.OfpInstructionType_OFPIT_APPLY_ACTIONS),
khenaidoo19d7b632018-10-30 10:49:50 -040096 uint32(ofp.OfpPortNo_OFPP_CONTROLLER))
97 }
98 // Update flow Id as a change in the instruction field will result in a new flow ID
Mahir Gunyeladdb66a2020-04-29 18:08:50 -070099 //var err error
100 //if f.Id, err = fu.HashFlowStats(f); err != nil {
101 //return nil, err
102 //}
npujar1d86a522019-11-14 17:11:16 +0530103 newDeviceRules.AddFlow(deviceID, (proto.Clone(f)).(*ofp.OfpFlowStats))
khenaidoo19d7b632018-10-30 10:49:50 -0400104 }
105 }
106 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400107
Scott Bakerfdea1e32020-02-21 15:35:41 -0800108 return newDeviceRules, nil
khenaidoo19d7b632018-10-30 10:49:50 -0400109}
110
khenaidood20a5852018-10-22 22:09:55 -0400111//processControllerBoundFlow decomposes trap flows
Kent Hagerman6031aad2020-07-29 16:36:33 -0400112func (fd *FlowDecomposer) processControllerBoundFlow(ctx context.Context, agent LogicalDeviceAgent, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800113 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400114
Rohan Agrawal31f21802020-06-12 05:38:46 +0000115 logger.Debugw(ctx, "trap-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo, "flow": flow})
khenaidood20a5852018-10-22 22:09:55 -0400116 deviceRules := fu.NewDeviceRules()
npujar1d86a522019-11-14 17:11:16 +0530117 meterID := fu.GetMeterIdFromFlow(flow)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000118 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
khenaidood20a5852018-10-22 22:09:55 -0400119
khenaidoo820197c2020-02-13 16:35:33 -0500120 ingressHop := path[0]
121 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400122
Humera Kouser4ff89012019-08-25 19:01:51 -0400123 //case of packet_in from NNI port rule
khenaidoo820197c2020-02-13 16:35:33 -0500124 if agent.GetDeviceRoutes().IsRootPort(inPortNo) {
Humera Kouser4ff89012019-08-25 19:01:51 -0400125 // Trap flow for NNI port
Rohan Agrawal31f21802020-06-12 05:38:46 +0000126 logger.Debug(ctx, "trap-nni")
Humera Kouser4ff89012019-08-25 19:01:51 -0400127
npujar1d86a522019-11-14 17:11:16 +0530128 fa := &fu.FlowArgs{
Humera Kouser4ff89012019-08-25 19:01:51 -0400129 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie},
130 MatchFields: []*ofp.OfpOxmOfbField{
131 fu.InPort(egressHop.Egress),
132 },
133 Actions: fu.GetActions(flow),
134 }
135 // Augment the matchfields with the ofpfields from the flow
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400136 fg := fu.NewFlowsAndGroups()
Humera Kouser4ff89012019-08-25 19:01:51 -0400137 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800138 fs, err := fu.MkFlowStat(fa)
139 if err != nil {
140 return nil, err
141 }
142 fg.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400143 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400144 } else {
145 // Trap flow for UNI port
Rohan Agrawal31f21802020-06-12 05:38:46 +0000146 logger.Debug(ctx, "trap-uni")
khenaidoo89b0e942018-10-21 21:11:33 -0400147
Matt Jeannerete75f2842020-03-14 15:45:12 -0400148 //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 -0400149 var inPorts = map[uint32]struct{}{inPortNo: {}}
khenaidoo89b0e942018-10-21 21:11:33 -0400150 if inPortNo == 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000151 inPorts = agent.GetWildcardInputPorts(ctx, egressHop.Egress) // exclude egress_hop.egress_port.port_no
khenaidoo89b0e942018-10-21 21:11:33 -0400152 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400153 for inputPort := range inPorts {
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400154 // Upstream flow on parent (olt) device
npujar1d86a522019-11-14 17:11:16 +0530155 faParent := &fu.FlowArgs{
156 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400157 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400158 fu.InPort(egressHop.Ingress),
khenaidoo68c930b2019-05-13 11:46:51 -0400159 fu.TunnelId(uint64(inputPort)),
khenaidoo89b0e942018-10-21 21:11:33 -0400160 },
161 Actions: []*ofp.OfpAction{
khenaidoo68c930b2019-05-13 11:46:51 -0400162 fu.Output(egressHop.Egress),
khenaidoo89b0e942018-10-21 21:11:33 -0400163 },
164 }
165 // Augment the matchfields with the ofpfields from the flow
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400166 faParent.MatchFields = append(faParent.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
167 fgParent := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800168 fs, err := fu.MkFlowStat(faParent)
169 if err != nil {
170 return nil, err
171 }
172 fgParent.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400173 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fgParent)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000174 logger.Debugw(ctx, "parent-trap-flow-set", log.Fields{"flow": faParent})
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400175
176 // Upstream flow on child (onu) device
177 var actions []*ofp.OfpAction
178 setvid := fu.GetVlanVid(flow)
179 if setvid != nil {
180 // have this child push the vlan the parent is matching/trapping on above
181 actions = []*ofp.OfpAction{
182 fu.PushVlan(0x8100),
183 fu.SetField(fu.VlanVid(*setvid)),
184 fu.Output(ingressHop.Egress),
185 }
186 } else {
187 // otherwise just set the egress port
188 actions = []*ofp.OfpAction{
189 fu.Output(ingressHop.Egress),
190 }
191 }
npujar1d86a522019-11-14 17:11:16 +0530192 faChild := &fu.FlowArgs{
193 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400194 MatchFields: []*ofp.OfpOxmOfbField{
195 fu.InPort(ingressHop.Ingress),
196 fu.TunnelId(uint64(inputPort)),
197 },
198 Actions: actions,
199 }
200 // Augment the matchfields with the ofpfields from the flow.
201 // If the parent has a match vid and the child is setting that match vid exclude the the match vlan
202 // for the child given it will be setting that vlan and the parent will be matching on it
203 if setvid != nil {
204 faChild.MatchFields = append(faChild.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT, fu.VLAN_VID)...)
205 } else {
206 faChild.MatchFields = append(faChild.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
207 }
208 fgChild := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800209 fs, err = fu.MkFlowStat(faChild)
210 if err != nil {
211 return nil, err
212 }
213 fgChild.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400214 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fgChild)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000215 logger.Debugw(ctx, "child-trap-flow-set", log.Fields{"flow": faChild})
khenaidoo89b0e942018-10-21 21:11:33 -0400216 }
217 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400218
Scott Bakerfdea1e32020-02-21 15:35:41 -0800219 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400220}
221
222// processUpstreamNonControllerBoundFlow processes non-controller bound flow. We assume that anything that is
223// upstream needs to get Q-in-Q treatment and that this is expressed via two flow rules, the first using the
224// goto-statement. We also assume that the inner tag is applied at the ONU, while the outer tag is
225// applied at the OLT
khenaidoo820197c2020-02-13 16:35:33 -0500226func (fd *FlowDecomposer) processUpstreamNonControllerBoundFlow(ctx context.Context,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800227 path []route.Hop, inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400228
Rohan Agrawal31f21802020-06-12 05:38:46 +0000229 logger.Debugw(ctx, "upstream-non-controller-bound-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400230 deviceRules := fu.NewDeviceRules()
231
npujar1d86a522019-11-14 17:11:16 +0530232 meterID := fu.GetMeterIdFromFlow(flow)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000233 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400234
khenaidoo820197c2020-02-13 16:35:33 -0500235 ingressHop := path[0]
236 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400237
Manikkaraj kb1a10922019-07-29 12:10:34 -0400238 if flow.TableId == 0 && fu.HasNextTable(flow) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000239 logger.Debugw(ctx, "decomposing-onu-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
khenaidoo89b0e942018-10-21 21:11:33 -0400240 if outPortNo != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000241 logger.Warnw(ctx, "outPort-should-not-be-specified", log.Fields{"outPortNo": outPortNo})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800242 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400243 }
npujar1d86a522019-11-14 17:11:16 +0530244 fa := &fu.FlowArgs{
245 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400246 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400247 fu.InPort(ingressHop.Ingress),
248 fu.TunnelId(uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400249 },
khenaidoo68c930b2019-05-13 11:46:51 -0400250 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400251 }
252 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400253 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400254
255 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400256 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400257
khenaidoo89b0e942018-10-21 21:11:33 -0400258 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800259 fs, err := fu.MkFlowStat(fa)
260 if err != nil {
261 return nil, err
262 }
263 fg.AddFlow(fs)
khenaidood20a5852018-10-22 22:09:55 -0400264 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400265 } else if flow.TableId == 1 && outPortNo != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000266 logger.Debugw(ctx, "decomposing-olt-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
npujar1d86a522019-11-14 17:11:16 +0530267 fa := &fu.FlowArgs{
268 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Manikkaraj kb1a10922019-07-29 12:10:34 -0400269 MatchFields: []*ofp.OfpOxmOfbField{
270 fu.InPort(egressHop.Ingress),
271 fu.TunnelId(uint64(inPortNo)),
272 },
khenaidoo89b0e942018-10-21 21:11:33 -0400273 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400274 // Augment the matchfields with the ofpfields from the flow
275 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidoo89b0e942018-10-21 21:11:33 -0400276
Manikkaraj kb1a10922019-07-29 12:10:34 -0400277 //Augment the actions
278 filteredAction := fu.GetActions(flow, fu.OUTPUT)
279 filteredAction = append(filteredAction, fu.Output(egressHop.Egress))
280 fa.Actions = filteredAction
khenaidood20a5852018-10-22 22:09:55 -0400281
Manikkaraj kb1a10922019-07-29 12:10:34 -0400282 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800283 fs, err := fu.MkFlowStat(fa)
284 if err != nil {
285 return nil, err
286 }
287 fg.AddFlow(fs)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400288 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400289 }
Scott Bakerfdea1e32020-02-21 15:35:41 -0800290 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400291}
292
khenaidood20a5852018-10-22 22:09:55 -0400293// processDownstreamFlowWithNextTable decomposes downstream flows containing next table ID instructions
Kent Hagerman6031aad2020-07-29 16:36:33 -0400294func (fd *FlowDecomposer) processDownstreamFlowWithNextTable(ctx context.Context, agent LogicalDeviceAgent, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800295 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000296 logger.Debugw(ctx, "decomposing-olt-flow-in-downstream-flow-with-next-table", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400297 deviceRules := fu.NewDeviceRules()
npujar1d86a522019-11-14 17:11:16 +0530298 meterID := fu.GetMeterIdFromFlow(flow)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000299 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
khenaidood20a5852018-10-22 22:09:55 -0400300
khenaidoo89b0e942018-10-21 21:11:33 -0400301 if outPortNo != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000302 logger.Warnw(ctx, "outPort-should-not-be-specified", log.Fields{"outPortNo": outPortNo})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800303 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400304 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400305
306 if flow.TableId != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000307 logger.Warnw(ctx, "This is not olt pipeline table, so skipping", log.Fields{"tableId": flow.TableId})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800308 return deviceRules, nil
Manikkaraj kb1a10922019-07-29 12:10:34 -0400309 }
310
khenaidoo820197c2020-02-13 16:35:33 -0500311 ingressHop := path[0]
312 egressHop := path[1]
Manikkaraj kb1a10922019-07-29 12:10:34 -0400313 if metadataFromwriteMetadata != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000314 logger.Debugw(ctx, "creating-metadata-flow", log.Fields{"flow": flow})
315 portNumber := fu.GetEgressPortNumberFromWriteMetadata(ctx, flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400316 if portNumber != 0 {
khenaidoo820197c2020-02-13 16:35:33 -0500317 recalculatedRoute, err := agent.GetRoute(ctx, inPortNo, portNumber)
318 if err != nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000319 logger.Errorw(ctx, "no-route-double-tag", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo, "metadata": metadataFromwriteMetadata, "error": err})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800320 return deviceRules, nil
khenaidoo820197c2020-02-13 16:35:33 -0500321 }
khenaidoo89b0e942018-10-21 21:11:33 -0400322 switch len(recalculatedRoute) {
323 case 0:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000324 logger.Errorw(ctx, "no-route-double-tag", log.Fields{"inPortNo": inPortNo, "outPortNo": portNumber, "comment": "deleting-flow", "metadata": metadataFromwriteMetadata})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400325 //TODO: Delete flow
Scott Bakerfdea1e32020-02-21 15:35:41 -0800326 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400327 case 2:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000328 logger.Debugw(ctx, "route-found", log.Fields{"ingressHop": ingressHop, "egressHop": egressHop})
khenaidoo89b0e942018-10-21 21:11:33 -0400329 default:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000330 logger.Errorw(ctx, "invalid-route-length", log.Fields{"routeLen": len(path)})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800331 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400332 }
333 ingressHop = recalculatedRoute[0]
334 }
Rohan Agrawal31f21802020-06-12 05:38:46 +0000335 innerTag := fu.GetInnerTagFromMetaData(ctx, flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400336 if innerTag == 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000337 logger.Errorw(ctx, "no-inner-route-double-tag", log.Fields{"inPortNo": inPortNo, "outPortNo": portNumber, "comment": "deleting-flow", "metadata": metadataFromwriteMetadata})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400338 //TODO: Delete flow
Scott Bakerfdea1e32020-02-21 15:35:41 -0800339 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400340 }
npujar1d86a522019-11-14 17:11:16 +0530341 fa := &fu.FlowArgs{
342 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400343 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400344 fu.InPort(ingressHop.Ingress),
Manikkaraj kb1a10922019-07-29 12:10:34 -0400345 fu.Metadata_ofp(uint64(innerTag)),
khenaidoo68c930b2019-05-13 11:46:51 -0400346 fu.TunnelId(uint64(portNumber)),
khenaidoo89b0e942018-10-21 21:11:33 -0400347 },
khenaidoo68c930b2019-05-13 11:46:51 -0400348 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400349 }
350 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400351 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT, fu.METADATA)...)
khenaidood20a5852018-10-22 22:09:55 -0400352
353 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400354 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400355
khenaidoo89b0e942018-10-21 21:11:33 -0400356 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800357 fs, err := fu.MkFlowStat(fa)
358 if err != nil {
359 return nil, err
360 }
361 fg.AddFlow(fs)
khenaidoo89b0e942018-10-21 21:11:33 -0400362 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
363 } else { // Create standard flow
Rohan Agrawal31f21802020-06-12 05:38:46 +0000364 logger.Debugw(ctx, "creating-standard-flow", log.Fields{"flow": flow})
npujar1d86a522019-11-14 17:11:16 +0530365 fa := &fu.FlowArgs{
366 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400367 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400368 fu.InPort(ingressHop.Ingress),
369 fu.TunnelId(uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400370 },
khenaidoo68c930b2019-05-13 11:46:51 -0400371 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400372 }
373 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400374 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400375
376 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400377 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400378
khenaidoo89b0e942018-10-21 21:11:33 -0400379 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800380 fs, err := fu.MkFlowStat(fa)
381 if err != nil {
382 return nil, err
383 }
384 fg.AddFlow(fs)
khenaidoo89b0e942018-10-21 21:11:33 -0400385 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
386 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400387
Scott Bakerfdea1e32020-02-21 15:35:41 -0800388 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400389}
390
khenaidood20a5852018-10-22 22:09:55 -0400391// processUnicastFlow decomposes unicast flows
khenaidoo820197c2020-02-13 16:35:33 -0500392func (fd *FlowDecomposer) processUnicastFlow(ctx context.Context, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800393 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400394
Rohan Agrawal31f21802020-06-12 05:38:46 +0000395 logger.Debugw(ctx, "decomposing-onu-flow-in-downstream-unicast-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400396 deviceRules := fu.NewDeviceRules()
397
khenaidoo820197c2020-02-13 16:35:33 -0500398 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400399
npujar1d86a522019-11-14 17:11:16 +0530400 meterID := fu.GetMeterIdFromFlow(flow)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000401 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
npujar1d86a522019-11-14 17:11:16 +0530402 fa := &fu.FlowArgs{
403 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Manikkaraj kb1a10922019-07-29 12:10:34 -0400404 MatchFields: []*ofp.OfpOxmOfbField{
405 fu.InPort(egressHop.Ingress),
406 },
khenaidoo89b0e942018-10-21 21:11:33 -0400407 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400408 // Augment the matchfields with the ofpfields from the flow
409 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400410
Manikkaraj kb1a10922019-07-29 12:10:34 -0400411 // Augment the Actions
412 filteredAction := fu.GetActions(flow, fu.OUTPUT)
413 filteredAction = append(filteredAction, fu.Output(egressHop.Egress))
414 fa.Actions = filteredAction
khenaidoo89b0e942018-10-21 21:11:33 -0400415
Manikkaraj kb1a10922019-07-29 12:10:34 -0400416 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800417 fs, err := fu.MkFlowStat(fa)
418 if err != nil {
419 return nil, err
420 }
421 fg.AddFlow(fs)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400422 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800423 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400424}
425
khenaidood20a5852018-10-22 22:09:55 -0400426// processMulticastFlow decompose multicast flows
khenaidoo820197c2020-02-13 16:35:33 -0500427func (fd *FlowDecomposer) processMulticastFlow(ctx context.Context, path []route.Hop,
npujar1d86a522019-11-14 17:11:16 +0530428 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats, grpID uint32,
khenaidood20a5852018-10-22 22:09:55 -0400429 groupMap map[uint32]*ofp.OfpGroupEntry) *fu.DeviceRules {
430
Rohan Agrawal31f21802020-06-12 05:38:46 +0000431 logger.Debugw(ctx, "multicast-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400432 deviceRules := fu.NewDeviceRules()
khenaidoo89b0e942018-10-21 21:11:33 -0400433
434 //having no Group yet is the same as having a Group with no buckets
435 var grp *ofp.OfpGroupEntry
436 var ok bool
npujar1d86a522019-11-14 17:11:16 +0530437 if grp, ok = groupMap[grpID]; !ok {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000438 logger.Warnw(ctx, "Group-id-not-present-in-map", log.Fields{"grpId": grpID, "groupMap": groupMap})
khenaidoo89b0e942018-10-21 21:11:33 -0400439 return deviceRules
440 }
441 if grp == nil || grp.Desc == nil {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000442 logger.Warnw(ctx, "Group-or-desc-nil", log.Fields{"grpId": grpID, "grp": grp})
khenaidoo89b0e942018-10-21 21:11:33 -0400443 return deviceRules
444 }
khenaidoo89b0e942018-10-21 21:11:33 -0400445
khenaidoo820197c2020-02-13 16:35:33 -0500446 deviceRules.CreateEntryIfNotExist(path[0].DeviceID)
Esin Karaman09959ae2019-11-29 13:59:58 +0000447 fg := fu.NewFlowsAndGroups()
448 fg.AddFlow(flow)
449 //return the multicast flow without decomposing it
khenaidoo820197c2020-02-13 16:35:33 -0500450 deviceRules.AddFlowsAndGroup(path[0].DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400451 return deviceRules
452}
453
khenaidood20a5852018-10-22 22:09:55 -0400454// decomposeFlow decomposes a flow for a logical device into flows for each physical device
Kent Hagerman6031aad2020-07-29 16:36:33 -0400455func (fd *FlowDecomposer) decomposeFlow(ctx context.Context, agent LogicalDeviceAgent, flow *ofp.OfpFlowStats,
khenaidoo820197c2020-02-13 16:35:33 -0500456 groupMap map[uint32]*ofp.OfpGroupEntry) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400457
khenaidoo68c930b2019-05-13 11:46:51 -0400458 inPortNo := fu.GetInPort(flow)
Esin Karaman09959ae2019-11-29 13:59:58 +0000459 if fu.HasGroup(flow) && inPortNo == 0 {
460 //if no in-port specified for a multicast flow, put NNI port as in-port
khenaidoo820197c2020-02-13 16:35:33 -0500461 //so that a valid path can be found for the flow
Esin Karaman09959ae2019-11-29 13:59:58 +0000462 nniPorts := agent.GetNNIPorts()
463 if len(nniPorts) > 0 {
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400464 for port := range nniPorts {
465 inPortNo = port
466 break
467 }
Rohan Agrawal31f21802020-06-12 05:38:46 +0000468 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 +0000469 }
470 }
khenaidoo68c930b2019-05-13 11:46:51 -0400471 outPortNo := fu.GetOutPort(flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400472 deviceRules := fu.NewDeviceRules()
khenaidoo820197c2020-02-13 16:35:33 -0500473 path, err := agent.GetRoute(ctx, inPortNo, outPortNo)
474 if err != nil {
khenaidoo820197c2020-02-13 16:35:33 -0500475 return deviceRules, err
476 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400477
khenaidoo820197c2020-02-13 16:35:33 -0500478 switch len(path) {
khenaidoo89b0e942018-10-21 21:11:33 -0400479 case 0:
khenaidoo787224a2020-04-16 18:08:47 -0400480 return deviceRules, fmt.Errorf("no route from:%d to:%d :%w", inPortNo, outPortNo, route.ErrNoRoute)
khenaidoo89b0e942018-10-21 21:11:33 -0400481 case 2:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000482 logger.Debugw(ctx, "route-found", log.Fields{"ingressHop": path[0], "egressHop": path[1]})
khenaidoo89b0e942018-10-21 21:11:33 -0400483 default:
khenaidoo787224a2020-04-16 18:08:47 -0400484 return deviceRules, fmt.Errorf("invalid route length %d :%w", len(path), route.ErrNoRoute)
khenaidoo89b0e942018-10-21 21:11:33 -0400485 }
486
khenaidoo89b0e942018-10-21 21:11:33 -0400487 // Process controller bound flow
488 if outPortNo != 0 && (outPortNo&0x7fffffff) == uint32(ofp.OfpPortNo_OFPP_CONTROLLER) {
Scott Bakerfdea1e32020-02-21 15:35:41 -0800489 deviceRules, err = fd.processControllerBoundFlow(ctx, agent, path, inPortNo, outPortNo, flow)
490 if err != nil {
491 return nil, err
492 }
khenaidoo89b0e942018-10-21 21:11:33 -0400493 } else {
khenaidoo297cd252019-02-07 22:10:23 -0500494 var ingressDevice *voltha.Device
495 var err error
Kent Hagerman6031aad2020-07-29 16:36:33 -0400496 if ingressDevice, err = fd.getDevice(ctx, path[0].DeviceID); err != nil {
khenaidoo787224a2020-04-16 18:08:47 -0400497 // This can happen in a race condition where a device is deleted right after we obtain a
498 // route involving the device (GetRoute() above). Handle it as a no route event as well.
499 return deviceRules, fmt.Errorf("get-device-error :%v :%w", err, route.ErrNoRoute)
khenaidoo297cd252019-02-07 22:10:23 -0500500 }
501 isUpstream := !ingressDevice.Root
Manikkaraj kb1a10922019-07-29 12:10:34 -0400502 if isUpstream { // Unicast OLT and ONU UL
Rohan Agrawal31f21802020-06-12 05:38:46 +0000503 logger.Debug(ctx, "process-olt-nd-onu-upstream-noncontrollerbound-unicast-flows", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800504 deviceRules, err = fd.processUpstreamNonControllerBoundFlow(ctx, path, inPortNo, outPortNo, flow)
505 if err != nil {
506 return nil, err
507 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400508 } else if fu.HasNextTable(flow) && flow.TableId == 0 { // Unicast OLT flow DL
Rohan Agrawal31f21802020-06-12 05:38:46 +0000509 logger.Debugw(ctx, "process-olt-downstream-noncontrollerbound-flow-with-nexttable", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800510 deviceRules, err = fd.processDownstreamFlowWithNextTable(ctx, agent, path, inPortNo, outPortNo, flow)
511 if err != nil {
512 return nil, err
513 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400514 } else if flow.TableId == 1 && outPortNo != 0 { // Unicast ONU flow DL
Rohan Agrawal31f21802020-06-12 05:38:46 +0000515 logger.Debugw(ctx, "process-onu-downstream-unicast-flow", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800516 deviceRules, err = fd.processUnicastFlow(ctx, path, inPortNo, outPortNo, flow)
517 if err != nil {
518 return nil, err
519 }
npujar1d86a522019-11-14 17:11:16 +0530520 } else if grpID := fu.GetGroup(flow); grpID != 0 && flow.TableId == 0 { //Multicast
Rohan Agrawal31f21802020-06-12 05:38:46 +0000521 logger.Debugw(ctx, "process-multicast-flow", log.Fields{"flows": flow})
khenaidoo820197c2020-02-13 16:35:33 -0500522 deviceRules = fd.processMulticastFlow(ctx, path, inPortNo, outPortNo, flow, grpID, groupMap)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400523 } else {
khenaidoo820197c2020-02-13 16:35:33 -0500524 return deviceRules, status.Errorf(codes.Aborted, "unknown downstream flow %v", *flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400525 }
526 }
Kent Hagerman6031aad2020-07-29 16:36:33 -0400527 deviceRules, err = fd.updateOutputPortForControllerBoundFlowForParentDevide(ctx, deviceRules)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800528 return deviceRules, err
khenaidoo89b0e942018-10-21 21:11:33 -0400529}