Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions test_suites/networkconfig/common_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright 2026 Google LLC
//
// 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 networkconfig

import (
"context"
_ "embed" // Required for //go:embed.
"fmt"
"path"
"regexp"
"strings"

"github.com/GoogleCloudPlatform/cloud-image-tests/utils"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"

pb "github.com/GoogleCloudPlatform/cloud-image-tests/test_suites/networkconfig/config_expectations_proto"
)

//go:embed config_expectations/config_expectations.textproto
var configExpectationsBytes []byte

func parseConfigExpectations() (*pb.ConfigExpectations, error) {
var configExpectations pb.ConfigExpectations
if err := prototext.Unmarshal(configExpectationsBytes, &configExpectations); err != nil {
return nil, fmt.Errorf("unmarshaling embedded config expectations: %w", err)
}
return &configExpectations, nil
}

// guestOSConstraints fetches the guest image from MDS and merges all matching constraints.
func guestOSConstraints(ctx context.Context, constraints []*pb.GuestOsConstraint) (*pb.GuestOsConstraint, error) {
image, err := utils.GetMetadata(ctx, "instance", "image")
if err != nil {
return nil, fmt.Errorf("getting instance image from metadata: %w", err)
}
imageName := path.Base(strings.TrimSpace(image))

merged := &pb.GuestOsConstraint{}
for _, c := range constraints {
matched, err := regexp.MatchString(c.GetOsPattern(), imageName)
if err != nil {
return nil, fmt.Errorf("matching os_pattern %q: %w", c.GetOsPattern(), err)
}
if matched {
proto.Merge(merged, c)
}
}
return merged, nil
}
214 changes: 135 additions & 79 deletions test_suites/networkconfig/deviceconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
package networkconfig

import (
_ "embed"
"context"
"fmt"
fs "io/fs"
"os"
Expand All @@ -32,7 +32,6 @@ import (
"github.com/GoogleCloudPlatform/cloud-image-tests/utils"
"github.com/GoogleCloudPlatform/cloud-image-tests/utils/networkutils"
"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/encoding/prototext"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/testing/protocmp"

Expand All @@ -55,9 +54,6 @@ var (
idpfTxRxIRQRe = regexp.MustCompile(`.*TxRx-(\d+)`)
)

//go:embed config_expectations/config_expectations.textproto
var configExpectationsBytes []byte

type systemKey struct {
machineType string
nicTypes []string
Expand All @@ -73,7 +69,7 @@ func matchKey(a, b *systemKey) bool {
return slices.Equal(a.nicTypes, b.nicTypes)
}

func expectedConfigForMachine(configExpectations *pb.ConfigExpectations, machineType string, nicTypes []string, kernelVersion string) (*pb.SystemConfig, error) {
func expectedConfigForMachine(ctx context.Context, configExpectations *pb.ConfigExpectations, machineType string, nicTypes []string, kernelVersion string) (*pb.SystemConfig, error) {
thisSystemKey := &systemKey{machineType: machineType, nicTypes: nicTypes}
for _, config := range configExpectations.GetConfigExpectations() {
var configNicTypes []string
Expand All @@ -91,7 +87,19 @@ func expectedConfigForMachine(configExpectations *pb.ConfigExpectations, machine
if maxVer := config.GetGuestOsSelector().GetMaxKernelVersion(); maxVer != "" && networkutils.CompareKernelVersion(kernelVersion, maxVer) > 0 {
continue
}
return config, nil
constraints, err := guestOSConstraints(ctx, configExpectations.GetGuestOsConstraints())
if err != nil {
return nil, fmt.Errorf("getting guest OS constraints: %w", err)
}
adjusted := proto.CloneOf(config)
if !constraints.GetIncludesIrdmaDriver() {
for _, nic := range adjusted.GetNics() {
if nic.GetType() == networkutils.NICTypeIRDMA {
nic.SetType(networkutils.NICTypeIDPF)
}
}
}
return adjusted, nil
}
}
return nil, fmt.Errorf("no config expectation found for machine type %q, nic types %v, and kernel version %q", machineType, nicTypes, kernelVersion)
Expand Down Expand Up @@ -486,53 +494,102 @@ func numaNodeForInterface(ifaceName string) (*int32, error) {
return proto.Int32(int32(numaNode)), nil
}

func buildQueuesXPSOnly(txQueueToXPS map[int]string) ([]*pb.TxQueue, error) {
var txQueues []*pb.TxQueue
for txQueueIndex, xpsCPUListStr := range txQueueToXPS {
txQueues = append(txQueues, pb.TxQueue_builder{
Index: proto.Int32(int32(txQueueIndex)),
XpsCpulist: proto.String(xpsCPUListStr),
}.Build())
func buildQueues(irqCPULists *irqCPULists, txQueueToXPS map[int]string) ([]*pb.TxQueue, []*pb.RxQueue) {
// Some drivers don't have a 1:1 queue-index IRQ mapping: MRDMA (mlx5_core)
// matches no IRQ names in queueIndexToIRQs, and IRDMA (idpf) can expose fewer
// TxRx vectors than TX queues (e.g. 7 vectors for 8 queues). Merge by queue
// index rather than failing here; cmp.Diff enforces expected IRQs.
txIndices := make(map[int]bool, len(txQueueToXPS))
for txQueueIndex := range txQueueToXPS {
txIndices[txQueueIndex] = true
}
if irqCPULists != nil {
for txQueueIndex := range irqCPULists.txIRQAffinity {
txIndices[txQueueIndex] = true
}
}
return txQueues, nil
}

func buildQueues(nicType string, irqCPULists *irqCPULists, txQueueToXPS map[int]string) ([]*pb.TxQueue, []*pb.RxQueue, error) {
// The guest environment don't configure IRQs for IRDMA and MRDMA.
// We may wish to assert on this in the future (i.e. values from the driver), but for now we
// ignore it.
switch nicType {
case networkutils.NICTypeIRDMA, networkutils.NICTypeMRDMA:
txQueues, err := buildQueuesXPSOnly(txQueueToXPS)
if err != nil {
return nil, nil, err
var txQueues []*pb.TxQueue
for txQueueIndex := range txIndices {
txQueueBuilder := pb.TxQueue_builder{
Index: proto.Int32(int32(txQueueIndex)),
}
return txQueues, nil, nil
default:
// continue
if xpsCPUListStr, ok := txQueueToXPS[txQueueIndex]; ok {
txQueueBuilder.XpsCpulist = proto.String(xpsCPUListStr)
}
if irqCPULists != nil {
if irqList, ok := irqCPULists.txIRQAffinity[txQueueIndex]; ok {
txQueueBuilder.IrqCpulist = proto.String(irqList)
}
}
txQueues = append(txQueues, txQueueBuilder.Build())
}

if len(irqCPULists.txIRQAffinity) != len(txQueueToXPS) {
return nil, nil, fmt.Errorf("number of tx queues in IRQs (%d) does not match number of tx queues in XPS (%d)", len(irqCPULists.txIRQAffinity), len(txQueueToXPS))
var rxQueues []*pb.RxQueue
if irqCPULists != nil {
for rxQueueIndex, irqList := range irqCPULists.rxIRQAffinity {
rxQueues = append(rxQueues, pb.RxQueue_builder{
Index: proto.Int32(int32(rxQueueIndex)),
IrqCpulist: proto.String(irqList),
}.Build())
}
}
return txQueues, rxQueues
}

var txQueues []*pb.TxQueue
for txQueueIndex, irqList := range irqCPULists.txIRQAffinity {
txQueues = append(txQueues, pb.TxQueue_builder{
Index: proto.Int32(int32(txQueueIndex)),
IrqCpulist: proto.String(irqList),
XpsCpulist: proto.String(txQueueToXPS[txQueueIndex]),
}.Build())
func nicOmitsIRQs(nic *pb.NicExpectation) bool {
if len(nic.GetRxQueues()) > 0 {
return false
}
for _, txQueue := range nic.GetTxQueues() {
if txQueue.HasIrqCpulist() {
return false
}
}
return true
}

var rxQueues []*pb.RxQueue
for rxQueueIndex, irqList := range irqCPULists.rxIRQAffinity {
rxQueues = append(rxQueues, pb.RxQueue_builder{
Index: proto.Int32(int32(rxQueueIndex)),
IrqCpulist: proto.String(irqList),
}.Build())
func ignoreOmittedIRDMAIRQs(mdsIfaces []networkutils.NetworkInterface, want *pb.SystemConfig) cmp.Option {
ignoredNICs := make(map[int]bool)
for i, wantNic := range want.GetNics() {
if i >= len(mdsIfaces) || mdsIfaces[i].NICType != networkutils.NICTypeIRDMA {
continue
}
if nicOmitsIRQs(wantNic) {
ignoredNICs[i] = true
}
}
if len(ignoredNICs) == 0 {
return cmp.Options{}
}
return txQueues, rxQueues, nil
return cmp.FilterPath(func(p cmp.Path) bool {
// Ignore rx_queues and tx_queues[*].irq_cpulist on IRDMA NICs whose
// expectation omits IRQs.
lastMapIdx, ok := p.Last().(cmp.MapIndex)
if !ok {
return false
}
fieldName, ok := lastMapIdx.Key().Interface().(string)
if !ok || (fieldName != "rx_queues" && fieldName != "irq_cpulist") {
return false
}
for i := 0; i < len(p)-1; i++ {
mi, ok := p[i].(cmp.MapIndex)
if !ok || mi.Key().Interface() != "nics" {
continue
}
// protocmp wraps repeated message fields in an interface value, so a
// TypeAssertion step sits between MapIndex("nics") and SliceIndex(idx).
for j := i + 1; j < len(p); j++ {
if si, ok := p[j].(cmp.SliceIndex); ok {
wantIdx, gotIdx := si.SplitKeys()
return wantIdx == gotIdx && ignoredNICs[wantIdx]
}
}
return false
}
return false
}, cmp.Ignore())
}

func thisSystemConfig(mdsIfaces []networkutils.NetworkInterface) (*pb.SystemConfig, error) {
Expand All @@ -554,27 +611,21 @@ func thisSystemConfig(mdsIfaces []networkutils.NetworkInterface) (*pb.SystemConf
return nil, fmt.Errorf("parsing ethtool -l output for %q: %w", nicName, err)
}

var queueToIRQs *irqCPULists
if nicType != networkutils.NICTypeIRDMA && nicType != networkutils.NICTypeMRDMA {
irqs, err := deviceIRQs(nicName)
if err != nil {
return nil, fmt.Errorf("getting device IRQs for %q: %w", nicName, err)
}
queueToIRQs, err = queueIndexToIRQs(irqs, queueCounts)
if err != nil {
return nil, fmt.Errorf("converting IRQs to queue index to IRQs for %q: %w", nicName, err)
}
irqs, err := deviceIRQs(nicName)
if err != nil {
return nil, fmt.Errorf("getting device IRQs for %q: %w", nicName, err)
}
queueToIRQs, err := queueIndexToIRQs(irqs, queueCounts)
if err != nil {
return nil, fmt.Errorf("converting IRQs to queue index to IRQs for %q: %w", nicName, err)
}

txQueueToXPS, err := queueIndexToXPS(nicName)
if err != nil {
return nil, fmt.Errorf("getting XPS for %q: %w", nicName, err)
}

txQueues, rxQueues, err := buildQueues(nicType, queueToIRQs, txQueueToXPS)
if err != nil {
return nil, fmt.Errorf("building queues for %q: %w", nicName, err)
}
txQueues, rxQueues := buildQueues(queueToIRQs, txQueueToXPS)

numaNode, err := numaNodeForInterface(nicName)
if err != nil {
Expand Down Expand Up @@ -607,25 +658,15 @@ func thisSystemConfig(mdsIfaces []networkutils.NetworkInterface) (*pb.SystemConf
}.Build(), nil
}

func TestDeviceConfig(t *testing.T) {
ctx := utils.Context(t)

var configExpectations pb.ConfigExpectations
if err := prototext.Unmarshal(configExpectationsBytes, &configExpectations); err != nil {
t.Fatalf("failed to unmarshal embedded config expectations: %v", err)
}

mdsIfaces, err := networkutils.ListMDSIfaces(ctx)
switch {
case err != nil:
t.Fatalf("ListMDSIfaces(ctx) = err %v want nil", err)
case len(mdsIfaces) == 0:
t.Fatalf("no network interfaces found in metadata")
func expectedSystemConfig(ctx context.Context, mdsIfaces []networkutils.NetworkInterface) (*pb.SystemConfig, error) {
configExpectations, err := parseConfigExpectations()
if err != nil {
return nil, fmt.Errorf("parsing config expectations: %w", err)
}

machineTypePath, err := utils.GetMetadata(ctx, "instance", "machine-type")
if err != nil {
t.Fatalf("failed to get machine type: %v", err)
return nil, fmt.Errorf("getting machine type: %w", err)
}
machineType := filepath.Base(machineTypePath)

Expand All @@ -637,21 +678,35 @@ func TestDeviceConfig(t *testing.T) {
kernelVersionCmd := exec.CommandContext(ctx, "uname", "-r")
kernelVersionOut, err := kernelVersionCmd.CombinedOutput()
if err != nil {
t.Fatalf("error getting kernel version: %v", err)
return nil, fmt.Errorf("getting kernel version: %w", err)
}
kernelVersion := strings.TrimSpace(string(kernelVersionOut))

wantSystemConfig, err := expectedConfigForMachine(&configExpectations, machineType, nicTypes, kernelVersion)
return expectedConfigForMachine(ctx, configExpectations, machineType, nicTypes, kernelVersion)
}

func TestDeviceConfig(t *testing.T) {
ctx := utils.Context(t)

mdsIfaces, err := networkutils.ListMDSIfaces(ctx)
switch {
case err != nil:
t.Fatalf("ListMDSIfaces(ctx) = err %v want nil", err)
case len(mdsIfaces) == 0:
t.Fatalf("no network interfaces found in metadata")
}

wantSystemConfig, err := expectedSystemConfig(ctx, mdsIfaces)
if err != nil {
t.Fatalf("expectedConfigForMachine(&configExpectations, %q, %v, %q) = err: %v, want nil", machineType, nicTypes, kernelVersion, err)
t.Fatalf("expectedSystemConfig(ctx, mdsIfaces) = err: %v, want nil", err)
}

gotSystemConfig, err := thisSystemConfig(mdsIfaces)
if err != nil {
t.Fatalf("thisSystemConfig(mdsIfaces) = err: %v, want nil", err)
}

// Only assert the ring size if they're set in the expected config.
// Only assert optional fields if they're set in the expected config.
for i, wantNic := range wantSystemConfig.GetNics() {
if i >= len(gotSystemConfig.GetNics()) {
break
Expand All @@ -670,9 +725,10 @@ func TestDeviceConfig(t *testing.T) {

if diff := cmp.Diff(wantSystemConfig, gotSystemConfig,
protocmp.Transform(),
protocmp.SortRepeatedFields(&pb.NicExpectation{}, "tx_queues"),
protocmp.SortRepeatedFields(&pb.NicExpectation{}, "rx_queues"),
protocmp.SortRepeated(func(a, b *pb.TxQueue) bool { return a.GetIndex() < b.GetIndex() }),
protocmp.SortRepeated(func(a, b *pb.RxQueue) bool { return a.GetIndex() < b.GetIndex() }),
protocmp.IgnoreFields(&pb.SystemConfig{}, "description", "machine_type", "guest_os_selector"),
ignoreOmittedIRDMAIRQs(mdsIfaces, wantSystemConfig),
); diff != "" {
t.Errorf("SystemConfig mismatch (-want +got):\n%s", diff)
}
Expand Down
Loading