github.com/jaypipes/ghw@v0.21.1/pkg/net/net.go (about) 1 // 2 // Use and distribution licensed under the Apache license version 2. 3 // 4 // See the COPYING file in the root project directory for full text. 5 // 6 7 package net 8 9 import ( 10 "fmt" 11 12 "github.com/jaypipes/ghw/pkg/context" 13 "github.com/jaypipes/ghw/pkg/marshal" 14 "github.com/jaypipes/ghw/pkg/option" 15 ) 16 17 // NICCapability is a feature/capability of a Network Interface Controller 18 // (NIC) 19 type NICCapability struct { 20 // Name is the string name for the capability, e.g. 21 // "tcp-segmentation-offload" 22 Name string `json:"name"` 23 // IsEnabled is true if the capability is currently enabled on the NIC, 24 // false otherwise. 25 IsEnabled bool `json:"is_enabled"` 26 // CanEnable is true if the capability can be enabled on the NIC, false 27 // otherwise. 28 CanEnable bool `json:"can_enable"` 29 } 30 31 // NIC contains information about a single Network Interface Controller (NIC). 32 type NIC struct { 33 // Name is the string identifier the system gave this NIC. 34 Name string `json:"name"` 35 // MACAddress is the Media Access Control (MAC) address of this NIC. 36 MACAddress string `json:"mac_address"` 37 // DEPRECATED: Please use MACAddress instead. 38 MacAddress string `json:"-"` 39 // IsVirtual is true if the NIC is entirely virtual/emulated, false 40 // otherwise. 41 IsVirtual bool `json:"is_virtual"` 42 // Capabilities is a slice of pointers to `NICCapability` structs 43 // describing a feature/capability of this NIC. 44 Capabilities []*NICCapability `json:"capabilities"` 45 // PCIAddress is a pointer to the PCI address for this NIC, or nil if there 46 // is no PCI address for this NIC. 47 PCIAddress *string `json:"pci_address,omitempty"` 48 // Speed is a string describing the link speed of this NIC, e.g. "1000Mb/s" 49 Speed string `json:"speed"` 50 // Duplex is a string indicating the current duplex setting of this NIC, 51 // e.g. "Full" 52 Duplex string `json:"duplex"` 53 // SupportedLinkModes is a slice of strings containing the supported link 54 // modes of this NIC, e.g. "10baseT/Half", "1000baseT/Full", etc. 55 SupportedLinkModes []string `json:"supported_link_modes,omitempty"` 56 // SupportedPorts is a slice of strings containing the supported physical 57 // ports on this NIC, e.g. "Twisted Pair" 58 SupportedPorts []string `json:"supported_ports,omitempty"` 59 // SupportedFECModes is a slice of strings containing the supported Forward 60 // Error Correction (FEC) modes for this NIC. 61 SupportedFECModes []string `json:"supported_fec_modes,omitempty"` 62 // AdvertiseLinkModes is a slice of strings containing the advertised 63 // (during auto-negotiation) link modes of this NIC, e.g. "10baseT/Half", 64 // "1000baseT/Full", etc. 65 AdvertisedLinkModes []string `json:"advertised_link_modes,omitempty"` 66 // AvertisedFECModes is a slice of strings containing the advertised 67 // (during auto-negotiation) Forward Error Correction (FEC) modes for this 68 // NIC. 69 AdvertisedFECModes []string `json:"advertised_fec_modes,omitempty"` 70 // TODO(fromani): add other hw addresses (USB) when we support them 71 } 72 73 // String returns a short string with information about the NIC capability. 74 func (nc *NICCapability) String() string { 75 return fmt.Sprintf( 76 "{Name:%s IsEnabled:%t CanEnable:%t}", 77 nc.Name, 78 nc.IsEnabled, 79 nc.CanEnable, 80 ) 81 } 82 83 // String returns a short string with information about the NIC. 84 func (n *NIC) String() string { 85 isVirtualStr := "" 86 if n.IsVirtual { 87 isVirtualStr = " (virtual)" 88 } 89 return fmt.Sprintf( 90 "%s%s", 91 n.Name, 92 isVirtualStr, 93 ) 94 } 95 96 // Info describes all network interface controllers (NICs) in the host system. 97 type Info struct { 98 ctx *context.Context 99 // NICs is a slice of pointers to `NIC` structs describing the network 100 // interface controllers (NICs) on the host system. 101 NICs []*NIC `json:"nics"` 102 } 103 104 // New returns a pointer to an Info struct that contains information about the 105 // network interface controllers (NICs) on the host system 106 func New(opts ...*option.Option) (*Info, error) { 107 ctx := context.New(opts...) 108 info := &Info{ctx: ctx} 109 if err := ctx.Do(info.load); err != nil { 110 return nil, err 111 } 112 return info, nil 113 } 114 115 // String returns a short string with information about the networking on the 116 // host system. 117 func (i *Info) String() string { 118 return fmt.Sprintf( 119 "net (%d NICs)", 120 len(i.NICs), 121 ) 122 } 123 124 // simple private struct used to encapsulate net information in a 125 // top-level "net" YAML/JSON map/object key 126 type netPrinter struct { 127 Info *Info `json:"network"` 128 } 129 130 // YAMLString returns a string with the net information formatted as YAML 131 // under a top-level "net:" key 132 func (i *Info) YAMLString() string { 133 return marshal.SafeYAML(i.ctx, netPrinter{i}) 134 } 135 136 // JSONString returns a string with the net information formatted as JSON 137 // under a top-level "net:" key 138 func (i *Info) JSONString(indent bool) string { 139 return marshal.SafeJSON(i.ctx, netPrinter{i}, indent) 140 }