ipoib_multicast.c 30 KB

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