ipoib_multicast.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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/ip.h>
  37. #include <linux/in.h>
  38. #include <linux/igmp.h>
  39. #include <linux/inetdevice.h>
  40. #include <linux/delay.h>
  41. #include <linux/completion.h>
  42. #include <net/dst.h>
  43. #include "ipoib.h"
  44. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  45. static int mcast_debug_level;
  46. module_param(mcast_debug_level, int, 0644);
  47. MODULE_PARM_DESC(mcast_debug_level,
  48. "Enable multicast debug tracing if > 0");
  49. #endif
  50. static DEFINE_MUTEX(mcast_mutex);
  51. struct ipoib_mcast_iter {
  52. struct net_device *dev;
  53. union ib_gid mgid;
  54. unsigned long created;
  55. unsigned int queuelen;
  56. unsigned int complete;
  57. unsigned int send_only;
  58. };
  59. static void ipoib_mcast_free(struct ipoib_mcast *mcast)
  60. {
  61. struct net_device *dev = mcast->dev;
  62. struct ipoib_dev_priv *priv = netdev_priv(dev);
  63. struct ipoib_neigh *neigh, *tmp;
  64. int tx_dropped = 0;
  65. ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
  66. mcast->mcmember.mgid.raw);
  67. spin_lock_irq(&priv->lock);
  68. list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
  69. /*
  70. * It's safe to call ipoib_put_ah() inside priv->lock
  71. * here, because we know that mcast->ah will always
  72. * hold one more reference, so ipoib_put_ah() will
  73. * never do more than decrement the ref count.
  74. */
  75. if (neigh->ah)
  76. ipoib_put_ah(neigh->ah);
  77. ipoib_neigh_free(dev, neigh);
  78. }
  79. spin_unlock_irq(&priv->lock);
  80. if (mcast->ah)
  81. ipoib_put_ah(mcast->ah);
  82. while (!skb_queue_empty(&mcast->pkt_queue)) {
  83. ++tx_dropped;
  84. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  85. }
  86. netif_tx_lock_bh(dev);
  87. dev->stats.tx_dropped += tx_dropped;
  88. netif_tx_unlock_bh(dev);
  89. kfree(mcast);
  90. }
  91. static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
  92. int can_sleep)
  93. {
  94. struct ipoib_mcast *mcast;
  95. mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
  96. if (!mcast)
  97. return NULL;
  98. mcast->dev = dev;
  99. mcast->created = jiffies;
  100. mcast->backoff = 1;
  101. INIT_LIST_HEAD(&mcast->list);
  102. INIT_LIST_HEAD(&mcast->neigh_list);
  103. skb_queue_head_init(&mcast->pkt_queue);
  104. return mcast;
  105. }
  106. static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
  107. {
  108. struct ipoib_dev_priv *priv = netdev_priv(dev);
  109. struct rb_node *n = priv->multicast_tree.rb_node;
  110. while (n) {
  111. struct ipoib_mcast *mcast;
  112. int ret;
  113. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  114. ret = memcmp(mgid, mcast->mcmember.mgid.raw,
  115. sizeof (union ib_gid));
  116. if (ret < 0)
  117. n = n->rb_left;
  118. else if (ret > 0)
  119. n = n->rb_right;
  120. else
  121. return mcast;
  122. }
  123. return NULL;
  124. }
  125. static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
  126. {
  127. struct ipoib_dev_priv *priv = netdev_priv(dev);
  128. struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
  129. while (*n) {
  130. struct ipoib_mcast *tmcast;
  131. int ret;
  132. pn = *n;
  133. tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
  134. ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
  135. sizeof (union ib_gid));
  136. if (ret < 0)
  137. n = &pn->rb_left;
  138. else if (ret > 0)
  139. n = &pn->rb_right;
  140. else
  141. return -EEXIST;
  142. }
  143. rb_link_node(&mcast->rb_node, pn, n);
  144. rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
  145. return 0;
  146. }
  147. static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
  148. struct ib_sa_mcmember_rec *mcmember)
  149. {
  150. struct net_device *dev = mcast->dev;
  151. struct ipoib_dev_priv *priv = netdev_priv(dev);
  152. struct ipoib_ah *ah;
  153. int ret;
  154. int set_qkey = 0;
  155. mcast->mcmember = *mcmember;
  156. /* Set the cached Q_Key before we attach if it's the broadcast group */
  157. if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  158. sizeof (union ib_gid))) {
  159. spin_lock_irq(&priv->lock);
  160. if (!priv->broadcast) {
  161. spin_unlock_irq(&priv->lock);
  162. return -EAGAIN;
  163. }
  164. priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
  165. spin_unlock_irq(&priv->lock);
  166. priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
  167. set_qkey = 1;
  168. }
  169. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  170. if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  171. ipoib_warn(priv, "multicast group %pI6 already attached\n",
  172. mcast->mcmember.mgid.raw);
  173. return 0;
  174. }
  175. ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
  176. &mcast->mcmember.mgid, set_qkey);
  177. if (ret < 0) {
  178. ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
  179. mcast->mcmember.mgid.raw);
  180. clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
  181. return ret;
  182. }
  183. }
  184. {
  185. struct ib_ah_attr av = {
  186. .dlid = be16_to_cpu(mcast->mcmember.mlid),
  187. .port_num = priv->port,
  188. .sl = mcast->mcmember.sl,
  189. .ah_flags = IB_AH_GRH,
  190. .static_rate = mcast->mcmember.rate,
  191. .grh = {
  192. .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
  193. .hop_limit = mcast->mcmember.hop_limit,
  194. .sgid_index = 0,
  195. .traffic_class = mcast->mcmember.traffic_class
  196. }
  197. };
  198. av.grh.dgid = mcast->mcmember.mgid;
  199. ah = ipoib_create_ah(dev, priv->pd, &av);
  200. if (!ah) {
  201. ipoib_warn(priv, "ib_address_create failed\n");
  202. } else {
  203. spin_lock_irq(&priv->lock);
  204. mcast->ah = ah;
  205. spin_unlock_irq(&priv->lock);
  206. ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
  207. mcast->mcmember.mgid.raw,
  208. mcast->ah->ah,
  209. be16_to_cpu(mcast->mcmember.mlid),
  210. mcast->mcmember.sl);
  211. }
  212. }
  213. /* actually send any queued packets */
  214. netif_tx_lock_bh(dev);
  215. while (!skb_queue_empty(&mcast->pkt_queue)) {
  216. struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
  217. netif_tx_unlock_bh(dev);
  218. skb->dev = dev;
  219. if (!skb->dst || !skb->dst->neighbour) {
  220. /* put pseudoheader back on for next time */
  221. skb_push(skb, sizeof (struct ipoib_pseudoheader));
  222. }
  223. if (dev_queue_xmit(skb))
  224. ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
  225. netif_tx_lock_bh(dev);
  226. }
  227. netif_tx_unlock_bh(dev);
  228. return 0;
  229. }
  230. static int
  231. ipoib_mcast_sendonly_join_complete(int status,
  232. struct ib_sa_multicast *multicast)
  233. {
  234. struct ipoib_mcast *mcast = multicast->context;
  235. struct net_device *dev = mcast->dev;
  236. /* We trap for port events ourselves. */
  237. if (status == -ENETRESET)
  238. return 0;
  239. if (!status)
  240. status = ipoib_mcast_join_finish(mcast, &multicast->rec);
  241. if (status) {
  242. if (mcast->logcount++ < 20)
  243. ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for %pI6, status %d\n",
  244. mcast->mcmember.mgid.raw, status);
  245. /* Flush out any queued packets */
  246. netif_tx_lock_bh(dev);
  247. while (!skb_queue_empty(&mcast->pkt_queue)) {
  248. ++dev->stats.tx_dropped;
  249. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  250. }
  251. netif_tx_unlock_bh(dev);
  252. /* Clear the busy flag so we try again */
  253. status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
  254. &mcast->flags);
  255. }
  256. return status;
  257. }
  258. static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
  259. {
  260. struct net_device *dev = mcast->dev;
  261. struct ipoib_dev_priv *priv = netdev_priv(dev);
  262. struct ib_sa_mcmember_rec rec = {
  263. #if 0 /* Some SMs don't support send-only yet */
  264. .join_state = 4
  265. #else
  266. .join_state = 1
  267. #endif
  268. };
  269. int ret = 0;
  270. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  271. ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
  272. return -ENODEV;
  273. }
  274. if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
  275. ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
  276. return -EBUSY;
  277. }
  278. rec.mgid = mcast->mcmember.mgid;
  279. rec.port_gid = priv->local_gid;
  280. rec.pkey = cpu_to_be16(priv->pkey);
  281. mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
  282. priv->port, &rec,
  283. IB_SA_MCMEMBER_REC_MGID |
  284. IB_SA_MCMEMBER_REC_PORT_GID |
  285. IB_SA_MCMEMBER_REC_PKEY |
  286. IB_SA_MCMEMBER_REC_JOIN_STATE,
  287. GFP_ATOMIC,
  288. ipoib_mcast_sendonly_join_complete,
  289. mcast);
  290. if (IS_ERR(mcast->mc)) {
  291. ret = PTR_ERR(mcast->mc);
  292. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  293. ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
  294. ret);
  295. } else {
  296. ipoib_dbg_mcast(priv, "no multicast record for %pI6, starting join\n",
  297. mcast->mcmember.mgid.raw);
  298. }
  299. return ret;
  300. }
  301. void ipoib_mcast_carrier_on_task(struct work_struct *work)
  302. {
  303. struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
  304. carrier_on_task);
  305. /*
  306. * Take rtnl_lock to avoid racing with ipoib_stop() and
  307. * turning the carrier back on while a device is being
  308. * removed.
  309. */
  310. rtnl_lock();
  311. netif_carrier_on(priv->dev);
  312. rtnl_unlock();
  313. }
  314. static int ipoib_mcast_join_complete(int status,
  315. struct ib_sa_multicast *multicast)
  316. {
  317. struct ipoib_mcast *mcast = multicast->context;
  318. struct net_device *dev = mcast->dev;
  319. struct ipoib_dev_priv *priv = netdev_priv(dev);
  320. ipoib_dbg_mcast(priv, "join completion for %pI6 (status %d)\n",
  321. mcast->mcmember.mgid.raw, status);
  322. /* We trap for port events ourselves. */
  323. if (status == -ENETRESET)
  324. return 0;
  325. if (!status)
  326. status = ipoib_mcast_join_finish(mcast, &multicast->rec);
  327. if (!status) {
  328. mcast->backoff = 1;
  329. mutex_lock(&mcast_mutex);
  330. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  331. queue_delayed_work(ipoib_workqueue,
  332. &priv->mcast_task, 0);
  333. mutex_unlock(&mcast_mutex);
  334. /*
  335. * Defer carrier on work to ipoib_workqueue to avoid a
  336. * deadlock on rtnl_lock here.
  337. */
  338. if (mcast == priv->broadcast)
  339. queue_work(ipoib_workqueue, &priv->carrier_on_task);
  340. return 0;
  341. }
  342. if (mcast->logcount++ < 20) {
  343. if (status == -ETIMEDOUT || status == -EAGAIN) {
  344. ipoib_dbg_mcast(priv, "multicast join failed for %pI6, status %d\n",
  345. mcast->mcmember.mgid.raw, status);
  346. } else {
  347. ipoib_warn(priv, "multicast join failed for %pI6, status %d\n",
  348. mcast->mcmember.mgid.raw, status);
  349. }
  350. }
  351. mcast->backoff *= 2;
  352. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  353. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  354. /* Clear the busy flag so we try again */
  355. status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  356. mutex_lock(&mcast_mutex);
  357. spin_lock_irq(&priv->lock);
  358. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  359. queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
  360. mcast->backoff * HZ);
  361. spin_unlock_irq(&priv->lock);
  362. mutex_unlock(&mcast_mutex);
  363. return status;
  364. }
  365. static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
  366. int create)
  367. {
  368. struct ipoib_dev_priv *priv = netdev_priv(dev);
  369. struct ib_sa_mcmember_rec rec = {
  370. .join_state = 1
  371. };
  372. ib_sa_comp_mask comp_mask;
  373. int ret = 0;
  374. ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
  375. rec.mgid = mcast->mcmember.mgid;
  376. rec.port_gid = priv->local_gid;
  377. rec.pkey = cpu_to_be16(priv->pkey);
  378. comp_mask =
  379. IB_SA_MCMEMBER_REC_MGID |
  380. IB_SA_MCMEMBER_REC_PORT_GID |
  381. IB_SA_MCMEMBER_REC_PKEY |
  382. IB_SA_MCMEMBER_REC_JOIN_STATE;
  383. if (create) {
  384. comp_mask |=
  385. IB_SA_MCMEMBER_REC_QKEY |
  386. IB_SA_MCMEMBER_REC_MTU_SELECTOR |
  387. IB_SA_MCMEMBER_REC_MTU |
  388. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
  389. IB_SA_MCMEMBER_REC_RATE_SELECTOR |
  390. IB_SA_MCMEMBER_REC_RATE |
  391. IB_SA_MCMEMBER_REC_SL |
  392. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  393. IB_SA_MCMEMBER_REC_HOP_LIMIT;
  394. rec.qkey = priv->broadcast->mcmember.qkey;
  395. rec.mtu_selector = IB_SA_EQ;
  396. rec.mtu = priv->broadcast->mcmember.mtu;
  397. rec.traffic_class = priv->broadcast->mcmember.traffic_class;
  398. rec.rate_selector = IB_SA_EQ;
  399. rec.rate = priv->broadcast->mcmember.rate;
  400. rec.sl = priv->broadcast->mcmember.sl;
  401. rec.flow_label = priv->broadcast->mcmember.flow_label;
  402. rec.hop_limit = priv->broadcast->mcmember.hop_limit;
  403. }
  404. set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  405. mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
  406. &rec, comp_mask, GFP_KERNEL,
  407. ipoib_mcast_join_complete, mcast);
  408. if (IS_ERR(mcast->mc)) {
  409. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  410. ret = PTR_ERR(mcast->mc);
  411. ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
  412. mcast->backoff *= 2;
  413. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  414. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  415. mutex_lock(&mcast_mutex);
  416. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  417. queue_delayed_work(ipoib_workqueue,
  418. &priv->mcast_task,
  419. mcast->backoff * HZ);
  420. mutex_unlock(&mcast_mutex);
  421. }
  422. }
  423. void ipoib_mcast_join_task(struct work_struct *work)
  424. {
  425. struct ipoib_dev_priv *priv =
  426. container_of(work, struct ipoib_dev_priv, mcast_task.work);
  427. struct net_device *dev = priv->dev;
  428. if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
  429. return;
  430. if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
  431. ipoib_warn(priv, "ib_query_gid() failed\n");
  432. else
  433. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  434. {
  435. struct ib_port_attr attr;
  436. if (!ib_query_port(priv->ca, priv->port, &attr))
  437. priv->local_lid = attr.lid;
  438. else
  439. ipoib_warn(priv, "ib_query_port failed\n");
  440. }
  441. if (!priv->broadcast) {
  442. struct ipoib_mcast *broadcast;
  443. if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  444. return;
  445. broadcast = ipoib_mcast_alloc(dev, 1);
  446. if (!broadcast) {
  447. ipoib_warn(priv, "failed to allocate broadcast group\n");
  448. mutex_lock(&mcast_mutex);
  449. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  450. queue_delayed_work(ipoib_workqueue,
  451. &priv->mcast_task, HZ);
  452. mutex_unlock(&mcast_mutex);
  453. return;
  454. }
  455. spin_lock_irq(&priv->lock);
  456. memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  457. sizeof (union ib_gid));
  458. priv->broadcast = broadcast;
  459. __ipoib_mcast_add(dev, priv->broadcast);
  460. spin_unlock_irq(&priv->lock);
  461. }
  462. if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  463. if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
  464. ipoib_mcast_join(dev, priv->broadcast, 0);
  465. return;
  466. }
  467. while (1) {
  468. struct ipoib_mcast *mcast = NULL;
  469. spin_lock_irq(&priv->lock);
  470. list_for_each_entry(mcast, &priv->multicast_list, list) {
  471. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
  472. && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
  473. && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  474. /* Found the next unjoined group */
  475. break;
  476. }
  477. }
  478. spin_unlock_irq(&priv->lock);
  479. if (&mcast->list == &priv->multicast_list) {
  480. /* All done */
  481. break;
  482. }
  483. ipoib_mcast_join(dev, mcast, 1);
  484. return;
  485. }
  486. priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  487. if (!ipoib_cm_admin_enabled(dev)) {
  488. rtnl_lock();
  489. dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
  490. rtnl_unlock();
  491. }
  492. ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
  493. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  494. }
  495. int ipoib_mcast_start_thread(struct net_device *dev)
  496. {
  497. struct ipoib_dev_priv *priv = netdev_priv(dev);
  498. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  499. mutex_lock(&mcast_mutex);
  500. if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
  501. queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
  502. mutex_unlock(&mcast_mutex);
  503. return 0;
  504. }
  505. int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
  506. {
  507. struct ipoib_dev_priv *priv = netdev_priv(dev);
  508. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  509. mutex_lock(&mcast_mutex);
  510. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  511. cancel_delayed_work(&priv->mcast_task);
  512. mutex_unlock(&mcast_mutex);
  513. if (flush)
  514. flush_workqueue(ipoib_workqueue);
  515. return 0;
  516. }
  517. static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  518. {
  519. struct ipoib_dev_priv *priv = netdev_priv(dev);
  520. int ret = 0;
  521. if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  522. ib_sa_free_multicast(mcast->mc);
  523. if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  524. ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
  525. mcast->mcmember.mgid.raw);
  526. /* Remove ourselves from the multicast group */
  527. ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
  528. be16_to_cpu(mcast->mcmember.mlid));
  529. if (ret)
  530. ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
  531. }
  532. return 0;
  533. }
  534. void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
  535. {
  536. struct ipoib_dev_priv *priv = netdev_priv(dev);
  537. struct ipoib_mcast *mcast;
  538. unsigned long flags;
  539. spin_lock_irqsave(&priv->lock, flags);
  540. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
  541. !priv->broadcast ||
  542. !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  543. ++dev->stats.tx_dropped;
  544. dev_kfree_skb_any(skb);
  545. goto unlock;
  546. }
  547. mcast = __ipoib_mcast_find(dev, mgid);
  548. if (!mcast) {
  549. /* Let's create a new send only group now */
  550. ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
  551. mgid);
  552. mcast = ipoib_mcast_alloc(dev, 0);
  553. if (!mcast) {
  554. ipoib_warn(priv, "unable to allocate memory for "
  555. "multicast structure\n");
  556. ++dev->stats.tx_dropped;
  557. dev_kfree_skb_any(skb);
  558. goto out;
  559. }
  560. set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
  561. memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
  562. __ipoib_mcast_add(dev, mcast);
  563. list_add_tail(&mcast->list, &priv->multicast_list);
  564. }
  565. if (!mcast->ah) {
  566. if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
  567. skb_queue_tail(&mcast->pkt_queue, skb);
  568. else {
  569. ++dev->stats.tx_dropped;
  570. dev_kfree_skb_any(skb);
  571. }
  572. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  573. ipoib_dbg_mcast(priv, "no address vector, "
  574. "but multicast join already started\n");
  575. else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  576. ipoib_mcast_sendonly_join(mcast);
  577. /*
  578. * If lookup completes between here and out:, don't
  579. * want to send packet twice.
  580. */
  581. mcast = NULL;
  582. }
  583. out:
  584. if (mcast && mcast->ah) {
  585. if (skb->dst &&
  586. skb->dst->neighbour &&
  587. !*to_ipoib_neigh(skb->dst->neighbour)) {
  588. struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour,
  589. skb->dev);
  590. if (neigh) {
  591. kref_get(&mcast->ah->ref);
  592. neigh->ah = mcast->ah;
  593. list_add_tail(&neigh->list, &mcast->neigh_list);
  594. }
  595. }
  596. ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
  597. }
  598. unlock:
  599. spin_unlock_irqrestore(&priv->lock, flags);
  600. }
  601. void ipoib_mcast_dev_flush(struct net_device *dev)
  602. {
  603. struct ipoib_dev_priv *priv = netdev_priv(dev);
  604. LIST_HEAD(remove_list);
  605. struct ipoib_mcast *mcast, *tmcast;
  606. unsigned long flags;
  607. ipoib_dbg_mcast(priv, "flushing multicast list\n");
  608. spin_lock_irqsave(&priv->lock, flags);
  609. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  610. list_del(&mcast->list);
  611. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  612. list_add_tail(&mcast->list, &remove_list);
  613. }
  614. if (priv->broadcast) {
  615. rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
  616. list_add_tail(&priv->broadcast->list, &remove_list);
  617. priv->broadcast = NULL;
  618. }
  619. spin_unlock_irqrestore(&priv->lock, flags);
  620. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  621. ipoib_mcast_leave(dev, mcast);
  622. ipoib_mcast_free(mcast);
  623. }
  624. }
  625. void ipoib_mcast_restart_task(struct work_struct *work)
  626. {
  627. struct ipoib_dev_priv *priv =
  628. container_of(work, struct ipoib_dev_priv, restart_task);
  629. struct net_device *dev = priv->dev;
  630. struct dev_mc_list *mclist;
  631. struct ipoib_mcast *mcast, *tmcast;
  632. LIST_HEAD(remove_list);
  633. unsigned long flags;
  634. struct ib_sa_mcmember_rec rec;
  635. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  636. ipoib_mcast_stop_thread(dev, 0);
  637. local_irq_save(flags);
  638. netif_addr_lock(dev);
  639. spin_lock(&priv->lock);
  640. /*
  641. * Unfortunately, the networking core only gives us a list of all of
  642. * the multicast hardware addresses. We need to figure out which ones
  643. * are new and which ones have been removed
  644. */
  645. /* Clear out the found flag */
  646. list_for_each_entry(mcast, &priv->multicast_list, list)
  647. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  648. /* Mark all of the entries that are found or don't exist */
  649. for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
  650. union ib_gid mgid;
  651. memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
  652. mcast = __ipoib_mcast_find(dev, &mgid);
  653. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  654. struct ipoib_mcast *nmcast;
  655. /* ignore group which is directly joined by userspace */
  656. if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
  657. !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
  658. ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
  659. mgid.raw);
  660. continue;
  661. }
  662. /* Not found or send-only group, let's add a new entry */
  663. ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
  664. mgid.raw);
  665. nmcast = ipoib_mcast_alloc(dev, 0);
  666. if (!nmcast) {
  667. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  668. continue;
  669. }
  670. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  671. nmcast->mcmember.mgid = mgid;
  672. if (mcast) {
  673. /* Destroy the send only entry */
  674. list_move_tail(&mcast->list, &remove_list);
  675. rb_replace_node(&mcast->rb_node,
  676. &nmcast->rb_node,
  677. &priv->multicast_tree);
  678. } else
  679. __ipoib_mcast_add(dev, nmcast);
  680. list_add_tail(&nmcast->list, &priv->multicast_list);
  681. }
  682. if (mcast)
  683. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  684. }
  685. /* Remove all of the entries don't exist anymore */
  686. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  687. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  688. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  689. ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
  690. mcast->mcmember.mgid.raw);
  691. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  692. /* Move to the remove list */
  693. list_move_tail(&mcast->list, &remove_list);
  694. }
  695. }
  696. spin_unlock(&priv->lock);
  697. netif_addr_unlock(dev);
  698. local_irq_restore(flags);
  699. /* We have to cancel outside of the spinlock */
  700. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  701. ipoib_mcast_leave(mcast->dev, mcast);
  702. ipoib_mcast_free(mcast);
  703. }
  704. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  705. ipoib_mcast_start_thread(dev);
  706. }
  707. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  708. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  709. {
  710. struct ipoib_mcast_iter *iter;
  711. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  712. if (!iter)
  713. return NULL;
  714. iter->dev = dev;
  715. memset(iter->mgid.raw, 0, 16);
  716. if (ipoib_mcast_iter_next(iter)) {
  717. kfree(iter);
  718. return NULL;
  719. }
  720. return iter;
  721. }
  722. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  723. {
  724. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  725. struct rb_node *n;
  726. struct ipoib_mcast *mcast;
  727. int ret = 1;
  728. spin_lock_irq(&priv->lock);
  729. n = rb_first(&priv->multicast_tree);
  730. while (n) {
  731. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  732. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  733. sizeof (union ib_gid)) < 0) {
  734. iter->mgid = mcast->mcmember.mgid;
  735. iter->created = mcast->created;
  736. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  737. iter->complete = !!mcast->ah;
  738. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  739. ret = 0;
  740. break;
  741. }
  742. n = rb_next(n);
  743. }
  744. spin_unlock_irq(&priv->lock);
  745. return ret;
  746. }
  747. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  748. union ib_gid *mgid,
  749. unsigned long *created,
  750. unsigned int *queuelen,
  751. unsigned int *complete,
  752. unsigned int *send_only)
  753. {
  754. *mgid = iter->mgid;
  755. *created = iter->created;
  756. *queuelen = iter->queuelen;
  757. *complete = iter->complete;
  758. *send_only = iter->send_only;
  759. }
  760. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */