ipoib_multicast.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961
  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. neigh->neighbour->ops->destructor = NULL;
  100. kfree(neigh);
  101. }
  102. spin_unlock_irqrestore(&priv->lock, flags);
  103. if (mcast->ah)
  104. ipoib_put_ah(mcast->ah);
  105. while (!skb_queue_empty(&mcast->pkt_queue)) {
  106. ++tx_dropped;
  107. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  108. }
  109. spin_lock_irqsave(&priv->tx_lock, flags);
  110. priv->stats.tx_dropped += tx_dropped;
  111. spin_unlock_irqrestore(&priv->tx_lock, flags);
  112. kfree(mcast);
  113. }
  114. static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
  115. int can_sleep)
  116. {
  117. struct ipoib_mcast *mcast;
  118. mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
  119. if (!mcast)
  120. return NULL;
  121. mcast->dev = dev;
  122. mcast->created = jiffies;
  123. mcast->backoff = 1;
  124. INIT_LIST_HEAD(&mcast->list);
  125. INIT_LIST_HEAD(&mcast->neigh_list);
  126. skb_queue_head_init(&mcast->pkt_queue);
  127. return mcast;
  128. }
  129. static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, union ib_gid *mgid)
  130. {
  131. struct ipoib_dev_priv *priv = netdev_priv(dev);
  132. struct rb_node *n = priv->multicast_tree.rb_node;
  133. while (n) {
  134. struct ipoib_mcast *mcast;
  135. int ret;
  136. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  137. ret = memcmp(mgid->raw, mcast->mcmember.mgid.raw,
  138. sizeof (union ib_gid));
  139. if (ret < 0)
  140. n = n->rb_left;
  141. else if (ret > 0)
  142. n = n->rb_right;
  143. else
  144. return mcast;
  145. }
  146. return NULL;
  147. }
  148. static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
  149. {
  150. struct ipoib_dev_priv *priv = netdev_priv(dev);
  151. struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
  152. while (*n) {
  153. struct ipoib_mcast *tmcast;
  154. int ret;
  155. pn = *n;
  156. tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
  157. ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
  158. sizeof (union ib_gid));
  159. if (ret < 0)
  160. n = &pn->rb_left;
  161. else if (ret > 0)
  162. n = &pn->rb_right;
  163. else
  164. return -EEXIST;
  165. }
  166. rb_link_node(&mcast->rb_node, pn, n);
  167. rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
  168. return 0;
  169. }
  170. static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
  171. struct ib_sa_mcmember_rec *mcmember)
  172. {
  173. struct net_device *dev = mcast->dev;
  174. struct ipoib_dev_priv *priv = netdev_priv(dev);
  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. mcast->ah = ipoib_create_ah(dev, priv->pd, &av);
  221. if (!mcast->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. }
  232. /* actually send any queued packets */
  233. spin_lock_irq(&priv->tx_lock);
  234. while (!skb_queue_empty(&mcast->pkt_queue)) {
  235. struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
  236. spin_unlock_irq(&priv->tx_lock);
  237. skb->dev = dev;
  238. if (!skb->dst || !skb->dst->neighbour) {
  239. /* put pseudoheader back on for next time */
  240. skb_push(skb, sizeof (struct ipoib_pseudoheader));
  241. }
  242. if (dev_queue_xmit(skb))
  243. ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
  244. spin_lock_irq(&priv->tx_lock);
  245. }
  246. spin_unlock_irq(&priv->tx_lock);
  247. return 0;
  248. }
  249. static void
  250. ipoib_mcast_sendonly_join_complete(int status,
  251. struct ib_sa_mcmember_rec *mcmember,
  252. void *mcast_ptr)
  253. {
  254. struct ipoib_mcast *mcast = mcast_ptr;
  255. struct net_device *dev = mcast->dev;
  256. struct ipoib_dev_priv *priv = netdev_priv(dev);
  257. if (!status)
  258. ipoib_mcast_join_finish(mcast, mcmember);
  259. else {
  260. if (mcast->logcount++ < 20)
  261. ipoib_dbg_mcast(netdev_priv(dev), "multicast join failed for "
  262. IPOIB_GID_FMT ", status %d\n",
  263. IPOIB_GID_ARG(mcast->mcmember.mgid), status);
  264. /* Flush out any queued packets */
  265. spin_lock_irq(&priv->tx_lock);
  266. while (!skb_queue_empty(&mcast->pkt_queue)) {
  267. ++priv->stats.tx_dropped;
  268. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  269. }
  270. spin_unlock_irq(&priv->tx_lock);
  271. /* Clear the busy flag so we try again */
  272. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  273. }
  274. complete(&mcast->done);
  275. }
  276. static int ipoib_mcast_sendonly_join(struct ipoib_mcast *mcast)
  277. {
  278. struct net_device *dev = mcast->dev;
  279. struct ipoib_dev_priv *priv = netdev_priv(dev);
  280. struct ib_sa_mcmember_rec rec = {
  281. #if 0 /* Some SMs don't support send-only yet */
  282. .join_state = 4
  283. #else
  284. .join_state = 1
  285. #endif
  286. };
  287. int ret = 0;
  288. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  289. ipoib_dbg_mcast(priv, "device shutting down, no multicast joins\n");
  290. return -ENODEV;
  291. }
  292. if (test_and_set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
  293. ipoib_dbg_mcast(priv, "multicast entry busy, skipping\n");
  294. return -EBUSY;
  295. }
  296. rec.mgid = mcast->mcmember.mgid;
  297. rec.port_gid = priv->local_gid;
  298. rec.pkey = cpu_to_be16(priv->pkey);
  299. init_completion(&mcast->done);
  300. ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec,
  301. IB_SA_MCMEMBER_REC_MGID |
  302. IB_SA_MCMEMBER_REC_PORT_GID |
  303. IB_SA_MCMEMBER_REC_PKEY |
  304. IB_SA_MCMEMBER_REC_JOIN_STATE,
  305. 1000, GFP_ATOMIC,
  306. ipoib_mcast_sendonly_join_complete,
  307. mcast, &mcast->query);
  308. if (ret < 0) {
  309. ipoib_warn(priv, "ib_sa_mcmember_rec_set failed (ret = %d)\n",
  310. ret);
  311. } else {
  312. ipoib_dbg_mcast(priv, "no multicast record for " IPOIB_GID_FMT
  313. ", starting join\n",
  314. IPOIB_GID_ARG(mcast->mcmember.mgid));
  315. mcast->query_id = ret;
  316. }
  317. return ret;
  318. }
  319. static void ipoib_mcast_join_complete(int status,
  320. struct ib_sa_mcmember_rec *mcmember,
  321. void *mcast_ptr)
  322. {
  323. struct ipoib_mcast *mcast = mcast_ptr;
  324. struct net_device *dev = mcast->dev;
  325. struct ipoib_dev_priv *priv = netdev_priv(dev);
  326. ipoib_dbg_mcast(priv, "join completion for " IPOIB_GID_FMT
  327. " (status %d)\n",
  328. IPOIB_GID_ARG(mcast->mcmember.mgid), status);
  329. if (!status && !ipoib_mcast_join_finish(mcast, mcmember)) {
  330. mcast->backoff = 1;
  331. mutex_lock(&mcast_mutex);
  332. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  333. queue_work(ipoib_workqueue, &priv->mcast_task);
  334. mutex_unlock(&mcast_mutex);
  335. complete(&mcast->done);
  336. return;
  337. }
  338. if (status == -EINTR) {
  339. complete(&mcast->done);
  340. return;
  341. }
  342. if (status && mcast->logcount++ < 20) {
  343. if (status == -ETIMEDOUT || status == -EINTR) {
  344. ipoib_dbg_mcast(priv, "multicast join failed for " IPOIB_GID_FMT
  345. ", status %d\n",
  346. IPOIB_GID_ARG(mcast->mcmember.mgid),
  347. status);
  348. } else {
  349. ipoib_warn(priv, "multicast join failed for "
  350. IPOIB_GID_FMT ", status %d\n",
  351. IPOIB_GID_ARG(mcast->mcmember.mgid),
  352. status);
  353. }
  354. }
  355. mcast->backoff *= 2;
  356. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  357. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  358. mcast->query = NULL;
  359. mutex_lock(&mcast_mutex);
  360. if (test_bit(IPOIB_MCAST_RUN, &priv->flags)) {
  361. if (status == -ETIMEDOUT)
  362. queue_work(ipoib_workqueue, &priv->mcast_task);
  363. else
  364. queue_delayed_work(ipoib_workqueue, &priv->mcast_task,
  365. mcast->backoff * HZ);
  366. } else
  367. complete(&mcast->done);
  368. mutex_unlock(&mcast_mutex);
  369. return;
  370. }
  371. static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
  372. int create)
  373. {
  374. struct ipoib_dev_priv *priv = netdev_priv(dev);
  375. struct ib_sa_mcmember_rec rec = {
  376. .join_state = 1
  377. };
  378. ib_sa_comp_mask comp_mask;
  379. int ret = 0;
  380. ipoib_dbg_mcast(priv, "joining MGID " IPOIB_GID_FMT "\n",
  381. IPOIB_GID_ARG(mcast->mcmember.mgid));
  382. rec.mgid = mcast->mcmember.mgid;
  383. rec.port_gid = priv->local_gid;
  384. rec.pkey = cpu_to_be16(priv->pkey);
  385. comp_mask =
  386. IB_SA_MCMEMBER_REC_MGID |
  387. IB_SA_MCMEMBER_REC_PORT_GID |
  388. IB_SA_MCMEMBER_REC_PKEY |
  389. IB_SA_MCMEMBER_REC_JOIN_STATE;
  390. if (create) {
  391. comp_mask |=
  392. IB_SA_MCMEMBER_REC_QKEY |
  393. IB_SA_MCMEMBER_REC_SL |
  394. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  395. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
  396. rec.qkey = priv->broadcast->mcmember.qkey;
  397. rec.sl = priv->broadcast->mcmember.sl;
  398. rec.flow_label = priv->broadcast->mcmember.flow_label;
  399. rec.traffic_class = priv->broadcast->mcmember.traffic_class;
  400. }
  401. init_completion(&mcast->done);
  402. ret = ib_sa_mcmember_rec_set(priv->ca, priv->port, &rec, comp_mask,
  403. mcast->backoff * 1000, GFP_ATOMIC,
  404. ipoib_mcast_join_complete,
  405. mcast, &mcast->query);
  406. if (ret < 0) {
  407. ipoib_warn(priv, "ib_sa_mcmember_rec_set failed, status %d\n", ret);
  408. mcast->backoff *= 2;
  409. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  410. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  411. mutex_lock(&mcast_mutex);
  412. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  413. queue_delayed_work(ipoib_workqueue,
  414. &priv->mcast_task,
  415. mcast->backoff * HZ);
  416. mutex_unlock(&mcast_mutex);
  417. } else
  418. mcast->query_id = ret;
  419. }
  420. void ipoib_mcast_join_task(void *dev_ptr)
  421. {
  422. struct net_device *dev = dev_ptr;
  423. struct ipoib_dev_priv *priv = netdev_priv(dev);
  424. if (!test_bit(IPOIB_MCAST_RUN, &priv->flags))
  425. return;
  426. if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
  427. ipoib_warn(priv, "ib_gid_entry_get() failed\n");
  428. else
  429. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  430. {
  431. struct ib_port_attr attr;
  432. if (!ib_query_port(priv->ca, priv->port, &attr)) {
  433. priv->local_lid = attr.lid;
  434. priv->local_rate = attr.active_speed *
  435. ib_width_enum_to_int(attr.active_width);
  436. } else
  437. ipoib_warn(priv, "ib_query_port failed\n");
  438. }
  439. if (!priv->broadcast) {
  440. priv->broadcast = ipoib_mcast_alloc(dev, 1);
  441. if (!priv->broadcast) {
  442. ipoib_warn(priv, "failed to allocate broadcast group\n");
  443. mutex_lock(&mcast_mutex);
  444. if (test_bit(IPOIB_MCAST_RUN, &priv->flags))
  445. queue_delayed_work(ipoib_workqueue,
  446. &priv->mcast_task, HZ);
  447. mutex_unlock(&mcast_mutex);
  448. return;
  449. }
  450. memcpy(priv->broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  451. sizeof (union ib_gid));
  452. spin_lock_irq(&priv->lock);
  453. __ipoib_mcast_add(dev, priv->broadcast);
  454. spin_unlock_irq(&priv->lock);
  455. }
  456. if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  457. ipoib_mcast_join(dev, priv->broadcast, 0);
  458. return;
  459. }
  460. while (1) {
  461. struct ipoib_mcast *mcast = NULL;
  462. spin_lock_irq(&priv->lock);
  463. list_for_each_entry(mcast, &priv->multicast_list, list) {
  464. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)
  465. && !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)
  466. && !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  467. /* Found the next unjoined group */
  468. break;
  469. }
  470. }
  471. spin_unlock_irq(&priv->lock);
  472. if (&mcast->list == &priv->multicast_list) {
  473. /* All done */
  474. break;
  475. }
  476. ipoib_mcast_join(dev, mcast, 1);
  477. return;
  478. }
  479. priv->mcast_mtu = ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu) -
  480. IPOIB_ENCAP_LEN;
  481. dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
  482. ipoib_dbg_mcast(priv, "successfully joined all multicast groups\n");
  483. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  484. netif_carrier_on(dev);
  485. }
  486. int ipoib_mcast_start_thread(struct net_device *dev)
  487. {
  488. struct ipoib_dev_priv *priv = netdev_priv(dev);
  489. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  490. mutex_lock(&mcast_mutex);
  491. if (!test_and_set_bit(IPOIB_MCAST_RUN, &priv->flags))
  492. queue_work(ipoib_workqueue, &priv->mcast_task);
  493. mutex_unlock(&mcast_mutex);
  494. return 0;
  495. }
  496. int ipoib_mcast_stop_thread(struct net_device *dev, int flush)
  497. {
  498. struct ipoib_dev_priv *priv = netdev_priv(dev);
  499. struct ipoib_mcast *mcast;
  500. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  501. mutex_lock(&mcast_mutex);
  502. clear_bit(IPOIB_MCAST_RUN, &priv->flags);
  503. cancel_delayed_work(&priv->mcast_task);
  504. mutex_unlock(&mcast_mutex);
  505. if (flush)
  506. flush_workqueue(ipoib_workqueue);
  507. if (priv->broadcast && priv->broadcast->query) {
  508. ib_sa_cancel_query(priv->broadcast->query_id, priv->broadcast->query);
  509. priv->broadcast->query = NULL;
  510. ipoib_dbg_mcast(priv, "waiting for bcast\n");
  511. wait_for_completion(&priv->broadcast->done);
  512. }
  513. list_for_each_entry(mcast, &priv->multicast_list, list) {
  514. if (mcast->query) {
  515. ib_sa_cancel_query(mcast->query_id, mcast->query);
  516. mcast->query = NULL;
  517. ipoib_dbg_mcast(priv, "waiting for MGID " IPOIB_GID_FMT "\n",
  518. IPOIB_GID_ARG(mcast->mcmember.mgid));
  519. wait_for_completion(&mcast->done);
  520. }
  521. }
  522. return 0;
  523. }
  524. static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  525. {
  526. struct ipoib_dev_priv *priv = netdev_priv(dev);
  527. struct ib_sa_mcmember_rec rec = {
  528. .join_state = 1
  529. };
  530. int ret = 0;
  531. if (!test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags))
  532. return 0;
  533. ipoib_dbg_mcast(priv, "leaving MGID " IPOIB_GID_FMT "\n",
  534. IPOIB_GID_ARG(mcast->mcmember.mgid));
  535. rec.mgid = mcast->mcmember.mgid;
  536. rec.port_gid = priv->local_gid;
  537. rec.pkey = cpu_to_be16(priv->pkey);
  538. /* Remove ourselves from the multicast group */
  539. ret = ipoib_mcast_detach(dev, be16_to_cpu(mcast->mcmember.mlid),
  540. &mcast->mcmember.mgid);
  541. if (ret)
  542. ipoib_warn(priv, "ipoib_mcast_detach failed (result = %d)\n", ret);
  543. /*
  544. * Just make one shot at leaving and don't wait for a reply;
  545. * if we fail, too bad.
  546. */
  547. ret = ib_sa_mcmember_rec_delete(priv->ca, priv->port, &rec,
  548. IB_SA_MCMEMBER_REC_MGID |
  549. IB_SA_MCMEMBER_REC_PORT_GID |
  550. IB_SA_MCMEMBER_REC_PKEY |
  551. IB_SA_MCMEMBER_REC_JOIN_STATE,
  552. 0, GFP_ATOMIC, NULL,
  553. mcast, &mcast->query);
  554. if (ret < 0)
  555. ipoib_warn(priv, "ib_sa_mcmember_rec_delete failed "
  556. "for leave (result = %d)\n", ret);
  557. return 0;
  558. }
  559. void ipoib_mcast_send(struct net_device *dev, union ib_gid *mgid,
  560. struct sk_buff *skb)
  561. {
  562. struct ipoib_dev_priv *priv = netdev_priv(dev);
  563. struct ipoib_mcast *mcast;
  564. /*
  565. * We can only be called from ipoib_start_xmit, so we're
  566. * inside tx_lock -- no need to save/restore flags.
  567. */
  568. spin_lock(&priv->lock);
  569. mcast = __ipoib_mcast_find(dev, mgid);
  570. if (!mcast) {
  571. /* Let's create a new send only group now */
  572. ipoib_dbg_mcast(priv, "setting up send only multicast group for "
  573. IPOIB_GID_FMT "\n", IPOIB_GID_ARG(*mgid));
  574. mcast = ipoib_mcast_alloc(dev, 0);
  575. if (!mcast) {
  576. ipoib_warn(priv, "unable to allocate memory for "
  577. "multicast structure\n");
  578. ++priv->stats.tx_dropped;
  579. dev_kfree_skb_any(skb);
  580. goto out;
  581. }
  582. set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
  583. mcast->mcmember.mgid = *mgid;
  584. __ipoib_mcast_add(dev, mcast);
  585. list_add_tail(&mcast->list, &priv->multicast_list);
  586. }
  587. if (!mcast->ah) {
  588. if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
  589. skb_queue_tail(&mcast->pkt_queue, skb);
  590. else {
  591. ++priv->stats.tx_dropped;
  592. dev_kfree_skb_any(skb);
  593. }
  594. if (mcast->query)
  595. ipoib_dbg_mcast(priv, "no address vector, "
  596. "but multicast join already started\n");
  597. else if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  598. ipoib_mcast_sendonly_join(mcast);
  599. /*
  600. * If lookup completes between here and out:, don't
  601. * want to send packet twice.
  602. */
  603. mcast = NULL;
  604. }
  605. out:
  606. if (mcast && mcast->ah) {
  607. if (skb->dst &&
  608. skb->dst->neighbour &&
  609. !*to_ipoib_neigh(skb->dst->neighbour)) {
  610. struct ipoib_neigh *neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
  611. if (neigh) {
  612. kref_get(&mcast->ah->ref);
  613. neigh->ah = mcast->ah;
  614. neigh->neighbour = skb->dst->neighbour;
  615. *to_ipoib_neigh(skb->dst->neighbour) = neigh;
  616. list_add_tail(&neigh->list, &mcast->neigh_list);
  617. }
  618. }
  619. ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
  620. }
  621. spin_unlock(&priv->lock);
  622. }
  623. void ipoib_mcast_dev_flush(struct net_device *dev)
  624. {
  625. struct ipoib_dev_priv *priv = netdev_priv(dev);
  626. LIST_HEAD(remove_list);
  627. struct ipoib_mcast *mcast, *tmcast;
  628. unsigned long flags;
  629. ipoib_dbg_mcast(priv, "flushing multicast list\n");
  630. spin_lock_irqsave(&priv->lock, flags);
  631. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  632. list_del(&mcast->list);
  633. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  634. list_add_tail(&mcast->list, &remove_list);
  635. }
  636. if (priv->broadcast) {
  637. rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
  638. list_add_tail(&priv->broadcast->list, &remove_list);
  639. priv->broadcast = NULL;
  640. }
  641. spin_unlock_irqrestore(&priv->lock, flags);
  642. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  643. ipoib_mcast_leave(dev, mcast);
  644. ipoib_mcast_free(mcast);
  645. }
  646. }
  647. void ipoib_mcast_restart_task(void *dev_ptr)
  648. {
  649. struct net_device *dev = dev_ptr;
  650. struct ipoib_dev_priv *priv = netdev_priv(dev);
  651. struct dev_mc_list *mclist;
  652. struct ipoib_mcast *mcast, *tmcast;
  653. LIST_HEAD(remove_list);
  654. unsigned long flags;
  655. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  656. ipoib_mcast_stop_thread(dev, 0);
  657. spin_lock_irqsave(&dev->xmit_lock, flags);
  658. spin_lock(&priv->lock);
  659. /*
  660. * Unfortunately, the networking core only gives us a list of all of
  661. * the multicast hardware addresses. We need to figure out which ones
  662. * are new and which ones have been removed
  663. */
  664. /* Clear out the found flag */
  665. list_for_each_entry(mcast, &priv->multicast_list, list)
  666. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  667. /* Mark all of the entries that are found or don't exist */
  668. for (mclist = dev->mc_list; mclist; mclist = mclist->next) {
  669. union ib_gid mgid;
  670. memcpy(mgid.raw, mclist->dmi_addr + 4, sizeof mgid);
  671. /* Add in the P_Key */
  672. mgid.raw[4] = (priv->pkey >> 8) & 0xff;
  673. mgid.raw[5] = priv->pkey & 0xff;
  674. mcast = __ipoib_mcast_find(dev, &mgid);
  675. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  676. struct ipoib_mcast *nmcast;
  677. /* Not found or send-only group, let's add a new entry */
  678. ipoib_dbg_mcast(priv, "adding multicast entry for mgid "
  679. IPOIB_GID_FMT "\n", IPOIB_GID_ARG(mgid));
  680. nmcast = ipoib_mcast_alloc(dev, 0);
  681. if (!nmcast) {
  682. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  683. continue;
  684. }
  685. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  686. nmcast->mcmember.mgid = mgid;
  687. if (mcast) {
  688. /* Destroy the send only entry */
  689. list_del(&mcast->list);
  690. list_add_tail(&mcast->list, &remove_list);
  691. rb_replace_node(&mcast->rb_node,
  692. &nmcast->rb_node,
  693. &priv->multicast_tree);
  694. } else
  695. __ipoib_mcast_add(dev, nmcast);
  696. list_add_tail(&nmcast->list, &priv->multicast_list);
  697. }
  698. if (mcast)
  699. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  700. }
  701. /* Remove all of the entries don't exist anymore */
  702. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  703. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  704. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  705. ipoib_dbg_mcast(priv, "deleting multicast group " IPOIB_GID_FMT "\n",
  706. IPOIB_GID_ARG(mcast->mcmember.mgid));
  707. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  708. /* Move to the remove list */
  709. list_del(&mcast->list);
  710. list_add_tail(&mcast->list, &remove_list);
  711. }
  712. }
  713. spin_unlock(&priv->lock);
  714. spin_unlock_irqrestore(&dev->xmit_lock, flags);
  715. /* We have to cancel outside of the spinlock */
  716. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  717. ipoib_mcast_leave(mcast->dev, mcast);
  718. ipoib_mcast_free(mcast);
  719. }
  720. if (test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
  721. ipoib_mcast_start_thread(dev);
  722. }
  723. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  724. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  725. {
  726. struct ipoib_mcast_iter *iter;
  727. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  728. if (!iter)
  729. return NULL;
  730. iter->dev = dev;
  731. memset(iter->mgid.raw, 0, 16);
  732. if (ipoib_mcast_iter_next(iter)) {
  733. kfree(iter);
  734. return NULL;
  735. }
  736. return iter;
  737. }
  738. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  739. {
  740. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  741. struct rb_node *n;
  742. struct ipoib_mcast *mcast;
  743. int ret = 1;
  744. spin_lock_irq(&priv->lock);
  745. n = rb_first(&priv->multicast_tree);
  746. while (n) {
  747. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  748. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  749. sizeof (union ib_gid)) < 0) {
  750. iter->mgid = mcast->mcmember.mgid;
  751. iter->created = mcast->created;
  752. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  753. iter->complete = !!mcast->ah;
  754. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  755. ret = 0;
  756. break;
  757. }
  758. n = rb_next(n);
  759. }
  760. spin_unlock_irq(&priv->lock);
  761. return ret;
  762. }
  763. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  764. union ib_gid *mgid,
  765. unsigned long *created,
  766. unsigned int *queuelen,
  767. unsigned int *complete,
  768. unsigned int *send_only)
  769. {
  770. *mgid = iter->mgid;
  771. *created = iter->created;
  772. *queuelen = iter->queuelen;
  773. *complete = iter->complete;
  774. *send_only = iter->send_only;
  775. }
  776. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */