ipoib_multicast.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /*
  2. * Copyright (c) 2004, 2005 Topspin Communications. All rights reserved.
  3. * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
  4. * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
  5. *
  6. * This software is available to you under a choice of one of two
  7. * licenses. You may choose to be licensed under the terms of the GNU
  8. * General Public License (GPL) Version 2, available from the file
  9. * COPYING in the main directory of this source tree, or the
  10. * OpenIB.org BSD license below:
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above
  17. * copyright notice, this list of conditions and the following
  18. * disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials
  23. * provided with the distribution.
  24. *
  25. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  29. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  30. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  31. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32. * SOFTWARE.
  33. */
  34. #include <linux/skbuff.h>
  35. #include <linux/rtnetlink.h>
  36. #include <linux/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. void ipoib_mcast_carrier_on_task(struct work_struct *work)
  310. {
  311. struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
  312. carrier_on_task);
  313. /*
  314. * Take rtnl_lock to avoid racing with ipoib_stop() and
  315. * turning the carrier back on while a device is being
  316. * removed.
  317. */
  318. rtnl_lock();
  319. netif_carrier_on(priv->dev);
  320. rtnl_unlock();
  321. }
  322. static int ipoib_mcast_join_complete(int status,
  323. struct ib_sa_multicast *multicast)
  324. {
  325. struct ipoib_mcast *mcast = multicast->context;
  326. struct net_device *dev = mcast->dev;
  327. struct ipoib_dev_priv *priv = netdev_priv(dev);
  328. ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
  329. " (status %d)\n",
  330. IPOIB_GID_ARG(mcast->mcmember.mgid), status);
  331. /* We trap for port events ourselves. */
  332. if (status == -ENETRESET)
  333. return 0;
  334. if (!status)
  335. status = ipoib_mcast_join_finish(mcast, &multicast->rec);
  336. if (!status) {
  337. mcast->backoff = 1;
  338. mutex_lock(&mcast_mutex);
  339. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  340. queue_delayed_work(ipoib_workqueue,
  341. &priv->mcast_task, 0);
  342. mutex_unlock(&mcast_mutex);
  343. /*
  344. * Defer carrier on work to ipoib_workqueue to avoid a
  345. * deadlock on rtnl_lock here.
  346. */
  347. if (mcast == priv->broadcast)
  348. queue_work(ipoib_workqueue, &priv->carrier_on_task);
  349. return 0;
  350. }
  351. if (mcast->logcount++ < 20) {
  352. if (status == -ETIMEDOUT) {
  353. ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
  354. ", status %d\n",
  355. IPOIB_GID_ARG(mcast->mcmember.mgid),
  356. status);
  357. } else {
  358. ipoib_warn(priv, "multicast join failed for "
  359. IPOIB_GID_FMT ", status %d\n",
  360. IPOIB_GID_ARG(mcast->mcmember.mgid),
  361. status);
  362. }
  363. }
  364. mcast->backoff *= 2;
  365. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  366. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  367. /* Clear the busy flag so we try again */
  368. status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  369. mutex_lock(&mcast_mutex);
  370. spin_lock_irq(&priv->lock);
  371. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  372. queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
  373. mcast->backoff * HZ);
  374. spin_unlock_irq(&priv->lock);
  375. mutex_unlock(&mcast_mutex);
  376. return status;
  377. }
  378. static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
  379. int create)
  380. {
  381. struct ipoib_dev_priv *priv = netdev_priv(dev);
  382. struct ib_sa_mcmember_rec rec = {
  383. .join_state = 1
  384. };
  385. ib_sa_comp_mask comp_mask;
  386. int ret = 0;
  387. ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
  388. IPOIB_GID_ARG(mcast->mcmember.mgid));
  389. rec.mgid = mcast->mcmember.mgid;
  390. rec.port_gid = priv->local_gid;
  391. rec.pkey = cpu_to_be16(priv->pkey);
  392. comp_mask =
  393. IB_SA_MCMEMBER_REC_MGID |
  394. IB_SA_MCMEMBER_REC_PORT_GID |
  395. IB_SA_MCMEMBER_REC_PKEY |
  396. IB_SA_MCMEMBER_REC_JOIN_STATE;
  397. if (create) {
  398. comp_mask |=
  399. IB_SA_MCMEMBER_REC_QKEY |
  400. IB_SA_MCMEMBER_REC_MTU_SELECTOR |
  401. IB_SA_MCMEMBER_REC_MTU |
  402. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
  403. IB_SA_MCMEMBER_REC_RATE_SELECTOR |
  404. IB_SA_MCMEMBER_REC_RATE |
  405. IB_SA_MCMEMBER_REC_SL |
  406. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  407. IB_SA_MCMEMBER_REC_HOP_LIMIT;
  408. rec.qkey = priv->broadcast->mcmember.qkey;
  409. rec.mtu_selector = IB_SA_EQ;
  410. rec.mtu = priv->broadcast->mcmember.mtu;
  411. rec.traffic_class = priv->broadcast->mcmember.traffic_class;
  412. rec.rate_selector = IB_SA_EQ;
  413. rec.rate = priv->broadcast->mcmember.rate;
  414. rec.sl = priv->broadcast->mcmember.sl;
  415. rec.flow_label = priv->broadcast->mcmember.flow_label;
  416. rec.hop_limit = priv->broadcast->mcmember.hop_limit;
  417. }
  418. set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  419. mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
  420. &rec, comp_mask, GFP_KERNEL,
  421. ipoib_mcast_join_complete, mcast);
  422. if (IS_ERR(mcast->mc)) {
  423. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  424. ret = PTR_ERR(mcast->mc);
  425. ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
  426. mcast->backoff *= 2;
  427. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  428. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  429. mutex_lock(&mcast_mutex);
  430. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  431. queue_delayed_work(ipoib_workqueue,
  432. &priv->mcast_task,
  433. mcast->backoff * HZ);
  434. mutex_unlock(&mcast_mutex);
  435. }
  436. }
  437. void ipoib_mcast_join_task(struct work_struct *work)
  438. {
  439. struct ipoib_dev_priv *priv =
  440. container_of(work, struct ipoib_dev_priv, mcast_task.work);
  441. struct net_device *dev = priv->dev;
  442. if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
  443. return;
  444. if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
  445. ipoib_warn(priv, "ib_query_gid() failed\n");
  446. else
  447. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  448. {
  449. struct ib_port_attr attr;
  450. if (!ib_query_port(priv->ca, priv->port, &attr))
  451. priv->local_lid = attr.lid;
  452. else
  453. ipoib_warn(priv, "ib_query_port failed\n");
  454. }
  455. if (!priv->broadcast) {
  456. struct ipoib_mcast *broadcast;
  457. broadcast = ipoib_mcast_alloc(dev, 1);
  458. if (!broadcast) {
  459. ipoib_warn(priv, "failed to allocate broadcast group\n");
  460. mutex_lock(&mcast_mutex);
  461. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  462. queue_delayed_work(ipoib_workqueue,
  463. &priv->mcast_task, HZ);
  464. mutex_unlock(&mcast_mutex);
  465. return;
  466. }
  467. spin_lock_irq(&priv->lock);
  468. memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  469. sizeof (union ib_gid));
  470. priv->broadcast = broadcast;
  471. __ipoib_mcast_add(dev, priv->broadcast);
  472. spin_unlock_irq(&priv->lock);
  473. }
  474. if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  475. if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
  476. ipoib_mcast_join(dev, priv->broadcast, 0);
  477. return;
  478. }
  479. while (1) {
  480. struct ipoib_mcast *mcast = NULL;
  481. spin_lock_irq(&priv->lock);
  482. list_for_each_entry(mcast, &priv->multicast_list, list) {
  483. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
  484. && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
  485. && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  486. /* Found the next unjoined group */
  487. break;
  488. }
  489. }
  490. spin_unlock_irq(&priv->lock);
  491. if (&mcast->list == &priv->multicast_list) {
  492. /* All done */
  493. break;
  494. }
  495. ipoib_mcast_join(dev, mcast, 1);
  496. return;
  497. }
  498. priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  499. if (!ipoib_cm_admin_enabled(dev)) {
  500. rtnl_lock();
  501. dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
  502. rtnl_unlock();
  503. }
  504. ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
  505. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  506. }
  507. int ipoib_mcast_start_thread(struct net_device *dev)
  508. {
  509. struct ipoib_dev_priv *priv = netdev_priv(dev);
  510. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  511. mutex_lock(&mcast_mutex);
  512. if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
  513. queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
  514. mutex_unlock(&mcast_mutex);
  515. return 0;
  516. }
  517. int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
  518. {
  519. struct ipoib_dev_priv *priv = netdev_priv(dev);
  520. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  521. mutex_lock(&mcast_mutex);
  522. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  523. cancel_delayed_work(&priv->mcast_task);
  524. mutex_unlock(&mcast_mutex);
  525. if (flush)
  526. flush_workqueue(ipoib_workqueue);
  527. return 0;
  528. }
  529. static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  530. {
  531. struct ipoib_dev_priv *priv = netdev_priv(dev);
  532. int ret = 0;
  533. if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  534. ib_sa_free_multicast(mcast->mc);
  535. if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  536. ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
  537. IPOIB_GID_ARG(mcast->mcmember.mgid));
  538. /* Remove ourselves from the multicast group */
  539. ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
  540. be16_to_cpu(mcast->mcmember.mlid));
  541. if (ret)
  542. ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
  543. }
  544. return 0;
  545. }
  546. void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
  547. {
  548. struct ipoib_dev_priv *priv = netdev_priv(dev);
  549. struct ipoib_mcast *mcast;
  550. /*
  551. * We can only be called from ipoib_start_xmit, so we're
  552. * inside tx_lock -- no need to save/restore flags.
  553. */
  554. spin_lock(&priv->lock);
  555. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
  556. !priv->broadcast ||
  557. !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  558. ++dev->stats.tx_dropped;
  559. dev_kfree_skb_any(skb);
  560. goto unlock;
  561. }
  562. mcast = __ipoib_mcast_find(dev, mgid);
  563. if (!mcast) {
  564. /* Let's create a new send only group now */
  565. ipoib_dbg_mcast(priv, "setting up send only multicast group for "
  566. IPOIB_GID_FMT "\n", IPOIB_GID_RAW_ARG(mgid));
  567. mcast = ipoib_mcast_alloc(dev, 0);
  568. if (!mcast) {
  569. ipoib_warn(priv, "unable to allocate memory for "
  570. "multicast structure\n");
  571. ++dev->stats.tx_dropped;
  572. dev_kfree_skb_any(skb);
  573. goto out;
  574. }
  575. set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
  576. memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
  577. __ipoib_mcast_add(dev, mcast);
  578. list_add_tail(&mcast->list, &priv->multicast_list);
  579. }
  580. if (!mcast->ah) {
  581. if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
  582. skb_queue_tail(&mcast->pkt_queue, skb);
  583. else {
  584. ++dev->stats.tx_dropped;
  585. dev_kfree_skb_any(skb);
  586. }
  587. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  588. ipoib_dbg_mcast(priv, "no address vector, "
  589. "but multicast join already started\n");
  590. else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  591. ipoib_mcast_sendonly_join(mcast);
  592. /*
  593. * If lookup completes between here and out:, don't
  594. * want to send packet twice.
  595. */
  596. mcast = NULL;
  597. }
  598. out:
  599. if (mcast && mcast->ah) {
  600. if (skb->dst &&
  601. skb->dst->neighbour &&
  602. !*to_ipoib_neigh(skb->dst->neighbour)) {
  603. struct ipoib_neigh *neigh = ipoib_neigh_alloc(skb->dst->neighbour,
  604. skb->dev);
  605. if (neigh) {
  606. kref_get(&mcast->ah->ref);
  607. neigh->ah = mcast->ah;
  608. list_add_tail(&neigh->list, &mcast->neigh_list);
  609. }
  610. }
  611. ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
  612. }
  613. unlock:
  614. spin_unlock(&priv->lock);
  615. }
  616. void ipoib_mcast_dev_flush(struct net_device *dev)
  617. {
  618. struct ipoib_dev_priv *priv = netdev_priv(dev);
  619. LIST_HEAD(remove_list);
  620. struct ipoib_mcast *mcast, *tmcast;
  621. unsigned long flags;
  622. ipoib_dbg_mcast(priv, "flushing multicast list\n");
  623. spin_lock_irqsave(&priv->lock, flags);
  624. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  625. list_del(&mcast->list);
  626. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  627. list_add_tail(&mcast->list, &remove_list);
  628. }
  629. if (priv->broadcast) {
  630. rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
  631. list_add_tail(&priv->broadcast->list, &remove_list);
  632. priv->broadcast = NULL;
  633. }
  634. spin_unlock_irqrestore(&priv->lock, flags);
  635. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  636. ipoib_mcast_leave(dev, mcast);
  637. ipoib_mcast_free(mcast);
  638. }
  639. }
  640. void ipoib_mcast_restart_task(struct work_struct *work)
  641. {
  642. struct ipoib_dev_priv *priv =
  643. container_of(work, struct ipoib_dev_priv, restart_task);
  644. struct net_device *dev = priv->dev;
  645. struct dev_mc_list *mclist;
  646. struct ipoib_mcast *mcast, *tmcast;
  647. LIST_HEAD(remove_list);
  648. unsigned long flags;
  649. struct ib_sa_mcmember_rec rec;
  650. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  651. ipoib_mcast_stop_thread(dev, 0);
  652. local_irq_save(flags);
  653. netif_addr_lock(dev);
  654. spin_lock(&priv->lock);
  655. /*
  656. * Unfortunately, the networking core only gives us a list of all of
  657. * the multicast hardware addresses. We need to figure out which ones
  658. * are new and which ones have been removed
  659. */
  660. /* Clear out the found flag */
  661. list_for_each_entry(mcast, &priv->multicast_list, list)
  662. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  663. /* Mark all of the entries that are found or don't exist */
  664. for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
  665. union ib_gid mgid;
  666. memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
  667. mcast = __ipoib_mcast_find(dev, &mgid);
  668. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  669. struct ipoib_mcast *nmcast;
  670. /* ignore group which is directly joined by userspace */
  671. if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
  672. !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
  673. ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid "
  674. IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
  675. continue;
  676. }
  677. /* Not found or send-only group, let's add a new entry */
  678. ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
  679. IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
  680. nmcast = ipoib_mcast_alloc(dev, 0);
  681. if (!nmcast) {
  682. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  683. continue;
  684. }
  685. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  686. nmcast->mcmember.mgid = mgid;
  687. if (mcast) {
  688. /* Destroy the send only entry */
  689. list_move_tail(&mcast->list, &remove_list);
  690. rb_replace_node(&mcast->rb_node,
  691. &nmcast->rb_node,
  692. &priv->multicast_tree);
  693. } else
  694. __ipoib_mcast_add(dev, nmcast);
  695. list_add_tail(&nmcast->list, &priv->multicast_list);
  696. }
  697. if (mcast)
  698. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  699. }
  700. /* Remove all of the entries don't exist anymore */
  701. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  702. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  703. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  704. ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
  705. IPOIB_GID_ARG(mcast->mcmember.mgid));
  706. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  707. /* Move to the remove list */
  708. list_move_tail(&mcast->list, &remove_list);
  709. }
  710. }
  711. spin_unlock(&priv->lock);
  712. netif_addr_unlock(dev);
  713. local_irq_restore(flags);
  714. /* We have to cancel outside of the spinlock */
  715. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  716. ipoib_mcast_leave(mcast->dev, mcast);
  717. ipoib_mcast_free(mcast);
  718. }
  719. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  720. ipoib_mcast_start_thread(dev);
  721. }
  722. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  723. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  724. {
  725. struct ipoib_mcast_iter *iter;
  726. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  727. if (!iter)
  728. return NULL;
  729. iter->dev = dev;
  730. memset(iter->mgid.raw, 0, 16);
  731. if (ipoib_mcast_iter_next(iter)) {
  732. kfree(iter);
  733. return NULL;
  734. }
  735. return iter;
  736. }
  737. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  738. {
  739. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  740. struct rb_node *n;
  741. struct ipoib_mcast *mcast;
  742. int ret = 1;
  743. spin_lock_irq(&priv->lock);
  744. n = rb_first(&priv->multicast_tree);
  745. while (n) {
  746. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  747. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  748. sizeof (union ib_gid)) < 0) {
  749. iter->mgid = mcast->mcmember.mgid;
  750. iter->created = mcast->created;
  751. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  752. iter->complete = !!mcast->ah;
  753. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  754. ret = 0;
  755. break;
  756. }
  757. n = rb_next(n);
  758. }
  759. spin_unlock_irq(&priv->lock);
  760. return ret;
  761. }
  762. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  763. union ib_gid *mgid,
  764. unsigned long *created,
  765. unsigned int *queuelen,
  766. unsigned int *complete,
  767. unsigned int *send_only)
  768. {
  769. *mgid = iter->mgid;
  770. *created = iter->created;
  771. *queuelen = iter->queuelen;
  772. *complete = iter->complete;
  773. *send_only = iter->send_only;
  774. }
  775. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */