swconfig: support receiving SWITCH_TYPE_LINK from kernel
[openwrt/openwrt.git] / package / network / config / swconfig / src / cli.c
index 5f9e532..9055414 100644 (file)
@@ -41,6 +41,7 @@ enum {
        CMD_LOAD,
        CMD_HELP,
        CMD_SHOW,
+       CMD_PORTMAP,
 };
 
 static void
@@ -83,9 +84,27 @@ list_attributes(struct switch_dev *dev)
        print_attrs(dev->port_ops);
 }
 
+static const char *
+speed_str(int speed)
+{
+       switch (speed) {
+       case 10:
+               return "10baseT";
+       case 100:
+               return "100baseT";
+       case 1000:
+               return "1000baseT";
+       default:
+               break;
+       }
+
+       return "unknown";
+}
+
 static void
 print_attr_val(const struct switch_attr *attr, const struct switch_val *val)
 {
+       struct switch_port_link *link;
        int i;
 
        switch (attr->type) {
@@ -103,6 +122,21 @@ print_attr_val(const struct switch_attr *attr, const struct switch_val *val)
                                 SWLIB_PORT_FLAG_TAGGED) ? "t" : "");
                }
                break;
+       case SWITCH_TYPE_LINK:
+               link = val->value.link;
+               if (link->link)
+                       printf("port:%d link:up speed:%s %s-duplex %s%s%s%s%s",
+                               val->port_vlan,
+                               speed_str(link->speed),
+                               link->duplex ? "full" : "half",
+                               link->tx_flow ? "txflow " : "",
+                               link->rx_flow ? "rxflow " : "",
+                               link->eee & SWLIB_LINK_FLAG_EEE_100BASET ? "eee100 " : "",
+                               link->eee & SWLIB_LINK_FLAG_EEE_1000BASET ? "eee1000 " : "",
+                               link->aneg ? "auto" : "");
+               else
+                       printf("port:%d link:down", val->port_vlan);
+               break;
        default:
                printf("?unknown-type?");
        }
@@ -177,7 +211,6 @@ swconfig_load_uci(struct switch_dev *dev, const char *name)
 {
        struct uci_context *ctx;
        struct uci_package *p = NULL;
-       struct uci_element *e;
        int ret = -1;
 
        ctx = uci_alloc_context();
@@ -205,7 +238,6 @@ int main(int argc, char **argv)
        struct switch_dev *dev;
        struct switch_attr *a;
        struct switch_val val;
-       int err;
        int i;
 
        int cmd = CMD_NONE;
@@ -214,6 +246,7 @@ int main(int argc, char **argv)
        int cvlan = -1;
        char *ckey = NULL;
        char *cvalue = NULL;
+       char *csegment = NULL;
 
        if((argc == 2) && !strcmp(argv[1], "list")) {
                swlib_list();
@@ -252,6 +285,10 @@ int main(int argc, char **argv)
                                print_usage();
                        cmd = CMD_LOAD;
                        ckey = argv[++i];
+               } else if (!strcmp(arg, "portmap")) {
+                       if (i + 1 < argc)
+                               csegment = argv[++i];
+                       cmd = CMD_PORTMAP;
                } else if (!strcmp(arg, "show")) {
                        cmd = CMD_SHOW;
                } else {
@@ -266,7 +303,7 @@ int main(int argc, char **argv)
 
        dev = swlib_connect(cdev);
        if (!dev) {
-               fprintf(stderr, "Failed to connect to the switch\n");
+               fprintf(stderr, "Failed to connect to the switch. Use the \"list\" command to see which switches are available.\n");
                return 1;
        }
 
@@ -283,6 +320,7 @@ int main(int argc, char **argv)
                if(!a)
                {
                        fprintf(stderr, "Unknown attribute \"%s\"\n", ckey);
+                       retval = -1;
                        goto out;
                }
        }
@@ -324,6 +362,9 @@ int main(int argc, char **argv)
        case CMD_HELP:
                list_attributes(dev);
                break;
+       case CMD_PORTMAP:
+               swlib_print_portmap(dev, csegment);
+               break;
        case CMD_SHOW:
                if (cport >= 0 || cvlan >= 0) {
                        if (cport >= 0)
@@ -342,5 +383,5 @@ int main(int argc, char **argv)
 
 out:
        swlib_free_all(dev);
-       return 0;
+       return retval;
 }