github.com/vishvananda/netlink@v1.3.0/class.go (about)

     1  package netlink
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  // Class interfaces for all classes
     8  type Class interface {
     9  	Attrs() *ClassAttrs
    10  	Type() string
    11  }
    12  
    13  // Generic networking statistics for netlink users.
    14  // This file contains "gnet_" prefixed structs and relevant functions.
    15  // See Documentation/networking/getn_stats.txt in Linux source code for more details.
    16  
    17  // GnetStatsBasic Ref: struct gnet_stats_basic { ... }
    18  type GnetStatsBasic struct {
    19  	Bytes   uint64 // number of seen bytes
    20  	Packets uint32 // number of seen packets
    21  }
    22  
    23  // GnetStatsRateEst Ref: struct gnet_stats_rate_est { ... }
    24  type GnetStatsRateEst struct {
    25  	Bps uint32 // current byte rate
    26  	Pps uint32 // current packet rate
    27  }
    28  
    29  // GnetStatsRateEst64 Ref: struct gnet_stats_rate_est64 { ... }
    30  type GnetStatsRateEst64 struct {
    31  	Bps uint64 // current byte rate
    32  	Pps uint64 // current packet rate
    33  }
    34  
    35  // GnetStatsQueue Ref: struct gnet_stats_queue { ... }
    36  type GnetStatsQueue struct {
    37  	Qlen       uint32 // queue length
    38  	Backlog    uint32 // backlog size of queue
    39  	Drops      uint32 // number of dropped packets
    40  	Requeues   uint32 // number of requues
    41  	Overlimits uint32 // number of enqueues over the limit
    42  }
    43  
    44  // ClassStatistics representation based on generic networking statistics for netlink.
    45  // See Documentation/networking/gen_stats.txt in Linux source code for more details.
    46  type ClassStatistics struct {
    47  	Basic   *GnetStatsBasic
    48  	Queue   *GnetStatsQueue
    49  	RateEst *GnetStatsRateEst
    50  	BasicHw *GnetStatsBasic // Hardward statistics added in kernel 4.20
    51  }
    52  
    53  // NewClassStatistics Construct a ClassStatistics struct which fields are all initialized by 0.
    54  func NewClassStatistics() *ClassStatistics {
    55  	return &ClassStatistics{
    56  		Basic:   &GnetStatsBasic{},
    57  		Queue:   &GnetStatsQueue{},
    58  		RateEst: &GnetStatsRateEst{},
    59  		BasicHw: &GnetStatsBasic{},
    60  	}
    61  }
    62  
    63  // ClassAttrs represents a netlink class. A filter is associated with a link,
    64  // has a handle and a parent. The root filter of a device should have a
    65  // parent == HANDLE_ROOT.
    66  type ClassAttrs struct {
    67  	LinkIndex  int
    68  	Handle     uint32
    69  	Parent     uint32
    70  	Leaf       uint32
    71  	Statistics *ClassStatistics
    72  }
    73  
    74  func (q ClassAttrs) String() string {
    75  	return fmt.Sprintf("{LinkIndex: %d, Handle: %s, Parent: %s, Leaf: %d}", q.LinkIndex, HandleStr(q.Handle), HandleStr(q.Parent), q.Leaf)
    76  }
    77  
    78  // HtbClassAttrs stores the attributes of HTB class
    79  type HtbClassAttrs struct {
    80  	// TODO handle all attributes
    81  	Rate    uint64
    82  	Ceil    uint64
    83  	Buffer  uint32
    84  	Cbuffer uint32
    85  	Quantum uint32
    86  	Level   uint32
    87  	Prio    uint32
    88  }
    89  
    90  func (q HtbClassAttrs) String() string {
    91  	return fmt.Sprintf("{Rate: %d, Ceil: %d, Buffer: %d, Cbuffer: %d}", q.Rate, q.Ceil, q.Buffer, q.Cbuffer)
    92  }
    93  
    94  // HtbClass represents an Htb class
    95  type HtbClass struct {
    96  	ClassAttrs
    97  	Rate    uint64
    98  	Ceil    uint64
    99  	Buffer  uint32
   100  	Cbuffer uint32
   101  	Quantum uint32
   102  	Level   uint32
   103  	Prio    uint32
   104  }
   105  
   106  func (q HtbClass) String() string {
   107  	return fmt.Sprintf("{Rate: %d, Ceil: %d, Buffer: %d, Cbuffer: %d}", q.Rate, q.Ceil, q.Buffer, q.Cbuffer)
   108  }
   109  
   110  // Attrs returns the class attributes
   111  func (q *HtbClass) Attrs() *ClassAttrs {
   112  	return &q.ClassAttrs
   113  }
   114  
   115  // Type return the class type
   116  func (q *HtbClass) Type() string {
   117  	return "htb"
   118  }
   119  
   120  // GenericClass classes represent types that are not currently understood
   121  // by this netlink library.
   122  type GenericClass struct {
   123  	ClassAttrs
   124  	ClassType string
   125  }
   126  
   127  // Attrs return the class attributes
   128  func (class *GenericClass) Attrs() *ClassAttrs {
   129  	return &class.ClassAttrs
   130  }
   131  
   132  // Type return the class type
   133  func (class *GenericClass) Type() string {
   134  	return class.ClassType
   135  }
   136  
   137  // ServiceCurve is a nondecreasing function of some time unit, returning the amount of service
   138  // (an allowed or allocated amount of bandwidth) at some specific point in time. The purpose of it
   139  // should be subconsciously obvious: if a class was allowed to transfer not less than the amount
   140  // specified by its service curve, then the service curve is not violated.
   141  type ServiceCurve struct {
   142  	m1 uint32
   143  	d  uint32
   144  	m2 uint32
   145  }
   146  
   147  // Attrs return the parameters of the service curve
   148  func (c *ServiceCurve) Attrs() (uint32, uint32, uint32) {
   149  	return c.m1, c.d, c.m2
   150  }
   151  
   152  // Burst returns the burst rate (m1) of the curve
   153  func (c *ServiceCurve) Burst() uint32 {
   154  	return c.m1
   155  }
   156  
   157  // Delay return the delay (d) of the curve
   158  func (c *ServiceCurve) Delay() uint32 {
   159  	return c.d
   160  }
   161  
   162  // Rate returns the rate (m2) of the curve
   163  func (c *ServiceCurve) Rate() uint32 {
   164  	return c.m2
   165  }
   166  
   167  // HfscClass is a representation of the HFSC class
   168  type HfscClass struct {
   169  	ClassAttrs
   170  	Rsc ServiceCurve
   171  	Fsc ServiceCurve
   172  	Usc ServiceCurve
   173  }
   174  
   175  // SetUsc sets the USC curve. The bandwidth (m1 and m2) is specified in bits and the delay in
   176  // seconds.
   177  func (hfsc *HfscClass) SetUsc(m1 uint32, d uint32, m2 uint32) {
   178  	hfsc.Usc = ServiceCurve{m1: m1, d: d, m2: m2}
   179  }
   180  
   181  // SetFsc sets the Fsc curve. The bandwidth (m1 and m2) is specified in bits and the delay in
   182  // seconds.
   183  func (hfsc *HfscClass) SetFsc(m1 uint32, d uint32, m2 uint32) {
   184  	hfsc.Fsc = ServiceCurve{m1: m1, d: d, m2: m2}
   185  }
   186  
   187  // SetRsc sets the Rsc curve. The bandwidth (m1 and m2) is specified in bits and the delay in
   188  // seconds.
   189  func (hfsc *HfscClass) SetRsc(m1 uint32, d uint32, m2 uint32) {
   190  	hfsc.Rsc = ServiceCurve{m1: m1, d: d, m2: m2}
   191  }
   192  
   193  // SetSC implements the SC from the `tc` CLI. This function behaves the same as if one would set the
   194  // USC through the `tc` command-line tool. This means bandwidth (m1 and m2) is specified in bits and
   195  // the delay in ms.
   196  func (hfsc *HfscClass) SetSC(m1 uint32, d uint32, m2 uint32) {
   197  	hfsc.SetRsc(m1, d, m2)
   198  	hfsc.SetFsc(m1, d, m2)
   199  }
   200  
   201  // SetUL implements the UL from the `tc` CLI. This function behaves the same as if one would set the
   202  // USC through the `tc` command-line tool. This means bandwidth (m1 and m2) is specified in bits and
   203  // the delay in ms.
   204  func (hfsc *HfscClass) SetUL(m1 uint32, d uint32, m2 uint32) {
   205  	hfsc.SetUsc(m1, d, m2)
   206  }
   207  
   208  // SetLS implements the LS from the `tc` CLI. This function behaves the same as if one would set the
   209  // USC through the `tc` command-line tool. This means bandwidth (m1 and m2) is specified in bits and
   210  // the delay in ms.
   211  func (hfsc *HfscClass) SetLS(m1 uint32, d uint32, m2 uint32) {
   212  	hfsc.SetFsc(m1, d, m2)
   213  }
   214  
   215  // NewHfscClass returns a new HFSC struct with the set parameters
   216  func NewHfscClass(attrs ClassAttrs) *HfscClass {
   217  	return &HfscClass{
   218  		ClassAttrs: attrs,
   219  		Rsc:        ServiceCurve{},
   220  		Fsc:        ServiceCurve{},
   221  		Usc:        ServiceCurve{},
   222  	}
   223  }
   224  
   225  // String() returns a string that contains the information and attributes of the HFSC class
   226  func (hfsc *HfscClass) String() string {
   227  	return fmt.Sprintf(
   228  		"{%s -- {RSC: {m1=%d d=%d m2=%d}} {FSC: {m1=%d d=%d m2=%d}} {USC: {m1=%d d=%d m2=%d}}}",
   229  		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,
   230  	)
   231  }
   232  
   233  // Attrs return the Hfsc parameters
   234  func (hfsc *HfscClass) Attrs() *ClassAttrs {
   235  	return &hfsc.ClassAttrs
   236  }
   237  
   238  // Type return the type of the class
   239  func (hfsc *HfscClass) Type() string {
   240  	return "hfsc"
   241  }