ipoib_multicast.c 25 KB

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