blob: f16477fef84c5bc24c282e534a2d8097572959fb [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
40func NewFlowDecomposer(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) {
khenaidoo3306c992019-05-24 16:57:35 -040048 deviceRules := *fu.NewDeviceRules()
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 {
62 deviceRules.CreateEntryIfNotExist(deviceID)
63 deviceRules.Rules[deviceID].AddFrom(flowAndGroups)
64 devicesToUpdate[deviceID] = deviceID
khenaidoo89b0e942018-10-21 21:11:33 -040065 }
66 }
khenaidoo820197c2020-02-13 16:35:33 -050067 return deviceRules.FilterRules(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
71func (fd *FlowDecomposer) updateOutputPortForControllerBoundFlowForParentDevide(flow *ofp.OfpFlowStats,
Scott Bakerfdea1e32020-02-21 15:35:41 -080072 dr *fu.DeviceRules) (*fu.DeviceRules, error) {
khenaidoo68c930b2019-05-13 11:46:51 -040073 EAPOL := fu.EthType(0x888e)
74 IGMP := fu.IpProto(2)
75 UDP := fu.IpProto(17)
khenaidoo19d7b632018-10-30 10:49:50 -040076
77 newDeviceRules := dr.Copy()
78 // Check whether we are dealing with a parent device
npujar1d86a522019-11-14 17:11:16 +053079 for deviceID, fg := range dr.GetRules() {
80 if root, _ := fd.deviceMgr.IsRootDevice(deviceID); root {
81 newDeviceRules.ClearFlows(deviceID)
khenaidoo19d7b632018-10-30 10:49:50 -040082 for i := 0; i < fg.Flows.Len(); i++ {
83 f := fg.GetFlow(i)
84 UpdateOutPortNo := false
khenaidoo68c930b2019-05-13 11:46:51 -040085 for _, field := range fu.GetOfbFields(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 {
khenaidoo68c930b2019-05-13 11:46:51 -040094 f = fu.UpdateOutputPortByActionType(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 //}
npujar1d86a522019-11-14 17:11:16 +0530102 newDeviceRules.AddFlow(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})
khenaidood20a5852018-10-22 22:09:55 -0400115 deviceRules := fu.NewDeviceRules()
npujar1d86a522019-11-14 17:11:16 +0530116 meterID := fu.GetMeterIdFromFlow(flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400117 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(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
khenaidoo820197c2020-02-13 16:35:33 -0500123 if agent.GetDeviceRoutes().IsRootPort(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{
130 fu.InPort(egressHop.Egress),
131 },
132 Actions: fu.GetActions(flow),
133 }
134 // Augment the matchfields with the ofpfields from the flow
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400135 fg := fu.NewFlowsAndGroups()
Humera Kouser4ff89012019-08-25 19:01:51 -0400136 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
Scott Bakerfdea1e32020-02-21 15:35:41 -0800137 fs, err := fu.MkFlowStat(fa)
138 if err != nil {
139 return nil, err
140 }
141 fg.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400142 deviceRules.AddFlowsAndGroup(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 {
khenaidood20a5852018-10-22 22:09:55 -0400150 inPorts = agent.GetWildcardInputPorts(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{
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 }
166 // Augment the matchfields with the ofpfields from the flow
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400167 faParent.MatchFields = append(faParent.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
168 fgParent := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800169 fs, err := fu.MkFlowStat(faParent)
170 if err != nil {
171 return nil, err
172 }
173 fgParent.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400174 deviceRules.AddFlowsAndGroup(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
179 setvid := fu.GetVlanVid(flow)
180 if setvid != nil {
181 // have this child push the vlan the parent is matching/trapping on above
182 actions = []*ofp.OfpAction{
183 fu.PushVlan(0x8100),
184 fu.SetField(fu.VlanVid(*setvid)),
185 fu.Output(ingressHop.Egress),
186 }
187 } else {
188 // otherwise just set the egress port
189 actions = []*ofp.OfpAction{
190 fu.Output(ingressHop.Egress),
191 }
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{
196 fu.InPort(ingressHop.Ingress),
197 fu.TunnelId(uint64(inputPort)),
198 },
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 {
205 faChild.MatchFields = append(faChild.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT, fu.VLAN_VID)...)
206 } else {
207 faChild.MatchFields = append(faChild.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
208 }
209 fgChild := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800210 fs, err = fu.MkFlowStat(faChild)
211 if err != nil {
212 return nil, err
213 }
214 fgChild.AddFlow(fs)
Matt Jeanneretb423bad2019-10-10 20:42:19 -0400215 deviceRules.AddFlowsAndGroup(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})
khenaidood20a5852018-10-22 22:09:55 -0400231 deviceRules := fu.NewDeviceRules()
232
npujar1d86a522019-11-14 17:11:16 +0530233 meterID := fu.GetMeterIdFromFlow(flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400234 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(flow)
235
khenaidoo820197c2020-02-13 16:35:33 -0500236 ingressHop := path[0]
237 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400238
Manikkaraj kb1a10922019-07-29 12:10:34 -0400239 if flow.TableId == 0 && fu.HasNextTable(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{
khenaidoo68c930b2019-05-13 11:46:51 -0400248 fu.InPort(ingressHop.Ingress),
249 fu.TunnelId(uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400250 },
khenaidoo68c930b2019-05-13 11:46:51 -0400251 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400252 }
253 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400254 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400255
256 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400257 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400258
khenaidoo89b0e942018-10-21 21:11:33 -0400259 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800260 fs, err := fu.MkFlowStat(fa)
261 if err != nil {
262 return nil, err
263 }
264 fg.AddFlow(fs)
khenaidood20a5852018-10-22 22:09:55 -0400265 deviceRules.AddFlowsAndGroup(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{
271 fu.InPort(egressHop.Ingress),
272 fu.TunnelId(uint64(inPortNo)),
273 },
khenaidoo89b0e942018-10-21 21:11:33 -0400274 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400275 // Augment the matchfields with the ofpfields from the flow
276 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidoo89b0e942018-10-21 21:11:33 -0400277
Manikkaraj kb1a10922019-07-29 12:10:34 -0400278 //Augment the actions
279 filteredAction := fu.GetActions(flow, fu.OUTPUT)
280 filteredAction = append(filteredAction, fu.Output(egressHop.Egress))
281 fa.Actions = filteredAction
khenaidood20a5852018-10-22 22:09:55 -0400282
Manikkaraj kb1a10922019-07-29 12:10:34 -0400283 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800284 fs, err := fu.MkFlowStat(fa)
285 if err != nil {
286 return nil, err
287 }
288 fg.AddFlow(fs)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400289 deviceRules.AddFlowsAndGroup(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})
khenaidood20a5852018-10-22 22:09:55 -0400298 deviceRules := fu.NewDeviceRules()
npujar1d86a522019-11-14 17:11:16 +0530299 meterID := fu.GetMeterIdFromFlow(flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400300 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(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})
Manikkaraj kb1a10922019-07-29 12:10:34 -0400316 portNumber := fu.GetEgressPortNumberFromWriteMetadata(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 }
khenaidoo68c930b2019-05-13 11:46:51 -0400336 innerTag := fu.GetInnerTagFromMetaData(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{
khenaidoo68c930b2019-05-13 11:46:51 -0400345 fu.InPort(ingressHop.Ingress),
Manikkaraj kb1a10922019-07-29 12:10:34 -0400346 fu.Metadata_ofp(uint64(innerTag)),
khenaidoo68c930b2019-05-13 11:46:51 -0400347 fu.TunnelId(uint64(portNumber)),
khenaidoo89b0e942018-10-21 21:11:33 -0400348 },
khenaidoo68c930b2019-05-13 11:46:51 -0400349 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400350 }
351 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400352 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT, fu.METADATA)...)
khenaidood20a5852018-10-22 22:09:55 -0400353
354 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400355 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400356
khenaidoo89b0e942018-10-21 21:11:33 -0400357 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800358 fs, err := fu.MkFlowStat(fa)
359 if err != nil {
360 return nil, err
361 }
362 fg.AddFlow(fs)
khenaidoo89b0e942018-10-21 21:11:33 -0400363 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
364 } 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{
khenaidoo68c930b2019-05-13 11:46:51 -0400369 fu.InPort(ingressHop.Ingress),
370 fu.TunnelId(uint64(inPortNo)),
khenaidoo89b0e942018-10-21 21:11:33 -0400371 },
khenaidoo68c930b2019-05-13 11:46:51 -0400372 Actions: fu.GetActions(flow),
khenaidoo89b0e942018-10-21 21:11:33 -0400373 }
374 // Augment the matchfields with the ofpfields from the flow
khenaidoo68c930b2019-05-13 11:46:51 -0400375 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400376
377 // Augment the Actions
khenaidoo68c930b2019-05-13 11:46:51 -0400378 fa.Actions = append(fa.Actions, fu.Output(ingressHop.Egress))
khenaidood20a5852018-10-22 22:09:55 -0400379
khenaidoo89b0e942018-10-21 21:11:33 -0400380 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800381 fs, err := fu.MkFlowStat(fa)
382 if err != nil {
383 return nil, err
384 }
385 fg.AddFlow(fs)
khenaidoo89b0e942018-10-21 21:11:33 -0400386 deviceRules.AddFlowsAndGroup(ingressHop.DeviceID, fg)
387 }
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})
khenaidood20a5852018-10-22 22:09:55 -0400397 deviceRules := fu.NewDeviceRules()
398
khenaidoo820197c2020-02-13 16:35:33 -0500399 egressHop := path[1]
khenaidood20a5852018-10-22 22:09:55 -0400400
npujar1d86a522019-11-14 17:11:16 +0530401 meterID := fu.GetMeterIdFromFlow(flow)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400402 metadataFromwriteMetadata := fu.GetMetadataFromWriteMetadataAction(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{
406 fu.InPort(egressHop.Ingress),
407 },
khenaidoo89b0e942018-10-21 21:11:33 -0400408 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400409 // Augment the matchfields with the ofpfields from the flow
410 fa.MatchFields = append(fa.MatchFields, fu.GetOfbFields(flow, fu.IN_PORT)...)
khenaidood20a5852018-10-22 22:09:55 -0400411
Manikkaraj kb1a10922019-07-29 12:10:34 -0400412 // Augment the Actions
413 filteredAction := fu.GetActions(flow, fu.OUTPUT)
414 filteredAction = append(filteredAction, fu.Output(egressHop.Egress))
415 fa.Actions = filteredAction
khenaidoo89b0e942018-10-21 21:11:33 -0400416
Manikkaraj kb1a10922019-07-29 12:10:34 -0400417 fg := fu.NewFlowsAndGroups()
Scott Bakerfdea1e32020-02-21 15:35:41 -0800418 fs, err := fu.MkFlowStat(fa)
419 if err != nil {
420 return nil, err
421 }
422 fg.AddFlow(fs)
Manikkaraj kb1a10922019-07-29 12:10:34 -0400423 deviceRules.AddFlowsAndGroup(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})
khenaidood20a5852018-10-22 22:09:55 -0400433 deviceRules := fu.NewDeviceRules()
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
khenaidoo820197c2020-02-13 16:35:33 -0500447 deviceRules.CreateEntryIfNotExist(path[0].DeviceID)
Esin Karaman09959ae2019-11-29 13:59:58 +0000448 fg := fu.NewFlowsAndGroups()
449 fg.AddFlow(flow)
450 //return the multicast flow without decomposing it
khenaidoo820197c2020-02-13 16:35:33 -0500451 deviceRules.AddFlowsAndGroup(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
khenaidoo68c930b2019-05-13 11:46:51 -0400459 inPortNo := fu.GetInPort(flow)
Esin Karaman09959ae2019-11-29 13:59:58 +0000460 if fu.HasGroup(flow) && inPortNo == 0 {
461 //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
Esin Karaman09959ae2019-11-29 13:59:58 +0000463 nniPorts := agent.GetNNIPorts()
464 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 }
khenaidoo68c930b2019-05-13 11:46:51 -0400469 outPortNo := fu.GetOutPort(flow)
khenaidoo89b0e942018-10-21 21:11:33 -0400470 deviceRules := fu.NewDeviceRules()
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 }
Manikkaraj kb1a10922019-07-29 12:10:34 -0400506 } else if fu.HasNextTable(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 }
npujar1d86a522019-11-14 17:11:16 +0530518 } else if grpID := fu.GetGroup(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 }
Scott Bakerfdea1e32020-02-21 15:35:41 -0800525 deviceRules, err = fd.updateOutputPortForControllerBoundFlowForParentDevide(flow, deviceRules)
526 return deviceRules, err
khenaidoo89b0e942018-10-21 21:11:33 -0400527}