Sophie

Sophie

distrib > Fedora > 17 > i386 > media > updates > by-pkgid > 675c8c8167236dfcf8d66da674f931e8 > files > 160

erlang-doc-R15B-03.3.fc17.noarch.rpm

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns:fn="http://www.w3.org/2005/02/xpath-functions">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../../doc/otp_doc.css" type="text/css">
<title>Erlang -- How to implement an alternative carrier for  the Erlang distribution</title>
</head>
<body bgcolor="white" text="#000000" link="#0000ff" vlink="#ff00ff" alink="#ff0000"><div id="container">
<script id="js" type="text/javascript" language="JavaScript" src="../../../doc/js/flipmenu/flipmenu.js"></script><script id="js2" type="text/javascript" src="../../../doc/js/erlresolvelinks.js"></script><script language="JavaScript" type="text/javascript">
            <!--
              function getWinHeight() {
                var myHeight = 0;
                if( typeof( window.innerHeight ) == 'number' ) {
                  //Non-IE
                  myHeight = window.innerHeight;
                } else if( document.documentElement && ( document.documentElement.clientWidth ||
                                                         document.documentElement.clientHeight ) ) {
                  //IE 6+ in 'standards compliant mode'
                  myHeight = document.documentElement.clientHeight;
                } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
                  //IE 4 compatible
                  myHeight = document.body.clientHeight;
                }
                return myHeight;
              }

              function setscrollpos() {
                var objf=document.getElementById('loadscrollpos');
                 document.getElementById("leftnav").scrollTop = objf.offsetTop - getWinHeight()/2;
              }

              function addEvent(obj, evType, fn){
                if (obj.addEventListener){
                obj.addEventListener(evType, fn, true);
                return true;
              } else if (obj.attachEvent){
                var r = obj.attachEvent("on"+evType, fn);
                return r;
              } else {
                return false;
              }
             }

             addEvent(window, 'load', setscrollpos);

             //--></script><div id="leftnav"><div class="innertube">
<img alt="Erlang logo" src="../../../doc/erlang-logo.png"><br><small><a href="users_guide.html">User's Guide</a><br><a href="index.html">Reference Manual</a><br><a href="release_notes.html">Release Notes</a><br><a href="../pdf/erts-5.9.3.1.pdf">PDF</a><br><a href="../../../doc/index.html">Top</a></small><p><strong>Erlang Run-Time System Application (ERTS)</strong><br><strong>User's Guide</strong><br><small>Version 5.9.3.1</small></p>
<br><a href="javascript:openAllFlips()">Expand All</a><br><a href="javascript:closeAllFlips()">Contract All</a><p><small><strong>Chapters</strong></small></p>
<ul class="flipMenu" imagepath="../../../doc/js/flipmenu">
<li id="no" title="Match specifications in Erlang" expanded="false">Match specifications in Erlang<ul>
<li><a href="match_spec.html">
              Top of chapter
            </a></li>
<li title="Grammar"><a href="match_spec.html#id67432">Grammar</a></li>
<li title="Function descriptions"><a href="match_spec.html#id65382">Function descriptions</a></li>
<li title="Variables and literals"><a href="match_spec.html#id73136">Variables and literals</a></li>
<li title="Execution of the match"><a href="match_spec.html#id73066">Execution of the match</a></li>
<li title="Differences between match specifications in ETS and tracing"><a href="match_spec.html#id69369">Differences between match specifications in ETS and tracing</a></li>
<li title="Examples"><a href="match_spec.html#id69408">Examples</a></li>
</ul>
</li>
<li id="no" title="How to interpret the Erlang crash dumps" expanded="false">How to interpret the Erlang crash dumps<ul>
<li><a href="crash_dump.html">
              Top of chapter
            </a></li>
<li title="General information"><a href="crash_dump.html#id71973">General information</a></li>
<li title="Memory information"><a href="crash_dump.html#id69719">Memory information</a></li>
<li title="Internal table information"><a href="crash_dump.html#id69746">Internal table information</a></li>
<li title="Allocated areas"><a href="crash_dump.html#id71628">Allocated areas</a></li>
<li title="Allocator"><a href="crash_dump.html#id71654">Allocator</a></li>
<li title="Process information"><a href="crash_dump.html#id71693">Process information</a></li>
<li title="Port information"><a href="crash_dump.html#id70337">Port information</a></li>
<li title="ETS tables"><a href="crash_dump.html#id70354">ETS tables</a></li>
<li title="Timers"><a href="crash_dump.html#id70452">Timers</a></li>
<li title="Distribution information"><a href="crash_dump.html#id77672">Distribution information</a></li>
<li title="Loaded module information"><a href="crash_dump.html#id77796">Loaded module information</a></li>
<li title="Fun information"><a href="crash_dump.html#id77887">Fun information</a></li>
<li title="Process Data"><a href="crash_dump.html#id77959">Process Data</a></li>
<li title="Atoms"><a href="crash_dump.html#id78034">Atoms</a></li>
<li title="Disclaimer"><a href="crash_dump.html#id78051">Disclaimer</a></li>
</ul>
</li>
<li id="loadscrollpos" title="How to implement an alternative carrier for  the Erlang distribution" expanded="true">How to implement an alternative carrier for  the Erlang distribution<ul>
<li><a href="alt_dist.html">
              Top of chapter
            </a></li>
<li title="Introduction"><a href="alt_dist.html#id78171">Introduction</a></li>
<li title="The driver"><a href="alt_dist.html#id78261">The driver</a></li>
<li title="Putting it all together"><a href="alt_dist.html#id79511">Putting it all together</a></li>
</ul>
</li>
<li id="no" title="The Abstract Format" expanded="false">The Abstract Format<ul>
<li><a href="absform.html">
              Top of chapter
            </a></li>
<li title="Module declarations and forms"><a href="absform.html#id79791">Module declarations and forms</a></li>
<li title="Atomic literals"><a href="absform.html#id79999">Atomic literals</a></li>
<li title="Patterns"><a href="absform.html#id80061">Patterns</a></li>
<li title="Expressions"><a href="absform.html#id80275">Expressions</a></li>
<li title="Clauses"><a href="absform.html#id80983">Clauses</a></li>
<li title="Guards"><a href="absform.html#id81203">Guards</a></li>
<li title="The abstract format after preprocessing"><a href="absform.html#id81478">The abstract format after preprocessing</a></li>
</ul>
</li>
<li id="no" title="tty - A command line interface" expanded="false">tty - A command line interface<ul>
<li><a href="tty.html">
              Top of chapter
            </a></li>
<li title="Normal Mode"><a href="tty.html#id81651">Normal Mode</a></li>
<li title="Shell Break Mode"><a href="tty.html#id82034">Shell Break Mode</a></li>
</ul>
</li>
<li id="no" title="How to implement a driver" expanded="false">How to implement a driver<ul>
<li><a href="driver.html">
              Top of chapter
            </a></li>
<li title="Introduction"><a href="driver.html#id82159">Introduction</a></li>
<li title="Sample driver"><a href="driver.html#id82191">Sample driver</a></li>
<li title="Compiling and linking the sample driver"><a href="driver.html#id82593">Compiling and linking the sample driver</a></li>
<li title="Calling a driver as a port in Erlang"><a href="driver.html#id82629">Calling a driver as a port in Erlang</a></li>
<li title="Sample asynchronous driver"><a href="driver.html#id82736">Sample asynchronous driver</a></li>
<li title="An asynchronous driver using driver_async"><a href="driver.html#id83010">An asynchronous driver using driver_async</a></li>
</ul>
</li>
<li id="no" title="Inet configuration" expanded="false">Inet configuration<ul>
<li><a href="inet_cfg.html">
              Top of chapter
            </a></li>
<li title="Introduction"><a href="inet_cfg.html#id83274">Introduction</a></li>
<li title="Configuration Data"><a href="inet_cfg.html#id83378">Configuration Data</a></li>
<li title="User Configuration Example"><a href="inet_cfg.html#id84255">User Configuration Example</a></li>
</ul>
</li>
<li id="no" title="External Term Format" expanded="false">External Term Format<ul>
<li><a href="erl_ext_dist.html">
              Top of chapter
            </a></li>
<li title="Introduction"><a href="erl_ext_dist.html#id84356">Introduction</a></li>
<li title="Distribution header"><a href="erl_ext_dist.html#id84636">Distribution header</a></li>
<li title="ATOM_CACHE_REF"><a href="erl_ext_dist.html#id85209">ATOM_CACHE_REF</a></li>
<li title="SMALL_INTEGER_EXT"><a href="erl_ext_dist.html#id85285">SMALL_INTEGER_EXT</a></li>
<li title="INTEGER_EXT"><a href="erl_ext_dist.html#id85348">INTEGER_EXT</a></li>
<li title="FLOAT_EXT"><a href="erl_ext_dist.html#id85412">FLOAT_EXT</a></li>
<li title="ATOM_EXT"><a href="erl_ext_dist.html#id85488">ATOM_EXT</a></li>
<li title="REFERENCE_EXT"><a href="erl_ext_dist.html#id85583">REFERENCE_EXT</a></li>
<li title="PORT_EXT"><a href="erl_ext_dist.html#id85741">PORT_EXT</a></li>
<li title="PID_EXT"><a href="erl_ext_dist.html#id85860">PID_EXT</a></li>
<li title="SMALL_TUPLE_EXT"><a href="erl_ext_dist.html#id86004">SMALL_TUPLE_EXT</a></li>
<li title="LARGE_TUPLE_EXT"><a href="erl_ext_dist.html#id86096">LARGE_TUPLE_EXT</a></li>
<li title="NIL_EXT"><a href="erl_ext_dist.html#id86186">NIL_EXT</a></li>
<li title="STRING_EXT"><a href="erl_ext_dist.html#id86237">STRING_EXT</a></li>
<li title="LIST_EXT"><a href="erl_ext_dist.html#id86329">LIST_EXT</a></li>
<li title="BINARY_EXT"><a href="erl_ext_dist.html#id86448">BINARY_EXT</a></li>
<li title="SMALL_BIG_EXT"><a href="erl_ext_dist.html#id86545">SMALL_BIG_EXT</a></li>
<li title="LARGE_BIG_EXT"><a href="erl_ext_dist.html#id86652">LARGE_BIG_EXT</a></li>
<li title="NEW_REFERENCE_EXT"><a href="erl_ext_dist.html#id86755">NEW_REFERENCE_EXT</a></li>
<li title="SMALL_ATOM_EXT"><a href="erl_ext_dist.html#id86928">SMALL_ATOM_EXT</a></li>
<li title="FUN_EXT"><a href="erl_ext_dist.html#id87034">FUN_EXT</a></li>
<li title="NEW_FUN_EXT"><a href="erl_ext_dist.html#id87281">NEW_FUN_EXT</a></li>
<li title="EXPORT_EXT"><a href="erl_ext_dist.html#id87658">EXPORT_EXT</a></li>
<li title="BIT_BINARY_EXT"><a href="erl_ext_dist.html#id87802">BIT_BINARY_EXT</a></li>
<li title="NEW_FLOAT_EXT"><a href="erl_ext_dist.html#id87908">NEW_FLOAT_EXT</a></li>
</ul>
</li>
<li id="no" title="Distribution Protocol" expanded="false">Distribution Protocol<ul>
<li><a href="erl_dist_protocol.html">
              Top of chapter
            </a></li>
<li title="EPMD Protocol"><a href="erl_dist_protocol.html#id88085">EPMD Protocol</a></li>
<li title="Handshake"><a href="erl_dist_protocol.html#id89383">Handshake</a></li>
<li title="Protocol between connected nodes"><a href="erl_dist_protocol.html#id89399">Protocol between connected nodes</a></li>
<li title="New Ctrlmessages for distrvsn = 1 (OTP R4)"><a href="erl_dist_protocol.html#id89841">New Ctrlmessages for distrvsn = 1 (OTP R4)</a></li>
<li title="New Ctrlmessages for distrvsn = 2"><a href="erl_dist_protocol.html#id89939">New Ctrlmessages for distrvsn = 2</a></li>
<li title="New Ctrlmessages for distrvsn = 3 (OTP R5C)"><a href="erl_dist_protocol.html#id89951">New Ctrlmessages for distrvsn = 3 (OTP R5C)</a></li>
<li title="New Ctrlmessages for distrvsn = 4 (OTP R6)"><a href="erl_dist_protocol.html#id89963">New Ctrlmessages for distrvsn = 4 (OTP R6)</a></li>
</ul>
</li>
</ul>
</div></div>
<div id="content">
<div class="innertube">
<h1>3 How to implement an alternative carrier for  the Erlang distribution</h1>
  
  <p>This document describes how one can implement ones own carrier
    protocol for the Erlang distribution. The distribution is normally
    carried by the TCP/IP protocol. What's explained here is the method for 
    replacing TCP/IP with another protocol. </p>
  <p>The document is a step by step explanation of the <span class="code">uds_dist</span> example 
    application (seated in the kernel applications <span class="code">examples</span> directory). 
    The <span class="code">uds_dist</span> application implements distribution over Unix domain 
    sockets and is written for the Sun Solaris 2 operating environment. The 
    mechanisms are however general and applies to any operating system Erlang 
    runs on. The reason the C code is not made portable, is simply readability.</p>
  <div class="note">
<div class="label">Note</div>
<div class="content"><p><p>This document was written a long time ago. Most of it is still
           valid, but some things have changed since it was first written.
	   Most notably the driver interface. There have been some updates
	   to the documentation of the driver presented in this documentation,
	   but more could be done and are planned for the future. The
	   reader is encouraged to also read the
	   <span class="bold_code"><a href="erl_driver.html">erl_driver</a></span>, and the
	   <span class="bold_code"><a href="erl_driver.html">driver_entry</a></span> documentation.
   </p></p></div>
</div>

  <h3><a name="id78171">3.1 
        Introduction</a></h3>
    
    <p>To implement a new carrier for the Erlang distribution, one must first
      make the protocol available to the Erlang machine, which involves writing 
      an Erlang driver. There is no way one can use a port program,
      there <strong>has</strong> to
      be an Erlang driver. Erlang drivers can either be statically
      linked
      to the emulator, which can be an alternative when using the open source
      distribution of Erlang, or dynamically loaded into the Erlang machines
      address space, which is the only alternative if a precompiled version of 
      Erlang is to be used. </p>
    <p>Writing an Erlang driver is by no means easy. The driver is written 
      as a couple of call-back functions called by the Erlang emulator when
      data is sent to the driver or the driver has any data available on a file
      descriptor. As the driver call-back routines execute in the main
      thread of the Erlang machine, the call-back functions can perform
      no blocking activity whatsoever. The call-backs should only set up
      file descriptors for waiting and/or read/write available data. All
      I/O has to be non blocking. Driver call-backs are however executed
      in sequence, why a global state can safely be updated within the
      routines. </p>
    <p>When the driver is implemented, one would preferably write an
      Erlang interface for the driver to be able to test the
      functionality of the driver separately. This interface can then
      be used by the distribution module which will cover the details of
      the protocol from the <span class="code">net_kernel</span>. The easiest path is to
      mimic the <span class="code">inet</span> and <span class="code">inet_tcp</span> interfaces, but a lot of
      functionality in those modules need not be implemented. In the
      example application, only a few of the usual interfaces are
      implemented, and they are much simplified.</p>
    <p>When the protocol is available to Erlang through a driver and an
      Erlang interface module, a distribution module can be
      written. The distribution module is a module with well defined
      call-backs, much like a <span class="code">gen_server</span> (there is no compiler support
      for checking the call-backs though). The details of finding other
      nodes (i.e. talking to epmd or something similar), creating a
      listen port (or similar), connecting to other nodes and performing
      the handshakes/cookie verification are all implemented by this
      module. There is however a utility module, <span class="code">dist_util</span>, that
      will do most of the hard work of handling handshakes, cookies,
      timers and ticking. Using <span class="code">dist_util</span> makes implementing a
      distribution module much easier and that's what we are doing in
      the example application.</p>
    <p>The last step is to create boot scripts to make the protocol
      implementation available at boot time. The implementation can be
      debugged by starting the distribution when all of the system is
      running, but in a real system the distribution should start very
      early, why a boot-script and some command line parameters are
      necessary. This last step also implies that the Erlang code in the 
      interface and distribution modules is written in such a way that
      it can be run in the startup phase. Most notably there can be no
      calls to the <span class="code">application</span> module or to any modules not
      loaded at boot-time (i.e. only <span class="code">kernel</span>, <span class="code">stdlib</span> and the
      application itself can be used).</p>
  

  <h3><a name="id78261">3.2 
        The driver</a></h3>
    
    <p>Although Erlang drivers in general may be beyond the scope of this
      document, a brief introduction seems to be in place.</p>

    <h4>Drivers in general</h4>
      
      <p>An Erlang driver is a native code module written in C (or
        assembler) which serves as an interface for some special operating
        system service. This is a general mechanism that is used
        throughout the Erlang emulator for all kinds of I/O. An Erlang
        driver can be dynamically linked (or loaded) to the Erlang
        emulator at runtime by using the <span class="code">erl_ddll</span> Erlang
        module. Some of the drivers in OTP are however statically linked
        to the runtime system, but that's more an optimization than a
        necessity.</p>
      <p>The driver data-types and the functions available to the driver
        writer are defined in the header file <span class="code">erl_driver.h</span> (there
        is also an deprecated version called <span class="code">driver.h</span>, don't use
        that one.) seated in Erlang's include directory (and in
        $ERL_TOP/erts/emulator/beam in the source code
        distribution). Refer to that file for function prototypes etc.</p>
      <p>When writing a driver to make a communications protocol available
        to Erlang, one should know just about everything worth knowing
        about that particular protocol. All operation has to be non
        blocking and all possible situations should be accounted for in
        the driver. A non stable driver will affect and/or crash the
        whole Erlang runtime system, which is seldom what's wanted. </p>
      <p>The emulator calls the driver in the following situations:</p>
      <ul>
        <li>When the driver is loaded. This call-back has to have a
         special name and will inform the emulator of what call-backs should
         be used by returning a pointer to a <span class="code">ErlDrvEntry</span> struct,
         which should be properly filled in (see below).</li>
        <li>When a port to the driver is opened (by a <span class="code">open_port</span>
         call from Erlang). This routine should set up internal data
         structures and return an opaque data entity of the type
        <span class="code">ErlDrvData</span>, which is a data-type large enough to hold a
         pointer. The pointer returned by this function will be the first
         argument to all other call-backs concerning this particular
         port. It is usually called the port handle. The emulator only
         stores the handle and does never try to interpret it, why it can
         be virtually anything (well anything not larger than a pointer
         that is) and can point to anything if it is a pointer. Usually
         this pointer will refer to a structure holding information about
         the particular port, as i t does in our example.</li>
        <li>When an Erlang process sends data to the port. The data will
         arrive as a buffer of bytes, the interpretation is not defined,
         but is up to the implementor. This call-back returns nothing to the
         caller, answers are sent to the caller as messages (using a
         routine called <span class="code">driver_output</span> available to all
         drivers). There is also a way to talk in a synchronous way to
         drivers, described below. There can be an additional call-back
         function for handling data that is fragmented (sent in a deep
         io-list). That interface will get the data in a form suitable for
         Unix <span class="code">writev</span> rather than in a single buffer. There is no
         need for a distribution driver to implement such a call-back, so
         we wont.</li>
        <li>When a file descriptor is signaled for input. This call-back
         is called when the emulator detects input on a file descriptor
         which the driver has marked for monitoring by using the interface
        <span class="code">driver_select</span>. The mechanism of driver select makes it
         possible to read non blocking from file descriptors by calling
        <span class="code">driver_select</span> when reading is needed and then do the actual
         reading in this call-back (when reading is actually possible). The
         typical scenario is that <span class="code">driver_select</span> is called when an 
         Erlang process orders a read operation, and that this routine
         sends the answer when data is available on the file descriptor.</li>
        <li>When a file descriptor is signaled for output. This call-back
         is called in a similar way as the previous, but when writing to a
         file descriptor is possible. The usual scenario is that Erlang
         orders writing on a file descriptor and that the driver calls
        <span class="code">driver_select</span>. When the descriptor is ready for output,
         this call-back is called an the driver can try to send the
         output. There may of course be queuing involved in such
         operations, and there are some convenient queue routines available
         to the driver writer to use in such situations.</li>
        <li>When a port is closed, either by an Erlang process or by the
         driver calling one of the <span class="code">driver_failure_XXX</span> routines. This
         routine should clean up everything connected to one particular
         port. Note that when other call-backs call a
        <span class="code">driver_failure_XXX</span> routine, this routine will be
         immediately called and the call-back routine issuing the error can
         make no more use of the data structures for the port, as this
         routine surely has freed all associated data and closed all file
         descriptors. If the queue utility available to driver writes is
         used, this routine will however <strong>not</strong> be called until the
         queue is empty.</li>
        <li>When an Erlang process calls <span class="code">erlang:port_control/3</span>,
         which is a synchronous interface to drivers. The control interface
         is used to set driver options, change states of ports etc. We'll
         use this interface quite a lot in our example.</li>
        <li>When a timer expires. The driver can set timers with the
         function <span class="code">driver_set_timer</span>. When such timers expire, a
         specific call-back function is called. We will not use timers in
         our example.</li>
        <li>When the whole driver is unloaded. Every resource allocated
         by the driver should be freed.</li>
      </ul>
    

    <h4>The distribution driver's data structures</h4>
      
      <p>The driver used for Erlang distribution should implement a
        reliable, order maintaining, variable length packet oriented
        protocol. All error correction, re-sending and such need to be
        implemented in the driver or by the underlying communications
        protocol. If the protocol is stream oriented (as is the case with
        both TCP/IP and our streamed Unix domain sockets), some mechanism
        for packaging is needed. We will use the simple method of having a
        header of four bytes containing the length of the package in a big
        endian 32 bit integer (as Unix domain sockets only can be used
        between processes on the same machine, we actually don't need to
        code the integer in some special endianess, but I'll do it anyway
        because in most situation you do need to do it. Unix domain
        sockets are reliable and order maintaining, so we don't need to
        implement resends and such in our driver.</p>
      <p>Lets start writing our example Unix domain sockets driver by
        declaring prototypes and filling in a static ErlDrvEntry
        structure.</p>
      <div class="example"><pre>
( 1) #include &lt;stdio.h&gt;
( 2) #include &lt;stdlib.h&gt;
( 3) #include &lt;string.h&gt;
( 4) #include &lt;unistd.h&gt;
( 5) #include &lt;errno.h&gt;
( 6) #include &lt;sys/types.h&gt;
( 7) #include &lt;sys/stat.h&gt;
( 8) #include &lt;sys/socket.h&gt;
( 9) #include &lt;sys/un.h&gt;
(10) #include &lt;fcntl.h&gt;

(11) #define HAVE_UIO_H
(12) #include "erl_driver.h"

(13) /*
(14) ** Interface routines
(15) */
(16) static ErlDrvData uds_start(ErlDrvPort port, char *buff);
(17) static void uds_stop(ErlDrvData handle);
(18) static void uds_command(ErlDrvData handle, char *buff, int bufflen);
(19) static void uds_input(ErlDrvData handle, ErlDrvEvent event);
(20) static void uds_output(ErlDrvData handle, ErlDrvEvent event);
(21) static void uds_finish(void);
(22) static int uds_control(ErlDrvData handle, unsigned int command, 
(23)                        char* buf, int count, char** res, int res_size);

(24) /* The driver entry */
(25) static ErlDrvEntry uds_driver_entry = {
(26)     NULL,                            /* init, N/A */
(27)     uds_start,                       /* start, called when port is opened */
(28)     uds_stop,                        /* stop, called when port is closed */
(29)     uds_command,                     /* output, called when erlang has sent */
(30)     uds_input,                       /* ready_input, called when input
(31)                                         descriptor ready */
(32)     uds_output,                      /* ready_output, called when output 
(33)                                         descriptor ready */
(34)     "uds_drv",                       /* char *driver_name, the argument 
(35)                                         to open_port */
(36)     uds_finish,                      /* finish, called when unloaded */
(37)     NULL,                            /* void * that is not used (BC) */
(38)     uds_control,                     /* control, port_control callback */
(39)     NULL,                            /* timeout, called on timeouts */
(40)     NULL,                            /* outputv, vector output interface */
(41)     NULL,                            /* ready_async callback */
(42)     NULL,                            /* flush callback */
(43)     NULL,                            /* call callback */
(44)     NULL,                            /* event callback */
(45)     ERL_DRV_EXTENDED_MARKER,         /* Extended driver interface marker */
(46)     ERL_DRV_EXTENDED_MAJOR_VERSION,  /* Major version number */
(47)     ERL_DRV_EXTENDED_MINOR_VERSION,  /* Minor version number */
(48)     ERL_DRV_FLAG_SOFT_BUSY,          /* Driver flags. Soft busy flag is
(49)                                         required for distribution drivers */
(50)     NULL,                            /* Reserved for internal use */
(51)     NULL,                            /* process_exit callback */
(52)     NULL                             /* stop_select callback */
(53) };</pre></div>
      <p>On line 1 to 10 we have included the OS headers needed for our
        driver. As this driver is written for Solaris, we know that the
        header <span class="code">uio.h</span> exists, why we can define the preprocessor
        variable <span class="code">HAVE_UIO_H</span> before we include <span class="code">erl_driver.h</span> 
        at line 12. The definition of <span class="code">HAVE_UIO_H</span> will make the
        I/O vectors used in Erlang's driver queues to correspond to the
        operating systems ditto, which is very convenient.</p>
      <p>The different call-back functions are declared ("forward
        declarations") on line 16 to 23.</p>
      <p>The driver structure is similar for statically linked in
        drivers and dynamically loaded. However some of the fields
        should be left empty (i.e. initialized to NULL) in the
        different types of drivers. The first field (the <span class="code">init</span>
        function pointer) is always left blank in a dynamically loaded
        driver, which can be seen on line 26. The NULL on line 37
        should always be there, the field is no longer used and is
        retained for backward compatibility. We use no timers in this
        driver, why no call-back for timers is needed. The <span class="code">outputv</span> field
        (line 40) can be used to implement an interface similar to
        Unix <span class="code">writev</span> for output. The Erlang runtime
	system could previously not use <span class="code">outputv</span> for the
	distribution, but since erts version 5.7.2 it can.
	Since this driver was written before erts version 5.7.2 it does 
	not use the <span class="code">outputv</span> callback. Using the <span class="code">outputv</span>
	callback is preferred since it reduces copying of data. (We
	will however use scatter/gather I/O internally in the driver).</p>
      <p>As of erts version 5.5.3 the driver interface was extended with
         version control and the possibility to pass capability information.
	 Capability flags are present at line 48. As of erts version 5.7.4
	 the
	 <span class="bold_code"><a href="driver_entry.html#driver_flags">ERL_DRV_FLAG_SOFT_BUSY</a></span>
	 flag is required for drivers that are to be used by the distribution.
	 The soft busy flag implies that the driver is capable of handling
	 calls to the <span class="code">output</span> and <span class="code">outputv</span> callbacks even though
	 it has marked itself as busy. This has always been a requirement
	 on drivers used by the distribution, but there have previously not
	 been any capability information available about this. For more
	 information see
         <span class="bold_code"><a href="erl_driver.html#set_busy_port">set_busy_port()</a></span>).
</p>
      <p>This driver was written before the runtime system had SMP support.
         The driver will still function in the runtime system with SMP support,
	 but performance will suffer from lock contention on the driver lock
	 used for the driver. This can be alleviated by reviewing and perhaps
	 rewriting the code so that each instance of the driver safely can
	 execute in parallel. When instances safely can execute in parallel it
	 is safe to enable instance specific locking on the driver. This is done
	 by passing
	 <span class="bold_code"><a href="driver_entry.html#driver_flags">ERL_DRV_FLAG_USE_PORT_LOCKING</a></span>
	 as a driver flag. This is left as an exercise for the reader.</p>
      <p>Our defined call-backs thus are:</p>
      <ul>
        <li>uds_start, which shall initiate data for a port. We wont
         create any actual sockets here, just initialize data structures.</li>
        <li>uds_stop, the function called when a port is closed.</li>
        <li>uds_command, which will handle messages from Erlang. The
         messages can either be plain data to be sent or more subtle
         instructions to the driver. We will use this function mostly for
         data pumping.</li>
        <li>uds_input, this is the call-back which is called when we have
         something to read from a socket.</li>
        <li>uds_output, this is the function called when we can write to a
         socket.</li>
        <li>uds_finish, which is called when the driver is unloaded. A
         distribution driver will actually (or hopefully) never be unloaded,
         but we include this for completeness. Being able to clean up after
         oneself is always a good thing.</li>
        <li>uds_control, the <span class="code">erlang:port_control/2</span> call-back, which
         will be used a lot in this implementation.</li>
      </ul>
      <p>The ports implemented by this driver will operate in two major
        modes, which i will call the <strong>command</strong> and <strong>data</strong>
        modes. In command mode, only passive reading and writing (like
        gen_tcp:recv/gen_tcp:send) can be
        done, and this is the mode the port will be in during the
        distribution handshake. When the connection is up, the port will
        be switched to data mode and all data will be immediately read and
        passed further to the Erlang emulator. In data mode, no data
        arriving to the uds_command will be interpreted, but just packaged
        and sent out on the socket. The uds_control call-back will do the
        switching between those two modes.</p>
      <p>While the <span class="code">net_kernel</span> informs different subsystems that the
        connection is coming up, the port should accept data to send, but
        not receive any data, to avoid that data arrives from another node
        before every kernel subsystem is prepared to handle it. We have a
        third mode for this intermediate stage, lets call it the
        <strong>intermediate</strong> mode.</p>
      <p>Lets define an enum for the different types of ports we have:</p>
      <div class="example"><pre>
( 1) typedef enum { 
( 2)     portTypeUnknown,      /* An uninitialized port */
( 3)     portTypeListener,     /* A listening port/socket */
( 4)     portTypeAcceptor,     /* An intermediate stage when accepting
( 5)                              on a listen port */
( 6)     portTypeConnector,    /* An intermediate stage when connecting */
( 7)     portTypeCommand,      /* A connected open port in command mode */
( 8)     portTypeIntermediate, /* A connected open port in special
( 9)                              half active mode */
(10)     portTypeData          /* A connectec open port in data mode */ 
(11) } PortType;      </pre></div>
      <p>Lets look at the different types:</p>
      <ul>
        <li>portTypeUnknown - The type a port has when it's opened, but
         not actually bound to any file descriptor.</li>
        <li>portTypeListener - A port that is connected to a listen
         socket. This port will not do especially much, there will be no data
         pumping done on this socket, but there will be read data available
         when one is trying to do an accept on the port.</li>
        <li>portTypeAcceptor - This is a port that is to represent the
         result of an accept operation. It is created when one wants to
         accept from a listen socket, and it will be converted to a
         portTypeCommand when the accept succeeds.</li>
        <li>portTypeConnector - Very similar to portTypeAcceptor, an
         intermediate stage between the request for a connect operation and
         that the socket is really connected to an accepting ditto in the
         other end. As soon as the sockets are connected, the port will
         switch type to portTypeCommand.</li>
        <li>portTypeCommand - A connected socket (or accepted socket if
         you want) that is in the command mode mentioned earlier.</li>
        <li>portTypeIntermediate - The intermediate stage for a connected
         socket. There should be no processing of input for this socket.</li>
        <li>portTypeData - The mode where data is pumped through the port
         and the uds_command routine will regard every call as a call where
         sending is wanted. In this mode all input available will be read and
         sent to Erlang as soon as it arrives on the socket, much like in the
         active mode of a <span class="code">gen_tcp</span> socket.</li>
      </ul>
      <p>Now lets look at the state we'll need for our ports. One can note
        that not all fields are used for all types of ports and that one
        could save some space by using unions, but that would clutter the
        code with multiple indirections, so i simply use one struct for
        all types of ports, for readability.</p>
      <div class="example"><pre>
( 1) typedef unsigned char Byte;
( 2) typedef unsigned int Word;

( 3) typedef struct uds_data {
( 4)     int fd;                   /* File descriptor */
( 5)     ErlDrvPort port;          /* The port identifier */
( 6)     int lockfd;               /* The file descriptor for a lock file in 
( 7)                                  case of listen sockets */
( 8)     Byte creation;            /* The creation serial derived from the 
( 9)                                  lockfile */
(10)     PortType type;            /* Type of port */
(11)     char *name;               /* Short name of socket for unlink */
(12)     Word sent;                /* Bytes sent */
(13)     Word received;            /* Bytes received */
(14)     struct uds_data *partner; /* The partner in an accept/listen pair */
(15)     struct uds_data *next;    /* Next structure in list */
(16)     /* The input buffer and its data */
(17)     int buffer_size;          /* The allocated size of the input buffer */
(18)     int buffer_pos;           /* Current position in input buffer */
(19)     int header_pos;           /* Where the current header is in the 
(20)                                  input buffer */
(21)     Byte *buffer;             /* The actual input buffer */
(22) } UdsData;      </pre></div>
      <p>This structure is used for all types of ports although some
        fields are useless for some types. The least memory consuming
        solution would be to arrange this structure as a union of
        structures, but the multiple indirections in the code to
        access a field in such a structure will clutter the code to
        much for an example.</p>
      <p>Let's look at the fields in our structure:</p>
      <ul>
        <li>fd - The file descriptor of the socket associated with the
         port.</li>
        <li>port - The port identifier for the port which this structure
         corresponds to. It is needed for most <span class="code">driver_XXX</span>
         calls from the driver back to the emulator.</li>
        <li>
          <p>lockfd - If the socket is a listen socket, we use a separate
            (regular) file for two purposes:</p>
          <ul>
            <li>We want a locking mechanism that gives no race
             conditions, so that we can be sure of if another Erlang
             node uses the listen socket name we require or if the
             file is only left there from a previous (crashed)
             session.</li>
            <li>
              <p>We store the <strong>creation</strong> serial number in the
                file. The <strong>creation</strong> is a number that should
                change between different instances of different Erlang
                emulators with the same name, so that process
                identifiers from one emulator won't be valid when sent
                to a new emulator with the same distribution name. The
                creation can be between 0 and 3 (two bits) and is stored
                in every process identifier sent to another node. </p>
              <p>In a system with TCP based distribution, this data is
                kept in the <strong>Erlang port mapper daemon</strong>
                (<span class="code">epmd</span>), which is contacted when a distributed
                node starts. The lock-file and a convention for the UDS
                listen socket's name will remove the need for
                <span class="code">epmd</span> when using this distribution module. UDS
                is always restricted to one host, why avoiding a port
                mapper is easy.</p>
            </li>
          </ul>
        </li>
        <li>creation - The creation number for a listen socket, which is
         calculated as (the value found in the lock-file + 1) rem
         4. This creation value is also written back into the
         lock-file, so that the next invocation of the emulator will
         found our value in the file.</li>
        <li>type - The current type/state of the port, which can be one
         of the values declared above.</li>
        <li>name - The name of the socket file (the path prefix
         removed), which allows for deletion (<span class="code">unlink</span>) when the
         socket is closed.</li>
        <li>sent - How many bytes that have been sent over the
         socket. This may wrap, but that's no problem for the
         distribution, as the only thing that interests the Erlang
         distribution is if this value has changed (the Erlang
         net_kernel <strong>ticker</strong> uses this value by calling the
         driver to fetch it, which is done through the
	 <span class="code">erlang:port_control</span> routine).</li>
        <li>received - How many bytes that are read (received) from the
         socket, used in similar ways as <span class="code">sent</span>.</li>
        <li>partner - A pointer to another port structure, which is
         either the listen port from which this port is accepting a
         connection or the other way around. The "partner relation"
         is always bidirectional.</li>
        <li>next - Pointer to next structure in a linked list of all
         port structures. This list is used when accepting
         connections and when the driver is unloaded.</li>
        <li>buffer_size, buffer_pos, header_pos, buffer - data for input
         buffering. Refer to the source code (in the kernel/examples
         directory) for details about the input buffering. That
         certainly goes beyond the scope of this document.</li>
      </ul>
    

    <h4>Selected parts of the distribution driver implementation</h4>
      
      <p>The distribution drivers implementation is not completely
        covered in this text, details about buffering and other things
        unrelated to driver writing are not explained. Likewise are
        some peculiarities of the UDS protocol not explained in
        detail. The chosen protocol is not important.</p>
      <p>Prototypes for the driver call-back routines can be found in
        the <span class="code">erl_driver.h</span> header file.</p>
      <p>The driver initialization routine is (usually) declared with a
        macro to make the driver easier to port between different
        operating systems (and flavours of systems). This is the only
        routine that has to have a well defined name. All other
        call-backs are reached through the driver structure. The macro
        to use is named <span class="code">DRIVER_INIT</span> and takes the driver name
        as parameter.</p>
      <div class="example"><pre>
(1) /* Beginning of linked list of ports */
(2) static UdsData *first_data;


(3) DRIVER_INIT(uds_drv)
(4) {
(5)     first_data = NULL;
(6)     return &amp;uds_driver_entry;
(7) }      </pre></div>
      <p>The routine initializes the single global data structure and
        returns a pointer to the driver entry. The routine will be
        called when <span class="code">erl_ddll:load_driver</span> is called from Erlang.</p>
      <p>The <span class="code">uds_start</span> routine is called when a port is opened
        from Erlang. In our case, we only allocate a structure and
        initialize it. Creating the actual socket is left to the
        <span class="code">uds_command</span> routine.</p>
      <div class="example"><pre>
( 1) static ErlDrvData uds_start(ErlDrvPort port, char *buff)
( 2) {
( 3)     UdsData *ud;
( 4)     
( 5)     ud = ALLOC(sizeof(UdsData));
( 6)     ud-&gt;fd = -1;
( 7)     ud-&gt;lockfd = -1;
( 8)     ud-&gt;creation = 0;
( 9)     ud-&gt;port = port;
(10)     ud-&gt;type = portTypeUnknown;
(11)     ud-&gt;name = NULL;
(12)     ud-&gt;buffer_size = 0;
(13)     ud-&gt;buffer_pos = 0;
(14)     ud-&gt;header_pos = 0;
(15)     ud-&gt;buffer = NULL;
(16)     ud-&gt;sent = 0;
(17)     ud-&gt;received = 0;
(18)     ud-&gt;partner = NULL;
(19)     ud-&gt;next = first_data;
(20)     first_data = ud;
(21)     
(22)     return((ErlDrvData) ud);
(23) }      </pre></div>
      <p>Every data item is initialized, so that no problems will arise
        when a newly created port is closed (without there being any
        corresponding socket). This routine is called when
        <span class="code">open_port({spawn, "uds_drv"},[])</span> is called from Erlang.</p>
      <p>The <span class="code">uds_command</span> routine is the routine called when an
        Erlang process sends data to the port. All asynchronous
        commands when the port is in <strong>command mode</strong> as well as 
        the sending of all data when the port is in <strong>data mode</strong>
        is handled in this9s routine. Let's have a look at it:</p>
      <div class="example"><pre>
( 1) static void uds_command(ErlDrvData handle, char *buff, int bufflen)
( 2) {
( 3)     UdsData *ud = (UdsData *) handle;

( 4)     if (ud-&gt;type == portTypeData || ud-&gt;type == portTypeIntermediate) {
( 5)         DEBUGF(("Passive do_send %d",bufflen));
( 6)         do_send(ud, buff + 1, bufflen - 1); /* XXX */
( 7)         return;
( 8)     } 
( 9)     if (bufflen == 0) {
(10)         return;
(11)     }
(12)     switch (*buff) {
(13)     case 'L':
(14)         if (ud-&gt;type != portTypeUnknown) {
(15)             driver_failure_posix(ud-&gt;port, ENOTSUP);
(16)             return;
(17)         }
(18)         uds_command_listen(ud,buff,bufflen);
(19)         return;
(20)     case 'A':
(21)         if (ud-&gt;type != portTypeUnknown) {
(22)             driver_failure_posix(ud-&gt;port, ENOTSUP);
(23)             return;
(24)         }
(25)         uds_command_accept(ud,buff,bufflen);
(26)         return;
(27)     case 'C':
(28)         if (ud-&gt;type != portTypeUnknown) {
(29)             driver_failure_posix(ud-&gt;port, ENOTSUP);
(30)             return;
(31)         }
(32)         uds_command_connect(ud,buff,bufflen);
(33)         return;
(34)     case 'S':
(35)         if (ud-&gt;type != portTypeCommand) {
(36)             driver_failure_posix(ud-&gt;port, ENOTSUP);
(37)             return;
(38)         }
(39)         do_send(ud, buff + 1, bufflen - 1);
(40)         return;
(41)     case 'R':
(42)         if (ud-&gt;type != portTypeCommand) {
(43)             driver_failure_posix(ud-&gt;port, ENOTSUP);
(44)             return;
(45)         }
(46)         do_recv(ud);
(47)         return;
(48)     default:
(49)         return;
(50)     }
(51) }      </pre></div>
      <p>The command routine takes three parameters; the handle
        returned for the port by <span class="code">uds_start</span>, which is a pointer
        to the internal port structure, the data buffer and the length
        of the data buffer. The buffer is the data sent from Erlang
        (a list of bytes) converted to an C array (of bytes). </p>
      <p>If Erlang sends i.e. the list <span class="code">[$a,$b,$c]</span> to the port,
        the <span class="code">bufflen</span> variable will be <span class="code">3</span> ant the
        <span class="code">buff</span> variable will contain <span class="code">{'a','b','c'}</span> (no
        null termination). Usually the first byte is used as an
        opcode, which is the case in our driver to (at least when the
        port is in command mode). The opcodes are defined as:</p>
      <ul>
        <li>'L'&lt;socketname&gt;: Create and listen on socket with the
         given name.</li>
        <li>'A'&lt;listennumber as 32 bit bigendian&gt;: Accept from the
         listen socket identified by the given identification
         number. The identification number is retrieved with the
         uds_control routine.</li>
        <li>'C'&lt;socketname&gt;: Connect to the socket named
         &lt;socketname&gt;.</li>
        <li>'S'&lt;data&gt;: Send the data &lt;data&gt; on the
         connected/accepted socket (in command mode). The sending is
         acked when the data has left this process.</li>
        <li>'R': Receive one packet of data.</li>
      </ul>
      <p>One may wonder what is meant by "one packet of data" in the
        'R' command. This driver always sends data packeted with a 4
        byte header containing a big endian 32 bit integer that
        represents the length of the data in the packet. There is no
        need for different packet sizes or some kind of streamed
        mode, as this driver is for the distribution only. One may
        wonder why the header word is coded explicitly in big endian
        when an UDS socket is local to the host. The answer simply is
        that I see it as a good practice when writing a distribution
        driver, as distribution in practice usually cross the host
        boundaries. </p>
      <p>On line 4-8 we handle the case where the port is in data or
        intermediate mode, the rest of the routine handles the
        different commands. We see (first on line 15) that the routine
        uses the <span class="code">driver_failure_posix()</span> routine to report
        errors. One important thing to remember is that the failure
        routines make a call to our <span class="code">uds_stop</span> routine, which
        will remove the internal port data. The handle (and the casted
        handle <span class="code">ud</span>) is therefore <strong>invalid pointers</strong> after a
        <span class="code">driver_failure</span> call and we should <strong>immediately return</strong>. The runtime system will send exit signals to all
        linked processes.</p>
      <p>The uds_input routine gets called when data is available on a
        file descriptor previously passed to the <span class="code">driver_select</span>
        routine. Typically this happens when a read command is issued
        and no data is available. Lets look at the <span class="code">do_recv</span>
        routine:</p>
      <div class="example"><pre>
( 1) static void do_recv(UdsData *ud)
( 2) {
( 3)     int res;
( 4)     char *ibuf;
( 5)     for(;;) {
( 6)         if ((res = buffered_read_package(ud,&amp;ibuf)) &lt; 0) {
( 7)             if (res == NORMAL_READ_FAILURE) {
( 8)                 driver_select(ud-&gt;port, (ErlDrvEvent) ud-&gt;fd, DO_READ, 1);
( 9)             } else {
(10)                 driver_failure_eof(ud-&gt;port);
(11)             }
(12)             return;
(13)         }
(14)         /* Got a package */
(15)         if (ud-&gt;type == portTypeCommand) {
(16)             ibuf[-1] = 'R'; /* There is always room for a single byte 
(17)                                opcode before the actual buffer 
(18)                                (where the packet header was) */
(19)             driver_output(ud-&gt;port,ibuf - 1, res + 1);
(20)             driver_select(ud-&gt;port, (ErlDrvEvent) ud-&gt;fd, DO_READ,0);
(21)             return;
(22)         } else {
(23)             ibuf[-1] = DIST_MAGIC_RECV_TAG; /* XXX */
(24)             driver_output(ud-&gt;port,ibuf - 1, res + 1);
(25)             driver_select(ud-&gt;port, (ErlDrvEvent) ud-&gt;fd, DO_READ,1);
(26)         }
(27)     }
(28) }      </pre></div>
      <p>The routine tries to read data until a packet is read or the
        <span class="code">buffered_read_package</span> routine returns a
        <span class="code">NORMAL_READ_FAILURE</span> (an internally defined constant for
        the module that means that the read operation resulted in an
        <span class="code">EWOULDBLOCK</span>). If the port is in command mode, the
        reading stops when one package is read, but if it is in data
        mode, the reading continues until the socket buffer is empty
        (read failure). If no more data can be read and more is wanted
        (always the case when socket is in data mode) driver_select is
        called to make the <span class="code">uds_input</span> call-back be called when
        more data is available for reading.</p>
      <p>When the port is in data mode, all data is sent to Erlang in a
        format that suits the distribution, in fact the raw data will
        never reach any Erlang process, but will be
        translated/interpreted by the emulator itself and then
        delivered in the correct format to the correct processes. In
        the current emulator version, received data should be tagged
        with a single byte of 100. Thats what the macro
        <span class="code">DIST_MAGIC_RECV_TAG</span> is defined to. The tagging of data
        in the distribution will possibly change in the future.</p>
      <p>The <span class="code">uds_input</span> routine will handle other input events
        (like nonblocking <span class="code">accept</span>), but most importantly handle
        data arriving at the socket by calling <span class="code">do_recv</span>:</p>
      <div class="example"><pre>
( 1) static void uds_input(ErlDrvData handle, ErlDrvEvent event)
( 2) {
( 3)     UdsData *ud = (UdsData *) handle;

( 4)     if (ud-&gt;type == portTypeListener) {
( 5)         UdsData *ad = ud-&gt;partner;
( 6)         struct sockaddr_un peer;
( 7)         int pl = sizeof(struct sockaddr_un);
( 8)         int fd;

( 9)         if ((fd = accept(ud-&gt;fd, (struct sockaddr *) &amp;peer, &amp;pl)) &lt; 0) {
(10)             if (errno != EWOULDBLOCK) {
(11)                 driver_failure_posix(ud-&gt;port, errno);
(12)                 return;
(13)             }
(14)             return;
(15)         }
(16)         SET_NONBLOCKING(fd);
(17)         ad-&gt;fd = fd;
(18)         ad-&gt;partner = NULL;
(19)         ad-&gt;type = portTypeCommand;
(20)         ud-&gt;partner = NULL;
(21)         driver_select(ud-&gt;port, (ErlDrvEvent) ud-&gt;fd, DO_READ, 0);
(22)         driver_output(ad-&gt;port, "Aok",3);
(23)         return;
(24)     }
(25)     do_recv(ud);
(26) }      </pre></div>
      <p>The important line here is the last line in the function, the
        <span class="code">do_read</span> routine is called to handle new input. The rest
        of the function handles input on a listen socket, which means
        that there should be possible to do an accept on the
        socket, which is also recognized as a read event.</p>
      <p>The output mechanisms are similar to the input. Lets first
        look at the <span class="code">do_send</span> routine:</p>
      <div class="example"><pre>
( 1) static void do_send(UdsData *ud, char *buff, int bufflen) 
( 2) {
( 3)     char header[4];
( 4)     int written;
( 5)     SysIOVec iov[2];
( 6)     ErlIOVec eio;
( 7)     ErlDrvBinary *binv[] = {NULL,NULL};

( 8)     put_packet_length(header, bufflen);
( 9)     iov[0].iov_base = (char *) header;
(10)     iov[0].iov_len = 4;
(11)     iov[1].iov_base = buff;
(12)     iov[1].iov_len = bufflen;
(13)     eio.iov = iov;
(14)     eio.binv = binv;
(15)     eio.vsize = 2;
(16)     eio.size = bufflen + 4;
(17)     written = 0;
(18)     if (driver_sizeq(ud-&gt;port) == 0) {
(19)         if ((written = writev(ud-&gt;fd, iov, 2)) == eio.size) {
(20)             ud-&gt;sent += written;
(21)             if (ud-&gt;type == portTypeCommand) {
(22)                 driver_output(ud-&gt;port, "Sok", 3);
(23)             }
(24)             return;
(25)         } else if (written &lt; 0) {
(26)             if (errno != EWOULDBLOCK) {
(27)                 driver_failure_eof(ud-&gt;port);
(28)                 return;
(29)             } else {
(30)                 written = 0;
(31)             }
(32)         } else {
(33)             ud-&gt;sent += written;
(34)         }
(35)         /* Enqueue remaining */
(36)     }
(37)     driver_enqv(ud-&gt;port, &amp;eio, written);
(38)     send_out_queue(ud);
(39) }      </pre></div>
      <p>This driver uses the <span class="code">writev</span> system call to send data
        onto the socket. A combination of writev and the driver output
        queues is very convenient. An <strong>ErlIOVec</strong> structure
        contains a <strong>SysIOVec</strong> (which is equivalent to the
        <span class="code">struct iovec</span> structure defined in <span class="code">uio.h</span>. The
        ErlIOVec also contains an array of <strong>ErlDrvBinary</strong>
        pointers, of the same length as the number of buffers in the
        I/O vector itself. One can use this to allocate the binaries
        for the queue "manually" in the driver, but we'll just fill
        the binary array with NULL values (line 7) , which will make
        the runtime system allocate its own buffers when we call
        <span class="code">driver_enqv</span> (line 37).</p>
      <p></p>
      <p>The routine builds an I/O vector containing the header bytes
        and the buffer (the opcode has been removed and the buffer
        length decreased by the output routine). If the queue is
        empty, we'll write the data directly to the socket (or at
        least try to). If any data is left, it is stored in the queue
        and then we try to send the queue (line 38). An ack is sent
        when the message is delivered completely (line 22). The
        <span class="code">send_out_queue</span> will send acks if the sending is
        completed there. If the port is in command mode, the Erlang
        code serializes the send operations so that only one packet
        can be waiting for delivery at a time. Therefore the ack can
        be sent simply whenever the queue is empty.</p>
      <p></p>
      <p>A short look at the <span class="code">send_out_queue</span> routine:</p>
      <div class="example"><pre>
( 1) static int send_out_queue(UdsData *ud)
( 2) {
( 3)     for(;;) {
( 4)         int vlen;
( 5)         SysIOVec *tmp = driver_peekq(ud-&gt;port, &amp;vlen);
( 6)         int wrote;
( 7)         if (tmp == NULL) {
( 8)             driver_select(ud-&gt;port, (ErlDrvEvent) ud-&gt;fd, DO_WRITE, 0);
( 9)             if (ud-&gt;type == portTypeCommand) {
(10)                 driver_output(ud-&gt;port, "Sok", 3);
(11)             }
(12)             return 0;
(13)         }
(14)         if (vlen &gt; IO_VECTOR_MAX) {
(15)             vlen = IO_VECTOR_MAX;
(16)         } 
(17)         if ((wrote = writev(ud-&gt;fd, tmp, vlen)) &lt; 0) {
(18)             if (errno == EWOULDBLOCK) {
(19)                 driver_select(ud-&gt;port, (ErlDrvEvent) ud-&gt;fd, 
(20)                               DO_WRITE, 1);
(21)                 return 0;
(22)             } else {
(23)                 driver_failure_eof(ud-&gt;port);
(24)                 return -1;
(25)             }
(26)         }
(27)         driver_deq(ud-&gt;port, wrote);
(28)         ud-&gt;sent += wrote;
(29)     }
(30) }      </pre></div>
      <p>What we do is simply to pick out an I/O vector from the queue
        (which is the whole queue as an <strong>SysIOVec</strong>). If the I/O
        vector is to long (IO_VECTOR_MAX is defined to 16), the vector
        length is decreased (line 15), otherwise the <span class="code">writev</span>
        (line 17) call will
        fail. Writing is tried and anything written is dequeued (line
        27). If the write fails with <span class="code">EWOULDBLOCK</span> (note that all
        sockets are in nonblocking mode), <span class="code">driver_select</span> is
        called to make the <span class="code">uds_output</span> routine be called when
        there is space to write again.</p>
      <p>We will continue trying to write until the queue is empty or
        the writing would block.</p>
      <p>The routine above are called from the <span class="code">uds_output</span>
        routine, which looks like this:</p>
      <div class="example"><pre>
( 1) static void uds_output(ErlDrvData handle, ErlDrvEvent event)
( 2) {
( 3)    UdsData *ud = (UdsData *) handle;
( 4)    if (ud-&gt;type == portTypeConnector) {
( 5)        ud-&gt;type = portTypeCommand;
( 6)        driver_select(ud-&gt;port, (ErlDrvEvent) ud-&gt;fd, DO_WRITE, 0);
( 7)        driver_output(ud-&gt;port, "Cok",3);
( 8)        return;
( 9)    }
(10)    send_out_queue(ud);
(11) }      </pre></div>
      <p>The routine is simple, it first handles the fact that the
        output select will concern a socket in the business of
        connecting (and the connecting blocked). If the socket is in
        a connected state it simply sends the output queue, this
        routine is called when there is possible to write to a socket
        where we have an output queue, so there is no question what to
        do.</p>
      <p>The driver implements a control interface, which is a
        synchronous interface called when Erlang calls
        <span class="code">erlang:port_control/3</span>. This is the only interface
        that can control the driver when it is in data mode and it may
        be called with the following opcodes:</p>
      <ul>
        <li>'C': Set port in command mode.</li>
        <li>'I': Set port in intermediate mode.</li>
        <li>'D': Set port in data mode.</li>
        <li>'N': Get identification number for listen port, this
         identification number is used in an accept command to the
         driver, it is returned as a big endian 32 bit integer, which
         happens to be the file identifier for the listen socket.</li>
        <li>'S': Get statistics, which is the number of bytes received,
         the number of bytes sent and the number of bytes pending in
         the output queue. This data is used when the distribution
         checks that a connection is alive (ticking). The statistics
         is returned as 3 32 bit big endian integers.</li>
        <li>'T': Send a tick message, which is a packet of length
         0. Ticking is done when the port is in data mode, so the
         command for sending data cannot be used (besides it ignores
         zero length packages in command mode). This is used by the
         ticker to send dummy data when no other traffic is present.
	 <strong>Note</strong> that it is important that the interface for
	 sending ticks is not blocking. This implementation uses
	 <span class="code">erlang:port_control/3</span> which does not block the caller.
	 If <span class="code">erlang:port_command</span> is used, use
	 <span class="code">erlang:port_command/3</span> and pass <span class="code">[force]</span> as
	 option list; otherwise, the caller can be blocked indefinitely
	 on a busy port and prevent the system from taking down a
	 connection that is not functioning.</li>
        <li>'R': Get creation number of listen socket, which is used to
         dig out the number stored in the lock file to differentiate
         between invocations of Erlang nodes with the same name.</li>
      </ul>
      <p>The control interface gets a buffer to return its value in,
        but is free to allocate its own buffer is the provided one is
        to small. Here is the code for <span class="code">uds_control</span>:</p>
      <div class="example"><pre>
( 1) static int uds_control(ErlDrvData handle, unsigned int command, 
( 2)                        char* buf, int count, char** res, int res_size)
( 3) {
( 4) /* Local macro to ensure large enough buffer. */
( 5) #define ENSURE(N)                               \
( 6)    do {                                         \
( 7)        if (res_size &lt; N) {                      \
( 8)            *res = ALLOC(N);                     \
( 9)        }                                        \
(10)    } while(0)

(11)    UdsData *ud = (UdsData *) handle;

(12)    switch (command) {
(13)    case 'S':
(14)        {
(15)            ENSURE(13);
(16)            **res = 0;
(17)            put_packet_length((*res) + 1, ud-&gt;received);
(18)            put_packet_length((*res) + 5, ud-&gt;sent);
(19)            put_packet_length((*res) + 9, driver_sizeq(ud-&gt;port));
(20)            return 13;
(21)        }
(22)    case 'C':
(23)        if (ud-&gt;type &lt; portTypeCommand) {
(24)            return report_control_error(res, res_size, "einval");
(25)        }
(26)        ud-&gt;type = portTypeCommand;
(27)        driver_select(ud-&gt;port, (ErlDrvEvent) ud-&gt;fd, DO_READ, 0);
(28)        ENSURE(1);
(29)        **res = 0;
(30)        return 1;
(31)    case 'I':
(32)        if (ud-&gt;type &lt; portTypeCommand) {
(33)            return report_control_error(res, res_size, "einval");
(34)        }
(35)        ud-&gt;type = portTypeIntermediate;
(36)        driver_select(ud-&gt;port, (ErlDrvEvent) ud-&gt;fd, DO_READ, 0);
(37)        ENSURE(1);
(38)        **res = 0;
(39)        return 1;
(40)    case 'D':
(41)        if (ud-&gt;type &lt; portTypeCommand) {
(42)            return report_control_error(res, res_size, "einval");
(43)        }
(44)        ud-&gt;type = portTypeData;
(45)        do_recv(ud);
(46)        ENSURE(1);
(47)        **res = 0;
(48)        return 1;
(49)    case 'N':
(50)        if (ud-&gt;type != portTypeListener) {
(51)            return report_control_error(res, res_size, "einval");
(52)        }
(53)        ENSURE(5);
(54)        (*res)[0] = 0;
(55)        put_packet_length((*res) + 1, ud-&gt;fd);
(56)        return 5;
(57)    case 'T': /* tick */
(58)        if (ud-&gt;type != portTypeData) {
(59)            return report_control_error(res, res_size, "einval");
(60)        }
(61)        do_send(ud,"",0);
(62)        ENSURE(1);
(63)        **res = 0;
(64)        return 1;
(65)    case 'R':
(66)        if (ud-&gt;type != portTypeListener) {
(67)            return report_control_error(res, res_size, "einval");
(68)        }
(69)        ENSURE(2);
(70)        (*res)[0] = 0;
(71)        (*res)[1] = ud-&gt;creation;
(72)        return 2;
(73)    default:
(74)        return report_control_error(res, res_size, "einval");
(75)    }
(76) #undef ENSURE
(77) }      </pre></div>
      <p>The macro <span class="code">ENSURE</span> (line 5 to 10) is used to ensure that
        the buffer is large enough for our answer. We switch on the
        command and take actions, there is not much to say about this
        routine. Worth noting is that we always has read select active
        on a port in data mode (achieved by calling <span class="code">do_recv</span> on
        line 45), but turn off read selection in intermediate and
        command modes (line 27 and 36).</p>
      <p>The rest of the driver is more or less UDS specific and not of
        general interest.</p>
    
  

  <h3><a name="id79511">3.3 
        Putting it all together</a></h3>
    
    <p>To test the distribution, one can use the
      <span class="code">net_kernel:start/1</span> function, which is useful as it starts
      the distribution on a running system, where tracing/debugging
      can be performed. The <span class="code">net_kernel:start/1</span> routine takes a
      list as its single argument. The lists first element should be
      the node name (without the "@hostname") as an atom, and the second (and
      last) element should be one of the atoms <span class="code">shortnames</span> or 
      <span class="code">longnames</span>. In the example case <span class="code">shortnames</span> is
      preferred. </p>
    <p>For net kernel to find out which distribution module to use, the
      command line argument <span class="code">-proto_dist</span> is used. The argument
      is followed by one or more distribution module names, with the
      "_dist" suffix removed, i.e. uds_dist as a distribution module
      is specified as <span class="code">-proto_dist uds</span>.</p>
    <p>If no epmd (TCP port mapper daemon) is used, one should also
      specify the command line option <span class="code">-no_epmd</span>, which will make
      Erlang skip the epmd startup, both as a OS process and as an
      Erlang ditto.</p>
    <p>The path to the directory where the distribution modules reside
      must be known at boot, which can either be achieved by
      specifying <span class="code">-pa &lt;path&gt;</span> on the command line or by building
      a boot script containing the applications used for your
      distribution protocol (in the uds_dist protocol, it's only the
      uds_dist application that needs to be added to the script).</p>
    <p>The distribution will be started at boot if all the above is
      specified and an <span class="code">-sname &lt;name&gt;</span> flag is present at the
      command line, here follows two examples: </p>
    <div class="example"><pre>
$ <span class="bold_code">erl -pa $ERL_TOP/lib/kernel/examples/uds_dist/ebin -proto_dist uds -no_epmd</span>
Erlang (BEAM) emulator version 5.0 
 
Eshell V5.0  (abort with ^G)
1&gt; <span class="bold_code">net_kernel:start([bing,shortnames]).</span>
{ok,&lt;0.30.0&gt;}
(bing@hador)2&gt;</pre></div>
    <p>...</p>
    <div class="example"><pre>
$ <span class="bold_code">erl -pa $ERL_TOP/lib/kernel/examples/uds_dist/ebin -proto_dist uds \ </span>
<span class="bold_code">      -no_epmd -sname bong</span>
Erlang (BEAM) emulator version 5.0 
 
Eshell V5.0  (abort with ^G)
(bong@hador)1&gt;</pre></div>
    <p>One can utilize the ERL_FLAGS environment variable to store the
      complicated parameters in:</p>
    <div class="example"><pre>
$ <span class="bold_code">ERL_FLAGS=-pa $ERL_TOP/lib/kernel/examples/uds_dist/ebin \ </span>
<span class="bold_code">      -proto_dist uds -no_epmd</span>
$ <span class="bold_code">export ERL_FLAGS</span>
$ <span class="bold_code">erl -sname bang</span>
Erlang (BEAM) emulator version 5.0 
 
Eshell V5.0  (abort with ^G)
(bang@hador)1&gt;</pre></div>
    <p>The <span class="code">ERL_FLAGS</span> should preferably not include the name of
      the node.</p>
  
</div>
<div class="footer">
<hr>
<p>Copyright © 1997-2012 Ericsson AB. All Rights Reserved.</p>
</div>
</div>
</div></body>
</html>