forked from siderolabs/talos
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support hardware watchdog timers
Only enabled when activated by config, disabled on shutdown/reboot Fixes siderolabs#8284 Signed-off-by: Dmitry Sharshakov <[email protected]>
- Loading branch information
Showing
7 changed files
with
168 additions
and
0 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
internal/app/machined/pkg/controllers/runtime/watchdog_timer.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
// This Source Code Form is subject to the terms of the Mozilla Public | ||
// License, v. 2.0. If a copy of the MPL was not distributed with this | ||
// file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
|
||
package runtime | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"syscall" | ||
"time" | ||
"unsafe" | ||
|
||
"github.com/cosi-project/runtime/pkg/controller" | ||
"github.com/cosi-project/runtime/pkg/safe" | ||
"github.com/cosi-project/runtime/pkg/state" | ||
"github.com/siderolabs/gen/optional" | ||
"go.uber.org/zap" | ||
"golang.org/x/sys/unix" | ||
"sigs.k8s.io/kustomize/kyaml/errors" | ||
|
||
"github.com/siderolabs/talos/pkg/machinery/resources/config" | ||
) | ||
|
||
// WatchdogTimerController watches v1alpha1.Config, creates/updates/deletes kernel module specs. | ||
type WatchdogTimerController struct{} | ||
|
||
// Name implements controller.Controller interface. | ||
func (ctrl *WatchdogTimerController) Name() string { | ||
return "runtime.WatchdogTimerController" | ||
} | ||
|
||
// Inputs implements controller.Controller interface. | ||
func (ctrl *WatchdogTimerController) Inputs() []controller.Input { | ||
return []controller.Input{ | ||
{ | ||
Namespace: config.NamespaceName, | ||
Type: config.MachineConfigType, | ||
ID: optional.Some(config.V1Alpha1ID), | ||
}, | ||
} | ||
} | ||
|
||
// Outputs implements controller.Controller interface. | ||
func (ctrl *WatchdogTimerController) Outputs() []controller.Output { | ||
return nil | ||
} | ||
|
||
// Run implements controller.Controller interface. | ||
// | ||
//nolint:gocyclo | ||
func (ctrl *WatchdogTimerController) Run(ctx context.Context, r controller.Runtime, logger *zap.Logger) error { | ||
ticker := time.NewTicker(3 * time.Second) | ||
defer ticker.Stop() | ||
var wd *os.File | ||
|
||
wdClose := func() { | ||
logger.Info("Closing hardware watchdog", zap.String("filename", wd.Name())) | ||
// Magic close: make sure old watchdog won't trip after we switch | ||
wd.Write([]byte("V")) | ||
wd.Close() | ||
wd = nil | ||
} | ||
|
||
for { | ||
select { | ||
case <-ctx.Done(): | ||
return nil | ||
case <-ticker.C: | ||
if wd != nil { | ||
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, wd.Fd(), unix.WDIOC_KEEPALIVE, 0); err != 0 { | ||
return fmt.Errorf("Failed to feed watchdog: %w", err) | ||
} | ||
} | ||
continue | ||
case <-r.EventCh(): | ||
} | ||
|
||
r.ResetRestartBackoff() | ||
|
||
cfg, err := safe.ReaderGetByID[*config.MachineConfig](ctx, r, config.V1Alpha1ID) | ||
if err != nil { | ||
if !state.IsNotFoundError(err) { | ||
return fmt.Errorf("Error getting watchdog config: %w", err) | ||
} | ||
} | ||
|
||
if cfg != nil && cfg.Config().Machine() != nil { | ||
dev := cfg.Config().Machine().Kernel().WatchdogDevice() | ||
if dev == "" { | ||
continue | ||
} | ||
|
||
timeout := cfg.Config().Machine().Kernel().WatchdogTimeout() | ||
if timeout == 0 { | ||
continue | ||
} | ||
|
||
// Close the watchdog if requested to use new one | ||
if wd != nil && wd.Name() != dev { | ||
wdClose() | ||
} | ||
|
||
if wd == nil { | ||
wd, err = os.OpenFile(dev, syscall.O_RDWR, 0600) | ||
if err != nil { | ||
return errors.Errorf("Failed to open watchdog device: %s", err) | ||
} | ||
logger.Info("Opened hardware watchdog", zap.String("filename", dev)) | ||
// TODO: support reboot/shutdown watchdogs | ||
defer wdClose() | ||
} | ||
|
||
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, wd.Fd(), uintptr(unix.WDIOC_SETTIMEOUT), uintptr(unsafe.Pointer(&timeout))); err != 0 { | ||
return fmt.Errorf("Failed to set watchdog timeout: %w", err) | ||
} | ||
|
||
// 3 pings per timeout should suffice in any case | ||
ticker = time.NewTicker(time.Duration(timeout/3) * time.Second) | ||
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, wd.Fd(), uintptr(unix.WDIOC_KEEPALIVE), uintptr(0)); err != 0 { | ||
return fmt.Errorf("Failed to feed watchdog: %w", err) | ||
} | ||
logger.Info("Set hardware watchdog timeout", zap.Int("timeout", timeout), zap.Int("feed_interval", timeout/3)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters