forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dump_link_linuxcalls.go
188 lines (159 loc) · 5.91 KB
/
dump_link_linuxcalls.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package linuxcalls
import (
"github.com/go-errors/errors"
"github.com/ligato/vpp-agent/plugins/linux/model/interfaces"
"github.com/ligato/vpp-agent/plugins/linux/nsplugin"
"github.com/vishvananda/netlink"
)
// LinuxInterfaceDetails is the wrapper structure for the linux interface northbound API structure.
type LinuxInterfaceDetails struct {
Interface *interfaces.LinuxInterfaces_Interface `json:"linux_interface"`
Meta *LinuxInterfaceMeta `json:"linux_interface_meta"`
}
// LinuxInterfaceMeta is combination of proto-modelled Interface data and linux provided metadata
type LinuxInterfaceMeta struct {
Index int `json:"index"`
Name string `json:"name"`
Alias string `json:"alias"`
OperState string `json:"oper_state"`
Flags string `json:"flags"`
MacAddr string `json:"mac_addr"`
Mtu int `json:"mtu"`
Type string `json:"type"`
NetNsID int `json:"net_ns_id"`
NumTxQueues int `json:"num_tx_queues"`
TxQueueLen int `json:"tx_queue_len"`
NumRxQueues int `json:"num_rx_queues"`
}
// DumpInterfaces is an implementation of linux interface handler
func (h *NetLinkHandler) DumpInterfaces() ([]*LinuxInterfaceDetails, error) {
var ifs []*LinuxInterfaceDetails
ctx := nsplugin.NewNamespaceMgmtCtx()
// Vpp-agent should know all the configured interfaces, and all the interfaces from default namespace. Use index
// map to iterate over them
for _, ifName := range h.ifIndexes.GetMapping().ListNames() {
ifDetails := &LinuxInterfaceDetails{}
_, meta, found := h.ifIndexes.LookupIdx(ifName)
if !found {
h.log.Warnf("Expected interface %s not found in the mapping", ifName)
continue
}
if meta == nil || meta.Data == nil {
h.log.Warnf("Expected interface %s metadata are missing", ifName)
continue
}
// Copy base configuration from mapping metadata. Linux specific fields are stored in LinuxInterfaceMeta.
ifDetails.Interface = meta.Data
// Check the interface namespace
link, linkAddrs, err := h.dumpInterfaceData(ifName, h.nsHandler.IfNsToGeneric(meta.Data.Namespace), ctx)
if err != nil {
// Do not return error, read what is possible
h.log.Errorf("failed to get interface %s data: %v", ifName, err)
continue
}
if link == nil || link.Attrs() == nil {
h.log.Warnf("Unable to get link data for interface %s", ifName)
continue
}
linkAttrs := link.Attrs()
// Base fields
linuxMeta := &LinuxInterfaceMeta{
Index: linkAttrs.Index,
Name: linkAttrs.Name,
Alias: linkAttrs.Alias,
OperState: linkAttrs.OperState.String(),
Flags: linkAttrs.Flags.String(),
Mtu: linkAttrs.MTU,
Type: linkAttrs.EncapType,
NetNsID: linkAttrs.NetNsID,
NumTxQueues: linkAttrs.NumTxQueues,
TxQueueLen: linkAttrs.TxQLen,
NumRxQueues: linkAttrs.NumRxQueues,
}
// IP addresses
var ipAddrs []string
for _, linkAddr := range linkAddrs {
ipAddrs = append(ipAddrs, linkAddr.String())
}
// MAC address
if linkAttrs.HardwareAddr != nil {
linuxMeta.MacAddr = linkAttrs.HardwareAddr.String()
}
ifDetails.Meta = linuxMeta
ifs = append(ifs, ifDetails)
}
return ifs, nil
}
// LinuxInterfaceStatistics returns linux interface name/index with statistics data
type LinuxInterfaceStatistics struct {
Name string
Index int
Statistics *netlink.LinkStatistics
}
// DumpInterfaceStatistics is an implementation of linux interface handler
func (h *NetLinkHandler) DumpInterfaceStatistics() ([]*LinuxInterfaceStatistics, error) {
var ifs []*LinuxInterfaceStatistics
ctx := nsplugin.NewNamespaceMgmtCtx()
// Iterate over all known interfaces
for _, ifName := range h.ifIndexes.GetMapping().ListNames() {
_, meta, found := h.ifIndexes.LookupIdx(ifName)
if !found {
h.log.Warnf("Expected interface %s not found in the mapping", ifName)
continue
}
if meta == nil || meta.Data == nil {
h.log.Warnf("Expected interface %s metadata are missing", ifName)
continue
}
// Check the interface namespace
link, _, err := h.dumpInterfaceData(ifName, h.nsHandler.IfNsToGeneric(meta.Data.Namespace), ctx)
if err != nil {
// Do not return error, read what is possible
h.log.Errorf("failed to get interface %s data: %v", ifName, err)
continue
}
if link == nil || link.Attrs() == nil {
h.log.Warnf("Unable to get link data for interface %s", ifName)
continue
}
linkAttrs := link.Attrs()
// Fill data
linuxStats := &LinuxInterfaceStatistics{
Name: linkAttrs.Name,
Index: linkAttrs.Index,
Statistics: linkAttrs.Statistics,
}
ifs = append(ifs, linuxStats)
}
return ifs, nil
}
// Reads interface data and ip addresses from provided namespace
func (h *NetLinkHandler) dumpInterfaceData(ifName string, ns *nsplugin.Namespace, ctx *nsplugin.NamespaceMgmtCtx) (netlink.Link, []netlink.Addr, error) {
revert, err := h.nsHandler.SwitchNamespace(ns, ctx)
defer revert()
if err != nil {
return nil, nil, errors.Errorf("failed to switch to namespace %s: %v", ns.Name, err)
}
link, err := h.GetLinkByName(ifName)
if err != nil {
return nil, nil, errors.Errorf("failed to get interface %s from namespace %s: %v", ifName, ns.Name, err)
}
linkAddrs, err := h.GetAddressList(ifName)
if err != nil {
return nil, nil, errors.Errorf("failed to get interface %s addresses: %v", ifName, err)
}
return link, linkAddrs, nil
}