swlib.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * swlib.h: Switch configuration API (user space part)
  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 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_val;
  87. struct switch_dev {
  88. int id;
  89. const char *name;
  90. const char *dev_name;
  91. int ports;
  92. int vlans;
  93. struct switch_attr *ops;
  94. struct switch_attr *port_ops;
  95. struct switch_attr *vlan_ops;
  96. struct switch_dev *next;
  97. void *priv;
  98. };
  99. struct switch_val {
  100. struct switch_attr *attr;
  101. int len;
  102. int err;
  103. int port_vlan;
  104. union {
  105. const char *s;
  106. int i;
  107. struct switch_port *ports;
  108. } value;
  109. };
  110. struct switch_attr {
  111. struct switch_dev *dev;
  112. int atype;
  113. int id;
  114. int type;
  115. const char *name;
  116. const char *description;
  117. struct switch_attr *next;
  118. };
  119. struct switch_port {
  120. unsigned int id;
  121. unsigned int flags;
  122. };
  123. /**
  124. * swlib_connect: connect to the switch through netlink
  125. * @name: name of the ethernet interface,
  126. *
  127. * if name is NULL, it connect and builds a chain of all switches
  128. */
  129. struct switch_dev *swlib_connect(const char *name);
  130. /**
  131. * swlib_free: free all dynamically allocated data for the switch connection
  132. * @dev: switch device struct
  133. *
  134. * all members of a switch device chain (generated by swlib_connect(NULL))
  135. * must be freed individually
  136. */
  137. void swlib_free(struct switch_dev *dev);
  138. /**
  139. * swlib_free_all: run swlib_free on all devices in the chain
  140. * @dev: switch device struct
  141. */
  142. void swlib_free_all(struct switch_dev *dev);
  143. /**
  144. * swlib_scan: probe the switch driver for available commands/attributes
  145. * @dev: switch device struct
  146. */
  147. int swlib_scan(struct switch_dev *dev);
  148. /**
  149. * swlib_lookup_attr: look up a switch attribute
  150. * @dev: switch device struct
  151. * @type: global, port or vlan
  152. * @name: name of the attribute
  153. */
  154. struct switch_attr *swlib_lookup_attr(struct switch_dev *dev,
  155. enum swlib_attr_group atype, const char *name);
  156. /**
  157. * swlib_set_attr: set the value for an attribute
  158. * @dev: switch device struct
  159. * @attr: switch attribute struct
  160. * @val: attribute value pointer
  161. * returns 0 on success
  162. */
  163. int swlib_set_attr(struct switch_dev *dev, struct switch_attr *attr,
  164. struct switch_val *val);
  165. /**
  166. * swlib_get_attr: get the value for an attribute
  167. * @dev: switch device struct
  168. * @attr: switch attribute struct
  169. * @val: attribute value pointer
  170. * returns 0 on success
  171. * for string attributes, the result string must be freed by the caller
  172. */
  173. int swlib_get_attr(struct switch_dev *dev, struct switch_attr *attr,
  174. struct switch_val *val);
  175. #endif