enic_pp.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright 2011 Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This program is free software; you may redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  9. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  10. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  12. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  13. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15. * SOFTWARE.
  16. *
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/string.h>
  20. #include <linux/errno.h>
  21. #include <linux/types.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/rtnetlink.h>
  25. #include <net/ip.h>
  26. #include "vnic_vic.h"
  27. #include "enic_res.h"
  28. #include "enic.h"
  29. #include "enic_dev.h"
  30. static int enic_set_port_profile(struct enic *enic)
  31. {
  32. struct net_device *netdev = enic->netdev;
  33. struct vic_provinfo *vp;
  34. const u8 oui[3] = VIC_PROVINFO_CISCO_OUI;
  35. const u16 os_type = htons(VIC_GENERIC_PROV_OS_TYPE_LINUX);
  36. char uuid_str[38];
  37. char client_mac_str[18];
  38. u8 *client_mac;
  39. int err;
  40. if (!(enic->pp.set & ENIC_SET_NAME) || !strlen(enic->pp.name))
  41. return -EINVAL;
  42. vp = vic_provinfo_alloc(GFP_KERNEL, oui,
  43. VIC_PROVINFO_GENERIC_TYPE);
  44. if (!vp)
  45. return -ENOMEM;
  46. VIC_PROVINFO_ADD_TLV(vp,
  47. VIC_GENERIC_PROV_TLV_PORT_PROFILE_NAME_STR,
  48. strlen(enic->pp.name) + 1, enic->pp.name);
  49. if (!is_zero_ether_addr(enic->pp.mac_addr))
  50. client_mac = enic->pp.mac_addr;
  51. else
  52. client_mac = netdev->dev_addr;
  53. VIC_PROVINFO_ADD_TLV(vp,
  54. VIC_GENERIC_PROV_TLV_CLIENT_MAC_ADDR,
  55. ETH_ALEN, client_mac);
  56. snprintf(client_mac_str, sizeof(client_mac_str), "%pM", client_mac);
  57. VIC_PROVINFO_ADD_TLV(vp,
  58. VIC_GENERIC_PROV_TLV_CLUSTER_PORT_UUID_STR,
  59. sizeof(client_mac_str), client_mac_str);
  60. if (enic->pp.set & ENIC_SET_INSTANCE) {
  61. sprintf(uuid_str, "%pUB", enic->pp.instance_uuid);
  62. VIC_PROVINFO_ADD_TLV(vp,
  63. VIC_GENERIC_PROV_TLV_CLIENT_UUID_STR,
  64. sizeof(uuid_str), uuid_str);
  65. }
  66. if (enic->pp.set & ENIC_SET_HOST) {
  67. sprintf(uuid_str, "%pUB", enic->pp.host_uuid);
  68. VIC_PROVINFO_ADD_TLV(vp,
  69. VIC_GENERIC_PROV_TLV_HOST_UUID_STR,
  70. sizeof(uuid_str), uuid_str);
  71. }
  72. VIC_PROVINFO_ADD_TLV(vp,
  73. VIC_GENERIC_PROV_TLV_OS_TYPE,
  74. sizeof(os_type), &os_type);
  75. err = enic_dev_status_to_errno(enic_dev_init_prov2(enic, vp));
  76. add_tlv_failure:
  77. vic_provinfo_free(vp);
  78. return err;
  79. }
  80. static int enic_unset_port_profile(struct enic *enic)
  81. {
  82. int err;
  83. err = enic_vnic_dev_deinit(enic);
  84. if (err)
  85. return enic_dev_status_to_errno(err);
  86. enic_reset_addr_lists(enic);
  87. return 0;
  88. }
  89. static int enic_are_pp_different(struct enic_port_profile *pp1,
  90. struct enic_port_profile *pp2)
  91. {
  92. return strcmp(pp1->name, pp2->name) | !!memcmp(pp1->instance_uuid,
  93. pp2->instance_uuid, PORT_UUID_MAX) |
  94. !!memcmp(pp1->host_uuid, pp2->host_uuid, PORT_UUID_MAX) |
  95. !!memcmp(pp1->mac_addr, pp2->mac_addr, ETH_ALEN);
  96. }
  97. static int enic_pp_preassociate(struct enic *enic,
  98. struct enic_port_profile *prev_pp, int *restore_pp);
  99. static int enic_pp_disassociate(struct enic *enic,
  100. struct enic_port_profile *prev_pp, int *restore_pp);
  101. static int enic_pp_preassociate_rr(struct enic *enic,
  102. struct enic_port_profile *prev_pp, int *restore_pp);
  103. static int enic_pp_associate(struct enic *enic,
  104. struct enic_port_profile *prev_pp, int *restore_pp);
  105. static int (*enic_pp_handlers[])(struct enic *enic,
  106. struct enic_port_profile *prev_state, int *restore_pp) = {
  107. [PORT_REQUEST_PREASSOCIATE] = enic_pp_preassociate,
  108. [PORT_REQUEST_PREASSOCIATE_RR] = enic_pp_preassociate_rr,
  109. [PORT_REQUEST_ASSOCIATE] = enic_pp_associate,
  110. [PORT_REQUEST_DISASSOCIATE] = enic_pp_disassociate,
  111. };
  112. static const int enic_pp_handlers_count =
  113. sizeof(enic_pp_handlers)/sizeof(*enic_pp_handlers);
  114. static int enic_pp_preassociate(struct enic *enic,
  115. struct enic_port_profile *prev_pp, int *restore_pp)
  116. {
  117. return -EOPNOTSUPP;
  118. }
  119. static int enic_pp_disassociate(struct enic *enic,
  120. struct enic_port_profile *prev_pp, int *restore_pp)
  121. {
  122. return enic_unset_port_profile(enic);
  123. }
  124. static int enic_pp_preassociate_rr(struct enic *enic,
  125. struct enic_port_profile *prev_pp, int *restore_pp)
  126. {
  127. int err;
  128. int active = 0;
  129. if (enic->pp.request != PORT_REQUEST_ASSOCIATE) {
  130. /* If pre-associate is not part of an associate.
  131. We always disassociate first */
  132. err = enic_pp_handlers[PORT_REQUEST_DISASSOCIATE](enic,
  133. prev_pp, restore_pp);
  134. if (err)
  135. return err;
  136. *restore_pp = 0;
  137. }
  138. *restore_pp = 0;
  139. err = enic_set_port_profile(enic);
  140. if (err)
  141. return err;
  142. /* If pre-associate is not part of an associate. */
  143. if (enic->pp.request != PORT_REQUEST_ASSOCIATE)
  144. err = enic_dev_status_to_errno(enic_dev_enable2(enic, active));
  145. return err;
  146. }
  147. static int enic_pp_associate(struct enic *enic,
  148. struct enic_port_profile *prev_pp, int *restore_pp)
  149. {
  150. int err;
  151. int active = 1;
  152. /* Check if a pre-associate was called before */
  153. if (prev_pp->request != PORT_REQUEST_PREASSOCIATE_RR ||
  154. (prev_pp->request == PORT_REQUEST_PREASSOCIATE_RR &&
  155. enic_are_pp_different(prev_pp, &enic->pp))) {
  156. err = enic_pp_handlers[PORT_REQUEST_DISASSOCIATE](
  157. enic, prev_pp, restore_pp);
  158. if (err)
  159. return err;
  160. *restore_pp = 0;
  161. }
  162. err = enic_pp_handlers[PORT_REQUEST_PREASSOCIATE_RR](
  163. enic, prev_pp, restore_pp);
  164. if (err)
  165. return err;
  166. *restore_pp = 0;
  167. return enic_dev_status_to_errno(enic_dev_enable2(enic, active));
  168. }
  169. int enic_process_set_pp_request(struct enic *enic,
  170. struct enic_port_profile *prev_pp, int *restore_pp)
  171. {
  172. if (enic->pp.request < enic_pp_handlers_count
  173. && enic_pp_handlers[enic->pp.request])
  174. return enic_pp_handlers[enic->pp.request](enic,
  175. prev_pp, restore_pp);
  176. else
  177. return -EOPNOTSUPP;
  178. }
  179. int enic_process_get_pp_request(struct enic *enic, int request,
  180. u16 *response)
  181. {
  182. int err, status = ERR_SUCCESS;
  183. switch (request) {
  184. case PORT_REQUEST_PREASSOCIATE_RR:
  185. case PORT_REQUEST_ASSOCIATE:
  186. err = enic_dev_enable2_done(enic, &status);
  187. break;
  188. case PORT_REQUEST_DISASSOCIATE:
  189. err = enic_dev_deinit_done(enic, &status);
  190. break;
  191. default:
  192. return -EINVAL;
  193. }
  194. if (err)
  195. status = err;
  196. switch (status) {
  197. case ERR_SUCCESS:
  198. *response = PORT_PROFILE_RESPONSE_SUCCESS;
  199. break;
  200. case ERR_EINVAL:
  201. *response = PORT_PROFILE_RESPONSE_INVALID;
  202. break;
  203. case ERR_EBADSTATE:
  204. *response = PORT_PROFILE_RESPONSE_BADSTATE;
  205. break;
  206. case ERR_ENOMEM:
  207. *response = PORT_PROFILE_RESPONSE_INSUFFICIENT_RESOURCES;
  208. break;
  209. case ERR_EINPROGRESS:
  210. *response = PORT_PROFILE_RESPONSE_INPROGRESS;
  211. break;
  212. default:
  213. *response = PORT_PROFILE_RESPONSE_ERROR;
  214. break;
  215. }
  216. return 0;
  217. }