ipoib_multicast.c 26 KB

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