ipoib_multicast.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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. if (dev_queue_xmit(skb))
  272. ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
  273. netif_tx_lock_bh(dev);
  274. }
  275. netif_tx_unlock_bh(dev);
  276. return 0;
  277. }
  278. void ipoib_mcast_carrier_on_task(struct work_struct *work)
  279. {
  280. struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
  281. carrier_on_task);
  282. struct ib_port_attr attr;
  283. int ret;
  284. if (ib_query_port(priv->ca, priv->port, &attr) ||
  285. attr.state != IB_PORT_ACTIVE) {
  286. ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
  287. return;
  288. }
  289. /*
  290. * Check if can send sendonly MCG's with sendonly-fullmember join state.
  291. * It done here after the successfully join to the broadcast group,
  292. * because the broadcast group must always be joined first and is always
  293. * re-joined if the SM changes substantially.
  294. */
  295. ret = ipoib_check_sm_sendonly_fullmember_support(priv);
  296. if (ret < 0)
  297. pr_debug("%s failed query sm support for sendonly-fullmember (ret: %d)\n",
  298. priv->dev->name, ret);
  299. /*
  300. * Take rtnl_lock to avoid racing with ipoib_stop() and
  301. * turning the carrier back on while a device is being
  302. * removed. However, ipoib_stop() will attempt to flush
  303. * the workqueue while holding the rtnl lock, so loop
  304. * on trylock until either we get the lock or we see
  305. * FLAG_OPER_UP go away as that signals that we are bailing
  306. * and can safely ignore the carrier on work.
  307. */
  308. while (!rtnl_trylock()) {
  309. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  310. return;
  311. else
  312. msleep(20);
  313. }
  314. if (!ipoib_cm_admin_enabled(priv->dev))
  315. dev_set_mtu(priv->dev, min(priv->mcast_mtu, priv->admin_mtu));
  316. netif_carrier_on(priv->dev);
  317. rtnl_unlock();
  318. }
  319. static int ipoib_mcast_join_complete(int status,
  320. struct ib_sa_multicast *multicast)
  321. {
  322. struct ipoib_mcast *mcast = multicast->context;
  323. struct net_device *dev = mcast->dev;
  324. struct ipoib_dev_priv *priv = netdev_priv(dev);
  325. ipoib_dbg_mcast(priv, "%sjoin completion for %pI6 (status %d)\n",
  326. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ?
  327. "sendonly " : "",
  328. mcast->mcmember.mgid.raw, status);
  329. /* We trap for port events ourselves. */
  330. if (status == -ENETRESET) {
  331. status = 0;
  332. goto out;
  333. }
  334. if (!status)
  335. status = ipoib_mcast_join_finish(mcast, &multicast->rec);
  336. if (!status) {
  337. mcast->backoff = 1;
  338. mcast->delay_until = jiffies;
  339. /*
  340. * Defer carrier on work to priv->wq to avoid a
  341. * deadlock on rtnl_lock here. Requeue our multicast
  342. * work too, which will end up happening right after
  343. * our carrier on task work and will allow us to
  344. * send out all of the non-broadcast joins
  345. */
  346. if (mcast == priv->broadcast) {
  347. spin_lock_irq(&priv->lock);
  348. queue_work(priv->wq, &priv->carrier_on_task);
  349. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  350. goto out_locked;
  351. }
  352. } else {
  353. bool silent_fail =
  354. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
  355. status == -EINVAL;
  356. if (mcast->logcount < 20) {
  357. if (status == -ETIMEDOUT || status == -EAGAIN ||
  358. silent_fail) {
  359. ipoib_dbg_mcast(priv, "%smulticast join failed for %pI6, status %d\n",
  360. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
  361. mcast->mcmember.mgid.raw, status);
  362. } else {
  363. ipoib_warn(priv, "%smulticast join failed for %pI6, status %d\n",
  364. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
  365. mcast->mcmember.mgid.raw, status);
  366. }
  367. if (!silent_fail)
  368. mcast->logcount++;
  369. }
  370. if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
  371. mcast->backoff >= 2) {
  372. /*
  373. * We only retry sendonly joins once before we drop
  374. * the packet and quit trying to deal with the
  375. * group. However, we leave the group in the
  376. * mcast list as an unjoined group. If we want to
  377. * try joining again, we simply queue up a packet
  378. * and restart the join thread. The empty queue
  379. * is why the join thread ignores this group.
  380. */
  381. mcast->backoff = 1;
  382. netif_tx_lock_bh(dev);
  383. while (!skb_queue_empty(&mcast->pkt_queue)) {
  384. ++dev->stats.tx_dropped;
  385. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  386. }
  387. netif_tx_unlock_bh(dev);
  388. } else {
  389. spin_lock_irq(&priv->lock);
  390. /* Requeue this join task with a backoff delay */
  391. __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
  392. goto out_locked;
  393. }
  394. }
  395. out:
  396. spin_lock_irq(&priv->lock);
  397. out_locked:
  398. /*
  399. * Make sure to set mcast->mc before we clear the busy flag to avoid
  400. * racing with code that checks for BUSY before checking mcast->mc
  401. */
  402. if (status)
  403. mcast->mc = NULL;
  404. else
  405. mcast->mc = multicast;
  406. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  407. spin_unlock_irq(&priv->lock);
  408. complete(&mcast->done);
  409. return status;
  410. }
  411. /*
  412. * Caller must hold 'priv->lock'
  413. */
  414. static int ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast)
  415. {
  416. struct ipoib_dev_priv *priv = netdev_priv(dev);
  417. struct ib_sa_multicast *multicast;
  418. struct ib_sa_mcmember_rec rec = {
  419. .join_state = 1
  420. };
  421. ib_sa_comp_mask comp_mask;
  422. int ret = 0;
  423. if (!priv->broadcast ||
  424. !test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  425. return -EINVAL;
  426. init_completion(&mcast->done);
  427. set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  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. if (ipoib_mcast_join(dev, mcast)) {
  570. spin_unlock_irq(&priv->lock);
  571. return;
  572. }
  573. } else if (!delay_until ||
  574. time_before(mcast->delay_until, delay_until))
  575. delay_until = mcast->delay_until;
  576. }
  577. }
  578. mcast = NULL;
  579. ipoib_dbg_mcast(priv, "successfully started all multicast joins\n");
  580. out:
  581. if (delay_until) {
  582. cancel_delayed_work(&priv->mcast_task);
  583. queue_delayed_work(priv->wq, &priv->mcast_task,
  584. delay_until - jiffies);
  585. }
  586. if (mcast)
  587. ipoib_mcast_join(dev, mcast);
  588. spin_unlock_irq(&priv->lock);
  589. }
  590. int ipoib_mcast_start_thread(struct net_device *dev)
  591. {
  592. struct ipoib_dev_priv *priv = netdev_priv(dev);
  593. unsigned long flags;
  594. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  595. spin_lock_irqsave(&priv->lock, flags);
  596. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  597. spin_unlock_irqrestore(&priv->lock, flags);
  598. return 0;
  599. }
  600. int ipoib_mcast_stop_thread(struct net_device *dev)
  601. {
  602. struct ipoib_dev_priv *priv = netdev_priv(dev);
  603. unsigned long flags;
  604. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  605. spin_lock_irqsave(&priv->lock, flags);
  606. cancel_delayed_work(&priv->mcast_task);
  607. spin_unlock_irqrestore(&priv->lock, flags);
  608. flush_workqueue(priv->wq);
  609. return 0;
  610. }
  611. static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  612. {
  613. struct ipoib_dev_priv *priv = netdev_priv(dev);
  614. int ret = 0;
  615. if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  616. ipoib_warn(priv, "ipoib_mcast_leave on an in-flight join\n");
  617. if (!IS_ERR_OR_NULL(mcast->mc))
  618. ib_sa_free_multicast(mcast->mc);
  619. if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  620. ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
  621. mcast->mcmember.mgid.raw);
  622. /* Remove ourselves from the multicast group */
  623. ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
  624. be16_to_cpu(mcast->mcmember.mlid));
  625. if (ret)
  626. ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
  627. } else if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  628. ipoib_dbg(priv, "leaving with no mcmember but not a "
  629. "SENDONLY join\n");
  630. return 0;
  631. }
  632. /*
  633. * Check if the multicast group is sendonly. If so remove it from the maps
  634. * and add to the remove list
  635. */
  636. void ipoib_check_and_add_mcast_sendonly(struct ipoib_dev_priv *priv, u8 *mgid,
  637. struct list_head *remove_list)
  638. {
  639. /* Is this multicast ? */
  640. if (*mgid == 0xff) {
  641. struct ipoib_mcast *mcast = __ipoib_mcast_find(priv->dev, mgid);
  642. if (mcast && test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  643. list_del(&mcast->list);
  644. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  645. list_add_tail(&mcast->list, remove_list);
  646. }
  647. }
  648. }
  649. void ipoib_mcast_remove_list(struct list_head *remove_list)
  650. {
  651. struct ipoib_mcast *mcast, *tmcast;
  652. list_for_each_entry_safe(mcast, tmcast, remove_list, list) {
  653. ipoib_mcast_leave(mcast->dev, mcast);
  654. ipoib_mcast_free(mcast);
  655. }
  656. }
  657. void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
  658. {
  659. struct ipoib_dev_priv *priv = netdev_priv(dev);
  660. struct ipoib_mcast *mcast;
  661. unsigned long flags;
  662. void *mgid = daddr + 4;
  663. spin_lock_irqsave(&priv->lock, flags);
  664. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags) ||
  665. !priv->broadcast ||
  666. !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  667. ++dev->stats.tx_dropped;
  668. dev_kfree_skb_any(skb);
  669. goto unlock;
  670. }
  671. mcast = __ipoib_mcast_find(dev, mgid);
  672. if (!mcast || !mcast->ah) {
  673. if (!mcast) {
  674. /* Let's create a new send only group now */
  675. ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
  676. mgid);
  677. mcast = ipoib_mcast_alloc(dev, 0);
  678. if (!mcast) {
  679. ipoib_warn(priv, "unable to allocate memory "
  680. "for multicast structure\n");
  681. ++dev->stats.tx_dropped;
  682. dev_kfree_skb_any(skb);
  683. goto unlock;
  684. }
  685. set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
  686. memcpy(mcast->mcmember.mgid.raw, mgid,
  687. sizeof (union ib_gid));
  688. __ipoib_mcast_add(dev, mcast);
  689. list_add_tail(&mcast->list, &priv->multicast_list);
  690. }
  691. if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE) {
  692. /* put pseudoheader back on for next time */
  693. skb_push(skb, sizeof(struct ipoib_pseudo_header));
  694. skb_queue_tail(&mcast->pkt_queue, skb);
  695. } else {
  696. ++dev->stats.tx_dropped;
  697. dev_kfree_skb_any(skb);
  698. }
  699. if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
  700. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  701. }
  702. } else {
  703. struct ipoib_neigh *neigh;
  704. spin_unlock_irqrestore(&priv->lock, flags);
  705. neigh = ipoib_neigh_get(dev, daddr);
  706. spin_lock_irqsave(&priv->lock, flags);
  707. if (!neigh) {
  708. neigh = ipoib_neigh_alloc(daddr, dev);
  709. /* Make sure that the neigh will be added only
  710. * once to mcast list.
  711. */
  712. if (neigh && list_empty(&neigh->list)) {
  713. kref_get(&mcast->ah->ref);
  714. neigh->ah = mcast->ah;
  715. list_add_tail(&neigh->list, &mcast->neigh_list);
  716. }
  717. }
  718. spin_unlock_irqrestore(&priv->lock, flags);
  719. ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
  720. if (neigh)
  721. ipoib_neigh_put(neigh);
  722. return;
  723. }
  724. unlock:
  725. spin_unlock_irqrestore(&priv->lock, flags);
  726. }
  727. void ipoib_mcast_dev_flush(struct net_device *dev)
  728. {
  729. struct ipoib_dev_priv *priv = netdev_priv(dev);
  730. LIST_HEAD(remove_list);
  731. struct ipoib_mcast *mcast, *tmcast;
  732. unsigned long flags;
  733. ipoib_dbg_mcast(priv, "flushing multicast list\n");
  734. spin_lock_irqsave(&priv->lock, flags);
  735. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  736. list_del(&mcast->list);
  737. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  738. list_add_tail(&mcast->list, &remove_list);
  739. }
  740. if (priv->broadcast) {
  741. rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
  742. list_add_tail(&priv->broadcast->list, &remove_list);
  743. priv->broadcast = NULL;
  744. }
  745. spin_unlock_irqrestore(&priv->lock, flags);
  746. /*
  747. * make sure the in-flight joins have finished before we attempt
  748. * to leave
  749. */
  750. list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
  751. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  752. wait_for_completion(&mcast->done);
  753. ipoib_mcast_remove_list(&remove_list);
  754. }
  755. static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
  756. {
  757. /* reserved QPN, prefix, scope */
  758. if (memcmp(addr, broadcast, 6))
  759. return 0;
  760. /* signature lower, pkey */
  761. if (memcmp(addr + 7, broadcast + 7, 3))
  762. return 0;
  763. return 1;
  764. }
  765. void ipoib_mcast_restart_task(struct work_struct *work)
  766. {
  767. struct ipoib_dev_priv *priv =
  768. container_of(work, struct ipoib_dev_priv, restart_task);
  769. struct net_device *dev = priv->dev;
  770. struct netdev_hw_addr *ha;
  771. struct ipoib_mcast *mcast, *tmcast;
  772. LIST_HEAD(remove_list);
  773. unsigned long flags;
  774. struct ib_sa_mcmember_rec rec;
  775. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  776. /*
  777. * shortcut...on shutdown flush is called next, just
  778. * let it do all the work
  779. */
  780. return;
  781. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  782. local_irq_save(flags);
  783. netif_addr_lock(dev);
  784. spin_lock(&priv->lock);
  785. /*
  786. * Unfortunately, the networking core only gives us a list of all of
  787. * the multicast hardware addresses. We need to figure out which ones
  788. * are new and which ones have been removed
  789. */
  790. /* Clear out the found flag */
  791. list_for_each_entry(mcast, &priv->multicast_list, list)
  792. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  793. /* Mark all of the entries that are found or don't exist */
  794. netdev_for_each_mc_addr(ha, dev) {
  795. union ib_gid mgid;
  796. if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
  797. continue;
  798. memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
  799. mcast = __ipoib_mcast_find(dev, &mgid);
  800. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  801. struct ipoib_mcast *nmcast;
  802. /* ignore group which is directly joined by userspace */
  803. if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
  804. !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
  805. ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
  806. mgid.raw);
  807. continue;
  808. }
  809. /* Not found or send-only group, let's add a new entry */
  810. ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
  811. mgid.raw);
  812. nmcast = ipoib_mcast_alloc(dev, 0);
  813. if (!nmcast) {
  814. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  815. continue;
  816. }
  817. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  818. nmcast->mcmember.mgid = mgid;
  819. if (mcast) {
  820. /* Destroy the send only entry */
  821. list_move_tail(&mcast->list, &remove_list);
  822. rb_replace_node(&mcast->rb_node,
  823. &nmcast->rb_node,
  824. &priv->multicast_tree);
  825. } else
  826. __ipoib_mcast_add(dev, nmcast);
  827. list_add_tail(&nmcast->list, &priv->multicast_list);
  828. }
  829. if (mcast)
  830. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  831. }
  832. /* Remove all of the entries don't exist anymore */
  833. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  834. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  835. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  836. ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
  837. mcast->mcmember.mgid.raw);
  838. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  839. /* Move to the remove list */
  840. list_move_tail(&mcast->list, &remove_list);
  841. }
  842. }
  843. spin_unlock(&priv->lock);
  844. netif_addr_unlock(dev);
  845. local_irq_restore(flags);
  846. /*
  847. * make sure the in-flight joins have finished before we attempt
  848. * to leave
  849. */
  850. list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
  851. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  852. wait_for_completion(&mcast->done);
  853. ipoib_mcast_remove_list(&remove_list);
  854. /*
  855. * Double check that we are still up
  856. */
  857. if (test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  858. spin_lock_irqsave(&priv->lock, flags);
  859. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  860. spin_unlock_irqrestore(&priv->lock, flags);
  861. }
  862. }
  863. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  864. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  865. {
  866. struct ipoib_mcast_iter *iter;
  867. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  868. if (!iter)
  869. return NULL;
  870. iter->dev = dev;
  871. memset(iter->mgid.raw, 0, 16);
  872. if (ipoib_mcast_iter_next(iter)) {
  873. kfree(iter);
  874. return NULL;
  875. }
  876. return iter;
  877. }
  878. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  879. {
  880. struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
  881. struct rb_node *n;
  882. struct ipoib_mcast *mcast;
  883. int ret = 1;
  884. spin_lock_irq(&priv->lock);
  885. n = rb_first(&priv->multicast_tree);
  886. while (n) {
  887. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  888. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  889. sizeof (union ib_gid)) < 0) {
  890. iter->mgid = mcast->mcmember.mgid;
  891. iter->created = mcast->created;
  892. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  893. iter->complete = !!mcast->ah;
  894. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  895. ret = 0;
  896. break;
  897. }
  898. n = rb_next(n);
  899. }
  900. spin_unlock_irq(&priv->lock);
  901. return ret;
  902. }
  903. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  904. union ib_gid *mgid,
  905. unsigned long *created,
  906. unsigned int *queuelen,
  907. unsigned int *complete,
  908. unsigned int *send_only)
  909. {
  910. *mgid = iter->mgid;
  911. *created = iter->created;
  912. *queuelen = iter->queuelen;
  913. *complete = iter->complete;
  914. *send_only = iter->send_only;
  915. }
  916. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */