]> git.proxmox.com Git - mirror_iproute2.git/blobdiff - ip/iproute.c
ip: iplink: Convert to use parse_on_off()
[mirror_iproute2.git] / ip / iproute.c
index 6b8142250349f71764a83612ec1d29ee88ef5b05..ebb5f160fc4437b9e0f15af3a307425b0f701483 100644 (file)
@@ -101,7 +101,7 @@ static void usage(void)
                "TIME := NUMBER[s|ms]\n"
                "BOOL := [1|0]\n"
                "FEATURES := ecn\n"
-               "ENCAPTYPE := [ mpls | ip | ip6 | seg6 | seg6local ]\n"
+               "ENCAPTYPE := [ mpls | ip | ip6 | seg6 | seg6local | rpl ]\n"
                "ENCAPHDR := [ MPLSLABEL | SEG6HDR ]\n"
                "SEG6HDR := [ mode SEGMODE ] segs ADDR1,ADDRi,ADDRn [hmac HMACKEYID] [cleanup]\n"
                "SEGMODE := [ encap | inline ]\n"
@@ -362,12 +362,18 @@ void print_rt_flags(FILE *fp, unsigned int flags)
                print_string(PRINT_ANY, NULL, "%s ", "pervasive");
        if (flags & RTNH_F_OFFLOAD)
                print_string(PRINT_ANY, NULL, "%s ", "offload");
+       if (flags & RTNH_F_TRAP)
+               print_string(PRINT_ANY, NULL, "%s ", "trap");
        if (flags & RTM_F_NOTIFY)
                print_string(PRINT_ANY, NULL, "%s ", "notify");
        if (flags & RTNH_F_LINKDOWN)
                print_string(PRINT_ANY, NULL, "%s ", "linkdown");
        if (flags & RTNH_F_UNRESOLVED)
                print_string(PRINT_ANY, NULL, "%s ", "unresolved");
+       if (flags & RTM_F_OFFLOAD)
+               print_string(PRINT_ANY, NULL, "%s ", "rt_offload");
+       if (flags & RTM_F_TRAP)
+               print_string(PRINT_ANY, NULL, "%s ", "rt_trap");
 
        close_json_array(PRINT_JSON, NULL);
 }
@@ -576,6 +582,7 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
        int i;
 
        open_json_array(PRINT_JSON, "metrics");
+       open_json_object(NULL);
 
        parse_rtattr(mxrta, RTAX_MAX, RTA_DATA(rta), RTA_PAYLOAD(rta));
 
@@ -609,7 +616,7 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
                        print_rtax_features(fp, val);
                        break;
                default:
-                       fprintf(fp, "%u ", val);
+                       print_uint(PRINT_ANY, mx_names[i], "%u ", val);
                        break;
 
                case RTAX_RTT:
@@ -637,6 +644,7 @@ static void print_rta_metrics(FILE *fp, const struct rtattr *rta)
                }
        }
 
+       close_json_object();
        close_json_array(PRINT_JSON, NULL);
 }
 
@@ -647,24 +655,26 @@ static void print_rta_multipath(FILE *fp, const struct rtmsg *r,
        int len = RTA_PAYLOAD(rta);
        int first = 1;
 
+       open_json_array(PRINT_JSON, "nexthops");
+
        while (len >= sizeof(*nh)) {
                struct rtattr *tb[RTA_MAX + 1];
 
                if (nh->rtnh_len > len)
                        break;
 
-               if (!is_json_context()) {
-                       if ((r->rtm_flags & RTM_F_CLONED) &&
-                           r->rtm_type == RTN_MULTICAST) {
-                               if (first) {
-                                       fprintf(fp, "Oifs: ");
-                                       first = 0;
-                               } else {
-                                       fprintf(fp, " ");
-                               }
-                       } else
-                               fprintf(fp, "%s\tnexthop ", _SL_);
-               }
+               open_json_object(NULL);
+
+               if ((r->rtm_flags & RTM_F_CLONED) &&
+                   r->rtm_type == RTN_MULTICAST) {
+                       if (first) {
+                               print_string(PRINT_FP, NULL, "Oifs: ", NULL);
+                               first = 0;
+                       } else {
+                               print_string(PRINT_FP, NULL, " ", NULL);
+                       }
+               } else
+                       print_string(PRINT_FP, NULL, "%s\tnexthop ", _SL_);
 
                if (nh->rtnh_len > sizeof(*nh)) {
                        parse_rtattr(tb, RTA_MAX, RTNH_DATA(nh),
@@ -687,22 +697,30 @@ static void print_rta_multipath(FILE *fp, const struct rtmsg *r,
 
                if ((r->rtm_flags & RTM_F_CLONED) &&
                    r->rtm_type == RTN_MULTICAST) {
-                       fprintf(fp, "%s", ll_index_to_name(nh->rtnh_ifindex));
+                       print_string(PRINT_ANY, "dev",
+                                    "%s", ll_index_to_name(nh->rtnh_ifindex));
+
                        if (nh->rtnh_hops != 1)
-                               fprintf(fp, "(ttl>%d)", nh->rtnh_hops);
-                       fprintf(fp, " ");
+                               print_int(PRINT_ANY, "ttl", "(ttl>%d)", nh->rtnh_hops);
+
+                       print_string(PRINT_FP, NULL, " ", NULL);
                } else {
-                       fprintf(fp, "dev %s ", ll_index_to_name(nh->rtnh_ifindex));
+                       print_string(PRINT_ANY, "dev",
+                                    "dev %s ", ll_index_to_name(nh->rtnh_ifindex));
+
                        if (r->rtm_family != AF_MPLS)
-                               fprintf(fp, "weight %d ",
-                                       nh->rtnh_hops+1);
+                               print_int(PRINT_ANY, "weight",
+                                         "weight %d ", nh->rtnh_hops + 1);
                }
 
                print_rt_flags(fp, nh->rtnh_flags);
 
                len -= NLMSG_ALIGN(nh->rtnh_len);
                nh = RTNH_NEXT(nh);
+
+               close_json_object();
        }
+       close_json_array(PRINT_JSON, NULL);
 }
 
 int print_route(struct nlmsghdr *n, void *arg)
@@ -917,9 +935,6 @@ int print_route(struct nlmsghdr *n, void *arg)
        if (tb[RTA_IIF] && filter.iifmask != -1)
                print_rta_if(fp, tb[RTA_IIF], "iif");
 
-       if (tb[RTA_MULTIPATH])
-               print_rta_multipath(fp, r, tb[RTA_MULTIPATH]);
-
        if (tb[RTA_PREF])
                print_rt_pref(fp, rta_getattr_u8(tb[RTA_PREF]));
 
@@ -935,6 +950,14 @@ int print_route(struct nlmsghdr *n, void *arg)
                                     propagate ? "enabled" : "disabled");
        }
 
+       if (tb[RTA_MULTIPATH])
+               print_rta_multipath(fp, r, tb[RTA_MULTIPATH]);
+
+       /* If you are adding new route RTA_XXXX then place it above
+        * the RTA_MULTIPATH else it will appear that the last nexthop
+        * in the ECMP has new attributes
+        */
+
        print_string(PRINT_FP, NULL, "\n", NULL);
        close_json_object();
        fflush(fp);
@@ -1613,6 +1636,30 @@ static int save_route_prep(void)
        return 0;
 }
 
+static int iproute_dump_filter(struct nlmsghdr *nlh, int reqlen)
+{
+       struct rtmsg *rtm = NLMSG_DATA(nlh);
+       int err;
+
+       rtm->rtm_protocol = filter.protocol;
+       if (filter.cloned)
+               rtm->rtm_flags |= RTM_F_CLONED;
+
+       if (filter.tb) {
+               err = addattr32(nlh, reqlen, RTA_TABLE, filter.tb);
+               if (err)
+                       return err;
+       }
+
+       if (filter.oif) {
+               err = addattr32(nlh, reqlen, RTA_OIF, filter.oif);
+               if (err)
+                       return err;
+       }
+
+       return 0;
+}
+
 static int iproute_flush(int family, rtnl_filter_t filter_fn)
 {
        time_t start = time(0);
@@ -1635,7 +1682,7 @@ static int iproute_flush(int family, rtnl_filter_t filter_fn)
        filter.flushe = sizeof(flushb);
 
        for (;;) {
-               if (rtnl_routedump_req(&rth, family, NULL) < 0) {
+               if (rtnl_routedump_req(&rth, family, iproute_dump_filter) < 0) {
                        perror("Cannot send dump request");
                        return -2;
                }
@@ -1675,30 +1722,6 @@ static int iproute_flush(int family, rtnl_filter_t filter_fn)
        }
 }
 
-static int iproute_dump_filter(struct nlmsghdr *nlh, int reqlen)
-{
-       struct rtmsg *rtm = NLMSG_DATA(nlh);
-       int err;
-
-       rtm->rtm_protocol = filter.protocol;
-       if (filter.cloned)
-               rtm->rtm_flags |= RTM_F_CLONED;
-
-       if (filter.tb) {
-               err = addattr32(nlh, reqlen, RTA_TABLE, filter.tb);
-               if (err)
-                       return err;
-       }
-
-       if (filter.oif) {
-               err = addattr32(nlh, reqlen, RTA_OIF, filter.oif);
-               if (err)
-                       return err;
-       }
-
-       return 0;
-}
-
 static int iproute_list_flush_or_save(int argc, char **argv, int action)
 {
        int dump_family = preferred_family;