ipoib_multicast.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994
  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. * $Id: ipoib_multicast.c 1362 2004-12-18 15:56:29Z roland $
  35. */
  36. #include <linux/skbuff.h>
  37. #include <linux/rtnetlink.h>
  38. #include <linux/ip.h>
  39. #include <linux/in.h>
  40. #include <linux/igmp.h>
  41. #include <linux/inetdevice.h>
  42. #include <linux/delay.h>
  43. #include <linux/completion.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. /* Used for all multicast joins (broadcast, IPv4 mcast and IPv6 mcast) */
  54. struct ipoib_mcast {
  55. struct ib_sa_mcmember_rec mcmember;
  56. struct ipoib_ah *ah;
  57. struct rb_node rb_node;
  58. struct list_head list;
  59. struct completion done;
  60. int query_id;
  61. struct ib_sa_query *query;
  62. unsigned long created;
  63. unsigned long backoff;
  64. unsigned long flags;
  65. unsigned char logcount;
  66. struct list_head neigh_list;
  67. struct sk_buff_head pkt_queue;
  68. struct net_device *dev;
  69. };
  70. struct ipoib_mcast_iter {
  71. struct net_device *dev;
  72. union ib_gid mgid;
  73. unsigned long created;
  74. unsigned int queuelen;
  75. unsigned int complete;
  76. unsigned int send_only;
  77. };
  78. static void ipoib_mcast_free(struct ipoib_mcast *mcast)
  79. {
  80. struct net_device *dev = mcast->dev;
  81. struct ipoib_dev_priv *priv = netdev_priv(dev);
  82. struct ipoib_neigh *neigh, *tmp;
  83. unsigned long flags;
  84. int tx_dropped = 0;
  85. ipoib_dbg_mcast(netdev_priv(dev),
  86. "deleting multicast group " IPOIB_GID_FMT "\n",
  87. IPOIB_GID_ARG(mcast->mcmember.mgid));
  88. spin_lock_irqsave(&priv->lock, flags);
  89. list_for_each_entry_safe(neigh, tmp, &mcast->neigh_list, list) {
  90. /*
  91. * It's safe to call ipoib_put_ah() inside priv->lock
  92. * here, because we know that mcast->ah will always
  93. * hold one more reference, so ipoib_put_ah() will
  94. * never do more than decrement the ref count.
  95. */
  96. if (neigh->ah)
  97. ipoib_put_ah(neigh->ah);
  98. *to_ipoib_neigh(neigh->neighbour) = NULL;
  99. kfree(neigh);
  100. }
  101. spin_unlock_irqrestore(&priv->lock, flags);
  102. if (mcast->ah)
  103. ipoib_put_ah(mcast->ah);
  104. while (!skb_queue_empty(&mcast->pkt_queue)) {
  105. ++tx_dropped;
  106. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  107. }
  108. spin_lock_irqsave(&priv->tx_lock, flags);
  109. priv->stats.tx_dropped += tx_dropped;
  110. spin_unlock_irqrestore(&priv->tx_lock, flags);
  111. kfree(mcast);
  112. }
  113. static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
  114. int can_sleep)
  115. {
  116. struct ipoib_mcast *mcast;
  117. mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
  118. if (!mcast)
  119. return NULL;
  120. mcast->dev = dev;
  121. mcast->created = jiffies;
  122. mcast->backoff = 1;
  123. INIT_LIST_HEAD(&mcast->list);
  124. INIT_LIST_HEAD(&mcast->neigh_list);
  125. skb_queue_head_init(&mcast->pkt_queue);
  126. return mcast;
  127. }
  128. static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, union ib_gid *mgid)
  129. {
  130. struct ipoib_dev_priv *priv = netdev_priv(dev);
  131. struct rb_node *n = priv->multicast_tree.rb_node;
  132. while (n) {
  133. struct ipoib_mcast *mcast;
  134. int ret;
  135. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  136. ret = memcmp(mgid->raw, mcast->mcmember.mgid.raw,
  137. sizeof (union ib_gid));
  138. if (ret < 0)
  139. n = n->rb_left;
  140. else if (ret > 0)
  141. n = n->rb_right;
  142. else
  143. return mcast;
  144. }
  145. return NULL;
  146. }
  147. static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
  148. {
  149. struct ipoib_dev_priv *priv = netdev_priv(dev);
  150. struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
  151. while (*n) {
  152. struct ipoib_mcast *tmcast;
  153. int ret;
  154. pn = *n;
  155. tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
  156. ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
  157. sizeof (union ib_gid));
  158. if (ret < 0)
  159. n = &pn->rb_left;
  160. else if (ret > 0)
  161. n = &pn->rb_right;
  162. else
  163. return -EEXIST;
  164. }
  165. rb_link_node(&mcast->rb_node, pn, n);
  166. rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
  167. return 0;
  168. }
  169. static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
  170. struct ib_sa_mcmember_rec *mcmember)
  171. {
  172. struct net_device *dev = mcast->dev;
  173. struct ipoib_dev_priv *priv = netdev_priv(dev);
  174. struct ipoib_ah *ah;
  175. int ret;
  176. mcast->mcmember = *mcmember;
  177. /* Set the cached Q_Key before we attach if it's the broadcast group */
  178. if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  179. sizeof (union ib_gid))) {
  180. priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
  181. priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
  182. }
  183. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  184. if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  185. ipoib_warn(priv, "multicast group " IPOIB_GID_FMT
  186. " already attached\n",
  187. IPOIB_GID_ARG(mcast->mcmember.mgid));
  188. return 0;
  189. }
  190. ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
  191. &mcast->mcmember.mgid);
  192. if (ret < 0) {
  193. ipoib_warn(priv, "couldn't attach QP to multicast group "
  194. IPOIB_GID_FMT "\n",
  195. IPOIB_GID_ARG(mcast->mcmember.mgid));
  196. clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
  197. return ret;
  198. }
  199. }
  200. {
  201. struct ib_ah_attr av = {
  202. .dlid = be16_to_cpu(mcast->mcmember.mlid),
  203. .port_num = priv->port,
  204. .sl = mcast->mcmember.sl,
  205. .ah_flags = IB_AH_GRH,
  206. .grh = {
  207. .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
  208. .hop_limit = mcast->mcmember.hop_limit,
  209. .sgid_index = 0,
  210. .traffic_class = mcast->mcmember.traffic_class
  211. }
  212. };
  213. int path_rate = ib_sa_rate_enum_to_int(mcast->mcmember.rate);
  214. av.grh.dgid = mcast->mcmember.mgid;
  215. if (path_rate > 0 && priv->local_rate > path_rate)
  216. av.static_rate = (priv->local_rate - 1) / path_rate;
  217. ipoib_dbg_mcast(priv, "static_rate %d for local port %dX, mcmember %dX\n",
  218. av.static_rate, priv->local_rate,
  219. ib_sa_rate_enum_to_int(mcast->mcmember.rate));
  220. ah = ipoib_create_ah(dev, priv->pd, &av);
  221. if (!ah) {
  222. ipoib_warn(priv, "ib_address_create failed\n");
  223. } else {
  224. ipoib_dbg_mcast(priv, "MGID " IPOIB_GID_FMT
  225. " AV %p, LID 0x%04x, SL %d\n",
  226. IPOIB_GID_ARG(mcast->mcmember.mgid),
  227. mcast->ah->ah,
  228. be16_to_cpu(mcast->mcmember.mlid),
  229. mcast->mcmember.sl);
  230. }
  231. spin_lock_irq(&priv->lock);
  232. mcast->ah = ah;
  233. spin_unlock_irq(&priv->lock);
  234. }
  235. /* actually send any queued packets */
  236. spin_lock_irq(&priv->tx_lock);
  237. while (!skb_queue_empty(&mcast->pkt_queue)) {
  238. struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
  239. spin_unlock_irq(&priv->tx_lock);
  240. skb->dev = dev;
  241. if (!skb->dst || !skb->dst->neighbour) {
  242. /* put pseudoheader back on for next time */
  243. skb_push(skb, sizeof (struct ipoib_pseudoheader));
  244. }
  245. if (dev_queue_xmit(skb))
  246. ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
  247. spin_lock_irq(&priv->tx_lock);
  248. }
  249. spin_unlock_irq(&priv->tx_lock);
  250. return 0;
  251. }
  252. static void
  253. ipoib_mcast_sendonly_join_complete(int status,
  254. struct ib_sa_mcmember_rec *mcmember,
  255. void *mcast_ptr)
  256. {
  257. struct ipoib_mcast *mcast = mcast_ptr;
  258. struct net_device *dev = mcast->dev;
  259. struct ipoib_dev_priv *priv = netdev_priv(dev);
  260. if (!status)
  261. ipoib_mcast_join_finish(mcast, mcmember);
  262. else {
  263. if (mcast->logcount++ < 20)
  264. ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
  265. IPOIB_GID_FMT ", status %d\n",
  266. IPOIB_GID_ARG(mcast->mcmember.mgid), status);
  267. /* Flush out any queued packets */
  268. spin_lock_irq(&priv->tx_lock);
  269. while (!skb_queue_empty(&mcast->pkt_queue)) {
  270. ++priv->stats.tx_dropped;
  271. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  272. }
  273. spin_unlock_irq(&priv->tx_lock);
  274. /* Clear the busy flag so we try again */
  275. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  276. }
  277. complete(&mcast->done);
  278. }
  279. static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
  280. {
  281. struct net_device *dev = mcast->dev;
  282. struct ipoib_dev_priv *priv = netdev_priv(dev);
  283. struct ib_sa_mcmember_rec rec = {
  284. #if 0 /* Some SMs don't support send-only yet */
  285. .join_state = 4
  286. #else
  287. .join_state = 1
  288. #endif
  289. };
  290. int ret = 0;
  291. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  292. ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
  293. return -ENODEV;
  294. }
  295. if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
  296. ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
  297. return -EBUSY;
  298. }
  299. rec.mgid = mcast->mcmember.mgid;
  300. rec.port_gid = priv->local_gid;
  301. rec.pkey = cpu_to_be16(priv->pkey);
  302. init_completion(&mcast->done);
  303. ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec,
  304. IB_SA_MCMEMBER_REC_MGID |
  305. IB_SA_MCMEMBER_REC_PORT_GID |
  306. IB_SA_MCMEMBER_REC_PKEY |
  307. IB_SA_MCMEMBER_REC_JOIN_STATE,
  308. 1000, GFP_ATOMIC,
  309. ipoib_mcast_sendonly_join_complete,
  310. mcast, &mcast->query);
  311. if (ret < 0) {
  312. ipoib_warn(priv, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
  313. ret);
  314. } else {
  315. ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
  316. ", starting join\n",
  317. IPOIB_GID_ARG(mcast->mcmember.mgid));
  318. mcast->query_id = ret;
  319. }
  320. return ret;
  321. }
  322. static void ipoib_mcast_join_complete(int status,
  323. struct ib_sa_mcmember_rec *mcmember,
  324. void *mcast_ptr)
  325. {
  326. struct ipoib_mcast *mcast = mcast_ptr;
  327. struct net_device *dev = mcast->dev;
  328. struct ipoib_dev_priv *priv = netdev_priv(dev);
  329. ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
  330. " (status %d)\n",
  331. IPOIB_GID_ARG(mcast->mcmember.mgid), status);
  332. if (!status && !ipoib_mcast_join_finish(mcast, mcmember)) {
  333. mcast->backoff = 1;
  334. mutex_lock(&mcast_mutex);
  335. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  336. queue_work(ipoib_workqueue, &priv->mcast_task);
  337. mutex_unlock(&mcast_mutex);
  338. complete(&mcast->done);
  339. return;
  340. }
  341. if (status == -EINTR) {
  342. complete(&mcast->done);
  343. return;
  344. }
  345. if (status && mcast->logcount++ < 20) {
  346. if (status == -ETIMEDOUT || status == -EINTR) {
  347. ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
  348. ", status %d\n",
  349. IPOIB_GID_ARG(mcast->mcmember.mgid),
  350. status);
  351. } else {
  352. ipoib_warn(priv, "multicast join failed for "
  353. IPOIB_GID_FMT ", status %d\n",
  354. IPOIB_GID_ARG(mcast->mcmember.mgid),
  355. status);
  356. }
  357. }
  358. mcast->backoff *= 2;
  359. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  360. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  361. mutex_lock(&mcast_mutex);
  362. spin_lock_irq(&priv->lock);
  363. mcast->query = NULL;
  364. if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) {
  365. if (status == -ETIMEDOUT)
  366. queue_work(ipoib_workqueue, &priv->mcast_task);
  367. else
  368. queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
  369. mcast->backoff * HZ);
  370. } else
  371. complete(&mcast->done);
  372. spin_unlock_irq(&priv->lock);
  373. mutex_unlock(&mcast_mutex);
  374. return;
  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 " IPOIB_GID_FMT "\n",
  386. IPOIB_GID_ARG(mcast->mcmember.mgid));
  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_SL |
  399. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  400. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
  401. rec.qkey = priv->broadcast->mcmember.qkey;
  402. rec.sl = priv->broadcast->mcmember.sl;
  403. rec.flow_label = priv->broadcast->mcmember.flow_label;
  404. rec.traffic_class = priv->broadcast->mcmember.traffic_class;
  405. }
  406. init_completion(&mcast->done);
  407. ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec, comp_mask,
  408. mcast->backoff * 1000, GFP_ATOMIC,
  409. ipoib_mcast_join_complete,
  410. mcast, &mcast->query);
  411. if (ret < 0) {
  412. ipoib_warn(priv, "ib_sa_mcmember_rec_set failed, status %d\n", ret);
  413. mcast->backoff *= 2;
  414. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  415. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  416. mutex_lock(&mcast_mutex);
  417. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  418. queue_delayed_work(ipoib_workqueue,
  419. &priv->mcast_task,
  420. mcast->backoff * HZ);
  421. mutex_unlock(&mcast_mutex);
  422. } else
  423. mcast->query_id = ret;
  424. }
  425. void ipoib_mcast_join_task(void *dev_ptr)
  426. {
  427. struct net_device *dev = dev_ptr;
  428. struct ipoib_dev_priv *priv = netdev_priv(dev);
  429. if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
  430. return;
  431. if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
  432. ipoib_warn(priv, "ib_gid_entry_get() failed\n");
  433. else
  434. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  435. {
  436. struct ib_port_attr attr;
  437. if (!ib_query_port(priv->ca, priv->port, &attr)) {
  438. priv->local_lid = attr.lid;
  439. priv->local_rate = attr.active_speed *
  440. ib_width_enum_to_int(attr.active_width);
  441. } else
  442. ipoib_warn(priv, "ib_query_port failed\n");
  443. }
  444. if (!priv->broadcast) {
  445. struct ipoib_mcast *broadcast;
  446. broadcast = ipoib_mcast_alloc(dev, 1);
  447. if (!broadcast) {
  448. ipoib_warn(priv, "failed to allocate broadcast group\n");
  449. mutex_lock(&mcast_mutex);
  450. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  451. queue_delayed_work(ipoib_workqueue,
  452. &priv->mcast_task, HZ);
  453. mutex_unlock(&mcast_mutex);
  454. return;
  455. }
  456. spin_lock_irq(&priv->lock);
  457. memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  458. sizeof (union ib_gid));
  459. priv->broadcast = broadcast;
  460. __ipoib_mcast_add(dev, priv->broadcast);
  461. spin_unlock_irq(&priv->lock);
  462. }
  463. if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  464. ipoib_mcast_join(dev, priv->broadcast, 0);
  465. return;
  466. }
  467. while (1) {
  468. struct ipoib_mcast *mcast = NULL;
  469. spin_lock_irq(&priv->lock);
  470. list_for_each_entry(mcast, &priv->multicast_list, list) {
  471. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
  472. && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
  473. && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  474. /* Found the next unjoined group */
  475. break;
  476. }
  477. }
  478. spin_unlock_irq(&priv->lock);
  479. if (&mcast->list == &priv->multicast_list) {
  480. /* All done */
  481. break;
  482. }
  483. ipoib_mcast_join(dev, mcast, 1);
  484. return;
  485. }
  486. priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
  487. IPOIB_ENCAP_LEN;
  488. dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
  489. ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
  490. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  491. netif_carrier_on(dev);
  492. }
  493. int ipoib_mcast_start_thread(struct net_device *dev)
  494. {
  495. struct ipoib_dev_priv *priv = netdev_priv(dev);
  496. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  497. mutex_lock(&mcast_mutex);
  498. if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
  499. queue_work(ipoib_workqueue, &priv->mcast_task);
  500. mutex_unlock(&mcast_mutex);
  501. spin_lock_irq(&priv->lock);
  502. set_bit(IPOIB_MCAST_STARTED, &priv->flags);
  503. spin_unlock_irq(&priv->lock);
  504. return 0;
  505. }
  506. int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
  507. {
  508. struct ipoib_dev_priv *priv = netdev_priv(dev);
  509. struct ipoib_mcast *mcast;
  510. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  511. spin_lock_irq(&priv->lock);
  512. clear_bit(IPOIB_MCAST_STARTED, &priv->flags);
  513. spin_unlock_irq(&priv->lock);
  514. mutex_lock(&mcast_mutex);
  515. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  516. cancel_delayed_work(&priv->mcast_task);
  517. mutex_unlock(&mcast_mutex);
  518. if (flush)
  519. flush_workqueue(ipoib_workqueue);
  520. spin_lock_irq(&priv->lock);
  521. if (priv->broadcast && priv->broadcast->query) {
  522. ib_sa_cancel_query(priv->broadcast->query_id, priv->broadcast->query);
  523. priv->broadcast->query = NULL;
  524. spin_unlock_irq(&priv->lock);
  525. ipoib_dbg_mcast(priv, "waiting for bcast\n");
  526. wait_for_completion(&priv->broadcast->done);
  527. } else
  528. spin_unlock_irq(&priv->lock);
  529. list_for_each_entry(mcast, &priv->multicast_list, list) {
  530. spin_lock_irq(&priv->lock);
  531. if (mcast->query) {
  532. ib_sa_cancel_query(mcast->query_id, mcast->query);
  533. mcast->query = NULL;
  534. spin_unlock_irq(&priv->lock);
  535. ipoib_dbg_mcast(priv, "waiting for MGID " IPOIB_GID_FMT "\n",
  536. IPOIB_GID_ARG(mcast->mcmember.mgid));
  537. wait_for_completion(&mcast->done);
  538. } else
  539. spin_unlock_irq(&priv->lock);
  540. }
  541. return 0;
  542. }
  543. static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  544. {
  545. struct ipoib_dev_priv *priv = netdev_priv(dev);
  546. struct ib_sa_mcmember_rec rec = {
  547. .join_state = 1
  548. };
  549. int ret = 0;
  550. if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags))
  551. return 0;
  552. ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
  553. IPOIB_GID_ARG(mcast->mcmember.mgid));
  554. rec.mgid = mcast->mcmember.mgid;
  555. rec.port_gid = priv->local_gid;
  556. rec.pkey = cpu_to_be16(priv->pkey);
  557. /* Remove ourselves from the multicast group */
  558. ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
  559. &mcast->mcmember.mgid);
  560. if (ret)
  561. ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
  562. /*
  563. * Just make one shot at leaving and don't wait for a reply;
  564. * if we fail, too bad.
  565. */
  566. ret = ib_sa_mcmember_rec_delete(priv->ca, priv->port, &rec,
  567. IB_SA_MCMEMBER_REC_MGID |
  568. IB_SA_MCMEMBER_REC_PORT_GID |
  569. IB_SA_MCMEMBER_REC_PKEY |
  570. IB_SA_MCMEMBER_REC_JOIN_STATE,
  571. 0, GFP_ATOMIC, NULL,
  572. mcast, &mcast->query);
  573. if (ret < 0)
  574. ipoib_warn(priv, "ib_sa_mcmember_rec_delete failed "
  575. "for leave (result = %d)\n", ret);
  576. return 0;
  577. }
  578. void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid,
  579. struct sk_buff *skb)
  580. {
  581. struct ipoib_dev_priv *priv = netdev_priv(dev);
  582. struct ipoib_mcast *mcast;
  583. /*
  584. * We can only be called from ipoib_start_xmit, so we're
  585. * inside tx_lock -- no need to save/restore flags.
  586. */
  587. spin_lock(&priv->lock);
  588. if (!test_bit(IPOIB_MCAST_STARTED, &priv->flags) ||
  589. !priv->broadcast ||
  590. !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  591. ++priv->stats.tx_dropped;
  592. dev_kfree_skb_any(skb);
  593. goto unlock;
  594. }
  595. mcast = __ipoib_mcast_find(dev, mgid);
  596. if (!mcast) {
  597. /* Let's create a new send only group now */
  598. ipoib_dbg_mcast(priv, "setting up send only multicast group for "
  599. IPOIB_GID_FMT "\n", IPOIB_GID_ARG(*mgid));
  600. mcast = ipoib_mcast_alloc(dev, 0);
  601. if (!mcast) {
  602. ipoib_warn(priv, "unable to allocate memory for "
  603. "multicast structure\n");
  604. ++priv->stats.tx_dropped;
  605. dev_kfree_skb_any(skb);
  606. goto out;
  607. }
  608. set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
  609. mcast->mcmember.mgid = *mgid;
  610. __ipoib_mcast_add(dev, mcast);
  611. list_add_tail(&mcast->list, &priv->multicast_list);
  612. }
  613. if (!mcast->ah) {
  614. if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
  615. skb_queue_tail(&mcast->pkt_queue, skb);
  616. else {
  617. ++priv->stats.tx_dropped;
  618. dev_kfree_skb_any(skb);
  619. }
  620. if (mcast->query)
  621. ipoib_dbg_mcast(priv, "no address vector, "
  622. "but multicast join already started\n");
  623. else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  624. ipoib_mcast_sendonly_join(mcast);
  625. /*
  626. * If lookup completes between here and out:, don't
  627. * want to send packet twice.
  628. */
  629. mcast = NULL;
  630. }
  631. out:
  632. if (mcast && mcast->ah) {
  633. if (skb->dst &&
  634. skb->dst->neighbour &&
  635. !*to_ipoib_neigh(skb->dst->neighbour)) {
  636. struct ipoib_neigh *neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
  637. if (neigh) {
  638. kref_get(&mcast->ah->ref);
  639. neigh->ah = mcast->ah;
  640. neigh->neighbour = skb->dst->neighbour;
  641. *to_ipoib_neigh(skb->dst->neighbour) = neigh;
  642. list_add_tail(&neigh->list, &mcast->neigh_list);
  643. }
  644. }
  645. ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
  646. }
  647. unlock:
  648. spin_unlock(&priv->lock);
  649. }
  650. void ipoib_mcast_dev_flush(struct net_device *dev)
  651. {
  652. struct ipoib_dev_priv *priv = netdev_priv(dev);
  653. LIST_HEAD(remove_list);
  654. struct ipoib_mcast *mcast, *tmcast;
  655. unsigned long flags;
  656. ipoib_dbg_mcast(priv, "flushing multicast list\n");
  657. spin_lock_irqsave(&priv->lock, flags);
  658. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  659. list_del(&mcast->list);
  660. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  661. list_add_tail(&mcast->list, &remove_list);
  662. }
  663. if (priv->broadcast) {
  664. rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
  665. list_add_tail(&priv->broadcast->list, &remove_list);
  666. priv->broadcast = NULL;
  667. }
  668. spin_unlock_irqrestore(&priv->lock, flags);
  669. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  670. ipoib_mcast_leave(dev, mcast);
  671. ipoib_mcast_free(mcast);
  672. }
  673. }
  674. void ipoib_mcast_restart_task(void *dev_ptr)
  675. {
  676. struct net_device *dev = dev_ptr;
  677. struct ipoib_dev_priv *priv = netdev_priv(dev);
  678. struct dev_mc_list *mclist;
  679. struct ipoib_mcast *mcast, *tmcast;
  680. LIST_HEAD(remove_list);
  681. unsigned long flags;
  682. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  683. ipoib_mcast_stop_thread(dev, 0);
  684. spin_lock_irqsave(&dev->xmit_lock, flags);
  685. spin_lock(&priv->lock);
  686. /*
  687. * Unfortunately, the networking core only gives us a list of all of
  688. * the multicast hardware addresses. We need to figure out which ones
  689. * are new and which ones have been removed
  690. */
  691. /* Clear out the found flag */
  692. list_for_each_entry(mcast, &priv->multicast_list, list)
  693. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  694. /* Mark all of the entries that are found or don't exist */
  695. for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
  696. union ib_gid mgid;
  697. memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
  698. /* Add in the P_Key */
  699. mgid.raw[4] = (priv->pkey >> 8) & 0xff;
  700. mgid.raw[5] = priv->pkey & 0xff;
  701. mcast = __ipoib_mcast_find(dev, &mgid);
  702. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  703. struct ipoib_mcast *nmcast;
  704. /* Not found or send-only group, let's add a new entry */
  705. ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
  706. IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
  707. nmcast = ipoib_mcast_alloc(dev, 0);
  708. if (!nmcast) {
  709. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  710. continue;
  711. }
  712. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  713. nmcast->mcmember.mgid = mgid;
  714. if (mcast) {
  715. /* Destroy the send only entry */
  716. list_del(&mcast->list);
  717. list_add_tail(&mcast->list, &remove_list);
  718. rb_replace_node(&mcast->rb_node,
  719. &nmcast->rb_node,
  720. &priv->multicast_tree);
  721. } else
  722. __ipoib_mcast_add(dev, nmcast);
  723. list_add_tail(&nmcast->list, &priv->multicast_list);
  724. }
  725. if (mcast)
  726. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  727. }
  728. /* Remove all of the entries don't exist anymore */
  729. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  730. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  731. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  732. ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
  733. IPOIB_GID_ARG(mcast->mcmember.mgid));
  734. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  735. /* Move to the remove list */
  736. list_del(&mcast->list);
  737. list_add_tail(&mcast->list, &remove_list);
  738. }
  739. }
  740. spin_unlock(&priv->lock);
  741. spin_unlock_irqrestore(&dev->xmit_lock, flags);
  742. /* We have to cancel outside of the spinlock */
  743. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  744. ipoib_mcast_leave(mcast->dev, mcast);
  745. ipoib_mcast_free(mcast);
  746. }
  747. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  748. ipoib_mcast_start_thread(dev);
  749. }
  750. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  751. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  752. {
  753. struct ipoib_mcast_iter *iter;
  754. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  755. if (!iter)
  756. return NULL;
  757. iter->dev = dev;
  758. memset(iter->mgid.raw, 0, 16);
  759. if (ipoib_mcast_iter_next(iter)) {
  760. kfree(iter);
  761. return NULL;
  762. }
  763. return iter;
  764. }
  765. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  766. {
  767. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  768. struct rb_node *n;
  769. struct ipoib_mcast *mcast;
  770. int ret = 1;
  771. spin_lock_irq(&priv->lock);
  772. n = rb_first(&priv->multicast_tree);
  773. while (n) {
  774. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  775. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  776. sizeof (union ib_gid)) < 0) {
  777. iter->mgid = mcast->mcmember.mgid;
  778. iter->created = mcast->created;
  779. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  780. iter->complete = !!mcast->ah;
  781. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  782. ret = 0;
  783. break;
  784. }
  785. n = rb_next(n);
  786. }
  787. spin_unlock_irq(&priv->lock);
  788. return ret;
  789. }
  790. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  791. union ib_gid *mgid,
  792. unsigned long *created,
  793. unsigned int *queuelen,
  794. unsigned int *complete,
  795. unsigned int *send_only)
  796. {
  797. *mgid = iter->mgid;
  798. *created = iter->created;
  799. *queuelen = iter->queuelen;
  800. *complete = iter->complete;
  801. *send_only = iter->send_only;
  802. }
  803. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */