e96fde340f397747e5f68afdbc8a78cf17243b6b
[dpdk.git] / doc / guides / contributing / abi_versioning.rst
1 ..  SPDX-License-Identifier: BSD-3-Clause
2     Copyright 2018 The DPDK contributors
3
4 .. _abi_versioning:
5
6 ABI Versioning
7 ==============
8
9 This document details the mechanics of ABI version management in DPDK.
10
11 .. _what_is_soname:
12
13 What is a library's soname?
14 ---------------------------
15
16 System libraries usually adopt the familiar major and minor version naming
17 convention, where major versions (e.g. ``librte_eal 20.x, 21.x``) are presumed
18 to be ABI incompatible with each other and minor versions (e.g. ``librte_eal
19 20.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
21 compatibility information about a given library, describing the lowest common
22 denominator ABI supported by the library. The soname or logical name for the
23 library, is typically comprised of the library's name and major version e.g.
24 ``librte_eal.so.20``.
25
26 During an application's build process, a library's soname is noted as a runtime
27 dependency of the application. This information is then used by the `dynamic
28 linker <https://en.wikipedia.org/wiki/Dynamic_linker>`_ when resolving the
29 applications dependencies at runtime, to load a library supporting the correct
30 ABI version. The library loaded at runtime therefore, may be a minor revision
31 supporting the same major ABI version (e.g. ``librte_eal.20.2``), as the library
32 used to link the application (e.g ``librte_eal.20.0``).
33
34 .. _major_abi_versions:
35
36 Major ABI versions
37 ------------------
38
39 An 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
41 consuming libraries, causing ABI breakages. There may however be no explicit
42 reason to bump a dependent library's ABI version, as there may have been no
43 obvious change to the dependent library's API, even though the library's ABI
44 compatibility will have been broken.
45
46 This interdependence of DPDK libraries, means that ABI versioning of libraries
47 is 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
49 number of releases as described in the section :doc:`abi_policy`, means
50 that ABI version increments need to carefully planned and managed at a project
51 level.
52
53 Major ABI versions are therefore declared typically aligned with an LTS release
54 and is then supported some number of subsequent releases, shared across all
55 libraries. This means that a single project level ABI version, reflected in all
56 individual library's soname, library filenames and associated version maps
57 persists 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
71 When an ABI change is made between major ABI versions to a given library, a new
72 section is added to that library's version map describing the impending new ABI
73 version, as described in the section :ref:`example_abi_macro_usage`. The
74 library's soname and filename however do not change, e.g. ``libacl.so.20``, as
75 ABI compatibility with the last major ABI version continues to be preserved for
76 that 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
96 However when a new ABI version is declared, for example DPDK ``21``, old
97 depreciated functions may be safely removed at this point and the entire old
98 major ABI version removed, see the section :ref:`deprecating_entire_abi` on
99 how 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
113 At the same time, the major ABI version is changed atomically across all
114 libraries by incrementing the major version in the ABI_VERSION file. This is
115 done globally for all libraries that declare a stable ABI. For libraries marked
116 as EXPERIMENTAL, their major ABI version is always set to 0.
117
118 Minor ABI versions
119 ~~~~~~~~~~~~~~~~~~
120
121 Each non-LTS release will also increment minor ABI version, to permit multiple
122 DPDK versions being installed alongside each other. Both stable and
123 experimental ABI's are versioned using the global version file that is updated
124 at the start of each release cycle, and are managed at the project level.
125
126 Versioning Macros
127 -----------------
128
129 When a symbol is exported from a library to provide an API, it also provides a
130 calling convention (ABI) that is embodied in its name, return type and
131 arguments. Occasionally that function may need to change to accommodate new
132 functionality or behavior. When that occurs, it is may be required to allow for
133 backward compatibility for a time with older binaries that are dynamically
134 linked to the DPDK.
135
136 To support backward compatibility the ``rte_function_versioning.h``
137 header file provides macros to use when updating exported functions. These
138 macros are used in conjunction with the ``rte_<library>_version.map`` file for
139 a given library to allow multiple versions of a symbol to exist in a shared
140 library so that older binaries need not be immediately recompiled.
141
142 The macros exported are:
143
144 * ``VERSION_SYMBOL(b, e, n)``: Creates a symbol version table entry binding
145   versioned symbol ``b@DPDK_n`` to the internal function ``be``.
146
147 * ``BIND_DEFAULT_SYMBOL(b, e, n)``: Creates a symbol version entry instructing
148   the linker to bind references to symbol ``b`` to the internal symbol
149   ``be``.
150
151 * ``MAP_STATIC_SYMBOL(f, p)``: Declare the prototype ``f``, and map it to the
152   fully qualified function ``p``, so that if a symbol becomes versioned, it
153   can still be mapped back to the public symbol name.
154
155 * ``__vsym``:  Annotation to be used in a declaration of the internal symbol
156   ``be`` to signal that it is being used as an implementation of a particular
157   version of symbol ``b``.
158
159 * ``VERSION_SYMBOL_EXPERIMENTAL(b, e)``: Creates a symbol version table entry
160   binding versioned symbol ``b@EXPERIMENTAL`` to the internal function ``be``.
161   The macro is used when a symbol matures to become part of the stable ABI, to
162   provide an alias to experimental for some time.
163
164 .. _example_abi_macro_usage:
165
166 Examples of ABI Macro use
167 ~~~~~~~~~~~~~~~~~~~~~~~~~
168
169 Updating a public API
170 _____________________
171
172 Assume we have a function as follows
173
174 .. code-block:: c
175
176  /*
177   * Create an acl context object for apps to
178   * manipulate
179   */
180  struct rte_acl_ctx *
181  rte_acl_create(const struct rte_acl_param *param)
182  {
183         ...
184  }
185
186
187 Assume that struct rte_acl_ctx is a private structure, and that a developer
188 wishes to enhance the acl api so that a debugging flag can be enabled on a
189 per-context basis.  This requires an addition to the structure (which, being
190 private, is safe), but it also requires modifying the code as follows
191
192 .. code-block:: c
193
194  /*
195   * Create an acl context object for apps to
196   * manipulate
197   */
198  struct rte_acl_ctx *
199  rte_acl_create(const struct rte_acl_param *param, int debug)
200  {
201         ...
202  }
203
204
205 Note also that, being a public function, the header file prototype must also be
206 changed, as must all the call sites, to reflect the new ABI footprint.  We will
207 maintain previous ABI versions that are accessible only to previously compiled
208 binaries.
209
210 The addition of a parameter to the function is ABI breaking as the function is
211 public, and existing application may use it in its current form. However, the
212 compatibility macros in DPDK allow a developer to use symbol versioning so that
213 multiple functions can be mapped to the same public symbol based on when an
214 application was linked to it. To see how this is done, we start with the
215 requisite libraries version map file. Initially the version map file for the acl
216 library looks like this
217
218 .. code-block:: none
219
220    DPDK_20 {
221         global:
222
223         rte_acl_add_rules;
224         rte_acl_build;
225         rte_acl_classify;
226         rte_acl_classify_alg;
227         rte_acl_classify_scalar;
228         rte_acl_create;
229         rte_acl_dump;
230         rte_acl_find_existing;
231         rte_acl_free;
232         rte_acl_ipv4vlan_add_rules;
233         rte_acl_ipv4vlan_build;
234         rte_acl_list_dump;
235         rte_acl_reset;
236         rte_acl_reset_rules;
237         rte_acl_set_ctx_classify;
238
239         local: *;
240    };
241
242 This file needs to be modified as follows
243
244 .. code-block:: none
245
246    DPDK_20 {
247         global:
248
249         rte_acl_add_rules;
250         rte_acl_build;
251         rte_acl_classify;
252         rte_acl_classify_alg;
253         rte_acl_classify_scalar;
254         rte_acl_create;
255         rte_acl_dump;
256         rte_acl_find_existing;
257         rte_acl_free;
258         rte_acl_ipv4vlan_add_rules;
259         rte_acl_ipv4vlan_build;
260         rte_acl_list_dump;
261         rte_acl_reset;
262         rte_acl_reset_rules;
263         rte_acl_set_ctx_classify;
264
265         local: *;
266    };
267
268    DPDK_21 {
269         global:
270         rte_acl_create;
271
272    } DPDK_20;
273
274 The addition of the new block tells the linker that a new version node
275 ``DPDK_21`` is available, which contains the symbol rte_acl_create, and inherits
276 the symbols from the DPDK_20 node. This list is directly translated into a
277 list of exported symbols when DPDK is compiled as a shared library.
278
279 Next, we need to specify in the code which function maps to the rte_acl_create
280 symbol at which versions.  First, at the site of the initial symbol definition,
281 we need to update the function so that it is uniquely named, and not in conflict
282 with the public symbol name
283
284 .. code-block:: c
285
286  -struct rte_acl_ctx *
287  -rte_acl_create(const struct rte_acl_param *param)
288  +struct rte_acl_ctx * __vsym
289  +rte_acl_create_v20(const struct rte_acl_param *param)
290  {
291         size_t sz;
292         struct rte_acl_ctx *ctx;
293         ...
294
295 Note that the base name of the symbol was kept intact, as this is conducive to
296 the macros used for versioning symbols and we have annotated the function as
297 ``__vsym``, an implementation of a versioned symbol . That is our next step,
298 mapping this new symbol name to the initial symbol name at version node 20.
299 Immediately after the function, we add the VERSION_SYMBOL macro.
300
301 .. code-block:: c
302
303    #include <rte_function_versioning.h>
304
305    ...
306    VERSION_SYMBOL(rte_acl_create, _v20, 20);
307
308 Remembering to also add the rte_function_versioning.h header to the requisite c
309 file where these changes are being made. The macro instructs the linker to
310 create a new symbol ``rte_acl_create@DPDK_20``, which matches the symbol created
311 in older builds, but now points to the above newly named function. We have now
312 mapped the original rte_acl_create symbol to the original function (but with a
313 new name).
314
315 Please see the section :ref:`Enabling versioning macros
316 <enabling_versioning_macros>` to enable this macro in the meson/ninja build.
317 Next, we need to create the new ``v21`` version of the symbol. We create a new
318 function name, with the ``v21`` suffix, and implement it appropriately.
319
320 .. code-block:: c
321
322    struct rte_acl_ctx * __vsym
323    rte_acl_create_v21(const struct rte_acl_param *param, int debug);
324    {
325         struct rte_acl_ctx *ctx = rte_acl_create_v20(param);
326
327         ctx->debug = debug;
328
329         return ctx;
330    }
331
332 This code serves as our new API call. Its the same as our old call, but adds the
333 new parameter in place. Next we need to map this function to the new default
334 symbol ``rte_acl_create@DPDK_21``. To do this, immediately after the function,
335 we add the BIND_DEFAULT_SYMBOL macro.
336
337 .. code-block:: c
338
339    #include <rte_function_versioning.h>
340
341    ...
342    BIND_DEFAULT_SYMBOL(rte_acl_create, _v21, 21);
343
344 The macro instructs the linker to create the new default symbol
345 ``rte_acl_create@DPDK_21``, which points to the above newly named function.
346
347 We finally modify the prototype of the call in the public header file,
348 such that it contains both versions of the symbol and the public API.
349
350 .. code-block:: c
351
352    struct rte_acl_ctx *
353    rte_acl_create(const struct rte_acl_param *param);
354
355    struct rte_acl_ctx * __vsym
356    rte_acl_create_v20(const struct rte_acl_param *param);
357
358    struct rte_acl_ctx * __vsym
359    rte_acl_create_v21(const struct rte_acl_param *param, int debug);
360
361
362 And that's it, on the next shared library rebuild, there will be two versions of
363 rte_acl_create, an old DPDK_20 version, used by previously built applications,
364 and a new DPDK_21 version, used by future built applications.
365
366 .. note::
367
368    **Before you leave**, please take care reviewing the sections on
369    :ref:`mapping static symbols <mapping_static_symbols>`,
370    :ref:`enabling versioning macros <enabling_versioning_macros>`,
371    and :ref:`ABI deprecation <abi_deprecation>`.
372
373
374 .. _mapping_static_symbols:
375
376 Mapping static symbols
377 ______________________
378
379 Now we've taken what was a public symbol, and duplicated it into two uniquely
380 and differently named symbols. We've then mapped each of those back to the
381 public symbol ``rte_acl_create`` with different version tags. This only applies
382 to dynamic linking, as static linking has no notion of versioning. That leaves
383 this code in a position of no longer having a symbol simply named
384 ``rte_acl_create`` and a static build will fail on that missing symbol.
385
386 To correct this, we can simply map a function of our choosing back to the public
387 symbol in the static build with the ``MAP_STATIC_SYMBOL`` macro.  Generally the
388 assumption is that the most recent version of the symbol is the one you want to
389 map.  So, back in the C file where, immediately after ``rte_acl_create_v21`` is
390 defined, we add this
391
392
393 .. code-block:: c
394
395    struct rte_acl_ctx * __vsym
396    rte_acl_create_v21(const struct rte_acl_param *param, int debug)
397    {
398         ...
399    }
400    MAP_STATIC_SYMBOL(struct rte_acl_ctx *rte_acl_create(const struct rte_acl_param *param, int debug), rte_acl_create_v21);
401
402 That tells the compiler that, when building a static library, any calls to the
403 symbol ``rte_acl_create`` should be linked to ``rte_acl_create_v21``
404
405
406 .. _enabling_versioning_macros:
407
408 Enabling versioning macros
409 __________________________
410
411 Finally, we need to indicate to the :doc:`meson/ninja build system
412 <../prog_guide/build-sdk-meson>` to enable versioning macros when building the
413 library or driver. In the libraries or driver where we have added symbol
414 versioning, in the ``meson.build`` file we add the following
415
416 .. code-block:: none
417
418    use_function_versioning = true
419
420 at the start of the head of the file. This will indicate to the tool-chain to
421 enable the function version macros when building. There is no corresponding
422 directive required for the ``make`` build system.
423
424
425 .. _aliasing_experimental_symbols:
426
427 Aliasing experimental symbols
428 _____________________________
429
430 In situations in which an ``experimental`` symbol has been stable for some time,
431 and it becomes a candidate for promotion to the stable ABI. At this time, when
432 promoting the symbol, maintainer may choose to provide an alias to the
433 ``experimental`` symbol version, so as not to break consuming applications.
434
435 The process to provide an alias to ``experimental`` is similar to that, of
436 :ref:`symbol versioning <example_abi_macro_usage>` described above.
437 Assume we have an experimental function ``rte_acl_create`` as follows:
438
439 .. code-block:: c
440
441    #include <rte_compat.h>
442
443    /*
444     * Create an acl context object for apps to
445     * manipulate
446     */
447    __rte_experimental
448    struct rte_acl_ctx *
449    rte_acl_create(const struct rte_acl_param *param)
450    {
451    ...
452    }
453
454 In the map file, experimental symbols are listed as part of the ``EXPERIMENTAL``
455 version node.
456
457 .. code-block:: none
458
459    DPDK_20 {
460         global:
461         ...
462
463         local: *;
464    };
465
466    EXPERIMENTAL {
467         global:
468
469         rte_acl_create;
470    };
471
472 When we promote the symbol to the stable ABI, we simply strip the
473 ``__rte_experimental`` annotation from the function and move the symbol from the
474 ``EXPERIMENTAL`` node, to the node of the next major ABI version as follow.
475
476 .. code-block:: c
477
478    /*
479     * Create an acl context object for apps to
480     * manipulate
481     */
482    struct rte_acl_ctx *
483    rte_acl_create(const struct rte_acl_param *param)
484    {
485           ...
486    }
487
488 We then update the map file, adding the symbol ``rte_acl_create``
489 to the ``DPDK_21`` version node.
490
491 .. code-block:: none
492
493    DPDK_20 {
494         global:
495         ...
496
497         local: *;
498    };
499
500    DPDK_21 {
501         global:
502
503         rte_acl_create;
504    } DPDK_20;
505
506
507 Although there are strictly no guarantees or commitments associated with
508 :ref:`experimental symbols <experimental_apis>`, a maintainer may wish to offer
509 an alias to experimental. The process to add an alias to experimental,
510 is similar to the symbol versioning process. Assuming we have an experimental
511 symbol as before, we now add the symbol to both the ``EXPERIMENTAL``
512 and ``DPDK_21`` version nodes.
513
514 .. code-block:: c
515
516    #include <rte_compat.h>;
517    #include <rte_function_versioning.h>
518
519    /*
520     * Create an acl context object for apps to
521     * manipulate
522     */
523    struct rte_acl_ctx *
524    rte_acl_create(const struct rte_acl_param *param)
525    {
526    ...
527    }
528
529    __rte_experimental
530    struct rte_acl_ctx *
531    rte_acl_create_e(const struct rte_acl_param *param)
532    {
533       return rte_acl_create(param);
534    }
535    VERSION_SYMBOL_EXPERIMENTAL(rte_acl_create, _e);
536
537    struct rte_acl_ctx *
538    rte_acl_create_v21(const struct rte_acl_param *param)
539    {
540       return rte_acl_create(param);
541    }
542    BIND_DEFAULT_SYMBOL(rte_acl_create, _v21, 21);
543
544 In the map file, we map the symbol to both the ``EXPERIMENTAL``
545 and ``DPDK_21`` version nodes.
546
547 .. code-block:: none
548
549    DPDK_20 {
550         global:
551         ...
552
553         local: *;
554    };
555
556    DPDK_21 {
557         global:
558
559         rte_acl_create;
560    } DPDK_20;
561
562    EXPERIMENTAL {
563         global:
564
565         rte_acl_create;
566    };
567
568 .. note::
569
570    Please note, similar to :ref:`symbol versioning <example_abi_macro_usage>`,
571    when aliasing to experimental you will also need to take care of
572    :ref:`mapping static symbols <mapping_static_symbols>`.
573
574
575 .. _abi_deprecation:
576
577 Deprecating part of a public API
578 ________________________________
579
580 Lets assume that you've done the above updates, and in preparation for the next
581 major ABI version you decide you would like to retire the old version of the
582 function. After having gone through the ABI deprecation announcement process,
583 removal is easy. Start by removing the symbol from the requisite version map
584 file:
585
586 .. code-block:: none
587
588    DPDK_20 {
589         global:
590
591         rte_acl_add_rules;
592         rte_acl_build;
593         rte_acl_classify;
594         rte_acl_classify_alg;
595         rte_acl_classify_scalar;
596         rte_acl_dump;
597  -      rte_acl_create
598         rte_acl_find_existing;
599         rte_acl_free;
600         rte_acl_ipv4vlan_add_rules;
601         rte_acl_ipv4vlan_build;
602         rte_acl_list_dump;
603         rte_acl_reset;
604         rte_acl_reset_rules;
605         rte_acl_set_ctx_classify;
606
607         local: *;
608    };
609
610    DPDK_21 {
611         global:
612         rte_acl_create;
613    } DPDK_20;
614
615
616 Next remove the corresponding versioned export.
617
618 .. code-block:: c
619
620  -VERSION_SYMBOL(rte_acl_create, _v20, 20);
621
622
623 Note that the internal function definition could also be removed, but its used
624 in our example by the newer version ``v21``, so we leave it in place and declare
625 it as static. This is a coding style choice.
626
627 .. _deprecating_entire_abi:
628
629 Deprecating an entire ABI version
630 _________________________________
631
632 While removing a symbol from an ABI may be useful, it is more practical to
633 remove an entire version node at once, as is typically done at the declaration
634 of a major ABI version. If a version node completely specifies an API, then
635 removing part of it, typically makes it incomplete. In those cases it is better
636 to remove the entire node.
637
638 To do this, start by modifying the version map file, such that all symbols from
639 the node to be removed are merged into the next node in the map.
640
641 In the case of our map above, it would transform to look as follows
642
643 .. code-block:: none
644
645    DPDK_21 {
646         global:
647
648         rte_acl_add_rules;
649         rte_acl_build;
650         rte_acl_classify;
651         rte_acl_classify_alg;
652         rte_acl_classify_scalar;
653         rte_acl_dump;
654         rte_acl_create
655         rte_acl_find_existing;
656         rte_acl_free;
657         rte_acl_ipv4vlan_add_rules;
658         rte_acl_ipv4vlan_build;
659         rte_acl_list_dump;
660         rte_acl_reset;
661         rte_acl_reset_rules;
662         rte_acl_set_ctx_classify;
663
664         local: *;
665  };
666
667 Then any uses of BIND_DEFAULT_SYMBOL that pointed to the old node should be
668 updated to point to the new version node in any header files for all affected
669 symbols.
670
671 .. code-block:: c
672
673  -BIND_DEFAULT_SYMBOL(rte_acl_create, _v20, 20);
674  +BIND_DEFAULT_SYMBOL(rte_acl_create, _v21, 21);
675
676 Lastly, any VERSION_SYMBOL macros that point to the old version node should be
677 removed, taking care to keep, where need old code in place to support newer
678 versions of the symbol.
679
680
681 Running the ABI Validator
682 -------------------------
683
684 The ``devtools`` directory in the DPDK source tree contains a utility program,
685 ``check-abi.sh``, for validating the DPDK ABI based on the libabigail
686 `abidiff utility <https://sourceware.org/libabigail/manual/abidiff.html>`_.
687
688 The syntax of the ``check-abi.sh`` utility is::
689
690    devtools/check-abi.sh <refdir> <newdir>
691
692 Where <refdir> specifies the directory housing the reference build of DPDK,
693 and <newdir> specifies the DPDK build directory to check the ABI of.
694
695 The ABI compatibility is automatically verified when using a build script
696 from ``devtools``, if the variable ``DPDK_ABI_REF_VERSION`` is set with a tag,
697 as described in :ref:`ABI check recommendations<integrated_abi_check>`.