devether.c 10 KB

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