From patchwork Sat Oct 1 13:12:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexey Abramov X-Patchwork-Id: 43093 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id 39D7327BBEA; Sat, 1 Oct 2022 14:14:10 +0100 (BST) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,MAILING_LIST_MULTI,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 8205827BBE9 for ; Sat, 1 Oct 2022 14:14:09 +0100 (BST) Received: from localhost ([::1]:34452 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oecJk-0008IX-FX for patchwork@mira.cbaines.net; Sat, 01 Oct 2022 09:14:08 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:36940) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oecJe-0008IL-N6 for guix-patches@gnu.org; Sat, 01 Oct 2022 09:14:02 -0400 Received: from debbugs.gnu.org ([209.51.188.43]:44880) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oecJe-00041Y-FG for guix-patches@gnu.org; Sat, 01 Oct 2022 09:14:02 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oecJe-0000SZ-AK for guix-patches@gnu.org; Sat, 01 Oct 2022 09:14:02 -0400 X-Loop: help-debbugs@gnu.org Subject: [bug#58223] [PATCH 1/1] services: dhcp-client: Implement and use a configuration record References: <20221001131112.2649-1-levenson@mmer.org> In-Reply-To: <20221001131112.2649-1-levenson@mmer.org> Resent-From: Alexey Abramov Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sat, 01 Oct 2022 13:14:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 58223 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 58223@debbugs.gnu.org Received: via spool by 58223-submit@debbugs.gnu.org id=B58223.16646299971705 (code B ref 58223); Sat, 01 Oct 2022 13:14:02 +0000 Received: (at 58223) by debbugs.gnu.org; 1 Oct 2022 13:13:17 +0000 Received: from localhost ([127.0.0.1]:43958 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oecIu-0000RQ-QG for submit@debbugs.gnu.org; Sat, 01 Oct 2022 09:13:17 -0400 Received: from mail.mmer.org ([178.22.65.174]:46322) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oecIs-0000RC-NG for 58223@debbugs.gnu.org; Sat, 01 Oct 2022 09:13:15 -0400 Received: from mail.mmer.org (localhost [127.0.0.1]) by mail.mmer.org (OpenSMTPD) with ESMTP id 3310f5a9 for <58223@debbugs.gnu.org>; Sat, 1 Oct 2022 13:13:08 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed; d=mmer.org; h=from:to :subject:date:message-id:mime-version:content-transfer-encoding; s=dkim; bh=GH9nkz9Ya1sD0QqXy1KfAVmXSU9IDN5F/hY5FYxiZUg=; b=QNr9 MzHaMsUu1g+tMqjJqng2huw2E878CupS93kfPYPMCxTIwjwv4cJoPIUVPqEo36ry gyoj9KcT4vb789MYI78tRqjnRDSZQaQadT60tZ2UunIgFHtA/krCQDmXF6e7A1wx hLcC8Bozt9Yb+KZEDRFKXrSDogv+JEEywjTryj0= Received: from delta (j74210.upc-j.chello.nl [24.132.74.210]) by mail.mmer.org (OpenSMTPD) with ESMTPSA id 8bad73e5 (TLSv1.3:AEAD-AES256-GCM-SHA384:256:NO) for <58223@debbugs.gnu.org>; Sat, 1 Oct 2022 13:13:08 +0000 (UTC) Date: Sat, 1 Oct 2022 15:12:57 +0200 Message-Id: <20221001131257.3407-1-levenson@mmer.org> X-Mailer: git-send-email 2.36.1 MIME-Version: 1.0 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" Reply-to: Alexey Abramov X-ACL-Warn: , Alexey Abramov via Guix-patches X-Patchwork-Original-From: Alexey Abramov via Guix-patches via From: Alexey Abramov X-getmail-retrieved-from-mailbox: Patches * gnu/services/networking.scm (dhcp-client-configuration): New record configuration. (dhcp-client-shepherd-service): Implement a shepher service. Provide a deprication message for legacy configurations. (dhcp-client-service-type): Use dhcp-client-shepherd-service. * doc/guix.texi: Update documentation --- doc/guix.texi | 18 +++++- gnu/services/networking.scm | 114 ++++++++++++++++++++++-------------- 2 files changed, 85 insertions(+), 47 deletions(-) diff --git a/doc/guix.texi b/doc/guix.texi index 30eb7f4cbf..e425d98d26 100644 --- a/doc/guix.texi +++ b/doc/guix.texi @@ -19227,10 +19227,24 @@ the user mode network stack,,, QEMU, QEMU Documentation}). @cindex DHCP, networking service @defvr {Scheme Variable} dhcp-client-service-type This is the type of services that run @var{dhcp}, a Dynamic Host Configuration -Protocol (DHCP) client, on all the non-loopback network interfaces. Its value -is the DHCP client package to use, @code{isc-dhcp} by default. +Protocol (DHCP) client. @end defvr +@deftp {Data Type} dhcp-client-configuration +Data type representing the configuration of dhcp client network service. + +@table @asis +@item @code{package} (default: @code{isc-dhcp}) +DHCP client package to use. + +@item @code{interfaces} (default: @code{'()}) +List of strings of interface names that dhcp client should listen on. By +default dhcp client will listen on all available non-loopback interfaces +that can be activated (meaning, to set them up). (default: @code{'()}) + +@end table +@end deftp + @cindex NetworkManager @defvr {Scheme Variable} network-manager-service-type diff --git a/gnu/services/networking.scm b/gnu/services/networking.scm index 9d85728371..1185f7e57d 100644 --- a/gnu/services/networking.scm +++ b/gnu/services/networking.scm @@ -77,6 +77,10 @@ (define-module (gnu services networking) static-networking-service-type) #:export (%facebook-host-aliases dhcp-client-service-type + dhcp-client-configuration + dhcp-client-configuration? + dhcp-client-configuration-package + dhcp-client-configuration-interfaces dhcpd-service-type dhcpd-configuration @@ -259,52 +263,72 @@ (define %facebook-host-aliases fe80::1%lo0 www.connect.facebook.net fe80::1%lo0 apps.facebook.com\n") + +(define-record-type* + dhcp-client-configuration make-dhcp-client-configuration + dhcp-client-configuration? + (package dhcp-client-configuration-package ;file-like + (default isc-dhcp)) + ;; Empty list (means any) or a list of valid interfaces + (interfaces dhcp-client-configuration-interfaces + (default '()))) + +(define dhcp-client-shepherd-service + (match-lambda + (($ package interfaces) + (let ((pid-file "/var/run/dhclient.pid")) + (list (shepherd-service + (documentation "Set up networking via DHCP.") + (requirement '(user-processes udev)) + + ;; XXX: Running with '-nw' ("no wait") avoids blocking for a minute when + ;; networking is unavailable, but also means that the interface is not up + ;; yet when 'start' completes. To wait for the interface to be ready, one + ;; should instead monitor udev events. + (provision '(networking)) + + (start #~(lambda _ + (define dhclient + (string-append #$package "/sbin/dhclient")) + + ;; When invoked without any arguments, 'dhclient' discovers all + ;; non-loopback interfaces *that are up*. However, the relevant + ;; interfaces are typically down at this point. Thus we perform + ;; our own interface discovery here. + (define valid? + (lambda (interface) + (and (arp-network-interface? interface) + (not (loopback-network-interface? interface)) + ;; XXX: Make sure the interfaces are up so that + ;; 'dhclient' can actually send/receive over them. + ;; Ignore those that cannot be activated. + (false-if-exception + (set-network-interface-up interface))))) + (define ifaces + (filter valid? (or '#$interfaces + (all-network-interface-names)))) + + (false-if-exception (delete-file #$pid-file)) + (let ((pid (fork+exec-command + (cons* dhclient "-nw" + "-pf" #$pid-file ifaces)))) + (and (zero? (cdr (waitpid pid))) + (read-pid-file #$pid-file))))) + (stop #~(make-kill-destructor)))))) + (anything + (format (current-error-port) "warning: Defining dhcp-client service with +a single argument value being a client package to use, is deprecated. Please +use record instead.\n") + (dhcp-client-shepherd-service + (dhcp-client-configuration + (package anything)))))) + (define dhcp-client-service-type - (shepherd-service-type - 'dhcp-client - (lambda (dhcp) - (define dhclient - (file-append dhcp "/sbin/dhclient")) - - (define pid-file - "/var/run/dhclient.pid") - - (shepherd-service - (documentation "Set up networking via DHCP.") - (requirement '(user-processes udev)) - - ;; XXX: Running with '-nw' ("no wait") avoids blocking for a minute when - ;; networking is unavailable, but also means that the interface is not up - ;; yet when 'start' completes. To wait for the interface to be ready, one - ;; should instead monitor udev events. - (provision '(networking)) - - (start #~(lambda _ - ;; When invoked without any arguments, 'dhclient' discovers all - ;; non-loopback interfaces *that are up*. However, the relevant - ;; interfaces are typically down at this point. Thus we perform - ;; our own interface discovery here. - (define valid? - (lambda (interface) - (and (arp-network-interface? interface) - (not (loopback-network-interface? interface)) - ;; XXX: Make sure the interfaces are up so that - ;; 'dhclient' can actually send/receive over them. - ;; Ignore those that cannot be activated. - (false-if-exception - (set-network-interface-up interface))))) - (define ifaces - (filter valid? (all-network-interface-names))) - - (false-if-exception (delete-file #$pid-file)) - (let ((pid (fork+exec-command - (cons* #$dhclient "-nw" - "-pf" #$pid-file ifaces)))) - (and (zero? (cdr (waitpid pid))) - (read-pid-file #$pid-file))))) - (stop #~(make-kill-destructor)))) - isc-dhcp - (description "Run @command{dhcp}, a Dynamic Host Configuration + (service-type (name 'dhcp-client) + (extensions + (list (service-extension shepherd-root-service-type dhcp-client-shepherd-service))) + (default-value (dhcp-client-configuration)) + (description "Run @command{dhcp}, a Dynamic Host Configuration Protocol (DHCP) client, on all the non-loopback network interfaces."))) (define-record-type*