home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / std_unix / volume.14 / text0011.txt < prev    next >
Encoding:
Internet Message Format  |  1989-01-07  |  15.1 KB

  1. From: uunet!uts.amdahl.com!gam (Gordon Moffett)
  2.  
  3. [ This is Greg Chesson's packet driver protocol (UUCP g protocol) paper
  4. that I posted a year ago.  It originally came from John Gilmore, who has
  5. since appended some useful code, thus this updated reposting.  -mod ]
  6.  
  7. .\" @(#) packet.driver.ms    Version hoptoad-1.4    88/03/24
  8. .\"
  9. .\" format this with [nt]roff -ms.
  10. .\"
  11. .\" From: greg@sgi.uucp (Greg Chesson)
  12. .\" Newsgroups: mod.std.unix
  13. .\" Volume-Number: Volume 9, Number 55
  14. .\" Subject: Packet Driver Protocol
  15. .\" Message-ID: <7136@ut-sally.UUCP>
  16. .\" Date: 11 Feb 87 23:44:09 GMT
  17. .\"
  18. .\" This message contains a copy of ``Packet Driver Protocol,''
  19. .\" written by G. L. Chesson while he was at Bell Laboratories.
  20. .\" He remarks that it was approved for public distribution, and that
  21. .\" 
  22. .\"     The version of the note that you probably have omits the
  23. .\"     detail that the transmitted checksum is really 0125252
  24. .\"     - the block checksum function.
  25. .\" 
  26. .\" [Note that 0125252 is 0xAAAA, which is easier to remember.
  27. .\" I have folded this update into the document. -- hoptoad!gnu]
  28. .\"
  29. .\" [I have also updated the checksum routine to include one that
  30. .\" works regardless of the size of a "short" or an "int". -- hoptoad!gnu]
  31. .ce
  32. .B
  33. Packet Driver Protocol
  34. .R
  35. .sp 1
  36. .ce
  37. G. L. Chesson
  38. .br
  39. .ce
  40. Bell Laboratories
  41. .SH
  42. Abstract
  43. .in +.5i
  44. .PP
  45. These notes describe the packet driver link
  46. protocol that was supplied
  47. with the
  48. Seventh Edition of
  49. .UX
  50. and is used by the UUCP program.
  51. .in -.5i
  52. .SH
  53. General
  54. .PP
  55. Information flow between a pair of machines
  56. may be regulated by
  57. first
  58. representing the data 
  59. as sequence-numbered 
  60. .I
  61. packets
  62. .R
  63. of data 
  64. and then establishing conventions that
  65. govern the use of sequence numbers.
  66. The
  67. .I
  68. PK,
  69. .R
  70. or
  71. .I
  72. packet driver,
  73. .R
  74. protocol
  75. is a particular instance of this type of
  76. flow-control discipline.
  77. The technique depends on the notion of a transmission
  78. .I
  79. window
  80. .R
  81. to determine upper and lower bounds for valid
  82. sequence numbers.
  83. The transmitter is allowed to retransmit packets
  84. having sequence numbers
  85. within the window until the receiver indicates that
  86. packets have been correctly received.
  87. Positive acknowledgement from the receiver moves the
  88. window;
  89. negative acknowledgement or no acknowledgement
  90. causes retransmission.
  91. The receiver must ignore duplicate transmission, detect
  92. the various errors that may occur,
  93. and inform the transmitter when packets are 
  94. correctly or incorrectly received.
  95. .PP
  96. The following paragraphs describe the packet formats,
  97. message exchanges,
  98. and framing
  99. used by the protocol as coded
  100. in the UUCP program and the
  101. .UX
  102. kernel.
  103. Although no attempt will be made here to present
  104. internal details of the algorithms that were used,
  105. the checksum routine is supplied
  106. for the benefit of other implementors.
  107. .SH
  108. Packet Formats
  109. .PP
  110. The protocol is defined in terms of message
  111. transmissions of 8-bit bytes.
  112. Each message includes one
  113. .I
  114. control
  115. .R
  116. byte plus a
  117. .I
  118. data segment
  119. .R
  120. of zero or more information bytes.
  121. The allowed data segment sizes range
  122. between 32 and 4096 as determined by the formula
  123. 32(2\uk\d) where
  124. k is a 3-bit number.
  125. The packet sequence numbers are likewise constrained
  126. to 3-bits; i.e. counting proceeds modulo-8.
  127. .PP
  128. The control byte is partitioned into three fields as
  129. depicted below.
  130. .bp
  131. .nf
  132. .sp 
  133. .in 1i
  134. .ls 1
  135. bit    7    6    5    4    3    2    1    0
  136.     t    t    x    x    x    y    y    y
  137. .ls 1
  138. .in -1i
  139. .fi
  140. .sp
  141. The
  142. .I
  143. t
  144. .R
  145. bits indicate a packet type and
  146. determine the interpretation to be placed on
  147. the
  148. .I
  149. xxx
  150. .R
  151. and
  152. .I
  153. yyy
  154. .R
  155. fields.
  156. The various interpretations are as follows:
  157. .in +1i
  158. .sp
  159. .nf
  160. .ls 1
  161. .I
  162. tt    interpretation
  163. .sp
  164. .R
  165. 00    control packet
  166. 10    data packet
  167. 11    `short' data packet
  168. 01    alternate channel
  169. .ls 1
  170. .fi
  171. .sp
  172. .in -1i
  173. A data segment accompanies all non-control packets.
  174. Each transmitter is constrained to observe the maximum
  175. data segment size
  176. established during initial synchronization by the
  177. receiver that it sends to.
  178. Type 10 packets have maximal size data segments.
  179. Type 11, or `short', packets have zero or more data
  180. bytes but less than the maximum.
  181. The first one or two bytes of the data segment of a
  182. short packet are `count' bytes that
  183. indicate the difference between the
  184. maximum size and the number of bytes in the short
  185. segment.
  186. If the difference is less than 127, one count
  187. byte is used.
  188. If the difference exceeds 127,
  189. then the low-order seven bits of the difference
  190. are put in the first data byte and the high-order
  191. bit is set as an indicator that the remaining
  192. bits of the difference are in the second byte.
  193. Type 01 packets are never used by UUCP
  194. and need not be discussed in detail here.
  195. .PP
  196. The sequence number of a non-control packet is
  197. given by the
  198. .I
  199. xxx
  200. .R
  201. field.
  202. Control packets are not sequenced.
  203. The newest sequence number,
  204. excluding duplicate transmissions,
  205. accepted by a receiver is placed in the
  206. .I
  207. yyy
  208. .R
  209. field of non-control packets sent to the
  210. `other' receiver.
  211. .PP
  212. There are no data bytes associated with a control packet,
  213. the
  214. .I
  215. xxx
  216. .R
  217. field is interpreted as a control message,
  218. and the
  219. .I
  220. yyy
  221. .R
  222. field is a value accompanying the control message.
  223. The control messages are listed below in decreasing priority.
  224. That is, if several control messages are to be sent,
  225. the lower-numbered ones are sent first.
  226. .in +1i
  227. .nf
  228. .ls 1
  229. .sp
  230. .I
  231. xxx    name        yyy
  232. .R
  233.  
  234. 1    CLOSE    n/a
  235. 2    RJ        last correctly received sequence number
  236. 3    SRJ        sequence number to retransmit
  237. 4    RR        last correctly received sequence number
  238. 5    INITC    window size
  239. 6    INITB    data segment size
  240. 7    INITA    window size
  241. .in -i
  242. .ls 1
  243. .fi
  244. .sp
  245. .PP
  246. The CLOSE message indicates that the communications channel
  247. is to be shut down.
  248. The RJ, or
  249. .I
  250. reject,
  251. .R
  252. message indicates that the receiver has detected an error
  253. and the sender should retransmit after using the 
  254. .I
  255. yyy
  256. .R
  257. field to update the window.
  258. This mode of retransmission is usually
  259. referred to as a
  260. `go-back-N' procedure.
  261. The SRJ, or
  262. .I
  263. selective reject,
  264. .R
  265. message carries with it the sequence number of
  266. a particular packet to be retransmitted.
  267. The RR, or
  268. .I
  269. receiver ready,
  270. .R
  271. message indicates that the receiver has detected
  272. no errors; the
  273. .I
  274. yyy
  275. .R
  276. field updates the sender's window.
  277. The INITA/B/C messages are used
  278. to set window and data segment sizes.
  279. Segment sizes are calculated by the formula
  280. 32(2\uyyy\d)
  281. as mentioned above,
  282. and window sizes may range between 1 and 7.
  283. .PP
  284. Measurements of the protocol running on communication
  285. links at rates up to 9600 baud showed that
  286. a window size of 2 is optimal
  287. given a packet size greater than 32 bytes.
  288. This means that the link bandwidth can be fully utilized
  289. by the software.
  290. For this reason the SRJ message is not as important as it
  291. might otherwise be.
  292. Therefore the
  293. .UX
  294. implementations no longer generate or respond to SRJ
  295. messages.
  296. It is mentioned here for historical accuracy only,
  297. and one may assume that SRJ is no longer part of the protocol.
  298. .SH
  299. Message Exchanges
  300. .SH
  301.     Initialization
  302. .PP
  303. Messages are exchanged between four cooperating
  304. entities: two senders and two receivers.
  305. This means that the communication channel is thought of
  306. as two independent half-duplex data paths.
  307. For example the window and segment sizes need not
  308. be the same in each direction.
  309. .PP
  310. Initial synchronization is accomplished
  311. with two 3-way handshakes: two each of
  312. INITA/INITB/INITC.
  313. Each sender transmits INITA messages repeatedly.
  314. When an INITA message is received, INITB is
  315. sent in return.
  316. When an INITB message is received
  317. .I
  318. and
  319. .R
  320. an INITB message has been sent,
  321. an INITC message is sent.
  322. The INITA and INITB messages carry 
  323. with them the packet and window size that
  324. each receiver wants to use,
  325. and the senders are supposed to comply.
  326. When a receiver has seen all three
  327. INIT messages, the channel is 
  328. considered to be open.
  329. .PP
  330. It is possible to design a protocol that starts up using
  331. fewer messages than the interlocked handshakes described above.
  332. The advantage of the more complicated design lies in its use as
  333. a research vehicle:
  334. the initial handshake sequence is completely symmetric,
  335. a handshake
  336. can be initiated by one side of the link while the
  337. connection is in use, and the software to do this can
  338. utilize code that would ordinarily be used only once
  339. at connection setup time.
  340. These properties were used in experiments with dynamically
  341. adjusted parameters.
  342. That is attempts were made to adapt the window and segment
  343. sizes to changes observed in traffic while a link was in use.
  344. Other experiments used the initial
  345. handshake  in a different way
  346. for restarting the protocol without data loss
  347. after machine crashes.
  348. These experiments never worked well in the packet driver and
  349. basically provided the impetus for other protocol designs.
  350. The result 
  351. as far as UUCP is concerned is that initial synchronization
  352. uses the two 3-way handshakes, and the INIT
  353. messages are ignored elsewhere.
  354. .SH
  355.     Data Transport
  356. .PP
  357. After initial synchronization each receiver
  358. sets a modulo-8 incrementing counter R to 0;
  359. each sender sets a similar counter S to 1.
  360. The value of R is always the number of the most recent
  361. correctly received packet.
  362. The value of S is always the first sequence number in
  363. the output window.
  364. Let W denote window size.
  365. Note that the value of W may be different for each sender.
  366. .PP
  367. A sender may transmit packets with sequence numbers
  368. in the range S to (S+W-1)\ mod-8.
  369. At any particular time a receiver expects
  370. arriving packets to have numbers in the range
  371. (R+1)\ mod-8 to (R+W)\ mod-8.
  372. Packets must arrive in sequence number order
  373. are are only acknowledged in order.
  374. That is,
  375. the `next' packet a receiver
  376. will acknowledge must have
  377. sequence number (R+1)\ mod-8.
  378. .PP
  379. A receiver acknowledges receipt of data packets
  380. by arranging for the value of its R counter to be
  381. sent across the channel
  382. where it will be used to update an S counter.
  383. This is done in two ways.
  384. If data is flowing in both directions across a
  385. channel then each receiver's current R value is
  386. carried in the
  387. .I
  388. yyy
  389. .R
  390. field of non-control packets.
  391. Otherwise when there is no bidirectional
  392. data flow,
  393. each receiver's R value is transmitted across the link
  394. as the
  395. .I
  396. yyy
  397. .R
  398. field of an RR control packet.
  399. .PP
  400. Error handling is up to the discretion
  401. of the receiver.
  402. It can ignore all errors in which case
  403. transmitter timeouts must provide for
  404. retransmission.
  405. The receiver may also generate RJ 
  406. error control packets.
  407. The
  408. .I
  409. yyy
  410. .R
  411. field of an incoming RJ message replaces
  412. the S value of the local sender and
  413. constitutes a request for retransmission to start
  414. at that sequence number.
  415. The
  416. .I
  417. yyy
  418. .R
  419. field of an incoming SRJ message selects a particular
  420. packet for retransmission.
  421. .PP
  422. The resemblance between the flow control procedure in the
  423. packet driver and that defined for X.25 is no accident.
  424. The packet driver protocol began life as an attempt at
  425. cleaning up X.25.
  426. That is why, for example,
  427. control information is uniform in length (one byte),
  428. there is no RNR message (not needed),
  429. and there is but one timeout defined
  430. in the sender.
  431. .SH
  432.     Termination
  433. .PP
  434. The CLOSE message is used to terminate communications.
  435. Software on either or both ends of the communication
  436. channel may initiate termination.
  437. In any case when one end wants to terminate it sends
  438. CLOSE messages until one is received from the other end
  439. or until a programmable limit on the number of CLOSE
  440. messages is reached.
  441. Receipt of a CLOSE message causes a CLOSE message to be sent.
  442. In the 
  443. .UX
  444. environment
  445. it also causes the SIGPIPE or
  446. `broken pipe' signal to be sent to
  447. the local process using the communication channel.
  448. .SH
  449.     Framing
  450. .PP
  451. The term
  452. .I
  453. framing
  454. .R
  455. is used to denote the technique by which the
  456. beginning and end of a message is detected
  457. in a byte stream;
  458. .I
  459. error control
  460. .R
  461. denotes the method by which transmission
  462. errors are detected.
  463. Strategies for framing and error control depend
  464. upon
  465. additional information being transmitted along
  466. with the control byte and data segment,
  467. and the choice of a particular strategy usually
  468. depends on characteristics of input/output
  469. devices and transmission media.
  470. .PP
  471. Several framing techniques are in used in support
  472. of PK protocol implementations,
  473. not all of which can be described in detail here.
  474. The technique used on asynchronous serial lines
  475. will be described.
  476. .PP
  477. A six byte
  478. framing
  479. .I
  480. envelope
  481. .R
  482. is constructed using the control byte
  483. C of a packet and five other bytes as
  484. depicted below.
  485. .in +1i
  486. <DLE><k><c0><c1><C><x>
  487. .in -1i
  488. The <DLE> symbol denotes the ASCII ctrl/P character.
  489. If the envelope is to be followed by a data segment,
  490. <k> has the value
  491. log\d2\u(size)-4;
  492. i.e. 1 \(<= k \(<= 8.
  493. If k is 9, then the envelope represents a control packet.
  494. The <c0> and <c1> bytes are the low-order and high-order
  495. bytes respectively of 0xAAAA minus a 16-bit checksum.
  496. For control packets, this 16-bit checksum is the same
  497. as the control byte C.
  498. For data packets, the checksum is calculated by the 
  499. program below.
  500. The <x> byte is the exclusive-or of <k><c0><c1><C>.
  501. Error control is accomplished by checking 
  502. a received framing envelope for compliance with the definition,
  503. and comparing a checksum function of the data segment
  504. with <c0><c1>.
  505. .PP
  506. This particular framing strategy assumes data segments
  507. are constant-sized:
  508. the `unused' bytes in a short packet are actually
  509. transmitted.
  510. This creates a certain amount of overhead which
  511. can be eliminated by a more complicated framing technique.
  512. The advantage of this strategy is that i/o
  513. devices can be programmed to take advantage of the
  514. constant-sized framing envelopes and data segments.
  515. .bp
  516. .PP
  517. The checksum calculation is displayed below as a C function.
  518. Note that the code is not truly portable because
  519. the definitions of
  520. .I short
  521. and
  522. .I char
  523. are not necessarily uniform across all machines
  524. that might support this language.
  525. This code assumes that
  526. .I short
  527. and
  528. .I char
  529. are 16 and 8-bits respectively.
  530. .PP
  531. .in +.5i
  532. .nf
  533. .ft CW
  534. .ls 1
  535. /* [Original document's version corrected to actual version] */
  536. chksum(s,n)
  537. register char *s;
  538. register n;
  539. {
  540.     register short sum;
  541.     register unsigned short t;
  542.     register short x;
  543.  
  544.     sum = -1;
  545.     x = 0;
  546.  
  547.     do {
  548.         if (sum<0) {
  549.             sum <<= 1;
  550.             sum++;
  551.         } else
  552.             sum <<= 1;
  553.         t = sum;
  554.         sum += (unsigned)*s++ & 0377;
  555.         x += sum^n;
  556.         if ((unsigned short)sum <= t) {
  557.             sum ^= x;
  558.         }
  559.     } while (--n > 0);
  560.  
  561.     return(sum);
  562. }
  563.  
  564. .fi
  565. .in -.5i
  566. .ft
  567. The checksum routine used in gnuucp has been updated to avoid depending
  568. on the particular sizes of 
  569. .I char
  570. and
  571. .I short
  572. variables.  As long as a
  573. .I char
  574. holds 8 bits or more, and a
  575. .I short
  576. holds 16 bits or more, the code will work.
  577. To test it, uncomment the ``#define short long'' below.
  578. A good compiler produces the same code from this function as from
  579. the less portable version.
  580. .PP
  581. .in +.5i
  582. .nf
  583. .ft CW
  584. .ls 1
  585. #define    HIGHBIT16    0x8000
  586. #define    JUST16BITS    0xFFFF
  587. #define    JUST8BITS    0x00FF
  588. #define    MAGIC        0125252        /* checksum is subtracted from this */
  589.  
  590. int
  591. pktchksum(msg, bytes)
  592.     unsigned char *msg;
  593.     int bytes;
  594. {
  595.     return (JUST16BITS &
  596.         (MAGIC - (chksum(&msg[6], bytes) ^ (JUST8BITS & msg[4]))));
  597.     
  598. }
  599.  
  600.  
  601. int
  602. chksum(s,n)
  603. register unsigned char *s;
  604. register n;
  605. {
  606. /* #define short long    /* To make sure it works with shorts > 16 bits */
  607.     register short sum;
  608.     register unsigned short t;
  609.     register short x;
  610.  
  611.     sum = (-1) & JUST16BITS;
  612.     x = 0;
  613.     do {
  614.         /* Rotate "sum" left by 1 bit, in a 16-bit barrel */
  615.         if (sum & HIGHBIT16)
  616.         {
  617.             sum = (1 + (sum << 1)) & JUST16BITS;
  618.         }
  619.         else
  620.             sum <<= 1;
  621.         t = sum;
  622.         sum = (sum + (*s++ & JUST8BITS)) & JUST16BITS;
  623.         x += sum ^ n;
  624.         if ((unsigned short)sum <= t)
  625.             sum = (sum ^ x) & JUST16BITS;
  626.     } while (--n > 0);
  627.  
  628.     return(sum);
  629. #undef short        /* End of debugging check */
  630. }
  631. .fi
  632. .in -.5i
  633. .ft
  634.  
  635. Volume-Number: Volume 14, Number 13
  636.  
  637.