Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package device |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "fmt" |
| 22 | |
Maninder | dfadc98 | 2020-10-28 14:04:33 +0530 | [diff] [blame^] | 23 | fu "github.com/opencord/voltha-lib-go/v4/pkg/flows" |
| 24 | "github.com/opencord/voltha-lib-go/v4/pkg/log" |
| 25 | ofp "github.com/opencord/voltha-protos/v4/go/openflow_13" |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 26 | ) |
| 27 | |
| 28 | // GetMeterConfig returns meters which which are used by the given flows |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 29 | func (agent *LogicalAgent) GetMeterConfig(ctx context.Context, flows map[uint64]*ofp.OfpFlowStats) (map[uint32]*ofp.OfpMeterConfig, error) { |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 30 | metersConfig := make(map[uint32]*ofp.OfpMeterConfig) |
| 31 | for _, flow := range flows { |
| 32 | if flowMeterID := fu.GetMeterIdFromFlow(flow); flowMeterID != 0 { |
| 33 | if _, have := metersConfig[flowMeterID]; !have { |
| 34 | // Meter is present in the flow, Get from logical device |
| 35 | meterHandle, have := agent.meterLoader.Lock(flowMeterID) |
| 36 | if !have { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 37 | logger.Errorw(ctx, "Meter-referred-by-flow-is-not-found-in-logicaldevice", |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 38 | log.Fields{"meterID": flowMeterID, "Available-meters": metersConfig, "flow": *flow}) |
| 39 | return nil, fmt.Errorf("Meter-referred-by-flow-is-not-found-in-logicaldevice.MeterId-%d", flowMeterID) |
| 40 | } |
| 41 | |
| 42 | meter := meterHandle.GetReadOnly() |
| 43 | metersConfig[flowMeterID] = meter.Config |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 44 | logger.Debugw(ctx, "Found meter in logical device", log.Fields{"meterID": flowMeterID, "meter-band": meter.Config}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 45 | |
| 46 | meterHandle.Unlock() |
| 47 | } |
| 48 | } |
| 49 | } |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 50 | logger.Debugw(ctx, "meter-bands-for-flows", log.Fields{"flows": len(flows), "meters": metersConfig}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 51 | return metersConfig, nil |
| 52 | } |
| 53 | |
| 54 | // updateFlowCountOfMeterStats updates the number of flows associated with this meter |
| 55 | func (agent *LogicalAgent) updateFlowCountOfMeterStats(ctx context.Context, modCommand *ofp.OfpFlowMod, flow *ofp.OfpFlowStats, revertUpdate bool) bool { |
| 56 | flowCommand := modCommand.GetCommand() |
| 57 | meterID := fu.GetMeterIdFromFlow(flow) |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 58 | logger.Debugw(ctx, "Meter-id-in-flow-mod", log.Fields{"meterId": meterID}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 59 | if meterID == 0 { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 60 | logger.Debugw(ctx, "No-meter-present-in-the-flow", log.Fields{"flow": *flow}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 61 | return true |
| 62 | } |
| 63 | |
| 64 | if flowCommand != ofp.OfpFlowModCommand_OFPFC_ADD && flowCommand != ofp.OfpFlowModCommand_OFPFC_DELETE_STRICT { |
| 65 | return true |
| 66 | } |
| 67 | |
| 68 | meterHandle, have := agent.meterLoader.Lock(meterID) |
| 69 | if !have { |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 70 | logger.Debugw(ctx, "Meter-is-not-present-in-logical-device", log.Fields{"meterID": meterID}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 71 | return true |
| 72 | } |
| 73 | defer meterHandle.Unlock() |
| 74 | |
| 75 | oldMeter := meterHandle.GetReadOnly() |
| 76 | // avoiding using proto.Clone by only copying what have changed (this assumes that the oldMeter will never be modified) |
| 77 | newStats := *oldMeter.Stats |
| 78 | if flowCommand == ofp.OfpFlowModCommand_OFPFC_ADD { |
| 79 | if revertUpdate { |
| 80 | newStats.FlowCount-- |
| 81 | } else { |
| 82 | newStats.FlowCount++ |
| 83 | } |
| 84 | } else if flowCommand == ofp.OfpFlowModCommand_OFPFC_DELETE_STRICT { |
| 85 | if revertUpdate { |
| 86 | newStats.FlowCount++ |
| 87 | } else { |
| 88 | newStats.FlowCount-- |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | newMeter := &ofp.OfpMeterEntry{ |
| 93 | Config: oldMeter.Config, |
| 94 | Stats: &newStats, |
| 95 | } |
| 96 | if err := meterHandle.Update(ctx, newMeter); err != nil { |
divyadesai | cb8b59d | 2020-08-18 09:55:47 +0000 | [diff] [blame] | 97 | logger.Debugw(ctx, "unable-to-update-meter-in-db", log.Fields{"logical-device-id": agent.logicalDeviceID, "meterID": meterID}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 98 | return false |
| 99 | } |
| 100 | |
Rohan Agrawal | 31f2180 | 2020-06-12 05:38:46 +0000 | [diff] [blame] | 101 | logger.Debugw(ctx, "updated-meter-flow-stats", log.Fields{"meterId": meterID}) |
Kent Hagerman | 433a31a | 2020-05-20 19:04:48 -0400 | [diff] [blame] | 102 | return true |
| 103 | } |