Debian Codegraph @

docs/libnm/libnm-docs.xml

raw
1 <?xml version="1.0"?>
2 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
3 "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
4 <!ENTITY version SYSTEM "version.xml">
5 ]>
6
7 <book id="index" xmlns:xi="http://www.w3.org/2003/XInclude">
8 <bookinfo>
9 <title>libnm Reference Manual</title>
10 <releaseinfo>
11 for libnm &version;
12 The latest version of this documentation can be found on-line at
13 <ulink url="https://networkmanager.dev/docs/libnm/latest/">https://networkmanager.dev/docs/libnm/latest/</ulink>.
14 </releaseinfo>
15
16 <copyright>
17 <year>2012</year>
18 <year>2013</year>
19 <year>2014</year>
20 <year>2015</year>
21 <year>2016</year>
22 <year>2017</year>
23 <year>2018</year>
24 <holder>The NetworkManager Authors</holder>
25 </copyright>
26
27 <legalnotice>
28 <para>
29 Permission is granted to copy, distribute and/or modify this
30 document under the terms of the <citetitle>GNU Free
31 Documentation License</citetitle>, Version 1.1 or any later
32 version published by the Free Software Foundation with no
33 Invariant Sections, no Front-Cover Texts, and no Back-Cover
34 Texts. You may obtain a copy of the <citetitle>GNU Free
35 Documentation License</citetitle> from the Free Software
36 Foundation by visiting <ulink type="http"
37 url="http://www.fsf.org">their Web site</ulink>.
38 </para>
39 </legalnotice>
40 </bookinfo>
41
42 <chapter id="ref-overview">
43 <title>Overview</title>
44 <section id="intro">
45 <title>Introduction to libnm</title>
46 <para>
47 libnm is a client library for NetworkManager, the standard Linux network
48 management service. NetworkManager supports a wide variety of network
49 configuration scenarios, hardware devices and protocol families. Most of
50 the functionality is exposed on a
51 <ulink url="https://networkmanager.dev/docs/api/latest/spec.html">D-Bus API</ulink>,
52 allowing other tools to use the functionality provided by NetworkManager.
53 </para>
54 <para>
55 libnm provides C language bindings for functionality provided by
56 NetworkManager, optionally useful from other language runtimes as well.
57 </para>
58 <para>
59 libnm maps fairly closely to the actual D-Bus API that NetworkManager
60 provides, wrapping the remote D-Bus objects as native GObjects,
61 mapping D-Bus signals and properties to GObject signals and properties,
62 and providing helpful accessor and utility functions. However, unlike
63 the old libnm-util/libnm-glib API, the mapping to the D-Bus API is not
64 exact, and various inconveniences and historical anomalies of the D-Bus
65 API are papered over.
66 </para>
67 <para>
68 The following is a rough overview of the libnm object structure and
69 how to use the various parts of it:
70 <mediaobject id="libnm-overview">
71 <imageobject>
72 <imagedata fileref="libnm.png" format="PNG"/>
73 </imageobject>
74 </mediaobject>
75 </para>
76 </section>
77
78 <section id="usage">
79 <title>Using libnm</title>
80 <simplesect>
81 <title>When to use libnm</title>
82 <para>
83 libnm is fairly simple to use from C. It's based on glib and GObject.
84 If your project uses these already you'll find integration libnm with your
85 project rather convenient. In fact, the <command>nmcli</command> tool shipped
86 with NetworkManager is based on libnm.
87 </para>
88 <para>
89 libnm should be also the way to go if your project does something non-trivial
90 with NetworkManager, such as manipulating the connection profiles.
91 That is, if you're writing a specialized networking control tool or a desktop
92 environment, libnm is probably the right choice. The popular desktop
93 environments in fact all use libnm directly or with nm-applet and
94 nm-connection-editor that are all based on libnm.
95 </para>
96 <para>
97 An alternative to use of libnm is the use of the
98 <ulink url="https://networkmanager.dev/docs/api/latest/spec.html">D-Bus API</ulink>,
99 directly. This gives you larger flexibility and reduces the overhead of linking
100 with the libnm library. This makes sense if your task is simple and you have a good
101 D-Bus library at your disposal. Activating a particular connection profile
102 from a Python script is a good example of a task that is perfectly simple
103 without using libnm.
104 </para>
105 </simplesect>
106
107 <simplesect>
108 <title>How to use libnm</title>
109 <para>
110 You can use the libnm's C API directly. To do so, all libnm programs need to
111 include <filename>NetworkManager.h</filename> that provides necessary definitions.
112 The rest of the API is documented in the reference manual.
113 </para>
114 <informalexample><programlisting><![CDATA[#include <glib.h>
115 #include <NetworkManager.h>
116
117 int
118 main (int argc, char *argv[])
119 {
120 NMClient *client;
121
122 client = nm_client_new (NULL, NULL);
123 if (client)
124 g_print ("NetworkManager version: %s\n", nm_client_get_version (client));
125 }]]></programlisting></informalexample>
126 <para>
127 Use <command>pkg-config</command> for <varname>libnm</varname> to discover the necessary
128 compiler flags.
129 </para>
130 <screen><prompt>$ </prompt><userinput>cc $(pkg-config --libs --cflags libnm) -o hello-nm hello-nm.c</userinput>
131 <prompt>$ </prompt><userinput>./hello-nm</userinput>
132 NetworkManager version: &version;
133 <prompt>$ </prompt></screen>
134 <para>
135 Utilize the <varname>PKG_CHECK_MODULES</varname> macro to integrate with an
136 autoconf-based build system. It's also recommended to use
137 <varname>NM_VERSION_MIN_REQUIRED</varname> and <varname>NM_VERSION_MAX_ALLOWED</varname>
138 macros to tell libnm headers which API version does your application need to work with.
139 If you use them, the compiler will warn you when you use functionality that is not
140 available in the versions you specified.
141 </para>
142 <informalexample><programlisting><![CDATA[PKG_CHECK_MODULES(LIBNM, libnm >= 1.8)
143 LIBNM_CFLAGS="$LIBNM_CFLAGS -DNM_VERSION_MIN_REQUIRED=NM_VERSION_1_8"
144 LIBNM_CFLAGS="$LIBNM_CFLAGS -DNM_VERSION_MAX_ALLOWED=NM_VERSION_1_8"]]></programlisting></informalexample>
145 <para>
146 You can use libnm from other languages than C with the use of GObject introspection.
147 This includes Perl, Python, Javascript, Lua, Ruby and more. The example below shows what the
148 typical libnm use in Python would look like.
149 </para>
150 <informalexample><programlisting><![CDATA[import gi
151 gi.require_version('NM', '1.0')
152 from gi.repository import NM
153
154 client = NM.Client.new(None)
155 print ("NetworkManager version " + client.get_version())]]></programlisting></informalexample>
156 <para>
157 There's <ulink url="https://lazka.github.io/pgi-docs/#NM-1.0">NM-1.0 Python API Reference</ulink>
158 maintained a third party that is generated from the introspection metadata.
159 </para>
160 <para>
161 In general, the C API documentation applies to the use GObject introspection
162 from other languages, with the calling convention respecting the language's
163 customs. Consult the source tree for
164 <ulink url="https://gitlab.freedesktop.org/NetworkManager/NetworkManager/tree/main/examples">some examples</ulink>.
165 </para>
166 </simplesect>
167
168 <simplesect id="sync-api">
169 <title>Synchronous API in libnm</title>
170 <para>
171 Libnm contains some synchronous API. This API basically makes a blocking
172 D-Bus call (g_dbus_connection_call_sync()) and is now deprecated.
173 </para>
174 <para>
175 Note that D-Bus is fundamentally asynchronous. Doing blocking calls
176 on top of D-Bus is odd, especially for libnm's NMClient. That is because
177 NMClient essentially is a client-side cache of the objects of the D-Bus
178 interface. This cache should be filled exclusively by (asynchronous) D-Bus
179 events. So, making a blocking D-Bus call means to wait for a response and
180 return it, while queuing everything that happens in between. Basically,
181 there are three options how a synchronous API on NMClient could behave:
182 <orderedlist>
183 <listitem>
184 <para>
185 The call basically calls g_dbus_connection_call_sync(). This means
186 that libnm sends a D-Bus request via GDBusConnection, and blockingly
187 waits for the response. All D-Bus messages that get received in the
188 meantime are queued in the GMainContext that belongs to NMClient.
189 That means, none of these D-Bus events are processed until we
190 iterate the GMainContext after the call returns. The effect is,
191 that NMClient (and all cached objects in there) are unaffected by
192 the D-Bus request.
193 Most of the synchronous API calls in libnm are of this kind.
194 The problem is that the strict ordering of D-Bus events gets
195 violated.
196 For some API this is not an immediate problem. Take for example
197 nm_device_wifi_request_scan(). The call merely blockingly tells
198 NetworkManager to start scanning, but since NetworkManager's D-Bus
199 API does not directly expose any state that tells whether we are
200 currently scanning, this out of order processing of the D-Bus
201 request is a small issue.
202 The problem is more obvious for nm_client_networking_set_enabled().
203 After calling it, NM_CLIENT_NETWORKING_ENABLED is still unaffected
204 and unchanged, because the PropertiesChanged signal from D-Bus
205 is not yet processed.
206 This means, while you make such a blocking call, NMClient's state
207 does not change. But usually you perform the synchronous call
208 to change some state. In this form, the blocking call is not useful,
209 because NMClient only changes the state after iterating the GMainContext,
210 and not after the blocking call returns.
211 </para>
212 </listitem>
213 <listitem>
214 <para>
215 Like 1), but after making the blocking g_dbus_connection_call_sync(),
216 update the NMClient cache artificially. This is what
217 nm_manager_check_connectivity() does, to "fix" bgo#784629.
218 This also has the problem of out-of-order events, but it kinda
219 solves the problem of not changing the state during the blocking
220 call. But it does so by hacking the state of the cache. I think
221 this is really wrong because the state should only be updated from
222 the ordered stream of D-Bus messages. When libnm decides to modify
223 the state, there are already D-Bus messages queued that affect this
224 very state.
225 </para>
226 </listitem>
227 <listitem>
228 <para>
229 Instead of calling g_dbus_connection_call_sync(), use the
230 asynchronous g_dbus_connection_call(). If we would use a separate
231 GMainContext for all D-Bus related calls, we could ensure that
232 while we block for the response, we iterate the internal main context.
233 This might be nice, because all events are processed in order and
234 after the blocking call returns, the NMClient state is up to date.
235 The are problems however: current blocking API does not do this,
236 so it's a significant change in behavior. Also, it might be
237 unexpected to the user that during the blocking call the entire
238 content of NMClient's cache might change and all pointers to the
239 cache might be invalidated. Also, of course NMClient would invoke
240 signals for all the changes that happen.
241 Another problem is that this would be more effort to implement
242 and it involves a small performance overhead for all D-Bus related
243 calls (because we have to serialize all events in an internal
244 GMainContext first and then invoke them on the caller's context).
245 Also, if the users wants this, they could implement it themself
246 using their own extra GMainContext and the asynchronous API.
247 </para>
248 </listitem>
249 </orderedlist>
250
251 See also <ulink url="https://smcv.pseudorandom.co.uk/2008/11/nonblocking/">this blog</ulink>
252 for why blocking calls are wrong.
253 </para>
254 <para>
255 All possible behaviors for synchronous API have severe behavioural
256 issues and thus such API is deprecated. Note that "deprecated" here does not
257 mean that the API is going to be removed. Libnm does not break API. The
258 user may:
259
260 <itemizedlist>
261 <listitem>
262 <para>
263 Continue to use this API. It's deprecated, awkward and discouraged,
264 but if it works for you, that's fine.
265 </para>
266 </listitem>
267 <listitem>
268 <para>
269 Use asynchronous API. That's the only sensible way to use D-Bus.
270 If libnm lacks a certain asynchronous counterpart, it should be
271 added.
272 </para>
273 </listitem>
274 <listitem>
275 <para>
276 Use GDBusConnection directly. There really isn't anything wrong
277 with D-Bus or GDBusConnection. This deprecated API is just a wrapper
278 around g_dbus_connection_call_sync(). You may call it directly
279 without feeling dirty.
280 </para>
281 </listitem>
282 </itemizedlist>
283 </para>
284 </simplesect>
285
286 </section>
287 </chapter>
288
289 <chapter>
290 <title>Client Object API Reference</title>
291 <xi:include href="xml/nm-client.xml"/>
292 <xi:include href="xml/nm-secret-agent-old.xml"/>
293 <xi:include href="xml/nm-object.xml"/>
294 <xi:include href="xml/nm-errors.xml"/>
295 <xi:include href="xml/nm-dbus-interface.xml"/>
296 <xi:include href="xml/nm-vpn-dbus-interface.xml"/>
297 </chapter>
298
299 <chapter>
300 <title>Connection and Setting API Reference</title>
301 <xi:include href="xml/nm-connection.xml"/>
302 <xi:include href="xml/nm-simple-connection.xml"/>
303 <xi:include href="xml/nm-remote-connection.xml"/>
304 <xi:include href="xml/nm-setting.xml"/>
305 <xi:include href="xml/nm-setting-connection.xml"/>
306 <!-- begin alphabetical -->
307 <xi:include href="xml/nm-setting-6lowpan.xml"/>
308 <xi:include href="xml/nm-setting-8021x.xml"/>
309 <xi:include href="xml/nm-setting-adsl.xml"/>
310 <xi:include href="xml/nm-setting-bluetooth.xml"/>
311 <xi:include href="xml/nm-setting-bond-port.xml"/>
312 <xi:include href="xml/nm-setting-bond.xml"/>
313 <xi:include href="xml/nm-setting-bridge-port.xml"/>
314 <xi:include href="xml/nm-setting-bridge.xml"/>
315 <xi:include href="xml/nm-setting-cdma.xml"/>
316 <xi:include href="xml/nm-setting-dcb.xml"/>
317 <xi:include href="xml/nm-setting-dummy.xml"/>
318 <xi:include href="xml/nm-setting-ethtool.xml"/>
319 <xi:include href="xml/nm-setting-generic.xml"/>
320 <xi:include href="xml/nm-setting-geneve.xml"/>
321 <xi:include href="xml/nm-setting-gsm.xml"/>
322 <xi:include href="xml/nm-setting-hostname.xml"/>
323 <xi:include href="xml/nm-setting-hsr.xml"/>
324 <xi:include href="xml/nm-setting-infiniband.xml"/>
325 <xi:include href="xml/nm-setting-ip-config.xml"/>
326 <xi:include href="xml/nm-setting-ip-tunnel.xml"/>
327 <xi:include href="xml/nm-setting-ip4-config.xml"/>
328 <xi:include href="xml/nm-setting-ip6-config.xml"/>
329 <xi:include href="xml/nm-setting-ipvlan.xml"/>
330 <xi:include href="xml/nm-setting-link.xml"/>
331 <xi:include href="xml/nm-setting-loopback.xml"/>
332 <xi:include href="xml/nm-setting-macsec.xml"/>
333 <xi:include href="xml/nm-setting-macvlan.xml"/>
334 <xi:include href="xml/nm-setting-match.xml"/>
335 <xi:include href="xml/nm-setting-olpc-mesh.xml"/>
336 <xi:include href="xml/nm-setting-ovs-bridge.xml"/>
337 <xi:include href="xml/nm-setting-ovs-dpdk.xml"/>
338 <xi:include href="xml/nm-setting-ovs-external-ids.xml"/>
339 <xi:include href="xml/nm-setting-ovs-interface.xml"/>
340 <xi:include href="xml/nm-setting-ovs-other-config.xml"/>
341 <xi:include href="xml/nm-setting-ovs-patch.xml"/>
342 <xi:include href="xml/nm-setting-ovs-port.xml"/>
343 <xi:include href="xml/nm-setting-ppp.xml"/>
344 <xi:include href="xml/nm-setting-pppoe.xml"/>
345 <xi:include href="xml/nm-setting-prefix-delegation.xml"/>
346 <xi:include href="xml/nm-setting-proxy.xml"/>
347 <xi:include href="xml/nm-setting-serial.xml"/>
348 <xi:include href="xml/nm-setting-sriov.xml"/>
349 <xi:include href="xml/nm-setting-tc-config.xml"/>
350 <xi:include href="xml/nm-setting-team-port.xml"/>
351 <xi:include href="xml/nm-setting-team.xml"/>
352 <xi:include href="xml/nm-setting-tun.xml"/>
353 <xi:include href="xml/nm-setting-user.xml"/>
354 <xi:include href="xml/nm-setting-veth.xml"/>
355 <xi:include href="xml/nm-setting-vlan.xml"/>
356 <xi:include href="xml/nm-setting-vpn.xml"/>
357 <xi:include href="xml/nm-setting-vrf.xml"/>
358 <xi:include href="xml/nm-setting-vxlan.xml"/>
359 <xi:include href="xml/nm-setting-wifi-p2p.xml"/>
360 <xi:include href="xml/nm-setting-wimax.xml"/>
361 <xi:include href="xml/nm-setting-wired.xml"/>
362 <xi:include href="xml/nm-setting-wireguard.xml"/>
363 <xi:include href="xml/nm-setting-wireless-security.xml"/>
364 <xi:include href="xml/nm-setting-wireless.xml"/>
365 <xi:include href="xml/nm-setting-wpan.xml"/>
366 <!-- end alphabetical -->
367 </chapter>
368
369 <chapter>
370 <title>Device and Runtime Configuration API Reference</title>
371 <xi:include href="xml/nm-device.xml"/>
372 <!-- begin alphabetical -->
373 <xi:include href="xml/nm-device-6lowpan.xml"/>
374 <xi:include href="xml/nm-device-adsl.xml"/>
375 <xi:include href="xml/nm-device-bond.xml"/>
376 <xi:include href="xml/nm-device-bridge.xml"/>
377 <xi:include href="xml/nm-device-bt.xml"/>
378 <xi:include href="xml/nm-device-dummy.xml"/>
379 <xi:include href="xml/nm-device-ethernet.xml"/>
380 <xi:include href="xml/nm-device-generic.xml"/>
381 <xi:include href="xml/nm-device-geneve.xml"/>
382 <xi:include href="xml/nm-device-hsr.xml"/>
383 <xi:include href="xml/nm-device-infiniband.xml"/>
384 <xi:include href="xml/nm-device-ip-tunnel.xml"/>
385 <xi:include href="xml/nm-device-ipvlan.xml"/>
386 <xi:include href="xml/nm-device-loopback.xml"/>
387 <xi:include href="xml/nm-device-macsec.xml"/>
388 <xi:include href="xml/nm-device-macvlan.xml"/>
389 <xi:include href="xml/nm-device-modem.xml"/>
390 <xi:include href="xml/nm-device-olpc-mesh.xml"/>
391 <xi:include href="xml/nm-device-ovs-bridge.xml"/>
392 <xi:include href="xml/nm-device-ovs-interface.xml"/>
393 <xi:include href="xml/nm-device-ovs-port.xml"/>
394 <xi:include href="xml/nm-device-ppp.xml"/>
395 <xi:include href="xml/nm-device-team.xml"/>
396 <xi:include href="xml/nm-device-tun.xml"/>
397 <xi:include href="xml/nm-device-veth.xml"/>
398 <xi:include href="xml/nm-device-vlan.xml"/>
399 <xi:include href="xml/nm-device-vrf.xml"/>
400 <xi:include href="xml/nm-device-vxlan.xml"/>
401 <xi:include href="xml/nm-device-wifi-p2p.xml"/>
402 <xi:include href="xml/nm-device-wifi.xml"/>
403 <xi:include href="xml/nm-device-wimax.xml"/>
404 <xi:include href="xml/nm-device-wireguard.xml"/>
405 <xi:include href="xml/nm-device-wpan.xml"/>
406 <!-- end alphabetical -->
407 <xi:include href="xml/nm-active-connection.xml"/>
408 <xi:include href="xml/nm-vpn-connection.xml"/>
409 <xi:include href="xml/nm-access-point.xml"/>
410 <xi:include href="xml/nm-wifi-p2p-peer.xml"/>
411 <xi:include href="xml/nm-wimax-nsp.xml"/>
412 <xi:include href="xml/nm-ip-config.xml"/>
413 <xi:include href="xml/nm-dhcp-config.xml"/>
414 <xi:include href="xml/nm-checkpoint.xml"/>
415 </chapter>
416
417 <chapter>
418 <title>Utility API Reference</title>
419 <xi:include href="xml/nm-keyfile.xml"/>
420 <xi:include href="xml/nm-utils.xml"/>
421 <xi:include href="xml/nm-conn-utils.xml"/>
422 <xi:include href="xml/nm-ethtool-utils.xml"/>
423 <xi:include href="xml/nm-version.xml"/>
424 <xi:include href="xml/nm-version-macros.xml"/>
425 </chapter>
426
427 <chapter>
428 <title>VPN Plugin API Reference</title>
429 <xi:include href="xml/nm-vpn-service-plugin.xml"/>
430 <xi:include href="xml/nm-vpn-plugin-info.xml"/>
431 <xi:include href="xml/nm-vpn-editor.xml"/>
432 <xi:include href="xml/nm-vpn-editor-plugin.xml"/>
433 <xi:include href="xml/nm-vpn-plugin-old.xml"/>
434 </chapter>
435
436 <chapter id="object-tree">
437 <title>Object Hierarchy</title>
438 <xi:include href="xml/tree_index.sgml"/>
439 </chapter>
440 <index id="api-index-full">
441 <title>API Index</title>
442 <xi:include href="xml/api-index-full.xml"><xi:fallback /></xi:include>
443 </index>
444
445 <xi:include href="xml/annotation-glossary.xml"><xi:fallback /></xi:include>
446
447 <appendix id="license">
448 <title>License</title>
449
450 <para>
451 This library is free software; you can redistribute
452 it and/or modify it under the terms of the <citetitle>GNU
453 Lesser General Public License</citetitle> as published by
454 the Free Software Foundation; either version 2 of the
455 License, or (at your option) any later version.
456 </para>
457
458 <para>
459 This library is distributed in the hope that it will
460 be useful, but WITHOUT ANY WARRANTY; without even the
461 implied warranty of MERCHANTABILITY or FITNESS FOR A
462 PARTICULAR PURPOSE. See the GNU Lesser General Public
463 License for more details.
464 </para>
465
466 <para>
467 You should have received a copy of the <citetitle>GNU
468 Lesser General Public License</citetitle> along with this
469 library.
470 </para>
471
472 <para>
473 A copy of the <citetitle>GNU Lesser General Public License</citetitle>
474 can also be obtained from the <ulink url="https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html">
475 GNU web site</ulink>.
476 </para>
477 </appendix>
478
479 </book>