⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 6.t

📁 早期freebsd实现
💻 T
📖 第 1 页 / 共 2 页
字号:
.\" Copyright (c) 1983, 1986, 1993.\"	The Regents of the University of California.  All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\"    notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\"    notice, this list of conditions and the following disclaimer in the.\"    documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\"    must display the following acknowledgement:.\"	This product includes software developed by the University of.\"	California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\"    may be used to endorse or promote products derived from this software.\"    without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\"	@(#)6.t	8.1 (Berkeley) 6/8/93.\".nr H2 1.\".ds RH "Internal layering.br.ne 2i.NH\s+2Internal layering\s0.PPThe internal structure of the network system is divided intothree layers.  Theselayers correspond to the services provided by the socketabstraction, those provided by the communication protocols,and those provided by the hardware interfaces.  The communicationprotocols are normally layered into two or more individualcooperating layers, though they are collectively viewedin the system as one layer providing services supportiveof the appropriate socket abstraction..PPThe following sections describe the properties of each layerin the system and the interfaces to which each must conform..NH 2Socket layer.PPThe socket layer deals with the interprocess communicationfacilities provided by the system.  A socket is a bidirectionalendpoint of communication which is ``typed'' by the semanticsof communication it supports.  The system calls described inthe \fIBerkeley Software Architecture Manual\fP [Joy86]are used to manipulate sockets..PPA socket consists of the following data structure:.DS._fstruct socket {	short	so_type;		/* generic type */	short	so_options;		/* from socket call */	short	so_linger;		/* time to linger while closing */	short	so_state;		/* internal state flags */	caddr_t	so_pcb;			/* protocol control block */	struct	protosw *so_proto;	/* protocol handle */	struct	socket *so_head;	/* back pointer to accept socket */	struct	socket *so_q0;		/* queue of partial connections */	short	so_q0len;		/* partials on so_q0 */	struct	socket *so_q;		/* queue of incoming connections */	short	so_qlen;		/* number of connections on so_q */	short	so_qlimit;		/* max number queued connections */	struct	sockbuf so_rcv;		/* receive queue */	struct	sockbuf so_snd;		/* send queue */	short	so_timeo;		/* connection timeout */	u_short	so_error;		/* error affecting connection */	u_short	so_oobmark;		/* chars to oob mark */	short	so_pgrp;		/* pgrp for signals */};.DE.PPEach socket contains two data queues, \fIso_rcv\fP and \fIso_snd\fP,and a pointer to routines which provide supporting services. The type of the socket,\fIso_type\fP is defined at socket creation time and used in selectingthose services which are appropriate to support it.  The supportingprotocol is selected at socket creation time and recorded inthe socket data structure for later use.  Protocols are definedby a table of procedures, the \fIprotosw\fP structure, which willbe described in detail later.  A pointer to a protocol-specificdata structure,the ``protocol control block,'' is also present in the socket structure.Protocols control this data structure, which normally includes aback pointer to the parent socket structure to allow easylookup when returning information to a user (for example, placing an error number in the \fIso_error\fPfield).  The other entries in the socket structure are used inqueuing connection requests, validating user requests, storingsocket characteristics (e.g.options supplied at the time a socket is created), and maintaininga socket's state..PPProcesses ``rendezvous at a socket'' in many instances.  For instance,when a process wishes to extract data from a socket's receive queueand it is empty, or lacks sufficient data to satisfy the request,the process blocks, supplying the address of the receive queue asa ``wait channel' to be used in notification.  When data arrivesfor the process and is placed in the socket's queue, the blockedprocess is identified by the fact it is waiting ``on the queue.''.NH 3Socket state.PPA socket's state is defined from the following:.DS.ta \w'#define 'u +\w'SS_ISDISCONNECTING    'u +\w'0x000     'u#define	SS_NOFDREF	0x001	/* no file table ref any more */#define	SS_ISCONNECTED	0x002	/* socket connected to a peer */#define	SS_ISCONNECTING	0x004	/* in process of connecting to peer */#define	SS_ISDISCONNECTING	0x008	/* in process of disconnecting */#define	SS_CANTSENDMORE	0x010	/* can't send more data to peer */#define	SS_CANTRCVMORE	0x020	/* can't receive more data from peer */#define	SS_RCVATMARK	0x040	/* at mark on input */#define	SS_PRIV	0x080	/* privileged */#define	SS_NBIO	0x100	/* non-blocking ops */#define	SS_ASYNC	0x200	/* async i/o notify */.DE.PPThe state of a socket is manipulated both by the protocolsand the user (through system calls).When a socket is created, the state is defined based on the type of socket.It may change as control actions are performed, for example connectionestablishment.It may also change according to the type ofinput/output the user wishes to perform, as indicated by optionsset with \fIfcntl\fP.  ``Non-blocking'' I/O  implies thata process should never be blocked to await resources.  Instead, anycall which would block returns prematurelywith the error EWOULDBLOCK, or the service request may be partiallyfulfilled, e.g. a request for more data than is present..PPIf a process requested ``asynchronous'' notification of eventsrelated to the socket, the SIGIO signal is posted to the processwhen such events occur.An event is a change in the socket's state;examples of such occurrences are: spacebecoming available in the send queue, new data available in thereceive queue, connection establishment or disestablishment, etc. .PPA socket may be marked ``privileged'' if it was created by thesuper-user.  Only privileged sockets maybind addresses in privileged portions of an address spaceor use ``raw'' sockets to access lower levels of the network..NH 3Socket data queues.PPA socket's data queue contains a pointer to the data stored inthe queue and other entries related to the management ofthe data.  The following structure defines a data queue:.DS._fstruct sockbuf {	u_short	sb_cc;		/* actual chars in buffer */	u_short	sb_hiwat;	/* max actual char count */	u_short	sb_mbcnt;	/* chars of mbufs used */	u_short	sb_mbmax;	/* max chars of mbufs to use */	u_short	sb_lowat;	/* low water mark */	short	sb_timeo;	/* timeout */	struct	mbuf *sb_mb;	/* the mbuf chain */	struct	proc *sb_sel;	/* process selecting read/write */	short	sb_flags;	/* flags, see below */};.DE.PPData is stored in a queue as a chain of mbufs.The actual count of data characters as well as high and low water marks areused by the protocols in controlling the flow of data.The amount of buffer space (characters of mbufs and associated data pages)is also recorded along with the limit on buffer allocation.The socket routines cooperate in implementing the flow controlpolicy by blocking a process when it requests to send data andthe high water mark has been reached, or when it requests toreceive data and less than the low water mark is present(assuming non-blocking I/O has not been specified).*.FS* The low-water mark is always presumed to be 0in the current implementation..FE.PPWhen a socket is created, the supporting protocol ``reserves'' spacefor the send and receive queues of the socket.The limit on buffer allocation is set somewhat higher than the limiton data charactersto account for the granularity of buffer allocation.The actual storage associated with asocket queue may fluctuate during a socket's lifetime, but it is assumedthat this reservation will always allow a protocol to acquire enough memoryto satisfy the high water marks..PPThe timeout and select values are manipulated by the socket routinesin implementing various portions of the interprocess communicationsfacilities and will not be described here..PPData queued at a socket is stored in one of two styles.Stream-oriented sockets queue data with no addresses, headersor record boundaries.The data are in mbufs linked through the \fIm_next\fP field.Buffers containing access rights may be present within the chainif the underlying protocol supports passage of access rights.Record-oriented sockets, including datagram sockets,queue data as a list of packets; the sections of packets are distinguishedby the types of the mbufs containing them.The mbufs which comprise a record are linked through the \fIm_next\fP field;records are linked from the \fIm_act\fP field of the first mbufof one packet to the first mbuf of the next.Each packet begins with an mbuf containing the ``from'' addressif the protocol provides it,then any buffers containing access rights, and finally any bufferscontaining data.If a record contains no data,no data buffers are required unless neither address nor access rightsare present..PPA socket queue has a number of flags used in synchronizing accessto the data and in acquiring resources:.DS._d#define	SB_LOCK	0x01	/* lock on data queue (so_rcv only) */#define	SB_WANT	0x02	/* someone is waiting to lock */#define	SB_WAIT	0x04	/* someone is waiting for data/space */#define	SB_SEL	0x08	/* buffer is selected */#define	SB_COLL	0x10	/* collision selecting */.DEThe last two flags are manipulated by the system in implementingthe select mechanism..NH 3Socket connection queuing.PPIn dealing with connection oriented sockets (e.g. SOCK_STREAM)the two ends are considered distinct.  One end is termed\fIactive\fP, and generates connection requests.  The otherend is called \fIpassive\fP and accepts connection requests..PPFrom the passive side, a socket is marked withSO_ACCEPTCONN when a \fIlisten\fP call is made, creating two queues of sockets: \fIso_q0\fP for connectionsin progress and \fIso_q\fP for connections already made andawaiting user acceptance.As a protocol is preparing incoming connections, it createsa socket structure queued on \fIso_q0\fP by calling the routine\fIsonewconn\fP().  When the connectionis established, the socket structure is then transferredto \fIso_q\fP, making it available for an \fIaccept\fP..PPIf an SO_ACCEPTCONN socket is closed with sockets on either\fIso_q0\fP or \fIso_q\fP, these sockets are dropped,with notification to the peers as appropriate..NH 2Protocol layer(s).PPEach socket is created in a communications domain,which usually implies both an addressing structure (address family)and a set of protocols which implement various socket types within the domain(protocol family).Each domain is defined by the following structure:.DS.ta .5i +\w'struct  'u +\w'(*dom_externalize)();   'ustruct	domain {	int	dom_family;		/* PF_xxx */	char	*dom_name;	int	(*dom_init)();		/* initialize domain data structures */	int	(*dom_externalize)();	/* externalize access rights */	int	(*dom_dispose)();	/* dispose of internalized rights */	struct	protosw *dom_protosw, *dom_protoswNPROTOSW;	struct	domain *dom_next;};.DE.PPAt boot time, each domain configured into the kernelis added to a linked list of domain.The initialization procedure of each domain is then called.After that time, the domain structure is used to locate protocolswithin the protocol family.It may also contain procedure referencesfor externalization of access rights at the receiving socketand the disposal of access rights that are not received..PPProtocols are described by a set of entry points and certainsocket-visible characteristics, some of which are used indeciding which socket type(s) they may support.  .PPAn entry in the ``protocol switch'' table exists for eachprotocol module configured into the system.  It has the following form:.DS.ta .5i +\w'struct  'u +\w'domain *pr_domain;    'ustruct protosw {	short	pr_type;		/* socket type used for */	struct	domain *pr_domain;	/* domain protocol a member of */	short	pr_protocol;		/* protocol number */	short	pr_flags;		/* socket visible attributes *//* protocol-protocol hooks */	int	(*pr_input)();		/* input to protocol (from below) */	int	(*pr_output)();		/* output to protocol (from above) */	int	(*pr_ctlinput)();	/* control input (from below) */	int	(*pr_ctloutput)();	/* control output (from above) *//* user-protocol hook */	int	(*pr_usrreq)();		/* user request *//* utility hooks */	int	(*pr_init)();		/* initialization routine */	int	(*pr_fasttimo)();	/* fast timeout (200ms) */	int	(*pr_slowtimo)();	/* slow timeout (500ms) */	int	(*pr_drain)();		/* flush any excess space possible */};.DE.PPA protocol is called through the \fIpr_init\fP entry before any other.Thereafter it is called every 200 milliseconds through the\fIpr_fasttimo\fP entry andevery 500 milliseconds through the \fIpr_slowtimo\fP for timer based actions.The system will call the \fIpr_drain\fP entry if it is low on space andthis should throw away any non-critical data..PPProtocols pass data between themselves as chains of mbufs usingthe \fIpr_input\fP and \fIpr_output\fP routines.  \fIPr_input\fPpasses data up (towardsthe user) and \fIpr_output\fP passes it down (towards the network); controlinformation passes up and down on \fIpr_ctlinput\fP and \fIpr_ctloutput\fP.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -