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

📄 tp_emit.c

📁 早期freebsd实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/*- * Copyright (c) 1991, 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. * *	@(#)tp_emit.c	8.1 (Berkeley) 6/10/93 *//***********************************************************		Copyright IBM Corporation 1987                      All Rights ReservedPermission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and thatboth that copyright notice and this permission notice appear in supporting documentation, and that the name of IBM not beused in advertising or publicity pertaining to distribution of thesoftware without specific, written prior permission.  IBM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDINGALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALLIBM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES ORANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THISSOFTWARE.******************************************************************//* * ARGO Project, Computer Sciences Dept., University of Wisconsin - Madison *//*  * ARGO TP * * $Header: tp_emit.c,v 5.5 88/11/18 17:27:20 nhall Exp $ * $Source: /usr/argo/sys/netiso/RCS/tp_emit.c,v $ * * This file contains tp_emit() and tp_error_emit(), which * form TPDUs and hand them to ip. * They take data in the form of mbuf chain, allocate mbufs as * necessary for headers, and set the fields as appropriate from * information found in the tpcb and net-level pcb. * * The worst thing about this code is adding the variable-length * options on a machine that requires alignment for any memory access * that isn't of size 1.  See the macro ADDOPTION() below. * * We don't do any concatenation. (There's a kludge to test the * basic mechanism of separation under the 'w' tpdebug option, that's all.) */#include <sys/param.h>#include <sys/systm.h>#include <sys/mbuf.h>#include <sys/socket.h>#include <sys/socketvar.h>#include <sys/protosw.h>#include <sys/errno.h>#include <sys/time.h>#include <netiso/iso.h>#include <netiso/iso_pcb.h>#include <netiso/argo_debug.h>#include <netiso/tp_timer.h>#include <netiso/tp_param.h>#include <netiso/tp_stat.h>#include <netiso/tp_pcb.h>#include <netiso/tp_tpdu.h>#include <netiso/tp_trace.h>#include <netiso/tp_meas.h>#include <netiso/tp_seq.h>#include <netiso/iso_errno.h>#include <net/if.h>#ifdef TRUE#undef FALSE#undef TRUE#endif#include <netccitt/x25.h>#include <netccitt/pk.h>#include <netccitt/pk_var.h>void iso_gen_csum();/* Here is a mighty kludge.  The token ring misorders packets if you * fire them at it too fast, and TP sans checksum is "too fast", so * we have introduced a delay when checksumming isn't used. */char tp_delay = 0x00; /* delay to keep token ring from blowing it *//* * NAME:	tp_emit() * * CALLED FROM: tp.trans and from tp_sbsend() * * FUNCTION and ARGUMENTS: * 	Emits one tpdu of the type (dutype), of the format appropriate * 	to the connection described by the pcb (tpcb), with sequence * 	number (seq) (where appropriate), end-of-tsdu bit (eot) where * 	appropriate, and with the data in the mbuf chain (data). * 	For DR and ER tpdus, the argument (eot) is * 	the reason for issuing the tpdu rather than an end-of-tsdu indicator. * * RETURNS:			 * 	0  OK * 	ENOBUFS  * 	E* returned from net layer output rtn  * * SIDE EFFECTS:	 * * NOTES:			 *   * 	WE ASSUME that the tp header + all options will fit in ONE mbuf.   *	If mbufs are 256 this will most likely be true, but if they are 128 it's *	possible that they won't.  *	If you used every option on the CR + max. user data you'd overrun  *	112 but unless you used > 115 bytes for the security *	parameter, it would fit in a 256-byte mbuf (240 bytes for the header) *	We don't support the security parameter, so this isn't a problem. *	If security is added, we ought to remove this assumption. * *  We do not implement the flow control confirmation "element of procedure". *  A) it should not affect interoperability, *  B) it should not be necessary - the protocol will eventually *   	straighten things out w/o FCC, as long as we don't have severely *		mismatched keepalive and inactivity timers, and *	C) it appears not to be REQUIRED, and *  D) it's incredibly grotesque, and no doubt will lengthen a few *   	critical paths. *  HOWEVER, we're thinking about putting it in anyway, for *  completeness, just like we did with ack subsequencing. */inttp_emit(dutype,	tpcb, seq, eot, data)	int dutype;	struct tp_pcb *tpcb;	SeqNum	seq;	u_int 	eot;	struct mbuf *data;{	register struct tpdu *hdr; 	register struct mbuf *m;	int csum_offset=0;	int datalen = 0;	int error = 0; 	SeqNum olduwe;	int acking_ooo;	/* NOTE:	 * here we treat tpdu_li as if it DID include the li field, up until	 * the end, at which time we subtract 1	 * THis is because if we subtract 1 right away, we end up adding	 * one every time we add an option.	 */	IFDEBUG(D_EMIT)		printf(	"tp_emit dutype 0x%x, tpcb 0x%x, eot 0x%x, seq 0x%x, data 0x%x",		dutype, tpcb, eot, seq, data);	ENDDEBUG	if (dutype == CR_TPDU || dutype == CC_TPDU) {		m = (struct mbuf *) malloc((u_long)256, M_MBUF, M_DONTWAIT);		if (m) {			m->m_type = TPMT_TPHDR;			mbstat.m_mtypes[TPMT_TPHDR]++;			m->m_next = MNULL;			m->m_nextpkt = MNULL;			m->m_data = m->m_pktdat;			m->m_flags = M_PKTHDR;		}	} else {		MGETHDR(m, M_DONTWAIT, TPMT_TPHDR); 	}	m->m_data += max_hdr;	if (m == NULL) {		if(data != (struct mbuf *)0)			m_freem(data);		error = ENOBUFS;		goto done;	}	m->m_len = sizeof(struct tpdu);	m->m_act = MNULL;	hdr = mtod(m, struct tpdu *);	bzero((caddr_t)hdr, sizeof(struct tpdu));	{		int 	tp_headersize();		hdr->tpdu_type = dutype;		hdr->tpdu_li = tp_headersize(dutype, tpcb);  		/*		 * class 0 doesn't use this for DT		 * it'll just get overwritten below 		 */		hdr->tpdu_dref = htons(tpcb->tp_fref); 		if( tpcb->tp_use_checksum || 			(dutype == CR_TPDU_type && (tpcb->tp_class & TP_CLASS_4) )) {			csum_offset =  hdr->tpdu_li + 2; /* DOESN'T include csum */			ADDOPTION(TPP_checksum, hdr, 2, eot /* dummy arg */);			IFDEBUG(D_CHKSUM)				printf(					"tp_emit: csum_offset 0x%x, hdr->tpdu_li 0x%x\n",						csum_offset, hdr->tpdu_li);			ENDDEBUG		} 		/*		 * VARIABLE PARTS...		 */		switch( dutype ) {		case CR_TPDU_type:			hdr->tpdu_CRdref_0 = 0;	/* must be zero */		case CC_TPDU_type: 			if (!tpcb->tp_cebit_off) {				tpcb->tp_win_recv = tp_start_win << 8;				LOCAL_CREDIT(tpcb);				CONG_INIT_SAMPLE(tpcb);			} else				LOCAL_CREDIT(tpcb);/* Case CC_TPDU_type used to be here */		{					u_char x;				hdr->tpdu_CCsref =  htons(tpcb->tp_lref); /* same as CRsref */				if( tpcb->tp_class > TP_CLASS_1 ) {					tpcb->tp_sent_uwe = tpcb->tp_lcredit -1;					tpcb->tp_sent_rcvnxt = 1;					tpcb->tp_sent_lcdt = tpcb->tp_lcredit;					hdr->tpdu_cdt = tpcb->tp_lcredit;				} else {#ifdef TPCONS					if (tpcb->tp_netservice == ISO_CONS) {						struct isopcb *isop = (struct isopcb *)tpcb->tp_npcb;						struct pklcd *lcp = (struct pklcd *)(isop->isop_chan);						lcp->lcd_flags &= ~X25_DG_CIRCUIT;					}#endif					hdr->tpdu_cdt = 0;				}				hdr->tpdu_CCclass = tp_mask_to_num(tpcb->tp_class);				hdr->tpdu_CCoptions = 					(tpcb->tp_xtd_format? TPO_XTD_FMT:0) |					(tpcb->tp_use_efc? TPO_USE_EFC:0);				IFPERF(tpcb)					u_char perf_meas = tpcb->tp_perf_on;					ADDOPTION(TPP_perf_meas, hdr, sizeof(perf_meas), perf_meas);				ENDPERF				if( dutype == CR_TPDU_type ) {					IncStat(ts_CR_sent);					ASSERT( tpcb->tp_lsuffixlen > 0 );					ASSERT( tpcb->tp_fsuffixlen > 0 );					ADDOPTION(TPP_calling_sufx, hdr,						tpcb->tp_lsuffixlen, tpcb->tp_lsuffix[0]);					ADDOPTION(TPP_called_sufx, hdr,						tpcb->tp_fsuffixlen, tpcb->tp_fsuffix[0]);				} else {					IncStat(ts_CC_sent);				}				ADDOPTION(TPP_tpdu_size, hdr, 					sizeof(tpcb->tp_tpdusize), tpcb->tp_tpdusize);				if (tpcb->tp_class != TP_CLASS_0) {					short millisec = 500*(tpcb->tp_sendack_ticks);					millisec = htons(millisec);					ADDOPTION(TPP_acktime, hdr, sizeof(short), millisec);					x = (tpcb->tp_use_nxpd? TPAO_USE_NXPD: 0)					 |	(tpcb->tp_use_rcc?  TPAO_USE_RCC : 0) 					 |  (tpcb->tp_use_checksum?0: TPAO_NO_CSUM)					 |	(tpcb->tp_xpd_service? TPAO_USE_TXPD: 0);					ADDOPTION(TPP_addl_opt, hdr, 1, x);					if ((tpcb->tp_l_tpdusize ^ (1 << tpcb->tp_tpdusize)) != 0) {						u_short size_s = tpcb->tp_l_tpdusize >> 7;						u_char size_c = size_s;						ASSERT(tpcb->tp_l_tpdusize < 65536 * 128);						if (dutype == CR_TPDU_type)							tpcb->tp_ptpdusize = size_s;						if (size_s < 256) {							ADDOPTION(TPP_ptpdu_size, hdr, 1, size_c);						} else {							size_s = htons(size_s);							ADDOPTION(TPP_ptpdu_size, hdr, 2, size_s);						}					}				}									if( (dutype == CR_TPDU_type) && (tpcb->tp_class != TP_CLASS_0)){					ASSERT( 1 == sizeof(tpcb->tp_vers) );					ADDOPTION(TPP_vers, hdr, 1, tpcb->tp_vers);					/* for each alt protocol class x,					 * 	x = x<<4;					 *  option = concat(option, x);					 * Well, for now we only have TP0 for an					 * alternative so... this is easy.					 *					 * HOWEVER... There should be NO alt protocol					 * class over CLNS.  Need to see if the route suggests					 * CONS, and iff so add alt class.					 */					x = 0;					ADDOPTION(TPP_alt_class, hdr, 1, x);				}				if( hdr->tpdu_li > MLEN) 					panic("tp_emit CR/CC");			}			break;		case DR_TPDU_type:			if( hdr->tpdu_DRdref == 0 ) {				/* don't issue the DR */				goto done;			}			hdr->tpdu_cdt = 0;			hdr->tpdu_DRsref = htons(tpcb->tp_lref);			hdr->tpdu_DRreason = (u_char)eot; /* WHICH BYTE OF THIS??? */			/* forget the add'l information variable part */			IncStat(ts_DR_sent);			break;		case DC_TPDU_type: /* not used in class 0 */			ASSERT( tpcb->tp_class != TP_CLASS_0);			hdr->tpdu_DCsref =  htons(tpcb->tp_lref);			hdr->tpdu_cdt = 0;			data = (struct mbuf *)0;			IncStat(ts_DC_sent);			break;		case XAK_TPDU_type: /* xak not used in class 0 */			ASSERT( tpcb->tp_class != TP_CLASS_0); /* fall through */			hdr->tpdu_cdt = 0;			IFTRACE(D_XPD)				tptraceTPCB(TPPTXack, seq, 0, 0, 0, 0);			ENDTRACE			data = (struct mbuf *)0;			if (tpcb->tp_xtd_format) {#ifdef BYTE_ORDER				union seq_type seqeotX;				seqeotX.s_seq = seq;				seqeotX.s_eot = 1;				hdr->tpdu_seqeotX = htonl(seqeotX.s_seqeot);#else				hdr->tpdu_XAKseqX = seq;#endif /* BYTE_ORDER */			} else {				hdr->tpdu_XAKseq = seq;			}			IncStat(ts_XAK_sent);			IncPStat(tpcb, tps_XAK_sent);			break;		case XPD_TPDU_type: /* xpd not used in class 0 */			ASSERT( tpcb->tp_class != TP_CLASS_0); /* fall through */			hdr->tpdu_cdt = 0;			if (tpcb->tp_xtd_format) {#ifdef BYTE_ORDER				union seq_type seqeotX;				seqeotX.s_seq = seq;				seqeotX.s_eot = 1;				hdr->tpdu_seqeotX = htonl(seqeotX.s_seqeot);#else				hdr->tpdu_XPDseqX = seq;				hdr->tpdu_XPDeotX = 1; /* always 1 for XPD tpdu */#endif /* BYTE_ORDER */			} else {				hdr->tpdu_XPDseq = seq;				hdr->tpdu_XPDeot = 1; /* always 1 for XPD tpdu */			}			IncStat(ts_XPD_sent);			IncPStat(tpcb, tps_XPD_sent);			/* kludge to test the input size checking */			IFDEBUG(D_SIZE_CHECK)				/*if(data->m_len <= 16 && data->m_off < (MLEN-18) ) {					printf("Sending too much data on XPD: 18 bytes\n");					data->m_len = 18;				}*/			ENDDEBUG			break;		case DT_TPDU_type:			hdr->tpdu_cdt = 0;			IFTRACE(D_DATA)				tptraceTPCB(TPPTmisc, "emit DT: eot seq tpdu_li", eot, seq, 					hdr->tpdu_li, 0);			ENDTRACE			if (tpcb->tp_xtd_format) {#ifdef BYTE_ORDER				union seq_type seqeotX;				seqeotX.s_seq = seq;				seqeotX.s_eot = eot;				hdr->tpdu_seqeotX = htonl(seqeotX.s_seqeot);#else				hdr->tpdu_DTseqX = seq;				hdr->tpdu_DTeotX = eot;#endif /* BYTE_ORDER */			} else if (tpcb->tp_class == TP_CLASS_0) {				IFDEBUG(D_EMIT)					printf("DT tpdu: class 0 m 0x%x hdr 0x%x\n", m, hdr);					dump_buf( hdr, hdr->tpdu_li + 1 );				ENDDEBUG				((struct tp0du *)hdr)->tp0du_eot = eot;				((struct tp0du *)hdr)->tp0du_mbz = 0;				IFDEBUG(D_EMIT)					printf("DT 2 tpdu: class 0 m 0x%x hdr 0x%x\n", m, hdr);					dump_buf( hdr, hdr->tpdu_li + 1 );				ENDDEBUG			} else {				hdr->tpdu_DTseq = seq;				hdr->tpdu_DTeot = eot;			}			if(eot) {				IncStat(ts_EOT_sent);			}			IncStat(ts_DT_sent);			IncPStat(tpcb, tps_DT_sent);			break;		case AK_TPDU_type:/* ak not used in class 0 */			ASSERT( tpcb->tp_class != TP_CLASS_0); 			data = (struct mbuf *)0;			olduwe = tpcb->tp_sent_uwe;			if (seq != tpcb->tp_sent_rcvnxt || tpcb->tp_rsycnt == 0) {				LOCAL_CREDIT( tpcb ); 				tpcb->tp_sent_uwe = 					SEQ(tpcb,tpcb->tp_rcvnxt + tpcb->tp_lcredit -1);				tpcb->tp_sent_lcdt = tpcb->tp_lcredit;				acking_ooo = 0;			} else				acking_ooo = 1;			IFDEBUG(D_RENEG)				/* occasionally fake a reneging so 					you can test subsequencing */				if( olduwe & 0x1 ) {					tpcb->tp_reneged = 1;					IncStat(ts_ldebug);				}			ENDDEBUG			/* Are we about to reneg on credit? 			 * When might we do so?			 *	a) when using optimistic credit (which we no longer do).			 *  b) when drain() gets implemented (not in the plans).			 *  c) when D_RENEG is on.			 *  d) when DEC BIT response is implemented.			 *	(not- when we do this, we'll need to implement flow control			 *	confirmation)			 */			if( SEQ_LT(tpcb, tpcb->tp_sent_uwe, olduwe) ) {

⌨️ 快捷键说明

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