aoe.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. // aoe.c: the ATA over Ethernet virtual EtherDrive (R) blade
  2. #define _GNU_SOURCE
  3. #include "config.h"
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdlib.h>
  7. #include <unistd.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <netinet/in.h>
  12. #include "dat.h"
  13. #include "fns.h"
  14. enum {
  15. Nmasks= 32,
  16. Nsrr= 256,
  17. Alen= 6,
  18. };
  19. uchar masks[Nmasks*Alen];
  20. int nmasks;
  21. uchar srr[Nsrr*Alen];
  22. int nsrr;
  23. char config[Nconfig];
  24. int nconfig = 0;
  25. int maxscnt = 2;
  26. char *ifname;
  27. int bufcnt = Bufcount;
  28. void
  29. aoead(int fd) // advertise the virtual blade
  30. {
  31. uchar buf[2000];
  32. Conf *p;
  33. int i;
  34. p = (Conf *)buf;
  35. memset(p, 0, sizeof *p);
  36. memset(p->h.dst, 0xff, 6);
  37. memmove(p->h.src, mac, 6);
  38. p->h.type = htons(0x88a2);
  39. p->h.flags = Resp;
  40. p->h.maj = htons(shelf);
  41. p->h.min = slot;
  42. p->h.cmd = Config;
  43. p->bufcnt = htons(bufcnt);
  44. p->scnt = maxscnt = (getmtu(sfd, ifname) - sizeof (Ata)) / 512;
  45. p->firmware = htons(FWV);
  46. p->vercmd = 0x10 | Qread;
  47. memcpy(p->data, config, nconfig);
  48. p->len = htons(nconfig);
  49. if (nmasks == 0)
  50. if (putpkt(fd, buf, sizeof *p - sizeof p->data + nconfig) == -1) {
  51. perror("putpkt aoe id");
  52. return;
  53. }
  54. for (i=0; i<nmasks; i++) {
  55. memcpy(p->h.dst, &masks[i*Alen], Alen);
  56. if (putpkt(fd, buf, sizeof *p - sizeof p->data + nconfig) == -1)
  57. perror("putpkt aoe id");
  58. }
  59. }
  60. int
  61. isbcast(uchar *ea)
  62. {
  63. uchar *b = (uchar *)"\377\377\377\377\377\377";
  64. return memcmp(ea, b, 6) == 0;
  65. }
  66. long long
  67. getlba(uchar *p)
  68. {
  69. vlong v;
  70. int i;
  71. v = 0;
  72. for (i = 0; i < 6; i++)
  73. v |= (vlong)(*p++) << i * 8;
  74. return v;
  75. }
  76. int
  77. aoeata(Ata *p, int pktlen) // do ATA reqeust
  78. {
  79. Ataregs r;
  80. int len = 60;
  81. int n;
  82. r.lba = getlba(p->lba);
  83. r.sectors = p->sectors;
  84. r.feature = p->err;
  85. r.cmd = p->cmd;
  86. if (r.cmd != 0xec)
  87. if (!rrok(p->h.src)) {
  88. p->h.flags |= Error;
  89. p->h.error = Res;
  90. return len;
  91. }
  92. if (atacmd(&r, (uchar *)(p+1), maxscnt*512, pktlen - sizeof(*p)) < 0) {
  93. p->h.flags |= Error;
  94. p->h.error = BadArg;
  95. return len;
  96. }
  97. if (!(p->aflag & Write))
  98. if ((n = p->sectors)) {
  99. n -= r.sectors;
  100. len = sizeof (Ata) + (n*512);
  101. }
  102. p->sectors = r.sectors;
  103. p->err = r.err;
  104. p->cmd = r.status;
  105. return len;
  106. }
  107. #define QCMD(x) ((x)->vercmd & 0xf)
  108. // yes, this makes unnecessary copies.
  109. int
  110. confcmd(Conf *p, int payload) // process conf request
  111. {
  112. int len;
  113. len = ntohs(p->len);
  114. if (QCMD(p) != Qread)
  115. if (len > Nconfig || len > payload)
  116. return 0; // if you can't play nice ...
  117. switch (QCMD(p)) {
  118. case Qtest:
  119. if (len != nconfig)
  120. return 0;
  121. // fall thru
  122. case Qprefix:
  123. if (len > nconfig)
  124. return 0;
  125. if (memcmp(config, p->data, len))
  126. return 0;
  127. // fall thru
  128. case Qread:
  129. break;
  130. case Qset:
  131. if (nconfig)
  132. if (nconfig != len || memcmp(config, p->data, len)) {
  133. p->h.flags |= Error;
  134. p->h.error = ConfigErr;
  135. break;
  136. }
  137. // fall thru
  138. case Qfset:
  139. nconfig = len;
  140. memcpy(config, p->data, nconfig);
  141. break;
  142. default:
  143. p->h.flags |= Error;
  144. p->h.error = BadArg;
  145. }
  146. memmove(p->data, config, nconfig);
  147. p->len = htons(nconfig);
  148. p->bufcnt = htons(bufcnt);
  149. p->scnt = maxscnt = (getmtu(sfd, ifname) - sizeof (Ata)) / 512;
  150. p->firmware = htons(FWV);
  151. p->vercmd = 0x10 | QCMD(p); // aoe v.1
  152. return nconfig + sizeof *p - sizeof p->data;
  153. }
  154. static int
  155. aoesrr(Aoesrr *sh, int len)
  156. {
  157. uchar *m, *e;
  158. int n;
  159. e = (uchar *) sh + len;
  160. m = (uchar *) sh + Nsrrhdr;
  161. switch (sh->rcmd) {
  162. default:
  163. e: sh->h.error = BadArg;
  164. sh->h.flags |= Error;
  165. break;
  166. case 1: // set
  167. if (!rrok(sh->h.src)) {
  168. sh->h.error = Res;
  169. sh->h.flags |= Error;
  170. break;
  171. }
  172. case 2: // force set
  173. n = sh->nmacs * 6;
  174. if (e < m + n)
  175. goto e;
  176. nsrr = sh->nmacs;
  177. memmove(srr, m, n);
  178. case 0: // read
  179. break;
  180. }
  181. sh->nmacs = nsrr;
  182. n = nsrr * 6;
  183. memmove(m, srr, n);
  184. return Nsrrhdr + n;
  185. }
  186. static int
  187. addmask(uchar *ea)
  188. {
  189. uchar *p, *e;
  190. p = masks;
  191. e = p + nmasks;
  192. for (; p<e; p += 6)
  193. if (!memcmp(p, ea, 6))
  194. return 2;
  195. if (nmasks >= Nmasks)
  196. return 0;
  197. memmove(p, ea, 6);
  198. nmasks++;
  199. return 1;
  200. }
  201. static void
  202. rmmask(uchar *ea)
  203. {
  204. uchar *p, *e;
  205. p = masks;
  206. e = p + nmasks;
  207. for (; p<e; p+=6)
  208. if (!memcmp(p, ea, 6)) {
  209. memmove(p, p+6, e-p-6);
  210. nmasks--;
  211. return;
  212. }
  213. }
  214. static int
  215. aoemask(Aoemask *mh, int len)
  216. {
  217. Mdir *md, *mdi, *mde;
  218. int i, n;
  219. n = 0;
  220. md = mdi = (Mdir *) ((uchar *)mh + Nmaskhdr);
  221. switch (mh->cmd) {
  222. case Medit:
  223. mde = md + mh->nmacs;
  224. for (; md<mde; md++) {
  225. switch (md->cmd) {
  226. case MDdel:
  227. rmmask(md->mac);
  228. continue;
  229. case MDadd:
  230. if (addmask(md->mac))
  231. continue;
  232. mh->merror = MEfull;
  233. mh->nmacs = md - mdi;
  234. goto e;
  235. case MDnop:
  236. continue;
  237. default:
  238. mh->merror = MEbaddir;
  239. mh->nmacs = md - mdi;
  240. goto e;
  241. }
  242. }
  243. // success. fall thru to return list
  244. case Mread:
  245. md = mdi;
  246. for (i=0; i<nmasks; i++) {
  247. md->res = md->cmd = 0;
  248. memmove(md->mac, &masks[i*6], 6);
  249. md++;
  250. }
  251. mh->merror = 0;
  252. mh->nmacs = nmasks;
  253. n = sizeof *md * nmasks;
  254. break;
  255. default:
  256. mh->h.flags |= Error;
  257. mh->h.error = BadArg;
  258. }
  259. e: return n + Nmaskhdr;
  260. }
  261. void
  262. doaoe(Aoehdr *p, int n)
  263. {
  264. int len;
  265. switch (p->cmd) {
  266. case ATAcmd:
  267. if (n < Natahdr)
  268. return;
  269. len = aoeata((Ata*)p, n);
  270. break;
  271. case Config:
  272. if (n < Ncfghdr)
  273. return;
  274. len = confcmd((Conf *)p, n);
  275. break;
  276. case Mask:
  277. if (n < Nmaskhdr)
  278. return;
  279. len = aoemask((Aoemask *)p, n);
  280. break;
  281. case Resrel:
  282. if (n < Nsrrhdr)
  283. return;
  284. len = aoesrr((Aoesrr *)p, n);
  285. break;
  286. default:
  287. p->error = BadCmd;
  288. p->flags |= Error;
  289. len = n;
  290. break;
  291. }
  292. if (len <= 0)
  293. return;
  294. memmove(p->dst, p->src, 6);
  295. memmove(p->src, mac, 6);
  296. p->maj = htons(shelf);
  297. p->min = slot;
  298. p->flags |= Resp;
  299. if (putpkt(sfd, (uchar *) p, len) == -1) {
  300. perror("write to network");
  301. exit(1);
  302. }
  303. }
  304. void
  305. aoe(void)
  306. {
  307. Aoehdr *p;
  308. uchar *buf;
  309. int n, sh;
  310. long pagesz;
  311. enum { bufsz = 1<<16, };
  312. if ((pagesz = sysconf(_SC_PAGESIZE)) < 0) {
  313. perror("sysconf");
  314. exit(1);
  315. }
  316. if ((buf = malloc(bufsz + pagesz)) == NULL) {
  317. perror("malloc");
  318. exit(1);
  319. }
  320. n = (size_t) buf + sizeof(Ata);
  321. if (n & (pagesz - 1))
  322. buf += pagesz - (n & (pagesz - 1));
  323. aoead(sfd);
  324. for (;;) {
  325. n = getpkt(sfd, buf, bufsz);
  326. if (n < 0) {
  327. perror("read network");
  328. exit(1);
  329. }
  330. if (n < sizeof(Aoehdr))
  331. continue;
  332. p = (Aoehdr *) buf;
  333. if (ntohs(p->type) != 0x88a2)
  334. continue;
  335. if (p->flags & Resp)
  336. continue;
  337. sh = ntohs(p->maj);
  338. if (sh != shelf && sh != (ushort)~0)
  339. continue;
  340. if (p->min != slot && p->min != (uchar)~0)
  341. continue;
  342. if (nmasks && !maskok(p->src))
  343. continue;
  344. doaoe(p, n);
  345. }
  346. }
  347. void
  348. usage(void)
  349. {
  350. fprintf(stderr, "usage: %s [-b bufcnt] [-o offset] [-l length] [-d ] [-s] [-r] [ -m mac[,mac...] ] shelf slot netif filename\n",
  351. progname);
  352. exit(1);
  353. }
  354. /* parseether from plan 9 */
  355. int
  356. parseether(uchar *to, char *from)
  357. {
  358. char nip[4];
  359. char *p;
  360. int i;
  361. p = from;
  362. for(i = 0; i < 6; i++){
  363. if(*p == 0)
  364. return -1;
  365. nip[0] = *p++;
  366. if(*p == 0)
  367. return -1;
  368. nip[1] = *p++;
  369. nip[2] = 0;
  370. to[i] = strtoul(nip, 0, 16);
  371. if(*p == ':')
  372. p++;
  373. }
  374. return 0;
  375. }
  376. void
  377. setmask(char *ml)
  378. {
  379. char *p;
  380. int n;
  381. for (; ml; ml=p) {
  382. p = strchr(ml, ',');
  383. if (p)
  384. *p++ = '\0';
  385. n = parseether(&masks[nmasks*Alen], ml);
  386. if (n < 0)
  387. fprintf(stderr, "ignoring mask %s, parseether failure\n", ml);
  388. else
  389. nmasks++;
  390. }
  391. }
  392. int
  393. maskok(uchar *ea)
  394. {
  395. int i, ok = 0;
  396. for (i=0; !ok && i<nmasks; i++)
  397. ok = memcmp(ea, &masks[i*Alen], Alen) == 0;
  398. return ok;
  399. }
  400. int
  401. rrok(uchar *ea)
  402. {
  403. int i, ok = 0;
  404. if (nsrr == 0)
  405. return 1;
  406. for (i=0; !ok && i<nsrr; i++)
  407. ok = memcmp(ea, &srr[i*Alen], Alen) == 0;
  408. return ok;
  409. }
  410. void
  411. setserial(int sh, int sl)
  412. {
  413. char h[32];
  414. h[0] = 0;
  415. gethostname(h, sizeof h);
  416. snprintf(serial, Nserial, "%d.%d:%.*s", sh, sl, (int) sizeof h, h);
  417. }
  418. int
  419. main(int argc, char **argv)
  420. {
  421. int ch, omode = 0, readonly = 0;
  422. vlong length = 0;
  423. char *end;
  424. bufcnt = Bufcount;
  425. offset = 0;
  426. setbuf(stdin, NULL);
  427. progname = *argv;
  428. while ((ch = getopt(argc, argv, "b:dsrm:o:l:")) != -1) {
  429. switch (ch) {
  430. case 'b':
  431. bufcnt = atoi(optarg);
  432. break;
  433. case 'd':
  434. #ifdef O_DIRECT
  435. omode |= O_DIRECT;
  436. #endif
  437. break;
  438. case 's':
  439. omode |= O_SYNC;
  440. break;
  441. case 'r':
  442. readonly = 1;
  443. break;
  444. case 'm':
  445. setmask(optarg);
  446. break;
  447. case 'o':
  448. offset = strtoll(optarg, &end, 0);
  449. if (end == optarg || offset < 0)
  450. usage();
  451. break;
  452. case 'l':
  453. length = strtoll(optarg, &end, 0);
  454. if (end == optarg || length < 1)
  455. usage();
  456. break;
  457. case '?':
  458. default:
  459. usage();
  460. }
  461. }
  462. argc -= optind;
  463. argv += optind;
  464. if (argc != 4 || bufcnt <= 0)
  465. usage();
  466. omode |= readonly ? O_RDONLY : O_RDWR;
  467. bfd = open(argv[3], omode);
  468. if (bfd == -1) {
  469. perror("open");
  470. exit(1);
  471. }
  472. shelf = atoi(argv[0]);
  473. slot = atoi(argv[1]);
  474. setserial(shelf, slot);
  475. size = getsize(bfd);
  476. size /= 512;
  477. if (size <= offset) {
  478. if (offset)
  479. fprintf(stderr,
  480. "Offset %lld too large for %lld-sector export\n",
  481. offset,
  482. size);
  483. else
  484. fputs("0-sector file size is too small\n", stderr);
  485. exit(1);
  486. }
  487. size -= offset;
  488. if (length) {
  489. if (length > size) {
  490. fprintf(stderr, "Length %llu too big - exceeds size of file!\n", offset);
  491. exit(1);
  492. }
  493. size = length;
  494. }
  495. ifname = argv[2];
  496. sfd = dial(ifname, bufcnt);
  497. if (sfd < 0)
  498. return 1;
  499. getea(sfd, ifname, mac);
  500. printf("pid %ld: e%d.%d, %lld sectors %s\n",
  501. (long) getpid(), shelf, slot, size,
  502. readonly ? "O_RDONLY" : "O_RDWR");
  503. fflush(stdout);
  504. atainit();
  505. aoe();
  506. return 0;
  507. }