Commit 7901660a authored by osdl.net!shemminger's avatar osdl.net!shemminger

change how netlink socket is handled for batch mode.

(Logical change 1.166)
parent 008b73a8
...@@ -35,8 +35,7 @@ int show_details = 0; ...@@ -35,8 +35,7 @@ int show_details = 0;
int show_raw = 0; int show_raw = 0;
int resolve_hosts = 0; int resolve_hosts = 0;
int use_iec = 0; int use_iec = 0;
struct rtnl_handle g_rth; struct rtnl_handle rth;
int is_batch_mode = 0;
static void *BODY; /* cached handle dlopen(NULL) */ static void *BODY; /* cached handle dlopen(NULL) */
static struct qdisc_util * qdisc_list; static struct qdisc_util * qdisc_list;
...@@ -189,15 +188,6 @@ static int usage(void) ...@@ -189,15 +188,6 @@ static int usage(void)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
char *basename;
basename = strrchr(argv[0], '/');
if (basename == NULL)
basename = argv[0];
else
basename++;
/* batch mode */ /* batch mode */
if (argc > 1 && matches(argv[1], "-batch") == 0) { if (argc > 1 && matches(argv[1], "-batch") == 0) {
FILE *batch; FILE *batch;
...@@ -223,12 +213,10 @@ int main(int argc, char **argv) ...@@ -223,12 +213,10 @@ int main(int argc, char **argv)
} }
tc_core_init(); tc_core_init();
is_batch_mode=1; if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
if (rtnl_open(&g_rth, 0) < 0) { exit(1);
fprintf(stderr, "Cannot open rtnetlink\n"); }
return 1;
}
while (fgets(line, sizeof(line)-1, batch)) { while (fgets(line, sizeof(line)-1, batch)) {
if (line[strlen(line)-1]=='\n') { if (line[strlen(line)-1]=='\n') {
...@@ -252,6 +240,7 @@ int main(int argc, char **argv) ...@@ -252,6 +240,7 @@ int main(int argc, char **argv)
if (largv[0][0]=='#') if (largv[0][0]=='#')
continue; continue;
if (matches(largv[0], "qdisc") == 0) { if (matches(largv[0], "qdisc") == 0) {
ret += do_qdisc(largc-1, largv+1); ret += do_qdisc(largc-1, largv+1);
} else if (matches(largv[0], "class") == 0) { } else if (matches(largv[0], "class") == 0) {
...@@ -267,7 +256,9 @@ int main(int argc, char **argv) ...@@ -267,7 +256,9 @@ int main(int argc, char **argv)
} }
} }
fclose(batch); fclose(batch);
rtnl_close(&g_rth);
rtnl_close(&rth);
return 0; /* end of batch, that's all */ return 0; /* end of batch, that's all */
} }
...@@ -296,6 +287,10 @@ int main(int argc, char **argv) ...@@ -296,6 +287,10 @@ int main(int argc, char **argv)
} }
tc_core_init(); tc_core_init();
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
exit(1);
}
if (argc > 1) { if (argc > 1) {
if (matches(argv[1], "qdisc") == 0) if (matches(argv[1], "qdisc") == 0)
...@@ -312,6 +307,7 @@ int main(int argc, char **argv) ...@@ -312,6 +307,7 @@ int main(int argc, char **argv)
return -1; return -1;
} }
rtnl_close(&rth);
usage(); usage();
return 0; return 0;
} }
...@@ -42,7 +42,6 @@ static void usage(void) ...@@ -42,7 +42,6 @@ static void usage(void)
int tc_class_modify(int cmd, unsigned flags, int argc, char **argv) int tc_class_modify(int cmd, unsigned flags, int argc, char **argv)
{ {
struct rtnl_handle rth;
struct { struct {
struct nlmsghdr n; struct nlmsghdr n;
struct tcmsg t; struct tcmsg t;
...@@ -127,14 +126,8 @@ int tc_class_modify(int cmd, unsigned flags, int argc, char **argv) ...@@ -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]) { 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) { if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n", d); fprintf(stderr, "Cannot find device \"%s\"\n", d);
...@@ -142,11 +135,9 @@ int tc_class_modify(int cmd, unsigned flags, int argc, char **argv) ...@@ -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; return 2;
if (!is_batch_mode)
rtnl_close(&rth);
return 0; return 0;
} }
...@@ -280,13 +271,7 @@ int tc_class_list(int argc, char **argv) ...@@ -280,13 +271,7 @@ int tc_class_list(int argc, char **argv)
argc--; argv++; argc--; argv++;
} }
if (!is_batch_mode) ll_init_map(&rth);
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
return 1;
}
ll_init_map(&(is_batch_mode?g_rth:rth));
if (d[0]) { if (d[0]) {
if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) { if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
...@@ -296,20 +281,18 @@ int tc_class_list(int argc, char **argv) ...@@ -296,20 +281,18 @@ int tc_class_list(int argc, char **argv)
filter_ifindex = t.tcm_ifindex; 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"); perror("Cannot send dump request");
rtnl_close(&rth); rtnl_close(&rth);
return 1; 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"); fprintf(stderr, "Dump terminated\n");
rtnl_close(&rth); rtnl_close(&rth);
return 1; return 1;
} }
if (!is_batch_mode)
rtnl_close(&rth);
return 0; return 0;
} }
......
#define TCA_BUF_MAX (64*1024) #define TCA_BUF_MAX (64*1024)
extern struct rtnl_handle rth;
extern int do_qdisc(int argc, char **argv); extern int do_qdisc(int argc, char **argv);
extern int do_class(int argc, char **argv); extern int do_class(int argc, char **argv);
extern int do_filter(int argc, char **argv); extern int do_filter(int argc, char **argv);
......
...@@ -49,7 +49,6 @@ static void usage(void) ...@@ -49,7 +49,6 @@ static void usage(void)
int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv) int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
{ {
struct rtnl_handle rth;
struct { struct {
struct nlmsghdr n; struct nlmsghdr n;
struct tcmsg t; struct tcmsg t;
...@@ -154,14 +153,9 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv) ...@@ -154,14 +153,9 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv)
if (est.ewma_log) if (est.ewma_log)
addattr_l(&req.n, sizeof(req), TCA_RATE, &est, sizeof(est)); 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]) { 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) { if ((req.t.tcm_ifindex = ll_name_to_index(d)) == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n", d); fprintf(stderr, "Cannot find device \"%s\"\n", d);
...@@ -170,15 +164,11 @@ int tc_filter_modify(int cmd, unsigned flags, int argc, char **argv) ...@@ -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"); fprintf(stderr, "We have an error talking to the kernel\n");
if (!is_batch_mode)
rtnl_close(&rth);
return 2; return 2;
} }
if (!is_batch_mode)
rtnl_close(&rth);
return 0; return 0;
} }
...@@ -333,13 +323,7 @@ int tc_filter_list(int argc, char **argv) ...@@ -333,13 +323,7 @@ int tc_filter_list(int argc, char **argv)
t.tcm_info = TC_H_MAKE(prio<<16, protocol); t.tcm_info = TC_H_MAKE(prio<<16, protocol);
if (!is_batch_mode) ll_init_map(&rth);
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
return 1;
}
ll_init_map(&(is_batch_mode?g_rth:rth));
if (d[0]) { if (d[0]) {
if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) { if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
...@@ -350,20 +334,16 @@ int tc_filter_list(int argc, char **argv) ...@@ -350,20 +334,16 @@ int tc_filter_list(int argc, char **argv)
filter_ifindex = t.tcm_ifindex; 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"); perror("Cannot send dump request");
if (!is_batch_mode)
rtnl_close(&rth);
return 1; 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"); fprintf(stderr, "Dump terminated\n");
return 1; return 1;
} }
if (!is_batch_mode)
rtnl_close(&rth);
return 0; return 0;
} }
......
...@@ -43,7 +43,6 @@ static int usage(void) ...@@ -43,7 +43,6 @@ static int usage(void)
int tc_qdisc_modify(int cmd, unsigned flags, int argc, char **argv) int tc_qdisc_modify(int cmd, unsigned flags, int argc, char **argv)
{ {
struct rtnl_handle rth;
struct qdisc_util *q = NULL; struct qdisc_util *q = NULL;
struct tc_estimator est; struct tc_estimator est;
char d[16]; char d[16];
...@@ -139,17 +138,10 @@ int tc_qdisc_modify(int cmd, unsigned flags, int argc, char **argv) ...@@ -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]) { if (d[0]) {
int idx; int idx;
ll_init_map(&(is_batch_mode?g_rth:rth)); ll_init_map(&rth);
if ((idx = ll_name_to_index(d)) == 0) { if ((idx = ll_name_to_index(d)) == 0) {
fprintf(stderr, "Cannot find device \"%s\"\n", d); fprintf(stderr, "Cannot find device \"%s\"\n", d);
...@@ -159,14 +151,9 @@ int tc_qdisc_modify(int cmd, unsigned flags, int argc, char **argv) ...@@ -159,14 +151,9 @@ int tc_qdisc_modify(int cmd, unsigned flags, int argc, char **argv)
req.t.tcm_ifindex = idx; req.t.tcm_ifindex = idx;
} }
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)
if (!is_batch_mode)
rtnl_close(&rth);
return 2; return 2;
}
if (!is_batch_mode)
rtnl_close(&rth);
return 0; return 0;
} }
...@@ -283,13 +270,7 @@ int tc_qdisc_list(int argc, char **argv) ...@@ -283,13 +270,7 @@ int tc_qdisc_list(int argc, char **argv)
argc--; argv++; argc--; argv++;
} }
if (!is_batch_mode) ll_init_map(&rth);
if (rtnl_open(&rth, 0) < 0) {
fprintf(stderr, "Cannot open rtnetlink\n");
return 1;
}
ll_init_map(&(is_batch_mode?g_rth:rth));
if (d[0]) { if (d[0]) {
if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) { if ((t.tcm_ifindex = ll_name_to_index(d)) == 0) {
...@@ -300,22 +281,16 @@ int tc_qdisc_list(int argc, char **argv) ...@@ -300,22 +281,16 @@ int tc_qdisc_list(int argc, char **argv)
filter_ifindex = t.tcm_ifindex; 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"); perror("Cannot send dump request");
if (!is_batch_mode)
rtnl_close(&rth);
return 1; 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"); fprintf(stderr, "Dump terminated\n");
if (!is_batch_mode)
rtnl_close(&rth);
return 1; return 1;
} }
if (!is_batch_mode)
rtnl_close(&rth);
return 0; return 0;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment