2 #include "../port/lib.h"
7 #include "../port/error.h"
9 #include "../port/netif.h"
12 extern int archether(unsigned ctlno, Ether *ether);
14 static Ether *etherxx[MaxEther];
17 etherattach(char* spec)
25 ctlrno = strtoul(spec, &p, 0);
26 if((ctlrno == 0 && p == spec) || *p != 0)
28 if(ctlrno < 0 || ctlrno >= MaxEther)
31 if(etherxx[ctlrno] == 0)
34 chan = devattach('l', spec);
40 if(etherxx[ctlrno]->attach)
41 etherxx[ctlrno]->attach(etherxx[ctlrno]);
47 etherwalk(Chan* chan, Chan* nchan, char** name, int nname)
49 return netifwalk(etherxx[chan->dev], chan, nchan, name, nname);
53 etherstat(Chan* chan, uchar* dp, int n)
55 return netifstat(etherxx[chan->dev], chan, dp, n);
59 etheropen(Chan* chan, int omode)
61 return netifopen(etherxx[chan->dev], chan, omode);
65 ethercreate(Chan*, char*, int, ulong)
72 etherclose(Chan* chan)
74 netifclose(etherxx[chan->dev], chan);
78 etherread(Chan* chan, void* buf, long n, vlong off)
83 ether = etherxx[chan->dev];
84 if((chan->qid.type & QTDIR) == 0 && ether->ifstat){
86 * With some controllers it is necessary to reach
87 * into the chip to extract statistics.
89 if(NETTYPE(chan->qid.path) == Nifstatqid)
90 return ether->ifstat(ether, buf, n, offset);
91 else if(NETTYPE(chan->qid.path) == Nstatqid)
92 ether->ifstat(ether, buf, 0, offset);
95 return netifread(ether, chan, buf, n, offset);
99 etherbread(Chan* chan, long n, ulong offset)
101 return netifbread(etherxx[chan->dev], chan, n, offset);
105 etherwstat(Chan* chan, uchar* dp, int n)
107 return netifwstat(etherxx[chan->dev], chan, dp, n);
111 etherrtrace(Netfile* f, Etherpkt* pkt, int len)
116 if(qwindow(f->in) <= 0)
125 memmove(bp->wp, pkt->d, n);
126 i = TK2MS(MACHP(0)->ticks);
138 etheriq(Ether* ether, Block* bp, int fromwire)
142 int len, multi, tome, fromme;
143 Netfile **ep, *f, **fp, *fx;
148 pkt = (Etherpkt*)bp->rp;
150 type = (pkt->type[0]<<8)|pkt->type[1];
152 ep = ðer->f[Ntypes];
154 multi = pkt->d[0] & 1;
155 /* check for valid multicast addresses */
156 if(multi && memcmp(pkt->d, ether->bcast, sizeof(pkt->d)) != 0 &&
158 if(!activemulti(ether, pkt->d, sizeof(pkt->d))){
167 tome = memcmp(pkt->d, ether->ea, sizeof(pkt->d)) == 0;
168 fromme = memcmp(pkt->s, ether->ea, sizeof(pkt->s)) == 0;
171 * Multiplex the packet to all the connections which want it.
172 * If the packet is not to be used subsequently (fromwire != 0),
173 * attempt to simply pass it into one of the connections, thereby
174 * saving a copy of the data (usual case hopefully).
176 for(fp = ether->f; fp < ep; fp++){
177 if((f = *fp) != nil && (f->type == type || f->type < 0) &&
178 (tome || multi || f->prom)){
179 /* Don't want to hear loopback or bridged packets */
180 if(f->bridge && (tome || !fromwire && !fromme))
183 if(fromwire && fx == 0)
185 else if(xbp = iallocb(len)){
186 memmove(xbp->wp, pkt, len);
188 if(qpass(f->in, xbp) < 0)
195 etherrtrace(f, pkt, len);
200 if(qpass(fx->in, bp) < 0)
212 etheroq(Ether* ether, Block* bp)
220 * Check if the packet has to be placed back onto the input queue,
221 * i.e. if it's a loopback or broadcast packet or the interface is
222 * in promiscuous mode.
223 * If it's a loopback packet indicate to etheriq that the data isn't
224 * needed and return, etheriq will pass-on or free the block.
225 * To enable bridging to work, only packets that were originated
226 * by this interface are fed back.
228 pkt = (Etherpkt*)bp->rp;
230 loopback = memcmp(pkt->d, ether->ea, sizeof(pkt->d)) == 0;
231 if(loopback || memcmp(pkt->d, ether->bcast, sizeof(pkt->d)) == 0 || ether->prom)
232 if(etheriq(ether, bp, loopback) == 0)
235 qbwrite(ether->oq, bp);
236 if(ether->transmit != nil)
237 ether->transmit(ether);
242 etherwrite(Chan* chan, void* buf, long n, vlong)
249 ether = etherxx[chan->dev];
250 if(NETTYPE(chan->qid.path) != Ndataqid) {
251 nn = netifwrite(ether, chan, buf, n);
254 cb = parsecmd(buf, n);
255 if(cb->f[0] && strcmp(cb->f[0], "nonblocking") == 0){
259 onoff = atoi(cb->f[1]);
260 qnoblock(ether->oq, onoff);
266 return ether->ctl(ether,buf,n);
271 if(n > ether->maxmtu)
273 if(n < ether->minmtu)
281 memmove(bp->rp, buf, n);
282 if(!ether->f[NETID(chan->qid.path)]->bridge)
283 memmove(bp->rp+Eaddrlen, ether->ea, Eaddrlen);
287 return etheroq(ether, bp);
291 etherbwrite(Chan* chan, Block* bp, ulong)
297 if(NETTYPE(chan->qid.path) != Ndataqid){
302 n = etherwrite(chan, bp->rp, n, 0);
307 ether = etherxx[chan->dev];
309 if(n > ether->maxmtu){
313 if(n < ether->minmtu){
318 return etheroq(ether, bp);
323 int (*reset)(Ether*);
327 addethercard(char* t, int (*r)(Ether*))
331 if(ncard == MaxEther)
332 panic("too many ether cards");
333 cards[ncard].type = t;
334 cards[ncard].reset = r;
343 char name[KNAMELEN], buf[128];
345 for(ether = 0, ctlrno = 0; ctlrno < MaxEther; ctlrno++){
347 ether = malloc(sizeof(Ether));
348 memset(ether, 0, sizeof(Ether));
349 ether->ctlrno = ctlrno;
351 ether->minmtu = ETHERMINTU;
352 ether->maxmtu = ETHERMAXTU;
354 if(archether(ctlrno, ether) <= 0)
357 if(isaconfig("ether", ctlrno, ether) == 0){
362 for(n = 0; cards[n].type; n++){
363 if(cistrcmp(cards[n].type, ether->type))
365 for(i = 0; i < ether->nopt; i++)
366 if(cistrncmp(ether->opt[i], "ea=", 3) == 0){
367 if(parseether(ether->ea,
368 ðer->opt[i][3]) == -1)
369 memset(ether->ea, 0, Eaddrlen);
370 } else if(cistrcmp(ether->opt[i],
371 "100BASE-TXFD") == 0)
373 if(cards[n].reset(ether))
375 snprint(name, sizeof(name), "ether%d", ctlrno);
377 if(ether->interrupt != nil && ether->irq >= 0)
378 intrenable(ether->irq, ether->interrupt,
381 i = snprint(buf, sizeof buf,
382 "#l%d: %s: %dMbps port %#lux irq %d",
383 ctlrno, ether->type, ether->mbps, ether->port,
386 i += snprint(buf+i, sizeof buf - i,
387 " addr %#lux", PADDR(ether->mem));
389 i += snprint(buf+i, sizeof buf - i,
390 " size %#luX", ether->size);
391 i += snprint(buf+i, sizeof buf - i,
392 ": %2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
393 ether->ea[0], ether->ea[1], ether->ea[2],
394 ether->ea[3], ether->ea[4], ether->ea[5]);
395 snprint(buf+i, sizeof buf - i, "\n");
396 iprint("%s", buf); /* it may be too early for print */
398 if(ether->mbps >= 1000)
399 netifinit(ether, name, Ntypes, 4*1024*1024);
400 else if(ether->mbps >= 100)
401 netifinit(ether, name, Ntypes, 1024*1024);
403 netifinit(ether, name, Ntypes, 65*1024);
405 ether->oq = qopen(ether->limit, Qmsg, 0, 0);
407 panic("etherreset %s", name);
408 ether->alen = Eaddrlen;
409 memmove(ether->addr, ether->ea, Eaddrlen);
410 memset(ether->bcast, 0xFF, Eaddrlen);
412 etherxx[ctlrno] = ether;
427 for(i = 0; i < MaxEther; i++){
431 if(ether->shutdown == nil) {
432 print("#l%d: no shutdown function\n", i);
435 (*ether->shutdown)(ether);
440 #define POLY 0xedb88320
442 /* really slow 32 bit crc for ethers */
444 ethercrc(uchar *p, int len)
450 for(i = 0; i < len; i++){
452 for(j = 0; j < 8; j++){
453 crc = (crc>>1) ^ (((crc^b) & 1) ? POLY : 0);
465 else if (qisclosed(oq))
466 print("outq closed ");
470 print("outq %d ", qlen(oq));
474 dumpnetif(Netif *netif)
476 print("netif %s ", netif->name);
477 print("limit %d mbps %d link %d ",
478 netif->limit, netif->mbps, netif->link);
479 print("inpkts %lld outpkts %lld errs %d\n",
480 netif->inpackets, netif->outpackets,
481 netif->crcs + netif->oerrs + netif->frames + netif->overflows +
482 netif->buffs + netif->soverflows);