2015-09-10 23:20:18 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
2018-04-09 20:40:06 +00:00
|
|
|
// Class interfaces for all classes
|
2015-09-10 23:20:18 +00:00
|
|
|
type Class interface {
|
|
|
|
Attrs() *ClassAttrs
|
|
|
|
Type() string
|
|
|
|
}
|
|
|
|
|
2018-03-08 03:09:28 +00:00
|
|
|
// Generic networking statistics for netlink users.
|
|
|
|
// This file contains "gnet_" prefixed structs and relevant functions.
|
|
|
|
// See Documentation/networking/getn_stats.txt in Linux source code for more details.
|
|
|
|
|
2018-04-09 20:40:06 +00:00
|
|
|
// GnetStatsBasic Ref: struct gnet_stats_basic { ... }
|
2018-03-08 03:09:28 +00:00
|
|
|
type GnetStatsBasic struct {
|
|
|
|
Bytes uint64 // number of seen bytes
|
|
|
|
Packets uint32 // number of seen packets
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:40:06 +00:00
|
|
|
// GnetStatsRateEst Ref: struct gnet_stats_rate_est { ... }
|
2018-03-08 03:09:28 +00:00
|
|
|
type GnetStatsRateEst struct {
|
|
|
|
Bps uint32 // current byte rate
|
|
|
|
Pps uint32 // current packet rate
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:40:06 +00:00
|
|
|
// GnetStatsRateEst64 Ref: struct gnet_stats_rate_est64 { ... }
|
2018-03-08 03:09:28 +00:00
|
|
|
type GnetStatsRateEst64 struct {
|
|
|
|
Bps uint64 // current byte rate
|
|
|
|
Pps uint64 // current packet rate
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:40:06 +00:00
|
|
|
// GnetStatsQueue Ref: struct gnet_stats_queue { ... }
|
2018-03-08 03:09:28 +00:00
|
|
|
type GnetStatsQueue struct {
|
|
|
|
Qlen uint32 // queue length
|
|
|
|
Backlog uint32 // backlog size of queue
|
|
|
|
Drops uint32 // number of dropped packets
|
|
|
|
Requeues uint32 // number of requues
|
|
|
|
Overlimits uint32 // number of enqueues over the limit
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:55:56 +00:00
|
|
|
// ClassStatistics representation based on generic networking statistics for netlink.
|
2018-03-08 03:09:28 +00:00
|
|
|
// See Documentation/networking/gen_stats.txt in Linux source code for more details.
|
|
|
|
type ClassStatistics struct {
|
|
|
|
Basic *GnetStatsBasic
|
|
|
|
Queue *GnetStatsQueue
|
|
|
|
RateEst *GnetStatsRateEst
|
2023-07-27 06:37:55 +00:00
|
|
|
BasicHw *GnetStatsBasic // Hardward statistics added in kernel 4.20
|
2018-03-08 03:09:28 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 20:40:06 +00:00
|
|
|
// NewClassStatistics Construct a ClassStatistics struct which fields are all initialized by 0.
|
2018-03-08 03:09:28 +00:00
|
|
|
func NewClassStatistics() *ClassStatistics {
|
|
|
|
return &ClassStatistics{
|
|
|
|
Basic: &GnetStatsBasic{},
|
|
|
|
Queue: &GnetStatsQueue{},
|
|
|
|
RateEst: &GnetStatsRateEst{},
|
2023-07-27 06:37:55 +00:00
|
|
|
BasicHw: &GnetStatsBasic{},
|
2018-03-08 03:09:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:12:26 +00:00
|
|
|
// ClassAttrs represents a netlink class. A filter is associated with a link,
|
2015-09-10 23:20:18 +00:00
|
|
|
// has a handle and a parent. The root filter of a device should have a
|
|
|
|
// parent == HANDLE_ROOT.
|
|
|
|
type ClassAttrs struct {
|
2018-03-08 03:09:28 +00:00
|
|
|
LinkIndex int
|
|
|
|
Handle uint32
|
|
|
|
Parent uint32
|
|
|
|
Leaf uint32
|
|
|
|
Statistics *ClassStatistics
|
2015-09-10 23:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (q ClassAttrs) String() string {
|
2016-03-20 00:12:26 +00:00
|
|
|
return fmt.Sprintf("{LinkIndex: %d, Handle: %s, Parent: %s, Leaf: %d}", q.LinkIndex, HandleStr(q.Handle), HandleStr(q.Parent), q.Leaf)
|
2015-09-10 23:20:18 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 20:40:06 +00:00
|
|
|
// HtbClassAttrs stores the attributes of HTB class
|
2015-09-10 23:20:18 +00:00
|
|
|
type HtbClassAttrs struct {
|
|
|
|
// TODO handle all attributes
|
|
|
|
Rate uint64
|
|
|
|
Ceil uint64
|
|
|
|
Buffer uint32
|
|
|
|
Cbuffer uint32
|
|
|
|
Quantum uint32
|
|
|
|
Level uint32
|
|
|
|
Prio uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q HtbClassAttrs) String() string {
|
|
|
|
return fmt.Sprintf("{Rate: %d, Ceil: %d, Buffer: %d, Cbuffer: %d}", q.Rate, q.Ceil, q.Buffer, q.Cbuffer)
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:12:26 +00:00
|
|
|
// HtbClass represents an Htb class
|
2015-09-10 23:20:18 +00:00
|
|
|
type HtbClass struct {
|
|
|
|
ClassAttrs
|
2016-01-14 05:48:14 +00:00
|
|
|
Rate uint64
|
|
|
|
Ceil uint64
|
2015-09-10 23:20:18 +00:00
|
|
|
Buffer uint32
|
|
|
|
Cbuffer uint32
|
|
|
|
Quantum uint32
|
|
|
|
Level uint32
|
|
|
|
Prio uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q HtbClass) String() string {
|
|
|
|
return fmt.Sprintf("{Rate: %d, Ceil: %d, Buffer: %d, Cbuffer: %d}", q.Rate, q.Ceil, q.Buffer, q.Cbuffer)
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:40:06 +00:00
|
|
|
// Attrs returns the class attributes
|
2016-03-20 00:12:26 +00:00
|
|
|
func (q *HtbClass) Attrs() *ClassAttrs {
|
|
|
|
return &q.ClassAttrs
|
2015-09-10 23:20:18 +00:00
|
|
|
}
|
|
|
|
|
2018-04-09 20:40:06 +00:00
|
|
|
// Type return the class type
|
2016-03-20 00:12:26 +00:00
|
|
|
func (q *HtbClass) Type() string {
|
2015-09-10 23:20:18 +00:00
|
|
|
return "htb"
|
|
|
|
}
|
|
|
|
|
|
|
|
// GenericClass classes represent types that are not currently understood
|
|
|
|
// by this netlink library.
|
|
|
|
type GenericClass struct {
|
|
|
|
ClassAttrs
|
|
|
|
ClassType string
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:40:06 +00:00
|
|
|
// Attrs return the class attributes
|
2015-09-10 23:20:18 +00:00
|
|
|
func (class *GenericClass) Attrs() *ClassAttrs {
|
|
|
|
return &class.ClassAttrs
|
|
|
|
}
|
|
|
|
|
2019-03-18 19:55:56 +00:00
|
|
|
// Type return the class type
|
2015-09-10 23:20:18 +00:00
|
|
|
func (class *GenericClass) Type() string {
|
|
|
|
return class.ClassType
|
|
|
|
}
|
2018-04-09 20:40:06 +00:00
|
|
|
|
2018-08-19 10:37:29 +00:00
|
|
|
// ServiceCurve is a nondecreasing function of some time unit, returning the amount of service
|
|
|
|
// (an allowed or allocated amount of bandwidth) at some specific point in time. The purpose of it
|
|
|
|
// should be subconsciously obvious: if a class was allowed to transfer not less than the amount
|
|
|
|
// specified by its service curve, then the service curve is not violated.
|
2018-04-09 20:40:06 +00:00
|
|
|
type ServiceCurve struct {
|
|
|
|
m1 uint32
|
|
|
|
d uint32
|
|
|
|
m2 uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attrs return the parameters of the service curve
|
|
|
|
func (c *ServiceCurve) Attrs() (uint32, uint32, uint32) {
|
|
|
|
return c.m1, c.d, c.m2
|
|
|
|
}
|
|
|
|
|
2018-08-19 10:37:29 +00:00
|
|
|
// Burst returns the burst rate (m1) of the curve
|
|
|
|
func (c *ServiceCurve) Burst() uint32 {
|
|
|
|
return c.m1
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delay return the delay (d) of the curve
|
|
|
|
func (c *ServiceCurve) Delay() uint32 {
|
|
|
|
return c.d
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rate returns the rate (m2) of the curve
|
|
|
|
func (c *ServiceCurve) Rate() uint32 {
|
|
|
|
return c.m2
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:40:06 +00:00
|
|
|
// HfscClass is a representation of the HFSC class
|
|
|
|
type HfscClass struct {
|
|
|
|
ClassAttrs
|
|
|
|
Rsc ServiceCurve
|
|
|
|
Fsc ServiceCurve
|
|
|
|
Usc ServiceCurve
|
|
|
|
}
|
|
|
|
|
2018-08-19 10:37:29 +00:00
|
|
|
// SetUsc sets the USC curve. The bandwidth (m1 and m2) is specified in bits and the delay in
|
|
|
|
// seconds.
|
2018-04-09 20:40:06 +00:00
|
|
|
func (hfsc *HfscClass) SetUsc(m1 uint32, d uint32, m2 uint32) {
|
2018-08-19 10:37:29 +00:00
|
|
|
hfsc.Usc = ServiceCurve{m1: m1, d: d, m2: m2}
|
2018-04-09 20:40:06 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 10:37:29 +00:00
|
|
|
// SetFsc sets the Fsc curve. The bandwidth (m1 and m2) is specified in bits and the delay in
|
|
|
|
// seconds.
|
2018-04-09 20:40:06 +00:00
|
|
|
func (hfsc *HfscClass) SetFsc(m1 uint32, d uint32, m2 uint32) {
|
2018-08-19 10:37:29 +00:00
|
|
|
hfsc.Fsc = ServiceCurve{m1: m1, d: d, m2: m2}
|
2018-04-09 20:40:06 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 10:37:29 +00:00
|
|
|
// SetRsc sets the Rsc curve. The bandwidth (m1 and m2) is specified in bits and the delay in
|
|
|
|
// seconds.
|
2018-04-09 20:40:06 +00:00
|
|
|
func (hfsc *HfscClass) SetRsc(m1 uint32, d uint32, m2 uint32) {
|
2018-08-19 10:37:29 +00:00
|
|
|
hfsc.Rsc = ServiceCurve{m1: m1, d: d, m2: m2}
|
2018-04-09 20:40:06 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 10:37:29 +00:00
|
|
|
// SetSC implements the SC from the `tc` CLI. This function behaves the same as if one would set the
|
|
|
|
// USC through the `tc` command-line tool. This means bandwidth (m1 and m2) is specified in bits and
|
|
|
|
// the delay in ms.
|
2018-04-09 20:40:06 +00:00
|
|
|
func (hfsc *HfscClass) SetSC(m1 uint32, d uint32, m2 uint32) {
|
2018-08-19 10:37:29 +00:00
|
|
|
hfsc.SetRsc(m1, d, m2)
|
|
|
|
hfsc.SetFsc(m1, d, m2)
|
2018-04-09 20:40:06 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 10:37:29 +00:00
|
|
|
// SetUL implements the UL from the `tc` CLI. This function behaves the same as if one would set the
|
|
|
|
// USC through the `tc` command-line tool. This means bandwidth (m1 and m2) is specified in bits and
|
|
|
|
// the delay in ms.
|
2018-04-09 20:40:06 +00:00
|
|
|
func (hfsc *HfscClass) SetUL(m1 uint32, d uint32, m2 uint32) {
|
2018-08-19 10:37:29 +00:00
|
|
|
hfsc.SetUsc(m1, d, m2)
|
2018-04-09 20:40:06 +00:00
|
|
|
}
|
|
|
|
|
2018-08-19 10:37:29 +00:00
|
|
|
// SetLS implements the LS from the `tc` CLI. This function behaves the same as if one would set the
|
|
|
|
// USC through the `tc` command-line tool. This means bandwidth (m1 and m2) is specified in bits and
|
|
|
|
// the delay in ms.
|
2018-04-09 20:40:06 +00:00
|
|
|
func (hfsc *HfscClass) SetLS(m1 uint32, d uint32, m2 uint32) {
|
2018-08-19 10:37:29 +00:00
|
|
|
hfsc.SetFsc(m1, d, m2)
|
2018-04-09 20:40:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewHfscClass returns a new HFSC struct with the set parameters
|
|
|
|
func NewHfscClass(attrs ClassAttrs) *HfscClass {
|
|
|
|
return &HfscClass{
|
|
|
|
ClassAttrs: attrs,
|
|
|
|
Rsc: ServiceCurve{},
|
|
|
|
Fsc: ServiceCurve{},
|
|
|
|
Usc: ServiceCurve{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-19 10:37:29 +00:00
|
|
|
// String() returns a string that contains the information and attributes of the HFSC class
|
2018-04-09 20:40:06 +00:00
|
|
|
func (hfsc *HfscClass) String() string {
|
|
|
|
return fmt.Sprintf(
|
|
|
|
"{%s -- {RSC: {m1=%d d=%d m2=%d}} {FSC: {m1=%d d=%d m2=%d}} {USC: {m1=%d d=%d m2=%d}}}",
|
|
|
|
hfsc.Attrs(), hfsc.Rsc.m1*8, hfsc.Rsc.d, hfsc.Rsc.m2*8, hfsc.Fsc.m1*8, hfsc.Fsc.d, hfsc.Fsc.m2*8, hfsc.Usc.m1*8, hfsc.Usc.d, hfsc.Usc.m2*8,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attrs return the Hfsc parameters
|
|
|
|
func (hfsc *HfscClass) Attrs() *ClassAttrs {
|
|
|
|
return &hfsc.ClassAttrs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type return the type of the class
|
|
|
|
func (hfsc *HfscClass) Type() string {
|
|
|
|
return "hfsc"
|
|
|
|
}
|