ipoib_multicast.c 25 KB

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