12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391 |
- #include "u.h"
- #include "../port/lib.h"
- #include "mem.h"
- #include "dat.h"
- #include "fns.h"
- #include "../port/error.h"
- #include "ip.h"
- enum /* Connection state */
- {
- Ilclosed,
- Ilsyncer,
- Ilsyncee,
- Ilestablished,
- Illistening,
- Ilclosing,
- Ilopening, /* only for file server */
- };
- char *ilstates[] =
- {
- "Closed",
- "Syncer",
- "Syncee",
- "Established",
- "Listening",
- "Closing",
- "Opening", /* only for file server */
- };
- enum /* Packet types */
- {
- Ilsync,
- Ildata,
- Ildataquery,
- Ilack,
- Ilquery,
- Ilstate,
- Ilclose,
- };
- char *iltype[] =
- {
- "sync",
- "data",
- "dataquery",
- "ack",
- "query",
- "state",
- "close"
- };
- enum
- {
- Seconds = 1000,
- Iltickms = 50, /* time base */
- AckDelay = 2*Iltickms, /* max time twixt message rcvd & ack sent */
- MaxTimeout = 30*Seconds, /* max time between rexmit */
- QueryTime = 10*Seconds, /* time between subsequent queries */
- DeathTime = 30*QueryTime,
- MaxRexmit = 16, /* max retransmissions before hangup */
- Defaultwin = 20,
- LogAGain = 3,
- AGain = 1<<LogAGain,
- LogDGain = 2,
- DGain = 1<<LogDGain,
- DefByteRate = 100, /* assume a megabit link */
- DefRtt = 50, /* cross country on a great day */
- };
- enum
- {
- Nqt= 8,
- };
- typedef struct Ilcb Ilcb;
- struct Ilcb /* Control block */
- {
- int state; /* Connection state */
- Conv *conv;
- QLock ackq; /* Unacknowledged queue */
- Block *unacked;
- Block *unackedtail;
- ulong unackedbytes;
- QLock outo; /* Out of order packet queue */
- Block *outoforder;
- ulong next; /* Id of next to send */
- ulong recvd; /* Last packet received */
- ulong acksent; /* Last packet acked */
- ulong start; /* Local start id */
- ulong rstart; /* Remote start id */
- int window; /* Maximum receive window */
- int rxquery; /* number of queries on this connection */
- int rxtot; /* number of retransmits on this connection */
- int rexmit; /* number of retransmits of *unacked */
- ulong qt[Nqt+1]; /* state table for query messages */
- int qtx; /* ... index into qt */
- /* if set, fasttimeout causes a connection request to terminate after 4*Iltickms */
- int fasttimeout;
- /* timers */
- ulong lastxmit; /* time of last xmit */
- ulong lastrecv; /* time of last recv */
- ulong timeout; /* retransmission time for *unacked */
- ulong acktime; /* time to send next ack */
- ulong querytime; /* time to send next query */
- /* adaptive measurements */
- int delay; /* Average of the fixed rtt delay */
- int rate; /* Average uchar rate */
- int mdev; /* Mean deviation of rtt */
- int maxrtt; /* largest rtt seen */
- ulong rttack; /* The ack we are waiting for */
- int rttlen; /* Length of rttack packet */
- uvlong rttstart; /* Time we issued rttack packet */
- };
- enum
- {
- IL_IPSIZE = 20,
- IL_HDRSIZE = 18,
- IL_LISTEN = 0,
- IL_CONNECT = 1,
- IP_ILPROTO = 40,
- };
- typedef struct Ilhdr Ilhdr;
- struct Ilhdr
- {
- uchar vihl; /* Version and header length */
- uchar tos; /* Type of service */
- uchar length[2]; /* packet length */
- uchar id[2]; /* Identification */
- uchar frag[2]; /* Fragment information */
- uchar ttl; /* Time to live */
- uchar proto; /* Protocol */
- uchar cksum[2]; /* Header checksum */
- uchar src[4]; /* Ip source */
- uchar dst[4]; /* Ip destination */
- uchar ilsum[2]; /* Checksum including header */
- uchar illen[2]; /* Packet length */
- uchar iltype; /* Packet type */
- uchar ilspec; /* Special */
- uchar ilsrc[2]; /* Src port */
- uchar ildst[2]; /* Dst port */
- uchar ilid[4]; /* Sequence id */
- uchar ilack[4]; /* Acked sequence */
- };
- enum
- {
- InMsgs,
- OutMsgs,
- CsumErrs, /* checksum errors */
- HlenErrs, /* header length error */
- LenErrs, /* short packet */
- OutOfOrder, /* out of order */
- Retrans, /* retransmissions */
- DupMsg,
- DupBytes,
- Nstats,
- };
- static char *statnames[] =
- {
- [InMsgs] "InMsgs",
- [OutMsgs] "OutMsgs",
- [CsumErrs] "CsumErrs",
- [HlenErrs] "HlenErr",
- [LenErrs] "LenErrs",
- [OutOfOrder] "OutOfOrder",
- [Retrans] "Retrans",
- [DupMsg] "DupMsg",
- [DupBytes] "DupBytes",
- };
- typedef struct Ilpriv Ilpriv;
- struct Ilpriv
- {
- Ipht ht;
- ulong stats[Nstats];
- ulong csumerr; /* checksum errors */
- ulong hlenerr; /* header length error */
- ulong lenerr; /* short packet */
- ulong order; /* out of order */
- ulong rexmit; /* retransmissions */
- ulong dup;
- ulong dupb;
- /* keeping track of the ack kproc */
- int ackprocstarted;
- QLock apl;
- };
- /* state for query/dataquery messages */
- void ilrcvmsg(Conv*, Block*);
- void ilsendctl(Conv*, Ilhdr*, int, ulong, ulong, int);
- void ilackq(Ilcb*, Block*);
- void ilprocess(Conv*, Ilhdr*, Block*);
- void ilpullup(Conv*);
- void ilhangup(Conv*, char*);
- void ilfreeq(Ilcb*);
- void ilrexmit(Ilcb*);
- void ilbackoff(Ilcb*);
- void ilsettimeout(Ilcb*);
- char* ilstart(Conv*, int, int);
- void ilackproc(void*);
- void iloutoforder(Conv*, Ilhdr*, Block*);
- void iliput(Proto*, Ipifc*, Block*);
- void iladvise(Proto*, Block*, char*);
- int ilnextqt(Ilcb*);
- void ilcbinit(Ilcb*);
- int later(ulong, ulong, char*);
- void ilreject(Fs*, Ilhdr*);
- void illocalclose(Conv *c);
- int ilcksum = 1;
- static int initseq = 25001;
- static ulong scalediv, scalemul;
- static char *etime = "connection timed out";
- static char*
- ilconnect(Conv *c, char **argv, int argc)
- {
- char *e, *p;
- int fast;
- /* huge hack to quickly try an il connection */
- fast = 0;
- if(argc > 1){
- p = strstr(argv[1], "!fasttimeout");
- if(p != nil){
- *p = 0;
- fast = 1;
- }
- }
- e = Fsstdconnect(c, argv, argc);
- if(e != nil)
- return e;
- return ilstart(c, IL_CONNECT, fast);
- }
- static int
- ilstate(Conv *c, char *state, int n)
- {
- Ilcb *ic;
- ic = (Ilcb*)(c->ptcl);
- return snprint(state, n, "%s del %5.5d Br %5.5d md %5.5d una %5.5lud rex %5.5d rxq %5.5d max %5.5d",
- ilstates[ic->state],
- ic->delay>>LogAGain, ic->rate>>LogAGain, ic->mdev>>LogDGain,
- ic->unackedbytes, ic->rxtot, ic->rxquery, ic->maxrtt);
- }
- static int
- ilinuse(Conv *c)
- {
- Ilcb *ic;
- ic = (Ilcb*)(c->ptcl);
- return ic->state != Ilclosed;
- }
- /* called with c locked */
- static char*
- ilannounce(Conv *c, char **argv, int argc)
- {
- char *e;
- e = Fsstdannounce(c, argv, argc);
- if(e != nil)
- return e;
- e = ilstart(c, IL_LISTEN, 0);
- if(e != nil)
- return e;
- Fsconnected(c, nil);
- return nil;
- }
- void
- illocalclose(Conv *c)
- {
- Ilcb *ic;
- Ilpriv *ipriv;
- ipriv = c->p->priv;
- ic = (Ilcb*)c->ptcl;
- ic->state = Ilclosed;
- iphtrem(&ipriv->ht, c);
- ipmove(c->laddr, IPnoaddr);
- c->lport = 0;
- }
- static void
- ilclose(Conv *c)
- {
- Ilcb *ic;
- ic = (Ilcb*)c->ptcl;
- qclose(c->rq);
- qclose(c->wq);
- qclose(c->eq);
- switch(ic->state) {
- case Ilclosing:
- case Ilclosed:
- break;
- case Ilsyncer:
- case Ilsyncee:
- case Ilestablished:
- ic->state = Ilclosing;
- ilsettimeout(ic);
- ilsendctl(c, nil, Ilclose, ic->next, ic->recvd, 0);
- break;
- case Illistening:
- illocalclose(c);
- break;
- }
- ilfreeq(ic);
- }
- void
- ilkick(void *x, Block *bp)
- {
- Conv *c = x;
- Ilhdr *ih;
- Ilcb *ic;
- int dlen;
- ulong id, ack;
- Fs *f;
- Ilpriv *priv;
- f = c->p->f;
- priv = c->p->priv;
- ic = (Ilcb*)c->ptcl;
- if(bp == nil)
- return;
- switch(ic->state) {
- case Ilclosed:
- case Illistening:
- case Ilclosing:
- freeblist(bp);
- qhangup(c->rq, nil);
- return;
- }
- dlen = blocklen(bp);
- /* Make space to fit il & ip */
- bp = padblock(bp, IL_IPSIZE+IL_HDRSIZE);
- ih = (Ilhdr *)(bp->rp);
- ih->vihl = IP_VER4;
- /* Ip fields */
- ih->frag[0] = 0;
- ih->frag[1] = 0;
- v6tov4(ih->dst, c->raddr);
- v6tov4(ih->src, c->laddr);
- ih->proto = IP_ILPROTO;
- /* Il fields */
- hnputs(ih->illen, dlen+IL_HDRSIZE);
- hnputs(ih->ilsrc, c->lport);
- hnputs(ih->ildst, c->rport);
- qlock(&ic->ackq);
- id = ic->next++;
- hnputl(ih->ilid, id);
- ack = ic->recvd;
- hnputl(ih->ilack, ack);
- ic->acksent = ack;
- ic->acktime = NOW + AckDelay;
- ih->iltype = Ildata;
- ih->ilspec = 0;
- ih->ilsum[0] = 0;
- ih->ilsum[1] = 0;
- /* Checksum of ilheader plus data (not ip & no pseudo header) */
- if(ilcksum)
- hnputs(ih->ilsum, ptclcsum(bp, IL_IPSIZE, dlen+IL_HDRSIZE));
- ilackq(ic, bp);
- qunlock(&ic->ackq);
- /* Start the round trip timer for this packet if the timer is free */
- if(ic->rttack == 0) {
- ic->rttack = id;
- ic->rttstart = fastticks(nil);
- ic->rttlen = dlen + IL_IPSIZE + IL_HDRSIZE;
- }
- if(later(NOW, ic->timeout, nil))
- ilsettimeout(ic);
- ipoput4(f, bp, 0, c->ttl, c->tos, c);
- priv->stats[OutMsgs]++;
- }
- static void
- ilcreate(Conv *c)
- {
- c->rq = qopen(64*1024, 0, 0, c);
- c->wq = qbypass(ilkick, c);
- }
- int
- ilxstats(Proto *il, char *buf, int len)
- {
- Ilpriv *priv;
- char *p, *e;
- int i;
- priv = il->priv;
- p = buf;
- e = p+len;
- for(i = 0; i < Nstats; i++)
- p = seprint(p, e, "%s: %lud\n", statnames[i], priv->stats[i]);
- return p - buf;
- }
- void
- ilackq(Ilcb *ic, Block *bp)
- {
- Block *np;
- int n;
- n = blocklen(bp);
- /* Enqueue a copy on the unacked queue in case this one gets lost */
- np = copyblock(bp, n);
- if(ic->unacked)
- ic->unackedtail->list = np;
- else
- ic->unacked = np;
- ic->unackedtail = np;
- np->list = nil;
- ic->unackedbytes += n;
- }
- static
- void
- ilrttcalc(Ilcb *ic, Block *bp)
- {
- int rtt, tt, pt, delay, rate;
- rtt = fastticks(nil) - ic->rttstart;
- rtt = (rtt*scalemul)/scalediv;
- delay = ic->delay;
- rate = ic->rate;
- /* Guard against zero wrap */
- if(rtt > 120000 || rtt < 0)
- return;
- /* this block had to be transmitted after the one acked so count its size */
- ic->rttlen += blocklen(bp) + IL_IPSIZE + IL_HDRSIZE;
- if(ic->rttlen < 256){
- /* guess fixed delay as rtt of small packets */
- delay += rtt - (delay>>LogAGain);
- if(delay < AGain)
- delay = AGain;
- ic->delay = delay;
- } else {
- /* if packet took longer than avg rtt delay, recalc rate */
- tt = rtt - (delay>>LogAGain);
- if(tt > 0){
- rate += ic->rttlen/tt - (rate>>LogAGain);
- if(rate < AGain)
- rate = AGain;
- ic->rate = rate;
- }
- }
- /* mdev */
- pt = ic->rttlen/(rate>>LogAGain) + (delay>>LogAGain);
- ic->mdev += abs(rtt-pt) - (ic->mdev>>LogDGain);
- if(rtt > ic->maxrtt)
- ic->maxrtt = rtt;
- }
- void
- ilackto(Ilcb *ic, ulong ackto, Block *bp)
- {
- Ilhdr *h;
- ulong id;
- if(ic->rttack == ackto)
- ilrttcalc(ic, bp);
- /* Cancel if we've passed the packet we were interested in */
- if(ic->rttack <= ackto)
- ic->rttack = 0;
- qlock(&ic->ackq);
- while(ic->unacked) {
- h = (Ilhdr *)ic->unacked->rp;
- id = nhgetl(h->ilid);
- if(ackto < id)
- break;
- bp = ic->unacked;
- ic->unacked = bp->list;
- bp->list = nil;
- ic->unackedbytes -= blocklen(bp);
- freeblist(bp);
- ic->rexmit = 0;
- ilsettimeout(ic);
- }
- qunlock(&ic->ackq);
- }
- void
- iliput(Proto *il, Ipifc*, Block *bp)
- {
- char *st;
- Ilcb *ic;
- Ilhdr *ih;
- uchar raddr[IPaddrlen];
- uchar laddr[IPaddrlen];
- ushort sp, dp, csum;
- int plen, illen;
- Conv *new, *s;
- Ilpriv *ipriv;
- ipriv = il->priv;
- ih = (Ilhdr *)bp->rp;
- plen = blocklen(bp);
- if(plen < IL_IPSIZE+IL_HDRSIZE){
- netlog(il->f, Logil, "il: hlenerr\n");
- ipriv->stats[HlenErrs]++;
- goto raise;
- }
- illen = nhgets(ih->illen);
- if(illen+IL_IPSIZE > plen){
- netlog(il->f, Logil, "il: lenerr\n");
- ipriv->stats[LenErrs]++;
- goto raise;
- }
- sp = nhgets(ih->ildst);
- dp = nhgets(ih->ilsrc);
- v4tov6(raddr, ih->src);
- v4tov6(laddr, ih->dst);
- if((csum = ptclcsum(bp, IL_IPSIZE, illen)) != 0) {
- if(ih->iltype < 0 || ih->iltype > Ilclose)
- st = "?";
- else
- st = iltype[ih->iltype];
- ipriv->stats[CsumErrs]++;
- netlog(il->f, Logil, "il: cksum %ux %ux, pkt(%s id %lud ack %lud %I/%d->%d)\n",
- csum, st, nhgetl(ih->ilid), nhgetl(ih->ilack), raddr, sp, dp);
- goto raise;
- }
- qlock(il);
- s = iphtlook(&ipriv->ht, raddr, dp, laddr, sp);
- if(s == nil){
- if(ih->iltype == Ilsync)
- ilreject(il->f, ih); /* no listener */
- qunlock(il);
- goto raise;
- }
- ic = (Ilcb*)s->ptcl;
- if(ic->state == Illistening){
- if(ih->iltype != Ilsync){
- qunlock(il);
- if(ih->iltype < 0 || ih->iltype > Ilclose)
- st = "?";
- else
- st = iltype[ih->iltype];
- ilreject(il->f, ih); /* no channel and not sync */
- netlog(il->f, Logil, "il: no channel, pkt(%s id %lud ack %lud %I/%ud->%ud)\n",
- st, nhgetl(ih->ilid), nhgetl(ih->ilack), raddr, sp, dp);
- goto raise;
- }
- new = Fsnewcall(s, raddr, dp, laddr, sp, V4);
- if(new == nil){
- qunlock(il);
- netlog(il->f, Logil, "il: bad newcall %I/%ud->%ud\n", raddr, sp, dp);
- ilsendctl(s, ih, Ilclose, 0, nhgetl(ih->ilid), 0);
- goto raise;
- }
- s = new;
- ic = (Ilcb*)s->ptcl;
-
- ic->conv = s;
- ic->state = Ilsyncee;
- ilcbinit(ic);
- ic->rstart = nhgetl(ih->ilid);
- iphtadd(&ipriv->ht, s);
- }
- qlock(s);
- qunlock(il);
- if(waserror()){
- qunlock(s);
- nexterror();
- }
- ilprocess(s, ih, bp);
- qunlock(s);
- poperror();
- return;
- raise:
- freeblist(bp);
- }
- void
- _ilprocess(Conv *s, Ilhdr *h, Block *bp)
- {
- Ilcb *ic;
- ulong id, ack;
- Ilpriv *priv;
- id = nhgetl(h->ilid);
- ack = nhgetl(h->ilack);
- ic = (Ilcb*)s->ptcl;
- ic->lastrecv = NOW;
- ic->querytime = NOW + QueryTime;
- priv = s->p->priv;
- priv->stats[InMsgs]++;
- switch(ic->state) {
- default:
- netlog(s->p->f, Logil, "il: unknown state %d\n", ic->state);
- case Ilclosed:
- freeblist(bp);
- break;
- case Ilsyncer:
- switch(h->iltype) {
- default:
- break;
- case Ilsync:
- if(ack != ic->start)
- ilhangup(s, "connection rejected");
- else {
- ic->recvd = id;
- ic->rstart = id;
- ilsendctl(s, nil, Ilack, ic->next, ic->recvd, 0);
- ic->state = Ilestablished;
- ic->fasttimeout = 0;
- ic->rexmit = 0;
- Fsconnected(s, nil);
- ilpullup(s);
- }
- break;
- case Ilclose:
- if(ack == ic->start)
- ilhangup(s, "connection rejected");
- break;
- }
- freeblist(bp);
- break;
- case Ilsyncee:
- switch(h->iltype) {
- default:
- break;
- case Ilsync:
- if(id != ic->rstart || ack != 0){
- illocalclose(s);
- } else {
- ic->recvd = id;
- ilsendctl(s, nil, Ilsync, ic->start, ic->recvd, 0);
- }
- break;
- case Ilack:
- if(ack == ic->start) {
- ic->state = Ilestablished;
- ic->fasttimeout = 0;
- ic->rexmit = 0;
- ilpullup(s);
- }
- break;
- case Ildata:
- if(ack == ic->start) {
- ic->state = Ilestablished;
- ic->fasttimeout = 0;
- ic->rexmit = 0;
- goto established;
- }
- break;
- case Ilclose:
- if(ack == ic->start)
- ilhangup(s, "remote close");
- break;
- }
- freeblist(bp);
- break;
- case Ilestablished:
- established:
- switch(h->iltype) {
- case Ilsync:
- if(id != ic->rstart)
- ilhangup(s, "remote close");
- else
- ilsendctl(s, nil, Ilack, ic->next, ic->rstart, 0);
- freeblist(bp);
- break;
- case Ildata:
- ilackto(ic, ack, bp);
- iloutoforder(s, h, bp);
- ilpullup(s);
- break;
- case Ildataquery:
- ilackto(ic, ack, bp);
- iloutoforder(s, h, bp);
- ilpullup(s);
- ilsendctl(s, nil, Ilstate, ic->next, ic->recvd, h->ilspec);
- break;
- case Ilack:
- ilackto(ic, ack, bp);
- freeblist(bp);
- break;
- case Ilquery:
- ilackto(ic, ack, bp);
- ilsendctl(s, nil, Ilstate, ic->next, ic->recvd, h->ilspec);
- freeblist(bp);
- break;
- case Ilstate:
- if(ack >= ic->rttack)
- ic->rttack = 0;
- ilackto(ic, ack, bp);
- if(h->ilspec > Nqt)
- h->ilspec = 0;
- if(ic->qt[h->ilspec] > ack){
- ilrexmit(ic);
- ilsettimeout(ic);
- }
- freeblist(bp);
- break;
- case Ilclose:
- freeblist(bp);
- if(ack < ic->start || ack > ic->next)
- break;
- ic->recvd = id;
- ilsendctl(s, nil, Ilclose, ic->next, ic->recvd, 0);
- ic->state = Ilclosing;
- ilsettimeout(ic);
- ilfreeq(ic);
- break;
- }
- break;
- case Illistening:
- freeblist(bp);
- break;
- case Ilclosing:
- switch(h->iltype) {
- case Ilclose:
- ic->recvd = id;
- ilsendctl(s, nil, Ilclose, ic->next, ic->recvd, 0);
- if(ack == ic->next)
- ilhangup(s, nil);
- break;
- default:
- break;
- }
- freeblist(bp);
- break;
- }
- }
- void
- ilrexmit(Ilcb *ic)
- {
- Ilhdr *h;
- Block *nb;
- Conv *c;
- ulong id;
- Ilpriv *priv;
- nb = nil;
- qlock(&ic->ackq);
- if(ic->unacked)
- nb = copyblock(ic->unacked, blocklen(ic->unacked));
- qunlock(&ic->ackq);
- if(nb == nil)
- return;
- h = (Ilhdr*)nb->rp;
- h->vihl = IP_VER4;
- h->iltype = Ildataquery;
- hnputl(h->ilack, ic->recvd);
- h->ilspec = ilnextqt(ic);
- h->ilsum[0] = 0;
- h->ilsum[1] = 0;
- hnputs(h->ilsum, ptclcsum(nb, IL_IPSIZE, nhgets(h->illen)));
- c = ic->conv;
- id = nhgetl(h->ilid);
- netlog(c->p->f, Logil, "il: rexmit %d %ud: %d %d: %i %d/%d\n", id, ic->recvd,
- ic->rexmit, ic->timeout,
- c->raddr, c->lport, c->rport);
- ilbackoff(ic);
- ipoput4(c->p->f, nb, 0, c->ttl, c->tos, c);
- /* statistics */
- ic->rxtot++;
- priv = c->p->priv;
- priv->rexmit++;
- }
- /* DEBUG */
- void
- ilprocess(Conv *s, Ilhdr *h, Block *bp)
- {
- Ilcb *ic;
- ic = (Ilcb*)s->ptcl;
- USED(ic);
- netlog(s->p->f, Logilmsg, "%11s rcv %d/%d snt %d/%d pkt(%s id %d ack %d %d->%d) ",
- ilstates[ic->state], ic->rstart, ic->recvd, ic->start,
- ic->next, iltype[h->iltype], nhgetl(h->ilid),
- nhgetl(h->ilack), nhgets(h->ilsrc), nhgets(h->ildst));
- _ilprocess(s, h, bp);
- netlog(s->p->f, Logilmsg, "%11s rcv %d snt %d\n", ilstates[ic->state], ic->recvd, ic->next);
- }
- void
- ilhangup(Conv *s, char *msg)
- {
- Ilcb *ic;
- int callout;
- netlog(s->p->f, Logil, "il: hangup! %I %d/%d: %s\n", s->raddr,
- s->lport, s->rport, msg?msg:"no reason");
- ic = (Ilcb*)s->ptcl;
- callout = ic->state == Ilsyncer;
- illocalclose(s);
- qhangup(s->rq, msg);
- qhangup(s->wq, msg);
- if(callout)
- Fsconnected(s, msg);
- }
- void
- ilpullup(Conv *s)
- {
- Ilcb *ic;
- Ilhdr *oh;
- Block *bp;
- ulong oid, dlen;
- Ilpriv *ipriv;
- ic = (Ilcb*)s->ptcl;
- if(ic->state != Ilestablished)
- return;
- qlock(&ic->outo);
- while(ic->outoforder) {
- bp = ic->outoforder;
- oh = (Ilhdr*)bp->rp;
- oid = nhgetl(oh->ilid);
- if(oid <= ic->recvd) {
- ic->outoforder = bp->list;
- freeblist(bp);
- continue;
- }
- if(oid != ic->recvd+1){
- ipriv = s->p->priv;
- ipriv->stats[OutOfOrder]++;
- break;
- }
- ic->recvd = oid;
- ic->outoforder = bp->list;
- bp->list = nil;
- dlen = nhgets(oh->illen)-IL_HDRSIZE;
- bp = trimblock(bp, IL_IPSIZE+IL_HDRSIZE, dlen);
- /*
- * Upper levels don't know about multiple-block
- * messages so copy all into one (yick).
- */
- bp = concatblock(bp);
- if(bp == 0)
- panic("ilpullup");
- bp = packblock(bp);
- if(bp == 0)
- panic("ilpullup2");
- qpassnolim(s->rq, bp);
- }
- qunlock(&ic->outo);
- }
- void
- iloutoforder(Conv *s, Ilhdr *h, Block *bp)
- {
- Ilcb *ic;
- uchar *lid;
- Block *f, **l;
- ulong id, newid;
- Ilpriv *ipriv;
- ipriv = s->p->priv;
- ic = (Ilcb*)s->ptcl;
- bp->list = nil;
- id = nhgetl(h->ilid);
- /* Window checks */
- if(id <= ic->recvd || id > ic->recvd+ic->window) {
- netlog(s->p->f, Logil, "il: message outside window %ud <%ud-%ud>: %i %d/%d\n",
- id, ic->recvd, ic->recvd+ic->window, s->raddr, s->lport, s->rport);
- freeblist(bp);
- return;
- }
- /* Packet is acceptable so sort onto receive queue for pullup */
- qlock(&ic->outo);
- if(ic->outoforder == nil)
- ic->outoforder = bp;
- else {
- l = &ic->outoforder;
- for(f = *l; f; f = f->list) {
- lid = ((Ilhdr*)(f->rp))->ilid;
- newid = nhgetl(lid);
- if(id <= newid) {
- if(id == newid) {
- ipriv->stats[DupMsg]++;
- ipriv->stats[DupBytes] += blocklen(bp);
- qunlock(&ic->outo);
- freeblist(bp);
- return;
- }
- bp->list = f;
- *l = bp;
- qunlock(&ic->outo);
- return;
- }
- l = &f->list;
- }
- *l = bp;
- }
- qunlock(&ic->outo);
- }
- void
- ilsendctl(Conv *ipc, Ilhdr *inih, int type, ulong id, ulong ack, int ilspec)
- {
- Ilhdr *ih;
- Ilcb *ic;
- Block *bp;
- int ttl, tos;
- bp = allocb(IL_IPSIZE+IL_HDRSIZE);
- bp->wp += IL_IPSIZE+IL_HDRSIZE;
- ih = (Ilhdr *)(bp->rp);
- ih->vihl = IP_VER4;
- /* Ip fields */
- ih->proto = IP_ILPROTO;
- hnputs(ih->illen, IL_HDRSIZE);
- ih->frag[0] = 0;
- ih->frag[1] = 0;
- if(inih) {
- hnputl(ih->dst, nhgetl(inih->src));
- hnputl(ih->src, nhgetl(inih->dst));
- hnputs(ih->ilsrc, nhgets(inih->ildst));
- hnputs(ih->ildst, nhgets(inih->ilsrc));
- hnputl(ih->ilid, nhgetl(inih->ilack));
- hnputl(ih->ilack, nhgetl(inih->ilid));
- ttl = MAXTTL;
- tos = DFLTTOS;
- }
- else {
- v6tov4(ih->dst, ipc->raddr);
- v6tov4(ih->src, ipc->laddr);
- hnputs(ih->ilsrc, ipc->lport);
- hnputs(ih->ildst, ipc->rport);
- hnputl(ih->ilid, id);
- hnputl(ih->ilack, ack);
- ic = (Ilcb*)ipc->ptcl;
- ic->acksent = ack;
- ic->acktime = NOW;
- ttl = ipc->ttl;
- tos = ipc->tos;
- }
- ih->iltype = type;
- ih->ilspec = ilspec;
- ih->ilsum[0] = 0;
- ih->ilsum[1] = 0;
- if(ilcksum)
- hnputs(ih->ilsum, ptclcsum(bp, IL_IPSIZE, IL_HDRSIZE));
- if(ipc==nil)
- panic("ipc is nil caller is %.8lux", getcallerpc(&ipc));
- if(ipc->p==nil)
- panic("ipc->p is nil");
- netlog(ipc->p->f, Logilmsg, "ctl(%s id %d ack %d %d->%d)\n",
- iltype[ih->iltype], nhgetl(ih->ilid), nhgetl(ih->ilack),
- nhgets(ih->ilsrc), nhgets(ih->ildst));
- ipoput4(ipc->p->f, bp, 0, ttl, tos, ipc);
- }
- void
- ilreject(Fs *f, Ilhdr *inih)
- {
- Ilhdr *ih;
- Block *bp;
- bp = allocb(IL_IPSIZE+IL_HDRSIZE);
- bp->wp += IL_IPSIZE+IL_HDRSIZE;
- ih = (Ilhdr *)(bp->rp);
- ih->vihl = IP_VER4;
- /* Ip fields */
- ih->proto = IP_ILPROTO;
- hnputs(ih->illen, IL_HDRSIZE);
- ih->frag[0] = 0;
- ih->frag[1] = 0;
- hnputl(ih->dst, nhgetl(inih->src));
- hnputl(ih->src, nhgetl(inih->dst));
- hnputs(ih->ilsrc, nhgets(inih->ildst));
- hnputs(ih->ildst, nhgets(inih->ilsrc));
- hnputl(ih->ilid, nhgetl(inih->ilack));
- hnputl(ih->ilack, nhgetl(inih->ilid));
- ih->iltype = Ilclose;
- ih->ilspec = 0;
- ih->ilsum[0] = 0;
- ih->ilsum[1] = 0;
- if(ilcksum)
- hnputs(ih->ilsum, ptclcsum(bp, IL_IPSIZE, IL_HDRSIZE));
- ipoput4(f, bp, 0, MAXTTL, DFLTTOS, nil);
- }
- void
- ilsettimeout(Ilcb *ic)
- {
- ulong pt;
- pt = (ic->delay>>LogAGain)
- + ic->unackedbytes/(ic->rate>>LogAGain)
- + (ic->mdev>>(LogDGain-1))
- + AckDelay;
- if(pt > MaxTimeout)
- pt = MaxTimeout;
- ic->timeout = NOW + pt;
- }
- void
- ilbackoff(Ilcb *ic)
- {
- ulong pt;
- int i;
- pt = (ic->delay>>LogAGain)
- + ic->unackedbytes/(ic->rate>>LogAGain)
- + (ic->mdev>>(LogDGain-1))
- + AckDelay;
- for(i = 0; i < ic->rexmit; i++)
- pt = pt + (pt>>1);
- if(pt > MaxTimeout)
- pt = MaxTimeout;
- ic->timeout = NOW + pt;
- if(ic->fasttimeout)
- ic->timeout = NOW+Iltickms;
- ic->rexmit++;
- }
- // complain if two numbers not within an hour of each other
- #define Tfuture (1000*60*60)
- int
- later(ulong t1, ulong t2, char *x)
- {
- int dt;
- dt = t1 - t2;
- if(dt > 0) {
- if(x != nil && dt > Tfuture)
- print("%s: way future %d\n", x, dt);
- return 1;
- }
- if(dt < -Tfuture) {
- if(x != nil)
- print("%s: way past %d\n", x, -dt);
- return 1;
- }
- return 0;
- }
- void
- ilackproc(void *x)
- {
- Ilcb *ic;
- Conv **s, *p;
- Proto *il;
- il = x;
- loop:
- tsleep(&up->sleep, return0, 0, Iltickms);
- for(s = il->conv; s && *s; s++) {
- p = *s;
- ic = (Ilcb*)p->ptcl;
- switch(ic->state) {
- case Ilclosed:
- case Illistening:
- break;
- case Ilclosing:
- if(later(NOW, ic->timeout, "timeout0")) {
- if(ic->rexmit > MaxRexmit){
- ilhangup(p, nil);
- break;
- }
- ilsendctl(p, nil, Ilclose, ic->next, ic->recvd, 0);
- ilbackoff(ic);
- }
- break;
- case Ilsyncee:
- case Ilsyncer:
- if(later(NOW, ic->timeout, "timeout1")) {
- if(ic->rexmit > MaxRexmit){
- ilhangup(p, etime);
- break;
- }
- ilsendctl(p, nil, Ilsync, ic->start, ic->recvd, 0);
- ilbackoff(ic);
- }
- break;
- case Ilestablished:
- if(ic->recvd != ic->acksent)
- if(later(NOW, ic->acktime, "acktime"))
- ilsendctl(p, nil, Ilack, ic->next, ic->recvd, 0);
- if(later(NOW, ic->querytime, "querytime")){
- if(later(NOW, ic->lastrecv+DeathTime, "deathtime")){
- netlog(il->f, Logil, "il: hangup: deathtime\n");
- ilhangup(p, etime);
- break;
- }
- ilsendctl(p, nil, Ilquery, ic->next, ic->recvd, ilnextqt(ic));
- ic->querytime = NOW + QueryTime;
- }
- if(ic->unacked != nil)
- if(later(NOW, ic->timeout, "timeout2")) {
- if(ic->rexmit > MaxRexmit){
- netlog(il->f, Logil, "il: hangup: too many rexmits\n");
- ilhangup(p, etime);
- break;
- }
- ilsendctl(p, nil, Ilquery, ic->next, ic->recvd, ilnextqt(ic));
- ic->rxquery++;
- ilbackoff(ic);
- }
- break;
- }
- }
- goto loop;
- }
- void
- ilcbinit(Ilcb *ic)
- {
- ic->start = nrand(0x1000000);
- ic->next = ic->start+1;
- ic->recvd = 0;
- ic->window = Defaultwin;
- ic->unackedbytes = 0;
- ic->unacked = nil;
- ic->outoforder = nil;
- ic->rexmit = 0;
- ic->rxtot = 0;
- ic->rxquery = 0;
- ic->qtx = 1;
- ic->fasttimeout = 0;
- /* timers */
- ic->delay = DefRtt<<LogAGain;
- ic->mdev = DefRtt<<LogDGain;
- ic->rate = DefByteRate<<LogAGain;
- ic->querytime = NOW + QueryTime;
- ic->lastrecv = NOW; /* or we'll timeout right away */
- ilsettimeout(ic);
- }
- char*
- ilstart(Conv *c, int type, int fasttimeout)
- {
- Ilcb *ic;
- Ilpriv *ipriv;
- char kpname[KNAMELEN];
- ipriv = c->p->priv;
- if(ipriv->ackprocstarted == 0){
- qlock(&ipriv->apl);
- if(ipriv->ackprocstarted == 0){
- sprint(kpname, "#I%dilack", c->p->f->dev);
- kproc(kpname, ilackproc, c->p);
- ipriv->ackprocstarted = 1;
- }
- qunlock(&ipriv->apl);
- }
- ic = (Ilcb*)c->ptcl;
- ic->conv = c;
- if(ic->state != Ilclosed)
- return nil;
- ilcbinit(ic);
- if(fasttimeout){
- /* timeout if we can't connect quickly */
- ic->fasttimeout = 1;
- ic->timeout = NOW+Iltickms;
- ic->rexmit = MaxRexmit - 4;
- };
- switch(type) {
- default:
- netlog(c->p->f, Logil, "il: start: type %d\n", type);
- break;
- case IL_LISTEN:
- ic->state = Illistening;
- iphtadd(&ipriv->ht, c);
- break;
- case IL_CONNECT:
- ic->state = Ilsyncer;
- iphtadd(&ipriv->ht, c);
- ilsendctl(c, nil, Ilsync, ic->start, ic->recvd, 0);
- break;
- }
- return nil;
- }
- void
- ilfreeq(Ilcb *ic)
- {
- Block *bp, *next;
- qlock(&ic->ackq);
- for(bp = ic->unacked; bp; bp = next) {
- next = bp->list;
- freeblist(bp);
- }
- ic->unacked = nil;
- qunlock(&ic->ackq);
- qlock(&ic->outo);
- for(bp = ic->outoforder; bp; bp = next) {
- next = bp->list;
- freeblist(bp);
- }
- ic->outoforder = nil;
- qunlock(&ic->outo);
- }
- void
- iladvise(Proto *il, Block *bp, char *msg)
- {
- Ilhdr *h;
- Ilcb *ic;
- uchar source[IPaddrlen], dest[IPaddrlen];
- ushort psource;
- Conv *s, **p;
- h = (Ilhdr*)(bp->rp);
- v4tov6(dest, h->dst);
- v4tov6(source, h->src);
- psource = nhgets(h->ilsrc);
- /* Look for a connection, unfortunately the destination port is missing */
- qlock(il);
- for(p = il->conv; *p; p++) {
- s = *p;
- if(s->lport == psource)
- if(ipcmp(s->laddr, source) == 0)
- if(ipcmp(s->raddr, dest) == 0){
- qunlock(il);
- ic = (Ilcb*)s->ptcl;
- switch(ic->state){
- case Ilsyncer:
- ilhangup(s, msg);
- break;
- }
- freeblist(bp);
- return;
- }
- }
- qunlock(il);
- freeblist(bp);
- }
- int
- ilnextqt(Ilcb *ic)
- {
- int x;
- qlock(&ic->ackq);
- x = ic->qtx;
- if(++x > Nqt)
- x = 1;
- ic->qtx = x;
- ic->qt[x] = ic->next-1; /* highest xmitted packet */
- ic->qt[0] = ic->qt[x]; /* compatibility with old implementations */
- qunlock(&ic->ackq);
- return x;
- }
- /* calculate scale constants that converts fast ticks to ms (more or less) */
- static void
- inittimescale(void)
- {
- uvlong hz;
- fastticks(&hz);
- if(hz > 1000){
- scalediv = hz/1000;
- scalemul = 1;
- } else {
- scalediv = 1;
- scalemul = 1000/hz;
- }
- }
- void
- ilinit(Fs *f)
- {
- Proto *il;
- inittimescale();
- il = smalloc(sizeof(Proto));
- il->priv = smalloc(sizeof(Ilpriv));
- il->name = "il";
- il->connect = ilconnect;
- il->announce = ilannounce;
- il->state = ilstate;
- il->create = ilcreate;
- il->close = ilclose;
- il->rcv = iliput;
- il->ctl = nil;
- il->advise = iladvise;
- il->stats = ilxstats;
- il->inuse = ilinuse;
- il->gc = nil;
- il->ipproto = IP_ILPROTO;
- il->nc = scalednconv();
- il->ptclsize = sizeof(Ilcb);
- Fsproto(f, il);
- }
|