source: src/router/swconfig/cli.c @ 12079

Last change on this file since 12079 was 12079, checked in by BrainSlayer, 4 years ago

nbds swconfig tool (modified)

File size: 4.4 KB
Line 
1/*
2 * swconfig.c: Switch configuration utility
3 *
4 * Copyright (C) 2008 Felix Fietkau <nbd@openwrt.org>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * version 2 as published by the Free Software Foundatio.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU General Public License for more details.
14 */
15
16#include <stdio.h>
17#include <string.h>
18#include <stdlib.h>
19#include <inttypes.h>
20#include <errno.h>
21#include <stdint.h>
22#include <getopt.h>
23#include <sys/types.h>
24#include <sys/socket.h>
25
26#include <linux/types.h>
27#include <linux/netlink.h>
28#include <linux/genetlink.h>
29#include <netlink/netlink.h>
30#include <netlink/genl/genl.h>
31#include <netlink/genl/ctrl.h>
32#include <linux/switch.h>
33#include "swlib.h"
34
35enum {
36        GET,
37        SET,
38        LOAD
39};
40
41static void
42print_attrs(const struct switch_attr *attr)
43{
44        int i = 0;
45        while (attr) {
46                const char *type;
47                switch(attr->type) {
48                        case SWITCH_TYPE_INT:
49                                type = "int";
50                                break;
51                        case SWITCH_TYPE_STRING:
52                                type = "string";
53                                break;
54                        case SWITCH_TYPE_PORTS:
55                                type = "ports";
56                                break;
57                        case SWITCH_TYPE_NOVAL:
58                                type = "none";
59                                break;
60                        default:
61                                type = "unknown";
62                                break;
63                }
64                printf("\tAttribute %d (%s): %s (%s)\n", ++i, type, attr->name, attr->description);
65                attr = attr->next;
66        }
67}
68
69static void
70list_attributes(struct switch_dev *dev)
71{
72        printf("Switch %d: %s(%s), ports: %d, vlans: %d\n", dev->id, dev->dev_name, dev->name, dev->ports, dev->vlans);
73        printf("     --switch\n");
74        print_attrs(dev->ops);
75        printf("     --vlan\n");
76        print_attrs(dev->vlan_ops);
77        printf("     --port\n");
78        print_attrs(dev->port_ops);
79}
80
81static void
82print_usage(void)
83{
84        printf("swconfig dev <dev> [port <port>|vlan <vlan>] (help|set <key> <value>|get <key>)\n");
85        exit(1);
86}
87
88
89int main(int argc, char **argv)
90{
91        int retval = 0;
92        struct switch_dev *dev;
93        struct switch_attr *a;
94        struct switch_val val;
95        int err;
96        int i;
97
98        struct switch_port *ports;
99
100        int cmd = 0;
101        char *cdev = NULL;
102        int cport = -1;
103        int cvlan = -1;
104        char *ckey = NULL;
105        char *cvalue = NULL;
106        int chelp = 0;
107
108        if(argc < 4)
109                print_usage();
110
111        if(strcmp(argv[1], "dev"))
112                print_usage();
113
114        cdev = argv[2];
115
116        for(i = 3; i < argc; i++)
117        {
118                int p;
119                if (!strcmp(argv[i], "help")) {
120                        chelp = 1;
121                        continue;
122                }
123                if( i + 1 >= argc)
124                        print_usage();
125                p = atoi(argv[i + 1]);
126                if (!strcmp(argv[i], "port")) {
127                        cport = p;
128                } else if (!strcmp(argv[i], "vlan")) {
129                        cvlan = p;
130                } else if (!strcmp(argv[i], "set")) {
131                        if(argc <= i + 1)
132                                print_usage();
133                        cmd = SET;
134                        ckey = argv[i + 1];
135                        if (argc > i + 2)
136                                cvalue = argv[i + 2];
137                        else
138                                cvalue = NULL;
139                        i++;
140                } else if (!strcmp(argv[i], "get")) {
141                        cmd = GET;
142                        ckey = argv[i + 1];
143                } else {
144                        print_usage();
145                }
146                i++;
147        }
148
149        if(cport > -1 && cvlan > -1)
150                print_usage();
151
152        dev = swlib_connect(cdev);
153        if (!dev) {
154                fprintf(stderr, "Failed to connect to the switch\n");
155                return 1;
156        }
157
158        ports = malloc(sizeof(struct switch_port) * dev->ports);
159        memset(ports, 0, sizeof(struct switch_port) * dev->ports);
160        swlib_scan(dev);
161
162        if(chelp)
163        {
164                list_attributes(dev);
165                goto out;
166        }
167
168        if (cmd != LOAD) {
169                if(cport > -1)
170                        a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_PORT, ckey);
171                else if(cvlan > -1)
172                        a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_VLAN, ckey);
173                else
174                        a = swlib_lookup_attr(dev, SWLIB_ATTR_GROUP_GLOBAL, ckey);
175
176                if(!a)
177                {
178                        fprintf(stderr, "Unknown attribute \"%s\"\n", ckey);
179                        goto out;
180                }
181        }
182
183        switch(cmd)
184        {
185        case SET:
186                if ((a->type != SWITCH_TYPE_NOVAL) &&
187                                (cvalue == NULL))
188                        print_usage();
189
190                if(cvlan > -1)
191                        cport = cvlan;
192
193                if(swlib_set_attr_string(dev, a, cport, cvalue) < 0)
194                {
195                        fprintf(stderr, "failed\n");
196                        retval = -1;
197                        goto out;
198                }
199                break;
200        case GET:
201                if(cvlan > -1)
202                        val.port_vlan = cvlan;
203                if(cport > -1)
204                        val.port_vlan = cport;
205                if(swlib_get_attr(dev, a, &val) < 0)
206                {
207                        fprintf(stderr, "failed\n");
208                        retval = -1;
209                        goto out;
210                }
211                switch(a->type) {
212                case SWITCH_TYPE_INT:
213                        printf("%d\n", val.value.i);
214                        break;
215                case SWITCH_TYPE_STRING:
216                        printf("%s\n", val.value.s);
217                        break;
218                case SWITCH_TYPE_PORTS:
219                        for(i = 0; i < val.len; i++)
220                                printf("%d ", val.value.ports[i]);
221                        printf("\n");
222                        break;
223                }
224                break;
225        }
226
227out:
228        swlib_free_all(dev);
229        free(ports);
230
231        return 0;
232}
Note: See TracBrowser for help on using the repository browser.