ipoib_multicast.c 25 KB

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