Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OCI DHCP renewal #1334

Merged
merged 5 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 95 additions & 30 deletions cmd/incusd/main_forknet.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package main
#include "incus.h"
#include "macro.h"
#include "memory_utils.h"
#include "process_utils.h"

static void forkdonetinfo(int pidfd, int ns_fd)
{
Expand Down Expand Up @@ -74,21 +75,75 @@ static void forkdonetdetach(char *file) {
static void forkdonetdhcp() {
char *pidstr;
char path[PATH_MAX];
pid_t pid;

pidstr = getenv("LXC_PID");
if (!pidstr) {
fprintf(stderr, "No LXC_PID in environment\n");
_exit(1);
}

snprintf(path, sizeof(path), "/proc/%s/ns/net", pidstr);

// Attach to the network namespace.
snprintf(path, sizeof(path), "/proc/%s/ns/net", pidstr);
if (dosetns_file(path, "net") < 0) {
fprintf(stderr, "Failed setns to container network namespace: %s\n", strerror(errno));
_exit(1);
}

// Attach to the PID namespace.
snprintf(path, sizeof(path), "/proc/%s/ns/pid", pidstr);
if (dosetns_file(path, "pid") < 0) {
fprintf(stderr, "Failed setns to container PID namespace: %s\n", strerror(errno));
_exit(1);
}

// Run in the background.
pid = fork();
if (pid < 0) {
fprintf(stderr, "%s - Failed to create new process\n",
strerror(errno));
_exit(EXIT_FAILURE);
}

if (pid > 0) {
_exit(EXIT_SUCCESS);
}

if (!freopen("/dev/null", "r", stdin)) {
fprintf(stderr, "Failed to reconfigure stdin: %s\n", strerror(errno));
_exit(1);
}

if (!freopen("/dev/null", "w", stdout)) {
fprintf(stderr, "Failed to reconfigure stdout: %s\n", strerror(errno));
_exit(1);
}

if (!freopen("/dev/null", "w", stderr)) {
fprintf(stderr, "Failed to reconfigure stderr: %s\n", strerror(errno));
_exit(1);
}

if (setsid() < 0) {
fprintf(stderr, "%s - Failed to setup new session\n",
strerror(errno));
_exit(EXIT_FAILURE);
}

pid = fork();
if (pid < 0) {
fprintf(stderr, "%s - Failed to create new process\n",
strerror(errno));
_exit(EXIT_FAILURE);
}

if (pid > 0) {
_exit(EXIT_SUCCESS);
}

// Set the process title.
(void)setproctitle("[incus DHCP] eth0");

// Jump back to Go for the rest
}

Expand Down Expand Up @@ -145,6 +200,7 @@ void forknet(void)
import "C"

import (
"context"
"encoding/json"
"fmt"
"net"
Expand All @@ -154,7 +210,7 @@ import (
"time"

"github.com/insomniacslk/dhcp/dhcpv4"
"github.com/insomniacslk/dhcp/dhcpv4/client4"
"github.com/insomniacslk/dhcp/dhcpv4/nclient4"
"github.com/spf13/cobra"

"github.com/lxc/incus/v6/internal/netutils"
Expand Down Expand Up @@ -227,9 +283,9 @@ func (c *cmdForknet) RunInfo(cmd *cobra.Command, args []string) error {

// RunDHCP runs a one time DHCPv4 client and applies address, route and DNS configuration.
func (c *cmdForknet) RunDHCP(cmd *cobra.Command, args []string) error {
var messages []*dhcpv4.DHCPv4
iface := "eth0"

// Bring the interface up.
link := &ip.Link{
Name: iface,
}
Expand All @@ -249,33 +305,27 @@ func (c *cmdForknet) RunDHCP(cmd *cobra.Command, args []string) error {
hostname := strings.TrimSpace(string(bb))

// Try to get a lease.
client := client4.NewClient()
for i := 0; i < 10; i++ {
var err error
client, err := nclient4.New(iface)
if err != nil {
fmt.Fprintf(os.Stderr, "Giving up on DHCP, couldn't set up client for %q: %v\n", iface, err)
return nil
}

messages, err = client.Exchange(iface, dhcpv4.WithOption(dhcpv4.OptHostName(hostname)))
if err == nil {
break
}
defer func() { _ = client.Close() }()

time.Sleep(500 * time.Millisecond)
lease, err := client.Request(context.Background(), dhcpv4.WithOption(dhcpv4.OptHostName(hostname)))
if err != nil {
fmt.Fprintf(os.Stderr, "Giving up on DHCP, couldn't get a lease on %q (%q): %v\n", iface, hostname, err)
return nil
}

// Parse the response.
var reply *dhcpv4.DHCPv4
for _, m := range messages {
if m.OpCode == dhcpv4.OpcodeBootReply && m.MessageType() == dhcpv4.MessageTypeOffer {
reply = m
break
}
}

if reply == nil {
if lease.Offer == nil {
fmt.Fprintf(os.Stderr, "Giving up on DHCP, couldn't get a lease on %q after 5s\n", iface)
return nil
}

if reply.YourIPAddr == nil || reply.YourIPAddr.Equal(net.IPv4zero) || reply.SubnetMask() == nil || len(reply.Router()) != 1 || len(reply.DNS()) < 1 {
if lease.Offer.YourIPAddr == nil || lease.Offer.YourIPAddr.Equal(net.IPv4zero) || lease.Offer.SubnetMask() == nil || len(lease.Offer.Router()) != 1 || len(lease.Offer.DNS()) < 1 {
fmt.Fprintf(os.Stderr, "Giving up on DHCP, lease for %q didn't contain required fields\n", iface)
return nil
}
Expand All @@ -289,36 +339,36 @@ func (c *cmdForknet) RunDHCP(cmd *cobra.Command, args []string) error {

defer f.Close()

for _, nameserver := range reply.DNS() {
for _, nameserver := range lease.Offer.DNS() {
_, err = f.Write([]byte(fmt.Sprintf("nameserver %s\n", nameserver)))
if err != nil {
fmt.Fprintf(os.Stderr, "Giving up on DHCP, couldn't prepare resolv.conf: %v\n", err)
return nil
}
}

if reply.DomainName() != "" {
_, err = f.Write([]byte(fmt.Sprintf("domain %s\n", reply.DomainName())))
if lease.Offer.DomainName() != "" {
_, err = f.Write([]byte(fmt.Sprintf("domain %s\n", lease.Offer.DomainName())))
if err != nil {
fmt.Fprintf(os.Stderr, "Giving up on DHCP, couldn't prepare resolv.conf: %v\n", err)
return nil
}
}

if reply.DomainSearch() != nil && len(reply.DomainSearch().Labels) > 0 {
_, err = f.Write([]byte(fmt.Sprintf("search %s\n", strings.Join(reply.DomainSearch().Labels, ", "))))
if lease.Offer.DomainSearch() != nil && len(lease.Offer.DomainSearch().Labels) > 0 {
_, err = f.Write([]byte(fmt.Sprintf("search %s\n", strings.Join(lease.Offer.DomainSearch().Labels, ", "))))
if err != nil {
fmt.Fprintf(os.Stderr, "Giving up on DHCP, couldn't prepare resolv.conf: %v\n", err)
return nil
}
}

// Network configuration.
netMask, _ := reply.SubnetMask().Size()
netMask, _ := lease.Offer.SubnetMask().Size()

addr := &ip.Addr{
DevName: iface,
Address: fmt.Sprintf("%s/%d", reply.YourIPAddr, netMask),
Address: fmt.Sprintf("%s/%d", lease.Offer.YourIPAddr, netMask),
Family: ip.FamilyV4,
}

Expand All @@ -331,7 +381,7 @@ func (c *cmdForknet) RunDHCP(cmd *cobra.Command, args []string) error {
route := &ip.Route{
DevName: iface,
Route: "default",
Via: reply.Router()[0].String(),
Via: lease.Offer.Router()[0].String(),
Family: ip.FamilyV4,
}

Expand All @@ -341,6 +391,21 @@ func (c *cmdForknet) RunDHCP(cmd *cobra.Command, args []string) error {
return nil
}

// Handle DHCP renewal.
for {
// Wait until it's renewal time.
time.Sleep(lease.Offer.IPAddressRenewalTime(time.Minute))

// Renew the lease.
newLease, err := client.Renew(context.Background(), lease, dhcpv4.WithOption(dhcpv4.OptHostName(hostname)))
if err != nil {
fmt.Fprintf(os.Stderr, "Giving up on DHCP, couldn't renew the lease for %q\n", iface)
return nil
}

lease = newLease
}

return nil
}

Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,15 @@ require (
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jkeiser/iter v0.0.0-20200628201005-c8aa0ae784d1 // indirect
github.com/josharian/native v1.1.0 // indirect
github.com/k-sone/critbitgo v1.4.0 // indirect
github.com/klauspost/compress v1.17.10 // indirect
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/mdlayher/packet v1.1.2 // indirect
github.com/mdlayher/socket v0.5.1 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO
github.com/hashicorp/mdns v1.0.0/go.mod h1:tL+uN++7HEJ6SQLQ2/p+z2pH24WQKWjBPkE0mNTz8vQ=
github.com/hashicorp/memberlist v0.1.3/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
github.com/hashicorp/serf v0.8.2/go.mod h1:6hOLApaqBFA1NXqRQAsxw9QxuDEvNxSQRwA/JwenrHc=
github.com/hugelgupf/socketpair v0.0.0-20190730060125-05d35a94e714 h1:/jC7qQFrv8CrSJVmaolDVOxTfS9kc36uB6H40kdbQq8=
github.com/hugelgupf/socketpair v0.0.0-20190730060125-05d35a94e714/go.mod h1:2Goc3h8EklBH5mspfHFxBnEoURQCGzQQH1ga9Myjvis=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
Expand All @@ -266,6 +268,8 @@ github.com/jkeiser/iter v0.0.0-20200628201005-c8aa0ae784d1 h1:smvLGU3obGU5kny71B
github.com/jkeiser/iter v0.0.0-20200628201005-c8aa0ae784d1/go.mod h1:fP/NdyhRVOv09PLRbVXrSqHhrfQypdZwgE2L4h2U5C8=
github.com/jochenvg/go-udev v0.0.0-20240801134859-b65ed646224b h1:Pzf7tldbCVqwl3NnOnTamEWdh/rL41fsoYCn2HdHgRA=
github.com/jochenvg/go-udev v0.0.0-20240801134859-b65ed646224b/go.mod h1:IBDUGq30U56w969YNPomhMbRje1GrhUsCh7tHdwgLXA=
github.com/josharian/native v1.1.0 h1:uuaP0hAbW7Y4l0ZRQ6C9zfb7Mg1mbFKry/xzDAfmtLA=
github.com/josharian/native v1.1.0/go.mod h1:7X/raswPFr05uY3HiLlYeyQntB6OO7E/d2Cu7qoaN2w=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
Expand Down Expand Up @@ -314,6 +318,8 @@ github.com/mdlayher/ndp v1.1.0 h1:QylGKGVtH60sKZUE88+IW5ila1Z/M9/OXhWdsVKuscs=
github.com/mdlayher/ndp v1.1.0/go.mod h1:FmgESgemgjl38vuOIyAHWUUL6vQKA/pQNkvXdWsdQFM=
github.com/mdlayher/netx v0.0.0-20230430222610-7e21880baee8 h1:HMgSn3c16SXca3M+n6fLK2hXJLd4mhKAsZZh7lQfYmQ=
github.com/mdlayher/netx v0.0.0-20230430222610-7e21880baee8/go.mod h1:qhZhwMDNWwZglKfwuWm0U9pCr/YKX1QAEwwJk9qfiTQ=
github.com/mdlayher/packet v1.1.2 h1:3Up1NG6LZrsgDVn6X4L9Ge/iyRyxFEFD9o6Pr3Q1nQY=
github.com/mdlayher/packet v1.1.2/go.mod h1:GEu1+n9sG5VtiRE4SydOmX5GTwyyYlteZiFU+x0kew4=
github.com/mdlayher/socket v0.5.1 h1:VZaqt6RkGkt2OE9l3GcC6nZkqD3xKeQLyfleW/uBcos=
github.com/mdlayher/socket v0.5.1/go.mod h1:TjPLHI1UgwEv5J1B5q0zTZq12A/6H7nKmtTanQE37IQ=
github.com/mdlayher/vsock v1.2.1 h1:pC1mTJTvjo1r9n9fbm7S1j04rCgCzhCOS5DY0zqHlnQ=
Expand Down
Loading
Loading