|
| 1 | +package aws |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "sigs.k8s.io/controller-runtime/pkg/log" |
| 8 | + |
| 9 | + sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1" |
| 10 | + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts" |
| 11 | + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/helper" |
| 12 | + plugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins" |
| 13 | + virtualplugin "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/plugins/virtual" |
| 14 | + "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + // metadataBaseURL is the base URL for the EC2 instance metadata service. |
| 19 | + metadataBaseURL = "http://169.254.169.254/latest/meta-data/" |
| 20 | + // macsPath is the path to list MAC addresses. Note the trailing slash, |
| 21 | + // which is standard for directory-like listings in the metadata service. |
| 22 | + macsPath = "network/interfaces/macs/" |
| 23 | + // subnetIDSuffix is appended to a specific MAC address path to get its subnet ID. |
| 24 | + subnetIDSuffix = "/subnet-id" |
| 25 | +) |
| 26 | + |
| 27 | +type Aws struct { |
| 28 | + hostHelpers helper.HostHelpersInterface |
| 29 | + loadedDevicesInfo sriovnetworkv1.InterfaceExts |
| 30 | +} |
| 31 | + |
| 32 | +func New(hostHelpers helper.HostHelpersInterface) (*Aws, error) { |
| 33 | + return &Aws{ |
| 34 | + hostHelpers: hostHelpers, |
| 35 | + }, nil |
| 36 | +} |
| 37 | + |
| 38 | +func (a *Aws) Init() error { |
| 39 | + ns, err := a.hostHelpers.GetCheckPointNodeState() |
| 40 | + if err != nil { |
| 41 | + return err |
| 42 | + } |
| 43 | + |
| 44 | + if ns == nil { |
| 45 | + err = a.createDevicesInfo() |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + a.createDevicesInfoFromNodeStatus(ns) |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +func (a *Aws) Name() string { |
| 54 | + return string(consts.AWS) |
| 55 | +} |
| 56 | + |
| 57 | +func (a *Aws) GetVendorPlugins(_ *sriovnetworkv1.SriovNetworkNodeState) (plugin.VendorPlugin, []plugin.VendorPlugin, error) { |
| 58 | + virtual, err := virtualplugin.NewVirtualPlugin(a.hostHelpers) |
| 59 | + return virtual, []plugin.VendorPlugin{}, err |
| 60 | +} |
| 61 | + |
| 62 | +func (a *Aws) SystemdGetVendorPlugin(_ string) (plugin.VendorPlugin, error) { |
| 63 | + return nil, fmt.Errorf("aws platform not supported in systemd") |
| 64 | +} |
| 65 | + |
| 66 | +// DiscoverSriovDevices discovers VFs on a virtual platform |
| 67 | +func (a *Aws) DiscoverSriovDevices() ([]sriovnetworkv1.InterfaceExt, error) { |
| 68 | + funcLog := log.Log.WithName("DiscoverSriovDevices") |
| 69 | + log.Log.V(2).Info("discovering sriov devices") |
| 70 | + |
| 71 | + // TODO: check if we want to support hot plug here in the future |
| 72 | + for idx, iface := range a.loadedDevicesInfo { |
| 73 | + hasDriver, driver := a.hostHelpers.HasDriver(iface.PciAddress) |
| 74 | + if !hasDriver { |
| 75 | + funcLog.Error(nil, "device doesn't have a driver, skipping", |
| 76 | + "iface", iface) |
| 77 | + continue |
| 78 | + } |
| 79 | + iface.Driver = driver |
| 80 | + |
| 81 | + if mtu := a.hostHelpers.GetNetdevMTU(iface.PciAddress); mtu > 0 { |
| 82 | + iface.Mtu = mtu |
| 83 | + } |
| 84 | + |
| 85 | + if name := a.hostHelpers.TryToGetVirtualInterfaceName(iface.PciAddress); name != "" { |
| 86 | + iface.Name = name |
| 87 | + if macAddr := a.hostHelpers.GetNetDevMac(name); macAddr != "" { |
| 88 | + iface.Mac = macAddr |
| 89 | + } |
| 90 | + iface.LinkSpeed = a.hostHelpers.GetNetDevLinkSpeed(name) |
| 91 | + iface.LinkType = a.hostHelpers.GetLinkType(name) |
| 92 | + } |
| 93 | + if len(iface.VFs) != 1 { |
| 94 | + log.Log.Error(nil, "only one vf should exist", "iface", iface) |
| 95 | + return nil, fmt.Errorf("unexpected number of vfs found for device %s", iface.Name) |
| 96 | + } |
| 97 | + iface.VFs[0] = sriovnetworkv1.VirtualFunction{ |
| 98 | + PciAddress: iface.PciAddress, |
| 99 | + Driver: driver, |
| 100 | + VfID: 0, |
| 101 | + Vendor: iface.Vendor, |
| 102 | + DeviceID: iface.DeviceID, |
| 103 | + Mtu: iface.Mtu, |
| 104 | + Mac: iface.Mac, |
| 105 | + } |
| 106 | + a.loadedDevicesInfo[idx] = iface |
| 107 | + } |
| 108 | + return a.loadedDevicesInfo, nil |
| 109 | +} |
| 110 | + |
| 111 | +func (a *Aws) DiscoverBridges() (sriovnetworkv1.Bridges, error) { |
| 112 | + if vars.ManageSoftwareBridges { |
| 113 | + return sriovnetworkv1.Bridges{}, fmt.Errorf("bridge configuration not supported on openstack platform") |
| 114 | + } |
| 115 | + return sriovnetworkv1.Bridges{}, nil |
| 116 | +} |
| 117 | + |
| 118 | +// CreateOpenstackDevicesInfo create the openstack device info map |
| 119 | +func (a *Aws) createDevicesInfo() error { |
| 120 | + funcLog := log.Log.WithName("getDataFromMetadataService") |
| 121 | + a.loadedDevicesInfo = make(sriovnetworkv1.InterfaceExts, 0) |
| 122 | + funcLog.Info("getting aws network info from metadata server") |
| 123 | + metaData, err := a.hostHelpers.HTTPGetFetchData(metadataBaseURL + macsPath) |
| 124 | + if err != nil { |
| 125 | + return fmt.Errorf("error getting aws meta_data from %s: %v", metadataBaseURL+macsPath, err) |
| 126 | + } |
| 127 | + |
| 128 | + // If the endpoint returns an empty body (e.g., no MACs available or an issue), |
| 129 | + // treat it as no MACs found rather than an error. The caller can then decide how to handle an empty list. |
| 130 | + if metaData == "" { |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + // MAC addresses are returned separated by newlines, each ending with a '/'. |
| 135 | + // Example raw response: "0e:1a:95:aa:12:a1/\n0e:92:d3:ee:52:1b/" |
| 136 | + macEntries := strings.Split(metaData, "\n") |
| 137 | + if len(macEntries) == 0 { |
| 138 | + return nil |
| 139 | + } |
| 140 | + |
| 141 | + macAddressToSubNetID := map[string]string{} |
| 142 | + for _, macEntry := range macEntries { |
| 143 | + subnetIDURL := metadataBaseURL + macsPath + macEntry + subnetIDSuffix |
| 144 | + subnetIDData, err := a.hostHelpers.HTTPGetFetchData(subnetIDURL) |
| 145 | + if err != nil { |
| 146 | + return fmt.Errorf("error getting aws subnet_id from %s: %v", subnetIDURL, err) |
| 147 | + } |
| 148 | + |
| 149 | + if subnetIDData == "" { |
| 150 | + return fmt.Errorf("empty subnet_id from %s: %v", subnetIDURL, err) |
| 151 | + } |
| 152 | + macAddressToSubNetID[strings.ReplaceAll(macEntry, "/", "")] = subnetIDData |
| 153 | + } |
| 154 | + |
| 155 | + pfList, err := a.hostHelpers.DiscoverSriovVirtualDevices() |
| 156 | + if err != nil { |
| 157 | + return err |
| 158 | + } |
| 159 | + |
| 160 | + for _, iface := range pfList { |
| 161 | + subnetID, exist := macAddressToSubNetID[iface.Mac] |
| 162 | + if !exist { |
| 163 | + continue |
| 164 | + } |
| 165 | + |
| 166 | + subnetID = strings.TrimPrefix(subnetID, "subnet-") |
| 167 | + iface.NetFilter = fmt.Sprintf("aws/NetworkID:%s", subnetID) |
| 168 | + iface.TotalVfs = 1 |
| 169 | + iface.NumVfs = 1 |
| 170 | + |
| 171 | + vf := sriovnetworkv1.VirtualFunction{ |
| 172 | + PciAddress: iface.PciAddress, |
| 173 | + Driver: iface.Driver, |
| 174 | + VfID: 0, |
| 175 | + Vendor: iface.Vendor, |
| 176 | + DeviceID: iface.DeviceID, |
| 177 | + Mtu: iface.Mtu, |
| 178 | + Mac: iface.Mac, |
| 179 | + } |
| 180 | + iface.VFs = append(iface.VFs, vf) |
| 181 | + a.loadedDevicesInfo = append(a.loadedDevicesInfo, iface) |
| 182 | + } |
| 183 | + funcLog.V(2).Info("loaded devices info from metadata server", "devices", a.loadedDevicesInfo) |
| 184 | + return nil |
| 185 | +} |
| 186 | + |
| 187 | +func (a *Aws) createDevicesInfoFromNodeStatus(networkState *sriovnetworkv1.SriovNetworkNodeState) { |
| 188 | + a.loadedDevicesInfo = networkState.Status.Interfaces |
| 189 | +} |
0 commit comments