b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * swconfig.c: Switch configuration utility |
| 3 | * |
| 4 | * Copyright (C) 2008 Felix Fietkau <nbd@nbd.name> |
| 5 | * Copyright (C) 2010 Martin Mares <mj@ucw.cz> |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or |
| 8 | * modify it under the terms of the GNU General Public License |
| 9 | * version 2 as published by the Free Software Foundatio. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <inttypes.h> |
| 21 | #include <errno.h> |
| 22 | #include <stdint.h> |
| 23 | #include <getopt.h> |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/socket.h> |
| 26 | #include <uci.h> |
| 27 | |
| 28 | #include <linux/types.h> |
| 29 | #include <linux/netlink.h> |
| 30 | #include <linux/genetlink.h> |
| 31 | #include <netlink/netlink.h> |
| 32 | #include <netlink/genl/genl.h> |
| 33 | #include <netlink/genl/ctrl.h> |
| 34 | #include <linux/switch.h> |
| 35 | #include "swlib.h" |
| 36 | |
| 37 | enum { |
| 38 | CMD_NONE, |
| 39 | CMD_GET, |
| 40 | CMD_SET, |
| 41 | CMD_LOAD, |
| 42 | CMD_HELP, |
| 43 | CMD_SHOW, |
| 44 | CMD_PORTMAP, |
| 45 | }; |
| 46 | |
| 47 | static void |
| 48 | print_attrs(const struct switch_attr *attr) |
| 49 | { |
| 50 | int i = 0; |
| 51 | while (attr) { |
| 52 | const char *type; |
| 53 | switch(attr->type) { |
| 54 | case SWITCH_TYPE_INT: |
| 55 | type = "int"; |
| 56 | break; |
| 57 | case SWITCH_TYPE_STRING: |
| 58 | type = "string"; |
| 59 | break; |
| 60 | case SWITCH_TYPE_PORTS: |
| 61 | type = "ports"; |
| 62 | break; |
| 63 | case SWITCH_TYPE_NOVAL: |
| 64 | type = "none"; |
| 65 | break; |
| 66 | default: |
| 67 | type = "unknown"; |
| 68 | break; |
| 69 | } |
| 70 | printf("\tAttribute %d (%s): %s (%s)\n", ++i, type, attr->name, attr->description); |
| 71 | attr = attr->next; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | static void |
| 76 | list_attributes(struct switch_dev *dev) |
| 77 | { |
| 78 | printf("%s: %s(%s), ports: %d (cpu @ %d), vlans: %d\n", dev->dev_name, dev->alias, dev->name, dev->ports, dev->cpu_port, dev->vlans); |
| 79 | printf(" --switch\n"); |
| 80 | print_attrs(dev->ops); |
| 81 | printf(" --vlan\n"); |
| 82 | print_attrs(dev->vlan_ops); |
| 83 | printf(" --port\n"); |
| 84 | print_attrs(dev->port_ops); |
| 85 | } |
| 86 | |
| 87 | static const char * |
| 88 | speed_str(int speed) |
| 89 | { |
| 90 | switch (speed) { |
| 91 | case 10: |
| 92 | return "10baseT"; |
| 93 | case 100: |
| 94 | return "100baseT"; |
| 95 | case 1000: |
| 96 | return "1000baseT"; |
| 97 | default: |
| 98 | break; |
| 99 | } |
| 100 | |
| 101 | return "unknown"; |
| 102 | } |
| 103 | |
| 104 | static void |
| 105 | free_attr_val(const struct switch_attr *attr, const struct switch_val *val) |
| 106 | { |
| 107 | switch (attr->type) { |
| 108 | case SWITCH_TYPE_STRING: |
| 109 | free(val->value.s); |
| 110 | break; |
| 111 | case SWITCH_TYPE_PORTS: |
| 112 | free(val->value.ports); |
| 113 | break; |
| 114 | case SWITCH_TYPE_LINK: |
| 115 | free(val->value.link); |
| 116 | break; |
| 117 | default: |
| 118 | break; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | static void |
| 123 | print_attr_val(const struct switch_attr *attr, const struct switch_val *val) |
| 124 | { |
| 125 | struct switch_port_link *link; |
| 126 | int i; |
| 127 | |
| 128 | switch (attr->type) { |
| 129 | case SWITCH_TYPE_INT: |
| 130 | printf("%d", val->value.i); |
| 131 | break; |
| 132 | case SWITCH_TYPE_STRING: |
| 133 | printf("%s", val->value.s); |
| 134 | break; |
| 135 | case SWITCH_TYPE_PORTS: |
| 136 | for(i = 0; i < val->len; i++) { |
| 137 | printf("%d%s ", |
| 138 | val->value.ports[i].id, |
| 139 | (val->value.ports[i].flags & |
| 140 | SWLIB_PORT_FLAG_TAGGED) ? "t" : ""); |
| 141 | } |
| 142 | break; |
| 143 | case SWITCH_TYPE_LINK: |
| 144 | link = val->value.link; |
| 145 | if (link->link) |
| 146 | printf("port:%d link:up speed:%s %s-duplex %s%s%s%s%s", |
| 147 | val->port_vlan, |
| 148 | speed_str(link->speed), |
| 149 | link->duplex ? "full" : "half", |
| 150 | link->tx_flow ? "txflow " : "", |
| 151 | link->rx_flow ? "rxflow " : "", |
| 152 | link->eee & SWLIB_LINK_FLAG_EEE_100BASET ? "eee100 " : "", |
| 153 | link->eee & SWLIB_LINK_FLAG_EEE_1000BASET ? "eee1000 " : "", |
| 154 | link->aneg ? "auto" : ""); |
| 155 | else |
| 156 | printf("port:%d link:down", val->port_vlan); |
| 157 | break; |
| 158 | default: |
| 159 | printf("?unknown-type?"); |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | static void |
| 164 | show_attrs(struct switch_dev *dev, struct switch_attr *attr, struct switch_val *val) |
| 165 | { |
| 166 | while (attr) { |
| 167 | if (attr->type != SWITCH_TYPE_NOVAL) { |
| 168 | printf("\t%s: ", attr->name); |
| 169 | if (swlib_get_attr(dev, attr, val) < 0) |
| 170 | printf("???"); |
| 171 | else { |
| 172 | print_attr_val(attr, val); |
| 173 | free_attr_val(attr, val); |
| 174 | } |
| 175 | putchar('\n'); |
| 176 | } |
| 177 | attr = attr->next; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static void |
| 182 | show_global(struct switch_dev *dev) |
| 183 | { |
| 184 | struct switch_val val; |
| 185 | |
| 186 | printf("Global attributes:\n"); |
| 187 | show_attrs(dev, dev->ops, &val); |
| 188 | } |
| 189 | |
| 190 | static void |
| 191 | show_port(struct switch_dev *dev, int port) |
| 192 | { |
| 193 | struct switch_val val; |
| 194 | |
| 195 | printf("Port %d:\n", port); |
| 196 | val.port_vlan = port; |
| 197 | show_attrs(dev, dev->port_ops, &val); |
| 198 | } |
| 199 | |
| 200 | static void |
| 201 | show_vlan(struct switch_dev *dev, int vlan, bool all) |
| 202 | { |
| 203 | struct switch_val val; |
| 204 | struct switch_attr *attr; |
| 205 | |
| 206 | val.port_vlan = vlan; |
| 207 | |
| 208 | if (all) { |
| 209 | attr = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, "ports"); |
| 210 | if (swlib_get_attr(dev, attr, &val) < 0) |
| 211 | return; |
| 212 | |
| 213 | if (!val.len) |
| 214 | return; |
| 215 | } |
| 216 | |
| 217 | printf("VLAN %d:\n", vlan); |
| 218 | show_attrs(dev, dev->vlan_ops, &val); |
| 219 | } |
| 220 | |
| 221 | static void |
| 222 | print_usage(void) |
| 223 | { |
| 224 | printf("swconfig list\n"); |
| 225 | printf("swconfig dev <dev> [port <port>|vlan <vlan>] (help|set <key> <value>|get <key>|load <config>|show)\n"); |
| 226 | exit(1); |
| 227 | } |
| 228 | |
| 229 | static void |
| 230 | swconfig_load_uci(struct switch_dev *dev, const char *name) |
| 231 | { |
| 232 | struct uci_context *ctx; |
| 233 | struct uci_package *p = NULL; |
| 234 | int ret = -1; |
| 235 | |
| 236 | ctx = uci_alloc_context(); |
| 237 | if (!ctx) |
| 238 | return; |
| 239 | |
| 240 | uci_load(ctx, name, &p); |
| 241 | if (!p) { |
| 242 | uci_perror(ctx, "Failed to load config file: "); |
| 243 | goto out; |
| 244 | } |
| 245 | |
| 246 | ret = swlib_apply_from_uci(dev, p); |
| 247 | if (ret < 0) |
| 248 | fprintf(stderr, "Failed to apply configuration for switch '%s'\n", dev->dev_name); |
| 249 | |
| 250 | out: |
| 251 | uci_free_context(ctx); |
| 252 | exit(ret); |
| 253 | } |
| 254 | |
| 255 | int main(int argc, char **argv) |
| 256 | { |
| 257 | int retval = 0; |
| 258 | struct switch_dev *dev; |
| 259 | struct switch_attr *a; |
| 260 | struct switch_val val; |
| 261 | int i; |
| 262 | |
| 263 | int cmd = CMD_NONE; |
| 264 | char *cdev = NULL; |
| 265 | int cport = -1; |
| 266 | int cvlan = -1; |
| 267 | char *ckey = NULL; |
| 268 | char *cvalue = NULL; |
| 269 | char *csegment = NULL; |
| 270 | |
| 271 | if((argc == 2) && !strcmp(argv[1], "list")) { |
| 272 | swlib_list(); |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | if(argc < 4) |
| 277 | print_usage(); |
| 278 | |
| 279 | if(strcmp(argv[1], "dev")) |
| 280 | print_usage(); |
| 281 | |
| 282 | cdev = argv[2]; |
| 283 | |
| 284 | for(i = 3; i < argc; i++) |
| 285 | { |
| 286 | char *arg = argv[i]; |
| 287 | if (cmd != CMD_NONE) { |
| 288 | print_usage(); |
| 289 | } else if (!strcmp(arg, "port") && i+1 < argc) { |
| 290 | cport = atoi(argv[++i]); |
| 291 | } else if (!strcmp(arg, "vlan") && i+1 < argc) { |
| 292 | cvlan = atoi(argv[++i]); |
| 293 | } else if (!strcmp(arg, "help")) { |
| 294 | cmd = CMD_HELP; |
| 295 | } else if (!strcmp(arg, "set") && i+1 < argc) { |
| 296 | cmd = CMD_SET; |
| 297 | ckey = argv[++i]; |
| 298 | if (i+1 < argc) |
| 299 | cvalue = argv[++i]; |
| 300 | } else if (!strcmp(arg, "get") && i+1 < argc) { |
| 301 | cmd = CMD_GET; |
| 302 | ckey = argv[++i]; |
| 303 | } else if (!strcmp(arg, "load") && i+1 < argc) { |
| 304 | if ((cport >= 0) || (cvlan >= 0)) |
| 305 | print_usage(); |
| 306 | cmd = CMD_LOAD; |
| 307 | ckey = argv[++i]; |
| 308 | } else if (!strcmp(arg, "portmap")) { |
| 309 | if (i + 1 < argc) |
| 310 | csegment = argv[++i]; |
| 311 | cmd = CMD_PORTMAP; |
| 312 | } else if (!strcmp(arg, "show")) { |
| 313 | cmd = CMD_SHOW; |
| 314 | } else { |
| 315 | print_usage(); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | if (cmd == CMD_NONE) |
| 320 | print_usage(); |
| 321 | if (cport > -1 && cvlan > -1) |
| 322 | print_usage(); |
| 323 | |
| 324 | dev = swlib_connect(cdev); |
| 325 | if (!dev) { |
| 326 | fprintf(stderr, "Failed to connect to the switch. Use the \"list\" command to see which switches are available.\n"); |
| 327 | return 1; |
| 328 | } |
| 329 | |
| 330 | swlib_scan(dev); |
| 331 | |
| 332 | if (cmd == CMD_GET || cmd == CMD_SET) { |
| 333 | if(cport > -1) |
| 334 | a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_PORT, ckey); |
| 335 | else if(cvlan > -1) |
| 336 | a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, ckey); |
| 337 | else |
| 338 | a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, ckey); |
| 339 | |
| 340 | if(!a) |
| 341 | { |
| 342 | fprintf(stderr, "Unknown attribute \"%s\"\n", ckey); |
| 343 | retval = -1; |
| 344 | goto out; |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | switch(cmd) |
| 349 | { |
| 350 | case CMD_SET: |
| 351 | if ((a->type != SWITCH_TYPE_NOVAL) && |
| 352 | (cvalue == NULL)) |
| 353 | print_usage(); |
| 354 | |
| 355 | if(cvlan > -1) |
| 356 | cport = cvlan; |
| 357 | |
| 358 | retval = swlib_set_attr_string(dev, a, cport, cvalue); |
| 359 | if (retval < 0) |
| 360 | { |
| 361 | nl_perror(-retval, "Failed to set attribute"); |
| 362 | goto out; |
| 363 | } |
| 364 | break; |
| 365 | case CMD_GET: |
| 366 | if(cvlan > -1) |
| 367 | val.port_vlan = cvlan; |
| 368 | if(cport > -1) |
| 369 | val.port_vlan = cport; |
| 370 | retval = swlib_get_attr(dev, a, &val); |
| 371 | if (retval < 0) |
| 372 | { |
| 373 | nl_perror(-retval, "Failed to get attribute"); |
| 374 | goto out; |
| 375 | } |
| 376 | print_attr_val(a, &val); |
| 377 | free_attr_val(a, &val); |
| 378 | putchar('\n'); |
| 379 | break; |
| 380 | case CMD_LOAD: |
| 381 | swconfig_load_uci(dev, ckey); |
| 382 | break; |
| 383 | case CMD_HELP: |
| 384 | list_attributes(dev); |
| 385 | break; |
| 386 | case CMD_PORTMAP: |
| 387 | swlib_print_portmap(dev, csegment); |
| 388 | break; |
| 389 | case CMD_SHOW: |
| 390 | if (cport >= 0 || cvlan >= 0) { |
| 391 | if (cport >= 0) |
| 392 | show_port(dev, cport); |
| 393 | else |
| 394 | show_vlan(dev, cvlan, false); |
| 395 | } else { |
| 396 | show_global(dev); |
| 397 | for (i=0; i < dev->ports; i++) |
| 398 | show_port(dev, i); |
| 399 | for (i=0; i < dev->vlans; i++) |
| 400 | show_vlan(dev, i, true); |
| 401 | } |
| 402 | break; |
| 403 | } |
| 404 | |
| 405 | out: |
| 406 | swlib_free_all(dev); |
| 407 | return retval; |
| 408 | } |