/drivers/infiniband/ulp/ipoib/ipoib_multicast.c

https://bitbucket.org/evzijst/gittest · C · 991 lines · 726 code · 195 blank · 70 comment · 99 complexity · 112c47de3ed3d3668b9be27279386f59 MD5 · raw file

  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. * $Id: ipoib_multicast.c 1362 2004-12-18 15:56:29Z roland $
  33. */
  34. #include <linux/skbuff.h>
  35. #include <linux/rtnetlink.h>
  36. #include <linux/ip.h>
  37. #include <linux/in.h>
  38. #include <linux/igmp.h>
  39. #include <linux/inetdevice.h>
  40. #include <linux/delay.h>
  41. #include <linux/completion.h>
  42. #include "ipoib.h"
  43. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  44. static int mcast_debug_level;
  45. module_param(mcast_debug_level, int, 0644);
  46. MODULE_PARM_DESC(mcast_debug_level,
  47. "Enable multicast debug tracing if > 0");
  48. #endif
  49. static DECLARE_MUTEX(mcast_mutex);
  50. /* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */
  51. struct ipoib_mcast {
  52. struct ib_sa_mcmember_rec mcmember;
  53. struct ipoib_ah *ah;
  54. struct rb_node rb_node;
  55. struct list_head list;
  56. struct completion done;
  57. int query_id;
  58. struct ib_sa_query *query;
  59. unsigned long created;
  60. unsigned long backoff;
  61. unsigned long flags;
  62. unsigned char logcount;
  63. struct list_head neigh_list;
  64. struct sk_buff_head pkt_queue;
  65. struct net_device *dev;
  66. };
  67. struct ipoib_mcast_iter {
  68. struct net_device *dev;
  69. union ib_gid mgid;
  70. unsigned long created;
  71. unsigned int queuelen;
  72. unsigned int complete;
  73. unsigned int send_only;
  74. };
  75. static void ipoib_mcast_free(struct ipoib_mcast *mcast)
  76. {
  77. struct net_device *dev = mcast->dev;
  78. struct ipoib_dev_priv *priv = netdev_priv(dev);
  79. struct ipoib_neigh *neigh, *tmp;
  80. unsigned long flags;
  81. LIST_HEAD(ah_list);
  82. struct ipoib_ah *ah, *tah;
  83. ipoib_dbg_mcast(netdev_priv(dev),
  84. "deleting multicast group " IPOIB_GID_FMT "\n",
  85. IPOIB_GID_ARG(mcast->mcmember.mgid));
  86. spin_lock_irqsave(&priv->lock, flags);
  87. list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
  88. if (neigh->ah)
  89. list_add_tail(&neigh->ah->list, &ah_list);
  90. *to_ipoib_neigh(neigh->neighbour) = NULL;
  91. neigh->neighbour->ops->destructor = NULL;
  92. kfree(neigh);
  93. }
  94. spin_unlock_irqrestore(&priv->lock, flags);
  95. list_for_each_entry_safe(ah, tah, &ah_list, list)
  96. ipoib_put_ah(ah);
  97. if (mcast->ah)
  98. ipoib_put_ah(mcast->ah);
  99. while (!skb_queue_empty(&mcast->pkt_queue)) {
  100. struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
  101. skb->dev = dev;
  102. dev_kfree_skb_any(skb);
  103. }
  104. kfree(mcast);
  105. }
  106. static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
  107. int can_sleep)
  108. {
  109. struct ipoib_mcast *mcast;
  110. mcast = kmalloc(sizeof (*mcast), can_sleep ? GFP_KERNEL : GFP_ATOMIC);
  111. if (!mcast)
  112. return NULL;
  113. memset(mcast, 0, sizeof (*mcast));
  114. init_completion(&mcast->done);
  115. mcast->dev = dev;
  116. mcast->created = jiffies;
  117. mcast->backoff = HZ;
  118. mcast->logcount = 0;
  119. INIT_LIST_HEAD(&mcast->list);
  120. INIT_LIST_HEAD(&mcast->neigh_list);
  121. skb_queue_head_init(&mcast->pkt_queue);
  122. mcast->ah = NULL;
  123. mcast->query = NULL;
  124. return mcast;
  125. }
  126. static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, union ib_gid *mgid)
  127. {
  128. struct ipoib_dev_priv *priv = netdev_priv(dev);
  129. struct rb_node *n = priv->multicast_tree.rb_node;
  130. while (n) {
  131. struct ipoib_mcast *mcast;
  132. int ret;
  133. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  134. ret = memcmp(mgid->raw, mcast->mcmember.mgid.raw,
  135. sizeof (union ib_gid));
  136. if (ret < 0)
  137. n = n->rb_left;
  138. else if (ret > 0)
  139. n = n->rb_right;
  140. else
  141. return mcast;
  142. }
  143. return NULL;
  144. }
  145. static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
  146. {
  147. struct ipoib_dev_priv *priv = netdev_priv(dev);
  148. struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
  149. while (*n) {
  150. struct ipoib_mcast *tmcast;
  151. int ret;
  152. pn = *n;
  153. tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
  154. ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
  155. sizeof (union ib_gid));
  156. if (ret < 0)
  157. n = &pn->rb_left;
  158. else if (ret > 0)
  159. n = &pn->rb_right;
  160. else
  161. return -EEXIST;
  162. }
  163. rb_link_node(&mcast->rb_node, pn, n);
  164. rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
  165. return 0;
  166. }
  167. static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
  168. struct ib_sa_mcmember_rec *mcmember)
  169. {
  170. struct net_device *dev = mcast->dev;
  171. struct ipoib_dev_priv *priv = netdev_priv(dev);
  172. int ret;
  173. mcast->mcmember = *mcmember;
  174. /* Set the cached Q_Key before we attach if it's the broadcast group */
  175. if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  176. sizeof (union ib_gid))) {
  177. priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
  178. priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
  179. }
  180. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  181. if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  182. ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
  183. " already attached\n",
  184. IPOIB_GID_ARG(mcast->mcmember.mgid));
  185. return 0;
  186. }
  187. ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
  188. &mcast->mcmember.mgid);
  189. if (ret < 0) {
  190. ipoib_warn(priv, "couldn't attach QP to multicast group "
  191. IPOIB_GID_FMT "\n",
  192. IPOIB_GID_ARG(mcast->mcmember.mgid));
  193. clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
  194. return ret;
  195. }
  196. }
  197. {
  198. struct ib_ah_attr av = {
  199. .dlid = be16_to_cpu(mcast->mcmember.mlid),
  200. .port_num = priv->port,
  201. .sl = mcast->mcmember.sl,
  202. .ah_flags = IB_AH_GRH,
  203. .grh = {
  204. .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
  205. .hop_limit = mcast->mcmember.hop_limit,
  206. .sgid_index = 0,
  207. .traffic_class = mcast->mcmember.traffic_class
  208. }
  209. };
  210. av.grh.dgid = mcast->mcmember.mgid;
  211. if (ib_sa_rate_enum_to_int(mcast->mcmember.rate) > 0)
  212. av.static_rate = (2 * priv->local_rate -
  213. ib_sa_rate_enum_to_int(mcast->mcmember.rate) - 1) /
  214. (priv->local_rate ? priv->local_rate : 1);
  215. ipoib_dbg_mcast(priv, "static_rate %d for local port %dX, mcmember %dX\n",
  216. av.static_rate, priv->local_rate,
  217. ib_sa_rate_enum_to_int(mcast->mcmember.rate));
  218. mcast->ah = ipoib_create_ah(dev, priv->pd, &av);
  219. if (!mcast->ah) {
  220. ipoib_warn(priv, "ib_address_create failed\n");
  221. } else {
  222. ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
  223. " AV %p, LID 0x%04x, SL %d\n",
  224. IPOIB_GID_ARG(mcast->mcmember.mgid),
  225. mcast->ah->ah,
  226. be16_to_cpu(mcast->mcmember.mlid),
  227. mcast->mcmember.sl);
  228. }
  229. }
  230. /* actually send any queued packets */
  231. while (!skb_queue_empty(&mcast->pkt_queue)) {
  232. struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
  233. skb->dev = dev;
  234. if (!skb->dst || !skb->dst->neighbour) {
  235. /* put pseudoheader back on for next time */
  236. skb_push(skb, sizeof (struct ipoib_pseudoheader));
  237. }
  238. if (dev_queue_xmit(skb))
  239. ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
  240. }
  241. return 0;
  242. }
  243. static void
  244. ipoib_mcast_sendonly_join_complete(int status,
  245. struct ib_sa_mcmember_rec *mcmember,
  246. void *mcast_ptr)
  247. {
  248. struct ipoib_mcast *mcast = mcast_ptr;
  249. struct net_device *dev = mcast->dev;
  250. if (!status)
  251. ipoib_mcast_join_finish(mcast, mcmember);
  252. else {
  253. if (mcast->logcount++ < 20)
  254. ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
  255. IPOIB_GID_FMT ", status %d\n",
  256. IPOIB_GID_ARG(mcast->mcmember.mgid), status);
  257. /* Flush out any queued packets */
  258. while (!skb_queue_empty(&mcast->pkt_queue)) {
  259. struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
  260. skb->dev = dev;
  261. dev_kfree_skb_any(skb);
  262. }
  263. /* Clear the busy flag so we try again */
  264. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  265. }
  266. complete(&mcast->done);
  267. }
  268. static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
  269. {
  270. struct net_device *dev = mcast->dev;
  271. struct ipoib_dev_priv *priv = netdev_priv(dev);
  272. struct ib_sa_mcmember_rec rec = {
  273. #if 0 /* Some SMs don't support send-only yet */
  274. .join_state = 4
  275. #else
  276. .join_state = 1
  277. #endif
  278. };
  279. int ret = 0;
  280. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  281. ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
  282. return -ENODEV;
  283. }
  284. if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
  285. ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
  286. return -EBUSY;
  287. }
  288. rec.mgid = mcast->mcmember.mgid;
  289. rec.port_gid = priv->local_gid;
  290. rec.pkey = be16_to_cpu(priv->pkey);
  291. ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec,
  292. IB_SA_MCMEMBER_REC_MGID |
  293. IB_SA_MCMEMBER_REC_PORT_GID |
  294. IB_SA_MCMEMBER_REC_PKEY |
  295. IB_SA_MCMEMBER_REC_JOIN_STATE,
  296. 1000, GFP_ATOMIC,
  297. ipoib_mcast_sendonly_join_complete,
  298. mcast, &mcast->query);
  299. if (ret < 0) {
  300. ipoib_warn(priv, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
  301. ret);
  302. } else {
  303. ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
  304. ", starting join\n",
  305. IPOIB_GID_ARG(mcast->mcmember.mgid));
  306. mcast->query_id = ret;
  307. }
  308. return ret;
  309. }
  310. static void ipoib_mcast_join_complete(int status,
  311. struct ib_sa_mcmember_rec *mcmember,
  312. void *mcast_ptr)
  313. {
  314. struct ipoib_mcast *mcast = mcast_ptr;
  315. struct net_device *dev = mcast->dev;
  316. struct ipoib_dev_priv *priv = netdev_priv(dev);
  317. ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
  318. " (status %d)\n",
  319. IPOIB_GID_ARG(mcast->mcmember.mgid), status);
  320. if (!status && !ipoib_mcast_join_finish(mcast, mcmember)) {
  321. mcast->backoff = HZ;
  322. down(&mcast_mutex);
  323. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  324. queue_work(ipoib_workqueue, &priv->mcast_task);
  325. up(&mcast_mutex);
  326. complete(&mcast->done);
  327. return;
  328. }
  329. if (status == -EINTR) {
  330. complete(&mcast->done);
  331. return;
  332. }
  333. if (status && mcast->logcount++ < 20) {
  334. if (status == -ETIMEDOUT || status == -EINTR) {
  335. ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
  336. ", status %d\n",
  337. IPOIB_GID_ARG(mcast->mcmember.mgid),
  338. status);
  339. } else {
  340. ipoib_warn(priv, "multicast join failed for "
  341. IPOIB_GID_FMT ", status %d\n",
  342. IPOIB_GID_ARG(mcast->mcmember.mgid),
  343. status);
  344. }
  345. }
  346. mcast->backoff *= 2;
  347. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  348. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  349. mcast->query = NULL;
  350. down(&mcast_mutex);
  351. if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) {
  352. if (status == -ETIMEDOUT)
  353. queue_work(ipoib_workqueue, &priv->mcast_task);
  354. else
  355. queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
  356. mcast->backoff * HZ);
  357. } else
  358. complete(&mcast->done);
  359. up(&mcast_mutex);
  360. return;
  361. }
  362. static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
  363. int create)
  364. {
  365. struct ipoib_dev_priv *priv = netdev_priv(dev);
  366. struct ib_sa_mcmember_rec rec = {
  367. .join_state = 1
  368. };
  369. ib_sa_comp_mask comp_mask;
  370. int ret = 0;
  371. ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
  372. IPOIB_GID_ARG(mcast->mcmember.mgid));
  373. rec.mgid = mcast->mcmember.mgid;
  374. rec.port_gid = priv->local_gid;
  375. rec.pkey = be16_to_cpu(priv->pkey);
  376. comp_mask =
  377. IB_SA_MCMEMBER_REC_MGID |
  378. IB_SA_MCMEMBER_REC_PORT_GID |
  379. IB_SA_MCMEMBER_REC_PKEY |
  380. IB_SA_MCMEMBER_REC_JOIN_STATE;
  381. if (create) {
  382. comp_mask |=
  383. IB_SA_MCMEMBER_REC_QKEY |
  384. IB_SA_MCMEMBER_REC_SL |
  385. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  386. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
  387. rec.qkey = priv->broadcast->mcmember.qkey;
  388. rec.sl = priv->broadcast->mcmember.sl;
  389. rec.flow_label = priv->broadcast->mcmember.flow_label;
  390. rec.traffic_class = priv->broadcast->mcmember.traffic_class;
  391. }
  392. ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec, comp_mask,
  393. mcast->backoff * 1000, GFP_ATOMIC,
  394. ipoib_mcast_join_complete,
  395. mcast, &mcast->query);
  396. if (ret < 0) {
  397. ipoib_warn(priv, "ib_sa_mcmember_rec_set failed, status %d\n", ret);
  398. mcast->backoff *= 2;
  399. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  400. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  401. down(&mcast_mutex);
  402. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  403. queue_delayed_work(ipoib_workqueue,
  404. &priv->mcast_task,
  405. mcast->backoff);
  406. up(&mcast_mutex);
  407. } else
  408. mcast->query_id = ret;
  409. }
  410. void ipoib_mcast_join_task(void *dev_ptr)
  411. {
  412. struct net_device *dev = dev_ptr;
  413. struct ipoib_dev_priv *priv = netdev_priv(dev);
  414. if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
  415. return;
  416. if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
  417. ipoib_warn(priv, "ib_gid_entry_get() failed\n");
  418. else
  419. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  420. {
  421. struct ib_port_attr attr;
  422. if (!ib_query_port(priv->ca, priv->port, &attr)) {
  423. priv->local_lid = attr.lid;
  424. priv->local_rate = attr.active_speed *
  425. ib_width_enum_to_int(attr.active_width);
  426. } else
  427. ipoib_warn(priv, "ib_query_port failed\n");
  428. }
  429. if (!priv->broadcast) {
  430. priv->broadcast = ipoib_mcast_alloc(dev, 1);
  431. if (!priv->broadcast) {
  432. ipoib_warn(priv, "failed to allocate broadcast group\n");
  433. down(&mcast_mutex);
  434. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  435. queue_delayed_work(ipoib_workqueue,
  436. &priv->mcast_task, HZ);
  437. up(&mcast_mutex);
  438. return;
  439. }
  440. memcpy(priv->broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  441. sizeof (union ib_gid));
  442. spin_lock_irq(&priv->lock);
  443. __ipoib_mcast_add(dev, priv->broadcast);
  444. spin_unlock_irq(&priv->lock);
  445. }
  446. if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  447. ipoib_mcast_join(dev, priv->broadcast, 0);
  448. return;
  449. }
  450. while (1) {
  451. struct ipoib_mcast *mcast = NULL;
  452. spin_lock_irq(&priv->lock);
  453. list_for_each_entry(mcast, &priv->multicast_list, list) {
  454. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
  455. && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
  456. && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  457. /* Found the next unjoined group */
  458. break;
  459. }
  460. }
  461. spin_unlock_irq(&priv->lock);
  462. if (&mcast->list == &priv->multicast_list) {
  463. /* All done */
  464. break;
  465. }
  466. ipoib_mcast_join(dev, mcast, 1);
  467. return;
  468. }
  469. priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
  470. IPOIB_ENCAP_LEN;
  471. dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
  472. ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
  473. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  474. netif_carrier_on(dev);
  475. }
  476. int ipoib_mcast_start_thread(struct net_device *dev)
  477. {
  478. struct ipoib_dev_priv *priv = netdev_priv(dev);
  479. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  480. down(&mcast_mutex);
  481. if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
  482. queue_work(ipoib_workqueue, &priv->mcast_task);
  483. up(&mcast_mutex);
  484. return 0;
  485. }
  486. int ipoib_mcast_stop_thread(struct net_device *dev)
  487. {
  488. struct ipoib_dev_priv *priv = netdev_priv(dev);
  489. struct ipoib_mcast *mcast;
  490. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  491. down(&mcast_mutex);
  492. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  493. cancel_delayed_work(&priv->mcast_task);
  494. up(&mcast_mutex);
  495. flush_workqueue(ipoib_workqueue);
  496. if (priv->broadcast && priv->broadcast->query) {
  497. ib_sa_cancel_query(priv->broadcast->query_id, priv->broadcast->query);
  498. priv->broadcast->query = NULL;
  499. ipoib_dbg_mcast(priv, "waiting for bcast\n");
  500. wait_for_completion(&priv->broadcast->done);
  501. }
  502. list_for_each_entry(mcast, &priv->multicast_list, list) {
  503. if (mcast->query) {
  504. ib_sa_cancel_query(mcast->query_id, mcast->query);
  505. mcast->query = NULL;
  506. ipoib_dbg_mcast(priv, "waiting for MGID " IPOIB_GID_FMT "\n",
  507. IPOIB_GID_ARG(mcast->mcmember.mgid));
  508. wait_for_completion(&mcast->done);
  509. }
  510. }
  511. return 0;
  512. }
  513. static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  514. {
  515. struct ipoib_dev_priv *priv = netdev_priv(dev);
  516. struct ib_sa_mcmember_rec rec = {
  517. .join_state = 1
  518. };
  519. int ret = 0;
  520. if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags))
  521. return 0;
  522. ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
  523. IPOIB_GID_ARG(mcast->mcmember.mgid));
  524. rec.mgid = mcast->mcmember.mgid;
  525. rec.port_gid = priv->local_gid;
  526. rec.pkey = be16_to_cpu(priv->pkey);
  527. /* Remove ourselves from the multicast group */
  528. ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
  529. &mcast->mcmember.mgid);
  530. if (ret)
  531. ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
  532. /*
  533. * Just make one shot at leaving and don't wait for a reply;
  534. * if we fail, too bad.
  535. */
  536. ret = ib_sa_mcmember_rec_delete(priv->ca, priv->port, &rec,
  537. IB_SA_MCMEMBER_REC_MGID |
  538. IB_SA_MCMEMBER_REC_PORT_GID |
  539. IB_SA_MCMEMBER_REC_PKEY |
  540. IB_SA_MCMEMBER_REC_JOIN_STATE,
  541. 0, GFP_ATOMIC, NULL,
  542. mcast, &mcast->query);
  543. if (ret < 0)
  544. ipoib_warn(priv, "ib_sa_mcmember_rec_delete failed "
  545. "for leave (result = %d)\n", ret);
  546. return 0;
  547. }
  548. void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid,
  549. struct sk_buff *skb)
  550. {
  551. struct ipoib_dev_priv *priv = netdev_priv(dev);
  552. struct ipoib_mcast *mcast;
  553. /*
  554. * We can only be called from ipoib_start_xmit, so we're
  555. * inside tx_lock -- no need to save/restore flags.
  556. */
  557. spin_lock(&priv->lock);
  558. mcast = __ipoib_mcast_find(dev, mgid);
  559. if (!mcast) {
  560. /* Let's create a new send only group now */
  561. ipoib_dbg_mcast(priv, "setting up send only multicast group for "
  562. IPOIB_GID_FMT "\n", IPOIB_GID_ARG(*mgid));
  563. mcast = ipoib_mcast_alloc(dev, 0);
  564. if (!mcast) {
  565. ipoib_warn(priv, "unable to allocate memory for "
  566. "multicast structure\n");
  567. dev_kfree_skb_any(skb);
  568. goto out;
  569. }
  570. set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
  571. mcast->mcmember.mgid = *mgid;
  572. __ipoib_mcast_add(dev, mcast);
  573. list_add_tail(&mcast->list, &priv->multicast_list);
  574. }
  575. if (!mcast->ah) {
  576. if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
  577. skb_queue_tail(&mcast->pkt_queue, skb);
  578. else
  579. dev_kfree_skb_any(skb);
  580. if (mcast->query)
  581. ipoib_dbg_mcast(priv, "no address vector, "
  582. "but multicast join already started\n");
  583. else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  584. ipoib_mcast_sendonly_join(mcast);
  585. /*
  586. * If lookup completes between here and out:, don't
  587. * want to send packet twice.
  588. */
  589. mcast = NULL;
  590. }
  591. out:
  592. if (mcast && mcast->ah) {
  593. if (skb->dst &&
  594. skb->dst->neighbour &&
  595. !*to_ipoib_neigh(skb->dst->neighbour)) {
  596. struct ipoib_neigh *neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
  597. if (neigh) {
  598. kref_get(&mcast->ah->ref);
  599. neigh->ah = mcast->ah;
  600. neigh->neighbour = skb->dst->neighbour;
  601. *to_ipoib_neigh(skb->dst->neighbour) = neigh;
  602. list_add_tail(&neigh->list, &mcast->neigh_list);
  603. }
  604. }
  605. ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
  606. }
  607. spin_unlock(&priv->lock);
  608. }
  609. void ipoib_mcast_dev_flush(struct net_device *dev)
  610. {
  611. struct ipoib_dev_priv *priv = netdev_priv(dev);
  612. LIST_HEAD(remove_list);
  613. struct ipoib_mcast *mcast, *tmcast, *nmcast;
  614. unsigned long flags;
  615. ipoib_dbg_mcast(priv, "flushing multicast list\n");
  616. spin_lock_irqsave(&priv->lock, flags);
  617. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  618. nmcast = ipoib_mcast_alloc(dev, 0);
  619. if (nmcast) {
  620. nmcast->flags =
  621. mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY);
  622. nmcast->mcmember.mgid = mcast->mcmember.mgid;
  623. /* Add the new group in before the to-be-destroyed group */
  624. list_add_tail(&nmcast->list, &mcast->list);
  625. list_del_init(&mcast->list);
  626. rb_replace_node(&mcast->rb_node, &nmcast->rb_node,
  627. &priv->multicast_tree);
  628. list_add_tail(&mcast->list, &remove_list);
  629. } else {
  630. ipoib_warn(priv, "could not reallocate multicast group "
  631. IPOIB_GID_FMT "\n",
  632. IPOIB_GID_ARG(mcast->mcmember.mgid));
  633. }
  634. }
  635. if (priv->broadcast) {
  636. nmcast = ipoib_mcast_alloc(dev, 0);
  637. if (nmcast) {
  638. nmcast->mcmember.mgid = priv->broadcast->mcmember.mgid;
  639. rb_replace_node(&priv->broadcast->rb_node,
  640. &nmcast->rb_node,
  641. &priv->multicast_tree);
  642. list_add_tail(&priv->broadcast->list, &remove_list);
  643. }
  644. priv->broadcast = nmcast;
  645. }
  646. spin_unlock_irqrestore(&priv->lock, flags);
  647. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  648. ipoib_mcast_leave(dev, mcast);
  649. ipoib_mcast_free(mcast);
  650. }
  651. }
  652. void ipoib_mcast_dev_down(struct net_device *dev)
  653. {
  654. struct ipoib_dev_priv *priv = netdev_priv(dev);
  655. unsigned long flags;
  656. /* Delete broadcast since it will be recreated */
  657. if (priv->broadcast) {
  658. ipoib_dbg_mcast(priv, "deleting broadcast group\n");
  659. spin_lock_irqsave(&priv->lock, flags);
  660. rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
  661. spin_unlock_irqrestore(&priv->lock, flags);
  662. ipoib_mcast_leave(dev, priv->broadcast);
  663. ipoib_mcast_free(priv->broadcast);
  664. priv->broadcast = NULL;
  665. }
  666. }
  667. void ipoib_mcast_restart_task(void *dev_ptr)
  668. {
  669. struct net_device *dev = dev_ptr;
  670. struct ipoib_dev_priv *priv = netdev_priv(dev);
  671. struct dev_mc_list *mclist;
  672. struct ipoib_mcast *mcast, *tmcast;
  673. LIST_HEAD(remove_list);
  674. unsigned long flags;
  675. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  676. ipoib_mcast_stop_thread(dev);
  677. spin_lock_irqsave(&priv->lock, flags);
  678. /*
  679. * Unfortunately, the networking core only gives us a list of all of
  680. * the multicast hardware addresses. We need to figure out which ones
  681. * are new and which ones have been removed
  682. */
  683. /* Clear out the found flag */
  684. list_for_each_entry(mcast, &priv->multicast_list, list)
  685. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  686. /* Mark all of the entries that are found or don't exist */
  687. for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
  688. union ib_gid mgid;
  689. memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
  690. /* Add in the P_Key */
  691. mgid.raw[4] = (priv->pkey >> 8) & 0xff;
  692. mgid.raw[5] = priv->pkey & 0xff;
  693. mcast = __ipoib_mcast_find(dev, &mgid);
  694. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  695. struct ipoib_mcast *nmcast;
  696. /* Not found or send-only group, let's add a new entry */
  697. ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
  698. IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
  699. nmcast = ipoib_mcast_alloc(dev, 0);
  700. if (!nmcast) {
  701. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  702. continue;
  703. }
  704. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  705. nmcast->mcmember.mgid = mgid;
  706. if (mcast) {
  707. /* Destroy the send only entry */
  708. list_del(&mcast->list);
  709. list_add_tail(&mcast->list, &remove_list);
  710. rb_replace_node(&mcast->rb_node,
  711. &nmcast->rb_node,
  712. &priv->multicast_tree);
  713. } else
  714. __ipoib_mcast_add(dev, nmcast);
  715. list_add_tail(&nmcast->list, &priv->multicast_list);
  716. }
  717. if (mcast)
  718. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  719. }
  720. /* Remove all of the entries don't exist anymore */
  721. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  722. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  723. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  724. ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
  725. IPOIB_GID_ARG(mcast->mcmember.mgid));
  726. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  727. /* Move to the remove list */
  728. list_del(&mcast->list);
  729. list_add_tail(&mcast->list, &remove_list);
  730. }
  731. }
  732. spin_unlock_irqrestore(&priv->lock, flags);
  733. /* We have to cancel outside of the spinlock */
  734. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  735. ipoib_mcast_leave(mcast->dev, mcast);
  736. ipoib_mcast_free(mcast);
  737. }
  738. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  739. ipoib_mcast_start_thread(dev);
  740. }
  741. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  742. {
  743. struct ipoib_mcast_iter *iter;
  744. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  745. if (!iter)
  746. return NULL;
  747. iter->dev = dev;
  748. memset(iter->mgid.raw, 0, sizeof iter->mgid);
  749. if (ipoib_mcast_iter_next(iter)) {
  750. ipoib_mcast_iter_free(iter);
  751. return NULL;
  752. }
  753. return iter;
  754. }
  755. void ipoib_mcast_iter_free(struct ipoib_mcast_iter *iter)
  756. {
  757. kfree(iter);
  758. }
  759. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  760. {
  761. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  762. struct rb_node *n;
  763. struct ipoib_mcast *mcast;
  764. int ret = 1;
  765. spin_lock_irq(&priv->lock);
  766. n = rb_first(&priv->multicast_tree);
  767. while (n) {
  768. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  769. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  770. sizeof (union ib_gid)) < 0) {
  771. iter->mgid = mcast->mcmember.mgid;
  772. iter->created = mcast->created;
  773. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  774. iter->complete = !!mcast->ah;
  775. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  776. ret = 0;
  777. break;
  778. }
  779. n = rb_next(n);
  780. }
  781. spin_unlock_irq(&priv->lock);
  782. return ret;
  783. }
  784. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  785. union ib_gid *mgid,
  786. unsigned long *created,
  787. unsigned int *queuelen,
  788. unsigned int *complete,
  789. unsigned int *send_only)
  790. {
  791. *mgid = iter->mgid;
  792. *created = iter->created;
  793. *queuelen = iter->queuelen;
  794. *complete = iter->complete;
  795. *send_only = iter->send_only;
  796. }