ipoib_multicast.c 30 KB

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