ipoib_multicast.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084
  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. int ret;
  186. int set_qkey = 0;
  187. mcast->mcmember = *mcmember;
  188. /* Set the multicast MTU and cached Q_Key before we attach if it's
  189. * the broadcast group.
  190. */
  191. if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  192. sizeof (union ib_gid))) {
  193. spin_lock_irq(&priv->lock);
  194. if (!priv->broadcast) {
  195. spin_unlock_irq(&priv->lock);
  196. return -EAGAIN;
  197. }
  198. /*update priv member according to the new mcast*/
  199. priv->broadcast->mcmember.qkey = mcmember->qkey;
  200. priv->broadcast->mcmember.mtu = mcmember->mtu;
  201. priv->broadcast->mcmember.traffic_class = mcmember->traffic_class;
  202. priv->broadcast->mcmember.rate = mcmember->rate;
  203. priv->broadcast->mcmember.sl = mcmember->sl;
  204. priv->broadcast->mcmember.flow_label = mcmember->flow_label;
  205. priv->broadcast->mcmember.hop_limit = mcmember->hop_limit;
  206. /* assume if the admin and the mcast are the same both can be changed */
  207. if (priv->mcast_mtu == priv->admin_mtu)
  208. priv->admin_mtu =
  209. priv->mcast_mtu =
  210. IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  211. else
  212. priv->mcast_mtu =
  213. IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  214. priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
  215. spin_unlock_irq(&priv->lock);
  216. priv->tx_wr.remote_qkey = priv->qkey;
  217. set_qkey = 1;
  218. }
  219. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  220. if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  221. ipoib_warn(priv, "multicast group %pI6 already attached\n",
  222. mcast->mcmember.mgid.raw);
  223. return 0;
  224. }
  225. ret = rn->attach_mcast(dev, priv->ca, &mcast->mcmember.mgid,
  226. be16_to_cpu(mcast->mcmember.mlid),
  227. set_qkey, priv->qkey);
  228. if (ret < 0) {
  229. ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
  230. mcast->mcmember.mgid.raw);
  231. clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
  232. return ret;
  233. }
  234. }
  235. {
  236. struct ib_ah_attr av = {
  237. .dlid = be16_to_cpu(mcast->mcmember.mlid),
  238. .port_num = priv->port,
  239. .sl = mcast->mcmember.sl,
  240. .ah_flags = IB_AH_GRH,
  241. .static_rate = mcast->mcmember.rate,
  242. .grh = {
  243. .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
  244. .hop_limit = mcast->mcmember.hop_limit,
  245. .sgid_index = 0,
  246. .traffic_class = mcast->mcmember.traffic_class
  247. }
  248. };
  249. av.grh.dgid = mcast->mcmember.mgid;
  250. ah = ipoib_create_ah(dev, priv->pd, &av);
  251. if (IS_ERR(ah)) {
  252. ipoib_warn(priv, "ib_address_create failed %ld\n",
  253. -PTR_ERR(ah));
  254. /* use original error */
  255. return PTR_ERR(ah);
  256. } else {
  257. spin_lock_irq(&priv->lock);
  258. mcast->ah = ah;
  259. spin_unlock_irq(&priv->lock);
  260. ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
  261. mcast->mcmember.mgid.raw,
  262. mcast->ah->ah,
  263. be16_to_cpu(mcast->mcmember.mlid),
  264. mcast->mcmember.sl);
  265. }
  266. }
  267. /* actually send any queued packets */
  268. netif_tx_lock_bh(dev);
  269. while (!skb_queue_empty(&mcast->pkt_queue)) {
  270. struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
  271. netif_tx_unlock_bh(dev);
  272. skb->dev = dev;
  273. ret = dev_queue_xmit(skb);
  274. if (ret)
  275. ipoib_warn(priv, "%s:dev_queue_xmit failed to re-queue packet, ret:%d\n",
  276. __func__, ret);
  277. netif_tx_lock_bh(dev);
  278. }
  279. netif_tx_unlock_bh(dev);
  280. return 0;
  281. }
  282. void ipoib_mcast_carrier_on_task(struct work_struct *work)
  283. {
  284. struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
  285. carrier_on_task);
  286. struct ib_port_attr attr;
  287. int ret;
  288. if (ib_query_port(priv->ca, priv->port, &attr) ||
  289. attr.state != IB_PORT_ACTIVE) {
  290. ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
  291. return;
  292. }
  293. /*
  294. * Check if can send sendonly MCG's with sendonly-fullmember join state.
  295. * It done here after the successfully join to the broadcast group,
  296. * because the broadcast group must always be joined first and is always
  297. * re-joined if the SM changes substantially.
  298. */
  299. ret = ipoib_check_sm_sendonly_fullmember_support(priv);
  300. if (ret < 0)
  301. pr_debug("%s failed query sm support for sendonly-fullmember (ret: %d)\n",
  302. priv->dev->name, ret);
  303. /*
  304. * Take rtnl_lock to avoid racing with ipoib_stop() and
  305. * turning the carrier back on while a device is being
  306. * removed. However, ipoib_stop() will attempt to flush
  307. * the workqueue while holding the rtnl lock, so loop
  308. * on trylock until either we get the lock or we see
  309. * FLAG_OPER_UP go away as that signals that we are bailing
  310. * and can safely ignore the carrier on work.
  311. */
  312. while (!rtnl_trylock()) {
  313. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  314. return;
  315. else
  316. msleep(20);
  317. }
  318. if (!ipoib_cm_admin_enabled(priv->dev))
  319. dev_set_mtu(priv->dev, min(priv->mcast_mtu, priv->admin_mtu));
  320. netif_carrier_on(priv->dev);
  321. rtnl_unlock();
  322. }
  323. static int ipoib_mcast_join_complete(int status,
  324. struct ib_sa_multicast *multicast)
  325. {
  326. struct ipoib_mcast *mcast = multicast->context;
  327. struct net_device *dev = mcast->dev;
  328. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  329. ipoib_dbg_mcast(priv, "%sjoin completion for %pI6 (status %d)\n",
  330. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ?
  331. "sendonly " : "",
  332. mcast->mcmember.mgid.raw, status);
  333. /* We trap for port events ourselves. */
  334. if (status == -ENETRESET) {
  335. status = 0;
  336. goto out;
  337. }
  338. if (!status)
  339. status = ipoib_mcast_join_finish(mcast, &multicast->rec);
  340. if (!status) {
  341. mcast->backoff = 1;
  342. mcast->delay_until = jiffies;
  343. /*
  344. * Defer carrier on work to priv->wq to avoid a
  345. * deadlock on rtnl_lock here. Requeue our multicast
  346. * work too, which will end up happening right after
  347. * our carrier on task work and will allow us to
  348. * send out all of the non-broadcast joins
  349. */
  350. if (mcast == priv->broadcast) {
  351. spin_lock_irq(&priv->lock);
  352. queue_work(priv->wq, &priv->carrier_on_task);
  353. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  354. goto out_locked;
  355. }
  356. } else {
  357. bool silent_fail =
  358. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
  359. status == -EINVAL;
  360. if (mcast->logcount < 20) {
  361. if (status == -ETIMEDOUT || status == -EAGAIN ||
  362. silent_fail) {
  363. ipoib_dbg_mcast(priv, "%smulticast join failed for %pI6, status %d\n",
  364. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
  365. mcast->mcmember.mgid.raw, status);
  366. } else {
  367. ipoib_warn(priv, "%smulticast join failed for %pI6, status %d\n",
  368. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
  369. mcast->mcmember.mgid.raw, status);
  370. }
  371. if (!silent_fail)
  372. mcast->logcount++;
  373. }
  374. if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
  375. mcast->backoff >= 2) {
  376. /*
  377. * We only retry sendonly joins once before we drop
  378. * the packet and quit trying to deal with the
  379. * group. However, we leave the group in the
  380. * mcast list as an unjoined group. If we want to
  381. * try joining again, we simply queue up a packet
  382. * and restart the join thread. The empty queue
  383. * is why the join thread ignores this group.
  384. */
  385. mcast->backoff = 1;
  386. netif_tx_lock_bh(dev);
  387. while (!skb_queue_empty(&mcast->pkt_queue)) {
  388. ++dev->stats.tx_dropped;
  389. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  390. }
  391. netif_tx_unlock_bh(dev);
  392. } else {
  393. spin_lock_irq(&priv->lock);
  394. /* Requeue this join task with a backoff delay */
  395. __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
  396. goto out_locked;
  397. }
  398. }
  399. out:
  400. spin_lock_irq(&priv->lock);
  401. out_locked:
  402. /*
  403. * Make sure to set mcast->mc before we clear the busy flag to avoid
  404. * racing with code that checks for BUSY before checking mcast->mc
  405. */
  406. if (status)
  407. mcast->mc = NULL;
  408. else
  409. mcast->mc = multicast;
  410. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  411. spin_unlock_irq(&priv->lock);
  412. complete(&mcast->done);
  413. return status;
  414. }
  415. /*
  416. * Caller must hold 'priv->lock'
  417. */
  418. static int ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast)
  419. {
  420. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  421. struct ib_sa_multicast *multicast;
  422. struct ib_sa_mcmember_rec rec = {
  423. .join_state = 1
  424. };
  425. ib_sa_comp_mask comp_mask;
  426. int ret = 0;
  427. if (!priv->broadcast ||
  428. !test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  429. return -EINVAL;
  430. ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
  431. rec.mgid = mcast->mcmember.mgid;
  432. rec.port_gid = priv->local_gid;
  433. rec.pkey = cpu_to_be16(priv->pkey);
  434. comp_mask =
  435. IB_SA_MCMEMBER_REC_MGID |
  436. IB_SA_MCMEMBER_REC_PORT_GID |
  437. IB_SA_MCMEMBER_REC_PKEY |
  438. IB_SA_MCMEMBER_REC_JOIN_STATE;
  439. if (mcast != priv->broadcast) {
  440. /*
  441. * RFC 4391:
  442. * The MGID MUST use the same P_Key, Q_Key, SL, MTU,
  443. * and HopLimit as those used in the broadcast-GID. The rest
  444. * of attributes SHOULD follow the values used in the
  445. * broadcast-GID as well.
  446. */
  447. comp_mask |=
  448. IB_SA_MCMEMBER_REC_QKEY |
  449. IB_SA_MCMEMBER_REC_MTU_SELECTOR |
  450. IB_SA_MCMEMBER_REC_MTU |
  451. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
  452. IB_SA_MCMEMBER_REC_RATE_SELECTOR |
  453. IB_SA_MCMEMBER_REC_RATE |
  454. IB_SA_MCMEMBER_REC_SL |
  455. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  456. IB_SA_MCMEMBER_REC_HOP_LIMIT;
  457. rec.qkey = priv->broadcast->mcmember.qkey;
  458. rec.mtu_selector = IB_SA_EQ;
  459. rec.mtu = priv->broadcast->mcmember.mtu;
  460. rec.traffic_class = priv->broadcast->mcmember.traffic_class;
  461. rec.rate_selector = IB_SA_EQ;
  462. rec.rate = priv->broadcast->mcmember.rate;
  463. rec.sl = priv->broadcast->mcmember.sl;
  464. rec.flow_label = priv->broadcast->mcmember.flow_label;
  465. rec.hop_limit = priv->broadcast->mcmember.hop_limit;
  466. /*
  467. * Send-only IB Multicast joins work at the core IB layer but
  468. * require specific SM support.
  469. * We can use such joins here only if the current SM supports that feature.
  470. * However, if not, we emulate an Ethernet multicast send,
  471. * which does not require a multicast subscription and will
  472. * still send properly. The most appropriate thing to
  473. * do is to create the group if it doesn't exist as that
  474. * most closely emulates the behavior, from a user space
  475. * application perspective, of Ethernet multicast operation.
  476. */
  477. if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
  478. priv->sm_fullmember_sendonly_support)
  479. /* SM supports sendonly-fullmember, otherwise fallback to full-member */
  480. rec.join_state = SENDONLY_FULLMEMBER_JOIN;
  481. }
  482. spin_unlock_irq(&priv->lock);
  483. multicast = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
  484. &rec, comp_mask, GFP_KERNEL,
  485. ipoib_mcast_join_complete, mcast);
  486. spin_lock_irq(&priv->lock);
  487. if (IS_ERR(multicast)) {
  488. ret = PTR_ERR(multicast);
  489. ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
  490. /* Requeue this join task with a backoff delay */
  491. __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
  492. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  493. spin_unlock_irq(&priv->lock);
  494. complete(&mcast->done);
  495. spin_lock_irq(&priv->lock);
  496. }
  497. return 0;
  498. }
  499. void ipoib_mcast_join_task(struct work_struct *work)
  500. {
  501. struct ipoib_dev_priv *priv =
  502. container_of(work, struct ipoib_dev_priv, mcast_task.work);
  503. struct net_device *dev = priv->dev;
  504. struct ib_port_attr port_attr;
  505. unsigned long delay_until = 0;
  506. struct ipoib_mcast *mcast = NULL;
  507. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  508. return;
  509. if (ib_query_port(priv->ca, priv->port, &port_attr)) {
  510. ipoib_dbg(priv, "ib_query_port() failed\n");
  511. return;
  512. }
  513. if (port_attr.state != IB_PORT_ACTIVE) {
  514. ipoib_dbg(priv, "port state is not ACTIVE (state = %d) suspending join task\n",
  515. port_attr.state);
  516. return;
  517. }
  518. priv->local_lid = port_attr.lid;
  519. netif_addr_lock_bh(dev);
  520. if (!test_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags)) {
  521. netif_addr_unlock_bh(dev);
  522. return;
  523. }
  524. netif_addr_unlock_bh(dev);
  525. spin_lock_irq(&priv->lock);
  526. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  527. goto out;
  528. if (!priv->broadcast) {
  529. struct ipoib_mcast *broadcast;
  530. broadcast = ipoib_mcast_alloc(dev, 0);
  531. if (!broadcast) {
  532. ipoib_warn(priv, "failed to allocate broadcast group\n");
  533. /*
  534. * Restart us after a 1 second delay to retry
  535. * creating our broadcast group and attaching to
  536. * it. Until this succeeds, this ipoib dev is
  537. * completely stalled (multicast wise).
  538. */
  539. __ipoib_mcast_schedule_join_thread(priv, NULL, 1);
  540. goto out;
  541. }
  542. memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  543. sizeof (union ib_gid));
  544. priv->broadcast = broadcast;
  545. __ipoib_mcast_add(dev, priv->broadcast);
  546. }
  547. if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  548. if (IS_ERR_OR_NULL(priv->broadcast->mc) &&
  549. !test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags)) {
  550. mcast = priv->broadcast;
  551. if (mcast->backoff > 1 &&
  552. time_before(jiffies, mcast->delay_until)) {
  553. delay_until = mcast->delay_until;
  554. mcast = NULL;
  555. }
  556. }
  557. goto out;
  558. }
  559. /*
  560. * We'll never get here until the broadcast group is both allocated
  561. * and attached
  562. */
  563. list_for_each_entry(mcast, &priv->multicast_list, list) {
  564. if (IS_ERR_OR_NULL(mcast->mc) &&
  565. !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags) &&
  566. (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ||
  567. !skb_queue_empty(&mcast->pkt_queue))) {
  568. if (mcast->backoff == 1 ||
  569. time_after_eq(jiffies, mcast->delay_until)) {
  570. /* Found the next unjoined group */
  571. init_completion(&mcast->done);
  572. set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  573. if (ipoib_mcast_join(dev, mcast)) {
  574. spin_unlock_irq(&priv->lock);
  575. return;
  576. }
  577. } else if (!delay_until ||
  578. time_before(mcast->delay_until, delay_until))
  579. delay_until = mcast->delay_until;
  580. }
  581. }
  582. mcast = NULL;
  583. ipoib_dbg_mcast(priv, "successfully started all multicast joins\n");
  584. out:
  585. if (delay_until) {
  586. cancel_delayed_work(&priv->mcast_task);
  587. queue_delayed_work(priv->wq, &priv->mcast_task,
  588. delay_until - jiffies);
  589. }
  590. if (mcast) {
  591. init_completion(&mcast->done);
  592. set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  593. ipoib_mcast_join(dev, mcast);
  594. }
  595. spin_unlock_irq(&priv->lock);
  596. }
  597. void ipoib_mcast_start_thread(struct net_device *dev)
  598. {
  599. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  600. unsigned long flags;
  601. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  602. spin_lock_irqsave(&priv->lock, flags);
  603. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  604. spin_unlock_irqrestore(&priv->lock, flags);
  605. }
  606. int ipoib_mcast_stop_thread(struct net_device *dev)
  607. {
  608. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  609. unsigned long flags;
  610. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  611. spin_lock_irqsave(&priv->lock, flags);
  612. cancel_delayed_work(&priv->mcast_task);
  613. spin_unlock_irqrestore(&priv->lock, flags);
  614. flush_workqueue(priv->wq);
  615. return 0;
  616. }
  617. static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  618. {
  619. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  620. struct rdma_netdev *rn = netdev_priv(dev);
  621. int ret = 0;
  622. if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  623. ipoib_warn(priv, "ipoib_mcast_leave on an in-flight join\n");
  624. if (!IS_ERR_OR_NULL(mcast->mc))
  625. ib_sa_free_multicast(mcast->mc);
  626. if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  627. ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
  628. mcast->mcmember.mgid.raw);
  629. /* Remove ourselves from the multicast group */
  630. ret = rn->detach_mcast(dev, priv->ca, &mcast->mcmember.mgid,
  631. be16_to_cpu(mcast->mcmember.mlid));
  632. if (ret)
  633. ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
  634. } else if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  635. ipoib_dbg(priv, "leaving with no mcmember but not a "
  636. "SENDONLY join\n");
  637. return 0;
  638. }
  639. /*
  640. * Check if the multicast group is sendonly. If so remove it from the maps
  641. * and add to the remove list
  642. */
  643. void ipoib_check_and_add_mcast_sendonly(struct ipoib_dev_priv *priv, u8 *mgid,
  644. struct list_head *remove_list)
  645. {
  646. /* Is this multicast ? */
  647. if (*mgid == 0xff) {
  648. struct ipoib_mcast *mcast = __ipoib_mcast_find(priv->dev, mgid);
  649. if (mcast && test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  650. list_del(&mcast->list);
  651. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  652. list_add_tail(&mcast->list, remove_list);
  653. }
  654. }
  655. }
  656. void ipoib_mcast_remove_list(struct list_head *remove_list)
  657. {
  658. struct ipoib_mcast *mcast, *tmcast;
  659. list_for_each_entry_safe(mcast, tmcast, remove_list, list) {
  660. ipoib_mcast_leave(mcast->dev, mcast);
  661. ipoib_mcast_free(mcast);
  662. }
  663. }
  664. void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
  665. {
  666. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  667. struct rdma_netdev *rn = netdev_priv(dev);
  668. struct ipoib_mcast *mcast;
  669. unsigned long flags;
  670. void *mgid = daddr + 4;
  671. spin_lock_irqsave(&priv->lock, flags);
  672. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
  673. !priv->broadcast ||
  674. !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  675. ++dev->stats.tx_dropped;
  676. dev_kfree_skb_any(skb);
  677. goto unlock;
  678. }
  679. mcast = __ipoib_mcast_find(dev, mgid);
  680. if (!mcast || !mcast->ah) {
  681. if (!mcast) {
  682. /* Let's create a new send only group now */
  683. ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
  684. mgid);
  685. mcast = ipoib_mcast_alloc(dev, 0);
  686. if (!mcast) {
  687. ipoib_warn(priv, "unable to allocate memory "
  688. "for multicast structure\n");
  689. ++dev->stats.tx_dropped;
  690. dev_kfree_skb_any(skb);
  691. goto unlock;
  692. }
  693. set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
  694. memcpy(mcast->mcmember.mgid.raw, mgid,
  695. sizeof (union ib_gid));
  696. __ipoib_mcast_add(dev, mcast);
  697. list_add_tail(&mcast->list, &priv->multicast_list);
  698. }
  699. if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE) {
  700. /* put pseudoheader back on for next time */
  701. skb_push(skb, sizeof(struct ipoib_pseudo_header));
  702. skb_queue_tail(&mcast->pkt_queue, skb);
  703. } else {
  704. ++dev->stats.tx_dropped;
  705. dev_kfree_skb_any(skb);
  706. }
  707. if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
  708. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  709. }
  710. } else {
  711. struct ipoib_neigh *neigh;
  712. spin_unlock_irqrestore(&priv->lock, flags);
  713. neigh = ipoib_neigh_get(dev, daddr);
  714. spin_lock_irqsave(&priv->lock, flags);
  715. if (!neigh) {
  716. neigh = ipoib_neigh_alloc(daddr, dev);
  717. if (neigh) {
  718. kref_get(&mcast->ah->ref);
  719. neigh->ah = mcast->ah;
  720. list_add_tail(&neigh->list, &mcast->neigh_list);
  721. }
  722. }
  723. spin_unlock_irqrestore(&priv->lock, flags);
  724. mcast->ah->last_send = rn->send(dev, skb, mcast->ah->ah,
  725. IB_MULTICAST_QPN);
  726. if (neigh)
  727. ipoib_neigh_put(neigh);
  728. return;
  729. }
  730. unlock:
  731. spin_unlock_irqrestore(&priv->lock, flags);
  732. }
  733. void ipoib_mcast_dev_flush(struct net_device *dev)
  734. {
  735. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  736. LIST_HEAD(remove_list);
  737. struct ipoib_mcast *mcast, *tmcast;
  738. unsigned long flags;
  739. ipoib_dbg_mcast(priv, "flushing multicast list\n");
  740. spin_lock_irqsave(&priv->lock, flags);
  741. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  742. list_del(&mcast->list);
  743. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  744. list_add_tail(&mcast->list, &remove_list);
  745. }
  746. if (priv->broadcast) {
  747. rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
  748. list_add_tail(&priv->broadcast->list, &remove_list);
  749. priv->broadcast = NULL;
  750. }
  751. spin_unlock_irqrestore(&priv->lock, flags);
  752. /*
  753. * make sure the in-flight joins have finished before we attempt
  754. * to leave
  755. */
  756. list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
  757. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  758. wait_for_completion(&mcast->done);
  759. ipoib_mcast_remove_list(&remove_list);
  760. }
  761. static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
  762. {
  763. /* reserved QPN, prefix, scope */
  764. if (memcmp(addr, broadcast, 6))
  765. return 0;
  766. /* signature lower, pkey */
  767. if (memcmp(addr + 7, broadcast + 7, 3))
  768. return 0;
  769. return 1;
  770. }
  771. void ipoib_mcast_restart_task(struct work_struct *work)
  772. {
  773. struct ipoib_dev_priv *priv =
  774. container_of(work, struct ipoib_dev_priv, restart_task);
  775. struct net_device *dev = priv->dev;
  776. struct netdev_hw_addr *ha;
  777. struct ipoib_mcast *mcast, *tmcast;
  778. LIST_HEAD(remove_list);
  779. unsigned long flags;
  780. struct ib_sa_mcmember_rec rec;
  781. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  782. /*
  783. * shortcut...on shutdown flush is called next, just
  784. * let it do all the work
  785. */
  786. return;
  787. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  788. local_irq_save(flags);
  789. netif_addr_lock(dev);
  790. spin_lock(&priv->lock);
  791. /*
  792. * Unfortunately, the networking core only gives us a list of all of
  793. * the multicast hardware addresses. We need to figure out which ones
  794. * are new and which ones have been removed
  795. */
  796. /* Clear out the found flag */
  797. list_for_each_entry(mcast, &priv->multicast_list, list)
  798. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  799. /* Mark all of the entries that are found or don't exist */
  800. netdev_for_each_mc_addr(ha, dev) {
  801. union ib_gid mgid;
  802. if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
  803. continue;
  804. memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
  805. mcast = __ipoib_mcast_find(dev, &mgid);
  806. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  807. struct ipoib_mcast *nmcast;
  808. /* ignore group which is directly joined by userspace */
  809. if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
  810. !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
  811. ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
  812. mgid.raw);
  813. continue;
  814. }
  815. /* Not found or send-only group, let's add a new entry */
  816. ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
  817. mgid.raw);
  818. nmcast = ipoib_mcast_alloc(dev, 0);
  819. if (!nmcast) {
  820. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  821. continue;
  822. }
  823. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  824. nmcast->mcmember.mgid = mgid;
  825. if (mcast) {
  826. /* Destroy the send only entry */
  827. list_move_tail(&mcast->list, &remove_list);
  828. rb_replace_node(&mcast->rb_node,
  829. &nmcast->rb_node,
  830. &priv->multicast_tree);
  831. } else
  832. __ipoib_mcast_add(dev, nmcast);
  833. list_add_tail(&nmcast->list, &priv->multicast_list);
  834. }
  835. if (mcast)
  836. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  837. }
  838. /* Remove all of the entries don't exist anymore */
  839. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  840. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  841. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  842. ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
  843. mcast->mcmember.mgid.raw);
  844. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  845. /* Move to the remove list */
  846. list_move_tail(&mcast->list, &remove_list);
  847. }
  848. }
  849. spin_unlock(&priv->lock);
  850. netif_addr_unlock(dev);
  851. local_irq_restore(flags);
  852. /*
  853. * make sure the in-flight joins have finished before we attempt
  854. * to leave
  855. */
  856. list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
  857. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  858. wait_for_completion(&mcast->done);
  859. ipoib_mcast_remove_list(&remove_list);
  860. /*
  861. * Double check that we are still up
  862. */
  863. if (test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  864. spin_lock_irqsave(&priv->lock, flags);
  865. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  866. spin_unlock_irqrestore(&priv->lock, flags);
  867. }
  868. }
  869. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  870. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  871. {
  872. struct ipoib_mcast_iter *iter;
  873. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  874. if (!iter)
  875. return NULL;
  876. iter->dev = dev;
  877. memset(iter->mgid.raw, 0, 16);
  878. if (ipoib_mcast_iter_next(iter)) {
  879. kfree(iter);
  880. return NULL;
  881. }
  882. return iter;
  883. }
  884. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  885. {
  886. struct ipoib_dev_priv *priv = ipoib_priv(iter->dev);
  887. struct rb_node *n;
  888. struct ipoib_mcast *mcast;
  889. int ret = 1;
  890. spin_lock_irq(&priv->lock);
  891. n = rb_first(&priv->multicast_tree);
  892. while (n) {
  893. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  894. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  895. sizeof (union ib_gid)) < 0) {
  896. iter->mgid = mcast->mcmember.mgid;
  897. iter->created = mcast->created;
  898. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  899. iter->complete = !!mcast->ah;
  900. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  901. ret = 0;
  902. break;
  903. }
  904. n = rb_next(n);
  905. }
  906. spin_unlock_irq(&priv->lock);
  907. return ret;
  908. }
  909. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  910. union ib_gid *mgid,
  911. unsigned long *created,
  912. unsigned int *queuelen,
  913. unsigned int *complete,
  914. unsigned int *send_only)
  915. {
  916. *mgid = iter->mgid;
  917. *created = iter->created;
  918. *queuelen = iter->queuelen;
  919. *complete = iter->complete;
  920. *send_only = iter->send_only;
  921. }
  922. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */