devether.c 10 KB

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