ipoib_multicast.c 29 KB

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