devether.c 10 KB

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