forked from mdlayher/ethtool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient_linux.go
803 lines (693 loc) · 20.3 KB
/
client_linux.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
//go:build linux
// +build linux
package ethtool
import (
"errors"
"fmt"
"os"
"strings"
"github.com/mdlayher/genetlink"
"github.com/mdlayher/netlink"
"golang.org/x/sys/unix"
)
// errBadRequest indicates an invalid Request from the caller.
var errBadRequest = errors.New("ethtool: Request must have Index and/or Name set when calling Client methods")
// A client is the Linux implementation backing a Client.
type client struct {
c *genetlink.Conn
family uint16
monitorID uint32
}
// Note that some Client methods may panic if the kernel returns an unexpected
// number of netlink messages when only one is expected. This means that a
// fundamental request invariant is broken and we can't provide anything of use
// to the caller, so a panic seems reasonable.
// newClient opens a generic netlink connection to the ethtool family.
func newClient() (*client, error) {
// ethtool is a reasonably new genetlink family and anyone using its API
// should support the Strict socket options set.
conn, err := genetlink.Dial(&netlink.Config{Strict: true})
if err != nil {
return nil, err
}
c, err := initClient(conn)
if err != nil {
_ = conn.Close()
return nil, err
}
return c, nil
}
// initClient is the internal client constructor used in some tests.
func initClient(c *genetlink.Conn) (*client, error) {
f, err := c.GetFamily(unix.ETHTOOL_GENL_NAME)
if err != nil {
return nil, err
}
// Find the group ID for the monitor group in case we need it later.
var monitorID uint32
for _, g := range f.Groups {
if g.Name == unix.ETHTOOL_MCGRP_MONITOR_NAME {
monitorID = g.ID
break
}
}
if monitorID == 0 {
return nil, errors.New("ethtool: could not find monitor multicast group ID")
}
return &client{
c: c,
family: f.ID,
monitorID: monitorID,
}, nil
}
// Close closes the underlying generic netlink connection.
func (c *client) Close() error { return c.c.Close() }
// LinkInfos fetches information about all ethtool-supported links.
func (c *client) LinkInfos() ([]*LinkInfo, error) {
return c.linkInfo(netlink.Dump, Interface{})
}
// LinkInfo fetches information about a single ethtool-supported link.
func (c *client) LinkInfo(ifi Interface) (*LinkInfo, error) {
lis, err := c.linkInfo(0, ifi)
if err != nil {
return nil, err
}
if l := len(lis); l != 1 {
panicf("ethtool: unexpected number of LinkInfo messages for request index: %d, name: %q: %d",
ifi.Index, ifi.Name, l)
}
return lis[0], nil
}
// linkInfo is the shared logic for Client.LinkInfo(s).
func (c *client) linkInfo(flags netlink.HeaderFlags, ifi Interface) ([]*LinkInfo, error) {
msgs, err := c.get(
unix.ETHTOOL_A_LINKINFO_HEADER,
unix.ETHTOOL_MSG_LINKINFO_GET,
flags,
ifi,
nil,
)
if err != nil {
return nil, err
}
return parseLinkInfo(msgs)
}
// LinkModes fetches modes for all ethtool-supported links.
func (c *client) LinkModes() ([]*LinkMode, error) {
return c.linkMode(netlink.Dump, Interface{})
}
// LinkMode fetches information about a single ethtool-supported link's modes.
func (c *client) LinkMode(ifi Interface) (*LinkMode, error) {
lms, err := c.linkMode(0, ifi)
if err != nil {
return nil, err
}
if l := len(lms); l != 1 {
panicf("ethtool: unexpected number of LinkMode messages for request index: %d, name: %q: %d",
ifi.Index, ifi.Name, l)
}
return lms[0], nil
}
// linkMode is the shared logic for Client.LinkMode(s).
func (c *client) linkMode(flags netlink.HeaderFlags, ifi Interface) ([]*LinkMode, error) {
msgs, err := c.get(
unix.ETHTOOL_A_LINKMODES_HEADER,
unix.ETHTOOL_MSG_LINKMODES_GET,
flags,
ifi,
nil,
)
if err != nil {
return nil, err
}
return parseLinkModes(msgs)
}
// LinkStates fetches link state data for all ethtool-supported links.
func (c *client) LinkStates() ([]*LinkState, error) {
return c.linkState(netlink.Dump, Interface{})
}
// LinkState fetches link state data for a single ethtool-supported link.
func (c *client) LinkState(ifi Interface) (*LinkState, error) {
lss, err := c.linkState(0, ifi)
if err != nil {
return nil, err
}
if l := len(lss); l != 1 {
panicf("ethtool: unexpected number of LinkState messages for request index: %d, name: %q: %d",
ifi.Index, ifi.Name, l)
}
return lss[0], nil
}
// linkState is the shared logic for Client.LinkState(s).
func (c *client) linkState(flags netlink.HeaderFlags, ifi Interface) ([]*LinkState, error) {
msgs, err := c.get(
unix.ETHTOOL_A_LINKSTATE_HEADER,
unix.ETHTOOL_MSG_LINKSTATE_GET,
flags,
ifi,
nil,
)
if err != nil {
return nil, err
}
return parseLinkState(msgs)
}
// FEC fetches the forward error correction (FEC) setting for a single
// ethtool-supported link.
func (c *client) FEC(ifi Interface) (*FEC, error) {
fecs, err := c.fec(0, ifi)
if err != nil {
return nil, err
}
if l := len(fecs); l != 1 {
panicf("ethtool: unexpected number of FEC messages for request index: %d, name: %q: %d",
ifi.Index, ifi.Name, l)
}
return fecs[0], nil
}
// fec is the shared logic for Client.FEC(s).
func (c *client) fec(flags netlink.HeaderFlags, ifi Interface) ([]*FEC, error) {
msgs, err := c.get(
_ETHTOOL_A_FEC_HEADER,
unix.ETHTOOL_MSG_FEC_GET,
flags,
ifi,
nil,
)
if err != nil {
return nil, err
}
return parseFEC(msgs)
}
// SetFEC configures forward error correction (FEC) parameters for a single
// ethtool-supported interface.
func (c *client) SetFEC(fec FEC) error {
_, err := c.get(
_ETHTOOL_A_FEC_HEADER,
unix.ETHTOOL_MSG_FEC_SET,
netlink.Acknowledge,
fec.Interface,
fec.encode,
)
return err
}
// encode packs FEC data into the appropriate netlink attributes for the
// encoder.
func (fec FEC) encode(ae *netlink.AttributeEncoder) {
var bits []string
if fec.Modes&unix.ETHTOOL_FEC_OFF != 0 {
bits = append(bits, "None")
}
if fec.Modes&unix.ETHTOOL_FEC_RS != 0 {
bits = append(bits, "RS")
}
if fec.Modes&unix.ETHTOOL_FEC_BASER != 0 {
bits = append(bits, "BASER")
}
if fec.Modes&unix.ETHTOOL_FEC_LLRS != 0 {
bits = append(bits, "LLRS")
}
ae.Nested(_ETHTOOL_A_FEC_MODES, func(nae *netlink.AttributeEncoder) error {
// Overwrite the bits instead of merging them.
nae.Flag(unix.ETHTOOL_A_BITSET_NOMASK, true)
nae.Nested(unix.ETHTOOL_A_BITSET_BITS, func(nae *netlink.AttributeEncoder) error {
for _, bit := range bits {
nae.Nested(unix.ETHTOOL_A_BITSET_BITS_BIT, func(nae *netlink.AttributeEncoder) error {
nae.String(unix.ETHTOOL_A_BITSET_BIT_NAME, bit)
return nil
})
}
return nil
})
return nil
})
var auto uint8
if fec.Auto {
auto = 1
}
ae.Uint8(_ETHTOOL_A_FEC_AUTO, auto)
}
// Supported returns the supported/configured FEC modes. Some drivers report
// supported, others configured. See
// https://kernel.googlesource.com/pub/scm/network/ethtool/ethtool/+/2b3ddcb35357ae34ed0a6ae2bb006dcdaec353a9
func (f *FEC) Supported() FECModes {
result := f.Modes
if f.Auto {
result |= unix.ETHTOOL_FEC_AUTO
}
return result
}
// String implements fmt.Stringer.
func (f FECMode) String() string {
switch f {
case unix.ETHTOOL_FEC_AUTO:
return "Auto"
case unix.ETHTOOL_FEC_BASER:
return "BaseR"
case unix.ETHTOOL_FEC_LLRS:
return "LLRS"
case unix.ETHTOOL_FEC_NONE:
return "Off"
case unix.ETHTOOL_FEC_OFF:
return "Off"
case unix.ETHTOOL_FEC_RS:
return "RS"
default:
return "<unknown>"
}
}
// String implements fmt.Stringer.
func (f FECModes) String() string {
var modes []string
if f&unix.ETHTOOL_FEC_AUTO > 0 {
modes = append(modes, "Auto")
}
if f&unix.ETHTOOL_FEC_BASER > 0 {
modes = append(modes, "BaseR")
}
if f&unix.ETHTOOL_FEC_LLRS > 0 {
modes = append(modes, "LLRS")
}
if f&unix.ETHTOOL_FEC_NONE > 0 {
modes = append(modes, "Off")
}
if f&unix.ETHTOOL_FEC_OFF > 0 {
modes = append(modes, "Off")
}
if f&unix.ETHTOOL_FEC_RS > 0 {
modes = append(modes, "RS")
}
return strings.Join(modes, " ")
}
// WakeOnLANs fetches Wake-on-LAN information for all ethtool-supported links.
func (c *client) WakeOnLANs() ([]*WakeOnLAN, error) {
return c.wakeOnLAN(netlink.Dump, Interface{})
}
// WakeOnLAN fetches Wake-on-LAN information for a single ethtool-supported
// interface.
func (c *client) WakeOnLAN(ifi Interface) (*WakeOnLAN, error) {
wols, err := c.wakeOnLAN(0, ifi)
if err != nil {
return nil, err
}
if l := len(wols); l != 1 {
panicf("ethtool: unexpected number of WakeOnLAN messages for request index: %d, name: %q: %d",
ifi.Index, ifi.Name, l)
}
return wols[0], nil
}
// SetWakeOnLAN configures Wake-on-LAN parameters for a single ethtool-supported
// interface.
func (c *client) SetWakeOnLAN(wol WakeOnLAN) error {
_, err := c.get(
unix.ETHTOOL_A_WOL_HEADER,
unix.ETHTOOL_MSG_WOL_SET,
netlink.Acknowledge,
wol.Interface,
wol.encode,
)
return err
}
// wakeOnLAN is the shared logic for Client.WakeOnLAN(s).
func (c *client) wakeOnLAN(flags netlink.HeaderFlags, ifi Interface) ([]*WakeOnLAN, error) {
msgs, err := c.get(
unix.ETHTOOL_A_WOL_HEADER,
unix.ETHTOOL_MSG_WOL_GET,
flags,
ifi,
nil,
)
if err != nil {
return nil, err
}
return parseWakeOnLAN(msgs)
}
// encode packs WakeOnLAN data into the appropriate netlink attributes for the
// encoder.
func (wol WakeOnLAN) encode(ae *netlink.AttributeEncoder) {
ae.Nested(unix.ETHTOOL_A_WOL_MODES, func(nae *netlink.AttributeEncoder) error {
// TODO(mdlayher): ensure this stays in sync if new modes are added!
nae.Uint32(unix.ETHTOOL_A_BITSET_SIZE, 8)
// Note that we are cheating a bit here by directly passing a
// uint32, but this is okay because there are less than 32 bits
// for the WOL modes and therefore the bitset is just the native
// endian representation of the modes bitmask.
nae.Uint32(unix.ETHTOOL_A_BITSET_VALUE, uint32(wol.Modes))
nae.Uint32(unix.ETHTOOL_A_BITSET_MASK, uint32(wol.Modes))
return nil
})
}
// get performs a request/response interaction with ethtool netlink.
func (c *client) get(
header uint16,
cmd uint8,
flags netlink.HeaderFlags,
ifi Interface,
// May be nil; used to apply optional parameters.
params func(ae *netlink.AttributeEncoder),
) ([]genetlink.Message, error) {
if flags&netlink.Dump == 0 && ifi.Index == 0 && ifi.Name == "" {
// The caller is not requesting to dump information for multiple
// interfaces and thus has to specify some identifier or the kernel will
// EINVAL on this path.
return nil, errBadRequest
}
// TODO(mdlayher): make this faster by potentially precomputing the byte
// slice of packed netlink attributes and then modifying the index value at
// the appropriate byte slice index.
ae := netlink.NewAttributeEncoder()
ae.Nested(header, func(nae *netlink.AttributeEncoder) error {
// When fetching by index or name, one or both will be non-zero.
// Otherwise we leave the header empty to dump all the links.
//
// Note that if the client happens to pass an incompatible non-zero
// index/name pair, the kernel will return an error and we'll
// immediately send that back.
if ifi.Index > 0 {
nae.Uint32(unix.ETHTOOL_A_HEADER_DEV_INDEX, uint32(ifi.Index))
}
if ifi.Name != "" {
nae.String(unix.ETHTOOL_A_HEADER_DEV_NAME, ifi.Name)
}
// Unconditionally add the compact bitsets flag to all query commands
// since the ethtool multicast group notifications require the compact
// format, so we might as well always use it.
if cmd != unix.ETHTOOL_MSG_FEC_SET &&
cmd != unix.ETHTOOL_MSG_WOL_SET {
nae.Uint32(unix.ETHTOOL_A_HEADER_FLAGS, unix.ETHTOOL_FLAG_COMPACT_BITSETS)
}
return nil
})
if params != nil {
// Optionally apply more parameters to the attribute encoder.
params(ae)
}
// Note: don't send netlink.Acknowledge or we get an extra message back from
// the kernel which doesn't seem useful as of now.
msgs, err := c.execute(cmd, flags, ae)
if err != nil {
// Unpack the extended acknowledgement error message if possible so the
// caller doesn't have to unpack it themselves.
var msg string
if oerr, ok := err.(*netlink.OpError); ok {
msg = oerr.Message
}
return nil, &Error{
Message: msg,
Err: err,
}
}
return msgs, nil
}
// execute executes the specified command with additional header flags and input
// netlink request attributes. The netlink.Request header flag is automatically
// set.
func (c *client) execute(cmd uint8, flags netlink.HeaderFlags, ae *netlink.AttributeEncoder) ([]genetlink.Message, error) {
b, err := ae.Encode()
if err != nil {
return nil, err
}
return c.c.Execute(
genetlink.Message{
Header: genetlink.Header{
Command: cmd,
Version: unix.ETHTOOL_GENL_VERSION,
},
Data: b,
},
// Always pass the genetlink family ID and request flag.
c.family,
netlink.Request|flags,
)
}
// Is enables Error comparison with sentinel errors that are part of the
// Client's API contract such as os.ErrNotExist and os.ErrPermission.
func (e *Error) Is(target error) bool {
switch target {
case os.ErrNotExist:
// The queried interface is not supported by the ethtool APIs
// (EOPNOTSUPP) or does not exist at all (ENODEV).
return errors.Is(e.Err, unix.EOPNOTSUPP) || errors.Is(e.Err, unix.ENODEV)
case os.ErrPermission:
// The caller lacks permission to perform an operation.
return errors.Is(e.Err, unix.EPERM)
default:
return false
}
}
// parseLinkInfo parses LinkInfo structures from a slice of generic netlink
// messages.
func parseLinkInfo(msgs []genetlink.Message) ([]*LinkInfo, error) {
lis := make([]*LinkInfo, 0, len(msgs))
for _, m := range msgs {
ad, err := netlink.NewAttributeDecoder(m.Data)
if err != nil {
return nil, err
}
var li LinkInfo
for ad.Next() {
switch ad.Type() {
case unix.ETHTOOL_A_LINKINFO_HEADER:
ad.Nested(parseInterface(&li.Interface))
case unix.ETHTOOL_A_LINKINFO_PORT:
li.Port = Port(ad.Uint8())
}
}
if err := ad.Err(); err != nil {
return nil, err
}
lis = append(lis, &li)
}
return lis, nil
}
// parseLinkModes parses LinkMode structures from a slice of generic netlink
// messages.
func parseLinkModes(msgs []genetlink.Message) ([]*LinkMode, error) {
lms := make([]*LinkMode, 0, len(msgs))
for _, m := range msgs {
ad, err := netlink.NewAttributeDecoder(m.Data)
if err != nil {
return nil, err
}
var lm LinkMode
for ad.Next() {
switch ad.Type() {
case unix.ETHTOOL_A_LINKMODES_HEADER:
ad.Nested(parseInterface(&lm.Interface))
case unix.ETHTOOL_A_LINKMODES_OURS:
ad.Nested(parseAdvertisedLinkModes(&lm.Ours))
case unix.ETHTOOL_A_LINKMODES_PEER:
ad.Nested(parseAdvertisedLinkModes(&lm.Peer))
case unix.ETHTOOL_A_LINKMODES_SPEED:
lm.SpeedMegabits = int(ad.Uint32())
case unix.ETHTOOL_A_LINKMODES_DUPLEX:
lm.Duplex = Duplex(ad.Uint8())
}
}
if err := ad.Err(); err != nil {
return nil, err
}
lms = append(lms, &lm)
}
return lms, nil
}
// parseAdvertisedLinkModes decodes an ethtool compact bitset into the input
// slice of AdvertisedLinkModes.
func parseAdvertisedLinkModes(alms *[]AdvertisedLinkMode) func(*netlink.AttributeDecoder) error {
return func(ad *netlink.AttributeDecoder) error {
values, err := newBitset(ad)
if err != nil {
return err
}
for i, v := range values {
if v == 0 {
// No bits set, don't bother checking.
continue
}
// Test each bit to find which ones are set, and use that to look up
// the proper index in linkModes (accounting for the offset of 32
// for each value in the array) so we can find the correct link mode
// to attach. Note that the lookup assumes that there will never be
// any skipped bits in the linkModes table.
//
// Thanks 0x0f10, c_h_lunde, TheCi, and Wacholderbaer from Twitch
// chat for saving me from myself!
for j := 0; j < 32; j++ {
if v&(1<<j) != 0 {
m := linkModes[(32*i)+j]
*alms = append(*alms, AdvertisedLinkMode{
Index: int(m.bit),
Name: m.str,
})
}
}
}
return nil
}
}
// parseLinkState parses LinkState structures from a slice of generic netlink
// messages.
func parseLinkState(msgs []genetlink.Message) ([]*LinkState, error) {
lss := make([]*LinkState, 0, len(msgs))
for _, m := range msgs {
ad, err := netlink.NewAttributeDecoder(m.Data)
if err != nil {
return nil, err
}
var ls LinkState
for ad.Next() {
// TODO(mdlayher): try this out on fancier NICs to parse more of the
// extended state information.
switch ad.Type() {
case unix.ETHTOOL_A_LINKSTATE_HEADER:
ad.Nested(parseInterface(&ls.Interface))
case unix.ETHTOOL_A_LINKSTATE_LINK:
// Up/down is reported as a uint8 boolean.
ls.Link = ad.Uint8() != 0
}
}
if err := ad.Err(); err != nil {
return nil, err
}
lss = append(lss, &ls)
}
return lss, nil
}
// TODO: get these into x/sys/unix
const (
_ETHTOOL_A_FEC_UNSPEC = iota
_ETHTOOL_A_FEC_HEADER
_ETHTOOL_A_FEC_MODES
_ETHTOOL_A_FEC_AUTO
_ETHTOOL_A_FEC_ACTIVE
_ETHTOOL_A_FEC_STATS
)
// parseFEC parses FEC structures from a slice of generic netlink
// messages.
func parseFEC(msgs []genetlink.Message) ([]*FEC, error) {
fecs := make([]*FEC, 0, len(msgs))
for _, m := range msgs {
ad, err := netlink.NewAttributeDecoder(m.Data)
if err != nil {
return nil, err
}
var fec FEC
for ad.Next() {
switch ad.Type() {
case _ETHTOOL_A_FEC_HEADER:
ad.Nested(parseInterface(&fec.Interface))
case _ETHTOOL_A_FEC_MODES:
ad.Nested(parseFECModes(&fec.Modes))
if fec.Modes == 0 {
fec.Modes |= unix.ETHTOOL_FEC_OFF
}
case _ETHTOOL_A_FEC_AUTO:
fec.Auto = ad.Uint8() > 0
case _ETHTOOL_A_FEC_ACTIVE:
switch b := ad.Uint32(); b {
case unix.ETHTOOL_LINK_MODE_FEC_NONE_BIT:
fec.Active = unix.ETHTOOL_FEC_OFF
case unix.ETHTOOL_LINK_MODE_FEC_RS_BIT:
fec.Active = unix.ETHTOOL_FEC_RS
case unix.ETHTOOL_LINK_MODE_FEC_BASER_BIT:
fec.Active = unix.ETHTOOL_FEC_BASER
case unix.ETHTOOL_LINK_MODE_FEC_LLRS_BIT:
fec.Active = unix.ETHTOOL_FEC_LLRS
default:
return nil, fmt.Errorf("unsupported FEC link mode bit: %d", b)
}
}
}
if err := ad.Err(); err != nil {
return nil, err
}
fecs = append(fecs, &fec)
}
return fecs, nil
}
// parseFECModes decodes an ethtool compact bitset into the input FECModes.
func parseFECModes(m *FECModes) func(*netlink.AttributeDecoder) error {
return func(ad *netlink.AttributeDecoder) error {
values, err := newBitset(ad)
if err != nil {
return err
}
*m = 0
if values.test(unix.ETHTOOL_LINK_MODE_FEC_NONE_BIT) {
*m |= unix.ETHTOOL_FEC_OFF
}
if values.test(unix.ETHTOOL_LINK_MODE_FEC_RS_BIT) {
*m |= unix.ETHTOOL_FEC_RS
}
if values.test(unix.ETHTOOL_LINK_MODE_FEC_BASER_BIT) {
*m |= unix.ETHTOOL_FEC_BASER
}
if values.test(unix.ETHTOOL_LINK_MODE_FEC_LLRS_BIT) {
*m |= unix.ETHTOOL_FEC_LLRS
}
return nil
}
}
// parseWakeOnLAN parses WakeOnLAN structures from a slice of generic netlink
// messages.
func parseWakeOnLAN(msgs []genetlink.Message) ([]*WakeOnLAN, error) {
wols := make([]*WakeOnLAN, 0, len(msgs))
for _, m := range msgs {
ad, err := netlink.NewAttributeDecoder(m.Data)
if err != nil {
return nil, err
}
var wol WakeOnLAN
for ad.Next() {
switch ad.Type() {
case unix.ETHTOOL_A_WOL_HEADER:
ad.Nested(parseInterface(&wol.Interface))
case unix.ETHTOOL_A_WOL_MODES:
ad.Nested(parseWakeOnLANModes(&wol.Modes))
case unix.ETHTOOL_A_WOL_SOPASS:
// TODO(mdlayher): parse the password if we can find a NIC that
// supports it, probably using ad.Bytes.
}
}
if err := ad.Err(); err != nil {
return nil, err
}
wols = append(wols, &wol)
}
return wols, nil
}
// parseWakeOnLANModes decodes an ethtool compact bitset into the input WOLMode.
func parseWakeOnLANModes(m *WOLMode) func(*netlink.AttributeDecoder) error {
return func(ad *netlink.AttributeDecoder) error {
values, err := newBitset(ad)
if err != nil {
return err
}
// Assume the kernel will not sprout 25 more Wake-on-LAN modes and just
// inspect the first uint32 so we can populate the WOLMode bitmask for
// the caller.
if l := len(values); l > 1 {
panicf("ethtool: too many Wake-on-LAN mode uint32s in bitset: %d", l)
}
*m = WOLMode(values[0])
return nil
}
}
// parseInterface decodes information from a response header into the input
// Interface.
func parseInterface(ifi *Interface) func(*netlink.AttributeDecoder) error {
return func(ad *netlink.AttributeDecoder) error {
for ad.Next() {
switch ad.Type() {
case unix.ETHTOOL_A_HEADER_DEV_INDEX:
(*ifi).Index = int(ad.Uint32())
case unix.ETHTOOL_A_HEADER_DEV_NAME:
(*ifi).Name = ad.String()
}
}
return nil
}
}
func panicf(format string, a ...interface{}) {
panic(fmt.Sprintf(format, a...))
}