ipoib_multicast.c 26 KB

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