ipoib_multicast.c 25 KB

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