blob: 56e8ecc9bc2802cf7e64d20254184eb6370ca8d1 [file] [log] [blame]
Gamze Abaka52e35922021-03-12 10:48:34 +00001/*
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 */
16package meters
17
18import (
19 "context"
20 "fmt"
khenaidoo26721882021-08-11 17:42:52 -040021
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 Abaka52e35922021-03-12 10:48:34 +000025)
26
27// GetTrafficShapingInfo returns CIR,PIR and GIR values
28func 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}