swlib.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * swlib.h: Switch configuration API (user space part)
  3. *
  4. * Copyright (C) 2008-2009 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 Lesser General Public License
  8. * version 2.1 as published by the Free Software Foundation.
  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. Usage of the library functions:
  16. The main datastructure for a switch is the struct switch_device
  17. To get started, you first need to use switch_connect() to probe
  18. for switches and allocate an instance of this struct.
  19. There are two possible usage modes:
  20. dev = switch_connect("eth0");
  21. - this call will look for a switch registered for the linux device
  22. "eth0" and only allocate a switch_device for this particular switch.
  23. dev = switch_connect(NULL)
  24. - this will return one switch_device struct for each available
  25. switch. The switch_device structs are chained with by ->next pointer
  26. Then to query a switch for all available attributes, use:
  27. swlib_scan(dev);
  28. All allocated datastructures for the switch_device struct can be freed with
  29. swlib_free(dev);
  30. or
  31. swlib_free_all(dev);
  32. The latter traverses a whole chain of switch_device structs and frees them all
  33. Switch attributes (struct switch_attr) are divided into three groups:
  34. dev->ops:
  35. - global settings
  36. dev->port_ops:
  37. - per-port settings
  38. dev->vlan_ops:
  39. - per-vlan settings
  40. switch_lookup_attr() is a small helper function to locate attributes
  41. by name.
  42. switch_set_attr() and switch_get_attr() can alter or request the values
  43. of attributes.
  44. Usage of the switch_attr struct:
  45. ->atype: attribute group, one of:
  46. - SWLIB_ATTR_GROUP_GLOBAL
  47. - SWLIB_ATTR_GROUP_VLAN
  48. - SWLIB_ATTR_GROUP_PORT
  49. ->id: identifier for the attribute
  50. ->type: data type, one of:
  51. - SWITCH_TYPE_INT
  52. - SWITCH_TYPE_STRING
  53. - SWITCH_TYPE_PORT
  54. ->name: short name of the attribute
  55. ->description: longer description
  56. ->next: pointer to the next attribute of the current group
  57. Usage of the switch_val struct:
  58. When setting attributes, following members of the struct switch_val need
  59. to be set up:
  60. ->len (for attr->type == SWITCH_TYPE_PORT)
  61. ->port_vlan:
  62. - port number (for attr->atype == SWLIB_ATTR_GROUP_PORT), or:
  63. - vlan number (for attr->atype == SWLIB_ATTR_GROUP_VLAN)
  64. ->value.i (for attr->type == SWITCH_TYPE_INT)
  65. ->value.s (for attr->type == SWITCH_TYPE_STRING)
  66. - owned by the caller, not stored in the library internally
  67. ->value.ports (for attr->type == SWITCH_TYPE_PORT)
  68. - must point to an array of at lest val->len * sizeof(struct switch_port)
  69. When getting string attributes, val->value.s must be freed by the caller
  70. When getting port list attributes, an internal static buffer is used,
  71. which changes from call to call.
  72. */
  73. #ifndef __SWLIB_H
  74. #define __SWLIB_H
  75. enum swlib_attr_group {
  76. SWLIB_ATTR_GROUP_GLOBAL,
  77. SWLIB_ATTR_GROUP_VLAN,
  78. SWLIB_ATTR_GROUP_PORT,
  79. };
  80. enum swlib_port_flags {
  81. SWLIB_PORT_FLAG_TAGGED = (1 << 0),
  82. };
  83. struct switch_dev;
  84. struct switch_attr;
  85. struct switch_port;
  86. struct switch_port_map;
  87. struct switch_val;
  88. struct switch_dev {
  89. int id;
  90. char dev_name[IFNAMSIZ];
  91. const char *name;
  92. const char *alias;
  93. int ports;
  94. int vlans;
  95. int cpu_port;
  96. struct switch_attr *ops;
  97. struct switch_attr *port_ops;
  98. struct switch_attr *vlan_ops;
  99. struct switch_portmap *maps;
  100. struct switch_dev *next;
  101. void *priv;
  102. };
  103. struct switch_val {
  104. struct switch_attr *attr;
  105. int len;
  106. int err;
  107. int port_vlan;
  108. union {
  109. const char *s;
  110. int i;
  111. struct switch_port *ports;
  112. } value;
  113. };
  114. struct switch_attr {
  115. struct switch_dev *dev;
  116. int atype;
  117. int id;
  118. int type;
  119. const char *name;
  120. const char *description;
  121. struct switch_attr *next;
  122. };
  123. struct switch_port {
  124. unsigned int id;
  125. unsigned int flags;
  126. };
  127. struct switch_portmap {
  128. unsigned int virt;
  129. const char *segment;
  130. };
  131. /**
  132. * swlib_list: list all switches
  133. */
  134. void swlib_list(void);
  135. /**
  136. * swlib_print_portmap: get portmap
  137. * @dev: switch device struct
  138. */
  139. void swlib_print_portmap(struct switch_dev *dev, char *segment);
  140. /**
  141. * swlib_connect: connect to the switch through netlink
  142. * @name: name of the ethernet interface,
  143. *
  144. * if name is NULL, it connect and builds a chain of all switches
  145. */
  146. struct switch_dev *swlib_connect(const char *name);
  147. /**
  148. * swlib_free: free all dynamically allocated data for the switch connection
  149. * @dev: switch device struct
  150. *
  151. * all members of a switch device chain (generated by swlib_connect(NULL))
  152. * must be freed individually
  153. */
  154. void swlib_free(struct switch_dev *dev);
  155. /**
  156. * swlib_free_all: run swlib_free on all devices in the chain
  157. * @dev: switch device struct
  158. */
  159. void swlib_free_all(struct switch_dev *dev);
  160. /**
  161. * swlib_scan: probe the switch driver for available commands/attributes
  162. * @dev: switch device struct
  163. */
  164. int swlib_scan(struct switch_dev *dev);
  165. /**
  166. * swlib_lookup_attr: look up a switch attribute
  167. * @dev: switch device struct
  168. * @type: global, port or vlan
  169. * @name: name of the attribute
  170. */
  171. struct switch_attr *swlib_lookup_attr(struct switch_dev *dev,
  172. enum swlib_attr_group atype, const char *name);
  173. /**
  174. * swlib_set_attr: set the value for an attribute
  175. * @dev: switch device struct
  176. * @attr: switch attribute struct
  177. * @val: attribute value pointer
  178. * returns 0 on success
  179. */
  180. int swlib_set_attr(struct switch_dev *dev, struct switch_attr *attr,
  181. struct switch_val *val);
  182. /**
  183. * swlib_set_attr_string: set the value for an attribute with type conversion
  184. * @dev: switch device struct
  185. * @attr: switch attribute struct
  186. * @port_vlan: port or vlan (if applicable)
  187. * @str: string value
  188. * returns 0 on success
  189. */
  190. int swlib_set_attr_string(struct switch_dev *dev, struct switch_attr *attr,
  191. int port_vlan, const char *str);
  192. /**
  193. * swlib_get_attr: get the value for an attribute
  194. * @dev: switch device struct
  195. * @attr: switch attribute struct
  196. * @val: attribute value pointer
  197. * returns 0 on success
  198. * for string attributes, the result string must be freed by the caller
  199. */
  200. int swlib_get_attr(struct switch_dev *dev, struct switch_attr *attr,
  201. struct switch_val *val);
  202. #endif