From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms9.migadu.com with LMTPS id CL6YN5q0bGT/UwEASxT56A (envelope-from ) for ; Tue, 23 May 2023 14:42:02 +0200 Received: from aspmx1.migadu.com ([2001:41d0:8:6d80::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id iFH5Npq0bGSifAEAauVa8A (envelope-from ) for ; Tue, 23 May 2023 14:42:02 +0200 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id A678919F1B for ; Tue, 23 May 2023 14:42:02 +0200 (CEST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1q1RKM-0001na-BY; Tue, 23 May 2023 08:41:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q1RK8-0001i7-NK for bug-guix@gnu.org; Tue, 23 May 2023 08:41:08 -0400 Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1q1RK7-0003X9-1w for bug-guix@gnu.org; Tue, 23 May 2023 08:41:08 -0400 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1q1RK6-00036c-UH for bug-guix@gnu.org; Tue, 23 May 2023 08:41:06 -0400 X-Loop: help-debbugs@gnu.org Subject: bug#63516: [PATCH Guile-Netlink 11/11] link: Add 'wait-for-link'. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: bug-guix@gnu.org Resent-Date: Tue, 23 May 2023 12:41:06 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 63516 X-GNU-PR-Package: guix X-GNU-PR-Keywords: To: 63516@debbugs.gnu.org Cc: Ludovic =?UTF-8?Q?Court=C3=A8s?= Received: via spool by 63516-submit@debbugs.gnu.org id=B63516.168484565711837 (code B ref 63516); Tue, 23 May 2023 12:41:06 +0000 Received: (at 63516) by debbugs.gnu.org; 23 May 2023 12:40:57 +0000 Received: from localhost ([127.0.0.1]:38551 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1q1RJw-00034r-6N for submit@debbugs.gnu.org; Tue, 23 May 2023 08:40:56 -0400 Received: from eggs.gnu.org ([209.51.188.92]:60086) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1q1RJd-00033c-DJ for 63516@debbugs.gnu.org; Tue, 23 May 2023 08:40:38 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q1RJY-0003Ok-4w; Tue, 23 May 2023 08:40:32 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=96dPjYGBHJpAqssVQymcJ5iGYyIfM9S6xobENvE0cQ0=; b=InXohDxO6rIckaMVPzHw epZVuKRzYZU83P/LCrswgX6buw19Q0G8cuDVKoJOdyAXR95y4UAQvnK+fKu7xRHVpWxVuZvPQpF7e AAb7qW6EUngE0vXK/SCejWJlv74osrN9acxFCROR4ijlsZFr6bi0aarCRA7Rze2og/kaAf1InMhXM LVhnz8R91M3yHcMFXh/ZyzZxDgCacXTAvVscc9FiBEttIKYhc172gbrNAd4lWi2mr3qI+OlWBnBRK /WH33AgqLLrTZzpg8M2aSQAthS+EY1jTfOH1dxdXKkkWGIQb4aEi5XgQrPtYUG8BiSqAWc7Mhp745 Ga9N4dsHyHX/kw==; Received: from [193.50.110.247] (helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1q1RJK-00081G-5x; Tue, 23 May 2023 08:40:31 -0400 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Date: Tue, 23 May 2023 14:39:51 +0200 Message-Id: <20230523123951.6225-12-ludo@gnu.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230523123951.6225-1-ludo@gnu.org> References: <871qjawqpt.fsf@gnu.org> <20230523123951.6225-1-ludo@gnu.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-guix@gnu.org List-Id: Bug reports for GNU Guix List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-guix-bounces+larch=yhetil.org@gnu.org Sender: bug-guix-bounces+larch=yhetil.org@gnu.org X-Migadu-Country: US X-Migadu-Flow: FLOW_IN ARC-Seal: i=1; s=key1; d=yhetil.org; t=1684845722; a=rsa-sha256; cv=none; b=nxkLyJTG+5warBZyi88vmprKZ8K1oEK84Zou+TNYVcOK/tliZaTxWH9tF5Smr7uWjzzKjs XIscfuY4fAQqvXQhNr0MzLqmHdFNRvRKT0vvSIpo1Dn6T6hLWhCqfWPM/nisQ+j1noAWtC dnIM6N1cVNCZPT/MaKwRShr3kYR+CAz0kuarzmOVsL6UTuYW4AOvgLdwmAbE7hhKxkhKL+ b5GpCb7Yxq2uBEDwL+TKGFM6ZfqZH9710wE6zaPbuyBHjzfLOX3b9C0OYm1zZ0AlR5RZ6P FQHww696IjlZcsYSytdSmPsiyxiJdAQLboR7uj0u8qd+l2g2IAWJMh5ExhnvmA== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b=InXohDxO; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "bug-guix-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="bug-guix-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1684845722; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=96dPjYGBHJpAqssVQymcJ5iGYyIfM9S6xobENvE0cQ0=; b=sGq1SOUbM8nlbouNslo2sIG6PomTKxDBzsaRIB4U6uXju8NGWlqMf9ncNK3ma4RTN9idyP kQVvX1R6G9FlJla+p5sIIZA4nDyIj48E8+ClNvB0BBwmzlMZ7AFTIFt623tIgbEwpzXmbd He2/n+/iZbO84DmAZiEcI08facg5UGm5fpKzrLqMXDukJkaBnLRFazmyKyzmLG2WniGUwk ZT9OYTwjoOsPZYfUhm7Us3+Y9awBwtyplW2le3TsrVPy1/pm8zJ1AHK/+EOXbjUYlHyzjF UrlwVdFrcHzPHNnmdE/IXf9eGCs0iwE+Y++vOA3IIpOg2hbxP1DXrLiDRusoRA== X-Migadu-Spam-Score: -0.81 X-Spam-Score: -0.81 X-Migadu-Queue-Id: A678919F1B X-Migadu-Scanner: scn0.migadu.com Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b=InXohDxO; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "bug-guix-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="bug-guix-bounces+larch=yhetil.org@gnu.org" X-TUID: WRmOfZ8/FzZN * ip/link.scm (message->event+link): New procedure. (new-link-message->link): Use it. (monitor-links, wait-for-link): New procedures. * doc/guile-netlink.texi (Link): Document 'wait-for-link'. --- doc/guile-netlink.texi | 8 ++++ ip/link.scm | 102 ++++++++++++++++++++++++++++++++++------- 2 files changed, 94 insertions(+), 16 deletions(-) diff --git a/doc/guile-netlink.texi b/doc/guile-netlink.texi index 4dbeafe..3355c27 100644 --- a/doc/guile-netlink.texi +++ b/doc/guile-netlink.texi @@ -567,6 +567,14 @@ Returns the list of existing links in the system, as a list of @code{} objects. @end deffn +@deffn {Scheme Procedure} wait-for-link @var{name} [#:blocking? #t] +Wait until a link called @var{name} (a string such as @code{"ens3"}) shows +up. + +When @var{blocking?} is false, use a non-blocking socket and cooperate via +@code{current-read-waiter}---useful when using Fibers. +@end deffn + @deffn {Sceme Procedure} print-link @var{link} Display @var{link} on the standard output, using a format similar to @command{ip link} from @code{iproute2}. diff --git a/ip/link.scm b/ip/link.scm index 7e0ae6b..1323444 100644 --- a/ip/link.scm +++ b/ip/link.scm @@ -1,7 +1,8 @@ ;;;; This file is part of Guile Netlink ;;;; ;;;; Copyright (C) 2021 Julien Lepiller -;;;; +;;;; Copyright (C) 2023 Ludovic Courtès +;;;; ;;;; This library is free software: you can redistribute it and/or modify ;;;; it under the terms of the GNU General Public License as published by ;;;; the Free Software Foundation, either version 3 of the License, or @@ -31,12 +32,14 @@ #:use-module (srfi srfi-9) #:use-module (srfi srfi-34) #:use-module (srfi srfi-35) + #:use-module (srfi srfi-71) #:export (link-add link-del link-set link-show link-name->index get-links + wait-for-link print-link make-link link? @@ -59,24 +62,35 @@ (addr link-addr) (brd link-brd)) +(define (message->event+link msg) + "If MSG relates to a link event, return two values: its kind (e.g., +RTM_NEWLINK) and its associated value. Otherwise return #f and #f." + (if (memv (message-kind msg) + (list RTM_NEWLINK + RTM_DELLINK + RTM_SETLINK)) + (values (message-kind msg) + (let* ((data (message-data msg)) + (attrs (link-message-attrs data))) + (make-link (get-attr attrs IFLA_IFNAME) + (link-message-index data) + (link-message-kind data) + (map int->device-flags (split-flags (link-message-flags data))) + (get-attr attrs IFLA_MTU) + (get-attr attrs IFLA_QDISC) + (get-attr attrs IFLA_OPERSTATE) + (get-attr attrs IFLA_LINKMODE) + (get-attr attrs IFLA_GROUP) + (get-attr attrs IFLA_TXQLEN) + (get-attr attrs IFLA_ADDRESS) + (get-attr attrs IFLA_BROADCAST)))) + (values #f #f))) + (define (new-link-message->link msg) "If MSG has type 'RTM_NEWLINK', return the corresponding object. Otherwise return #f." - (and (eqv? (message-kind msg) RTM_NEWLINK) - (let* ((data (message-data msg)) - (attrs (link-message-attrs data))) - (make-link (get-attr attrs IFLA_IFNAME) - (link-message-index data) - (link-message-kind data) - (map int->device-flags (split-flags (link-message-flags data))) - (get-attr attrs IFLA_MTU) - (get-attr attrs IFLA_QDISC) - (get-attr attrs IFLA_OPERSTATE) - (get-attr attrs IFLA_LINKMODE) - (get-attr attrs IFLA_GROUP) - (get-attr attrs IFLA_TXQLEN) - (get-attr attrs IFLA_ADDRESS) - (get-attr attrs IFLA_BROADCAST))))) + (let ((kind link (message->event+link msg))) + (and (eqv? kind RTM_NEWLINK) link))) (define (get-links) (define request-num (random 65535)) @@ -390,3 +404,59 @@ balance-rr|active-backup|balance-xor|broadcast|802.3ad|balance-tlb|balance-alb" (let ((answer (receive-and-decode-msg sock %default-route-decoder))) (close-port sock) (answer-ok? (last answer))))) + +(define* (monitor-links proc init terminate? ;TODO: Make public? + #:key (blocking? #t)) + "Wait for link events until @var{terminate?} returns true. Call @var{init} +with the initial list of links; use its result as the initial state. From +then on, call @code{(@var{proc} @var{event} @var{link} @var{state})} where +@var{event} is a constant such as @code{RTM_NEWLINK} and @var{link} is the +corresponding link. Return the final state. + +When @code{blocking?} is false, use a non-blocking socket and cooperate via +@code{current-read-waiter}---useful when using Fibers." + (define request-num (random 65536)) + (define message + (make-message + RTM_GETLINK + (logior NLM_F_REQUEST NLM_F_DUMP) + request-num + 0 + (make-link-message AF_UNSPEC 0 0 0 0 '()))) + + (let ((sock (connect-route #:flags (if blocking? 0 SOCK_NONBLOCK)))) + ;; Subscribe to the "link" group. + (add-socket-membership sock RTNLGRP_LINK) + + (send-msg message sock) + (let* ((answer (receive-and-decode-msg sock %default-route-decoder)) + (links (filter-map new-link-message->link answer))) + (let loop ((state (init links))) + (if (terminate? state) + (begin + (close-port sock) + state) + (loop (fold (lambda (msg state) + (let ((event link (message->event+link msg))) + (proc event link state))) + state + (receive-and-decode-msg sock %default-route-decoder)))))))) + + +(define* (wait-for-link name #:key (blocking? #t)) + "Wait until a link called @var{name} (a string such as @code{\"ens3\"}) shows +up. + +When @var{blocking?} is false, use a non-blocking socket and cooperate via +@code{current-read-waiter}---useful when using Fibers." + (monitor-links (lambda (event link result) + (and (= RTM_NEWLINK) + (string=? (link-name link) name) + link)) + (lambda (links) + (find (lambda (link) + (string=? (link-name link) name)) + links)) + (lambda (link) ;if LINK is true, terminate + link) + #:blocking? blocking?)) -- 2.40.1