/
ethernetinterface.go
355 lines (318 loc) · 12.6 KB
/
ethernetinterface.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"reflect"
"github.com/stmcginnis/gofish/common"
)
// DHCPv6OperatingMode is the IPv6 DHCP mode.
type DHCPv6OperatingMode string
const (
// StatefulDHCPv6OperatingMode shall operate in stateful mode on this
// interface. DHCPv6 stateful mode is used to configure addresses, and
// when it is enabled, stateless mode is also implicitly enabled.
StatefulDHCPv6OperatingMode DHCPv6OperatingMode = "Stateful"
// StatelessDHCPv6OperatingMode shall operate in stateless mode on this
// interface. DHCPv6 stateless mode allows configuring the interface
// using DHCP options but does not configure addresses. It is always
// enabled by default whenever DHCPv6 Stateful mode is also enabled.
StatelessDHCPv6OperatingMode DHCPv6OperatingMode = "Stateless"
// DisabledDHCPv6OperatingMode shall be disabled for this interface.
DisabledDHCPv6OperatingMode DHCPv6OperatingMode = "Disabled"
)
// EthernetDeviceType is the device type.
type EthernetDeviceType string
const (
// PhysicalEthernetDeviceType shall indicate a physical traditional
// network interface.
PhysicalEthernetDeviceType EthernetDeviceType = "Physical"
// VirtualEthernetDeviceType shall indicate a network device function has
// multiple VLANs and is representing one of them as a virtual Ethernet
// interface. The NetworkDeviceFunction property within Links shall
// contain the locator for the parent network device function.
VirtualEthernetDeviceType EthernetDeviceType = "Virtual"
)
// LinkStatus is the interface link status.
type LinkStatus string
const (
// LinkUpLinkStatus The link is available for communication on this
// interface.
LinkUpLinkStatus LinkStatus = "LinkUp"
// NoLinkLinkStatus There is no link or connection detected on this
// interface.
NoLinkLinkStatus LinkStatus = "NoLink"
// LinkDownLinkStatus There is no link on this interface, but the
// interface is connected.
LinkDownLinkStatus LinkStatus = "LinkDown"
)
// DHCPv4Configuration describes the configuration of DHCP v4.
type DHCPv4Configuration struct {
// DHCPEnabled shall indicate whether DHCP v4 is enabled for this
// EthernetInterface.
DHCPEnabled bool
// UseDNSServers shall indicate whether the interface will use
// DHCPv4-supplied DNS servers.
UseDNSServers bool
// UseDomainName shall indicate whether the interface will use a
// DHCPv4-supplied domain name.
UseDomainName bool
// UseGateway shall indicate whether the interface will use a
// DHCPv4-supplied gateway.
UseGateway bool
// UseNTPServers shall indicate whether the interface will use
// DHCPv4-supplied NTP servers.
UseNTPServers bool
// UseStaticRoutes shall indicate whether the interface will use a
// DHCPv4-supplied static routes.
UseStaticRoutes bool
}
// DHCPv6Configuration describes the configuration of DHCP v6.
type DHCPv6Configuration struct {
// OperatingMode is used to configure addresses, and when it is enabled,
// stateless mode is also implicitly enabled.
OperatingMode DHCPv6OperatingMode
// UseDNSServers shall indicate whether the interface will use
// DHCPv6-supplied DNS servers.
UseDNSServers bool
// UseDomainName shall indicate whether the interface will use a domain name
// supplied through DHCPv6 stateless mode.
UseDomainName bool
// UseNTPServers shall indicate whether the interface will use
// DHCPv6-supplied NTP servers.
UseNTPServers bool
// UseRapidCommit shall indicate whether the interface will use DHCPv6 rapid
// commit mode for stateful mode address assignments.
UseRapidCommit bool
}
// EthernetInterface is used to represent NIC resources.
type EthernetInterface struct {
common.Entity
// ODataContext is the odata context.
ODataContext string `json:"@odata.context"`
// ODataType is the odata type.
ODataType string `json:"@odata.type"`
// AutoNeg shall be true if auto negotiation of speed and duplex is enabled
// on this interface and false if it is disabled.
AutoNeg bool
// DHCPv4 shall contain the configuration of DHCP v4.
DHCPv4 DHCPv4Configuration
// DHCPv6 shall contain the configuration of DHCP v6.
DHCPv6 DHCPv6Configuration
// Description provides a description of this resource.
Description string
// EthernetInterfaceType shall contain the type of interface.
EthernetInterfaceType EthernetDeviceType
// FQDN shall be the fully qualified domain name for this interface.
FQDN string
// FullDuplex shall represent the duplex status of the Ethernet connection
// on this interface.
FullDuplex bool
// HostName shall be host name for this interface.
HostName string
// IPv4Addresses is used to represent the IPv4 connection characteristics
// for this interface. It is recommended that this property be regarded as
// read-only, with configuration of static addresses performed by
// updating the values within IPv4StaticAddresses. Services may reject
// updates to this array for this reason.
IPv4Addresses []IPv4Address
// IPv4StaticAddresses is used to represent all IPv4 static addresses
// assigned (but not necessarily in use) to this interface. Addresses in
// use by this interface shall also appear in the IPv4Addresses property.
IPv4StaticAddresses []IPv4Address
// IPv6AddressPolicyTable is used to represent the Address Selection
// Policy Table as defined in RFC 6724.
IPv6AddressPolicyTable []IPv6AddressPolicyEntry
// IPv6Addresses is used to represent the IPv6 connection characteristics
// for this interface.
IPv6Addresses []IPv6Address
// IPv6DefaultGateway shall be the current
// IPv6 default gateway address that is in use on this interface.
IPv6DefaultGateway string
// IPv6StaticAddresses is used to represent the IPv6 static connection
// characteristics for this interface.
IPv6StaticAddresses []IPv6StaticAddress
// IPv6StaticDefaultGateways is The values in this array shall represent
// the IPv6 static default gateway addresses for this interface.
IPv6StaticDefaultGateways []IPv6GatewayStaticAddress
// InterfaceEnabled shall be a boolean
// indicating whether this interface is enabled.
InterfaceEnabled bool
// LinkStatus shall be the link status of this interface (port).
LinkStatus LinkStatus
// MACAddress shall be the effective
// current MAC Address of this interface. If an assignable MAC address is
// not supported, this is a read only alias of the PermanentMACAddress.
MACAddress string
// MTUSize shall be the size in bytes of largest Protocol Data Unit (PDU)
// that can be passed in an Ethernet (MAC) frame on this interface.
MTUSize int
// MaxIPv6StaticAddresses shall indicate the number of array items supported
// by IPv6StaticAddresses.
MaxIPv6StaticAddresses int
// NameServers used on this interface.
NameServers []string
// Oem object used on this interface.
Oem interface{}
// PermanentMACAddress shall be the Permanent MAC Address of this interface
// (port). This value is typically programmed during the manufacturing time.
// This address is not assignable.
PermanentMACAddress string
// SpeedMbps shall be the link speed of the interface in Mbps.
SpeedMbps int
// StatelessAddressAutoConfig is This object shall contain the IPv4 and
// IPv6 Stateless Address Automatic Configuration (SLAAC) properties for
// this interface.
StatelessAddressAutoConfig StatelessAddressAutoConfiguration
// StaticNameServers is used when DHCP provisioning is not in enabled for
// name server configuration. As an implementation option they may also
// be used in addition to DHCP provided addresses, or in cases where the
// DHCP server provides no DNS assignments.
StaticNameServers []string
// Status shall contain any status or health properties
// of the resource.
Status common.Status
// UefiDevicePath shall be the UEFI device path to the device which
// implements this interface (port).
UefiDevicePath string
// VLAN shall be the VLAN for this interface. If this interface supports
// more than one VLAN, the VLAN property shall not be present and the VLANS
// collection link shall be present instead.
VLAN VLAN
// VLANs is a collection of VLANs and is only used if the interface supports
// more than one VLANs.
vlans string
// Chassis shall be a reference to a resource of type Chassis that represent
// the physical container associated with this Ethernet Interface.
chassis string
// Endpoints shall be a reference to the resources that this ethernet
// interface is associated with and shall reference a resource of type
// Endpoint.
endpoints []string
// EndpointsCount is the number of endpoints.
EndpointsCount int
// HostInterface is used by a host to communicate with a Manager.
hostInterface string
// rawData holds the original serialized JSON so we can compare updates.
rawData []byte
}
// UnmarshalJSON unmarshals a EthernetInterface object from the raw JSON.
func (ethernetinterface *EthernetInterface) UnmarshalJSON(b []byte) error {
type temp EthernetInterface
type links struct {
Chassis common.Link
Endpoints common.Links
EndpointsCount int `json:"Endpoints@odata.count"`
HostInterface common.Link
}
var t struct {
temp
Links links
VLANs common.Link
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
*ethernetinterface = EthernetInterface(t.temp)
// Extract the links to other entities for later
ethernetinterface.chassis = t.Links.Chassis.String()
ethernetinterface.endpoints = t.Links.Endpoints.ToStrings()
ethernetinterface.EndpointsCount = t.Links.EndpointsCount
ethernetinterface.hostInterface = t.Links.HostInterface.String()
ethernetinterface.vlans = t.VLANs.String()
// This is a read/write object, so we need to save the raw object data for later
ethernetinterface.rawData = b
return nil
}
// Update commits updates to this object's properties to the running system.
func (ethernetinterface *EthernetInterface) Update() error {
// Get a representation of the object's original state so we can find what
// to update.
original := new(EthernetInterface)
err := original.UnmarshalJSON(ethernetinterface.rawData)
if err != nil {
return err
}
readWriteFields := []string{
"AutoNeg",
"FQDN",
"FullDuplex",
"HostName",
"InterfaceEnabled",
"MACAddress",
"MTUSize",
"SpeedMbps",
"StaticNameServers",
}
originalElement := reflect.ValueOf(original).Elem()
currentElement := reflect.ValueOf(ethernetinterface).Elem()
return ethernetinterface.Entity.Update(originalElement, currentElement, readWriteFields)
}
// GetEthernetInterface will get a EthernetInterface instance from the service.
func GetEthernetInterface(c common.Client, uri string) (*EthernetInterface, error) {
var ethernetInterface EthernetInterface
return ðernetInterface, ethernetInterface.Get(c, uri, ðernetInterface)
}
// ListReferencedEthernetInterfaces gets the collection of EthernetInterface from
// a provided reference.
func ListReferencedEthernetInterfaces(c common.Client, link string) ([]*EthernetInterface, error) { //nolint:dupl
var result []*EthernetInterface
if link == "" {
return result, nil
}
type GetResult struct {
Item *EthernetInterface
Link string
Error error
}
ch := make(chan GetResult)
collectionError := common.NewCollectionError()
get := func(link string) {
ethernetinterface, err := GetEthernetInterface(c, link)
ch <- GetResult{Item: ethernetinterface, Link: link, Error: err}
}
go func() {
err := common.CollectList(get, c, link)
if err != nil {
collectionError.Failures[link] = err
}
close(ch)
}()
for r := range ch {
if r.Error != nil {
collectionError.Failures[r.Link] = r.Error
} else {
result = append(result, r.Item)
}
}
if collectionError.Empty() {
return result, nil
}
return result, collectionError
}
// IPv6AddressPolicyEntry describes and entry in the Address Selection Policy
// Table as defined in RFC 6724.
type IPv6AddressPolicyEntry struct {
// Label shall contain the IPv6 Label value for this table entry as defined
// in RFC 6724 section 2.1.
Label int
// Precedence shall contain the IPv6 Precedence value for this table entry
// as defined in RFC 6724 section 2.1.
Precedence int
// Prefix shall contain the IPv6 Address Prefix for this table entry as
// defined in RFC 6724 section 2.1.
Prefix string
}
// StatelessAddressAutoConfiguration describes the IPv4 and IPv6 Stateless
// Address Automatic Configuration (SLAAC) for this interface.
type StatelessAddressAutoConfiguration struct {
// IPv4AutoConfigEnabled shall indicate whether IPv4 Stateless Address
// Auto-Configuration (SLAAC) is enabled for this interface.
IPv4AutoConfigEnabled bool
// IPv6AutoConfigEnabled shall indicate whether IPv6 Stateless Address
// Auto-Configuration (SLAAC) is enabled for this interface.
IPv6AutoConfigEnabled bool
}
// TODO: Add vlans