| 1 |
/* Copyright Coraid, Inc. 2010. All Rights Reserved. */ |
| 2 |
|
| 3 |
#include <stdio.h> |
| 4 |
#include <sched.h> |
| 5 |
#include <stdlib.h> |
| 6 |
#include <unistd.h> |
| 7 |
#include <signal.h> |
| 8 |
#include <string.h> |
| 9 |
#include <time.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <sys/ioctl.h> |
| 12 |
#include <pthread.h> |
| 13 |
#include <poll.h> |
| 14 |
#include <errno.h> |
| 15 |
|
| 16 |
#include <netpacket/packet.h> |
| 17 |
#include <net/ethernet.h> |
| 18 |
#include <sys/types.h> |
| 19 |
#include <net/if.h> |
| 20 |
#include <netinet/in.h> |
| 21 |
#include <arpa/inet.h> |
| 22 |
#include <sys/socket.h> |
| 23 |
|
| 24 |
#define nelem(x) (sizeof(x)/sizeof((x)[0])) |
| 25 |
#define nil NULL |
| 26 |
#define vprintf(...) if (qflag) ; else fprintf(stderr, __VA_ARGS__) |
| 27 |
#define mintu(x) ((x) > 60 ? x : 60) |
| 28 |
|
| 29 |
typedef unsigned char uchar; |
| 30 |
typedef unsigned long ulong; |
| 31 |
|
| 32 |
typedef struct Aoe Aoe; |
| 33 |
typedef struct Qc Qc; |
| 34 |
typedef struct Ata Ata; |
| 35 |
typedef struct Lun Lun; |
| 36 |
typedef struct Eth Eth; |
| 37 |
typedef struct Targ Targ; |
| 38 |
typedef struct Mac Mac; |
| 39 |
|
| 40 |
struct Aoe { |
| 41 |
uchar dst[6]; |
| 42 |
uchar src[6]; |
| 43 |
uchar type[2]; |
| 44 |
uchar flags; |
| 45 |
uchar error; |
| 46 |
uchar major[2]; |
| 47 |
uchar minor; |
| 48 |
uchar cmd; |
| 49 |
uchar tag[4]; |
| 50 |
}; |
| 51 |
|
| 52 |
struct Qc { |
| 53 |
Aoe h; |
| 54 |
uchar bufcnt[2]; |
| 55 |
uchar firmware[2]; |
| 56 |
uchar scnt; |
| 57 |
uchar vercmd; |
| 58 |
uchar len[2]; |
| 59 |
// uchar data[1024]; |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
struct Ata { |
| 64 |
Aoe h; |
| 65 |
uchar aflag; |
| 66 |
uchar err; |
| 67 |
uchar scnt; |
| 68 |
uchar cmd; |
| 69 |
uchar lba[6]; |
| 70 |
uchar res[2]; |
| 71 |
}; |
| 72 |
|
| 73 |
struct Lun { |
| 74 |
Lun *next; |
| 75 |
int state; |
| 76 |
char ea[6]; |
| 77 |
int major; |
| 78 |
int minor; |
| 79 |
int nsect; |
| 80 |
int maxsect; |
| 81 |
int id; |
| 82 |
}; |
| 83 |
|
| 84 |
struct Eth { |
| 85 |
Lun *luns; |
| 86 |
int fd; |
| 87 |
char *name; |
| 88 |
char ea[6]; |
| 89 |
int mtu; |
| 90 |
int up; |
| 91 |
uchar pkt[16*1024]; |
| 92 |
}; |
| 93 |
|
| 94 |
struct Targ { |
| 95 |
Targ *next; |
| 96 |
int major; |
| 97 |
int minor; |
| 98 |
}; |
| 99 |
|
| 100 |
struct Mac { |
| 101 |
Mac *next; |
| 102 |
char ea[6]; |
| 103 |
}; |
| 104 |
|
| 105 |
enum { |
| 106 |
Neth= 16, |
| 107 |
Nstack= 16*1024, |
| 108 |
Nws= 5, |
| 109 |
|
| 110 |
Lnew= 0, |
| 111 |
Lprobe, |
| 112 |
|
| 113 |
Arsp= (1<<3), |
| 114 |
Aerr= (1<<2), |
| 115 |
|
| 116 |
Cata= 0, |
| 117 |
Cqc= 1, |
| 118 |
|
| 119 |
ETaoe= 0x88a2, |
| 120 |
}; |
| 121 |
|
| 122 |
int ethlist(char **, int); |
| 123 |
int ethopen(Eth *); |
| 124 |
void *jcheck(void *); |
| 125 |
int jinput(Eth *); |
| 126 |
Lun *findlun(Eth *e, Aoe *a); |
| 127 |
void jprobe(Eth *e, Lun *lun); |
| 128 |
void printlist(void); |
| 129 |
void printsancheck(void); |
| 130 |
ulong nhgetl(uchar *); |
| 131 |
ushort nhgets(uchar *); |
| 132 |
void hnputs(uchar *, ushort); |
| 133 |
void hnputl(uchar *, ulong); |
| 134 |
void *mallocz(int); |
| 135 |
void inserttarg(int, int); |
| 136 |
void insertmac(Mac **, char *); |
| 137 |
void sancheck(int, int); |
| 138 |
void ifsummary(void); |
| 139 |
int ifup(char *); |
| 140 |
void inserteth(char **, int, char *); |
| 141 |
char *getpciid(char *, int, char *); |
| 142 |
|
| 143 |
Eth eth[Neth]; |
| 144 |
int waitsecs = Nws; |
| 145 |
pthread_t threads[Neth]; |
| 146 |
Targ *targlist; |
| 147 |
|
| 148 |
int |
| 149 |
main(int argc, char **argv) |
| 150 |
{ |
| 151 |
int n, i; |
| 152 |
char *ethnames[Neth]; |
| 153 |
Eth *e; |
| 154 |
|
| 155 |
memset(ethnames, 0, sizeof ethnames); |
| 156 |
printf("Probing..."); |
| 157 |
fflush(0); |
| 158 |
n = ethlist(ethnames, nelem(ethnames)); |
| 159 |
for (i=0; i<n; i++) { |
| 160 |
e = ð[i]; |
| 161 |
e->name = ethnames[i]; |
| 162 |
e->up = ifup(ethnames[i]); |
| 163 |
if (pthread_create(&threads[i], 0, jcheck, e)) { |
| 164 |
fprintf(stderr, "pthread_create failed.\n"); |
| 165 |
break; |
| 166 |
} |
| 167 |
} |
| 168 |
n = i; |
| 169 |
for (i=0; i<n; i++) |
| 170 |
pthread_join(threads[i], 0); |
| 171 |
printf("done.\n"); |
| 172 |
printsancheck(); |
| 173 |
if (argc == 2 && strcmp(argv[1], "-v") == 0) |
| 174 |
printlist(); |
| 175 |
return 0; |
| 176 |
} |
| 177 |
|
| 178 |
char * |
| 179 |
cea(char *op, char *ea) |
| 180 |
{ |
| 181 |
int i; |
| 182 |
char *hex = "0123456789abcdef", *p; |
| 183 |
|
| 184 |
p = op; |
| 185 |
for (i=0; i<6; i++) { |
| 186 |
*p++ = hex[((ea[i] >> 4) & 0xf)]; |
| 187 |
*p++ = hex[ea[i] & 0xf]; |
| 188 |
} |
| 189 |
*p = 0; |
| 190 |
return op; |
| 191 |
} |
| 192 |
|
| 193 |
void |
| 194 |
printlist(void) |
| 195 |
{ |
| 196 |
Eth *e; |
| 197 |
Lun *lun; |
| 198 |
char mac[13]; |
| 199 |
|
| 200 |
for (e=eth; e->name; e++) { |
| 201 |
printf("%s:\n", e->name); |
| 202 |
for (lun=e->luns; lun; lun=lun->next) |
| 203 |
printf("e%d.%d %s %d\n", lun->major, lun->minor, cea(mac, lun->ea), lun->maxsect); |
| 204 |
printf("\n"); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
void |
| 209 |
timewait(int secs) /* arrange for a sig_alarm signal after `secs' seconds */ |
| 210 |
{ |
| 211 |
struct sigaction sa; |
| 212 |
void catch(int); |
| 213 |
|
| 214 |
memset(&sa, 0, sizeof sa); |
| 215 |
sa.sa_handler = catch; |
| 216 |
sa.sa_flags = SA_RESETHAND; |
| 217 |
sigaction(SIGALRM, &sa, NULL); |
| 218 |
alarm(secs); |
| 219 |
} |
| 220 |
|
| 221 |
int |
| 222 |
discover(Eth *e) |
| 223 |
{ |
| 224 |
Aoe *a; |
| 225 |
Qc *q; |
| 226 |
|
| 227 |
memset(e->pkt, 0, sizeof e->pkt); |
| 228 |
a = (Aoe *) e->pkt; |
| 229 |
memset(a->dst, 0xff, 6); |
| 230 |
memmove(a->src, e->ea, 6); |
| 231 |
hnputs(a->type, ETaoe); |
| 232 |
hnputl(a->tag, 1<<31); |
| 233 |
hnputs(a->major, 0xffff); |
| 234 |
a->minor = 0xff; |
| 235 |
a->cmd = Cqc; |
| 236 |
a->flags = 0x10; |
| 237 |
if (write(e->fd, a, mintu(sizeof *q)) <= 0) |
| 238 |
return -1; |
| 239 |
return 0; |
| 240 |
} |
| 241 |
|
| 242 |
void * |
| 243 |
jcheck(void *v) |
| 244 |
{ |
| 245 |
Eth *e = v; |
| 246 |
int n; |
| 247 |
time_t t, nt; |
| 248 |
struct pollfd pd; |
| 249 |
|
| 250 |
if (ethopen(e) < 0) |
| 251 |
return 0; |
| 252 |
if (discover(e) < 0) { |
| 253 |
fprintf(stderr, "skipping %s, discover failure: %s\n", e->name, strerror(errno)); |
| 254 |
return 0; |
| 255 |
} |
| 256 |
|
| 257 |
pd.fd = e->fd; |
| 258 |
pd.events = POLLIN; |
| 259 |
t = time(0); |
| 260 |
for (;;) { |
| 261 |
nt = time(0); |
| 262 |
if (nt-t >= waitsecs) |
| 263 |
return 0; |
| 264 |
if (poll(&pd, 1, waitsecs*1000) > 0) |
| 265 |
if ((n = read(e->fd, e->pkt, sizeof e->pkt)) > 0) |
| 266 |
if (jinput(e)) |
| 267 |
t = time(0); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
/* return 1 == useful, 0 == not useful */ |
| 272 |
int |
| 273 |
jinput(Eth *e) |
| 274 |
{ |
| 275 |
Aoe *a; |
| 276 |
Qc *q; |
| 277 |
Ata *aa; |
| 278 |
Lun *lun; |
| 279 |
ulong tag; |
| 280 |
int n; |
| 281 |
|
| 282 |
a = (Aoe *) e->pkt; |
| 283 |
if ((a->flags & (Arsp|Aerr)) != Arsp) |
| 284 |
return 0; |
| 285 |
if ((a->tag[0] & 0x80) == 0) |
| 286 |
return 0; |
| 287 |
tag = nhgetl(a->tag); |
| 288 |
switch (a->cmd) { |
| 289 |
case Cqc: |
| 290 |
q = (Qc *) a; |
| 291 |
lun = findlun(e, a); |
| 292 |
if (lun->state == Lnew) { |
| 293 |
lun->nsect = q->scnt; |
| 294 |
jprobe(e, lun); |
| 295 |
lun->state = Lprobe; |
| 296 |
break; |
| 297 |
} |
| 298 |
return 0; |
| 299 |
case Cata: |
| 300 |
aa = (Ata *) a; |
| 301 |
lun = findlun(e, a); |
| 302 |
if (lun == nil) |
| 303 |
return 0; |
| 304 |
if (lun->id != tag>>16) { |
| 305 |
printf("lun->id %d != tag %ld for %d.%d\n", lun->id, tag>>16, lun->major, lun->minor); |
| 306 |
return 0; |
| 307 |
} |
| 308 |
n = tag & 0xff; |
| 309 |
if (n > lun->maxsect) |
| 310 |
lun->maxsect = n; |
| 311 |
break; |
| 312 |
default: |
| 313 |
return 0; |
| 314 |
} |
| 315 |
return 1; |
| 316 |
} |
| 317 |
|
| 318 |
void |
| 319 |
hnputl(uchar *p, ulong n) |
| 320 |
{ |
| 321 |
*p++ = n >> 24; |
| 322 |
*p++ = n >> 16; |
| 323 |
*p++ = n >> 8; |
| 324 |
*p = n; |
| 325 |
} |
| 326 |
|
| 327 |
void |
| 328 |
hnputs(uchar *p, ushort s) |
| 329 |
{ |
| 330 |
*p++ = s >> 8; |
| 331 |
*p = s; |
| 332 |
} |
| 333 |
|
| 334 |
ushort |
| 335 |
nhgets(uchar *p) |
| 336 |
{ |
| 337 |
ushort s; |
| 338 |
|
| 339 |
s = *p++; |
| 340 |
s <<= 8; |
| 341 |
s += *p++ & 0xff; |
| 342 |
return s; |
| 343 |
} |
| 344 |
|
| 345 |
ulong |
| 346 |
nhgetl(uchar *p) |
| 347 |
{ |
| 348 |
ulong n; |
| 349 |
|
| 350 |
n = *p++; |
| 351 |
n <<= 8; |
| 352 |
n += *p++ & 0xff; |
| 353 |
n <<= 8; |
| 354 |
n += *p++ & 0xff; |
| 355 |
n <<= 8; |
| 356 |
n += *p++ & 0xff; |
| 357 |
return n; |
| 358 |
} |
| 359 |
|
| 360 |
void |
| 361 |
jprobe(Eth *e, Lun *lun) |
| 362 |
{ |
| 363 |
Aoe *a; |
| 364 |
Ata *aa; |
| 365 |
int n; |
| 366 |
|
| 367 |
memset(e->pkt, 0, sizeof e->pkt); |
| 368 |
a = (Aoe *) e->pkt; |
| 369 |
aa = (Ata *) a; |
| 370 |
memcpy(a->dst, lun->ea, 6); |
| 371 |
memcpy(a->src, e->ea, 6); |
| 372 |
hnputs(a->type, ETaoe); |
| 373 |
hnputs(a->major, lun->major); |
| 374 |
a->minor = lun->minor; |
| 375 |
a->flags = 0x10; |
| 376 |
hnputl(a->tag, lun->id<<16); |
| 377 |
aa->cmd = 0xec; |
| 378 |
aa->scnt = 1; |
| 379 |
n = e->mtu - sizeof *aa; |
| 380 |
for (n &= ~511; n > 0; n -= 512) { |
| 381 |
a->tag[3] = n/512; |
| 382 |
if (write(e->fd, a, sizeof *aa + n) <= 0) { |
| 383 |
printf("write failed\n"); |
| 384 |
} |
| 385 |
usleep(100); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
Lun * |
| 390 |
findlun(Eth *e, Aoe *a) |
| 391 |
{ |
| 392 |
Lun *p, **pp; |
| 393 |
int maj, n; |
| 394 |
static int id; |
| 395 |
|
| 396 |
maj = nhgets(a->major); |
| 397 |
pp = &e->luns; |
| 398 |
for (; (p=*pp); pp=&p->next) { |
| 399 |
if (maj < p->major) |
| 400 |
continue; |
| 401 |
if (maj > p->major) |
| 402 |
break; |
| 403 |
if (a->minor < p->minor) |
| 404 |
continue; |
| 405 |
if (a->minor > p->minor) |
| 406 |
break; |
| 407 |
n = memcmp(p->ea, a->src, 6); |
| 408 |
if (n < 0) |
| 409 |
continue; |
| 410 |
if (n > 0) |
| 411 |
break; |
| 412 |
return p; |
| 413 |
} |
| 414 |
if (a->cmd == Cata) |
| 415 |
return nil; |
| 416 |
p = mallocz(sizeof *p); |
| 417 |
p->major = maj; |
| 418 |
p->minor = a->minor; |
| 419 |
memmove(p->ea, a->src, 6); |
| 420 |
p->next = *pp; |
| 421 |
p->id = 0x8000 | id++; |
| 422 |
inserttarg(p->major, p->minor); |
| 423 |
return *pp = p; |
| 424 |
} |
| 425 |
|
| 426 |
|
| 427 |
void |
| 428 |
catch(int sig) |
| 429 |
{ |
| 430 |
} |
| 431 |
|
| 432 |
int |
| 433 |
getindx(int sfd, char *name) // return the index of device 'name' |
| 434 |
{ |
| 435 |
struct ifreq xx; |
| 436 |
int n; |
| 437 |
|
| 438 |
strcpy(xx.ifr_name, name); |
| 439 |
n = ioctl(sfd, SIOCGIFINDEX, &xx); |
| 440 |
if (n == -1) |
| 441 |
return -1; |
| 442 |
return xx.ifr_ifindex; |
| 443 |
} |
| 444 |
|
| 445 |
int |
| 446 |
getmtu(Eth *e) |
| 447 |
{ |
| 448 |
struct ifreq xx; |
| 449 |
int n; |
| 450 |
|
| 451 |
strcpy(xx.ifr_name, e->name); |
| 452 |
n = ioctl(e->fd, SIOCGIFMTU, &xx); |
| 453 |
if (n == -1) { |
| 454 |
perror("Can't get mtu"); |
| 455 |
return 1500; |
| 456 |
} |
| 457 |
return xx.ifr_mtu; |
| 458 |
} |
| 459 |
|
| 460 |
int |
| 461 |
ethopen(Eth *e) // get us a raw connection to an interface |
| 462 |
{ |
| 463 |
int n, sfd, rbsz, sbsz; |
| 464 |
struct sockaddr_ll sa; |
| 465 |
struct ifreq xx; |
| 466 |
|
| 467 |
rbsz = 64*1024*1024; |
| 468 |
sbsz = 64*1024*1024; |
| 469 |
memset(&sa, 0, sizeof sa); |
| 470 |
memset(&xx, 0, sizeof xx); |
| 471 |
sfd = socket(PF_PACKET, SOCK_RAW, htons(ETaoe)); |
| 472 |
if (sfd == -1) { |
| 473 |
perror("got bad socket"); |
| 474 |
return -1; |
| 475 |
} |
| 476 |
if (setsockopt(sfd, SOL_SOCKET, SO_RCVBUFFORCE, &rbsz, sizeof rbsz) < 0) |
| 477 |
fprintf(stderr, "Failed to set socket rcvbuf size\n"); |
| 478 |
if (setsockopt(sfd, SOL_SOCKET, SO_SNDBUFFORCE, &sbsz, sizeof sbsz) < 0) |
| 479 |
fprintf(stderr, "Failed to set socket sndbuf size\n"); |
| 480 |
n = getindx(sfd, e->name); |
| 481 |
sa.sll_family = AF_PACKET; |
| 482 |
sa.sll_protocol = htons(ETaoe); |
| 483 |
sa.sll_ifindex = n; |
| 484 |
n = bind(sfd, (struct sockaddr *)&sa, sizeof sa); |
| 485 |
if (n == -1) { |
| 486 |
perror("bind funky"); |
| 487 |
return -1; |
| 488 |
} |
| 489 |
strcpy(xx.ifr_name, e->name); |
| 490 |
n = ioctl(sfd, SIOCGIFHWADDR, &xx); |
| 491 |
if (n == -1) { |
| 492 |
perror("Can't get hw addr"); |
| 493 |
return -1; |
| 494 |
} |
| 495 |
memmove(e->ea, xx.ifr_hwaddr.sa_data, 6); |
| 496 |
e->fd = sfd; |
| 497 |
e->mtu = getmtu(e); |
| 498 |
return 0; |
| 499 |
} |
| 500 |
|
| 501 |
int |
| 502 |
ethlist(char **ifs, int nifs) |
| 503 |
{ |
| 504 |
int i, s, n; |
| 505 |
struct ifreq ifr; |
| 506 |
|
| 507 |
s = socket(AF_INET, SOCK_STREAM, 0); |
| 508 |
if (s < 0) |
| 509 |
return 0; |
| 510 |
n = 0; |
| 511 |
for (i=0; i<nifs; i++) { |
| 512 |
memset(&ifr, 0, sizeof ifr); |
| 513 |
ifr.ifr_ifindex = i; |
| 514 |
if (ioctl(s, SIOCGIFNAME, &ifr) < 0) |
| 515 |
continue; |
| 516 |
if (strncmp(ifr.ifr_name, "eth", 3)) |
| 517 |
continue; |
| 518 |
inserteth(ifs, nifs, ifr.ifr_name); |
| 519 |
n++; |
| 520 |
} |
| 521 |
close(s); |
| 522 |
return n; |
| 523 |
} |
| 524 |
|
| 525 |
void * |
| 526 |
mallocz(int sz) |
| 527 |
{ |
| 528 |
void *p; |
| 529 |
|
| 530 |
p = malloc(sz); |
| 531 |
memset(p, 0, sz); |
| 532 |
return p; |
| 533 |
} |
| 534 |
|
| 535 |
void |
| 536 |
inserttarg(int maj, int min) |
| 537 |
{ |
| 538 |
Targ *nt, *t, *p; |
| 539 |
|
| 540 |
nt = mallocz(sizeof *nt); |
| 541 |
nt->major = maj; |
| 542 |
nt->minor = min; |
| 543 |
if (targlist == NULL) { |
| 544 |
targlist = nt; |
| 545 |
return; |
| 546 |
} |
| 547 |
if (nt->major < targlist->major) { |
| 548 |
nt->next = targlist; |
| 549 |
targlist = nt; |
| 550 |
return; |
| 551 |
} else if (nt->major == targlist->major && nt->minor < targlist->minor) { |
| 552 |
nt->next = targlist; |
| 553 |
targlist = nt; |
| 554 |
return; |
| 555 |
} else if (nt->major == targlist->major && nt->minor == targlist->minor) |
| 556 |
return; |
| 557 |
for (p = targlist,t = targlist->next; t; p = t,t = t->next) { |
| 558 |
if (nt->major == t->major && nt->minor == t->minor) |
| 559 |
return; |
| 560 |
if (nt->major < t->major) { |
| 561 |
p->next = nt; |
| 562 |
nt->next = t; |
| 563 |
return; |
| 564 |
} else if (nt->major == t->major && nt->minor < t->minor) { |
| 565 |
p->next = nt; |
| 566 |
nt->next = t; |
| 567 |
return; |
| 568 |
} |
| 569 |
} |
| 570 |
p->next = nt; |
| 571 |
} |
| 572 |
|
| 573 |
void |
| 574 |
printsancheck() |
| 575 |
{ |
| 576 |
Targ *t; |
| 577 |
|
| 578 |
printf("==========================================\n"); |
| 579 |
printf("INTERFACE SUMMARY\n"); |
| 580 |
printf("==========================================\n"); |
| 581 |
printf("Name\tStatus\tMTU\tPCI ID\n"); |
| 582 |
ifsummary(); |
| 583 |
printf("==========================================\n"); |
| 584 |
printf("DEVICE SUMMARY\n"); |
| 585 |
printf("==========================================\n"); |
| 586 |
printf("Device\tMacs\tPayload\tLocal Interfaces\n"); |
| 587 |
for (t = targlist; t; t = t->next) { |
| 588 |
sancheck(t->major, t->minor); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
void |
| 593 |
ifsummary() |
| 594 |
{ |
| 595 |
Eth *e; |
| 596 |
char buf[32]; |
| 597 |
char *p; |
| 598 |
|
| 599 |
for (e=eth; e->name; e++) { |
| 600 |
p = getpciid(buf, sizeof buf, e->name); |
| 601 |
printf("%s\t%s\t%d\t%s\n", e->name, (e->up ? "UP" : "DN"), e->mtu, (p ? p : "")); |
| 602 |
} |
| 603 |
|
| 604 |
} |
| 605 |
|
| 606 |
void |
| 607 |
sancheck(int maj, int min) |
| 608 |
{ |
| 609 |
Eth *e; |
| 610 |
Lun *l; |
| 611 |
Mac *ml, *m; |
| 612 |
int a, found; |
| 613 |
int ps, nsect, nea, nloc; |
| 614 |
int mtu, mtuflag; |
| 615 |
char buf[128]; |
| 616 |
char mac[13]; |
| 617 |
|
| 618 |
nsect = mtu = 0; |
| 619 |
mtuflag = 0; |
| 620 |
nloc = nea = 0; |
| 621 |
a = 0; |
| 622 |
ps = 0; |
| 623 |
memset(buf, 0, sizeof buf); |
| 624 |
ml = NULL; |
| 625 |
|
| 626 |
for (e=eth; e->name; e++) { |
| 627 |
found = 0; |
| 628 |
for (l = e->luns; l; l = l->next) { |
| 629 |
if (!(l->major == maj && l->minor == min)) |
| 630 |
continue; |
| 631 |
found = 1; |
| 632 |
if (mtu == 0) |
| 633 |
mtu = e->mtu; |
| 634 |
else if (mtu != e->mtu) { |
| 635 |
mtuflag = 1; |
| 636 |
mtu = (e->mtu > mtu ? e->mtu : mtu); |
| 637 |
} |
| 638 |
insertmac(&ml, l->ea); |
| 639 |
if (ps == 0) |
| 640 |
ps = l->maxsect; |
| 641 |
else |
| 642 |
ps = (l->maxsect < ps ? l->maxsect : ps); |
| 643 |
nsect = l->nsect; |
| 644 |
} |
| 645 |
if (found) { |
| 646 |
snprintf(buf + a,(sizeof buf) - a, "%s%s", (a ? ",": ""), e->name); |
| 647 |
a += strlen(e->name) + (a ? 1 : 0); |
| 648 |
nloc++; |
| 649 |
} |
| 650 |
} |
| 651 |
for (m = ml; m; ml = m, nea++) { |
| 652 |
m = m->next; |
| 653 |
free(ml); |
| 654 |
} |
| 655 |
printf("e%d.%d\t%4d\t%d\t%s\n", maj, min, nea, nsect*512, buf); |
| 656 |
if (nea != nloc) |
| 657 |
printf(" Mismatched number of local vs remote interfaces\n"); |
| 658 |
for (e=eth; e->name; e++) { |
| 659 |
found = 0; |
| 660 |
for (l = e->luns; l; l = l->next) |
| 661 |
if (l->major == maj && l->minor == min) { |
| 662 |
found = 1; |
| 663 |
if (l->maxsect < (e->mtu-32)/512) |
| 664 |
printf(" The path %s->%s is only capable of %d byte payloads\n", e->name, cea(mac, l->ea), l->maxsect*512); |
| 665 |
} |
| 666 |
if (found) { |
| 667 |
if (e->mtu < nsect*512 + 36) |
| 668 |
printf(" %s: MTU (%d) not set optimally for device's capable payload\n",e->name, e->mtu); |
| 669 |
if (e->mtu < mtu) |
| 670 |
printf(" %s: MTU different from other interfaces (%d vs. %d)\n", e->name, e->mtu, mtu); |
| 671 |
} |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
void |
| 676 |
insertmac(Mac **ml, char *m) |
| 677 |
{ |
| 678 |
Mac *nm,*p, *pp; |
| 679 |
|
| 680 |
for (p = *ml, pp=NULL; p; pp = p, p = p->next) |
| 681 |
if (memcmp(p->ea, m, 6) == 0) |
| 682 |
return; |
| 683 |
nm = mallocz(sizeof *nm); |
| 684 |
memcpy(nm->ea, m, 6); |
| 685 |
if (pp) |
| 686 |
pp->next = nm; |
| 687 |
else |
| 688 |
*ml = nm; |
| 689 |
} |
| 690 |
|
| 691 |
int |
| 692 |
ifup(char *ethname) |
| 693 |
{ |
| 694 |
struct ifreq ifr; |
| 695 |
int r, s; |
| 696 |
|
| 697 |
memset(&ifr, 0, sizeof ifr); |
| 698 |
s = socket(AF_INET, SOCK_STREAM, 0); |
| 699 |
if (s < 0) |
| 700 |
return 0; |
| 701 |
strcpy(ifr.ifr_name, ethname); |
| 702 |
if (ioctl(s, SIOCGIFFLAGS, &ifr) < 0) { |
| 703 |
close(s); |
| 704 |
return 0; |
| 705 |
} |
| 706 |
r = ifr.ifr_flags & IFF_UP; |
| 707 |
close(s); |
| 708 |
return r; |
| 709 |
} |
| 710 |
|
| 711 |
void |
| 712 |
inserteth(char **ifs, int nifs, char *n) |
| 713 |
{ |
| 714 |
int i, j; |
| 715 |
char a[64], b[64]; |
| 716 |
|
| 717 |
memset(a, 0, sizeof a); |
| 718 |
memset(b, 0, sizeof b); |
| 719 |
for (i=0; i < nifs; i++) { |
| 720 |
if (ifs[i] == 0) { |
| 721 |
ifs[i] = strdup(n); |
| 722 |
break; |
| 723 |
} |
| 724 |
j = strcmp(n, ifs[i]); |
| 725 |
if (j < 0) { |
| 726 |
strcpy(a, n); |
| 727 |
for (; ifs[i]; i++) { |
| 728 |
strcpy(b, ifs[i]); |
| 729 |
free(ifs[i]); |
| 730 |
ifs[i] = strdup(a); |
| 731 |
strcpy(a, b); |
| 732 |
} |
| 733 |
ifs[i] = strdup(a); |
| 734 |
break; |
| 735 |
} else if (j == 0) |
| 736 |
break; |
| 737 |
else if (j > 0) |
| 738 |
continue; |
| 739 |
} |
| 740 |
} |
| 741 |
|
| 742 |
char * |
| 743 |
getpciid(char *b, int blen, char *n) |
| 744 |
{ |
| 745 |
FILE *fd; |
| 746 |
char dev[8]; |
| 747 |
char ven[8]; |
| 748 |
char path[128]; |
| 749 |
|
| 750 |
memset(dev, 0, sizeof dev); |
| 751 |
memset(ven, 0, sizeof ven); |
| 752 |
memset(path, 0, sizeof path); |
| 753 |
sprintf(path, "/sys/class/net/%s/device/vendor", n); |
| 754 |
if ((fd = fopen(path, "r")) == NULL) |
| 755 |
return NULL; |
| 756 |
fseek(fd, 2, SEEK_SET); |
| 757 |
if (fread(ven, 1, 4, fd) <= 0) { |
| 758 |
fclose(fd); |
| 759 |
return NULL; |
| 760 |
} |
| 761 |
fclose(fd); |
| 762 |
sprintf(path, "/sys/class/net/%s/device/device", n); |
| 763 |
if ((fd = fopen(path, "r")) == NULL) |
| 764 |
return NULL; |
| 765 |
fseek(fd, 2, SEEK_SET); |
| 766 |
if (fread(dev, 1, 4, fd) <= 0) { |
| 767 |
fclose(fd); |
| 768 |
return NULL; |
| 769 |
} |
| 770 |
fclose(fd); |
| 771 |
snprintf(b, blen, "%s:%s", ven, dev); |
| 772 |
return b; |
| 773 |
} |
| 774 |
|