devether.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. #include "u.h"
  2. #include "../port/lib.h"
  3. #include "mem.h"
  4. #include "dat.h"
  5. #include "fns.h"
  6. #include "io.h"
  7. #include "../port/error.h"
  8. #include "../port/netif.h"
  9. #include "etherif.h"
  10. static Ether *etherxx[MaxEther];
  11. Chan*
  12. etherattach(char* spec)
  13. {
  14. int ctlrno;
  15. char *p;
  16. Chan *chan;
  17. ctlrno = 0;
  18. if(spec && *spec){
  19. ctlrno = strtoul(spec, &p, 0);
  20. if((ctlrno == 0 && p == spec) || *p != 0)
  21. error(Ebadarg);
  22. if(ctlrno < 0 || ctlrno >= MaxEther)
  23. error(Ebadarg);
  24. }
  25. if(etherxx[ctlrno] == 0)
  26. error(Enodev);
  27. chan = devattach('l', spec);
  28. if(waserror()){
  29. chanfree(chan);
  30. nexterror();
  31. }
  32. chan->dev = ctlrno;
  33. if(etherxx[ctlrno]->attach)
  34. etherxx[ctlrno]->attach(etherxx[ctlrno]);
  35. poperror();
  36. return chan;
  37. }
  38. static Walkqid*
  39. etherwalk(Chan* chan, Chan* nchan, char** name, int nname)
  40. {
  41. return netifwalk(etherxx[chan->dev], chan, nchan, name, nname);
  42. }
  43. static int
  44. etherstat(Chan* chan, uchar* dp, int n)
  45. {
  46. return netifstat(etherxx[chan->dev], chan, dp, n);
  47. }
  48. static Chan*
  49. etheropen(Chan* chan, int omode)
  50. {
  51. return netifopen(etherxx[chan->dev], chan, omode);
  52. }
  53. static void
  54. ethercreate(Chan*, char*, int, ulong)
  55. {
  56. }
  57. static void
  58. etherclose(Chan* chan)
  59. {
  60. netifclose(etherxx[chan->dev], chan);
  61. }
  62. static long
  63. etherread(Chan* chan, void* buf, long n, vlong off)
  64. {
  65. Ether *ether;
  66. ulong offset = off;
  67. ether = etherxx[chan->dev];
  68. if((chan->qid.type & QTDIR) == 0 && ether->ifstat){
  69. /*
  70. * With some controllers it is necessary to reach
  71. * into the chip to extract statistics.
  72. */
  73. if(NETTYPE(chan->qid.path) == Nifstatqid)
  74. return ether->ifstat(ether, buf, n, offset);
  75. else if(NETTYPE(chan->qid.path) == Nstatqid)
  76. ether->ifstat(ether, buf, 0, offset);
  77. }
  78. return netifread(ether, chan, buf, n, offset);
  79. }
  80. static Block*
  81. etherbread(Chan* chan, long n, ulong offset)
  82. {
  83. return netifbread(etherxx[chan->dev], chan, n, offset);
  84. }
  85. static int
  86. etherwstat(Chan* chan, uchar* dp, int n)
  87. {
  88. return netifwstat(etherxx[chan->dev], chan, dp, n);
  89. }
  90. static void
  91. etherrtrace(Netfile* f, Etherpkt* pkt, int len)
  92. {
  93. int i, n;
  94. Block *bp;
  95. if(qwindow(f->in) <= 0)
  96. return;
  97. if(len > 58)
  98. n = 58;
  99. else
  100. n = len;
  101. bp = iallocb(64);
  102. if(bp == nil)
  103. return;
  104. memmove(bp->wp, pkt->d, n);
  105. i = TK2MS(MACHP(0)->ticks);
  106. bp->wp[58] = len>>8;
  107. bp->wp[59] = len;
  108. bp->wp[60] = i>>24;
  109. bp->wp[61] = i>>16;
  110. bp->wp[62] = i>>8;
  111. bp->wp[63] = i;
  112. bp->wp += 64;
  113. qpass(f->in, bp);
  114. }
  115. Block*
  116. etheriq(Ether* ether, Block* bp, int fromwire)
  117. {
  118. Etherpkt *pkt;
  119. ushort type;
  120. int len, multi, tome, fromme;
  121. Netfile **ep, *f, **fp, *fx;
  122. Block *xbp;
  123. ether->inpackets++;
  124. pkt = (Etherpkt*)bp->rp;
  125. len = BLEN(bp);
  126. type = (pkt->type[0]<<8)|pkt->type[1];
  127. fx = 0;
  128. ep = &ether->f[Ntypes];
  129. multi = pkt->d[0] & 1;
  130. /* check for valid multicast addresses */
  131. if(multi && memcmp(pkt->d, ether->bcast, sizeof(pkt->d)) != 0 &&
  132. ether->prom == 0){
  133. if(!activemulti(ether, pkt->d, sizeof(pkt->d))){
  134. if(fromwire){
  135. freeb(bp);
  136. bp = 0;
  137. }
  138. return bp;
  139. }
  140. }
  141. /* is it for me? */
  142. tome = memcmp(pkt->d, ether->ea, sizeof(pkt->d)) == 0;
  143. fromme = memcmp(pkt->s, ether->ea, sizeof(pkt->s)) == 0;
  144. /*
  145. * Multiplex the packet to all the connections which want it.
  146. * If the packet is not to be used subsequently (fromwire != 0),
  147. * attempt to simply pass it into one of the connections, thereby
  148. * saving a copy of the data (usual case hopefully).
  149. */
  150. for(fp = ether->f; fp < ep; fp++){
  151. if((f = *fp) != nil && (f->type == type || f->type < 0) &&
  152. (tome || multi || f->prom)){
  153. /* Don't want to hear bridged packets */
  154. if(f->bridge && !fromwire && !fromme)
  155. continue;
  156. if(!f->headersonly){
  157. if(fromwire && fx == 0)
  158. fx = f;
  159. else if(xbp = iallocb(len)){
  160. memmove(xbp->wp, pkt, len);
  161. xbp->wp += len;
  162. if(qpass(f->in, xbp) < 0)
  163. ether->soverflows++;
  164. }
  165. else
  166. ether->soverflows++;
  167. }
  168. else
  169. etherrtrace(f, pkt, len);
  170. }
  171. }
  172. if(fx){
  173. if(qpass(fx->in, bp) < 0)
  174. ether->soverflows++;
  175. return 0;
  176. }
  177. if(fromwire){
  178. freeb(bp);
  179. return 0;
  180. }
  181. return bp;
  182. }
  183. static int
  184. etheroq(Ether* ether, Block* bp)
  185. {
  186. int len, loopback, s;
  187. Etherpkt *pkt;
  188. ether->outpackets++;
  189. /*
  190. * Check if the packet has to be placed back onto the input queue,
  191. * i.e. if it's a loopback or broadcast packet or the interface is
  192. * in promiscuous mode.
  193. * If it's a loopback packet indicate to etheriq that the data isn't
  194. * needed and return, etheriq will pass-on or free the block.
  195. * To enable bridging to work, only packets that were originated
  196. * by this interface are fed back.
  197. */
  198. pkt = (Etherpkt*)bp->rp;
  199. len = BLEN(bp);
  200. loopback = memcmp(pkt->d, ether->ea, sizeof(pkt->d)) == 0;
  201. if(loopback || memcmp(pkt->d, ether->bcast, sizeof(pkt->d)) == 0 || ether->prom){
  202. s = splhi();
  203. etheriq(ether, bp, 0);
  204. splx(s);
  205. }
  206. if(!loopback){
  207. qbwrite(ether->oq, bp);
  208. if(ether->transmit != nil)
  209. ether->transmit(ether);
  210. } else
  211. freeb(bp);
  212. return len;
  213. }
  214. static long
  215. etherwrite(Chan* chan, void* buf, long n, vlong)
  216. {
  217. Ether *ether;
  218. Block *bp;
  219. int nn, onoff;
  220. Cmdbuf *cb;
  221. ether = etherxx[chan->dev];
  222. if(NETTYPE(chan->qid.path) != Ndataqid) {
  223. nn = netifwrite(ether, chan, buf, n);
  224. if(nn >= 0)
  225. return nn;
  226. cb = parsecmd(buf, n);
  227. if(cb->f[0] && strcmp(cb->f[0], "nonblocking") == 0){
  228. if(cb->nf <= 1)
  229. onoff = 1;
  230. else
  231. onoff = atoi(cb->f[1]);
  232. qnoblock(ether->oq, onoff);
  233. free(cb);
  234. return n;
  235. }
  236. free(cb);
  237. if(ether->ctl!=nil)
  238. return ether->ctl(ether,buf,n);
  239. error(Ebadctl);
  240. }
  241. if(n > ether->maxmtu)
  242. error(Etoobig);
  243. if(n < ether->minmtu)
  244. error(Etoosmall);
  245. bp = allocb(n);
  246. if(waserror()){
  247. freeb(bp);
  248. nexterror();
  249. }
  250. memmove(bp->rp, buf, n);
  251. memmove(bp->rp+Eaddrlen, ether->ea, Eaddrlen);
  252. poperror();
  253. bp->wp += n;
  254. return etheroq(ether, bp);
  255. }
  256. static long
  257. etherbwrite(Chan* chan, Block* bp, ulong)
  258. {
  259. Ether *ether;
  260. long n;
  261. n = BLEN(bp);
  262. if(NETTYPE(chan->qid.path) != Ndataqid){
  263. if(waserror()) {
  264. freeb(bp);
  265. nexterror();
  266. }
  267. n = etherwrite(chan, bp->rp, n, 0);
  268. poperror();
  269. freeb(bp);
  270. return n;
  271. }
  272. ether = etherxx[chan->dev];
  273. if(n > ether->maxmtu){
  274. freeb(bp);
  275. error(Etoobig);
  276. }
  277. if(n < ether->minmtu){
  278. freeb(bp);
  279. error(Etoosmall);
  280. }
  281. return etheroq(ether, bp);
  282. }
  283. static struct {
  284. char* type;
  285. int (*reset)(Ether*);
  286. } cards[MaxEther+1];
  287. void
  288. addethercard(char* t, int (*r)(Ether*))
  289. {
  290. static int ncard;
  291. if(ncard == MaxEther)
  292. panic("too many ether cards");
  293. cards[ncard].type = t;
  294. cards[ncard].reset = r;
  295. ncard++;
  296. }
  297. int
  298. parseether(uchar *to, char *from)
  299. {
  300. char nip[4];
  301. char *p;
  302. int i;
  303. p = from;
  304. for(i = 0; i < Eaddrlen; i++){
  305. if(*p == 0)
  306. return -1;
  307. nip[0] = *p++;
  308. if(*p == 0)
  309. return -1;
  310. nip[1] = *p++;
  311. nip[2] = 0;
  312. to[i] = strtoul(nip, 0, 16);
  313. if(*p == ':')
  314. p++;
  315. }
  316. return 0;
  317. }
  318. static void
  319. etherreset(void)
  320. {
  321. Ether *ether;
  322. int i, n, ctlrno;
  323. char name[KNAMELEN], buf[128];
  324. for(ether = 0, ctlrno = 0; ctlrno < MaxEther; ctlrno++){
  325. if(ether == 0)
  326. ether = malloc(sizeof(Ether));
  327. memset(ether, 0, sizeof(Ether));
  328. ether->ctlrno = ctlrno;
  329. ether->mbps = 10;
  330. ether->minmtu = ETHERMINTU;
  331. ether->maxmtu = ETHERMAXTU;
  332. if(archether(ctlrno, ether) <= 0)
  333. continue;
  334. if(isaconfig("ether", ctlrno, ether) == 0){
  335. free(ether);
  336. // return nil;
  337. continue;
  338. }
  339. for(n = 0; cards[n].type; n++){
  340. if(cistrcmp(cards[n].type, ether->type))
  341. continue;
  342. for(i = 0; i < ether->nopt; i++)
  343. if(cistrncmp(ether->opt[i], "ea=", 3) == 0){
  344. if(parseether(ether->ea,
  345. &ether->opt[i][3]) == -1)
  346. memset(ether->ea, 0, Eaddrlen);
  347. } else if(cistrcmp(ether->opt[i],
  348. "100BASE-TXFD") == 0)
  349. ether->mbps = 100;
  350. if(cards[n].reset(ether))
  351. break;
  352. snprint(name, sizeof(name), "ether%d", ctlrno);
  353. if(ether->interrupt != nil && ether->irq >= 0)
  354. intrenable(ether->irq, ether->interrupt,
  355. ether, 0, name);
  356. i = snprint(buf, sizeof buf,
  357. "#l%d: %s: %dMbps port %#lux irq %d",
  358. ctlrno, ether->type, ether->mbps, ether->port,
  359. ether->irq);
  360. if(ether->mem)
  361. i += snprint(buf+i, sizeof buf - i,
  362. " addr %#lux", PADDR(ether->mem));
  363. if(ether->size)
  364. i += snprint(buf+i, sizeof buf - i,
  365. " size %#luX", ether->size);
  366. i += snprint(buf+i, sizeof buf - i,
  367. ": %2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
  368. ether->ea[0], ether->ea[1], ether->ea[2],
  369. ether->ea[3], ether->ea[4], ether->ea[5]);
  370. snprint(buf+i, sizeof buf - i, "\n");
  371. iprint("%s", buf); /* it may be too early for print */
  372. if(ether->mbps >= 1000)
  373. netifinit(ether, name, Ntypes, 4*1024*1024);
  374. else if(ether->mbps >= 100)
  375. netifinit(ether, name, Ntypes, 1024*1024);
  376. else
  377. netifinit(ether, name, Ntypes, 65*1024);
  378. if(ether->oq == 0)
  379. ether->oq = qopen(ether->limit, Qmsg, 0, 0);
  380. if(ether->oq == 0)
  381. panic("etherreset %s", name);
  382. ether->alen = Eaddrlen;
  383. memmove(ether->addr, ether->ea, Eaddrlen);
  384. memset(ether->bcast, 0xFF, Eaddrlen);
  385. etherxx[ctlrno] = ether;
  386. ether = 0;
  387. break;
  388. }
  389. }
  390. if(ether)
  391. free(ether);
  392. }
  393. static void
  394. ethershutdown(void)
  395. {
  396. Ether *ether;
  397. int i;
  398. for(i = 0; i < MaxEther; i++){
  399. ether = etherxx[i];
  400. if(ether == nil)
  401. continue;
  402. if(ether->shutdown == nil) {
  403. print("#l%d: no shutdown function\n", i);
  404. continue;
  405. }
  406. (*ether->shutdown)(ether);
  407. }
  408. }
  409. #define POLY 0xedb88320
  410. /* really slow 32 bit crc for ethers */
  411. ulong
  412. ethercrc(uchar *p, int len)
  413. {
  414. int i, j;
  415. ulong crc, b;
  416. crc = 0xffffffff;
  417. for(i = 0; i < len; i++){
  418. b = *p++;
  419. for(j = 0; j < 8; j++){
  420. crc = (crc>>1) ^ (((crc^b) & 1) ? POLY : 0);
  421. b >>= 1;
  422. }
  423. }
  424. return crc;
  425. }
  426. void
  427. dumpoq(Queue *oq)
  428. {
  429. if (oq == nil)
  430. print("no outq! ");
  431. else if (qisclosed(oq))
  432. print("outq closed ");
  433. else if (qfull(oq))
  434. print("outq full ");
  435. else
  436. print("outq %d ", qlen(oq));
  437. }
  438. void
  439. dumpnetif(Netif *netif)
  440. {
  441. print("netif %s ", netif->name);
  442. print("limit %d mbps %d link %d ",
  443. netif->limit, netif->mbps, netif->link);
  444. print("inpkts %lld outpkts %lld errs %d\n",
  445. netif->inpackets, netif->outpackets,
  446. netif->crcs + netif->oerrs + netif->frames + netif->overflows +
  447. netif->buffs + netif->soverflows);
  448. }
  449. Dev etherdevtab = {
  450. 'l',
  451. "ether",
  452. etherreset,
  453. devinit,
  454. ethershutdown,
  455. etherattach,
  456. etherwalk,
  457. etherstat,
  458. etheropen,
  459. ethercreate,
  460. etherclose,
  461. etherread,
  462. etherbread,
  463. etherwrite,
  464. etherbwrite,
  465. devremove,
  466. etherwstat,
  467. };