netns: add JSON support

Basic support for JSON output when showing network namespaces.

Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
Signed-off-by: David Ahern <dsahern@gmail.com>
This commit is contained in:
Stephen Hemminger 2018-03-08 08:39:10 -08:00 committed by David Ahern
parent 8c278ecad0
commit e93d922123
1 changed files with 24 additions and 8 deletions

View File

@ -22,6 +22,7 @@
#include "list.h" #include "list.h"
#include "ip_common.h" #include "ip_common.h"
#include "namespace.h" #include "namespace.h"
#include "json_print.h"
static int usage(void) static int usage(void)
{ {
@ -293,26 +294,30 @@ int print_nsid(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
return -1; return -1;
} }
open_json_object(NULL);
if (n->nlmsg_type == RTM_DELNSID) if (n->nlmsg_type == RTM_DELNSID)
fprintf(fp, "Deleted "); print_bool(PRINT_ANY, "deleted", "Deleted ", true);
nsid = rta_getattr_u32(tb[NETNSA_NSID]); nsid = rta_getattr_u32(tb[NETNSA_NSID]);
fprintf(fp, "nsid %u ", nsid); print_uint(PRINT_ANY, "nsid", "nsid %u ", nsid);
c = netns_map_get_by_nsid(nsid); c = netns_map_get_by_nsid(nsid);
if (c != NULL) { if (c != NULL) {
fprintf(fp, "(iproute2 netns name: %s)", c->name); print_string(PRINT_ANY, "name",
"(iproute2 netns name: %s)", c->name);
netns_map_del(c); netns_map_del(c);
} }
/* During 'ip monitor nsid', no chance to have new nsid in cache. */ /* During 'ip monitor nsid', no chance to have new nsid in cache. */
if (c == NULL && n->nlmsg_type == RTM_NEWNSID) if (c == NULL && n->nlmsg_type == RTM_NEWNSID)
if (netns_get_name(nsid, name) == 0) { if (netns_get_name(nsid, name) == 0) {
fprintf(fp, "(iproute2 netns name: %s)", name); print_string(PRINT_ANY, "name",
"(iproute2 netns name: %s)", name);
netns_map_add(nsid, name); netns_map_add(nsid, name);
} }
fprintf(fp, "\n"); print_string(PRINT_FP, NULL, "\n", NULL);
close_json_object();
fflush(fp); fflush(fp);
return 0; return 0;
} }
@ -329,10 +334,14 @@ static int netns_list_id(int argc, char **argv)
perror("Cannot send dump request"); perror("Cannot send dump request");
exit(1); exit(1);
} }
new_json_obj(json);
if (rtnl_dump_filter(&rth, print_nsid, stdout) < 0) { if (rtnl_dump_filter(&rth, print_nsid, stdout) < 0) {
delete_json_obj();
fprintf(stderr, "Dump terminated\n"); fprintf(stderr, "Dump terminated\n");
exit(1); exit(1);
} }
delete_json_obj();
return 0; return 0;
} }
@ -346,20 +355,27 @@ static int netns_list(int argc, char **argv)
if (!dir) if (!dir)
return 0; return 0;
new_json_obj(json);
while ((entry = readdir(dir)) != NULL) { while ((entry = readdir(dir)) != NULL) {
if (strcmp(entry->d_name, ".") == 0) if (strcmp(entry->d_name, ".") == 0)
continue; continue;
if (strcmp(entry->d_name, "..") == 0) if (strcmp(entry->d_name, "..") == 0)
continue; continue;
printf("%s", entry->d_name);
open_json_object(NULL);
print_string(PRINT_ANY, "name",
"%s", entry->d_name);
if (ipnetns_have_nsid()) { if (ipnetns_have_nsid()) {
id = get_netnsid_from_name(entry->d_name); id = get_netnsid_from_name(entry->d_name);
if (id >= 0) if (id >= 0)
printf(" (id: %d)", id); print_uint(PRINT_ANY, "id",
" (id: %d)", id);
} }
printf("\n"); print_string(PRINT_FP, NULL, "\n", NULL);
close_json_object();
} }
closedir(dir); closedir(dir);
delete_json_obj();
return 0; return 0;
} }