ipoib_multicast.c 25 KB

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