blob: bfeccf6c8986f0e22668af9084f0b3612c936c7f [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"
khenaidoo19d7b632018-10-30 10:49:50 -040022 "github.com/gogo/protobuf/proto"
npujar1d86a522019-11-14 17:11:16 +053023 "github.com/opencord/voltha-go/rw_core/coreif"
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 {
npujar1d86a522019-11-14 17:11:16 +053035 deviceMgr coreif.DeviceManager
khenaidoo89b0e942018-10-21 21:11:33 -040036}
37
npujar1d86a522019-11-14 17:11:16 +053038// NewFlowDecomposer creates flow decomposer instance
39func NewFlowDecomposer(deviceMgr coreif.DeviceManager) *FlowDecomposer {
khenaidoo89b0e942018-10-21 21:11:33 -040040 var decomposer FlowDecomposer
41 decomposer.deviceMgr = deviceMgr
42 return &decomposer
43}
44
45//DecomposeRules decomposes per-device flows and flow-groups from the flows and groups defined on a logical device
khenaidoo820197c2020-02-13 16:35:33 -050046func (fd *FlowDecomposer) DecomposeRules(ctx context.Context, agent coreif.LogicalDeviceAgent, flows ofp.Flows, groups ofp.FlowGroups) (*fu.DeviceRules, error) {
khenaidoo3306c992019-05-24 16:57:35 -040047 deviceRules := *fu.NewDeviceRules()
khenaidoo2c6a0992019-04-29 13:46:56 -040048 devicesToUpdate := make(map[string]string)
khenaidoo89b0e942018-10-21 21:11:33 -040049
50 groupMap := make(map[uint32]*ofp.OfpGroupEntry)
51 for _, groupEntry := range groups.Items {
52 groupMap[groupEntry.Desc.GroupId] = groupEntry
53 }
54
khenaidoo89b0e942018-10-21 21:11:33 -040055 for _, flow := range flows.Items {
khenaidoo820197c2020-02-13 16:35:33 -050056 decomposedRules, err := fd.decomposeFlow(ctx, agent, flow, groupMap)
57 if err != nil {
58 return nil, err
59 }
npujar1d86a522019-11-14 17:11:16 +053060 for deviceID, flowAndGroups := range decomposedRules.Rules {
61 deviceRules.CreateEntryIfNotExist(deviceID)
62 deviceRules.Rules[deviceID].AddFrom(flowAndGroups)
63 devicesToUpdate[deviceID] = deviceID
khenaidoo89b0e942018-10-21 21:11:33 -040064 }
65 }
khenaidoo820197c2020-02-13 16:35:33 -050066 return deviceRules.FilterRules(devicesToUpdate), nil
khenaidoo89b0e942018-10-21 21:11:33 -040067}
68
khenaidoo19d7b632018-10-30 10:49:50 -040069// Handles special case of any controller-bound flow for a parent device
70func (fd *FlowDecomposer) updateOutputPortForControllerBoundFlowForParentDevide(flow *ofp.OfpFlowStats,
Scott Bakerfdea1e32020-02-21 15:35:41 -080071 dr *fu.DeviceRules) (*fu.DeviceRules, error) {
khenaidoo68c930b2019-05-13 11:46:51 -040072 EAPOL := fu.EthType(0x888e)
73 IGMP := fu.IpProto(2)
74 UDP := fu.IpProto(17)
khenaidoo19d7b632018-10-30 10:49:50 -040075
76 newDeviceRules := dr.Copy()
77 // Check whether we are dealing with a parent device
npujar1d86a522019-11-14 17:11:16 +053078 for deviceID, fg := range dr.GetRules() {
79 if root, _ := fd.deviceMgr.IsRootDevice(deviceID); root {
80 newDeviceRules.ClearFlows(deviceID)
khenaidoo19d7b632018-10-30 10:49:50 -040081 for i := 0; i < fg.Flows.Len(); i++ {
82 f := fg.GetFlow(i)
83 UpdateOutPortNo := false
khenaidoo68c930b2019-05-13 11:46:51 -040084 for _, field := range fu.GetOfbFields(f) {
khenaidoo19d7b632018-10-30 10:49:50 -040085 UpdateOutPortNo = (field.String() == EAPOL.String())
86 UpdateOutPortNo = UpdateOutPortNo || (field.String() == IGMP.String())
87 UpdateOutPortNo = UpdateOutPortNo || (field.String() == UDP.String())
88 if UpdateOutPortNo {
89 break
90 }
91 }
92 if UpdateOutPortNo {
khenaidoo68c930b2019-05-13 11:46:51 -040093 f = fu.UpdateOutputPortByActionType(f, uint32(ofp.OfpInstructionType_OFPIT_APPLY_ACTIONS),
khenaidoo19d7b632018-10-30 10:49:50 -040094 uint32(ofp.OfpPortNo_OFPP_CONTROLLER))
95 }
96 // Update flow Id as a change in the instruction field will result in a new flow ID
Scott Bakerfdea1e32020-02-21 15:35:41 -080097 var err error
98 if f.Id, err = fu.HashFlowStats(f); err != nil {
99 return nil, err
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
khenaidood20a5852018-10-22 22:09:55 -0400109//processControllerBoundFlow decomposes trap flows
khenaidoo820197c2020-02-13 16:35:33 -0500110func (fd *FlowDecomposer) processControllerBoundFlow(ctx context.Context, agent coreif.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
Girish Kumarf56a4682020-03-20 20:07:46 +0000113 logger.Debugw("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)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400116 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(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
Humera Kouser4ff89012019-08-25 19:01:51 -0400121 //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
Girish Kumarf56a4682020-03-20 20:07:46 +0000124 logger.Debug("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
Girish Kumarf56a4682020-03-20 20:07:46 +0000144 logger.Debug("trap-uni")
khenaidoo89b0e942018-10-21 21:11:33 -0400145
Matt Jeannerete75f2842020-03-14 15:45:12 -0400146 //inPortNo is 0 for wildcard input case, do not include upstream port for controller bound flow in input
khenaidoo89b0e942018-10-21 21:11:33 -0400147 var inPorts []uint32
148 if inPortNo == 0 {
khenaidood20a5852018-10-22 22:09:55 -0400149 inPorts = agent.GetWildcardInputPorts(egressHop.Egress) // exclude egress_hop.egress_port.port_no
khenaidoo89b0e942018-10-21 21:11:33 -0400150 } else {
151 inPorts = []uint32{inPortNo}
152 }
153 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)
Girish Kumarf56a4682020-03-20 20:07:46 +0000174 logger.Debugw("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)
Girish Kumarf56a4682020-03-20 20:07:46 +0000215 logger.Debugw("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
Girish Kumarf56a4682020-03-20 20:07:46 +0000229 logger.Debugw("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)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400233 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(flow)
234
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) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000239 logger.Debugw("decomposing-onu-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
khenaidoo89b0e942018-10-21 21:11:33 -0400240 if outPortNo != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000241 logger.Warnw("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 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000266 logger.Debugw("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
khenaidoo820197c2020-02-13 16:35:33 -0500294func (fd *FlowDecomposer) processDownstreamFlowWithNextTable(ctx context.Context, agent coreif.LogicalDeviceAgent, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800295 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000296 logger.Debugw("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)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400299 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(flow)
khenaidood20a5852018-10-22 22:09:55 -0400300
khenaidoo89b0e942018-10-21 21:11:33 -0400301 if outPortNo != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000302 logger.Warnw("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 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000307 logger.Warnw("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 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000314 logger.Debugw("creating-metadata-flow", log.Fields{"flow": flow})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400315 portNumber := fu.GetEgressPortNumberFromWriteMetadata(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 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000319 logger.Errorw("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:
Girish Kumarf56a4682020-03-20 20:07:46 +0000324 logger.Errorw("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:
Girish Kumarf56a4682020-03-20 20:07:46 +0000328 logger.Debugw("route-found", log.Fields{"ingressHop": ingressHop, "egressHop": egressHop})
khenaidoo89b0e942018-10-21 21:11:33 -0400329 default:
Girish Kumarf56a4682020-03-20 20:07:46 +0000330 logger.Errorw("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 }
khenaidoo68c930b2019-05-13 11:46:51 -0400335 innerTag := fu.GetInnerTagFromMetaData(flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400336 if innerTag == 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000337 logger.Errorw("no-inner-route-double-tag", log.Fields{"inPortNo": inPortNo, "outPortNo": portNumber, "comment": "deleting-flow", "metadata": metadataFromwriteMetadata})
Manikkaraj kb1a10922019-07-29 12:10:34 -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
Girish Kumarf56a4682020-03-20 20:07:46 +0000364 logger.Debugw("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
Girish Kumarf56a4682020-03-20 20:07:46 +0000395 logger.Debugw("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)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400401 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(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
Girish Kumarf56a4682020-03-20 20:07:46 +0000431 logger.Debugw("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 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000438 logger.Warnw("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 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000442 logger.Warnw("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
npujar467fe752020-01-16 20:17:45 +0530455func (fd *FlowDecomposer) decomposeFlow(ctx context.Context, agent coreif.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 {
464 inPortNo = nniPorts[0]
Girish Kumarf56a4682020-03-20 20:07:46 +0000465 logger.Debugw("assigning-nni-port-as-in-port-for-multicast-flow", log.Fields{"nni": nniPorts[0], "flow:": flow})
Esin Karaman09959ae2019-11-29 13:59:58 +0000466 }
467 }
khenaidoo68c930b2019-05-13 11:46:51 -0400468 outPortNo := fu.GetOutPort(flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400469 deviceRules := fu.NewDeviceRules()
khenaidoo820197c2020-02-13 16:35:33 -0500470 path, err := agent.GetRoute(ctx, inPortNo, outPortNo)
471 if err != nil {
khenaidoo820197c2020-02-13 16:35:33 -0500472 return deviceRules, err
473 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400474
khenaidoo820197c2020-02-13 16:35:33 -0500475 switch len(path) {
khenaidoo89b0e942018-10-21 21:11:33 -0400476 case 0:
khenaidoo787224a2020-04-16 18:08:47 -0400477 return deviceRules, fmt.Errorf("no route from:%d to:%d :%w", inPortNo, outPortNo, route.ErrNoRoute)
khenaidoo89b0e942018-10-21 21:11:33 -0400478 case 2:
Girish Kumarf56a4682020-03-20 20:07:46 +0000479 logger.Debugw("route-found", log.Fields{"ingressHop": path[0], "egressHop": path[1]})
khenaidoo89b0e942018-10-21 21:11:33 -0400480 default:
khenaidoo787224a2020-04-16 18:08:47 -0400481 return deviceRules, fmt.Errorf("invalid route length %d :%w", len(path), route.ErrNoRoute)
khenaidoo89b0e942018-10-21 21:11:33 -0400482 }
483
khenaidoo89b0e942018-10-21 21:11:33 -0400484 // Process controller bound flow
485 if outPortNo != 0 && (outPortNo&0x7fffffff) == uint32(ofp.OfpPortNo_OFPP_CONTROLLER) {
Scott Bakerfdea1e32020-02-21 15:35:41 -0800486 deviceRules, err = fd.processControllerBoundFlow(ctx, agent, path, inPortNo, outPortNo, flow)
487 if err != nil {
488 return nil, err
489 }
khenaidoo89b0e942018-10-21 21:11:33 -0400490 } else {
khenaidoo297cd252019-02-07 22:10:23 -0500491 var ingressDevice *voltha.Device
492 var err error
khenaidoo820197c2020-02-13 16:35:33 -0500493 if ingressDevice, err = fd.deviceMgr.GetDevice(ctx, path[0].DeviceID); err != nil {
khenaidoo787224a2020-04-16 18:08:47 -0400494 // This can happen in a race condition where a device is deleted right after we obtain a
495 // route involving the device (GetRoute() above). Handle it as a no route event as well.
496 return deviceRules, fmt.Errorf("get-device-error :%v :%w", err, route.ErrNoRoute)
khenaidoo297cd252019-02-07 22:10:23 -0500497 }
498 isUpstream := !ingressDevice.Root
Manikkaraj kb1a10922019-07-29 12:10:34 -0400499 if isUpstream { // Unicast OLT and ONU UL
Girish Kumarf56a4682020-03-20 20:07:46 +0000500 logger.Debug("process-olt-nd-onu-upstream-noncontrollerbound-unicast-flows", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800501 deviceRules, err = fd.processUpstreamNonControllerBoundFlow(ctx, path, inPortNo, outPortNo, flow)
502 if err != nil {
503 return nil, err
504 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400505 } else if fu.HasNextTable(flow) && flow.TableId == 0 { // Unicast OLT flow DL
Girish Kumarf56a4682020-03-20 20:07:46 +0000506 logger.Debugw("process-olt-downstream-noncontrollerbound-flow-with-nexttable", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800507 deviceRules, err = fd.processDownstreamFlowWithNextTable(ctx, agent, path, inPortNo, outPortNo, flow)
508 if err != nil {
509 return nil, err
510 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400511 } else if flow.TableId == 1 && outPortNo != 0 { // Unicast ONU flow DL
Girish Kumarf56a4682020-03-20 20:07:46 +0000512 logger.Debugw("process-onu-downstream-unicast-flow", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800513 deviceRules, err = fd.processUnicastFlow(ctx, path, inPortNo, outPortNo, flow)
514 if err != nil {
515 return nil, err
516 }
npujar1d86a522019-11-14 17:11:16 +0530517 } else if grpID := fu.GetGroup(flow); grpID != 0 && flow.TableId == 0 { //Multicast
Girish Kumarf56a4682020-03-20 20:07:46 +0000518 logger.Debugw("process-multicast-flow", log.Fields{"flows": flow})
khenaidoo820197c2020-02-13 16:35:33 -0500519 deviceRules = fd.processMulticastFlow(ctx, path, inPortNo, outPortNo, flow, grpID, groupMap)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400520 } else {
khenaidoo820197c2020-02-13 16:35:33 -0500521 return deviceRules, status.Errorf(codes.Aborted, "unknown downstream flow %v", *flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400522 }
523 }
Scott Bakerfdea1e32020-02-21 15:35:41 -0800524 deviceRules, err = fd.updateOutputPortForControllerBoundFlowForParentDevide(flow, deviceRules)
525 return deviceRules, err
khenaidoo89b0e942018-10-21 21:11:33 -0400526}