doc: separate versioning guide into version and policy
[dpdk.git] / doc / guides / contributing / abi_versioning.rst
1 ..  SPDX-License-Identifier: BSD-3-Clause
2     Copyright 2018 The DPDK contributors
3
4 .. library_versioning:
5
6 Library versioning
7 ------------------
8
9 Downstreams might want to provide different DPDK releases at the same time to
10 support multiple consumers of DPDK linked against older and newer sonames.
11
12 Also due to the interdependencies that DPDK libraries can have applications
13 might end up with an executable space in which multiple versions of a library
14 are mapped by ld.so.
15
16 Think of LibA that got an ABI bump and LibB that did not get an ABI bump but is
17 depending on LibA.
18
19 .. note::
20
21     Application
22     \-> LibA.old
23     \-> LibB.new -> LibA.new
24
25 That is a conflict which can be avoided by setting ``CONFIG_RTE_MAJOR_ABI``.
26 If set, the value of ``CONFIG_RTE_MAJOR_ABI`` overwrites all - otherwise per
27 library - versions defined in the libraries ``LIBABIVER``.
28 An example might be ``CONFIG_RTE_MAJOR_ABI=16.11`` which will make all libraries
29 ``librte<?>.so.16.11`` instead of ``librte<?>.so.<LIBABIVER>``.
30
31
32 ABI versioning
33 --------------
34
35 Versioning Macros
36 ~~~~~~~~~~~~~~~~~
37
38 When a symbol is exported from a library to provide an API, it also provides a
39 calling convention (ABI) that is embodied in its name, return type and
40 arguments. Occasionally that function may need to change to accommodate new
41 functionality or behavior. When that occurs, it is desirable to allow for
42 backward compatibility for a time with older binaries that are dynamically
43 linked to the DPDK.
44
45 To support backward compatibility the ``rte_function_versioning.h``
46 header file provides macros to use when updating exported functions. These
47 macros are used in conjunction with the ``rte_<library>_version.map`` file for
48 a given library to allow multiple versions of a symbol to exist in a shared
49 library so that older binaries need not be immediately recompiled.
50
51 The macros exported are:
52
53 * ``VERSION_SYMBOL(b, e, n)``: Creates a symbol version table entry binding
54   versioned symbol ``b@DPDK_n`` to the internal function ``be``.
55
56 * ``BIND_DEFAULT_SYMBOL(b, e, n)``: Creates a symbol version entry instructing
57   the linker to bind references to symbol ``b`` to the internal symbol
58   ``be``.
59
60 * ``MAP_STATIC_SYMBOL(f, p)``: Declare the prototype ``f``, and map it to the
61   fully qualified function ``p``, so that if a symbol becomes versioned, it
62   can still be mapped back to the public symbol name.
63
64 * ``__vsym``:  Annotation to be used in a declaration of the internal symbol
65   ``be`` to signal that it is being used as an implementation of a particular
66   version of symbol ``b``.
67
68 Examples of ABI Macro use
69 ^^^^^^^^^^^^^^^^^^^^^^^^^
70
71 Updating a public API
72 _____________________
73
74 Assume we have a function as follows
75
76 .. code-block:: c
77
78  /*
79   * Create an acl context object for apps to
80   * manipulate
81   */
82  struct rte_acl_ctx *
83  rte_acl_create(const struct rte_acl_param *param)
84  {
85         ...
86  }
87
88
89 Assume that struct rte_acl_ctx is a private structure, and that a developer
90 wishes to enhance the acl api so that a debugging flag can be enabled on a
91 per-context basis.  This requires an addition to the structure (which, being
92 private, is safe), but it also requires modifying the code as follows
93
94 .. code-block:: c
95
96  /*
97   * Create an acl context object for apps to
98   * manipulate
99   */
100  struct rte_acl_ctx *
101  rte_acl_create(const struct rte_acl_param *param, int debug)
102  {
103         ...
104  }
105
106
107 Note also that, being a public function, the header file prototype must also be
108 changed, as must all the call sites, to reflect the new ABI footprint.  We will
109 maintain previous ABI versions that are accessible only to previously compiled
110 binaries
111
112 The addition of a parameter to the function is ABI breaking as the function is
113 public, and existing application may use it in its current form.  However, the
114 compatibility macros in DPDK allow a developer to use symbol versioning so that
115 multiple functions can be mapped to the same public symbol based on when an
116 application was linked to it.  To see how this is done, we start with the
117 requisite libraries version map file.  Initially the version map file for the
118 acl library looks like this
119
120 .. code-block:: none
121
122    DPDK_2.0 {
123         global:
124
125         rte_acl_add_rules;
126         rte_acl_build;
127         rte_acl_classify;
128         rte_acl_classify_alg;
129         rte_acl_classify_scalar;
130         rte_acl_create;
131         rte_acl_dump;
132         rte_acl_find_existing;
133         rte_acl_free;
134         rte_acl_ipv4vlan_add_rules;
135         rte_acl_ipv4vlan_build;
136         rte_acl_list_dump;
137         rte_acl_reset;
138         rte_acl_reset_rules;
139         rte_acl_set_ctx_classify;
140
141         local: *;
142    };
143
144 This file needs to be modified as follows
145
146 .. code-block:: none
147
148    DPDK_2.0 {
149         global:
150
151         rte_acl_add_rules;
152         rte_acl_build;
153         rte_acl_classify;
154         rte_acl_classify_alg;
155         rte_acl_classify_scalar;
156         rte_acl_create;
157         rte_acl_dump;
158         rte_acl_find_existing;
159         rte_acl_free;
160         rte_acl_ipv4vlan_add_rules;
161         rte_acl_ipv4vlan_build;
162         rte_acl_list_dump;
163         rte_acl_reset;
164         rte_acl_reset_rules;
165         rte_acl_set_ctx_classify;
166
167         local: *;
168    };
169
170    DPDK_2.1 {
171         global:
172         rte_acl_create;
173
174    } DPDK_2.0;
175
176 The addition of the new block tells the linker that a new version node is
177 available (DPDK_2.1), which contains the symbol rte_acl_create, and inherits the
178 symbols from the DPDK_2.0 node.  This list is directly translated into a list of
179 exported symbols when DPDK is compiled as a shared library
180
181 Next, we need to specify in the code which function map to the rte_acl_create
182 symbol at which versions.  First, at the site of the initial symbol definition,
183 we need to update the function so that it is uniquely named, and not in conflict
184 with the public symbol name
185
186 .. code-block:: c
187
188  -struct rte_acl_ctx *
189  -rte_acl_create(const struct rte_acl_param *param)
190  +struct rte_acl_ctx * __vsym
191  +rte_acl_create_v20(const struct rte_acl_param *param)
192  {
193         size_t sz;
194         struct rte_acl_ctx *ctx;
195         ...
196
197 Note that the base name of the symbol was kept intact, as this is conducive to
198 the macros used for versioning symbols and we have annotated the function as an
199 implementation of versioned symbol.  That is our next step, mapping this new
200 symbol name to the initial symbol name at version node 2.0.  Immediately after
201 the function, we add this line of code
202
203 .. code-block:: c
204
205    VERSION_SYMBOL(rte_acl_create, _v20, 2.0);
206
207 Remembering to also add the rte_function_versioning.h header to the requisite c file where
208 these changes are being made.  The above macro instructs the linker to create a
209 new symbol ``rte_acl_create@DPDK_2.0``, which matches the symbol created in older
210 builds, but now points to the above newly named function.  We have now mapped
211 the original rte_acl_create symbol to the original function (but with a new
212 name)
213
214 Next, we need to create the 2.1 version of the symbol.  We create a new function
215 name, with a different suffix, and  implement it appropriately
216
217 .. code-block:: c
218
219    struct rte_acl_ctx * __vsym
220    rte_acl_create_v21(const struct rte_acl_param *param, int debug);
221    {
222         struct rte_acl_ctx *ctx = rte_acl_create_v20(param);
223
224         ctx->debug = debug;
225
226         return ctx;
227    }
228
229 This code serves as our new API call.  Its the same as our old call, but adds
230 the new parameter in place.  Next we need to map this function to the symbol
231 ``rte_acl_create@DPDK_2.1``.  To do this, we modify the public prototype of the call
232 in the header file, adding the macro there to inform all including applications,
233 that on re-link, the default rte_acl_create symbol should point to this
234 function.  Note that we could do this by simply naming the function above
235 rte_acl_create, and the linker would chose the most recent version tag to apply
236 in the version script, but we can also do this in the header file
237
238 .. code-block:: c
239
240    struct rte_acl_ctx *
241    -rte_acl_create(const struct rte_acl_param *param);
242    +rte_acl_create(const struct rte_acl_param *param, int debug);
243    +BIND_DEFAULT_SYMBOL(rte_acl_create, _v21, 2.1);
244
245 The BIND_DEFAULT_SYMBOL macro explicitly tells applications that include this
246 header, to link to the rte_acl_create_v21 function and apply the DPDK_2.1
247 version node to it.  This method is more explicit and flexible than just
248 re-implementing the exact symbol name, and allows for other features (such as
249 linking to the old symbol version by default, when the new ABI is to be opt-in
250 for a period.
251
252 One last thing we need to do.  Note that we've taken what was a public symbol,
253 and duplicated it into two uniquely and differently named symbols.  We've then
254 mapped each of those back to the public symbol ``rte_acl_create`` with different
255 version tags.  This only applies to dynamic linking, as static linking has no
256 notion of versioning.  That leaves this code in a position of no longer having a
257 symbol simply named ``rte_acl_create`` and a static build will fail on that
258 missing symbol.
259
260 To correct this, we can simply map a function of our choosing back to the public
261 symbol in the static build with the ``MAP_STATIC_SYMBOL`` macro.  Generally the
262 assumption is that the most recent version of the symbol is the one you want to
263 map.  So, back in the C file where, immediately after ``rte_acl_create_v21`` is
264 defined, we add this
265
266 .. code-block:: c
267
268    struct rte_acl_ctx * __vsym
269    rte_acl_create_v21(const struct rte_acl_param *param, int debug)
270    {
271         ...
272    }
273    MAP_STATIC_SYMBOL(struct rte_acl_ctx *rte_acl_create(const struct rte_acl_param *param, int debug), rte_acl_create_v21);
274
275 That tells the compiler that, when building a static library, any calls to the
276 symbol ``rte_acl_create`` should be linked to ``rte_acl_create_v21``
277
278 That's it, on the next shared library rebuild, there will be two versions of
279 rte_acl_create, an old DPDK_2.0 version, used by previously built applications,
280 and a new DPDK_2.1 version, used by future built applications.
281
282
283 Deprecating part of a public API
284 ________________________________
285
286 Lets assume that you've done the above update, and after a few releases have
287 passed you decide you would like to retire the old version of the function.
288 After having gone through the ABI deprecation announcement process, removal is
289 easy.  Start by removing the symbol from the requisite version map file:
290
291 .. code-block:: none
292
293    DPDK_2.0 {
294         global:
295
296         rte_acl_add_rules;
297         rte_acl_build;
298         rte_acl_classify;
299         rte_acl_classify_alg;
300         rte_acl_classify_scalar;
301         rte_acl_dump;
302  -      rte_acl_create
303         rte_acl_find_existing;
304         rte_acl_free;
305         rte_acl_ipv4vlan_add_rules;
306         rte_acl_ipv4vlan_build;
307         rte_acl_list_dump;
308         rte_acl_reset;
309         rte_acl_reset_rules;
310         rte_acl_set_ctx_classify;
311
312         local: *;
313    };
314
315    DPDK_2.1 {
316         global:
317         rte_acl_create;
318    } DPDK_2.0;
319
320
321 Next remove the corresponding versioned export.
322
323 .. code-block:: c
324
325  -VERSION_SYMBOL(rte_acl_create, _v20, 2.0);
326
327
328 Note that the internal function definition could also be removed, but its used
329 in our example by the newer version _v21, so we leave it in place.  This is a
330 coding style choice.
331
332 Lastly, we need to bump the LIBABIVER number for this library in the Makefile to
333 indicate to applications doing dynamic linking that this is a later, and
334 possibly incompatible library version:
335
336 .. code-block:: c
337
338    -LIBABIVER := 1
339    +LIBABIVER := 2
340
341 Deprecating an entire ABI version
342 _________________________________
343
344 While removing a symbol from and ABI may be useful, it is often more practical
345 to remove an entire version node at once.  If a version node completely
346 specifies an API, then removing part of it, typically makes it incomplete.  In
347 those cases it is better to remove the entire node
348
349 To do this, start by modifying the version map file, such that all symbols from
350 the node to be removed are merged into the next node in the map
351
352 In the case of our map above, it would transform to look as follows
353
354 .. code-block:: none
355
356    DPDK_2.1 {
357         global:
358
359         rte_acl_add_rules;
360         rte_acl_build;
361         rte_acl_classify;
362         rte_acl_classify_alg;
363         rte_acl_classify_scalar;
364         rte_acl_dump;
365         rte_acl_create
366         rte_acl_find_existing;
367         rte_acl_free;
368         rte_acl_ipv4vlan_add_rules;
369         rte_acl_ipv4vlan_build;
370         rte_acl_list_dump;
371         rte_acl_reset;
372         rte_acl_reset_rules;
373         rte_acl_set_ctx_classify;
374
375         local: *;
376  };
377
378 Then any uses of BIND_DEFAULT_SYMBOL that pointed to the old node should be
379 updated to point to the new version node in any header files for all affected
380 symbols.
381
382 .. code-block:: c
383
384  -BIND_DEFAULT_SYMBOL(rte_acl_create, _v20, 2.0);
385  +BIND_DEFAULT_SYMBOL(rte_acl_create, _v21, 2.1);
386
387 Lastly, any VERSION_SYMBOL macros that point to the old version node should be
388 removed, taking care to keep, where need old code in place to support newer
389 versions of the symbol.
390
391
392 Running the ABI Validator
393 -------------------------
394
395 The ``devtools`` directory in the DPDK source tree contains a utility program,
396 ``validate-abi.sh``, for validating the DPDK ABI based on the Linux `ABI
397 Compliance Checker
398 <http://ispras.linuxbase.org/index.php/ABI_compliance_checker>`_.
399
400 This has a dependency on the ``abi-compliance-checker`` and ``and abi-dumper``
401 utilities which can be installed via a package manager. For example::
402
403    sudo yum install abi-compliance-checker
404    sudo yum install abi-dumper
405
406 The syntax of the ``validate-abi.sh`` utility is::
407
408    ./devtools/validate-abi.sh <REV1> <REV2>
409
410 Where ``REV1`` and ``REV2`` are valid gitrevisions(7)
411 https://www.kernel.org/pub/software/scm/git/docs/gitrevisions.html
412 on the local repo.
413
414 For example::
415
416    # Check between the previous and latest commit:
417    ./devtools/validate-abi.sh HEAD~1 HEAD
418
419    # Check on a specific compilation target:
420    ./devtools/validate-abi.sh -t x86_64-native-linux-gcc HEAD~1 HEAD
421
422    # Check between two tags:
423    ./devtools/validate-abi.sh v2.0.0 v2.1.0
424
425    # Check between git master and local topic-branch "vhost-hacking":
426    ./devtools/validate-abi.sh master vhost-hacking
427
428 After the validation script completes (it can take a while since it need to
429 compile both tags) it will create compatibility reports in the
430 ``./abi-check/compat_report`` directory. Listed incompatibilities can be found
431 as follows::
432
433   grep -lr Incompatible abi-check/compat_reports/