change how netlink socket is handled for batch mode.

(Logical change 1.166)
This commit is contained in:
osdl.net!shemminger 2005-03-14 19:34:12 +00:00
parent 008b73a881
commit 7901660a0d
5 changed files with 30 additions and 95 deletions

32
tc/tc.c
View File

@ -35,8 +35,7 @@ int show_details = 0;
int show_raw = 0;
int resolve_hosts = 0;
int use_iec = 0;
struct rtnl_handle g_rth;
int is_batch_mode = 0;
struct rtnl_handle rth;
static void *BODY; /* cached handle dlopen(NULL) */
static struct qdisc_util * qdisc_list;
@ -189,15 +188,6 @@ static int usage(void)
int main(int argc, char **argv)
{
char *basename;
basename = strrchr(argv[0], '/');
if (basename == NULL)
basename = argv[0];
else
basename++;
/* batch mode */
if (argc > 1 && matches(argv[1], "-batch") == 0) {
FILE *batch;
@ -223,12 +213,10 @@ int main(int argc, char **argv)
}
tc_core_init();
is_batch_mode=1;
if (rtnl_open(&g_rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
return 1;
}
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
exit(1);
}
while (fgets(line, sizeof(line)-1, batch)) {
if (line[strlen(line)-1]=='\n') {
@ -252,6 +240,7 @@ int main(int argc, char **argv)
if (largv[0][0]=='#')
continue;
if (matches(largv[0], "qdisc") == 0) {
ret += do_qdisc(largc-1, largv+1);
} else if (matches(largv[0], "class") == 0) {
@ -267,7 +256,9 @@ int main(int argc, char **argv)
}
}
fclose(batch);
rtnl_close(&g_rth);
rtnl_close(&rth);
return 0; /* end of batch, that's all */
}
@ -296,6 +287,10 @@ int main(int argc, char **argv)
}
tc_core_init();
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
exit(1);
}
if (argc > 1) {
if (matches(argv[1], "qdisc") == 0)
@ -312,6 +307,7 @@ int main(int argc, char **argv)
return -1;
}
rtnl_close(&rth);
usage();
return 0;
}

View File

@ -42,7 +42,6 @@ static void usage(void)
int tc_class_modify(int cmd, unsigned flags, int argc, char **argv)
{
struct rtnl_handle rth;
struct {
struct nlmsghdr n;
struct tcmsg t;
@ -127,14 +126,8 @@ int tc_class_modify(int cmd, unsigned flags, int argc, char **argv)
}
}
if (!is_batch_mode)
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
return 1;
}
if (d[0]) {
ll_init_map(&(is_batch_mode?g_rth:rth));
ll_init_map(&rth);
if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n", d);
@ -142,11 +135,9 @@ int tc_class_modify(int cmd, unsigned flags, int argc, char **argv)
}
}
if (rtnl_talk(&(is_batch_mode?g_rth:rth), &req.n, 0, 0, NULL, NULL, NULL) < 0)
if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
return 2;
if (!is_batch_mode)
rtnl_close(&rth);
return 0;
}
@ -280,13 +271,7 @@ int tc_class_list(int argc, char **argv)
argc--; argv++;
}
if (!is_batch_mode)
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
return 1;
}
ll_init_map(&(is_batch_mode?g_rth:rth));
ll_init_map(&rth);
if (d[0]) {
if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
@ -296,20 +281,18 @@ int tc_class_list(int argc, char **argv)
filter_ifindex = t.tcm_ifindex;
}
if (rtnl_dump_request(&(is_batch_mode?g_rth:rth), RTM_GETTCLASS, &t, sizeof(t)) < 0) {
if (rtnl_dump_request(&rth, RTM_GETTCLASS, &t, sizeof(t)) < 0) {
perror("Cannot send dump request");
rtnl_close(&rth);
return 1;
}
if (rtnl_dump_filter(&(is_batch_mode?g_rth:rth), print_class, stdout, NULL, NULL) < 0) {
if (rtnl_dump_filter(&rth, print_class, stdout, NULL, NULL) < 0) {
fprintf(stderr, "Dump terminated\n");
rtnl_close(&rth);
return 1;
}
if (!is_batch_mode)
rtnl_close(&rth);
return 0;
}

View File

@ -1,6 +1,7 @@
#define TCA_BUF_MAX (64*1024)
extern struct rtnl_handle rth;
extern int do_qdisc(int argc, char **argv);
extern int do_class(int argc, char **argv);
extern int do_filter(int argc, char **argv);

View File

@ -49,7 +49,6 @@ static void usage(void)
int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
{
struct rtnl_handle rth;
struct {
struct nlmsghdr n;
struct tcmsg t;
@ -154,14 +153,9 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
if (est.ewma_log)
addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est));
if (!is_batch_mode)
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
return 1;
}
if (d[0]) {
ll_init_map(&(is_batch_mode?g_rth:rth));
ll_init_map(&rth);
if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n", d);
@ -170,15 +164,11 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
}
}
if (rtnl_talk(&(is_batch_mode?g_rth:rth), &req.n, 0, 0, NULL, NULL, NULL) < 0) {
if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
fprintf(stderr, "We have an error talking to the kernel\n");
if (!is_batch_mode)
rtnl_close(&rth);
return 2;
}
if (!is_batch_mode)
rtnl_close(&rth);
return 0;
}
@ -333,13 +323,7 @@ int tc_filter_list(int argc, char **argv)
t.tcm_info = TC_H_MAKE(prio<<16, protocol);
if (!is_batch_mode)
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
return 1;
}
ll_init_map(&(is_batch_mode?g_rth:rth));
ll_init_map(&rth);
if (d[0]) {
if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
@ -350,20 +334,16 @@ int tc_filter_list(int argc, char **argv)
filter_ifindex = t.tcm_ifindex;
}
if (rtnl_dump_request(&(is_batch_mode?g_rth:rth), RTM_GETTFILTER, &t, sizeof(t)) < 0) {
if (rtnl_dump_request(&rth, RTM_GETTFILTER, &t, sizeof(t)) < 0) {
perror("Cannot send dump request");
if (!is_batch_mode)
rtnl_close(&rth);
return 1;
}
if (rtnl_dump_filter(&(is_batch_mode?g_rth:rth), print_filter, stdout, NULL, NULL) < 0) {
if (rtnl_dump_filter(&rth, print_filter, stdout, NULL, NULL) < 0) {
fprintf(stderr, "Dump terminated\n");
return 1;
}
if (!is_batch_mode)
rtnl_close(&rth);
return 0;
}

View File

@ -43,7 +43,6 @@ static int usage(void)
int tc_qdisc_modify(int cmd, unsigned flags, int argc, char **argv)
{
struct rtnl_handle rth;
struct qdisc_util *q = NULL;
struct tc_estimator est;
char d[16];
@ -139,17 +138,10 @@ int tc_qdisc_modify(int cmd, unsigned flags, int argc, char **argv)
}
}
if (!is_batch_mode)
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
rtnl_close(&rth);
return 1;
}
if (d[0]) {
int idx;
ll_init_map(&(is_batch_mode?g_rth:rth));
ll_init_map(&rth);
if ((idx = ll_name_to_index(d)) == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n", d);
@ -159,14 +151,9 @@ int tc_qdisc_modify(int cmd, unsigned flags, int argc, char **argv)
req.t.tcm_ifindex = idx;
}
if (rtnl_talk(&(is_batch_mode?g_rth:rth), &req.n, 0, 0, NULL, NULL, NULL) < 0) {
if (!is_batch_mode)
rtnl_close(&rth);
if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
return 2;
}
if (!is_batch_mode)
rtnl_close(&rth);
return 0;
}
@ -283,13 +270,7 @@ int tc_qdisc_list(int argc, char **argv)
argc--; argv++;
}
if (!is_batch_mode)
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
return 1;
}
ll_init_map(&(is_batch_mode?g_rth:rth));
ll_init_map(&rth);
if (d[0]) {
if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
@ -300,22 +281,16 @@ int tc_qdisc_list(int argc, char **argv)
filter_ifindex = t.tcm_ifindex;
}
if (rtnl_dump_request(&(is_batch_mode?g_rth:rth), RTM_GETQDISC, &t, sizeof(t)) < 0) {
if (rtnl_dump_request(&rth, RTM_GETQDISC, &t, sizeof(t)) < 0) {
perror("Cannot send dump request");
if (!is_batch_mode)
rtnl_close(&rth);
return 1;
}
if (rtnl_dump_filter(&(is_batch_mode?g_rth:rth), print_qdisc, stdout, NULL, NULL) < 0) {
if (rtnl_dump_filter(&rth, print_qdisc, stdout, NULL, NULL) < 0) {
fprintf(stderr, "Dump terminated\n");
if (!is_batch_mode)
rtnl_close(&rth);
return 1;
}
if (!is_batch_mode)
rtnl_close(&rth);
return 0;
}