guile-netlink.texi
| 1 | \input texinfo |
| 2 | @setfilename guile-netlink.info |
| 3 | @documentencoding UTF-8 |
| 4 | @settitle guile-netlink |
| 5 | |
| 6 | @include version.texi |
| 7 | |
| 8 | @copying |
| 9 | Copyright @copyright{} 2020 Julien Lepiller |
| 10 | Copyright @copyright{} 2025 Dale Mellor |
| 11 | |
| 12 | @quotation |
| 13 | Permission is granted to copy, distribute and/or modify this document |
| 14 | under the terms of the GNU Free Documentation License, Version 1.3 or |
| 15 | any later version published by the Free Software Foundation; with no |
| 16 | Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A |
| 17 | copy of the license is included in the section entitled ``GNU Free |
| 18 | Documentation License''. |
| 19 | @end quotation |
| 20 | @end copying |
| 21 | |
| 22 | @titlepage |
| 23 | @end titlepage |
| 24 | |
| 25 | @contents |
| 26 | |
| 27 | @node Top |
| 28 | @top guile-netlink |
| 29 | |
| 30 | This document describes guile-netlink version @value{VERSION}, a guile |
| 31 | implementation of the netlink protocol. |
| 32 | |
| 33 | @menu |
| 34 | * Introduction:: What is netlink? |
| 35 | * IP Library:: High-level procedures for network devices. |
| 36 | * ACPI API:: High-level procedures for receiving ACPI event notifications. |
| 37 | * API Reference:: Low-level procedure reference. |
| 38 | |
| 39 | * Concept Index:: Concepts. |
| 40 | * Programming Index:: Data types, procedures, and variables. |
| 41 | |
| 42 | @detailmenu |
| 43 | --- The Detailed Node Listing --- |
| 44 | |
| 45 | IP Library |
| 46 | |
| 47 | * Link:: |
| 48 | * Addr:: |
| 49 | * Route:: |
| 50 | |
| 51 | ACPI API |
| 52 | |
| 53 | * ACPI example:: |
| 54 | * ACPI reference:: |
| 55 | |
| 56 | Low-Level API Reference |
| 57 | |
| 58 | * Common API:: |
| 59 | * Netlink API:: |
| 60 | * Rtnetlink API:: |
| 61 | |
| 62 | Common API |
| 63 | |
| 64 | * Data Types:: |
| 65 | * Constants:: |
| 66 | * Netlink Connections:: |
| 67 | |
| 68 | Netlink API |
| 69 | |
| 70 | * Netlink Headers:: |
| 71 | * Standard Message Types:: |
| 72 | |
| 73 | Rtnetlink API |
| 74 | |
| 75 | * Routing Attributes:: |
| 76 | * Link Messages:: |
| 77 | * Address Messages:: |
| 78 | |
| 79 | @end detailmenu |
| 80 | @end menu |
| 81 | |
| 82 | @node Introduction |
| 83 | @chapter Introduction |
| 84 | @cindex netlink |
| 85 | @cindex routing protocol |
| 86 | @cindex ACPI protocol |
| 87 | @cindex contributions |
| 88 | @cindex high-level |
| 89 | @cindex intermediate-level |
| 90 | @cindex low-level |
| 91 | |
| 92 | Netlink is an inter-process communication protocol that can be used for |
| 93 | communication between processes, or with the kernel. It is implemented by |
| 94 | Linux. |
| 95 | |
| 96 | Many protocols exist on top of Netlink. For example, the @emph{routing} |
| 97 | protocol is used to configure network-related functions in the kernel, such as |
| 98 | firewall, route table or IP addresses of interfaces, and the @emph{ACPI} |
| 99 | protocol is used by the Linux kernel to send notifications of changes in |
| 100 | system power usage. (Deep down, these two protocols are very different beasts, |
| 101 | with the former being regarded as a @emph{classical} protocol with very fixed |
| 102 | message formats, and the latter being an example of a @emph{generic} protocol |
| 103 | which entails subscribing to a broadcast channel and deeply introspecting the |
| 104 | transmitted data.) |
| 105 | |
| 106 | Lots of other protocols, or families, are provided by Linux, but this library |
| 107 | currently only services the two examples described above. Of course, |
| 108 | contributions of code which implements others are always welcome! |
| 109 | |
| 110 | This library implements code at three levels: at high-level we have the |
| 111 | @ref{IP Library} and @ref{ACPI API} which give immediate access to the |
| 112 | protocols described above; at intermediate level there is the |
| 113 | @ref{Rtnetlink API} which defines special structures and functions used in the |
| 114 | routing protocol, and many of which are borrowed by the ACPI protocol; finally |
| 115 | the low-level details are outlined in the @ref{API Reference}. |
| 116 | |
| 117 | @node IP Library |
| 118 | @chapter IP Library |
| 119 | |
| 120 | This library comes with higher-level procedures that let you access and modify |
| 121 | the state of network on your computer. |
| 122 | |
| 123 | @menu |
| 124 | * Link:: |
| 125 | * Addr:: |
| 126 | * Route:: |
| 127 | @end menu |
| 128 | |
| 129 | @node Link |
| 130 | @section Link |
| 131 | |
| 132 | The @code{(ip link)} module introduces procedures to access and modify the |
| 133 | network links on your machine. They are equivalent to the @command{ip link} |
| 134 | family of commands, from @code{iproute2}. |
| 135 | |
| 136 | @deffn {Datatype} <link> |
| 137 | |
| 138 | Datatype representing the status of a network link. |
| 139 | |
| 140 | get-links |
| 141 | print-link |
| 142 | |
| 143 | <link> make-link link? |
| 144 | link-name link-id link-type link-flags link-mtu link-qdisc |
| 145 | link-state link-mode link-group link-qlen link-addr link-brd |
| 146 | |
| 147 | @table @asis |
| 148 | @item @code{name} |
| 149 | Name of the link, such as @code{"enp1s0"}. |
| 150 | |
| 151 | @item @code{id} |
| 152 | Index of the link, a unique number used to identify the link. |
| 153 | |
| 154 | @item @code{type} |
| 155 | Type of the link, as an integer. |
| 156 | |
| 157 | @item @code{flags} |
| 158 | Flags associated with the device, as a list of symbols, such as |
| 159 | @code{'(UP LOOPBACK)}. |
| 160 | |
| 161 | @item @code{mtu} |
| 162 | MTU of the link, as an integer. |
| 163 | |
| 164 | @item @code{qdisc} |
| 165 | Queuing discipline of the link, as a string, such as @code{"noqueue"}. |
| 166 | |
| 167 | @item @code{state} |
| 168 | State of the link, as an integer. Use @code{int->operstate} from |
| 169 | @code{(netlink constant)} to get a symbol, such as @code{IF_OPER_UP}. |
| 170 | |
| 171 | @item @code{mode} |
| 172 | Mode of the link. 0 means @code{DORMANT}, 1 means @code{DEFAULT}. |
| 173 | |
| 174 | @item @code{group} |
| 175 | Identifier of the group it belongs to. 0 for @code{default}. |
| 176 | |
| 177 | @item @code{qlen} |
| 178 | Size of the queue. |
| 179 | |
| 180 | @item @code{addr} |
| 181 | Ethernet address of the link, as a string. |
| 182 | |
| 183 | @item @code{brd} |
| 184 | Broadcast (ethernet) address of the link, as a string. |
| 185 | |
| 186 | @end table |
| 187 | @end deffn |
| 188 | |
| 189 | @deffn {Scheme Procedure} get-links |
| 190 | Returns the list of existing links in the system, as a list of @code{<link>} |
| 191 | objects. |
| 192 | @end deffn |
| 193 | |
| 194 | @deffn {Scheme Procedure} wait-for-link @var{name} [#:blocking? #t] |
| 195 | Wait until a link called @var{name} (a string such as @code{"ens3"}) shows |
| 196 | up. |
| 197 | |
| 198 | When @var{blocking?} is false, use a non-blocking socket and cooperate via |
| 199 | @code{current-read-waiter}---useful when using Fibers. |
| 200 | @end deffn |
| 201 | |
| 202 | @deffn {Sceme Procedure} print-link @var{link} |
| 203 | Display @var{link} on the standard output, using a format similar to |
| 204 | @command{ip link} from @code{iproute2}. |
| 205 | @end deffn |
| 206 | |
| 207 | @deffn {Scheme Procedure} link-set @var{device} [#:up @code{#f}] @ |
| 208 | [#:down @code{#f}] [#:type @code{#f}] [#:arp-on @code{#f}] @ |
| 209 | [#:arp-off @code{#f}] [#:dynamic-on @code{#f}] [#:dynamic-off @code{#f}] @ |
| 210 | [#:multicast-on @code{#f}] [#:multicast-off @code{#f}] @ |
| 211 | [#:allmulticast-on @code{#f}] [#:allmulticast-off @code{#f}] @ |
| 212 | [#:promisc-on @code{#f}] [#:promisc-off @code{#f}] [#:trailers-on @code{#f}] @ |
| 213 | [#:trailers-off @code{#f}] [#:carrier-on @code{#f}] [#:carrier-off @code{#f}] @ |
| 214 | [#:txqueuelen @code{#f}] [#:name @code{#f}] [#:address @code{#f}] @ |
| 215 | [#:broadcast @code{#f}] [#:mtu @code{#f}] [#:netns @code{#f}] |
| 216 | Modify an existing link and set its flags and attributes to the ones specified |
| 217 | by the various keywords. When a keyword is omited, the corresponding attribute |
| 218 | is not changed. |
| 219 | |
| 220 | @var{device} can be a device index (as a number) or a device name (as a string). |
| 221 | |
| 222 | Do not set @code{#:up} and @code{#:down} at the same time. Do not set |
| 223 | @code{*-on} and @code{*-off} at the same time. |
| 224 | @end deffn |
| 225 | |
| 226 | @deffn {Scheme Procedure} link-show [#:device @code{#f}] [#:group @code{#f}] @ |
| 227 | [#:up @code{#f}] [#:master @code{#f}] [#:vrf @code{#f}] [#:type @code{#f}] |
| 228 | Print the set of devices on standard output. Setting any of the keyword to a |
| 229 | non-false value will filter the results to only show results that match the |
| 230 | corresponding value. You may set more than one keyword. |
| 231 | @end deffn |
| 232 | |
| 233 | @deffn {Scheme Procedure} link-add @var{name} @var{type} [#:type-args @code{'()}] |
| 234 | Add a new link with given name and type. Additional arguments can be passed to |
| 235 | control the state of the link at creation. @var{type-args} is an association |
| 236 | list containing additional values for the given type. |
| 237 | |
| 238 | When @var{type} is @code{"vlan"}, @var{type-args} can contain a number associated |
| 239 | with @code{'id}: the VLAN id to be created and a link name associated with |
| 240 | @code{"link"}: the name of the link on which the vlan is created. |
| 241 | |
| 242 | The following is an example in which we create a new vlan link: |
| 243 | @example |
| 244 | ;; same as "ip l add link eth0 name eth0.7 type vlan id 7" |
| 245 | (link-add "eth0.7" "vlan" #:type-args '((id . 7) (link . "eth0"))) |
| 246 | @end example |
| 247 | |
| 248 | When @var{type} is @code{"veth"}, @var{type-args} can contain a string associated |
| 249 | with @code{'peer}: the name of the peer. |
| 250 | |
| 251 | The following is an example in which we create a new veth (virtual ethernet) |
| 252 | pair and give them a name: |
| 253 | @example |
| 254 | ;; same as "ip l add v0p0 type veth peer v0p1" |
| 255 | (link-add "v0p0" "veth" #:type-args '((peer . "v0p1"))) |
| 256 | @end example |
| 257 | @end deffn |
| 258 | |
| 259 | @deffn {Scheme Procedure} link-del @var{device} |
| 260 | Delete a link. @var{device} can contain the name of the link, as a string, |
| 261 | or its index, as a number. |
| 262 | @end deffn |
| 263 | |
| 264 | @node Addr |
| 265 | @section Addr |
| 266 | |
| 267 | The @code{(ip addr)} module introduces procedures to access and modify the |
| 268 | network addresses on your machine. They are equivalent to the @command{ip addr} |
| 269 | family of commands, from @code{iproute2}. |
| 270 | |
| 271 | @deffn {Scheme Procedure} addr-add @var{device} @var{cidr} [@var{#:ipv6?} #f] @ |
| 272 | [@var{#:peer} @code{(cidr->addr cidr)}] [@var{#:broadcast} #f] @ |
| 273 | [@var{#:anycast} #f] [@var{#:label} #f] [@var{#:scope} @code{'global}] @ |
| 274 | [@var{#:metric} #f] [@var{#:home?} #f] [@var{#:mngtmpaddr?} #f] @ |
| 275 | [@var{#:nodad?} #f] [@var{optimistic?} #f] [@var{noprefixroute?} #f] @ |
| 276 | [@var{#:autojoin?} #f] |
| 277 | Add the address given in @var{cidr} to @var{device}. @var{device} can |
| 278 | contain the name of the link, as a string, or its index, as a number. |
| 279 | |
| 280 | @var{cidr} must be a string containing the address and prefix length, in |
| 281 | CIDR notation (@code{addr/prefix}). |
| 282 | |
| 283 | @example |
| 284 | (addr-add "enp1s0" "192.0.2.15/24") |
| 285 | @end example |
| 286 | |
| 287 | If you wish to add an IPv6 address instead, set @code{#:ipv6} to @code{#t}, |
| 288 | as in the following example. |
| 289 | |
| 290 | @example |
| 291 | (addr-add "enp1s0" "2001:db8::1a4c/64" #:ipv6? #t) |
| 292 | @end example |
| 293 | |
| 294 | Note that using the wrong ip type with the wrong value for the @code{#:ipv6?} |
| 295 | flag will result in a @code{Bad address} exception from inet-pton. |
| 296 | |
| 297 | Additional flags are available; they follow the same semantics as Iproute2. |
| 298 | For pointopoint interfaces, you can specify the address of the remote endpoint |
| 299 | with @var{#:peer}. You can specify a broadcast or anycast address with |
| 300 | @var{#:broadcast} and @var{#:anycast}. All three require an IP address passed |
| 301 | as a string when specified. |
| 302 | |
| 303 | You can specify a label for the address with @var{#:label}. The parameter must |
| 304 | be a string that is either the name of the device, or starts with the name of |
| 305 | the device, followed by a colon and must contain at most 15 characters. |
| 306 | |
| 307 | You can specify a scope with @var{#:scope}, whose value is either @code{'global}, |
| 308 | @code{'link}, @code{'host} or a numeric value. |
| 309 | |
| 310 | You can specify the priority of the prefix route associated with this address |
| 311 | using @code{#:metric}, a number. |
| 312 | |
| 313 | Finally, this procedures accepts address configuration flags, whose values are |
| 314 | booleans. They are unset by default. Some flags only work for IPv6 addresses, |
| 315 | those are @var{#:home?} to designate this address as the ``home address'', |
| 316 | @var{#:mngtmpaddr?}, @var{#:nodad?} and @var{#:optimistic?}. The flags |
| 317 | @var{#:noprefixroute?} and @var{#:autojoin?} can be set for IPv4 and IPv6 |
| 318 | addresses. |
| 319 | @end deffn |
| 320 | |
| 321 | @deffn {Scheme Procedure} addr-del @var{device} @var{cidr} [@var{#:ipv6?} #f] @ |
| 322 | [@var{#:peer} @code{(cidr->addr cidr)}] [@var{#:broadcast} #f] @ |
| 323 | [@var{#:anycast} #f] [@var{#:label} #f] [@var{#:scope} @code{'global}] @ |
| 324 | [@var{#:metric} #f] [@var{#:home?} #f] [@var{#:mngtmpaddr?} #f] @ |
| 325 | [@var{#:nodad?} #f] [@var{optimistic?} #f] [@var{noprefixroute?} #f] @ |
| 326 | [@var{#:autojoin?} #f] |
| 327 | Delete the address given in @var{cidr} from @var{device}. @var{device} can |
| 328 | contain the name of the link, as a string, or its index, as a number. |
| 329 | |
| 330 | @var{cidr} must be a string containing the address and prefix length, in |
| 331 | CIDR notation (@code{addr/prefix}). |
| 332 | |
| 333 | @example |
| 334 | (addr-del "enp1s0" "192.0.2.15/24") |
| 335 | @end example |
| 336 | |
| 337 | If you wish to remove an IPv6 address instead, set @code{#:ipv6} to @code{#t}, |
| 338 | as in the following example. |
| 339 | |
| 340 | @example |
| 341 | (addr-del "enp1s0" "2001:db8::1a4c/64" #:ipv6? #t) |
| 342 | @end example |
| 343 | |
| 344 | Note that using the wrong ip type with the wrong value for the @code{#:ipv6?} |
| 345 | flag will result in a @code{Bad address} exception from inet-pton. |
| 346 | |
| 347 | Additional flags are available, see the description in @code{addr-add} for more |
| 348 | details. |
| 349 | @end deffn |
| 350 | |
| 351 | @deffn {Scheme Procedure} addr-show [@var{device}] |
| 352 | Print the list of addresses for each device on standard output. Setting |
| 353 | @code{device} to a link name or link identifier will restrict the output |
| 354 | to addresses of that device. |
| 355 | @end deffn |
| 356 | |
| 357 | @node Route |
| 358 | @section Route |
| 359 | |
| 360 | The @code{(ip route)} module introduces procedures to access and modify the |
| 361 | network routes on your machine. They are equivalent to the @command{ip route} |
| 362 | family of commands, from @code{iproute2}. |
| 363 | |
| 364 | @deffn {Scheme Procedure} route-add @var{dest} [@var{#:ipv6?} #f] @ |
| 365 | [@var{#:device} #f] [@var{#:table} RT_TABLE_MAIN] [@var{#:protocol} #f] @ |
| 366 | [@var{#:scope} RT_SCOPE_LINK] [@var{#:type} RTN_UNICAST] @ |
| 367 | [@var{#:priority} #f] [@var{#:src} #f] [@var{#:via} #f] |
| 368 | Add the route described by the argmuents. @var{dest} is the destination network, |
| 369 | in cidr notation (@code{addr/prefix}) or the string @code{"default"}. |
| 370 | |
| 371 | @var{#:device} is the name or index of a network link. @var{#:table} is the |
| 372 | index of a routing table, one of @code{RT_TABLE_COMPAT}, @code{RT_TABLE_DEFAULT}, |
| 373 | @code{RT_TABLE_MAIN} or @code{RT_TABLE_LOCAL}, as defined in |
| 374 | @code{(netlink constant)}. |
| 375 | |
| 376 | If it is set, @var{#:protocol} must be the routing protocol, @code{RTPROT_*}, |
| 377 | as defined in @code{(netlink constant)}. |
| 378 | |
| 379 | @var{#:scope} must be the scope of the route, one of @code{RT_SCOPE_*}, as |
| 380 | defined in @code{(netlink constant)}. |
| 381 | |
| 382 | @var{#:type} must be the type of route, one of @code{RTN_*}, as defined in |
| 383 | @code{(netlink constant)}. |
| 384 | |
| 385 | If set, @var{#:priority} is a number specifying the priority of the rule |
| 386 | when the kernel is looking for a matching rule. This is also known as the |
| 387 | metric of the route. |
| 388 | |
| 389 | If set, @var{#:src} is the source address in cidr notation, or as a single |
| 390 | address. |
| 391 | |
| 392 | If set, @var{#:via} is the gateway address. This is not in cidr notation, as |
| 393 | the gateway is a single address, not a network. |
| 394 | |
| 395 | @example |
| 396 | (route-add "default" #:device "enp1s0" #:via "192.0.2.1") |
| 397 | (route-add "192.0.2.0/24" #:device "enp1s0" #:src "192.0.2.15") |
| 398 | @end example |
| 399 | |
| 400 | If you wish to add an IPv6 route instead, set @code{#:ipv6} to @code{#t}, |
| 401 | as in the following example. |
| 402 | |
| 403 | @example |
| 404 | (addr-add "2001:db8::/64" #:device "enp1s0" #:src "2001:db8::1a4c" #:ipv6? #t) |
| 405 | @end example |
| 406 | |
| 407 | Note that using the wrong ip type with the wrong value for the @code{#:ipv6?} |
| 408 | flag will result in a @code{Bad address} exception from inet-pton. |
| 409 | @end deffn |
| 410 | |
| 411 | @deffn {Scheme Procedure} route-del @var{dest} [@var{#:ipv6?} #f] @ |
| 412 | [@var{#:device} #f] [@var{#:table} RT_TABLE_MAIN] [@var{#:protocol} #f] @ |
| 413 | [@var{#:scope} #f] [@var{#:type} #f] [@var{#:priority} #f] @ |
| 414 | [@var{#:src} #f] [@var{#:via} #f] |
| 415 | Delete the route given in arguments. The arguments follow the same structure |
| 416 | as @code{route-add}. By specifying more arguments, you can narrow down the |
| 417 | search for the rule to delete further. Each call will only remove one route, |
| 418 | so being more precise ensures you target the rule you wish to delete. It |
| 419 | is not clear which route is deleted if multiple routes match your query. |
| 420 | @end deffn |
| 421 | |
| 422 | @deffn {Scheme Procedure} route-show [@var{#:table} RT_TABLE_MAIN] @ |
| 423 | [@var{#:family} AF_UNSPEC] |
| 424 | Print the list of routes on standard output. Note that, contrary to |
| 425 | @command{ip route show}, we show both IPv4 and IPv6 routes. To narrow down the |
| 426 | number of routes displayed, you can specify the family as in this example. |
| 427 | |
| 428 | @example |
| 429 | (route-show #:family AF_INET6) |
| 430 | @end example |
| 431 | @end deffn |
| 432 | |
| 433 | @node ACPI API |
| 434 | @chapter ACPI API |
| 435 | @cindex high-level |
| 436 | @cindex read-selector procedure |
| 437 | @cindex process-data procedure |
| 438 | @cindex close-connection procedure |
| 439 | |
| 440 | This chapter describes the high-level procedures contained in the |
| 441 | @code{(netlink acpi)} module that let you monitor the kernel for ACPI event |
| 442 | notifications. At top level, the requirement is to connect to the kernel's |
| 443 | netlink system, and then you receive three procedures for handling |
| 444 | notifications on the connection. The first, nominally called |
| 445 | @code{(read-selector)}, provides a file descriptor which you can use in a |
| 446 | @code{select} read-list to wait for a notification to become available on the |
| 447 | connection. |
| 448 | |
| 449 | Once you know that data are there waiting to be read, you use the second |
| 450 | provided procedure: @code{(process-data @var{action})}, where the @var{action} |
| 451 | you supply must be a procedure which takes in an @code{event} object, and does |
| 452 | whatever the application needs to do. Data can be extracted from this object |
| 453 | with @code{event-device-class}, @code{event-bus-id}, @code{event-kind} and |
| 454 | @code{event-data}. |
| 455 | |
| 456 | When all is done, the connection should be closed with a call to the third |
| 457 | provided function: @code{(close-connection)}. |
| 458 | |
| 459 | @menu |
| 460 | * ACPI example:: |
| 461 | * ACPI reference:: |
| 462 | @end menu |
| 463 | |
| 464 | @node ACPI example |
| 465 | @section acpi-listener example |
| 466 | @cindex acpi-listener application |
| 467 | @cindex ACPI example usage |
| 468 | |
| 469 | The following code snippet is the heart of the acpi-listener application |
| 470 | distributed with guile-netlink. When you run the command at the command line |
| 471 | (it takes no arguments), it will sit there silently and forever, waiting for |
| 472 | an ACPI event to happen (perhaps you can put your computer to suspended sleep |
| 473 | and then re-awaken it), at which time a message will be printed with the data |
| 474 | of that event. |
| 475 | |
| 476 | @lisp |
| 477 | ;; Use guile-netlinkʼs ACPI interface. The use of a prefix is discretionary; |
| 478 | ;; it is useful to have in example code. |
| 479 | (use-modules ((netlink acpi) #:prefix ACPI::)) |
| 480 | |
| 481 | |
| 482 | (define (action event) ;; [1] |
| 483 | ((@@ (ice-9 format) format) |
| 484 | #t |
| 485 | "device: ~20a, bus ID: ~15a, type: ~8,'0x, data: ~8,'0x\n" |
| 486 | (ACPI::event-device-class event) |
| 487 | (ACPI::event-bus-id event) |
| 488 | (ACPI::event-kind event) |
| 489 | (ACPI::event-data event))) |
| 490 | |
| 491 | |
| 492 | ((@@ (ice-9 receive) receive) |
| 493 | (ACPI::read-selector ACPI::process-events ACPI::close-connection) ;; [3] |
| 494 | (ACPI::connect-events) ;; [2] |
| 495 | |
| 496 | (let loop () ;; [4] |
| 497 | (select (list (ACPI::read-selector)) '() '()) |
| 498 | (ACPI::process-events action) |
| 499 | (loop)) |
| 500 | |
| 501 | (ACPI::close-connection)) ;; [5] |
| 502 | @end lisp |
| 503 | |
| 504 | The action we take on receiving events is defined as @code{action} [1], and |
| 505 | simply writes the data elements of the @var{event} to the command-line. |
| 506 | |
| 507 | The connection to the kernel is made with the @code{ACPI::connect-events} [2] |
| 508 | procedure call, which gives us the @code{ACPI::read-selector}, |
| 509 | @code{ACPI::process-events} and @code{ACPI::close-connection} [3] procedures |
| 510 | for managing the connection. |
| 511 | |
| 512 | We then simply go into a tight loop [4] waiting for data, and processing that |
| 513 | when it comes in. |
| 514 | |
| 515 | If, by some unattainable miracle, we get out of that loop and want to stop |
| 516 | monitoring for kernel events, we can call the @code{ACPI::close-connection} |
| 517 | procedure [5]. |
| 518 | |
| 519 | @node ACPI reference |
| 520 | @section ACPI reference |
| 521 | |
| 522 | The following procedures are available in the @code{(netlink acpi)} module. |
| 523 | |
| 524 | @deffn {Scheme Procedure} connect-events |
| 525 | Open a connection to the kernel, and set it up to listen for ACPI event |
| 526 | notifications. The procedure returns three new procedures for managing this |
| 527 | connection: @dfn{read-selector} which returns a file descriptor which can be |
| 528 | passed to the @code{select} procedure to wait for data from the kernel, |
| 529 | @dfn{process-data @var{action}} which reads and evaluates the data, and calls |
| 530 | @code{ACTION @var{event}} on it, and finally @dfn{close-connection} which will |
| 531 | terminate the connection with the kernel. |
| 532 | @end deffn |
| 533 | |
| 534 | @deffn {Scheme Procedure} event-display-class @var{event} |
| 535 | Extract the display class from the @var{event}. This will be a string. |
| 536 | @end deffn |
| 537 | |
| 538 | @deffn {Scheme Procedure} event-bus-id @var{event} |
| 539 | Extract the bus-id from the @var{event}, again a string. |
| 540 | @end deffn |
| 541 | |
| 542 | @deffn {Scheme Procedure} event-kind @var{event} |
| 543 | Extract the type of the @var{event}, a number, nominally unsigned 32 bit. |
| 544 | @end deffn |
| 545 | |
| 546 | @deffn {Scheme Procedure} event-data @var{event} |
| 547 | Extract the data from the @var{event}, a number, also nominally unsigned 32 |
| 548 | bit. |
| 549 | @end deffn |
| 550 | |
| 551 | @node API Reference |
| 552 | @chapter Low-Level API Reference |
| 553 | |
| 554 | @menu |
| 555 | * Common API:: |
| 556 | * Netlink API:: |
| 557 | * Rtnetlink API:: |
| 558 | @end menu |
| 559 | |
| 560 | @node Common API |
| 561 | @section Common API |
| 562 | |
| 563 | Guile-netlink implements a common API for expressing other protocols. This |
| 564 | section describes how to use this API to augment guile-netlink with additional |
| 565 | protocols. |
| 566 | |
| 567 | @menu |
| 568 | * Data Types:: |
| 569 | * Constants:: |
| 570 | * Netlink Connections:: |
| 571 | @end menu |
| 572 | |
| 573 | @node Data Types |
| 574 | @subsection Data Types |
| 575 | |
| 576 | Guile-netlink defines data types that are used in the various Netlink protocols. |
| 577 | We need to be able to serialize and deserialize data that guile-netlink |
| 578 | understands, but we also want to let users of guile-netlink extend this process |
| 579 | easily. This need has lead to the creating of the following data structure, |
| 580 | defined in @code{(netlink data}). |
| 581 | |
| 582 | @deffn {Datatype} nl-data |
| 583 | |
| 584 | @table @asis |
| 585 | @item @code{data} |
| 586 | The data that is held by this record. |
| 587 | |
| 588 | @item @code{size-proc} |
| 589 | A procedure that takes a data (of the same type as the data recorded in the |
| 590 | @code{data} field) and returns the size of its serialization. |
| 591 | |
| 592 | @item @code{serialization-proc} |
| 593 | A procedure that takes a data (of the same type as the data recorded in the |
| 594 | @code{data} field), the position at which to start serializing, and a |
| 595 | bytevector in which to serialize. This procedure should modify the bytevector |
| 596 | and its return value is ignored. |
| 597 | |
| 598 | @end table |
| 599 | @end deffn |
| 600 | |
| 601 | The module also defines the following function, that takes a @code{nl-data} |
| 602 | structure and provides its serialization in a bytevector: |
| 603 | |
| 604 | @deffn {Scheme Procedure} serialize @var{data} @var{pos} @var{bv} |
| 605 | Takes a @code{nl-data} structure as @var{data}, a position @var{pos} in |
| 606 | the bytevector @var{bv}, and returns an unspecified value. |
| 607 | |
| 608 | This function updates the bytevector and adds the serialization of @var{data} |
| 609 | into @var{bv} at @var{pos}. |
| 610 | @end deffn |
| 611 | |
| 612 | By providing a @code{nl-data} structure, we defer the knowledge of how to |
| 613 | serialize the data to the structure itself, instead of the @code{serialize} |
| 614 | function. This allows for more flexibility and extensibility, as the user |
| 615 | of the procedure can pass any kind of data, even if it is not yet supported by |
| 616 | guile-netlink. |
| 617 | |
| 618 | Similarly, we need to be able to deserialize netlink answers into a data |
| 619 | structure. To do so, we also defer the knowledge of the datastructure to |
| 620 | deserialize to, to a decoder structure that is passed to the deserialization |
| 621 | procedure. @code{(netlink data)} also defines the following procedures to |
| 622 | deserialize data: |
| 623 | |
| 624 | @deffn {Scheme Procedure} deserialize @var{type} @var{decoder} @var{bv} @var{pos} |
| 625 | Takes a bytevector @var{bv} and starts deserializing the data starting at |
| 626 | position @var{pos}. To do so, it uses the @var{type} variable as the lookup |
| 627 | key in the @var{decoder}. @var{type} is a symbol that represents the type of |
| 628 | data to deserialize to. |
| 629 | |
| 630 | The decoder is a structure that associates each known type to its deserializer |
| 631 | (a function that takes a decoder, a bytevector and a position and returns some |
| 632 | data) and an alist that associates a type (an integer, as returned by the |
| 633 | protocol in use) to the proper decoder of that type. |
| 634 | @end deffn |
| 635 | |
| 636 | @deffn {Scheme Procedure} get-current-deserialize @var{decoder} @var{current-type} |
| 637 | Takes a decoder and a type, and returns the deserialization procedure associated |
| 638 | with the type (a symbol) in @var{decoder}. |
| 639 | @end deffn |
| 640 | |
| 641 | @deffn {Scheme Procedure} get-next-deserialize @var{decoder} @var{current-type} @ |
| 642 | @var{target-type} |
| 643 | Takes a decoder, a type (a symbol that represents the type of data being |
| 644 | deserialized) and another type (an integer as returned by the protocol), and |
| 645 | returns the deserialization procedure needed to continue decoding the data |
| 646 | associated with the currently being deserialized data. |
| 647 | |
| 648 | For example, when decoding an answer in the netlink protocol, we first deserialize |
| 649 | the header into a @code{message} structure. That header contains a type field |
| 650 | that contains an integer constant representing the type of data of the body. |
| 651 | Similarly, when deserializing a routing attribute in the rtnetlink protocol, |
| 652 | we first find a header of the attribute that defines an integer constant |
| 653 | corresponding to the type of attribute in the body. |
| 654 | |
| 655 | By knowing the context in which the type is declared, this procedure can return |
| 656 | the correct deserializing procedure. For instance, when deserializing a |
| 657 | @code{message}, type @code{16} means @code{RTM_NEWLINK} in the rtnetlink |
| 658 | protocol, whereas it means @code{IFLA_OPERSTATE} when deserializing a |
| 659 | @code{route-attribute}. |
| 660 | @end deffn |
| 661 | |
| 662 | guile-netlink provides the following default decoder for the rtnetlink |
| 663 | protocol in @code{(netlink deserialize)}: |
| 664 | |
| 665 | @deffn {Scheme Variable} %default-route-decoder |
| 666 | Contains the default decoder for the NETLINK_ROUTE protocol. |
| 667 | @end deffn |
| 668 | |
| 669 | For convenience, guile-netlink defines the following structures that can be used |
| 670 | to create a custom decoder. |
| 671 | |
| 672 | @deffn {Scheme Variable} %default-message-decoder |
| 673 | Contains the default association list for the common message types of netlink, |
| 674 | associating each of them to a deserialization procedure. |
| 675 | @end deffn |
| 676 | |
| 677 | @deffn {Scheme Procedure} default-route-attr-decoder @var{deserialize-addr} |
| 678 | Creates the default association list for a route protocol, given the specified |
| 679 | address deserializer. This is useful because the @code{IFA_ADDRESS}, |
| 680 | @code{IFA_BROADCAST}, etc, contain a different type of address depending on |
| 681 | the message type or its header. This is defined an @code{(netlink route attrs)} |
| 682 | and used by the following variables: |
| 683 | @end deffn |
| 684 | |
| 685 | @deffn {Scheme Variable} %default-route-link-attr-decoder |
| 686 | Contains the default association list for the known types of routing attributes |
| 687 | for link messages. This list is defined in @code{(netlink route attrs)}. |
| 688 | @end deffn |
| 689 | |
| 690 | @node Constants |
| 691 | @subsection Constants |
| 692 | |
| 693 | Guile-netlink defines constants used by the Netlink protocols in the |
| 694 | @code{(netlink constant)} module. The constants are the ones present in the |
| 695 | kernel and are too numerous to list here. Please see the source for the |
| 696 | complete list. |
| 697 | |
| 698 | The module also defines the following macro: |
| 699 | |
| 700 | @deffn {Scheme Macro} define-enum @var{integer->symbol} @var{name-spec} ... |
| 701 | This macros defines an enumeration. @var{integer->symbol} is the name of |
| 702 | a procedure that is publicly defined, that takes an integer and returns the |
| 703 | associated symbol in the enumeration. |
| 704 | |
| 705 | The macro also publicly defines variables whose names are in @var{name-spec} |
| 706 | to an integer. |
| 707 | |
| 708 | A @var{name-spec} is either a single name, and the associated value is 0 for |
| 709 | the first @var{name-spec}, or one more than the previous @var{name-spec}. |
| 710 | It can also be a pair of a name and an integer, in which case the associated |
| 711 | value is that integer. For instance: |
| 712 | |
| 713 | @example |
| 714 | (define-enum get-foo FOO0 FOO1 (FOO10 10) FOO11 FOO12) |
| 715 | (get-foo 9) -> #<unspecified> |
| 716 | (get-foo 0) -> FOO0 |
| 717 | FOO11 -> 11 |
| 718 | @end example |
| 719 | @end deffn |
| 720 | |
| 721 | @node Netlink Connections |
| 722 | @subsection Netlink Connections |
| 723 | |
| 724 | The @code{(netlink connection)} module defines the following procedures, used |
| 725 | to connect and communicate with another process or the kernel using a netlink |
| 726 | socket. |
| 727 | |
| 728 | @deffn {Scheme Procedure} get-addr @var{family} @var{pid} @var{groups} |
| 729 | Return a bytevector that represents a netlink address. @var{family} |
| 730 | should be @code{AF_NETLINK}, @var{pid} is the PID of the process with which |
| 731 | to communicate or 0 for the kernel. @var{groups} is an integer representing |
| 732 | the set of broadcast groups to which the connection subscribes. |
| 733 | @end deffn |
| 734 | |
| 735 | @cindex non-blocking socket |
| 736 | @deffn {Scheme Procedure} connect @var{proto} @var{addr} [#:flags 0] |
| 737 | Creates a netlink socket for @var{proto} and binds it to @var{addr}. |
| 738 | |
| 739 | @var{proto} is the integer representing the protocol. For instance, rtnetlink |
| 740 | can be selected by usin @code{NETLINK_ROUTE} (defined in |
| 741 | @code{(netlink constant)}). |
| 742 | |
| 743 | @var{addr} is a bytevector, as returned by @code{get-addr}. |
| 744 | |
| 745 | @var{flags} is a set of additional flags to pass as the second argument |
| 746 | to the @code{socket} system call---e.g., @code{SOCK_NONBLOCK}. |
| 747 | @end deffn |
| 748 | |
| 749 | @deffn {Scheme Procedure} connect-route [#:groups 0] [#:flags 0] |
| 750 | This procedure is a wrapper for @code{connect} that creates a socket for the |
| 751 | rtnetlink protocol, binds it to the kernel and returns it. By passing the |
| 752 | optional @var{groups} keyword, you can select broadcast groups to subscribe to. |
| 753 | |
| 754 | @var{flags} is a set of additional flags to pass as the second argument |
| 755 | to the @code{socket} system call---e.g., @code{SOCK_NONBLOCK}. |
| 756 | @end deffn |
| 757 | |
| 758 | @cindex subscribing, to an rtnetlink group |
| 759 | @deffn {Scheme Procedure} add-socket-membership @var{sock} @var{group} |
| 760 | Make @var{sock} a member of @var{group}, an @code{RTNLGRP_} constant, |
| 761 | meaning that it will be subscribed to events of that group. |
| 762 | |
| 763 | For example, here is how you could create a netlink socket and subscribe |
| 764 | it to the ``link'' group so that it receives notifications for new and |
| 765 | removed links: |
| 766 | |
| 767 | @lisp |
| 768 | (let ((sock (connect-route))) |
| 769 | (add-socket-membership sock RTNLGRP_LINK) |
| 770 | @dots{}) |
| 771 | @end lisp |
| 772 | |
| 773 | This procedure is implemented as a @code{setsockopt} call. |
| 774 | @end deffn |
| 775 | |
| 776 | @deffn {Scheme Procedure} send-msg @var{msg} @var{sock} [#:@var{addr}] |
| 777 | Send @var{msg} (it must be of type message, @xref{Netlink Headers}) to |
| 778 | @var{addr} using @var{sock}. If not passed, @var{addr} is the address of |
| 779 | the kernel. |
| 780 | @end deffn |
| 781 | |
| 782 | @deffn {Scheme Procedure} receive-msg @var{sock} [#:@var{addr}] |
| 783 | Receives a message from @var{sock} from @var{addr}. This procedure is blocking. |
| 784 | If not passed, @var{addr} defaults to the address of the kernel. This |
| 785 | procedure returns the message as a bytevector, that you can deserialize with |
| 786 | @code{deserialize} (@xref{Data Types}) |
| 787 | @end deffn |
| 788 | |
| 789 | @deffn {Scheme Procedure} receive-and-decode-msg @var{sock} @var{decoder} @ |
| 790 | [#:@var{addr}] |
| 791 | Receives one or more messages from @var{sock} from @var{addr}. this procedure |
| 792 | is blocking. If not passed, @var{addr} defaults to the address of the kernel. |
| 793 | This procedure returns a list of messages that were decoded using @var{decoder}. |
| 794 | |
| 795 | When the answer has the @code{NLM_F_MULTI} flag, this procedure decodes the next |
| 796 | message, until it receives a @code{NLMSG_DONE} message. It returns the list |
| 797 | of every netlink messages it received, including the @code{NLMSG_DONE}. |
| 798 | @end deffn |
| 799 | |
| 800 | @node Netlink API |
| 801 | @section Netlink API |
| 802 | |
| 803 | This section introduces the data structures used for all the netlink protocols. |
| 804 | First, we introduce the structure of a netlink message, then we present the |
| 805 | standard types of netlink messages, that can be used with every protocol. |
| 806 | |
| 807 | @menu |
| 808 | * Netlink Headers:: |
| 809 | * Standard Message Types:: |
| 810 | @end menu |
| 811 | |
| 812 | @node Netlink Headers |
| 813 | @subsection Netlink Headers |
| 814 | |
| 815 | The @code{(netlink message)} module defines the message structure that contains |
| 816 | a netlink message. It is composed of a header and a body, and is the data |
| 817 | structure to pass to @code{send-msg} (@xref{Netlink Connections}). |
| 818 | |
| 819 | This module defines the following data structure: |
| 820 | |
| 821 | @deffn {Datatype} message |
| 822 | @table @asis |
| 823 | @item @code{type} |
| 824 | The type of data in the body of the message. For instance, @code{RTM_GETLINK}. |
| 825 | |
| 826 | @item @code{flags} |
| 827 | The set of flags that are set in the header. For instance, |
| 828 | @code{(logior NLM_F_REQUEST NLM_F_DUMP)}. |
| 829 | |
| 830 | @item @code{seq} |
| 831 | The sequence number of the message. If this message is an answer to a request, |
| 832 | it must keep the same sequence number. Otherwise, you must generate a new and |
| 833 | unique sequence number, to track the answers. |
| 834 | |
| 835 | @item @code{pid} |
| 836 | The PID of the receiving process, or 0 for the kernel. |
| 837 | |
| 838 | @item @code{data} |
| 839 | The actual body, as an @code{nl-data} structure. |
| 840 | |
| 841 | @end table |
| 842 | @end deffn |
| 843 | |
| 844 | @node Standard Message Types |
| 845 | @subsection Standard Message Types |
| 846 | |
| 847 | The @code{(netlink standard)} module defines the set of standard message types |
| 848 | and their data type. |
| 849 | |
| 850 | @deffn {Datatype} error-message |
| 851 | @table @asis |
| 852 | @item @code{err} |
| 853 | The error code, as a negative number. |
| 854 | |
| 855 | @item @code{hdr} |
| 856 | The message on which this error applies. |
| 857 | |
| 858 | @end table |
| 859 | |
| 860 | @deffn {Scheme Variable} no-data |
| 861 | This variable defines the absence of data. This is useful when a structure |
| 862 | is expecting a body part, but the protocol specifically defines that it should |
| 863 | not take any data in some cases. For instance, a @code{NLMSG_NOOP} message |
| 864 | takes no data, so the @code{data} field of the message will contain this |
| 865 | @code{no-data} value. |
| 866 | @end deffn |
| 867 | |
| 868 | @end deffn |
| 869 | |
| 870 | @node Rtnetlink API |
| 871 | @section Rtnetlink API |
| 872 | @cindex rtnetlink |
| 873 | @cindex ROUTE_NETLINK |
| 874 | |
| 875 | This section describes the support for rtnetlink in guile-netlink. Rtnetlink |
| 876 | is the protocol responsible for everything related to network routing. It |
| 877 | allows you to manage links, addresses, routing tables, neighbor chaces, |
| 878 | routing rules, queueing disciplines, traffic classes, traffic filters and |
| 879 | more. |
| 880 | |
| 881 | @menu |
| 882 | * Routing Attributes:: |
| 883 | * Link Messages:: |
| 884 | * Address Messages:: |
| 885 | @end menu |
| 886 | |
| 887 | @node Routing Attributes |
| 888 | @subsection Routing Attributes |
| 889 | |
| 890 | The @code{(netlink route attrs)} module defines the following data types: |
| 891 | |
| 892 | @deffn {Datatype} route-attr |
| 893 | This defines a header structure for the attribute, as well as its body. |
| 894 | |
| 895 | @table @asis |
| 896 | @item @code{type} |
| 897 | This is the type of the attribute, for instance @code{IFLA_ADDRESS}. |
| 898 | |
| 899 | @item @code{data} |
| 900 | This is the body of the attribute, ie.@: its value. |
| 901 | @end table |
| 902 | @end deffn |
| 903 | |
| 904 | The module also defines additional data types that are not represented as |
| 905 | a record, but by a simple type. For each of the following types, there is |
| 906 | a @code{make-*-route-attr} procedure to produce a @code{nl-data} value |
| 907 | for this type. There is also @code{deserialize-route-attr-data-*} procedure |
| 908 | to deserialize a value of this type. |
| 909 | |
| 910 | @table @asis |
| 911 | @item @code{u8} |
| 912 | A one-byte unsigned integer |
| 913 | @item @code{u16} |
| 914 | A two-bytes unsigned integer |
| 915 | @item @code{u32} |
| 916 | A four-bytes unsigned integer |
| 917 | @item @code{s32} |
| 918 | A four-bytes signed integer |
| 919 | @item @code{string} |
| 920 | A string |
| 921 | @item @code{ethernet} |
| 922 | An ethernet address. Its value is a string that represents that address, |
| 923 | for instnace @code{"01:23:45:67:89:ab"} |
| 924 | @item @code{ipv4} |
| 925 | An IPv4 address. Its value is a string that represents that address, |
| 926 | for instnace @code{"192.0.2.152"} |
| 927 | @item @code{ipv6} |
| 928 | An IPv6 address. Its value is a string that represents that address, |
| 929 | for instnace @code{"2001:db8::0123:4567:89ab:cdef"} |
| 930 | @item @code{bv} |
| 931 | A bytevector. This is used by default when the type is not supported. |
| 932 | @end table |
| 933 | |
| 934 | @node Link Messages |
| 935 | @subsection Link Messages |
| 936 | |
| 937 | The @code{(netlink route link)} package defines the following data type: |
| 938 | |
| 939 | @deffn {Datatype} link-message |
| 940 | This datatype represents a link message with its routing attributes. This type |
| 941 | of message is expected when using the @var{RTM_*LINK} message types. |
| 942 | |
| 943 | @table @asis |
| 944 | @item @code{family} |
| 945 | The network family, defined as @code{AF_UNSPEC} in the rtnetlink documentation, |
| 946 | although it holds different values in practice. |
| 947 | |
| 948 | @item @code{type} |
| 949 | The device type. |
| 950 | |
| 951 | @item @code{index} |
| 952 | The index of the device. This is used to select a specific device by its index, |
| 953 | or 0 to not filter by device index. |
| 954 | |
| 955 | @item @code{flags} |
| 956 | The device flags. See @code{man 7 netdevices} for a list. |
| 957 | |
| 958 | @item @code{attrs} |
| 959 | A list of attributes. This field must contain a list of @code{nl-data} |
| 960 | structures, not a structure by itself. |
| 961 | @end table |
| 962 | @end deffn |
| 963 | |
| 964 | @node Address Messages |
| 965 | @subsection Address Messages |
| 966 | |
| 967 | The @code{(netlink route addr)} package defines the following data type: |
| 968 | |
| 969 | @deffn {Datatype} addr-message |
| 970 | This datatype represents an address message with its routing attributes. This |
| 971 | type of message is expected when using the @var{RTM_*ADDR} message types. |
| 972 | |
| 973 | @table @asis |
| 974 | @item @code{family} |
| 975 | The network family, either @code{AF_INET} for IPv4 addresses, or @code{AF_INET6} |
| 976 | for IPv6 addresses. |
| 977 | |
| 978 | @item @code{prefix-len} |
| 979 | The prefix length, i.e.@: the length of the prefix mask, in bits, if defined |
| 980 | for the address family. |
| 981 | |
| 982 | @item @code{flags} |
| 983 | Address flags. This can be a flag word of @code{IFA_F_SECONDARY} for secondary |
| 984 | address (old alias interface), @code{IFA_F_PERMANENT} for a permanent |
| 985 | address set by the user and other undocumented flags. |
| 986 | |
| 987 | @item @code{scope} |
| 988 | The address scope. |
| 989 | |
| 990 | @item @code{index} |
| 991 | The index of the device this address is for. |
| 992 | |
| 993 | @item @code{attrs} |
| 994 | A list of attributes. This field must contain a list of @code{nl-data} |
| 995 | structures, not a structure by itself. |
| 996 | @end table |
| 997 | @end deffn |
| 998 | |
| 999 | |
| 1000 | @c ********************************************************************* |
| 1001 | @node Concept Index |
| 1002 | @unnumbered Concept Index |
| 1003 | @printindex cp |
| 1004 | |
| 1005 | @node Programming Index |
| 1006 | @unnumbered Programming Index |
| 1007 | @syncodeindex tp fn |
| 1008 | @syncodeindex vr fn |
| 1009 | @printindex fn |
| 1010 | |
| 1011 | @bye |
| 1012 |