ipoib_multicast.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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.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. /*
  399. * Caller must hold 'priv->lock'
  400. */
  401. static int ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast)
  402. {
  403. struct ipoib_dev_priv *priv = netdev_priv(dev);
  404. struct ib_sa_multicast *multicast;
  405. struct ib_sa_mcmember_rec rec = {
  406. .join_state = 1
  407. };
  408. ib_sa_comp_mask comp_mask;
  409. int ret = 0;
  410. if (!priv->broadcast ||
  411. !test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  412. return -EINVAL;
  413. init_completion(&mcast->done);
  414. set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  415. ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
  416. rec.mgid = mcast->mcmember.mgid;
  417. rec.port_gid = priv->local_gid;
  418. rec.pkey = cpu_to_be16(priv->pkey);
  419. comp_mask =
  420. IB_SA_MCMEMBER_REC_MGID |
  421. IB_SA_MCMEMBER_REC_PORT_GID |
  422. IB_SA_MCMEMBER_REC_PKEY |
  423. IB_SA_MCMEMBER_REC_JOIN_STATE;
  424. if (mcast != priv->broadcast) {
  425. /*
  426. * RFC 4391:
  427. * The MGID MUST use the same P_Key, Q_Key, SL, MTU,
  428. * and HopLimit as those used in the broadcast-GID. The rest
  429. * of attributes SHOULD follow the values used in the
  430. * broadcast-GID as well.
  431. */
  432. comp_mask |=
  433. IB_SA_MCMEMBER_REC_QKEY |
  434. IB_SA_MCMEMBER_REC_MTU_SELECTOR |
  435. IB_SA_MCMEMBER_REC_MTU |
  436. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
  437. IB_SA_MCMEMBER_REC_RATE_SELECTOR |
  438. IB_SA_MCMEMBER_REC_RATE |
  439. IB_SA_MCMEMBER_REC_SL |
  440. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  441. IB_SA_MCMEMBER_REC_HOP_LIMIT;
  442. rec.qkey = priv->broadcast->mcmember.qkey;
  443. rec.mtu_selector = IB_SA_EQ;
  444. rec.mtu = priv->broadcast->mcmember.mtu;
  445. rec.traffic_class = priv->broadcast->mcmember.traffic_class;
  446. rec.rate_selector = IB_SA_EQ;
  447. rec.rate = priv->broadcast->mcmember.rate;
  448. rec.sl = priv->broadcast->mcmember.sl;
  449. rec.flow_label = priv->broadcast->mcmember.flow_label;
  450. rec.hop_limit = priv->broadcast->mcmember.hop_limit;
  451. /*
  452. * Send-only IB Multicast joins do not work at the core
  453. * IB layer yet, so we can't use them here. However,
  454. * we are emulating an Ethernet multicast send, which
  455. * does not require a multicast subscription and will
  456. * still send properly. The most appropriate thing to
  457. * do is to create the group if it doesn't exist as that
  458. * most closely emulates the behavior, from a user space
  459. * application perspecitive, of Ethernet multicast
  460. * operation. For now, we do a full join, maybe later
  461. * when the core IB layers support send only joins we
  462. * will use them.
  463. */
  464. #if 0
  465. if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  466. rec.join_state = 4;
  467. #endif
  468. }
  469. spin_unlock_irq(&priv->lock);
  470. multicast = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
  471. &rec, comp_mask, GFP_KERNEL,
  472. ipoib_mcast_join_complete, mcast);
  473. spin_lock_irq(&priv->lock);
  474. if (IS_ERR(multicast)) {
  475. ret = PTR_ERR(multicast);
  476. ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
  477. /* Requeue this join task with a backoff delay */
  478. __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
  479. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  480. spin_unlock_irq(&priv->lock);
  481. complete(&mcast->done);
  482. spin_lock_irq(&priv->lock);
  483. }
  484. return 0;
  485. }
  486. void ipoib_mcast_join_task(struct work_struct *work)
  487. {
  488. struct ipoib_dev_priv *priv =
  489. container_of(work, struct ipoib_dev_priv, mcast_task.work);
  490. struct net_device *dev = priv->dev;
  491. struct ib_port_attr port_attr;
  492. unsigned long delay_until = 0;
  493. struct ipoib_mcast *mcast = NULL;
  494. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  495. return;
  496. if (ib_query_port(priv->ca, priv->port, &port_attr)) {
  497. ipoib_dbg(priv, "ib_query_port() failed\n");
  498. return;
  499. }
  500. if (port_attr.state != IB_PORT_ACTIVE) {
  501. ipoib_dbg(priv, "port state is not ACTIVE (state = %d) suspending join task\n",
  502. port_attr.state);
  503. return;
  504. }
  505. priv->local_lid = port_attr.lid;
  506. if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid, NULL))
  507. ipoib_warn(priv, "ib_query_gid() failed\n");
  508. else
  509. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  510. spin_lock_irq(&priv->lock);
  511. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  512. goto out;
  513. if (!priv->broadcast) {
  514. struct ipoib_mcast *broadcast;
  515. broadcast = ipoib_mcast_alloc(dev, 0);
  516. if (!broadcast) {
  517. ipoib_warn(priv, "failed to allocate broadcast group\n");
  518. /*
  519. * Restart us after a 1 second delay to retry
  520. * creating our broadcast group and attaching to
  521. * it. Until this succeeds, this ipoib dev is
  522. * completely stalled (multicast wise).
  523. */
  524. __ipoib_mcast_schedule_join_thread(priv, NULL, 1);
  525. goto out;
  526. }
  527. memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  528. sizeof (union ib_gid));
  529. priv->broadcast = broadcast;
  530. __ipoib_mcast_add(dev, priv->broadcast);
  531. }
  532. if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  533. if (IS_ERR_OR_NULL(priv->broadcast->mc) &&
  534. !test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags)) {
  535. mcast = priv->broadcast;
  536. if (mcast->backoff > 1 &&
  537. time_before(jiffies, mcast->delay_until)) {
  538. delay_until = mcast->delay_until;
  539. mcast = NULL;
  540. }
  541. }
  542. goto out;
  543. }
  544. /*
  545. * We'll never get here until the broadcast group is both allocated
  546. * and attached
  547. */
  548. list_for_each_entry(mcast, &priv->multicast_list, list) {
  549. if (IS_ERR_OR_NULL(mcast->mc) &&
  550. !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags) &&
  551. (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ||
  552. !skb_queue_empty(&mcast->pkt_queue))) {
  553. if (mcast->backoff == 1 ||
  554. time_after_eq(jiffies, mcast->delay_until)) {
  555. /* Found the next unjoined group */
  556. if (ipoib_mcast_join(dev, mcast)) {
  557. spin_unlock_irq(&priv->lock);
  558. return;
  559. }
  560. } else if (!delay_until ||
  561. time_before(mcast->delay_until, delay_until))
  562. delay_until = mcast->delay_until;
  563. }
  564. }
  565. mcast = NULL;
  566. ipoib_dbg_mcast(priv, "successfully started all multicast joins\n");
  567. out:
  568. if (delay_until) {
  569. cancel_delayed_work(&priv->mcast_task);
  570. queue_delayed_work(priv->wq, &priv->mcast_task,
  571. delay_until - jiffies);
  572. }
  573. if (mcast)
  574. ipoib_mcast_join(dev, mcast);
  575. spin_unlock_irq(&priv->lock);
  576. }
  577. int ipoib_mcast_start_thread(struct net_device *dev)
  578. {
  579. struct ipoib_dev_priv *priv = netdev_priv(dev);
  580. unsigned long flags;
  581. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  582. spin_lock_irqsave(&priv->lock, flags);
  583. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  584. spin_unlock_irqrestore(&priv->lock, flags);
  585. return 0;
  586. }
  587. int ipoib_mcast_stop_thread(struct net_device *dev)
  588. {
  589. struct ipoib_dev_priv *priv = netdev_priv(dev);
  590. unsigned long flags;
  591. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  592. spin_lock_irqsave(&priv->lock, flags);
  593. cancel_delayed_work(&priv->mcast_task);
  594. spin_unlock_irqrestore(&priv->lock, flags);
  595. flush_workqueue(priv->wq);
  596. return 0;
  597. }
  598. int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  599. {
  600. struct ipoib_dev_priv *priv = netdev_priv(dev);
  601. int ret = 0;
  602. if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  603. ipoib_warn(priv, "ipoib_mcast_leave on an in-flight join\n");
  604. if (!IS_ERR_OR_NULL(mcast->mc))
  605. ib_sa_free_multicast(mcast->mc);
  606. if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  607. ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
  608. mcast->mcmember.mgid.raw);
  609. /* Remove ourselves from the multicast group */
  610. ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
  611. be16_to_cpu(mcast->mcmember.mlid));
  612. if (ret)
  613. ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
  614. } else if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  615. ipoib_dbg(priv, "leaving with no mcmember but not a "
  616. "SENDONLY join\n");
  617. return 0;
  618. }
  619. void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
  620. {
  621. struct ipoib_dev_priv *priv = netdev_priv(dev);
  622. struct ipoib_mcast *mcast;
  623. unsigned long flags;
  624. void *mgid = daddr + 4;
  625. spin_lock_irqsave(&priv->lock, flags);
  626. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
  627. !priv->broadcast ||
  628. !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  629. ++dev->stats.tx_dropped;
  630. dev_kfree_skb_any(skb);
  631. goto unlock;
  632. }
  633. mcast = __ipoib_mcast_find(dev, mgid);
  634. if (!mcast || !mcast->ah) {
  635. if (!mcast) {
  636. /* Let's create a new send only group now */
  637. ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
  638. mgid);
  639. mcast = ipoib_mcast_alloc(dev, 0);
  640. if (!mcast) {
  641. ipoib_warn(priv, "unable to allocate memory "
  642. "for multicast structure\n");
  643. ++dev->stats.tx_dropped;
  644. dev_kfree_skb_any(skb);
  645. goto unlock;
  646. }
  647. set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
  648. memcpy(mcast->mcmember.mgid.raw, mgid,
  649. sizeof (union ib_gid));
  650. __ipoib_mcast_add(dev, mcast);
  651. list_add_tail(&mcast->list, &priv->multicast_list);
  652. }
  653. if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE) {
  654. /* put pseudoheader back on for next time */
  655. skb_push(skb, sizeof(struct ipoib_pseudo_header));
  656. skb_queue_tail(&mcast->pkt_queue, skb);
  657. } else {
  658. ++dev->stats.tx_dropped;
  659. dev_kfree_skb_any(skb);
  660. }
  661. if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
  662. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  663. }
  664. } else {
  665. struct ipoib_neigh *neigh;
  666. spin_unlock_irqrestore(&priv->lock, flags);
  667. neigh = ipoib_neigh_get(dev, daddr);
  668. spin_lock_irqsave(&priv->lock, flags);
  669. if (!neigh) {
  670. neigh = ipoib_neigh_alloc(daddr, dev);
  671. /* Make sure that the neigh will be added only
  672. * once to mcast list.
  673. */
  674. if (neigh && list_empty(&neigh->list)) {
  675. kref_get(&mcast->ah->ref);
  676. neigh->ah = mcast->ah;
  677. list_add_tail(&neigh->list, &mcast->neigh_list);
  678. }
  679. }
  680. spin_unlock_irqrestore(&priv->lock, flags);
  681. ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
  682. if (neigh)
  683. ipoib_neigh_put(neigh);
  684. return;
  685. }
  686. unlock:
  687. spin_unlock_irqrestore(&priv->lock, flags);
  688. }
  689. void ipoib_mcast_dev_flush(struct net_device *dev)
  690. {
  691. struct ipoib_dev_priv *priv = netdev_priv(dev);
  692. LIST_HEAD(remove_list);
  693. struct ipoib_mcast *mcast, *tmcast;
  694. unsigned long flags;
  695. ipoib_dbg_mcast(priv, "flushing multicast list\n");
  696. spin_lock_irqsave(&priv->lock, flags);
  697. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  698. list_del(&mcast->list);
  699. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  700. list_add_tail(&mcast->list, &remove_list);
  701. }
  702. if (priv->broadcast) {
  703. rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
  704. list_add_tail(&priv->broadcast->list, &remove_list);
  705. priv->broadcast = NULL;
  706. }
  707. spin_unlock_irqrestore(&priv->lock, flags);
  708. /*
  709. * make sure the in-flight joins have finished before we attempt
  710. * to leave
  711. */
  712. list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
  713. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  714. wait_for_completion(&mcast->done);
  715. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  716. ipoib_mcast_leave(dev, mcast);
  717. ipoib_mcast_free(mcast);
  718. }
  719. }
  720. static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
  721. {
  722. /* reserved QPN, prefix, scope */
  723. if (memcmp(addr, broadcast, 6))
  724. return 0;
  725. /* signature lower, pkey */
  726. if (memcmp(addr + 7, broadcast + 7, 3))
  727. return 0;
  728. return 1;
  729. }
  730. void ipoib_mcast_restart_task(struct work_struct *work)
  731. {
  732. struct ipoib_dev_priv *priv =
  733. container_of(work, struct ipoib_dev_priv, restart_task);
  734. struct net_device *dev = priv->dev;
  735. struct netdev_hw_addr *ha;
  736. struct ipoib_mcast *mcast, *tmcast;
  737. LIST_HEAD(remove_list);
  738. unsigned long flags;
  739. struct ib_sa_mcmember_rec rec;
  740. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  741. /*
  742. * shortcut...on shutdown flush is called next, just
  743. * let it do all the work
  744. */
  745. return;
  746. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  747. local_irq_save(flags);
  748. netif_addr_lock(dev);
  749. spin_lock(&priv->lock);
  750. /*
  751. * Unfortunately, the networking core only gives us a list of all of
  752. * the multicast hardware addresses. We need to figure out which ones
  753. * are new and which ones have been removed
  754. */
  755. /* Clear out the found flag */
  756. list_for_each_entry(mcast, &priv->multicast_list, list)
  757. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  758. /* Mark all of the entries that are found or don't exist */
  759. netdev_for_each_mc_addr(ha, dev) {
  760. union ib_gid mgid;
  761. if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
  762. continue;
  763. memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
  764. mcast = __ipoib_mcast_find(dev, &mgid);
  765. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  766. struct ipoib_mcast *nmcast;
  767. /* ignore group which is directly joined by userspace */
  768. if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
  769. !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
  770. ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
  771. mgid.raw);
  772. continue;
  773. }
  774. /* Not found or send-only group, let's add a new entry */
  775. ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
  776. mgid.raw);
  777. nmcast = ipoib_mcast_alloc(dev, 0);
  778. if (!nmcast) {
  779. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  780. continue;
  781. }
  782. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  783. nmcast->mcmember.mgid = mgid;
  784. if (mcast) {
  785. /* Destroy the send only entry */
  786. list_move_tail(&mcast->list, &remove_list);
  787. rb_replace_node(&mcast->rb_node,
  788. &nmcast->rb_node,
  789. &priv->multicast_tree);
  790. } else
  791. __ipoib_mcast_add(dev, nmcast);
  792. list_add_tail(&nmcast->list, &priv->multicast_list);
  793. }
  794. if (mcast)
  795. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  796. }
  797. /* Remove all of the entries don't exist anymore */
  798. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  799. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  800. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  801. ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
  802. mcast->mcmember.mgid.raw);
  803. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  804. /* Move to the remove list */
  805. list_move_tail(&mcast->list, &remove_list);
  806. }
  807. }
  808. spin_unlock(&priv->lock);
  809. netif_addr_unlock(dev);
  810. local_irq_restore(flags);
  811. /*
  812. * make sure the in-flight joins have finished before we attempt
  813. * to leave
  814. */
  815. list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
  816. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  817. wait_for_completion(&mcast->done);
  818. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  819. ipoib_mcast_leave(mcast->dev, mcast);
  820. ipoib_mcast_free(mcast);
  821. }
  822. /*
  823. * Double check that we are still up
  824. */
  825. if (test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  826. spin_lock_irqsave(&priv->lock, flags);
  827. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  828. spin_unlock_irqrestore(&priv->lock, flags);
  829. }
  830. }
  831. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  832. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  833. {
  834. struct ipoib_mcast_iter *iter;
  835. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  836. if (!iter)
  837. return NULL;
  838. iter->dev = dev;
  839. memset(iter->mgid.raw, 0, 16);
  840. if (ipoib_mcast_iter_next(iter)) {
  841. kfree(iter);
  842. return NULL;
  843. }
  844. return iter;
  845. }
  846. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  847. {
  848. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  849. struct rb_node *n;
  850. struct ipoib_mcast *mcast;
  851. int ret = 1;
  852. spin_lock_irq(&priv->lock);
  853. n = rb_first(&priv->multicast_tree);
  854. while (n) {
  855. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  856. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  857. sizeof (union ib_gid)) < 0) {
  858. iter->mgid = mcast->mcmember.mgid;
  859. iter->created = mcast->created;
  860. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  861. iter->complete = !!mcast->ah;
  862. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  863. ret = 0;
  864. break;
  865. }
  866. n = rb_next(n);
  867. }
  868. spin_unlock_irq(&priv->lock);
  869. return ret;
  870. }
  871. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  872. union ib_gid *mgid,
  873. unsigned long *created,
  874. unsigned int *queuelen,
  875. unsigned int *complete,
  876. unsigned int *send_only)
  877. {
  878. *mgid = iter->mgid;
  879. *created = iter->created;
  880. *queuelen = iter->queuelen;
  881. *complete = iter->complete;
  882. *send_only = iter->send_only;
  883. }
  884. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */