ipoib_multicast.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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. /*
  61. * This should be called with the priv->lock held
  62. */
  63. static void __ipoib_mcast_schedule_join_thread(struct ipoib_dev_priv *priv,
  64. struct ipoib_mcast *mcast,
  65. bool delay)
  66. {
  67. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  68. return;
  69. /*
  70. * We will be scheduling *something*, so cancel whatever is
  71. * currently scheduled first
  72. */
  73. cancel_delayed_work(&priv->mcast_task);
  74. if (mcast && delay) {
  75. /*
  76. * We had a failure and want to schedule a retry later
  77. */
  78. mcast->backoff *= 2;
  79. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  80. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  81. mcast->delay_until = jiffies + (mcast->backoff * HZ);
  82. /*
  83. * Mark this mcast for its delay, but restart the
  84. * task immediately. The join task will make sure to
  85. * clear out all entries without delays, and then
  86. * schedule itself to run again when the earliest
  87. * delay expires
  88. */
  89. queue_delayed_work(priv->wq, &priv->mcast_task, 0);
  90. } else if (delay) {
  91. /*
  92. * Special case of retrying after a failure to
  93. * allocate the broadcast multicast group, wait
  94. * 1 second and try again
  95. */
  96. queue_delayed_work(priv->wq, &priv->mcast_task, HZ);
  97. } else
  98. queue_delayed_work(priv->wq, &priv->mcast_task, 0);
  99. }
  100. void ipoib_mcast_free(struct ipoib_mcast *mcast)
  101. {
  102. struct net_device *dev = mcast->dev;
  103. int tx_dropped = 0;
  104. ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
  105. mcast->mcmember.mgid.raw);
  106. /* remove all neigh connected to this mcast */
  107. ipoib_del_neighs_by_gid(dev, mcast->mcmember.mgid.raw);
  108. if (mcast->ah)
  109. ipoib_put_ah(mcast->ah);
  110. while (!skb_queue_empty(&mcast->pkt_queue)) {
  111. ++tx_dropped;
  112. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  113. }
  114. netif_tx_lock_bh(dev);
  115. dev->stats.tx_dropped += tx_dropped;
  116. netif_tx_unlock_bh(dev);
  117. kfree(mcast);
  118. }
  119. static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
  120. int can_sleep)
  121. {
  122. struct ipoib_mcast *mcast;
  123. mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
  124. if (!mcast)
  125. return NULL;
  126. mcast->dev = dev;
  127. mcast->created = jiffies;
  128. mcast->delay_until = jiffies;
  129. mcast->backoff = 1;
  130. INIT_LIST_HEAD(&mcast->list);
  131. INIT_LIST_HEAD(&mcast->neigh_list);
  132. skb_queue_head_init(&mcast->pkt_queue);
  133. return mcast;
  134. }
  135. struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
  136. {
  137. struct ipoib_dev_priv *priv = netdev_priv(dev);
  138. struct rb_node *n = priv->multicast_tree.rb_node;
  139. while (n) {
  140. struct ipoib_mcast *mcast;
  141. int ret;
  142. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  143. ret = memcmp(mgid, mcast->mcmember.mgid.raw,
  144. sizeof (union ib_gid));
  145. if (ret < 0)
  146. n = n->rb_left;
  147. else if (ret > 0)
  148. n = n->rb_right;
  149. else
  150. return mcast;
  151. }
  152. return NULL;
  153. }
  154. static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
  155. {
  156. struct ipoib_dev_priv *priv = netdev_priv(dev);
  157. struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
  158. while (*n) {
  159. struct ipoib_mcast *tmcast;
  160. int ret;
  161. pn = *n;
  162. tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
  163. ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
  164. sizeof (union ib_gid));
  165. if (ret < 0)
  166. n = &pn->rb_left;
  167. else if (ret > 0)
  168. n = &pn->rb_right;
  169. else
  170. return -EEXIST;
  171. }
  172. rb_link_node(&mcast->rb_node, pn, n);
  173. rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
  174. return 0;
  175. }
  176. static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
  177. struct ib_sa_mcmember_rec *mcmember)
  178. {
  179. struct net_device *dev = mcast->dev;
  180. struct ipoib_dev_priv *priv = netdev_priv(dev);
  181. struct ipoib_ah *ah;
  182. int ret;
  183. int set_qkey = 0;
  184. mcast->mcmember = *mcmember;
  185. /* Set the multicast MTU and cached Q_Key before we attach if it's
  186. * the broadcast group.
  187. */
  188. if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  189. sizeof (union ib_gid))) {
  190. spin_lock_irq(&priv->lock);
  191. if (!priv->broadcast) {
  192. spin_unlock_irq(&priv->lock);
  193. return -EAGAIN;
  194. }
  195. /*update priv member according to the new mcast*/
  196. priv->broadcast->mcmember.qkey = mcmember->qkey;
  197. priv->broadcast->mcmember.mtu = mcmember->mtu;
  198. priv->broadcast->mcmember.traffic_class = mcmember->traffic_class;
  199. priv->broadcast->mcmember.rate = mcmember->rate;
  200. priv->broadcast->mcmember.sl = mcmember->sl;
  201. priv->broadcast->mcmember.flow_label = mcmember->flow_label;
  202. priv->broadcast->mcmember.hop_limit = mcmember->hop_limit;
  203. /* assume if the admin and the mcast are the same both can be changed */
  204. if (priv->mcast_mtu == priv->admin_mtu)
  205. priv->admin_mtu =
  206. priv->mcast_mtu =
  207. IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  208. else
  209. priv->mcast_mtu =
  210. IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  211. priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
  212. spin_unlock_irq(&priv->lock);
  213. priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
  214. set_qkey = 1;
  215. }
  216. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  217. if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  218. ipoib_warn(priv, "multicast group %pI6 already attached\n",
  219. mcast->mcmember.mgid.raw);
  220. return 0;
  221. }
  222. ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
  223. &mcast->mcmember.mgid, set_qkey);
  224. if (ret < 0) {
  225. ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
  226. mcast->mcmember.mgid.raw);
  227. clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
  228. return ret;
  229. }
  230. }
  231. {
  232. struct ib_ah_attr av = {
  233. .dlid = be16_to_cpu(mcast->mcmember.mlid),
  234. .port_num = priv->port,
  235. .sl = mcast->mcmember.sl,
  236. .ah_flags = IB_AH_GRH,
  237. .static_rate = mcast->mcmember.rate,
  238. .grh = {
  239. .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
  240. .hop_limit = mcast->mcmember.hop_limit,
  241. .sgid_index = 0,
  242. .traffic_class = mcast->mcmember.traffic_class
  243. }
  244. };
  245. av.grh.dgid = mcast->mcmember.mgid;
  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. } else {
  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. }
  262. }
  263. /* actually send any queued packets */
  264. netif_tx_lock_bh(dev);
  265. while (!skb_queue_empty(&mcast->pkt_queue)) {
  266. struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
  267. netif_tx_unlock_bh(dev);
  268. skb->dev = dev;
  269. if (dev_queue_xmit(skb))
  270. ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
  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. * Take rtnl_lock to avoid racing with ipoib_stop() and
  288. * turning the carrier back on while a device is being
  289. * removed. However, ipoib_stop() will attempt to flush
  290. * the workqueue while holding the rtnl lock, so loop
  291. * on trylock until either we get the lock or we see
  292. * FLAG_OPER_UP go away as that signals that we are bailing
  293. * and can safely ignore the carrier on work.
  294. */
  295. while (!rtnl_trylock()) {
  296. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  297. return;
  298. else
  299. msleep(20);
  300. }
  301. if (!ipoib_cm_admin_enabled(priv->dev))
  302. dev_set_mtu(priv->dev, min(priv->mcast_mtu, priv->admin_mtu));
  303. netif_carrier_on(priv->dev);
  304. rtnl_unlock();
  305. }
  306. static int ipoib_mcast_join_complete(int status,
  307. struct ib_sa_multicast *multicast)
  308. {
  309. struct ipoib_mcast *mcast = multicast->context;
  310. struct net_device *dev = mcast->dev;
  311. struct ipoib_dev_priv *priv = netdev_priv(dev);
  312. ipoib_dbg_mcast(priv, "%sjoin completion for %pI6 (status %d)\n",
  313. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ?
  314. "sendonly " : "",
  315. mcast->mcmember.mgid.raw, status);
  316. /* We trap for port events ourselves. */
  317. if (status == -ENETRESET) {
  318. status = 0;
  319. goto out;
  320. }
  321. if (!status)
  322. status = ipoib_mcast_join_finish(mcast, &multicast->rec);
  323. if (!status) {
  324. mcast->backoff = 1;
  325. mcast->delay_until = jiffies;
  326. /*
  327. * Defer carrier on work to priv->wq to avoid a
  328. * deadlock on rtnl_lock here. Requeue our multicast
  329. * work too, which will end up happening right after
  330. * our carrier on task work and will allow us to
  331. * send out all of the non-broadcast joins
  332. */
  333. if (mcast == priv->broadcast) {
  334. spin_lock_irq(&priv->lock);
  335. queue_work(priv->wq, &priv->carrier_on_task);
  336. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  337. goto out_locked;
  338. }
  339. } else {
  340. bool silent_fail =
  341. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
  342. status == -EINVAL;
  343. if (mcast->logcount < 20) {
  344. if (status == -ETIMEDOUT || status == -EAGAIN ||
  345. silent_fail) {
  346. ipoib_dbg_mcast(priv, "%smulticast join failed for %pI6, status %d\n",
  347. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
  348. mcast->mcmember.mgid.raw, status);
  349. } else {
  350. ipoib_warn(priv, "%smulticast join failed for %pI6, status %d\n",
  351. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
  352. mcast->mcmember.mgid.raw, status);
  353. }
  354. if (!silent_fail)
  355. mcast->logcount++;
  356. }
  357. if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
  358. mcast->backoff >= 2) {
  359. /*
  360. * We only retry sendonly joins once before we drop
  361. * the packet and quit trying to deal with the
  362. * group. However, we leave the group in the
  363. * mcast list as an unjoined group. If we want to
  364. * try joining again, we simply queue up a packet
  365. * and restart the join thread. The empty queue
  366. * is why the join thread ignores this group.
  367. */
  368. mcast->backoff = 1;
  369. netif_tx_lock_bh(dev);
  370. while (!skb_queue_empty(&mcast->pkt_queue)) {
  371. ++dev->stats.tx_dropped;
  372. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  373. }
  374. netif_tx_unlock_bh(dev);
  375. } else {
  376. spin_lock_irq(&priv->lock);
  377. /* Requeue this join task with a backoff delay */
  378. __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
  379. goto out_locked;
  380. }
  381. }
  382. out:
  383. spin_lock_irq(&priv->lock);
  384. out_locked:
  385. /*
  386. * Make sure to set mcast->mc before we clear the busy flag to avoid
  387. * racing with code that checks for BUSY before checking mcast->mc
  388. */
  389. if (status)
  390. mcast->mc = NULL;
  391. else
  392. mcast->mc = multicast;
  393. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  394. spin_unlock_irq(&priv->lock);
  395. complete(&mcast->done);
  396. return status;
  397. }
  398. static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast)
  399. {
  400. struct ipoib_dev_priv *priv = netdev_priv(dev);
  401. struct ib_sa_multicast *multicast;
  402. struct ib_sa_mcmember_rec rec = {
  403. .join_state = 1
  404. };
  405. ib_sa_comp_mask comp_mask;
  406. int ret = 0;
  407. ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
  408. rec.mgid = mcast->mcmember.mgid;
  409. rec.port_gid = priv->local_gid;
  410. rec.pkey = cpu_to_be16(priv->pkey);
  411. comp_mask =
  412. IB_SA_MCMEMBER_REC_MGID |
  413. IB_SA_MCMEMBER_REC_PORT_GID |
  414. IB_SA_MCMEMBER_REC_PKEY |
  415. IB_SA_MCMEMBER_REC_JOIN_STATE;
  416. if (mcast != priv->broadcast) {
  417. /*
  418. * RFC 4391:
  419. * The MGID MUST use the same P_Key, Q_Key, SL, MTU,
  420. * and HopLimit as those used in the broadcast-GID. The rest
  421. * of attributes SHOULD follow the values used in the
  422. * broadcast-GID as well.
  423. */
  424. comp_mask |=
  425. IB_SA_MCMEMBER_REC_QKEY |
  426. IB_SA_MCMEMBER_REC_MTU_SELECTOR |
  427. IB_SA_MCMEMBER_REC_MTU |
  428. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
  429. IB_SA_MCMEMBER_REC_RATE_SELECTOR |
  430. IB_SA_MCMEMBER_REC_RATE |
  431. IB_SA_MCMEMBER_REC_SL |
  432. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  433. IB_SA_MCMEMBER_REC_HOP_LIMIT;
  434. rec.qkey = priv->broadcast->mcmember.qkey;
  435. rec.mtu_selector = IB_SA_EQ;
  436. rec.mtu = priv->broadcast->mcmember.mtu;
  437. rec.traffic_class = priv->broadcast->mcmember.traffic_class;
  438. rec.rate_selector = IB_SA_EQ;
  439. rec.rate = priv->broadcast->mcmember.rate;
  440. rec.sl = priv->broadcast->mcmember.sl;
  441. rec.flow_label = priv->broadcast->mcmember.flow_label;
  442. rec.hop_limit = priv->broadcast->mcmember.hop_limit;
  443. /*
  444. * Send-only IB Multicast joins do not work at the core
  445. * IB layer yet, so we can't use them here. However,
  446. * we are emulating an Ethernet multicast send, which
  447. * does not require a multicast subscription and will
  448. * still send properly. The most appropriate thing to
  449. * do is to create the group if it doesn't exist as that
  450. * most closely emulates the behavior, from a user space
  451. * application perspecitive, of Ethernet multicast
  452. * operation. For now, we do a full join, maybe later
  453. * when the core IB layers support send only joins we
  454. * will use them.
  455. */
  456. #if 0
  457. if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  458. rec.join_state = 4;
  459. #endif
  460. }
  461. multicast = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
  462. &rec, comp_mask, GFP_KERNEL,
  463. ipoib_mcast_join_complete, mcast);
  464. if (IS_ERR(multicast)) {
  465. ret = PTR_ERR(multicast);
  466. ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
  467. spin_lock_irq(&priv->lock);
  468. /* Requeue this join task with a backoff delay */
  469. __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
  470. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  471. spin_unlock_irq(&priv->lock);
  472. complete(&mcast->done);
  473. }
  474. }
  475. void ipoib_mcast_join_task(struct work_struct *work)
  476. {
  477. struct ipoib_dev_priv *priv =
  478. container_of(work, struct ipoib_dev_priv, mcast_task.work);
  479. struct net_device *dev = priv->dev;
  480. struct ib_port_attr port_attr;
  481. unsigned long delay_until = 0;
  482. struct ipoib_mcast *mcast = NULL;
  483. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  484. return;
  485. if (ib_query_port(priv->ca, priv->port, &port_attr) ||
  486. port_attr.state != IB_PORT_ACTIVE) {
  487. ipoib_dbg(priv, "port state is not ACTIVE (state = %d) suspending join task\n",
  488. port_attr.state);
  489. return;
  490. }
  491. priv->local_lid = port_attr.lid;
  492. if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
  493. ipoib_warn(priv, "ib_query_gid() failed\n");
  494. else
  495. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  496. spin_lock_irq(&priv->lock);
  497. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  498. goto out;
  499. if (!priv->broadcast) {
  500. struct ipoib_mcast *broadcast;
  501. broadcast = ipoib_mcast_alloc(dev, 0);
  502. if (!broadcast) {
  503. ipoib_warn(priv, "failed to allocate broadcast group\n");
  504. /*
  505. * Restart us after a 1 second delay to retry
  506. * creating our broadcast group and attaching to
  507. * it. Until this succeeds, this ipoib dev is
  508. * completely stalled (multicast wise).
  509. */
  510. __ipoib_mcast_schedule_join_thread(priv, NULL, 1);
  511. goto out;
  512. }
  513. memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  514. sizeof (union ib_gid));
  515. priv->broadcast = broadcast;
  516. __ipoib_mcast_add(dev, priv->broadcast);
  517. }
  518. if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  519. if (IS_ERR_OR_NULL(priv->broadcast->mc) &&
  520. !test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags)) {
  521. mcast = priv->broadcast;
  522. if (mcast->backoff > 1 &&
  523. time_before(jiffies, mcast->delay_until)) {
  524. delay_until = mcast->delay_until;
  525. mcast = NULL;
  526. }
  527. }
  528. goto out;
  529. }
  530. /*
  531. * We'll never get here until the broadcast group is both allocated
  532. * and attached
  533. */
  534. list_for_each_entry(mcast, &priv->multicast_list, list) {
  535. if (IS_ERR_OR_NULL(mcast->mc) &&
  536. !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags) &&
  537. (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ||
  538. !skb_queue_empty(&mcast->pkt_queue))) {
  539. if (mcast->backoff == 1 ||
  540. time_after_eq(jiffies, mcast->delay_until)) {
  541. /* Found the next unjoined group */
  542. init_completion(&mcast->done);
  543. set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  544. spin_unlock_irq(&priv->lock);
  545. ipoib_mcast_join(dev, mcast);
  546. spin_lock_irq(&priv->lock);
  547. } else if (!delay_until ||
  548. time_before(mcast->delay_until, delay_until))
  549. delay_until = mcast->delay_until;
  550. }
  551. }
  552. mcast = NULL;
  553. ipoib_dbg_mcast(priv, "successfully started all multicast joins\n");
  554. out:
  555. if (delay_until) {
  556. cancel_delayed_work(&priv->mcast_task);
  557. queue_delayed_work(priv->wq, &priv->mcast_task,
  558. delay_until - jiffies);
  559. }
  560. if (mcast) {
  561. init_completion(&mcast->done);
  562. set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  563. }
  564. spin_unlock_irq(&priv->lock);
  565. if (mcast)
  566. ipoib_mcast_join(dev, mcast);
  567. }
  568. int ipoib_mcast_start_thread(struct net_device *dev)
  569. {
  570. struct ipoib_dev_priv *priv = netdev_priv(dev);
  571. unsigned long flags;
  572. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  573. spin_lock_irqsave(&priv->lock, flags);
  574. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  575. spin_unlock_irqrestore(&priv->lock, flags);
  576. return 0;
  577. }
  578. int ipoib_mcast_stop_thread(struct net_device *dev)
  579. {
  580. struct ipoib_dev_priv *priv = netdev_priv(dev);
  581. unsigned long flags;
  582. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  583. spin_lock_irqsave(&priv->lock, flags);
  584. cancel_delayed_work(&priv->mcast_task);
  585. spin_unlock_irqrestore(&priv->lock, flags);
  586. flush_workqueue(priv->wq);
  587. return 0;
  588. }
  589. int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  590. {
  591. struct ipoib_dev_priv *priv = netdev_priv(dev);
  592. int ret = 0;
  593. if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  594. ipoib_warn(priv, "ipoib_mcast_leave on an in-flight join\n");
  595. if (!IS_ERR_OR_NULL(mcast->mc))
  596. ib_sa_free_multicast(mcast->mc);
  597. if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  598. ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
  599. mcast->mcmember.mgid.raw);
  600. /* Remove ourselves from the multicast group */
  601. ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
  602. be16_to_cpu(mcast->mcmember.mlid));
  603. if (ret)
  604. ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
  605. } else if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  606. ipoib_dbg(priv, "leaving with no mcmember but not a "
  607. "SENDONLY join\n");
  608. return 0;
  609. }
  610. void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
  611. {
  612. struct ipoib_dev_priv *priv = netdev_priv(dev);
  613. struct ipoib_mcast *mcast;
  614. unsigned long flags;
  615. void *mgid = daddr + 4;
  616. spin_lock_irqsave(&priv->lock, flags);
  617. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
  618. !priv->broadcast ||
  619. !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  620. ++dev->stats.tx_dropped;
  621. dev_kfree_skb_any(skb);
  622. goto unlock;
  623. }
  624. mcast = __ipoib_mcast_find(dev, mgid);
  625. if (!mcast || !mcast->ah) {
  626. if (!mcast) {
  627. /* Let's create a new send only group now */
  628. ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
  629. mgid);
  630. mcast = ipoib_mcast_alloc(dev, 0);
  631. if (!mcast) {
  632. ipoib_warn(priv, "unable to allocate memory "
  633. "for multicast structure\n");
  634. ++dev->stats.tx_dropped;
  635. dev_kfree_skb_any(skb);
  636. goto unlock;
  637. }
  638. set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
  639. memcpy(mcast->mcmember.mgid.raw, mgid,
  640. sizeof (union ib_gid));
  641. __ipoib_mcast_add(dev, mcast);
  642. list_add_tail(&mcast->list, &priv->multicast_list);
  643. }
  644. if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
  645. skb_queue_tail(&mcast->pkt_queue, skb);
  646. else {
  647. ++dev->stats.tx_dropped;
  648. dev_kfree_skb_any(skb);
  649. }
  650. if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
  651. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  652. }
  653. } else {
  654. struct ipoib_neigh *neigh;
  655. spin_unlock_irqrestore(&priv->lock, flags);
  656. neigh = ipoib_neigh_get(dev, daddr);
  657. spin_lock_irqsave(&priv->lock, flags);
  658. if (!neigh) {
  659. neigh = ipoib_neigh_alloc(daddr, dev);
  660. if (neigh) {
  661. kref_get(&mcast->ah->ref);
  662. neigh->ah = mcast->ah;
  663. list_add_tail(&neigh->list, &mcast->neigh_list);
  664. }
  665. }
  666. spin_unlock_irqrestore(&priv->lock, flags);
  667. ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
  668. if (neigh)
  669. ipoib_neigh_put(neigh);
  670. return;
  671. }
  672. unlock:
  673. spin_unlock_irqrestore(&priv->lock, flags);
  674. }
  675. void ipoib_mcast_dev_flush(struct net_device *dev)
  676. {
  677. struct ipoib_dev_priv *priv = netdev_priv(dev);
  678. LIST_HEAD(remove_list);
  679. struct ipoib_mcast *mcast, *tmcast;
  680. unsigned long flags;
  681. ipoib_dbg_mcast(priv, "flushing multicast list\n");
  682. spin_lock_irqsave(&priv->lock, flags);
  683. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  684. list_del(&mcast->list);
  685. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  686. list_add_tail(&mcast->list, &remove_list);
  687. }
  688. if (priv->broadcast) {
  689. rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
  690. list_add_tail(&priv->broadcast->list, &remove_list);
  691. priv->broadcast = NULL;
  692. }
  693. spin_unlock_irqrestore(&priv->lock, flags);
  694. /*
  695. * make sure the in-flight joins have finished before we attempt
  696. * to leave
  697. */
  698. list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
  699. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  700. wait_for_completion(&mcast->done);
  701. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  702. ipoib_mcast_leave(dev, mcast);
  703. ipoib_mcast_free(mcast);
  704. }
  705. }
  706. static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
  707. {
  708. /* reserved QPN, prefix, scope */
  709. if (memcmp(addr, broadcast, 6))
  710. return 0;
  711. /* signature lower, pkey */
  712. if (memcmp(addr + 7, broadcast + 7, 3))
  713. return 0;
  714. return 1;
  715. }
  716. void ipoib_mcast_restart_task(struct work_struct *work)
  717. {
  718. struct ipoib_dev_priv *priv =
  719. container_of(work, struct ipoib_dev_priv, restart_task);
  720. struct net_device *dev = priv->dev;
  721. struct netdev_hw_addr *ha;
  722. struct ipoib_mcast *mcast, *tmcast;
  723. LIST_HEAD(remove_list);
  724. unsigned long flags;
  725. struct ib_sa_mcmember_rec rec;
  726. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  727. /*
  728. * shortcut...on shutdown flush is called next, just
  729. * let it do all the work
  730. */
  731. return;
  732. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  733. local_irq_save(flags);
  734. netif_addr_lock(dev);
  735. spin_lock(&priv->lock);
  736. /*
  737. * Unfortunately, the networking core only gives us a list of all of
  738. * the multicast hardware addresses. We need to figure out which ones
  739. * are new and which ones have been removed
  740. */
  741. /* Clear out the found flag */
  742. list_for_each_entry(mcast, &priv->multicast_list, list)
  743. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  744. /* Mark all of the entries that are found or don't exist */
  745. netdev_for_each_mc_addr(ha, dev) {
  746. union ib_gid mgid;
  747. if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
  748. continue;
  749. memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
  750. mcast = __ipoib_mcast_find(dev, &mgid);
  751. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  752. struct ipoib_mcast *nmcast;
  753. /* ignore group which is directly joined by userspace */
  754. if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
  755. !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
  756. ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
  757. mgid.raw);
  758. continue;
  759. }
  760. /* Not found or send-only group, let's add a new entry */
  761. ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
  762. mgid.raw);
  763. nmcast = ipoib_mcast_alloc(dev, 0);
  764. if (!nmcast) {
  765. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  766. continue;
  767. }
  768. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  769. nmcast->mcmember.mgid = mgid;
  770. if (mcast) {
  771. /* Destroy the send only entry */
  772. list_move_tail(&mcast->list, &remove_list);
  773. rb_replace_node(&mcast->rb_node,
  774. &nmcast->rb_node,
  775. &priv->multicast_tree);
  776. } else
  777. __ipoib_mcast_add(dev, nmcast);
  778. list_add_tail(&nmcast->list, &priv->multicast_list);
  779. }
  780. if (mcast)
  781. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  782. }
  783. /* Remove all of the entries don't exist anymore */
  784. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  785. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  786. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  787. ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
  788. mcast->mcmember.mgid.raw);
  789. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  790. /* Move to the remove list */
  791. list_move_tail(&mcast->list, &remove_list);
  792. }
  793. }
  794. spin_unlock(&priv->lock);
  795. netif_addr_unlock(dev);
  796. local_irq_restore(flags);
  797. /*
  798. * make sure the in-flight joins have finished before we attempt
  799. * to leave
  800. */
  801. list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
  802. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  803. wait_for_completion(&mcast->done);
  804. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  805. ipoib_mcast_leave(mcast->dev, mcast);
  806. ipoib_mcast_free(mcast);
  807. }
  808. /*
  809. * Double check that we are still up
  810. */
  811. if (test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  812. spin_lock_irqsave(&priv->lock, flags);
  813. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  814. spin_unlock_irqrestore(&priv->lock, flags);
  815. }
  816. }
  817. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  818. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  819. {
  820. struct ipoib_mcast_iter *iter;
  821. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  822. if (!iter)
  823. return NULL;
  824. iter->dev = dev;
  825. memset(iter->mgid.raw, 0, 16);
  826. if (ipoib_mcast_iter_next(iter)) {
  827. kfree(iter);
  828. return NULL;
  829. }
  830. return iter;
  831. }
  832. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  833. {
  834. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  835. struct rb_node *n;
  836. struct ipoib_mcast *mcast;
  837. int ret = 1;
  838. spin_lock_irq(&priv->lock);
  839. n = rb_first(&priv->multicast_tree);
  840. while (n) {
  841. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  842. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  843. sizeof (union ib_gid)) < 0) {
  844. iter->mgid = mcast->mcmember.mgid;
  845. iter->created = mcast->created;
  846. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  847. iter->complete = !!mcast->ah;
  848. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  849. ret = 0;
  850. break;
  851. }
  852. n = rb_next(n);
  853. }
  854. spin_unlock_irq(&priv->lock);
  855. return ret;
  856. }
  857. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  858. union ib_gid *mgid,
  859. unsigned long *created,
  860. unsigned int *queuelen,
  861. unsigned int *complete,
  862. unsigned int *send_only)
  863. {
  864. *mgid = iter->mgid;
  865. *created = iter->created;
  866. *queuelen = iter->queuelen;
  867. *complete = iter->complete;
  868. *send_only = iter->send_only;
  869. }
  870. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */