-
Notifications
You must be signed in to change notification settings - Fork 13
/
report.go
171 lines (157 loc) · 3.29 KB
/
report.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package gtp5gnl
import (
"fmt"
"syscall"
"github.com/khirono/go-genl"
"github.com/khirono/go-nl"
)
func GetReport(c *Client, link *Link, urrid uint64, seid uint64) ([]USAReport, error) {
return GetReportOID(c, link, OID{uint64(urrid), seid})
}
func GetUsageStatistic(c *Client, link *Link) (*UsageStatistic, error) {
flags := syscall.NLM_F_ACK
req := nl.NewRequest(c.ID, flags)
err := req.Append(genl.Header{Cmd: CMD_GET_USAGE_STATISTIC})
if err != nil {
return nil, err
}
err = req.Append(nl.AttrList{
{
Type: LINK,
Value: nl.AttrU32(link.Index),
},
})
if err != nil {
return nil, err
}
rsps, err := c.Do(req)
if err != nil {
return nil, err
}
if len(rsps) < 1 {
return nil, fmt.Errorf("nil Usage Statistic")
}
ustat, err := DecodeUsageStatistic(rsps[0].Body[genl.SizeofHeader:])
if err != nil {
return nil, err
}
return ustat, err
}
func GetReportOID(c *Client, link *Link, oid OID) ([]USAReport, error) {
flags := syscall.NLM_F_ACK
req := nl.NewRequest(c.ID, flags)
err := req.Append(genl.Header{Cmd: CMD_GET_REPORT})
if err != nil {
return nil, err
}
urrid, ok := oid.ID()
if !ok {
return nil, fmt.Errorf("invalid oid: %v", oid)
}
err = req.Append(nl.AttrList{
{
Type: LINK,
Value: nl.AttrU32(link.Index),
},
{
Type: URR_ID,
Value: nl.AttrU32(urrid),
},
})
if err != nil {
return nil, err
}
seid, ok := oid.SEID()
if ok {
err = req.Append(&nl.Attr{
Type: URR_SEID,
Value: nl.AttrU64(seid),
})
if err != nil {
return nil, err
}
}
rsps, err := c.Do(req)
if err != nil {
return nil, err
}
if len(rsps) < 1 {
return nil, fmt.Errorf("nil Report of oid(%v)", oid)
}
reports, err := DecodeAllUSAReports(rsps[0].Body[genl.SizeofHeader:])
if err != nil {
return nil, err
}
return reports, err
}
// map[uint64][]uint32 // key: seid, value: urrids
func GetMultiReports(c *Client, link *Link, lSeidUrridsMap map[uint64][]uint32) ([]USAReport, error) {
var oids []OID
for seid, urrIds := range lSeidUrridsMap {
for _, urrId := range urrIds {
oids = append(oids, OID{seid, uint64(urrId)})
}
}
return GetMultiReportsOID(c, link, oids)
}
func GetMultiReportsOID(c *Client, link *Link, oids []OID) ([]USAReport, error) {
var attrs []nl.Attr
flags := syscall.NLM_F_ACK
req := nl.NewRequest(c.ID, flags)
err := req.Append(genl.Header{Cmd: CMD_GET_MULTI_REPORTS})
if err != nil {
return nil, err
}
err = req.Append(nl.AttrList{
{
Type: LINK,
Value: nl.AttrU32(link.Index),
},
{
Type: URR_NUM,
Value: nl.AttrU32(len(oids)),
},
})
if err != nil {
return nil, err
}
for _, oid := range oids {
urrid, ok := oid.ID()
if !ok {
return nil, fmt.Errorf("invalid oid: %v", oid)
}
seid, ok := oid.SEID()
if ok {
attrs = append(attrs, nl.Attr{
Type: URR_MULTI_SEID_URRID,
Value: nl.AttrList{
{
Type: URR_ID,
Value: nl.AttrU32(urrid),
},
{
Type: URR_SEID,
Value: nl.AttrU64(seid),
},
},
},
)
}
}
err = req.Append(nl.AttrList(attrs))
if err != nil {
return nil, err
}
rsps, err := c.Do(req)
if err != nil {
return nil, err
}
if len(rsps) < 1 {
return nil, fmt.Errorf("nil Report")
}
reports, err := DecodeAllUSAReports(rsps[0].Body[genl.SizeofHeader:])
if err != nil {
return nil, err
}
return reports, err
}