xref: /dpdk/doc/guides/contributing/abi_versioning.rst (revision f43d3dbbd90c9e195d26d18ac7da9ca2854c3f1e)
1..  SPDX-License-Identifier: BSD-3-Clause
2    Copyright 2018 The DPDK contributors
3
4.. _abi_versioning:
5
6ABI Versioning
7==============
8
9This document details the mechanics of ABI version management in DPDK.
10
11.. _what_is_soname:
12
13What is a library's soname?
14---------------------------
15
16System libraries usually adopt the familiar major and minor version naming
17convention, where major versions (e.g. ``librte_eal 20.x, 21.x``) are presumed
18to be ABI incompatible with each other and minor versions (e.g. ``librte_eal
1920.1, 20.2``) are presumed to be ABI compatible. A library's `soname
20<https://en.wikipedia.org/wiki/Soname>`_. is typically used to provide backward
21compatibility information about a given library, describing the lowest common
22denominator ABI supported by the library. The soname or logical name for the
23library, is typically comprised of the library's name and major version e.g.
24``librte_eal.so.20``.
25
26During an application's build process, a library's soname is noted as a runtime
27dependency of the application. This information is then used by the `dynamic
28linker <https://en.wikipedia.org/wiki/Dynamic_linker>`_ when resolving the
29applications dependencies at runtime, to load a library supporting the correct
30ABI version. The library loaded at runtime therefore, may be a minor revision
31supporting the same major ABI version (e.g. ``librte_eal.20.2``), as the library
32used to link the application (e.g ``librte_eal.20.0``).
33
34.. _major_abi_versions:
35
36Major ABI versions
37------------------
38
39An ABI version change to a given library, especially in core libraries such as
40``librte_mbuf``, may cause an implicit ripple effect on the ABI of it's
41consuming libraries, causing ABI breakages. There may however be no explicit
42reason to bump a dependent library's ABI version, as there may have been no
43obvious change to the dependent library's API, even though the library's ABI
44compatibility will have been broken.
45
46This interdependence of DPDK libraries, means that ABI versioning of libraries
47is more manageable at a project level, with all project libraries sharing a
48**single ABI version**. In addition, the need to maintain a stable ABI for some
49number of releases as described in the section :doc:`abi_policy`, means
50that ABI version increments need to carefully planned and managed at a project
51level.
52
53Major ABI versions are therefore declared typically aligned with an LTS release
54and is then supported some number of subsequent releases, shared across all
55libraries. This means that a single project level ABI version, reflected in all
56individual library's soname, library filenames and associated version maps
57persists over multiple releases.
58
59.. code-block:: none
60
61 $ head ./lib/librte_acl/rte_acl_version.map
62 DPDK_20 {
63        global:
64 ...
65
66 $ head ./lib/librte_eal/rte_eal_version.map
67 DPDK_20 {
68        global:
69 ...
70
71When an ABI change is made between major ABI versions to a given library, a new
72section is added to that library's version map describing the impending new ABI
73version, as described in the section :ref:`example_abi_macro_usage`. The
74library's soname and filename however do not change, e.g. ``libacl.so.20``, as
75ABI compatibility with the last major ABI version continues to be preserved for
76that library.
77
78.. code-block:: none
79
80 $ head ./lib/librte_acl/rte_acl_version.map
81 DPDK_20 {
82        global:
83 ...
84
85 DPDK_21 {
86        global:
87
88 } DPDK_20;
89 ...
90
91 $ head ./lib/librte_eal/rte_eal_version.map
92 DPDK_20 {
93        global:
94 ...
95
96However when a new ABI version is declared, for example DPDK ``21``, old
97depreciated functions may be safely removed at this point and the entire old
98major ABI version removed, see the section :ref:`deprecating_entire_abi` on
99how this may be done.
100
101.. code-block:: none
102
103 $ head ./lib/librte_acl/rte_acl_version.map
104 DPDK_21 {
105        global:
106 ...
107
108 $ head ./lib/librte_eal/rte_eal_version.map
109 DPDK_21 {
110        global:
111 ...
112
113At the same time, the major ABI version is changed atomically across all
114libraries by incrementing the major version in individual library's soname, e.g.
115``libacl.so.21``. This is done by bumping the LIBABIVER number in the libraries
116Makefile to indicate to dynamic linking applications that this is a later, and
117possibly incompatible library version:
118
119.. code-block:: c
120
121   -LIBABIVER := 20
122   +LIBABIVER := 21
123
124
125Versioning Macros
126-----------------
127
128When a symbol is exported from a library to provide an API, it also provides a
129calling convention (ABI) that is embodied in its name, return type and
130arguments. Occasionally that function may need to change to accommodate new
131functionality or behavior. When that occurs, it is may be required to allow for
132backward compatibility for a time with older binaries that are dynamically
133linked to the DPDK.
134
135To support backward compatibility the ``rte_function_versioning.h``
136header file provides macros to use when updating exported functions. These
137macros are used in conjunction with the ``rte_<library>_version.map`` file for
138a given library to allow multiple versions of a symbol to exist in a shared
139library so that older binaries need not be immediately recompiled.
140
141The macros exported are:
142
143* ``VERSION_SYMBOL(b, e, n)``: Creates a symbol version table entry binding
144  versioned symbol ``b@DPDK_n`` to the internal function ``be``.
145
146* ``BIND_DEFAULT_SYMBOL(b, e, n)``: Creates a symbol version entry instructing
147  the linker to bind references to symbol ``b`` to the internal symbol
148  ``be``.
149
150* ``MAP_STATIC_SYMBOL(f, p)``: Declare the prototype ``f``, and map it to the
151  fully qualified function ``p``, so that if a symbol becomes versioned, it
152  can still be mapped back to the public symbol name.
153
154* ``__vsym``:  Annotation to be used in a declaration of the internal symbol
155  ``be`` to signal that it is being used as an implementation of a particular
156  version of symbol ``b``.
157
158.. _example_abi_macro_usage:
159
160Examples of ABI Macro use
161~~~~~~~~~~~~~~~~~~~~~~~~~
162
163Updating a public API
164_____________________
165
166Assume we have a function as follows
167
168.. code-block:: c
169
170 /*
171  * Create an acl context object for apps to
172  * manipulate
173  */
174 struct rte_acl_ctx *
175 rte_acl_create(const struct rte_acl_param *param)
176 {
177        ...
178 }
179
180
181Assume that struct rte_acl_ctx is a private structure, and that a developer
182wishes to enhance the acl api so that a debugging flag can be enabled on a
183per-context basis.  This requires an addition to the structure (which, being
184private, is safe), but it also requires modifying the code as follows
185
186.. code-block:: c
187
188 /*
189  * Create an acl context object for apps to
190  * manipulate
191  */
192 struct rte_acl_ctx *
193 rte_acl_create(const struct rte_acl_param *param, int debug)
194 {
195        ...
196 }
197
198
199Note also that, being a public function, the header file prototype must also be
200changed, as must all the call sites, to reflect the new ABI footprint.  We will
201maintain previous ABI versions that are accessible only to previously compiled
202binaries
203
204The addition of a parameter to the function is ABI breaking as the function is
205public, and existing application may use it in its current form. However, the
206compatibility macros in DPDK allow a developer to use symbol versioning so that
207multiple functions can be mapped to the same public symbol based on when an
208application was linked to it. To see how this is done, we start with the
209requisite libraries version map file. Initially the version map file for the acl
210library looks like this
211
212.. code-block:: none
213
214   DPDK_20 {
215        global:
216
217        rte_acl_add_rules;
218        rte_acl_build;
219        rte_acl_classify;
220        rte_acl_classify_alg;
221        rte_acl_classify_scalar;
222        rte_acl_create;
223        rte_acl_dump;
224        rte_acl_find_existing;
225        rte_acl_free;
226        rte_acl_ipv4vlan_add_rules;
227        rte_acl_ipv4vlan_build;
228        rte_acl_list_dump;
229        rte_acl_reset;
230        rte_acl_reset_rules;
231        rte_acl_set_ctx_classify;
232
233        local: *;
234   };
235
236This file needs to be modified as follows
237
238.. code-block:: none
239
240   DPDK_20 {
241        global:
242
243        rte_acl_add_rules;
244        rte_acl_build;
245        rte_acl_classify;
246        rte_acl_classify_alg;
247        rte_acl_classify_scalar;
248        rte_acl_create;
249        rte_acl_dump;
250        rte_acl_find_existing;
251        rte_acl_free;
252        rte_acl_ipv4vlan_add_rules;
253        rte_acl_ipv4vlan_build;
254        rte_acl_list_dump;
255        rte_acl_reset;
256        rte_acl_reset_rules;
257        rte_acl_set_ctx_classify;
258
259        local: *;
260   };
261
262   DPDK_21 {
263        global:
264        rte_acl_create;
265
266   } DPDK_20;
267
268The addition of the new block tells the linker that a new version node is
269available (DPDK_21), which contains the symbol rte_acl_create, and inherits
270the symbols from the DPDK_20 node. This list is directly translated into a
271list of exported symbols when DPDK is compiled as a shared library
272
273Next, we need to specify in the code which function map to the rte_acl_create
274symbol at which versions.  First, at the site of the initial symbol definition,
275we need to update the function so that it is uniquely named, and not in conflict
276with the public symbol name
277
278.. code-block:: c
279
280 -struct rte_acl_ctx *
281 -rte_acl_create(const struct rte_acl_param *param)
282 +struct rte_acl_ctx * __vsym
283 +rte_acl_create_v20(const struct rte_acl_param *param)
284 {
285        size_t sz;
286        struct rte_acl_ctx *ctx;
287        ...
288
289Note that the base name of the symbol was kept intact, as this is conducive to
290the macros used for versioning symbols and we have annotated the function as an
291implementation of versioned symbol.  That is our next step, mapping this new
292symbol name to the initial symbol name at version node 20.  Immediately after
293the function, we add this line of code
294
295.. code-block:: c
296
297   VERSION_SYMBOL(rte_acl_create, _v20, 20);
298
299Remembering to also add the rte_function_versioning.h header to the requisite c
300file where these changes are being made. The above macro instructs the linker to
301create a new symbol ``rte_acl_create@DPDK_20``, which matches the symbol created
302in older builds, but now points to the above newly named function. We have now
303mapped the original rte_acl_create symbol to the original function (but with a
304new name).
305
306Next, we need to create the 21 version of the symbol. We create a new function
307name, with a different suffix, and implement it appropriately
308
309.. code-block:: c
310
311   struct rte_acl_ctx * __vsym
312   rte_acl_create_v21(const struct rte_acl_param *param, int debug);
313   {
314        struct rte_acl_ctx *ctx = rte_acl_create_v20(param);
315
316        ctx->debug = debug;
317
318        return ctx;
319   }
320
321This code serves as our new API call. Its the same as our old call, but adds the
322new parameter in place. Next we need to map this function to the symbol
323``rte_acl_create@DPDK_21``. To do this, we modify the public prototype of the
324call in the header file, adding the macro there to inform all including
325applications, that on re-link, the default rte_acl_create symbol should point to
326this function. Note that we could do this by simply naming the function above
327rte_acl_create, and the linker would chose the most recent version tag to apply
328in the version script, but we can also do this in the header file
329
330.. code-block:: c
331
332   struct rte_acl_ctx *
333   -rte_acl_create(const struct rte_acl_param *param);
334   +rte_acl_create_v21(const struct rte_acl_param *param, int debug);
335   +BIND_DEFAULT_SYMBOL(rte_acl_create, _v21, 21);
336
337The BIND_DEFAULT_SYMBOL macro explicitly tells applications that include this
338header, to link to the rte_acl_create_v21 function and apply the DPDK_21
339version node to it.  This method is more explicit and flexible than just
340re-implementing the exact symbol name, and allows for other features (such as
341linking to the old symbol version by default, when the new ABI is to be opt-in
342for a period.
343
344One last thing we need to do.  Note that we've taken what was a public symbol,
345and duplicated it into two uniquely and differently named symbols.  We've then
346mapped each of those back to the public symbol ``rte_acl_create`` with different
347version tags.  This only applies to dynamic linking, as static linking has no
348notion of versioning.  That leaves this code in a position of no longer having a
349symbol simply named ``rte_acl_create`` and a static build will fail on that
350missing symbol.
351
352To correct this, we can simply map a function of our choosing back to the public
353symbol in the static build with the ``MAP_STATIC_SYMBOL`` macro.  Generally the
354assumption is that the most recent version of the symbol is the one you want to
355map.  So, back in the C file where, immediately after ``rte_acl_create_v21`` is
356defined, we add this
357
358
359.. code-block:: c
360
361   struct rte_acl_ctx * __vsym
362   rte_acl_create_v21(const struct rte_acl_param *param, int debug)
363   {
364        ...
365   }
366   MAP_STATIC_SYMBOL(struct rte_acl_ctx *rte_acl_create(const struct rte_acl_param *param, int debug), rte_acl_create_v21);
367
368That tells the compiler that, when building a static library, any calls to the
369symbol ``rte_acl_create`` should be linked to ``rte_acl_create_v21``
370
371That's it, on the next shared library rebuild, there will be two versions of
372rte_acl_create, an old DPDK_20 version, used by previously built applications,
373and a new DPDK_21 version, used by future built applications.
374
375
376Deprecating part of a public API
377________________________________
378
379Lets assume that you've done the above update, and in preparation for the next
380major ABI version you decide you would like to retire the old version of the
381function. After having gone through the ABI deprecation announcement process,
382removal is easy. Start by removing the symbol from the requisite version map
383file:
384
385.. code-block:: none
386
387   DPDK_20 {
388        global:
389
390        rte_acl_add_rules;
391        rte_acl_build;
392        rte_acl_classify;
393        rte_acl_classify_alg;
394        rte_acl_classify_scalar;
395        rte_acl_dump;
396 -      rte_acl_create
397        rte_acl_find_existing;
398        rte_acl_free;
399        rte_acl_ipv4vlan_add_rules;
400        rte_acl_ipv4vlan_build;
401        rte_acl_list_dump;
402        rte_acl_reset;
403        rte_acl_reset_rules;
404        rte_acl_set_ctx_classify;
405
406        local: *;
407   };
408
409   DPDK_21 {
410        global:
411        rte_acl_create;
412   } DPDK_20;
413
414
415Next remove the corresponding versioned export.
416
417.. code-block:: c
418
419 -VERSION_SYMBOL(rte_acl_create, _v20, 20);
420
421
422Note that the internal function definition could also be removed, but its used
423in our example by the newer version v21, so we leave it in place and declare it
424as static. This is a coding style choice.
425
426.. _deprecating_entire_abi:
427
428Deprecating an entire ABI version
429_________________________________
430
431While removing a symbol from an ABI may be useful, it is more practical to
432remove an entire version node at once, as is typically done at the declaration
433of a major ABI version. If a version node completely specifies an API, then
434removing part of it, typically makes it incomplete. In those cases it is better
435to remove the entire node.
436
437To do this, start by modifying the version map file, such that all symbols from
438the node to be removed are merged into the next node in the map.
439
440In the case of our map above, it would transform to look as follows
441
442.. code-block:: none
443
444   DPDK_21 {
445        global:
446
447        rte_acl_add_rules;
448        rte_acl_build;
449        rte_acl_classify;
450        rte_acl_classify_alg;
451        rte_acl_classify_scalar;
452        rte_acl_dump;
453        rte_acl_create
454        rte_acl_find_existing;
455        rte_acl_free;
456        rte_acl_ipv4vlan_add_rules;
457        rte_acl_ipv4vlan_build;
458        rte_acl_list_dump;
459        rte_acl_reset;
460        rte_acl_reset_rules;
461        rte_acl_set_ctx_classify;
462
463        local: *;
464 };
465
466Then any uses of BIND_DEFAULT_SYMBOL that pointed to the old node should be
467updated to point to the new version node in any header files for all affected
468symbols.
469
470.. code-block:: c
471
472 -BIND_DEFAULT_SYMBOL(rte_acl_create, _v20, 20);
473 +BIND_DEFAULT_SYMBOL(rte_acl_create, _v21, 21);
474
475Lastly, any VERSION_SYMBOL macros that point to the old version node should be
476removed, taking care to keep, where need old code in place to support newer
477versions of the symbol.
478
479
480Running the ABI Validator
481-------------------------
482
483The ``devtools`` directory in the DPDK source tree contains a utility program,
484``validate-abi.sh``, for validating the DPDK ABI based on the Linux `ABI
485Compliance Checker
486<http://ispras.linuxbase.org/index.php/ABI_compliance_checker>`_.
487
488This has a dependency on the ``abi-compliance-checker`` and ``and abi-dumper``
489utilities which can be installed via a package manager. For example::
490
491   sudo yum install abi-compliance-checker
492   sudo yum install abi-dumper
493
494The syntax of the ``validate-abi.sh`` utility is::
495
496   ./devtools/validate-abi.sh <REV1> <REV2>
497
498Where ``REV1`` and ``REV2`` are valid gitrevisions(7)
499https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html
500on the local repo.
501
502For example::
503
504   # Check between the previous and latest commit:
505   ./devtools/validate-abi.sh HEAD~1 HEAD
506
507   # Check on a specific compilation target:
508   ./devtools/validate-abi.sh -t x86_64-native-linux-gcc HEAD~1 HEAD
509
510   # Check between two tags:
511   ./devtools/validate-abi.sh v2.0.0 v2.1.0
512
513   # Check between git master and local topic-branch "vhost-hacking":
514   ./devtools/validate-abi.sh master vhost-hacking
515
516After the validation script completes (it can take a while since it need to
517compile both tags) it will create compatibility reports in the
518``./abi-check/compat_report`` directory. Listed incompatibilities can be found
519as follows::
520
521  grep -lr Incompatible abi-check/compat_reports/
522