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