ipoib_multicast.c 25 KB

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