ipoib_multicast.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934
  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) {
  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. broadcast = ipoib_mcast_alloc(dev, 1);
  444. if (!broadcast) {
  445. ipoib_warn(priv, "failed to allocate broadcast group\n");
  446. mutex_lock(&mcast_mutex);
  447. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  448. queue_delayed_work(ipoib_workqueue,
  449. &priv->mcast_task, HZ);
  450. mutex_unlock(&mcast_mutex);
  451. return;
  452. }
  453. spin_lock_irq(&priv->lock);
  454. memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  455. sizeof (union ib_gid));
  456. priv->broadcast = broadcast;
  457. __ipoib_mcast_add(dev, priv->broadcast);
  458. spin_unlock_irq(&priv->lock);
  459. }
  460. if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  461. if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
  462. ipoib_mcast_join(dev, priv->broadcast, 0);
  463. return;
  464. }
  465. while (1) {
  466. struct ipoib_mcast *mcast = NULL;
  467. spin_lock_irq(&priv->lock);
  468. list_for_each_entry(mcast, &priv->multicast_list, list) {
  469. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
  470. && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
  471. && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  472. /* Found the next unjoined group */
  473. break;
  474. }
  475. }
  476. spin_unlock_irq(&priv->lock);
  477. if (&mcast->list == &priv->multicast_list) {
  478. /* All done */
  479. break;
  480. }
  481. ipoib_mcast_join(dev, mcast, 1);
  482. return;
  483. }
  484. priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  485. if (!ipoib_cm_admin_enabled(dev)) {
  486. rtnl_lock();
  487. dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
  488. rtnl_unlock();
  489. }
  490. ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
  491. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  492. }
  493. int ipoib_mcast_start_thread(struct net_device *dev)
  494. {
  495. struct ipoib_dev_priv *priv = netdev_priv(dev);
  496. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  497. mutex_lock(&mcast_mutex);
  498. if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
  499. queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
  500. mutex_unlock(&mcast_mutex);
  501. return 0;
  502. }
  503. int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
  504. {
  505. struct ipoib_dev_priv *priv = netdev_priv(dev);
  506. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  507. mutex_lock(&mcast_mutex);
  508. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  509. cancel_delayed_work(&priv->mcast_task);
  510. mutex_unlock(&mcast_mutex);
  511. if (flush)
  512. flush_workqueue(ipoib_workqueue);
  513. return 0;
  514. }
  515. static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  516. {
  517. struct ipoib_dev_priv *priv = netdev_priv(dev);
  518. int ret = 0;
  519. if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  520. ib_sa_free_multicast(mcast->mc);
  521. if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  522. ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
  523. mcast->mcmember.mgid.raw);
  524. /* Remove ourselves from the multicast group */
  525. ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
  526. be16_to_cpu(mcast->mcmember.mlid));
  527. if (ret)
  528. ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
  529. }
  530. return 0;
  531. }
  532. void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
  533. {
  534. struct ipoib_dev_priv *priv = netdev_priv(dev);
  535. struct ipoib_mcast *mcast;
  536. unsigned long flags;
  537. spin_lock_irqsave(&priv->lock, flags);
  538. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
  539. !priv->broadcast ||
  540. !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  541. ++dev->stats.tx_dropped;
  542. dev_kfree_skb_any(skb);
  543. goto unlock;
  544. }
  545. mcast = __ipoib_mcast_find(dev, mgid);
  546. if (!mcast) {
  547. /* Let's create a new send only group now */
  548. ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
  549. mgid);
  550. mcast = ipoib_mcast_alloc(dev, 0);
  551. if (!mcast) {
  552. ipoib_warn(priv, "unable to allocate memory for "
  553. "multicast structure\n");
  554. ++dev->stats.tx_dropped;
  555. dev_kfree_skb_any(skb);
  556. goto out;
  557. }
  558. set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
  559. memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
  560. __ipoib_mcast_add(dev, mcast);
  561. list_add_tail(&mcast->list, &priv->multicast_list);
  562. }
  563. if (!mcast->ah) {
  564. if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
  565. skb_queue_tail(&mcast->pkt_queue, skb);
  566. else {
  567. ++dev->stats.tx_dropped;
  568. dev_kfree_skb_any(skb);
  569. }
  570. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  571. ipoib_dbg_mcast(priv, "no address vector, "
  572. "but multicast join already started\n");
  573. else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  574. ipoib_mcast_sendonly_join(mcast);
  575. /*
  576. * If lookup completes between here and out:, don't
  577. * want to send packet twice.
  578. */
  579. mcast = NULL;
  580. }
  581. out:
  582. if (mcast && mcast->ah) {
  583. if (skb->dst &&
  584. skb->dst->neighbour &&
  585. !*to_ipoib_neigh(skb->dst->neighbour)) {
  586. struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour,
  587. skb->dev);
  588. if (neigh) {
  589. kref_get(&mcast->ah->ref);
  590. neigh->ah = mcast->ah;
  591. list_add_tail(&neigh->list, &mcast->neigh_list);
  592. }
  593. }
  594. ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
  595. }
  596. unlock:
  597. spin_unlock_irqrestore(&priv->lock, flags);
  598. }
  599. void ipoib_mcast_dev_flush(struct net_device *dev)
  600. {
  601. struct ipoib_dev_priv *priv = netdev_priv(dev);
  602. LIST_HEAD(remove_list);
  603. struct ipoib_mcast *mcast, *tmcast;
  604. unsigned long flags;
  605. ipoib_dbg_mcast(priv, "flushing multicast list\n");
  606. spin_lock_irqsave(&priv->lock, flags);
  607. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  608. list_del(&mcast->list);
  609. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  610. list_add_tail(&mcast->list, &remove_list);
  611. }
  612. if (priv->broadcast) {
  613. rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
  614. list_add_tail(&priv->broadcast->list, &remove_list);
  615. priv->broadcast = NULL;
  616. }
  617. spin_unlock_irqrestore(&priv->lock, flags);
  618. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  619. ipoib_mcast_leave(dev, mcast);
  620. ipoib_mcast_free(mcast);
  621. }
  622. }
  623. void ipoib_mcast_restart_task(struct work_struct *work)
  624. {
  625. struct ipoib_dev_priv *priv =
  626. container_of(work, struct ipoib_dev_priv, restart_task);
  627. struct net_device *dev = priv->dev;
  628. struct dev_mc_list *mclist;
  629. struct ipoib_mcast *mcast, *tmcast;
  630. LIST_HEAD(remove_list);
  631. unsigned long flags;
  632. struct ib_sa_mcmember_rec rec;
  633. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  634. ipoib_mcast_stop_thread(dev, 0);
  635. local_irq_save(flags);
  636. netif_addr_lock(dev);
  637. spin_lock(&priv->lock);
  638. /*
  639. * Unfortunately, the networking core only gives us a list of all of
  640. * the multicast hardware addresses. We need to figure out which ones
  641. * are new and which ones have been removed
  642. */
  643. /* Clear out the found flag */
  644. list_for_each_entry(mcast, &priv->multicast_list, list)
  645. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  646. /* Mark all of the entries that are found or don't exist */
  647. for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
  648. union ib_gid mgid;
  649. memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
  650. mcast = __ipoib_mcast_find(dev, &mgid);
  651. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  652. struct ipoib_mcast *nmcast;
  653. /* ignore group which is directly joined by userspace */
  654. if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
  655. !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
  656. ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
  657. mgid.raw);
  658. continue;
  659. }
  660. /* Not found or send-only group, let's add a new entry */
  661. ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
  662. mgid.raw);
  663. nmcast = ipoib_mcast_alloc(dev, 0);
  664. if (!nmcast) {
  665. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  666. continue;
  667. }
  668. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  669. nmcast->mcmember.mgid = mgid;
  670. if (mcast) {
  671. /* Destroy the send only entry */
  672. list_move_tail(&mcast->list, &remove_list);
  673. rb_replace_node(&mcast->rb_node,
  674. &nmcast->rb_node,
  675. &priv->multicast_tree);
  676. } else
  677. __ipoib_mcast_add(dev, nmcast);
  678. list_add_tail(&nmcast->list, &priv->multicast_list);
  679. }
  680. if (mcast)
  681. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  682. }
  683. /* Remove all of the entries don't exist anymore */
  684. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  685. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  686. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  687. ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
  688. mcast->mcmember.mgid.raw);
  689. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  690. /* Move to the remove list */
  691. list_move_tail(&mcast->list, &remove_list);
  692. }
  693. }
  694. spin_unlock(&priv->lock);
  695. netif_addr_unlock(dev);
  696. local_irq_restore(flags);
  697. /* We have to cancel outside of the spinlock */
  698. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  699. ipoib_mcast_leave(mcast->dev, mcast);
  700. ipoib_mcast_free(mcast);
  701. }
  702. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  703. ipoib_mcast_start_thread(dev);
  704. }
  705. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  706. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  707. {
  708. struct ipoib_mcast_iter *iter;
  709. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  710. if (!iter)
  711. return NULL;
  712. iter->dev = dev;
  713. memset(iter->mgid.raw, 0, 16);
  714. if (ipoib_mcast_iter_next(iter)) {
  715. kfree(iter);
  716. return NULL;
  717. }
  718. return iter;
  719. }
  720. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  721. {
  722. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  723. struct rb_node *n;
  724. struct ipoib_mcast *mcast;
  725. int ret = 1;
  726. spin_lock_irq(&priv->lock);
  727. n = rb_first(&priv->multicast_tree);
  728. while (n) {
  729. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  730. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  731. sizeof (union ib_gid)) < 0) {
  732. iter->mgid = mcast->mcmember.mgid;
  733. iter->created = mcast->created;
  734. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  735. iter->complete = !!mcast->ah;
  736. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  737. ret = 0;
  738. break;
  739. }
  740. n = rb_next(n);
  741. }
  742. spin_unlock_irq(&priv->lock);
  743. return ret;
  744. }
  745. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  746. union ib_gid *mgid,
  747. unsigned long *created,
  748. unsigned int *queuelen,
  749. unsigned int *complete,
  750. unsigned int *send_only)
  751. {
  752. *mgid = iter->mgid;
  753. *created = iter->created;
  754. *queuelen = iter->queuelen;
  755. *complete = iter->complete;
  756. *send_only = iter->send_only;
  757. }
  758. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */