blob: 986d911a0d6435800fce3b52c82ab4defa6980ae [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")
Girish Gowdra5abbc0c2020-09-16 13:21:10 -0700147 var setVid, setPcp uint32
148 var setVidOk, setPcpOk bool
Matt Jeannerete75f2842020-03-14 15:45:12 -0400149 //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 -0400150 var inPorts = map[uint32]struct{}{inPortNo: {}}
khenaidoo89b0e942018-10-21 21:11:33 -0400151 if inPortNo == 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000152 inPorts = agent.GetWildcardInputPorts(ctx, egressHop.Egress) // exclude egress_hop.egress_port.port_no
khenaidoo89b0e942018-10-21 21:11:33 -0400153 }
Kent Hagermanfa9d6d42020-05-25 11:49:40 -0400154 for inputPort := range inPorts {
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400155 // Upstream flow on parent (olt) device
npujar1d86a522019-11-14 17:11:16 +0530156 faParent := &fu.FlowArgs{
157 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400158 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400159 fu.InPort(egressHop.Ingress),
khenaidoo68c930b2019-05-13 11:46:51 -0400160 fu.TunnelId(uint64(inputPort)),
khenaidoo89b0e942018-10-21 21:11:33 -0400161 },
162 Actions: []*ofp.OfpAction{
khenaidoo68c930b2019-05-13 11:46:51 -0400163 fu.Output(egressHop.Egress),
khenaidoo89b0e942018-10-21 21:11:33 -0400164 },
165 }
Girish Gowdra5abbc0c2020-09-16 13:21:10 -0700166 // Augment the parent device flow matchfields with the ofpfields from the flow
167 faParent.MatchFields = append(faParent.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT, fu.VLAN_VID, fu.VLAN_PCP)...)
168 // Augment the parent device flow matchfields with vlan vid and vlan pcp from action field.
169 // The child device is going to set the vlan and pcp and parent device has to match on them
170 if setVid, setVidOk = fu.GetSetActionField(ctx, flow, fu.VLAN_VID); setVidOk {
171 faParent.MatchFields = append(faParent.MatchFields, fu.VlanVid(setVid))
172 if setPcp, setPcpOk = fu.GetSetActionField(ctx, flow, fu.VLAN_PCP); setPcpOk {
173 faParent.MatchFields = append(faParent.MatchFields, fu.VlanPcp(setPcp))
174 }
175 }
176
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400177 fgParent := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800178 fs, err := fu.MkFlowStat(faParent)
179 if err != nil {
180 return nil, err
181 }
182 fgParent.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400183 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fgParent)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000184 logger.Debugw(ctx, "parent-trap-flow-set", log.Fields{"flow": faParent})
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400185
186 // Upstream flow on child (onu) device
187 var actions []*ofp.OfpAction
Girish Gowdra5abbc0c2020-09-16 13:21:10 -0700188 if setVidOk {
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400189 // have this child push the vlan the parent is matching/trapping on above
190 actions = []*ofp.OfpAction{
191 fu.PushVlan(0x8100),
Girish Gowdra5abbc0c2020-09-16 13:21:10 -0700192 fu.SetField(fu.VlanVid(setVid)),
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400193 fu.Output(ingressHop.Egress),
194 }
Girish Gowdra5abbc0c2020-09-16 13:21:10 -0700195 if setPcpOk {
196 actions = append(actions, fu.SetField(fu.VlanPcp(setPcp)))
197 }
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400198 } else {
199 // otherwise just set the egress port
200 actions = []*ofp.OfpAction{
201 fu.Output(ingressHop.Egress),
202 }
203 }
npujar1d86a522019-11-14 17:11:16 +0530204 faChild := &fu.FlowArgs{
205 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400206 MatchFields: []*ofp.OfpOxmOfbField{
207 fu.InPort(ingressHop.Ingress),
208 fu.TunnelId(uint64(inputPort)),
209 },
210 Actions: actions,
211 }
212 // Augment the matchfields with the ofpfields from the flow.
213 // If the parent has a match vid and the child is setting that match vid exclude the the match vlan
214 // for the child given it will be setting that vlan and the parent will be matching on it
Girish Gowdra5abbc0c2020-09-16 13:21:10 -0700215 faChild.MatchFields = append(faChild.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400216 fgChild := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800217 fs, err = fu.MkFlowStat(faChild)
218 if err != nil {
219 return nil, err
220 }
221 fgChild.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400222 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fgChild)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000223 logger.Debugw(ctx, "child-trap-flow-set", log.Fields{"flow": faChild})
khenaidoo89b0e942018-10-21 21:11:33 -0400224 }
225 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400226
Scott Bakerfdea1e32020-02-21 15:35:41 -0800227 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400228}
229
230// processUpstreamNonControllerBoundFlow processes non-controller bound flow. We assume that anything that is
231// upstream needs to get Q-in-Q treatment and that this is expressed via two flow rules, the first using the
232// goto-statement. We also assume that the inner tag is applied at the ONU, while the outer tag is
233// applied at the OLT
khenaidoo820197c2020-02-13 16:35:33 -0500234func (fd *FlowDecomposer) processUpstreamNonControllerBoundFlow(ctx context.Context,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800235 path []route.Hop, inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400236
Rohan Agrawal31f21802020-06-12 05:38:46 +0000237 logger.Debugw(ctx, "upstream-non-controller-bound-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400238 deviceRules := fu.NewDeviceRules()
239
npujar1d86a522019-11-14 17:11:16 +0530240 meterID := fu.GetMeterIdFromFlow(flow)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000241 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400242
khenaidoo820197c2020-02-13 16:35:33 -0500243 ingressHop := path[0]
244 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400245
Manikkaraj kb1a10922019-07-29 12:10:34 -0400246 if flow.TableId == 0 && fu.HasNextTable(flow) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000247 logger.Debugw(ctx, "decomposing-onu-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
khenaidoo89b0e942018-10-21 21:11:33 -0400248 if outPortNo != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000249 logger.Warnw(ctx, "outPort-should-not-be-specified", log.Fields{"outPortNo": outPortNo})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800250 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400251 }
npujar1d86a522019-11-14 17:11:16 +0530252 fa := &fu.FlowArgs{
253 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400254 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400255 fu.InPort(ingressHop.Ingress),
256 fu.TunnelId(uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400257 },
khenaidoo68c930b2019-05-13 11:46:51 -0400258 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400259 }
260 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400261 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400262
263 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400264 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400265
khenaidoo89b0e942018-10-21 21:11:33 -0400266 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800267 fs, err := fu.MkFlowStat(fa)
268 if err != nil {
269 return nil, err
270 }
271 fg.AddFlow(fs)
khenaidood20a5852018-10-22 22:09:55 -0400272 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400273 } else if flow.TableId == 1 && outPortNo != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000274 logger.Debugw(ctx, "decomposing-olt-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
npujar1d86a522019-11-14 17:11:16 +0530275 fa := &fu.FlowArgs{
276 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Manikkaraj kb1a10922019-07-29 12:10:34 -0400277 MatchFields: []*ofp.OfpOxmOfbField{
278 fu.InPort(egressHop.Ingress),
279 fu.TunnelId(uint64(inPortNo)),
280 },
khenaidoo89b0e942018-10-21 21:11:33 -0400281 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400282 // Augment the matchfields with the ofpfields from the flow
283 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidoo89b0e942018-10-21 21:11:33 -0400284
Manikkaraj kb1a10922019-07-29 12:10:34 -0400285 //Augment the actions
286 filteredAction := fu.GetActions(flow, fu.OUTPUT)
287 filteredAction = append(filteredAction, fu.Output(egressHop.Egress))
288 fa.Actions = filteredAction
khenaidood20a5852018-10-22 22:09:55 -0400289
Manikkaraj kb1a10922019-07-29 12:10:34 -0400290 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800291 fs, err := fu.MkFlowStat(fa)
292 if err != nil {
293 return nil, err
294 }
295 fg.AddFlow(fs)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400296 deviceRules.AddFlowsAndGroup(egressHop.DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400297 }
Scott Bakerfdea1e32020-02-21 15:35:41 -0800298 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400299}
300
khenaidood20a5852018-10-22 22:09:55 -0400301// processDownstreamFlowWithNextTable decomposes downstream flows containing next table ID instructions
Kent Hagerman6031aad2020-07-29 16:36:33 -0400302func (fd *FlowDecomposer) processDownstreamFlowWithNextTable(ctx context.Context, agent LogicalDeviceAgent, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800303 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000304 logger.Debugw(ctx, "decomposing-olt-flow-in-downstream-flow-with-next-table", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
khenaidood20a5852018-10-22 22:09:55 -0400305 deviceRules := fu.NewDeviceRules()
npujar1d86a522019-11-14 17:11:16 +0530306 meterID := fu.GetMeterIdFromFlow(flow)
Rohan Agrawal31f21802020-06-12 05:38:46 +0000307 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
khenaidood20a5852018-10-22 22:09:55 -0400308
khenaidoo89b0e942018-10-21 21:11:33 -0400309 if outPortNo != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000310 logger.Warnw(ctx, "outPort-should-not-be-specified", log.Fields{"outPortNo": outPortNo})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800311 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400312 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400313
314 if flow.TableId != 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000315 logger.Warnw(ctx, "This is not olt pipeline table, so skipping", log.Fields{"tableId": flow.TableId})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800316 return deviceRules, nil
Manikkaraj kb1a10922019-07-29 12:10:34 -0400317 }
318
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 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000327 logger.Errorw(ctx, "no-route-double-tag", 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:
Rohan Agrawal31f21802020-06-12 05:38:46 +0000332 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 -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 }
Rohan Agrawal31f21802020-06-12 05:38:46 +0000343 innerTag := fu.GetInnerTagFromMetaData(ctx, flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400344 if innerTag == 0 {
Rohan Agrawal31f21802020-06-12 05:38:46 +0000345 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 -0400346 //TODO: Delete flow
Scott Bakerfdea1e32020-02-21 15:35:41 -0800347 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400348 }
npujar1d86a522019-11-14 17:11:16 +0530349 fa := &fu.FlowArgs{
350 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400351 MatchFields: []*ofp.OfpOxmOfbField{
khenaidoo68c930b2019-05-13 11:46:51 -0400352 fu.InPort(ingressHop.Ingress),
Manikkaraj kb1a10922019-07-29 12:10:34 -0400353 fu.Metadata_ofp(uint64(innerTag)),
khenaidoo68c930b2019-05-13 11:46:51 -0400354 fu.TunnelId(uint64(portNumber)),
khenaidoo89b0e942018-10-21 21:11:33 -0400355 },
khenaidoo68c930b2019-05-13 11:46:51 -0400356 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400357 }
358 // 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
442 //having no Group yet is the same as having a Group with no buckets
443 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)
457 //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 {
468 //if no in-port specified for a multicast flow, put NNI port as in-port
khenaidoo820197c2020-02-13 16:35:33 -0500469 //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
503 var err error
Kent Hagerman6031aad2020-07-29 16:36:33 -0400504 if ingressDevice, err = fd.getDevice(ctx, path[0].DeviceID); err != nil {
khenaidoo787224a2020-04-16 18:08:47 -0400505 // This can happen in a race condition where a device is deleted right after we obtain a
506 // route involving the device (GetRoute() above). Handle it as a no route event as well.
507 return deviceRules, fmt.Errorf("get-device-error :%v :%w", err, route.ErrNoRoute)
khenaidoo297cd252019-02-07 22:10:23 -0500508 }
509 isUpstream := !ingressDevice.Root
Manikkaraj kb1a10922019-07-29 12:10:34 -0400510 if isUpstream { // Unicast OLT and ONU UL
Rohan Agrawal31f21802020-06-12 05:38:46 +0000511 logger.Debug(ctx, "process-olt-nd-onu-upstream-noncontrollerbound-unicast-flows", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800512 deviceRules, err = fd.processUpstreamNonControllerBoundFlow(ctx, path, inPortNo, outPortNo, flow)
513 if err != nil {
514 return nil, err
515 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400516 } else if fu.HasNextTable(flow) && flow.TableId == 0 { // Unicast OLT flow DL
Rohan Agrawal31f21802020-06-12 05:38:46 +0000517 logger.Debugw(ctx, "process-olt-downstream-noncontrollerbound-flow-with-nexttable", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800518 deviceRules, err = fd.processDownstreamFlowWithNextTable(ctx, agent, path, inPortNo, outPortNo, flow)
519 if err != nil {
520 return nil, err
521 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400522 } else if flow.TableId == 1 && outPortNo != 0 { // Unicast ONU flow DL
Rohan Agrawal31f21802020-06-12 05:38:46 +0000523 logger.Debugw(ctx, "process-onu-downstream-unicast-flow", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800524 deviceRules, err = fd.processUnicastFlow(ctx, path, inPortNo, outPortNo, flow)
525 if err != nil {
526 return nil, err
527 }
npujar1d86a522019-11-14 17:11:16 +0530528 } else if grpID := fu.GetGroup(flow); grpID != 0 && flow.TableId == 0 { //Multicast
Rohan Agrawal31f21802020-06-12 05:38:46 +0000529 logger.Debugw(ctx, "process-multicast-flow", log.Fields{"flows": flow})
khenaidoo820197c2020-02-13 16:35:33 -0500530 deviceRules = fd.processMulticastFlow(ctx, path, inPortNo, outPortNo, flow, grpID, groupMap)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400531 } else {
khenaidoo820197c2020-02-13 16:35:33 -0500532 return deviceRules, status.Errorf(codes.Aborted, "unknown downstream flow %v", *flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400533 }
534 }
Kent Hagerman6031aad2020-07-29 16:36:33 -0400535 deviceRules, err = fd.updateOutputPortForControllerBoundFlowForParentDevide(ctx, deviceRules)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800536 return deviceRules, err
khenaidoo89b0e942018-10-21 21:11:33 -0400537}