ib_media.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. /*
  2. * net/tipc/ib_media.c: Infiniband bearer support for TIPC
  3. *
  4. * Copyright (c) 2013 Patrick McHardy <kaber@trash.net>
  5. *
  6. * Based on eth_media.c, which carries the following copyright notice:
  7. *
  8. * Copyright (c) 2001-2007, Ericsson AB
  9. * Copyright (c) 2005-2008, 2011, Wind River Systems
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the names of the copyright holders nor the names of its
  21. * contributors may be used to endorse or promote products derived from
  22. * this software without specific prior written permission.
  23. *
  24. * Alternatively, this software may be distributed under the terms of the
  25. * GNU General Public License ("GPL") version 2 as published by the Free
  26. * Software Foundation.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. */
  40. #include <linux/if_infiniband.h>
  41. #include "core.h"
  42. #include "bearer.h"
  43. #define MAX_IB_BEARERS MAX_BEARERS
  44. /**
  45. * struct ib_bearer - Infiniband bearer data structure
  46. * @bearer: ptr to associated "generic" bearer structure
  47. * @dev: ptr to associated Infiniband network device
  48. * @tipc_packet_type: used in binding TIPC to Infiniband driver
  49. * @cleanup: work item used when disabling bearer
  50. */
  51. struct ib_bearer {
  52. struct tipc_bearer *bearer;
  53. struct net_device *dev;
  54. struct packet_type tipc_packet_type;
  55. struct work_struct setup;
  56. struct work_struct cleanup;
  57. };
  58. static struct tipc_media ib_media_info;
  59. static struct ib_bearer ib_bearers[MAX_IB_BEARERS];
  60. static int ib_started;
  61. /**
  62. * ib_media_addr_set - initialize Infiniband media address structure
  63. *
  64. * Media-dependent "value" field stores MAC address in first 6 bytes
  65. * and zeroes out the remaining bytes.
  66. */
  67. static void ib_media_addr_set(const struct tipc_bearer *tb_ptr,
  68. struct tipc_media_addr *a, char *mac)
  69. {
  70. BUILD_BUG_ON(sizeof(a->value) < INFINIBAND_ALEN);
  71. memcpy(a->value, mac, INFINIBAND_ALEN);
  72. a->media_id = TIPC_MEDIA_TYPE_IB;
  73. a->broadcast = !memcmp(mac, tb_ptr->bcast_addr.value, INFINIBAND_ALEN);
  74. }
  75. /**
  76. * send_msg - send a TIPC message out over an InfiniBand interface
  77. */
  78. static int send_msg(struct sk_buff *buf, struct tipc_bearer *tb_ptr,
  79. struct tipc_media_addr *dest)
  80. {
  81. struct sk_buff *clone;
  82. struct net_device *dev;
  83. int delta;
  84. clone = skb_clone(buf, GFP_ATOMIC);
  85. if (!clone)
  86. return 0;
  87. dev = ((struct ib_bearer *)(tb_ptr->usr_handle))->dev;
  88. delta = dev->hard_header_len - skb_headroom(buf);
  89. if ((delta > 0) &&
  90. pskb_expand_head(clone, SKB_DATA_ALIGN(delta), 0, GFP_ATOMIC)) {
  91. kfree_skb(clone);
  92. return 0;
  93. }
  94. skb_reset_network_header(clone);
  95. clone->dev = dev;
  96. clone->protocol = htons(ETH_P_TIPC);
  97. dev_hard_header(clone, dev, ETH_P_TIPC, dest->value,
  98. dev->dev_addr, clone->len);
  99. dev_queue_xmit(clone);
  100. return 0;
  101. }
  102. /**
  103. * recv_msg - handle incoming TIPC message from an InfiniBand interface
  104. *
  105. * Accept only packets explicitly sent to this node, or broadcast packets;
  106. * ignores packets sent using InfiniBand multicast, and traffic sent to other
  107. * nodes (which can happen if interface is running in promiscuous mode).
  108. */
  109. static int recv_msg(struct sk_buff *buf, struct net_device *dev,
  110. struct packet_type *pt, struct net_device *orig_dev)
  111. {
  112. struct ib_bearer *ib_ptr = (struct ib_bearer *)pt->af_packet_priv;
  113. if (!net_eq(dev_net(dev), &init_net)) {
  114. kfree_skb(buf);
  115. return 0;
  116. }
  117. if (likely(ib_ptr->bearer)) {
  118. if (likely(buf->pkt_type <= PACKET_BROADCAST)) {
  119. buf->next = NULL;
  120. tipc_recv_msg(buf, ib_ptr->bearer);
  121. return 0;
  122. }
  123. }
  124. kfree_skb(buf);
  125. return 0;
  126. }
  127. /**
  128. * setup_bearer - setup association between InfiniBand bearer and interface
  129. */
  130. static void setup_bearer(struct work_struct *work)
  131. {
  132. struct ib_bearer *ib_ptr =
  133. container_of(work, struct ib_bearer, setup);
  134. dev_add_pack(&ib_ptr->tipc_packet_type);
  135. }
  136. /**
  137. * enable_bearer - attach TIPC bearer to an InfiniBand interface
  138. */
  139. static int enable_bearer(struct tipc_bearer *tb_ptr)
  140. {
  141. struct net_device *dev;
  142. struct ib_bearer *ib_ptr = &ib_bearers[0];
  143. struct ib_bearer *stop = &ib_bearers[MAX_IB_BEARERS];
  144. char *driver_name = strchr((const char *)tb_ptr->name, ':') + 1;
  145. int pending_dev = 0;
  146. /* Find unused InfiniBand bearer structure */
  147. while (ib_ptr->dev) {
  148. if (!ib_ptr->bearer)
  149. pending_dev++;
  150. if (++ib_ptr == stop)
  151. return pending_dev ? -EAGAIN : -EDQUOT;
  152. }
  153. /* Find device with specified name */
  154. dev = dev_get_by_name(&init_net, driver_name);
  155. if (!dev)
  156. return -ENODEV;
  157. /* Create InfiniBand bearer for device */
  158. ib_ptr->dev = dev;
  159. ib_ptr->tipc_packet_type.type = htons(ETH_P_TIPC);
  160. ib_ptr->tipc_packet_type.dev = dev;
  161. ib_ptr->tipc_packet_type.func = recv_msg;
  162. ib_ptr->tipc_packet_type.af_packet_priv = ib_ptr;
  163. INIT_LIST_HEAD(&(ib_ptr->tipc_packet_type.list));
  164. INIT_WORK(&ib_ptr->setup, setup_bearer);
  165. schedule_work(&ib_ptr->setup);
  166. /* Associate TIPC bearer with InfiniBand bearer */
  167. ib_ptr->bearer = tb_ptr;
  168. tb_ptr->usr_handle = (void *)ib_ptr;
  169. memset(tb_ptr->bcast_addr.value, 0, sizeof(tb_ptr->bcast_addr.value));
  170. memcpy(tb_ptr->bcast_addr.value, dev->broadcast, INFINIBAND_ALEN);
  171. tb_ptr->bcast_addr.media_id = TIPC_MEDIA_TYPE_IB;
  172. tb_ptr->bcast_addr.broadcast = 1;
  173. tb_ptr->mtu = dev->mtu;
  174. tb_ptr->blocked = 0;
  175. ib_media_addr_set(tb_ptr, &tb_ptr->addr, (char *)dev->dev_addr);
  176. return 0;
  177. }
  178. /**
  179. * cleanup_bearer - break association between InfiniBand bearer and interface
  180. *
  181. * This routine must be invoked from a work queue because it can sleep.
  182. */
  183. static void cleanup_bearer(struct work_struct *work)
  184. {
  185. struct ib_bearer *ib_ptr =
  186. container_of(work, struct ib_bearer, cleanup);
  187. dev_remove_pack(&ib_ptr->tipc_packet_type);
  188. dev_put(ib_ptr->dev);
  189. ib_ptr->dev = NULL;
  190. }
  191. /**
  192. * disable_bearer - detach TIPC bearer from an InfiniBand interface
  193. *
  194. * Mark InfiniBand bearer as inactive so that incoming buffers are thrown away,
  195. * then get worker thread to complete bearer cleanup. (Can't do cleanup
  196. * here because cleanup code needs to sleep and caller holds spinlocks.)
  197. */
  198. static void disable_bearer(struct tipc_bearer *tb_ptr)
  199. {
  200. struct ib_bearer *ib_ptr = (struct ib_bearer *)tb_ptr->usr_handle;
  201. ib_ptr->bearer = NULL;
  202. INIT_WORK(&ib_ptr->cleanup, cleanup_bearer);
  203. schedule_work(&ib_ptr->cleanup);
  204. }
  205. /**
  206. * recv_notification - handle device updates from OS
  207. *
  208. * Change the state of the InfiniBand bearer (if any) associated with the
  209. * specified device.
  210. */
  211. static int recv_notification(struct notifier_block *nb, unsigned long evt,
  212. void *ptr)
  213. {
  214. struct net_device *dev = netdev_notifier_info_to_dev(ptr);
  215. struct ib_bearer *ib_ptr = &ib_bearers[0];
  216. struct ib_bearer *stop = &ib_bearers[MAX_IB_BEARERS];
  217. if (!net_eq(dev_net(dev), &init_net))
  218. return NOTIFY_DONE;
  219. while ((ib_ptr->dev != dev)) {
  220. if (++ib_ptr == stop)
  221. return NOTIFY_DONE; /* couldn't find device */
  222. }
  223. if (!ib_ptr->bearer)
  224. return NOTIFY_DONE; /* bearer had been disabled */
  225. ib_ptr->bearer->mtu = dev->mtu;
  226. switch (evt) {
  227. case NETDEV_CHANGE:
  228. if (netif_carrier_ok(dev))
  229. tipc_continue(ib_ptr->bearer);
  230. else
  231. tipc_block_bearer(ib_ptr->bearer->name);
  232. break;
  233. case NETDEV_UP:
  234. tipc_continue(ib_ptr->bearer);
  235. break;
  236. case NETDEV_DOWN:
  237. tipc_block_bearer(ib_ptr->bearer->name);
  238. break;
  239. case NETDEV_CHANGEMTU:
  240. case NETDEV_CHANGEADDR:
  241. tipc_block_bearer(ib_ptr->bearer->name);
  242. tipc_continue(ib_ptr->bearer);
  243. break;
  244. case NETDEV_UNREGISTER:
  245. case NETDEV_CHANGENAME:
  246. tipc_disable_bearer(ib_ptr->bearer->name);
  247. break;
  248. }
  249. return NOTIFY_OK;
  250. }
  251. static struct notifier_block notifier = {
  252. .notifier_call = recv_notification,
  253. .priority = 0,
  254. };
  255. /**
  256. * ib_addr2str - convert InfiniBand address to string
  257. */
  258. static int ib_addr2str(struct tipc_media_addr *a, char *str_buf, int str_size)
  259. {
  260. if (str_size < 60) /* 60 = 19 * strlen("xx:") + strlen("xx\0") */
  261. return 1;
  262. sprintf(str_buf, "%20phC", a->value);
  263. return 0;
  264. }
  265. /**
  266. * ib_addr2msg - convert InfiniBand address format to message header format
  267. */
  268. static int ib_addr2msg(struct tipc_media_addr *a, char *msg_area)
  269. {
  270. memset(msg_area, 0, TIPC_MEDIA_ADDR_SIZE);
  271. msg_area[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_IB;
  272. memcpy(msg_area, a->value, INFINIBAND_ALEN);
  273. return 0;
  274. }
  275. /**
  276. * ib_msg2addr - convert message header address format to InfiniBand format
  277. */
  278. static int ib_msg2addr(const struct tipc_bearer *tb_ptr,
  279. struct tipc_media_addr *a, char *msg_area)
  280. {
  281. ib_media_addr_set(tb_ptr, a, msg_area);
  282. return 0;
  283. }
  284. /*
  285. * InfiniBand media registration info
  286. */
  287. static struct tipc_media ib_media_info = {
  288. .send_msg = send_msg,
  289. .enable_bearer = enable_bearer,
  290. .disable_bearer = disable_bearer,
  291. .addr2str = ib_addr2str,
  292. .addr2msg = ib_addr2msg,
  293. .msg2addr = ib_msg2addr,
  294. .priority = TIPC_DEF_LINK_PRI,
  295. .tolerance = TIPC_DEF_LINK_TOL,
  296. .window = TIPC_DEF_LINK_WIN,
  297. .type_id = TIPC_MEDIA_TYPE_IB,
  298. .name = "ib"
  299. };
  300. /**
  301. * tipc_ib_media_start - activate InfiniBand bearer support
  302. *
  303. * Register InfiniBand media type with TIPC bearer code. Also register
  304. * with OS for notifications about device state changes.
  305. */
  306. int tipc_ib_media_start(void)
  307. {
  308. int res;
  309. if (ib_started)
  310. return -EINVAL;
  311. res = tipc_register_media(&ib_media_info);
  312. if (res)
  313. return res;
  314. res = register_netdevice_notifier(&notifier);
  315. if (!res)
  316. ib_started = 1;
  317. return res;
  318. }
  319. /**
  320. * tipc_ib_media_stop - deactivate InfiniBand bearer support
  321. */
  322. void tipc_ib_media_stop(void)
  323. {
  324. if (!ib_started)
  325. return;
  326. flush_scheduled_work();
  327. unregister_netdevice_notifier(&notifier);
  328. ib_started = 0;
  329. }