guile-netlink/doc/guile-netlink.texi

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