devether.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  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->mtu)
  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->mtu){
  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. if(ether == nil)
  335. error(Enomem);
  336. memset(ether, 0, sizeof(Ether));
  337. ether->ctlrno = ctlrno;
  338. ether->tbdf = BUSUNKNOWN;
  339. ether->mbps = 10;
  340. ether->minmtu = ETHERMINTU;
  341. ether->maxmtu = ETHERMAXTU;
  342. ether->mtu = ETHERMAXTU;
  343. if(cardno < 0){
  344. if(isaconfig("ether", ctlrno, ether) == 0){
  345. free(ether);
  346. return nil;
  347. }
  348. for(cardno = 0; cards[cardno].type; cardno++){
  349. if(cistrcmp(cards[cardno].type, ether->type))
  350. continue;
  351. for(i = 0; i < ether->nopt; i++){
  352. if(strncmp(ether->opt[i], "ea=", 3))
  353. continue;
  354. if(parseether(ether->ea, &ether->opt[i][3]))
  355. memset(ether->ea, 0, Eaddrlen);
  356. }
  357. break;
  358. }
  359. }
  360. if(cardno >= MaxEther || cards[cardno].type == nil){
  361. free(ether);
  362. return nil;
  363. }
  364. if(cards[cardno].reset(ether) < 0){
  365. free(ether);
  366. return nil;
  367. }
  368. /*
  369. * IRQ2 doesn't really exist, it's used to gang the interrupt
  370. * controllers together. A device set to IRQ2 will appear on
  371. * the second interrupt controller as IRQ9.
  372. */
  373. if(ether->irq == 2)
  374. ether->irq = 9;
  375. snprint(name, sizeof(name), "ether%d", ctlrno);
  376. /*
  377. * If ether->irq is <0, it is a hack to indicate no interrupt
  378. * used by ethersink.
  379. */
  380. if(ether->irq >= 0)
  381. intrenable(ether->irq, ether->interrupt, ether, ether->tbdf, name);
  382. i = sprint(buf, "#l%d: %s: ", ctlrno, cards[cardno].type);
  383. if(ether->mbps >= 1000)
  384. i += sprint(buf+i, "%dGbps", ether->mbps/1000);
  385. else
  386. i += sprint(buf+i, "%dMbps", ether->mbps);
  387. i += sprint(buf+i, " port 0x%luX irq %d", ether->port, ether->irq);
  388. if(ether->mem)
  389. i += sprint(buf+i, " addr 0x%luX", ether->mem);
  390. if(ether->size)
  391. i += sprint(buf+i, " size 0x%luX", ether->size);
  392. i += sprint(buf+i, ": %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. sprint(buf+i, "\n");
  396. print(buf);
  397. /* compute log10(ether->mbps) into lg */
  398. for(lg = 0, mb = ether->mbps; mb >= 10; lg++)
  399. mb /= 10;
  400. if (lg > 0)
  401. lg--;
  402. if (lg > 14) /* 2^(14+17) = 2⁳ⁱ */
  403. lg = 14;
  404. /* allocate larger output queues for higher-speed interfaces */
  405. bsz = 1UL << (lg + 17); /* 2ⁱ⁷ = 128K, bsz = 2ⁿ × 128K */
  406. while (bsz > mainmem->maxsize / 8 && bsz > 128*1024)
  407. bsz /= 2;
  408. netifinit(ether, name, Ntypes, bsz);
  409. if(ether->oq == nil) {
  410. ether->oq = qopen(bsz, Qmsg, 0, 0);
  411. ether->limit = bsz;
  412. }
  413. if(ether->oq == nil)
  414. panic("etherreset %s: can't allocate output queue of %ld bytes",
  415. name, bsz);
  416. ether->alen = Eaddrlen;
  417. memmove(ether->addr, ether->ea, Eaddrlen);
  418. memset(ether->bcast, 0xFF, Eaddrlen);
  419. return ether;
  420. }
  421. static void
  422. etherreset(void)
  423. {
  424. Ether *ether;
  425. int cardno, ctlrno;
  426. for(ctlrno = 0; ctlrno < MaxEther; ctlrno++){
  427. if((ether = etherprobe(-1, ctlrno)) == nil)
  428. continue;
  429. etherxx[ctlrno] = ether;
  430. }
  431. if(getconf("*noetherprobe"))
  432. return;
  433. cardno = ctlrno = 0;
  434. while(cards[cardno].type != nil && ctlrno < MaxEther){
  435. if(etherxx[ctlrno] != nil){
  436. ctlrno++;
  437. continue;
  438. }
  439. if((ether = etherprobe(cardno, ctlrno)) == nil){
  440. cardno++;
  441. continue;
  442. }
  443. etherxx[ctlrno] = ether;
  444. ctlrno++;
  445. }
  446. }
  447. static void
  448. ethershutdown(void)
  449. {
  450. Ether *ether;
  451. int i;
  452. for(i = 0; i < MaxEther; i++){
  453. ether = etherxx[i];
  454. if(ether == nil)
  455. continue;
  456. if(ether->shutdown == nil) {
  457. print("#l%d: no shutdown function\n", i);
  458. continue;
  459. }
  460. (*ether->shutdown)(ether);
  461. }
  462. }
  463. #define POLY 0xedb88320
  464. /* really slow 32 bit crc for ethers */
  465. ulong
  466. ethercrc(uchar *p, int len)
  467. {
  468. int i, j;
  469. ulong crc, b;
  470. crc = 0xffffffff;
  471. for(i = 0; i < len; i++){
  472. b = *p++;
  473. for(j = 0; j < 8; j++){
  474. crc = (crc>>1) ^ (((crc^b) & 1) ? POLY : 0);
  475. b >>= 1;
  476. }
  477. }
  478. return crc;
  479. }
  480. Dev etherdevtab = {
  481. 'l',
  482. "ether",
  483. etherreset,
  484. devinit,
  485. ethershutdown,
  486. etherattach,
  487. etherwalk,
  488. etherstat,
  489. etheropen,
  490. ethercreate,
  491. etherclose,
  492. etherread,
  493. etherbread,
  494. etherwrite,
  495. etherbwrite,
  496. devremove,
  497. etherwstat,
  498. };