📄 6.t
字号:
The protocol is responsible for the space occupied by any of thearguments to these entries and must either pass it onward or dispose of it.(On output, the lowest level reached must free buffers storing the arguments;on input, the highest level is responsible for freeing buffers.).PPThe \fIpr_usrreq\fP routine interfaces protocols to the socketcode and is described below..PPThe \fIpr_flags\fP field is constructed from the following values:.DS.ta \w'#define 'u +\w'PR_CONNREQUIRED 'u +8n#define PR_ATOMIC 0x01 /* exchange atomic messages only */#define PR_ADDR 0x02 /* addresses given with messages */#define PR_CONNREQUIRED 0x04 /* connection required by protocol */#define PR_WANTRCVD 0x08 /* want PRU_RCVD calls */#define PR_RIGHTS 0x10 /* passes capabilities */.DEProtocols which are connection-based specify the PR_CONNREQUIREDflag so that the socket routines will never attempt to send databefore a connection has been established. If the PR_WANTRCVD flagis set, the socket routines will notify the protocol when the userhas removed data from the socket's receive queue. This allowsthe protocol to implement acknowledgement on user receipt, andalso update windowing information based on the amount of spaceavailable in the receive queue. The PR_ADDR field indicates that anydata placed in the socket's receive queue will be preceded by theaddress of the sender. The PR_ATOMIC flag specifies that each \fIuser\fPrequest to send data must be performed in a single \fIprotocol\fP sendrequest; it is the protocol's responsibility to maintain recordboundaries on data to be sent. The PR_RIGHTS flag indicates that theprotocol supports the passing of capabilities; this is currentlyused only by the protocols in the UNIX protocol family..PPWhen a socket is created, the socket routines scan the protocoltable for the domainlooking for an appropriate protocol to support the type ofsocket being created. The \fIpr_type\fP field contains one of thepossible socket types (e.g. SOCK_STREAM), while the \fIpr_domain\fPis a back pointer to the domain structure.The \fIpr_protocol\fP field contains the protocol number of theprotocol, normally a well-known value..NH 2Network-interface layer.PPEach network-interface configured into a system defines apath through which packets may be sent and received.Normally a hardware device is associated with this interface,though there is no requirement for this (for example, allsystems have a software ``loopback'' interface used for debugging and performance analysis).In addition to manipulating the hardware device, an interfacemodule is responsiblefor encapsulation and decapsulation of any link-layer headerinformation required to deliver a message to its destination.The selection of which interface to use in delivering packetsis a routing decision carried out at ahigher level than the network-interface layer.An interface may have addresses in one or more address families.The address is set at boot time using an \fIioctl\fP on a socketin the appropriate domain; this operation is implemented by the protocolfamily, after verifying the operation through the device \fIioctl\fP entry..PPAn interface is defined by the following structure,.DS.ta .5i +\w'struct 'u +\w'ifaddr *if_addrlist; 'ustruct ifnet { char *if_name; /* name, e.g. ``en'' or ``lo'' */ short if_unit; /* sub-unit for lower level driver */ short if_mtu; /* maximum transmission unit */ short if_flags; /* up/down, broadcast, etc. */ short if_timer; /* time 'til if_watchdog called */ struct ifaddr *if_addrlist; /* list of addresses of interface */ struct ifqueue if_snd; /* output queue */ int (*if_init)(); /* init routine */ int (*if_output)(); /* output routine */ int (*if_ioctl)(); /* ioctl routine */ int (*if_reset)(); /* bus reset routine */ int (*if_watchdog)(); /* timer routine */ int if_ipackets; /* packets received on interface */ int if_ierrors; /* input errors on interface */ int if_opackets; /* packets sent on interface */ int if_oerrors; /* output errors on interface */ int if_collisions; /* collisions on csma interfaces */ struct ifnet *if_next;};.DEEach interface address has the following form:.DS.ta \w'#define 'u +\w'struct 'u +\w'struct 'u +\w'sockaddr ifa_addr; 'u-\w'struct 'ustruct ifaddr { struct sockaddr ifa_addr; /* address of interface */ union { struct sockaddr ifu_broadaddr; struct sockaddr ifu_dstaddr; } ifa_ifu; struct ifnet *ifa_ifp; /* back-pointer to interface */ struct ifaddr *ifa_next; /* next address for interface */};.ta \w'#define 'u +\w'ifa_broadaddr 'u +\w'ifa_ifu.ifu_broadaddr 'u#define ifa_broadaddr ifa_ifu.ifu_broadaddr /* broadcast address */#define ifa_dstaddr ifa_ifu.ifu_dstaddr /* other end of p-to-p link */.DEThe protocol generally maintains this structure as part of a largerstructure containing additional information concerning the address..PPEach interface has a send queue and routines used for initialization, \fIif_init\fP, and output, \fIif_output\fP.If the interface resides on a system bus, the routine \fIif_reset\fPwill be called after a bus reset has been performed. An interface may alsospecify a timer routine, \fIif_watchdog\fP;if \fIif_timer\fP is non-zero, it is decremented once per seconduntil it reaches zero, at which time the watchdog routine is called..PPThe state of an interface and certain characteristics are stored inthe \fIif_flags\fP field. The following values are possible:.DS._d#define IFF_UP 0x1 /* interface is up */#define IFF_BROADCAST 0x2 /* broadcast is possible */#define IFF_DEBUG 0x4 /* turn on debugging */#define IFF_LOOPBACK 0x8 /* is a loopback net */#define IFF_POINTOPOINT 0x10 /* interface is point-to-point link */#define IFF_NOTRAILERS 0x20 /* avoid use of trailers */#define IFF_RUNNING 0x40 /* resources allocated */#define IFF_NOARP 0x80 /* no address resolution protocol */.DEIf the interface is connected to a network which supports transmissionof \fIbroadcast\fP packets, the IFF_BROADCAST flag will be set andthe \fIifa_broadaddr\fP field will contain the address to be used insending or accepting a broadcast packet. If the interface is associatedwith a point-to-point hardware link (for example, a DEC DMR-11), theIFF_POINTOPOINT flag will be set and \fIifa_dstaddr\fP will contain theaddress of the host on the other side of the connection. These addressesand the local address of the interface, \fIif_addr\fP, are used infiltering incoming packets. The interface sets IFF_RUNNING afterit has allocated system resources and posted an initial read on thedevice it manages. This state bit is used to avoid multiple allocationrequests when an interface's address is changed. The IFF_NOTRAILERSflag indicates the interface should refrain from using a \fItrailer\fPencapsulation on outgoing packets, or (where per-host negotiationof trailers is possible) that trailer encapsulations should not be requested;\fItrailer\fP protocols are describedin section 14. The IFF_NOARP flag indicates the interface should notuse an ``address resolution protocol'' in mapping internetwork addressesto local network addresses..PPVarious statistics are also stored in the interface structure. Thesemay be viewed by users using the \fInetstat\fP(1) program..PPThe interface address and flags may be set with the SIOCSIFADDR andSIOCSIFFLAGS \fIioctl\fP\^s. SIOCSIFADDR is used initially to define eachinterface's address; SIOGSIFFLAGS can be used to markan interface down and perform site-specific configuration.The destination address of a point-to-point link is set with SIOCSIFDSTADDR.Corresponding operations exist to read each value.Protocol families may also support operations to set and read the broadcastaddress.In addition, the SIOCGIFCONF \fIioctl\fP retrieves a list of interfacenames and addresses for all interfaces and protocols on the host..NH 3UNIBUS interfaces.PPAll hardware related interfaces currently reside on the UNIBUS.Consequently a common set of utility routines for dealingwith the UNIBUS has been developed. Each UNIBUS interfaceutilizes a structure of the following form:.DS.ta \w'#define 'u +\w'ifw_xtofree 'u +\w'pte ifu_wmap[IF_MAXNUBAMR]; 'ustruct ifubinfo { short iff_uban; /* uba number */ short iff_hlen; /* local net header length */ struct uba_regs *iff_uba; /* uba regs, in vm */ short iff_flags; /* used during uballoc's */};.DEAdditional structures are associated with each receive and transmit buffer,normally one each per interface; for read,.DS.ta \w'#define 'u +\w'ifw_xtofree 'u +\w'pte ifu_wmap[IF_MAXNUBAMR]; 'ustruct ifrw { caddr_t ifrw_addr; /* virt addr of header */ short ifrw_bdp; /* unibus bdp */ short ifrw_flags; /* type, etc. */#define IFRW_W 0x01 /* is a transmit buffer */ int ifrw_info; /* value from ubaalloc */ int ifrw_proto; /* map register prototype */ struct pte *ifrw_mr; /* base of map registers */};.DEand for write,.DS.ta \w'#define 'u +\w'ifw_xtofree 'u +\w'pte ifu_wmap[IF_MAXNUBAMR]; 'ustruct ifxmt { struct ifrw ifrw; caddr_t ifw_base; /* virt addr of buffer */ struct pte ifw_wmap[IF_MAXNUBAMR]; /* base pages for output */ struct mbuf *ifw_xtofree; /* pages being dma'd out */ short ifw_xswapd; /* mask of clusters swapped */ short ifw_nmr; /* number of entries in wmap */};.ta \w'#define 'u +\w'ifw_xtofree 'u +\w'pte ifu_wmap[IF_MAXNUBAMR]; 'u#define ifw_addr ifrw.ifrw_addr#define ifw_bdp ifrw.ifrw_bdp#define ifw_flags ifrw.ifrw_flags#define ifw_info ifrw.ifrw_info#define ifw_proto ifrw.ifrw_proto#define ifw_mr ifrw.ifrw_mr.DEOne of each of these structures is conveniently packaged for interfaceswith single buffers for each direction, as follows:.DS.ta \w'#define 'u +\w'ifw_xtofree 'u +\w'pte ifu_wmap[IF_MAXNUBAMR]; 'ustruct ifuba { struct ifubinfo ifu_info; struct ifrw ifu_r; struct ifxmt ifu_xmt;};.ta \w'#define 'u +\w'ifw_xtofree 'u#define ifu_uban ifu_info.iff_uban#define ifu_hlen ifu_info.iff_hlen#define ifu_uba ifu_info.iff_uba#define ifu_flags ifu_info.iff_flags#define ifu_w ifu_xmt.ifrw#define ifu_xtofree ifu_xmt.ifw_xtofree.DE.PPThe \fIif_ubinfo\fP structure contains the general information neededto characterize the I/O-mapped buffers for the device.In addition, there is a structure describing each buffer, includingUNIBUS resources held by the interface.Sufficient memory pages and bus map registers are allocated to each bufferupon initialization according to the maximum packet size and header length.The kernel virtual address of the buffer is held in \fIifrw_addr\fP,and the map registers beginat \fIifrw_mr\fP. UNIBUS map register \fIifrw_mr\fP\^[\-1]maps the local network headerending on a page boundary. UNIBUS data paths arereserved for read and forwrite, given by \fIifrw_bdp\fP. The prototype of the mapregisters for read and for write is saved in \fIifrw_proto\fP..PPWhen write transfers are not at least half-full pages on page boundaries,the data are just copied into the pages mapped on the UNIBUSand the transfer is started.If a write transfer is at least half a page long and on a pageboundary, UNIBUS page table entries are swapped to referencethe pages, and then the initial pages areremapped from \fIifw_wmap\fP when the transfer completes.The mbufs containing the mapped pages are placed on the \fIifw_xtofree\fPqueue to be freed after transmission..PPWhen read transfers give at least half a page of data to be input, pageframes are allocated from a network page list and tradedwith the pages already containing the data, mapping the allocatedpages to replace the input pages for the next UNIBUS data input..PPThe following utility routines are available for use inwriting network interface drivers; all use thestructures described above..LPif_ubaminit(ifubinfo, uban, hlen, nmr, ifr, nr, ifx, nx);.brif_ubainit(ifuba, uban, hlen, nmr);.IP\fIif_ubaminit\fP allocates resources on UNIBUS adapter \fIuban\fP,storing the information in the \fIifubinfo\fP, \fIifrw\fP and \fIifxmt\fPstructures referenced.The \fIifr\fP and \fIifx\fP parameters are pointers to arraysof \fIifrw\fP and \fIifxmt\fP structures whose dimensionsare \fInr\fP and \fInx\fP, respectively.\fIif_ubainit\fP is a simpler, backwards-compatible interface usedfor hardware with single buffers of each type.They are called only at boot time or after a UNIBUS reset. One data path (buffered or unbuffered,depending on the \fIifu_flags\fP field) is allocated for each buffer.The \fInmr\fP parameter indicatesthe number of UNIBUS mapping registers required to map a maximalsized packet onto the UNIBUS, while \fIhlen\fP specifies the sizeof a local network header, if any, which should be mapped separatelyfrom the data (see the description of trailer protocols in chapter 14).Sufficient UNIBUS mapping registers and pages of memory are allocatedto initialize the input data path for an initial read. For the outputdata path, mapping registers and pages of memory are also allocatedand mapped onto the UNIBUS. The pages associated with the outputdata path are held in reserve in the event a write requires copyingnon-page-aligned data (see \fIif_wubaput\fP below).If \fIif_ubainit\fP is called with memory pages already allocated,they will be used instead of allocating new ones (this normallyoccurs after a UNIBUS reset).A 1 is returned when allocation and initialization are successful,0 otherwise..LPm = if_ubaget(ifubinfo, ifr, totlen, off0, ifp);.brm = if_rubaget(ifuba, totlen, off0, ifp);.IP\fIif_ubaget\fP and \fIif_rubaget\fP pull input dataout of an interface receive buffer and into an mbuf chain.The first interface passes pointers to the \fIifubinfo\fP structurefor the interface and the \fIifrw\fP structure for the receive buffer;the second call may be used for single-buffered devices.\fItotlen\fP specifies the length of data to be obtained, not counting thelocal network header. If \fIoff0\fP is non-zero, it indicatesa byte offset to a trailing local network header which should becopied into a separate mbuf and prepended to the front of the resultant mbufchain. When the data amount to at least a half a page,the previously mapped data pages are remappedinto the mbufs and swapped with fresh pages, thus avoidingany copy.The receiving interface is recorded as \fIifp\fP, a pointer to an \fIifnet\fPstructure, for the use of the receiving network protocol.A 0 return value indicates a failure to allocate resources..LPif_wubaput(ifubinfo, ifx, m);.brif_wubaput(ifuba, m);.IP\fIif_ubaput\fP and \fIif_wubaput\fP map a chain of mbufsonto a network interface in preparation for output.The first interface is used by devices with multiple transmit buffers.The chain includes any local networkheader, which is copied so that it resides in the mapped andaligned I/O space.Page-aligned data that are page-aligned in the output bufferare mapped to the UNIBUS in place of the normal buffer page,and the corresponding mbuf is placed on a queue to be freed after transmission.Any other mbufs which contained non-page-sizeddata portions are copied to the I/O space and then freed.Pages mapped from a previous output operation (no longer needed)are unmapped.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -