html_src/code-submission.html
author Josh Pelkey <jpelkey@gatech.edu>
Wed May 25 18:37:45 2011 -0400 (12 months ago)
changeset 295 f277803f2618
parent 220 db462de61401
permissions -rw-r--r--
update main page roadmap
     1 <h1>How to submit new code to ns-3</h1>
     2 
     3 <h2>Introduction</h2>
     4 
     5 <p>This document summarizes the steps to take to prepare code for submission
     6 to ns-3 as a new feature, such as a new feature of an existing simulation
     7 model, or a new model altogether.  If you are interested in submitting a 
     8 bugfix to an existing ns-3 feature, or new documentation, see the companion 
     9 sections <a href="contributing.html#bugs">How to submit a bug report</a> or 
    10 <a href="contributing.html#documentation">How to submit documentation</a> to ns-3.</p>
    11 
    12 <p>We actively encourage submission of new features to ns-3.  
    13 Independent submissions are essential for open source projects, and
    14 you will also be credited as an author of future versions of ns-3.  
    15 However, please keep in mind that there is already a large burden on the 
    16 ns-3 maintainers to manage the flow of incoming contributions and maintain 
    17 new and existing code.  The goal of this document is thus to outline 
    18 how you can help to minimize this burden and thus minimize the average 
    19 time-to-merge of your code. Making sure that each code submission fulfills 
    20 as many items as possible in the following checklist is the best way to 
    21 ensure quick merging of your code. 
    22 </p>
    23 
    24 <p>
    25 In brief, we can summarize the guidelines as follows, and expand on them
    26 in detail below:
    27 <ul>
    28 <li>be licensed appropriately
    29 <li>follow the ns-3 
    30 <a href="http://www.nsnam.org/codingstyle.html">coding style and
    31 engineering guidelines </a> 
    32 <li>write associated documentation, tests, examples
    33 <li>include a short description with each submission
    34 <li>minimize the size of each submission
    35 <li>changes should be coherent!
    36 <li>split your code into multi-part submissions
    37 <li>before sending a submission: ask yourself <i>if I were a reviewer, would I understand the submission?</i>
    38 <li>pick a submission tool:
    39   <ul>
    40     <li>a simple patch
    41     <li>a patch review
    42     <li>a binary bundle
    43     <li>a hosted clone
    44     <li>patch emails
    45   </ul>
    46 <li>consider rewriting your development history
    47 </ul>
    48 <p>
    49 If you do not have the time to follow through the process to include your
    50 code in the main tree, please see <a href="#out-of-tree">below</a> about 
    51 contributing ns-3 code that is not maintained in the main tree.
    52 </p>
    53 
    54 <h2>ns-3 code reviews</h2>
    55 
    56 <p>Code submissions are expected to go through a review process where
    57 one or more maintainers comment on the code.  Contributors may be asked
    58 to revise their code or rewrite portions during this process.  New
    59 features are also typically only merged during the beginning of a new
    60 release cycle, to avoid destabilizing code before release.  
    61 
    62 <p>ns-3 reviews are conducted on the 
    63 <a href="http://groups.google.com/group/ns-3-reviews"> ns-3-reviews Google 
    64 group</a> and on the ns-developers
    65 mailing list for reviews that involve code that cuts across maintainer
    66 boundaries or is otherwise controversial.  
    67 
    68 <p>The basic purpose of the code reviews is to ensure the long-term
    69 maintenance and overall system integrity of ns-3.  Going through detailed
    70 code reviews is a departure in practice from ns-2, where many contributed
    71 modules were integrated without style or consistency requirements.  
    72 For ns-3, we feel that code reviews are essential to maintaining a high
    73 quality simulation tool and reducing the potential for simulation errors.
    74 
    75 <p>Some basic principles of the ns-3 reviews are to enforce the 
    76 <a href="codingstyle.html">coding style requirements and guidelines</a>
    77 and to review for consistency in overall system design and performance.
    78 A bad outcome (to be avoided) is when the contributor is asked to
    79 refactor code that is otherwise stylistically compliant late in the
    80 design/review process.  Sometimes the contributor may feel annoyed at
    81 somewhat arbitrary application of rules.  In such a situation, it is
    82 best to discuss points in question on the public mailing lists.
    83 For aspects of the contribution that are self contained and don't
    84 affect the rest of the simulator, when one of the (non-mandatory) coding 
    85 style or engineering guidelines is perceived to be not followed,
    86 maintainers/reviewers should offer suggestions such as "here is the ns-3
    87 way to do that" or "here is how I recommend you should change it" but
    88 leave it up to the contributor how he or she wants to deal with the
    89 comment.  Contributors should respond somehow to review suggestions not
    90 followed, even if it is a response like "gee, that is simply too much
    91 work to do for the benefit"
    92 
    93 <p> Maintainers should be free to ask contributors to rewrite proposed
    94 modifications to existing classes in the codebase, such as base classes.
    95 Because this is a bad outcome for the contributor, the maintainer should
    96 try to avoid this, but if deemed necessary, offer technical rationale
    97 why and be willing to debate this on the list (sometimes the maintainer
    98 is wrong).  If the maintainer is ultimately not convinced, the
    99 contributor should follow the maintainer's guidelines.  Again, to avoid
   100 surprises, contributors are encouraged to contact maintainers early
   101 in the process if they are planning to modify existing ns-3 code or APIs.
   102 
   103 <p>One final important issue that we touched on briefly above is system 
   104 integrity.  What we have seen in the past from ns-2 is that people contribute 
   105 models that have been tested within a specific configuration range 
   106 (e.g. running on a WiFi network only), but may not do something sane 
   107 with respect to other possible configurations.  If we do not look at these 
   108 overall system issues and require that, at the very least, contributed 
   109 models do something reasonable when they are configured outside their range 
   110 of applicability (even if the behavior is just a hard assert), invariably 
   111 there will be user mail in the future to the effect of "I tried to use 
   112 model X with configuration Y and got a surprising outcome."  That is if 
   113 we are lucky; the interaction may be error prone but not reveal any 
   114 warning signs to the user.  So, maintainers and reviewers may require 
   115 contributors to deal with these (current and potential) system issues. 
   116 
   117 <h2>Licensing, copyright, and authorship</h2>
   118 
   119 <p>
   120 All code submitted must conform to the project licensing framework, which
   121 is GNU GPLv2 compatibility.  All new source files must contain a 
   122 license statement.  All modified source files must either conform to
   123 the license of the original file or (in rare cases) may add a 
   124 license to the original license.  
   125 
   126 <p>Note that it is incumbent upon the submitter to make sure that the licensing
   127 attribution is correct and that the code is suitable for ns-3 inclusion.
   128 <i>Do not</i> include code (even snippets) from sources that have incompatible
   129 licenses.
   130 
   131 <p>If you are writing a new file or making large modifications (e.g. more 
   132 than 20 or 30 lines)
   133 to an existing file, include a copyright statement in the header, such as: 
   134 <tt>Copyright 2009 John Doe</tt>. Do not use the phrase <tt>All rights reserved.</tt>.
   135 Optionally, you may also add an author line to help us track you down in case of problems in
   136 the code such as <tt>Author:  john_doe@example.edu</tt>. However, for
   137 small modifications, do not create a chain of authors by
   138 appending to this list in the header such as <tt>Modified by Jane Doe 02/04/09</tt>;
   139 we use Mercurial change history to log who is changing what file.
   140 
   141 <h2>Coding style</h2>
   142 
   143 <p>
   144 Whatever code you submit, make sure that you follow the ns-3 <a href="http://www.nsnam.org/codingstyle.html">coding style</a> discussed on a separate page.
   145 
   146 <h2>Completing your code submission</h2>
   147 
   148 <p>Often we have observed that contributors will provide nice new classes
   149 implementing a new model, but will fail to include supporting test
   150 code, example scripts, and documentation.  Therefore, we ask people
   151 submitting the code (who are in the best position to do this type of
   152 work) to provide documentation, test code, and example scripts.
   153 
   154 <p>Note that you may want to go through multiple phases of code reviews,
   155 and all of this supporting material may not be needed at the first stage (e.g.
   156 when you want some feedback on public API header declarations only, before
   157 delving into implementation).  However, when it times come to merge your
   158 code, you should be prepared to provide these things, as fits your
   159 contribution (maintainers will provide some guidance here).
   160 
   161 <h3>Including examples</h3>
   162 
   163 <p>For many submissions, it will be important to include at least one example
   164 that exercises the new code, so the reviewer understands how it is intended
   165 to be used.  For final submission, please consider to add as many examples
   166 as you can that will help new users of your code.  The <tt>samples/</tt> and <tt>examples/</tt>
   167 directories are places for these files.
   168 
   169 <h3>Testing</h3>
   170 
   171 <p>Every new code should include automated tests.  These should, where 
   172 appropriate, be unit tests of model correctness, validation
   173 tests of stochastic behavior, and overall system tests if the model's 
   174 interaction with other components must be tested.  The test code should try 
   175 to cover as much model code as possible, and should be checked as much as 
   176 possible by regression tests.  
   177 
   178 <p> A separate <a href="http://www.nsnam.org/docs/testing.html">ns-3 testing 
   179 and validation manual</a> provides documentation and suggestions for 
   180 how to write tests.
   181  
   182 <h3>Documentation</h3>
   183 
   184 <p>If you add a new features, or make changes to existing features, you
   185 need to update existing or write new documentation and example code.
   186 Consider the following checklist:
   187 <ul>
   188   <li>doxygen should be added to header files for public classes and methods, 
   189 and should be checked for doxygen errors
   190   <li>incompatible API changes must be documented in <tt>CHANGES.html</tt>
   191   <li>new features should be described in <tt>RELEASE_NOTES</tt>
   192   <li>new API or changes to existing API must update the inline doxygen
   193     documentation in header files
   194   <li>consider updating or adding a new section to the tutorial in <tt>doc/tutorial/</tt>
   195   <li>consider updating or adding a new section to the manual in <tt>doc/manual/</tt>
   196   <li>update the <tt>AUTHORS</tt> file as appropriate
   197 </ul>
   198 
   199 <h2>Preparing your code for review</h2>
   200 
   201 <h3>Description</h3>
   202 
   203 <p>For each code submission, include a description of what your code is
   204 doing, and why. Ideally, you should be able to provide a summary
   205 description in a 5-line paragraph with a 1-line (15 word) subject. If
   206 you fix a bug filed in our <a href="http://www.nsnam.org/bugzilla">bugzilla database</a>, 
   207 the subject should include first the bug number, and then, the bug title.
   208 For example: <tt>bug 558: qos-tag.h is gone missing from wscript</tt>
   209 
   210 <p>Bonus points go to submitters who provide a description of their 
   211 testing strategy.
   212 
   213 <h3>Minimize the size of each submission</h3>
   214 
   215 <p>Each submission should be as small as possible. Ideally, each submission
   216 should deal with one issue only. If your description of your submission 
   217 includes words such as <i>and</i>, it is a big red warning sign that you
   218 should think about splitting your submission in two separate smaller 
   219 submissions, if the changes are not intertwined.
   220 
   221 <h3>Coherent changes</h3>
   222 
   223 <p>Each submission should be a coherent whole: if you need to edit ten
   224 files to get a feature to work, then, the submission should contain all
   225 the changes for these ten files. Of course, if you can split the feature 
   226 in sub-features, then, you should do it to decrease the size of the 
   227 submission as per the previous item.
   228 
   229 <p>For example, if you have made changes to optimized a module and to fix
   230 a bug in another module, make sure you separate these two sets of changes
   231 in two separate submissions.
   232 
   233 <h3>Multi-part submissions</h3>
   234 
   235 <p>If you are working on a large new feature or a large refactoring, and 
   236 because you will attempt to minimize the size of your submissions, you
   237 will have to split your large work in multiple separate submissions.
   238 
   239 <p>Ideally, these submissions will be started by a detailed explanation
   240 of the overall plan such that code reviewers can review each submission
   241 separately but within a large context. This kind of work typically is
   242 split in multiple dependent steps where each step depends on the previous
   243 one. If this is the case, make it very clear in your initial explanation.
   244 If you can, minimize dependencies between each step such that reviewers
   245 can merge each step separately without having to consider the impact of
   246 merging one submission on other submissions.
   247 
   248 <h3>Before sending a submission</h3>
   249 
   250 <p>When you send a submission for review and merging in ns-3, before
   251 you hit the 'Send' button of your email client, ask yourself one 
   252 last time: <i>If I were a reviewer, and I had to review that submission,
   253 what would I do ?</i>. Specifically, make sure that you have provided 
   254 enough context to allow someone else to understand what you did and why.
   255 Keep in mind that your reviewer does not have access to a readable
   256 dump of your brain: he has access only to your code, and your emails.
   257 
   258 <h3>Submission tools</h3>
   259 
   260 <p>
   261 There are many ways to submit a piece of code. Some of them are not
   262 appropriate:
   263 <ul>
   264   <li>do not send us a modified version of each file you have changed
   265   <li>do not send us a .zip or .tar.gz file by email with a copy of
   266 every modified file
   267   <li>do not send us a patch against an unspecified version of ns-3
   268 </ul>
   269 
   270 <p>Others are more appropriate:
   271 <ol>
   272   <li>a simple patch
   273   <li>patch reviews at <a href="http://codereview.appspot.com">codereview</a>
   274   <li>a hosted clone
   275   <li>a binary bundle
   276   <li>patchbombs
   277 </ol>
   278 <p>Most submissions to ns-3 use the first three techniques.
   279 
   280 <ol>
   281 <li><b>a simple patch</b>
   282 
   283 <p>Patches can be attached to a bug report or sent to our developer mailing-lists.
   284 
   285 <p>The UNIX <tt>diff</tt> tool is the most common way of producing a patch: a 
   286 patch is a text-based representation of the difference between
   287 two text files or two directories with text files in them. If you have
   288 two files, <tt>original.cc</tt>, and, <tt>modified.cc</tt>, you can generate a patch
   289 with the command <tt>diff -u original.cc modified.cc</tt>. If you wish to
   290 produce a patch between two directories, use the command 
   291 <tt>diff -uprN original modified</tt>.
   292 
   293 <p>Make sure you specify to the reviewer where the original files came 
   294 from and make sure that the resulting patch file does not contain 
   295 unexpected content by performing a final inspection of the patch 
   296 file yourself.
   297 
   298 <p>Patches such as this are sufficient for simple bug fixes or very simple
   299 small features.
   300 
   301 <li><b>patch reviews at <a href="http://codereview.appspot.com">codereview</a></b>
   302 
   303 <p>The best way to submit a larger patch for consideration is to request 
   304 a patch review:
   305 
   306 <ol>
   307 <li>download <a href="http://codereview.appspot.com/static/upload.py">upload.py</a>
   308 <li>record the changes you want to request a review for in a mercurial 
   309 repository: <tt>hg commit ...</tt>
   310 <li>within the mercurial repository, run upload.py. Make sure you specify 
   311 <tt>ns-3-reviews@googlegroups.com</tt> as a CC.
   312 <li>announce your review request on ns-developers 
   313 </ol>
   314 
   315 <p>When you send a tree without a detailed summary of your changes, it 
   316 would help if you could send a list of the changesets you want to merge. 
   317 To generate it, first merge with ns-3-dev and then, from your modified 
   318 directory, run <tt>hg outgoing -p http://code.nsnam.org/ns-3-dev</tt>
   319 
   320 <p>If you already pulled changes from ns-3-dev into your private repository 
   321 after you started doing your private modifications, there is an issue to 
   322 be considered. upload.py does not let you specify a range of revisions, 
   323 nor a set of changesets. So if you just run <tt>upload.py</tt> in your private 
   324 repository, the changesets pulled from ns-3-dev will also be published 
   325 on codereview, which is, of course, not desirable.
   326 
   327 <p>A possible workaround is to pull your changes into a temporary 
   328 repository which is an up-to-date clone of ns-3-dev. The following code 
   329 should do the trick. This code assumes that your private repository is in 
   330 path DEV_BRANCH_WITH_NEW_FEATURE, and that it is in sync with ns-3-dev.
   331 
   332 <pre>
   333 hg clone http://code.nsnam.org/ns-3-dev ns-3-tmp
   334 cd ns-3-tmp
   335 export REVNO=`hg tip -q | sed 's/:.*$//'`
   336 hg pull DEV_BRANCH_WITH_NEW_FEATURE
   337 hg merge
   338 hg commit -m "merged new feature"
   339 upload.py --rev=$REVNO --cc=ns-3-reviews@googlegroups.com 
   340 </pre>
   341 
   342 <p>To update a previous issue with a new patchset, use the -i (--issue) option 
   343 to avoid the tool creating a new issue.  For instance, if the issue to which
   344 you want to add a new patchset is numbered 165087, you would do
   345 
   346 <pre>
   347 hg commit -m "merged new feature"
   348 upload.py --rev=$REVNO --issue=165087 --cc=ns-3-reviews@googlegroups.com
   349 </pre>
   350 
   351 <li><b>Hosted clones</b>
   352 
   353 <p>If your bundles grow large in size, it can become tricky to send them over
   354 email: hosting a clone of your mercurial repository is a simple way to work
   355 around this. If you have a host with a static ip address and/or a hostname,
   356 you can run <tt>hg serve</tt> within your repository in this host and send instead
   357 to your co-workers a url to that repository: if I run <tt>hg serve -d -p 8080</tt> 
   358 on my local ns-3-dev clone, I could send the url <tt>http://diese.inria.fr:8080/</tt>
   359 to my co-workers to allow them to pull all my development history from there.
   360 
   361 <p>If you don't have a static ip address or hostname, or are located behind
   362 a NAT or a firewall, you have to use a third-party hosting provider for your
   363 code. A small list of hosting providers is available 
   364 <a href="http://www.selenic.com/mercurial/wiki/index.cgi/MercurialHosting">there</a>
   365 
   366 <p>We can also provide hosting to ns-3 developers on <a href="http://code.nsnam.org/">code.nsnam.org</a>
   367 should none of the above alternative work for you.
   368 
   369 <li><b>Binary bundles</b>
   370 
   371 <p>ns-3 uses <a href="http://www.selenic.com/mercurial/">mercurial</a> as its 
   372 version control tool: a quick introduction to using it can be found on our 
   373 <a href="http://www.nsnam.org/mercurial.html.">homepage</a>.
   374 Further extensive documentation and manuals are available from the mercurial
   375 <a href="http://www.selenic.com/mercurial/">homepage</a>.
   376 
   377 <p>If you get used to use mercurial to record your daily/hourly work using
   378 mercurial in a local clone of the ns-3 main repository, and if you want to 
   379 share that work with other people to get initial feedback about your work,
   380 your implementation approach, and start a high-level discussion about it,
   381 you can easily package all your local work history as a single binary bundle
   382 file with the <tt>hg bundle</tt> command and send this file over email. 
   383 
   384 <li><b>Patchbombs</b>
   385 
   386 <p>Patchbombs are a nice way to perform a request for a detailed
   387 review. Make sure you enable the patchbomb extension in your ~/.hgrc:
   388 <pre>
   389 [extensions]
   390 hgext.patchbomb = 
   391 </pre>
   392 Make sure you configure your patchbomb extension correctly: 
   393 <tt>man 5 hgrc</tt> and look in the [email] and [smtp] sections
   394 
   395 Finally:
   396 <pre>
   397 hg email -i -t ns-developers@isi.edu http://code.nsnam.org/ns-3-dev
   398 </pre>
   399 
   400 <p>Beware that generating too many emails with this tool without doing
   401 proper <i>history rewriting</i> to produce clean easy-to-review patches will
   402 be considered rude.
   403 
   404 </ol>
   405 
   406 <h3>History rewriting</h3>
   407 
   408 <p>The idea behind history rewriting is to request a code submitter to 
   409 re-arrange their repository history prior to merging in the main
   410 ns-3 repository. 
   411 
   412 <p>For example, let's say that during development of a new feature, you
   413 decide to use your mercurial repository as a fancy version-enabled
   414 backup system: you do a lot of work, and regularly commit it to save it.
   415 When you are done implementing and testing that new feature, the resulting
   416 repository history as shown with <tt>hg log</tt> will look very verbose, and
   417 will most likely contain a lot of commit messages such as <i>fix bug</i>.
   418 Clearly, none of these commits are very helpful and there is little point
   419 in keeping them around: they make it painful to use the annotate, and bisect
   420 commands, and will make review of the final mercurial tree harder than it
   421 need to be.
   422 
   423 <p>A simple way to work around these problems is to ask each contributor to
   424 re-structure their commit history before submitting their tree for review:
   425 <ul>
   426   <li>pointless backup commits are deleted from the history
   427   <li>commits are re-organized in sub-feature commits
   428   <li>each commit is made buildable
   429   <li>etc.
   430 </ul>
   431 
   432 <ol>
   433 <li><b>Rewrite a new history from scratch</b>
   434 
   435 <p>Once you are done with a new feature, you can generate one final patch
   436 with the mercurial <tt>diff</tt> command and apply it to a clean repository
   437 as one single commit with a nice new commit message.
   438 
   439 <p>A variant on the above is to split the final commit in multiple smaller
   440 commits, each of which addresses one aspect of the final feature and
   441 make sure that each commit is still buildable.
   442 
   443 <li><b>Build clean history from the start</b>
   444 
   445 <p>Instead of using mercurial as a powerful backup system and trying to go back
   446 later, some users prefer to use tools such as 'mq' or 'pbranch' to split
   447 their work from the start in a set of separate entities and still record 
   448 somewhere their day-to-day development history.
   449 
   450 <p>More information on these tools:
   451 <ul>
   452 <li><a href="http://hgbook.red-bean.com/read/managing-change-with-mercurial-queues.html">Managing change with mercurial queues</a>
   453 <li><a href="http://hgbook.red-bean.com/read/advanced-uses-of-mercurial-queues.html">Advanced uses of mercurial queues</a>
   454 <li>The <a href="http://arrenbrecht.ch/mercurial/pbranch/">Pbranch</a> extension.
   455 </ul>
   456 
   457 </ol>
   458 
   459 <h2><a name="out-of-tree">Submitting out-of-tree code</a></h2>
   460 
   461 <p>Anyone who wants to provide access to code that has been developed
   462 to extend ns-3, but who chooses not to go through the above review process,
   463  may list its availability on our website.  Furthermore,
   464 we will provide storage on a web server if needed.  This type of code
   465 contribution will not be formally maintained by the project, although
   466 popular extensions might be adopted downstream.
   467 
   468 <p>We ask anyone who wishes to do this to provide at least this information
   469 on our wiki:
   470 <ul>
   471 <li>Authors,</li>
   472 <li>Name and description of the extension,</li>
   473 <li>How it is distributed (as a patch or full tarball),</li>
   474 <li>Location,</li>
   475 <li>Status (how it is maintained)</li>
   476 </ul>
   477 
   478 <p>The contribution will be stored on our wiki 
   479 <a href="wiki/index.php/Contributed_Code">here</a>.  
   480 If you need web server space for your extension, please
   481 contact one of the project maintainers.
   482