Skip to content

Commit be936c6

Browse files
authored
Merge pull request #103 from kzys/patch-network
Add UpdateGuestNetworkInterface
2 parents a585107 + 5d48116 commit be936c6

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

firecracker.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,27 @@ func (f *Client) PutGuestNetworkInterfaceByID(ctx context.Context, ifaceID strin
141141
return f.client.Operations.PutGuestNetworkInterfaceByID(cfg)
142142
}
143143

144+
// PatchGuestNetworkInterfaceByIDOpt is a functional option to be used for the
145+
// PatchGuestNetworkInterfaceByID API in setting any additional optional fields.
146+
type PatchGuestNetworkInterfaceByIDOpt func(*ops.PatchGuestNetworkInterfaceByIDParams)
147+
148+
// PatchGuestNetworkInterfaceByID is a wrapper for the swagger generated client to make calling of the
149+
// API easier.
150+
func (f *Client) PatchGuestNetworkInterfaceByID(ctx context.Context, ifaceID string, ifaceCfg *models.PartialNetworkInterface, opts ...PatchGuestNetworkInterfaceByIDOpt) (*ops.PatchGuestNetworkInterfaceByIDNoContent, error) {
151+
timeout, cancel := context.WithTimeout(ctx, firecrackerRequestTimeout)
152+
defer cancel()
153+
154+
cfg := ops.NewPatchGuestNetworkInterfaceByIDParamsWithContext(timeout)
155+
cfg.SetBody(ifaceCfg)
156+
cfg.SetIfaceID(ifaceID)
157+
158+
for _, opt := range opts {
159+
opt(cfg)
160+
}
161+
162+
return f.client.Operations.PatchGuestNetworkInterfaceByID(cfg)
163+
}
164+
144165
// PutGuestDriveByIDOpt is a functional option to be used for the
145166
// PutGuestDriveByID API in setting any additional optional fields.
146167
type PutGuestDriveByIDOpt func(*ops.PutGuestDriveByIDParams)

machine.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,14 @@ type NetworkInterface struct {
192192
OutRateLimiter *models.RateLimiter
193193
}
194194

195+
// RateLimiterSet represents a pair of RateLimiters (inbound and outbound)
196+
type RateLimiterSet struct {
197+
// InRateLimiter limits the incoming bytes.
198+
InRateLimiter *models.RateLimiter
199+
// OutRateLimiter limits the outgoing bytes.
200+
OutRateLimiter *models.RateLimiter
201+
}
202+
195203
// VsockDevice represents a vsock connection between the host and the guest
196204
// microVM.
197205
type VsockDevice struct {
@@ -564,6 +572,26 @@ func (m *Machine) createNetworkInterface(ctx context.Context, iface NetworkInter
564572
return err
565573
}
566574

575+
// UpdateGuestNetworkInterfaceRateLimit modifies the specified network interface's rate limits
576+
func (m *Machine) UpdateGuestNetworkInterfaceRateLimit(ctx context.Context, ifaceID string, rateLimiters RateLimiterSet, opts ...PatchGuestNetworkInterfaceByIDOpt) error {
577+
iface := models.PartialNetworkInterface{
578+
IfaceID: &ifaceID,
579+
}
580+
if rateLimiters.InRateLimiter != nil {
581+
iface.RxRateLimiter = rateLimiters.InRateLimiter
582+
}
583+
if rateLimiters.OutRateLimiter != nil {
584+
iface.TxRateLimiter = rateLimiters.InRateLimiter
585+
}
586+
if _, err := m.client.PatchGuestNetworkInterfaceByID(ctx, ifaceID, &iface, opts...); err != nil {
587+
m.logger.Errorf("Update network interface failed: %s: %v", ifaceID, err)
588+
return err
589+
}
590+
591+
m.logger.Infof("Updated network interface: %s", ifaceID)
592+
return nil
593+
}
594+
567595
// attachDrive attaches a secondary block device
568596
func (m *Machine) attachDrive(ctx context.Context, dev models.Drive) error {
569597
hostPath := StringValue(dev.PathOnHost)

machine_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ func TestMicroVMExecution(t *testing.T) {
275275

276276
vmlinuxPath := getVmlinuxPath(t)
277277

278+
networkIfaces := []NetworkInterface{
279+
{
280+
MacAddress: "01-23-45-67-89-AB-CD-EF",
281+
HostDevName: "tap0",
282+
},
283+
}
284+
278285
cfg := Config{
279286
SocketPath: socketPath,
280287
LogFifo: logFifo,
@@ -288,6 +295,7 @@ func TestMicroVMExecution(t *testing.T) {
288295
},
289296
Debug: true,
290297
DisableValidation: true,
298+
NetworkInterfaces: networkIfaces,
291299
}
292300

293301
ctx := context.Background()
@@ -332,6 +340,7 @@ func TestMicroVMExecution(t *testing.T) {
332340
t.Run("TestAttachVsock", func(t *testing.T) { testAttachVsock(ctx, t, m) })
333341
t.Run("SetMetadata", func(t *testing.T) { testSetMetadata(ctx, t, m) })
334342
t.Run("TestUpdateGuestDrive", func(t *testing.T) { testUpdateGuestDrive(ctx, t, m) })
343+
t.Run("TestUpdateGuestNetworkInterface", func(t *testing.T) { testUpdateGuestNetworkInterface(ctx, t, m) })
335344
t.Run("TestStartInstance", func(t *testing.T) { testStartInstance(ctx, t, m) })
336345

337346
// Let the VMM start and stabilize...
@@ -489,6 +498,18 @@ func testUpdateGuestDrive(ctx context.Context, t *testing.T, m *Machine) {
489498
}
490499
}
491500

501+
func testUpdateGuestNetworkInterface(ctx context.Context, t *testing.T, m *Machine) {
502+
rateLimitSet := RateLimiterSet{
503+
InRateLimiter: NewRateLimiter(
504+
TokenBucketBuilder{}.WithBucketSize(10).WithRefillDuration(10).Build(),
505+
TokenBucketBuilder{}.WithBucketSize(10).WithRefillDuration(10).Build(),
506+
),
507+
}
508+
if err := m.UpdateGuestNetworkInterfaceRateLimit(ctx, "1", rateLimitSet); err != nil {
509+
t.Fatalf("Failed to update the network interface %v", err)
510+
}
511+
}
512+
492513
func testCreateNetworkInterfaceByID(ctx context.Context, t *testing.T, m *Machine) {
493514
if skipTuntap {
494515
t.Skip("Skipping: tuntap tests explicitly disabled")

machineiface.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,5 @@ type MachineIface interface {
2929
Wait(context.Context) error
3030
SetMetadata(context.Context, interface{}) error
3131
UpdateGuestDrive(context.Context, string, string, ...PatchGuestDriveByIDOpt) error
32+
UpdateGuestNetworkInterfaceRateLimit(context.Context, string, RateLimiterSet, ...PatchGuestNetworkInterfaceByIDOpt) error
3233
}

0 commit comments

Comments
 (0)