ipoib_multicast.c 25 KB

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