ipoib_multicast.c 25 KB

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