blob: 439c64d82160752e891dc2a42c7ffce0553fc6c8 [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"
npujar1d86a522019-11-14 17:11:16 +053024 "github.com/opencord/voltha-go/rw_core/coreif"
khenaidoo820197c2020-02-13 16:35:33 -050025 "github.com/opencord/voltha-go/rw_core/route"
serkant.uluderya2ae470f2020-01-21 11:13:09 -080026 fu "github.com/opencord/voltha-lib-go/v3/pkg/flows"
27 "github.com/opencord/voltha-lib-go/v3/pkg/log"
28 ofp "github.com/opencord/voltha-protos/v3/go/openflow_13"
29 "github.com/opencord/voltha-protos/v3/go/voltha"
khenaidoo820197c2020-02-13 16:35:33 -050030 "google.golang.org/grpc/codes"
31 "google.golang.org/grpc/status"
khenaidoo89b0e942018-10-21 21:11:33 -040032)
33
npujar1d86a522019-11-14 17:11:16 +053034// FlowDecomposer represent flow decomposer attribute
khenaidoo89b0e942018-10-21 21:11:33 -040035type FlowDecomposer struct {
npujar1d86a522019-11-14 17:11:16 +053036 deviceMgr coreif.DeviceManager
khenaidoo89b0e942018-10-21 21:11:33 -040037}
38
npujar1d86a522019-11-14 17:11:16 +053039// NewFlowDecomposer creates flow decomposer instance
Neha Sharmabe485932020-05-25 21:52:55 +000040func NewFlowDecomposer(ctx context.Context, deviceMgr coreif.DeviceManager) *FlowDecomposer {
khenaidoo89b0e942018-10-21 21:11:33 -040041 var decomposer FlowDecomposer
42 decomposer.deviceMgr = deviceMgr
43 return &decomposer
44}
45
46//DecomposeRules decomposes per-device flows and flow-groups from the flows and groups defined on a logical device
khenaidoo820197c2020-02-13 16:35:33 -050047func (fd *FlowDecomposer) DecomposeRules(ctx context.Context, agent coreif.LogicalDeviceAgent, flows ofp.Flows, groups ofp.FlowGroups) (*fu.DeviceRules, error) {
Neha Sharmabe485932020-05-25 21:52:55 +000048 deviceRules := *fu.NewDeviceRules(ctx)
khenaidoo2c6a0992019-04-29 13:46:56 -040049 devicesToUpdate := make(map[string]string)
khenaidoo89b0e942018-10-21 21:11:33 -040050
51 groupMap := make(map[uint32]*ofp.OfpGroupEntry)
52 for _, groupEntry := range groups.Items {
53 groupMap[groupEntry.Desc.GroupId] = groupEntry
54 }
55
khenaidoo89b0e942018-10-21 21:11:33 -040056 for _, flow := range flows.Items {
khenaidoo820197c2020-02-13 16:35:33 -050057 decomposedRules, err := fd.decomposeFlow(ctx, agent, flow, groupMap)
58 if err != nil {
59 return nil, err
60 }
npujar1d86a522019-11-14 17:11:16 +053061 for deviceID, flowAndGroups := range decomposedRules.Rules {
Neha Sharmabe485932020-05-25 21:52:55 +000062 deviceRules.CreateEntryIfNotExist(ctx, deviceID)
63 deviceRules.Rules[deviceID].AddFrom(ctx, flowAndGroups)
npujar1d86a522019-11-14 17:11:16 +053064 devicesToUpdate[deviceID] = deviceID
khenaidoo89b0e942018-10-21 21:11:33 -040065 }
66 }
Neha Sharmabe485932020-05-25 21:52:55 +000067 return deviceRules.FilterRules(ctx, devicesToUpdate), nil
khenaidoo89b0e942018-10-21 21:11:33 -040068}
69
khenaidoo19d7b632018-10-30 10:49:50 -040070// Handles special case of any controller-bound flow for a parent device
Neha Sharmabe485932020-05-25 21:52:55 +000071func (fd *FlowDecomposer) updateOutputPortForControllerBoundFlowForParentDevide(ctx context.Context, flow *ofp.OfpFlowStats,
Scott Bakerfdea1e32020-02-21 15:35:41 -080072 dr *fu.DeviceRules) (*fu.DeviceRules, error) {
Neha Sharmabe485932020-05-25 21:52:55 +000073 EAPOL := fu.EthType(ctx, 0x888e)
74 IGMP := fu.IpProto(ctx, 2)
75 UDP := fu.IpProto(ctx, 17)
khenaidoo19d7b632018-10-30 10:49:50 -040076
Neha Sharmabe485932020-05-25 21:52:55 +000077 newDeviceRules := dr.Copy(ctx)
khenaidoo19d7b632018-10-30 10:49:50 -040078 // Check whether we are dealing with a parent device
Neha Sharmabe485932020-05-25 21:52:55 +000079 for deviceID, fg := range dr.GetRules(ctx) {
80 if root, _ := fd.deviceMgr.IsRootDevice(ctx, deviceID); root {
81 newDeviceRules.ClearFlows(ctx, deviceID)
khenaidoo19d7b632018-10-30 10:49:50 -040082 for i := 0; i < fg.Flows.Len(); i++ {
Neha Sharmabe485932020-05-25 21:52:55 +000083 f := fg.GetFlow(ctx, i)
khenaidoo19d7b632018-10-30 10:49:50 -040084 UpdateOutPortNo := false
Neha Sharmabe485932020-05-25 21:52:55 +000085 for _, field := range fu.GetOfbFields(ctx, f) {
khenaidoo19d7b632018-10-30 10:49:50 -040086 UpdateOutPortNo = (field.String() == EAPOL.String())
87 UpdateOutPortNo = UpdateOutPortNo || (field.String() == IGMP.String())
88 UpdateOutPortNo = UpdateOutPortNo || (field.String() == UDP.String())
89 if UpdateOutPortNo {
90 break
91 }
92 }
93 if UpdateOutPortNo {
Neha Sharmabe485932020-05-25 21:52:55 +000094 f = fu.UpdateOutputPortByActionType(ctx, f, uint32(ofp.OfpInstructionType_OFPIT_APPLY_ACTIONS),
khenaidoo19d7b632018-10-30 10:49:50 -040095 uint32(ofp.OfpPortNo_OFPP_CONTROLLER))
96 }
97 // Update flow Id as a change in the instruction field will result in a new flow ID
Mahir Gunyeladdb66a2020-04-29 18:08:50 -070098 //var err error
99 //if f.Id, err = fu.HashFlowStats(f); err != nil {
100 //return nil, err
101 //}
Neha Sharmabe485932020-05-25 21:52:55 +0000102 newDeviceRules.AddFlow(ctx, deviceID, (proto.Clone(f)).(*ofp.OfpFlowStats))
khenaidoo19d7b632018-10-30 10:49:50 -0400103 }
104 }
105 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400106
Scott Bakerfdea1e32020-02-21 15:35:41 -0800107 return newDeviceRules, nil
khenaidoo19d7b632018-10-30 10:49:50 -0400108}
109
khenaidood20a5852018-10-22 22:09:55 -0400110//processControllerBoundFlow decomposes trap flows
khenaidoo820197c2020-02-13 16:35:33 -0500111func (fd *FlowDecomposer) processControllerBoundFlow(ctx context.Context, agent coreif.LogicalDeviceAgent, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800112 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400113
Girish Kumarf56a4682020-03-20 20:07:46 +0000114 logger.Debugw("trap-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo, "flow": flow})
Neha Sharmabe485932020-05-25 21:52:55 +0000115 deviceRules := fu.NewDeviceRules(ctx)
116 meterID := fu.GetMeterIdFromFlow(ctx, flow)
117 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
khenaidood20a5852018-10-22 22:09:55 -0400118
khenaidoo820197c2020-02-13 16:35:33 -0500119 ingressHop := path[0]
120 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400121
Humera Kouser4ff89012019-08-25 19:01:51 -0400122 //case of packet_in from NNI port rule
Neha Sharmabe485932020-05-25 21:52:55 +0000123 if agent.GetDeviceRoutes(ctx).IsRootPort(ctx, inPortNo) {
Humera Kouser4ff89012019-08-25 19:01:51 -0400124 // Trap flow for NNI port
Girish Kumarf56a4682020-03-20 20:07:46 +0000125 logger.Debug("trap-nni")
Humera Kouser4ff89012019-08-25 19:01:51 -0400126
npujar1d86a522019-11-14 17:11:16 +0530127 fa := &fu.FlowArgs{
Humera Kouser4ff89012019-08-25 19:01:51 -0400128 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie},
129 MatchFields: []*ofp.OfpOxmOfbField{
Neha Sharmabe485932020-05-25 21:52:55 +0000130 fu.InPort(ctx, egressHop.Egress),
Humera Kouser4ff89012019-08-25 19:01:51 -0400131 },
Neha Sharmabe485932020-05-25 21:52:55 +0000132 Actions: fu.GetActions(ctx, flow),
Humera Kouser4ff89012019-08-25 19:01:51 -0400133 }
134 // Augment the matchfields with the ofpfields from the flow
Neha Sharmabe485932020-05-25 21:52:55 +0000135 fg := fu.NewFlowsAndGroups(ctx)
136 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(ctx, flow, fu.IN_PORT)...)
137 fs, err := fu.MkFlowStat(ctx, fa)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800138 if err != nil {
139 return nil, err
140 }
Neha Sharmabe485932020-05-25 21:52:55 +0000141 fg.AddFlow(ctx, fs)
142 deviceRules.AddFlowsAndGroup(ctx, egressHop.DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400143 } else {
144 // Trap flow for UNI port
Girish Kumarf56a4682020-03-20 20:07:46 +0000145 logger.Debug("trap-uni")
khenaidoo89b0e942018-10-21 21:11:33 -0400146
Matt Jeannerete75f2842020-03-14 15:45:12 -0400147 //inPortNo is 0 for wildcard input case, do not include upstream port for controller bound flow in input
khenaidoo89b0e942018-10-21 21:11:33 -0400148 var inPorts []uint32
149 if inPortNo == 0 {
Neha Sharmabe485932020-05-25 21:52:55 +0000150 inPorts = agent.GetWildcardInputPorts(ctx, egressHop.Egress) // exclude egress_hop.egress_port.port_no
khenaidoo89b0e942018-10-21 21:11:33 -0400151 } else {
152 inPorts = []uint32{inPortNo}
153 }
154 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{
Neha Sharmabe485932020-05-25 21:52:55 +0000159 fu.InPort(ctx, egressHop.Ingress),
160 fu.TunnelId(ctx, uint64(inputPort)),
khenaidoo89b0e942018-10-21 21:11:33 -0400161 },
162 Actions: []*ofp.OfpAction{
Neha Sharmabe485932020-05-25 21:52:55 +0000163 fu.Output(ctx, egressHop.Egress),
khenaidoo89b0e942018-10-21 21:11:33 -0400164 },
165 }
166 // Augment the matchfields with the ofpfields from the flow
Neha Sharmabe485932020-05-25 21:52:55 +0000167 faParent.MatchFields = append(faParent.MatchFields, fu.GetOfbFields(ctx, flow, fu.IN_PORT)...)
168 fgParent := fu.NewFlowsAndGroups(ctx)
169 fs, err := fu.MkFlowStat(ctx, faParent)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800170 if err != nil {
171 return nil, err
172 }
Neha Sharmabe485932020-05-25 21:52:55 +0000173 fgParent.AddFlow(ctx, fs)
174 deviceRules.AddFlowsAndGroup(ctx, egressHop.DeviceID, fgParent)
Girish Kumarf56a4682020-03-20 20:07:46 +0000175 logger.Debugw("parent-trap-flow-set", log.Fields{"flow": faParent})
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400176
177 // Upstream flow on child (onu) device
178 var actions []*ofp.OfpAction
Neha Sharmabe485932020-05-25 21:52:55 +0000179 setvid := fu.GetVlanVid(ctx, flow)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400180 if setvid != nil {
181 // have this child push the vlan the parent is matching/trapping on above
182 actions = []*ofp.OfpAction{
Neha Sharmabe485932020-05-25 21:52:55 +0000183 fu.PushVlan(ctx, 0x8100),
184 fu.SetField(ctx, fu.VlanVid(ctx, *setvid)),
185 fu.Output(ctx, ingressHop.Egress),
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400186 }
187 } else {
188 // otherwise just set the egress port
189 actions = []*ofp.OfpAction{
Neha Sharmabe485932020-05-25 21:52:55 +0000190 fu.Output(ctx, ingressHop.Egress),
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400191 }
192 }
npujar1d86a522019-11-14 17:11:16 +0530193 faChild := &fu.FlowArgs{
194 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400195 MatchFields: []*ofp.OfpOxmOfbField{
Neha Sharmabe485932020-05-25 21:52:55 +0000196 fu.InPort(ctx, ingressHop.Ingress),
197 fu.TunnelId(ctx, uint64(inputPort)),
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400198 },
199 Actions: actions,
200 }
201 // Augment the matchfields with the ofpfields from the flow.
202 // If the parent has a match vid and the child is setting that match vid exclude the the match vlan
203 // for the child given it will be setting that vlan and the parent will be matching on it
204 if setvid != nil {
Neha Sharmabe485932020-05-25 21:52:55 +0000205 faChild.MatchFields = append(faChild.MatchFields, fu.GetOfbFields(ctx, flow, fu.IN_PORT, fu.VLAN_VID)...)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400206 } else {
Neha Sharmabe485932020-05-25 21:52:55 +0000207 faChild.MatchFields = append(faChild.MatchFields, fu.GetOfbFields(ctx, flow, fu.IN_PORT)...)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400208 }
Neha Sharmabe485932020-05-25 21:52:55 +0000209 fgChild := fu.NewFlowsAndGroups(ctx)
210 fs, err = fu.MkFlowStat(ctx, faChild)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800211 if err != nil {
212 return nil, err
213 }
Neha Sharmabe485932020-05-25 21:52:55 +0000214 fgChild.AddFlow(ctx, fs)
215 deviceRules.AddFlowsAndGroup(ctx, ingressHop.DeviceID, fgChild)
Girish Kumarf56a4682020-03-20 20:07:46 +0000216 logger.Debugw("child-trap-flow-set", log.Fields{"flow": faChild})
khenaidoo89b0e942018-10-21 21:11:33 -0400217 }
218 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400219
Scott Bakerfdea1e32020-02-21 15:35:41 -0800220 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400221}
222
223// processUpstreamNonControllerBoundFlow processes non-controller bound flow. We assume that anything that is
224// upstream needs to get Q-in-Q treatment and that this is expressed via two flow rules, the first using the
225// goto-statement. We also assume that the inner tag is applied at the ONU, while the outer tag is
226// applied at the OLT
khenaidoo820197c2020-02-13 16:35:33 -0500227func (fd *FlowDecomposer) processUpstreamNonControllerBoundFlow(ctx context.Context,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800228 path []route.Hop, inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400229
Girish Kumarf56a4682020-03-20 20:07:46 +0000230 logger.Debugw("upstream-non-controller-bound-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
Neha Sharmabe485932020-05-25 21:52:55 +0000231 deviceRules := fu.NewDeviceRules(ctx)
khenaidood20a5852018-10-22 22:09:55 -0400232
Neha Sharmabe485932020-05-25 21:52:55 +0000233 meterID := fu.GetMeterIdFromFlow(ctx, flow)
234 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400235
khenaidoo820197c2020-02-13 16:35:33 -0500236 ingressHop := path[0]
237 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400238
Neha Sharmabe485932020-05-25 21:52:55 +0000239 if flow.TableId == 0 && fu.HasNextTable(ctx, flow) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000240 logger.Debugw("decomposing-onu-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
khenaidoo89b0e942018-10-21 21:11:33 -0400241 if outPortNo != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000242 logger.Warnw("outPort-should-not-be-specified", log.Fields{"outPortNo": outPortNo})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800243 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400244 }
npujar1d86a522019-11-14 17:11:16 +0530245 fa := &fu.FlowArgs{
246 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400247 MatchFields: []*ofp.OfpOxmOfbField{
Neha Sharmabe485932020-05-25 21:52:55 +0000248 fu.InPort(ctx, ingressHop.Ingress),
249 fu.TunnelId(ctx, uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400250 },
Neha Sharmabe485932020-05-25 21:52:55 +0000251 Actions: fu.GetActions(ctx, flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400252 }
253 // Augment the matchfields with the ofpfields from the flow
Neha Sharmabe485932020-05-25 21:52:55 +0000254 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(ctx, flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400255
256 // Augment the Actions
Neha Sharmabe485932020-05-25 21:52:55 +0000257 fa.Actions = append(fa.Actions, fu.Output(ctx, ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400258
Neha Sharmabe485932020-05-25 21:52:55 +0000259 fg := fu.NewFlowsAndGroups(ctx)
260 fs, err := fu.MkFlowStat(ctx, fa)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800261 if err != nil {
262 return nil, err
263 }
Neha Sharmabe485932020-05-25 21:52:55 +0000264 fg.AddFlow(ctx, fs)
265 deviceRules.AddFlowsAndGroup(ctx, ingressHop.DeviceID, fg)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400266 } else if flow.TableId == 1 && outPortNo != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000267 logger.Debugw("decomposing-olt-flow-in-upstream-has-next-table", log.Fields{"table_id": flow.TableId})
npujar1d86a522019-11-14 17:11:16 +0530268 fa := &fu.FlowArgs{
269 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Manikkaraj kb1a10922019-07-29 12:10:34 -0400270 MatchFields: []*ofp.OfpOxmOfbField{
Neha Sharmabe485932020-05-25 21:52:55 +0000271 fu.InPort(ctx, egressHop.Ingress),
272 fu.TunnelId(ctx, uint64(inPortNo)),
Manikkaraj kb1a10922019-07-29 12:10:34 -0400273 },
khenaidoo89b0e942018-10-21 21:11:33 -0400274 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400275 // Augment the matchfields with the ofpfields from the flow
Neha Sharmabe485932020-05-25 21:52:55 +0000276 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(ctx, flow, fu.IN_PORT)...)
khenaidoo89b0e942018-10-21 21:11:33 -0400277
Manikkaraj kb1a10922019-07-29 12:10:34 -0400278 //Augment the actions
Neha Sharmabe485932020-05-25 21:52:55 +0000279 filteredAction := fu.GetActions(ctx, flow, fu.OUTPUT)
280 filteredAction = append(filteredAction, fu.Output(ctx, egressHop.Egress))
Manikkaraj kb1a10922019-07-29 12:10:34 -0400281 fa.Actions = filteredAction
khenaidood20a5852018-10-22 22:09:55 -0400282
Neha Sharmabe485932020-05-25 21:52:55 +0000283 fg := fu.NewFlowsAndGroups(ctx)
284 fs, err := fu.MkFlowStat(ctx, fa)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800285 if err != nil {
286 return nil, err
287 }
Neha Sharmabe485932020-05-25 21:52:55 +0000288 fg.AddFlow(ctx, fs)
289 deviceRules.AddFlowsAndGroup(ctx, egressHop.DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400290 }
Scott Bakerfdea1e32020-02-21 15:35:41 -0800291 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400292}
293
khenaidood20a5852018-10-22 22:09:55 -0400294// processDownstreamFlowWithNextTable decomposes downstream flows containing next table ID instructions
khenaidoo820197c2020-02-13 16:35:33 -0500295func (fd *FlowDecomposer) processDownstreamFlowWithNextTable(ctx context.Context, agent coreif.LogicalDeviceAgent, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800296 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
Girish Kumarf56a4682020-03-20 20:07:46 +0000297 logger.Debugw("decomposing-olt-flow-in-downstream-flow-with-next-table", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
Neha Sharmabe485932020-05-25 21:52:55 +0000298 deviceRules := fu.NewDeviceRules(ctx)
299 meterID := fu.GetMeterIdFromFlow(ctx, flow)
300 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
khenaidood20a5852018-10-22 22:09:55 -0400301
khenaidoo89b0e942018-10-21 21:11:33 -0400302 if outPortNo != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000303 logger.Warnw("outPort-should-not-be-specified", log.Fields{"outPortNo": outPortNo})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800304 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400305 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400306
307 if flow.TableId != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000308 logger.Warnw("This is not olt pipeline table, so skipping", log.Fields{"tableId": flow.TableId})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800309 return deviceRules, nil
Manikkaraj kb1a10922019-07-29 12:10:34 -0400310 }
311
khenaidoo820197c2020-02-13 16:35:33 -0500312 ingressHop := path[0]
313 egressHop := path[1]
Manikkaraj kb1a10922019-07-29 12:10:34 -0400314 if metadataFromwriteMetadata != 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000315 logger.Debugw("creating-metadata-flow", log.Fields{"flow": flow})
Neha Sharmabe485932020-05-25 21:52:55 +0000316 portNumber := fu.GetEgressPortNumberFromWriteMetadata(ctx, flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400317 if portNumber != 0 {
khenaidoo820197c2020-02-13 16:35:33 -0500318 recalculatedRoute, err := agent.GetRoute(ctx, inPortNo, portNumber)
319 if err != nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000320 logger.Errorw("no-route-double-tag", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo, "metadata": metadataFromwriteMetadata, "error": err})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800321 return deviceRules, nil
khenaidoo820197c2020-02-13 16:35:33 -0500322 }
khenaidoo89b0e942018-10-21 21:11:33 -0400323 switch len(recalculatedRoute) {
324 case 0:
Girish Kumarf56a4682020-03-20 20:07:46 +0000325 logger.Errorw("no-route-double-tag", log.Fields{"inPortNo": inPortNo, "outPortNo": portNumber, "comment": "deleting-flow", "metadata": metadataFromwriteMetadata})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400326 //TODO: Delete flow
Scott Bakerfdea1e32020-02-21 15:35:41 -0800327 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400328 case 2:
Girish Kumarf56a4682020-03-20 20:07:46 +0000329 logger.Debugw("route-found", log.Fields{"ingressHop": ingressHop, "egressHop": egressHop})
khenaidoo89b0e942018-10-21 21:11:33 -0400330 default:
Girish Kumarf56a4682020-03-20 20:07:46 +0000331 logger.Errorw("invalid-route-length", log.Fields{"routeLen": len(path)})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800332 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400333 }
334 ingressHop = recalculatedRoute[0]
335 }
Neha Sharmabe485932020-05-25 21:52:55 +0000336 innerTag := fu.GetInnerTagFromMetaData(ctx, flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400337 if innerTag == 0 {
Girish Kumarf56a4682020-03-20 20:07:46 +0000338 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 -0400339 //TODO: Delete flow
Scott Bakerfdea1e32020-02-21 15:35:41 -0800340 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400341 }
npujar1d86a522019-11-14 17:11:16 +0530342 fa := &fu.FlowArgs{
343 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400344 MatchFields: []*ofp.OfpOxmOfbField{
Neha Sharmabe485932020-05-25 21:52:55 +0000345 fu.InPort(ctx, ingressHop.Ingress),
346 fu.Metadata_ofp(ctx, uint64(innerTag)),
347 fu.TunnelId(ctx, uint64(portNumber)),
khenaidoo89b0e942018-10-21 21:11:33 -0400348 },
Neha Sharmabe485932020-05-25 21:52:55 +0000349 Actions: fu.GetActions(ctx, flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400350 }
351 // Augment the matchfields with the ofpfields from the flow
Neha Sharmabe485932020-05-25 21:52:55 +0000352 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(ctx, flow, fu.IN_PORT, fu.METADATA)...)
khenaidood20a5852018-10-22 22:09:55 -0400353
354 // Augment the Actions
Neha Sharmabe485932020-05-25 21:52:55 +0000355 fa.Actions = append(fa.Actions, fu.Output(ctx, ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400356
Neha Sharmabe485932020-05-25 21:52:55 +0000357 fg := fu.NewFlowsAndGroups(ctx)
358 fs, err := fu.MkFlowStat(ctx, fa)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800359 if err != nil {
360 return nil, err
361 }
Neha Sharmabe485932020-05-25 21:52:55 +0000362 fg.AddFlow(ctx, fs)
363 deviceRules.AddFlowsAndGroup(ctx, ingressHop.DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400364 } else { // Create standard flow
Girish Kumarf56a4682020-03-20 20:07:46 +0000365 logger.Debugw("creating-standard-flow", log.Fields{"flow": flow})
npujar1d86a522019-11-14 17:11:16 +0530366 fa := &fu.FlowArgs{
367 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
khenaidoo89b0e942018-10-21 21:11:33 -0400368 MatchFields: []*ofp.OfpOxmOfbField{
Neha Sharmabe485932020-05-25 21:52:55 +0000369 fu.InPort(ctx, ingressHop.Ingress),
370 fu.TunnelId(ctx, uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400371 },
Neha Sharmabe485932020-05-25 21:52:55 +0000372 Actions: fu.GetActions(ctx, flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400373 }
374 // Augment the matchfields with the ofpfields from the flow
Neha Sharmabe485932020-05-25 21:52:55 +0000375 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(ctx, flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400376
377 // Augment the Actions
Neha Sharmabe485932020-05-25 21:52:55 +0000378 fa.Actions = append(fa.Actions, fu.Output(ctx, ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400379
Neha Sharmabe485932020-05-25 21:52:55 +0000380 fg := fu.NewFlowsAndGroups(ctx)
381 fs, err := fu.MkFlowStat(ctx, fa)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800382 if err != nil {
383 return nil, err
384 }
Neha Sharmabe485932020-05-25 21:52:55 +0000385 fg.AddFlow(ctx, fs)
386 deviceRules.AddFlowsAndGroup(ctx, ingressHop.DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400387 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400388
Scott Bakerfdea1e32020-02-21 15:35:41 -0800389 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400390}
391
khenaidood20a5852018-10-22 22:09:55 -0400392// processUnicastFlow decomposes unicast flows
khenaidoo820197c2020-02-13 16:35:33 -0500393func (fd *FlowDecomposer) processUnicastFlow(ctx context.Context, path []route.Hop,
Scott Bakerfdea1e32020-02-21 15:35:41 -0800394 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400395
Girish Kumarf56a4682020-03-20 20:07:46 +0000396 logger.Debugw("decomposing-onu-flow-in-downstream-unicast-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
Neha Sharmabe485932020-05-25 21:52:55 +0000397 deviceRules := fu.NewDeviceRules(ctx)
khenaidood20a5852018-10-22 22:09:55 -0400398
khenaidoo820197c2020-02-13 16:35:33 -0500399 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400400
Neha Sharmabe485932020-05-25 21:52:55 +0000401 meterID := fu.GetMeterIdFromFlow(ctx, flow)
402 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(ctx, flow)
npujar1d86a522019-11-14 17:11:16 +0530403 fa := &fu.FlowArgs{
404 KV: fu.OfpFlowModArgs{"priority": uint64(flow.Priority), "cookie": flow.Cookie, "meter_id": uint64(meterID), "write_metadata": metadataFromwriteMetadata},
Manikkaraj kb1a10922019-07-29 12:10:34 -0400405 MatchFields: []*ofp.OfpOxmOfbField{
Neha Sharmabe485932020-05-25 21:52:55 +0000406 fu.InPort(ctx, egressHop.Ingress),
Manikkaraj kb1a10922019-07-29 12:10:34 -0400407 },
khenaidoo89b0e942018-10-21 21:11:33 -0400408 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400409 // Augment the matchfields with the ofpfields from the flow
Neha Sharmabe485932020-05-25 21:52:55 +0000410 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(ctx, flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400411
Manikkaraj kb1a10922019-07-29 12:10:34 -0400412 // Augment the Actions
Neha Sharmabe485932020-05-25 21:52:55 +0000413 filteredAction := fu.GetActions(ctx, flow, fu.OUTPUT)
414 filteredAction = append(filteredAction, fu.Output(ctx, egressHop.Egress))
Manikkaraj kb1a10922019-07-29 12:10:34 -0400415 fa.Actions = filteredAction
khenaidoo89b0e942018-10-21 21:11:33 -0400416
Neha Sharmabe485932020-05-25 21:52:55 +0000417 fg := fu.NewFlowsAndGroups(ctx)
418 fs, err := fu.MkFlowStat(ctx, fa)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800419 if err != nil {
420 return nil, err
421 }
Neha Sharmabe485932020-05-25 21:52:55 +0000422 fg.AddFlow(ctx, fs)
423 deviceRules.AddFlowsAndGroup(ctx, egressHop.DeviceID, fg)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800424 return deviceRules, nil
khenaidoo89b0e942018-10-21 21:11:33 -0400425}
426
khenaidood20a5852018-10-22 22:09:55 -0400427// processMulticastFlow decompose multicast flows
khenaidoo820197c2020-02-13 16:35:33 -0500428func (fd *FlowDecomposer) processMulticastFlow(ctx context.Context, path []route.Hop,
npujar1d86a522019-11-14 17:11:16 +0530429 inPortNo uint32, outPortNo uint32, flow *ofp.OfpFlowStats, grpID uint32,
khenaidood20a5852018-10-22 22:09:55 -0400430 groupMap map[uint32]*ofp.OfpGroupEntry) *fu.DeviceRules {
431
Girish Kumarf56a4682020-03-20 20:07:46 +0000432 logger.Debugw("multicast-flow", log.Fields{"inPortNo": inPortNo, "outPortNo": outPortNo})
Neha Sharmabe485932020-05-25 21:52:55 +0000433 deviceRules := fu.NewDeviceRules(ctx)
khenaidoo89b0e942018-10-21 21:11:33 -0400434
435 //having no Group yet is the same as having a Group with no buckets
436 var grp *ofp.OfpGroupEntry
437 var ok bool
npujar1d86a522019-11-14 17:11:16 +0530438 if grp, ok = groupMap[grpID]; !ok {
Girish Kumarf56a4682020-03-20 20:07:46 +0000439 logger.Warnw("Group-id-not-present-in-map", log.Fields{"grpId": grpID, "groupMap": groupMap})
khenaidoo89b0e942018-10-21 21:11:33 -0400440 return deviceRules
441 }
442 if grp == nil || grp.Desc == nil {
Girish Kumarf56a4682020-03-20 20:07:46 +0000443 logger.Warnw("Group-or-desc-nil", log.Fields{"grpId": grpID, "grp": grp})
khenaidoo89b0e942018-10-21 21:11:33 -0400444 return deviceRules
445 }
khenaidoo89b0e942018-10-21 21:11:33 -0400446
Neha Sharmabe485932020-05-25 21:52:55 +0000447 deviceRules.CreateEntryIfNotExist(ctx, path[0].DeviceID)
448 fg := fu.NewFlowsAndGroups(ctx)
449 fg.AddFlow(ctx, flow)
Esin Karaman09959ae2019-11-29 13:59:58 +0000450 //return the multicast flow without decomposing it
Neha Sharmabe485932020-05-25 21:52:55 +0000451 deviceRules.AddFlowsAndGroup(ctx, path[0].DeviceID, fg)
khenaidoo89b0e942018-10-21 21:11:33 -0400452 return deviceRules
453}
454
khenaidood20a5852018-10-22 22:09:55 -0400455// decomposeFlow decomposes a flow for a logical device into flows for each physical device
npujar467fe752020-01-16 20:17:45 +0530456func (fd *FlowDecomposer) decomposeFlow(ctx context.Context, agent coreif.LogicalDeviceAgent, flow *ofp.OfpFlowStats,
khenaidoo820197c2020-02-13 16:35:33 -0500457 groupMap map[uint32]*ofp.OfpGroupEntry) (*fu.DeviceRules, error) {
khenaidood20a5852018-10-22 22:09:55 -0400458
Neha Sharmabe485932020-05-25 21:52:55 +0000459 inPortNo := fu.GetInPort(ctx, flow)
460 if fu.HasGroup(ctx, flow) && inPortNo == 0 {
Esin Karaman09959ae2019-11-29 13:59:58 +0000461 //if no in-port specified for a multicast flow, put NNI port as in-port
khenaidoo820197c2020-02-13 16:35:33 -0500462 //so that a valid path can be found for the flow
Neha Sharmabe485932020-05-25 21:52:55 +0000463 nniPorts := agent.GetNNIPorts(ctx)
Esin Karaman09959ae2019-11-29 13:59:58 +0000464 if len(nniPorts) > 0 {
465 inPortNo = nniPorts[0]
Girish Kumarf56a4682020-03-20 20:07:46 +0000466 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 +0000467 }
468 }
Neha Sharmabe485932020-05-25 21:52:55 +0000469 outPortNo := fu.GetOutPort(ctx, flow)
470 deviceRules := fu.NewDeviceRules(ctx)
khenaidoo820197c2020-02-13 16:35:33 -0500471 path, err := agent.GetRoute(ctx, inPortNo, outPortNo)
472 if err != nil {
khenaidoo820197c2020-02-13 16:35:33 -0500473 return deviceRules, err
474 }
khenaidoo2c6a0992019-04-29 13:46:56 -0400475
khenaidoo820197c2020-02-13 16:35:33 -0500476 switch len(path) {
khenaidoo89b0e942018-10-21 21:11:33 -0400477 case 0:
khenaidoo787224a2020-04-16 18:08:47 -0400478 return deviceRules, fmt.Errorf("no route from:%d to:%d :%w", inPortNo, outPortNo, route.ErrNoRoute)
khenaidoo89b0e942018-10-21 21:11:33 -0400479 case 2:
Girish Kumarf56a4682020-03-20 20:07:46 +0000480 logger.Debugw("route-found", log.Fields{"ingressHop": path[0], "egressHop": path[1]})
khenaidoo89b0e942018-10-21 21:11:33 -0400481 default:
khenaidoo787224a2020-04-16 18:08:47 -0400482 return deviceRules, fmt.Errorf("invalid route length %d :%w", len(path), route.ErrNoRoute)
khenaidoo89b0e942018-10-21 21:11:33 -0400483 }
484
khenaidoo89b0e942018-10-21 21:11:33 -0400485 // Process controller bound flow
486 if outPortNo != 0 && (outPortNo&0x7fffffff) == uint32(ofp.OfpPortNo_OFPP_CONTROLLER) {
Scott Bakerfdea1e32020-02-21 15:35:41 -0800487 deviceRules, err = fd.processControllerBoundFlow(ctx, agent, path, inPortNo, outPortNo, flow)
488 if err != nil {
489 return nil, err
490 }
khenaidoo89b0e942018-10-21 21:11:33 -0400491 } else {
khenaidoo297cd252019-02-07 22:10:23 -0500492 var ingressDevice *voltha.Device
493 var err error
Kent Hagerman45a13e42020-04-13 12:23:50 -0400494 if ingressDevice, err = fd.deviceMgr.GetDevice(ctx, &voltha.ID{Id: path[0].DeviceID}); err != nil {
khenaidoo787224a2020-04-16 18:08:47 -0400495 // This can happen in a race condition where a device is deleted right after we obtain a
496 // route involving the device (GetRoute() above). Handle it as a no route event as well.
497 return deviceRules, fmt.Errorf("get-device-error :%v :%w", err, route.ErrNoRoute)
khenaidoo297cd252019-02-07 22:10:23 -0500498 }
499 isUpstream := !ingressDevice.Root
Manikkaraj kb1a10922019-07-29 12:10:34 -0400500 if isUpstream { // Unicast OLT and ONU UL
Girish Kumarf56a4682020-03-20 20:07:46 +0000501 logger.Debug("process-olt-nd-onu-upstream-noncontrollerbound-unicast-flows", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800502 deviceRules, err = fd.processUpstreamNonControllerBoundFlow(ctx, path, inPortNo, outPortNo, flow)
503 if err != nil {
504 return nil, err
505 }
Neha Sharmabe485932020-05-25 21:52:55 +0000506 } else if fu.HasNextTable(ctx, flow) && flow.TableId == 0 { // Unicast OLT flow DL
Girish Kumarf56a4682020-03-20 20:07:46 +0000507 logger.Debugw("process-olt-downstream-noncontrollerbound-flow-with-nexttable", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800508 deviceRules, err = fd.processDownstreamFlowWithNextTable(ctx, agent, path, inPortNo, outPortNo, flow)
509 if err != nil {
510 return nil, err
511 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400512 } else if flow.TableId == 1 && outPortNo != 0 { // Unicast ONU flow DL
Girish Kumarf56a4682020-03-20 20:07:46 +0000513 logger.Debugw("process-onu-downstream-unicast-flow", log.Fields{"flows": flow})
Scott Bakerfdea1e32020-02-21 15:35:41 -0800514 deviceRules, err = fd.processUnicastFlow(ctx, path, inPortNo, outPortNo, flow)
515 if err != nil {
516 return nil, err
517 }
Neha Sharmabe485932020-05-25 21:52:55 +0000518 } else if grpID := fu.GetGroup(ctx, flow); grpID != 0 && flow.TableId == 0 { //Multicast
Girish Kumarf56a4682020-03-20 20:07:46 +0000519 logger.Debugw("process-multicast-flow", log.Fields{"flows": flow})
khenaidoo820197c2020-02-13 16:35:33 -0500520 deviceRules = fd.processMulticastFlow(ctx, path, inPortNo, outPortNo, flow, grpID, groupMap)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400521 } else {
khenaidoo820197c2020-02-13 16:35:33 -0500522 return deviceRules, status.Errorf(codes.Aborted, "unknown downstream flow %v", *flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400523 }
524 }
Neha Sharmabe485932020-05-25 21:52:55 +0000525 deviceRules, err = fd.updateOutputPortForControllerBoundFlowForParentDevide(ctx, flow, deviceRules)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800526 return deviceRules, err
khenaidoo89b0e942018-10-21 21:11:33 -0400527}