use getopt to parse options, add one option to set

the wait time for link-up events
This commit is contained in:
Luigi Rizzo 2012-04-12 08:38:56 +00:00
parent 3ad4cbcf20
commit aaca8f41f5

View File

@ -48,7 +48,7 @@ int verbose = 0;
} while (0)
char *version = "$Id: bridge.c 10637 2012-02-24 16:36:25Z luigi $";
char *version = "$Id: bridge.c 10857 2012-04-06 12:18:22Z luigi $";
static int do_abort = 0;
@ -306,6 +306,14 @@ howmany(struct my_ring *me, int tx)
return tot;
}
static void
usage(void)
{
fprintf(stderr,
"usage: bridge [-v] [-i ifa] [-i ifb] [-b burst] [-w wait_time] [iface]\n");
exit(1);
}
/*
* bridge [-v] if1 [if2]
*
@ -317,36 +325,72 @@ int
main(int argc, char **argv)
{
struct pollfd pollfd[2];
int i;
u_int burst = 1024;
int i, ch;
u_int burst = 1024, wait_link = 4;
struct my_ring me[2];
char *ifa = NULL, *ifb = NULL;
fprintf(stderr, "%s %s built %s %s\n",
argv[0], version, __DATE__, __TIME__);
bzero(me, sizeof(me));
while (argc > 1 && !strcmp(argv[1], "-v")) {
verbose++;
argv++;
argc--;
while ( (ch = getopt(argc, argv, "b:i:vw:")) != -1) {
switch (ch) {
D("bad option %c %s", ch, optarg);
usage();
break;
case 'b': /* burst */
burst = atoi(optarg);
break;
case 'i': /* interface */
if (ifa == NULL)
ifa = optarg;
else if (ifb == NULL)
ifb = optarg;
else
D("%s ignored, already have 2 interfaces",
optarg);
break;
case 'v':
verbose++;
break;
case 'w':
wait_link = atoi(optarg);
break;
}
}
if (argc < 2 || argc > 4) {
D("Usage: %s IFNAME1 [IFNAME2 [BURST]]", argv[0]);
return (1);
if (argc > 1)
ifa = argv[1];
if (argc > 2)
ifb = argv[2];
if (argc > 3)
burst = atoi(argv[3]);
if (!ifb)
ifb = ifa;
if (!ifa) {
D("missing interface");
usage();
}
if (burst < 1 || burst > 8192) {
D("invalid burst %d, set to 1024", burst);
burst = 1024;
}
if (wait_link > 100) {
D("invalid wait_link %d, set to 4", wait_link);
wait_link = 4;
}
/* setup netmap interface #1. */
me[0].ifname = argv[1];
if (argc == 2 || !strcmp(argv[1], argv[2])) {
me[0].ifname = ifa;
me[1].ifname = ifb;
if (!strcmp(ifa, ifb)) {
D("same interface, endpoint 0 goes to host");
i = NETMAP_SW_RING;
me[1].ifname = argv[1];
} else {
/* two different interfaces. Take all rings on if1 */
i = 0; // all hw rings
me[1].ifname = argv[2];
}
if (netmap_open(me, i))
return (1);
@ -385,8 +429,6 @@ main(int argc, char **argv)
me[1].if_reqcap = me[1].if_curcap;
me[1].if_reqcap &= ~(IFCAP_HWCSUM | IFCAP_TSO | IFCAP_TOE);
do_ioctl(me+1, SIOCSIFCAP);
if (argc > 3)
burst = atoi(argv[3]); /* packets burst size. */
/* setup poll(2) variables. */
memset(pollfd, 0, sizeof(pollfd));
@ -395,8 +437,8 @@ main(int argc, char **argv)
pollfd[i].events = (POLLIN);
}
D("Wait 2 secs for link to come up...");
sleep(2);
D("Wait %d secs for link to come up...", wait_link);
sleep(wait_link);
D("Ready to go, %s 0x%x/%d <-> %s 0x%x/%d.",
me[0].ifname, me[0].queueid, me[0].nifp->ni_rx_queues,
me[1].ifname, me[1].queueid, me[1].nifp->ni_rx_queues);