]> git.proxmox.com Git - mirror_frr.git/blame - doc/developer/workflow.rst
Merge pull request #10504 from qingkaishi/master
[mirror_frr.git] / doc / developer / workflow.rst
CommitLineData
9de103f0
QY
1.. _process-and-workflow:
2
3*******************
b22ba015 4Process & Workflow
9de103f0 5*******************
d1890d04 6
b6820993
QY
7.. highlight:: none
8
b22ba015
QY
9FRR is a large project developed by many different groups. This section
10documents standards for code style & quality, commit messages, pull requests
11and best practices that all contributors are asked to follow.
d1890d04 12
9de103f0
QY
13This chapter is "descriptive/post-factual" in that it documents pratices that
14are in use; it is not "definitive/pre-factual" in prescribing practices. This
b22ba015
QY
15means that when a procedure changes, it is agreed upon, then put into practice,
16and then documented here. If this document doesn't match reality, it's the
17document that needs to be updated, not reality.
d1890d04 18
9de103f0
QY
19Mailing Lists
20=============
d1890d04 21
b22ba015
QY
22The FRR development group maintains multiple mailing lists for use by the
23community. Italicized lists are private.
d1890d04
QY
24
25+----------------------------------+--------------------------------+
26| Topic | List |
27+==================================+================================+
28| Development | dev@lists.frrouting.org |
29+----------------------------------+--------------------------------+
30| Users & Operators | frog@lists.frrouting.org |
31+----------------------------------+--------------------------------+
32| Announcements | announce@lists.frrouting.org |
33+----------------------------------+--------------------------------+
34| *Security* | security@lists.frrouting.org |
35+----------------------------------+--------------------------------+
36| *Technical Steering Committee* | tsc@lists.frrouting.org |
37+----------------------------------+--------------------------------+
38
9de103f0 39The Development list is used to discuss and document general issues related to
b6820993
QY
40project development and governance. The public
41`Slack instance <https://frrouting.slack.com>`_ and weekly technical meetings
42provide a higher bandwidth channel for discussions. The results of such
43discussions must be reflected in updates, as appropriate, to code (i.e.,
44merges), `GitHub issues`_, and for governance or process changes, updates to
45the Development list and either this file or information posted at
46https://frrouting.org/.
47
48Development & Release Cycle
49===========================
50
51Development
52-----------
53
54.. figure:: ../figures/git_branches.png
55 :align: center
56 :scale: 55%
57 :alt: Merging Git branches into a central trunk
58
59 Rough outline of FRR development workflow
60
61The master Git for FRR resides on `GitHub`_.
62
63There is one main branch for development, ``master``. For each major release
64(2.0, 3.0 etc) a new release branch is created based on the master. Significant
65bugfixes should be backported to upcoming and existing release branches no more
66than 1 year old. As a general rule new features are not backported to release
67branches.
8ce7861f 68
b6820993 69Subsequent point releases based on a major branch are handled with git tags.
c804874a 70
b6820993
QY
71Releases
72--------
73FRR employs a ``<MAJOR>.<MINOR>.<BUGFIX>`` versioning scheme.
c804874a 74
b6820993 75``MAJOR``
ac97970d
DL
76 Significant new features or multiple minor features. This should mostly
77 cover any kind of disruptive change that is visible or "risky" to operators.
78 New features or protocols do not necessarily trigger this. (This was changed
79 for FRR 7.x after feedback from users that the pace of major version number
80 increments was too high.)
c804874a 81
b6820993 82``MINOR``
ac97970d
DL
83 General incremental development releases, excluding "major" changes
84 mentioned above. Not necessarily fully backwards compatible, as smaller
85 (but still visible) changes or deprecated feature removals may still happen.
86 However, there shouldn't be any huge "surprises" between minor releases.
c804874a 87
b6820993 88``BUGFIX``
ac97970d 89 Fixes for actual bugs and/or security issues. Fully compatible.
c804874a 90
dc1c0bc2
DL
91Releases are scheduled in a 4-month cycle on the first Tuesday each
92March/July/November. Walking backwards from this date:
93
94 - 6 weeks earlier, ``master`` is frozen for new features, and feature PRs
95 are considered lowest priority (regardless of when they were opened.)
96
97 - 4 weeks earlier, the stable branch separates from master (named
5568f9d1
DA
98 ``dev/MAJOR.MINOR`` at this point) and tagged as ```base_X.Y``.
99 Master is unfrozen and new features may again proceed.
dc1c0bc2 100
16044e7f
DL
101 Part of unfreezing master is editing the ``AC_INIT`` statement in
102 :file:`configure.ac` to reflect the new development version that master
103 now refers to. This is accompanied by a ``frr-X.Y-dev`` tag on master,
104 which should always be on the first commit on master *after* the stable
105 branch was forked (even if that is not the edit to ``AC_INIT``; it's more
106 important to have it on the very first commit on master after the fork.)
107
108 (The :file:`configure.ac` edit and tag push are considered git housekeeping
109 and are pushed directly to ``master``, not through a PR.)
110
5568f9d1
DA
111 Below is the snippet of the commands to use in this step.
112
113 .. code-block:: console
114
115 % git remote --verbose
116 upstream git@github.com:frrouting/frr (fetch)
117 upstream git@github.com:frrouting/frr (push)
118
119 % git checkout master
120 % git pull upstream master
121 % git checkout -b dev/8.2
122 % git tag base_8.2
123 % git push upstream base_8.2
124 % git push upstream dev/8.2
125 % git checkout master
126 % sed -i 's/8.2-dev/8.3-dev/' configure.ac
127 % git tag -a frr-8.3-dev -m "frr-8.3-dev"
128 % git push upstream frr-8.3-dev
129 % git add configure.ac
130 % git commit -s -m "build: FRR 8.3 development version"
131 % git push upstream master
132
133 In this step, we also have to update package versions to reflect
134 the development version. Versions need to be updated using
135 a standard way of development (Pull Requests) based on master branch.
136
137 Only change the version number with no other changes. This will produce
138 packages with the a version number that is higher than any previous
139 version. Once the release is done, whatever updates we make to changelog
140 files on the release branch need to be cherry-picked to the master branch.
141
142 - 2 weeks earlier, a ``frr-X.Y-rc`` release candidate is tagged.
dc1c0bc2
DL
143
144 - on release date, the branch is renamed to ``stable/MAJOR.MINOR``.
145
146The 2 week window between each of these events should be used to run any and
147all testing possible for the release in progress. However, the current
148intention is to stick to the schedule even if known issues remain. This would
149hopefully occur only after all avenues of fixing issues are exhausted, but to
150achieve this, an as exhaustive as possible list of issues needs to be available
151as early as possible, i.e. the first 2-week window.
152
153For reference, the expected release schedule according to the above is:
154
5568f9d1
DA
155+---------+------------+------------+------------+------------+------------+
156| Release | 2021-11-02 | 2022-03-01 | 2022-07-05 | 2022-11-01 | 2023-03-07 |
157+---------+------------+------------+------------+------------+------------+
158| RC | 2021-10-19 | 2022-02-15 | 2022-06-21 | 2022-10-18 | 2023-02-21 |
159+---------+------------+------------+------------+------------+------------+
160| dev/X.Y | 2021-10-05 | 2022-02-01 | 2022-06-07 | 2022-10-04 | 2023-02-07 |
161+---------+------------+------------+------------+------------+------------+
162| freeze | 2021-09-21 | 2022-01-18 | 2022-05-24 | 2022-09-20 | 2023-01-24 |
163+---------+------------+------------+------------+------------+------------+
dc1c0bc2
DL
164
165Each release is managed by one or more volunteer release managers from the FRR
166community. To spread and distribute this workload, this should be rotated for
167subsequent releases. The release managers are currently assumed/expected to
168run a release management meeting during the weeks listed above. Barring other
169constraints, this would be scheduled before the regular weekly FRR community
170call such that important items can be carried over into that call.
c804874a 171
07ff01d2
PG
172Bugfixes are applied to the two most recent releases. However, backporting of bug
173fixes to older than the two most recent releases will not be prevented, if acked
174under the classical development workflow applying for a pull request.
175
176Security fixes are backported to all releases less than or equal to at least one
177year old. Security fixes may also be backported to older releases depending on
178severity.
179
f4bcc72f
QY
180For detailed instructions on how to produce an FRR release, refer to
181:ref:`frr-release-procedure`.
182
bd2b4fc3
PG
183
184Long term support branches ( LTS )
185-----------------------------------------
186
187This kind of branch is not yet officially supported, and need experimentation
188before being effective.
189
190Previous definition of releases prevents long term support of previous releases.
191For instance, bug and security fixes are not applied if the stable branch is too
192old.
193
194Because the FRR users have a need to backport bug and security fixes after the
195stable branch becomes too old, there is a need to provide support on a long term
196basis on that stable branch. If that support is applied on that stable branch,
197then that branch is a long term support branch.
198
199Having a LTS branch requires extra-work and requires one person to be in charge
200of that maintenance branch for a certain amount of time. The amount of time will
201be by default set to 4 months, and can be increased. 4 months stands for the time
202between two releases, this time can be applied to the decision to continue with a
203LTS release or not. In all cases, that time period will be well-defined and
204published. Also, a self nomination from a person that proposes to handle the LTS
205branch is required. The work can be shared by multiple people. In all cases, there
206must be at least one person that is in charge of the maintenance branch. The person
207on people responsible for a maintenance branch must be a FRR maintainer. Note that
208they may choose to abandon support for the maintenance branch at any time. If
56f0bea7 209no one takes over the responsibility of the LTS branch, then the support will be
bd2b4fc3
PG
210discontinued.
211
212The LTS branch duties are the following ones:
213
214- organise meetings on a (bi-)weekly or monthly basis, the handling of issues
215 and pull requested relative to that branch. When time permits, this may be done
216 during the regularly scheduled FRR meeting.
217
218- ensure the stability of the branch, by using and eventually adapting the
219 checking the CI tools of FRR ( indeed, maintaining may lead to create
220 maintenance branches for topotests or for CI).
221
222It will not be possible to backport feature requests to LTS branches. Actually, it
223is a false good idea to use LTS for that need. Introducing feature requests may
224break the paradigm where all more recent releases should also include the feature
225request. This would require the LTS maintainer to ensure that all more recent
226releases have support for this feature request. Moreover, introducing features
227requests may result in breaking the stability of the branch. LTS branches are first
228done to bring long term support for stability.
8ce7861f 229
16318c5c
DS
230Development Branches
231--------------------
232
233Occassionally the community will desire the ability to work together
234on a feature that is considered useful to FRR. In this case the
235parties may ask the Maintainers for the creation of a development
236branch in the main FRR repository. Requirements for this to happen
237are:
238
239- A one paragraph description of the feature being implemented to
240 allow for the facilitation of discussion about the feature. This
241 might include pointers to relevant RFC's or presentations that
242 explain what is planned. This is intended to set a somewhat
243 low bar for organization.
244- A branch maintainer must be named. This person is responsible for
245 keeping the branch up to date, and general communication about the
246 project with the other FRR Maintainers. Additionally this person
247 must already be a FRR Maintainer.
248- Commits to this branch must follow the normal PR and commit process
249 as outlined in other areas of this document. The goal of this is
250 to prevent the current state where large features are submitted
251 and are so large they are difficult to review.
252
253After a development branch has completed the work together, a final
254review can be made and the branch merged into master. If a development
255branch is becomes un-maintained or not being actively worked on after
256three months then the Maintainers can decide to remove the branch.
257
32db86a9
DL
258Debian Branches
259---------------
260
261The Debian project contains "official" packages for FRR. While FRR
262Maintainers may participate in creating these, it is entirely the Debian
263project's decision what to ship and how to work on this.
264
265As a courtesy and for FRR's benefit, this packaging work is currently visible
266in git branches named ``debian/*`` on the main FRR git repository. These
267branches are for the exclusive use by people involved in Debian packaging work
268for FRR. Direct commit access may be handed out and FRR git rules (review,
269testing, etc.) do not apply. Do not push to these branches without talking
270to the people noted under ``Maintainer:`` and ``Uploaders:`` in
271``debian/control`` on the target branch -- even if you are a FRR Maintainer.
272
d1890d04 273Changelog
b6820993 274---------
b22ba015
QY
275The changelog will be the base for the release notes. A changelog entry for
276your changes is usually not required and will be added based on your commit
277messages by the maintainers. However, you are free to include an update to the
278changelog with some better description.
d1890d04
QY
279
280Submitting Patches and Enhancements
9de103f0 281===================================
d1890d04 282
85c6ecca 283FRR accepts patches using GitHub pull requests.
b22ba015
QY
284
285The base branch for new contributions and non-critical bug fixes should be
286``master``. Please ensure your pull request is based on this branch when you
287submit it.
288
85c6ecca 289Code submitted by pull request will be automatically tested by one or more CI
b6820993
QY
290systems. Once the automated tests succeed, other developers will review your
291code for quality and correctness. After any concerns are resolved, your code
292will be merged into the branch it was submitted against.
d1890d04 293
01bf2ccb
LB
294The title of the pull request should provide a high level technical
295summary of the included patches. The description should provide
296additional details that will help the reviewer to understand the context
297of the included patches.
298
b6820993 299.. _license-for-contributions:
d1890d04 300
b6820993
QY
301License for Contributions
302-------------------------
303FRR is under a “GPLv2 or later” license. Any code submitted must be released
304under the same license (preferred) or any license which allows redistribution
305under this GPLv2 license (eg MIT License).
e2abcff8
PG
306It is forbidden to push any code that prevents from using GPLv3 license. This
307becomes a community rule, as FRR produces binaries that links with Apache 2.0
308libraries. Apache 2.0 and GPLv2 license are incompatible, if put together.
309Please see `<http://www.apache.org/licenses/GPL-compatibility.html>`_ for
310more information. This rule guarantees the user to distribute FRR binary code
311without any licensing issues.
b22ba015 312
b6820993
QY
313Pre-submission Checklist
314------------------------
315- Format code (see `Code Formatting <#code-formatting>`__)
316- Verify and acknowledge license (see :ref:`license-for-contributions`)
317- Ensure you have properly signed off (see :ref:`signing-off`)
318- Test building with various configurations:
d1890d04 319
b6820993 320 - ``buildtest.sh``
d1890d04 321
b6820993 322- Verify building source distribution:
d1890d04 323
b6820993 324 - ``make dist`` (and try rebuilding from the resulting tar file)
d1890d04 325
b6820993 326- Run unit tests:
d1890d04 327
b6820993 328 - ``make test``
d1890d04 329
b6820993 330- In the case of a major new feature or other significant change, document
8bc6e629
DS
331 plans for continued maintenance of the feature. In addition it is a
332 requirement that automated testing must be written that exercises
431dd37e 333 the new feature within our existing CI infrastructure. Also the
8bc6e629 334 addition of automated testing to cover any pull request is encouraged.
d1890d04 335
b6820993 336.. _signing-off:
d1890d04 337
b6820993
QY
338Signing Off
339-----------
340Code submitted to FRR must be signed off. We have the same requirements for
341using the signed-off-by process as the Linux kernel. In short, you must include
342a ``Signed-off-by`` tag in every patch.
d1890d04 343
118cf7ed
SW
344An easy way to do this is to use ``git commit -s`` where ``-s`` will automatically
345append a signed-off line to the end of your commit message. Also, if you commit
346and forgot to add the line you can use ``git commit --amend -s`` to add the
347signed-off line to the last commit.
348
b6820993
QY
349``Signed-off-by`` is a developer's certification that they have the right to
350submit the patch for inclusion into the project. It is an agreement to the
351:ref:`Developer's Certificate of Origin <developers-certificate-of-origin>`.
352Code without a proper ``Signed-off-by`` line cannot and will not be merged.
d1890d04 353
b6820993
QY
354If you are unfamiliar with this process, you should read the
355`official policy at kernel.org <https://www.kernel.org/doc/html/latest/process/submitting-patches.html>`_.
356You might also find
357`this article <http://www.linuxfoundation.org/content/how-participate-linux-community-0>`_
358about participating in the Linux community on the Linux Foundation website to
359be a helpful resource.
d1890d04 360
b6820993 361.. _developers-certificate-of-origin:
d1890d04 362
b6820993
QY
363In short, when you sign off on a commit, you assert your agreement to all of
364the following::
d1890d04 365
b6820993 366 Developer's Certificate of Origin 1.1
d1890d04 367
b6820993 368 By making a contribution to this project, I certify that:
d1890d04 369
b6820993
QY
370 (a) The contribution was created in whole or in part by me and I
371 have the right to submit it under the open source license
372 indicated in the file; or
d1890d04 373
b6820993
QY
374 (b) The contribution is based upon previous work that, to the best
375 of my knowledge, is covered under an appropriate open source
376 license and I have the right under that license to submit that
377 work with modifications, whether created in whole or in part by
378 me, under the same open source license (unless I am permitted to
379 submit under a different license), as indicated in the file; or
d1890d04 380
b6820993
QY
381 (c) The contribution was provided directly to me by some other
382 person who certified (a), (b) or (c) and I have not modified it.
d1890d04 383
b6820993
QY
384 (d) I understand and agree that this project and the contribution
385 are public and that a record of the contribution (including all
386 personal information I submit with it, including my sign-off) is
387 maintained indefinitely and may be redistributed consistent with
388 this project or the open source license(s) involved.
d1890d04 389
b6820993 390After Submitting Your Changes
d1890d04
QY
391-----------------------------
392
b6820993 393- Watch for Continuous Integration (CI) test results
d1890d04
QY
394
395 - You should automatically receive an email with the test results
396 within less than 2 hrs of the submission. If you don’t get the
b6820993 397 email, then check status on the GitHub pull request.
d1890d04 398 - Please notify the development mailing list if you think something
b22ba015 399 doesn't work.
d1890d04
QY
400
401- If the tests failed:
402
403 - In general, expect the community to ignore the submission until
404 the tests pass.
405 - It is up to you to fix and resubmit.
406
407 - This includes fixing existing unit (“make test”) tests if your
408 changes broke or changed them.
409 - It also includes fixing distribution packages for the failing
410 platforms (ie if new libraries are required).
411 - Feel free to ask for help on the development list.
412
413 - Go back to the submission process and repeat until the tests pass.
414
415- If the tests pass:
416
417 - Wait for reviewers. Someone will review your code or be assigned
418 to review your code.
493e3eed
LB
419 - Respond to any comments or concerns the reviewer has. Use e-mail or
420 add a comment via github to respond or to let the reviewer know how
421 their comment or concern is addressed.
422 - An author must never delete or manually dismiss someone else's comments
423 or review. (A review may be overridden by agreement in the weekly
424 technical meeting.)
70aa675d
DL
425 - When you have addressed someone's review comments, please click the
426 "re-request review" button (in the top-right corner of the PR page, next
427 to the reviewer's name, an icon that looks like "reload")
428 - The responsibility for keeping a PR moving rests with the author at
429 least as long as there are either negative CI results or negative review
430 comments. If you forget to mark a review comment as addressed (by
431 clicking re-request review), the reviewer may very well not notice and
432 won't come back to your PR.
493e3eed
LB
433 - Automatically generated comments, e.g., those generated by CI systems,
434 may be deleted by authors and others when such comments are not the most
22265b35 435 recent results from that automated comment source.
d1890d04
QY
436 - After all comments and concerns are addressed, expect your patch
437 to be merged.
438
439- Watch out for questions on the mailing list. At this time there will
440 be a manual code review and further (longer) tests by various
441 community members.
442- Your submission is done once it is merged to the master branch.
443
9de103f0
QY
444Programming Languages, Tools and Libraries
445==========================================
446
447The core of FRR is written in C (gcc or clang supported) and makes
448use of GNU compiler extensions. A few non-essential scripts are
449implemented in Perl and Python. FRR requires the following tools
450to build distribution packages: automake, autoconf, texinfo, libtool and
451gawk and various libraries (i.e. libpam and libjson-c).
452
453If your contribution requires a new library or other tool, then please
454highlight this in your description of the change. Also make sure it’s
455supported by all FRR platform OSes or provide a way to build
456without the library (potentially without the new feature) on the other
457platforms.
458
459Documentation should be written in reStructuredText. Sphinx extensions may be
460utilized but pure ReST is preferred where possible. See
461:ref:`documentation`.
462
ca9dfee0
DL
463Use of C++
464----------
465
466While C++ is not accepted for core components of FRR, extensions, modules or
467other distinct components may want to use C++ and include FRR header files.
468There is no requirement on contributors to work to retain C++ compatibility,
469but fixes for C++ compatibility are welcome.
470
471This implies that the burden of work to keep C++ compatibility is placed with
472the people who need it, and they may provide it at their leisure to the extent
473it is useful to them. So, if only a subset of header files, or even parts of
474a header file are made available to C++, this is perfectly fine.
475
590a7368
QY
476Code Reviews
477============
478
479Code quality is paramount for any large program. Consequently we require
480reviews of all submitted patches by at least one person other than the
481submitter before the patch is merged.
482
483Because of the nature of the software, FRR's maintainer list (i.e. those with
484commit permissions) tends to contain employees / members of various
485organizations. In order to prevent conflicts of interest, we use an honor
486system in which submissions from an individual representing one company should
487be merged by someone unaffiliated with that company.
488
489Guidelines for code review
924947e4 490--------------------------
590a7368
QY
491
492- As a rule of thumb, the depth of the review should be proportional to the
493 scope and / or impact of the patch.
494
495- Anyone may review a patch.
496
497- When using GitHub reviews, marking "Approve" on a code review indicates
498 willingness to merge the PR.
499
500- For individuals with merge rights, marking "Changes requested" is equivalent
501 to a NAK.
502
503- For a PR you marked with "Changes requested", please respond to updates in a
504 timely manner to avoid impeding the flow of development.
505
7e678379
LB
506- Rejected or obsolete PRs are generally closed by the submitter based
507 on requests and/or agreement captured in a PR comment. The comment
508 may originate with a reviewer or document agreement reached on Slack,
509 the Development mailing list, or the weekly technical meeting.
510
8bc6e629
DS
511- Reviewers may ask for new automated testing if they feel that the
512 code change is large enough/significant enough to warrant such
513 a requirement.
514
70aa675d
DL
515For project members with merge permissions, the following patterns have
516emerged:
517
518- a PR with any reviews requesting changes may not be merged.
519
520- a PR with any negative CI result may not be merged.
521
522- an open "yellow" review mark ("review requested, but not done") should be
523 given some time (a few days up to weeks, depending on the size of the PR),
524 but is not a merge blocker.
525
526- a "textbubble" review mark ("review comments, but not positive/negative")
527 should be read through but is not a merge blocker.
528
529- non-trivial PRs are generally given some time (again depending on the size)
530 for people to mark an interest in reviewing. Trivial PRs may be merged
531 immediately when CI is green.
532
590a7368 533
b22ba015 534Coding Practices & Style
9de103f0 535========================
d1890d04
QY
536
537Commit messages
9de103f0 538---------------
d1890d04
QY
539
540Commit messages should be formatted in the same way as Linux kernel
b6820993 541commit messages. The format is roughly::
d1890d04
QY
542
543 dir: short summary
544
545 extended summary
546
b6820993
QY
547``dir`` should be the top level source directory under which the change was
548made. For example, a change in :file:`bgpd/rfapi` would be formatted as::
d1890d04 549
9de103f0 550 bgpd: short summary
d1890d04 551
b6820993
QY
552 ...
553
554The first line should be no longer than 50 characters. Subsequent lines should
555be wrapped to 72 characters.
d1890d04 556
7bd4560b
QY
557The purpose of commit messages is to briefly summarize what the commit is
558changing. Therefore, the extended summary portion should be in the form of an
559English paragraph. Brief examples of program output are acceptable but if
560present should be short (on the order of 10 lines) and clearly demonstrate what
561has changed. The goal should be that someone with only passing familiarity with
562the code in question can understand what is being changed.
563
564Commit messages consisting entirely of program output are *unacceptable*. These
565do not describe the behavior changed. For example, putting VTYSH output or the
566result of test runs as the sole content of commit messages is unacceptable.
567
b6820993
QY
568You must also sign off on your commit.
569
570.. seealso:: :ref:`signing-off`
571
7bd4560b 572
b6820993 573Source File Header
9de103f0 574------------------
d1890d04 575
b6820993
QY
576New files must have a copyright header (see :ref:`license-for-contributions`
577above) added to the file. The header should be:
d1890d04 578
b6820993 579.. code-block:: c
d1890d04
QY
580
581 /*
582 * Title/Function of file
583 * Copyright (C) YEAR Author’s Name
584 *
585 * This program is free software; you can redistribute it and/or modify it
586 * under the terms of the GNU General Public License as published by the Free
587 * Software Foundation; either version 2 of the License, or (at your option)
588 * any later version.
589 *
590 * This program is distributed in the hope that it will be useful, but WITHOUT
591 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
592 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
593 * more details.
594 *
595 * You should have received a copy of the GNU General Public License along
596 * with this program; see the file COPYING; if not, write to the Free Software
597 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
598 */
599
600 #include <zebra.h>
601
b6820993
QY
602Please copy-paste this header verbatim. In particular:
603
604- Do not replace "This program" with "FRR"
605- Do not change the address of the FSF
606
607Adding Copyright Claims to Existing Files
9de103f0 608-----------------------------------------
d1890d04 609
b6820993
QY
610When adding copyright claims for modifications to an existing file, please
611add a ``Portions:`` section as shown below. If this section already exists, add
612your new claim at the end of the list.
d1890d04 613
b6820993 614.. code-block:: c
d1890d04 615
b6820993
QY
616 /*
617 * Title/Function of file
618 * Copyright (C) YEAR Author’s Name
619 * Portions:
620 * Copyright (C) 2010 Entity A ....
621 * Copyright (C) 2016 Your name [optional brief change description]
622 * ...
623 */
d1890d04 624
08cffeb5
DL
625Defensive coding requirements
626-----------------------------
627
628In general, code submitted into FRR will be rejected if it uses unsafe
629programming practices. While there is no enforced overall ruleset, the
630following requirements have achieved consensus:
631
7533cad7 632- ``strcpy``, ``strcat`` and ``sprintf`` are unacceptable without exception.
08cffeb5
DL
633 Use ``strlcpy``, ``strlcat`` and ``snprintf`` instead. (Rationale: even if
634 you know the operation cannot overflow the buffer, a future code change may
635 inadvertedly introduce an overflow.)
636
637- buffer size arguments, particularly to ``strlcpy`` and ``snprintf``, must
638 use ``sizeof()`` whereever possible. Particularly, do not use a size
639 constant in these cases. (Rationale: changing a buffer to another size
640 constant may leave the write operations on a now-incorrect size limit.)
641
2787347d
QY
642- For stack allocated structs and arrays that should be zero initialized,
643 prefer initializer expressions over ``memset()`` wherever possible. This
644 helps prevent ``memset()`` calls being missed in branches, and eliminates the
645 error class of an incorrect ``size`` argument to ``memset()``.
646
647 For example, instead of:
648
649 .. code-block:: c
650
651 struct foo mystruct;
652 ...
653 memset(&mystruct, 0x00, sizeof(struct foo));
654
655 Prefer:
656
657 .. code-block:: c
658
659 struct foo mystruct = {};
660
661- Do not zero initialize stack allocated values that must be initialized with a
662 nonzero value in order to be used. This way the compiler and memory checking
663 tools can catch uninitialized value use that would otherwise be suppressed by
664 the (incorrect) zero initialization.
665
08cffeb5
DL
666Other than these specific rules, coding practices from the Linux kernel as
667well as CERT or MISRA C guidelines may provide useful input on safe C code.
668However, these rules are not applied as-is; some of them expressly collide
669with established practice.
670
161ed8a6
DL
671
672Container implementations
673^^^^^^^^^^^^^^^^^^^^^^^^^
674
675In particular to gain defensive coding benefits from better compiler type
676checks, there is a set of replacement container data structures to be found
677in :file:`lib/typesafe.h`. They're documented under :ref:`lists`.
678
679Unfortunately, the FRR codebase is quite large, and migrating existing code to
680use these new structures is a tedious and far-reaching process (even if it
681can be automated with coccinelle, the patches would touch whole swaths of code
682and create tons of merge conflicts for ongoing work.) Therefore, little
683existing code has been migrated.
684
685However, both **new code and refactors of existing code should use the new
686containers**. If there are any reasons this can't be done, please work to
687remove these reasons (e.g. by adding necessary features to the new containers)
688rather than falling back to the old code.
689
690In order of likelyhood of removal, these are the old containers:
691
692- :file:`nhrpd/list.*`, ``hlist_*`` ⇒ ``DECLARE_LIST``
693- :file:`nhrpd/list.*`, ``list_*`` ⇒ ``DECLARE_DLIST``
694- :file:`lib/skiplist.*`, ``skiplist_*`` ⇒ ``DECLARE_SKIPLIST``
695- :file:`lib/*_queue.h` (BSD), ``SLIST_*`` ⇒ ``DECLARE_LIST``
696- :file:`lib/*_queue.h` (BSD), ``LIST_*`` ⇒ ``DECLARE_DLIST``
697- :file:`lib/*_queue.h` (BSD), ``STAILQ_*`` ⇒ ``DECLARE_LIST``
698- :file:`lib/*_queue.h` (BSD), ``TAILQ_*`` ⇒ ``DECLARE_DLIST``
699- :file:`lib/hash.*`, ``hash_*`` ⇒ ``DECLARE_HASH``
700- :file:`lib/linklist.*`, ``list_*`` ⇒ ``DECLARE_DLIST``
701- open-coded linked lists ⇒ ``DECLARE_LIST``/``DECLARE_DLIST``
702
703
c964e511 704Code Formatting
9de103f0 705---------------
d1890d04 706
6f7a9254
QY
707C Code
708^^^^^^
709
710For C code, FRR uses Linux kernel style except where noted below. Code which
711does not comply with these style guidelines will not be accepted.
d1890d04 712
281ba953
QY
713The project provides multiple tools to allow you to correctly style your code
714as painlessly as possible, primarily built around ``clang-format``.
715
716clang-format
717 In the project root there is a :file:`.clang-format` configuration file
718 which can be used with the ``clang-format`` source formatter tool from the
719 LLVM project. Most of the time, this is the easiest and smartest tool to
720 use. It can be run in a variety of ways. If you point it at a C source file
721 or directory of source files, it will format all of them. In the LLVM source
722 tree there are scripts that allow you to integrate it with ``git``, ``vim``
723 and ``emacs``, and there are third-party plugins for other editors. The
724 ``git`` integration is particularly useful; suppose you have some changes in
725 your git index. Then, with the integration installed, you can do the
726 following:
727
728 ::
729
730 git clang-format
731
732 This will format *only* the changes present in your index. If you have just
733 made a few commits and would like to correctly style only the changes made
734 in those commits, you can use the following syntax:
735
736 ::
737
738 git clang-format HEAD~X
739
740 Where X is one more than the number of commits back from the tip of your
741 branch you would like ``clang-format`` to look at (similar to specifying the
742 target for a rebase).
743
744 The ``vim`` plugin is particularly useful. It allows you to select lines in
745 visual line mode and press a key binding to invoke ``clang-format`` on only
746 those lines.
747
748 When using ``clang-format``, it is recommended to use the latest version.
749 Each consecutive version generally has better handling of various edge
750 cases. You may notice on occasion that two consecutive runs of
751 ``clang-format`` over the same code may result in changes being made on the
752 second run. This is an unfortunate artifact of the tool. Please check with
753 the kernel style guide if in doubt.
754
755 One stylistic problem with the FRR codebase is the use of ``DEFUN`` macros
756 for defining CLI commands. ``clang-format`` will happily format these macro
757 invocations, but the result is often unsightly and difficult to read.
758 Consequently, FRR takes a more relaxed position with how these are
759 formatted. In general you should lean towards using the style exemplified in
760 the section on :ref:`command-line-interface`. Because ``clang-format``
761 mangles this style, there is a Python script named ``tools/indent.py`` that
762 wraps ``clang-format`` and handles ``DEFUN`` macros as well as some other
763 edge cases specific to FRR. If you are submitting a new file, it is
764 recommended to run that script over the new file, preferably after ensuring
765 that the latest stable release of ``clang-format`` is in your ``PATH``.
766
767 Documentation on ``clang-format`` and its various integrations is maintained
768 on the LLVM website.
769
770 https://clang.llvm.org/docs/ClangFormat.html
771
772checkpatch.sh
773 In the Linux kernel source tree there is a Perl script used to check
774 incoming patches for style errors. FRR uses an adapted version of this
775 script for the same purpose. It can be found at
2780ae0c 776 :file:`tools/checkpatch.sh`. This script takes a git-formatted diff or
281ba953
QY
777 patch file, applies it to a clean FRR tree, and inspects the result to catch
778 potential style errors. Running this script on your patches before
779 submission is highly recommended. The CI system runs this script as well and
780 will comment on the PR with the results if style errors are found.
781
b6820993 782 It is run like this::
281ba953 783
b6820993 784 ./checkpatch.sh <patch> <tree>
281ba953
QY
785
786 Reports are generated on ``stderr`` and the exit code indicates whether
787 issues were found (2, 1) or not (0).
788
789 Where ``<patch>`` is the path to the diff or patch file and ``<tree>`` is
790 the path to your FRR source tree. The tree should be on the branch that you
791 intend to submit the patch against. The script will make a best-effort
792 attempt to save the state of your working tree and index before applying the
793 patch, and to restore it when it is done, but it is still recommended that
794 you have a clean working tree as the script does perform a hard reset on
795 your tree during its run.
796
797 The script reports two classes of issues, namely WARNINGs and ERRORs. Please
798 pay attention to both of them. The script will generally report WARNINGs
799 where it cannot be 100% sure that a particular issue is real. In most cases
800 WARNINGs indicate an issue that needs to be fixed. Sometimes the script will
801 report false positives; these will be handled in code review on a
802 case-by-case basis. Since the script only looks at changed lines,
803 occasionally changing one part of a line can cause the script to report a
804 style issue already present on that line that is unrelated to the change.
805 When convenient it is preferred that these be cleaned up inline, but this is
806 not required.
807
115e70a1
PZ
808 In general, a developer should heed the information reported by checkpatch.
809 However, some flexibility is needed for cases where human judgement yields
810 better clarity than the script. Accordingly, it may be appropriate to
811 ignore some checkpatch.sh warnings per discussion among the submitter(s)
812 and reviewer(s) of a change. Misreporting of errors by the script is
d3c2e316
QY
813 possible. When this occurs, the exception should be handled either by
814 patching checkpatch to correct the false error report, or by documenting the
815 exception in this document under :ref:`style-exceptions`. If the incorrect
816 report is likely to appear again, a checkpatch update is preferred.
115e70a1 817
281ba953
QY
818 If the script finds one or more WARNINGs it will exit with 1. If it finds
819 one or more ERRORs it will exit with 2.
820
821
822Please remember that while FRR provides these tools for your convenience,
823responsibility for properly formatting your code ultimately lies on the
824shoulders of the submitter. As such, it is recommended to double-check the
825results of these tools to avoid delays in merging your submission.
d1890d04 826
115e70a1
PZ
827In some cases, these tools modify or flag the format in ways that go beyond or
828even conflict [#tool_style_conflicts]_ with the canonical documented Linux
829kernel style. In these cases, the Linux kernel style takes priority;
830non-canonical issues flagged by the tools are not compulsory but rather are
831opportunities for discussion among the submitter(s) and reviewer(s) of a change.
832
d1890d04
QY
833**Whitespace changes in untouched parts of the code are not acceptable
834in patches that change actual code.** To change/fix formatting issues,
835please create a separate patch that only does formatting changes and
836nothing else.
837
d1890d04
QY
838Kernel and BSD styles are documented externally:
839
840- https://www.kernel.org/doc/html/latest/process/coding-style.html
841- http://man.openbsd.org/style
842
843For GNU coding style, use ``indent`` with the following invocation:
844
845::
846
847 indent -nut -nfc1 file_for_submission.c
848
28ac5a03
QY
849
850Historically, FRR used fixed-width integral types that do not exist in any
851standard but were defined by most platforms at some point. Officially these
852types are not guaranteed to exist. Therefore, please use the fixed-width
853integral types introduced in the C99 standard when contributing new code to
854FRR. If you need to convert a large amount of code to use the correct types,
855there is a shell script in :file:`tools/convert-fixedwidth.sh` that will do the
856necessary replacements.
857
858+-----------+--------------------------+
859| Incorrect | Correct |
860+===========+==========================+
861| u_int8_t | uint8_t |
862+-----------+--------------------------+
863| u_int16_t | uint16_t |
864+-----------+--------------------------+
865| u_int32_t | uint32_t |
866+-----------+--------------------------+
867| u_int64_t | uint64_t |
868+-----------+--------------------------+
869| u_char | uint8_t or unsigned char |
870+-----------+--------------------------+
871| u_short | unsigned short |
872+-----------+--------------------------+
873| u_int | unsigned int |
874+-----------+--------------------------+
875| u_long | unsigned long |
876+-----------+--------------------------+
877
d3c2e316
QY
878.. _style-exceptions:
879
d1890d04 880Exceptions
3656e87b 881""""""""""
d1890d04
QY
882
883FRR project code comes from a variety of sources, so there are some
884stylistic exceptions in place. They are organized here by branch.
885
3656e87b 886For ``master``:
d1890d04
QY
887
888BSD coding style applies to:
889
890- ``ldpd/``
891
892``babeld`` uses, approximately, the following style:
893
894- K&R style braces
895- Indents are 4 spaces
896- Function return types are on their own line
897
3656e87b 898For ``stable/3.0`` and ``stable/2.0``:
d1890d04
QY
899
900GNU coding style apply to the following parts:
901
902- ``lib/``
903- ``zebra/``
904- ``bgpd/``
905- ``ospfd/``
906- ``ospf6d/``
907- ``isisd/``
908- ``ripd/``
909- ``ripngd/``
910- ``vtysh/``
911
912BSD coding style applies to:
913
914- ``ldpd/``
915
3656e87b
QY
916
917Python Code
918^^^^^^^^^^^
919
920Format all Python code with `black <https://github.com/psf/black>`_.
921
922In a line::
923
924 python3 -m black <file.py>
925
926Run this on any Python files you modify before committing.
927
928FRR's Python code has been formatted with black version 19.10b.
929
930
6f7a9254
QY
931YANG
932^^^^
933
934FRR uses YANG to define data models for its northbound interface. YANG models
935should follow conventions used by the IETF standard models. From a practical
936standpoint, this corresponds to the output produced by the ``yanglint`` tool
937included in the ``libyang`` project, which is used by FRR to parse and validate
938YANG models. You should run the following command on all YANG documents you
939write:
940
941.. code-block:: console
942
943 yanglint -f yang <model>
944
945The output of this command should be identical to the input file. The sole
946exception to this is comments. ``yanglint`` does not support comments and will
947strip them from its output. You may include comments in your YANG documents,
948but they should be indented appropriately (use spaces). Where possible,
949comments should be eschewed in favor of a suitable ``description`` statement.
950
951In short, a diff between your input file and the output of ``yanglint`` should
952either be empty or contain only comments.
d3c2e316
QY
953
954Specific Exceptions
955^^^^^^^^^^^^^^^^^^^
956
957Most of the time checkpatch errors should be corrected. Occasionally as a group
958maintainers will decide to ignore certain stylistic issues. Usually this is
959because correcting the issue is not possible without large unrelated code
960changes. When an exception is made, if it is unlikely to show up again and
961doesn't warrant an update to checkpatch, it is documented here.
962
963+------------------------------------------+---------------------------------------------------------------+
964| Issue | Ignore Reason |
965+==========================================+===============================================================+
966| DEFPY_HIDDEN, DEFPY_ATTR: complex macros | DEF* macros cannot be wrapped in parentheses without updating |
967| should be wrapped in parentheses | all usages of the macro, which would be highly disruptive. |
968+------------------------------------------+---------------------------------------------------------------+
969
b6660a65
DL
970Types of configurables
971----------------------
972
973.. note::
974
975 This entire section essentially just argues to not make configuration
976 unnecessarily involved for the user. Rather than rules, this is more of
977 a list of conclusions intended to help make FRR usable for operators.
978
979
980Almost every feature FRR has comes with its own set of switches and options.
981There are several stages at which configuration can be applied. In order of
982preference, these are:
983
984- at configuration/runtime, through YANG.
985
986 This is the preferred way for all FRR knobs. Not all daemons and features
987 are fully YANGified yet, so in some cases new features cannot rely on a
424117e5
DL
988 YANG interface. If a daemon already implements a YANG interface (even
989 partial), new CLI options must be implemented through a YANG model.
990
991 .. warning::
992
993 Unlike everything else in this section being guidelines with some slack,
994 implementing and using a YANG interface for new CLI options in (even
995 partially!) YANGified daemons is a hard requirement.
996
b6660a65
DL
997
998- at configuration/runtime, through the CLI.
999
1000 The "good old" way for all regular configuration. More involved for users
1001 to automate *correctly* than YANG.
1002
1003- at startup, by loading additional modules.
1004
1005 If a feature introduces a dependency on additional libraries (e.g. libsnmp,
1006 rtrlib, etc.), this is the best way to encapsulate the dependency. Having
1007 a separate module allows the distribution to create a separate package
1008 with the extra dependency, so FRR can still be installed without pulling
1009 everything in.
1010
1011 A module may also be appropriate if a feature is large and reasonably well
1012 isolated. Reducing the amount of running the code is a security benefit,
1013 so even if there are no new external dependencies, modules can be useful.
1014
1015 While modules cannot currently be loaded at runtime, this is a tradeoff
1016 decision that was made to allow modules to change/extend code that is very
1017 hard to (re)adjust at runtime. If there is a case for runtime (un)loading
1018 of modules, this tradeoff can absolutely be reevaluated.
1019
1020- at startup, with command line options.
1021
1022 This interface is only appropriate for options that have an effect very
1023 early in FRR startup, i.e. before configuration is loaded. Anything that
1024 affects configuration load itself should be here, as well as options
1025 changing the environment FRR runs in.
1026
1027 If a tunable can be changed at runtime, a command line option is only
1028 acceptable if the configured value has an effect before configuration is
1029 loaded (e.g. zebra reads routes from the kernel before loading config, so
1030 the netlink buffer size is an appropriate command line option.)
1031
1032- at compile time, with ``./configure`` options.
1033
1034 This is the absolute last preference for tunables, since the distribution
1035 needs to make the decision for the user and/or the user needs to rebuild
1036 FRR in order to change the option.
1037
1038 "Good" configure options do one of three things:
1039
1040 - set distribution-specific parameters, most prominently all the path
1041 options. File system layout is a distribution/packaging choice, so the
1042 user would hopefully never need to adjust these.
1043
1044 - changing toolchain behavior, e.g. instrumentation, warnings,
1045 optimizations and sanitizers.
1046
1047 - enabling/disabling parts of the build, especially if they need
1048 additional dependencies. Being able to build only parts of FRR, or
1049 without some library, is useful. **The only effect these options should
1050 have is adding or removing files from the build result.** If a knob
1051 in this category causes the same binary to exist in different variants,
1052 it is likely implemented incorrectly!
1053
1054 .. note::
1055
1056 This last guideline is currently ignored by several configure options.
1057 ``vtysh`` in general depends on the entire list of enabled daemons,
1058 and options like ``--enable-bgp-vnc`` and ``--enable-ospfapi`` change
1059 daemons internally. Consider this more of an "ideal" than a "rule".
1060
1061
1062Whenever adding new knobs, please try reasonably hard to go up as far as
1063possible on the above list. Especially ``./configure`` flags are often enough
1064the "easy way out" but should be avoided when at all possible. To a lesser
1065degree, the same applies to command line options.
1066
1067
d1890d04 1068Compile-time conditional code
9de103f0 1069-----------------------------
d1890d04
QY
1070
1071Many users access FRR via binary packages from 3rd party sources;
1072compile-time code puts inclusion/exclusion in the hands of the package
1073maintainer. Please think very carefully before making code conditional
1074at compile time, as it increases regression testing, maintenance
1075burdens, and user confusion. In particular, please avoid gratuitous
1076``--enable-…`` switches to the configure script - in general, code
1077should be of high quality and in working condition, or it shouldn’t be
1078in FRR at all.
1079
1080When code must be compile-time conditional, try have the compiler make
1081it conditional rather than the C pre-processor so that it will still be
1082checked by the compiler, even if disabled. For example,
1083
1084::
1085
1086 if (SOME_SYMBOL)
1087 frobnicate();
1088
1089is preferred to
1090
1091::
1092
1093 #ifdef SOME_SYMBOL
1094 frobnicate ();
1095 #endif /* SOME_SYMBOL */
1096
b6820993
QY
1097Note that the former approach requires ensuring that ``SOME_SYMBOL`` will be
1098defined (watch your ``AC_DEFINE``\ s).
d1890d04
QY
1099
1100Debug-guards in code
9de103f0 1101--------------------
d1890d04 1102
b6820993
QY
1103Debugging statements are an important methodology to allow developers to fix
1104issues found in the code after it has been released. The caveat here is that
1105the developer must remember that people will be using the code at scale and in
1106ways that can be unexpected for the original implementor. As such debugs
1107**MUST** be guarded in such a way that they can be turned off. FRR has the
1108ability to turn on/off debugs from the CLI and it is expected that the
1109developer will use this convention to allow control of their debugs.
d1890d04 1110
81047bc5
DL
1111Custom syntax-like block macros
1112-------------------------------
1113
1114FRR uses some macros that behave like the ``for`` or ``if`` C keywords. These
1115macros follow these patterns:
1116
1117- loop-style macros are named ``frr_each_*`` (and ``frr_each``)
1118- single run macros are named ``frr_with_*``
1119- to avoid confusion, ``frr_with_*`` macros must always use a ``{ ... }``
1120 block even if the block only contains one statement. The ``frr_each``
1121 constructs are assumed to be well-known enough to use normal ``for`` rules.
1122- ``break``, ``return`` and ``goto`` all work correctly. For loop-style
1123 macros, ``continue`` works correctly too.
1124
1125Both the ``each`` and ``with`` keywords are inspired by other (more
1126higher-level) programming languages that provide these constructs.
1127
1128There are also some older iteration macros, e.g. ``ALL_LIST_ELEMENTS`` and
1129``FOREACH_AFI_SAFI``. These macros in some cases do **not** fulfill the above
1130pattern (e.g. ``break`` does not work in ``FOREACH_AFI_SAFI`` because it
1131expands to 2 nested loops.)
1132
9e001286
QY
1133Static Analysis and Sanitizers
1134------------------------------
81af0317
DL
1135Clang/LLVM and GCC come with a variety of tools that can be used to help find
1136bugs in FRR.
9e001286
QY
1137
1138clang-analyze
1139 This is a static analyzer that scans the source code looking for patterns
1140 that are likely to be bugs. The tool is run automatically on pull requests
1141 as part of CI and new static analysis warnings will be placed in the CI
1142 results. FRR aims for absolutely zero static analysis errors. While the
1143 project is not quite there, code that introduces new static analysis errors
1144 is very unlikely to be merged.
1145
1146AddressSanitizer
1147 This is an excellent tool that provides runtime instrumentation for
1148 detecting memory errors. As part of CI FRR is built with this
1149 instrumentation and run through a series of tests to look for any results.
1150 Testing your own code with this tool before submission is encouraged. You
1151 can enable it by passing::
d5403d4f 1152
9e001286
QY
1153 --enable-address-sanitizer
1154
1155 to ``configure``.
1156
1157ThreadSanitizer
1158 Similar to AddressSanitizer, this tool provides runtime instrumentation for
1159 detecting data races. If you are working on or around multithreaded code,
1160 extensive testing with this instrumtation enabled is *highly* recommended.
1161 You can enable it by passing::
d5403d4f 1162
9e001286
QY
1163 --enable-thread-sanitizer
1164
1165 to ``configure``.
1166
1167MemorySanitizer
1168 Similar to AddressSanitizer, this tool provides runtime instrumentation for
1169 detecting use of uninitialized heap memory. Testing your own code with this
1170 tool before submission is encouraged. You can enable it by passing::
d5403d4f 1171
9e001286
QY
1172 --enable-memory-sanitizer
1173
1174 to ``configure``.
1175
1176All of the above tools are available in the Clang/LLVM toolchain since 3.4.
1177AddressSanitizer and ThreadSanitizer are available in recent versions of GCC,
1178but are no longer actively maintained. MemorySanitizer is not available in GCC.
1179
81af0317
DL
1180.. note::
1181
1182 The different Sanitizers are mostly incompatible with each other. Please
1183 refer to GCC/LLVM documentation for details.
1184
f62de63c
DL
1185frr-format plugin
1186 This is a GCC plugin provided with FRR that does extended type checks for
1187 ``%pFX``-style printfrr extensions. To use this plugin,
1188
1189 1. install GCC plugin development files, e.g.::
1190
1191 apt-get install gcc-10-plugin-dev
1192
1193 2. **before** running ``configure``, compile the plugin with::
1194
1195 make -C tools/gcc-plugins CXX=g++-10
1196
1197 (Edit the GCC version to what you're using, it should work for GCC 9 or
1198 newer.)
1199
1200 After this, the plugin should be automatically picked up by ``configure``.
1201 The plugin does not change very frequently, so you can keep it around across
1202 work on different FRR branches. After a ``git clean -x``, the ``make`` line
1203 will need to be run again. You can also add ``--with-frr-format`` to the
1204 ``configure`` line to make sure the plugin is used, otherwise if something
1205 is not set up correctly it might be silently ignored.
1206
1207 .. warning::
1208
1209 Do **not** enable this plugin for package/release builds. It is intended
1210 for developer/debug builds only. Since it modifies the compiler, it may
1211 cause silent corruption of the executable files.
1212
1213 Using the plugin also changes the string for ``PRI[udx]64`` from the
1214 system value to ``%L[udx]`` (normally ``%ll[udx]`` or ``%l[udx]``.)
1215
9e001286
QY
1216Additionally, the FRR codebase is regularly scanned with Coverity.
1217Unfortunately Coverity does not have the ability to handle scanning pull
1218requests, but after code is merged it will send an email notifying project
1219members with Coverity access of newly introduced defects.
1220
81af0317
DL
1221Executing non-installed dynamic binaries
1222----------------------------------------
1223
1224Since FRR uses the GNU autotools build system, it inherits its shortcomings.
1225To execute a binary directly from the build tree under a wrapper like
1226`valgrind`, `gdb` or `strace`, use::
1227
1228 ./libtool --mode=execute valgrind [--valgrind-opts] zebra/zebra [--zebra-opts]
1229
1230While replacing valgrind/zebra as needed. The `libtool` script is found in
1231the root of the build directory after `./configure` has completed. Its purpose
1232is to correctly set up `LD_LIBRARY_PATH` so that libraries from the build tree
1233are used. (On some systems, `libtool` is also available from PATH, but this is
1234not always the case.)
1235
d1890d04 1236CLI changes
9de103f0 1237-----------
d1890d04 1238
b6820993
QY
1239CLI's are a complicated ugly beast. Additions or changes to the CLI should use
1240a DEFUN to encapsulate one setting as much as is possible. Additionally as new
1241DEFUN's are added to the system, documentation should be provided for the new
1242commands.
d1890d04
QY
1243
1244Backwards Compatibility
9de103f0 1245-----------------------
d1890d04 1246
b6820993
QY
1247As a general principle, changes to CLI and code in the lib/ directory should be
1248made in a backwards compatible fashion. This means that changes that are purely
1249stylistic in nature should be avoided, e.g., renaming an existing macro or
1250library function name without any functional change. When adding new parameters
1251to common functions, it is also good to consider if this too should be done in
1252a backward compatible fashion, e.g., by preserving the old form in addition to
d1890d04
QY
1253adding the new form.
1254
b6820993
QY
1255This is not to say that minor or even major functional changes to CLI and
1256common code should be avoided, but rather that the benefit gained from a change
1257should be weighed against the added cost/complexity to existing code. Also,
1258that when making such changes, it is good to preserve compatibility when
1259possible to do so without introducing maintenance overhead/cost. It is also
1260important to keep in mind, existing code includes code that may reside in
1261private repositories (and is yet to be submitted) or code that has yet to be
1262migrated from Quagga to FRR.
110bb121 1263
b6820993
QY
1264That said, compatibility measures can (and should) be removed when either:
1265
1266- they become a significant burden, e.g. when data structures change and the
1267 compatibility measure would need a complex adaptation layer or becomes
1268 flat-out impossible
1269- some measure of time (dependent on the specific case) has passed, so that
1270 the compatibility grace period is considered expired.
1271
e12ea4bb
QY
1272For CLI commands, the deprecation period is 1 year.
1273
b6820993
QY
1274In all cases, compatibility pieces should be marked with compiler/preprocessor
1275annotations to print warnings at compile time, pointing to the appropriate
1276update path. A ``-Werror`` build should fail if compatibility bits are used. To
1277avoid compilation issues in released code, such compiler/preprocessor
1278annotations must be ignored non-development branches. For example:
1279
1280.. code-block:: c
1281
e60dd6ca 1282 #if CONFDATE > 20180403
b6820993
QY
1283 CPP_NOTICE("Use of <XYZ> is deprecated, please use <ABC>")
1284 #endif
d1890d04 1285
cab3f811
LB
1286Preferably, the shell script :file:`tools/fixup-deprecated.py` will be
1287updated along with making non-backwards compatible code changes, or an
1288alternate script should be introduced, to update the code to match the
1289change. When the script is updated, there is no need to preserve the
1290deprecated code. Note that this does not apply to user interface
1291changes, just internal code, macros and libraries.
1292
d1890d04 1293Miscellaneous
9de103f0 1294-------------
d1890d04 1295
b6820993
QY
1296When in doubt, follow the guidelines in the Linux kernel style guide, or ask on
1297the development mailing list / public Slack instance.
9de103f0 1298
e9f2bc24
QY
1299JSON Output
1300^^^^^^^^^^^
1301
47563324
QY
1302* All JSON keys are to be camelCased, with no spaces
1303* Commands which output JSON should produce ``{}`` if they have nothing to
1304 display
e9f2bc24 1305
7d68dd44
MS
1306Use of const
1307^^^^^^^^^^^^
1308
1309Please consider using ``const`` when possible: it's a useful hint to
1310callers about the limits to side-effects from your apis, and it makes
1311it possible to use your apis in paths that involve ``const``
1312objects. If you encounter existing apis that *could* be ``const``,
1313consider including changes in your own pull-request.
1314
e5af0fc8
DL
1315Help with specific warnings
1316^^^^^^^^^^^^^^^^^^^^^^^^^^^
1317
1318FRR's configure script enables a whole batch of extra warnings, some of which
1319may not be obvious in how to fix. Here are some notes on specific warnings:
1320
1321* ``-Wstrict-prototypes``: you probably just forgot the ``void`` in a function
1322 declaration with no parameters, i.e. ``static void foo() {...}`` rather than
1323 ``static void foo(void) {...}``.
1324
1325 Without the ``void``, in C, it's a function with *unspecified* parameters
1326 (and varargs calling convention.) This is a notable difference to C++, where
1327 the ``void`` is optional and an empty parameter list means no parameters.
1328
f62de63c
DL
1329* ``"strict match required"`` from the frr-format plugin: check if you are
1330 using a cast in a printf parameter list. The frr-format plugin cannot
1331 access correct full type information for casts like
1332 ``printfrr(..., (uint64_t)something, ...)`` and will print incorrect
1333 warnings particularly if ``uint64_t``, ``size_t`` or ``ptrdiff_t`` are
1334 involved. The problem is *not* triggered with a variable or function return
1335 value of the exact same type (without a cast).
1336
1337 Since these cases are very rare, community consensus is to just work around
1338 the warning even though the code might be correct. If you are running into
1339 this, your options are:
1340
1341 1. try to avoid the cast altogether, maybe using a different printf format
1342 specifier (e.g. ``%lu`` instead of ``%zu`` or ``PRIu64``).
1343 2. fix the type(s) of the function/variable/struct member being printed
1344 3. create a temporary variable with the value and print that without a cast
1345 (this is the last resort and was not necessary anywhere so far.)
1346
9de103f0
QY
1347
1348.. _documentation:
1349
1350Documentation
1351=============
1352
1353FRR uses Sphinx+RST as its documentation system. The document you are currently
1354reading was generated by Sphinx from RST source in
1355:file:`doc/developer/workflow.rst`. The documentation is structured as follows:
1356
d5403d4f
QY
1357+-----------------------+-------------------------------------------+
1358| Directory | Contents |
1359+=======================+===========================================+
1360| :file:`doc/user` | User documentation; configuration guides; |
1361| | protocol overviews |
1362+-----------------------+-------------------------------------------+
1363| :file:`doc/developer` | Developer's documentation; API specs; |
1364| | datastructures; architecture overviews; |
1365| | project management procedure |
1366+-----------------------+-------------------------------------------+
1367| :file:`doc/manpages` | Source for manpages |
1368+-----------------------+-------------------------------------------+
1369| :file:`doc/figures` | Images and diagrams |
1370+-----------------------+-------------------------------------------+
1371| :file:`doc/extra` | Miscellaneous Sphinx extensions, scripts, |
1372| | customizations, etc. |
1373+-----------------------+-------------------------------------------+
1374
1375Each of these directories, with the exception of :file:`doc/figures` and
1376:file:`doc/extra`, contains a Sphinx-generated Makefile and configuration
1377script :file:`conf.py` used to set various document parameters. The makefile
1378can be used for a variety of targets; invoke `make help` in any of these
1379directories for a listing of available output formats. For convenience, there
1380is a top-level :file:`Makefile.am` that has targets for PDF and HTML
1381documentation for both developer and user documentation, respectively. That
1382makefile is also responsible for building manual pages packed with distribution
1383builds.
9de103f0
QY
1384
1385Indent and styling should follow existing conventions:
1386
1387- 3 spaces for indents under directives
1388- Cross references may contain only lowercase alphanumeric characters and
1389 hyphens ('-')
1390- Lines wrapped to 80 characters where possible
1391
1392Characters for header levels should follow Python documentation guide:
1393
1394- ``#`` with overline, for parts
1395- ``*`` with overline, for chapters
1396- ``=``, for sections
1397- ``-``, for subsections
1398- ``^``, for subsubsections
1399- ``"``, for paragraphs
1400
1401After you have made your changes, please make sure that you can invoke
1402``make latexpdf`` and ``make html`` with no warnings.
1403
1404The documentation is currently incomplete and needs love. If you find a broken
1405cross-reference, figure, dead hyperlink, style issue or any other nastiness we
1406gladly accept documentation patches.
1407
c91e9b8f
QY
1408To build the docs, please ensure you have installed a recent version of
1409`Sphinx <http://www.sphinx-doc.org/en/stable/install.html>`_. If you want to
1410build LaTeX or PDF docs, you will also need a full LaTeX distribution
1411installed.
1412
9de103f0
QY
1413Code
1414----
1415
1416FRR is a large and complex software project developed by many different people
1417over a long period of time. Without adequate documentation, it can be
1418exceedingly difficult to understand code segments, APIs and other interfaces.
1419In the interest of keeping the project healthy and maintainable, you should
1420make every effort to document your code so that other people can understand
1421what it does without needing to closely read the code itself.
1422
1423Some specific guidelines that contributors should follow are:
1424
1425- Functions exposed in header files should have descriptive comments above
1426 their signatures in the header file. At a minimum, a function comment should
1427 contain information about the return value, parameters, and a general summary
1428 of the function's purpose. Documentation on parameter values can be omitted
1429 if it is (very) obvious what they are used for.
1430
1431 Function comments must follow the style for multiline comments laid out in
1432 the kernel style guide.
1433
1434 Example:
1435
1436 .. code-block:: c
1437
1438 /*
1439 * Determines whether or not a string is cool.
1440 *
b6820993
QY
1441 * text
1442 * the string to check for coolness
1443 *
1444 * is_clccfc
1445 * whether capslock is cruise control for cool
1446 *
1447 * Returns:
1448 * 7 if the text is cool, 0 otherwise
9de103f0
QY
1449 */
1450 int check_coolness(const char *text, bool is_clccfc);
1451
b6820993
QY
1452 Function comments should make it clear what parameters and return values are
1453 used for.
9de103f0
QY
1454
1455- Static functions should have descriptive comments in the same form as above
1456 if what they do is not immediately obvious. Use good engineering judgement
1457 when deciding whether a comment is necessary. If you are unsure, document
1458 your code.
1459- Global variables, static or not, should have a comment describing their use.
1460- **For new code in lib/, these guidelines are hard requirements.**
1461
1462If you make significant changes to portions of the codebase covered in the
1463Developer's Manual, add a major subsystem or feature, or gain arcane mastery of
1464some undocumented or poorly documented part of the codebase, please document
1465your work so others can benefit. If you add a major feature or introduce a new
1466API, please document the architecture and API to the best of your abilities in
1467the Developer's Manual, using good judgement when choosing where to place it.
1468
1469Finally, if you come across some code that is undocumented and feel like
1470going above and beyond, document it! We absolutely appreciate and accept
1471patches that document previously undocumented code.
1472
1473User
1474----
1475
1476If you are contributing code that adds significant user-visible functionality
1477please document how to use it in :file:`doc/user`. Use good judgement when
1478choosing where to place documentation. For example, instructions on how to use
1479your implementation of a new BGP draft should go in the BGP chapter instead of
1480being its own chapter. If you are adding a new protocol daemon, please create a
1481new chapter.
1482
d5403d4f
QY
1483FRR Specific Markup
1484-------------------
1485
1486FRR has some customizations applied to the Sphinx markup that go a long way
1487towards making documentation easier to use, write and maintain.
1488
1489CLI Commands
1490^^^^^^^^^^^^
1491
e1ac6ff4
QY
1492When documenting CLI please use the ``.. clicmd::`` directive. This directive
1493will format the command and generate index entries automatically. For example,
1494the command :clicmd:`show pony` would be documented as follows:
9de103f0
QY
1495
1496.. code-block:: rest
1497
9de103f0
QY
1498 .. clicmd:: show pony
1499
1500 Prints an ASCII pony. Example output:::
1501
1502 >>\.
1503 /_ )`.
1504 / _)`^)`. _.---. _
1505 (_,' \ `^-)"" `.\
1506 | | \
1507 \ / |
1508 / \ /.___.'\ (\ (_
1509 < ,"|| \ |`. \`-'
1510 \\ () )| )/
1511 hjw |_>|> /_] //
1512 /_] /_]
1513
e1ac6ff4 1514
9de103f0
QY
1515When documented this way, CLI commands can be cross referenced with the
1516``:clicmd:`` inline markup like so:
1517
1518.. code-block:: rest
1519
1520 :clicmd:`show pony`
1521
1522This is very helpful for users who want to quickly remind themselves what a
1523particular command does.
1524
e1ac6ff4
QY
1525When documenting a cli that has a ``no`` form, please do not include the ``no``
1526form. I.e. ``no show pony`` would not be documented anywhere. Since most
1527commands have ``no`` forms, users should be able to infer these or get help
1528from vtysh's completions.
1529
1530When documenting commands that have lots of possible variants, just document
1531the single command in summary rather than enumerating each possible variant.
1532E.g. for ``show pony [foo|bar]``, do not:
1533
1534.. code-block:: rest
1535
1536 .. clicmd:: show pony
1537 .. clicmd:: show pony foo
1538 .. clicmd:: show pony bar
1539
1540Do:
1541
1542.. code-block:: rest
1543
1544 .. clicmd:: show pony [foo|bar]
1545
41cb383f 1546
d5403d4f
QY
1547Configuration Snippets
1548^^^^^^^^^^^^^^^^^^^^^^
1549
1550When putting blocks of example configuration please use the
1551``.. code-block::`` directive and specify ``frr`` as the highlighting language,
1552as in the following example. This will tell Sphinx to use a custom Pygments
1553lexer to highlight FRR configuration syntax.
1554
1555.. code-block:: rest
1556
1557 .. code-block:: frr
1558
1559 !
1560 ! Example configuration file.
1561 !
1562 log file /tmp/log.log
1563 service integrated-vtysh-config
1564 !
1565 ip route 1.2.3.0/24 reject
1566 ipv6 route de:ea:db:ee:ff::/64 reject
1567 !
1568
1569
9de103f0
QY
1570.. _GitHub: https://github.com/frrouting/frr
1571.. _GitHub issues: https://github.com/frrouting/frr/issues
115e70a1
PZ
1572
1573.. rubric:: Footnotes
1574
1575.. [#tool_style_conflicts] For example, lines over 80 characters are allowed
1576 for text strings to make it possible to search the code for them: please
1577 see `Linux kernel style (breaking long lines and strings) <https://www.kernel.org/doc/html/v4.10/process/coding-style.html#breaking-long-lines-and-strings>`_
1578 and `Issue #1794 <https://github.com/FRRouting/frr/issues/1794>`_.