ipoib_multicast.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067
  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(ipoib_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 = ipoib_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 = ipoib_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 = ipoib_priv(dev);
  183. struct rdma_netdev *rn = netdev_priv(dev);
  184. struct ipoib_ah *ah;
  185. struct rdma_ah_attr av;
  186. int ret;
  187. int set_qkey = 0;
  188. mcast->mcmember = *mcmember;
  189. /* Set the multicast MTU and cached Q_Key before we attach if it's
  190. * the broadcast group.
  191. */
  192. if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  193. sizeof (union ib_gid))) {
  194. spin_lock_irq(&priv->lock);
  195. if (!priv->broadcast) {
  196. spin_unlock_irq(&priv->lock);
  197. return -EAGAIN;
  198. }
  199. /*update priv member according to the new mcast*/
  200. priv->broadcast->mcmember.qkey = mcmember->qkey;
  201. priv->broadcast->mcmember.mtu = mcmember->mtu;
  202. priv->broadcast->mcmember.traffic_class = mcmember->traffic_class;
  203. priv->broadcast->mcmember.rate = mcmember->rate;
  204. priv->broadcast->mcmember.sl = mcmember->sl;
  205. priv->broadcast->mcmember.flow_label = mcmember->flow_label;
  206. priv->broadcast->mcmember.hop_limit = mcmember->hop_limit;
  207. /* assume if the admin and the mcast are the same both can be changed */
  208. if (priv->mcast_mtu == priv->admin_mtu)
  209. priv->admin_mtu =
  210. priv->mcast_mtu =
  211. IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  212. else
  213. priv->mcast_mtu =
  214. IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
  215. priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
  216. spin_unlock_irq(&priv->lock);
  217. priv->tx_wr.remote_qkey = priv->qkey;
  218. set_qkey = 1;
  219. }
  220. if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  221. if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  222. ipoib_warn(priv, "multicast group %pI6 already attached\n",
  223. mcast->mcmember.mgid.raw);
  224. return 0;
  225. }
  226. ret = rn->attach_mcast(dev, priv->ca, &mcast->mcmember.mgid,
  227. be16_to_cpu(mcast->mcmember.mlid),
  228. set_qkey, priv->qkey);
  229. if (ret < 0) {
  230. ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
  231. mcast->mcmember.mgid.raw);
  232. clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
  233. return ret;
  234. }
  235. }
  236. memset(&av, 0, sizeof(av));
  237. av.type = rdma_ah_find_type(priv->ca, priv->port);
  238. rdma_ah_set_dlid(&av, be16_to_cpu(mcast->mcmember.mlid)),
  239. rdma_ah_set_port_num(&av, priv->port);
  240. rdma_ah_set_sl(&av, mcast->mcmember.sl);
  241. rdma_ah_set_static_rate(&av, mcast->mcmember.rate);
  242. rdma_ah_set_grh(&av, &mcast->mcmember.mgid,
  243. be32_to_cpu(mcast->mcmember.flow_label),
  244. 0, mcast->mcmember.hop_limit,
  245. mcast->mcmember.traffic_class);
  246. ah = ipoib_create_ah(dev, priv->pd, &av);
  247. if (IS_ERR(ah)) {
  248. ipoib_warn(priv, "ib_address_create failed %ld\n",
  249. -PTR_ERR(ah));
  250. /* use original error */
  251. return PTR_ERR(ah);
  252. }
  253. spin_lock_irq(&priv->lock);
  254. mcast->ah = ah;
  255. spin_unlock_irq(&priv->lock);
  256. ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
  257. mcast->mcmember.mgid.raw,
  258. mcast->ah->ah,
  259. be16_to_cpu(mcast->mcmember.mlid),
  260. mcast->mcmember.sl);
  261. /* actually send any queued packets */
  262. netif_tx_lock_bh(dev);
  263. while (!skb_queue_empty(&mcast->pkt_queue)) {
  264. struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
  265. netif_tx_unlock_bh(dev);
  266. skb->dev = dev;
  267. ret = dev_queue_xmit(skb);
  268. if (ret)
  269. ipoib_warn(priv, "%s:dev_queue_xmit failed to re-queue packet, ret:%d\n",
  270. __func__, ret);
  271. netif_tx_lock_bh(dev);
  272. }
  273. netif_tx_unlock_bh(dev);
  274. return 0;
  275. }
  276. void ipoib_mcast_carrier_on_task(struct work_struct *work)
  277. {
  278. struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
  279. carrier_on_task);
  280. struct ib_port_attr attr;
  281. if (ib_query_port(priv->ca, priv->port, &attr) ||
  282. attr.state != IB_PORT_ACTIVE) {
  283. ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
  284. return;
  285. }
  286. /*
  287. * Check if can send sendonly MCG's with sendonly-fullmember join state.
  288. * It done here after the successfully join to the broadcast group,
  289. * because the broadcast group must always be joined first and is always
  290. * re-joined if the SM changes substantially.
  291. */
  292. priv->sm_fullmember_sendonly_support =
  293. ib_sa_sendonly_fullmem_support(&ipoib_sa_client,
  294. priv->ca, priv->port);
  295. /*
  296. * Take rtnl_lock to avoid racing with ipoib_stop() and
  297. * turning the carrier back on while a device is being
  298. * removed. However, ipoib_stop() will attempt to flush
  299. * the workqueue while holding the rtnl lock, so loop
  300. * on trylock until either we get the lock or we see
  301. * FLAG_OPER_UP go away as that signals that we are bailing
  302. * and can safely ignore the carrier on work.
  303. */
  304. while (!rtnl_trylock()) {
  305. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  306. return;
  307. else
  308. msleep(20);
  309. }
  310. if (!ipoib_cm_admin_enabled(priv->dev))
  311. dev_set_mtu(priv->dev, min(priv->mcast_mtu, priv->admin_mtu));
  312. netif_carrier_on(priv->dev);
  313. rtnl_unlock();
  314. }
  315. static int ipoib_mcast_join_complete(int status,
  316. struct ib_sa_multicast *multicast)
  317. {
  318. struct ipoib_mcast *mcast = multicast->context;
  319. struct net_device *dev = mcast->dev;
  320. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  321. ipoib_dbg_mcast(priv, "%sjoin completion for %pI6 (status %d)\n",
  322. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ?
  323. "sendonly " : "",
  324. mcast->mcmember.mgid.raw, status);
  325. /* We trap for port events ourselves. */
  326. if (status == -ENETRESET) {
  327. status = 0;
  328. goto out;
  329. }
  330. if (!status)
  331. status = ipoib_mcast_join_finish(mcast, &multicast->rec);
  332. if (!status) {
  333. mcast->backoff = 1;
  334. mcast->delay_until = jiffies;
  335. /*
  336. * Defer carrier on work to priv->wq to avoid a
  337. * deadlock on rtnl_lock here. Requeue our multicast
  338. * work too, which will end up happening right after
  339. * our carrier on task work and will allow us to
  340. * send out all of the non-broadcast joins
  341. */
  342. if (mcast == priv->broadcast) {
  343. spin_lock_irq(&priv->lock);
  344. queue_work(priv->wq, &priv->carrier_on_task);
  345. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  346. goto out_locked;
  347. }
  348. } else {
  349. bool silent_fail =
  350. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
  351. status == -EINVAL;
  352. if (mcast->logcount < 20) {
  353. if (status == -ETIMEDOUT || status == -EAGAIN ||
  354. silent_fail) {
  355. ipoib_dbg_mcast(priv, "%smulticast join failed for %pI6, status %d\n",
  356. test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
  357. mcast->mcmember.mgid.raw, status);
  358. } else {
  359. ipoib_warn(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. }
  363. if (!silent_fail)
  364. mcast->logcount++;
  365. }
  366. if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
  367. mcast->backoff >= 2) {
  368. /*
  369. * We only retry sendonly joins once before we drop
  370. * the packet and quit trying to deal with the
  371. * group. However, we leave the group in the
  372. * mcast list as an unjoined group. If we want to
  373. * try joining again, we simply queue up a packet
  374. * and restart the join thread. The empty queue
  375. * is why the join thread ignores this group.
  376. */
  377. mcast->backoff = 1;
  378. netif_tx_lock_bh(dev);
  379. while (!skb_queue_empty(&mcast->pkt_queue)) {
  380. ++dev->stats.tx_dropped;
  381. dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
  382. }
  383. netif_tx_unlock_bh(dev);
  384. } else {
  385. spin_lock_irq(&priv->lock);
  386. /* Requeue this join task with a backoff delay */
  387. __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
  388. goto out_locked;
  389. }
  390. }
  391. out:
  392. spin_lock_irq(&priv->lock);
  393. out_locked:
  394. /*
  395. * Make sure to set mcast->mc before we clear the busy flag to avoid
  396. * racing with code that checks for BUSY before checking mcast->mc
  397. */
  398. if (status)
  399. mcast->mc = NULL;
  400. else
  401. mcast->mc = multicast;
  402. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  403. spin_unlock_irq(&priv->lock);
  404. complete(&mcast->done);
  405. return status;
  406. }
  407. /*
  408. * Caller must hold 'priv->lock'
  409. */
  410. static int ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast)
  411. {
  412. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  413. struct ib_sa_multicast *multicast;
  414. struct ib_sa_mcmember_rec rec = {
  415. .join_state = 1
  416. };
  417. ib_sa_comp_mask comp_mask;
  418. int ret = 0;
  419. if (!priv->broadcast ||
  420. !test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  421. return -EINVAL;
  422. init_completion(&mcast->done);
  423. set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  424. ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
  425. rec.mgid = mcast->mcmember.mgid;
  426. rec.port_gid = priv->local_gid;
  427. rec.pkey = cpu_to_be16(priv->pkey);
  428. comp_mask =
  429. IB_SA_MCMEMBER_REC_MGID |
  430. IB_SA_MCMEMBER_REC_PORT_GID |
  431. IB_SA_MCMEMBER_REC_PKEY |
  432. IB_SA_MCMEMBER_REC_JOIN_STATE;
  433. if (mcast != priv->broadcast) {
  434. /*
  435. * RFC 4391:
  436. * The MGID MUST use the same P_Key, Q_Key, SL, MTU,
  437. * and HopLimit as those used in the broadcast-GID. The rest
  438. * of attributes SHOULD follow the values used in the
  439. * broadcast-GID as well.
  440. */
  441. comp_mask |=
  442. IB_SA_MCMEMBER_REC_QKEY |
  443. IB_SA_MCMEMBER_REC_MTU_SELECTOR |
  444. IB_SA_MCMEMBER_REC_MTU |
  445. IB_SA_MCMEMBER_REC_TRAFFIC_CLASS |
  446. IB_SA_MCMEMBER_REC_RATE_SELECTOR |
  447. IB_SA_MCMEMBER_REC_RATE |
  448. IB_SA_MCMEMBER_REC_SL |
  449. IB_SA_MCMEMBER_REC_FLOW_LABEL |
  450. IB_SA_MCMEMBER_REC_HOP_LIMIT;
  451. rec.qkey = priv->broadcast->mcmember.qkey;
  452. rec.mtu_selector = IB_SA_EQ;
  453. rec.mtu = priv->broadcast->mcmember.mtu;
  454. rec.traffic_class = priv->broadcast->mcmember.traffic_class;
  455. rec.rate_selector = IB_SA_EQ;
  456. rec.rate = priv->broadcast->mcmember.rate;
  457. rec.sl = priv->broadcast->mcmember.sl;
  458. rec.flow_label = priv->broadcast->mcmember.flow_label;
  459. rec.hop_limit = priv->broadcast->mcmember.hop_limit;
  460. /*
  461. * Send-only IB Multicast joins work at the core IB layer but
  462. * require specific SM support.
  463. * We can use such joins here only if the current SM supports that feature.
  464. * However, if not, we emulate an Ethernet multicast send,
  465. * which does not require a multicast subscription and will
  466. * still send properly. The most appropriate thing to
  467. * do is to create the group if it doesn't exist as that
  468. * most closely emulates the behavior, from a user space
  469. * application perspective, of Ethernet multicast operation.
  470. */
  471. if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
  472. priv->sm_fullmember_sendonly_support)
  473. /* SM supports sendonly-fullmember, otherwise fallback to full-member */
  474. rec.join_state = SENDONLY_FULLMEMBER_JOIN;
  475. }
  476. spin_unlock_irq(&priv->lock);
  477. multicast = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
  478. &rec, comp_mask, GFP_KERNEL,
  479. ipoib_mcast_join_complete, mcast);
  480. spin_lock_irq(&priv->lock);
  481. if (IS_ERR(multicast)) {
  482. ret = PTR_ERR(multicast);
  483. ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
  484. /* Requeue this join task with a backoff delay */
  485. __ipoib_mcast_schedule_join_thread(priv, mcast, 1);
  486. clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
  487. spin_unlock_irq(&priv->lock);
  488. complete(&mcast->done);
  489. spin_lock_irq(&priv->lock);
  490. }
  491. return 0;
  492. }
  493. void ipoib_mcast_join_task(struct work_struct *work)
  494. {
  495. struct ipoib_dev_priv *priv =
  496. container_of(work, struct ipoib_dev_priv, mcast_task.work);
  497. struct net_device *dev = priv->dev;
  498. struct ib_port_attr port_attr;
  499. unsigned long delay_until = 0;
  500. struct ipoib_mcast *mcast = NULL;
  501. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  502. return;
  503. if (ib_query_port(priv->ca, priv->port, &port_attr)) {
  504. ipoib_dbg(priv, "ib_query_port() failed\n");
  505. return;
  506. }
  507. if (port_attr.state != IB_PORT_ACTIVE) {
  508. ipoib_dbg(priv, "port state is not ACTIVE (state = %d) suspending join task\n",
  509. port_attr.state);
  510. return;
  511. }
  512. priv->local_lid = port_attr.lid;
  513. netif_addr_lock_bh(dev);
  514. if (!test_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags)) {
  515. netif_addr_unlock_bh(dev);
  516. return;
  517. }
  518. netif_addr_unlock_bh(dev);
  519. spin_lock_irq(&priv->lock);
  520. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  521. goto out;
  522. if (!priv->broadcast) {
  523. struct ipoib_mcast *broadcast;
  524. broadcast = ipoib_mcast_alloc(dev, 0);
  525. if (!broadcast) {
  526. ipoib_warn(priv, "failed to allocate broadcast group\n");
  527. /*
  528. * Restart us after a 1 second delay to retry
  529. * creating our broadcast group and attaching to
  530. * it. Until this succeeds, this ipoib dev is
  531. * completely stalled (multicast wise).
  532. */
  533. __ipoib_mcast_schedule_join_thread(priv, NULL, 1);
  534. goto out;
  535. }
  536. memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
  537. sizeof (union ib_gid));
  538. priv->broadcast = broadcast;
  539. __ipoib_mcast_add(dev, priv->broadcast);
  540. }
  541. if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
  542. if (IS_ERR_OR_NULL(priv->broadcast->mc) &&
  543. !test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags)) {
  544. mcast = priv->broadcast;
  545. if (mcast->backoff > 1 &&
  546. time_before(jiffies, mcast->delay_until)) {
  547. delay_until = mcast->delay_until;
  548. mcast = NULL;
  549. }
  550. }
  551. goto out;
  552. }
  553. /*
  554. * We'll never get here until the broadcast group is both allocated
  555. * and attached
  556. */
  557. list_for_each_entry(mcast, &priv->multicast_list, list) {
  558. if (IS_ERR_OR_NULL(mcast->mc) &&
  559. !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags) &&
  560. (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ||
  561. !skb_queue_empty(&mcast->pkt_queue))) {
  562. if (mcast->backoff == 1 ||
  563. time_after_eq(jiffies, mcast->delay_until)) {
  564. /* Found the next unjoined group */
  565. if (ipoib_mcast_join(dev, mcast)) {
  566. spin_unlock_irq(&priv->lock);
  567. return;
  568. }
  569. } else if (!delay_until ||
  570. time_before(mcast->delay_until, delay_until))
  571. delay_until = mcast->delay_until;
  572. }
  573. }
  574. mcast = NULL;
  575. ipoib_dbg_mcast(priv, "successfully started all multicast joins\n");
  576. out:
  577. if (delay_until) {
  578. cancel_delayed_work(&priv->mcast_task);
  579. queue_delayed_work(priv->wq, &priv->mcast_task,
  580. delay_until - jiffies);
  581. }
  582. if (mcast)
  583. ipoib_mcast_join(dev, mcast);
  584. spin_unlock_irq(&priv->lock);
  585. }
  586. void ipoib_mcast_start_thread(struct net_device *dev)
  587. {
  588. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  589. unsigned long flags;
  590. ipoib_dbg_mcast(priv, "starting multicast thread\n");
  591. spin_lock_irqsave(&priv->lock, flags);
  592. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  593. spin_unlock_irqrestore(&priv->lock, flags);
  594. }
  595. int ipoib_mcast_stop_thread(struct net_device *dev)
  596. {
  597. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  598. ipoib_dbg_mcast(priv, "stopping multicast thread\n");
  599. cancel_delayed_work_sync(&priv->mcast_task);
  600. return 0;
  601. }
  602. static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
  603. {
  604. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  605. struct rdma_netdev *rn = netdev_priv(dev);
  606. int ret = 0;
  607. if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  608. ipoib_warn(priv, "ipoib_mcast_leave on an in-flight join\n");
  609. if (!IS_ERR_OR_NULL(mcast->mc))
  610. ib_sa_free_multicast(mcast->mc);
  611. if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
  612. ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
  613. mcast->mcmember.mgid.raw);
  614. /* Remove ourselves from the multicast group */
  615. ret = rn->detach_mcast(dev, priv->ca, &mcast->mcmember.mgid,
  616. be16_to_cpu(mcast->mcmember.mlid));
  617. if (ret)
  618. ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
  619. } else if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
  620. ipoib_dbg(priv, "leaving with no mcmember but not a "
  621. "SENDONLY join\n");
  622. return 0;
  623. }
  624. /*
  625. * Check if the multicast group is sendonly. If so remove it from the maps
  626. * and add to the remove list
  627. */
  628. void ipoib_check_and_add_mcast_sendonly(struct ipoib_dev_priv *priv, u8 *mgid,
  629. struct list_head *remove_list)
  630. {
  631. /* Is this multicast ? */
  632. if (*mgid == 0xff) {
  633. struct ipoib_mcast *mcast = __ipoib_mcast_find(priv->dev, mgid);
  634. if (mcast && test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  635. list_del(&mcast->list);
  636. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  637. list_add_tail(&mcast->list, remove_list);
  638. }
  639. }
  640. }
  641. void ipoib_mcast_remove_list(struct list_head *remove_list)
  642. {
  643. struct ipoib_mcast *mcast, *tmcast;
  644. /*
  645. * make sure the in-flight joins have finished before we attempt
  646. * to leave
  647. */
  648. list_for_each_entry_safe(mcast, tmcast, remove_list, list)
  649. if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
  650. wait_for_completion(&mcast->done);
  651. list_for_each_entry_safe(mcast, tmcast, remove_list, list) {
  652. ipoib_mcast_leave(mcast->dev, mcast);
  653. ipoib_mcast_free(mcast);
  654. }
  655. }
  656. void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
  657. {
  658. struct ipoib_dev_priv *priv = ipoib_priv(dev);
  659. struct rdma_netdev *rn = 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. mcast->ah->last_send = rn->send(dev, skb, mcast->ah->ah,
  720. 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 = ipoib_priv(dev);
  731. LIST_HEAD(remove_list);
  732. struct ipoib_mcast *mcast, *tmcast;
  733. unsigned long flags;
  734. mutex_lock(&priv->mcast_mutex);
  735. ipoib_dbg_mcast(priv, "flushing multicast list\n");
  736. spin_lock_irqsave(&priv->lock, flags);
  737. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  738. list_del(&mcast->list);
  739. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  740. list_add_tail(&mcast->list, &remove_list);
  741. }
  742. if (priv->broadcast) {
  743. rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
  744. list_add_tail(&priv->broadcast->list, &remove_list);
  745. priv->broadcast = NULL;
  746. }
  747. spin_unlock_irqrestore(&priv->lock, flags);
  748. ipoib_mcast_remove_list(&remove_list);
  749. mutex_unlock(&priv->mcast_mutex);
  750. }
  751. static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
  752. {
  753. /* reserved QPN, prefix, scope */
  754. if (memcmp(addr, broadcast, 6))
  755. return 0;
  756. /* signature lower, pkey */
  757. if (memcmp(addr + 7, broadcast + 7, 3))
  758. return 0;
  759. return 1;
  760. }
  761. void ipoib_mcast_restart_task(struct work_struct *work)
  762. {
  763. struct ipoib_dev_priv *priv =
  764. container_of(work, struct ipoib_dev_priv, restart_task);
  765. struct net_device *dev = priv->dev;
  766. struct netdev_hw_addr *ha;
  767. struct ipoib_mcast *mcast, *tmcast;
  768. LIST_HEAD(remove_list);
  769. unsigned long flags;
  770. struct ib_sa_mcmember_rec rec;
  771. if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
  772. /*
  773. * shortcut...on shutdown flush is called next, just
  774. * let it do all the work
  775. */
  776. return;
  777. ipoib_dbg_mcast(priv, "restarting multicast task\n");
  778. local_irq_save(flags);
  779. netif_addr_lock(dev);
  780. spin_lock(&priv->lock);
  781. /*
  782. * Unfortunately, the networking core only gives us a list of all of
  783. * the multicast hardware addresses. We need to figure out which ones
  784. * are new and which ones have been removed
  785. */
  786. /* Clear out the found flag */
  787. list_for_each_entry(mcast, &priv->multicast_list, list)
  788. clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  789. /* Mark all of the entries that are found or don't exist */
  790. netdev_for_each_mc_addr(ha, dev) {
  791. union ib_gid mgid;
  792. if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
  793. continue;
  794. memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
  795. mcast = __ipoib_mcast_find(dev, &mgid);
  796. if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  797. struct ipoib_mcast *nmcast;
  798. /* ignore group which is directly joined by userspace */
  799. if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
  800. !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
  801. ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
  802. mgid.raw);
  803. continue;
  804. }
  805. /* Not found or send-only group, let's add a new entry */
  806. ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
  807. mgid.raw);
  808. nmcast = ipoib_mcast_alloc(dev, 0);
  809. if (!nmcast) {
  810. ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
  811. continue;
  812. }
  813. set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);
  814. nmcast->mcmember.mgid = mgid;
  815. if (mcast) {
  816. /* Destroy the send only entry */
  817. list_move_tail(&mcast->list, &remove_list);
  818. rb_replace_node(&mcast->rb_node,
  819. &nmcast->rb_node,
  820. &priv->multicast_tree);
  821. } else
  822. __ipoib_mcast_add(dev, nmcast);
  823. list_add_tail(&nmcast->list, &priv->multicast_list);
  824. }
  825. if (mcast)
  826. set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
  827. }
  828. /* Remove all of the entries don't exist anymore */
  829. list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
  830. if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
  831. !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
  832. ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
  833. mcast->mcmember.mgid.raw);
  834. rb_erase(&mcast->rb_node, &priv->multicast_tree);
  835. /* Move to the remove list */
  836. list_move_tail(&mcast->list, &remove_list);
  837. }
  838. }
  839. spin_unlock(&priv->lock);
  840. netif_addr_unlock(dev);
  841. local_irq_restore(flags);
  842. ipoib_mcast_remove_list(&remove_list);
  843. /*
  844. * Double check that we are still up
  845. */
  846. if (test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
  847. spin_lock_irqsave(&priv->lock, flags);
  848. __ipoib_mcast_schedule_join_thread(priv, NULL, 0);
  849. spin_unlock_irqrestore(&priv->lock, flags);
  850. }
  851. }
  852. #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
  853. struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
  854. {
  855. struct ipoib_mcast_iter *iter;
  856. iter = kmalloc(sizeof *iter, GFP_KERNEL);
  857. if (!iter)
  858. return NULL;
  859. iter->dev = dev;
  860. memset(iter->mgid.raw, 0, 16);
  861. if (ipoib_mcast_iter_next(iter)) {
  862. kfree(iter);
  863. return NULL;
  864. }
  865. return iter;
  866. }
  867. int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
  868. {
  869. struct ipoib_dev_priv *priv = ipoib_priv(iter->dev);
  870. struct rb_node *n;
  871. struct ipoib_mcast *mcast;
  872. int ret = 1;
  873. spin_lock_irq(&priv->lock);
  874. n = rb_first(&priv->multicast_tree);
  875. while (n) {
  876. mcast = rb_entry(n, struct ipoib_mcast, rb_node);
  877. if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
  878. sizeof (union ib_gid)) < 0) {
  879. iter->mgid = mcast->mcmember.mgid;
  880. iter->created = mcast->created;
  881. iter->queuelen = skb_queue_len(&mcast->pkt_queue);
  882. iter->complete = !!mcast->ah;
  883. iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));
  884. ret = 0;
  885. break;
  886. }
  887. n = rb_next(n);
  888. }
  889. spin_unlock_irq(&priv->lock);
  890. return ret;
  891. }
  892. void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
  893. union ib_gid *mgid,
  894. unsigned long *created,
  895. unsigned int *queuelen,
  896. unsigned int *complete,
  897. unsigned int *send_only)
  898. {
  899. *mgid = iter->mgid;
  900. *created = iter->created;
  901. *queuelen = iter->queuelen;
  902. *complete = iter->complete;
  903. *send_only = iter->send_only;
  904. }
  905. #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */