devether.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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. print("soverflow for f->in\n");
  164. ether->soverflows++;
  165. }
  166. }
  167. else {
  168. print("soverflow iallocb\n");
  169. ether->soverflows++;
  170. }
  171. }
  172. else
  173. etherrtrace(f, pkt, len);
  174. }
  175. }
  176. if(fx){
  177. if(qpass(fx->in, bp) < 0) {
  178. print("soverflow for fx->in\n");
  179. ether->soverflows++;
  180. }
  181. return 0;
  182. }
  183. if(fromwire){
  184. freeb(bp);
  185. return 0;
  186. }
  187. return bp;
  188. }
  189. static int
  190. etheroq(Ether* ether, Block* bp)
  191. {
  192. int len, loopback, s;
  193. Etherpkt *pkt;
  194. ether->outpackets++;
  195. /*
  196. * Check if the packet has to be placed back onto the input queue,
  197. * i.e. if it's a loopback or broadcast packet or the interface is
  198. * in promiscuous mode.
  199. * If it's a loopback packet indicate to etheriq that the data isn't
  200. * needed and return, etheriq will pass-on or free the block.
  201. * To enable bridging to work, only packets that were originated
  202. * by this interface are fed back.
  203. */
  204. pkt = (Etherpkt*)bp->rp;
  205. len = BLEN(bp);
  206. loopback = memcmp(pkt->d, ether->ea, sizeof(pkt->d)) == 0;
  207. if(loopback || memcmp(pkt->d, ether->bcast, sizeof(pkt->d)) == 0 || ether->prom){
  208. s = splhi();
  209. etheriq(ether, bp, 0);
  210. splx(s);
  211. }
  212. if(!loopback){
  213. if(qfull(ether->oq))
  214. print("etheroq: WARNING: ether->oq full!\n");
  215. qbwrite(ether->oq, bp);
  216. if(ether->transmit != nil)
  217. ether->transmit(ether);
  218. } else
  219. freeb(bp);
  220. return len;
  221. }
  222. static long
  223. etherwrite(Chan* chan, void* buf, long n, vlong)
  224. {
  225. Ether *ether;
  226. Block *bp;
  227. int nn, onoff;
  228. Cmdbuf *cb;
  229. ether = etherxx[chan->dev];
  230. if(NETTYPE(chan->qid.path) != Ndataqid) {
  231. nn = netifwrite(ether, chan, buf, n);
  232. if(nn >= 0)
  233. return nn;
  234. cb = parsecmd(buf, n);
  235. if(cb->f[0] && strcmp(cb->f[0], "nonblocking") == 0){
  236. if(cb->nf <= 1)
  237. onoff = 1;
  238. else
  239. onoff = atoi(cb->f[1]);
  240. qnoblock(ether->oq, onoff);
  241. free(cb);
  242. return n;
  243. }
  244. free(cb);
  245. if(ether->ctl!=nil)
  246. return ether->ctl(ether,buf,n);
  247. error(Ebadctl);
  248. }
  249. if(n > ether->maxmtu)
  250. error(Etoobig);
  251. if(n < ether->minmtu)
  252. error(Etoosmall);
  253. bp = allocb(n);
  254. if(waserror()){
  255. freeb(bp);
  256. nexterror();
  257. }
  258. memmove(bp->rp, buf, n);
  259. memmove(bp->rp+Eaddrlen, ether->ea, Eaddrlen);
  260. poperror();
  261. bp->wp += n;
  262. return etheroq(ether, bp);
  263. }
  264. static long
  265. etherbwrite(Chan* chan, Block* bp, ulong)
  266. {
  267. Ether *ether;
  268. long n;
  269. n = BLEN(bp);
  270. if(NETTYPE(chan->qid.path) != Ndataqid){
  271. if(waserror()) {
  272. freeb(bp);
  273. nexterror();
  274. }
  275. n = etherwrite(chan, bp->rp, n, 0);
  276. poperror();
  277. freeb(bp);
  278. return n;
  279. }
  280. ether = etherxx[chan->dev];
  281. if(n > ether->maxmtu){
  282. freeb(bp);
  283. error(Etoobig);
  284. }
  285. if(n < ether->minmtu){
  286. freeb(bp);
  287. error(Etoosmall);
  288. }
  289. return etheroq(ether, bp);
  290. }
  291. static struct {
  292. char* type;
  293. int (*reset)(Ether*);
  294. } cards[MaxEther+1];
  295. void
  296. addethercard(char* t, int (*r)(Ether*))
  297. {
  298. static int ncard;
  299. if(ncard == MaxEther)
  300. panic("too many ether cards");
  301. cards[ncard].type = t;
  302. cards[ncard].reset = r;
  303. ncard++;
  304. }
  305. int
  306. parseether(uchar *to, char *from)
  307. {
  308. char nip[4];
  309. char *p;
  310. int i;
  311. p = from;
  312. for(i = 0; i < Eaddrlen; i++){
  313. if(*p == 0)
  314. return -1;
  315. nip[0] = *p++;
  316. if(*p == 0)
  317. return -1;
  318. nip[1] = *p++;
  319. nip[2] = 0;
  320. to[i] = strtoul(nip, 0, 16);
  321. if(*p == ':')
  322. p++;
  323. }
  324. return 0;
  325. }
  326. static Ether*
  327. etherprobe(int cardno, int ctlrno)
  328. {
  329. int i, lg;
  330. ulong mb, bsz;
  331. Ether *ether;
  332. char buf[128], name[32];
  333. ether = malloc(sizeof(Ether));
  334. memset(ether, 0, sizeof(Ether));
  335. ether->ctlrno = ctlrno;
  336. ether->tbdf = BUSUNKNOWN;
  337. ether->mbps = 10;
  338. ether->minmtu = ETHERMINTU;
  339. ether->maxmtu = ETHERMAXTU;
  340. if(cardno < 0){
  341. if(isaconfig("ether", ctlrno, ether) == 0){
  342. free(ether);
  343. return nil;
  344. }
  345. for(cardno = 0; cards[cardno].type; cardno++){
  346. if(cistrcmp(cards[cardno].type, ether->type))
  347. continue;
  348. for(i = 0; i < ether->nopt; i++){
  349. if(strncmp(ether->opt[i], "ea=", 3))
  350. continue;
  351. if(parseether(ether->ea, &ether->opt[i][3]))
  352. memset(ether->ea, 0, Eaddrlen);
  353. }
  354. break;
  355. }
  356. }
  357. if(cardno >= MaxEther || cards[cardno].type == nil){
  358. free(ether);
  359. return nil;
  360. }
  361. if(cards[cardno].reset(ether) < 0){
  362. free(ether);
  363. return nil;
  364. }
  365. /*
  366. * IRQ2 doesn't really exist, it's used to gang the interrupt
  367. * controllers together. A device set to IRQ2 will appear on
  368. * the second interrupt controller as IRQ9.
  369. */
  370. if(ether->irq == 2)
  371. ether->irq = 9;
  372. snprint(name, sizeof(name), "ether%d", ctlrno);
  373. /*
  374. * If ether->irq is <0, it is a hack to indicate no interrupt
  375. * used by ethersink.
  376. */
  377. if(ether->irq >= 0)
  378. intrenable(ether->irq, ether->interrupt, ether, ether->tbdf, name);
  379. i = sprint(buf, "#l%d: %s: %dMbps port 0x%luX irq %d",
  380. ctlrno, cards[cardno].type, ether->mbps, ether->port, ether->irq);
  381. if(ether->mem)
  382. i += sprint(buf+i, " addr 0x%luX", ether->mem);
  383. if(ether->size)
  384. i += sprint(buf+i, " size 0x%luX", ether->size);
  385. i += sprint(buf+i, ": %2.2ux%2.2ux%2.2ux%2.2ux%2.2ux%2.2ux",
  386. ether->ea[0], ether->ea[1], ether->ea[2],
  387. ether->ea[3], ether->ea[4], ether->ea[5]);
  388. sprint(buf+i, "\n");
  389. print(buf);
  390. /* compute log10(ether->mbps) into lg */
  391. for(lg = 0, mb = ether->mbps; mb >= 10; lg++)
  392. mb /= 10;
  393. if (lg > 0)
  394. lg--;
  395. if (lg > 14) /* 2^(14+17) = 2⁳ⁱ */
  396. lg = 14;
  397. /* allocate larger output queues for higher-speed interfaces */
  398. bsz = 1UL << (lg + 17); /* 2ⁱ⁷ = 128K, bsz = 2ⁿ × 128K */
  399. while (bsz > mainmem->maxsize / 8 && bsz > 128*1024)
  400. bsz /= 2;
  401. netifinit(ether, name, Ntypes, bsz);
  402. if(ether->oq == nil) {
  403. ether->oq = qopen(bsz, Qmsg, 0, 0);
  404. ether->limit = bsz;
  405. }
  406. if(ether->oq == nil)
  407. panic("etherreset %s: can't allocate output queue of %ld bytes",
  408. name, bsz);
  409. ether->alen = Eaddrlen;
  410. memmove(ether->addr, ether->ea, Eaddrlen);
  411. memset(ether->bcast, 0xFF, Eaddrlen);
  412. return ether;
  413. }
  414. static void
  415. etherreset(void)
  416. {
  417. Ether *ether;
  418. int cardno, ctlrno;
  419. for(ctlrno = 0; ctlrno < MaxEther; ctlrno++){
  420. if((ether = etherprobe(-1, ctlrno)) == nil)
  421. continue;
  422. etherxx[ctlrno] = ether;
  423. }
  424. if(getconf("*noetherprobe"))
  425. return;
  426. cardno = ctlrno = 0;
  427. while(cards[cardno].type != nil && ctlrno < MaxEther){
  428. if(etherxx[ctlrno] != nil){
  429. ctlrno++;
  430. continue;
  431. }
  432. if((ether = etherprobe(cardno, ctlrno)) == nil){
  433. cardno++;
  434. continue;
  435. }
  436. etherxx[ctlrno] = ether;
  437. ctlrno++;
  438. }
  439. }
  440. static void
  441. ethershutdown(void)
  442. {
  443. Ether *ether;
  444. int i;
  445. for(i = 0; i < MaxEther; i++){
  446. ether = etherxx[i];
  447. if(ether == nil)
  448. continue;
  449. if(ether->shutdown == nil) {
  450. print("#l%d: no shutdown fuction\n", i);
  451. continue;
  452. }
  453. (*ether->shutdown)(ether);
  454. }
  455. }
  456. #define POLY 0xedb88320
  457. /* really slow 32 bit crc for ethers */
  458. ulong
  459. ethercrc(uchar *p, int len)
  460. {
  461. int i, j;
  462. ulong crc, b;
  463. crc = 0xffffffff;
  464. for(i = 0; i < len; i++){
  465. b = *p++;
  466. for(j = 0; j < 8; j++){
  467. crc = (crc>>1) ^ (((crc^b) & 1) ? POLY : 0);
  468. b >>= 1;
  469. }
  470. }
  471. return crc;
  472. }
  473. Dev etherdevtab = {
  474. 'l',
  475. "ether",
  476. etherreset,
  477. devinit,
  478. ethershutdown,
  479. etherattach,
  480. etherwalk,
  481. etherstat,
  482. etheropen,
  483. ethercreate,
  484. etherclose,
  485. etherread,
  486. etherbread,
  487. etherwrite,
  488. etherbwrite,
  489. devremove,
  490. etherwstat,
  491. };