libgpg-error: Fix compiling with uclibc
[openadk.git] / package / swconfig / src / swlib.h
blobdba68b28e26b76985a64ff1a94c27f994cde7218
1 /*
2 * swlib.h: Switch configuration API (user space part)
4 * Copyright (C) 2008-2009 Felix Fietkau <nbd@openwrt.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License
8 * version 2.1 as published by the Free Software Foundation.
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.
16 Usage of the library functions:
18 The main datastructure for a switch is the struct switch_device
19 To get started, you first need to use switch_connect() to probe
20 for switches and allocate an instance of this struct.
22 There are two possible usage modes:
23 dev = switch_connect("eth0");
24 - this call will look for a switch registered for the linux device
25 "eth0" and only allocate a switch_device for this particular switch.
27 dev = switch_connect(NULL)
28 - this will return one switch_device struct for each available
29 switch. The switch_device structs are chained with by ->next pointer
31 Then to query a switch for all available attributes, use:
32 swlib_scan(dev);
34 All allocated datastructures for the switch_device struct can be freed with
35 swlib_free(dev);
37 swlib_free_all(dev);
39 The latter traverses a whole chain of switch_device structs and frees them all
41 Switch attributes (struct switch_attr) are divided into three groups:
42 dev->ops:
43 - global settings
44 dev->port_ops:
45 - per-port settings
46 dev->vlan_ops:
47 - per-vlan settings
49 switch_lookup_attr() is a small helper function to locate attributes
50 by name.
52 switch_set_attr() and switch_get_attr() can alter or request the values
53 of attributes.
55 Usage of the switch_attr struct:
57 ->atype: attribute group, one of:
58 - SWLIB_ATTR_GROUP_GLOBAL
59 - SWLIB_ATTR_GROUP_VLAN
60 - SWLIB_ATTR_GROUP_PORT
62 ->id: identifier for the attribute
64 ->type: data type, one of:
65 - SWITCH_TYPE_INT
66 - SWITCH_TYPE_STRING
67 - SWITCH_TYPE_PORT
69 ->name: short name of the attribute
70 ->description: longer description
71 ->next: pointer to the next attribute of the current group
74 Usage of the switch_val struct:
76 When setting attributes, following members of the struct switch_val need
77 to be set up:
79 ->len (for attr->type == SWITCH_TYPE_PORT)
80 ->port_vlan:
81 - port number (for attr->atype == SWLIB_ATTR_GROUP_PORT), or:
82 - vlan number (for attr->atype == SWLIB_ATTR_GROUP_VLAN)
83 ->value.i (for attr->type == SWITCH_TYPE_INT)
84 ->value.s (for attr->type == SWITCH_TYPE_STRING)
85 - owned by the caller, not stored in the library internally
86 ->value.ports (for attr->type == SWITCH_TYPE_PORT)
87 - must point to an array of at lest val->len * sizeof(struct switch_port)
89 When getting string attributes, val->value.s must be freed by the caller
90 When getting port list attributes, an internal static buffer is used,
91 which changes from call to call.
95 #ifndef __SWLIB_H
96 #define __SWLIB_H
98 enum swlib_attr_group {
99 SWLIB_ATTR_GROUP_GLOBAL,
100 SWLIB_ATTR_GROUP_VLAN,
101 SWLIB_ATTR_GROUP_PORT,
104 enum swlib_port_flags {
105 SWLIB_PORT_FLAG_TAGGED = (1 << 0),
109 struct switch_dev;
110 struct switch_attr;
111 struct switch_port;
112 struct switch_port_map;
113 struct switch_val;
115 struct switch_dev {
116 int id;
117 char dev_name[IFNAMSIZ];
118 const char *name;
119 const char *alias;
120 int ports;
121 int vlans;
122 int cpu_port;
123 struct switch_attr *ops;
124 struct switch_attr *port_ops;
125 struct switch_attr *vlan_ops;
126 struct switch_portmap *maps;
127 struct switch_dev *next;
128 void *priv;
131 struct switch_val {
132 struct switch_attr *attr;
133 int len;
134 int err;
135 int port_vlan;
136 union {
137 const char *s;
138 int i;
139 struct switch_port *ports;
140 } value;
143 struct switch_attr {
144 struct switch_dev *dev;
145 int atype;
146 int id;
147 int type;
148 const char *name;
149 const char *description;
150 struct switch_attr *next;
153 struct switch_port {
154 unsigned int id;
155 unsigned int flags;
158 struct switch_portmap {
159 unsigned int virt;
160 const char *segment;
164 * swlib_list: list all switches
166 void swlib_list(void);
169 * swlib_print_portmap: get portmap
170 * @dev: switch device struct
172 void swlib_print_portmap(struct switch_dev *dev, char *segment);
175 * swlib_connect: connect to the switch through netlink
176 * @name: name of the ethernet interface,
178 * if name is NULL, it connect and builds a chain of all switches
180 struct switch_dev *swlib_connect(const char *name);
183 * swlib_free: free all dynamically allocated data for the switch connection
184 * @dev: switch device struct
186 * all members of a switch device chain (generated by swlib_connect(NULL))
187 * must be freed individually
189 void swlib_free(struct switch_dev *dev);
192 * swlib_free_all: run swlib_free on all devices in the chain
193 * @dev: switch device struct
195 void swlib_free_all(struct switch_dev *dev);
198 * swlib_scan: probe the switch driver for available commands/attributes
199 * @dev: switch device struct
201 int swlib_scan(struct switch_dev *dev);
204 * swlib_lookup_attr: look up a switch attribute
205 * @dev: switch device struct
206 * @type: global, port or vlan
207 * @name: name of the attribute
209 struct switch_attr *swlib_lookup_attr(struct switch_dev *dev,
210 enum swlib_attr_group atype, const char *name);
213 * swlib_set_attr: set the value for an attribute
214 * @dev: switch device struct
215 * @attr: switch attribute struct
216 * @val: attribute value pointer
217 * returns 0 on success
219 int swlib_set_attr(struct switch_dev *dev, struct switch_attr *attr,
220 struct switch_val *val);
223 * swlib_set_attr_string: set the value for an attribute with type conversion
224 * @dev: switch device struct
225 * @attr: switch attribute struct
226 * @port_vlan: port or vlan (if applicable)
227 * @str: string value
228 * returns 0 on success
230 int swlib_set_attr_string(struct switch_dev *dev, struct switch_attr *attr,
231 int port_vlan, const char *str);
234 * swlib_get_attr: get the value for an attribute
235 * @dev: switch device struct
236 * @attr: switch attribute struct
237 * @val: attribute value pointer
238 * returns 0 on success
239 * for string attributes, the result string must be freed by the caller
241 int swlib_get_attr(struct switch_dev *dev, struct switch_attr *attr,
242 struct switch_val *val);
244 #endif