ipoib_multicast.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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 multicast MTU and cached Q_Key before we attach if it's
  146. * the broadcast group.
  147. */
  148. if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  149. sizeof (union ib_gid))) {
  150. spin_lock_irq(&priv->lock);
  151. if (!priv->broadcast) {
  152. spin_unlock_irq(&priv->lock);
  153. return -EAGAIN;
  154. }
  155. priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  156. priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
  157. spin_unlock_irq(&priv->lock);
  158. priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
  159. set_qkey = 1;
  160. if (!ipoib_cm_admin_enabled(dev)) {
  161. rtnl_lock();
  162. dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
  163. rtnl_unlock();
  164. }
  165. }
  166. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  167. if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  168. ipoib_warn(priv, "multicast group %pI6 already attached\n",
  169. mcast->mcmember.mgid.raw);
  170. return 0;
  171. }
  172. ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
  173. &mcast->mcmember.mgid, set_qkey);
  174. if (ret < 0) {
  175. ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
  176. mcast->mcmember.mgid.raw);
  177. clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
  178. return ret;
  179. }
  180. }
  181. {
  182. struct ib_ah_attr av = {
  183. .dlid = be16_to_cpu(mcast->mcmember.mlid),
  184. .port_num = priv->port,
  185. .sl = mcast->mcmember.sl,
  186. .ah_flags = IB_AH_GRH,
  187. .static_rate = mcast->mcmember.rate,
  188. .grh = {
  189. .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
  190. .hop_limit = mcast->mcmember.hop_limit,
  191. .sgid_index = 0,
  192. .traffic_class = mcast->mcmember.traffic_class
  193. }
  194. };
  195. av.grh.dgid = mcast->mcmember.mgid;
  196. ah = ipoib_create_ah(dev, priv->pd, &av);
  197. if (IS_ERR(ah)) {
  198. ipoib_warn(priv, "ib_address_create failed %ld\n",
  199. -PTR_ERR(ah));
  200. /* use original error */
  201. return PTR_ERR(ah);
  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 (dev_queue_xmit(skb))
  220. ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
  221. netif_tx_lock_bh(dev);
  222. }
  223. netif_tx_unlock_bh(dev);
  224. return 0;
  225. }
  226. static int
  227. ipoib_mcast_sendonly_join_complete(int status,
  228. struct ib_sa_multicast *multicast)
  229. {
  230. struct ipoib_mcast *mcast = multicast->context;
  231. struct net_device *dev = mcast->dev;
  232. /* We trap for port events ourselves. */
  233. if (status == -ENETRESET)
  234. return 0;
  235. if (!status)
  236. status = ipoib_mcast_join_finish(mcast, &multicast->rec);
  237. if (status) {
  238. if (mcast->logcount++ < 20)
  239. ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for %pI6, status %d\n",
  240. mcast->mcmember.mgid.raw, status);
  241. /* Flush out any queued packets */
  242. netif_tx_lock_bh(dev);
  243. while (!skb_queue_empty(&mcast->pkt_queue)) {
  244. ++dev->stats.tx_dropped;
  245. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  246. }
  247. netif_tx_unlock_bh(dev);
  248. /* Clear the busy flag so we try again */
  249. status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
  250. &mcast->flags);
  251. }
  252. return status;
  253. }
  254. static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
  255. {
  256. struct net_device *dev = mcast->dev;
  257. struct ipoib_dev_priv *priv = netdev_priv(dev);
  258. struct ib_sa_mcmember_rec rec = {
  259. #if 0 /* Some SMs don't support send-only yet */
  260. .join_state = 4
  261. #else
  262. .join_state = 1
  263. #endif
  264. };
  265. int ret = 0;
  266. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  267. ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
  268. return -ENODEV;
  269. }
  270. if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
  271. ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
  272. return -EBUSY;
  273. }
  274. rec.mgid = mcast->mcmember.mgid;
  275. rec.port_gid = priv->local_gid;
  276. rec.pkey = cpu_to_be16(priv->pkey);
  277. mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
  278. priv->port, &rec,
  279. IB_SA_MCMEMBER_REC_MGID |
  280. IB_SA_MCMEMBER_REC_PORT_GID |
  281. IB_SA_MCMEMBER_REC_PKEY |
  282. IB_SA_MCMEMBER_REC_JOIN_STATE,
  283. GFP_ATOMIC,
  284. ipoib_mcast_sendonly_join_complete,
  285. mcast);
  286. if (IS_ERR(mcast->mc)) {
  287. ret = PTR_ERR(mcast->mc);
  288. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  289. ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
  290. ret);
  291. } else {
  292. ipoib_dbg_mcast(priv, "no multicast record for %pI6, starting join\n",
  293. mcast->mcmember.mgid.raw);
  294. }
  295. return ret;
  296. }
  297. void ipoib_mcast_carrier_on_task(struct work_struct *work)
  298. {
  299. struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
  300. carrier_on_task);
  301. struct ib_port_attr attr;
  302. /*
  303. * Take rtnl_lock to avoid racing with ipoib_stop() and
  304. * turning the carrier back on while a device is being
  305. * removed.
  306. */
  307. if (ib_query_port(priv->ca, priv->port, &attr) ||
  308. attr.state != IB_PORT_ACTIVE) {
  309. ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
  310. return;
  311. }
  312. rtnl_lock();
  313. netif_carrier_on(priv->dev);
  314. rtnl_unlock();
  315. }
  316. static int ipoib_mcast_join_complete(int status,
  317. struct ib_sa_multicast *multicast)
  318. {
  319. struct ipoib_mcast *mcast = multicast->context;
  320. struct net_device *dev = mcast->dev;
  321. struct ipoib_dev_priv *priv = netdev_priv(dev);
  322. ipoib_dbg_mcast(priv, "join completion for %pI6 (status %d)\n",
  323. mcast->mcmember.mgid.raw, status);
  324. /* We trap for port events ourselves. */
  325. if (status == -ENETRESET)
  326. return 0;
  327. if (!status)
  328. status = ipoib_mcast_join_finish(mcast, &multicast->rec);
  329. if (!status) {
  330. mcast->backoff = 1;
  331. mutex_lock(&mcast_mutex);
  332. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  333. queue_delayed_work(ipoib_workqueue,
  334. &priv->mcast_task, 0);
  335. mutex_unlock(&mcast_mutex);
  336. /*
  337. * Defer carrier on work to ipoib_workqueue to avoid a
  338. * deadlock on rtnl_lock here.
  339. */
  340. if (mcast == priv->broadcast)
  341. queue_work(ipoib_workqueue, &priv->carrier_on_task);
  342. return 0;
  343. }
  344. if (mcast->logcount++ < 20) {
  345. if (status == -ETIMEDOUT || status == -EAGAIN) {
  346. ipoib_dbg_mcast(priv, "multicast join failed for %pI6, status %d\n",
  347. mcast->mcmember.mgid.raw, status);
  348. } else {
  349. ipoib_warn(priv, "multicast join failed for %pI6, status %d\n",
  350. mcast->mcmember.mgid.raw, status);
  351. }
  352. }
  353. mcast->backoff *= 2;
  354. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  355. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  356. /* Clear the busy flag so we try again */
  357. status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  358. mutex_lock(&mcast_mutex);
  359. spin_lock_irq(&priv->lock);
  360. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  361. queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
  362. mcast->backoff * HZ);
  363. spin_unlock_irq(&priv->lock);
  364. mutex_unlock(&mcast_mutex);
  365. return status;
  366. }
  367. static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
  368. int create)
  369. {
  370. struct ipoib_dev_priv *priv = netdev_priv(dev);
  371. struct ib_sa_mcmember_rec rec = {
  372. .join_state = 1
  373. };
  374. ib_sa_comp_mask comp_mask;
  375. int ret = 0;
  376. ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
  377. rec.mgid = mcast->mcmember.mgid;
  378. rec.port_gid = priv->local_gid;
  379. rec.pkey = cpu_to_be16(priv->pkey);
  380. comp_mask =
  381. IB_SA_MCMEMBER_REC_MGID |
  382. IB_SA_MCMEMBER_REC_PORT_GID |
  383. IB_SA_MCMEMBER_REC_PKEY |
  384. IB_SA_MCMEMBER_REC_JOIN_STATE;
  385. if (create) {
  386. comp_mask |=
  387. IB_SA_MCMEMBER_REC_QKEY |
  388. IB_SA_MCMEMBER_REC_MTU_SELECTOR |
  389. IB_SA_MCMEMBER_REC_MTU |
  390. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
  391. IB_SA_MCMEMBER_REC_RATE_SELECTOR |
  392. IB_SA_MCMEMBER_REC_RATE |
  393. IB_SA_MCMEMBER_REC_SL |
  394. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  395. IB_SA_MCMEMBER_REC_HOP_LIMIT;
  396. rec.qkey = priv->broadcast->mcmember.qkey;
  397. rec.mtu_selector = IB_SA_EQ;
  398. rec.mtu = priv->broadcast->mcmember.mtu;
  399. rec.traffic_class = priv->broadcast->mcmember.traffic_class;
  400. rec.rate_selector = IB_SA_EQ;
  401. rec.rate = priv->broadcast->mcmember.rate;
  402. rec.sl = priv->broadcast->mcmember.sl;
  403. rec.flow_label = priv->broadcast->mcmember.flow_label;
  404. rec.hop_limit = priv->broadcast->mcmember.hop_limit;
  405. }
  406. set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  407. mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
  408. &rec, comp_mask, GFP_KERNEL,
  409. ipoib_mcast_join_complete, mcast);
  410. if (IS_ERR(mcast->mc)) {
  411. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  412. ret = PTR_ERR(mcast->mc);
  413. ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
  414. mcast->backoff *= 2;
  415. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  416. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  417. mutex_lock(&mcast_mutex);
  418. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  419. queue_delayed_work(ipoib_workqueue,
  420. &priv->mcast_task,
  421. mcast->backoff * HZ);
  422. mutex_unlock(&mcast_mutex);
  423. }
  424. }
  425. void ipoib_mcast_join_task(struct work_struct *work)
  426. {
  427. struct ipoib_dev_priv *priv =
  428. container_of(work, struct ipoib_dev_priv, mcast_task.work);
  429. struct net_device *dev = priv->dev;
  430. if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
  431. return;
  432. if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
  433. ipoib_warn(priv, "ib_query_gid() failed\n");
  434. else
  435. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  436. {
  437. struct ib_port_attr attr;
  438. if (!ib_query_port(priv->ca, priv->port, &attr))
  439. priv->local_lid = attr.lid;
  440. else
  441. ipoib_warn(priv, "ib_query_port failed\n");
  442. }
  443. if (!priv->broadcast) {
  444. struct ipoib_mcast *broadcast;
  445. if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  446. return;
  447. broadcast = ipoib_mcast_alloc(dev, 1);
  448. if (!broadcast) {
  449. ipoib_warn(priv, "failed to allocate broadcast group\n");
  450. mutex_lock(&mcast_mutex);
  451. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  452. queue_delayed_work(ipoib_workqueue,
  453. &priv->mcast_task, HZ);
  454. mutex_unlock(&mcast_mutex);
  455. return;
  456. }
  457. spin_lock_irq(&priv->lock);
  458. memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  459. sizeof (union ib_gid));
  460. priv->broadcast = broadcast;
  461. __ipoib_mcast_add(dev, priv->broadcast);
  462. spin_unlock_irq(&priv->lock);
  463. }
  464. if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  465. if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
  466. ipoib_mcast_join(dev, priv->broadcast, 0);
  467. return;
  468. }
  469. while (1) {
  470. struct ipoib_mcast *mcast = NULL;
  471. spin_lock_irq(&priv->lock);
  472. list_for_each_entry(mcast, &priv->multicast_list, list) {
  473. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
  474. && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
  475. && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  476. /* Found the next unjoined group */
  477. break;
  478. }
  479. }
  480. spin_unlock_irq(&priv->lock);
  481. if (&mcast->list == &priv->multicast_list) {
  482. /* All done */
  483. break;
  484. }
  485. ipoib_mcast_join(dev, mcast, 1);
  486. return;
  487. }
  488. ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
  489. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  490. }
  491. int ipoib_mcast_start_thread(struct net_device *dev)
  492. {
  493. struct ipoib_dev_priv *priv = netdev_priv(dev);
  494. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  495. mutex_lock(&mcast_mutex);
  496. if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
  497. queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
  498. mutex_unlock(&mcast_mutex);
  499. return 0;
  500. }
  501. int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
  502. {
  503. struct ipoib_dev_priv *priv = netdev_priv(dev);
  504. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  505. mutex_lock(&mcast_mutex);
  506. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  507. cancel_delayed_work(&priv->mcast_task);
  508. mutex_unlock(&mcast_mutex);
  509. if (flush)
  510. flush_workqueue(ipoib_workqueue);
  511. return 0;
  512. }
  513. static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  514. {
  515. struct ipoib_dev_priv *priv = netdev_priv(dev);
  516. int ret = 0;
  517. if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  518. ib_sa_free_multicast(mcast->mc);
  519. if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  520. ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
  521. mcast->mcmember.mgid.raw);
  522. /* Remove ourselves from the multicast group */
  523. ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
  524. be16_to_cpu(mcast->mcmember.mlid));
  525. if (ret)
  526. ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
  527. }
  528. return 0;
  529. }
  530. void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
  531. {
  532. struct ipoib_dev_priv *priv = netdev_priv(dev);
  533. struct ipoib_mcast *mcast;
  534. unsigned long flags;
  535. void *mgid = daddr + 4;
  536. spin_lock_irqsave(&priv->lock, flags);
  537. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
  538. !priv->broadcast ||
  539. !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  540. ++dev->stats.tx_dropped;
  541. dev_kfree_skb_any(skb);
  542. goto unlock;
  543. }
  544. mcast = __ipoib_mcast_find(dev, mgid);
  545. if (!mcast) {
  546. /* Let's create a new send only group now */
  547. ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
  548. mgid);
  549. mcast = ipoib_mcast_alloc(dev, 0);
  550. if (!mcast) {
  551. ipoib_warn(priv, "unable to allocate memory for "
  552. "multicast structure\n");
  553. ++dev->stats.tx_dropped;
  554. dev_kfree_skb_any(skb);
  555. goto out;
  556. }
  557. set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
  558. memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
  559. __ipoib_mcast_add(dev, mcast);
  560. list_add_tail(&mcast->list, &priv->multicast_list);
  561. }
  562. if (!mcast->ah) {
  563. if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
  564. skb_queue_tail(&mcast->pkt_queue, skb);
  565. else {
  566. ++dev->stats.tx_dropped;
  567. dev_kfree_skb_any(skb);
  568. }
  569. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  570. ipoib_dbg_mcast(priv, "no address vector, "
  571. "but multicast join already started\n");
  572. else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  573. ipoib_mcast_sendonly_join(mcast);
  574. /*
  575. * If lookup completes between here and out:, don't
  576. * want to send packet twice.
  577. */
  578. mcast = NULL;
  579. }
  580. out:
  581. if (mcast && mcast->ah) {
  582. struct ipoib_neigh *neigh;
  583. spin_unlock_irqrestore(&priv->lock, flags);
  584. neigh = ipoib_neigh_get(dev, daddr);
  585. spin_lock_irqsave(&priv->lock, flags);
  586. if (!neigh) {
  587. neigh = ipoib_neigh_alloc(daddr, 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. 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 */