ipoib_multicast.c 28.1 KB
Newer Older
L
Linus Torvalds 已提交
1 2
/*
 * Copyright (c) 2004, 2005 Topspin Communications.  All rights reserved.
3 4
 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
L
Linus Torvalds 已提交
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL) Version 2, available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <linux/skbuff.h>
#include <linux/rtnetlink.h>
37
#include <linux/moduleparam.h>
L
Linus Torvalds 已提交
38 39 40 41 42 43
#include <linux/ip.h>
#include <linux/in.h>
#include <linux/igmp.h>
#include <linux/inetdevice.h>
#include <linux/delay.h>
#include <linux/completion.h>
44
#include <linux/slab.h>
L
Linus Torvalds 已提交
45

46 47
#include <net/dst.h>

L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
#include "ipoib.h"

#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
static int mcast_debug_level;

module_param(mcast_debug_level, int, 0644);
MODULE_PARM_DESC(mcast_debug_level,
		 "Enable multicast debug tracing if > 0");
#endif

struct ipoib_mcast_iter {
	struct net_device *dev;
	union ib_gid       mgid;
	unsigned long      created;
	unsigned int       queuelen;
	unsigned int       complete;
	unsigned int       send_only;
};

67
/*
D
Doug Ledford 已提交
68
 * This should be called with the priv->lock held
69 70 71 72 73
 */
static void __ipoib_mcast_schedule_join_thread(struct ipoib_dev_priv *priv,
					       struct ipoib_mcast *mcast,
					       bool delay)
{
74
	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
		return;

	/*
	 * We will be scheduling *something*, so cancel whatever is
	 * currently scheduled first
	 */
	cancel_delayed_work(&priv->mcast_task);
	if (mcast && delay) {
		/*
		 * We had a failure and want to schedule a retry later
		 */
		mcast->backoff *= 2;
		if (mcast->backoff > IPOIB_MAX_BACKOFF_SECONDS)
			mcast->backoff = IPOIB_MAX_BACKOFF_SECONDS;
		mcast->delay_until = jiffies + (mcast->backoff * HZ);
		/*
		 * Mark this mcast for its delay, but restart the
		 * task immediately.  The join task will make sure to
		 * clear out all entries without delays, and then
		 * schedule itself to run again when the earliest
		 * delay expires
		 */
		queue_delayed_work(priv->wq, &priv->mcast_task, 0);
	} else if (delay) {
		/*
		 * Special case of retrying after a failure to
		 * allocate the broadcast multicast group, wait
		 * 1 second and try again
		 */
		queue_delayed_work(priv->wq, &priv->mcast_task, HZ);
	} else
		queue_delayed_work(priv->wq, &priv->mcast_task, 0);
}

L
Linus Torvalds 已提交
109 110 111
static void ipoib_mcast_free(struct ipoib_mcast *mcast)
{
	struct net_device *dev = mcast->dev;
112
	int tx_dropped = 0;
L
Linus Torvalds 已提交
113

H
Harvey Harrison 已提交
114
	ipoib_dbg_mcast(netdev_priv(dev), "deleting multicast group %pI6\n",
115
			mcast->mcmember.mgid.raw);
L
Linus Torvalds 已提交
116

117 118
	/* remove all neigh connected to this mcast */
	ipoib_del_neighs_by_gid(dev, mcast->mcmember.mgid.raw);
L
Linus Torvalds 已提交
119 120 121 122

	if (mcast->ah)
		ipoib_put_ah(mcast->ah);

123 124
	while (!skb_queue_empty(&mcast->pkt_queue)) {
		++tx_dropped;
125
		dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
126 127
	}

128
	netif_tx_lock_bh(dev);
129
	dev->stats.tx_dropped += tx_dropped;
130
	netif_tx_unlock_bh(dev);
L
Linus Torvalds 已提交
131 132 133 134 135 136 137 138 139

	kfree(mcast);
}

static struct ipoib_mcast *ipoib_mcast_alloc(struct net_device *dev,
					     int can_sleep)
{
	struct ipoib_mcast *mcast;

R
Roland Dreier 已提交
140
	mcast = kzalloc(sizeof *mcast, can_sleep ? GFP_KERNEL : GFP_ATOMIC);
L
Linus Torvalds 已提交
141 142 143 144 145
	if (!mcast)
		return NULL;

	mcast->dev = dev;
	mcast->created = jiffies;
146
	mcast->delay_until = jiffies;
147
	mcast->backoff = 1;
L
Linus Torvalds 已提交
148 149 150 151 152 153 154 155

	INIT_LIST_HEAD(&mcast->list);
	INIT_LIST_HEAD(&mcast->neigh_list);
	skb_queue_head_init(&mcast->pkt_queue);

	return mcast;
}

156
static struct ipoib_mcast *__ipoib_mcast_find(struct net_device *dev, void *mgid)
L
Linus Torvalds 已提交
157 158 159 160 161 162 163 164 165 166
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	struct rb_node *n = priv->multicast_tree.rb_node;

	while (n) {
		struct ipoib_mcast *mcast;
		int ret;

		mcast = rb_entry(n, struct ipoib_mcast, rb_node);

167
		ret = memcmp(mgid, mcast->mcmember.mgid.raw,
L
Linus Torvalds 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
			     sizeof (union ib_gid));
		if (ret < 0)
			n = n->rb_left;
		else if (ret > 0)
			n = n->rb_right;
		else
			return mcast;
	}

	return NULL;
}

static int __ipoib_mcast_add(struct net_device *dev, struct ipoib_mcast *mcast)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	struct rb_node **n = &priv->multicast_tree.rb_node, *pn = NULL;

	while (*n) {
		struct ipoib_mcast *tmcast;
		int ret;

		pn = *n;
		tmcast = rb_entry(pn, struct ipoib_mcast, rb_node);

		ret = memcmp(mcast->mcmember.mgid.raw, tmcast->mcmember.mgid.raw,
			     sizeof (union ib_gid));
		if (ret < 0)
			n = &pn->rb_left;
		else if (ret > 0)
			n = &pn->rb_right;
		else
			return -EEXIST;
	}

	rb_link_node(&mcast->rb_node, pn, n);
	rb_insert_color(&mcast->rb_node, &priv->multicast_tree);

	return 0;
}

static int ipoib_mcast_join_finish(struct ipoib_mcast *mcast,
				   struct ib_sa_mcmember_rec *mcmember)
{
	struct net_device *dev = mcast->dev;
	struct ipoib_dev_priv *priv = netdev_priv(dev);
213
	struct ipoib_ah *ah;
L
Linus Torvalds 已提交
214
	int ret;
215
	int set_qkey = 0;
L
Linus Torvalds 已提交
216 217 218

	mcast->mcmember = *mcmember;

219 220 221
	/* Set the multicast MTU and cached Q_Key before we attach if it's
	 * the broadcast group.
	 */
L
Linus Torvalds 已提交
222 223
	if (!memcmp(mcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
		    sizeof (union ib_gid))) {
224 225 226 227 228
		spin_lock_irq(&priv->lock);
		if (!priv->broadcast) {
			spin_unlock_irq(&priv->lock);
			return -EAGAIN;
		}
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
		/*update priv member according to the new mcast*/
		priv->broadcast->mcmember.qkey = mcmember->qkey;
		priv->broadcast->mcmember.mtu = mcmember->mtu;
		priv->broadcast->mcmember.traffic_class = mcmember->traffic_class;
		priv->broadcast->mcmember.rate = mcmember->rate;
		priv->broadcast->mcmember.sl = mcmember->sl;
		priv->broadcast->mcmember.flow_label = mcmember->flow_label;
		priv->broadcast->mcmember.hop_limit = mcmember->hop_limit;
		/* assume if the admin and the mcast are the same both can be changed */
		if (priv->mcast_mtu == priv->admin_mtu)
			priv->admin_mtu =
			priv->mcast_mtu =
			IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));
		else
			priv->mcast_mtu =
			IPOIB_UD_MTU(ib_mtu_enum_to_int(priv->broadcast->mcmember.mtu));

L
Linus Torvalds 已提交
246
		priv->qkey = be32_to_cpu(priv->broadcast->mcmember.qkey);
247
		spin_unlock_irq(&priv->lock);
L
Linus Torvalds 已提交
248
		priv->tx_wr.wr.ud.remote_qkey = priv->qkey;
249
		set_qkey = 1;
L
Linus Torvalds 已提交
250 251 252 253
	}

	if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
		if (test_and_set_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
H
Harvey Harrison 已提交
254
			ipoib_warn(priv, "multicast group %pI6 already attached\n",
255
				   mcast->mcmember.mgid.raw);
L
Linus Torvalds 已提交
256 257 258 259 260

			return 0;
		}

		ret = ipoib_mcast_attach(dev, be16_to_cpu(mcast->mcmember.mlid),
261
					 &mcast->mcmember.mgid, set_qkey);
L
Linus Torvalds 已提交
262
		if (ret < 0) {
H
Harvey Harrison 已提交
263
			ipoib_warn(priv, "couldn't attach QP to multicast group %pI6\n",
264
				   mcast->mcmember.mgid.raw);
L
Linus Torvalds 已提交
265 266 267 268 269 270 271 272 273 274 275 276

			clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags);
			return ret;
		}
	}

	{
		struct ib_ah_attr av = {
			.dlid	       = be16_to_cpu(mcast->mcmember.mlid),
			.port_num      = priv->port,
			.sl	       = mcast->mcmember.sl,
			.ah_flags      = IB_AH_GRH,
277
			.static_rate   = mcast->mcmember.rate,
L
Linus Torvalds 已提交
278 279 280 281 282 283 284 285 286
			.grh	       = {
				.flow_label    = be32_to_cpu(mcast->mcmember.flow_label),
				.hop_limit     = mcast->mcmember.hop_limit,
				.sgid_index    = 0,
				.traffic_class = mcast->mcmember.traffic_class
			}
		};
		av.grh.dgid = mcast->mcmember.mgid;

287
		ah = ipoib_create_ah(dev, priv->pd, &av);
288 289 290 291 292
		if (IS_ERR(ah)) {
			ipoib_warn(priv, "ib_address_create failed %ld\n",
				-PTR_ERR(ah));
			/* use original error */
			return PTR_ERR(ah);
L
Linus Torvalds 已提交
293
		} else {
294 295 296 297
			spin_lock_irq(&priv->lock);
			mcast->ah = ah;
			spin_unlock_irq(&priv->lock);

H
Harvey Harrison 已提交
298
			ipoib_dbg_mcast(priv, "MGID %pI6 AV %p, LID 0x%04x, SL %d\n",
299
					mcast->mcmember.mgid.raw,
L
Linus Torvalds 已提交
300 301 302 303 304 305 306
					mcast->ah->ah,
					be16_to_cpu(mcast->mcmember.mlid),
					mcast->mcmember.sl);
		}
	}

	/* actually send any queued packets */
307
	netif_tx_lock_bh(dev);
L
Linus Torvalds 已提交
308 309
	while (!skb_queue_empty(&mcast->pkt_queue)) {
		struct sk_buff *skb = skb_dequeue(&mcast->pkt_queue);
310

311
		netif_tx_unlock_bh(dev);
L
Linus Torvalds 已提交
312 313 314 315

		skb->dev = dev;
		if (dev_queue_xmit(skb))
			ipoib_warn(priv, "dev_queue_xmit failed to requeue packet\n");
316

317
		netif_tx_lock_bh(dev);
L
Linus Torvalds 已提交
318
	}
319
	netif_tx_unlock_bh(dev);
L
Linus Torvalds 已提交
320 321 322 323

	return 0;
}

324 325 326 327
void ipoib_mcast_carrier_on_task(struct work_struct *work)
{
	struct ipoib_dev_priv *priv = container_of(work, struct ipoib_dev_priv,
						   carrier_on_task);
328
	struct ib_port_attr attr;
329

330 331 332 333 334 335
	if (ib_query_port(priv->ca, priv->port, &attr) ||
	    attr.state != IB_PORT_ACTIVE) {
		ipoib_dbg(priv, "Keeping carrier off until IB port is active\n");
		return;
	}

336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
	/*
	 * Take rtnl_lock to avoid racing with ipoib_stop() and
	 * turning the carrier back on while a device is being
	 * removed.  However, ipoib_stop() will attempt to flush
	 * the workqueue while holding the rtnl lock, so loop
	 * on trylock until either we get the lock or we see
	 * FLAG_OPER_UP go away as that signals that we are bailing
	 * and can safely ignore the carrier on work.
	 */
	while (!rtnl_trylock()) {
		if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
			return;
		else
			msleep(20);
	}
351 352
	if (!ipoib_cm_admin_enabled(priv->dev))
		dev_set_mtu(priv->dev, min(priv->mcast_mtu, priv->admin_mtu));
353 354 355 356
	netif_carrier_on(priv->dev);
	rtnl_unlock();
}

357 358
static int ipoib_mcast_join_complete(int status,
				     struct ib_sa_multicast *multicast)
L
Linus Torvalds 已提交
359
{
360
	struct ipoib_mcast *mcast = multicast->context;
L
Linus Torvalds 已提交
361 362 363
	struct net_device *dev = mcast->dev;
	struct ipoib_dev_priv *priv = netdev_priv(dev);

364 365 366
	ipoib_dbg_mcast(priv, "%sjoin completion for %pI6 (status %d)\n",
			test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ?
			"sendonly " : "",
367
			mcast->mcmember.mgid.raw, status);
L
Linus Torvalds 已提交
368

369
	/* We trap for port events ourselves. */
370 371
	if (status == -ENETRESET) {
		status = 0;
372
		goto out;
373
	}
374 375 376 377 378

	if (!status)
		status = ipoib_mcast_join_finish(mcast, &multicast->rec);

	if (!status) {
379
		mcast->backoff = 1;
380
		mcast->delay_until = jiffies;
381

382
		/*
383
		 * Defer carrier on work to priv->wq to avoid a
384 385 386 387
		 * deadlock on rtnl_lock here.  Requeue our multicast
		 * work too, which will end up happening right after
		 * our carrier on task work and will allow us to
		 * send out all of the non-broadcast joins
388
		 */
389
		if (mcast == priv->broadcast) {
D
Doug Ledford 已提交
390
			spin_lock_irq(&priv->lock);
391
			queue_work(priv->wq, &priv->carrier_on_task);
392
			__ipoib_mcast_schedule_join_thread(priv, NULL, 0);
D
Doug Ledford 已提交
393
			goto out_locked;
394
		}
395 396 397
	} else {
		if (mcast->logcount++ < 20) {
			if (status == -ETIMEDOUT || status == -EAGAIN) {
398 399
				ipoib_dbg_mcast(priv, "%smulticast join failed for %pI6, status %d\n",
						test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
400 401
						mcast->mcmember.mgid.raw, status);
			} else {
402 403
				ipoib_warn(priv, "%smulticast join failed for %pI6, status %d\n",
						test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ? "sendonly " : "",
404 405
					   mcast->mcmember.mgid.raw, status);
			}
406 407
		}

408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
		if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) &&
		    mcast->backoff >= 2) {
			/*
			 * We only retry sendonly joins once before we drop
			 * the packet and quit trying to deal with the
			 * group.  However, we leave the group in the
			 * mcast list as an unjoined group.  If we want to
			 * try joining again, we simply queue up a packet
			 * and restart the join thread.  The empty queue
			 * is why the join thread ignores this group.
			 */
			mcast->backoff = 1;
			netif_tx_lock_bh(dev);
			while (!skb_queue_empty(&mcast->pkt_queue)) {
				++dev->stats.tx_dropped;
				dev_kfree_skb_any(skb_dequeue(&mcast->pkt_queue));
			}
			netif_tx_unlock_bh(dev);
D
Doug Ledford 已提交
426 427
		} else {
			spin_lock_irq(&priv->lock);
428 429
			/* Requeue this join task with a backoff delay */
			__ipoib_mcast_schedule_join_thread(priv, mcast, 1);
D
Doug Ledford 已提交
430 431
			goto out_locked;
		}
432
	}
433
out:
D
Doug Ledford 已提交
434 435 436 437 438 439
	spin_lock_irq(&priv->lock);
out_locked:
	/*
	 * Make sure to set mcast->mc before we clear the busy flag to avoid
	 * racing with code that checks for BUSY before checking mcast->mc
	 */
440 441
	if (status)
		mcast->mc = NULL;
D
Doug Ledford 已提交
442 443 444 445
	else
		mcast->mc = multicast;
	clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
	spin_unlock_irq(&priv->lock);
446
	complete(&mcast->done);
D
Doug Ledford 已提交
447

448
	return status;
L
Linus Torvalds 已提交
449 450
}

451
static void ipoib_mcast_join(struct net_device *dev, struct ipoib_mcast *mcast)
L
Linus Torvalds 已提交
452 453
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
D
Doug Ledford 已提交
454
	struct ib_sa_multicast *multicast;
L
Linus Torvalds 已提交
455 456 457 458 459 460
	struct ib_sa_mcmember_rec rec = {
		.join_state = 1
	};
	ib_sa_comp_mask comp_mask;
	int ret = 0;

H
Harvey Harrison 已提交
461
	ipoib_dbg_mcast(priv, "joining MGID %pI6\n", mcast->mcmember.mgid.raw);
L
Linus Torvalds 已提交
462 463 464

	rec.mgid     = mcast->mcmember.mgid;
	rec.port_gid = priv->local_gid;
465
	rec.pkey     = cpu_to_be16(priv->pkey);
L
Linus Torvalds 已提交
466 467 468 469 470 471 472

	comp_mask =
		IB_SA_MCMEMBER_REC_MGID		|
		IB_SA_MCMEMBER_REC_PORT_GID	|
		IB_SA_MCMEMBER_REC_PKEY		|
		IB_SA_MCMEMBER_REC_JOIN_STATE;

473 474 475 476 477 478 479 480
	if (mcast != priv->broadcast) {
		/*
		 * RFC 4391:
		 *  The MGID MUST use the same P_Key, Q_Key, SL, MTU,
		 *  and HopLimit as those used in the broadcast-GID.  The rest
		 *  of attributes SHOULD follow the values used in the
		 *  broadcast-GID as well.
		 */
L
Linus Torvalds 已提交
481
		comp_mask |=
482 483 484 485 486 487 488 489 490
			IB_SA_MCMEMBER_REC_QKEY			|
			IB_SA_MCMEMBER_REC_MTU_SELECTOR		|
			IB_SA_MCMEMBER_REC_MTU			|
			IB_SA_MCMEMBER_REC_TRAFFIC_CLASS	|
			IB_SA_MCMEMBER_REC_RATE_SELECTOR	|
			IB_SA_MCMEMBER_REC_RATE			|
			IB_SA_MCMEMBER_REC_SL			|
			IB_SA_MCMEMBER_REC_FLOW_LABEL		|
			IB_SA_MCMEMBER_REC_HOP_LIMIT;
L
Linus Torvalds 已提交
491 492

		rec.qkey	  = priv->broadcast->mcmember.qkey;
493 494 495 496 497
		rec.mtu_selector  = IB_SA_EQ;
		rec.mtu		  = priv->broadcast->mcmember.mtu;
		rec.traffic_class = priv->broadcast->mcmember.traffic_class;
		rec.rate_selector = IB_SA_EQ;
		rec.rate	  = priv->broadcast->mcmember.rate;
L
Linus Torvalds 已提交
498 499
		rec.sl		  = priv->broadcast->mcmember.sl;
		rec.flow_label	  = priv->broadcast->mcmember.flow_label;
500
		rec.hop_limit	  = priv->broadcast->mcmember.hop_limit;
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516

		/*
		 * Historically Linux IPoIB has never properly supported SEND
		 * ONLY join. It emulated it by not providing all the required
		 * attributes, which is enough to prevent group creation and
		 * detect if there are full members or not. A major problem
		 * with supporting SEND ONLY is detecting when the group is
		 * auto-destroyed as IPoIB will cache the MLID..
		 */
#if 1
		if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
			comp_mask &= ~IB_SA_MCMEMBER_REC_TRAFFIC_CLASS;
#else
		if (test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
			rec.join_state = 4;
#endif
L
Linus Torvalds 已提交
517 518
	}

D
Doug Ledford 已提交
519
	multicast = ib_sa_join_multicast(&ipoib_sa_client, priv->ca, priv->port,
520 521
					 &rec, comp_mask, GFP_KERNEL,
					 ipoib_mcast_join_complete, mcast);
D
Doug Ledford 已提交
522 523
	if (IS_ERR(multicast)) {
		ret = PTR_ERR(multicast);
524
		ipoib_warn(priv, "ib_sa_join_multicast failed, status %d\n", ret);
D
Doug Ledford 已提交
525 526
		spin_lock_irq(&priv->lock);
		/* Requeue this join task with a backoff delay */
527
		__ipoib_mcast_schedule_join_thread(priv, mcast, 1);
D
Doug Ledford 已提交
528 529
		clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
		spin_unlock_irq(&priv->lock);
530
		complete(&mcast->done);
531
	}
L
Linus Torvalds 已提交
532 533
}

D
David Howells 已提交
534
void ipoib_mcast_join_task(struct work_struct *work)
L
Linus Torvalds 已提交
535
{
D
David Howells 已提交
536 537 538
	struct ipoib_dev_priv *priv =
		container_of(work, struct ipoib_dev_priv, mcast_task.work);
	struct net_device *dev = priv->dev;
539
	struct ib_port_attr port_attr;
540 541
	unsigned long delay_until = 0;
	struct ipoib_mcast *mcast = NULL;
L
Linus Torvalds 已提交
542

543
	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
L
Linus Torvalds 已提交
544 545
		return;

546 547 548 549 550 551
	if (ib_query_port(priv->ca, priv->port, &port_attr) ||
	    port_attr.state != IB_PORT_ACTIVE) {
		ipoib_dbg(priv, "port state is not ACTIVE (state = %d) suspending join task\n",
			  port_attr.state);
		return;
	}
552
	priv->local_lid = port_attr.lid;
553

L
Linus Torvalds 已提交
554
	if (ib_query_gid(priv->ca, priv->port, 0, &priv->local_gid))
555
		ipoib_warn(priv, "ib_query_gid() failed\n");
L
Linus Torvalds 已提交
556 557 558
	else
		memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));

559 560 561 562
	spin_lock_irq(&priv->lock);
	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
		goto out;

L
Linus Torvalds 已提交
563
	if (!priv->broadcast) {
564 565
		struct ipoib_mcast *broadcast;

566
		broadcast = ipoib_mcast_alloc(dev, 0);
567
		if (!broadcast) {
L
Linus Torvalds 已提交
568
			ipoib_warn(priv, "failed to allocate broadcast group\n");
569 570 571 572 573 574 575 576
			/*
			 * Restart us after a 1 second delay to retry
			 * creating our broadcast group and attaching to
			 * it.  Until this succeeds, this ipoib dev is
			 * completely stalled (multicast wise).
			 */
			__ipoib_mcast_schedule_join_thread(priv, NULL, 1);
			goto out;
L
Linus Torvalds 已提交
577 578
		}

579
		memcpy(broadcast->mcmember.mgid.raw, priv->dev->broadcast + 4,
L
Linus Torvalds 已提交
580
		       sizeof (union ib_gid));
581
		priv->broadcast = broadcast;
L
Linus Torvalds 已提交
582 583 584 585 586

		__ipoib_mcast_add(dev, priv->broadcast);
	}

	if (!test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
587 588 589 590 591 592 593 594 595 596
		if (IS_ERR_OR_NULL(priv->broadcast->mc) &&
		    !test_bit(IPOIB_MCAST_FLAG_BUSY, &priv->broadcast->flags)) {
			mcast = priv->broadcast;
			if (mcast->backoff > 1 &&
			    time_before(jiffies, mcast->delay_until)) {
				delay_until = mcast->delay_until;
				mcast = NULL;
			}
		}
		goto out;
L
Linus Torvalds 已提交
597 598
	}

599 600 601 602 603 604 605
	/*
	 * We'll never get here until the broadcast group is both allocated
	 * and attached
	 */
	list_for_each_entry(mcast, &priv->multicast_list, list) {
		if (IS_ERR_OR_NULL(mcast->mc) &&
		    !test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags) &&
606 607
		    (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags) ||
		     !skb_queue_empty(&mcast->pkt_queue))) {
608
			if (mcast->backoff == 1 ||
609
			    time_after_eq(jiffies, mcast->delay_until)) {
L
Linus Torvalds 已提交
610
				/* Found the next unjoined group */
611 612 613
				init_completion(&mcast->done);
				set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
				spin_unlock_irq(&priv->lock);
614
				ipoib_mcast_join(dev, mcast);
615 616
				spin_lock_irq(&priv->lock);
			} else if (!delay_until ||
617 618
				 time_before(mcast->delay_until, delay_until))
				delay_until = mcast->delay_until;
L
Linus Torvalds 已提交
619 620 621
		}
	}

622 623
	mcast = NULL;
	ipoib_dbg_mcast(priv, "successfully started all multicast joins\n");
L
Linus Torvalds 已提交
624

625
out:
626 627 628 629 630
	if (delay_until) {
		cancel_delayed_work(&priv->mcast_task);
		queue_delayed_work(priv->wq, &priv->mcast_task,
				   delay_until - jiffies);
	}
631 632 633 634 635
	if (mcast) {
		init_completion(&mcast->done);
		set_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags);
	}
	spin_unlock_irq(&priv->lock);
636
	if (mcast)
637
		ipoib_mcast_join(dev, mcast);
L
Linus Torvalds 已提交
638 639 640 641 642
}

int ipoib_mcast_start_thread(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
D
Doug Ledford 已提交
643
	unsigned long flags;
L
Linus Torvalds 已提交
644 645 646

	ipoib_dbg_mcast(priv, "starting multicast thread\n");

D
Doug Ledford 已提交
647
	spin_lock_irqsave(&priv->lock, flags);
648
	__ipoib_mcast_schedule_join_thread(priv, NULL, 0);
D
Doug Ledford 已提交
649
	spin_unlock_irqrestore(&priv->lock, flags);
L
Linus Torvalds 已提交
650 651 652 653

	return 0;
}

654
int ipoib_mcast_stop_thread(struct net_device *dev)
L
Linus Torvalds 已提交
655 656
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
D
Doug Ledford 已提交
657
	unsigned long flags;
L
Linus Torvalds 已提交
658 659 660

	ipoib_dbg_mcast(priv, "stopping multicast thread\n");

D
Doug Ledford 已提交
661
	spin_lock_irqsave(&priv->lock, flags);
L
Linus Torvalds 已提交
662
	cancel_delayed_work(&priv->mcast_task);
D
Doug Ledford 已提交
663
	spin_unlock_irqrestore(&priv->lock, flags);
L
Linus Torvalds 已提交
664

665
	flush_workqueue(priv->wq);
L
Linus Torvalds 已提交
666 667 668 669 670 671 672 673 674

	return 0;
}

static int ipoib_mcast_leave(struct net_device *dev, struct ipoib_mcast *mcast)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	int ret = 0;

675
	if (test_and_clear_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
676 677 678
		ipoib_warn(priv, "ipoib_mcast_leave on an in-flight join\n");

	if (!IS_ERR_OR_NULL(mcast->mc))
679 680
		ib_sa_free_multicast(mcast->mc);

681
	if (test_and_clear_bit(IPOIB_MCAST_FLAG_ATTACHED, &mcast->flags)) {
H
Harvey Harrison 已提交
682
		ipoib_dbg_mcast(priv, "leaving MGID %pI6\n",
683
				mcast->mcmember.mgid.raw);
L
Linus Torvalds 已提交
684

685
		/* Remove ourselves from the multicast group */
686 687
		ret = ib_detach_mcast(priv->qp, &mcast->mcmember.mgid,
				      be16_to_cpu(mcast->mcmember.mlid));
688
		if (ret)
689
			ipoib_warn(priv, "ib_detach_mcast failed (result = %d)\n", ret);
690 691 692
	} else if (!test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags))
		ipoib_dbg(priv, "leaving with no mcmember but not a "
			  "SENDONLY join\n");
L
Linus Torvalds 已提交
693 694 695 696

	return 0;
}

697
void ipoib_mcast_send(struct net_device *dev, u8 *daddr, struct sk_buff *skb)
L
Linus Torvalds 已提交
698 699 700
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	struct ipoib_mcast *mcast;
701
	unsigned long flags;
702
	void *mgid = daddr + 4;
703

704
	spin_lock_irqsave(&priv->lock, flags);
L
Linus Torvalds 已提交
705

706
	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)		||
707 708
	    !priv->broadcast					||
	    !test_bit(IPOIB_MCAST_FLAG_ATTACHED, &priv->broadcast->flags)) {
709
		++dev->stats.tx_dropped;
710 711 712 713
		dev_kfree_skb_any(skb);
		goto unlock;
	}

L
Linus Torvalds 已提交
714
	mcast = __ipoib_mcast_find(dev, mgid);
715
	if (!mcast || !mcast->ah) {
L
Linus Torvalds 已提交
716
		if (!mcast) {
717 718 719 720 721 722 723 724 725 726 727 728
			/* Let's create a new send only group now */
			ipoib_dbg_mcast(priv, "setting up send only multicast group for %pI6\n",
					mgid);

			mcast = ipoib_mcast_alloc(dev, 0);
			if (!mcast) {
				ipoib_warn(priv, "unable to allocate memory "
					   "for multicast structure\n");
				++dev->stats.tx_dropped;
				dev_kfree_skb_any(skb);
				goto unlock;
			}
L
Linus Torvalds 已提交
729

730 731 732 733 734 735
			set_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags);
			memcpy(mcast->mcmember.mgid.raw, mgid,
			       sizeof (union ib_gid));
			__ipoib_mcast_add(dev, mcast);
			list_add_tail(&mcast->list, &priv->multicast_list);
		}
L
Linus Torvalds 已提交
736 737
		if (skb_queue_len(&mcast->pkt_queue) < IPOIB_MAX_MCAST_QUEUE)
			skb_queue_tail(&mcast->pkt_queue, skb);
738
		else {
739
			++dev->stats.tx_dropped;
L
Linus Torvalds 已提交
740
			dev_kfree_skb_any(skb);
741
		}
742 743 744 745
		if (!test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags)) {
			__ipoib_mcast_schedule_join_thread(priv, NULL, 0);
		}
	} else {
746 747 748 749 750 751 752 753 754 755 756
		struct ipoib_neigh *neigh;

		spin_unlock_irqrestore(&priv->lock, flags);
		neigh = ipoib_neigh_get(dev, daddr);
		spin_lock_irqsave(&priv->lock, flags);
		if (!neigh) {
			neigh = ipoib_neigh_alloc(daddr, dev);
			if (neigh) {
				kref_get(&mcast->ah->ref);
				neigh->ah	= mcast->ah;
				list_add_tail(&neigh->list, &mcast->neigh_list);
L
Linus Torvalds 已提交
757 758
			}
		}
759
		spin_unlock_irqrestore(&priv->lock, flags);
L
Linus Torvalds 已提交
760
		ipoib_send(dev, skb, mcast->ah, IB_MULTICAST_QPN);
761 762
		if (neigh)
			ipoib_neigh_put(neigh);
763
		return;
L
Linus Torvalds 已提交
764 765
	}

766
unlock:
767
	spin_unlock_irqrestore(&priv->lock, flags);
L
Linus Torvalds 已提交
768 769 770 771 772 773
}

void ipoib_mcast_dev_flush(struct net_device *dev)
{
	struct ipoib_dev_priv *priv = netdev_priv(dev);
	LIST_HEAD(remove_list);
774
	struct ipoib_mcast *mcast, *tmcast;
L
Linus Torvalds 已提交
775 776 777 778 779 780
	unsigned long flags;

	ipoib_dbg_mcast(priv, "flushing multicast list\n");

	spin_lock_irqsave(&priv->lock, flags);

781 782 783 784
	list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
		list_del(&mcast->list);
		rb_erase(&mcast->rb_node, &priv->multicast_tree);
		list_add_tail(&mcast->list, &remove_list);
L
Linus Torvalds 已提交
785 786 787
	}

	if (priv->broadcast) {
R
Roland Dreier 已提交
788
		rb_erase(&priv->broadcast->rb_node, &priv->multicast_tree);
789 790
		list_add_tail(&priv->broadcast->list, &remove_list);
		priv->broadcast = NULL;
L
Linus Torvalds 已提交
791 792 793 794
	}

	spin_unlock_irqrestore(&priv->lock, flags);

795 796 797 798
	/*
	 * make sure the in-flight joins have finished before we attempt
	 * to leave
	 */
799
	list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
800
		if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
801 802
			wait_for_completion(&mcast->done);

L
Linus Torvalds 已提交
803 804 805 806 807 808
	list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
		ipoib_mcast_leave(dev, mcast);
		ipoib_mcast_free(mcast);
	}
}

809
static int ipoib_mcast_addr_is_valid(const u8 *addr, const u8 *broadcast)
810 811 812 813 814 815 816 817 818 819
{
	/* reserved QPN, prefix, scope */
	if (memcmp(addr, broadcast, 6))
		return 0;
	/* signature lower, pkey */
	if (memcmp(addr + 7, broadcast + 7, 3))
		return 0;
	return 1;
}

D
David Howells 已提交
820
void ipoib_mcast_restart_task(struct work_struct *work)
L
Linus Torvalds 已提交
821
{
D
David Howells 已提交
822 823 824
	struct ipoib_dev_priv *priv =
		container_of(work, struct ipoib_dev_priv, restart_task);
	struct net_device *dev = priv->dev;
825
	struct netdev_hw_addr *ha;
L
Linus Torvalds 已提交
826 827 828
	struct ipoib_mcast *mcast, *tmcast;
	LIST_HEAD(remove_list);
	unsigned long flags;
829
	struct ib_sa_mcmember_rec rec;
L
Linus Torvalds 已提交
830

831 832 833 834 835 836
	if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags))
		/*
		 * shortcut...on shutdown flush is called next, just
		 * let it do all the work
		 */
		return;
L
Linus Torvalds 已提交
837

838
	ipoib_dbg_mcast(priv, "restarting multicast task\n");
839

H
Herbert Xu 已提交
840
	local_irq_save(flags);
841
	netif_addr_lock(dev);
842
	spin_lock(&priv->lock);
L
Linus Torvalds 已提交
843 844 845 846 847 848 849 850 851 852 853 854

	/*
	 * Unfortunately, the networking core only gives us a list of all of
	 * the multicast hardware addresses. We need to figure out which ones
	 * are new and which ones have been removed
	 */

	/* Clear out the found flag */
	list_for_each_entry(mcast, &priv->multicast_list, list)
		clear_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);

	/* Mark all of the entries that are found or don't exist */
855
	netdev_for_each_mc_addr(ha, dev) {
L
Linus Torvalds 已提交
856 857
		union ib_gid mgid;

858
		if (!ipoib_mcast_addr_is_valid(ha->addr, dev->broadcast))
859 860
			continue;

861
		memcpy(mgid.raw, ha->addr + 4, sizeof mgid);
L
Linus Torvalds 已提交
862 863 864 865 866

		mcast = __ipoib_mcast_find(dev, &mgid);
		if (!mcast || test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
			struct ipoib_mcast *nmcast;

867 868 869
			/* ignore group which is directly joined by userspace */
			if (test_bit(IPOIB_FLAG_UMCAST, &priv->flags) &&
			    !ib_sa_get_mcmember_rec(priv->ca, priv->port, &mgid, &rec)) {
H
Harvey Harrison 已提交
870
				ipoib_dbg_mcast(priv, "ignoring multicast entry for mgid %pI6\n",
871
						mgid.raw);
872 873 874
				continue;
			}

L
Linus Torvalds 已提交
875
			/* Not found or send-only group, let's add a new entry */
H
Harvey Harrison 已提交
876
			ipoib_dbg_mcast(priv, "adding multicast entry for mgid %pI6\n",
877
					mgid.raw);
L
Linus Torvalds 已提交
878 879 880 881 882 883 884 885 886 887 888 889 890

			nmcast = ipoib_mcast_alloc(dev, 0);
			if (!nmcast) {
				ipoib_warn(priv, "unable to allocate memory for multicast structure\n");
				continue;
			}

			set_bit(IPOIB_MCAST_FLAG_FOUND, &nmcast->flags);

			nmcast->mcmember.mgid = mgid;

			if (mcast) {
				/* Destroy the send only entry */
A
Akinobu Mita 已提交
891
				list_move_tail(&mcast->list, &remove_list);
L
Linus Torvalds 已提交
892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909

				rb_replace_node(&mcast->rb_node,
						&nmcast->rb_node,
						&priv->multicast_tree);
			} else
				__ipoib_mcast_add(dev, nmcast);

			list_add_tail(&nmcast->list, &priv->multicast_list);
		}

		if (mcast)
			set_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags);
	}

	/* Remove all of the entries don't exist anymore */
	list_for_each_entry_safe(mcast, tmcast, &priv->multicast_list, list) {
		if (!test_bit(IPOIB_MCAST_FLAG_FOUND, &mcast->flags) &&
		    !test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
H
Harvey Harrison 已提交
910
			ipoib_dbg_mcast(priv, "deleting multicast group %pI6\n",
911
					mcast->mcmember.mgid.raw);
L
Linus Torvalds 已提交
912 913 914 915

			rb_erase(&mcast->rb_node, &priv->multicast_tree);

			/* Move to the remove list */
A
Akinobu Mita 已提交
916
			list_move_tail(&mcast->list, &remove_list);
L
Linus Torvalds 已提交
917 918
		}
	}
919 920

	spin_unlock(&priv->lock);
921
	netif_addr_unlock(dev);
H
Herbert Xu 已提交
922
	local_irq_restore(flags);
L
Linus Torvalds 已提交
923

924 925 926 927 928 929 930 931
	/*
	 * make sure the in-flight joins have finished before we attempt
	 * to leave
	 */
	list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
		if (test_bit(IPOIB_MCAST_FLAG_BUSY, &mcast->flags))
			wait_for_completion(&mcast->done);

L
Linus Torvalds 已提交
932 933 934 935
	list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
		ipoib_mcast_leave(mcast->dev, mcast);
		ipoib_mcast_free(mcast);
	}
936

937 938 939 940 941 942 943 944
	/*
	 * Double check that we are still up
	 */
	if (test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
		spin_lock_irqsave(&priv->lock, flags);
		__ipoib_mcast_schedule_join_thread(priv, NULL, 0);
		spin_unlock_irqrestore(&priv->lock, flags);
	}
L
Linus Torvalds 已提交
945 946
}

947 948
#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG

L
Linus Torvalds 已提交
949 950 951 952 953 954 955 956 957
struct ipoib_mcast_iter *ipoib_mcast_iter_init(struct net_device *dev)
{
	struct ipoib_mcast_iter *iter;

	iter = kmalloc(sizeof *iter, GFP_KERNEL);
	if (!iter)
		return NULL;

	iter->dev = dev;
958
	memset(iter->mgid.raw, 0, 16);
L
Linus Torvalds 已提交
959 960

	if (ipoib_mcast_iter_next(iter)) {
961
		kfree(iter);
L
Linus Torvalds 已提交
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
		return NULL;
	}

	return iter;
}

int ipoib_mcast_iter_next(struct ipoib_mcast_iter *iter)
{
	struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
	struct rb_node *n;
	struct ipoib_mcast *mcast;
	int ret = 1;

	spin_lock_irq(&priv->lock);

	n = rb_first(&priv->multicast_tree);

	while (n) {
		mcast = rb_entry(n, struct ipoib_mcast, rb_node);

		if (memcmp(iter->mgid.raw, mcast->mcmember.mgid.raw,
			   sizeof (union ib_gid)) < 0) {
			iter->mgid      = mcast->mcmember.mgid;
			iter->created   = mcast->created;
			iter->queuelen  = skb_queue_len(&mcast->pkt_queue);
			iter->complete  = !!mcast->ah;
			iter->send_only = !!(mcast->flags & (1 << IPOIB_MCAST_FLAG_SENDONLY));

			ret = 0;

			break;
		}

		n = rb_next(n);
	}

	spin_unlock_irq(&priv->lock);

	return ret;
}

void ipoib_mcast_iter_read(struct ipoib_mcast_iter *iter,
			   union ib_gid *mgid,
			   unsigned long *created,
			   unsigned int *queuelen,
			   unsigned int *complete,
			   unsigned int *send_only)
{
	*mgid      = iter->mgid;
	*created   = iter->created;
	*queuelen  = iter->queuelen;
	*complete  = iter->complete;
	*send_only = iter->send_only;
}
1016 1017

#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */