ipoib_multicast.c 25 KB

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