Gamze Abaka | 52e3592 | 2021-03-12 10:48:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019-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 | package meters |
| 17 | |
| 18 | import ( |
| 19 | "context" |
| 20 | "fmt" |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 21 | |
| 22 | "github.com/opencord/voltha-lib-go/v7/pkg/log" |
| 23 | ofp "github.com/opencord/voltha-protos/v5/go/openflow_13" |
| 24 | tp_pb "github.com/opencord/voltha-protos/v5/go/tech_profile" |
Gamze Abaka | 52e3592 | 2021-03-12 10:48:34 +0000 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | // GetTrafficShapingInfo returns CIR,PIR and GIR values |
| 28 | func GetTrafficShapingInfo(ctx context.Context, meterConfig *ofp.OfpMeterConfig) (*tp_pb.TrafficShapingInfo, error) { |
| 29 | switch meterBandSize := len(meterConfig.Bands); { |
| 30 | case meterBandSize == 1: |
| 31 | band := meterConfig.Bands[0] |
| 32 | if band.BurstSize == 0 { // GIR, tcont type 1 |
| 33 | return &tp_pb.TrafficShapingInfo{Gir: band.Rate}, nil |
| 34 | } |
| 35 | return &tp_pb.TrafficShapingInfo{Pir: band.Rate, Pbs: band.BurstSize}, nil // PIR, tcont type 4 |
| 36 | case meterBandSize == 2: |
| 37 | firstBand, secondBand := meterConfig.Bands[0], meterConfig.Bands[1] |
| 38 | if firstBand.BurstSize == 0 && secondBand.BurstSize == 0 && |
| 39 | firstBand.Rate == secondBand.Rate { // PIR == GIR, tcont type 1 |
| 40 | return &tp_pb.TrafficShapingInfo{Pir: firstBand.Rate, Gir: secondBand.Rate}, nil |
| 41 | } |
| 42 | if firstBand.BurstSize > 0 && secondBand.BurstSize > 0 { // PIR, CIR, tcont type 2 or 3 |
| 43 | if firstBand.Rate > secondBand.Rate { // always PIR >= CIR |
| 44 | return &tp_pb.TrafficShapingInfo{Pir: firstBand.Rate, Pbs: firstBand.BurstSize, Cir: secondBand.Rate, Cbs: secondBand.BurstSize}, nil |
| 45 | } |
| 46 | return &tp_pb.TrafficShapingInfo{Pir: secondBand.Rate, Pbs: secondBand.BurstSize, Cir: firstBand.Rate, Cbs: firstBand.BurstSize}, nil |
| 47 | } |
| 48 | case meterBandSize == 3: // PIR,CIR,GIR, tcont type 5 |
| 49 | var count, girIndex int |
| 50 | for i, band := range meterConfig.Bands { |
| 51 | if band.BurstSize == 0 { // find GIR |
| 52 | count = count + 1 |
| 53 | girIndex = i |
| 54 | } |
| 55 | } |
| 56 | if count == 1 { |
| 57 | bands := make([]*ofp.OfpMeterBandHeader, len(meterConfig.Bands)) |
| 58 | copy(bands, meterConfig.Bands) |
| 59 | pirCirBands := append(bands[:girIndex], bands[girIndex+1:]...) |
| 60 | firstBand, secondBand := pirCirBands[0], pirCirBands[1] |
| 61 | if firstBand.Rate > secondBand.Rate { |
| 62 | return &tp_pb.TrafficShapingInfo{Pir: firstBand.Rate, Pbs: firstBand.BurstSize, Cir: secondBand.Rate, Cbs: secondBand.BurstSize, Gir: meterConfig.Bands[girIndex].Rate}, nil |
| 63 | } |
| 64 | return &tp_pb.TrafficShapingInfo{Pir: secondBand.Rate, Pbs: secondBand.BurstSize, Cir: firstBand.Rate, Cbs: firstBand.BurstSize, Gir: meterConfig.Bands[girIndex].Rate}, nil |
| 65 | } |
| 66 | default: |
| 67 | logger.Errorw(ctx, "invalid-meter-config", log.Fields{"meter-config": meterConfig}) |
| 68 | return nil, fmt.Errorf("invalid-meter-config: %v", meterConfig) |
| 69 | } |
| 70 | return nil, fmt.Errorf("invalid-meter-config: %v", meterConfig) |
| 71 | } |