ipoib_multicast.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  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 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 %pI6 already attached\n",
  174. mcast->mcmember.mgid.raw);
  175. return 0;
  176. }
  177. ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
  178. &mcast->mcmember.mgid, set_qkey);
  179. if (ret < 0) {
  180. ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
  181. mcast->mcmember.mgid.raw);
  182. clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
  183. return ret;
  184. }
  185. }
  186. {
  187. struct ib_ah_attr av = {
  188. .dlid = be16_to_cpu(mcast->mcmember.mlid),
  189. .port_num = priv->port,
  190. .sl = mcast->mcmember.sl,
  191. .ah_flags = IB_AH_GRH,
  192. .static_rate = mcast->mcmember.rate,
  193. .grh = {
  194. .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
  195. .hop_limit = mcast->mcmember.hop_limit,
  196. .sgid_index = 0,
  197. .traffic_class = mcast->mcmember.traffic_class
  198. }
  199. };
  200. av.grh.dgid = mcast->mcmember.mgid;
  201. ah = ipoib_create_ah(dev, priv->pd, &av);
  202. if (IS_ERR(ah)) {
  203. ipoib_warn(priv, "ib_address_create failed %ld\n",
  204. -PTR_ERR(ah));
  205. /* use original error */
  206. return PTR_ERR(ah);
  207. } else {
  208. spin_lock_irq(&priv->lock);
  209. mcast->ah = ah;
  210. spin_unlock_irq(&priv->lock);
  211. ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
  212. mcast->mcmember.mgid.raw,
  213. mcast->ah->ah,
  214. be16_to_cpu(mcast->mcmember.mlid),
  215. mcast->mcmember.sl);
  216. }
  217. }
  218. /* actually send any queued packets */
  219. netif_tx_lock_bh(dev);
  220. while (!skb_queue_empty(&mcast->pkt_queue)) {
  221. struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
  222. struct dst_entry *dst = skb_dst(skb);
  223. struct neighbour *n = NULL;
  224. netif_tx_unlock_bh(dev);
  225. skb->dev = dev;
  226. if (dst)
  227. n = dst_get_neighbour_noref_raw(dst);
  228. if (!dst || !n) {
  229. /* put pseudoheader back on for next time */
  230. skb_push(skb, sizeof (struct ipoib_pseudoheader));
  231. }
  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. priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  502. if (!ipoib_cm_admin_enabled(dev)) {
  503. rtnl_lock();
  504. dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
  505. rtnl_unlock();
  506. }
  507. ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
  508. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  509. }
  510. int ipoib_mcast_start_thread(struct net_device *dev)
  511. {
  512. struct ipoib_dev_priv *priv = netdev_priv(dev);
  513. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  514. mutex_lock(&mcast_mutex);
  515. if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
  516. queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
  517. mutex_unlock(&mcast_mutex);
  518. return 0;
  519. }
  520. int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
  521. {
  522. struct ipoib_dev_priv *priv = netdev_priv(dev);
  523. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  524. mutex_lock(&mcast_mutex);
  525. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  526. cancel_delayed_work(&priv->mcast_task);
  527. mutex_unlock(&mcast_mutex);
  528. if (flush)
  529. flush_workqueue(ipoib_workqueue);
  530. return 0;
  531. }
  532. static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  533. {
  534. struct ipoib_dev_priv *priv = netdev_priv(dev);
  535. int ret = 0;
  536. if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  537. ib_sa_free_multicast(mcast->mc);
  538. if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  539. ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
  540. mcast->mcmember.mgid.raw);
  541. /* Remove ourselves from the multicast group */
  542. ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
  543. be16_to_cpu(mcast->mcmember.mlid));
  544. if (ret)
  545. ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
  546. }
  547. return 0;
  548. }
  549. void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
  550. {
  551. struct ipoib_dev_priv *priv = netdev_priv(dev);
  552. struct ipoib_mcast *mcast;
  553. unsigned long flags;
  554. spin_lock_irqsave(&priv->lock, flags);
  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 %pI6\n",
  566. 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. struct dst_entry *dst = skb_dst(skb);
  601. struct neighbour *n = NULL;
  602. rcu_read_lock();
  603. if (dst)
  604. n = dst_get_neighbour_noref(dst);
  605. if (n && !*to_ipoib_neigh(n)) {
  606. struct ipoib_neigh *neigh = ipoib_neigh_alloc(n,
  607. skb->dev);
  608. if (neigh) {
  609. kref_get(&mcast->ah->ref);
  610. neigh->ah = mcast->ah;
  611. list_add_tail(&neigh->list, &mcast->neigh_list);
  612. }
  613. }
  614. rcu_read_unlock();
  615. spin_unlock_irqrestore(&priv->lock, flags);
  616. ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
  617. return;
  618. }
  619. unlock:
  620. spin_unlock_irqrestore(&priv->lock, flags);
  621. }
  622. void ipoib_mcast_dev_flush(struct net_device *dev)
  623. {
  624. struct ipoib_dev_priv *priv = netdev_priv(dev);
  625. LIST_HEAD(remove_list);
  626. struct ipoib_mcast *mcast, *tmcast;
  627. unsigned long flags;
  628. ipoib_dbg_mcast(priv, "flushing multicast list\n");
  629. spin_lock_irqsave(&priv->lock, flags);
  630. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  631. list_del(&mcast->list);
  632. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  633. list_add_tail(&mcast->list, &remove_list);
  634. }
  635. if (priv->broadcast) {
  636. rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
  637. list_add_tail(&priv->broadcast->list, &remove_list);
  638. priv->broadcast = NULL;
  639. }
  640. spin_unlock_irqrestore(&priv->lock, flags);
  641. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  642. ipoib_mcast_leave(dev, mcast);
  643. ipoib_mcast_free(mcast);
  644. }
  645. }
  646. static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
  647. {
  648. /* reserved QPN, prefix, scope */
  649. if (memcmp(addr, broadcast, 6))
  650. return 0;
  651. /* signature lower, pkey */
  652. if (memcmp(addr + 7, broadcast + 7, 3))
  653. return 0;
  654. return 1;
  655. }
  656. void ipoib_mcast_restart_task(struct work_struct *work)
  657. {
  658. struct ipoib_dev_priv *priv =
  659. container_of(work, struct ipoib_dev_priv, restart_task);
  660. struct net_device *dev = priv->dev;
  661. struct netdev_hw_addr *ha;
  662. struct ipoib_mcast *mcast, *tmcast;
  663. LIST_HEAD(remove_list);
  664. unsigned long flags;
  665. struct ib_sa_mcmember_rec rec;
  666. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  667. ipoib_mcast_stop_thread(dev, 0);
  668. local_irq_save(flags);
  669. netif_addr_lock(dev);
  670. spin_lock(&priv->lock);
  671. /*
  672. * Unfortunately, the networking core only gives us a list of all of
  673. * the multicast hardware addresses. We need to figure out which ones
  674. * are new and which ones have been removed
  675. */
  676. /* Clear out the found flag */
  677. list_for_each_entry(mcast, &priv->multicast_list, list)
  678. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  679. /* Mark all of the entries that are found or don't exist */
  680. netdev_for_each_mc_addr(ha, dev) {
  681. union ib_gid mgid;
  682. if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
  683. continue;
  684. memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
  685. mcast = __ipoib_mcast_find(dev, &mgid);
  686. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  687. struct ipoib_mcast *nmcast;
  688. /* ignore group which is directly joined by userspace */
  689. if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
  690. !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
  691. ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
  692. mgid.raw);
  693. continue;
  694. }
  695. /* Not found or send-only group, let's add a new entry */
  696. ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
  697. mgid.raw);
  698. nmcast = ipoib_mcast_alloc(dev, 0);
  699. if (!nmcast) {
  700. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  701. continue;
  702. }
  703. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  704. nmcast->mcmember.mgid = mgid;
  705. if (mcast) {
  706. /* Destroy the send only entry */
  707. list_move_tail(&mcast->list, &remove_list);
  708. rb_replace_node(&mcast->rb_node,
  709. &nmcast->rb_node,
  710. &priv->multicast_tree);
  711. } else
  712. __ipoib_mcast_add(dev, nmcast);
  713. list_add_tail(&nmcast->list, &priv->multicast_list);
  714. }
  715. if (mcast)
  716. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  717. }
  718. /* Remove all of the entries don't exist anymore */
  719. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  720. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  721. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  722. ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
  723. mcast->mcmember.mgid.raw);
  724. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  725. /* Move to the remove list */
  726. list_move_tail(&mcast->list, &remove_list);
  727. }
  728. }
  729. spin_unlock(&priv->lock);
  730. netif_addr_unlock(dev);
  731. local_irq_restore(flags);
  732. /* We have to cancel outside of the spinlock */
  733. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  734. ipoib_mcast_leave(mcast->dev, mcast);
  735. ipoib_mcast_free(mcast);
  736. }
  737. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  738. ipoib_mcast_start_thread(dev);
  739. }
  740. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  741. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  742. {
  743. struct ipoib_mcast_iter *iter;
  744. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  745. if (!iter)
  746. return NULL;
  747. iter->dev = dev;
  748. memset(iter->mgid.raw, 0, 16);
  749. if (ipoib_mcast_iter_next(iter)) {
  750. kfree(iter);
  751. return NULL;
  752. }
  753. return iter;
  754. }
  755. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  756. {
  757. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  758. struct rb_node *n;
  759. struct ipoib_mcast *mcast;
  760. int ret = 1;
  761. spin_lock_irq(&priv->lock);
  762. n = rb_first(&priv->multicast_tree);
  763. while (n) {
  764. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  765. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  766. sizeof (union ib_gid)) < 0) {
  767. iter->mgid = mcast->mcmember.mgid;
  768. iter->created = mcast->created;
  769. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  770. iter->complete = !!mcast->ah;
  771. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  772. ret = 0;
  773. break;
  774. }
  775. n = rb_next(n);
  776. }
  777. spin_unlock_irq(&priv->lock);
  778. return ret;
  779. }
  780. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  781. union ib_gid *mgid,
  782. unsigned long *created,
  783. unsigned int *queuelen,
  784. unsigned int *complete,
  785. unsigned int *send_only)
  786. {
  787. *mgid = iter->mgid;
  788. *created = iter->created;
  789. *queuelen = iter->queuelen;
  790. *complete = iter->complete;
  791. *send_only = iter->send_only;
  792. }
  793. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */