ipoib_multicast.c 27 KB

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