ipoib_multicast.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  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 (!ah) {
  203. ipoib_warn(priv, "ib_address_create failed\n");
  204. } else {
  205. spin_lock_irq(&priv->lock);
  206. mcast->ah = ah;
  207. spin_unlock_irq(&priv->lock);
  208. ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
  209. mcast->mcmember.mgid.raw,
  210. mcast->ah->ah,
  211. be16_to_cpu(mcast->mcmember.mlid),
  212. mcast->mcmember.sl);
  213. }
  214. }
  215. /* actually send any queued packets */
  216. netif_tx_lock_bh(dev);
  217. while (!skb_queue_empty(&mcast->pkt_queue)) {
  218. struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
  219. struct dst_entry *dst = skb_dst(skb);
  220. struct neighbour *n = NULL;
  221. netif_tx_unlock_bh(dev);
  222. skb->dev = dev;
  223. if (dst)
  224. n = dst_get_neighbour(dst);
  225. if (!dst || !n) {
  226. /* put pseudoheader back on for next time */
  227. skb_push(skb, sizeof (struct ipoib_pseudoheader));
  228. }
  229. if (dev_queue_xmit(skb))
  230. ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
  231. netif_tx_lock_bh(dev);
  232. }
  233. netif_tx_unlock_bh(dev);
  234. return 0;
  235. }
  236. static int
  237. ipoib_mcast_sendonly_join_complete(int status,
  238. struct ib_sa_multicast *multicast)
  239. {
  240. struct ipoib_mcast *mcast = multicast->context;
  241. struct net_device *dev = mcast->dev;
  242. /* We trap for port events ourselves. */
  243. if (status == -ENETRESET)
  244. return 0;
  245. if (!status)
  246. status = ipoib_mcast_join_finish(mcast, &multicast->rec);
  247. if (status) {
  248. if (mcast->logcount++ < 20)
  249. ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for %pI6, status %d\n",
  250. mcast->mcmember.mgid.raw, status);
  251. /* Flush out any queued packets */
  252. netif_tx_lock_bh(dev);
  253. while (!skb_queue_empty(&mcast->pkt_queue)) {
  254. ++dev->stats.tx_dropped;
  255. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  256. }
  257. netif_tx_unlock_bh(dev);
  258. /* Clear the busy flag so we try again */
  259. status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY,
  260. &mcast->flags);
  261. }
  262. return status;
  263. }
  264. static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
  265. {
  266. struct net_device *dev = mcast->dev;
  267. struct ipoib_dev_priv *priv = netdev_priv(dev);
  268. struct ib_sa_mcmember_rec rec = {
  269. #if 0 /* Some SMs don't support send-only yet */
  270. .join_state = 4
  271. #else
  272. .join_state = 1
  273. #endif
  274. };
  275. int ret = 0;
  276. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  277. ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
  278. return -ENODEV;
  279. }
  280. if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
  281. ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
  282. return -EBUSY;
  283. }
  284. rec.mgid = mcast->mcmember.mgid;
  285. rec.port_gid = priv->local_gid;
  286. rec.pkey = cpu_to_be16(priv->pkey);
  287. mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca,
  288. priv->port, &rec,
  289. IB_SA_MCMEMBER_REC_MGID |
  290. IB_SA_MCMEMBER_REC_PORT_GID |
  291. IB_SA_MCMEMBER_REC_PKEY |
  292. IB_SA_MCMEMBER_REC_JOIN_STATE,
  293. GFP_ATOMIC,
  294. ipoib_mcast_sendonly_join_complete,
  295. mcast);
  296. if (IS_ERR(mcast->mc)) {
  297. ret = PTR_ERR(mcast->mc);
  298. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  299. ipoib_warn(priv, "ib_sa_join_multicast failed (ret = %d)\n",
  300. ret);
  301. } else {
  302. ipoib_dbg_mcast(priv, "no multicast record for %pI6, starting join\n",
  303. mcast->mcmember.mgid.raw);
  304. }
  305. return ret;
  306. }
  307. void ipoib_mcast_carrier_on_task(struct work_struct *work)
  308. {
  309. struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
  310. carrier_on_task);
  311. struct ib_port_attr attr;
  312. /*
  313. * Take rtnl_lock to avoid racing with ipoib_stop() and
  314. * turning the carrier back on while a device is being
  315. * removed.
  316. */
  317. if (ib_query_port(priv->ca, priv->port, &attr) ||
  318. attr.state != IB_PORT_ACTIVE) {
  319. ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
  320. return;
  321. }
  322. rtnl_lock();
  323. netif_carrier_on(priv->dev);
  324. rtnl_unlock();
  325. }
  326. static int ipoib_mcast_join_complete(int status,
  327. struct ib_sa_multicast *multicast)
  328. {
  329. struct ipoib_mcast *mcast = multicast->context;
  330. struct net_device *dev = mcast->dev;
  331. struct ipoib_dev_priv *priv = netdev_priv(dev);
  332. ipoib_dbg_mcast(priv, "join completion for %pI6 (status %d)\n",
  333. mcast->mcmember.mgid.raw, status);
  334. /* We trap for port events ourselves. */
  335. if (status == -ENETRESET)
  336. return 0;
  337. if (!status)
  338. status = ipoib_mcast_join_finish(mcast, &multicast->rec);
  339. if (!status) {
  340. mcast->backoff = 1;
  341. mutex_lock(&mcast_mutex);
  342. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  343. queue_delayed_work(ipoib_workqueue,
  344. &priv->mcast_task, 0);
  345. mutex_unlock(&mcast_mutex);
  346. /*
  347. * Defer carrier on work to ipoib_workqueue to avoid a
  348. * deadlock on rtnl_lock here.
  349. */
  350. if (mcast == priv->broadcast)
  351. queue_work(ipoib_workqueue, &priv->carrier_on_task);
  352. return 0;
  353. }
  354. if (mcast->logcount++ < 20) {
  355. if (status == -ETIMEDOUT || status == -EAGAIN) {
  356. ipoib_dbg_mcast(priv, "multicast join failed for %pI6, status %d\n",
  357. mcast->mcmember.mgid.raw, status);
  358. } else {
  359. ipoib_warn(priv, "multicast join failed for %pI6, status %d\n",
  360. mcast->mcmember.mgid.raw, status);
  361. }
  362. }
  363. mcast->backoff *= 2;
  364. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  365. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  366. /* Clear the busy flag so we try again */
  367. status = test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  368. mutex_lock(&mcast_mutex);
  369. spin_lock_irq(&priv->lock);
  370. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  371. queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
  372. mcast->backoff * HZ);
  373. spin_unlock_irq(&priv->lock);
  374. mutex_unlock(&mcast_mutex);
  375. return status;
  376. }
  377. static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
  378. int create)
  379. {
  380. struct ipoib_dev_priv *priv = netdev_priv(dev);
  381. struct ib_sa_mcmember_rec rec = {
  382. .join_state = 1
  383. };
  384. ib_sa_comp_mask comp_mask;
  385. int ret = 0;
  386. ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
  387. rec.mgid = mcast->mcmember.mgid;
  388. rec.port_gid = priv->local_gid;
  389. rec.pkey = cpu_to_be16(priv->pkey);
  390. comp_mask =
  391. IB_SA_MCMEMBER_REC_MGID |
  392. IB_SA_MCMEMBER_REC_PORT_GID |
  393. IB_SA_MCMEMBER_REC_PKEY |
  394. IB_SA_MCMEMBER_REC_JOIN_STATE;
  395. if (create) {
  396. comp_mask |=
  397. IB_SA_MCMEMBER_REC_QKEY |
  398. IB_SA_MCMEMBER_REC_MTU_SELECTOR |
  399. IB_SA_MCMEMBER_REC_MTU |
  400. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
  401. IB_SA_MCMEMBER_REC_RATE_SELECTOR |
  402. IB_SA_MCMEMBER_REC_RATE |
  403. IB_SA_MCMEMBER_REC_SL |
  404. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  405. IB_SA_MCMEMBER_REC_HOP_LIMIT;
  406. rec.qkey = priv->broadcast->mcmember.qkey;
  407. rec.mtu_selector = IB_SA_EQ;
  408. rec.mtu = priv->broadcast->mcmember.mtu;
  409. rec.traffic_class = priv->broadcast->mcmember.traffic_class;
  410. rec.rate_selector = IB_SA_EQ;
  411. rec.rate = priv->broadcast->mcmember.rate;
  412. rec.sl = priv->broadcast->mcmember.sl;
  413. rec.flow_label = priv->broadcast->mcmember.flow_label;
  414. rec.hop_limit = priv->broadcast->mcmember.hop_limit;
  415. }
  416. set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  417. mcast->mc = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
  418. &rec, comp_mask, GFP_KERNEL,
  419. ipoib_mcast_join_complete, mcast);
  420. if (IS_ERR(mcast->mc)) {
  421. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  422. ret = PTR_ERR(mcast->mc);
  423. ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
  424. mcast->backoff *= 2;
  425. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  426. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  427. mutex_lock(&mcast_mutex);
  428. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  429. queue_delayed_work(ipoib_workqueue,
  430. &priv->mcast_task,
  431. mcast->backoff * HZ);
  432. mutex_unlock(&mcast_mutex);
  433. }
  434. }
  435. void ipoib_mcast_join_task(struct work_struct *work)
  436. {
  437. struct ipoib_dev_priv *priv =
  438. container_of(work, struct ipoib_dev_priv, mcast_task.work);
  439. struct net_device *dev = priv->dev;
  440. if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
  441. return;
  442. if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
  443. ipoib_warn(priv, "ib_query_gid() failed\n");
  444. else
  445. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  446. {
  447. struct ib_port_attr attr;
  448. if (!ib_query_port(priv->ca, priv->port, &attr))
  449. priv->local_lid = attr.lid;
  450. else
  451. ipoib_warn(priv, "ib_query_port failed\n");
  452. }
  453. if (!priv->broadcast) {
  454. struct ipoib_mcast *broadcast;
  455. if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  456. return;
  457. broadcast = ipoib_mcast_alloc(dev, 1);
  458. if (!broadcast) {
  459. ipoib_warn(priv, "failed to allocate broadcast group\n");
  460. mutex_lock(&mcast_mutex);
  461. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  462. queue_delayed_work(ipoib_workqueue,
  463. &priv->mcast_task, HZ);
  464. mutex_unlock(&mcast_mutex);
  465. return;
  466. }
  467. spin_lock_irq(&priv->lock);
  468. memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  469. sizeof (union ib_gid));
  470. priv->broadcast = broadcast;
  471. __ipoib_mcast_add(dev, priv->broadcast);
  472. spin_unlock_irq(&priv->lock);
  473. }
  474. if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  475. if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags))
  476. ipoib_mcast_join(dev, priv->broadcast, 0);
  477. return;
  478. }
  479. while (1) {
  480. struct ipoib_mcast *mcast = NULL;
  481. spin_lock_irq(&priv->lock);
  482. list_for_each_entry(mcast, &priv->multicast_list, list) {
  483. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
  484. && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
  485. && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  486. /* Found the next unjoined group */
  487. break;
  488. }
  489. }
  490. spin_unlock_irq(&priv->lock);
  491. if (&mcast->list == &priv->multicast_list) {
  492. /* All done */
  493. break;
  494. }
  495. ipoib_mcast_join(dev, mcast, 1);
  496. return;
  497. }
  498. priv->mcast_mtu = IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  499. if (!ipoib_cm_admin_enabled(dev)) {
  500. rtnl_lock();
  501. dev_set_mtu(dev, min(priv->mcast_mtu, priv->admin_mtu));
  502. rtnl_unlock();
  503. }
  504. ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
  505. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  506. }
  507. int ipoib_mcast_start_thread(struct net_device *dev)
  508. {
  509. struct ipoib_dev_priv *priv = netdev_priv(dev);
  510. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  511. mutex_lock(&mcast_mutex);
  512. if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
  513. queue_delayed_work(ipoib_workqueue, &priv->mcast_task, 0);
  514. mutex_unlock(&mcast_mutex);
  515. return 0;
  516. }
  517. int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
  518. {
  519. struct ipoib_dev_priv *priv = netdev_priv(dev);
  520. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  521. mutex_lock(&mcast_mutex);
  522. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  523. cancel_delayed_work(&priv->mcast_task);
  524. mutex_unlock(&mcast_mutex);
  525. if (flush)
  526. flush_workqueue(ipoib_workqueue);
  527. return 0;
  528. }
  529. static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  530. {
  531. struct ipoib_dev_priv *priv = netdev_priv(dev);
  532. int ret = 0;
  533. if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  534. ib_sa_free_multicast(mcast->mc);
  535. if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  536. ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
  537. mcast->mcmember.mgid.raw);
  538. /* Remove ourselves from the multicast group */
  539. ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
  540. be16_to_cpu(mcast->mcmember.mlid));
  541. if (ret)
  542. ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
  543. }
  544. return 0;
  545. }
  546. void ipoib_mcast_send(struct net_device *dev, void *mgid, struct sk_buff *skb)
  547. {
  548. struct ipoib_dev_priv *priv = netdev_priv(dev);
  549. struct ipoib_mcast *mcast;
  550. unsigned long flags;
  551. spin_lock_irqsave(&priv->lock, flags);
  552. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
  553. !priv->broadcast ||
  554. !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  555. ++dev->stats.tx_dropped;
  556. dev_kfree_skb_any(skb);
  557. goto unlock;
  558. }
  559. mcast = __ipoib_mcast_find(dev, mgid);
  560. if (!mcast) {
  561. /* Let's create a new send only group now */
  562. ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
  563. mgid);
  564. mcast = ipoib_mcast_alloc(dev, 0);
  565. if (!mcast) {
  566. ipoib_warn(priv, "unable to allocate memory for "
  567. "multicast structure\n");
  568. ++dev->stats.tx_dropped;
  569. dev_kfree_skb_any(skb);
  570. goto out;
  571. }
  572. set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
  573. memcpy(mcast->mcmember.mgid.raw, mgid, sizeof (union ib_gid));
  574. __ipoib_mcast_add(dev, mcast);
  575. list_add_tail(&mcast->list, &priv->multicast_list);
  576. }
  577. if (!mcast->ah) {
  578. if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
  579. skb_queue_tail(&mcast->pkt_queue, skb);
  580. else {
  581. ++dev->stats.tx_dropped;
  582. dev_kfree_skb_any(skb);
  583. }
  584. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  585. ipoib_dbg_mcast(priv, "no address vector, "
  586. "but multicast join already started\n");
  587. else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  588. ipoib_mcast_sendonly_join(mcast);
  589. /*
  590. * If lookup completes between here and out:, don't
  591. * want to send packet twice.
  592. */
  593. mcast = NULL;
  594. }
  595. out:
  596. if (mcast && mcast->ah) {
  597. struct dst_entry *dst = skb_dst(skb);
  598. struct neighbour *n = NULL;
  599. if (dst)
  600. n = dst_get_neighbour(dst);
  601. if (n && !*to_ipoib_neigh(n)) {
  602. struct ipoib_neigh *neigh = ipoib_neigh_alloc(n,
  603. skb->dev);
  604. if (neigh) {
  605. kref_get(&mcast->ah->ref);
  606. neigh->ah = mcast->ah;
  607. list_add_tail(&neigh->list, &mcast->neigh_list);
  608. }
  609. }
  610. spin_unlock_irqrestore(&priv->lock, flags);
  611. ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
  612. return;
  613. }
  614. unlock:
  615. spin_unlock_irqrestore(&priv->lock, flags);
  616. }
  617. void ipoib_mcast_dev_flush(struct net_device *dev)
  618. {
  619. struct ipoib_dev_priv *priv = netdev_priv(dev);
  620. LIST_HEAD(remove_list);
  621. struct ipoib_mcast *mcast, *tmcast;
  622. unsigned long flags;
  623. ipoib_dbg_mcast(priv, "flushing multicast list\n");
  624. spin_lock_irqsave(&priv->lock, flags);
  625. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  626. list_del(&mcast->list);
  627. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  628. list_add_tail(&mcast->list, &remove_list);
  629. }
  630. if (priv->broadcast) {
  631. rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
  632. list_add_tail(&priv->broadcast->list, &remove_list);
  633. priv->broadcast = NULL;
  634. }
  635. spin_unlock_irqrestore(&priv->lock, flags);
  636. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  637. ipoib_mcast_leave(dev, mcast);
  638. ipoib_mcast_free(mcast);
  639. }
  640. }
  641. static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
  642. {
  643. /* reserved QPN, prefix, scope */
  644. if (memcmp(addr, broadcast, 6))
  645. return 0;
  646. /* signature lower, pkey */
  647. if (memcmp(addr + 7, broadcast + 7, 3))
  648. return 0;
  649. return 1;
  650. }
  651. void ipoib_mcast_restart_task(struct work_struct *work)
  652. {
  653. struct ipoib_dev_priv *priv =
  654. container_of(work, struct ipoib_dev_priv, restart_task);
  655. struct net_device *dev = priv->dev;
  656. struct netdev_hw_addr *ha;
  657. struct ipoib_mcast *mcast, *tmcast;
  658. LIST_HEAD(remove_list);
  659. unsigned long flags;
  660. struct ib_sa_mcmember_rec rec;
  661. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  662. ipoib_mcast_stop_thread(dev, 0);
  663. local_irq_save(flags);
  664. netif_addr_lock(dev);
  665. spin_lock(&priv->lock);
  666. /*
  667. * Unfortunately, the networking core only gives us a list of all of
  668. * the multicast hardware addresses. We need to figure out which ones
  669. * are new and which ones have been removed
  670. */
  671. /* Clear out the found flag */
  672. list_for_each_entry(mcast, &priv->multicast_list, list)
  673. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  674. /* Mark all of the entries that are found or don't exist */
  675. netdev_for_each_mc_addr(ha, dev) {
  676. union ib_gid mgid;
  677. if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
  678. continue;
  679. memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
  680. mcast = __ipoib_mcast_find(dev, &mgid);
  681. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  682. struct ipoib_mcast *nmcast;
  683. /* ignore group which is directly joined by userspace */
  684. if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
  685. !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
  686. ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
  687. mgid.raw);
  688. continue;
  689. }
  690. /* Not found or send-only group, let's add a new entry */
  691. ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
  692. mgid.raw);
  693. nmcast = ipoib_mcast_alloc(dev, 0);
  694. if (!nmcast) {
  695. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  696. continue;
  697. }
  698. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  699. nmcast->mcmember.mgid = mgid;
  700. if (mcast) {
  701. /* Destroy the send only entry */
  702. list_move_tail(&mcast->list, &remove_list);
  703. rb_replace_node(&mcast->rb_node,
  704. &nmcast->rb_node,
  705. &priv->multicast_tree);
  706. } else
  707. __ipoib_mcast_add(dev, nmcast);
  708. list_add_tail(&nmcast->list, &priv->multicast_list);
  709. }
  710. if (mcast)
  711. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  712. }
  713. /* Remove all of the entries don't exist anymore */
  714. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  715. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  716. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  717. ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
  718. mcast->mcmember.mgid.raw);
  719. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  720. /* Move to the remove list */
  721. list_move_tail(&mcast->list, &remove_list);
  722. }
  723. }
  724. spin_unlock(&priv->lock);
  725. netif_addr_unlock(dev);
  726. local_irq_restore(flags);
  727. /* We have to cancel outside of the spinlock */
  728. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  729. ipoib_mcast_leave(mcast->dev, mcast);
  730. ipoib_mcast_free(mcast);
  731. }
  732. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  733. ipoib_mcast_start_thread(dev);
  734. }
  735. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  736. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  737. {
  738. struct ipoib_mcast_iter *iter;
  739. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  740. if (!iter)
  741. return NULL;
  742. iter->dev = dev;
  743. memset(iter->mgid.raw, 0, 16);
  744. if (ipoib_mcast_iter_next(iter)) {
  745. kfree(iter);
  746. return NULL;
  747. }
  748. return iter;
  749. }
  750. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  751. {
  752. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  753. struct rb_node *n;
  754. struct ipoib_mcast *mcast;
  755. int ret = 1;
  756. spin_lock_irq(&priv->lock);
  757. n = rb_first(&priv->multicast_tree);
  758. while (n) {
  759. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  760. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  761. sizeof (union ib_gid)) < 0) {
  762. iter->mgid = mcast->mcmember.mgid;
  763. iter->created = mcast->created;
  764. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  765. iter->complete = !!mcast->ah;
  766. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  767. ret = 0;
  768. break;
  769. }
  770. n = rb_next(n);
  771. }
  772. spin_unlock_irq(&priv->lock);
  773. return ret;
  774. }
  775. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  776. union ib_gid *mgid,
  777. unsigned long *created,
  778. unsigned int *queuelen,
  779. unsigned int *complete,
  780. unsigned int *send_only)
  781. {
  782. *mgid = iter->mgid;
  783. *created = iter->created;
  784. *queuelen = iter->queuelen;
  785. *complete = iter->complete;
  786. *send_only = iter->send_only;
  787. }
  788. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */