Make <link>, print-link and get-links public and document them.

Julien LepillerSat Mar 13 23:14:01+0100 2021

54b25a6

Make <link>, print-link and get-links public and document them.

doc/guile-netlink.texi

483483
network links on your machine.  They are equivalent to the @command{ip link}
484484
family of commands, from @code{iproute2}.
485485
486+
@deffn {Datatype} <link>
487+
488+
Datatype representing the status of a network link.
489+
490+
get-links                                                           
491+
            print-link                                                          
492+
                                                                                
493+
            <link> make-link link?                                              
494+
            link-name link-id link-type link-flags link-mtu link-qdisc          
495+
            link-state link-mode link-group link-qlen link-addr link-brd
496+
497+
@table @asis
498+
@item @code{name}
499+
Name of the link, such as @code{"enp1s0"}.
500+
501+
@item @code{id}
502+
Index of the link, a unique number used to identify the link.
503+
504+
@item @code{type}
505+
Type of the link, as an integer.
506+
507+
@item @code{flags}
508+
Flags associated with the device, as a list of symbols, such as
509+
@code{'(UP LOOPBACK)}.
510+
511+
@item @code{mtu}
512+
MTU of the link, as an integer.
513+
514+
@item @code{qdisc}
515+
Queuing discipline of the link, as a string, such as @code{"noqueue"}.
516+
517+
@item @code{state}
518+
State of the link, as an integer.  Use @code{int->operstate} from
519+
@code{(netlink constant)} to get a symbol, such as @code{IF_OPER_UP}.
520+
521+
@item @code{mode}
522+
Mode of the link.  0 means @code{DORMANT}, 1 means @code{DEFAULT}.
523+
524+
@item @code{group}
525+
Identifier of the group it belongs to.  0 for @code{default}.
526+
527+
@item @code{qlen}
528+
Size of the queue.
529+
530+
@item @code{addr}
531+
Ethernet address of the link, as a string.
532+
533+
@item @code{brd}
534+
Broadcast (ethernet) address of the link, as a string.
535+
536+
@end table
537+
@end deffn
538+
539+
@deffn {Scheme Procedure} get-links
540+
Returns the list of existing links in the system, as a list of @code{<link>}
541+
objects.
542+
@end deffn
543+
544+
@deffn {Sceme Procedure} print-link @var{link}
545+
Display @var{link} on the standard output, using a format similar to
546+
@command{ip link} from @code{iproute2}.
547+
@end deffn
548+
486549
@deffn {Scheme Procedure} link-set @var{device} [#:up @code{#f}] @
487550
  [#:down @code{#f}] [#:type @code{#f}] [#:arp-on @code{#f}] @
488551
  [#:arp-off @code{#f}] [#:dynamic-on @code{#f}] [#:dynamic-off @code{#f}] @

ip/link.scm

3232
            link-del
3333
            link-set
3434
            link-show
35-
            link-name->index))
35+
            link-name->index
36+
            get-links
37+
            print-link
38+
39+
            <link> make-link link?
40+
            link-name link-id link-type link-flags link-mtu link-qdisc
41+
            link-state link-mode link-group link-qlen link-addr link-brd))
3642
3743
(define-record-type <link>
3844
  (make-link name id type flags mtu qdisc state mode group qlen addr brd)

109115
      (close-socket sock)
110116
      links)))
111117
118+
(define print-link
119+
  (match-lambda
120+
    (($ <link> name id type flags mtu qdisc state mode group qlen addr brd)
121+
     (format #t "~a: ~a: <~a>" id name
122+
             (string-join 
123+
               (map
124+
                 (lambda (s)
125+
                   ;; IFF_UP -> UP
126+
                   (substring (symbol->string s) 4))
127+
                 flags)
128+
               ","))
129+
     (when mtu
130+
       (format #t " mtu ~a" mtu))
131+
     (when qdisc
132+
       (format #t " qdisc ~a" qdisc))
133+
     (when state
134+
       (format #t " state ~a"
135+
               (substring (symbol->string (int->operstate state)) 8)))
136+
     (when mode
137+
       (format #t " mode ~a" (match mode (0 "DEFAULT") (1 "DORMANT"))))
138+
     (when group
139+
       (format #t " group ~a" (match group (0 "DEFAULT"))))
140+
     (when qlen
141+
       (format #t " qlen ~a" qlen))
142+
     (newline)
143+
     (cond
144+
       ((equal? type ARPHRD_ETHER)
145+
        (format #t "    link/ether ~a brd ~a~%" addr brd))
146+
       ((equal? type ARPHRD_LOOPBACK)
147+
        (format #t "    link/loopback ~a brd ~a~%" addr brd))))))
148+
112149
(define* (link-show #:key (device #f) (group #f) (up #f) (master #f) (vrf #f)
113150
                    (type #f))
114151
  "Return a list whose elements represent the data about the links.  If a key
115152
is given, the resulting list is limited to those elements that match the given
116153
criteria."
117-
  (define print-link
118-
    (match-lambda
119-
      (($ <link> name id type flags mtu qdisc state mode group qlen addr brd)
120-
       (format #t "~a: ~a: <~a>" id name
121-
               (string-join 
122-
                 (map
123-
                   (lambda (s)
124-
                     ;; IFF_UP -> UP
125-
                     (substring (symbol->string s) 4))
126-
                   flags)
127-
                 ","))
128-
       (when mtu
129-
         (format #t " mtu ~a" mtu))
130-
       (when qdisc
131-
         (format #t " qdisc ~a" qdisc))
132-
       (when state
133-
         (format #t " state ~a"
134-
                 (substring (symbol->string (int->operstate state)) 8)))
135-
       (when mode
136-
         (format #t " mode ~a" (match mode (0 "DEFAULT") (1 "DORMANT"))))
137-
       (when group
138-
         (format #t " group ~a" (match group (0 "DEFAULT"))))
139-
       (when qlen
140-
         (format #t " qlen ~a" qlen))
141-
       (newline)
142-
       (cond
143-
         ((equal? type ARPHRD_ETHER)
144-
          (format #t "    link/ether ~a brd ~a~%" addr brd))
145-
         ((equal? type ARPHRD_LOOPBACK)
146-
          (format #t "    link/loopback ~a brd ~a~%" addr brd))))))
147-
148154
  (for-each
149155
    (lambda (link)
150156
      (match link