Skip to content

Commit 236be8b

Browse files
authored
Migrate vpp_if_stats plugin from VPP (#361)
This patch migrates the vpp_if_stats plugin which is an example usage of the VPP stats API client from VPP's extras/ folder to govpp's extras folder. Change-Id: Ib874199b4c847037dcc62fbcedb09226b2ff7bfc Signed-off-by: Nathan Skrzypczak <nathan.skrzypczak@gmail.com>
1 parent 94e50af commit 236be8b

14 files changed

Lines changed: 1339 additions & 2 deletions

‎extras/Makefile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ extras:
1111
cd gomemif/examples/bridge && $(GO) build -v
1212
cd gomemif/examples/icmp_responder_cb && $(GO) build -v
1313
cd gomemif/examples/icmp_responder_poll && $(GO) build -v
14+
cd vpp_if_stats && $(GO) build -v
1415

1516
clean:
1617
@echo "=> cleaning extras"

‎extras/go.mod‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
11
module go.fd.io/govpp/extras
22

3-
go 1.22
3+
go 1.23.0
44

55
require (
66
github.com/google/gopacket v1.1.17
77
github.com/pkg/profile v1.7.0
88
github.com/sirupsen/logrus v1.9.3
9+
github.com/stretchr/testify v1.9.0
910
go.fd.io/govpp v0.10.0
11+
go.uber.org/mock v0.6.0
1012
)
1113

1214
require (
15+
github.com/davecgh/go-spew v1.1.1 // indirect
1316
github.com/felixge/fgprof v0.9.3 // indirect
1417
github.com/fsnotify/fsnotify v1.7.0 // indirect
1518
github.com/ftrvxmtrx/fd v0.0.0-20150925145434-c6d800382fff // indirect
1619
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd // indirect
20+
github.com/kr/text v0.2.0 // indirect
1721
github.com/lunixbochs/struc v0.0.0-20200521075829-a4cb8d33dbbe // indirect
1822
github.com/mitchellh/go-ps v1.0.0 // indirect
23+
github.com/pmezard/go-difflib v1.0.0 // indirect
1924
golang.org/x/sys v0.16.0 // indirect
25+
gopkg.in/yaml.v3 v3.0.1 // indirect
2026
)

‎extras/go.sum‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
22
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
33
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
4+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
45
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
56
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
67
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -17,6 +18,10 @@ github.com/google/gopacket v1.1.17/go.mod h1:UdDNZ1OO62aGYVnPhxT1U6aI7ukYtA/kB8v
1718
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd h1:1FjCyPC+syAzJ5/2S8fqdZK1R22vvA0J7JZKcuOIQ7Y=
1819
github.com/google/pprof v0.0.0-20211214055906-6f57359322fd/go.mod h1:KgnwoLYCZ8IQu3XUZ8Nc/bM9CCZFOyjUNOSygVozoDg=
1920
github.com/ianlancetaylor/demangle v0.0.0-20210905161508-09a460cdf81d/go.mod h1:aYm2/VgdVmcIU8iMfdMvDMsRAQjcfZSKFby6HOFvi/w=
21+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
22+
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
23+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
24+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2025
github.com/lunixbochs/struc v0.0.0-20200521075829-a4cb8d33dbbe h1:ewr1srjRCmcQogPQ/NCx6XCk6LGVmsVCc9Y3vvPZj+Y=
2126
github.com/lunixbochs/struc v0.0.0-20200521075829-a4cb8d33dbbe/go.mod h1:vy1vK6wD6j7xX6O6hXe621WabdtNkou2h7uRtTfRMyg=
2227
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
@@ -33,10 +38,13 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
3338
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
3439
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
3540
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
36-
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
3741
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
42+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
43+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
3844
go.fd.io/govpp v0.10.0 h1:lL93SbqOILjON2pMvazrlHRekGYTRy0Qmj57RuAkxR0=
3945
go.fd.io/govpp v0.10.0/go.mod h1:5m3bZM9ck+2EGC2O3ASmSSJAaoouyOlVWtiwj5BdCv0=
46+
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
47+
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
4048
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4149
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
4250
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
@@ -51,6 +59,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
5159
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
5260
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
5361
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
62+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
63+
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
5464
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5565
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
5666
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

‎extras/vpp_if_stats/.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vpp_if_stats

‎extras/vpp_if_stats/Makefile‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
.PHONY: build
3+
build:
4+
go build .
5+
6+
.PHONY: test
7+
test:
8+
go test .
9+
10+
# Generate test mocks
11+
.PHONY: gen
12+
gen:
13+
go generate .

‎extras/vpp_if_stats/README.rst‎

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
.. _if_stats_client_doc:
2+
3+
VPP interface stats client
4+
==========================
5+
6+
This is a source code and a binary of a ‘thin client’ to collect,
7+
aggregate and expose VPP interface stats through VPP stats socket API.
8+
It also provides some information about the installed VPP version.
9+
10+
This can be used by monitoring systems that needs to grab those details
11+
through a simple executable client with no dependencies.
12+
13+
example use case: where VPP runs in a container that can’t expose the
14+
socket API to the host level
15+
16+
Prerequisites (for building)
17+
----------------------------
18+
19+
**GoVPP** library (compatible with VPP 18.10) vpp, vpp-api, vpp-lib
20+
21+
Building
22+
--------
23+
24+
.. code:: bash
25+
26+
make build
27+
28+
Using (post-build for example on linux 64bit)
29+
---------------------------------------------
30+
31+
.. code:: bash
32+
33+
./bin/vpp_if_stats_linux_amd64
34+
35+
Output examples
36+
---------------
37+
38+
`JSON schema <./response_schema.json>`__
39+
`Example <./response_example.json>`__
40+
41+
Testing
42+
-------
43+
44+
.. code:: bash
45+
46+
# Start VPP
47+
docker run -v /run/vpp:/run/vpp --privileged -it \
48+
ligato/vpp-base:25.02 vpp \
49+
'unix { interactive }
50+
dpdk { no-pci }
51+
statseg { socket-name /run/vpp/stats.sock }'
52+
53+
# Run the stats client
54+
sudo ./vpp_if_stats | jq .

‎extras/vpp_if_stats/apimock.go‎

Lines changed: 127 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎extras/vpp_if_stats/gen.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package main
2+
3+
import (
4+
_ "go.uber.org/mock/mockgen/model"
5+
)
6+
7+
//go:generate go run go.uber.org/mock/mockgen -package main -destination statsmock.go go.fd.io/govpp/adapter StatsAPI
8+
//go:generate go run go.uber.org/mock/mockgen -package main -destination apimock.go go.fd.io/govpp/api Channel
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net"
7+
8+
"go.fd.io/govpp/binapi/interface_types"
9+
"go.fd.io/govpp/binapi/vpe"
10+
)
11+
12+
type jsonVppDetails struct {
13+
Program string `json:"program"`
14+
Version string `json:"version"`
15+
BuildDate string `json:"build_date"`
16+
BuildDirectory string `json:"build_directory"`
17+
}
18+
19+
type jsonVppInterface struct {
20+
Index uint32 `json:"if_index"`
21+
Name string `json:"if_name"`
22+
Tag string `json:"if_tag"`
23+
MacAddress string `json:"if_mac"`
24+
AdminState bool `json:"if_admin_state"`
25+
LinkState bool `json:"if_link_state"`
26+
LinkMTU uint16 `json:"if_link_mtu"`
27+
SubID uint32 `json:"if_sub_id"`
28+
29+
TxBytes uint64 `json:"if_tx_bytes"`
30+
TxPackets uint64 `json:"if_tx_packets"`
31+
TxErrors uint64 `json:"if_tx_errors"`
32+
RxBytes uint64 `json:"if_rx_bytes"`
33+
RxPackets uint64 `json:"if_rx_packets"`
34+
RxErrors uint64 `json:"if_rx_errors"`
35+
Drops uint64 `json:"if_drops"`
36+
Punts uint64 `json:"if_punts"`
37+
}
38+
39+
type jsonVppPayload struct {
40+
*jsonVppDetails `json:"vpp_details"`
41+
Interfaces []*jsonVppInterface `json:"interfaces"`
42+
}
43+
44+
func toJSONVppDetails(svReply *vpe.ShowVersionReply) *jsonVppDetails {
45+
return &jsonVppDetails{
46+
Program: svReply.Program,
47+
Version: svReply.Version,
48+
BuildDate: svReply.BuildDate,
49+
BuildDirectory: svReply.BuildDirectory,
50+
}
51+
}
52+
53+
func toJSONVppInterface(vppIf *vppInterface) *jsonVppInterface {
54+
return &jsonVppInterface{
55+
Index: uint32(vppIf.SwIfIndex),
56+
Name: vppIf.InterfaceName,
57+
Tag: vppIf.Tag,
58+
MacAddress: net.HardwareAddr(vppIf.L2Address[:]).String(),
59+
AdminState: vppIf.Flags&interface_types.IF_STATUS_API_FLAG_ADMIN_UP != 0,
60+
LinkState: vppIf.Flags&interface_types.IF_STATUS_API_FLAG_LINK_UP != 0,
61+
LinkMTU: vppIf.LinkMtu,
62+
SubID: vppIf.SubID,
63+
TxBytes: vppIf.Stats.TxBytes,
64+
TxPackets: vppIf.Stats.TxPackets,
65+
TxErrors: vppIf.Stats.TxErrors,
66+
RxBytes: vppIf.Stats.RxBytes,
67+
RxPackets: vppIf.Stats.RxPackets,
68+
RxErrors: vppIf.Stats.RxErrors,
69+
Drops: vppIf.Stats.Drops,
70+
Punts: vppIf.Stats.Punts,
71+
}
72+
}
73+
74+
func toJSONVppPayload(svReply *vpe.ShowVersionReply, vppIfs []*vppInterface) *jsonVppPayload {
75+
p := &jsonVppPayload{
76+
jsonVppDetails: toJSONVppDetails(svReply),
77+
Interfaces: make([]*jsonVppInterface, len(vppIfs)),
78+
}
79+
for index, vppIf := range vppIfs {
80+
p.Interfaces[index] = toJSONVppInterface(vppIf)
81+
}
82+
return p
83+
}
84+
85+
func dumpToJSONString(v *vppConnector) (string, error) {
86+
payload := toJSONVppPayload(&v.VppDetails, v.Interfaces)
87+
jsonBytes, err := json.Marshal(payload)
88+
if err != nil {
89+
return "", fmt.Errorf("failed to dump to json: %v", err)
90+
}
91+
return string(jsonBytes), nil
92+
}

0 commit comments

Comments
 (0)