123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395 |
- #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;
- Rendez ilr;
- /* 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;
- Ilpriv *ipriv;
- il = x;
- ipriv = il->priv;
- loop:
- tsleep(&ipriv->ilr, 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);
- }
|