ipoib_multicast.c 25 KB

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