home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / UUCP_Blars.lzh / DOC / uucp_g.doc next >
Text File  |  1990-02-11  |  19KB  |  364 lines

  1.  
  2.  
  3.  
  4.                              Packet Driver Protocol
  5.  
  6.                                  G. L. Chesson
  7.                                Bell Laboratories
  8.  
  9.           Abstract
  10.  
  11.                     These notes describe the packet driver link proto-
  12.                col that was supplied with the Seventh Edition of UNIX*
  13.                and is used by the UUCP program.
  14.  
  15.           General
  16.  
  17.                Information flow between a  pair  of  machines  may  be
  18.           regulated  by  first  representing  the  data  as  sequence-
  19.           numbered packets of data and then  establishing  conventions
  20.           that  govern the use of sequence numbers.  The PK, or packet
  21.           driver, protocol is a particular instance of  this  type  of
  22.           flow-control  discipline.   The  technique  depends  on  the
  23.           notion of a transmission window to determine upper and lower
  24.           bounds  for  valid  sequence  numbers.   The  transmitter is
  25.           allowed to retransmit packets having sequence numbers within
  26.           the  window  until  the receiver indicates that packets have
  27.           been correctly received.  Positive acknowledgement from  the
  28.           receiver  moves  the  window; negative acknowledgement or no
  29.           acknowledgement causes retransmission.   The  receiver  must
  30.           ignore  duplicate  transmission,  detect  the various errors
  31.           that may occur, and inform the transmitter when packets  are
  32.           correctly or incorrectly received.
  33.  
  34.                The following paragraphs describe the  packet  formats,
  35.           message exchanges, and framing used by the protocol as coded
  36.           in the UUCP  program  and  the  UNIX  kernel.   Although  no
  37.           attempt will be made here to present internal details of the
  38.           algorithms that were used, the checksum routine is  supplied
  39.           for the benefit of other implementors.
  40.  
  41.           Packet Formats
  42.  
  43.                The protocol is defined in terms of  message  transmis-
  44.           sions  of  8-bit  bytes.   Each message includes one control
  45.           byte plus a data segment of zero or more information  bytes.
  46.           The  allowed data segment sizes range between 32 and 4096 as
  47.           determined by the formula 32(2k) where k is a 3-bit  number.
  48.           The  packet  sequence numbers are likewise constrained to 3-
  49.           bits; i.e. counting proceeds modulo-8.
  50.  
  51.                The control byte is partitioned into  three  fields  as
  52.           depicted below.
  53.  
  54.                   bit  7  6  5  4  3  2  1  0
  55.                        t  t  x  x  x  y  y  y
  56.  
  57.           The  t  bits  indicate  a  packet  type  and  determine  the
  58.           interpretation  to be placed on the xxx and yyy fields.  The
  59.           various interpretations are as follows:
  60.  
  61.                     tt      interpretation
  62.  
  63.                     00      control packet
  64.                     10      data packet
  65.                     11      `short' data packet
  66.                     01      alternate channel
  67.  
  68.           A data segment accompanies all  non-control  packets.   Each
  69.           transmitter  is constrained to observe the maximum data seg-
  70.           ment size established during initial synchronization by  the
  71.           receiver  that  it  sends  to.  Type 10 packets have maximal
  72.           size data segments.  Type 11, or `short', packets have  zero
  73.           or more data bytes but less than the maximum.  The first one
  74.           or two bytes of the data  segment  of  a  short  packet  are
  75.           `count'  bytes that indicate the difference between the max-
  76.           imum size and the number of bytes in the short segment.   If
  77.           the difference is less than 127, one count byte is used.  If
  78.           the difference exceeds 127, then the low-order seven bits of
  79.           the  difference are put in the first data byte and the high-
  80.           order bit is set as an indicator that the remaining bits  of
  81.           the  difference are in the second byte.  Type 01 packets are
  82.           never used by UUCP and need not be discussed in detail here.
  83.  
  84.                The sequence number of a non-control packet is given by
  85.           the  xxx  field.   Control  packets  are not sequenced.  The
  86.           newest sequence number, excluding  duplicate  transmissions,
  87.           accepted  by  a  receiver is placed in the yyy field of non-
  88.           control packets sent to the `other' receiver.
  89.  
  90.                There are no  data  bytes  associated  with  a  control
  91.           packet,  the  xxx field is interpreted as a control message,
  92.           and the yyy field is a value accompanying the  control  mes-
  93.           sage.   The  control messages are listed below in decreasing
  94.           priority.  That is, if several control messages  are  to  be
  95.           sent, the lower-numbered ones are sent first.
  96.  
  97.                xxx     name    yyy
  98.  
  99.                1       CLOSE   n/a
  100.                2       RJ              last correctly received sequence number
  101.                3       SRJ             sequence number to retransmit
  102.                4       RR              last correctly received sequence number
  103.                5       INITC           window size
  104.                6       INITB           data segment size
  105.                7       INITA           window size
  106.  
  107.                The CLOSE message  indicates  that  the  communications
  108.           channel  is  to  be  shut  down.  The RJ, or reject, message
  109.           indicates that the receiver has detected an  error  and  the
  110.           sender should retransmit after using the yyy field to update
  111.           the window.  This mode of retransmission is usually referred
  112.           to  as  a  `go-back-N'  procedure.   The  SRJ,  or selective
  113.           reject, message carries with it the  sequence  number  of  a
  114.           particular  packet to be retransmitted.  The RR, or receiver
  115.           ready, message indicates that the receiver has  detected  no
  116.           errors;  the  yyy  field  updates  the sender's window.  The
  117.           INITA/B/C messages are used to set window and  data  segment
  118.           sizes.  Segment sizes are calculated by the formula 32(2yyy)
  119.           as mentioned above, and window sizes may range between 1 and
  120.           7.
  121.  
  122.                Measurements of the protocol running  on  communication
  123.           links  at rates up to 9600 baud showed that a window size of
  124.           2 is optimal given a packet  size  greater  than  32  bytes.
  125.           This  means that the link bandwidth can be fully utilized by
  126.           the software.  For this reason the SRJ  message  is  not  as
  127.           important  as  it  might  otherwise  be.  Therefore the UNIX
  128.           implementations no longer generate or respond  to  SRJ  mes-
  129.           sages.   It  is mentioned here for historical accuracy only,
  130.           and one may assume that SRJ is no longer part of the  proto-
  131.           col.
  132.  
  133.           Message Exchanges
  134.  
  135.                   Initialization
  136.  
  137.                Messages are exchanged between four  cooperating  enti-
  138.           ties:  two  senders  and two receivers.  This means that the
  139.           communication channel  is  thought  of  as  two  independent
  140.           half-duplex  data paths.  For example the window and segment
  141.           sizes need not be the same in each direction.
  142.  
  143.                Initial synchronization is accomplished with two  3-way
  144.           handshakes:  two  each  of  INITA/INITB/INITC.   Each sender
  145.           transmits INITA messages repeatedly.  When an INITA  message
  146.           is received, INITB is sent in return.  When an INITB message
  147.           is received and an INITB message has  been  sent,  an  INITC
  148.           message  is  sent.   The INITA and INITB messages carry with
  149.           them the packet and window size that each receiver wants  to
  150.           use,  and  the  senders  are  supposed  to  comply.   When a
  151.           receiver has seen all three INIT messages,  the  channel  is
  152.           considered to be open.
  153.  
  154.                It is possible to design  a  protocol  that  starts  up
  155.           using   fewer   messages  than  the  interlocked  handshakes
  156.           described above.  The  advantage  of  the  more  complicated
  157.           design  lies  in  its use as a research vehicle: the initial
  158.           handshake sequence is completely symmetric, a handshake  can
  159.           be initiated by one side of the link while the connection is
  160.           in use, and the software to do this can  utilize  code  that
  161.           would ordinarily be used only once at connection setup time.
  162.           These properties were used in experiments  with  dynamically
  163.           adjusted  parameters.   That  is attempts were made to adapt
  164.           the window and segment sizes to changes observed in  traffic
  165.           while a link was in use.  Other experiments used the initial
  166.           handshake  in a different way for  restarting  the  protocol
  167.           without  data loss after machine crashes.  These experiments
  168.           never worked well in the packet driver  and  basically  pro-
  169.           vided the impetus for other protocol designs.  The result as
  170.           far as UUCP is concerned  is  that  initial  synchronization
  171.           uses  the  two  3-way  handshakes, and the INIT messages are
  172.           ignored elsewhere.
  173.  
  174.                   Data Transport
  175.  
  176.                After initial  synchronization  each  receiver  sets  a
  177.           modulo-8  incrementing  counter  R  to 0; each sender sets a
  178.           similar counter S to 1.  The value of R is always the number
  179.           of  the most recent correctly received packet.  The value of
  180.           S is always the first sequence number in the output  window.
  181.           Let  W  denote window size.  Note that the value of W may be
  182.           different for each sender.
  183.  
  184.                A sender may transmit packets with sequence numbers  in
  185.           the  range  S  to  (S+W-1) mod-8.   At any particular time a
  186.           receiver expects arriving packets to  have  numbers  in  the
  187.           range  (R+1) mod-8  to  (R+W) mod-8.  Packets must arrive in
  188.           sequence number order are are only  acknowledged  in  order.
  189.           That  is, the `next' packet a receiver will acknowledge must
  190.           have sequence number (R+1) mod-8.
  191.  
  192.                A receiver acknowledges  receipt  of  data  packets  by
  193.           arranging  for  the value of its R counter to be sent across
  194.           the channel where it will be used to update  an  S  counter.
  195.           This is done in two ways.  If data is flowing in both direc-
  196.           tions across a channel then each receiver's current R  value
  197.           is  carried in the yyy field of non-control packets.  Other-
  198.           wise  when  there  is  no  bidirectional  data  flow,   each
  199.           receiver's R value is transmitted across the link as the yyy
  200.           field of an RR control packet.
  201.  
  202.                Error handling is up to the discretion of the receiver.
  203.           It  can ignore all errors in which case transmitter timeouts
  204.           must provide for retransmission.  The receiver may also gen-
  205.           erate  RJ error control packets.  The yyy field of an incom-
  206.           ing RJ message replaces the S value of the local sender  and
  207.           constitutes  a  request  for retransmission to start at that
  208.           sequence number.  The yyy field of an incoming  SRJ  message
  209.           selects a particular packet for retransmission.
  210.  
  211.                The resemblance between the flow control  procedure  in
  212.           the  packet driver and that defined for X.25 is no accident.
  213.           The packet driver protocol  began  life  as  an  attempt  at
  214.           cleaning  up  X.25.   That  is  why,  for  example,  control
  215.           information is uniform in length (one byte), there is no RNR
  216.           message  (not  needed), and there is but one timeout defined
  217.           in the sender.
  218.  
  219.                   Termination
  220.  
  221.                The CLOSE message is used to terminate  communications.
  222.           Software on either or both ends of the communication channel
  223.           may initiate termination.  In any case when one end wants to
  224.           terminate it sends CLOSE messages until one is received from
  225.           the other end or until a programmable limit on the number of
  226.           CLOSE  messages  is  reached.   Receipt  of  a CLOSE message
  227.           causes a CLOSE message to be sent.  In the UNIX  environment
  228.           it  also  causes  the  SIGPIPE or `broken pipe' signal to be
  229.           sent to the local process using the communication channel.
  230.  
  231.                   Framing
  232.  
  233.                The term framing is used to  denote  the  technique  by
  234.           which  the  beginning  and end of a message is detected in a
  235.           byte stream; error  control  denotes  the  method  by  which
  236.           transmission  errors  are  detected.  Strategies for framing
  237.           and error control depend upon additional  information  being
  238.           transmitted  along  with  the control byte and data segment,
  239.           and the choice of a particular strategy usually  depends  on
  240.           characteristics  of  input/output  devices  and transmission
  241.           media.
  242.  
  243.                Several framing techniques are in used in support of PK
  244.           protocol  implementations, not all of which can be described
  245.           in detail here.  The technique used on  asynchronous  serial
  246.           lines will be described.
  247.  
  248.                A six byte framing envelope is  constructed  using  the
  249.           control  byte C of a packet and five other bytes as depicted
  250.           below.
  251.                     <DLE><k><c0><c1><C><x>
  252.  
  253.           The <DLE> symbol denotes the ASCII ctrl/P character.  If the
  254.           envelope  is  to  be followed by a data segment, <k> has the
  255.           value log2(size) - 4; i.e. 1 <= k <= 8.  If k is 9, then the
  256.           envelope  represents  a  control  packet.  The <c0> and <c1>
  257.           bytes are the low-order and high-order bytes respectively of
  258.           0xAAAA  minus  a 16-bit checksum.  For control packets, this
  259.           16-bit checksum is the same as the control byte C.  For data
  260.           packets,  the  checksum  is calculated by the program below.
  261.           The <x> byte is the exclusive-or of  <k><c0><c1><C>.   Error
  262.           control  is  accomplished  by  checking  a  received framing
  263.           envelope for compliance with the definition, and comparing a
  264.           checksum function of the data segment with <c0><c1>.
  265.  
  266.                This particular framing strategy assumes data  segments
  267.           are constant-sized: the `unused' bytes in a short packet are
  268.           actually transmitted.  This  creates  a  certain  amount  of
  269.           overhead  which  can  be  eliminated  by  a more complicated
  270.           framing technique.  The advantage of this strategy  is  that
  271.           i/o  devices  can  be  programmed  to  take advantage of the
  272.           constant-sized framing envelopes and data segments.
  273.  
  274.                The checksum calculation is  displayed  below  as  a  C
  275.           function.   Note that the code is not truly portable because
  276.           the definitions of short and char are not  necessarily  uni-
  277.           form  across  all machines that might support this language.
  278.           This code assumes that short and  char  are  16  and  8-bits
  279.           respectively.
  280.  
  281.           /* [Original document's version corrected to actual version] */
  282.                chksum(s,n)
  283.                register char *s;
  284.                register n;
  285.                {
  286.                        register short sum;
  287.                        register unsigned short t;
  288.                        register short x;
  289.  
  290.                        sum = -1;
  291.                        x = 0;
  292.  
  293.                        do {
  294.                                if (sum<0) {
  295.                                        sum <<= 1;
  296.                                        sum++;
  297.                                } else
  298.                                        sum <<= 1;
  299.                                t = sum;
  300.                                sum += (unsigned)*s++ & 0377;
  301.                                x += sum^n;
  302.                                if ((unsigned short)sum <= t) {
  303.                                        sum ^= x;
  304.                                }
  305.                        } while (--n > 0);
  306.  
  307.                        return(sum);
  308.                }
  309.  
  310.           The checksum routine used in  gnuucp  has  been  updated  to
  311.           avoid  depending  on  the particular sizes of char and short
  312.           variables.  As long as a char holds 8 bits or  more,  and  a
  313.           short  holds  16  bits or more, the code will work.  To test
  314.           it, uncomment the ``#define short long'' below.  A good com-
  315.           piler  produces the same code from this function as from the
  316.           less portable version.
  317.  
  318.                #define HIGHBIT16       0x8000
  319.                #define JUST16BITS      0xFFFF
  320.                #define JUST8BITS       0x00FF
  321.                #define MAGIC           0125252 /* checksum is subtracted
  322.                                                   from this */
  323.                int
  324.                pktchksum(msg, bytes)
  325.                        unsigned char *msg;
  326.                        int bytes;
  327.                {
  328.                        return (JUST16BITS &
  329.                               (MAGIC - (chksum(&msg[6], bytes)
  330.                               ^ (JUST8BITS & msg[4]))));
  331.                }
  332.  
  333.                int
  334.                chksum(s,n)
  335.                register unsigned char *s;
  336.                register n;
  337.                {
  338.                /* #define short long   /* To make sure it works with
  339.                                           shorts > 16 bits */
  340.                        register short sum;
  341.                        register unsigned short t;
  342.                        register short x;
  343.  
  344.                        sum = (-1) & JUST16BITS;
  345.                        x = 0;
  346.                        do {
  347.                      /* Rotate "sum" left by 1 bit, in a 16-bit barrel */
  348.                                if (sum & HIGHBIT16)
  349.                                {
  350.                                        sum = (1 + (sum << 1)) & JUST16BITS;
  351.                                }
  352.                                else
  353.                                        sum <<= 1;
  354.                                t = sum;
  355.                                sum = (sum + (*s++ & JUST8BITS)) & JUST16BITS;
  356.                                x += sum ^ n;
  357.                                if ((unsigned short)sum <= t)
  358.                                        sum = (sum ^ x) & JUST16BITS;
  359.                        } while (--n > 0);
  360.  
  361.                        return(sum);
  362.                #undef short            /* End of debugging check */
  363.                }
  364.