devether.c 10 KB

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