/* reset hw switch */ int switch_reset(struct swconfig_context *sw_ctx){ int ret = 0; struct switch_attr *a; if(NULL == sw_ctx->dev) return -1; a = swlib_lookup_attr(sw_ctx->dev, SWLIB_ATTR_GROUP_GLOBAL, "reset"); if(!a) /* attribute unknown */ return -1; ret = swlib_set_attr_string(sw_ctx->dev, a, -1, NULL); return ret; }
int port_disable(int id, struct swconfig_context *sw_ctx){ int ret = 0; struct switch_attr *a; if(NULL == sw_ctx->dev) return -1; a = swlib_lookup_attr(sw_ctx->dev, SWLIB_ATTR_GROUP_PORT, "disable"); if(!a) /* attribute unknown */ return -1; ret = swlib_set_attr_string(sw_ctx->dev, a, id, "0"); return ret; }
/* set/get switch port pvid (native vlan) */ int port_set_pvid(int id, struct swconfig_context *sw_ctx, int vlan){ int ret = 0; struct switch_attr *a; char *v; if(NULL == sw_ctx->dev) return -1; a = swlib_lookup_attr(sw_ctx->dev, SWLIB_ATTR_GROUP_PORT, "pvid"); if(!a) /* attribute unknown */ return -1; sprintf(v, "%d", vlan); ret = swlib_set_attr_string(sw_ctx->dev, a, id, v); return ret; }
/* set/get switch port untagged */ int port_set_untag(int id, struct swconfig_context *sw_ctx, int untag){ int ret = 0; struct switch_attr *a; char *f; if(NULL == sw_ctx->dev) return -1; a = swlib_lookup_attr(sw_ctx->dev, SWLIB_ATTR_GROUP_PORT, "untag"); if(!a) /* attribute unknown */ return -1; sprintf(f, "%d", untag); ret = swlib_set_attr_string(sw_ctx->dev, a, id, f); return ret; }
int main(int argc, char **argv) { int retval = 0; struct switch_dev *dev; struct switch_attr *a; struct switch_val val; int err; int i; int cmd = CMD_NONE; char *cdev = NULL; int cport = -1; int cvlan = -1; char *ckey = NULL; char *cvalue = NULL; if(argc < 4) print_usage(); if(strcmp(argv[1], "dev")) print_usage(); cdev = argv[2]; for(i = 3; i < argc; i++) { char *arg = argv[i]; if (cmd != CMD_NONE) { print_usage(); } else if (!strcmp(arg, "port") && i+1 < argc) { cport = atoi(argv[++i]); } else if (!strcmp(arg, "vlan") && i+1 < argc) { cvlan = atoi(argv[++i]); } else if (!strcmp(arg, "help")) { cmd = CMD_HELP; } else if (!strcmp(arg, "set") && i+1 < argc) { cmd = CMD_SET; ckey = argv[++i]; if (i+1 < argc) cvalue = argv[++i]; } else if (!strcmp(arg, "get") && i+1 < argc) { cmd = CMD_GET; ckey = argv[++i]; } else if (!strcmp(arg, "load") && i+1 < argc) { if ((cport >= 0) || (cvlan >= 0)) print_usage(); cmd = CMD_LOAD; ckey = argv[++i]; } else if (!strcmp(arg, "show")) { cmd = CMD_SHOW; } else { print_usage(); } } if (cmd == CMD_NONE) print_usage(); if (cport > -1 && cvlan > -1) print_usage(); dev = swlib_connect(cdev); if (!dev) { fprintf(stderr, "Failed to connect to the switch\n"); return 1; } swlib_scan(dev); if (cmd == CMD_GET || cmd == CMD_SET) { if(cport > -1) a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_PORT, ckey); else if(cvlan > -1) a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, ckey); else a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, ckey); if(!a) { fprintf(stderr, "Unknown attribute \"%s\"\n", ckey); goto out; } } switch(cmd) { case CMD_SET: if ((a->type != SWITCH_TYPE_NOVAL) && (cvalue == NULL)) print_usage(); if(cvlan > -1) cport = cvlan; if(swlib_set_attr_string(dev, a, cport, cvalue) < 0) { fprintf(stderr, "failed\n"); retval = -1; goto out; } break; case CMD_GET: if(cvlan > -1) val.port_vlan = cvlan; if(cport > -1) val.port_vlan = cport; if(swlib_get_attr(dev, a, &val) < 0) { fprintf(stderr, "failed\n"); retval = -1; goto out; } print_attr_val(a, &val); putchar('\n'); break; case CMD_LOAD: swconfig_load_uci(dev, ckey); break; case CMD_HELP: list_attributes(dev); break; case CMD_SHOW: if (cport >= 0 || cvlan >= 0) { if (cport >= 0) show_port(dev, cport); else show_vlan(dev, cvlan, false); } else { show_global(dev); for (i=0; i < dev->ports; i++) show_port(dev, i); for (i=0; i < dev->vlans; i++) show_vlan(dev, i, true); } break; } out: swlib_free_all(dev); return 0; }