ipoib_multicast.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  4. * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/skbuff.h>
  35. #include <linux/rtnetlink.h>
  36. #include <linux/moduleparam.h>
  37. #include <linux/ip.h>
  38. #include <linux/in.h>
  39. #include <linux/igmp.h>
  40. #include <linux/inetdevice.h>
  41. #include <linux/delay.h>
  42. #include <linux/completion.h>
  43. #include <linux/slab.h>
  44. #include <net/dst.h>
  45. #include "ipoib.h"
  46. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  47. static int mcast_debug_level;
  48. module_param(mcast_debug_level, int, 0644);
  49. MODULE_PARM_DESC(mcast_debug_level,
  50. "Enable multicast debug tracing if > 0");
  51. #endif
  52. struct ipoib_mcast_iter {
  53. struct net_device *dev;
  54. union ib_gid mgid;
  55. unsigned long created;
  56. unsigned int queuelen;
  57. unsigned int complete;
  58. unsigned int send_only;
  59. };
  60. /* join state that allows creating mcg with sendonly member request */
  61. #define SENDONLY_FULLMEMBER_JOIN 8
  62. /*
  63. * This should be called with the priv->lock held
  64. */
  65. static void __ipoib_mcast_schedule_join_thread(struct ipoib_dev_priv *priv,
  66. struct ipoib_mcast *mcast,
  67. bool delay)
  68. {
  69. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  70. return;
  71. /*
  72. * We will be scheduling *something*, so cancel whatever is
  73. * currently scheduled first
  74. */
  75. cancel_delayed_work(&priv->mcast_task);
  76. if (mcast && delay) {
  77. /*
  78. * We had a failure and want to schedule a retry later
  79. */
  80. mcast->backoff *= 2;
  81. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  82. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  83. mcast->delay_until = jiffies + (mcast->backoff * HZ);
  84. /*
  85. * Mark this mcast for its delay, but restart the
  86. * task immediately. The join task will make sure to
  87. * clear out all entries without delays, and then
  88. * schedule itself to run again when the earliest
  89. * delay expires
  90. */
  91. queue_delayed_work(priv->wq, &priv->mcast_task, 0);
  92. } else if (delay) {
  93. /*
  94. * Special case of retrying after a failure to
  95. * allocate the broadcast multicast group, wait
  96. * 1 second and try again
  97. */
  98. queue_delayed_work(priv->wq, &priv->mcast_task, HZ);
  99. } else
  100. queue_delayed_work(priv->wq, &priv->mcast_task, 0);
  101. }
  102. static void ipoib_mcast_free(struct ipoib_mcast *mcast)
  103. {
  104. struct net_device *dev = mcast->dev;
  105. int tx_dropped = 0;
  106. ipoib_dbg_mcast(ipoib_priv(dev), "deleting multicast group %pI6\n",
  107. mcast->mcmember.mgid.raw);
  108. /* remove all neigh connected to this mcast */
  109. ipoib_del_neighs_by_gid(dev, mcast->mcmember.mgid.raw);
  110. if (mcast->ah)
  111. ipoib_put_ah(mcast->ah);
  112. while (!skb_queue_empty(&mcast->pkt_queue)) {
  113. ++tx_dropped;
  114. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  115. }
  116. netif_tx_lock_bh(dev);
  117. dev->stats.tx_dropped += tx_dropped;
  118. netif_tx_unlock_bh(dev);
  119. kfree(mcast);
  120. }
  121. static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
  122. int can_sleep)
  123. {
  124. struct ipoib_mcast *mcast;
  125. mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
  126. if (!mcast)
  127. return NULL;
  128. mcast->dev = dev;
  129. mcast->created = jiffies;
  130. mcast->delay_until = jiffies;
  131. mcast->backoff = 1;
  132. INIT_LIST_HEAD(&mcast->list);
  133. INIT_LIST_HEAD(&mcast->neigh_list);
  134. skb_queue_head_init(&mcast->pkt_queue);
  135. return mcast;
  136. }
  137. static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
  138. {
  139. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  140. struct rb_node *n = priv->multicast_tree.rb_node;
  141. while (n) {
  142. struct ipoib_mcast *mcast;
  143. int ret;
  144. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  145. ret = memcmp(mgid, mcast->mcmember.mgid.raw,
  146. sizeof (union ib_gid));
  147. if (ret < 0)
  148. n = n->rb_left;
  149. else if (ret > 0)
  150. n = n->rb_right;
  151. else
  152. return mcast;
  153. }
  154. return NULL;
  155. }
  156. static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
  157. {
  158. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  159. struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
  160. while (*n) {
  161. struct ipoib_mcast *tmcast;
  162. int ret;
  163. pn = *n;
  164. tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
  165. ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
  166. sizeof (union ib_gid));
  167. if (ret < 0)
  168. n = &pn->rb_left;
  169. else if (ret > 0)
  170. n = &pn->rb_right;
  171. else
  172. return -EEXIST;
  173. }
  174. rb_link_node(&mcast->rb_node, pn, n);
  175. rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
  176. return 0;
  177. }
  178. static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
  179. struct ib_sa_mcmember_rec *mcmember)
  180. {
  181. struct net_device *dev = mcast->dev;
  182. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  183. struct rdma_netdev *rn = netdev_priv(dev);
  184. struct ipoib_ah *ah;
  185. struct rdma_ah_attr av;
  186. int ret;
  187. int set_qkey = 0;
  188. mcast->mcmember = *mcmember;
  189. /* Set the multicast MTU and cached Q_Key before we attach if it's
  190. * the broadcast group.
  191. */
  192. if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  193. sizeof (union ib_gid))) {
  194. spin_lock_irq(&priv->lock);
  195. if (!priv->broadcast) {
  196. spin_unlock_irq(&priv->lock);
  197. return -EAGAIN;
  198. }
  199. /*update priv member according to the new mcast*/
  200. priv->broadcast->mcmember.qkey = mcmember->qkey;
  201. priv->broadcast->mcmember.mtu = mcmember->mtu;
  202. priv->broadcast->mcmember.traffic_class = mcmember->traffic_class;
  203. priv->broadcast->mcmember.rate = mcmember->rate;
  204. priv->broadcast->mcmember.sl = mcmember->sl;
  205. priv->broadcast->mcmember.flow_label = mcmember->flow_label;
  206. priv->broadcast->mcmember.hop_limit = mcmember->hop_limit;
  207. /* assume if the admin and the mcast are the same both can be changed */
  208. if (priv->mcast_mtu == priv->admin_mtu)
  209. priv->admin_mtu =
  210. priv->mcast_mtu =
  211. IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  212. else
  213. priv->mcast_mtu =
  214. IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  215. priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
  216. spin_unlock_irq(&priv->lock);
  217. priv->tx_wr.remote_qkey = priv->qkey;
  218. set_qkey = 1;
  219. }
  220. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  221. if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  222. ipoib_warn(priv, "multicast group %pI6 already attached\n",
  223. mcast->mcmember.mgid.raw);
  224. return 0;
  225. }
  226. ret = rn->attach_mcast(dev, priv->ca, &mcast->mcmember.mgid,
  227. be16_to_cpu(mcast->mcmember.mlid),
  228. set_qkey, priv->qkey);
  229. if (ret < 0) {
  230. ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
  231. mcast->mcmember.mgid.raw);
  232. clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
  233. return ret;
  234. }
  235. }
  236. memset(&av, 0, sizeof(av));
  237. av.type = rdma_ah_find_type(priv->ca, priv->port);
  238. rdma_ah_set_dlid(&av, be16_to_cpu(mcast->mcmember.mlid)),
  239. rdma_ah_set_port_num(&av, priv->port);
  240. rdma_ah_set_sl(&av, mcast->mcmember.sl);
  241. rdma_ah_set_static_rate(&av, mcast->mcmember.rate);
  242. rdma_ah_set_grh(&av, &mcast->mcmember.mgid,
  243. be32_to_cpu(mcast->mcmember.flow_label),
  244. 0, mcast->mcmember.hop_limit,
  245. mcast->mcmember.traffic_class);
  246. ah = ipoib_create_ah(dev, priv->pd, &av);
  247. if (IS_ERR(ah)) {
  248. ipoib_warn(priv, "ib_address_create failed %ld\n",
  249. -PTR_ERR(ah));
  250. /* use original error */
  251. return PTR_ERR(ah);
  252. }
  253. spin_lock_irq(&priv->lock);
  254. mcast->ah = ah;
  255. spin_unlock_irq(&priv->lock);
  256. ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
  257. mcast->mcmember.mgid.raw,
  258. mcast->ah->ah,
  259. be16_to_cpu(mcast->mcmember.mlid),
  260. mcast->mcmember.sl);
  261. /* actually send any queued packets */
  262. netif_tx_lock_bh(dev);
  263. while (!skb_queue_empty(&mcast->pkt_queue)) {
  264. struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
  265. netif_tx_unlock_bh(dev);
  266. skb->dev = dev;
  267. ret = dev_queue_xmit(skb);
  268. if (ret)
  269. ipoib_warn(priv, "%s:dev_queue_xmit failed to re-queue packet, ret:%d\n",
  270. __func__, ret);
  271. netif_tx_lock_bh(dev);
  272. }
  273. netif_tx_unlock_bh(dev);
  274. return 0;
  275. }
  276. void ipoib_mcast_carrier_on_task(struct work_struct *work)
  277. {
  278. struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
  279. carrier_on_task);
  280. struct ib_port_attr attr;
  281. if (ib_query_port(priv->ca, priv->port, &attr) ||
  282. attr.state != IB_PORT_ACTIVE) {
  283. ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
  284. return;
  285. }
  286. /*
  287. * Check if can send sendonly MCG's with sendonly-fullmember join state.
  288. * It done here after the successfully join to the broadcast group,
  289. * because the broadcast group must always be joined first and is always
  290. * re-joined if the SM changes substantially.
  291. */
  292. priv->sm_fullmember_sendonly_support =
  293. ib_sa_sendonly_fullmem_support(&ipoib_sa_client,
  294. priv->ca, priv->port);
  295. /*
  296. * Take rtnl_lock to avoid racing with ipoib_stop() and
  297. * turning the carrier back on while a device is being
  298. * removed. However, ipoib_stop() will attempt to flush
  299. * the workqueue while holding the rtnl lock, so loop
  300. * on trylock until either we get the lock or we see
  301. * FLAG_OPER_UP go away as that signals that we are bailing
  302. * and can safely ignore the carrier on work.
  303. */
  304. while (!rtnl_trylock()) {
  305. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  306. return;
  307. else
  308. msleep(20);
  309. }
  310. if (!ipoib_cm_admin_enabled(priv->dev))
  311. dev_set_mtu(priv->dev, min(priv->mcast_mtu, priv->admin_mtu));
  312. netif_carrier_on(priv->dev);
  313. rtnl_unlock();
  314. }
  315. static int ipoib_mcast_join_complete(int status,
  316. struct ib_sa_multicast *multicast)
  317. {
  318. struct ipoib_mcast *mcast = multicast->context;
  319. struct net_device *dev = mcast->dev;
  320. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  321. ipoib_dbg_mcast(priv, "%sjoin completion for %pI6 (status %d)\n",
  322. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ?
  323. "sendonly " : "",
  324. mcast->mcmember.mgid.raw, status);
  325. /* We trap for port events ourselves. */
  326. if (status == -ENETRESET) {
  327. status = 0;
  328. goto out;
  329. }
  330. if (!status)
  331. status = ipoib_mcast_join_finish(mcast, &multicast->rec);
  332. if (!status) {
  333. mcast->backoff = 1;
  334. mcast->delay_until = jiffies;
  335. /*
  336. * Defer carrier on work to priv->wq to avoid a
  337. * deadlock on rtnl_lock here. Requeue our multicast
  338. * work too, which will end up happening right after
  339. * our carrier on task work and will allow us to
  340. * send out all of the non-broadcast joins
  341. */
  342. if (mcast == priv->broadcast) {
  343. spin_lock_irq(&priv->lock);
  344. queue_work(priv->wq, &priv->carrier_on_task);
  345. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  346. goto out_locked;
  347. }
  348. } else {
  349. bool silent_fail =
  350. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
  351. status == -EINVAL;
  352. if (mcast->logcount < 20) {
  353. if (status == -ETIMEDOUT || status == -EAGAIN ||
  354. silent_fail) {
  355. ipoib_dbg_mcast(priv, "%smulticast join failed for %pI6, status %d\n",
  356. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
  357. mcast->mcmember.mgid.raw, status);
  358. } else {
  359. ipoib_warn(priv, "%smulticast join failed for %pI6, status %d\n",
  360. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
  361. mcast->mcmember.mgid.raw, status);
  362. }
  363. if (!silent_fail)
  364. mcast->logcount++;
  365. }
  366. if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
  367. mcast->backoff >= 2) {
  368. /*
  369. * We only retry sendonly joins once before we drop
  370. * the packet and quit trying to deal with the
  371. * group. However, we leave the group in the
  372. * mcast list as an unjoined group. If we want to
  373. * try joining again, we simply queue up a packet
  374. * and restart the join thread. The empty queue
  375. * is why the join thread ignores this group.
  376. */
  377. mcast->backoff = 1;
  378. netif_tx_lock_bh(dev);
  379. while (!skb_queue_empty(&mcast->pkt_queue)) {
  380. ++dev->stats.tx_dropped;
  381. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  382. }
  383. netif_tx_unlock_bh(dev);
  384. } else {
  385. spin_lock_irq(&priv->lock);
  386. /* Requeue this join task with a backoff delay */
  387. __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
  388. goto out_locked;
  389. }
  390. }
  391. out:
  392. spin_lock_irq(&priv->lock);
  393. out_locked:
  394. /*
  395. * Make sure to set mcast->mc before we clear the busy flag to avoid
  396. * racing with code that checks for BUSY before checking mcast->mc
  397. */
  398. if (status)
  399. mcast->mc = NULL;
  400. else
  401. mcast->mc = multicast;
  402. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  403. spin_unlock_irq(&priv->lock);
  404. complete(&mcast->done);
  405. return status;
  406. }
  407. /*
  408. * Caller must hold 'priv->lock'
  409. */
  410. static int ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast)
  411. {
  412. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  413. struct ib_sa_multicast *multicast;
  414. struct ib_sa_mcmember_rec rec = {
  415. .join_state = 1
  416. };
  417. ib_sa_comp_mask comp_mask;
  418. int ret = 0;
  419. if (!priv->broadcast ||
  420. !test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  421. return -EINVAL;
  422. init_completion(&mcast->done);
  423. set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  424. ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
  425. rec.mgid = mcast->mcmember.mgid;
  426. rec.port_gid = priv->local_gid;
  427. rec.pkey = cpu_to_be16(priv->pkey);
  428. comp_mask =
  429. IB_SA_MCMEMBER_REC_MGID |
  430. IB_SA_MCMEMBER_REC_PORT_GID |
  431. IB_SA_MCMEMBER_REC_PKEY |
  432. IB_SA_MCMEMBER_REC_JOIN_STATE;
  433. if (mcast != priv->broadcast) {
  434. /*
  435. * RFC 4391:
  436. * The MGID MUST use the same P_Key, Q_Key, SL, MTU,
  437. * and HopLimit as those used in the broadcast-GID. The rest
  438. * of attributes SHOULD follow the values used in the
  439. * broadcast-GID as well.
  440. */
  441. comp_mask |=
  442. IB_SA_MCMEMBER_REC_QKEY |
  443. IB_SA_MCMEMBER_REC_MTU_SELECTOR |
  444. IB_SA_MCMEMBER_REC_MTU |
  445. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
  446. IB_SA_MCMEMBER_REC_RATE_SELECTOR |
  447. IB_SA_MCMEMBER_REC_RATE |
  448. IB_SA_MCMEMBER_REC_SL |
  449. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  450. IB_SA_MCMEMBER_REC_HOP_LIMIT;
  451. rec.qkey = priv->broadcast->mcmember.qkey;
  452. rec.mtu_selector = IB_SA_EQ;
  453. rec.mtu = priv->broadcast->mcmember.mtu;
  454. rec.traffic_class = priv->broadcast->mcmember.traffic_class;
  455. rec.rate_selector = IB_SA_EQ;
  456. rec.rate = priv->broadcast->mcmember.rate;
  457. rec.sl = priv->broadcast->mcmember.sl;
  458. rec.flow_label = priv->broadcast->mcmember.flow_label;
  459. rec.hop_limit = priv->broadcast->mcmember.hop_limit;
  460. /*
  461. * Send-only IB Multicast joins work at the core IB layer but
  462. * require specific SM support.
  463. * We can use such joins here only if the current SM supports that feature.
  464. * However, if not, we emulate an Ethernet multicast send,
  465. * which does not require a multicast subscription and will
  466. * still send properly. The most appropriate thing to
  467. * do is to create the group if it doesn't exist as that
  468. * most closely emulates the behavior, from a user space
  469. * application perspective, of Ethernet multicast operation.
  470. */
  471. if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
  472. priv->sm_fullmember_sendonly_support)
  473. /* SM supports sendonly-fullmember, otherwise fallback to full-member */
  474. rec.join_state = SENDONLY_FULLMEMBER_JOIN;
  475. }
  476. spin_unlock_irq(&priv->lock);
  477. multicast = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
  478. &rec, comp_mask, GFP_KERNEL,
  479. ipoib_mcast_join_complete, mcast);
  480. spin_lock_irq(&priv->lock);
  481. if (IS_ERR(multicast)) {
  482. ret = PTR_ERR(multicast);
  483. ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
  484. /* Requeue this join task with a backoff delay */
  485. __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
  486. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  487. spin_unlock_irq(&priv->lock);
  488. complete(&mcast->done);
  489. spin_lock_irq(&priv->lock);
  490. }
  491. return 0;
  492. }
  493. void ipoib_mcast_join_task(struct work_struct *work)
  494. {
  495. struct ipoib_dev_priv *priv =
  496. container_of(work, struct ipoib_dev_priv, mcast_task.work);
  497. struct net_device *dev = priv->dev;
  498. struct ib_port_attr port_attr;
  499. unsigned long delay_until = 0;
  500. struct ipoib_mcast *mcast = NULL;
  501. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  502. return;
  503. if (ib_query_port(priv->ca, priv->port, &port_attr)) {
  504. ipoib_dbg(priv, "ib_query_port() failed\n");
  505. return;
  506. }
  507. if (port_attr.state != IB_PORT_ACTIVE) {
  508. ipoib_dbg(priv, "port state is not ACTIVE (state = %d) suspending join task\n",
  509. port_attr.state);
  510. return;
  511. }
  512. priv->local_lid = port_attr.lid;
  513. netif_addr_lock_bh(dev);
  514. if (!test_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags)) {
  515. netif_addr_unlock_bh(dev);
  516. return;
  517. }
  518. netif_addr_unlock_bh(dev);
  519. spin_lock_irq(&priv->lock);
  520. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  521. goto out;
  522. if (!priv->broadcast) {
  523. struct ipoib_mcast *broadcast;
  524. broadcast = ipoib_mcast_alloc(dev, 0);
  525. if (!broadcast) {
  526. ipoib_warn(priv, "failed to allocate broadcast group\n");
  527. /*
  528. * Restart us after a 1 second delay to retry
  529. * creating our broadcast group and attaching to
  530. * it. Until this succeeds, this ipoib dev is
  531. * completely stalled (multicast wise).
  532. */
  533. __ipoib_mcast_schedule_join_thread(priv, NULL, 1);
  534. goto out;
  535. }
  536. memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  537. sizeof (union ib_gid));
  538. priv->broadcast = broadcast;
  539. __ipoib_mcast_add(dev, priv->broadcast);
  540. }
  541. if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  542. if (IS_ERR_OR_NULL(priv->broadcast->mc) &&
  543. !test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags)) {
  544. mcast = priv->broadcast;
  545. if (mcast->backoff > 1 &&
  546. time_before(jiffies, mcast->delay_until)) {
  547. delay_until = mcast->delay_until;
  548. mcast = NULL;
  549. }
  550. }
  551. goto out;
  552. }
  553. /*
  554. * We'll never get here until the broadcast group is both allocated
  555. * and attached
  556. */
  557. list_for_each_entry(mcast, &priv->multicast_list, list) {
  558. if (IS_ERR_OR_NULL(mcast->mc) &&
  559. !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags) &&
  560. (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ||
  561. !skb_queue_empty(&mcast->pkt_queue))) {
  562. if (mcast->backoff == 1 ||
  563. time_after_eq(jiffies, mcast->delay_until)) {
  564. /* Found the next unjoined group */
  565. if (ipoib_mcast_join(dev, mcast)) {
  566. spin_unlock_irq(&priv->lock);
  567. return;
  568. }
  569. } else if (!delay_until ||
  570. time_before(mcast->delay_until, delay_until))
  571. delay_until = mcast->delay_until;
  572. }
  573. }
  574. mcast = NULL;
  575. ipoib_dbg_mcast(priv, "successfully started all multicast joins\n");
  576. out:
  577. if (delay_until) {
  578. cancel_delayed_work(&priv->mcast_task);
  579. queue_delayed_work(priv->wq, &priv->mcast_task,
  580. delay_until - jiffies);
  581. }
  582. if (mcast)
  583. ipoib_mcast_join(dev, mcast);
  584. spin_unlock_irq(&priv->lock);
  585. }
  586. void ipoib_mcast_start_thread(struct net_device *dev)
  587. {
  588. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  589. unsigned long flags;
  590. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  591. spin_lock_irqsave(&priv->lock, flags);
  592. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  593. spin_unlock_irqrestore(&priv->lock, flags);
  594. }
  595. int ipoib_mcast_stop_thread(struct net_device *dev)
  596. {
  597. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  598. unsigned long flags;
  599. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  600. spin_lock_irqsave(&priv->lock, flags);
  601. cancel_delayed_work(&priv->mcast_task);
  602. spin_unlock_irqrestore(&priv->lock, flags);
  603. flush_workqueue(priv->wq);
  604. return 0;
  605. }
  606. static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  607. {
  608. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  609. struct rdma_netdev *rn = netdev_priv(dev);
  610. int ret = 0;
  611. if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  612. ipoib_warn(priv, "ipoib_mcast_leave on an in-flight join\n");
  613. if (!IS_ERR_OR_NULL(mcast->mc))
  614. ib_sa_free_multicast(mcast->mc);
  615. if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  616. ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
  617. mcast->mcmember.mgid.raw);
  618. /* Remove ourselves from the multicast group */
  619. ret = rn->detach_mcast(dev, priv->ca, &mcast->mcmember.mgid,
  620. be16_to_cpu(mcast->mcmember.mlid));
  621. if (ret)
  622. ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
  623. } else if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  624. ipoib_dbg(priv, "leaving with no mcmember but not a "
  625. "SENDONLY join\n");
  626. return 0;
  627. }
  628. /*
  629. * Check if the multicast group is sendonly. If so remove it from the maps
  630. * and add to the remove list
  631. */
  632. void ipoib_check_and_add_mcast_sendonly(struct ipoib_dev_priv *priv, u8 *mgid,
  633. struct list_head *remove_list)
  634. {
  635. /* Is this multicast ? */
  636. if (*mgid == 0xff) {
  637. struct ipoib_mcast *mcast = __ipoib_mcast_find(priv->dev, mgid);
  638. if (mcast && test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  639. list_del(&mcast->list);
  640. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  641. list_add_tail(&mcast->list, remove_list);
  642. }
  643. }
  644. }
  645. void ipoib_mcast_remove_list(struct list_head *remove_list)
  646. {
  647. struct ipoib_mcast *mcast, *tmcast;
  648. list_for_each_entry_safe(mcast, tmcast, remove_list, list) {
  649. ipoib_mcast_leave(mcast->dev, mcast);
  650. ipoib_mcast_free(mcast);
  651. }
  652. }
  653. void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
  654. {
  655. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  656. struct rdma_netdev *rn = netdev_priv(dev);
  657. struct ipoib_mcast *mcast;
  658. unsigned long flags;
  659. void *mgid = daddr + 4;
  660. spin_lock_irqsave(&priv->lock, flags);
  661. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
  662. !priv->broadcast ||
  663. !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  664. ++dev->stats.tx_dropped;
  665. dev_kfree_skb_any(skb);
  666. goto unlock;
  667. }
  668. mcast = __ipoib_mcast_find(dev, mgid);
  669. if (!mcast || !mcast->ah) {
  670. if (!mcast) {
  671. /* Let's create a new send only group now */
  672. ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
  673. mgid);
  674. mcast = ipoib_mcast_alloc(dev, 0);
  675. if (!mcast) {
  676. ipoib_warn(priv, "unable to allocate memory "
  677. "for multicast structure\n");
  678. ++dev->stats.tx_dropped;
  679. dev_kfree_skb_any(skb);
  680. goto unlock;
  681. }
  682. set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
  683. memcpy(mcast->mcmember.mgid.raw, mgid,
  684. sizeof (union ib_gid));
  685. __ipoib_mcast_add(dev, mcast);
  686. list_add_tail(&mcast->list, &priv->multicast_list);
  687. }
  688. if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE) {
  689. /* put pseudoheader back on for next time */
  690. skb_push(skb, sizeof(struct ipoib_pseudo_header));
  691. skb_queue_tail(&mcast->pkt_queue, skb);
  692. } else {
  693. ++dev->stats.tx_dropped;
  694. dev_kfree_skb_any(skb);
  695. }
  696. if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
  697. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  698. }
  699. } else {
  700. struct ipoib_neigh *neigh;
  701. spin_unlock_irqrestore(&priv->lock, flags);
  702. neigh = ipoib_neigh_get(dev, daddr);
  703. spin_lock_irqsave(&priv->lock, flags);
  704. if (!neigh) {
  705. neigh = ipoib_neigh_alloc(daddr, dev);
  706. if (neigh) {
  707. kref_get(&mcast->ah->ref);
  708. neigh->ah = mcast->ah;
  709. list_add_tail(&neigh->list, &mcast->neigh_list);
  710. }
  711. }
  712. spin_unlock_irqrestore(&priv->lock, flags);
  713. mcast->ah->last_send = rn->send(dev, skb, mcast->ah->ah,
  714. IB_MULTICAST_QPN);
  715. if (neigh)
  716. ipoib_neigh_put(neigh);
  717. return;
  718. }
  719. unlock:
  720. spin_unlock_irqrestore(&priv->lock, flags);
  721. }
  722. void ipoib_mcast_dev_flush(struct net_device *dev)
  723. {
  724. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  725. LIST_HEAD(remove_list);
  726. struct ipoib_mcast *mcast, *tmcast;
  727. unsigned long flags;
  728. ipoib_dbg_mcast(priv, "flushing multicast list\n");
  729. spin_lock_irqsave(&priv->lock, flags);
  730. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  731. list_del(&mcast->list);
  732. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  733. list_add_tail(&mcast->list, &remove_list);
  734. }
  735. if (priv->broadcast) {
  736. rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
  737. list_add_tail(&priv->broadcast->list, &remove_list);
  738. priv->broadcast = NULL;
  739. }
  740. spin_unlock_irqrestore(&priv->lock, flags);
  741. /*
  742. * make sure the in-flight joins have finished before we attempt
  743. * to leave
  744. */
  745. list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
  746. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  747. wait_for_completion(&mcast->done);
  748. ipoib_mcast_remove_list(&remove_list);
  749. }
  750. static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
  751. {
  752. /* reserved QPN, prefix, scope */
  753. if (memcmp(addr, broadcast, 6))
  754. return 0;
  755. /* signature lower, pkey */
  756. if (memcmp(addr + 7, broadcast + 7, 3))
  757. return 0;
  758. return 1;
  759. }
  760. void ipoib_mcast_restart_task(struct work_struct *work)
  761. {
  762. struct ipoib_dev_priv *priv =
  763. container_of(work, struct ipoib_dev_priv, restart_task);
  764. struct net_device *dev = priv->dev;
  765. struct netdev_hw_addr *ha;
  766. struct ipoib_mcast *mcast, *tmcast;
  767. LIST_HEAD(remove_list);
  768. unsigned long flags;
  769. struct ib_sa_mcmember_rec rec;
  770. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  771. /*
  772. * shortcut...on shutdown flush is called next, just
  773. * let it do all the work
  774. */
  775. return;
  776. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  777. local_irq_save(flags);
  778. netif_addr_lock(dev);
  779. spin_lock(&priv->lock);
  780. /*
  781. * Unfortunately, the networking core only gives us a list of all of
  782. * the multicast hardware addresses. We need to figure out which ones
  783. * are new and which ones have been removed
  784. */
  785. /* Clear out the found flag */
  786. list_for_each_entry(mcast, &priv->multicast_list, list)
  787. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  788. /* Mark all of the entries that are found or don't exist */
  789. netdev_for_each_mc_addr(ha, dev) {
  790. union ib_gid mgid;
  791. if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
  792. continue;
  793. memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
  794. mcast = __ipoib_mcast_find(dev, &mgid);
  795. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  796. struct ipoib_mcast *nmcast;
  797. /* ignore group which is directly joined by userspace */
  798. if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
  799. !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
  800. ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
  801. mgid.raw);
  802. continue;
  803. }
  804. /* Not found or send-only group, let's add a new entry */
  805. ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
  806. mgid.raw);
  807. nmcast = ipoib_mcast_alloc(dev, 0);
  808. if (!nmcast) {
  809. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  810. continue;
  811. }
  812. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  813. nmcast->mcmember.mgid = mgid;
  814. if (mcast) {
  815. /* Destroy the send only entry */
  816. list_move_tail(&mcast->list, &remove_list);
  817. rb_replace_node(&mcast->rb_node,
  818. &nmcast->rb_node,
  819. &priv->multicast_tree);
  820. } else
  821. __ipoib_mcast_add(dev, nmcast);
  822. list_add_tail(&nmcast->list, &priv->multicast_list);
  823. }
  824. if (mcast)
  825. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  826. }
  827. /* Remove all of the entries don't exist anymore */
  828. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  829. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  830. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  831. ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
  832. mcast->mcmember.mgid.raw);
  833. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  834. /* Move to the remove list */
  835. list_move_tail(&mcast->list, &remove_list);
  836. }
  837. }
  838. spin_unlock(&priv->lock);
  839. netif_addr_unlock(dev);
  840. local_irq_restore(flags);
  841. /*
  842. * make sure the in-flight joins have finished before we attempt
  843. * to leave
  844. */
  845. list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
  846. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  847. wait_for_completion(&mcast->done);
  848. ipoib_mcast_remove_list(&remove_list);
  849. /*
  850. * Double check that we are still up
  851. */
  852. if (test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  853. spin_lock_irqsave(&priv->lock, flags);
  854. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  855. spin_unlock_irqrestore(&priv->lock, flags);
  856. }
  857. }
  858. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  859. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  860. {
  861. struct ipoib_mcast_iter *iter;
  862. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  863. if (!iter)
  864. return NULL;
  865. iter->dev = dev;
  866. memset(iter->mgid.raw, 0, 16);
  867. if (ipoib_mcast_iter_next(iter)) {
  868. kfree(iter);
  869. return NULL;
  870. }
  871. return iter;
  872. }
  873. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  874. {
  875. struct ipoib_dev_priv *priv = ipoib_priv(iter->dev);
  876. struct rb_node *n;
  877. struct ipoib_mcast *mcast;
  878. int ret = 1;
  879. spin_lock_irq(&priv->lock);
  880. n = rb_first(&priv->multicast_tree);
  881. while (n) {
  882. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  883. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  884. sizeof (union ib_gid)) < 0) {
  885. iter->mgid = mcast->mcmember.mgid;
  886. iter->created = mcast->created;
  887. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  888. iter->complete = !!mcast->ah;
  889. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  890. ret = 0;
  891. break;
  892. }
  893. n = rb_next(n);
  894. }
  895. spin_unlock_irq(&priv->lock);
  896. return ret;
  897. }
  898. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  899. union ib_gid *mgid,
  900. unsigned long *created,
  901. unsigned int *queuelen,
  902. unsigned int *complete,
  903. unsigned int *send_only)
  904. {
  905. *mgid = iter->mgid;
  906. *created = iter->created;
  907. *queuelen = iter->queuelen;
  908. *complete = iter->complete;
  909. *send_only = iter->send_only;
  910. }
  911. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */