ipoib_multicast.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  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. #include <linux/skbuff.h>
  35. #include <linux/rtnetlink.h>
  36. #include <linux/moduleparam.h>
  37. #include <linux/ip.h>
  38. #include <linux/in.h>
  39. #include <linux/igmp.h>
  40. #include <linux/inetdevice.h>
  41. #include <linux/delay.h>
  42. #include <linux/completion.h>
  43. #include <linux/slab.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. struct ipoib_mcast_iter {
  53. struct net_device *dev;
  54. union ib_gid mgid;
  55. unsigned long created;
  56. unsigned int queuelen;
  57. unsigned int complete;
  58. unsigned int send_only;
  59. };
  60. /*
  61. * This should be called with the priv->lock held
  62. */
  63. static void __ipoib_mcast_schedule_join_thread(struct ipoib_dev_priv *priv,
  64. struct ipoib_mcast *mcast,
  65. bool delay)
  66. {
  67. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  68. return;
  69. /*
  70. * We will be scheduling *something*, so cancel whatever is
  71. * currently scheduled first
  72. */
  73. cancel_delayed_work(&priv->mcast_task);
  74. if (mcast && delay) {
  75. /*
  76. * We had a failure and want to schedule a retry later
  77. */
  78. mcast->backoff *= 2;
  79. if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
  80. mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
  81. mcast->delay_until = jiffies + (mcast->backoff * HZ);
  82. /*
  83. * Mark this mcast for its delay, but restart the
  84. * task immediately. The join task will make sure to
  85. * clear out all entries without delays, and then
  86. * schedule itself to run again when the earliest
  87. * delay expires
  88. */
  89. queue_delayed_work(priv->wq, &priv->mcast_task, 0);
  90. } else if (delay) {
  91. /*
  92. * Special case of retrying after a failure to
  93. * allocate the broadcast multicast group, wait
  94. * 1 second and try again
  95. */
  96. queue_delayed_work(priv->wq, &priv->mcast_task, HZ);
  97. } else
  98. queue_delayed_work(priv->wq, &priv->mcast_task, 0);
  99. }
  100. static void ipoib_mcast_free(struct ipoib_mcast *mcast)
  101. {
  102. struct net_device *dev = mcast->dev;
  103. int tx_dropped = 0;
  104. ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
  105. mcast->mcmember.mgid.raw);
  106. /* remove all neigh connected to this mcast */
  107. ipoib_del_neighs_by_gid(dev, mcast->mcmember.mgid.raw);
  108. if (mcast->ah)
  109. ipoib_put_ah(mcast->ah);
  110. while (!skb_queue_empty(&mcast->pkt_queue)) {
  111. ++tx_dropped;
  112. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  113. }
  114. netif_tx_lock_bh(dev);
  115. dev->stats.tx_dropped += tx_dropped;
  116. netif_tx_unlock_bh(dev);
  117. kfree(mcast);
  118. }
  119. static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
  120. int can_sleep)
  121. {
  122. struct ipoib_mcast *mcast;
  123. mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
  124. if (!mcast)
  125. return NULL;
  126. mcast->dev = dev;
  127. mcast->created = jiffies;
  128. mcast->delay_until = jiffies;
  129. mcast->backoff = 1;
  130. INIT_LIST_HEAD(&mcast->list);
  131. INIT_LIST_HEAD(&mcast->neigh_list);
  132. skb_queue_head_init(&mcast->pkt_queue);
  133. return mcast;
  134. }
  135. static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
  136. {
  137. struct ipoib_dev_priv *priv = netdev_priv(dev);
  138. struct rb_node *n = priv->multicast_tree.rb_node;
  139. while (n) {
  140. struct ipoib_mcast *mcast;
  141. int ret;
  142. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  143. ret = memcmp(mgid, mcast->mcmember.mgid.raw,
  144. sizeof (union ib_gid));
  145. if (ret < 0)
  146. n = n->rb_left;
  147. else if (ret > 0)
  148. n = n->rb_right;
  149. else
  150. return mcast;
  151. }
  152. return NULL;
  153. }
  154. static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
  155. {
  156. struct ipoib_dev_priv *priv = netdev_priv(dev);
  157. struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;
  158. while (*n) {
  159. struct ipoib_mcast *tmcast;
  160. int ret;
  161. pn = *n;
  162. tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);
  163. ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
  164. sizeof (union ib_gid));
  165. if (ret < 0)
  166. n = &pn->rb_left;
  167. else if (ret > 0)
  168. n = &pn->rb_right;
  169. else
  170. return -EEXIST;
  171. }
  172. rb_link_node(&mcast->rb_node, pn, n);
  173. rb_insert_color(&mcast->rb_node, &priv->multicast_tree);
  174. return 0;
  175. }
  176. static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
  177. struct ib_sa_mcmember_rec *mcmember)
  178. {
  179. struct net_device *dev = mcast->dev;
  180. struct ipoib_dev_priv *priv = netdev_priv(dev);
  181. struct ipoib_ah *ah;
  182. int ret;
  183. int set_qkey = 0;
  184. mcast->mcmember = *mcmember;
  185. /* Set the multicast MTU and cached Q_Key before we attach if it's
  186. * the broadcast group.
  187. */
  188. if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  189. sizeof (union ib_gid))) {
  190. spin_lock_irq(&priv->lock);
  191. if (!priv->broadcast) {
  192. spin_unlock_irq(&priv->lock);
  193. return -EAGAIN;
  194. }
  195. /*update priv member according to the new mcast*/
  196. priv->broadcast->mcmember.qkey = mcmember->qkey;
  197. priv->broadcast->mcmember.mtu = mcmember->mtu;
  198. priv->broadcast->mcmember.traffic_class = mcmember->traffic_class;
  199. priv->broadcast->mcmember.rate = mcmember->rate;
  200. priv->broadcast->mcmember.sl = mcmember->sl;
  201. priv->broadcast->mcmember.flow_label = mcmember->flow_label;
  202. priv->broadcast->mcmember.hop_limit = mcmember->hop_limit;
  203. /* assume if the admin and the mcast are the same both can be changed */
  204. if (priv->mcast_mtu == priv->admin_mtu)
  205. priv->admin_mtu =
  206. priv->mcast_mtu =
  207. IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  208. else
  209. priv->mcast_mtu =
  210. IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  211. priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
  212. spin_unlock_irq(&priv->lock);
  213. priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
  214. set_qkey = 1;
  215. }
  216. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  217. if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  218. ipoib_warn(priv, "multicast group %pI6 already attached\n",
  219. mcast->mcmember.mgid.raw);
  220. return 0;
  221. }
  222. ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
  223. &mcast->mcmember.mgid, set_qkey);
  224. if (ret < 0) {
  225. ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
  226. mcast->mcmember.mgid.raw);
  227. clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
  228. return ret;
  229. }
  230. }
  231. {
  232. struct ib_ah_attr av = {
  233. .dlid = be16_to_cpu(mcast->mcmember.mlid),
  234. .port_num = priv->port,
  235. .sl = mcast->mcmember.sl,
  236. .ah_flags = IB_AH_GRH,
  237. .static_rate = mcast->mcmember.rate,
  238. .grh = {
  239. .flow_label = be32_to_cpu(mcast->mcmember.flow_label),
  240. .hop_limit = mcast->mcmember.hop_limit,
  241. .sgid_index = 0,
  242. .traffic_class = mcast->mcmember.traffic_class
  243. }
  244. };
  245. av.grh.dgid = mcast->mcmember.mgid;
  246. ah = ipoib_create_ah(dev, priv->pd, &av);
  247. if (IS_ERR(ah)) {
  248. ipoib_warn(priv, "ib_address_create failed %ld\n",
  249. -PTR_ERR(ah));
  250. /* use original error */
  251. return PTR_ERR(ah);
  252. } else {
  253. spin_lock_irq(&priv->lock);
  254. mcast->ah = ah;
  255. spin_unlock_irq(&priv->lock);
  256. ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
  257. mcast->mcmember.mgid.raw,
  258. mcast->ah->ah,
  259. be16_to_cpu(mcast->mcmember.mlid),
  260. mcast->mcmember.sl);
  261. }
  262. }
  263. /* actually send any queued packets */
  264. netif_tx_lock_bh(dev);
  265. while (!skb_queue_empty(&mcast->pkt_queue)) {
  266. struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
  267. netif_tx_unlock_bh(dev);
  268. skb->dev = dev;
  269. if (dev_queue_xmit(skb))
  270. ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
  271. netif_tx_lock_bh(dev);
  272. }
  273. netif_tx_unlock_bh(dev);
  274. return 0;
  275. }
  276. void ipoib_mcast_carrier_on_task(struct work_struct *work)
  277. {
  278. struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
  279. carrier_on_task);
  280. struct ib_port_attr attr;
  281. if (ib_query_port(priv->ca, priv->port, &attr) ||
  282. attr.state != IB_PORT_ACTIVE) {
  283. ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
  284. return;
  285. }
  286. /*
  287. * Take rtnl_lock to avoid racing with ipoib_stop() and
  288. * turning the carrier back on while a device is being
  289. * removed. However, ipoib_stop() will attempt to flush
  290. * the workqueue while holding the rtnl lock, so loop
  291. * on trylock until either we get the lock or we see
  292. * FLAG_OPER_UP go away as that signals that we are bailing
  293. * and can safely ignore the carrier on work.
  294. */
  295. while (!rtnl_trylock()) {
  296. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  297. return;
  298. else
  299. msleep(20);
  300. }
  301. if (!ipoib_cm_admin_enabled(priv->dev))
  302. dev_set_mtu(priv->dev, min(priv->mcast_mtu, priv->admin_mtu));
  303. netif_carrier_on(priv->dev);
  304. rtnl_unlock();
  305. }
  306. static int ipoib_mcast_join_complete(int status,
  307. struct ib_sa_multicast *multicast)
  308. {
  309. struct ipoib_mcast *mcast = multicast->context;
  310. struct net_device *dev = mcast->dev;
  311. struct ipoib_dev_priv *priv = netdev_priv(dev);
  312. ipoib_dbg_mcast(priv, "%sjoin completion for %pI6 (status %d)\n",
  313. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ?
  314. "sendonly " : "",
  315. mcast->mcmember.mgid.raw, status);
  316. /* We trap for port events ourselves. */
  317. if (status == -ENETRESET) {
  318. status = 0;
  319. goto out;
  320. }
  321. if (!status)
  322. status = ipoib_mcast_join_finish(mcast, &multicast->rec);
  323. if (!status) {
  324. mcast->backoff = 1;
  325. mcast->delay_until = jiffies;
  326. /*
  327. * Defer carrier on work to priv->wq to avoid a
  328. * deadlock on rtnl_lock here. Requeue our multicast
  329. * work too, which will end up happening right after
  330. * our carrier on task work and will allow us to
  331. * send out all of the non-broadcast joins
  332. */
  333. if (mcast == priv->broadcast) {
  334. spin_lock_irq(&priv->lock);
  335. queue_work(priv->wq, &priv->carrier_on_task);
  336. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  337. goto out_locked;
  338. }
  339. } else {
  340. if (mcast->logcount++ < 20) {
  341. if (status == -ETIMEDOUT || status == -EAGAIN) {
  342. ipoib_dbg_mcast(priv, "%smulticast join failed for %pI6, status %d\n",
  343. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
  344. mcast->mcmember.mgid.raw, status);
  345. } else {
  346. ipoib_warn(priv, "%smulticast join failed for %pI6, status %d\n",
  347. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
  348. mcast->mcmember.mgid.raw, status);
  349. }
  350. }
  351. if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
  352. mcast->backoff >= 2) {
  353. /*
  354. * We only retry sendonly joins once before we drop
  355. * the packet and quit trying to deal with the
  356. * group. However, we leave the group in the
  357. * mcast list as an unjoined group. If we want to
  358. * try joining again, we simply queue up a packet
  359. * and restart the join thread. The empty queue
  360. * is why the join thread ignores this group.
  361. */
  362. mcast->backoff = 1;
  363. netif_tx_lock_bh(dev);
  364. while (!skb_queue_empty(&mcast->pkt_queue)) {
  365. ++dev->stats.tx_dropped;
  366. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  367. }
  368. netif_tx_unlock_bh(dev);
  369. } else {
  370. spin_lock_irq(&priv->lock);
  371. /* Requeue this join task with a backoff delay */
  372. __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
  373. goto out_locked;
  374. }
  375. }
  376. out:
  377. spin_lock_irq(&priv->lock);
  378. out_locked:
  379. /*
  380. * Make sure to set mcast->mc before we clear the busy flag to avoid
  381. * racing with code that checks for BUSY before checking mcast->mc
  382. */
  383. if (status)
  384. mcast->mc = NULL;
  385. else
  386. mcast->mc = multicast;
  387. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  388. spin_unlock_irq(&priv->lock);
  389. complete(&mcast->done);
  390. return status;
  391. }
  392. static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast,
  393. int create)
  394. {
  395. struct ipoib_dev_priv *priv = netdev_priv(dev);
  396. struct ib_sa_multicast *multicast;
  397. struct ib_sa_mcmember_rec rec = {
  398. .join_state = 1
  399. };
  400. ib_sa_comp_mask comp_mask;
  401. int ret = 0;
  402. ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
  403. rec.mgid = mcast->mcmember.mgid;
  404. rec.port_gid = priv->local_gid;
  405. rec.pkey = cpu_to_be16(priv->pkey);
  406. comp_mask =
  407. IB_SA_MCMEMBER_REC_MGID |
  408. IB_SA_MCMEMBER_REC_PORT_GID |
  409. IB_SA_MCMEMBER_REC_PKEY |
  410. IB_SA_MCMEMBER_REC_JOIN_STATE;
  411. if (create) {
  412. comp_mask |=
  413. IB_SA_MCMEMBER_REC_QKEY |
  414. IB_SA_MCMEMBER_REC_MTU_SELECTOR |
  415. IB_SA_MCMEMBER_REC_MTU |
  416. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
  417. IB_SA_MCMEMBER_REC_RATE_SELECTOR |
  418. IB_SA_MCMEMBER_REC_RATE |
  419. IB_SA_MCMEMBER_REC_SL |
  420. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  421. IB_SA_MCMEMBER_REC_HOP_LIMIT;
  422. rec.qkey = priv->broadcast->mcmember.qkey;
  423. rec.mtu_selector = IB_SA_EQ;
  424. rec.mtu = priv->broadcast->mcmember.mtu;
  425. rec.traffic_class = priv->broadcast->mcmember.traffic_class;
  426. rec.rate_selector = IB_SA_EQ;
  427. rec.rate = priv->broadcast->mcmember.rate;
  428. rec.sl = priv->broadcast->mcmember.sl;
  429. rec.flow_label = priv->broadcast->mcmember.flow_label;
  430. rec.hop_limit = priv->broadcast->mcmember.hop_limit;
  431. }
  432. multicast = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
  433. &rec, comp_mask, GFP_KERNEL,
  434. ipoib_mcast_join_complete, mcast);
  435. if (IS_ERR(multicast)) {
  436. ret = PTR_ERR(multicast);
  437. ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
  438. spin_lock_irq(&priv->lock);
  439. /* Requeue this join task with a backoff delay */
  440. __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
  441. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  442. spin_unlock_irq(&priv->lock);
  443. complete(&mcast->done);
  444. }
  445. }
  446. void ipoib_mcast_join_task(struct work_struct *work)
  447. {
  448. struct ipoib_dev_priv *priv =
  449. container_of(work, struct ipoib_dev_priv, mcast_task.work);
  450. struct net_device *dev = priv->dev;
  451. struct ib_port_attr port_attr;
  452. unsigned long delay_until = 0;
  453. struct ipoib_mcast *mcast = NULL;
  454. int create = 1;
  455. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  456. return;
  457. if (ib_query_port(priv->ca, priv->port, &port_attr) ||
  458. port_attr.state != IB_PORT_ACTIVE) {
  459. ipoib_dbg(priv, "port state is not ACTIVE (state = %d) suspending join task\n",
  460. port_attr.state);
  461. return;
  462. }
  463. priv->local_lid = port_attr.lid;
  464. if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
  465. ipoib_warn(priv, "ib_query_gid() failed\n");
  466. else
  467. memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
  468. spin_lock_irq(&priv->lock);
  469. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  470. goto out;
  471. if (!priv->broadcast) {
  472. struct ipoib_mcast *broadcast;
  473. broadcast = ipoib_mcast_alloc(dev, 0);
  474. if (!broadcast) {
  475. ipoib_warn(priv, "failed to allocate broadcast group\n");
  476. /*
  477. * Restart us after a 1 second delay to retry
  478. * creating our broadcast group and attaching to
  479. * it. Until this succeeds, this ipoib dev is
  480. * completely stalled (multicast wise).
  481. */
  482. __ipoib_mcast_schedule_join_thread(priv, NULL, 1);
  483. goto out;
  484. }
  485. memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  486. sizeof (union ib_gid));
  487. priv->broadcast = broadcast;
  488. __ipoib_mcast_add(dev, priv->broadcast);
  489. }
  490. if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  491. if (IS_ERR_OR_NULL(priv->broadcast->mc) &&
  492. !test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags)) {
  493. mcast = priv->broadcast;
  494. create = 0;
  495. if (mcast->backoff > 1 &&
  496. time_before(jiffies, mcast->delay_until)) {
  497. delay_until = mcast->delay_until;
  498. mcast = NULL;
  499. }
  500. }
  501. goto out;
  502. }
  503. /*
  504. * We'll never get here until the broadcast group is both allocated
  505. * and attached
  506. */
  507. list_for_each_entry(mcast, &priv->multicast_list, list) {
  508. if (IS_ERR_OR_NULL(mcast->mc) &&
  509. !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags) &&
  510. (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ||
  511. !skb_queue_empty(&mcast->pkt_queue))) {
  512. if (mcast->backoff == 1 ||
  513. time_after_eq(jiffies, mcast->delay_until)) {
  514. /* Found the next unjoined group */
  515. init_completion(&mcast->done);
  516. set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  517. if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  518. create = 0;
  519. else
  520. create = 1;
  521. spin_unlock_irq(&priv->lock);
  522. ipoib_mcast_join(dev, mcast, create);
  523. spin_lock_irq(&priv->lock);
  524. } else if (!delay_until ||
  525. time_before(mcast->delay_until, delay_until))
  526. delay_until = mcast->delay_until;
  527. }
  528. }
  529. mcast = NULL;
  530. ipoib_dbg_mcast(priv, "successfully started all multicast joins\n");
  531. out:
  532. if (delay_until) {
  533. cancel_delayed_work(&priv->mcast_task);
  534. queue_delayed_work(priv->wq, &priv->mcast_task,
  535. delay_until - jiffies);
  536. }
  537. if (mcast) {
  538. init_completion(&mcast->done);
  539. set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  540. }
  541. spin_unlock_irq(&priv->lock);
  542. if (mcast)
  543. ipoib_mcast_join(dev, mcast, create);
  544. }
  545. int ipoib_mcast_start_thread(struct net_device *dev)
  546. {
  547. struct ipoib_dev_priv *priv = netdev_priv(dev);
  548. unsigned long flags;
  549. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  550. spin_lock_irqsave(&priv->lock, flags);
  551. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  552. spin_unlock_irqrestore(&priv->lock, flags);
  553. return 0;
  554. }
  555. int ipoib_mcast_stop_thread(struct net_device *dev)
  556. {
  557. struct ipoib_dev_priv *priv = netdev_priv(dev);
  558. unsigned long flags;
  559. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  560. spin_lock_irqsave(&priv->lock, flags);
  561. cancel_delayed_work(&priv->mcast_task);
  562. spin_unlock_irqrestore(&priv->lock, flags);
  563. flush_workqueue(priv->wq);
  564. return 0;
  565. }
  566. static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  567. {
  568. struct ipoib_dev_priv *priv = netdev_priv(dev);
  569. int ret = 0;
  570. if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  571. ipoib_warn(priv, "ipoib_mcast_leave on an in-flight join\n");
  572. if (!IS_ERR_OR_NULL(mcast->mc))
  573. ib_sa_free_multicast(mcast->mc);
  574. if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  575. ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
  576. mcast->mcmember.mgid.raw);
  577. /* Remove ourselves from the multicast group */
  578. ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
  579. be16_to_cpu(mcast->mcmember.mlid));
  580. if (ret)
  581. ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
  582. } else if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  583. ipoib_dbg(priv, "leaving with no mcmember but not a "
  584. "SENDONLY join\n");
  585. return 0;
  586. }
  587. void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
  588. {
  589. struct ipoib_dev_priv *priv = netdev_priv(dev);
  590. struct ipoib_mcast *mcast;
  591. unsigned long flags;
  592. void *mgid = daddr + 4;
  593. spin_lock_irqsave(&priv->lock, flags);
  594. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
  595. !priv->broadcast ||
  596. !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  597. ++dev->stats.tx_dropped;
  598. dev_kfree_skb_any(skb);
  599. goto unlock;
  600. }
  601. mcast = __ipoib_mcast_find(dev, mgid);
  602. if (!mcast || !mcast->ah) {
  603. if (!mcast) {
  604. /* Let's create a new send only group now */
  605. ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
  606. mgid);
  607. mcast = ipoib_mcast_alloc(dev, 0);
  608. if (!mcast) {
  609. ipoib_warn(priv, "unable to allocate memory "
  610. "for multicast structure\n");
  611. ++dev->stats.tx_dropped;
  612. dev_kfree_skb_any(skb);
  613. goto unlock;
  614. }
  615. set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
  616. memcpy(mcast->mcmember.mgid.raw, mgid,
  617. sizeof (union ib_gid));
  618. __ipoib_mcast_add(dev, mcast);
  619. list_add_tail(&mcast->list, &priv->multicast_list);
  620. }
  621. if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
  622. skb_queue_tail(&mcast->pkt_queue, skb);
  623. else {
  624. ++dev->stats.tx_dropped;
  625. dev_kfree_skb_any(skb);
  626. }
  627. if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
  628. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  629. }
  630. } else {
  631. struct ipoib_neigh *neigh;
  632. spin_unlock_irqrestore(&priv->lock, flags);
  633. neigh = ipoib_neigh_get(dev, daddr);
  634. spin_lock_irqsave(&priv->lock, flags);
  635. if (!neigh) {
  636. neigh = ipoib_neigh_alloc(daddr, dev);
  637. if (neigh) {
  638. kref_get(&mcast->ah->ref);
  639. neigh->ah = mcast->ah;
  640. list_add_tail(&neigh->list, &mcast->neigh_list);
  641. }
  642. }
  643. spin_unlock_irqrestore(&priv->lock, flags);
  644. ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
  645. if (neigh)
  646. ipoib_neigh_put(neigh);
  647. return;
  648. }
  649. unlock:
  650. spin_unlock_irqrestore(&priv->lock, flags);
  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. /*
  672. * make sure the in-flight joins have finished before we attempt
  673. * to leave
  674. */
  675. list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
  676. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  677. wait_for_completion(&mcast->done);
  678. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  679. ipoib_mcast_leave(dev, mcast);
  680. ipoib_mcast_free(mcast);
  681. }
  682. }
  683. static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
  684. {
  685. /* reserved QPN, prefix, scope */
  686. if (memcmp(addr, broadcast, 6))
  687. return 0;
  688. /* signature lower, pkey */
  689. if (memcmp(addr + 7, broadcast + 7, 3))
  690. return 0;
  691. return 1;
  692. }
  693. void ipoib_mcast_restart_task(struct work_struct *work)
  694. {
  695. struct ipoib_dev_priv *priv =
  696. container_of(work, struct ipoib_dev_priv, restart_task);
  697. struct net_device *dev = priv->dev;
  698. struct netdev_hw_addr *ha;
  699. struct ipoib_mcast *mcast, *tmcast;
  700. LIST_HEAD(remove_list);
  701. unsigned long flags;
  702. struct ib_sa_mcmember_rec rec;
  703. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  704. /*
  705. * shortcut...on shutdown flush is called next, just
  706. * let it do all the work
  707. */
  708. return;
  709. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  710. local_irq_save(flags);
  711. netif_addr_lock(dev);
  712. spin_lock(&priv->lock);
  713. /*
  714. * Unfortunately, the networking core only gives us a list of all of
  715. * the multicast hardware addresses. We need to figure out which ones
  716. * are new and which ones have been removed
  717. */
  718. /* Clear out the found flag */
  719. list_for_each_entry(mcast, &priv->multicast_list, list)
  720. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  721. /* Mark all of the entries that are found or don't exist */
  722. netdev_for_each_mc_addr(ha, dev) {
  723. union ib_gid mgid;
  724. if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
  725. continue;
  726. memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
  727. mcast = __ipoib_mcast_find(dev, &mgid);
  728. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  729. struct ipoib_mcast *nmcast;
  730. /* ignore group which is directly joined by userspace */
  731. if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
  732. !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
  733. ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
  734. mgid.raw);
  735. continue;
  736. }
  737. /* Not found or send-only group, let's add a new entry */
  738. ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
  739. mgid.raw);
  740. nmcast = ipoib_mcast_alloc(dev, 0);
  741. if (!nmcast) {
  742. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  743. continue;
  744. }
  745. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  746. nmcast->mcmember.mgid = mgid;
  747. if (mcast) {
  748. /* Destroy the send only entry */
  749. list_move_tail(&mcast->list, &remove_list);
  750. rb_replace_node(&mcast->rb_node,
  751. &nmcast->rb_node,
  752. &priv->multicast_tree);
  753. } else
  754. __ipoib_mcast_add(dev, nmcast);
  755. list_add_tail(&nmcast->list, &priv->multicast_list);
  756. }
  757. if (mcast)
  758. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  759. }
  760. /* Remove all of the entries don't exist anymore */
  761. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  762. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  763. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  764. ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
  765. mcast->mcmember.mgid.raw);
  766. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  767. /* Move to the remove list */
  768. list_move_tail(&mcast->list, &remove_list);
  769. }
  770. }
  771. spin_unlock(&priv->lock);
  772. netif_addr_unlock(dev);
  773. local_irq_restore(flags);
  774. /*
  775. * make sure the in-flight joins have finished before we attempt
  776. * to leave
  777. */
  778. list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
  779. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  780. wait_for_completion(&mcast->done);
  781. list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
  782. ipoib_mcast_leave(mcast->dev, mcast);
  783. ipoib_mcast_free(mcast);
  784. }
  785. /*
  786. * Double check that we are still up
  787. */
  788. if (test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  789. spin_lock_irqsave(&priv->lock, flags);
  790. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  791. spin_unlock_irqrestore(&priv->lock, flags);
  792. }
  793. }
  794. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  795. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  796. {
  797. struct ipoib_mcast_iter *iter;
  798. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  799. if (!iter)
  800. return NULL;
  801. iter->dev = dev;
  802. memset(iter->mgid.raw, 0, 16);
  803. if (ipoib_mcast_iter_next(iter)) {
  804. kfree(iter);
  805. return NULL;
  806. }
  807. return iter;
  808. }
  809. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  810. {
  811. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  812. struct rb_node *n;
  813. struct ipoib_mcast *mcast;
  814. int ret = 1;
  815. spin_lock_irq(&priv->lock);
  816. n = rb_first(&priv->multicast_tree);
  817. while (n) {
  818. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  819. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  820. sizeof (union ib_gid)) < 0) {
  821. iter->mgid = mcast->mcmember.mgid;
  822. iter->created = mcast->created;
  823. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  824. iter->complete = !!mcast->ah;
  825. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  826. ret = 0;
  827. break;
  828. }
  829. n = rb_next(n);
  830. }
  831. spin_unlock_irq(&priv->lock);
  832. return ret;
  833. }
  834. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  835. union ib_gid *mgid,
  836. unsigned long *created,
  837. unsigned int *queuelen,
  838. unsigned int *complete,
  839. unsigned int *send_only)
  840. {
  841. *mgid = iter->mgid;
  842. *created = iter->created;
  843. *queuelen = iter->queuelen;
  844. *complete = iter->complete;
  845. *send_only = iter->send_only;
  846. }
  847. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */