ipoib_multicast.c 25 KB

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