1 // Copyright (c) 2017 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
22 "github.com/lunixbochs/struc"
25 // VnetInterfaceCounters is the input data type defined in the 'interface.api', with binary encoded Data field,
26 // that can be decoded into the InterfaceCounter or CombinedInterfaceCounter struct using this package.
27 type VnetInterfaceCounters struct {
31 Count uint32 `struc:"sizeof=Data"`
35 // CounterType is the basic counter type - contains only packet statistics.
38 // constants as defined in the vnet_interface_counter_type_t enum in 'vnet/interface.h'
51 // CombinedCounterType is the extended counter type - contains both packet and byte statistics.
52 type CombinedCounterType int
54 // constants as defined in the vnet_interface_counter_type_t enum in 'vnet/interface.h'
56 Rx CombinedCounterType = 0
60 // InterfaceCounter contains basic counter data (contains only packet statistics).
61 type InterfaceCounter struct {
67 // CombinedInterfaceCounter contains extended counter data (contains both packet and byte statistics).
68 type CombinedInterfaceCounter struct {
69 Type CombinedCounterType
75 type counterData struct {
80 Count uint32 `struc:"sizeof=Data"`
84 type combinedCounterData struct {
89 type combinedCounter struct {
90 Count uint32 `struc:"sizeof=Data"`
91 Data []combinedCounterData
94 // DecodeCounters decodes VnetInterfaceCounters struct content into the slice of InterfaceCounter structs.
95 func DecodeCounters(vnetCounters VnetInterfaceCounters) ([]InterfaceCounter, error) {
96 if vnetCounters.IsCombined == 1 {
97 return nil, errors.New("invalid argument - combined counter passed in")
100 // decode into internal struct
102 buf := bytes.NewReader(vnetCounters.Data)
103 err := struc.Unpack(buf, &c)
105 return nil, fmt.Errorf("unable to decode counter data: %v", err)
109 res := make([]InterfaceCounter, c.Count)
112 for i := uint32(0); i < c.Count; i++ {
113 res[i].Type = CounterType(vnetCounters.VnetCounterType)
114 res[i].SwIfIndex = vnetCounters.FirstSwIfIndex + i
115 res[i].Packets = c.Data[i].Packets
121 // DecodeCombinedCounters decodes VnetInterfaceCounters struct content into the slice of CombinedInterfaceCounter structs.
122 func DecodeCombinedCounters(vnetCounters VnetInterfaceCounters) ([]CombinedInterfaceCounter, error) {
123 if vnetCounters.IsCombined != 1 {
124 return nil, errors.New("invalid argument - simple counter passed in")
127 // decode into internal struct
128 var c combinedCounter
129 buf := bytes.NewReader(vnetCounters.Data)
130 err := struc.Unpack(buf, &c)
132 return nil, fmt.Errorf("unable to decode counter data: %v", err)
136 res := make([]CombinedInterfaceCounter, c.Count)
139 for i := uint32(0); i < c.Count; i++ {
140 res[i].Type = CombinedCounterType(vnetCounters.VnetCounterType)
141 res[i].SwIfIndex = vnetCounters.FirstSwIfIndex + i
142 res[i].Packets = c.Data[i].Packets
143 res[i].Bytes = c.Data[i].Bytes