ib_media.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 = NULL;
  142. struct net_device *pdev = NULL;
  143. struct ib_bearer *ib_ptr = &ib_bearers[0];
  144. struct ib_bearer *stop = &ib_bearers[MAX_IB_BEARERS];
  145. char *driver_name = strchr((const char *)tb_ptr->name, ':') + 1;
  146. int pending_dev = 0;
  147. /* Find unused InfiniBand bearer structure */
  148. while (ib_ptr->dev) {
  149. if (!ib_ptr->bearer)
  150. pending_dev++;
  151. if (++ib_ptr == stop)
  152. return pending_dev ? -EAGAIN : -EDQUOT;
  153. }
  154. /* Find device with specified name */
  155. read_lock(&dev_base_lock);
  156. for_each_netdev(&init_net, pdev) {
  157. if (!strncmp(pdev->name, driver_name, IFNAMSIZ)) {
  158. dev = pdev;
  159. dev_hold(dev);
  160. break;
  161. }
  162. }
  163. read_unlock(&dev_base_lock);
  164. if (!dev)
  165. return -ENODEV;
  166. /* Create InfiniBand bearer for device */
  167. ib_ptr->dev = dev;
  168. ib_ptr->tipc_packet_type.type = htons(ETH_P_TIPC);
  169. ib_ptr->tipc_packet_type.dev = dev;
  170. ib_ptr->tipc_packet_type.func = recv_msg;
  171. ib_ptr->tipc_packet_type.af_packet_priv = ib_ptr;
  172. INIT_LIST_HEAD(&(ib_ptr->tipc_packet_type.list));
  173. INIT_WORK(&ib_ptr->setup, setup_bearer);
  174. schedule_work(&ib_ptr->setup);
  175. /* Associate TIPC bearer with InfiniBand bearer */
  176. ib_ptr->bearer = tb_ptr;
  177. tb_ptr->usr_handle = (void *)ib_ptr;
  178. memset(tb_ptr->bcast_addr.value, 0, sizeof(tb_ptr->bcast_addr.value));
  179. memcpy(tb_ptr->bcast_addr.value, dev->broadcast, INFINIBAND_ALEN);
  180. tb_ptr->bcast_addr.media_id = TIPC_MEDIA_TYPE_IB;
  181. tb_ptr->bcast_addr.broadcast = 1;
  182. tb_ptr->mtu = dev->mtu;
  183. tb_ptr->blocked = 0;
  184. ib_media_addr_set(tb_ptr, &tb_ptr->addr, (char *)dev->dev_addr);
  185. return 0;
  186. }
  187. /**
  188. * cleanup_bearer - break association between InfiniBand bearer and interface
  189. *
  190. * This routine must be invoked from a work queue because it can sleep.
  191. */
  192. static void cleanup_bearer(struct work_struct *work)
  193. {
  194. struct ib_bearer *ib_ptr =
  195. container_of(work, struct ib_bearer, cleanup);
  196. dev_remove_pack(&ib_ptr->tipc_packet_type);
  197. dev_put(ib_ptr->dev);
  198. ib_ptr->dev = NULL;
  199. }
  200. /**
  201. * disable_bearer - detach TIPC bearer from an InfiniBand interface
  202. *
  203. * Mark InfiniBand bearer as inactive so that incoming buffers are thrown away,
  204. * then get worker thread to complete bearer cleanup. (Can't do cleanup
  205. * here because cleanup code needs to sleep and caller holds spinlocks.)
  206. */
  207. static void disable_bearer(struct tipc_bearer *tb_ptr)
  208. {
  209. struct ib_bearer *ib_ptr = (struct ib_bearer *)tb_ptr->usr_handle;
  210. ib_ptr->bearer = NULL;
  211. INIT_WORK(&ib_ptr->cleanup, cleanup_bearer);
  212. schedule_work(&ib_ptr->cleanup);
  213. }
  214. /**
  215. * recv_notification - handle device updates from OS
  216. *
  217. * Change the state of the InfiniBand bearer (if any) associated with the
  218. * specified device.
  219. */
  220. static int recv_notification(struct notifier_block *nb, unsigned long evt,
  221. void *dv)
  222. {
  223. struct net_device *dev = (struct net_device *)dv;
  224. struct ib_bearer *ib_ptr = &ib_bearers[0];
  225. struct ib_bearer *stop = &ib_bearers[MAX_IB_BEARERS];
  226. if (!net_eq(dev_net(dev), &init_net))
  227. return NOTIFY_DONE;
  228. while ((ib_ptr->dev != dev)) {
  229. if (++ib_ptr == stop)
  230. return NOTIFY_DONE; /* couldn't find device */
  231. }
  232. if (!ib_ptr->bearer)
  233. return NOTIFY_DONE; /* bearer had been disabled */
  234. ib_ptr->bearer->mtu = dev->mtu;
  235. switch (evt) {
  236. case NETDEV_CHANGE:
  237. if (netif_carrier_ok(dev))
  238. tipc_continue(ib_ptr->bearer);
  239. else
  240. tipc_block_bearer(ib_ptr->bearer->name);
  241. break;
  242. case NETDEV_UP:
  243. tipc_continue(ib_ptr->bearer);
  244. break;
  245. case NETDEV_DOWN:
  246. tipc_block_bearer(ib_ptr->bearer->name);
  247. break;
  248. case NETDEV_CHANGEMTU:
  249. case NETDEV_CHANGEADDR:
  250. tipc_block_bearer(ib_ptr->bearer->name);
  251. tipc_continue(ib_ptr->bearer);
  252. break;
  253. case NETDEV_UNREGISTER:
  254. case NETDEV_CHANGENAME:
  255. tipc_disable_bearer(ib_ptr->bearer->name);
  256. break;
  257. }
  258. return NOTIFY_OK;
  259. }
  260. static struct notifier_block notifier = {
  261. .notifier_call = recv_notification,
  262. .priority = 0,
  263. };
  264. /**
  265. * ib_addr2str - convert InfiniBand address to string
  266. */
  267. static int ib_addr2str(struct tipc_media_addr *a, char *str_buf, int str_size)
  268. {
  269. if (str_size < 60) /* 60 = 19 * strlen("xx:") + strlen("xx\0") */
  270. return 1;
  271. sprintf(str_buf, "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:"
  272. "%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x",
  273. a->value[0], a->value[1], a->value[2], a->value[3],
  274. a->value[4], a->value[5], a->value[6], a->value[7],
  275. a->value[8], a->value[9], a->value[10], a->value[11],
  276. a->value[12], a->value[13], a->value[14], a->value[15],
  277. a->value[16], a->value[17], a->value[18], a->value[19]);
  278. return 0;
  279. }
  280. /**
  281. * ib_addr2msg - convert InfiniBand address format to message header format
  282. */
  283. static int ib_addr2msg(struct tipc_media_addr *a, char *msg_area)
  284. {
  285. memset(msg_area, 0, TIPC_MEDIA_ADDR_SIZE);
  286. msg_area[TIPC_MEDIA_TYPE_OFFSET] = TIPC_MEDIA_TYPE_IB;
  287. memcpy(msg_area, a->value, INFINIBAND_ALEN);
  288. return 0;
  289. }
  290. /**
  291. * ib_msg2addr - convert message header address format to InfiniBand format
  292. */
  293. static int ib_msg2addr(const struct tipc_bearer *tb_ptr,
  294. struct tipc_media_addr *a, char *msg_area)
  295. {
  296. ib_media_addr_set(tb_ptr, a, msg_area);
  297. return 0;
  298. }
  299. /*
  300. * InfiniBand media registration info
  301. */
  302. static struct tipc_media ib_media_info = {
  303. .send_msg = send_msg,
  304. .enable_bearer = enable_bearer,
  305. .disable_bearer = disable_bearer,
  306. .addr2str = ib_addr2str,
  307. .addr2msg = ib_addr2msg,
  308. .msg2addr = ib_msg2addr,
  309. .priority = TIPC_DEF_LINK_PRI,
  310. .tolerance = TIPC_DEF_LINK_TOL,
  311. .window = TIPC_DEF_LINK_WIN,
  312. .type_id = TIPC_MEDIA_TYPE_IB,
  313. .name = "ib"
  314. };
  315. /**
  316. * tipc_ib_media_start - activate InfiniBand bearer support
  317. *
  318. * Register InfiniBand media type with TIPC bearer code. Also register
  319. * with OS for notifications about device state changes.
  320. */
  321. int tipc_ib_media_start(void)
  322. {
  323. int res;
  324. if (ib_started)
  325. return -EINVAL;
  326. res = tipc_register_media(&ib_media_info);
  327. if (res)
  328. return res;
  329. res = register_netdevice_notifier(&notifier);
  330. if (!res)
  331. ib_started = 1;
  332. return res;
  333. }
  334. /**
  335. * tipc_ib_media_stop - deactivate InfiniBand bearer support
  336. */
  337. void tipc_ib_media_stop(void)
  338. {
  339. if (!ib_started)
  340. return;
  341. flush_scheduled_work();
  342. unregister_netdevice_notifier(&notifier);
  343. ib_started = 0;
  344. }