devether.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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. 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 && ether->prom == 0){
  132. if(!activemulti(ether, pkt->d, sizeof(pkt->d))){
  133. if(fromwire){
  134. freeb(bp);
  135. bp = 0;
  136. }
  137. return bp;
  138. }
  139. }
  140. /* is it for me? */
  141. tome = memcmp(pkt->d, ether->ea, sizeof(pkt->d)) == 0;
  142. fromme = memcmp(pkt->s, ether->ea, sizeof(pkt->s)) == 0;
  143. /*
  144. * Multiplex the packet to all the connections which want it.
  145. * If the packet is not to be used subsequently (fromwire != 0),
  146. * attempt to simply pass it into one of the connections, thereby
  147. * saving a copy of the data (usual case hopefully).
  148. */
  149. for(fp = ether->f; fp < ep; fp++){
  150. if(f = *fp)
  151. if(f->type == type || f->type < 0)
  152. if(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 Ether*
  319. etherprobe(int cardno, int ctlrno)
  320. {
  321. int i, lg;
  322. ulong mb, bsz;
  323. Ether *ether;
  324. char buf[128], name[32];
  325. ether = malloc(sizeof(Ether));
  326. memset(ether, 0, sizeof(Ether));
  327. ether->ctlrno = ctlrno;
  328. ether->tbdf = BUSUNKNOWN;
  329. ether->mbps = 10;
  330. ether->minmtu = ETHERMINTU;
  331. ether->maxmtu = ETHERMAXTU;
  332. if(cardno < 0){
  333. if(isaconfig("ether", ctlrno, ether) == 0){
  334. free(ether);
  335. return nil;
  336. }
  337. for(cardno = 0; cards[cardno].type; cardno++){
  338. if(cistrcmp(cards[cardno].type, ether->type))
  339. continue;
  340. for(i = 0; i < ether->nopt; i++){
  341. if(strncmp(ether->opt[i], "ea=", 3))
  342. continue;
  343. if(parseether(ether->ea, &ether->opt[i][3]))
  344. memset(ether->ea, 0, Eaddrlen);
  345. }
  346. break;
  347. }
  348. }
  349. if(cardno >= MaxEther || cards[cardno].type == nil){
  350. free(ether);
  351. return nil;
  352. }
  353. if(cards[cardno].reset(ether) < 0){
  354. free(ether);
  355. return nil;
  356. }
  357. /*
  358. * IRQ2 doesn't really exist, it's used to gang the interrupt
  359. * controllers together. A device set to IRQ2 will appear on
  360. * the second interrupt controller as IRQ9.
  361. */
  362. if(ether->irq == 2)
  363. ether->irq = 9;
  364. snprint(name, sizeof(name), "ether%d", ctlrno);
  365. /*
  366. * If ether->irq is <0, it is a hack to indicate no interrupt
  367. * used by ethersink.
  368. */
  369. if(ether->irq >= 0)
  370. intrenable(ether->irq, ether->interrupt, ether, ether->tbdf, name);
  371. i = sprint(buf, "#l%d: %s: %dMbps port 0x%luX irq %d",
  372. ctlrno, cards[cardno].type, ether->mbps, ether->port, ether->irq);
  373. if(ether->mem)
  374. i += sprint(buf+i, " addr 0x%luX", ether->mem);
  375. if(ether->size)
  376. i += sprint(buf+i, " size 0x%luX", ether->size);
  377. i += sprint(buf+i, ": %2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
  378. ether->ea[0], ether->ea[1], ether->ea[2],
  379. ether->ea[3], ether->ea[4], ether->ea[5]);
  380. sprint(buf+i, "\n");
  381. print(buf);
  382. /* compute log10(ether->mbps) into lg */
  383. for(lg = 0, mb = ether->mbps; mb >= 10; lg++)
  384. mb /= 10;
  385. if (lg > 0)
  386. lg--;
  387. if (lg > 14) /* 2^(14+17) = 2⁳ⁱ */
  388. lg = 14;
  389. /* allocate larger output queues for higher-speed interfaces */
  390. bsz = 1UL << (lg + 17); /* 2ⁱ⁷ = 128K, bsz = 2ⁿ × 128K */
  391. while (bsz > mainmem->maxsize && bsz >= 128*1024)
  392. bsz /= 2;
  393. netifinit(ether, name, Ntypes, bsz);
  394. while (ether->oq == nil && bsz >= 128*1024) {
  395. bsz /= 2;
  396. ether->oq = qopen(bsz, Qmsg, 0, 0);
  397. ether->limit = bsz;
  398. }
  399. if(ether->oq == nil)
  400. panic("etherreset %s", name);
  401. ether->alen = Eaddrlen;
  402. memmove(ether->addr, ether->ea, Eaddrlen);
  403. memset(ether->bcast, 0xFF, Eaddrlen);
  404. return ether;
  405. }
  406. static void
  407. etherreset(void)
  408. {
  409. Ether *ether;
  410. int cardno, ctlrno;
  411. for(ctlrno = 0; ctlrno < MaxEther; ctlrno++){
  412. if((ether = etherprobe(-1, ctlrno)) == nil)
  413. continue;
  414. etherxx[ctlrno] = ether;
  415. }
  416. if(getconf("*noetherprobe"))
  417. return;
  418. cardno = ctlrno = 0;
  419. while(cards[cardno].type != nil && ctlrno < MaxEther){
  420. if(etherxx[ctlrno] != nil){
  421. ctlrno++;
  422. continue;
  423. }
  424. if((ether = etherprobe(cardno, ctlrno)) == nil){
  425. cardno++;
  426. continue;
  427. }
  428. etherxx[ctlrno] = ether;
  429. ctlrno++;
  430. }
  431. }
  432. static void
  433. ethershutdown(void)
  434. {
  435. Ether *ether;
  436. int i;
  437. for(i = 0; i < MaxEther; i++){
  438. ether = etherxx[i];
  439. if(ether == nil)
  440. continue;
  441. if(ether->shutdown == nil) {
  442. print("#l%d: no shutdown fuction\n", i);
  443. continue;
  444. }
  445. (*ether->shutdown)(ether);
  446. }
  447. }
  448. #define POLY 0xedb88320
  449. /* really slow 32 bit crc for ethers */
  450. ulong
  451. ethercrc(uchar *p, int len)
  452. {
  453. int i, j;
  454. ulong crc, b;
  455. crc = 0xffffffff;
  456. for(i = 0; i < len; i++){
  457. b = *p++;
  458. for(j = 0; j < 8; j++){
  459. crc = (crc>>1) ^ (((crc^b) & 1) ? POLY : 0);
  460. b >>= 1;
  461. }
  462. }
  463. return crc;
  464. }
  465. Dev etherdevtab = {
  466. 'l',
  467. "ether",
  468. etherreset,
  469. devinit,
  470. ethershutdown,
  471. etherattach,
  472. etherwalk,
  473. etherstat,
  474. etheropen,
  475. ethercreate,
  476. etherclose,
  477. etherread,
  478. etherbread,
  479. etherwrite,
  480. etherbwrite,
  481. devremove,
  482. etherwstat,
  483. };