#include "libgenl.h"
#include "utils.h"
#include "ip_common.h"
+#include "ila_common.h"
+#include "json_print.h"
static void usage(void)
{
- fprintf(stderr, "Usage: ip ila add loc_match LOCATOR_MATCH "
- "loc LOCATOR [ dev DEV ] "
- "[ csum-mode { adj-transport | neutral-map | "
- "neutral-map-auto | no-action } ] "
- "[ ident-type { luid | use-format } ]\n");
- fprintf(stderr, " ip ila del loc_match LOCATOR_MATCH "
- "[ loc LOCATOR ] [ dev DEV ]\n");
- fprintf(stderr, " ip ila list\n");
- fprintf(stderr, "\n");
+ fprintf(stderr,
+"Usage: ip ila add loc_match LOCATOR_MATCH loc LOCATOR [ dev DEV ] OPTIONS\n"
+" ip ila del loc_match LOCATOR_MATCH [ loc LOCATOR ] [ dev DEV ]\n"
+" ip ila list\n"
+"OPTIONS := [ csum-mode { adj-transport | neutral-map | neutral-map-auto | no-action } ]\n"
+" [ ident-type { luid | use-format } ]\n");
exit(-1);
}
#define ILA_RTA(g) ((struct rtattr *)(((char *)(g)) + \
NLMSG_ALIGN(sizeof(struct genlmsghdr))))
-#define ADDR_BUF_SIZE sizeof("xxxx:xxxx:xxxx:xxxx")
-
-static char *ila_csum_mode2name(__u8 csum_mode)
-{
- switch (csum_mode) {
- case ILA_CSUM_ADJUST_TRANSPORT:
- return "adj-transport";
- case ILA_CSUM_NEUTRAL_MAP:
- return "neutral-map";
- case ILA_CSUM_NO_ACTION:
- return "no-action";
- case ILA_CSUM_NEUTRAL_MAP_AUTO:
- return "neutral-map-auto";
- default:
- return "unknown";
- }
-}
-
-static int ila_csum_name2mode(char *name)
-{
- if (strcmp(name, "adj-transport") == 0)
- return ILA_CSUM_ADJUST_TRANSPORT;
- else if (strcmp(name, "neutral-map") == 0)
- return ILA_CSUM_NEUTRAL_MAP;
- else if (strcmp(name, "neutral-map-auto") == 0)
- return ILA_CSUM_NEUTRAL_MAP_AUTO;
- else if (strcmp(name, "no-action") == 0)
- return ILA_CSUM_NO_ACTION;
- else if (strcmp(name, "neutral-map-auto") == 0)
- return ILA_CSUM_NEUTRAL_MAP_AUTO;
- else
- return -1;
-}
-
-static char *ila_ident_type2name(__u8 ident_type)
-{
- switch (ident_type) {
- case ILA_ATYPE_IID:
- return "iid";
- case ILA_ATYPE_LUID:
- return "luid";
- case ILA_ATYPE_VIRT_V4:
- return "virt-v4";
- case ILA_ATYPE_VIRT_UNI_V6:
- return "virt-uni-v6";
- case ILA_ATYPE_VIRT_MULTI_V6:
- return "virt-multi-v6";
- case ILA_ATYPE_NONLOCAL_ADDR:
- return "nonlocal-addr";
- case ILA_ATYPE_USE_FORMAT:
- return "use-format";
- default:
- return "unknown";
- }
-}
-
-static int ila_ident_name2type(char *name)
-{
- if (!strcmp(name, "luid"))
- return ILA_ATYPE_LUID;
- else if (!strcmp(name, "use-format"))
- return ILA_ATYPE_USE_FORMAT;
-#if 0 /* No kernel support for configuring these yet */
- else if (!strcmp(name, "iid"))
- return ILA_ATYPE_IID;
- else if (!strcmp(name, "virt-v4"))
- return ILA_ATYPE_VIRT_V4;
- else if (!strcmp(name, "virt-uni-v6"))
- return ILA_ATYPE_VIRT_UNI_V6;
- else if (!strcmp(name, "virt-multi-v6"))
- return ILA_ATYPE_VIRT_MULTI_V6;
- else if (!strcmp(name, "nonlocal-addr"))
- return ILA_ATYPE_NONLOCAL_ADDR;
-#endif
- else
- return -1;
-}
-
-static int print_addr64(__u64 addr, char *buff, size_t len)
+static void print_addr64(__u64 addr, char *buff, size_t len)
{
__u16 *words = (__u16 *)&addr;
__u16 v;
sep = "";
ret = snprintf(&buff[written], len - written, "%x%s", v, sep);
- if (ret < 0)
- return ret;
-
written += ret;
}
-
- return written;
}
-static void print_ila_locid(FILE *fp, int attr, struct rtattr *tb[], int space)
+static void print_ila_locid(const char *tag, int attr, struct rtattr *tb[])
{
char abuf[256];
- size_t blen;
- int i;
- if (tb[attr]) {
- blen = print_addr64(rta_getattr_u64(tb[attr]),
- abuf, sizeof(abuf));
- fprintf(fp, "%s", abuf);
- } else {
- fprintf(fp, "-");
- blen = 1;
- }
+ if (tb[attr])
+ print_addr64(rta_getattr_u64(tb[attr]),
+ abuf, sizeof(abuf));
+ else
+ snprintf(abuf, sizeof(abuf), "-");
- for (i = 0; i < space - blen; i++)
- fprintf(fp, " ");
+ /* 20 = sizeof("xxxx:xxxx:xxxx:xxxx") */
+ print_string(PRINT_ANY, tag, "%-20s", abuf);
}
-static int print_ila_mapping(const struct sockaddr_nl *who,
- struct nlmsghdr *n, void *arg)
+static int print_ila_mapping(struct nlmsghdr *n, void *arg)
{
- FILE *fp = (FILE *)arg;
struct genlmsghdr *ghdr;
struct rtattr *tb[ILA_ATTR_MAX + 1];
int len = n->nlmsg_len;
ghdr = NLMSG_DATA(n);
parse_rtattr(tb, ILA_ATTR_MAX, (void *) ghdr + GENL_HDRLEN, len);
- print_ila_locid(fp, ILA_ATTR_LOCATOR_MATCH, tb, ADDR_BUF_SIZE);
- print_ila_locid(fp, ILA_ATTR_LOCATOR, tb, ADDR_BUF_SIZE);
+ open_json_object(NULL);
+ print_ila_locid("locator_match", ILA_ATTR_LOCATOR_MATCH, tb);
+ print_ila_locid("locator", ILA_ATTR_LOCATOR, tb);
- if (tb[ILA_ATTR_IFINDEX])
- fprintf(fp, "%-16s",
- ll_index_to_name(rta_getattr_u32(
- tb[ILA_ATTR_IFINDEX])));
- else
- fprintf(fp, "%-10s ", "-");
+ if (tb[ILA_ATTR_IFINDEX]) {
+ __u32 ifindex
+ = rta_getattr_u32(tb[ILA_ATTR_IFINDEX]);
- if (tb[ILA_ATTR_CSUM_MODE])
- fprintf(fp, "%s",
- ila_csum_mode2name(rta_getattr_u8(
- tb[ILA_ATTR_CSUM_MODE])));
- else
- fprintf(fp, "%-10s ", "-");
+ print_color_string(PRINT_ANY, COLOR_IFNAME,
+ "interface", "%-16s",
+ ll_index_to_name(ifindex));
+ } else {
+ print_string(PRINT_FP, NULL, "%-10s ", "-");
+ }
+
+ if (tb[ILA_ATTR_CSUM_MODE]) {
+ __u8 csum = rta_getattr_u8(tb[ILA_ATTR_CSUM_MODE]);
+
+ print_string(PRINT_ANY, "csum_mode", "%s",
+ ila_csum_mode2name(csum));
+ } else
+ print_string(PRINT_FP, NULL, "%-10s ", "-");
if (tb[ILA_ATTR_IDENT_TYPE])
- fprintf(fp, "%s",
+ print_string(PRINT_ANY, "ident_type", "%s",
ila_ident_type2name(rta_getattr_u8(
tb[ILA_ATTR_IDENT_TYPE])));
else
- fprintf(fp, "-");
+ print_string(PRINT_FP, NULL, "%s", "-");
- fprintf(fp, "\n");
+ print_nl();
+ close_json_object();
return 0;
}
exit(1);
}
+ new_json_obj(json);
if (rtnl_dump_filter(&genl_rth, print_ila_mapping, stdout) < 0) {
fprintf(stderr, "Dump terminated\n");
return 1;
}
+ delete_json_obj();
+ fflush(stdout);
return 0;
}