home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / share / doc / smm / 15.net / 6.t < prev    next >
Encoding:
Text File  |  1991-04-17  |  29.5 KB  |  665 lines

  1. .\" Copyright (c) 1983, 1986 The Regents of the University of California.
  2. .\" All rights reserved.
  3. .\"
  4. .\" Redistribution and use in source and binary forms, with or without
  5. .\" modification, are permitted provided that the following conditions
  6. .\" are met:
  7. .\" 1. Redistributions of source code must retain the above copyright
  8. .\"    notice, this list of conditions and the following disclaimer.
  9. .\" 2. Redistributions in binary form must reproduce the above copyright
  10. .\"    notice, this list of conditions and the following disclaimer in the
  11. .\"    documentation and/or other materials provided with the distribution.
  12. .\" 3. All advertising materials mentioning features or use of this software
  13. .\"    must display the following acknowledgement:
  14. .\"    This product includes software developed by the University of
  15. .\"    California, Berkeley and its contributors.
  16. .\" 4. Neither the name of the University nor the names of its contributors
  17. .\"    may be used to endorse or promote products derived from this software
  18. .\"    without specific prior written permission.
  19. .\"
  20. .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. .\" SUCH DAMAGE.
  31. .\"
  32. .\"    @(#)6.t    6.5 (Berkeley) 4/17/91
  33. .\"
  34. .nr H2 1
  35. .\".ds RH "Internal layering
  36. .br
  37. .ne 2i
  38. .NH
  39. \s+2Internal layering\s0
  40. .PP
  41. The internal structure of the network system is divided into
  42. three layers.  These
  43. layers correspond to the services provided by the socket
  44. abstraction, those provided by the communication protocols,
  45. and those provided by the hardware interfaces.  The communication
  46. protocols are normally layered into two or more individual
  47. cooperating layers, though they are collectively viewed
  48. in the system as one layer providing services supportive
  49. of the appropriate socket abstraction.
  50. .PP
  51. The following sections describe the properties of each layer
  52. in the system and the interfaces to which each must conform.
  53. .NH 2
  54. Socket layer
  55. .PP
  56. The socket layer deals with the interprocess communication
  57. facilities provided by the system.  A socket is a bidirectional
  58. endpoint of communication which is ``typed'' by the semantics
  59. of communication it supports.  The system calls described in
  60. the \fIBerkeley Software Architecture Manual\fP [Joy86]
  61. are used to manipulate sockets.
  62. .PP
  63. A socket consists of the following data structure:
  64. .DS
  65. ._f
  66. struct socket {
  67.     short    so_type;        /* generic type */
  68.     short    so_options;        /* from socket call */
  69.     short    so_linger;        /* time to linger while closing */
  70.     short    so_state;        /* internal state flags */
  71.     caddr_t    so_pcb;            /* protocol control block */
  72.     struct    protosw *so_proto;    /* protocol handle */
  73.     struct    socket *so_head;    /* back pointer to accept socket */
  74.     struct    socket *so_q0;        /* queue of partial connections */
  75.     short    so_q0len;        /* partials on so_q0 */
  76.     struct    socket *so_q;        /* queue of incoming connections */
  77.     short    so_qlen;        /* number of connections on so_q */
  78.     short    so_qlimit;        /* max number queued connections */
  79.     struct    sockbuf so_rcv;        /* receive queue */
  80.     struct    sockbuf so_snd;        /* send queue */
  81.     short    so_timeo;        /* connection timeout */
  82.     u_short    so_error;        /* error affecting connection */
  83.     u_short    so_oobmark;        /* chars to oob mark */
  84.     short    so_pgrp;        /* pgrp for signals */
  85. };
  86. .DE
  87. .PP
  88. Each socket contains two data queues, \fIso_rcv\fP and \fIso_snd\fP,
  89. and a pointer to routines which provide supporting services. 
  90. The type of the socket,
  91. \fIso_type\fP is defined at socket creation time and used in selecting
  92. those services which are appropriate to support it.  The supporting
  93. protocol is selected at socket creation time and recorded in
  94. the socket data structure for later use.  Protocols are defined
  95. by a table of procedures, the \fIprotosw\fP structure, which will
  96. be described in detail later.  A pointer to a protocol-specific
  97. data structure,
  98. the ``protocol control block,'' is also present in the socket structure.
  99. Protocols control this data structure, which normally includes a
  100. back pointer to the parent socket structure to allow easy
  101. lookup when returning information to a user 
  102. (for example, placing an error number in the \fIso_error\fP
  103. field).  The other entries in the socket structure are used in
  104. queuing connection requests, validating user requests, storing
  105. socket characteristics (e.g.
  106. options supplied at the time a socket is created), and maintaining
  107. a socket's state.
  108. .PP
  109. Processes ``rendezvous at a socket'' in many instances.  For instance,
  110. when a process wishes to extract data from a socket's receive queue
  111. and it is empty, or lacks sufficient data to satisfy the request,
  112. the process blocks, supplying the address of the receive queue as
  113. a ``wait channel' to be used in notification.  When data arrives
  114. for the process and is placed in the socket's queue, the blocked
  115. process is identified by the fact it is waiting ``on the queue.''
  116. .NH 3
  117. Socket state
  118. .PP
  119. A socket's state is defined from the following:
  120. .DS
  121. .ta \w'#define 'u +\w'SS_ISDISCONNECTING    'u +\w'0x000     'u
  122. #define    SS_NOFDREF    0x001    /* no file table ref any more */
  123. #define    SS_ISCONNECTED    0x002    /* socket connected to a peer */
  124. #define    SS_ISCONNECTING    0x004    /* in process of connecting to peer */
  125. #define    SS_ISDISCONNECTING    0x008    /* in process of disconnecting */
  126. #define    SS_CANTSENDMORE    0x010    /* can't send more data to peer */
  127. #define    SS_CANTRCVMORE    0x020    /* can't receive more data from peer */
  128. #define    SS_RCVATMARK    0x040    /* at mark on input */
  129.  
  130. #define    SS_PRIV    0x080    /* privileged */
  131. #define    SS_NBIO    0x100    /* non-blocking ops */
  132. #define    SS_ASYNC    0x200    /* async i/o notify */
  133. .DE
  134. .PP
  135. The state of a socket is manipulated both by the protocols
  136. and the user (through system calls).
  137. When a socket is created, the state is defined based on the type of socket.
  138. It may change as control actions are performed, for example connection
  139. establishment.
  140. It may also change according to the type of
  141. input/output the user wishes to perform, as indicated by options
  142. set with \fIfcntl\fP.  ``Non-blocking'' I/O  implies that
  143. a process should never be blocked to await resources.  Instead, any
  144. call which would block returns prematurely
  145. with the error EWOULDBLOCK, or the service request may be partially
  146. fulfilled, e.g. a request for more data than is present.
  147. .PP
  148. If a process requested ``asynchronous'' notification of events
  149. related to the socket, the SIGIO signal is posted to the process
  150. when such events occur.
  151. An event is a change in the socket's state;
  152. examples of such occurrences are: space
  153. becoming available in the send queue, new data available in the
  154. receive queue, connection establishment or disestablishment, etc. 
  155. .PP
  156. A socket may be marked ``privileged'' if it was created by the
  157. super-user.  Only privileged sockets may
  158. bind addresses in privileged portions of an address space
  159. or use ``raw'' sockets to access lower levels of the network.
  160. .NH 3
  161. Socket data queues
  162. .PP
  163. A socket's data queue contains a pointer to the data stored in
  164. the queue and other entries related to the management of
  165. the data.  The following structure defines a data queue:
  166. .DS
  167. ._f
  168. struct sockbuf {
  169.     u_short    sb_cc;        /* actual chars in buffer */
  170.     u_short    sb_hiwat;    /* max actual char count */
  171.     u_short    sb_mbcnt;    /* chars of mbufs used */
  172.     u_short    sb_mbmax;    /* max chars of mbufs to use */
  173.     u_short    sb_lowat;    /* low water mark */
  174.     short    sb_timeo;    /* timeout */
  175.     struct    mbuf *sb_mb;    /* the mbuf chain */
  176.     struct    proc *sb_sel;    /* process selecting read/write */
  177.     short    sb_flags;    /* flags, see below */
  178. };
  179. .DE
  180. .PP
  181. Data is stored in a queue as a chain of mbufs.
  182. The actual count of data characters as well as high and low water marks are
  183. used by the protocols in controlling the flow of data.
  184. The amount of buffer space (characters of mbufs and associated data pages)
  185. is also recorded along with the limit on buffer allocation.
  186. The socket routines cooperate in implementing the flow control
  187. policy by blocking a process when it requests to send data and
  188. the high water mark has been reached, or when it requests to
  189. receive data and less than the low water mark is present
  190. (assuming non-blocking I/O has not been specified).*
  191. .FS
  192. * The low-water mark is always presumed to be 0
  193. in the current implementation.
  194. .FE
  195. .PP
  196. When a socket is created, the supporting protocol ``reserves'' space
  197. for the send and receive queues of the socket.
  198. The limit on buffer allocation is set somewhat higher than the limit
  199. on data characters
  200. to account for the granularity of buffer allocation.
  201. The actual storage associated with a
  202. socket queue may fluctuate during a socket's lifetime, but it is assumed
  203. that this reservation will always allow a protocol to acquire enough memory
  204. to satisfy the high water marks.
  205. .PP
  206. The timeout and select values are manipulated by the socket routines
  207. in implementing various portions of the interprocess communications
  208. facilities and will not be described here.
  209. .PP
  210. Data queued at a socket is stored in one of two styles.
  211. Stream-oriented sockets queue data with no addresses, headers
  212. or record boundaries.
  213. The data are in mbufs linked through the \fIm_next\fP field.
  214. Buffers containing access rights may be present within the chain
  215. if the underlying protocol supports passage of access rights.
  216. Record-oriented sockets, including datagram sockets,
  217. queue data as a list of packets; the sections of packets are distinguished
  218. by the types of the mbufs containing them.
  219. The mbufs which comprise a record are linked through the \fIm_next\fP field;
  220. records are linked from the \fIm_act\fP field of the first mbuf
  221. of one packet to the first mbuf of the next.
  222. Each packet begins with an mbuf containing the ``from'' address
  223. if the protocol provides it,
  224. then any buffers containing access rights, and finally any buffers
  225. containing data.
  226. If a record contains no data,
  227. no data buffers are required unless neither address nor access rights
  228. are present.
  229. .PP
  230. A socket queue has a number of flags used in synchronizing access
  231. to the data and in acquiring resources:
  232. .DS
  233. ._d
  234. #define    SB_LOCK    0x01    /* lock on data queue (so_rcv only) */
  235. #define    SB_WANT    0x02    /* someone is waiting to lock */
  236. #define    SB_WAIT    0x04    /* someone is waiting for data/space */
  237. #define    SB_SEL    0x08    /* buffer is selected */
  238. #define    SB_COLL    0x10    /* collision selecting */
  239. .DE
  240. The last two flags are manipulated by the system in implementing
  241. the select mechanism.
  242. .NH 3
  243. Socket connection queuing
  244. .PP
  245. In dealing with connection oriented sockets (e.g. SOCK_STREAM)
  246. the two ends are considered distinct.  One end is termed
  247. \fIactive\fP, and generates connection requests.  The other
  248. end is called \fIpassive\fP and accepts connection requests.
  249. .PP
  250. From the passive side, a socket is marked with
  251. SO_ACCEPTCONN when a \fIlisten\fP call is made, 
  252. creating two queues of sockets: \fIso_q0\fP for connections
  253. in progress and \fIso_q\fP for connections already made and
  254. awaiting user acceptance.
  255. As a protocol is preparing incoming connections, it creates
  256. a socket structure queued on \fIso_q0\fP by calling the routine
  257. \fIsonewconn\fP().  When the connection
  258. is established, the socket structure is then transferred
  259. to \fIso_q\fP, making it available for an \fIaccept\fP.
  260. .PP
  261. If an SO_ACCEPTCONN socket is closed with sockets on either
  262. \fIso_q0\fP or \fIso_q\fP, these sockets are dropped,
  263. with notification to the peers as appropriate.
  264. .NH 2
  265. Protocol layer(s)
  266. .PP
  267. Each socket is created in a communications domain,
  268. which usually implies both an addressing structure (address family)
  269. and a set of protocols which implement various socket types within the domain
  270. (protocol family).
  271. Each domain is defined by the following structure:
  272. .DS
  273. .ta .5i +\w'struct  'u +\w'(*dom_externalize)();   'u
  274. struct    domain {
  275.     int    dom_family;        /* PF_xxx */
  276.     char    *dom_name;
  277.     int    (*dom_init)();        /* initialize domain data structures */
  278.     int    (*dom_externalize)();    /* externalize access rights */
  279.     int    (*dom_dispose)();    /* dispose of internalized rights */
  280.     struct    protosw *dom_protosw, *dom_protoswNPROTOSW;
  281.     struct    domain *dom_next;
  282. };
  283. .DE
  284. .PP
  285. At boot time, each domain configured into the kernel
  286. is added to a linked list of domain.
  287. The initialization procedure of each domain is then called.
  288. After that time, the domain structure is used to locate protocols
  289. within the protocol family.
  290. It may also contain procedure references
  291. for externalization of access rights at the receiving socket
  292. and the disposal of access rights that are not received.
  293. .PP
  294. Protocols are described by a set of entry points and certain
  295. socket-visible characteristics, some of which are used in
  296. deciding which socket type(s) they may support.  
  297. .PP
  298. An entry in the ``protocol switch'' table exists for each
  299. protocol module configured into the system.  It has the following form:
  300. .DS
  301. .ta .5i +\w'struct  'u +\w'domain *pr_domain;    'u
  302. struct protosw {
  303.     short    pr_type;        /* socket type used for */
  304.     struct    domain *pr_domain;    /* domain protocol a member of */
  305.     short    pr_protocol;        /* protocol number */
  306.     short    pr_flags;        /* socket visible attributes */
  307. /* protocol-protocol hooks */
  308.     int    (*pr_input)();        /* input to protocol (from below) */
  309.     int    (*pr_output)();        /* output to protocol (from above) */
  310.     int    (*pr_ctlinput)();    /* control input (from below) */
  311.     int    (*pr_ctloutput)();    /* control output (from above) */
  312. /* user-protocol hook */
  313.     int    (*pr_usrreq)();        /* user request */
  314. /* utility hooks */
  315.     int    (*pr_init)();        /* initialization routine */
  316.     int    (*pr_fasttimo)();    /* fast timeout (200ms) */
  317.     int    (*pr_slowtimo)();    /* slow timeout (500ms) */
  318.     int    (*pr_drain)();        /* flush any excess space possible */
  319. };
  320. .DE
  321. .PP
  322. A protocol is called through the \fIpr_init\fP entry before any other.
  323. Thereafter it is called every 200 milliseconds through the
  324. \fIpr_fasttimo\fP entry and
  325. every 500 milliseconds through the \fIpr_slowtimo\fP for timer based actions.
  326. The system will call the \fIpr_drain\fP entry if it is low on space and
  327. this should throw away any non-critical data.
  328. .PP
  329. Protocols pass data between themselves as chains of mbufs using
  330. the \fIpr_input\fP and \fIpr_output\fP routines.  \fIPr_input\fP
  331. passes data up (towards
  332. the user) and \fIpr_output\fP passes it down (towards the network); control
  333. information passes up and down on \fIpr_ctlinput\fP and \fIpr_ctloutput\fP.
  334. The protocol is responsible for the space occupied by any of the
  335. arguments to these entries and must either pass it onward or dispose of it.
  336. (On output, the lowest level reached must free buffers storing the arguments;
  337. on input, the highest level is responsible for freeing buffers.)
  338. .PP
  339. The \fIpr_usrreq\fP routine interfaces protocols to the socket
  340. code and is described below.
  341. .PP
  342. The \fIpr_flags\fP field is constructed from the following values:
  343. .DS
  344. .ta \w'#define 'u +\w'PR_CONNREQUIRED   'u +8n
  345. #define    PR_ATOMIC    0x01        /* exchange atomic messages only */
  346. #define    PR_ADDR    0x02        /* addresses given with messages */
  347. #define    PR_CONNREQUIRED    0x04        /* connection required by protocol */
  348. #define    PR_WANTRCVD    0x08        /* want PRU_RCVD calls */
  349. #define    PR_RIGHTS    0x10        /* passes capabilities */
  350. .DE
  351. Protocols which are connection-based specify the PR_CONNREQUIRED
  352. flag so that the socket routines will never attempt to send data
  353. before a connection has been established.  If the PR_WANTRCVD flag
  354. is set, the socket routines will notify the protocol when the user
  355. has removed data from the socket's receive queue.  This allows
  356. the protocol to implement acknowledgement on user receipt, and
  357. also update windowing information based on the amount of space
  358. available in the receive queue.  The PR_ADDR field indicates that any
  359. data placed in the socket's receive queue will be preceded by the
  360. address of the sender.  The PR_ATOMIC flag specifies that each \fIuser\fP
  361. request to send data must be performed in a single \fIprotocol\fP send
  362. request; it is the protocol's responsibility to maintain record
  363. boundaries on data to be sent.  The PR_RIGHTS flag indicates that the
  364. protocol supports the passing of capabilities;  this is currently
  365. used only by the protocols in the UNIX protocol family.
  366. .PP
  367. When a socket is created, the socket routines scan the protocol
  368. table for the domain
  369. looking for an appropriate protocol to support the type of
  370. socket being created.  The \fIpr_type\fP field contains one of the
  371. possible socket types (e.g. SOCK_STREAM), while the \fIpr_domain\fP
  372. is a back pointer to the domain structure.
  373. The \fIpr_protocol\fP field contains the protocol number of the
  374. protocol, normally a well-known value.
  375. .NH 2
  376. Network-interface layer
  377. .PP
  378. Each network-interface configured into a system defines a
  379. path through which packets may be sent and received.
  380. Normally a hardware device is associated with this interface,
  381. though there is no requirement for this (for example, all
  382. systems have a software ``loopback'' interface used for 
  383. debugging and performance analysis).
  384. In addition to manipulating the hardware device, an interface
  385. module is responsible
  386. for encapsulation and decapsulation of any link-layer header
  387. information required to deliver a message to its destination.
  388. The selection of which interface to use in delivering packets
  389. is a routing decision carried out at a
  390. higher level than the network-interface layer.
  391. An interface may have addresses in one or more address families.
  392. The address is set at boot time using an \fIioctl\fP on a socket
  393. in the appropriate domain; this operation is implemented by the protocol
  394. family, after verifying the operation through the device \fIioctl\fP entry.
  395. .PP
  396. An interface is defined by the following structure,
  397. .DS
  398. .ta .5i +\w'struct   'u +\w'ifaddr *if_addrlist;   'u
  399. struct ifnet {
  400.     char    *if_name;        /* name, e.g. ``en'' or ``lo'' */
  401.     short    if_unit;        /* sub-unit for lower level driver */
  402.     short    if_mtu;            /* maximum transmission unit */
  403.     short    if_flags;        /* up/down, broadcast, etc. */
  404.     short    if_timer;        /* time 'til if_watchdog called */
  405.     struct    ifaddr *if_addrlist;    /* list of addresses of interface */
  406.     struct    ifqueue if_snd;        /* output queue */
  407.     int    (*if_init)();        /* init routine */
  408.     int    (*if_output)();        /* output routine */
  409.     int    (*if_ioctl)();        /* ioctl routine */
  410.     int    (*if_reset)();        /* bus reset routine */
  411.     int    (*if_watchdog)();    /* timer routine */
  412.     int    if_ipackets;        /* packets received on interface */
  413.     int    if_ierrors;        /* input errors on interface */
  414.     int    if_opackets;        /* packets sent on interface */
  415.     int    if_oerrors;        /* output errors on interface */
  416.     int    if_collisions;        /* collisions on csma interfaces */
  417.     struct    ifnet *if_next;
  418. };
  419. .DE
  420. Each interface address has the following form:
  421. .DS
  422. .ta \w'#define 'u +\w'struct   'u +\w'struct   'u +\w'sockaddr ifa_addr;   'u-\w'struct   'u
  423. struct ifaddr {
  424.     struct    sockaddr ifa_addr;    /* address of interface */
  425.     union {
  426.         struct    sockaddr ifu_broadaddr;
  427.         struct    sockaddr ifu_dstaddr;
  428.     } ifa_ifu;
  429.     struct    ifnet *ifa_ifp;        /* back-pointer to interface */
  430.     struct    ifaddr *ifa_next;    /* next address for interface */
  431. };
  432. .ta \w'#define 'u +\w'ifa_broadaddr   'u +\w'ifa_ifu.ifu_broadaddr       'u
  433. #define    ifa_broadaddr    ifa_ifu.ifu_broadaddr    /* broadcast address */
  434. #define    ifa_dstaddr    ifa_ifu.ifu_dstaddr    /* other end of p-to-p link */
  435. .DE
  436. The protocol generally maintains this structure as part of a larger
  437. structure containing additional information concerning the address.
  438. .PP
  439. Each interface has a send queue and routines used for 
  440. initialization, \fIif_init\fP, and output, \fIif_output\fP.
  441. If the interface resides on a system bus, the routine \fIif_reset\fP
  442. will be called after a bus reset has been performed. 
  443. An interface may also
  444. specify a timer routine, \fIif_watchdog\fP;
  445. if \fIif_timer\fP is non-zero, it is decremented once per second
  446. until it reaches zero, at which time the watchdog routine is called.
  447. .PP
  448. The state of an interface and certain characteristics are stored in
  449. the \fIif_flags\fP field.  The following values are possible:
  450. .DS
  451. ._d
  452. #define    IFF_UP    0x1    /* interface is up */
  453. #define    IFF_BROADCAST    0x2    /* broadcast is possible */
  454. #define    IFF_DEBUG    0x4    /* turn on debugging */
  455. #define    IFF_LOOPBACK    0x8    /* is a loopback net */
  456. #define    IFF_POINTOPOINT    0x10    /* interface is point-to-point link */
  457. #define    IFF_NOTRAILERS    0x20    /* avoid use of trailers */
  458. #define    IFF_RUNNING    0x40    /* resources allocated */
  459. #define    IFF_NOARP    0x80    /* no address resolution protocol */
  460. .DE
  461. If the interface is connected to a network which supports transmission
  462. of \fIbroadcast\fP packets, the IFF_BROADCAST flag will be set and
  463. the \fIifa_broadaddr\fP field will contain the address to be used in
  464. sending or accepting a broadcast packet.  If the interface is associated
  465. with a point-to-point hardware link (for example, a DEC DMR-11), the
  466. IFF_POINTOPOINT flag will be set and \fIifa_dstaddr\fP will contain the
  467. address of the host on the other side of the connection.  These addresses
  468. and the local address of the interface, \fIif_addr\fP, are used in
  469. filtering incoming packets.  The interface sets IFF_RUNNING after
  470. it has allocated system resources and posted an initial read on the
  471. device it manages.  This state bit is used to avoid multiple allocation
  472. requests when an interface's address is changed.  The IFF_NOTRAILERS
  473. flag indicates the interface should refrain from using a \fItrailer\fP
  474. encapsulation on outgoing packets, or (where per-host negotiation
  475. of trailers is possible) that trailer encapsulations should not be requested;
  476. \fItrailer\fP protocols are described
  477. in section 14.  The IFF_NOARP flag indicates the interface should not
  478. use an ``address resolution protocol'' in mapping internetwork addresses
  479. to local network addresses.
  480. .PP
  481. Various statistics are also stored in the interface structure.  These
  482. may be viewed by users using the \fInetstat\fP(1) program.
  483. .PP
  484. The interface address and flags may be set with the SIOCSIFADDR and
  485. SIOCSIFFLAGS \fIioctl\fP\^s.  SIOCSIFADDR is used initially to define each
  486. interface's address; SIOGSIFFLAGS can be used to mark
  487. an interface down and perform site-specific configuration.
  488. The destination address of a point-to-point link is set with SIOCSIFDSTADDR.
  489. Corresponding operations exist to read each value.
  490. Protocol families may also support operations to set and read the broadcast
  491. address.
  492. In addition, the SIOCGIFCONF \fIioctl\fP retrieves a list of interface
  493. names and addresses for all interfaces and protocols on the host.
  494. .NH 3
  495. UNIBUS interfaces
  496. .PP
  497. All hardware related interfaces currently reside on the UNIBUS.
  498. Consequently a common set of utility routines for dealing
  499. with the UNIBUS has been developed.  Each UNIBUS interface
  500. utilizes a structure of the following form:
  501. .DS
  502. .ta \w'#define 'u +\w'ifw_xtofree 'u +\w'pte ifu_wmap[IF_MAXNUBAMR];    'u
  503. struct    ifubinfo {
  504.     short    iff_uban;            /* uba number */
  505.     short    iff_hlen;            /* local net header length */
  506.     struct    uba_regs *iff_uba;        /* uba regs, in vm */
  507.     short    iff_flags;            /* used during uballoc's */
  508. };
  509. .DE
  510. Additional structures are associated with each receive and transmit buffer,
  511. normally one each per interface; for read,
  512. .DS
  513. .ta \w'#define 'u +\w'ifw_xtofree 'u +\w'pte ifu_wmap[IF_MAXNUBAMR];    'u
  514. struct    ifrw {
  515.     caddr_t    ifrw_addr;            /* virt addr of header */
  516.     short    ifrw_bdp;            /* unibus bdp */
  517.     short    ifrw_flags;            /* type, etc. */
  518. #define    IFRW_W    0x01                /* is a transmit buffer */
  519.     int    ifrw_info;            /* value from ubaalloc */
  520.     int    ifrw_proto;            /* map register prototype */
  521.     struct    pte *ifrw_mr;            /* base of map registers */
  522. };
  523. .DE
  524. and for write,
  525. .DS
  526. .ta \w'#define 'u +\w'ifw_xtofree 'u +\w'pte ifu_wmap[IF_MAXNUBAMR];    'u
  527. struct    ifxmt {
  528.     struct    ifrw ifrw;
  529.     caddr_t    ifw_base;            /* virt addr of buffer */
  530.     struct    pte ifw_wmap[IF_MAXNUBAMR];    /* base pages for output */
  531.     struct    mbuf *ifw_xtofree;        /* pages being dma'd out */
  532.     short    ifw_xswapd;            /* mask of clusters swapped */
  533.     short    ifw_nmr;            /* number of entries in wmap */
  534. };
  535. .ta \w'#define 'u +\w'ifw_xtofree 'u +\w'pte ifu_wmap[IF_MAXNUBAMR];    'u
  536. #define    ifw_addr    ifrw.ifrw_addr
  537. #define    ifw_bdp    ifrw.ifrw_bdp
  538. #define    ifw_flags    ifrw.ifrw_flags
  539. #define    ifw_info    ifrw.ifrw_info
  540. #define    ifw_proto    ifrw.ifrw_proto
  541. #define    ifw_mr    ifrw.ifrw_mr
  542. .DE
  543. One of each of these structures is conveniently packaged for interfaces
  544. with single buffers for each direction, as follows:
  545. .DS
  546. .ta \w'#define 'u +\w'ifw_xtofree 'u +\w'pte ifu_wmap[IF_MAXNUBAMR];    'u
  547. struct    ifuba {
  548.     struct    ifubinfo ifu_info;
  549.     struct    ifrw ifu_r;
  550.     struct    ifxmt ifu_xmt;
  551. };
  552. .ta \w'#define 'u +\w'ifw_xtofree 'u
  553. #define    ifu_uban    ifu_info.iff_uban
  554. #define    ifu_hlen    ifu_info.iff_hlen
  555. #define    ifu_uba        ifu_info.iff_uba
  556. #define    ifu_flags    ifu_info.iff_flags
  557. #define    ifu_w        ifu_xmt.ifrw
  558. #define    ifu_xtofree    ifu_xmt.ifw_xtofree
  559. .DE
  560. .PP
  561. The \fIif_ubinfo\fP structure contains the general information needed
  562. to characterize the I/O-mapped buffers for the device.
  563. In addition, there is a structure describing each buffer, including
  564. UNIBUS resources held by the interface.
  565. Sufficient memory pages and bus map registers are allocated to each buffer
  566. upon initialization according to the maximum packet size and header length.
  567. The kernel virtual address of the buffer is held in \fIifrw_addr\fP,
  568. and the map registers begin
  569. at \fIifrw_mr\fP.  UNIBUS map register \fIifrw_mr\fP\^[\-1]
  570. maps the local network header
  571. ending on a page boundary.  UNIBUS data paths are
  572. reserved for read and for
  573. write, given by \fIifrw_bdp\fP.  The prototype of the map
  574. registers for read and for write is saved in \fIifrw_proto\fP.
  575. .PP
  576. When write transfers are not at least half-full pages on page boundaries,
  577. the data are just copied into the pages mapped on the UNIBUS
  578. and the transfer is started.
  579. If a write transfer is at least half a page long and on a page
  580. boundary, UNIBUS page table entries are swapped to reference
  581. the pages, and then the initial pages are
  582. remapped from \fIifw_wmap\fP when the transfer completes.
  583. The mbufs containing the mapped pages are placed on the \fIifw_xtofree\fP
  584. queue to be freed after transmission.
  585. .PP
  586. When read transfers give at least half a page of data to be input, page
  587. frames are allocated from a network page list and traded
  588. with the pages already containing the data, mapping the allocated
  589. pages to replace the input pages for the next UNIBUS data input.
  590. .PP
  591. The following utility routines are available for use in
  592. writing network interface drivers; all use the
  593. structures described above.
  594. .LP
  595. if_ubaminit(ifubinfo, uban, hlen, nmr, ifr, nr, ifx, nx);
  596. .br
  597. if_ubainit(ifuba, uban, hlen, nmr);
  598. .IP
  599. \fIif_ubaminit\fP allocates resources on UNIBUS adapter \fIuban\fP,
  600. storing the information in the \fIifubinfo\fP, \fIifrw\fP and \fIifxmt\fP
  601. structures referenced.
  602. The \fIifr\fP and \fIifx\fP parameters are pointers to arrays
  603. of \fIifrw\fP and \fIifxmt\fP structures whose dimensions
  604. are \fInr\fP and \fInx\fP, respectively.
  605. \fIif_ubainit\fP is a simpler, backwards-compatible interface used
  606. for hardware with single buffers of each type.
  607. They are called only at boot time or after a UNIBUS reset. 
  608. One data path (buffered or unbuffered,
  609. depending on the \fIifu_flags\fP field) is allocated for each buffer.
  610. The \fInmr\fP parameter indicates
  611. the number of UNIBUS mapping registers required to map a maximal
  612. sized packet onto the UNIBUS, while \fIhlen\fP specifies the size
  613. of a local network header, if any, which should be mapped separately
  614. from the data (see the description of trailer protocols in chapter 14).
  615. Sufficient UNIBUS mapping registers and pages of memory are allocated
  616. to initialize the input data path for an initial read.  For the output
  617. data path, mapping registers and pages of memory are also allocated
  618. and mapped onto the UNIBUS.  The pages associated with the output
  619. data path are held in reserve in the event a write requires copying
  620. non-page-aligned data (see \fIif_wubaput\fP below).
  621. If \fIif_ubainit\fP is called with memory pages already allocated,
  622. they will be used instead of allocating new ones (this normally
  623. occurs after a UNIBUS reset).
  624. A 1 is returned when allocation and initialization are successful,
  625. 0 otherwise.
  626. .LP
  627. m = if_ubaget(ifubinfo, ifr, totlen, off0, ifp);
  628. .br
  629. m = if_rubaget(ifuba, totlen, off0, ifp);
  630. .IP
  631. \fIif_ubaget\fP and \fIif_rubaget\fP pull input data
  632. out of an interface receive buffer and into an mbuf chain.
  633. The first interface passes pointers to the \fIifubinfo\fP structure
  634. for the interface and the \fIifrw\fP structure for the receive buffer;
  635. the second call may be used for single-buffered devices.
  636. \fItotlen\fP specifies the length of data to be obtained, not counting the
  637. local network header.  If \fIoff0\fP is non-zero, it indicates
  638. a byte offset to a trailing local network header which should be
  639. copied into a separate mbuf and prepended to the front of the resultant mbuf
  640. chain.  When the data amount to at least a half a page,
  641. the previously mapped data pages are remapped
  642. into the mbufs and swapped with fresh pages, thus avoiding
  643. any copy.
  644. The receiving interface is recorded as \fIifp\fP, a pointer to an \fIifnet\fP
  645. structure, for the use of the receiving network protocol.
  646. A 0 return value indicates a failure to allocate resources.
  647. .LP
  648. if_wubaput(ifubinfo, ifx, m);
  649. .br
  650. if_wubaput(ifuba, m);
  651. .IP
  652. \fIif_ubaput\fP and \fIif_wubaput\fP map a chain of mbufs
  653. onto a network interface in preparation for output.
  654. The first interface is used by devices with multiple transmit buffers.
  655. The chain includes any local network
  656. header, which is copied so that it resides in the mapped and
  657. aligned I/O space.
  658. Page-aligned data that are page-aligned in the output buffer
  659. are mapped to the UNIBUS in place of the normal buffer page,
  660. and the corresponding mbuf is placed on a queue to be freed after transmission.
  661. Any other mbufs which contained non-page-sized
  662. data portions are copied to the I/O space and then freed.
  663. Pages mapped from a previous output operation (no longer needed)
  664. are unmapped.
  665.